1 /*
2  *
3  * Copyright (C) 2011 Novell Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <linux/fs.h>
11 #include <linux/slab.h>
12 #include <linux/cred.h>
13 #include <linux/xattr.h>
14 #include <linux/posix_acl.h>
15 #include <linux/ratelimit.h>
16 #include "overlayfs.h"
17 
18 
ovl_setattr(struct dentry * dentry,struct iattr * attr)19 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
20 {
21 	int err;
22 	bool full_copy_up = false;
23 	struct dentry *upperdentry;
24 	const struct cred *old_cred;
25 
26 	err = setattr_prepare(dentry, attr);
27 	if (err)
28 		return err;
29 
30 	err = ovl_want_write(dentry);
31 	if (err)
32 		goto out;
33 
34 	if (attr->ia_valid & ATTR_SIZE) {
35 		struct inode *realinode = d_inode(ovl_dentry_real(dentry));
36 
37 		err = -ETXTBSY;
38 		if (atomic_read(&realinode->i_writecount) < 0)
39 			goto out_drop_write;
40 
41 		/* Truncate should trigger data copy up as well */
42 		full_copy_up = true;
43 	}
44 
45 	if (!full_copy_up)
46 		err = ovl_copy_up(dentry);
47 	else
48 		err = ovl_copy_up_with_data(dentry);
49 	if (!err) {
50 		struct inode *winode = NULL;
51 
52 		upperdentry = ovl_dentry_upper(dentry);
53 
54 		if (attr->ia_valid & ATTR_SIZE) {
55 			winode = d_inode(upperdentry);
56 			err = get_write_access(winode);
57 			if (err)
58 				goto out_drop_write;
59 		}
60 
61 		if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
62 			attr->ia_valid &= ~ATTR_MODE;
63 
64 		inode_lock(upperdentry->d_inode);
65 		old_cred = ovl_override_creds(dentry->d_sb);
66 		err = notify_change(upperdentry, attr, NULL);
67 		revert_creds(old_cred);
68 		if (!err)
69 			ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
70 		inode_unlock(upperdentry->d_inode);
71 
72 		if (winode)
73 			put_write_access(winode);
74 	}
75 out_drop_write:
76 	ovl_drop_write(dentry);
77 out:
78 	return err;
79 }
80 
ovl_map_dev_ino(struct dentry * dentry,struct kstat * stat,struct ovl_layer * lower_layer)81 static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat,
82 			   struct ovl_layer *lower_layer)
83 {
84 	bool samefs = ovl_same_sb(dentry->d_sb);
85 	unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
86 
87 	if (samefs) {
88 		/*
89 		 * When all layers are on the same fs, all real inode
90 		 * number are unique, so we use the overlay st_dev,
91 		 * which is friendly to du -x.
92 		 */
93 		stat->dev = dentry->d_sb->s_dev;
94 		return 0;
95 	} else if (xinobits) {
96 		unsigned int shift = 64 - xinobits;
97 		/*
98 		 * All inode numbers of underlying fs should not be using the
99 		 * high xinobits, so we use high xinobits to partition the
100 		 * overlay st_ino address space. The high bits holds the fsid
101 		 * (upper fsid is 0). This way overlay inode numbers are unique
102 		 * and all inodes use overlay st_dev. Inode numbers are also
103 		 * persistent for a given layer configuration.
104 		 */
105 		if (stat->ino >> shift) {
106 			pr_warn_ratelimited("overlayfs: inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
107 					    dentry, stat->ino, xinobits);
108 		} else {
109 			if (lower_layer)
110 				stat->ino |= ((u64)lower_layer->fsid) << shift;
111 
112 			stat->dev = dentry->d_sb->s_dev;
113 			return 0;
114 		}
115 	}
116 
117 	/* The inode could not be mapped to a unified st_ino address space */
118 	if (S_ISDIR(dentry->d_inode->i_mode)) {
119 		/*
120 		 * Always use the overlay st_dev for directories, so 'find
121 		 * -xdev' will scan the entire overlay mount and won't cross the
122 		 * overlay mount boundaries.
123 		 *
124 		 * If not all layers are on the same fs the pair {real st_ino;
125 		 * overlay st_dev} is not unique, so use the non persistent
126 		 * overlay st_ino for directories.
127 		 */
128 		stat->dev = dentry->d_sb->s_dev;
129 		stat->ino = dentry->d_inode->i_ino;
130 	} else if (lower_layer && lower_layer->fsid) {
131 		/*
132 		 * For non-samefs setup, if we cannot map all layers st_ino
133 		 * to a unified address space, we need to make sure that st_dev
134 		 * is unique per lower fs. Upper layer uses real st_dev and
135 		 * lower layers use the unique anonymous bdev assigned to the
136 		 * lower fs.
137 		 */
138 		stat->dev = lower_layer->fs->pseudo_dev;
139 	}
140 
141 	return 0;
142 }
143 
ovl_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)144 int ovl_getattr(const struct path *path, struct kstat *stat,
145 		u32 request_mask, unsigned int flags)
146 {
147 	struct dentry *dentry = path->dentry;
148 	enum ovl_path_type type;
149 	struct path realpath;
150 	const struct cred *old_cred;
151 	bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
152 	bool samefs = ovl_same_sb(dentry->d_sb);
153 	struct ovl_layer *lower_layer = NULL;
154 	int err;
155 	bool metacopy_blocks = false;
156 
157 	metacopy_blocks = ovl_is_metacopy_dentry(dentry);
158 
159 	type = ovl_path_real(dentry, &realpath);
160 	old_cred = ovl_override_creds(dentry->d_sb);
161 	err = vfs_getattr(&realpath, stat, request_mask, flags);
162 	if (err)
163 		goto out;
164 
165 	/*
166 	 * For non-dir or same fs, we use st_ino of the copy up origin.
167 	 * This guaranties constant st_dev/st_ino across copy up.
168 	 * With xino feature and non-samefs, we use st_ino of the copy up
169 	 * origin masked with high bits that represent the layer id.
170 	 *
171 	 * If lower filesystem supports NFS file handles, this also guaranties
172 	 * persistent st_ino across mount cycle.
173 	 */
174 	if (!is_dir || samefs || ovl_xino_bits(dentry->d_sb)) {
175 		if (!OVL_TYPE_UPPER(type)) {
176 			lower_layer = ovl_layer_lower(dentry);
177 		} else if (OVL_TYPE_ORIGIN(type)) {
178 			struct kstat lowerstat;
179 			u32 lowermask = STATX_INO | STATX_BLOCKS |
180 					(!is_dir ? STATX_NLINK : 0);
181 
182 			ovl_path_lower(dentry, &realpath);
183 			err = vfs_getattr(&realpath, &lowerstat,
184 					  lowermask, flags);
185 			if (err)
186 				goto out;
187 
188 			/*
189 			 * Lower hardlinks may be broken on copy up to different
190 			 * upper files, so we cannot use the lower origin st_ino
191 			 * for those different files, even for the same fs case.
192 			 *
193 			 * Similarly, several redirected dirs can point to the
194 			 * same dir on a lower layer. With the "verify_lower"
195 			 * feature, we do not use the lower origin st_ino, if
196 			 * we haven't verified that this redirect is unique.
197 			 *
198 			 * With inodes index enabled, it is safe to use st_ino
199 			 * of an indexed origin. The index validates that the
200 			 * upper hardlink is not broken and that a redirected
201 			 * dir is the only redirect to that origin.
202 			 */
203 			if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
204 			    (!ovl_verify_lower(dentry->d_sb) &&
205 			     (is_dir || lowerstat.nlink == 1))) {
206 				lower_layer = ovl_layer_lower(dentry);
207 				/*
208 				 * Cannot use origin st_dev;st_ino because
209 				 * origin inode content may differ from overlay
210 				 * inode content.
211 				 */
212 				if (samefs || lower_layer->fsid)
213 					stat->ino = lowerstat.ino;
214 			}
215 
216 			/*
217 			 * If we are querying a metacopy dentry and lower
218 			 * dentry is data dentry, then use the blocks we
219 			 * queried just now. We don't have to do additional
220 			 * vfs_getattr(). If lower itself is metacopy, then
221 			 * additional vfs_getattr() is unavoidable.
222 			 */
223 			if (metacopy_blocks &&
224 			    realpath.dentry == ovl_dentry_lowerdata(dentry)) {
225 				stat->blocks = lowerstat.blocks;
226 				metacopy_blocks = false;
227 			}
228 		}
229 
230 		if (metacopy_blocks) {
231 			/*
232 			 * If lower is not same as lowerdata or if there was
233 			 * no origin on upper, we can end up here.
234 			 */
235 			struct kstat lowerdatastat;
236 			u32 lowermask = STATX_BLOCKS;
237 
238 			ovl_path_lowerdata(dentry, &realpath);
239 			err = vfs_getattr(&realpath, &lowerdatastat,
240 					  lowermask, flags);
241 			if (err)
242 				goto out;
243 			stat->blocks = lowerdatastat.blocks;
244 		}
245 	}
246 
247 	err = ovl_map_dev_ino(dentry, stat, lower_layer);
248 	if (err)
249 		goto out;
250 
251 	/*
252 	 * It's probably not worth it to count subdirs to get the
253 	 * correct link count.  nlink=1 seems to pacify 'find' and
254 	 * other utilities.
255 	 */
256 	if (is_dir && OVL_TYPE_MERGE(type))
257 		stat->nlink = 1;
258 
259 	/*
260 	 * Return the overlay inode nlinks for indexed upper inodes.
261 	 * Overlay inode nlink counts the union of the upper hardlinks
262 	 * and non-covered lower hardlinks. It does not include the upper
263 	 * index hardlink.
264 	 */
265 	if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
266 		stat->nlink = dentry->d_inode->i_nlink;
267 
268 out:
269 	revert_creds(old_cred);
270 
271 	return err;
272 }
273 
ovl_permission(struct inode * inode,int mask)274 int ovl_permission(struct inode *inode, int mask)
275 {
276 	struct inode *upperinode = ovl_inode_upper(inode);
277 	struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
278 	const struct cred *old_cred;
279 	int err;
280 
281 	/* Careful in RCU walk mode */
282 	if (!realinode) {
283 		WARN_ON(!(mask & MAY_NOT_BLOCK));
284 		return -ECHILD;
285 	}
286 
287 	/*
288 	 * Check overlay inode with the creds of task and underlying inode
289 	 * with creds of mounter
290 	 */
291 	err = generic_permission(inode, mask);
292 	if (err)
293 		return err;
294 
295 	old_cred = ovl_override_creds(inode->i_sb);
296 	if (!upperinode &&
297 	    !special_file(realinode->i_mode) && mask & MAY_WRITE) {
298 		mask &= ~(MAY_WRITE | MAY_APPEND);
299 		/* Make sure mounter can read file for copy up later */
300 		mask |= MAY_READ;
301 	}
302 	err = inode_permission(realinode, mask);
303 	revert_creds(old_cred);
304 
305 	return err;
306 }
307 
ovl_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)308 static const char *ovl_get_link(struct dentry *dentry,
309 				struct inode *inode,
310 				struct delayed_call *done)
311 {
312 	const struct cred *old_cred;
313 	const char *p;
314 
315 	if (!dentry)
316 		return ERR_PTR(-ECHILD);
317 
318 	old_cred = ovl_override_creds(dentry->d_sb);
319 	p = vfs_get_link(ovl_dentry_real(dentry), done);
320 	revert_creds(old_cred);
321 	return p;
322 }
323 
ovl_is_private_xattr(const char * name)324 bool ovl_is_private_xattr(const char *name)
325 {
326 	return strncmp(name, OVL_XATTR_PREFIX,
327 		       sizeof(OVL_XATTR_PREFIX) - 1) == 0;
328 }
329 
ovl_xattr_set(struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)330 int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
331 		  const void *value, size_t size, int flags)
332 {
333 	int err;
334 	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
335 	struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
336 	const struct cred *old_cred;
337 
338 	err = ovl_want_write(dentry);
339 	if (err)
340 		goto out;
341 
342 	if (!value && !upperdentry) {
343 		old_cred = ovl_override_creds(dentry->d_sb);
344 		err = vfs_getxattr(realdentry, name, NULL, 0);
345 		revert_creds(old_cred);
346 		if (err < 0)
347 			goto out_drop_write;
348 	}
349 
350 	if (!upperdentry) {
351 		err = ovl_copy_up(dentry);
352 		if (err)
353 			goto out_drop_write;
354 
355 		realdentry = ovl_dentry_upper(dentry);
356 	}
357 
358 	old_cred = ovl_override_creds(dentry->d_sb);
359 	if (value)
360 		err = vfs_setxattr(realdentry, name, value, size, flags);
361 	else {
362 		WARN_ON(flags != XATTR_REPLACE);
363 		err = vfs_removexattr(realdentry, name);
364 	}
365 	revert_creds(old_cred);
366 
367 	/* copy c/mtime */
368 	ovl_copyattr(d_inode(realdentry), inode);
369 
370 out_drop_write:
371 	ovl_drop_write(dentry);
372 out:
373 	return err;
374 }
375 
ovl_xattr_get(struct dentry * dentry,struct inode * inode,const char * name,void * value,size_t size)376 int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
377 		  void *value, size_t size)
378 {
379 	ssize_t res;
380 	const struct cred *old_cred;
381 	struct dentry *realdentry =
382 		ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
383 
384 	old_cred = ovl_override_creds(dentry->d_sb);
385 	res = vfs_getxattr(realdentry, name, value, size);
386 	revert_creds(old_cred);
387 	return res;
388 }
389 
ovl_can_list(const char * s)390 static bool ovl_can_list(const char *s)
391 {
392 	/* List all non-trusted xatts */
393 	if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
394 		return true;
395 
396 	/* Never list trusted.overlay, list other trusted for superuser only */
397 	return !ovl_is_private_xattr(s) &&
398 	       ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
399 }
400 
ovl_listxattr(struct dentry * dentry,char * list,size_t size)401 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
402 {
403 	struct dentry *realdentry = ovl_dentry_real(dentry);
404 	ssize_t res;
405 	size_t len;
406 	char *s;
407 	const struct cred *old_cred;
408 
409 	old_cred = ovl_override_creds(dentry->d_sb);
410 	res = vfs_listxattr(realdentry, list, size);
411 	revert_creds(old_cred);
412 	if (res <= 0 || size == 0)
413 		return res;
414 
415 	/* filter out private xattrs */
416 	for (s = list, len = res; len;) {
417 		size_t slen = strnlen(s, len) + 1;
418 
419 		/* underlying fs providing us with an broken xattr list? */
420 		if (WARN_ON(slen > len))
421 			return -EIO;
422 
423 		len -= slen;
424 		if (!ovl_can_list(s)) {
425 			res -= slen;
426 			memmove(s, s + slen, len);
427 		} else {
428 			s += slen;
429 		}
430 	}
431 
432 	return res;
433 }
434 
ovl_get_acl(struct inode * inode,int type)435 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
436 {
437 	struct inode *realinode = ovl_inode_real(inode);
438 	const struct cred *old_cred;
439 	struct posix_acl *acl;
440 
441 	if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
442 		return NULL;
443 
444 	old_cred = ovl_override_creds(inode->i_sb);
445 	acl = get_acl(realinode, type);
446 	revert_creds(old_cred);
447 
448 	return acl;
449 }
450 
ovl_update_time(struct inode * inode,struct timespec64 * ts,int flags)451 int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
452 {
453 	if (flags & S_ATIME) {
454 		struct ovl_fs *ofs = inode->i_sb->s_fs_info;
455 		struct path upperpath = {
456 			.mnt = ofs->upper_mnt,
457 			.dentry = ovl_upperdentry_dereference(OVL_I(inode)),
458 		};
459 
460 		if (upperpath.dentry) {
461 			touch_atime(&upperpath);
462 			inode->i_atime = d_inode(upperpath.dentry)->i_atime;
463 		}
464 	}
465 	return 0;
466 }
467 
ovl_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,u64 start,u64 len)468 static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
469 		      u64 start, u64 len)
470 {
471 	int err;
472 	struct inode *realinode = ovl_inode_real(inode);
473 	const struct cred *old_cred;
474 
475 	if (!realinode->i_op->fiemap)
476 		return -EOPNOTSUPP;
477 
478 	old_cred = ovl_override_creds(inode->i_sb);
479 
480 	if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
481 		filemap_write_and_wait(realinode->i_mapping);
482 
483 	err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
484 	revert_creds(old_cred);
485 
486 	return err;
487 }
488 
489 static const struct inode_operations ovl_file_inode_operations = {
490 	.setattr	= ovl_setattr,
491 	.permission	= ovl_permission,
492 	.getattr	= ovl_getattr,
493 	.listxattr	= ovl_listxattr,
494 	.get_acl	= ovl_get_acl,
495 	.update_time	= ovl_update_time,
496 	.fiemap		= ovl_fiemap,
497 };
498 
499 static const struct inode_operations ovl_symlink_inode_operations = {
500 	.setattr	= ovl_setattr,
501 	.get_link	= ovl_get_link,
502 	.getattr	= ovl_getattr,
503 	.listxattr	= ovl_listxattr,
504 	.update_time	= ovl_update_time,
505 };
506 
507 static const struct inode_operations ovl_special_inode_operations = {
508 	.setattr	= ovl_setattr,
509 	.permission	= ovl_permission,
510 	.getattr	= ovl_getattr,
511 	.listxattr	= ovl_listxattr,
512 	.get_acl	= ovl_get_acl,
513 	.update_time	= ovl_update_time,
514 };
515 
516 static const struct address_space_operations ovl_aops = {
517 	/* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
518 	.direct_IO		= noop_direct_IO,
519 };
520 
521 /*
522  * It is possible to stack overlayfs instance on top of another
523  * overlayfs instance as lower layer. We need to annonate the
524  * stackable i_mutex locks according to stack level of the super
525  * block instance. An overlayfs instance can never be in stack
526  * depth 0 (there is always a real fs below it).  An overlayfs
527  * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
528  *
529  * For example, here is a snip from /proc/lockdep_chains after
530  * dir_iterate of nested overlayfs:
531  *
532  * [...] &ovl_i_mutex_dir_key[depth]   (stack_depth=2)
533  * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
534  * [...] &type->i_mutex_dir_key        (stack_depth=0)
535  */
536 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
537 
ovl_lockdep_annotate_inode_mutex_key(struct inode * inode)538 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
539 {
540 #ifdef CONFIG_LOCKDEP
541 	static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
542 	static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
543 	static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
544 
545 	int depth = inode->i_sb->s_stack_depth - 1;
546 
547 	if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
548 		depth = 0;
549 
550 	if (S_ISDIR(inode->i_mode))
551 		lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
552 	else
553 		lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
554 
555 	lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
556 #endif
557 }
558 
ovl_fill_inode(struct inode * inode,umode_t mode,dev_t rdev,unsigned long ino,int fsid)559 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
560 			   unsigned long ino, int fsid)
561 {
562 	int xinobits = ovl_xino_bits(inode->i_sb);
563 
564 	/*
565 	 * When d_ino is consistent with st_ino (samefs or i_ino has enough
566 	 * bits to encode layer), set the same value used for st_ino to i_ino,
567 	 * so inode number exposed via /proc/locks and a like will be
568 	 * consistent with d_ino and st_ino values. An i_ino value inconsistent
569 	 * with d_ino also causes nfsd readdirplus to fail.  When called from
570 	 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
571 	 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
572 	 */
573 	if (ovl_same_sb(inode->i_sb) || xinobits) {
574 		inode->i_ino = ino;
575 		if (xinobits && fsid && !(ino >> (64 - xinobits)))
576 			inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
577 	} else {
578 		inode->i_ino = get_next_ino();
579 	}
580 	inode->i_mode = mode;
581 	inode->i_flags |= S_NOCMTIME;
582 #ifdef CONFIG_FS_POSIX_ACL
583 	inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
584 #endif
585 
586 	ovl_lockdep_annotate_inode_mutex_key(inode);
587 
588 	switch (mode & S_IFMT) {
589 	case S_IFREG:
590 		inode->i_op = &ovl_file_inode_operations;
591 		inode->i_fop = &ovl_file_operations;
592 		inode->i_mapping->a_ops = &ovl_aops;
593 		break;
594 
595 	case S_IFDIR:
596 		inode->i_op = &ovl_dir_inode_operations;
597 		inode->i_fop = &ovl_dir_operations;
598 		break;
599 
600 	case S_IFLNK:
601 		inode->i_op = &ovl_symlink_inode_operations;
602 		break;
603 
604 	default:
605 		inode->i_op = &ovl_special_inode_operations;
606 		init_special_inode(inode, mode, rdev);
607 		break;
608 	}
609 }
610 
611 /*
612  * With inodes index enabled, an overlay inode nlink counts the union of upper
613  * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
614  * upper inode, the following nlink modifying operations can happen:
615  *
616  * 1. Lower hardlink copy up
617  * 2. Upper hardlink created, unlinked or renamed over
618  * 3. Lower hardlink whiteout or renamed over
619  *
620  * For the first, copy up case, the union nlink does not change, whether the
621  * operation succeeds or fails, but the upper inode nlink may change.
622  * Therefore, before copy up, we store the union nlink value relative to the
623  * lower inode nlink in the index inode xattr trusted.overlay.nlink.
624  *
625  * For the second, upper hardlink case, the union nlink should be incremented
626  * or decremented IFF the operation succeeds, aligned with nlink change of the
627  * upper inode. Therefore, before link/unlink/rename, we store the union nlink
628  * value relative to the upper inode nlink in the index inode.
629  *
630  * For the last, lower cover up case, we simplify things by preceding the
631  * whiteout or cover up with copy up. This makes sure that there is an index
632  * upper inode where the nlink xattr can be stored before the copied up upper
633  * entry is unlink.
634  */
635 #define OVL_NLINK_ADD_UPPER	(1 << 0)
636 
637 /*
638  * On-disk format for indexed nlink:
639  *
640  * nlink relative to the upper inode - "U[+-]NUM"
641  * nlink relative to the lower inode - "L[+-]NUM"
642  */
643 
ovl_set_nlink_common(struct dentry * dentry,struct dentry * realdentry,const char * format)644 static int ovl_set_nlink_common(struct dentry *dentry,
645 				struct dentry *realdentry, const char *format)
646 {
647 	struct inode *inode = d_inode(dentry);
648 	struct inode *realinode = d_inode(realdentry);
649 	char buf[13];
650 	int len;
651 
652 	len = snprintf(buf, sizeof(buf), format,
653 		       (int) (inode->i_nlink - realinode->i_nlink));
654 
655 	if (WARN_ON(len >= sizeof(buf)))
656 		return -EIO;
657 
658 	return ovl_do_setxattr(ovl_dentry_upper(dentry),
659 			       OVL_XATTR_NLINK, buf, len, 0);
660 }
661 
ovl_set_nlink_upper(struct dentry * dentry)662 int ovl_set_nlink_upper(struct dentry *dentry)
663 {
664 	return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
665 }
666 
ovl_set_nlink_lower(struct dentry * dentry)667 int ovl_set_nlink_lower(struct dentry *dentry)
668 {
669 	return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
670 }
671 
ovl_get_nlink(struct dentry * lowerdentry,struct dentry * upperdentry,unsigned int fallback)672 unsigned int ovl_get_nlink(struct dentry *lowerdentry,
673 			   struct dentry *upperdentry,
674 			   unsigned int fallback)
675 {
676 	int nlink_diff;
677 	int nlink;
678 	char buf[13];
679 	int err;
680 
681 	if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
682 		return fallback;
683 
684 	err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
685 	if (err < 0)
686 		goto fail;
687 
688 	buf[err] = '\0';
689 	if ((buf[0] != 'L' && buf[0] != 'U') ||
690 	    (buf[1] != '+' && buf[1] != '-'))
691 		goto fail;
692 
693 	err = kstrtoint(buf + 1, 10, &nlink_diff);
694 	if (err < 0)
695 		goto fail;
696 
697 	nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
698 	nlink += nlink_diff;
699 
700 	if (nlink <= 0)
701 		goto fail;
702 
703 	return nlink;
704 
705 fail:
706 	pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
707 			    upperdentry, err);
708 	return fallback;
709 }
710 
ovl_new_inode(struct super_block * sb,umode_t mode,dev_t rdev)711 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
712 {
713 	struct inode *inode;
714 
715 	inode = new_inode(sb);
716 	if (inode)
717 		ovl_fill_inode(inode, mode, rdev, 0, 0);
718 
719 	return inode;
720 }
721 
ovl_inode_test(struct inode * inode,void * data)722 static int ovl_inode_test(struct inode *inode, void *data)
723 {
724 	return inode->i_private == data;
725 }
726 
ovl_inode_set(struct inode * inode,void * data)727 static int ovl_inode_set(struct inode *inode, void *data)
728 {
729 	inode->i_private = data;
730 	return 0;
731 }
732 
ovl_verify_inode(struct inode * inode,struct dentry * lowerdentry,struct dentry * upperdentry,bool strict)733 static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
734 			     struct dentry *upperdentry, bool strict)
735 {
736 	/*
737 	 * For directories, @strict verify from lookup path performs consistency
738 	 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
739 	 * inode. Non @strict verify from NFS handle decode path passes NULL for
740 	 * 'unknown' lower/upper.
741 	 */
742 	if (S_ISDIR(inode->i_mode) && strict) {
743 		/* Real lower dir moved to upper layer under us? */
744 		if (!lowerdentry && ovl_inode_lower(inode))
745 			return false;
746 
747 		/* Lookup of an uncovered redirect origin? */
748 		if (!upperdentry && ovl_inode_upper(inode))
749 			return false;
750 	}
751 
752 	/*
753 	 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
754 	 * This happens when finding a copied up overlay inode for a renamed
755 	 * or hardlinked overlay dentry and lower dentry cannot be followed
756 	 * by origin because lower fs does not support file handles.
757 	 */
758 	if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
759 		return false;
760 
761 	/*
762 	 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
763 	 * This happens when finding a lower alias for a copied up hard link.
764 	 */
765 	if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
766 		return false;
767 
768 	return true;
769 }
770 
ovl_lookup_inode(struct super_block * sb,struct dentry * real,bool is_upper)771 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
772 			       bool is_upper)
773 {
774 	struct inode *inode, *key = d_inode(real);
775 
776 	inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
777 	if (!inode)
778 		return NULL;
779 
780 	if (!ovl_verify_inode(inode, is_upper ? NULL : real,
781 			      is_upper ? real : NULL, false)) {
782 		iput(inode);
783 		return ERR_PTR(-ESTALE);
784 	}
785 
786 	return inode;
787 }
788 
ovl_lookup_trap_inode(struct super_block * sb,struct dentry * dir)789 bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
790 {
791 	struct inode *key = d_inode(dir);
792 	struct inode *trap;
793 	bool res;
794 
795 	trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
796 	if (!trap)
797 		return false;
798 
799 	res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
800 				  !ovl_inode_lower(trap);
801 
802 	iput(trap);
803 	return res;
804 }
805 
806 /*
807  * Create an inode cache entry for layer root dir, that will intentionally
808  * fail ovl_verify_inode(), so any lookup that will find some layer root
809  * will fail.
810  */
ovl_get_trap_inode(struct super_block * sb,struct dentry * dir)811 struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
812 {
813 	struct inode *key = d_inode(dir);
814 	struct inode *trap;
815 
816 	if (!d_is_dir(dir))
817 		return ERR_PTR(-ENOTDIR);
818 
819 	trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
820 			    ovl_inode_set, key);
821 	if (!trap)
822 		return ERR_PTR(-ENOMEM);
823 
824 	if (!(trap->i_state & I_NEW)) {
825 		/* Conflicting layer roots? */
826 		iput(trap);
827 		return ERR_PTR(-ELOOP);
828 	}
829 
830 	trap->i_mode = S_IFDIR;
831 	trap->i_flags = S_DEAD;
832 	unlock_new_inode(trap);
833 
834 	return trap;
835 }
836 
837 /*
838  * Does overlay inode need to be hashed by lower inode?
839  */
ovl_hash_bylower(struct super_block * sb,struct dentry * upper,struct dentry * lower,struct dentry * index)840 static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
841 			     struct dentry *lower, struct dentry *index)
842 {
843 	struct ovl_fs *ofs = sb->s_fs_info;
844 
845 	/* No, if pure upper */
846 	if (!lower)
847 		return false;
848 
849 	/* Yes, if already indexed */
850 	if (index)
851 		return true;
852 
853 	/* Yes, if won't be copied up */
854 	if (!ofs->upper_mnt)
855 		return true;
856 
857 	/* No, if lower hardlink is or will be broken on copy up */
858 	if ((upper || !ovl_indexdir(sb)) &&
859 	    !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
860 		return false;
861 
862 	/* No, if non-indexed upper with NFS export */
863 	if (sb->s_export_op && upper)
864 		return false;
865 
866 	/* Otherwise, hash by lower inode for fsnotify */
867 	return true;
868 }
869 
ovl_iget5(struct super_block * sb,struct inode * newinode,struct inode * key)870 static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
871 			       struct inode *key)
872 {
873 	return newinode ? inode_insert5(newinode, (unsigned long) key,
874 					 ovl_inode_test, ovl_inode_set, key) :
875 			  iget5_locked(sb, (unsigned long) key,
876 				       ovl_inode_test, ovl_inode_set, key);
877 }
878 
ovl_get_inode(struct super_block * sb,struct ovl_inode_params * oip)879 struct inode *ovl_get_inode(struct super_block *sb,
880 			    struct ovl_inode_params *oip)
881 {
882 	struct dentry *upperdentry = oip->upperdentry;
883 	struct ovl_path *lowerpath = oip->lowerpath;
884 	struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
885 	struct inode *inode;
886 	struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
887 	bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
888 					oip->index);
889 	int fsid = bylower ? lowerpath->layer->fsid : 0;
890 	bool is_dir, metacopy = false;
891 	unsigned long ino = 0;
892 	int err = oip->newinode ? -EEXIST : -ENOMEM;
893 
894 	if (!realinode)
895 		realinode = d_inode(lowerdentry);
896 
897 	/*
898 	 * Copy up origin (lower) may exist for non-indexed upper, but we must
899 	 * not use lower as hash key if this is a broken hardlink.
900 	 */
901 	is_dir = S_ISDIR(realinode->i_mode);
902 	if (upperdentry || bylower) {
903 		struct inode *key = d_inode(bylower ? lowerdentry :
904 						      upperdentry);
905 		unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
906 
907 		inode = ovl_iget5(sb, oip->newinode, key);
908 		if (!inode)
909 			goto out_err;
910 		if (!(inode->i_state & I_NEW)) {
911 			/*
912 			 * Verify that the underlying files stored in the inode
913 			 * match those in the dentry.
914 			 */
915 			if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
916 					      true)) {
917 				iput(inode);
918 				err = -ESTALE;
919 				goto out_err;
920 			}
921 
922 			dput(upperdentry);
923 			kfree(oip->redirect);
924 			goto out;
925 		}
926 
927 		/* Recalculate nlink for non-dir due to indexing */
928 		if (!is_dir)
929 			nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
930 		set_nlink(inode, nlink);
931 		ino = key->i_ino;
932 	} else {
933 		/* Lower hardlink that will be broken on copy up */
934 		inode = new_inode(sb);
935 		if (!inode) {
936 			err = -ENOMEM;
937 			goto out_err;
938 		}
939 		ino = realinode->i_ino;
940 		fsid = lowerpath->layer->fsid;
941 	}
942 	ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
943 	ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata);
944 
945 	if (upperdentry && ovl_is_impuredir(upperdentry))
946 		ovl_set_flag(OVL_IMPURE, inode);
947 
948 	if (oip->index)
949 		ovl_set_flag(OVL_INDEX, inode);
950 
951 	if (upperdentry) {
952 		err = ovl_check_metacopy_xattr(upperdentry);
953 		if (err < 0)
954 			goto out_err;
955 		metacopy = err;
956 		if (!metacopy)
957 			ovl_set_flag(OVL_UPPERDATA, inode);
958 	}
959 
960 	OVL_I(inode)->redirect = oip->redirect;
961 
962 	if (bylower)
963 		ovl_set_flag(OVL_CONST_INO, inode);
964 
965 	/* Check for non-merge dir that may have whiteouts */
966 	if (is_dir) {
967 		if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
968 		    ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
969 			ovl_set_flag(OVL_WHITEOUTS, inode);
970 		}
971 	}
972 
973 	if (inode->i_state & I_NEW)
974 		unlock_new_inode(inode);
975 out:
976 	return inode;
977 
978 out_err:
979 	pr_warn_ratelimited("overlayfs: failed to get inode (%i)\n", err);
980 	inode = ERR_PTR(err);
981 	goto out;
982 }
983