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