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