blob: b6f6555128db31f75a1e024f7f9b7d9c283f3295 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sunrpc/rpc_pipe.c
3 *
4 * Userland/kernel interface for rpcauth_gss.
5 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
Rolf Eike Beerd51fe1b2005-09-02 08:59:25 +02006 * and fs/sysfs/inode.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
9 *
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/string.h>
14#include <linux/pagemap.h>
15#include <linux/mount.h>
16#include <linux/namei.h>
Trond Myklebust50e437d2007-06-07 22:44:34 -040017#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19
20#include <asm/ioctls.h>
21#include <linux/fs.h>
22#include <linux/poll.h>
23#include <linux/wait.h>
24#include <linux/seq_file.h>
25
26#include <linux/sunrpc/clnt.h>
27#include <linux/workqueue.h>
28#include <linux/sunrpc/rpc_pipe_fs.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040029#include <linux/sunrpc/cache.h>
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +030030#include <linux/nsproxy.h>
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030031#include <linux/notifier.h>
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +030032
33#include "netns.h"
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030034#include "sunrpc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +030036#define RPCDBG_FACILITY RPCDBG_DEBUG
37
38#define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
39
Al Virofc14f2f2010-07-25 01:48:30 +040040static struct vfsmount *rpc_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int rpc_mount_count;
42
43static struct file_system_type rpc_pipe_fs_type;
44
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define RPC_UPCALL_TIMEOUT (30*HZ)
49
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030050static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
51
52int rpc_pipefs_notifier_register(struct notifier_block *nb)
53{
54 return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
55}
56EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
57
58void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
59{
60 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
61}
62EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
63
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030064static void rpc_purge_list(struct rpc_pipe *pipe, struct list_head *head,
Trond Myklebust9842ef32006-02-01 12:18:44 -050065 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050066{
67 struct rpc_pipe_msg *msg;
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050068
Trond Myklebust9842ef32006-02-01 12:18:44 -050069 if (list_empty(head))
70 return;
71 do {
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050072 msg = list_entry(head->next, struct rpc_pipe_msg, list);
Trond Myklebust5a676572010-09-12 19:55:25 -040073 list_del_init(&msg->list);
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050074 msg->errno = err;
75 destroy_msg(msg);
Trond Myklebust9842ef32006-02-01 12:18:44 -050076 } while (!list_empty(head));
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030077 wake_up(&pipe->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static void
David Howells65f27f32006-11-22 14:55:48 +000081rpc_timeout_upcall_queue(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Trond Myklebust9842ef32006-02-01 12:18:44 -050083 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030084 struct rpc_pipe *pipe =
85 container_of(work, struct rpc_pipe, queue_timeout.work);
Trond Myklebust9842ef32006-02-01 12:18:44 -050086 void (*destroy_msg)(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030088 spin_lock(&pipe->lock);
89 if (pipe->ops == NULL) {
90 spin_unlock(&pipe->lock);
Trond Myklebust9842ef32006-02-01 12:18:44 -050091 return;
92 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030093 destroy_msg = pipe->ops->destroy_msg;
94 if (pipe->nreaders == 0) {
95 list_splice_init(&pipe->pipe, &free_list);
96 pipe->pipelen = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -050097 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030098 spin_unlock(&pipe->lock);
99 rpc_purge_list(pipe, &free_list, destroy_msg, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Peng Taoc1225152011-09-22 21:50:10 -0400102ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
103 char __user *dst, size_t buflen)
104{
105 char *data = (char *)msg->data + msg->copied;
106 size_t mlen = min(msg->len - msg->copied, buflen);
107 unsigned long left;
108
109 left = copy_to_user(dst, data, mlen);
110 if (left == mlen) {
111 msg->errno = -EFAULT;
112 return -EFAULT;
113 }
114
115 mlen -= left;
116 msg->copied += mlen;
117 msg->errno = 0;
118 return mlen;
119}
120EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
121
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500122/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -0800123 * rpc_queue_upcall - queue an upcall message to userspace
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500124 * @inode: inode of upcall pipe on which to queue given message
125 * @msg: message to queue
126 *
127 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
128 * A userspace process may then later read the upcall by performing a
129 * read on an open file for this inode. It is up to the caller to
130 * initialize the fields of @msg (other than @msg->list) appropriately.
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132int
133rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
134{
135 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400136 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300138 spin_lock(&rpci->pipe->lock);
139 if (rpci->pipe->ops == NULL)
Trond Myklebust6070fe62005-10-27 22:12:46 -0400140 goto out;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300141 if (rpci->pipe->nreaders) {
142 list_add_tail(&msg->list, &rpci->pipe->pipe);
143 rpci->pipe->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400144 res = 0;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300145 } else if (rpci->pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
146 if (list_empty(&rpci->pipe->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -0500147 queue_delayed_work(rpciod_workqueue,
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300148 &rpci->pipe->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 RPC_UPCALL_TIMEOUT);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300150 list_add_tail(&msg->list, &rpci->pipe->pipe);
151 rpci->pipe->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400152 res = 0;
153 }
154out:
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300155 spin_unlock(&rpci->pipe->lock);
156 wake_up(&rpci->pipe->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return res;
158}
Trond Myklebust468039e2008-12-23 15:21:31 -0500159EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Trond Myklebust6070fe62005-10-27 22:12:46 -0400161static inline void
162rpc_inode_setowner(struct inode *inode, void *private)
163{
164 RPC_I(inode)->private = private;
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void
168rpc_close_pipes(struct inode *inode)
169{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300170 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Trond Myklebustb693ba42009-08-09 15:14:15 -0400171 const struct rpc_pipe_ops *ops;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500172 int need_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800174 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300175 ops = pipe->ops;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500176 if (ops != NULL) {
177 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300178 spin_lock(&pipe->lock);
179 need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
180 pipe->nreaders = 0;
181 list_splice_init(&pipe->in_upcall, &free_list);
182 list_splice_init(&pipe->pipe, &free_list);
183 pipe->pipelen = 0;
184 pipe->ops = NULL;
185 spin_unlock(&pipe->lock);
186 rpc_purge_list(pipe, &free_list, ops->destroy_msg, -EPIPE);
187 pipe->nwriters = 0;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500188 if (need_release && ops->release_pipe)
Trond Myklebust9842ef32006-02-01 12:18:44 -0500189 ops->release_pipe(inode);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300190 cancel_delayed_work_sync(&pipe->queue_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400192 rpc_inode_setowner(inode, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800193 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static struct inode *
197rpc_alloc_inode(struct super_block *sb)
198{
199 struct rpc_inode *rpci;
Christoph Lametere94b1762006-12-06 20:33:17 -0800200 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (!rpci)
202 return NULL;
203 return &rpci->vfs_inode;
204}
205
206static void
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100207rpc_i_callback(struct rcu_head *head)
208{
209 struct inode *inode = container_of(head, struct inode, i_rcu);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300210 kfree(RPC_I(inode)->pipe);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100211 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
212}
213
214static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215rpc_destroy_inode(struct inode *inode)
216{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100217 call_rcu(&inode->i_rcu, rpc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220static int
221rpc_pipe_open(struct inode *inode, struct file *filp)
222{
223 struct rpc_inode *rpci = RPC_I(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500224 int first_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int res = -ENXIO;
226
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800227 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300228 if (rpci->pipe->ops == NULL)
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500229 goto out;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300230 first_open = rpci->pipe->nreaders == 0 && rpci->pipe->nwriters == 0;
231 if (first_open && rpci->pipe->ops->open_pipe) {
232 res = rpci->pipe->ops->open_pipe(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500233 if (res)
234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500236 if (filp->f_mode & FMODE_READ)
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300237 rpci->pipe->nreaders++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500238 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300239 rpci->pipe->nwriters++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500240 res = 0;
241out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800242 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return res;
244}
245
246static int
247rpc_pipe_release(struct inode *inode, struct file *filp)
248{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300249 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct rpc_pipe_msg *msg;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500251 int last_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800253 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300254 if (pipe->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 goto out;
Joe Perches655b5bb2010-09-04 18:52:53 -0700256 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (msg != NULL) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300258 spin_lock(&pipe->lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500259 msg->errno = -EAGAIN;
Trond Myklebust5a676572010-09-12 19:55:25 -0400260 list_del_init(&msg->list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300261 spin_unlock(&pipe->lock);
262 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300265 pipe->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500266 if (filp->f_mode & FMODE_READ) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300267 pipe->nreaders --;
268 if (pipe->nreaders == 0) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500269 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300270 spin_lock(&pipe->lock);
271 list_splice_init(&pipe->pipe, &free_list);
272 pipe->pipelen = 0;
273 spin_unlock(&pipe->lock);
274 rpc_purge_list(pipe, &free_list,
275 pipe->ops->destroy_msg, -EAGAIN);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500276 }
277 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300278 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
279 if (last_close && pipe->ops->release_pipe)
280 pipe->ops->release_pipe(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800282 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284}
285
286static ssize_t
287rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
288{
Josef Sipek303b46b2006-12-08 02:37:42 -0800289 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct rpc_inode *rpci = RPC_I(inode);
291 struct rpc_pipe_msg *msg;
292 int res = 0;
293
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800294 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300295 if (rpci->pipe->ops == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 res = -EPIPE;
297 goto out_unlock;
298 }
299 msg = filp->private_data;
300 if (msg == NULL) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300301 spin_lock(&rpci->pipe->lock);
302 if (!list_empty(&rpci->pipe->pipe)) {
303 msg = list_entry(rpci->pipe->pipe.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct rpc_pipe_msg,
305 list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300306 list_move(&msg->list, &rpci->pipe->in_upcall);
307 rpci->pipe->pipelen -= msg->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 filp->private_data = msg;
309 msg->copied = 0;
310 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300311 spin_unlock(&rpci->pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (msg == NULL)
313 goto out_unlock;
314 }
315 /* NOTE: it is up to the callback to update msg->copied */
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300316 res = rpci->pipe->ops->upcall(filp, msg, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (res < 0 || msg->len == msg->copied) {
318 filp->private_data = NULL;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300319 spin_lock(&rpci->pipe->lock);
Trond Myklebust5a676572010-09-12 19:55:25 -0400320 list_del_init(&msg->list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300321 spin_unlock(&rpci->pipe->lock);
322 rpci->pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800325 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return res;
327}
328
329static ssize_t
330rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
331{
Josef Sipek303b46b2006-12-08 02:37:42 -0800332 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct rpc_inode *rpci = RPC_I(inode);
334 int res;
335
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800336 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 res = -EPIPE;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300338 if (rpci->pipe->ops != NULL)
339 res = rpci->pipe->ops->downcall(filp, buf, len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800340 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return res;
342}
343
344static unsigned int
345rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
346{
347 struct rpc_inode *rpci;
348 unsigned int mask = 0;
349
Josef Sipek303b46b2006-12-08 02:37:42 -0800350 rpci = RPC_I(filp->f_path.dentry->d_inode);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300351 poll_wait(filp, &rpci->pipe->waitq, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 mask = POLLOUT | POLLWRNORM;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300354 if (rpci->pipe->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 mask |= POLLERR | POLLHUP;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300356 if (filp->private_data || !list_empty(&rpci->pipe->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 mask |= POLLIN | POLLRDNORM;
358 return mask;
359}
360
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200361static long
362rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200364 struct inode *inode = filp->f_path.dentry->d_inode;
365 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int len;
367
368 switch (cmd) {
369 case FIONREAD:
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300370 spin_lock(&rpci->pipe->lock);
371 if (rpci->pipe->ops == NULL) {
372 spin_unlock(&rpci->pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -EPIPE;
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200374 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300375 len = rpci->pipe->pipelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (filp->private_data) {
377 struct rpc_pipe_msg *msg;
Joe Perches655b5bb2010-09-04 18:52:53 -0700378 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 len += msg->len - msg->copied;
380 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300381 spin_unlock(&rpci->pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return put_user(len, (int __user *)arg);
383 default:
384 return -EINVAL;
385 }
386}
387
Arjan van de Venda7071d2007-02-12 00:55:36 -0800388static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 .owner = THIS_MODULE,
390 .llseek = no_llseek,
391 .read = rpc_pipe_read,
392 .write = rpc_pipe_write,
393 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200394 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 .open = rpc_pipe_open,
396 .release = rpc_pipe_release,
397};
398
399static int
400rpc_show_info(struct seq_file *m, void *v)
401{
402 struct rpc_clnt *clnt = m->private;
403
404 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
405 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
406 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400407 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
408 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400409 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
412
413static int
414rpc_info_open(struct inode *inode, struct file *file)
415{
Trond Myklebust006abe82010-09-12 19:55:25 -0400416 struct rpc_clnt *clnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 int ret = single_open(file, rpc_show_info, NULL);
418
419 if (!ret) {
420 struct seq_file *m = file->private_data;
Trond Myklebust006abe82010-09-12 19:55:25 -0400421
422 spin_lock(&file->f_path.dentry->d_lock);
423 if (!d_unhashed(file->f_path.dentry))
424 clnt = RPC_I(inode)->private;
425 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
426 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 m->private = clnt;
428 } else {
Trond Myklebust006abe82010-09-12 19:55:25 -0400429 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 single_release(inode, file);
431 ret = -EINVAL;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 return ret;
435}
436
437static int
438rpc_info_release(struct inode *inode, struct file *file)
439{
440 struct seq_file *m = file->private_data;
441 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
442
443 if (clnt)
444 rpc_release_client(clnt);
445 return single_release(inode, file);
446}
447
Arjan van de Venda7071d2007-02-12 00:55:36 -0800448static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 .owner = THIS_MODULE,
450 .open = rpc_info_open,
451 .read = seq_read,
452 .llseek = seq_lseek,
453 .release = rpc_info_release,
454};
455
456
457/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * Description of fs contents.
459 */
460struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400461 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800462 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400463 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464};
465
Trond Myklebust54281542006-03-20 13:44:49 -0500466struct vfsmount *rpc_get_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Trond Myklebust54281542006-03-20 13:44:49 -0500468 int err;
469
Al Virofc14f2f2010-07-25 01:48:30 +0400470 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
Trond Myklebust54281542006-03-20 13:44:49 -0500471 if (err != 0)
472 return ERR_PTR(err);
Al Virofc14f2f2010-07-25 01:48:30 +0400473 return rpc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400475EXPORT_SYMBOL_GPL(rpc_get_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Trond Myklebust54281542006-03-20 13:44:49 -0500477void rpc_put_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Al Virofc14f2f2010-07-25 01:48:30 +0400479 simple_release_fs(&rpc_mnt, &rpc_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400481EXPORT_SYMBOL_GPL(rpc_put_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Nick Pigginfe15ce42011-01-07 17:49:23 +1100483static int rpc_delete_dentry(const struct dentry *dentry)
Trond Myklebust62e17612007-06-08 14:14:46 -0400484{
485 return 1;
486}
487
Al Viro3ba13d12009-02-20 06:02:22 +0000488static const struct dentry_operations rpc_dentry_operations = {
Trond Myklebust62e17612007-06-08 14:14:46 -0400489 .d_delete = rpc_delete_dentry,
490};
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400493rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct inode *inode = new_inode(sb);
496 if (!inode)
497 return NULL;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400498 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Joe Perches89f0e4f2011-07-01 09:43:12 +0000501 switch (mode & S_IFMT) {
502 case S_IFDIR:
503 inode->i_fop = &simple_dir_operations;
504 inode->i_op = &simple_dir_inode_operations;
505 inc_nlink(inode);
506 default:
507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 return inode;
510}
511
Trond Myklebust75898062009-08-09 15:14:17 -0400512static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
513 umode_t mode,
514 const struct file_operations *i_fop,
515 void *private)
516{
517 struct inode *inode;
518
Trond Myklebustbeb0f0a2010-12-20 21:19:26 +0000519 d_drop(dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400520 inode = rpc_get_inode(dir->i_sb, mode);
521 if (!inode)
522 goto out_err;
523 inode->i_ino = iunique(dir->i_sb, 100);
524 if (i_fop)
525 inode->i_fop = i_fop;
526 if (private)
527 rpc_inode_setowner(inode, private);
528 d_add(dentry, inode);
529 return 0;
530out_err:
531 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
532 __FILE__, __func__, dentry->d_name.name);
533 dput(dentry);
534 return -ENOMEM;
535}
536
Trond Myklebustac6fece2009-08-09 15:14:20 -0400537static int __rpc_create(struct inode *dir, struct dentry *dentry,
538 umode_t mode,
539 const struct file_operations *i_fop,
540 void *private)
541{
542 int err;
543
544 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
545 if (err)
546 return err;
547 fsnotify_create(dir, dentry);
548 return 0;
549}
550
Trond Myklebust75898062009-08-09 15:14:17 -0400551static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
552 umode_t mode,
553 const struct file_operations *i_fop,
554 void *private)
555{
556 int err;
557
558 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
559 if (err)
560 return err;
561 inc_nlink(dir);
562 fsnotify_mkdir(dir, dentry);
563 return 0;
564}
565
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300566static void
567init_pipe(struct rpc_pipe *pipe)
568{
569 pipe->nreaders = 0;
570 pipe->nwriters = 0;
571 INIT_LIST_HEAD(&pipe->in_upcall);
572 INIT_LIST_HEAD(&pipe->in_downcall);
573 INIT_LIST_HEAD(&pipe->pipe);
574 pipe->pipelen = 0;
575 init_waitqueue_head(&pipe->waitq);
576 INIT_DELAYED_WORK(&pipe->queue_timeout,
577 rpc_timeout_upcall_queue);
578 pipe->ops = NULL;
579 spin_lock_init(&pipe->lock);
580
581}
582
Trond Myklebust75898062009-08-09 15:14:17 -0400583static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
584 umode_t mode,
585 const struct file_operations *i_fop,
586 void *private,
587 const struct rpc_pipe_ops *ops,
588 int flags)
589{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300590 struct rpc_pipe *pipe;
Trond Myklebust75898062009-08-09 15:14:17 -0400591 struct rpc_inode *rpci;
592 int err;
593
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300594 pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
595 if (!pipe)
596 return -ENOMEM;
597 init_pipe(pipe);
Trond Myklebust75898062009-08-09 15:14:17 -0400598 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300599 if (err) {
600 kfree(pipe);
Trond Myklebust75898062009-08-09 15:14:17 -0400601 return err;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300602 }
Trond Myklebust75898062009-08-09 15:14:17 -0400603 rpci = RPC_I(dentry->d_inode);
Trond Myklebust75898062009-08-09 15:14:17 -0400604 rpci->private = private;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300605 rpci->pipe = pipe;
606 rpci->pipe->flags = flags;
607 rpci->pipe->ops = ops;
Trond Myklebust75898062009-08-09 15:14:17 -0400608 fsnotify_create(dir, dentry);
609 return 0;
610}
611
Trond Myklebustac6fece2009-08-09 15:14:20 -0400612static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
613{
614 int ret;
615
616 dget(dentry);
617 ret = simple_rmdir(dir, dentry);
618 d_delete(dentry);
619 dput(dentry);
620 return ret;
621}
622
Trond Myklebust810d90b2009-08-09 15:14:18 -0400623static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
624{
625 int ret;
626
627 dget(dentry);
628 ret = simple_unlink(dir, dentry);
629 d_delete(dentry);
630 dput(dentry);
631 return ret;
632}
633
634static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
635{
636 struct inode *inode = dentry->d_inode;
Trond Myklebust810d90b2009-08-09 15:14:18 -0400637
Trond Myklebust810d90b2009-08-09 15:14:18 -0400638 rpc_close_pipes(inode);
639 return __rpc_unlink(dir, dentry);
640}
641
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400642static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
643 struct qstr *name)
644{
645 struct dentry *dentry;
646
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300647 dentry = d_lookup(parent, name);
648 if (!dentry) {
649 dentry = d_alloc(parent, name);
650 if (!dentry)
651 return ERR_PTR(-ENOMEM);
652 }
653 if (dentry->d_inode == NULL) {
654 d_set_d_op(dentry, &rpc_dentry_operations);
Dan Carpenterf1f0abe2010-03-21 12:10:34 -0400655 return dentry;
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300656 }
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400657 dput(dentry);
658 return ERR_PTR(-EEXIST);
659}
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661/*
662 * FIXME: This probably has races.
663 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400664static void __rpc_depopulate(struct dentry *parent,
665 const struct rpc_filelist *files,
666 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct inode *dir = parent->d_inode;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400669 struct dentry *dentry;
670 struct qstr name;
671 int i;
672
673 for (i = start; i < eof; i++) {
674 name.name = files[i].name;
675 name.len = strlen(files[i].name);
676 name.hash = full_name_hash(name.name, name.len);
677 dentry = d_lookup(parent, &name);
678
679 if (dentry == NULL)
680 continue;
681 if (dentry->d_inode == NULL)
682 goto next;
683 switch (dentry->d_inode->i_mode & S_IFMT) {
684 default:
685 BUG();
686 case S_IFREG:
687 __rpc_unlink(dir, dentry);
688 break;
689 case S_IFDIR:
690 __rpc_rmdir(dir, dentry);
691 }
692next:
693 dput(dentry);
694 }
695}
696
697static void rpc_depopulate(struct dentry *parent,
698 const struct rpc_filelist *files,
699 int start, int eof)
700{
701 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Arjan van de Venc6573c22006-07-03 00:25:16 -0700703 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400704 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800705 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Trond Myklebustac6fece2009-08-09 15:14:20 -0400708static int rpc_populate(struct dentry *parent,
709 const struct rpc_filelist *files,
710 int start, int eof,
711 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Trond Myklebustac6fece2009-08-09 15:14:20 -0400713 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400715 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800717 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 for (i = start; i < eof; i++) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400719 struct qstr q;
720
721 q.name = files[i].name;
722 q.len = strlen(files[i].name);
723 q.hash = full_name_hash(q.name, q.len);
724 dentry = __rpc_lookup_create_exclusive(parent, &q);
725 err = PTR_ERR(dentry);
726 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400728 switch (files[i].mode & S_IFMT) {
729 default:
730 BUG();
731 case S_IFREG:
732 err = __rpc_create(dir, dentry,
733 files[i].mode,
734 files[i].i_fop,
735 private);
736 break;
737 case S_IFDIR:
738 err = __rpc_mkdir(dir, dentry,
739 files[i].mode,
740 NULL,
741 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400743 if (err != 0)
744 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800746 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return 0;
748out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400749 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800750 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800752 __FILE__, __func__, parent->d_name.name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400753 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400754}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100755
Trond Myklebuste57aed72009-08-09 15:14:26 -0400756static struct dentry *rpc_mkdir_populate(struct dentry *parent,
757 struct qstr *name, umode_t mode, void *private,
758 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400759{
Trond Myklebustf1345852005-09-23 11:08:25 -0400760 struct dentry *dentry;
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400761 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400762 int error;
763
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400764 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
765 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400766 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400767 goto out;
768 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400769 if (error != 0)
770 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400771 if (populate != NULL) {
772 error = populate(dentry, args_populate);
773 if (error)
774 goto err_rmdir;
775 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400776out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800777 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400778 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400779err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400780 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400781out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400782 dentry = ERR_PTR(error);
783 goto out;
784}
785
Trond Myklebuste57aed72009-08-09 15:14:26 -0400786static int rpc_rmdir_depopulate(struct dentry *dentry,
787 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400788{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700789 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400790 struct inode *dir;
791 int error;
792
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700793 parent = dget_parent(dentry);
794 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700795 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400796 if (depopulate != NULL)
797 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400798 error = __rpc_rmdir(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800799 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700800 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400801 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500804/**
805 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
806 * @parent: dentry of directory to create new "pipe" in
807 * @name: name of pipe
808 * @private: private data to associate with the pipe, for the caller's use
809 * @ops: operations defining the behavior of the pipe: upcall, downcall,
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500810 * release_pipe, open_pipe, and destroy_msg.
Randy Dunlap65b6e422008-02-13 15:03:23 -0800811 * @flags: rpc_inode flags
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500812 *
813 * Data is made available for userspace to read by calls to
814 * rpc_queue_upcall(). The actual reads will result in calls to
815 * @ops->upcall, which will be called with the file pointer,
816 * message, and userspace buffer to copy to.
817 *
818 * Writes can come at any time, and do not necessarily have to be
819 * responses to upcalls. They will result in calls to @msg->downcall.
820 *
821 * The @private argument passed here will be available to all these methods
822 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
823 */
Trond Myklebustb693ba42009-08-09 15:14:15 -0400824struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
825 void *private, const struct rpc_pipe_ops *ops,
826 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 struct dentry *dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400829 struct inode *dir = parent->d_inode;
Trond Myklebust7364af62009-08-09 15:14:16 -0400830 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400831 struct qstr q;
Trond Myklebust75898062009-08-09 15:14:17 -0400832 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400833
834 if (ops->upcall == NULL)
835 umode &= ~S_IRUGO;
836 if (ops->downcall == NULL)
837 umode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400839 q.name = name;
840 q.len = strlen(name);
841 q.hash = full_name_hash(q.name, q.len),
842
843 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300844 dentry = __rpc_lookup_create_exclusive(parent, &q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400846 goto out;
Trond Myklebust75898062009-08-09 15:14:17 -0400847 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
Trond Myklebust810d90b2009-08-09 15:14:18 -0400848 private, ops, flags);
Trond Myklebust75898062009-08-09 15:14:17 -0400849 if (err)
850 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400851out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800852 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400853 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400854out_err:
855 dentry = ERR_PTR(err);
Trond Myklebust158998b2006-08-24 01:03:17 -0400856 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800857 __FILE__, __func__, parent->d_name.name, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400858 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400859 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
Trond Myklebust468039e2008-12-23 15:21:31 -0500861EXPORT_SYMBOL_GPL(rpc_mkpipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500863/**
864 * rpc_unlink - remove a pipe
865 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
866 *
867 * After this call, lookups will no longer find the pipe, and any
868 * attempts to read or write using preexisting opens of the pipe will
869 * return -EPIPE.
870 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400871int
Trond Myklebust5d674762006-07-31 14:11:48 -0700872rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Trond Myklebust5d674762006-07-31 14:11:48 -0700874 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400875 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700876 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Trond Myklebust5d674762006-07-31 14:11:48 -0700878 parent = dget_parent(dentry);
879 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700880 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400881 error = __rpc_rmpipe(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800882 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700883 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400884 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Trond Myklebust468039e2008-12-23 15:21:31 -0500886EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Trond Myklebuste57aed72009-08-09 15:14:26 -0400888enum {
889 RPCAUTH_info,
890 RPCAUTH_EOF
891};
892
893static const struct rpc_filelist authfiles[] = {
894 [RPCAUTH_info] = {
895 .name = "info",
896 .i_fop = &rpc_info_operations,
897 .mode = S_IFREG | S_IRUSR,
898 },
899};
900
901static int rpc_clntdir_populate(struct dentry *dentry, void *private)
902{
903 return rpc_populate(dentry,
904 authfiles, RPCAUTH_info, RPCAUTH_EOF,
905 private);
906}
907
908static void rpc_clntdir_depopulate(struct dentry *dentry)
909{
910 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
911}
912
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400913/**
914 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Randy Dunlap4111d4f2009-09-23 14:36:38 -0400915 * @dentry: dentry from the rpc_pipefs root to the new directory
916 * @name: &struct qstr for the name
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400917 * @rpc_client: rpc client to associate with this directory
918 *
919 * This creates a directory at the given @path associated with
920 * @rpc_clnt, which will contain a file named "info" with some basic
921 * information about the client, together with any "pipes" that may
922 * later be created using rpc_mkpipe().
923 */
Trond Myklebust23ac6582009-08-09 15:14:25 -0400924struct dentry *rpc_create_client_dir(struct dentry *dentry,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400925 struct qstr *name,
926 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400927{
Trond Myklebuste57aed72009-08-09 15:14:26 -0400928 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
929 rpc_clntdir_populate, rpc_client);
930}
931
932/**
933 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
934 * @dentry: directory to remove
935 */
936int rpc_remove_client_dir(struct dentry *dentry)
937{
938 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400939}
940
Trond Myklebust8854e822009-08-09 15:14:30 -0400941static const struct rpc_filelist cache_pipefs_files[3] = {
942 [0] = {
943 .name = "channel",
944 .i_fop = &cache_file_operations_pipefs,
Trond Myklebust96c61cb2009-08-19 18:12:21 -0400945 .mode = S_IFREG|S_IRUSR|S_IWUSR,
Trond Myklebust8854e822009-08-09 15:14:30 -0400946 },
947 [1] = {
948 .name = "content",
949 .i_fop = &content_file_operations_pipefs,
950 .mode = S_IFREG|S_IRUSR,
951 },
952 [2] = {
953 .name = "flush",
954 .i_fop = &cache_flush_operations_pipefs,
955 .mode = S_IFREG|S_IRUSR|S_IWUSR,
956 },
957};
958
959static int rpc_cachedir_populate(struct dentry *dentry, void *private)
960{
961 return rpc_populate(dentry,
962 cache_pipefs_files, 0, 3,
963 private);
964}
965
966static void rpc_cachedir_depopulate(struct dentry *dentry)
967{
968 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
969}
970
971struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
Al Viro64f14262011-07-25 00:35:13 -0400972 umode_t umode, struct cache_detail *cd)
Trond Myklebust8854e822009-08-09 15:14:30 -0400973{
974 return rpc_mkdir_populate(parent, name, umode, NULL,
975 rpc_cachedir_populate, cd);
976}
977
978void rpc_remove_cache_dir(struct dentry *dentry)
979{
980 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983/*
984 * populate the filesystem
985 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700986static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 .alloc_inode = rpc_alloc_inode,
988 .destroy_inode = rpc_destroy_inode,
989 .statfs = simple_statfs,
990};
991
992#define RPCAUTH_GSSMAGIC 0x67596969
993
Trond Myklebustbb156742009-08-09 15:14:21 -0400994/*
995 * We have a single directory with 1 node in it.
996 */
997enum {
998 RPCAUTH_lockd,
999 RPCAUTH_mount,
1000 RPCAUTH_nfs,
1001 RPCAUTH_portmap,
1002 RPCAUTH_statd,
1003 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001004 RPCAUTH_cache,
Trond Myklebustbb156742009-08-09 15:14:21 -04001005 RPCAUTH_RootEOF
1006};
1007
1008static const struct rpc_filelist files[] = {
1009 [RPCAUTH_lockd] = {
1010 .name = "lockd",
1011 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1012 },
1013 [RPCAUTH_mount] = {
1014 .name = "mount",
1015 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1016 },
1017 [RPCAUTH_nfs] = {
1018 .name = "nfs",
1019 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1020 },
1021 [RPCAUTH_portmap] = {
1022 .name = "portmap",
1023 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1024 },
1025 [RPCAUTH_statd] = {
1026 .name = "statd",
1027 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1028 },
1029 [RPCAUTH_nfsd4_cb] = {
1030 .name = "nfsd4_cb",
1031 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1032 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001033 [RPCAUTH_cache] = {
1034 .name = "cache",
1035 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1036 },
Trond Myklebustbb156742009-08-09 15:14:21 -04001037};
1038
Stanislav Kinsbursky432eb1a2011-12-26 15:39:22 +03001039/*
1040 * This call can be used only in RPC pipefs mount notification hooks.
1041 */
1042struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1043 const unsigned char *dir_name)
1044{
1045 struct qstr dir = {
1046 .name = dir_name,
1047 .len = strlen(dir_name),
1048 .hash = full_name_hash(dir_name, strlen(dir_name)),
1049 };
1050
1051 return d_lookup(sb->s_root, &dir);
1052}
1053EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1054
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001055void rpc_pipefs_init_net(struct net *net)
1056{
1057 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1058
1059 mutex_init(&sn->pipefs_sb_lock);
1060}
1061
1062/*
1063 * This call will be used for per network namespace operations calls.
1064 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1065 * found. This lock have to be released by rpc_put_sb_net() when all operations
1066 * will be completed.
1067 */
1068struct super_block *rpc_get_sb_net(const struct net *net)
1069{
1070 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1071
1072 mutex_lock(&sn->pipefs_sb_lock);
1073 if (sn->pipefs_sb)
1074 return sn->pipefs_sb;
1075 mutex_unlock(&sn->pipefs_sb_lock);
1076 return NULL;
1077}
1078EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1079
1080void rpc_put_sb_net(const struct net *net)
1081{
1082 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1083
1084 BUG_ON(sn->pipefs_sb == NULL);
1085 mutex_unlock(&sn->pipefs_sb_lock);
1086}
1087EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089static int
1090rpc_fill_super(struct super_block *sb, void *data, int silent)
1091{
1092 struct inode *inode;
1093 struct dentry *root;
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001094 struct net *net = data;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001095 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001096 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 sb->s_blocksize = PAGE_CACHE_SIZE;
1099 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1100 sb->s_magic = RPCAUTH_GSSMAGIC;
1101 sb->s_op = &s_ops;
1102 sb->s_time_gran = 1;
1103
1104 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1105 if (!inode)
1106 return -ENOMEM;
Al Virofc7bed82010-01-25 18:30:38 -05001107 sb->s_root = root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!root) {
1109 iput(inode);
1110 return -ENOMEM;
1111 }
Trond Myklebustac6fece2009-08-09 15:14:20 -04001112 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001113 return -ENOMEM;
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001114 dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net,
1115 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001116 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1117 RPC_PIPEFS_MOUNT,
1118 sb);
1119 if (err)
1120 goto err_depopulate;
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001121 sb->s_fs_info = get_net(net);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001122 sn->pipefs_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return 0;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001124
1125err_depopulate:
1126 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1127 RPC_PIPEFS_UMOUNT,
1128 sb);
1129 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1130 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
Al Virofc14f2f2010-07-25 01:48:30 +04001133static struct dentry *
1134rpc_mount(struct file_system_type *fs_type,
1135 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001137 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001140void rpc_kill_sb(struct super_block *sb)
1141{
1142 struct net *net = sb->s_fs_info;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001143 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001144
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001145 mutex_lock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001146 sn->pipefs_sb = NULL;
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001147 mutex_unlock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001148 put_net(net);
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001149 dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net,
1150 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001151 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1152 RPC_PIPEFS_UMOUNT,
1153 sb);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001154 kill_litter_super(sb);
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157static struct file_system_type rpc_pipe_fs_type = {
1158 .owner = THIS_MODULE,
1159 .name = "rpc_pipefs",
Al Virofc14f2f2010-07-25 01:48:30 +04001160 .mount = rpc_mount,
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001161 .kill_sb = rpc_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162};
1163
1164static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001165init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1168
Christoph Lametera35afb82007-05-16 22:10:57 -07001169 inode_init_once(&rpci->vfs_inode);
1170 rpci->private = NULL;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +03001171 rpci->pipe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174int register_rpc_pipefs(void)
1175{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001176 int err;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001179 sizeof(struct rpc_inode),
1180 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1181 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001182 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (!rpc_inode_cachep)
1184 return -ENOMEM;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001185 err = register_filesystem(&rpc_pipe_fs_type);
1186 if (err) {
1187 kmem_cache_destroy(rpc_inode_cachep);
1188 return err;
1189 }
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return 0;
1192}
1193
1194void unregister_rpc_pipefs(void)
1195{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001196 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 unregister_filesystem(&rpc_pipe_fs_type);
1198}
Michal Schmidtdcbf8c32011-07-02 00:47:12 +02001199
1200/* Make 'mount -t rpc_pipefs ...' autoload this module. */
1201MODULE_ALIAS("rpc_pipefs");