Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/sunrpc/rpc_pipe.c | |
3 | * | |
4 | * Userland/kernel interface for rpcauth_gss. | |
5 | * Code shamelessly plagiarized from fs/nfsd/nfsctl.c | |
d51fe1be | 6 | * and fs/sysfs/inode.c |
1da177e4 LT |
7 | * |
8 | * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no> | |
9 | * | |
10 | */ | |
11 | #include <linux/config.h> | |
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> | |
18 | #include <linux/dnotify.h> | |
19 | #include <linux/kernel.h> | |
20 | ||
21 | #include <asm/ioctls.h> | |
22 | #include <linux/fs.h> | |
23 | #include <linux/poll.h> | |
24 | #include <linux/wait.h> | |
25 | #include <linux/seq_file.h> | |
26 | ||
27 | #include <linux/sunrpc/clnt.h> | |
28 | #include <linux/workqueue.h> | |
29 | #include <linux/sunrpc/rpc_pipe_fs.h> | |
30 | ||
ba89966c | 31 | static struct vfsmount *rpc_mount __read_mostly; |
1da177e4 LT |
32 | static int rpc_mount_count; |
33 | ||
34 | static struct file_system_type rpc_pipe_fs_type; | |
35 | ||
36 | ||
ba89966c | 37 | static kmem_cache_t *rpc_inode_cachep __read_mostly; |
1da177e4 LT |
38 | |
39 | #define RPC_UPCALL_TIMEOUT (30*HZ) | |
40 | ||
41 | static void | |
b3eb67a2 | 42 | __rpc_purge_list(struct rpc_inode *rpci, struct list_head *head, int err) |
1da177e4 | 43 | { |
1da177e4 | 44 | struct rpc_pipe_msg *msg; |
b3eb67a2 | 45 | void (*destroy_msg)(struct rpc_pipe_msg *); |
1da177e4 | 46 | |
b3eb67a2 TM |
47 | destroy_msg = rpci->ops->destroy_msg; |
48 | while (!list_empty(head)) { | |
49 | msg = list_entry(head->next, struct rpc_pipe_msg, list); | |
1da177e4 LT |
50 | list_del_init(&msg->list); |
51 | msg->errno = err; | |
b3eb67a2 | 52 | destroy_msg(msg); |
1da177e4 | 53 | } |
b3eb67a2 TM |
54 | } |
55 | ||
56 | static void | |
57 | __rpc_purge_upcall(struct inode *inode, int err) | |
58 | { | |
59 | struct rpc_inode *rpci = RPC_I(inode); | |
60 | ||
61 | __rpc_purge_list(rpci, &rpci->pipe, err); | |
1da177e4 LT |
62 | rpci->pipelen = 0; |
63 | wake_up(&rpci->waitq); | |
64 | } | |
65 | ||
66 | static void | |
67 | rpc_timeout_upcall_queue(void *data) | |
68 | { | |
69 | struct rpc_inode *rpci = (struct rpc_inode *)data; | |
70 | struct inode *inode = &rpci->vfs_inode; | |
71 | ||
1b1dcc1b | 72 | mutex_lock(&inode->i_mutex); |
969b7f25 TM |
73 | if (rpci->ops == NULL) |
74 | goto out; | |
1da177e4 LT |
75 | if (rpci->nreaders == 0 && !list_empty(&rpci->pipe)) |
76 | __rpc_purge_upcall(inode, -ETIMEDOUT); | |
969b7f25 | 77 | out: |
1b1dcc1b | 78 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
79 | } |
80 | ||
81 | int | |
82 | rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg) | |
83 | { | |
84 | struct rpc_inode *rpci = RPC_I(inode); | |
6070fe6f | 85 | int res = -EPIPE; |
1da177e4 | 86 | |
1b1dcc1b | 87 | mutex_lock(&inode->i_mutex); |
6070fe6f TM |
88 | if (rpci->ops == NULL) |
89 | goto out; | |
1da177e4 LT |
90 | if (rpci->nreaders) { |
91 | list_add_tail(&msg->list, &rpci->pipe); | |
92 | rpci->pipelen += msg->len; | |
6070fe6f | 93 | res = 0; |
1da177e4 LT |
94 | } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) { |
95 | if (list_empty(&rpci->pipe)) | |
96 | schedule_delayed_work(&rpci->queue_timeout, | |
97 | RPC_UPCALL_TIMEOUT); | |
98 | list_add_tail(&msg->list, &rpci->pipe); | |
99 | rpci->pipelen += msg->len; | |
6070fe6f TM |
100 | res = 0; |
101 | } | |
102 | out: | |
1b1dcc1b | 103 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
104 | wake_up(&rpci->waitq); |
105 | return res; | |
106 | } | |
107 | ||
6070fe6f TM |
108 | static inline void |
109 | rpc_inode_setowner(struct inode *inode, void *private) | |
110 | { | |
111 | RPC_I(inode)->private = private; | |
112 | } | |
113 | ||
1da177e4 LT |
114 | static void |
115 | rpc_close_pipes(struct inode *inode) | |
116 | { | |
117 | struct rpc_inode *rpci = RPC_I(inode); | |
118 | ||
1b1dcc1b | 119 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
120 | if (rpci->ops != NULL) { |
121 | rpci->nreaders = 0; | |
bb184f33 | 122 | __rpc_purge_list(rpci, &rpci->in_upcall, -EPIPE); |
1da177e4 LT |
123 | __rpc_purge_upcall(inode, -EPIPE); |
124 | rpci->nwriters = 0; | |
125 | if (rpci->ops->release_pipe) | |
126 | rpci->ops->release_pipe(inode); | |
127 | rpci->ops = NULL; | |
128 | } | |
6070fe6f | 129 | rpc_inode_setowner(inode, NULL); |
1b1dcc1b | 130 | mutex_unlock(&inode->i_mutex); |
969b7f25 TM |
131 | cancel_delayed_work(&rpci->queue_timeout); |
132 | flush_scheduled_work(); | |
1da177e4 LT |
133 | } |
134 | ||
1da177e4 LT |
135 | static struct inode * |
136 | rpc_alloc_inode(struct super_block *sb) | |
137 | { | |
138 | struct rpc_inode *rpci; | |
139 | rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, SLAB_KERNEL); | |
140 | if (!rpci) | |
141 | return NULL; | |
142 | return &rpci->vfs_inode; | |
143 | } | |
144 | ||
145 | static void | |
146 | rpc_destroy_inode(struct inode *inode) | |
147 | { | |
148 | kmem_cache_free(rpc_inode_cachep, RPC_I(inode)); | |
149 | } | |
150 | ||
151 | static int | |
152 | rpc_pipe_open(struct inode *inode, struct file *filp) | |
153 | { | |
154 | struct rpc_inode *rpci = RPC_I(inode); | |
155 | int res = -ENXIO; | |
156 | ||
1b1dcc1b | 157 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
158 | if (rpci->ops != NULL) { |
159 | if (filp->f_mode & FMODE_READ) | |
160 | rpci->nreaders ++; | |
161 | if (filp->f_mode & FMODE_WRITE) | |
162 | rpci->nwriters ++; | |
163 | res = 0; | |
164 | } | |
1b1dcc1b | 165 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
166 | return res; |
167 | } | |
168 | ||
169 | static int | |
170 | rpc_pipe_release(struct inode *inode, struct file *filp) | |
171 | { | |
969b7f25 | 172 | struct rpc_inode *rpci = RPC_I(inode); |
1da177e4 LT |
173 | struct rpc_pipe_msg *msg; |
174 | ||
1b1dcc1b | 175 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
176 | if (rpci->ops == NULL) |
177 | goto out; | |
178 | msg = (struct rpc_pipe_msg *)filp->private_data; | |
179 | if (msg != NULL) { | |
48e49187 | 180 | msg->errno = -EAGAIN; |
1da177e4 LT |
181 | list_del_init(&msg->list); |
182 | rpci->ops->destroy_msg(msg); | |
183 | } | |
184 | if (filp->f_mode & FMODE_WRITE) | |
185 | rpci->nwriters --; | |
186 | if (filp->f_mode & FMODE_READ) | |
187 | rpci->nreaders --; | |
188 | if (!rpci->nreaders) | |
48e49187 | 189 | __rpc_purge_upcall(inode, -EAGAIN); |
1da177e4 LT |
190 | if (rpci->ops->release_pipe) |
191 | rpci->ops->release_pipe(inode); | |
192 | out: | |
1b1dcc1b | 193 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
194 | return 0; |
195 | } | |
196 | ||
197 | static ssize_t | |
198 | rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset) | |
199 | { | |
200 | struct inode *inode = filp->f_dentry->d_inode; | |
201 | struct rpc_inode *rpci = RPC_I(inode); | |
202 | struct rpc_pipe_msg *msg; | |
203 | int res = 0; | |
204 | ||
1b1dcc1b | 205 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
206 | if (rpci->ops == NULL) { |
207 | res = -EPIPE; | |
208 | goto out_unlock; | |
209 | } | |
210 | msg = filp->private_data; | |
211 | if (msg == NULL) { | |
212 | if (!list_empty(&rpci->pipe)) { | |
213 | msg = list_entry(rpci->pipe.next, | |
214 | struct rpc_pipe_msg, | |
215 | list); | |
216 | list_move(&msg->list, &rpci->in_upcall); | |
217 | rpci->pipelen -= msg->len; | |
218 | filp->private_data = msg; | |
219 | msg->copied = 0; | |
220 | } | |
221 | if (msg == NULL) | |
222 | goto out_unlock; | |
223 | } | |
224 | /* NOTE: it is up to the callback to update msg->copied */ | |
225 | res = rpci->ops->upcall(filp, msg, buf, len); | |
226 | if (res < 0 || msg->len == msg->copied) { | |
227 | filp->private_data = NULL; | |
228 | list_del_init(&msg->list); | |
229 | rpci->ops->destroy_msg(msg); | |
230 | } | |
231 | out_unlock: | |
1b1dcc1b | 232 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
233 | return res; |
234 | } | |
235 | ||
236 | static ssize_t | |
237 | rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset) | |
238 | { | |
239 | struct inode *inode = filp->f_dentry->d_inode; | |
240 | struct rpc_inode *rpci = RPC_I(inode); | |
241 | int res; | |
242 | ||
1b1dcc1b | 243 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
244 | res = -EPIPE; |
245 | if (rpci->ops != NULL) | |
246 | res = rpci->ops->downcall(filp, buf, len); | |
1b1dcc1b | 247 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
248 | return res; |
249 | } | |
250 | ||
251 | static unsigned int | |
252 | rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait) | |
253 | { | |
254 | struct rpc_inode *rpci; | |
255 | unsigned int mask = 0; | |
256 | ||
257 | rpci = RPC_I(filp->f_dentry->d_inode); | |
258 | poll_wait(filp, &rpci->waitq, wait); | |
259 | ||
260 | mask = POLLOUT | POLLWRNORM; | |
261 | if (rpci->ops == NULL) | |
262 | mask |= POLLERR | POLLHUP; | |
263 | if (!list_empty(&rpci->pipe)) | |
264 | mask |= POLLIN | POLLRDNORM; | |
265 | return mask; | |
266 | } | |
267 | ||
268 | static int | |
269 | rpc_pipe_ioctl(struct inode *ino, struct file *filp, | |
270 | unsigned int cmd, unsigned long arg) | |
271 | { | |
272 | struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode); | |
273 | int len; | |
274 | ||
275 | switch (cmd) { | |
276 | case FIONREAD: | |
277 | if (rpci->ops == NULL) | |
278 | return -EPIPE; | |
279 | len = rpci->pipelen; | |
280 | if (filp->private_data) { | |
281 | struct rpc_pipe_msg *msg; | |
282 | msg = (struct rpc_pipe_msg *)filp->private_data; | |
283 | len += msg->len - msg->copied; | |
284 | } | |
285 | return put_user(len, (int __user *)arg); | |
286 | default: | |
287 | return -EINVAL; | |
288 | } | |
289 | } | |
290 | ||
291 | static struct file_operations rpc_pipe_fops = { | |
292 | .owner = THIS_MODULE, | |
293 | .llseek = no_llseek, | |
294 | .read = rpc_pipe_read, | |
295 | .write = rpc_pipe_write, | |
296 | .poll = rpc_pipe_poll, | |
297 | .ioctl = rpc_pipe_ioctl, | |
298 | .open = rpc_pipe_open, | |
299 | .release = rpc_pipe_release, | |
300 | }; | |
301 | ||
302 | static int | |
303 | rpc_show_info(struct seq_file *m, void *v) | |
304 | { | |
305 | struct rpc_clnt *clnt = m->private; | |
306 | ||
307 | seq_printf(m, "RPC server: %s\n", clnt->cl_server); | |
308 | seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname, | |
309 | clnt->cl_prog, clnt->cl_vers); | |
310 | seq_printf(m, "address: %u.%u.%u.%u\n", | |
311 | NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr)); | |
312 | seq_printf(m, "protocol: %s\n", | |
313 | clnt->cl_xprt->prot == IPPROTO_UDP ? "udp" : "tcp"); | |
314 | return 0; | |
315 | } | |
316 | ||
317 | static int | |
318 | rpc_info_open(struct inode *inode, struct file *file) | |
319 | { | |
320 | struct rpc_clnt *clnt; | |
321 | int ret = single_open(file, rpc_show_info, NULL); | |
322 | ||
323 | if (!ret) { | |
324 | struct seq_file *m = file->private_data; | |
1b1dcc1b | 325 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
326 | clnt = RPC_I(inode)->private; |
327 | if (clnt) { | |
328 | atomic_inc(&clnt->cl_users); | |
329 | m->private = clnt; | |
330 | } else { | |
331 | single_release(inode, file); | |
332 | ret = -EINVAL; | |
333 | } | |
1b1dcc1b | 334 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
335 | } |
336 | return ret; | |
337 | } | |
338 | ||
339 | static int | |
340 | rpc_info_release(struct inode *inode, struct file *file) | |
341 | { | |
342 | struct seq_file *m = file->private_data; | |
343 | struct rpc_clnt *clnt = (struct rpc_clnt *)m->private; | |
344 | ||
345 | if (clnt) | |
346 | rpc_release_client(clnt); | |
347 | return single_release(inode, file); | |
348 | } | |
349 | ||
350 | static struct file_operations rpc_info_operations = { | |
351 | .owner = THIS_MODULE, | |
352 | .open = rpc_info_open, | |
353 | .read = seq_read, | |
354 | .llseek = seq_lseek, | |
355 | .release = rpc_info_release, | |
356 | }; | |
357 | ||
358 | ||
359 | /* | |
360 | * We have a single directory with 1 node in it. | |
361 | */ | |
362 | enum { | |
363 | RPCAUTH_Root = 1, | |
364 | RPCAUTH_lockd, | |
365 | RPCAUTH_mount, | |
366 | RPCAUTH_nfs, | |
367 | RPCAUTH_portmap, | |
368 | RPCAUTH_statd, | |
369 | RPCAUTH_RootEOF | |
370 | }; | |
371 | ||
372 | /* | |
373 | * Description of fs contents. | |
374 | */ | |
375 | struct rpc_filelist { | |
376 | char *name; | |
377 | struct file_operations *i_fop; | |
378 | int mode; | |
379 | }; | |
380 | ||
381 | static struct rpc_filelist files[] = { | |
382 | [RPCAUTH_lockd] = { | |
383 | .name = "lockd", | |
384 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, | |
385 | }, | |
386 | [RPCAUTH_mount] = { | |
387 | .name = "mount", | |
388 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, | |
389 | }, | |
390 | [RPCAUTH_nfs] = { | |
391 | .name = "nfs", | |
392 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, | |
393 | }, | |
394 | [RPCAUTH_portmap] = { | |
395 | .name = "portmap", | |
396 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, | |
397 | }, | |
398 | [RPCAUTH_statd] = { | |
399 | .name = "statd", | |
400 | .mode = S_IFDIR | S_IRUGO | S_IXUGO, | |
401 | }, | |
402 | }; | |
403 | ||
404 | enum { | |
405 | RPCAUTH_info = 2, | |
406 | RPCAUTH_EOF | |
407 | }; | |
408 | ||
409 | static struct rpc_filelist authfiles[] = { | |
410 | [RPCAUTH_info] = { | |
411 | .name = "info", | |
412 | .i_fop = &rpc_info_operations, | |
413 | .mode = S_IFREG | S_IRUSR, | |
414 | }, | |
415 | }; | |
416 | ||
417 | static int | |
418 | rpc_get_mount(void) | |
419 | { | |
420 | return simple_pin_fs("rpc_pipefs", &rpc_mount, &rpc_mount_count); | |
421 | } | |
422 | ||
423 | static void | |
424 | rpc_put_mount(void) | |
425 | { | |
426 | simple_release_fs(&rpc_mount, &rpc_mount_count); | |
427 | } | |
428 | ||
f134585a TM |
429 | static int |
430 | rpc_lookup_parent(char *path, struct nameidata *nd) | |
431 | { | |
432 | if (path[0] == '\0') | |
433 | return -ENOENT; | |
434 | if (rpc_get_mount()) { | |
435 | printk(KERN_WARNING "%s: %s failed to mount " | |
436 | "pseudofilesystem \n", __FILE__, __FUNCTION__); | |
437 | return -ENODEV; | |
438 | } | |
439 | nd->mnt = mntget(rpc_mount); | |
440 | nd->dentry = dget(rpc_mount->mnt_root); | |
441 | nd->last_type = LAST_ROOT; | |
442 | nd->flags = LOOKUP_PARENT; | |
443 | nd->depth = 0; | |
444 | ||
445 | if (path_walk(path, nd)) { | |
446 | printk(KERN_WARNING "%s: %s failed to find path %s\n", | |
447 | __FILE__, __FUNCTION__, path); | |
448 | rpc_put_mount(); | |
449 | return -ENOENT; | |
450 | } | |
451 | return 0; | |
452 | } | |
453 | ||
454 | static void | |
455 | rpc_release_path(struct nameidata *nd) | |
456 | { | |
457 | path_release(nd); | |
458 | rpc_put_mount(); | |
459 | } | |
460 | ||
1da177e4 LT |
461 | static struct inode * |
462 | rpc_get_inode(struct super_block *sb, int mode) | |
463 | { | |
464 | struct inode *inode = new_inode(sb); | |
465 | if (!inode) | |
466 | return NULL; | |
467 | inode->i_mode = mode; | |
468 | inode->i_uid = inode->i_gid = 0; | |
469 | inode->i_blksize = PAGE_CACHE_SIZE; | |
470 | inode->i_blocks = 0; | |
471 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
472 | switch(mode & S_IFMT) { | |
473 | case S_IFDIR: | |
474 | inode->i_fop = &simple_dir_operations; | |
475 | inode->i_op = &simple_dir_inode_operations; | |
476 | inode->i_nlink++; | |
477 | default: | |
478 | break; | |
479 | } | |
480 | return inode; | |
481 | } | |
482 | ||
483 | /* | |
484 | * FIXME: This probably has races. | |
485 | */ | |
486 | static void | |
487 | rpc_depopulate(struct dentry *parent) | |
488 | { | |
489 | struct inode *dir = parent->d_inode; | |
490 | struct list_head *pos, *next; | |
491 | struct dentry *dentry, *dvec[10]; | |
492 | int n = 0; | |
493 | ||
1b1dcc1b | 494 | mutex_lock(&dir->i_mutex); |
1da177e4 LT |
495 | repeat: |
496 | spin_lock(&dcache_lock); | |
497 | list_for_each_safe(pos, next, &parent->d_subdirs) { | |
5160ee6f | 498 | dentry = list_entry(pos, struct dentry, d_u.d_child); |
1da177e4 LT |
499 | spin_lock(&dentry->d_lock); |
500 | if (!d_unhashed(dentry)) { | |
501 | dget_locked(dentry); | |
502 | __d_drop(dentry); | |
503 | spin_unlock(&dentry->d_lock); | |
504 | dvec[n++] = dentry; | |
505 | if (n == ARRAY_SIZE(dvec)) | |
506 | break; | |
507 | } else | |
508 | spin_unlock(&dentry->d_lock); | |
509 | } | |
510 | spin_unlock(&dcache_lock); | |
511 | if (n) { | |
512 | do { | |
513 | dentry = dvec[--n]; | |
514 | if (dentry->d_inode) { | |
515 | rpc_close_pipes(dentry->d_inode); | |
1da177e4 LT |
516 | simple_unlink(dir, dentry); |
517 | } | |
518 | dput(dentry); | |
519 | } while (n); | |
520 | goto repeat; | |
521 | } | |
1b1dcc1b | 522 | mutex_unlock(&dir->i_mutex); |
1da177e4 LT |
523 | } |
524 | ||
525 | static int | |
526 | rpc_populate(struct dentry *parent, | |
527 | struct rpc_filelist *files, | |
528 | int start, int eof) | |
529 | { | |
530 | struct inode *inode, *dir = parent->d_inode; | |
531 | void *private = RPC_I(dir)->private; | |
532 | struct dentry *dentry; | |
533 | int mode, i; | |
534 | ||
1b1dcc1b | 535 | mutex_lock(&dir->i_mutex); |
1da177e4 LT |
536 | for (i = start; i < eof; i++) { |
537 | dentry = d_alloc_name(parent, files[i].name); | |
538 | if (!dentry) | |
539 | goto out_bad; | |
540 | mode = files[i].mode; | |
541 | inode = rpc_get_inode(dir->i_sb, mode); | |
542 | if (!inode) { | |
543 | dput(dentry); | |
544 | goto out_bad; | |
545 | } | |
546 | inode->i_ino = i; | |
547 | if (files[i].i_fop) | |
548 | inode->i_fop = files[i].i_fop; | |
549 | if (private) | |
550 | rpc_inode_setowner(inode, private); | |
551 | if (S_ISDIR(mode)) | |
552 | dir->i_nlink++; | |
553 | d_add(dentry, inode); | |
554 | } | |
1b1dcc1b | 555 | mutex_unlock(&dir->i_mutex); |
1da177e4 LT |
556 | return 0; |
557 | out_bad: | |
1b1dcc1b | 558 | mutex_unlock(&dir->i_mutex); |
1da177e4 LT |
559 | printk(KERN_WARNING "%s: %s failed to populate directory %s\n", |
560 | __FILE__, __FUNCTION__, parent->d_name.name); | |
561 | return -ENOMEM; | |
562 | } | |
563 | ||
f134585a TM |
564 | static int |
565 | __rpc_mkdir(struct inode *dir, struct dentry *dentry) | |
1da177e4 LT |
566 | { |
567 | struct inode *inode; | |
f134585a TM |
568 | |
569 | inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR); | |
570 | if (!inode) | |
571 | goto out_err; | |
572 | inode->i_ino = iunique(dir->i_sb, 100); | |
573 | d_instantiate(dentry, inode); | |
574 | dir->i_nlink++; | |
575 | inode_dir_notify(dir, DN_CREATE); | |
576 | rpc_get_mount(); | |
577 | return 0; | |
578 | out_err: | |
579 | printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n", | |
580 | __FILE__, __FUNCTION__, dentry->d_name.name); | |
581 | return -ENOMEM; | |
582 | } | |
583 | ||
584 | static int | |
585 | __rpc_rmdir(struct inode *dir, struct dentry *dentry) | |
586 | { | |
1da177e4 LT |
587 | int error; |
588 | ||
f134585a | 589 | shrink_dcache_parent(dentry); |
6070fe6f | 590 | if (dentry->d_inode) |
f134585a | 591 | rpc_close_pipes(dentry->d_inode); |
f134585a TM |
592 | if ((error = simple_rmdir(dir, dentry)) != 0) |
593 | return error; | |
594 | if (!error) { | |
595 | inode_dir_notify(dir, DN_DELETE); | |
596 | d_drop(dentry); | |
597 | rpc_put_mount(); | |
598 | } | |
599 | return 0; | |
600 | } | |
1da177e4 | 601 | |
f134585a TM |
602 | static struct dentry * |
603 | rpc_lookup_negative(char *path, struct nameidata *nd) | |
604 | { | |
605 | struct dentry *dentry; | |
606 | struct inode *dir; | |
607 | int error; | |
278c995c | 608 | |
f134585a TM |
609 | if ((error = rpc_lookup_parent(path, nd)) != 0) |
610 | return ERR_PTR(error); | |
611 | dir = nd->dentry->d_inode; | |
1b1dcc1b | 612 | mutex_lock(&dir->i_mutex); |
49705b77 | 613 | dentry = lookup_hash(nd); |
1da177e4 | 614 | if (IS_ERR(dentry)) |
f134585a | 615 | goto out_err; |
1da177e4 | 616 | if (dentry->d_inode) { |
f134585a | 617 | dput(dentry); |
1da177e4 | 618 | dentry = ERR_PTR(-EEXIST); |
f134585a | 619 | goto out_err; |
1da177e4 | 620 | } |
f134585a TM |
621 | return dentry; |
622 | out_err: | |
1b1dcc1b | 623 | mutex_unlock(&dir->i_mutex); |
f134585a TM |
624 | rpc_release_path(nd); |
625 | return dentry; | |
626 | } | |
278c995c | 627 | |
1da177e4 | 628 | |
f134585a TM |
629 | struct dentry * |
630 | rpc_mkdir(char *path, struct rpc_clnt *rpc_client) | |
631 | { | |
632 | struct nameidata nd; | |
633 | struct dentry *dentry; | |
634 | struct inode *dir; | |
635 | int error; | |
636 | ||
637 | dentry = rpc_lookup_negative(path, &nd); | |
638 | if (IS_ERR(dentry)) | |
639 | return dentry; | |
640 | dir = nd.dentry->d_inode; | |
641 | if ((error = __rpc_mkdir(dir, dentry)) != 0) | |
642 | goto err_dput; | |
643 | RPC_I(dentry->d_inode)->private = rpc_client; | |
1da177e4 LT |
644 | error = rpc_populate(dentry, authfiles, |
645 | RPCAUTH_info, RPCAUTH_EOF); | |
646 | if (error) | |
f134585a TM |
647 | goto err_depopulate; |
648 | out: | |
1b1dcc1b | 649 | mutex_unlock(&dir->i_mutex); |
f134585a | 650 | rpc_release_path(&nd); |
278c995c | 651 | return dentry; |
f134585a TM |
652 | err_depopulate: |
653 | rpc_depopulate(dentry); | |
654 | __rpc_rmdir(dir, dentry); | |
655 | err_dput: | |
656 | dput(dentry); | |
657 | printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n", | |
658 | __FILE__, __FUNCTION__, path, error); | |
659 | dentry = ERR_PTR(error); | |
660 | goto out; | |
1da177e4 LT |
661 | } |
662 | ||
f134585a TM |
663 | int |
664 | rpc_rmdir(char *path) | |
1da177e4 | 665 | { |
f134585a TM |
666 | struct nameidata nd; |
667 | struct dentry *dentry; | |
668 | struct inode *dir; | |
669 | int error; | |
278c995c | 670 | |
f134585a TM |
671 | if ((error = rpc_lookup_parent(path, &nd)) != 0) |
672 | return error; | |
673 | dir = nd.dentry->d_inode; | |
1b1dcc1b | 674 | mutex_lock(&dir->i_mutex); |
49705b77 | 675 | dentry = lookup_hash(&nd); |
f134585a TM |
676 | if (IS_ERR(dentry)) { |
677 | error = PTR_ERR(dentry); | |
678 | goto out_release; | |
278c995c | 679 | } |
f134585a TM |
680 | rpc_depopulate(dentry); |
681 | error = __rpc_rmdir(dir, dentry); | |
682 | dput(dentry); | |
683 | out_release: | |
1b1dcc1b | 684 | mutex_unlock(&dir->i_mutex); |
f134585a TM |
685 | rpc_release_path(&nd); |
686 | return error; | |
1da177e4 LT |
687 | } |
688 | ||
689 | struct dentry * | |
f134585a | 690 | rpc_mkpipe(char *path, void *private, struct rpc_pipe_ops *ops, int flags) |
1da177e4 | 691 | { |
f134585a | 692 | struct nameidata nd; |
1da177e4 | 693 | struct dentry *dentry; |
f134585a | 694 | struct inode *dir, *inode; |
1da177e4 LT |
695 | struct rpc_inode *rpci; |
696 | ||
f134585a | 697 | dentry = rpc_lookup_negative(path, &nd); |
1da177e4 | 698 | if (IS_ERR(dentry)) |
f134585a TM |
699 | return dentry; |
700 | dir = nd.dentry->d_inode; | |
701 | inode = rpc_get_inode(dir->i_sb, S_IFSOCK | S_IRUSR | S_IWUSR); | |
702 | if (!inode) | |
703 | goto err_dput; | |
1da177e4 LT |
704 | inode->i_ino = iunique(dir->i_sb, 100); |
705 | inode->i_fop = &rpc_pipe_fops; | |
f134585a | 706 | d_instantiate(dentry, inode); |
1da177e4 LT |
707 | rpci = RPC_I(inode); |
708 | rpci->private = private; | |
709 | rpci->flags = flags; | |
710 | rpci->ops = ops; | |
711 | inode_dir_notify(dir, DN_CREATE); | |
f134585a | 712 | out: |
1b1dcc1b | 713 | mutex_unlock(&dir->i_mutex); |
f134585a | 714 | rpc_release_path(&nd); |
1da177e4 | 715 | return dentry; |
f134585a | 716 | err_dput: |
1da177e4 | 717 | dput(dentry); |
f134585a TM |
718 | dentry = ERR_PTR(-ENOMEM); |
719 | printk(KERN_WARNING "%s: %s() failed to create pipe %s (errno = %d)\n", | |
720 | __FILE__, __FUNCTION__, path, -ENOMEM); | |
721 | goto out; | |
1da177e4 LT |
722 | } |
723 | ||
f134585a TM |
724 | int |
725 | rpc_unlink(char *path) | |
1da177e4 | 726 | { |
f134585a TM |
727 | struct nameidata nd; |
728 | struct dentry *dentry; | |
729 | struct inode *dir; | |
730 | int error; | |
1da177e4 | 731 | |
f134585a TM |
732 | if ((error = rpc_lookup_parent(path, &nd)) != 0) |
733 | return error; | |
734 | dir = nd.dentry->d_inode; | |
1b1dcc1b | 735 | mutex_lock(&dir->i_mutex); |
49705b77 | 736 | dentry = lookup_hash(&nd); |
f134585a TM |
737 | if (IS_ERR(dentry)) { |
738 | error = PTR_ERR(dentry); | |
739 | goto out_release; | |
740 | } | |
741 | d_drop(dentry); | |
1da177e4 LT |
742 | if (dentry->d_inode) { |
743 | rpc_close_pipes(dentry->d_inode); | |
f134585a | 744 | error = simple_unlink(dir, dentry); |
1da177e4 | 745 | } |
f134585a TM |
746 | dput(dentry); |
747 | inode_dir_notify(dir, DN_DELETE); | |
748 | out_release: | |
1b1dcc1b | 749 | mutex_unlock(&dir->i_mutex); |
f134585a TM |
750 | rpc_release_path(&nd); |
751 | return error; | |
1da177e4 LT |
752 | } |
753 | ||
754 | /* | |
755 | * populate the filesystem | |
756 | */ | |
757 | static struct super_operations s_ops = { | |
758 | .alloc_inode = rpc_alloc_inode, | |
759 | .destroy_inode = rpc_destroy_inode, | |
760 | .statfs = simple_statfs, | |
761 | }; | |
762 | ||
763 | #define RPCAUTH_GSSMAGIC 0x67596969 | |
764 | ||
765 | static int | |
766 | rpc_fill_super(struct super_block *sb, void *data, int silent) | |
767 | { | |
768 | struct inode *inode; | |
769 | struct dentry *root; | |
770 | ||
771 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
772 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
773 | sb->s_magic = RPCAUTH_GSSMAGIC; | |
774 | sb->s_op = &s_ops; | |
775 | sb->s_time_gran = 1; | |
776 | ||
777 | inode = rpc_get_inode(sb, S_IFDIR | 0755); | |
778 | if (!inode) | |
779 | return -ENOMEM; | |
780 | root = d_alloc_root(inode); | |
781 | if (!root) { | |
782 | iput(inode); | |
783 | return -ENOMEM; | |
784 | } | |
785 | if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF)) | |
786 | goto out; | |
787 | sb->s_root = root; | |
788 | return 0; | |
789 | out: | |
790 | d_genocide(root); | |
791 | dput(root); | |
792 | return -ENOMEM; | |
793 | } | |
794 | ||
795 | static struct super_block * | |
796 | rpc_get_sb(struct file_system_type *fs_type, | |
797 | int flags, const char *dev_name, void *data) | |
798 | { | |
799 | return get_sb_single(fs_type, flags, data, rpc_fill_super); | |
800 | } | |
801 | ||
802 | static struct file_system_type rpc_pipe_fs_type = { | |
803 | .owner = THIS_MODULE, | |
804 | .name = "rpc_pipefs", | |
805 | .get_sb = rpc_get_sb, | |
806 | .kill_sb = kill_litter_super, | |
807 | }; | |
808 | ||
809 | static void | |
810 | init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) | |
811 | { | |
812 | struct rpc_inode *rpci = (struct rpc_inode *) foo; | |
813 | ||
814 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == | |
815 | SLAB_CTOR_CONSTRUCTOR) { | |
816 | inode_init_once(&rpci->vfs_inode); | |
817 | rpci->private = NULL; | |
818 | rpci->nreaders = 0; | |
819 | rpci->nwriters = 0; | |
820 | INIT_LIST_HEAD(&rpci->in_upcall); | |
821 | INIT_LIST_HEAD(&rpci->pipe); | |
822 | rpci->pipelen = 0; | |
823 | init_waitqueue_head(&rpci->waitq); | |
824 | INIT_WORK(&rpci->queue_timeout, rpc_timeout_upcall_queue, rpci); | |
825 | rpci->ops = NULL; | |
826 | } | |
827 | } | |
828 | ||
829 | int register_rpc_pipefs(void) | |
830 | { | |
831 | rpc_inode_cachep = kmem_cache_create("rpc_inode_cache", | |
832 | sizeof(struct rpc_inode), | |
833 | 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, | |
834 | init_once, NULL); | |
835 | if (!rpc_inode_cachep) | |
836 | return -ENOMEM; | |
837 | register_filesystem(&rpc_pipe_fs_type); | |
838 | return 0; | |
839 | } | |
840 | ||
841 | void unregister_rpc_pipefs(void) | |
842 | { | |
843 | if (kmem_cache_destroy(rpc_inode_cachep)) | |
844 | printk(KERN_WARNING "RPC: unable to free inode cache\n"); | |
845 | unregister_filesystem(&rpc_pipe_fs_type); | |
846 | } |