nfsd4: fail when delegreturn gets a non-delegation stateid
[safe/jmp/linux-2.6] / fs / nfsd / nfs4state.c
index 099938e..6ae28e6 100644 (file)
@@ -2000,10 +2000,7 @@ out:
 static inline __be32
 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
 {
-       /* Trying to call delegreturn with a special stateid? Yuch: */
-       if (!(flags & (RD_STATE | WR_STATE)))
-               return nfserr_bad_stateid;
-       else if (ONE_STATEID(stateid) && (flags & RD_STATE))
+       if (ONE_STATEID(stateid) && (flags & RD_STATE))
                return nfs_ok;
        else if (locks_in_grace()) {
                /* Answer in remaining cases depends on existance of
@@ -2024,8 +2021,7 @@ check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
 static inline int
 io_during_grace_disallowed(struct inode *inode, int flags)
 {
-       return locks_in_grace() && (flags & (RD_STATE | WR_STATE))
-               && mandatory_lock(inode);
+       return locks_in_grace() && mandatory_lock(inode);
 }
 
 static int check_stateid_generation(stateid_t *in, stateid_t *ref)
@@ -2048,6 +2044,11 @@ static int check_stateid_generation(stateid_t *in, stateid_t *ref)
        return nfs_ok;
 }
 
+static int is_delegation_stateid(stateid_t *stateid)
+{
+       return stateid->si_fileid == 0;
+}
+
 /*
 * Checks for stateid operations
 */
@@ -2059,9 +2060,6 @@ nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int fl
        struct inode *ino = current_fh->fh_dentry->d_inode;
        __be32 status;
 
-       dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
-               stateid->si_boot, stateid->si_stateownerid, 
-               stateid->si_fileid, stateid->si_generation); 
        if (filpp)
                *filpp = NULL;
 
@@ -2076,12 +2074,10 @@ nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int fl
                goto out;
 
        status = nfserr_bad_stateid;
-       if (!stateid->si_fileid) { /* delegation stateid */
+       if (is_delegation_stateid(stateid)) {
                dp = find_delegation_stateid(ino, stateid);
-               if (!dp) {
-                       dprintk("NFSD: delegation stateid not found\n");
+               if (!dp)
                        goto out;
-               }
                status = check_stateid_generation(stateid, &dp->dl_stateid);
                if (status)
                        goto out;
@@ -2089,16 +2085,12 @@ nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int fl
                if (status)
                        goto out;
                renew_client(dp->dl_client);
-               if (flags & DELEG_RET)
-                       unhash_delegation(dp);
                if (filpp)
                        *filpp = dp->dl_vfs_file;
        } else { /* open or lock stateid */
                stp = find_stateid(stateid, flags);
-               if (!stp) {
-                       dprintk("NFSD: open or lock stateid not found\n");
+               if (!stp)
                        goto out;
-               }
                if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
                        goto out;
                if (!stp->st_stateowner->so_confirmed)
@@ -2410,16 +2402,37 @@ __be32
 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                  struct nfsd4_delegreturn *dr)
 {
+       struct nfs4_delegation *dp;
+       stateid_t *stateid = &dr->dr_stateid;
+       struct inode *inode;
        __be32 status;
 
        if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
-               goto out;
+               return status;
+       inode = cstate->current_fh.fh_dentry->d_inode;
 
        nfs4_lock_state();
-       status = nfs4_preprocess_stateid_op(&cstate->current_fh,
-                                           &dr->dr_stateid, DELEG_RET, NULL);
-       nfs4_unlock_state();
+       status = nfserr_bad_stateid;
+       if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
+               goto out;
+       status = nfserr_stale_stateid;
+       if (STALE_STATEID(stateid))
+               goto out;
+       status = nfserr_bad_stateid;
+       if (!is_delegation_stateid(stateid))
+               goto out;
+       dp = find_delegation_stateid(inode, stateid);
+       if (!dp)
+               goto out;
+       status = check_stateid_generation(stateid, &dp->dl_stateid);
+       if (status)
+               goto out;
+       renew_client(dp->dl_client);
+
+       unhash_delegation(dp);
 out:
+       nfs4_unlock_state();
+
        return status;
 }