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