Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
e9be9d5e MS |
2 | /* |
3 | * | |
4 | * Copyright (C) 2011 Novell Inc. | |
e9be9d5e MS |
5 | */ |
6 | ||
fb5bb2c3 | 7 | #include <linux/module.h> |
e9be9d5e MS |
8 | #include <linux/fs.h> |
9 | #include <linux/slab.h> | |
10 | #include <linux/file.h> | |
11 | #include <linux/splice.h> | |
12 | #include <linux/xattr.h> | |
13 | #include <linux/security.h> | |
14 | #include <linux/uaccess.h> | |
174cd4b1 | 15 | #include <linux/sched/signal.h> |
5b825c3a | 16 | #include <linux/cred.h> |
e9be9d5e | 17 | #include <linux/namei.h> |
fb5bb2c3 DH |
18 | #include <linux/fdtable.h> |
19 | #include <linux/ratelimit.h> | |
3a1e819b | 20 | #include <linux/exportfs.h> |
e9be9d5e MS |
21 | #include "overlayfs.h" |
22 | ||
23 | #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) | |
24 | ||
670c2324 | 25 | static int ovl_ccup_set(const char *buf, const struct kernel_param *param) |
fb5bb2c3 | 26 | { |
1bd0a3ae | 27 | pr_warn("\"check_copy_up\" module option is obsolete\n"); |
fb5bb2c3 DH |
28 | return 0; |
29 | } | |
30 | ||
670c2324 | 31 | static int ovl_ccup_get(char *buf, const struct kernel_param *param) |
fb5bb2c3 | 32 | { |
670c2324 | 33 | return sprintf(buf, "N\n"); |
fb5bb2c3 DH |
34 | } |
35 | ||
670c2324 | 36 | module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644); |
253e7483 | 37 | MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing"); |
670c2324 | 38 | |
c61ca557 MS |
39 | static bool ovl_must_copy_xattr(const char *name) |
40 | { | |
41 | return !strcmp(name, XATTR_POSIX_ACL_ACCESS) || | |
42 | !strcmp(name, XATTR_POSIX_ACL_DEFAULT) || | |
43 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN); | |
44 | } | |
45 | ||
610afc0b MS |
46 | int ovl_copy_xattr(struct super_block *sb, struct dentry *old, |
47 | struct dentry *new) | |
e9be9d5e | 48 | { |
e4ad29fa VC |
49 | ssize_t list_size, size, value_size = 0; |
50 | char *buf, *name, *value = NULL; | |
520da69d | 51 | int error = 0; |
8b326c61 | 52 | size_t slen; |
e9be9d5e | 53 | |
5d6c3191 AG |
54 | if (!(old->d_inode->i_opflags & IOP_XATTR) || |
55 | !(new->d_inode->i_opflags & IOP_XATTR)) | |
e9be9d5e MS |
56 | return 0; |
57 | ||
58 | list_size = vfs_listxattr(old, NULL, 0); | |
59 | if (list_size <= 0) { | |
60 | if (list_size == -EOPNOTSUPP) | |
61 | return 0; | |
62 | return list_size; | |
63 | } | |
64 | ||
65 | buf = kzalloc(list_size, GFP_KERNEL); | |
66 | if (!buf) | |
67 | return -ENOMEM; | |
68 | ||
e9be9d5e MS |
69 | list_size = vfs_listxattr(old, buf, list_size); |
70 | if (list_size <= 0) { | |
71 | error = list_size; | |
e4ad29fa | 72 | goto out; |
e9be9d5e MS |
73 | } |
74 | ||
8b326c61 MS |
75 | for (name = buf; list_size; name += slen) { |
76 | slen = strnlen(name, list_size) + 1; | |
77 | ||
78 | /* underlying fs providing us with an broken xattr list? */ | |
79 | if (WARN_ON(slen > list_size)) { | |
80 | error = -EIO; | |
81 | break; | |
82 | } | |
83 | list_size -= slen; | |
84 | ||
610afc0b | 85 | if (ovl_is_private_xattr(sb, name)) |
0956254a | 86 | continue; |
e4ad29fa VC |
87 | retry: |
88 | size = vfs_getxattr(old, name, value, value_size); | |
89 | if (size == -ERANGE) | |
90 | size = vfs_getxattr(old, name, NULL, 0); | |
91 | ||
97daf8b9 | 92 | if (size < 0) { |
e9be9d5e | 93 | error = size; |
e4ad29fa | 94 | break; |
e9be9d5e | 95 | } |
e4ad29fa VC |
96 | |
97 | if (size > value_size) { | |
98 | void *new; | |
99 | ||
100 | new = krealloc(value, size, GFP_KERNEL); | |
101 | if (!new) { | |
102 | error = -ENOMEM; | |
103 | break; | |
104 | } | |
105 | value = new; | |
106 | value_size = size; | |
107 | goto retry; | |
108 | } | |
109 | ||
121ab822 VG |
110 | error = security_inode_copy_up_xattr(name); |
111 | if (error < 0 && error != -EOPNOTSUPP) | |
112 | break; | |
113 | if (error == 1) { | |
114 | error = 0; | |
115 | continue; /* Discard */ | |
116 | } | |
e9be9d5e | 117 | error = vfs_setxattr(new, name, value, size, 0); |
c61ca557 MS |
118 | if (error) { |
119 | if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name)) | |
120 | break; | |
121 | ||
122 | /* Ignore failure to copy unknown xattrs */ | |
123 | error = 0; | |
124 | } | |
e9be9d5e | 125 | } |
e9be9d5e MS |
126 | kfree(value); |
127 | out: | |
128 | kfree(buf); | |
129 | return error; | |
130 | } | |
131 | ||
c86243b0 VG |
132 | static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old, |
133 | struct path *new, loff_t len) | |
e9be9d5e MS |
134 | { |
135 | struct file *old_file; | |
136 | struct file *new_file; | |
137 | loff_t old_pos = 0; | |
138 | loff_t new_pos = 0; | |
42ec3d4c | 139 | loff_t cloned; |
b504c654 CX |
140 | loff_t data_pos = -1; |
141 | loff_t hole_len; | |
142 | bool skip_hole = false; | |
e9be9d5e MS |
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 | 158 | /* Try to use clone_file_range to clone up within the same fs */ |
452ce659 | 159 | cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0); |
42ec3d4c | 160 | if (cloned == len) |
2ea98466 AG |
161 | goto out; |
162 | /* Couldn't clone, so now we try to copy the data */ | |
2ea98466 | 163 | |
b504c654 CX |
164 | /* Check if lower fs supports seek operation */ |
165 | if (old_file->f_mode & FMODE_LSEEK && | |
166 | old_file->f_op->llseek) | |
167 | skip_hole = true; | |
168 | ||
e9be9d5e MS |
169 | while (len) { |
170 | size_t this_len = OVL_COPY_UP_CHUNK_SIZE; | |
171 | long bytes; | |
172 | ||
173 | if (len < this_len) | |
174 | this_len = len; | |
175 | ||
176 | if (signal_pending_state(TASK_KILLABLE, current)) { | |
177 | error = -EINTR; | |
178 | break; | |
179 | } | |
180 | ||
b504c654 CX |
181 | /* |
182 | * Fill zero for hole will cost unnecessary disk space | |
183 | * and meanwhile slow down the copy-up speed, so we do | |
184 | * an optimization for hole during copy-up, it relies | |
185 | * on SEEK_DATA implementation in lower fs so if lower | |
186 | * fs does not support it, copy-up will behave as before. | |
187 | * | |
188 | * Detail logic of hole detection as below: | |
189 | * When we detect next data position is larger than current | |
190 | * position we will skip that hole, otherwise we copy | |
191 | * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually, | |
192 | * it may not recognize all kind of holes and sometimes | |
193 | * only skips partial of hole area. However, it will be | |
194 | * enough for most of the use cases. | |
195 | */ | |
196 | ||
197 | if (skip_hole && data_pos < old_pos) { | |
198 | data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA); | |
199 | if (data_pos > old_pos) { | |
200 | hole_len = data_pos - old_pos; | |
201 | len -= hole_len; | |
202 | old_pos = new_pos = data_pos; | |
203 | continue; | |
204 | } else if (data_pos == -ENXIO) { | |
205 | break; | |
206 | } else if (data_pos < 0) { | |
207 | skip_hole = false; | |
208 | } | |
209 | } | |
210 | ||
e9be9d5e MS |
211 | bytes = do_splice_direct(old_file, &old_pos, |
212 | new_file, &new_pos, | |
213 | this_len, SPLICE_F_MOVE); | |
214 | if (bytes <= 0) { | |
215 | error = bytes; | |
216 | break; | |
217 | } | |
218 | WARN_ON(old_pos != new_pos); | |
219 | ||
220 | len -= bytes; | |
221 | } | |
2ea98466 | 222 | out: |
c86243b0 | 223 | if (!error && ovl_should_sync(ofs)) |
641089c1 | 224 | error = vfs_fsync(new_file, 0); |
e9be9d5e MS |
225 | fput(new_file); |
226 | out_fput: | |
227 | fput(old_file); | |
228 | return error; | |
229 | } | |
230 | ||
0c288874 VG |
231 | static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat) |
232 | { | |
233 | struct iattr attr = { | |
234 | .ia_valid = ATTR_SIZE, | |
235 | .ia_size = stat->size, | |
236 | }; | |
237 | ||
238 | return notify_change(upperdentry, &attr, NULL); | |
239 | } | |
240 | ||
e9be9d5e MS |
241 | static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) |
242 | { | |
243 | struct iattr attr = { | |
244 | .ia_valid = | |
245 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, | |
246 | .ia_atime = stat->atime, | |
247 | .ia_mtime = stat->mtime, | |
248 | }; | |
249 | ||
250 | return notify_change(upperdentry, &attr, NULL); | |
251 | } | |
252 | ||
253 | int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) | |
254 | { | |
255 | int err = 0; | |
256 | ||
257 | if (!S_ISLNK(stat->mode)) { | |
258 | struct iattr attr = { | |
259 | .ia_valid = ATTR_MODE, | |
260 | .ia_mode = stat->mode, | |
261 | }; | |
262 | err = notify_change(upperdentry, &attr, NULL); | |
263 | } | |
264 | if (!err) { | |
265 | struct iattr attr = { | |
266 | .ia_valid = ATTR_UID | ATTR_GID, | |
267 | .ia_uid = stat->uid, | |
268 | .ia_gid = stat->gid, | |
269 | }; | |
270 | err = notify_change(upperdentry, &attr, NULL); | |
271 | } | |
272 | if (!err) | |
273 | ovl_set_timestamps(upperdentry, stat); | |
274 | ||
275 | return err; | |
e9be9d5e MS |
276 | } |
277 | ||
1cdb0cb6 PT |
278 | struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real, |
279 | bool is_upper) | |
3a1e819b AG |
280 | { |
281 | struct ovl_fh *fh; | |
ec7bbb53 | 282 | int fh_type, dwords; |
3a1e819b | 283 | int buflen = MAX_HANDLE_SZ; |
05122443 | 284 | uuid_t *uuid = &real->d_sb->s_uuid; |
ec7bbb53 | 285 | int err; |
3a1e819b | 286 | |
ec7bbb53 AG |
287 | /* Make sure the real fid stays 32bit aligned */ |
288 | BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4); | |
289 | BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255); | |
290 | ||
291 | fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL); | |
292 | if (!fh) | |
3a1e819b AG |
293 | return ERR_PTR(-ENOMEM); |
294 | ||
295 | /* | |
296 | * We encode a non-connectable file handle for non-dir, because we | |
297 | * only need to find the lower inode number and we don't want to pay | |
298 | * the price or reconnecting the dentry. | |
299 | */ | |
300 | dwords = buflen >> 2; | |
ec7bbb53 | 301 | fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0); |
3a1e819b AG |
302 | buflen = (dwords << 2); |
303 | ||
ec7bbb53 | 304 | err = -EIO; |
3a1e819b AG |
305 | if (WARN_ON(fh_type < 0) || |
306 | WARN_ON(buflen > MAX_HANDLE_SZ) || | |
307 | WARN_ON(fh_type == FILEID_INVALID)) | |
ec7bbb53 | 308 | goto out_err; |
3a1e819b | 309 | |
cbe7fba8 AG |
310 | fh->fb.version = OVL_FH_VERSION; |
311 | fh->fb.magic = OVL_FH_MAGIC; | |
312 | fh->fb.type = fh_type; | |
313 | fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN; | |
54fb347e AG |
314 | /* |
315 | * When we will want to decode an overlay dentry from this handle | |
316 | * and all layers are on the same fs, if we get a disconncted real | |
317 | * dentry when we decode fid, the only way to tell if we should assign | |
318 | * it to upperdentry or to lowerstack is by checking this flag. | |
319 | */ | |
320 | if (is_upper) | |
cbe7fba8 | 321 | fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER; |
ec7bbb53 | 322 | fh->fb.len = sizeof(fh->fb) + buflen; |
5830fb6b PT |
323 | if (ofs->config.uuid) |
324 | fh->fb.uuid = *uuid; | |
3a1e819b | 325 | |
3a1e819b | 326 | return fh; |
ec7bbb53 AG |
327 | |
328 | out_err: | |
329 | kfree(fh); | |
330 | return ERR_PTR(err); | |
3a1e819b AG |
331 | } |
332 | ||
1cdb0cb6 PT |
333 | int ovl_set_origin(struct ovl_fs *ofs, struct dentry *dentry, |
334 | struct dentry *lower, struct dentry *upper) | |
3a1e819b | 335 | { |
3a1e819b AG |
336 | const struct ovl_fh *fh = NULL; |
337 | int err; | |
338 | ||
339 | /* | |
340 | * When lower layer doesn't support export operations store a 'null' fh, | |
341 | * so we can use the overlay.origin xattr to distignuish between a copy | |
342 | * up and a pure upper inode. | |
343 | */ | |
02bcd157 | 344 | if (ovl_can_decode_fh(lower->d_sb)) { |
1cdb0cb6 | 345 | fh = ovl_encode_real_fh(ofs, lower, false); |
3a1e819b AG |
346 | if (IS_ERR(fh)) |
347 | return PTR_ERR(fh); | |
348 | } | |
349 | ||
6266d465 MS |
350 | /* |
351 | * Do not fail when upper doesn't support xattrs. | |
352 | */ | |
cbe7fba8 AG |
353 | err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh->buf, |
354 | fh ? fh->fb.len : 0, 0); | |
3a1e819b AG |
355 | kfree(fh); |
356 | ||
6939f977 MS |
357 | /* Ignore -EPERM from setting "user.*" on symlink/special */ |
358 | return err == -EPERM ? 0 : err; | |
3a1e819b AG |
359 | } |
360 | ||
016b720f | 361 | /* Store file handle of @upper dir in @index dir entry */ |
610afc0b MS |
362 | static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper, |
363 | struct dentry *index) | |
016b720f AG |
364 | { |
365 | const struct ovl_fh *fh; | |
366 | int err; | |
367 | ||
1cdb0cb6 | 368 | fh = ovl_encode_real_fh(ofs, upper, true); |
016b720f AG |
369 | if (IS_ERR(fh)) |
370 | return PTR_ERR(fh); | |
371 | ||
610afc0b | 372 | err = ovl_do_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len); |
016b720f AG |
373 | |
374 | kfree(fh); | |
375 | return err; | |
376 | } | |
377 | ||
378 | /* | |
379 | * Create and install index entry. | |
380 | * | |
381 | * Caller must hold i_mutex on indexdir. | |
382 | */ | |
383 | static int ovl_create_index(struct dentry *dentry, struct dentry *origin, | |
384 | struct dentry *upper) | |
385 | { | |
1cdb0cb6 | 386 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb); |
016b720f AG |
387 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); |
388 | struct inode *dir = d_inode(indexdir); | |
389 | struct dentry *index = NULL; | |
390 | struct dentry *temp = NULL; | |
391 | struct qstr name = { }; | |
392 | int err; | |
393 | ||
394 | /* | |
395 | * For now this is only used for creating index entry for directories, | |
396 | * because non-dir are copied up directly to index and then hardlinked | |
397 | * to upper dir. | |
398 | * | |
399 | * TODO: implement create index for non-dir, so we can call it when | |
400 | * encoding file handle for non-dir in case index does not exist. | |
401 | */ | |
402 | if (WARN_ON(!d_is_dir(dentry))) | |
403 | return -EIO; | |
404 | ||
405 | /* Directory not expected to be indexed before copy up */ | |
406 | if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry)))) | |
407 | return -EIO; | |
408 | ||
1cdb0cb6 | 409 | err = ovl_get_index_name(ofs, origin, &name); |
016b720f AG |
410 | if (err) |
411 | return err; | |
412 | ||
137ec526 | 413 | temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0)); |
b148cba4 | 414 | err = PTR_ERR(temp); |
016b720f | 415 | if (IS_ERR(temp)) |
b148cba4 | 416 | goto free_name; |
016b720f | 417 | |
1cdb0cb6 | 418 | err = ovl_set_upper_fh(ofs, upper, temp); |
016b720f | 419 | if (err) |
b148cba4 | 420 | goto out; |
016b720f AG |
421 | |
422 | index = lookup_one_len(name.name, indexdir, name.len); | |
423 | if (IS_ERR(index)) { | |
424 | err = PTR_ERR(index); | |
425 | } else { | |
426 | err = ovl_do_rename(dir, temp, dir, index, 0); | |
427 | dput(index); | |
428 | } | |
016b720f | 429 | out: |
b148cba4 MS |
430 | if (err) |
431 | ovl_cleanup(dir, temp); | |
016b720f | 432 | dput(temp); |
b148cba4 | 433 | free_name: |
016b720f AG |
434 | kfree(name.name); |
435 | return err; | |
016b720f AG |
436 | } |
437 | ||
f4439de1 AG |
438 | struct ovl_copy_up_ctx { |
439 | struct dentry *parent; | |
440 | struct dentry *dentry; | |
441 | struct path lowerpath; | |
442 | struct kstat stat; | |
443 | struct kstat pstat; | |
444 | const char *link; | |
445 | struct dentry *destdir; | |
446 | struct qstr destname; | |
447 | struct dentry *workdir; | |
f4439de1 | 448 | bool origin; |
016b720f | 449 | bool indexed; |
44d5bf10 | 450 | bool metacopy; |
f4439de1 AG |
451 | }; |
452 | ||
453 | static int ovl_link_up(struct ovl_copy_up_ctx *c) | |
59be0971 AG |
454 | { |
455 | int err; | |
456 | struct dentry *upper; | |
f4439de1 | 457 | struct dentry *upperdir = ovl_dentry_upper(c->parent); |
59be0971 AG |
458 | struct inode *udir = d_inode(upperdir); |
459 | ||
f4439de1 AG |
460 | /* Mark parent "impure" because it may now contain non-pure upper */ |
461 | err = ovl_set_impure(c->parent, upperdir); | |
462 | if (err) | |
463 | return err; | |
464 | ||
465 | err = ovl_set_nlink_lower(c->dentry); | |
5f8415d6 AG |
466 | if (err) |
467 | return err; | |
468 | ||
59be0971 | 469 | inode_lock_nested(udir, I_MUTEX_PARENT); |
f4439de1 AG |
470 | upper = lookup_one_len(c->dentry->d_name.name, upperdir, |
471 | c->dentry->d_name.len); | |
59be0971 AG |
472 | err = PTR_ERR(upper); |
473 | if (!IS_ERR(upper)) { | |
6cf00764 | 474 | err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper); |
59be0971 AG |
475 | dput(upper); |
476 | ||
f4439de1 AG |
477 | if (!err) { |
478 | /* Restore timestamps on parent (best effort) */ | |
479 | ovl_set_timestamps(upperdir, &c->pstat); | |
480 | ovl_dentry_set_upper_alias(c->dentry); | |
481 | } | |
59be0971 AG |
482 | } |
483 | inode_unlock(udir); | |
aa3ff3c1 AG |
484 | if (err) |
485 | return err; | |
486 | ||
487 | err = ovl_set_nlink_upper(c->dentry); | |
59be0971 AG |
488 | |
489 | return err; | |
490 | } | |
491 | ||
23f0ab13 | 492 | static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp) |
7d90b853 | 493 | { |
c86243b0 | 494 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
7d90b853 MS |
495 | int err; |
496 | ||
5f32879e VG |
497 | /* |
498 | * Copy up data first and then xattrs. Writing data after | |
499 | * xattrs will remove security.capability xattr automatically. | |
500 | */ | |
501 | if (S_ISREG(c->stat.mode) && !c->metacopy) { | |
502 | struct path upperpath, datapath; | |
503 | ||
504 | ovl_path_upper(c->dentry, &upperpath); | |
505 | if (WARN_ON(upperpath.dentry != NULL)) | |
506 | return -EIO; | |
507 | upperpath.dentry = temp; | |
508 | ||
509 | ovl_path_lowerdata(c->dentry, &datapath); | |
c86243b0 VG |
510 | err = ovl_copy_up_data(ofs, &datapath, &upperpath, |
511 | c->stat.size); | |
5f32879e VG |
512 | if (err) |
513 | return err; | |
514 | } | |
515 | ||
610afc0b | 516 | err = ovl_copy_xattr(c->dentry->d_sb, c->lowerpath.dentry, temp); |
e9be9d5e | 517 | if (err) |
02209d10 | 518 | return err; |
e9be9d5e | 519 | |
3a1e819b AG |
520 | /* |
521 | * Store identifier of lower inode in upper inode xattr to | |
522 | * allow lookup of the copy up origin inode. | |
fbaf94ee MS |
523 | * |
524 | * Don't set origin when we are breaking the association with a lower | |
525 | * hard link. | |
3a1e819b | 526 | */ |
59be0971 | 527 | if (c->origin) { |
1cdb0cb6 | 528 | err = ovl_set_origin(ofs, c->dentry, c->lowerpath.dentry, temp); |
fbaf94ee | 529 | if (err) |
02209d10 | 530 | return err; |
fbaf94ee | 531 | } |
3a1e819b | 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); |
b504c654 | 541 | if (S_ISREG(c->stat.mode)) |
0c288874 VG |
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 | ||
6b52243f MS |
550 | struct ovl_cu_creds { |
551 | const struct cred *old; | |
552 | struct cred *new; | |
553 | }; | |
554 | ||
555 | static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc) | |
b10cdcdc AG |
556 | { |
557 | int err; | |
b10cdcdc | 558 | |
6b52243f MS |
559 | cc->old = cc->new = NULL; |
560 | err = security_inode_copy_up(dentry, &cc->new); | |
b10cdcdc | 561 | if (err < 0) |
6b52243f | 562 | return err; |
b10cdcdc | 563 | |
6b52243f MS |
564 | if (cc->new) |
565 | cc->old = override_creds(cc->new); | |
b10cdcdc | 566 | |
6b52243f | 567 | return 0; |
b10cdcdc AG |
568 | } |
569 | ||
6b52243f | 570 | static void ovl_revert_cu_creds(struct ovl_cu_creds *cc) |
b10cdcdc | 571 | { |
6b52243f MS |
572 | if (cc->new) { |
573 | revert_creds(cc->old); | |
574 | put_cred(cc->new); | |
575 | } | |
b10cdcdc AG |
576 | } |
577 | ||
578 | /* | |
579 | * Copyup using workdir to prepare temp file. Used when copying up directories, | |
580 | * special files or when upper fs doesn't support O_TMPFILE. | |
581 | */ | |
582 | static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c) | |
02209d10 | 583 | { |
b79e05aa | 584 | struct inode *inode; |
6b52243f MS |
585 | struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir); |
586 | struct dentry *temp, *upper; | |
587 | struct ovl_cu_creds cc; | |
02209d10 | 588 | int err; |
6b52243f MS |
589 | struct ovl_cattr cattr = { |
590 | /* Can't properly set mode on creation because of the umask */ | |
591 | .mode = c->stat.mode & S_IFMT, | |
592 | .rdev = c->stat.rdev, | |
593 | .link = c->link | |
594 | }; | |
02209d10 | 595 | |
773cb4c5 AG |
596 | /* workdir and destdir could be the same when copying up to indexdir */ |
597 | err = -EIO; | |
598 | if (lock_rename(c->workdir, c->destdir) != NULL) | |
599 | goto unlock; | |
b10cdcdc | 600 | |
6b52243f MS |
601 | err = ovl_prep_cu_creds(c->dentry, &cc); |
602 | if (err) | |
603 | goto unlock; | |
604 | ||
605 | temp = ovl_create_temp(c->workdir, &cattr); | |
606 | ovl_revert_cu_creds(&cc); | |
607 | ||
b10cdcdc | 608 | err = PTR_ERR(temp); |
b148cba4 | 609 | if (IS_ERR(temp)) |
b10cdcdc | 610 | goto unlock; |
02209d10 | 611 | |
23f0ab13 | 612 | err = ovl_copy_up_inode(c, temp); |
02209d10 | 613 | if (err) |
b10cdcdc | 614 | goto cleanup; |
02209d10 | 615 | |
016b720f AG |
616 | if (S_ISDIR(c->stat.mode) && c->indexed) { |
617 | err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp); | |
618 | if (err) | |
b10cdcdc | 619 | goto cleanup; |
016b720f AG |
620 | } |
621 | ||
6b52243f MS |
622 | upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); |
623 | err = PTR_ERR(upper); | |
624 | if (IS_ERR(upper)) | |
625 | goto cleanup; | |
626 | ||
627 | err = ovl_do_rename(wdir, temp, udir, upper, 0); | |
628 | dput(upper); | |
e9be9d5e | 629 | if (err) |
b10cdcdc | 630 | goto cleanup; |
e9be9d5e | 631 | |
0c288874 VG |
632 | if (!c->metacopy) |
633 | ovl_set_upperdata(d_inode(c->dentry)); | |
b79e05aa | 634 | inode = d_inode(c->dentry); |
6b52243f | 635 | ovl_inode_update(inode, temp); |
b79e05aa AG |
636 | if (S_ISDIR(inode->i_mode)) |
637 | ovl_set_flag(OVL_WHITEOUTS, inode); | |
b10cdcdc AG |
638 | unlock: |
639 | unlock_rename(c->workdir, c->destdir); | |
640 | ||
641 | return err; | |
642 | ||
643 | cleanup: | |
6b52243f MS |
644 | ovl_cleanup(wdir, temp); |
645 | dput(temp); | |
646 | goto unlock; | |
b10cdcdc AG |
647 | } |
648 | ||
6b52243f MS |
649 | /* Copyup using O_TMPFILE which does not require cross dir locking */ |
650 | static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c) | |
b10cdcdc | 651 | { |
6b52243f MS |
652 | struct inode *udir = d_inode(c->destdir); |
653 | struct dentry *temp, *upper; | |
654 | struct ovl_cu_creds cc; | |
b10cdcdc | 655 | int err; |
b10cdcdc | 656 | |
6b52243f MS |
657 | err = ovl_prep_cu_creds(c->dentry, &cc); |
658 | if (err) | |
659 | return err; | |
b10cdcdc AG |
660 | |
661 | temp = ovl_do_tmpfile(c->workdir, c->stat.mode); | |
6b52243f | 662 | ovl_revert_cu_creds(&cc); |
b10cdcdc | 663 | |
6b52243f MS |
664 | if (IS_ERR(temp)) |
665 | return PTR_ERR(temp); | |
b10cdcdc | 666 | |
6b52243f MS |
667 | err = ovl_copy_up_inode(c, temp); |
668 | if (err) | |
669 | goto out_dput; | |
b10cdcdc AG |
670 | |
671 | inode_lock_nested(udir, I_MUTEX_PARENT); | |
672 | ||
673 | upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); | |
674 | err = PTR_ERR(upper); | |
6b52243f MS |
675 | if (!IS_ERR(upper)) { |
676 | err = ovl_do_link(temp, udir, upper); | |
677 | dput(upper); | |
678 | } | |
b10cdcdc AG |
679 | inode_unlock(udir); |
680 | ||
b10cdcdc | 681 | if (err) |
6b52243f | 682 | goto out_dput; |
b10cdcdc AG |
683 | |
684 | if (!c->metacopy) | |
685 | ovl_set_upperdata(d_inode(c->dentry)); | |
6b52243f | 686 | ovl_inode_update(d_inode(c->dentry), temp); |
b79e05aa | 687 | |
6b52243f MS |
688 | return 0; |
689 | ||
690 | out_dput: | |
42f269b9 | 691 | dput(temp); |
e9be9d5e | 692 | return err; |
e9be9d5e MS |
693 | } |
694 | ||
695 | /* | |
696 | * Copy up a single dentry | |
697 | * | |
a6c60655 MS |
698 | * All renames start with copy up of source if necessary. The actual |
699 | * rename will only proceed once the copy up was successful. Copy up uses | |
700 | * upper parent i_mutex for exclusion. Since rename can change d_parent it | |
701 | * is possible that the copy up will lock the old parent. At that point | |
702 | * the file will have already been copied up anyway. | |
e9be9d5e | 703 | */ |
a6fb235a | 704 | static int ovl_do_copy_up(struct ovl_copy_up_ctx *c) |
e9be9d5e | 705 | { |
e9be9d5e | 706 | int err; |
1cdb0cb6 | 707 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
016b720f | 708 | bool to_index = false; |
59be0971 | 709 | |
016b720f AG |
710 | /* |
711 | * Indexed non-dir is copied up directly to the index entry and then | |
712 | * hardlinked to upper dir. Indexed dir is copied up to indexdir, | |
713 | * then index entry is created and then copied up dir installed. | |
714 | * Copying dir up to indexdir instead of workdir simplifies locking. | |
715 | */ | |
716 | if (ovl_need_index(c->dentry)) { | |
717 | c->indexed = true; | |
718 | if (S_ISDIR(c->stat.mode)) | |
719 | c->workdir = ovl_indexdir(c->dentry->d_sb); | |
720 | else | |
721 | to_index = true; | |
722 | } | |
723 | ||
724 | if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index) | |
59be0971 AG |
725 | c->origin = true; |
726 | ||
016b720f | 727 | if (to_index) { |
59be0971 | 728 | c->destdir = ovl_indexdir(c->dentry->d_sb); |
1cdb0cb6 | 729 | err = ovl_get_index_name(ofs, c->lowerpath.dentry, &c->destname); |
59be0971 AG |
730 | if (err) |
731 | return err; | |
aa3ff3c1 AG |
732 | } else if (WARN_ON(!c->parent)) { |
733 | /* Disconnected dentry must be copied up to index dir */ | |
734 | return -EIO; | |
59be0971 AG |
735 | } else { |
736 | /* | |
737 | * Mark parent "impure" because it may now contain non-pure | |
738 | * upper | |
739 | */ | |
740 | err = ovl_set_impure(c->parent, c->destdir); | |
741 | if (err) | |
742 | return err; | |
743 | } | |
e9be9d5e | 744 | |
01ad3eb8 | 745 | /* Should we copyup with O_TMPFILE or with workdir? */ |
b10cdcdc AG |
746 | if (S_ISREG(c->stat.mode) && ofs->tmpfile) |
747 | err = ovl_copy_up_tmpfile(c); | |
748 | else | |
749 | err = ovl_copy_up_workdir(c); | |
aa3ff3c1 AG |
750 | if (err) |
751 | goto out; | |
752 | ||
753 | if (c->indexed) | |
016b720f AG |
754 | ovl_set_flag(OVL_INDEX, d_inode(c->dentry)); |
755 | ||
756 | if (to_index) { | |
aa3ff3c1 AG |
757 | /* Initialize nlink for copy up of disconnected dentry */ |
758 | err = ovl_set_nlink_upper(c->dentry); | |
759 | } else { | |
59be0971 AG |
760 | struct inode *udir = d_inode(c->destdir); |
761 | ||
762 | /* Restore timestamps on parent (best effort) */ | |
763 | inode_lock(udir); | |
764 | ovl_set_timestamps(c->destdir, &c->pstat); | |
765 | inode_unlock(udir); | |
766 | ||
767 | ovl_dentry_set_upper_alias(c->dentry); | |
e9be9d5e MS |
768 | } |
769 | ||
aa3ff3c1 AG |
770 | out: |
771 | if (to_index) | |
772 | kfree(c->destname.name); | |
a6fb235a MS |
773 | return err; |
774 | } | |
775 | ||
44d5bf10 VG |
776 | static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode, |
777 | int flags) | |
778 | { | |
779 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
780 | ||
44d5bf10 VG |
781 | if (!ofs->config.metacopy) |
782 | return false; | |
783 | ||
784 | if (!S_ISREG(mode)) | |
785 | return false; | |
786 | ||
787 | if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC))) | |
788 | return false; | |
789 | ||
790 | return true; | |
791 | } | |
792 | ||
de7a52c9 | 793 | static ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value) |
fee0f298 MS |
794 | { |
795 | ssize_t res; | |
de7a52c9 | 796 | char *buf; |
fee0f298 MS |
797 | |
798 | res = vfs_getxattr(dentry, name, NULL, 0); | |
de7a52c9 MS |
799 | if (res == -ENODATA || res == -EOPNOTSUPP) |
800 | res = 0; | |
fee0f298 | 801 | |
de7a52c9 MS |
802 | if (res > 0) { |
803 | buf = kzalloc(res, GFP_KERNEL); | |
fee0f298 MS |
804 | if (!buf) |
805 | return -ENOMEM; | |
806 | ||
807 | res = vfs_getxattr(dentry, name, buf, res); | |
808 | if (res < 0) | |
de7a52c9 MS |
809 | kfree(buf); |
810 | else | |
811 | *value = buf; | |
fee0f298 | 812 | } |
fee0f298 MS |
813 | return res; |
814 | } | |
815 | ||
0c288874 VG |
816 | /* Copy up data of an inode which was copied up metadata only in the past. */ |
817 | static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c) | |
818 | { | |
c86243b0 | 819 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
4f93b426 | 820 | struct path upperpath, datapath; |
0c288874 | 821 | int err; |
993a0b2a | 822 | char *capability = NULL; |
3f649ab7 | 823 | ssize_t cap_size; |
0c288874 VG |
824 | |
825 | ovl_path_upper(c->dentry, &upperpath); | |
826 | if (WARN_ON(upperpath.dentry == NULL)) | |
827 | return -EIO; | |
828 | ||
4f93b426 VG |
829 | ovl_path_lowerdata(c->dentry, &datapath); |
830 | if (WARN_ON(datapath.dentry == NULL)) | |
831 | return -EIO; | |
832 | ||
993a0b2a VG |
833 | if (c->stat.size) { |
834 | err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS, | |
de7a52c9 MS |
835 | &capability); |
836 | if (cap_size < 0) | |
993a0b2a VG |
837 | goto out; |
838 | } | |
839 | ||
c86243b0 | 840 | err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size); |
0c288874 | 841 | if (err) |
993a0b2a VG |
842 | goto out_free; |
843 | ||
844 | /* | |
845 | * Writing to upper file will clear security.capability xattr. We | |
846 | * don't want that to happen for normal copy-up operation. | |
847 | */ | |
848 | if (capability) { | |
71097047 MS |
849 | err = vfs_setxattr(upperpath.dentry, XATTR_NAME_CAPS, |
850 | capability, cap_size, 0); | |
993a0b2a VG |
851 | if (err) |
852 | goto out_free; | |
853 | } | |
854 | ||
0c288874 | 855 | |
610afc0b | 856 | err = ovl_do_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY); |
0c288874 | 857 | if (err) |
993a0b2a | 858 | goto out_free; |
0c288874 VG |
859 | |
860 | ovl_set_upperdata(d_inode(c->dentry)); | |
993a0b2a VG |
861 | out_free: |
862 | kfree(capability); | |
863 | out: | |
0c288874 VG |
864 | return err; |
865 | } | |
866 | ||
a6fb235a MS |
867 | static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
868 | int flags) | |
869 | { | |
870 | int err; | |
871 | DEFINE_DELAYED_CALL(done); | |
872 | struct path parentpath; | |
873 | struct ovl_copy_up_ctx ctx = { | |
874 | .parent = parent, | |
875 | .dentry = dentry, | |
876 | .workdir = ovl_workdir(dentry), | |
877 | }; | |
878 | ||
879 | if (WARN_ON(!ctx.workdir)) | |
880 | return -EROFS; | |
881 | ||
882 | ovl_path_lower(dentry, &ctx.lowerpath); | |
883 | err = vfs_getattr(&ctx.lowerpath, &ctx.stat, | |
884 | STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); | |
885 | if (err) | |
886 | return err; | |
887 | ||
44d5bf10 VG |
888 | ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags); |
889 | ||
aa3ff3c1 AG |
890 | if (parent) { |
891 | ovl_path_upper(parent, &parentpath); | |
892 | ctx.destdir = parentpath.dentry; | |
893 | ctx.destname = dentry->d_name; | |
a6fb235a | 894 | |
aa3ff3c1 AG |
895 | err = vfs_getattr(&parentpath, &ctx.pstat, |
896 | STATX_ATIME | STATX_MTIME, | |
897 | AT_STATX_SYNC_AS_STAT); | |
898 | if (err) | |
899 | return err; | |
900 | } | |
a6fb235a MS |
901 | |
902 | /* maybe truncate regular file. this has no effect on dirs */ | |
903 | if (flags & O_TRUNC) | |
904 | ctx.stat.size = 0; | |
905 | ||
906 | if (S_ISLNK(ctx.stat.mode)) { | |
907 | ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done); | |
908 | if (IS_ERR(ctx.link)) | |
909 | return PTR_ERR(ctx.link); | |
910 | } | |
a6fb235a | 911 | |
0c288874 | 912 | err = ovl_copy_up_start(dentry, flags); |
fd210b7d MS |
913 | /* err < 0: interrupted, err > 0: raced with another copy-up */ |
914 | if (unlikely(err)) { | |
915 | if (err > 0) | |
916 | err = 0; | |
917 | } else { | |
59be0971 AG |
918 | if (!ovl_dentry_upper(dentry)) |
919 | err = ovl_do_copy_up(&ctx); | |
aa3ff3c1 | 920 | if (!err && parent && !ovl_dentry_has_upper_alias(dentry)) |
f4439de1 | 921 | err = ovl_link_up(&ctx); |
0c288874 VG |
922 | if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags)) |
923 | err = ovl_copy_up_meta_inode_data(&ctx); | |
fd210b7d MS |
924 | ovl_copy_up_end(dentry); |
925 | } | |
7764235b | 926 | do_delayed_call(&done); |
e9be9d5e MS |
927 | |
928 | return err; | |
929 | } | |
930 | ||
5ac8e802 | 931 | static int ovl_copy_up_flags(struct dentry *dentry, int flags) |
e9be9d5e | 932 | { |
8eac98b8 VG |
933 | int err = 0; |
934 | const struct cred *old_cred = ovl_override_creds(dentry->d_sb); | |
aa3ff3c1 AG |
935 | bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED); |
936 | ||
937 | /* | |
938 | * With NFS export, copy up can get called for a disconnected non-dir. | |
939 | * In this case, we will copy up lower inode to index dir without | |
940 | * linking it to upper dir. | |
941 | */ | |
942 | if (WARN_ON(disconnected && d_is_dir(dentry))) | |
943 | return -EIO; | |
e9be9d5e | 944 | |
e9be9d5e MS |
945 | while (!err) { |
946 | struct dentry *next; | |
aa3ff3c1 | 947 | struct dentry *parent = NULL; |
e9be9d5e | 948 | |
0c288874 | 949 | if (ovl_already_copied_up(dentry, flags)) |
e9be9d5e MS |
950 | break; |
951 | ||
952 | next = dget(dentry); | |
953 | /* find the topmost dentry not yet copied up */ | |
aa3ff3c1 | 954 | for (; !disconnected;) { |
e9be9d5e MS |
955 | parent = dget_parent(next); |
956 | ||
59be0971 | 957 | if (ovl_dentry_upper(parent)) |
e9be9d5e MS |
958 | break; |
959 | ||
960 | dput(next); | |
961 | next = parent; | |
962 | } | |
963 | ||
a6fb235a | 964 | err = ovl_copy_up_one(parent, next, flags); |
e9be9d5e MS |
965 | |
966 | dput(parent); | |
967 | dput(next); | |
968 | } | |
8eac98b8 | 969 | revert_creds(old_cred); |
e9be9d5e MS |
970 | |
971 | return err; | |
972 | } | |
9aba6521 | 973 | |
d6eac039 VG |
974 | static bool ovl_open_need_copy_up(struct dentry *dentry, int flags) |
975 | { | |
976 | /* Copy up of disconnected dentry does not set upper alias */ | |
0c288874 | 977 | if (ovl_already_copied_up(dentry, flags)) |
d6eac039 VG |
978 | return false; |
979 | ||
980 | if (special_file(d_inode(dentry)->i_mode)) | |
981 | return false; | |
982 | ||
0c288874 | 983 | if (!ovl_open_flags_need_copy_up(flags)) |
d6eac039 VG |
984 | return false; |
985 | ||
986 | return true; | |
987 | } | |
988 | ||
3428030d | 989 | int ovl_maybe_copy_up(struct dentry *dentry, int flags) |
d6eac039 VG |
990 | { |
991 | int err = 0; | |
992 | ||
3428030d | 993 | if (ovl_open_need_copy_up(dentry, flags)) { |
d6eac039 VG |
994 | err = ovl_want_write(dentry); |
995 | if (!err) { | |
3428030d | 996 | err = ovl_copy_up_flags(dentry, flags); |
d6eac039 VG |
997 | ovl_drop_write(dentry); |
998 | } | |
999 | } | |
1000 | ||
1001 | return err; | |
1002 | } | |
1003 | ||
d1e6f6a9 VG |
1004 | int ovl_copy_up_with_data(struct dentry *dentry) |
1005 | { | |
1006 | return ovl_copy_up_flags(dentry, O_WRONLY); | |
1007 | } | |
1008 | ||
9aba6521 AG |
1009 | int ovl_copy_up(struct dentry *dentry) |
1010 | { | |
1011 | return ovl_copy_up_flags(dentry, 0); | |
1012 | } |