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