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 | 143 | /* Try to use clone_file_range to clone up within the same fs */ |
a725356b | 144 | error = do_clone_file_range(old_file, 0, new_file, 0, len); |
2ea98466 AG |
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 | ||
0c288874 VG |
183 | static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat) |
184 | { | |
185 | struct iattr attr = { | |
186 | .ia_valid = ATTR_SIZE, | |
187 | .ia_size = stat->size, | |
188 | }; | |
189 | ||
190 | return notify_change(upperdentry, &attr, NULL); | |
191 | } | |
192 | ||
e9be9d5e MS |
193 | static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) |
194 | { | |
195 | struct iattr attr = { | |
196 | .ia_valid = | |
197 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, | |
198 | .ia_atime = stat->atime, | |
199 | .ia_mtime = stat->mtime, | |
200 | }; | |
201 | ||
202 | return notify_change(upperdentry, &attr, NULL); | |
203 | } | |
204 | ||
205 | int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) | |
206 | { | |
207 | int err = 0; | |
208 | ||
209 | if (!S_ISLNK(stat->mode)) { | |
210 | struct iattr attr = { | |
211 | .ia_valid = ATTR_MODE, | |
212 | .ia_mode = stat->mode, | |
213 | }; | |
214 | err = notify_change(upperdentry, &attr, NULL); | |
215 | } | |
216 | if (!err) { | |
217 | struct iattr attr = { | |
218 | .ia_valid = ATTR_UID | ATTR_GID, | |
219 | .ia_uid = stat->uid, | |
220 | .ia_gid = stat->gid, | |
221 | }; | |
222 | err = notify_change(upperdentry, &attr, NULL); | |
223 | } | |
224 | if (!err) | |
225 | ovl_set_timestamps(upperdentry, stat); | |
226 | ||
227 | return err; | |
e9be9d5e MS |
228 | } |
229 | ||
5b2cccd3 | 230 | struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper) |
3a1e819b AG |
231 | { |
232 | struct ovl_fh *fh; | |
233 | int fh_type, fh_len, dwords; | |
234 | void *buf; | |
235 | int buflen = MAX_HANDLE_SZ; | |
05122443 | 236 | uuid_t *uuid = &real->d_sb->s_uuid; |
3a1e819b | 237 | |
0ee931c4 | 238 | buf = kmalloc(buflen, GFP_KERNEL); |
3a1e819b AG |
239 | if (!buf) |
240 | return ERR_PTR(-ENOMEM); | |
241 | ||
242 | /* | |
243 | * We encode a non-connectable file handle for non-dir, because we | |
244 | * only need to find the lower inode number and we don't want to pay | |
245 | * the price or reconnecting the dentry. | |
246 | */ | |
247 | dwords = buflen >> 2; | |
05122443 | 248 | fh_type = exportfs_encode_fh(real, buf, &dwords, 0); |
3a1e819b AG |
249 | buflen = (dwords << 2); |
250 | ||
251 | fh = ERR_PTR(-EIO); | |
252 | if (WARN_ON(fh_type < 0) || | |
253 | WARN_ON(buflen > MAX_HANDLE_SZ) || | |
254 | WARN_ON(fh_type == FILEID_INVALID)) | |
255 | goto out; | |
256 | ||
257 | BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255); | |
258 | fh_len = offsetof(struct ovl_fh, fid) + buflen; | |
259 | fh = kmalloc(fh_len, GFP_KERNEL); | |
260 | if (!fh) { | |
261 | fh = ERR_PTR(-ENOMEM); | |
262 | goto out; | |
263 | } | |
264 | ||
265 | fh->version = OVL_FH_VERSION; | |
266 | fh->magic = OVL_FH_MAGIC; | |
267 | fh->type = fh_type; | |
268 | fh->flags = OVL_FH_FLAG_CPU_ENDIAN; | |
54fb347e AG |
269 | /* |
270 | * When we will want to decode an overlay dentry from this handle | |
271 | * and all layers are on the same fs, if we get a disconncted real | |
272 | * dentry when we decode fid, the only way to tell if we should assign | |
273 | * it to upperdentry or to lowerstack is by checking this flag. | |
274 | */ | |
275 | if (is_upper) | |
276 | fh->flags |= OVL_FH_FLAG_PATH_UPPER; | |
3a1e819b AG |
277 | fh->len = fh_len; |
278 | fh->uuid = *uuid; | |
279 | memcpy(fh->fid, buf, buflen); | |
280 | ||
281 | out: | |
282 | kfree(buf); | |
283 | return fh; | |
284 | } | |
285 | ||
9678e630 AG |
286 | int ovl_set_origin(struct dentry *dentry, struct dentry *lower, |
287 | struct dentry *upper) | |
3a1e819b | 288 | { |
3a1e819b AG |
289 | const struct ovl_fh *fh = NULL; |
290 | int err; | |
291 | ||
292 | /* | |
293 | * When lower layer doesn't support export operations store a 'null' fh, | |
294 | * so we can use the overlay.origin xattr to distignuish between a copy | |
295 | * up and a pure upper inode. | |
296 | */ | |
02bcd157 | 297 | if (ovl_can_decode_fh(lower->d_sb)) { |
5b2cccd3 | 298 | fh = ovl_encode_real_fh(lower, false); |
3a1e819b AG |
299 | if (IS_ERR(fh)) |
300 | return PTR_ERR(fh); | |
301 | } | |
302 | ||
6266d465 MS |
303 | /* |
304 | * Do not fail when upper doesn't support xattrs. | |
305 | */ | |
306 | err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh, | |
307 | fh ? fh->len : 0, 0); | |
3a1e819b AG |
308 | kfree(fh); |
309 | ||
310 | return err; | |
311 | } | |
312 | ||
016b720f AG |
313 | /* Store file handle of @upper dir in @index dir entry */ |
314 | static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index) | |
315 | { | |
316 | const struct ovl_fh *fh; | |
317 | int err; | |
318 | ||
5b2cccd3 | 319 | fh = ovl_encode_real_fh(upper, true); |
016b720f AG |
320 | if (IS_ERR(fh)) |
321 | return PTR_ERR(fh); | |
322 | ||
323 | err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0); | |
324 | ||
325 | kfree(fh); | |
326 | return err; | |
327 | } | |
328 | ||
329 | /* | |
330 | * Create and install index entry. | |
331 | * | |
332 | * Caller must hold i_mutex on indexdir. | |
333 | */ | |
334 | static int ovl_create_index(struct dentry *dentry, struct dentry *origin, | |
335 | struct dentry *upper) | |
336 | { | |
337 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); | |
338 | struct inode *dir = d_inode(indexdir); | |
339 | struct dentry *index = NULL; | |
340 | struct dentry *temp = NULL; | |
341 | struct qstr name = { }; | |
342 | int err; | |
343 | ||
344 | /* | |
345 | * For now this is only used for creating index entry for directories, | |
346 | * because non-dir are copied up directly to index and then hardlinked | |
347 | * to upper dir. | |
348 | * | |
349 | * TODO: implement create index for non-dir, so we can call it when | |
350 | * encoding file handle for non-dir in case index does not exist. | |
351 | */ | |
352 | if (WARN_ON(!d_is_dir(dentry))) | |
353 | return -EIO; | |
354 | ||
355 | /* Directory not expected to be indexed before copy up */ | |
356 | if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry)))) | |
357 | return -EIO; | |
358 | ||
359 | err = ovl_get_index_name(origin, &name); | |
360 | if (err) | |
361 | return err; | |
362 | ||
137ec526 | 363 | temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0)); |
b148cba4 | 364 | err = PTR_ERR(temp); |
016b720f | 365 | if (IS_ERR(temp)) |
b148cba4 | 366 | goto free_name; |
016b720f | 367 | |
016b720f AG |
368 | err = ovl_set_upper_fh(upper, temp); |
369 | if (err) | |
b148cba4 | 370 | goto out; |
016b720f AG |
371 | |
372 | index = lookup_one_len(name.name, indexdir, name.len); | |
373 | if (IS_ERR(index)) { | |
374 | err = PTR_ERR(index); | |
375 | } else { | |
376 | err = ovl_do_rename(dir, temp, dir, index, 0); | |
377 | dput(index); | |
378 | } | |
016b720f | 379 | out: |
b148cba4 MS |
380 | if (err) |
381 | ovl_cleanup(dir, temp); | |
016b720f | 382 | dput(temp); |
b148cba4 | 383 | free_name: |
016b720f AG |
384 | kfree(name.name); |
385 | return err; | |
016b720f AG |
386 | } |
387 | ||
f4439de1 AG |
388 | struct ovl_copy_up_ctx { |
389 | struct dentry *parent; | |
390 | struct dentry *dentry; | |
391 | struct path lowerpath; | |
392 | struct kstat stat; | |
393 | struct kstat pstat; | |
394 | const char *link; | |
395 | struct dentry *destdir; | |
396 | struct qstr destname; | |
397 | struct dentry *workdir; | |
398 | bool tmpfile; | |
399 | bool origin; | |
016b720f | 400 | bool indexed; |
44d5bf10 | 401 | bool metacopy; |
f4439de1 AG |
402 | }; |
403 | ||
404 | static int ovl_link_up(struct ovl_copy_up_ctx *c) | |
59be0971 AG |
405 | { |
406 | int err; | |
407 | struct dentry *upper; | |
f4439de1 | 408 | struct dentry *upperdir = ovl_dentry_upper(c->parent); |
59be0971 AG |
409 | struct inode *udir = d_inode(upperdir); |
410 | ||
f4439de1 AG |
411 | /* Mark parent "impure" because it may now contain non-pure upper */ |
412 | err = ovl_set_impure(c->parent, upperdir); | |
413 | if (err) | |
414 | return err; | |
415 | ||
416 | err = ovl_set_nlink_lower(c->dentry); | |
5f8415d6 AG |
417 | if (err) |
418 | return err; | |
419 | ||
59be0971 | 420 | inode_lock_nested(udir, I_MUTEX_PARENT); |
f4439de1 AG |
421 | upper = lookup_one_len(c->dentry->d_name.name, upperdir, |
422 | c->dentry->d_name.len); | |
59be0971 AG |
423 | err = PTR_ERR(upper); |
424 | if (!IS_ERR(upper)) { | |
6cf00764 | 425 | err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper); |
59be0971 AG |
426 | dput(upper); |
427 | ||
f4439de1 AG |
428 | if (!err) { |
429 | /* Restore timestamps on parent (best effort) */ | |
430 | ovl_set_timestamps(upperdir, &c->pstat); | |
431 | ovl_dentry_set_upper_alias(c->dentry); | |
432 | } | |
59be0971 AG |
433 | } |
434 | inode_unlock(udir); | |
aa3ff3c1 AG |
435 | if (err) |
436 | return err; | |
437 | ||
438 | err = ovl_set_nlink_upper(c->dentry); | |
59be0971 AG |
439 | |
440 | return err; | |
441 | } | |
442 | ||
23f0ab13 MS |
443 | static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp, |
444 | struct dentry **newdentry) | |
15932c41 AG |
445 | { |
446 | int err; | |
447 | struct dentry *upper; | |
59be0971 | 448 | struct inode *udir = d_inode(c->destdir); |
15932c41 | 449 | |
59be0971 | 450 | upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); |
15932c41 AG |
451 | if (IS_ERR(upper)) |
452 | return PTR_ERR(upper); | |
453 | ||
23f0ab13 | 454 | if (c->tmpfile) |
6cf00764 | 455 | err = ovl_do_link(temp, udir, upper); |
15932c41 | 456 | else |
23f0ab13 | 457 | err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0); |
15932c41 | 458 | |
59be0971 | 459 | if (!err) |
23f0ab13 | 460 | *newdentry = dget(c->tmpfile ? upper : temp); |
15932c41 AG |
461 | dput(upper); |
462 | ||
463 | return err; | |
464 | } | |
465 | ||
b148cba4 | 466 | static struct dentry *ovl_get_tmpfile(struct ovl_copy_up_ctx *c) |
e9be9d5e | 467 | { |
e9be9d5e | 468 | int err; |
7d90b853 | 469 | struct dentry *temp; |
d8ad8b49 VG |
470 | const struct cred *old_creds = NULL; |
471 | struct cred *new_creds = NULL; | |
471ec5dc | 472 | struct ovl_cattr cattr = { |
32a3d848 | 473 | /* Can't properly set mode on creation because of the umask */ |
23f0ab13 MS |
474 | .mode = c->stat.mode & S_IFMT, |
475 | .rdev = c->stat.rdev, | |
476 | .link = c->link | |
32a3d848 | 477 | }; |
e9be9d5e | 478 | |
23f0ab13 | 479 | err = security_inode_copy_up(c->dentry, &new_creds); |
d8ad8b49 | 480 | if (err < 0) |
1f244dc5 | 481 | return ERR_PTR(err); |
d8ad8b49 VG |
482 | |
483 | if (new_creds) | |
484 | old_creds = override_creds(new_creds); | |
485 | ||
137ec526 | 486 | if (c->tmpfile) |
23f0ab13 | 487 | temp = ovl_do_tmpfile(c->workdir, c->stat.mode); |
137ec526 AG |
488 | else |
489 | temp = ovl_create_temp(c->workdir, &cattr); | |
1f244dc5 | 490 | |
d8ad8b49 VG |
491 | if (new_creds) { |
492 | revert_creds(old_creds); | |
493 | put_cred(new_creds); | |
494 | } | |
495 | ||
b148cba4 | 496 | return temp; |
7d90b853 MS |
497 | } |
498 | ||
23f0ab13 | 499 | static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp) |
7d90b853 | 500 | { |
7d90b853 MS |
501 | int err; |
502 | ||
23f0ab13 | 503 | err = ovl_copy_xattr(c->lowerpath.dentry, temp); |
e9be9d5e | 504 | if (err) |
02209d10 | 505 | return err; |
e9be9d5e | 506 | |
3a1e819b AG |
507 | /* |
508 | * Store identifier of lower inode in upper inode xattr to | |
509 | * allow lookup of the copy up origin inode. | |
fbaf94ee MS |
510 | * |
511 | * Don't set origin when we are breaking the association with a lower | |
512 | * hard link. | |
3a1e819b | 513 | */ |
59be0971 | 514 | if (c->origin) { |
23f0ab13 | 515 | err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp); |
fbaf94ee | 516 | if (err) |
02209d10 | 517 | return err; |
fbaf94ee | 518 | } |
3a1e819b | 519 | |
44d5bf10 | 520 | if (S_ISREG(c->stat.mode) && !c->metacopy) { |
4f93b426 | 521 | struct path upperpath, datapath; |
bd64e575 VG |
522 | |
523 | ovl_path_upper(c->dentry, &upperpath); | |
524 | BUG_ON(upperpath.dentry != NULL); | |
525 | upperpath.dentry = temp; | |
526 | ||
4f93b426 VG |
527 | ovl_path_lowerdata(c->dentry, &datapath); |
528 | err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size); | |
bd64e575 VG |
529 | if (err) |
530 | return err; | |
531 | } | |
532 | ||
0c288874 VG |
533 | if (c->metacopy) { |
534 | err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY, | |
535 | NULL, 0, -EOPNOTSUPP); | |
536 | if (err) | |
537 | return err; | |
538 | } | |
539 | ||
bd64e575 | 540 | inode_lock(temp->d_inode); |
0c288874 VG |
541 | if (c->metacopy) |
542 | err = ovl_set_size(temp, &c->stat); | |
543 | if (!err) | |
544 | err = ovl_set_attr(temp, &c->stat); | |
bd64e575 VG |
545 | inode_unlock(temp->d_inode); |
546 | ||
547 | return err; | |
02209d10 AG |
548 | } |
549 | ||
23f0ab13 | 550 | static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c) |
02209d10 | 551 | { |
59be0971 | 552 | struct inode *udir = c->destdir->d_inode; |
b79e05aa | 553 | struct inode *inode; |
02209d10 | 554 | struct dentry *newdentry = NULL; |
b148cba4 | 555 | struct dentry *temp; |
02209d10 AG |
556 | int err; |
557 | ||
b148cba4 MS |
558 | temp = ovl_get_tmpfile(c); |
559 | if (IS_ERR(temp)) | |
560 | return PTR_ERR(temp); | |
02209d10 | 561 | |
23f0ab13 | 562 | err = ovl_copy_up_inode(c, temp); |
02209d10 | 563 | if (err) |
b148cba4 | 564 | goto out; |
02209d10 | 565 | |
016b720f AG |
566 | if (S_ISDIR(c->stat.mode) && c->indexed) { |
567 | err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp); | |
568 | if (err) | |
b148cba4 | 569 | goto out; |
016b720f AG |
570 | } |
571 | ||
23f0ab13 | 572 | if (c->tmpfile) { |
15932c41 | 573 | inode_lock_nested(udir, I_MUTEX_PARENT); |
23f0ab13 | 574 | err = ovl_install_temp(c, temp, &newdentry); |
15932c41 AG |
575 | inode_unlock(udir); |
576 | } else { | |
23f0ab13 | 577 | err = ovl_install_temp(c, temp, &newdentry); |
e85f82ff | 578 | } |
e9be9d5e | 579 | if (err) |
b148cba4 | 580 | goto out; |
e9be9d5e | 581 | |
0c288874 VG |
582 | if (!c->metacopy) |
583 | ovl_set_upperdata(d_inode(c->dentry)); | |
b79e05aa AG |
584 | inode = d_inode(c->dentry); |
585 | ovl_inode_update(inode, newdentry); | |
586 | if (S_ISDIR(inode->i_mode)) | |
587 | ovl_set_flag(OVL_WHITEOUTS, inode); | |
588 | ||
e85f82ff | 589 | out: |
b148cba4 MS |
590 | if (err && !c->tmpfile) |
591 | ovl_cleanup(d_inode(c->workdir), temp); | |
42f269b9 | 592 | dput(temp); |
e9be9d5e MS |
593 | return err; |
594 | ||
e9be9d5e MS |
595 | } |
596 | ||
597 | /* | |
598 | * Copy up a single dentry | |
599 | * | |
a6c60655 MS |
600 | * All renames start with copy up of source if necessary. The actual |
601 | * rename will only proceed once the copy up was successful. Copy up uses | |
602 | * upper parent i_mutex for exclusion. Since rename can change d_parent it | |
603 | * is possible that the copy up will lock the old parent. At that point | |
604 | * the file will have already been copied up anyway. | |
e9be9d5e | 605 | */ |
a6fb235a | 606 | static int ovl_do_copy_up(struct ovl_copy_up_ctx *c) |
e9be9d5e | 607 | { |
e9be9d5e | 608 | int err; |
23f0ab13 | 609 | struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info; |
016b720f | 610 | bool to_index = false; |
59be0971 | 611 | |
016b720f AG |
612 | /* |
613 | * Indexed non-dir is copied up directly to the index entry and then | |
614 | * hardlinked to upper dir. Indexed dir is copied up to indexdir, | |
615 | * then index entry is created and then copied up dir installed. | |
616 | * Copying dir up to indexdir instead of workdir simplifies locking. | |
617 | */ | |
618 | if (ovl_need_index(c->dentry)) { | |
619 | c->indexed = true; | |
620 | if (S_ISDIR(c->stat.mode)) | |
621 | c->workdir = ovl_indexdir(c->dentry->d_sb); | |
622 | else | |
623 | to_index = true; | |
624 | } | |
625 | ||
626 | if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index) | |
59be0971 AG |
627 | c->origin = true; |
628 | ||
016b720f | 629 | if (to_index) { |
59be0971 AG |
630 | c->destdir = ovl_indexdir(c->dentry->d_sb); |
631 | err = ovl_get_index_name(c->lowerpath.dentry, &c->destname); | |
632 | if (err) | |
633 | return err; | |
aa3ff3c1 AG |
634 | } else if (WARN_ON(!c->parent)) { |
635 | /* Disconnected dentry must be copied up to index dir */ | |
636 | return -EIO; | |
59be0971 AG |
637 | } else { |
638 | /* | |
639 | * Mark parent "impure" because it may now contain non-pure | |
640 | * upper | |
641 | */ | |
642 | err = ovl_set_impure(c->parent, c->destdir); | |
643 | if (err) | |
644 | return err; | |
645 | } | |
e9be9d5e | 646 | |
01ad3eb8 | 647 | /* Should we copyup with O_TMPFILE or with workdir? */ |
23f0ab13 | 648 | if (S_ISREG(c->stat.mode) && ofs->tmpfile) { |
23f0ab13 | 649 | c->tmpfile = true; |
59be0971 AG |
650 | err = ovl_copy_up_locked(c); |
651 | } else { | |
5820dc08 AG |
652 | err = ovl_lock_rename_workdir(c->workdir, c->destdir); |
653 | if (!err) { | |
59be0971 AG |
654 | err = ovl_copy_up_locked(c); |
655 | unlock_rename(c->workdir, c->destdir); | |
656 | } | |
01ad3eb8 AG |
657 | } |
658 | ||
aa3ff3c1 AG |
659 | |
660 | if (err) | |
661 | goto out; | |
662 | ||
663 | if (c->indexed) | |
016b720f AG |
664 | ovl_set_flag(OVL_INDEX, d_inode(c->dentry)); |
665 | ||
666 | if (to_index) { | |
aa3ff3c1 AG |
667 | /* Initialize nlink for copy up of disconnected dentry */ |
668 | err = ovl_set_nlink_upper(c->dentry); | |
669 | } else { | |
59be0971 AG |
670 | struct inode *udir = d_inode(c->destdir); |
671 | ||
672 | /* Restore timestamps on parent (best effort) */ | |
673 | inode_lock(udir); | |
674 | ovl_set_timestamps(c->destdir, &c->pstat); | |
675 | inode_unlock(udir); | |
676 | ||
677 | ovl_dentry_set_upper_alias(c->dentry); | |
e9be9d5e MS |
678 | } |
679 | ||
aa3ff3c1 AG |
680 | out: |
681 | if (to_index) | |
682 | kfree(c->destname.name); | |
a6fb235a MS |
683 | return err; |
684 | } | |
685 | ||
44d5bf10 VG |
686 | static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode, |
687 | int flags) | |
688 | { | |
689 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
690 | ||
44d5bf10 VG |
691 | if (!ofs->config.metacopy) |
692 | return false; | |
693 | ||
694 | if (!S_ISREG(mode)) | |
695 | return false; | |
696 | ||
697 | if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC))) | |
698 | return false; | |
699 | ||
700 | return true; | |
701 | } | |
702 | ||
0c288874 VG |
703 | /* Copy up data of an inode which was copied up metadata only in the past. */ |
704 | static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c) | |
705 | { | |
4f93b426 | 706 | struct path upperpath, datapath; |
0c288874 VG |
707 | int err; |
708 | ||
709 | ovl_path_upper(c->dentry, &upperpath); | |
710 | if (WARN_ON(upperpath.dentry == NULL)) | |
711 | return -EIO; | |
712 | ||
4f93b426 VG |
713 | ovl_path_lowerdata(c->dentry, &datapath); |
714 | if (WARN_ON(datapath.dentry == NULL)) | |
715 | return -EIO; | |
716 | ||
717 | err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size); | |
0c288874 VG |
718 | if (err) |
719 | return err; | |
720 | ||
721 | err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY); | |
722 | if (err) | |
723 | return err; | |
724 | ||
725 | ovl_set_upperdata(d_inode(c->dentry)); | |
726 | return err; | |
727 | } | |
728 | ||
a6fb235a MS |
729 | static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
730 | int flags) | |
731 | { | |
732 | int err; | |
733 | DEFINE_DELAYED_CALL(done); | |
734 | struct path parentpath; | |
735 | struct ovl_copy_up_ctx ctx = { | |
736 | .parent = parent, | |
737 | .dentry = dentry, | |
738 | .workdir = ovl_workdir(dentry), | |
739 | }; | |
740 | ||
741 | if (WARN_ON(!ctx.workdir)) | |
742 | return -EROFS; | |
743 | ||
744 | ovl_path_lower(dentry, &ctx.lowerpath); | |
745 | err = vfs_getattr(&ctx.lowerpath, &ctx.stat, | |
746 | STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); | |
747 | if (err) | |
748 | return err; | |
749 | ||
44d5bf10 VG |
750 | ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags); |
751 | ||
aa3ff3c1 AG |
752 | if (parent) { |
753 | ovl_path_upper(parent, &parentpath); | |
754 | ctx.destdir = parentpath.dentry; | |
755 | ctx.destname = dentry->d_name; | |
a6fb235a | 756 | |
aa3ff3c1 AG |
757 | err = vfs_getattr(&parentpath, &ctx.pstat, |
758 | STATX_ATIME | STATX_MTIME, | |
759 | AT_STATX_SYNC_AS_STAT); | |
760 | if (err) | |
761 | return err; | |
762 | } | |
a6fb235a MS |
763 | |
764 | /* maybe truncate regular file. this has no effect on dirs */ | |
765 | if (flags & O_TRUNC) | |
766 | ctx.stat.size = 0; | |
767 | ||
768 | if (S_ISLNK(ctx.stat.mode)) { | |
769 | ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done); | |
770 | if (IS_ERR(ctx.link)) | |
771 | return PTR_ERR(ctx.link); | |
772 | } | |
a6fb235a | 773 | |
0c288874 | 774 | err = ovl_copy_up_start(dentry, flags); |
fd210b7d MS |
775 | /* err < 0: interrupted, err > 0: raced with another copy-up */ |
776 | if (unlikely(err)) { | |
777 | if (err > 0) | |
778 | err = 0; | |
779 | } else { | |
59be0971 AG |
780 | if (!ovl_dentry_upper(dentry)) |
781 | err = ovl_do_copy_up(&ctx); | |
aa3ff3c1 | 782 | if (!err && parent && !ovl_dentry_has_upper_alias(dentry)) |
f4439de1 | 783 | err = ovl_link_up(&ctx); |
0c288874 VG |
784 | if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags)) |
785 | err = ovl_copy_up_meta_inode_data(&ctx); | |
fd210b7d MS |
786 | ovl_copy_up_end(dentry); |
787 | } | |
7764235b | 788 | do_delayed_call(&done); |
e9be9d5e MS |
789 | |
790 | return err; | |
791 | } | |
792 | ||
9aba6521 | 793 | int ovl_copy_up_flags(struct dentry *dentry, int flags) |
e9be9d5e | 794 | { |
8eac98b8 VG |
795 | int err = 0; |
796 | const struct cred *old_cred = ovl_override_creds(dentry->d_sb); | |
aa3ff3c1 AG |
797 | bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED); |
798 | ||
799 | /* | |
800 | * With NFS export, copy up can get called for a disconnected non-dir. | |
801 | * In this case, we will copy up lower inode to index dir without | |
802 | * linking it to upper dir. | |
803 | */ | |
804 | if (WARN_ON(disconnected && d_is_dir(dentry))) | |
805 | return -EIO; | |
e9be9d5e | 806 | |
e9be9d5e MS |
807 | while (!err) { |
808 | struct dentry *next; | |
aa3ff3c1 | 809 | struct dentry *parent = NULL; |
e9be9d5e | 810 | |
0c288874 | 811 | if (ovl_already_copied_up(dentry, flags)) |
e9be9d5e MS |
812 | break; |
813 | ||
814 | next = dget(dentry); | |
815 | /* find the topmost dentry not yet copied up */ | |
aa3ff3c1 | 816 | for (; !disconnected;) { |
e9be9d5e MS |
817 | parent = dget_parent(next); |
818 | ||
59be0971 | 819 | if (ovl_dentry_upper(parent)) |
e9be9d5e MS |
820 | break; |
821 | ||
822 | dput(next); | |
823 | next = parent; | |
824 | } | |
825 | ||
a6fb235a | 826 | err = ovl_copy_up_one(parent, next, flags); |
e9be9d5e MS |
827 | |
828 | dput(parent); | |
829 | dput(next); | |
830 | } | |
8eac98b8 | 831 | revert_creds(old_cred); |
e9be9d5e MS |
832 | |
833 | return err; | |
834 | } | |
9aba6521 | 835 | |
d6eac039 VG |
836 | static bool ovl_open_need_copy_up(struct dentry *dentry, int flags) |
837 | { | |
838 | /* Copy up of disconnected dentry does not set upper alias */ | |
0c288874 | 839 | if (ovl_already_copied_up(dentry, flags)) |
d6eac039 VG |
840 | return false; |
841 | ||
842 | if (special_file(d_inode(dentry)->i_mode)) | |
843 | return false; | |
844 | ||
0c288874 | 845 | if (!ovl_open_flags_need_copy_up(flags)) |
d6eac039 VG |
846 | return false; |
847 | ||
848 | return true; | |
849 | } | |
850 | ||
851 | int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags) | |
852 | { | |
853 | int err = 0; | |
854 | ||
855 | if (ovl_open_need_copy_up(dentry, file_flags)) { | |
856 | err = ovl_want_write(dentry); | |
857 | if (!err) { | |
858 | err = ovl_copy_up_flags(dentry, file_flags); | |
859 | ovl_drop_write(dentry); | |
860 | } | |
861 | } | |
862 | ||
863 | return err; | |
864 | } | |
865 | ||
d1e6f6a9 VG |
866 | int ovl_copy_up_with_data(struct dentry *dentry) |
867 | { | |
868 | return ovl_copy_up_flags(dentry, O_WRONLY); | |
869 | } | |
870 | ||
9aba6521 AG |
871 | int ovl_copy_up(struct dentry *dentry) |
872 | { | |
873 | return ovl_copy_up_flags(dentry, 0); | |
874 | } |