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