memblock: Reimplement memblock allocation using reverse free area iterator
[linux-2.6.git] / net / sunrpc / rpc_pipe.c
blobbfddd68b31d392ccc3917cd851667ff972a11e8a
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>
31 static struct vfsmount *rpc_mnt __read_mostly;
32 static int rpc_mount_count;
34 static struct file_system_type rpc_pipe_fs_type;
37 static struct kmem_cache *rpc_inode_cachep __read_mostly;
39 #define RPC_UPCALL_TIMEOUT (30*HZ)
41 static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
42 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
44 struct rpc_pipe_msg *msg;
46 if (list_empty(head))
47 return;
48 do {
49 msg = list_entry(head->next, struct rpc_pipe_msg, list);
50 list_del_init(&msg->list);
51 msg->errno = err;
52 destroy_msg(msg);
53 } while (!list_empty(head));
54 wake_up(&rpci->waitq);
57 static void
58 rpc_timeout_upcall_queue(struct work_struct *work)
60 LIST_HEAD(free_list);
61 struct rpc_inode *rpci =
62 container_of(work, struct rpc_inode, queue_timeout.work);
63 struct inode *inode = &rpci->vfs_inode;
64 void (*destroy_msg)(struct rpc_pipe_msg *);
66 spin_lock(&inode->i_lock);
67 if (rpci->ops == NULL) {
68 spin_unlock(&inode->i_lock);
69 return;
71 destroy_msg = rpci->ops->destroy_msg;
72 if (rpci->nreaders == 0) {
73 list_splice_init(&rpci->pipe, &free_list);
74 rpci->pipelen = 0;
76 spin_unlock(&inode->i_lock);
77 rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
80 ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
81 char __user *dst, size_t buflen)
83 char *data = (char *)msg->data + msg->copied;
84 size_t mlen = min(msg->len - msg->copied, buflen);
85 unsigned long left;
87 left = copy_to_user(dst, data, mlen);
88 if (left == mlen) {
89 msg->errno = -EFAULT;
90 return -EFAULT;
93 mlen -= left;
94 msg->copied += mlen;
95 msg->errno = 0;
96 return mlen;
98 EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
101 * rpc_queue_upcall - queue an upcall message to userspace
102 * @inode: inode of upcall pipe on which to queue given message
103 * @msg: message to queue
105 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
106 * A userspace process may then later read the upcall by performing a
107 * read on an open file for this inode. It is up to the caller to
108 * initialize the fields of @msg (other than @msg->list) appropriately.
111 rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
113 struct rpc_inode *rpci = RPC_I(inode);
114 int res = -EPIPE;
116 spin_lock(&inode->i_lock);
117 if (rpci->ops == NULL)
118 goto out;
119 if (rpci->nreaders) {
120 list_add_tail(&msg->list, &rpci->pipe);
121 rpci->pipelen += msg->len;
122 res = 0;
123 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
124 if (list_empty(&rpci->pipe))
125 queue_delayed_work(rpciod_workqueue,
126 &rpci->queue_timeout,
127 RPC_UPCALL_TIMEOUT);
128 list_add_tail(&msg->list, &rpci->pipe);
129 rpci->pipelen += msg->len;
130 res = 0;
132 out:
133 spin_unlock(&inode->i_lock);
134 wake_up(&rpci->waitq);
135 return res;
137 EXPORT_SYMBOL_GPL(rpc_queue_upcall);
139 static inline void
140 rpc_inode_setowner(struct inode *inode, void *private)
142 RPC_I(inode)->private = private;
145 static void
146 rpc_close_pipes(struct inode *inode)
148 struct rpc_inode *rpci = RPC_I(inode);
149 const struct rpc_pipe_ops *ops;
150 int need_release;
152 mutex_lock(&inode->i_mutex);
153 ops = rpci->ops;
154 if (ops != NULL) {
155 LIST_HEAD(free_list);
156 spin_lock(&inode->i_lock);
157 need_release = rpci->nreaders != 0 || rpci->nwriters != 0;
158 rpci->nreaders = 0;
159 list_splice_init(&rpci->in_upcall, &free_list);
160 list_splice_init(&rpci->pipe, &free_list);
161 rpci->pipelen = 0;
162 rpci->ops = NULL;
163 spin_unlock(&inode->i_lock);
164 rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
165 rpci->nwriters = 0;
166 if (need_release && ops->release_pipe)
167 ops->release_pipe(inode);
168 cancel_delayed_work_sync(&rpci->queue_timeout);
170 rpc_inode_setowner(inode, NULL);
171 mutex_unlock(&inode->i_mutex);
174 static struct inode *
175 rpc_alloc_inode(struct super_block *sb)
177 struct rpc_inode *rpci;
178 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
179 if (!rpci)
180 return NULL;
181 return &rpci->vfs_inode;
184 static void
185 rpc_i_callback(struct rcu_head *head)
187 struct inode *inode = container_of(head, struct inode, i_rcu);
188 INIT_LIST_HEAD(&inode->i_dentry);
189 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
192 static void
193 rpc_destroy_inode(struct inode *inode)
195 call_rcu(&inode->i_rcu, rpc_i_callback);
198 static int
199 rpc_pipe_open(struct inode *inode, struct file *filp)
201 struct rpc_inode *rpci = RPC_I(inode);
202 int first_open;
203 int res = -ENXIO;
205 mutex_lock(&inode->i_mutex);
206 if (rpci->ops == NULL)
207 goto out;
208 first_open = rpci->nreaders == 0 && rpci->nwriters == 0;
209 if (first_open && rpci->ops->open_pipe) {
210 res = rpci->ops->open_pipe(inode);
211 if (res)
212 goto out;
214 if (filp->f_mode & FMODE_READ)
215 rpci->nreaders++;
216 if (filp->f_mode & FMODE_WRITE)
217 rpci->nwriters++;
218 res = 0;
219 out:
220 mutex_unlock(&inode->i_mutex);
221 return res;
224 static int
225 rpc_pipe_release(struct inode *inode, struct file *filp)
227 struct rpc_inode *rpci = RPC_I(inode);
228 struct rpc_pipe_msg *msg;
229 int last_close;
231 mutex_lock(&inode->i_mutex);
232 if (rpci->ops == NULL)
233 goto out;
234 msg = filp->private_data;
235 if (msg != NULL) {
236 spin_lock(&inode->i_lock);
237 msg->errno = -EAGAIN;
238 list_del_init(&msg->list);
239 spin_unlock(&inode->i_lock);
240 rpci->ops->destroy_msg(msg);
242 if (filp->f_mode & FMODE_WRITE)
243 rpci->nwriters --;
244 if (filp->f_mode & FMODE_READ) {
245 rpci->nreaders --;
246 if (rpci->nreaders == 0) {
247 LIST_HEAD(free_list);
248 spin_lock(&inode->i_lock);
249 list_splice_init(&rpci->pipe, &free_list);
250 rpci->pipelen = 0;
251 spin_unlock(&inode->i_lock);
252 rpc_purge_list(rpci, &free_list,
253 rpci->ops->destroy_msg, -EAGAIN);
256 last_close = rpci->nwriters == 0 && rpci->nreaders == 0;
257 if (last_close && rpci->ops->release_pipe)
258 rpci->ops->release_pipe(inode);
259 out:
260 mutex_unlock(&inode->i_mutex);
261 return 0;
264 static ssize_t
265 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
267 struct inode *inode = filp->f_path.dentry->d_inode;
268 struct rpc_inode *rpci = RPC_I(inode);
269 struct rpc_pipe_msg *msg;
270 int res = 0;
272 mutex_lock(&inode->i_mutex);
273 if (rpci->ops == NULL) {
274 res = -EPIPE;
275 goto out_unlock;
277 msg = filp->private_data;
278 if (msg == NULL) {
279 spin_lock(&inode->i_lock);
280 if (!list_empty(&rpci->pipe)) {
281 msg = list_entry(rpci->pipe.next,
282 struct rpc_pipe_msg,
283 list);
284 list_move(&msg->list, &rpci->in_upcall);
285 rpci->pipelen -= msg->len;
286 filp->private_data = msg;
287 msg->copied = 0;
289 spin_unlock(&inode->i_lock);
290 if (msg == NULL)
291 goto out_unlock;
293 /* NOTE: it is up to the callback to update msg->copied */
294 res = rpci->ops->upcall(filp, msg, buf, len);
295 if (res < 0 || msg->len == msg->copied) {
296 filp->private_data = NULL;
297 spin_lock(&inode->i_lock);
298 list_del_init(&msg->list);
299 spin_unlock(&inode->i_lock);
300 rpci->ops->destroy_msg(msg);
302 out_unlock:
303 mutex_unlock(&inode->i_mutex);
304 return res;
307 static ssize_t
308 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
310 struct inode *inode = filp->f_path.dentry->d_inode;
311 struct rpc_inode *rpci = RPC_I(inode);
312 int res;
314 mutex_lock(&inode->i_mutex);
315 res = -EPIPE;
316 if (rpci->ops != NULL)
317 res = rpci->ops->downcall(filp, buf, len);
318 mutex_unlock(&inode->i_mutex);
319 return res;
322 static unsigned int
323 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
325 struct rpc_inode *rpci;
326 unsigned int mask = 0;
328 rpci = RPC_I(filp->f_path.dentry->d_inode);
329 poll_wait(filp, &rpci->waitq, wait);
331 mask = POLLOUT | POLLWRNORM;
332 if (rpci->ops == NULL)
333 mask |= POLLERR | POLLHUP;
334 if (filp->private_data || !list_empty(&rpci->pipe))
335 mask |= POLLIN | POLLRDNORM;
336 return mask;
339 static long
340 rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
342 struct inode *inode = filp->f_path.dentry->d_inode;
343 struct rpc_inode *rpci = RPC_I(inode);
344 int len;
346 switch (cmd) {
347 case FIONREAD:
348 spin_lock(&inode->i_lock);
349 if (rpci->ops == NULL) {
350 spin_unlock(&inode->i_lock);
351 return -EPIPE;
353 len = rpci->pipelen;
354 if (filp->private_data) {
355 struct rpc_pipe_msg *msg;
356 msg = filp->private_data;
357 len += msg->len - msg->copied;
359 spin_unlock(&inode->i_lock);
360 return put_user(len, (int __user *)arg);
361 default:
362 return -EINVAL;
366 static const struct file_operations rpc_pipe_fops = {
367 .owner = THIS_MODULE,
368 .llseek = no_llseek,
369 .read = rpc_pipe_read,
370 .write = rpc_pipe_write,
371 .poll = rpc_pipe_poll,
372 .unlocked_ioctl = rpc_pipe_ioctl,
373 .open = rpc_pipe_open,
374 .release = rpc_pipe_release,
377 static int
378 rpc_show_info(struct seq_file *m, void *v)
380 struct rpc_clnt *clnt = m->private;
382 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
383 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
384 clnt->cl_prog, clnt->cl_vers);
385 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
386 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
387 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
388 return 0;
391 static int
392 rpc_info_open(struct inode *inode, struct file *file)
394 struct rpc_clnt *clnt = NULL;
395 int ret = single_open(file, rpc_show_info, NULL);
397 if (!ret) {
398 struct seq_file *m = file->private_data;
400 spin_lock(&file->f_path.dentry->d_lock);
401 if (!d_unhashed(file->f_path.dentry))
402 clnt = RPC_I(inode)->private;
403 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
404 spin_unlock(&file->f_path.dentry->d_lock);
405 m->private = clnt;
406 } else {
407 spin_unlock(&file->f_path.dentry->d_lock);
408 single_release(inode, file);
409 ret = -EINVAL;
412 return ret;
415 static int
416 rpc_info_release(struct inode *inode, struct file *file)
418 struct seq_file *m = file->private_data;
419 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
421 if (clnt)
422 rpc_release_client(clnt);
423 return single_release(inode, file);
426 static const struct file_operations rpc_info_operations = {
427 .owner = THIS_MODULE,
428 .open = rpc_info_open,
429 .read = seq_read,
430 .llseek = seq_lseek,
431 .release = rpc_info_release,
436 * Description of fs contents.
438 struct rpc_filelist {
439 const char *name;
440 const struct file_operations *i_fop;
441 umode_t mode;
444 struct vfsmount *rpc_get_mount(void)
446 int err;
448 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
449 if (err != 0)
450 return ERR_PTR(err);
451 return rpc_mnt;
453 EXPORT_SYMBOL_GPL(rpc_get_mount);
455 void rpc_put_mount(void)
457 simple_release_fs(&rpc_mnt, &rpc_mount_count);
459 EXPORT_SYMBOL_GPL(rpc_put_mount);
461 static int rpc_delete_dentry(const struct dentry *dentry)
463 return 1;
466 static const struct dentry_operations rpc_dentry_operations = {
467 .d_delete = rpc_delete_dentry,
470 static struct inode *
471 rpc_get_inode(struct super_block *sb, umode_t mode)
473 struct inode *inode = new_inode(sb);
474 if (!inode)
475 return NULL;
476 inode->i_ino = get_next_ino();
477 inode->i_mode = mode;
478 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
479 switch (mode & S_IFMT) {
480 case S_IFDIR:
481 inode->i_fop = &simple_dir_operations;
482 inode->i_op = &simple_dir_inode_operations;
483 inc_nlink(inode);
484 default:
485 break;
487 return inode;
490 static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
491 umode_t mode,
492 const struct file_operations *i_fop,
493 void *private)
495 struct inode *inode;
497 d_drop(dentry);
498 inode = rpc_get_inode(dir->i_sb, mode);
499 if (!inode)
500 goto out_err;
501 inode->i_ino = iunique(dir->i_sb, 100);
502 if (i_fop)
503 inode->i_fop = i_fop;
504 if (private)
505 rpc_inode_setowner(inode, private);
506 d_add(dentry, inode);
507 return 0;
508 out_err:
509 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
510 __FILE__, __func__, dentry->d_name.name);
511 dput(dentry);
512 return -ENOMEM;
515 static int __rpc_create(struct inode *dir, struct dentry *dentry,
516 umode_t mode,
517 const struct file_operations *i_fop,
518 void *private)
520 int err;
522 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
523 if (err)
524 return err;
525 fsnotify_create(dir, dentry);
526 return 0;
529 static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
530 umode_t mode,
531 const struct file_operations *i_fop,
532 void *private)
534 int err;
536 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
537 if (err)
538 return err;
539 inc_nlink(dir);
540 fsnotify_mkdir(dir, dentry);
541 return 0;
544 static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
545 umode_t mode,
546 const struct file_operations *i_fop,
547 void *private,
548 const struct rpc_pipe_ops *ops,
549 int flags)
551 struct rpc_inode *rpci;
552 int err;
554 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
555 if (err)
556 return err;
557 rpci = RPC_I(dentry->d_inode);
558 rpci->nkern_readwriters = 1;
559 rpci->private = private;
560 rpci->flags = flags;
561 rpci->ops = ops;
562 fsnotify_create(dir, dentry);
563 return 0;
566 static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
568 int ret;
570 dget(dentry);
571 ret = simple_rmdir(dir, dentry);
572 d_delete(dentry);
573 dput(dentry);
574 return ret;
577 static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
579 int ret;
581 dget(dentry);
582 ret = simple_unlink(dir, dentry);
583 d_delete(dentry);
584 dput(dentry);
585 return ret;
588 static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
590 struct inode *inode = dentry->d_inode;
591 struct rpc_inode *rpci = RPC_I(inode);
593 rpci->nkern_readwriters--;
594 if (rpci->nkern_readwriters != 0)
595 return 0;
596 rpc_close_pipes(inode);
597 return __rpc_unlink(dir, dentry);
600 static struct dentry *__rpc_lookup_create(struct dentry *parent,
601 struct qstr *name)
603 struct dentry *dentry;
605 dentry = d_lookup(parent, name);
606 if (!dentry) {
607 dentry = d_alloc(parent, name);
608 if (!dentry) {
609 dentry = ERR_PTR(-ENOMEM);
610 goto out_err;
613 if (!dentry->d_inode)
614 d_set_d_op(dentry, &rpc_dentry_operations);
615 out_err:
616 return dentry;
619 static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
620 struct qstr *name)
622 struct dentry *dentry;
624 dentry = __rpc_lookup_create(parent, name);
625 if (IS_ERR(dentry))
626 return dentry;
627 if (dentry->d_inode == NULL)
628 return dentry;
629 dput(dentry);
630 return ERR_PTR(-EEXIST);
634 * FIXME: This probably has races.
636 static void __rpc_depopulate(struct dentry *parent,
637 const struct rpc_filelist *files,
638 int start, int eof)
640 struct inode *dir = parent->d_inode;
641 struct dentry *dentry;
642 struct qstr name;
643 int i;
645 for (i = start; i < eof; i++) {
646 name.name = files[i].name;
647 name.len = strlen(files[i].name);
648 name.hash = full_name_hash(name.name, name.len);
649 dentry = d_lookup(parent, &name);
651 if (dentry == NULL)
652 continue;
653 if (dentry->d_inode == NULL)
654 goto next;
655 switch (dentry->d_inode->i_mode & S_IFMT) {
656 default:
657 BUG();
658 case S_IFREG:
659 __rpc_unlink(dir, dentry);
660 break;
661 case S_IFDIR:
662 __rpc_rmdir(dir, dentry);
664 next:
665 dput(dentry);
669 static void rpc_depopulate(struct dentry *parent,
670 const struct rpc_filelist *files,
671 int start, int eof)
673 struct inode *dir = parent->d_inode;
675 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
676 __rpc_depopulate(parent, files, start, eof);
677 mutex_unlock(&dir->i_mutex);
680 static int rpc_populate(struct dentry *parent,
681 const struct rpc_filelist *files,
682 int start, int eof,
683 void *private)
685 struct inode *dir = parent->d_inode;
686 struct dentry *dentry;
687 int i, err;
689 mutex_lock(&dir->i_mutex);
690 for (i = start; i < eof; i++) {
691 struct qstr q;
693 q.name = files[i].name;
694 q.len = strlen(files[i].name);
695 q.hash = full_name_hash(q.name, q.len);
696 dentry = __rpc_lookup_create_exclusive(parent, &q);
697 err = PTR_ERR(dentry);
698 if (IS_ERR(dentry))
699 goto out_bad;
700 switch (files[i].mode & S_IFMT) {
701 default:
702 BUG();
703 case S_IFREG:
704 err = __rpc_create(dir, dentry,
705 files[i].mode,
706 files[i].i_fop,
707 private);
708 break;
709 case S_IFDIR:
710 err = __rpc_mkdir(dir, dentry,
711 files[i].mode,
712 NULL,
713 private);
715 if (err != 0)
716 goto out_bad;
718 mutex_unlock(&dir->i_mutex);
719 return 0;
720 out_bad:
721 __rpc_depopulate(parent, files, start, eof);
722 mutex_unlock(&dir->i_mutex);
723 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
724 __FILE__, __func__, parent->d_name.name);
725 return err;
728 static struct dentry *rpc_mkdir_populate(struct dentry *parent,
729 struct qstr *name, umode_t mode, void *private,
730 int (*populate)(struct dentry *, void *), void *args_populate)
732 struct dentry *dentry;
733 struct inode *dir = parent->d_inode;
734 int error;
736 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
737 dentry = __rpc_lookup_create_exclusive(parent, name);
738 if (IS_ERR(dentry))
739 goto out;
740 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
741 if (error != 0)
742 goto out_err;
743 if (populate != NULL) {
744 error = populate(dentry, args_populate);
745 if (error)
746 goto err_rmdir;
748 out:
749 mutex_unlock(&dir->i_mutex);
750 return dentry;
751 err_rmdir:
752 __rpc_rmdir(dir, dentry);
753 out_err:
754 dentry = ERR_PTR(error);
755 goto out;
758 static int rpc_rmdir_depopulate(struct dentry *dentry,
759 void (*depopulate)(struct dentry *))
761 struct dentry *parent;
762 struct inode *dir;
763 int error;
765 parent = dget_parent(dentry);
766 dir = parent->d_inode;
767 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
768 if (depopulate != NULL)
769 depopulate(dentry);
770 error = __rpc_rmdir(dir, dentry);
771 mutex_unlock(&dir->i_mutex);
772 dput(parent);
773 return error;
777 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
778 * @parent: dentry of directory to create new "pipe" in
779 * @name: name of pipe
780 * @private: private data to associate with the pipe, for the caller's use
781 * @ops: operations defining the behavior of the pipe: upcall, downcall,
782 * release_pipe, open_pipe, and destroy_msg.
783 * @flags: rpc_inode flags
785 * Data is made available for userspace to read by calls to
786 * rpc_queue_upcall(). The actual reads will result in calls to
787 * @ops->upcall, which will be called with the file pointer,
788 * message, and userspace buffer to copy to.
790 * Writes can come at any time, and do not necessarily have to be
791 * responses to upcalls. They will result in calls to @msg->downcall.
793 * The @private argument passed here will be available to all these methods
794 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
796 struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
797 void *private, const struct rpc_pipe_ops *ops,
798 int flags)
800 struct dentry *dentry;
801 struct inode *dir = parent->d_inode;
802 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
803 struct qstr q;
804 int err;
806 if (ops->upcall == NULL)
807 umode &= ~S_IRUGO;
808 if (ops->downcall == NULL)
809 umode &= ~S_IWUGO;
811 q.name = name;
812 q.len = strlen(name);
813 q.hash = full_name_hash(q.name, q.len),
815 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
816 dentry = __rpc_lookup_create(parent, &q);
817 if (IS_ERR(dentry))
818 goto out;
819 if (dentry->d_inode) {
820 struct rpc_inode *rpci = RPC_I(dentry->d_inode);
821 if (rpci->private != private ||
822 rpci->ops != ops ||
823 rpci->flags != flags) {
824 dput (dentry);
825 err = -EBUSY;
826 goto out_err;
828 rpci->nkern_readwriters++;
829 goto out;
832 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
833 private, ops, flags);
834 if (err)
835 goto out_err;
836 out:
837 mutex_unlock(&dir->i_mutex);
838 return dentry;
839 out_err:
840 dentry = ERR_PTR(err);
841 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
842 __FILE__, __func__, parent->d_name.name, name,
843 err);
844 goto out;
846 EXPORT_SYMBOL_GPL(rpc_mkpipe);
849 * rpc_unlink - remove a pipe
850 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
852 * After this call, lookups will no longer find the pipe, and any
853 * attempts to read or write using preexisting opens of the pipe will
854 * return -EPIPE.
857 rpc_unlink(struct dentry *dentry)
859 struct dentry *parent;
860 struct inode *dir;
861 int error = 0;
863 parent = dget_parent(dentry);
864 dir = parent->d_inode;
865 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
866 error = __rpc_rmpipe(dir, dentry);
867 mutex_unlock(&dir->i_mutex);
868 dput(parent);
869 return error;
871 EXPORT_SYMBOL_GPL(rpc_unlink);
873 enum {
874 RPCAUTH_info,
875 RPCAUTH_EOF
878 static const struct rpc_filelist authfiles[] = {
879 [RPCAUTH_info] = {
880 .name = "info",
881 .i_fop = &rpc_info_operations,
882 .mode = S_IFREG | S_IRUSR,
886 static int rpc_clntdir_populate(struct dentry *dentry, void *private)
888 return rpc_populate(dentry,
889 authfiles, RPCAUTH_info, RPCAUTH_EOF,
890 private);
893 static void rpc_clntdir_depopulate(struct dentry *dentry)
895 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
899 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
900 * @dentry: dentry from the rpc_pipefs root to the new directory
901 * @name: &struct qstr for the name
902 * @rpc_client: rpc client to associate with this directory
904 * This creates a directory at the given @path associated with
905 * @rpc_clnt, which will contain a file named "info" with some basic
906 * information about the client, together with any "pipes" that may
907 * later be created using rpc_mkpipe().
909 struct dentry *rpc_create_client_dir(struct dentry *dentry,
910 struct qstr *name,
911 struct rpc_clnt *rpc_client)
913 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
914 rpc_clntdir_populate, rpc_client);
918 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
919 * @dentry: directory to remove
921 int rpc_remove_client_dir(struct dentry *dentry)
923 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
926 static const struct rpc_filelist cache_pipefs_files[3] = {
927 [0] = {
928 .name = "channel",
929 .i_fop = &cache_file_operations_pipefs,
930 .mode = S_IFREG|S_IRUSR|S_IWUSR,
932 [1] = {
933 .name = "content",
934 .i_fop = &content_file_operations_pipefs,
935 .mode = S_IFREG|S_IRUSR,
937 [2] = {
938 .name = "flush",
939 .i_fop = &cache_flush_operations_pipefs,
940 .mode = S_IFREG|S_IRUSR|S_IWUSR,
944 static int rpc_cachedir_populate(struct dentry *dentry, void *private)
946 return rpc_populate(dentry,
947 cache_pipefs_files, 0, 3,
948 private);
951 static void rpc_cachedir_depopulate(struct dentry *dentry)
953 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
956 struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
957 mode_t umode, struct cache_detail *cd)
959 return rpc_mkdir_populate(parent, name, umode, NULL,
960 rpc_cachedir_populate, cd);
963 void rpc_remove_cache_dir(struct dentry *dentry)
965 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
969 * populate the filesystem
971 static const struct super_operations s_ops = {
972 .alloc_inode = rpc_alloc_inode,
973 .destroy_inode = rpc_destroy_inode,
974 .statfs = simple_statfs,
977 #define RPCAUTH_GSSMAGIC 0x67596969
980 * We have a single directory with 1 node in it.
982 enum {
983 RPCAUTH_lockd,
984 RPCAUTH_mount,
985 RPCAUTH_nfs,
986 RPCAUTH_portmap,
987 RPCAUTH_statd,
988 RPCAUTH_nfsd4_cb,
989 RPCAUTH_cache,
990 RPCAUTH_RootEOF
993 static const struct rpc_filelist files[] = {
994 [RPCAUTH_lockd] = {
995 .name = "lockd",
996 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
998 [RPCAUTH_mount] = {
999 .name = "mount",
1000 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1002 [RPCAUTH_nfs] = {
1003 .name = "nfs",
1004 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1006 [RPCAUTH_portmap] = {
1007 .name = "portmap",
1008 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1010 [RPCAUTH_statd] = {
1011 .name = "statd",
1012 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1014 [RPCAUTH_nfsd4_cb] = {
1015 .name = "nfsd4_cb",
1016 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1018 [RPCAUTH_cache] = {
1019 .name = "cache",
1020 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1024 static int
1025 rpc_fill_super(struct super_block *sb, void *data, int silent)
1027 struct inode *inode;
1028 struct dentry *root;
1030 sb->s_blocksize = PAGE_CACHE_SIZE;
1031 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1032 sb->s_magic = RPCAUTH_GSSMAGIC;
1033 sb->s_op = &s_ops;
1034 sb->s_time_gran = 1;
1036 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1037 if (!inode)
1038 return -ENOMEM;
1039 sb->s_root = root = d_alloc_root(inode);
1040 if (!root) {
1041 iput(inode);
1042 return -ENOMEM;
1044 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
1045 return -ENOMEM;
1046 return 0;
1049 static struct dentry *
1050 rpc_mount(struct file_system_type *fs_type,
1051 int flags, const char *dev_name, void *data)
1053 return mount_single(fs_type, flags, data, rpc_fill_super);
1056 static struct file_system_type rpc_pipe_fs_type = {
1057 .owner = THIS_MODULE,
1058 .name = "rpc_pipefs",
1059 .mount = rpc_mount,
1060 .kill_sb = kill_litter_super,
1063 static void
1064 init_once(void *foo)
1066 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1068 inode_init_once(&rpci->vfs_inode);
1069 rpci->private = NULL;
1070 rpci->nreaders = 0;
1071 rpci->nwriters = 0;
1072 INIT_LIST_HEAD(&rpci->in_upcall);
1073 INIT_LIST_HEAD(&rpci->in_downcall);
1074 INIT_LIST_HEAD(&rpci->pipe);
1075 rpci->pipelen = 0;
1076 init_waitqueue_head(&rpci->waitq);
1077 INIT_DELAYED_WORK(&rpci->queue_timeout,
1078 rpc_timeout_upcall_queue);
1079 rpci->ops = NULL;
1082 int register_rpc_pipefs(void)
1084 int err;
1086 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
1087 sizeof(struct rpc_inode),
1088 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1089 SLAB_MEM_SPREAD),
1090 init_once);
1091 if (!rpc_inode_cachep)
1092 return -ENOMEM;
1093 err = register_filesystem(&rpc_pipe_fs_type);
1094 if (err) {
1095 kmem_cache_destroy(rpc_inode_cachep);
1096 return err;
1099 return 0;
1102 void unregister_rpc_pipefs(void)
1104 kmem_cache_destroy(rpc_inode_cachep);
1105 unregister_filesystem(&rpc_pipe_fs_type);
1108 /* Make 'mount -t rpc_pipefs ...' autoload this module. */
1109 MODULE_ALIAS("rpc_pipefs");