]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/overlayfs/inode.c
USB: serial: whiteheat: fix line-speed endianness
[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 */
90481f7a
MS
285 return !ovl_is_private_xattr(s) &&
286 ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
a082c6f6
MS
287}
288
e9be9d5e
MS
289ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
290{
b581755b 291 struct dentry *realdentry = ovl_dentry_real(dentry);
e9be9d5e 292 ssize_t res;
7cb35119
MS
293 size_t len;
294 char *s;
1175b6b8 295 const struct cred *old_cred;
e9be9d5e 296
1175b6b8 297 old_cred = ovl_override_creds(dentry->d_sb);
b581755b 298 res = vfs_listxattr(realdentry, list, size);
1175b6b8 299 revert_creds(old_cred);
e9be9d5e
MS
300 if (res <= 0 || size == 0)
301 return res;
302
e9be9d5e 303 /* filter out private xattrs */
7cb35119
MS
304 for (s = list, len = res; len;) {
305 size_t slen = strnlen(s, len) + 1;
e9be9d5e 306
7cb35119
MS
307 /* underlying fs providing us with an broken xattr list? */
308 if (WARN_ON(slen > len))
309 return -EIO;
e9be9d5e 310
7cb35119 311 len -= slen;
a082c6f6 312 if (!ovl_can_list(s)) {
e9be9d5e 313 res -= slen;
7cb35119 314 memmove(s, s + slen, len);
e9be9d5e 315 } else {
7cb35119 316 s += slen;
e9be9d5e
MS
317 }
318 }
319
320 return res;
321}
322
39a25b2b
VG
323struct posix_acl *ovl_get_acl(struct inode *inode, int type)
324{
09d8b586 325 struct inode *realinode = ovl_inode_real(inode);
1175b6b8
VG
326 const struct cred *old_cred;
327 struct posix_acl *acl;
39a25b2b 328
5201dc44 329 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
39a25b2b
VG
330 return NULL;
331
1175b6b8 332 old_cred = ovl_override_creds(inode->i_sb);
5201dc44 333 acl = get_acl(realinode, type);
1175b6b8
VG
334 revert_creds(old_cred);
335
336 return acl;
39a25b2b
VG
337}
338
59be0971 339static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
e9be9d5e 340{
59be0971
AG
341 if (ovl_dentry_upper(dentry) &&
342 ovl_dentry_has_upper_alias(dentry))
e9be9d5e
MS
343 return false;
344
59be0971 345 if (special_file(d_inode(dentry)->i_mode))
e9be9d5e
MS
346 return false;
347
348 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
349 return false;
350
351 return true;
352}
353
2d902671 354int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
e9be9d5e 355{
2d902671 356 int err = 0;
e9be9d5e 357
59be0971 358 if (ovl_open_need_copy_up(dentry, file_flags)) {
e9be9d5e 359 err = ovl_want_write(dentry);
2d902671 360 if (!err) {
9aba6521 361 err = ovl_copy_up_flags(dentry, file_flags);
2d902671
MS
362 ovl_drop_write(dentry);
363 }
e9be9d5e
MS
364 }
365
2d902671 366 return err;
e9be9d5e
MS
367}
368
d719e8f2
MS
369int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
370{
371 struct dentry *alias;
372 struct path upperpath;
373
374 if (!(flags & S_ATIME))
375 return 0;
376
377 alias = d_find_any_alias(inode);
378 if (!alias)
379 return 0;
380
381 ovl_path_upper(alias, &upperpath);
382 if (upperpath.dentry) {
383 touch_atime(&upperpath);
384 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
385 }
386
387 dput(alias);
388
389 return 0;
390}
391
e9be9d5e
MS
392static const struct inode_operations ovl_file_inode_operations = {
393 .setattr = ovl_setattr,
394 .permission = ovl_permission,
395 .getattr = ovl_getattr,
e9be9d5e 396 .listxattr = ovl_listxattr,
39a25b2b 397 .get_acl = ovl_get_acl,
d719e8f2 398 .update_time = ovl_update_time,
e9be9d5e
MS
399};
400
401static const struct inode_operations ovl_symlink_inode_operations = {
402 .setattr = ovl_setattr,
6b255391 403 .get_link = ovl_get_link,
e9be9d5e 404 .getattr = ovl_getattr,
e9be9d5e 405 .listxattr = ovl_listxattr,
d719e8f2 406 .update_time = ovl_update_time,
e9be9d5e
MS
407};
408
b1eaa950
AG
409/*
410 * It is possible to stack overlayfs instance on top of another
411 * overlayfs instance as lower layer. We need to annonate the
412 * stackable i_mutex locks according to stack level of the super
413 * block instance. An overlayfs instance can never be in stack
414 * depth 0 (there is always a real fs below it). An overlayfs
415 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
416 *
417 * For example, here is a snip from /proc/lockdep_chains after
418 * dir_iterate of nested overlayfs:
419 *
420 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
421 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
422 * [...] &type->i_mutex_dir_key (stack_depth=0)
423 */
424#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
425
426static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
427{
428#ifdef CONFIG_LOCKDEP
429 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
430 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
4eae06de 431 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
b1eaa950
AG
432
433 int depth = inode->i_sb->s_stack_depth - 1;
434
435 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
436 depth = 0;
437
438 if (S_ISDIR(inode->i_mode))
439 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
440 else
441 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
4eae06de
AG
442
443 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
b1eaa950
AG
444#endif
445}
446
ca4c8a3a 447static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
e9be9d5e 448{
e9be9d5e
MS
449 inode->i_ino = get_next_ino();
450 inode->i_mode = mode;
d719e8f2 451 inode->i_flags |= S_NOCMTIME;
2a3a2a3f
MS
452#ifdef CONFIG_FS_POSIX_ACL
453 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
454#endif
e9be9d5e 455
b1eaa950
AG
456 ovl_lockdep_annotate_inode_mutex_key(inode);
457
ca4c8a3a
MS
458 switch (mode & S_IFMT) {
459 case S_IFREG:
460 inode->i_op = &ovl_file_inode_operations;
461 break;
462
e9be9d5e 463 case S_IFDIR:
e9be9d5e
MS
464 inode->i_op = &ovl_dir_inode_operations;
465 inode->i_fop = &ovl_dir_operations;
466 break;
467
468 case S_IFLNK:
469 inode->i_op = &ovl_symlink_inode_operations;
470 break;
471
51f7e52d 472 default:
e9be9d5e 473 inode->i_op = &ovl_file_inode_operations;
ca4c8a3a 474 init_special_inode(inode, mode, rdev);
e9be9d5e 475 break;
51f7e52d
MS
476 }
477}
e9be9d5e 478
5f8415d6
AG
479/*
480 * With inodes index enabled, an overlay inode nlink counts the union of upper
481 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
482 * upper inode, the following nlink modifying operations can happen:
483 *
484 * 1. Lower hardlink copy up
485 * 2. Upper hardlink created, unlinked or renamed over
486 * 3. Lower hardlink whiteout or renamed over
487 *
488 * For the first, copy up case, the union nlink does not change, whether the
489 * operation succeeds or fails, but the upper inode nlink may change.
490 * Therefore, before copy up, we store the union nlink value relative to the
491 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
492 *
493 * For the second, upper hardlink case, the union nlink should be incremented
494 * or decremented IFF the operation succeeds, aligned with nlink change of the
495 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
496 * value relative to the upper inode nlink in the index inode.
497 *
498 * For the last, lower cover up case, we simplify things by preceding the
499 * whiteout or cover up with copy up. This makes sure that there is an index
500 * upper inode where the nlink xattr can be stored before the copied up upper
501 * entry is unlink.
502 */
503#define OVL_NLINK_ADD_UPPER (1 << 0)
504
505/*
506 * On-disk format for indexed nlink:
507 *
508 * nlink relative to the upper inode - "U[+-]NUM"
509 * nlink relative to the lower inode - "L[+-]NUM"
510 */
511
512static int ovl_set_nlink_common(struct dentry *dentry,
513 struct dentry *realdentry, const char *format)
514{
515 struct inode *inode = d_inode(dentry);
516 struct inode *realinode = d_inode(realdentry);
517 char buf[13];
518 int len;
519
520 len = snprintf(buf, sizeof(buf), format,
521 (int) (inode->i_nlink - realinode->i_nlink));
522
6787341a
MS
523 if (WARN_ON(len >= sizeof(buf)))
524 return -EIO;
525
5f8415d6
AG
526 return ovl_do_setxattr(ovl_dentry_upper(dentry),
527 OVL_XATTR_NLINK, buf, len, 0);
528}
529
530int ovl_set_nlink_upper(struct dentry *dentry)
531{
532 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
533}
534
535int ovl_set_nlink_lower(struct dentry *dentry)
536{
537 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
538}
539
caf70cb2
AG
540unsigned int ovl_get_nlink(struct dentry *lowerdentry,
541 struct dentry *upperdentry,
542 unsigned int fallback)
5f8415d6
AG
543{
544 int nlink_diff;
545 int nlink;
546 char buf[13];
547 int err;
548
549 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
550 return fallback;
551
552 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
553 if (err < 0)
554 goto fail;
555
556 buf[err] = '\0';
557 if ((buf[0] != 'L' && buf[0] != 'U') ||
558 (buf[1] != '+' && buf[1] != '-'))
559 goto fail;
560
561 err = kstrtoint(buf + 1, 10, &nlink_diff);
562 if (err < 0)
563 goto fail;
564
565 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
566 nlink += nlink_diff;
567
568 if (nlink <= 0)
569 goto fail;
570
571 return nlink;
572
573fail:
574 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
575 upperdentry, err);
576 return fallback;
577}
578
ca4c8a3a 579struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
51f7e52d
MS
580{
581 struct inode *inode;
582
583 inode = new_inode(sb);
584 if (inode)
ca4c8a3a 585 ovl_fill_inode(inode, mode, rdev);
51f7e52d
MS
586
587 return inode;
588}
589
590static int ovl_inode_test(struct inode *inode, void *data)
591{
25b7713a 592 return inode->i_private == data;
51f7e52d
MS
593}
594
595static int ovl_inode_set(struct inode *inode, void *data)
596{
25b7713a 597 inode->i_private = data;
51f7e52d
MS
598 return 0;
599}
600
b9ac5c27
MS
601static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
602 struct dentry *upperdentry)
603{
d2c9584e
AG
604 if (S_ISDIR(inode->i_mode)) {
605 /* Real lower dir moved to upper layer under us? */
606 if (!lowerdentry && ovl_inode_lower(inode))
607 return false;
608
609 /* Lookup of an uncovered redirect origin? */
610 if (!upperdentry && ovl_inode_upper(inode))
611 return false;
612 }
613
939ae4ef
AG
614 /*
615 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
616 * This happens when finding a copied up overlay inode for a renamed
617 * or hardlinked overlay dentry and lower dentry cannot be followed
618 * by origin because lower fs does not support file handles.
619 */
620 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
b9ac5c27
MS
621 return false;
622
623 /*
624 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
625 * This happens when finding a lower alias for a copied up hard link.
626 */
627 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
628 return false;
629
630 return true;
631}
632
cd3d03d0
AG
633/*
634 * Does overlay inode need to be hashed by lower inode?
635 */
636static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
637 struct dentry *lower, struct dentry *index)
638{
639 struct ovl_fs *ofs = sb->s_fs_info;
640
641 /* No, if pure upper */
642 if (!lower)
643 return false;
644
645 /* Yes, if already indexed */
646 if (index)
647 return true;
648
649 /* Yes, if won't be copied up */
650 if (!ofs->upper_mnt)
651 return true;
652
653 /* No, if lower hardlink is or will be broken on copy up */
654 if ((upper || !ovl_indexdir(sb)) &&
655 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
656 return false;
657
658 /* No, if non-indexed upper with NFS export */
659 if (sb->s_export_op && upper)
660 return false;
661
662 /* Otherwise, hash by lower inode for fsnotify */
663 return true;
664}
665
6eaf0111
AG
666struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry,
667 struct dentry *index)
51f7e52d 668{
cd3d03d0 669 struct super_block *sb = dentry->d_sb;
09d8b586
MS
670 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
671 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
51f7e52d 672 struct inode *inode;
cd3d03d0 673 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, index);
d2c9584e 674 bool is_dir;
6eaf0111 675
09d8b586
MS
676 if (!realinode)
677 realinode = d_inode(lowerdentry);
678
6eaf0111 679 /*
cd3d03d0
AG
680 * Copy up origin (lower) may exist for non-indexed upper, but we must
681 * not use lower as hash key if this is a broken hardlink.
6eaf0111 682 */
d2c9584e 683 is_dir = S_ISDIR(realinode->i_mode);
cd3d03d0
AG
684 if (upperdentry || bylower) {
685 struct inode *key = d_inode(bylower ? lowerdentry :
686 upperdentry);
d2c9584e 687 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
b9ac5c27 688
cd3d03d0 689 inode = iget5_locked(sb, (unsigned long) key,
b9ac5c27 690 ovl_inode_test, ovl_inode_set, key);
09d8b586 691 if (!inode)
b9ac5c27 692 goto out_nomem;
09d8b586 693 if (!(inode->i_state & I_NEW)) {
b9ac5c27
MS
694 /*
695 * Verify that the underlying files stored in the inode
696 * match those in the dentry.
697 */
698 if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) {
699 iput(inode);
700 inode = ERR_PTR(-ESTALE);
701 goto out;
702 }
703
09d8b586
MS
704 dput(upperdentry);
705 goto out;
706 }
e6d2ebdd 707
d2c9584e
AG
708 /* Recalculate nlink for non-dir due to indexing */
709 if (!is_dir)
710 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
5f8415d6 711 set_nlink(inode, nlink);
e6d2ebdd 712 } else {
cd3d03d0
AG
713 /* Lower hardlink that will be broken on copy up */
714 inode = new_inode(sb);
e6d2ebdd 715 if (!inode)
b9ac5c27 716 goto out_nomem;
e9be9d5e 717 }
e6d2ebdd 718 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
09d8b586 719 ovl_inode_init(inode, upperdentry, lowerdentry);
13c72075
MS
720
721 if (upperdentry && ovl_is_impuredir(upperdentry))
722 ovl_set_flag(OVL_IMPURE, inode);
723
b79e05aa 724 /* Check for non-merge dir that may have whiteouts */
d2c9584e 725 if (is_dir) {
b79e05aa
AG
726 struct ovl_entry *oe = dentry->d_fsdata;
727
728 if (((upperdentry && lowerdentry) || oe->numlower > 1) ||
729 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
730 ovl_set_flag(OVL_WHITEOUTS, inode);
731 }
732 }
733
e6d2ebdd
MS
734 if (inode->i_state & I_NEW)
735 unlock_new_inode(inode);
736out:
e9be9d5e 737 return inode;
b9ac5c27
MS
738
739out_nomem:
740 inode = ERR_PTR(-ENOMEM);
741 goto out;
e9be9d5e 742}