xref: /openbmc/linux/net/sunrpc/rpc_pipe.c (revision eee17325)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * net/sunrpc/rpc_pipe.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Userland/kernel interface for rpcauth_gss.
51da177e4SLinus Torvalds  * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
6d51fe1beSRolf Eike Beer  * and fs/sysfs/inode.c
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/string.h>
141da177e4SLinus Torvalds #include <linux/pagemap.h>
151da177e4SLinus Torvalds #include <linux/mount.h>
161da177e4SLinus Torvalds #include <linux/namei.h>
1750e437d5STrond Myklebust #include <linux/fsnotify.h>
181da177e4SLinus Torvalds #include <linux/kernel.h>
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <asm/ioctls.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
221da177e4SLinus Torvalds #include <linux/poll.h>
231da177e4SLinus Torvalds #include <linux/wait.h>
241da177e4SLinus Torvalds #include <linux/seq_file.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
271da177e4SLinus Torvalds #include <linux/workqueue.h>
281da177e4SLinus Torvalds #include <linux/sunrpc/rpc_pipe_fs.h>
298854e82dSTrond Myklebust #include <linux/sunrpc/cache.h>
30021c68deSStanislav Kinsbursky #include <linux/nsproxy.h>
312d00131aSStanislav Kinsbursky #include <linux/notifier.h>
32021c68deSStanislav Kinsbursky 
33021c68deSStanislav Kinsbursky #include "netns.h"
342d00131aSStanislav Kinsbursky #include "sunrpc.h"
351da177e4SLinus Torvalds 
36efc46bf2SStanislav Kinsbursky #define RPCDBG_FACILITY RPCDBG_DEBUG
37efc46bf2SStanislav Kinsbursky 
38efc46bf2SStanislav Kinsbursky #define NET_NAME(net)	((net == &init_net) ? " (init_net)" : "")
39efc46bf2SStanislav Kinsbursky 
40fc14f2feSAl Viro static struct vfsmount *rpc_mnt __read_mostly;
411da177e4SLinus Torvalds static int rpc_mount_count;
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds static struct file_system_type rpc_pipe_fs_type;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 
46e18b890bSChristoph Lameter static struct kmem_cache *rpc_inode_cachep __read_mostly;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #define RPC_UPCALL_TIMEOUT (30*HZ)
491da177e4SLinus Torvalds 
502d00131aSStanislav Kinsbursky static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
512d00131aSStanislav Kinsbursky 
522d00131aSStanislav Kinsbursky int rpc_pipefs_notifier_register(struct notifier_block *nb)
532d00131aSStanislav Kinsbursky {
542d00131aSStanislav Kinsbursky 	return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
552d00131aSStanislav Kinsbursky }
562d00131aSStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
572d00131aSStanislav Kinsbursky 
582d00131aSStanislav Kinsbursky void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
592d00131aSStanislav Kinsbursky {
602d00131aSStanislav Kinsbursky 	blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
612d00131aSStanislav Kinsbursky }
622d00131aSStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
632d00131aSStanislav Kinsbursky 
64ba9e0975SStanislav Kinsbursky static void rpc_purge_list(struct rpc_pipe *pipe, struct list_head *head,
659842ef35STrond Myklebust 		void (*destroy_msg)(struct rpc_pipe_msg *), int err)
66b3eb67a2STrond Myklebust {
67b3eb67a2STrond Myklebust 	struct rpc_pipe_msg *msg;
68b3eb67a2STrond Myklebust 
699842ef35STrond Myklebust 	if (list_empty(head))
709842ef35STrond Myklebust 		return;
719842ef35STrond Myklebust 	do {
72b3eb67a2STrond Myklebust 		msg = list_entry(head->next, struct rpc_pipe_msg, list);
735a67657aSTrond Myklebust 		list_del_init(&msg->list);
74b3eb67a2STrond Myklebust 		msg->errno = err;
75b3eb67a2STrond Myklebust 		destroy_msg(msg);
769842ef35STrond Myklebust 	} while (!list_empty(head));
77ba9e0975SStanislav Kinsbursky 	wake_up(&pipe->waitq);
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds static void
8165f27f38SDavid Howells rpc_timeout_upcall_queue(struct work_struct *work)
821da177e4SLinus Torvalds {
839842ef35STrond Myklebust 	LIST_HEAD(free_list);
84ba9e0975SStanislav Kinsbursky 	struct rpc_pipe *pipe =
85ba9e0975SStanislav Kinsbursky 		container_of(work, struct rpc_pipe, queue_timeout.work);
869842ef35STrond Myklebust 	void (*destroy_msg)(struct rpc_pipe_msg *);
871da177e4SLinus Torvalds 
88ba9e0975SStanislav Kinsbursky 	spin_lock(&pipe->lock);
89ba9e0975SStanislav Kinsbursky 	destroy_msg = pipe->ops->destroy_msg;
90ba9e0975SStanislav Kinsbursky 	if (pipe->nreaders == 0) {
91ba9e0975SStanislav Kinsbursky 		list_splice_init(&pipe->pipe, &free_list);
92ba9e0975SStanislav Kinsbursky 		pipe->pipelen = 0;
939842ef35STrond Myklebust 	}
94ba9e0975SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
95ba9e0975SStanislav Kinsbursky 	rpc_purge_list(pipe, &free_list, destroy_msg, -ETIMEDOUT);
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
98c1225158SPeng Tao ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
99c1225158SPeng Tao 				char __user *dst, size_t buflen)
100c1225158SPeng Tao {
101c1225158SPeng Tao 	char *data = (char *)msg->data + msg->copied;
102c1225158SPeng Tao 	size_t mlen = min(msg->len - msg->copied, buflen);
103c1225158SPeng Tao 	unsigned long left;
104c1225158SPeng Tao 
105c1225158SPeng Tao 	left = copy_to_user(dst, data, mlen);
106c1225158SPeng Tao 	if (left == mlen) {
107c1225158SPeng Tao 		msg->errno = -EFAULT;
108c1225158SPeng Tao 		return -EFAULT;
109c1225158SPeng Tao 	}
110c1225158SPeng Tao 
111c1225158SPeng Tao 	mlen -= left;
112c1225158SPeng Tao 	msg->copied += mlen;
113c1225158SPeng Tao 	msg->errno = 0;
114c1225158SPeng Tao 	return mlen;
115c1225158SPeng Tao }
116c1225158SPeng Tao EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
117c1225158SPeng Tao 
11893a44a75SJ. Bruce Fields /**
1191a5778aaSBen Hutchings  * rpc_queue_upcall - queue an upcall message to userspace
12093a44a75SJ. Bruce Fields  * @inode: inode of upcall pipe on which to queue given message
12193a44a75SJ. Bruce Fields  * @msg: message to queue
12293a44a75SJ. Bruce Fields  *
12393a44a75SJ. Bruce Fields  * Call with an @inode created by rpc_mkpipe() to queue an upcall.
12493a44a75SJ. Bruce Fields  * A userspace process may then later read the upcall by performing a
12593a44a75SJ. Bruce Fields  * read on an open file for this inode.  It is up to the caller to
12693a44a75SJ. Bruce Fields  * initialize the fields of @msg (other than @msg->list) appropriately.
12793a44a75SJ. Bruce Fields  */
1281da177e4SLinus Torvalds int
129d706ed1fSStanislav Kinsbursky rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
1301da177e4SLinus Torvalds {
1316070fe6fSTrond Myklebust 	int res = -EPIPE;
1321da177e4SLinus Torvalds 
133d0fe13baSStanislav Kinsbursky 	spin_lock(&pipe->lock);
134d0fe13baSStanislav Kinsbursky 	if (pipe->nreaders) {
135d0fe13baSStanislav Kinsbursky 		list_add_tail(&msg->list, &pipe->pipe);
136d0fe13baSStanislav Kinsbursky 		pipe->pipelen += msg->len;
1376070fe6fSTrond Myklebust 		res = 0;
138d0fe13baSStanislav Kinsbursky 	} else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
139d0fe13baSStanislav Kinsbursky 		if (list_empty(&pipe->pipe))
14024c5d9d7STrond Myklebust 			queue_delayed_work(rpciod_workqueue,
141d0fe13baSStanislav Kinsbursky 					&pipe->queue_timeout,
1421da177e4SLinus Torvalds 					RPC_UPCALL_TIMEOUT);
143d0fe13baSStanislav Kinsbursky 		list_add_tail(&msg->list, &pipe->pipe);
144d0fe13baSStanislav Kinsbursky 		pipe->pipelen += msg->len;
1456070fe6fSTrond Myklebust 		res = 0;
1466070fe6fSTrond Myklebust 	}
147d0fe13baSStanislav Kinsbursky 	spin_unlock(&pipe->lock);
148d0fe13baSStanislav Kinsbursky 	wake_up(&pipe->waitq);
1491da177e4SLinus Torvalds 	return res;
1501da177e4SLinus Torvalds }
151468039eeSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_queue_upcall);
1521da177e4SLinus Torvalds 
1536070fe6fSTrond Myklebust static inline void
1546070fe6fSTrond Myklebust rpc_inode_setowner(struct inode *inode, void *private)
1556070fe6fSTrond Myklebust {
1566070fe6fSTrond Myklebust 	RPC_I(inode)->private = private;
1576070fe6fSTrond Myklebust }
1586070fe6fSTrond Myklebust 
1591da177e4SLinus Torvalds static void
1601da177e4SLinus Torvalds rpc_close_pipes(struct inode *inode)
1611da177e4SLinus Torvalds {
162ba9e0975SStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
163e712804aS\"J. Bruce Fields\ 	int need_release;
164ad6b1340SStanislav Kinsbursky 	LIST_HEAD(free_list);
1651da177e4SLinus Torvalds 
1661b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
167ba9e0975SStanislav Kinsbursky 	spin_lock(&pipe->lock);
168ba9e0975SStanislav Kinsbursky 	need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
169ba9e0975SStanislav Kinsbursky 	pipe->nreaders = 0;
170ba9e0975SStanislav Kinsbursky 	list_splice_init(&pipe->in_upcall, &free_list);
171ba9e0975SStanislav Kinsbursky 	list_splice_init(&pipe->pipe, &free_list);
172ba9e0975SStanislav Kinsbursky 	pipe->pipelen = 0;
173ad6b1340SStanislav Kinsbursky 	pipe->dentry = NULL;
174ba9e0975SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
175ad6b1340SStanislav Kinsbursky 	rpc_purge_list(pipe, &free_list, pipe->ops->destroy_msg, -EPIPE);
176ba9e0975SStanislav Kinsbursky 	pipe->nwriters = 0;
177ad6b1340SStanislav Kinsbursky 	if (need_release && pipe->ops->release_pipe)
178ad6b1340SStanislav Kinsbursky 		pipe->ops->release_pipe(inode);
179ba9e0975SStanislav Kinsbursky 	cancel_delayed_work_sync(&pipe->queue_timeout);
1806070fe6fSTrond Myklebust 	rpc_inode_setowner(inode, NULL);
1811b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds static struct inode *
1851da177e4SLinus Torvalds rpc_alloc_inode(struct super_block *sb)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	struct rpc_inode *rpci;
188e94b1766SChristoph Lameter 	rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
1891da177e4SLinus Torvalds 	if (!rpci)
1901da177e4SLinus Torvalds 		return NULL;
1911da177e4SLinus Torvalds 	return &rpci->vfs_inode;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static void
195fa0d7e3dSNick Piggin rpc_i_callback(struct rcu_head *head)
196fa0d7e3dSNick Piggin {
197fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
198fa0d7e3dSNick Piggin 	kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
199fa0d7e3dSNick Piggin }
200fa0d7e3dSNick Piggin 
201fa0d7e3dSNick Piggin static void
2021da177e4SLinus Torvalds rpc_destroy_inode(struct inode *inode)
2031da177e4SLinus Torvalds {
204fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, rpc_i_callback);
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds static int
2081da177e4SLinus Torvalds rpc_pipe_open(struct inode *inode, struct file *filp)
2091da177e4SLinus Torvalds {
210d0fe13baSStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
211c3810608S\"J. Bruce Fields\ 	int first_open;
2121da177e4SLinus Torvalds 	int res = -ENXIO;
2131da177e4SLinus Torvalds 
2141b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
215ad6b1340SStanislav Kinsbursky 	if (pipe->dentry == NULL)
216c3810608S\"J. Bruce Fields\ 		goto out;
217d0fe13baSStanislav Kinsbursky 	first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
218d0fe13baSStanislav Kinsbursky 	if (first_open && pipe->ops->open_pipe) {
219d0fe13baSStanislav Kinsbursky 		res = pipe->ops->open_pipe(inode);
220c3810608S\"J. Bruce Fields\ 		if (res)
221c3810608S\"J. Bruce Fields\ 			goto out;
222c3810608S\"J. Bruce Fields\ 	}
2231da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_READ)
224d0fe13baSStanislav Kinsbursky 		pipe->nreaders++;
2251da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_WRITE)
226d0fe13baSStanislav Kinsbursky 		pipe->nwriters++;
2271da177e4SLinus Torvalds 	res = 0;
228c3810608S\"J. Bruce Fields\ out:
2291b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
2301da177e4SLinus Torvalds 	return res;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static int
2341da177e4SLinus Torvalds rpc_pipe_release(struct inode *inode, struct file *filp)
2351da177e4SLinus Torvalds {
236ba9e0975SStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
2371da177e4SLinus Torvalds 	struct rpc_pipe_msg *msg;
238e712804aS\"J. Bruce Fields\ 	int last_close;
2391da177e4SLinus Torvalds 
2401b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
241ad6b1340SStanislav Kinsbursky 	if (pipe->dentry == NULL)
2421da177e4SLinus Torvalds 		goto out;
243655b5bb4SJoe Perches 	msg = filp->private_data;
2441da177e4SLinus Torvalds 	if (msg != NULL) {
245ba9e0975SStanislav Kinsbursky 		spin_lock(&pipe->lock);
24648e49187STrond Myklebust 		msg->errno = -EAGAIN;
2475a67657aSTrond Myklebust 		list_del_init(&msg->list);
248ba9e0975SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
249ba9e0975SStanislav Kinsbursky 		pipe->ops->destroy_msg(msg);
2501da177e4SLinus Torvalds 	}
2511da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_WRITE)
252ba9e0975SStanislav Kinsbursky 		pipe->nwriters --;
2539842ef35STrond Myklebust 	if (filp->f_mode & FMODE_READ) {
254ba9e0975SStanislav Kinsbursky 		pipe->nreaders --;
255ba9e0975SStanislav Kinsbursky 		if (pipe->nreaders == 0) {
2569842ef35STrond Myklebust 			LIST_HEAD(free_list);
257ba9e0975SStanislav Kinsbursky 			spin_lock(&pipe->lock);
258ba9e0975SStanislav Kinsbursky 			list_splice_init(&pipe->pipe, &free_list);
259ba9e0975SStanislav Kinsbursky 			pipe->pipelen = 0;
260ba9e0975SStanislav Kinsbursky 			spin_unlock(&pipe->lock);
261ba9e0975SStanislav Kinsbursky 			rpc_purge_list(pipe, &free_list,
262ba9e0975SStanislav Kinsbursky 					pipe->ops->destroy_msg, -EAGAIN);
2639842ef35STrond Myklebust 		}
2649842ef35STrond Myklebust 	}
265ba9e0975SStanislav Kinsbursky 	last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
266ba9e0975SStanislav Kinsbursky 	if (last_close && pipe->ops->release_pipe)
267ba9e0975SStanislav Kinsbursky 		pipe->ops->release_pipe(inode);
2681da177e4SLinus Torvalds out:
2691b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
2701da177e4SLinus Torvalds 	return 0;
2711da177e4SLinus Torvalds }
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds static ssize_t
2741da177e4SLinus Torvalds rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
2751da177e4SLinus Torvalds {
276303b46bbSJosef Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
277d0fe13baSStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
2781da177e4SLinus Torvalds 	struct rpc_pipe_msg *msg;
2791da177e4SLinus Torvalds 	int res = 0;
2801da177e4SLinus Torvalds 
2811b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
282ad6b1340SStanislav Kinsbursky 	if (pipe->dentry == NULL) {
2831da177e4SLinus Torvalds 		res = -EPIPE;
2841da177e4SLinus Torvalds 		goto out_unlock;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 	msg = filp->private_data;
2871da177e4SLinus Torvalds 	if (msg == NULL) {
288d0fe13baSStanislav Kinsbursky 		spin_lock(&pipe->lock);
289d0fe13baSStanislav Kinsbursky 		if (!list_empty(&pipe->pipe)) {
290d0fe13baSStanislav Kinsbursky 			msg = list_entry(pipe->pipe.next,
2911da177e4SLinus Torvalds 					struct rpc_pipe_msg,
2921da177e4SLinus Torvalds 					list);
293d0fe13baSStanislav Kinsbursky 			list_move(&msg->list, &pipe->in_upcall);
294d0fe13baSStanislav Kinsbursky 			pipe->pipelen -= msg->len;
2951da177e4SLinus Torvalds 			filp->private_data = msg;
2961da177e4SLinus Torvalds 			msg->copied = 0;
2971da177e4SLinus Torvalds 		}
298d0fe13baSStanislav Kinsbursky 		spin_unlock(&pipe->lock);
2991da177e4SLinus Torvalds 		if (msg == NULL)
3001da177e4SLinus Torvalds 			goto out_unlock;
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds 	/* NOTE: it is up to the callback to update msg->copied */
303d0fe13baSStanislav Kinsbursky 	res = pipe->ops->upcall(filp, msg, buf, len);
3041da177e4SLinus Torvalds 	if (res < 0 || msg->len == msg->copied) {
3051da177e4SLinus Torvalds 		filp->private_data = NULL;
306d0fe13baSStanislav Kinsbursky 		spin_lock(&pipe->lock);
3075a67657aSTrond Myklebust 		list_del_init(&msg->list);
308d0fe13baSStanislav Kinsbursky 		spin_unlock(&pipe->lock);
309d0fe13baSStanislav Kinsbursky 		pipe->ops->destroy_msg(msg);
3101da177e4SLinus Torvalds 	}
3111da177e4SLinus Torvalds out_unlock:
3121b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
3131da177e4SLinus Torvalds 	return res;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds static ssize_t
3171da177e4SLinus Torvalds rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
3181da177e4SLinus Torvalds {
319303b46bbSJosef Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
320d0fe13baSStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
3211da177e4SLinus Torvalds 	int res;
3221da177e4SLinus Torvalds 
3231b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
3241da177e4SLinus Torvalds 	res = -EPIPE;
325ad6b1340SStanislav Kinsbursky 	if (pipe->dentry != NULL)
326d0fe13baSStanislav Kinsbursky 		res = pipe->ops->downcall(filp, buf, len);
3271b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
3281da177e4SLinus Torvalds 	return res;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static unsigned int
3321da177e4SLinus Torvalds rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
3331da177e4SLinus Torvalds {
334d0fe13baSStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(filp->f_path.dentry->d_inode)->pipe;
3351da177e4SLinus Torvalds 	unsigned int mask = 0;
3361da177e4SLinus Torvalds 
337d0fe13baSStanislav Kinsbursky 	poll_wait(filp, &pipe->waitq, wait);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	mask = POLLOUT | POLLWRNORM;
340ad6b1340SStanislav Kinsbursky 	if (pipe->dentry == NULL)
3411da177e4SLinus Torvalds 		mask |= POLLERR | POLLHUP;
342d0fe13baSStanislav Kinsbursky 	if (filp->private_data || !list_empty(&pipe->pipe))
3431da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
3441da177e4SLinus Torvalds 	return mask;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
347a6f8dbc6SArnd Bergmann static long
348a6f8dbc6SArnd Bergmann rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3491da177e4SLinus Torvalds {
350a6f8dbc6SArnd Bergmann 	struct inode *inode = filp->f_path.dentry->d_inode;
351d0fe13baSStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
3521da177e4SLinus Torvalds 	int len;
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	switch (cmd) {
3551da177e4SLinus Torvalds 	case FIONREAD:
356d0fe13baSStanislav Kinsbursky 		spin_lock(&pipe->lock);
357ad6b1340SStanislav Kinsbursky 		if (pipe->dentry == NULL) {
358d0fe13baSStanislav Kinsbursky 			spin_unlock(&pipe->lock);
3591da177e4SLinus Torvalds 			return -EPIPE;
360a6f8dbc6SArnd Bergmann 		}
361d0fe13baSStanislav Kinsbursky 		len = pipe->pipelen;
3621da177e4SLinus Torvalds 		if (filp->private_data) {
3631da177e4SLinus Torvalds 			struct rpc_pipe_msg *msg;
364655b5bb4SJoe Perches 			msg = filp->private_data;
3651da177e4SLinus Torvalds 			len += msg->len - msg->copied;
3661da177e4SLinus Torvalds 		}
367d0fe13baSStanislav Kinsbursky 		spin_unlock(&pipe->lock);
3681da177e4SLinus Torvalds 		return put_user(len, (int __user *)arg);
3691da177e4SLinus Torvalds 	default:
3701da177e4SLinus Torvalds 		return -EINVAL;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
374da7071d7SArjan van de Ven static const struct file_operations rpc_pipe_fops = {
3751da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
3761da177e4SLinus Torvalds 	.llseek		= no_llseek,
3771da177e4SLinus Torvalds 	.read		= rpc_pipe_read,
3781da177e4SLinus Torvalds 	.write		= rpc_pipe_write,
3791da177e4SLinus Torvalds 	.poll		= rpc_pipe_poll,
380674b604cSFrederic Weisbecker 	.unlocked_ioctl	= rpc_pipe_ioctl,
3811da177e4SLinus Torvalds 	.open		= rpc_pipe_open,
3821da177e4SLinus Torvalds 	.release	= rpc_pipe_release,
3831da177e4SLinus Torvalds };
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds static int
3861da177e4SLinus Torvalds rpc_show_info(struct seq_file *m, void *v)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct rpc_clnt *clnt = m->private;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	seq_printf(m, "RPC server: %s\n", clnt->cl_server);
3911da177e4SLinus Torvalds 	seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
3921da177e4SLinus Torvalds 			clnt->cl_prog, clnt->cl_vers);
393e7f78657SChuck Lever 	seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
394e7f78657SChuck Lever 	seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
395bf19aaceSJ. Bruce Fields 	seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
3961da177e4SLinus Torvalds 	return 0;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds static int
4001da177e4SLinus Torvalds rpc_info_open(struct inode *inode, struct file *file)
4011da177e4SLinus Torvalds {
402006abe88STrond Myklebust 	struct rpc_clnt *clnt = NULL;
4031da177e4SLinus Torvalds 	int ret = single_open(file, rpc_show_info, NULL);
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	if (!ret) {
4061da177e4SLinus Torvalds 		struct seq_file *m = file->private_data;
407006abe88STrond Myklebust 
408006abe88STrond Myklebust 		spin_lock(&file->f_path.dentry->d_lock);
409006abe88STrond Myklebust 		if (!d_unhashed(file->f_path.dentry))
4101da177e4SLinus Torvalds 			clnt = RPC_I(inode)->private;
411006abe88STrond Myklebust 		if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
412006abe88STrond Myklebust 			spin_unlock(&file->f_path.dentry->d_lock);
4131da177e4SLinus Torvalds 			m->private = clnt;
4141da177e4SLinus Torvalds 		} else {
415006abe88STrond Myklebust 			spin_unlock(&file->f_path.dentry->d_lock);
4161da177e4SLinus Torvalds 			single_release(inode, file);
4171da177e4SLinus Torvalds 			ret = -EINVAL;
4181da177e4SLinus Torvalds 		}
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 	return ret;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds static int
4241da177e4SLinus Torvalds rpc_info_release(struct inode *inode, struct file *file)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	struct seq_file *m = file->private_data;
4271da177e4SLinus Torvalds 	struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	if (clnt)
4301da177e4SLinus Torvalds 		rpc_release_client(clnt);
4311da177e4SLinus Torvalds 	return single_release(inode, file);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
434da7071d7SArjan van de Ven static const struct file_operations rpc_info_operations = {
4351da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
4361da177e4SLinus Torvalds 	.open		= rpc_info_open,
4371da177e4SLinus Torvalds 	.read		= seq_read,
4381da177e4SLinus Torvalds 	.llseek		= seq_lseek,
4391da177e4SLinus Torvalds 	.release	= rpc_info_release,
4401da177e4SLinus Torvalds };
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds /*
4441da177e4SLinus Torvalds  * Description of fs contents.
4451da177e4SLinus Torvalds  */
4461da177e4SLinus Torvalds struct rpc_filelist {
447ac6feceeSTrond Myklebust 	const char *name;
44899ac48f5SArjan van de Ven 	const struct file_operations *i_fop;
4497364af6aSTrond Myklebust 	umode_t mode;
4501da177e4SLinus Torvalds };
4511da177e4SLinus Torvalds 
45254281548STrond Myklebust struct vfsmount *rpc_get_mount(void)
4531da177e4SLinus Torvalds {
45454281548STrond Myklebust 	int err;
45554281548STrond Myklebust 
456fc14f2feSAl Viro 	err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
45754281548STrond Myklebust 	if (err != 0)
45854281548STrond Myklebust 		return ERR_PTR(err);
459fc14f2feSAl Viro 	return rpc_mnt;
4601da177e4SLinus Torvalds }
461e571cbf1STrond Myklebust EXPORT_SYMBOL_GPL(rpc_get_mount);
4621da177e4SLinus Torvalds 
46354281548STrond Myklebust void rpc_put_mount(void)
4641da177e4SLinus Torvalds {
465fc14f2feSAl Viro 	simple_release_fs(&rpc_mnt, &rpc_mount_count);
4661da177e4SLinus Torvalds }
467e571cbf1STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_mount);
4681da177e4SLinus Torvalds 
469fe15ce44SNick Piggin static int rpc_delete_dentry(const struct dentry *dentry)
47062e1761cSTrond Myklebust {
47162e1761cSTrond Myklebust 	return 1;
47262e1761cSTrond Myklebust }
47362e1761cSTrond Myklebust 
4743ba13d17SAl Viro static const struct dentry_operations rpc_dentry_operations = {
47562e1761cSTrond Myklebust 	.d_delete = rpc_delete_dentry,
47662e1761cSTrond Myklebust };
47762e1761cSTrond Myklebust 
4781da177e4SLinus Torvalds static struct inode *
4797364af6aSTrond Myklebust rpc_get_inode(struct super_block *sb, umode_t mode)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	struct inode *inode = new_inode(sb);
4821da177e4SLinus Torvalds 	if (!inode)
4831da177e4SLinus Torvalds 		return NULL;
48485fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
4851da177e4SLinus Torvalds 	inode->i_mode = mode;
4861da177e4SLinus Torvalds 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
4871da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
4881da177e4SLinus Torvalds 	case S_IFDIR:
4891da177e4SLinus Torvalds 		inode->i_fop = &simple_dir_operations;
4901da177e4SLinus Torvalds 		inode->i_op = &simple_dir_inode_operations;
491d8c76e6fSDave Hansen 		inc_nlink(inode);
4921da177e4SLinus Torvalds 	default:
4931da177e4SLinus Torvalds 		break;
4941da177e4SLinus Torvalds 	}
4951da177e4SLinus Torvalds 	return inode;
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4987589806eSTrond Myklebust static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
4997589806eSTrond Myklebust 			       umode_t mode,
5007589806eSTrond Myklebust 			       const struct file_operations *i_fop,
5017589806eSTrond Myklebust 			       void *private)
5027589806eSTrond Myklebust {
5037589806eSTrond Myklebust 	struct inode *inode;
5047589806eSTrond Myklebust 
505beb0f0a9STrond Myklebust 	d_drop(dentry);
5067589806eSTrond Myklebust 	inode = rpc_get_inode(dir->i_sb, mode);
5077589806eSTrond Myklebust 	if (!inode)
5087589806eSTrond Myklebust 		goto out_err;
5097589806eSTrond Myklebust 	inode->i_ino = iunique(dir->i_sb, 100);
5107589806eSTrond Myklebust 	if (i_fop)
5117589806eSTrond Myklebust 		inode->i_fop = i_fop;
5127589806eSTrond Myklebust 	if (private)
5137589806eSTrond Myklebust 		rpc_inode_setowner(inode, private);
5147589806eSTrond Myklebust 	d_add(dentry, inode);
5157589806eSTrond Myklebust 	return 0;
5167589806eSTrond Myklebust out_err:
5177589806eSTrond Myklebust 	printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
5187589806eSTrond Myklebust 			__FILE__, __func__, dentry->d_name.name);
5197589806eSTrond Myklebust 	dput(dentry);
5207589806eSTrond Myklebust 	return -ENOMEM;
5217589806eSTrond Myklebust }
5227589806eSTrond Myklebust 
523ac6feceeSTrond Myklebust static int __rpc_create(struct inode *dir, struct dentry *dentry,
524ac6feceeSTrond Myklebust 			umode_t mode,
525ac6feceeSTrond Myklebust 			const struct file_operations *i_fop,
526ac6feceeSTrond Myklebust 			void *private)
527ac6feceeSTrond Myklebust {
528ac6feceeSTrond Myklebust 	int err;
529ac6feceeSTrond Myklebust 
530ac6feceeSTrond Myklebust 	err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
531ac6feceeSTrond Myklebust 	if (err)
532ac6feceeSTrond Myklebust 		return err;
533ac6feceeSTrond Myklebust 	fsnotify_create(dir, dentry);
534ac6feceeSTrond Myklebust 	return 0;
535ac6feceeSTrond Myklebust }
536ac6feceeSTrond Myklebust 
5377589806eSTrond Myklebust static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
5387589806eSTrond Myklebust 		       umode_t mode,
5397589806eSTrond Myklebust 		       const struct file_operations *i_fop,
5407589806eSTrond Myklebust 		       void *private)
5417589806eSTrond Myklebust {
5427589806eSTrond Myklebust 	int err;
5437589806eSTrond Myklebust 
5447589806eSTrond Myklebust 	err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
5457589806eSTrond Myklebust 	if (err)
5467589806eSTrond Myklebust 		return err;
5477589806eSTrond Myklebust 	inc_nlink(dir);
5487589806eSTrond Myklebust 	fsnotify_mkdir(dir, dentry);
5497589806eSTrond Myklebust 	return 0;
5507589806eSTrond Myklebust }
5517589806eSTrond Myklebust 
552ba9e0975SStanislav Kinsbursky static void
553ba9e0975SStanislav Kinsbursky init_pipe(struct rpc_pipe *pipe)
554ba9e0975SStanislav Kinsbursky {
555ba9e0975SStanislav Kinsbursky 	pipe->nreaders = 0;
556ba9e0975SStanislav Kinsbursky 	pipe->nwriters = 0;
557ba9e0975SStanislav Kinsbursky 	INIT_LIST_HEAD(&pipe->in_upcall);
558ba9e0975SStanislav Kinsbursky 	INIT_LIST_HEAD(&pipe->in_downcall);
559ba9e0975SStanislav Kinsbursky 	INIT_LIST_HEAD(&pipe->pipe);
560ba9e0975SStanislav Kinsbursky 	pipe->pipelen = 0;
561ba9e0975SStanislav Kinsbursky 	init_waitqueue_head(&pipe->waitq);
562ba9e0975SStanislav Kinsbursky 	INIT_DELAYED_WORK(&pipe->queue_timeout,
563ba9e0975SStanislav Kinsbursky 			    rpc_timeout_upcall_queue);
564ba9e0975SStanislav Kinsbursky 	pipe->ops = NULL;
565ba9e0975SStanislav Kinsbursky 	spin_lock_init(&pipe->lock);
566c239d83bSStanislav Kinsbursky 	pipe->dentry = NULL;
567ba9e0975SStanislav Kinsbursky }
568ba9e0975SStanislav Kinsbursky 
569c239d83bSStanislav Kinsbursky void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
570c239d83bSStanislav Kinsbursky {
571c239d83bSStanislav Kinsbursky 	kfree(pipe);
572c239d83bSStanislav Kinsbursky }
573c239d83bSStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
574c239d83bSStanislav Kinsbursky 
575c239d83bSStanislav Kinsbursky struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
5767589806eSTrond Myklebust {
577ba9e0975SStanislav Kinsbursky 	struct rpc_pipe *pipe;
5787589806eSTrond Myklebust 
579ba9e0975SStanislav Kinsbursky 	pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
580ba9e0975SStanislav Kinsbursky 	if (!pipe)
581c239d83bSStanislav Kinsbursky 		return ERR_PTR(-ENOMEM);
582ba9e0975SStanislav Kinsbursky 	init_pipe(pipe);
583c239d83bSStanislav Kinsbursky 	pipe->ops = ops;
584c239d83bSStanislav Kinsbursky 	pipe->flags = flags;
585c239d83bSStanislav Kinsbursky 	return pipe;
586ba9e0975SStanislav Kinsbursky }
587c239d83bSStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
588c239d83bSStanislav Kinsbursky 
589c239d83bSStanislav Kinsbursky static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
590c239d83bSStanislav Kinsbursky 			       umode_t mode,
591c239d83bSStanislav Kinsbursky 			       const struct file_operations *i_fop,
592c239d83bSStanislav Kinsbursky 			       void *private,
593c239d83bSStanislav Kinsbursky 			       struct rpc_pipe *pipe)
594c239d83bSStanislav Kinsbursky {
595c239d83bSStanislav Kinsbursky 	struct rpc_inode *rpci;
596c239d83bSStanislav Kinsbursky 	int err;
597c239d83bSStanislav Kinsbursky 
598c239d83bSStanislav Kinsbursky 	err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
599c239d83bSStanislav Kinsbursky 	if (err)
600c239d83bSStanislav Kinsbursky 		return err;
6017589806eSTrond Myklebust 	rpci = RPC_I(dentry->d_inode);
6027589806eSTrond Myklebust 	rpci->private = private;
603ba9e0975SStanislav Kinsbursky 	rpci->pipe = pipe;
6047589806eSTrond Myklebust 	fsnotify_create(dir, dentry);
6057589806eSTrond Myklebust 	return 0;
6067589806eSTrond Myklebust }
6077589806eSTrond Myklebust 
608ac6feceeSTrond Myklebust static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
609ac6feceeSTrond Myklebust {
610ac6feceeSTrond Myklebust 	int ret;
611ac6feceeSTrond Myklebust 
612ac6feceeSTrond Myklebust 	dget(dentry);
613ac6feceeSTrond Myklebust 	ret = simple_rmdir(dir, dentry);
614ac6feceeSTrond Myklebust 	d_delete(dentry);
615ac6feceeSTrond Myklebust 	dput(dentry);
616ac6feceeSTrond Myklebust 	return ret;
617ac6feceeSTrond Myklebust }
618ac6feceeSTrond Myklebust 
619eee17325SStanislav Kinsbursky int rpc_rmdir(struct dentry *dentry)
620eee17325SStanislav Kinsbursky {
621eee17325SStanislav Kinsbursky 	struct dentry *parent;
622eee17325SStanislav Kinsbursky 	struct inode *dir;
623eee17325SStanislav Kinsbursky 	int error;
624eee17325SStanislav Kinsbursky 
625eee17325SStanislav Kinsbursky 	parent = dget_parent(dentry);
626eee17325SStanislav Kinsbursky 	dir = parent->d_inode;
627eee17325SStanislav Kinsbursky 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
628eee17325SStanislav Kinsbursky 	error = __rpc_rmdir(dir, dentry);
629eee17325SStanislav Kinsbursky 	mutex_unlock(&dir->i_mutex);
630eee17325SStanislav Kinsbursky 	dput(parent);
631eee17325SStanislav Kinsbursky 	return error;
632eee17325SStanislav Kinsbursky }
633eee17325SStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_rmdir);
634eee17325SStanislav Kinsbursky 
635810d90bcSTrond Myklebust static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
636810d90bcSTrond Myklebust {
637810d90bcSTrond Myklebust 	int ret;
638810d90bcSTrond Myklebust 
639810d90bcSTrond Myklebust 	dget(dentry);
640810d90bcSTrond Myklebust 	ret = simple_unlink(dir, dentry);
641810d90bcSTrond Myklebust 	d_delete(dentry);
642810d90bcSTrond Myklebust 	dput(dentry);
643810d90bcSTrond Myklebust 	return ret;
644810d90bcSTrond Myklebust }
645810d90bcSTrond Myklebust 
646810d90bcSTrond Myklebust static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
647810d90bcSTrond Myklebust {
648810d90bcSTrond Myklebust 	struct inode *inode = dentry->d_inode;
649810d90bcSTrond Myklebust 
650810d90bcSTrond Myklebust 	rpc_close_pipes(inode);
651810d90bcSTrond Myklebust 	return __rpc_unlink(dir, dentry);
652810d90bcSTrond Myklebust }
653810d90bcSTrond Myklebust 
654cfeaa4a3STrond Myklebust static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
655cfeaa4a3STrond Myklebust 					  struct qstr *name)
656cfeaa4a3STrond Myklebust {
657cfeaa4a3STrond Myklebust 	struct dentry *dentry;
658cfeaa4a3STrond Myklebust 
6595bff0386SStanislav Kinsbursky 	dentry = d_lookup(parent, name);
6605bff0386SStanislav Kinsbursky 	if (!dentry) {
6615bff0386SStanislav Kinsbursky 		dentry = d_alloc(parent, name);
6625bff0386SStanislav Kinsbursky 		if (!dentry)
6635bff0386SStanislav Kinsbursky 			return ERR_PTR(-ENOMEM);
6645bff0386SStanislav Kinsbursky 	}
6655bff0386SStanislav Kinsbursky 	if (dentry->d_inode == NULL) {
6665bff0386SStanislav Kinsbursky 		d_set_d_op(dentry, &rpc_dentry_operations);
667f1f0abe1SDan Carpenter 		return dentry;
6685bff0386SStanislav Kinsbursky 	}
669cfeaa4a3STrond Myklebust 	dput(dentry);
670cfeaa4a3STrond Myklebust 	return ERR_PTR(-EEXIST);
671cfeaa4a3STrond Myklebust }
672cfeaa4a3STrond Myklebust 
6731da177e4SLinus Torvalds /*
6741da177e4SLinus Torvalds  * FIXME: This probably has races.
6751da177e4SLinus Torvalds  */
676ac6feceeSTrond Myklebust static void __rpc_depopulate(struct dentry *parent,
677ac6feceeSTrond Myklebust 			     const struct rpc_filelist *files,
678ac6feceeSTrond Myklebust 			     int start, int eof)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	struct inode *dir = parent->d_inode;
681ac6feceeSTrond Myklebust 	struct dentry *dentry;
682ac6feceeSTrond Myklebust 	struct qstr name;
683ac6feceeSTrond Myklebust 	int i;
684ac6feceeSTrond Myklebust 
685ac6feceeSTrond Myklebust 	for (i = start; i < eof; i++) {
686ac6feceeSTrond Myklebust 		name.name = files[i].name;
687ac6feceeSTrond Myklebust 		name.len = strlen(files[i].name);
688ac6feceeSTrond Myklebust 		name.hash = full_name_hash(name.name, name.len);
689ac6feceeSTrond Myklebust 		dentry = d_lookup(parent, &name);
690ac6feceeSTrond Myklebust 
691ac6feceeSTrond Myklebust 		if (dentry == NULL)
692ac6feceeSTrond Myklebust 			continue;
693ac6feceeSTrond Myklebust 		if (dentry->d_inode == NULL)
694ac6feceeSTrond Myklebust 			goto next;
695ac6feceeSTrond Myklebust 		switch (dentry->d_inode->i_mode & S_IFMT) {
696ac6feceeSTrond Myklebust 			default:
697ac6feceeSTrond Myklebust 				BUG();
698ac6feceeSTrond Myklebust 			case S_IFREG:
699ac6feceeSTrond Myklebust 				__rpc_unlink(dir, dentry);
700ac6feceeSTrond Myklebust 				break;
701ac6feceeSTrond Myklebust 			case S_IFDIR:
702ac6feceeSTrond Myklebust 				__rpc_rmdir(dir, dentry);
703ac6feceeSTrond Myklebust 		}
704ac6feceeSTrond Myklebust next:
705ac6feceeSTrond Myklebust 		dput(dentry);
706ac6feceeSTrond Myklebust 	}
707ac6feceeSTrond Myklebust }
708ac6feceeSTrond Myklebust 
709ac6feceeSTrond Myklebust static void rpc_depopulate(struct dentry *parent,
710ac6feceeSTrond Myklebust 			   const struct rpc_filelist *files,
711ac6feceeSTrond Myklebust 			   int start, int eof)
712ac6feceeSTrond Myklebust {
713ac6feceeSTrond Myklebust 	struct inode *dir = parent->d_inode;
7141da177e4SLinus Torvalds 
715c6573c29SArjan van de Ven 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
716ac6feceeSTrond Myklebust 	__rpc_depopulate(parent, files, start, eof);
7171b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
720ac6feceeSTrond Myklebust static int rpc_populate(struct dentry *parent,
721ac6feceeSTrond Myklebust 			const struct rpc_filelist *files,
722ac6feceeSTrond Myklebust 			int start, int eof,
723ac6feceeSTrond Myklebust 			void *private)
7241da177e4SLinus Torvalds {
725ac6feceeSTrond Myklebust 	struct inode *dir = parent->d_inode;
7261da177e4SLinus Torvalds 	struct dentry *dentry;
727ac6feceeSTrond Myklebust 	int i, err;
7281da177e4SLinus Torvalds 
7291b1dcc1bSJes Sorensen 	mutex_lock(&dir->i_mutex);
7301da177e4SLinus Torvalds 	for (i = start; i < eof; i++) {
731ac6feceeSTrond Myklebust 		struct qstr q;
732ac6feceeSTrond Myklebust 
733ac6feceeSTrond Myklebust 		q.name = files[i].name;
734ac6feceeSTrond Myklebust 		q.len = strlen(files[i].name);
735ac6feceeSTrond Myklebust 		q.hash = full_name_hash(q.name, q.len);
736ac6feceeSTrond Myklebust 		dentry = __rpc_lookup_create_exclusive(parent, &q);
737ac6feceeSTrond Myklebust 		err = PTR_ERR(dentry);
738ac6feceeSTrond Myklebust 		if (IS_ERR(dentry))
7391da177e4SLinus Torvalds 			goto out_bad;
740ac6feceeSTrond Myklebust 		switch (files[i].mode & S_IFMT) {
741ac6feceeSTrond Myklebust 			default:
742ac6feceeSTrond Myklebust 				BUG();
743ac6feceeSTrond Myklebust 			case S_IFREG:
744ac6feceeSTrond Myklebust 				err = __rpc_create(dir, dentry,
745ac6feceeSTrond Myklebust 						files[i].mode,
746ac6feceeSTrond Myklebust 						files[i].i_fop,
747ac6feceeSTrond Myklebust 						private);
748ac6feceeSTrond Myklebust 				break;
749ac6feceeSTrond Myklebust 			case S_IFDIR:
750ac6feceeSTrond Myklebust 				err = __rpc_mkdir(dir, dentry,
751ac6feceeSTrond Myklebust 						files[i].mode,
752ac6feceeSTrond Myklebust 						NULL,
753ac6feceeSTrond Myklebust 						private);
7541da177e4SLinus Torvalds 		}
755ac6feceeSTrond Myklebust 		if (err != 0)
756ac6feceeSTrond Myklebust 			goto out_bad;
7571da177e4SLinus Torvalds 	}
7581b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
7591da177e4SLinus Torvalds 	return 0;
7601da177e4SLinus Torvalds out_bad:
761ac6feceeSTrond Myklebust 	__rpc_depopulate(parent, files, start, eof);
7621b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
7631da177e4SLinus Torvalds 	printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
7640dc47877SHarvey Harrison 			__FILE__, __func__, parent->d_name.name);
765ac6feceeSTrond Myklebust 	return err;
766f134585aSTrond Myklebust }
767f134585aSTrond Myklebust 
768e57aed77STrond Myklebust static struct dentry *rpc_mkdir_populate(struct dentry *parent,
769e57aed77STrond Myklebust 		struct qstr *name, umode_t mode, void *private,
770e57aed77STrond Myklebust 		int (*populate)(struct dentry *, void *), void *args_populate)
771f134585aSTrond Myklebust {
772f134585aSTrond Myklebust 	struct dentry *dentry;
7737d59d1e8STrond Myklebust 	struct inode *dir = parent->d_inode;
774f134585aSTrond Myklebust 	int error;
775f134585aSTrond Myklebust 
7767d59d1e8STrond Myklebust 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
7777d59d1e8STrond Myklebust 	dentry = __rpc_lookup_create_exclusive(parent, name);
778f134585aSTrond Myklebust 	if (IS_ERR(dentry))
7797d59d1e8STrond Myklebust 		goto out;
7807d59d1e8STrond Myklebust 	error = __rpc_mkdir(dir, dentry, mode, NULL, private);
7817589806eSTrond Myklebust 	if (error != 0)
7827589806eSTrond Myklebust 		goto out_err;
783e57aed77STrond Myklebust 	if (populate != NULL) {
784e57aed77STrond Myklebust 		error = populate(dentry, args_populate);
785f134585aSTrond Myklebust 		if (error)
786ac6feceeSTrond Myklebust 			goto err_rmdir;
787e57aed77STrond Myklebust 	}
788f134585aSTrond Myklebust out:
7891b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
7905c3e985aSTrond Myklebust 	return dentry;
791ac6feceeSTrond Myklebust err_rmdir:
792f134585aSTrond Myklebust 	__rpc_rmdir(dir, dentry);
7937589806eSTrond Myklebust out_err:
794f134585aSTrond Myklebust 	dentry = ERR_PTR(error);
795f134585aSTrond Myklebust 	goto out;
796f134585aSTrond Myklebust }
797f134585aSTrond Myklebust 
798e57aed77STrond Myklebust static int rpc_rmdir_depopulate(struct dentry *dentry,
799e57aed77STrond Myklebust 		void (*depopulate)(struct dentry *))
800f134585aSTrond Myklebust {
801dff02cc1STrond Myklebust 	struct dentry *parent;
802f134585aSTrond Myklebust 	struct inode *dir;
803f134585aSTrond Myklebust 	int error;
804f134585aSTrond Myklebust 
805dff02cc1STrond Myklebust 	parent = dget_parent(dentry);
806dff02cc1STrond Myklebust 	dir = parent->d_inode;
807c6573c29SArjan van de Ven 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
808e57aed77STrond Myklebust 	if (depopulate != NULL)
809e57aed77STrond Myklebust 		depopulate(dentry);
810f134585aSTrond Myklebust 	error = __rpc_rmdir(dir, dentry);
8111b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
812dff02cc1STrond Myklebust 	dput(parent);
813f134585aSTrond Myklebust 	return error;
814f134585aSTrond Myklebust }
8151da177e4SLinus Torvalds 
81693a44a75SJ. Bruce Fields /**
81793a44a75SJ. Bruce Fields  * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
81893a44a75SJ. Bruce Fields  * @parent: dentry of directory to create new "pipe" in
81993a44a75SJ. Bruce Fields  * @name: name of pipe
82093a44a75SJ. Bruce Fields  * @private: private data to associate with the pipe, for the caller's use
82193a44a75SJ. Bruce Fields  * @ops: operations defining the behavior of the pipe: upcall, downcall,
822c3810608S\"J. Bruce Fields\  *	release_pipe, open_pipe, and destroy_msg.
823d0fe13baSStanislav Kinsbursky  * @flags: rpc_pipe flags
82493a44a75SJ. Bruce Fields  *
82593a44a75SJ. Bruce Fields  * Data is made available for userspace to read by calls to
82693a44a75SJ. Bruce Fields  * rpc_queue_upcall().  The actual reads will result in calls to
82793a44a75SJ. Bruce Fields  * @ops->upcall, which will be called with the file pointer,
82893a44a75SJ. Bruce Fields  * message, and userspace buffer to copy to.
82993a44a75SJ. Bruce Fields  *
83093a44a75SJ. Bruce Fields  * Writes can come at any time, and do not necessarily have to be
83193a44a75SJ. Bruce Fields  * responses to upcalls.  They will result in calls to @msg->downcall.
83293a44a75SJ. Bruce Fields  *
83393a44a75SJ. Bruce Fields  * The @private argument passed here will be available to all these methods
83493a44a75SJ. Bruce Fields  * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
83593a44a75SJ. Bruce Fields  */
836c239d83bSStanislav Kinsbursky struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
837c239d83bSStanislav Kinsbursky 				 void *private, struct rpc_pipe *pipe)
8381da177e4SLinus Torvalds {
8391da177e4SLinus Torvalds 	struct dentry *dentry;
8407589806eSTrond Myklebust 	struct inode *dir = parent->d_inode;
8417364af6aSTrond Myklebust 	umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
842cfeaa4a3STrond Myklebust 	struct qstr q;
8437589806eSTrond Myklebust 	int err;
8447364af6aSTrond Myklebust 
845c239d83bSStanislav Kinsbursky 	if (pipe->ops->upcall == NULL)
8467364af6aSTrond Myklebust 		umode &= ~S_IRUGO;
847c239d83bSStanislav Kinsbursky 	if (pipe->ops->downcall == NULL)
8487364af6aSTrond Myklebust 		umode &= ~S_IWUGO;
8491da177e4SLinus Torvalds 
850cfeaa4a3STrond Myklebust 	q.name = name;
851cfeaa4a3STrond Myklebust 	q.len = strlen(name);
852cfeaa4a3STrond Myklebust 	q.hash = full_name_hash(q.name, q.len),
853cfeaa4a3STrond Myklebust 
854cfeaa4a3STrond Myklebust 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
8555bff0386SStanislav Kinsbursky 	dentry = __rpc_lookup_create_exclusive(parent, &q);
8561da177e4SLinus Torvalds 	if (IS_ERR(dentry))
857cfeaa4a3STrond Myklebust 		goto out;
858c239d83bSStanislav Kinsbursky 	err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
859c239d83bSStanislav Kinsbursky 				  private, pipe);
8607589806eSTrond Myklebust 	if (err)
8617589806eSTrond Myklebust 		goto out_err;
862f134585aSTrond Myklebust out:
8631b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
8645c3e985aSTrond Myklebust 	return dentry;
8657589806eSTrond Myklebust out_err:
8667589806eSTrond Myklebust 	dentry = ERR_PTR(err);
867158998b6STrond Myklebust 	printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
8680dc47877SHarvey Harrison 			__FILE__, __func__, parent->d_name.name, name,
8697589806eSTrond Myklebust 			err);
870f134585aSTrond Myklebust 	goto out;
8711da177e4SLinus Torvalds }
872c239d83bSStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
8731da177e4SLinus Torvalds 
87493a44a75SJ. Bruce Fields /**
87593a44a75SJ. Bruce Fields  * rpc_unlink - remove a pipe
87693a44a75SJ. Bruce Fields  * @dentry: dentry for the pipe, as returned from rpc_mkpipe
87793a44a75SJ. Bruce Fields  *
87893a44a75SJ. Bruce Fields  * After this call, lookups will no longer find the pipe, and any
87993a44a75SJ. Bruce Fields  * attempts to read or write using preexisting opens of the pipe will
88093a44a75SJ. Bruce Fields  * return -EPIPE.
88193a44a75SJ. Bruce Fields  */
882f134585aSTrond Myklebust int
8835d67476fSTrond Myklebust rpc_unlink(struct dentry *dentry)
8841da177e4SLinus Torvalds {
8855d67476fSTrond Myklebust 	struct dentry *parent;
886f134585aSTrond Myklebust 	struct inode *dir;
8875d67476fSTrond Myklebust 	int error = 0;
8881da177e4SLinus Torvalds 
8895d67476fSTrond Myklebust 	parent = dget_parent(dentry);
8905d67476fSTrond Myklebust 	dir = parent->d_inode;
891c6573c29SArjan van de Ven 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
892810d90bcSTrond Myklebust 	error = __rpc_rmpipe(dir, dentry);
8931b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
8945d67476fSTrond Myklebust 	dput(parent);
895f134585aSTrond Myklebust 	return error;
8961da177e4SLinus Torvalds }
897468039eeSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_unlink);
8981da177e4SLinus Torvalds 
899e57aed77STrond Myklebust enum {
900e57aed77STrond Myklebust 	RPCAUTH_info,
901e57aed77STrond Myklebust 	RPCAUTH_EOF
902e57aed77STrond Myklebust };
903e57aed77STrond Myklebust 
904e57aed77STrond Myklebust static const struct rpc_filelist authfiles[] = {
905e57aed77STrond Myklebust 	[RPCAUTH_info] = {
906e57aed77STrond Myklebust 		.name = "info",
907e57aed77STrond Myklebust 		.i_fop = &rpc_info_operations,
908e57aed77STrond Myklebust 		.mode = S_IFREG | S_IRUSR,
909e57aed77STrond Myklebust 	},
910e57aed77STrond Myklebust };
911e57aed77STrond Myklebust 
912e57aed77STrond Myklebust static int rpc_clntdir_populate(struct dentry *dentry, void *private)
913e57aed77STrond Myklebust {
914e57aed77STrond Myklebust 	return rpc_populate(dentry,
915e57aed77STrond Myklebust 			    authfiles, RPCAUTH_info, RPCAUTH_EOF,
916e57aed77STrond Myklebust 			    private);
917e57aed77STrond Myklebust }
918e57aed77STrond Myklebust 
919e57aed77STrond Myklebust static void rpc_clntdir_depopulate(struct dentry *dentry)
920e57aed77STrond Myklebust {
921e57aed77STrond Myklebust 	rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
922e57aed77STrond Myklebust }
923e57aed77STrond Myklebust 
9247d59d1e8STrond Myklebust /**
9257d59d1e8STrond Myklebust  * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
9264111d4fdSRandy Dunlap  * @dentry: dentry from the rpc_pipefs root to the new directory
9274111d4fdSRandy Dunlap  * @name: &struct qstr for the name
9287d59d1e8STrond Myklebust  * @rpc_client: rpc client to associate with this directory
9297d59d1e8STrond Myklebust  *
9307d59d1e8STrond Myklebust  * This creates a directory at the given @path associated with
9317d59d1e8STrond Myklebust  * @rpc_clnt, which will contain a file named "info" with some basic
9327d59d1e8STrond Myklebust  * information about the client, together with any "pipes" that may
9337d59d1e8STrond Myklebust  * later be created using rpc_mkpipe().
9347d59d1e8STrond Myklebust  */
93523ac6581STrond Myklebust struct dentry *rpc_create_client_dir(struct dentry *dentry,
93623ac6581STrond Myklebust 				   struct qstr *name,
9377d59d1e8STrond Myklebust 				   struct rpc_clnt *rpc_client)
9387d59d1e8STrond Myklebust {
939e57aed77STrond Myklebust 	return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
940e57aed77STrond Myklebust 			rpc_clntdir_populate, rpc_client);
941e57aed77STrond Myklebust }
942e57aed77STrond Myklebust 
943e57aed77STrond Myklebust /**
944e57aed77STrond Myklebust  * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
94580df9d20SStanislav Kinsbursky  * @clnt: rpc client
946e57aed77STrond Myklebust  */
947e57aed77STrond Myklebust int rpc_remove_client_dir(struct dentry *dentry)
948e57aed77STrond Myklebust {
949e57aed77STrond Myklebust 	return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
9507d59d1e8STrond Myklebust }
9517d59d1e8STrond Myklebust 
9528854e82dSTrond Myklebust static const struct rpc_filelist cache_pipefs_files[3] = {
9538854e82dSTrond Myklebust 	[0] = {
9548854e82dSTrond Myklebust 		.name = "channel",
9558854e82dSTrond Myklebust 		.i_fop = &cache_file_operations_pipefs,
95696c61cbdSTrond Myklebust 		.mode = S_IFREG|S_IRUSR|S_IWUSR,
9578854e82dSTrond Myklebust 	},
9588854e82dSTrond Myklebust 	[1] = {
9598854e82dSTrond Myklebust 		.name = "content",
9608854e82dSTrond Myklebust 		.i_fop = &content_file_operations_pipefs,
9618854e82dSTrond Myklebust 		.mode = S_IFREG|S_IRUSR,
9628854e82dSTrond Myklebust 	},
9638854e82dSTrond Myklebust 	[2] = {
9648854e82dSTrond Myklebust 		.name = "flush",
9658854e82dSTrond Myklebust 		.i_fop = &cache_flush_operations_pipefs,
9668854e82dSTrond Myklebust 		.mode = S_IFREG|S_IRUSR|S_IWUSR,
9678854e82dSTrond Myklebust 	},
9688854e82dSTrond Myklebust };
9698854e82dSTrond Myklebust 
9708854e82dSTrond Myklebust static int rpc_cachedir_populate(struct dentry *dentry, void *private)
9718854e82dSTrond Myklebust {
9728854e82dSTrond Myklebust 	return rpc_populate(dentry,
9738854e82dSTrond Myklebust 			    cache_pipefs_files, 0, 3,
9748854e82dSTrond Myklebust 			    private);
9758854e82dSTrond Myklebust }
9768854e82dSTrond Myklebust 
9778854e82dSTrond Myklebust static void rpc_cachedir_depopulate(struct dentry *dentry)
9788854e82dSTrond Myklebust {
9798854e82dSTrond Myklebust 	rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
9808854e82dSTrond Myklebust }
9818854e82dSTrond Myklebust 
9828854e82dSTrond Myklebust struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
98364f1426fSAl Viro 				    umode_t umode, struct cache_detail *cd)
9848854e82dSTrond Myklebust {
9858854e82dSTrond Myklebust 	return rpc_mkdir_populate(parent, name, umode, NULL,
9868854e82dSTrond Myklebust 			rpc_cachedir_populate, cd);
9878854e82dSTrond Myklebust }
9888854e82dSTrond Myklebust 
9898854e82dSTrond Myklebust void rpc_remove_cache_dir(struct dentry *dentry)
9908854e82dSTrond Myklebust {
9918854e82dSTrond Myklebust 	rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
9928854e82dSTrond Myklebust }
9938854e82dSTrond Myklebust 
9941da177e4SLinus Torvalds /*
9951da177e4SLinus Torvalds  * populate the filesystem
9961da177e4SLinus Torvalds  */
997b87221deSAlexey Dobriyan static const struct super_operations s_ops = {
9981da177e4SLinus Torvalds 	.alloc_inode	= rpc_alloc_inode,
9991da177e4SLinus Torvalds 	.destroy_inode	= rpc_destroy_inode,
10001da177e4SLinus Torvalds 	.statfs		= simple_statfs,
10011da177e4SLinus Torvalds };
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds #define RPCAUTH_GSSMAGIC 0x67596969
10041da177e4SLinus Torvalds 
1005bb156749STrond Myklebust /*
1006bb156749STrond Myklebust  * We have a single directory with 1 node in it.
1007bb156749STrond Myklebust  */
1008bb156749STrond Myklebust enum {
1009bb156749STrond Myklebust 	RPCAUTH_lockd,
1010bb156749STrond Myklebust 	RPCAUTH_mount,
1011bb156749STrond Myklebust 	RPCAUTH_nfs,
1012bb156749STrond Myklebust 	RPCAUTH_portmap,
1013bb156749STrond Myklebust 	RPCAUTH_statd,
1014bb156749STrond Myklebust 	RPCAUTH_nfsd4_cb,
1015e571cbf1STrond Myklebust 	RPCAUTH_cache,
1016bb156749STrond Myklebust 	RPCAUTH_RootEOF
1017bb156749STrond Myklebust };
1018bb156749STrond Myklebust 
1019bb156749STrond Myklebust static const struct rpc_filelist files[] = {
1020bb156749STrond Myklebust 	[RPCAUTH_lockd] = {
1021bb156749STrond Myklebust 		.name = "lockd",
1022bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1023bb156749STrond Myklebust 	},
1024bb156749STrond Myklebust 	[RPCAUTH_mount] = {
1025bb156749STrond Myklebust 		.name = "mount",
1026bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1027bb156749STrond Myklebust 	},
1028bb156749STrond Myklebust 	[RPCAUTH_nfs] = {
1029bb156749STrond Myklebust 		.name = "nfs",
1030bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1031bb156749STrond Myklebust 	},
1032bb156749STrond Myklebust 	[RPCAUTH_portmap] = {
1033bb156749STrond Myklebust 		.name = "portmap",
1034bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1035bb156749STrond Myklebust 	},
1036bb156749STrond Myklebust 	[RPCAUTH_statd] = {
1037bb156749STrond Myklebust 		.name = "statd",
1038bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1039bb156749STrond Myklebust 	},
1040bb156749STrond Myklebust 	[RPCAUTH_nfsd4_cb] = {
1041bb156749STrond Myklebust 		.name = "nfsd4_cb",
1042bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1043bb156749STrond Myklebust 	},
1044e571cbf1STrond Myklebust 	[RPCAUTH_cache] = {
1045e571cbf1STrond Myklebust 		.name = "cache",
1046e571cbf1STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1047e571cbf1STrond Myklebust 	},
1048bb156749STrond Myklebust };
1049bb156749STrond Myklebust 
1050432eb1a5SStanislav Kinsbursky /*
1051432eb1a5SStanislav Kinsbursky  * This call can be used only in RPC pipefs mount notification hooks.
1052432eb1a5SStanislav Kinsbursky  */
1053432eb1a5SStanislav Kinsbursky struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1054432eb1a5SStanislav Kinsbursky 			       const unsigned char *dir_name)
1055432eb1a5SStanislav Kinsbursky {
1056432eb1a5SStanislav Kinsbursky 	struct qstr dir = {
1057432eb1a5SStanislav Kinsbursky 		.name = dir_name,
1058432eb1a5SStanislav Kinsbursky 		.len = strlen(dir_name),
1059432eb1a5SStanislav Kinsbursky 		.hash = full_name_hash(dir_name, strlen(dir_name)),
1060432eb1a5SStanislav Kinsbursky 	};
1061432eb1a5SStanislav Kinsbursky 
1062432eb1a5SStanislav Kinsbursky 	return d_lookup(sb->s_root, &dir);
1063432eb1a5SStanislav Kinsbursky }
1064432eb1a5SStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1065432eb1a5SStanislav Kinsbursky 
1066c21a588fSStanislav Kinsbursky void rpc_pipefs_init_net(struct net *net)
1067c21a588fSStanislav Kinsbursky {
1068c21a588fSStanislav Kinsbursky 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1069c21a588fSStanislav Kinsbursky 
1070c21a588fSStanislav Kinsbursky 	mutex_init(&sn->pipefs_sb_lock);
1071c21a588fSStanislav Kinsbursky }
1072c21a588fSStanislav Kinsbursky 
1073c21a588fSStanislav Kinsbursky /*
1074c21a588fSStanislav Kinsbursky  * This call will be used for per network namespace operations calls.
1075c21a588fSStanislav Kinsbursky  * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1076c21a588fSStanislav Kinsbursky  * found. This lock have to be released by rpc_put_sb_net() when all operations
1077c21a588fSStanislav Kinsbursky  * will be completed.
1078c21a588fSStanislav Kinsbursky  */
1079c21a588fSStanislav Kinsbursky struct super_block *rpc_get_sb_net(const struct net *net)
1080c21a588fSStanislav Kinsbursky {
1081c21a588fSStanislav Kinsbursky 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1082c21a588fSStanislav Kinsbursky 
1083c21a588fSStanislav Kinsbursky 	mutex_lock(&sn->pipefs_sb_lock);
1084c21a588fSStanislav Kinsbursky 	if (sn->pipefs_sb)
1085c21a588fSStanislav Kinsbursky 		return sn->pipefs_sb;
1086c21a588fSStanislav Kinsbursky 	mutex_unlock(&sn->pipefs_sb_lock);
1087c21a588fSStanislav Kinsbursky 	return NULL;
1088c21a588fSStanislav Kinsbursky }
1089c21a588fSStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1090c21a588fSStanislav Kinsbursky 
1091c21a588fSStanislav Kinsbursky void rpc_put_sb_net(const struct net *net)
1092c21a588fSStanislav Kinsbursky {
1093c21a588fSStanislav Kinsbursky 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1094c21a588fSStanislav Kinsbursky 
1095c21a588fSStanislav Kinsbursky 	BUG_ON(sn->pipefs_sb == NULL);
1096c21a588fSStanislav Kinsbursky 	mutex_unlock(&sn->pipefs_sb_lock);
1097c21a588fSStanislav Kinsbursky }
1098c21a588fSStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1099c21a588fSStanislav Kinsbursky 
11001da177e4SLinus Torvalds static int
11011da177e4SLinus Torvalds rpc_fill_super(struct super_block *sb, void *data, int silent)
11021da177e4SLinus Torvalds {
11031da177e4SLinus Torvalds 	struct inode *inode;
11041da177e4SLinus Torvalds 	struct dentry *root;
110538b0da75SStanislav Kinsbursky 	struct net *net = data;
110690c4e829SStanislav Kinsbursky 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
11072d00131aSStanislav Kinsbursky 	int err;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
11101da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
11111da177e4SLinus Torvalds 	sb->s_magic = RPCAUTH_GSSMAGIC;
11121da177e4SLinus Torvalds 	sb->s_op = &s_ops;
11131da177e4SLinus Torvalds 	sb->s_time_gran = 1;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	inode = rpc_get_inode(sb, S_IFDIR | 0755);
11161da177e4SLinus Torvalds 	if (!inode)
11171da177e4SLinus Torvalds 		return -ENOMEM;
1118fc7bed8cSAl Viro 	sb->s_root = root = d_alloc_root(inode);
11191da177e4SLinus Torvalds 	if (!root) {
11201da177e4SLinus Torvalds 		iput(inode);
11211da177e4SLinus Torvalds 		return -ENOMEM;
11221da177e4SLinus Torvalds 	}
1123ac6feceeSTrond Myklebust 	if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
11241da177e4SLinus Torvalds 		return -ENOMEM;
1125efc46bf2SStanislav Kinsbursky 	dprintk("RPC:	sending pipefs MOUNT notification for net %p%s\n", net,
1126efc46bf2SStanislav Kinsbursky 								NET_NAME(net));
11272d00131aSStanislav Kinsbursky 	err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
11282d00131aSStanislav Kinsbursky 					   RPC_PIPEFS_MOUNT,
11292d00131aSStanislav Kinsbursky 					   sb);
11302d00131aSStanislav Kinsbursky 	if (err)
11312d00131aSStanislav Kinsbursky 		goto err_depopulate;
1132021c68deSStanislav Kinsbursky 	sb->s_fs_info = get_net(net);
113390c4e829SStanislav Kinsbursky 	sn->pipefs_sb = sb;
1134fc7bed8cSAl Viro 	return 0;
11352d00131aSStanislav Kinsbursky 
11362d00131aSStanislav Kinsbursky err_depopulate:
11372d00131aSStanislav Kinsbursky 	blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
11382d00131aSStanislav Kinsbursky 					   RPC_PIPEFS_UMOUNT,
11392d00131aSStanislav Kinsbursky 					   sb);
11402d00131aSStanislav Kinsbursky 	__rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
11412d00131aSStanislav Kinsbursky 	return err;
11421da177e4SLinus Torvalds }
11431da177e4SLinus Torvalds 
1144fc14f2feSAl Viro static struct dentry *
1145fc14f2feSAl Viro rpc_mount(struct file_system_type *fs_type,
1146fc14f2feSAl Viro 		int flags, const char *dev_name, void *data)
11471da177e4SLinus Torvalds {
114838b0da75SStanislav Kinsbursky 	return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
1151021c68deSStanislav Kinsbursky void rpc_kill_sb(struct super_block *sb)
1152021c68deSStanislav Kinsbursky {
1153021c68deSStanislav Kinsbursky 	struct net *net = sb->s_fs_info;
115490c4e829SStanislav Kinsbursky 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1155021c68deSStanislav Kinsbursky 
1156c21a588fSStanislav Kinsbursky 	mutex_lock(&sn->pipefs_sb_lock);
115790c4e829SStanislav Kinsbursky 	sn->pipefs_sb = NULL;
1158c21a588fSStanislav Kinsbursky 	mutex_unlock(&sn->pipefs_sb_lock);
1159021c68deSStanislav Kinsbursky 	put_net(net);
1160efc46bf2SStanislav Kinsbursky 	dprintk("RPC:	sending pipefs UMOUNT notification for net %p%s\n", net,
1161efc46bf2SStanislav Kinsbursky 								NET_NAME(net));
11622d00131aSStanislav Kinsbursky 	blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
11632d00131aSStanislav Kinsbursky 					   RPC_PIPEFS_UMOUNT,
11642d00131aSStanislav Kinsbursky 					   sb);
1165021c68deSStanislav Kinsbursky 	kill_litter_super(sb);
1166021c68deSStanislav Kinsbursky }
1167021c68deSStanislav Kinsbursky 
11681da177e4SLinus Torvalds static struct file_system_type rpc_pipe_fs_type = {
11691da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
11701da177e4SLinus Torvalds 	.name		= "rpc_pipefs",
1171fc14f2feSAl Viro 	.mount		= rpc_mount,
1172021c68deSStanislav Kinsbursky 	.kill_sb	= rpc_kill_sb,
11731da177e4SLinus Torvalds };
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds static void
117651cc5068SAlexey Dobriyan init_once(void *foo)
11771da177e4SLinus Torvalds {
11781da177e4SLinus Torvalds 	struct rpc_inode *rpci = (struct rpc_inode *) foo;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	inode_init_once(&rpci->vfs_inode);
11811da177e4SLinus Torvalds 	rpci->private = NULL;
1182ba9e0975SStanislav Kinsbursky 	rpci->pipe = NULL;
11831da177e4SLinus Torvalds }
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds int register_rpc_pipefs(void)
11861da177e4SLinus Torvalds {
11875bd5f581SAkinobu Mita 	int err;
11885bd5f581SAkinobu Mita 
11891da177e4SLinus Torvalds 	rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
11901da177e4SLinus Torvalds 				sizeof(struct rpc_inode),
1191fffb60f9SPaul Jackson 				0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1192fffb60f9SPaul Jackson 						SLAB_MEM_SPREAD),
119320c2df83SPaul Mundt 				init_once);
11941da177e4SLinus Torvalds 	if (!rpc_inode_cachep)
11951da177e4SLinus Torvalds 		return -ENOMEM;
119680df9d20SStanislav Kinsbursky 	err = rpc_clients_notifier_register();
119780df9d20SStanislav Kinsbursky 	if (err)
119880df9d20SStanislav Kinsbursky 		goto err_notifier;
11995bd5f581SAkinobu Mita 	err = register_filesystem(&rpc_pipe_fs_type);
120080df9d20SStanislav Kinsbursky 	if (err)
120180df9d20SStanislav Kinsbursky 		goto err_register;
120280df9d20SStanislav Kinsbursky 	return 0;
120380df9d20SStanislav Kinsbursky 
120480df9d20SStanislav Kinsbursky err_register:
120580df9d20SStanislav Kinsbursky 	rpc_clients_notifier_unregister();
120680df9d20SStanislav Kinsbursky err_notifier:
12075bd5f581SAkinobu Mita 	kmem_cache_destroy(rpc_inode_cachep);
12085bd5f581SAkinobu Mita 	return err;
12095bd5f581SAkinobu Mita }
12105bd5f581SAkinobu Mita 
12111da177e4SLinus Torvalds void unregister_rpc_pipefs(void)
12121da177e4SLinus Torvalds {
121380df9d20SStanislav Kinsbursky 	rpc_clients_notifier_unregister();
12141a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(rpc_inode_cachep);
12151da177e4SLinus Torvalds 	unregister_filesystem(&rpc_pipe_fs_type);
12161da177e4SLinus Torvalds }
1217dcbf8c30SMichal Schmidt 
1218dcbf8c30SMichal Schmidt /* Make 'mount -t rpc_pipefs ...' autoload this module. */
1219dcbf8c30SMichal Schmidt MODULE_ALIAS("rpc_pipefs");
1220