11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
18 #include <linux/kernel.h>
21 #include <asm/ioctls.h>
22 #include <linux/poll.h>
23 #include <linux/wait.h>
36 #define RPCDBG_FACILITY RPCDBG_DEBUG
38 #define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
45 #define RPC_UPCALL_TIMEOUT (30*HZ)
62 void (*destroy_msg)(
struct rpc_pipe_msg *),
int err)
64 struct rpc_pipe_msg *
msg;
70 list_del_init(&msg->list);
73 }
while (!list_empty(head));
83 struct rpc_pipe *
pipe =
85 void (*destroy_msg)(
struct rpc_pipe_msg *);
88 spin_lock(&pipe->lock);
89 destroy_msg = pipe->ops->destroy_msg;
90 if (pipe->nreaders == 0) {
91 list_splice_init(&pipe->pipe, &free_list);
94 dentry = dget(pipe->dentry);
95 spin_unlock(&pipe->lock);
96 rpc_purge_list(dentry ? &RPC_I(dentry->
d_inode)->waitq :
NULL,
104 char *
data = (
char *)msg->data + msg->copied;
105 size_t mlen =
min(msg->len - msg->copied, buflen);
135 struct dentry *dentry;
137 spin_lock(&pipe->lock);
138 if (pipe->nreaders) {
140 pipe->pipelen += msg->len;
142 }
else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
143 if (list_empty(&pipe->pipe))
145 &pipe->queue_timeout,
148 pipe->pipelen += msg->len;
151 dentry = dget(pipe->dentry);
152 spin_unlock(&pipe->lock);
162 rpc_inode_setowner(
struct inode *
inode,
void *
private)
164 RPC_I(inode)->private =
private;
170 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
175 spin_lock(&pipe->lock);
176 need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
178 list_splice_init(&pipe->in_upcall, &free_list);
179 list_splice_init(&pipe->pipe, &free_list);
182 spin_unlock(&pipe->lock);
183 rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -
EPIPE);
185 if (need_release && pipe->ops->release_pipe)
186 pipe->ops->release_pipe(inode);
188 rpc_inode_setowner(inode,
NULL);
189 RPC_I(inode)->pipe =
NULL;
193 static struct inode *
196 struct rpc_inode *rpci;
200 return &rpci->vfs_inode;
204 rpc_i_callback(
struct rcu_head *head)
211 rpc_destroy_inode(
struct inode *inode)
217 rpc_pipe_open(
struct inode *inode,
struct file *filp)
219 struct rpc_pipe *
pipe;
224 pipe = RPC_I(inode)->pipe;
227 first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
228 if (first_open && pipe->ops->open_pipe) {
229 res = pipe->ops->open_pipe(inode);
244 rpc_pipe_release(
struct inode *inode,
struct file *filp)
246 struct rpc_pipe *
pipe;
247 struct rpc_pipe_msg *
msg;
251 pipe = RPC_I(inode)->pipe;
256 spin_lock(&pipe->lock);
258 list_del_init(&msg->list);
259 spin_unlock(&pipe->lock);
260 pipe->ops->destroy_msg(msg);
266 if (pipe->nreaders == 0) {
268 spin_lock(&pipe->lock);
269 list_splice_init(&pipe->pipe, &free_list);
271 spin_unlock(&pipe->lock);
272 rpc_purge_list(&RPC_I(inode)->waitq, &free_list,
273 pipe->ops->destroy_msg, -
EAGAIN);
276 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
277 if (last_close && pipe->ops->release_pipe)
278 pipe->ops->release_pipe(inode);
285 rpc_pipe_read(
struct file *filp,
char __user *
buf,
size_t len, loff_t *
offset)
287 struct inode *inode = filp->
f_path.dentry->d_inode;
288 struct rpc_pipe *
pipe;
289 struct rpc_pipe_msg *
msg;
293 pipe = RPC_I(inode)->pipe;
300 spin_lock(&pipe->lock);
301 if (!list_empty(&pipe->pipe)) {
305 list_move(&msg->list, &pipe->in_upcall);
306 pipe->pipelen -= msg->len;
310 spin_unlock(&pipe->lock);
315 res = pipe->ops->upcall(filp, msg, buf, len);
316 if (res < 0 || msg->len == msg->copied) {
318 spin_lock(&pipe->lock);
319 list_del_init(&msg->list);
320 spin_unlock(&pipe->lock);
321 pipe->ops->destroy_msg(msg);
329 rpc_pipe_write(
struct file *filp,
const char __user *buf,
size_t len, loff_t *offset)
331 struct inode *inode = filp->
f_path.dentry->d_inode;
336 if (RPC_I(inode)->pipe !=
NULL)
337 res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
345 struct inode *inode = filp->
f_path.dentry->d_inode;
346 struct rpc_inode *rpci = RPC_I(inode);
349 poll_wait(filp, &rpci->waitq, wait);
352 if (rpci->pipe ==
NULL)
354 else if (filp->
private_data || !list_empty(&rpci->pipe->pipe))
361 rpc_pipe_ioctl(
struct file *filp,
unsigned int cmd,
unsigned long arg)
363 struct inode *inode = filp->
f_path.dentry->d_inode;
364 struct rpc_pipe *
pipe;
370 pipe = RPC_I(inode)->pipe;
375 spin_lock(&pipe->lock);
378 struct rpc_pipe_msg *
msg;
380 len += msg->len - msg->copied;
382 spin_unlock(&pipe->lock);
384 return put_user(len, (
int __user *)arg);
393 .read = rpc_pipe_read,
394 .write = rpc_pipe_write,
395 .poll = rpc_pipe_poll,
396 .unlocked_ioctl = rpc_pipe_ioctl,
397 .open = rpc_pipe_open,
398 .release = rpc_pipe_release,
419 rpc_info_open(
struct inode *inode,
struct file *
file)
427 spin_lock(&file->
f_path.dentry->d_lock);
428 if (!d_unhashed(file->
f_path.dentry))
429 clnt = RPC_I(inode)->private;
431 spin_unlock(&file->
f_path.dentry->d_lock);
434 spin_unlock(&file->
f_path.dentry->d_lock);
443 rpc_info_release(
struct inode *inode,
struct file *file)
455 .open = rpc_info_open,
458 .release = rpc_info_release,
471 static int rpc_delete_dentry(
const struct dentry *dentry)
477 .d_delete = rpc_delete_dentry,
480 static struct inode *
500 static int __rpc_create_common(
struct inode *dir,
struct dentry *dentry,
508 inode = rpc_get_inode(dir->
i_sb, mode);
515 rpc_inode_setowner(inode,
private);
516 d_add(dentry, inode);
520 __FILE__, __func__, dentry->
d_name.name);
525 static int __rpc_create(
struct inode *dir,
struct dentry *dentry,
532 err = __rpc_create_common(dir, dentry,
S_IFREG | mode, i_fop,
private);
535 fsnotify_create(dir, dentry);
539 static int __rpc_mkdir(
struct inode *dir,
struct dentry *dentry,
546 err = __rpc_create_common(dir, dentry,
S_IFDIR | mode, i_fop,
private);
550 fsnotify_mkdir(dir, dentry);
555 init_pipe(
struct rpc_pipe *pipe)
559 INIT_LIST_HEAD(&pipe->in_upcall);
560 INIT_LIST_HEAD(&pipe->in_downcall);
561 INIT_LIST_HEAD(&pipe->pipe);
564 rpc_timeout_upcall_queue);
578 struct rpc_pipe *
pipe;
580 pipe = kzalloc(
sizeof(
struct rpc_pipe),
GFP_KERNEL);
590 static int __rpc_mkpipe_dentry(
struct inode *dir,
struct dentry *dentry,
594 struct rpc_pipe *pipe)
596 struct rpc_inode *rpci;
599 err = __rpc_create_common(dir, dentry,
S_IFIFO | mode, i_fop,
private);
603 rpci->private =
private;
605 fsnotify_create(dir, dentry);
609 static int __rpc_rmdir(
struct inode *dir,
struct dentry *dentry)
629 error = __rpc_rmdir(dir, dentry);
636 static int __rpc_unlink(
struct inode *dir,
struct dentry *dentry)
647 static int __rpc_rmpipe(
struct inode *dir,
struct dentry *dentry)
649 struct inode *inode = dentry->
d_inode;
651 rpc_close_pipes(inode);
652 return __rpc_unlink(dir, dentry);
655 static struct dentry *__rpc_lookup_create_exclusive(
struct dentry *parent,
658 struct dentry *dentry;
662 dentry =
d_alloc(parent, name);
677 static void __rpc_depopulate(
struct dentry *parent,
681 struct inode *dir = parent->
d_inode;
682 struct dentry *dentry;
686 for (i = start; i < eof; i++) {
688 name.len =
strlen(files[i].name);
700 __rpc_unlink(dir, dentry);
703 __rpc_rmdir(dir, dentry);
710 static void rpc_depopulate(
struct dentry *parent,
714 struct inode *dir = parent->
d_inode;
717 __rpc_depopulate(parent, files, start, eof);
721 static int rpc_populate(
struct dentry *parent,
726 struct inode *dir = parent->
d_inode;
727 struct dentry *dentry;
731 for (i = start; i < eof; i++) {
737 dentry = __rpc_lookup_create_exclusive(parent, &
q);
738 err = PTR_ERR(dentry);
741 switch (files[i].mode &
S_IFMT) {
745 err = __rpc_create(dir, dentry,
751 err = __rpc_mkdir(dir, dentry,
762 __rpc_depopulate(parent, files, start, eof);
765 __FILE__, __func__, parent->
d_name.name);
769 static struct dentry *rpc_mkdir_populate(
struct dentry *parent,
771 int (*populate)(
struct dentry *,
void *),
void *args_populate)
773 struct dentry *dentry;
774 struct inode *dir = parent->
d_inode;
778 dentry = __rpc_lookup_create_exclusive(parent, name);
781 error = __rpc_mkdir(dir, dentry, mode,
NULL,
private);
784 if (populate !=
NULL) {
785 error = populate(dentry, args_populate);
793 __rpc_rmdir(dir, dentry);
795 dentry = ERR_PTR(error);
799 static int rpc_rmdir_depopulate(
struct dentry *dentry,
800 void (*depopulate)(
struct dentry *))
802 struct dentry *parent;
809 if (depopulate !=
NULL)
811 error = __rpc_rmdir(dir, dentry);
836 void *
private,
struct rpc_pipe *pipe)
838 struct dentry *dentry;
839 struct inode *dir = parent->
d_inode;
844 if (pipe->ops->upcall ==
NULL)
846 if (pipe->ops->downcall ==
NULL)
854 dentry = __rpc_lookup_create_exclusive(parent, &q);
857 err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
865 dentry = ERR_PTR(err);
867 __FILE__, __func__, parent->
d_name.name, name,
884 struct dentry *parent;
891 error = __rpc_rmpipe(dir, dentry);
906 .i_fop = &rpc_info_operations,
911 static int rpc_clntdir_populate(
struct dentry *dentry,
void *
private)
913 return rpc_populate(dentry,
918 static void rpc_clntdir_depopulate(
struct dentry *dentry)
939 rpc_clntdir_populate, rpc_client);
948 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
951 static const struct rpc_filelist cache_pipefs_files[3] = {
969 static int rpc_cachedir_populate(
struct dentry *dentry,
void *
private)
971 return rpc_populate(dentry,
972 cache_pipefs_files, 0, 3,
976 static void rpc_cachedir_depopulate(
struct dentry *dentry)
978 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
984 return rpc_mkdir_populate(parent, name, umode,
NULL,
985 rpc_cachedir_populate, cd);
990 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
997 .alloc_inode = rpc_alloc_inode,
998 .destroy_inode = rpc_destroy_inode,
1002 #define RPCAUTH_GSSMAGIC 0x67596969
1058 const unsigned char *dir_name)
1104 struct inode *
inode;
1105 struct dentry *
root;
1122 dprintk(
"RPC: sending pipefs MOUNT notification for net %p%s\n",
1129 goto err_depopulate;
1142 static struct dentry *
1144 int flags,
const char *dev_name,
void *data)
1146 return mount_ns(fs_type, flags,
current->nsproxy->net_ns, rpc_fill_super);
1158 dprintk(
"RPC: sending pipefs UMOUNT notification for net %p%s\n",
1168 .name =
"rpc_pipefs",
1170 .kill_sb = rpc_kill_sb,
1174 init_once(
void *
foo)
1176 struct rpc_inode *rpci = (
struct rpc_inode *) foo;
1179 rpci->private =
NULL;
1189 sizeof(
struct rpc_inode),
1193 if (!rpc_inode_cachep)