1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 *
4 * Copyright (C) 2011 Novell Inc.
5 */
6
7 #include <linux/fs.h>
8 #include <linux/slab.h>
9 #include <linux/cred.h>
10 #include <linux/xattr.h>
11 #include <linux/ratelimit.h>
12 #include <linux/fiemap.h>
13 #include <linux/fileattr.h>
14 #include <linux/security.h>
15 #include <linux/namei.h>
16 #include <linux/posix_acl.h>
17 #include <linux/posix_acl_xattr.h>
18 #include "overlayfs.h"
19
20
ovl_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)21 int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
22 struct iattr *attr)
23 {
24 int err;
25 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
26 bool full_copy_up = false;
27 struct dentry *upperdentry;
28
29 err = setattr_prepare(idmap, dentry, attr);
30 if (err)
31 return err;
32
33 /* Rebase ownership from the mount idmap into overlay id space. */
34 if (attr->ia_valid & ATTR_UID)
35 attr->ia_vfsuid = VFSUIDT_INIT(from_vfsuid(idmap,
36 i_user_ns(d_inode(dentry)), attr->ia_vfsuid));
37 if (attr->ia_valid & ATTR_GID)
38 attr->ia_vfsgid = VFSGIDT_INIT(from_vfsgid(idmap,
39 i_user_ns(d_inode(dentry)), attr->ia_vfsgid));
40
41 if (attr->ia_valid & ATTR_SIZE) {
42 /* Truncate should trigger data copy up as well */
43 full_copy_up = true;
44 }
45
46 if (!full_copy_up)
47 err = ovl_copy_up(dentry);
48 else
49 err = ovl_copy_up_with_data(dentry);
50 if (!err) {
51 struct inode *winode = NULL;
52
53 upperdentry = ovl_dentry_upper(dentry);
54
55 if (attr->ia_valid & ATTR_SIZE) {
56 winode = d_inode(upperdentry);
57 err = get_write_access(winode);
58 if (err)
59 goto out;
60 }
61
62 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
63 attr->ia_valid &= ~ATTR_MODE;
64
65 /*
66 * We might have to translate ovl file into real file object
67 * once use cases emerge. For now, simply don't let underlying
68 * filesystem rely on attr->ia_file
69 */
70 attr->ia_valid &= ~ATTR_FILE;
71
72 /*
73 * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN
74 * set. Overlayfs does not pass O_TRUNC flag to underlying
75 * filesystem during open -> do not pass ATTR_OPEN. This
76 * disables optimization in fuse which assumes open(O_TRUNC)
77 * already set file size to 0. But we never passed O_TRUNC to
78 * fuse. So by clearing ATTR_OPEN, fuse will be forced to send
79 * setattr request to server.
80 */
81 attr->ia_valid &= ~ATTR_OPEN;
82
83 err = ovl_want_write(dentry);
84 if (err)
85 goto out_put_write;
86
87 inode_lock(upperdentry->d_inode);
88 with_ovl_creds(dentry->d_sb)
89 err = ovl_do_notify_change(ofs, upperdentry, attr);
90 if (!err)
91 ovl_copyattr(dentry->d_inode);
92 inode_unlock(upperdentry->d_inode);
93 ovl_drop_write(dentry);
94
95 out_put_write:
96 if (winode)
97 put_write_access(winode);
98 }
99 out:
100 return err;
101 }
102
ovl_map_dev_ino(struct dentry * dentry,struct kstat * stat,int fsid)103 static void ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid)
104 {
105 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
106 bool samefs = ovl_same_fs(ofs);
107 unsigned int xinobits = ovl_xino_bits(ofs);
108 unsigned int xinoshift = 64 - xinobits;
109
110 if (samefs) {
111 /*
112 * When all layers are on the same fs, all real inode
113 * number are unique, so we use the overlay st_dev,
114 * which is friendly to du -x.
115 */
116 stat->dev = dentry->d_sb->s_dev;
117 return;
118 } else if (xinobits) {
119 /*
120 * All inode numbers of underlying fs should not be using the
121 * high xinobits, so we use high xinobits to partition the
122 * overlay st_ino address space. The high bits holds the fsid
123 * (upper fsid is 0). The lowest xinobit is reserved for mapping
124 * the non-persistent inode numbers range in case of overflow.
125 * This way all overlay inode numbers are unique and use the
126 * overlay st_dev.
127 */
128 if (likely(!(stat->ino >> xinoshift))) {
129 stat->ino |= ((u64)fsid) << (xinoshift + 1);
130 stat->dev = dentry->d_sb->s_dev;
131 return;
132 } else if (ovl_xino_warn(ofs)) {
133 pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
134 dentry, stat->ino, xinobits);
135 }
136 }
137
138 /* The inode could not be mapped to a unified st_ino address space */
139 if (S_ISDIR(dentry->d_inode->i_mode)) {
140 /*
141 * Always use the overlay st_dev for directories, so 'find
142 * -xdev' will scan the entire overlay mount and won't cross the
143 * overlay mount boundaries.
144 *
145 * If not all layers are on the same fs the pair {real st_ino;
146 * overlay st_dev} is not unique, so use the non persistent
147 * overlay st_ino for directories.
148 */
149 stat->dev = dentry->d_sb->s_dev;
150 stat->ino = dentry->d_inode->i_ino;
151 } else {
152 /*
153 * For non-samefs setup, if we cannot map all layers st_ino
154 * to a unified address space, we need to make sure that st_dev
155 * is unique per underlying fs, so we use the unique anonymous
156 * bdev assigned to the underlying fs.
157 */
158 stat->dev = ofs->fs[fsid].pseudo_dev;
159 }
160 }
161
ovl_real_getattr_nosec(struct super_block * sb,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)162 static inline int ovl_real_getattr_nosec(struct super_block *sb,
163 const struct path *path,
164 struct kstat *stat, u32 request_mask,
165 unsigned int flags)
166 {
167 with_ovl_creds(sb)
168 return vfs_getattr_nosec(path, stat, request_mask, flags);
169 }
170
ovl_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)171 int ovl_getattr(struct mnt_idmap *idmap, const struct path *path,
172 struct kstat *stat, u32 request_mask, unsigned int flags)
173 {
174 struct dentry *dentry = path->dentry;
175 struct super_block *sb = dentry->d_sb;
176 enum ovl_path_type type;
177 struct path realpath;
178 struct inode *inode = d_inode(dentry);
179 bool is_dir = S_ISDIR(inode->i_mode);
180 int fsid = 0;
181 int err;
182 bool metacopy_blocks = false;
183 vfsuid_t vfsuid;
184 vfsgid_t vfsgid;
185
186 metacopy_blocks = ovl_is_metacopy_dentry(dentry);
187
188 type = ovl_path_real(dentry, &realpath);
189 err = ovl_real_getattr_nosec(sb, &realpath, stat, request_mask, flags);
190 if (err)
191 return err;
192
193 /* Report the effective immutable/append-only STATX flags */
194 generic_fill_statx_attr(inode, stat);
195
196 /*
197 * For non-dir or same fs, we use st_ino of the copy up origin.
198 * This guaranties constant st_dev/st_ino across copy up.
199 * With xino feature and non-samefs, we use st_ino of the copy up
200 * origin masked with high bits that represent the layer id.
201 *
202 * If lower filesystem supports NFS file handles, this also guaranties
203 * persistent st_ino across mount cycle.
204 */
205 if (!is_dir || ovl_same_dev(OVL_FS(dentry->d_sb))) {
206 if (!OVL_TYPE_UPPER(type)) {
207 fsid = ovl_layer_lower(dentry)->fsid;
208 } else if (OVL_TYPE_ORIGIN(type)) {
209 struct kstat lowerstat;
210 u32 lowermask = STATX_INO | STATX_BLOCKS |
211 (!is_dir ? STATX_NLINK : 0);
212
213 ovl_path_lower(dentry, &realpath);
214 err = ovl_real_getattr_nosec(sb, &realpath, &lowerstat, lowermask, flags);
215 if (err)
216 return err;
217
218 /*
219 * Lower hardlinks may be broken on copy up to different
220 * upper files, so we cannot use the lower origin st_ino
221 * for those different files, even for the same fs case.
222 *
223 * Similarly, several redirected dirs can point to the
224 * same dir on a lower layer. With the "verify_lower"
225 * feature, we do not use the lower origin st_ino, if
226 * we haven't verified that this redirect is unique.
227 *
228 * With inodes index enabled, it is safe to use st_ino
229 * of an indexed origin. The index validates that the
230 * upper hardlink is not broken and that a redirected
231 * dir is the only redirect to that origin.
232 */
233 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
234 (!ovl_verify_lower(dentry->d_sb) &&
235 (is_dir || lowerstat.nlink == 1))) {
236 fsid = ovl_layer_lower(dentry)->fsid;
237 stat->ino = lowerstat.ino;
238 }
239
240 /*
241 * If we are querying a metacopy dentry and lower
242 * dentry is data dentry, then use the blocks we
243 * queried just now. We don't have to do additional
244 * vfs_getattr(). If lower itself is metacopy, then
245 * additional vfs_getattr() is unavoidable.
246 */
247 if (metacopy_blocks &&
248 realpath.dentry == ovl_dentry_lowerdata(dentry)) {
249 stat->blocks = lowerstat.blocks;
250 metacopy_blocks = false;
251 }
252 }
253
254 if (metacopy_blocks) {
255 /*
256 * If lower is not same as lowerdata or if there was
257 * no origin on upper, we can end up here.
258 * With lazy lowerdata lookup, guess lowerdata blocks
259 * from size to avoid lowerdata lookup on stat(2).
260 */
261 struct kstat lowerdatastat;
262 u32 lowermask = STATX_BLOCKS;
263
264 ovl_path_lowerdata(dentry, &realpath);
265 if (realpath.dentry) {
266 err = ovl_real_getattr_nosec(sb, &realpath, &lowerdatastat,
267 lowermask, flags);
268 if (err)
269 return err;
270 } else {
271 lowerdatastat.blocks =
272 round_up(stat->size, stat->blksize) >> 9;
273 }
274 stat->blocks = lowerdatastat.blocks;
275 }
276 }
277
278 ovl_map_dev_ino(dentry, stat, fsid);
279
280 /*
281 * It's probably not worth it to count subdirs to get the
282 * correct link count. nlink=1 seems to pacify 'find' and
283 * other utilities.
284 */
285 if (is_dir && OVL_TYPE_MERGE(type))
286 stat->nlink = 1;
287
288 /*
289 * Return the overlay inode nlinks for indexed upper inodes.
290 * Overlay inode nlink counts the union of the upper hardlinks
291 * and non-covered lower hardlinks. It does not include the upper
292 * index hardlink.
293 */
294 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
295 stat->nlink = dentry->d_inode->i_nlink;
296
297 /* Map ownership of the real inode through the overlay mount idmap. */
298 vfsuid = make_vfsuid(idmap, i_user_ns(inode), stat->uid);
299 vfsgid = make_vfsgid(idmap, i_user_ns(inode), stat->gid);
300 stat->uid = vfsuid_into_kuid(vfsuid);
301 stat->gid = vfsgid_into_kgid(vfsgid);
302
303 return err;
304 }
305
ovl_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)306 int ovl_permission(struct mnt_idmap *idmap,
307 struct inode *inode, int mask)
308 {
309 struct inode *upperinode = ovl_inode_upper(inode);
310 struct inode *realinode;
311 struct path realpath;
312 int err;
313
314 /* Careful in RCU walk mode */
315 realinode = ovl_i_path_real(inode, &realpath);
316 if (!realinode) {
317 WARN_ON(!(mask & MAY_NOT_BLOCK));
318 return -ECHILD;
319 }
320
321 /*
322 * Check overlay inode with the creds of task and underlying inode
323 * with creds of mounter
324 */
325 err = generic_permission(idmap, inode, mask);
326 if (err)
327 return err;
328
329 if (!upperinode &&
330 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
331 mask &= ~(MAY_WRITE | MAY_APPEND);
332 /* Make sure mounter can read file for copy up later */
333 mask |= MAY_READ;
334 }
335
336 with_ovl_creds(inode->i_sb)
337 return inode_permission(mnt_idmap(realpath.mnt), realinode, mask);
338 }
339
ovl_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)340 static const char *ovl_get_link(struct dentry *dentry,
341 struct inode *inode,
342 struct delayed_call *done)
343 {
344 if (!dentry)
345 return ERR_PTR(-ECHILD);
346
347 with_ovl_creds(dentry->d_sb)
348 return vfs_get_link(ovl_dentry_real(dentry), done);
349 }
350
351 #ifdef CONFIG_FS_POSIX_ACL
352 /*
353 * Apply the idmapping of the layer to POSIX ACLs. The caller must pass a clone
354 * of the POSIX ACLs retrieved from the lower layer to this function to not
355 * alter the POSIX ACLs for the underlying filesystem.
356 */
ovl_idmap_posix_acl(const struct inode * realinode,struct mnt_idmap * idmap,struct posix_acl * acl)357 static void ovl_idmap_posix_acl(const struct inode *realinode,
358 struct mnt_idmap *idmap,
359 struct posix_acl *acl)
360 {
361 struct user_namespace *fs_userns = i_user_ns(realinode);
362
363 for (unsigned int i = 0; i < acl->a_count; i++) {
364 vfsuid_t vfsuid;
365 vfsgid_t vfsgid;
366
367 struct posix_acl_entry *e = &acl->a_entries[i];
368 switch (e->e_tag) {
369 case ACL_USER:
370 vfsuid = make_vfsuid(idmap, fs_userns, e->e_uid);
371 e->e_uid = vfsuid_into_kuid(vfsuid);
372 break;
373 case ACL_GROUP:
374 vfsgid = make_vfsgid(idmap, fs_userns, e->e_gid);
375 e->e_gid = vfsgid_into_kgid(vfsgid);
376 break;
377 }
378 }
379 }
380
381 /*
382 * The @noperm argument is used to skip permission checking and is a temporary
383 * measure. Quoting Miklos from an earlier discussion:
384 *
385 * > So there are two paths to getting an acl:
386 * > 1) permission checking and 2) retrieving the value via getxattr(2).
387 * > This is a similar situation as reading a symlink vs. following it.
388 * > When following a symlink overlayfs always reads the link on the
389 * > underlying fs just as if it was a readlink(2) call, calling
390 * > security_inode_readlink() instead of security_inode_follow_link().
391 * > This is logical: we are reading the link from the underlying storage,
392 * > and following it on overlayfs.
393 * >
394 * > Applying the same logic to acl: we do need to call the
395 * > security_inode_getxattr() on the underlying fs, even if just want to
396 * > check permissions on overlay. This is currently not done, which is an
397 * > inconsistency.
398 * >
399 * > Maybe adding the check to ovl_get_acl() is the right way to go, but
400 * > I'm a little afraid of a performance regression. Will look into that.
401 *
402 * Until we have made a decision allow this helper to take the @noperm
403 * argument. We should hopefully be able to remove it soon.
404 */
ovl_get_acl_path(const struct path * path,const char * acl_name,bool noperm)405 struct posix_acl *ovl_get_acl_path(const struct path *path,
406 const char *acl_name, bool noperm)
407 {
408 struct posix_acl *real_acl, *clone;
409 struct mnt_idmap *idmap;
410 struct inode *realinode = d_inode(path->dentry);
411
412 idmap = mnt_idmap(path->mnt);
413
414 if (noperm)
415 real_acl = get_inode_acl(realinode, posix_acl_type(acl_name));
416 else
417 real_acl = vfs_get_acl(idmap, path->dentry, acl_name);
418 if (IS_ERR_OR_NULL(real_acl))
419 return real_acl;
420
421 if (!is_idmapped_mnt(path->mnt))
422 return real_acl;
423
424 /*
425 * We cannot alter the ACLs returned from the relevant layer as that
426 * would alter the cached values filesystem wide for the lower
427 * filesystem. Instead we can clone the ACLs and then apply the
428 * relevant idmapping of the layer.
429 */
430 clone = posix_acl_clone(real_acl, GFP_KERNEL);
431 posix_acl_release(real_acl); /* release original acl */
432 if (!clone)
433 return ERR_PTR(-ENOMEM);
434
435 ovl_idmap_posix_acl(realinode, idmap, clone);
436 return clone;
437 }
438
439 /*
440 * When the relevant layer is an idmapped mount we need to take the idmapping
441 * of the layer into account and translate any ACL_{GROUP,USER} values
442 * according to the idmapped mount.
443 *
444 * We cannot alter the ACLs returned from the relevant layer as that would
445 * alter the cached values filesystem wide for the lower filesystem. Instead we
446 * can clone the ACLs and then apply the relevant idmapping of the layer.
447 *
448 * This is obviously only relevant when idmapped layers are used.
449 */
do_ovl_get_acl(struct mnt_idmap * idmap,struct inode * inode,int type,bool rcu,bool noperm)450 struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap,
451 struct inode *inode, int type,
452 bool rcu, bool noperm)
453 {
454 struct inode *realinode;
455 struct posix_acl *acl;
456 struct path realpath;
457
458 /* Careful in RCU walk mode */
459 realinode = ovl_i_path_real(inode, &realpath);
460 if (!realinode) {
461 WARN_ON(!rcu);
462 return ERR_PTR(-ECHILD);
463 }
464
465 if (!IS_POSIXACL(realinode))
466 return NULL;
467
468 if (rcu) {
469 /*
470 * If the layer is idmapped drop out of RCU path walk
471 * so we can clone the ACLs.
472 */
473 if (is_idmapped_mnt(realpath.mnt))
474 return ERR_PTR(-ECHILD);
475
476 acl = get_cached_acl_rcu(realinode, type);
477 } else {
478 with_ovl_creds(inode->i_sb)
479 acl = ovl_get_acl_path(&realpath, posix_acl_xattr_name(type), noperm);
480 }
481
482 return acl;
483 }
484
ovl_set_or_remove_acl(struct dentry * dentry,struct inode * inode,struct posix_acl * acl,int type)485 static int ovl_set_or_remove_acl(struct dentry *dentry, struct inode *inode,
486 struct posix_acl *acl, int type)
487 {
488 int err;
489 struct path realpath;
490 const char *acl_name;
491 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
492 struct dentry *upperdentry = ovl_dentry_upper(dentry);
493 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
494
495 /*
496 * If ACL is to be removed from a lower file, check if it exists in
497 * the first place before copying it up.
498 */
499 acl_name = posix_acl_xattr_name(type);
500 if (!acl && !upperdentry) {
501 struct posix_acl *real_acl;
502
503 ovl_path_lower(dentry, &realpath);
504 with_ovl_creds(dentry->d_sb)
505 real_acl = vfs_get_acl(mnt_idmap(realpath.mnt), realdentry, acl_name);
506 if (IS_ERR(real_acl)) {
507 err = PTR_ERR(real_acl);
508 goto out;
509 }
510 posix_acl_release(real_acl);
511 }
512
513 if (!upperdentry) {
514 err = ovl_copy_up(dentry);
515 if (err)
516 goto out;
517
518 realdentry = ovl_dentry_upper(dentry);
519 }
520
521 err = ovl_want_write(dentry);
522 if (err)
523 goto out;
524
525 with_ovl_creds(dentry->d_sb) {
526 if (acl)
527 err = ovl_do_set_acl(ofs, realdentry, acl_name, acl);
528 else
529 err = ovl_do_remove_acl(ofs, realdentry, acl_name);
530 }
531 ovl_drop_write(dentry);
532
533 /* copy c/mtime */
534 ovl_copyattr(inode);
535 out:
536 return err;
537 }
538
ovl_set_acl(struct mnt_idmap * idmap,struct dentry * dentry,struct posix_acl * acl,int type)539 int ovl_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
540 struct posix_acl *acl, int type)
541 {
542 int err;
543 struct inode *inode = d_inode(dentry);
544 struct dentry *workdir = ovl_workdir(dentry);
545 struct inode *realinode = ovl_inode_real(inode);
546
547 if (!IS_POSIXACL(d_inode(workdir)))
548 return -EOPNOTSUPP;
549 if (!realinode->i_op->set_acl)
550 return -EOPNOTSUPP;
551 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
552 return acl ? -EACCES : 0;
553 if (!inode_owner_or_capable(idmap, inode))
554 return -EPERM;
555
556 /*
557 * Check if sgid bit needs to be cleared (actual setacl operation will
558 * be done with mounter's capabilities and so that won't do it for us).
559 */
560 if (unlikely(inode->i_mode & S_ISGID) && type == ACL_TYPE_ACCESS &&
561 !in_group_or_capable(idmap, inode,
562 i_gid_into_vfsgid(idmap, inode))) {
563 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
564
565 err = ovl_setattr(&nop_mnt_idmap, dentry, &iattr);
566 if (err)
567 return err;
568 }
569
570 return ovl_set_or_remove_acl(dentry, inode, acl, type);
571 }
572 #endif
573
ovl_update_time(struct inode * inode,enum fs_update_time type,unsigned int flags)574 int ovl_update_time(struct inode *inode, enum fs_update_time type,
575 unsigned int flags)
576 {
577 if (type == FS_UPD_ATIME) {
578 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
579 struct path upperpath = {
580 .mnt = ovl_upper_mnt(ofs),
581 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
582 };
583
584 if (upperpath.dentry) {
585 if (flags & IOCB_NOWAIT)
586 return -EAGAIN;
587 touch_atime(&upperpath);
588 inode_set_atime_to_ts(inode,
589 inode_get_atime(d_inode(upperpath.dentry)));
590 }
591 }
592 return 0;
593 }
594
ovl_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,u64 start,u64 len)595 static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
596 u64 start, u64 len)
597 {
598 struct inode *realinode = ovl_inode_realdata(inode);
599
600 if (!realinode)
601 return -EIO;
602
603 if (!realinode->i_op->fiemap)
604 return -EOPNOTSUPP;
605
606 with_ovl_creds(inode->i_sb)
607 return realinode->i_op->fiemap(realinode, fieinfo, start, len);
608 }
609
610 /*
611 * Work around the fact that security_file_ioctl() takes a file argument.
612 * Introducing security_inode_fileattr_get/set() hooks would solve this issue
613 * properly.
614 */
ovl_security_fileattr(const struct path * realpath,struct file_kattr * fa,bool set)615 static int ovl_security_fileattr(const struct path *realpath, struct file_kattr *fa,
616 bool set)
617 {
618 struct file *file;
619 unsigned int cmd;
620 int err;
621 unsigned int flags;
622
623 flags = O_RDONLY;
624 if (force_o_largefile())
625 flags |= O_LARGEFILE;
626
627 file = dentry_open(realpath, flags, current_cred());
628 if (IS_ERR(file))
629 return PTR_ERR(file);
630
631 if (set)
632 cmd = fa->fsx_valid ? FS_IOC_FSSETXATTR : FS_IOC_SETFLAGS;
633 else
634 cmd = fa->fsx_valid ? FS_IOC_FSGETXATTR : FS_IOC_GETFLAGS;
635
636 err = security_file_ioctl(file, cmd, 0);
637 fput(file);
638
639 return err;
640 }
641
ovl_real_fileattr_set(const struct path * realpath,struct file_kattr * fa)642 int ovl_real_fileattr_set(const struct path *realpath, struct file_kattr *fa)
643 {
644 int err;
645
646 err = ovl_security_fileattr(realpath, fa, true);
647 if (err)
648 return err;
649
650 return vfs_fileattr_set(mnt_idmap(realpath->mnt), realpath->dentry, fa);
651 }
652
ovl_fileattr_set(struct mnt_idmap * idmap,struct dentry * dentry,struct file_kattr * fa)653 int ovl_fileattr_set(struct mnt_idmap *idmap,
654 struct dentry *dentry, struct file_kattr *fa)
655 {
656 struct inode *inode = d_inode(dentry);
657 struct path upperpath;
658 unsigned int flags;
659 int err;
660
661 err = ovl_copy_up(dentry);
662 if (!err) {
663 ovl_path_real(dentry, &upperpath);
664
665 err = ovl_want_write(dentry);
666 if (err)
667 goto out;
668
669 with_ovl_creds(inode->i_sb) {
670 /*
671 * Store immutable/append-only flags in xattr and clear them
672 * in upper fileattr (in case they were set by older kernel)
673 * so children of "ovl-immutable" directories lower aliases of
674 * "ovl-immutable" hardlinks could be copied up.
675 * Clear xattr when flags are cleared.
676 */
677 err = ovl_set_protattr(inode, upperpath.dentry, fa);
678 if (!err)
679 err = ovl_real_fileattr_set(&upperpath, fa);
680 }
681 ovl_drop_write(dentry);
682
683 /*
684 * Merge real inode flags with inode flags read from
685 * overlay.protattr xattr
686 */
687 flags = ovl_inode_real(inode)->i_flags & OVL_COPY_I_FLAGS_MASK;
688
689 BUILD_BUG_ON(OVL_PROT_I_FLAGS_MASK & ~OVL_COPY_I_FLAGS_MASK);
690 flags |= inode->i_flags & OVL_PROT_I_FLAGS_MASK;
691 inode_set_flags(inode, flags, OVL_COPY_I_FLAGS_MASK);
692
693 /* Update ctime */
694 ovl_copyattr(inode);
695 }
696 out:
697 return err;
698 }
699
700 /* Convert inode protection flags to fileattr flags */
ovl_fileattr_prot_flags(struct inode * inode,struct file_kattr * fa)701 static void ovl_fileattr_prot_flags(struct inode *inode, struct file_kattr *fa)
702 {
703 BUILD_BUG_ON(OVL_PROT_FS_FLAGS_MASK & ~FS_COMMON_FL);
704 BUILD_BUG_ON(OVL_PROT_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
705
706 if (inode->i_flags & S_APPEND) {
707 fa->flags |= FS_APPEND_FL;
708 fa->fsx_xflags |= FS_XFLAG_APPEND;
709 }
710 if (inode->i_flags & S_IMMUTABLE) {
711 fa->flags |= FS_IMMUTABLE_FL;
712 fa->fsx_xflags |= FS_XFLAG_IMMUTABLE;
713 }
714 }
715
ovl_real_fileattr_get(const struct path * realpath,struct file_kattr * fa)716 int ovl_real_fileattr_get(const struct path *realpath, struct file_kattr *fa)
717 {
718 int err;
719
720 err = ovl_security_fileattr(realpath, fa, false);
721 if (err)
722 return err;
723
724 err = vfs_fileattr_get(realpath->dentry, fa);
725 if (err == -ENOIOCTLCMD)
726 err = -ENOTTY;
727 return err;
728 }
729
ovl_fileattr_get(struct dentry * dentry,struct file_kattr * fa)730 int ovl_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
731 {
732 struct inode *inode = d_inode(dentry);
733 struct path realpath;
734 int err;
735
736 ovl_path_real(dentry, &realpath);
737
738 with_ovl_creds(inode->i_sb)
739 err = ovl_real_fileattr_get(&realpath, fa);
740 ovl_fileattr_prot_flags(inode, fa);
741
742 return err;
743 }
744
745 static const struct inode_operations ovl_file_inode_operations = {
746 .setattr = ovl_setattr,
747 .permission = ovl_permission,
748 .getattr = ovl_getattr,
749 .listxattr = ovl_listxattr,
750 .get_inode_acl = ovl_get_inode_acl,
751 .get_acl = ovl_get_acl,
752 .set_acl = ovl_set_acl,
753 .update_time = ovl_update_time,
754 .fiemap = ovl_fiemap,
755 .fileattr_get = ovl_fileattr_get,
756 .fileattr_set = ovl_fileattr_set,
757 };
758
759 static const struct inode_operations ovl_symlink_inode_operations = {
760 .setattr = ovl_setattr,
761 .get_link = ovl_get_link,
762 .getattr = ovl_getattr,
763 .listxattr = ovl_listxattr,
764 .update_time = ovl_update_time,
765 };
766
767 static const struct inode_operations ovl_special_inode_operations = {
768 .setattr = ovl_setattr,
769 .permission = ovl_permission,
770 .getattr = ovl_getattr,
771 .listxattr = ovl_listxattr,
772 .get_inode_acl = ovl_get_inode_acl,
773 .get_acl = ovl_get_acl,
774 .set_acl = ovl_set_acl,
775 .update_time = ovl_update_time,
776 };
777
778 static const struct address_space_operations ovl_aops = {
779 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
780 .direct_IO = noop_direct_IO,
781 };
782
783 /*
784 * It is possible to stack overlayfs instance on top of another
785 * overlayfs instance as lower layer. We need to annotate the
786 * stackable i_mutex locks according to stack level of the super
787 * block instance. An overlayfs instance can never be in stack
788 * depth 0 (there is always a real fs below it). An overlayfs
789 * inode lock will use the lockdep annotation ovl_i_mutex_key[depth].
790 *
791 * For example, here is a snip from /proc/lockdep_chains after
792 * dir_iterate of nested overlayfs:
793 *
794 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
795 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
796 * [...] &type->i_mutex_dir_key (stack_depth=0)
797 *
798 * Locking order w.r.t ovl_want_write() is important for nested overlayfs.
799 *
800 * This chain is valid:
801 * - inode->i_rwsem (inode_lock[2])
802 * - OVL_I(inode)->lock (ovl_inode_lock[2])
803 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
804 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
805 *
806 * And this chain is valid:
807 * - inode->i_rwsem (inode_lock[2])
808 * - OVL_I(inode)->lock (ovl_inode_lock[2])
809 * - lowerinode->i_rwsem (inode_lock[1])
810 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
811 *
812 * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is
813 * held, because it is in reverse order of the non-nested case using the same
814 * upper fs:
815 * - inode->i_rwsem (inode_lock[1])
816 * - OVL_I(inode)->lock (ovl_inode_lock[1])
817 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
818 */
819 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
820
ovl_lockdep_annotate_inode_mutex_key(struct inode * inode)821 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
822 {
823 #ifdef CONFIG_LOCKDEP
824 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
825 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
826 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
827
828 int depth = inode->i_sb->s_stack_depth - 1;
829
830 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
831 depth = 0;
832
833 if (S_ISDIR(inode->i_mode))
834 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
835 else
836 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
837
838 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
839 #endif
840 }
841
ovl_next_ino(struct inode * inode)842 static void ovl_next_ino(struct inode *inode)
843 {
844 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
845
846 inode->i_ino = atomic_long_inc_return(&ofs->last_ino);
847 if (unlikely(!inode->i_ino))
848 inode->i_ino = atomic_long_inc_return(&ofs->last_ino);
849 }
850
ovl_map_ino(struct inode * inode,unsigned long ino,int fsid)851 static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid)
852 {
853 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
854 int xinobits = ovl_xino_bits(ofs);
855 unsigned int xinoshift = 64 - xinobits;
856
857 /*
858 * When d_ino is consistent with st_ino (samefs or i_ino has enough
859 * bits to encode layer), set the same value used for st_ino to i_ino,
860 * so inode number exposed via /proc/locks and a like will be
861 * consistent with d_ino and st_ino values. An i_ino value inconsistent
862 * with d_ino also causes nfsd readdirplus to fail.
863 */
864 inode->i_ino = ino;
865 if (ovl_same_fs(ofs)) {
866 return;
867 } else if (xinobits && likely(!(ino >> xinoshift))) {
868 inode->i_ino |= (unsigned long)fsid << (xinoshift + 1);
869 return;
870 }
871
872 /*
873 * For directory inodes on non-samefs with xino disabled or xino
874 * overflow, we allocate a non-persistent inode number, to be used for
875 * resolving st_ino collisions in ovl_map_dev_ino().
876 *
877 * To avoid ino collision with legitimate xino values from upper
878 * layer (fsid 0), use the lowest xinobit to map the non
879 * persistent inode numbers to the unified st_ino address space.
880 */
881 if (S_ISDIR(inode->i_mode)) {
882 ovl_next_ino(inode);
883 if (xinobits) {
884 inode->i_ino &= ~0UL >> xinobits;
885 inode->i_ino |= 1UL << xinoshift;
886 }
887 }
888 }
889
ovl_inode_init(struct inode * inode,struct ovl_inode_params * oip,unsigned long ino,int fsid)890 void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
891 unsigned long ino, int fsid)
892 {
893 struct inode *realinode;
894 struct ovl_inode *oi = OVL_I(inode);
895
896 oi->__upperdentry = oip->upperdentry;
897 oi->oe = oip->oe;
898 oi->redirect = oip->redirect;
899 oi->lowerdata_redirect = oip->lowerdata_redirect;
900
901 realinode = ovl_inode_real(inode);
902 ovl_copyattr(inode);
903 ovl_copyflags(realinode, inode);
904 ovl_map_ino(inode, ino, fsid);
905 }
906
ovl_fill_inode(struct inode * inode,umode_t mode,dev_t rdev)907 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
908 {
909 inode->i_mode = mode;
910 inode->i_flags |= S_NOCMTIME;
911 #ifdef CONFIG_FS_POSIX_ACL
912 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
913 #endif
914
915 ovl_lockdep_annotate_inode_mutex_key(inode);
916
917 switch (mode & S_IFMT) {
918 case S_IFREG:
919 inode->i_op = &ovl_file_inode_operations;
920 inode->i_fop = &ovl_file_operations;
921 inode->i_mapping->a_ops = &ovl_aops;
922 break;
923
924 case S_IFDIR:
925 inode->i_op = &ovl_dir_inode_operations;
926 inode->i_fop = &ovl_dir_operations;
927 break;
928
929 case S_IFLNK:
930 inode->i_op = &ovl_symlink_inode_operations;
931 break;
932
933 default:
934 inode->i_op = &ovl_special_inode_operations;
935 init_special_inode(inode, mode, rdev);
936 break;
937 }
938 }
939
940 /*
941 * With inodes index enabled, an overlay inode nlink counts the union of upper
942 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
943 * upper inode, the following nlink modifying operations can happen:
944 *
945 * 1. Lower hardlink copy up
946 * 2. Upper hardlink created, unlinked or renamed over
947 * 3. Lower hardlink whiteout or renamed over
948 *
949 * For the first, copy up case, the union nlink does not change, whether the
950 * operation succeeds or fails, but the upper inode nlink may change.
951 * Therefore, before copy up, we store the union nlink value relative to the
952 * lower inode nlink in the index inode xattr .overlay.nlink.
953 *
954 * For the second, upper hardlink case, the union nlink should be incremented
955 * or decremented IFF the operation succeeds, aligned with nlink change of the
956 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
957 * value relative to the upper inode nlink in the index inode.
958 *
959 * For the last, lower cover up case, we simplify things by preceding the
960 * whiteout or cover up with copy up. This makes sure that there is an index
961 * upper inode where the nlink xattr can be stored before the copied up upper
962 * entry is unlink.
963 */
964 #define OVL_NLINK_ADD_UPPER (1 << 0)
965
966 /*
967 * On-disk format for indexed nlink:
968 *
969 * nlink relative to the upper inode - "U[+-]NUM"
970 * nlink relative to the lower inode - "L[+-]NUM"
971 */
972
ovl_set_nlink_common(struct dentry * dentry,struct dentry * realdentry,const char * format)973 static int ovl_set_nlink_common(struct dentry *dentry,
974 struct dentry *realdentry, const char *format)
975 {
976 struct inode *inode = d_inode(dentry);
977 struct inode *realinode = d_inode(realdentry);
978 char buf[13];
979 int len;
980
981 len = snprintf(buf, sizeof(buf), format,
982 (int) (inode->i_nlink - realinode->i_nlink));
983
984 if (WARN_ON(len >= sizeof(buf)))
985 return -EIO;
986
987 return ovl_setxattr(OVL_FS(inode->i_sb), ovl_dentry_upper(dentry),
988 OVL_XATTR_NLINK, buf, len);
989 }
990
ovl_set_nlink_upper(struct dentry * dentry)991 int ovl_set_nlink_upper(struct dentry *dentry)
992 {
993 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
994 }
995
ovl_set_nlink_lower(struct dentry * dentry)996 int ovl_set_nlink_lower(struct dentry *dentry)
997 {
998 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
999 }
1000
ovl_get_nlink(struct ovl_fs * ofs,struct dentry * lowerdentry,struct dentry * upperdentry,unsigned int fallback)1001 unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
1002 struct dentry *upperdentry,
1003 unsigned int fallback)
1004 {
1005 int nlink_diff;
1006 int nlink;
1007 char buf[13];
1008 int err;
1009
1010 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
1011 return fallback;
1012
1013 err = ovl_getxattr_upper(ofs, upperdentry, OVL_XATTR_NLINK,
1014 &buf, sizeof(buf) - 1);
1015 if (err < 0)
1016 goto fail;
1017
1018 buf[err] = '\0';
1019 if ((buf[0] != 'L' && buf[0] != 'U') ||
1020 (buf[1] != '+' && buf[1] != '-'))
1021 goto fail;
1022
1023 err = kstrtoint(buf + 1, 10, &nlink_diff);
1024 if (err < 0)
1025 goto fail;
1026
1027 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
1028 nlink += nlink_diff;
1029
1030 if (nlink <= 0)
1031 goto fail;
1032
1033 return nlink;
1034
1035 fail:
1036 pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n",
1037 upperdentry, err);
1038 return fallback;
1039 }
1040
ovl_new_inode(struct super_block * sb,umode_t mode,dev_t rdev)1041 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
1042 {
1043 struct inode *inode;
1044
1045 inode = new_inode(sb);
1046 if (inode)
1047 ovl_fill_inode(inode, mode, rdev);
1048
1049 return inode;
1050 }
1051
ovl_inode_test(struct inode * inode,void * data)1052 static int ovl_inode_test(struct inode *inode, void *data)
1053 {
1054 return inode->i_private == data;
1055 }
1056
ovl_inode_set(struct inode * inode,void * data)1057 static int ovl_inode_set(struct inode *inode, void *data)
1058 {
1059 inode->i_private = data;
1060 return 0;
1061 }
1062
ovl_verify_inode(struct inode * inode,struct dentry * lowerdentry,struct dentry * upperdentry,bool strict)1063 static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
1064 struct dentry *upperdentry, bool strict)
1065 {
1066 /*
1067 * For directories, @strict verify from lookup path performs consistency
1068 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
1069 * inode. Non @strict verify from NFS handle decode path passes NULL for
1070 * 'unknown' lower/upper.
1071 */
1072 if (S_ISDIR(inode->i_mode) && strict) {
1073 /* Real lower dir moved to upper layer under us? */
1074 if (!lowerdentry && ovl_inode_lower(inode))
1075 return false;
1076
1077 /* Lookup of an uncovered redirect origin? */
1078 if (!upperdentry && ovl_inode_upper(inode))
1079 return false;
1080 }
1081
1082 /*
1083 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
1084 * This happens when finding a copied up overlay inode for a renamed
1085 * or hardlinked overlay dentry and lower dentry cannot be followed
1086 * by origin because lower fs does not support file handles.
1087 */
1088 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
1089 return false;
1090
1091 /*
1092 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
1093 * This happens when finding a lower alias for a copied up hard link.
1094 */
1095 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
1096 return false;
1097
1098 return true;
1099 }
1100
ovl_lookup_inode(struct super_block * sb,struct dentry * real,bool is_upper)1101 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
1102 bool is_upper)
1103 {
1104 struct inode *inode, *key = d_inode(real);
1105
1106 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
1107 if (!inode)
1108 return NULL;
1109
1110 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
1111 is_upper ? real : NULL, false)) {
1112 iput(inode);
1113 return ERR_PTR(-ESTALE);
1114 }
1115
1116 return inode;
1117 }
1118
ovl_lookup_trap_inode(struct super_block * sb,struct dentry * dir)1119 bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
1120 {
1121 struct inode *key = d_inode(dir);
1122 struct inode *trap;
1123 bool res;
1124
1125 trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
1126 if (!trap)
1127 return false;
1128
1129 res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
1130 !ovl_inode_lower(trap);
1131
1132 iput(trap);
1133 return res;
1134 }
1135
1136 /*
1137 * Create an inode cache entry for layer root dir, that will intentionally
1138 * fail ovl_verify_inode(), so any lookup that will find some layer root
1139 * will fail.
1140 */
ovl_get_trap_inode(struct super_block * sb,struct dentry * dir)1141 struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
1142 {
1143 struct inode *key = d_inode(dir);
1144 struct inode *trap;
1145
1146 if (!d_is_dir(dir))
1147 return ERR_PTR(-ENOTDIR);
1148
1149 trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
1150 ovl_inode_set, key);
1151 if (!trap)
1152 return ERR_PTR(-ENOMEM);
1153
1154 if (!(inode_state_read_once(trap) & I_NEW)) {
1155 /* Conflicting layer roots? */
1156 iput(trap);
1157 return ERR_PTR(-ELOOP);
1158 }
1159
1160 trap->i_mode = S_IFDIR;
1161 trap->i_flags = S_DEAD;
1162 unlock_new_inode(trap);
1163
1164 return trap;
1165 }
1166
1167 /*
1168 * Does overlay inode need to be hashed by lower inode?
1169 */
ovl_hash_bylower(struct super_block * sb,struct dentry * upper,struct dentry * lower,bool index)1170 static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
1171 struct dentry *lower, bool index)
1172 {
1173 struct ovl_fs *ofs = OVL_FS(sb);
1174
1175 /* No, if pure upper */
1176 if (!lower)
1177 return false;
1178
1179 /* Yes, if already indexed */
1180 if (index)
1181 return true;
1182
1183 /* Yes, if won't be copied up */
1184 if (!ovl_upper_mnt(ofs))
1185 return true;
1186
1187 /* No, if lower hardlink is or will be broken on copy up */
1188 if ((upper || !ovl_indexdir(sb)) &&
1189 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
1190 return false;
1191
1192 /* No, if non-indexed upper with NFS export */
1193 if (ofs->config.nfs_export && upper)
1194 return false;
1195
1196 /* Otherwise, hash by lower inode for fsnotify */
1197 return true;
1198 }
1199
ovl_iget5(struct super_block * sb,struct inode * newinode,struct inode * key)1200 static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
1201 struct inode *key)
1202 {
1203 return newinode ? inode_insert5(newinode, (unsigned long) key,
1204 ovl_inode_test, ovl_inode_set, key) :
1205 iget5_locked(sb, (unsigned long) key,
1206 ovl_inode_test, ovl_inode_set, key);
1207 }
1208
ovl_get_inode(struct super_block * sb,struct ovl_inode_params * oip)1209 struct inode *ovl_get_inode(struct super_block *sb,
1210 struct ovl_inode_params *oip)
1211 {
1212 struct ovl_fs *ofs = OVL_FS(sb);
1213 struct dentry *upperdentry = oip->upperdentry;
1214 struct ovl_path *lowerpath = ovl_lowerpath(oip->oe);
1215 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
1216 struct inode *inode;
1217 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
1218 struct path realpath = {
1219 .dentry = upperdentry ?: lowerdentry,
1220 .mnt = upperdentry ? ovl_upper_mnt(ofs) : lowerpath->layer->mnt,
1221 };
1222 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
1223 oip->index);
1224 int fsid = bylower ? lowerpath->layer->fsid : 0;
1225 bool is_dir;
1226 unsigned long ino = 0;
1227 int err = oip->newinode ? -EEXIST : -ENOMEM;
1228
1229 if (!realinode)
1230 realinode = d_inode(lowerdentry);
1231
1232 /*
1233 * Copy up origin (lower) may exist for non-indexed upper, but we must
1234 * not use lower as hash key if this is a broken hardlink.
1235 */
1236 is_dir = S_ISDIR(realinode->i_mode);
1237 if (upperdentry || bylower) {
1238 struct inode *key = d_inode(bylower ? lowerdentry :
1239 upperdentry);
1240 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
1241
1242 inode = ovl_iget5(sb, oip->newinode, key);
1243 if (!inode)
1244 goto out_err;
1245 if (!(inode_state_read_once(inode) & I_NEW)) {
1246 /*
1247 * Verify that the underlying files stored in the inode
1248 * match those in the dentry.
1249 */
1250 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
1251 true)) {
1252 iput(inode);
1253 err = -ESTALE;
1254 goto out_err;
1255 }
1256
1257 dput(upperdentry);
1258 ovl_free_entry(oip->oe);
1259 kfree(oip->redirect);
1260 kfree(oip->lowerdata_redirect);
1261 goto out;
1262 }
1263
1264 /* Recalculate nlink for non-dir due to indexing */
1265 if (!is_dir)
1266 nlink = ovl_get_nlink(ofs, lowerdentry, upperdentry,
1267 nlink);
1268 set_nlink(inode, nlink);
1269 ino = key->i_ino;
1270 } else {
1271 /* Lower hardlink that will be broken on copy up */
1272 inode = new_inode(sb);
1273 if (!inode) {
1274 err = -ENOMEM;
1275 goto out_err;
1276 }
1277 ino = realinode->i_ino;
1278 fsid = lowerpath->layer->fsid;
1279 }
1280 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
1281 ovl_inode_init(inode, oip, ino, fsid);
1282 WARN_ON_ONCE(!!IS_CASEFOLDED(inode) != ofs->casefold);
1283
1284 if (upperdentry && ovl_is_impuredir(sb, upperdentry))
1285 ovl_set_flag(OVL_IMPURE, inode);
1286
1287 if (oip->index)
1288 ovl_set_flag(OVL_INDEX, inode);
1289
1290 if (bylower)
1291 ovl_set_flag(OVL_CONST_INO, inode);
1292
1293 /* Check for non-merge dir that may have whiteouts */
1294 if (is_dir) {
1295 if (((upperdentry && lowerdentry) || ovl_numlower(oip->oe) > 1) ||
1296 ovl_path_check_origin_xattr(ofs, &realpath)) {
1297 ovl_set_flag(OVL_WHITEOUTS, inode);
1298 }
1299 }
1300
1301 /* Check for immutable/append-only inode flags in xattr */
1302 if (upperdentry)
1303 ovl_check_protattr(inode, upperdentry);
1304
1305 if (inode_state_read_once(inode) & I_NEW)
1306 unlock_new_inode(inode);
1307 out:
1308 return inode;
1309
1310 out_err:
1311 pr_warn_ratelimited("failed to get inode (%i)\n", err);
1312 inode = ERR_PTR(err);
1313 goto out;
1314 }
1315