Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/sunrpc/rpc_pipe.c |
| 3 | * |
| 4 | * Userland/kernel interface for rpcauth_gss. |
| 5 | * Code shamelessly plagiarized from fs/nfsd/nfsctl.c |
Rolf Eike Beer | d51fe1b | 2005-09-02 08:59:25 +0200 | [diff] [blame] | 6 | * and fs/sysfs/inode.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no> |
| 9 | * |
| 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Trond Myklebust | 50e437d | 2007-06-07 22:44:34 -0400 | [diff] [blame] | 17 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | |
| 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> |
| 25 | |
| 26 | #include <linux/sunrpc/clnt.h> |
| 27 | #include <linux/workqueue.h> |
| 28 | #include <linux/sunrpc/rpc_pipe_fs.h> |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 29 | #include <linux/sunrpc/cache.h> |
Stanislav Kinsbursky | 021c68d | 2011-12-26 15:39:04 +0300 | [diff] [blame] | 30 | #include <linux/nsproxy.h> |
Stanislav Kinsbursky | 2d00131 | 2011-12-26 15:39:13 +0300 | [diff] [blame] | 31 | #include <linux/notifier.h> |
Stanislav Kinsbursky | 021c68d | 2011-12-26 15:39:04 +0300 | [diff] [blame] | 32 | |
| 33 | #include "netns.h" |
Stanislav Kinsbursky | 2d00131 | 2011-12-26 15:39:13 +0300 | [diff] [blame] | 34 | #include "sunrpc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Stanislav Kinsbursky | efc46bf | 2011-12-26 15:39:47 +0300 | [diff] [blame^] | 36 | #define RPCDBG_FACILITY RPCDBG_DEBUG |
| 37 | |
| 38 | #define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "") |
| 39 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 40 | static struct vfsmount *rpc_mnt __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static int rpc_mount_count; |
| 42 | |
| 43 | static struct file_system_type rpc_pipe_fs_type; |
| 44 | |
| 45 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 46 | static struct kmem_cache *rpc_inode_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #define RPC_UPCALL_TIMEOUT (30*HZ) |
| 49 | |
Stanislav Kinsbursky | 2d00131 | 2011-12-26 15:39:13 +0300 | [diff] [blame] | 50 | static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list); |
| 51 | |
| 52 | int rpc_pipefs_notifier_register(struct notifier_block *nb) |
| 53 | { |
| 54 | return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb); |
| 55 | } |
| 56 | EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register); |
| 57 | |
| 58 | void rpc_pipefs_notifier_unregister(struct notifier_block *nb) |
| 59 | { |
| 60 | blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb); |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister); |
| 63 | |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 64 | static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head, |
| 65 | void (*destroy_msg)(struct rpc_pipe_msg *), int err) |
Trond Myklebust | b3eb67a | 2005-11-25 17:10:11 -0500 | [diff] [blame] | 66 | { |
| 67 | struct rpc_pipe_msg *msg; |
Trond Myklebust | b3eb67a | 2005-11-25 17:10:11 -0500 | [diff] [blame] | 68 | |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 69 | if (list_empty(head)) |
| 70 | return; |
| 71 | do { |
Trond Myklebust | b3eb67a | 2005-11-25 17:10:11 -0500 | [diff] [blame] | 72 | msg = list_entry(head->next, struct rpc_pipe_msg, list); |
Trond Myklebust | 5a67657 | 2010-09-12 19:55:25 -0400 | [diff] [blame] | 73 | list_del_init(&msg->list); |
Trond Myklebust | b3eb67a | 2005-11-25 17:10:11 -0500 | [diff] [blame] | 74 | msg->errno = err; |
| 75 | destroy_msg(msg); |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 76 | } while (!list_empty(head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | wake_up(&rpci->waitq); |
| 78 | } |
| 79 | |
| 80 | static void |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 81 | rpc_timeout_upcall_queue(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 83 | LIST_HEAD(free_list); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 84 | struct rpc_inode *rpci = |
| 85 | container_of(work, struct rpc_inode, queue_timeout.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | struct inode *inode = &rpci->vfs_inode; |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 87 | void (*destroy_msg)(struct rpc_pipe_msg *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 89 | spin_lock(&inode->i_lock); |
| 90 | if (rpci->ops == NULL) { |
| 91 | spin_unlock(&inode->i_lock); |
| 92 | return; |
| 93 | } |
| 94 | destroy_msg = rpci->ops->destroy_msg; |
| 95 | if (rpci->nreaders == 0) { |
| 96 | list_splice_init(&rpci->pipe, &free_list); |
| 97 | rpci->pipelen = 0; |
| 98 | } |
| 99 | spin_unlock(&inode->i_lock); |
| 100 | rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Peng Tao | c122515 | 2011-09-22 21:50:10 -0400 | [diff] [blame] | 103 | ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg, |
| 104 | char __user *dst, size_t buflen) |
| 105 | { |
| 106 | char *data = (char *)msg->data + msg->copied; |
| 107 | size_t mlen = min(msg->len - msg->copied, buflen); |
| 108 | unsigned long left; |
| 109 | |
| 110 | left = copy_to_user(dst, data, mlen); |
| 111 | if (left == mlen) { |
| 112 | msg->errno = -EFAULT; |
| 113 | return -EFAULT; |
| 114 | } |
| 115 | |
| 116 | mlen -= left; |
| 117 | msg->copied += mlen; |
| 118 | msg->errno = 0; |
| 119 | return mlen; |
| 120 | } |
| 121 | EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall); |
| 122 | |
J. Bruce Fields | 93a44a7 | 2007-11-06 13:06:03 -0500 | [diff] [blame] | 123 | /** |
Ben Hutchings | 1a5778a | 2010-02-14 22:35:47 -0800 | [diff] [blame] | 124 | * rpc_queue_upcall - queue an upcall message to userspace |
J. Bruce Fields | 93a44a7 | 2007-11-06 13:06:03 -0500 | [diff] [blame] | 125 | * @inode: inode of upcall pipe on which to queue given message |
| 126 | * @msg: message to queue |
| 127 | * |
| 128 | * Call with an @inode created by rpc_mkpipe() to queue an upcall. |
| 129 | * A userspace process may then later read the upcall by performing a |
| 130 | * read on an open file for this inode. It is up to the caller to |
| 131 | * initialize the fields of @msg (other than @msg->list) appropriately. |
| 132 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | int |
| 134 | rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg) |
| 135 | { |
| 136 | struct rpc_inode *rpci = RPC_I(inode); |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 137 | int res = -EPIPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 139 | spin_lock(&inode->i_lock); |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 140 | if (rpci->ops == NULL) |
| 141 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | if (rpci->nreaders) { |
| 143 | list_add_tail(&msg->list, &rpci->pipe); |
| 144 | rpci->pipelen += msg->len; |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 145 | res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) { |
| 147 | if (list_empty(&rpci->pipe)) |
Trond Myklebust | 24c5d9d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 148 | queue_delayed_work(rpciod_workqueue, |
| 149 | &rpci->queue_timeout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | RPC_UPCALL_TIMEOUT); |
| 151 | list_add_tail(&msg->list, &rpci->pipe); |
| 152 | rpci->pipelen += msg->len; |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 153 | res = 0; |
| 154 | } |
| 155 | out: |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 156 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | wake_up(&rpci->waitq); |
| 158 | return res; |
| 159 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 160 | EXPORT_SYMBOL_GPL(rpc_queue_upcall); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 162 | static inline void |
| 163 | rpc_inode_setowner(struct inode *inode, void *private) |
| 164 | { |
| 165 | RPC_I(inode)->private = private; |
| 166 | } |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | static void |
| 169 | rpc_close_pipes(struct inode *inode) |
| 170 | { |
| 171 | struct rpc_inode *rpci = RPC_I(inode); |
Trond Myklebust | b693ba4 | 2009-08-09 15:14:15 -0400 | [diff] [blame] | 172 | const struct rpc_pipe_ops *ops; |
\"J. Bruce Fields\ | e712804 | 2008-12-23 16:09:47 -0500 | [diff] [blame] | 173 | int need_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 175 | mutex_lock(&inode->i_mutex); |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 176 | ops = rpci->ops; |
| 177 | if (ops != NULL) { |
| 178 | LIST_HEAD(free_list); |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 179 | spin_lock(&inode->i_lock); |
\"J. Bruce Fields\ | e712804 | 2008-12-23 16:09:47 -0500 | [diff] [blame] | 180 | need_release = rpci->nreaders != 0 || rpci->nwriters != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | rpci->nreaders = 0; |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 182 | list_splice_init(&rpci->in_upcall, &free_list); |
| 183 | list_splice_init(&rpci->pipe, &free_list); |
| 184 | rpci->pipelen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | rpci->ops = NULL; |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 186 | spin_unlock(&inode->i_lock); |
| 187 | rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE); |
| 188 | rpci->nwriters = 0; |
\"J. Bruce Fields\ | e712804 | 2008-12-23 16:09:47 -0500 | [diff] [blame] | 189 | if (need_release && ops->release_pipe) |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 190 | ops->release_pipe(inode); |
Trond Myklebust | 4011cd9 | 2007-08-07 15:33:01 -0400 | [diff] [blame] | 191 | cancel_delayed_work_sync(&rpci->queue_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 193 | rpc_inode_setowner(inode, NULL); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 194 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | static struct inode * |
| 198 | rpc_alloc_inode(struct super_block *sb) |
| 199 | { |
| 200 | struct rpc_inode *rpci; |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 201 | rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | if (!rpci) |
| 203 | return NULL; |
| 204 | return &rpci->vfs_inode; |
| 205 | } |
| 206 | |
| 207 | static void |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 208 | rpc_i_callback(struct rcu_head *head) |
| 209 | { |
| 210 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 211 | kmem_cache_free(rpc_inode_cachep, RPC_I(inode)); |
| 212 | } |
| 213 | |
| 214 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | rpc_destroy_inode(struct inode *inode) |
| 216 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 217 | call_rcu(&inode->i_rcu, rpc_i_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int |
| 221 | rpc_pipe_open(struct inode *inode, struct file *filp) |
| 222 | { |
| 223 | struct rpc_inode *rpci = RPC_I(inode); |
\"J. Bruce Fields\ | c381060 | 2008-12-23 16:08:32 -0500 | [diff] [blame] | 224 | int first_open; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | int res = -ENXIO; |
| 226 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 227 | mutex_lock(&inode->i_mutex); |
\"J. Bruce Fields\ | c381060 | 2008-12-23 16:08:32 -0500 | [diff] [blame] | 228 | if (rpci->ops == NULL) |
| 229 | goto out; |
| 230 | first_open = rpci->nreaders == 0 && rpci->nwriters == 0; |
| 231 | if (first_open && rpci->ops->open_pipe) { |
| 232 | res = rpci->ops->open_pipe(inode); |
| 233 | if (res) |
| 234 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
\"J. Bruce Fields\ | c381060 | 2008-12-23 16:08:32 -0500 | [diff] [blame] | 236 | if (filp->f_mode & FMODE_READ) |
| 237 | rpci->nreaders++; |
| 238 | if (filp->f_mode & FMODE_WRITE) |
| 239 | rpci->nwriters++; |
| 240 | res = 0; |
| 241 | out: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 242 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return res; |
| 244 | } |
| 245 | |
| 246 | static int |
| 247 | rpc_pipe_release(struct inode *inode, struct file *filp) |
| 248 | { |
Trond Myklebust | 969b7f2 | 2006-01-03 09:55:36 +0100 | [diff] [blame] | 249 | struct rpc_inode *rpci = RPC_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | struct rpc_pipe_msg *msg; |
\"J. Bruce Fields\ | e712804 | 2008-12-23 16:09:47 -0500 | [diff] [blame] | 251 | int last_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 253 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | if (rpci->ops == NULL) |
| 255 | goto out; |
Joe Perches | 655b5bb | 2010-09-04 18:52:53 -0700 | [diff] [blame] | 256 | msg = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (msg != NULL) { |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 258 | spin_lock(&inode->i_lock); |
Trond Myklebust | 48e4918 | 2005-12-19 17:11:22 -0500 | [diff] [blame] | 259 | msg->errno = -EAGAIN; |
Trond Myklebust | 5a67657 | 2010-09-12 19:55:25 -0400 | [diff] [blame] | 260 | list_del_init(&msg->list); |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 261 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | rpci->ops->destroy_msg(msg); |
| 263 | } |
| 264 | if (filp->f_mode & FMODE_WRITE) |
| 265 | rpci->nwriters --; |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 266 | if (filp->f_mode & FMODE_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | rpci->nreaders --; |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 268 | if (rpci->nreaders == 0) { |
| 269 | LIST_HEAD(free_list); |
| 270 | spin_lock(&inode->i_lock); |
| 271 | list_splice_init(&rpci->pipe, &free_list); |
| 272 | rpci->pipelen = 0; |
| 273 | spin_unlock(&inode->i_lock); |
| 274 | rpc_purge_list(rpci, &free_list, |
| 275 | rpci->ops->destroy_msg, -EAGAIN); |
| 276 | } |
| 277 | } |
\"J. Bruce Fields\ | e712804 | 2008-12-23 16:09:47 -0500 | [diff] [blame] | 278 | last_close = rpci->nwriters == 0 && rpci->nreaders == 0; |
| 279 | if (last_close && rpci->ops->release_pipe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | rpci->ops->release_pipe(inode); |
| 281 | out: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 282 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static ssize_t |
| 287 | rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset) |
| 288 | { |
Josef Sipek | 303b46b | 2006-12-08 02:37:42 -0800 | [diff] [blame] | 289 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | struct rpc_inode *rpci = RPC_I(inode); |
| 291 | struct rpc_pipe_msg *msg; |
| 292 | int res = 0; |
| 293 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 294 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (rpci->ops == NULL) { |
| 296 | res = -EPIPE; |
| 297 | goto out_unlock; |
| 298 | } |
| 299 | msg = filp->private_data; |
| 300 | if (msg == NULL) { |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 301 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (!list_empty(&rpci->pipe)) { |
| 303 | msg = list_entry(rpci->pipe.next, |
| 304 | struct rpc_pipe_msg, |
| 305 | list); |
| 306 | list_move(&msg->list, &rpci->in_upcall); |
| 307 | rpci->pipelen -= msg->len; |
| 308 | filp->private_data = msg; |
| 309 | msg->copied = 0; |
| 310 | } |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 311 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | if (msg == NULL) |
| 313 | goto out_unlock; |
| 314 | } |
| 315 | /* NOTE: it is up to the callback to update msg->copied */ |
| 316 | res = rpci->ops->upcall(filp, msg, buf, len); |
| 317 | if (res < 0 || msg->len == msg->copied) { |
| 318 | filp->private_data = NULL; |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 319 | spin_lock(&inode->i_lock); |
Trond Myklebust | 5a67657 | 2010-09-12 19:55:25 -0400 | [diff] [blame] | 320 | list_del_init(&msg->list); |
Trond Myklebust | 9842ef3 | 2006-02-01 12:18:44 -0500 | [diff] [blame] | 321 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | rpci->ops->destroy_msg(msg); |
| 323 | } |
| 324 | out_unlock: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 325 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | return res; |
| 327 | } |
| 328 | |
| 329 | static ssize_t |
| 330 | rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset) |
| 331 | { |
Josef Sipek | 303b46b | 2006-12-08 02:37:42 -0800 | [diff] [blame] | 332 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | struct rpc_inode *rpci = RPC_I(inode); |
| 334 | int res; |
| 335 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 336 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | res = -EPIPE; |
| 338 | if (rpci->ops != NULL) |
| 339 | res = rpci->ops->downcall(filp, buf, len); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 340 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return res; |
| 342 | } |
| 343 | |
| 344 | static unsigned int |
| 345 | rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait) |
| 346 | { |
| 347 | struct rpc_inode *rpci; |
| 348 | unsigned int mask = 0; |
| 349 | |
Josef Sipek | 303b46b | 2006-12-08 02:37:42 -0800 | [diff] [blame] | 350 | rpci = RPC_I(filp->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | poll_wait(filp, &rpci->waitq, wait); |
| 352 | |
| 353 | mask = POLLOUT | POLLWRNORM; |
| 354 | if (rpci->ops == NULL) |
| 355 | mask |= POLLERR | POLLHUP; |
J. Bruce Fields | eda4f9b | 2007-11-06 13:05:36 -0500 | [diff] [blame] | 356 | if (filp->private_data || !list_empty(&rpci->pipe)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | mask |= POLLIN | POLLRDNORM; |
| 358 | return mask; |
| 359 | } |
| 360 | |
Arnd Bergmann | a6f8dbc | 2010-10-04 21:18:23 +0200 | [diff] [blame] | 361 | static long |
| 362 | rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
Arnd Bergmann | a6f8dbc | 2010-10-04 21:18:23 +0200 | [diff] [blame] | 364 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 365 | struct rpc_inode *rpci = RPC_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | int len; |
| 367 | |
| 368 | switch (cmd) { |
| 369 | case FIONREAD: |
Arnd Bergmann | a6f8dbc | 2010-10-04 21:18:23 +0200 | [diff] [blame] | 370 | spin_lock(&inode->i_lock); |
| 371 | if (rpci->ops == NULL) { |
| 372 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return -EPIPE; |
Arnd Bergmann | a6f8dbc | 2010-10-04 21:18:23 +0200 | [diff] [blame] | 374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | len = rpci->pipelen; |
| 376 | if (filp->private_data) { |
| 377 | struct rpc_pipe_msg *msg; |
Joe Perches | 655b5bb | 2010-09-04 18:52:53 -0700 | [diff] [blame] | 378 | msg = filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | len += msg->len - msg->copied; |
| 380 | } |
Arnd Bergmann | a6f8dbc | 2010-10-04 21:18:23 +0200 | [diff] [blame] | 381 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | return put_user(len, (int __user *)arg); |
| 383 | default: |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | } |
| 387 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 388 | static const struct file_operations rpc_pipe_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | .owner = THIS_MODULE, |
| 390 | .llseek = no_llseek, |
| 391 | .read = rpc_pipe_read, |
| 392 | .write = rpc_pipe_write, |
| 393 | .poll = rpc_pipe_poll, |
Frederic Weisbecker | 674b604 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 394 | .unlocked_ioctl = rpc_pipe_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | .open = rpc_pipe_open, |
| 396 | .release = rpc_pipe_release, |
| 397 | }; |
| 398 | |
| 399 | static int |
| 400 | rpc_show_info(struct seq_file *m, void *v) |
| 401 | { |
| 402 | struct rpc_clnt *clnt = m->private; |
| 403 | |
| 404 | seq_printf(m, "RPC server: %s\n", clnt->cl_server); |
| 405 | seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname, |
| 406 | clnt->cl_prog, clnt->cl_vers); |
Chuck Lever | e7f7865 | 2006-08-22 20:06:19 -0400 | [diff] [blame] | 407 | seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR)); |
| 408 | seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO)); |
J. Bruce Fields | bf19aac | 2007-09-26 14:38:09 -0400 | [diff] [blame] | 409 | seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static int |
| 414 | rpc_info_open(struct inode *inode, struct file *file) |
| 415 | { |
Trond Myklebust | 006abe8 | 2010-09-12 19:55:25 -0400 | [diff] [blame] | 416 | struct rpc_clnt *clnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | int ret = single_open(file, rpc_show_info, NULL); |
| 418 | |
| 419 | if (!ret) { |
| 420 | struct seq_file *m = file->private_data; |
Trond Myklebust | 006abe8 | 2010-09-12 19:55:25 -0400 | [diff] [blame] | 421 | |
| 422 | spin_lock(&file->f_path.dentry->d_lock); |
| 423 | if (!d_unhashed(file->f_path.dentry)) |
| 424 | clnt = RPC_I(inode)->private; |
| 425 | if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) { |
| 426 | spin_unlock(&file->f_path.dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | m->private = clnt; |
| 428 | } else { |
Trond Myklebust | 006abe8 | 2010-09-12 19:55:25 -0400 | [diff] [blame] | 429 | spin_unlock(&file->f_path.dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | single_release(inode, file); |
| 431 | ret = -EINVAL; |
| 432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static int |
| 438 | rpc_info_release(struct inode *inode, struct file *file) |
| 439 | { |
| 440 | struct seq_file *m = file->private_data; |
| 441 | struct rpc_clnt *clnt = (struct rpc_clnt *)m->private; |
| 442 | |
| 443 | if (clnt) |
| 444 | rpc_release_client(clnt); |
| 445 | return single_release(inode, file); |
| 446 | } |
| 447 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 448 | static const struct file_operations rpc_info_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | .owner = THIS_MODULE, |
| 450 | .open = rpc_info_open, |
| 451 | .read = seq_read, |
| 452 | .llseek = seq_lseek, |
| 453 | .release = rpc_info_release, |
| 454 | }; |
| 455 | |
| 456 | |
| 457 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | * Description of fs contents. |
| 459 | */ |
| 460 | struct rpc_filelist { |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 461 | const char *name; |
Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 462 | const struct file_operations *i_fop; |
Trond Myklebust | 7364af6 | 2009-08-09 15:14:16 -0400 | [diff] [blame] | 463 | umode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | }; |
| 465 | |
Trond Myklebust | 5428154 | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 466 | struct vfsmount *rpc_get_mount(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Trond Myklebust | 5428154 | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 468 | int err; |
| 469 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 470 | err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count); |
Trond Myklebust | 5428154 | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 471 | if (err != 0) |
| 472 | return ERR_PTR(err); |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 473 | return rpc_mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 475 | EXPORT_SYMBOL_GPL(rpc_get_mount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Trond Myklebust | 5428154 | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 477 | void rpc_put_mount(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 479 | simple_release_fs(&rpc_mnt, &rpc_mount_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 481 | EXPORT_SYMBOL_GPL(rpc_put_mount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 483 | static int rpc_delete_dentry(const struct dentry *dentry) |
Trond Myklebust | 62e1761 | 2007-06-08 14:14:46 -0400 | [diff] [blame] | 484 | { |
| 485 | return 1; |
| 486 | } |
| 487 | |
Al Viro | 3ba13d1 | 2009-02-20 06:02:22 +0000 | [diff] [blame] | 488 | static const struct dentry_operations rpc_dentry_operations = { |
Trond Myklebust | 62e1761 | 2007-06-08 14:14:46 -0400 | [diff] [blame] | 489 | .d_delete = rpc_delete_dentry, |
| 490 | }; |
| 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | static struct inode * |
Trond Myklebust | 7364af6 | 2009-08-09 15:14:16 -0400 | [diff] [blame] | 493 | rpc_get_inode(struct super_block *sb, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
| 495 | struct inode *inode = new_inode(sb); |
| 496 | if (!inode) |
| 497 | return NULL; |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 498 | inode->i_ino = get_next_ino(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | inode->i_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Joe Perches | 89f0e4f | 2011-07-01 09:43:12 +0000 | [diff] [blame] | 501 | switch (mode & S_IFMT) { |
| 502 | case S_IFDIR: |
| 503 | inode->i_fop = &simple_dir_operations; |
| 504 | inode->i_op = &simple_dir_inode_operations; |
| 505 | inc_nlink(inode); |
| 506 | default: |
| 507 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
| 509 | return inode; |
| 510 | } |
| 511 | |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 512 | static int __rpc_create_common(struct inode *dir, struct dentry *dentry, |
| 513 | umode_t mode, |
| 514 | const struct file_operations *i_fop, |
| 515 | void *private) |
| 516 | { |
| 517 | struct inode *inode; |
| 518 | |
Trond Myklebust | beb0f0a | 2010-12-20 21:19:26 +0000 | [diff] [blame] | 519 | d_drop(dentry); |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 520 | inode = rpc_get_inode(dir->i_sb, mode); |
| 521 | if (!inode) |
| 522 | goto out_err; |
| 523 | inode->i_ino = iunique(dir->i_sb, 100); |
| 524 | if (i_fop) |
| 525 | inode->i_fop = i_fop; |
| 526 | if (private) |
| 527 | rpc_inode_setowner(inode, private); |
| 528 | d_add(dentry, inode); |
| 529 | return 0; |
| 530 | out_err: |
| 531 | printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n", |
| 532 | __FILE__, __func__, dentry->d_name.name); |
| 533 | dput(dentry); |
| 534 | return -ENOMEM; |
| 535 | } |
| 536 | |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 537 | static int __rpc_create(struct inode *dir, struct dentry *dentry, |
| 538 | umode_t mode, |
| 539 | const struct file_operations *i_fop, |
| 540 | void *private) |
| 541 | { |
| 542 | int err; |
| 543 | |
| 544 | err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private); |
| 545 | if (err) |
| 546 | return err; |
| 547 | fsnotify_create(dir, dentry); |
| 548 | return 0; |
| 549 | } |
| 550 | |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 551 | static int __rpc_mkdir(struct inode *dir, struct dentry *dentry, |
| 552 | umode_t mode, |
| 553 | const struct file_operations *i_fop, |
| 554 | void *private) |
| 555 | { |
| 556 | int err; |
| 557 | |
| 558 | err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private); |
| 559 | if (err) |
| 560 | return err; |
| 561 | inc_nlink(dir); |
| 562 | fsnotify_mkdir(dir, dentry); |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry, |
| 567 | umode_t mode, |
| 568 | const struct file_operations *i_fop, |
| 569 | void *private, |
| 570 | const struct rpc_pipe_ops *ops, |
| 571 | int flags) |
| 572 | { |
| 573 | struct rpc_inode *rpci; |
| 574 | int err; |
| 575 | |
| 576 | err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private); |
| 577 | if (err) |
| 578 | return err; |
| 579 | rpci = RPC_I(dentry->d_inode); |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 580 | rpci->private = private; |
| 581 | rpci->flags = flags; |
| 582 | rpci->ops = ops; |
| 583 | fsnotify_create(dir, dentry); |
| 584 | return 0; |
| 585 | } |
| 586 | |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 587 | static int __rpc_rmdir(struct inode *dir, struct dentry *dentry) |
| 588 | { |
| 589 | int ret; |
| 590 | |
| 591 | dget(dentry); |
| 592 | ret = simple_rmdir(dir, dentry); |
| 593 | d_delete(dentry); |
| 594 | dput(dentry); |
| 595 | return ret; |
| 596 | } |
| 597 | |
Trond Myklebust | 810d90b | 2009-08-09 15:14:18 -0400 | [diff] [blame] | 598 | static int __rpc_unlink(struct inode *dir, struct dentry *dentry) |
| 599 | { |
| 600 | int ret; |
| 601 | |
| 602 | dget(dentry); |
| 603 | ret = simple_unlink(dir, dentry); |
| 604 | d_delete(dentry); |
| 605 | dput(dentry); |
| 606 | return ret; |
| 607 | } |
| 608 | |
| 609 | static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry) |
| 610 | { |
| 611 | struct inode *inode = dentry->d_inode; |
Trond Myklebust | 810d90b | 2009-08-09 15:14:18 -0400 | [diff] [blame] | 612 | |
Trond Myklebust | 810d90b | 2009-08-09 15:14:18 -0400 | [diff] [blame] | 613 | rpc_close_pipes(inode); |
| 614 | return __rpc_unlink(dir, dentry); |
| 615 | } |
| 616 | |
Trond Myklebust | cfeaa4a | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 617 | static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent, |
| 618 | struct qstr *name) |
| 619 | { |
| 620 | struct dentry *dentry; |
| 621 | |
Stanislav Kinsbursky | 5bff038 | 2011-11-08 15:09:19 +0300 | [diff] [blame] | 622 | dentry = d_lookup(parent, name); |
| 623 | if (!dentry) { |
| 624 | dentry = d_alloc(parent, name); |
| 625 | if (!dentry) |
| 626 | return ERR_PTR(-ENOMEM); |
| 627 | } |
| 628 | if (dentry->d_inode == NULL) { |
| 629 | d_set_d_op(dentry, &rpc_dentry_operations); |
Dan Carpenter | f1f0abe1 | 2010-03-21 12:10:34 -0400 | [diff] [blame] | 630 | return dentry; |
Stanislav Kinsbursky | 5bff038 | 2011-11-08 15:09:19 +0300 | [diff] [blame] | 631 | } |
Trond Myklebust | cfeaa4a | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 632 | dput(dentry); |
| 633 | return ERR_PTR(-EEXIST); |
| 634 | } |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | /* |
| 637 | * FIXME: This probably has races. |
| 638 | */ |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 639 | static void __rpc_depopulate(struct dentry *parent, |
| 640 | const struct rpc_filelist *files, |
| 641 | int start, int eof) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
| 643 | struct inode *dir = parent->d_inode; |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 644 | struct dentry *dentry; |
| 645 | struct qstr name; |
| 646 | int i; |
| 647 | |
| 648 | for (i = start; i < eof; i++) { |
| 649 | name.name = files[i].name; |
| 650 | name.len = strlen(files[i].name); |
| 651 | name.hash = full_name_hash(name.name, name.len); |
| 652 | dentry = d_lookup(parent, &name); |
| 653 | |
| 654 | if (dentry == NULL) |
| 655 | continue; |
| 656 | if (dentry->d_inode == NULL) |
| 657 | goto next; |
| 658 | switch (dentry->d_inode->i_mode & S_IFMT) { |
| 659 | default: |
| 660 | BUG(); |
| 661 | case S_IFREG: |
| 662 | __rpc_unlink(dir, dentry); |
| 663 | break; |
| 664 | case S_IFDIR: |
| 665 | __rpc_rmdir(dir, dentry); |
| 666 | } |
| 667 | next: |
| 668 | dput(dentry); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | static void rpc_depopulate(struct dentry *parent, |
| 673 | const struct rpc_filelist *files, |
| 674 | int start, int eof) |
| 675 | { |
| 676 | struct inode *dir = parent->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
Arjan van de Ven | c6573c2 | 2006-07-03 00:25:16 -0700 | [diff] [blame] | 678 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD); |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 679 | __rpc_depopulate(parent, files, start, eof); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 680 | mutex_unlock(&dir->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 683 | static int rpc_populate(struct dentry *parent, |
| 684 | const struct rpc_filelist *files, |
| 685 | int start, int eof, |
| 686 | void *private) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | { |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 688 | struct inode *dir = parent->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | struct dentry *dentry; |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 690 | int i, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 692 | mutex_lock(&dir->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | for (i = start; i < eof; i++) { |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 694 | struct qstr q; |
| 695 | |
| 696 | q.name = files[i].name; |
| 697 | q.len = strlen(files[i].name); |
| 698 | q.hash = full_name_hash(q.name, q.len); |
| 699 | dentry = __rpc_lookup_create_exclusive(parent, &q); |
| 700 | err = PTR_ERR(dentry); |
| 701 | if (IS_ERR(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | goto out_bad; |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 703 | switch (files[i].mode & S_IFMT) { |
| 704 | default: |
| 705 | BUG(); |
| 706 | case S_IFREG: |
| 707 | err = __rpc_create(dir, dentry, |
| 708 | files[i].mode, |
| 709 | files[i].i_fop, |
| 710 | private); |
| 711 | break; |
| 712 | case S_IFDIR: |
| 713 | err = __rpc_mkdir(dir, dentry, |
| 714 | files[i].mode, |
| 715 | NULL, |
| 716 | private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 718 | if (err != 0) |
| 719 | goto out_bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 721 | mutex_unlock(&dir->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | return 0; |
| 723 | out_bad: |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 724 | __rpc_depopulate(parent, files, start, eof); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 725 | mutex_unlock(&dir->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | printk(KERN_WARNING "%s: %s failed to populate directory %s\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 727 | __FILE__, __func__, parent->d_name.name); |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 728 | return err; |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 729 | } |
Christoph Hellwig | 278c995 | 2005-07-24 23:53:01 +0100 | [diff] [blame] | 730 | |
Trond Myklebust | e57aed7 | 2009-08-09 15:14:26 -0400 | [diff] [blame] | 731 | static struct dentry *rpc_mkdir_populate(struct dentry *parent, |
| 732 | struct qstr *name, umode_t mode, void *private, |
| 733 | int (*populate)(struct dentry *, void *), void *args_populate) |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 734 | { |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 735 | struct dentry *dentry; |
Trond Myklebust | 7d59d1e | 2009-08-09 15:14:23 -0400 | [diff] [blame] | 736 | struct inode *dir = parent->d_inode; |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 737 | int error; |
| 738 | |
Trond Myklebust | 7d59d1e | 2009-08-09 15:14:23 -0400 | [diff] [blame] | 739 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 740 | dentry = __rpc_lookup_create_exclusive(parent, name); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 741 | if (IS_ERR(dentry)) |
Trond Myklebust | 7d59d1e | 2009-08-09 15:14:23 -0400 | [diff] [blame] | 742 | goto out; |
| 743 | error = __rpc_mkdir(dir, dentry, mode, NULL, private); |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 744 | if (error != 0) |
| 745 | goto out_err; |
Trond Myklebust | e57aed7 | 2009-08-09 15:14:26 -0400 | [diff] [blame] | 746 | if (populate != NULL) { |
| 747 | error = populate(dentry, args_populate); |
| 748 | if (error) |
| 749 | goto err_rmdir; |
| 750 | } |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 751 | out: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 752 | mutex_unlock(&dir->i_mutex); |
Trond Myklebust | 5c3e985 | 2006-07-29 17:37:40 -0400 | [diff] [blame] | 753 | return dentry; |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 754 | err_rmdir: |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 755 | __rpc_rmdir(dir, dentry); |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 756 | out_err: |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 757 | dentry = ERR_PTR(error); |
| 758 | goto out; |
| 759 | } |
| 760 | |
Trond Myklebust | e57aed7 | 2009-08-09 15:14:26 -0400 | [diff] [blame] | 761 | static int rpc_rmdir_depopulate(struct dentry *dentry, |
| 762 | void (*depopulate)(struct dentry *)) |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 763 | { |
Trond Myklebust | dff02cc | 2006-07-31 14:17:18 -0700 | [diff] [blame] | 764 | struct dentry *parent; |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 765 | struct inode *dir; |
| 766 | int error; |
| 767 | |
Trond Myklebust | dff02cc | 2006-07-31 14:17:18 -0700 | [diff] [blame] | 768 | parent = dget_parent(dentry); |
| 769 | dir = parent->d_inode; |
Arjan van de Ven | c6573c2 | 2006-07-03 00:25:16 -0700 | [diff] [blame] | 770 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
Trond Myklebust | e57aed7 | 2009-08-09 15:14:26 -0400 | [diff] [blame] | 771 | if (depopulate != NULL) |
| 772 | depopulate(dentry); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 773 | error = __rpc_rmdir(dir, dentry); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 774 | mutex_unlock(&dir->i_mutex); |
Trond Myklebust | dff02cc | 2006-07-31 14:17:18 -0700 | [diff] [blame] | 775 | dput(parent); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 776 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
| 778 | |
J. Bruce Fields | 93a44a7 | 2007-11-06 13:06:03 -0500 | [diff] [blame] | 779 | /** |
| 780 | * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication |
| 781 | * @parent: dentry of directory to create new "pipe" in |
| 782 | * @name: name of pipe |
| 783 | * @private: private data to associate with the pipe, for the caller's use |
| 784 | * @ops: operations defining the behavior of the pipe: upcall, downcall, |
\"J. Bruce Fields\ | c381060 | 2008-12-23 16:08:32 -0500 | [diff] [blame] | 785 | * release_pipe, open_pipe, and destroy_msg. |
Randy Dunlap | 65b6e42 | 2008-02-13 15:03:23 -0800 | [diff] [blame] | 786 | * @flags: rpc_inode flags |
J. Bruce Fields | 93a44a7 | 2007-11-06 13:06:03 -0500 | [diff] [blame] | 787 | * |
| 788 | * Data is made available for userspace to read by calls to |
| 789 | * rpc_queue_upcall(). The actual reads will result in calls to |
| 790 | * @ops->upcall, which will be called with the file pointer, |
| 791 | * message, and userspace buffer to copy to. |
| 792 | * |
| 793 | * Writes can come at any time, and do not necessarily have to be |
| 794 | * responses to upcalls. They will result in calls to @msg->downcall. |
| 795 | * |
| 796 | * The @private argument passed here will be available to all these methods |
| 797 | * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private. |
| 798 | */ |
Trond Myklebust | b693ba4 | 2009-08-09 15:14:15 -0400 | [diff] [blame] | 799 | struct dentry *rpc_mkpipe(struct dentry *parent, const char *name, |
| 800 | void *private, const struct rpc_pipe_ops *ops, |
| 801 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | struct dentry *dentry; |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 804 | struct inode *dir = parent->d_inode; |
Trond Myklebust | 7364af6 | 2009-08-09 15:14:16 -0400 | [diff] [blame] | 805 | umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR; |
Trond Myklebust | cfeaa4a | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 806 | struct qstr q; |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 807 | int err; |
Trond Myklebust | 7364af6 | 2009-08-09 15:14:16 -0400 | [diff] [blame] | 808 | |
| 809 | if (ops->upcall == NULL) |
| 810 | umode &= ~S_IRUGO; |
| 811 | if (ops->downcall == NULL) |
| 812 | umode &= ~S_IWUGO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Trond Myklebust | cfeaa4a | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 814 | q.name = name; |
| 815 | q.len = strlen(name); |
| 816 | q.hash = full_name_hash(q.name, q.len), |
| 817 | |
| 818 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
Stanislav Kinsbursky | 5bff038 | 2011-11-08 15:09:19 +0300 | [diff] [blame] | 819 | dentry = __rpc_lookup_create_exclusive(parent, &q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | if (IS_ERR(dentry)) |
Trond Myklebust | cfeaa4a | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 821 | goto out; |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 822 | err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops, |
Trond Myklebust | 810d90b | 2009-08-09 15:14:18 -0400 | [diff] [blame] | 823 | private, ops, flags); |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 824 | if (err) |
| 825 | goto out_err; |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 826 | out: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 827 | mutex_unlock(&dir->i_mutex); |
Trond Myklebust | 5c3e985 | 2006-07-29 17:37:40 -0400 | [diff] [blame] | 828 | return dentry; |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 829 | out_err: |
| 830 | dentry = ERR_PTR(err); |
Trond Myklebust | 158998b | 2006-08-24 01:03:17 -0400 | [diff] [blame] | 831 | printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 832 | __FILE__, __func__, parent->d_name.name, name, |
Trond Myklebust | 7589806 | 2009-08-09 15:14:17 -0400 | [diff] [blame] | 833 | err); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 834 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 836 | EXPORT_SYMBOL_GPL(rpc_mkpipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
J. Bruce Fields | 93a44a7 | 2007-11-06 13:06:03 -0500 | [diff] [blame] | 838 | /** |
| 839 | * rpc_unlink - remove a pipe |
| 840 | * @dentry: dentry for the pipe, as returned from rpc_mkpipe |
| 841 | * |
| 842 | * After this call, lookups will no longer find the pipe, and any |
| 843 | * attempts to read or write using preexisting opens of the pipe will |
| 844 | * return -EPIPE. |
| 845 | */ |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 846 | int |
Trond Myklebust | 5d67476 | 2006-07-31 14:11:48 -0700 | [diff] [blame] | 847 | rpc_unlink(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | { |
Trond Myklebust | 5d67476 | 2006-07-31 14:11:48 -0700 | [diff] [blame] | 849 | struct dentry *parent; |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 850 | struct inode *dir; |
Trond Myklebust | 5d67476 | 2006-07-31 14:11:48 -0700 | [diff] [blame] | 851 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
Trond Myklebust | 5d67476 | 2006-07-31 14:11:48 -0700 | [diff] [blame] | 853 | parent = dget_parent(dentry); |
| 854 | dir = parent->d_inode; |
Arjan van de Ven | c6573c2 | 2006-07-03 00:25:16 -0700 | [diff] [blame] | 855 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
Trond Myklebust | 810d90b | 2009-08-09 15:14:18 -0400 | [diff] [blame] | 856 | error = __rpc_rmpipe(dir, dentry); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 857 | mutex_unlock(&dir->i_mutex); |
Trond Myklebust | 5d67476 | 2006-07-31 14:11:48 -0700 | [diff] [blame] | 858 | dput(parent); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 859 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } |
Trond Myklebust | 468039e | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 861 | EXPORT_SYMBOL_GPL(rpc_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
Trond Myklebust | e57aed7 | 2009-08-09 15:14:26 -0400 | [diff] [blame] | 863 | enum { |
| 864 | RPCAUTH_info, |
| 865 | RPCAUTH_EOF |
| 866 | }; |
| 867 | |
| 868 | static const struct rpc_filelist authfiles[] = { |
| 869 | [RPCAUTH_info] = { |
| 870 | .name = "info", |
| 871 | .i_fop = &rpc_info_operations, |
| 872 | .mode = S_IFREG | S_IRUSR, |
| 873 | }, |
| 874 | }; |
| 875 | |
| 876 | static int rpc_clntdir_populate(struct dentry *dentry, void *private) |
| 877 | { |
| 878 | return rpc_populate(dentry, |
| 879 | authfiles, RPCAUTH_info, RPCAUTH_EOF, |
| 880 | private); |
| 881 | } |
| 882 | |
| 883 | static void rpc_clntdir_depopulate(struct dentry *dentry) |
| 884 | { |
| 885 | rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF); |
| 886 | } |
| 887 | |
Trond Myklebust | 7d59d1e | 2009-08-09 15:14:23 -0400 | [diff] [blame] | 888 | /** |
| 889 | * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs |
Randy Dunlap | 4111d4f | 2009-09-23 14:36:38 -0400 | [diff] [blame] | 890 | * @dentry: dentry from the rpc_pipefs root to the new directory |
| 891 | * @name: &struct qstr for the name |
Trond Myklebust | 7d59d1e | 2009-08-09 15:14:23 -0400 | [diff] [blame] | 892 | * @rpc_client: rpc client to associate with this directory |
| 893 | * |
| 894 | * This creates a directory at the given @path associated with |
| 895 | * @rpc_clnt, which will contain a file named "info" with some basic |
| 896 | * information about the client, together with any "pipes" that may |
| 897 | * later be created using rpc_mkpipe(). |
| 898 | */ |
Trond Myklebust | 23ac658 | 2009-08-09 15:14:25 -0400 | [diff] [blame] | 899 | struct dentry *rpc_create_client_dir(struct dentry *dentry, |
Trond Myklebust | e57aed7 | 2009-08-09 15:14:26 -0400 | [diff] [blame] | 900 | struct qstr *name, |
| 901 | struct rpc_clnt *rpc_client) |
Trond Myklebust | 7d59d1e | 2009-08-09 15:14:23 -0400 | [diff] [blame] | 902 | { |
Trond Myklebust | e57aed7 | 2009-08-09 15:14:26 -0400 | [diff] [blame] | 903 | return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL, |
| 904 | rpc_clntdir_populate, rpc_client); |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir() |
| 909 | * @dentry: directory to remove |
| 910 | */ |
| 911 | int rpc_remove_client_dir(struct dentry *dentry) |
| 912 | { |
| 913 | return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate); |
Trond Myklebust | 7d59d1e | 2009-08-09 15:14:23 -0400 | [diff] [blame] | 914 | } |
| 915 | |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 916 | static const struct rpc_filelist cache_pipefs_files[3] = { |
| 917 | [0] = { |
| 918 | .name = "channel", |
| 919 | .i_fop = &cache_file_operations_pipefs, |
Trond Myklebust | 96c61cb | 2009-08-19 18:12:21 -0400 | [diff] [blame] | 920 | .mode = S_IFREG|S_IRUSR|S_IWUSR, |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 921 | }, |
| 922 | [1] = { |
| 923 | .name = "content", |
| 924 | .i_fop = &content_file_operations_pipefs, |
| 925 | .mode = S_IFREG|S_IRUSR, |
| 926 | }, |
| 927 | [2] = { |
| 928 | .name = "flush", |
| 929 | .i_fop = &cache_flush_operations_pipefs, |
| 930 | .mode = S_IFREG|S_IRUSR|S_IWUSR, |
| 931 | }, |
| 932 | }; |
| 933 | |
| 934 | static int rpc_cachedir_populate(struct dentry *dentry, void *private) |
| 935 | { |
| 936 | return rpc_populate(dentry, |
| 937 | cache_pipefs_files, 0, 3, |
| 938 | private); |
| 939 | } |
| 940 | |
| 941 | static void rpc_cachedir_depopulate(struct dentry *dentry) |
| 942 | { |
| 943 | rpc_depopulate(dentry, cache_pipefs_files, 0, 3); |
| 944 | } |
| 945 | |
| 946 | struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name, |
Al Viro | 64f1426 | 2011-07-25 00:35:13 -0400 | [diff] [blame] | 947 | umode_t umode, struct cache_detail *cd) |
Trond Myklebust | 8854e82 | 2009-08-09 15:14:30 -0400 | [diff] [blame] | 948 | { |
| 949 | return rpc_mkdir_populate(parent, name, umode, NULL, |
| 950 | rpc_cachedir_populate, cd); |
| 951 | } |
| 952 | |
| 953 | void rpc_remove_cache_dir(struct dentry *dentry) |
| 954 | { |
| 955 | rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate); |
| 956 | } |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | /* |
| 959 | * populate the filesystem |
| 960 | */ |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 961 | static const struct super_operations s_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | .alloc_inode = rpc_alloc_inode, |
| 963 | .destroy_inode = rpc_destroy_inode, |
| 964 | .statfs = simple_statfs, |
| 965 | }; |
| 966 | |
| 967 | #define RPCAUTH_GSSMAGIC 0x67596969 |
| 968 | |
Trond Myklebust | bb15674 | 2009-08-09 15:14:21 -0400 | [diff] [blame] | 969 | /* |
| 970 | * We have a single directory with 1 node in it. |
| 971 | */ |
| 972 | enum { |
| 973 | RPCAUTH_lockd, |
| 974 | RPCAUTH_mount, |
| 975 | RPCAUTH_nfs, |
| 976 | RPCAUTH_portmap, |
| 977 | RPCAUTH_statd, |
| 978 | RPCAUTH_nfsd4_cb, |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 979 | RPCAUTH_cache, |
Trond Myklebust | bb15674 | 2009-08-09 15:14:21 -0400 | [diff] [blame] | 980 | RPCAUTH_RootEOF |
| 981 | }; |
| 982 | |
| 983 | static const struct rpc_filelist files[] = { |
| 984 | [RPCAUTH_lockd] = { |
| 985 | .name = "lockd", |
| 986 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 987 | }, |
| 988 | [RPCAUTH_mount] = { |
| 989 | .name = "mount", |
| 990 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 991 | }, |
| 992 | [RPCAUTH_nfs] = { |
| 993 | .name = "nfs", |
| 994 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 995 | }, |
| 996 | [RPCAUTH_portmap] = { |
| 997 | .name = "portmap", |
| 998 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 999 | }, |
| 1000 | [RPCAUTH_statd] = { |
| 1001 | .name = "statd", |
| 1002 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 1003 | }, |
| 1004 | [RPCAUTH_nfsd4_cb] = { |
| 1005 | .name = "nfsd4_cb", |
| 1006 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 1007 | }, |
Trond Myklebust | e571cbf | 2009-08-19 18:12:27 -0400 | [diff] [blame] | 1008 | [RPCAUTH_cache] = { |
| 1009 | .name = "cache", |
| 1010 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 1011 | }, |
Trond Myklebust | bb15674 | 2009-08-09 15:14:21 -0400 | [diff] [blame] | 1012 | }; |
| 1013 | |
Stanislav Kinsbursky | 432eb1a | 2011-12-26 15:39:22 +0300 | [diff] [blame] | 1014 | /* |
| 1015 | * This call can be used only in RPC pipefs mount notification hooks. |
| 1016 | */ |
| 1017 | struct dentry *rpc_d_lookup_sb(const struct super_block *sb, |
| 1018 | const unsigned char *dir_name) |
| 1019 | { |
| 1020 | struct qstr dir = { |
| 1021 | .name = dir_name, |
| 1022 | .len = strlen(dir_name), |
| 1023 | .hash = full_name_hash(dir_name, strlen(dir_name)), |
| 1024 | }; |
| 1025 | |
| 1026 | return d_lookup(sb->s_root, &dir); |
| 1027 | } |
| 1028 | EXPORT_SYMBOL_GPL(rpc_d_lookup_sb); |
| 1029 | |
Stanislav Kinsbursky | c21a588 | 2011-12-26 15:39:39 +0300 | [diff] [blame] | 1030 | void rpc_pipefs_init_net(struct net *net) |
| 1031 | { |
| 1032 | struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); |
| 1033 | |
| 1034 | mutex_init(&sn->pipefs_sb_lock); |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * This call will be used for per network namespace operations calls. |
| 1039 | * Note: Function will be returned with pipefs_sb_lock taken if superblock was |
| 1040 | * found. This lock have to be released by rpc_put_sb_net() when all operations |
| 1041 | * will be completed. |
| 1042 | */ |
| 1043 | struct super_block *rpc_get_sb_net(const struct net *net) |
| 1044 | { |
| 1045 | struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); |
| 1046 | |
| 1047 | mutex_lock(&sn->pipefs_sb_lock); |
| 1048 | if (sn->pipefs_sb) |
| 1049 | return sn->pipefs_sb; |
| 1050 | mutex_unlock(&sn->pipefs_sb_lock); |
| 1051 | return NULL; |
| 1052 | } |
| 1053 | EXPORT_SYMBOL_GPL(rpc_get_sb_net); |
| 1054 | |
| 1055 | void rpc_put_sb_net(const struct net *net) |
| 1056 | { |
| 1057 | struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); |
| 1058 | |
| 1059 | BUG_ON(sn->pipefs_sb == NULL); |
| 1060 | mutex_unlock(&sn->pipefs_sb_lock); |
| 1061 | } |
| 1062 | EXPORT_SYMBOL_GPL(rpc_put_sb_net); |
| 1063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | static int |
| 1065 | rpc_fill_super(struct super_block *sb, void *data, int silent) |
| 1066 | { |
| 1067 | struct inode *inode; |
| 1068 | struct dentry *root; |
Stanislav Kinsbursky | 38b0da7 | 2011-12-26 15:38:56 +0300 | [diff] [blame] | 1069 | struct net *net = data; |
Stanislav Kinsbursky | 90c4e82 | 2011-12-26 15:39:30 +0300 | [diff] [blame] | 1070 | struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); |
Stanislav Kinsbursky | 2d00131 | 2011-12-26 15:39:13 +0300 | [diff] [blame] | 1071 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
| 1073 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1074 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1075 | sb->s_magic = RPCAUTH_GSSMAGIC; |
| 1076 | sb->s_op = &s_ops; |
| 1077 | sb->s_time_gran = 1; |
| 1078 | |
| 1079 | inode = rpc_get_inode(sb, S_IFDIR | 0755); |
| 1080 | if (!inode) |
| 1081 | return -ENOMEM; |
Al Viro | fc7bed8 | 2010-01-25 18:30:38 -0500 | [diff] [blame] | 1082 | sb->s_root = root = d_alloc_root(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | if (!root) { |
| 1084 | iput(inode); |
| 1085 | return -ENOMEM; |
| 1086 | } |
Trond Myklebust | ac6fece | 2009-08-09 15:14:20 -0400 | [diff] [blame] | 1087 | if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL)) |
Al Viro | fc7bed8 | 2010-01-25 18:30:38 -0500 | [diff] [blame] | 1088 | return -ENOMEM; |
Stanislav Kinsbursky | efc46bf | 2011-12-26 15:39:47 +0300 | [diff] [blame^] | 1089 | dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net, |
| 1090 | NET_NAME(net)); |
Stanislav Kinsbursky | 2d00131 | 2011-12-26 15:39:13 +0300 | [diff] [blame] | 1091 | err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list, |
| 1092 | RPC_PIPEFS_MOUNT, |
| 1093 | sb); |
| 1094 | if (err) |
| 1095 | goto err_depopulate; |
Stanislav Kinsbursky | 021c68d | 2011-12-26 15:39:04 +0300 | [diff] [blame] | 1096 | sb->s_fs_info = get_net(net); |
Stanislav Kinsbursky | 90c4e82 | 2011-12-26 15:39:30 +0300 | [diff] [blame] | 1097 | sn->pipefs_sb = sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | return 0; |
Stanislav Kinsbursky | 2d00131 | 2011-12-26 15:39:13 +0300 | [diff] [blame] | 1099 | |
| 1100 | err_depopulate: |
| 1101 | blocking_notifier_call_chain(&rpc_pipefs_notifier_list, |
| 1102 | RPC_PIPEFS_UMOUNT, |
| 1103 | sb); |
| 1104 | __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF); |
| 1105 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 1108 | static struct dentry * |
| 1109 | rpc_mount(struct file_system_type *fs_type, |
| 1110 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | { |
Stanislav Kinsbursky | 38b0da7 | 2011-12-26 15:38:56 +0300 | [diff] [blame] | 1112 | return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Stanislav Kinsbursky | 021c68d | 2011-12-26 15:39:04 +0300 | [diff] [blame] | 1115 | void rpc_kill_sb(struct super_block *sb) |
| 1116 | { |
| 1117 | struct net *net = sb->s_fs_info; |
Stanislav Kinsbursky | 90c4e82 | 2011-12-26 15:39:30 +0300 | [diff] [blame] | 1118 | struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); |
Stanislav Kinsbursky | 021c68d | 2011-12-26 15:39:04 +0300 | [diff] [blame] | 1119 | |
Stanislav Kinsbursky | c21a588 | 2011-12-26 15:39:39 +0300 | [diff] [blame] | 1120 | mutex_lock(&sn->pipefs_sb_lock); |
Stanislav Kinsbursky | 90c4e82 | 2011-12-26 15:39:30 +0300 | [diff] [blame] | 1121 | sn->pipefs_sb = NULL; |
Stanislav Kinsbursky | c21a588 | 2011-12-26 15:39:39 +0300 | [diff] [blame] | 1122 | mutex_unlock(&sn->pipefs_sb_lock); |
Stanislav Kinsbursky | 021c68d | 2011-12-26 15:39:04 +0300 | [diff] [blame] | 1123 | put_net(net); |
Stanislav Kinsbursky | efc46bf | 2011-12-26 15:39:47 +0300 | [diff] [blame^] | 1124 | dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net, |
| 1125 | NET_NAME(net)); |
Stanislav Kinsbursky | 2d00131 | 2011-12-26 15:39:13 +0300 | [diff] [blame] | 1126 | blocking_notifier_call_chain(&rpc_pipefs_notifier_list, |
| 1127 | RPC_PIPEFS_UMOUNT, |
| 1128 | sb); |
Stanislav Kinsbursky | 021c68d | 2011-12-26 15:39:04 +0300 | [diff] [blame] | 1129 | kill_litter_super(sb); |
| 1130 | } |
| 1131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | static struct file_system_type rpc_pipe_fs_type = { |
| 1133 | .owner = THIS_MODULE, |
| 1134 | .name = "rpc_pipefs", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 1135 | .mount = rpc_mount, |
Stanislav Kinsbursky | 021c68d | 2011-12-26 15:39:04 +0300 | [diff] [blame] | 1136 | .kill_sb = rpc_kill_sb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | }; |
| 1138 | |
| 1139 | static void |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 1140 | init_once(void *foo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | { |
| 1142 | struct rpc_inode *rpci = (struct rpc_inode *) foo; |
| 1143 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 1144 | inode_init_once(&rpci->vfs_inode); |
| 1145 | rpci->private = NULL; |
| 1146 | rpci->nreaders = 0; |
| 1147 | rpci->nwriters = 0; |
| 1148 | INIT_LIST_HEAD(&rpci->in_upcall); |
Trond Myklebust | 6e84c7b | 2007-06-07 15:31:36 -0400 | [diff] [blame] | 1149 | INIT_LIST_HEAD(&rpci->in_downcall); |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 1150 | INIT_LIST_HEAD(&rpci->pipe); |
| 1151 | rpci->pipelen = 0; |
| 1152 | init_waitqueue_head(&rpci->waitq); |
| 1153 | INIT_DELAYED_WORK(&rpci->queue_timeout, |
| 1154 | rpc_timeout_upcall_queue); |
| 1155 | rpci->ops = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | int register_rpc_pipefs(void) |
| 1159 | { |
Akinobu Mita | 5bd5f58 | 2007-05-09 02:34:51 -0700 | [diff] [blame] | 1160 | int err; |
| 1161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | rpc_inode_cachep = kmem_cache_create("rpc_inode_cache", |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 1163 | sizeof(struct rpc_inode), |
| 1164 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 1165 | SLAB_MEM_SPREAD), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1166 | init_once); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | if (!rpc_inode_cachep) |
| 1168 | return -ENOMEM; |
Akinobu Mita | 5bd5f58 | 2007-05-09 02:34:51 -0700 | [diff] [blame] | 1169 | err = register_filesystem(&rpc_pipe_fs_type); |
| 1170 | if (err) { |
| 1171 | kmem_cache_destroy(rpc_inode_cachep); |
| 1172 | return err; |
| 1173 | } |
| 1174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | void unregister_rpc_pipefs(void) |
| 1179 | { |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 1180 | kmem_cache_destroy(rpc_inode_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | unregister_filesystem(&rpc_pipe_fs_type); |
| 1182 | } |
Michal Schmidt | dcbf8c3 | 2011-07-02 00:47:12 +0200 | [diff] [blame] | 1183 | |
| 1184 | /* Make 'mount -t rpc_pipefs ...' autoload this module. */ |
| 1185 | MODULE_ALIAS("rpc_pipefs"); |