ovl: fix out of date comment and unreachable code
[linux-block.git] / fs / overlayfs / inode.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
e9be9d5e
MS
2/*
3 *
4 * Copyright (C) 2011 Novell Inc.
e9be9d5e
MS
5 */
6
7#include <linux/fs.h>
8#include <linux/slab.h>
5b825c3a 9#include <linux/cred.h>
e9be9d5e 10#include <linux/xattr.h>
5201dc44 11#include <linux/posix_acl.h>
5f8415d6 12#include <linux/ratelimit.h>
e9be9d5e
MS
13#include "overlayfs.h"
14
ba1e563c 15
e9be9d5e
MS
16int ovl_setattr(struct dentry *dentry, struct iattr *attr)
17{
18 int err;
997336f2 19 bool full_copy_up = false;
e9be9d5e 20 struct dentry *upperdentry;
1175b6b8 21 const struct cred *old_cred;
e9be9d5e 22
31051c85 23 err = setattr_prepare(dentry, attr);
cf9a6784
MS
24 if (err)
25 return err;
26
e9be9d5e
MS
27 err = ovl_want_write(dentry);
28 if (err)
29 goto out;
30
5812160e
MS
31 if (attr->ia_valid & ATTR_SIZE) {
32 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
33
34 err = -ETXTBSY;
35 if (atomic_read(&realinode->i_writecount) < 0)
36 goto out_drop_write;
997336f2
VG
37
38 /* Truncate should trigger data copy up as well */
39 full_copy_up = true;
5812160e
MS
40 }
41
997336f2
VG
42 if (!full_copy_up)
43 err = ovl_copy_up(dentry);
44 else
45 err = ovl_copy_up_with_data(dentry);
acff81ec 46 if (!err) {
5812160e
MS
47 struct inode *winode = NULL;
48
acff81ec
MS
49 upperdentry = ovl_dentry_upper(dentry);
50
5812160e
MS
51 if (attr->ia_valid & ATTR_SIZE) {
52 winode = d_inode(upperdentry);
53 err = get_write_access(winode);
54 if (err)
55 goto out_drop_write;
56 }
57
b99c2d91
MS
58 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
59 attr->ia_valid &= ~ATTR_MODE;
60
5955102c 61 inode_lock(upperdentry->d_inode);
1175b6b8 62 old_cred = ovl_override_creds(dentry->d_sb);
e9be9d5e 63 err = notify_change(upperdentry, attr, NULL);
1175b6b8 64 revert_creds(old_cred);
b81de061
KK
65 if (!err)
66 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
5955102c 67 inode_unlock(upperdentry->d_inode);
5812160e
MS
68
69 if (winode)
70 put_write_access(winode);
e9be9d5e 71 }
5812160e 72out_drop_write:
e9be9d5e
MS
73 ovl_drop_write(dentry);
74out:
75 return err;
76}
77
07f1e596 78static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid)
da309e8c 79{
0f831ec8 80 bool samefs = ovl_same_fs(dentry->d_sb);
e487d889 81 unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
da309e8c
AG
82
83 if (samefs) {
84 /*
85 * When all layers are on the same fs, all real inode
86 * number are unique, so we use the overlay st_dev,
87 * which is friendly to du -x.
88 */
89 stat->dev = dentry->d_sb->s_dev;
e487d889
AG
90 return 0;
91 } else if (xinobits) {
92 unsigned int shift = 64 - xinobits;
93 /*
94 * All inode numbers of underlying fs should not be using the
95 * high xinobits, so we use high xinobits to partition the
96 * overlay st_ino address space. The high bits holds the fsid
97 * (upper fsid is 0). This way overlay inode numbers are unique
98 * and all inodes use overlay st_dev. Inode numbers are also
99 * persistent for a given layer configuration.
100 */
101 if (stat->ino >> shift) {
1bd0a3ae 102 pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
e487d889
AG
103 dentry, stat->ino, xinobits);
104 } else {
07f1e596 105 stat->ino |= ((u64)fsid) << shift;
e487d889
AG
106 stat->dev = dentry->d_sb->s_dev;
107 return 0;
108 }
109 }
110
111 /* The inode could not be mapped to a unified st_ino address space */
112 if (S_ISDIR(dentry->d_inode->i_mode)) {
da309e8c
AG
113 /*
114 * Always use the overlay st_dev for directories, so 'find
115 * -xdev' will scan the entire overlay mount and won't cross the
116 * overlay mount boundaries.
117 *
118 * If not all layers are on the same fs the pair {real st_ino;
119 * overlay st_dev} is not unique, so use the non persistent
120 * overlay st_ino for directories.
121 */
122 stat->dev = dentry->d_sb->s_dev;
123 stat->ino = dentry->d_inode->i_ino;
b7bf9908 124 } else {
da309e8c
AG
125 /*
126 * For non-samefs setup, if we cannot map all layers st_ino
127 * to a unified address space, we need to make sure that st_dev
b7bf9908
AG
128 * is unique per underlying fs, so we use the unique anonymous
129 * bdev assigned to the underlying fs.
da309e8c 130 */
07f1e596 131 stat->dev = OVL_FS(dentry->d_sb)->fs[fsid].pseudo_dev;
da309e8c
AG
132 }
133
134 return 0;
135}
136
5b712091
MS
137int ovl_getattr(const struct path *path, struct kstat *stat,
138 u32 request_mask, unsigned int flags)
e9be9d5e 139{
a528d35e 140 struct dentry *dentry = path->dentry;
72b608f0 141 enum ovl_path_type type;
e9be9d5e 142 struct path realpath;
1175b6b8 143 const struct cred *old_cred;
5b712091 144 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
07f1e596 145 int fsid = 0;
1175b6b8 146 int err;
67d756c2
VG
147 bool metacopy_blocks = false;
148
149 metacopy_blocks = ovl_is_metacopy_dentry(dentry);
e9be9d5e 150
72b608f0 151 type = ovl_path_real(dentry, &realpath);
1175b6b8 152 old_cred = ovl_override_creds(dentry->d_sb);
a528d35e 153 err = vfs_getattr(&realpath, stat, request_mask, flags);
72b608f0
AG
154 if (err)
155 goto out;
156
157 /*
da309e8c
AG
158 * For non-dir or same fs, we use st_ino of the copy up origin.
159 * This guaranties constant st_dev/st_ino across copy up.
e487d889
AG
160 * With xino feature and non-samefs, we use st_ino of the copy up
161 * origin masked with high bits that represent the layer id.
72b608f0 162 *
da309e8c 163 * If lower filesystem supports NFS file handles, this also guaranties
72b608f0
AG
164 * persistent st_ino across mount cycle.
165 */
0f831ec8 166 if (!is_dir || ovl_same_dev(dentry->d_sb)) {
da309e8c 167 if (!OVL_TYPE_UPPER(type)) {
07f1e596 168 fsid = ovl_layer_lower(dentry)->fsid;
da309e8c 169 } else if (OVL_TYPE_ORIGIN(type)) {
72b608f0 170 struct kstat lowerstat;
67d756c2
VG
171 u32 lowermask = STATX_INO | STATX_BLOCKS |
172 (!is_dir ? STATX_NLINK : 0);
72b608f0
AG
173
174 ovl_path_lower(dentry, &realpath);
175 err = vfs_getattr(&realpath, &lowerstat,
5b712091 176 lowermask, flags);
72b608f0
AG
177 if (err)
178 goto out;
179
72b608f0 180 /*
359f392c 181 * Lower hardlinks may be broken on copy up to different
72b608f0
AG
182 * upper files, so we cannot use the lower origin st_ino
183 * for those different files, even for the same fs case.
86eaa130
AG
184 *
185 * Similarly, several redirected dirs can point to the
186 * same dir on a lower layer. With the "verify_lower"
187 * feature, we do not use the lower origin st_ino, if
188 * we haven't verified that this redirect is unique.
189 *
359f392c 190 * With inodes index enabled, it is safe to use st_ino
86eaa130
AG
191 * of an indexed origin. The index validates that the
192 * upper hardlink is not broken and that a redirected
193 * dir is the only redirect to that origin.
72b608f0 194 */
86eaa130
AG
195 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
196 (!ovl_verify_lower(dentry->d_sb) &&
9f99e50d 197 (is_dir || lowerstat.nlink == 1))) {
07f1e596 198 fsid = ovl_layer_lower(dentry)->fsid;
b7bf9908 199 stat->ino = lowerstat.ino;
9f99e50d 200 }
67d756c2
VG
201
202 /*
203 * If we are querying a metacopy dentry and lower
204 * dentry is data dentry, then use the blocks we
205 * queried just now. We don't have to do additional
206 * vfs_getattr(). If lower itself is metacopy, then
207 * additional vfs_getattr() is unavoidable.
208 */
209 if (metacopy_blocks &&
210 realpath.dentry == ovl_dentry_lowerdata(dentry)) {
211 stat->blocks = lowerstat.blocks;
212 metacopy_blocks = false;
213 }
214 }
215
216 if (metacopy_blocks) {
217 /*
218 * If lower is not same as lowerdata or if there was
219 * no origin on upper, we can end up here.
220 */
221 struct kstat lowerdatastat;
222 u32 lowermask = STATX_BLOCKS;
223
224 ovl_path_lowerdata(dentry, &realpath);
225 err = vfs_getattr(&realpath, &lowerdatastat,
226 lowermask, flags);
227 if (err)
228 goto out;
229 stat->blocks = lowerdatastat.blocks;
72b608f0 230 }
72b608f0 231 }
5b712091 232
07f1e596 233 err = ovl_map_dev_ino(dentry, stat, fsid);
da309e8c
AG
234 if (err)
235 goto out;
236
5b712091
MS
237 /*
238 * It's probably not worth it to count subdirs to get the
239 * correct link count. nlink=1 seems to pacify 'find' and
240 * other utilities.
241 */
242 if (is_dir && OVL_TYPE_MERGE(type))
243 stat->nlink = 1;
244
5f8415d6
AG
245 /*
246 * Return the overlay inode nlinks for indexed upper inodes.
247 * Overlay inode nlink counts the union of the upper hardlinks
248 * and non-covered lower hardlinks. It does not include the upper
249 * index hardlink.
250 */
251 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
252 stat->nlink = dentry->d_inode->i_nlink;
253
72b608f0 254out:
1175b6b8 255 revert_creds(old_cred);
72b608f0 256
1175b6b8 257 return err;
e9be9d5e
MS
258}
259
260int ovl_permission(struct inode *inode, int mask)
261{
09d8b586
MS
262 struct inode *upperinode = ovl_inode_upper(inode);
263 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
c0ca3d70 264 const struct cred *old_cred;
e9be9d5e
MS
265 int err;
266
e9be9d5e 267 /* Careful in RCU walk mode */
e9be9d5e
MS
268 if (!realinode) {
269 WARN_ON(!(mask & MAY_NOT_BLOCK));
a999d7e1 270 return -ECHILD;
e9be9d5e
MS
271 }
272
c0ca3d70
VG
273 /*
274 * Check overlay inode with the creds of task and underlying inode
275 * with creds of mounter
276 */
277 err = generic_permission(inode, mask);
278 if (err)
279 return err;
280
ec7ba118
MS
281 old_cred = ovl_override_creds(inode->i_sb);
282 if (!upperinode &&
283 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
754f8cb7 284 mask &= ~(MAY_WRITE | MAY_APPEND);
ec7ba118 285 /* Make sure mounter can read file for copy up later */
500cac3c
VG
286 mask |= MAY_READ;
287 }
9c630ebe 288 err = inode_permission(realinode, mask);
c0ca3d70
VG
289 revert_creds(old_cred);
290
291 return err;
e9be9d5e
MS
292}
293
6b255391 294static const char *ovl_get_link(struct dentry *dentry,
fceef393
AV
295 struct inode *inode,
296 struct delayed_call *done)
e9be9d5e 297{
1175b6b8
VG
298 const struct cred *old_cred;
299 const char *p;
e9be9d5e 300
6b255391
AV
301 if (!dentry)
302 return ERR_PTR(-ECHILD);
303
1175b6b8 304 old_cred = ovl_override_creds(dentry->d_sb);
7764235b 305 p = vfs_get_link(ovl_dentry_real(dentry), done);
1175b6b8
VG
306 revert_creds(old_cred);
307 return p;
e9be9d5e
MS
308}
309
0956254a 310bool ovl_is_private_xattr(const char *name)
e9be9d5e 311{
fe2b7595
AG
312 return strncmp(name, OVL_XATTR_PREFIX,
313 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
e9be9d5e
MS
314}
315
1d88f183
MS
316int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
317 const void *value, size_t size, int flags)
e9be9d5e
MS
318{
319 int err;
1d88f183
MS
320 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
321 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
1175b6b8 322 const struct cred *old_cred;
e9be9d5e
MS
323
324 err = ovl_want_write(dentry);
325 if (err)
326 goto out;
327
1d88f183
MS
328 if (!value && !upperdentry) {
329 err = vfs_getxattr(realdentry, name, NULL, 0);
0e585ccc
AG
330 if (err < 0)
331 goto out_drop_write;
332 }
333
1d88f183
MS
334 if (!upperdentry) {
335 err = ovl_copy_up(dentry);
336 if (err)
337 goto out_drop_write;
e9be9d5e 338
1d88f183
MS
339 realdentry = ovl_dentry_upper(dentry);
340 }
0e585ccc 341
1175b6b8 342 old_cred = ovl_override_creds(dentry->d_sb);
0e585ccc 343 if (value)
1d88f183 344 err = vfs_setxattr(realdentry, name, value, size, flags);
0e585ccc
AG
345 else {
346 WARN_ON(flags != XATTR_REPLACE);
1d88f183 347 err = vfs_removexattr(realdentry, name);
0e585ccc 348 }
1175b6b8 349 revert_creds(old_cred);
e9be9d5e 350
d9854c87
MS
351 /* copy c/mtime */
352 ovl_copyattr(d_inode(realdentry), inode);
353
e9be9d5e
MS
354out_drop_write:
355 ovl_drop_write(dentry);
356out:
357 return err;
358}
359
1d88f183 360int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
0eb45fc3 361 void *value, size_t size)
e9be9d5e 362{
1175b6b8
VG
363 ssize_t res;
364 const struct cred *old_cred;
1d88f183
MS
365 struct dentry *realdentry =
366 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
52148463 367
1175b6b8
VG
368 old_cred = ovl_override_creds(dentry->d_sb);
369 res = vfs_getxattr(realdentry, name, value, size);
370 revert_creds(old_cred);
371 return res;
e9be9d5e
MS
372}
373
a082c6f6
MS
374static bool ovl_can_list(const char *s)
375{
376 /* List all non-trusted xatts */
377 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
378 return true;
379
380 /* Never list trusted.overlay, list other trusted for superuser only */
5c2e9f34
MS
381 return !ovl_is_private_xattr(s) &&
382 ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
a082c6f6
MS
383}
384
e9be9d5e
MS
385ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
386{
b581755b 387 struct dentry *realdentry = ovl_dentry_real(dentry);
e9be9d5e 388 ssize_t res;
7cb35119
MS
389 size_t len;
390 char *s;
1175b6b8 391 const struct cred *old_cred;
e9be9d5e 392
1175b6b8 393 old_cred = ovl_override_creds(dentry->d_sb);
b581755b 394 res = vfs_listxattr(realdentry, list, size);
1175b6b8 395 revert_creds(old_cred);
e9be9d5e
MS
396 if (res <= 0 || size == 0)
397 return res;
398
e9be9d5e 399 /* filter out private xattrs */
7cb35119
MS
400 for (s = list, len = res; len;) {
401 size_t slen = strnlen(s, len) + 1;
e9be9d5e 402
7cb35119
MS
403 /* underlying fs providing us with an broken xattr list? */
404 if (WARN_ON(slen > len))
405 return -EIO;
e9be9d5e 406
7cb35119 407 len -= slen;
a082c6f6 408 if (!ovl_can_list(s)) {
e9be9d5e 409 res -= slen;
7cb35119 410 memmove(s, s + slen, len);
e9be9d5e 411 } else {
7cb35119 412 s += slen;
e9be9d5e
MS
413 }
414 }
415
416 return res;
417}
418
39a25b2b
VG
419struct posix_acl *ovl_get_acl(struct inode *inode, int type)
420{
09d8b586 421 struct inode *realinode = ovl_inode_real(inode);
1175b6b8
VG
422 const struct cred *old_cred;
423 struct posix_acl *acl;
39a25b2b 424
5201dc44 425 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
39a25b2b
VG
426 return NULL;
427
1175b6b8 428 old_cred = ovl_override_creds(inode->i_sb);
5201dc44 429 acl = get_acl(realinode, type);
1175b6b8
VG
430 revert_creds(old_cred);
431
432 return acl;
39a25b2b
VG
433}
434
95582b00 435int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
d719e8f2 436{
8f35cf51
MS
437 if (flags & S_ATIME) {
438 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
439 struct path upperpath = {
440 .mnt = ofs->upper_mnt,
441 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
442 };
443
444 if (upperpath.dentry) {
445 touch_atime(&upperpath);
446 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
447 }
d719e8f2 448 }
d719e8f2
MS
449 return 0;
450}
451
9e142c41
MS
452static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
453 u64 start, u64 len)
454{
455 int err;
456 struct inode *realinode = ovl_inode_real(inode);
457 const struct cred *old_cred;
458
459 if (!realinode->i_op->fiemap)
460 return -EOPNOTSUPP;
461
462 old_cred = ovl_override_creds(inode->i_sb);
80d34810
AG
463
464 if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
465 filemap_write_and_wait(realinode->i_mapping);
466
9e142c41
MS
467 err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
468 revert_creds(old_cred);
469
470 return err;
471}
472
e9be9d5e
MS
473static const struct inode_operations ovl_file_inode_operations = {
474 .setattr = ovl_setattr,
475 .permission = ovl_permission,
476 .getattr = ovl_getattr,
e9be9d5e 477 .listxattr = ovl_listxattr,
39a25b2b 478 .get_acl = ovl_get_acl,
d719e8f2 479 .update_time = ovl_update_time,
9e142c41 480 .fiemap = ovl_fiemap,
e9be9d5e
MS
481};
482
483static const struct inode_operations ovl_symlink_inode_operations = {
484 .setattr = ovl_setattr,
6b255391 485 .get_link = ovl_get_link,
e9be9d5e 486 .getattr = ovl_getattr,
e9be9d5e 487 .listxattr = ovl_listxattr,
d719e8f2 488 .update_time = ovl_update_time,
e9be9d5e
MS
489};
490
9e142c41
MS
491static const struct inode_operations ovl_special_inode_operations = {
492 .setattr = ovl_setattr,
493 .permission = ovl_permission,
494 .getattr = ovl_getattr,
495 .listxattr = ovl_listxattr,
496 .get_acl = ovl_get_acl,
497 .update_time = ovl_update_time,
498};
499
69383c59 500static const struct address_space_operations ovl_aops = {
5b910bd6
AG
501 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
502 .direct_IO = noop_direct_IO,
503};
504
b1eaa950
AG
505/*
506 * It is possible to stack overlayfs instance on top of another
507 * overlayfs instance as lower layer. We need to annonate the
508 * stackable i_mutex locks according to stack level of the super
509 * block instance. An overlayfs instance can never be in stack
510 * depth 0 (there is always a real fs below it). An overlayfs
511 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
512 *
513 * For example, here is a snip from /proc/lockdep_chains after
514 * dir_iterate of nested overlayfs:
515 *
516 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
517 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
518 * [...] &type->i_mutex_dir_key (stack_depth=0)
b1f9d385
AG
519 *
520 * Locking order w.r.t ovl_want_write() is important for nested overlayfs.
521 *
522 * This chain is valid:
523 * - inode->i_rwsem (inode_lock[2])
524 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
525 * - OVL_I(inode)->lock (ovl_inode_lock[2])
526 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
527 *
528 * And this chain is valid:
529 * - inode->i_rwsem (inode_lock[2])
530 * - OVL_I(inode)->lock (ovl_inode_lock[2])
531 * - lowerinode->i_rwsem (inode_lock[1])
532 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
533 *
534 * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is
535 * held, because it is in reverse order of the non-nested case using the same
536 * upper fs:
537 * - inode->i_rwsem (inode_lock[1])
538 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
539 * - OVL_I(inode)->lock (ovl_inode_lock[1])
b1eaa950
AG
540 */
541#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
542
543static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
544{
545#ifdef CONFIG_LOCKDEP
546 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
547 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
4eae06de 548 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
b1eaa950
AG
549
550 int depth = inode->i_sb->s_stack_depth - 1;
551
552 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
553 depth = 0;
554
555 if (S_ISDIR(inode->i_mode))
556 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
557 else
558 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
4eae06de
AG
559
560 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
b1eaa950
AG
561#endif
562}
563
695b46e7 564static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
12574a9f 565 unsigned long ino, int fsid)
e9be9d5e 566{
12574a9f
AG
567 int xinobits = ovl_xino_bits(inode->i_sb);
568
695b46e7 569 /*
6dde1e42
AG
570 * When d_ino is consistent with st_ino (samefs or i_ino has enough
571 * bits to encode layer), set the same value used for st_ino to i_ino,
572 * so inode number exposed via /proc/locks and a like will be
573 * consistent with d_ino and st_ino values. An i_ino value inconsistent
735c907d
AG
574 * with d_ino also causes nfsd readdirplus to fail.
575 *
576 * When called from ovl_create_object() => ovl_new_inode(), with
577 * ino = 0, i_ino will be updated to consistent value later on in
578 * ovl_get_inode() => ovl_fill_inode().
695b46e7 579 */
0f831ec8 580 if (ovl_same_dev(inode->i_sb)) {
695b46e7 581 inode->i_ino = ino;
12574a9f
AG
582 if (xinobits && fsid && !(ino >> (64 - xinobits)))
583 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
584 } else {
695b46e7 585 inode->i_ino = get_next_ino();
12574a9f 586 }
e9be9d5e 587 inode->i_mode = mode;
d719e8f2 588 inode->i_flags |= S_NOCMTIME;
2a3a2a3f
MS
589#ifdef CONFIG_FS_POSIX_ACL
590 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
591#endif
e9be9d5e 592
b1eaa950
AG
593 ovl_lockdep_annotate_inode_mutex_key(inode);
594
ca4c8a3a
MS
595 switch (mode & S_IFMT) {
596 case S_IFREG:
597 inode->i_op = &ovl_file_inode_operations;
d1d04ef8 598 inode->i_fop = &ovl_file_operations;
5b910bd6 599 inode->i_mapping->a_ops = &ovl_aops;
ca4c8a3a
MS
600 break;
601
e9be9d5e 602 case S_IFDIR:
e9be9d5e
MS
603 inode->i_op = &ovl_dir_inode_operations;
604 inode->i_fop = &ovl_dir_operations;
605 break;
606
607 case S_IFLNK:
608 inode->i_op = &ovl_symlink_inode_operations;
609 break;
610
51f7e52d 611 default:
9e142c41 612 inode->i_op = &ovl_special_inode_operations;
ca4c8a3a 613 init_special_inode(inode, mode, rdev);
e9be9d5e 614 break;
51f7e52d
MS
615 }
616}
e9be9d5e 617
5f8415d6
AG
618/*
619 * With inodes index enabled, an overlay inode nlink counts the union of upper
620 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
621 * upper inode, the following nlink modifying operations can happen:
622 *
623 * 1. Lower hardlink copy up
624 * 2. Upper hardlink created, unlinked or renamed over
625 * 3. Lower hardlink whiteout or renamed over
626 *
627 * For the first, copy up case, the union nlink does not change, whether the
628 * operation succeeds or fails, but the upper inode nlink may change.
629 * Therefore, before copy up, we store the union nlink value relative to the
630 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
631 *
632 * For the second, upper hardlink case, the union nlink should be incremented
633 * or decremented IFF the operation succeeds, aligned with nlink change of the
634 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
635 * value relative to the upper inode nlink in the index inode.
636 *
637 * For the last, lower cover up case, we simplify things by preceding the
638 * whiteout or cover up with copy up. This makes sure that there is an index
639 * upper inode where the nlink xattr can be stored before the copied up upper
640 * entry is unlink.
641 */
642#define OVL_NLINK_ADD_UPPER (1 << 0)
643
644/*
645 * On-disk format for indexed nlink:
646 *
647 * nlink relative to the upper inode - "U[+-]NUM"
648 * nlink relative to the lower inode - "L[+-]NUM"
649 */
650
651static int ovl_set_nlink_common(struct dentry *dentry,
652 struct dentry *realdentry, const char *format)
653{
654 struct inode *inode = d_inode(dentry);
655 struct inode *realinode = d_inode(realdentry);
656 char buf[13];
657 int len;
658
659 len = snprintf(buf, sizeof(buf), format,
660 (int) (inode->i_nlink - realinode->i_nlink));
661
6787341a
MS
662 if (WARN_ON(len >= sizeof(buf)))
663 return -EIO;
664
5f8415d6
AG
665 return ovl_do_setxattr(ovl_dentry_upper(dentry),
666 OVL_XATTR_NLINK, buf, len, 0);
667}
668
669int ovl_set_nlink_upper(struct dentry *dentry)
670{
671 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
672}
673
674int ovl_set_nlink_lower(struct dentry *dentry)
675{
676 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
677}
678
caf70cb2
AG
679unsigned int ovl_get_nlink(struct dentry *lowerdentry,
680 struct dentry *upperdentry,
681 unsigned int fallback)
5f8415d6
AG
682{
683 int nlink_diff;
684 int nlink;
685 char buf[13];
686 int err;
687
688 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
689 return fallback;
690
691 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
692 if (err < 0)
693 goto fail;
694
695 buf[err] = '\0';
696 if ((buf[0] != 'L' && buf[0] != 'U') ||
697 (buf[1] != '+' && buf[1] != '-'))
698 goto fail;
699
700 err = kstrtoint(buf + 1, 10, &nlink_diff);
701 if (err < 0)
702 goto fail;
703
704 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
705 nlink += nlink_diff;
706
707 if (nlink <= 0)
708 goto fail;
709
710 return nlink;
711
712fail:
1bd0a3ae 713 pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n",
5f8415d6
AG
714 upperdentry, err);
715 return fallback;
716}
717
ca4c8a3a 718struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
51f7e52d
MS
719{
720 struct inode *inode;
721
722 inode = new_inode(sb);
723 if (inode)
12574a9f 724 ovl_fill_inode(inode, mode, rdev, 0, 0);
51f7e52d
MS
725
726 return inode;
727}
728
729static int ovl_inode_test(struct inode *inode, void *data)
730{
25b7713a 731 return inode->i_private == data;
51f7e52d
MS
732}
733
734static int ovl_inode_set(struct inode *inode, void *data)
735{
25b7713a 736 inode->i_private = data;
51f7e52d
MS
737 return 0;
738}
739
b9ac5c27 740static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
4b91c30a 741 struct dentry *upperdentry, bool strict)
b9ac5c27 742{
4b91c30a
AG
743 /*
744 * For directories, @strict verify from lookup path performs consistency
745 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
746 * inode. Non @strict verify from NFS handle decode path passes NULL for
747 * 'unknown' lower/upper.
748 */
749 if (S_ISDIR(inode->i_mode) && strict) {
31747eda
AG
750 /* Real lower dir moved to upper layer under us? */
751 if (!lowerdentry && ovl_inode_lower(inode))
752 return false;
753
754 /* Lookup of an uncovered redirect origin? */
755 if (!upperdentry && ovl_inode_upper(inode))
756 return false;
757 }
758
939ae4ef
AG
759 /*
760 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
761 * This happens when finding a copied up overlay inode for a renamed
762 * or hardlinked overlay dentry and lower dentry cannot be followed
763 * by origin because lower fs does not support file handles.
764 */
765 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
b9ac5c27
MS
766 return false;
767
768 /*
769 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
770 * This happens when finding a lower alias for a copied up hard link.
771 */
772 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
773 return false;
774
775 return true;
776}
777
4b91c30a
AG
778struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
779 bool is_upper)
9436a1a3 780{
4b91c30a 781 struct inode *inode, *key = d_inode(real);
9436a1a3
AG
782
783 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
784 if (!inode)
785 return NULL;
786
4b91c30a
AG
787 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
788 is_upper ? real : NULL, false)) {
9436a1a3
AG
789 iput(inode);
790 return ERR_PTR(-ESTALE);
791 }
792
793 return inode;
794}
795
146d62e5
AG
796bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
797{
798 struct inode *key = d_inode(dir);
799 struct inode *trap;
800 bool res;
801
802 trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
803 if (!trap)
804 return false;
805
806 res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
807 !ovl_inode_lower(trap);
808
809 iput(trap);
810 return res;
811}
812
813/*
814 * Create an inode cache entry for layer root dir, that will intentionally
815 * fail ovl_verify_inode(), so any lookup that will find some layer root
816 * will fail.
817 */
818struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
819{
820 struct inode *key = d_inode(dir);
821 struct inode *trap;
822
823 if (!d_is_dir(dir))
824 return ERR_PTR(-ENOTDIR);
825
826 trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
827 ovl_inode_set, key);
828 if (!trap)
829 return ERR_PTR(-ENOMEM);
830
831 if (!(trap->i_state & I_NEW)) {
832 /* Conflicting layer roots? */
833 iput(trap);
834 return ERR_PTR(-ELOOP);
835 }
836
837 trap->i_mode = S_IFDIR;
838 trap->i_flags = S_DEAD;
839 unlock_new_inode(trap);
840
841 return trap;
842}
843
764baba8
AG
844/*
845 * Does overlay inode need to be hashed by lower inode?
846 */
847static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
848 struct dentry *lower, struct dentry *index)
849{
850 struct ovl_fs *ofs = sb->s_fs_info;
851
852 /* No, if pure upper */
853 if (!lower)
854 return false;
855
856 /* Yes, if already indexed */
857 if (index)
858 return true;
859
860 /* Yes, if won't be copied up */
861 if (!ofs->upper_mnt)
862 return true;
863
864 /* No, if lower hardlink is or will be broken on copy up */
865 if ((upper || !ovl_indexdir(sb)) &&
866 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
867 return false;
868
869 /* No, if non-indexed upper with NFS export */
870 if (sb->s_export_op && upper)
871 return false;
872
873 /* Otherwise, hash by lower inode for fsnotify */
874 return true;
875}
876
01b39dcc
AG
877static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
878 struct inode *key)
879{
880 return newinode ? inode_insert5(newinode, (unsigned long) key,
881 ovl_inode_test, ovl_inode_set, key) :
882 iget5_locked(sb, (unsigned long) key,
883 ovl_inode_test, ovl_inode_set, key);
884}
885
ac6a52eb
VG
886struct inode *ovl_get_inode(struct super_block *sb,
887 struct ovl_inode_params *oip)
51f7e52d 888{
ac6a52eb
VG
889 struct dentry *upperdentry = oip->upperdentry;
890 struct ovl_path *lowerpath = oip->lowerpath;
09d8b586 891 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
51f7e52d 892 struct inode *inode;
12574a9f 893 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
ac6a52eb
VG
894 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
895 oip->index);
300b124f 896 int fsid = bylower ? lowerpath->layer->fsid : 0;
9d3dfea3 897 bool is_dir, metacopy = false;
695b46e7 898 unsigned long ino = 0;
acf3062a 899 int err = oip->newinode ? -EEXIST : -ENOMEM;
6eaf0111 900
09d8b586
MS
901 if (!realinode)
902 realinode = d_inode(lowerdentry);
903
6eaf0111 904 /*
764baba8
AG
905 * Copy up origin (lower) may exist for non-indexed upper, but we must
906 * not use lower as hash key if this is a broken hardlink.
6eaf0111 907 */
31747eda 908 is_dir = S_ISDIR(realinode->i_mode);
764baba8
AG
909 if (upperdentry || bylower) {
910 struct inode *key = d_inode(bylower ? lowerdentry :
911 upperdentry);
31747eda 912 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
b9ac5c27 913
01b39dcc 914 inode = ovl_iget5(sb, oip->newinode, key);
09d8b586 915 if (!inode)
027065b7 916 goto out_err;
09d8b586 917 if (!(inode->i_state & I_NEW)) {
b9ac5c27
MS
918 /*
919 * Verify that the underlying files stored in the inode
920 * match those in the dentry.
921 */
4b91c30a
AG
922 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
923 true)) {
b9ac5c27 924 iput(inode);
027065b7
VG
925 err = -ESTALE;
926 goto out_err;
b9ac5c27
MS
927 }
928
09d8b586 929 dput(upperdentry);
9cec54c8 930 kfree(oip->redirect);
09d8b586
MS
931 goto out;
932 }
e6d2ebdd 933
31747eda
AG
934 /* Recalculate nlink for non-dir due to indexing */
935 if (!is_dir)
936 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
5f8415d6 937 set_nlink(inode, nlink);
695b46e7 938 ino = key->i_ino;
e6d2ebdd 939 } else {
764baba8 940 /* Lower hardlink that will be broken on copy up */
0aceb53e 941 inode = new_inode(sb);
027065b7
VG
942 if (!inode) {
943 err = -ENOMEM;
944 goto out_err;
945 }
300b124f
AG
946 ino = realinode->i_ino;
947 fsid = lowerpath->layer->fsid;
e9be9d5e 948 }
12574a9f 949 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
2664bd08 950 ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata);
13c72075
MS
951
952 if (upperdentry && ovl_is_impuredir(upperdentry))
953 ovl_set_flag(OVL_IMPURE, inode);
954
ac6a52eb 955 if (oip->index)
0471a9cd
VG
956 ovl_set_flag(OVL_INDEX, inode);
957
9d3dfea3
VG
958 if (upperdentry) {
959 err = ovl_check_metacopy_xattr(upperdentry);
960 if (err < 0)
961 goto out_err;
962 metacopy = err;
963 if (!metacopy)
964 ovl_set_flag(OVL_UPPERDATA, inode);
965 }
966
9cec54c8
VG
967 OVL_I(inode)->redirect = oip->redirect;
968
a00c2d59
VG
969 if (bylower)
970 ovl_set_flag(OVL_CONST_INO, inode);
971
b79e05aa 972 /* Check for non-merge dir that may have whiteouts */
31747eda 973 if (is_dir) {
ac6a52eb 974 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
b79e05aa
AG
975 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
976 ovl_set_flag(OVL_WHITEOUTS, inode);
977 }
978 }
979
e6d2ebdd
MS
980 if (inode->i_state & I_NEW)
981 unlock_new_inode(inode);
982out:
e9be9d5e 983 return inode;
b9ac5c27 984
027065b7 985out_err:
1bd0a3ae 986 pr_warn_ratelimited("failed to get inode (%i)\n", err);
027065b7 987 inode = ERR_PTR(err);
b9ac5c27 988 goto out;
e9be9d5e 989}