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