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