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
nfs_proc_get_root(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr)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
nfs_proc_getattr(struct inode * inode,struct nfs_fattr * fattr)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
nfs_proc_setattr(struct inode * inode,struct nfs_fattr * fattr,struct iattr * sattr)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
nfs_proc_lookup(struct inode * dir,struct qstr * name,struct nfs_fh * fhandle,struct nfs_fattr * fattr)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
nfs_proc_readlink(struct inode * inode,struct page * page)109 nfs_proc_readlink(struct inode *inode, struct page *page)
110 {
111 struct nfs_readlinkargs args = { NFS_FH(inode), PAGE_CACHE_SIZE, &page };
112 int status;
113
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);
117 return status;
118 }
119
120 static int
nfs_proc_read(struct inode * inode,struct rpc_cred * cred,struct nfs_fattr * fattr,int flags,unsigned int base,unsigned int count,struct page * page,int * eofp)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)
125 {
126 u64 offset = page_offset(page) + base;
127 struct nfs_readargs arg = { NFS_FH(inode), offset, count,
128 base, &page };
129 struct nfs_readres res = { fattr, count, 0};
130 struct rpc_message msg = { NFSPROC_READ, &arg, &res, cred };
131 int status;
132
133 dprintk("NFS call read %d @ %Ld\n", count, (long long)offset);
134 fattr->valid = 0;
135 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
136
137 dprintk("NFS reply read: %d\n", status);
138 *eofp = res.eof;
139 return status;
140 }
141
142 static int
nfs_proc_write(struct inode * inode,struct rpc_cred * cred,struct nfs_fattr * fattr,int how,unsigned int base,unsigned int count,struct page * page,struct nfs_writeverf * verf)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)
147 {
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;
154
155 dprintk("NFS call write %d @ %Ld\n", count, (long long)offset);
156 fattr->valid = 0;
157 if (how & NFS_RW_SWAP)
158 flags |= NFS_RPC_SWAPFLAGS;
159 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
160
161 dprintk("NFS reply write: %d\n", status);
162 verf->committed = NFS_FILE_SYNC; /* NFSv2 always syncs data */
163 return status < 0? status : count;
164 }
165
166 static int
nfs_proc_create(struct inode * dir,struct qstr * name,struct iattr * sattr,int flags,struct nfs_fh * fhandle,struct nfs_fattr * fattr)167 nfs_proc_create(struct inode *dir, struct qstr *name, struct iattr *sattr,
168 int flags, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
169 {
170 struct nfs_createargs arg = { NFS_FH(dir), name->name,
171 name->len, sattr };
172 struct nfs_diropok res = { fhandle, fattr };
173 int status;
174
175 fattr->valid = 0;
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);
179 return status;
180 }
181
182 /*
183 * In NFSv2, mknod is grafted onto the create call.
184 */
185 static int
nfs_proc_mknod(struct inode * dir,struct qstr * name,struct iattr * sattr,dev_t rdev,struct nfs_fh * fhandle,struct nfs_fattr * fattr)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)
188 {
189 struct nfs_createargs arg = { NFS_FH(dir), name->name,
190 name->len, sattr };
191 struct nfs_diropok res = { fhandle, fattr };
192 int status, mode;
193
194 dprintk("NFS call mknod %s\n", name->name);
195
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 */
203 }
204
205 fattr->valid = 0;
206 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
207
208 if (status == -EINVAL && S_ISFIFO(mode)) {
209 sattr->ia_mode = mode;
210 fattr->valid = 0;
211 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
212 }
213 dprintk("NFS reply mknod: %d\n", status);
214 return status;
215 }
216
217 static int
nfs_proc_remove(struct inode * dir,struct qstr * name)218 nfs_proc_remove(struct inode *dir, struct qstr *name)
219 {
220 struct nfs_diropargs arg = { NFS_FH(dir), name->name, name->len };
221 struct rpc_message msg = { NFSPROC_REMOVE, &arg, NULL, NULL };
222 int status;
223
224 dprintk("NFS call remove %s\n", name->name);
225 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
226
227 dprintk("NFS reply remove: %d\n", status);
228 return status;
229 }
230
231 static int
nfs_proc_unlink_setup(struct rpc_message * msg,struct dentry * dir,struct qstr * name)232 nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
233 {
234 struct nfs_diropargs *arg;
235
236 arg = (struct nfs_diropargs *)kmalloc(sizeof(*arg), GFP_KERNEL);
237 if (!arg)
238 return -ENOMEM;
239 arg->fh = NFS_FH(dir->d_inode);
240 arg->name = name->name;
241 arg->len = name->len;
242 msg->rpc_proc = NFSPROC_REMOVE;
243 msg->rpc_argp = arg;
244 return 0;
245 }
246
247 static void
nfs_proc_unlink_done(struct dentry * dir,struct rpc_message * msg)248 nfs_proc_unlink_done(struct dentry *dir, struct rpc_message *msg)
249 {
250 if (msg->rpc_argp) {
251 NFS_CACHEINV(dir->d_inode);
252 kfree(msg->rpc_argp);
253 }
254 }
255
256 static int
nfs_proc_rename(struct inode * old_dir,struct qstr * old_name,struct inode * new_dir,struct qstr * new_name)257 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
258 struct inode *new_dir, struct qstr *new_name)
259 {
260 struct nfs_renameargs arg = { NFS_FH(old_dir), old_name->name,
261 old_name->len,
262 NFS_FH(new_dir), new_name->name,
263 new_name->len};
264 int status;
265
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);
269 return status;
270 }
271
272 static int
nfs_proc_link(struct inode * inode,struct inode * dir,struct qstr * name)273 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
274 {
275 struct nfs_linkargs arg = { NFS_FH(inode), NFS_FH(dir),
276 name->name, name->len };
277 int status;
278
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);
282 return status;
283 }
284
285 static int
nfs_proc_symlink(struct inode * dir,struct qstr * name,struct qstr * path,struct iattr * sattr,struct nfs_fh * fhandle,struct nfs_fattr * fattr)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)
289 {
290 struct nfs_symlinkargs arg = { NFS_FH(dir), name->name, name->len,
291 path->name, path->len, sattr };
292 int status;
293
294 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
295 fattr->valid = 0;
296 status = rpc_call(NFS_CLIENT(dir), NFSPROC_SYMLINK, &arg, NULL, 0);
297 dprintk("NFS reply symlink: %d\n", status);
298 return status;
299 }
300
301 static int
nfs_proc_mkdir(struct inode * dir,struct qstr * name,struct iattr * sattr,struct nfs_fh * fhandle,struct nfs_fattr * fattr)302 nfs_proc_mkdir(struct inode *dir, struct qstr *name, struct iattr *sattr,
303 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
304 {
305 struct nfs_createargs arg = { NFS_FH(dir), name->name, name->len,
306 sattr };
307 struct nfs_diropok res = { fhandle, fattr };
308 int status;
309
310 dprintk("NFS call mkdir %s\n", name->name);
311 fattr->valid = 0;
312 status = rpc_call(NFS_CLIENT(dir), NFSPROC_MKDIR, &arg, &res, 0);
313 dprintk("NFS reply mkdir: %d\n", status);
314 return status;
315 }
316
317 static int
nfs_proc_rmdir(struct inode * dir,struct qstr * name)318 nfs_proc_rmdir(struct inode *dir, struct qstr *name)
319 {
320 struct nfs_diropargs arg = { NFS_FH(dir), name->name, name->len };
321 int status;
322
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);
326 return status;
327 }
328
329 /*
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.
335 */
336 static int
nfs_proc_readdir(struct inode * dir,struct rpc_cred * cred,__u64 cookie,struct page * page,unsigned int count,int plus)337 nfs_proc_readdir(struct inode *dir, struct rpc_cred *cred,
338 __u64 cookie, struct page *page,
339 unsigned int count, int plus)
340 {
341 struct nfs_readdirargs arg = { NFS_FH(dir), cookie, count, &page };
342 struct rpc_message msg = { NFSPROC_READDIR, &arg, NULL, cred };
343 int status;
344
345 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
346 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
347
348 dprintk("NFS reply readdir: %d\n", status);
349 return status;
350 }
351
352 static int
nfs_proc_statfs(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)353 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
354 struct nfs_fsinfo *info)
355 {
356 int status;
357
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);
362 return status;
363 }
364
365 extern u32 * nfs_decode_dirent(u32 *, struct nfs_entry *, int);
366
367 struct nfs_rpc_ops nfs_v2_clientops = {
368 2, /* protocol version */
369 nfs_proc_get_root,
370 nfs_proc_getattr,
371 nfs_proc_setattr,
372 nfs_proc_lookup,
373 NULL, /* access */
374 nfs_proc_readlink,
375 nfs_proc_read,
376 nfs_proc_write,
377 NULL, /* commit */
378 nfs_proc_create,
379 nfs_proc_remove,
380 nfs_proc_unlink_setup,
381 nfs_proc_unlink_done,
382 nfs_proc_rename,
383 nfs_proc_link,
384 nfs_proc_symlink,
385 nfs_proc_mkdir,
386 nfs_proc_rmdir,
387 nfs_proc_readdir,
388 nfs_proc_mknod,
389 nfs_proc_statfs,
390 nfs_decode_dirent,
391 };
392