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