UPSTREAM: of/pci: Add helper function to parse max-link-speed from dt
[firefly-linux-kernel-4.4.55.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 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         fput(new_file);
143 out_fput:
144         fput(old_file);
145         return error;
146 }
147
148 static char *ovl_read_symlink(struct dentry *realdentry)
149 {
150         int res;
151         char *buf;
152         struct inode *inode = realdentry->d_inode;
153         mm_segment_t old_fs;
154
155         res = -EINVAL;
156         if (!inode->i_op->readlink)
157                 goto err;
158
159         res = -ENOMEM;
160         buf = (char *) __get_free_page(GFP_KERNEL);
161         if (!buf)
162                 goto err;
163
164         old_fs = get_fs();
165         set_fs(get_ds());
166         /* The cast to a user pointer is valid due to the set_fs() */
167         res = inode->i_op->readlink(realdentry,
168                                     (char __user *)buf, PAGE_SIZE - 1);
169         set_fs(old_fs);
170         if (res < 0) {
171                 free_page((unsigned long) buf);
172                 goto err;
173         }
174         buf[res] = '\0';
175
176         return buf;
177
178 err:
179         return ERR_PTR(res);
180 }
181
182 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
183 {
184         struct iattr attr = {
185                 .ia_valid =
186                      ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
187                 .ia_atime = stat->atime,
188                 .ia_mtime = stat->mtime,
189         };
190
191         return notify_change(upperdentry, &attr, NULL);
192 }
193
194 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
195 {
196         int err = 0;
197
198         if (!S_ISLNK(stat->mode)) {
199                 struct iattr attr = {
200                         .ia_valid = ATTR_MODE,
201                         .ia_mode = stat->mode,
202                 };
203                 err = notify_change(upperdentry, &attr, NULL);
204         }
205         if (!err) {
206                 struct iattr attr = {
207                         .ia_valid = ATTR_UID | ATTR_GID,
208                         .ia_uid = stat->uid,
209                         .ia_gid = stat->gid,
210                 };
211                 err = notify_change(upperdentry, &attr, NULL);
212         }
213         if (!err)
214                 ovl_set_timestamps(upperdentry, stat);
215
216         return err;
217 }
218
219 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
220                               struct dentry *dentry, struct path *lowerpath,
221                               struct kstat *stat, const char *link)
222 {
223         struct inode *wdir = workdir->d_inode;
224         struct inode *udir = upperdir->d_inode;
225         struct dentry *newdentry = NULL;
226         struct dentry *upper = NULL;
227         umode_t mode = stat->mode;
228         int err;
229
230         newdentry = ovl_lookup_temp(workdir, dentry);
231         err = PTR_ERR(newdentry);
232         if (IS_ERR(newdentry))
233                 goto out;
234
235         upper = lookup_one_len(dentry->d_name.name, upperdir,
236                                dentry->d_name.len);
237         err = PTR_ERR(upper);
238         if (IS_ERR(upper))
239                 goto out1;
240
241         /* Can't properly set mode on creation because of the umask */
242         stat->mode &= S_IFMT;
243         err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
244         stat->mode = mode;
245         if (err)
246                 goto out2;
247
248         if (S_ISREG(stat->mode)) {
249                 struct path upperpath;
250                 ovl_path_upper(dentry, &upperpath);
251                 BUG_ON(upperpath.dentry != NULL);
252                 upperpath.dentry = newdentry;
253
254                 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
255                 if (err)
256                         goto out_cleanup;
257         }
258
259         err = ovl_copy_xattr(lowerpath->dentry, newdentry);
260         if (err)
261                 goto out_cleanup;
262
263         mutex_lock(&newdentry->d_inode->i_mutex);
264         err = ovl_set_attr(newdentry, stat);
265         mutex_unlock(&newdentry->d_inode->i_mutex);
266         if (err)
267                 goto out_cleanup;
268
269         err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
270         if (err)
271                 goto out_cleanup;
272
273         ovl_dentry_update(dentry, newdentry);
274         newdentry = NULL;
275
276         /*
277          * Non-directores become opaque when copied up.
278          */
279         if (!S_ISDIR(stat->mode))
280                 ovl_dentry_set_opaque(dentry, true);
281 out2:
282         dput(upper);
283 out1:
284         dput(newdentry);
285 out:
286         return err;
287
288 out_cleanup:
289         ovl_cleanup(wdir, newdentry);
290         goto out2;
291 }
292
293 /*
294  * Copy up a single dentry
295  *
296  * Directory renames only allowed on "pure upper" (already created on
297  * upper filesystem, never copied up).  Directories which are on lower or
298  * are merged may not be renamed.  For these -EXDEV is returned and
299  * userspace has to deal with it.  This means, when copying up a
300  * directory we can rely on it and ancestors being stable.
301  *
302  * Non-directory renames start with copy up of source if necessary.  The
303  * actual rename will only proceed once the copy up was successful.  Copy
304  * up uses upper parent i_mutex for exclusion.  Since rename can change
305  * d_parent it is possible that the copy up will lock the old parent.  At
306  * that point the file will have already been copied up anyway.
307  */
308 int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
309                     struct path *lowerpath, struct kstat *stat)
310 {
311         struct dentry *workdir = ovl_workdir(dentry);
312         int err;
313         struct kstat pstat;
314         struct path parentpath;
315         struct dentry *upperdir;
316         struct dentry *upperdentry;
317         const struct cred *old_cred;
318         struct cred *override_cred;
319         char *link = NULL;
320
321         if (WARN_ON(!workdir))
322                 return -EROFS;
323
324         ovl_path_upper(parent, &parentpath);
325         upperdir = parentpath.dentry;
326
327         err = vfs_getattr(&parentpath, &pstat);
328         if (err)
329                 return err;
330
331         if (S_ISLNK(stat->mode)) {
332                 link = ovl_read_symlink(lowerpath->dentry);
333                 if (IS_ERR(link))
334                         return PTR_ERR(link);
335         }
336
337         err = -ENOMEM;
338         override_cred = prepare_creds();
339         if (!override_cred)
340                 goto out_free_link;
341
342         override_cred->fsuid = stat->uid;
343         override_cred->fsgid = stat->gid;
344         /*
345          * CAP_SYS_ADMIN for copying up extended attributes
346          * CAP_DAC_OVERRIDE for create
347          * CAP_FOWNER for chmod, timestamp update
348          * CAP_FSETID for chmod
349          * CAP_CHOWN for chown
350          * CAP_MKNOD for mknod
351          */
352         cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
353         cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
354         cap_raise(override_cred->cap_effective, CAP_FOWNER);
355         cap_raise(override_cred->cap_effective, CAP_FSETID);
356         cap_raise(override_cred->cap_effective, CAP_CHOWN);
357         cap_raise(override_cred->cap_effective, CAP_MKNOD);
358         old_cred = override_creds(override_cred);
359
360         err = -EIO;
361         if (lock_rename(workdir, upperdir) != NULL) {
362                 pr_err("overlayfs: failed to lock workdir+upperdir\n");
363                 goto out_unlock;
364         }
365         upperdentry = ovl_dentry_upper(dentry);
366         if (upperdentry) {
367                 /* Raced with another copy-up?  Nothing to do, then... */
368                 err = 0;
369                 goto out_unlock;
370         }
371
372         err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
373                                  stat, link);
374         if (!err) {
375                 /* Restore timestamps on parent (best effort) */
376                 ovl_set_timestamps(upperdir, &pstat);
377         }
378 out_unlock:
379         unlock_rename(workdir, upperdir);
380         revert_creds(old_cred);
381         put_cred(override_cred);
382
383 out_free_link:
384         if (link)
385                 free_page((unsigned long) link);
386
387         return err;
388 }
389
390 int ovl_copy_up(struct dentry *dentry)
391 {
392         int err;
393
394         err = 0;
395         while (!err) {
396                 struct dentry *next;
397                 struct dentry *parent;
398                 struct path lowerpath;
399                 struct kstat stat;
400                 enum ovl_path_type type = ovl_path_type(dentry);
401
402                 if (OVL_TYPE_UPPER(type))
403                         break;
404
405                 next = dget(dentry);
406                 /* find the topmost dentry not yet copied up */
407                 for (;;) {
408                         parent = dget_parent(next);
409
410                         type = ovl_path_type(parent);
411                         if (OVL_TYPE_UPPER(type))
412                                 break;
413
414                         dput(next);
415                         next = parent;
416                 }
417
418                 ovl_path_lower(next, &lowerpath);
419                 err = vfs_getattr(&lowerpath, &stat);
420                 if (!err)
421                         err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
422
423                 dput(parent);
424                 dput(next);
425         }
426
427         return err;
428 }