Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / 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 uninitialized_var(error);
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         struct cred *override_cred;
321         char *link = NULL;
322
323         if (WARN_ON(!workdir))
324                 return -EROFS;
325
326         ovl_path_upper(parent, &parentpath);
327         upperdir = parentpath.dentry;
328
329         err = vfs_getattr(&parentpath, &pstat);
330         if (err)
331                 return err;
332
333         if (S_ISLNK(stat->mode)) {
334                 link = ovl_read_symlink(lowerpath->dentry);
335                 if (IS_ERR(link))
336                         return PTR_ERR(link);
337         }
338
339         err = -ENOMEM;
340         override_cred = prepare_creds();
341         if (!override_cred)
342                 goto out_free_link;
343
344         override_cred->fsuid = stat->uid;
345         override_cred->fsgid = stat->gid;
346         /*
347          * CAP_SYS_ADMIN for copying up extended attributes
348          * CAP_DAC_OVERRIDE for create
349          * CAP_FOWNER for chmod, timestamp update
350          * CAP_FSETID for chmod
351          * CAP_CHOWN for chown
352          * CAP_MKNOD for mknod
353          */
354         cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
355         cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
356         cap_raise(override_cred->cap_effective, CAP_FOWNER);
357         cap_raise(override_cred->cap_effective, CAP_FSETID);
358         cap_raise(override_cred->cap_effective, CAP_CHOWN);
359         cap_raise(override_cred->cap_effective, CAP_MKNOD);
360         old_cred = override_creds(override_cred);
361
362         err = -EIO;
363         if (lock_rename(workdir, upperdir) != NULL) {
364                 pr_err("overlayfs: failed to lock workdir+upperdir\n");
365                 goto out_unlock;
366         }
367         upperdentry = ovl_dentry_upper(dentry);
368         if (upperdentry) {
369                 /* Raced with another copy-up?  Nothing to do, then... */
370                 err = 0;
371                 goto out_unlock;
372         }
373
374         err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
375                                  stat, link);
376         if (!err) {
377                 /* Restore timestamps on parent (best effort) */
378                 ovl_set_timestamps(upperdir, &pstat);
379         }
380 out_unlock:
381         unlock_rename(workdir, upperdir);
382         revert_creds(old_cred);
383         put_cred(override_cred);
384
385 out_free_link:
386         if (link)
387                 free_page((unsigned long) link);
388
389         return err;
390 }
391
392 int ovl_copy_up(struct dentry *dentry)
393 {
394         int err;
395
396         err = 0;
397         while (!err) {
398                 struct dentry *next;
399                 struct dentry *parent;
400                 struct path lowerpath;
401                 struct kstat stat;
402                 enum ovl_path_type type = ovl_path_type(dentry);
403
404                 if (OVL_TYPE_UPPER(type))
405                         break;
406
407                 next = dget(dentry);
408                 /* find the topmost dentry not yet copied up */
409                 for (;;) {
410                         parent = dget_parent(next);
411
412                         type = ovl_path_type(parent);
413                         if (OVL_TYPE_UPPER(type))
414                                 break;
415
416                         dput(next);
417                         next = parent;
418                 }
419
420                 ovl_path_lower(next, &lowerpath);
421                 err = vfs_getattr(&lowerpath, &stat);
422                 if (!err)
423                         err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
424
425                 dput(parent);
426                 dput(next);
427         }
428
429         return err;
430 }