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