4 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
6 * OS-independent nfs remote procedure call functions
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
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.
16 * Also, the code currently doesn't check the size of the packet, when
17 * it decodes the packet.
19 * Feel free to fix it and mail me the diffs if it worries you.
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
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
30 #include <linux/types.h>
31 #include <linux/param.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
35 #include <linux/utsname.h>
36 #include <linux/errno.h>
37 #include <linux/string.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>
46 #include <linux/smp_lock.h>
48 #define NFSDBG_FACILITY NFSDBG_PROC
50 extern struct rpc_procinfo nfs_procedures
[];
53 * Bare-bones access to getattr: this is for nfs_read_super.
56 nfs_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
57 struct nfs_fsinfo
*info
)
59 struct nfs_fattr
*fattr
= info
->fattr
;
60 struct nfs2_fsstat fsinfo
;
63 dprintk("%s: call getattr\n", __FUNCTION__
);
65 status
= rpc_call(server
->client_sys
, NFSPROC_GETATTR
, fhandle
, fattr
, 0);
66 dprintk("%s: reply getattr %d\n", __FUNCTION__
, status
);
69 dprintk("%s: call statfs\n", __FUNCTION__
);
70 status
= rpc_call(server
->client_sys
, NFSPROC_STATFS
, fhandle
, &fsinfo
, 0);
71 dprintk("%s: reply statfs %d\n", __FUNCTION__
, status
);
74 info
->rtmax
= NFS_MAXDATA
;
75 info
->rtpref
= fsinfo
.tsize
;
76 info
->rtmult
= fsinfo
.bsize
;
77 info
->wtmax
= NFS_MAXDATA
;
78 info
->wtpref
= fsinfo
.tsize
;
79 info
->wtmult
= fsinfo
.bsize
;
80 info
->dtpref
= fsinfo
.tsize
;
81 info
->maxfilesize
= 0x7FFFFFFF;
87 * One function for each procedure in the NFS protocol.
90 nfs_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
91 struct nfs_fattr
*fattr
)
95 dprintk("NFS call getattr\n");
97 status
= rpc_call(server
->client
, NFSPROC_GETATTR
,
99 dprintk("NFS reply getattr\n");
104 nfs_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
107 struct inode
*inode
= dentry
->d_inode
;
108 struct nfs_sattrargs arg
= {
114 dprintk("NFS call setattr\n");
116 status
= rpc_call(NFS_CLIENT(inode
), NFSPROC_SETATTR
, &arg
, fattr
, 0);
117 dprintk("NFS reply setattr\n");
122 nfs_proc_lookup(struct inode
*dir
, struct qstr
*name
,
123 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
125 struct nfs_diropargs arg
= {
130 struct nfs_diropok res
= {
136 dprintk("NFS call lookup %s\n", name
->name
);
138 status
= rpc_call(NFS_CLIENT(dir
), NFSPROC_LOOKUP
, &arg
, &res
, 0);
139 dprintk("NFS reply lookup: %d\n", status
);
143 static int nfs_proc_readlink(struct inode
*inode
, struct page
*page
,
144 unsigned int pgbase
, unsigned int pglen
)
146 struct nfs_readlinkargs args
= {
154 dprintk("NFS call readlink\n");
155 status
= rpc_call(NFS_CLIENT(inode
), NFSPROC_READLINK
, &args
, NULL
, 0);
156 dprintk("NFS reply readlink: %d\n", status
);
160 static int nfs_proc_read(struct nfs_read_data
*rdata
)
162 int flags
= rdata
->flags
;
163 struct inode
* inode
= rdata
->inode
;
164 struct nfs_fattr
* fattr
= rdata
->res
.fattr
;
165 struct rpc_message msg
= {
166 .rpc_proc
= &nfs_procedures
[NFSPROC_READ
],
167 .rpc_argp
= &rdata
->args
,
168 .rpc_resp
= &rdata
->res
,
169 .rpc_cred
= rdata
->cred
,
173 dprintk("NFS call read %d @ %Ld\n", rdata
->args
.count
,
174 (long long) rdata
->args
.offset
);
176 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, flags
);
178 nfs_refresh_inode(inode
, fattr
);
179 /* Emulate the eof flag, which isn't normally needed in NFSv2
180 * as it is guaranteed to always return the file attributes
182 if (rdata
->args
.offset
+ rdata
->args
.count
>= fattr
->size
)
185 dprintk("NFS reply read: %d\n", status
);
189 static int nfs_proc_write(struct nfs_write_data
*wdata
)
191 int flags
= wdata
->flags
;
192 struct inode
* inode
= wdata
->inode
;
193 struct nfs_fattr
* fattr
= wdata
->res
.fattr
;
194 struct rpc_message msg
= {
195 .rpc_proc
= &nfs_procedures
[NFSPROC_WRITE
],
196 .rpc_argp
= &wdata
->args
,
197 .rpc_resp
= &wdata
->res
,
198 .rpc_cred
= wdata
->cred
,
202 dprintk("NFS call write %d @ %Ld\n", wdata
->args
.count
,
203 (long long) wdata
->args
.offset
);
205 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, flags
);
207 nfs_refresh_inode(inode
, fattr
);
208 wdata
->res
.count
= wdata
->args
.count
;
209 wdata
->verf
.committed
= NFS_FILE_SYNC
;
211 dprintk("NFS reply write: %d\n", status
);
212 return status
< 0? status
: wdata
->res
.count
;
215 static struct inode
*
216 nfs_proc_create(struct inode
*dir
, struct qstr
*name
, struct iattr
*sattr
,
219 struct nfs_fh fhandle
;
220 struct nfs_fattr fattr
;
221 struct nfs_createargs arg
= {
227 struct nfs_diropok res
= {
234 dprintk("NFS call create %s\n", name
->name
);
235 status
= rpc_call(NFS_CLIENT(dir
), NFSPROC_CREATE
, &arg
, &res
, 0);
236 dprintk("NFS reply create: %d\n", status
);
239 inode
= nfs_fhget(dir
->i_sb
, &fhandle
, &fattr
);
244 return ERR_PTR(status
);
248 * In NFSv2, mknod is grafted onto the create call.
251 nfs_proc_mknod(struct inode
*dir
, struct qstr
*name
, struct iattr
*sattr
,
252 dev_t rdev
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
254 struct nfs_createargs arg
= {
260 struct nfs_diropok res
= {
266 dprintk("NFS call mknod %s\n", name
->name
);
268 mode
= sattr
->ia_mode
;
269 if (S_ISFIFO(mode
)) {
270 sattr
->ia_mode
= (mode
& ~S_IFMT
) | S_IFCHR
;
271 sattr
->ia_valid
&= ~ATTR_SIZE
;
272 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
273 sattr
->ia_valid
|= ATTR_SIZE
;
274 sattr
->ia_size
= new_encode_dev(rdev
);/* get out your barf bag */
278 status
= rpc_call(NFS_CLIENT(dir
), NFSPROC_CREATE
, &arg
, &res
, 0);
280 if (status
== -EINVAL
&& S_ISFIFO(mode
)) {
281 sattr
->ia_mode
= mode
;
283 status
= rpc_call(NFS_CLIENT(dir
), NFSPROC_CREATE
, &arg
, &res
, 0);
285 dprintk("NFS reply mknod: %d\n", status
);
290 nfs_proc_remove(struct inode
*dir
, struct qstr
*name
)
292 struct nfs_diropargs arg
= {
297 struct rpc_message msg
= {
298 .rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
],
305 dprintk("NFS call remove %s\n", name
->name
);
306 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
308 dprintk("NFS reply remove: %d\n", status
);
313 nfs_proc_unlink_setup(struct rpc_message
*msg
, struct dentry
*dir
, struct qstr
*name
)
315 struct nfs_diropargs
*arg
;
317 arg
= (struct nfs_diropargs
*)kmalloc(sizeof(*arg
), GFP_KERNEL
);
320 arg
->fh
= NFS_FH(dir
->d_inode
);
321 arg
->name
= name
->name
;
322 arg
->len
= name
->len
;
323 msg
->rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
];
329 nfs_proc_unlink_done(struct dentry
*dir
, struct rpc_task
*task
)
331 struct rpc_message
*msg
= &task
->tk_msg
;
334 kfree(msg
->rpc_argp
);
339 nfs_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
340 struct inode
*new_dir
, struct qstr
*new_name
)
342 struct nfs_renameargs arg
= {
343 .fromfh
= NFS_FH(old_dir
),
344 .fromname
= old_name
->name
,
345 .fromlen
= old_name
->len
,
346 .tofh
= NFS_FH(new_dir
),
347 .toname
= new_name
->name
,
348 .tolen
= new_name
->len
352 dprintk("NFS call rename %s -> %s\n", old_name
->name
, new_name
->name
);
353 status
= rpc_call(NFS_CLIENT(old_dir
), NFSPROC_RENAME
, &arg
, NULL
, 0);
354 dprintk("NFS reply rename: %d\n", status
);
359 nfs_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
361 struct nfs_linkargs arg
= {
362 .fromfh
= NFS_FH(inode
),
364 .toname
= name
->name
,
369 dprintk("NFS call link %s\n", name
->name
);
370 status
= rpc_call(NFS_CLIENT(inode
), NFSPROC_LINK
, &arg
, NULL
, 0);
371 dprintk("NFS reply link: %d\n", status
);
376 nfs_proc_symlink(struct inode
*dir
, struct qstr
*name
, struct qstr
*path
,
377 struct iattr
*sattr
, struct nfs_fh
*fhandle
,
378 struct nfs_fattr
*fattr
)
380 struct nfs_symlinkargs arg
= {
381 .fromfh
= NFS_FH(dir
),
382 .fromname
= name
->name
,
383 .fromlen
= name
->len
,
384 .topath
= path
->name
,
390 if (path
->len
> NFS2_MAXPATHLEN
)
391 return -ENAMETOOLONG
;
392 dprintk("NFS call symlink %s -> %s\n", name
->name
, path
->name
);
395 status
= rpc_call(NFS_CLIENT(dir
), NFSPROC_SYMLINK
, &arg
, NULL
, 0);
396 dprintk("NFS reply symlink: %d\n", status
);
401 nfs_proc_mkdir(struct inode
*dir
, struct qstr
*name
, struct iattr
*sattr
,
402 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
404 struct nfs_createargs arg
= {
410 struct nfs_diropok res
= {
416 dprintk("NFS call mkdir %s\n", name
->name
);
418 status
= rpc_call(NFS_CLIENT(dir
), NFSPROC_MKDIR
, &arg
, &res
, 0);
419 dprintk("NFS reply mkdir: %d\n", status
);
424 nfs_proc_rmdir(struct inode
*dir
, struct qstr
*name
)
426 struct nfs_diropargs arg
= {
433 dprintk("NFS call rmdir %s\n", name
->name
);
434 status
= rpc_call(NFS_CLIENT(dir
), NFSPROC_RMDIR
, &arg
, NULL
, 0);
435 dprintk("NFS reply rmdir: %d\n", status
);
440 * The READDIR implementation is somewhat hackish - we pass a temporary
441 * buffer to the encode function, which installs it in the receive
442 * the receive iovec. The decode function just parses the reply to make
443 * sure it is syntactically correct; the entries itself are decoded
444 * from nfs_readdir by calling the decode_entry function directly.
447 nfs_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
448 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
450 struct inode
*dir
= dentry
->d_inode
;
451 struct nfs_readdirargs arg
= {
457 struct rpc_message msg
= {
458 .rpc_proc
= &nfs_procedures
[NFSPROC_READDIR
],
467 dprintk("NFS call readdir %d\n", (unsigned int)cookie
);
468 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
470 dprintk("NFS reply readdir: %d\n", status
);
476 nfs_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
477 struct nfs_fsstat
*stat
)
479 struct nfs2_fsstat fsinfo
;
482 dprintk("NFS call statfs\n");
483 stat
->fattr
->valid
= 0;
484 status
= rpc_call(server
->client
, NFSPROC_STATFS
, fhandle
, &fsinfo
, 0);
485 dprintk("NFS reply statfs: %d\n", status
);
488 stat
->tbytes
= (u64
)fsinfo
.blocks
* fsinfo
.bsize
;
489 stat
->fbytes
= (u64
)fsinfo
.bfree
* fsinfo
.bsize
;
490 stat
->abytes
= (u64
)fsinfo
.bavail
* fsinfo
.bsize
;
499 nfs_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
500 struct nfs_fsinfo
*info
)
502 struct nfs2_fsstat fsinfo
;
505 dprintk("NFS call fsinfo\n");
506 info
->fattr
->valid
= 0;
507 status
= rpc_call(server
->client
, NFSPROC_STATFS
, fhandle
, &fsinfo
, 0);
508 dprintk("NFS reply fsinfo: %d\n", status
);
511 info
->rtmax
= NFS_MAXDATA
;
512 info
->rtpref
= fsinfo
.tsize
;
513 info
->rtmult
= fsinfo
.bsize
;
514 info
->wtmax
= NFS_MAXDATA
;
515 info
->wtpref
= fsinfo
.tsize
;
516 info
->wtmult
= fsinfo
.bsize
;
517 info
->dtpref
= fsinfo
.tsize
;
518 info
->maxfilesize
= 0x7FFFFFFF;
519 info
->lease_time
= 0;
525 nfs_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
526 struct nfs_pathconf
*info
)
529 info
->max_namelen
= NFS2_MAXNAMLEN
;
533 extern u32
* nfs_decode_dirent(u32
*, struct nfs_entry
*, int);
536 nfs_read_done(struct rpc_task
*task
)
538 struct nfs_read_data
*data
= (struct nfs_read_data
*) task
->tk_calldata
;
540 if (task
->tk_status
>= 0) {
541 nfs_refresh_inode(data
->inode
, data
->res
.fattr
);
542 /* Emulate the eof flag, which isn't normally needed in NFSv2
543 * as it is guaranteed to always return the file attributes
545 if (data
->args
.offset
+ data
->args
.count
>= data
->res
.fattr
->size
)
548 nfs_readpage_result(task
);
552 nfs_proc_read_setup(struct nfs_read_data
*data
)
554 struct rpc_task
*task
= &data
->task
;
555 struct inode
*inode
= data
->inode
;
557 struct rpc_message msg
= {
558 .rpc_proc
= &nfs_procedures
[NFSPROC_READ
],
559 .rpc_argp
= &data
->args
,
560 .rpc_resp
= &data
->res
,
561 .rpc_cred
= data
->cred
,
564 /* N.B. Do we need to test? Never called for swapfile inode */
565 flags
= RPC_TASK_ASYNC
| (IS_SWAPFILE(inode
)? NFS_RPC_SWAPFLAGS
: 0);
567 /* Finalize the task. */
568 rpc_init_task(task
, NFS_CLIENT(inode
), nfs_read_done
, flags
);
569 rpc_call_setup(task
, &msg
, 0);
573 nfs_write_done(struct rpc_task
*task
)
575 struct nfs_write_data
*data
= (struct nfs_write_data
*) task
->tk_calldata
;
577 if (task
->tk_status
>= 0)
578 nfs_refresh_inode(data
->inode
, data
->res
.fattr
);
579 nfs_writeback_done(task
);
583 nfs_proc_write_setup(struct nfs_write_data
*data
, int how
)
585 struct rpc_task
*task
= &data
->task
;
586 struct inode
*inode
= data
->inode
;
588 struct rpc_message msg
= {
589 .rpc_proc
= &nfs_procedures
[NFSPROC_WRITE
],
590 .rpc_argp
= &data
->args
,
591 .rpc_resp
= &data
->res
,
592 .rpc_cred
= data
->cred
,
595 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
596 data
->args
.stable
= NFS_FILE_SYNC
;
598 /* Set the initial flags for the task. */
599 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
601 /* Finalize the task. */
602 rpc_init_task(task
, NFS_CLIENT(inode
), nfs_write_done
, flags
);
603 rpc_call_setup(task
, &msg
, 0);
607 nfs_proc_commit_setup(struct nfs_write_data
*data
, int how
)
613 nfs_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
615 return nlmclnt_proc(filp
->f_dentry
->d_inode
, cmd
, fl
);
619 struct nfs_rpc_ops nfs_v2_clientops
= {
620 .version
= 2, /* protocol version */
621 .dentry_ops
= &nfs_dentry_operations
,
622 .dir_inode_ops
= &nfs_dir_inode_operations
,
623 .getroot
= nfs_proc_get_root
,
624 .getattr
= nfs_proc_getattr
,
625 .setattr
= nfs_proc_setattr
,
626 .lookup
= nfs_proc_lookup
,
627 .access
= NULL
, /* access */
628 .readlink
= nfs_proc_readlink
,
629 .read
= nfs_proc_read
,
630 .write
= nfs_proc_write
,
631 .commit
= NULL
, /* commit */
632 .create
= nfs_proc_create
,
633 .remove
= nfs_proc_remove
,
634 .unlink_setup
= nfs_proc_unlink_setup
,
635 .unlink_done
= nfs_proc_unlink_done
,
636 .rename
= nfs_proc_rename
,
637 .link
= nfs_proc_link
,
638 .symlink
= nfs_proc_symlink
,
639 .mkdir
= nfs_proc_mkdir
,
640 .rmdir
= nfs_proc_rmdir
,
641 .readdir
= nfs_proc_readdir
,
642 .mknod
= nfs_proc_mknod
,
643 .statfs
= nfs_proc_statfs
,
644 .fsinfo
= nfs_proc_fsinfo
,
645 .pathconf
= nfs_proc_pathconf
,
646 .decode_dirent
= nfs_decode_dirent
,
647 .read_setup
= nfs_proc_read_setup
,
648 .write_setup
= nfs_proc_write_setup
,
649 .commit_setup
= nfs_proc_commit_setup
,
650 .file_open
= nfs_open
,
651 .file_release
= nfs_release
,
652 .lock
= nfs_proc_lock
,