blob: ef583854d8d03c77864c7d68d7cb3af3c88cfa47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/proc.c
3 *
4 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
5 *
6 * OS-independent nfs remote procedure call functions
7 *
8 * Tuned by Alan Cox <A.Cox@swansea.ac.uk> for >3K buffers
9 * so at last we can have decent(ish) throughput off a
10 * Sun server.
11 *
12 * Coding optimized and cleaned up by Florian La Roche.
13 * Note: Error returns are optimized for NFS_OK, which isn't translated via
14 * nfs_stat_to_errno(), but happens to be already the right return code.
15 *
16 * Also, the code currently doesn't check the size of the packet, when
17 * it decodes the packet.
18 *
19 * Feel free to fix it and mail me the diffs if it worries you.
20 *
21 * Completely rewritten to support the new RPC call interface;
22 * rewrote and moved the entire XDR stuff to xdr.c
23 * --Olaf Kirch June 1996
24 *
25 * The code below initializes all auto variables explicitly, otherwise
26 * it will fail to work as a module (gcc generates a memset call for an
27 * incomplete struct).
28 */
29
30#include <linux/types.h>
31#include <linux/param.h>
32#include <linux/slab.h>
33#include <linux/time.h>
34#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/errno.h>
36#include <linux/string.h>
37#include <linux/in.h>
38#include <linux/pagemap.h>
39#include <linux/sunrpc/clnt.h>
40#include <linux/nfs.h>
41#include <linux/nfs2.h>
42#include <linux/nfs_fs.h>
43#include <linux/nfs_page.h>
44#include <linux/lockd/bind.h>
David Howellsf7b422b2006-06-09 09:34:33 -040045#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define NFSDBG_FACILITY NFSDBG_PROC
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * Bare-bones access to getattr: this is for nfs_read_super.
51 */
52static int
53nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
54 struct nfs_fsinfo *info)
55{
56 struct nfs_fattr *fattr = info->fattr;
57 struct nfs2_fsstat fsinfo;
Chuck Leverdead28d2006-03-20 13:44:23 -050058 struct rpc_message msg = {
59 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
60 .rpc_argp = fhandle,
61 .rpc_resp = fattr,
62 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int status;
64
Harvey Harrison3110ff82008-05-02 13:42:44 -070065 dprintk("%s: call getattr\n", __func__);
Trond Myklebust0e574af2005-10-27 22:12:38 -040066 nfs_fattr_init(fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -040067 status = rpc_call_sync(server->client, &msg, 0);
68 /* Retry with default authentication if different */
69 if (status && server->nfs_client->cl_rpcclient != server->client)
70 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070071 dprintk("%s: reply getattr: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (status)
73 return status;
Harvey Harrison3110ff82008-05-02 13:42:44 -070074 dprintk("%s: call statfs\n", __func__);
Chuck Leverdead28d2006-03-20 13:44:23 -050075 msg.rpc_proc = &nfs_procedures[NFSPROC_STATFS];
76 msg.rpc_resp = &fsinfo;
EG Keizer37ca8f52008-08-19 16:34:36 -040077 status = rpc_call_sync(server->client, &msg, 0);
78 /* Retry with default authentication if different */
79 if (status && server->nfs_client->cl_rpcclient != server->client)
80 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070081 dprintk("%s: reply statfs: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (status)
83 return status;
84 info->rtmax = NFS_MAXDATA;
85 info->rtpref = fsinfo.tsize;
86 info->rtmult = fsinfo.bsize;
87 info->wtmax = NFS_MAXDATA;
88 info->wtpref = fsinfo.tsize;
89 info->wtmult = fsinfo.bsize;
90 info->dtpref = fsinfo.tsize;
91 info->maxfilesize = 0x7FFFFFFF;
92 info->lease_time = 0;
93 return 0;
94}
95
96/*
97 * One function for each procedure in the NFS protocol.
98 */
99static int
100nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
101 struct nfs_fattr *fattr)
102{
Chuck Leverdead28d2006-03-20 13:44:23 -0500103 struct rpc_message msg = {
104 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
105 .rpc_argp = fhandle,
106 .rpc_resp = fattr,
107 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int status;
109
110 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400111 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500112 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 dprintk("NFS reply getattr: %d\n", status);
114 return status;
115}
116
117static int
118nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
119 struct iattr *sattr)
120{
121 struct inode *inode = dentry->d_inode;
122 struct nfs_sattrargs arg = {
123 .fh = NFS_FH(inode),
124 .sattr = sattr
125 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500126 struct rpc_message msg = {
127 .rpc_proc = &nfs_procedures[NFSPROC_SETATTR],
128 .rpc_argp = &arg,
129 .rpc_resp = fattr,
130 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int status;
132
Trond Myklebustcf3fff52006-01-03 09:55:53 +0100133 /* Mask out the non-modebit related stuff from attr->ia_mode */
134 sattr->ia_mode &= S_IALLUGO;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 dprintk("NFS call setattr\n");
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400137 if (sattr->ia_valid & ATTR_FILE)
138 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400139 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500140 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400141 if (status == 0)
142 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 dprintk("NFS reply setattr: %d\n", status);
144 return status;
145}
146
147static int
148nfs_proc_lookup(struct inode *dir, struct qstr *name,
149 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
150{
151 struct nfs_diropargs arg = {
152 .fh = NFS_FH(dir),
153 .name = name->name,
154 .len = name->len
155 };
156 struct nfs_diropok res = {
157 .fh = fhandle,
158 .fattr = fattr
159 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500160 struct rpc_message msg = {
161 .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP],
162 .rpc_argp = &arg,
163 .rpc_resp = &res,
164 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int status;
166
167 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400168 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500169 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 dprintk("NFS reply lookup: %d\n", status);
171 return status;
172}
173
174static int nfs_proc_readlink(struct inode *inode, struct page *page,
175 unsigned int pgbase, unsigned int pglen)
176{
177 struct nfs_readlinkargs args = {
178 .fh = NFS_FH(inode),
179 .pgbase = pgbase,
180 .pglen = pglen,
181 .pages = &page
182 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500183 struct rpc_message msg = {
184 .rpc_proc = &nfs_procedures[NFSPROC_READLINK],
185 .rpc_argp = &args,
186 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int status;
188
189 dprintk("NFS call readlink\n");
Chuck Leverdead28d2006-03-20 13:44:23 -0500190 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 dprintk("NFS reply readlink: %d\n", status);
192 return status;
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int
196nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700197 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 struct nfs_fh fhandle;
200 struct nfs_fattr fattr;
201 struct nfs_createargs arg = {
202 .fh = NFS_FH(dir),
203 .name = dentry->d_name.name,
204 .len = dentry->d_name.len,
205 .sattr = sattr
206 };
207 struct nfs_diropok res = {
208 .fh = &fhandle,
209 .fattr = &fattr
210 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500211 struct rpc_message msg = {
212 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
213 .rpc_argp = &arg,
214 .rpc_resp = &res,
215 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int status;
217
Trond Myklebust0e574af2005-10-27 22:12:38 -0400218 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 dprintk("NFS call create %s\n", dentry->d_name.name);
Chuck Leverdead28d2006-03-20 13:44:23 -0500220 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustbad2a522007-10-20 13:07:21 -0400221 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (status == 0)
223 status = nfs_instantiate(dentry, &fhandle, &fattr);
224 dprintk("NFS reply create: %d\n", status);
225 return status;
226}
227
228/*
229 * In NFSv2, mknod is grafted onto the create call.
230 */
231static int
232nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
233 dev_t rdev)
234{
235 struct nfs_fh fhandle;
236 struct nfs_fattr fattr;
237 struct nfs_createargs arg = {
238 .fh = NFS_FH(dir),
239 .name = dentry->d_name.name,
240 .len = dentry->d_name.len,
241 .sattr = sattr
242 };
243 struct nfs_diropok res = {
244 .fh = &fhandle,
245 .fattr = &fattr
246 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500247 struct rpc_message msg = {
248 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
249 .rpc_argp = &arg,
250 .rpc_resp = &res,
251 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int status, mode;
253
254 dprintk("NFS call mknod %s\n", dentry->d_name.name);
255
256 mode = sattr->ia_mode;
257 if (S_ISFIFO(mode)) {
258 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
259 sattr->ia_valid &= ~ATTR_SIZE;
260 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
261 sattr->ia_valid |= ATTR_SIZE;
262 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
263 }
264
Trond Myklebust0e574af2005-10-27 22:12:38 -0400265 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500266 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400267 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (status == -EINVAL && S_ISFIFO(mode)) {
270 sattr->ia_mode = mode;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400271 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500272 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 if (status == 0)
275 status = nfs_instantiate(dentry, &fhandle, &fattr);
276 dprintk("NFS reply mknod: %d\n", status);
277 return status;
278}
279
280static int
281nfs_proc_remove(struct inode *dir, struct qstr *name)
282{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400283 struct nfs_removeargs arg = {
284 .fh = NFS_FH(dir),
285 .name.len = name->len,
286 .name.name = name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400288 struct rpc_message msg = {
289 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
290 .rpc_argp = &arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 };
292 int status;
293
294 dprintk("NFS call remove %s\n", name->name);
295 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400296 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 dprintk("NFS reply remove: %d\n", status);
299 return status;
300}
301
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400302static void
303nfs_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400308static int nfs_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400310 nfs_mark_for_revalidate(dir);
311 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314static int
315nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
316 struct inode *new_dir, struct qstr *new_name)
317{
318 struct nfs_renameargs arg = {
319 .fromfh = NFS_FH(old_dir),
320 .fromname = old_name->name,
321 .fromlen = old_name->len,
322 .tofh = NFS_FH(new_dir),
323 .toname = new_name->name,
324 .tolen = new_name->len
325 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500326 struct rpc_message msg = {
327 .rpc_proc = &nfs_procedures[NFSPROC_RENAME],
328 .rpc_argp = &arg,
329 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int status;
331
332 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Chuck Leverdead28d2006-03-20 13:44:23 -0500333 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400334 nfs_mark_for_revalidate(old_dir);
335 nfs_mark_for_revalidate(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dprintk("NFS reply rename: %d\n", status);
337 return status;
338}
339
340static int
341nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
342{
343 struct nfs_linkargs arg = {
344 .fromfh = NFS_FH(inode),
345 .tofh = NFS_FH(dir),
346 .toname = name->name,
347 .tolen = name->len
348 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500349 struct rpc_message msg = {
350 .rpc_proc = &nfs_procedures[NFSPROC_LINK],
351 .rpc_argp = &arg,
352 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 int status;
354
355 dprintk("NFS call link %s\n", name->name);
Chuck Leverdead28d2006-03-20 13:44:23 -0500356 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -0500357 nfs_mark_for_revalidate(inode);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400358 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 dprintk("NFS reply link: %d\n", status);
360 return status;
361}
362
363static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400364nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
365 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Chuck Lever4f390c12006-08-22 20:06:22 -0400367 struct nfs_fh fhandle;
368 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct nfs_symlinkargs arg = {
370 .fromfh = NFS_FH(dir),
Chuck Lever4f390c12006-08-22 20:06:22 -0400371 .fromname = dentry->d_name.name,
372 .fromlen = dentry->d_name.len,
Chuck Lever94a6d752006-08-22 20:06:23 -0400373 .pages = &page,
374 .pathlen = len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 .sattr = sattr
376 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500377 struct rpc_message msg = {
378 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
379 .rpc_argp = &arg,
380 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int status;
382
Chuck Lever94a6d752006-08-22 20:06:23 -0400383 if (len > NFS2_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400385
Chuck Lever94a6d752006-08-22 20:06:23 -0400386 dprintk("NFS call symlink %s\n", dentry->d_name.name);
387
Chuck Leverdead28d2006-03-20 13:44:23 -0500388 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400389 nfs_mark_for_revalidate(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -0400390
391 /*
392 * V2 SYMLINK requests don't return any attributes. Setting the
393 * filehandle size to zero indicates to nfs_instantiate that it
394 * should fill in the data with a LOOKUP call on the wire.
395 */
396 if (status == 0) {
397 nfs_fattr_init(&fattr);
398 fhandle.size = 0;
399 status = nfs_instantiate(dentry, &fhandle, &fattr);
400 }
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 dprintk("NFS reply symlink: %d\n", status);
403 return status;
404}
405
406static int
407nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
408{
409 struct nfs_fh fhandle;
410 struct nfs_fattr fattr;
411 struct nfs_createargs arg = {
412 .fh = NFS_FH(dir),
413 .name = dentry->d_name.name,
414 .len = dentry->d_name.len,
415 .sattr = sattr
416 };
417 struct nfs_diropok res = {
418 .fh = &fhandle,
419 .fattr = &fattr
420 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500421 struct rpc_message msg = {
422 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
423 .rpc_argp = &arg,
424 .rpc_resp = &res,
425 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int status;
427
428 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400429 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500430 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400431 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (status == 0)
433 status = nfs_instantiate(dentry, &fhandle, &fattr);
434 dprintk("NFS reply mkdir: %d\n", status);
435 return status;
436}
437
438static int
439nfs_proc_rmdir(struct inode *dir, struct qstr *name)
440{
441 struct nfs_diropargs arg = {
442 .fh = NFS_FH(dir),
443 .name = name->name,
444 .len = name->len
445 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500446 struct rpc_message msg = {
447 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
448 .rpc_argp = &arg,
449 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int status;
451
452 dprintk("NFS call rmdir %s\n", name->name);
Chuck Leverdead28d2006-03-20 13:44:23 -0500453 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400454 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 dprintk("NFS reply rmdir: %d\n", status);
456 return status;
457}
458
459/*
460 * The READDIR implementation is somewhat hackish - we pass a temporary
461 * buffer to the encode function, which installs it in the receive
462 * the receive iovec. The decode function just parses the reply to make
463 * sure it is syntactically correct; the entries itself are decoded
464 * from nfs_readdir by calling the decode_entry function directly.
465 */
466static int
467nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
468 u64 cookie, struct page *page, unsigned int count, int plus)
469{
470 struct inode *dir = dentry->d_inode;
471 struct nfs_readdirargs arg = {
472 .fh = NFS_FH(dir),
473 .cookie = cookie,
474 .count = count,
Chuck Leverdead28d2006-03-20 13:44:23 -0500475 .pages = &page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 };
477 struct rpc_message msg = {
478 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
479 .rpc_argp = &arg,
Chuck Leverdead28d2006-03-20 13:44:23 -0500480 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 };
482 int status;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
485 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
486
Trond Myklebustc4812992007-09-28 17:11:45 -0400487 nfs_invalidate_atime(dir);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 dprintk("NFS reply readdir: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return status;
491}
492
493static int
494nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
495 struct nfs_fsstat *stat)
496{
497 struct nfs2_fsstat fsinfo;
Chuck Leverdead28d2006-03-20 13:44:23 -0500498 struct rpc_message msg = {
499 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
500 .rpc_argp = fhandle,
501 .rpc_resp = &fsinfo,
502 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int status;
504
505 dprintk("NFS call statfs\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400506 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500507 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 dprintk("NFS reply statfs: %d\n", status);
509 if (status)
510 goto out;
511 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
512 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
513 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
514 stat->tfiles = 0;
515 stat->ffiles = 0;
516 stat->afiles = 0;
517out:
518 return status;
519}
520
521static int
522nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
523 struct nfs_fsinfo *info)
524{
525 struct nfs2_fsstat fsinfo;
Chuck Leverdead28d2006-03-20 13:44:23 -0500526 struct rpc_message msg = {
527 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
528 .rpc_argp = fhandle,
529 .rpc_resp = &fsinfo,
530 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int status;
532
533 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400534 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500535 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 dprintk("NFS reply fsinfo: %d\n", status);
537 if (status)
538 goto out;
539 info->rtmax = NFS_MAXDATA;
540 info->rtpref = fsinfo.tsize;
541 info->rtmult = fsinfo.bsize;
542 info->wtmax = NFS_MAXDATA;
543 info->wtpref = fsinfo.tsize;
544 info->wtmult = fsinfo.bsize;
545 info->dtpref = fsinfo.tsize;
546 info->maxfilesize = 0x7FFFFFFF;
547 info->lease_time = 0;
548out:
549 return status;
550}
551
552static int
553nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
554 struct nfs_pathconf *info)
555{
556 info->max_link = 0;
557 info->max_namelen = NFS2_MAXNAMLEN;
558 return 0;
559}
560
Trond Myklebustec06c092006-03-20 13:44:27 -0500561static int nfs_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Trond Myklebust8850df92007-09-28 17:20:07 -0400563 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (task->tk_status >= 0) {
565 nfs_refresh_inode(data->inode, data->res.fattr);
566 /* Emulate the eof flag, which isn't normally needed in NFSv2
567 * as it is guaranteed to always return the file attributes
568 */
569 if (data->args.offset + data->args.count >= data->res.fattr->size)
570 data->res.eof = 1;
571 }
Trond Myklebustec06c092006-03-20 13:44:27 -0500572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400575static void nfs_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400577 msg->rpc_proc = &nfs_procedures[NFSPROC_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Trond Myklebust788e7a82006-03-20 13:44:27 -0500580static int nfs_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (task->tk_status >= 0)
Trond Myklebust70ca8852007-09-30 15:21:24 -0400583 nfs_post_op_update_inode_force_wcc(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400587static void nfs_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
590 data->args.stable = NFS_FILE_SYNC;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400591 msg->rpc_proc = &nfs_procedures[NFSPROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594static void
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400595nfs_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 BUG();
598}
599
600static int
601nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
602{
Chuck Lever1093a602008-01-11 17:09:59 -0500603 struct inode *inode = filp->f_path.dentry->d_inode;
604
605 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Trond Myklebust21162712008-05-20 19:34:39 -0400608/* Helper functions for NFS lock bounds checking */
609#define NFS_LOCK32_OFFSET_MAX ((__s32)0x7fffffffUL)
610static int nfs_lock_check_bounds(const struct file_lock *fl)
611{
612 __s32 start, end;
613
614 start = (__s32)fl->fl_start;
615 if ((loff_t)start != fl->fl_start)
616 goto out_einval;
617
618 if (fl->fl_end != OFFSET_MAX) {
619 end = (__s32)fl->fl_end;
620 if ((loff_t)end != fl->fl_end)
621 goto out_einval;
622 } else
623 end = NFS_LOCK32_OFFSET_MAX;
624
625 if (start < 0 || start > end)
626 goto out_einval;
627 return 0;
628out_einval:
629 return -EINVAL;
630}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
David Howells509de812006-08-22 20:06:11 -0400632const struct nfs_rpc_ops nfs_v2_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 .version = 2, /* protocol version */
634 .dentry_ops = &nfs_dentry_operations,
635 .dir_inode_ops = &nfs_dir_inode_operations,
J. Bruce Fields92cfc622005-06-22 17:16:22 +0000636 .file_inode_ops = &nfs_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 .getroot = nfs_proc_get_root,
638 .getattr = nfs_proc_getattr,
639 .setattr = nfs_proc_setattr,
640 .lookup = nfs_proc_lookup,
641 .access = NULL, /* access */
642 .readlink = nfs_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 .create = nfs_proc_create,
644 .remove = nfs_proc_remove,
645 .unlink_setup = nfs_proc_unlink_setup,
646 .unlink_done = nfs_proc_unlink_done,
647 .rename = nfs_proc_rename,
648 .link = nfs_proc_link,
649 .symlink = nfs_proc_symlink,
650 .mkdir = nfs_proc_mkdir,
651 .rmdir = nfs_proc_rmdir,
652 .readdir = nfs_proc_readdir,
653 .mknod = nfs_proc_mknod,
654 .statfs = nfs_proc_statfs,
655 .fsinfo = nfs_proc_fsinfo,
656 .pathconf = nfs_proc_pathconf,
657 .decode_dirent = nfs_decode_dirent,
658 .read_setup = nfs_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500659 .read_done = nfs_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 .write_setup = nfs_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500661 .write_done = nfs_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 .commit_setup = nfs_proc_commit_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 .lock = nfs_proc_lock,
Trond Myklebust21162712008-05-20 19:34:39 -0400664 .lock_check_bounds = nfs_lock_check_bounds,
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400665 .close_context = nfs_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666};