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 | */ |
| 11 | #include <linux/config.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/mount.h> |
| 17 | #include <linux/namei.h> |
| 18 | #include <linux/dnotify.h> |
| 19 | #include <linux/kernel.h> |
| 20 | |
| 21 | #include <asm/ioctls.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/poll.h> |
| 24 | #include <linux/wait.h> |
| 25 | #include <linux/seq_file.h> |
| 26 | |
| 27 | #include <linux/sunrpc/clnt.h> |
| 28 | #include <linux/workqueue.h> |
| 29 | #include <linux/sunrpc/rpc_pipe_fs.h> |
| 30 | |
Eric Dumazet | ba89966 | 2005-08-26 12:05:31 -0700 | [diff] [blame] | 31 | static struct vfsmount *rpc_mount __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static int rpc_mount_count; |
| 33 | |
| 34 | static struct file_system_type rpc_pipe_fs_type; |
| 35 | |
| 36 | |
Eric Dumazet | ba89966 | 2005-08-26 12:05:31 -0700 | [diff] [blame] | 37 | static kmem_cache_t *rpc_inode_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #define RPC_UPCALL_TIMEOUT (30*HZ) |
| 40 | |
| 41 | static void |
Trond Myklebust | b3eb67a | 2005-11-25 17:10:11 -0500 | [diff] [blame] | 42 | __rpc_purge_list(struct rpc_inode *rpci, struct list_head *head, int err) |
| 43 | { |
| 44 | struct rpc_pipe_msg *msg; |
| 45 | void (*destroy_msg)(struct rpc_pipe_msg *); |
| 46 | |
| 47 | destroy_msg = rpci->ops->destroy_msg; |
| 48 | while (!list_empty(head)) { |
| 49 | msg = list_entry(head->next, struct rpc_pipe_msg, list); |
| 50 | list_del_init(&msg->list); |
| 51 | msg->errno = err; |
| 52 | destroy_msg(msg); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | __rpc_purge_upcall(struct inode *inode, int err) |
| 58 | { |
| 59 | struct rpc_inode *rpci = RPC_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Trond Myklebust | b3eb67a | 2005-11-25 17:10:11 -0500 | [diff] [blame] | 61 | __rpc_purge_list(rpci, &rpci->pipe, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | rpci->pipelen = 0; |
| 63 | wake_up(&rpci->waitq); |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | rpc_timeout_upcall_queue(void *data) |
| 68 | { |
| 69 | struct rpc_inode *rpci = (struct rpc_inode *)data; |
| 70 | struct inode *inode = &rpci->vfs_inode; |
| 71 | |
| 72 | down(&inode->i_sem); |
Trond Myklebust | 969b7f2 | 2006-01-03 09:55:36 +0100 | [diff] [blame] | 73 | if (rpci->ops == NULL) |
| 74 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | if (rpci->nreaders == 0 && !list_empty(&rpci->pipe)) |
| 76 | __rpc_purge_upcall(inode, -ETIMEDOUT); |
Trond Myklebust | 969b7f2 | 2006-01-03 09:55:36 +0100 | [diff] [blame] | 77 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | up(&inode->i_sem); |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg) |
| 83 | { |
| 84 | struct rpc_inode *rpci = RPC_I(inode); |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 85 | int res = -EPIPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | down(&inode->i_sem); |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 88 | if (rpci->ops == NULL) |
| 89 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (rpci->nreaders) { |
| 91 | list_add_tail(&msg->list, &rpci->pipe); |
| 92 | rpci->pipelen += msg->len; |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 93 | res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) { |
| 95 | if (list_empty(&rpci->pipe)) |
| 96 | schedule_delayed_work(&rpci->queue_timeout, |
| 97 | RPC_UPCALL_TIMEOUT); |
| 98 | list_add_tail(&msg->list, &rpci->pipe); |
| 99 | rpci->pipelen += msg->len; |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 100 | res = 0; |
| 101 | } |
| 102 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | up(&inode->i_sem); |
| 104 | wake_up(&rpci->waitq); |
| 105 | return res; |
| 106 | } |
| 107 | |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 108 | static inline void |
| 109 | rpc_inode_setowner(struct inode *inode, void *private) |
| 110 | { |
| 111 | RPC_I(inode)->private = private; |
| 112 | } |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | static void |
| 115 | rpc_close_pipes(struct inode *inode) |
| 116 | { |
| 117 | struct rpc_inode *rpci = RPC_I(inode); |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | down(&inode->i_sem); |
| 120 | if (rpci->ops != NULL) { |
| 121 | rpci->nreaders = 0; |
Trond Myklebust | bb184f3 | 2005-12-03 15:20:10 -0500 | [diff] [blame] | 122 | __rpc_purge_list(rpci, &rpci->in_upcall, -EPIPE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | __rpc_purge_upcall(inode, -EPIPE); |
| 124 | rpci->nwriters = 0; |
| 125 | if (rpci->ops->release_pipe) |
| 126 | rpci->ops->release_pipe(inode); |
| 127 | rpci->ops = NULL; |
| 128 | } |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 129 | rpc_inode_setowner(inode, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | up(&inode->i_sem); |
Trond Myklebust | 969b7f2 | 2006-01-03 09:55:36 +0100 | [diff] [blame] | 131 | cancel_delayed_work(&rpci->queue_timeout); |
| 132 | flush_scheduled_work(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | static struct inode * |
| 136 | rpc_alloc_inode(struct super_block *sb) |
| 137 | { |
| 138 | struct rpc_inode *rpci; |
| 139 | rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, SLAB_KERNEL); |
| 140 | if (!rpci) |
| 141 | return NULL; |
| 142 | return &rpci->vfs_inode; |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | rpc_destroy_inode(struct inode *inode) |
| 147 | { |
| 148 | kmem_cache_free(rpc_inode_cachep, RPC_I(inode)); |
| 149 | } |
| 150 | |
| 151 | static int |
| 152 | rpc_pipe_open(struct inode *inode, struct file *filp) |
| 153 | { |
| 154 | struct rpc_inode *rpci = RPC_I(inode); |
| 155 | int res = -ENXIO; |
| 156 | |
| 157 | down(&inode->i_sem); |
| 158 | if (rpci->ops != NULL) { |
| 159 | if (filp->f_mode & FMODE_READ) |
| 160 | rpci->nreaders ++; |
| 161 | if (filp->f_mode & FMODE_WRITE) |
| 162 | rpci->nwriters ++; |
| 163 | res = 0; |
| 164 | } |
| 165 | up(&inode->i_sem); |
| 166 | return res; |
| 167 | } |
| 168 | |
| 169 | static int |
| 170 | rpc_pipe_release(struct inode *inode, struct file *filp) |
| 171 | { |
Trond Myklebust | 969b7f2 | 2006-01-03 09:55:36 +0100 | [diff] [blame] | 172 | struct rpc_inode *rpci = RPC_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | struct rpc_pipe_msg *msg; |
| 174 | |
| 175 | down(&inode->i_sem); |
| 176 | if (rpci->ops == NULL) |
| 177 | goto out; |
| 178 | msg = (struct rpc_pipe_msg *)filp->private_data; |
| 179 | if (msg != NULL) { |
Trond Myklebust | 48e4918 | 2005-12-19 17:11:22 -0500 | [diff] [blame] | 180 | msg->errno = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | list_del_init(&msg->list); |
| 182 | rpci->ops->destroy_msg(msg); |
| 183 | } |
| 184 | if (filp->f_mode & FMODE_WRITE) |
| 185 | rpci->nwriters --; |
| 186 | if (filp->f_mode & FMODE_READ) |
| 187 | rpci->nreaders --; |
| 188 | if (!rpci->nreaders) |
Trond Myklebust | 48e4918 | 2005-12-19 17:11:22 -0500 | [diff] [blame] | 189 | __rpc_purge_upcall(inode, -EAGAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (rpci->ops->release_pipe) |
| 191 | rpci->ops->release_pipe(inode); |
| 192 | out: |
| 193 | up(&inode->i_sem); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static ssize_t |
| 198 | rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset) |
| 199 | { |
| 200 | struct inode *inode = filp->f_dentry->d_inode; |
| 201 | struct rpc_inode *rpci = RPC_I(inode); |
| 202 | struct rpc_pipe_msg *msg; |
| 203 | int res = 0; |
| 204 | |
| 205 | down(&inode->i_sem); |
| 206 | if (rpci->ops == NULL) { |
| 207 | res = -EPIPE; |
| 208 | goto out_unlock; |
| 209 | } |
| 210 | msg = filp->private_data; |
| 211 | if (msg == NULL) { |
| 212 | if (!list_empty(&rpci->pipe)) { |
| 213 | msg = list_entry(rpci->pipe.next, |
| 214 | struct rpc_pipe_msg, |
| 215 | list); |
| 216 | list_move(&msg->list, &rpci->in_upcall); |
| 217 | rpci->pipelen -= msg->len; |
| 218 | filp->private_data = msg; |
| 219 | msg->copied = 0; |
| 220 | } |
| 221 | if (msg == NULL) |
| 222 | goto out_unlock; |
| 223 | } |
| 224 | /* NOTE: it is up to the callback to update msg->copied */ |
| 225 | res = rpci->ops->upcall(filp, msg, buf, len); |
| 226 | if (res < 0 || msg->len == msg->copied) { |
| 227 | filp->private_data = NULL; |
| 228 | list_del_init(&msg->list); |
| 229 | rpci->ops->destroy_msg(msg); |
| 230 | } |
| 231 | out_unlock: |
| 232 | up(&inode->i_sem); |
| 233 | return res; |
| 234 | } |
| 235 | |
| 236 | static ssize_t |
| 237 | rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset) |
| 238 | { |
| 239 | struct inode *inode = filp->f_dentry->d_inode; |
| 240 | struct rpc_inode *rpci = RPC_I(inode); |
| 241 | int res; |
| 242 | |
| 243 | down(&inode->i_sem); |
| 244 | res = -EPIPE; |
| 245 | if (rpci->ops != NULL) |
| 246 | res = rpci->ops->downcall(filp, buf, len); |
| 247 | up(&inode->i_sem); |
| 248 | return res; |
| 249 | } |
| 250 | |
| 251 | static unsigned int |
| 252 | rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait) |
| 253 | { |
| 254 | struct rpc_inode *rpci; |
| 255 | unsigned int mask = 0; |
| 256 | |
| 257 | rpci = RPC_I(filp->f_dentry->d_inode); |
| 258 | poll_wait(filp, &rpci->waitq, wait); |
| 259 | |
| 260 | mask = POLLOUT | POLLWRNORM; |
| 261 | if (rpci->ops == NULL) |
| 262 | mask |= POLLERR | POLLHUP; |
| 263 | if (!list_empty(&rpci->pipe)) |
| 264 | mask |= POLLIN | POLLRDNORM; |
| 265 | return mask; |
| 266 | } |
| 267 | |
| 268 | static int |
| 269 | rpc_pipe_ioctl(struct inode *ino, struct file *filp, |
| 270 | unsigned int cmd, unsigned long arg) |
| 271 | { |
| 272 | struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode); |
| 273 | int len; |
| 274 | |
| 275 | switch (cmd) { |
| 276 | case FIONREAD: |
| 277 | if (rpci->ops == NULL) |
| 278 | return -EPIPE; |
| 279 | len = rpci->pipelen; |
| 280 | if (filp->private_data) { |
| 281 | struct rpc_pipe_msg *msg; |
| 282 | msg = (struct rpc_pipe_msg *)filp->private_data; |
| 283 | len += msg->len - msg->copied; |
| 284 | } |
| 285 | return put_user(len, (int __user *)arg); |
| 286 | default: |
| 287 | return -EINVAL; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static struct file_operations rpc_pipe_fops = { |
| 292 | .owner = THIS_MODULE, |
| 293 | .llseek = no_llseek, |
| 294 | .read = rpc_pipe_read, |
| 295 | .write = rpc_pipe_write, |
| 296 | .poll = rpc_pipe_poll, |
| 297 | .ioctl = rpc_pipe_ioctl, |
| 298 | .open = rpc_pipe_open, |
| 299 | .release = rpc_pipe_release, |
| 300 | }; |
| 301 | |
| 302 | static int |
| 303 | rpc_show_info(struct seq_file *m, void *v) |
| 304 | { |
| 305 | struct rpc_clnt *clnt = m->private; |
| 306 | |
| 307 | seq_printf(m, "RPC server: %s\n", clnt->cl_server); |
| 308 | seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname, |
| 309 | clnt->cl_prog, clnt->cl_vers); |
| 310 | seq_printf(m, "address: %u.%u.%u.%u\n", |
| 311 | NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr)); |
| 312 | seq_printf(m, "protocol: %s\n", |
| 313 | clnt->cl_xprt->prot == IPPROTO_UDP ? "udp" : "tcp"); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int |
| 318 | rpc_info_open(struct inode *inode, struct file *file) |
| 319 | { |
| 320 | struct rpc_clnt *clnt; |
| 321 | int ret = single_open(file, rpc_show_info, NULL); |
| 322 | |
| 323 | if (!ret) { |
| 324 | struct seq_file *m = file->private_data; |
| 325 | down(&inode->i_sem); |
| 326 | clnt = RPC_I(inode)->private; |
| 327 | if (clnt) { |
| 328 | atomic_inc(&clnt->cl_users); |
| 329 | m->private = clnt; |
| 330 | } else { |
| 331 | single_release(inode, file); |
| 332 | ret = -EINVAL; |
| 333 | } |
| 334 | up(&inode->i_sem); |
| 335 | } |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | static int |
| 340 | rpc_info_release(struct inode *inode, struct file *file) |
| 341 | { |
| 342 | struct seq_file *m = file->private_data; |
| 343 | struct rpc_clnt *clnt = (struct rpc_clnt *)m->private; |
| 344 | |
| 345 | if (clnt) |
| 346 | rpc_release_client(clnt); |
| 347 | return single_release(inode, file); |
| 348 | } |
| 349 | |
| 350 | static struct file_operations rpc_info_operations = { |
| 351 | .owner = THIS_MODULE, |
| 352 | .open = rpc_info_open, |
| 353 | .read = seq_read, |
| 354 | .llseek = seq_lseek, |
| 355 | .release = rpc_info_release, |
| 356 | }; |
| 357 | |
| 358 | |
| 359 | /* |
| 360 | * We have a single directory with 1 node in it. |
| 361 | */ |
| 362 | enum { |
| 363 | RPCAUTH_Root = 1, |
| 364 | RPCAUTH_lockd, |
| 365 | RPCAUTH_mount, |
| 366 | RPCAUTH_nfs, |
| 367 | RPCAUTH_portmap, |
| 368 | RPCAUTH_statd, |
| 369 | RPCAUTH_RootEOF |
| 370 | }; |
| 371 | |
| 372 | /* |
| 373 | * Description of fs contents. |
| 374 | */ |
| 375 | struct rpc_filelist { |
| 376 | char *name; |
| 377 | struct file_operations *i_fop; |
| 378 | int mode; |
| 379 | }; |
| 380 | |
| 381 | static struct rpc_filelist files[] = { |
| 382 | [RPCAUTH_lockd] = { |
| 383 | .name = "lockd", |
| 384 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 385 | }, |
| 386 | [RPCAUTH_mount] = { |
| 387 | .name = "mount", |
| 388 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 389 | }, |
| 390 | [RPCAUTH_nfs] = { |
| 391 | .name = "nfs", |
| 392 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 393 | }, |
| 394 | [RPCAUTH_portmap] = { |
| 395 | .name = "portmap", |
| 396 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 397 | }, |
| 398 | [RPCAUTH_statd] = { |
| 399 | .name = "statd", |
| 400 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, |
| 401 | }, |
| 402 | }; |
| 403 | |
| 404 | enum { |
| 405 | RPCAUTH_info = 2, |
| 406 | RPCAUTH_EOF |
| 407 | }; |
| 408 | |
| 409 | static struct rpc_filelist authfiles[] = { |
| 410 | [RPCAUTH_info] = { |
| 411 | .name = "info", |
| 412 | .i_fop = &rpc_info_operations, |
| 413 | .mode = S_IFREG | S_IRUSR, |
| 414 | }, |
| 415 | }; |
| 416 | |
| 417 | static int |
| 418 | rpc_get_mount(void) |
| 419 | { |
| 420 | return simple_pin_fs("rpc_pipefs", &rpc_mount, &rpc_mount_count); |
| 421 | } |
| 422 | |
| 423 | static void |
| 424 | rpc_put_mount(void) |
| 425 | { |
| 426 | simple_release_fs(&rpc_mount, &rpc_mount_count); |
| 427 | } |
| 428 | |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 429 | static int |
| 430 | rpc_lookup_parent(char *path, struct nameidata *nd) |
| 431 | { |
| 432 | if (path[0] == '\0') |
| 433 | return -ENOENT; |
| 434 | if (rpc_get_mount()) { |
| 435 | printk(KERN_WARNING "%s: %s failed to mount " |
| 436 | "pseudofilesystem \n", __FILE__, __FUNCTION__); |
| 437 | return -ENODEV; |
| 438 | } |
| 439 | nd->mnt = mntget(rpc_mount); |
| 440 | nd->dentry = dget(rpc_mount->mnt_root); |
| 441 | nd->last_type = LAST_ROOT; |
| 442 | nd->flags = LOOKUP_PARENT; |
| 443 | nd->depth = 0; |
| 444 | |
| 445 | if (path_walk(path, nd)) { |
| 446 | printk(KERN_WARNING "%s: %s failed to find path %s\n", |
| 447 | __FILE__, __FUNCTION__, path); |
| 448 | rpc_put_mount(); |
| 449 | return -ENOENT; |
| 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static void |
| 455 | rpc_release_path(struct nameidata *nd) |
| 456 | { |
| 457 | path_release(nd); |
| 458 | rpc_put_mount(); |
| 459 | } |
| 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | static struct inode * |
| 462 | rpc_get_inode(struct super_block *sb, int mode) |
| 463 | { |
| 464 | struct inode *inode = new_inode(sb); |
| 465 | if (!inode) |
| 466 | return NULL; |
| 467 | inode->i_mode = mode; |
| 468 | inode->i_uid = inode->i_gid = 0; |
| 469 | inode->i_blksize = PAGE_CACHE_SIZE; |
| 470 | inode->i_blocks = 0; |
| 471 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 472 | switch(mode & S_IFMT) { |
| 473 | case S_IFDIR: |
| 474 | inode->i_fop = &simple_dir_operations; |
| 475 | inode->i_op = &simple_dir_inode_operations; |
| 476 | inode->i_nlink++; |
| 477 | default: |
| 478 | break; |
| 479 | } |
| 480 | return inode; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * FIXME: This probably has races. |
| 485 | */ |
| 486 | static void |
| 487 | rpc_depopulate(struct dentry *parent) |
| 488 | { |
| 489 | struct inode *dir = parent->d_inode; |
| 490 | struct list_head *pos, *next; |
| 491 | struct dentry *dentry, *dvec[10]; |
| 492 | int n = 0; |
| 493 | |
| 494 | down(&dir->i_sem); |
| 495 | repeat: |
| 496 | spin_lock(&dcache_lock); |
| 497 | list_for_each_safe(pos, next, &parent->d_subdirs) { |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame^] | 498 | dentry = list_entry(pos, struct dentry, d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | spin_lock(&dentry->d_lock); |
| 500 | if (!d_unhashed(dentry)) { |
| 501 | dget_locked(dentry); |
| 502 | __d_drop(dentry); |
| 503 | spin_unlock(&dentry->d_lock); |
| 504 | dvec[n++] = dentry; |
| 505 | if (n == ARRAY_SIZE(dvec)) |
| 506 | break; |
| 507 | } else |
| 508 | spin_unlock(&dentry->d_lock); |
| 509 | } |
| 510 | spin_unlock(&dcache_lock); |
| 511 | if (n) { |
| 512 | do { |
| 513 | dentry = dvec[--n]; |
| 514 | if (dentry->d_inode) { |
| 515 | rpc_close_pipes(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | simple_unlink(dir, dentry); |
| 517 | } |
| 518 | dput(dentry); |
| 519 | } while (n); |
| 520 | goto repeat; |
| 521 | } |
| 522 | up(&dir->i_sem); |
| 523 | } |
| 524 | |
| 525 | static int |
| 526 | rpc_populate(struct dentry *parent, |
| 527 | struct rpc_filelist *files, |
| 528 | int start, int eof) |
| 529 | { |
| 530 | struct inode *inode, *dir = parent->d_inode; |
| 531 | void *private = RPC_I(dir)->private; |
| 532 | struct dentry *dentry; |
| 533 | int mode, i; |
| 534 | |
| 535 | down(&dir->i_sem); |
| 536 | for (i = start; i < eof; i++) { |
| 537 | dentry = d_alloc_name(parent, files[i].name); |
| 538 | if (!dentry) |
| 539 | goto out_bad; |
| 540 | mode = files[i].mode; |
| 541 | inode = rpc_get_inode(dir->i_sb, mode); |
| 542 | if (!inode) { |
| 543 | dput(dentry); |
| 544 | goto out_bad; |
| 545 | } |
| 546 | inode->i_ino = i; |
| 547 | if (files[i].i_fop) |
| 548 | inode->i_fop = files[i].i_fop; |
| 549 | if (private) |
| 550 | rpc_inode_setowner(inode, private); |
| 551 | if (S_ISDIR(mode)) |
| 552 | dir->i_nlink++; |
| 553 | d_add(dentry, inode); |
| 554 | } |
| 555 | up(&dir->i_sem); |
| 556 | return 0; |
| 557 | out_bad: |
| 558 | up(&dir->i_sem); |
| 559 | printk(KERN_WARNING "%s: %s failed to populate directory %s\n", |
| 560 | __FILE__, __FUNCTION__, parent->d_name.name); |
| 561 | return -ENOMEM; |
| 562 | } |
| 563 | |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 564 | static int |
| 565 | __rpc_mkdir(struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | { |
| 567 | struct inode *inode; |
| 568 | |
| 569 | inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR); |
| 570 | if (!inode) |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 571 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | inode->i_ino = iunique(dir->i_sb, 100); |
Christoph Hellwig | 278c995 | 2005-07-24 23:53:01 +0100 | [diff] [blame] | 573 | d_instantiate(dentry, inode); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 574 | dir->i_nlink++; |
Christoph Hellwig | 278c995 | 2005-07-24 23:53:01 +0100 | [diff] [blame] | 575 | inode_dir_notify(dir, DN_CREATE); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 576 | rpc_get_mount(); |
| 577 | return 0; |
| 578 | out_err: |
| 579 | printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n", |
| 580 | __FILE__, __FUNCTION__, dentry->d_name.name); |
| 581 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 584 | static int |
| 585 | __rpc_rmdir(struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | { |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 587 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 589 | shrink_dcache_parent(dentry); |
Trond Myklebust | 6070fe6 | 2005-10-27 22:12:46 -0400 | [diff] [blame] | 590 | if (dentry->d_inode) |
Christoph Hellwig | 278c995 | 2005-07-24 23:53:01 +0100 | [diff] [blame] | 591 | rpc_close_pipes(dentry->d_inode); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 592 | if ((error = simple_rmdir(dir, dentry)) != 0) |
| 593 | return error; |
| 594 | if (!error) { |
| 595 | inode_dir_notify(dir, DN_DELETE); |
| 596 | d_drop(dentry); |
| 597 | rpc_put_mount(); |
| 598 | } |
| 599 | return 0; |
| 600 | } |
Christoph Hellwig | 278c995 | 2005-07-24 23:53:01 +0100 | [diff] [blame] | 601 | |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 602 | static struct dentry * |
| 603 | rpc_lookup_negative(char *path, struct nameidata *nd) |
| 604 | { |
| 605 | struct dentry *dentry; |
| 606 | struct inode *dir; |
| 607 | int error; |
| 608 | |
| 609 | if ((error = rpc_lookup_parent(path, nd)) != 0) |
| 610 | return ERR_PTR(error); |
| 611 | dir = nd->dentry->d_inode; |
| 612 | down(&dir->i_sem); |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 613 | dentry = lookup_hash(nd); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 614 | if (IS_ERR(dentry)) |
| 615 | goto out_err; |
| 616 | if (dentry->d_inode) { |
| 617 | dput(dentry); |
| 618 | dentry = ERR_PTR(-EEXIST); |
| 619 | goto out_err; |
| 620 | } |
| 621 | return dentry; |
| 622 | out_err: |
| 623 | up(&dir->i_sem); |
| 624 | rpc_release_path(nd); |
| 625 | return dentry; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | struct dentry * |
| 630 | rpc_mkdir(char *path, struct rpc_clnt *rpc_client) |
| 631 | { |
| 632 | struct nameidata nd; |
| 633 | struct dentry *dentry; |
| 634 | struct inode *dir; |
| 635 | int error; |
| 636 | |
| 637 | dentry = rpc_lookup_negative(path, &nd); |
| 638 | if (IS_ERR(dentry)) |
| 639 | return dentry; |
| 640 | dir = nd.dentry->d_inode; |
| 641 | if ((error = __rpc_mkdir(dir, dentry)) != 0) |
| 642 | goto err_dput; |
| 643 | RPC_I(dentry->d_inode)->private = rpc_client; |
| 644 | error = rpc_populate(dentry, authfiles, |
| 645 | RPCAUTH_info, RPCAUTH_EOF); |
| 646 | if (error) |
| 647 | goto err_depopulate; |
| 648 | out: |
| 649 | up(&dir->i_sem); |
| 650 | rpc_release_path(&nd); |
| 651 | return dentry; |
| 652 | err_depopulate: |
| 653 | rpc_depopulate(dentry); |
| 654 | __rpc_rmdir(dir, dentry); |
| 655 | err_dput: |
| 656 | dput(dentry); |
| 657 | printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n", |
| 658 | __FILE__, __FUNCTION__, path, error); |
| 659 | dentry = ERR_PTR(error); |
| 660 | goto out; |
| 661 | } |
| 662 | |
| 663 | int |
| 664 | rpc_rmdir(char *path) |
| 665 | { |
| 666 | struct nameidata nd; |
| 667 | struct dentry *dentry; |
| 668 | struct inode *dir; |
| 669 | int error; |
| 670 | |
| 671 | if ((error = rpc_lookup_parent(path, &nd)) != 0) |
| 672 | return error; |
| 673 | dir = nd.dentry->d_inode; |
| 674 | down(&dir->i_sem); |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 675 | dentry = lookup_hash(&nd); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 676 | if (IS_ERR(dentry)) { |
| 677 | error = PTR_ERR(dentry); |
| 678 | goto out_release; |
| 679 | } |
| 680 | rpc_depopulate(dentry); |
| 681 | error = __rpc_rmdir(dir, dentry); |
| 682 | dput(dentry); |
| 683 | out_release: |
| 684 | up(&dir->i_sem); |
| 685 | rpc_release_path(&nd); |
| 686 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | struct dentry * |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 690 | rpc_mkpipe(char *path, void *private, struct rpc_pipe_ops *ops, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 692 | struct nameidata nd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | struct dentry *dentry; |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 694 | struct inode *dir, *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | struct rpc_inode *rpci; |
| 696 | |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 697 | dentry = rpc_lookup_negative(path, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | if (IS_ERR(dentry)) |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 699 | return dentry; |
| 700 | dir = nd.dentry->d_inode; |
| 701 | inode = rpc_get_inode(dir->i_sb, S_IFSOCK | S_IRUSR | S_IWUSR); |
| 702 | if (!inode) |
| 703 | goto err_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | inode->i_ino = iunique(dir->i_sb, 100); |
| 705 | inode->i_fop = &rpc_pipe_fops; |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 706 | d_instantiate(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | rpci = RPC_I(inode); |
| 708 | rpci->private = private; |
| 709 | rpci->flags = flags; |
| 710 | rpci->ops = ops; |
| 711 | inode_dir_notify(dir, DN_CREATE); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 712 | out: |
| 713 | up(&dir->i_sem); |
| 714 | rpc_release_path(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | return dentry; |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 716 | err_dput: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | dput(dentry); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 718 | dentry = ERR_PTR(-ENOMEM); |
| 719 | printk(KERN_WARNING "%s: %s() failed to create pipe %s (errno = %d)\n", |
| 720 | __FILE__, __FUNCTION__, path, -ENOMEM); |
| 721 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 724 | int |
| 725 | rpc_unlink(char *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | { |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 727 | struct nameidata nd; |
| 728 | struct dentry *dentry; |
| 729 | struct inode *dir; |
| 730 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 732 | if ((error = rpc_lookup_parent(path, &nd)) != 0) |
| 733 | return error; |
| 734 | dir = nd.dentry->d_inode; |
| 735 | down(&dir->i_sem); |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 736 | dentry = lookup_hash(&nd); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 737 | if (IS_ERR(dentry)) { |
| 738 | error = PTR_ERR(dentry); |
| 739 | goto out_release; |
| 740 | } |
| 741 | d_drop(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | if (dentry->d_inode) { |
| 743 | rpc_close_pipes(dentry->d_inode); |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 744 | error = simple_unlink(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
Trond Myklebust | f134585 | 2005-09-23 11:08:25 -0400 | [diff] [blame] | 746 | dput(dentry); |
| 747 | inode_dir_notify(dir, DN_DELETE); |
| 748 | out_release: |
| 749 | up(&dir->i_sem); |
| 750 | rpc_release_path(&nd); |
| 751 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* |
| 755 | * populate the filesystem |
| 756 | */ |
| 757 | static struct super_operations s_ops = { |
| 758 | .alloc_inode = rpc_alloc_inode, |
| 759 | .destroy_inode = rpc_destroy_inode, |
| 760 | .statfs = simple_statfs, |
| 761 | }; |
| 762 | |
| 763 | #define RPCAUTH_GSSMAGIC 0x67596969 |
| 764 | |
| 765 | static int |
| 766 | rpc_fill_super(struct super_block *sb, void *data, int silent) |
| 767 | { |
| 768 | struct inode *inode; |
| 769 | struct dentry *root; |
| 770 | |
| 771 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 772 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 773 | sb->s_magic = RPCAUTH_GSSMAGIC; |
| 774 | sb->s_op = &s_ops; |
| 775 | sb->s_time_gran = 1; |
| 776 | |
| 777 | inode = rpc_get_inode(sb, S_IFDIR | 0755); |
| 778 | if (!inode) |
| 779 | return -ENOMEM; |
| 780 | root = d_alloc_root(inode); |
| 781 | if (!root) { |
| 782 | iput(inode); |
| 783 | return -ENOMEM; |
| 784 | } |
| 785 | if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF)) |
| 786 | goto out; |
| 787 | sb->s_root = root; |
| 788 | return 0; |
| 789 | out: |
| 790 | d_genocide(root); |
| 791 | dput(root); |
| 792 | return -ENOMEM; |
| 793 | } |
| 794 | |
| 795 | static struct super_block * |
| 796 | rpc_get_sb(struct file_system_type *fs_type, |
| 797 | int flags, const char *dev_name, void *data) |
| 798 | { |
| 799 | return get_sb_single(fs_type, flags, data, rpc_fill_super); |
| 800 | } |
| 801 | |
| 802 | static struct file_system_type rpc_pipe_fs_type = { |
| 803 | .owner = THIS_MODULE, |
| 804 | .name = "rpc_pipefs", |
| 805 | .get_sb = rpc_get_sb, |
| 806 | .kill_sb = kill_litter_super, |
| 807 | }; |
| 808 | |
| 809 | static void |
| 810 | init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) |
| 811 | { |
| 812 | struct rpc_inode *rpci = (struct rpc_inode *) foo; |
| 813 | |
| 814 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 815 | SLAB_CTOR_CONSTRUCTOR) { |
| 816 | inode_init_once(&rpci->vfs_inode); |
| 817 | rpci->private = NULL; |
| 818 | rpci->nreaders = 0; |
| 819 | rpci->nwriters = 0; |
| 820 | INIT_LIST_HEAD(&rpci->in_upcall); |
| 821 | INIT_LIST_HEAD(&rpci->pipe); |
| 822 | rpci->pipelen = 0; |
| 823 | init_waitqueue_head(&rpci->waitq); |
| 824 | INIT_WORK(&rpci->queue_timeout, rpc_timeout_upcall_queue, rpci); |
| 825 | rpci->ops = NULL; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | int register_rpc_pipefs(void) |
| 830 | { |
| 831 | rpc_inode_cachep = kmem_cache_create("rpc_inode_cache", |
| 832 | sizeof(struct rpc_inode), |
| 833 | 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, |
| 834 | init_once, NULL); |
| 835 | if (!rpc_inode_cachep) |
| 836 | return -ENOMEM; |
| 837 | register_filesystem(&rpc_pipe_fs_type); |
| 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | void unregister_rpc_pipefs(void) |
| 842 | { |
| 843 | if (kmem_cache_destroy(rpc_inode_cachep)) |
| 844 | printk(KERN_WARNING "RPC: unable to free inode cache\n"); |
| 845 | unregister_filesystem(&rpc_pipe_fs_type); |
| 846 | } |