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