blob: 595ea0d83dd4e4c2a17dced1f091b934c98bb704 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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
19int 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 ovl_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 }
75out_drop_write:
76 ovl_drop_write(dentry);
77out:
78 return err;
79}
80
81static 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
144int 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
268out:
269 ovl_revert_creds(old_cred);
270
271 return err;
272}
273
274int 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 ovl_revert_creds(old_cred);
304
305 return err;
306}
307
308static 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 ovl_revert_creds(old_cred);
321 return p;
322}
323
324bool ovl_is_private_xattr(const char *name)
325{
326 return strncmp(name, OVL_XATTR_PREFIX,
327 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
328}
329
330int 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 err = vfs_getxattr(realdentry, name, NULL, 0);
344 if (err < 0)
345 goto out_drop_write;
346 }
347
348 if (!upperdentry) {
349 err = ovl_copy_up(dentry);
350 if (err)
351 goto out_drop_write;
352
353 realdentry = ovl_dentry_upper(dentry);
354 }
355
356 old_cred = ovl_override_creds(dentry->d_sb);
357 if (value)
358 err = vfs_setxattr(realdentry, name, value, size, flags);
359 else {
360 WARN_ON(flags != XATTR_REPLACE);
361 err = vfs_removexattr(realdentry, name);
362 }
363 ovl_revert_creds(old_cred);
364
365 /* copy c/mtime */
366 ovl_copyattr(d_inode(realdentry), inode);
367
368out_drop_write:
369 ovl_drop_write(dentry);
370out:
371 return err;
372}
373
374int __ovl_xattr_get(struct dentry *dentry, struct inode *inode,
375 const char *name, void *value, size_t size)
376{
377 ssize_t res;
378 const struct cred *old_cred;
379 struct dentry *realdentry =
380 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
381
382 old_cred = ovl_override_creds(dentry->d_sb);
383 res = __vfs_getxattr(realdentry, d_inode(realdentry), name, value,
384 size);
385 ovl_revert_creds(old_cred);
386 return res;
387}
388
389int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
390 void *value, size_t size)
391{
392 ssize_t res;
393 const struct cred *old_cred;
394 struct dentry *realdentry =
395 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
396
397 old_cred = ovl_override_creds(dentry->d_sb);
398 res = vfs_getxattr(realdentry, name, value, size);
399 ovl_revert_creds(old_cred);
400 return res;
401}
402
403static bool ovl_can_list(const char *s)
404{
405 /* List all non-trusted xatts */
406 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
407 return true;
408
409 /* Never list trusted.overlay, list other trusted for superuser only */
410 return !ovl_is_private_xattr(s) &&
411 ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
412}
413
414ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
415{
416 struct dentry *realdentry = ovl_dentry_real(dentry);
417 ssize_t res;
418 size_t len;
419 char *s;
420 const struct cred *old_cred;
421
422 old_cred = ovl_override_creds(dentry->d_sb);
423 res = vfs_listxattr(realdentry, list, size);
424 ovl_revert_creds(old_cred);
425 if (res <= 0 || size == 0)
426 return res;
427
428 /* filter out private xattrs */
429 for (s = list, len = res; len;) {
430 size_t slen = strnlen(s, len) + 1;
431
432 /* underlying fs providing us with an broken xattr list? */
433 if (WARN_ON(slen > len))
434 return -EIO;
435
436 len -= slen;
437 if (!ovl_can_list(s)) {
438 res -= slen;
439 memmove(s, s + slen, len);
440 } else {
441 s += slen;
442 }
443 }
444
445 return res;
446}
447
448struct posix_acl *ovl_get_acl(struct inode *inode, int type)
449{
450 struct inode *realinode = ovl_inode_real(inode);
451 const struct cred *old_cred;
452 struct posix_acl *acl;
453
454 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
455 return NULL;
456
457 old_cred = ovl_override_creds(inode->i_sb);
458 acl = get_acl(realinode, type);
459 ovl_revert_creds(old_cred);
460
461 return acl;
462}
463
464int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
465{
466 if (flags & S_ATIME) {
467 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
468 struct path upperpath = {
469 .mnt = ofs->upper_mnt,
470 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
471 };
472
473 if (upperpath.dentry) {
474 touch_atime(&upperpath);
475 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
476 }
477 }
478 return 0;
479}
480
481static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
482 u64 start, u64 len)
483{
484 int err;
485 struct inode *realinode = ovl_inode_real(inode);
486 const struct cred *old_cred;
487
488 if (!realinode->i_op->fiemap)
489 return -EOPNOTSUPP;
490
491 old_cred = ovl_override_creds(inode->i_sb);
492
493 if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
494 filemap_write_and_wait(realinode->i_mapping);
495
496 err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
497 ovl_revert_creds(old_cred);
498
499 return err;
500}
501
502static const struct inode_operations ovl_file_inode_operations = {
503 .setattr = ovl_setattr,
504 .permission = ovl_permission,
505 .getattr = ovl_getattr,
506 .listxattr = ovl_listxattr,
507 .get_acl = ovl_get_acl,
508 .update_time = ovl_update_time,
509 .fiemap = ovl_fiemap,
510};
511
512static const struct inode_operations ovl_symlink_inode_operations = {
513 .setattr = ovl_setattr,
514 .get_link = ovl_get_link,
515 .getattr = ovl_getattr,
516 .listxattr = ovl_listxattr,
517 .update_time = ovl_update_time,
518};
519
520static const struct inode_operations ovl_special_inode_operations = {
521 .setattr = ovl_setattr,
522 .permission = ovl_permission,
523 .getattr = ovl_getattr,
524 .listxattr = ovl_listxattr,
525 .get_acl = ovl_get_acl,
526 .update_time = ovl_update_time,
527};
528
529static const struct address_space_operations ovl_aops = {
530 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
531 .direct_IO = noop_direct_IO,
532};
533
534/*
535 * It is possible to stack overlayfs instance on top of another
536 * overlayfs instance as lower layer. We need to annonate the
537 * stackable i_mutex locks according to stack level of the super
538 * block instance. An overlayfs instance can never be in stack
539 * depth 0 (there is always a real fs below it). An overlayfs
540 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
541 *
542 * For example, here is a snip from /proc/lockdep_chains after
543 * dir_iterate of nested overlayfs:
544 *
545 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
546 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
547 * [...] &type->i_mutex_dir_key (stack_depth=0)
548 */
549#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
550
551static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
552{
553#ifdef CONFIG_LOCKDEP
554 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
555 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
556 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
557
558 int depth = inode->i_sb->s_stack_depth - 1;
559
560 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
561 depth = 0;
562
563 if (S_ISDIR(inode->i_mode))
564 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
565 else
566 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
567
568 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
569#endif
570}
571
572static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
573 unsigned long ino, int fsid)
574{
575 int xinobits = ovl_xino_bits(inode->i_sb);
576
577 /*
578 * When d_ino is consistent with st_ino (samefs or i_ino has enough
579 * bits to encode layer), set the same value used for st_ino to i_ino,
580 * so inode number exposed via /proc/locks and a like will be
581 * consistent with d_ino and st_ino values. An i_ino value inconsistent
582 * with d_ino also causes nfsd readdirplus to fail. When called from
583 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
584 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
585 */
586 if (ovl_same_sb(inode->i_sb) || xinobits) {
587 inode->i_ino = ino;
588 if (xinobits && fsid && !(ino >> (64 - xinobits)))
589 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
590 } else {
591 inode->i_ino = get_next_ino();
592 }
593 inode->i_mode = mode;
594 inode->i_flags |= S_NOCMTIME;
595#ifdef CONFIG_FS_POSIX_ACL
596 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
597#endif
598
599 ovl_lockdep_annotate_inode_mutex_key(inode);
600
601 switch (mode & S_IFMT) {
602 case S_IFREG:
603 inode->i_op = &ovl_file_inode_operations;
604 inode->i_fop = &ovl_file_operations;
605 inode->i_mapping->a_ops = &ovl_aops;
606 break;
607
608 case S_IFDIR:
609 inode->i_op = &ovl_dir_inode_operations;
610 inode->i_fop = &ovl_dir_operations;
611 break;
612
613 case S_IFLNK:
614 inode->i_op = &ovl_symlink_inode_operations;
615 break;
616
617 default:
618 inode->i_op = &ovl_special_inode_operations;
619 init_special_inode(inode, mode, rdev);
620 break;
621 }
622}
623
624/*
625 * With inodes index enabled, an overlay inode nlink counts the union of upper
626 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
627 * upper inode, the following nlink modifying operations can happen:
628 *
629 * 1. Lower hardlink copy up
630 * 2. Upper hardlink created, unlinked or renamed over
631 * 3. Lower hardlink whiteout or renamed over
632 *
633 * For the first, copy up case, the union nlink does not change, whether the
634 * operation succeeds or fails, but the upper inode nlink may change.
635 * Therefore, before copy up, we store the union nlink value relative to the
636 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
637 *
638 * For the second, upper hardlink case, the union nlink should be incremented
639 * or decremented IFF the operation succeeds, aligned with nlink change of the
640 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
641 * value relative to the upper inode nlink in the index inode.
642 *
643 * For the last, lower cover up case, we simplify things by preceding the
644 * whiteout or cover up with copy up. This makes sure that there is an index
645 * upper inode where the nlink xattr can be stored before the copied up upper
646 * entry is unlink.
647 */
648#define OVL_NLINK_ADD_UPPER (1 << 0)
649
650/*
651 * On-disk format for indexed nlink:
652 *
653 * nlink relative to the upper inode - "U[+-]NUM"
654 * nlink relative to the lower inode - "L[+-]NUM"
655 */
656
657static int ovl_set_nlink_common(struct dentry *dentry,
658 struct dentry *realdentry, const char *format)
659{
660 struct inode *inode = d_inode(dentry);
661 struct inode *realinode = d_inode(realdentry);
662 char buf[13];
663 int len;
664
665 len = snprintf(buf, sizeof(buf), format,
666 (int) (inode->i_nlink - realinode->i_nlink));
667
668 if (WARN_ON(len >= sizeof(buf)))
669 return -EIO;
670
671 return ovl_do_setxattr(ovl_dentry_upper(dentry),
672 OVL_XATTR_NLINK, buf, len, 0);
673}
674
675int ovl_set_nlink_upper(struct dentry *dentry)
676{
677 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
678}
679
680int ovl_set_nlink_lower(struct dentry *dentry)
681{
682 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
683}
684
685unsigned int ovl_get_nlink(struct dentry *lowerdentry,
686 struct dentry *upperdentry,
687 unsigned int fallback)
688{
689 int nlink_diff;
690 int nlink;
691 char buf[13];
692 int err;
693
694 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
695 return fallback;
696
697 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
698 if (err < 0)
699 goto fail;
700
701 buf[err] = '\0';
702 if ((buf[0] != 'L' && buf[0] != 'U') ||
703 (buf[1] != '+' && buf[1] != '-'))
704 goto fail;
705
706 err = kstrtoint(buf + 1, 10, &nlink_diff);
707 if (err < 0)
708 goto fail;
709
710 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
711 nlink += nlink_diff;
712
713 if (nlink <= 0)
714 goto fail;
715
716 return nlink;
717
718fail:
719 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
720 upperdentry, err);
721 return fallback;
722}
723
724struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
725{
726 struct inode *inode;
727
728 inode = new_inode(sb);
729 if (inode)
730 ovl_fill_inode(inode, mode, rdev, 0, 0);
731
732 return inode;
733}
734
735static int ovl_inode_test(struct inode *inode, void *data)
736{
737 return inode->i_private == data;
738}
739
740static int ovl_inode_set(struct inode *inode, void *data)
741{
742 inode->i_private = data;
743 return 0;
744}
745
746static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
747 struct dentry *upperdentry, bool strict)
748{
749 /*
750 * For directories, @strict verify from lookup path performs consistency
751 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
752 * inode. Non @strict verify from NFS handle decode path passes NULL for
753 * 'unknown' lower/upper.
754 */
755 if (S_ISDIR(inode->i_mode) && strict) {
756 /* Real lower dir moved to upper layer under us? */
757 if (!lowerdentry && ovl_inode_lower(inode))
758 return false;
759
760 /* Lookup of an uncovered redirect origin? */
761 if (!upperdentry && ovl_inode_upper(inode))
762 return false;
763 }
764
765 /*
766 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
767 * This happens when finding a copied up overlay inode for a renamed
768 * or hardlinked overlay dentry and lower dentry cannot be followed
769 * by origin because lower fs does not support file handles.
770 */
771 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
772 return false;
773
774 /*
775 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
776 * This happens when finding a lower alias for a copied up hard link.
777 */
778 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
779 return false;
780
781 return true;
782}
783
784struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
785 bool is_upper)
786{
787 struct inode *inode, *key = d_inode(real);
788
789 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
790 if (!inode)
791 return NULL;
792
793 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
794 is_upper ? real : NULL, false)) {
795 iput(inode);
796 return ERR_PTR(-ESTALE);
797 }
798
799 return inode;
800}
801
802bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
803{
804 struct inode *key = d_inode(dir);
805 struct inode *trap;
806 bool res;
807
808 trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
809 if (!trap)
810 return false;
811
812 res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
813 !ovl_inode_lower(trap);
814
815 iput(trap);
816 return res;
817}
818
819/*
820 * Create an inode cache entry for layer root dir, that will intentionally
821 * fail ovl_verify_inode(), so any lookup that will find some layer root
822 * will fail.
823 */
824struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
825{
826 struct inode *key = d_inode(dir);
827 struct inode *trap;
828
829 if (!d_is_dir(dir))
830 return ERR_PTR(-ENOTDIR);
831
832 trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
833 ovl_inode_set, key);
834 if (!trap)
835 return ERR_PTR(-ENOMEM);
836
837 if (!(trap->i_state & I_NEW)) {
838 /* Conflicting layer roots? */
839 iput(trap);
840 return ERR_PTR(-ELOOP);
841 }
842
843 trap->i_mode = S_IFDIR;
844 trap->i_flags = S_DEAD;
845 unlock_new_inode(trap);
846
847 return trap;
848}
849
850/*
851 * Does overlay inode need to be hashed by lower inode?
852 */
853static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
854 struct dentry *lower, struct dentry *index)
855{
856 struct ovl_fs *ofs = sb->s_fs_info;
857
858 /* No, if pure upper */
859 if (!lower)
860 return false;
861
862 /* Yes, if already indexed */
863 if (index)
864 return true;
865
866 /* Yes, if won't be copied up */
867 if (!ofs->upper_mnt)
868 return true;
869
870 /* No, if lower hardlink is or will be broken on copy up */
871 if ((upper || !ovl_indexdir(sb)) &&
872 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
873 return false;
874
875 /* No, if non-indexed upper with NFS export */
876 if (sb->s_export_op && upper)
877 return false;
878
879 /* Otherwise, hash by lower inode for fsnotify */
880 return true;
881}
882
883static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
884 struct inode *key)
885{
886 return newinode ? inode_insert5(newinode, (unsigned long) key,
887 ovl_inode_test, ovl_inode_set, key) :
888 iget5_locked(sb, (unsigned long) key,
889 ovl_inode_test, ovl_inode_set, key);
890}
891
892struct inode *ovl_get_inode(struct super_block *sb,
893 struct ovl_inode_params *oip)
894{
895 struct dentry *upperdentry = oip->upperdentry;
896 struct ovl_path *lowerpath = oip->lowerpath;
897 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
898 struct inode *inode;
899 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
900 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
901 oip->index);
902 int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
903 bool is_dir, metacopy = false;
904 unsigned long ino = 0;
905 int err = oip->newinode ? -EEXIST : -ENOMEM;
906
907 if (!realinode)
908 realinode = d_inode(lowerdentry);
909
910 /*
911 * Copy up origin (lower) may exist for non-indexed upper, but we must
912 * not use lower as hash key if this is a broken hardlink.
913 */
914 is_dir = S_ISDIR(realinode->i_mode);
915 if (upperdentry || bylower) {
916 struct inode *key = d_inode(bylower ? lowerdentry :
917 upperdentry);
918 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
919
920 inode = ovl_iget5(sb, oip->newinode, key);
921 if (!inode)
922 goto out_err;
923 if (!(inode->i_state & I_NEW)) {
924 /*
925 * Verify that the underlying files stored in the inode
926 * match those in the dentry.
927 */
928 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
929 true)) {
930 iput(inode);
931 err = -ESTALE;
932 goto out_err;
933 }
934
935 dput(upperdentry);
936 kfree(oip->redirect);
937 goto out;
938 }
939
940 /* Recalculate nlink for non-dir due to indexing */
941 if (!is_dir)
942 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
943 set_nlink(inode, nlink);
944 ino = key->i_ino;
945 } else {
946 /* Lower hardlink that will be broken on copy up */
947 inode = new_inode(sb);
948 if (!inode) {
949 err = -ENOMEM;
950 goto out_err;
951 }
952 }
953 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
954 ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata);
955
956 if (upperdentry && ovl_is_impuredir(upperdentry))
957 ovl_set_flag(OVL_IMPURE, inode);
958
959 if (oip->index)
960 ovl_set_flag(OVL_INDEX, inode);
961
962 if (upperdentry) {
963 err = ovl_check_metacopy_xattr(upperdentry);
964 if (err < 0)
965 goto out_err;
966 metacopy = err;
967 if (!metacopy)
968 ovl_set_flag(OVL_UPPERDATA, inode);
969 }
970
971 OVL_I(inode)->redirect = oip->redirect;
972
973 if (bylower)
974 ovl_set_flag(OVL_CONST_INO, inode);
975
976 /* Check for non-merge dir that may have whiteouts */
977 if (is_dir) {
978 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
979 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
980 ovl_set_flag(OVL_WHITEOUTS, inode);
981 }
982 }
983
984 if (inode->i_state & I_NEW)
985 unlock_new_inode(inode);
986out:
987 return inode;
988
989out_err:
990 pr_warn_ratelimited("overlayfs: failed to get inode (%i)\n", err);
991 inode = ERR_PTR(err);
992 goto out;
993}