Skip to content

Commit 6e3e2c4

Browse files
author
Al Viro
committed
new helper: inode_wrong_type()
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>
1 parent a38fd87 commit 6e3e2c4

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
@@ -399,7 +399,7 @@ static int v9fs_test_inode(struct inode *inode, void *data)
399399

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

405405
/* compare qid details */
@@ -1390,7 +1390,7 @@ int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
13901390
* Don't update inode if the file type is different
13911391
*/
13921392
umode = p9mode2unixmode(v9ses, st, &rdev);
1393-
if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
1393+
if (inode_wrong_type(inode, umode))
13941394
goto out;
13951395

13961396
/*

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)
@@ -959,7 +959,7 @@ int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
959959
/*
960960
* Don't update inode if the file type is different
961961
*/
962-
if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
962+
if (inode_wrong_type(inode, st->st_mode))
963963
goto out;
964964

965965
/*

fs/cifs/inode.c

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

428428
/* if filetype is different, return error */
429-
if (unlikely(((*pinode)->i_mode & S_IFMT) !=
430-
(fattr.cf_mode & S_IFMT))) {
429+
if (unlikely(inode_wrong_type(*pinode, fattr.cf_mode))) {
431430
CIFS_I(*pinode)->time = 0; /* force reval */
432431
rc = -ESTALE;
433432
goto cgiiu_exit;
@@ -1249,7 +1248,7 @@ cifs_find_inode(struct inode *inode, void *opaque)
12491248
return 0;
12501249

12511250
/* don't match inode of different type */
1252-
if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
1251+
if (inode_wrong_type(inode, fattr->cf_mode))
12531252
return 0;
12541253

12551254
/* 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);
@@ -1054,7 +1054,7 @@ static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
10541054
err = fuse_simple_request(fm, &args);
10551055
if (!err) {
10561056
if (fuse_invalid_attr(&outarg.attr) ||
1057-
(inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1057+
inode_wrong_type(inode, outarg.attr.mode)) {
10581058
fuse_make_bad(inode);
10591059
err = -EIO;
10601060
} else {
@@ -1703,7 +1703,7 @@ int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
17031703
}
17041704

17051705
if (fuse_invalid_attr(&outarg.attr) ||
1706-
(inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1706+
inode_wrong_type(inode, outarg.attr.mode)) {
17071707
fuse_make_bad(inode);
17081708
err = -EIO;
17091709
goto error;

fs/fuse/inode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
350350
inode->i_generation = generation;
351351
fuse_init_inode(inode, attr);
352352
unlock_new_inode(inode);
353-
} else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
353+
} else if (inode_wrong_type(inode, attr->mode)) {
354354
/* Inode has changed type, any I/O on the old should fail */
355355
fuse_make_bad(inode);
356356
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
@@ -334,7 +334,7 @@ nfs_find_actor(struct inode *inode, void *opaque)
334334

335335
if (NFS_FILEID(inode) != fattr->fileid)
336336
return 0;
337-
if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode))
337+
if (inode_wrong_type(inode, fattr->mode))
338338
return 0;
339339
if (nfs_compare_fh(NFS_FH(inode), fh))
340340
return 0;
@@ -1460,7 +1460,7 @@ static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fat
14601460
return 0;
14611461
return -ESTALE;
14621462
}
1463-
if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1463+
if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode))
14641464
return -ESTALE;
14651465

14661466

@@ -1875,7 +1875,7 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
18751875
/*
18761876
* Make sure the inode's type hasn't changed.
18771877
*/
1878-
if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
1878+
if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) {
18791879
/*
18801880
* Big trouble! The inode has become a different object.
18811881
*/

fs/nfsd/nfsproc.c

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

382382
/* Make sure the type and device matches */
383383
resp->status = nfserr_exist;
384-
if (inode && type != (inode->i_mode & S_IFMT))
384+
if (inode && inode_wrong_type(inode, type))
385385
goto out_unlock;
386386
}
387387

fs/overlayfs/namei.c

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

373373
if (upperdentry && !ovl_is_whiteout(upperdentry) &&
374-
((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
374+
inode_wrong_type(d_inode(upperdentry), d_inode(origin)->i_mode))
375375
goto invalid;
376376

377377
if (!*stackp)
@@ -730,7 +730,7 @@ struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
730730
index = ERR_PTR(-ESTALE);
731731
goto out;
732732
} else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
733-
((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
733+
inode_wrong_type(inode, d_inode(origin)->i_mode)) {
734734
/*
735735
* Index should always be of the same file type as origin
736736
* 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
@@ -2884,6 +2884,11 @@ static inline bool execute_ok(struct inode *inode)
28842884
return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode);
28852885
}
28862886

2887+
static inline bool inode_wrong_type(const struct inode *inode, umode_t mode)
2888+
{
2889+
return (inode->i_mode ^ mode) & S_IFMT;
2890+
}
2891+
28872892
static inline void file_start_write(struct file *file)
28882893
{
28892894
if (!S_ISREG(file_inode(file)->i_mode))

0 commit comments

Comments
 (0)