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