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