]> git.dujemihanovic.xyz Git - linux.git/commitdiff
nfsd: move error choice for incorrect object types to version-specific code.
authorNeilBrown <neilb@suse.de>
Wed, 14 Aug 2024 13:21:01 +0000 (09:21 -0400)
committerChuck Lever <chuck.lever@oracle.com>
Fri, 20 Sep 2024 23:31:03 +0000 (19:31 -0400)
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 <neilb@suse.de>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
fs/nfsd/nfs3proc.c
fs/nfsd/nfs4proc.c
fs/nfsd/nfs4xdr.c
fs/nfsd/nfsd.h
fs/nfsd/nfsfh.c
fs/nfsd/nfsproc.c

index 7581d64edfa3968e6ac3e78ee7bc17c1c1b3736b..372bdcf5e07a5c835da240ecebb02e3576eb2ca6 100644 (file)
@@ -40,6 +40,13 @@ static __be32 nfsd3_map_status(__be32 status)
        case nfserr_file_open:
                status = nfserr_acces;
                break;
+       case nfserr_symlink_not_dir:
+               status = nfserr_notdir;
+               break;
+       case nfserr_symlink:
+       case nfserr_wrong_type:
+               status = nfserr_inval;
+               break;
        }
        return status;
 }
index 4f5b8c4be417110413dc619b65b77d961e6363ec..fc68af7570801722c2a31b6c29767ac3bad03b42 100644 (file)
@@ -158,7 +158,7 @@ do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfs
        return fh_verify(rqstp, current_fh, S_IFREG, accmode);
 }
 
-static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
+static __be32 nfsd_check_obj_isreg(struct svc_fh *fh, u32 minor_version)
 {
        umode_t mode = d_inode(fh->fh_dentry)->i_mode;
 
@@ -166,14 +166,15 @@ static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
                return nfs_ok;
        if (S_ISDIR(mode))
                return nfserr_isdir;
-       /*
-        * Using err_symlink as our catch-all case may look odd; but
-        * there's no other obvious error for this case in 4.0, and we
-        * happen to know that it will cause the linux v4 client to do
-        * the right thing on attempts to open something other than a
-        * regular file.
-        */
-       return nfserr_symlink;
+       if (S_ISLNK(mode))
+               return nfserr_symlink;
+
+       /* RFC 7530 - 16.16.6 */
+       if (minor_version == 0)
+               return nfserr_symlink;
+       else
+               return nfserr_wrong_type;
+
 }
 
 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
        }
        if (status)
                goto out;
-       status = nfsd_check_obj_isreg(*resfh);
+       status = nfsd_check_obj_isreg(*resfh, cstate->minorversion);
        if (status)
                goto out;
 
index 97f583777972618f831e8bdec953c9218a00c00c..4643fcfb7187abba55b4414213c7febac1e0313e 100644 (file)
@@ -5731,6 +5731,23 @@ __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
        return nfserr_rep_too_big;
 }
 
+static __be32 nfsd4_map_status(__be32 status, u32 minor)
+{
+       switch (status) {
+       case nfs_ok:
+               break;
+       case nfserr_wrong_type:
+               /* RFC 8881 - 15.1.2.9 */
+               if (minor == 0)
+                       status = nfserr_inval;
+               break;
+       case nfserr_symlink_not_dir:
+               status = nfserr_symlink;
+               break;
+       }
+       return status;
+}
+
 void
 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
 {
@@ -5798,6 +5815,8 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
                                                so->so_replay.rp_buf, len);
        }
 status:
+       op->status = nfsd4_map_status(op->status,
+                                     resp->cstate.minorversion);
        *p = op->status;
 release:
        if (opdesc && opdesc->op_release)
index 11825cbe4360906d42949e2ff4f3834d70baa205..4ccbf014a2c7b3bd73032648cbf4f544832db14a 100644 (file)
@@ -354,6 +354,11 @@ enum {
        NFSERR_REPLAY_CACHE,
 #define        nfserr_replay_cache     cpu_to_be32(NFSERR_REPLAY_CACHE)
 
+/* symlink found where dir expected - handled differently to
+ * other symlink found errors by NFSv3.
+ */
+       NFSERR_SYMLINK_NOT_DIR,
+#define        nfserr_symlink_not_dir  cpu_to_be32(NFSERR_SYMLINK_NOT_DIR)
 };
 
 /* Check for dir entries '.' and '..' */
index 388c2952a0a815ec8bed3ea7d96fd353f8803c7e..50d23d56f40315b98acf3eabd24a2aa9c7abe6f7 100644 (file)
@@ -62,8 +62,7 @@ static int nfsd_acceptable(void *expv, struct dentry *dentry)
  * the write call).
  */
 static inline __be32
-nfsd_mode_check(struct svc_rqst *rqstp, struct dentry *dentry,
-               umode_t requested)
+nfsd_mode_check(struct dentry *dentry, umode_t requested)
 {
        umode_t mode = d_inode(dentry)->i_mode & S_IFMT;
 
@@ -76,17 +75,16 @@ nfsd_mode_check(struct svc_rqst *rqstp, struct dentry *dentry,
                }
                return nfs_ok;
        }
-       /*
-        * v4 has an error more specific than err_notdir which we should
-        * return in preference to err_notdir:
-        */
-       if (rqstp->rq_vers == 4 && mode == S_IFLNK)
+       if (mode == S_IFLNK) {
+               if (requested == S_IFDIR)
+                       return nfserr_symlink_not_dir;
                return nfserr_symlink;
+       }
        if (requested == S_IFDIR)
                return nfserr_notdir;
        if (mode == S_IFDIR)
                return nfserr_isdir;
-       return nfserr_inval;
+       return nfserr_wrong_type;
 }
 
 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)
        if (error)
                goto out;
 
-       error = nfsd_mode_check(rqstp, dentry, type);
+       error = nfsd_mode_check(dentry, type);
        if (error)
                goto out;
 
index 5392f4775ee87b635644abe994a888b9b6611c50..6dda081eb24c00b834ab0965c3a35a12115bceb7 100644 (file)
@@ -27,6 +27,13 @@ static __be32 nfsd_map_status(__be32 status)
        case nfserr_file_open:
                status = nfserr_acces;
                break;
+       case nfserr_symlink_not_dir:
+               status = nfserr_notdir;
+               break;
+       case nfserr_symlink:
+       case nfserr_wrong_type:
+               status = nfserr_inval;
+               break;
        }
        return status;
 }