]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
vfs: introduce inode 'inuse' lock
authorAmir Goldstein <amir73il@gmail.com>
Wed, 21 Jun 2017 12:28:32 +0000 (15:28 +0300)
committerMiklos Szeredi <mszeredi@redhat.com>
Tue, 4 Jul 2017 20:03:16 +0000 (22:03 +0200)
Added an i_state flag I_INUSE and helpers to set/clear/test the bit.

The 'inuse' lock is an 'advisory' inode lock, that can be used to extend
exclusive create protection beyond parent->i_mutex lock among cooperating
users.

This is going to be used by overlayfs to get exclusive ownership on upper
and work dirs among overlayfs mounts.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
fs/overlayfs/overlayfs.h
fs/overlayfs/util.c
include/linux/fs.h

index b1be3d39ac9dafaea89f26a4bc6dc459f99725a5..5e958427463d1d716a5176e039f46fea73a687c0 100644 (file)
@@ -220,6 +220,8 @@ int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
 void ovl_set_flag(unsigned long flag, struct inode *inode);
 bool ovl_test_flag(unsigned long flag, struct inode *inode);
+bool ovl_inuse_trylock(struct dentry *dentry);
+void ovl_inuse_unlock(struct dentry *dentry);
 
 static inline bool ovl_is_impuredir(struct dentry *dentry)
 {
index f093fcf2b4bd78872d80e4f53b9d54f00d62d0ec..adccd74162d6387b74d97c85c1cbd01dcc5b2802 100644 (file)
@@ -347,3 +347,34 @@ bool ovl_test_flag(unsigned long flag, struct inode *inode)
 {
        return test_bit(flag, &OVL_I(inode)->flags);
 }
+
+/**
+ * Caller must hold a reference to inode to prevent it from being freed while
+ * it is marked inuse.
+ */
+bool ovl_inuse_trylock(struct dentry *dentry)
+{
+       struct inode *inode = d_inode(dentry);
+       bool locked = false;
+
+       spin_lock(&inode->i_lock);
+       if (!(inode->i_state & I_OVL_INUSE)) {
+               inode->i_state |= I_OVL_INUSE;
+               locked = true;
+       }
+       spin_unlock(&inode->i_lock);
+
+       return locked;
+}
+
+void ovl_inuse_unlock(struct dentry *dentry)
+{
+       if (dentry) {
+               struct inode *inode = d_inode(dentry);
+
+               spin_lock(&inode->i_lock);
+               WARN_ON(!(inode->i_state & I_OVL_INUSE));
+               inode->i_state &= ~I_OVL_INUSE;
+               spin_unlock(&inode->i_lock);
+       }
+}
index 3e68cabb8457e5a9ae5b53bd9d7aa0f844ee99fa..75a5fafaf096a5f4a85a69390dc0f6a82b105608 100644 (file)
@@ -1930,6 +1930,9 @@ static inline bool HAS_UNMAPPED_ID(struct inode *inode)
  *                     wb stat updates to grab mapping->tree_lock.  See
  *                     inode_switch_wb_work_fn() for details.
  *
+ * I_OVL_INUSE         Used by overlayfs to get exclusive ownership on upper
+ *                     and work dirs among overlayfs mounts.
+ *
  * Q: What is the difference between I_WILL_FREE and I_FREEING?
  */
 #define I_DIRTY_SYNC           (1 << 0)
@@ -1950,6 +1953,7 @@ static inline bool HAS_UNMAPPED_ID(struct inode *inode)
 #define __I_DIRTY_TIME_EXPIRED 12
 #define I_DIRTY_TIME_EXPIRED   (1 << __I_DIRTY_TIME_EXPIRED)
 #define I_WB_SWITCH            (1 << 13)
+#define I_OVL_INUSE                    (1 << 14)
 
 #define I_DIRTY (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_PAGES)
 #define I_DIRTY_ALL (I_DIRTY | I_DIRTY_TIME)