1/*
2 * net/sunrpc/rpc_pipe.c
3 *
4 * Userland/kernel interface for rpcauth_gss.
5 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
6 * and fs/sysfs/inode.c
7 *
8 * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
9 *
10 */
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/string.h>
14#include <linux/pagemap.h>
15#include <linux/mount.h>
16#include <linux/namei.h>
17#include <linux/fsnotify.h>
18#include <linux/kernel.h>
19
20#include <asm/ioctls.h>
21#include <linux/fs.h>
22#include <linux/poll.h>
23#include <linux/wait.h>
24#include <linux/seq_file.h>
25
26#include <linux/sunrpc/clnt.h>
27#include <linux/workqueue.h>
28#include <linux/sunrpc/rpc_pipe_fs.h>
29#include <linux/sunrpc/cache.h>
30#include <linux/smp_lock.h>
31
32static struct vfsmount *rpc_mount __read_mostly;
33static int rpc_mount_count;
34
35static struct file_system_type rpc_pipe_fs_type;
36
37
38static struct kmem_cache *rpc_inode_cachep __read_mostly;
39
40#define RPC_UPCALL_TIMEOUT (30*HZ)
41
42static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
43		void (*destroy_msg)(struct rpc_pipe_msg *), int err)
44{
45	struct rpc_pipe_msg *msg;
46
47	if (list_empty(head))
48		return;
49	do {
50		msg = list_entry(head->next, struct rpc_pipe_msg, list);
51		list_del_init(&msg->list);
52		msg->errno = err;
53		destroy_msg(msg);
54	} while (!list_empty(head));
55	wake_up(&rpci->waitq);
56}
57
58static void
59rpc_timeout_upcall_queue(struct work_struct *work)
60{
61	LIST_HEAD(free_list);
62	struct rpc_inode *rpci =
63		container_of(work, struct rpc_inode, queue_timeout.work);
64	struct inode *inode = &rpci->vfs_inode;
65	void (*destroy_msg)(struct rpc_pipe_msg *);
66
67	spin_lock(&inode->i_lock);
68	if (rpci->ops == NULL) {
69		spin_unlock(&inode->i_lock);
70		return;
71	}
72	destroy_msg = rpci->ops->destroy_msg;
73	if (rpci->nreaders == 0) {
74		list_splice_init(&rpci->pipe, &free_list);
75		rpci->pipelen = 0;
76	}
77	spin_unlock(&inode->i_lock);
78	rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
79}
80
81/**
82 * rpc_queue_upcall - queue an upcall message to userspace
83 * @inode: inode of upcall pipe on which to queue given message
84 * @msg: message to queue
85 *
86 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
87 * A userspace process may then later read the upcall by performing a
88 * read on an open file for this inode.  It is up to the caller to
89 * initialize the fields of @msg (other than @msg->list) appropriately.
90 */
91int
92rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
93{
94	struct rpc_inode *rpci = RPC_I(inode);
95	int res = -EPIPE;
96
97	spin_lock(&inode->i_lock);
98	if (rpci->ops == NULL)
99		goto out;
100	if (rpci->nreaders) {
101		list_add_tail(&msg->list, &rpci->pipe);
102		rpci->pipelen += msg->len;
103		res = 0;
104	} else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
105		if (list_empty(&rpci->pipe))
106			queue_delayed_work(rpciod_workqueue,
107					&rpci->queue_timeout,
108					RPC_UPCALL_TIMEOUT);
109		list_add_tail(&msg->list, &rpci->pipe);
110		rpci->pipelen += msg->len;
111		res = 0;
112	}
113out:
114	spin_unlock(&inode->i_lock);
115	wake_up(&rpci->waitq);
116	return res;
117}
118EXPORT_SYMBOL_GPL(rpc_queue_upcall);
119
120static inline void
121rpc_inode_setowner(struct inode *inode, void *private)
122{
123	RPC_I(inode)->private = private;
124}
125
126static void
127rpc_close_pipes(struct inode *inode)
128{
129	struct rpc_inode *rpci = RPC_I(inode);
130	const struct rpc_pipe_ops *ops;
131	int need_release;
132
133	mutex_lock(&inode->i_mutex);
134	ops = rpci->ops;
135	if (ops != NULL) {
136		LIST_HEAD(free_list);
137		spin_lock(&inode->i_lock);
138		need_release = rpci->nreaders != 0 || rpci->nwriters != 0;
139		rpci->nreaders = 0;
140		list_splice_init(&rpci->in_upcall, &free_list);
141		list_splice_init(&rpci->pipe, &free_list);
142		rpci->pipelen = 0;
143		rpci->ops = NULL;
144		spin_unlock(&inode->i_lock);
145		rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
146		rpci->nwriters = 0;
147		if (need_release && ops->release_pipe)
148			ops->release_pipe(inode);
149		cancel_delayed_work_sync(&rpci->queue_timeout);
150	}
151	rpc_inode_setowner(inode, NULL);
152	mutex_unlock(&inode->i_mutex);
153}
154
155static struct inode *
156rpc_alloc_inode(struct super_block *sb)
157{
158	struct rpc_inode *rpci;
159	rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
160	if (!rpci)
161		return NULL;
162	return &rpci->vfs_inode;
163}
164
165static void
166rpc_destroy_inode(struct inode *inode)
167{
168	kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
169}
170
171static int
172rpc_pipe_open(struct inode *inode, struct file *filp)
173{
174	struct rpc_inode *rpci = RPC_I(inode);
175	int first_open;
176	int res = -ENXIO;
177
178	mutex_lock(&inode->i_mutex);
179	if (rpci->ops == NULL)
180		goto out;
181	first_open = rpci->nreaders == 0 && rpci->nwriters == 0;
182	if (first_open && rpci->ops->open_pipe) {
183		res = rpci->ops->open_pipe(inode);
184		if (res)
185			goto out;
186	}
187	if (filp->f_mode & FMODE_READ)
188		rpci->nreaders++;
189	if (filp->f_mode & FMODE_WRITE)
190		rpci->nwriters++;
191	res = 0;
192out:
193	mutex_unlock(&inode->i_mutex);
194	return res;
195}
196
197static int
198rpc_pipe_release(struct inode *inode, struct file *filp)
199{
200	struct rpc_inode *rpci = RPC_I(inode);
201	struct rpc_pipe_msg *msg;
202	int last_close;
203
204	mutex_lock(&inode->i_mutex);
205	if (rpci->ops == NULL)
206		goto out;
207	msg = (struct rpc_pipe_msg *)filp->private_data;
208	if (msg != NULL) {
209		spin_lock(&inode->i_lock);
210		msg->errno = -EAGAIN;
211		list_del_init(&msg->list);
212		spin_unlock(&inode->i_lock);
213		rpci->ops->destroy_msg(msg);
214	}
215	if (filp->f_mode & FMODE_WRITE)
216		rpci->nwriters --;
217	if (filp->f_mode & FMODE_READ) {
218		rpci->nreaders --;
219		if (rpci->nreaders == 0) {
220			LIST_HEAD(free_list);
221			spin_lock(&inode->i_lock);
222			list_splice_init(&rpci->pipe, &free_list);
223			rpci->pipelen = 0;
224			spin_unlock(&inode->i_lock);
225			rpc_purge_list(rpci, &free_list,
226					rpci->ops->destroy_msg, -EAGAIN);
227		}
228	}
229	last_close = rpci->nwriters == 0 && rpci->nreaders == 0;
230	if (last_close && rpci->ops->release_pipe)
231		rpci->ops->release_pipe(inode);
232out:
233	mutex_unlock(&inode->i_mutex);
234	return 0;
235}
236
237static ssize_t
238rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
239{
240	struct inode *inode = filp->f_path.dentry->d_inode;
241	struct rpc_inode *rpci = RPC_I(inode);
242	struct rpc_pipe_msg *msg;
243	int res = 0;
244
245	mutex_lock(&inode->i_mutex);
246	if (rpci->ops == NULL) {
247		res = -EPIPE;
248		goto out_unlock;
249	}
250	msg = filp->private_data;
251	if (msg == NULL) {
252		spin_lock(&inode->i_lock);
253		if (!list_empty(&rpci->pipe)) {
254			msg = list_entry(rpci->pipe.next,
255					struct rpc_pipe_msg,
256					list);
257			list_move(&msg->list, &rpci->in_upcall);
258			rpci->pipelen -= msg->len;
259			filp->private_data = msg;
260			msg->copied = 0;
261		}
262		spin_unlock(&inode->i_lock);
263		if (msg == NULL)
264			goto out_unlock;
265	}
266	/* NOTE: it is up to the callback to update msg->copied */
267	res = rpci->ops->upcall(filp, msg, buf, len);
268	if (res < 0 || msg->len == msg->copied) {
269		filp->private_data = NULL;
270		spin_lock(&inode->i_lock);
271		list_del_init(&msg->list);
272		spin_unlock(&inode->i_lock);
273		rpci->ops->destroy_msg(msg);
274	}
275out_unlock:
276	mutex_unlock(&inode->i_mutex);
277	return res;
278}
279
280static ssize_t
281rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
282{
283	struct inode *inode = filp->f_path.dentry->d_inode;
284	struct rpc_inode *rpci = RPC_I(inode);
285	int res;
286
287	mutex_lock(&inode->i_mutex);
288	res = -EPIPE;
289	if (rpci->ops != NULL)
290		res = rpci->ops->downcall(filp, buf, len);
291	mutex_unlock(&inode->i_mutex);
292	return res;
293}
294
295static unsigned int
296rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
297{
298	struct rpc_inode *rpci;
299	unsigned int mask = 0;
300
301	rpci = RPC_I(filp->f_path.dentry->d_inode);
302	poll_wait(filp, &rpci->waitq, wait);
303
304	mask = POLLOUT | POLLWRNORM;
305	if (rpci->ops == NULL)
306		mask |= POLLERR | POLLHUP;
307	if (filp->private_data || !list_empty(&rpci->pipe))
308		mask |= POLLIN | POLLRDNORM;
309	return mask;
310}
311
312static int
313rpc_pipe_ioctl_unlocked(struct file *filp, unsigned int cmd, unsigned long arg)
314{
315	struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
316	int len;
317
318	switch (cmd) {
319	case FIONREAD:
320		if (rpci->ops == NULL)
321			return -EPIPE;
322		len = rpci->pipelen;
323		if (filp->private_data) {
324			struct rpc_pipe_msg *msg;
325			msg = (struct rpc_pipe_msg *)filp->private_data;
326			len += msg->len - msg->copied;
327		}
328		return put_user(len, (int __user *)arg);
329	default:
330		return -EINVAL;
331	}
332}
333
334static long
335rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
336{
337	long ret;
338
339	lock_kernel();
340	ret = rpc_pipe_ioctl_unlocked(filp, cmd, arg);
341	unlock_kernel();
342
343	return ret;
344}
345
346static const struct file_operations rpc_pipe_fops = {
347	.owner		= THIS_MODULE,
348	.llseek		= no_llseek,
349	.read		= rpc_pipe_read,
350	.write		= rpc_pipe_write,
351	.poll		= rpc_pipe_poll,
352	.unlocked_ioctl	= rpc_pipe_ioctl,
353	.open		= rpc_pipe_open,
354	.release	= rpc_pipe_release,
355};
356
357static int
358rpc_show_info(struct seq_file *m, void *v)
359{
360	struct rpc_clnt *clnt = m->private;
361
362	seq_printf(m, "RPC server: %s\n", clnt->cl_server);
363	seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
364			clnt->cl_prog, clnt->cl_vers);
365	seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
366	seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
367	seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
368	return 0;
369}
370
371static int
372rpc_info_open(struct inode *inode, struct file *file)
373{
374	struct rpc_clnt *clnt = NULL;
375	int ret = single_open(file, rpc_show_info, NULL);
376
377	if (!ret) {
378		struct seq_file *m = file->private_data;
379
380		spin_lock(&file->f_path.dentry->d_lock);
381		if (!d_unhashed(file->f_path.dentry))
382			clnt = RPC_I(inode)->private;
383		if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
384			spin_unlock(&file->f_path.dentry->d_lock);
385			m->private = clnt;
386		} else {
387			spin_unlock(&file->f_path.dentry->d_lock);
388			single_release(inode, file);
389			ret = -EINVAL;
390		}
391	}
392	return ret;
393}
394
395static int
396rpc_info_release(struct inode *inode, struct file *file)
397{
398	struct seq_file *m = file->private_data;
399	struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
400
401	if (clnt)
402		rpc_release_client(clnt);
403	return single_release(inode, file);
404}
405
406static const struct file_operations rpc_info_operations = {
407	.owner		= THIS_MODULE,
408	.open		= rpc_info_open,
409	.read		= seq_read,
410	.llseek		= seq_lseek,
411	.release	= rpc_info_release,
412};
413
414
415/*
416 * Description of fs contents.
417 */
418struct rpc_filelist {
419	const char *name;
420	const struct file_operations *i_fop;
421	umode_t mode;
422};
423
424struct vfsmount *rpc_get_mount(void)
425{
426	int err;
427
428	err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mount, &rpc_mount_count);
429	if (err != 0)
430		return ERR_PTR(err);
431	return rpc_mount;
432}
433EXPORT_SYMBOL_GPL(rpc_get_mount);
434
435void rpc_put_mount(void)
436{
437	simple_release_fs(&rpc_mount, &rpc_mount_count);
438}
439EXPORT_SYMBOL_GPL(rpc_put_mount);
440
441static int rpc_delete_dentry(struct dentry *dentry)
442{
443	return 1;
444}
445
446static const struct dentry_operations rpc_dentry_operations = {
447	.d_delete = rpc_delete_dentry,
448};
449
450static struct inode *
451rpc_get_inode(struct super_block *sb, umode_t mode)
452{
453	struct inode *inode = new_inode(sb);
454	if (!inode)
455		return NULL;
456	inode->i_mode = mode;
457	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
458	switch(mode & S_IFMT) {
459		case S_IFDIR:
460			inode->i_fop = &simple_dir_operations;
461			inode->i_op = &simple_dir_inode_operations;
462			inc_nlink(inode);
463		default:
464			break;
465	}
466	return inode;
467}
468
469static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
470			       umode_t mode,
471			       const struct file_operations *i_fop,
472			       void *private)
473{
474	struct inode *inode;
475
476	BUG_ON(!d_unhashed(dentry));
477	inode = rpc_get_inode(dir->i_sb, mode);
478	if (!inode)
479		goto out_err;
480	inode->i_ino = iunique(dir->i_sb, 100);
481	if (i_fop)
482		inode->i_fop = i_fop;
483	if (private)
484		rpc_inode_setowner(inode, private);
485	d_add(dentry, inode);
486	return 0;
487out_err:
488	printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
489			__FILE__, __func__, dentry->d_name.name);
490	dput(dentry);
491	return -ENOMEM;
492}
493
494static int __rpc_create(struct inode *dir, struct dentry *dentry,
495			umode_t mode,
496			const struct file_operations *i_fop,
497			void *private)
498{
499	int err;
500
501	err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
502	if (err)
503		return err;
504	fsnotify_create(dir, dentry);
505	return 0;
506}
507
508static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
509		       umode_t mode,
510		       const struct file_operations *i_fop,
511		       void *private)
512{
513	int err;
514
515	err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
516	if (err)
517		return err;
518	inc_nlink(dir);
519	fsnotify_mkdir(dir, dentry);
520	return 0;
521}
522
523static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
524			umode_t mode,
525			const struct file_operations *i_fop,
526			void *private,
527			const struct rpc_pipe_ops *ops,
528			int flags)
529{
530	struct rpc_inode *rpci;
531	int err;
532
533	err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
534	if (err)
535		return err;
536	rpci = RPC_I(dentry->d_inode);
537	rpci->nkern_readwriters = 1;
538	rpci->private = private;
539	rpci->flags = flags;
540	rpci->ops = ops;
541	fsnotify_create(dir, dentry);
542	return 0;
543}
544
545static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
546{
547	int ret;
548
549	dget(dentry);
550	ret = simple_rmdir(dir, dentry);
551	d_delete(dentry);
552	dput(dentry);
553	return ret;
554}
555
556static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
557{
558	int ret;
559
560	dget(dentry);
561	ret = simple_unlink(dir, dentry);
562	d_delete(dentry);
563	dput(dentry);
564	return ret;
565}
566
567static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
568{
569	struct inode *inode = dentry->d_inode;
570	struct rpc_inode *rpci = RPC_I(inode);
571
572	rpci->nkern_readwriters--;
573	if (rpci->nkern_readwriters != 0)
574		return 0;
575	rpc_close_pipes(inode);
576	return __rpc_unlink(dir, dentry);
577}
578
579static struct dentry *__rpc_lookup_create(struct dentry *parent,
580					  struct qstr *name)
581{
582	struct dentry *dentry;
583
584	dentry = d_lookup(parent, name);
585	if (!dentry) {
586		dentry = d_alloc(parent, name);
587		if (!dentry) {
588			dentry = ERR_PTR(-ENOMEM);
589			goto out_err;
590		}
591	}
592	if (!dentry->d_inode)
593		dentry->d_op = &rpc_dentry_operations;
594out_err:
595	return dentry;
596}
597
598static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
599					  struct qstr *name)
600{
601	struct dentry *dentry;
602
603	dentry = __rpc_lookup_create(parent, name);
604	if (IS_ERR(dentry))
605		return dentry;
606	if (dentry->d_inode == NULL)
607		return dentry;
608	dput(dentry);
609	return ERR_PTR(-EEXIST);
610}
611
612static void __rpc_depopulate(struct dentry *parent,
613			     const struct rpc_filelist *files,
614			     int start, int eof)
615{
616	struct inode *dir = parent->d_inode;
617	struct dentry *dentry;
618	struct qstr name;
619	int i;
620
621	for (i = start; i < eof; i++) {
622		name.name = files[i].name;
623		name.len = strlen(files[i].name);
624		name.hash = full_name_hash(name.name, name.len);
625		dentry = d_lookup(parent, &name);
626
627		if (dentry == NULL)
628			continue;
629		if (dentry->d_inode == NULL)
630			goto next;
631		switch (dentry->d_inode->i_mode & S_IFMT) {
632			default:
633				BUG();
634			case S_IFREG:
635				__rpc_unlink(dir, dentry);
636				break;
637			case S_IFDIR:
638				__rpc_rmdir(dir, dentry);
639		}
640next:
641		dput(dentry);
642	}
643}
644
645static void rpc_depopulate(struct dentry *parent,
646			   const struct rpc_filelist *files,
647			   int start, int eof)
648{
649	struct inode *dir = parent->d_inode;
650
651	mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
652	__rpc_depopulate(parent, files, start, eof);
653	mutex_unlock(&dir->i_mutex);
654}
655
656static int rpc_populate(struct dentry *parent,
657			const struct rpc_filelist *files,
658			int start, int eof,
659			void *private)
660{
661	struct inode *dir = parent->d_inode;
662	struct dentry *dentry;
663	int i, err;
664
665	mutex_lock(&dir->i_mutex);
666	for (i = start; i < eof; i++) {
667		struct qstr q;
668
669		q.name = files[i].name;
670		q.len = strlen(files[i].name);
671		q.hash = full_name_hash(q.name, q.len);
672		dentry = __rpc_lookup_create_exclusive(parent, &q);
673		err = PTR_ERR(dentry);
674		if (IS_ERR(dentry))
675			goto out_bad;
676		switch (files[i].mode & S_IFMT) {
677			default:
678				BUG();
679			case S_IFREG:
680				err = __rpc_create(dir, dentry,
681						files[i].mode,
682						files[i].i_fop,
683						private);
684				break;
685			case S_IFDIR:
686				err = __rpc_mkdir(dir, dentry,
687						files[i].mode,
688						NULL,
689						private);
690		}
691		if (err != 0)
692			goto out_bad;
693	}
694	mutex_unlock(&dir->i_mutex);
695	return 0;
696out_bad:
697	__rpc_depopulate(parent, files, start, eof);
698	mutex_unlock(&dir->i_mutex);
699	printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
700			__FILE__, __func__, parent->d_name.name);
701	return err;
702}
703
704static struct dentry *rpc_mkdir_populate(struct dentry *parent,
705		struct qstr *name, umode_t mode, void *private,
706		int (*populate)(struct dentry *, void *), void *args_populate)
707{
708	struct dentry *dentry;
709	struct inode *dir = parent->d_inode;
710	int error;
711
712	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
713	dentry = __rpc_lookup_create_exclusive(parent, name);
714	if (IS_ERR(dentry))
715		goto out;
716	error = __rpc_mkdir(dir, dentry, mode, NULL, private);
717	if (error != 0)
718		goto out_err;
719	if (populate != NULL) {
720		error = populate(dentry, args_populate);
721		if (error)
722			goto err_rmdir;
723	}
724out:
725	mutex_unlock(&dir->i_mutex);
726	return dentry;
727err_rmdir:
728	__rpc_rmdir(dir, dentry);
729out_err:
730	dentry = ERR_PTR(error);
731	goto out;
732}
733
734static int rpc_rmdir_depopulate(struct dentry *dentry,
735		void (*depopulate)(struct dentry *))
736{
737	struct dentry *parent;
738	struct inode *dir;
739	int error;
740
741	parent = dget_parent(dentry);
742	dir = parent->d_inode;
743	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
744	if (depopulate != NULL)
745		depopulate(dentry);
746	error = __rpc_rmdir(dir, dentry);
747	mutex_unlock(&dir->i_mutex);
748	dput(parent);
749	return error;
750}
751
752/**
753 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
754 * @parent: dentry of directory to create new "pipe" in
755 * @name: name of pipe
756 * @private: private data to associate with the pipe, for the caller's use
757 * @ops: operations defining the behavior of the pipe: upcall, downcall,
758 *	release_pipe, open_pipe, and destroy_msg.
759 * @flags: rpc_inode flags
760 *
761 * Data is made available for userspace to read by calls to
762 * rpc_queue_upcall().  The actual reads will result in calls to
763 * @ops->upcall, which will be called with the file pointer,
764 * message, and userspace buffer to copy to.
765 *
766 * Writes can come at any time, and do not necessarily have to be
767 * responses to upcalls.  They will result in calls to @msg->downcall.
768 *
769 * The @private argument passed here will be available to all these methods
770 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
771 */
772struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
773			  void *private, const struct rpc_pipe_ops *ops,
774			  int flags)
775{
776	struct dentry *dentry;
777	struct inode *dir = parent->d_inode;
778	umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
779	struct qstr q;
780	int err;
781
782	if (ops->upcall == NULL)
783		umode &= ~S_IRUGO;
784	if (ops->downcall == NULL)
785		umode &= ~S_IWUGO;
786
787	q.name = name;
788	q.len = strlen(name);
789	q.hash = full_name_hash(q.name, q.len),
790
791	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
792	dentry = __rpc_lookup_create(parent, &q);
793	if (IS_ERR(dentry))
794		goto out;
795	if (dentry->d_inode) {
796		struct rpc_inode *rpci = RPC_I(dentry->d_inode);
797		if (rpci->private != private ||
798				rpci->ops != ops ||
799				rpci->flags != flags) {
800			dput (dentry);
801			err = -EBUSY;
802			goto out_err;
803		}
804		rpci->nkern_readwriters++;
805		goto out;
806	}
807
808	err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
809			   private, ops, flags);
810	if (err)
811		goto out_err;
812out:
813	mutex_unlock(&dir->i_mutex);
814	return dentry;
815out_err:
816	dentry = ERR_PTR(err);
817	printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
818			__FILE__, __func__, parent->d_name.name, name,
819			err);
820	goto out;
821}
822EXPORT_SYMBOL_GPL(rpc_mkpipe);
823
824/**
825 * rpc_unlink - remove a pipe
826 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
827 *
828 * After this call, lookups will no longer find the pipe, and any
829 * attempts to read or write using preexisting opens of the pipe will
830 * return -EPIPE.
831 */
832int
833rpc_unlink(struct dentry *dentry)
834{
835	struct dentry *parent;
836	struct inode *dir;
837	int error = 0;
838
839	parent = dget_parent(dentry);
840	dir = parent->d_inode;
841	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
842	error = __rpc_rmpipe(dir, dentry);
843	mutex_unlock(&dir->i_mutex);
844	dput(parent);
845	return error;
846}
847EXPORT_SYMBOL_GPL(rpc_unlink);
848
849enum {
850	RPCAUTH_info,
851	RPCAUTH_EOF
852};
853
854static const struct rpc_filelist authfiles[] = {
855	[RPCAUTH_info] = {
856		.name = "info",
857		.i_fop = &rpc_info_operations,
858		.mode = S_IFREG | S_IRUSR,
859	},
860};
861
862static int rpc_clntdir_populate(struct dentry *dentry, void *private)
863{
864	return rpc_populate(dentry,
865			    authfiles, RPCAUTH_info, RPCAUTH_EOF,
866			    private);
867}
868
869static void rpc_clntdir_depopulate(struct dentry *dentry)
870{
871	rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
872}
873
874/**
875 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
876 * @dentry: dentry from the rpc_pipefs root to the new directory
877 * @name: &struct qstr for the name
878 * @rpc_client: rpc client to associate with this directory
879 *
880 * This creates a directory at the given @path associated with
881 * @rpc_clnt, which will contain a file named "info" with some basic
882 * information about the client, together with any "pipes" that may
883 * later be created using rpc_mkpipe().
884 */
885struct dentry *rpc_create_client_dir(struct dentry *dentry,
886				   struct qstr *name,
887				   struct rpc_clnt *rpc_client)
888{
889	return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
890			rpc_clntdir_populate, rpc_client);
891}
892
893/**
894 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
895 * @dentry: directory to remove
896 */
897int rpc_remove_client_dir(struct dentry *dentry)
898{
899	return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
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|S_IRUSR|S_IWUSR,
907	},
908	[1] = {
909		.name = "content",
910		.i_fop = &content_file_operations_pipefs,
911		.mode = S_IFREG|S_IRUSR,
912	},
913	[2] = {
914		.name = "flush",
915		.i_fop = &cache_flush_operations_pipefs,
916		.mode = S_IFREG|S_IRUSR|S_IWUSR,
917	},
918};
919
920static int rpc_cachedir_populate(struct dentry *dentry, void *private)
921{
922	return rpc_populate(dentry,
923			    cache_pipefs_files, 0, 3,
924			    private);
925}
926
927static void rpc_cachedir_depopulate(struct dentry *dentry)
928{
929	rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
930}
931
932struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
933				    mode_t umode, struct cache_detail *cd)
934{
935	return rpc_mkdir_populate(parent, name, umode, NULL,
936			rpc_cachedir_populate, cd);
937}
938
939void rpc_remove_cache_dir(struct dentry *dentry)
940{
941	rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
942}
943
944/*
945 * populate the filesystem
946 */
947static const struct super_operations s_ops = {
948	.alloc_inode	= rpc_alloc_inode,
949	.destroy_inode	= rpc_destroy_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 */
958enum {
959	RPCAUTH_lockd,
960	RPCAUTH_mount,
961	RPCAUTH_nfs,
962	RPCAUTH_portmap,
963	RPCAUTH_statd,
964	RPCAUTH_nfsd4_cb,
965	RPCAUTH_cache,
966	RPCAUTH_RootEOF
967};
968
969static const struct rpc_filelist files[] = {
970	[RPCAUTH_lockd] = {
971		.name = "lockd",
972		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
973	},
974	[RPCAUTH_mount] = {
975		.name = "mount",
976		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
977	},
978	[RPCAUTH_nfs] = {
979		.name = "nfs",
980		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
981	},
982	[RPCAUTH_portmap] = {
983		.name = "portmap",
984		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
985	},
986	[RPCAUTH_statd] = {
987		.name = "statd",
988		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
989	},
990	[RPCAUTH_nfsd4_cb] = {
991		.name = "nfsd4_cb",
992		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
993	},
994	[RPCAUTH_cache] = {
995		.name = "cache",
996		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
997	},
998};
999
1000static int
1001rpc_fill_super(struct super_block *sb, void *data, int silent)
1002{
1003	struct inode *inode;
1004	struct dentry *root;
1005
1006	sb->s_blocksize = PAGE_CACHE_SIZE;
1007	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1008	sb->s_magic = RPCAUTH_GSSMAGIC;
1009	sb->s_op = &s_ops;
1010	sb->s_time_gran = 1;
1011
1012	inode = rpc_get_inode(sb, S_IFDIR | 0755);
1013	if (!inode)
1014		return -ENOMEM;
1015	sb->s_root = root = d_alloc_root(inode);
1016	if (!root) {
1017		iput(inode);
1018		return -ENOMEM;
1019	}
1020	if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
1021		return -ENOMEM;
1022	return 0;
1023}
1024
1025static int
1026rpc_get_sb(struct file_system_type *fs_type,
1027		int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1028{
1029	return get_sb_single(fs_type, flags, data, rpc_fill_super, mnt);
1030}
1031
1032static struct file_system_type rpc_pipe_fs_type = {
1033	.owner		= THIS_MODULE,
1034	.name		= "rpc_pipefs",
1035	.get_sb		= rpc_get_sb,
1036	.kill_sb	= kill_litter_super,
1037};
1038
1039static void
1040init_once(void *foo)
1041{
1042	struct rpc_inode *rpci = (struct rpc_inode *) foo;
1043
1044	inode_init_once(&rpci->vfs_inode);
1045	rpci->private = NULL;
1046	rpci->nreaders = 0;
1047	rpci->nwriters = 0;
1048	INIT_LIST_HEAD(&rpci->in_upcall);
1049	INIT_LIST_HEAD(&rpci->in_downcall);
1050	INIT_LIST_HEAD(&rpci->pipe);
1051	rpci->pipelen = 0;
1052	init_waitqueue_head(&rpci->waitq);
1053	INIT_DELAYED_WORK(&rpci->queue_timeout,
1054			    rpc_timeout_upcall_queue);
1055	rpci->ops = NULL;
1056}
1057
1058int register_rpc_pipefs(void)
1059{
1060	int err;
1061
1062	rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
1063				sizeof(struct rpc_inode),
1064				0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1065						SLAB_MEM_SPREAD),
1066				init_once);
1067	if (!rpc_inode_cachep)
1068		return -ENOMEM;
1069	err = register_filesystem(&rpc_pipe_fs_type);
1070	if (err) {
1071		kmem_cache_destroy(rpc_inode_cachep);
1072		return err;
1073	}
1074
1075	return 0;
1076}
1077
1078void unregister_rpc_pipefs(void)
1079{
1080	kmem_cache_destroy(rpc_inode_cachep);
1081	unregister_filesystem(&rpc_pipe_fs_type);
1082}
1083