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