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