File: /usr/src/linux/fs/nfs/proc.c

1     /*
2      *  linux/fs/nfs/proc.c
3      *
4      *  Copyright (C) 1992, 1993, 1994  Rick Sladkey
5      *
6      *  OS-independent nfs remote procedure call functions
7      *
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.
11      *
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.
15      *
16      *  Also, the code currently doesn't check the size of the packet, when
17      *  it decodes the packet.
18      *
19      *  Feel free to fix it and mail me the diffs if it worries you.
20      *
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
24      *
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).
28      */
29     
30     #include <linux/types.h>
31     #include <linux/param.h>
32     #include <linux/slab.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>
44     
45     #define NFSDBG_FACILITY		NFSDBG_PROC
46     
47     /*
48      * Bare-bones access to getattr: this is for nfs_read_super.
49      */
50     static int
51     nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
52     		  struct nfs_fattr *fattr)
53     {
54     	int		status;
55     
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;
61     }
62     
63     /*
64      * One function for each procedure in the NFS protocol.
65      */
66     static int
67     nfs_proc_getattr(struct inode *inode, struct nfs_fattr *fattr)
68     {
69     	int	status;
70     
71     	dprintk("NFS call  getattr\n");
72     	fattr->valid = 0;
73     	status = rpc_call(NFS_CLIENT(inode), NFSPROC_GETATTR,
74     				NFS_FH(inode), fattr, 0);
75     	dprintk("NFS reply getattr\n");
76     	return status;
77     }
78     
79     static int
80     nfs_proc_setattr(struct inode *inode, struct nfs_fattr *fattr,
81     		 struct iattr *sattr)
82     {
83     	struct nfs_sattrargs	arg = { NFS_FH(inode), sattr };
84     	int	status;
85     
86     	dprintk("NFS call  setattr\n");
87     	fattr->valid = 0;
88     	status = rpc_call(NFS_CLIENT(inode), NFSPROC_SETATTR, &arg, fattr, 0);
89     	dprintk("NFS reply setattr\n");
90     	return status;
91     }
92     
93     static int
94     nfs_proc_lookup(struct inode *dir, struct qstr *name,
95     		struct nfs_fh *fhandle, struct nfs_fattr *fattr)
96     {
97     	struct nfs_diropargs	arg = { NFS_FH(dir), name->name, name->len };
98     	struct nfs_diropok	res = { fhandle, fattr };
99     	int			status;
100     
101     	dprintk("NFS call  lookup %s\n", name->name);
102     	fattr->valid = 0;
103     	status = rpc_call(NFS_CLIENT(dir), NFSPROC_LOOKUP, &arg, &res, 0);
104     	dprintk("NFS reply lookup: %d\n", status);
105     	return status;
106     }
107     
108     static int
109     nfs_proc_readlink(struct inode *inode, void *buffer, unsigned int bufsiz)
110     {
111     	struct nfs_readlinkargs	args = { NFS_FH(inode), buffer, bufsiz };
112     	struct nfs_readlinkres	res = { buffer, bufsiz };
113     	int			status;
114     
115     	dprintk("NFS call  readlink\n");
116     	status = rpc_call(NFS_CLIENT(inode), NFSPROC_READLINK,
117     					&args, &res, 0);
118     	dprintk("NFS reply readlink: %d\n", status);
119     	return status;
120     }
121     
122     static int
123     nfs_proc_read(struct inode *inode, struct rpc_cred *cred,
124     	      struct nfs_fattr *fattr, int flags,
125     	      loff_t offset, unsigned int count, void *buffer, int *eofp)
126     {
127     	struct nfs_readargs	arg = { NFS_FH(inode), offset, count, 1,
128     				       {{ buffer, count }, {0,0}, {0,0}, {0,0},
129     					{0,0}, {0,0}, {0,0}, {0,0}} };
130     	struct nfs_readres	res = { fattr, count, 0};
131     	struct rpc_message	msg = { NFSPROC_READ, &arg, &res, cred };
132     	int			status;
133     
134     	dprintk("NFS call  read %d @ %Ld\n", count, (long long)offset);
135     	fattr->valid = 0;
136     	status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
137     
138     	dprintk("NFS reply read: %d\n", status);
139     	*eofp = res.eof;
140     	return status;
141     }
142     
143     static int
144     nfs_proc_write(struct inode *inode, struct rpc_cred *cred,
145     	       struct nfs_fattr *fattr, int how,
146     	       loff_t offset, unsigned int count,
147     	       void *buffer, struct nfs_writeverf *verf)
148     {
149     	struct nfs_writeargs	arg = {NFS_FH(inode), offset, count,
150     					NFS_FILE_SYNC, 1,
151     					{{buffer, count}, {0,0}, {0,0}, {0,0},
152     					 {0,0}, {0,0}, {0,0}, {0,0}}};
153     	struct nfs_writeres     res = {fattr, verf, count};
154     	struct rpc_message	msg = { NFSPROC_WRITE, &arg, &res, cred };
155     	int			status, flags = 0;
156     
157     	dprintk("NFS call  write %d @ %Ld\n", count, (long long)offset);
158     	fattr->valid = 0;
159     	if (how & NFS_RW_SWAP)
160     		flags |= NFS_RPC_SWAPFLAGS;
161     	status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
162     
163     	dprintk("NFS reply write: %d\n", status);
164     	verf->committed = NFS_FILE_SYNC;      /* NFSv2 always syncs data */
165     	return status < 0? status : count;
166     }
167     
168     static int
169     nfs_proc_create(struct inode *dir, struct qstr *name, struct iattr *sattr,
170     		int flags, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
171     {
172     	struct nfs_createargs	arg = { NFS_FH(dir), name->name,
173     					name->len, sattr };
174     	struct nfs_diropok	res = { fhandle, fattr };
175     	int			status;
176     
177     	fattr->valid = 0;
178     	dprintk("NFS call  create %s\n", name->name);
179     	status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
180     	dprintk("NFS reply create: %d\n", status);
181     	return status;
182     }
183     
184     /*
185      * In NFSv2, mknod is grafted onto the create call.
186      */
187     static int
188     nfs_proc_mknod(struct inode *dir, struct qstr *name, struct iattr *sattr,
189     	       dev_t rdev, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
190     {
191     	struct nfs_createargs	arg = { NFS_FH(dir), name->name,
192     						     name->len, sattr };
193     	struct nfs_diropok	res = { fhandle, fattr };
194     	int			status, mode;
195     
196     	dprintk("NFS call  mknod %s\n", name->name);
197     
198     	mode = sattr->ia_mode;
199     	if (S_ISFIFO(mode)) {
200     		sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
201     		sattr->ia_valid &= ~ATTR_SIZE;
202     	} else if (S_ISCHR(mode) || S_ISBLK(mode)) {
203     		sattr->ia_valid |= ATTR_SIZE;
204     		sattr->ia_size   = rdev;	/* get out your barf bag */
205     	}
206     
207     	fattr->valid = 0;
208     	status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
209     
210     	if (status == -EINVAL && S_ISFIFO(mode)) {
211     		sattr->ia_mode = mode;
212     		fattr->valid = 0;
213     		status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
214     	}
215     	dprintk("NFS reply mknod: %d\n", status);
216     	return status;
217     }
218       
219     static int
220     nfs_proc_remove(struct inode *dir, struct qstr *name)
221     {
222     	struct nfs_diropargs	arg = { NFS_FH(dir), name->name, name->len };
223     	struct rpc_message	msg = { NFSPROC_REMOVE, &arg, NULL, NULL };
224     	int			status;
225     
226     	dprintk("NFS call  remove %s\n", name->name);
227     	status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
228     
229     	dprintk("NFS reply remove: %d\n", status);
230     	return status;
231     }
232     
233     static int
234     nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
235     {
236     	struct nfs_diropargs	*arg;
237     
238     	arg = (struct nfs_diropargs *)kmalloc(sizeof(*arg), GFP_KERNEL);
239     	if (!arg)
240     		return -ENOMEM;
241     	arg->fh = NFS_FH(dir->d_inode);
242     	arg->name = name->name;
243     	arg->len = name->len;
244     	msg->rpc_proc = NFSPROC_REMOVE;
245     	msg->rpc_argp = arg;
246     	return 0;
247     }
248     
249     static void
250     nfs_proc_unlink_done(struct dentry *dir, struct rpc_message *msg)
251     {
252     	if (msg->rpc_argp) {
253     		NFS_CACHEINV(dir->d_inode);
254     		kfree(msg->rpc_argp);
255     	}
256     }
257     
258     static int
259     nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
260     		struct inode *new_dir, struct qstr *new_name)
261     {
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;
267     
268     	dprintk("NFS call  rename %s -> %s\n", old_name->name, new_name->name);
269     	status = rpc_call(NFS_CLIENT(old_dir), NFSPROC_RENAME, &arg, NULL, 0);
270     	dprintk("NFS reply rename: %d\n", status);
271     	return status;
272     }
273     
274     static int
275     nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
276     {
277     	struct nfs_linkargs	arg = { NFS_FH(inode), NFS_FH(dir),
278     					name->name, name->len };
279     	int			status;
280     
281     	dprintk("NFS call  link %s\n", name->name);
282     	status = rpc_call(NFS_CLIENT(inode), NFSPROC_LINK, &arg, NULL, 0);
283     	dprintk("NFS reply link: %d\n", status);
284     	return status;
285     }
286     
287     static int
288     nfs_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
289     		 struct iattr *sattr, struct nfs_fh *fhandle,
290     		 struct nfs_fattr *fattr)
291     {
292     	struct nfs_symlinkargs	arg = { NFS_FH(dir), name->name, name->len,
293     					path->name, path->len, sattr };
294     	int			status;
295     
296     	dprintk("NFS call  symlink %s -> %s\n", name->name, path->name);
297     	fattr->valid = 0;
298     	status = rpc_call(NFS_CLIENT(dir), NFSPROC_SYMLINK, &arg, NULL, 0);
299     	dprintk("NFS reply symlink: %d\n", status);
300     	return status;
301     }
302     
303     static int
304     nfs_proc_mkdir(struct inode *dir, struct qstr *name, struct iattr *sattr,
305     	       struct nfs_fh *fhandle, struct nfs_fattr *fattr)
306     {
307     	struct nfs_createargs	arg = { NFS_FH(dir), name->name, name->len,
308     					sattr };
309     	struct nfs_diropok	res = { fhandle, fattr };
310     	int			status;
311     
312     	dprintk("NFS call  mkdir %s\n", name->name);
313     	fattr->valid = 0;
314     	status = rpc_call(NFS_CLIENT(dir), NFSPROC_MKDIR, &arg, &res, 0);
315     	dprintk("NFS reply mkdir: %d\n", status);
316     	return status;
317     }
318     
319     static int
320     nfs_proc_rmdir(struct inode *dir, struct qstr *name)
321     {
322     	struct nfs_diropargs	arg = { NFS_FH(dir), name->name, name->len };
323     	int			status;
324     
325     	dprintk("NFS call  rmdir %s\n", name->name);
326     	status = rpc_call(NFS_CLIENT(dir), NFSPROC_RMDIR, &arg, NULL, 0);
327     	dprintk("NFS reply rmdir: %d\n", status);
328     	return status;
329     }
330     
331     /*
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.
337      */
338     static int
339     nfs_proc_readdir(struct inode *dir, struct rpc_cred *cred,
340     		 __u64 cookie, void *entry, 
341     		 unsigned int size, int plus)
342     {
343     	struct nfs_readdirargs	arg;
344     	struct nfs_readdirres	res;
345     	struct rpc_message	msg = { NFSPROC_READDIR, &arg, &res, cred };
346     	int			status;
347     
348     	arg.fh = NFS_FH(dir);
349     	arg.cookie = cookie;
350     	arg.buffer = entry;
351     	arg.bufsiz = size;
352     	res.buffer = entry;
353     	res.bufsiz = size;
354     
355     	dprintk("NFS call  readdir %d\n", (unsigned int)cookie);
356     	status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
357     
358     	dprintk("NFS reply readdir: %d\n", status);
359     	return status;
360     }
361     
362     static int
363     nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
364     			struct nfs_fsinfo *info)
365     {
366     	int	status;
367     
368     	dprintk("NFS call  statfs\n");
369     	memset((char *)info, 0, sizeof(*info));
370     	status = rpc_call(server->client, NFSPROC_STATFS, fhandle, info, 0);
371     	dprintk("NFS reply statfs: %d\n", status);
372     	return status;
373     }
374     
375     extern u32 * nfs_decode_dirent(u32 *, struct nfs_entry *, int);
376     
377     struct nfs_rpc_ops     nfs_v2_clientops = {
378            2,		       /* protocol version */
379            nfs_proc_get_root,
380            nfs_proc_getattr,
381            nfs_proc_setattr,
382            nfs_proc_lookup,
383            NULL,		       /* access */
384            nfs_proc_readlink,
385            nfs_proc_read,
386            nfs_proc_write,
387            NULL,		       /* commit */
388            nfs_proc_create,
389            nfs_proc_remove,
390            nfs_proc_unlink_setup,
391            nfs_proc_unlink_done,
392            nfs_proc_rename,
393            nfs_proc_link,
394            nfs_proc_symlink,
395            nfs_proc_mkdir,
396            nfs_proc_rmdir,
397            nfs_proc_readdir,
398            nfs_proc_mknod,
399            nfs_proc_statfs,
400            nfs_decode_dirent,
401     };
402