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