SUNRPC: hold current network namespace while pipefs superblock is active
[linux-2.6/libata-dev.git] / net / sunrpc / rpc_pipe.c
blobf628b0f48a87c04f6cf384356148e6d9f5029c35
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/fs.h>
22 #include <linux/poll.h>
23 #include <linux/wait.h>
24 #include <linux/seq_file.h>
26 #include <linux/sunrpc/clnt.h>
27 #include <linux/workqueue.h>
28 #include <linux/sunrpc/rpc_pipe_fs.h>
29 #include <linux/sunrpc/cache.h>
30 #include <linux/nsproxy.h>
32 #include "netns.h"
34 static struct vfsmount *rpc_mnt __read_mostly;
35 static int rpc_mount_count;
37 static struct file_system_type rpc_pipe_fs_type;
40 static struct kmem_cache *rpc_inode_cachep __read_mostly;
42 #define RPC_UPCALL_TIMEOUT (30*HZ)
44 static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
45 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
47 struct rpc_pipe_msg *msg;
49 if (list_empty(head))
50 return;
51 do {
52 msg = list_entry(head->next, struct rpc_pipe_msg, list);
53 list_del_init(&msg->list);
54 msg->errno = err;
55 destroy_msg(msg);
56 } while (!list_empty(head));
57 wake_up(&rpci->waitq);
60 static void
61 rpc_timeout_upcall_queue(struct work_struct *work)
63 LIST_HEAD(free_list);
64 struct rpc_inode *rpci =
65 container_of(work, struct rpc_inode, queue_timeout.work);
66 struct inode *inode = &rpci->vfs_inode;
67 void (*destroy_msg)(struct rpc_pipe_msg *);
69 spin_lock(&inode->i_lock);
70 if (rpci->ops == NULL) {
71 spin_unlock(&inode->i_lock);
72 return;
74 destroy_msg = rpci->ops->destroy_msg;
75 if (rpci->nreaders == 0) {
76 list_splice_init(&rpci->pipe, &free_list);
77 rpci->pipelen = 0;
79 spin_unlock(&inode->i_lock);
80 rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
83 ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
84 char __user *dst, size_t buflen)
86 char *data = (char *)msg->data + msg->copied;
87 size_t mlen = min(msg->len - msg->copied, buflen);
88 unsigned long left;
90 left = copy_to_user(dst, data, mlen);
91 if (left == mlen) {
92 msg->errno = -EFAULT;
93 return -EFAULT;
96 mlen -= left;
97 msg->copied += mlen;
98 msg->errno = 0;
99 return mlen;
101 EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
104 * rpc_queue_upcall - queue an upcall message to userspace
105 * @inode: inode of upcall pipe on which to queue given message
106 * @msg: message to queue
108 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
109 * A userspace process may then later read the upcall by performing a
110 * read on an open file for this inode. It is up to the caller to
111 * initialize the fields of @msg (other than @msg->list) appropriately.
114 rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
116 struct rpc_inode *rpci = RPC_I(inode);
117 int res = -EPIPE;
119 spin_lock(&inode->i_lock);
120 if (rpci->ops == NULL)
121 goto out;
122 if (rpci->nreaders) {
123 list_add_tail(&msg->list, &rpci->pipe);
124 rpci->pipelen += msg->len;
125 res = 0;
126 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
127 if (list_empty(&rpci->pipe))
128 queue_delayed_work(rpciod_workqueue,
129 &rpci->queue_timeout,
130 RPC_UPCALL_TIMEOUT);
131 list_add_tail(&msg->list, &rpci->pipe);
132 rpci->pipelen += msg->len;
133 res = 0;
135 out:
136 spin_unlock(&inode->i_lock);
137 wake_up(&rpci->waitq);
138 return res;
140 EXPORT_SYMBOL_GPL(rpc_queue_upcall);
142 static inline void
143 rpc_inode_setowner(struct inode *inode, void *private)
145 RPC_I(inode)->private = private;
148 static void
149 rpc_close_pipes(struct inode *inode)
151 struct rpc_inode *rpci = RPC_I(inode);
152 const struct rpc_pipe_ops *ops;
153 int need_release;
155 mutex_lock(&inode->i_mutex);
156 ops = rpci->ops;
157 if (ops != NULL) {
158 LIST_HEAD(free_list);
159 spin_lock(&inode->i_lock);
160 need_release = rpci->nreaders != 0 || rpci->nwriters != 0;
161 rpci->nreaders = 0;
162 list_splice_init(&rpci->in_upcall, &free_list);
163 list_splice_init(&rpci->pipe, &free_list);
164 rpci->pipelen = 0;
165 rpci->ops = NULL;
166 spin_unlock(&inode->i_lock);
167 rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
168 rpci->nwriters = 0;
169 if (need_release && ops->release_pipe)
170 ops->release_pipe(inode);
171 cancel_delayed_work_sync(&rpci->queue_timeout);
173 rpc_inode_setowner(inode, NULL);
174 mutex_unlock(&inode->i_mutex);
177 static struct inode *
178 rpc_alloc_inode(struct super_block *sb)
180 struct rpc_inode *rpci;
181 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
182 if (!rpci)
183 return NULL;
184 return &rpci->vfs_inode;
187 static void
188 rpc_i_callback(struct rcu_head *head)
190 struct inode *inode = container_of(head, struct inode, i_rcu);
191 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
194 static void
195 rpc_destroy_inode(struct inode *inode)
197 call_rcu(&inode->i_rcu, rpc_i_callback);
200 static int
201 rpc_pipe_open(struct inode *inode, struct file *filp)
203 struct rpc_inode *rpci = RPC_I(inode);
204 int first_open;
205 int res = -ENXIO;
207 mutex_lock(&inode->i_mutex);
208 if (rpci->ops == NULL)
209 goto out;
210 first_open = rpci->nreaders == 0 && rpci->nwriters == 0;
211 if (first_open && rpci->ops->open_pipe) {
212 res = rpci->ops->open_pipe(inode);
213 if (res)
214 goto out;
216 if (filp->f_mode & FMODE_READ)
217 rpci->nreaders++;
218 if (filp->f_mode & FMODE_WRITE)
219 rpci->nwriters++;
220 res = 0;
221 out:
222 mutex_unlock(&inode->i_mutex);
223 return res;
226 static int
227 rpc_pipe_release(struct inode *inode, struct file *filp)
229 struct rpc_inode *rpci = RPC_I(inode);
230 struct rpc_pipe_msg *msg;
231 int last_close;
233 mutex_lock(&inode->i_mutex);
234 if (rpci->ops == NULL)
235 goto out;
236 msg = filp->private_data;
237 if (msg != NULL) {
238 spin_lock(&inode->i_lock);
239 msg->errno = -EAGAIN;
240 list_del_init(&msg->list);
241 spin_unlock(&inode->i_lock);
242 rpci->ops->destroy_msg(msg);
244 if (filp->f_mode & FMODE_WRITE)
245 rpci->nwriters --;
246 if (filp->f_mode & FMODE_READ) {
247 rpci->nreaders --;
248 if (rpci->nreaders == 0) {
249 LIST_HEAD(free_list);
250 spin_lock(&inode->i_lock);
251 list_splice_init(&rpci->pipe, &free_list);
252 rpci->pipelen = 0;
253 spin_unlock(&inode->i_lock);
254 rpc_purge_list(rpci, &free_list,
255 rpci->ops->destroy_msg, -EAGAIN);
258 last_close = rpci->nwriters == 0 && rpci->nreaders == 0;
259 if (last_close && rpci->ops->release_pipe)
260 rpci->ops->release_pipe(inode);
261 out:
262 mutex_unlock(&inode->i_mutex);
263 return 0;
266 static ssize_t
267 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
269 struct inode *inode = filp->f_path.dentry->d_inode;
270 struct rpc_inode *rpci = RPC_I(inode);
271 struct rpc_pipe_msg *msg;
272 int res = 0;
274 mutex_lock(&inode->i_mutex);
275 if (rpci->ops == NULL) {
276 res = -EPIPE;
277 goto out_unlock;
279 msg = filp->private_data;
280 if (msg == NULL) {
281 spin_lock(&inode->i_lock);
282 if (!list_empty(&rpci->pipe)) {
283 msg = list_entry(rpci->pipe.next,
284 struct rpc_pipe_msg,
285 list);
286 list_move(&msg->list, &rpci->in_upcall);
287 rpci->pipelen -= msg->len;
288 filp->private_data = msg;
289 msg->copied = 0;
291 spin_unlock(&inode->i_lock);
292 if (msg == NULL)
293 goto out_unlock;
295 /* NOTE: it is up to the callback to update msg->copied */
296 res = rpci->ops->upcall(filp, msg, buf, len);
297 if (res < 0 || msg->len == msg->copied) {
298 filp->private_data = NULL;
299 spin_lock(&inode->i_lock);
300 list_del_init(&msg->list);
301 spin_unlock(&inode->i_lock);
302 rpci->ops->destroy_msg(msg);
304 out_unlock:
305 mutex_unlock(&inode->i_mutex);
306 return res;
309 static ssize_t
310 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
312 struct inode *inode = filp->f_path.dentry->d_inode;
313 struct rpc_inode *rpci = RPC_I(inode);
314 int res;
316 mutex_lock(&inode->i_mutex);
317 res = -EPIPE;
318 if (rpci->ops != NULL)
319 res = rpci->ops->downcall(filp, buf, len);
320 mutex_unlock(&inode->i_mutex);
321 return res;
324 static unsigned int
325 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
327 struct rpc_inode *rpci;
328 unsigned int mask = 0;
330 rpci = RPC_I(filp->f_path.dentry->d_inode);
331 poll_wait(filp, &rpci->waitq, wait);
333 mask = POLLOUT | POLLWRNORM;
334 if (rpci->ops == NULL)
335 mask |= POLLERR | POLLHUP;
336 if (filp->private_data || !list_empty(&rpci->pipe))
337 mask |= POLLIN | POLLRDNORM;
338 return mask;
341 static long
342 rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
344 struct inode *inode = filp->f_path.dentry->d_inode;
345 struct rpc_inode *rpci = RPC_I(inode);
346 int len;
348 switch (cmd) {
349 case FIONREAD:
350 spin_lock(&inode->i_lock);
351 if (rpci->ops == NULL) {
352 spin_unlock(&inode->i_lock);
353 return -EPIPE;
355 len = rpci->pipelen;
356 if (filp->private_data) {
357 struct rpc_pipe_msg *msg;
358 msg = filp->private_data;
359 len += msg->len - msg->copied;
361 spin_unlock(&inode->i_lock);
362 return put_user(len, (int __user *)arg);
363 default:
364 return -EINVAL;
368 static const struct file_operations rpc_pipe_fops = {
369 .owner = THIS_MODULE,
370 .llseek = no_llseek,
371 .read = rpc_pipe_read,
372 .write = rpc_pipe_write,
373 .poll = rpc_pipe_poll,
374 .unlocked_ioctl = rpc_pipe_ioctl,
375 .open = rpc_pipe_open,
376 .release = rpc_pipe_release,
379 static int
380 rpc_show_info(struct seq_file *m, void *v)
382 struct rpc_clnt *clnt = m->private;
384 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
385 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
386 clnt->cl_prog, clnt->cl_vers);
387 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
388 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
389 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
390 return 0;
393 static int
394 rpc_info_open(struct inode *inode, struct file *file)
396 struct rpc_clnt *clnt = NULL;
397 int ret = single_open(file, rpc_show_info, NULL);
399 if (!ret) {
400 struct seq_file *m = file->private_data;
402 spin_lock(&file->f_path.dentry->d_lock);
403 if (!d_unhashed(file->f_path.dentry))
404 clnt = RPC_I(inode)->private;
405 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
406 spin_unlock(&file->f_path.dentry->d_lock);
407 m->private = clnt;
408 } else {
409 spin_unlock(&file->f_path.dentry->d_lock);
410 single_release(inode, file);
411 ret = -EINVAL;
414 return ret;
417 static int
418 rpc_info_release(struct inode *inode, struct file *file)
420 struct seq_file *m = file->private_data;
421 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
423 if (clnt)
424 rpc_release_client(clnt);
425 return single_release(inode, file);
428 static const struct file_operations rpc_info_operations = {
429 .owner = THIS_MODULE,
430 .open = rpc_info_open,
431 .read = seq_read,
432 .llseek = seq_lseek,
433 .release = rpc_info_release,
438 * Description of fs contents.
440 struct rpc_filelist {
441 const char *name;
442 const struct file_operations *i_fop;
443 umode_t mode;
446 struct vfsmount *rpc_get_mount(void)
448 int err;
450 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
451 if (err != 0)
452 return ERR_PTR(err);
453 return rpc_mnt;
455 EXPORT_SYMBOL_GPL(rpc_get_mount);
457 void rpc_put_mount(void)
459 simple_release_fs(&rpc_mnt, &rpc_mount_count);
461 EXPORT_SYMBOL_GPL(rpc_put_mount);
463 static int rpc_delete_dentry(const struct dentry *dentry)
465 return 1;
468 static const struct dentry_operations rpc_dentry_operations = {
469 .d_delete = rpc_delete_dentry,
472 static struct inode *
473 rpc_get_inode(struct super_block *sb, umode_t mode)
475 struct inode *inode = new_inode(sb);
476 if (!inode)
477 return NULL;
478 inode->i_ino = get_next_ino();
479 inode->i_mode = mode;
480 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
481 switch (mode & S_IFMT) {
482 case S_IFDIR:
483 inode->i_fop = &simple_dir_operations;
484 inode->i_op = &simple_dir_inode_operations;
485 inc_nlink(inode);
486 default:
487 break;
489 return inode;
492 static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
493 umode_t mode,
494 const struct file_operations *i_fop,
495 void *private)
497 struct inode *inode;
499 d_drop(dentry);
500 inode = rpc_get_inode(dir->i_sb, mode);
501 if (!inode)
502 goto out_err;
503 inode->i_ino = iunique(dir->i_sb, 100);
504 if (i_fop)
505 inode->i_fop = i_fop;
506 if (private)
507 rpc_inode_setowner(inode, private);
508 d_add(dentry, inode);
509 return 0;
510 out_err:
511 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
512 __FILE__, __func__, dentry->d_name.name);
513 dput(dentry);
514 return -ENOMEM;
517 static int __rpc_create(struct inode *dir, struct dentry *dentry,
518 umode_t mode,
519 const struct file_operations *i_fop,
520 void *private)
522 int err;
524 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
525 if (err)
526 return err;
527 fsnotify_create(dir, dentry);
528 return 0;
531 static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
532 umode_t mode,
533 const struct file_operations *i_fop,
534 void *private)
536 int err;
538 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
539 if (err)
540 return err;
541 inc_nlink(dir);
542 fsnotify_mkdir(dir, dentry);
543 return 0;
546 static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
547 umode_t mode,
548 const struct file_operations *i_fop,
549 void *private,
550 const struct rpc_pipe_ops *ops,
551 int flags)
553 struct rpc_inode *rpci;
554 int err;
556 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
557 if (err)
558 return err;
559 rpci = RPC_I(dentry->d_inode);
560 rpci->private = private;
561 rpci->flags = flags;
562 rpci->ops = ops;
563 fsnotify_create(dir, dentry);
564 return 0;
567 static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
569 int ret;
571 dget(dentry);
572 ret = simple_rmdir(dir, dentry);
573 d_delete(dentry);
574 dput(dentry);
575 return ret;
578 static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
580 int ret;
582 dget(dentry);
583 ret = simple_unlink(dir, dentry);
584 d_delete(dentry);
585 dput(dentry);
586 return ret;
589 static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
591 struct inode *inode = dentry->d_inode;
593 rpc_close_pipes(inode);
594 return __rpc_unlink(dir, dentry);
597 static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
598 struct qstr *name)
600 struct dentry *dentry;
602 dentry = d_lookup(parent, name);
603 if (!dentry) {
604 dentry = d_alloc(parent, name);
605 if (!dentry)
606 return ERR_PTR(-ENOMEM);
608 if (dentry->d_inode == NULL) {
609 d_set_d_op(dentry, &rpc_dentry_operations);
610 return dentry;
612 dput(dentry);
613 return ERR_PTR(-EEXIST);
617 * FIXME: This probably has races.
619 static void __rpc_depopulate(struct dentry *parent,
620 const struct rpc_filelist *files,
621 int start, int eof)
623 struct inode *dir = parent->d_inode;
624 struct dentry *dentry;
625 struct qstr name;
626 int i;
628 for (i = start; i < eof; i++) {
629 name.name = files[i].name;
630 name.len = strlen(files[i].name);
631 name.hash = full_name_hash(name.name, name.len);
632 dentry = d_lookup(parent, &name);
634 if (dentry == NULL)
635 continue;
636 if (dentry->d_inode == NULL)
637 goto next;
638 switch (dentry->d_inode->i_mode & S_IFMT) {
639 default:
640 BUG();
641 case S_IFREG:
642 __rpc_unlink(dir, dentry);
643 break;
644 case S_IFDIR:
645 __rpc_rmdir(dir, dentry);
647 next:
648 dput(dentry);
652 static void rpc_depopulate(struct dentry *parent,
653 const struct rpc_filelist *files,
654 int start, int eof)
656 struct inode *dir = parent->d_inode;
658 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
659 __rpc_depopulate(parent, files, start, eof);
660 mutex_unlock(&dir->i_mutex);
663 static int rpc_populate(struct dentry *parent,
664 const struct rpc_filelist *files,
665 int start, int eof,
666 void *private)
668 struct inode *dir = parent->d_inode;
669 struct dentry *dentry;
670 int i, err;
672 mutex_lock(&dir->i_mutex);
673 for (i = start; i < eof; i++) {
674 struct qstr q;
676 q.name = files[i].name;
677 q.len = strlen(files[i].name);
678 q.hash = full_name_hash(q.name, q.len);
679 dentry = __rpc_lookup_create_exclusive(parent, &q);
680 err = PTR_ERR(dentry);
681 if (IS_ERR(dentry))
682 goto out_bad;
683 switch (files[i].mode & S_IFMT) {
684 default:
685 BUG();
686 case S_IFREG:
687 err = __rpc_create(dir, dentry,
688 files[i].mode,
689 files[i].i_fop,
690 private);
691 break;
692 case S_IFDIR:
693 err = __rpc_mkdir(dir, dentry,
694 files[i].mode,
695 NULL,
696 private);
698 if (err != 0)
699 goto out_bad;
701 mutex_unlock(&dir->i_mutex);
702 return 0;
703 out_bad:
704 __rpc_depopulate(parent, files, start, eof);
705 mutex_unlock(&dir->i_mutex);
706 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
707 __FILE__, __func__, parent->d_name.name);
708 return err;
711 static struct dentry *rpc_mkdir_populate(struct dentry *parent,
712 struct qstr *name, umode_t mode, void *private,
713 int (*populate)(struct dentry *, void *), void *args_populate)
715 struct dentry *dentry;
716 struct inode *dir = parent->d_inode;
717 int error;
719 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
720 dentry = __rpc_lookup_create_exclusive(parent, name);
721 if (IS_ERR(dentry))
722 goto out;
723 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
724 if (error != 0)
725 goto out_err;
726 if (populate != NULL) {
727 error = populate(dentry, args_populate);
728 if (error)
729 goto err_rmdir;
731 out:
732 mutex_unlock(&dir->i_mutex);
733 return dentry;
734 err_rmdir:
735 __rpc_rmdir(dir, dentry);
736 out_err:
737 dentry = ERR_PTR(error);
738 goto out;
741 static int rpc_rmdir_depopulate(struct dentry *dentry,
742 void (*depopulate)(struct dentry *))
744 struct dentry *parent;
745 struct inode *dir;
746 int error;
748 parent = dget_parent(dentry);
749 dir = parent->d_inode;
750 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
751 if (depopulate != NULL)
752 depopulate(dentry);
753 error = __rpc_rmdir(dir, dentry);
754 mutex_unlock(&dir->i_mutex);
755 dput(parent);
756 return error;
760 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
761 * @parent: dentry of directory to create new "pipe" in
762 * @name: name of pipe
763 * @private: private data to associate with the pipe, for the caller's use
764 * @ops: operations defining the behavior of the pipe: upcall, downcall,
765 * release_pipe, open_pipe, and destroy_msg.
766 * @flags: rpc_inode flags
768 * Data is made available for userspace to read by calls to
769 * rpc_queue_upcall(). The actual reads will result in calls to
770 * @ops->upcall, which will be called with the file pointer,
771 * message, and userspace buffer to copy to.
773 * Writes can come at any time, and do not necessarily have to be
774 * responses to upcalls. They will result in calls to @msg->downcall.
776 * The @private argument passed here will be available to all these methods
777 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
779 struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
780 void *private, const struct rpc_pipe_ops *ops,
781 int flags)
783 struct dentry *dentry;
784 struct inode *dir = parent->d_inode;
785 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
786 struct qstr q;
787 int err;
789 if (ops->upcall == NULL)
790 umode &= ~S_IRUGO;
791 if (ops->downcall == NULL)
792 umode &= ~S_IWUGO;
794 q.name = name;
795 q.len = strlen(name);
796 q.hash = full_name_hash(q.name, q.len),
798 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
799 dentry = __rpc_lookup_create_exclusive(parent, &q);
800 if (IS_ERR(dentry))
801 goto out;
802 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
803 private, ops, flags);
804 if (err)
805 goto out_err;
806 out:
807 mutex_unlock(&dir->i_mutex);
808 return dentry;
809 out_err:
810 dentry = ERR_PTR(err);
811 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
812 __FILE__, __func__, parent->d_name.name, name,
813 err);
814 goto out;
816 EXPORT_SYMBOL_GPL(rpc_mkpipe);
819 * rpc_unlink - remove a pipe
820 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
822 * After this call, lookups will no longer find the pipe, and any
823 * attempts to read or write using preexisting opens of the pipe will
824 * return -EPIPE.
827 rpc_unlink(struct dentry *dentry)
829 struct dentry *parent;
830 struct inode *dir;
831 int error = 0;
833 parent = dget_parent(dentry);
834 dir = parent->d_inode;
835 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
836 error = __rpc_rmpipe(dir, dentry);
837 mutex_unlock(&dir->i_mutex);
838 dput(parent);
839 return error;
841 EXPORT_SYMBOL_GPL(rpc_unlink);
843 enum {
844 RPCAUTH_info,
845 RPCAUTH_EOF
848 static const struct rpc_filelist authfiles[] = {
849 [RPCAUTH_info] = {
850 .name = "info",
851 .i_fop = &rpc_info_operations,
852 .mode = S_IFREG | S_IRUSR,
856 static int rpc_clntdir_populate(struct dentry *dentry, void *private)
858 return rpc_populate(dentry,
859 authfiles, RPCAUTH_info, RPCAUTH_EOF,
860 private);
863 static void rpc_clntdir_depopulate(struct dentry *dentry)
865 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
869 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
870 * @dentry: dentry from the rpc_pipefs root to the new directory
871 * @name: &struct qstr for the name
872 * @rpc_client: rpc client to associate with this directory
874 * This creates a directory at the given @path associated with
875 * @rpc_clnt, which will contain a file named "info" with some basic
876 * information about the client, together with any "pipes" that may
877 * later be created using rpc_mkpipe().
879 struct dentry *rpc_create_client_dir(struct dentry *dentry,
880 struct qstr *name,
881 struct rpc_clnt *rpc_client)
883 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
884 rpc_clntdir_populate, rpc_client);
888 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
889 * @dentry: directory to remove
891 int rpc_remove_client_dir(struct dentry *dentry)
893 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
896 static const struct rpc_filelist cache_pipefs_files[3] = {
897 [0] = {
898 .name = "channel",
899 .i_fop = &cache_file_operations_pipefs,
900 .mode = S_IFREG|S_IRUSR|S_IWUSR,
902 [1] = {
903 .name = "content",
904 .i_fop = &content_file_operations_pipefs,
905 .mode = S_IFREG|S_IRUSR,
907 [2] = {
908 .name = "flush",
909 .i_fop = &cache_flush_operations_pipefs,
910 .mode = S_IFREG|S_IRUSR|S_IWUSR,
914 static int rpc_cachedir_populate(struct dentry *dentry, void *private)
916 return rpc_populate(dentry,
917 cache_pipefs_files, 0, 3,
918 private);
921 static void rpc_cachedir_depopulate(struct dentry *dentry)
923 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
926 struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
927 umode_t umode, struct cache_detail *cd)
929 return rpc_mkdir_populate(parent, name, umode, NULL,
930 rpc_cachedir_populate, cd);
933 void rpc_remove_cache_dir(struct dentry *dentry)
935 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
939 * populate the filesystem
941 static const struct super_operations s_ops = {
942 .alloc_inode = rpc_alloc_inode,
943 .destroy_inode = rpc_destroy_inode,
944 .statfs = simple_statfs,
947 #define RPCAUTH_GSSMAGIC 0x67596969
950 * We have a single directory with 1 node in it.
952 enum {
953 RPCAUTH_lockd,
954 RPCAUTH_mount,
955 RPCAUTH_nfs,
956 RPCAUTH_portmap,
957 RPCAUTH_statd,
958 RPCAUTH_nfsd4_cb,
959 RPCAUTH_cache,
960 RPCAUTH_RootEOF
963 static const struct rpc_filelist files[] = {
964 [RPCAUTH_lockd] = {
965 .name = "lockd",
966 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
968 [RPCAUTH_mount] = {
969 .name = "mount",
970 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
972 [RPCAUTH_nfs] = {
973 .name = "nfs",
974 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
976 [RPCAUTH_portmap] = {
977 .name = "portmap",
978 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
980 [RPCAUTH_statd] = {
981 .name = "statd",
982 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
984 [RPCAUTH_nfsd4_cb] = {
985 .name = "nfsd4_cb",
986 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
988 [RPCAUTH_cache] = {
989 .name = "cache",
990 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
994 static int
995 rpc_fill_super(struct super_block *sb, void *data, int silent)
997 struct inode *inode;
998 struct dentry *root;
999 struct net *net = data;
1001 sb->s_blocksize = PAGE_CACHE_SIZE;
1002 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1003 sb->s_magic = RPCAUTH_GSSMAGIC;
1004 sb->s_op = &s_ops;
1005 sb->s_time_gran = 1;
1007 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1008 if (!inode)
1009 return -ENOMEM;
1010 sb->s_root = root = d_alloc_root(inode);
1011 if (!root) {
1012 iput(inode);
1013 return -ENOMEM;
1015 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
1016 return -ENOMEM;
1017 sb->s_fs_info = get_net(net);
1018 return 0;
1021 static struct dentry *
1022 rpc_mount(struct file_system_type *fs_type,
1023 int flags, const char *dev_name, void *data)
1025 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
1028 void rpc_kill_sb(struct super_block *sb)
1030 struct net *net = sb->s_fs_info;
1032 put_net(net);
1033 kill_litter_super(sb);
1036 static struct file_system_type rpc_pipe_fs_type = {
1037 .owner = THIS_MODULE,
1038 .name = "rpc_pipefs",
1039 .mount = rpc_mount,
1040 .kill_sb = rpc_kill_sb,
1043 static void
1044 init_once(void *foo)
1046 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1048 inode_init_once(&rpci->vfs_inode);
1049 rpci->private = NULL;
1050 rpci->nreaders = 0;
1051 rpci->nwriters = 0;
1052 INIT_LIST_HEAD(&rpci->in_upcall);
1053 INIT_LIST_HEAD(&rpci->in_downcall);
1054 INIT_LIST_HEAD(&rpci->pipe);
1055 rpci->pipelen = 0;
1056 init_waitqueue_head(&rpci->waitq);
1057 INIT_DELAYED_WORK(&rpci->queue_timeout,
1058 rpc_timeout_upcall_queue);
1059 rpci->ops = NULL;
1062 int register_rpc_pipefs(void)
1064 int err;
1066 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
1067 sizeof(struct rpc_inode),
1068 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1069 SLAB_MEM_SPREAD),
1070 init_once);
1071 if (!rpc_inode_cachep)
1072 return -ENOMEM;
1073 err = register_filesystem(&rpc_pipe_fs_type);
1074 if (err) {
1075 kmem_cache_destroy(rpc_inode_cachep);
1076 return err;
1079 return 0;
1082 void unregister_rpc_pipefs(void)
1084 kmem_cache_destroy(rpc_inode_cachep);
1085 unregister_filesystem(&rpc_pipe_fs_type);
1088 /* Make 'mount -t rpc_pipefs ...' autoload this module. */
1089 MODULE_ALIAS("rpc_pipefs");