Skip to content

Commit 4bacc9c

Browse files
dhowellsAl Viro
authored andcommitted
overlayfs: Make f_path always point to the overlay and f_inode to the underlay
Make file->f_path always point to the overlay dentry so that the path in /proc/pid/fd is correct and to ensure that label-based LSMs have access to the overlay as well as the underlay (path-based LSMs probably don't need it). Using my union testsuite to set things up, before the patch I see: [root@andromeda union-testsuite]# bash 5</mnt/a/foo107 [root@andromeda union-testsuite]# ls -l /proc/$$/fd/ ... lr-x------. 1 root root 64 Jun 5 14:38 5 -> /a/foo107 [root@andromeda union-testsuite]# stat /mnt/a/foo107 ... Device: 23h/35d Inode: 13381 Links: 1 ... [root@andromeda union-testsuite]# stat -L /proc/$$/fd/5 ... Device: 23h/35d Inode: 13381 Links: 1 ... After the patch: [root@andromeda union-testsuite]# bash 5</mnt/a/foo107 [root@andromeda union-testsuite]# ls -l /proc/$$/fd/ ... lr-x------. 1 root root 64 Jun 5 14:22 5 -> /mnt/a/foo107 [root@andromeda union-testsuite]# stat /mnt/a/foo107 ... Device: 23h/35d Inode: 40346 Links: 1 ... [root@andromeda union-testsuite]# stat -L /proc/$$/fd/5 ... Device: 23h/35d Inode: 40346 Links: 1 ... Note the change in where /proc/$$/fd/5 points to in the ls command. It was pointing to /a/foo107 (which doesn't exist) and now points to /mnt/a/foo107 (which is correct). The inode accessed, however, is the lower layer. The union layer is on device 25h/37d and the upper layer on 24h/36d. Signed-off-by: David Howells <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent f25801e commit 4bacc9c

File tree

8 files changed

+41
-34
lines changed

8 files changed

+41
-34
lines changed

fs/dcache.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,8 @@ void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
16731673
DCACHE_OP_COMPARE |
16741674
DCACHE_OP_REVALIDATE |
16751675
DCACHE_OP_WEAK_REVALIDATE |
1676-
DCACHE_OP_DELETE ));
1676+
DCACHE_OP_DELETE |
1677+
DCACHE_OP_SELECT_INODE));
16771678
dentry->d_op = op;
16781679
if (!op)
16791680
return;
@@ -1689,6 +1690,8 @@ void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
16891690
dentry->d_flags |= DCACHE_OP_DELETE;
16901691
if (op->d_prune)
16911692
dentry->d_flags |= DCACHE_OP_PRUNE;
1693+
if (op->d_select_inode)
1694+
dentry->d_flags |= DCACHE_OP_SELECT_INODE;
16921695

16931696
}
16941697
EXPORT_SYMBOL(d_set_d_op);

fs/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ extern struct file *do_file_open_root(struct dentry *, struct vfsmount *,
107107
extern long do_handle_open(int mountdirfd,
108108
struct file_handle __user *ufh, int open_flag);
109109
extern int open_check_o_direct(struct file *f);
110+
extern int vfs_open(const struct path *, struct file *, const struct cred *);
110111

111112
/*
112113
* inode.c

fs/open.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -678,18 +678,18 @@ int open_check_o_direct(struct file *f)
678678
}
679679

680680
static int do_dentry_open(struct file *f,
681+
struct inode *inode,
681682
int (*open)(struct inode *, struct file *),
682683
const struct cred *cred)
683684
{
684685
static const struct file_operations empty_fops = {};
685-
struct inode *inode;
686686
int error;
687687

688688
f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
689689
FMODE_PREAD | FMODE_PWRITE;
690690

691691
path_get(&f->f_path);
692-
inode = f->f_inode = f->f_path.dentry->d_inode;
692+
f->f_inode = inode;
693693
f->f_mapping = inode->i_mapping;
694694

695695
if (unlikely(f->f_flags & O_PATH)) {
@@ -793,7 +793,8 @@ int finish_open(struct file *file, struct dentry *dentry,
793793
BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
794794

795795
file->f_path.dentry = dentry;
796-
error = do_dentry_open(file, open, current_cred());
796+
error = do_dentry_open(file, d_backing_inode(dentry), open,
797+
current_cred());
797798
if (!error)
798799
*opened |= FILE_OPENED;
799800

@@ -822,6 +823,28 @@ int finish_no_open(struct file *file, struct dentry *dentry)
822823
}
823824
EXPORT_SYMBOL(finish_no_open);
824825

826+
/**
827+
* vfs_open - open the file at the given path
828+
* @path: path to open
829+
* @file: newly allocated file with f_flag initialized
830+
* @cred: credentials to use
831+
*/
832+
int vfs_open(const struct path *path, struct file *file,
833+
const struct cred *cred)
834+
{
835+
struct dentry *dentry = path->dentry;
836+
struct inode *inode = dentry->d_inode;
837+
838+
file->f_path = *path;
839+
if (dentry->d_flags & DCACHE_OP_SELECT_INODE) {
840+
inode = dentry->d_op->d_select_inode(dentry, file->f_flags);
841+
if (IS_ERR(inode))
842+
return PTR_ERR(inode);
843+
}
844+
845+
return do_dentry_open(file, inode, NULL, cred);
846+
}
847+
825848
struct file *dentry_open(const struct path *path, int flags,
826849
const struct cred *cred)
827850
{
@@ -853,26 +876,6 @@ struct file *dentry_open(const struct path *path, int flags,
853876
}
854877
EXPORT_SYMBOL(dentry_open);
855878

856-
/**
857-
* vfs_open - open the file at the given path
858-
* @path: path to open
859-
* @filp: newly allocated file with f_flag initialized
860-
* @cred: credentials to use
861-
*/
862-
int vfs_open(const struct path *path, struct file *filp,
863-
const struct cred *cred)
864-
{
865-
struct inode *inode = path->dentry->d_inode;
866-
867-
if (inode->i_op->dentry_open)
868-
return inode->i_op->dentry_open(path->dentry, filp, cred);
869-
else {
870-
filp->f_path = *path;
871-
return do_dentry_open(filp, NULL, cred);
872-
}
873-
}
874-
EXPORT_SYMBOL(vfs_open);
875-
876879
static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
877880
{
878881
int lookup_flags = 0;

fs/overlayfs/inode.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,31 +337,30 @@ static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
337337
return true;
338338
}
339339

340-
static int ovl_dentry_open(struct dentry *dentry, struct file *file,
341-
const struct cred *cred)
340+
struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
342341
{
343342
int err;
344343
struct path realpath;
345344
enum ovl_path_type type;
346345

347346
type = ovl_path_real(dentry, &realpath);
348-
if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
347+
if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
349348
err = ovl_want_write(dentry);
350349
if (err)
351-
return err;
350+
return ERR_PTR(err);
352351

353-
if (file->f_flags & O_TRUNC)
352+
if (file_flags & O_TRUNC)
354353
err = ovl_copy_up_last(dentry, NULL, true);
355354
else
356355
err = ovl_copy_up(dentry);
357356
ovl_drop_write(dentry);
358357
if (err)
359-
return err;
358+
return ERR_PTR(err);
360359

361360
ovl_path_upper(dentry, &realpath);
362361
}
363362

364-
return vfs_open(&realpath, file, cred);
363+
return d_backing_inode(realpath.dentry);
365364
}
366365

367366
static const struct inode_operations ovl_file_inode_operations = {
@@ -372,7 +371,6 @@ static const struct inode_operations ovl_file_inode_operations = {
372371
.getxattr = ovl_getxattr,
373372
.listxattr = ovl_listxattr,
374373
.removexattr = ovl_removexattr,
375-
.dentry_open = ovl_dentry_open,
376374
};
377375

378376
static const struct inode_operations ovl_symlink_inode_operations = {

fs/overlayfs/overlayfs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
173173
void *value, size_t size);
174174
ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
175175
int ovl_removexattr(struct dentry *dentry, const char *name);
176+
struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags);
176177

177178
struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
178179
struct ovl_entry *oe);

fs/overlayfs/super.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ static void ovl_dentry_release(struct dentry *dentry)
275275

276276
static const struct dentry_operations ovl_dentry_operations = {
277277
.d_release = ovl_dentry_release,
278+
.d_select_inode = ovl_d_select_inode,
278279
};
279280

280281
static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)

include/linux/dcache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ struct dentry_operations {
160160
char *(*d_dname)(struct dentry *, char *, int);
161161
struct vfsmount *(*d_automount)(struct path *);
162162
int (*d_manage)(struct dentry *, bool);
163+
struct inode *(*d_select_inode)(struct dentry *, unsigned);
163164
} ____cacheline_aligned;
164165

165166
/*
@@ -225,6 +226,7 @@ struct dentry_operations {
225226

226227
#define DCACHE_MAY_FREE 0x00800000
227228
#define DCACHE_FALLTHRU 0x01000000 /* Fall through to lower layer */
229+
#define DCACHE_OP_SELECT_INODE 0x02000000 /* Unioned entry: dcache op selects inode */
228230

229231
extern seqlock_t rename_lock;
230232

include/linux/fs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,6 @@ struct inode_operations {
16411641
int (*set_acl)(struct inode *, struct posix_acl *, int);
16421642

16431643
/* WARNING: probably going away soon, do not use! */
1644-
int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
16451644
} ____cacheline_aligned;
16461645

16471646
ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
@@ -2194,7 +2193,6 @@ extern struct file *file_open_name(struct filename *, int, umode_t);
21942193
extern struct file *filp_open(const char *, int, umode_t);
21952194
extern struct file *file_open_root(struct dentry *, struct vfsmount *,
21962195
const char *, int);
2197-
extern int vfs_open(const struct path *, struct file *, const struct cred *);
21982196
extern struct file * dentry_open(const struct path *, int, const struct cred *);
21992197
extern int filp_close(struct file *, fl_owner_t id);
22002198

0 commit comments

Comments
 (0)