blob: 193465210d7cdeec5548f7fbb2fbe0ccfecf2375 [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>
35#include <linux/utsname.h>
36#include <linux/errno.h>
37#include <linux/string.h>
38#include <linux/in.h>
39#include <linux/pagemap.h>
40#include <linux/sunrpc/clnt.h>
41#include <linux/nfs.h>
42#include <linux/nfs2.h>
43#include <linux/nfs_fs.h>
44#include <linux/nfs_page.h>
45#include <linux/lockd/bind.h>
David Howellsf7b422b2006-06-09 09:34:33 -040046#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define NFSDBG_FACILITY NFSDBG_PROC
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * Bare-bones access to getattr: this is for nfs_read_super.
52 */
53static int
54nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
55 struct nfs_fsinfo *info)
56{
57 struct nfs_fattr *fattr = info->fattr;
58 struct nfs2_fsstat fsinfo;
Chuck Leverdead28d2006-03-20 13:44:23 -050059 struct rpc_message msg = {
60 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
61 .rpc_argp = fhandle,
62 .rpc_resp = fattr,
63 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int status;
65
Harvey Harrison3110ff82008-05-02 13:42:44 -070066 dprintk("%s: call getattr\n", __func__);
Trond Myklebust0e574af2005-10-27 22:12:38 -040067 nfs_fattr_init(fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -040068 status = rpc_call_sync(server->client, &msg, 0);
69 /* Retry with default authentication if different */
70 if (status && server->nfs_client->cl_rpcclient != server->client)
71 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070072 dprintk("%s: reply getattr: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (status)
74 return status;
Harvey Harrison3110ff82008-05-02 13:42:44 -070075 dprintk("%s: call statfs\n", __func__);
Chuck Leverdead28d2006-03-20 13:44:23 -050076 msg.rpc_proc = &nfs_procedures[NFSPROC_STATFS];
77 msg.rpc_resp = &fsinfo;
EG Keizer37ca8f52008-08-19 16:34:36 -040078 status = rpc_call_sync(server->client, &msg, 0);
79 /* Retry with default authentication if different */
80 if (status && server->nfs_client->cl_rpcclient != server->client)
81 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070082 dprintk("%s: reply statfs: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (status)
84 return status;
85 info->rtmax = NFS_MAXDATA;
86 info->rtpref = fsinfo.tsize;
87 info->rtmult = fsinfo.bsize;
88 info->wtmax = NFS_MAXDATA;
89 info->wtpref = fsinfo.tsize;
90 info->wtmult = fsinfo.bsize;
91 info->dtpref = fsinfo.tsize;
92 info->maxfilesize = 0x7FFFFFFF;
93 info->lease_time = 0;
94 return 0;
95}
96
97/*
98 * One function for each procedure in the NFS protocol.
99 */
100static int
101nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
102 struct nfs_fattr *fattr)
103{
Chuck Leverdead28d2006-03-20 13:44:23 -0500104 struct rpc_message msg = {
105 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
106 .rpc_argp = fhandle,
107 .rpc_resp = fattr,
108 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int status;
110
111 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400112 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500113 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 dprintk("NFS reply getattr: %d\n", status);
115 return status;
116}
117
118static int
119nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
120 struct iattr *sattr)
121{
122 struct inode *inode = dentry->d_inode;
123 struct nfs_sattrargs arg = {
124 .fh = NFS_FH(inode),
125 .sattr = sattr
126 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500127 struct rpc_message msg = {
128 .rpc_proc = &nfs_procedures[NFSPROC_SETATTR],
129 .rpc_argp = &arg,
130 .rpc_resp = fattr,
131 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int status;
133
Trond Myklebustcf3fff52006-01-03 09:55:53 +0100134 /* Mask out the non-modebit related stuff from attr->ia_mode */
135 sattr->ia_mode &= S_IALLUGO;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 dprintk("NFS call setattr\n");
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400138 if (sattr->ia_valid & ATTR_FILE)
139 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400140 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500141 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400142 if (status == 0)
143 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 dprintk("NFS reply setattr: %d\n", status);
145 return status;
146}
147
148static int
149nfs_proc_lookup(struct inode *dir, struct qstr *name,
150 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
151{
152 struct nfs_diropargs arg = {
153 .fh = NFS_FH(dir),
154 .name = name->name,
155 .len = name->len
156 };
157 struct nfs_diropok res = {
158 .fh = fhandle,
159 .fattr = fattr
160 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500161 struct rpc_message msg = {
162 .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP],
163 .rpc_argp = &arg,
164 .rpc_resp = &res,
165 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int status;
167
168 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400169 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500170 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 dprintk("NFS reply lookup: %d\n", status);
172 return status;
173}
174
175static int nfs_proc_readlink(struct inode *inode, struct page *page,
176 unsigned int pgbase, unsigned int pglen)
177{
178 struct nfs_readlinkargs args = {
179 .fh = NFS_FH(inode),
180 .pgbase = pgbase,
181 .pglen = pglen,
182 .pages = &page
183 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500184 struct rpc_message msg = {
185 .rpc_proc = &nfs_procedures[NFSPROC_READLINK],
186 .rpc_argp = &args,
187 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int status;
189
190 dprintk("NFS call readlink\n");
Chuck Leverdead28d2006-03-20 13:44:23 -0500191 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 dprintk("NFS reply readlink: %d\n", status);
193 return status;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static int
197nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700198 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct nfs_fh fhandle;
201 struct nfs_fattr fattr;
202 struct nfs_createargs arg = {
203 .fh = NFS_FH(dir),
204 .name = dentry->d_name.name,
205 .len = dentry->d_name.len,
206 .sattr = sattr
207 };
208 struct nfs_diropok res = {
209 .fh = &fhandle,
210 .fattr = &fattr
211 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500212 struct rpc_message msg = {
213 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
214 .rpc_argp = &arg,
215 .rpc_resp = &res,
216 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int status;
218
Trond Myklebust0e574af2005-10-27 22:12:38 -0400219 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 dprintk("NFS call create %s\n", dentry->d_name.name);
Chuck Leverdead28d2006-03-20 13:44:23 -0500221 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustbad2a522007-10-20 13:07:21 -0400222 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (status == 0)
224 status = nfs_instantiate(dentry, &fhandle, &fattr);
225 dprintk("NFS reply create: %d\n", status);
226 return status;
227}
228
229/*
230 * In NFSv2, mknod is grafted onto the create call.
231 */
232static int
233nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
234 dev_t rdev)
235{
236 struct nfs_fh fhandle;
237 struct nfs_fattr fattr;
238 struct nfs_createargs arg = {
239 .fh = NFS_FH(dir),
240 .name = dentry->d_name.name,
241 .len = dentry->d_name.len,
242 .sattr = sattr
243 };
244 struct nfs_diropok res = {
245 .fh = &fhandle,
246 .fattr = &fattr
247 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500248 struct rpc_message msg = {
249 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
250 .rpc_argp = &arg,
251 .rpc_resp = &res,
252 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int status, mode;
254
255 dprintk("NFS call mknod %s\n", dentry->d_name.name);
256
257 mode = sattr->ia_mode;
258 if (S_ISFIFO(mode)) {
259 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
260 sattr->ia_valid &= ~ATTR_SIZE;
261 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
262 sattr->ia_valid |= ATTR_SIZE;
263 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
264 }
265
Trond Myklebust0e574af2005-10-27 22:12:38 -0400266 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500267 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400268 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (status == -EINVAL && S_ISFIFO(mode)) {
271 sattr->ia_mode = mode;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400272 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500273 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 if (status == 0)
276 status = nfs_instantiate(dentry, &fhandle, &fattr);
277 dprintk("NFS reply mknod: %d\n", status);
278 return status;
279}
280
281static int
282nfs_proc_remove(struct inode *dir, struct qstr *name)
283{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400284 struct nfs_removeargs arg = {
285 .fh = NFS_FH(dir),
286 .name.len = name->len,
287 .name.name = name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400289 struct rpc_message msg = {
290 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
291 .rpc_argp = &arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 };
293 int status;
294
295 dprintk("NFS call remove %s\n", name->name);
296 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400297 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 dprintk("NFS reply remove: %d\n", status);
300 return status;
301}
302
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400303static void
304nfs_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400309static int nfs_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400311 nfs_mark_for_revalidate(dir);
312 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315static int
316nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
317 struct inode *new_dir, struct qstr *new_name)
318{
319 struct nfs_renameargs arg = {
320 .fromfh = NFS_FH(old_dir),
321 .fromname = old_name->name,
322 .fromlen = old_name->len,
323 .tofh = NFS_FH(new_dir),
324 .toname = new_name->name,
325 .tolen = new_name->len
326 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500327 struct rpc_message msg = {
328 .rpc_proc = &nfs_procedures[NFSPROC_RENAME],
329 .rpc_argp = &arg,
330 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int status;
332
333 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Chuck Leverdead28d2006-03-20 13:44:23 -0500334 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400335 nfs_mark_for_revalidate(old_dir);
336 nfs_mark_for_revalidate(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 dprintk("NFS reply rename: %d\n", status);
338 return status;
339}
340
341static int
342nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
343{
344 struct nfs_linkargs arg = {
345 .fromfh = NFS_FH(inode),
346 .tofh = NFS_FH(dir),
347 .toname = name->name,
348 .tolen = name->len
349 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500350 struct rpc_message msg = {
351 .rpc_proc = &nfs_procedures[NFSPROC_LINK],
352 .rpc_argp = &arg,
353 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int status;
355
356 dprintk("NFS call link %s\n", name->name);
Chuck Leverdead28d2006-03-20 13:44:23 -0500357 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -0500358 nfs_mark_for_revalidate(inode);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400359 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 dprintk("NFS reply link: %d\n", status);
361 return status;
362}
363
364static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400365nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
366 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Chuck Lever4f390c12006-08-22 20:06:22 -0400368 struct nfs_fh fhandle;
369 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct nfs_symlinkargs arg = {
371 .fromfh = NFS_FH(dir),
Chuck Lever4f390c12006-08-22 20:06:22 -0400372 .fromname = dentry->d_name.name,
373 .fromlen = dentry->d_name.len,
Chuck Lever94a6d752006-08-22 20:06:23 -0400374 .pages = &page,
375 .pathlen = len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 .sattr = sattr
377 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500378 struct rpc_message msg = {
379 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
380 .rpc_argp = &arg,
381 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int status;
383
Chuck Lever94a6d752006-08-22 20:06:23 -0400384 if (len > NFS2_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400386
Chuck Lever94a6d752006-08-22 20:06:23 -0400387 dprintk("NFS call symlink %s\n", dentry->d_name.name);
388
Chuck Leverdead28d2006-03-20 13:44:23 -0500389 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400390 nfs_mark_for_revalidate(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -0400391
392 /*
393 * V2 SYMLINK requests don't return any attributes. Setting the
394 * filehandle size to zero indicates to nfs_instantiate that it
395 * should fill in the data with a LOOKUP call on the wire.
396 */
397 if (status == 0) {
398 nfs_fattr_init(&fattr);
399 fhandle.size = 0;
400 status = nfs_instantiate(dentry, &fhandle, &fattr);
401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dprintk("NFS reply symlink: %d\n", status);
404 return status;
405}
406
407static int
408nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
409{
410 struct nfs_fh fhandle;
411 struct nfs_fattr fattr;
412 struct nfs_createargs arg = {
413 .fh = NFS_FH(dir),
414 .name = dentry->d_name.name,
415 .len = dentry->d_name.len,
416 .sattr = sattr
417 };
418 struct nfs_diropok res = {
419 .fh = &fhandle,
420 .fattr = &fattr
421 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500422 struct rpc_message msg = {
423 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
424 .rpc_argp = &arg,
425 .rpc_resp = &res,
426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 int status;
428
429 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400430 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500431 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400432 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (status == 0)
434 status = nfs_instantiate(dentry, &fhandle, &fattr);
435 dprintk("NFS reply mkdir: %d\n", status);
436 return status;
437}
438
439static int
440nfs_proc_rmdir(struct inode *dir, struct qstr *name)
441{
442 struct nfs_diropargs arg = {
443 .fh = NFS_FH(dir),
444 .name = name->name,
445 .len = name->len
446 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500447 struct rpc_message msg = {
448 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
449 .rpc_argp = &arg,
450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int status;
452
453 dprintk("NFS call rmdir %s\n", name->name);
Chuck Leverdead28d2006-03-20 13:44:23 -0500454 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400455 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 dprintk("NFS reply rmdir: %d\n", status);
457 return status;
458}
459
460/*
461 * The READDIR implementation is somewhat hackish - we pass a temporary
462 * buffer to the encode function, which installs it in the receive
463 * the receive iovec. The decode function just parses the reply to make
464 * sure it is syntactically correct; the entries itself are decoded
465 * from nfs_readdir by calling the decode_entry function directly.
466 */
467static int
468nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
469 u64 cookie, struct page *page, unsigned int count, int plus)
470{
471 struct inode *dir = dentry->d_inode;
472 struct nfs_readdirargs arg = {
473 .fh = NFS_FH(dir),
474 .cookie = cookie,
475 .count = count,
Chuck Leverdead28d2006-03-20 13:44:23 -0500476 .pages = &page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 };
478 struct rpc_message msg = {
479 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
480 .rpc_argp = &arg,
Chuck Leverdead28d2006-03-20 13:44:23 -0500481 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 };
483 int status;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
486 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
487
Trond Myklebustc4812992007-09-28 17:11:45 -0400488 nfs_invalidate_atime(dir);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 dprintk("NFS reply readdir: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return status;
492}
493
494static int
495nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
496 struct nfs_fsstat *stat)
497{
498 struct nfs2_fsstat fsinfo;
Chuck Leverdead28d2006-03-20 13:44:23 -0500499 struct rpc_message msg = {
500 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
501 .rpc_argp = fhandle,
502 .rpc_resp = &fsinfo,
503 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int status;
505
506 dprintk("NFS call statfs\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400507 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500508 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 dprintk("NFS reply statfs: %d\n", status);
510 if (status)
511 goto out;
512 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
513 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
514 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
515 stat->tfiles = 0;
516 stat->ffiles = 0;
517 stat->afiles = 0;
518out:
519 return status;
520}
521
522static int
523nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
524 struct nfs_fsinfo *info)
525{
526 struct nfs2_fsstat fsinfo;
Chuck Leverdead28d2006-03-20 13:44:23 -0500527 struct rpc_message msg = {
528 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
529 .rpc_argp = fhandle,
530 .rpc_resp = &fsinfo,
531 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int status;
533
534 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400535 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500536 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 dprintk("NFS reply fsinfo: %d\n", status);
538 if (status)
539 goto out;
540 info->rtmax = NFS_MAXDATA;
541 info->rtpref = fsinfo.tsize;
542 info->rtmult = fsinfo.bsize;
543 info->wtmax = NFS_MAXDATA;
544 info->wtpref = fsinfo.tsize;
545 info->wtmult = fsinfo.bsize;
546 info->dtpref = fsinfo.tsize;
547 info->maxfilesize = 0x7FFFFFFF;
548 info->lease_time = 0;
549out:
550 return status;
551}
552
553static int
554nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
555 struct nfs_pathconf *info)
556{
557 info->max_link = 0;
558 info->max_namelen = NFS2_MAXNAMLEN;
559 return 0;
560}
561
Trond Myklebustec06c092006-03-20 13:44:27 -0500562static int nfs_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Trond Myklebust8850df92007-09-28 17:20:07 -0400564 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (task->tk_status >= 0) {
566 nfs_refresh_inode(data->inode, data->res.fattr);
567 /* Emulate the eof flag, which isn't normally needed in NFSv2
568 * as it is guaranteed to always return the file attributes
569 */
570 if (data->args.offset + data->args.count >= data->res.fattr->size)
571 data->res.eof = 1;
572 }
Trond Myklebustec06c092006-03-20 13:44:27 -0500573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400576static void nfs_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400578 msg->rpc_proc = &nfs_procedures[NFSPROC_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Trond Myklebust788e7a82006-03-20 13:44:27 -0500581static int nfs_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (task->tk_status >= 0)
Trond Myklebust70ca8852007-09-30 15:21:24 -0400584 nfs_post_op_update_inode_force_wcc(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400588static void nfs_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
591 data->args.stable = NFS_FILE_SYNC;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400592 msg->rpc_proc = &nfs_procedures[NFSPROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595static void
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400596nfs_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 BUG();
599}
600
601static int
602nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
603{
Chuck Lever1093a602008-01-11 17:09:59 -0500604 struct inode *inode = filp->f_path.dentry->d_inode;
605
606 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Trond Myklebust21162712008-05-20 19:34:39 -0400609/* Helper functions for NFS lock bounds checking */
610#define NFS_LOCK32_OFFSET_MAX ((__s32)0x7fffffffUL)
611static int nfs_lock_check_bounds(const struct file_lock *fl)
612{
613 __s32 start, end;
614
615 start = (__s32)fl->fl_start;
616 if ((loff_t)start != fl->fl_start)
617 goto out_einval;
618
619 if (fl->fl_end != OFFSET_MAX) {
620 end = (__s32)fl->fl_end;
621 if ((loff_t)end != fl->fl_end)
622 goto out_einval;
623 } else
624 end = NFS_LOCK32_OFFSET_MAX;
625
626 if (start < 0 || start > end)
627 goto out_einval;
628 return 0;
629out_einval:
630 return -EINVAL;
631}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
David Howells509de812006-08-22 20:06:11 -0400633const struct nfs_rpc_ops nfs_v2_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 .version = 2, /* protocol version */
635 .dentry_ops = &nfs_dentry_operations,
636 .dir_inode_ops = &nfs_dir_inode_operations,
J. Bruce Fields92cfc622005-06-22 17:16:22 +0000637 .file_inode_ops = &nfs_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 .getroot = nfs_proc_get_root,
639 .getattr = nfs_proc_getattr,
640 .setattr = nfs_proc_setattr,
641 .lookup = nfs_proc_lookup,
642 .access = NULL, /* access */
643 .readlink = nfs_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 .create = nfs_proc_create,
645 .remove = nfs_proc_remove,
646 .unlink_setup = nfs_proc_unlink_setup,
647 .unlink_done = nfs_proc_unlink_done,
648 .rename = nfs_proc_rename,
649 .link = nfs_proc_link,
650 .symlink = nfs_proc_symlink,
651 .mkdir = nfs_proc_mkdir,
652 .rmdir = nfs_proc_rmdir,
653 .readdir = nfs_proc_readdir,
654 .mknod = nfs_proc_mknod,
655 .statfs = nfs_proc_statfs,
656 .fsinfo = nfs_proc_fsinfo,
657 .pathconf = nfs_proc_pathconf,
658 .decode_dirent = nfs_decode_dirent,
659 .read_setup = nfs_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500660 .read_done = nfs_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 .write_setup = nfs_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500662 .write_done = nfs_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 .commit_setup = nfs_proc_commit_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 .lock = nfs_proc_lock,
Trond Myklebust21162712008-05-20 19:34:39 -0400665 .lock_check_bounds = nfs_lock_check_bounds,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666};