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