xref: /linux/net/sunrpc/rpc_pipe.c (revision ddf52f12ef500d9f2a5e325e0c86449f594abb25)
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 
rpc_pipefs_notifier_register(struct notifier_block * nb)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 
rpc_pipefs_notifier_unregister(struct notifier_block * nb)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 
rpc_purge_list(wait_queue_head_t * waitq,struct list_head * head,void (* destroy_msg)(struct rpc_pipe_msg *),int err)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
rpc_timeout_upcall_queue(struct work_struct * work)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 
rpc_pipe_generic_upcall(struct file * filp,struct rpc_pipe_msg * msg,char __user * dst,size_t buflen)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
rpc_queue_upcall(struct rpc_pipe * pipe,struct rpc_pipe_msg * msg)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
rpc_inode_setowner(struct inode * inode,void * private)165 rpc_inode_setowner(struct inode *inode, void *private)
166 {
167 	RPC_I(inode)->private = private;
168 }
169 
170 static void
rpc_close_pipes(struct dentry * dentry)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 	inode_unlock(inode);
195 }
196 
197 static struct inode *
rpc_alloc_inode(struct super_block * sb)198 rpc_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 
207 static void
rpc_free_inode(struct inode * inode)208 rpc_free_inode(struct inode *inode)
209 {
210 	kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
211 }
212 
213 static int
rpc_pipe_open(struct inode * inode,struct file * filp)214 rpc_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;
235 out:
236 	inode_unlock(inode);
237 	return res;
238 }
239 
240 static int
rpc_pipe_release(struct inode * inode,struct file * filp)241 rpc_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);
276 out:
277 	inode_unlock(inode);
278 	return 0;
279 }
280 
281 static ssize_t
rpc_pipe_read(struct file * filp,char __user * buf,size_t len,loff_t * offset)282 rpc_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 	}
320 out_unlock:
321 	inode_unlock(inode);
322 	return res;
323 }
324 
325 static ssize_t
rpc_pipe_write(struct file * filp,const char __user * buf,size_t len,loff_t * offset)326 rpc_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 
339 static __poll_t
rpc_pipe_poll(struct file * filp,struct poll_table_struct * wait)340 rpc_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 
357 static long
rpc_pipe_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)358 rpc_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 
387 static 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 
397 static int
rpc_show_info(struct seq_file * m,void * v)398 rpc_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 
414 static int
rpc_info_open(struct inode * inode,struct file * file)415 rpc_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 
438 static int
rpc_info_release(struct inode * inode,struct file * file)439 rpc_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 
449 static 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  */
461 struct rpc_filelist {
462 	const char *name;
463 	const struct file_operations *i_fop;
464 	umode_t mode;
465 };
466 
467 static struct inode *
rpc_get_inode(struct super_block * sb,umode_t mode)468 rpc_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 
488 static void
init_pipe(struct rpc_pipe * pipe)489 init_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 
rpc_destroy_pipe_data(struct rpc_pipe * pipe)504 void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
505 {
506 	kfree(pipe);
507 }
508 EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
509 
rpc_mkpipe_data(const struct rpc_pipe_ops * ops,int flags)510 struct 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 }
522 EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
523 
rpc_new_file(struct dentry * parent,const char * name,umode_t mode,const struct file_operations * i_fop,void * private)524 static 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 		dput(dentry);
540 		inode_unlock(dir);
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_instantiate(dentry, inode);
548 	fsnotify_create(dir, dentry);
549 	inode_unlock(dir);
550 	return 0;
551 }
552 
rpc_new_dir(struct dentry * parent,const char * name,umode_t mode)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 		dput(dentry);
567 		inode_unlock(dir);
568 		return ERR_PTR(-ENOMEM);
569 	}
570 
571 	inode->i_ino = iunique(dir->i_sb, 100);
572 	inc_nlink(dir);
573 	d_instantiate(dentry, inode);
574 	fsnotify_mkdir(dir, dentry);
575 	inode_unlock(dir);
576 
577 	return dentry;
578 }
579 
rpc_populate(struct dentry * parent,const struct rpc_filelist * files,int start,int eof,void * private)580 static int rpc_populate(struct dentry *parent,
581 			const struct rpc_filelist *files,
582 			int start, int eof,
583 			void *private)
584 {
585 	struct dentry *dentry;
586 	int i, err;
587 
588 	for (i = start; i < eof; i++) {
589 		switch (files[i].mode & S_IFMT) {
590 			default:
591 				BUG();
592 			case S_IFREG:
593 				err = rpc_new_file(parent,
594 						files[i].name,
595 						files[i].mode,
596 						files[i].i_fop,
597 						private);
598 				if (err)
599 					goto out_bad;
600 				break;
601 			case S_IFDIR:
602 				dentry = rpc_new_dir(parent,
603 						files[i].name,
604 						files[i].mode);
605 				if (IS_ERR(dentry)) {
606 					err = PTR_ERR(dentry);
607 					goto out_bad;
608 				}
609 		}
610 	}
611 	return 0;
612 out_bad:
613 	printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
614 			__FILE__, __func__, parent);
615 	return err;
616 }
617 
618 /**
619  * rpc_mkpipe_dentry - make an rpc_pipefs file for kernel<->userspace
620  *		       communication
621  * @parent: dentry of directory to create new "pipe" in
622  * @name: name of pipe
623  * @private: private data to associate with the pipe, for the caller's use
624  * @pipe: &rpc_pipe containing input parameters
625  *
626  * Data is made available for userspace to read by calls to
627  * rpc_queue_upcall().  The actual reads will result in calls to
628  * @ops->upcall, which will be called with the file pointer,
629  * message, and userspace buffer to copy to.
630  *
631  * Writes can come at any time, and do not necessarily have to be
632  * responses to upcalls.  They will result in calls to @msg->downcall.
633  *
634  * The @private argument passed here will be available to all these methods
635  * from the file pointer, via RPC_I(file_inode(file))->private.
636  */
rpc_mkpipe_dentry(struct dentry * parent,const char * name,void * private,struct rpc_pipe * pipe)637 int rpc_mkpipe_dentry(struct dentry *parent, const char *name,
638 				 void *private, struct rpc_pipe *pipe)
639 {
640 	struct inode *dir = d_inode(parent);
641 	struct dentry *dentry;
642 	struct inode *inode;
643 	struct rpc_inode *rpci;
644 	umode_t umode = S_IFIFO | 0600;
645 	int err;
646 
647 	if (pipe->ops->upcall == NULL)
648 		umode &= ~0444;
649 	if (pipe->ops->downcall == NULL)
650 		umode &= ~0222;
651 
652 	dentry = simple_start_creating(parent, name);
653 	if (IS_ERR(dentry)) {
654 		err = PTR_ERR(dentry);
655 		goto failed;
656 	}
657 
658 	inode = rpc_get_inode(dir->i_sb, umode);
659 	if (unlikely(!inode)) {
660 		dput(dentry);
661 		inode_unlock(dir);
662 		err = -ENOMEM;
663 		goto failed;
664 	}
665 	inode->i_ino = iunique(dir->i_sb, 100);
666 	inode->i_fop = &rpc_pipe_fops;
667 	rpci = RPC_I(inode);
668 	rpci->private = private;
669 	rpci->pipe = pipe;
670 	rpc_inode_setowner(inode, private);
671 	d_instantiate(dentry, inode);
672 	pipe->dentry = dentry;
673 	fsnotify_create(dir, dentry);
674 	inode_unlock(dir);
675 	return 0;
676 
677 failed:
678 	pr_warn("%s() failed to create pipe %pd/%s (errno = %d)\n",
679 			__func__, parent, name, err);
680 	return err;
681 }
682 EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
683 
684 /**
685  * rpc_unlink - remove a pipe
686  * @pipe: the pipe to be removed
687  *
688  * After this call, lookups will no longer find the pipe, and any
689  * attempts to read or write using preexisting opens of the pipe will
690  * return -EPIPE.
691  */
692 void
rpc_unlink(struct rpc_pipe * pipe)693 rpc_unlink(struct rpc_pipe *pipe)
694 {
695 	if (pipe->dentry) {
696 		simple_recursive_removal(pipe->dentry, rpc_close_pipes);
697 		pipe->dentry = NULL;
698 	}
699 }
700 EXPORT_SYMBOL_GPL(rpc_unlink);
701 
702 /**
703  * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head
704  * @pdh: pointer to struct rpc_pipe_dir_head
705  */
rpc_init_pipe_dir_head(struct rpc_pipe_dir_head * pdh)706 void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh)
707 {
708 	INIT_LIST_HEAD(&pdh->pdh_entries);
709 	pdh->pdh_dentry = NULL;
710 }
711 EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head);
712 
713 /**
714  * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object
715  * @pdo: pointer to struct rpc_pipe_dir_object
716  * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops
717  * @pdo_data: pointer to caller-defined data
718  */
rpc_init_pipe_dir_object(struct rpc_pipe_dir_object * pdo,const struct rpc_pipe_dir_object_ops * pdo_ops,void * pdo_data)719 void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo,
720 		const struct rpc_pipe_dir_object_ops *pdo_ops,
721 		void *pdo_data)
722 {
723 	INIT_LIST_HEAD(&pdo->pdo_head);
724 	pdo->pdo_ops = pdo_ops;
725 	pdo->pdo_data = pdo_data;
726 }
727 EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object);
728 
729 static int
rpc_add_pipe_dir_object_locked(struct net * net,struct rpc_pipe_dir_head * pdh,struct rpc_pipe_dir_object * pdo)730 rpc_add_pipe_dir_object_locked(struct net *net,
731 		struct rpc_pipe_dir_head *pdh,
732 		struct rpc_pipe_dir_object *pdo)
733 {
734 	int ret = 0;
735 
736 	if (pdh->pdh_dentry)
737 		ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo);
738 	if (ret == 0)
739 		list_add_tail(&pdo->pdo_head, &pdh->pdh_entries);
740 	return ret;
741 }
742 
743 static void
rpc_remove_pipe_dir_object_locked(struct net * net,struct rpc_pipe_dir_head * pdh,struct rpc_pipe_dir_object * pdo)744 rpc_remove_pipe_dir_object_locked(struct net *net,
745 		struct rpc_pipe_dir_head *pdh,
746 		struct rpc_pipe_dir_object *pdo)
747 {
748 	if (pdh->pdh_dentry)
749 		pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo);
750 	list_del_init(&pdo->pdo_head);
751 }
752 
753 /**
754  * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory
755  * @net: pointer to struct net
756  * @pdh: pointer to struct rpc_pipe_dir_head
757  * @pdo: pointer to struct rpc_pipe_dir_object
758  *
759  */
760 int
rpc_add_pipe_dir_object(struct net * net,struct rpc_pipe_dir_head * pdh,struct rpc_pipe_dir_object * pdo)761 rpc_add_pipe_dir_object(struct net *net,
762 		struct rpc_pipe_dir_head *pdh,
763 		struct rpc_pipe_dir_object *pdo)
764 {
765 	int ret = 0;
766 
767 	if (list_empty(&pdo->pdo_head)) {
768 		struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
769 
770 		mutex_lock(&sn->pipefs_sb_lock);
771 		ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo);
772 		mutex_unlock(&sn->pipefs_sb_lock);
773 	}
774 	return ret;
775 }
776 EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object);
777 
778 /**
779  * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory
780  * @net: pointer to struct net
781  * @pdh: pointer to struct rpc_pipe_dir_head
782  * @pdo: pointer to struct rpc_pipe_dir_object
783  *
784  */
785 void
rpc_remove_pipe_dir_object(struct net * net,struct rpc_pipe_dir_head * pdh,struct rpc_pipe_dir_object * pdo)786 rpc_remove_pipe_dir_object(struct net *net,
787 		struct rpc_pipe_dir_head *pdh,
788 		struct rpc_pipe_dir_object *pdo)
789 {
790 	if (!list_empty(&pdo->pdo_head)) {
791 		struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
792 
793 		mutex_lock(&sn->pipefs_sb_lock);
794 		rpc_remove_pipe_dir_object_locked(net, pdh, pdo);
795 		mutex_unlock(&sn->pipefs_sb_lock);
796 	}
797 }
798 EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object);
799 
800 /**
801  * rpc_find_or_alloc_pipe_dir_object
802  * @net: pointer to struct net
803  * @pdh: pointer to struct rpc_pipe_dir_head
804  * @match: match struct rpc_pipe_dir_object to data
805  * @alloc: allocate a new struct rpc_pipe_dir_object
806  * @data: user defined data for match() and alloc()
807  *
808  */
809 struct rpc_pipe_dir_object *
rpc_find_or_alloc_pipe_dir_object(struct net * net,struct rpc_pipe_dir_head * pdh,int (* match)(struct rpc_pipe_dir_object *,void *),struct rpc_pipe_dir_object * (* alloc)(void *),void * data)810 rpc_find_or_alloc_pipe_dir_object(struct net *net,
811 		struct rpc_pipe_dir_head *pdh,
812 		int (*match)(struct rpc_pipe_dir_object *, void *),
813 		struct rpc_pipe_dir_object *(*alloc)(void *),
814 		void *data)
815 {
816 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
817 	struct rpc_pipe_dir_object *pdo;
818 
819 	mutex_lock(&sn->pipefs_sb_lock);
820 	list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) {
821 		if (!match(pdo, data))
822 			continue;
823 		goto out;
824 	}
825 	pdo = alloc(data);
826 	if (!pdo)
827 		goto out;
828 	rpc_add_pipe_dir_object_locked(net, pdh, pdo);
829 out:
830 	mutex_unlock(&sn->pipefs_sb_lock);
831 	return pdo;
832 }
833 EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object);
834 
835 static void
rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head * pdh)836 rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
837 {
838 	struct rpc_pipe_dir_object *pdo;
839 	struct dentry *dir = pdh->pdh_dentry;
840 
841 	list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
842 		pdo->pdo_ops->create(dir, pdo);
843 }
844 
845 static void
rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head * pdh)846 rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
847 {
848 	struct rpc_pipe_dir_object *pdo;
849 	struct dentry *dir = pdh->pdh_dentry;
850 
851 	list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
852 		pdo->pdo_ops->destroy(dir, pdo);
853 }
854 
855 /**
856  * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
857  * @dentry: the parent of new directory
858  * @name: the name of new directory
859  * @rpc_client: rpc client to associate with this directory
860  *
861  * This creates a directory at the given @path associated with
862  * @rpc_clnt, which will contain a file named "info" with some basic
863  * information about the client, together with any "pipes" that may
864  * later be created using rpc_mkpipe().
865  */
rpc_create_client_dir(struct dentry * dentry,const char * name,struct rpc_clnt * rpc_client)866 int rpc_create_client_dir(struct dentry *dentry,
867 			   const char *name,
868 			   struct rpc_clnt *rpc_client)
869 {
870 	struct dentry *ret;
871 	int err;
872 
873 	ret = rpc_new_dir(dentry, name, 0555);
874 	if (IS_ERR(ret))
875 		return PTR_ERR(ret);
876 	err = rpc_new_file(ret, "info", S_IFREG | 0400,
877 			      &rpc_info_operations, rpc_client);
878 	if (err) {
879 		pr_warn("%s failed to populate directory %pd\n",
880 				__func__, ret);
881 		simple_recursive_removal(ret, NULL);
882 		return err;
883 	}
884 	rpc_client->cl_pipedir_objects.pdh_dentry = ret;
885 	rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
886 	return 0;
887 }
888 
889 /**
890  * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
891  * @rpc_client: rpc_client for the pipe
892  */
rpc_remove_client_dir(struct rpc_clnt * rpc_client)893 int rpc_remove_client_dir(struct rpc_clnt *rpc_client)
894 {
895 	struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry;
896 
897 	if (dentry == NULL)
898 		return 0;
899 	rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
900 	rpc_client->cl_pipedir_objects.pdh_dentry = NULL;
901 	simple_recursive_removal(dentry, NULL);
902 	return 0;
903 }
904 
905 static const struct rpc_filelist cache_pipefs_files[3] = {
906 	[0] = {
907 		.name = "channel",
908 		.i_fop = &cache_file_operations_pipefs,
909 		.mode = S_IFREG | 0600,
910 	},
911 	[1] = {
912 		.name = "content",
913 		.i_fop = &content_file_operations_pipefs,
914 		.mode = S_IFREG | 0400,
915 	},
916 	[2] = {
917 		.name = "flush",
918 		.i_fop = &cache_flush_operations_pipefs,
919 		.mode = S_IFREG | 0600,
920 	},
921 };
922 
rpc_create_cache_dir(struct dentry * parent,const char * name,umode_t umode,struct cache_detail * cd)923 struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name,
924 				    umode_t umode, struct cache_detail *cd)
925 {
926 	struct dentry *dentry;
927 
928 	dentry = rpc_new_dir(parent, name, umode);
929 	if (!IS_ERR(dentry)) {
930 		int error = rpc_populate(dentry, cache_pipefs_files, 0, 3, cd);
931 		if (error) {
932 			simple_recursive_removal(dentry, NULL);
933 			return ERR_PTR(error);
934 		}
935 	}
936 	return dentry;
937 }
938 
rpc_remove_cache_dir(struct dentry * dentry)939 void rpc_remove_cache_dir(struct dentry *dentry)
940 {
941 	simple_recursive_removal(dentry, NULL);
942 }
943 
944 /*
945  * populate the filesystem
946  */
947 static const struct super_operations s_ops = {
948 	.alloc_inode	= rpc_alloc_inode,
949 	.free_inode	= rpc_free_inode,
950 	.statfs		= simple_statfs,
951 };
952 
953 #define RPCAUTH_GSSMAGIC 0x67596969
954 
955 /*
956  * We have a single directory with 1 node in it.
957  */
958 enum {
959 	RPCAUTH_lockd,
960 	RPCAUTH_mount,
961 	RPCAUTH_nfs,
962 	RPCAUTH_portmap,
963 	RPCAUTH_statd,
964 	RPCAUTH_nfsd4_cb,
965 	RPCAUTH_cache,
966 	RPCAUTH_nfsd,
967 	RPCAUTH_RootEOF
968 };
969 
970 static const struct rpc_filelist files[] = {
971 	[RPCAUTH_lockd] = {
972 		.name = "lockd",
973 		.mode = S_IFDIR | 0555,
974 	},
975 	[RPCAUTH_mount] = {
976 		.name = "mount",
977 		.mode = S_IFDIR | 0555,
978 	},
979 	[RPCAUTH_nfs] = {
980 		.name = "nfs",
981 		.mode = S_IFDIR | 0555,
982 	},
983 	[RPCAUTH_portmap] = {
984 		.name = "portmap",
985 		.mode = S_IFDIR | 0555,
986 	},
987 	[RPCAUTH_statd] = {
988 		.name = "statd",
989 		.mode = S_IFDIR | 0555,
990 	},
991 	[RPCAUTH_nfsd4_cb] = {
992 		.name = "nfsd4_cb",
993 		.mode = S_IFDIR | 0555,
994 	},
995 	[RPCAUTH_cache] = {
996 		.name = "cache",
997 		.mode = S_IFDIR | 0555,
998 	},
999 	[RPCAUTH_nfsd] = {
1000 		.name = "nfsd",
1001 		.mode = S_IFDIR | 0555,
1002 	},
1003 };
1004 
1005 /*
1006  * This call can be used only in RPC pipefs mount notification hooks.
1007  */
rpc_d_lookup_sb(const struct super_block * sb,const unsigned char * dir_name)1008 struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1009 			       const unsigned char *dir_name)
1010 {
1011 	return try_lookup_noperm(&QSTR(dir_name), sb->s_root);
1012 }
1013 EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1014 
rpc_pipefs_init_net(struct net * net)1015 int rpc_pipefs_init_net(struct net *net)
1016 {
1017 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1018 
1019 	sn->gssd_dummy = rpc_mkpipe_data(&gssd_dummy_pipe_ops, 0);
1020 	if (IS_ERR(sn->gssd_dummy))
1021 		return PTR_ERR(sn->gssd_dummy);
1022 
1023 	mutex_init(&sn->pipefs_sb_lock);
1024 	sn->pipe_version = -1;
1025 	return 0;
1026 }
1027 
rpc_pipefs_exit_net(struct net * net)1028 void rpc_pipefs_exit_net(struct net *net)
1029 {
1030 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1031 
1032 	rpc_destroy_pipe_data(sn->gssd_dummy);
1033 }
1034 
1035 /*
1036  * This call will be used for per network namespace operations calls.
1037  * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1038  * found. This lock have to be released by rpc_put_sb_net() when all operations
1039  * will be completed.
1040  */
rpc_get_sb_net(const struct net * net)1041 struct super_block *rpc_get_sb_net(const struct net *net)
1042 {
1043 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1044 
1045 	mutex_lock(&sn->pipefs_sb_lock);
1046 	if (sn->pipefs_sb)
1047 		return sn->pipefs_sb;
1048 	mutex_unlock(&sn->pipefs_sb_lock);
1049 	return NULL;
1050 }
1051 EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1052 
rpc_put_sb_net(const struct net * net)1053 void rpc_put_sb_net(const struct net *net)
1054 {
1055 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1056 
1057 	WARN_ON(sn->pipefs_sb == NULL);
1058 	mutex_unlock(&sn->pipefs_sb_lock);
1059 }
1060 EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1061 
1062 static ssize_t
dummy_downcall(struct file * filp,const char __user * src,size_t len)1063 dummy_downcall(struct file *filp, const char __user *src, size_t len)
1064 {
1065 	return -EINVAL;
1066 }
1067 
1068 static const struct rpc_pipe_ops gssd_dummy_pipe_ops = {
1069 	.upcall		= rpc_pipe_generic_upcall,
1070 	.downcall	= dummy_downcall,
1071 };
1072 
1073 /*
1074  * Here we present a bogus "info" file to keep rpc.gssd happy. We don't expect
1075  * that it will ever use this info to handle an upcall, but rpc.gssd expects
1076  * that this file will be there and have a certain format.
1077  */
1078 static int
rpc_dummy_info_show(struct seq_file * m,void * v)1079 rpc_dummy_info_show(struct seq_file *m, void *v)
1080 {
1081 	seq_printf(m, "RPC server: %s\n", utsname()->nodename);
1082 	seq_printf(m, "service: foo (1) version 0\n");
1083 	seq_printf(m, "address: 127.0.0.1\n");
1084 	seq_printf(m, "protocol: tcp\n");
1085 	seq_printf(m, "port: 0\n");
1086 	return 0;
1087 }
1088 DEFINE_SHOW_ATTRIBUTE(rpc_dummy_info);
1089 
1090 /**
1091  * rpc_gssd_dummy_populate - create a dummy gssd pipe
1092  * @root:	root of the rpc_pipefs filesystem
1093  * @pipe_data:	pipe data created when netns is initialized
1094  *
1095  * Create a dummy set of directories and a pipe that gssd can hold open to
1096  * indicate that it is up and running.
1097  */
1098 static int
rpc_gssd_dummy_populate(struct dentry * root,struct rpc_pipe * pipe_data)1099 rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
1100 {
1101 	struct dentry *gssd_dentry, *clnt_dentry;
1102 	int err;
1103 
1104 	gssd_dentry = rpc_new_dir(root, "gssd", 0555);
1105 	if (IS_ERR(gssd_dentry))
1106 		return -ENOENT;
1107 
1108 	clnt_dentry = rpc_new_dir(gssd_dentry, "clntXX", 0555);
1109 	if (IS_ERR(clnt_dentry))
1110 		return -ENOENT;
1111 
1112 	err = rpc_new_file(clnt_dentry, "info", 0400,
1113 				   &rpc_dummy_info_fops, NULL);
1114 	if (!err)
1115 		err = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data);
1116 	return err;
1117 }
1118 
1119 static int
rpc_fill_super(struct super_block * sb,struct fs_context * fc)1120 rpc_fill_super(struct super_block *sb, struct fs_context *fc)
1121 {
1122 	struct inode *inode;
1123 	struct dentry *root;
1124 	struct net *net = sb->s_fs_info;
1125 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1126 	int err;
1127 
1128 	sb->s_blocksize = PAGE_SIZE;
1129 	sb->s_blocksize_bits = PAGE_SHIFT;
1130 	sb->s_magic = RPCAUTH_GSSMAGIC;
1131 	sb->s_op = &s_ops;
1132 	sb->s_d_flags = DCACHE_DONTCACHE;
1133 	sb->s_time_gran = 1;
1134 
1135 	inode = rpc_get_inode(sb, S_IFDIR | 0555);
1136 	sb->s_root = root = d_make_root(inode);
1137 	if (!root)
1138 		return -ENOMEM;
1139 	if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
1140 		return -ENOMEM;
1141 
1142 	err = rpc_gssd_dummy_populate(root, sn->gssd_dummy);
1143 	if (err)
1144 		return err;
1145 
1146 	dprintk("RPC:       sending pipefs MOUNT notification for net %x%s\n",
1147 		net->ns.inum, NET_NAME(net));
1148 	mutex_lock(&sn->pipefs_sb_lock);
1149 	sn->pipefs_sb = sb;
1150 	err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1151 					   RPC_PIPEFS_MOUNT,
1152 					   sb);
1153 	mutex_unlock(&sn->pipefs_sb_lock);
1154 	return err;
1155 }
1156 
1157 bool
gssd_running(struct net * net)1158 gssd_running(struct net *net)
1159 {
1160 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1161 	struct rpc_pipe *pipe = sn->gssd_dummy;
1162 
1163 	return pipe->nreaders || pipe->nwriters;
1164 }
1165 EXPORT_SYMBOL_GPL(gssd_running);
1166 
rpc_fs_get_tree(struct fs_context * fc)1167 static int rpc_fs_get_tree(struct fs_context *fc)
1168 {
1169 	return get_tree_keyed(fc, rpc_fill_super, get_net(fc->net_ns));
1170 }
1171 
rpc_fs_free_fc(struct fs_context * fc)1172 static void rpc_fs_free_fc(struct fs_context *fc)
1173 {
1174 	if (fc->s_fs_info)
1175 		put_net(fc->s_fs_info);
1176 }
1177 
1178 static const struct fs_context_operations rpc_fs_context_ops = {
1179 	.free		= rpc_fs_free_fc,
1180 	.get_tree	= rpc_fs_get_tree,
1181 };
1182 
rpc_init_fs_context(struct fs_context * fc)1183 static int rpc_init_fs_context(struct fs_context *fc)
1184 {
1185 	put_user_ns(fc->user_ns);
1186 	fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1187 	fc->ops = &rpc_fs_context_ops;
1188 	return 0;
1189 }
1190 
rpc_kill_sb(struct super_block * sb)1191 static void rpc_kill_sb(struct super_block *sb)
1192 {
1193 	struct net *net = sb->s_fs_info;
1194 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1195 
1196 	mutex_lock(&sn->pipefs_sb_lock);
1197 	if (sn->pipefs_sb != sb) {
1198 		mutex_unlock(&sn->pipefs_sb_lock);
1199 		goto out;
1200 	}
1201 	sn->pipefs_sb = NULL;
1202 	dprintk("RPC:       sending pipefs UMOUNT notification for net %x%s\n",
1203 		net->ns.inum, NET_NAME(net));
1204 	blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1205 					   RPC_PIPEFS_UMOUNT,
1206 					   sb);
1207 	mutex_unlock(&sn->pipefs_sb_lock);
1208 out:
1209 	kill_litter_super(sb);
1210 	put_net(net);
1211 }
1212 
1213 static struct file_system_type rpc_pipe_fs_type = {
1214 	.owner		= THIS_MODULE,
1215 	.name		= "rpc_pipefs",
1216 	.init_fs_context = rpc_init_fs_context,
1217 	.kill_sb	= rpc_kill_sb,
1218 };
1219 MODULE_ALIAS_FS("rpc_pipefs");
1220 MODULE_ALIAS("rpc_pipefs");
1221 
1222 static void
init_once(void * foo)1223 init_once(void *foo)
1224 {
1225 	struct rpc_inode *rpci = (struct rpc_inode *) foo;
1226 
1227 	inode_init_once(&rpci->vfs_inode);
1228 	rpci->private = NULL;
1229 	rpci->pipe = NULL;
1230 	init_waitqueue_head(&rpci->waitq);
1231 }
1232 
register_rpc_pipefs(void)1233 int register_rpc_pipefs(void)
1234 {
1235 	int err;
1236 
1237 	rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
1238 				sizeof(struct rpc_inode),
1239 				0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1240 						SLAB_ACCOUNT),
1241 				init_once);
1242 	if (!rpc_inode_cachep)
1243 		return -ENOMEM;
1244 	err = rpc_clients_notifier_register();
1245 	if (err)
1246 		goto err_notifier;
1247 	err = register_filesystem(&rpc_pipe_fs_type);
1248 	if (err)
1249 		goto err_register;
1250 	return 0;
1251 
1252 err_register:
1253 	rpc_clients_notifier_unregister();
1254 err_notifier:
1255 	kmem_cache_destroy(rpc_inode_cachep);
1256 	return err;
1257 }
1258 
unregister_rpc_pipefs(void)1259 void unregister_rpc_pipefs(void)
1260 {
1261 	rpc_clients_notifier_unregister();
1262 	unregister_filesystem(&rpc_pipe_fs_type);
1263 	kmem_cache_destroy(rpc_inode_cachep);
1264 }
1265