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