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