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