• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 Novell Inc.
3  * Copyright (C) 2016 Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <linux/fs.h>
11 #include <linux/mount.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
14 #include <linux/xattr.h>
15 #include <linux/exportfs.h>
16 #include <linux/uuid.h>
17 #include <linux/namei.h>
18 #include <linux/ratelimit.h>
19 #include "overlayfs.h"
20 
ovl_want_write(struct dentry * dentry)21 int ovl_want_write(struct dentry *dentry)
22 {
23 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
24 	return mnt_want_write(ofs->upper_mnt);
25 }
26 
ovl_drop_write(struct dentry * dentry)27 void ovl_drop_write(struct dentry *dentry)
28 {
29 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
30 	mnt_drop_write(ofs->upper_mnt);
31 }
32 
ovl_workdir(struct dentry * dentry)33 struct dentry *ovl_workdir(struct dentry *dentry)
34 {
35 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
36 	return ofs->workdir;
37 }
38 
ovl_override_creds(struct super_block * sb)39 const struct cred *ovl_override_creds(struct super_block *sb)
40 {
41 	struct ovl_fs *ofs = sb->s_fs_info;
42 
43 	return override_creds(ofs->creator_cred);
44 }
45 
ovl_same_sb(struct super_block * sb)46 struct super_block *ovl_same_sb(struct super_block *sb)
47 {
48 	struct ovl_fs *ofs = sb->s_fs_info;
49 
50 	if (!ofs->numlowerfs)
51 		return ofs->upper_mnt->mnt_sb;
52 	else if (ofs->numlowerfs == 1 && !ofs->upper_mnt)
53 		return ofs->lower_fs[0].sb;
54 	else
55 		return NULL;
56 }
57 
58 /*
59  * Check if underlying fs supports file handles and try to determine encoding
60  * type, in order to deduce maximum inode number used by fs.
61  *
62  * Return 0 if file handles are not supported.
63  * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
64  * Return -1 if fs uses a non default encoding with unknown inode size.
65  */
ovl_can_decode_fh(struct super_block * sb)66 int ovl_can_decode_fh(struct super_block *sb)
67 {
68 	if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry ||
69 	    uuid_is_null(&sb->s_uuid))
70 		return 0;
71 
72 	return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
73 }
74 
ovl_indexdir(struct super_block * sb)75 struct dentry *ovl_indexdir(struct super_block *sb)
76 {
77 	struct ovl_fs *ofs = sb->s_fs_info;
78 
79 	return ofs->indexdir;
80 }
81 
82 /* Index all files on copy up. For now only enabled for NFS export */
ovl_index_all(struct super_block * sb)83 bool ovl_index_all(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 
90 /* Verify lower origin on lookup. For now only enabled for NFS export */
ovl_verify_lower(struct super_block * sb)91 bool ovl_verify_lower(struct super_block *sb)
92 {
93 	struct ovl_fs *ofs = sb->s_fs_info;
94 
95 	return ofs->config.nfs_export && ofs->config.index;
96 }
97 
ovl_alloc_entry(unsigned int numlower)98 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
99 {
100 	size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
101 	struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
102 
103 	if (oe)
104 		oe->numlower = numlower;
105 
106 	return oe;
107 }
108 
ovl_dentry_remote(struct dentry * dentry)109 bool ovl_dentry_remote(struct dentry *dentry)
110 {
111 	return dentry->d_flags &
112 		(DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
113 		 DCACHE_OP_REAL);
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 = ofs->upper_mnt;
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 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_init(struct inode * inode,struct dentry * upperdentry,struct dentry * lowerdentry,struct dentry * lowerdata)405 void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
406 		    struct dentry *lowerdentry, struct dentry *lowerdata)
407 {
408 	struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
409 
410 	if (upperdentry)
411 		OVL_I(inode)->__upperdentry = upperdentry;
412 	if (lowerdentry)
413 		OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
414 	if (lowerdata)
415 		OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata));
416 
417 	ovl_copyattr(realinode, inode);
418 	ovl_copyflags(realinode, inode);
419 	if (!inode->i_ino)
420 		inode->i_ino = realinode->i_ino;
421 }
422 
ovl_inode_update(struct inode * inode,struct dentry * upperdentry)423 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
424 {
425 	struct inode *upperinode = d_inode(upperdentry);
426 
427 	WARN_ON(OVL_I(inode)->__upperdentry);
428 
429 	/*
430 	 * Make sure upperdentry is consistent before making it visible
431 	 */
432 	smp_wmb();
433 	OVL_I(inode)->__upperdentry = upperdentry;
434 	if (inode_unhashed(inode)) {
435 		if (!inode->i_ino)
436 			inode->i_ino = upperinode->i_ino;
437 		inode->i_private = upperinode;
438 		__insert_inode_hash(inode, (unsigned long) upperinode);
439 	}
440 }
441 
ovl_dentry_version_inc(struct dentry * dentry,bool impurity)442 static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
443 {
444 	struct inode *inode = d_inode(dentry);
445 
446 	WARN_ON(!inode_is_locked(inode));
447 	/*
448 	 * Version is used by readdir code to keep cache consistent.  For merge
449 	 * dirs all changes need to be noted.  For non-merge dirs, cache only
450 	 * contains impure (ones which have been copied up and have origins)
451 	 * entries, so only need to note changes to impure entries.
452 	 */
453 	if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
454 		OVL_I(inode)->version++;
455 }
456 
ovl_dir_modified(struct dentry * dentry,bool impurity)457 void ovl_dir_modified(struct dentry *dentry, bool impurity)
458 {
459 	/* Copy mtime/ctime */
460 	ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
461 
462 	ovl_dentry_version_inc(dentry, impurity);
463 }
464 
ovl_dentry_version_get(struct dentry * dentry)465 u64 ovl_dentry_version_get(struct dentry *dentry)
466 {
467 	struct inode *inode = d_inode(dentry);
468 
469 	WARN_ON(!inode_is_locked(inode));
470 	return OVL_I(inode)->version;
471 }
472 
ovl_is_whiteout(struct dentry * dentry)473 bool ovl_is_whiteout(struct dentry *dentry)
474 {
475 	struct inode *inode = dentry->d_inode;
476 
477 	return inode && IS_WHITEOUT(inode);
478 }
479 
ovl_path_open(struct path * path,int flags)480 struct file *ovl_path_open(struct path *path, int flags)
481 {
482 	struct inode *inode = d_inode(path->dentry);
483 	int err, acc_mode;
484 
485 	if (flags & ~(O_ACCMODE | O_LARGEFILE))
486 		BUG();
487 
488 	switch (flags & O_ACCMODE) {
489 	case O_RDONLY:
490 		acc_mode = MAY_READ;
491 		break;
492 	case O_WRONLY:
493 		acc_mode = MAY_WRITE;
494 		break;
495 	default:
496 		BUG();
497 	}
498 
499 	err = inode_permission(inode, acc_mode | MAY_OPEN);
500 	if (err)
501 		return ERR_PTR(err);
502 
503 	/* O_NOATIME is an optimization, don't fail if not permitted */
504 	if (inode_owner_or_capable(inode))
505 		flags |= O_NOATIME;
506 
507 	return dentry_open(path, flags, current_cred());
508 }
509 
510 /* Caller should hold ovl_inode->lock */
ovl_already_copied_up_locked(struct dentry * dentry,int flags)511 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
512 {
513 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
514 
515 	if (ovl_dentry_upper(dentry) &&
516 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
517 	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
518 		return true;
519 
520 	return false;
521 }
522 
ovl_already_copied_up(struct dentry * dentry,int flags)523 bool ovl_already_copied_up(struct dentry *dentry, int flags)
524 {
525 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
526 
527 	/*
528 	 * Check if copy-up has happened as well as for upper alias (in
529 	 * case of hard links) is there.
530 	 *
531 	 * Both checks are lockless:
532 	 *  - false negatives: will recheck under oi->lock
533 	 *  - false positives:
534 	 *    + ovl_dentry_upper() uses memory barriers to ensure the
535 	 *      upper dentry is up-to-date
536 	 *    + ovl_dentry_has_upper_alias() relies on locking of
537 	 *      upper parent i_rwsem to prevent reordering copy-up
538 	 *      with rename.
539 	 */
540 	if (ovl_dentry_upper(dentry) &&
541 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
542 	    !ovl_dentry_needs_data_copy_up(dentry, flags))
543 		return true;
544 
545 	return false;
546 }
547 
ovl_copy_up_start(struct dentry * dentry,int flags)548 int ovl_copy_up_start(struct dentry *dentry, int flags)
549 {
550 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
551 	int err;
552 
553 	err = mutex_lock_interruptible(&oi->lock);
554 	if (!err && ovl_already_copied_up_locked(dentry, flags)) {
555 		err = 1; /* Already copied up */
556 		mutex_unlock(&oi->lock);
557 	}
558 
559 	return err;
560 }
561 
ovl_copy_up_end(struct dentry * dentry)562 void ovl_copy_up_end(struct dentry *dentry)
563 {
564 	mutex_unlock(&OVL_I(d_inode(dentry))->lock);
565 }
566 
ovl_check_origin_xattr(struct dentry * dentry)567 bool ovl_check_origin_xattr(struct dentry *dentry)
568 {
569 	int res;
570 
571 	res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
572 
573 	/* Zero size value means "copied up but origin unknown" */
574 	if (res >= 0)
575 		return true;
576 
577 	return false;
578 }
579 
ovl_check_dir_xattr(struct dentry * dentry,const char * name)580 bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
581 {
582 	int res;
583 	char val;
584 
585 	if (!d_is_dir(dentry))
586 		return false;
587 
588 	res = vfs_getxattr(dentry, name, &val, 1);
589 	if (res == 1 && val == 'y')
590 		return true;
591 
592 	return false;
593 }
594 
ovl_check_setxattr(struct dentry * dentry,struct dentry * upperdentry,const char * name,const void * value,size_t size,int xerr)595 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
596 		       const char *name, const void *value, size_t size,
597 		       int xerr)
598 {
599 	int err;
600 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
601 
602 	if (ofs->noxattr)
603 		return xerr;
604 
605 	err = ovl_do_setxattr(upperdentry, name, value, size, 0);
606 
607 	if (err == -EOPNOTSUPP) {
608 		pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
609 		ofs->noxattr = true;
610 		return xerr;
611 	}
612 
613 	return err;
614 }
615 
ovl_set_impure(struct dentry * dentry,struct dentry * upperdentry)616 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
617 {
618 	int err;
619 
620 	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
621 		return 0;
622 
623 	/*
624 	 * Do not fail when upper doesn't support xattrs.
625 	 * Upper inodes won't have origin nor redirect xattr anyway.
626 	 */
627 	err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
628 				 "y", 1, 0);
629 	if (!err)
630 		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
631 
632 	return err;
633 }
634 
ovl_set_flag(unsigned long flag,struct inode * inode)635 void ovl_set_flag(unsigned long flag, struct inode *inode)
636 {
637 	set_bit(flag, &OVL_I(inode)->flags);
638 }
639 
ovl_clear_flag(unsigned long flag,struct inode * inode)640 void ovl_clear_flag(unsigned long flag, struct inode *inode)
641 {
642 	clear_bit(flag, &OVL_I(inode)->flags);
643 }
644 
ovl_test_flag(unsigned long flag,struct inode * inode)645 bool ovl_test_flag(unsigned long flag, struct inode *inode)
646 {
647 	return test_bit(flag, &OVL_I(inode)->flags);
648 }
649 
650 /**
651  * Caller must hold a reference to inode to prevent it from being freed while
652  * it is marked inuse.
653  */
ovl_inuse_trylock(struct dentry * dentry)654 bool ovl_inuse_trylock(struct dentry *dentry)
655 {
656 	struct inode *inode = d_inode(dentry);
657 	bool locked = false;
658 
659 	spin_lock(&inode->i_lock);
660 	if (!(inode->i_state & I_OVL_INUSE)) {
661 		inode->i_state |= I_OVL_INUSE;
662 		locked = true;
663 	}
664 	spin_unlock(&inode->i_lock);
665 
666 	return locked;
667 }
668 
ovl_inuse_unlock(struct dentry * dentry)669 void ovl_inuse_unlock(struct dentry *dentry)
670 {
671 	if (dentry) {
672 		struct inode *inode = d_inode(dentry);
673 
674 		spin_lock(&inode->i_lock);
675 		WARN_ON(!(inode->i_state & I_OVL_INUSE));
676 		inode->i_state &= ~I_OVL_INUSE;
677 		spin_unlock(&inode->i_lock);
678 	}
679 }
680 
ovl_is_inuse(struct dentry * dentry)681 bool ovl_is_inuse(struct dentry *dentry)
682 {
683 	struct inode *inode = d_inode(dentry);
684 	bool inuse;
685 
686 	spin_lock(&inode->i_lock);
687 	inuse = (inode->i_state & I_OVL_INUSE);
688 	spin_unlock(&inode->i_lock);
689 
690 	return inuse;
691 }
692 
693 /*
694  * Does this overlay dentry need to be indexed on copy up?
695  */
ovl_need_index(struct dentry * dentry)696 bool ovl_need_index(struct dentry *dentry)
697 {
698 	struct dentry *lower = ovl_dentry_lower(dentry);
699 
700 	if (!lower || !ovl_indexdir(dentry->d_sb))
701 		return false;
702 
703 	/* Index all files for NFS export and consistency verification */
704 	if (ovl_index_all(dentry->d_sb))
705 		return true;
706 
707 	/* Index only lower hardlinks on copy up */
708 	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
709 		return true;
710 
711 	return false;
712 }
713 
714 /* Caller must hold OVL_I(inode)->lock */
ovl_cleanup_index(struct dentry * dentry)715 static void ovl_cleanup_index(struct dentry *dentry)
716 {
717 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
718 	struct inode *dir = indexdir->d_inode;
719 	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
720 	struct dentry *upperdentry = ovl_dentry_upper(dentry);
721 	struct dentry *index = NULL;
722 	struct inode *inode;
723 	struct qstr name = { };
724 	int err;
725 
726 	err = ovl_get_index_name(lowerdentry, &name);
727 	if (err)
728 		goto fail;
729 
730 	inode = d_inode(upperdentry);
731 	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
732 		pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
733 				    upperdentry, inode->i_ino, inode->i_nlink);
734 		/*
735 		 * We either have a bug with persistent union nlink or a lower
736 		 * hardlink was added while overlay is mounted. Adding a lower
737 		 * hardlink and then unlinking all overlay hardlinks would drop
738 		 * overlay nlink to zero before all upper inodes are unlinked.
739 		 * As a safety measure, when that situation is detected, set
740 		 * the overlay nlink to the index inode nlink minus one for the
741 		 * index entry itself.
742 		 */
743 		set_nlink(d_inode(dentry), inode->i_nlink - 1);
744 		ovl_set_nlink_upper(dentry);
745 		goto out;
746 	}
747 
748 	inode_lock_nested(dir, I_MUTEX_PARENT);
749 	index = lookup_one_len(name.name, indexdir, name.len);
750 	err = PTR_ERR(index);
751 	if (IS_ERR(index)) {
752 		index = NULL;
753 	} else if (ovl_index_all(dentry->d_sb)) {
754 		/* Whiteout orphan index to block future open by handle */
755 		err = ovl_cleanup_and_whiteout(indexdir, dir, index);
756 	} else {
757 		/* Cleanup orphan index entries */
758 		err = ovl_cleanup(dir, index);
759 	}
760 
761 	inode_unlock(dir);
762 	if (err)
763 		goto fail;
764 
765 out:
766 	kfree(name.name);
767 	dput(index);
768 	return;
769 
770 fail:
771 	pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
772 	goto out;
773 }
774 
775 /*
776  * Operations that change overlay inode and upper inode nlink need to be
777  * synchronized with copy up for persistent nlink accounting.
778  */
ovl_nlink_start(struct dentry * dentry,bool * locked)779 int ovl_nlink_start(struct dentry *dentry, bool *locked)
780 {
781 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
782 	const struct cred *old_cred;
783 	int err;
784 
785 	if (!d_inode(dentry))
786 		return 0;
787 
788 	/*
789 	 * With inodes index is enabled, we store the union overlay nlink
790 	 * in an xattr on the index inode. When whiting out an indexed lower,
791 	 * we need to decrement the overlay persistent nlink, but before the
792 	 * first copy up, we have no upper index inode to store the xattr.
793 	 *
794 	 * As a workaround, before whiteout/rename over an indexed lower,
795 	 * copy up to create the upper index. Creating the upper index will
796 	 * initialize the overlay nlink, so it could be dropped if unlink
797 	 * or rename succeeds.
798 	 *
799 	 * TODO: implement metadata only index copy up when called with
800 	 *       ovl_copy_up_flags(dentry, O_PATH).
801 	 */
802 	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
803 		err = ovl_copy_up(dentry);
804 		if (err)
805 			return err;
806 	}
807 
808 	err = mutex_lock_interruptible(&oi->lock);
809 	if (err)
810 		return err;
811 
812 	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
813 		goto out;
814 
815 	old_cred = ovl_override_creds(dentry->d_sb);
816 	/*
817 	 * The overlay inode nlink should be incremented/decremented IFF the
818 	 * upper operation succeeds, along with nlink change of upper inode.
819 	 * Therefore, before link/unlink/rename, we store the union nlink
820 	 * value relative to the upper inode nlink in an upper inode xattr.
821 	 */
822 	err = ovl_set_nlink_upper(dentry);
823 	revert_creds(old_cred);
824 
825 out:
826 	if (err)
827 		mutex_unlock(&oi->lock);
828 	else
829 		*locked = true;
830 
831 	return err;
832 }
833 
ovl_nlink_end(struct dentry * dentry,bool locked)834 void ovl_nlink_end(struct dentry *dentry, bool locked)
835 {
836 	if (locked) {
837 		if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
838 		    d_inode(dentry)->i_nlink == 0) {
839 			const struct cred *old_cred;
840 
841 			old_cred = ovl_override_creds(dentry->d_sb);
842 			ovl_cleanup_index(dentry);
843 			revert_creds(old_cred);
844 		}
845 
846 		mutex_unlock(&OVL_I(d_inode(dentry))->lock);
847 	}
848 }
849 
ovl_lock_rename_workdir(struct dentry * workdir,struct dentry * upperdir)850 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
851 {
852 	/* Workdir should not be the same as upperdir */
853 	if (workdir == upperdir)
854 		goto err;
855 
856 	/* Workdir should not be subdir of upperdir and vice versa */
857 	if (lock_rename(workdir, upperdir) != NULL)
858 		goto err_unlock;
859 
860 	return 0;
861 
862 err_unlock:
863 	unlock_rename(workdir, upperdir);
864 err:
865 	pr_err("overlayfs: failed to lock workdir+upperdir\n");
866 	return -EIO;
867 }
868 
869 /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
ovl_check_metacopy_xattr(struct dentry * dentry)870 int ovl_check_metacopy_xattr(struct dentry *dentry)
871 {
872 	int res;
873 
874 	/* Only regular files can have metacopy xattr */
875 	if (!S_ISREG(d_inode(dentry)->i_mode))
876 		return 0;
877 
878 	res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
879 	if (res < 0) {
880 		if (res == -ENODATA || res == -EOPNOTSUPP)
881 			return 0;
882 		goto out;
883 	}
884 
885 	return 1;
886 out:
887 	pr_warn_ratelimited("overlayfs: failed to get metacopy (%i)\n", res);
888 	return res;
889 }
890 
ovl_is_metacopy_dentry(struct dentry * dentry)891 bool ovl_is_metacopy_dentry(struct dentry *dentry)
892 {
893 	struct ovl_entry *oe = dentry->d_fsdata;
894 
895 	if (!d_is_reg(dentry))
896 		return false;
897 
898 	if (ovl_dentry_upper(dentry)) {
899 		if (!ovl_has_upperdata(d_inode(dentry)))
900 			return true;
901 		return false;
902 	}
903 
904 	return (oe->numlower > 1);
905 }
906 
ovl_getxattr(struct dentry * dentry,char * name,char ** value,size_t padding)907 ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
908 		     size_t padding)
909 {
910 	ssize_t res;
911 	char *buf = NULL;
912 
913 	res = vfs_getxattr(dentry, name, NULL, 0);
914 	if (res < 0) {
915 		if (res == -ENODATA || res == -EOPNOTSUPP)
916 			return -ENODATA;
917 		goto fail;
918 	}
919 
920 	if (res != 0) {
921 		buf = kzalloc(res + padding, GFP_KERNEL);
922 		if (!buf)
923 			return -ENOMEM;
924 
925 		res = vfs_getxattr(dentry, name, buf, res);
926 		if (res < 0)
927 			goto fail;
928 	}
929 	*value = buf;
930 
931 	return res;
932 
933 fail:
934 	pr_warn_ratelimited("overlayfs: failed to get xattr %s: err=%zi)\n",
935 			    name, res);
936 	kfree(buf);
937 	return res;
938 }
939 
ovl_get_redirect_xattr(struct dentry * dentry,int padding)940 char *ovl_get_redirect_xattr(struct dentry *dentry, int padding)
941 {
942 	int res;
943 	char *s, *next, *buf = NULL;
944 
945 	res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1);
946 	if (res == -ENODATA)
947 		return NULL;
948 	if (res < 0)
949 		return ERR_PTR(res);
950 	if (res == 0)
951 		goto invalid;
952 
953 	if (buf[0] == '/') {
954 		for (s = buf; *s++ == '/'; s = next) {
955 			next = strchrnul(s, '/');
956 			if (s == next)
957 				goto invalid;
958 		}
959 	} else {
960 		if (strchr(buf, '/') != NULL)
961 			goto invalid;
962 	}
963 
964 	return buf;
965 invalid:
966 	pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
967 	res = -EINVAL;
968 	kfree(buf);
969 	return ERR_PTR(res);
970 }
971