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