]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/overlayfs/inode.c
ovl: hash non-dir by lower inode for fsnotify
[mirror_ubuntu-bionic-kernel.git] / fs / overlayfs / inode.c
CommitLineData
e9be9d5e
MS
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>
5b825c3a 12#include <linux/cred.h>
e9be9d5e 13#include <linux/xattr.h>
5201dc44 14#include <linux/posix_acl.h>
5f8415d6 15#include <linux/ratelimit.h>
e9be9d5e
MS
16#include "overlayfs.h"
17
ba1e563c
CR
18
19static dev_t ovl_get_pseudo_dev(struct dentry *dentry)
20{
21 struct ovl_entry *oe = dentry->d_fsdata;
22
23 return oe->lowerstack[0].layer->pseudo_dev;
24}
25
e9be9d5e
MS
26int ovl_setattr(struct dentry *dentry, struct iattr *attr)
27{
28 int err;
29 struct dentry *upperdentry;
1175b6b8 30 const struct cred *old_cred;
e9be9d5e 31
cf9a6784
MS
32 /*
33 * Check for permissions before trying to copy-up. This is redundant
34 * since it will be rechecked later by ->setattr() on upper dentry. But
35 * without this, copy-up can be triggered by just about anybody.
36 *
37 * We don't initialize inode->size, which just means that
38 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
39 * check for a swapfile (which this won't be anyway).
40 */
31051c85 41 err = setattr_prepare(dentry, attr);
cf9a6784
MS
42 if (err)
43 return err;
44
e9be9d5e
MS
45 err = ovl_want_write(dentry);
46 if (err)
47 goto out;
48
acff81ec
MS
49 err = ovl_copy_up(dentry);
50 if (!err) {
51 upperdentry = ovl_dentry_upper(dentry);
52
b99c2d91
MS
53 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
54 attr->ia_valid &= ~ATTR_MODE;
55
5955102c 56 inode_lock(upperdentry->d_inode);
1175b6b8 57 old_cred = ovl_override_creds(dentry->d_sb);
e9be9d5e 58 err = notify_change(upperdentry, attr, NULL);
1175b6b8 59 revert_creds(old_cred);
b81de061
KK
60 if (!err)
61 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
5955102c 62 inode_unlock(upperdentry->d_inode);
e9be9d5e
MS
63 }
64 ovl_drop_write(dentry);
65out:
66 return err;
67}
68
5b712091
MS
69int ovl_getattr(const struct path *path, struct kstat *stat,
70 u32 request_mask, unsigned int flags)
e9be9d5e 71{
a528d35e 72 struct dentry *dentry = path->dentry;
72b608f0 73 enum ovl_path_type type;
e9be9d5e 74 struct path realpath;
1175b6b8 75 const struct cred *old_cred;
5b712091 76 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
a0c5ad30 77 bool samefs = ovl_same_sb(dentry->d_sb);
1175b6b8 78 int err;
e9be9d5e 79
72b608f0 80 type = ovl_path_real(dentry, &realpath);
1175b6b8 81 old_cred = ovl_override_creds(dentry->d_sb);
a528d35e 82 err = vfs_getattr(&realpath, stat, request_mask, flags);
72b608f0
AG
83 if (err)
84 goto out;
85
86 /*
a0c5ad30
AG
87 * For non-dir or same fs, we use st_ino of the copy up origin, if we
88 * know it. This guaranties constant st_dev/st_ino across copy up.
72b608f0
AG
89 *
90 * If filesystem supports NFS export ops, this also guaranties
91 * persistent st_ino across mount cycle.
92 */
a0c5ad30 93 if (!is_dir || samefs) {
72b608f0
AG
94 if (OVL_TYPE_ORIGIN(type)) {
95 struct kstat lowerstat;
5b712091 96 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
72b608f0
AG
97
98 ovl_path_lower(dentry, &realpath);
99 err = vfs_getattr(&realpath, &lowerstat,
5b712091 100 lowermask, flags);
72b608f0
AG
101 if (err)
102 goto out;
103
72b608f0 104 /*
359f392c 105 * Lower hardlinks may be broken on copy up to different
72b608f0
AG
106 * upper files, so we cannot use the lower origin st_ino
107 * for those different files, even for the same fs case.
359f392c
AG
108 * With inodes index enabled, it is safe to use st_ino
109 * of an indexed hardlinked origin. The index validates
110 * that the upper hardlink is not broken.
72b608f0 111 */
359f392c 112 if (is_dir || lowerstat.nlink == 1 ||
a39a8c30 113 ovl_test_flag(OVL_INDEX, d_inode(dentry))) {
72b608f0 114 stat->ino = lowerstat.ino;
a0c5ad30 115 stat->dev = ovl_get_pseudo_dev(dentry);
a39a8c30 116 }
72b608f0 117 }
a0c5ad30
AG
118 if (samefs) {
119 /*
120 * When all layers are on the same fs, all real inode
121 * number are unique, so we use the overlay st_dev,
122 * which is friendly to du -x.
123 */
124 stat->dev = dentry->d_sb->s_dev;
125 } else if (!OVL_TYPE_UPPER(type)) {
126 /*
127 * For non-samefs setup, to make sure that st_dev/st_ino
128 * pair is unique across the system, we use a unique
129 * anonymous st_dev for lower layer inode.
130 */
131 stat->dev = ovl_get_pseudo_dev(dentry);
132 }
133 } else {
5b712091 134 /*
5b712091
MS
135 * Always use the overlay st_dev for directories, so 'find
136 * -xdev' will scan the entire overlay mount and won't cross the
137 * overlay mount boundaries.
a0c5ad30
AG
138 *
139 * If not all layers are on the same fs the pair {real st_ino;
140 * overlay st_dev} is not unique, so use the non persistent
141 * overlay st_ino for directories.
5b712091
MS
142 */
143 stat->dev = dentry->d_sb->s_dev;
144 stat->ino = dentry->d_inode->i_ino;
72b608f0 145 }
5b712091
MS
146
147 /*
148 * It's probably not worth it to count subdirs to get the
149 * correct link count. nlink=1 seems to pacify 'find' and
150 * other utilities.
151 */
152 if (is_dir && OVL_TYPE_MERGE(type))
153 stat->nlink = 1;
154
5f8415d6
AG
155 /*
156 * Return the overlay inode nlinks for indexed upper inodes.
157 * Overlay inode nlink counts the union of the upper hardlinks
158 * and non-covered lower hardlinks. It does not include the upper
159 * index hardlink.
160 */
161 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
162 stat->nlink = dentry->d_inode->i_nlink;
163
72b608f0 164out:
1175b6b8 165 revert_creds(old_cred);
72b608f0 166
1175b6b8 167 return err;
e9be9d5e
MS
168}
169
170int ovl_permission(struct inode *inode, int mask)
171{
09d8b586
MS
172 struct inode *upperinode = ovl_inode_upper(inode);
173 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
e9be9d5e
MS
174 int err;
175
e9be9d5e 176 /* Careful in RCU walk mode */
e9be9d5e
MS
177 if (!realinode) {
178 WARN_ON(!(mask & MAY_NOT_BLOCK));
a999d7e1 179 return -ECHILD;
e9be9d5e
MS
180 }
181
c0ca3d70
VG
182 /*
183 * Check overlay inode with the creds of task and underlying inode
184 * with creds of mounter
185 */
186 err = generic_permission(inode, mask);
187 if (err)
188 return err;
189
09d8b586
MS
190 if (!upperinode &&
191 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
754f8cb7 192 mask &= ~(MAY_WRITE | MAY_APPEND);
500cac3c
VG
193 /* Make sure mounter can read file for copy up later */
194 mask |= MAY_READ;
195 }
a0b09f9b 196 err = ovl_creator_permission(inode->i_sb, realinode, mask);
c0ca3d70
VG
197
198 return err;
e9be9d5e
MS
199}
200
6b255391 201static const char *ovl_get_link(struct dentry *dentry,
fceef393
AV
202 struct inode *inode,
203 struct delayed_call *done)
e9be9d5e 204{
1175b6b8
VG
205 const struct cred *old_cred;
206 const char *p;
e9be9d5e 207
6b255391
AV
208 if (!dentry)
209 return ERR_PTR(-ECHILD);
210
1175b6b8 211 old_cred = ovl_override_creds(dentry->d_sb);
7764235b 212 p = vfs_get_link(ovl_dentry_real(dentry), done);
1175b6b8
VG
213 revert_creds(old_cred);
214 return p;
e9be9d5e
MS
215}
216
0956254a 217bool ovl_is_private_xattr(const char *name)
e9be9d5e 218{
fe2b7595
AG
219 return strncmp(name, OVL_XATTR_PREFIX,
220 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
e9be9d5e
MS
221}
222
1d88f183
MS
223int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
224 const void *value, size_t size, int flags)
e9be9d5e
MS
225{
226 int err;
1d88f183
MS
227 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
228 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
1175b6b8 229 const struct cred *old_cred;
e9be9d5e
MS
230
231 err = ovl_want_write(dentry);
232 if (err)
233 goto out;
234
1d88f183
MS
235 if (!value && !upperdentry) {
236 err = vfs_getxattr(realdentry, name, NULL, 0);
0e585ccc
AG
237 if (err < 0)
238 goto out_drop_write;
239 }
240
1d88f183
MS
241 if (!upperdentry) {
242 err = ovl_copy_up(dentry);
243 if (err)
244 goto out_drop_write;
e9be9d5e 245
1d88f183
MS
246 realdentry = ovl_dentry_upper(dentry);
247 }
0e585ccc 248
1175b6b8 249 old_cred = ovl_override_creds(dentry->d_sb);
0e585ccc 250 if (value)
1d88f183 251 err = vfs_setxattr(realdentry, name, value, size, flags);
0e585ccc
AG
252 else {
253 WARN_ON(flags != XATTR_REPLACE);
1d88f183 254 err = vfs_removexattr(realdentry, name);
0e585ccc 255 }
1175b6b8 256 revert_creds(old_cred);
e9be9d5e
MS
257
258out_drop_write:
259 ovl_drop_write(dentry);
260out:
261 return err;
262}
263
1d88f183 264int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
0eb45fc3 265 void *value, size_t size)
e9be9d5e 266{
1175b6b8
VG
267 ssize_t res;
268 const struct cred *old_cred;
1d88f183
MS
269 struct dentry *realdentry =
270 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
52148463 271
1175b6b8
VG
272 old_cred = ovl_override_creds(dentry->d_sb);
273 res = vfs_getxattr(realdentry, name, value, size);
274 revert_creds(old_cred);
275 return res;
e9be9d5e
MS
276}
277
a082c6f6
MS
278static bool ovl_can_list(const char *s)
279{
280 /* List all non-trusted xatts */
281 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
282 return true;
283
284 /* Never list trusted.overlay, list other trusted for superuser only */
285 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
286}
287
e9be9d5e
MS
288ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
289{
b581755b 290 struct dentry *realdentry = ovl_dentry_real(dentry);
e9be9d5e 291 ssize_t res;
7cb35119
MS
292 size_t len;
293 char *s;
1175b6b8 294 const struct cred *old_cred;
e9be9d5e 295
1175b6b8 296 old_cred = ovl_override_creds(dentry->d_sb);
b581755b 297 res = vfs_listxattr(realdentry, list, size);
1175b6b8 298 revert_creds(old_cred);
e9be9d5e
MS
299 if (res <= 0 || size == 0)
300 return res;
301
e9be9d5e 302 /* filter out private xattrs */
7cb35119
MS
303 for (s = list, len = res; len;) {
304 size_t slen = strnlen(s, len) + 1;
e9be9d5e 305
7cb35119
MS
306 /* underlying fs providing us with an broken xattr list? */
307 if (WARN_ON(slen > len))
308 return -EIO;
e9be9d5e 309
7cb35119 310 len -= slen;
a082c6f6 311 if (!ovl_can_list(s)) {
e9be9d5e 312 res -= slen;
7cb35119 313 memmove(s, s + slen, len);
e9be9d5e 314 } else {
7cb35119 315 s += slen;
e9be9d5e
MS
316 }
317 }
318
319 return res;
320}
321
39a25b2b
VG
322struct posix_acl *ovl_get_acl(struct inode *inode, int type)
323{
09d8b586 324 struct inode *realinode = ovl_inode_real(inode);
1175b6b8
VG
325 const struct cred *old_cred;
326 struct posix_acl *acl;
39a25b2b 327
5201dc44 328 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
39a25b2b
VG
329 return NULL;
330
1175b6b8 331 old_cred = ovl_override_creds(inode->i_sb);
5201dc44 332 acl = get_acl(realinode, type);
1175b6b8
VG
333 revert_creds(old_cred);
334
335 return acl;
39a25b2b
VG
336}
337
59be0971 338static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
e9be9d5e 339{
59be0971
AG
340 if (ovl_dentry_upper(dentry) &&
341 ovl_dentry_has_upper_alias(dentry))
e9be9d5e
MS
342 return false;
343
59be0971 344 if (special_file(d_inode(dentry)->i_mode))
e9be9d5e
MS
345 return false;
346
347 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
348 return false;
349
350 return true;
351}
352
2d902671 353int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
e9be9d5e 354{
2d902671 355 int err = 0;
e9be9d5e 356
59be0971 357 if (ovl_open_need_copy_up(dentry, file_flags)) {
e9be9d5e 358 err = ovl_want_write(dentry);
2d902671 359 if (!err) {
9aba6521 360 err = ovl_copy_up_flags(dentry, file_flags);
2d902671
MS
361 ovl_drop_write(dentry);
362 }
e9be9d5e
MS
363 }
364
2d902671 365 return err;
e9be9d5e
MS
366}
367
d719e8f2
MS
368int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
369{
370 struct dentry *alias;
371 struct path upperpath;
372
373 if (!(flags & S_ATIME))
374 return 0;
375
376 alias = d_find_any_alias(inode);
377 if (!alias)
378 return 0;
379
380 ovl_path_upper(alias, &upperpath);
381 if (upperpath.dentry) {
382 touch_atime(&upperpath);
383 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
384 }
385
386 dput(alias);
387
388 return 0;
389}
390
e9be9d5e
MS
391static const struct inode_operations ovl_file_inode_operations = {
392 .setattr = ovl_setattr,
393 .permission = ovl_permission,
394 .getattr = ovl_getattr,
e9be9d5e 395 .listxattr = ovl_listxattr,
39a25b2b 396 .get_acl = ovl_get_acl,
d719e8f2 397 .update_time = ovl_update_time,
e9be9d5e
MS
398};
399
400static const struct inode_operations ovl_symlink_inode_operations = {
401 .setattr = ovl_setattr,
6b255391 402 .get_link = ovl_get_link,
e9be9d5e 403 .getattr = ovl_getattr,
e9be9d5e 404 .listxattr = ovl_listxattr,
d719e8f2 405 .update_time = ovl_update_time,
e9be9d5e
MS
406};
407
b1eaa950
AG
408/*
409 * It is possible to stack overlayfs instance on top of another
410 * overlayfs instance as lower layer. We need to annonate the
411 * stackable i_mutex locks according to stack level of the super
412 * block instance. An overlayfs instance can never be in stack
413 * depth 0 (there is always a real fs below it). An overlayfs
414 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
415 *
416 * For example, here is a snip from /proc/lockdep_chains after
417 * dir_iterate of nested overlayfs:
418 *
419 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
420 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
421 * [...] &type->i_mutex_dir_key (stack_depth=0)
422 */
423#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
424
425static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
426{
427#ifdef CONFIG_LOCKDEP
428 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
429 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
4eae06de 430 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
b1eaa950
AG
431
432 int depth = inode->i_sb->s_stack_depth - 1;
433
434 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
435 depth = 0;
436
437 if (S_ISDIR(inode->i_mode))
438 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
439 else
440 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
4eae06de
AG
441
442 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
b1eaa950
AG
443#endif
444}
445
ca4c8a3a 446static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
e9be9d5e 447{
e9be9d5e
MS
448 inode->i_ino = get_next_ino();
449 inode->i_mode = mode;
d719e8f2 450 inode->i_flags |= S_NOCMTIME;
2a3a2a3f
MS
451#ifdef CONFIG_FS_POSIX_ACL
452 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
453#endif
e9be9d5e 454
b1eaa950
AG
455 ovl_lockdep_annotate_inode_mutex_key(inode);
456
ca4c8a3a
MS
457 switch (mode & S_IFMT) {
458 case S_IFREG:
459 inode->i_op = &ovl_file_inode_operations;
460 break;
461
e9be9d5e 462 case S_IFDIR:
e9be9d5e
MS
463 inode->i_op = &ovl_dir_inode_operations;
464 inode->i_fop = &ovl_dir_operations;
465 break;
466
467 case S_IFLNK:
468 inode->i_op = &ovl_symlink_inode_operations;
469 break;
470
51f7e52d 471 default:
e9be9d5e 472 inode->i_op = &ovl_file_inode_operations;
ca4c8a3a 473 init_special_inode(inode, mode, rdev);
e9be9d5e 474 break;
51f7e52d
MS
475 }
476}
e9be9d5e 477
5f8415d6
AG
478/*
479 * With inodes index enabled, an overlay inode nlink counts the union of upper
480 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
481 * upper inode, the following nlink modifying operations can happen:
482 *
483 * 1. Lower hardlink copy up
484 * 2. Upper hardlink created, unlinked or renamed over
485 * 3. Lower hardlink whiteout or renamed over
486 *
487 * For the first, copy up case, the union nlink does not change, whether the
488 * operation succeeds or fails, but the upper inode nlink may change.
489 * Therefore, before copy up, we store the union nlink value relative to the
490 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
491 *
492 * For the second, upper hardlink case, the union nlink should be incremented
493 * or decremented IFF the operation succeeds, aligned with nlink change of the
494 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
495 * value relative to the upper inode nlink in the index inode.
496 *
497 * For the last, lower cover up case, we simplify things by preceding the
498 * whiteout or cover up with copy up. This makes sure that there is an index
499 * upper inode where the nlink xattr can be stored before the copied up upper
500 * entry is unlink.
501 */
502#define OVL_NLINK_ADD_UPPER (1 << 0)
503
504/*
505 * On-disk format for indexed nlink:
506 *
507 * nlink relative to the upper inode - "U[+-]NUM"
508 * nlink relative to the lower inode - "L[+-]NUM"
509 */
510
511static int ovl_set_nlink_common(struct dentry *dentry,
512 struct dentry *realdentry, const char *format)
513{
514 struct inode *inode = d_inode(dentry);
515 struct inode *realinode = d_inode(realdentry);
516 char buf[13];
517 int len;
518
519 len = snprintf(buf, sizeof(buf), format,
520 (int) (inode->i_nlink - realinode->i_nlink));
521
6787341a
MS
522 if (WARN_ON(len >= sizeof(buf)))
523 return -EIO;
524
5f8415d6
AG
525 return ovl_do_setxattr(ovl_dentry_upper(dentry),
526 OVL_XATTR_NLINK, buf, len, 0);
527}
528
529int ovl_set_nlink_upper(struct dentry *dentry)
530{
531 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
532}
533
534int ovl_set_nlink_lower(struct dentry *dentry)
535{
536 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
537}
538
caf70cb2
AG
539unsigned int ovl_get_nlink(struct dentry *lowerdentry,
540 struct dentry *upperdentry,
541 unsigned int fallback)
5f8415d6
AG
542{
543 int nlink_diff;
544 int nlink;
545 char buf[13];
546 int err;
547
548 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
549 return fallback;
550
551 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
552 if (err < 0)
553 goto fail;
554
555 buf[err] = '\0';
556 if ((buf[0] != 'L' && buf[0] != 'U') ||
557 (buf[1] != '+' && buf[1] != '-'))
558 goto fail;
559
560 err = kstrtoint(buf + 1, 10, &nlink_diff);
561 if (err < 0)
562 goto fail;
563
564 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
565 nlink += nlink_diff;
566
567 if (nlink <= 0)
568 goto fail;
569
570 return nlink;
571
572fail:
573 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
574 upperdentry, err);
575 return fallback;
576}
577
ca4c8a3a 578struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
51f7e52d
MS
579{
580 struct inode *inode;
581
582 inode = new_inode(sb);
583 if (inode)
ca4c8a3a 584 ovl_fill_inode(inode, mode, rdev);
51f7e52d
MS
585
586 return inode;
587}
588
589static int ovl_inode_test(struct inode *inode, void *data)
590{
25b7713a 591 return inode->i_private == data;
51f7e52d
MS
592}
593
594static int ovl_inode_set(struct inode *inode, void *data)
595{
25b7713a 596 inode->i_private = data;
51f7e52d
MS
597 return 0;
598}
599
b9ac5c27
MS
600static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
601 struct dentry *upperdentry)
602{
d2c9584e
AG
603 if (S_ISDIR(inode->i_mode)) {
604 /* Real lower dir moved to upper layer under us? */
605 if (!lowerdentry && ovl_inode_lower(inode))
606 return false;
607
608 /* Lookup of an uncovered redirect origin? */
609 if (!upperdentry && ovl_inode_upper(inode))
610 return false;
611 }
612
939ae4ef
AG
613 /*
614 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
615 * This happens when finding a copied up overlay inode for a renamed
616 * or hardlinked overlay dentry and lower dentry cannot be followed
617 * by origin because lower fs does not support file handles.
618 */
619 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
b9ac5c27
MS
620 return false;
621
622 /*
623 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
624 * This happens when finding a lower alias for a copied up hard link.
625 */
626 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
627 return false;
628
629 return true;
630}
631
cd3d03d0
AG
632/*
633 * Does overlay inode need to be hashed by lower inode?
634 */
635static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
636 struct dentry *lower, struct dentry *index)
637{
638 struct ovl_fs *ofs = sb->s_fs_info;
639
640 /* No, if pure upper */
641 if (!lower)
642 return false;
643
644 /* Yes, if already indexed */
645 if (index)
646 return true;
647
648 /* Yes, if won't be copied up */
649 if (!ofs->upper_mnt)
650 return true;
651
652 /* No, if lower hardlink is or will be broken on copy up */
653 if ((upper || !ovl_indexdir(sb)) &&
654 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
655 return false;
656
657 /* No, if non-indexed upper with NFS export */
658 if (sb->s_export_op && upper)
659 return false;
660
661 /* Otherwise, hash by lower inode for fsnotify */
662 return true;
663}
664
6eaf0111
AG
665struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry,
666 struct dentry *index)
51f7e52d 667{
cd3d03d0 668 struct super_block *sb = dentry->d_sb;
09d8b586
MS
669 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
670 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
51f7e52d 671 struct inode *inode;
cd3d03d0 672 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, index);
d2c9584e 673 bool is_dir;
6eaf0111 674
09d8b586
MS
675 if (!realinode)
676 realinode = d_inode(lowerdentry);
677
6eaf0111 678 /*
cd3d03d0
AG
679 * Copy up origin (lower) may exist for non-indexed upper, but we must
680 * not use lower as hash key if this is a broken hardlink.
6eaf0111 681 */
d2c9584e 682 is_dir = S_ISDIR(realinode->i_mode);
cd3d03d0
AG
683 if (upperdentry || bylower) {
684 struct inode *key = d_inode(bylower ? lowerdentry :
685 upperdentry);
d2c9584e 686 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
b9ac5c27 687
cd3d03d0 688 inode = iget5_locked(sb, (unsigned long) key,
b9ac5c27 689 ovl_inode_test, ovl_inode_set, key);
09d8b586 690 if (!inode)
b9ac5c27 691 goto out_nomem;
09d8b586 692 if (!(inode->i_state & I_NEW)) {
b9ac5c27
MS
693 /*
694 * Verify that the underlying files stored in the inode
695 * match those in the dentry.
696 */
697 if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) {
698 iput(inode);
699 inode = ERR_PTR(-ESTALE);
700 goto out;
701 }
702
09d8b586
MS
703 dput(upperdentry);
704 goto out;
705 }
e6d2ebdd 706
d2c9584e
AG
707 /* Recalculate nlink for non-dir due to indexing */
708 if (!is_dir)
709 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
5f8415d6 710 set_nlink(inode, nlink);
e6d2ebdd 711 } else {
cd3d03d0
AG
712 /* Lower hardlink that will be broken on copy up */
713 inode = new_inode(sb);
e6d2ebdd 714 if (!inode)
b9ac5c27 715 goto out_nomem;
e9be9d5e 716 }
e6d2ebdd 717 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
09d8b586 718 ovl_inode_init(inode, upperdentry, lowerdentry);
13c72075
MS
719
720 if (upperdentry && ovl_is_impuredir(upperdentry))
721 ovl_set_flag(OVL_IMPURE, inode);
722
b79e05aa 723 /* Check for non-merge dir that may have whiteouts */
d2c9584e 724 if (is_dir) {
b79e05aa
AG
725 struct ovl_entry *oe = dentry->d_fsdata;
726
727 if (((upperdentry && lowerdentry) || oe->numlower > 1) ||
728 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
729 ovl_set_flag(OVL_WHITEOUTS, inode);
730 }
731 }
732
e6d2ebdd
MS
733 if (inode->i_state & I_NEW)
734 unlock_new_inode(inode);
735out:
e9be9d5e 736 return inode;
b9ac5c27
MS
737
738out_nomem:
739 inode = ERR_PTR(-ENOMEM);
740 goto out;
e9be9d5e 741}