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