11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * fs/nfsd/nfs4proc.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Server-side procedures for NFSv4. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (c) 2002 The Regents of the University of Michigan. 71da177e4SLinus Torvalds * All rights reserved. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Kendrick Smith <kmsmith@umich.edu> 101da177e4SLinus Torvalds * Andy Adamson <andros@umich.edu> 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Redistribution and use in source and binary forms, with or without 131da177e4SLinus Torvalds * modification, are permitted provided that the following conditions 141da177e4SLinus Torvalds * are met: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * 1. Redistributions of source code must retain the above copyright 171da177e4SLinus Torvalds * notice, this list of conditions and the following disclaimer. 181da177e4SLinus Torvalds * 2. Redistributions in binary form must reproduce the above copyright 191da177e4SLinus Torvalds * notice, this list of conditions and the following disclaimer in the 201da177e4SLinus Torvalds * documentation and/or other materials provided with the distribution. 211da177e4SLinus Torvalds * 3. Neither the name of the University nor the names of its 221da177e4SLinus Torvalds * contributors may be used to endorse or promote products derived 231da177e4SLinus Torvalds * from this software without specific prior written permission. 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 261da177e4SLinus Torvalds * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 271da177e4SLinus Torvalds * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 281da177e4SLinus Torvalds * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 291da177e4SLinus Torvalds * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 301da177e4SLinus Torvalds * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 311da177e4SLinus Torvalds * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 321da177e4SLinus Torvalds * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 331da177e4SLinus Torvalds * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 341da177e4SLinus Torvalds * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 351da177e4SLinus Torvalds * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 361da177e4SLinus Torvalds * 371da177e4SLinus Torvalds * Note: some routines in this file are just trivial wrappers 381da177e4SLinus Torvalds * (e.g. nfsd4_lookup()) defined solely for the sake of consistent 391da177e4SLinus Torvalds * naming. Since all such routines have been declared "inline", 401da177e4SLinus Torvalds * there shouldn't be any associated overhead. At some point in 411da177e4SLinus Torvalds * the future, I might inline these "by hand" to clean up a 421da177e4SLinus Torvalds * little. 431da177e4SLinus Torvalds */ 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #include <linux/param.h> 461da177e4SLinus Torvalds #include <linux/major.h> 471da177e4SLinus Torvalds #include <linux/slab.h> 487e06b7f9SNeilBrown #include <linux/file.h> 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <linux/sunrpc/svc.h> 511da177e4SLinus Torvalds #include <linux/nfsd/nfsd.h> 521da177e4SLinus Torvalds #include <linux/nfsd/cache.h> 531da177e4SLinus Torvalds #include <linux/nfs4.h> 541da177e4SLinus Torvalds #include <linux/nfsd/state.h> 551da177e4SLinus Torvalds #include <linux/nfsd/xdr4.h> 561da177e4SLinus Torvalds #include <linux/nfs4_acl.h> 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #define NFSDDBG_FACILITY NFSDDBG_PROC 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds static inline void 611da177e4SLinus Torvalds fh_dup2(struct svc_fh *dst, struct svc_fh *src) 621da177e4SLinus Torvalds { 631da177e4SLinus Torvalds fh_put(dst); 641da177e4SLinus Torvalds dget(src->fh_dentry); 651da177e4SLinus Torvalds if (src->fh_export) 661da177e4SLinus Torvalds cache_get(&src->fh_export->h); 671da177e4SLinus Torvalds *dst = *src; 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds static int 711da177e4SLinus Torvalds do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds int accmode, status; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds if (open->op_truncate && 761da177e4SLinus Torvalds !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) 771da177e4SLinus Torvalds return nfserr_inval; 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds accmode = MAY_NOP; 801da177e4SLinus Torvalds if (open->op_share_access & NFS4_SHARE_ACCESS_READ) 811da177e4SLinus Torvalds accmode = MAY_READ; 821da177e4SLinus Torvalds if (open->op_share_deny & NFS4_SHARE_ACCESS_WRITE) 831da177e4SLinus Torvalds accmode |= (MAY_WRITE | MAY_TRUNC); 841da177e4SLinus Torvalds accmode |= MAY_OWNER_OVERRIDE; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds status = fh_verify(rqstp, current_fh, S_IFREG, accmode); 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds return status; 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds static int 921da177e4SLinus Torvalds do_open_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds struct svc_fh resfh; 951da177e4SLinus Torvalds int status; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds fh_init(&resfh, NFS4_FHSIZE); 981da177e4SLinus Torvalds open->op_truncate = 0; 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds if (open->op_create) { 1011da177e4SLinus Torvalds /* 1021da177e4SLinus Torvalds * Note: create modes (UNCHECKED,GUARDED...) are the same 1031da177e4SLinus Torvalds * in NFSv4 as in v3. 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds status = nfsd_create_v3(rqstp, current_fh, open->op_fname.data, 1061da177e4SLinus Torvalds open->op_fname.len, &open->op_iattr, 1071da177e4SLinus Torvalds &resfh, open->op_createmode, 1081da177e4SLinus Torvalds (u32 *)open->op_verf.data, &open->op_truncate); 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds else { 1111da177e4SLinus Torvalds status = nfsd_lookup(rqstp, current_fh, 1121da177e4SLinus Torvalds open->op_fname.data, open->op_fname.len, &resfh); 1131da177e4SLinus Torvalds fh_unlock(current_fh); 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds if (!status) { 1171da177e4SLinus Torvalds set_change_info(&open->op_cinfo, current_fh); 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds /* set reply cache */ 1201da177e4SLinus Torvalds fh_dup2(current_fh, &resfh); 1211da177e4SLinus Torvalds open->op_stateowner->so_replay.rp_openfh_len = 1221da177e4SLinus Torvalds resfh.fh_handle.fh_size; 1231da177e4SLinus Torvalds memcpy(open->op_stateowner->so_replay.rp_openfh, 1241da177e4SLinus Torvalds &resfh.fh_handle.fh_base, 1251da177e4SLinus Torvalds resfh.fh_handle.fh_size); 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds status = do_open_permission(rqstp, current_fh, open); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds fh_put(&resfh); 1311da177e4SLinus Torvalds return status; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds static int 1351da177e4SLinus Torvalds do_open_fhandle(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds int status; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* Only reclaims from previously confirmed clients are valid */ 1401da177e4SLinus Torvalds if ((status = nfs4_check_open_reclaim(&open->op_clientid))) 1411da177e4SLinus Torvalds return status; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds /* We don't know the target directory, and therefore can not 1441da177e4SLinus Torvalds * set the change info 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info)); 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* set replay cache */ 1501da177e4SLinus Torvalds open->op_stateowner->so_replay.rp_openfh_len = current_fh->fh_handle.fh_size; 1511da177e4SLinus Torvalds memcpy(open->op_stateowner->so_replay.rp_openfh, 1521da177e4SLinus Torvalds ¤t_fh->fh_handle.fh_base, 1531da177e4SLinus Torvalds current_fh->fh_handle.fh_size); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) && 1561da177e4SLinus Torvalds (open->op_iattr.ia_size == 0); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds status = do_open_permission(rqstp, current_fh, open); 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds return status; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds static inline int 165f2327d9aSNeil Brown nfsd4_open(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, struct nfs4_stateowner **replay_owner) 1661da177e4SLinus Torvalds { 1671da177e4SLinus Torvalds int status; 1681da177e4SLinus Torvalds dprintk("NFSD: nfsd4_open filename %.*s op_stateowner %p\n", 1691da177e4SLinus Torvalds (int)open->op_fname.len, open->op_fname.data, 1701da177e4SLinus Torvalds open->op_stateowner); 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* This check required by spec. */ 1731da177e4SLinus Torvalds if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL) 1741da177e4SLinus Torvalds return nfserr_inval; 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds nfs4_lock_state(); 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds /* check seqid for replay. set nfs4_owner */ 1791da177e4SLinus Torvalds status = nfsd4_process_open1(open); 1801da177e4SLinus Torvalds if (status == NFSERR_REPLAY_ME) { 1811da177e4SLinus Torvalds struct nfs4_replay *rp = &open->op_stateowner->so_replay; 1821da177e4SLinus Torvalds fh_put(current_fh); 1831da177e4SLinus Torvalds current_fh->fh_handle.fh_size = rp->rp_openfh_len; 1841da177e4SLinus Torvalds memcpy(¤t_fh->fh_handle.fh_base, rp->rp_openfh, 1851da177e4SLinus Torvalds rp->rp_openfh_len); 1861da177e4SLinus Torvalds status = fh_verify(rqstp, current_fh, 0, MAY_NOP); 1871da177e4SLinus Torvalds if (status) 1881da177e4SLinus Torvalds dprintk("nfsd4_open: replay failed" 1891da177e4SLinus Torvalds " restoring previous filehandle\n"); 1901da177e4SLinus Torvalds else 1911da177e4SLinus Torvalds status = NFSERR_REPLAY_ME; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds if (status) 1941da177e4SLinus Torvalds goto out; 195fb553c0fSJ. Bruce Fields 196fb553c0fSJ. Bruce Fields /* Openowner is now set, so sequence id will get bumped. Now we need 197fb553c0fSJ. Bruce Fields * these checks before we do any creates: */ 198cbd0d51aSJ. Bruce Fields status = nfserr_grace; 199fb553c0fSJ. Bruce Fields if (nfs4_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS) 200cbd0d51aSJ. Bruce Fields goto out; 201cbd0d51aSJ. Bruce Fields status = nfserr_no_grace; 202fb553c0fSJ. Bruce Fields if (!nfs4_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) 203cbd0d51aSJ. Bruce Fields goto out; 204fb553c0fSJ. Bruce Fields 2051da177e4SLinus Torvalds switch (open->op_claim_type) { 2060dd3c192SNeilBrown case NFS4_OPEN_CLAIM_DELEGATE_CUR: 2070dd3c192SNeilBrown status = nfserr_inval; 2080dd3c192SNeilBrown if (open->op_create) 2090dd3c192SNeilBrown goto out; 2100dd3c192SNeilBrown /* fall through */ 2111da177e4SLinus Torvalds case NFS4_OPEN_CLAIM_NULL: 2121da177e4SLinus Torvalds /* 2131da177e4SLinus Torvalds * (1) set CURRENT_FH to the file being opened, 2141da177e4SLinus Torvalds * creating it if necessary, (2) set open->op_cinfo, 2151da177e4SLinus Torvalds * (3) set open->op_truncate if the file is to be 2161da177e4SLinus Torvalds * truncated after opening, (4) do permission checking. 2171da177e4SLinus Torvalds */ 2181da177e4SLinus Torvalds status = do_open_lookup(rqstp, current_fh, open); 2191da177e4SLinus Torvalds if (status) 2201da177e4SLinus Torvalds goto out; 2211da177e4SLinus Torvalds break; 2221da177e4SLinus Torvalds case NFS4_OPEN_CLAIM_PREVIOUS: 223a525825dSJ. Bruce Fields open->op_stateowner->so_confirmed = 1; 2241da177e4SLinus Torvalds /* 2251da177e4SLinus Torvalds * The CURRENT_FH is already set to the file being 2261da177e4SLinus Torvalds * opened. (1) set open->op_cinfo, (2) set 2271da177e4SLinus Torvalds * open->op_truncate if the file is to be truncated 2281da177e4SLinus Torvalds * after opening, (3) do permission checking. 2291da177e4SLinus Torvalds */ 2301da177e4SLinus Torvalds status = do_open_fhandle(rqstp, current_fh, open); 2311da177e4SLinus Torvalds if (status) 2321da177e4SLinus Torvalds goto out; 2331da177e4SLinus Torvalds break; 2341da177e4SLinus Torvalds case NFS4_OPEN_CLAIM_DELEGATE_PREV: 235a525825dSJ. Bruce Fields open->op_stateowner->so_confirmed = 1; 2361da177e4SLinus Torvalds printk("NFSD: unsupported OPEN claim type %d\n", 2371da177e4SLinus Torvalds open->op_claim_type); 2381da177e4SLinus Torvalds status = nfserr_notsupp; 2391da177e4SLinus Torvalds goto out; 2401da177e4SLinus Torvalds default: 2411da177e4SLinus Torvalds printk("NFSD: Invalid OPEN claim type %d\n", 2421da177e4SLinus Torvalds open->op_claim_type); 2431da177e4SLinus Torvalds status = nfserr_inval; 2441da177e4SLinus Torvalds goto out; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds /* 2471da177e4SLinus Torvalds * nfsd4_process_open2() does the actual opening of the file. If 2481da177e4SLinus Torvalds * successful, it (1) truncates the file if open->op_truncate was 2491da177e4SLinus Torvalds * set, (2) sets open->op_stateid, (3) sets open->op_delegation. 2501da177e4SLinus Torvalds */ 2511da177e4SLinus Torvalds status = nfsd4_process_open2(rqstp, current_fh, open); 2521da177e4SLinus Torvalds out: 253f2327d9aSNeil Brown if (open->op_stateowner) { 2541da177e4SLinus Torvalds nfs4_get_stateowner(open->op_stateowner); 255f2327d9aSNeil Brown *replay_owner = open->op_stateowner; 256f2327d9aSNeil Brown } 2571da177e4SLinus Torvalds nfs4_unlock_state(); 2581da177e4SLinus Torvalds return status; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds /* 2621da177e4SLinus Torvalds * filehandle-manipulating ops. 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds static inline int 2651da177e4SLinus Torvalds nfsd4_getfh(struct svc_fh *current_fh, struct svc_fh **getfh) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds if (!current_fh->fh_dentry) 2681da177e4SLinus Torvalds return nfserr_nofilehandle; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds *getfh = current_fh; 2711da177e4SLinus Torvalds return nfs_ok; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds static inline int 2751da177e4SLinus Torvalds nfsd4_putfh(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_putfh *putfh) 2761da177e4SLinus Torvalds { 2771da177e4SLinus Torvalds fh_put(current_fh); 2781da177e4SLinus Torvalds current_fh->fh_handle.fh_size = putfh->pf_fhlen; 2791da177e4SLinus Torvalds memcpy(¤t_fh->fh_handle.fh_base, putfh->pf_fhval, putfh->pf_fhlen); 2801da177e4SLinus Torvalds return fh_verify(rqstp, current_fh, 0, MAY_NOP); 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds static inline int 2841da177e4SLinus Torvalds nfsd4_putrootfh(struct svc_rqst *rqstp, struct svc_fh *current_fh) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds int status; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds fh_put(current_fh); 2891da177e4SLinus Torvalds status = exp_pseudoroot(rqstp->rq_client, current_fh, 2901da177e4SLinus Torvalds &rqstp->rq_chandle); 2911da177e4SLinus Torvalds return status; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds static inline int 2951da177e4SLinus Torvalds nfsd4_restorefh(struct svc_fh *current_fh, struct svc_fh *save_fh) 2961da177e4SLinus Torvalds { 2971da177e4SLinus Torvalds if (!save_fh->fh_dentry) 2981da177e4SLinus Torvalds return nfserr_restorefh; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds fh_dup2(current_fh, save_fh); 3011da177e4SLinus Torvalds return nfs_ok; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds static inline int 3051da177e4SLinus Torvalds nfsd4_savefh(struct svc_fh *current_fh, struct svc_fh *save_fh) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds if (!current_fh->fh_dentry) 3081da177e4SLinus Torvalds return nfserr_nofilehandle; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds fh_dup2(save_fh, current_fh); 3111da177e4SLinus Torvalds return nfs_ok; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds /* 3151da177e4SLinus Torvalds * misc nfsv4 ops 3161da177e4SLinus Torvalds */ 3171da177e4SLinus Torvalds static inline int 3181da177e4SLinus Torvalds nfsd4_access(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_access *access) 3191da177e4SLinus Torvalds { 3201da177e4SLinus Torvalds if (access->ac_req_access & ~NFS3_ACCESS_FULL) 3211da177e4SLinus Torvalds return nfserr_inval; 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds access->ac_resp_access = access->ac_req_access; 3241da177e4SLinus Torvalds return nfsd_access(rqstp, current_fh, &access->ac_resp_access, &access->ac_supported); 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds static inline int 3281da177e4SLinus Torvalds nfsd4_commit(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_commit *commit) 3291da177e4SLinus Torvalds { 3301da177e4SLinus Torvalds int status; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds u32 *p = (u32 *)commit->co_verf.data; 3331da177e4SLinus Torvalds *p++ = nfssvc_boot.tv_sec; 3341da177e4SLinus Torvalds *p++ = nfssvc_boot.tv_usec; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds status = nfsd_commit(rqstp, current_fh, commit->co_offset, commit->co_count); 3371da177e4SLinus Torvalds if (status == nfserr_symlink) 3381da177e4SLinus Torvalds status = nfserr_inval; 3391da177e4SLinus Torvalds return status; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds static int 3431da177e4SLinus Torvalds nfsd4_create(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_create *create) 3441da177e4SLinus Torvalds { 3451da177e4SLinus Torvalds struct svc_fh resfh; 3461da177e4SLinus Torvalds int status; 3471da177e4SLinus Torvalds dev_t rdev; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds fh_init(&resfh, NFS4_FHSIZE); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds status = fh_verify(rqstp, current_fh, S_IFDIR, MAY_CREATE); 3521da177e4SLinus Torvalds if (status == nfserr_symlink) 3531da177e4SLinus Torvalds status = nfserr_notdir; 3541da177e4SLinus Torvalds if (status) 3551da177e4SLinus Torvalds return status; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds switch (create->cr_type) { 3581da177e4SLinus Torvalds case NF4LNK: 3591da177e4SLinus Torvalds /* ugh! we have to null-terminate the linktext, or 3601da177e4SLinus Torvalds * vfs_symlink() will choke. it is always safe to 3611da177e4SLinus Torvalds * null-terminate by brute force, since at worst we 3621da177e4SLinus Torvalds * will overwrite the first byte of the create namelen 3631da177e4SLinus Torvalds * in the XDR buffer, which has already been extracted 3641da177e4SLinus Torvalds * during XDR decode. 3651da177e4SLinus Torvalds */ 3661da177e4SLinus Torvalds create->cr_linkname[create->cr_linklen] = 0; 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds status = nfsd_symlink(rqstp, current_fh, create->cr_name, 3691da177e4SLinus Torvalds create->cr_namelen, create->cr_linkname, 3701da177e4SLinus Torvalds create->cr_linklen, &resfh, &create->cr_iattr); 3711da177e4SLinus Torvalds break; 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds case NF4BLK: 3741da177e4SLinus Torvalds rdev = MKDEV(create->cr_specdata1, create->cr_specdata2); 3751da177e4SLinus Torvalds if (MAJOR(rdev) != create->cr_specdata1 || 3761da177e4SLinus Torvalds MINOR(rdev) != create->cr_specdata2) 3771da177e4SLinus Torvalds return nfserr_inval; 3781da177e4SLinus Torvalds status = nfsd_create(rqstp, current_fh, create->cr_name, 3791da177e4SLinus Torvalds create->cr_namelen, &create->cr_iattr, 3801da177e4SLinus Torvalds S_IFBLK, rdev, &resfh); 3811da177e4SLinus Torvalds break; 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds case NF4CHR: 3841da177e4SLinus Torvalds rdev = MKDEV(create->cr_specdata1, create->cr_specdata2); 3851da177e4SLinus Torvalds if (MAJOR(rdev) != create->cr_specdata1 || 3861da177e4SLinus Torvalds MINOR(rdev) != create->cr_specdata2) 3871da177e4SLinus Torvalds return nfserr_inval; 3881da177e4SLinus Torvalds status = nfsd_create(rqstp, current_fh, create->cr_name, 3891da177e4SLinus Torvalds create->cr_namelen, &create->cr_iattr, 3901da177e4SLinus Torvalds S_IFCHR, rdev, &resfh); 3911da177e4SLinus Torvalds break; 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds case NF4SOCK: 3941da177e4SLinus Torvalds status = nfsd_create(rqstp, current_fh, create->cr_name, 3951da177e4SLinus Torvalds create->cr_namelen, &create->cr_iattr, 3961da177e4SLinus Torvalds S_IFSOCK, 0, &resfh); 3971da177e4SLinus Torvalds break; 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds case NF4FIFO: 4001da177e4SLinus Torvalds status = nfsd_create(rqstp, current_fh, create->cr_name, 4011da177e4SLinus Torvalds create->cr_namelen, &create->cr_iattr, 4021da177e4SLinus Torvalds S_IFIFO, 0, &resfh); 4031da177e4SLinus Torvalds break; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds case NF4DIR: 4061da177e4SLinus Torvalds create->cr_iattr.ia_valid &= ~ATTR_SIZE; 4071da177e4SLinus Torvalds status = nfsd_create(rqstp, current_fh, create->cr_name, 4081da177e4SLinus Torvalds create->cr_namelen, &create->cr_iattr, 4091da177e4SLinus Torvalds S_IFDIR, 0, &resfh); 4101da177e4SLinus Torvalds break; 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds default: 4131da177e4SLinus Torvalds status = nfserr_badtype; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds if (!status) { 4171da177e4SLinus Torvalds fh_unlock(current_fh); 4181da177e4SLinus Torvalds set_change_info(&create->cr_cinfo, current_fh); 4191da177e4SLinus Torvalds fh_dup2(current_fh, &resfh); 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds fh_put(&resfh); 4231da177e4SLinus Torvalds return status; 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds static inline int 4271da177e4SLinus Torvalds nfsd4_getattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_getattr *getattr) 4281da177e4SLinus Torvalds { 4291da177e4SLinus Torvalds int status; 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds status = fh_verify(rqstp, current_fh, 0, MAY_NOP); 4321da177e4SLinus Torvalds if (status) 4331da177e4SLinus Torvalds return status; 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1) 4361da177e4SLinus Torvalds return nfserr_inval; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds getattr->ga_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0; 4391da177e4SLinus Torvalds getattr->ga_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds getattr->ga_fhp = current_fh; 4421da177e4SLinus Torvalds return nfs_ok; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds static inline int 4461da177e4SLinus Torvalds nfsd4_link(struct svc_rqst *rqstp, struct svc_fh *current_fh, 4471da177e4SLinus Torvalds struct svc_fh *save_fh, struct nfsd4_link *link) 4481da177e4SLinus Torvalds { 4491da177e4SLinus Torvalds int status = nfserr_nofilehandle; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds if (!save_fh->fh_dentry) 4521da177e4SLinus Torvalds return status; 4531da177e4SLinus Torvalds status = nfsd_link(rqstp, current_fh, link->li_name, link->li_namelen, save_fh); 4541da177e4SLinus Torvalds if (!status) 4551da177e4SLinus Torvalds set_change_info(&link->li_cinfo, current_fh); 4561da177e4SLinus Torvalds return status; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds static int 4601da177e4SLinus Torvalds nfsd4_lookupp(struct svc_rqst *rqstp, struct svc_fh *current_fh) 4611da177e4SLinus Torvalds { 4621da177e4SLinus Torvalds struct svc_fh tmp_fh; 4631da177e4SLinus Torvalds int ret; 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds fh_init(&tmp_fh, NFS4_FHSIZE); 4661da177e4SLinus Torvalds if((ret = exp_pseudoroot(rqstp->rq_client, &tmp_fh, 4671da177e4SLinus Torvalds &rqstp->rq_chandle)) != 0) 4681da177e4SLinus Torvalds return ret; 4691da177e4SLinus Torvalds if (tmp_fh.fh_dentry == current_fh->fh_dentry) { 4701da177e4SLinus Torvalds fh_put(&tmp_fh); 4711da177e4SLinus Torvalds return nfserr_noent; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds fh_put(&tmp_fh); 4741da177e4SLinus Torvalds return nfsd_lookup(rqstp, current_fh, "..", 2, current_fh); 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds static inline int 4781da177e4SLinus Torvalds nfsd4_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lookup *lookup) 4791da177e4SLinus Torvalds { 4801da177e4SLinus Torvalds return nfsd_lookup(rqstp, current_fh, lookup->lo_name, lookup->lo_len, current_fh); 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds static inline int 4841da177e4SLinus Torvalds nfsd4_read(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_read *read) 4851da177e4SLinus Torvalds { 4861da177e4SLinus Torvalds int status; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* no need to check permission - this will be done in nfsd_read() */ 4891da177e4SLinus Torvalds 4907e06b7f9SNeilBrown read->rd_filp = NULL; 4911da177e4SLinus Torvalds if (read->rd_offset >= OFFSET_MAX) 4921da177e4SLinus Torvalds return nfserr_inval; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds nfs4_lock_state(); 4951da177e4SLinus Torvalds /* check stateid */ 4961da177e4SLinus Torvalds if ((status = nfs4_preprocess_stateid_op(current_fh, &read->rd_stateid, 4977e06b7f9SNeilBrown CHECK_FH | RD_STATE, &read->rd_filp))) { 4981da177e4SLinus Torvalds dprintk("NFSD: nfsd4_read: couldn't process stateid!\n"); 4991da177e4SLinus Torvalds goto out; 5001da177e4SLinus Torvalds } 5017e06b7f9SNeilBrown if (read->rd_filp) 5027e06b7f9SNeilBrown get_file(read->rd_filp); 5031da177e4SLinus Torvalds status = nfs_ok; 5041da177e4SLinus Torvalds out: 5051da177e4SLinus Torvalds nfs4_unlock_state(); 5061da177e4SLinus Torvalds read->rd_rqstp = rqstp; 5071da177e4SLinus Torvalds read->rd_fhp = current_fh; 5081da177e4SLinus Torvalds return status; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds static inline int 5121da177e4SLinus Torvalds nfsd4_readdir(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readdir *readdir) 5131da177e4SLinus Torvalds { 5141da177e4SLinus Torvalds u64 cookie = readdir->rd_cookie; 5151da177e4SLinus Torvalds static const nfs4_verifier zeroverf; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* no need to check permission - this will be done in nfsd_readdir() */ 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1) 5201da177e4SLinus Torvalds return nfserr_inval; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds readdir->rd_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0; 5231da177e4SLinus Torvalds readdir->rd_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1; 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds if ((cookie > ~(u32)0) || (cookie == 1) || (cookie == 2) || 5261da177e4SLinus Torvalds (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE))) 5271da177e4SLinus Torvalds return nfserr_bad_cookie; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds readdir->rd_rqstp = rqstp; 5301da177e4SLinus Torvalds readdir->rd_fhp = current_fh; 5311da177e4SLinus Torvalds return nfs_ok; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds static inline int 5351da177e4SLinus Torvalds nfsd4_readlink(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readlink *readlink) 5361da177e4SLinus Torvalds { 5371da177e4SLinus Torvalds readlink->rl_rqstp = rqstp; 5381da177e4SLinus Torvalds readlink->rl_fhp = current_fh; 5391da177e4SLinus Torvalds return nfs_ok; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds static inline int 5431da177e4SLinus Torvalds nfsd4_remove(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_remove *remove) 5441da177e4SLinus Torvalds { 5451da177e4SLinus Torvalds int status; 5461da177e4SLinus Torvalds 547c815afc7SNeilBrown if (nfs4_in_grace()) 548c815afc7SNeilBrown return nfserr_grace; 5491da177e4SLinus Torvalds status = nfsd_unlink(rqstp, current_fh, 0, remove->rm_name, remove->rm_namelen); 5501da177e4SLinus Torvalds if (status == nfserr_symlink) 5511da177e4SLinus Torvalds return nfserr_notdir; 5521da177e4SLinus Torvalds if (!status) { 5531da177e4SLinus Torvalds fh_unlock(current_fh); 5541da177e4SLinus Torvalds set_change_info(&remove->rm_cinfo, current_fh); 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds return status; 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds static inline int 5601da177e4SLinus Torvalds nfsd4_rename(struct svc_rqst *rqstp, struct svc_fh *current_fh, 5611da177e4SLinus Torvalds struct svc_fh *save_fh, struct nfsd4_rename *rename) 5621da177e4SLinus Torvalds { 5631da177e4SLinus Torvalds int status = nfserr_nofilehandle; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds if (!save_fh->fh_dentry) 5661da177e4SLinus Torvalds return status; 567c815afc7SNeilBrown if (nfs4_in_grace() && !(save_fh->fh_export->ex_flags 568c815afc7SNeilBrown & NFSEXP_NOSUBTREECHECK)) 569c815afc7SNeilBrown return nfserr_grace; 5701da177e4SLinus Torvalds status = nfsd_rename(rqstp, save_fh, rename->rn_sname, 5711da177e4SLinus Torvalds rename->rn_snamelen, current_fh, 5721da177e4SLinus Torvalds rename->rn_tname, rename->rn_tnamelen); 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds /* the underlying filesystem returns different error's than required 5751da177e4SLinus Torvalds * by NFSv4. both save_fh and current_fh have been verified.. */ 5761da177e4SLinus Torvalds if (status == nfserr_isdir) 5771da177e4SLinus Torvalds status = nfserr_exist; 5781da177e4SLinus Torvalds else if ((status == nfserr_notdir) && 5791da177e4SLinus Torvalds (S_ISDIR(save_fh->fh_dentry->d_inode->i_mode) && 5801da177e4SLinus Torvalds S_ISDIR(current_fh->fh_dentry->d_inode->i_mode))) 5811da177e4SLinus Torvalds status = nfserr_exist; 5821da177e4SLinus Torvalds else if (status == nfserr_symlink) 5831da177e4SLinus Torvalds status = nfserr_notdir; 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds if (!status) { 5861da177e4SLinus Torvalds set_change_info(&rename->rn_sinfo, current_fh); 5871da177e4SLinus Torvalds set_change_info(&rename->rn_tinfo, save_fh); 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds return status; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds static inline int 5931da177e4SLinus Torvalds nfsd4_setattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_setattr *setattr) 5941da177e4SLinus Torvalds { 5951da177e4SLinus Torvalds int status = nfs_ok; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds if (setattr->sa_iattr.ia_valid & ATTR_SIZE) { 5981da177e4SLinus Torvalds nfs4_lock_state(); 599375c5547SJ. Bruce Fields status = nfs4_preprocess_stateid_op(current_fh, 600375c5547SJ. Bruce Fields &setattr->sa_stateid, CHECK_FH | WR_STATE, NULL); 6011da177e4SLinus Torvalds nfs4_unlock_state(); 602375c5547SJ. Bruce Fields if (status) { 603375c5547SJ. Bruce Fields dprintk("NFSD: nfsd4_setattr: couldn't process stateid!"); 604375c5547SJ. Bruce Fields return status; 605375c5547SJ. Bruce Fields } 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds status = nfs_ok; 6081da177e4SLinus Torvalds if (setattr->sa_acl != NULL) 6091da177e4SLinus Torvalds status = nfsd4_set_nfs4_acl(rqstp, current_fh, setattr->sa_acl); 6101da177e4SLinus Torvalds if (status) 611375c5547SJ. Bruce Fields return status; 6121da177e4SLinus Torvalds status = nfsd_setattr(rqstp, current_fh, &setattr->sa_iattr, 6131da177e4SLinus Torvalds 0, (time_t)0); 6141da177e4SLinus Torvalds return status; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds static inline int 6181da177e4SLinus Torvalds nfsd4_write(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_write *write) 6191da177e4SLinus Torvalds { 6201da177e4SLinus Torvalds stateid_t *stateid = &write->wr_stateid; 6211da177e4SLinus Torvalds struct file *filp = NULL; 6221da177e4SLinus Torvalds u32 *p; 6231da177e4SLinus Torvalds int status = nfs_ok; 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds /* no need to check permission - this will be done in nfsd_write() */ 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds if (write->wr_offset >= OFFSET_MAX) 6281da177e4SLinus Torvalds return nfserr_inval; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds nfs4_lock_state(); 631375c5547SJ. Bruce Fields status = nfs4_preprocess_stateid_op(current_fh, stateid, 632375c5547SJ. Bruce Fields CHECK_FH | WR_STATE, &filp); 6337e06b7f9SNeilBrown if (filp) 6347e06b7f9SNeilBrown get_file(filp); 6351da177e4SLinus Torvalds nfs4_unlock_state(); 6361da177e4SLinus Torvalds 637375c5547SJ. Bruce Fields if (status) { 638375c5547SJ. Bruce Fields dprintk("NFSD: nfsd4_write: couldn't process stateid!\n"); 639375c5547SJ. Bruce Fields return status; 640375c5547SJ. Bruce Fields } 641375c5547SJ. Bruce Fields 6421da177e4SLinus Torvalds write->wr_bytes_written = write->wr_buflen; 6431da177e4SLinus Torvalds write->wr_how_written = write->wr_stable_how; 6441da177e4SLinus Torvalds p = (u32 *)write->wr_verifier.data; 6451da177e4SLinus Torvalds *p++ = nfssvc_boot.tv_sec; 6461da177e4SLinus Torvalds *p++ = nfssvc_boot.tv_usec; 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds status = nfsd_write(rqstp, current_fh, filp, write->wr_offset, 6491da177e4SLinus Torvalds write->wr_vec, write->wr_vlen, write->wr_buflen, 6501da177e4SLinus Torvalds &write->wr_how_written); 6517e06b7f9SNeilBrown if (filp) 6527e06b7f9SNeilBrown fput(filp); 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds if (status == nfserr_symlink) 6551da177e4SLinus Torvalds status = nfserr_inval; 6561da177e4SLinus Torvalds return status; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds /* This routine never returns NFS_OK! If there are no other errors, it 6601da177e4SLinus Torvalds * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the 6611da177e4SLinus Torvalds * attributes matched. VERIFY is implemented by mapping NFSERR_SAME 6621da177e4SLinus Torvalds * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK. 6631da177e4SLinus Torvalds */ 6641da177e4SLinus Torvalds static int 6651da177e4SLinus Torvalds nfsd4_verify(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_verify *verify) 6661da177e4SLinus Torvalds { 6671da177e4SLinus Torvalds u32 *buf, *p; 6681da177e4SLinus Torvalds int count; 6691da177e4SLinus Torvalds int status; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds status = fh_verify(rqstp, current_fh, 0, MAY_NOP); 6721da177e4SLinus Torvalds if (status) 6731da177e4SLinus Torvalds return status; 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds if ((verify->ve_bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0) 6761da177e4SLinus Torvalds || (verify->ve_bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1)) 6771da177e4SLinus Torvalds return nfserr_attrnotsupp; 6781da177e4SLinus Torvalds if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR) 6791da177e4SLinus Torvalds || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)) 6801da177e4SLinus Torvalds return nfserr_inval; 6811da177e4SLinus Torvalds if (verify->ve_attrlen & 3) 6821da177e4SLinus Torvalds return nfserr_inval; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds /* count in words: 6851da177e4SLinus Torvalds * bitmap_len(1) + bitmap(2) + attr_len(1) = 4 6861da177e4SLinus Torvalds */ 6871da177e4SLinus Torvalds count = 4 + (verify->ve_attrlen >> 2); 6881da177e4SLinus Torvalds buf = kmalloc(count << 2, GFP_KERNEL); 6891da177e4SLinus Torvalds if (!buf) 6901da177e4SLinus Torvalds return nfserr_resource; 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds status = nfsd4_encode_fattr(current_fh, current_fh->fh_export, 6931da177e4SLinus Torvalds current_fh->fh_dentry, buf, 6941da177e4SLinus Torvalds &count, verify->ve_bmval, 6951da177e4SLinus Torvalds rqstp); 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds /* this means that nfsd4_encode_fattr() ran out of space */ 6981da177e4SLinus Torvalds if (status == nfserr_resource && count == 0) 6991da177e4SLinus Torvalds status = nfserr_not_same; 7001da177e4SLinus Torvalds if (status) 7011da177e4SLinus Torvalds goto out_kfree; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds p = buf + 3; 7041da177e4SLinus Torvalds status = nfserr_not_same; 7051da177e4SLinus Torvalds if (ntohl(*p++) != verify->ve_attrlen) 7061da177e4SLinus Torvalds goto out_kfree; 7071da177e4SLinus Torvalds if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen)) 7081da177e4SLinus Torvalds status = nfserr_same; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds out_kfree: 7111da177e4SLinus Torvalds kfree(buf); 7121da177e4SLinus Torvalds return status; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds /* 7161da177e4SLinus Torvalds * NULL call. 7171da177e4SLinus Torvalds */ 7181da177e4SLinus Torvalds static int 7191da177e4SLinus Torvalds nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp) 7201da177e4SLinus Torvalds { 7211da177e4SLinus Torvalds return nfs_ok; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 724*e2b20950SShankar Anand static inline void nfsd4_increment_op_stats(u32 opnum) 725*e2b20950SShankar Anand { 726*e2b20950SShankar Anand if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP) 727*e2b20950SShankar Anand nfsdstats.nfs4_opcount[opnum]++; 728*e2b20950SShankar Anand } 729*e2b20950SShankar Anand 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds /* 7321da177e4SLinus Torvalds * COMPOUND call. 7331da177e4SLinus Torvalds */ 7341da177e4SLinus Torvalds static int 7351da177e4SLinus Torvalds nfsd4_proc_compound(struct svc_rqst *rqstp, 7361da177e4SLinus Torvalds struct nfsd4_compoundargs *args, 7371da177e4SLinus Torvalds struct nfsd4_compoundres *resp) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds struct nfsd4_op *op; 7401da177e4SLinus Torvalds struct svc_fh *current_fh = NULL; 7411da177e4SLinus Torvalds struct svc_fh *save_fh = NULL; 7421da177e4SLinus Torvalds struct nfs4_stateowner *replay_owner = NULL; 7431da177e4SLinus Torvalds int slack_space; /* in words, not bytes! */ 7441da177e4SLinus Torvalds int status; 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds status = nfserr_resource; 7471da177e4SLinus Torvalds current_fh = kmalloc(sizeof(*current_fh), GFP_KERNEL); 7481da177e4SLinus Torvalds if (current_fh == NULL) 7491da177e4SLinus Torvalds goto out; 7501da177e4SLinus Torvalds fh_init(current_fh, NFS4_FHSIZE); 7511da177e4SLinus Torvalds save_fh = kmalloc(sizeof(*save_fh), GFP_KERNEL); 7521da177e4SLinus Torvalds if (save_fh == NULL) 7531da177e4SLinus Torvalds goto out; 7541da177e4SLinus Torvalds fh_init(save_fh, NFS4_FHSIZE); 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds resp->xbuf = &rqstp->rq_res; 7571da177e4SLinus Torvalds resp->p = rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len; 7581da177e4SLinus Torvalds resp->tagp = resp->p; 7591da177e4SLinus Torvalds /* reserve space for: taglen, tag, and opcnt */ 7601da177e4SLinus Torvalds resp->p += 2 + XDR_QUADLEN(args->taglen); 7611da177e4SLinus Torvalds resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE; 7621da177e4SLinus Torvalds resp->taglen = args->taglen; 7631da177e4SLinus Torvalds resp->tag = args->tag; 7641da177e4SLinus Torvalds resp->opcnt = 0; 7651da177e4SLinus Torvalds resp->rqstp = rqstp; 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds /* 7681da177e4SLinus Torvalds * According to RFC3010, this takes precedence over all other errors. 7691da177e4SLinus Torvalds */ 7701da177e4SLinus Torvalds status = nfserr_minor_vers_mismatch; 7711da177e4SLinus Torvalds if (args->minorversion > NFSD_SUPPORTED_MINOR_VERSION) 7721da177e4SLinus Torvalds goto out; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds status = nfs_ok; 7751da177e4SLinus Torvalds while (!status && resp->opcnt < args->opcnt) { 7761da177e4SLinus Torvalds op = &args->ops[resp->opcnt++]; 7771da177e4SLinus Torvalds 778fd445277SJ. Bruce Fields dprintk("nfsv4 compound op #%d: %d\n", resp->opcnt, op->opnum); 779fd445277SJ. Bruce Fields 7801da177e4SLinus Torvalds /* 7811da177e4SLinus Torvalds * The XDR decode routines may have pre-set op->status; 7821da177e4SLinus Torvalds * for example, if there is a miscellaneous XDR error 7831da177e4SLinus Torvalds * it will be set to nfserr_bad_xdr. 7841da177e4SLinus Torvalds */ 7851da177e4SLinus Torvalds if (op->status) 7861da177e4SLinus Torvalds goto encode_op; 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* We must be able to encode a successful response to 7891da177e4SLinus Torvalds * this operation, with enough room left over to encode a 7901da177e4SLinus Torvalds * failed response to the next operation. If we don't 7911da177e4SLinus Torvalds * have enough room, fail with ERR_RESOURCE. 7921da177e4SLinus Torvalds */ 7931da177e4SLinus Torvalds /* FIXME - is slack_space *really* words, or bytes??? - neilb */ 7941da177e4SLinus Torvalds slack_space = (char *)resp->end - (char *)resp->p; 7951da177e4SLinus Torvalds if (slack_space < COMPOUND_SLACK_SPACE + COMPOUND_ERR_SLACK_SPACE) { 7961da177e4SLinus Torvalds BUG_ON(slack_space < COMPOUND_ERR_SLACK_SPACE); 7971da177e4SLinus Torvalds op->status = nfserr_resource; 7981da177e4SLinus Torvalds goto encode_op; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds /* All operations except RENEW, SETCLIENTID, RESTOREFH 8021da177e4SLinus Torvalds * SETCLIENTID_CONFIRM, PUTFH and PUTROOTFH 8031da177e4SLinus Torvalds * require a valid current filehandle 8041da177e4SLinus Torvalds */ 8051da177e4SLinus Torvalds if ((!current_fh->fh_dentry) && 8061da177e4SLinus Torvalds !((op->opnum == OP_PUTFH) || (op->opnum == OP_PUTROOTFH) || 8071da177e4SLinus Torvalds (op->opnum == OP_SETCLIENTID) || 8081da177e4SLinus Torvalds (op->opnum == OP_SETCLIENTID_CONFIRM) || 8091da177e4SLinus Torvalds (op->opnum == OP_RENEW) || (op->opnum == OP_RESTOREFH) || 81052748819SFred Isaman (op->opnum == OP_RELEASE_LOCKOWNER))) { 8111da177e4SLinus Torvalds op->status = nfserr_nofilehandle; 8121da177e4SLinus Torvalds goto encode_op; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds switch (op->opnum) { 8151da177e4SLinus Torvalds case OP_ACCESS: 8161da177e4SLinus Torvalds op->status = nfsd4_access(rqstp, current_fh, &op->u.access); 8171da177e4SLinus Torvalds break; 8181da177e4SLinus Torvalds case OP_CLOSE: 819f2327d9aSNeil Brown op->status = nfsd4_close(rqstp, current_fh, &op->u.close, &replay_owner); 8201da177e4SLinus Torvalds break; 8211da177e4SLinus Torvalds case OP_COMMIT: 8221da177e4SLinus Torvalds op->status = nfsd4_commit(rqstp, current_fh, &op->u.commit); 8231da177e4SLinus Torvalds break; 8241da177e4SLinus Torvalds case OP_CREATE: 8251da177e4SLinus Torvalds op->status = nfsd4_create(rqstp, current_fh, &op->u.create); 8261da177e4SLinus Torvalds break; 8271da177e4SLinus Torvalds case OP_DELEGRETURN: 8281da177e4SLinus Torvalds op->status = nfsd4_delegreturn(rqstp, current_fh, &op->u.delegreturn); 8291da177e4SLinus Torvalds break; 8301da177e4SLinus Torvalds case OP_GETATTR: 8311da177e4SLinus Torvalds op->status = nfsd4_getattr(rqstp, current_fh, &op->u.getattr); 8321da177e4SLinus Torvalds break; 8331da177e4SLinus Torvalds case OP_GETFH: 8341da177e4SLinus Torvalds op->status = nfsd4_getfh(current_fh, &op->u.getfh); 8351da177e4SLinus Torvalds break; 8361da177e4SLinus Torvalds case OP_LINK: 8371da177e4SLinus Torvalds op->status = nfsd4_link(rqstp, current_fh, save_fh, &op->u.link); 8381da177e4SLinus Torvalds break; 8391da177e4SLinus Torvalds case OP_LOCK: 840f2327d9aSNeil Brown op->status = nfsd4_lock(rqstp, current_fh, &op->u.lock, &replay_owner); 8411da177e4SLinus Torvalds break; 8421da177e4SLinus Torvalds case OP_LOCKT: 8431da177e4SLinus Torvalds op->status = nfsd4_lockt(rqstp, current_fh, &op->u.lockt); 8441da177e4SLinus Torvalds break; 8451da177e4SLinus Torvalds case OP_LOCKU: 846f2327d9aSNeil Brown op->status = nfsd4_locku(rqstp, current_fh, &op->u.locku, &replay_owner); 8471da177e4SLinus Torvalds break; 8481da177e4SLinus Torvalds case OP_LOOKUP: 8491da177e4SLinus Torvalds op->status = nfsd4_lookup(rqstp, current_fh, &op->u.lookup); 8501da177e4SLinus Torvalds break; 8511da177e4SLinus Torvalds case OP_LOOKUPP: 8521da177e4SLinus Torvalds op->status = nfsd4_lookupp(rqstp, current_fh); 8531da177e4SLinus Torvalds break; 8541da177e4SLinus Torvalds case OP_NVERIFY: 8551da177e4SLinus Torvalds op->status = nfsd4_verify(rqstp, current_fh, &op->u.nverify); 8561da177e4SLinus Torvalds if (op->status == nfserr_not_same) 8571da177e4SLinus Torvalds op->status = nfs_ok; 8581da177e4SLinus Torvalds break; 8591da177e4SLinus Torvalds case OP_OPEN: 860f2327d9aSNeil Brown op->status = nfsd4_open(rqstp, current_fh, &op->u.open, &replay_owner); 8611da177e4SLinus Torvalds break; 8621da177e4SLinus Torvalds case OP_OPEN_CONFIRM: 863f2327d9aSNeil Brown op->status = nfsd4_open_confirm(rqstp, current_fh, &op->u.open_confirm, &replay_owner); 8641da177e4SLinus Torvalds break; 8651da177e4SLinus Torvalds case OP_OPEN_DOWNGRADE: 866f2327d9aSNeil Brown op->status = nfsd4_open_downgrade(rqstp, current_fh, &op->u.open_downgrade, &replay_owner); 8671da177e4SLinus Torvalds break; 8681da177e4SLinus Torvalds case OP_PUTFH: 8691da177e4SLinus Torvalds op->status = nfsd4_putfh(rqstp, current_fh, &op->u.putfh); 8701da177e4SLinus Torvalds break; 8711da177e4SLinus Torvalds case OP_PUTROOTFH: 8721da177e4SLinus Torvalds op->status = nfsd4_putrootfh(rqstp, current_fh); 8731da177e4SLinus Torvalds break; 8741da177e4SLinus Torvalds case OP_READ: 8751da177e4SLinus Torvalds op->status = nfsd4_read(rqstp, current_fh, &op->u.read); 8761da177e4SLinus Torvalds break; 8771da177e4SLinus Torvalds case OP_READDIR: 8781da177e4SLinus Torvalds op->status = nfsd4_readdir(rqstp, current_fh, &op->u.readdir); 8791da177e4SLinus Torvalds break; 8801da177e4SLinus Torvalds case OP_READLINK: 8811da177e4SLinus Torvalds op->status = nfsd4_readlink(rqstp, current_fh, &op->u.readlink); 8821da177e4SLinus Torvalds break; 8831da177e4SLinus Torvalds case OP_REMOVE: 8841da177e4SLinus Torvalds op->status = nfsd4_remove(rqstp, current_fh, &op->u.remove); 8851da177e4SLinus Torvalds break; 8861da177e4SLinus Torvalds case OP_RENAME: 8871da177e4SLinus Torvalds op->status = nfsd4_rename(rqstp, current_fh, save_fh, &op->u.rename); 8881da177e4SLinus Torvalds break; 8891da177e4SLinus Torvalds case OP_RENEW: 8901da177e4SLinus Torvalds op->status = nfsd4_renew(&op->u.renew); 8911da177e4SLinus Torvalds break; 8921da177e4SLinus Torvalds case OP_RESTOREFH: 8931da177e4SLinus Torvalds op->status = nfsd4_restorefh(current_fh, save_fh); 8941da177e4SLinus Torvalds break; 8951da177e4SLinus Torvalds case OP_SAVEFH: 8961da177e4SLinus Torvalds op->status = nfsd4_savefh(current_fh, save_fh); 8971da177e4SLinus Torvalds break; 8981da177e4SLinus Torvalds case OP_SETATTR: 8991da177e4SLinus Torvalds op->status = nfsd4_setattr(rqstp, current_fh, &op->u.setattr); 9001da177e4SLinus Torvalds break; 9011da177e4SLinus Torvalds case OP_SETCLIENTID: 9021da177e4SLinus Torvalds op->status = nfsd4_setclientid(rqstp, &op->u.setclientid); 9031da177e4SLinus Torvalds break; 9041da177e4SLinus Torvalds case OP_SETCLIENTID_CONFIRM: 9051da177e4SLinus Torvalds op->status = nfsd4_setclientid_confirm(rqstp, &op->u.setclientid_confirm); 9061da177e4SLinus Torvalds break; 9071da177e4SLinus Torvalds case OP_VERIFY: 9081da177e4SLinus Torvalds op->status = nfsd4_verify(rqstp, current_fh, &op->u.verify); 9091da177e4SLinus Torvalds if (op->status == nfserr_same) 9101da177e4SLinus Torvalds op->status = nfs_ok; 9111da177e4SLinus Torvalds break; 9121da177e4SLinus Torvalds case OP_WRITE: 9131da177e4SLinus Torvalds op->status = nfsd4_write(rqstp, current_fh, &op->u.write); 9141da177e4SLinus Torvalds break; 9151da177e4SLinus Torvalds case OP_RELEASE_LOCKOWNER: 9161da177e4SLinus Torvalds op->status = nfsd4_release_lockowner(rqstp, &op->u.release_lockowner); 9171da177e4SLinus Torvalds break; 9181da177e4SLinus Torvalds default: 9191da177e4SLinus Torvalds BUG_ON(op->status == nfs_ok); 9201da177e4SLinus Torvalds break; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds encode_op: 9241da177e4SLinus Torvalds if (op->status == NFSERR_REPLAY_ME) { 9251da177e4SLinus Torvalds op->replay = &replay_owner->so_replay; 9261da177e4SLinus Torvalds nfsd4_encode_replay(resp, op); 9271da177e4SLinus Torvalds status = op->status = op->replay->rp_status; 9281da177e4SLinus Torvalds } else { 9291da177e4SLinus Torvalds nfsd4_encode_operation(resp, op); 9301da177e4SLinus Torvalds status = op->status; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds if (replay_owner && (replay_owner != (void *)(-1))) { 9331da177e4SLinus Torvalds nfs4_put_stateowner(replay_owner); 9341da177e4SLinus Torvalds replay_owner = NULL; 9351da177e4SLinus Torvalds } 9367e06b7f9SNeilBrown /* XXX Ugh, we need to get rid of this kind of special case: */ 9377e06b7f9SNeilBrown if (op->opnum == OP_READ && op->u.read.rd_filp) 9387e06b7f9SNeilBrown fput(op->u.read.rd_filp); 939*e2b20950SShankar Anand 940*e2b20950SShankar Anand nfsd4_increment_op_stats(op->opnum); 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds out: 9441da177e4SLinus Torvalds nfsd4_release_compoundargs(args); 9451da177e4SLinus Torvalds if (current_fh) 9461da177e4SLinus Torvalds fh_put(current_fh); 9471da177e4SLinus Torvalds kfree(current_fh); 9481da177e4SLinus Torvalds if (save_fh) 9491da177e4SLinus Torvalds fh_put(save_fh); 9501da177e4SLinus Torvalds kfree(save_fh); 9511da177e4SLinus Torvalds return status; 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds #define nfs4svc_decode_voidargs NULL 9551da177e4SLinus Torvalds #define nfs4svc_release_void NULL 9561da177e4SLinus Torvalds #define nfsd4_voidres nfsd4_voidargs 9571da177e4SLinus Torvalds #define nfs4svc_release_compound NULL 9581da177e4SLinus Torvalds struct nfsd4_voidargs { int dummy; }; 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds #define PROC(name, argt, rest, relt, cache, respsize) \ 9611da177e4SLinus Torvalds { (svc_procfunc) nfsd4_proc_##name, \ 9621da177e4SLinus Torvalds (kxdrproc_t) nfs4svc_decode_##argt##args, \ 9631da177e4SLinus Torvalds (kxdrproc_t) nfs4svc_encode_##rest##res, \ 9641da177e4SLinus Torvalds (kxdrproc_t) nfs4svc_release_##relt, \ 9651da177e4SLinus Torvalds sizeof(struct nfsd4_##argt##args), \ 9661da177e4SLinus Torvalds sizeof(struct nfsd4_##rest##res), \ 9671da177e4SLinus Torvalds 0, \ 9681da177e4SLinus Torvalds cache, \ 9691da177e4SLinus Torvalds respsize, \ 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds /* 9731da177e4SLinus Torvalds * TODO: At the present time, the NFSv4 server does not do XID caching 9741da177e4SLinus Torvalds * of requests. Implementing XID caching would not be a serious problem, 9751da177e4SLinus Torvalds * although it would require a mild change in interfaces since one 9761da177e4SLinus Torvalds * doesn't know whether an NFSv4 request is idempotent until after the 9771da177e4SLinus Torvalds * XDR decode. However, XID caching totally confuses pynfs (Peter 9781da177e4SLinus Torvalds * Astrand's regression testsuite for NFSv4 servers), which reuses 9791da177e4SLinus Torvalds * XID's liberally, so I've left it unimplemented until pynfs generates 9801da177e4SLinus Torvalds * better XID's. 9811da177e4SLinus Torvalds */ 9821da177e4SLinus Torvalds static struct svc_procedure nfsd_procedures4[2] = { 9831da177e4SLinus Torvalds PROC(null, void, void, void, RC_NOCACHE, 1), 9847775f4c8SNeilBrown PROC(compound, compound, compound, compound, RC_NOCACHE, NFSD_BUFSIZE/4) 9851da177e4SLinus Torvalds }; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds struct svc_version nfsd_version4 = { 9881da177e4SLinus Torvalds .vs_vers = 4, 9891da177e4SLinus Torvalds .vs_nproc = 2, 9901da177e4SLinus Torvalds .vs_proc = nfsd_procedures4, 9911da177e4SLinus Torvalds .vs_dispatch = nfsd_dispatch, 9921da177e4SLinus Torvalds .vs_xdrsize = NFS4_SVC_XDRSIZE, 9931da177e4SLinus Torvalds }; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds /* 9961da177e4SLinus Torvalds * Local variables: 9971da177e4SLinus Torvalds * c-basic-offset: 8 9981da177e4SLinus Torvalds * End: 9991da177e4SLinus Torvalds */ 1000