- pre5:
[davej-history.git] / fs / nfs / proc.c
blob80ae95674f9fe8c4ea3000f8fceb52574b2efe23
1 /*
2 * linux/fs/nfs/proc.c
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
10 * Sun server.
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
27 * incomplete struct).
30 #include <linux/types.h>
31 #include <linux/param.h>
32 #include <linux/malloc.h>
33 #include <linux/sched.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>
45 #define NFSDBG_FACILITY NFSDBG_PROC
48 * Bare-bones access to getattr: this is for nfs_read_super.
50 static int
51 nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
52 struct nfs_fattr *fattr)
54 int status;
56 dprintk("NFS call getroot\n");
57 fattr->valid = 0;
58 status = rpc_call(server->client, NFSPROC_GETATTR, fhandle, fattr, 0);
59 dprintk("NFS reply getroot\n");
60 return status;
64 * One function for each procedure in the NFS protocol.
66 static int
67 nfs_proc_getattr(struct dentry *dentry, struct nfs_fattr *fattr)
69 int status;
71 dprintk("NFS call getattr\n");
72 fattr->valid = 0;
73 status = rpc_call(NFS_CLIENT(dentry->d_inode), NFSPROC_GETATTR,
74 NFS_FH(dentry), fattr, 0);
75 dprintk("NFS reply getattr\n");
76 return status;
79 static int
80 nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
81 struct iattr *sattr)
83 struct nfs_sattrargs arg = { NFS_FH(dentry), sattr };
84 int status;
86 dprintk("NFS call setattr\n");
87 fattr->valid = 0;
88 status = rpc_call(NFS_CLIENT(dentry->d_inode), NFSPROC_SETATTR, &arg, fattr, 0);
89 dprintk("NFS reply setattr\n");
90 return status;
93 static int
94 nfs_proc_lookup(struct dentry *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 };
99 int status;
101 dprintk("NFS call lookup %s\n", name->name);
102 fattr->valid = 0;
103 status = rpc_call(NFS_CLIENT(dir->d_inode), NFSPROC_LOOKUP, &arg, &res, 0);
104 dprintk("NFS reply lookup: %d\n", status);
105 return status;
108 static int
109 nfs_proc_readlink(struct dentry *dentry, void *buffer, unsigned int bufsiz)
111 struct nfs_readlinkargs args = { NFS_FH(dentry), buffer, bufsiz };
112 struct nfs_readlinkres res = { buffer, bufsiz };
113 int status;
115 dprintk("NFS call readlink\n");
116 status = rpc_call(NFS_CLIENT(dentry->d_inode), NFSPROC_READLINK,
117 &args, &res, 0);
118 dprintk("NFS reply readlink: %d\n", status);
119 return status;
122 static int
123 nfs_proc_read(struct file *file, struct nfs_fattr *fattr, int flags,
124 loff_t offset, unsigned int count, void *buffer, int *eofp)
126 struct dentry *dentry = file->f_dentry;
127 struct rpc_cred *cred = nfs_file_cred(file);
128 struct nfs_readargs arg = { NFS_FH(dentry), offset, count, 1,
129 {{ buffer, count }, {0,0}, {0,0}, {0,0},
130 {0,0}, {0,0}, {0,0}, {0,0}} };
131 struct nfs_readres res = { fattr, count, 0};
132 struct rpc_message msg = { NFSPROC_READ, &arg, &res, cred };
133 int status;
135 dprintk("NFS call read %d @ %Ld\n", count, (long long)offset);
136 fattr->valid = 0;
137 status = rpc_call_sync(NFS_CLIENT(dentry->d_inode), &msg, flags);
139 dprintk("NFS reply read: %d\n", status);
140 *eofp = res.eof;
141 return status;
144 static int
145 nfs_proc_write(struct file *file, struct nfs_fattr *fattr, int how,
146 loff_t offset, unsigned int count,
147 void *buffer, struct nfs_writeverf *verf)
149 struct dentry *dentry = file->f_dentry;
150 struct rpc_cred *cred = nfs_file_cred(file);
151 struct nfs_writeargs arg = {NFS_FH(dentry), offset, count,
152 NFS_FILE_SYNC, 1,
153 {{buffer, count}, {0,0}, {0,0}, {0,0},
154 {0,0}, {0,0}, {0,0}, {0,0}}};
155 struct nfs_writeres res = {fattr, verf, count};
156 struct rpc_message msg = { NFSPROC_WRITE, &arg, &res, cred };
157 int status, flags = 0;
159 dprintk("NFS call write %d @ %Ld\n", count, (long long)offset);
160 fattr->valid = 0;
161 if (how & NFS_RW_SWAP)
162 flags |= NFS_RPC_SWAPFLAGS;
163 status = rpc_call_sync(NFS_CLIENT(dentry->d_inode), &msg, flags);
165 dprintk("NFS reply write: %d\n", status);
166 verf->committed = NFS_FILE_SYNC; /* NFSv2 always syncs data */
167 return status < 0? status : count;
170 static int
171 nfs_proc_create(struct dentry *dir, struct qstr *name, struct iattr *sattr,
172 int flags, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
174 struct nfs_createargs arg = { NFS_FH(dir), name->name,
175 name->len, sattr };
176 struct nfs_diropok res = { fhandle, fattr };
177 int status;
179 fattr->valid = 0;
180 dprintk("NFS call create %s\n", name->name);
181 status = rpc_call(NFS_CLIENT(dir->d_inode), NFSPROC_CREATE, &arg, &res, 0);
182 dprintk("NFS reply create: %d\n", status);
183 return status;
187 * In NFSv2, mknod is grafted onto the create call.
189 static int
190 nfs_proc_mknod(struct dentry *dir, struct qstr *name, struct iattr *sattr,
191 dev_t rdev, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
193 struct nfs_createargs arg = { NFS_FH(dir), name->name,
194 name->len, sattr };
195 struct nfs_diropok res = { fhandle, fattr };
196 int status, mode;
198 dprintk("NFS call mknod %s\n", name->name);
200 mode = sattr->ia_mode;
201 if (S_ISFIFO(mode)) {
202 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
203 sattr->ia_valid &= ~ATTR_SIZE;
204 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
205 sattr->ia_valid |= ATTR_SIZE;
206 sattr->ia_size = rdev; /* get out your barf bag */
209 fattr->valid = 0;
210 status = rpc_call(NFS_CLIENT(dir->d_inode), NFSPROC_CREATE, &arg, &res, 0);
212 if (status == -EINVAL && S_ISFIFO(mode)) {
213 sattr->ia_mode = mode;
214 fattr->valid = 0;
215 status = rpc_call(NFS_CLIENT(dir->d_inode), NFSPROC_CREATE, &arg, &res, 0);
217 dprintk("NFS reply mknod: %d\n", status);
218 return status;
221 static int
222 nfs_proc_remove(struct dentry *dir, struct qstr *name)
224 struct nfs_diropargs arg = { NFS_FH(dir), name->name, name->len };
225 struct rpc_message msg = { NFSPROC_REMOVE, &arg, NULL, NULL };
226 int status;
228 dprintk("NFS call remove %s\n", name->name);
229 status = rpc_call_sync(NFS_CLIENT(dir->d_inode), &msg, 0);
231 dprintk("NFS reply remove: %d\n", status);
232 return status;
235 static int
236 nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
238 struct nfs_diropargs *arg;
240 arg = (struct nfs_diropargs *)kmalloc(sizeof(*arg), GFP_KERNEL);
241 if (!arg)
242 return -ENOMEM;
243 arg->fh = NFS_FH(dir);
244 arg->name = name->name;
245 arg->len = name->len;
246 msg->rpc_proc = NFSPROC_REMOVE;
247 msg->rpc_argp = arg;
248 return 0;
251 static void
252 nfs_proc_unlink_done(struct dentry *dir, struct rpc_message *msg)
254 NFS_CACHEINV(dir->d_inode);
255 kfree(msg->rpc_argp);
258 static int
259 nfs_proc_rename(struct dentry *old_dir, struct qstr *old_name,
260 struct dentry *new_dir, struct qstr *new_name)
262 struct nfs_renameargs arg = { NFS_FH(old_dir), old_name->name,
263 old_name->len,
264 NFS_FH(new_dir), new_name->name,
265 new_name->len};
266 int status;
268 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
269 status = rpc_call(NFS_CLIENT(old_dir->d_inode), NFSPROC_RENAME, &arg, NULL, 0);
270 dprintk("NFS reply rename: %d\n", status);
271 return status;
274 static int
275 nfs_proc_link(struct dentry *dentry, struct dentry *dir, struct qstr *name)
277 struct nfs_linkargs arg = { NFS_FH(dentry), NFS_FH(dir),
278 name->name, name->len };
279 int status;
281 dprintk("NFS call link %s\n", name->name);
282 status = rpc_call(NFS_CLIENT(dentry->d_inode), NFSPROC_LINK, &arg, NULL, 0);
283 dprintk("NFS reply link: %d\n", status);
284 return status;
287 static int
288 nfs_proc_symlink(struct dentry *dir, struct qstr *name, struct qstr *path,
289 struct iattr *sattr, struct nfs_fh *fhandle,
290 struct nfs_fattr *fattr)
292 struct nfs_symlinkargs arg = { NFS_FH(dir), name->name, name->len,
293 path->name, path->len, sattr };
294 int status;
296 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
297 fattr->valid = 0;
298 status = rpc_call(NFS_CLIENT(dir->d_inode), NFSPROC_SYMLINK, &arg, NULL, 0);
299 dprintk("NFS reply symlink: %d\n", status);
300 return status;
303 static int
304 nfs_proc_mkdir(struct dentry *dir, struct qstr *name, struct iattr *sattr,
305 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
307 struct nfs_createargs arg = { NFS_FH(dir), name->name, name->len,
308 sattr };
309 struct nfs_diropok res = { fhandle, fattr };
310 int status;
312 dprintk("NFS call mkdir %s\n", name->name);
313 fattr->valid = 0;
314 status = rpc_call(NFS_CLIENT(dir->d_inode), NFSPROC_MKDIR, &arg, &res, 0);
315 dprintk("NFS reply mkdir: %d\n", status);
316 return status;
319 static int
320 nfs_proc_rmdir(struct dentry *dir, struct qstr *name)
322 struct nfs_diropargs arg = { NFS_FH(dir), name->name, name->len };
323 int status;
325 dprintk("NFS call rmdir %s\n", name->name);
326 status = rpc_call(NFS_CLIENT(dir->d_inode), NFSPROC_RMDIR, &arg, NULL, 0);
327 dprintk("NFS reply rmdir: %d\n", status);
328 return status;
332 * The READDIR implementation is somewhat hackish - we pass a temporary
333 * buffer to the encode function, which installs it in the receive
334 * the receive iovec. The decode function just parses the reply to make
335 * sure it is syntactically correct; the entries itself are decoded
336 * from nfs_readdir by calling the decode_entry function directly.
338 static int
339 nfs_proc_readdir(struct file *file, __u64 cookie, void *entry,
340 unsigned int size, int plus)
342 struct dentry *dir = file->f_dentry;
343 struct rpc_cred *cred = nfs_file_cred(file);
344 struct nfs_readdirargs arg;
345 struct nfs_readdirres res;
346 struct rpc_message msg = { NFSPROC_READDIR, &arg, &res, cred };
347 int status;
349 arg.fh = NFS_FH(dir);
350 arg.cookie = cookie;
351 arg.buffer = entry;
352 arg.bufsiz = size;
353 res.buffer = entry;
354 res.bufsiz = size;
356 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
357 status = rpc_call_sync(NFS_CLIENT(dir->d_inode), &msg, 0);
359 dprintk("NFS reply readdir: %d\n", status);
360 return status;
363 static int
364 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
365 struct nfs_fsinfo *info)
367 int status;
369 dprintk("NFS call statfs\n");
370 memset((char *)info, 0, sizeof(*info));
371 status = rpc_call(server->client, NFSPROC_STATFS, fhandle, info, 0);
372 dprintk("NFS reply statfs: %d\n", status);
373 return status;
376 extern u32 * nfs_decode_dirent(u32 *, struct nfs_entry *, int);
378 struct nfs_rpc_ops nfs_v2_clientops = {
379 2, /* protocol version */
380 nfs_proc_get_root,
381 nfs_proc_getattr,
382 nfs_proc_setattr,
383 nfs_proc_lookup,
384 NULL, /* access */
385 nfs_proc_readlink,
386 nfs_proc_read,
387 nfs_proc_write,
388 NULL, /* commit */
389 nfs_proc_create,
390 nfs_proc_remove,
391 nfs_proc_unlink_setup,
392 nfs_proc_unlink_done,
393 nfs_proc_rename,
394 nfs_proc_link,
395 nfs_proc_symlink,
396 nfs_proc_mkdir,
397 nfs_proc_rmdir,
398 nfs_proc_readdir,
399 nfs_proc_mknod,
400 nfs_proc_statfs,
401 nfs_decode_dirent,