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/sched.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>
45 #define NFSDBG_FACILITY NFSDBG_PROC
48 * Bare-bones access to getattr: this is for nfs_read_super.
51 nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
52 struct nfs_fattr *fattr)
56 dprintk("NFS call getroot\n");
58 status = rpc_call(server->client, NFSPROC_GETATTR, fhandle, fattr, 0);
59 dprintk("NFS reply getroot\n");
64 * One function for each procedure in the NFS protocol.
67 nfs_proc_getattr(struct inode *inode, struct nfs_fattr *fattr)
71 dprintk("NFS call getattr\n");
73 status = rpc_call(NFS_CLIENT(inode), NFSPROC_GETATTR,
74 NFS_FH(inode), fattr, 0);
75 dprintk("NFS reply getattr\n");
80 nfs_proc_setattr(struct inode *inode, struct nfs_fattr *fattr,
83 struct nfs_sattrargs arg = { NFS_FH(inode), sattr };
86 dprintk("NFS call setattr\n");
88 status = rpc_call(NFS_CLIENT(inode), NFSPROC_SETATTR, &arg, fattr, 0);
89 dprintk("NFS reply setattr\n");
94 nfs_proc_lookup(struct inode *dir, struct qstr *name,
95 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
97 struct nfs_diropargs arg = { NFS_FH(dir), name->name, name->len };
98 struct nfs_diropok res = { fhandle, fattr };
101 dprintk("NFS call lookup %s\n", name->name);
103 status = rpc_call(NFS_CLIENT(dir), NFSPROC_LOOKUP, &arg, &res, 0);
104 dprintk("NFS reply lookup: %d\n", status);
109 nfs_proc_readlink(struct inode *inode, struct page *page)
111 struct nfs_readlinkargs args = { NFS_FH(inode), PAGE_CACHE_SIZE, &page };
114 dprintk("NFS call readlink\n");
115 status = rpc_call(NFS_CLIENT(inode), NFSPROC_READLINK, &args, NULL, 0);
116 dprintk("NFS reply readlink: %d\n", status);
121 nfs_proc_read(struct inode *inode, struct rpc_cred *cred,
122 struct nfs_fattr *fattr, int flags,
123 unsigned int base, unsigned int count,
124 struct page *page, int *eofp)
126 u64 offset = page_offset(page) + base;
127 struct nfs_readargs arg = { NFS_FH(inode), offset, count,
129 struct nfs_readres res = { fattr, count, 0};
130 struct rpc_message msg = { NFSPROC_READ, &arg, &res, cred };
133 dprintk("NFS call read %d @ %Ld\n", count, (long long)offset);
135 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
137 dprintk("NFS reply read: %d\n", status);
143 nfs_proc_write(struct inode *inode, struct rpc_cred *cred,
144 struct nfs_fattr *fattr, int how,
145 unsigned int base, unsigned int count,
146 struct page *page, struct nfs_writeverf *verf)
148 u64 offset = page_offset(page) + base;
149 struct nfs_writeargs arg = { NFS_FH(inode), offset, count,
150 NFS_FILE_SYNC, base, &page };
151 struct nfs_writeres res = {fattr, verf, count};
152 struct rpc_message msg = { NFSPROC_WRITE, &arg, &res, cred };
153 int status, flags = 0;
155 dprintk("NFS call write %d @ %Ld\n", count, (long long)offset);
157 if (how & NFS_RW_SWAP)
158 flags |= NFS_RPC_SWAPFLAGS;
159 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
161 dprintk("NFS reply write: %d\n", status);
162 verf->committed = NFS_FILE_SYNC; /* NFSv2 always syncs data */
163 return status < 0? status : count;
167 nfs_proc_create(struct inode *dir, struct qstr *name, struct iattr *sattr,
168 int flags, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
170 struct nfs_createargs arg = { NFS_FH(dir), name->name,
172 struct nfs_diropok res = { fhandle, fattr };
176 dprintk("NFS call create %s\n", name->name);
177 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
178 dprintk("NFS reply create: %d\n", status);
183 * In NFSv2, mknod is grafted onto the create call.
186 nfs_proc_mknod(struct inode *dir, struct qstr *name, struct iattr *sattr,
187 dev_t rdev, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
189 struct nfs_createargs arg = { NFS_FH(dir), name->name,
191 struct nfs_diropok res = { fhandle, fattr };
194 dprintk("NFS call mknod %s\n", name->name);
196 mode = sattr->ia_mode;
197 if (S_ISFIFO(mode)) {
198 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
199 sattr->ia_valid &= ~ATTR_SIZE;
200 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
201 sattr->ia_valid |= ATTR_SIZE;
202 sattr->ia_size = rdev; /* get out your barf bag */
206 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
208 if (status == -EINVAL && S_ISFIFO(mode)) {
209 sattr->ia_mode = mode;
211 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
213 dprintk("NFS reply mknod: %d\n", status);
218 nfs_proc_remove(struct inode *dir, struct qstr *name)
220 struct nfs_diropargs arg = { NFS_FH(dir), name->name, name->len };
221 struct rpc_message msg = { NFSPROC_REMOVE, &arg, NULL, NULL };
224 dprintk("NFS call remove %s\n", name->name);
225 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
227 dprintk("NFS reply remove: %d\n", status);
232 nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
234 struct nfs_diropargs *arg;
236 arg = (struct nfs_diropargs *)kmalloc(sizeof(*arg), GFP_KERNEL);
239 arg->fh = NFS_FH(dir->d_inode);
240 arg->name = name->name;
241 arg->len = name->len;
242 msg->rpc_proc = NFSPROC_REMOVE;
248 nfs_proc_unlink_done(struct dentry *dir, struct rpc_message *msg)
251 NFS_CACHEINV(dir->d_inode);
252 kfree(msg->rpc_argp);
257 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
258 struct inode *new_dir, struct qstr *new_name)
260 struct nfs_renameargs arg = { NFS_FH(old_dir), old_name->name,
262 NFS_FH(new_dir), new_name->name,
266 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
267 status = rpc_call(NFS_CLIENT(old_dir), NFSPROC_RENAME, &arg, NULL, 0);
268 dprintk("NFS reply rename: %d\n", status);
273 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
275 struct nfs_linkargs arg = { NFS_FH(inode), NFS_FH(dir),
276 name->name, name->len };
279 dprintk("NFS call link %s\n", name->name);
280 status = rpc_call(NFS_CLIENT(inode), NFSPROC_LINK, &arg, NULL, 0);
281 dprintk("NFS reply link: %d\n", status);
286 nfs_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
287 struct iattr *sattr, struct nfs_fh *fhandle,
288 struct nfs_fattr *fattr)
290 struct nfs_symlinkargs arg = { NFS_FH(dir), name->name, name->len,
291 path->name, path->len, sattr };
294 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
296 status = rpc_call(NFS_CLIENT(dir), NFSPROC_SYMLINK, &arg, NULL, 0);
297 dprintk("NFS reply symlink: %d\n", status);
302 nfs_proc_mkdir(struct inode *dir, struct qstr *name, struct iattr *sattr,
303 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
305 struct nfs_createargs arg = { NFS_FH(dir), name->name, name->len,
307 struct nfs_diropok res = { fhandle, fattr };
310 dprintk("NFS call mkdir %s\n", name->name);
312 status = rpc_call(NFS_CLIENT(dir), NFSPROC_MKDIR, &arg, &res, 0);
313 dprintk("NFS reply mkdir: %d\n", status);
318 nfs_proc_rmdir(struct inode *dir, struct qstr *name)
320 struct nfs_diropargs arg = { NFS_FH(dir), name->name, name->len };
323 dprintk("NFS call rmdir %s\n", name->name);
324 status = rpc_call(NFS_CLIENT(dir), NFSPROC_RMDIR, &arg, NULL, 0);
325 dprintk("NFS reply rmdir: %d\n", status);
330 * The READDIR implementation is somewhat hackish - we pass a temporary
331 * buffer to the encode function, which installs it in the receive
332 * the receive iovec. The decode function just parses the reply to make
333 * sure it is syntactically correct; the entries itself are decoded
334 * from nfs_readdir by calling the decode_entry function directly.
337 nfs_proc_readdir(struct inode *dir, struct rpc_cred *cred,
338 __u64 cookie, struct page *page,
339 unsigned int count, int plus)
341 struct nfs_readdirargs arg = { NFS_FH(dir), cookie, count, &page };
342 struct rpc_message msg = { NFSPROC_READDIR, &arg, NULL, cred };
345 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
346 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
348 dprintk("NFS reply readdir: %d\n", status);
353 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
354 struct nfs_fsinfo *info)
358 dprintk("NFS call statfs\n");
359 memset((char *)info, 0, sizeof(*info));
360 status = rpc_call(server->client, NFSPROC_STATFS, fhandle, info, 0);
361 dprintk("NFS reply statfs: %d\n", status);
365 extern u32 * nfs_decode_dirent(u32 *, struct nfs_entry *, int);
367 struct nfs_rpc_ops nfs_v2_clientops = {
368 2, /* protocol version */
380 nfs_proc_unlink_setup,
381 nfs_proc_unlink_done,