From: Valerie Aurora <vaurora@xxxxxxxxxx> In readdir(), client file systems need to lookup the target of a fallthru in a lower layer for three reasons: (1) fill in d_ino, (2) fill in d_type, (2) make sure there is something to fall through to (and if not, don't return this dentry). Create a generic helper function. Signed-off-by: Valerie Aurora <valerie.aurora@xxxxxxxxx> --- fs/union.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/union.h | 3 ++ include/linux/fs.h | 15 ++++++++++++++ 3 files changed, 73 insertions(+), 0 deletions(-) diff --git a/fs/union.c b/fs/union.c index 62bad6a..2fdc095 100644 --- a/fs/union.c +++ b/fs/union.c @@ -335,3 +335,58 @@ out_fput: mnt_drop_write(topmost_path->mnt); return error; } + +/* Relationship between i_mode and the DT_xxx types */ +static inline unsigned char dt_type(struct inode *inode) +{ + return (inode->i_mode >> 12) & 15; +} + +/** + * generic_readdir_fallthru - Helper to lookup target of a fallthru + * + * @topmost_dentry: dentry for the topmost dentry of the dir being read + * @name: name of fallthru dirent + * @namelen: length of @name + * @ino: return inode number of target, if found + * @d_type: return directory type of target, if found + * + * In readdir(), client file systems need to lookup the target of a + * fallthru in a lower layer for three reasons: (1) fill in d_ino, (2) + * fill in d_type, (2) make sure there is something to fall through to + * (and if not, don't return this dentry). Upon detecting a fallthru + * dentry in readdir(), the client file system should call this function. + * + * Returns 0 on success and -ENOENT if no matching directory entry was + * found (which can happen when the topmost file system is unmounted + * and remounted over a different file system than). Any other errors + * are unexpected. + */ + +int +generic_readdir_fallthru(struct dentry *topmost_dentry, const char *name, + int namlen, ino_t *ino, unsigned char *d_type) +{ + struct path *parent; + struct dentry *dentry; + unsigned int i, layers = topmost_dentry->d_sb->s_union_count; + + BUG_ON(!mutex_is_locked(&topmost_dentry->d_inode->i_mutex)); + + for (i = 0; i < layers; i++) { + parent = union_find_dir(topmost_dentry, i); + mutex_lock(&parent->dentry->d_inode->i_mutex); + dentry = lookup_one_len(name, parent->dentry, namlen); + mutex_unlock(&parent->dentry->d_inode->i_mutex); + if (IS_ERR(dentry)) + return PTR_ERR(dentry); + if (dentry->d_inode) { + *ino = dentry->d_inode->i_ino; + *d_type = dt_type(dentry->d_inode); + dput(dentry); + return 0; + } + dput(dentry); + } + return -ENOENT; +} diff --git a/fs/union.h b/fs/union.h index 75c8d8b..4620cc1 100644 --- a/fs/union.h +++ b/fs/union.h @@ -57,6 +57,8 @@ extern int union_add_dir(struct path *, struct path *, unsigned int); extern int union_create_topmost_dir(struct path *, struct qstr *, struct path *, struct path *); extern int union_copyup_dir(struct path *); +extern int generic_readdir_fallthru(struct dentry *topmost_dentry, const char *name, + int namlen, ino_t *ino, unsigned char *d_type); static inline int needs_lookup_union(struct path *parent_path, struct path *path) { @@ -90,6 +92,7 @@ static inline struct path *union_find_dir(struct dentry *dentry, #define union_create_topmost_dir(w, x, y, z) ({ BUG(); (0); }) #define needs_lookup_union(x, y) ({ (0); }) #define union_copyup_dir(x) ({ BUG(); (0); }) +#define generic_readdir_fallthru(w, x, y, z) ({ BUG(); (0); }) #endif /* CONFIG_UNION_MOUNT */ #endif /* __KERNEL__ */ diff --git a/include/linux/fs.h b/include/linux/fs.h index 258f99b..3dfb0e2 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2403,6 +2403,21 @@ extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, extern int generic_file_fsync(struct file *, int); +#ifdef CONFIG_UNION_MOUNT +extern int generic_readdir_fallthru(struct dentry *topmost_dentry, const char *name, + int namlen, ino_t *ino, unsigned char *d_type); +#else +static inline int generic_readdir_fallthru(struct dentry *topmost_dentry, const char *name, + int namlen, ino_t *ino, unsigned char *d_type) +{ + /* + * Found a fallthru on a kernel without union support. + * There's nothing to fall through to, so return -ENOENT. + */ + return -ENOENT; +} +#endif + #ifdef CONFIG_MIGRATION extern int buffer_migrate_page(struct address_space *, struct page *, struct page *); -- 1.7.0.4 -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html