Linux-2.4.0-test2
[davej-history.git] / fs / nfs / proc.c
blob373d7296c1b6f27c069623274b02564def988eca
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_rename(struct dentry *old_dir, struct qstr *old_name,
237 struct dentry *new_dir, struct qstr *new_name)
239 struct nfs_renameargs arg = { NFS_FH(old_dir), old_name->name,
240 old_name->len,
241 NFS_FH(new_dir), new_name->name,
242 new_name->len};
243 int status;
245 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
246 status = rpc_call(NFS_CLIENT(old_dir->d_inode), NFSPROC_RENAME, &arg, NULL, 0);
247 dprintk("NFS reply rename: %d\n", status);
248 return status;
251 static int
252 nfs_proc_link(struct dentry *dentry, struct dentry *dir, struct qstr *name)
254 struct nfs_linkargs arg = { NFS_FH(dentry), NFS_FH(dir),
255 name->name, name->len };
256 int status;
258 dprintk("NFS call link %s\n", name->name);
259 status = rpc_call(NFS_CLIENT(dentry->d_inode), NFSPROC_LINK, &arg, NULL, 0);
260 dprintk("NFS reply link: %d\n", status);
261 return status;
264 static int
265 nfs_proc_symlink(struct dentry *dir, struct qstr *name, struct qstr *path,
266 struct iattr *sattr, struct nfs_fh *fhandle,
267 struct nfs_fattr *fattr)
269 struct nfs_symlinkargs arg = { NFS_FH(dir), name->name, name->len,
270 path->name, path->len, sattr };
271 int status;
273 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
274 fattr->valid = 0;
275 status = rpc_call(NFS_CLIENT(dir->d_inode), NFSPROC_SYMLINK, &arg, NULL, 0);
276 dprintk("NFS reply symlink: %d\n", status);
277 return status;
280 static int
281 nfs_proc_mkdir(struct dentry *dir, struct qstr *name, struct iattr *sattr,
282 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
284 struct nfs_createargs arg = { NFS_FH(dir), name->name, name->len,
285 sattr };
286 struct nfs_diropok res = { fhandle, fattr };
287 int status;
289 dprintk("NFS call mkdir %s\n", name->name);
290 fattr->valid = 0;
291 status = rpc_call(NFS_CLIENT(dir->d_inode), NFSPROC_MKDIR, &arg, &res, 0);
292 dprintk("NFS reply mkdir: %d\n", status);
293 return status;
296 static int
297 nfs_proc_rmdir(struct dentry *dir, struct qstr *name)
299 struct nfs_diropargs arg = { NFS_FH(dir), name->name, name->len };
300 int status;
302 dprintk("NFS call rmdir %s\n", name->name);
303 status = rpc_call(NFS_CLIENT(dir->d_inode), NFSPROC_RMDIR, &arg, NULL, 0);
304 dprintk("NFS reply rmdir: %d\n", status);
305 return status;
309 * The READDIR implementation is somewhat hackish - we pass a temporary
310 * buffer to the encode function, which installs it in the receive
311 * the receive iovec. The decode function just parses the reply to make
312 * sure it is syntactically correct; the entries itself are decoded
313 * from nfs_readdir by calling the decode_entry function directly.
315 static int
316 nfs_proc_readdir(struct file *file, __u64 cookie, void *entry,
317 unsigned int size, int plus)
319 struct dentry *dir = file->f_dentry;
320 struct rpc_cred *cred = nfs_file_cred(file);
321 struct nfs_readdirargs arg;
322 struct nfs_readdirres res;
323 struct rpc_message msg = { NFSPROC_READDIR, &arg, &res, cred };
324 struct nfs_server *server = NFS_DSERVER(dir);
325 int status;
327 if (server->rsize < size)
328 size = server->rsize;
330 arg.fh = NFS_FH(dir);
331 arg.cookie = cookie;
332 arg.buffer = entry;
333 arg.bufsiz = size;
334 res.buffer = entry;
335 res.bufsiz = size;
337 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
338 status = rpc_call_sync(NFS_CLIENT(dir->d_inode), &msg, 0);
340 dprintk("NFS reply readdir: %d\n", status);
341 return status;
344 static int
345 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
346 struct nfs_fsinfo *info)
348 int status;
350 dprintk("NFS call statfs\n");
351 memset((char *)info, 0, sizeof(*info));
352 status = rpc_call(server->client, NFSPROC_STATFS, fhandle, info, 0);
353 dprintk("NFS reply statfs: %d\n", status);
354 return status;
357 extern u32 * nfs_decode_dirent(u32 *, struct nfs_entry *, int);
359 struct nfs_rpc_ops nfs_v2_clientops = {
360 2, /* protocol version */
361 nfs_proc_get_root,
362 nfs_proc_getattr,
363 nfs_proc_setattr,
364 nfs_proc_lookup,
365 NULL, /* access */
366 nfs_proc_readlink,
367 nfs_proc_read,
368 nfs_proc_write,
369 NULL, /* commit */
370 nfs_proc_create,
371 nfs_proc_remove,
372 nfs_proc_rename,
373 nfs_proc_link,
374 nfs_proc_symlink,
375 nfs_proc_mkdir,
376 nfs_proc_rmdir,
377 nfs_proc_readdir,
378 nfs_proc_mknod,
379 nfs_proc_statfs,
380 nfs_decode_dirent,