Skip to content

Commit 438f81e

Browse files
neilbrownchucklever
authored andcommitted
nfsd: move error choice for incorrect object types to version-specific code.
If an NFS operation expects a particular sort of object (file, dir, link, etc) but gets a file handle for a different sort of object, it must return an error. The actual error varies among NFS versions in non-trivial ways. For v2 and v3 there are ISDIR and NOTDIR errors and, for NFSv4 only, INVAL is suitable. For v4.0 there is also NFS4ERR_SYMLINK which should be used if a SYMLINK was found when not expected. This take precedence over NOTDIR. For v4.1+ there is also NFS4ERR_WRONG_TYPE which should be used in preference to EINVAL when none of the specific error codes apply. When nfsd_mode_check() finds a symlink where it expected a directory it needs to return an error code that can be converted to NOTDIR for v2 or v3 but will be SYMLINK for v4. It must be different from the error code returns when it finds a symlink but expects a regular file - that must be converted to EINVAL or SYMLINK. So we introduce an internal error code nfserr_symlink_not_dir which each version converts as appropriate. nfsd_check_obj_isreg() is similar to nfsd_mode_check() except that it is only used by NFSv4 and only for OPEN. NFSERR_INVAL is never a suitable error if the object is the wrong time. For v4.0 we use nfserr_symlink for non-dirs even if not a symlink. For v4.1 we have nfserr_wrong_type. We handle this difference in-place in nfsd_check_obj_isreg() as there is nothing to be gained by delaying the choice to nfsd4_map_status(). As a result of these changes, nfsd_mode_check() doesn't need an rqstp arg any more. Note that NFSv4 operations are actually performed in the xdr code(!!!) so to the only place that we can map the status code successfully is in nfsd4_encode_operation(). Signed-off-by: NeilBrown <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Chuck Lever <[email protected]>
1 parent 36ffa3d commit 438f81e

File tree

6 files changed

+56
-19
lines changed

6 files changed

+56
-19
lines changed

fs/nfsd/nfs3proc.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ static __be32 nfsd3_map_status(__be32 status)
4040
case nfserr_file_open:
4141
status = nfserr_acces;
4242
break;
43+
case nfserr_symlink_not_dir:
44+
status = nfserr_notdir;
45+
break;
46+
case nfserr_symlink:
47+
case nfserr_wrong_type:
48+
status = nfserr_inval;
49+
break;
4350
}
4451
return status;
4552
}

fs/nfsd/nfs4proc.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,23 @@ do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfs
158158
return fh_verify(rqstp, current_fh, S_IFREG, accmode);
159159
}
160160

161-
static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
161+
static __be32 nfsd_check_obj_isreg(struct svc_fh *fh, u32 minor_version)
162162
{
163163
umode_t mode = d_inode(fh->fh_dentry)->i_mode;
164164

165165
if (S_ISREG(mode))
166166
return nfs_ok;
167167
if (S_ISDIR(mode))
168168
return nfserr_isdir;
169-
/*
170-
* Using err_symlink as our catch-all case may look odd; but
171-
* there's no other obvious error for this case in 4.0, and we
172-
* happen to know that it will cause the linux v4 client to do
173-
* the right thing on attempts to open something other than a
174-
* regular file.
175-
*/
176-
return nfserr_symlink;
169+
if (S_ISLNK(mode))
170+
return nfserr_symlink;
171+
172+
/* RFC 7530 - 16.16.6 */
173+
if (minor_version == 0)
174+
return nfserr_symlink;
175+
else
176+
return nfserr_wrong_type;
177+
177178
}
178179

179180
static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
@@ -466,7 +467,7 @@ do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, stru
466467
}
467468
if (status)
468469
goto out;
469-
status = nfsd_check_obj_isreg(*resfh);
470+
status = nfsd_check_obj_isreg(*resfh, cstate->minorversion);
470471
if (status)
471472
goto out;
472473

fs/nfsd/nfs4xdr.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5731,6 +5731,23 @@ __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
57315731
return nfserr_rep_too_big;
57325732
}
57335733

5734+
static __be32 nfsd4_map_status(__be32 status, u32 minor)
5735+
{
5736+
switch (status) {
5737+
case nfs_ok:
5738+
break;
5739+
case nfserr_wrong_type:
5740+
/* RFC 8881 - 15.1.2.9 */
5741+
if (minor == 0)
5742+
status = nfserr_inval;
5743+
break;
5744+
case nfserr_symlink_not_dir:
5745+
status = nfserr_symlink;
5746+
break;
5747+
}
5748+
return status;
5749+
}
5750+
57345751
void
57355752
nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
57365753
{
@@ -5798,6 +5815,8 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
57985815
so->so_replay.rp_buf, len);
57995816
}
58005817
status:
5818+
op->status = nfsd4_map_status(op->status,
5819+
resp->cstate.minorversion);
58015820
*p = op->status;
58025821
release:
58035822
if (opdesc && opdesc->op_release)

fs/nfsd/nfsd.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ enum {
354354
NFSERR_REPLAY_CACHE,
355355
#define nfserr_replay_cache cpu_to_be32(NFSERR_REPLAY_CACHE)
356356

357+
/* symlink found where dir expected - handled differently to
358+
* other symlink found errors by NFSv3.
359+
*/
360+
NFSERR_SYMLINK_NOT_DIR,
361+
#define nfserr_symlink_not_dir cpu_to_be32(NFSERR_SYMLINK_NOT_DIR)
357362
};
358363

359364
/* Check for dir entries '.' and '..' */

fs/nfsd/nfsfh.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ static int nfsd_acceptable(void *expv, struct dentry *dentry)
6262
* the write call).
6363
*/
6464
static inline __be32
65-
nfsd_mode_check(struct svc_rqst *rqstp, struct dentry *dentry,
66-
umode_t requested)
65+
nfsd_mode_check(struct dentry *dentry, umode_t requested)
6766
{
6867
umode_t mode = d_inode(dentry)->i_mode & S_IFMT;
6968

@@ -76,17 +75,16 @@ nfsd_mode_check(struct svc_rqst *rqstp, struct dentry *dentry,
7675
}
7776
return nfs_ok;
7877
}
79-
/*
80-
* v4 has an error more specific than err_notdir which we should
81-
* return in preference to err_notdir:
82-
*/
83-
if (rqstp->rq_vers == 4 && mode == S_IFLNK)
78+
if (mode == S_IFLNK) {
79+
if (requested == S_IFDIR)
80+
return nfserr_symlink_not_dir;
8481
return nfserr_symlink;
82+
}
8583
if (requested == S_IFDIR)
8684
return nfserr_notdir;
8785
if (mode == S_IFDIR)
8886
return nfserr_isdir;
89-
return nfserr_inval;
87+
return nfserr_wrong_type;
9088
}
9189

9290
static bool nfsd_originating_port_ok(struct svc_rqst *rqstp, int flags)
@@ -364,7 +362,7 @@ fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access)
364362
if (error)
365363
goto out;
366364

367-
error = nfsd_mode_check(rqstp, dentry, type);
365+
error = nfsd_mode_check(dentry, type);
368366
if (error)
369367
goto out;
370368

fs/nfsd/nfsproc.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ static __be32 nfsd_map_status(__be32 status)
2727
case nfserr_file_open:
2828
status = nfserr_acces;
2929
break;
30+
case nfserr_symlink_not_dir:
31+
status = nfserr_notdir;
32+
break;
33+
case nfserr_symlink:
34+
case nfserr_wrong_type:
35+
status = nfserr_inval;
36+
break;
3037
}
3138
return status;
3239
}

0 commit comments

Comments
 (0)