xref: /openbmc/linux/net/sunrpc/rpc_pipe.c (revision 38b0da75)
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>
301da177e4SLinus Torvalds 
31fc14f2feSAl Viro static struct vfsmount *rpc_mnt __read_mostly;
321da177e4SLinus Torvalds static int rpc_mount_count;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds static struct file_system_type rpc_pipe_fs_type;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds 
37e18b890bSChristoph Lameter static struct kmem_cache *rpc_inode_cachep __read_mostly;
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define RPC_UPCALL_TIMEOUT (30*HZ)
401da177e4SLinus Torvalds 
419842ef35STrond Myklebust static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
429842ef35STrond Myklebust 		void (*destroy_msg)(struct rpc_pipe_msg *), int err)
43b3eb67a2STrond Myklebust {
44b3eb67a2STrond Myklebust 	struct rpc_pipe_msg *msg;
45b3eb67a2STrond Myklebust 
469842ef35STrond Myklebust 	if (list_empty(head))
479842ef35STrond Myklebust 		return;
489842ef35STrond Myklebust 	do {
49b3eb67a2STrond Myklebust 		msg = list_entry(head->next, struct rpc_pipe_msg, list);
505a67657aSTrond Myklebust 		list_del_init(&msg->list);
51b3eb67a2STrond Myklebust 		msg->errno = err;
52b3eb67a2STrond Myklebust 		destroy_msg(msg);
539842ef35STrond Myklebust 	} while (!list_empty(head));
541da177e4SLinus Torvalds 	wake_up(&rpci->waitq);
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static void
5865f27f38SDavid Howells rpc_timeout_upcall_queue(struct work_struct *work)
591da177e4SLinus Torvalds {
609842ef35STrond Myklebust 	LIST_HEAD(free_list);
6165f27f38SDavid Howells 	struct rpc_inode *rpci =
6265f27f38SDavid Howells 		container_of(work, struct rpc_inode, queue_timeout.work);
631da177e4SLinus Torvalds 	struct inode *inode = &rpci->vfs_inode;
649842ef35STrond Myklebust 	void (*destroy_msg)(struct rpc_pipe_msg *);
651da177e4SLinus Torvalds 
669842ef35STrond Myklebust 	spin_lock(&inode->i_lock);
679842ef35STrond Myklebust 	if (rpci->ops == NULL) {
689842ef35STrond Myklebust 		spin_unlock(&inode->i_lock);
699842ef35STrond Myklebust 		return;
709842ef35STrond Myklebust 	}
719842ef35STrond Myklebust 	destroy_msg = rpci->ops->destroy_msg;
729842ef35STrond Myklebust 	if (rpci->nreaders == 0) {
739842ef35STrond Myklebust 		list_splice_init(&rpci->pipe, &free_list);
749842ef35STrond Myklebust 		rpci->pipelen = 0;
759842ef35STrond Myklebust 	}
769842ef35STrond Myklebust 	spin_unlock(&inode->i_lock);
779842ef35STrond Myklebust 	rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
80c1225158SPeng Tao ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
81c1225158SPeng Tao 				char __user *dst, size_t buflen)
82c1225158SPeng Tao {
83c1225158SPeng Tao 	char *data = (char *)msg->data + msg->copied;
84c1225158SPeng Tao 	size_t mlen = min(msg->len - msg->copied, buflen);
85c1225158SPeng Tao 	unsigned long left;
86c1225158SPeng Tao 
87c1225158SPeng Tao 	left = copy_to_user(dst, data, mlen);
88c1225158SPeng Tao 	if (left == mlen) {
89c1225158SPeng Tao 		msg->errno = -EFAULT;
90c1225158SPeng Tao 		return -EFAULT;
91c1225158SPeng Tao 	}
92c1225158SPeng Tao 
93c1225158SPeng Tao 	mlen -= left;
94c1225158SPeng Tao 	msg->copied += mlen;
95c1225158SPeng Tao 	msg->errno = 0;
96c1225158SPeng Tao 	return mlen;
97c1225158SPeng Tao }
98c1225158SPeng Tao EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
99c1225158SPeng Tao 
10093a44a75SJ. Bruce Fields /**
1011a5778aaSBen Hutchings  * rpc_queue_upcall - queue an upcall message to userspace
10293a44a75SJ. Bruce Fields  * @inode: inode of upcall pipe on which to queue given message
10393a44a75SJ. Bruce Fields  * @msg: message to queue
10493a44a75SJ. Bruce Fields  *
10593a44a75SJ. Bruce Fields  * Call with an @inode created by rpc_mkpipe() to queue an upcall.
10693a44a75SJ. Bruce Fields  * A userspace process may then later read the upcall by performing a
10793a44a75SJ. Bruce Fields  * read on an open file for this inode.  It is up to the caller to
10893a44a75SJ. Bruce Fields  * initialize the fields of @msg (other than @msg->list) appropriately.
10993a44a75SJ. Bruce Fields  */
1101da177e4SLinus Torvalds int
1111da177e4SLinus Torvalds rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	struct rpc_inode *rpci = RPC_I(inode);
1146070fe6fSTrond Myklebust 	int res = -EPIPE;
1151da177e4SLinus Torvalds 
1169842ef35STrond Myklebust 	spin_lock(&inode->i_lock);
1176070fe6fSTrond Myklebust 	if (rpci->ops == NULL)
1186070fe6fSTrond Myklebust 		goto out;
1191da177e4SLinus Torvalds 	if (rpci->nreaders) {
1201da177e4SLinus Torvalds 		list_add_tail(&msg->list, &rpci->pipe);
1211da177e4SLinus Torvalds 		rpci->pipelen += msg->len;
1226070fe6fSTrond Myklebust 		res = 0;
1231da177e4SLinus Torvalds 	} else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
1241da177e4SLinus Torvalds 		if (list_empty(&rpci->pipe))
12524c5d9d7STrond Myklebust 			queue_delayed_work(rpciod_workqueue,
12624c5d9d7STrond Myklebust 					&rpci->queue_timeout,
1271da177e4SLinus Torvalds 					RPC_UPCALL_TIMEOUT);
1281da177e4SLinus Torvalds 		list_add_tail(&msg->list, &rpci->pipe);
1291da177e4SLinus Torvalds 		rpci->pipelen += msg->len;
1306070fe6fSTrond Myklebust 		res = 0;
1316070fe6fSTrond Myklebust 	}
1326070fe6fSTrond Myklebust out:
1339842ef35STrond Myklebust 	spin_unlock(&inode->i_lock);
1341da177e4SLinus Torvalds 	wake_up(&rpci->waitq);
1351da177e4SLinus Torvalds 	return res;
1361da177e4SLinus Torvalds }
137468039eeSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_queue_upcall);
1381da177e4SLinus Torvalds 
1396070fe6fSTrond Myklebust static inline void
1406070fe6fSTrond Myklebust rpc_inode_setowner(struct inode *inode, void *private)
1416070fe6fSTrond Myklebust {
1426070fe6fSTrond Myklebust 	RPC_I(inode)->private = private;
1436070fe6fSTrond Myklebust }
1446070fe6fSTrond Myklebust 
1451da177e4SLinus Torvalds static void
1461da177e4SLinus Torvalds rpc_close_pipes(struct inode *inode)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	struct rpc_inode *rpci = RPC_I(inode);
149b693ba4aSTrond Myklebust 	const struct rpc_pipe_ops *ops;
150e712804aS\"J. Bruce Fields\ 	int need_release;
1511da177e4SLinus Torvalds 
1521b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
1539842ef35STrond Myklebust 	ops = rpci->ops;
1549842ef35STrond Myklebust 	if (ops != NULL) {
1559842ef35STrond Myklebust 		LIST_HEAD(free_list);
1569842ef35STrond Myklebust 		spin_lock(&inode->i_lock);
157e712804aS\"J. Bruce Fields\ 		need_release = rpci->nreaders != 0 || rpci->nwriters != 0;
1581da177e4SLinus Torvalds 		rpci->nreaders = 0;
1599842ef35STrond Myklebust 		list_splice_init(&rpci->in_upcall, &free_list);
1609842ef35STrond Myklebust 		list_splice_init(&rpci->pipe, &free_list);
1619842ef35STrond Myklebust 		rpci->pipelen = 0;
1621da177e4SLinus Torvalds 		rpci->ops = NULL;
1639842ef35STrond Myklebust 		spin_unlock(&inode->i_lock);
1649842ef35STrond Myklebust 		rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
1659842ef35STrond Myklebust 		rpci->nwriters = 0;
166e712804aS\"J. Bruce Fields\ 		if (need_release && ops->release_pipe)
1679842ef35STrond Myklebust 			ops->release_pipe(inode);
1684011cd97STrond Myklebust 		cancel_delayed_work_sync(&rpci->queue_timeout);
1691da177e4SLinus Torvalds 	}
1706070fe6fSTrond Myklebust 	rpc_inode_setowner(inode, NULL);
1711b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static struct inode *
1751da177e4SLinus Torvalds rpc_alloc_inode(struct super_block *sb)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	struct rpc_inode *rpci;
178e94b1766SChristoph Lameter 	rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
1791da177e4SLinus Torvalds 	if (!rpci)
1801da177e4SLinus Torvalds 		return NULL;
1811da177e4SLinus Torvalds 	return &rpci->vfs_inode;
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds static void
185fa0d7e3dSNick Piggin rpc_i_callback(struct rcu_head *head)
186fa0d7e3dSNick Piggin {
187fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
188fa0d7e3dSNick Piggin 	kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
189fa0d7e3dSNick Piggin }
190fa0d7e3dSNick Piggin 
191fa0d7e3dSNick Piggin static void
1921da177e4SLinus Torvalds rpc_destroy_inode(struct inode *inode)
1931da177e4SLinus Torvalds {
194fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, rpc_i_callback);
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds static int
1981da177e4SLinus Torvalds rpc_pipe_open(struct inode *inode, struct file *filp)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	struct rpc_inode *rpci = RPC_I(inode);
201c3810608S\"J. Bruce Fields\ 	int first_open;
2021da177e4SLinus Torvalds 	int res = -ENXIO;
2031da177e4SLinus Torvalds 
2041b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
205c3810608S\"J. Bruce Fields\ 	if (rpci->ops == NULL)
206c3810608S\"J. Bruce Fields\ 		goto out;
207c3810608S\"J. Bruce Fields\ 	first_open = rpci->nreaders == 0 && rpci->nwriters == 0;
208c3810608S\"J. Bruce Fields\ 	if (first_open && rpci->ops->open_pipe) {
209c3810608S\"J. Bruce Fields\ 		res = rpci->ops->open_pipe(inode);
210c3810608S\"J. Bruce Fields\ 		if (res)
211c3810608S\"J. Bruce Fields\ 			goto out;
212c3810608S\"J. Bruce Fields\ 	}
2131da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_READ)
2141da177e4SLinus Torvalds 		rpci->nreaders++;
2151da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_WRITE)
2161da177e4SLinus Torvalds 		rpci->nwriters++;
2171da177e4SLinus Torvalds 	res = 0;
218c3810608S\"J. Bruce Fields\ out:
2191b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
2201da177e4SLinus Torvalds 	return res;
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds static int
2241da177e4SLinus Torvalds rpc_pipe_release(struct inode *inode, struct file *filp)
2251da177e4SLinus Torvalds {
226969b7f25STrond Myklebust 	struct rpc_inode *rpci = RPC_I(inode);
2271da177e4SLinus Torvalds 	struct rpc_pipe_msg *msg;
228e712804aS\"J. Bruce Fields\ 	int last_close;
2291da177e4SLinus Torvalds 
2301b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
2311da177e4SLinus Torvalds 	if (rpci->ops == NULL)
2321da177e4SLinus Torvalds 		goto out;
233655b5bb4SJoe Perches 	msg = filp->private_data;
2341da177e4SLinus Torvalds 	if (msg != NULL) {
2359842ef35STrond Myklebust 		spin_lock(&inode->i_lock);
23648e49187STrond Myklebust 		msg->errno = -EAGAIN;
2375a67657aSTrond Myklebust 		list_del_init(&msg->list);
2389842ef35STrond Myklebust 		spin_unlock(&inode->i_lock);
2391da177e4SLinus Torvalds 		rpci->ops->destroy_msg(msg);
2401da177e4SLinus Torvalds 	}
2411da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_WRITE)
2421da177e4SLinus Torvalds 		rpci->nwriters --;
2439842ef35STrond Myklebust 	if (filp->f_mode & FMODE_READ) {
2441da177e4SLinus Torvalds 		rpci->nreaders --;
2459842ef35STrond Myklebust 		if (rpci->nreaders == 0) {
2469842ef35STrond Myklebust 			LIST_HEAD(free_list);
2479842ef35STrond Myklebust 			spin_lock(&inode->i_lock);
2489842ef35STrond Myklebust 			list_splice_init(&rpci->pipe, &free_list);
2499842ef35STrond Myklebust 			rpci->pipelen = 0;
2509842ef35STrond Myklebust 			spin_unlock(&inode->i_lock);
2519842ef35STrond Myklebust 			rpc_purge_list(rpci, &free_list,
2529842ef35STrond Myklebust 					rpci->ops->destroy_msg, -EAGAIN);
2539842ef35STrond Myklebust 		}
2549842ef35STrond Myklebust 	}
255e712804aS\"J. Bruce Fields\ 	last_close = rpci->nwriters == 0 && rpci->nreaders == 0;
256e712804aS\"J. Bruce Fields\ 	if (last_close && rpci->ops->release_pipe)
2571da177e4SLinus Torvalds 		rpci->ops->release_pipe(inode);
2581da177e4SLinus Torvalds out:
2591b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
2601da177e4SLinus Torvalds 	return 0;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds static ssize_t
2641da177e4SLinus Torvalds rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
2651da177e4SLinus Torvalds {
266303b46bbSJosef Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
2671da177e4SLinus Torvalds 	struct rpc_inode *rpci = RPC_I(inode);
2681da177e4SLinus Torvalds 	struct rpc_pipe_msg *msg;
2691da177e4SLinus Torvalds 	int res = 0;
2701da177e4SLinus Torvalds 
2711b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
2721da177e4SLinus Torvalds 	if (rpci->ops == NULL) {
2731da177e4SLinus Torvalds 		res = -EPIPE;
2741da177e4SLinus Torvalds 		goto out_unlock;
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds 	msg = filp->private_data;
2771da177e4SLinus Torvalds 	if (msg == NULL) {
2789842ef35STrond Myklebust 		spin_lock(&inode->i_lock);
2791da177e4SLinus Torvalds 		if (!list_empty(&rpci->pipe)) {
2801da177e4SLinus Torvalds 			msg = list_entry(rpci->pipe.next,
2811da177e4SLinus Torvalds 					struct rpc_pipe_msg,
2821da177e4SLinus Torvalds 					list);
2831da177e4SLinus Torvalds 			list_move(&msg->list, &rpci->in_upcall);
2841da177e4SLinus Torvalds 			rpci->pipelen -= msg->len;
2851da177e4SLinus Torvalds 			filp->private_data = msg;
2861da177e4SLinus Torvalds 			msg->copied = 0;
2871da177e4SLinus Torvalds 		}
2889842ef35STrond Myklebust 		spin_unlock(&inode->i_lock);
2891da177e4SLinus Torvalds 		if (msg == NULL)
2901da177e4SLinus Torvalds 			goto out_unlock;
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 	/* NOTE: it is up to the callback to update msg->copied */
2931da177e4SLinus Torvalds 	res = rpci->ops->upcall(filp, msg, buf, len);
2941da177e4SLinus Torvalds 	if (res < 0 || msg->len == msg->copied) {
2951da177e4SLinus Torvalds 		filp->private_data = NULL;
2969842ef35STrond Myklebust 		spin_lock(&inode->i_lock);
2975a67657aSTrond Myklebust 		list_del_init(&msg->list);
2989842ef35STrond Myklebust 		spin_unlock(&inode->i_lock);
2991da177e4SLinus Torvalds 		rpci->ops->destroy_msg(msg);
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds out_unlock:
3021b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
3031da177e4SLinus Torvalds 	return res;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds static ssize_t
3071da177e4SLinus Torvalds rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
3081da177e4SLinus Torvalds {
309303b46bbSJosef Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
3101da177e4SLinus Torvalds 	struct rpc_inode *rpci = RPC_I(inode);
3111da177e4SLinus Torvalds 	int res;
3121da177e4SLinus Torvalds 
3131b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
3141da177e4SLinus Torvalds 	res = -EPIPE;
3151da177e4SLinus Torvalds 	if (rpci->ops != NULL)
3161da177e4SLinus Torvalds 		res = rpci->ops->downcall(filp, buf, len);
3171b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
3181da177e4SLinus Torvalds 	return res;
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds static unsigned int
3221da177e4SLinus Torvalds rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	struct rpc_inode *rpci;
3251da177e4SLinus Torvalds 	unsigned int mask = 0;
3261da177e4SLinus Torvalds 
327303b46bbSJosef Sipek 	rpci = RPC_I(filp->f_path.dentry->d_inode);
3281da177e4SLinus Torvalds 	poll_wait(filp, &rpci->waitq, wait);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	mask = POLLOUT | POLLWRNORM;
3311da177e4SLinus Torvalds 	if (rpci->ops == NULL)
3321da177e4SLinus Torvalds 		mask |= POLLERR | POLLHUP;
333eda4f9b7SJ. Bruce Fields 	if (filp->private_data || !list_empty(&rpci->pipe))
3341da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
3351da177e4SLinus Torvalds 	return mask;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
338a6f8dbc6SArnd Bergmann static long
339a6f8dbc6SArnd Bergmann rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3401da177e4SLinus Torvalds {
341a6f8dbc6SArnd Bergmann 	struct inode *inode = filp->f_path.dentry->d_inode;
342a6f8dbc6SArnd Bergmann 	struct rpc_inode *rpci = RPC_I(inode);
3431da177e4SLinus Torvalds 	int len;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	switch (cmd) {
3461da177e4SLinus Torvalds 	case FIONREAD:
347a6f8dbc6SArnd Bergmann 		spin_lock(&inode->i_lock);
348a6f8dbc6SArnd Bergmann 		if (rpci->ops == NULL) {
349a6f8dbc6SArnd Bergmann 			spin_unlock(&inode->i_lock);
3501da177e4SLinus Torvalds 			return -EPIPE;
351a6f8dbc6SArnd Bergmann 		}
3521da177e4SLinus Torvalds 		len = rpci->pipelen;
3531da177e4SLinus Torvalds 		if (filp->private_data) {
3541da177e4SLinus Torvalds 			struct rpc_pipe_msg *msg;
355655b5bb4SJoe Perches 			msg = filp->private_data;
3561da177e4SLinus Torvalds 			len += msg->len - msg->copied;
3571da177e4SLinus Torvalds 		}
358a6f8dbc6SArnd Bergmann 		spin_unlock(&inode->i_lock);
3591da177e4SLinus Torvalds 		return put_user(len, (int __user *)arg);
3601da177e4SLinus Torvalds 	default:
3611da177e4SLinus Torvalds 		return -EINVAL;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
365da7071d7SArjan van de Ven static const struct file_operations rpc_pipe_fops = {
3661da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
3671da177e4SLinus Torvalds 	.llseek		= no_llseek,
3681da177e4SLinus Torvalds 	.read		= rpc_pipe_read,
3691da177e4SLinus Torvalds 	.write		= rpc_pipe_write,
3701da177e4SLinus Torvalds 	.poll		= rpc_pipe_poll,
371674b604cSFrederic Weisbecker 	.unlocked_ioctl	= rpc_pipe_ioctl,
3721da177e4SLinus Torvalds 	.open		= rpc_pipe_open,
3731da177e4SLinus Torvalds 	.release	= rpc_pipe_release,
3741da177e4SLinus Torvalds };
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds static int
3771da177e4SLinus Torvalds rpc_show_info(struct seq_file *m, void *v)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	struct rpc_clnt *clnt = m->private;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	seq_printf(m, "RPC server: %s\n", clnt->cl_server);
3821da177e4SLinus Torvalds 	seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
3831da177e4SLinus Torvalds 			clnt->cl_prog, clnt->cl_vers);
384e7f78657SChuck Lever 	seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
385e7f78657SChuck Lever 	seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
386bf19aaceSJ. Bruce Fields 	seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
3871da177e4SLinus Torvalds 	return 0;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds static int
3911da177e4SLinus Torvalds rpc_info_open(struct inode *inode, struct file *file)
3921da177e4SLinus Torvalds {
393006abe88STrond Myklebust 	struct rpc_clnt *clnt = NULL;
3941da177e4SLinus Torvalds 	int ret = single_open(file, rpc_show_info, NULL);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	if (!ret) {
3971da177e4SLinus Torvalds 		struct seq_file *m = file->private_data;
398006abe88STrond Myklebust 
399006abe88STrond Myklebust 		spin_lock(&file->f_path.dentry->d_lock);
400006abe88STrond Myklebust 		if (!d_unhashed(file->f_path.dentry))
4011da177e4SLinus Torvalds 			clnt = RPC_I(inode)->private;
402006abe88STrond Myklebust 		if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
403006abe88STrond Myklebust 			spin_unlock(&file->f_path.dentry->d_lock);
4041da177e4SLinus Torvalds 			m->private = clnt;
4051da177e4SLinus Torvalds 		} else {
406006abe88STrond Myklebust 			spin_unlock(&file->f_path.dentry->d_lock);
4071da177e4SLinus Torvalds 			single_release(inode, file);
4081da177e4SLinus Torvalds 			ret = -EINVAL;
4091da177e4SLinus Torvalds 		}
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 	return ret;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds static int
4151da177e4SLinus Torvalds rpc_info_release(struct inode *inode, struct file *file)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	struct seq_file *m = file->private_data;
4181da177e4SLinus Torvalds 	struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	if (clnt)
4211da177e4SLinus Torvalds 		rpc_release_client(clnt);
4221da177e4SLinus Torvalds 	return single_release(inode, file);
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
425da7071d7SArjan van de Ven static const struct file_operations rpc_info_operations = {
4261da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
4271da177e4SLinus Torvalds 	.open		= rpc_info_open,
4281da177e4SLinus Torvalds 	.read		= seq_read,
4291da177e4SLinus Torvalds 	.llseek		= seq_lseek,
4301da177e4SLinus Torvalds 	.release	= rpc_info_release,
4311da177e4SLinus Torvalds };
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /*
4351da177e4SLinus Torvalds  * Description of fs contents.
4361da177e4SLinus Torvalds  */
4371da177e4SLinus Torvalds struct rpc_filelist {
438ac6feceeSTrond Myklebust 	const char *name;
43999ac48f5SArjan van de Ven 	const struct file_operations *i_fop;
4407364af6aSTrond Myklebust 	umode_t mode;
4411da177e4SLinus Torvalds };
4421da177e4SLinus Torvalds 
44354281548STrond Myklebust struct vfsmount *rpc_get_mount(void)
4441da177e4SLinus Torvalds {
44554281548STrond Myklebust 	int err;
44654281548STrond Myklebust 
447fc14f2feSAl Viro 	err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
44854281548STrond Myklebust 	if (err != 0)
44954281548STrond Myklebust 		return ERR_PTR(err);
450fc14f2feSAl Viro 	return rpc_mnt;
4511da177e4SLinus Torvalds }
452e571cbf1STrond Myklebust EXPORT_SYMBOL_GPL(rpc_get_mount);
4531da177e4SLinus Torvalds 
45454281548STrond Myklebust void rpc_put_mount(void)
4551da177e4SLinus Torvalds {
456fc14f2feSAl Viro 	simple_release_fs(&rpc_mnt, &rpc_mount_count);
4571da177e4SLinus Torvalds }
458e571cbf1STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_mount);
4591da177e4SLinus Torvalds 
460fe15ce44SNick Piggin static int rpc_delete_dentry(const struct dentry *dentry)
46162e1761cSTrond Myklebust {
46262e1761cSTrond Myklebust 	return 1;
46362e1761cSTrond Myklebust }
46462e1761cSTrond Myklebust 
4653ba13d17SAl Viro static const struct dentry_operations rpc_dentry_operations = {
46662e1761cSTrond Myklebust 	.d_delete = rpc_delete_dentry,
46762e1761cSTrond Myklebust };
46862e1761cSTrond Myklebust 
4691da177e4SLinus Torvalds static struct inode *
4707364af6aSTrond Myklebust rpc_get_inode(struct super_block *sb, umode_t mode)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	struct inode *inode = new_inode(sb);
4731da177e4SLinus Torvalds 	if (!inode)
4741da177e4SLinus Torvalds 		return NULL;
47585fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
4761da177e4SLinus Torvalds 	inode->i_mode = mode;
4771da177e4SLinus Torvalds 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
4781da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
4791da177e4SLinus Torvalds 	case S_IFDIR:
4801da177e4SLinus Torvalds 		inode->i_fop = &simple_dir_operations;
4811da177e4SLinus Torvalds 		inode->i_op = &simple_dir_inode_operations;
482d8c76e6fSDave Hansen 		inc_nlink(inode);
4831da177e4SLinus Torvalds 	default:
4841da177e4SLinus Torvalds 		break;
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds 	return inode;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4897589806eSTrond Myklebust static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
4907589806eSTrond Myklebust 			       umode_t mode,
4917589806eSTrond Myklebust 			       const struct file_operations *i_fop,
4927589806eSTrond Myklebust 			       void *private)
4937589806eSTrond Myklebust {
4947589806eSTrond Myklebust 	struct inode *inode;
4957589806eSTrond Myklebust 
496beb0f0a9STrond Myklebust 	d_drop(dentry);
4977589806eSTrond Myklebust 	inode = rpc_get_inode(dir->i_sb, mode);
4987589806eSTrond Myklebust 	if (!inode)
4997589806eSTrond Myklebust 		goto out_err;
5007589806eSTrond Myklebust 	inode->i_ino = iunique(dir->i_sb, 100);
5017589806eSTrond Myklebust 	if (i_fop)
5027589806eSTrond Myklebust 		inode->i_fop = i_fop;
5037589806eSTrond Myklebust 	if (private)
5047589806eSTrond Myklebust 		rpc_inode_setowner(inode, private);
5057589806eSTrond Myklebust 	d_add(dentry, inode);
5067589806eSTrond Myklebust 	return 0;
5077589806eSTrond Myklebust out_err:
5087589806eSTrond Myklebust 	printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
5097589806eSTrond Myklebust 			__FILE__, __func__, dentry->d_name.name);
5107589806eSTrond Myklebust 	dput(dentry);
5117589806eSTrond Myklebust 	return -ENOMEM;
5127589806eSTrond Myklebust }
5137589806eSTrond Myklebust 
514ac6feceeSTrond Myklebust static int __rpc_create(struct inode *dir, struct dentry *dentry,
515ac6feceeSTrond Myklebust 			umode_t mode,
516ac6feceeSTrond Myklebust 			const struct file_operations *i_fop,
517ac6feceeSTrond Myklebust 			void *private)
518ac6feceeSTrond Myklebust {
519ac6feceeSTrond Myklebust 	int err;
520ac6feceeSTrond Myklebust 
521ac6feceeSTrond Myklebust 	err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
522ac6feceeSTrond Myklebust 	if (err)
523ac6feceeSTrond Myklebust 		return err;
524ac6feceeSTrond Myklebust 	fsnotify_create(dir, dentry);
525ac6feceeSTrond Myklebust 	return 0;
526ac6feceeSTrond Myklebust }
527ac6feceeSTrond Myklebust 
5287589806eSTrond Myklebust static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
5297589806eSTrond Myklebust 		       umode_t mode,
5307589806eSTrond Myklebust 		       const struct file_operations *i_fop,
5317589806eSTrond Myklebust 		       void *private)
5327589806eSTrond Myklebust {
5337589806eSTrond Myklebust 	int err;
5347589806eSTrond Myklebust 
5357589806eSTrond Myklebust 	err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
5367589806eSTrond Myklebust 	if (err)
5377589806eSTrond Myklebust 		return err;
5387589806eSTrond Myklebust 	inc_nlink(dir);
5397589806eSTrond Myklebust 	fsnotify_mkdir(dir, dentry);
5407589806eSTrond Myklebust 	return 0;
5417589806eSTrond Myklebust }
5427589806eSTrond Myklebust 
5437589806eSTrond Myklebust static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
5447589806eSTrond Myklebust 			umode_t mode,
5457589806eSTrond Myklebust 			const struct file_operations *i_fop,
5467589806eSTrond Myklebust 			void *private,
5477589806eSTrond Myklebust 			const struct rpc_pipe_ops *ops,
5487589806eSTrond Myklebust 			int flags)
5497589806eSTrond Myklebust {
5507589806eSTrond Myklebust 	struct rpc_inode *rpci;
5517589806eSTrond Myklebust 	int err;
5527589806eSTrond Myklebust 
5537589806eSTrond Myklebust 	err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
5547589806eSTrond Myklebust 	if (err)
5557589806eSTrond Myklebust 		return err;
5567589806eSTrond Myklebust 	rpci = RPC_I(dentry->d_inode);
5577589806eSTrond Myklebust 	rpci->private = private;
5587589806eSTrond Myklebust 	rpci->flags = flags;
5597589806eSTrond Myklebust 	rpci->ops = ops;
5607589806eSTrond Myklebust 	fsnotify_create(dir, dentry);
5617589806eSTrond Myklebust 	return 0;
5627589806eSTrond Myklebust }
5637589806eSTrond Myklebust 
564ac6feceeSTrond Myklebust static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
565ac6feceeSTrond Myklebust {
566ac6feceeSTrond Myklebust 	int ret;
567ac6feceeSTrond Myklebust 
568ac6feceeSTrond Myklebust 	dget(dentry);
569ac6feceeSTrond Myklebust 	ret = simple_rmdir(dir, dentry);
570ac6feceeSTrond Myklebust 	d_delete(dentry);
571ac6feceeSTrond Myklebust 	dput(dentry);
572ac6feceeSTrond Myklebust 	return ret;
573ac6feceeSTrond Myklebust }
574ac6feceeSTrond Myklebust 
575810d90bcSTrond Myklebust static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
576810d90bcSTrond Myklebust {
577810d90bcSTrond Myklebust 	int ret;
578810d90bcSTrond Myklebust 
579810d90bcSTrond Myklebust 	dget(dentry);
580810d90bcSTrond Myklebust 	ret = simple_unlink(dir, dentry);
581810d90bcSTrond Myklebust 	d_delete(dentry);
582810d90bcSTrond Myklebust 	dput(dentry);
583810d90bcSTrond Myklebust 	return ret;
584810d90bcSTrond Myklebust }
585810d90bcSTrond Myklebust 
586810d90bcSTrond Myklebust static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
587810d90bcSTrond Myklebust {
588810d90bcSTrond Myklebust 	struct inode *inode = dentry->d_inode;
589810d90bcSTrond Myklebust 
590810d90bcSTrond Myklebust 	rpc_close_pipes(inode);
591810d90bcSTrond Myklebust 	return __rpc_unlink(dir, dentry);
592810d90bcSTrond Myklebust }
593810d90bcSTrond Myklebust 
594cfeaa4a3STrond Myklebust static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
595cfeaa4a3STrond Myklebust 					  struct qstr *name)
596cfeaa4a3STrond Myklebust {
597cfeaa4a3STrond Myklebust 	struct dentry *dentry;
598cfeaa4a3STrond Myklebust 
5995bff0386SStanislav Kinsbursky 	dentry = d_lookup(parent, name);
6005bff0386SStanislav Kinsbursky 	if (!dentry) {
6015bff0386SStanislav Kinsbursky 		dentry = d_alloc(parent, name);
6025bff0386SStanislav Kinsbursky 		if (!dentry)
6035bff0386SStanislav Kinsbursky 			return ERR_PTR(-ENOMEM);
6045bff0386SStanislav Kinsbursky 	}
6055bff0386SStanislav Kinsbursky 	if (dentry->d_inode == NULL) {
6065bff0386SStanislav Kinsbursky 		d_set_d_op(dentry, &rpc_dentry_operations);
607f1f0abe1SDan Carpenter 		return dentry;
6085bff0386SStanislav Kinsbursky 	}
609cfeaa4a3STrond Myklebust 	dput(dentry);
610cfeaa4a3STrond Myklebust 	return ERR_PTR(-EEXIST);
611cfeaa4a3STrond Myklebust }
612cfeaa4a3STrond Myklebust 
6131da177e4SLinus Torvalds /*
6141da177e4SLinus Torvalds  * FIXME: This probably has races.
6151da177e4SLinus Torvalds  */
616ac6feceeSTrond Myklebust static void __rpc_depopulate(struct dentry *parent,
617ac6feceeSTrond Myklebust 			     const struct rpc_filelist *files,
618ac6feceeSTrond Myklebust 			     int start, int eof)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	struct inode *dir = parent->d_inode;
621ac6feceeSTrond Myklebust 	struct dentry *dentry;
622ac6feceeSTrond Myklebust 	struct qstr name;
623ac6feceeSTrond Myklebust 	int i;
624ac6feceeSTrond Myklebust 
625ac6feceeSTrond Myklebust 	for (i = start; i < eof; i++) {
626ac6feceeSTrond Myklebust 		name.name = files[i].name;
627ac6feceeSTrond Myklebust 		name.len = strlen(files[i].name);
628ac6feceeSTrond Myklebust 		name.hash = full_name_hash(name.name, name.len);
629ac6feceeSTrond Myklebust 		dentry = d_lookup(parent, &name);
630ac6feceeSTrond Myklebust 
631ac6feceeSTrond Myklebust 		if (dentry == NULL)
632ac6feceeSTrond Myklebust 			continue;
633ac6feceeSTrond Myklebust 		if (dentry->d_inode == NULL)
634ac6feceeSTrond Myklebust 			goto next;
635ac6feceeSTrond Myklebust 		switch (dentry->d_inode->i_mode & S_IFMT) {
636ac6feceeSTrond Myklebust 			default:
637ac6feceeSTrond Myklebust 				BUG();
638ac6feceeSTrond Myklebust 			case S_IFREG:
639ac6feceeSTrond Myklebust 				__rpc_unlink(dir, dentry);
640ac6feceeSTrond Myklebust 				break;
641ac6feceeSTrond Myklebust 			case S_IFDIR:
642ac6feceeSTrond Myklebust 				__rpc_rmdir(dir, dentry);
643ac6feceeSTrond Myklebust 		}
644ac6feceeSTrond Myklebust next:
645ac6feceeSTrond Myklebust 		dput(dentry);
646ac6feceeSTrond Myklebust 	}
647ac6feceeSTrond Myklebust }
648ac6feceeSTrond Myklebust 
649ac6feceeSTrond Myklebust static void rpc_depopulate(struct dentry *parent,
650ac6feceeSTrond Myklebust 			   const struct rpc_filelist *files,
651ac6feceeSTrond Myklebust 			   int start, int eof)
652ac6feceeSTrond Myklebust {
653ac6feceeSTrond Myklebust 	struct inode *dir = parent->d_inode;
6541da177e4SLinus Torvalds 
655c6573c29SArjan van de Ven 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
656ac6feceeSTrond Myklebust 	__rpc_depopulate(parent, files, start, eof);
6571b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
660ac6feceeSTrond Myklebust static int rpc_populate(struct dentry *parent,
661ac6feceeSTrond Myklebust 			const struct rpc_filelist *files,
662ac6feceeSTrond Myklebust 			int start, int eof,
663ac6feceeSTrond Myklebust 			void *private)
6641da177e4SLinus Torvalds {
665ac6feceeSTrond Myklebust 	struct inode *dir = parent->d_inode;
6661da177e4SLinus Torvalds 	struct dentry *dentry;
667ac6feceeSTrond Myklebust 	int i, err;
6681da177e4SLinus Torvalds 
6691b1dcc1bSJes Sorensen 	mutex_lock(&dir->i_mutex);
6701da177e4SLinus Torvalds 	for (i = start; i < eof; i++) {
671ac6feceeSTrond Myklebust 		struct qstr q;
672ac6feceeSTrond Myklebust 
673ac6feceeSTrond Myklebust 		q.name = files[i].name;
674ac6feceeSTrond Myklebust 		q.len = strlen(files[i].name);
675ac6feceeSTrond Myklebust 		q.hash = full_name_hash(q.name, q.len);
676ac6feceeSTrond Myklebust 		dentry = __rpc_lookup_create_exclusive(parent, &q);
677ac6feceeSTrond Myklebust 		err = PTR_ERR(dentry);
678ac6feceeSTrond Myklebust 		if (IS_ERR(dentry))
6791da177e4SLinus Torvalds 			goto out_bad;
680ac6feceeSTrond Myklebust 		switch (files[i].mode & S_IFMT) {
681ac6feceeSTrond Myklebust 			default:
682ac6feceeSTrond Myklebust 				BUG();
683ac6feceeSTrond Myklebust 			case S_IFREG:
684ac6feceeSTrond Myklebust 				err = __rpc_create(dir, dentry,
685ac6feceeSTrond Myklebust 						files[i].mode,
686ac6feceeSTrond Myklebust 						files[i].i_fop,
687ac6feceeSTrond Myklebust 						private);
688ac6feceeSTrond Myklebust 				break;
689ac6feceeSTrond Myklebust 			case S_IFDIR:
690ac6feceeSTrond Myklebust 				err = __rpc_mkdir(dir, dentry,
691ac6feceeSTrond Myklebust 						files[i].mode,
692ac6feceeSTrond Myklebust 						NULL,
693ac6feceeSTrond Myklebust 						private);
6941da177e4SLinus Torvalds 		}
695ac6feceeSTrond Myklebust 		if (err != 0)
696ac6feceeSTrond Myklebust 			goto out_bad;
6971da177e4SLinus Torvalds 	}
6981b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
6991da177e4SLinus Torvalds 	return 0;
7001da177e4SLinus Torvalds out_bad:
701ac6feceeSTrond Myklebust 	__rpc_depopulate(parent, files, start, eof);
7021b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
7031da177e4SLinus Torvalds 	printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
7040dc47877SHarvey Harrison 			__FILE__, __func__, parent->d_name.name);
705ac6feceeSTrond Myklebust 	return err;
706f134585aSTrond Myklebust }
707f134585aSTrond Myklebust 
708e57aed77STrond Myklebust static struct dentry *rpc_mkdir_populate(struct dentry *parent,
709e57aed77STrond Myklebust 		struct qstr *name, umode_t mode, void *private,
710e57aed77STrond Myklebust 		int (*populate)(struct dentry *, void *), void *args_populate)
711f134585aSTrond Myklebust {
712f134585aSTrond Myklebust 	struct dentry *dentry;
7137d59d1e8STrond Myklebust 	struct inode *dir = parent->d_inode;
714f134585aSTrond Myklebust 	int error;
715f134585aSTrond Myklebust 
7167d59d1e8STrond Myklebust 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
7177d59d1e8STrond Myklebust 	dentry = __rpc_lookup_create_exclusive(parent, name);
718f134585aSTrond Myklebust 	if (IS_ERR(dentry))
7197d59d1e8STrond Myklebust 		goto out;
7207d59d1e8STrond Myklebust 	error = __rpc_mkdir(dir, dentry, mode, NULL, private);
7217589806eSTrond Myklebust 	if (error != 0)
7227589806eSTrond Myklebust 		goto out_err;
723e57aed77STrond Myklebust 	if (populate != NULL) {
724e57aed77STrond Myklebust 		error = populate(dentry, args_populate);
725f134585aSTrond Myklebust 		if (error)
726ac6feceeSTrond Myklebust 			goto err_rmdir;
727e57aed77STrond Myklebust 	}
728f134585aSTrond Myklebust out:
7291b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
7305c3e985aSTrond Myklebust 	return dentry;
731ac6feceeSTrond Myklebust err_rmdir:
732f134585aSTrond Myklebust 	__rpc_rmdir(dir, dentry);
7337589806eSTrond Myklebust out_err:
734f134585aSTrond Myklebust 	dentry = ERR_PTR(error);
735f134585aSTrond Myklebust 	goto out;
736f134585aSTrond Myklebust }
737f134585aSTrond Myklebust 
738e57aed77STrond Myklebust static int rpc_rmdir_depopulate(struct dentry *dentry,
739e57aed77STrond Myklebust 		void (*depopulate)(struct dentry *))
740f134585aSTrond Myklebust {
741dff02cc1STrond Myklebust 	struct dentry *parent;
742f134585aSTrond Myklebust 	struct inode *dir;
743f134585aSTrond Myklebust 	int error;
744f134585aSTrond Myklebust 
745dff02cc1STrond Myklebust 	parent = dget_parent(dentry);
746dff02cc1STrond Myklebust 	dir = parent->d_inode;
747c6573c29SArjan van de Ven 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
748e57aed77STrond Myklebust 	if (depopulate != NULL)
749e57aed77STrond Myklebust 		depopulate(dentry);
750f134585aSTrond Myklebust 	error = __rpc_rmdir(dir, dentry);
7511b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
752dff02cc1STrond Myklebust 	dput(parent);
753f134585aSTrond Myklebust 	return error;
754f134585aSTrond Myklebust }
7551da177e4SLinus Torvalds 
75693a44a75SJ. Bruce Fields /**
75793a44a75SJ. Bruce Fields  * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
75893a44a75SJ. Bruce Fields  * @parent: dentry of directory to create new "pipe" in
75993a44a75SJ. Bruce Fields  * @name: name of pipe
76093a44a75SJ. Bruce Fields  * @private: private data to associate with the pipe, for the caller's use
76193a44a75SJ. Bruce Fields  * @ops: operations defining the behavior of the pipe: upcall, downcall,
762c3810608S\"J. Bruce Fields\  *	release_pipe, open_pipe, and destroy_msg.
76365b6e42cSRandy Dunlap  * @flags: rpc_inode flags
76493a44a75SJ. Bruce Fields  *
76593a44a75SJ. Bruce Fields  * Data is made available for userspace to read by calls to
76693a44a75SJ. Bruce Fields  * rpc_queue_upcall().  The actual reads will result in calls to
76793a44a75SJ. Bruce Fields  * @ops->upcall, which will be called with the file pointer,
76893a44a75SJ. Bruce Fields  * message, and userspace buffer to copy to.
76993a44a75SJ. Bruce Fields  *
77093a44a75SJ. Bruce Fields  * Writes can come at any time, and do not necessarily have to be
77193a44a75SJ. Bruce Fields  * responses to upcalls.  They will result in calls to @msg->downcall.
77293a44a75SJ. Bruce Fields  *
77393a44a75SJ. Bruce Fields  * The @private argument passed here will be available to all these methods
77493a44a75SJ. Bruce Fields  * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
77593a44a75SJ. Bruce Fields  */
776b693ba4aSTrond Myklebust struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
777b693ba4aSTrond Myklebust 			  void *private, const struct rpc_pipe_ops *ops,
778b693ba4aSTrond Myklebust 			  int flags)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	struct dentry *dentry;
7817589806eSTrond Myklebust 	struct inode *dir = parent->d_inode;
7827364af6aSTrond Myklebust 	umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
783cfeaa4a3STrond Myklebust 	struct qstr q;
7847589806eSTrond Myklebust 	int err;
7857364af6aSTrond Myklebust 
7867364af6aSTrond Myklebust 	if (ops->upcall == NULL)
7877364af6aSTrond Myklebust 		umode &= ~S_IRUGO;
7887364af6aSTrond Myklebust 	if (ops->downcall == NULL)
7897364af6aSTrond Myklebust 		umode &= ~S_IWUGO;
7901da177e4SLinus Torvalds 
791cfeaa4a3STrond Myklebust 	q.name = name;
792cfeaa4a3STrond Myklebust 	q.len = strlen(name);
793cfeaa4a3STrond Myklebust 	q.hash = full_name_hash(q.name, q.len),
794cfeaa4a3STrond Myklebust 
795cfeaa4a3STrond Myklebust 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
7965bff0386SStanislav Kinsbursky 	dentry = __rpc_lookup_create_exclusive(parent, &q);
7971da177e4SLinus Torvalds 	if (IS_ERR(dentry))
798cfeaa4a3STrond Myklebust 		goto out;
7997589806eSTrond Myklebust 	err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
8007589806eSTrond Myklebust 			   private, ops, flags);
8017589806eSTrond Myklebust 	if (err)
8027589806eSTrond Myklebust 		goto out_err;
803f134585aSTrond Myklebust out:
8041b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
8055c3e985aSTrond Myklebust 	return dentry;
8067589806eSTrond Myklebust out_err:
8077589806eSTrond Myklebust 	dentry = ERR_PTR(err);
808158998b6STrond Myklebust 	printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
8090dc47877SHarvey Harrison 			__FILE__, __func__, parent->d_name.name, name,
8107589806eSTrond Myklebust 			err);
811f134585aSTrond Myklebust 	goto out;
8121da177e4SLinus Torvalds }
813468039eeSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_mkpipe);
8141da177e4SLinus Torvalds 
81593a44a75SJ. Bruce Fields /**
81693a44a75SJ. Bruce Fields  * rpc_unlink - remove a pipe
81793a44a75SJ. Bruce Fields  * @dentry: dentry for the pipe, as returned from rpc_mkpipe
81893a44a75SJ. Bruce Fields  *
81993a44a75SJ. Bruce Fields  * After this call, lookups will no longer find the pipe, and any
82093a44a75SJ. Bruce Fields  * attempts to read or write using preexisting opens of the pipe will
82193a44a75SJ. Bruce Fields  * return -EPIPE.
82293a44a75SJ. Bruce Fields  */
823f134585aSTrond Myklebust int
8245d67476fSTrond Myklebust rpc_unlink(struct dentry *dentry)
8251da177e4SLinus Torvalds {
8265d67476fSTrond Myklebust 	struct dentry *parent;
827f134585aSTrond Myklebust 	struct inode *dir;
8285d67476fSTrond Myklebust 	int error = 0;
8291da177e4SLinus Torvalds 
8305d67476fSTrond Myklebust 	parent = dget_parent(dentry);
8315d67476fSTrond Myklebust 	dir = parent->d_inode;
832c6573c29SArjan van de Ven 	mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
833810d90bcSTrond Myklebust 	error = __rpc_rmpipe(dir, dentry);
8341b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
8355d67476fSTrond Myklebust 	dput(parent);
836f134585aSTrond Myklebust 	return error;
8371da177e4SLinus Torvalds }
838468039eeSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_unlink);
8391da177e4SLinus Torvalds 
840e57aed77STrond Myklebust enum {
841e57aed77STrond Myklebust 	RPCAUTH_info,
842e57aed77STrond Myklebust 	RPCAUTH_EOF
843e57aed77STrond Myklebust };
844e57aed77STrond Myklebust 
845e57aed77STrond Myklebust static const struct rpc_filelist authfiles[] = {
846e57aed77STrond Myklebust 	[RPCAUTH_info] = {
847e57aed77STrond Myklebust 		.name = "info",
848e57aed77STrond Myklebust 		.i_fop = &rpc_info_operations,
849e57aed77STrond Myklebust 		.mode = S_IFREG | S_IRUSR,
850e57aed77STrond Myklebust 	},
851e57aed77STrond Myklebust };
852e57aed77STrond Myklebust 
853e57aed77STrond Myklebust static int rpc_clntdir_populate(struct dentry *dentry, void *private)
854e57aed77STrond Myklebust {
855e57aed77STrond Myklebust 	return rpc_populate(dentry,
856e57aed77STrond Myklebust 			    authfiles, RPCAUTH_info, RPCAUTH_EOF,
857e57aed77STrond Myklebust 			    private);
858e57aed77STrond Myklebust }
859e57aed77STrond Myklebust 
860e57aed77STrond Myklebust static void rpc_clntdir_depopulate(struct dentry *dentry)
861e57aed77STrond Myklebust {
862e57aed77STrond Myklebust 	rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
863e57aed77STrond Myklebust }
864e57aed77STrond Myklebust 
8657d59d1e8STrond Myklebust /**
8667d59d1e8STrond Myklebust  * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
8674111d4fdSRandy Dunlap  * @dentry: dentry from the rpc_pipefs root to the new directory
8684111d4fdSRandy Dunlap  * @name: &struct qstr for the name
8697d59d1e8STrond Myklebust  * @rpc_client: rpc client to associate with this directory
8707d59d1e8STrond Myklebust  *
8717d59d1e8STrond Myklebust  * This creates a directory at the given @path associated with
8727d59d1e8STrond Myklebust  * @rpc_clnt, which will contain a file named "info" with some basic
8737d59d1e8STrond Myklebust  * information about the client, together with any "pipes" that may
8747d59d1e8STrond Myklebust  * later be created using rpc_mkpipe().
8757d59d1e8STrond Myklebust  */
87623ac6581STrond Myklebust struct dentry *rpc_create_client_dir(struct dentry *dentry,
87723ac6581STrond Myklebust 				   struct qstr *name,
8787d59d1e8STrond Myklebust 				   struct rpc_clnt *rpc_client)
8797d59d1e8STrond Myklebust {
880e57aed77STrond Myklebust 	return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
881e57aed77STrond Myklebust 			rpc_clntdir_populate, rpc_client);
882e57aed77STrond Myklebust }
883e57aed77STrond Myklebust 
884e57aed77STrond Myklebust /**
885e57aed77STrond Myklebust  * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
886e57aed77STrond Myklebust  * @dentry: directory to remove
887e57aed77STrond Myklebust  */
888e57aed77STrond Myklebust int rpc_remove_client_dir(struct dentry *dentry)
889e57aed77STrond Myklebust {
890e57aed77STrond Myklebust 	return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
8917d59d1e8STrond Myklebust }
8927d59d1e8STrond Myklebust 
8938854e82dSTrond Myklebust static const struct rpc_filelist cache_pipefs_files[3] = {
8948854e82dSTrond Myklebust 	[0] = {
8958854e82dSTrond Myklebust 		.name = "channel",
8968854e82dSTrond Myklebust 		.i_fop = &cache_file_operations_pipefs,
89796c61cbdSTrond Myklebust 		.mode = S_IFREG|S_IRUSR|S_IWUSR,
8988854e82dSTrond Myklebust 	},
8998854e82dSTrond Myklebust 	[1] = {
9008854e82dSTrond Myklebust 		.name = "content",
9018854e82dSTrond Myklebust 		.i_fop = &content_file_operations_pipefs,
9028854e82dSTrond Myklebust 		.mode = S_IFREG|S_IRUSR,
9038854e82dSTrond Myklebust 	},
9048854e82dSTrond Myklebust 	[2] = {
9058854e82dSTrond Myklebust 		.name = "flush",
9068854e82dSTrond Myklebust 		.i_fop = &cache_flush_operations_pipefs,
9078854e82dSTrond Myklebust 		.mode = S_IFREG|S_IRUSR|S_IWUSR,
9088854e82dSTrond Myklebust 	},
9098854e82dSTrond Myklebust };
9108854e82dSTrond Myklebust 
9118854e82dSTrond Myklebust static int rpc_cachedir_populate(struct dentry *dentry, void *private)
9128854e82dSTrond Myklebust {
9138854e82dSTrond Myklebust 	return rpc_populate(dentry,
9148854e82dSTrond Myklebust 			    cache_pipefs_files, 0, 3,
9158854e82dSTrond Myklebust 			    private);
9168854e82dSTrond Myklebust }
9178854e82dSTrond Myklebust 
9188854e82dSTrond Myklebust static void rpc_cachedir_depopulate(struct dentry *dentry)
9198854e82dSTrond Myklebust {
9208854e82dSTrond Myklebust 	rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
9218854e82dSTrond Myklebust }
9228854e82dSTrond Myklebust 
9238854e82dSTrond Myklebust struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
92464f1426fSAl Viro 				    umode_t umode, struct cache_detail *cd)
9258854e82dSTrond Myklebust {
9268854e82dSTrond Myklebust 	return rpc_mkdir_populate(parent, name, umode, NULL,
9278854e82dSTrond Myklebust 			rpc_cachedir_populate, cd);
9288854e82dSTrond Myklebust }
9298854e82dSTrond Myklebust 
9308854e82dSTrond Myklebust void rpc_remove_cache_dir(struct dentry *dentry)
9318854e82dSTrond Myklebust {
9328854e82dSTrond Myklebust 	rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
9338854e82dSTrond Myklebust }
9348854e82dSTrond Myklebust 
9351da177e4SLinus Torvalds /*
9361da177e4SLinus Torvalds  * populate the filesystem
9371da177e4SLinus Torvalds  */
938b87221deSAlexey Dobriyan static const struct super_operations s_ops = {
9391da177e4SLinus Torvalds 	.alloc_inode	= rpc_alloc_inode,
9401da177e4SLinus Torvalds 	.destroy_inode	= rpc_destroy_inode,
9411da177e4SLinus Torvalds 	.statfs		= simple_statfs,
9421da177e4SLinus Torvalds };
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds #define RPCAUTH_GSSMAGIC 0x67596969
9451da177e4SLinus Torvalds 
946bb156749STrond Myklebust /*
947bb156749STrond Myklebust  * We have a single directory with 1 node in it.
948bb156749STrond Myklebust  */
949bb156749STrond Myklebust enum {
950bb156749STrond Myklebust 	RPCAUTH_lockd,
951bb156749STrond Myklebust 	RPCAUTH_mount,
952bb156749STrond Myklebust 	RPCAUTH_nfs,
953bb156749STrond Myklebust 	RPCAUTH_portmap,
954bb156749STrond Myklebust 	RPCAUTH_statd,
955bb156749STrond Myklebust 	RPCAUTH_nfsd4_cb,
956e571cbf1STrond Myklebust 	RPCAUTH_cache,
957bb156749STrond Myklebust 	RPCAUTH_RootEOF
958bb156749STrond Myklebust };
959bb156749STrond Myklebust 
960bb156749STrond Myklebust static const struct rpc_filelist files[] = {
961bb156749STrond Myklebust 	[RPCAUTH_lockd] = {
962bb156749STrond Myklebust 		.name = "lockd",
963bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
964bb156749STrond Myklebust 	},
965bb156749STrond Myklebust 	[RPCAUTH_mount] = {
966bb156749STrond Myklebust 		.name = "mount",
967bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
968bb156749STrond Myklebust 	},
969bb156749STrond Myklebust 	[RPCAUTH_nfs] = {
970bb156749STrond Myklebust 		.name = "nfs",
971bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
972bb156749STrond Myklebust 	},
973bb156749STrond Myklebust 	[RPCAUTH_portmap] = {
974bb156749STrond Myklebust 		.name = "portmap",
975bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
976bb156749STrond Myklebust 	},
977bb156749STrond Myklebust 	[RPCAUTH_statd] = {
978bb156749STrond Myklebust 		.name = "statd",
979bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
980bb156749STrond Myklebust 	},
981bb156749STrond Myklebust 	[RPCAUTH_nfsd4_cb] = {
982bb156749STrond Myklebust 		.name = "nfsd4_cb",
983bb156749STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
984bb156749STrond Myklebust 	},
985e571cbf1STrond Myklebust 	[RPCAUTH_cache] = {
986e571cbf1STrond Myklebust 		.name = "cache",
987e571cbf1STrond Myklebust 		.mode = S_IFDIR | S_IRUGO | S_IXUGO,
988e571cbf1STrond Myklebust 	},
989bb156749STrond Myklebust };
990bb156749STrond Myklebust 
9911da177e4SLinus Torvalds static int
9921da177e4SLinus Torvalds rpc_fill_super(struct super_block *sb, void *data, int silent)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	struct inode *inode;
9951da177e4SLinus Torvalds 	struct dentry *root;
99638b0da75SStanislav Kinsbursky 	struct net *net = data;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	sb->s_blocksize = PAGE_CACHE_SIZE;
9991da177e4SLinus Torvalds 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
10001da177e4SLinus Torvalds 	sb->s_magic = RPCAUTH_GSSMAGIC;
10011da177e4SLinus Torvalds 	sb->s_op = &s_ops;
10021da177e4SLinus Torvalds 	sb->s_time_gran = 1;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	inode = rpc_get_inode(sb, S_IFDIR | 0755);
10051da177e4SLinus Torvalds 	if (!inode)
10061da177e4SLinus Torvalds 		return -ENOMEM;
1007fc7bed8cSAl Viro 	sb->s_root = root = d_alloc_root(inode);
10081da177e4SLinus Torvalds 	if (!root) {
10091da177e4SLinus Torvalds 		iput(inode);
10101da177e4SLinus Torvalds 		return -ENOMEM;
10111da177e4SLinus Torvalds 	}
1012ac6feceeSTrond Myklebust 	if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
10131da177e4SLinus Torvalds 		return -ENOMEM;
1014fc7bed8cSAl Viro 	return 0;
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
1017fc14f2feSAl Viro static struct dentry *
1018fc14f2feSAl Viro rpc_mount(struct file_system_type *fs_type,
1019fc14f2feSAl Viro 		int flags, const char *dev_name, void *data)
10201da177e4SLinus Torvalds {
102138b0da75SStanislav Kinsbursky 	return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds static struct file_system_type rpc_pipe_fs_type = {
10251da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
10261da177e4SLinus Torvalds 	.name		= "rpc_pipefs",
1027fc14f2feSAl Viro 	.mount		= rpc_mount,
10281da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
10291da177e4SLinus Torvalds };
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds static void
103251cc5068SAlexey Dobriyan init_once(void *foo)
10331da177e4SLinus Torvalds {
10341da177e4SLinus Torvalds 	struct rpc_inode *rpci = (struct rpc_inode *) foo;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	inode_init_once(&rpci->vfs_inode);
10371da177e4SLinus Torvalds 	rpci->private = NULL;
10381da177e4SLinus Torvalds 	rpci->nreaders = 0;
10391da177e4SLinus Torvalds 	rpci->nwriters = 0;
10401da177e4SLinus Torvalds 	INIT_LIST_HEAD(&rpci->in_upcall);
10416e84c7b6STrond Myklebust 	INIT_LIST_HEAD(&rpci->in_downcall);
10421da177e4SLinus Torvalds 	INIT_LIST_HEAD(&rpci->pipe);
10431da177e4SLinus Torvalds 	rpci->pipelen = 0;
10441da177e4SLinus Torvalds 	init_waitqueue_head(&rpci->waitq);
104552bad64dSDavid Howells 	INIT_DELAYED_WORK(&rpci->queue_timeout,
104665f27f38SDavid Howells 			    rpc_timeout_upcall_queue);
10471da177e4SLinus Torvalds 	rpci->ops = NULL;
10481da177e4SLinus Torvalds }
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds int register_rpc_pipefs(void)
10511da177e4SLinus Torvalds {
10525bd5f581SAkinobu Mita 	int err;
10535bd5f581SAkinobu Mita 
10541da177e4SLinus Torvalds 	rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
10551da177e4SLinus Torvalds 				sizeof(struct rpc_inode),
1056fffb60f9SPaul Jackson 				0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1057fffb60f9SPaul Jackson 						SLAB_MEM_SPREAD),
105820c2df83SPaul Mundt 				init_once);
10591da177e4SLinus Torvalds 	if (!rpc_inode_cachep)
10601da177e4SLinus Torvalds 		return -ENOMEM;
10615bd5f581SAkinobu Mita 	err = register_filesystem(&rpc_pipe_fs_type);
10625bd5f581SAkinobu Mita 	if (err) {
10635bd5f581SAkinobu Mita 		kmem_cache_destroy(rpc_inode_cachep);
10645bd5f581SAkinobu Mita 		return err;
10655bd5f581SAkinobu Mita 	}
10665bd5f581SAkinobu Mita 
10671da177e4SLinus Torvalds 	return 0;
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds void unregister_rpc_pipefs(void)
10711da177e4SLinus Torvalds {
10721a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(rpc_inode_cachep);
10731da177e4SLinus Torvalds 	unregister_filesystem(&rpc_pipe_fs_type);
10741da177e4SLinus Torvalds }
1075dcbf8c30SMichal Schmidt 
1076dcbf8c30SMichal Schmidt /* Make 'mount -t rpc_pipefs ...' autoload this module. */
1077dcbf8c30SMichal Schmidt MODULE_ALIAS("rpc_pipefs");
1078