blob: fc51f7f6b36dc8ce57081069b26fee74c316011d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Process version 3 NFS requests.
3 *
4 * Copyright (C) 1996, 1997, 1998 Olaf Kirch <okir@monad.swb.de>
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/fs.h>
8#include <linux/ext2_fs.h>
Qinghuang Feng12214cb2009-01-12 03:13:53 +08009#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Boaz Harrosh9a74af22009-12-03 20:30:56 +020011#include "cache.h"
12#include "xdr3.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050013#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#define NFSDDBG_FACILITY NFSDDBG_PROC
16
17#define RETURN_STATUS(st) { resp->status = (st); return (st); }
18
19static int nfs3_ftypes[] = {
20 0, /* NF3NON */
21 S_IFREG, /* NF3REG */
22 S_IFDIR, /* NF3DIR */
23 S_IFBLK, /* NF3BLK */
24 S_IFCHR, /* NF3CHR */
25 S_IFLNK, /* NF3LNK */
26 S_IFSOCK, /* NF3SOCK */
27 S_IFIFO, /* NF3FIFO */
28};
29
30/*
31 * NULL call.
32 */
Al Viro7111c662006-10-19 23:28:45 -070033static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070034nfsd3_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
35{
36 return nfs_ok;
37}
38
39/*
40 * Get a file's attributes
41 */
Al Viro7111c662006-10-19 23:28:45 -070042static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070043nfsd3_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
44 struct nfsd3_attrstat *resp)
45{
Al Viroc4d987b2006-10-19 23:29:00 -070046 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 dprintk("nfsd: GETATTR(3) %s\n",
David Shawa334de22006-01-06 00:19:58 -080049 SVCFH_fmt(&argp->fh));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 fh_copy(&resp->fh, &argp->fh);
J. Bruce Fields04716e62008-08-07 13:00:20 -040052 nfserr = fh_verify(rqstp, &resp->fh, 0,
53 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
David Shawa334de22006-01-06 00:19:58 -080054 if (nfserr)
55 RETURN_STATUS(nfserr);
56
Al Viro3dadecc2013-01-24 02:18:08 -050057 nfserr = fh_getattr(&resp->fh, &resp->stat);
David Shawa334de22006-01-06 00:19:58 -080058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 RETURN_STATUS(nfserr);
60}
61
62/*
63 * Set a file's attributes
64 */
Al Viro7111c662006-10-19 23:28:45 -070065static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070066nfsd3_proc_setattr(struct svc_rqst *rqstp, struct nfsd3_sattrargs *argp,
67 struct nfsd3_attrstat *resp)
68{
Al Viroc4d987b2006-10-19 23:29:00 -070069 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 dprintk("nfsd: SETATTR(3) %s\n",
72 SVCFH_fmt(&argp->fh));
73
74 fh_copy(&resp->fh, &argp->fh);
75 nfserr = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,
76 argp->check_guard, argp->guardtime);
77 RETURN_STATUS(nfserr);
78}
79
80/*
81 * Look up a path name component
82 */
Al Viro7111c662006-10-19 23:28:45 -070083static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070084nfsd3_proc_lookup(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
85 struct nfsd3_diropres *resp)
86{
Al Viroc4d987b2006-10-19 23:29:00 -070087 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 dprintk("nfsd: LOOKUP(3) %s %.*s\n",
90 SVCFH_fmt(&argp->fh),
91 argp->len,
92 argp->name);
93
94 fh_copy(&resp->dirfh, &argp->fh);
95 fh_init(&resp->fh, NFS3_FHSIZE);
96
97 nfserr = nfsd_lookup(rqstp, &resp->dirfh,
98 argp->name,
99 argp->len,
100 &resp->fh);
101 RETURN_STATUS(nfserr);
102}
103
104/*
105 * Check file access
106 */
Al Viro7111c662006-10-19 23:28:45 -0700107static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108nfsd3_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
109 struct nfsd3_accessres *resp)
110{
Al Viroc4d987b2006-10-19 23:29:00 -0700111 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 dprintk("nfsd: ACCESS(3) %s 0x%x\n",
114 SVCFH_fmt(&argp->fh),
115 argp->access);
116
117 fh_copy(&resp->fh, &argp->fh);
118 resp->access = argp->access;
119 nfserr = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
120 RETURN_STATUS(nfserr);
121}
122
123/*
124 * Read a symlink.
125 */
Al Viro7111c662006-10-19 23:28:45 -0700126static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127nfsd3_proc_readlink(struct svc_rqst *rqstp, struct nfsd3_readlinkargs *argp,
128 struct nfsd3_readlinkres *resp)
129{
Al Viroc4d987b2006-10-19 23:29:00 -0700130 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 dprintk("nfsd: READLINK(3) %s\n", SVCFH_fmt(&argp->fh));
133
134 /* Read the symlink. */
135 fh_copy(&resp->fh, &argp->fh);
136 resp->len = NFS3_MAXPATHLEN;
137 nfserr = nfsd_readlink(rqstp, &resp->fh, argp->buffer, &resp->len);
138 RETURN_STATUS(nfserr);
139}
140
141/*
142 * Read a portion of a file.
143 */
Al Viro7111c662006-10-19 23:28:45 -0700144static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145nfsd3_proc_read(struct svc_rqst *rqstp, struct nfsd3_readargs *argp,
146 struct nfsd3_readres *resp)
147{
Al Viroc4d987b2006-10-19 23:29:00 -0700148 __be32 nfserr;
Greg Banks7adae482006-10-04 02:15:47 -0700149 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
J. Bruce Fields18b631f2010-11-29 15:28:10 -0500151 dprintk("nfsd: READ(3) %s %lu bytes at %Lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 SVCFH_fmt(&argp->fh),
153 (unsigned long) argp->count,
J. Bruce Fields18b631f2010-11-29 15:28:10 -0500154 (unsigned long long) argp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* Obtain buffer pointer for payload.
157 * 1 (status) + 22 (post_op_attr) + 1 (count) + 1 (eof)
158 * + 1 (xdr opaque byte count) = 26
159 */
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800160 resp->count = min(argp->count, max_blocksize);
Jeff Laytoncd123012007-05-09 02:34:50 -0700161 svc_reserve_auth(rqstp, ((1 + NFS3_POST_OP_ATTR_WORDS + 3)<<2) + resp->count +4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 fh_copy(&resp->fh, &argp->fh);
J. Bruce Fields039a87c2010-07-30 11:33:32 -0400164 nfserr = nfsd_read(rqstp, &resp->fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 argp->offset,
NeilBrown3cc03b12006-10-04 02:15:47 -0700166 rqstp->rq_vec, argp->vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 &resp->count);
168 if (nfserr == 0) {
169 struct inode *inode = resp->fh.fh_dentry->d_inode;
170
171 resp->eof = (argp->offset + resp->count) >= inode->i_size;
172 }
173
174 RETURN_STATUS(nfserr);
175}
176
177/*
178 * Write data to a file
179 */
Al Viro7111c662006-10-19 23:28:45 -0700180static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181nfsd3_proc_write(struct svc_rqst *rqstp, struct nfsd3_writeargs *argp,
182 struct nfsd3_writeres *resp)
183{
Al Viroc4d987b2006-10-19 23:29:00 -0700184 __be32 nfserr;
David Shaw31dec252009-03-05 20:16:14 -0500185 unsigned long cnt = argp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
J. Bruce Fields18b631f2010-11-29 15:28:10 -0500187 dprintk("nfsd: WRITE(3) %s %d bytes at %Lu%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 SVCFH_fmt(&argp->fh),
189 argp->len,
J. Bruce Fields18b631f2010-11-29 15:28:10 -0500190 (unsigned long long) argp->offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 argp->stable? " stable" : "");
192
193 fh_copy(&resp->fh, &argp->fh);
194 resp->committed = argp->stable;
195 nfserr = nfsd_write(rqstp, &resp->fh, NULL,
196 argp->offset,
NeilBrown3cc03b12006-10-04 02:15:47 -0700197 rqstp->rq_vec, argp->vlen,
David Shaw31dec252009-03-05 20:16:14 -0500198 &cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 &resp->committed);
David Shaw31dec252009-03-05 20:16:14 -0500200 resp->count = cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 RETURN_STATUS(nfserr);
202}
203
204/*
205 * With NFSv3, CREATE processing is a lot easier than with NFSv2.
206 * At least in theory; we'll see how it fares in practice when the
207 * first reports about SunOS compatibility problems start to pour in...
208 */
Al Viro7111c662006-10-19 23:28:45 -0700209static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210nfsd3_proc_create(struct svc_rqst *rqstp, struct nfsd3_createargs *argp,
211 struct nfsd3_diropres *resp)
212{
213 svc_fh *dirfhp, *newfhp = NULL;
214 struct iattr *attr;
Al Viroc4d987b2006-10-19 23:29:00 -0700215 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 dprintk("nfsd: CREATE(3) %s %.*s\n",
218 SVCFH_fmt(&argp->fh),
219 argp->len,
220 argp->name);
221
222 dirfhp = fh_copy(&resp->dirfh, &argp->fh);
223 newfhp = fh_init(&resp->fh, NFS3_FHSIZE);
224 attr = &argp->attrs;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* Unfudge the mode bits */
227 attr->ia_mode &= ~S_IFMT;
228 if (!(attr->ia_valid & ATTR_MODE)) {
229 attr->ia_valid |= ATTR_MODE;
230 attr->ia_mode = S_IFREG;
231 } else {
232 attr->ia_mode = (attr->ia_mode & ~S_IFMT) | S_IFREG;
233 }
234
235 /* Now create the file and set attributes */
Mi Jinlongac6721a2011-04-20 17:06:25 +0800236 nfserr = do_nfsd_create(rqstp, dirfhp, argp->name, argp->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 attr, newfhp,
J. Bruce Fields95c7a202012-07-27 15:50:11 -0400238 argp->createmode, (u32 *)argp->verf, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 RETURN_STATUS(nfserr);
241}
242
243/*
244 * Make directory. This operation is not idempotent.
245 */
Al Viro7111c662006-10-19 23:28:45 -0700246static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247nfsd3_proc_mkdir(struct svc_rqst *rqstp, struct nfsd3_createargs *argp,
248 struct nfsd3_diropres *resp)
249{
Al Viroc4d987b2006-10-19 23:29:00 -0700250 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 dprintk("nfsd: MKDIR(3) %s %.*s\n",
253 SVCFH_fmt(&argp->fh),
254 argp->len,
255 argp->name);
256
257 argp->attrs.ia_valid &= ~ATTR_SIZE;
258 fh_copy(&resp->dirfh, &argp->fh);
259 fh_init(&resp->fh, NFS3_FHSIZE);
260 nfserr = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
261 &argp->attrs, S_IFDIR, 0, &resp->fh);
Chuck Lever43a9aa62010-07-06 16:53:34 -0400262 fh_unlock(&resp->dirfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 RETURN_STATUS(nfserr);
264}
265
Al Viro7111c662006-10-19 23:28:45 -0700266static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267nfsd3_proc_symlink(struct svc_rqst *rqstp, struct nfsd3_symlinkargs *argp,
268 struct nfsd3_diropres *resp)
269{
Al Viroc4d987b2006-10-19 23:29:00 -0700270 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 dprintk("nfsd: SYMLINK(3) %s %.*s -> %.*s\n",
273 SVCFH_fmt(&argp->ffh),
274 argp->flen, argp->fname,
275 argp->tlen, argp->tname);
276
277 fh_copy(&resp->dirfh, &argp->ffh);
278 fh_init(&resp->fh, NFS3_FHSIZE);
279 nfserr = nfsd_symlink(rqstp, &resp->dirfh, argp->fname, argp->flen,
Kinglong Mee1e444f52014-07-01 17:48:02 +0800280 argp->tname, &resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 RETURN_STATUS(nfserr);
282}
283
284/*
285 * Make socket/fifo/device.
286 */
Al Viro7111c662006-10-19 23:28:45 -0700287static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288nfsd3_proc_mknod(struct svc_rqst *rqstp, struct nfsd3_mknodargs *argp,
289 struct nfsd3_diropres *resp)
290{
Al Viroc4d987b2006-10-19 23:29:00 -0700291 __be32 nfserr;
292 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 dev_t rdev = 0;
294
295 dprintk("nfsd: MKNOD(3) %s %.*s\n",
296 SVCFH_fmt(&argp->fh),
297 argp->len,
298 argp->name);
299
300 fh_copy(&resp->dirfh, &argp->fh);
301 fh_init(&resp->fh, NFS3_FHSIZE);
302
303 if (argp->ftype == 0 || argp->ftype >= NF3BAD)
304 RETURN_STATUS(nfserr_inval);
305 if (argp->ftype == NF3CHR || argp->ftype == NF3BLK) {
306 rdev = MKDEV(argp->major, argp->minor);
307 if (MAJOR(rdev) != argp->major ||
308 MINOR(rdev) != argp->minor)
309 RETURN_STATUS(nfserr_inval);
310 } else
311 if (argp->ftype != NF3SOCK && argp->ftype != NF3FIFO)
312 RETURN_STATUS(nfserr_inval);
313
314 type = nfs3_ftypes[argp->ftype];
315 nfserr = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
316 &argp->attrs, type, rdev, &resp->fh);
Chuck Lever43a9aa62010-07-06 16:53:34 -0400317 fh_unlock(&resp->dirfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 RETURN_STATUS(nfserr);
319}
320
321/*
322 * Remove file/fifo/socket etc.
323 */
Al Viro7111c662006-10-19 23:28:45 -0700324static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325nfsd3_proc_remove(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
326 struct nfsd3_attrstat *resp)
327{
Al Viroc4d987b2006-10-19 23:29:00 -0700328 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 dprintk("nfsd: REMOVE(3) %s %.*s\n",
331 SVCFH_fmt(&argp->fh),
332 argp->len,
333 argp->name);
334
335 /* Unlink. -S_IFDIR means file must not be a directory */
336 fh_copy(&resp->fh, &argp->fh);
337 nfserr = nfsd_unlink(rqstp, &resp->fh, -S_IFDIR, argp->name, argp->len);
Chuck Lever43a9aa62010-07-06 16:53:34 -0400338 fh_unlock(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 RETURN_STATUS(nfserr);
340}
341
342/*
343 * Remove a directory
344 */
Al Viro7111c662006-10-19 23:28:45 -0700345static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346nfsd3_proc_rmdir(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
347 struct nfsd3_attrstat *resp)
348{
Al Viroc4d987b2006-10-19 23:29:00 -0700349 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 dprintk("nfsd: RMDIR(3) %s %.*s\n",
352 SVCFH_fmt(&argp->fh),
353 argp->len,
354 argp->name);
355
356 fh_copy(&resp->fh, &argp->fh);
357 nfserr = nfsd_unlink(rqstp, &resp->fh, S_IFDIR, argp->name, argp->len);
Chuck Lever43a9aa62010-07-06 16:53:34 -0400358 fh_unlock(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 RETURN_STATUS(nfserr);
360}
361
Al Viro7111c662006-10-19 23:28:45 -0700362static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363nfsd3_proc_rename(struct svc_rqst *rqstp, struct nfsd3_renameargs *argp,
364 struct nfsd3_renameres *resp)
365{
Al Viroc4d987b2006-10-19 23:29:00 -0700366 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 dprintk("nfsd: RENAME(3) %s %.*s ->\n",
369 SVCFH_fmt(&argp->ffh),
370 argp->flen,
371 argp->fname);
372 dprintk("nfsd: -> %s %.*s\n",
373 SVCFH_fmt(&argp->tfh),
374 argp->tlen,
375 argp->tname);
376
377 fh_copy(&resp->ffh, &argp->ffh);
378 fh_copy(&resp->tfh, &argp->tfh);
379 nfserr = nfsd_rename(rqstp, &resp->ffh, argp->fname, argp->flen,
380 &resp->tfh, argp->tname, argp->tlen);
381 RETURN_STATUS(nfserr);
382}
383
Al Viro7111c662006-10-19 23:28:45 -0700384static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385nfsd3_proc_link(struct svc_rqst *rqstp, struct nfsd3_linkargs *argp,
386 struct nfsd3_linkres *resp)
387{
Al Viroc4d987b2006-10-19 23:29:00 -0700388 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 dprintk("nfsd: LINK(3) %s ->\n",
391 SVCFH_fmt(&argp->ffh));
392 dprintk("nfsd: -> %s %.*s\n",
393 SVCFH_fmt(&argp->tfh),
394 argp->tlen,
395 argp->tname);
396
397 fh_copy(&resp->fh, &argp->ffh);
398 fh_copy(&resp->tfh, &argp->tfh);
399 nfserr = nfsd_link(rqstp, &resp->tfh, argp->tname, argp->tlen,
400 &resp->fh);
401 RETURN_STATUS(nfserr);
402}
403
404/*
405 * Read a portion of a directory.
406 */
Al Viro7111c662006-10-19 23:28:45 -0700407static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408nfsd3_proc_readdir(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
409 struct nfsd3_readdirres *resp)
410{
Al Viroc4d987b2006-10-19 23:29:00 -0700411 __be32 nfserr;
412 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 dprintk("nfsd: READDIR(3) %s %d bytes at %d\n",
415 SVCFH_fmt(&argp->fh),
416 argp->count, (u32) argp->cookie);
417
418 /* Make sure we've room for the NULL ptr & eof flag, and shrink to
419 * client read size */
420 count = (argp->count >> 2) - 2;
421
422 /* Read directory and encode entries on the fly */
423 fh_copy(&resp->fh, &argp->fh);
424
425 resp->buflen = count;
426 resp->common.err = nfs_ok;
427 resp->buffer = argp->buffer;
428 resp->rqstp = rqstp;
429 nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t*) &argp->cookie,
430 &resp->common, nfs3svc_encode_entry);
431 memcpy(resp->verf, argp->verf, 8);
432 resp->count = resp->buffer - argp->buffer;
433 if (resp->offset)
434 xdr_encode_hyper(resp->offset, argp->cookie);
435
436 RETURN_STATUS(nfserr);
437}
438
439/*
440 * Read a portion of a directory, including file handles and attrs.
441 * For now, we choose to ignore the dircount parameter.
442 */
Al Viro7111c662006-10-19 23:28:45 -0700443static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444nfsd3_proc_readdirplus(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
445 struct nfsd3_readdirres *resp)
446{
Al Viroc4d987b2006-10-19 23:29:00 -0700447 __be32 nfserr;
448 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 loff_t offset;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500450 struct page **p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 caddr_t page_addr = NULL;
452
453 dprintk("nfsd: READDIR+(3) %s %d bytes at %d\n",
454 SVCFH_fmt(&argp->fh),
455 argp->count, (u32) argp->cookie);
456
457 /* Convert byte count to number of words (i.e. >> 2),
458 * and reserve room for the NULL ptr & eof flag (-2 words) */
459 resp->count = (argp->count >> 2) - 2;
460
461 /* Read directory and encode entries on the fly */
462 fh_copy(&resp->fh, &argp->fh);
463
464 resp->common.err = nfs_ok;
465 resp->buffer = argp->buffer;
466 resp->buflen = resp->count;
467 resp->rqstp = rqstp;
468 offset = argp->cookie;
469 nfserr = nfsd_readdir(rqstp, &resp->fh,
470 &offset,
471 &resp->common,
472 nfs3svc_encode_entry_plus);
473 memcpy(resp->verf, argp->verf, 8);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500474 for (p = rqstp->rq_respages + 1; p < rqstp->rq_next_page; p++) {
475 page_addr = page_address(*p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (((caddr_t)resp->buffer >= page_addr) &&
478 ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
479 count += (caddr_t)resp->buffer - page_addr;
480 break;
481 }
482 count += PAGE_SIZE;
483 }
484 resp->count = count >> 2;
485 if (resp->offset) {
486 if (unlikely(resp->offset1)) {
487 /* we ended up with offset on a page boundary */
488 *resp->offset = htonl(offset >> 32);
489 *resp->offset1 = htonl(offset & 0xffffffff);
490 resp->offset1 = NULL;
491 } else {
492 xdr_encode_hyper(resp->offset, offset);
493 }
494 }
495
496 RETURN_STATUS(nfserr);
497}
498
499/*
500 * Get file system stats
501 */
Al Viro7111c662006-10-19 23:28:45 -0700502static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503nfsd3_proc_fsstat(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
504 struct nfsd3_fsstatres *resp)
505{
Al Viroc4d987b2006-10-19 23:29:00 -0700506 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 dprintk("nfsd: FSSTAT(3) %s\n",
509 SVCFH_fmt(&argp->fh));
510
J. Bruce Fields04716e62008-08-07 13:00:20 -0400511 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 fh_put(&argp->fh);
513 RETURN_STATUS(nfserr);
514}
515
516/*
517 * Get file system info
518 */
Al Viro7111c662006-10-19 23:28:45 -0700519static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520nfsd3_proc_fsinfo(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
521 struct nfsd3_fsinfores *resp)
522{
Al Viroc4d987b2006-10-19 23:29:00 -0700523 __be32 nfserr;
Greg Banks7adae482006-10-04 02:15:47 -0700524 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 dprintk("nfsd: FSINFO(3) %s\n",
527 SVCFH_fmt(&argp->fh));
528
Greg Banks7adae482006-10-04 02:15:47 -0700529 resp->f_rtmax = max_blocksize;
530 resp->f_rtpref = max_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 resp->f_rtmult = PAGE_SIZE;
Greg Banks7adae482006-10-04 02:15:47 -0700532 resp->f_wtmax = max_blocksize;
533 resp->f_wtpref = max_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 resp->f_wtmult = PAGE_SIZE;
535 resp->f_dtpref = PAGE_SIZE;
536 resp->f_maxfilesize = ~(u32) 0;
537 resp->f_properties = NFS3_FSF_DEFAULT;
538
J. Bruce Fields04716e62008-08-07 13:00:20 -0400539 nfserr = fh_verify(rqstp, &argp->fh, 0,
540 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* Check special features of the file system. May request
543 * different read/write sizes for file systems known to have
544 * problems with large blocks */
545 if (nfserr == 0) {
546 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
547
548 /* Note that we don't care for remote fs's here */
Qinghuang Feng12214cb2009-01-12 03:13:53 +0800549 if (sb->s_magic == MSDOS_SUPER_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 resp->f_properties = NFS3_FSF_BILLYBOY;
551 }
552 resp->f_maxfilesize = sb->s_maxbytes;
553 }
554
555 fh_put(&argp->fh);
556 RETURN_STATUS(nfserr);
557}
558
559/*
560 * Get pathconf info for the specified file
561 */
Al Viro7111c662006-10-19 23:28:45 -0700562static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563nfsd3_proc_pathconf(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
564 struct nfsd3_pathconfres *resp)
565{
Al Viroc4d987b2006-10-19 23:29:00 -0700566 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 dprintk("nfsd: PATHCONF(3) %s\n",
569 SVCFH_fmt(&argp->fh));
570
571 /* Set default pathconf */
572 resp->p_link_max = 255; /* at least */
573 resp->p_name_max = 255; /* at least */
574 resp->p_no_trunc = 0;
575 resp->p_chown_restricted = 1;
576 resp->p_case_insensitive = 0;
577 resp->p_case_preserving = 1;
578
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200579 nfserr = fh_verify(rqstp, &argp->fh, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (nfserr == 0) {
582 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
583
584 /* Note that we don't care for remote fs's here */
585 switch (sb->s_magic) {
586 case EXT2_SUPER_MAGIC:
587 resp->p_link_max = EXT2_LINK_MAX;
588 resp->p_name_max = EXT2_NAME_LEN;
589 break;
Qinghuang Feng12214cb2009-01-12 03:13:53 +0800590 case MSDOS_SUPER_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 resp->p_case_insensitive = 1;
592 resp->p_case_preserving = 0;
593 break;
594 }
595 }
596
597 fh_put(&argp->fh);
598 RETURN_STATUS(nfserr);
599}
600
601
602/*
603 * Commit a file (range) to stable storage.
604 */
Al Viro7111c662006-10-19 23:28:45 -0700605static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606nfsd3_proc_commit(struct svc_rqst * rqstp, struct nfsd3_commitargs *argp,
607 struct nfsd3_commitres *resp)
608{
Al Viroc4d987b2006-10-19 23:29:00 -0700609 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 dprintk("nfsd: COMMIT(3) %s %u@%Lu\n",
612 SVCFH_fmt(&argp->fh),
613 argp->count,
614 (unsigned long long) argp->offset);
615
616 if (argp->offset > NFS_OFFSET_MAX)
617 RETURN_STATUS(nfserr_inval);
618
619 fh_copy(&resp->fh, &argp->fh);
620 nfserr = nfsd_commit(rqstp, &resp->fh, argp->offset, argp->count);
621
622 RETURN_STATUS(nfserr);
623}
624
625
626/*
627 * NFSv3 Server procedures.
628 * Only the results of non-idempotent operations are cached.
629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630#define nfs3svc_decode_fhandleargs nfs3svc_decode_fhandle
631#define nfs3svc_encode_attrstatres nfs3svc_encode_attrstat
632#define nfs3svc_encode_wccstatres nfs3svc_encode_wccstat
633#define nfsd3_mkdirargs nfsd3_createargs
634#define nfsd3_readdirplusargs nfsd3_readdirargs
635#define nfsd3_fhandleargs nfsd_fhandle
636#define nfsd3_fhandleres nfsd3_attrstat
637#define nfsd3_attrstatres nfsd3_attrstat
638#define nfsd3_wccstatres nfsd3_attrstat
639#define nfsd3_createres nfsd3_diropres
640#define nfsd3_voidres nfsd3_voidargs
641struct nfsd3_voidargs { int dummy; };
642
643#define PROC(name, argt, rest, relt, cache, respsize) \
644 { (svc_procfunc) nfsd3_proc_##name, \
645 (kxdrproc_t) nfs3svc_decode_##argt##args, \
646 (kxdrproc_t) nfs3svc_encode_##rest##res, \
647 (kxdrproc_t) nfs3svc_release_##relt, \
648 sizeof(struct nfsd3_##argt##args), \
649 sizeof(struct nfsd3_##rest##res), \
650 0, \
651 cache, \
652 respsize, \
653 }
654
655#define ST 1 /* status*/
656#define FH 17 /* filehandle with length */
657#define AT 21 /* attributes */
658#define pAT (1+AT) /* post attributes - conditional */
659#define WC (7+pAT) /* WCC attributes */
660
661static struct svc_procedure nfsd_procedures3[22] = {
Yu Zhiguob9081d92009-06-09 17:33:34 +0800662 [NFS3PROC_NULL] = {
663 .pc_func = (svc_procfunc) nfsd3_proc_null,
664 .pc_encode = (kxdrproc_t) nfs3svc_encode_voidres,
665 .pc_argsize = sizeof(struct nfsd3_voidargs),
666 .pc_ressize = sizeof(struct nfsd3_voidres),
667 .pc_cachetype = RC_NOCACHE,
668 .pc_xdrressize = ST,
669 },
670 [NFS3PROC_GETATTR] = {
671 .pc_func = (svc_procfunc) nfsd3_proc_getattr,
672 .pc_decode = (kxdrproc_t) nfs3svc_decode_fhandleargs,
673 .pc_encode = (kxdrproc_t) nfs3svc_encode_attrstatres,
674 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
675 .pc_argsize = sizeof(struct nfsd3_fhandleargs),
676 .pc_ressize = sizeof(struct nfsd3_attrstatres),
677 .pc_cachetype = RC_NOCACHE,
678 .pc_xdrressize = ST+AT,
679 },
680 [NFS3PROC_SETATTR] = {
681 .pc_func = (svc_procfunc) nfsd3_proc_setattr,
682 .pc_decode = (kxdrproc_t) nfs3svc_decode_sattrargs,
683 .pc_encode = (kxdrproc_t) nfs3svc_encode_wccstatres,
684 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
685 .pc_argsize = sizeof(struct nfsd3_sattrargs),
686 .pc_ressize = sizeof(struct nfsd3_wccstatres),
687 .pc_cachetype = RC_REPLBUFF,
688 .pc_xdrressize = ST+WC,
689 },
690 [NFS3PROC_LOOKUP] = {
691 .pc_func = (svc_procfunc) nfsd3_proc_lookup,
692 .pc_decode = (kxdrproc_t) nfs3svc_decode_diropargs,
693 .pc_encode = (kxdrproc_t) nfs3svc_encode_diropres,
694 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle2,
695 .pc_argsize = sizeof(struct nfsd3_diropargs),
696 .pc_ressize = sizeof(struct nfsd3_diropres),
697 .pc_cachetype = RC_NOCACHE,
698 .pc_xdrressize = ST+FH+pAT+pAT,
699 },
700 [NFS3PROC_ACCESS] = {
701 .pc_func = (svc_procfunc) nfsd3_proc_access,
702 .pc_decode = (kxdrproc_t) nfs3svc_decode_accessargs,
703 .pc_encode = (kxdrproc_t) nfs3svc_encode_accessres,
704 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
705 .pc_argsize = sizeof(struct nfsd3_accessargs),
706 .pc_ressize = sizeof(struct nfsd3_accessres),
707 .pc_cachetype = RC_NOCACHE,
708 .pc_xdrressize = ST+pAT+1,
709 },
710 [NFS3PROC_READLINK] = {
711 .pc_func = (svc_procfunc) nfsd3_proc_readlink,
712 .pc_decode = (kxdrproc_t) nfs3svc_decode_readlinkargs,
713 .pc_encode = (kxdrproc_t) nfs3svc_encode_readlinkres,
714 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
715 .pc_argsize = sizeof(struct nfsd3_readlinkargs),
716 .pc_ressize = sizeof(struct nfsd3_readlinkres),
717 .pc_cachetype = RC_NOCACHE,
718 .pc_xdrressize = ST+pAT+1+NFS3_MAXPATHLEN/4,
719 },
720 [NFS3PROC_READ] = {
721 .pc_func = (svc_procfunc) nfsd3_proc_read,
722 .pc_decode = (kxdrproc_t) nfs3svc_decode_readargs,
723 .pc_encode = (kxdrproc_t) nfs3svc_encode_readres,
724 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
725 .pc_argsize = sizeof(struct nfsd3_readargs),
726 .pc_ressize = sizeof(struct nfsd3_readres),
727 .pc_cachetype = RC_NOCACHE,
728 .pc_xdrressize = ST+pAT+4+NFSSVC_MAXBLKSIZE/4,
729 },
730 [NFS3PROC_WRITE] = {
731 .pc_func = (svc_procfunc) nfsd3_proc_write,
732 .pc_decode = (kxdrproc_t) nfs3svc_decode_writeargs,
733 .pc_encode = (kxdrproc_t) nfs3svc_encode_writeres,
734 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
735 .pc_argsize = sizeof(struct nfsd3_writeargs),
736 .pc_ressize = sizeof(struct nfsd3_writeres),
737 .pc_cachetype = RC_REPLBUFF,
738 .pc_xdrressize = ST+WC+4,
739 },
740 [NFS3PROC_CREATE] = {
741 .pc_func = (svc_procfunc) nfsd3_proc_create,
742 .pc_decode = (kxdrproc_t) nfs3svc_decode_createargs,
743 .pc_encode = (kxdrproc_t) nfs3svc_encode_createres,
744 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle2,
745 .pc_argsize = sizeof(struct nfsd3_createargs),
746 .pc_ressize = sizeof(struct nfsd3_createres),
747 .pc_cachetype = RC_REPLBUFF,
748 .pc_xdrressize = ST+(1+FH+pAT)+WC,
749 },
750 [NFS3PROC_MKDIR] = {
751 .pc_func = (svc_procfunc) nfsd3_proc_mkdir,
752 .pc_decode = (kxdrproc_t) nfs3svc_decode_mkdirargs,
753 .pc_encode = (kxdrproc_t) nfs3svc_encode_createres,
754 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle2,
755 .pc_argsize = sizeof(struct nfsd3_mkdirargs),
756 .pc_ressize = sizeof(struct nfsd3_createres),
757 .pc_cachetype = RC_REPLBUFF,
758 .pc_xdrressize = ST+(1+FH+pAT)+WC,
759 },
760 [NFS3PROC_SYMLINK] = {
761 .pc_func = (svc_procfunc) nfsd3_proc_symlink,
762 .pc_decode = (kxdrproc_t) nfs3svc_decode_symlinkargs,
763 .pc_encode = (kxdrproc_t) nfs3svc_encode_createres,
764 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle2,
765 .pc_argsize = sizeof(struct nfsd3_symlinkargs),
766 .pc_ressize = sizeof(struct nfsd3_createres),
767 .pc_cachetype = RC_REPLBUFF,
768 .pc_xdrressize = ST+(1+FH+pAT)+WC,
769 },
770 [NFS3PROC_MKNOD] = {
771 .pc_func = (svc_procfunc) nfsd3_proc_mknod,
772 .pc_decode = (kxdrproc_t) nfs3svc_decode_mknodargs,
773 .pc_encode = (kxdrproc_t) nfs3svc_encode_createres,
774 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle2,
775 .pc_argsize = sizeof(struct nfsd3_mknodargs),
776 .pc_ressize = sizeof(struct nfsd3_createres),
777 .pc_cachetype = RC_REPLBUFF,
778 .pc_xdrressize = ST+(1+FH+pAT)+WC,
779 },
780 [NFS3PROC_REMOVE] = {
781 .pc_func = (svc_procfunc) nfsd3_proc_remove,
782 .pc_decode = (kxdrproc_t) nfs3svc_decode_diropargs,
783 .pc_encode = (kxdrproc_t) nfs3svc_encode_wccstatres,
784 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
785 .pc_argsize = sizeof(struct nfsd3_diropargs),
786 .pc_ressize = sizeof(struct nfsd3_wccstatres),
787 .pc_cachetype = RC_REPLBUFF,
788 .pc_xdrressize = ST+WC,
789 },
790 [NFS3PROC_RMDIR] = {
791 .pc_func = (svc_procfunc) nfsd3_proc_rmdir,
792 .pc_decode = (kxdrproc_t) nfs3svc_decode_diropargs,
793 .pc_encode = (kxdrproc_t) nfs3svc_encode_wccstatres,
794 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
795 .pc_argsize = sizeof(struct nfsd3_diropargs),
796 .pc_ressize = sizeof(struct nfsd3_wccstatres),
797 .pc_cachetype = RC_REPLBUFF,
798 .pc_xdrressize = ST+WC,
799 },
800 [NFS3PROC_RENAME] = {
801 .pc_func = (svc_procfunc) nfsd3_proc_rename,
802 .pc_decode = (kxdrproc_t) nfs3svc_decode_renameargs,
803 .pc_encode = (kxdrproc_t) nfs3svc_encode_renameres,
804 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle2,
805 .pc_argsize = sizeof(struct nfsd3_renameargs),
806 .pc_ressize = sizeof(struct nfsd3_renameres),
807 .pc_cachetype = RC_REPLBUFF,
808 .pc_xdrressize = ST+WC+WC,
809 },
810 [NFS3PROC_LINK] = {
811 .pc_func = (svc_procfunc) nfsd3_proc_link,
812 .pc_decode = (kxdrproc_t) nfs3svc_decode_linkargs,
813 .pc_encode = (kxdrproc_t) nfs3svc_encode_linkres,
814 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle2,
815 .pc_argsize = sizeof(struct nfsd3_linkargs),
816 .pc_ressize = sizeof(struct nfsd3_linkres),
817 .pc_cachetype = RC_REPLBUFF,
818 .pc_xdrressize = ST+pAT+WC,
819 },
820 [NFS3PROC_READDIR] = {
821 .pc_func = (svc_procfunc) nfsd3_proc_readdir,
822 .pc_decode = (kxdrproc_t) nfs3svc_decode_readdirargs,
823 .pc_encode = (kxdrproc_t) nfs3svc_encode_readdirres,
824 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
825 .pc_argsize = sizeof(struct nfsd3_readdirargs),
826 .pc_ressize = sizeof(struct nfsd3_readdirres),
827 .pc_cachetype = RC_NOCACHE,
828 },
829 [NFS3PROC_READDIRPLUS] = {
830 .pc_func = (svc_procfunc) nfsd3_proc_readdirplus,
831 .pc_decode = (kxdrproc_t) nfs3svc_decode_readdirplusargs,
832 .pc_encode = (kxdrproc_t) nfs3svc_encode_readdirres,
833 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
834 .pc_argsize = sizeof(struct nfsd3_readdirplusargs),
835 .pc_ressize = sizeof(struct nfsd3_readdirres),
836 .pc_cachetype = RC_NOCACHE,
837 },
838 [NFS3PROC_FSSTAT] = {
839 .pc_func = (svc_procfunc) nfsd3_proc_fsstat,
840 .pc_decode = (kxdrproc_t) nfs3svc_decode_fhandleargs,
841 .pc_encode = (kxdrproc_t) nfs3svc_encode_fsstatres,
842 .pc_argsize = sizeof(struct nfsd3_fhandleargs),
843 .pc_ressize = sizeof(struct nfsd3_fsstatres),
844 .pc_cachetype = RC_NOCACHE,
845 .pc_xdrressize = ST+pAT+2*6+1,
846 },
847 [NFS3PROC_FSINFO] = {
848 .pc_func = (svc_procfunc) nfsd3_proc_fsinfo,
849 .pc_decode = (kxdrproc_t) nfs3svc_decode_fhandleargs,
850 .pc_encode = (kxdrproc_t) nfs3svc_encode_fsinfores,
851 .pc_argsize = sizeof(struct nfsd3_fhandleargs),
852 .pc_ressize = sizeof(struct nfsd3_fsinfores),
853 .pc_cachetype = RC_NOCACHE,
854 .pc_xdrressize = ST+pAT+12,
855 },
856 [NFS3PROC_PATHCONF] = {
857 .pc_func = (svc_procfunc) nfsd3_proc_pathconf,
858 .pc_decode = (kxdrproc_t) nfs3svc_decode_fhandleargs,
859 .pc_encode = (kxdrproc_t) nfs3svc_encode_pathconfres,
860 .pc_argsize = sizeof(struct nfsd3_fhandleargs),
861 .pc_ressize = sizeof(struct nfsd3_pathconfres),
862 .pc_cachetype = RC_NOCACHE,
863 .pc_xdrressize = ST+pAT+6,
864 },
865 [NFS3PROC_COMMIT] = {
866 .pc_func = (svc_procfunc) nfsd3_proc_commit,
867 .pc_decode = (kxdrproc_t) nfs3svc_decode_commitargs,
868 .pc_encode = (kxdrproc_t) nfs3svc_encode_commitres,
869 .pc_release = (kxdrproc_t) nfs3svc_release_fhandle,
870 .pc_argsize = sizeof(struct nfsd3_commitargs),
871 .pc_ressize = sizeof(struct nfsd3_commitres),
872 .pc_cachetype = RC_NOCACHE,
873 .pc_xdrressize = ST+WC+2,
874 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875};
876
877struct svc_version nfsd_version3 = {
878 .vs_vers = 3,
879 .vs_nproc = 22,
880 .vs_proc = nfsd_procedures3,
881 .vs_dispatch = nfsd_dispatch,
882 .vs_xdrsize = NFS3_SVC_XDRSIZE,
883};