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