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