Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* -*- c -*- --------------------------------------------------------------- * |
2 | * | |
3 | * linux/fs/autofs/inode.c | |
4 | * | |
5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | |
34ca959c | 6 | * Copyright 2005-2006 Ian Kent <raven@themaw.net> |
1da177e4 LT |
7 | * |
8 | * This file is part of the Linux kernel and is made available under | |
9 | * the terms of the GNU General Public License, version 2, or at your | |
10 | * option, any later version, incorporated herein by reference. | |
11 | * | |
12 | * ------------------------------------------------------------------------- */ | |
13 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/file.h> | |
d7c4a5f1 | 17 | #include <linux/seq_file.h> |
1da177e4 LT |
18 | #include <linux/pagemap.h> |
19 | #include <linux/parser.h> | |
20 | #include <linux/bitops.h> | |
e18fa700 | 21 | #include <linux/magic.h> |
1da177e4 LT |
22 | #include "autofs_i.h" |
23 | #include <linux/module.h> | |
24 | ||
25 | static void ino_lnkfree(struct autofs_info *ino) | |
26 | { | |
25767378 IK |
27 | if (ino->u.symlink) { |
28 | kfree(ino->u.symlink); | |
29 | ino->u.symlink = NULL; | |
30 | } | |
1da177e4 LT |
31 | } |
32 | ||
33 | struct autofs_info *autofs4_init_ino(struct autofs_info *ino, | |
34 | struct autofs_sb_info *sbi, mode_t mode) | |
35 | { | |
36 | int reinit = 1; | |
37 | ||
38 | if (ino == NULL) { | |
39 | reinit = 0; | |
40 | ino = kmalloc(sizeof(*ino), GFP_KERNEL); | |
41 | } | |
42 | ||
43 | if (ino == NULL) | |
44 | return NULL; | |
45 | ||
25767378 IK |
46 | if (!reinit) { |
47 | ino->flags = 0; | |
48 | ino->inode = NULL; | |
49 | ino->dentry = NULL; | |
50 | ino->size = 0; | |
51 | INIT_LIST_HEAD(&ino->active); | |
4f8427d1 | 52 | ino->active_count = 0; |
25767378 IK |
53 | INIT_LIST_HEAD(&ino->expiring); |
54 | atomic_set(&ino->count, 0); | |
55 | } | |
f50b6f86 | 56 | |
c0f54d3e IK |
57 | ino->uid = 0; |
58 | ino->gid = 0; | |
25767378 | 59 | ino->mode = mode; |
1da177e4 LT |
60 | ino->last_used = jiffies; |
61 | ||
62 | ino->sbi = sbi; | |
63 | ||
64 | if (reinit && ino->free) | |
65 | (ino->free)(ino); | |
66 | ||
67 | memset(&ino->u, 0, sizeof(ino->u)); | |
68 | ||
69 | ino->free = NULL; | |
70 | ||
71 | if (S_ISLNK(mode)) | |
72 | ino->free = ino_lnkfree; | |
73 | ||
74 | return ino; | |
75 | } | |
76 | ||
77 | void autofs4_free_ino(struct autofs_info *ino) | |
78 | { | |
1aff3c8b IK |
79 | struct autofs_info *p_ino; |
80 | ||
1da177e4 LT |
81 | if (ino->dentry) { |
82 | ino->dentry->d_fsdata = NULL; | |
1aff3c8b IK |
83 | if (ino->dentry->d_inode) { |
84 | struct dentry *parent = ino->dentry->d_parent; | |
85 | if (atomic_dec_and_test(&ino->count)) { | |
86 | p_ino = autofs4_dentry_ino(parent); | |
87 | if (p_ino && parent != ino->dentry) | |
88 | atomic_dec(&p_ino->count); | |
89 | } | |
1da177e4 | 90 | dput(ino->dentry); |
1aff3c8b | 91 | } |
1da177e4 LT |
92 | ino->dentry = NULL; |
93 | } | |
94 | if (ino->free) | |
95 | (ino->free)(ino); | |
96 | kfree(ino); | |
97 | } | |
98 | ||
104e49fc IK |
99 | /* |
100 | * Deal with the infamous "Busy inodes after umount ..." message. | |
101 | * | |
102 | * Clean up the dentry tree. This happens with autofs if the user | |
103 | * space program goes away due to a SIGKILL, SIGSEGV etc. | |
104 | */ | |
105 | static void autofs4_force_release(struct autofs_sb_info *sbi) | |
106 | { | |
6ce31523 | 107 | struct dentry *this_parent = sbi->sb->s_root; |
104e49fc IK |
108 | struct list_head *next; |
109 | ||
ba8df43c IK |
110 | if (!sbi->sb->s_root) |
111 | return; | |
112 | ||
104e49fc IK |
113 | spin_lock(&dcache_lock); |
114 | repeat: | |
115 | next = this_parent->d_subdirs.next; | |
116 | resume: | |
117 | while (next != &this_parent->d_subdirs) { | |
5160ee6f | 118 | struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child); |
104e49fc IK |
119 | |
120 | /* Negative dentry - don`t care */ | |
121 | if (!simple_positive(dentry)) { | |
122 | next = next->next; | |
123 | continue; | |
124 | } | |
125 | ||
126 | if (!list_empty(&dentry->d_subdirs)) { | |
127 | this_parent = dentry; | |
128 | goto repeat; | |
129 | } | |
130 | ||
131 | next = next->next; | |
132 | spin_unlock(&dcache_lock); | |
133 | ||
134 | DPRINTK("dentry %p %.*s", | |
135 | dentry, (int)dentry->d_name.len, dentry->d_name.name); | |
136 | ||
137 | dput(dentry); | |
138 | spin_lock(&dcache_lock); | |
139 | } | |
140 | ||
6ce31523 | 141 | if (this_parent != sbi->sb->s_root) { |
104e49fc IK |
142 | struct dentry *dentry = this_parent; |
143 | ||
5160ee6f | 144 | next = this_parent->d_u.d_child.next; |
104e49fc IK |
145 | this_parent = this_parent->d_parent; |
146 | spin_unlock(&dcache_lock); | |
147 | DPRINTK("parent dentry %p %.*s", | |
148 | dentry, (int)dentry->d_name.len, dentry->d_name.name); | |
149 | dput(dentry); | |
150 | spin_lock(&dcache_lock); | |
151 | goto resume; | |
152 | } | |
153 | spin_unlock(&dcache_lock); | |
104e49fc IK |
154 | } |
155 | ||
6ce31523 | 156 | void autofs4_kill_sb(struct super_block *sb) |
1da177e4 LT |
157 | { |
158 | struct autofs_sb_info *sbi = autofs4_sbi(sb); | |
159 | ||
ba8df43c IK |
160 | /* |
161 | * In the event of a failure in get_sb_nodev the superblock | |
162 | * info is not present so nothing else has been setup, so | |
c949d4eb JK |
163 | * just call kill_anon_super when we are called from |
164 | * deactivate_super. | |
ba8df43c IK |
165 | */ |
166 | if (!sbi) | |
c949d4eb | 167 | goto out_kill_sb; |
ba8df43c | 168 | |
5a11d4d0 IK |
169 | /* Free wait queues, close pipe */ |
170 | autofs4_catatonic_mode(sbi); | |
1da177e4 | 171 | |
104e49fc | 172 | /* Clean up and release dangling references */ |
3370c74b | 173 | autofs4_force_release(sbi); |
104e49fc | 174 | |
f50b6f86 | 175 | sb->s_fs_info = NULL; |
1da177e4 LT |
176 | kfree(sbi); |
177 | ||
c949d4eb | 178 | out_kill_sb: |
1da177e4 | 179 | DPRINTK("shutting down"); |
6ce31523 | 180 | kill_anon_super(sb); |
1da177e4 LT |
181 | } |
182 | ||
d7c4a5f1 IK |
183 | static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt) |
184 | { | |
185 | struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb); | |
aef97cb9 | 186 | struct inode *root_inode = mnt->mnt_sb->s_root->d_inode; |
d7c4a5f1 IK |
187 | |
188 | if (!sbi) | |
189 | return 0; | |
190 | ||
191 | seq_printf(m, ",fd=%d", sbi->pipefd); | |
aef97cb9 MS |
192 | if (root_inode->i_uid != 0) |
193 | seq_printf(m, ",uid=%u", root_inode->i_uid); | |
194 | if (root_inode->i_gid != 0) | |
195 | seq_printf(m, ",gid=%u", root_inode->i_gid); | |
d7c4a5f1 IK |
196 | seq_printf(m, ",pgrp=%d", sbi->oz_pgrp); |
197 | seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ); | |
198 | seq_printf(m, ",minproto=%d", sbi->min_proto); | |
199 | seq_printf(m, ",maxproto=%d", sbi->max_proto); | |
200 | ||
a92daf6b | 201 | if (autofs_type_offset(sbi->type)) |
34ca959c | 202 | seq_printf(m, ",offset"); |
a92daf6b | 203 | else if (autofs_type_direct(sbi->type)) |
34ca959c IK |
204 | seq_printf(m, ",direct"); |
205 | else | |
206 | seq_printf(m, ",indirect"); | |
207 | ||
d7c4a5f1 IK |
208 | return 0; |
209 | } | |
210 | ||
ee9b6d61 | 211 | static const struct super_operations autofs4_sops = { |
1da177e4 | 212 | .statfs = simple_statfs, |
d7c4a5f1 | 213 | .show_options = autofs4_show_options, |
1da177e4 LT |
214 | }; |
215 | ||
34ca959c IK |
216 | enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto, |
217 | Opt_indirect, Opt_direct, Opt_offset}; | |
1da177e4 | 218 | |
a447c093 | 219 | static const match_table_t tokens = { |
1da177e4 LT |
220 | {Opt_fd, "fd=%u"}, |
221 | {Opt_uid, "uid=%u"}, | |
222 | {Opt_gid, "gid=%u"}, | |
223 | {Opt_pgrp, "pgrp=%u"}, | |
224 | {Opt_minproto, "minproto=%u"}, | |
225 | {Opt_maxproto, "maxproto=%u"}, | |
34ca959c IK |
226 | {Opt_indirect, "indirect"}, |
227 | {Opt_direct, "direct"}, | |
228 | {Opt_offset, "offset"}, | |
1da177e4 LT |
229 | {Opt_err, NULL} |
230 | }; | |
231 | ||
232 | static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid, | |
d78e53c8 | 233 | pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto) |
1da177e4 LT |
234 | { |
235 | char *p; | |
236 | substring_t args[MAX_OPT_ARGS]; | |
237 | int option; | |
238 | ||
0eb790e3 DH |
239 | *uid = current_uid(); |
240 | *gid = current_gid(); | |
a47afb0f | 241 | *pgrp = task_pgrp_nr(current); |
1da177e4 LT |
242 | |
243 | *minproto = AUTOFS_MIN_PROTO_VERSION; | |
244 | *maxproto = AUTOFS_MAX_PROTO_VERSION; | |
245 | ||
246 | *pipefd = -1; | |
247 | ||
248 | if (!options) | |
249 | return 1; | |
250 | ||
251 | while ((p = strsep(&options, ",")) != NULL) { | |
252 | int token; | |
253 | if (!*p) | |
254 | continue; | |
255 | ||
256 | token = match_token(p, tokens, args); | |
257 | switch (token) { | |
258 | case Opt_fd: | |
259 | if (match_int(args, pipefd)) | |
260 | return 1; | |
261 | break; | |
262 | case Opt_uid: | |
263 | if (match_int(args, &option)) | |
264 | return 1; | |
265 | *uid = option; | |
266 | break; | |
267 | case Opt_gid: | |
268 | if (match_int(args, &option)) | |
269 | return 1; | |
270 | *gid = option; | |
271 | break; | |
272 | case Opt_pgrp: | |
273 | if (match_int(args, &option)) | |
274 | return 1; | |
275 | *pgrp = option; | |
276 | break; | |
277 | case Opt_minproto: | |
278 | if (match_int(args, &option)) | |
279 | return 1; | |
280 | *minproto = option; | |
281 | break; | |
282 | case Opt_maxproto: | |
283 | if (match_int(args, &option)) | |
284 | return 1; | |
285 | *maxproto = option; | |
286 | break; | |
34ca959c | 287 | case Opt_indirect: |
a92daf6b | 288 | set_autofs_type_indirect(type); |
34ca959c IK |
289 | break; |
290 | case Opt_direct: | |
a92daf6b | 291 | set_autofs_type_direct(type); |
34ca959c IK |
292 | break; |
293 | case Opt_offset: | |
a92daf6b | 294 | set_autofs_type_offset(type); |
34ca959c | 295 | break; |
1da177e4 LT |
296 | default: |
297 | return 1; | |
298 | } | |
299 | } | |
300 | return (*pipefd < 0); | |
301 | } | |
302 | ||
303 | static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi) | |
304 | { | |
305 | struct autofs_info *ino; | |
306 | ||
307 | ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755); | |
308 | if (!ino) | |
309 | return NULL; | |
310 | ||
311 | return ino; | |
312 | } | |
313 | ||
08f11513 | 314 | static const struct dentry_operations autofs4_sb_dentry_operations = { |
34ca959c IK |
315 | .d_release = autofs4_dentry_release, |
316 | }; | |
317 | ||
1da177e4 LT |
318 | int autofs4_fill_super(struct super_block *s, void *data, int silent) |
319 | { | |
320 | struct inode * root_inode; | |
321 | struct dentry * root; | |
322 | struct file * pipe; | |
323 | int pipefd; | |
324 | struct autofs_sb_info *sbi; | |
325 | struct autofs_info *ino; | |
1da177e4 | 326 | |
b63d50c4 | 327 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); |
d78e53c8 | 328 | if (!sbi) |
1da177e4 LT |
329 | goto fail_unlock; |
330 | DPRINTK("starting up, sbi = %p",sbi); | |
331 | ||
1da177e4 LT |
332 | s->s_fs_info = sbi; |
333 | sbi->magic = AUTOFS_SBI_MAGIC; | |
d7c4a5f1 | 334 | sbi->pipefd = -1; |
ba8df43c IK |
335 | sbi->pipe = NULL; |
336 | sbi->catatonic = 1; | |
1da177e4 | 337 | sbi->exp_timeout = 0; |
a47afb0f | 338 | sbi->oz_pgrp = task_pgrp_nr(current); |
1da177e4 LT |
339 | sbi->sb = s; |
340 | sbi->version = 0; | |
341 | sbi->sub_version = 0; | |
a92daf6b | 342 | set_autofs_type_indirect(&sbi->type); |
d7c4a5f1 IK |
343 | sbi->min_proto = 0; |
344 | sbi->max_proto = 0; | |
1d5599e3 | 345 | mutex_init(&sbi->wq_mutex); |
3a9720ce | 346 | spin_lock_init(&sbi->fs_lock); |
1da177e4 | 347 | sbi->queues = NULL; |
5f6f4f28 | 348 | spin_lock_init(&sbi->lookup_lock); |
25767378 | 349 | INIT_LIST_HEAD(&sbi->active_list); |
5f6f4f28 | 350 | INIT_LIST_HEAD(&sbi->expiring_list); |
1da177e4 LT |
351 | s->s_blocksize = 1024; |
352 | s->s_blocksize_bits = 10; | |
353 | s->s_magic = AUTOFS_SUPER_MAGIC; | |
354 | s->s_op = &autofs4_sops; | |
355 | s->s_time_gran = 1; | |
356 | ||
357 | /* | |
358 | * Get the root inode and dentry, but defer checking for errors. | |
359 | */ | |
360 | ino = autofs4_mkroot(sbi); | |
361 | if (!ino) | |
362 | goto fail_free; | |
363 | root_inode = autofs4_get_inode(s, ino); | |
1da177e4 | 364 | if (!root_inode) |
34ca959c | 365 | goto fail_ino; |
1da177e4 | 366 | |
1da177e4 | 367 | root = d_alloc_root(root_inode); |
1da177e4 LT |
368 | if (!root) |
369 | goto fail_iput; | |
34ca959c IK |
370 | pipe = NULL; |
371 | ||
372 | root->d_op = &autofs4_sb_dentry_operations; | |
373 | root->d_fsdata = ino; | |
1da177e4 LT |
374 | |
375 | /* Can this call block? */ | |
d78e53c8 SB |
376 | if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid, |
377 | &sbi->oz_pgrp, &sbi->type, &sbi->min_proto, | |
378 | &sbi->max_proto)) { | |
1da177e4 LT |
379 | printk("autofs: called with bogus options\n"); |
380 | goto fail_dput; | |
381 | } | |
382 | ||
34ca959c | 383 | root_inode->i_fop = &autofs4_root_operations; |
a92daf6b | 384 | root_inode->i_op = autofs_type_trigger(sbi->type) ? |
34ca959c IK |
385 | &autofs4_direct_root_inode_operations : |
386 | &autofs4_indirect_root_inode_operations; | |
387 | ||
1da177e4 | 388 | /* Couldn't this be tested earlier? */ |
d7c4a5f1 IK |
389 | if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION || |
390 | sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) { | |
1da177e4 LT |
391 | printk("autofs: kernel does not match daemon version " |
392 | "daemon (%d, %d) kernel (%d, %d)\n", | |
d7c4a5f1 | 393 | sbi->min_proto, sbi->max_proto, |
1da177e4 LT |
394 | AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION); |
395 | goto fail_dput; | |
396 | } | |
397 | ||
d7c4a5f1 IK |
398 | /* Establish highest kernel protocol version */ |
399 | if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION) | |
400 | sbi->version = AUTOFS_MAX_PROTO_VERSION; | |
401 | else | |
402 | sbi->version = sbi->max_proto; | |
1da177e4 LT |
403 | sbi->sub_version = AUTOFS_PROTO_SUBVERSION; |
404 | ||
405 | DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp); | |
406 | pipe = fget(pipefd); | |
407 | ||
d78e53c8 | 408 | if (!pipe) { |
1da177e4 LT |
409 | printk("autofs: could not open pipe file descriptor\n"); |
410 | goto fail_dput; | |
411 | } | |
d78e53c8 | 412 | if (!pipe->f_op || !pipe->f_op->write) |
1da177e4 LT |
413 | goto fail_fput; |
414 | sbi->pipe = pipe; | |
d7c4a5f1 | 415 | sbi->pipefd = pipefd; |
ba8df43c | 416 | sbi->catatonic = 0; |
1da177e4 LT |
417 | |
418 | /* | |
419 | * Success! Install the root dentry now to indicate completion. | |
420 | */ | |
421 | s->s_root = root; | |
422 | return 0; | |
423 | ||
424 | /* | |
425 | * Failure ... clean up. | |
426 | */ | |
427 | fail_fput: | |
428 | printk("autofs: pipe file descriptor does not contain proper ops\n"); | |
429 | fput(pipe); | |
430 | /* fall through */ | |
431 | fail_dput: | |
432 | dput(root); | |
433 | goto fail_free; | |
434 | fail_iput: | |
435 | printk("autofs: get root dentry failed\n"); | |
436 | iput(root_inode); | |
34ca959c IK |
437 | fail_ino: |
438 | kfree(ino); | |
1da177e4 LT |
439 | fail_free: |
440 | kfree(sbi); | |
ba8df43c | 441 | s->s_fs_info = NULL; |
1da177e4 LT |
442 | fail_unlock: |
443 | return -EINVAL; | |
444 | } | |
445 | ||
446 | struct inode *autofs4_get_inode(struct super_block *sb, | |
447 | struct autofs_info *inf) | |
448 | { | |
449 | struct inode *inode = new_inode(sb); | |
450 | ||
451 | if (inode == NULL) | |
452 | return NULL; | |
453 | ||
454 | inf->inode = inode; | |
455 | inode->i_mode = inf->mode; | |
456 | if (sb->s_root) { | |
457 | inode->i_uid = sb->s_root->d_inode->i_uid; | |
458 | inode->i_gid = sb->s_root->d_inode->i_gid; | |
1da177e4 | 459 | } |
1da177e4 LT |
460 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
461 | ||
462 | if (S_ISDIR(inf->mode)) { | |
463 | inode->i_nlink = 2; | |
464 | inode->i_op = &autofs4_dir_inode_operations; | |
465 | inode->i_fop = &autofs4_dir_operations; | |
466 | } else if (S_ISLNK(inf->mode)) { | |
467 | inode->i_size = inf->size; | |
468 | inode->i_op = &autofs4_symlink_inode_operations; | |
469 | } | |
470 | ||
471 | return inode; | |
472 | } |