patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / nfsd / nfsctl.c
1 /*
2  * linux/fs/nfsd/nfsctl.c
3  *
4  * Syscall interface to knfsd.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #include <linux/config.h>
10 #include <linux/module.h>
11
12 #include <linux/linkage.h>
13 #include <linux/time.h>
14 #include <linux/errno.h>
15 #include <linux/fs.h>
16 #include <linux/fcntl.h>
17 #include <linux/net.h>
18 #include <linux/in.h>
19 #include <linux/syscalls.h>
20 #include <linux/unistd.h>
21 #include <linux/slab.h>
22 #include <linux/proc_fs.h>
23 #include <linux/seq_file.h>
24 #include <linux/pagemap.h>
25 #include <linux/init.h>
26
27 #include <linux/nfs.h>
28 #include <linux/nfsd_idmap.h>
29 #include <linux/sunrpc/svc.h>
30 #include <linux/nfsd/nfsd.h>
31 #include <linux/nfsd/cache.h>
32 #include <linux/nfsd/xdr.h>
33 #include <linux/nfsd/syscall.h>
34 #include <linux/nfsd/interface.h>
35
36 #include <asm/uaccess.h>
37
38 /*
39  *      We have a single directory with 8 nodes in it.
40  */
41 enum {
42         NFSD_Root = 1,
43         NFSD_Svc,
44         NFSD_Add,
45         NFSD_Del,
46         NFSD_Export,
47         NFSD_Unexport,
48         NFSD_Getfd,
49         NFSD_Getfs,
50         NFSD_List,
51         NFSD_Fh,
52         NFSD_Threads,
53 };
54
55 /*
56  * write() for these nodes.
57  */
58 static ssize_t write_svc(struct file *file, char *buf, size_t size);
59 static ssize_t write_add(struct file *file, char *buf, size_t size);
60 static ssize_t write_del(struct file *file, char *buf, size_t size);
61 static ssize_t write_export(struct file *file, char *buf, size_t size);
62 static ssize_t write_unexport(struct file *file, char *buf, size_t size);
63 static ssize_t write_getfd(struct file *file, char *buf, size_t size);
64 static ssize_t write_getfs(struct file *file, char *buf, size_t size);
65 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
66 static ssize_t write_threads(struct file *file, char *buf, size_t size);
67
68 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
69         [NFSD_Svc] = write_svc,
70         [NFSD_Add] = write_add,
71         [NFSD_Del] = write_del,
72         [NFSD_Export] = write_export,
73         [NFSD_Unexport] = write_unexport,
74         [NFSD_Getfd] = write_getfd,
75         [NFSD_Getfs] = write_getfs,
76         [NFSD_Fh] = write_filehandle,
77         [NFSD_Threads] = write_threads,
78 };
79
80 /* an argresp is stored in an allocated page and holds the 
81  * size of the argument or response, along with its content
82  */
83 struct argresp {
84         ssize_t size;
85         char data[0];
86 };
87
88 /*
89  * transaction based IO methods.
90  * The file expects a single write which triggers the transaction, and then
91  * possibly a read which collects the result - which is stored in a 
92  * file-local buffer.
93  */
94 static ssize_t TA_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
95 {
96         ino_t ino =  file->f_dentry->d_inode->i_ino;
97         struct argresp *ar;
98         ssize_t rv = 0;
99
100         if (ino >= sizeof(write_op)/sizeof(write_op[0]) || !write_op[ino])
101                 return -EINVAL;
102         if (file->private_data) 
103                 return -EINVAL; /* only one write allowed per open */
104         if (size > PAGE_SIZE - sizeof(struct argresp))
105                 return -EFBIG;
106
107         ar = kmalloc(PAGE_SIZE, GFP_KERNEL);
108         if (!ar)
109                 return -ENOMEM;
110         ar->size = 0;
111         down(&file->f_dentry->d_inode->i_sem);
112         if (file->private_data)
113                 rv = -EINVAL;
114         else
115                 file->private_data = ar;
116         up(&file->f_dentry->d_inode->i_sem);
117         if (rv) {
118                 kfree(ar);
119                 return rv;
120         }
121         if (copy_from_user(ar->data, buf, size))
122                 return -EFAULT;
123         
124         rv =  write_op[ino](file, ar->data, size);
125         if (rv>0) {
126                 ar->size = rv;
127                 rv = size;
128         }
129         return rv;
130 }
131
132
133 static ssize_t TA_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
134 {
135         struct argresp *ar;
136         ssize_t rv = 0;
137         
138         if (file->private_data == NULL)
139                 rv = TA_write(file, buf, 0, pos);
140         if (rv < 0)
141                 return rv;
142
143         ar = file->private_data;
144         if (!ar)
145                 return 0;
146         if (*pos >= ar->size)
147                 return 0;
148         if (*pos + size > ar->size)
149                 size = ar->size - *pos;
150         if (copy_to_user(buf, ar->data + *pos, size))
151                 return -EFAULT;
152         *pos += size;
153         return size;
154 }
155
156 static int TA_open(struct inode *inode, struct file *file)
157 {
158         file->private_data = NULL;
159         return 0;
160 }
161
162 static int TA_release(struct inode *inode, struct file *file)
163 {
164         void *p = file->private_data;
165         file->private_data = NULL;
166         kfree(p);
167         return 0;
168 }
169
170 static struct file_operations transaction_ops = {
171         .write          = TA_write,
172         .read           = TA_read,
173         .open           = TA_open,
174         .release        = TA_release,
175 };
176
177 extern struct seq_operations nfs_exports_op;
178 static int exports_open(struct inode *inode, struct file *file)
179 {
180         return seq_open(file, &nfs_exports_op);
181 }
182
183 static struct file_operations exports_operations = {
184         .open           = exports_open,
185         .read           = seq_read,
186         .llseek         = seq_lseek,
187         .release        = seq_release,
188 };
189
190 /*----------------------------------------------------------------------------*/
191 /*
192  * payload - write methods
193  * If the method has a response, the response should be put in buf,
194  * and the length returned.  Otherwise return 0 or and -error.
195  */
196
197 static ssize_t write_svc(struct file *file, char *buf, size_t size)
198 {
199         struct nfsctl_svc *data;
200         if (size < sizeof(*data))
201                 return -EINVAL;
202         data = (struct nfsctl_svc*) buf;
203         return nfsd_svc(data->svc_port, data->svc_nthreads);
204 }
205
206 static ssize_t write_add(struct file *file, char *buf, size_t size)
207 {
208         struct nfsctl_client *data;
209         if (size < sizeof(*data))
210                 return -EINVAL;
211         data = (struct nfsctl_client *)buf;
212         return exp_addclient(data);
213 }
214
215 static ssize_t write_del(struct file *file, char *buf, size_t size)
216 {
217         struct nfsctl_client *data;
218         if (size < sizeof(*data))
219                 return -EINVAL;
220         data = (struct nfsctl_client *)buf;
221         return exp_delclient(data);
222 }
223
224 static ssize_t write_export(struct file *file, char *buf, size_t size)
225 {
226         struct nfsctl_export *data;
227         if (size < sizeof(*data))
228                 return -EINVAL;
229         data = (struct nfsctl_export*)buf;
230         return exp_export(data);
231 }
232
233 static ssize_t write_unexport(struct file *file, char *buf, size_t size)
234 {
235         struct nfsctl_export *data;
236
237         if (size < sizeof(*data))
238                 return -EINVAL;
239         data = (struct nfsctl_export*)buf;
240         return exp_unexport(data);
241 }
242
243 static ssize_t write_getfs(struct file *file, char *buf, size_t size)
244 {
245         struct nfsctl_fsparm *data;
246         struct sockaddr_in *sin;
247         struct auth_domain *clp;
248         int err = 0;
249         struct knfsd_fh *res;
250
251         if (size < sizeof(*data))
252                 return -EINVAL;
253         data = (struct nfsctl_fsparm*)buf;
254         err = -EPROTONOSUPPORT;
255         if (data->gd_addr.sa_family != AF_INET)
256                 goto out;
257         sin = (struct sockaddr_in *)&data->gd_addr;
258         if (data->gd_maxlen > NFS3_FHSIZE)
259                 data->gd_maxlen = NFS3_FHSIZE;
260
261         res = (struct knfsd_fh*)buf;
262
263         exp_readlock();
264         if (!(clp = auth_unix_lookup(sin->sin_addr)))
265                 err = -EPERM;
266         else {
267                 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
268                 auth_domain_put(clp);
269         }
270         exp_readunlock();
271         if (err == 0)
272                 err = res->fh_size + (int)&((struct knfsd_fh*)0)->fh_base;
273  out:
274         return err;
275 }
276
277 static ssize_t write_getfd(struct file *file, char *buf, size_t size)
278 {
279         struct nfsctl_fdparm *data;
280         struct sockaddr_in *sin;
281         struct auth_domain *clp;
282         int err = 0;
283         struct knfsd_fh fh;
284         char *res;
285
286         if (size < sizeof(*data))
287                 return -EINVAL;
288         data = (struct nfsctl_fdparm*)buf;
289         err = -EPROTONOSUPPORT;
290         if (data->gd_addr.sa_family != AF_INET)
291                 goto out;
292         err = -EINVAL;
293         if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
294                 goto out;
295
296         res = buf;
297         sin = (struct sockaddr_in *)&data->gd_addr;
298         exp_readlock();
299         if (!(clp = auth_unix_lookup(sin->sin_addr)))
300                 err = -EPERM;
301         else {
302                 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
303                 auth_domain_put(clp);
304         }
305         exp_readunlock();
306
307         if (err == 0) {
308                 memset(res,0, NFS_FHSIZE);
309                 memcpy(res, &fh.fh_base, fh.fh_size);
310                 err = NFS_FHSIZE;
311         }
312  out:
313         return err;
314 }
315
316 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
317 {
318         /* request is:
319          *   domain path maxsize
320          * response is
321          *   filehandle
322          *
323          * qword quoting is used, so filehandle will be \x....
324          */
325         char *dname, *path;
326         int maxsize;
327         char *mesg = buf;
328         int len;
329         struct auth_domain *dom;
330         struct knfsd_fh fh;
331
332         if (buf[size-1] != '\n')
333                 return -EINVAL;
334         buf[size-1] = 0;
335
336         dname = mesg;
337         len = qword_get(&mesg, dname, size);
338         if (len <= 0) return -EINVAL;
339         
340         path = dname+len+1;
341         len = qword_get(&mesg, path, size);
342         if (len <= 0) return -EINVAL;
343
344         len = get_int(&mesg, &maxsize);
345         if (len)
346                 return len;
347
348         if (maxsize < NFS_FHSIZE)
349                 return -EINVAL;
350         if (maxsize > NFS3_FHSIZE)
351                 maxsize = NFS3_FHSIZE;
352
353         if (qword_get(&mesg, mesg, size)>0)
354                 return -EINVAL;
355
356         /* we have all the words, they are in buf.. */
357         dom = unix_domain_find(dname);
358         if (!dom)
359                 return -ENOMEM;
360
361         len = exp_rootfh(dom, path, &fh,  maxsize);
362         auth_domain_put(dom);
363         if (len)
364                 return len;
365         
366         mesg = buf; len = PAGE_SIZE-sizeof(struct argresp);
367         qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
368         mesg[-1] = '\n';
369         return mesg - buf;      
370 }
371
372 extern int nfsd_nrthreads(void);
373
374 static ssize_t write_threads(struct file *file, char *buf, size_t size)
375 {
376         /* if size > 0, look for a number of threads and call nfsd_svc
377          * then write out number of threads as reply
378          */
379         char *mesg = buf;
380         int rv;
381         if (size > 0) {
382                 int newthreads;
383                 rv = get_int(&mesg, &newthreads);
384                 if (rv)
385                         return rv;
386                 if (newthreads <0)
387                         return -EINVAL;
388                 rv = nfsd_svc(2049, newthreads);
389                 if (rv)
390                         return rv;
391         }
392         sprintf(buf, "%d\n", nfsd_nrthreads());
393         return strlen(buf);
394 }
395
396 /*----------------------------------------------------------------------------*/
397 /*
398  *      populating the filesystem.
399  */
400
401 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
402 {
403         static struct tree_descr nfsd_files[] = {
404                 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
405                 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
406                 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
407                 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
408                 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
409                 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
410                 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
411                 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
412                 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
413                 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
414                 /* last one */ {""}
415         };
416         return simple_fill_super(sb, 0x6e667364, nfsd_files);
417 }
418
419 static struct super_block *nfsd_get_sb(struct file_system_type *fs_type,
420         int flags, const char *dev_name, void *data)
421 {
422         return get_sb_single(fs_type, flags, data, nfsd_fill_super);
423 }
424
425 static struct file_system_type nfsd_fs_type = {
426         .owner          = THIS_MODULE,
427         .name           = "nfsd",
428         .get_sb         = nfsd_get_sb,
429         .kill_sb        = kill_litter_super,
430 };
431
432 static int __init init_nfsd(void)
433 {
434         int retval;
435         printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
436
437         nfsd_stat_init();       /* Statistics */
438         nfsd_cache_init();      /* RPC reply cache */
439         nfsd_export_init();     /* Exports table */
440         nfsd_lockd_init();      /* lockd->nfsd callbacks */
441 #ifdef CONFIG_NFSD_V4
442         nfsd_idmap_init();      /* Name to ID mapping */
443 #endif /* CONFIG_NFSD_V4 */
444         if (proc_mkdir("fs/nfs", 0)) {
445                 struct proc_dir_entry *entry;
446                 entry = create_proc_entry("fs/nfs/exports", 0, NULL);
447                 if (entry)
448                         entry->proc_fops =  &exports_operations;
449         }
450         retval = register_filesystem(&nfsd_fs_type);
451         if (retval) {
452                 nfsd_export_shutdown();
453                 nfsd_cache_shutdown();
454                 remove_proc_entry("fs/nfs/exports", NULL);
455                 remove_proc_entry("fs/nfs", NULL);
456                 nfsd_stat_shutdown();
457                 nfsd_lockd_shutdown();
458         }
459         return retval;
460 }
461
462 static void __exit exit_nfsd(void)
463 {
464         nfsd_export_shutdown();
465         nfsd_cache_shutdown();
466         remove_proc_entry("fs/nfs/exports", NULL);
467         remove_proc_entry("fs/nfs", NULL);
468         nfsd_stat_shutdown();
469         nfsd_lockd_shutdown();
470 #ifdef CONFIG_NFSD_V4
471         nfsd_idmap_shutdown();
472 #endif /* CONFIG_NFSD_V4 */
473         unregister_filesystem(&nfsd_fs_type);
474 }
475
476 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
477 MODULE_LICENSE("GPL");
478 module_init(init_nfsd)
479 module_exit(exit_nfsd)