vfs: Convert rpc_pipefs to use the new mount API
[linux-2.6-block.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>
b9662f31 16#include <linux/fs_context.h>
1da177e4 17#include <linux/namei.h>
50e437d5 18#include <linux/fsnotify.h>
1da177e4 19#include <linux/kernel.h>
2446ab60 20#include <linux/rcupdate.h>
e2f0c83a 21#include <linux/utsname.h>
1da177e4
LT
22
23#include <asm/ioctls.h>
1da177e4
LT
24#include <linux/poll.h>
25#include <linux/wait.h>
26#include <linux/seq_file.h>
27
28#include <linux/sunrpc/clnt.h>
29#include <linux/workqueue.h>
30#include <linux/sunrpc/rpc_pipe_fs.h>
8854e82d 31#include <linux/sunrpc/cache.h>
021c68de 32#include <linux/nsproxy.h>
2d00131a 33#include <linux/notifier.h>
1da177e4 34
021c68de 35#include "netns.h"
2d00131a 36#include "sunrpc.h"
1da177e4 37
efc46bf2
SK
38#define RPCDBG_FACILITY RPCDBG_DEBUG
39
40#define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
1da177e4
LT
41
42static struct file_system_type rpc_pipe_fs_type;
4b9a445e 43static const struct rpc_pipe_ops gssd_dummy_pipe_ops;
1da177e4 44
e18b890b 45static struct kmem_cache *rpc_inode_cachep __read_mostly;
1da177e4
LT
46
47#define RPC_UPCALL_TIMEOUT (30*HZ)
48
2d00131a
SK
49static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
50
51int rpc_pipefs_notifier_register(struct notifier_block *nb)
52{
53 return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
54}
55EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
56
57void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
58{
59 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
60}
61EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
62
591ad7fe 63static void rpc_purge_list(wait_queue_head_t *waitq, struct list_head *head,
9842ef35 64 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
1da177e4 65{
1da177e4
LT
66 struct rpc_pipe_msg *msg;
67
9842ef35
TM
68 if (list_empty(head))
69 return;
70 do {
b3eb67a2 71 msg = list_entry(head->next, struct rpc_pipe_msg, list);
5a67657a 72 list_del_init(&msg->list);
1da177e4 73 msg->errno = err;
b3eb67a2 74 destroy_msg(msg);
9842ef35 75 } while (!list_empty(head));
92123e06
JL
76
77 if (waitq)
78 wake_up(waitq);
1da177e4
LT
79}
80
81static void
65f27f38 82rpc_timeout_upcall_queue(struct work_struct *work)
1da177e4 83{
9842ef35 84 LIST_HEAD(free_list);
ba9e0975
SK
85 struct rpc_pipe *pipe =
86 container_of(work, struct rpc_pipe, queue_timeout.work);
9842ef35 87 void (*destroy_msg)(struct rpc_pipe_msg *);
591ad7fe 88 struct dentry *dentry;
1da177e4 89
ba9e0975 90 spin_lock(&pipe->lock);
ba9e0975
SK
91 destroy_msg = pipe->ops->destroy_msg;
92 if (pipe->nreaders == 0) {
93 list_splice_init(&pipe->pipe, &free_list);
94 pipe->pipelen = 0;
9842ef35 95 }
591ad7fe 96 dentry = dget(pipe->dentry);
ba9e0975 97 spin_unlock(&pipe->lock);
c5ef6035 98 rpc_purge_list(dentry ? &RPC_I(d_inode(dentry))->waitq : NULL,
92123e06
JL
99 &free_list, destroy_msg, -ETIMEDOUT);
100 dput(dentry);
1da177e4
LT
101}
102
c1225158
PT
103ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
104 char __user *dst, size_t buflen)
105{
106 char *data = (char *)msg->data + msg->copied;
107 size_t mlen = min(msg->len - msg->copied, buflen);
108 unsigned long left;
109
110 left = copy_to_user(dst, data, mlen);
111 if (left == mlen) {
112 msg->errno = -EFAULT;
113 return -EFAULT;
114 }
115
116 mlen -= left;
117 msg->copied += mlen;
118 msg->errno = 0;
119 return mlen;
120}
121EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
122
93a44a75 123/**
1a5778aa 124 * rpc_queue_upcall - queue an upcall message to userspace
bda14606 125 * @pipe: upcall pipe on which to queue given message
93a44a75
BF
126 * @msg: message to queue
127 *
128 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
129 * A userspace process may then later read the upcall by performing a
130 * read on an open file for this inode. It is up to the caller to
131 * initialize the fields of @msg (other than @msg->list) appropriately.
132 */
1da177e4 133int
d706ed1f 134rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
1da177e4 135{
6070fe6f 136 int res = -EPIPE;
591ad7fe 137 struct dentry *dentry;
1da177e4 138
d0fe13ba 139 spin_lock(&pipe->lock);
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 }
591ad7fe 153 dentry = dget(pipe->dentry);
d0fe13ba 154 spin_unlock(&pipe->lock);
591ad7fe 155 if (dentry) {
c5ef6035 156 wake_up(&RPC_I(d_inode(dentry))->waitq);
591ad7fe
SK
157 dput(dentry);
158 }
1da177e4
LT
159 return res;
160}
468039ee 161EXPORT_SYMBOL_GPL(rpc_queue_upcall);
1da177e4 162
6070fe6f
TM
163static inline void
164rpc_inode_setowner(struct inode *inode, void *private)
165{
166 RPC_I(inode)->private = private;
167}
168
1da177e4
LT
169static void
170rpc_close_pipes(struct inode *inode)
171{
ba9e0975 172 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
e712804a 173 int need_release;
ad6b1340 174 LIST_HEAD(free_list);
1da177e4 175
5955102c 176 inode_lock(inode);
ad6b1340
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->dentry = NULL;
184 spin_unlock(&pipe->lock);
591ad7fe 185 rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -EPIPE);
ad6b1340
SK
186 pipe->nwriters = 0;
187 if (need_release && pipe->ops->release_pipe)
188 pipe->ops->release_pipe(inode);
189 cancel_delayed_work_sync(&pipe->queue_timeout);
6070fe6f 190 rpc_inode_setowner(inode, NULL);
2c9030ee 191 RPC_I(inode)->pipe = NULL;
5955102c 192 inode_unlock(inode);
1da177e4
LT
193}
194
1da177e4
LT
195static struct inode *
196rpc_alloc_inode(struct super_block *sb)
197{
198 struct rpc_inode *rpci;
adcda652 199 rpci = 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
bef252fa 206rpc_free_inode(struct inode *inode)
1da177e4
LT
207{
208 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
209}
210
211static int
212rpc_pipe_open(struct inode *inode, struct file *filp)
213{
2c9030ee 214 struct rpc_pipe *pipe;
c3810608 215 int first_open;
1da177e4
LT
216 int res = -ENXIO;
217
5955102c 218 inode_lock(inode);
2c9030ee
SK
219 pipe = RPC_I(inode)->pipe;
220 if (pipe == NULL)
c3810608 221 goto out;
d0fe13ba
SK
222 first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
223 if (first_open && pipe->ops->open_pipe) {
224 res = pipe->ops->open_pipe(inode);
c3810608
BF
225 if (res)
226 goto out;
1da177e4 227 }
c3810608 228 if (filp->f_mode & FMODE_READ)
d0fe13ba 229 pipe->nreaders++;
c3810608 230 if (filp->f_mode & FMODE_WRITE)
d0fe13ba 231 pipe->nwriters++;
c3810608
BF
232 res = 0;
233out:
5955102c 234 inode_unlock(inode);
1da177e4
LT
235 return res;
236}
237
238static int
239rpc_pipe_release(struct inode *inode, struct file *filp)
240{
2c9030ee 241 struct rpc_pipe *pipe;
1da177e4 242 struct rpc_pipe_msg *msg;
e712804a 243 int last_close;
1da177e4 244
5955102c 245 inode_lock(inode);
2c9030ee
SK
246 pipe = RPC_I(inode)->pipe;
247 if (pipe == NULL)
1da177e4 248 goto out;
655b5bb4 249 msg = filp->private_data;
1da177e4 250 if (msg != NULL) {
ba9e0975 251 spin_lock(&pipe->lock);
48e49187 252 msg->errno = -EAGAIN;
5a67657a 253 list_del_init(&msg->list);
ba9e0975
SK
254 spin_unlock(&pipe->lock);
255 pipe->ops->destroy_msg(msg);
1da177e4
LT
256 }
257 if (filp->f_mode & FMODE_WRITE)
ba9e0975 258 pipe->nwriters --;
9842ef35 259 if (filp->f_mode & FMODE_READ) {
ba9e0975
SK
260 pipe->nreaders --;
261 if (pipe->nreaders == 0) {
9842ef35 262 LIST_HEAD(free_list);
ba9e0975
SK
263 spin_lock(&pipe->lock);
264 list_splice_init(&pipe->pipe, &free_list);
265 pipe->pipelen = 0;
266 spin_unlock(&pipe->lock);
591ad7fe 267 rpc_purge_list(&RPC_I(inode)->waitq, &free_list,
ba9e0975 268 pipe->ops->destroy_msg, -EAGAIN);
9842ef35
TM
269 }
270 }
ba9e0975
SK
271 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
272 if (last_close && pipe->ops->release_pipe)
273 pipe->ops->release_pipe(inode);
1da177e4 274out:
5955102c 275 inode_unlock(inode);
1da177e4
LT
276 return 0;
277}
278
279static ssize_t
280rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
281{
496ad9aa 282 struct inode *inode = file_inode(filp);
2c9030ee 283 struct rpc_pipe *pipe;
1da177e4
LT
284 struct rpc_pipe_msg *msg;
285 int res = 0;
286
5955102c 287 inode_lock(inode);
2c9030ee
SK
288 pipe = RPC_I(inode)->pipe;
289 if (pipe == NULL) {
1da177e4
LT
290 res = -EPIPE;
291 goto out_unlock;
292 }
293 msg = filp->private_data;
294 if (msg == NULL) {
d0fe13ba
SK
295 spin_lock(&pipe->lock);
296 if (!list_empty(&pipe->pipe)) {
297 msg = list_entry(pipe->pipe.next,
1da177e4
LT
298 struct rpc_pipe_msg,
299 list);
d0fe13ba
SK
300 list_move(&msg->list, &pipe->in_upcall);
301 pipe->pipelen -= msg->len;
1da177e4
LT
302 filp->private_data = msg;
303 msg->copied = 0;
304 }
d0fe13ba 305 spin_unlock(&pipe->lock);
1da177e4
LT
306 if (msg == NULL)
307 goto out_unlock;
308 }
309 /* NOTE: it is up to the callback to update msg->copied */
d0fe13ba 310 res = pipe->ops->upcall(filp, msg, buf, len);
1da177e4
LT
311 if (res < 0 || msg->len == msg->copied) {
312 filp->private_data = NULL;
d0fe13ba 313 spin_lock(&pipe->lock);
5a67657a 314 list_del_init(&msg->list);
d0fe13ba
SK
315 spin_unlock(&pipe->lock);
316 pipe->ops->destroy_msg(msg);
1da177e4
LT
317 }
318out_unlock:
5955102c 319 inode_unlock(inode);
1da177e4
LT
320 return res;
321}
322
323static ssize_t
324rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
325{
496ad9aa 326 struct inode *inode = file_inode(filp);
1da177e4
LT
327 int res;
328
5955102c 329 inode_lock(inode);
1da177e4 330 res = -EPIPE;
2c9030ee
SK
331 if (RPC_I(inode)->pipe != NULL)
332 res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
5955102c 333 inode_unlock(inode);
1da177e4
LT
334 return res;
335}
336
ade994f4 337static __poll_t
1da177e4
LT
338rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
339{
496ad9aa 340 struct inode *inode = file_inode(filp);
591ad7fe 341 struct rpc_inode *rpci = RPC_I(inode);
a9a08845 342 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
1da177e4 343
1da177e4
LT
344 poll_wait(filp, &rpci->waitq, wait);
345
5955102c 346 inode_lock(inode);
591ad7fe 347 if (rpci->pipe == NULL)
a9a08845 348 mask |= EPOLLERR | EPOLLHUP;
591ad7fe 349 else if (filp->private_data || !list_empty(&rpci->pipe->pipe))
a9a08845 350 mask |= EPOLLIN | EPOLLRDNORM;
5955102c 351 inode_unlock(inode);
1da177e4
LT
352 return mask;
353}
354
a6f8dbc6
AB
355static long
356rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1da177e4 357{
496ad9aa 358 struct inode *inode = file_inode(filp);
2c9030ee 359 struct rpc_pipe *pipe;
1da177e4
LT
360 int len;
361
362 switch (cmd) {
363 case FIONREAD:
5955102c 364 inode_lock(inode);
2c9030ee
SK
365 pipe = RPC_I(inode)->pipe;
366 if (pipe == NULL) {
5955102c 367 inode_unlock(inode);
1da177e4 368 return -EPIPE;
a6f8dbc6 369 }
2c9030ee 370 spin_lock(&pipe->lock);
d0fe13ba 371 len = pipe->pipelen;
1da177e4
LT
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 }
d0fe13ba 377 spin_unlock(&pipe->lock);
5955102c 378 inode_unlock(inode);
1da177e4
LT
379 return put_user(len, (int __user *)arg);
380 default:
381 return -EINVAL;
382 }
383}
384
da7071d7 385static const struct file_operations rpc_pipe_fops = {
1da177e4
LT
386 .owner = THIS_MODULE,
387 .llseek = no_llseek,
388 .read = rpc_pipe_read,
389 .write = rpc_pipe_write,
390 .poll = rpc_pipe_poll,
674b604c 391 .unlocked_ioctl = rpc_pipe_ioctl,
1da177e4
LT
392 .open = rpc_pipe_open,
393 .release = rpc_pipe_release,
394};
395
396static int
397rpc_show_info(struct seq_file *m, void *v)
398{
399 struct rpc_clnt *clnt = m->private;
400
2446ab60 401 rcu_read_lock();
4e0038b6
TM
402 seq_printf(m, "RPC server: %s\n",
403 rcu_dereference(clnt->cl_xprt)->servername);
55909f21 404 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_program->name,
1da177e4 405 clnt->cl_prog, clnt->cl_vers);
e7f78657
CL
406 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
407 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
bf19aace 408 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
2446ab60 409 rcu_read_unlock();
1da177e4
LT
410 return 0;
411}
412
413static int
414rpc_info_open(struct inode *inode, struct file *file)
415{
006abe88 416 struct rpc_clnt *clnt = NULL;
1da177e4
LT
417 int ret = single_open(file, rpc_show_info, NULL);
418
419 if (!ret) {
420 struct seq_file *m = file->private_data;
006abe88
TM
421
422 spin_lock(&file->f_path.dentry->d_lock);
423 if (!d_unhashed(file->f_path.dentry))
424 clnt = RPC_I(inode)->private;
425 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
426 spin_unlock(&file->f_path.dentry->d_lock);
1da177e4
LT
427 m->private = clnt;
428 } else {
006abe88 429 spin_unlock(&file->f_path.dentry->d_lock);
1da177e4
LT
430 single_release(inode, file);
431 ret = -EINVAL;
432 }
1da177e4
LT
433 }
434 return ret;
435}
436
437static int
438rpc_info_release(struct inode *inode, struct file *file)
439{
440 struct seq_file *m = file->private_data;
441 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
442
443 if (clnt)
444 rpc_release_client(clnt);
445 return single_release(inode, file);
446}
447
da7071d7 448static const struct file_operations rpc_info_operations = {
1da177e4
LT
449 .owner = THIS_MODULE,
450 .open = rpc_info_open,
451 .read = seq_read,
452 .llseek = seq_lseek,
453 .release = rpc_info_release,
454};
455
456
1da177e4
LT
457/*
458 * Description of fs contents.
459 */
460struct rpc_filelist {
ac6fecee 461 const char *name;
99ac48f5 462 const struct file_operations *i_fop;
7364af6a 463 umode_t mode;
1da177e4
LT
464};
465
1da177e4 466static struct inode *
7364af6a 467rpc_get_inode(struct super_block *sb, umode_t mode)
1da177e4
LT
468{
469 struct inode *inode = new_inode(sb);
470 if (!inode)
471 return NULL;
85fe4025 472 inode->i_ino = get_next_ino();
1da177e4 473 inode->i_mode = mode;
078cd827 474 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
89f0e4fe
JP
475 switch (mode & S_IFMT) {
476 case S_IFDIR:
477 inode->i_fop = &simple_dir_operations;
275448eb 478 inode->i_op = &simple_dir_inode_operations;
89f0e4fe
JP
479 inc_nlink(inode);
480 default:
481 break;
1da177e4
LT
482 }
483 return inode;
484}
485
7589806e
TM
486static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
487 umode_t mode,
488 const struct file_operations *i_fop,
489 void *private)
490{
491 struct inode *inode;
492
beb0f0a9 493 d_drop(dentry);
7589806e
TM
494 inode = rpc_get_inode(dir->i_sb, mode);
495 if (!inode)
496 goto out_err;
497 inode->i_ino = iunique(dir->i_sb, 100);
498 if (i_fop)
499 inode->i_fop = i_fop;
500 if (private)
501 rpc_inode_setowner(inode, private);
502 d_add(dentry, inode);
503 return 0;
504out_err:
1e903eda
AV
505 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %pd\n",
506 __FILE__, __func__, dentry);
7589806e
TM
507 dput(dentry);
508 return -ENOMEM;
509}
510
ac6fecee
TM
511static int __rpc_create(struct inode *dir, struct dentry *dentry,
512 umode_t mode,
513 const struct file_operations *i_fop,
514 void *private)
515{
516 int err;
517
518 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
519 if (err)
520 return err;
521 fsnotify_create(dir, dentry);
522 return 0;
523}
524
7589806e
TM
525static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
526 umode_t mode,
527 const struct file_operations *i_fop,
528 void *private)
529{
530 int err;
531
532 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
533 if (err)
534 return err;
535 inc_nlink(dir);
536 fsnotify_mkdir(dir, dentry);
537 return 0;
538}
539
ba9e0975
SK
540static void
541init_pipe(struct rpc_pipe *pipe)
542{
543 pipe->nreaders = 0;
544 pipe->nwriters = 0;
545 INIT_LIST_HEAD(&pipe->in_upcall);
546 INIT_LIST_HEAD(&pipe->in_downcall);
547 INIT_LIST_HEAD(&pipe->pipe);
548 pipe->pipelen = 0;
ba9e0975
SK
549 INIT_DELAYED_WORK(&pipe->queue_timeout,
550 rpc_timeout_upcall_queue);
551 pipe->ops = NULL;
552 spin_lock_init(&pipe->lock);
c239d83b
SK
553 pipe->dentry = NULL;
554}
ba9e0975 555
c239d83b
SK
556void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
557{
558 kfree(pipe);
ba9e0975 559}
c239d83b 560EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
ba9e0975 561
c239d83b 562struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
7589806e 563{
ba9e0975 564 struct rpc_pipe *pipe;
7589806e 565
ba9e0975
SK
566 pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
567 if (!pipe)
c239d83b 568 return ERR_PTR(-ENOMEM);
ba9e0975 569 init_pipe(pipe);
c239d83b
SK
570 pipe->ops = ops;
571 pipe->flags = flags;
572 return pipe;
573}
574EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
575
576static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
577 umode_t mode,
578 const struct file_operations *i_fop,
579 void *private,
580 struct rpc_pipe *pipe)
7589806e
TM
581{
582 struct rpc_inode *rpci;
583 int err;
584
585 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
586 if (err)
587 return err;
c5ef6035 588 rpci = RPC_I(d_inode(dentry));
7589806e 589 rpci->private = private;
ba9e0975 590 rpci->pipe = pipe;
7589806e
TM
591 fsnotify_create(dir, dentry);
592 return 0;
593}
594
ac6fecee
TM
595static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
596{
597 int ret;
598
599 dget(dentry);
600 ret = simple_rmdir(dir, dentry);
601 d_delete(dentry);
602 dput(dentry);
603 return ret;
604}
605
810d90bc
TM
606static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
607{
608 int ret;
609
610 dget(dentry);
611 ret = simple_unlink(dir, dentry);
612 d_delete(dentry);
613 dput(dentry);
614 return ret;
615}
616
617static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
618{
c5ef6035 619 struct inode *inode = d_inode(dentry);
810d90bc 620
810d90bc
TM
621 rpc_close_pipes(inode);
622 return __rpc_unlink(dir, dentry);
623}
624
5bff0386 625static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
d3db90b0 626 const char *name)
cfeaa4a3 627{
d3db90b0
AV
628 struct qstr q = QSTR_INIT(name, strlen(name));
629 struct dentry *dentry = d_hash_and_lookup(parent, &q);
cfeaa4a3 630 if (!dentry) {
d3db90b0 631 dentry = d_alloc(parent, &q);
5bff0386
SK
632 if (!dentry)
633 return ERR_PTR(-ENOMEM);
cfeaa4a3 634 }
c5ef6035 635 if (d_really_is_negative(dentry))
cfeaa4a3
TM
636 return dentry;
637 dput(dentry);
638 return ERR_PTR(-EEXIST);
639}
640
1da177e4
LT
641/*
642 * FIXME: This probably has races.
643 */
ac6fecee
TM
644static void __rpc_depopulate(struct dentry *parent,
645 const struct rpc_filelist *files,
646 int start, int eof)
1da177e4 647{
c5ef6035 648 struct inode *dir = d_inode(parent);
ac6fecee
TM
649 struct dentry *dentry;
650 struct qstr name;
651 int i;
1da177e4 652
ac6fecee
TM
653 for (i = start; i < eof; i++) {
654 name.name = files[i].name;
655 name.len = strlen(files[i].name);
d3db90b0 656 dentry = d_hash_and_lookup(parent, &name);
ac6fecee
TM
657
658 if (dentry == NULL)
62e1761c 659 continue;
c5ef6035 660 if (d_really_is_negative(dentry))
ac6fecee 661 goto next;
c5ef6035 662 switch (d_inode(dentry)->i_mode & S_IFMT) {
ac6fecee
TM
663 default:
664 BUG();
665 case S_IFREG:
666 __rpc_unlink(dir, dentry);
1da177e4 667 break;
ac6fecee
TM
668 case S_IFDIR:
669 __rpc_rmdir(dir, dentry);
670 }
671next:
672 dput(dentry);
1da177e4 673 }
ac6fecee
TM
674}
675
676static void rpc_depopulate(struct dentry *parent,
677 const struct rpc_filelist *files,
678 int start, int eof)
679{
c5ef6035 680 struct inode *dir = d_inode(parent);
ac6fecee 681
5955102c 682 inode_lock_nested(dir, I_MUTEX_CHILD);
ac6fecee 683 __rpc_depopulate(parent, files, start, eof);
5955102c 684 inode_unlock(dir);
1da177e4
LT
685}
686
ac6fecee
TM
687static int rpc_populate(struct dentry *parent,
688 const struct rpc_filelist *files,
689 int start, int eof,
690 void *private)
1da177e4 691{
c5ef6035 692 struct inode *dir = d_inode(parent);
1da177e4 693 struct dentry *dentry;
ac6fecee 694 int i, err;
1da177e4 695
5955102c 696 inode_lock(dir);
1da177e4 697 for (i = start; i < eof; i++) {
d3db90b0 698 dentry = __rpc_lookup_create_exclusive(parent, files[i].name);
ac6fecee
TM
699 err = PTR_ERR(dentry);
700 if (IS_ERR(dentry))
1da177e4 701 goto out_bad;
ac6fecee
TM
702 switch (files[i].mode & S_IFMT) {
703 default:
704 BUG();
705 case S_IFREG:
706 err = __rpc_create(dir, dentry,
707 files[i].mode,
708 files[i].i_fop,
709 private);
710 break;
711 case S_IFDIR:
712 err = __rpc_mkdir(dir, dentry,
713 files[i].mode,
714 NULL,
715 private);
1da177e4 716 }
ac6fecee
TM
717 if (err != 0)
718 goto out_bad;
1da177e4 719 }
5955102c 720 inode_unlock(dir);
1da177e4
LT
721 return 0;
722out_bad:
ac6fecee 723 __rpc_depopulate(parent, files, start, eof);
5955102c 724 inode_unlock(dir);
1e903eda
AV
725 printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
726 __FILE__, __func__, parent);
ac6fecee 727 return err;
f134585a 728}
1da177e4 729
e57aed77 730static struct dentry *rpc_mkdir_populate(struct dentry *parent,
a95e691f 731 const char *name, umode_t mode, void *private,
e57aed77 732 int (*populate)(struct dentry *, void *), void *args_populate)
f134585a 733{
f134585a 734 struct dentry *dentry;
c5ef6035 735 struct inode *dir = d_inode(parent);
f134585a
TM
736 int error;
737
5955102c 738 inode_lock_nested(dir, I_MUTEX_PARENT);
7d59d1e8 739 dentry = __rpc_lookup_create_exclusive(parent, name);
f134585a 740 if (IS_ERR(dentry))
7d59d1e8
TM
741 goto out;
742 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
7589806e
TM
743 if (error != 0)
744 goto out_err;
e57aed77
TM
745 if (populate != NULL) {
746 error = populate(dentry, args_populate);
747 if (error)
748 goto err_rmdir;
749 }
f134585a 750out:
5955102c 751 inode_unlock(dir);
5c3e985a 752 return dentry;
ac6fecee 753err_rmdir:
f134585a 754 __rpc_rmdir(dir, dentry);
7589806e 755out_err:
f134585a
TM
756 dentry = ERR_PTR(error);
757 goto out;
1da177e4
LT
758}
759
e57aed77
TM
760static int rpc_rmdir_depopulate(struct dentry *dentry,
761 void (*depopulate)(struct dentry *))
1da177e4 762{
dff02cc1 763 struct dentry *parent;
f134585a
TM
764 struct inode *dir;
765 int error;
278c995c 766
dff02cc1 767 parent = dget_parent(dentry);
c5ef6035 768 dir = d_inode(parent);
5955102c 769 inode_lock_nested(dir, I_MUTEX_PARENT);
e57aed77
TM
770 if (depopulate != NULL)
771 depopulate(dentry);
f134585a 772 error = __rpc_rmdir(dir, dentry);
5955102c 773 inode_unlock(dir);
dff02cc1 774 dput(parent);
f134585a 775 return error;
1da177e4
LT
776}
777
93a44a75
BF
778/**
779 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
780 * @parent: dentry of directory to create new "pipe" in
781 * @name: name of pipe
782 * @private: private data to associate with the pipe, for the caller's use
bda14606 783 * @pipe: &rpc_pipe containing input parameters
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
496ad9aa 794 * from the file pointer, via RPC_I(file_inode(file))->private.
93a44a75 795 */
c239d83b
SK
796struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
797 void *private, struct rpc_pipe *pipe)
1da177e4 798{
1da177e4 799 struct dentry *dentry;
c5ef6035 800 struct inode *dir = d_inode(parent);
d6444062 801 umode_t umode = S_IFIFO | 0600;
7589806e 802 int err;
7364af6a 803
c239d83b 804 if (pipe->ops->upcall == NULL)
d6444062 805 umode &= ~0444;
c239d83b 806 if (pipe->ops->downcall == NULL)
d6444062 807 umode &= ~0222;
1da177e4 808
5955102c 809 inode_lock_nested(dir, I_MUTEX_PARENT);
d3db90b0 810 dentry = __rpc_lookup_create_exclusive(parent, name);
1da177e4 811 if (IS_ERR(dentry))
cfeaa4a3 812 goto out;
c239d83b
SK
813 err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
814 private, pipe);
7589806e
TM
815 if (err)
816 goto out_err;
f134585a 817out:
5955102c 818 inode_unlock(dir);
5c3e985a 819 return dentry;
7589806e
TM
820out_err:
821 dentry = ERR_PTR(err);
1e903eda
AV
822 printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
823 __FILE__, __func__, parent, name,
7589806e 824 err);
f134585a 825 goto out;
1da177e4 826}
c239d83b 827EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
1da177e4 828
93a44a75
BF
829/**
830 * rpc_unlink - remove a pipe
831 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
832 *
833 * After this call, lookups will no longer find the pipe, and any
834 * attempts to read or write using preexisting opens of the pipe will
835 * return -EPIPE.
836 */
f134585a 837int
5d67476f 838rpc_unlink(struct dentry *dentry)
1da177e4 839{
5d67476f 840 struct dentry *parent;
f134585a 841 struct inode *dir;
5d67476f 842 int error = 0;
1da177e4 843
5d67476f 844 parent = dget_parent(dentry);
c5ef6035 845 dir = d_inode(parent);
5955102c 846 inode_lock_nested(dir, I_MUTEX_PARENT);
810d90bc 847 error = __rpc_rmpipe(dir, dentry);
5955102c 848 inode_unlock(dir);
5d67476f 849 dput(parent);
f134585a 850 return error;
1da177e4 851}
468039ee 852EXPORT_SYMBOL_GPL(rpc_unlink);
1da177e4 853
6739ffb7
TM
854/**
855 * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head
856 * @pdh: pointer to struct rpc_pipe_dir_head
857 */
858void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh)
859{
860 INIT_LIST_HEAD(&pdh->pdh_entries);
861 pdh->pdh_dentry = NULL;
862}
863EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head);
864
865/**
866 * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object
867 * @pdo: pointer to struct rpc_pipe_dir_object
868 * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops
869 * @pdo_data: pointer to caller-defined data
870 */
871void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo,
872 const struct rpc_pipe_dir_object_ops *pdo_ops,
873 void *pdo_data)
874{
875 INIT_LIST_HEAD(&pdo->pdo_head);
876 pdo->pdo_ops = pdo_ops;
877 pdo->pdo_data = pdo_data;
878}
879EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object);
880
881static int
882rpc_add_pipe_dir_object_locked(struct net *net,
883 struct rpc_pipe_dir_head *pdh,
884 struct rpc_pipe_dir_object *pdo)
885{
886 int ret = 0;
887
888 if (pdh->pdh_dentry)
889 ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo);
890 if (ret == 0)
891 list_add_tail(&pdo->pdo_head, &pdh->pdh_entries);
892 return ret;
893}
894
895static void
896rpc_remove_pipe_dir_object_locked(struct net *net,
897 struct rpc_pipe_dir_head *pdh,
898 struct rpc_pipe_dir_object *pdo)
899{
900 if (pdh->pdh_dentry)
901 pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo);
902 list_del_init(&pdo->pdo_head);
903}
904
905/**
906 * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory
907 * @net: pointer to struct net
908 * @pdh: pointer to struct rpc_pipe_dir_head
909 * @pdo: pointer to struct rpc_pipe_dir_object
910 *
911 */
912int
913rpc_add_pipe_dir_object(struct net *net,
914 struct rpc_pipe_dir_head *pdh,
915 struct rpc_pipe_dir_object *pdo)
916{
917 int ret = 0;
918
919 if (list_empty(&pdo->pdo_head)) {
920 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
921
922 mutex_lock(&sn->pipefs_sb_lock);
923 ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo);
924 mutex_unlock(&sn->pipefs_sb_lock);
925 }
926 return ret;
927}
928EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object);
929
930/**
931 * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory
932 * @net: pointer to struct net
933 * @pdh: pointer to struct rpc_pipe_dir_head
934 * @pdo: pointer to struct rpc_pipe_dir_object
935 *
936 */
937void
938rpc_remove_pipe_dir_object(struct net *net,
939 struct rpc_pipe_dir_head *pdh,
940 struct rpc_pipe_dir_object *pdo)
941{
942 if (!list_empty(&pdo->pdo_head)) {
943 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
944
945 mutex_lock(&sn->pipefs_sb_lock);
946 rpc_remove_pipe_dir_object_locked(net, pdh, pdo);
947 mutex_unlock(&sn->pipefs_sb_lock);
948 }
949}
950EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object);
951
298fc355
TM
952/**
953 * rpc_find_or_alloc_pipe_dir_object
954 * @net: pointer to struct net
955 * @pdh: pointer to struct rpc_pipe_dir_head
956 * @match: match struct rpc_pipe_dir_object to data
957 * @alloc: allocate a new struct rpc_pipe_dir_object
958 * @data: user defined data for match() and alloc()
959 *
960 */
961struct rpc_pipe_dir_object *
962rpc_find_or_alloc_pipe_dir_object(struct net *net,
963 struct rpc_pipe_dir_head *pdh,
964 int (*match)(struct rpc_pipe_dir_object *, void *),
965 struct rpc_pipe_dir_object *(*alloc)(void *),
966 void *data)
967{
968 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
969 struct rpc_pipe_dir_object *pdo;
970
971 mutex_lock(&sn->pipefs_sb_lock);
972 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) {
973 if (!match(pdo, data))
974 continue;
975 goto out;
976 }
977 pdo = alloc(data);
978 if (!pdo)
979 goto out;
980 rpc_add_pipe_dir_object_locked(net, pdh, pdo);
981out:
982 mutex_unlock(&sn->pipefs_sb_lock);
983 return pdo;
984}
985EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object);
986
6739ffb7
TM
987static void
988rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
989{
990 struct rpc_pipe_dir_object *pdo;
991 struct dentry *dir = pdh->pdh_dentry;
992
993 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
994 pdo->pdo_ops->create(dir, pdo);
995}
996
997static void
998rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
999{
1000 struct rpc_pipe_dir_object *pdo;
1001 struct dentry *dir = pdh->pdh_dentry;
1002
1003 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
1004 pdo->pdo_ops->destroy(dir, pdo);
1005}
1006
e57aed77
TM
1007enum {
1008 RPCAUTH_info,
1009 RPCAUTH_EOF
1010};
1011
1012static const struct rpc_filelist authfiles[] = {
1013 [RPCAUTH_info] = {
1014 .name = "info",
1015 .i_fop = &rpc_info_operations,
d6444062 1016 .mode = S_IFREG | 0400,
e57aed77
TM
1017 },
1018};
1019
1020static int rpc_clntdir_populate(struct dentry *dentry, void *private)
1021{
1022 return rpc_populate(dentry,
1023 authfiles, RPCAUTH_info, RPCAUTH_EOF,
1024 private);
1025}
1026
1027static void rpc_clntdir_depopulate(struct dentry *dentry)
1028{
1029 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
1030}
1031
7d59d1e8
TM
1032/**
1033 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
a95e691f
AV
1034 * @dentry: the parent of new directory
1035 * @name: the name of new directory
7d59d1e8
TM
1036 * @rpc_client: rpc client to associate with this directory
1037 *
1038 * This creates a directory at the given @path associated with
1039 * @rpc_clnt, which will contain a file named "info" with some basic
1040 * information about the client, together with any "pipes" that may
1041 * later be created using rpc_mkpipe().
1042 */
23ac6581 1043struct dentry *rpc_create_client_dir(struct dentry *dentry,
a95e691f 1044 const char *name,
e57aed77
TM
1045 struct rpc_clnt *rpc_client)
1046{
6739ffb7
TM
1047 struct dentry *ret;
1048
d6444062
JP
1049 ret = rpc_mkdir_populate(dentry, name, 0555, NULL,
1050 rpc_clntdir_populate, rpc_client);
6739ffb7
TM
1051 if (!IS_ERR(ret)) {
1052 rpc_client->cl_pipedir_objects.pdh_dentry = ret;
1053 rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1054 }
1055 return ret;
e57aed77
TM
1056}
1057
1058/**
1059 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
6739ffb7 1060 * @rpc_client: rpc_client for the pipe
e57aed77 1061 */
c36dcfe1 1062int rpc_remove_client_dir(struct rpc_clnt *rpc_client)
7d59d1e8 1063{
c36dcfe1
TM
1064 struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry;
1065
1066 if (dentry == NULL)
1067 return 0;
1068 rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1069 rpc_client->cl_pipedir_objects.pdh_dentry = NULL;
e57aed77 1070 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
7d59d1e8
TM
1071}
1072
8854e82d
TM
1073static const struct rpc_filelist cache_pipefs_files[3] = {
1074 [0] = {
1075 .name = "channel",
1076 .i_fop = &cache_file_operations_pipefs,
d6444062 1077 .mode = S_IFREG | 0600,
8854e82d
TM
1078 },
1079 [1] = {
1080 .name = "content",
1081 .i_fop = &content_file_operations_pipefs,
d6444062 1082 .mode = S_IFREG | 0400,
8854e82d
TM
1083 },
1084 [2] = {
1085 .name = "flush",
1086 .i_fop = &cache_flush_operations_pipefs,
d6444062 1087 .mode = S_IFREG | 0600,
8854e82d
TM
1088 },
1089};
1090
1091static int rpc_cachedir_populate(struct dentry *dentry, void *private)
1092{
1093 return rpc_populate(dentry,
1094 cache_pipefs_files, 0, 3,
1095 private);
1096}
1097
1098static void rpc_cachedir_depopulate(struct dentry *dentry)
1099{
1100 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
1101}
1102
a95e691f 1103struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name,
64f1426f 1104 umode_t umode, struct cache_detail *cd)
8854e82d
TM
1105{
1106 return rpc_mkdir_populate(parent, name, umode, NULL,
1107 rpc_cachedir_populate, cd);
1108}
1109
1110void rpc_remove_cache_dir(struct dentry *dentry)
1111{
1112 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
1113}
1114
1da177e4
LT
1115/*
1116 * populate the filesystem
1117 */
b87221de 1118static const struct super_operations s_ops = {
1da177e4 1119 .alloc_inode = rpc_alloc_inode,
bef252fa 1120 .free_inode = rpc_free_inode,
1da177e4
LT
1121 .statfs = simple_statfs,
1122};
1123
1124#define RPCAUTH_GSSMAGIC 0x67596969
1125
bb156749
TM
1126/*
1127 * We have a single directory with 1 node in it.
1128 */
1129enum {
1130 RPCAUTH_lockd,
1131 RPCAUTH_mount,
1132 RPCAUTH_nfs,
1133 RPCAUTH_portmap,
1134 RPCAUTH_statd,
1135 RPCAUTH_nfsd4_cb,
e571cbf1 1136 RPCAUTH_cache,
b3537c35 1137 RPCAUTH_nfsd,
4b9a445e 1138 RPCAUTH_gssd,
bb156749
TM
1139 RPCAUTH_RootEOF
1140};
1141
1142static const struct rpc_filelist files[] = {
1143 [RPCAUTH_lockd] = {
1144 .name = "lockd",
d6444062 1145 .mode = S_IFDIR | 0555,
bb156749
TM
1146 },
1147 [RPCAUTH_mount] = {
1148 .name = "mount",
d6444062 1149 .mode = S_IFDIR | 0555,
bb156749
TM
1150 },
1151 [RPCAUTH_nfs] = {
1152 .name = "nfs",
d6444062 1153 .mode = S_IFDIR | 0555,
bb156749
TM
1154 },
1155 [RPCAUTH_portmap] = {
1156 .name = "portmap",
d6444062 1157 .mode = S_IFDIR | 0555,
bb156749
TM
1158 },
1159 [RPCAUTH_statd] = {
1160 .name = "statd",
d6444062 1161 .mode = S_IFDIR | 0555,
bb156749
TM
1162 },
1163 [RPCAUTH_nfsd4_cb] = {
1164 .name = "nfsd4_cb",
d6444062 1165 .mode = S_IFDIR | 0555,
bb156749 1166 },
e571cbf1
TM
1167 [RPCAUTH_cache] = {
1168 .name = "cache",
d6444062 1169 .mode = S_IFDIR | 0555,
e571cbf1 1170 },
b3537c35
JL
1171 [RPCAUTH_nfsd] = {
1172 .name = "nfsd",
d6444062 1173 .mode = S_IFDIR | 0555,
b3537c35 1174 },
4b9a445e
JL
1175 [RPCAUTH_gssd] = {
1176 .name = "gssd",
d6444062 1177 .mode = S_IFDIR | 0555,
4b9a445e 1178 },
bb156749
TM
1179};
1180
432eb1a5
SK
1181/*
1182 * This call can be used only in RPC pipefs mount notification hooks.
1183 */
1184struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1185 const unsigned char *dir_name)
1186{
26fe5750 1187 struct qstr dir = QSTR_INIT(dir_name, strlen(dir_name));
d3db90b0 1188 return d_hash_and_lookup(sb->s_root, &dir);
432eb1a5
SK
1189}
1190EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1191
4b9a445e 1192int rpc_pipefs_init_net(struct net *net)
c21a588f
SK
1193{
1194 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1195
4b9a445e
JL
1196 sn->gssd_dummy = rpc_mkpipe_data(&gssd_dummy_pipe_ops, 0);
1197 if (IS_ERR(sn->gssd_dummy))
1198 return PTR_ERR(sn->gssd_dummy);
1199
c21a588f 1200 mutex_init(&sn->pipefs_sb_lock);
2aed8b47 1201 sn->pipe_version = -1;
4b9a445e
JL
1202 return 0;
1203}
1204
1205void rpc_pipefs_exit_net(struct net *net)
1206{
1207 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1208
1209 rpc_destroy_pipe_data(sn->gssd_dummy);
c21a588f
SK
1210}
1211
1212/*
1213 * This call will be used for per network namespace operations calls.
1214 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1215 * found. This lock have to be released by rpc_put_sb_net() when all operations
1216 * will be completed.
1217 */
1218struct super_block *rpc_get_sb_net(const struct net *net)
1219{
1220 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1221
1222 mutex_lock(&sn->pipefs_sb_lock);
1223 if (sn->pipefs_sb)
1224 return sn->pipefs_sb;
1225 mutex_unlock(&sn->pipefs_sb_lock);
1226 return NULL;
1227}
1228EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1229
1230void rpc_put_sb_net(const struct net *net)
1231{
1232 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1233
749386e9 1234 WARN_ON(sn->pipefs_sb == NULL);
c21a588f
SK
1235 mutex_unlock(&sn->pipefs_sb_lock);
1236}
1237EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1238
4b9a445e
JL
1239static const struct rpc_filelist gssd_dummy_clnt_dir[] = {
1240 [0] = {
1241 .name = "clntXX",
d6444062 1242 .mode = S_IFDIR | 0555,
4b9a445e
JL
1243 },
1244};
1245
1246static ssize_t
1247dummy_downcall(struct file *filp, const char __user *src, size_t len)
1248{
1249 return -EINVAL;
1250}
1251
1252static const struct rpc_pipe_ops gssd_dummy_pipe_ops = {
1253 .upcall = rpc_pipe_generic_upcall,
1254 .downcall = dummy_downcall,
1255};
1256
e2f0c83a
JL
1257/*
1258 * Here we present a bogus "info" file to keep rpc.gssd happy. We don't expect
1259 * that it will ever use this info to handle an upcall, but rpc.gssd expects
1260 * that this file will be there and have a certain format.
1261 */
1262static int
260f71ef 1263rpc_dummy_info_show(struct seq_file *m, void *v)
e2f0c83a
JL
1264{
1265 seq_printf(m, "RPC server: %s\n", utsname()->nodename);
1266 seq_printf(m, "service: foo (1) version 0\n");
1267 seq_printf(m, "address: 127.0.0.1\n");
1268 seq_printf(m, "protocol: tcp\n");
1269 seq_printf(m, "port: 0\n");
1270 return 0;
1271}
260f71ef 1272DEFINE_SHOW_ATTRIBUTE(rpc_dummy_info);
e2f0c83a
JL
1273
1274static const struct rpc_filelist gssd_dummy_info_file[] = {
1275 [0] = {
1276 .name = "info",
260f71ef 1277 .i_fop = &rpc_dummy_info_fops,
d6444062 1278 .mode = S_IFREG | 0400,
e2f0c83a
JL
1279 },
1280};
1281
4b9a445e
JL
1282/**
1283 * rpc_gssd_dummy_populate - create a dummy gssd pipe
1284 * @root: root of the rpc_pipefs filesystem
1285 * @pipe_data: pipe data created when netns is initialized
1286 *
1287 * Create a dummy set of directories and a pipe that gssd can hold open to
1288 * indicate that it is up and running.
1289 */
1290static struct dentry *
1291rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
1292{
1293 int ret = 0;
1294 struct dentry *gssd_dentry;
1295 struct dentry *clnt_dentry = NULL;
1296 struct dentry *pipe_dentry = NULL;
1297 struct qstr q = QSTR_INIT(files[RPCAUTH_gssd].name,
1298 strlen(files[RPCAUTH_gssd].name));
1299
1300 /* We should never get this far if "gssd" doesn't exist */
1301 gssd_dentry = d_hash_and_lookup(root, &q);
1302 if (!gssd_dentry)
1303 return ERR_PTR(-ENOENT);
1304
1305 ret = rpc_populate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1, NULL);
1306 if (ret) {
1307 pipe_dentry = ERR_PTR(ret);
1308 goto out;
1309 }
1310
1311 q.name = gssd_dummy_clnt_dir[0].name;
1312 q.len = strlen(gssd_dummy_clnt_dir[0].name);
1313 clnt_dentry = d_hash_and_lookup(gssd_dentry, &q);
1314 if (!clnt_dentry) {
1315 pipe_dentry = ERR_PTR(-ENOENT);
1316 goto out;
1317 }
1318
e2f0c83a
JL
1319 ret = rpc_populate(clnt_dentry, gssd_dummy_info_file, 0, 1, NULL);
1320 if (ret) {
1321 __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
1322 pipe_dentry = ERR_PTR(ret);
1323 goto out;
1324 }
1325
4b9a445e 1326 pipe_dentry = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data);
e2f0c83a
JL
1327 if (IS_ERR(pipe_dentry)) {
1328 __rpc_depopulate(clnt_dentry, gssd_dummy_info_file, 0, 1);
3396f92f 1329 __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
e2f0c83a 1330 }
4b9a445e
JL
1331out:
1332 dput(clnt_dentry);
1333 dput(gssd_dentry);
1334 return pipe_dentry;
1335}
1336
23e66ba9
JL
1337static void
1338rpc_gssd_dummy_depopulate(struct dentry *pipe_dentry)
1339{
1340 struct dentry *clnt_dir = pipe_dentry->d_parent;
1341 struct dentry *gssd_dir = clnt_dir->d_parent;
1342
4a3877c4 1343 dget(pipe_dentry);
c5ef6035 1344 __rpc_rmpipe(d_inode(clnt_dir), pipe_dentry);
23e66ba9
JL
1345 __rpc_depopulate(clnt_dir, gssd_dummy_info_file, 0, 1);
1346 __rpc_depopulate(gssd_dir, gssd_dummy_clnt_dir, 0, 1);
1347 dput(pipe_dentry);
1348}
1349
1da177e4 1350static int
b9662f31 1351rpc_fill_super(struct super_block *sb, struct fs_context *fc)
1da177e4
LT
1352{
1353 struct inode *inode;
4b9a445e 1354 struct dentry *root, *gssd_dentry;
b9662f31 1355 struct net *net = sb->s_fs_info;
90c4e829 1356 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2d00131a 1357 int err;
1da177e4 1358
09cbfeaf
KS
1359 sb->s_blocksize = PAGE_SIZE;
1360 sb->s_blocksize_bits = PAGE_SHIFT;
1da177e4
LT
1361 sb->s_magic = RPCAUTH_GSSMAGIC;
1362 sb->s_op = &s_ops;
b26d4cd3 1363 sb->s_d_op = &simple_dentry_operations;
1da177e4
LT
1364 sb->s_time_gran = 1;
1365
d6444062 1366 inode = rpc_get_inode(sb, S_IFDIR | 0555);
48fde701
AV
1367 sb->s_root = root = d_make_root(inode);
1368 if (!root)
1da177e4 1369 return -ENOMEM;
ac6fecee 1370 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
fc7bed8c 1371 return -ENOMEM;
4b9a445e
JL
1372
1373 gssd_dentry = rpc_gssd_dummy_populate(root, sn->gssd_dummy);
1374 if (IS_ERR(gssd_dentry)) {
1375 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1376 return PTR_ERR(gssd_dentry);
1377 }
1378
6c67a3e4
VA
1379 dprintk("RPC: sending pipefs MOUNT notification for net %x%s\n",
1380 net->ns.inum, NET_NAME(net));
38481605 1381 mutex_lock(&sn->pipefs_sb_lock);
37629b57 1382 sn->pipefs_sb = sb;
2d00131a
SK
1383 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1384 RPC_PIPEFS_MOUNT,
1385 sb);
1386 if (err)
1387 goto err_depopulate;
38481605 1388 mutex_unlock(&sn->pipefs_sb_lock);
1da177e4 1389 return 0;
2d00131a
SK
1390
1391err_depopulate:
23e66ba9 1392 rpc_gssd_dummy_depopulate(gssd_dentry);
2d00131a
SK
1393 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1394 RPC_PIPEFS_UMOUNT,
1395 sb);
37629b57 1396 sn->pipefs_sb = NULL;
2d00131a 1397 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
38481605 1398 mutex_unlock(&sn->pipefs_sb_lock);
2d00131a 1399 return err;
1da177e4
LT
1400}
1401
89f84243
JL
1402bool
1403gssd_running(struct net *net)
1404{
1405 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1406 struct rpc_pipe *pipe = sn->gssd_dummy;
1407
1408 return pipe->nreaders || pipe->nwriters;
1409}
1410EXPORT_SYMBOL_GPL(gssd_running);
1411
b9662f31
DH
1412static int rpc_fs_get_tree(struct fs_context *fc)
1413{
1414 fc->s_fs_info = get_net(fc->net_ns);
1415 return vfs_get_super(fc, vfs_get_keyed_super, rpc_fill_super);
1416}
1417
1418static void rpc_fs_free_fc(struct fs_context *fc)
1da177e4 1419{
b9662f31
DH
1420 if (fc->s_fs_info)
1421 put_net(fc->s_fs_info);
1422}
1423
1424static const struct fs_context_operations rpc_fs_context_ops = {
1425 .free = rpc_fs_free_fc,
1426 .get_tree = rpc_fs_get_tree,
1427};
1428
1429static int rpc_init_fs_context(struct fs_context *fc)
1430{
1431 put_user_ns(fc->user_ns);
1432 fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1433 fc->ops = &rpc_fs_context_ops;
1434 return 0;
1da177e4
LT
1435}
1436
09acfea5 1437static void rpc_kill_sb(struct super_block *sb)
021c68de
SK
1438{
1439 struct net *net = sb->s_fs_info;
90c4e829 1440 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
021c68de 1441
c21a588f 1442 mutex_lock(&sn->pipefs_sb_lock);
642fe4d0
TM
1443 if (sn->pipefs_sb != sb) {
1444 mutex_unlock(&sn->pipefs_sb_lock);
1445 goto out;
1446 }
90c4e829 1447 sn->pipefs_sb = NULL;
6c67a3e4
VA
1448 dprintk("RPC: sending pipefs UMOUNT notification for net %x%s\n",
1449 net->ns.inum, NET_NAME(net));
2d00131a
SK
1450 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1451 RPC_PIPEFS_UMOUNT,
1452 sb);
adb6fa7f 1453 mutex_unlock(&sn->pipefs_sb_lock);
642fe4d0 1454out:
021c68de 1455 kill_litter_super(sb);
d91ee87d 1456 put_net(net);
1da177e4
LT
1457}
1458
1459static struct file_system_type rpc_pipe_fs_type = {
1460 .owner = THIS_MODULE,
1461 .name = "rpc_pipefs",
b9662f31 1462 .init_fs_context = rpc_init_fs_context,
021c68de 1463 .kill_sb = rpc_kill_sb,
1da177e4 1464};
7f78e035 1465MODULE_ALIAS_FS("rpc_pipefs");
fa7614dd 1466MODULE_ALIAS("rpc_pipefs");
1da177e4
LT
1467
1468static void
51cc5068 1469init_once(void *foo)
1da177e4
LT
1470{
1471 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1472
a35afb83
CL
1473 inode_init_once(&rpci->vfs_inode);
1474 rpci->private = NULL;
ba9e0975 1475 rpci->pipe = NULL;
a35afb83 1476 init_waitqueue_head(&rpci->waitq);
1da177e4
LT
1477}
1478
1479int register_rpc_pipefs(void)
1480{
5bd5f581
AM
1481 int err;
1482
1da177e4 1483 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
fffb60f9
PJ
1484 sizeof(struct rpc_inode),
1485 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
5d097056 1486 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
20c2df83 1487 init_once);
1da177e4
LT
1488 if (!rpc_inode_cachep)
1489 return -ENOMEM;
80df9d20
SK
1490 err = rpc_clients_notifier_register();
1491 if (err)
1492 goto err_notifier;
5bd5f581 1493 err = register_filesystem(&rpc_pipe_fs_type);
80df9d20
SK
1494 if (err)
1495 goto err_register;
1da177e4 1496 return 0;
80df9d20
SK
1497
1498err_register:
1499 rpc_clients_notifier_unregister();
1500err_notifier:
1501 kmem_cache_destroy(rpc_inode_cachep);
1502 return err;
1da177e4
LT
1503}
1504
1505void unregister_rpc_pipefs(void)
1506{
80df9d20 1507 rpc_clients_notifier_unregister();
1a1d92c1 1508 kmem_cache_destroy(rpc_inode_cachep);
1da177e4
LT
1509 unregister_filesystem(&rpc_pipe_fs_type);
1510}