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