SUNRPC: cleanup GSS pipes usage
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / sunrpc / rpc_pipe.c
CommitLineData
1da177e4
LT
1/*
2 * net/sunrpc/rpc_pipe.c
3 *
4 * Userland/kernel interface for rpcauth_gss.
5 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
d51fe1be 6 * and fs/sysfs/inode.c
1da177e4
LT
7 *
8 * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
9 *
10 */
1da177e4
LT
11#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>
50e437d5 17#include <linux/fsnotify.h>
1da177e4
LT
18#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>
8854e82d 29#include <linux/sunrpc/cache.h>
021c68de 30#include <linux/nsproxy.h>
2d00131a 31#include <linux/notifier.h>
021c68de
SK
32
33#include "netns.h"
2d00131a 34#include "sunrpc.h"
1da177e4 35
efc46bf2
SK
36#define RPCDBG_FACILITY RPCDBG_DEBUG
37
38#define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
39
fc14f2fe 40static struct vfsmount *rpc_mnt __read_mostly;
1da177e4
LT
41static int rpc_mount_count;
42
43static struct file_system_type rpc_pipe_fs_type;
44
45
e18b890b 46static struct kmem_cache *rpc_inode_cachep __read_mostly;
1da177e4
LT
47
48#define RPC_UPCALL_TIMEOUT (30*HZ)
49
2d00131a
SK
50static 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
ba9e0975 64static void rpc_purge_list(struct rpc_pipe *pipe, struct list_head *head,
9842ef35 65 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
1da177e4 66{
1da177e4
LT
67 struct rpc_pipe_msg *msg;
68
9842ef35
TM
69 if (list_empty(head))
70 return;
71 do {
b3eb67a2 72 msg = list_entry(head->next, struct rpc_pipe_msg, list);
5a67657a 73 list_del_init(&msg->list);
1da177e4 74 msg->errno = err;
b3eb67a2 75 destroy_msg(msg);
9842ef35 76 } while (!list_empty(head));
ba9e0975 77 wake_up(&pipe->waitq);
1da177e4
LT
78}
79
80static void
65f27f38 81rpc_timeout_upcall_queue(struct work_struct *work)
1da177e4 82{
9842ef35 83 LIST_HEAD(free_list);
ba9e0975
SK
84 struct rpc_pipe *pipe =
85 container_of(work, struct rpc_pipe, queue_timeout.work);
9842ef35 86 void (*destroy_msg)(struct rpc_pipe_msg *);
1da177e4 87
ba9e0975
SK
88 spin_lock(&pipe->lock);
89 if (pipe->ops == NULL) {
90 spin_unlock(&pipe->lock);
9842ef35
TM
91 return;
92 }
ba9e0975
SK
93 destroy_msg = pipe->ops->destroy_msg;
94 if (pipe->nreaders == 0) {
95 list_splice_init(&pipe->pipe, &free_list);
96 pipe->pipelen = 0;
9842ef35 97 }
ba9e0975
SK
98 spin_unlock(&pipe->lock);
99 rpc_purge_list(pipe, &free_list, destroy_msg, -ETIMEDOUT);
1da177e4
LT
100}
101
c1225158
PT
102ssize_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
93a44a75 122/**
1a5778aa 123 * rpc_queue_upcall - queue an upcall message to userspace
93a44a75
BF
124 * @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 */
1da177e4 132int
d706ed1f 133rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
1da177e4 134{
6070fe6f 135 int res = -EPIPE;
1da177e4 136
d0fe13ba
SK
137 spin_lock(&pipe->lock);
138 if (pipe->ops == NULL)
6070fe6f 139 goto out;
d0fe13ba
SK
140 if (pipe->nreaders) {
141 list_add_tail(&msg->list, &pipe->pipe);
142 pipe->pipelen += msg->len;
6070fe6f 143 res = 0;
d0fe13ba
SK
144 } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
145 if (list_empty(&pipe->pipe))
24c5d9d7 146 queue_delayed_work(rpciod_workqueue,
d0fe13ba 147 &pipe->queue_timeout,
1da177e4 148 RPC_UPCALL_TIMEOUT);
d0fe13ba
SK
149 list_add_tail(&msg->list, &pipe->pipe);
150 pipe->pipelen += msg->len;
6070fe6f
TM
151 res = 0;
152 }
153out:
d0fe13ba
SK
154 spin_unlock(&pipe->lock);
155 wake_up(&pipe->waitq);
1da177e4
LT
156 return res;
157}
468039ee 158EXPORT_SYMBOL_GPL(rpc_queue_upcall);
1da177e4 159
6070fe6f
TM
160static inline void
161rpc_inode_setowner(struct inode *inode, void *private)
162{
163 RPC_I(inode)->private = private;
164}
165
1da177e4
LT
166static void
167rpc_close_pipes(struct inode *inode)
168{
ba9e0975 169 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
b693ba4a 170 const struct rpc_pipe_ops *ops;
e712804a 171 int need_release;
1da177e4 172
1b1dcc1b 173 mutex_lock(&inode->i_mutex);
ba9e0975 174 ops = pipe->ops;
9842ef35
TM
175 if (ops != NULL) {
176 LIST_HEAD(free_list);
ba9e0975
SK
177 spin_lock(&pipe->lock);
178 need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
179 pipe->nreaders = 0;
180 list_splice_init(&pipe->in_upcall, &free_list);
181 list_splice_init(&pipe->pipe, &free_list);
182 pipe->pipelen = 0;
183 pipe->ops = NULL;
184 spin_unlock(&pipe->lock);
185 rpc_purge_list(pipe, &free_list, ops->destroy_msg, -EPIPE);
186 pipe->nwriters = 0;
e712804a 187 if (need_release && ops->release_pipe)
9842ef35 188 ops->release_pipe(inode);
ba9e0975 189 cancel_delayed_work_sync(&pipe->queue_timeout);
1da177e4 190 }
6070fe6f 191 rpc_inode_setowner(inode, NULL);
1b1dcc1b 192 mutex_unlock(&inode->i_mutex);
1da177e4
LT
193}
194
1da177e4
LT
195static struct inode *
196rpc_alloc_inode(struct super_block *sb)
197{
198 struct rpc_inode *rpci;
e94b1766 199 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
1da177e4
LT
200 if (!rpci)
201 return NULL;
202 return &rpci->vfs_inode;
203}
204
205static void
fa0d7e3d 206rpc_i_callback(struct rcu_head *head)
1da177e4 207{
fa0d7e3d 208 struct inode *inode = container_of(head, struct inode, i_rcu);
ba9e0975 209 kfree(RPC_I(inode)->pipe);
1da177e4
LT
210 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
211}
212
fa0d7e3d
NP
213static void
214rpc_destroy_inode(struct inode *inode)
215{
216 call_rcu(&inode->i_rcu, rpc_i_callback);
217}
218
1da177e4
LT
219static int
220rpc_pipe_open(struct inode *inode, struct file *filp)
221{
d0fe13ba 222 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
c3810608 223 int first_open;
1da177e4
LT
224 int res = -ENXIO;
225
1b1dcc1b 226 mutex_lock(&inode->i_mutex);
d0fe13ba 227 if (pipe->ops == NULL)
c3810608 228 goto out;
d0fe13ba
SK
229 first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
230 if (first_open && pipe->ops->open_pipe) {
231 res = pipe->ops->open_pipe(inode);
c3810608
BF
232 if (res)
233 goto out;
1da177e4 234 }
c3810608 235 if (filp->f_mode & FMODE_READ)
d0fe13ba 236 pipe->nreaders++;
c3810608 237 if (filp->f_mode & FMODE_WRITE)
d0fe13ba 238 pipe->nwriters++;
c3810608
BF
239 res = 0;
240out:
1b1dcc1b 241 mutex_unlock(&inode->i_mutex);
1da177e4
LT
242 return res;
243}
244
245static int
246rpc_pipe_release(struct inode *inode, struct file *filp)
247{
ba9e0975 248 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
1da177e4 249 struct rpc_pipe_msg *msg;
e712804a 250 int last_close;
1da177e4 251
1b1dcc1b 252 mutex_lock(&inode->i_mutex);
ba9e0975 253 if (pipe->ops == NULL)
1da177e4 254 goto out;
655b5bb4 255 msg = filp->private_data;
1da177e4 256 if (msg != NULL) {
ba9e0975 257 spin_lock(&pipe->lock);
48e49187 258 msg->errno = -EAGAIN;
5a67657a 259 list_del_init(&msg->list);
ba9e0975
SK
260 spin_unlock(&pipe->lock);
261 pipe->ops->destroy_msg(msg);
1da177e4
LT
262 }
263 if (filp->f_mode & FMODE_WRITE)
ba9e0975 264 pipe->nwriters --;
9842ef35 265 if (filp->f_mode & FMODE_READ) {
ba9e0975
SK
266 pipe->nreaders --;
267 if (pipe->nreaders == 0) {
9842ef35 268 LIST_HEAD(free_list);
ba9e0975
SK
269 spin_lock(&pipe->lock);
270 list_splice_init(&pipe->pipe, &free_list);
271 pipe->pipelen = 0;
272 spin_unlock(&pipe->lock);
273 rpc_purge_list(pipe, &free_list,
274 pipe->ops->destroy_msg, -EAGAIN);
9842ef35
TM
275 }
276 }
ba9e0975
SK
277 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
278 if (last_close && pipe->ops->release_pipe)
279 pipe->ops->release_pipe(inode);
1da177e4 280out:
1b1dcc1b 281 mutex_unlock(&inode->i_mutex);
1da177e4
LT
282 return 0;
283}
284
285static ssize_t
286rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
287{
303b46bb 288 struct inode *inode = filp->f_path.dentry->d_inode;
d0fe13ba 289 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
1da177e4
LT
290 struct rpc_pipe_msg *msg;
291 int res = 0;
292
1b1dcc1b 293 mutex_lock(&inode->i_mutex);
d0fe13ba 294 if (pipe->ops == NULL) {
1da177e4
LT
295 res = -EPIPE;
296 goto out_unlock;
297 }
298 msg = filp->private_data;
299 if (msg == NULL) {
d0fe13ba
SK
300 spin_lock(&pipe->lock);
301 if (!list_empty(&pipe->pipe)) {
302 msg = list_entry(pipe->pipe.next,
1da177e4
LT
303 struct rpc_pipe_msg,
304 list);
d0fe13ba
SK
305 list_move(&msg->list, &pipe->in_upcall);
306 pipe->pipelen -= msg->len;
1da177e4
LT
307 filp->private_data = msg;
308 msg->copied = 0;
309 }
d0fe13ba 310 spin_unlock(&pipe->lock);
1da177e4
LT
311 if (msg == NULL)
312 goto out_unlock;
313 }
314 /* NOTE: it is up to the callback to update msg->copied */
d0fe13ba 315 res = pipe->ops->upcall(filp, msg, buf, len);
1da177e4
LT
316 if (res < 0 || msg->len == msg->copied) {
317 filp->private_data = NULL;
d0fe13ba 318 spin_lock(&pipe->lock);
5a67657a 319 list_del_init(&msg->list);
d0fe13ba
SK
320 spin_unlock(&pipe->lock);
321 pipe->ops->destroy_msg(msg);
1da177e4
LT
322 }
323out_unlock:
1b1dcc1b 324 mutex_unlock(&inode->i_mutex);
1da177e4
LT
325 return res;
326}
327
328static ssize_t
329rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
330{
303b46bb 331 struct inode *inode = filp->f_path.dentry->d_inode;
d0fe13ba 332 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
1da177e4
LT
333 int res;
334
1b1dcc1b 335 mutex_lock(&inode->i_mutex);
1da177e4 336 res = -EPIPE;
d0fe13ba
SK
337 if (pipe->ops != NULL)
338 res = pipe->ops->downcall(filp, buf, len);
1b1dcc1b 339 mutex_unlock(&inode->i_mutex);
1da177e4
LT
340 return res;
341}
342
343static unsigned int
344rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
345{
d0fe13ba 346 struct rpc_pipe *pipe = RPC_I(filp->f_path.dentry->d_inode)->pipe;
1da177e4
LT
347 unsigned int mask = 0;
348
d0fe13ba 349 poll_wait(filp, &pipe->waitq, wait);
1da177e4
LT
350
351 mask = POLLOUT | POLLWRNORM;
d0fe13ba 352 if (pipe->ops == NULL)
1da177e4 353 mask |= POLLERR | POLLHUP;
d0fe13ba 354 if (filp->private_data || !list_empty(&pipe->pipe))
1da177e4
LT
355 mask |= POLLIN | POLLRDNORM;
356 return mask;
357}
358
a6f8dbc6
AB
359static long
360rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1da177e4 361{
a6f8dbc6 362 struct inode *inode = filp->f_path.dentry->d_inode;
d0fe13ba 363 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
1da177e4
LT
364 int len;
365
366 switch (cmd) {
367 case FIONREAD:
d0fe13ba
SK
368 spin_lock(&pipe->lock);
369 if (pipe->ops == NULL) {
370 spin_unlock(&pipe->lock);
1da177e4 371 return -EPIPE;
a6f8dbc6 372 }
d0fe13ba 373 len = pipe->pipelen;
1da177e4
LT
374 if (filp->private_data) {
375 struct rpc_pipe_msg *msg;
655b5bb4 376 msg = filp->private_data;
1da177e4
LT
377 len += msg->len - msg->copied;
378 }
d0fe13ba 379 spin_unlock(&pipe->lock);
1da177e4
LT
380 return put_user(len, (int __user *)arg);
381 default:
382 return -EINVAL;
383 }
384}
385
da7071d7 386static const struct file_operations rpc_pipe_fops = {
1da177e4
LT
387 .owner = THIS_MODULE,
388 .llseek = no_llseek,
389 .read = rpc_pipe_read,
390 .write = rpc_pipe_write,
391 .poll = rpc_pipe_poll,
674b604c 392 .unlocked_ioctl = rpc_pipe_ioctl,
1da177e4
LT
393 .open = rpc_pipe_open,
394 .release = rpc_pipe_release,
395};
396
397static int
398rpc_show_info(struct seq_file *m, void *v)
399{
400 struct rpc_clnt *clnt = m->private;
401
402 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
403 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
404 clnt->cl_prog, clnt->cl_vers);
e7f78657
CL
405 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
406 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
bf19aace 407 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
1da177e4
LT
408 return 0;
409}
410
411static int
412rpc_info_open(struct inode *inode, struct file *file)
413{
006abe88 414 struct rpc_clnt *clnt = NULL;
1da177e4
LT
415 int ret = single_open(file, rpc_show_info, NULL);
416
417 if (!ret) {
418 struct seq_file *m = file->private_data;
006abe88
TM
419
420 spin_lock(&file->f_path.dentry->d_lock);
421 if (!d_unhashed(file->f_path.dentry))
422 clnt = RPC_I(inode)->private;
423 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
424 spin_unlock(&file->f_path.dentry->d_lock);
1da177e4
LT
425 m->private = clnt;
426 } else {
006abe88 427 spin_unlock(&file->f_path.dentry->d_lock);
1da177e4
LT
428 single_release(inode, file);
429 ret = -EINVAL;
430 }
1da177e4
LT
431 }
432 return ret;
433}
434
435static int
436rpc_info_release(struct inode *inode, struct file *file)
437{
438 struct seq_file *m = file->private_data;
439 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
440
441 if (clnt)
442 rpc_release_client(clnt);
443 return single_release(inode, file);
444}
445
da7071d7 446static const struct file_operations rpc_info_operations = {
1da177e4
LT
447 .owner = THIS_MODULE,
448 .open = rpc_info_open,
449 .read = seq_read,
450 .llseek = seq_lseek,
451 .release = rpc_info_release,
452};
453
454
1da177e4
LT
455/*
456 * Description of fs contents.
457 */
458struct rpc_filelist {
ac6fecee 459 const char *name;
99ac48f5 460 const struct file_operations *i_fop;
7364af6a 461 umode_t mode;
1da177e4
LT
462};
463
54281548 464struct vfsmount *rpc_get_mount(void)
1da177e4 465{
54281548
TM
466 int err;
467
fc14f2fe 468 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
54281548
TM
469 if (err != 0)
470 return ERR_PTR(err);
fc14f2fe 471 return rpc_mnt;
1da177e4 472}
e571cbf1 473EXPORT_SYMBOL_GPL(rpc_get_mount);
1da177e4 474
54281548 475void rpc_put_mount(void)
1da177e4 476{
fc14f2fe 477 simple_release_fs(&rpc_mnt, &rpc_mount_count);
1da177e4 478}
e571cbf1 479EXPORT_SYMBOL_GPL(rpc_put_mount);
1da177e4 480
fe15ce44 481static int rpc_delete_dentry(const struct dentry *dentry)
62e1761c
TM
482{
483 return 1;
484}
485
3ba13d17 486static const struct dentry_operations rpc_dentry_operations = {
62e1761c
TM
487 .d_delete = rpc_delete_dentry,
488};
489
1da177e4 490static struct inode *
7364af6a 491rpc_get_inode(struct super_block *sb, umode_t mode)
1da177e4
LT
492{
493 struct inode *inode = new_inode(sb);
494 if (!inode)
495 return NULL;
85fe4025 496 inode->i_ino = get_next_ino();
1da177e4 497 inode->i_mode = mode;
1da177e4 498 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
89f0e4fe
JP
499 switch (mode & S_IFMT) {
500 case S_IFDIR:
501 inode->i_fop = &simple_dir_operations;
502 inode->i_op = &simple_dir_inode_operations;
503 inc_nlink(inode);
504 default:
505 break;
1da177e4
LT
506 }
507 return inode;
508}
509
7589806e
TM
510static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
511 umode_t mode,
512 const struct file_operations *i_fop,
513 void *private)
514{
515 struct inode *inode;
516
beb0f0a9 517 d_drop(dentry);
7589806e
TM
518 inode = rpc_get_inode(dir->i_sb, mode);
519 if (!inode)
520 goto out_err;
521 inode->i_ino = iunique(dir->i_sb, 100);
522 if (i_fop)
523 inode->i_fop = i_fop;
524 if (private)
525 rpc_inode_setowner(inode, private);
526 d_add(dentry, inode);
527 return 0;
528out_err:
529 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
530 __FILE__, __func__, dentry->d_name.name);
531 dput(dentry);
532 return -ENOMEM;
533}
534
ac6fecee
TM
535static int __rpc_create(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_IFREG | mode, i_fop, private);
543 if (err)
544 return err;
545 fsnotify_create(dir, dentry);
546 return 0;
547}
548
7589806e
TM
549static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
550 umode_t mode,
551 const struct file_operations *i_fop,
552 void *private)
553{
554 int err;
555
556 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
557 if (err)
558 return err;
559 inc_nlink(dir);
560 fsnotify_mkdir(dir, dentry);
561 return 0;
562}
563
ba9e0975
SK
564static void
565init_pipe(struct rpc_pipe *pipe)
566{
567 pipe->nreaders = 0;
568 pipe->nwriters = 0;
569 INIT_LIST_HEAD(&pipe->in_upcall);
570 INIT_LIST_HEAD(&pipe->in_downcall);
571 INIT_LIST_HEAD(&pipe->pipe);
572 pipe->pipelen = 0;
573 init_waitqueue_head(&pipe->waitq);
574 INIT_DELAYED_WORK(&pipe->queue_timeout,
575 rpc_timeout_upcall_queue);
576 pipe->ops = NULL;
577 spin_lock_init(&pipe->lock);
578
579}
580
7589806e
TM
581static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
582 umode_t mode,
583 const struct file_operations *i_fop,
584 void *private,
585 const struct rpc_pipe_ops *ops,
586 int flags)
587{
ba9e0975 588 struct rpc_pipe *pipe;
7589806e
TM
589 struct rpc_inode *rpci;
590 int err;
591
ba9e0975
SK
592 pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
593 if (!pipe)
594 return -ENOMEM;
595 init_pipe(pipe);
7589806e 596 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
ba9e0975
SK
597 if (err) {
598 kfree(pipe);
7589806e 599 return err;
ba9e0975 600 }
7589806e 601 rpci = RPC_I(dentry->d_inode);
7589806e 602 rpci->private = private;
ba9e0975
SK
603 rpci->pipe = pipe;
604 rpci->pipe->flags = flags;
605 rpci->pipe->ops = ops;
7589806e
TM
606 fsnotify_create(dir, dentry);
607 return 0;
608}
609
ac6fecee
TM
610static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
611{
612 int ret;
613
614 dget(dentry);
615 ret = simple_rmdir(dir, dentry);
616 d_delete(dentry);
617 dput(dentry);
618 return ret;
619}
620
810d90bc
TM
621static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
622{
623 int ret;
624
625 dget(dentry);
626 ret = simple_unlink(dir, dentry);
627 d_delete(dentry);
628 dput(dentry);
629 return ret;
630}
631
632static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
633{
634 struct inode *inode = dentry->d_inode;
810d90bc 635
810d90bc
TM
636 rpc_close_pipes(inode);
637 return __rpc_unlink(dir, dentry);
638}
639
5bff0386 640static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
cfeaa4a3
TM
641 struct qstr *name)
642{
643 struct dentry *dentry;
644
645 dentry = d_lookup(parent, name);
646 if (!dentry) {
647 dentry = d_alloc(parent, name);
5bff0386
SK
648 if (!dentry)
649 return ERR_PTR(-ENOMEM);
cfeaa4a3 650 }
5bff0386 651 if (dentry->d_inode == NULL) {
fb045adb 652 d_set_d_op(dentry, &rpc_dentry_operations);
cfeaa4a3 653 return dentry;
5bff0386 654 }
cfeaa4a3
TM
655 dput(dentry);
656 return ERR_PTR(-EEXIST);
657}
658
1da177e4
LT
659/*
660 * FIXME: This probably has races.
661 */
ac6fecee
TM
662static void __rpc_depopulate(struct dentry *parent,
663 const struct rpc_filelist *files,
664 int start, int eof)
1da177e4
LT
665{
666 struct inode *dir = parent->d_inode;
ac6fecee
TM
667 struct dentry *dentry;
668 struct qstr name;
669 int i;
1da177e4 670
ac6fecee
TM
671 for (i = start; i < eof; i++) {
672 name.name = files[i].name;
673 name.len = strlen(files[i].name);
674 name.hash = full_name_hash(name.name, name.len);
675 dentry = d_lookup(parent, &name);
676
677 if (dentry == NULL)
62e1761c 678 continue;
ac6fecee
TM
679 if (dentry->d_inode == NULL)
680 goto next;
681 switch (dentry->d_inode->i_mode & S_IFMT) {
682 default:
683 BUG();
684 case S_IFREG:
685 __rpc_unlink(dir, dentry);
1da177e4 686 break;
ac6fecee
TM
687 case S_IFDIR:
688 __rpc_rmdir(dir, dentry);
689 }
690next:
691 dput(dentry);
1da177e4 692 }
ac6fecee
TM
693}
694
695static void rpc_depopulate(struct dentry *parent,
696 const struct rpc_filelist *files,
697 int start, int eof)
698{
699 struct inode *dir = parent->d_inode;
700
701 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
702 __rpc_depopulate(parent, files, start, eof);
1b1dcc1b 703 mutex_unlock(&dir->i_mutex);
1da177e4
LT
704}
705
ac6fecee
TM
706static int rpc_populate(struct dentry *parent,
707 const struct rpc_filelist *files,
708 int start, int eof,
709 void *private)
1da177e4 710{
ac6fecee 711 struct inode *dir = parent->d_inode;
1da177e4 712 struct dentry *dentry;
ac6fecee 713 int i, err;
1da177e4 714
1b1dcc1b 715 mutex_lock(&dir->i_mutex);
1da177e4 716 for (i = start; i < eof; i++) {
ac6fecee
TM
717 struct qstr q;
718
719 q.name = files[i].name;
720 q.len = strlen(files[i].name);
721 q.hash = full_name_hash(q.name, q.len);
722 dentry = __rpc_lookup_create_exclusive(parent, &q);
723 err = PTR_ERR(dentry);
724 if (IS_ERR(dentry))
1da177e4 725 goto out_bad;
ac6fecee
TM
726 switch (files[i].mode & S_IFMT) {
727 default:
728 BUG();
729 case S_IFREG:
730 err = __rpc_create(dir, dentry,
731 files[i].mode,
732 files[i].i_fop,
733 private);
734 break;
735 case S_IFDIR:
736 err = __rpc_mkdir(dir, dentry,
737 files[i].mode,
738 NULL,
739 private);
1da177e4 740 }
ac6fecee
TM
741 if (err != 0)
742 goto out_bad;
1da177e4 743 }
1b1dcc1b 744 mutex_unlock(&dir->i_mutex);
1da177e4
LT
745 return 0;
746out_bad:
ac6fecee 747 __rpc_depopulate(parent, files, start, eof);
1b1dcc1b 748 mutex_unlock(&dir->i_mutex);
1da177e4 749 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
0dc47877 750 __FILE__, __func__, parent->d_name.name);
ac6fecee 751 return err;
f134585a 752}
1da177e4 753
e57aed77
TM
754static struct dentry *rpc_mkdir_populate(struct dentry *parent,
755 struct qstr *name, umode_t mode, void *private,
756 int (*populate)(struct dentry *, void *), void *args_populate)
f134585a 757{
f134585a 758 struct dentry *dentry;
7d59d1e8 759 struct inode *dir = parent->d_inode;
f134585a
TM
760 int error;
761
7d59d1e8
TM
762 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
763 dentry = __rpc_lookup_create_exclusive(parent, name);
f134585a 764 if (IS_ERR(dentry))
7d59d1e8
TM
765 goto out;
766 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
7589806e
TM
767 if (error != 0)
768 goto out_err;
e57aed77
TM
769 if (populate != NULL) {
770 error = populate(dentry, args_populate);
771 if (error)
772 goto err_rmdir;
773 }
f134585a 774out:
1b1dcc1b 775 mutex_unlock(&dir->i_mutex);
5c3e985a 776 return dentry;
ac6fecee 777err_rmdir:
f134585a 778 __rpc_rmdir(dir, dentry);
7589806e 779out_err:
f134585a
TM
780 dentry = ERR_PTR(error);
781 goto out;
1da177e4
LT
782}
783
e57aed77
TM
784static int rpc_rmdir_depopulate(struct dentry *dentry,
785 void (*depopulate)(struct dentry *))
1da177e4 786{
dff02cc1 787 struct dentry *parent;
f134585a
TM
788 struct inode *dir;
789 int error;
278c995c 790
dff02cc1
TM
791 parent = dget_parent(dentry);
792 dir = parent->d_inode;
c6573c29 793 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
e57aed77
TM
794 if (depopulate != NULL)
795 depopulate(dentry);
f134585a 796 error = __rpc_rmdir(dir, dentry);
1b1dcc1b 797 mutex_unlock(&dir->i_mutex);
dff02cc1 798 dput(parent);
f134585a 799 return error;
1da177e4
LT
800}
801
93a44a75
BF
802/**
803 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
804 * @parent: dentry of directory to create new "pipe" in
805 * @name: name of pipe
806 * @private: private data to associate with the pipe, for the caller's use
807 * @ops: operations defining the behavior of the pipe: upcall, downcall,
c3810608 808 * release_pipe, open_pipe, and destroy_msg.
d0fe13ba 809 * @flags: rpc_pipe flags
93a44a75
BF
810 *
811 * Data is made available for userspace to read by calls to
812 * rpc_queue_upcall(). The actual reads will result in calls to
813 * @ops->upcall, which will be called with the file pointer,
814 * message, and userspace buffer to copy to.
815 *
816 * Writes can come at any time, and do not necessarily have to be
817 * responses to upcalls. They will result in calls to @msg->downcall.
818 *
819 * The @private argument passed here will be available to all these methods
820 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
821 */
b693ba4a
TM
822struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
823 void *private, const struct rpc_pipe_ops *ops,
824 int flags)
1da177e4 825{
1da177e4 826 struct dentry *dentry;
7589806e 827 struct inode *dir = parent->d_inode;
7364af6a 828 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
cfeaa4a3 829 struct qstr q;
7589806e 830 int err;
7364af6a
TM
831
832 if (ops->upcall == NULL)
833 umode &= ~S_IRUGO;
834 if (ops->downcall == NULL)
835 umode &= ~S_IWUGO;
1da177e4 836
cfeaa4a3
TM
837 q.name = name;
838 q.len = strlen(name);
839 q.hash = full_name_hash(q.name, q.len),
840
841 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
5bff0386 842 dentry = __rpc_lookup_create_exclusive(parent, &q);
1da177e4 843 if (IS_ERR(dentry))
cfeaa4a3 844 goto out;
7589806e 845 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
810d90bc 846 private, ops, flags);
7589806e
TM
847 if (err)
848 goto out_err;
f134585a 849out:
1b1dcc1b 850 mutex_unlock(&dir->i_mutex);
5c3e985a 851 return dentry;
7589806e
TM
852out_err:
853 dentry = ERR_PTR(err);
158998b6 854 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
0dc47877 855 __FILE__, __func__, parent->d_name.name, name,
7589806e 856 err);
f134585a 857 goto out;
1da177e4 858}
468039ee 859EXPORT_SYMBOL_GPL(rpc_mkpipe);
1da177e4 860
93a44a75
BF
861/**
862 * rpc_unlink - remove a pipe
863 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
864 *
865 * After this call, lookups will no longer find the pipe, and any
866 * attempts to read or write using preexisting opens of the pipe will
867 * return -EPIPE.
868 */
f134585a 869int
5d67476f 870rpc_unlink(struct dentry *dentry)
1da177e4 871{
5d67476f 872 struct dentry *parent;
f134585a 873 struct inode *dir;
5d67476f 874 int error = 0;
1da177e4 875
5d67476f
TM
876 parent = dget_parent(dentry);
877 dir = parent->d_inode;
c6573c29 878 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
810d90bc 879 error = __rpc_rmpipe(dir, dentry);
1b1dcc1b 880 mutex_unlock(&dir->i_mutex);
5d67476f 881 dput(parent);
f134585a 882 return error;
1da177e4 883}
468039ee 884EXPORT_SYMBOL_GPL(rpc_unlink);
1da177e4 885
e57aed77
TM
886enum {
887 RPCAUTH_info,
888 RPCAUTH_EOF
889};
890
891static const struct rpc_filelist authfiles[] = {
892 [RPCAUTH_info] = {
893 .name = "info",
894 .i_fop = &rpc_info_operations,
895 .mode = S_IFREG | S_IRUSR,
896 },
897};
898
899static int rpc_clntdir_populate(struct dentry *dentry, void *private)
900{
901 return rpc_populate(dentry,
902 authfiles, RPCAUTH_info, RPCAUTH_EOF,
903 private);
904}
905
906static void rpc_clntdir_depopulate(struct dentry *dentry)
907{
908 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
909}
910
7d59d1e8
TM
911/**
912 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
4111d4fd
RD
913 * @dentry: dentry from the rpc_pipefs root to the new directory
914 * @name: &struct qstr for the name
7d59d1e8
TM
915 * @rpc_client: rpc client to associate with this directory
916 *
917 * This creates a directory at the given @path associated with
918 * @rpc_clnt, which will contain a file named "info" with some basic
919 * information about the client, together with any "pipes" that may
920 * later be created using rpc_mkpipe().
921 */
23ac6581 922struct dentry *rpc_create_client_dir(struct dentry *dentry,
e57aed77
TM
923 struct qstr *name,
924 struct rpc_clnt *rpc_client)
925{
926 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
927 rpc_clntdir_populate, rpc_client);
928}
929
930/**
931 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
932 * @dentry: directory to remove
933 */
934int rpc_remove_client_dir(struct dentry *dentry)
7d59d1e8 935{
e57aed77 936 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
7d59d1e8
TM
937}
938
8854e82d
TM
939static const struct rpc_filelist cache_pipefs_files[3] = {
940 [0] = {
941 .name = "channel",
942 .i_fop = &cache_file_operations_pipefs,
96c61cbd 943 .mode = S_IFREG|S_IRUSR|S_IWUSR,
8854e82d
TM
944 },
945 [1] = {
946 .name = "content",
947 .i_fop = &content_file_operations_pipefs,
948 .mode = S_IFREG|S_IRUSR,
949 },
950 [2] = {
951 .name = "flush",
952 .i_fop = &cache_flush_operations_pipefs,
953 .mode = S_IFREG|S_IRUSR|S_IWUSR,
954 },
955};
956
957static int rpc_cachedir_populate(struct dentry *dentry, void *private)
958{
959 return rpc_populate(dentry,
960 cache_pipefs_files, 0, 3,
961 private);
962}
963
964static void rpc_cachedir_depopulate(struct dentry *dentry)
965{
966 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
967}
968
969struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
64f1426f 970 umode_t umode, struct cache_detail *cd)
8854e82d
TM
971{
972 return rpc_mkdir_populate(parent, name, umode, NULL,
973 rpc_cachedir_populate, cd);
974}
975
976void rpc_remove_cache_dir(struct dentry *dentry)
977{
978 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
979}
980
1da177e4
LT
981/*
982 * populate the filesystem
983 */
b87221de 984static const struct super_operations s_ops = {
1da177e4
LT
985 .alloc_inode = rpc_alloc_inode,
986 .destroy_inode = rpc_destroy_inode,
987 .statfs = simple_statfs,
988};
989
990#define RPCAUTH_GSSMAGIC 0x67596969
991
bb156749
TM
992/*
993 * We have a single directory with 1 node in it.
994 */
995enum {
996 RPCAUTH_lockd,
997 RPCAUTH_mount,
998 RPCAUTH_nfs,
999 RPCAUTH_portmap,
1000 RPCAUTH_statd,
1001 RPCAUTH_nfsd4_cb,
e571cbf1 1002 RPCAUTH_cache,
bb156749
TM
1003 RPCAUTH_RootEOF
1004};
1005
1006static const struct rpc_filelist files[] = {
1007 [RPCAUTH_lockd] = {
1008 .name = "lockd",
1009 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1010 },
1011 [RPCAUTH_mount] = {
1012 .name = "mount",
1013 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1014 },
1015 [RPCAUTH_nfs] = {
1016 .name = "nfs",
1017 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1018 },
1019 [RPCAUTH_portmap] = {
1020 .name = "portmap",
1021 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1022 },
1023 [RPCAUTH_statd] = {
1024 .name = "statd",
1025 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1026 },
1027 [RPCAUTH_nfsd4_cb] = {
1028 .name = "nfsd4_cb",
1029 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1030 },
e571cbf1
TM
1031 [RPCAUTH_cache] = {
1032 .name = "cache",
1033 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1034 },
bb156749
TM
1035};
1036
432eb1a5
SK
1037/*
1038 * This call can be used only in RPC pipefs mount notification hooks.
1039 */
1040struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1041 const unsigned char *dir_name)
1042{
1043 struct qstr dir = {
1044 .name = dir_name,
1045 .len = strlen(dir_name),
1046 .hash = full_name_hash(dir_name, strlen(dir_name)),
1047 };
1048
1049 return d_lookup(sb->s_root, &dir);
1050}
1051EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1052
c21a588f
SK
1053void rpc_pipefs_init_net(struct net *net)
1054{
1055 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1056
1057 mutex_init(&sn->pipefs_sb_lock);
1058}
1059
1060/*
1061 * This call will be used for per network namespace operations calls.
1062 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1063 * found. This lock have to be released by rpc_put_sb_net() when all operations
1064 * will be completed.
1065 */
1066struct super_block *rpc_get_sb_net(const struct net *net)
1067{
1068 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1069
1070 mutex_lock(&sn->pipefs_sb_lock);
1071 if (sn->pipefs_sb)
1072 return sn->pipefs_sb;
1073 mutex_unlock(&sn->pipefs_sb_lock);
1074 return NULL;
1075}
1076EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1077
1078void rpc_put_sb_net(const struct net *net)
1079{
1080 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1081
1082 BUG_ON(sn->pipefs_sb == NULL);
1083 mutex_unlock(&sn->pipefs_sb_lock);
1084}
1085EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1086
1da177e4
LT
1087static int
1088rpc_fill_super(struct super_block *sb, void *data, int silent)
1089{
1090 struct inode *inode;
1091 struct dentry *root;
38b0da75 1092 struct net *net = data;
90c4e829 1093 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2d00131a 1094 int err;
1da177e4
LT
1095
1096 sb->s_blocksize = PAGE_CACHE_SIZE;
1097 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1098 sb->s_magic = RPCAUTH_GSSMAGIC;
1099 sb->s_op = &s_ops;
1100 sb->s_time_gran = 1;
1101
1102 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1103 if (!inode)
1104 return -ENOMEM;
fc7bed8c 1105 sb->s_root = root = d_alloc_root(inode);
1da177e4
LT
1106 if (!root) {
1107 iput(inode);
1108 return -ENOMEM;
1109 }
ac6fecee 1110 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
fc7bed8c 1111 return -ENOMEM;
efc46bf2
SK
1112 dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net,
1113 NET_NAME(net));
2d00131a
SK
1114 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1115 RPC_PIPEFS_MOUNT,
1116 sb);
1117 if (err)
1118 goto err_depopulate;
021c68de 1119 sb->s_fs_info = get_net(net);
90c4e829 1120 sn->pipefs_sb = sb;
1da177e4 1121 return 0;
2d00131a
SK
1122
1123err_depopulate:
1124 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1125 RPC_PIPEFS_UMOUNT,
1126 sb);
1127 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1128 return err;
1da177e4
LT
1129}
1130
fc14f2fe
AV
1131static struct dentry *
1132rpc_mount(struct file_system_type *fs_type,
1133 int flags, const char *dev_name, void *data)
1da177e4 1134{
38b0da75 1135 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
1da177e4
LT
1136}
1137
021c68de
SK
1138void rpc_kill_sb(struct super_block *sb)
1139{
1140 struct net *net = sb->s_fs_info;
90c4e829 1141 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
021c68de 1142
c21a588f 1143 mutex_lock(&sn->pipefs_sb_lock);
90c4e829 1144 sn->pipefs_sb = NULL;
c21a588f 1145 mutex_unlock(&sn->pipefs_sb_lock);
021c68de 1146 put_net(net);
efc46bf2
SK
1147 dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net,
1148 NET_NAME(net));
2d00131a
SK
1149 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1150 RPC_PIPEFS_UMOUNT,
1151 sb);
021c68de
SK
1152 kill_litter_super(sb);
1153}
1154
1da177e4
LT
1155static struct file_system_type rpc_pipe_fs_type = {
1156 .owner = THIS_MODULE,
1157 .name = "rpc_pipefs",
fc14f2fe 1158 .mount = rpc_mount,
021c68de 1159 .kill_sb = rpc_kill_sb,
1da177e4
LT
1160};
1161
1162static void
51cc5068 1163init_once(void *foo)
1da177e4
LT
1164{
1165 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1166
a35afb83
CL
1167 inode_init_once(&rpci->vfs_inode);
1168 rpci->private = NULL;
ba9e0975 1169 rpci->pipe = NULL;
1da177e4
LT
1170}
1171
1172int register_rpc_pipefs(void)
1173{
5bd5f581
AM
1174 int err;
1175
1da177e4 1176 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
fffb60f9
PJ
1177 sizeof(struct rpc_inode),
1178 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1179 SLAB_MEM_SPREAD),
20c2df83 1180 init_once);
1da177e4
LT
1181 if (!rpc_inode_cachep)
1182 return -ENOMEM;
5bd5f581
AM
1183 err = register_filesystem(&rpc_pipe_fs_type);
1184 if (err) {
1185 kmem_cache_destroy(rpc_inode_cachep);
1186 return err;
1187 }
1188
1da177e4
LT
1189 return 0;
1190}
1191
1192void unregister_rpc_pipefs(void)
1193{
1a1d92c1 1194 kmem_cache_destroy(rpc_inode_cachep);
1da177e4
LT
1195 unregister_filesystem(&rpc_pipe_fs_type);
1196}
dcbf8c30
MS
1197
1198/* Make 'mount -t rpc_pipefs ...' autoload this module. */
1199MODULE_ALIAS("rpc_pipefs");