GNU Linux-libre 4.4.284-gnu1
[releases.git] / fs / overlayfs / copy_up.c
1 /*
2  *
3  * Copyright (C) 2011 Novell Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9
10 #include <linux/fs.h>
11 #include <linux/slab.h>
12 #include <linux/file.h>
13 #include <linux/splice.h>
14 #include <linux/xattr.h>
15 #include <linux/security.h>
16 #include <linux/uaccess.h>
17 #include <linux/sched.h>
18 #include <linux/namei.h>
19 #include "overlayfs.h"
20
21 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
22
23 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
24 {
25         ssize_t list_size, size, value_size = 0;
26         char *buf, *name, *value = NULL;
27         int error = 0;
28         size_t slen;
29
30         if (!old->d_inode->i_op->getxattr ||
31             !new->d_inode->i_op->getxattr)
32                 return 0;
33
34         list_size = vfs_listxattr(old, NULL, 0);
35         if (list_size <= 0) {
36                 if (list_size == -EOPNOTSUPP)
37                         return 0;
38                 return list_size;
39         }
40
41         buf = kzalloc(list_size, GFP_KERNEL);
42         if (!buf)
43                 return -ENOMEM;
44
45         list_size = vfs_listxattr(old, buf, list_size);
46         if (list_size <= 0) {
47                 error = list_size;
48                 goto out;
49         }
50
51         for (name = buf; list_size; name += slen) {
52                 slen = strnlen(name, list_size) + 1;
53
54                 /* underlying fs providing us with an broken xattr list? */
55                 if (WARN_ON(slen > list_size)) {
56                         error = -EIO;
57                         break;
58                 }
59                 list_size -= slen;
60
61                 if (ovl_is_private_xattr(name))
62                         continue;
63 retry:
64                 size = vfs_getxattr(old, name, value, value_size);
65                 if (size == -ERANGE)
66                         size = vfs_getxattr(old, name, NULL, 0);
67
68                 if (size < 0) {
69                         error = size;
70                         break;
71                 }
72
73                 if (size > value_size) {
74                         void *new;
75
76                         new = krealloc(value, size, GFP_KERNEL);
77                         if (!new) {
78                                 error = -ENOMEM;
79                                 break;
80                         }
81                         value = new;
82                         value_size = size;
83                         goto retry;
84                 }
85
86                 error = vfs_setxattr(new, name, value, size, 0);
87                 if (error)
88                         break;
89         }
90         kfree(value);
91 out:
92         kfree(buf);
93         return error;
94 }
95
96 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
97 {
98         struct file *old_file;
99         struct file *new_file;
100         loff_t old_pos = 0;
101         loff_t new_pos = 0;
102         int error = 0;
103
104         if (len == 0)
105                 return 0;
106
107         old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
108         if (IS_ERR(old_file))
109                 return PTR_ERR(old_file);
110
111         new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
112         if (IS_ERR(new_file)) {
113                 error = PTR_ERR(new_file);
114                 goto out_fput;
115         }
116
117         /* FIXME: copy up sparse files efficiently */
118         while (len) {
119                 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
120                 long bytes;
121
122                 if (len < this_len)
123                         this_len = len;
124
125                 if (signal_pending_state(TASK_KILLABLE, current)) {
126                         error = -EINTR;
127                         break;
128                 }
129
130                 bytes = do_splice_direct(old_file, &old_pos,
131                                          new_file, &new_pos,
132                                          this_len, SPLICE_F_MOVE);
133                 if (bytes <= 0) {
134                         error = bytes;
135                         break;
136                 }
137                 WARN_ON(old_pos != new_pos);
138
139                 len -= bytes;
140         }
141
142         if (!error)
143                 error = vfs_fsync(new_file, 0);
144         fput(new_file);
145 out_fput:
146         fput(old_file);
147         return error;
148 }
149
150 static char *ovl_read_symlink(struct dentry *realdentry)
151 {
152         int res;
153         char *buf;
154         struct inode *inode = realdentry->d_inode;
155         mm_segment_t old_fs;
156
157         res = -EINVAL;
158         if (!inode->i_op->readlink)
159                 goto err;
160
161         res = -ENOMEM;
162         buf = (char *) __get_free_page(GFP_KERNEL);
163         if (!buf)
164                 goto err;
165
166         old_fs = get_fs();
167         set_fs(get_ds());
168         /* The cast to a user pointer is valid due to the set_fs() */
169         res = inode->i_op->readlink(realdentry,
170                                     (char __user *)buf, PAGE_SIZE - 1);
171         set_fs(old_fs);
172         if (res < 0) {
173                 free_page((unsigned long) buf);
174                 goto err;
175         }
176         buf[res] = '\0';
177
178         return buf;
179
180 err:
181         return ERR_PTR(res);
182 }
183
184 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
185 {
186         struct iattr attr = {
187                 .ia_valid =
188                      ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
189                 .ia_atime = stat->atime,
190                 .ia_mtime = stat->mtime,
191         };
192
193         return notify_change(upperdentry, &attr, NULL);
194 }
195
196 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
197 {
198         int err = 0;
199
200         if (!S_ISLNK(stat->mode)) {
201                 struct iattr attr = {
202                         .ia_valid = ATTR_MODE,
203                         .ia_mode = stat->mode,
204                 };
205                 err = notify_change(upperdentry, &attr, NULL);
206         }
207         if (!err) {
208                 struct iattr attr = {
209                         .ia_valid = ATTR_UID | ATTR_GID,
210                         .ia_uid = stat->uid,
211                         .ia_gid = stat->gid,
212                 };
213                 err = notify_change(upperdentry, &attr, NULL);
214         }
215         if (!err)
216                 ovl_set_timestamps(upperdentry, stat);
217
218         return err;
219 }
220
221 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
222                               struct dentry *dentry, struct path *lowerpath,
223                               struct kstat *stat, const char *link)
224 {
225         struct inode *wdir = workdir->d_inode;
226         struct inode *udir = upperdir->d_inode;
227         struct dentry *newdentry = NULL;
228         struct dentry *upper = NULL;
229         umode_t mode = stat->mode;
230         int err;
231
232         newdentry = ovl_lookup_temp(workdir, dentry);
233         err = PTR_ERR(newdentry);
234         if (IS_ERR(newdentry))
235                 goto out;
236
237         upper = lookup_one_len(dentry->d_name.name, upperdir,
238                                dentry->d_name.len);
239         err = PTR_ERR(upper);
240         if (IS_ERR(upper))
241                 goto out1;
242
243         /* Can't properly set mode on creation because of the umask */
244         stat->mode &= S_IFMT;
245         err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
246         stat->mode = mode;
247         if (err)
248                 goto out2;
249
250         if (S_ISREG(stat->mode)) {
251                 struct path upperpath;
252                 ovl_path_upper(dentry, &upperpath);
253                 BUG_ON(upperpath.dentry != NULL);
254                 upperpath.dentry = newdentry;
255
256                 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
257                 if (err)
258                         goto out_cleanup;
259         }
260
261         err = ovl_copy_xattr(lowerpath->dentry, newdentry);
262         if (err)
263                 goto out_cleanup;
264
265         mutex_lock(&newdentry->d_inode->i_mutex);
266         err = ovl_set_attr(newdentry, stat);
267         mutex_unlock(&newdentry->d_inode->i_mutex);
268         if (err)
269                 goto out_cleanup;
270
271         err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
272         if (err)
273                 goto out_cleanup;
274
275         ovl_dentry_update(dentry, newdentry);
276         newdentry = NULL;
277
278         /*
279          * Non-directores become opaque when copied up.
280          */
281         if (!S_ISDIR(stat->mode))
282                 ovl_dentry_set_opaque(dentry, true);
283 out2:
284         dput(upper);
285 out1:
286         dput(newdentry);
287 out:
288         return err;
289
290 out_cleanup:
291         ovl_cleanup(wdir, newdentry);
292         goto out2;
293 }
294
295 /*
296  * Copy up a single dentry
297  *
298  * Directory renames only allowed on "pure upper" (already created on
299  * upper filesystem, never copied up).  Directories which are on lower or
300  * are merged may not be renamed.  For these -EXDEV is returned and
301  * userspace has to deal with it.  This means, when copying up a
302  * directory we can rely on it and ancestors being stable.
303  *
304  * Non-directory renames start with copy up of source if necessary.  The
305  * actual rename will only proceed once the copy up was successful.  Copy
306  * up uses upper parent i_mutex for exclusion.  Since rename can change
307  * d_parent it is possible that the copy up will lock the old parent.  At
308  * that point the file will have already been copied up anyway.
309  */
310 int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
311                     struct path *lowerpath, struct kstat *stat)
312 {
313         struct dentry *workdir = ovl_workdir(dentry);
314         int err;
315         struct kstat pstat;
316         struct path parentpath;
317         struct dentry *upperdir;
318         struct dentry *upperdentry;
319         const struct cred *old_cred;
320         char *link = NULL;
321
322         if (WARN_ON(!workdir))
323                 return -EROFS;
324
325         ovl_path_upper(parent, &parentpath);
326         upperdir = parentpath.dentry;
327
328         err = vfs_getattr(&parentpath, &pstat);
329         if (err)
330                 return err;
331
332         if (S_ISLNK(stat->mode)) {
333                 link = ovl_read_symlink(lowerpath->dentry);
334                 if (IS_ERR(link))
335                         return PTR_ERR(link);
336         }
337
338         old_cred = ovl_override_creds(dentry->d_sb);
339
340         err = -EIO;
341         if (lock_rename(workdir, upperdir) != NULL) {
342                 pr_err("overlayfs: failed to lock workdir+upperdir\n");
343                 goto out_unlock;
344         }
345         upperdentry = ovl_dentry_upper(dentry);
346         if (upperdentry) {
347                 /* Raced with another copy-up?  Nothing to do, then... */
348                 err = 0;
349                 goto out_unlock;
350         }
351
352         err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
353                                  stat, link);
354         if (!err) {
355                 /* Restore timestamps on parent (best effort) */
356                 ovl_set_timestamps(upperdir, &pstat);
357         }
358 out_unlock:
359         unlock_rename(workdir, upperdir);
360         revert_creds(old_cred);
361
362         if (link)
363                 free_page((unsigned long) link);
364
365         return err;
366 }
367
368 int ovl_copy_up(struct dentry *dentry)
369 {
370         int err;
371
372         err = 0;
373         while (!err) {
374                 struct dentry *next;
375                 struct dentry *parent;
376                 struct path lowerpath;
377                 struct kstat stat;
378                 enum ovl_path_type type = ovl_path_type(dentry);
379
380                 if (OVL_TYPE_UPPER(type))
381                         break;
382
383                 next = dget(dentry);
384                 /* find the topmost dentry not yet copied up */
385                 for (;;) {
386                         parent = dget_parent(next);
387
388                         type = ovl_path_type(parent);
389                         if (OVL_TYPE_UPPER(type))
390                                 break;
391
392                         dput(next);
393                         next = parent;
394                 }
395
396                 ovl_path_lower(next, &lowerpath);
397                 err = vfs_getattr(&lowerpath, &stat);
398                 if (!err)
399                         err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
400
401                 dput(parent);
402                 dput(next);
403         }
404
405         return err;
406 }