Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / fs / nfsd / nfsproc.c
1 /*
2  * nfsproc2.c   Process version 2 NFS requests.
3  * linux/fs/nfsd/nfs2proc.c
4  * 
5  * Process version 2 NFS requests.
6  *
7  * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
8  */
9
10 #include <linux/linkage.h>
11 #include <linux/time.h>
12 #include <linux/errno.h>
13 #include <linux/fs.h>
14 #include <linux/stat.h>
15 #include <linux/fcntl.h>
16 #include <linux/net.h>
17 #include <linux/in.h>
18 #include <linux/namei.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
21
22 #include <linux/sunrpc/svc.h>
23 #include <linux/nfsd/nfsd.h>
24 #include <linux/nfsd/cache.h>
25 #include <linux/nfsd/xdr.h>
26
27 typedef struct svc_rqst svc_rqst;
28 typedef struct svc_buf  svc_buf;
29
30 #define NFSDDBG_FACILITY                NFSDDBG_PROC
31
32
33 static int
34 nfsd_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
35 {
36         return nfs_ok;
37 }
38
39 /*
40  * Get a file's attributes
41  * N.B. After this call resp->fh needs an fh_put
42  */
43 static int
44 nfsd_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle  *argp,
45                                           struct nfsd_attrstat *resp)
46 {
47         dprintk("nfsd: GETATTR  %s\n", SVCFH_fmt(&argp->fh));
48
49         fh_copy(&resp->fh, &argp->fh);
50         return fh_verify(rqstp, &resp->fh, 0, MAY_NOP);
51 }
52
53 /*
54  * Set a file's attributes
55  * N.B. After this call resp->fh needs an fh_put
56  */
57 static int
58 nfsd_proc_setattr(struct svc_rqst *rqstp, struct nfsd_sattrargs *argp,
59                                           struct nfsd_attrstat  *resp)
60 {
61         dprintk("nfsd: SETATTR  %s, valid=%x, size=%ld\n",
62                 SVCFH_fmt(&argp->fh),
63                 argp->attrs.ia_valid, (long) argp->attrs.ia_size);
64
65         fh_copy(&resp->fh, &argp->fh);
66         return nfsd_setattr(rqstp, &resp->fh, &argp->attrs,0, (time_t)0);
67 }
68
69 /*
70  * Look up a path name component
71  * Note: the dentry in the resp->fh may be negative if the file
72  * doesn't exist yet.
73  * N.B. After this call resp->fh needs an fh_put
74  */
75 static int
76 nfsd_proc_lookup(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
77                                          struct nfsd_diropres  *resp)
78 {
79         int     nfserr;
80
81         dprintk("nfsd: LOOKUP   %s %.*s\n",
82                 SVCFH_fmt(&argp->fh), argp->len, argp->name);
83
84         fh_init(&resp->fh, NFS_FHSIZE);
85         nfserr = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
86                                  &resp->fh);
87
88         fh_put(&argp->fh);
89         return nfserr;
90 }
91
92 /*
93  * Read a symlink.
94  */
95 static int
96 nfsd_proc_readlink(struct svc_rqst *rqstp, struct nfsd_readlinkargs *argp,
97                                            struct nfsd_readlinkres *resp)
98 {
99         int     nfserr;
100
101         dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
102
103         /* Read the symlink. */
104         resp->len = NFS_MAXPATHLEN;
105         nfserr = nfsd_readlink(rqstp, &argp->fh, argp->buffer, &resp->len);
106
107         fh_put(&argp->fh);
108         return nfserr;
109 }
110
111 /*
112  * Read a portion of a file.
113  * N.B. After this call resp->fh needs an fh_put
114  */
115 static int
116 nfsd_proc_read(struct svc_rqst *rqstp, struct nfsd_readargs *argp,
117                                        struct nfsd_readres  *resp)
118 {
119         int     nfserr;
120
121         dprintk("nfsd: READ    %s %d bytes at %d\n",
122                 SVCFH_fmt(&argp->fh),
123                 argp->count, argp->offset);
124
125         /* Obtain buffer pointer for payload. 19 is 1 word for
126          * status, 17 words for fattr, and 1 word for the byte count.
127          */
128
129         if (NFSSVC_MAXBLKSIZE < argp->count) {
130                 printk(KERN_NOTICE
131                         "oversized read request from %u.%u.%u.%u:%d (%d bytes)\n",
132                                 NIPQUAD(rqstp->rq_addr.sin_addr.s_addr),
133                                 ntohs(rqstp->rq_addr.sin_port),
134                                 argp->count);
135                 argp->count = NFSSVC_MAXBLKSIZE;
136         }
137         svc_reserve(rqstp, (19<<2) + argp->count + 4);
138
139         resp->count = argp->count;
140         nfserr = nfsd_read(rqstp, fh_copy(&resp->fh, &argp->fh), NULL,
141                                   argp->offset,
142                                   argp->vec, argp->vlen,
143                                   &resp->count);
144
145         return nfserr;
146 }
147
148 /*
149  * Write data to a file
150  * N.B. After this call resp->fh needs an fh_put
151  */
152 static int
153 nfsd_proc_write(struct svc_rqst *rqstp, struct nfsd_writeargs *argp,
154                                         struct nfsd_attrstat  *resp)
155 {
156         int     nfserr;
157         int     stable = 1;
158
159         dprintk("nfsd: WRITE    %s %d bytes at %d\n",
160                 SVCFH_fmt(&argp->fh),
161                 argp->len, argp->offset);
162
163         nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh), NULL,
164                                    argp->offset,
165                                    argp->vec, argp->vlen,
166                                    argp->len,
167                                    &stable);
168         return nfserr;
169 }
170
171 /*
172  * CREATE processing is complicated. The keyword here is `overloaded.'
173  * The parent directory is kept locked between the check for existence
174  * and the actual create() call in compliance with VFS protocols.
175  * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
176  */
177 static int
178 nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
179                                          struct nfsd_diropres   *resp)
180 {
181         svc_fh          *dirfhp = &argp->fh;
182         svc_fh          *newfhp = &resp->fh;
183         struct iattr    *attr = &argp->attrs;
184         struct inode    *inode;
185         struct dentry   *dchild;
186         int             nfserr, type, mode;
187         dev_t           rdev = 0, wanted = new_decode_dev(attr->ia_size);
188
189         dprintk("nfsd: CREATE   %s %.*s\n",
190                 SVCFH_fmt(dirfhp), argp->len, argp->name);
191
192         /* First verify the parent file handle */
193         nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, MAY_EXEC);
194         if (nfserr)
195                 goto done; /* must fh_put dirfhp even on error */
196
197         /* Check for MAY_WRITE in nfsd_create if necessary */
198
199         nfserr = nfserr_acces;
200         if (!argp->len)
201                 goto done;
202         nfserr = nfserr_exist;
203         if (isdotent(argp->name, argp->len))
204                 goto done;
205         fh_lock(dirfhp);
206         dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
207         if (IS_ERR(dchild)) {
208                 nfserr = nfserrno(PTR_ERR(dchild));
209                 goto out_unlock;
210         }
211         fh_init(newfhp, NFS_FHSIZE);
212         nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
213         if (!nfserr && !dchild->d_inode)
214                 nfserr = nfserr_noent;
215         dput(dchild);
216         if (nfserr) {
217                 if (nfserr != nfserr_noent)
218                         goto out_unlock;
219                 /*
220                  * If the new file handle wasn't verified, we can't tell
221                  * whether the file exists or not. Time to bail ...
222                  */
223                 nfserr = nfserr_acces;
224                 if (!newfhp->fh_dentry) {
225                         printk(KERN_WARNING 
226                                 "nfsd_proc_create: file handle not verified\n");
227                         goto out_unlock;
228                 }
229         }
230
231         inode = newfhp->fh_dentry->d_inode;
232
233         /* Unfudge the mode bits */
234         if (attr->ia_valid & ATTR_MODE) {
235                 type = attr->ia_mode & S_IFMT;
236                 mode = attr->ia_mode & ~S_IFMT;
237                 if (!type) {
238                         /* no type, so if target exists, assume same as that,
239                          * else assume a file */
240                         if (inode) {
241                                 type = inode->i_mode & S_IFMT;
242                                 switch(type) {
243                                 case S_IFCHR:
244                                 case S_IFBLK:
245                                         /* reserve rdev for later checking */
246                                         rdev = inode->i_rdev;
247                                         attr->ia_valid |= ATTR_SIZE;
248
249                                         /* FALLTHROUGH */
250                                 case S_IFIFO:
251                                         /* this is probably a permission check..
252                                          * at least IRIX implements perm checking on
253                                          *   echo thing > device-special-file-or-pipe
254                                          * by doing a CREATE with type==0
255                                          */
256                                         nfserr = nfsd_permission(newfhp->fh_export,
257                                                                  newfhp->fh_dentry,
258                                                                  MAY_WRITE|MAY_LOCAL_ACCESS);
259                                         if (nfserr && nfserr != nfserr_rofs)
260                                                 goto out_unlock;
261                                 }
262                         } else
263                                 type = S_IFREG;
264                 }
265         } else if (inode) {
266                 type = inode->i_mode & S_IFMT;
267                 mode = inode->i_mode & ~S_IFMT;
268         } else {
269                 type = S_IFREG;
270                 mode = 0;       /* ??? */
271         }
272
273         attr->ia_valid |= ATTR_MODE;
274         attr->ia_mode = mode;
275
276         /* Special treatment for non-regular files according to the
277          * gospel of sun micro
278          */
279         if (type != S_IFREG) {
280                 int     is_borc = 0;
281                 if (type != S_IFBLK && type != S_IFCHR) {
282                         rdev = 0;
283                 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
284                         /* If you think you've seen the worst, grok this. */
285                         type = S_IFIFO;
286                 } else {
287                         /* Okay, char or block special */
288                         is_borc = 1;
289                         if (!rdev)
290                                 rdev = wanted;
291                 }
292
293                 /* we've used the SIZE information, so discard it */
294                 attr->ia_valid &= ~ATTR_SIZE;
295
296                 /* Make sure the type and device matches */
297                 nfserr = nfserr_exist;
298                 if (inode && type != (inode->i_mode & S_IFMT))
299                         goto out_unlock;
300         }
301
302         nfserr = 0;
303         if (!inode) {
304                 /* File doesn't exist. Create it and set attrs */
305                 nfserr = nfsd_create(rqstp, dirfhp, argp->name, argp->len,
306                                         attr, type, rdev, newfhp);
307         } else if (type == S_IFREG) {
308                 dprintk("nfsd:   existing %s, valid=%x, size=%ld\n",
309                         argp->name, attr->ia_valid, (long) attr->ia_size);
310                 /* File already exists. We ignore all attributes except
311                  * size, so that creat() behaves exactly like
312                  * open(..., O_CREAT|O_TRUNC|O_WRONLY).
313                  */
314                 attr->ia_valid &= ATTR_SIZE;
315                 if (attr->ia_valid)
316                         nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
317         }
318
319 out_unlock:
320         /* We don't really need to unlock, as fh_put does it. */
321         fh_unlock(dirfhp);
322
323 done:
324         fh_put(dirfhp);
325         return nfserr;
326 }
327
328 static int
329 nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
330                                          void                  *resp)
331 {
332         int     nfserr;
333
334         dprintk("nfsd: REMOVE   %s %.*s\n", SVCFH_fmt(&argp->fh),
335                 argp->len, argp->name);
336
337         /* Unlink. -SIFDIR means file must not be a directory */
338         nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
339         fh_put(&argp->fh);
340         return nfserr;
341 }
342
343 static int
344 nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp,
345                                          void                   *resp)
346 {
347         int     nfserr;
348
349         dprintk("nfsd: RENAME   %s %.*s -> \n",
350                 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
351         dprintk("nfsd:        ->  %s %.*s\n",
352                 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
353
354         nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
355                                     &argp->tfh, argp->tname, argp->tlen);
356         fh_put(&argp->ffh);
357         fh_put(&argp->tfh);
358         return nfserr;
359 }
360
361 static int
362 nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp,
363                                 void                        *resp)
364 {
365         int     nfserr;
366
367         dprintk("nfsd: LINK     %s ->\n",
368                 SVCFH_fmt(&argp->ffh));
369         dprintk("nfsd:    %s %.*s\n",
370                 SVCFH_fmt(&argp->tfh),
371                 argp->tlen,
372                 argp->tname);
373
374         nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
375                                   &argp->ffh);
376         fh_put(&argp->ffh);
377         fh_put(&argp->tfh);
378         return nfserr;
379 }
380
381 static int
382 nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp,
383                                           void                    *resp)
384 {
385         struct svc_fh   newfh;
386         int             nfserr;
387
388         dprintk("nfsd: SYMLINK  %s %.*s -> %.*s\n",
389                 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
390                 argp->tlen, argp->tname);
391
392         fh_init(&newfh, NFS_FHSIZE);
393         /*
394          * Create the link, look up new file and set attrs.
395          */
396         nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
397                                                  argp->tname, argp->tlen,
398                                                  &newfh, &argp->attrs);
399
400
401         fh_put(&argp->ffh);
402         fh_put(&newfh);
403         return nfserr;
404 }
405
406 /*
407  * Make directory. This operation is not idempotent.
408  * N.B. After this call resp->fh needs an fh_put
409  */
410 static int
411 nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
412                                         struct nfsd_diropres   *resp)
413 {
414         int     nfserr;
415
416         dprintk("nfsd: MKDIR    %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
417
418         if (resp->fh.fh_dentry) {
419                 printk(KERN_WARNING
420                         "nfsd_proc_mkdir: response already verified??\n");
421         }
422
423         argp->attrs.ia_valid &= ~ATTR_SIZE;
424         fh_init(&resp->fh, NFS_FHSIZE);
425         nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
426                                     &argp->attrs, S_IFDIR, 0, &resp->fh);
427         fh_put(&argp->fh);
428         return nfserr;
429 }
430
431 /*
432  * Remove a directory
433  */
434 static int
435 nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
436                                         void                  *resp)
437 {
438         int     nfserr;
439
440         dprintk("nfsd: RMDIR    %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
441
442         nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
443         fh_put(&argp->fh);
444         return nfserr;
445 }
446
447 /*
448  * Read a portion of a directory.
449  */
450 static int
451 nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp,
452                                           struct nfsd_readdirres  *resp)
453 {
454         int             nfserr, count;
455         loff_t          offset;
456
457         dprintk("nfsd: READDIR  %s %d bytes at %d\n",
458                 SVCFH_fmt(&argp->fh),           
459                 argp->count, argp->cookie);
460
461         /* Shrink to the client read size */
462         count = (argp->count >> 2) - 2;
463
464         /* Make sure we've room for the NULL ptr & eof flag */
465         count -= 2;
466         if (count < 0)
467                 count = 0;
468
469         resp->buffer = argp->buffer;
470         resp->offset = NULL;
471         resp->buflen = count;
472         resp->common.err = nfs_ok;
473         /* Read directory and encode entries on the fly */
474         offset = argp->cookie;
475         nfserr = nfsd_readdir(rqstp, &argp->fh, &offset, 
476                               &resp->common, nfssvc_encode_entry);
477
478         resp->count = resp->buffer - argp->buffer;
479         if (resp->offset)
480                 *resp->offset = htonl(offset);
481
482         fh_put(&argp->fh);
483         return nfserr;
484 }
485
486 /*
487  * Get file system info
488  */
489 static int
490 nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle   *argp,
491                                           struct nfsd_statfsres *resp)
492 {
493         int     nfserr;
494
495         dprintk("nfsd: STATFS   %s\n", SVCFH_fmt(&argp->fh));
496
497         nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
498         fh_put(&argp->fh);
499         return nfserr;
500 }
501
502 /*
503  * NFSv2 Server procedures.
504  * Only the results of non-idempotent operations are cached.
505  */
506 #define nfsd_proc_none          NULL
507 #define nfssvc_release_none     NULL
508 struct nfsd_void { int dummy; };
509
510 #define PROC(name, argt, rest, relt, cache, respsize)   \
511  { (svc_procfunc) nfsd_proc_##name,             \
512    (kxdrproc_t) nfssvc_decode_##argt,           \
513    (kxdrproc_t) nfssvc_encode_##rest,           \
514    (kxdrproc_t) nfssvc_release_##relt,          \
515    sizeof(struct nfsd_##argt),                  \
516    sizeof(struct nfsd_##rest),                  \
517    0,                                           \
518    cache,                                       \
519    respsize,                                    \
520  }
521
522 #define ST 1            /* status */
523 #define FH 8            /* filehandle */
524 #define AT 18           /* attributes */
525
526 static struct svc_procedure             nfsd_procedures2[18] = {
527   PROC(null,     void,          void,           none,           RC_NOCACHE, ST),
528   PROC(getattr,  fhandle,       attrstat,       fhandle,        RC_NOCACHE, ST+AT),
529   PROC(setattr,  sattrargs,     attrstat,       fhandle,        RC_REPLBUFF, ST+AT),
530   PROC(none,     void,          void,           none,           RC_NOCACHE, ST),
531   PROC(lookup,   diropargs,     diropres,       fhandle,        RC_NOCACHE, ST+FH+AT),
532   PROC(readlink, readlinkargs,  readlinkres,    none,           RC_NOCACHE, ST+1+NFS_MAXPATHLEN/4),
533   PROC(read,     readargs,      readres,        fhandle,        RC_NOCACHE, ST+AT+1+NFSSVC_MAXBLKSIZE),
534   PROC(none,     void,          void,           none,           RC_NOCACHE, ST),
535   PROC(write,    writeargs,     attrstat,       fhandle,        RC_REPLBUFF, ST+AT),
536   PROC(create,   createargs,    diropres,       fhandle,        RC_REPLBUFF, ST+FH+AT),
537   PROC(remove,   diropargs,     void,           none,           RC_REPLSTAT, ST),
538   PROC(rename,   renameargs,    void,           none,           RC_REPLSTAT, ST),
539   PROC(link,     linkargs,      void,           none,           RC_REPLSTAT, ST),
540   PROC(symlink,  symlinkargs,   void,           none,           RC_REPLSTAT, ST),
541   PROC(mkdir,    createargs,    diropres,       fhandle,        RC_REPLBUFF, ST+FH+AT),
542   PROC(rmdir,    diropargs,     void,           none,           RC_REPLSTAT, ST),
543   PROC(readdir,  readdirargs,   readdirres,     none,           RC_NOCACHE, 0),
544   PROC(statfs,   fhandle,       statfsres,      none,           RC_NOCACHE, ST+5),
545 };
546
547
548 struct svc_version      nfsd_version2 = {
549                 .vs_vers        = 2,
550                 .vs_nproc       = 18,
551                 .vs_proc        = nfsd_procedures2,
552                 .vs_dispatch    = nfsd_dispatch,
553                 .vs_xdrsize     = NFS2_SVC_XDRSIZE,
554 };
555
556 /*
557  * Map errnos to NFS errnos.
558  */
559 int
560 nfserrno (int errno)
561 {
562         static struct {
563                 int     nfserr;
564                 int     syserr;
565         } nfs_errtbl[] = {
566                 { nfs_ok, 0 },
567                 { nfserr_perm, -EPERM },
568                 { nfserr_noent, -ENOENT },
569                 { nfserr_io, -EIO },
570                 { nfserr_nxio, -ENXIO },
571                 { nfserr_acces, -EACCES },
572                 { nfserr_exist, -EEXIST },
573                 { nfserr_xdev, -EXDEV },
574                 { nfserr_mlink, -EMLINK },
575                 { nfserr_nodev, -ENODEV },
576                 { nfserr_notdir, -ENOTDIR },
577                 { nfserr_isdir, -EISDIR },
578                 { nfserr_inval, -EINVAL },
579                 { nfserr_fbig, -EFBIG },
580                 { nfserr_nospc, -ENOSPC },
581                 { nfserr_rofs, -EROFS },
582                 { nfserr_mlink, -EMLINK },
583                 { nfserr_nametoolong, -ENAMETOOLONG },
584                 { nfserr_notempty, -ENOTEMPTY },
585 #ifdef EDQUOT
586                 { nfserr_dquot, -EDQUOT },
587 #endif
588                 { nfserr_stale, -ESTALE },
589                 { nfserr_jukebox, -ETIMEDOUT },
590                 { nfserr_dropit, -EAGAIN },
591                 { nfserr_dropit, -ENOMEM },
592                 { nfserr_badname, -ESRCH },
593                 { nfserr_io, -ETXTBSY },
594                 { -1, -EIO }
595         };
596         int     i;
597
598         for (i = 0; nfs_errtbl[i].nfserr != -1; i++) {
599                 if (nfs_errtbl[i].syserr == errno)
600                         return nfs_errtbl[i].nfserr;
601         }
602         printk (KERN_INFO "nfsd: non-standard errno: %d\n", errno);
603         return nfserr_io;
604 }
605