Commit | Line | Data |
---|---|---|
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> | |
174cd4b1 | 18 | #include <linux/sched/signal.h> |
5b825c3a | 19 | #include <linux/cred.h> |
e9be9d5e | 20 | #include <linux/namei.h> |
fb5bb2c3 DH |
21 | #include <linux/fdtable.h> |
22 | #include <linux/ratelimit.h> | |
3a1e819b | 23 | #include <linux/exportfs.h> |
e9be9d5e MS |
24 | #include "overlayfs.h" |
25 | ||
26 | #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) | |
27 | ||
fb5bb2c3 DH |
28 | static bool __read_mostly ovl_check_copy_up; |
29 | module_param_named(check_copy_up, ovl_check_copy_up, bool, | |
30 | S_IWUSR | S_IRUGO); | |
31 | MODULE_PARM_DESC(ovl_check_copy_up, | |
32 | "Warn on copy-up when causing process also has a R/O fd open"); | |
33 | ||
34 | static int ovl_check_fd(const void *data, struct file *f, unsigned int fd) | |
35 | { | |
36 | const struct dentry *dentry = data; | |
37 | ||
45063097 | 38 | if (file_inode(f) == d_inode(dentry)) |
fb5bb2c3 DH |
39 | 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", |
40 | f, fd, current->pid, current->comm); | |
41 | return 0; | |
42 | } | |
43 | ||
44 | /* | |
45 | * Check the fds open by this process and warn if something like the following | |
46 | * scenario is about to occur: | |
47 | * | |
48 | * fd1 = open("foo", O_RDONLY); | |
49 | * fd2 = open("foo", O_RDWR); | |
50 | */ | |
51 | static void ovl_do_check_copy_up(struct dentry *dentry) | |
52 | { | |
53 | if (ovl_check_copy_up) | |
54 | iterate_fd(current->files, 0, ovl_check_fd, dentry); | |
55 | } | |
56 | ||
e9be9d5e MS |
57 | int ovl_copy_xattr(struct dentry *old, struct dentry *new) |
58 | { | |
e4ad29fa VC |
59 | ssize_t list_size, size, value_size = 0; |
60 | char *buf, *name, *value = NULL; | |
61 | int uninitialized_var(error); | |
8b326c61 | 62 | size_t slen; |
e9be9d5e | 63 | |
5d6c3191 AG |
64 | if (!(old->d_inode->i_opflags & IOP_XATTR) || |
65 | !(new->d_inode->i_opflags & IOP_XATTR)) | |
e9be9d5e MS |
66 | return 0; |
67 | ||
68 | list_size = vfs_listxattr(old, NULL, 0); | |
69 | if (list_size <= 0) { | |
70 | if (list_size == -EOPNOTSUPP) | |
71 | return 0; | |
72 | return list_size; | |
73 | } | |
74 | ||
75 | buf = kzalloc(list_size, GFP_KERNEL); | |
76 | if (!buf) | |
77 | return -ENOMEM; | |
78 | ||
e9be9d5e MS |
79 | list_size = vfs_listxattr(old, buf, list_size); |
80 | if (list_size <= 0) { | |
81 | error = list_size; | |
e4ad29fa | 82 | goto out; |
e9be9d5e MS |
83 | } |
84 | ||
8b326c61 MS |
85 | for (name = buf; list_size; name += slen) { |
86 | slen = strnlen(name, list_size) + 1; | |
87 | ||
88 | /* underlying fs providing us with an broken xattr list? */ | |
89 | if (WARN_ON(slen > list_size)) { | |
90 | error = -EIO; | |
91 | break; | |
92 | } | |
93 | list_size -= slen; | |
94 | ||
0956254a MS |
95 | if (ovl_is_private_xattr(name)) |
96 | continue; | |
e4ad29fa VC |
97 | retry: |
98 | size = vfs_getxattr(old, name, value, value_size); | |
99 | if (size == -ERANGE) | |
100 | size = vfs_getxattr(old, name, NULL, 0); | |
101 | ||
97daf8b9 | 102 | if (size < 0) { |
e9be9d5e | 103 | error = size; |
e4ad29fa | 104 | break; |
e9be9d5e | 105 | } |
e4ad29fa VC |
106 | |
107 | if (size > value_size) { | |
108 | void *new; | |
109 | ||
110 | new = krealloc(value, size, GFP_KERNEL); | |
111 | if (!new) { | |
112 | error = -ENOMEM; | |
113 | break; | |
114 | } | |
115 | value = new; | |
116 | value_size = size; | |
117 | goto retry; | |
118 | } | |
119 | ||
121ab822 VG |
120 | error = security_inode_copy_up_xattr(name); |
121 | if (error < 0 && error != -EOPNOTSUPP) | |
122 | break; | |
123 | if (error == 1) { | |
124 | error = 0; | |
125 | continue; /* Discard */ | |
126 | } | |
e9be9d5e MS |
127 | error = vfs_setxattr(new, name, value, size, 0); |
128 | if (error) | |
e4ad29fa | 129 | break; |
e9be9d5e | 130 | } |
e9be9d5e MS |
131 | kfree(value); |
132 | out: | |
133 | kfree(buf); | |
134 | return error; | |
135 | } | |
136 | ||
137 | static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len) | |
138 | { | |
139 | struct file *old_file; | |
140 | struct file *new_file; | |
141 | loff_t old_pos = 0; | |
142 | loff_t new_pos = 0; | |
143 | int error = 0; | |
144 | ||
145 | if (len == 0) | |
146 | return 0; | |
147 | ||
0480334f | 148 | old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); |
e9be9d5e MS |
149 | if (IS_ERR(old_file)) |
150 | return PTR_ERR(old_file); | |
151 | ||
0480334f | 152 | new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); |
e9be9d5e MS |
153 | if (IS_ERR(new_file)) { |
154 | error = PTR_ERR(new_file); | |
155 | goto out_fput; | |
156 | } | |
157 | ||
2ea98466 AG |
158 | /* Try to use clone_file_range to clone up within the same fs */ |
159 | error = vfs_clone_file_range(old_file, 0, new_file, 0, len); | |
160 | if (!error) | |
161 | goto out; | |
162 | /* Couldn't clone, so now we try to copy the data */ | |
163 | error = 0; | |
164 | ||
e9be9d5e MS |
165 | /* FIXME: copy up sparse files efficiently */ |
166 | while (len) { | |
167 | size_t this_len = OVL_COPY_UP_CHUNK_SIZE; | |
168 | long bytes; | |
169 | ||
170 | if (len < this_len) | |
171 | this_len = len; | |
172 | ||
173 | if (signal_pending_state(TASK_KILLABLE, current)) { | |
174 | error = -EINTR; | |
175 | break; | |
176 | } | |
177 | ||
178 | bytes = do_splice_direct(old_file, &old_pos, | |
179 | new_file, &new_pos, | |
180 | this_len, SPLICE_F_MOVE); | |
181 | if (bytes <= 0) { | |
182 | error = bytes; | |
183 | break; | |
184 | } | |
185 | WARN_ON(old_pos != new_pos); | |
186 | ||
187 | len -= bytes; | |
188 | } | |
2ea98466 | 189 | out: |
641089c1 MS |
190 | if (!error) |
191 | error = vfs_fsync(new_file, 0); | |
e9be9d5e MS |
192 | fput(new_file); |
193 | out_fput: | |
194 | fput(old_file); | |
195 | return error; | |
196 | } | |
197 | ||
e9be9d5e MS |
198 | static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) |
199 | { | |
200 | struct iattr attr = { | |
201 | .ia_valid = | |
202 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, | |
203 | .ia_atime = stat->atime, | |
204 | .ia_mtime = stat->mtime, | |
205 | }; | |
206 | ||
207 | return notify_change(upperdentry, &attr, NULL); | |
208 | } | |
209 | ||
210 | int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) | |
211 | { | |
212 | int err = 0; | |
213 | ||
214 | if (!S_ISLNK(stat->mode)) { | |
215 | struct iattr attr = { | |
216 | .ia_valid = ATTR_MODE, | |
217 | .ia_mode = stat->mode, | |
218 | }; | |
219 | err = notify_change(upperdentry, &attr, NULL); | |
220 | } | |
221 | if (!err) { | |
222 | struct iattr attr = { | |
223 | .ia_valid = ATTR_UID | ATTR_GID, | |
224 | .ia_uid = stat->uid, | |
225 | .ia_gid = stat->gid, | |
226 | }; | |
227 | err = notify_change(upperdentry, &attr, NULL); | |
228 | } | |
229 | if (!err) | |
230 | ovl_set_timestamps(upperdentry, stat); | |
231 | ||
232 | return err; | |
e9be9d5e MS |
233 | } |
234 | ||
5b2cccd3 | 235 | struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper) |
3a1e819b AG |
236 | { |
237 | struct ovl_fh *fh; | |
238 | int fh_type, fh_len, dwords; | |
239 | void *buf; | |
240 | int buflen = MAX_HANDLE_SZ; | |
05122443 | 241 | uuid_t *uuid = &real->d_sb->s_uuid; |
3a1e819b | 242 | |
0ee931c4 | 243 | buf = kmalloc(buflen, GFP_KERNEL); |
3a1e819b AG |
244 | if (!buf) |
245 | return ERR_PTR(-ENOMEM); | |
246 | ||
247 | /* | |
248 | * We encode a non-connectable file handle for non-dir, because we | |
249 | * only need to find the lower inode number and we don't want to pay | |
250 | * the price or reconnecting the dentry. | |
251 | */ | |
252 | dwords = buflen >> 2; | |
05122443 | 253 | fh_type = exportfs_encode_fh(real, buf, &dwords, 0); |
3a1e819b AG |
254 | buflen = (dwords << 2); |
255 | ||
256 | fh = ERR_PTR(-EIO); | |
257 | if (WARN_ON(fh_type < 0) || | |
258 | WARN_ON(buflen > MAX_HANDLE_SZ) || | |
259 | WARN_ON(fh_type == FILEID_INVALID)) | |
260 | goto out; | |
261 | ||
262 | BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255); | |
263 | fh_len = offsetof(struct ovl_fh, fid) + buflen; | |
264 | fh = kmalloc(fh_len, GFP_KERNEL); | |
265 | if (!fh) { | |
266 | fh = ERR_PTR(-ENOMEM); | |
267 | goto out; | |
268 | } | |
269 | ||
270 | fh->version = OVL_FH_VERSION; | |
271 | fh->magic = OVL_FH_MAGIC; | |
272 | fh->type = fh_type; | |
273 | fh->flags = OVL_FH_FLAG_CPU_ENDIAN; | |
54fb347e AG |
274 | /* |
275 | * When we will want to decode an overlay dentry from this handle | |
276 | * and all layers are on the same fs, if we get a disconncted real | |
277 | * dentry when we decode fid, the only way to tell if we should assign | |
278 | * it to upperdentry or to lowerstack is by checking this flag. | |
279 | */ | |
280 | if (is_upper) | |
281 | fh->flags |= OVL_FH_FLAG_PATH_UPPER; | |
3a1e819b AG |
282 | fh->len = fh_len; |
283 | fh->uuid = *uuid; | |
284 | memcpy(fh->fid, buf, buflen); | |
285 | ||
286 | out: | |
287 | kfree(buf); | |
288 | return fh; | |
289 | } | |
290 | ||
9678e630 AG |
291 | int ovl_set_origin(struct dentry *dentry, struct dentry *lower, |
292 | struct dentry *upper) | |
3a1e819b | 293 | { |
3a1e819b AG |
294 | const struct ovl_fh *fh = NULL; |
295 | int err; | |
296 | ||
297 | /* | |
298 | * When lower layer doesn't support export operations store a 'null' fh, | |
299 | * so we can use the overlay.origin xattr to distignuish between a copy | |
300 | * up and a pure upper inode. | |
301 | */ | |
02bcd157 | 302 | if (ovl_can_decode_fh(lower->d_sb)) { |
5b2cccd3 | 303 | fh = ovl_encode_real_fh(lower, false); |
3a1e819b AG |
304 | if (IS_ERR(fh)) |
305 | return PTR_ERR(fh); | |
306 | } | |
307 | ||
6266d465 MS |
308 | /* |
309 | * Do not fail when upper doesn't support xattrs. | |
310 | */ | |
311 | err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh, | |
312 | fh ? fh->len : 0, 0); | |
3a1e819b AG |
313 | kfree(fh); |
314 | ||
315 | return err; | |
316 | } | |
317 | ||
016b720f AG |
318 | /* Store file handle of @upper dir in @index dir entry */ |
319 | static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index) | |
320 | { | |
321 | const struct ovl_fh *fh; | |
322 | int err; | |
323 | ||
5b2cccd3 | 324 | fh = ovl_encode_real_fh(upper, true); |
016b720f AG |
325 | if (IS_ERR(fh)) |
326 | return PTR_ERR(fh); | |
327 | ||
328 | err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0); | |
329 | ||
330 | kfree(fh); | |
331 | return err; | |
332 | } | |
333 | ||
334 | /* | |
335 | * Create and install index entry. | |
336 | * | |
337 | * Caller must hold i_mutex on indexdir. | |
338 | */ | |
339 | static int ovl_create_index(struct dentry *dentry, struct dentry *origin, | |
340 | struct dentry *upper) | |
341 | { | |
342 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); | |
343 | struct inode *dir = d_inode(indexdir); | |
344 | struct dentry *index = NULL; | |
345 | struct dentry *temp = NULL; | |
346 | struct qstr name = { }; | |
347 | int err; | |
348 | ||
349 | /* | |
350 | * For now this is only used for creating index entry for directories, | |
351 | * because non-dir are copied up directly to index and then hardlinked | |
352 | * to upper dir. | |
353 | * | |
354 | * TODO: implement create index for non-dir, so we can call it when | |
355 | * encoding file handle for non-dir in case index does not exist. | |
356 | */ | |
357 | if (WARN_ON(!d_is_dir(dentry))) | |
358 | return -EIO; | |
359 | ||
360 | /* Directory not expected to be indexed before copy up */ | |
361 | if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry)))) | |
362 | return -EIO; | |
363 | ||
364 | err = ovl_get_index_name(origin, &name); | |
365 | if (err) | |
366 | return err; | |
367 | ||
137ec526 | 368 | temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0)); |
b148cba4 | 369 | err = PTR_ERR(temp); |
016b720f | 370 | if (IS_ERR(temp)) |
b148cba4 | 371 | goto free_name; |
016b720f | 372 | |
016b720f AG |
373 | err = ovl_set_upper_fh(upper, temp); |
374 | if (err) | |
b148cba4 | 375 | goto out; |
016b720f AG |
376 | |
377 | index = lookup_one_len(name.name, indexdir, name.len); | |
378 | if (IS_ERR(index)) { | |
379 | err = PTR_ERR(index); | |
380 | } else { | |
381 | err = ovl_do_rename(dir, temp, dir, index, 0); | |
382 | dput(index); | |
383 | } | |
016b720f | 384 | out: |
b148cba4 MS |
385 | if (err) |
386 | ovl_cleanup(dir, temp); | |
016b720f | 387 | dput(temp); |
b148cba4 | 388 | free_name: |
016b720f AG |
389 | kfree(name.name); |
390 | return err; | |
016b720f AG |
391 | } |
392 | ||
f4439de1 AG |
393 | struct ovl_copy_up_ctx { |
394 | struct dentry *parent; | |
395 | struct dentry *dentry; | |
396 | struct path lowerpath; | |
397 | struct kstat stat; | |
398 | struct kstat pstat; | |
399 | const char *link; | |
400 | struct dentry *destdir; | |
401 | struct qstr destname; | |
402 | struct dentry *workdir; | |
403 | bool tmpfile; | |
404 | bool origin; | |
016b720f | 405 | bool indexed; |
f4439de1 AG |
406 | }; |
407 | ||
408 | static int ovl_link_up(struct ovl_copy_up_ctx *c) | |
59be0971 AG |
409 | { |
410 | int err; | |
411 | struct dentry *upper; | |
f4439de1 | 412 | struct dentry *upperdir = ovl_dentry_upper(c->parent); |
59be0971 AG |
413 | struct inode *udir = d_inode(upperdir); |
414 | ||
f4439de1 AG |
415 | /* Mark parent "impure" because it may now contain non-pure upper */ |
416 | err = ovl_set_impure(c->parent, upperdir); | |
417 | if (err) | |
418 | return err; | |
419 | ||
420 | err = ovl_set_nlink_lower(c->dentry); | |
5f8415d6 AG |
421 | if (err) |
422 | return err; | |
423 | ||
59be0971 | 424 | inode_lock_nested(udir, I_MUTEX_PARENT); |
f4439de1 AG |
425 | upper = lookup_one_len(c->dentry->d_name.name, upperdir, |
426 | c->dentry->d_name.len); | |
59be0971 AG |
427 | err = PTR_ERR(upper); |
428 | if (!IS_ERR(upper)) { | |
6cf00764 | 429 | err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper); |
59be0971 AG |
430 | dput(upper); |
431 | ||
f4439de1 AG |
432 | if (!err) { |
433 | /* Restore timestamps on parent (best effort) */ | |
434 | ovl_set_timestamps(upperdir, &c->pstat); | |
435 | ovl_dentry_set_upper_alias(c->dentry); | |
436 | } | |
59be0971 AG |
437 | } |
438 | inode_unlock(udir); | |
aa3ff3c1 AG |
439 | if (err) |
440 | return err; | |
441 | ||
442 | err = ovl_set_nlink_upper(c->dentry); | |
59be0971 AG |
443 | |
444 | return err; | |
445 | } | |
446 | ||
23f0ab13 MS |
447 | static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp, |
448 | struct dentry **newdentry) | |
15932c41 AG |
449 | { |
450 | int err; | |
451 | struct dentry *upper; | |
59be0971 | 452 | struct inode *udir = d_inode(c->destdir); |
15932c41 | 453 | |
59be0971 | 454 | upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); |
15932c41 AG |
455 | if (IS_ERR(upper)) |
456 | return PTR_ERR(upper); | |
457 | ||
23f0ab13 | 458 | if (c->tmpfile) |
6cf00764 | 459 | err = ovl_do_link(temp, udir, upper); |
15932c41 | 460 | else |
23f0ab13 | 461 | err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0); |
15932c41 | 462 | |
59be0971 | 463 | if (!err) |
23f0ab13 | 464 | *newdentry = dget(c->tmpfile ? upper : temp); |
15932c41 AG |
465 | dput(upper); |
466 | ||
467 | return err; | |
468 | } | |
469 | ||
b148cba4 | 470 | static struct dentry *ovl_get_tmpfile(struct ovl_copy_up_ctx *c) |
e9be9d5e | 471 | { |
e9be9d5e | 472 | int err; |
7d90b853 | 473 | struct dentry *temp; |
d8ad8b49 VG |
474 | const struct cred *old_creds = NULL; |
475 | struct cred *new_creds = NULL; | |
471ec5dc | 476 | struct ovl_cattr cattr = { |
32a3d848 | 477 | /* Can't properly set mode on creation because of the umask */ |
23f0ab13 MS |
478 | .mode = c->stat.mode & S_IFMT, |
479 | .rdev = c->stat.rdev, | |
480 | .link = c->link | |
32a3d848 | 481 | }; |
e9be9d5e | 482 | |
23f0ab13 | 483 | err = security_inode_copy_up(c->dentry, &new_creds); |
b148cba4 | 484 | temp = ERR_PTR(err); |
d8ad8b49 | 485 | if (err < 0) |
e85f82ff | 486 | goto out; |
d8ad8b49 VG |
487 | |
488 | if (new_creds) | |
489 | old_creds = override_creds(new_creds); | |
490 | ||
137ec526 | 491 | if (c->tmpfile) |
23f0ab13 | 492 | temp = ovl_do_tmpfile(c->workdir, c->stat.mode); |
137ec526 AG |
493 | else |
494 | temp = ovl_create_temp(c->workdir, &cattr); | |
7d90b853 | 495 | out: |
d8ad8b49 VG |
496 | if (new_creds) { |
497 | revert_creds(old_creds); | |
498 | put_cred(new_creds); | |
499 | } | |
500 | ||
b148cba4 | 501 | return temp; |
7d90b853 MS |
502 | } |
503 | ||
23f0ab13 | 504 | static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp) |
7d90b853 | 505 | { |
7d90b853 MS |
506 | int err; |
507 | ||
23f0ab13 | 508 | if (S_ISREG(c->stat.mode)) { |
e9be9d5e | 509 | struct path upperpath; |
f134f244 | 510 | |
23f0ab13 | 511 | ovl_path_upper(c->dentry, &upperpath); |
e9be9d5e | 512 | BUG_ON(upperpath.dentry != NULL); |
42f269b9 | 513 | upperpath.dentry = temp; |
e9be9d5e | 514 | |
23f0ab13 | 515 | err = ovl_copy_up_data(&c->lowerpath, &upperpath, c->stat.size); |
e9be9d5e | 516 | if (err) |
02209d10 | 517 | return err; |
e9be9d5e MS |
518 | } |
519 | ||
23f0ab13 | 520 | err = ovl_copy_xattr(c->lowerpath.dentry, temp); |
e9be9d5e | 521 | if (err) |
02209d10 | 522 | return err; |
e9be9d5e | 523 | |
42f269b9 | 524 | inode_lock(temp->d_inode); |
23f0ab13 | 525 | err = ovl_set_attr(temp, &c->stat); |
42f269b9 | 526 | inode_unlock(temp->d_inode); |
e9be9d5e | 527 | if (err) |
02209d10 | 528 | return err; |
e9be9d5e | 529 | |
3a1e819b AG |
530 | /* |
531 | * Store identifier of lower inode in upper inode xattr to | |
532 | * allow lookup of the copy up origin inode. | |
fbaf94ee MS |
533 | * |
534 | * Don't set origin when we are breaking the association with a lower | |
535 | * hard link. | |
3a1e819b | 536 | */ |
59be0971 | 537 | if (c->origin) { |
23f0ab13 | 538 | err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp); |
fbaf94ee | 539 | if (err) |
02209d10 | 540 | return err; |
fbaf94ee | 541 | } |
3a1e819b | 542 | |
02209d10 AG |
543 | return 0; |
544 | } | |
545 | ||
23f0ab13 | 546 | static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c) |
02209d10 | 547 | { |
59be0971 | 548 | struct inode *udir = c->destdir->d_inode; |
b79e05aa | 549 | struct inode *inode; |
02209d10 | 550 | struct dentry *newdentry = NULL; |
b148cba4 | 551 | struct dentry *temp; |
02209d10 AG |
552 | int err; |
553 | ||
b148cba4 MS |
554 | temp = ovl_get_tmpfile(c); |
555 | if (IS_ERR(temp)) | |
556 | return PTR_ERR(temp); | |
02209d10 | 557 | |
23f0ab13 | 558 | err = ovl_copy_up_inode(c, temp); |
02209d10 | 559 | if (err) |
b148cba4 | 560 | goto out; |
02209d10 | 561 | |
016b720f AG |
562 | if (S_ISDIR(c->stat.mode) && c->indexed) { |
563 | err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp); | |
564 | if (err) | |
b148cba4 | 565 | goto out; |
016b720f AG |
566 | } |
567 | ||
23f0ab13 | 568 | if (c->tmpfile) { |
15932c41 | 569 | inode_lock_nested(udir, I_MUTEX_PARENT); |
23f0ab13 | 570 | err = ovl_install_temp(c, temp, &newdentry); |
15932c41 AG |
571 | inode_unlock(udir); |
572 | } else { | |
23f0ab13 | 573 | err = ovl_install_temp(c, temp, &newdentry); |
e85f82ff | 574 | } |
e9be9d5e | 575 | if (err) |
b148cba4 | 576 | goto out; |
e9be9d5e | 577 | |
b79e05aa AG |
578 | inode = d_inode(c->dentry); |
579 | ovl_inode_update(inode, newdentry); | |
580 | if (S_ISDIR(inode->i_mode)) | |
581 | ovl_set_flag(OVL_WHITEOUTS, inode); | |
582 | ||
e85f82ff | 583 | out: |
b148cba4 MS |
584 | if (err && !c->tmpfile) |
585 | ovl_cleanup(d_inode(c->workdir), temp); | |
42f269b9 | 586 | dput(temp); |
e9be9d5e MS |
587 | return err; |
588 | ||
e9be9d5e MS |
589 | } |
590 | ||
591 | /* | |
592 | * Copy up a single dentry | |
593 | * | |
a6c60655 MS |
594 | * All renames start with copy up of source if necessary. The actual |
595 | * rename will only proceed once the copy up was successful. Copy up uses | |
596 | * upper parent i_mutex for exclusion. Since rename can change d_parent it | |
597 | * is possible that the copy up will lock the old parent. At that point | |
598 | * the file will have already been copied up anyway. | |
e9be9d5e | 599 | */ |
a6fb235a | 600 | static int ovl_do_copy_up(struct ovl_copy_up_ctx *c) |
e9be9d5e | 601 | { |
e9be9d5e | 602 | int err; |
23f0ab13 | 603 | struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info; |
016b720f | 604 | bool to_index = false; |
59be0971 | 605 | |
016b720f AG |
606 | /* |
607 | * Indexed non-dir is copied up directly to the index entry and then | |
608 | * hardlinked to upper dir. Indexed dir is copied up to indexdir, | |
609 | * then index entry is created and then copied up dir installed. | |
610 | * Copying dir up to indexdir instead of workdir simplifies locking. | |
611 | */ | |
612 | if (ovl_need_index(c->dentry)) { | |
613 | c->indexed = true; | |
614 | if (S_ISDIR(c->stat.mode)) | |
615 | c->workdir = ovl_indexdir(c->dentry->d_sb); | |
616 | else | |
617 | to_index = true; | |
618 | } | |
619 | ||
620 | if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index) | |
59be0971 AG |
621 | c->origin = true; |
622 | ||
016b720f | 623 | if (to_index) { |
59be0971 AG |
624 | c->destdir = ovl_indexdir(c->dentry->d_sb); |
625 | err = ovl_get_index_name(c->lowerpath.dentry, &c->destname); | |
626 | if (err) | |
627 | return err; | |
aa3ff3c1 AG |
628 | } else if (WARN_ON(!c->parent)) { |
629 | /* Disconnected dentry must be copied up to index dir */ | |
630 | return -EIO; | |
59be0971 AG |
631 | } else { |
632 | /* | |
633 | * Mark parent "impure" because it may now contain non-pure | |
634 | * upper | |
635 | */ | |
636 | err = ovl_set_impure(c->parent, c->destdir); | |
637 | if (err) | |
638 | return err; | |
639 | } | |
e9be9d5e | 640 | |
01ad3eb8 | 641 | /* Should we copyup with O_TMPFILE or with workdir? */ |
23f0ab13 | 642 | if (S_ISREG(c->stat.mode) && ofs->tmpfile) { |
23f0ab13 | 643 | c->tmpfile = true; |
59be0971 AG |
644 | err = ovl_copy_up_locked(c); |
645 | } else { | |
5820dc08 AG |
646 | err = ovl_lock_rename_workdir(c->workdir, c->destdir); |
647 | if (!err) { | |
59be0971 AG |
648 | err = ovl_copy_up_locked(c); |
649 | unlock_rename(c->workdir, c->destdir); | |
650 | } | |
01ad3eb8 AG |
651 | } |
652 | ||
aa3ff3c1 AG |
653 | |
654 | if (err) | |
655 | goto out; | |
656 | ||
657 | if (c->indexed) | |
016b720f AG |
658 | ovl_set_flag(OVL_INDEX, d_inode(c->dentry)); |
659 | ||
660 | if (to_index) { | |
aa3ff3c1 AG |
661 | /* Initialize nlink for copy up of disconnected dentry */ |
662 | err = ovl_set_nlink_upper(c->dentry); | |
663 | } else { | |
59be0971 AG |
664 | struct inode *udir = d_inode(c->destdir); |
665 | ||
666 | /* Restore timestamps on parent (best effort) */ | |
667 | inode_lock(udir); | |
668 | ovl_set_timestamps(c->destdir, &c->pstat); | |
669 | inode_unlock(udir); | |
670 | ||
671 | ovl_dentry_set_upper_alias(c->dentry); | |
e9be9d5e MS |
672 | } |
673 | ||
aa3ff3c1 AG |
674 | out: |
675 | if (to_index) | |
676 | kfree(c->destname.name); | |
a6fb235a MS |
677 | return err; |
678 | } | |
679 | ||
680 | static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, | |
681 | int flags) | |
682 | { | |
683 | int err; | |
684 | DEFINE_DELAYED_CALL(done); | |
685 | struct path parentpath; | |
686 | struct ovl_copy_up_ctx ctx = { | |
687 | .parent = parent, | |
688 | .dentry = dentry, | |
689 | .workdir = ovl_workdir(dentry), | |
690 | }; | |
691 | ||
692 | if (WARN_ON(!ctx.workdir)) | |
693 | return -EROFS; | |
694 | ||
695 | ovl_path_lower(dentry, &ctx.lowerpath); | |
696 | err = vfs_getattr(&ctx.lowerpath, &ctx.stat, | |
697 | STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); | |
698 | if (err) | |
699 | return err; | |
700 | ||
aa3ff3c1 AG |
701 | if (parent) { |
702 | ovl_path_upper(parent, &parentpath); | |
703 | ctx.destdir = parentpath.dentry; | |
704 | ctx.destname = dentry->d_name; | |
a6fb235a | 705 | |
aa3ff3c1 AG |
706 | err = vfs_getattr(&parentpath, &ctx.pstat, |
707 | STATX_ATIME | STATX_MTIME, | |
708 | AT_STATX_SYNC_AS_STAT); | |
709 | if (err) | |
710 | return err; | |
711 | } | |
a6fb235a MS |
712 | |
713 | /* maybe truncate regular file. this has no effect on dirs */ | |
714 | if (flags & O_TRUNC) | |
715 | ctx.stat.size = 0; | |
716 | ||
717 | if (S_ISLNK(ctx.stat.mode)) { | |
718 | ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done); | |
719 | if (IS_ERR(ctx.link)) | |
720 | return PTR_ERR(ctx.link); | |
721 | } | |
722 | ovl_do_check_copy_up(ctx.lowerpath.dentry); | |
723 | ||
fd210b7d MS |
724 | err = ovl_copy_up_start(dentry); |
725 | /* err < 0: interrupted, err > 0: raced with another copy-up */ | |
726 | if (unlikely(err)) { | |
727 | if (err > 0) | |
728 | err = 0; | |
729 | } else { | |
59be0971 AG |
730 | if (!ovl_dentry_upper(dentry)) |
731 | err = ovl_do_copy_up(&ctx); | |
aa3ff3c1 | 732 | if (!err && parent && !ovl_dentry_has_upper_alias(dentry)) |
f4439de1 | 733 | err = ovl_link_up(&ctx); |
fd210b7d MS |
734 | ovl_copy_up_end(dentry); |
735 | } | |
7764235b | 736 | do_delayed_call(&done); |
e9be9d5e MS |
737 | |
738 | return err; | |
739 | } | |
740 | ||
9aba6521 | 741 | int ovl_copy_up_flags(struct dentry *dentry, int flags) |
e9be9d5e | 742 | { |
8eac98b8 VG |
743 | int err = 0; |
744 | const struct cred *old_cred = ovl_override_creds(dentry->d_sb); | |
aa3ff3c1 AG |
745 | bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED); |
746 | ||
747 | /* | |
748 | * With NFS export, copy up can get called for a disconnected non-dir. | |
749 | * In this case, we will copy up lower inode to index dir without | |
750 | * linking it to upper dir. | |
751 | */ | |
752 | if (WARN_ON(disconnected && d_is_dir(dentry))) | |
753 | return -EIO; | |
e9be9d5e | 754 | |
e9be9d5e MS |
755 | while (!err) { |
756 | struct dentry *next; | |
aa3ff3c1 | 757 | struct dentry *parent = NULL; |
e9be9d5e | 758 | |
59be0971 AG |
759 | /* |
760 | * Check if copy-up has happened as well as for upper alias (in | |
761 | * case of hard links) is there. | |
762 | * | |
763 | * Both checks are lockless: | |
764 | * - false negatives: will recheck under oi->lock | |
765 | * - false positives: | |
766 | * + ovl_dentry_upper() uses memory barriers to ensure the | |
767 | * upper dentry is up-to-date | |
768 | * + ovl_dentry_has_upper_alias() relies on locking of | |
769 | * upper parent i_rwsem to prevent reordering copy-up | |
770 | * with rename. | |
771 | */ | |
772 | if (ovl_dentry_upper(dentry) && | |
aa3ff3c1 | 773 | (ovl_dentry_has_upper_alias(dentry) || disconnected)) |
e9be9d5e MS |
774 | break; |
775 | ||
776 | next = dget(dentry); | |
777 | /* find the topmost dentry not yet copied up */ | |
aa3ff3c1 | 778 | for (; !disconnected;) { |
e9be9d5e MS |
779 | parent = dget_parent(next); |
780 | ||
59be0971 | 781 | if (ovl_dentry_upper(parent)) |
e9be9d5e MS |
782 | break; |
783 | ||
784 | dput(next); | |
785 | next = parent; | |
786 | } | |
787 | ||
a6fb235a | 788 | err = ovl_copy_up_one(parent, next, flags); |
e9be9d5e MS |
789 | |
790 | dput(parent); | |
791 | dput(next); | |
792 | } | |
8eac98b8 | 793 | revert_creds(old_cred); |
e9be9d5e MS |
794 | |
795 | return err; | |
796 | } | |
9aba6521 AG |
797 | |
798 | int ovl_copy_up(struct dentry *dentry) | |
799 | { | |
800 | return ovl_copy_up_flags(dentry, 0); | |
801 | } |