1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * net/sunrpc/rpc_pipe.c 4 * 5 * Userland/kernel interface for rpcauth_gss. 6 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c 7 * and fs/sysfs/inode.c 8 * 9 * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no> 10 * 11 */ 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/fs_context.h> 18 #include <linux/namei.h> 19 #include <linux/fsnotify.h> 20 #include <linux/kernel.h> 21 #include <linux/rcupdate.h> 22 #include <linux/utsname.h> 23 24 #include <asm/ioctls.h> 25 #include <linux/poll.h> 26 #include <linux/wait.h> 27 #include <linux/seq_file.h> 28 29 #include <linux/sunrpc/clnt.h> 30 #include <linux/workqueue.h> 31 #include <linux/sunrpc/rpc_pipe_fs.h> 32 #include <linux/sunrpc/cache.h> 33 #include <linux/nsproxy.h> 34 #include <linux/notifier.h> 35 36 #include "netns.h" 37 #include "sunrpc.h" 38 39 #define RPCDBG_FACILITY RPCDBG_DEBUG 40 41 #define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "") 42 43 static struct file_system_type rpc_pipe_fs_type; 44 static const struct rpc_pipe_ops gssd_dummy_pipe_ops; 45 46 static struct kmem_cache *rpc_inode_cachep __read_mostly; 47 48 #define RPC_UPCALL_TIMEOUT (30*HZ) 49 50 static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list); 51 52 int rpc_pipefs_notifier_register(struct notifier_block *nb) 53 { 54 return blocking_notifier_chain_register(&rpc_pipefs_notifier_list, nb); 55 } 56 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register); 57 58 void rpc_pipefs_notifier_unregister(struct notifier_block *nb) 59 { 60 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb); 61 } 62 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister); 63 64 static void rpc_purge_list(wait_queue_head_t *waitq, struct list_head *head, 65 void (*destroy_msg)(struct rpc_pipe_msg *), int err) 66 { 67 struct rpc_pipe_msg *msg; 68 69 if (list_empty(head)) 70 return; 71 do { 72 msg = list_entry(head->next, struct rpc_pipe_msg, list); 73 list_del_init(&msg->list); 74 msg->errno = err; 75 destroy_msg(msg); 76 } while (!list_empty(head)); 77 78 if (waitq) 79 wake_up(waitq); 80 } 81 82 static void 83 rpc_timeout_upcall_queue(struct work_struct *work) 84 { 85 LIST_HEAD(free_list); 86 struct rpc_pipe *pipe = 87 container_of(work, struct rpc_pipe, queue_timeout.work); 88 void (*destroy_msg)(struct rpc_pipe_msg *); 89 struct dentry *dentry; 90 91 spin_lock(&pipe->lock); 92 destroy_msg = pipe->ops->destroy_msg; 93 if (pipe->nreaders == 0) { 94 list_splice_init(&pipe->pipe, &free_list); 95 pipe->pipelen = 0; 96 } 97 dentry = dget(pipe->dentry); 98 spin_unlock(&pipe->lock); 99 rpc_purge_list(dentry ? &RPC_I(d_inode(dentry))->waitq : NULL, 100 &free_list, destroy_msg, -ETIMEDOUT); 101 dput(dentry); 102 } 103 104 ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg, 105 char __user *dst, size_t buflen) 106 { 107 char *data = (char *)msg->data + msg->copied; 108 size_t mlen = min(msg->len - msg->copied, buflen); 109 unsigned long left; 110 111 left = copy_to_user(dst, data, mlen); 112 if (left == mlen) { 113 msg->errno = -EFAULT; 114 return -EFAULT; 115 } 116 117 mlen -= left; 118 msg->copied += mlen; 119 msg->errno = 0; 120 return mlen; 121 } 122 EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall); 123 124 /** 125 * rpc_queue_upcall - queue an upcall message to userspace 126 * @pipe: upcall pipe on which to queue given message 127 * @msg: message to queue 128 * 129 * Call with an @inode created by rpc_mkpipe() to queue an upcall. 130 * A userspace process may then later read the upcall by performing a 131 * read on an open file for this inode. It is up to the caller to 132 * initialize the fields of @msg (other than @msg->list) appropriately. 133 */ 134 int 135 rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg) 136 { 137 int res = -EPIPE; 138 struct dentry *dentry; 139 140 spin_lock(&pipe->lock); 141 if (pipe->nreaders) { 142 list_add_tail(&msg->list, &pipe->pipe); 143 pipe->pipelen += msg->len; 144 res = 0; 145 } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) { 146 if (list_empty(&pipe->pipe)) 147 queue_delayed_work(rpciod_workqueue, 148 &pipe->queue_timeout, 149 RPC_UPCALL_TIMEOUT); 150 list_add_tail(&msg->list, &pipe->pipe); 151 pipe->pipelen += msg->len; 152 res = 0; 153 } 154 dentry = dget(pipe->dentry); 155 spin_unlock(&pipe->lock); 156 if (dentry) { 157 wake_up(&RPC_I(d_inode(dentry))->waitq); 158 dput(dentry); 159 } 160 return res; 161 } 162 EXPORT_SYMBOL_GPL(rpc_queue_upcall); 163 164 static inline void 165 rpc_inode_setowner(struct inode *inode, void *private) 166 { 167 RPC_I(inode)->private = private; 168 } 169 170 static void 171 rpc_close_pipes(struct dentry *dentry) 172 { 173 struct inode *inode = dentry->d_inode; 174 struct rpc_pipe *pipe = RPC_I(inode)->pipe; 175 int need_release; 176 LIST_HEAD(free_list); 177 178 inode_lock(inode); 179 spin_lock(&pipe->lock); 180 need_release = pipe->nreaders != 0 || pipe->nwriters != 0; 181 pipe->nreaders = 0; 182 list_splice_init(&pipe->in_upcall, &free_list); 183 list_splice_init(&pipe->pipe, &free_list); 184 pipe->pipelen = 0; 185 pipe->dentry = NULL; 186 spin_unlock(&pipe->lock); 187 rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -EPIPE); 188 pipe->nwriters = 0; 189 if (need_release && pipe->ops->release_pipe) 190 pipe->ops->release_pipe(inode); 191 cancel_delayed_work_sync(&pipe->queue_timeout); 192 rpc_inode_setowner(inode, NULL); 193 RPC_I(inode)->pipe = NULL; 194 wake_up(&RPC_I(inode)->waitq); 195 inode_unlock(inode); 196 } 197 198 static struct inode * 199 rpc_alloc_inode(struct super_block *sb) 200 { 201 struct rpc_inode *rpci; 202 rpci = alloc_inode_sb(sb, rpc_inode_cachep, GFP_KERNEL); 203 if (!rpci) 204 return NULL; 205 return &rpci->vfs_inode; 206 } 207 208 static void 209 rpc_free_inode(struct inode *inode) 210 { 211 kmem_cache_free(rpc_inode_cachep, RPC_I(inode)); 212 } 213 214 static int 215 rpc_pipe_open(struct inode *inode, struct file *filp) 216 { 217 struct rpc_pipe *pipe; 218 int first_open; 219 int res = -ENXIO; 220 221 inode_lock(inode); 222 pipe = RPC_I(inode)->pipe; 223 if (pipe == NULL) 224 goto out; 225 first_open = pipe->nreaders == 0 && pipe->nwriters == 0; 226 if (first_open && pipe->ops->open_pipe) { 227 res = pipe->ops->open_pipe(inode); 228 if (res) 229 goto out; 230 } 231 if (filp->f_mode & FMODE_READ) 232 pipe->nreaders++; 233 if (filp->f_mode & FMODE_WRITE) 234 pipe->nwriters++; 235 res = 0; 236 out: 237 inode_unlock(inode); 238 return res; 239 } 240 241 static int 242 rpc_pipe_release(struct inode *inode, struct file *filp) 243 { 244 struct rpc_pipe *pipe; 245 struct rpc_pipe_msg *msg; 246 int last_close; 247 248 inode_lock(inode); 249 pipe = RPC_I(inode)->pipe; 250 if (pipe == NULL) 251 goto out; 252 msg = filp->private_data; 253 if (msg != NULL) { 254 spin_lock(&pipe->lock); 255 msg->errno = -EAGAIN; 256 list_del_init(&msg->list); 257 spin_unlock(&pipe->lock); 258 pipe->ops->destroy_msg(msg); 259 } 260 if (filp->f_mode & FMODE_WRITE) 261 pipe->nwriters --; 262 if (filp->f_mode & FMODE_READ) { 263 pipe->nreaders --; 264 if (pipe->nreaders == 0) { 265 LIST_HEAD(free_list); 266 spin_lock(&pipe->lock); 267 list_splice_init(&pipe->pipe, &free_list); 268 pipe->pipelen = 0; 269 spin_unlock(&pipe->lock); 270 rpc_purge_list(&RPC_I(inode)->waitq, &free_list, 271 pipe->ops->destroy_msg, -EAGAIN); 272 } 273 } 274 last_close = pipe->nwriters == 0 && pipe->nreaders == 0; 275 if (last_close && pipe->ops->release_pipe) 276 pipe->ops->release_pipe(inode); 277 out: 278 inode_unlock(inode); 279 return 0; 280 } 281 282 static ssize_t 283 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset) 284 { 285 struct inode *inode = file_inode(filp); 286 struct rpc_pipe *pipe; 287 struct rpc_pipe_msg *msg; 288 int res = 0; 289 290 inode_lock(inode); 291 pipe = RPC_I(inode)->pipe; 292 if (pipe == NULL) { 293 res = -EPIPE; 294 goto out_unlock; 295 } 296 msg = filp->private_data; 297 if (msg == NULL) { 298 spin_lock(&pipe->lock); 299 if (!list_empty(&pipe->pipe)) { 300 msg = list_entry(pipe->pipe.next, 301 struct rpc_pipe_msg, 302 list); 303 list_move(&msg->list, &pipe->in_upcall); 304 pipe->pipelen -= msg->len; 305 filp->private_data = msg; 306 msg->copied = 0; 307 } 308 spin_unlock(&pipe->lock); 309 if (msg == NULL) 310 goto out_unlock; 311 } 312 /* NOTE: it is up to the callback to update msg->copied */ 313 res = pipe->ops->upcall(filp, msg, buf, len); 314 if (res < 0 || msg->len == msg->copied) { 315 filp->private_data = NULL; 316 spin_lock(&pipe->lock); 317 list_del_init(&msg->list); 318 spin_unlock(&pipe->lock); 319 pipe->ops->destroy_msg(msg); 320 } 321 out_unlock: 322 inode_unlock(inode); 323 return res; 324 } 325 326 static ssize_t 327 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset) 328 { 329 struct inode *inode = file_inode(filp); 330 int res; 331 332 inode_lock(inode); 333 res = -EPIPE; 334 if (RPC_I(inode)->pipe != NULL) 335 res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len); 336 inode_unlock(inode); 337 return res; 338 } 339 340 static __poll_t 341 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait) 342 { 343 struct inode *inode = file_inode(filp); 344 struct rpc_inode *rpci = RPC_I(inode); 345 __poll_t mask = EPOLLOUT | EPOLLWRNORM; 346 347 poll_wait(filp, &rpci->waitq, wait); 348 349 inode_lock(inode); 350 if (rpci->pipe == NULL) 351 mask |= EPOLLERR | EPOLLHUP; 352 else if (filp->private_data || !list_empty(&rpci->pipe->pipe)) 353 mask |= EPOLLIN | EPOLLRDNORM; 354 inode_unlock(inode); 355 return mask; 356 } 357 358 static long 359 rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 360 { 361 struct inode *inode = file_inode(filp); 362 struct rpc_pipe *pipe; 363 int len; 364 365 switch (cmd) { 366 case FIONREAD: 367 inode_lock(inode); 368 pipe = RPC_I(inode)->pipe; 369 if (pipe == NULL) { 370 inode_unlock(inode); 371 return -EPIPE; 372 } 373 spin_lock(&pipe->lock); 374 len = pipe->pipelen; 375 if (filp->private_data) { 376 struct rpc_pipe_msg *msg; 377 msg = filp->private_data; 378 len += msg->len - msg->copied; 379 } 380 spin_unlock(&pipe->lock); 381 inode_unlock(inode); 382 return put_user(len, (int __user *)arg); 383 default: 384 return -EINVAL; 385 } 386 } 387 388 static const struct file_operations rpc_pipe_fops = { 389 .owner = THIS_MODULE, 390 .read = rpc_pipe_read, 391 .write = rpc_pipe_write, 392 .poll = rpc_pipe_poll, 393 .unlocked_ioctl = rpc_pipe_ioctl, 394 .open = rpc_pipe_open, 395 .release = rpc_pipe_release, 396 }; 397 398 static int 399 rpc_show_info(struct seq_file *m, void *v) 400 { 401 struct rpc_clnt *clnt = m->private; 402 403 rcu_read_lock(); 404 seq_printf(m, "RPC server: %s\n", 405 rcu_dereference(clnt->cl_xprt)->servername); 406 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_program->name, 407 clnt->cl_prog, clnt->cl_vers); 408 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR)); 409 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO)); 410 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT)); 411 rcu_read_unlock(); 412 return 0; 413 } 414 415 static int 416 rpc_info_open(struct inode *inode, struct file *file) 417 { 418 struct rpc_clnt *clnt = NULL; 419 int ret = single_open(file, rpc_show_info, NULL); 420 421 if (!ret) { 422 struct seq_file *m = file->private_data; 423 424 spin_lock(&file->f_path.dentry->d_lock); 425 if (!d_unhashed(file->f_path.dentry)) 426 clnt = RPC_I(inode)->private; 427 if (clnt != NULL && refcount_inc_not_zero(&clnt->cl_count)) { 428 spin_unlock(&file->f_path.dentry->d_lock); 429 m->private = clnt; 430 } else { 431 spin_unlock(&file->f_path.dentry->d_lock); 432 single_release(inode, file); 433 ret = -EINVAL; 434 } 435 } 436 return ret; 437 } 438 439 static int 440 rpc_info_release(struct inode *inode, struct file *file) 441 { 442 struct seq_file *m = file->private_data; 443 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private; 444 445 if (clnt) 446 rpc_release_client(clnt); 447 return single_release(inode, file); 448 } 449 450 static const struct file_operations rpc_info_operations = { 451 .owner = THIS_MODULE, 452 .open = rpc_info_open, 453 .read = seq_read, 454 .llseek = seq_lseek, 455 .release = rpc_info_release, 456 }; 457 458 459 /* 460 * Description of fs contents. 461 */ 462 struct rpc_filelist { 463 const char *name; 464 const struct file_operations *i_fop; 465 umode_t mode; 466 }; 467 468 static struct inode * 469 rpc_get_inode(struct super_block *sb, umode_t mode) 470 { 471 struct inode *inode = new_inode(sb); 472 if (!inode) 473 return NULL; 474 inode->i_ino = get_next_ino(); 475 inode->i_mode = mode; 476 simple_inode_init_ts(inode); 477 switch (mode & S_IFMT) { 478 case S_IFDIR: 479 inode->i_fop = &simple_dir_operations; 480 inode->i_op = &simple_dir_inode_operations; 481 inc_nlink(inode); 482 break; 483 default: 484 break; 485 } 486 return inode; 487 } 488 489 static void 490 init_pipe(struct rpc_pipe *pipe) 491 { 492 pipe->nreaders = 0; 493 pipe->nwriters = 0; 494 INIT_LIST_HEAD(&pipe->in_upcall); 495 INIT_LIST_HEAD(&pipe->in_downcall); 496 INIT_LIST_HEAD(&pipe->pipe); 497 pipe->pipelen = 0; 498 INIT_DELAYED_WORK(&pipe->queue_timeout, 499 rpc_timeout_upcall_queue); 500 pipe->ops = NULL; 501 spin_lock_init(&pipe->lock); 502 pipe->dentry = NULL; 503 } 504 505 void rpc_destroy_pipe_data(struct rpc_pipe *pipe) 506 { 507 kfree(pipe); 508 } 509 EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data); 510 511 struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags) 512 { 513 struct rpc_pipe *pipe; 514 515 pipe = kzalloc_obj(struct rpc_pipe); 516 if (!pipe) 517 return ERR_PTR(-ENOMEM); 518 init_pipe(pipe); 519 pipe->ops = ops; 520 pipe->flags = flags; 521 return pipe; 522 } 523 EXPORT_SYMBOL_GPL(rpc_mkpipe_data); 524 525 static int rpc_new_file(struct dentry *parent, 526 const char *name, 527 umode_t mode, 528 const struct file_operations *i_fop, 529 void *private) 530 { 531 struct dentry *dentry = simple_start_creating(parent, name); 532 struct inode *dir = parent->d_inode; 533 struct inode *inode; 534 535 if (IS_ERR(dentry)) 536 return PTR_ERR(dentry); 537 538 inode = rpc_get_inode(dir->i_sb, S_IFREG | mode); 539 if (unlikely(!inode)) { 540 simple_done_creating(dentry); 541 return -ENOMEM; 542 } 543 inode->i_ino = iunique(dir->i_sb, 100); 544 if (i_fop) 545 inode->i_fop = i_fop; 546 rpc_inode_setowner(inode, private); 547 d_make_persistent(dentry, inode); 548 fsnotify_create(dir, dentry); 549 simple_done_creating(dentry); 550 return 0; 551 } 552 553 static struct dentry *rpc_new_dir(struct dentry *parent, 554 const char *name, 555 umode_t mode) 556 { 557 struct dentry *dentry = simple_start_creating(parent, name); 558 struct inode *dir = parent->d_inode; 559 struct inode *inode; 560 561 if (IS_ERR(dentry)) 562 return dentry; 563 564 inode = rpc_get_inode(dir->i_sb, S_IFDIR | mode); 565 if (unlikely(!inode)) { 566 simple_done_creating(dentry); 567 return ERR_PTR(-ENOMEM); 568 } 569 570 inode->i_ino = iunique(dir->i_sb, 100); 571 inc_nlink(dir); 572 d_make_persistent(dentry, inode); 573 fsnotify_mkdir(dir, dentry); 574 simple_done_creating(dentry); 575 576 return dentry; // borrowed 577 } 578 579 static int rpc_populate(struct dentry *parent, 580 const struct rpc_filelist *files, 581 int start, int eof, 582 void *private) 583 { 584 struct dentry *dentry; 585 int i, err; 586 587 for (i = start; i < eof; i++) { 588 switch (files[i].mode & S_IFMT) { 589 default: 590 BUG(); 591 case S_IFREG: 592 err = rpc_new_file(parent, 593 files[i].name, 594 files[i].mode, 595 files[i].i_fop, 596 private); 597 if (err) 598 goto out_bad; 599 break; 600 case S_IFDIR: 601 dentry = rpc_new_dir(parent, 602 files[i].name, 603 files[i].mode); 604 if (IS_ERR(dentry)) { 605 err = PTR_ERR(dentry); 606 goto out_bad; 607 } 608 } 609 } 610 return 0; 611 out_bad: 612 printk(KERN_WARNING "%s: %s failed to populate directory %pd\n", 613 __FILE__, __func__, parent); 614 return err; 615 } 616 617 /** 618 * rpc_mkpipe_dentry - make an rpc_pipefs file for kernel<->userspace 619 * communication 620 * @parent: dentry of directory to create new "pipe" in 621 * @name: name of pipe 622 * @private: private data to associate with the pipe, for the caller's use 623 * @pipe: &rpc_pipe containing input parameters 624 * 625 * Data is made available for userspace to read by calls to 626 * rpc_queue_upcall(). The actual reads will result in calls to 627 * @ops->upcall, which will be called with the file pointer, 628 * message, and userspace buffer to copy to. 629 * 630 * Writes can come at any time, and do not necessarily have to be 631 * responses to upcalls. They will result in calls to @msg->downcall. 632 * 633 * The @private argument passed here will be available to all these methods 634 * from the file pointer, via RPC_I(file_inode(file))->private. 635 */ 636 int rpc_mkpipe_dentry(struct dentry *parent, const char *name, 637 void *private, struct rpc_pipe *pipe) 638 { 639 struct inode *dir = d_inode(parent); 640 struct dentry *dentry; 641 struct inode *inode; 642 struct rpc_inode *rpci; 643 umode_t umode = S_IFIFO | 0600; 644 int err; 645 646 if (pipe->ops->upcall == NULL) 647 umode &= ~0444; 648 if (pipe->ops->downcall == NULL) 649 umode &= ~0222; 650 651 dentry = simple_start_creating(parent, name); 652 if (IS_ERR(dentry)) { 653 err = PTR_ERR(dentry); 654 goto failed; 655 } 656 657 inode = rpc_get_inode(dir->i_sb, umode); 658 if (unlikely(!inode)) { 659 simple_done_creating(dentry); 660 err = -ENOMEM; 661 goto failed; 662 } 663 inode->i_ino = iunique(dir->i_sb, 100); 664 inode->i_fop = &rpc_pipe_fops; 665 rpci = RPC_I(inode); 666 rpci->private = private; 667 rpci->pipe = pipe; 668 rpc_inode_setowner(inode, private); 669 d_make_persistent(dentry, inode); 670 pipe->dentry = dentry; // borrowed 671 fsnotify_create(dir, dentry); 672 simple_done_creating(dentry); 673 return 0; 674 675 failed: 676 pr_warn("%s() failed to create pipe %pd/%s (errno = %d)\n", 677 __func__, parent, name, err); 678 return err; 679 } 680 EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry); 681 682 /** 683 * rpc_unlink - remove a pipe 684 * @pipe: the pipe to be removed 685 * 686 * After this call, lookups will no longer find the pipe, and any 687 * attempts to read or write using preexisting opens of the pipe will 688 * return -EPIPE. 689 */ 690 void 691 rpc_unlink(struct rpc_pipe *pipe) 692 { 693 if (pipe->dentry) { 694 simple_recursive_removal(pipe->dentry, rpc_close_pipes); 695 pipe->dentry = NULL; 696 } 697 } 698 EXPORT_SYMBOL_GPL(rpc_unlink); 699 700 /** 701 * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head 702 * @pdh: pointer to struct rpc_pipe_dir_head 703 */ 704 void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh) 705 { 706 INIT_LIST_HEAD(&pdh->pdh_entries); 707 pdh->pdh_dentry = NULL; 708 } 709 EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head); 710 711 /** 712 * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object 713 * @pdo: pointer to struct rpc_pipe_dir_object 714 * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops 715 * @pdo_data: pointer to caller-defined data 716 */ 717 void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo, 718 const struct rpc_pipe_dir_object_ops *pdo_ops, 719 void *pdo_data) 720 { 721 INIT_LIST_HEAD(&pdo->pdo_head); 722 pdo->pdo_ops = pdo_ops; 723 pdo->pdo_data = pdo_data; 724 } 725 EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object); 726 727 static int 728 rpc_add_pipe_dir_object_locked(struct net *net, 729 struct rpc_pipe_dir_head *pdh, 730 struct rpc_pipe_dir_object *pdo) 731 { 732 int ret = 0; 733 734 if (pdh->pdh_dentry) 735 ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo); 736 if (ret == 0) 737 list_add_tail(&pdo->pdo_head, &pdh->pdh_entries); 738 return ret; 739 } 740 741 static void 742 rpc_remove_pipe_dir_object_locked(struct net *net, 743 struct rpc_pipe_dir_head *pdh, 744 struct rpc_pipe_dir_object *pdo) 745 { 746 if (pdh->pdh_dentry) 747 pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo); 748 list_del_init(&pdo->pdo_head); 749 } 750 751 /** 752 * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory 753 * @net: pointer to struct net 754 * @pdh: pointer to struct rpc_pipe_dir_head 755 * @pdo: pointer to struct rpc_pipe_dir_object 756 * 757 */ 758 int 759 rpc_add_pipe_dir_object(struct net *net, 760 struct rpc_pipe_dir_head *pdh, 761 struct rpc_pipe_dir_object *pdo) 762 { 763 int ret = 0; 764 765 if (list_empty(&pdo->pdo_head)) { 766 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 767 768 mutex_lock(&sn->pipefs_sb_lock); 769 ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo); 770 mutex_unlock(&sn->pipefs_sb_lock); 771 } 772 return ret; 773 } 774 EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object); 775 776 /** 777 * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory 778 * @net: pointer to struct net 779 * @pdh: pointer to struct rpc_pipe_dir_head 780 * @pdo: pointer to struct rpc_pipe_dir_object 781 * 782 */ 783 void 784 rpc_remove_pipe_dir_object(struct net *net, 785 struct rpc_pipe_dir_head *pdh, 786 struct rpc_pipe_dir_object *pdo) 787 { 788 if (!list_empty(&pdo->pdo_head)) { 789 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 790 791 mutex_lock(&sn->pipefs_sb_lock); 792 rpc_remove_pipe_dir_object_locked(net, pdh, pdo); 793 mutex_unlock(&sn->pipefs_sb_lock); 794 } 795 } 796 EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object); 797 798 /** 799 * rpc_find_or_alloc_pipe_dir_object 800 * @net: pointer to struct net 801 * @pdh: pointer to struct rpc_pipe_dir_head 802 * @match: match struct rpc_pipe_dir_object to data 803 * @alloc: allocate a new struct rpc_pipe_dir_object 804 * @data: user defined data for match() and alloc() 805 * 806 */ 807 struct rpc_pipe_dir_object * 808 rpc_find_or_alloc_pipe_dir_object(struct net *net, 809 struct rpc_pipe_dir_head *pdh, 810 int (*match)(struct rpc_pipe_dir_object *, void *), 811 struct rpc_pipe_dir_object *(*alloc)(void *), 812 void *data) 813 { 814 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 815 struct rpc_pipe_dir_object *pdo; 816 817 mutex_lock(&sn->pipefs_sb_lock); 818 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) { 819 if (!match(pdo, data)) 820 continue; 821 goto out; 822 } 823 pdo = alloc(data); 824 if (!pdo) 825 goto out; 826 rpc_add_pipe_dir_object_locked(net, pdh, pdo); 827 out: 828 mutex_unlock(&sn->pipefs_sb_lock); 829 return pdo; 830 } 831 EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object); 832 833 static void 834 rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh) 835 { 836 struct rpc_pipe_dir_object *pdo; 837 struct dentry *dir = pdh->pdh_dentry; 838 839 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) 840 pdo->pdo_ops->create(dir, pdo); 841 } 842 843 static void 844 rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh) 845 { 846 struct rpc_pipe_dir_object *pdo; 847 struct dentry *dir = pdh->pdh_dentry; 848 849 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) 850 pdo->pdo_ops->destroy(dir, pdo); 851 } 852 853 /** 854 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs 855 * @dentry: the parent of new directory 856 * @name: the name of new directory 857 * @rpc_client: rpc client to associate with this directory 858 * 859 * This creates a directory at the given @path associated with 860 * @rpc_clnt, which will contain a file named "info" with some basic 861 * information about the client, together with any "pipes" that may 862 * later be created using rpc_mkpipe(). 863 */ 864 int rpc_create_client_dir(struct dentry *dentry, 865 const char *name, 866 struct rpc_clnt *rpc_client) 867 { 868 struct dentry *ret; 869 int err; 870 871 ret = rpc_new_dir(dentry, name, 0555); 872 if (IS_ERR(ret)) 873 return PTR_ERR(ret); 874 err = rpc_new_file(ret, "info", S_IFREG | 0400, 875 &rpc_info_operations, rpc_client); 876 if (err) { 877 pr_warn("%s failed to populate directory %pd\n", 878 __func__, ret); 879 simple_recursive_removal(ret, NULL); 880 return err; 881 } 882 rpc_client->cl_pipedir_objects.pdh_dentry = ret; 883 rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects); 884 return 0; 885 } 886 887 /** 888 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir() 889 * @rpc_client: rpc_client for the pipe 890 */ 891 int rpc_remove_client_dir(struct rpc_clnt *rpc_client) 892 { 893 struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry; 894 895 if (dentry == NULL) 896 return 0; 897 rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects); 898 rpc_client->cl_pipedir_objects.pdh_dentry = NULL; 899 simple_recursive_removal(dentry, NULL); 900 return 0; 901 } 902 903 static const struct rpc_filelist cache_pipefs_files[3] = { 904 [0] = { 905 .name = "channel", 906 .i_fop = &cache_file_operations_pipefs, 907 .mode = S_IFREG | 0600, 908 }, 909 [1] = { 910 .name = "content", 911 .i_fop = &content_file_operations_pipefs, 912 .mode = S_IFREG | 0400, 913 }, 914 [2] = { 915 .name = "flush", 916 .i_fop = &cache_flush_operations_pipefs, 917 .mode = S_IFREG | 0600, 918 }, 919 }; 920 921 struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name, 922 umode_t umode, struct cache_detail *cd) 923 { 924 struct dentry *dentry; 925 926 dentry = rpc_new_dir(parent, name, umode); 927 if (!IS_ERR(dentry)) { 928 int error = rpc_populate(dentry, cache_pipefs_files, 0, 3, cd); 929 if (error) { 930 simple_recursive_removal(dentry, NULL); 931 return ERR_PTR(error); 932 } 933 } 934 return dentry; 935 } 936 937 void rpc_remove_cache_dir(struct dentry *dentry) 938 { 939 simple_recursive_removal(dentry, NULL); 940 } 941 942 /* 943 * populate the filesystem 944 */ 945 static const struct super_operations s_ops = { 946 .alloc_inode = rpc_alloc_inode, 947 .free_inode = rpc_free_inode, 948 .statfs = simple_statfs, 949 }; 950 951 #define RPCAUTH_GSSMAGIC 0x67596969 952 953 /* 954 * We have a single directory with 1 node in it. 955 */ 956 enum { 957 RPCAUTH_lockd, 958 RPCAUTH_mount, 959 RPCAUTH_nfs, 960 RPCAUTH_portmap, 961 RPCAUTH_statd, 962 RPCAUTH_nfsd4_cb, 963 RPCAUTH_cache, 964 RPCAUTH_nfsd, 965 RPCAUTH_RootEOF 966 }; 967 968 static const struct rpc_filelist files[] = { 969 [RPCAUTH_lockd] = { 970 .name = "lockd", 971 .mode = S_IFDIR | 0555, 972 }, 973 [RPCAUTH_mount] = { 974 .name = "mount", 975 .mode = S_IFDIR | 0555, 976 }, 977 [RPCAUTH_nfs] = { 978 .name = "nfs", 979 .mode = S_IFDIR | 0555, 980 }, 981 [RPCAUTH_portmap] = { 982 .name = "portmap", 983 .mode = S_IFDIR | 0555, 984 }, 985 [RPCAUTH_statd] = { 986 .name = "statd", 987 .mode = S_IFDIR | 0555, 988 }, 989 [RPCAUTH_nfsd4_cb] = { 990 .name = "nfsd4_cb", 991 .mode = S_IFDIR | 0555, 992 }, 993 [RPCAUTH_cache] = { 994 .name = "cache", 995 .mode = S_IFDIR | 0555, 996 }, 997 [RPCAUTH_nfsd] = { 998 .name = "nfsd", 999 .mode = S_IFDIR | 0555, 1000 }, 1001 }; 1002 1003 /* 1004 * This call can be used only in RPC pipefs mount notification hooks. 1005 */ 1006 struct dentry *rpc_d_lookup_sb(const struct super_block *sb, 1007 const unsigned char *dir_name) 1008 { 1009 return try_lookup_noperm(&QSTR(dir_name), sb->s_root); 1010 } 1011 EXPORT_SYMBOL_GPL(rpc_d_lookup_sb); 1012 1013 int rpc_pipefs_init_net(struct net *net) 1014 { 1015 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1016 1017 sn->gssd_dummy = rpc_mkpipe_data(&gssd_dummy_pipe_ops, 0); 1018 if (IS_ERR(sn->gssd_dummy)) 1019 return PTR_ERR(sn->gssd_dummy); 1020 1021 mutex_init(&sn->pipefs_sb_lock); 1022 sn->pipe_version = -1; 1023 return 0; 1024 } 1025 1026 void rpc_pipefs_exit_net(struct net *net) 1027 { 1028 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1029 1030 rpc_destroy_pipe_data(sn->gssd_dummy); 1031 } 1032 1033 /* 1034 * This call will be used for per network namespace operations calls. 1035 * Note: Function will be returned with pipefs_sb_lock taken if superblock was 1036 * found. This lock have to be released by rpc_put_sb_net() when all operations 1037 * will be completed. 1038 */ 1039 struct super_block *rpc_get_sb_net(const struct net *net) 1040 { 1041 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1042 1043 mutex_lock(&sn->pipefs_sb_lock); 1044 if (sn->pipefs_sb) 1045 return sn->pipefs_sb; 1046 mutex_unlock(&sn->pipefs_sb_lock); 1047 return NULL; 1048 } 1049 EXPORT_SYMBOL_GPL(rpc_get_sb_net); 1050 1051 void rpc_put_sb_net(const struct net *net) 1052 { 1053 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1054 1055 WARN_ON(sn->pipefs_sb == NULL); 1056 mutex_unlock(&sn->pipefs_sb_lock); 1057 } 1058 EXPORT_SYMBOL_GPL(rpc_put_sb_net); 1059 1060 static ssize_t 1061 dummy_downcall(struct file *filp, const char __user *src, size_t len) 1062 { 1063 return -EINVAL; 1064 } 1065 1066 static const struct rpc_pipe_ops gssd_dummy_pipe_ops = { 1067 .upcall = rpc_pipe_generic_upcall, 1068 .downcall = dummy_downcall, 1069 }; 1070 1071 /* 1072 * Here we present a bogus "info" file to keep rpc.gssd happy. We don't expect 1073 * that it will ever use this info to handle an upcall, but rpc.gssd expects 1074 * that this file will be there and have a certain format. 1075 */ 1076 static int 1077 rpc_dummy_info_show(struct seq_file *m, void *v) 1078 { 1079 seq_printf(m, "RPC server: %s\n", utsname()->nodename); 1080 seq_printf(m, "service: foo (1) version 0\n"); 1081 seq_printf(m, "address: 127.0.0.1\n"); 1082 seq_printf(m, "protocol: tcp\n"); 1083 seq_printf(m, "port: 0\n"); 1084 return 0; 1085 } 1086 DEFINE_SHOW_ATTRIBUTE(rpc_dummy_info); 1087 1088 /** 1089 * rpc_gssd_dummy_populate - create a dummy gssd pipe 1090 * @root: root of the rpc_pipefs filesystem 1091 * @pipe_data: pipe data created when netns is initialized 1092 * 1093 * Create a dummy set of directories and a pipe that gssd can hold open to 1094 * indicate that it is up and running. 1095 */ 1096 static int 1097 rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data) 1098 { 1099 struct dentry *gssd_dentry, *clnt_dentry; 1100 int err; 1101 1102 gssd_dentry = rpc_new_dir(root, "gssd", 0555); 1103 if (IS_ERR(gssd_dentry)) 1104 return -ENOENT; 1105 1106 clnt_dentry = rpc_new_dir(gssd_dentry, "clntXX", 0555); 1107 if (IS_ERR(clnt_dentry)) 1108 return -ENOENT; 1109 1110 err = rpc_new_file(clnt_dentry, "info", 0400, 1111 &rpc_dummy_info_fops, NULL); 1112 if (!err) 1113 err = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data); 1114 return err; 1115 } 1116 1117 static int 1118 rpc_fill_super(struct super_block *sb, struct fs_context *fc) 1119 { 1120 struct inode *inode; 1121 struct dentry *root; 1122 struct net *net = sb->s_fs_info; 1123 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1124 int err; 1125 1126 sb->s_blocksize = PAGE_SIZE; 1127 sb->s_blocksize_bits = PAGE_SHIFT; 1128 sb->s_magic = RPCAUTH_GSSMAGIC; 1129 sb->s_op = &s_ops; 1130 sb->s_d_flags = DCACHE_DONTCACHE; 1131 sb->s_time_gran = 1; 1132 1133 inode = rpc_get_inode(sb, S_IFDIR | 0555); 1134 sb->s_root = root = d_make_root(inode); 1135 if (!root) 1136 return -ENOMEM; 1137 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL)) 1138 return -ENOMEM; 1139 1140 err = rpc_gssd_dummy_populate(root, sn->gssd_dummy); 1141 if (err) 1142 return err; 1143 1144 dprintk("RPC: sending pipefs MOUNT notification for net %x%s\n", 1145 net->ns.inum, NET_NAME(net)); 1146 mutex_lock(&sn->pipefs_sb_lock); 1147 sn->pipefs_sb = sb; 1148 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list, 1149 RPC_PIPEFS_MOUNT, 1150 sb); 1151 mutex_unlock(&sn->pipefs_sb_lock); 1152 return err; 1153 } 1154 1155 bool 1156 gssd_running(struct net *net) 1157 { 1158 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1159 struct rpc_pipe *pipe = sn->gssd_dummy; 1160 1161 return pipe->nreaders || pipe->nwriters; 1162 } 1163 EXPORT_SYMBOL_GPL(gssd_running); 1164 1165 static int rpc_fs_get_tree(struct fs_context *fc) 1166 { 1167 return get_tree_keyed(fc, rpc_fill_super, get_net(fc->net_ns)); 1168 } 1169 1170 static void rpc_fs_free_fc(struct fs_context *fc) 1171 { 1172 if (fc->s_fs_info) 1173 put_net(fc->s_fs_info); 1174 } 1175 1176 static const struct fs_context_operations rpc_fs_context_ops = { 1177 .free = rpc_fs_free_fc, 1178 .get_tree = rpc_fs_get_tree, 1179 }; 1180 1181 static int rpc_init_fs_context(struct fs_context *fc) 1182 { 1183 put_user_ns(fc->user_ns); 1184 fc->user_ns = get_user_ns(fc->net_ns->user_ns); 1185 fc->ops = &rpc_fs_context_ops; 1186 return 0; 1187 } 1188 1189 static void rpc_kill_sb(struct super_block *sb) 1190 { 1191 struct net *net = sb->s_fs_info; 1192 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id); 1193 1194 mutex_lock(&sn->pipefs_sb_lock); 1195 if (sn->pipefs_sb != sb) { 1196 mutex_unlock(&sn->pipefs_sb_lock); 1197 goto out; 1198 } 1199 sn->pipefs_sb = NULL; 1200 dprintk("RPC: sending pipefs UMOUNT notification for net %x%s\n", 1201 net->ns.inum, NET_NAME(net)); 1202 blocking_notifier_call_chain(&rpc_pipefs_notifier_list, 1203 RPC_PIPEFS_UMOUNT, 1204 sb); 1205 mutex_unlock(&sn->pipefs_sb_lock); 1206 out: 1207 kill_anon_super(sb); 1208 put_net(net); 1209 } 1210 1211 static struct file_system_type rpc_pipe_fs_type = { 1212 .owner = THIS_MODULE, 1213 .name = "rpc_pipefs", 1214 .init_fs_context = rpc_init_fs_context, 1215 .kill_sb = rpc_kill_sb, 1216 }; 1217 MODULE_ALIAS_FS("rpc_pipefs"); 1218 MODULE_ALIAS("rpc_pipefs"); 1219 1220 static void 1221 init_once(void *foo) 1222 { 1223 struct rpc_inode *rpci = (struct rpc_inode *) foo; 1224 1225 inode_init_once(&rpci->vfs_inode); 1226 rpci->private = NULL; 1227 rpci->pipe = NULL; 1228 init_waitqueue_head(&rpci->waitq); 1229 } 1230 1231 int register_rpc_pipefs(void) 1232 { 1233 int err; 1234 1235 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache", 1236 sizeof(struct rpc_inode), 1237 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 1238 SLAB_ACCOUNT), 1239 init_once); 1240 if (!rpc_inode_cachep) 1241 return -ENOMEM; 1242 err = rpc_clients_notifier_register(); 1243 if (err) 1244 goto err_notifier; 1245 err = register_filesystem(&rpc_pipe_fs_type); 1246 if (err) 1247 goto err_register; 1248 return 0; 1249 1250 err_register: 1251 rpc_clients_notifier_unregister(); 1252 err_notifier: 1253 kmem_cache_destroy(rpc_inode_cachep); 1254 return err; 1255 } 1256 1257 void unregister_rpc_pipefs(void) 1258 { 1259 rpc_clients_notifier_unregister(); 1260 unregister_filesystem(&rpc_pipe_fs_type); 1261 kmem_cache_destroy(rpc_inode_cachep); 1262 } 1263