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> | |
72db8211 | 11 | #include <linux/fileattr.h> |
e9be9d5e MS |
12 | #include <linux/splice.h> |
13 | #include <linux/xattr.h> | |
14 | #include <linux/security.h> | |
15 | #include <linux/uaccess.h> | |
174cd4b1 | 16 | #include <linux/sched/signal.h> |
5b825c3a | 17 | #include <linux/cred.h> |
e9be9d5e | 18 | #include <linux/namei.h> |
fb5bb2c3 DH |
19 | #include <linux/fdtable.h> |
20 | #include <linux/ratelimit.h> | |
3a1e819b | 21 | #include <linux/exportfs.h> |
e9be9d5e MS |
22 | #include "overlayfs.h" |
23 | ||
24 | #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) | |
25 | ||
670c2324 | 26 | static int ovl_ccup_set(const char *buf, const struct kernel_param *param) |
fb5bb2c3 | 27 | { |
1bd0a3ae | 28 | pr_warn("\"check_copy_up\" module option is obsolete\n"); |
fb5bb2c3 DH |
29 | return 0; |
30 | } | |
31 | ||
670c2324 | 32 | static int ovl_ccup_get(char *buf, const struct kernel_param *param) |
fb5bb2c3 | 33 | { |
670c2324 | 34 | return sprintf(buf, "N\n"); |
fb5bb2c3 DH |
35 | } |
36 | ||
670c2324 | 37 | module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644); |
253e7483 | 38 | MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing"); |
670c2324 | 39 | |
c61ca557 MS |
40 | static bool ovl_must_copy_xattr(const char *name) |
41 | { | |
42 | return !strcmp(name, XATTR_POSIX_ACL_ACCESS) || | |
43 | !strcmp(name, XATTR_POSIX_ACL_DEFAULT) || | |
44 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN); | |
45 | } | |
46 | ||
31acceb9 CB |
47 | static int ovl_copy_acl(struct ovl_fs *ofs, const struct path *path, |
48 | struct dentry *dentry, const char *acl_name) | |
49 | { | |
50 | int err; | |
51 | struct posix_acl *clone, *real_acl = NULL; | |
52 | ||
53 | real_acl = ovl_get_acl_path(path, acl_name, false); | |
54 | if (!real_acl) | |
55 | return 0; | |
56 | ||
57 | if (IS_ERR(real_acl)) { | |
58 | err = PTR_ERR(real_acl); | |
59 | if (err == -ENODATA || err == -EOPNOTSUPP) | |
60 | return 0; | |
61 | return err; | |
62 | } | |
63 | ||
64 | clone = posix_acl_clone(real_acl, GFP_KERNEL); | |
65 | posix_acl_release(real_acl); /* release original acl */ | |
66 | if (!clone) | |
67 | return -ENOMEM; | |
68 | ||
69 | err = ovl_do_set_acl(ofs, dentry, acl_name, clone); | |
70 | ||
71 | /* release cloned acl */ | |
72 | posix_acl_release(clone); | |
73 | return err; | |
74 | } | |
75 | ||
2d343087 | 76 | int ovl_copy_xattr(struct super_block *sb, const struct path *oldpath, struct dentry *new) |
e9be9d5e | 77 | { |
dad7017a | 78 | struct dentry *old = oldpath->dentry; |
e4ad29fa VC |
79 | ssize_t list_size, size, value_size = 0; |
80 | char *buf, *name, *value = NULL; | |
520da69d | 81 | int error = 0; |
8b326c61 | 82 | size_t slen; |
e9be9d5e | 83 | |
a1fbb607 | 84 | if (!old->d_inode->i_op->listxattr || !new->d_inode->i_op->listxattr) |
e9be9d5e MS |
85 | return 0; |
86 | ||
87 | list_size = vfs_listxattr(old, NULL, 0); | |
88 | if (list_size <= 0) { | |
89 | if (list_size == -EOPNOTSUPP) | |
90 | return 0; | |
91 | return list_size; | |
92 | } | |
93 | ||
f945ca19 | 94 | buf = kvzalloc(list_size, GFP_KERNEL); |
e9be9d5e MS |
95 | if (!buf) |
96 | return -ENOMEM; | |
97 | ||
e9be9d5e MS |
98 | list_size = vfs_listxattr(old, buf, list_size); |
99 | if (list_size <= 0) { | |
100 | error = list_size; | |
e4ad29fa | 101 | goto out; |
e9be9d5e MS |
102 | } |
103 | ||
8b326c61 MS |
104 | for (name = buf; list_size; name += slen) { |
105 | slen = strnlen(name, list_size) + 1; | |
106 | ||
107 | /* underlying fs providing us with an broken xattr list? */ | |
108 | if (WARN_ON(slen > list_size)) { | |
109 | error = -EIO; | |
110 | break; | |
111 | } | |
112 | list_size -= slen; | |
113 | ||
610afc0b | 114 | if (ovl_is_private_xattr(sb, name)) |
0956254a | 115 | continue; |
03fedf93 | 116 | |
32538047 | 117 | error = security_inode_copy_up_xattr(old, name); |
924e19c3 | 118 | if (error == -ECANCELED) { |
03fedf93 AG |
119 | error = 0; |
120 | continue; /* Discard */ | |
121 | } | |
924e19c3 XK |
122 | if (error < 0 && error != -EOPNOTSUPP) |
123 | break; | |
31acceb9 CB |
124 | |
125 | if (is_posix_acl_xattr(name)) { | |
126 | error = ovl_copy_acl(OVL_FS(sb), oldpath, new, name); | |
127 | if (!error) | |
128 | continue; | |
129 | /* POSIX ACLs must be copied. */ | |
130 | break; | |
131 | } | |
132 | ||
e4ad29fa | 133 | retry: |
dad7017a | 134 | size = ovl_do_getxattr(oldpath, name, value, value_size); |
e4ad29fa | 135 | if (size == -ERANGE) |
dad7017a | 136 | size = ovl_do_getxattr(oldpath, name, NULL, 0); |
e4ad29fa | 137 | |
97daf8b9 | 138 | if (size < 0) { |
e9be9d5e | 139 | error = size; |
e4ad29fa | 140 | break; |
e9be9d5e | 141 | } |
e4ad29fa VC |
142 | |
143 | if (size > value_size) { | |
144 | void *new; | |
145 | ||
f945ca19 | 146 | new = kvmalloc(size, GFP_KERNEL); |
e4ad29fa VC |
147 | if (!new) { |
148 | error = -ENOMEM; | |
149 | break; | |
150 | } | |
f945ca19 | 151 | kvfree(value); |
e4ad29fa VC |
152 | value = new; |
153 | value_size = size; | |
154 | goto retry; | |
155 | } | |
156 | ||
c914c0e2 | 157 | error = ovl_do_setxattr(OVL_FS(sb), new, name, value, size, 0); |
c61ca557 MS |
158 | if (error) { |
159 | if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name)) | |
160 | break; | |
161 | ||
162 | /* Ignore failure to copy unknown xattrs */ | |
163 | error = 0; | |
164 | } | |
e9be9d5e | 165 | } |
f945ca19 | 166 | kvfree(value); |
e9be9d5e | 167 | out: |
f945ca19 | 168 | kvfree(buf); |
e9be9d5e MS |
169 | return error; |
170 | } | |
171 | ||
2d343087 AV |
172 | static int ovl_copy_fileattr(struct inode *inode, const struct path *old, |
173 | const struct path *new) | |
72db8211 AG |
174 | { |
175 | struct fileattr oldfa = { .flags_valid = true }; | |
176 | struct fileattr newfa = { .flags_valid = true }; | |
177 | int err; | |
178 | ||
179 | err = ovl_real_fileattr_get(old, &oldfa); | |
5b0a414d MS |
180 | if (err) { |
181 | /* Ntfs-3g returns -EINVAL for "no fileattr support" */ | |
182 | if (err == -ENOTTY || err == -EINVAL) | |
183 | return 0; | |
184 | pr_warn("failed to retrieve lower fileattr (%pd2, err=%i)\n", | |
4ee7e4a6 | 185 | old->dentry, err); |
72db8211 | 186 | return err; |
5b0a414d | 187 | } |
72db8211 | 188 | |
096a218a AG |
189 | /* |
190 | * We cannot set immutable and append-only flags on upper inode, | |
191 | * because we would not be able to link upper inode to upper dir | |
192 | * not set overlay private xattr on upper inode. | |
193 | * Store these flags in overlay.protattr xattr instead. | |
194 | */ | |
195 | if (oldfa.flags & OVL_PROT_FS_FLAGS_MASK) { | |
196 | err = ovl_set_protattr(inode, new->dentry, &oldfa); | |
94fd1975 MS |
197 | if (err == -EPERM) |
198 | pr_warn_once("copying fileattr: no xattr on upper\n"); | |
199 | else if (err) | |
096a218a AG |
200 | return err; |
201 | } | |
202 | ||
5b0a414d MS |
203 | /* Don't bother copying flags if none are set */ |
204 | if (!(oldfa.flags & OVL_COPY_FS_FLAGS_MASK)) | |
205 | return 0; | |
206 | ||
207 | err = ovl_real_fileattr_get(new, &newfa); | |
208 | if (err) { | |
94fd1975 MS |
209 | /* |
210 | * Returning an error if upper doesn't support fileattr will | |
211 | * result in a regression, so revert to the old behavior. | |
212 | */ | |
213 | if (err == -ENOTTY || err == -EINVAL) { | |
214 | pr_warn_once("copying fileattr: no support on upper\n"); | |
215 | return 0; | |
216 | } | |
5b0a414d | 217 | pr_warn("failed to retrieve upper fileattr (%pd2, err=%i)\n", |
4ee7e4a6 | 218 | new->dentry, err); |
5b0a414d MS |
219 | return err; |
220 | } | |
221 | ||
72db8211 AG |
222 | BUILD_BUG_ON(OVL_COPY_FS_FLAGS_MASK & ~FS_COMMON_FL); |
223 | newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK; | |
224 | newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK); | |
225 | ||
226 | BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON); | |
227 | newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK; | |
228 | newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK); | |
229 | ||
230 | return ovl_real_fileattr_set(new, &newfa); | |
231 | } | |
232 | ||
ca7ab482 AG |
233 | static int ovl_verify_area(loff_t pos, loff_t pos2, loff_t len, loff_t totlen) |
234 | { | |
235 | loff_t tmp; | |
236 | ||
77a28aa4 | 237 | if (pos != pos2) |
ca7ab482 | 238 | return -EIO; |
77a28aa4 | 239 | if (pos < 0 || len < 0 || totlen < 0) |
ca7ab482 | 240 | return -EIO; |
77a28aa4 | 241 | if (check_add_overflow(pos, len, &tmp)) |
ca7ab482 AG |
242 | return -EIO; |
243 | return 0; | |
244 | } | |
245 | ||
7d6899fb AG |
246 | static int ovl_sync_file(struct path *path) |
247 | { | |
248 | struct file *new_file; | |
249 | int err; | |
250 | ||
251 | new_file = ovl_path_open(path, O_LARGEFILE | O_RDONLY); | |
252 | if (IS_ERR(new_file)) | |
253 | return PTR_ERR(new_file); | |
254 | ||
255 | err = vfs_fsync(new_file, 0); | |
256 | fput(new_file); | |
257 | ||
258 | return err; | |
259 | } | |
260 | ||
2b1a7746 | 261 | static int ovl_copy_up_file(struct ovl_fs *ofs, struct dentry *dentry, |
7d6899fb AG |
262 | struct file *new_file, loff_t len, |
263 | bool datasync) | |
e9be9d5e | 264 | { |
2b1a7746 | 265 | struct path datapath; |
e9be9d5e | 266 | struct file *old_file; |
e9be9d5e MS |
267 | loff_t old_pos = 0; |
268 | loff_t new_pos = 0; | |
42ec3d4c | 269 | loff_t cloned; |
b504c654 CX |
270 | loff_t data_pos = -1; |
271 | loff_t hole_len; | |
272 | bool skip_hole = false; | |
e9be9d5e MS |
273 | int error = 0; |
274 | ||
2b1a7746 | 275 | ovl_path_lowerdata(dentry, &datapath); |
ca7ab482 AG |
276 | if (WARN_ON_ONCE(datapath.dentry == NULL) || |
277 | WARN_ON_ONCE(len < 0)) | |
2b1a7746 | 278 | return -EIO; |
e9be9d5e | 279 | |
2b1a7746 | 280 | old_file = ovl_path_open(&datapath, O_LARGEFILE | O_RDONLY); |
e9be9d5e MS |
281 | if (IS_ERR(old_file)) |
282 | return PTR_ERR(old_file); | |
283 | ||
853b8d75 AG |
284 | /* Try to use clone_file_range to clone up within the same fs */ |
285 | cloned = vfs_clone_file_range(old_file, 0, new_file, 0, len, 0); | |
286 | if (cloned == len) | |
287 | goto out_fput; | |
288 | ||
289 | /* Couldn't clone, so now we try to copy the data */ | |
ca7ab482 AG |
290 | error = rw_verify_area(READ, old_file, &old_pos, len); |
291 | if (!error) | |
292 | error = rw_verify_area(WRITE, new_file, &new_pos, len); | |
293 | if (error) | |
294 | goto out_fput; | |
295 | ||
b504c654 | 296 | /* Check if lower fs supports seek operation */ |
4e3299ea | 297 | if (old_file->f_mode & FMODE_LSEEK) |
b504c654 CX |
298 | skip_hole = true; |
299 | ||
e9be9d5e MS |
300 | while (len) { |
301 | size_t this_len = OVL_COPY_UP_CHUNK_SIZE; | |
0f292086 | 302 | ssize_t bytes; |
e9be9d5e MS |
303 | |
304 | if (len < this_len) | |
305 | this_len = len; | |
306 | ||
307 | if (signal_pending_state(TASK_KILLABLE, current)) { | |
308 | error = -EINTR; | |
309 | break; | |
310 | } | |
311 | ||
b504c654 CX |
312 | /* |
313 | * Fill zero for hole will cost unnecessary disk space | |
314 | * and meanwhile slow down the copy-up speed, so we do | |
315 | * an optimization for hole during copy-up, it relies | |
316 | * on SEEK_DATA implementation in lower fs so if lower | |
317 | * fs does not support it, copy-up will behave as before. | |
318 | * | |
319 | * Detail logic of hole detection as below: | |
320 | * When we detect next data position is larger than current | |
321 | * position we will skip that hole, otherwise we copy | |
322 | * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually, | |
323 | * it may not recognize all kind of holes and sometimes | |
324 | * only skips partial of hole area. However, it will be | |
325 | * enough for most of the use cases. | |
c63e56a4 AG |
326 | * |
327 | * We do not hold upper sb_writers throughout the loop to avert | |
328 | * lockdep warning with llseek of lower file in nested overlay: | |
329 | * - upper sb_writers | |
330 | * -- lower ovl_inode_lock (ovl_llseek) | |
b504c654 | 331 | */ |
b504c654 CX |
332 | if (skip_hole && data_pos < old_pos) { |
333 | data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA); | |
334 | if (data_pos > old_pos) { | |
335 | hole_len = data_pos - old_pos; | |
336 | len -= hole_len; | |
337 | old_pos = new_pos = data_pos; | |
338 | continue; | |
339 | } else if (data_pos == -ENXIO) { | |
340 | break; | |
341 | } else if (data_pos < 0) { | |
342 | skip_hole = false; | |
343 | } | |
344 | } | |
345 | ||
ca7ab482 AG |
346 | error = ovl_verify_area(old_pos, new_pos, this_len, len); |
347 | if (error) | |
348 | break; | |
349 | ||
e9be9d5e MS |
350 | bytes = do_splice_direct(old_file, &old_pos, |
351 | new_file, &new_pos, | |
352 | this_len, SPLICE_F_MOVE); | |
353 | if (bytes <= 0) { | |
354 | error = bytes; | |
355 | break; | |
356 | } | |
357 | WARN_ON(old_pos != new_pos); | |
358 | ||
359 | len -= bytes; | |
360 | } | |
7d6899fb AG |
361 | /* call fsync once, either now or later along with metadata */ |
362 | if (!error && ovl_should_sync(ofs) && datasync) | |
641089c1 | 363 | error = vfs_fsync(new_file, 0); |
e9be9d5e MS |
364 | out_fput: |
365 | fput(old_file); | |
366 | return error; | |
367 | } | |
368 | ||
5272eaf3 CB |
369 | static int ovl_set_size(struct ovl_fs *ofs, |
370 | struct dentry *upperdentry, struct kstat *stat) | |
0c288874 VG |
371 | { |
372 | struct iattr attr = { | |
373 | .ia_valid = ATTR_SIZE, | |
374 | .ia_size = stat->size, | |
375 | }; | |
376 | ||
a15506ea | 377 | return ovl_do_notify_change(ofs, upperdentry, &attr); |
0c288874 VG |
378 | } |
379 | ||
5272eaf3 CB |
380 | static int ovl_set_timestamps(struct ovl_fs *ofs, struct dentry *upperdentry, |
381 | struct kstat *stat) | |
e9be9d5e MS |
382 | { |
383 | struct iattr attr = { | |
384 | .ia_valid = | |
03dbab3b | 385 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_CTIME, |
e9be9d5e MS |
386 | .ia_atime = stat->atime, |
387 | .ia_mtime = stat->mtime, | |
388 | }; | |
389 | ||
a15506ea | 390 | return ovl_do_notify_change(ofs, upperdentry, &attr); |
e9be9d5e MS |
391 | } |
392 | ||
5272eaf3 CB |
393 | int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upperdentry, |
394 | struct kstat *stat) | |
e9be9d5e MS |
395 | { |
396 | int err = 0; | |
397 | ||
398 | if (!S_ISLNK(stat->mode)) { | |
399 | struct iattr attr = { | |
400 | .ia_valid = ATTR_MODE, | |
401 | .ia_mode = stat->mode, | |
402 | }; | |
a15506ea | 403 | err = ovl_do_notify_change(ofs, upperdentry, &attr); |
e9be9d5e MS |
404 | } |
405 | if (!err) { | |
406 | struct iattr attr = { | |
407 | .ia_valid = ATTR_UID | ATTR_GID, | |
b27c82e1 CB |
408 | .ia_vfsuid = VFSUIDT_INIT(stat->uid), |
409 | .ia_vfsgid = VFSGIDT_INIT(stat->gid), | |
e9be9d5e | 410 | }; |
a15506ea | 411 | err = ovl_do_notify_change(ofs, upperdentry, &attr); |
e9be9d5e MS |
412 | } |
413 | if (!err) | |
5272eaf3 | 414 | ovl_set_timestamps(ofs, upperdentry, stat); |
e9be9d5e MS |
415 | |
416 | return err; | |
e9be9d5e MS |
417 | } |
418 | ||
1cdb0cb6 PT |
419 | struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real, |
420 | bool is_upper) | |
3a1e819b AG |
421 | { |
422 | struct ovl_fh *fh; | |
ec7bbb53 | 423 | int fh_type, dwords; |
3a1e819b | 424 | int buflen = MAX_HANDLE_SZ; |
05122443 | 425 | uuid_t *uuid = &real->d_sb->s_uuid; |
ec7bbb53 | 426 | int err; |
3a1e819b | 427 | |
ec7bbb53 AG |
428 | /* Make sure the real fid stays 32bit aligned */ |
429 | BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4); | |
430 | BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255); | |
431 | ||
432 | fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL); | |
433 | if (!fh) | |
3a1e819b AG |
434 | return ERR_PTR(-ENOMEM); |
435 | ||
436 | /* | |
437 | * We encode a non-connectable file handle for non-dir, because we | |
438 | * only need to find the lower inode number and we don't want to pay | |
439 | * the price or reconnecting the dentry. | |
440 | */ | |
441 | dwords = buflen >> 2; | |
ec7bbb53 | 442 | fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0); |
3a1e819b AG |
443 | buflen = (dwords << 2); |
444 | ||
ec7bbb53 | 445 | err = -EIO; |
3a1e819b AG |
446 | if (WARN_ON(fh_type < 0) || |
447 | WARN_ON(buflen > MAX_HANDLE_SZ) || | |
448 | WARN_ON(fh_type == FILEID_INVALID)) | |
ec7bbb53 | 449 | goto out_err; |
3a1e819b | 450 | |
cbe7fba8 AG |
451 | fh->fb.version = OVL_FH_VERSION; |
452 | fh->fb.magic = OVL_FH_MAGIC; | |
453 | fh->fb.type = fh_type; | |
454 | fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN; | |
54fb347e AG |
455 | /* |
456 | * When we will want to decode an overlay dentry from this handle | |
457 | * and all layers are on the same fs, if we get a disconncted real | |
458 | * dentry when we decode fid, the only way to tell if we should assign | |
459 | * it to upperdentry or to lowerstack is by checking this flag. | |
460 | */ | |
461 | if (is_upper) | |
cbe7fba8 | 462 | fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER; |
ec7bbb53 | 463 | fh->fb.len = sizeof(fh->fb) + buflen; |
b0504bfe | 464 | if (ovl_origin_uuid(ofs)) |
5830fb6b | 465 | fh->fb.uuid = *uuid; |
3a1e819b | 466 | |
3a1e819b | 467 | return fh; |
ec7bbb53 AG |
468 | |
469 | out_err: | |
470 | kfree(fh); | |
471 | return ERR_PTR(err); | |
3a1e819b AG |
472 | } |
473 | ||
5b02bfc1 | 474 | struct ovl_fh *ovl_get_origin_fh(struct ovl_fs *ofs, struct dentry *origin) |
3a1e819b | 475 | { |
3a1e819b AG |
476 | /* |
477 | * When lower layer doesn't support export operations store a 'null' fh, | |
478 | * so we can use the overlay.origin xattr to distignuish between a copy | |
479 | * up and a pure upper inode. | |
480 | */ | |
5b02bfc1 AG |
481 | if (!ovl_can_decode_fh(origin->d_sb)) |
482 | return NULL; | |
483 | ||
484 | return ovl_encode_real_fh(ofs, origin, false); | |
485 | } | |
486 | ||
487 | int ovl_set_origin_fh(struct ovl_fs *ofs, const struct ovl_fh *fh, | |
488 | struct dentry *upper) | |
489 | { | |
490 | int err; | |
3a1e819b | 491 | |
6266d465 MS |
492 | /* |
493 | * Do not fail when upper doesn't support xattrs. | |
494 | */ | |
a0c236b1 | 495 | err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf, |
cbe7fba8 | 496 | fh ? fh->fb.len : 0, 0); |
3a1e819b | 497 | |
6939f977 MS |
498 | /* Ignore -EPERM from setting "user.*" on symlink/special */ |
499 | return err == -EPERM ? 0 : err; | |
3a1e819b AG |
500 | } |
501 | ||
016b720f | 502 | /* Store file handle of @upper dir in @index dir entry */ |
610afc0b MS |
503 | static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper, |
504 | struct dentry *index) | |
016b720f AG |
505 | { |
506 | const struct ovl_fh *fh; | |
507 | int err; | |
508 | ||
1cdb0cb6 | 509 | fh = ovl_encode_real_fh(ofs, upper, true); |
016b720f AG |
510 | if (IS_ERR(fh)) |
511 | return PTR_ERR(fh); | |
512 | ||
c914c0e2 | 513 | err = ovl_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len); |
016b720f AG |
514 | |
515 | kfree(fh); | |
516 | return err; | |
517 | } | |
518 | ||
519 | /* | |
520 | * Create and install index entry. | |
521 | * | |
522 | * Caller must hold i_mutex on indexdir. | |
523 | */ | |
5b02bfc1 | 524 | static int ovl_create_index(struct dentry *dentry, const struct ovl_fh *fh, |
016b720f AG |
525 | struct dentry *upper) |
526 | { | |
1cdb0cb6 | 527 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb); |
016b720f AG |
528 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); |
529 | struct inode *dir = d_inode(indexdir); | |
530 | struct dentry *index = NULL; | |
531 | struct dentry *temp = NULL; | |
532 | struct qstr name = { }; | |
533 | int err; | |
534 | ||
535 | /* | |
536 | * For now this is only used for creating index entry for directories, | |
537 | * because non-dir are copied up directly to index and then hardlinked | |
538 | * to upper dir. | |
539 | * | |
540 | * TODO: implement create index for non-dir, so we can call it when | |
541 | * encoding file handle for non-dir in case index does not exist. | |
542 | */ | |
543 | if (WARN_ON(!d_is_dir(dentry))) | |
544 | return -EIO; | |
545 | ||
546 | /* Directory not expected to be indexed before copy up */ | |
547 | if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry)))) | |
548 | return -EIO; | |
549 | ||
5b02bfc1 | 550 | err = ovl_get_index_name_fh(fh, &name); |
016b720f AG |
551 | if (err) |
552 | return err; | |
553 | ||
576bb263 | 554 | temp = ovl_create_temp(ofs, indexdir, OVL_CATTR(S_IFDIR | 0)); |
b148cba4 | 555 | err = PTR_ERR(temp); |
016b720f | 556 | if (IS_ERR(temp)) |
b148cba4 | 557 | goto free_name; |
016b720f | 558 | |
1cdb0cb6 | 559 | err = ovl_set_upper_fh(ofs, upper, temp); |
016b720f | 560 | if (err) |
b148cba4 | 561 | goto out; |
016b720f | 562 | |
22f289ce | 563 | index = ovl_lookup_upper(ofs, name.name, indexdir, name.len); |
016b720f AG |
564 | if (IS_ERR(index)) { |
565 | err = PTR_ERR(index); | |
566 | } else { | |
576bb263 | 567 | err = ovl_do_rename(ofs, dir, temp, dir, index, 0); |
016b720f AG |
568 | dput(index); |
569 | } | |
016b720f | 570 | out: |
b148cba4 | 571 | if (err) |
576bb263 | 572 | ovl_cleanup(ofs, dir, temp); |
016b720f | 573 | dput(temp); |
b148cba4 | 574 | free_name: |
016b720f AG |
575 | kfree(name.name); |
576 | return err; | |
016b720f AG |
577 | } |
578 | ||
f4439de1 AG |
579 | struct ovl_copy_up_ctx { |
580 | struct dentry *parent; | |
581 | struct dentry *dentry; | |
582 | struct path lowerpath; | |
583 | struct kstat stat; | |
584 | struct kstat pstat; | |
585 | const char *link; | |
586 | struct dentry *destdir; | |
587 | struct qstr destname; | |
588 | struct dentry *workdir; | |
5b02bfc1 | 589 | const struct ovl_fh *origin_fh; |
f4439de1 | 590 | bool origin; |
016b720f | 591 | bool indexed; |
44d5bf10 | 592 | bool metacopy; |
0c71faf5 | 593 | bool metacopy_digest; |
7d6899fb | 594 | bool metadata_fsync; |
f4439de1 AG |
595 | }; |
596 | ||
597 | static int ovl_link_up(struct ovl_copy_up_ctx *c) | |
59be0971 AG |
598 | { |
599 | int err; | |
600 | struct dentry *upper; | |
f4439de1 | 601 | struct dentry *upperdir = ovl_dentry_upper(c->parent); |
576bb263 | 602 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
59be0971 AG |
603 | struct inode *udir = d_inode(upperdir); |
604 | ||
c63e56a4 AG |
605 | ovl_start_write(c->dentry); |
606 | ||
f4439de1 AG |
607 | /* Mark parent "impure" because it may now contain non-pure upper */ |
608 | err = ovl_set_impure(c->parent, upperdir); | |
609 | if (err) | |
c63e56a4 | 610 | goto out; |
f4439de1 AG |
611 | |
612 | err = ovl_set_nlink_lower(c->dentry); | |
5f8415d6 | 613 | if (err) |
c63e56a4 | 614 | goto out; |
5f8415d6 | 615 | |
59be0971 | 616 | inode_lock_nested(udir, I_MUTEX_PARENT); |
22f289ce CB |
617 | upper = ovl_lookup_upper(ofs, c->dentry->d_name.name, upperdir, |
618 | c->dentry->d_name.len); | |
59be0971 AG |
619 | err = PTR_ERR(upper); |
620 | if (!IS_ERR(upper)) { | |
576bb263 | 621 | err = ovl_do_link(ofs, ovl_dentry_upper(c->dentry), udir, upper); |
59be0971 AG |
622 | dput(upper); |
623 | ||
f4439de1 AG |
624 | if (!err) { |
625 | /* Restore timestamps on parent (best effort) */ | |
5272eaf3 | 626 | ovl_set_timestamps(ofs, upperdir, &c->pstat); |
f4439de1 | 627 | ovl_dentry_set_upper_alias(c->dentry); |
b07d5cc9 | 628 | ovl_dentry_update_reval(c->dentry, upper); |
f4439de1 | 629 | } |
59be0971 AG |
630 | } |
631 | inode_unlock(udir); | |
aa3ff3c1 | 632 | if (err) |
c63e56a4 | 633 | goto out; |
aa3ff3c1 AG |
634 | |
635 | err = ovl_set_nlink_upper(c->dentry); | |
59be0971 | 636 | |
c63e56a4 AG |
637 | out: |
638 | ovl_end_write(c->dentry); | |
59be0971 AG |
639 | return err; |
640 | } | |
641 | ||
2b1a7746 | 642 | static int ovl_copy_up_data(struct ovl_copy_up_ctx *c, const struct path *temp) |
7d90b853 | 643 | { |
c86243b0 | 644 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
2b1a7746 | 645 | struct file *new_file; |
7d90b853 MS |
646 | int err; |
647 | ||
2b1a7746 MS |
648 | if (!S_ISREG(c->stat.mode) || c->metacopy || !c->stat.size) |
649 | return 0; | |
72db8211 | 650 | |
2b1a7746 MS |
651 | new_file = ovl_path_open(temp, O_LARGEFILE | O_WRONLY); |
652 | if (IS_ERR(new_file)) | |
653 | return PTR_ERR(new_file); | |
72db8211 | 654 | |
7d6899fb AG |
655 | err = ovl_copy_up_file(ofs, c->dentry, new_file, c->stat.size, |
656 | !c->metadata_fsync); | |
2b1a7746 MS |
657 | fput(new_file); |
658 | ||
659 | return err; | |
660 | } | |
661 | ||
662 | static int ovl_copy_up_metadata(struct ovl_copy_up_ctx *c, struct dentry *temp) | |
663 | { | |
664 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); | |
665 | struct inode *inode = d_inode(c->dentry); | |
666 | struct path upperpath = { .mnt = ovl_upper_mnt(ofs), .dentry = temp }; | |
667 | int err; | |
5f32879e | 668 | |
dad7017a | 669 | err = ovl_copy_xattr(c->dentry->d_sb, &c->lowerpath, temp); |
e9be9d5e | 670 | if (err) |
02209d10 | 671 | return err; |
e9be9d5e | 672 | |
ab048302 AG |
673 | if (inode->i_flags & OVL_COPY_I_FLAGS_MASK && |
674 | (S_ISREG(c->stat.mode) || S_ISDIR(c->stat.mode))) { | |
72db8211 AG |
675 | /* |
676 | * Copy the fileattr inode flags that are the source of already | |
677 | * copied i_flags | |
678 | */ | |
096a218a | 679 | err = ovl_copy_fileattr(inode, &c->lowerpath, &upperpath); |
72db8211 AG |
680 | if (err) |
681 | return err; | |
682 | } | |
683 | ||
3a1e819b AG |
684 | /* |
685 | * Store identifier of lower inode in upper inode xattr to | |
686 | * allow lookup of the copy up origin inode. | |
fbaf94ee MS |
687 | * |
688 | * Don't set origin when we are breaking the association with a lower | |
689 | * hard link. | |
3a1e819b | 690 | */ |
59be0971 | 691 | if (c->origin) { |
5b02bfc1 | 692 | err = ovl_set_origin_fh(ofs, c->origin_fh, temp); |
fbaf94ee | 693 | if (err) |
02209d10 | 694 | return err; |
fbaf94ee | 695 | } |
3a1e819b | 696 | |
0c288874 | 697 | if (c->metacopy) { |
0c71faf5 AL |
698 | struct path lowerdatapath; |
699 | struct ovl_metacopy metacopy_data = OVL_METACOPY_INIT; | |
700 | ||
701 | ovl_path_lowerdata(c->dentry, &lowerdatapath); | |
702 | if (WARN_ON_ONCE(lowerdatapath.dentry == NULL)) | |
703 | return -EIO; | |
704 | err = ovl_get_verity_digest(ofs, &lowerdatapath, &metacopy_data); | |
705 | if (err) | |
706 | return err; | |
707 | ||
708 | if (metacopy_data.digest_algo) | |
709 | c->metacopy_digest = true; | |
710 | ||
711 | err = ovl_set_metacopy_xattr(ofs, temp, &metacopy_data); | |
0c288874 VG |
712 | if (err) |
713 | return err; | |
714 | } | |
715 | ||
bd64e575 | 716 | inode_lock(temp->d_inode); |
b504c654 | 717 | if (S_ISREG(c->stat.mode)) |
5272eaf3 | 718 | err = ovl_set_size(ofs, temp, &c->stat); |
0c288874 | 719 | if (!err) |
5272eaf3 | 720 | err = ovl_set_attr(ofs, temp, &c->stat); |
bd64e575 VG |
721 | inode_unlock(temp->d_inode); |
722 | ||
7d6899fb AG |
723 | /* fsync metadata before moving it into upper dir */ |
724 | if (!err && ovl_should_sync(ofs) && c->metadata_fsync) | |
725 | err = ovl_sync_file(&upperpath); | |
726 | ||
bd64e575 | 727 | return err; |
02209d10 AG |
728 | } |
729 | ||
6b52243f MS |
730 | struct ovl_cu_creds { |
731 | const struct cred *old; | |
732 | struct cred *new; | |
733 | }; | |
734 | ||
735 | static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc) | |
b10cdcdc AG |
736 | { |
737 | int err; | |
b10cdcdc | 738 | |
6b52243f MS |
739 | cc->old = cc->new = NULL; |
740 | err = security_inode_copy_up(dentry, &cc->new); | |
b10cdcdc | 741 | if (err < 0) |
6b52243f | 742 | return err; |
b10cdcdc | 743 | |
6b52243f MS |
744 | if (cc->new) |
745 | cc->old = override_creds(cc->new); | |
b10cdcdc | 746 | |
6b52243f | 747 | return 0; |
b10cdcdc AG |
748 | } |
749 | ||
6b52243f | 750 | static void ovl_revert_cu_creds(struct ovl_cu_creds *cc) |
b10cdcdc | 751 | { |
6b52243f MS |
752 | if (cc->new) { |
753 | revert_creds(cc->old); | |
754 | put_cred(cc->new); | |
755 | } | |
b10cdcdc AG |
756 | } |
757 | ||
758 | /* | |
759 | * Copyup using workdir to prepare temp file. Used when copying up directories, | |
760 | * special files or when upper fs doesn't support O_TMPFILE. | |
761 | */ | |
762 | static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c) | |
02209d10 | 763 | { |
576bb263 | 764 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
b79e05aa | 765 | struct inode *inode; |
6b52243f | 766 | struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir); |
2b1a7746 | 767 | struct path path = { .mnt = ovl_upper_mnt(ofs) }; |
a8b00268 | 768 | struct dentry *temp, *upper, *trap; |
6b52243f | 769 | struct ovl_cu_creds cc; |
02209d10 | 770 | int err; |
6b52243f MS |
771 | struct ovl_cattr cattr = { |
772 | /* Can't properly set mode on creation because of the umask */ | |
773 | .mode = c->stat.mode & S_IFMT, | |
774 | .rdev = c->stat.rdev, | |
775 | .link = c->link | |
776 | }; | |
02209d10 | 777 | |
6b52243f MS |
778 | err = ovl_prep_cu_creds(c->dentry, &cc); |
779 | if (err) | |
c63e56a4 | 780 | return err; |
6b52243f | 781 | |
c63e56a4 AG |
782 | ovl_start_write(c->dentry); |
783 | inode_lock(wdir); | |
576bb263 | 784 | temp = ovl_create_temp(ofs, c->workdir, &cattr); |
c63e56a4 AG |
785 | inode_unlock(wdir); |
786 | ovl_end_write(c->dentry); | |
6b52243f MS |
787 | ovl_revert_cu_creds(&cc); |
788 | ||
b148cba4 | 789 | if (IS_ERR(temp)) |
c63e56a4 | 790 | return PTR_ERR(temp); |
02209d10 | 791 | |
2b1a7746 MS |
792 | /* |
793 | * Copy up data first and then xattrs. Writing data after | |
794 | * xattrs will remove security.capability xattr automatically. | |
795 | */ | |
796 | path.dentry = temp; | |
797 | err = ovl_copy_up_data(c, &path); | |
c63e56a4 | 798 | /* |
413ba910 | 799 | * We cannot hold lock_rename() throughout this helper, because of |
c63e56a4 AG |
800 | * lock ordering with sb_writers, which shouldn't be held when calling |
801 | * ovl_copy_up_data(), so lock workdir and destdir and make sure that | |
802 | * temp wasn't moved before copy up completion or cleanup. | |
c63e56a4 AG |
803 | */ |
804 | ovl_start_write(c->dentry); | |
a8b00268 AV |
805 | trap = lock_rename(c->workdir, c->destdir); |
806 | if (trap || temp->d_parent != c->workdir) { | |
413ba910 AG |
807 | /* temp or workdir moved underneath us? abort without cleanup */ |
808 | dput(temp); | |
c63e56a4 | 809 | err = -EIO; |
a8b00268 AV |
810 | if (IS_ERR(trap)) |
811 | goto out; | |
c63e56a4 AG |
812 | goto unlock; |
813 | } else if (err) { | |
2b1a7746 | 814 | goto cleanup; |
c63e56a4 | 815 | } |
2b1a7746 MS |
816 | |
817 | err = ovl_copy_up_metadata(c, temp); | |
02209d10 | 818 | if (err) |
b10cdcdc | 819 | goto cleanup; |
02209d10 | 820 | |
016b720f | 821 | if (S_ISDIR(c->stat.mode) && c->indexed) { |
5b02bfc1 | 822 | err = ovl_create_index(c->dentry, c->origin_fh, temp); |
016b720f | 823 | if (err) |
b10cdcdc | 824 | goto cleanup; |
016b720f AG |
825 | } |
826 | ||
22f289ce CB |
827 | upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir, |
828 | c->destname.len); | |
6b52243f MS |
829 | err = PTR_ERR(upper); |
830 | if (IS_ERR(upper)) | |
831 | goto cleanup; | |
832 | ||
576bb263 | 833 | err = ovl_do_rename(ofs, wdir, temp, udir, upper, 0); |
6b52243f | 834 | dput(upper); |
e9be9d5e | 835 | if (err) |
b10cdcdc | 836 | goto cleanup; |
e9be9d5e | 837 | |
b79e05aa | 838 | inode = d_inode(c->dentry); |
0c71faf5 AL |
839 | if (c->metacopy_digest) |
840 | ovl_set_flag(OVL_HAS_DIGEST, inode); | |
841 | else | |
842 | ovl_clear_flag(OVL_HAS_DIGEST, inode); | |
843 | ovl_clear_flag(OVL_VERIFIED_DIGEST, inode); | |
844 | ||
845 | if (!c->metacopy) | |
846 | ovl_set_upperdata(inode); | |
6b52243f | 847 | ovl_inode_update(inode, temp); |
b79e05aa AG |
848 | if (S_ISDIR(inode->i_mode)) |
849 | ovl_set_flag(OVL_WHITEOUTS, inode); | |
b10cdcdc AG |
850 | unlock: |
851 | unlock_rename(c->workdir, c->destdir); | |
a8b00268 | 852 | out: |
c63e56a4 | 853 | ovl_end_write(c->dentry); |
b10cdcdc AG |
854 | |
855 | return err; | |
856 | ||
857 | cleanup: | |
576bb263 | 858 | ovl_cleanup(ofs, wdir, temp); |
6b52243f MS |
859 | dput(temp); |
860 | goto unlock; | |
b10cdcdc AG |
861 | } |
862 | ||
6b52243f MS |
863 | /* Copyup using O_TMPFILE which does not require cross dir locking */ |
864 | static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c) | |
b10cdcdc | 865 | { |
576bb263 | 866 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
6b52243f MS |
867 | struct inode *udir = d_inode(c->destdir); |
868 | struct dentry *temp, *upper; | |
2b1a7746 | 869 | struct file *tmpfile; |
6b52243f | 870 | struct ovl_cu_creds cc; |
b10cdcdc | 871 | int err; |
b10cdcdc | 872 | |
6b52243f MS |
873 | err = ovl_prep_cu_creds(c->dentry, &cc); |
874 | if (err) | |
875 | return err; | |
b10cdcdc | 876 | |
c63e56a4 | 877 | ovl_start_write(c->dentry); |
2b1a7746 | 878 | tmpfile = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode); |
c63e56a4 | 879 | ovl_end_write(c->dentry); |
6b52243f | 880 | ovl_revert_cu_creds(&cc); |
2b1a7746 MS |
881 | if (IS_ERR(tmpfile)) |
882 | return PTR_ERR(tmpfile); | |
b10cdcdc | 883 | |
2b1a7746 MS |
884 | temp = tmpfile->f_path.dentry; |
885 | if (!c->metacopy && c->stat.size) { | |
7d6899fb AG |
886 | err = ovl_copy_up_file(ofs, c->dentry, tmpfile, c->stat.size, |
887 | !c->metadata_fsync); | |
2b1a7746 | 888 | if (err) |
baabaa50 | 889 | goto out_fput; |
2b1a7746 MS |
890 | } |
891 | ||
c63e56a4 AG |
892 | ovl_start_write(c->dentry); |
893 | ||
2b1a7746 | 894 | err = ovl_copy_up_metadata(c, temp); |
6b52243f | 895 | if (err) |
c63e56a4 | 896 | goto out; |
b10cdcdc AG |
897 | |
898 | inode_lock_nested(udir, I_MUTEX_PARENT); | |
899 | ||
22f289ce CB |
900 | upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir, |
901 | c->destname.len); | |
b10cdcdc | 902 | err = PTR_ERR(upper); |
6b52243f | 903 | if (!IS_ERR(upper)) { |
576bb263 | 904 | err = ovl_do_link(ofs, temp, udir, upper); |
6b52243f MS |
905 | dput(upper); |
906 | } | |
b10cdcdc AG |
907 | inode_unlock(udir); |
908 | ||
b10cdcdc | 909 | if (err) |
c63e56a4 | 910 | goto out; |
b10cdcdc | 911 | |
0c71faf5 AL |
912 | if (c->metacopy_digest) |
913 | ovl_set_flag(OVL_HAS_DIGEST, d_inode(c->dentry)); | |
914 | else | |
915 | ovl_clear_flag(OVL_HAS_DIGEST, d_inode(c->dentry)); | |
916 | ovl_clear_flag(OVL_VERIFIED_DIGEST, d_inode(c->dentry)); | |
917 | ||
b10cdcdc AG |
918 | if (!c->metacopy) |
919 | ovl_set_upperdata(d_inode(c->dentry)); | |
2b1a7746 | 920 | ovl_inode_update(d_inode(c->dentry), dget(temp)); |
b79e05aa | 921 | |
c63e56a4 AG |
922 | out: |
923 | ovl_end_write(c->dentry); | |
2b1a7746 MS |
924 | out_fput: |
925 | fput(tmpfile); | |
e9be9d5e | 926 | return err; |
e9be9d5e MS |
927 | } |
928 | ||
929 | /* | |
930 | * Copy up a single dentry | |
931 | * | |
a6c60655 MS |
932 | * All renames start with copy up of source if necessary. The actual |
933 | * rename will only proceed once the copy up was successful. Copy up uses | |
934 | * upper parent i_mutex for exclusion. Since rename can change d_parent it | |
935 | * is possible that the copy up will lock the old parent. At that point | |
936 | * the file will have already been copied up anyway. | |
e9be9d5e | 937 | */ |
a6fb235a | 938 | static int ovl_do_copy_up(struct ovl_copy_up_ctx *c) |
e9be9d5e | 939 | { |
e9be9d5e | 940 | int err; |
1cdb0cb6 | 941 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
5b02bfc1 AG |
942 | struct dentry *origin = c->lowerpath.dentry; |
943 | struct ovl_fh *fh = NULL; | |
016b720f | 944 | bool to_index = false; |
59be0971 | 945 | |
016b720f AG |
946 | /* |
947 | * Indexed non-dir is copied up directly to the index entry and then | |
948 | * hardlinked to upper dir. Indexed dir is copied up to indexdir, | |
949 | * then index entry is created and then copied up dir installed. | |
950 | * Copying dir up to indexdir instead of workdir simplifies locking. | |
951 | */ | |
952 | if (ovl_need_index(c->dentry)) { | |
953 | c->indexed = true; | |
954 | if (S_ISDIR(c->stat.mode)) | |
955 | c->workdir = ovl_indexdir(c->dentry->d_sb); | |
956 | else | |
957 | to_index = true; | |
958 | } | |
959 | ||
5b02bfc1 AG |
960 | if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index) { |
961 | fh = ovl_get_origin_fh(ofs, origin); | |
962 | if (IS_ERR(fh)) | |
963 | return PTR_ERR(fh); | |
964 | ||
965 | /* origin_fh may be NULL */ | |
966 | c->origin_fh = fh; | |
59be0971 | 967 | c->origin = true; |
5b02bfc1 | 968 | } |
59be0971 | 969 | |
016b720f | 970 | if (to_index) { |
59be0971 | 971 | c->destdir = ovl_indexdir(c->dentry->d_sb); |
5b02bfc1 | 972 | err = ovl_get_index_name(ofs, origin, &c->destname); |
59be0971 | 973 | if (err) |
5b02bfc1 | 974 | goto out_free_fh; |
aa3ff3c1 AG |
975 | } else if (WARN_ON(!c->parent)) { |
976 | /* Disconnected dentry must be copied up to index dir */ | |
5b02bfc1 AG |
977 | err = -EIO; |
978 | goto out_free_fh; | |
59be0971 | 979 | } else { |
2c3ef4f8 AG |
980 | /* |
981 | * c->dentry->d_name is stabilzed by ovl_copy_up_start(), | |
982 | * because if we got here, it means that c->dentry has no upper | |
983 | * alias and changing ->d_name means going through ovl_rename() | |
984 | * that will call ovl_copy_up() on source and target dentry. | |
985 | */ | |
986 | c->destname = c->dentry->d_name; | |
59be0971 AG |
987 | /* |
988 | * Mark parent "impure" because it may now contain non-pure | |
989 | * upper | |
990 | */ | |
c63e56a4 | 991 | ovl_start_write(c->dentry); |
59be0971 | 992 | err = ovl_set_impure(c->parent, c->destdir); |
c63e56a4 | 993 | ovl_end_write(c->dentry); |
59be0971 | 994 | if (err) |
5b02bfc1 | 995 | goto out_free_fh; |
59be0971 | 996 | } |
e9be9d5e | 997 | |
01ad3eb8 | 998 | /* Should we copyup with O_TMPFILE or with workdir? */ |
b10cdcdc AG |
999 | if (S_ISREG(c->stat.mode) && ofs->tmpfile) |
1000 | err = ovl_copy_up_tmpfile(c); | |
1001 | else | |
1002 | err = ovl_copy_up_workdir(c); | |
aa3ff3c1 AG |
1003 | if (err) |
1004 | goto out; | |
1005 | ||
1006 | if (c->indexed) | |
016b720f AG |
1007 | ovl_set_flag(OVL_INDEX, d_inode(c->dentry)); |
1008 | ||
c63e56a4 | 1009 | ovl_start_write(c->dentry); |
016b720f | 1010 | if (to_index) { |
aa3ff3c1 AG |
1011 | /* Initialize nlink for copy up of disconnected dentry */ |
1012 | err = ovl_set_nlink_upper(c->dentry); | |
1013 | } else { | |
59be0971 AG |
1014 | struct inode *udir = d_inode(c->destdir); |
1015 | ||
1016 | /* Restore timestamps on parent (best effort) */ | |
1017 | inode_lock(udir); | |
5272eaf3 | 1018 | ovl_set_timestamps(ofs, c->destdir, &c->pstat); |
59be0971 AG |
1019 | inode_unlock(udir); |
1020 | ||
1021 | ovl_dentry_set_upper_alias(c->dentry); | |
b07d5cc9 | 1022 | ovl_dentry_update_reval(c->dentry, ovl_dentry_upper(c->dentry)); |
e9be9d5e | 1023 | } |
c63e56a4 | 1024 | ovl_end_write(c->dentry); |
e9be9d5e | 1025 | |
aa3ff3c1 AG |
1026 | out: |
1027 | if (to_index) | |
1028 | kfree(c->destname.name); | |
5b02bfc1 AG |
1029 | out_free_fh: |
1030 | kfree(fh); | |
a6fb235a MS |
1031 | return err; |
1032 | } | |
1033 | ||
44d5bf10 VG |
1034 | static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode, |
1035 | int flags) | |
1036 | { | |
f01d0889 | 1037 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb); |
44d5bf10 | 1038 | |
44d5bf10 VG |
1039 | if (!ofs->config.metacopy) |
1040 | return false; | |
1041 | ||
1042 | if (!S_ISREG(mode)) | |
1043 | return false; | |
1044 | ||
1045 | if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC))) | |
1046 | return false; | |
1047 | ||
0c71faf5 AL |
1048 | /* Fall back to full copy if no fsverity on source data and we require verity */ |
1049 | if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) { | |
1050 | struct path lowerdata; | |
1051 | ||
1052 | ovl_path_lowerdata(dentry, &lowerdata); | |
1053 | ||
1054 | if (WARN_ON_ONCE(lowerdata.dentry == NULL) || | |
1055 | ovl_ensure_verity_loaded(&lowerdata) || | |
1056 | !fsverity_active(d_inode(lowerdata.dentry))) { | |
1057 | return false; | |
1058 | } | |
1059 | } | |
1060 | ||
44d5bf10 VG |
1061 | return true; |
1062 | } | |
1063 | ||
2d343087 | 1064 | static ssize_t ovl_getxattr_value(const struct path *path, char *name, char **value) |
fee0f298 MS |
1065 | { |
1066 | ssize_t res; | |
de7a52c9 | 1067 | char *buf; |
fee0f298 | 1068 | |
dad7017a | 1069 | res = ovl_do_getxattr(path, name, NULL, 0); |
de7a52c9 MS |
1070 | if (res == -ENODATA || res == -EOPNOTSUPP) |
1071 | res = 0; | |
fee0f298 | 1072 | |
de7a52c9 MS |
1073 | if (res > 0) { |
1074 | buf = kzalloc(res, GFP_KERNEL); | |
fee0f298 MS |
1075 | if (!buf) |
1076 | return -ENOMEM; | |
1077 | ||
dad7017a | 1078 | res = ovl_do_getxattr(path, name, buf, res); |
fee0f298 | 1079 | if (res < 0) |
de7a52c9 MS |
1080 | kfree(buf); |
1081 | else | |
1082 | *value = buf; | |
fee0f298 | 1083 | } |
fee0f298 MS |
1084 | return res; |
1085 | } | |
1086 | ||
0c288874 VG |
1087 | /* Copy up data of an inode which was copied up metadata only in the past. */ |
1088 | static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c) | |
1089 | { | |
c86243b0 | 1090 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
2b1a7746 | 1091 | struct path upperpath; |
0c288874 | 1092 | int err; |
993a0b2a | 1093 | char *capability = NULL; |
3f649ab7 | 1094 | ssize_t cap_size; |
0c288874 VG |
1095 | |
1096 | ovl_path_upper(c->dentry, &upperpath); | |
1097 | if (WARN_ON(upperpath.dentry == NULL)) | |
1098 | return -EIO; | |
1099 | ||
993a0b2a | 1100 | if (c->stat.size) { |
dad7017a CB |
1101 | err = cap_size = ovl_getxattr_value(&upperpath, XATTR_NAME_CAPS, |
1102 | &capability); | |
de7a52c9 | 1103 | if (cap_size < 0) |
993a0b2a VG |
1104 | goto out; |
1105 | } | |
1106 | ||
2b1a7746 | 1107 | err = ovl_copy_up_data(c, &upperpath); |
0c288874 | 1108 | if (err) |
993a0b2a VG |
1109 | goto out_free; |
1110 | ||
1111 | /* | |
1112 | * Writing to upper file will clear security.capability xattr. We | |
1113 | * don't want that to happen for normal copy-up operation. | |
1114 | */ | |
c63e56a4 | 1115 | ovl_start_write(c->dentry); |
993a0b2a | 1116 | if (capability) { |
c914c0e2 AG |
1117 | err = ovl_do_setxattr(ofs, upperpath.dentry, XATTR_NAME_CAPS, |
1118 | capability, cap_size, 0); | |
993a0b2a | 1119 | } |
c63e56a4 AG |
1120 | if (!err) { |
1121 | err = ovl_removexattr(ofs, upperpath.dentry, | |
1122 | OVL_XATTR_METACOPY); | |
1123 | } | |
1124 | ovl_end_write(c->dentry); | |
0c288874 | 1125 | if (err) |
993a0b2a | 1126 | goto out_free; |
0c288874 | 1127 | |
0c71faf5 AL |
1128 | ovl_clear_flag(OVL_HAS_DIGEST, d_inode(c->dentry)); |
1129 | ovl_clear_flag(OVL_VERIFIED_DIGEST, d_inode(c->dentry)); | |
0c288874 | 1130 | ovl_set_upperdata(d_inode(c->dentry)); |
993a0b2a VG |
1131 | out_free: |
1132 | kfree(capability); | |
1133 | out: | |
0c288874 VG |
1134 | return err; |
1135 | } | |
1136 | ||
a6fb235a MS |
1137 | static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
1138 | int flags) | |
1139 | { | |
1140 | int err; | |
1141 | DEFINE_DELAYED_CALL(done); | |
1142 | struct path parentpath; | |
1143 | struct ovl_copy_up_ctx ctx = { | |
1144 | .parent = parent, | |
1145 | .dentry = dentry, | |
1146 | .workdir = ovl_workdir(dentry), | |
1147 | }; | |
1148 | ||
1149 | if (WARN_ON(!ctx.workdir)) | |
1150 | return -EROFS; | |
1151 | ||
1152 | ovl_path_lower(dentry, &ctx.lowerpath); | |
1153 | err = vfs_getattr(&ctx.lowerpath, &ctx.stat, | |
1154 | STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); | |
1155 | if (err) | |
1156 | return err; | |
1157 | ||
4f11ada1 MS |
1158 | if (!kuid_has_mapping(current_user_ns(), ctx.stat.uid) || |
1159 | !kgid_has_mapping(current_user_ns(), ctx.stat.gid)) | |
1160 | return -EOVERFLOW; | |
1161 | ||
7d6899fb AG |
1162 | /* |
1163 | * With metacopy disabled, we fsync after final metadata copyup, for | |
1164 | * both regular files and directories to get atomic copyup semantics | |
1165 | * on filesystems that do not use strict metadata ordering (e.g. ubifs). | |
1166 | * | |
1167 | * With metacopy enabled we want to avoid fsync on all meta copyup | |
1168 | * that will hurt performance of workloads such as chown -R, so we | |
1169 | * only fsync on data copyup as legacy behavior. | |
1170 | */ | |
1171 | ctx.metadata_fsync = !OVL_FS(dentry->d_sb)->config.metacopy && | |
1172 | (S_ISREG(ctx.stat.mode) || S_ISDIR(ctx.stat.mode)); | |
44d5bf10 VG |
1173 | ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags); |
1174 | ||
aa3ff3c1 AG |
1175 | if (parent) { |
1176 | ovl_path_upper(parent, &parentpath); | |
1177 | ctx.destdir = parentpath.dentry; | |
a6fb235a | 1178 | |
aa3ff3c1 AG |
1179 | err = vfs_getattr(&parentpath, &ctx.pstat, |
1180 | STATX_ATIME | STATX_MTIME, | |
1181 | AT_STATX_SYNC_AS_STAT); | |
1182 | if (err) | |
1183 | return err; | |
1184 | } | |
a6fb235a MS |
1185 | |
1186 | /* maybe truncate regular file. this has no effect on dirs */ | |
1187 | if (flags & O_TRUNC) | |
1188 | ctx.stat.size = 0; | |
1189 | ||
1190 | if (S_ISLNK(ctx.stat.mode)) { | |
1191 | ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done); | |
1192 | if (IS_ERR(ctx.link)) | |
1193 | return PTR_ERR(ctx.link); | |
1194 | } | |
a6fb235a | 1195 | |
0c288874 | 1196 | err = ovl_copy_up_start(dentry, flags); |
fd210b7d MS |
1197 | /* err < 0: interrupted, err > 0: raced with another copy-up */ |
1198 | if (unlikely(err)) { | |
1199 | if (err > 0) | |
1200 | err = 0; | |
1201 | } else { | |
59be0971 AG |
1202 | if (!ovl_dentry_upper(dentry)) |
1203 | err = ovl_do_copy_up(&ctx); | |
aa3ff3c1 | 1204 | if (!err && parent && !ovl_dentry_has_upper_alias(dentry)) |
f4439de1 | 1205 | err = ovl_link_up(&ctx); |
0c288874 VG |
1206 | if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags)) |
1207 | err = ovl_copy_up_meta_inode_data(&ctx); | |
fd210b7d MS |
1208 | ovl_copy_up_end(dentry); |
1209 | } | |
7764235b | 1210 | do_delayed_call(&done); |
e9be9d5e MS |
1211 | |
1212 | return err; | |
1213 | } | |
1214 | ||
5ac8e802 | 1215 | static int ovl_copy_up_flags(struct dentry *dentry, int flags) |
e9be9d5e | 1216 | { |
8eac98b8 | 1217 | int err = 0; |
7b279bbf | 1218 | const struct cred *old_cred; |
aa3ff3c1 AG |
1219 | bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED); |
1220 | ||
1221 | /* | |
1222 | * With NFS export, copy up can get called for a disconnected non-dir. | |
1223 | * In this case, we will copy up lower inode to index dir without | |
1224 | * linking it to upper dir. | |
1225 | */ | |
1226 | if (WARN_ON(disconnected && d_is_dir(dentry))) | |
1227 | return -EIO; | |
e9be9d5e | 1228 | |
42dd69ae AG |
1229 | /* |
1230 | * We may not need lowerdata if we are only doing metacopy up, but it is | |
1231 | * not very important to optimize this case, so do lazy lowerdata lookup | |
1232 | * before any copy up, so we can do it before taking ovl_inode_lock(). | |
1233 | */ | |
184996e9 | 1234 | err = ovl_verify_lowerdata(dentry); |
42dd69ae AG |
1235 | if (err) |
1236 | return err; | |
1237 | ||
7b279bbf | 1238 | old_cred = ovl_override_creds(dentry->d_sb); |
e9be9d5e MS |
1239 | while (!err) { |
1240 | struct dentry *next; | |
aa3ff3c1 | 1241 | struct dentry *parent = NULL; |
e9be9d5e | 1242 | |
0c288874 | 1243 | if (ovl_already_copied_up(dentry, flags)) |
e9be9d5e MS |
1244 | break; |
1245 | ||
1246 | next = dget(dentry); | |
1247 | /* find the topmost dentry not yet copied up */ | |
aa3ff3c1 | 1248 | for (; !disconnected;) { |
e9be9d5e MS |
1249 | parent = dget_parent(next); |
1250 | ||
59be0971 | 1251 | if (ovl_dentry_upper(parent)) |
e9be9d5e MS |
1252 | break; |
1253 | ||
1254 | dput(next); | |
1255 | next = parent; | |
1256 | } | |
1257 | ||
a6fb235a | 1258 | err = ovl_copy_up_one(parent, next, flags); |
e9be9d5e MS |
1259 | |
1260 | dput(parent); | |
1261 | dput(next); | |
1262 | } | |
8eac98b8 | 1263 | revert_creds(old_cred); |
e9be9d5e MS |
1264 | |
1265 | return err; | |
1266 | } | |
9aba6521 | 1267 | |
d6eac039 VG |
1268 | static bool ovl_open_need_copy_up(struct dentry *dentry, int flags) |
1269 | { | |
1270 | /* Copy up of disconnected dentry does not set upper alias */ | |
0c288874 | 1271 | if (ovl_already_copied_up(dentry, flags)) |
d6eac039 VG |
1272 | return false; |
1273 | ||
1274 | if (special_file(d_inode(dentry)->i_mode)) | |
1275 | return false; | |
1276 | ||
0c288874 | 1277 | if (!ovl_open_flags_need_copy_up(flags)) |
d6eac039 VG |
1278 | return false; |
1279 | ||
1280 | return true; | |
1281 | } | |
1282 | ||
3428030d | 1283 | int ovl_maybe_copy_up(struct dentry *dentry, int flags) |
d6eac039 | 1284 | { |
162d0644 AG |
1285 | if (!ovl_open_need_copy_up(dentry, flags)) |
1286 | return 0; | |
d6eac039 | 1287 | |
162d0644 | 1288 | return ovl_copy_up_flags(dentry, flags); |
d6eac039 VG |
1289 | } |
1290 | ||
d1e6f6a9 VG |
1291 | int ovl_copy_up_with_data(struct dentry *dentry) |
1292 | { | |
1293 | return ovl_copy_up_flags(dentry, O_WRONLY); | |
1294 | } | |
1295 | ||
9aba6521 AG |
1296 | int ovl_copy_up(struct dentry *dentry) |
1297 | { | |
1298 | return ovl_copy_up_flags(dentry, 0); | |
1299 | } |