blob: ce7c0f1dd529211b2f4e5eca8feba0c87428c33f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/inode.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
Ian Kent34ca9592006-03-27 01:14:54 -08006 * Copyright 2005-2006 Ian Kent <raven@themaw.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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>
Ian Kentd7c4a5f2006-03-27 01:14:49 -080017#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/pagemap.h>
19#include <linux/parser.h>
20#include <linux/bitops.h>
Ian Kent104e49f2005-07-27 11:43:45 -070021#include <linux/smp_lock.h>
Jeff Garzike18fa702006-09-24 11:13:19 -040022#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "autofs_i.h"
24#include <linux/module.h>
25
26static void ino_lnkfree(struct autofs_info *ino)
27{
Jesper Juhlf99d49a2005-11-07 01:01:34 -080028 kfree(ino->u.symlink);
29 ino->u.symlink = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
31
32struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
33 struct autofs_sb_info *sbi, mode_t mode)
34{
35 int reinit = 1;
36
37 if (ino == NULL) {
38 reinit = 0;
39 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
40 }
41
42 if (ino == NULL)
43 return NULL;
44
45 ino->flags = 0;
46 ino->mode = mode;
47 ino->inode = NULL;
48 ino->dentry = NULL;
49 ino->size = 0;
50
51 ino->last_used = jiffies;
Ian Kent1aff3c82006-03-27 01:14:46 -080052 atomic_set(&ino->count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 ino->sbi = sbi;
55
56 if (reinit && ino->free)
57 (ino->free)(ino);
58
59 memset(&ino->u, 0, sizeof(ino->u));
60
61 ino->free = NULL;
62
63 if (S_ISLNK(mode))
64 ino->free = ino_lnkfree;
65
66 return ino;
67}
68
69void autofs4_free_ino(struct autofs_info *ino)
70{
Ian Kent1aff3c82006-03-27 01:14:46 -080071 struct autofs_info *p_ino;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (ino->dentry) {
74 ino->dentry->d_fsdata = NULL;
Ian Kent1aff3c82006-03-27 01:14:46 -080075 if (ino->dentry->d_inode) {
76 struct dentry *parent = ino->dentry->d_parent;
77 if (atomic_dec_and_test(&ino->count)) {
78 p_ino = autofs4_dentry_ino(parent);
79 if (p_ino && parent != ino->dentry)
80 atomic_dec(&p_ino->count);
81 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 dput(ino->dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -080083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 ino->dentry = NULL;
85 }
86 if (ino->free)
87 (ino->free)(ino);
88 kfree(ino);
89}
90
Ian Kent104e49f2005-07-27 11:43:45 -070091/*
92 * Deal with the infamous "Busy inodes after umount ..." message.
93 *
94 * Clean up the dentry tree. This happens with autofs if the user
95 * space program goes away due to a SIGKILL, SIGSEGV etc.
96 */
97static void autofs4_force_release(struct autofs_sb_info *sbi)
98{
David Howells6ce31522006-10-11 01:22:15 -070099 struct dentry *this_parent = sbi->sb->s_root;
Ian Kent104e49f2005-07-27 11:43:45 -0700100 struct list_head *next;
101
Ian Kentba8df432006-11-14 02:03:29 -0800102 if (!sbi->sb->s_root)
103 return;
104
Ian Kent104e49f2005-07-27 11:43:45 -0700105 spin_lock(&dcache_lock);
106repeat:
107 next = this_parent->d_subdirs.next;
108resume:
109 while (next != &this_parent->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800110 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
Ian Kent104e49f2005-07-27 11:43:45 -0700111
112 /* Negative dentry - don`t care */
113 if (!simple_positive(dentry)) {
114 next = next->next;
115 continue;
116 }
117
118 if (!list_empty(&dentry->d_subdirs)) {
119 this_parent = dentry;
120 goto repeat;
121 }
122
123 next = next->next;
124 spin_unlock(&dcache_lock);
125
126 DPRINTK("dentry %p %.*s",
127 dentry, (int)dentry->d_name.len, dentry->d_name.name);
128
129 dput(dentry);
130 spin_lock(&dcache_lock);
131 }
132
David Howells6ce31522006-10-11 01:22:15 -0700133 if (this_parent != sbi->sb->s_root) {
Ian Kent104e49f2005-07-27 11:43:45 -0700134 struct dentry *dentry = this_parent;
135
Eric Dumazet5160ee62006-01-08 01:03:32 -0800136 next = this_parent->d_u.d_child.next;
Ian Kent104e49f2005-07-27 11:43:45 -0700137 this_parent = this_parent->d_parent;
138 spin_unlock(&dcache_lock);
139 DPRINTK("parent dentry %p %.*s",
140 dentry, (int)dentry->d_name.len, dentry->d_name.name);
141 dput(dentry);
142 spin_lock(&dcache_lock);
143 goto resume;
144 }
145 spin_unlock(&dcache_lock);
Ian Kent104e49f2005-07-27 11:43:45 -0700146}
147
David Howells6ce31522006-10-11 01:22:15 -0700148void autofs4_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct autofs_sb_info *sbi = autofs4_sbi(sb);
151
Ian Kentba8df432006-11-14 02:03:29 -0800152 /*
153 * In the event of a failure in get_sb_nodev the superblock
154 * info is not present so nothing else has been setup, so
155 * just exit when we are called from deactivate_super.
156 */
157 if (!sbi)
158 return;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 sb->s_fs_info = NULL;
161
162 if ( !sbi->catatonic )
163 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
164
Ian Kent104e49f2005-07-27 11:43:45 -0700165 /* Clean up and release dangling references */
Dave Jones3370c742006-03-27 01:14:57 -0800166 autofs4_force_release(sbi);
Ian Kent104e49f2005-07-27 11:43:45 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 kfree(sbi);
169
170 DPRINTK("shutting down");
David Howells6ce31522006-10-11 01:22:15 -0700171 kill_anon_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800174static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
175{
176 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
177
178 if (!sbi)
179 return 0;
180
181 seq_printf(m, ",fd=%d", sbi->pipefd);
182 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
183 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
184 seq_printf(m, ",minproto=%d", sbi->min_proto);
185 seq_printf(m, ",maxproto=%d", sbi->max_proto);
186
Ian Kent44d53eb2006-03-27 01:14:56 -0800187 if (sbi->type & AUTOFS_TYPE_OFFSET)
Ian Kent34ca9592006-03-27 01:14:54 -0800188 seq_printf(m, ",offset");
Ian Kent44d53eb2006-03-27 01:14:56 -0800189 else if (sbi->type & AUTOFS_TYPE_DIRECT)
Ian Kent34ca9592006-03-27 01:14:54 -0800190 seq_printf(m, ",direct");
191 else
192 seq_printf(m, ",indirect");
193
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800194 return 0;
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800199 .show_options = autofs4_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
Ian Kent34ca9592006-03-27 01:14:54 -0800202enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
203 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205static match_table_t tokens = {
206 {Opt_fd, "fd=%u"},
207 {Opt_uid, "uid=%u"},
208 {Opt_gid, "gid=%u"},
209 {Opt_pgrp, "pgrp=%u"},
210 {Opt_minproto, "minproto=%u"},
211 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800212 {Opt_indirect, "indirect"},
213 {Opt_direct, "direct"},
214 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 {Opt_err, NULL}
216};
217
218static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
Ian Kent34ca9592006-03-27 01:14:54 -0800219 pid_t *pgrp, unsigned int *type,
220 int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 char *p;
223 substring_t args[MAX_OPT_ARGS];
224 int option;
225
226 *uid = current->uid;
227 *gid = current->gid;
228 *pgrp = process_group(current);
229
230 *minproto = AUTOFS_MIN_PROTO_VERSION;
231 *maxproto = AUTOFS_MAX_PROTO_VERSION;
232
233 *pipefd = -1;
234
235 if (!options)
236 return 1;
237
238 while ((p = strsep(&options, ",")) != NULL) {
239 int token;
240 if (!*p)
241 continue;
242
243 token = match_token(p, tokens, args);
244 switch (token) {
245 case Opt_fd:
246 if (match_int(args, pipefd))
247 return 1;
248 break;
249 case Opt_uid:
250 if (match_int(args, &option))
251 return 1;
252 *uid = option;
253 break;
254 case Opt_gid:
255 if (match_int(args, &option))
256 return 1;
257 *gid = option;
258 break;
259 case Opt_pgrp:
260 if (match_int(args, &option))
261 return 1;
262 *pgrp = option;
263 break;
264 case Opt_minproto:
265 if (match_int(args, &option))
266 return 1;
267 *minproto = option;
268 break;
269 case Opt_maxproto:
270 if (match_int(args, &option))
271 return 1;
272 *maxproto = option;
273 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800274 case Opt_indirect:
Ian Kent44d53eb2006-03-27 01:14:56 -0800275 *type = AUTOFS_TYPE_INDIRECT;
Ian Kent34ca9592006-03-27 01:14:54 -0800276 break;
277 case Opt_direct:
Ian Kent44d53eb2006-03-27 01:14:56 -0800278 *type = AUTOFS_TYPE_DIRECT;
Ian Kent34ca9592006-03-27 01:14:54 -0800279 break;
280 case Opt_offset:
Ian Kent44d53eb2006-03-27 01:14:56 -0800281 *type = AUTOFS_TYPE_DIRECT | AUTOFS_TYPE_OFFSET;
Ian Kent34ca9592006-03-27 01:14:54 -0800282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 default:
284 return 1;
285 }
286 }
287 return (*pipefd < 0);
288}
289
290static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
291{
292 struct autofs_info *ino;
293
294 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
295 if (!ino)
296 return NULL;
297
298 return ino;
299}
300
Ian Kent34ca9592006-03-27 01:14:54 -0800301static struct dentry_operations autofs4_sb_dentry_operations = {
302 .d_release = autofs4_dentry_release,
303};
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305int autofs4_fill_super(struct super_block *s, void *data, int silent)
306{
307 struct inode * root_inode;
308 struct dentry * root;
309 struct file * pipe;
310 int pipefd;
311 struct autofs_sb_info *sbi;
312 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
315 if ( !sbi )
316 goto fail_unlock;
317 DPRINTK("starting up, sbi = %p",sbi);
318
319 memset(sbi, 0, sizeof(*sbi));
320
321 s->s_fs_info = sbi;
322 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800323 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800324 sbi->pipe = NULL;
325 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sbi->exp_timeout = 0;
327 sbi->oz_pgrp = process_group(current);
328 sbi->sb = s;
329 sbi->version = 0;
330 sbi->sub_version = 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800331 sbi->type = 0;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800332 sbi->min_proto = 0;
333 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800334 mutex_init(&sbi->wq_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700335 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 sbi->queues = NULL;
337 s->s_blocksize = 1024;
338 s->s_blocksize_bits = 10;
339 s->s_magic = AUTOFS_SUPER_MAGIC;
340 s->s_op = &autofs4_sops;
341 s->s_time_gran = 1;
342
343 /*
344 * Get the root inode and dentry, but defer checking for errors.
345 */
346 ino = autofs4_mkroot(sbi);
347 if (!ino)
348 goto fail_free;
349 root_inode = autofs4_get_inode(s, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (!root_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800351 goto fail_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 root = d_alloc_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!root)
355 goto fail_iput;
Ian Kent34ca9592006-03-27 01:14:54 -0800356 pipe = NULL;
357
358 root->d_op = &autofs4_sb_dentry_operations;
359 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* Can this call block? */
362 if (parse_options(data, &pipefd,
363 &root_inode->i_uid, &root_inode->i_gid,
Ian Kent34ca9592006-03-27 01:14:54 -0800364 &sbi->oz_pgrp, &sbi->type,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800365 &sbi->min_proto, &sbi->max_proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 printk("autofs: called with bogus options\n");
367 goto fail_dput;
368 }
369
Ian Kent34ca9592006-03-27 01:14:54 -0800370 root_inode->i_fop = &autofs4_root_operations;
Ian Kent44d53eb2006-03-27 01:14:56 -0800371 root_inode->i_op = sbi->type & AUTOFS_TYPE_DIRECT ?
Ian Kent34ca9592006-03-27 01:14:54 -0800372 &autofs4_direct_root_inode_operations :
373 &autofs4_indirect_root_inode_operations;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800376 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
377 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 printk("autofs: kernel does not match daemon version "
379 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800380 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
382 goto fail_dput;
383 }
384
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800385 /* Establish highest kernel protocol version */
386 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
387 sbi->version = AUTOFS_MAX_PROTO_VERSION;
388 else
389 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
391
392 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
393 pipe = fget(pipefd);
394
395 if ( !pipe ) {
396 printk("autofs: could not open pipe file descriptor\n");
397 goto fail_dput;
398 }
399 if ( !pipe->f_op || !pipe->f_op->write )
400 goto fail_fput;
401 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800402 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800403 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * Success! Install the root dentry now to indicate completion.
407 */
408 s->s_root = root;
409 return 0;
410
411 /*
412 * Failure ... clean up.
413 */
414fail_fput:
415 printk("autofs: pipe file descriptor does not contain proper ops\n");
416 fput(pipe);
417 /* fall through */
418fail_dput:
419 dput(root);
420 goto fail_free;
421fail_iput:
422 printk("autofs: get root dentry failed\n");
423 iput(root_inode);
Ian Kent34ca9592006-03-27 01:14:54 -0800424fail_ino:
425 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426fail_free:
427 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800428 s->s_fs_info = NULL;
429 kill_anon_super(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430fail_unlock:
431 return -EINVAL;
432}
433
434struct inode *autofs4_get_inode(struct super_block *sb,
435 struct autofs_info *inf)
436{
437 struct inode *inode = new_inode(sb);
438
439 if (inode == NULL)
440 return NULL;
441
442 inf->inode = inode;
443 inode->i_mode = inf->mode;
444 if (sb->s_root) {
445 inode->i_uid = sb->s_root->d_inode->i_uid;
446 inode->i_gid = sb->s_root->d_inode->i_gid;
447 } else {
448 inode->i_uid = 0;
449 inode->i_gid = 0;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 inode->i_blocks = 0;
452 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
453
454 if (S_ISDIR(inf->mode)) {
455 inode->i_nlink = 2;
456 inode->i_op = &autofs4_dir_inode_operations;
457 inode->i_fop = &autofs4_dir_operations;
458 } else if (S_ISLNK(inf->mode)) {
459 inode->i_size = inf->size;
460 inode->i_op = &autofs4_symlink_inode_operations;
461 }
462
463 return inode;
464}