2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
11 #include <linux/mount.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
14 #include <linux/xattr.h>
15 #include <linux/exportfs.h>
16 #include <linux/uuid.h>
17 #include <linux/namei.h>
18 #include <linux/ratelimit.h>
19 #include "overlayfs.h"
21 int ovl_want_write(struct dentry *dentry)
23 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
24 return mnt_want_write(ofs->upper_mnt);
27 void ovl_drop_write(struct dentry *dentry)
29 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
30 mnt_drop_write(ofs->upper_mnt);
33 struct dentry *ovl_workdir(struct dentry *dentry)
35 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
39 const struct cred *ovl_override_creds(struct super_block *sb)
41 struct ovl_fs *ofs = sb->s_fs_info;
43 return override_creds(ofs->creator_cred);
46 struct super_block *ovl_same_sb(struct super_block *sb)
48 struct ovl_fs *ofs = sb->s_fs_info;
53 bool ovl_can_decode_fh(struct super_block *sb)
55 return (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
56 !uuid_is_null(&sb->s_uuid));
59 struct dentry *ovl_indexdir(struct super_block *sb)
61 struct ovl_fs *ofs = sb->s_fs_info;
66 /* Index all files on copy up. For now only enabled for NFS export */
67 bool ovl_index_all(struct super_block *sb)
69 struct ovl_fs *ofs = sb->s_fs_info;
71 return ofs->config.nfs_export && ofs->config.index;
74 /* Verify lower origin on lookup. For now only enabled for NFS export */
75 bool ovl_verify_lower(struct super_block *sb)
77 struct ovl_fs *ofs = sb->s_fs_info;
79 return ofs->config.nfs_export && ofs->config.index;
82 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
84 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
85 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
88 oe->numlower = numlower;
93 bool ovl_dentry_remote(struct dentry *dentry)
95 return dentry->d_flags &
96 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
100 bool ovl_dentry_weird(struct dentry *dentry)
102 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
103 DCACHE_MANAGE_TRANSIT |
108 enum ovl_path_type ovl_path_type(struct dentry *dentry)
110 struct ovl_entry *oe = dentry->d_fsdata;
111 enum ovl_path_type type = 0;
113 if (ovl_dentry_upper(dentry)) {
114 type = __OVL_PATH_UPPER;
117 * Non-dir dentry can hold lower dentry of its copy up origin.
120 type |= __OVL_PATH_ORIGIN;
121 if (d_is_dir(dentry))
122 type |= __OVL_PATH_MERGE;
125 if (oe->numlower > 1)
126 type |= __OVL_PATH_MERGE;
131 void ovl_path_upper(struct dentry *dentry, struct path *path)
133 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
135 path->mnt = ofs->upper_mnt;
136 path->dentry = ovl_dentry_upper(dentry);
139 void ovl_path_lower(struct dentry *dentry, struct path *path)
141 struct ovl_entry *oe = dentry->d_fsdata;
144 path->mnt = oe->lowerstack[0].layer->mnt;
145 path->dentry = oe->lowerstack[0].dentry;
147 *path = (struct path) { };
151 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
153 enum ovl_path_type type = ovl_path_type(dentry);
155 if (!OVL_TYPE_UPPER(type))
156 ovl_path_lower(dentry, path);
158 ovl_path_upper(dentry, path);
163 struct dentry *ovl_dentry_upper(struct dentry *dentry)
165 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
168 struct dentry *ovl_dentry_lower(struct dentry *dentry)
170 struct ovl_entry *oe = dentry->d_fsdata;
172 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
175 struct dentry *ovl_dentry_real(struct dentry *dentry)
177 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
180 struct dentry *ovl_i_dentry_upper(struct inode *inode)
182 return ovl_upperdentry_dereference(OVL_I(inode));
185 struct inode *ovl_inode_upper(struct inode *inode)
187 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
189 return upperdentry ? d_inode(upperdentry) : NULL;
192 struct inode *ovl_inode_lower(struct inode *inode)
194 return OVL_I(inode)->lower;
197 struct inode *ovl_inode_real(struct inode *inode)
199 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
203 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
205 return OVL_I(inode)->cache;
208 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
210 OVL_I(inode)->cache = cache;
213 bool ovl_dentry_is_opaque(struct dentry *dentry)
215 struct ovl_entry *oe = dentry->d_fsdata;
219 bool ovl_dentry_is_whiteout(struct dentry *dentry)
221 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
224 void ovl_dentry_set_opaque(struct dentry *dentry)
226 struct ovl_entry *oe = dentry->d_fsdata;
232 * For hard links it's possible for ovl_dentry_upper() to return positive, while
233 * there's no actual upper alias for the inode. Copy up code needs to know
234 * about the existence of the upper alias, so it can't use ovl_dentry_upper().
236 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
238 struct ovl_entry *oe = dentry->d_fsdata;
240 return oe->has_upper;
243 void ovl_dentry_set_upper_alias(struct dentry *dentry)
245 struct ovl_entry *oe = dentry->d_fsdata;
247 oe->has_upper = true;
250 bool ovl_redirect_dir(struct super_block *sb)
252 struct ovl_fs *ofs = sb->s_fs_info;
254 return ofs->config.redirect_dir && !ofs->noxattr;
257 const char *ovl_dentry_get_redirect(struct dentry *dentry)
259 return OVL_I(d_inode(dentry))->redirect;
262 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
264 struct ovl_inode *oi = OVL_I(d_inode(dentry));
267 oi->redirect = redirect;
270 void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
271 struct dentry *lowerdentry)
274 OVL_I(inode)->__upperdentry = upperdentry;
276 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
278 ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
281 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
283 struct inode *upperinode = d_inode(upperdentry);
285 WARN_ON(OVL_I(inode)->__upperdentry);
288 * Make sure upperdentry is consistent before making it visible
291 OVL_I(inode)->__upperdentry = upperdentry;
292 if (inode_unhashed(inode)) {
293 inode->i_private = upperinode;
294 __insert_inode_hash(inode, (unsigned long) upperinode);
298 void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
300 struct inode *inode = d_inode(dentry);
302 WARN_ON(!inode_is_locked(inode));
304 * Version is used by readdir code to keep cache consistent. For merge
305 * dirs all changes need to be noted. For non-merge dirs, cache only
306 * contains impure (ones which have been copied up and have origins)
307 * entries, so only need to note changes to impure entries.
309 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
310 OVL_I(inode)->version++;
313 u64 ovl_dentry_version_get(struct dentry *dentry)
315 struct inode *inode = d_inode(dentry);
317 WARN_ON(!inode_is_locked(inode));
318 return OVL_I(inode)->version;
321 bool ovl_is_whiteout(struct dentry *dentry)
323 struct inode *inode = dentry->d_inode;
325 return inode && IS_WHITEOUT(inode);
328 struct file *ovl_path_open(struct path *path, int flags)
330 return dentry_open(path, flags | O_NOATIME, current_cred());
333 int ovl_copy_up_start(struct dentry *dentry)
335 struct ovl_inode *oi = OVL_I(d_inode(dentry));
338 err = mutex_lock_interruptible(&oi->lock);
339 if (!err && ovl_dentry_has_upper_alias(dentry)) {
340 err = 1; /* Already copied up */
341 mutex_unlock(&oi->lock);
347 void ovl_copy_up_end(struct dentry *dentry)
349 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
352 bool ovl_check_origin_xattr(struct dentry *dentry)
356 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
358 /* Zero size value means "copied up but origin unknown" */
365 bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
370 if (!d_is_dir(dentry))
373 res = vfs_getxattr(dentry, name, &val, 1);
374 if (res == 1 && val == 'y')
380 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
381 const char *name, const void *value, size_t size,
385 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
390 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
392 if (err == -EOPNOTSUPP) {
393 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
401 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
405 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
409 * Do not fail when upper doesn't support xattrs.
410 * Upper inodes won't have origin nor redirect xattr anyway.
412 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
415 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
420 void ovl_set_flag(unsigned long flag, struct inode *inode)
422 set_bit(flag, &OVL_I(inode)->flags);
425 void ovl_clear_flag(unsigned long flag, struct inode *inode)
427 clear_bit(flag, &OVL_I(inode)->flags);
430 bool ovl_test_flag(unsigned long flag, struct inode *inode)
432 return test_bit(flag, &OVL_I(inode)->flags);
436 * Caller must hold a reference to inode to prevent it from being freed while
437 * it is marked inuse.
439 bool ovl_inuse_trylock(struct dentry *dentry)
441 struct inode *inode = d_inode(dentry);
444 spin_lock(&inode->i_lock);
445 if (!(inode->i_state & I_OVL_INUSE)) {
446 inode->i_state |= I_OVL_INUSE;
449 spin_unlock(&inode->i_lock);
454 void ovl_inuse_unlock(struct dentry *dentry)
457 struct inode *inode = d_inode(dentry);
459 spin_lock(&inode->i_lock);
460 WARN_ON(!(inode->i_state & I_OVL_INUSE));
461 inode->i_state &= ~I_OVL_INUSE;
462 spin_unlock(&inode->i_lock);
466 /* Caller must hold OVL_I(inode)->lock */
467 static void ovl_cleanup_index(struct dentry *dentry)
469 struct inode *dir = ovl_indexdir(dentry->d_sb)->d_inode;
470 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
471 struct dentry *upperdentry = ovl_dentry_upper(dentry);
472 struct dentry *index = NULL;
477 err = ovl_get_index_name(lowerdentry, &name);
481 inode = d_inode(upperdentry);
482 if (inode->i_nlink != 1) {
483 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
484 upperdentry, inode->i_ino, inode->i_nlink);
486 * We either have a bug with persistent union nlink or a lower
487 * hardlink was added while overlay is mounted. Adding a lower
488 * hardlink and then unlinking all overlay hardlinks would drop
489 * overlay nlink to zero before all upper inodes are unlinked.
490 * As a safety measure, when that situation is detected, set
491 * the overlay nlink to the index inode nlink minus one for the
492 * index entry itself.
494 set_nlink(d_inode(dentry), inode->i_nlink - 1);
495 ovl_set_nlink_upper(dentry);
499 inode_lock_nested(dir, I_MUTEX_PARENT);
500 /* TODO: whiteout instead of cleanup to block future open by handle */
501 index = lookup_one_len(name.name, ovl_indexdir(dentry->d_sb), name.len);
502 err = PTR_ERR(index);
504 err = ovl_cleanup(dir, index);
517 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
522 * Operations that change overlay inode and upper inode nlink need to be
523 * synchronized with copy up for persistent nlink accounting.
525 int ovl_nlink_start(struct dentry *dentry, bool *locked)
527 struct ovl_inode *oi = OVL_I(d_inode(dentry));
528 const struct cred *old_cred;
531 if (!d_inode(dentry) || d_is_dir(dentry))
535 * With inodes index is enabled, we store the union overlay nlink
536 * in an xattr on the index inode. When whiting out lower hardlinks
537 * we need to decrement the overlay persistent nlink, but before the
538 * first copy up, we have no upper index inode to store the xattr.
540 * As a workaround, before whiteout/rename over of a lower hardlink,
541 * copy up to create the upper index. Creating the upper index will
542 * initialize the overlay nlink, so it could be dropped if unlink
543 * or rename succeeds.
545 * TODO: implement metadata only index copy up when called with
546 * ovl_copy_up_flags(dentry, O_PATH).
548 if (ovl_indexdir(dentry->d_sb) && !ovl_dentry_has_upper_alias(dentry) &&
549 d_inode(ovl_dentry_lower(dentry))->i_nlink > 1) {
550 err = ovl_copy_up(dentry);
555 err = mutex_lock_interruptible(&oi->lock);
559 if (!ovl_test_flag(OVL_INDEX, d_inode(dentry)))
562 old_cred = ovl_override_creds(dentry->d_sb);
564 * The overlay inode nlink should be incremented/decremented IFF the
565 * upper operation succeeds, along with nlink change of upper inode.
566 * Therefore, before link/unlink/rename, we store the union nlink
567 * value relative to the upper inode nlink in an upper inode xattr.
569 err = ovl_set_nlink_upper(dentry);
570 revert_creds(old_cred);
574 mutex_unlock(&oi->lock);
581 void ovl_nlink_end(struct dentry *dentry, bool locked)
584 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
585 d_inode(dentry)->i_nlink == 0) {
586 const struct cred *old_cred;
588 old_cred = ovl_override_creds(dentry->d_sb);
589 ovl_cleanup_index(dentry);
590 revert_creds(old_cred);
593 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
597 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
599 /* Workdir should not be the same as upperdir */
600 if (workdir == upperdir)
603 /* Workdir should not be subdir of upperdir and vice versa */
604 if (lock_rename(workdir, upperdir) != NULL)
610 unlock_rename(workdir, upperdir);
612 pr_err("overlayfs: failed to lock workdir+upperdir\n");