blob: ac9ee1590739a9f17c38fdb2f4412c029a39a07a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/poll.h>
22#include <linux/wait.h>
23#include <linux/seq_file.h>
24
25#include <linux/sunrpc/clnt.h>
26#include <linux/workqueue.h>
27#include <linux/sunrpc/rpc_pipe_fs.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040028#include <linux/sunrpc/cache.h>
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +030029#include <linux/nsproxy.h>
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030030#include <linux/notifier.h>
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +030031
32#include "netns.h"
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030033#include "sunrpc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +030035#define RPCDBG_FACILITY RPCDBG_DEBUG
36
37#define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static struct file_system_type rpc_pipe_fs_type;
40
41
Christoph Lametere18b8902006-12-06 20:33:20 -080042static struct kmem_cache *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define RPC_UPCALL_TIMEOUT (30*HZ)
45
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030046static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
47
48int rpc_pipefs_notifier_register(struct notifier_block *nb)
49{
50 return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
51}
52EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
53
54void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
55{
56 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
57}
58EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
59
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +040060static void rpc_purge_list(wait_queue_head_t *waitq, struct list_head *head,
Trond Myklebust9842ef32006-02-01 12:18:44 -050061 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050062{
63 struct rpc_pipe_msg *msg;
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050064
Trond Myklebust9842ef32006-02-01 12:18:44 -050065 if (list_empty(head))
66 return;
67 do {
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050068 msg = list_entry(head->next, struct rpc_pipe_msg, list);
Trond Myklebust5a676572010-09-12 19:55:25 -040069 list_del_init(&msg->list);
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050070 msg->errno = err;
71 destroy_msg(msg);
Trond Myklebust9842ef32006-02-01 12:18:44 -050072 } while (!list_empty(head));
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +040073 wake_up(waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76static void
David Howells65f27f32006-11-22 14:55:48 +000077rpc_timeout_upcall_queue(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Trond Myklebust9842ef32006-02-01 12:18:44 -050079 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030080 struct rpc_pipe *pipe =
81 container_of(work, struct rpc_pipe, queue_timeout.work);
Trond Myklebust9842ef32006-02-01 12:18:44 -050082 void (*destroy_msg)(struct rpc_pipe_msg *);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +040083 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030085 spin_lock(&pipe->lock);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030086 destroy_msg = pipe->ops->destroy_msg;
87 if (pipe->nreaders == 0) {
88 list_splice_init(&pipe->pipe, &free_list);
89 pipe->pipelen = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -050090 }
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +040091 dentry = dget(pipe->dentry);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030092 spin_unlock(&pipe->lock);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +040093 if (dentry) {
94 rpc_purge_list(&RPC_I(dentry->d_inode)->waitq,
95 &free_list, destroy_msg, -ETIMEDOUT);
96 dput(dentry);
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Peng Taoc1225152011-09-22 21:50:10 -0400100ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
101 char __user *dst, size_t buflen)
102{
103 char *data = (char *)msg->data + msg->copied;
104 size_t mlen = min(msg->len - msg->copied, buflen);
105 unsigned long left;
106
107 left = copy_to_user(dst, data, mlen);
108 if (left == mlen) {
109 msg->errno = -EFAULT;
110 return -EFAULT;
111 }
112
113 mlen -= left;
114 msg->copied += mlen;
115 msg->errno = 0;
116 return mlen;
117}
118EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
119
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500120/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -0800121 * rpc_queue_upcall - queue an upcall message to userspace
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500122 * @inode: inode of upcall pipe on which to queue given message
123 * @msg: message to queue
124 *
125 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
126 * A userspace process may then later read the upcall by performing a
127 * read on an open file for this inode. It is up to the caller to
128 * initialize the fields of @msg (other than @msg->list) appropriately.
129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130int
Stanislav Kinsburskyd706ed12011-12-26 15:43:49 +0300131rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Trond Myklebust6070fe62005-10-27 22:12:46 -0400133 int res = -EPIPE;
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400134 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300136 spin_lock(&pipe->lock);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300137 if (pipe->nreaders) {
138 list_add_tail(&msg->list, &pipe->pipe);
139 pipe->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400140 res = 0;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300141 } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
142 if (list_empty(&pipe->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -0500143 queue_delayed_work(rpciod_workqueue,
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300144 &pipe->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 RPC_UPCALL_TIMEOUT);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300146 list_add_tail(&msg->list, &pipe->pipe);
147 pipe->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400148 res = 0;
149 }
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400150 dentry = dget(pipe->dentry);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300151 spin_unlock(&pipe->lock);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400152 if (dentry) {
153 wake_up(&RPC_I(dentry->d_inode)->waitq);
154 dput(dentry);
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return res;
157}
Trond Myklebust468039e2008-12-23 15:21:31 -0500158EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Trond Myklebust6070fe62005-10-27 22:12:46 -0400160static inline void
161rpc_inode_setowner(struct inode *inode, void *private)
162{
163 RPC_I(inode)->private = private;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void
167rpc_close_pipes(struct inode *inode)
168{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300169 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500170 int need_release;
Stanislav Kinsburskyad6b1342012-01-10 16:12:38 +0400171 LIST_HEAD(free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800173 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyad6b1342012-01-10 16:12:38 +0400174 spin_lock(&pipe->lock);
175 need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
176 pipe->nreaders = 0;
177 list_splice_init(&pipe->in_upcall, &free_list);
178 list_splice_init(&pipe->pipe, &free_list);
179 pipe->pipelen = 0;
180 pipe->dentry = NULL;
181 spin_unlock(&pipe->lock);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400182 rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -EPIPE);
Stanislav Kinsburskyad6b1342012-01-10 16:12:38 +0400183 pipe->nwriters = 0;
184 if (need_release && pipe->ops->release_pipe)
185 pipe->ops->release_pipe(inode);
186 cancel_delayed_work_sync(&pipe->queue_timeout);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400187 rpc_inode_setowner(inode, NULL);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400188 RPC_I(inode)->pipe = NULL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800189 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static struct inode *
193rpc_alloc_inode(struct super_block *sb)
194{
195 struct rpc_inode *rpci;
Christoph Lametere94b1762006-12-06 20:33:17 -0800196 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!rpci)
198 return NULL;
199 return &rpci->vfs_inode;
200}
201
202static void
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100203rpc_i_callback(struct rcu_head *head)
204{
205 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100206 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
207}
208
209static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210rpc_destroy_inode(struct inode *inode)
211{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100212 call_rcu(&inode->i_rcu, rpc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215static int
216rpc_pipe_open(struct inode *inode, struct file *filp)
217{
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400218 struct rpc_pipe *pipe;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500219 int first_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 int res = -ENXIO;
221
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800222 mutex_lock(&inode->i_mutex);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400223 pipe = RPC_I(inode)->pipe;
224 if (pipe == NULL)
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500225 goto out;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300226 first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
227 if (first_open && pipe->ops->open_pipe) {
228 res = pipe->ops->open_pipe(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500229 if (res)
230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500232 if (filp->f_mode & FMODE_READ)
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300233 pipe->nreaders++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500234 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300235 pipe->nwriters++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500236 res = 0;
237out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800238 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return res;
240}
241
242static int
243rpc_pipe_release(struct inode *inode, struct file *filp)
244{
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400245 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct rpc_pipe_msg *msg;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500247 int last_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800249 mutex_lock(&inode->i_mutex);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400250 pipe = RPC_I(inode)->pipe;
251 if (pipe == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto out;
Joe Perches655b5bb2010-09-04 18:52:53 -0700253 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (msg != NULL) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300255 spin_lock(&pipe->lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500256 msg->errno = -EAGAIN;
Trond Myklebust5a676572010-09-12 19:55:25 -0400257 list_del_init(&msg->list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300258 spin_unlock(&pipe->lock);
259 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300262 pipe->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500263 if (filp->f_mode & FMODE_READ) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300264 pipe->nreaders --;
265 if (pipe->nreaders == 0) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500266 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300267 spin_lock(&pipe->lock);
268 list_splice_init(&pipe->pipe, &free_list);
269 pipe->pipelen = 0;
270 spin_unlock(&pipe->lock);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400271 rpc_purge_list(&RPC_I(inode)->waitq, &free_list,
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300272 pipe->ops->destroy_msg, -EAGAIN);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500273 }
274 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300275 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
276 if (last_close && pipe->ops->release_pipe)
277 pipe->ops->release_pipe(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800279 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return 0;
281}
282
283static ssize_t
284rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
285{
Josef Sipek303b46b2006-12-08 02:37:42 -0800286 struct inode *inode = filp->f_path.dentry->d_inode;
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400287 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct rpc_pipe_msg *msg;
289 int res = 0;
290
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800291 mutex_lock(&inode->i_mutex);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400292 pipe = RPC_I(inode)->pipe;
293 if (pipe == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 res = -EPIPE;
295 goto out_unlock;
296 }
297 msg = filp->private_data;
298 if (msg == NULL) {
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300299 spin_lock(&pipe->lock);
300 if (!list_empty(&pipe->pipe)) {
301 msg = list_entry(pipe->pipe.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct rpc_pipe_msg,
303 list);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300304 list_move(&msg->list, &pipe->in_upcall);
305 pipe->pipelen -= msg->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 filp->private_data = msg;
307 msg->copied = 0;
308 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300309 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (msg == NULL)
311 goto out_unlock;
312 }
313 /* NOTE: it is up to the callback to update msg->copied */
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300314 res = pipe->ops->upcall(filp, msg, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (res < 0 || msg->len == msg->copied) {
316 filp->private_data = NULL;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300317 spin_lock(&pipe->lock);
Trond Myklebust5a676572010-09-12 19:55:25 -0400318 list_del_init(&msg->list);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300319 spin_unlock(&pipe->lock);
320 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800323 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return res;
325}
326
327static ssize_t
328rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
329{
Josef Sipek303b46b2006-12-08 02:37:42 -0800330 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int res;
332
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800333 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 res = -EPIPE;
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400335 if (RPC_I(inode)->pipe != NULL)
336 res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800337 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return res;
339}
340
341static unsigned int
342rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
343{
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400344 struct inode *inode = filp->f_path.dentry->d_inode;
345 struct rpc_inode *rpci = RPC_I(inode);
346 unsigned int mask = POLLOUT | POLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400348 poll_wait(filp, &rpci->waitq, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400350 mutex_lock(&inode->i_mutex);
351 if (rpci->pipe == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 mask |= POLLERR | POLLHUP;
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400353 else if (filp->private_data || !list_empty(&rpci->pipe->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 mask |= POLLIN | POLLRDNORM;
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400355 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return mask;
357}
358
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200359static long
360rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200362 struct inode *inode = filp->f_path.dentry->d_inode;
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400363 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int len;
365
366 switch (cmd) {
367 case FIONREAD:
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400368 mutex_lock(&inode->i_mutex);
369 pipe = RPC_I(inode)->pipe;
370 if (pipe == NULL) {
371 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -EPIPE;
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200373 }
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400374 spin_lock(&pipe->lock);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300375 len = 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 Kinsburskyd0fe13b2011-12-26 15:43:41 +0300381 spin_unlock(&pipe->lock);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400382 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return put_user(len, (int __user *)arg);
384 default:
385 return -EINVAL;
386 }
387}
388
Arjan van de Venda7071d2007-02-12 00:55:36 -0800389static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 .owner = THIS_MODULE,
391 .llseek = no_llseek,
392 .read = rpc_pipe_read,
393 .write = rpc_pipe_write,
394 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200395 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 .open = rpc_pipe_open,
397 .release = rpc_pipe_release,
398};
399
400static int
401rpc_show_info(struct seq_file *m, void *v)
402{
403 struct rpc_clnt *clnt = m->private;
404
405 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
406 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
407 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400408 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
409 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400410 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
412}
413
414static int
415rpc_info_open(struct inode *inode, struct file *file)
416{
Trond Myklebust006abe82010-09-12 19:55:25 -0400417 struct rpc_clnt *clnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int ret = single_open(file, rpc_show_info, NULL);
419
420 if (!ret) {
421 struct seq_file *m = file->private_data;
Trond Myklebust006abe82010-09-12 19:55:25 -0400422
423 spin_lock(&file->f_path.dentry->d_lock);
424 if (!d_unhashed(file->f_path.dentry))
425 clnt = RPC_I(inode)->private;
426 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
427 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 m->private = clnt;
429 } else {
Trond Myklebust006abe82010-09-12 19:55:25 -0400430 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 single_release(inode, file);
432 ret = -EINVAL;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 return ret;
436}
437
438static int
439rpc_info_release(struct inode *inode, struct file *file)
440{
441 struct seq_file *m = file->private_data;
442 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
443
444 if (clnt)
445 rpc_release_client(clnt);
446 return single_release(inode, file);
447}
448
Arjan van de Venda7071d2007-02-12 00:55:36 -0800449static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 .owner = THIS_MODULE,
451 .open = rpc_info_open,
452 .read = seq_read,
453 .llseek = seq_lseek,
454 .release = rpc_info_release,
455};
456
457
458/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * Description of fs contents.
460 */
461struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400462 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800463 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400464 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465};
466
Nick Pigginfe15ce42011-01-07 17:49:23 +1100467static int rpc_delete_dentry(const struct dentry *dentry)
Trond Myklebust62e17612007-06-08 14:14:46 -0400468{
469 return 1;
470}
471
Al Viro3ba13d12009-02-20 06:02:22 +0000472static const struct dentry_operations rpc_dentry_operations = {
Trond Myklebust62e17612007-06-08 14:14:46 -0400473 .d_delete = rpc_delete_dentry,
474};
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400477rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct inode *inode = new_inode(sb);
480 if (!inode)
481 return NULL;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400482 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Joe Perches89f0e4f2011-07-01 09:43:12 +0000485 switch (mode & S_IFMT) {
486 case S_IFDIR:
487 inode->i_fop = &simple_dir_operations;
488 inode->i_op = &simple_dir_inode_operations;
489 inc_nlink(inode);
490 default:
491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 return inode;
494}
495
Trond Myklebust75898062009-08-09 15:14:17 -0400496static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
497 umode_t mode,
498 const struct file_operations *i_fop,
499 void *private)
500{
501 struct inode *inode;
502
Trond Myklebustbeb0f0a2010-12-20 21:19:26 +0000503 d_drop(dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400504 inode = rpc_get_inode(dir->i_sb, mode);
505 if (!inode)
506 goto out_err;
507 inode->i_ino = iunique(dir->i_sb, 100);
508 if (i_fop)
509 inode->i_fop = i_fop;
510 if (private)
511 rpc_inode_setowner(inode, private);
512 d_add(dentry, inode);
513 return 0;
514out_err:
515 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
516 __FILE__, __func__, dentry->d_name.name);
517 dput(dentry);
518 return -ENOMEM;
519}
520
Trond Myklebustac6fece2009-08-09 15:14:20 -0400521static int __rpc_create(struct inode *dir, struct dentry *dentry,
522 umode_t mode,
523 const struct file_operations *i_fop,
524 void *private)
525{
526 int err;
527
528 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
529 if (err)
530 return err;
531 fsnotify_create(dir, dentry);
532 return 0;
533}
534
Trond Myklebust75898062009-08-09 15:14:17 -0400535static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
536 umode_t mode,
537 const struct file_operations *i_fop,
538 void *private)
539{
540 int err;
541
542 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
543 if (err)
544 return err;
545 inc_nlink(dir);
546 fsnotify_mkdir(dir, dentry);
547 return 0;
548}
549
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300550static void
551init_pipe(struct rpc_pipe *pipe)
552{
553 pipe->nreaders = 0;
554 pipe->nwriters = 0;
555 INIT_LIST_HEAD(&pipe->in_upcall);
556 INIT_LIST_HEAD(&pipe->in_downcall);
557 INIT_LIST_HEAD(&pipe->pipe);
558 pipe->pipelen = 0;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300559 INIT_DELAYED_WORK(&pipe->queue_timeout,
560 rpc_timeout_upcall_queue);
561 pipe->ops = NULL;
562 spin_lock_init(&pipe->lock);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300563 pipe->dentry = NULL;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300564}
565
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300566void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
567{
568 kfree(pipe);
569}
570EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
571
572struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
Trond Myklebust75898062009-08-09 15:14:17 -0400573{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300574 struct rpc_pipe *pipe;
Trond Myklebust75898062009-08-09 15:14:17 -0400575
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300576 pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
577 if (!pipe)
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300578 return ERR_PTR(-ENOMEM);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300579 init_pipe(pipe);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300580 pipe->ops = ops;
581 pipe->flags = flags;
582 return pipe;
583}
584EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
585
586static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
587 umode_t mode,
588 const struct file_operations *i_fop,
589 void *private,
590 struct rpc_pipe *pipe)
591{
592 struct rpc_inode *rpci;
593 int err;
594
Trond Myklebust75898062009-08-09 15:14:17 -0400595 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300596 if (err)
Trond Myklebust75898062009-08-09 15:14:17 -0400597 return err;
598 rpci = RPC_I(dentry->d_inode);
Trond Myklebust75898062009-08-09 15:14:17 -0400599 rpci->private = private;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300600 rpci->pipe = pipe;
Trond Myklebust75898062009-08-09 15:14:17 -0400601 fsnotify_create(dir, dentry);
602 return 0;
603}
604
Trond Myklebustac6fece2009-08-09 15:14:20 -0400605static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
606{
607 int ret;
608
609 dget(dentry);
610 ret = simple_rmdir(dir, dentry);
611 d_delete(dentry);
612 dput(dentry);
613 return ret;
614}
615
Stanislav Kinsburskyeee173252012-01-10 16:13:19 +0400616int rpc_rmdir(struct dentry *dentry)
617{
618 struct dentry *parent;
619 struct inode *dir;
620 int error;
621
622 parent = dget_parent(dentry);
623 dir = parent->d_inode;
624 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
625 error = __rpc_rmdir(dir, dentry);
626 mutex_unlock(&dir->i_mutex);
627 dput(parent);
628 return error;
629}
630EXPORT_SYMBOL_GPL(rpc_rmdir);
631
Trond Myklebust810d90b2009-08-09 15:14:18 -0400632static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
633{
634 int ret;
635
636 dget(dentry);
637 ret = simple_unlink(dir, dentry);
638 d_delete(dentry);
639 dput(dentry);
640 return ret;
641}
642
643static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
644{
645 struct inode *inode = dentry->d_inode;
Trond Myklebust810d90b2009-08-09 15:14:18 -0400646
Trond Myklebust810d90b2009-08-09 15:14:18 -0400647 rpc_close_pipes(inode);
648 return __rpc_unlink(dir, dentry);
649}
650
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400651static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
652 struct qstr *name)
653{
654 struct dentry *dentry;
655
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300656 dentry = d_lookup(parent, name);
657 if (!dentry) {
658 dentry = d_alloc(parent, name);
659 if (!dentry)
660 return ERR_PTR(-ENOMEM);
661 }
662 if (dentry->d_inode == NULL) {
663 d_set_d_op(dentry, &rpc_dentry_operations);
Dan Carpenterf1f0abe12010-03-21 12:10:34 -0400664 return dentry;
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300665 }
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400666 dput(dentry);
667 return ERR_PTR(-EEXIST);
668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/*
671 * FIXME: This probably has races.
672 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400673static void __rpc_depopulate(struct dentry *parent,
674 const struct rpc_filelist *files,
675 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct inode *dir = parent->d_inode;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400678 struct dentry *dentry;
679 struct qstr name;
680 int i;
681
682 for (i = start; i < eof; i++) {
683 name.name = files[i].name;
684 name.len = strlen(files[i].name);
685 name.hash = full_name_hash(name.name, name.len);
686 dentry = d_lookup(parent, &name);
687
688 if (dentry == NULL)
689 continue;
690 if (dentry->d_inode == NULL)
691 goto next;
692 switch (dentry->d_inode->i_mode & S_IFMT) {
693 default:
694 BUG();
695 case S_IFREG:
696 __rpc_unlink(dir, dentry);
697 break;
698 case S_IFDIR:
699 __rpc_rmdir(dir, dentry);
700 }
701next:
702 dput(dentry);
703 }
704}
705
706static void rpc_depopulate(struct dentry *parent,
707 const struct rpc_filelist *files,
708 int start, int eof)
709{
710 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Arjan van de Venc6573c22006-07-03 00:25:16 -0700712 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400713 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800714 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Trond Myklebustac6fece2009-08-09 15:14:20 -0400717static int rpc_populate(struct dentry *parent,
718 const struct rpc_filelist *files,
719 int start, int eof,
720 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Trond Myklebustac6fece2009-08-09 15:14:20 -0400722 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400724 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800726 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 for (i = start; i < eof; i++) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400728 struct qstr q;
729
730 q.name = files[i].name;
731 q.len = strlen(files[i].name);
732 q.hash = full_name_hash(q.name, q.len);
733 dentry = __rpc_lookup_create_exclusive(parent, &q);
734 err = PTR_ERR(dentry);
735 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400737 switch (files[i].mode & S_IFMT) {
738 default:
739 BUG();
740 case S_IFREG:
741 err = __rpc_create(dir, dentry,
742 files[i].mode,
743 files[i].i_fop,
744 private);
745 break;
746 case S_IFDIR:
747 err = __rpc_mkdir(dir, dentry,
748 files[i].mode,
749 NULL,
750 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400752 if (err != 0)
753 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800755 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return 0;
757out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400758 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800759 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800761 __FILE__, __func__, parent->d_name.name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400762 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400763}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100764
Trond Myklebuste57aed72009-08-09 15:14:26 -0400765static struct dentry *rpc_mkdir_populate(struct dentry *parent,
766 struct qstr *name, umode_t mode, void *private,
767 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400768{
Trond Myklebustf1345852005-09-23 11:08:25 -0400769 struct dentry *dentry;
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400770 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400771 int error;
772
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400773 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
774 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400775 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400776 goto out;
777 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400778 if (error != 0)
779 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400780 if (populate != NULL) {
781 error = populate(dentry, args_populate);
782 if (error)
783 goto err_rmdir;
784 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400785out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800786 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400787 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400788err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400789 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400790out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400791 dentry = ERR_PTR(error);
792 goto out;
793}
794
Trond Myklebuste57aed72009-08-09 15:14:26 -0400795static int rpc_rmdir_depopulate(struct dentry *dentry,
796 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400797{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700798 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400799 struct inode *dir;
800 int error;
801
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700802 parent = dget_parent(dentry);
803 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700804 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400805 if (depopulate != NULL)
806 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400807 error = __rpc_rmdir(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800808 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700809 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400810 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500813/**
814 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
815 * @parent: dentry of directory to create new "pipe" in
816 * @name: name of pipe
817 * @private: private data to associate with the pipe, for the caller's use
818 * @ops: operations defining the behavior of the pipe: upcall, downcall,
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500819 * release_pipe, open_pipe, and destroy_msg.
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300820 * @flags: rpc_pipe flags
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500821 *
822 * Data is made available for userspace to read by calls to
823 * rpc_queue_upcall(). The actual reads will result in calls to
824 * @ops->upcall, which will be called with the file pointer,
825 * message, and userspace buffer to copy to.
826 *
827 * Writes can come at any time, and do not necessarily have to be
828 * responses to upcalls. They will result in calls to @msg->downcall.
829 *
830 * The @private argument passed here will be available to all these methods
831 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
832 */
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300833struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
834 void *private, struct rpc_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 struct dentry *dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400837 struct inode *dir = parent->d_inode;
Trond Myklebust7364af62009-08-09 15:14:16 -0400838 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400839 struct qstr q;
Trond Myklebust75898062009-08-09 15:14:17 -0400840 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400841
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300842 if (pipe->ops->upcall == NULL)
Trond Myklebust7364af62009-08-09 15:14:16 -0400843 umode &= ~S_IRUGO;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300844 if (pipe->ops->downcall == NULL)
Trond Myklebust7364af62009-08-09 15:14:16 -0400845 umode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400847 q.name = name;
848 q.len = strlen(name);
849 q.hash = full_name_hash(q.name, q.len),
850
851 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300852 dentry = __rpc_lookup_create_exclusive(parent, &q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400854 goto out;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300855 err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
856 private, pipe);
Trond Myklebust75898062009-08-09 15:14:17 -0400857 if (err)
858 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400859out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800860 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400861 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400862out_err:
863 dentry = ERR_PTR(err);
Trond Myklebust158998b2006-08-24 01:03:17 -0400864 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800865 __FILE__, __func__, parent->d_name.name, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400866 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300869EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500871/**
872 * rpc_unlink - remove a pipe
873 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
874 *
875 * After this call, lookups will no longer find the pipe, and any
876 * attempts to read or write using preexisting opens of the pipe will
877 * return -EPIPE.
878 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400879int
Trond Myklebust5d674762006-07-31 14:11:48 -0700880rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Trond Myklebust5d674762006-07-31 14:11:48 -0700882 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400883 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700884 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Trond Myklebust5d674762006-07-31 14:11:48 -0700886 parent = dget_parent(dentry);
887 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700888 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400889 error = __rpc_rmpipe(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800890 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700891 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400892 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
Trond Myklebust468039e2008-12-23 15:21:31 -0500894EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Trond Myklebuste57aed72009-08-09 15:14:26 -0400896enum {
897 RPCAUTH_info,
898 RPCAUTH_EOF
899};
900
901static const struct rpc_filelist authfiles[] = {
902 [RPCAUTH_info] = {
903 .name = "info",
904 .i_fop = &rpc_info_operations,
905 .mode = S_IFREG | S_IRUSR,
906 },
907};
908
909static int rpc_clntdir_populate(struct dentry *dentry, void *private)
910{
911 return rpc_populate(dentry,
912 authfiles, RPCAUTH_info, RPCAUTH_EOF,
913 private);
914}
915
916static void rpc_clntdir_depopulate(struct dentry *dentry)
917{
918 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
919}
920
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400921/**
922 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Randy Dunlap4111d4f2009-09-23 14:36:38 -0400923 * @dentry: dentry from the rpc_pipefs root to the new directory
924 * @name: &struct qstr for the name
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400925 * @rpc_client: rpc client to associate with this directory
926 *
927 * This creates a directory at the given @path associated with
928 * @rpc_clnt, which will contain a file named "info" with some basic
929 * information about the client, together with any "pipes" that may
930 * later be created using rpc_mkpipe().
931 */
Trond Myklebust23ac6582009-08-09 15:14:25 -0400932struct dentry *rpc_create_client_dir(struct dentry *dentry,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400933 struct qstr *name,
934 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400935{
Trond Myklebuste57aed72009-08-09 15:14:26 -0400936 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
937 rpc_clntdir_populate, rpc_client);
938}
939
940/**
941 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400942 * @clnt: rpc client
Trond Myklebuste57aed72009-08-09 15:14:26 -0400943 */
944int rpc_remove_client_dir(struct dentry *dentry)
945{
946 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400947}
948
Trond Myklebust8854e822009-08-09 15:14:30 -0400949static const struct rpc_filelist cache_pipefs_files[3] = {
950 [0] = {
951 .name = "channel",
952 .i_fop = &cache_file_operations_pipefs,
Trond Myklebust96c61cb2009-08-19 18:12:21 -0400953 .mode = S_IFREG|S_IRUSR|S_IWUSR,
Trond Myklebust8854e822009-08-09 15:14:30 -0400954 },
955 [1] = {
956 .name = "content",
957 .i_fop = &content_file_operations_pipefs,
958 .mode = S_IFREG|S_IRUSR,
959 },
960 [2] = {
961 .name = "flush",
962 .i_fop = &cache_flush_operations_pipefs,
963 .mode = S_IFREG|S_IRUSR|S_IWUSR,
964 },
965};
966
967static int rpc_cachedir_populate(struct dentry *dentry, void *private)
968{
969 return rpc_populate(dentry,
970 cache_pipefs_files, 0, 3,
971 private);
972}
973
974static void rpc_cachedir_depopulate(struct dentry *dentry)
975{
976 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
977}
978
979struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
Al Viro64f14262011-07-25 00:35:13 -0400980 umode_t umode, struct cache_detail *cd)
Trond Myklebust8854e822009-08-09 15:14:30 -0400981{
982 return rpc_mkdir_populate(parent, name, umode, NULL,
983 rpc_cachedir_populate, cd);
984}
985
986void rpc_remove_cache_dir(struct dentry *dentry)
987{
988 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991/*
992 * populate the filesystem
993 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700994static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 .alloc_inode = rpc_alloc_inode,
996 .destroy_inode = rpc_destroy_inode,
997 .statfs = simple_statfs,
998};
999
1000#define RPCAUTH_GSSMAGIC 0x67596969
1001
Trond Myklebustbb156742009-08-09 15:14:21 -04001002/*
1003 * We have a single directory with 1 node in it.
1004 */
1005enum {
1006 RPCAUTH_lockd,
1007 RPCAUTH_mount,
1008 RPCAUTH_nfs,
1009 RPCAUTH_portmap,
1010 RPCAUTH_statd,
1011 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001012 RPCAUTH_cache,
Trond Myklebustbb156742009-08-09 15:14:21 -04001013 RPCAUTH_RootEOF
1014};
1015
1016static const struct rpc_filelist files[] = {
1017 [RPCAUTH_lockd] = {
1018 .name = "lockd",
1019 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1020 },
1021 [RPCAUTH_mount] = {
1022 .name = "mount",
1023 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1024 },
1025 [RPCAUTH_nfs] = {
1026 .name = "nfs",
1027 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1028 },
1029 [RPCAUTH_portmap] = {
1030 .name = "portmap",
1031 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1032 },
1033 [RPCAUTH_statd] = {
1034 .name = "statd",
1035 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1036 },
1037 [RPCAUTH_nfsd4_cb] = {
1038 .name = "nfsd4_cb",
1039 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1040 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001041 [RPCAUTH_cache] = {
1042 .name = "cache",
1043 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1044 },
Trond Myklebustbb156742009-08-09 15:14:21 -04001045};
1046
Stanislav Kinsbursky432eb1a2011-12-26 15:39:22 +03001047/*
1048 * This call can be used only in RPC pipefs mount notification hooks.
1049 */
1050struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1051 const unsigned char *dir_name)
1052{
1053 struct qstr dir = {
1054 .name = dir_name,
1055 .len = strlen(dir_name),
1056 .hash = full_name_hash(dir_name, strlen(dir_name)),
1057 };
1058
1059 return d_lookup(sb->s_root, &dir);
1060}
1061EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1062
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001063void rpc_pipefs_init_net(struct net *net)
1064{
1065 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1066
1067 mutex_init(&sn->pipefs_sb_lock);
1068}
1069
1070/*
1071 * This call will be used for per network namespace operations calls.
1072 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1073 * found. This lock have to be released by rpc_put_sb_net() when all operations
1074 * will be completed.
1075 */
1076struct super_block *rpc_get_sb_net(const struct net *net)
1077{
1078 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1079
1080 mutex_lock(&sn->pipefs_sb_lock);
1081 if (sn->pipefs_sb)
1082 return sn->pipefs_sb;
1083 mutex_unlock(&sn->pipefs_sb_lock);
1084 return NULL;
1085}
1086EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1087
1088void rpc_put_sb_net(const struct net *net)
1089{
1090 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1091
1092 BUG_ON(sn->pipefs_sb == NULL);
1093 mutex_unlock(&sn->pipefs_sb_lock);
1094}
1095EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097static int
1098rpc_fill_super(struct super_block *sb, void *data, int silent)
1099{
1100 struct inode *inode;
1101 struct dentry *root;
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001102 struct net *net = data;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001103 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001104 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 sb->s_blocksize = PAGE_CACHE_SIZE;
1107 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1108 sb->s_magic = RPCAUTH_GSSMAGIC;
1109 sb->s_op = &s_ops;
1110 sb->s_time_gran = 1;
1111
1112 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1113 if (!inode)
1114 return -ENOMEM;
Al Virofc7bed82010-01-25 18:30:38 -05001115 sb->s_root = root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (!root) {
1117 iput(inode);
1118 return -ENOMEM;
1119 }
Trond Myklebustac6fece2009-08-09 15:14:20 -04001120 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001121 return -ENOMEM;
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001122 dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net,
1123 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001124 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1125 RPC_PIPEFS_MOUNT,
1126 sb);
1127 if (err)
1128 goto err_depopulate;
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001129 sb->s_fs_info = get_net(net);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001130 sn->pipefs_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return 0;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001132
1133err_depopulate:
1134 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1135 RPC_PIPEFS_UMOUNT,
1136 sb);
1137 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1138 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Al Virofc14f2f2010-07-25 01:48:30 +04001141static struct dentry *
1142rpc_mount(struct file_system_type *fs_type,
1143 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001145 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001148void rpc_kill_sb(struct super_block *sb)
1149{
1150 struct net *net = sb->s_fs_info;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001151 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001152
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001153 mutex_lock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001154 sn->pipefs_sb = NULL;
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001155 mutex_unlock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001156 put_net(net);
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001157 dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net,
1158 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001159 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1160 RPC_PIPEFS_UMOUNT,
1161 sb);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001162 kill_litter_super(sb);
1163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165static struct file_system_type rpc_pipe_fs_type = {
1166 .owner = THIS_MODULE,
1167 .name = "rpc_pipefs",
Al Virofc14f2f2010-07-25 01:48:30 +04001168 .mount = rpc_mount,
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001169 .kill_sb = rpc_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170};
1171
1172static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001173init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1176
Christoph Lametera35afb82007-05-16 22:10:57 -07001177 inode_init_once(&rpci->vfs_inode);
1178 rpci->private = NULL;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +03001179 rpci->pipe = NULL;
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +04001180 init_waitqueue_head(&rpci->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183int register_rpc_pipefs(void)
1184{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001185 int err;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001188 sizeof(struct rpc_inode),
1189 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1190 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001191 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!rpc_inode_cachep)
1193 return -ENOMEM;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001194 err = rpc_clients_notifier_register();
1195 if (err)
1196 goto err_notifier;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001197 err = register_filesystem(&rpc_pipe_fs_type);
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001198 if (err)
1199 goto err_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001201
1202err_register:
1203 rpc_clients_notifier_unregister();
1204err_notifier:
1205 kmem_cache_destroy(rpc_inode_cachep);
1206 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209void unregister_rpc_pipefs(void)
1210{
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001211 rpc_clients_notifier_unregister();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001212 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 unregister_filesystem(&rpc_pipe_fs_type);
1214}
Michal Schmidtdcbf8c32011-07-02 00:47:12 +02001215
1216/* Make 'mount -t rpc_pipefs ...' autoload this module. */
1217MODULE_ALIAS("rpc_pipefs");