ovl: introduce copy up waitqueue
[linux-block.git] / fs / overlayfs / copy_up.c
CommitLineData
e9be9d5e
MS
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
fb5bb2c3 10#include <linux/module.h>
e9be9d5e
MS
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.h>
19#include <linux/namei.h>
fb5bb2c3
DH
20#include <linux/fdtable.h>
21#include <linux/ratelimit.h>
e9be9d5e 22#include "overlayfs.h"
d8514d8e 23#include "ovl_entry.h"
e9be9d5e
MS
24
25#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
26
fb5bb2c3
DH
27static bool __read_mostly ovl_check_copy_up;
28module_param_named(check_copy_up, ovl_check_copy_up, bool,
29 S_IWUSR | S_IRUGO);
30MODULE_PARM_DESC(ovl_check_copy_up,
31 "Warn on copy-up when causing process also has a R/O fd open");
32
33static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
34{
35 const struct dentry *dentry = data;
36
45063097 37 if (file_inode(f) == d_inode(dentry))
fb5bb2c3
DH
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 */
50static 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
e9be9d5e
MS
56int ovl_copy_xattr(struct dentry *old, struct dentry *new)
57{
e4ad29fa
VC
58 ssize_t list_size, size, value_size = 0;
59 char *buf, *name, *value = NULL;
60 int uninitialized_var(error);
8b326c61 61 size_t slen;
e9be9d5e 62
5d6c3191
AG
63 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
64 !(new->d_inode->i_opflags & IOP_XATTR))
e9be9d5e
MS
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
e9be9d5e
MS
78 list_size = vfs_listxattr(old, buf, list_size);
79 if (list_size <= 0) {
80 error = list_size;
e4ad29fa 81 goto out;
e9be9d5e
MS
82 }
83
8b326c61
MS
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
0956254a
MS
94 if (ovl_is_private_xattr(name))
95 continue;
e4ad29fa
VC
96retry:
97 size = vfs_getxattr(old, name, value, value_size);
98 if (size == -ERANGE)
99 size = vfs_getxattr(old, name, NULL, 0);
100
97daf8b9 101 if (size < 0) {
e9be9d5e 102 error = size;
e4ad29fa 103 break;
e9be9d5e 104 }
e4ad29fa
VC
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
121ab822
VG
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 }
e9be9d5e
MS
126 error = vfs_setxattr(new, name, value, size, 0);
127 if (error)
e4ad29fa 128 break;
e9be9d5e 129 }
e9be9d5e
MS
130 kfree(value);
131out:
132 kfree(buf);
133 return error;
134}
135
136static 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
0480334f 147 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
e9be9d5e
MS
148 if (IS_ERR(old_file))
149 return PTR_ERR(old_file);
150
0480334f 151 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
e9be9d5e
MS
152 if (IS_ERR(new_file)) {
153 error = PTR_ERR(new_file);
154 goto out_fput;
155 }
156
2ea98466
AG
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
e9be9d5e
MS
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 }
2ea98466 188out:
641089c1
MS
189 if (!error)
190 error = vfs_fsync(new_file, 0);
e9be9d5e
MS
191 fput(new_file);
192out_fput:
193 fput(old_file);
194 return error;
195}
196
e9be9d5e
MS
197static 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
209int 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;
e9be9d5e
MS
232}
233
234static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
235 struct dentry *dentry, struct path *lowerpath,
42f269b9 236 struct kstat *stat, const char *link,
d8514d8e 237 struct kstat *pstat, bool tmpfile)
e9be9d5e
MS
238{
239 struct inode *wdir = workdir->d_inode;
240 struct inode *udir = upperdir->d_inode;
241 struct dentry *newdentry = NULL;
242 struct dentry *upper = NULL;
42f269b9 243 struct dentry *temp = NULL;
e9be9d5e 244 int err;
d8ad8b49
VG
245 const struct cred *old_creds = NULL;
246 struct cred *new_creds = NULL;
32a3d848
AV
247 struct cattr cattr = {
248 /* Can't properly set mode on creation because of the umask */
249 .mode = stat->mode & S_IFMT,
250 .rdev = stat->rdev,
251 .link = link
252 };
e9be9d5e 253
e9be9d5e
MS
254 upper = lookup_one_len(dentry->d_name.name, upperdir,
255 dentry->d_name.len);
256 err = PTR_ERR(upper);
257 if (IS_ERR(upper))
42f269b9 258 goto out;
e9be9d5e 259
d8ad8b49
VG
260 err = security_inode_copy_up(dentry, &new_creds);
261 if (err < 0)
42f269b9 262 goto out1;
d8ad8b49
VG
263
264 if (new_creds)
265 old_creds = override_creds(new_creds);
266
d8514d8e
AG
267 if (tmpfile)
268 temp = ovl_do_tmpfile(upperdir, stat->mode);
269 else
270 temp = ovl_lookup_temp(workdir, dentry);
42f269b9
AG
271 err = PTR_ERR(temp);
272 if (IS_ERR(temp))
273 goto out1;
274
d8514d8e
AG
275 err = 0;
276 if (!tmpfile)
277 err = ovl_create_real(wdir, temp, &cattr, NULL, true);
d8ad8b49
VG
278
279 if (new_creds) {
280 revert_creds(old_creds);
281 put_cred(new_creds);
282 }
283
e9be9d5e
MS
284 if (err)
285 goto out2;
286
287 if (S_ISREG(stat->mode)) {
288 struct path upperpath;
f134f244 289
e9be9d5e
MS
290 ovl_path_upper(dentry, &upperpath);
291 BUG_ON(upperpath.dentry != NULL);
42f269b9 292 upperpath.dentry = temp;
e9be9d5e
MS
293
294 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
295 if (err)
296 goto out_cleanup;
297 }
298
42f269b9 299 err = ovl_copy_xattr(lowerpath->dentry, temp);
e9be9d5e
MS
300 if (err)
301 goto out_cleanup;
302
42f269b9
AG
303 inode_lock(temp->d_inode);
304 err = ovl_set_attr(temp, stat);
305 inode_unlock(temp->d_inode);
e9be9d5e
MS
306 if (err)
307 goto out_cleanup;
308
d8514d8e
AG
309 if (tmpfile)
310 err = ovl_do_link(temp, udir, upper, true);
311 else
312 err = ovl_do_rename(wdir, temp, udir, upper, 0);
e9be9d5e
MS
313 if (err)
314 goto out_cleanup;
315
d8514d8e 316 newdentry = dget(tmpfile ? upper : temp);
e9be9d5e 317 ovl_dentry_update(dentry, newdentry);
39b681f8 318 ovl_inode_update(d_inode(dentry), d_inode(newdentry));
42f269b9
AG
319
320 /* Restore timestamps on parent (best effort) */
321 ovl_set_timestamps(upperdir, pstat);
e9be9d5e 322out2:
42f269b9 323 dput(temp);
e9be9d5e 324out1:
42f269b9 325 dput(upper);
e9be9d5e
MS
326out:
327 return err;
328
329out_cleanup:
d8514d8e
AG
330 if (!tmpfile)
331 ovl_cleanup(wdir, temp);
ab79efab 332 goto out2;
e9be9d5e
MS
333}
334
335/*
336 * Copy up a single dentry
337 *
a6c60655
MS
338 * All renames start with copy up of source if necessary. The actual
339 * rename will only proceed once the copy up was successful. Copy up uses
340 * upper parent i_mutex for exclusion. Since rename can change d_parent it
341 * is possible that the copy up will lock the old parent. At that point
342 * the file will have already been copied up anyway.
e9be9d5e 343 */
9aba6521
AG
344static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
345 struct path *lowerpath, struct kstat *stat)
e9be9d5e 346{
7764235b 347 DEFINE_DELAYED_CALL(done);
e9be9d5e
MS
348 struct dentry *workdir = ovl_workdir(dentry);
349 int err;
350 struct kstat pstat;
351 struct path parentpath;
7764235b 352 struct dentry *lowerdentry = lowerpath->dentry;
e9be9d5e 353 struct dentry *upperdir;
7764235b 354 const char *link = NULL;
d8514d8e
AG
355 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
356 /* Should we copyup with O_TMPFILE or with workdir? */
357 bool tmpfile = S_ISREG(stat->mode) && ofs->tmpfile;
e9be9d5e 358
cc6f67bc
MS
359 if (WARN_ON(!workdir))
360 return -EROFS;
361
7764235b 362 ovl_do_check_copy_up(lowerdentry);
fb5bb2c3 363
e9be9d5e
MS
364 ovl_path_upper(parent, &parentpath);
365 upperdir = parentpath.dentry;
366
367 err = vfs_getattr(&parentpath, &pstat);
368 if (err)
369 return err;
370
371 if (S_ISLNK(stat->mode)) {
7764235b 372 link = vfs_get_link(lowerdentry, &done);
e9be9d5e
MS
373 if (IS_ERR(link))
374 return PTR_ERR(link);
375 }
376
e9be9d5e
MS
377 err = -EIO;
378 if (lock_rename(workdir, upperdir) != NULL) {
379 pr_err("overlayfs: failed to lock workdir+upperdir\n");
380 goto out_unlock;
381 }
a6c60655 382 if (ovl_dentry_upper(dentry)) {
0f7ff2da 383 /* Raced with another copy-up? Nothing to do, then... */
e9be9d5e 384 err = 0;
0f7ff2da 385 goto out_unlock;
e9be9d5e
MS
386 }
387
388 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
d8514d8e 389 stat, link, &pstat, tmpfile);
e9be9d5e
MS
390out_unlock:
391 unlock_rename(workdir, upperdir);
7764235b 392 do_delayed_call(&done);
e9be9d5e
MS
393
394 return err;
395}
396
9aba6521 397int ovl_copy_up_flags(struct dentry *dentry, int flags)
e9be9d5e 398{
8eac98b8
VG
399 int err = 0;
400 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
e9be9d5e 401
e9be9d5e
MS
402 while (!err) {
403 struct dentry *next;
404 struct dentry *parent;
405 struct path lowerpath;
406 struct kstat stat;
407 enum ovl_path_type type = ovl_path_type(dentry);
408
1afaba1e 409 if (OVL_TYPE_UPPER(type))
e9be9d5e
MS
410 break;
411
412 next = dget(dentry);
413 /* find the topmost dentry not yet copied up */
414 for (;;) {
415 parent = dget_parent(next);
416
417 type = ovl_path_type(parent);
1afaba1e 418 if (OVL_TYPE_UPPER(type))
e9be9d5e
MS
419 break;
420
421 dput(next);
422 next = parent;
423 }
424
425 ovl_path_lower(next, &lowerpath);
426 err = vfs_getattr(&lowerpath, &stat);
9aba6521
AG
427 /* maybe truncate regular file. this has no effect on dirs */
428 if (flags & O_TRUNC)
429 stat.size = 0;
e9be9d5e 430 if (!err)
0f7ff2da 431 err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
e9be9d5e
MS
432
433 dput(parent);
434 dput(next);
435 }
8eac98b8 436 revert_creds(old_cred);
e9be9d5e
MS
437
438 return err;
439}
9aba6521
AG
440
441int ovl_copy_up(struct dentry *dentry)
442{
443 return ovl_copy_up_flags(dentry, 0);
444}