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