xref: /OK3568_Linux_fs/kernel/fs/overlayfs/util.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011 Novell Inc.
4  * Copyright (C) 2016 Red Hat, Inc.
5  */
6 
7 #include <linux/fs.h>
8 #include <linux/mount.h>
9 #include <linux/slab.h>
10 #include <linux/cred.h>
11 #include <linux/xattr.h>
12 #include <linux/exportfs.h>
13 #include <linux/uuid.h>
14 #include <linux/namei.h>
15 #include <linux/ratelimit.h>
16 #include "overlayfs.h"
17 
ovl_want_write(struct dentry * dentry)18 int ovl_want_write(struct dentry *dentry)
19 {
20 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
21 	return mnt_want_write(ovl_upper_mnt(ofs));
22 }
23 
ovl_drop_write(struct dentry * dentry)24 void ovl_drop_write(struct dentry *dentry)
25 {
26 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
27 	mnt_drop_write(ovl_upper_mnt(ofs));
28 }
29 
ovl_workdir(struct dentry * dentry)30 struct dentry *ovl_workdir(struct dentry *dentry)
31 {
32 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
33 	return ofs->workdir;
34 }
35 
ovl_override_creds(struct super_block * sb)36 const struct cred *ovl_override_creds(struct super_block *sb)
37 {
38 	struct ovl_fs *ofs = sb->s_fs_info;
39 
40 	if (!ofs->config.override_creds)
41 		return NULL;
42 	return override_creds(ofs->creator_cred);
43 }
44 
ovl_revert_creds(struct super_block * sb,const struct cred * old_cred)45 void ovl_revert_creds(struct super_block *sb, const struct cred *old_cred)
46 {
47 	if (old_cred)
48 		revert_creds(old_cred);
49 }
50 
51 /*
52  * Check if underlying fs supports file handles and try to determine encoding
53  * type, in order to deduce maximum inode number used by fs.
54  *
55  * Return 0 if file handles are not supported.
56  * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
57  * Return -1 if fs uses a non default encoding with unknown inode size.
58  */
ovl_can_decode_fh(struct super_block * sb)59 int ovl_can_decode_fh(struct super_block *sb)
60 {
61 	if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
62 		return 0;
63 
64 	return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
65 }
66 
ovl_indexdir(struct super_block * sb)67 struct dentry *ovl_indexdir(struct super_block *sb)
68 {
69 	struct ovl_fs *ofs = sb->s_fs_info;
70 
71 	return ofs->indexdir;
72 }
73 
74 /* Index all files on copy up. For now only enabled for NFS export */
ovl_index_all(struct super_block * sb)75 bool ovl_index_all(struct super_block *sb)
76 {
77 	struct ovl_fs *ofs = sb->s_fs_info;
78 
79 	return ofs->config.nfs_export && ofs->config.index;
80 }
81 
82 /* Verify lower origin on lookup. For now only enabled for NFS export */
ovl_verify_lower(struct super_block * sb)83 bool ovl_verify_lower(struct super_block *sb)
84 {
85 	struct ovl_fs *ofs = sb->s_fs_info;
86 
87 	return ofs->config.nfs_export && ofs->config.index;
88 }
89 
ovl_alloc_entry(unsigned int numlower)90 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
91 {
92 	size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
93 	struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
94 
95 	if (oe)
96 		oe->numlower = numlower;
97 
98 	return oe;
99 }
100 
ovl_dentry_remote(struct dentry * dentry)101 bool ovl_dentry_remote(struct dentry *dentry)
102 {
103 	return dentry->d_flags &
104 		(DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
105 }
106 
ovl_dentry_update_reval(struct dentry * dentry,struct dentry * upperdentry,unsigned int mask)107 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
108 			     unsigned int mask)
109 {
110 	struct ovl_entry *oe = OVL_E(dentry);
111 	unsigned int i, flags = 0;
112 
113 	if (upperdentry)
114 		flags |= upperdentry->d_flags;
115 	for (i = 0; i < oe->numlower; i++)
116 		flags |= oe->lowerstack[i].dentry->d_flags;
117 
118 	spin_lock(&dentry->d_lock);
119 	dentry->d_flags &= ~mask;
120 	dentry->d_flags |= flags & mask;
121 	spin_unlock(&dentry->d_lock);
122 }
123 
ovl_dentry_weird(struct dentry * dentry)124 bool ovl_dentry_weird(struct dentry *dentry)
125 {
126 	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
127 				  DCACHE_MANAGE_TRANSIT |
128 				  DCACHE_OP_HASH |
129 				  DCACHE_OP_COMPARE);
130 }
131 
ovl_path_type(struct dentry * dentry)132 enum ovl_path_type ovl_path_type(struct dentry *dentry)
133 {
134 	struct ovl_entry *oe = dentry->d_fsdata;
135 	enum ovl_path_type type = 0;
136 
137 	if (ovl_dentry_upper(dentry)) {
138 		type = __OVL_PATH_UPPER;
139 
140 		/*
141 		 * Non-dir dentry can hold lower dentry of its copy up origin.
142 		 */
143 		if (oe->numlower) {
144 			if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
145 				type |= __OVL_PATH_ORIGIN;
146 			if (d_is_dir(dentry) ||
147 			    !ovl_has_upperdata(d_inode(dentry)))
148 				type |= __OVL_PATH_MERGE;
149 		}
150 	} else {
151 		if (oe->numlower > 1)
152 			type |= __OVL_PATH_MERGE;
153 	}
154 	return type;
155 }
156 
ovl_path_upper(struct dentry * dentry,struct path * path)157 void ovl_path_upper(struct dentry *dentry, struct path *path)
158 {
159 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
160 
161 	path->mnt = ovl_upper_mnt(ofs);
162 	path->dentry = ovl_dentry_upper(dentry);
163 }
164 
ovl_path_lower(struct dentry * dentry,struct path * path)165 void ovl_path_lower(struct dentry *dentry, struct path *path)
166 {
167 	struct ovl_entry *oe = dentry->d_fsdata;
168 
169 	if (oe->numlower) {
170 		path->mnt = oe->lowerstack[0].layer->mnt;
171 		path->dentry = oe->lowerstack[0].dentry;
172 	} else {
173 		*path = (struct path) { };
174 	}
175 }
176 
ovl_path_lowerdata(struct dentry * dentry,struct path * path)177 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
178 {
179 	struct ovl_entry *oe = dentry->d_fsdata;
180 
181 	if (oe->numlower) {
182 		path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
183 		path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
184 	} else {
185 		*path = (struct path) { };
186 	}
187 }
188 
ovl_path_real(struct dentry * dentry,struct path * path)189 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
190 {
191 	enum ovl_path_type type = ovl_path_type(dentry);
192 
193 	if (!OVL_TYPE_UPPER(type))
194 		ovl_path_lower(dentry, path);
195 	else
196 		ovl_path_upper(dentry, path);
197 
198 	return type;
199 }
200 
ovl_dentry_upper(struct dentry * dentry)201 struct dentry *ovl_dentry_upper(struct dentry *dentry)
202 {
203 	return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
204 }
205 
ovl_dentry_lower(struct dentry * dentry)206 struct dentry *ovl_dentry_lower(struct dentry *dentry)
207 {
208 	struct ovl_entry *oe = dentry->d_fsdata;
209 
210 	return oe->numlower ? oe->lowerstack[0].dentry : NULL;
211 }
212 
ovl_layer_lower(struct dentry * dentry)213 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
214 {
215 	struct ovl_entry *oe = dentry->d_fsdata;
216 
217 	return oe->numlower ? oe->lowerstack[0].layer : NULL;
218 }
219 
220 /*
221  * ovl_dentry_lower() could return either a data dentry or metacopy dentry
222  * dependig on what is stored in lowerstack[0]. At times we need to find
223  * lower dentry which has data (and not metacopy dentry). This helper
224  * returns the lower data dentry.
225  */
ovl_dentry_lowerdata(struct dentry * dentry)226 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
227 {
228 	struct ovl_entry *oe = dentry->d_fsdata;
229 
230 	return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
231 }
232 
ovl_dentry_real(struct dentry * dentry)233 struct dentry *ovl_dentry_real(struct dentry *dentry)
234 {
235 	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
236 }
237 
ovl_i_dentry_upper(struct inode * inode)238 struct dentry *ovl_i_dentry_upper(struct inode *inode)
239 {
240 	return ovl_upperdentry_dereference(OVL_I(inode));
241 }
242 
ovl_inode_upper(struct inode * inode)243 struct inode *ovl_inode_upper(struct inode *inode)
244 {
245 	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
246 
247 	return upperdentry ? d_inode(upperdentry) : NULL;
248 }
249 
ovl_inode_lower(struct inode * inode)250 struct inode *ovl_inode_lower(struct inode *inode)
251 {
252 	return OVL_I(inode)->lower;
253 }
254 
ovl_inode_real(struct inode * inode)255 struct inode *ovl_inode_real(struct inode *inode)
256 {
257 	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
258 }
259 
260 /* Return inode which contains lower data. Do not return metacopy */
ovl_inode_lowerdata(struct inode * inode)261 struct inode *ovl_inode_lowerdata(struct inode *inode)
262 {
263 	if (WARN_ON(!S_ISREG(inode->i_mode)))
264 		return NULL;
265 
266 	return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
267 }
268 
269 /* Return real inode which contains data. Does not return metacopy inode */
ovl_inode_realdata(struct inode * inode)270 struct inode *ovl_inode_realdata(struct inode *inode)
271 {
272 	struct inode *upperinode;
273 
274 	upperinode = ovl_inode_upper(inode);
275 	if (upperinode && ovl_has_upperdata(inode))
276 		return upperinode;
277 
278 	return ovl_inode_lowerdata(inode);
279 }
280 
ovl_dir_cache(struct inode * inode)281 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
282 {
283 	return OVL_I(inode)->cache;
284 }
285 
ovl_set_dir_cache(struct inode * inode,struct ovl_dir_cache * cache)286 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
287 {
288 	OVL_I(inode)->cache = cache;
289 }
290 
ovl_dentry_set_flag(unsigned long flag,struct dentry * dentry)291 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
292 {
293 	set_bit(flag, &OVL_E(dentry)->flags);
294 }
295 
ovl_dentry_clear_flag(unsigned long flag,struct dentry * dentry)296 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
297 {
298 	clear_bit(flag, &OVL_E(dentry)->flags);
299 }
300 
ovl_dentry_test_flag(unsigned long flag,struct dentry * dentry)301 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
302 {
303 	return test_bit(flag, &OVL_E(dentry)->flags);
304 }
305 
ovl_dentry_is_opaque(struct dentry * dentry)306 bool ovl_dentry_is_opaque(struct dentry *dentry)
307 {
308 	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
309 }
310 
ovl_dentry_is_whiteout(struct dentry * dentry)311 bool ovl_dentry_is_whiteout(struct dentry *dentry)
312 {
313 	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
314 }
315 
ovl_dentry_set_opaque(struct dentry * dentry)316 void ovl_dentry_set_opaque(struct dentry *dentry)
317 {
318 	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
319 }
320 
321 /*
322  * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
323  * to return positive, while there's no actual upper alias for the inode.
324  * Copy up code needs to know about the existence of the upper alias, so it
325  * can't use ovl_dentry_upper().
326  */
ovl_dentry_has_upper_alias(struct dentry * dentry)327 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
328 {
329 	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
330 }
331 
ovl_dentry_set_upper_alias(struct dentry * dentry)332 void ovl_dentry_set_upper_alias(struct dentry *dentry)
333 {
334 	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
335 }
336 
ovl_should_check_upperdata(struct inode * inode)337 static bool ovl_should_check_upperdata(struct inode *inode)
338 {
339 	if (!S_ISREG(inode->i_mode))
340 		return false;
341 
342 	if (!ovl_inode_lower(inode))
343 		return false;
344 
345 	return true;
346 }
347 
ovl_has_upperdata(struct inode * inode)348 bool ovl_has_upperdata(struct inode *inode)
349 {
350 	if (!ovl_should_check_upperdata(inode))
351 		return true;
352 
353 	if (!ovl_test_flag(OVL_UPPERDATA, inode))
354 		return false;
355 	/*
356 	 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
357 	 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
358 	 * if setting of OVL_UPPERDATA is visible, then effects of writes
359 	 * before that are visible too.
360 	 */
361 	smp_rmb();
362 	return true;
363 }
364 
ovl_set_upperdata(struct inode * inode)365 void ovl_set_upperdata(struct inode *inode)
366 {
367 	/*
368 	 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
369 	 * if OVL_UPPERDATA flag is visible, then effects of write operations
370 	 * before it are visible as well.
371 	 */
372 	smp_wmb();
373 	ovl_set_flag(OVL_UPPERDATA, inode);
374 }
375 
376 /* Caller should hold ovl_inode->lock */
ovl_dentry_needs_data_copy_up_locked(struct dentry * dentry,int flags)377 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
378 {
379 	if (!ovl_open_flags_need_copy_up(flags))
380 		return false;
381 
382 	return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
383 }
384 
ovl_dentry_needs_data_copy_up(struct dentry * dentry,int flags)385 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
386 {
387 	if (!ovl_open_flags_need_copy_up(flags))
388 		return false;
389 
390 	return !ovl_has_upperdata(d_inode(dentry));
391 }
392 
ovl_redirect_dir(struct super_block * sb)393 bool ovl_redirect_dir(struct super_block *sb)
394 {
395 	struct ovl_fs *ofs = sb->s_fs_info;
396 
397 	return ofs->config.redirect_dir && !ofs->noxattr;
398 }
399 
ovl_dentry_get_redirect(struct dentry * dentry)400 const char *ovl_dentry_get_redirect(struct dentry *dentry)
401 {
402 	return OVL_I(d_inode(dentry))->redirect;
403 }
404 
ovl_dentry_set_redirect(struct dentry * dentry,const char * redirect)405 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
406 {
407 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
408 
409 	kfree(oi->redirect);
410 	oi->redirect = redirect;
411 }
412 
ovl_inode_update(struct inode * inode,struct dentry * upperdentry)413 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
414 {
415 	struct inode *upperinode = d_inode(upperdentry);
416 
417 	WARN_ON(OVL_I(inode)->__upperdentry);
418 
419 	/*
420 	 * Make sure upperdentry is consistent before making it visible
421 	 */
422 	smp_wmb();
423 	OVL_I(inode)->__upperdentry = upperdentry;
424 	if (inode_unhashed(inode)) {
425 		inode->i_private = upperinode;
426 		__insert_inode_hash(inode, (unsigned long) upperinode);
427 	}
428 }
429 
ovl_dir_version_inc(struct dentry * dentry,bool impurity)430 static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
431 {
432 	struct inode *inode = d_inode(dentry);
433 
434 	WARN_ON(!inode_is_locked(inode));
435 	WARN_ON(!d_is_dir(dentry));
436 	/*
437 	 * Version is used by readdir code to keep cache consistent.
438 	 * For merge dirs (or dirs with origin) all changes need to be noted.
439 	 * For non-merge dirs, cache contains only impure entries (i.e. ones
440 	 * which have been copied up and have origins), so only need to note
441 	 * changes to impure entries.
442 	 */
443 	if (!ovl_dir_is_real(dentry) || impurity)
444 		OVL_I(inode)->version++;
445 }
446 
ovl_dir_modified(struct dentry * dentry,bool impurity)447 void ovl_dir_modified(struct dentry *dentry, bool impurity)
448 {
449 	/* Copy mtime/ctime */
450 	ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
451 
452 	ovl_dir_version_inc(dentry, impurity);
453 }
454 
ovl_dentry_version_get(struct dentry * dentry)455 u64 ovl_dentry_version_get(struct dentry *dentry)
456 {
457 	struct inode *inode = d_inode(dentry);
458 
459 	WARN_ON(!inode_is_locked(inode));
460 	return OVL_I(inode)->version;
461 }
462 
ovl_is_whiteout(struct dentry * dentry)463 bool ovl_is_whiteout(struct dentry *dentry)
464 {
465 	struct inode *inode = dentry->d_inode;
466 
467 	return inode && IS_WHITEOUT(inode);
468 }
469 
ovl_path_open(struct path * path,int flags)470 struct file *ovl_path_open(struct path *path, int flags)
471 {
472 	struct inode *inode = d_inode(path->dentry);
473 	int err, acc_mode;
474 
475 	if (flags & ~(O_ACCMODE | O_LARGEFILE))
476 		BUG();
477 
478 	switch (flags & O_ACCMODE) {
479 	case O_RDONLY:
480 		acc_mode = MAY_READ;
481 		break;
482 	case O_WRONLY:
483 		acc_mode = MAY_WRITE;
484 		break;
485 	default:
486 		BUG();
487 	}
488 
489 	err = inode_permission(inode, acc_mode | MAY_OPEN);
490 	if (err)
491 		return ERR_PTR(err);
492 
493 	/* O_NOATIME is an optimization, don't fail if not permitted */
494 	if (inode_owner_or_capable(inode))
495 		flags |= O_NOATIME;
496 
497 	return dentry_open(path, flags, current_cred());
498 }
499 
500 /* Caller should hold ovl_inode->lock */
ovl_already_copied_up_locked(struct dentry * dentry,int flags)501 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
502 {
503 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
504 
505 	if (ovl_dentry_upper(dentry) &&
506 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
507 	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
508 		return true;
509 
510 	return false;
511 }
512 
ovl_already_copied_up(struct dentry * dentry,int flags)513 bool ovl_already_copied_up(struct dentry *dentry, int flags)
514 {
515 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
516 
517 	/*
518 	 * Check if copy-up has happened as well as for upper alias (in
519 	 * case of hard links) is there.
520 	 *
521 	 * Both checks are lockless:
522 	 *  - false negatives: will recheck under oi->lock
523 	 *  - false positives:
524 	 *    + ovl_dentry_upper() uses memory barriers to ensure the
525 	 *      upper dentry is up-to-date
526 	 *    + ovl_dentry_has_upper_alias() relies on locking of
527 	 *      upper parent i_rwsem to prevent reordering copy-up
528 	 *      with rename.
529 	 */
530 	if (ovl_dentry_upper(dentry) &&
531 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
532 	    !ovl_dentry_needs_data_copy_up(dentry, flags))
533 		return true;
534 
535 	return false;
536 }
537 
ovl_copy_up_start(struct dentry * dentry,int flags)538 int ovl_copy_up_start(struct dentry *dentry, int flags)
539 {
540 	struct inode *inode = d_inode(dentry);
541 	int err;
542 
543 	err = ovl_inode_lock_interruptible(inode);
544 	if (!err && ovl_already_copied_up_locked(dentry, flags)) {
545 		err = 1; /* Already copied up */
546 		ovl_inode_unlock(inode);
547 	}
548 
549 	return err;
550 }
551 
ovl_copy_up_end(struct dentry * dentry)552 void ovl_copy_up_end(struct dentry *dentry)
553 {
554 	ovl_inode_unlock(d_inode(dentry));
555 }
556 
ovl_check_origin_xattr(struct ovl_fs * ofs,struct dentry * dentry)557 bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry)
558 {
559 	ssize_t res;
560 
561 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0);
562 
563 	/* Zero size value means "copied up but origin unknown" */
564 	if (res >= 0)
565 		return true;
566 
567 	return false;
568 }
569 
ovl_check_dir_xattr(struct super_block * sb,struct dentry * dentry,enum ovl_xattr ox)570 bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
571 			 enum ovl_xattr ox)
572 {
573 	ssize_t res;
574 	char val;
575 
576 	if (!d_is_dir(dentry))
577 		return false;
578 
579 	res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1);
580 	if (res == 1 && val == 'y')
581 		return true;
582 
583 	return false;
584 }
585 
586 #define OVL_XATTR_OPAQUE_POSTFIX	"opaque"
587 #define OVL_XATTR_REDIRECT_POSTFIX	"redirect"
588 #define OVL_XATTR_ORIGIN_POSTFIX	"origin"
589 #define OVL_XATTR_IMPURE_POSTFIX	"impure"
590 #define OVL_XATTR_NLINK_POSTFIX		"nlink"
591 #define OVL_XATTR_UPPER_POSTFIX		"upper"
592 #define OVL_XATTR_METACOPY_POSTFIX	"metacopy"
593 
594 #define OVL_XATTR_TAB_ENTRY(x) \
595 	[x] = OVL_XATTR_PREFIX x ## _POSTFIX
596 
597 const char *ovl_xattr_table[] = {
598 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
599 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
600 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
601 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
602 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
603 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
604 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
605 };
606 
ovl_check_setxattr(struct dentry * dentry,struct dentry * upperdentry,enum ovl_xattr ox,const void * value,size_t size,int xerr)607 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
608 		       enum ovl_xattr ox, const void *value, size_t size,
609 		       int xerr)
610 {
611 	int err;
612 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
613 
614 	if (ofs->noxattr)
615 		return xerr;
616 
617 	err = ovl_do_setxattr(ofs, upperdentry, ox, value, size);
618 
619 	if (err == -EOPNOTSUPP) {
620 		pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
621 		ofs->noxattr = true;
622 		return xerr;
623 	}
624 
625 	return err;
626 }
627 
ovl_set_impure(struct dentry * dentry,struct dentry * upperdentry)628 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
629 {
630 	int err;
631 
632 	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
633 		return 0;
634 
635 	/*
636 	 * Do not fail when upper doesn't support xattrs.
637 	 * Upper inodes won't have origin nor redirect xattr anyway.
638 	 */
639 	err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
640 				 "y", 1, 0);
641 	if (!err)
642 		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
643 
644 	return err;
645 }
646 
647 /**
648  * Caller must hold a reference to inode to prevent it from being freed while
649  * it is marked inuse.
650  */
ovl_inuse_trylock(struct dentry * dentry)651 bool ovl_inuse_trylock(struct dentry *dentry)
652 {
653 	struct inode *inode = d_inode(dentry);
654 	bool locked = false;
655 
656 	spin_lock(&inode->i_lock);
657 	if (!(inode->i_state & I_OVL_INUSE)) {
658 		inode->i_state |= I_OVL_INUSE;
659 		locked = true;
660 	}
661 	spin_unlock(&inode->i_lock);
662 
663 	return locked;
664 }
665 
ovl_inuse_unlock(struct dentry * dentry)666 void ovl_inuse_unlock(struct dentry *dentry)
667 {
668 	if (dentry) {
669 		struct inode *inode = d_inode(dentry);
670 
671 		spin_lock(&inode->i_lock);
672 		WARN_ON(!(inode->i_state & I_OVL_INUSE));
673 		inode->i_state &= ~I_OVL_INUSE;
674 		spin_unlock(&inode->i_lock);
675 	}
676 }
677 
ovl_is_inuse(struct dentry * dentry)678 bool ovl_is_inuse(struct dentry *dentry)
679 {
680 	struct inode *inode = d_inode(dentry);
681 	bool inuse;
682 
683 	spin_lock(&inode->i_lock);
684 	inuse = (inode->i_state & I_OVL_INUSE);
685 	spin_unlock(&inode->i_lock);
686 
687 	return inuse;
688 }
689 
690 /*
691  * Does this overlay dentry need to be indexed on copy up?
692  */
ovl_need_index(struct dentry * dentry)693 bool ovl_need_index(struct dentry *dentry)
694 {
695 	struct dentry *lower = ovl_dentry_lower(dentry);
696 
697 	if (!lower || !ovl_indexdir(dentry->d_sb))
698 		return false;
699 
700 	/* Index all files for NFS export and consistency verification */
701 	if (ovl_index_all(dentry->d_sb))
702 		return true;
703 
704 	/* Index only lower hardlinks on copy up */
705 	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
706 		return true;
707 
708 	return false;
709 }
710 
711 /* Caller must hold OVL_I(inode)->lock */
ovl_cleanup_index(struct dentry * dentry)712 static void ovl_cleanup_index(struct dentry *dentry)
713 {
714 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
715 	struct inode *dir = indexdir->d_inode;
716 	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
717 	struct dentry *upperdentry = ovl_dentry_upper(dentry);
718 	struct dentry *index = NULL;
719 	struct inode *inode;
720 	struct qstr name = { };
721 	int err;
722 
723 	err = ovl_get_index_name(lowerdentry, &name);
724 	if (err)
725 		goto fail;
726 
727 	inode = d_inode(upperdentry);
728 	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
729 		pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
730 				    upperdentry, inode->i_ino, inode->i_nlink);
731 		/*
732 		 * We either have a bug with persistent union nlink or a lower
733 		 * hardlink was added while overlay is mounted. Adding a lower
734 		 * hardlink and then unlinking all overlay hardlinks would drop
735 		 * overlay nlink to zero before all upper inodes are unlinked.
736 		 * As a safety measure, when that situation is detected, set
737 		 * the overlay nlink to the index inode nlink minus one for the
738 		 * index entry itself.
739 		 */
740 		set_nlink(d_inode(dentry), inode->i_nlink - 1);
741 		ovl_set_nlink_upper(dentry);
742 		goto out;
743 	}
744 
745 	inode_lock_nested(dir, I_MUTEX_PARENT);
746 	index = lookup_one_len(name.name, indexdir, name.len);
747 	err = PTR_ERR(index);
748 	if (IS_ERR(index)) {
749 		index = NULL;
750 	} else if (ovl_index_all(dentry->d_sb)) {
751 		/* Whiteout orphan index to block future open by handle */
752 		err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
753 					       dir, index);
754 	} else {
755 		/* Cleanup orphan index entries */
756 		err = ovl_cleanup(dir, index);
757 	}
758 
759 	inode_unlock(dir);
760 	if (err)
761 		goto fail;
762 
763 out:
764 	kfree(name.name);
765 	dput(index);
766 	return;
767 
768 fail:
769 	pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
770 	goto out;
771 }
772 
773 /*
774  * Operations that change overlay inode and upper inode nlink need to be
775  * synchronized with copy up for persistent nlink accounting.
776  */
ovl_nlink_start(struct dentry * dentry)777 int ovl_nlink_start(struct dentry *dentry)
778 {
779 	struct inode *inode = d_inode(dentry);
780 	const struct cred *old_cred;
781 	int err;
782 
783 	if (WARN_ON(!inode))
784 		return -ENOENT;
785 
786 	/*
787 	 * With inodes index is enabled, we store the union overlay nlink
788 	 * in an xattr on the index inode. When whiting out an indexed lower,
789 	 * we need to decrement the overlay persistent nlink, but before the
790 	 * first copy up, we have no upper index inode to store the xattr.
791 	 *
792 	 * As a workaround, before whiteout/rename over an indexed lower,
793 	 * copy up to create the upper index. Creating the upper index will
794 	 * initialize the overlay nlink, so it could be dropped if unlink
795 	 * or rename succeeds.
796 	 *
797 	 * TODO: implement metadata only index copy up when called with
798 	 *       ovl_copy_up_flags(dentry, O_PATH).
799 	 */
800 	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
801 		err = ovl_copy_up(dentry);
802 		if (err)
803 			return err;
804 	}
805 
806 	err = ovl_inode_lock_interruptible(inode);
807 	if (err)
808 		return err;
809 
810 	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
811 		goto out;
812 
813 	old_cred = ovl_override_creds(dentry->d_sb);
814 	/*
815 	 * The overlay inode nlink should be incremented/decremented IFF the
816 	 * upper operation succeeds, along with nlink change of upper inode.
817 	 * Therefore, before link/unlink/rename, we store the union nlink
818 	 * value relative to the upper inode nlink in an upper inode xattr.
819 	 */
820 	err = ovl_set_nlink_upper(dentry);
821 	ovl_revert_creds(dentry->d_sb, old_cred);
822 
823 out:
824 	if (err)
825 		ovl_inode_unlock(inode);
826 
827 	return err;
828 }
829 
ovl_nlink_end(struct dentry * dentry)830 void ovl_nlink_end(struct dentry *dentry)
831 {
832 	struct inode *inode = d_inode(dentry);
833 
834 	if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
835 		const struct cred *old_cred;
836 
837 		old_cred = ovl_override_creds(dentry->d_sb);
838 		ovl_cleanup_index(dentry);
839 		ovl_revert_creds(dentry->d_sb, old_cred);
840 	}
841 
842 	ovl_inode_unlock(inode);
843 }
844 
ovl_lock_rename_workdir(struct dentry * workdir,struct dentry * upperdir)845 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
846 {
847 	/* Workdir should not be the same as upperdir */
848 	if (workdir == upperdir)
849 		goto err;
850 
851 	/* Workdir should not be subdir of upperdir and vice versa */
852 	if (lock_rename(workdir, upperdir) != NULL)
853 		goto err_unlock;
854 
855 	return 0;
856 
857 err_unlock:
858 	unlock_rename(workdir, upperdir);
859 err:
860 	pr_err("failed to lock workdir+upperdir\n");
861 	return -EIO;
862 }
863 
864 /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
ovl_check_metacopy_xattr(struct ovl_fs * ofs,struct dentry * dentry)865 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry)
866 {
867 	ssize_t res;
868 
869 	/* Only regular files can have metacopy xattr */
870 	if (!S_ISREG(d_inode(dentry)->i_mode))
871 		return 0;
872 
873 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0);
874 	if (res < 0) {
875 		if (res == -ENODATA || res == -EOPNOTSUPP)
876 			return 0;
877 		goto out;
878 	}
879 
880 	return 1;
881 out:
882 	pr_warn_ratelimited("failed to get metacopy (%zi)\n", res);
883 	return res;
884 }
885 
ovl_is_metacopy_dentry(struct dentry * dentry)886 bool ovl_is_metacopy_dentry(struct dentry *dentry)
887 {
888 	struct ovl_entry *oe = dentry->d_fsdata;
889 
890 	if (!d_is_reg(dentry))
891 		return false;
892 
893 	if (ovl_dentry_upper(dentry)) {
894 		if (!ovl_has_upperdata(d_inode(dentry)))
895 			return true;
896 		return false;
897 	}
898 
899 	return (oe->numlower > 1);
900 }
901 
ovl_get_redirect_xattr(struct ovl_fs * ofs,struct dentry * dentry,int padding)902 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
903 			     int padding)
904 {
905 	int res;
906 	char *s, *next, *buf = NULL;
907 
908 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0);
909 	if (res == -ENODATA || res == -EOPNOTSUPP)
910 		return NULL;
911 	if (res < 0)
912 		goto fail;
913 	if (res == 0)
914 		goto invalid;
915 
916 	buf = kzalloc(res + padding + 1, GFP_KERNEL);
917 	if (!buf)
918 		return ERR_PTR(-ENOMEM);
919 
920 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res);
921 	if (res < 0)
922 		goto fail;
923 	if (res == 0)
924 		goto invalid;
925 
926 	if (buf[0] == '/') {
927 		for (s = buf; *s++ == '/'; s = next) {
928 			next = strchrnul(s, '/');
929 			if (s == next)
930 				goto invalid;
931 		}
932 	} else {
933 		if (strchr(buf, '/') != NULL)
934 			goto invalid;
935 	}
936 
937 	return buf;
938 invalid:
939 	pr_warn_ratelimited("invalid redirect (%s)\n", buf);
940 	res = -EINVAL;
941 	goto err_free;
942 fail:
943 	pr_warn_ratelimited("failed to get redirect (%i)\n", res);
944 err_free:
945 	kfree(buf);
946 	return ERR_PTR(res);
947 }
948 
949 /*
950  * ovl_sync_status() - Check fs sync status for volatile mounts
951  *
952  * Returns 1 if this is not a volatile mount and a real sync is required.
953  *
954  * Returns 0 if syncing can be skipped because mount is volatile, and no errors
955  * have occurred on the upperdir since the mount.
956  *
957  * Returns -errno if it is a volatile mount, and the error that occurred since
958  * the last mount. If the error code changes, it'll return the latest error
959  * code.
960  */
961 
ovl_sync_status(struct ovl_fs * ofs)962 int ovl_sync_status(struct ovl_fs *ofs)
963 {
964 	struct vfsmount *mnt;
965 
966 	if (ovl_should_sync(ofs))
967 		return 1;
968 
969 	mnt = ovl_upper_mnt(ofs);
970 	if (!mnt)
971 		return 0;
972 
973 	return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
974 }
975