Linux-2.4.0-test2
[davej-history.git] / fs / nfs / nfs3proc.c
blob42191418f7b6327f92da313ad91025c45f77bd71
1 /*
2 * linux/fs/nfs/nfs3proc.c
4 * Client-side NFSv3 procedures stubs.
6 * Copyright (C) 1997, Olaf Kirch
7 */
9 #include <linux/mm.h>
10 #include <linux/utsname.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/nfs.h>
15 #include <linux/nfs3.h>
16 #include <linux/nfs_fs.h>
18 #define NFSDBG_FACILITY NFSDBG_PROC
21 * Bare-bones access to getattr: this is for nfs_read_super.
23 static int
24 nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
25 struct nfs_fattr *fattr)
27 int status;
29 dprintk("NFS call getroot\n");
30 fattr->valid = 0;
31 status = rpc_call(server->client, NFS3PROC_GETATTR, fhandle, fattr, 0);
32 dprintk("NFS reply getroot\n");
33 return status;
37 * One function for each procedure in the NFS protocol.
39 static int
40 nfs3_proc_getattr(struct dentry *dentry, struct nfs_fattr *fattr)
42 int status;
44 dprintk("NFS call getattr\n");
45 fattr->valid = 0;
46 status = rpc_call(NFS_CLIENT(dentry->d_inode), NFS3PROC_GETATTR,
47 NFS_FH(dentry), fattr, 0);
48 dprintk("NFS reply getattr\n");
49 return status;
52 static int
53 nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
54 struct iattr *sattr)
56 struct nfs3_sattrargs arg = { NFS_FH(dentry), sattr, 0, 0 };
57 int status;
59 dprintk("NFS call setattr\n");
60 fattr->valid = 0;
61 status = rpc_call(NFS_CLIENT(dentry->d_inode), NFS3PROC_SETATTR, &arg, fattr, 0);
62 dprintk("NFS reply setattr\n");
63 return status;
66 static int
67 nfs3_proc_lookup(struct dentry *dir, struct qstr *name,
68 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
70 struct nfs_fattr dir_attr;
71 struct nfs3_diropargs arg = { NFS_FH(dir), name->name, name->len };
72 struct nfs3_diropres res = { &dir_attr, fhandle, fattr };
73 int status;
75 dprintk("NFS call lookup %s\n", name->name);
76 dir_attr.valid = 0;
77 fattr->valid = 0;
78 status = rpc_call(NFS_CLIENT(dir->d_inode), NFS3PROC_LOOKUP, &arg, &res, 0);
79 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR))
80 status = rpc_call(NFS_CLIENT(dir->d_inode), NFS3PROC_GETATTR,
81 fhandle, fattr, 0);
82 dprintk("NFS reply lookup: %d\n", status);
83 nfs_refresh_inode(dir->d_inode, &dir_attr);
84 return status;
87 static int
88 nfs3_proc_access(struct dentry *dentry, int mode, int ruid)
90 struct nfs_fattr fattr;
91 struct nfs3_accessargs arg = { NFS_FH(dentry), 0 };
92 struct nfs3_accessres res = { &fattr, 0 };
93 int status, flags;
95 dprintk("NFS call access\n");
96 fattr.valid = 0;
98 if (mode & MAY_READ)
99 arg.access |= NFS3_ACCESS_READ;
100 if (S_ISDIR(dentry->d_inode->i_mode)) {
101 if (mode & MAY_WRITE)
102 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
103 if (mode & MAY_EXEC)
104 arg.access |= NFS3_ACCESS_LOOKUP;
105 } else {
106 if (mode & MAY_WRITE)
107 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
108 if (mode & MAY_EXEC)
109 arg.access |= NFS3_ACCESS_EXECUTE;
111 flags = (ruid) ? RPC_CALL_REALUID : 0;
112 status = rpc_call(NFS_CLIENT(dentry->d_inode), NFS3PROC_ACCESS, &arg, &res, flags);
113 nfs_refresh_inode(dentry->d_inode, &fattr);
114 dprintk("NFS reply access\n");
116 if (status == 0 && (arg.access & res.access) != arg.access)
117 status = -EACCES;
118 return status;
121 static int
122 nfs3_proc_readlink(struct dentry *dentry, void *buffer, unsigned int buflen)
124 struct nfs_fattr fattr;
125 struct nfs3_readlinkargs args = { NFS_FH(dentry), buffer, buflen };
126 struct nfs3_readlinkres res = { &fattr, buffer, buflen };
127 int status;
129 dprintk("NFS call readlink\n");
130 fattr.valid = 0;
131 status = rpc_call(NFS_CLIENT(dentry->d_inode), NFS3PROC_READLINK,
132 &args, &res, 0);
133 nfs_refresh_inode(dentry->d_inode, &fattr);
134 dprintk("NFS reply readlink: %d\n", status);
135 return status;
138 static int
139 nfs3_proc_read(struct file *file, struct nfs_fattr *fattr, int flags,
140 loff_t offset, unsigned int count, void *buffer, int *eofp)
142 struct dentry *dentry = file->f_dentry;
143 struct rpc_cred *cred = nfs_file_cred(file);
144 struct nfs_readargs arg = { NFS_FH(dentry), offset, count, 1,
145 {{buffer, count}, {0,0}, {0,0}, {0,0},
146 {0,0}, {0,0}, {0,0}, {0,0}} };
147 struct nfs_readres res = { fattr, count, 0 };
148 struct rpc_message msg = { NFS3PROC_READ, &arg, &res, cred };
149 int status;
151 dprintk("NFS call read %d @ %Ld\n", count, (long long)offset);
152 fattr->valid = 0;
153 status = rpc_call_sync(NFS_CLIENT(dentry->d_inode), &msg, flags);
154 dprintk("NFS reply read: %d\n", status);
155 *eofp = res.eof;
156 return status;
159 static int
160 nfs3_proc_write(struct file *file, struct nfs_fattr *fattr, int flags,
161 loff_t offset, unsigned int count,
162 void *buffer, struct nfs_writeverf *verf)
164 struct dentry *dentry = file->f_dentry;
165 struct rpc_cred *cred = nfs_file_cred(file);
166 struct nfs_writeargs arg = { NFS_FH(dentry), offset, count,
167 NFS_FILE_SYNC, 1,
168 {{buffer, count}, {0,0}, {0,0}, {0,0},
169 {0,0}, {0,0}, {0,0}, {0,0}} };
170 struct nfs_writeres res = { fattr, verf, 0 };
171 struct rpc_message msg = { NFS3PROC_WRITE, &arg, &res, cred };
172 int status, rpcflags = 0;
174 dprintk("NFS call write %d @ %Ld\n", count, (long long)offset);
175 fattr->valid = 0;
176 if (flags & NFS_RW_SWAP)
177 rpcflags |= NFS_RPC_SWAPFLAGS;
178 arg.stable = (flags & NFS_RW_SYNC) ? NFS_FILE_SYNC : NFS_UNSTABLE;
180 status = rpc_call_sync(NFS_CLIENT(dentry->d_inode), &msg, rpcflags);
182 dprintk("NFS reply read: %d\n", status);
183 return status < 0? status : res.count;
187 * Create a regular file.
188 * For now, we don't implement O_EXCL.
190 static int
191 nfs3_proc_create(struct dentry *dir, struct qstr *name, struct iattr *sattr,
192 int flags, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
194 struct nfs_fattr dir_attr;
195 struct nfs3_createargs arg = { NFS_FH(dir), name->name, name->len,
196 sattr, 0, { 0, 0 } };
197 struct nfs3_diropres res = { &dir_attr, fhandle, fattr };
198 int status;
200 dprintk("NFS call create %s\n", name->name);
201 arg.createmode = NFS3_CREATE_UNCHECKED;
202 if (flags & O_EXCL) {
203 arg.createmode = NFS3_CREATE_EXCLUSIVE;
204 arg.verifier[0] = jiffies;
205 arg.verifier[1] = current->pid;
208 again:
209 dir_attr.valid = 0;
210 fattr->valid = 0;
211 status = rpc_call(NFS_CLIENT(dir->d_inode), NFS3PROC_CREATE, &arg, &res, 0);
212 nfs_refresh_inode(dir->d_inode, &dir_attr);
214 /* If the server doesn't support the exclusive creation semantics,
215 * try again with simple 'guarded' mode. */
216 if (status == NFSERR_NOTSUPP) {
217 switch (arg.createmode) {
218 case NFS3_CREATE_EXCLUSIVE:
219 arg.createmode = NFS3_CREATE_GUARDED;
220 break;
222 case NFS3_CREATE_GUARDED:
223 arg.createmode = NFS3_CREATE_UNCHECKED;
224 break;
226 case NFS3_CREATE_UNCHECKED:
227 goto exit;
229 goto again;
232 exit:
233 dprintk("NFS reply create: %d\n", status);
235 /* When we created the file with exclusive semantics, make
236 * sure we set the attributes afterwards. */
237 if (status == 0 && arg.createmode == NFS3_CREATE_EXCLUSIVE) {
238 struct nfs3_sattrargs arg = { fhandle, sattr, 0, 0 };
239 dprintk("NFS call setattr (post-create)\n");
241 /* Note: we could use a guarded setattr here, but I'm
242 * not sure this buys us anything (and I'd have
243 * to revamp the NFSv3 XDR code) */
244 fattr->valid = 0;
245 status = rpc_call(NFS_CLIENT(dir->d_inode), NFS3PROC_SETATTR,
246 &arg, fattr, 0);
247 dprintk("NFS reply setattr (post-create): %d\n", status);
250 return status;
253 static int
254 nfs3_proc_remove(struct dentry *dir, struct qstr *name)
256 struct nfs_fattr dir_attr;
257 struct nfs3_diropargs arg = { NFS_FH(dir), name->name, name->len };
258 struct rpc_message msg = { NFS3PROC_REMOVE, &arg, &dir_attr, NULL };
259 int status;
261 dprintk("NFS call remove %s\n", name->name);
262 dir_attr.valid = 0;
263 status = rpc_call_sync(NFS_CLIENT(dir->d_inode), &msg, 0);
264 nfs_refresh_inode(dir->d_inode, &dir_attr);
265 dprintk("NFS reply remove: %d\n", status);
266 return status;
269 static int
270 nfs3_proc_rename(struct dentry *old_dir, struct qstr *old_name,
271 struct dentry *new_dir, struct qstr *new_name)
273 struct nfs_fattr old_dir_attr, new_dir_attr;
274 struct nfs3_renameargs arg = { NFS_FH(old_dir),
275 old_name->name, old_name->len,
276 NFS_FH(new_dir),
277 new_name->name, new_name->len };
278 struct nfs3_renameres res = { &old_dir_attr, &new_dir_attr };
279 int status;
281 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
282 old_dir_attr.valid = 0;
283 new_dir_attr.valid = 0;
284 status = rpc_call(NFS_CLIENT(old_dir->d_inode), NFS3PROC_RENAME, &arg, &res, 0);
285 nfs_refresh_inode(old_dir->d_inode, &old_dir_attr);
286 nfs_refresh_inode(new_dir->d_inode, &new_dir_attr);
287 dprintk("NFS reply rename: %d\n", status);
288 return status;
291 static int
292 nfs3_proc_link(struct dentry *dentry, struct dentry *dir, struct qstr *name)
294 struct nfs_fattr dir_attr, fattr;
295 struct nfs3_linkargs arg = { NFS_FH(dentry), NFS_FH(dir),
296 name->name, name->len };
297 struct nfs3_linkres res = { &dir_attr, &fattr };
298 int status;
300 dprintk("NFS call link %s\n", name->name);
301 dir_attr.valid = 0;
302 fattr.valid = 0;
303 status = rpc_call(NFS_CLIENT(dentry->d_inode), NFS3PROC_LINK, &arg, &res, 0);
304 nfs_refresh_inode(dir->d_inode, &dir_attr);
305 nfs_refresh_inode(dentry->d_inode, &fattr);
306 dprintk("NFS reply link: %d\n", status);
307 return status;
310 static int
311 nfs3_proc_symlink(struct dentry *dir, struct qstr *name, struct qstr *path,
312 struct iattr *sattr, struct nfs_fh *fhandle,
313 struct nfs_fattr *fattr)
315 struct nfs_fattr dir_attr;
316 struct nfs3_symlinkargs arg = { NFS_FH(dir), name->name, name->len,
317 path->name, path->len, sattr };
318 struct nfs3_diropres res = { &dir_attr, fhandle, fattr };
319 int status;
321 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
322 dir_attr.valid = 0;
323 fattr->valid = 0;
324 status = rpc_call(NFS_CLIENT(dir->d_inode), NFS3PROC_SYMLINK, &arg, &res, 0);
325 nfs_refresh_inode(dir->d_inode, &dir_attr);
326 dprintk("NFS reply symlink: %d\n", status);
327 return status;
330 static int
331 nfs3_proc_mkdir(struct dentry *dir, struct qstr *name, struct iattr *sattr,
332 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
334 struct nfs_fattr dir_attr;
335 struct nfs3_createargs arg = { NFS_FH(dir), name->name, name->len,
336 sattr, 0, { 0, 0 } };
337 struct nfs3_diropres res = { &dir_attr, fhandle, fattr };
338 int status;
340 dprintk("NFS call mkdir %s\n", name->name);
341 dir_attr.valid = 0;
342 fattr->valid = 0;
343 status = rpc_call(NFS_CLIENT(dir->d_inode), NFS3PROC_MKDIR, &arg, &res, 0);
344 nfs_refresh_inode(dir->d_inode, &dir_attr);
345 dprintk("NFS reply mkdir: %d\n", status);
346 return status;
349 static int
350 nfs3_proc_rmdir(struct dentry *dir, struct qstr *name)
352 struct nfs_fattr dir_attr;
353 struct nfs3_diropargs arg = { NFS_FH(dir), name->name, name->len };
354 int status;
356 dprintk("NFS call rmdir %s\n", name->name);
357 dir_attr.valid = 0;
358 status = rpc_call(NFS_CLIENT(dir->d_inode), NFS3PROC_RMDIR, &arg, &dir_attr, 0);
359 nfs_refresh_inode(dir->d_inode, &dir_attr);
360 dprintk("NFS reply rmdir: %d\n", status);
361 return status;
365 * The READDIR implementation is somewhat hackish - we pass the user buffer
366 * to the encode function, which installs it in the receive iovec.
367 * The decode function itself doesn't perform any decoding, it just makes
368 * sure the reply is syntactically correct.
370 * Also note that this implementation handles both plain readdir and
371 * readdirplus.
373 static int
374 nfs3_proc_readdir(struct file *file, u64 cookie, void *entry,
375 unsigned int size, int plus)
377 struct dentry *dir = file->f_dentry;
378 struct rpc_cred *cred = nfs_file_cred(file);
379 struct nfs_fattr dir_attr;
380 struct nfs3_readdirargs arg = { NFS_FH(dir), cookie, {0, 0}, 0, 0, 0 };
381 struct nfs3_readdirres res = { &dir_attr, 0, 0, 0, 0 };
382 struct rpc_message msg = { NFS3PROC_READDIR, &arg, &res, cred };
383 u32 *verf = NFS_COOKIEVERF(dir->d_inode);
384 int status;
386 arg.buffer = entry;
387 arg.bufsiz = size;
388 arg.verf[0] = verf[0];
389 arg.verf[1] = verf[1];
390 arg.plus = plus;
391 res.buffer = entry;
392 res.bufsiz = size;
393 res.verf = verf;
394 res.plus = plus;
396 if (plus)
397 msg.rpc_proc = NFS3PROC_READDIRPLUS;
399 dprintk("NFS call readdir%s %d\n",
400 plus? "plus" : "", (unsigned int) cookie);
402 dir_attr.valid = 0;
403 status = rpc_call_sync(NFS_CLIENT(dir->d_inode), &msg, 0);
404 nfs_refresh_inode(dir->d_inode, &dir_attr);
405 dprintk("NFS reply readdir: %d\n", status);
406 return status;
409 static int
410 nfs3_proc_mknod(struct dentry *dir, struct qstr *name, struct iattr *sattr,
411 dev_t rdev, struct nfs_fh *fh, struct nfs_fattr *fattr)
413 struct nfs_fattr dir_attr;
414 struct nfs3_mknodargs arg = { NFS_FH(dir), name->name, name->len, 0,
415 sattr, rdev };
416 struct nfs3_diropres res = { &dir_attr, fh, fattr };
417 int status;
419 switch (sattr->ia_mode & S_IFMT) {
420 case S_IFBLK: arg.type = NF3BLK; break;
421 case S_IFCHR: arg.type = NF3CHR; break;
422 case S_IFIFO: arg.type = NF3FIFO; break;
423 case S_IFSOCK: arg.type = NF3SOCK; break;
424 default: return -EINVAL;
427 dprintk("NFS call mknod %s %x\n", name->name, rdev);
428 dir_attr.valid = 0;
429 fattr->valid = 0;
430 status = rpc_call(NFS_CLIENT(dir->d_inode), NFS3PROC_MKNOD, &arg, &res, 0);
431 nfs_refresh_inode(dir->d_inode, &dir_attr);
432 dprintk("NFS reply mknod: %d\n", status);
433 return status;
437 * This is a combo call of fsstat and fsinfo
439 static int
440 nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
441 struct nfs_fsinfo *info)
443 int status;
445 dprintk("NFS call fsstat\n");
446 memset((char *)info, 0, sizeof(*info));
447 status = rpc_call(server->client, NFS3PROC_FSSTAT, fhandle, info, 0);
448 if (status < 0)
449 goto error;
450 status = rpc_call(server->client, NFS3PROC_FSINFO, fhandle, info, 0);
452 error:
453 dprintk("NFS reply statfs: %d\n", status);
454 return status;
457 extern u32 *nfs3_decode_dirent(u32 *, struct nfs_entry *, int);
459 struct nfs_rpc_ops nfs_v3_clientops = {
460 3, /* protocol version */
461 nfs3_proc_get_root,
462 nfs3_proc_getattr,
463 nfs3_proc_setattr,
464 nfs3_proc_lookup,
465 nfs3_proc_access,
466 nfs3_proc_readlink,
467 nfs3_proc_read,
468 nfs3_proc_write,
469 NULL, /* commit */
470 nfs3_proc_create,
471 nfs3_proc_remove,
472 nfs3_proc_rename,
473 nfs3_proc_link,
474 nfs3_proc_symlink,
475 nfs3_proc_mkdir,
476 nfs3_proc_rmdir,
477 nfs3_proc_readdir,
478 nfs3_proc_mknod,
479 nfs3_proc_statfs,
480 nfs3_decode_dirent,