Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / net / sunrpc / rpc_pipe.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * net/sunrpc/rpc_pipe.c
4 *
5 * Userland/kernel interface for rpcauth_gss.
6 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
d51fe1be 7 * and fs/sysfs/inode.c
1da177e4
LT
8 *
9 * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
10 *
11 */
1da177e4
LT
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/string.h>
15#include <linux/pagemap.h>
16#include <linux/mount.h>
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);
a35d632c
AG
601 if (!ret)
602 fsnotify_rmdir(dir, dentry);
ac6fecee
TM
603 d_delete(dentry);
604 dput(dentry);
605 return ret;
606}
607
810d90bc
TM
608static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
609{
610 int ret;
611
612 dget(dentry);
613 ret = simple_unlink(dir, dentry);
a35d632c
AG
614 if (!ret)
615 fsnotify_unlink(dir, dentry);
810d90bc
TM
616 d_delete(dentry);
617 dput(dentry);
618 return ret;
619}
620
621static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
622{
c5ef6035 623 struct inode *inode = d_inode(dentry);
810d90bc 624
810d90bc
TM
625 rpc_close_pipes(inode);
626 return __rpc_unlink(dir, dentry);
627}
628
5bff0386 629static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
d3db90b0 630 const char *name)
cfeaa4a3 631{
d3db90b0
AV
632 struct qstr q = QSTR_INIT(name, strlen(name));
633 struct dentry *dentry = d_hash_and_lookup(parent, &q);
cfeaa4a3 634 if (!dentry) {
d3db90b0 635 dentry = d_alloc(parent, &q);
5bff0386
SK
636 if (!dentry)
637 return ERR_PTR(-ENOMEM);
cfeaa4a3 638 }
c5ef6035 639 if (d_really_is_negative(dentry))
cfeaa4a3
TM
640 return dentry;
641 dput(dentry);
642 return ERR_PTR(-EEXIST);
643}
644
1da177e4
LT
645/*
646 * FIXME: This probably has races.
647 */
ac6fecee
TM
648static void __rpc_depopulate(struct dentry *parent,
649 const struct rpc_filelist *files,
650 int start, int eof)
1da177e4 651{
c5ef6035 652 struct inode *dir = d_inode(parent);
ac6fecee
TM
653 struct dentry *dentry;
654 struct qstr name;
655 int i;
1da177e4 656
ac6fecee
TM
657 for (i = start; i < eof; i++) {
658 name.name = files[i].name;
659 name.len = strlen(files[i].name);
d3db90b0 660 dentry = d_hash_and_lookup(parent, &name);
ac6fecee
TM
661
662 if (dentry == NULL)
62e1761c 663 continue;
c5ef6035 664 if (d_really_is_negative(dentry))
ac6fecee 665 goto next;
c5ef6035 666 switch (d_inode(dentry)->i_mode & S_IFMT) {
ac6fecee
TM
667 default:
668 BUG();
669 case S_IFREG:
670 __rpc_unlink(dir, dentry);
1da177e4 671 break;
ac6fecee
TM
672 case S_IFDIR:
673 __rpc_rmdir(dir, dentry);
674 }
675next:
676 dput(dentry);
1da177e4 677 }
ac6fecee
TM
678}
679
680static void rpc_depopulate(struct dentry *parent,
681 const struct rpc_filelist *files,
682 int start, int eof)
683{
c5ef6035 684 struct inode *dir = d_inode(parent);
ac6fecee 685
5955102c 686 inode_lock_nested(dir, I_MUTEX_CHILD);
ac6fecee 687 __rpc_depopulate(parent, files, start, eof);
5955102c 688 inode_unlock(dir);
1da177e4
LT
689}
690
ac6fecee
TM
691static int rpc_populate(struct dentry *parent,
692 const struct rpc_filelist *files,
693 int start, int eof,
694 void *private)
1da177e4 695{
c5ef6035 696 struct inode *dir = d_inode(parent);
1da177e4 697 struct dentry *dentry;
ac6fecee 698 int i, err;
1da177e4 699
5955102c 700 inode_lock(dir);
1da177e4 701 for (i = start; i < eof; i++) {
d3db90b0 702 dentry = __rpc_lookup_create_exclusive(parent, files[i].name);
ac6fecee
TM
703 err = PTR_ERR(dentry);
704 if (IS_ERR(dentry))
1da177e4 705 goto out_bad;
ac6fecee
TM
706 switch (files[i].mode & S_IFMT) {
707 default:
708 BUG();
709 case S_IFREG:
710 err = __rpc_create(dir, dentry,
711 files[i].mode,
712 files[i].i_fop,
713 private);
714 break;
715 case S_IFDIR:
716 err = __rpc_mkdir(dir, dentry,
717 files[i].mode,
718 NULL,
719 private);
1da177e4 720 }
ac6fecee
TM
721 if (err != 0)
722 goto out_bad;
1da177e4 723 }
5955102c 724 inode_unlock(dir);
1da177e4
LT
725 return 0;
726out_bad:
ac6fecee 727 __rpc_depopulate(parent, files, start, eof);
5955102c 728 inode_unlock(dir);
1e903eda
AV
729 printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
730 __FILE__, __func__, parent);
ac6fecee 731 return err;
f134585a 732}
1da177e4 733
e57aed77 734static struct dentry *rpc_mkdir_populate(struct dentry *parent,
a95e691f 735 const char *name, umode_t mode, void *private,
e57aed77 736 int (*populate)(struct dentry *, void *), void *args_populate)
f134585a 737{
f134585a 738 struct dentry *dentry;
c5ef6035 739 struct inode *dir = d_inode(parent);
f134585a
TM
740 int error;
741
5955102c 742 inode_lock_nested(dir, I_MUTEX_PARENT);
7d59d1e8 743 dentry = __rpc_lookup_create_exclusive(parent, name);
f134585a 744 if (IS_ERR(dentry))
7d59d1e8
TM
745 goto out;
746 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
7589806e
TM
747 if (error != 0)
748 goto out_err;
e57aed77
TM
749 if (populate != NULL) {
750 error = populate(dentry, args_populate);
751 if (error)
752 goto err_rmdir;
753 }
f134585a 754out:
5955102c 755 inode_unlock(dir);
5c3e985a 756 return dentry;
ac6fecee 757err_rmdir:
f134585a 758 __rpc_rmdir(dir, dentry);
7589806e 759out_err:
f134585a
TM
760 dentry = ERR_PTR(error);
761 goto out;
1da177e4
LT
762}
763
e57aed77
TM
764static int rpc_rmdir_depopulate(struct dentry *dentry,
765 void (*depopulate)(struct dentry *))
1da177e4 766{
dff02cc1 767 struct dentry *parent;
f134585a
TM
768 struct inode *dir;
769 int error;
278c995c 770
dff02cc1 771 parent = dget_parent(dentry);
c5ef6035 772 dir = d_inode(parent);
5955102c 773 inode_lock_nested(dir, I_MUTEX_PARENT);
e57aed77
TM
774 if (depopulate != NULL)
775 depopulate(dentry);
f134585a 776 error = __rpc_rmdir(dir, dentry);
5955102c 777 inode_unlock(dir);
dff02cc1 778 dput(parent);
f134585a 779 return error;
1da177e4
LT
780}
781
93a44a75
BF
782/**
783 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
784 * @parent: dentry of directory to create new "pipe" in
785 * @name: name of pipe
786 * @private: private data to associate with the pipe, for the caller's use
bda14606 787 * @pipe: &rpc_pipe containing input parameters
93a44a75
BF
788 *
789 * Data is made available for userspace to read by calls to
790 * rpc_queue_upcall(). The actual reads will result in calls to
791 * @ops->upcall, which will be called with the file pointer,
792 * message, and userspace buffer to copy to.
793 *
794 * Writes can come at any time, and do not necessarily have to be
795 * responses to upcalls. They will result in calls to @msg->downcall.
796 *
797 * The @private argument passed here will be available to all these methods
496ad9aa 798 * from the file pointer, via RPC_I(file_inode(file))->private.
93a44a75 799 */
c239d83b
SK
800struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
801 void *private, struct rpc_pipe *pipe)
1da177e4 802{
1da177e4 803 struct dentry *dentry;
c5ef6035 804 struct inode *dir = d_inode(parent);
d6444062 805 umode_t umode = S_IFIFO | 0600;
7589806e 806 int err;
7364af6a 807
c239d83b 808 if (pipe->ops->upcall == NULL)
d6444062 809 umode &= ~0444;
c239d83b 810 if (pipe->ops->downcall == NULL)
d6444062 811 umode &= ~0222;
1da177e4 812
5955102c 813 inode_lock_nested(dir, I_MUTEX_PARENT);
d3db90b0 814 dentry = __rpc_lookup_create_exclusive(parent, name);
1da177e4 815 if (IS_ERR(dentry))
cfeaa4a3 816 goto out;
c239d83b
SK
817 err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
818 private, pipe);
7589806e
TM
819 if (err)
820 goto out_err;
f134585a 821out:
5955102c 822 inode_unlock(dir);
5c3e985a 823 return dentry;
7589806e
TM
824out_err:
825 dentry = ERR_PTR(err);
1e903eda
AV
826 printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
827 __FILE__, __func__, parent, name,
7589806e 828 err);
f134585a 829 goto out;
1da177e4 830}
c239d83b 831EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
1da177e4 832
93a44a75
BF
833/**
834 * rpc_unlink - remove a pipe
835 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
836 *
837 * After this call, lookups will no longer find the pipe, and any
838 * attempts to read or write using preexisting opens of the pipe will
839 * return -EPIPE.
840 */
f134585a 841int
5d67476f 842rpc_unlink(struct dentry *dentry)
1da177e4 843{
5d67476f 844 struct dentry *parent;
f134585a 845 struct inode *dir;
5d67476f 846 int error = 0;
1da177e4 847
5d67476f 848 parent = dget_parent(dentry);
c5ef6035 849 dir = d_inode(parent);
5955102c 850 inode_lock_nested(dir, I_MUTEX_PARENT);
810d90bc 851 error = __rpc_rmpipe(dir, dentry);
5955102c 852 inode_unlock(dir);
5d67476f 853 dput(parent);
f134585a 854 return error;
1da177e4 855}
468039ee 856EXPORT_SYMBOL_GPL(rpc_unlink);
1da177e4 857
6739ffb7
TM
858/**
859 * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head
860 * @pdh: pointer to struct rpc_pipe_dir_head
861 */
862void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh)
863{
864 INIT_LIST_HEAD(&pdh->pdh_entries);
865 pdh->pdh_dentry = NULL;
866}
867EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head);
868
869/**
870 * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object
871 * @pdo: pointer to struct rpc_pipe_dir_object
872 * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops
873 * @pdo_data: pointer to caller-defined data
874 */
875void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo,
876 const struct rpc_pipe_dir_object_ops *pdo_ops,
877 void *pdo_data)
878{
879 INIT_LIST_HEAD(&pdo->pdo_head);
880 pdo->pdo_ops = pdo_ops;
881 pdo->pdo_data = pdo_data;
882}
883EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object);
884
885static int
886rpc_add_pipe_dir_object_locked(struct net *net,
887 struct rpc_pipe_dir_head *pdh,
888 struct rpc_pipe_dir_object *pdo)
889{
890 int ret = 0;
891
892 if (pdh->pdh_dentry)
893 ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo);
894 if (ret == 0)
895 list_add_tail(&pdo->pdo_head, &pdh->pdh_entries);
896 return ret;
897}
898
899static void
900rpc_remove_pipe_dir_object_locked(struct net *net,
901 struct rpc_pipe_dir_head *pdh,
902 struct rpc_pipe_dir_object *pdo)
903{
904 if (pdh->pdh_dentry)
905 pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo);
906 list_del_init(&pdo->pdo_head);
907}
908
909/**
910 * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory
911 * @net: pointer to struct net
912 * @pdh: pointer to struct rpc_pipe_dir_head
913 * @pdo: pointer to struct rpc_pipe_dir_object
914 *
915 */
916int
917rpc_add_pipe_dir_object(struct net *net,
918 struct rpc_pipe_dir_head *pdh,
919 struct rpc_pipe_dir_object *pdo)
920{
921 int ret = 0;
922
923 if (list_empty(&pdo->pdo_head)) {
924 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
925
926 mutex_lock(&sn->pipefs_sb_lock);
927 ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo);
928 mutex_unlock(&sn->pipefs_sb_lock);
929 }
930 return ret;
931}
932EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object);
933
934/**
935 * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory
936 * @net: pointer to struct net
937 * @pdh: pointer to struct rpc_pipe_dir_head
938 * @pdo: pointer to struct rpc_pipe_dir_object
939 *
940 */
941void
942rpc_remove_pipe_dir_object(struct net *net,
943 struct rpc_pipe_dir_head *pdh,
944 struct rpc_pipe_dir_object *pdo)
945{
946 if (!list_empty(&pdo->pdo_head)) {
947 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
948
949 mutex_lock(&sn->pipefs_sb_lock);
950 rpc_remove_pipe_dir_object_locked(net, pdh, pdo);
951 mutex_unlock(&sn->pipefs_sb_lock);
952 }
953}
954EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object);
955
298fc355
TM
956/**
957 * rpc_find_or_alloc_pipe_dir_object
958 * @net: pointer to struct net
959 * @pdh: pointer to struct rpc_pipe_dir_head
960 * @match: match struct rpc_pipe_dir_object to data
961 * @alloc: allocate a new struct rpc_pipe_dir_object
962 * @data: user defined data for match() and alloc()
963 *
964 */
965struct rpc_pipe_dir_object *
966rpc_find_or_alloc_pipe_dir_object(struct net *net,
967 struct rpc_pipe_dir_head *pdh,
968 int (*match)(struct rpc_pipe_dir_object *, void *),
969 struct rpc_pipe_dir_object *(*alloc)(void *),
970 void *data)
971{
972 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
973 struct rpc_pipe_dir_object *pdo;
974
975 mutex_lock(&sn->pipefs_sb_lock);
976 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) {
977 if (!match(pdo, data))
978 continue;
979 goto out;
980 }
981 pdo = alloc(data);
982 if (!pdo)
983 goto out;
984 rpc_add_pipe_dir_object_locked(net, pdh, pdo);
985out:
986 mutex_unlock(&sn->pipefs_sb_lock);
987 return pdo;
988}
989EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object);
990
6739ffb7
TM
991static void
992rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
993{
994 struct rpc_pipe_dir_object *pdo;
995 struct dentry *dir = pdh->pdh_dentry;
996
997 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
998 pdo->pdo_ops->create(dir, pdo);
999}
1000
1001static void
1002rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
1003{
1004 struct rpc_pipe_dir_object *pdo;
1005 struct dentry *dir = pdh->pdh_dentry;
1006
1007 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
1008 pdo->pdo_ops->destroy(dir, pdo);
1009}
1010
e57aed77
TM
1011enum {
1012 RPCAUTH_info,
1013 RPCAUTH_EOF
1014};
1015
1016static const struct rpc_filelist authfiles[] = {
1017 [RPCAUTH_info] = {
1018 .name = "info",
1019 .i_fop = &rpc_info_operations,
d6444062 1020 .mode = S_IFREG | 0400,
e57aed77
TM
1021 },
1022};
1023
1024static int rpc_clntdir_populate(struct dentry *dentry, void *private)
1025{
1026 return rpc_populate(dentry,
1027 authfiles, RPCAUTH_info, RPCAUTH_EOF,
1028 private);
1029}
1030
1031static void rpc_clntdir_depopulate(struct dentry *dentry)
1032{
1033 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
1034}
1035
7d59d1e8
TM
1036/**
1037 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
a95e691f
AV
1038 * @dentry: the parent of new directory
1039 * @name: the name of new directory
7d59d1e8
TM
1040 * @rpc_client: rpc client to associate with this directory
1041 *
1042 * This creates a directory at the given @path associated with
1043 * @rpc_clnt, which will contain a file named "info" with some basic
1044 * information about the client, together with any "pipes" that may
1045 * later be created using rpc_mkpipe().
1046 */
23ac6581 1047struct dentry *rpc_create_client_dir(struct dentry *dentry,
a95e691f 1048 const char *name,
e57aed77
TM
1049 struct rpc_clnt *rpc_client)
1050{
6739ffb7
TM
1051 struct dentry *ret;
1052
d6444062
JP
1053 ret = rpc_mkdir_populate(dentry, name, 0555, NULL,
1054 rpc_clntdir_populate, rpc_client);
6739ffb7
TM
1055 if (!IS_ERR(ret)) {
1056 rpc_client->cl_pipedir_objects.pdh_dentry = ret;
1057 rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1058 }
1059 return ret;
e57aed77
TM
1060}
1061
1062/**
1063 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
6739ffb7 1064 * @rpc_client: rpc_client for the pipe
e57aed77 1065 */
c36dcfe1 1066int rpc_remove_client_dir(struct rpc_clnt *rpc_client)
7d59d1e8 1067{
c36dcfe1
TM
1068 struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry;
1069
1070 if (dentry == NULL)
1071 return 0;
1072 rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1073 rpc_client->cl_pipedir_objects.pdh_dentry = NULL;
e57aed77 1074 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
7d59d1e8
TM
1075}
1076
8854e82d
TM
1077static const struct rpc_filelist cache_pipefs_files[3] = {
1078 [0] = {
1079 .name = "channel",
1080 .i_fop = &cache_file_operations_pipefs,
d6444062 1081 .mode = S_IFREG | 0600,
8854e82d
TM
1082 },
1083 [1] = {
1084 .name = "content",
1085 .i_fop = &content_file_operations_pipefs,
d6444062 1086 .mode = S_IFREG | 0400,
8854e82d
TM
1087 },
1088 [2] = {
1089 .name = "flush",
1090 .i_fop = &cache_flush_operations_pipefs,
d6444062 1091 .mode = S_IFREG | 0600,
8854e82d
TM
1092 },
1093};
1094
1095static int rpc_cachedir_populate(struct dentry *dentry, void *private)
1096{
1097 return rpc_populate(dentry,
1098 cache_pipefs_files, 0, 3,
1099 private);
1100}
1101
1102static void rpc_cachedir_depopulate(struct dentry *dentry)
1103{
1104 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
1105}
1106
a95e691f 1107struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name,
64f1426f 1108 umode_t umode, struct cache_detail *cd)
8854e82d
TM
1109{
1110 return rpc_mkdir_populate(parent, name, umode, NULL,
1111 rpc_cachedir_populate, cd);
1112}
1113
1114void rpc_remove_cache_dir(struct dentry *dentry)
1115{
1116 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
1117}
1118
1da177e4
LT
1119/*
1120 * populate the filesystem
1121 */
b87221de 1122static const struct super_operations s_ops = {
1da177e4 1123 .alloc_inode = rpc_alloc_inode,
bef252fa 1124 .free_inode = rpc_free_inode,
1da177e4
LT
1125 .statfs = simple_statfs,
1126};
1127
1128#define RPCAUTH_GSSMAGIC 0x67596969
1129
bb156749
TM
1130/*
1131 * We have a single directory with 1 node in it.
1132 */
1133enum {
1134 RPCAUTH_lockd,
1135 RPCAUTH_mount,
1136 RPCAUTH_nfs,
1137 RPCAUTH_portmap,
1138 RPCAUTH_statd,
1139 RPCAUTH_nfsd4_cb,
e571cbf1 1140 RPCAUTH_cache,
b3537c35 1141 RPCAUTH_nfsd,
4b9a445e 1142 RPCAUTH_gssd,
bb156749
TM
1143 RPCAUTH_RootEOF
1144};
1145
1146static const struct rpc_filelist files[] = {
1147 [RPCAUTH_lockd] = {
1148 .name = "lockd",
d6444062 1149 .mode = S_IFDIR | 0555,
bb156749
TM
1150 },
1151 [RPCAUTH_mount] = {
1152 .name = "mount",
d6444062 1153 .mode = S_IFDIR | 0555,
bb156749
TM
1154 },
1155 [RPCAUTH_nfs] = {
1156 .name = "nfs",
d6444062 1157 .mode = S_IFDIR | 0555,
bb156749
TM
1158 },
1159 [RPCAUTH_portmap] = {
1160 .name = "portmap",
d6444062 1161 .mode = S_IFDIR | 0555,
bb156749
TM
1162 },
1163 [RPCAUTH_statd] = {
1164 .name = "statd",
d6444062 1165 .mode = S_IFDIR | 0555,
bb156749
TM
1166 },
1167 [RPCAUTH_nfsd4_cb] = {
1168 .name = "nfsd4_cb",
d6444062 1169 .mode = S_IFDIR | 0555,
bb156749 1170 },
e571cbf1
TM
1171 [RPCAUTH_cache] = {
1172 .name = "cache",
d6444062 1173 .mode = S_IFDIR | 0555,
e571cbf1 1174 },
b3537c35
JL
1175 [RPCAUTH_nfsd] = {
1176 .name = "nfsd",
d6444062 1177 .mode = S_IFDIR | 0555,
b3537c35 1178 },
4b9a445e
JL
1179 [RPCAUTH_gssd] = {
1180 .name = "gssd",
d6444062 1181 .mode = S_IFDIR | 0555,
4b9a445e 1182 },
bb156749
TM
1183};
1184
432eb1a5
SK
1185/*
1186 * This call can be used only in RPC pipefs mount notification hooks.
1187 */
1188struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1189 const unsigned char *dir_name)
1190{
26fe5750 1191 struct qstr dir = QSTR_INIT(dir_name, strlen(dir_name));
d3db90b0 1192 return d_hash_and_lookup(sb->s_root, &dir);
432eb1a5
SK
1193}
1194EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1195
4b9a445e 1196int rpc_pipefs_init_net(struct net *net)
c21a588f
SK
1197{
1198 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1199
4b9a445e
JL
1200 sn->gssd_dummy = rpc_mkpipe_data(&gssd_dummy_pipe_ops, 0);
1201 if (IS_ERR(sn->gssd_dummy))
1202 return PTR_ERR(sn->gssd_dummy);
1203
c21a588f 1204 mutex_init(&sn->pipefs_sb_lock);
2aed8b47 1205 sn->pipe_version = -1;
4b9a445e
JL
1206 return 0;
1207}
1208
1209void rpc_pipefs_exit_net(struct net *net)
1210{
1211 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1212
1213 rpc_destroy_pipe_data(sn->gssd_dummy);
c21a588f
SK
1214}
1215
1216/*
1217 * This call will be used for per network namespace operations calls.
1218 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1219 * found. This lock have to be released by rpc_put_sb_net() when all operations
1220 * will be completed.
1221 */
1222struct super_block *rpc_get_sb_net(const struct net *net)
1223{
1224 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1225
1226 mutex_lock(&sn->pipefs_sb_lock);
1227 if (sn->pipefs_sb)
1228 return sn->pipefs_sb;
1229 mutex_unlock(&sn->pipefs_sb_lock);
1230 return NULL;
1231}
1232EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1233
1234void rpc_put_sb_net(const struct net *net)
1235{
1236 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1237
749386e9 1238 WARN_ON(sn->pipefs_sb == NULL);
c21a588f
SK
1239 mutex_unlock(&sn->pipefs_sb_lock);
1240}
1241EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1242
4b9a445e
JL
1243static const struct rpc_filelist gssd_dummy_clnt_dir[] = {
1244 [0] = {
1245 .name = "clntXX",
d6444062 1246 .mode = S_IFDIR | 0555,
4b9a445e
JL
1247 },
1248};
1249
1250static ssize_t
1251dummy_downcall(struct file *filp, const char __user *src, size_t len)
1252{
1253 return -EINVAL;
1254}
1255
1256static const struct rpc_pipe_ops gssd_dummy_pipe_ops = {
1257 .upcall = rpc_pipe_generic_upcall,
1258 .downcall = dummy_downcall,
1259};
1260
e2f0c83a
JL
1261/*
1262 * Here we present a bogus "info" file to keep rpc.gssd happy. We don't expect
1263 * that it will ever use this info to handle an upcall, but rpc.gssd expects
1264 * that this file will be there and have a certain format.
1265 */
1266static int
260f71ef 1267rpc_dummy_info_show(struct seq_file *m, void *v)
e2f0c83a
JL
1268{
1269 seq_printf(m, "RPC server: %s\n", utsname()->nodename);
1270 seq_printf(m, "service: foo (1) version 0\n");
1271 seq_printf(m, "address: 127.0.0.1\n");
1272 seq_printf(m, "protocol: tcp\n");
1273 seq_printf(m, "port: 0\n");
1274 return 0;
1275}
260f71ef 1276DEFINE_SHOW_ATTRIBUTE(rpc_dummy_info);
e2f0c83a
JL
1277
1278static const struct rpc_filelist gssd_dummy_info_file[] = {
1279 [0] = {
1280 .name = "info",
260f71ef 1281 .i_fop = &rpc_dummy_info_fops,
d6444062 1282 .mode = S_IFREG | 0400,
e2f0c83a
JL
1283 },
1284};
1285
4b9a445e
JL
1286/**
1287 * rpc_gssd_dummy_populate - create a dummy gssd pipe
1288 * @root: root of the rpc_pipefs filesystem
1289 * @pipe_data: pipe data created when netns is initialized
1290 *
1291 * Create a dummy set of directories and a pipe that gssd can hold open to
1292 * indicate that it is up and running.
1293 */
1294static struct dentry *
1295rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
1296{
1297 int ret = 0;
1298 struct dentry *gssd_dentry;
1299 struct dentry *clnt_dentry = NULL;
1300 struct dentry *pipe_dentry = NULL;
1301 struct qstr q = QSTR_INIT(files[RPCAUTH_gssd].name,
1302 strlen(files[RPCAUTH_gssd].name));
1303
1304 /* We should never get this far if "gssd" doesn't exist */
1305 gssd_dentry = d_hash_and_lookup(root, &q);
1306 if (!gssd_dentry)
1307 return ERR_PTR(-ENOENT);
1308
1309 ret = rpc_populate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1, NULL);
1310 if (ret) {
1311 pipe_dentry = ERR_PTR(ret);
1312 goto out;
1313 }
1314
1315 q.name = gssd_dummy_clnt_dir[0].name;
1316 q.len = strlen(gssd_dummy_clnt_dir[0].name);
1317 clnt_dentry = d_hash_and_lookup(gssd_dentry, &q);
1318 if (!clnt_dentry) {
1319 pipe_dentry = ERR_PTR(-ENOENT);
1320 goto out;
1321 }
1322
e2f0c83a
JL
1323 ret = rpc_populate(clnt_dentry, gssd_dummy_info_file, 0, 1, NULL);
1324 if (ret) {
1325 __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
1326 pipe_dentry = ERR_PTR(ret);
1327 goto out;
1328 }
1329
4b9a445e 1330 pipe_dentry = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data);
e2f0c83a
JL
1331 if (IS_ERR(pipe_dentry)) {
1332 __rpc_depopulate(clnt_dentry, gssd_dummy_info_file, 0, 1);
3396f92f 1333 __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
e2f0c83a 1334 }
4b9a445e
JL
1335out:
1336 dput(clnt_dentry);
1337 dput(gssd_dentry);
1338 return pipe_dentry;
1339}
1340
23e66ba9
JL
1341static void
1342rpc_gssd_dummy_depopulate(struct dentry *pipe_dentry)
1343{
1344 struct dentry *clnt_dir = pipe_dentry->d_parent;
1345 struct dentry *gssd_dir = clnt_dir->d_parent;
1346
4a3877c4 1347 dget(pipe_dentry);
c5ef6035 1348 __rpc_rmpipe(d_inode(clnt_dir), pipe_dentry);
23e66ba9
JL
1349 __rpc_depopulate(clnt_dir, gssd_dummy_info_file, 0, 1);
1350 __rpc_depopulate(gssd_dir, gssd_dummy_clnt_dir, 0, 1);
1351 dput(pipe_dentry);
1352}
1353
1da177e4
LT
1354static int
1355rpc_fill_super(struct super_block *sb, void *data, int silent)
1356{
1357 struct inode *inode;
4b9a445e 1358 struct dentry *root, *gssd_dentry;
d91ee87d 1359 struct net *net = get_net(sb->s_fs_info);
90c4e829 1360 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2d00131a 1361 int err;
1da177e4 1362
09cbfeaf
KS
1363 sb->s_blocksize = PAGE_SIZE;
1364 sb->s_blocksize_bits = PAGE_SHIFT;
1da177e4
LT
1365 sb->s_magic = RPCAUTH_GSSMAGIC;
1366 sb->s_op = &s_ops;
b26d4cd3 1367 sb->s_d_op = &simple_dentry_operations;
1da177e4
LT
1368 sb->s_time_gran = 1;
1369
d6444062 1370 inode = rpc_get_inode(sb, S_IFDIR | 0555);
48fde701
AV
1371 sb->s_root = root = d_make_root(inode);
1372 if (!root)
1da177e4 1373 return -ENOMEM;
ac6fecee 1374 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
fc7bed8c 1375 return -ENOMEM;
4b9a445e
JL
1376
1377 gssd_dentry = rpc_gssd_dummy_populate(root, sn->gssd_dummy);
1378 if (IS_ERR(gssd_dentry)) {
1379 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1380 return PTR_ERR(gssd_dentry);
1381 }
1382
6c67a3e4
VA
1383 dprintk("RPC: sending pipefs MOUNT notification for net %x%s\n",
1384 net->ns.inum, NET_NAME(net));
38481605 1385 mutex_lock(&sn->pipefs_sb_lock);
37629b57 1386 sn->pipefs_sb = sb;
2d00131a
SK
1387 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1388 RPC_PIPEFS_MOUNT,
1389 sb);
1390 if (err)
1391 goto err_depopulate;
38481605 1392 mutex_unlock(&sn->pipefs_sb_lock);
1da177e4 1393 return 0;
2d00131a
SK
1394
1395err_depopulate:
23e66ba9 1396 rpc_gssd_dummy_depopulate(gssd_dentry);
2d00131a
SK
1397 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1398 RPC_PIPEFS_UMOUNT,
1399 sb);
37629b57 1400 sn->pipefs_sb = NULL;
2d00131a 1401 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
38481605 1402 mutex_unlock(&sn->pipefs_sb_lock);
2d00131a 1403 return err;
1da177e4
LT
1404}
1405
89f84243
JL
1406bool
1407gssd_running(struct net *net)
1408{
1409 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1410 struct rpc_pipe *pipe = sn->gssd_dummy;
1411
1412 return pipe->nreaders || pipe->nwriters;
1413}
1414EXPORT_SYMBOL_GPL(gssd_running);
1415
fc14f2fe
AV
1416static struct dentry *
1417rpc_mount(struct file_system_type *fs_type,
1418 int flags, const char *dev_name, void *data)
1da177e4 1419{
d91ee87d
EB
1420 struct net *net = current->nsproxy->net_ns;
1421 return mount_ns(fs_type, flags, data, net, net->user_ns, rpc_fill_super);
1da177e4
LT
1422}
1423
09acfea5 1424static void rpc_kill_sb(struct super_block *sb)
021c68de
SK
1425{
1426 struct net *net = sb->s_fs_info;
90c4e829 1427 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
021c68de 1428
c21a588f 1429 mutex_lock(&sn->pipefs_sb_lock);
642fe4d0
TM
1430 if (sn->pipefs_sb != sb) {
1431 mutex_unlock(&sn->pipefs_sb_lock);
1432 goto out;
1433 }
90c4e829 1434 sn->pipefs_sb = NULL;
6c67a3e4
VA
1435 dprintk("RPC: sending pipefs UMOUNT notification for net %x%s\n",
1436 net->ns.inum, NET_NAME(net));
2d00131a
SK
1437 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1438 RPC_PIPEFS_UMOUNT,
1439 sb);
adb6fa7f 1440 mutex_unlock(&sn->pipefs_sb_lock);
642fe4d0 1441out:
021c68de 1442 kill_litter_super(sb);
d91ee87d 1443 put_net(net);
1da177e4
LT
1444}
1445
1446static struct file_system_type rpc_pipe_fs_type = {
1447 .owner = THIS_MODULE,
1448 .name = "rpc_pipefs",
fc14f2fe 1449 .mount = rpc_mount,
021c68de 1450 .kill_sb = rpc_kill_sb,
1da177e4 1451};
7f78e035 1452MODULE_ALIAS_FS("rpc_pipefs");
fa7614dd 1453MODULE_ALIAS("rpc_pipefs");
1da177e4
LT
1454
1455static void
51cc5068 1456init_once(void *foo)
1da177e4
LT
1457{
1458 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1459
a35afb83
CL
1460 inode_init_once(&rpci->vfs_inode);
1461 rpci->private = NULL;
ba9e0975 1462 rpci->pipe = NULL;
a35afb83 1463 init_waitqueue_head(&rpci->waitq);
1da177e4
LT
1464}
1465
1466int register_rpc_pipefs(void)
1467{
5bd5f581
AM
1468 int err;
1469
1da177e4 1470 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
fffb60f9
PJ
1471 sizeof(struct rpc_inode),
1472 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
5d097056 1473 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
20c2df83 1474 init_once);
1da177e4
LT
1475 if (!rpc_inode_cachep)
1476 return -ENOMEM;
80df9d20
SK
1477 err = rpc_clients_notifier_register();
1478 if (err)
1479 goto err_notifier;
5bd5f581 1480 err = register_filesystem(&rpc_pipe_fs_type);
80df9d20
SK
1481 if (err)
1482 goto err_register;
1da177e4 1483 return 0;
80df9d20
SK
1484
1485err_register:
1486 rpc_clients_notifier_unregister();
1487err_notifier:
1488 kmem_cache_destroy(rpc_inode_cachep);
1489 return err;
1da177e4
LT
1490}
1491
1492void unregister_rpc_pipefs(void)
1493{
80df9d20 1494 rpc_clients_notifier_unregister();
1a1d92c1 1495 kmem_cache_destroy(rpc_inode_cachep);
1da177e4
LT
1496 unregister_filesystem(&rpc_pipe_fs_type);
1497}