Skip to content

Commit 40ba433

Browse files
Al Virogregkh
authored andcommitted
new helper: inode_wrong_type()
commit 6e3e2c4 upstream. inode_wrong_type(inode, mode) returns true if setting inode->i_mode to given value would've changed the inode type. We have enough of those checks open-coded to make a helper worthwhile. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ded9137 commit 40ba433

10 files changed

Lines changed: 22 additions & 18 deletions

File tree

‎fs/9p/vfs_inode.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ static int v9fs_test_inode(struct inode *inode, void *data)
398398

399399
umode = p9mode2unixmode(v9ses, st, &rdev);
400400
/* don't match inode of different type */
401-
if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
401+
if (inode_wrong_type(inode, umode))
402402
return 0;
403403

404404
/* compare qid details */
@@ -1360,7 +1360,7 @@ int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
13601360
* Don't update inode if the file type is different
13611361
*/
13621362
umode = p9mode2unixmode(v9ses, st, &rdev);
1363-
if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
1363+
if (inode_wrong_type(inode, umode))
13641364
goto out;
13651365

13661366
/*

‎fs/9p/vfs_inode_dotl.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static int v9fs_test_inode_dotl(struct inode *inode, void *data)
5959
struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
6060

6161
/* don't match inode of different type */
62-
if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
62+
if (inode_wrong_type(inode, st->st_mode))
6363
return 0;
6464

6565
if (inode->i_generation != st->st_gen)
@@ -933,7 +933,7 @@ int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
933933
/*
934934
* Don't update inode if the file type is different
935935
*/
936-
if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
936+
if (inode_wrong_type(inode, st->st_mode))
937937
goto out;
938938

939939
/*

‎fs/cifs/inode.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,7 @@ int cifs_get_inode_info_unix(struct inode **pinode,
425425
}
426426

427427
/* if filetype is different, return error */
428-
if (unlikely(((*pinode)->i_mode & S_IFMT) !=
429-
(fattr.cf_mode & S_IFMT))) {
428+
if (unlikely(inode_wrong_type(*pinode, fattr.cf_mode))) {
430429
CIFS_I(*pinode)->time = 0; /* force reval */
431430
rc = -ESTALE;
432431
goto cgiiu_exit;
@@ -1243,7 +1242,7 @@ cifs_find_inode(struct inode *inode, void *opaque)
12431242
return 0;
12441243

12451244
/* don't match inode of different type */
1246-
if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
1245+
if (inode_wrong_type(inode, fattr->cf_mode))
12471246
return 0;
12481247

12491248
/* if it's not a directory or has no dentries, then flag it */

‎fs/fuse/dir.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
252252
if (ret == -ENOMEM)
253253
goto out;
254254
if (ret || fuse_invalid_attr(&outarg.attr) ||
255-
(outarg.attr.mode ^ inode->i_mode) & S_IFMT)
255+
inode_wrong_type(inode, outarg.attr.mode))
256256
goto invalid;
257257

258258
forget_all_cached_acls(inode);
@@ -1062,7 +1062,7 @@ static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
10621062
err = fuse_simple_request(fm, &args);
10631063
if (!err) {
10641064
if (fuse_invalid_attr(&outarg.attr) ||
1065-
(inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1065+
inode_wrong_type(inode, outarg.attr.mode)) {
10661066
fuse_make_bad(inode);
10671067
err = -EIO;
10681068
} else {
@@ -1699,7 +1699,7 @@ int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
16991699
}
17001700

17011701
if (fuse_invalid_attr(&outarg.attr) ||
1702-
(inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1702+
inode_wrong_type(inode, outarg.attr.mode)) {
17031703
fuse_make_bad(inode);
17041704
err = -EIO;
17051705
goto error;

‎fs/fuse/inode.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
340340
inode->i_generation = generation;
341341
fuse_init_inode(inode, attr);
342342
unlock_new_inode(inode);
343-
} else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
343+
} else if (inode_wrong_type(inode, attr->mode)) {
344344
/* Inode has changed type, any I/O on the old should fail */
345345
fuse_make_bad(inode);
346346
iput(inode);

‎fs/fuse/readdir.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static int fuse_direntplus_link(struct file *file,
202202
inode = d_inode(dentry);
203203
if (!inode ||
204204
get_node_id(inode) != o->nodeid ||
205-
((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
205+
inode_wrong_type(inode, o->attr.mode)) {
206206
d_invalidate(dentry);
207207
dput(dentry);
208208
goto retry;

‎fs/nfs/inode.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ nfs_find_actor(struct inode *inode, void *opaque)
322322

323323
if (NFS_FILEID(inode) != fattr->fileid)
324324
return 0;
325-
if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode))
325+
if (inode_wrong_type(inode, fattr->mode))
326326
return 0;
327327
if (nfs_compare_fh(NFS_FH(inode), fh))
328328
return 0;
@@ -1446,7 +1446,7 @@ static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fat
14461446
return 0;
14471447
return -ESTALE;
14481448
}
1449-
if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1449+
if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode))
14501450
return -ESTALE;
14511451

14521452

@@ -1861,7 +1861,7 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
18611861
/*
18621862
* Make sure the inode's type hasn't changed.
18631863
*/
1864-
if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
1864+
if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) {
18651865
/*
18661866
* Big trouble! The inode has become a different object.
18671867
*/

‎fs/nfsd/nfsproc.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ nfsd_proc_create(struct svc_rqst *rqstp)
376376

377377
/* Make sure the type and device matches */
378378
resp->status = nfserr_exist;
379-
if (inode && type != (inode->i_mode & S_IFMT))
379+
if (inode && inode_wrong_type(inode, type))
380380
goto out_unlock;
381381
}
382382

‎fs/overlayfs/namei.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
366366
return PTR_ERR(origin);
367367

368368
if (upperdentry && !ovl_is_whiteout(upperdentry) &&
369-
((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
369+
inode_wrong_type(d_inode(upperdentry), d_inode(origin)->i_mode))
370370
goto invalid;
371371

372372
if (!*stackp)
@@ -724,7 +724,7 @@ struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
724724
index = ERR_PTR(-ESTALE);
725725
goto out;
726726
} else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
727-
((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
727+
inode_wrong_type(inode, d_inode(origin)->i_mode)) {
728728
/*
729729
* Index should always be of the same file type as origin
730730
* except for the case of a whiteout index. A whiteout

‎include/linux/fs.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,6 +2768,11 @@ static inline bool execute_ok(struct inode *inode)
27682768
return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode);
27692769
}
27702770

2771+
static inline bool inode_wrong_type(const struct inode *inode, umode_t mode)
2772+
{
2773+
return (inode->i_mode ^ mode) & S_IFMT;
2774+
}
2775+
27712776
static inline void file_start_write(struct file *file)
27722777
{
27732778
if (!S_ISREG(file_inode(file)->i_mode))

0 commit comments

Comments
 (0)