xref: /openbmc/linux/net/sunrpc/rpc_pipe.c (revision ba9e0975)
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 	if (pipe->ops == NULL) {
90ba9e0975SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
919842ef35STrond Myklebust 		return;
929842ef35STrond Myklebust 	}
93ba9e0975SStanislav Kinsbursky 	destroy_msg = pipe->ops->destroy_msg;
94ba9e0975SStanislav Kinsbursky 	if (pipe->nreaders == 0) {
95ba9e0975SStanislav Kinsbursky 		list_splice_init(&pipe->pipe, &free_list);
96ba9e0975SStanislav Kinsbursky 		pipe->pipelen = 0;
979842ef35STrond Myklebust 	}
98ba9e0975SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
99ba9e0975SStanislav Kinsbursky 	rpc_purge_list(pipe, &free_list, destroy_msg, -ETIMEDOUT);
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
102c1225158SPeng Tao ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
103c1225158SPeng Tao 				char __user *dst, size_t buflen)
104c1225158SPeng Tao {
105c1225158SPeng Tao 	char *data = (char *)msg->data + msg->copied;
106c1225158SPeng Tao 	size_t mlen = min(msg->len - msg->copied, buflen);
107c1225158SPeng Tao 	unsigned long left;
108c1225158SPeng Tao 
109c1225158SPeng Tao 	left = copy_to_user(dst, data, mlen);
110c1225158SPeng Tao 	if (left == mlen) {
111c1225158SPeng Tao 		msg->errno = -EFAULT;
112c1225158SPeng Tao 		return -EFAULT;
113c1225158SPeng Tao 	}
114c1225158SPeng Tao 
115c1225158SPeng Tao 	mlen -= left;
116c1225158SPeng Tao 	msg->copied += mlen;
117c1225158SPeng Tao 	msg->errno = 0;
118c1225158SPeng Tao 	return mlen;
119c1225158SPeng Tao }
120c1225158SPeng Tao EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
121c1225158SPeng Tao 
12293a44a75SJ. Bruce Fields /**
1231a5778aaSBen Hutchings  * rpc_queue_upcall - queue an upcall message to userspace
12493a44a75SJ. Bruce Fields  * @inode: inode of upcall pipe on which to queue given message
12593a44a75SJ. Bruce Fields  * @msg: message to queue
12693a44a75SJ. Bruce Fields  *
12793a44a75SJ. Bruce Fields  * Call with an @inode created by rpc_mkpipe() to queue an upcall.
12893a44a75SJ. Bruce Fields  * A userspace process may then later read the upcall by performing a
12993a44a75SJ. Bruce Fields  * read on an open file for this inode.  It is up to the caller to
13093a44a75SJ. Bruce Fields  * initialize the fields of @msg (other than @msg->list) appropriately.
13193a44a75SJ. Bruce Fields  */
1321da177e4SLinus Torvalds int
1331da177e4SLinus Torvalds rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct rpc_inode *rpci = RPC_I(inode);
1366070fe6fSTrond Myklebust 	int res = -EPIPE;
1371da177e4SLinus Torvalds 
138ba9e0975SStanislav Kinsbursky 	spin_lock(&rpci->pipe->lock);
139ba9e0975SStanislav Kinsbursky 	if (rpci->pipe->ops == NULL)
1406070fe6fSTrond Myklebust 		goto out;
141ba9e0975SStanislav Kinsbursky 	if (rpci->pipe->nreaders) {
142ba9e0975SStanislav Kinsbursky 		list_add_tail(&msg->list, &rpci->pipe->pipe);
143ba9e0975SStanislav Kinsbursky 		rpci->pipe->pipelen += msg->len;
1446070fe6fSTrond Myklebust 		res = 0;
145ba9e0975SStanislav Kinsbursky 	} else if (rpci->pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
146ba9e0975SStanislav Kinsbursky 		if (list_empty(&rpci->pipe->pipe))
14724c5d9d7STrond Myklebust 			queue_delayed_work(rpciod_workqueue,
148ba9e0975SStanislav Kinsbursky 					&rpci->pipe->queue_timeout,
1491da177e4SLinus Torvalds 					RPC_UPCALL_TIMEOUT);
150ba9e0975SStanislav Kinsbursky 		list_add_tail(&msg->list, &rpci->pipe->pipe);
151ba9e0975SStanislav Kinsbursky 		rpci->pipe->pipelen += msg->len;
1526070fe6fSTrond Myklebust 		res = 0;
1536070fe6fSTrond Myklebust 	}
1546070fe6fSTrond Myklebust out:
155ba9e0975SStanislav Kinsbursky 	spin_unlock(&rpci->pipe->lock);
156ba9e0975SStanislav Kinsbursky 	wake_up(&rpci->pipe->waitq);
1571da177e4SLinus Torvalds 	return res;
1581da177e4SLinus Torvalds }
159468039eeSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_queue_upcall);
1601da177e4SLinus Torvalds 
1616070fe6fSTrond Myklebust static inline void
1626070fe6fSTrond Myklebust rpc_inode_setowner(struct inode *inode, void *private)
1636070fe6fSTrond Myklebust {
1646070fe6fSTrond Myklebust 	RPC_I(inode)->private = private;
1656070fe6fSTrond Myklebust }
1666070fe6fSTrond Myklebust 
1671da177e4SLinus Torvalds static void
1681da177e4SLinus Torvalds rpc_close_pipes(struct inode *inode)
1691da177e4SLinus Torvalds {
170ba9e0975SStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
171b693ba4aSTrond Myklebust 	const struct rpc_pipe_ops *ops;
172e712804aS\"J. Bruce Fields\ 	int need_release;
1731da177e4SLinus Torvalds 
1741b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
175ba9e0975SStanislav Kinsbursky 	ops = pipe->ops;
1769842ef35STrond Myklebust 	if (ops != NULL) {
1779842ef35STrond Myklebust 		LIST_HEAD(free_list);
178ba9e0975SStanislav Kinsbursky 		spin_lock(&pipe->lock);
179ba9e0975SStanislav Kinsbursky 		need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
180ba9e0975SStanislav Kinsbursky 		pipe->nreaders = 0;
181ba9e0975SStanislav Kinsbursky 		list_splice_init(&pipe->in_upcall, &free_list);
182ba9e0975SStanislav Kinsbursky 		list_splice_init(&pipe->pipe, &free_list);
183ba9e0975SStanislav Kinsbursky 		pipe->pipelen = 0;
184ba9e0975SStanislav Kinsbursky 		pipe->ops = NULL;
185ba9e0975SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
186ba9e0975SStanislav Kinsbursky 		rpc_purge_list(pipe, &free_list, ops->destroy_msg, -EPIPE);
187ba9e0975SStanislav Kinsbursky 		pipe->nwriters = 0;
188e712804aS\"J. Bruce Fields\ 		if (need_release && ops->release_pipe)
1899842ef35STrond Myklebust 			ops->release_pipe(inode);
190ba9e0975SStanislav Kinsbursky 		cancel_delayed_work_sync(&pipe->queue_timeout);
1911da177e4SLinus Torvalds 	}
1926070fe6fSTrond Myklebust 	rpc_inode_setowner(inode, NULL);
1931b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static struct inode *
1971da177e4SLinus Torvalds rpc_alloc_inode(struct super_block *sb)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	struct rpc_inode *rpci;
200e94b1766SChristoph Lameter 	rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
2011da177e4SLinus Torvalds 	if (!rpci)
2021da177e4SLinus Torvalds 		return NULL;
2031da177e4SLinus Torvalds 	return &rpci->vfs_inode;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static void
207fa0d7e3dSNick Piggin rpc_i_callback(struct rcu_head *head)
208fa0d7e3dSNick Piggin {
209fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
210ba9e0975SStanislav Kinsbursky 	kfree(RPC_I(inode)->pipe);
211fa0d7e3dSNick Piggin 	kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
212fa0d7e3dSNick Piggin }
213fa0d7e3dSNick Piggin 
214fa0d7e3dSNick Piggin static void
2151da177e4SLinus Torvalds rpc_destroy_inode(struct inode *inode)
2161da177e4SLinus Torvalds {
217fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, rpc_i_callback);
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds static int
2211da177e4SLinus Torvalds rpc_pipe_open(struct inode *inode, struct file *filp)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	struct rpc_inode *rpci = RPC_I(inode);
224c3810608S\"J. Bruce Fields\ 	int first_open;
2251da177e4SLinus Torvalds 	int res = -ENXIO;
2261da177e4SLinus Torvalds 
2271b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
228ba9e0975SStanislav Kinsbursky 	if (rpci->pipe->ops == NULL)
229c3810608S\"J. Bruce Fields\ 		goto out;
230ba9e0975SStanislav Kinsbursky 	first_open = rpci->pipe->nreaders == 0 && rpci->pipe->nwriters == 0;
231ba9e0975SStanislav Kinsbursky 	if (first_open && rpci->pipe->ops->open_pipe) {
232ba9e0975SStanislav Kinsbursky 		res = rpci->pipe->ops->open_pipe(inode);
233c3810608S\"J. Bruce Fields\ 		if (res)
234c3810608S\"J. Bruce Fields\ 			goto out;
235c3810608S\"J. Bruce Fields\ 	}
2361da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_READ)
237ba9e0975SStanislav Kinsbursky 		rpci->pipe->nreaders++;
2381da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_WRITE)
239ba9e0975SStanislav Kinsbursky 		rpci->pipe->nwriters++;
2401da177e4SLinus Torvalds 	res = 0;
241c3810608S\"J. Bruce Fields\ out:
2421b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
2431da177e4SLinus Torvalds 	return res;
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds static int
2471da177e4SLinus Torvalds rpc_pipe_release(struct inode *inode, struct file *filp)
2481da177e4SLinus Torvalds {
249ba9e0975SStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
2501da177e4SLinus Torvalds 	struct rpc_pipe_msg *msg;
251e712804aS\"J. Bruce Fields\ 	int last_close;
2521da177e4SLinus Torvalds 
2531b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
254ba9e0975SStanislav Kinsbursky 	if (pipe->ops == NULL)
2551da177e4SLinus Torvalds 		goto out;
256655b5bb4SJoe Perches 	msg = filp->private_data;
2571da177e4SLinus Torvalds 	if (msg != NULL) {
258ba9e0975SStanislav Kinsbursky 		spin_lock(&pipe->lock);
25948e49187STrond Myklebust 		msg->errno = -EAGAIN;
2605a67657aSTrond Myklebust 		list_del_init(&msg->list);
261ba9e0975SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
262ba9e0975SStanislav Kinsbursky 		pipe->ops->destroy_msg(msg);
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_WRITE)
265ba9e0975SStanislav Kinsbursky 		pipe->nwriters --;
2669842ef35STrond Myklebust 	if (filp->f_mode & FMODE_READ) {
267ba9e0975SStanislav Kinsbursky 		pipe->nreaders --;
268ba9e0975SStanislav Kinsbursky 		if (pipe->nreaders == 0) {
2699842ef35STrond Myklebust 			LIST_HEAD(free_list);
270ba9e0975SStanislav Kinsbursky 			spin_lock(&pipe->lock);
271ba9e0975SStanislav Kinsbursky 			list_splice_init(&pipe->pipe, &free_list);
272ba9e0975SStanislav Kinsbursky 			pipe->pipelen = 0;
273ba9e0975SStanislav Kinsbursky 			spin_unlock(&pipe->lock);
274ba9e0975SStanislav Kinsbursky 			rpc_purge_list(pipe, &free_list,
275ba9e0975SStanislav Kinsbursky 					pipe->ops->destroy_msg, -EAGAIN);
2769842ef35STrond Myklebust 		}
2779842ef35STrond Myklebust 	}
278ba9e0975SStanislav Kinsbursky 	last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
279ba9e0975SStanislav Kinsbursky 	if (last_close && pipe->ops->release_pipe)
280ba9e0975SStanislav Kinsbursky 		pipe->ops->release_pipe(inode);
2811da177e4SLinus Torvalds out:
2821b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
2831da177e4SLinus Torvalds 	return 0;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds static ssize_t
2871da177e4SLinus Torvalds rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
2881da177e4SLinus Torvalds {
289303b46bbSJosef Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
2901da177e4SLinus Torvalds 	struct rpc_inode *rpci = RPC_I(inode);
2911da177e4SLinus Torvalds 	struct rpc_pipe_msg *msg;
2921da177e4SLinus Torvalds 	int res = 0;
2931da177e4SLinus Torvalds 
2941b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
295ba9e0975SStanislav Kinsbursky 	if (rpci->pipe->ops == NULL) {
2961da177e4SLinus Torvalds 		res = -EPIPE;
2971da177e4SLinus Torvalds 		goto out_unlock;
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 	msg = filp->private_data;
3001da177e4SLinus Torvalds 	if (msg == NULL) {
301ba9e0975SStanislav Kinsbursky 		spin_lock(&rpci->pipe->lock);
302ba9e0975SStanislav Kinsbursky 		if (!list_empty(&rpci->pipe->pipe)) {
303ba9e0975SStanislav Kinsbursky 			msg = list_entry(rpci->pipe->pipe.next,
3041da177e4SLinus Torvalds 					struct rpc_pipe_msg,
3051da177e4SLinus Torvalds 					list);
306ba9e0975SStanislav Kinsbursky 			list_move(&msg->list, &rpci->pipe->in_upcall);
307ba9e0975SStanislav Kinsbursky 			rpci->pipe->pipelen -= msg->len;
3081da177e4SLinus Torvalds 			filp->private_data = msg;
3091da177e4SLinus Torvalds 			msg->copied = 0;
3101da177e4SLinus Torvalds 		}
311ba9e0975SStanislav Kinsbursky 		spin_unlock(&rpci->pipe->lock);
3121da177e4SLinus Torvalds 		if (msg == NULL)
3131da177e4SLinus Torvalds 			goto out_unlock;
3141da177e4SLinus Torvalds 	}
3151da177e4SLinus Torvalds 	/* NOTE: it is up to the callback to update msg->copied */
316ba9e0975SStanislav Kinsbursky 	res = rpci->pipe->ops->upcall(filp, msg, buf, len);
3171da177e4SLinus Torvalds 	if (res < 0 || msg->len == msg->copied) {
3181da177e4SLinus Torvalds 		filp->private_data = NULL;
319ba9e0975SStanislav Kinsbursky 		spin_lock(&rpci->pipe->lock);
3205a67657aSTrond Myklebust 		list_del_init(&msg->list);
321ba9e0975SStanislav Kinsbursky 		spin_unlock(&rpci->pipe->lock);
322ba9e0975SStanislav Kinsbursky 		rpci->pipe->ops->destroy_msg(msg);
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds out_unlock:
3251b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
3261da177e4SLinus Torvalds 	return res;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds static ssize_t
3301da177e4SLinus Torvalds rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
3311da177e4SLinus Torvalds {
332303b46bbSJosef Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
3331da177e4SLinus Torvalds 	struct rpc_inode *rpci = RPC_I(inode);
3341da177e4SLinus Torvalds 	int res;
3351da177e4SLinus Torvalds 
3361b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
3371da177e4SLinus Torvalds 	res = -EPIPE;
338ba9e0975SStanislav Kinsbursky 	if (rpci->pipe->ops != NULL)
339ba9e0975SStanislav Kinsbursky 		res = rpci->pipe->ops->downcall(filp, buf, len);
3401b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
3411da177e4SLinus Torvalds 	return res;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds static unsigned int
3451da177e4SLinus Torvalds rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	struct rpc_inode *rpci;
3481da177e4SLinus Torvalds 	unsigned int mask = 0;
3491da177e4SLinus Torvalds 
350303b46bbSJosef Sipek 	rpci = RPC_I(filp->f_path.dentry->d_inode);
351ba9e0975SStanislav Kinsbursky 	poll_wait(filp, &rpci->pipe->waitq, wait);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	mask = POLLOUT | POLLWRNORM;
354ba9e0975SStanislav Kinsbursky 	if (rpci->pipe->ops == NULL)
3551da177e4SLinus Torvalds 		mask |= POLLERR | POLLHUP;
356ba9e0975SStanislav Kinsbursky 	if (filp->private_data || !list_empty(&rpci->pipe->pipe))
3571da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
3581da177e4SLinus Torvalds 	return mask;
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
361a6f8dbc6SArnd Bergmann static long
362a6f8dbc6SArnd Bergmann rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3631da177e4SLinus Torvalds {
364a6f8dbc6SArnd Bergmann 	struct inode *inode = filp->f_path.dentry->d_inode;
365a6f8dbc6SArnd Bergmann 	struct rpc_inode *rpci = RPC_I(inode);
3661da177e4SLinus Torvalds 	int len;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	switch (cmd) {
3691da177e4SLinus Torvalds 	case FIONREAD:
370ba9e0975SStanislav Kinsbursky 		spin_lock(&rpci->pipe->lock);
371ba9e0975SStanislav Kinsbursky 		if (rpci->pipe->ops == NULL) {
372ba9e0975SStanislav Kinsbursky 			spin_unlock(&rpci->pipe->lock);
3731da177e4SLinus Torvalds 			return -EPIPE;
374a6f8dbc6SArnd Bergmann 		}
375ba9e0975SStanislav Kinsbursky 		len = rpci->pipe->pipelen;
3761da177e4SLinus Torvalds 		if (filp->private_data) {
3771da177e4SLinus Torvalds 			struct rpc_pipe_msg *msg;
378655b5bb4SJoe Perches 			msg = filp->private_data;
3791da177e4SLinus Torvalds 			len += msg->len - msg->copied;
3801da177e4SLinus Torvalds 		}
381ba9e0975SStanislav Kinsbursky 		spin_unlock(&rpci->pipe->lock);
3821da177e4SLinus Torvalds 		return put_user(len, (int __user *)arg);
3831da177e4SLinus Torvalds 	default:
3841da177e4SLinus Torvalds 		return -EINVAL;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
388da7071d7SArjan van de Ven static const struct file_operations rpc_pipe_fops = {
3891da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
3901da177e4SLinus Torvalds 	.llseek		= no_llseek,
3911da177e4SLinus Torvalds 	.read		= rpc_pipe_read,
3921da177e4SLinus Torvalds 	.write		= rpc_pipe_write,
3931da177e4SLinus Torvalds 	.poll		= rpc_pipe_poll,
394674b604cSFrederic Weisbecker 	.unlocked_ioctl	= rpc_pipe_ioctl,
3951da177e4SLinus Torvalds 	.open		= rpc_pipe_open,
3961da177e4SLinus Torvalds 	.release	= rpc_pipe_release,
3971da177e4SLinus Torvalds };
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds static int
4001da177e4SLinus Torvalds rpc_show_info(struct seq_file *m, void *v)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	struct rpc_clnt *clnt = m->private;
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	seq_printf(m, "RPC server: %s\n", clnt->cl_server);
4051da177e4SLinus Torvalds 	seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
4061da177e4SLinus Torvalds 			clnt->cl_prog, clnt->cl_vers);
407e7f78657SChuck Lever 	seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
408e7f78657SChuck Lever 	seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
409bf19aaceSJ. Bruce Fields 	seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
4101da177e4SLinus Torvalds 	return 0;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds static int
4141da177e4SLinus Torvalds rpc_info_open(struct inode *inode, struct file *file)
4151da177e4SLinus Torvalds {
416006abe88STrond Myklebust 	struct rpc_clnt *clnt = NULL;
4171da177e4SLinus Torvalds 	int ret = single_open(file, rpc_show_info, NULL);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	if (!ret) {
4201da177e4SLinus Torvalds 		struct seq_file *m = file->private_data;
421006abe88STrond Myklebust 
422006abe88STrond Myklebust 		spin_lock(&file->f_path.dentry->d_lock);
423006abe88STrond Myklebust 		if (!d_unhashed(file->f_path.dentry))
4241da177e4SLinus Torvalds 			clnt = RPC_I(inode)->private;
425006abe88STrond Myklebust 		if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
426006abe88STrond Myklebust 			spin_unlock(&file->f_path.dentry->d_lock);
4271da177e4SLinus Torvalds 			m->private = clnt;
4281da177e4SLinus Torvalds 		} else {
429006abe88STrond Myklebust 			spin_unlock(&file->f_path.dentry->d_lock);
4301da177e4SLinus Torvalds 			single_release(inode, file);
4311da177e4SLinus Torvalds 			ret = -EINVAL;
4321da177e4SLinus Torvalds 		}
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 	return ret;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds static int
4381da177e4SLinus Torvalds rpc_info_release(struct inode *inode, struct file *file)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	struct seq_file *m = file->private_data;
4411da177e4SLinus Torvalds 	struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	if (clnt)
4441da177e4SLinus Torvalds 		rpc_release_client(clnt);
4451da177e4SLinus Torvalds 	return single_release(inode, file);
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
448da7071d7SArjan van de Ven static const struct file_operations rpc_info_operations = {
4491da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
4501da177e4SLinus Torvalds 	.open		= rpc_info_open,
4511da177e4SLinus Torvalds 	.read		= seq_read,
4521da177e4SLinus Torvalds 	.llseek		= seq_lseek,
4531da177e4SLinus Torvalds 	.release	= rpc_info_release,
4541da177e4SLinus Torvalds };
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /*
4581da177e4SLinus Torvalds  * Description of fs contents.
4591da177e4SLinus Torvalds  */
4601da177e4SLinus Torvalds struct rpc_filelist {
461ac6feceeSTrond Myklebust 	const char *name;
46299ac48f5SArjan van de Ven 	const struct file_operations *i_fop;
4637364af6aSTrond Myklebust 	umode_t mode;
4641da177e4SLinus Torvalds };
4651da177e4SLinus Torvalds 
46654281548STrond Myklebust struct vfsmount *rpc_get_mount(void)
4671da177e4SLinus Torvalds {
46854281548STrond Myklebust 	int err;
46954281548STrond Myklebust 
470fc14f2feSAl Viro 	err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
47154281548STrond Myklebust 	if (err != 0)
47254281548STrond Myklebust 		return ERR_PTR(err);
473fc14f2feSAl Viro 	return rpc_mnt;
4741da177e4SLinus Torvalds }
475e571cbf1STrond Myklebust EXPORT_SYMBOL_GPL(rpc_get_mount);
4761da177e4SLinus Torvalds 
47754281548STrond Myklebust void rpc_put_mount(void)
4781da177e4SLinus Torvalds {
479fc14f2feSAl Viro 	simple_release_fs(&rpc_mnt, &rpc_mount_count);
4801da177e4SLinus Torvalds }
481e571cbf1STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_mount);
4821da177e4SLinus Torvalds 
483fe15ce44SNick Piggin static int rpc_delete_dentry(const struct dentry *dentry)
48462e1761cSTrond Myklebust {
48562e1761cSTrond Myklebust 	return 1;
48662e1761cSTrond Myklebust }
48762e1761cSTrond Myklebust 
4883ba13d17SAl Viro static const struct dentry_operations rpc_dentry_operations = {
48962e1761cSTrond Myklebust 	.d_delete = rpc_delete_dentry,
49062e1761cSTrond Myklebust };
49162e1761cSTrond Myklebust 
4921da177e4SLinus Torvalds static struct inode *
4937364af6aSTrond Myklebust rpc_get_inode(struct super_block *sb, umode_t mode)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct inode *inode = new_inode(sb);
4961da177e4SLinus Torvalds 	if (!inode)
4971da177e4SLinus Torvalds 		return NULL;
49885fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
4991da177e4SLinus Torvalds 	inode->i_mode = mode;
5001da177e4SLinus Torvalds 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
5011da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
5021da177e4SLinus Torvalds 	case S_IFDIR:
5031da177e4SLinus Torvalds 		inode->i_fop = &simple_dir_operations;
5041da177e4SLinus Torvalds 		inode->i_op = &simple_dir_inode_operations;
505d8c76e6fSDave Hansen 		inc_nlink(inode);
5061da177e4SLinus Torvalds 	default:
5071da177e4SLinus Torvalds 		break;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 	return inode;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5127589806eSTrond Myklebust static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
5137589806eSTrond Myklebust 			       umode_t mode,
5147589806eSTrond Myklebust 			       const struct file_operations *i_fop,
5157589806eSTrond Myklebust 			       void *private)
5167589806eSTrond Myklebust {
5177589806eSTrond Myklebust 	struct inode *inode;
5187589806eSTrond Myklebust 
519beb0f0a9STrond Myklebust 	d_drop(dentry);
5207589806eSTrond Myklebust 	inode = rpc_get_inode(dir->i_sb, mode);
5217589806eSTrond Myklebust 	if (!inode)
5227589806eSTrond Myklebust 		goto out_err;
5237589806eSTrond Myklebust 	inode->i_ino = iunique(dir->i_sb, 100);
5247589806eSTrond Myklebust 	if (i_fop)
5257589806eSTrond Myklebust 		inode->i_fop = i_fop;
5267589806eSTrond Myklebust 	if (private)
5277589806eSTrond Myklebust 		rpc_inode_setowner(inode, private);
5287589806eSTrond Myklebust 	d_add(dentry, inode);
5297589806eSTrond Myklebust 	return 0;
5307589806eSTrond Myklebust out_err:
5317589806eSTrond Myklebust 	printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
5327589806eSTrond Myklebust 			__FILE__, __func__, dentry->d_name.name);
5337589806eSTrond Myklebust 	dput(dentry);
5347589806eSTrond Myklebust 	return -ENOMEM;
5357589806eSTrond Myklebust }
5367589806eSTrond Myklebust 
537ac6feceeSTrond Myklebust static int __rpc_create(struct inode *dir, struct dentry *dentry,
538ac6feceeSTrond Myklebust 			umode_t mode,
539ac6feceeSTrond Myklebust 			const struct file_operations *i_fop,
540ac6feceeSTrond Myklebust 			void *private)
541ac6feceeSTrond Myklebust {
542ac6feceeSTrond Myklebust 	int err;
543ac6feceeSTrond Myklebust 
544ac6feceeSTrond Myklebust 	err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
545ac6feceeSTrond Myklebust 	if (err)
546ac6feceeSTrond Myklebust 		return err;
547ac6feceeSTrond Myklebust 	fsnotify_create(dir, dentry);
548ac6feceeSTrond Myklebust 	return 0;
549ac6feceeSTrond Myklebust }
550ac6feceeSTrond Myklebust 
5517589806eSTrond Myklebust static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
5527589806eSTrond Myklebust 		       umode_t mode,
5537589806eSTrond Myklebust 		       const struct file_operations *i_fop,
5547589806eSTrond Myklebust 		       void *private)
5557589806eSTrond Myklebust {
5567589806eSTrond Myklebust 	int err;
5577589806eSTrond Myklebust 
5587589806eSTrond Myklebust 	err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
5597589806eSTrond Myklebust 	if (err)
5607589806eSTrond Myklebust 		return err;
5617589806eSTrond Myklebust 	inc_nlink(dir);
5627589806eSTrond Myklebust 	fsnotify_mkdir(dir, dentry);
5637589806eSTrond Myklebust 	return 0;
5647589806eSTrond Myklebust }
5657589806eSTrond Myklebust 
566ba9e0975SStanislav Kinsbursky static void
567ba9e0975SStanislav Kinsbursky init_pipe(struct rpc_pipe *pipe)
568ba9e0975SStanislav Kinsbursky {
569ba9e0975SStanislav Kinsbursky 	pipe->nreaders = 0;
570ba9e0975SStanislav Kinsbursky 	pipe->nwriters = 0;
571ba9e0975SStanislav Kinsbursky 	INIT_LIST_HEAD(&pipe->in_upcall);
572ba9e0975SStanislav Kinsbursky 	INIT_LIST_HEAD(&pipe->in_downcall);
573ba9e0975SStanislav Kinsbursky 	INIT_LIST_HEAD(&pipe->pipe);
574ba9e0975SStanislav Kinsbursky 	pipe->pipelen = 0;
575ba9e0975SStanislav Kinsbursky 	init_waitqueue_head(&pipe->waitq);
576ba9e0975SStanislav Kinsbursky 	INIT_DELAYED_WORK(&pipe->queue_timeout,
577ba9e0975SStanislav Kinsbursky 			    rpc_timeout_upcall_queue);
578ba9e0975SStanislav Kinsbursky 	pipe->ops = NULL;
579ba9e0975SStanislav Kinsbursky 	spin_lock_init(&pipe->lock);
580ba9e0975SStanislav Kinsbursky 
581ba9e0975SStanislav Kinsbursky }
582ba9e0975SStanislav Kinsbursky 
5837589806eSTrond Myklebust static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
5847589806eSTrond Myklebust 			umode_t mode,
5857589806eSTrond Myklebust 			const struct file_operations *i_fop,
5867589806eSTrond Myklebust 			void *private,
5877589806eSTrond Myklebust 			const struct rpc_pipe_ops *ops,
5887589806eSTrond Myklebust 			int flags)
5897589806eSTrond Myklebust {
590ba9e0975SStanislav Kinsbursky 	struct rpc_pipe *pipe;
5917589806eSTrond Myklebust 	struct rpc_inode *rpci;
5927589806eSTrond Myklebust 	int err;
5937589806eSTrond Myklebust 
594ba9e0975SStanislav Kinsbursky 	pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
595ba9e0975SStanislav Kinsbursky 	if (!pipe)
596ba9e0975SStanislav Kinsbursky 		return -ENOMEM;
597ba9e0975SStanislav Kinsbursky 	init_pipe(pipe);
5987589806eSTrond Myklebust 	err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
599ba9e0975SStanislav Kinsbursky 	if (err) {
600ba9e0975SStanislav Kinsbursky 		kfree(pipe);
6017589806eSTrond Myklebust 		return err;
602ba9e0975SStanislav Kinsbursky 	}
6037589806eSTrond Myklebust 	rpci = RPC_I(dentry->d_inode);
6047589806eSTrond Myklebust 	rpci->private = private;
605ba9e0975SStanislav Kinsbursky 	rpci->pipe = pipe;
606ba9e0975SStanislav Kinsbursky 	rpci->pipe->flags = flags;
607ba9e0975SStanislav Kinsbursky 	rpci->pipe->ops = ops;
6087589806eSTrond Myklebust 	fsnotify_create(dir, dentry);
6097589806eSTrond Myklebust 	return 0;
6107589806eSTrond Myklebust }
6117589806eSTrond Myklebust 
612ac6feceeSTrond Myklebust static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
613ac6feceeSTrond Myklebust {
614ac6feceeSTrond Myklebust 	int ret;
615ac6feceeSTrond Myklebust 
616ac6feceeSTrond Myklebust 	dget(dentry);
617ac6feceeSTrond Myklebust 	ret = simple_rmdir(dir, dentry);
618ac6feceeSTrond Myklebust 	d_delete(dentry);
619ac6feceeSTrond Myklebust 	dput(dentry);
620ac6feceeSTrond Myklebust 	return ret;
621ac6feceeSTrond Myklebust }
622ac6feceeSTrond Myklebust 
623810d90bcSTrond Myklebust static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
624810d90bcSTrond Myklebust {
625810d90bcSTrond Myklebust 	int ret;
626810d90bcSTrond Myklebust 
627810d90bcSTrond Myklebust 	dget(dentry);
628810d90bcSTrond Myklebust 	ret = simple_unlink(dir, dentry);
629810d90bcSTrond Myklebust 	d_delete(dentry);
630810d90bcSTrond Myklebust 	dput(dentry);
631810d90bcSTrond Myklebust 	return ret;
632810d90bcSTrond Myklebust }
633810d90bcSTrond Myklebust 
634810d90bcSTrond Myklebust static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
635810d90bcSTrond Myklebust {
636810d90bcSTrond Myklebust 	struct inode *inode = dentry->d_inode;
637810d90bcSTrond Myklebust 
638810d90bcSTrond Myklebust 	rpc_close_pipes(inode);
639810d90bcSTrond Myklebust 	return __rpc_unlink(dir, dentry);
640810d90bcSTrond Myklebust }
641810d90bcSTrond Myklebust 
642cfeaa4a3STrond Myklebust static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
643cfeaa4a3STrond Myklebust 					  struct qstr *name)
644cfeaa4a3STrond Myklebust {
645cfeaa4a3STrond Myklebust 	struct dentry *dentry;
646cfeaa4a3STrond Myklebust 
6475bff0386SStanislav Kinsbursky 	dentry = d_lookup(parent, name);
6485bff0386SStanislav Kinsbursky 	if (!dentry) {
6495bff0386SStanislav Kinsbursky 		dentry = d_alloc(parent, name);
6505bff0386SStanislav Kinsbursky 		if (!dentry)
6515bff0386SStanislav Kinsbursky 			return ERR_PTR(-ENOMEM);
6525bff0386SStanislav Kinsbursky 	}
6535bff0386SStanislav Kinsbursky 	if (dentry->d_inode == NULL) {
6545bff0386SStanislav Kinsbursky 		d_set_d_op(dentry, &rpc_dentry_operations);
655f1f0abe1SDan Carpenter 		return dentry;
6565bff0386SStanislav Kinsbursky 	}
657cfeaa4a3STrond Myklebust 	dput(dentry);
658cfeaa4a3STrond Myklebust 	return ERR_PTR(-EEXIST);
659cfeaa4a3STrond Myklebust }
660cfeaa4a3STrond Myklebust 
6611da177e4SLinus Torvalds /*
6621da177e4SLinus Torvalds  * FIXME: This probably has races.
6631da177e4SLinus Torvalds  */
664ac6feceeSTrond Myklebust static void __rpc_depopulate(struct dentry *parent,
665ac6feceeSTrond Myklebust 			     const struct rpc_filelist *files,
666ac6feceeSTrond Myklebust 			     int start, int eof)
6671da177e4SLinus Torvalds {
6681da177e4SLinus Torvalds 	struct inode *dir = parent->d_inode;
669ac6feceeSTrond Myklebust 	struct dentry *dentry;
670ac6feceeSTrond Myklebust 	struct qstr name;
671ac6feceeSTrond Myklebust 	int i;
672ac6feceeSTrond Myklebust 
673ac6feceeSTrond Myklebust 	for (i = start; i < eof; i++) {
674ac6feceeSTrond Myklebust 		name.name = files[i].name;
675ac6feceeSTrond Myklebust 		name.len = strlen(files[i].name);
676ac6feceeSTrond Myklebust 		name.hash = full_name_hash(name.name, name.len);
677ac6feceeSTrond Myklebust 		dentry = d_lookup(parent, &name);
678ac6feceeSTrond Myklebust 
679ac6feceeSTrond Myklebust 		if (dentry == NULL)
680ac6feceeSTrond Myklebust 			continue;
681ac6feceeSTrond Myklebust 		if (dentry->d_inode == NULL)
682ac6feceeSTrond Myklebust 			goto next;
683ac6feceeSTrond Myklebust 		switch (dentry->d_inode->i_mode & S_IFMT) {
684ac6feceeSTrond Myklebust 			default:
685ac6feceeSTrond Myklebust 				BUG();
686ac6feceeSTrond Myklebust 			case S_IFREG:
687ac6feceeSTrond Myklebust 				__rpc_unlink(dir, dentry);
688ac6feceeSTrond Myklebust 				break;
689ac6feceeSTrond Myklebust 			case S_IFDIR:
690ac6feceeSTrond Myklebust 				__rpc_rmdir(dir, dentry);
691ac6feceeSTrond Myklebust 		}
692ac6feceeSTrond Myklebust next:
693ac6feceeSTrond Myklebust 		dput(dentry);
694ac6feceeSTrond Myklebust 	}
695ac6feceeSTrond Myklebust }
696ac6feceeSTrond Myklebust 
697ac6feceeSTrond Myklebust static void rpc_depopulate(struct dentry *parent,
698ac6feceeSTrond Myklebust 			   const struct rpc_filelist *files,
699ac6feceeSTrond Myklebust 			   int start, int eof)
700ac6feceeSTrond Myklebust {
701ac6feceeSTrond Myklebust 	struct inode *dir = parent->d_inode;
7021da177e4SLinus Torvalds 
703c6573c29SArjan van de Ven 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
704ac6feceeSTrond Myklebust 	__rpc_depopulate(parent, files, start, eof);
7051b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
708ac6feceeSTrond Myklebust static int rpc_populate(struct dentry *parent,
709ac6feceeSTrond Myklebust 			const struct rpc_filelist *files,
710ac6feceeSTrond Myklebust 			int start, int eof,
711ac6feceeSTrond Myklebust 			void *private)
7121da177e4SLinus Torvalds {
713ac6feceeSTrond Myklebust 	struct inode *dir = parent->d_inode;
7141da177e4SLinus Torvalds 	struct dentry *dentry;
715ac6feceeSTrond Myklebust 	int i, err;
7161da177e4SLinus Torvalds 
7171b1dcc1bSJes Sorensen 	mutex_lock(&dir->i_mutex);
7181da177e4SLinus Torvalds 	for (i = start; i < eof; i++) {
719ac6feceeSTrond Myklebust 		struct qstr q;
720ac6feceeSTrond Myklebust 
721ac6feceeSTrond Myklebust 		q.name = files[i].name;
722ac6feceeSTrond Myklebust 		q.len = strlen(files[i].name);
723ac6feceeSTrond Myklebust 		q.hash = full_name_hash(q.name, q.len);
724ac6feceeSTrond Myklebust 		dentry = __rpc_lookup_create_exclusive(parent, &q);
725ac6feceeSTrond Myklebust 		err = PTR_ERR(dentry);
726ac6feceeSTrond Myklebust 		if (IS_ERR(dentry))
7271da177e4SLinus Torvalds 			goto out_bad;
728ac6feceeSTrond Myklebust 		switch (files[i].mode & S_IFMT) {
729ac6feceeSTrond Myklebust 			default:
730ac6feceeSTrond Myklebust 				BUG();
731ac6feceeSTrond Myklebust 			case S_IFREG:
732ac6feceeSTrond Myklebust 				err = __rpc_create(dir, dentry,
733ac6feceeSTrond Myklebust 						files[i].mode,
734ac6feceeSTrond Myklebust 						files[i].i_fop,
735ac6feceeSTrond Myklebust 						private);
736ac6feceeSTrond Myklebust 				break;
737ac6feceeSTrond Myklebust 			case S_IFDIR:
738ac6feceeSTrond Myklebust 				err = __rpc_mkdir(dir, dentry,
739ac6feceeSTrond Myklebust 						files[i].mode,
740ac6feceeSTrond Myklebust 						NULL,
741ac6feceeSTrond Myklebust 						private);
7421da177e4SLinus Torvalds 		}
743ac6feceeSTrond Myklebust 		if (err != 0)
744ac6feceeSTrond Myklebust 			goto out_bad;
7451da177e4SLinus Torvalds 	}
7461b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
7471da177e4SLinus Torvalds 	return 0;
7481da177e4SLinus Torvalds out_bad:
749ac6feceeSTrond Myklebust 	__rpc_depopulate(parent, files, start, eof);
7501b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
7511da177e4SLinus Torvalds 	printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
7520dc47877SHarvey Harrison 			__FILE__, __func__, parent->d_name.name);
753ac6feceeSTrond Myklebust 	return err;
754f134585aSTrond Myklebust }
755f134585aSTrond Myklebust 
756e57aed77STrond Myklebust static struct dentry *rpc_mkdir_populate(struct dentry *parent,
757e57aed77STrond Myklebust 		struct qstr *name, umode_t mode, void *private,
758e57aed77STrond Myklebust 		int (*populate)(struct dentry *, void *), void *args_populate)
759f134585aSTrond Myklebust {
760f134585aSTrond Myklebust 	struct dentry *dentry;
7617d59d1e8STrond Myklebust 	struct inode *dir = parent->d_inode;
762f134585aSTrond Myklebust 	int error;
763f134585aSTrond Myklebust 
7647d59d1e8STrond Myklebust 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
7657d59d1e8STrond Myklebust 	dentry = __rpc_lookup_create_exclusive(parent, name);
766f134585aSTrond Myklebust 	if (IS_ERR(dentry))
7677d59d1e8STrond Myklebust 		goto out;
7687d59d1e8STrond Myklebust 	error = __rpc_mkdir(dir, dentry, mode, NULL, private);
7697589806eSTrond Myklebust 	if (error != 0)
7707589806eSTrond Myklebust 		goto out_err;
771e57aed77STrond Myklebust 	if (populate != NULL) {
772e57aed77STrond Myklebust 		error = populate(dentry, args_populate);
773f134585aSTrond Myklebust 		if (error)
774ac6feceeSTrond Myklebust 			goto err_rmdir;
775e57aed77STrond Myklebust 	}
776f134585aSTrond Myklebust out:
7771b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
7785c3e985aSTrond Myklebust 	return dentry;
779ac6feceeSTrond Myklebust err_rmdir:
780f134585aSTrond Myklebust 	__rpc_rmdir(dir, dentry);
7817589806eSTrond Myklebust out_err:
782f134585aSTrond Myklebust 	dentry = ERR_PTR(error);
783f134585aSTrond Myklebust 	goto out;
784f134585aSTrond Myklebust }
785f134585aSTrond Myklebust 
786e57aed77STrond Myklebust static int rpc_rmdir_depopulate(struct dentry *dentry,
787e57aed77STrond Myklebust 		void (*depopulate)(struct dentry *))
788f134585aSTrond Myklebust {
789dff02cc1STrond Myklebust 	struct dentry *parent;
790f134585aSTrond Myklebust 	struct inode *dir;
791f134585aSTrond Myklebust 	int error;
792f134585aSTrond Myklebust 
793dff02cc1STrond Myklebust 	parent = dget_parent(dentry);
794dff02cc1STrond Myklebust 	dir = parent->d_inode;
795c6573c29SArjan van de Ven 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
796e57aed77STrond Myklebust 	if (depopulate != NULL)
797e57aed77STrond Myklebust 		depopulate(dentry);
798f134585aSTrond Myklebust 	error = __rpc_rmdir(dir, dentry);
7991b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
800dff02cc1STrond Myklebust 	dput(parent);
801f134585aSTrond Myklebust 	return error;
802f134585aSTrond Myklebust }
8031da177e4SLinus Torvalds 
80493a44a75SJ. Bruce Fields /**
80593a44a75SJ. Bruce Fields  * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
80693a44a75SJ. Bruce Fields  * @parent: dentry of directory to create new "pipe" in
80793a44a75SJ. Bruce Fields  * @name: name of pipe
80893a44a75SJ. Bruce Fields  * @private: private data to associate with the pipe, for the caller's use
80993a44a75SJ. Bruce Fields  * @ops: operations defining the behavior of the pipe: upcall, downcall,
810c3810608S\"J. Bruce Fields\  *	release_pipe, open_pipe, and destroy_msg.
81165b6e42cSRandy Dunlap  * @flags: rpc_inode flags
81293a44a75SJ. Bruce Fields  *
81393a44a75SJ. Bruce Fields  * Data is made available for userspace to read by calls to
81493a44a75SJ. Bruce Fields  * rpc_queue_upcall().  The actual reads will result in calls to
81593a44a75SJ. Bruce Fields  * @ops->upcall, which will be called with the file pointer,
81693a44a75SJ. Bruce Fields  * message, and userspace buffer to copy to.
81793a44a75SJ. Bruce Fields  *
81893a44a75SJ. Bruce Fields  * Writes can come at any time, and do not necessarily have to be
81993a44a75SJ. Bruce Fields  * responses to upcalls.  They will result in calls to @msg->downcall.
82093a44a75SJ. Bruce Fields  *
82193a44a75SJ. Bruce Fields  * The @private argument passed here will be available to all these methods
82293a44a75SJ. Bruce Fields  * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
82393a44a75SJ. Bruce Fields  */
824b693ba4aSTrond Myklebust struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
825b693ba4aSTrond Myklebust 			  void *private, const struct rpc_pipe_ops *ops,
826b693ba4aSTrond Myklebust 			  int flags)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	struct dentry *dentry;
8297589806eSTrond Myklebust 	struct inode *dir = parent->d_inode;
8307364af6aSTrond Myklebust 	umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
831cfeaa4a3STrond Myklebust 	struct qstr q;
8327589806eSTrond Myklebust 	int err;
8337364af6aSTrond Myklebust 
8347364af6aSTrond Myklebust 	if (ops->upcall == NULL)
8357364af6aSTrond Myklebust 		umode &= ~S_IRUGO;
8367364af6aSTrond Myklebust 	if (ops->downcall == NULL)
8377364af6aSTrond Myklebust 		umode &= ~S_IWUGO;
8381da177e4SLinus Torvalds 
839cfeaa4a3STrond Myklebust 	q.name = name;
840cfeaa4a3STrond Myklebust 	q.len = strlen(name);
841cfeaa4a3STrond Myklebust 	q.hash = full_name_hash(q.name, q.len),
842cfeaa4a3STrond Myklebust 
843cfeaa4a3STrond Myklebust 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
8445bff0386SStanislav Kinsbursky 	dentry = __rpc_lookup_create_exclusive(parent, &q);
8451da177e4SLinus Torvalds 	if (IS_ERR(dentry))
846cfeaa4a3STrond Myklebust 		goto out;
8477589806eSTrond Myklebust 	err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
8487589806eSTrond Myklebust 			   private, ops, flags);
8497589806eSTrond Myklebust 	if (err)
8507589806eSTrond Myklebust 		goto out_err;
851f134585aSTrond Myklebust out:
8521b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
8535c3e985aSTrond Myklebust 	return dentry;
8547589806eSTrond Myklebust out_err:
8557589806eSTrond Myklebust 	dentry = ERR_PTR(err);
856158998b6STrond Myklebust 	printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
8570dc47877SHarvey Harrison 			__FILE__, __func__, parent->d_name.name, name,
8587589806eSTrond Myklebust 			err);
859f134585aSTrond Myklebust 	goto out;
8601da177e4SLinus Torvalds }
861468039eeSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_mkpipe);
8621da177e4SLinus Torvalds 
86393a44a75SJ. Bruce Fields /**
86493a44a75SJ. Bruce Fields  * rpc_unlink - remove a pipe
86593a44a75SJ. Bruce Fields  * @dentry: dentry for the pipe, as returned from rpc_mkpipe
86693a44a75SJ. Bruce Fields  *
86793a44a75SJ. Bruce Fields  * After this call, lookups will no longer find the pipe, and any
86893a44a75SJ. Bruce Fields  * attempts to read or write using preexisting opens of the pipe will
86993a44a75SJ. Bruce Fields  * return -EPIPE.
87093a44a75SJ. Bruce Fields  */
871f134585aSTrond Myklebust int
8725d67476fSTrond Myklebust rpc_unlink(struct dentry *dentry)
8731da177e4SLinus Torvalds {
8745d67476fSTrond Myklebust 	struct dentry *parent;
875f134585aSTrond Myklebust 	struct inode *dir;
8765d67476fSTrond Myklebust 	int error = 0;
8771da177e4SLinus Torvalds 
8785d67476fSTrond Myklebust 	parent = dget_parent(dentry);
8795d67476fSTrond Myklebust 	dir = parent->d_inode;
880c6573c29SArjan van de Ven 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
881810d90bcSTrond Myklebust 	error = __rpc_rmpipe(dir, dentry);
8821b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
8835d67476fSTrond Myklebust 	dput(parent);
884f134585aSTrond Myklebust 	return error;
8851da177e4SLinus Torvalds }
886468039eeSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_unlink);
8871da177e4SLinus Torvalds 
888e57aed77STrond Myklebust enum {
889e57aed77STrond Myklebust 	RPCAUTH_info,
890e57aed77STrond Myklebust 	RPCAUTH_EOF
891e57aed77STrond Myklebust };
892e57aed77STrond Myklebust 
893e57aed77STrond Myklebust static const struct rpc_filelist authfiles[] = {
894e57aed77STrond Myklebust 	[RPCAUTH_info] = {
895e57aed77STrond Myklebust 		.name = "info",
896e57aed77STrond Myklebust 		.i_fop = &rpc_info_operations,
897e57aed77STrond Myklebust 		.mode = S_IFREG | S_IRUSR,
898e57aed77STrond Myklebust 	},
899e57aed77STrond Myklebust };
900e57aed77STrond Myklebust 
901e57aed77STrond Myklebust static int rpc_clntdir_populate(struct dentry *dentry, void *private)
902e57aed77STrond Myklebust {
903e57aed77STrond Myklebust 	return rpc_populate(dentry,
904e57aed77STrond Myklebust 			    authfiles, RPCAUTH_info, RPCAUTH_EOF,
905e57aed77STrond Myklebust 			    private);
906e57aed77STrond Myklebust }
907e57aed77STrond Myklebust 
908e57aed77STrond Myklebust static void rpc_clntdir_depopulate(struct dentry *dentry)
909e57aed77STrond Myklebust {
910e57aed77STrond Myklebust 	rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
911e57aed77STrond Myklebust }
912e57aed77STrond Myklebust 
9137d59d1e8STrond Myklebust /**
9147d59d1e8STrond Myklebust  * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
9154111d4fdSRandy Dunlap  * @dentry: dentry from the rpc_pipefs root to the new directory
9164111d4fdSRandy Dunlap  * @name: &struct qstr for the name
9177d59d1e8STrond Myklebust  * @rpc_client: rpc client to associate with this directory
9187d59d1e8STrond Myklebust  *
9197d59d1e8STrond Myklebust  * This creates a directory at the given @path associated with
9207d59d1e8STrond Myklebust  * @rpc_clnt, which will contain a file named "info" with some basic
9217d59d1e8STrond Myklebust  * information about the client, together with any "pipes" that may
9227d59d1e8STrond Myklebust  * later be created using rpc_mkpipe().
9237d59d1e8STrond Myklebust  */
92423ac6581STrond Myklebust struct dentry *rpc_create_client_dir(struct dentry *dentry,
92523ac6581STrond Myklebust 				   struct qstr *name,
9267d59d1e8STrond Myklebust 				   struct rpc_clnt *rpc_client)
9277d59d1e8STrond Myklebust {
928e57aed77STrond Myklebust 	return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
929e57aed77STrond Myklebust 			rpc_clntdir_populate, rpc_client);
930e57aed77STrond Myklebust }
931e57aed77STrond Myklebust 
932e57aed77STrond Myklebust /**
933e57aed77STrond Myklebust  * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
934e57aed77STrond Myklebust  * @dentry: directory to remove
935e57aed77STrond Myklebust  */
936e57aed77STrond Myklebust int rpc_remove_client_dir(struct dentry *dentry)
937e57aed77STrond Myklebust {
938e57aed77STrond Myklebust 	return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
9397d59d1e8STrond Myklebust }
9407d59d1e8STrond Myklebust 
9418854e82dSTrond Myklebust static const struct rpc_filelist cache_pipefs_files[3] = {
9428854e82dSTrond Myklebust 	[0] = {
9438854e82dSTrond Myklebust 		.name = "channel",
9448854e82dSTrond Myklebust 		.i_fop = &cache_file_operations_pipefs,
94596c61cbdSTrond Myklebust 		.mode = S_IFREG|S_IRUSR|S_IWUSR,
9468854e82dSTrond Myklebust 	},
9478854e82dSTrond Myklebust 	[1] = {
9488854e82dSTrond Myklebust 		.name = "content",
9498854e82dSTrond Myklebust 		.i_fop = &content_file_operations_pipefs,
9508854e82dSTrond Myklebust 		.mode = S_IFREG|S_IRUSR,
9518854e82dSTrond Myklebust 	},
9528854e82dSTrond Myklebust 	[2] = {
9538854e82dSTrond Myklebust 		.name = "flush",
9548854e82dSTrond Myklebust 		.i_fop = &cache_flush_operations_pipefs,
9558854e82dSTrond Myklebust 		.mode = S_IFREG|S_IRUSR|S_IWUSR,
9568854e82dSTrond Myklebust 	},
9578854e82dSTrond Myklebust };
9588854e82dSTrond Myklebust 
9598854e82dSTrond Myklebust static int rpc_cachedir_populate(struct dentry *dentry, void *private)
9608854e82dSTrond Myklebust {
9618854e82dSTrond Myklebust 	return rpc_populate(dentry,
9628854e82dSTrond Myklebust 			    cache_pipefs_files, 0, 3,
9638854e82dSTrond Myklebust 			    private);
9648854e82dSTrond Myklebust }
9658854e82dSTrond Myklebust 
9668854e82dSTrond Myklebust static void rpc_cachedir_depopulate(struct dentry *dentry)
9678854e82dSTrond Myklebust {
9688854e82dSTrond Myklebust 	rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
9698854e82dSTrond Myklebust }
9708854e82dSTrond Myklebust 
9718854e82dSTrond Myklebust struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
97264f1426fSAl Viro 				    umode_t umode, struct cache_detail *cd)
9738854e82dSTrond Myklebust {
9748854e82dSTrond Myklebust 	return rpc_mkdir_populate(parent, name, umode, NULL,
9758854e82dSTrond Myklebust 			rpc_cachedir_populate, cd);
9768854e82dSTrond Myklebust }
9778854e82dSTrond Myklebust 
9788854e82dSTrond Myklebust void rpc_remove_cache_dir(struct dentry *dentry)
9798854e82dSTrond Myklebust {
9808854e82dSTrond Myklebust 	rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
9818854e82dSTrond Myklebust }
9828854e82dSTrond Myklebust 
9831da177e4SLinus Torvalds /*
9841da177e4SLinus Torvalds  * populate the filesystem
9851da177e4SLinus Torvalds  */
986b87221deSAlexey Dobriyan static const struct super_operations s_ops = {
9871da177e4SLinus Torvalds 	.alloc_inode	= rpc_alloc_inode,
9881da177e4SLinus Torvalds 	.destroy_inode	= rpc_destroy_inode,
9891da177e4SLinus Torvalds 	.statfs		= simple_statfs,
9901da177e4SLinus Torvalds };
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds #define RPCAUTH_GSSMAGIC 0x67596969
9931da177e4SLinus Torvalds 
994bb156749STrond Myklebust /*
995bb156749STrond Myklebust  * We have a single directory with 1 node in it.
996bb156749STrond Myklebust  */
997bb156749STrond Myklebust enum {
998bb156749STrond Myklebust 	RPCAUTH_lockd,
999bb156749STrond Myklebust 	RPCAUTH_mount,
1000bb156749STrond Myklebust 	RPCAUTH_nfs,
1001bb156749STrond Myklebust 	RPCAUTH_portmap,
1002bb156749STrond Myklebust 	RPCAUTH_statd,
1003bb156749STrond Myklebust 	RPCAUTH_nfsd4_cb,
1004e571cbf1STrond Myklebust 	RPCAUTH_cache,
1005bb156749STrond Myklebust 	RPCAUTH_RootEOF
1006bb156749STrond Myklebust };
1007bb156749STrond Myklebust 
1008bb156749STrond Myklebust static const struct rpc_filelist files[] = {
1009bb156749STrond Myklebust 	[RPCAUTH_lockd] = {
1010bb156749STrond Myklebust 		.name = "lockd",
1011bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1012bb156749STrond Myklebust 	},
1013bb156749STrond Myklebust 	[RPCAUTH_mount] = {
1014bb156749STrond Myklebust 		.name = "mount",
1015bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1016bb156749STrond Myklebust 	},
1017bb156749STrond Myklebust 	[RPCAUTH_nfs] = {
1018bb156749STrond Myklebust 		.name = "nfs",
1019bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1020bb156749STrond Myklebust 	},
1021bb156749STrond Myklebust 	[RPCAUTH_portmap] = {
1022bb156749STrond Myklebust 		.name = "portmap",
1023bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1024bb156749STrond Myklebust 	},
1025bb156749STrond Myklebust 	[RPCAUTH_statd] = {
1026bb156749STrond Myklebust 		.name = "statd",
1027bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1028bb156749STrond Myklebust 	},
1029bb156749STrond Myklebust 	[RPCAUTH_nfsd4_cb] = {
1030bb156749STrond Myklebust 		.name = "nfsd4_cb",
1031bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1032bb156749STrond Myklebust 	},
1033e571cbf1STrond Myklebust 	[RPCAUTH_cache] = {
1034e571cbf1STrond Myklebust 		.name = "cache",
1035e571cbf1STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
1036e571cbf1STrond Myklebust 	},
1037bb156749STrond Myklebust };
1038bb156749STrond Myklebust 
1039432eb1a5SStanislav Kinsbursky /*
1040432eb1a5SStanislav Kinsbursky  * This call can be used only in RPC pipefs mount notification hooks.
1041432eb1a5SStanislav Kinsbursky  */
1042432eb1a5SStanislav Kinsbursky struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1043432eb1a5SStanislav Kinsbursky 			       const unsigned char *dir_name)
1044432eb1a5SStanislav Kinsbursky {
1045432eb1a5SStanislav Kinsbursky 	struct qstr dir = {
1046432eb1a5SStanislav Kinsbursky 		.name = dir_name,
1047432eb1a5SStanislav Kinsbursky 		.len = strlen(dir_name),
1048432eb1a5SStanislav Kinsbursky 		.hash = full_name_hash(dir_name, strlen(dir_name)),
1049432eb1a5SStanislav Kinsbursky 	};
1050432eb1a5SStanislav Kinsbursky 
1051432eb1a5SStanislav Kinsbursky 	return d_lookup(sb->s_root, &dir);
1052432eb1a5SStanislav Kinsbursky }
1053432eb1a5SStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1054432eb1a5SStanislav Kinsbursky 
1055c21a588fSStanislav Kinsbursky void rpc_pipefs_init_net(struct net *net)
1056c21a588fSStanislav Kinsbursky {
1057c21a588fSStanislav Kinsbursky 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1058c21a588fSStanislav Kinsbursky 
1059c21a588fSStanislav Kinsbursky 	mutex_init(&sn->pipefs_sb_lock);
1060c21a588fSStanislav Kinsbursky }
1061c21a588fSStanislav Kinsbursky 
1062c21a588fSStanislav Kinsbursky /*
1063c21a588fSStanislav Kinsbursky  * This call will be used for per network namespace operations calls.
1064c21a588fSStanislav Kinsbursky  * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1065c21a588fSStanislav Kinsbursky  * found. This lock have to be released by rpc_put_sb_net() when all operations
1066c21a588fSStanislav Kinsbursky  * will be completed.
1067c21a588fSStanislav Kinsbursky  */
1068c21a588fSStanislav Kinsbursky struct super_block *rpc_get_sb_net(const struct net *net)
1069c21a588fSStanislav Kinsbursky {
1070c21a588fSStanislav Kinsbursky 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1071c21a588fSStanislav Kinsbursky 
1072c21a588fSStanislav Kinsbursky 	mutex_lock(&sn->pipefs_sb_lock);
1073c21a588fSStanislav Kinsbursky 	if (sn->pipefs_sb)
1074c21a588fSStanislav Kinsbursky 		return sn->pipefs_sb;
1075c21a588fSStanislav Kinsbursky 	mutex_unlock(&sn->pipefs_sb_lock);
1076c21a588fSStanislav Kinsbursky 	return NULL;
1077c21a588fSStanislav Kinsbursky }
1078c21a588fSStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1079c21a588fSStanislav Kinsbursky 
1080c21a588fSStanislav Kinsbursky void rpc_put_sb_net(const struct net *net)
1081c21a588fSStanislav Kinsbursky {
1082c21a588fSStanislav Kinsbursky 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1083c21a588fSStanislav Kinsbursky 
1084c21a588fSStanislav Kinsbursky 	BUG_ON(sn->pipefs_sb == NULL);
1085c21a588fSStanislav Kinsbursky 	mutex_unlock(&sn->pipefs_sb_lock);
1086c21a588fSStanislav Kinsbursky }
1087c21a588fSStanislav Kinsbursky EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1088c21a588fSStanislav Kinsbursky 
10891da177e4SLinus Torvalds static int
10901da177e4SLinus Torvalds rpc_fill_super(struct super_block *sb, void *data, int silent)
10911da177e4SLinus Torvalds {
10921da177e4SLinus Torvalds 	struct inode *inode;
10931da177e4SLinus Torvalds 	struct dentry *root;
109438b0da75SStanislav Kinsbursky 	struct net *net = data;
109590c4e829SStanislav Kinsbursky 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
10962d00131aSStanislav Kinsbursky 	int err;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
10991da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
11001da177e4SLinus Torvalds 	sb->s_magic = RPCAUTH_GSSMAGIC;
11011da177e4SLinus Torvalds 	sb->s_op = &s_ops;
11021da177e4SLinus Torvalds 	sb->s_time_gran = 1;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	inode = rpc_get_inode(sb, S_IFDIR | 0755);
11051da177e4SLinus Torvalds 	if (!inode)
11061da177e4SLinus Torvalds 		return -ENOMEM;
1107fc7bed8cSAl Viro 	sb->s_root = root = d_alloc_root(inode);
11081da177e4SLinus Torvalds 	if (!root) {
11091da177e4SLinus Torvalds 		iput(inode);
11101da177e4SLinus Torvalds 		return -ENOMEM;
11111da177e4SLinus Torvalds 	}
1112ac6feceeSTrond Myklebust 	if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
11131da177e4SLinus Torvalds 		return -ENOMEM;
1114efc46bf2SStanislav Kinsbursky 	dprintk("RPC:	sending pipefs MOUNT notification for net %p%s\n", net,
1115efc46bf2SStanislav Kinsbursky 								NET_NAME(net));
11162d00131aSStanislav Kinsbursky 	err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
11172d00131aSStanislav Kinsbursky 					   RPC_PIPEFS_MOUNT,
11182d00131aSStanislav Kinsbursky 					   sb);
11192d00131aSStanislav Kinsbursky 	if (err)
11202d00131aSStanislav Kinsbursky 		goto err_depopulate;
1121021c68deSStanislav Kinsbursky 	sb->s_fs_info = get_net(net);
112290c4e829SStanislav Kinsbursky 	sn->pipefs_sb = sb;
1123fc7bed8cSAl Viro 	return 0;
11242d00131aSStanislav Kinsbursky 
11252d00131aSStanislav Kinsbursky err_depopulate:
11262d00131aSStanislav Kinsbursky 	blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
11272d00131aSStanislav Kinsbursky 					   RPC_PIPEFS_UMOUNT,
11282d00131aSStanislav Kinsbursky 					   sb);
11292d00131aSStanislav Kinsbursky 	__rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
11302d00131aSStanislav Kinsbursky 	return err;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds 
1133fc14f2feSAl Viro static struct dentry *
1134fc14f2feSAl Viro rpc_mount(struct file_system_type *fs_type,
1135fc14f2feSAl Viro 		int flags, const char *dev_name, void *data)
11361da177e4SLinus Torvalds {
113738b0da75SStanislav Kinsbursky 	return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
11381da177e4SLinus Torvalds }
11391da177e4SLinus Torvalds 
1140021c68deSStanislav Kinsbursky void rpc_kill_sb(struct super_block *sb)
1141021c68deSStanislav Kinsbursky {
1142021c68deSStanislav Kinsbursky 	struct net *net = sb->s_fs_info;
114390c4e829SStanislav Kinsbursky 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1144021c68deSStanislav Kinsbursky 
1145c21a588fSStanislav Kinsbursky 	mutex_lock(&sn->pipefs_sb_lock);
114690c4e829SStanislav Kinsbursky 	sn->pipefs_sb = NULL;
1147c21a588fSStanislav Kinsbursky 	mutex_unlock(&sn->pipefs_sb_lock);
1148021c68deSStanislav Kinsbursky 	put_net(net);
1149efc46bf2SStanislav Kinsbursky 	dprintk("RPC:	sending pipefs UMOUNT notification for net %p%s\n", net,
1150efc46bf2SStanislav Kinsbursky 								NET_NAME(net));
11512d00131aSStanislav Kinsbursky 	blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
11522d00131aSStanislav Kinsbursky 					   RPC_PIPEFS_UMOUNT,
11532d00131aSStanislav Kinsbursky 					   sb);
1154021c68deSStanislav Kinsbursky 	kill_litter_super(sb);
1155021c68deSStanislav Kinsbursky }
1156021c68deSStanislav Kinsbursky 
11571da177e4SLinus Torvalds static struct file_system_type rpc_pipe_fs_type = {
11581da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
11591da177e4SLinus Torvalds 	.name		= "rpc_pipefs",
1160fc14f2feSAl Viro 	.mount		= rpc_mount,
1161021c68deSStanislav Kinsbursky 	.kill_sb	= rpc_kill_sb,
11621da177e4SLinus Torvalds };
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds static void
116551cc5068SAlexey Dobriyan init_once(void *foo)
11661da177e4SLinus Torvalds {
11671da177e4SLinus Torvalds 	struct rpc_inode *rpci = (struct rpc_inode *) foo;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	inode_init_once(&rpci->vfs_inode);
11701da177e4SLinus Torvalds 	rpci->private = NULL;
1171ba9e0975SStanislav Kinsbursky 	rpci->pipe = NULL;
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds int register_rpc_pipefs(void)
11751da177e4SLinus Torvalds {
11765bd5f581SAkinobu Mita 	int err;
11775bd5f581SAkinobu Mita 
11781da177e4SLinus Torvalds 	rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
11791da177e4SLinus Torvalds 				sizeof(struct rpc_inode),
1180fffb60f9SPaul Jackson 				0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1181fffb60f9SPaul Jackson 						SLAB_MEM_SPREAD),
118220c2df83SPaul Mundt 				init_once);
11831da177e4SLinus Torvalds 	if (!rpc_inode_cachep)
11841da177e4SLinus Torvalds 		return -ENOMEM;
11855bd5f581SAkinobu Mita 	err = register_filesystem(&rpc_pipe_fs_type);
11865bd5f581SAkinobu Mita 	if (err) {
11875bd5f581SAkinobu Mita 		kmem_cache_destroy(rpc_inode_cachep);
11885bd5f581SAkinobu Mita 		return err;
11895bd5f581SAkinobu Mita 	}
11905bd5f581SAkinobu Mita 
11911da177e4SLinus Torvalds 	return 0;
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds void unregister_rpc_pipefs(void)
11951da177e4SLinus Torvalds {
11961a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(rpc_inode_cachep);
11971da177e4SLinus Torvalds 	unregister_filesystem(&rpc_pipe_fs_type);
11981da177e4SLinus Torvalds }
1199dcbf8c30SMichal Schmidt 
1200dcbf8c30SMichal Schmidt /* Make 'mount -t rpc_pipefs ...' autoload this module. */
1201dcbf8c30SMichal Schmidt MODULE_ALIAS("rpc_pipefs");
1202