sched/headers: Prepare to move signal wakeup & sigpending methods from <linux/sched...
[platform/kernel/linux-starfive.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/module.h>
11 #include <linux/fs.h>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/splice.h>
15 #include <linux/xattr.h>
16 #include <linux/security.h>
17 #include <linux/uaccess.h>
18 #include <linux/sched/signal.h>
19 #include <linux/cred.h>
20 #include <linux/namei.h>
21 #include <linux/fdtable.h>
22 #include <linux/ratelimit.h>
23 #include "overlayfs.h"
24
25 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
26
27 static bool __read_mostly ovl_check_copy_up;
28 module_param_named(check_copy_up, ovl_check_copy_up, bool,
29                    S_IWUSR | S_IRUGO);
30 MODULE_PARM_DESC(ovl_check_copy_up,
31                  "Warn on copy-up when causing process also has a R/O fd open");
32
33 static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
34 {
35         const struct dentry *dentry = data;
36
37         if (file_inode(f) == d_inode(dentry))
38                 pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
39                                     f, fd, current->pid, current->comm);
40         return 0;
41 }
42
43 /*
44  * Check the fds open by this process and warn if something like the following
45  * scenario is about to occur:
46  *
47  *      fd1 = open("foo", O_RDONLY);
48  *      fd2 = open("foo", O_RDWR);
49  */
50 static void ovl_do_check_copy_up(struct dentry *dentry)
51 {
52         if (ovl_check_copy_up)
53                 iterate_fd(current->files, 0, ovl_check_fd, dentry);
54 }
55
56 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
57 {
58         ssize_t list_size, size, value_size = 0;
59         char *buf, *name, *value = NULL;
60         int uninitialized_var(error);
61         size_t slen;
62
63         if (!(old->d_inode->i_opflags & IOP_XATTR) ||
64             !(new->d_inode->i_opflags & IOP_XATTR))
65                 return 0;
66
67         list_size = vfs_listxattr(old, NULL, 0);
68         if (list_size <= 0) {
69                 if (list_size == -EOPNOTSUPP)
70                         return 0;
71                 return list_size;
72         }
73
74         buf = kzalloc(list_size, GFP_KERNEL);
75         if (!buf)
76                 return -ENOMEM;
77
78         list_size = vfs_listxattr(old, buf, list_size);
79         if (list_size <= 0) {
80                 error = list_size;
81                 goto out;
82         }
83
84         for (name = buf; list_size; name += slen) {
85                 slen = strnlen(name, list_size) + 1;
86
87                 /* underlying fs providing us with an broken xattr list? */
88                 if (WARN_ON(slen > list_size)) {
89                         error = -EIO;
90                         break;
91                 }
92                 list_size -= slen;
93
94                 if (ovl_is_private_xattr(name))
95                         continue;
96 retry:
97                 size = vfs_getxattr(old, name, value, value_size);
98                 if (size == -ERANGE)
99                         size = vfs_getxattr(old, name, NULL, 0);
100
101                 if (size < 0) {
102                         error = size;
103                         break;
104                 }
105
106                 if (size > value_size) {
107                         void *new;
108
109                         new = krealloc(value, size, GFP_KERNEL);
110                         if (!new) {
111                                 error = -ENOMEM;
112                                 break;
113                         }
114                         value = new;
115                         value_size = size;
116                         goto retry;
117                 }
118
119                 error = security_inode_copy_up_xattr(name);
120                 if (error < 0 && error != -EOPNOTSUPP)
121                         break;
122                 if (error == 1) {
123                         error = 0;
124                         continue; /* Discard */
125                 }
126                 error = vfs_setxattr(new, name, value, size, 0);
127                 if (error)
128                         break;
129         }
130         kfree(value);
131 out:
132         kfree(buf);
133         return error;
134 }
135
136 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
137 {
138         struct file *old_file;
139         struct file *new_file;
140         loff_t old_pos = 0;
141         loff_t new_pos = 0;
142         int error = 0;
143
144         if (len == 0)
145                 return 0;
146
147         old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
148         if (IS_ERR(old_file))
149                 return PTR_ERR(old_file);
150
151         new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
152         if (IS_ERR(new_file)) {
153                 error = PTR_ERR(new_file);
154                 goto out_fput;
155         }
156
157         /* Try to use clone_file_range to clone up within the same fs */
158         error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
159         if (!error)
160                 goto out;
161         /* Couldn't clone, so now we try to copy the data */
162         error = 0;
163
164         /* FIXME: copy up sparse files efficiently */
165         while (len) {
166                 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
167                 long bytes;
168
169                 if (len < this_len)
170                         this_len = len;
171
172                 if (signal_pending_state(TASK_KILLABLE, current)) {
173                         error = -EINTR;
174                         break;
175                 }
176
177                 bytes = do_splice_direct(old_file, &old_pos,
178                                          new_file, &new_pos,
179                                          this_len, SPLICE_F_MOVE);
180                 if (bytes <= 0) {
181                         error = bytes;
182                         break;
183                 }
184                 WARN_ON(old_pos != new_pos);
185
186                 len -= bytes;
187         }
188 out:
189         if (!error)
190                 error = vfs_fsync(new_file, 0);
191         fput(new_file);
192 out_fput:
193         fput(old_file);
194         return error;
195 }
196
197 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
198 {
199         struct iattr attr = {
200                 .ia_valid =
201                      ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
202                 .ia_atime = stat->atime,
203                 .ia_mtime = stat->mtime,
204         };
205
206         return notify_change(upperdentry, &attr, NULL);
207 }
208
209 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
210 {
211         int err = 0;
212
213         if (!S_ISLNK(stat->mode)) {
214                 struct iattr attr = {
215                         .ia_valid = ATTR_MODE,
216                         .ia_mode = stat->mode,
217                 };
218                 err = notify_change(upperdentry, &attr, NULL);
219         }
220         if (!err) {
221                 struct iattr attr = {
222                         .ia_valid = ATTR_UID | ATTR_GID,
223                         .ia_uid = stat->uid,
224                         .ia_gid = stat->gid,
225                 };
226                 err = notify_change(upperdentry, &attr, NULL);
227         }
228         if (!err)
229                 ovl_set_timestamps(upperdentry, stat);
230
231         return err;
232 }
233
234 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
235                               struct dentry *dentry, struct path *lowerpath,
236                               struct kstat *stat, const char *link)
237 {
238         struct inode *wdir = workdir->d_inode;
239         struct inode *udir = upperdir->d_inode;
240         struct dentry *newdentry = NULL;
241         struct dentry *upper = NULL;
242         int err;
243         const struct cred *old_creds = NULL;
244         struct cred *new_creds = NULL;
245         struct cattr cattr = {
246                 /* Can't properly set mode on creation because of the umask */
247                 .mode = stat->mode & S_IFMT,
248                 .rdev = stat->rdev,
249                 .link = link
250         };
251
252         newdentry = ovl_lookup_temp(workdir, dentry);
253         err = PTR_ERR(newdentry);
254         if (IS_ERR(newdentry))
255                 goto out;
256
257         upper = lookup_one_len(dentry->d_name.name, upperdir,
258                                dentry->d_name.len);
259         err = PTR_ERR(upper);
260         if (IS_ERR(upper))
261                 goto out1;
262
263         err = security_inode_copy_up(dentry, &new_creds);
264         if (err < 0)
265                 goto out2;
266
267         if (new_creds)
268                 old_creds = override_creds(new_creds);
269
270         err = ovl_create_real(wdir, newdentry, &cattr, NULL, true);
271
272         if (new_creds) {
273                 revert_creds(old_creds);
274                 put_cred(new_creds);
275         }
276
277         if (err)
278                 goto out2;
279
280         if (S_ISREG(stat->mode)) {
281                 struct path upperpath;
282
283                 ovl_path_upper(dentry, &upperpath);
284                 BUG_ON(upperpath.dentry != NULL);
285                 upperpath.dentry = newdentry;
286
287                 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
288                 if (err)
289                         goto out_cleanup;
290         }
291
292         err = ovl_copy_xattr(lowerpath->dentry, newdentry);
293         if (err)
294                 goto out_cleanup;
295
296         inode_lock(newdentry->d_inode);
297         err = ovl_set_attr(newdentry, stat);
298         inode_unlock(newdentry->d_inode);
299         if (err)
300                 goto out_cleanup;
301
302         err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
303         if (err)
304                 goto out_cleanup;
305
306         ovl_dentry_update(dentry, newdentry);
307         ovl_inode_update(d_inode(dentry), d_inode(newdentry));
308         newdentry = NULL;
309 out2:
310         dput(upper);
311 out1:
312         dput(newdentry);
313 out:
314         return err;
315
316 out_cleanup:
317         ovl_cleanup(wdir, newdentry);
318         goto out2;
319 }
320
321 /*
322  * Copy up a single dentry
323  *
324  * All renames start with copy up of source if necessary.  The actual
325  * rename will only proceed once the copy up was successful.  Copy up uses
326  * upper parent i_mutex for exclusion.  Since rename can change d_parent it
327  * is possible that the copy up will lock the old parent.  At that point
328  * the file will have already been copied up anyway.
329  */
330 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
331                            struct path *lowerpath, struct kstat *stat)
332 {
333         DEFINE_DELAYED_CALL(done);
334         struct dentry *workdir = ovl_workdir(dentry);
335         int err;
336         struct kstat pstat;
337         struct path parentpath;
338         struct dentry *lowerdentry = lowerpath->dentry;
339         struct dentry *upperdir;
340         const char *link = NULL;
341
342         if (WARN_ON(!workdir))
343                 return -EROFS;
344
345         ovl_do_check_copy_up(lowerdentry);
346
347         ovl_path_upper(parent, &parentpath);
348         upperdir = parentpath.dentry;
349
350         err = vfs_getattr(&parentpath, &pstat);
351         if (err)
352                 return err;
353
354         if (S_ISLNK(stat->mode)) {
355                 link = vfs_get_link(lowerdentry, &done);
356                 if (IS_ERR(link))
357                         return PTR_ERR(link);
358         }
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         if (ovl_dentry_upper(dentry)) {
366                 /* Raced with another copy-up?  Nothing to do, then... */
367                 err = 0;
368                 goto out_unlock;
369         }
370
371         err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
372                                  stat, link);
373         if (!err) {
374                 /* Restore timestamps on parent (best effort) */
375                 ovl_set_timestamps(upperdir, &pstat);
376         }
377 out_unlock:
378         unlock_rename(workdir, upperdir);
379         do_delayed_call(&done);
380
381         return err;
382 }
383
384 int ovl_copy_up_flags(struct dentry *dentry, int flags)
385 {
386         int err = 0;
387         const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
388
389         while (!err) {
390                 struct dentry *next;
391                 struct dentry *parent;
392                 struct path lowerpath;
393                 struct kstat stat;
394                 enum ovl_path_type type = ovl_path_type(dentry);
395
396                 if (OVL_TYPE_UPPER(type))
397                         break;
398
399                 next = dget(dentry);
400                 /* find the topmost dentry not yet copied up */
401                 for (;;) {
402                         parent = dget_parent(next);
403
404                         type = ovl_path_type(parent);
405                         if (OVL_TYPE_UPPER(type))
406                                 break;
407
408                         dput(next);
409                         next = parent;
410                 }
411
412                 ovl_path_lower(next, &lowerpath);
413                 err = vfs_getattr(&lowerpath, &stat);
414                 /* maybe truncate regular file. this has no effect on dirs */
415                 if (flags & O_TRUNC)
416                         stat.size = 0;
417                 if (!err)
418                         err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
419
420                 dput(parent);
421                 dput(next);
422         }
423         revert_creds(old_cred);
424
425         return err;
426 }
427
428 int ovl_copy_up(struct dentry *dentry)
429 {
430         return ovl_copy_up_flags(dentry, 0);
431 }