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