[PATCH] knfsd: nfsd4 reboot dirname fix
[safe/jmp/linux-2.6] / 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 9 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         NFSD_Leasetime,
54         NFSD_RecoveryDir,
55 };
56
57 /*
58  * write() for these nodes.
59  */
60 static ssize_t write_svc(struct file *file, char *buf, size_t size);
61 static ssize_t write_add(struct file *file, char *buf, size_t size);
62 static ssize_t write_del(struct file *file, char *buf, size_t size);
63 static ssize_t write_export(struct file *file, char *buf, size_t size);
64 static ssize_t write_unexport(struct file *file, char *buf, size_t size);
65 static ssize_t write_getfd(struct file *file, char *buf, size_t size);
66 static ssize_t write_getfs(struct file *file, char *buf, size_t size);
67 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
68 static ssize_t write_threads(struct file *file, char *buf, size_t size);
69 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
70 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
71
72 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
73         [NFSD_Svc] = write_svc,
74         [NFSD_Add] = write_add,
75         [NFSD_Del] = write_del,
76         [NFSD_Export] = write_export,
77         [NFSD_Unexport] = write_unexport,
78         [NFSD_Getfd] = write_getfd,
79         [NFSD_Getfs] = write_getfs,
80         [NFSD_Fh] = write_filehandle,
81         [NFSD_Threads] = write_threads,
82         [NFSD_Leasetime] = write_leasetime,
83         [NFSD_RecoveryDir] = write_recoverydir,
84 };
85
86 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
87 {
88         ino_t ino =  file->f_dentry->d_inode->i_ino;
89         char *data;
90         ssize_t rv;
91
92         if (ino >= sizeof(write_op)/sizeof(write_op[0]) || !write_op[ino])
93                 return -EINVAL;
94
95         data = simple_transaction_get(file, buf, size);
96         if (IS_ERR(data))
97                 return PTR_ERR(data);
98
99         rv =  write_op[ino](file, data, size);
100         if (rv>0) {
101                 simple_transaction_set(file, rv);
102                 rv = size;
103         }
104         return rv;
105 }
106
107 static struct file_operations transaction_ops = {
108         .write          = nfsctl_transaction_write,
109         .read           = simple_transaction_read,
110         .release        = simple_transaction_release,
111 };
112
113 extern struct seq_operations nfs_exports_op;
114 static int exports_open(struct inode *inode, struct file *file)
115 {
116         return seq_open(file, &nfs_exports_op);
117 }
118
119 static struct file_operations exports_operations = {
120         .open           = exports_open,
121         .read           = seq_read,
122         .llseek         = seq_lseek,
123         .release        = seq_release,
124 };
125
126 /*----------------------------------------------------------------------------*/
127 /*
128  * payload - write methods
129  * If the method has a response, the response should be put in buf,
130  * and the length returned.  Otherwise return 0 or and -error.
131  */
132
133 static ssize_t write_svc(struct file *file, char *buf, size_t size)
134 {
135         struct nfsctl_svc *data;
136         if (size < sizeof(*data))
137                 return -EINVAL;
138         data = (struct nfsctl_svc*) buf;
139         return nfsd_svc(data->svc_port, data->svc_nthreads);
140 }
141
142 static ssize_t write_add(struct file *file, char *buf, size_t size)
143 {
144         struct nfsctl_client *data;
145         if (size < sizeof(*data))
146                 return -EINVAL;
147         data = (struct nfsctl_client *)buf;
148         return exp_addclient(data);
149 }
150
151 static ssize_t write_del(struct file *file, char *buf, size_t size)
152 {
153         struct nfsctl_client *data;
154         if (size < sizeof(*data))
155                 return -EINVAL;
156         data = (struct nfsctl_client *)buf;
157         return exp_delclient(data);
158 }
159
160 static ssize_t write_export(struct file *file, char *buf, size_t size)
161 {
162         struct nfsctl_export *data;
163         if (size < sizeof(*data))
164                 return -EINVAL;
165         data = (struct nfsctl_export*)buf;
166         return exp_export(data);
167 }
168
169 static ssize_t write_unexport(struct file *file, char *buf, size_t size)
170 {
171         struct nfsctl_export *data;
172
173         if (size < sizeof(*data))
174                 return -EINVAL;
175         data = (struct nfsctl_export*)buf;
176         return exp_unexport(data);
177 }
178
179 static ssize_t write_getfs(struct file *file, char *buf, size_t size)
180 {
181         struct nfsctl_fsparm *data;
182         struct sockaddr_in *sin;
183         struct auth_domain *clp;
184         int err = 0;
185         struct knfsd_fh *res;
186
187         if (size < sizeof(*data))
188                 return -EINVAL;
189         data = (struct nfsctl_fsparm*)buf;
190         err = -EPROTONOSUPPORT;
191         if (data->gd_addr.sa_family != AF_INET)
192                 goto out;
193         sin = (struct sockaddr_in *)&data->gd_addr;
194         if (data->gd_maxlen > NFS3_FHSIZE)
195                 data->gd_maxlen = NFS3_FHSIZE;
196
197         res = (struct knfsd_fh*)buf;
198
199         exp_readlock();
200         if (!(clp = auth_unix_lookup(sin->sin_addr)))
201                 err = -EPERM;
202         else {
203                 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
204                 auth_domain_put(clp);
205         }
206         exp_readunlock();
207         if (err == 0)
208                 err = res->fh_size + (int)&((struct knfsd_fh*)0)->fh_base;
209  out:
210         return err;
211 }
212
213 static ssize_t write_getfd(struct file *file, char *buf, size_t size)
214 {
215         struct nfsctl_fdparm *data;
216         struct sockaddr_in *sin;
217         struct auth_domain *clp;
218         int err = 0;
219         struct knfsd_fh fh;
220         char *res;
221
222         if (size < sizeof(*data))
223                 return -EINVAL;
224         data = (struct nfsctl_fdparm*)buf;
225         err = -EPROTONOSUPPORT;
226         if (data->gd_addr.sa_family != AF_INET)
227                 goto out;
228         err = -EINVAL;
229         if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
230                 goto out;
231
232         res = buf;
233         sin = (struct sockaddr_in *)&data->gd_addr;
234         exp_readlock();
235         if (!(clp = auth_unix_lookup(sin->sin_addr)))
236                 err = -EPERM;
237         else {
238                 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
239                 auth_domain_put(clp);
240         }
241         exp_readunlock();
242
243         if (err == 0) {
244                 memset(res,0, NFS_FHSIZE);
245                 memcpy(res, &fh.fh_base, fh.fh_size);
246                 err = NFS_FHSIZE;
247         }
248  out:
249         return err;
250 }
251
252 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
253 {
254         /* request is:
255          *   domain path maxsize
256          * response is
257          *   filehandle
258          *
259          * qword quoting is used, so filehandle will be \x....
260          */
261         char *dname, *path;
262         int maxsize;
263         char *mesg = buf;
264         int len;
265         struct auth_domain *dom;
266         struct knfsd_fh fh;
267
268         if (buf[size-1] != '\n')
269                 return -EINVAL;
270         buf[size-1] = 0;
271
272         dname = mesg;
273         len = qword_get(&mesg, dname, size);
274         if (len <= 0) return -EINVAL;
275         
276         path = dname+len+1;
277         len = qword_get(&mesg, path, size);
278         if (len <= 0) return -EINVAL;
279
280         len = get_int(&mesg, &maxsize);
281         if (len)
282                 return len;
283
284         if (maxsize < NFS_FHSIZE)
285                 return -EINVAL;
286         if (maxsize > NFS3_FHSIZE)
287                 maxsize = NFS3_FHSIZE;
288
289         if (qword_get(&mesg, mesg, size)>0)
290                 return -EINVAL;
291
292         /* we have all the words, they are in buf.. */
293         dom = unix_domain_find(dname);
294         if (!dom)
295                 return -ENOMEM;
296
297         len = exp_rootfh(dom, path, &fh,  maxsize);
298         auth_domain_put(dom);
299         if (len)
300                 return len;
301         
302         mesg = buf; len = SIMPLE_TRANSACTION_LIMIT;
303         qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
304         mesg[-1] = '\n';
305         return mesg - buf;      
306 }
307
308 extern int nfsd_nrthreads(void);
309
310 static ssize_t write_threads(struct file *file, char *buf, size_t size)
311 {
312         /* if size > 0, look for a number of threads and call nfsd_svc
313          * then write out number of threads as reply
314          */
315         char *mesg = buf;
316         int rv;
317         if (size > 0) {
318                 int newthreads;
319                 rv = get_int(&mesg, &newthreads);
320                 if (rv)
321                         return rv;
322                 if (newthreads <0)
323                         return -EINVAL;
324                 rv = nfsd_svc(2049, newthreads);
325                 if (rv)
326                         return rv;
327         }
328         sprintf(buf, "%d\n", nfsd_nrthreads());
329         return strlen(buf);
330 }
331
332 extern time_t nfs4_leasetime(void);
333
334 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
335 {
336         /* if size > 10 seconds, call
337          * nfs4_reset_lease() then write out the new lease (seconds) as reply
338          */
339         char *mesg = buf;
340         int rv;
341
342         if (size > 0) {
343                 int lease;
344                 rv = get_int(&mesg, &lease);
345                 if (rv)
346                         return rv;
347                 if (lease < 10 || lease > 3600)
348                         return -EINVAL;
349                 nfs4_reset_lease(lease);
350         }
351         sprintf(buf, "%ld\n", nfs4_lease_time());
352         return strlen(buf);
353 }
354
355 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
356 {
357         char *mesg = buf;
358         char *recdir;
359         int len, status;
360
361         if (size > PATH_MAX || buf[size-1] != '\n')
362                 return -EINVAL;
363         buf[size-1] = 0;
364
365         recdir = mesg;
366         len = qword_get(&mesg, recdir, size);
367         if (len <= 0)
368                 return -EINVAL;
369
370         status = nfs4_reset_recoverydir(recdir);
371         return strlen(buf);
372 }
373
374 /*----------------------------------------------------------------------------*/
375 /*
376  *      populating the filesystem.
377  */
378
379 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
380 {
381         static struct tree_descr nfsd_files[] = {
382                 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
383                 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
384                 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
385                 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
386                 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
387                 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
388                 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
389                 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
390                 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
391                 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
392 #ifdef CONFIG_NFSD_V4
393                 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
394                 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
395 #endif
396                 /* last one */ {""}
397         };
398         return simple_fill_super(sb, 0x6e667364, nfsd_files);
399 }
400
401 static struct super_block *nfsd_get_sb(struct file_system_type *fs_type,
402         int flags, const char *dev_name, void *data)
403 {
404         return get_sb_single(fs_type, flags, data, nfsd_fill_super);
405 }
406
407 static struct file_system_type nfsd_fs_type = {
408         .owner          = THIS_MODULE,
409         .name           = "nfsd",
410         .get_sb         = nfsd_get_sb,
411         .kill_sb        = kill_litter_super,
412 };
413
414 static int __init init_nfsd(void)
415 {
416         int retval;
417         printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
418
419         nfsd_stat_init();       /* Statistics */
420         nfsd_cache_init();      /* RPC reply cache */
421         nfsd_export_init();     /* Exports table */
422         nfsd_lockd_init();      /* lockd->nfsd callbacks */
423         nfs4_state_init();      /* NFSv4 locking state */
424         nfsd_idmap_init();      /* Name to ID mapping */
425         if (proc_mkdir("fs/nfs", NULL)) {
426                 struct proc_dir_entry *entry;
427                 entry = create_proc_entry("fs/nfs/exports", 0, NULL);
428                 if (entry)
429                         entry->proc_fops =  &exports_operations;
430         }
431         retval = register_filesystem(&nfsd_fs_type);
432         if (retval) {
433                 nfsd_export_shutdown();
434                 nfsd_cache_shutdown();
435                 remove_proc_entry("fs/nfs/exports", NULL);
436                 remove_proc_entry("fs/nfs", NULL);
437                 nfsd_stat_shutdown();
438                 nfsd_lockd_shutdown();
439         }
440         return retval;
441 }
442
443 static void __exit exit_nfsd(void)
444 {
445         nfsd_export_shutdown();
446         nfsd_cache_shutdown();
447         remove_proc_entry("fs/nfs/exports", NULL);
448         remove_proc_entry("fs/nfs", NULL);
449         nfsd_stat_shutdown();
450         nfsd_lockd_shutdown();
451         nfsd_idmap_shutdown();
452         unregister_filesystem(&nfsd_fs_type);
453 }
454
455 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
456 MODULE_LICENSE("GPL");
457 module_init(init_nfsd)
458 module_exit(exit_nfsd)