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