SUNRPC: check RPC inode's pipe reference before dereferencing
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sunrpc / rpc_pipe.c
blobb67b2aecc4ff6fb29be24388cadda9a31d615ec2
1 /*
2 * net/sunrpc/rpc_pipe.c
4 * Userland/kernel interface for rpcauth_gss.
5 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
6 * and fs/sysfs/inode.c
8 * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/pagemap.h>
15 #include <linux/mount.h>
16 #include <linux/namei.h>
17 #include <linux/fsnotify.h>
18 #include <linux/kernel.h>
20 #include <asm/ioctls.h>
21 #include <linux/poll.h>
22 #include <linux/wait.h>
23 #include <linux/seq_file.h>
25 #include <linux/sunrpc/clnt.h>
26 #include <linux/workqueue.h>
27 #include <linux/sunrpc/rpc_pipe_fs.h>
28 #include <linux/sunrpc/cache.h>
29 #include <linux/nsproxy.h>
30 #include <linux/notifier.h>
32 #include "netns.h"
33 #include "sunrpc.h"
35 #define RPCDBG_FACILITY RPCDBG_DEBUG
37 #define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
39 static struct file_system_type rpc_pipe_fs_type;
42 static struct kmem_cache *rpc_inode_cachep __read_mostly;
44 #define RPC_UPCALL_TIMEOUT (30*HZ)
46 static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
48 int rpc_pipefs_notifier_register(struct notifier_block *nb)
50 return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
52 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
54 void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
56 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
58 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
60 static void rpc_purge_list(struct rpc_pipe *pipe, struct list_head *head,
61 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
63 struct rpc_pipe_msg *msg;
65 if (list_empty(head))
66 return;
67 do {
68 msg = list_entry(head->next, struct rpc_pipe_msg, list);
69 list_del_init(&msg->list);
70 msg->errno = err;
71 destroy_msg(msg);
72 } while (!list_empty(head));
73 wake_up(&pipe->waitq);
76 static void
77 rpc_timeout_upcall_queue(struct work_struct *work)
79 LIST_HEAD(free_list);
80 struct rpc_pipe *pipe =
81 container_of(work, struct rpc_pipe, queue_timeout.work);
82 void (*destroy_msg)(struct rpc_pipe_msg *);
84 spin_lock(&pipe->lock);
85 destroy_msg = pipe->ops->destroy_msg;
86 if (pipe->nreaders == 0) {
87 list_splice_init(&pipe->pipe, &free_list);
88 pipe->pipelen = 0;
90 spin_unlock(&pipe->lock);
91 rpc_purge_list(pipe, &free_list, destroy_msg, -ETIMEDOUT);
94 ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
95 char __user *dst, size_t buflen)
97 char *data = (char *)msg->data + msg->copied;
98 size_t mlen = min(msg->len - msg->copied, buflen);
99 unsigned long left;
101 left = copy_to_user(dst, data, mlen);
102 if (left == mlen) {
103 msg->errno = -EFAULT;
104 return -EFAULT;
107 mlen -= left;
108 msg->copied += mlen;
109 msg->errno = 0;
110 return mlen;
112 EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
115 * rpc_queue_upcall - queue an upcall message to userspace
116 * @inode: inode of upcall pipe on which to queue given message
117 * @msg: message to queue
119 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
120 * A userspace process may then later read the upcall by performing a
121 * read on an open file for this inode. It is up to the caller to
122 * initialize the fields of @msg (other than @msg->list) appropriately.
125 rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
127 int res = -EPIPE;
129 spin_lock(&pipe->lock);
130 if (pipe->nreaders) {
131 list_add_tail(&msg->list, &pipe->pipe);
132 pipe->pipelen += msg->len;
133 res = 0;
134 } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
135 if (list_empty(&pipe->pipe))
136 queue_delayed_work(rpciod_workqueue,
137 &pipe->queue_timeout,
138 RPC_UPCALL_TIMEOUT);
139 list_add_tail(&msg->list, &pipe->pipe);
140 pipe->pipelen += msg->len;
141 res = 0;
143 spin_unlock(&pipe->lock);
144 wake_up(&pipe->waitq);
145 return res;
147 EXPORT_SYMBOL_GPL(rpc_queue_upcall);
149 static inline void
150 rpc_inode_setowner(struct inode *inode, void *private)
152 RPC_I(inode)->private = private;
155 static void
156 rpc_close_pipes(struct inode *inode)
158 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
159 int need_release;
160 LIST_HEAD(free_list);
162 mutex_lock(&inode->i_mutex);
163 spin_lock(&pipe->lock);
164 need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
165 pipe->nreaders = 0;
166 list_splice_init(&pipe->in_upcall, &free_list);
167 list_splice_init(&pipe->pipe, &free_list);
168 pipe->pipelen = 0;
169 pipe->dentry = NULL;
170 spin_unlock(&pipe->lock);
171 rpc_purge_list(pipe, &free_list, pipe->ops->destroy_msg, -EPIPE);
172 pipe->nwriters = 0;
173 if (need_release && pipe->ops->release_pipe)
174 pipe->ops->release_pipe(inode);
175 cancel_delayed_work_sync(&pipe->queue_timeout);
176 rpc_inode_setowner(inode, NULL);
177 RPC_I(inode)->pipe = NULL;
178 mutex_unlock(&inode->i_mutex);
181 static struct inode *
182 rpc_alloc_inode(struct super_block *sb)
184 struct rpc_inode *rpci;
185 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
186 if (!rpci)
187 return NULL;
188 return &rpci->vfs_inode;
191 static void
192 rpc_i_callback(struct rcu_head *head)
194 struct inode *inode = container_of(head, struct inode, i_rcu);
195 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
198 static void
199 rpc_destroy_inode(struct inode *inode)
201 call_rcu(&inode->i_rcu, rpc_i_callback);
204 static int
205 rpc_pipe_open(struct inode *inode, struct file *filp)
207 struct rpc_pipe *pipe;
208 int first_open;
209 int res = -ENXIO;
211 mutex_lock(&inode->i_mutex);
212 pipe = RPC_I(inode)->pipe;
213 if (pipe == NULL)
214 goto out;
215 first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
216 if (first_open && pipe->ops->open_pipe) {
217 res = pipe->ops->open_pipe(inode);
218 if (res)
219 goto out;
221 if (filp->f_mode & FMODE_READ)
222 pipe->nreaders++;
223 if (filp->f_mode & FMODE_WRITE)
224 pipe->nwriters++;
225 res = 0;
226 out:
227 mutex_unlock(&inode->i_mutex);
228 return res;
231 static int
232 rpc_pipe_release(struct inode *inode, struct file *filp)
234 struct rpc_pipe *pipe;
235 struct rpc_pipe_msg *msg;
236 int last_close;
238 mutex_lock(&inode->i_mutex);
239 pipe = RPC_I(inode)->pipe;
240 if (pipe == NULL)
241 goto out;
242 msg = filp->private_data;
243 if (msg != NULL) {
244 spin_lock(&pipe->lock);
245 msg->errno = -EAGAIN;
246 list_del_init(&msg->list);
247 spin_unlock(&pipe->lock);
248 pipe->ops->destroy_msg(msg);
250 if (filp->f_mode & FMODE_WRITE)
251 pipe->nwriters --;
252 if (filp->f_mode & FMODE_READ) {
253 pipe->nreaders --;
254 if (pipe->nreaders == 0) {
255 LIST_HEAD(free_list);
256 spin_lock(&pipe->lock);
257 list_splice_init(&pipe->pipe, &free_list);
258 pipe->pipelen = 0;
259 spin_unlock(&pipe->lock);
260 rpc_purge_list(pipe, &free_list,
261 pipe->ops->destroy_msg, -EAGAIN);
264 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
265 if (last_close && pipe->ops->release_pipe)
266 pipe->ops->release_pipe(inode);
267 out:
268 mutex_unlock(&inode->i_mutex);
269 return 0;
272 static ssize_t
273 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
275 struct inode *inode = filp->f_path.dentry->d_inode;
276 struct rpc_pipe *pipe;
277 struct rpc_pipe_msg *msg;
278 int res = 0;
280 mutex_lock(&inode->i_mutex);
281 pipe = RPC_I(inode)->pipe;
282 if (pipe == NULL) {
283 res = -EPIPE;
284 goto out_unlock;
286 msg = filp->private_data;
287 if (msg == NULL) {
288 spin_lock(&pipe->lock);
289 if (!list_empty(&pipe->pipe)) {
290 msg = list_entry(pipe->pipe.next,
291 struct rpc_pipe_msg,
292 list);
293 list_move(&msg->list, &pipe->in_upcall);
294 pipe->pipelen -= msg->len;
295 filp->private_data = msg;
296 msg->copied = 0;
298 spin_unlock(&pipe->lock);
299 if (msg == NULL)
300 goto out_unlock;
302 /* NOTE: it is up to the callback to update msg->copied */
303 res = pipe->ops->upcall(filp, msg, buf, len);
304 if (res < 0 || msg->len == msg->copied) {
305 filp->private_data = NULL;
306 spin_lock(&pipe->lock);
307 list_del_init(&msg->list);
308 spin_unlock(&pipe->lock);
309 pipe->ops->destroy_msg(msg);
311 out_unlock:
312 mutex_unlock(&inode->i_mutex);
313 return res;
316 static ssize_t
317 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
319 struct inode *inode = filp->f_path.dentry->d_inode;
320 int res;
322 mutex_lock(&inode->i_mutex);
323 res = -EPIPE;
324 if (RPC_I(inode)->pipe != NULL)
325 res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
326 mutex_unlock(&inode->i_mutex);
327 return res;
330 static unsigned int
331 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
333 struct rpc_pipe *pipe = RPC_I(filp->f_path.dentry->d_inode)->pipe;
334 unsigned int mask = 0;
336 poll_wait(filp, &pipe->waitq, wait);
338 mask = POLLOUT | POLLWRNORM;
339 if (pipe->dentry == NULL)
340 mask |= POLLERR | POLLHUP;
341 if (filp->private_data || !list_empty(&pipe->pipe))
342 mask |= POLLIN | POLLRDNORM;
343 return mask;
346 static long
347 rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
349 struct inode *inode = filp->f_path.dentry->d_inode;
350 struct rpc_pipe *pipe;
351 int len;
353 switch (cmd) {
354 case FIONREAD:
355 mutex_lock(&inode->i_mutex);
356 pipe = RPC_I(inode)->pipe;
357 if (pipe == NULL) {
358 mutex_unlock(&inode->i_mutex);
359 return -EPIPE;
361 spin_lock(&pipe->lock);
362 len = pipe->pipelen;
363 if (filp->private_data) {
364 struct rpc_pipe_msg *msg;
365 msg = filp->private_data;
366 len += msg->len - msg->copied;
368 spin_unlock(&pipe->lock);
369 mutex_unlock(&inode->i_mutex);
370 return put_user(len, (int __user *)arg);
371 default:
372 return -EINVAL;
376 static const struct file_operations rpc_pipe_fops = {
377 .owner = THIS_MODULE,
378 .llseek = no_llseek,
379 .read = rpc_pipe_read,
380 .write = rpc_pipe_write,
381 .poll = rpc_pipe_poll,
382 .unlocked_ioctl = rpc_pipe_ioctl,
383 .open = rpc_pipe_open,
384 .release = rpc_pipe_release,
387 static int
388 rpc_show_info(struct seq_file *m, void *v)
390 struct rpc_clnt *clnt = m->private;
392 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
393 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
394 clnt->cl_prog, clnt->cl_vers);
395 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
396 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
397 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
398 return 0;
401 static int
402 rpc_info_open(struct inode *inode, struct file *file)
404 struct rpc_clnt *clnt = NULL;
405 int ret = single_open(file, rpc_show_info, NULL);
407 if (!ret) {
408 struct seq_file *m = file->private_data;
410 spin_lock(&file->f_path.dentry->d_lock);
411 if (!d_unhashed(file->f_path.dentry))
412 clnt = RPC_I(inode)->private;
413 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
414 spin_unlock(&file->f_path.dentry->d_lock);
415 m->private = clnt;
416 } else {
417 spin_unlock(&file->f_path.dentry->d_lock);
418 single_release(inode, file);
419 ret = -EINVAL;
422 return ret;
425 static int
426 rpc_info_release(struct inode *inode, struct file *file)
428 struct seq_file *m = file->private_data;
429 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
431 if (clnt)
432 rpc_release_client(clnt);
433 return single_release(inode, file);
436 static const struct file_operations rpc_info_operations = {
437 .owner = THIS_MODULE,
438 .open = rpc_info_open,
439 .read = seq_read,
440 .llseek = seq_lseek,
441 .release = rpc_info_release,
446 * Description of fs contents.
448 struct rpc_filelist {
449 const char *name;
450 const struct file_operations *i_fop;
451 umode_t mode;
454 static int rpc_delete_dentry(const struct dentry *dentry)
456 return 1;
459 static const struct dentry_operations rpc_dentry_operations = {
460 .d_delete = rpc_delete_dentry,
463 static struct inode *
464 rpc_get_inode(struct super_block *sb, umode_t mode)
466 struct inode *inode = new_inode(sb);
467 if (!inode)
468 return NULL;
469 inode->i_ino = get_next_ino();
470 inode->i_mode = mode;
471 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
472 switch (mode & S_IFMT) {
473 case S_IFDIR:
474 inode->i_fop = &simple_dir_operations;
475 inode->i_op = &simple_dir_inode_operations;
476 inc_nlink(inode);
477 default:
478 break;
480 return inode;
483 static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
484 umode_t mode,
485 const struct file_operations *i_fop,
486 void *private)
488 struct inode *inode;
490 d_drop(dentry);
491 inode = rpc_get_inode(dir->i_sb, mode);
492 if (!inode)
493 goto out_err;
494 inode->i_ino = iunique(dir->i_sb, 100);
495 if (i_fop)
496 inode->i_fop = i_fop;
497 if (private)
498 rpc_inode_setowner(inode, private);
499 d_add(dentry, inode);
500 return 0;
501 out_err:
502 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
503 __FILE__, __func__, dentry->d_name.name);
504 dput(dentry);
505 return -ENOMEM;
508 static int __rpc_create(struct inode *dir, struct dentry *dentry,
509 umode_t mode,
510 const struct file_operations *i_fop,
511 void *private)
513 int err;
515 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
516 if (err)
517 return err;
518 fsnotify_create(dir, dentry);
519 return 0;
522 static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
523 umode_t mode,
524 const struct file_operations *i_fop,
525 void *private)
527 int err;
529 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
530 if (err)
531 return err;
532 inc_nlink(dir);
533 fsnotify_mkdir(dir, dentry);
534 return 0;
537 static void
538 init_pipe(struct rpc_pipe *pipe)
540 pipe->nreaders = 0;
541 pipe->nwriters = 0;
542 INIT_LIST_HEAD(&pipe->in_upcall);
543 INIT_LIST_HEAD(&pipe->in_downcall);
544 INIT_LIST_HEAD(&pipe->pipe);
545 pipe->pipelen = 0;
546 init_waitqueue_head(&pipe->waitq);
547 INIT_DELAYED_WORK(&pipe->queue_timeout,
548 rpc_timeout_upcall_queue);
549 pipe->ops = NULL;
550 spin_lock_init(&pipe->lock);
551 pipe->dentry = NULL;
554 void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
556 kfree(pipe);
558 EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
560 struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
562 struct rpc_pipe *pipe;
564 pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
565 if (!pipe)
566 return ERR_PTR(-ENOMEM);
567 init_pipe(pipe);
568 pipe->ops = ops;
569 pipe->flags = flags;
570 return pipe;
572 EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
574 static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
575 umode_t mode,
576 const struct file_operations *i_fop,
577 void *private,
578 struct rpc_pipe *pipe)
580 struct rpc_inode *rpci;
581 int err;
583 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
584 if (err)
585 return err;
586 rpci = RPC_I(dentry->d_inode);
587 rpci->private = private;
588 rpci->pipe = pipe;
589 fsnotify_create(dir, dentry);
590 return 0;
593 static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
595 int ret;
597 dget(dentry);
598 ret = simple_rmdir(dir, dentry);
599 d_delete(dentry);
600 dput(dentry);
601 return ret;
604 int rpc_rmdir(struct dentry *dentry)
606 struct dentry *parent;
607 struct inode *dir;
608 int error;
610 parent = dget_parent(dentry);
611 dir = parent->d_inode;
612 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
613 error = __rpc_rmdir(dir, dentry);
614 mutex_unlock(&dir->i_mutex);
615 dput(parent);
616 return error;
618 EXPORT_SYMBOL_GPL(rpc_rmdir);
620 static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
622 int ret;
624 dget(dentry);
625 ret = simple_unlink(dir, dentry);
626 d_delete(dentry);
627 dput(dentry);
628 return ret;
631 static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
633 struct inode *inode = dentry->d_inode;
635 rpc_close_pipes(inode);
636 return __rpc_unlink(dir, dentry);
639 static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
640 struct qstr *name)
642 struct dentry *dentry;
644 dentry = d_lookup(parent, name);
645 if (!dentry) {
646 dentry = d_alloc(parent, name);
647 if (!dentry)
648 return ERR_PTR(-ENOMEM);
650 if (dentry->d_inode == NULL) {
651 d_set_d_op(dentry, &rpc_dentry_operations);
652 return dentry;
654 dput(dentry);
655 return ERR_PTR(-EEXIST);
659 * FIXME: This probably has races.
661 static void __rpc_depopulate(struct dentry *parent,
662 const struct rpc_filelist *files,
663 int start, int eof)
665 struct inode *dir = parent->d_inode;
666 struct dentry *dentry;
667 struct qstr name;
668 int i;
670 for (i = start; i < eof; i++) {
671 name.name = files[i].name;
672 name.len = strlen(files[i].name);
673 name.hash = full_name_hash(name.name, name.len);
674 dentry = d_lookup(parent, &name);
676 if (dentry == NULL)
677 continue;
678 if (dentry->d_inode == NULL)
679 goto next;
680 switch (dentry->d_inode->i_mode & S_IFMT) {
681 default:
682 BUG();
683 case S_IFREG:
684 __rpc_unlink(dir, dentry);
685 break;
686 case S_IFDIR:
687 __rpc_rmdir(dir, dentry);
689 next:
690 dput(dentry);
694 static void rpc_depopulate(struct dentry *parent,
695 const struct rpc_filelist *files,
696 int start, int eof)
698 struct inode *dir = parent->d_inode;
700 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
701 __rpc_depopulate(parent, files, start, eof);
702 mutex_unlock(&dir->i_mutex);
705 static int rpc_populate(struct dentry *parent,
706 const struct rpc_filelist *files,
707 int start, int eof,
708 void *private)
710 struct inode *dir = parent->d_inode;
711 struct dentry *dentry;
712 int i, err;
714 mutex_lock(&dir->i_mutex);
715 for (i = start; i < eof; i++) {
716 struct qstr q;
718 q.name = files[i].name;
719 q.len = strlen(files[i].name);
720 q.hash = full_name_hash(q.name, q.len);
721 dentry = __rpc_lookup_create_exclusive(parent, &q);
722 err = PTR_ERR(dentry);
723 if (IS_ERR(dentry))
724 goto out_bad;
725 switch (files[i].mode & S_IFMT) {
726 default:
727 BUG();
728 case S_IFREG:
729 err = __rpc_create(dir, dentry,
730 files[i].mode,
731 files[i].i_fop,
732 private);
733 break;
734 case S_IFDIR:
735 err = __rpc_mkdir(dir, dentry,
736 files[i].mode,
737 NULL,
738 private);
740 if (err != 0)
741 goto out_bad;
743 mutex_unlock(&dir->i_mutex);
744 return 0;
745 out_bad:
746 __rpc_depopulate(parent, files, start, eof);
747 mutex_unlock(&dir->i_mutex);
748 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
749 __FILE__, __func__, parent->d_name.name);
750 return err;
753 static struct dentry *rpc_mkdir_populate(struct dentry *parent,
754 struct qstr *name, umode_t mode, void *private,
755 int (*populate)(struct dentry *, void *), void *args_populate)
757 struct dentry *dentry;
758 struct inode *dir = parent->d_inode;
759 int error;
761 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
762 dentry = __rpc_lookup_create_exclusive(parent, name);
763 if (IS_ERR(dentry))
764 goto out;
765 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
766 if (error != 0)
767 goto out_err;
768 if (populate != NULL) {
769 error = populate(dentry, args_populate);
770 if (error)
771 goto err_rmdir;
773 out:
774 mutex_unlock(&dir->i_mutex);
775 return dentry;
776 err_rmdir:
777 __rpc_rmdir(dir, dentry);
778 out_err:
779 dentry = ERR_PTR(error);
780 goto out;
783 static int rpc_rmdir_depopulate(struct dentry *dentry,
784 void (*depopulate)(struct dentry *))
786 struct dentry *parent;
787 struct inode *dir;
788 int error;
790 parent = dget_parent(dentry);
791 dir = parent->d_inode;
792 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
793 if (depopulate != NULL)
794 depopulate(dentry);
795 error = __rpc_rmdir(dir, dentry);
796 mutex_unlock(&dir->i_mutex);
797 dput(parent);
798 return error;
802 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
803 * @parent: dentry of directory to create new "pipe" in
804 * @name: name of pipe
805 * @private: private data to associate with the pipe, for the caller's use
806 * @ops: operations defining the behavior of the pipe: upcall, downcall,
807 * release_pipe, open_pipe, and destroy_msg.
808 * @flags: rpc_pipe flags
810 * Data is made available for userspace to read by calls to
811 * rpc_queue_upcall(). The actual reads will result in calls to
812 * @ops->upcall, which will be called with the file pointer,
813 * message, and userspace buffer to copy to.
815 * Writes can come at any time, and do not necessarily have to be
816 * responses to upcalls. They will result in calls to @msg->downcall.
818 * The @private argument passed here will be available to all these methods
819 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
821 struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
822 void *private, struct rpc_pipe *pipe)
824 struct dentry *dentry;
825 struct inode *dir = parent->d_inode;
826 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
827 struct qstr q;
828 int err;
830 if (pipe->ops->upcall == NULL)
831 umode &= ~S_IRUGO;
832 if (pipe->ops->downcall == NULL)
833 umode &= ~S_IWUGO;
835 q.name = name;
836 q.len = strlen(name);
837 q.hash = full_name_hash(q.name, q.len),
839 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
840 dentry = __rpc_lookup_create_exclusive(parent, &q);
841 if (IS_ERR(dentry))
842 goto out;
843 err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
844 private, pipe);
845 if (err)
846 goto out_err;
847 out:
848 mutex_unlock(&dir->i_mutex);
849 return dentry;
850 out_err:
851 dentry = ERR_PTR(err);
852 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
853 __FILE__, __func__, parent->d_name.name, name,
854 err);
855 goto out;
857 EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
860 * rpc_unlink - remove a pipe
861 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
863 * After this call, lookups will no longer find the pipe, and any
864 * attempts to read or write using preexisting opens of the pipe will
865 * return -EPIPE.
868 rpc_unlink(struct dentry *dentry)
870 struct dentry *parent;
871 struct inode *dir;
872 int error = 0;
874 parent = dget_parent(dentry);
875 dir = parent->d_inode;
876 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
877 error = __rpc_rmpipe(dir, dentry);
878 mutex_unlock(&dir->i_mutex);
879 dput(parent);
880 return error;
882 EXPORT_SYMBOL_GPL(rpc_unlink);
884 enum {
885 RPCAUTH_info,
886 RPCAUTH_EOF
889 static const struct rpc_filelist authfiles[] = {
890 [RPCAUTH_info] = {
891 .name = "info",
892 .i_fop = &rpc_info_operations,
893 .mode = S_IFREG | S_IRUSR,
897 static int rpc_clntdir_populate(struct dentry *dentry, void *private)
899 return rpc_populate(dentry,
900 authfiles, RPCAUTH_info, RPCAUTH_EOF,
901 private);
904 static void rpc_clntdir_depopulate(struct dentry *dentry)
906 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
910 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
911 * @dentry: dentry from the rpc_pipefs root to the new directory
912 * @name: &struct qstr for the name
913 * @rpc_client: rpc client to associate with this directory
915 * This creates a directory at the given @path associated with
916 * @rpc_clnt, which will contain a file named "info" with some basic
917 * information about the client, together with any "pipes" that may
918 * later be created using rpc_mkpipe().
920 struct dentry *rpc_create_client_dir(struct dentry *dentry,
921 struct qstr *name,
922 struct rpc_clnt *rpc_client)
924 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
925 rpc_clntdir_populate, rpc_client);
929 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
930 * @clnt: rpc client
932 int rpc_remove_client_dir(struct dentry *dentry)
934 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
937 static const struct rpc_filelist cache_pipefs_files[3] = {
938 [0] = {
939 .name = "channel",
940 .i_fop = &cache_file_operations_pipefs,
941 .mode = S_IFREG|S_IRUSR|S_IWUSR,
943 [1] = {
944 .name = "content",
945 .i_fop = &content_file_operations_pipefs,
946 .mode = S_IFREG|S_IRUSR,
948 [2] = {
949 .name = "flush",
950 .i_fop = &cache_flush_operations_pipefs,
951 .mode = S_IFREG|S_IRUSR|S_IWUSR,
955 static int rpc_cachedir_populate(struct dentry *dentry, void *private)
957 return rpc_populate(dentry,
958 cache_pipefs_files, 0, 3,
959 private);
962 static void rpc_cachedir_depopulate(struct dentry *dentry)
964 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
967 struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
968 umode_t umode, struct cache_detail *cd)
970 return rpc_mkdir_populate(parent, name, umode, NULL,
971 rpc_cachedir_populate, cd);
974 void rpc_remove_cache_dir(struct dentry *dentry)
976 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
980 * populate the filesystem
982 static const struct super_operations s_ops = {
983 .alloc_inode = rpc_alloc_inode,
984 .destroy_inode = rpc_destroy_inode,
985 .statfs = simple_statfs,
988 #define RPCAUTH_GSSMAGIC 0x67596969
991 * We have a single directory with 1 node in it.
993 enum {
994 RPCAUTH_lockd,
995 RPCAUTH_mount,
996 RPCAUTH_nfs,
997 RPCAUTH_portmap,
998 RPCAUTH_statd,
999 RPCAUTH_nfsd4_cb,
1000 RPCAUTH_cache,
1001 RPCAUTH_RootEOF
1004 static const struct rpc_filelist files[] = {
1005 [RPCAUTH_lockd] = {
1006 .name = "lockd",
1007 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1009 [RPCAUTH_mount] = {
1010 .name = "mount",
1011 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1013 [RPCAUTH_nfs] = {
1014 .name = "nfs",
1015 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1017 [RPCAUTH_portmap] = {
1018 .name = "portmap",
1019 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1021 [RPCAUTH_statd] = {
1022 .name = "statd",
1023 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1025 [RPCAUTH_nfsd4_cb] = {
1026 .name = "nfsd4_cb",
1027 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1029 [RPCAUTH_cache] = {
1030 .name = "cache",
1031 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1036 * This call can be used only in RPC pipefs mount notification hooks.
1038 struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1039 const unsigned char *dir_name)
1041 struct qstr dir = {
1042 .name = dir_name,
1043 .len = strlen(dir_name),
1044 .hash = full_name_hash(dir_name, strlen(dir_name)),
1047 return d_lookup(sb->s_root, &dir);
1049 EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1051 void rpc_pipefs_init_net(struct net *net)
1053 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1055 mutex_init(&sn->pipefs_sb_lock);
1059 * This call will be used for per network namespace operations calls.
1060 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1061 * found. This lock have to be released by rpc_put_sb_net() when all operations
1062 * will be completed.
1064 struct super_block *rpc_get_sb_net(const struct net *net)
1066 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1068 mutex_lock(&sn->pipefs_sb_lock);
1069 if (sn->pipefs_sb)
1070 return sn->pipefs_sb;
1071 mutex_unlock(&sn->pipefs_sb_lock);
1072 return NULL;
1074 EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1076 void rpc_put_sb_net(const struct net *net)
1078 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1080 BUG_ON(sn->pipefs_sb == NULL);
1081 mutex_unlock(&sn->pipefs_sb_lock);
1083 EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1085 static int
1086 rpc_fill_super(struct super_block *sb, void *data, int silent)
1088 struct inode *inode;
1089 struct dentry *root;
1090 struct net *net = data;
1091 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1092 int err;
1094 sb->s_blocksize = PAGE_CACHE_SIZE;
1095 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1096 sb->s_magic = RPCAUTH_GSSMAGIC;
1097 sb->s_op = &s_ops;
1098 sb->s_time_gran = 1;
1100 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1101 if (!inode)
1102 return -ENOMEM;
1103 sb->s_root = root = d_alloc_root(inode);
1104 if (!root) {
1105 iput(inode);
1106 return -ENOMEM;
1108 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
1109 return -ENOMEM;
1110 dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net,
1111 NET_NAME(net));
1112 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1113 RPC_PIPEFS_MOUNT,
1114 sb);
1115 if (err)
1116 goto err_depopulate;
1117 sb->s_fs_info = get_net(net);
1118 sn->pipefs_sb = sb;
1119 return 0;
1121 err_depopulate:
1122 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1123 RPC_PIPEFS_UMOUNT,
1124 sb);
1125 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1126 return err;
1129 static struct dentry *
1130 rpc_mount(struct file_system_type *fs_type,
1131 int flags, const char *dev_name, void *data)
1133 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
1136 void rpc_kill_sb(struct super_block *sb)
1138 struct net *net = sb->s_fs_info;
1139 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1141 mutex_lock(&sn->pipefs_sb_lock);
1142 sn->pipefs_sb = NULL;
1143 mutex_unlock(&sn->pipefs_sb_lock);
1144 put_net(net);
1145 dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net,
1146 NET_NAME(net));
1147 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1148 RPC_PIPEFS_UMOUNT,
1149 sb);
1150 kill_litter_super(sb);
1153 static struct file_system_type rpc_pipe_fs_type = {
1154 .owner = THIS_MODULE,
1155 .name = "rpc_pipefs",
1156 .mount = rpc_mount,
1157 .kill_sb = rpc_kill_sb,
1160 static void
1161 init_once(void *foo)
1163 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1165 inode_init_once(&rpci->vfs_inode);
1166 rpci->private = NULL;
1167 rpci->pipe = NULL;
1170 int register_rpc_pipefs(void)
1172 int err;
1174 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
1175 sizeof(struct rpc_inode),
1176 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1177 SLAB_MEM_SPREAD),
1178 init_once);
1179 if (!rpc_inode_cachep)
1180 return -ENOMEM;
1181 err = rpc_clients_notifier_register();
1182 if (err)
1183 goto err_notifier;
1184 err = register_filesystem(&rpc_pipe_fs_type);
1185 if (err)
1186 goto err_register;
1187 return 0;
1189 err_register:
1190 rpc_clients_notifier_unregister();
1191 err_notifier:
1192 kmem_cache_destroy(rpc_inode_cachep);
1193 return err;
1196 void unregister_rpc_pipefs(void)
1198 rpc_clients_notifier_unregister();
1199 kmem_cache_destroy(rpc_inode_cachep);
1200 unregister_filesystem(&rpc_pipe_fs_type);
1203 /* Make 'mount -t rpc_pipefs ...' autoload this module. */
1204 MODULE_ALIAS("rpc_pipefs");