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