blob: 4eddee4e76fc5bee26d0e3270bae7958ef41536e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "autofs_i.h"
23#include <linux/module.h>
24
25static void ino_lnkfree(struct autofs_info *ino)
26{
Jesper Juhlf99d49a2005-11-07 01:01:34 -080027 kfree(ino->u.symlink);
28 ino->u.symlink = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029}
30
31struct 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
50 ino->last_used = jiffies;
Ian Kent1aff3c82006-03-27 01:14:46 -080051 atomic_set(&ino->count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 ino->sbi = sbi;
54
55 if (reinit && ino->free)
56 (ino->free)(ino);
57
58 memset(&ino->u, 0, sizeof(ino->u));
59
60 ino->free = NULL;
61
62 if (S_ISLNK(mode))
63 ino->free = ino_lnkfree;
64
65 return ino;
66}
67
68void autofs4_free_ino(struct autofs_info *ino)
69{
Ian Kent1aff3c82006-03-27 01:14:46 -080070 struct autofs_info *p_ino;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (ino->dentry) {
73 ino->dentry->d_fsdata = NULL;
Ian Kent1aff3c82006-03-27 01:14:46 -080074 if (ino->dentry->d_inode) {
75 struct dentry *parent = ino->dentry->d_parent;
76 if (atomic_dec_and_test(&ino->count)) {
77 p_ino = autofs4_dentry_ino(parent);
78 if (p_ino && parent != ino->dentry)
79 atomic_dec(&p_ino->count);
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 dput(ino->dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -080082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 ino->dentry = NULL;
84 }
85 if (ino->free)
86 (ino->free)(ino);
87 kfree(ino);
88}
89
Ian Kent104e49f2005-07-27 11:43:45 -070090/*
91 * Deal with the infamous "Busy inodes after umount ..." message.
92 *
93 * Clean up the dentry tree. This happens with autofs if the user
94 * space program goes away due to a SIGKILL, SIGSEGV etc.
95 */
96static void autofs4_force_release(struct autofs_sb_info *sbi)
97{
98 struct dentry *this_parent = sbi->root;
99 struct list_head *next;
100
101 spin_lock(&dcache_lock);
102repeat:
103 next = this_parent->d_subdirs.next;
104resume:
105 while (next != &this_parent->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800106 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
Ian Kent104e49f2005-07-27 11:43:45 -0700107
108 /* Negative dentry - don`t care */
109 if (!simple_positive(dentry)) {
110 next = next->next;
111 continue;
112 }
113
114 if (!list_empty(&dentry->d_subdirs)) {
115 this_parent = dentry;
116 goto repeat;
117 }
118
119 next = next->next;
120 spin_unlock(&dcache_lock);
121
122 DPRINTK("dentry %p %.*s",
123 dentry, (int)dentry->d_name.len, dentry->d_name.name);
124
125 dput(dentry);
126 spin_lock(&dcache_lock);
127 }
128
129 if (this_parent != sbi->root) {
130 struct dentry *dentry = this_parent;
131
Eric Dumazet5160ee62006-01-08 01:03:32 -0800132 next = this_parent->d_u.d_child.next;
Ian Kent104e49f2005-07-27 11:43:45 -0700133 this_parent = this_parent->d_parent;
134 spin_unlock(&dcache_lock);
135 DPRINTK("parent dentry %p %.*s",
136 dentry, (int)dentry->d_name.len, dentry->d_name.name);
137 dput(dentry);
138 spin_lock(&dcache_lock);
139 goto resume;
140 }
141 spin_unlock(&dcache_lock);
142
143 dput(sbi->root);
144 sbi->root = NULL;
145 shrink_dcache_sb(sbi->sb);
146
147 return;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static void autofs4_put_super(struct super_block *sb)
151{
152 struct autofs_sb_info *sbi = autofs4_sbi(sb);
153
154 sb->s_fs_info = NULL;
155
156 if ( !sbi->catatonic )
157 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
158
Ian Kent104e49f2005-07-27 11:43:45 -0700159 /* Clean up and release dangling references */
Dave Jones3370c742006-03-27 01:14:57 -0800160 autofs4_force_release(sbi);
Ian Kent104e49f2005-07-27 11:43:45 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 kfree(sbi);
163
164 DPRINTK("shutting down");
165}
166
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800167static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
168{
169 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
170
171 if (!sbi)
172 return 0;
173
174 seq_printf(m, ",fd=%d", sbi->pipefd);
175 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
176 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
177 seq_printf(m, ",minproto=%d", sbi->min_proto);
178 seq_printf(m, ",maxproto=%d", sbi->max_proto);
179
Ian Kent44d53eb2006-03-27 01:14:56 -0800180 if (sbi->type & AUTOFS_TYPE_OFFSET)
Ian Kent34ca9592006-03-27 01:14:54 -0800181 seq_printf(m, ",offset");
Ian Kent44d53eb2006-03-27 01:14:56 -0800182 else if (sbi->type & AUTOFS_TYPE_DIRECT)
Ian Kent34ca9592006-03-27 01:14:54 -0800183 seq_printf(m, ",direct");
184 else
185 seq_printf(m, ",indirect");
186
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800187 return 0;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static struct super_operations autofs4_sops = {
191 .put_super = autofs4_put_super,
192 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800193 .show_options = autofs4_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Ian Kent34ca9592006-03-27 01:14:54 -0800196enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
197 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199static match_table_t tokens = {
200 {Opt_fd, "fd=%u"},
201 {Opt_uid, "uid=%u"},
202 {Opt_gid, "gid=%u"},
203 {Opt_pgrp, "pgrp=%u"},
204 {Opt_minproto, "minproto=%u"},
205 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800206 {Opt_indirect, "indirect"},
207 {Opt_direct, "direct"},
208 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 {Opt_err, NULL}
210};
211
212static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
Ian Kent34ca9592006-03-27 01:14:54 -0800213 pid_t *pgrp, unsigned int *type,
214 int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 char *p;
217 substring_t args[MAX_OPT_ARGS];
218 int option;
219
220 *uid = current->uid;
221 *gid = current->gid;
222 *pgrp = process_group(current);
223
224 *minproto = AUTOFS_MIN_PROTO_VERSION;
225 *maxproto = AUTOFS_MAX_PROTO_VERSION;
226
227 *pipefd = -1;
228
229 if (!options)
230 return 1;
231
232 while ((p = strsep(&options, ",")) != NULL) {
233 int token;
234 if (!*p)
235 continue;
236
237 token = match_token(p, tokens, args);
238 switch (token) {
239 case Opt_fd:
240 if (match_int(args, pipefd))
241 return 1;
242 break;
243 case Opt_uid:
244 if (match_int(args, &option))
245 return 1;
246 *uid = option;
247 break;
248 case Opt_gid:
249 if (match_int(args, &option))
250 return 1;
251 *gid = option;
252 break;
253 case Opt_pgrp:
254 if (match_int(args, &option))
255 return 1;
256 *pgrp = option;
257 break;
258 case Opt_minproto:
259 if (match_int(args, &option))
260 return 1;
261 *minproto = option;
262 break;
263 case Opt_maxproto:
264 if (match_int(args, &option))
265 return 1;
266 *maxproto = option;
267 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800268 case Opt_indirect:
Ian Kent44d53eb2006-03-27 01:14:56 -0800269 *type = AUTOFS_TYPE_INDIRECT;
Ian Kent34ca9592006-03-27 01:14:54 -0800270 break;
271 case Opt_direct:
Ian Kent44d53eb2006-03-27 01:14:56 -0800272 *type = AUTOFS_TYPE_DIRECT;
Ian Kent34ca9592006-03-27 01:14:54 -0800273 break;
274 case Opt_offset:
Ian Kent44d53eb2006-03-27 01:14:56 -0800275 *type = AUTOFS_TYPE_DIRECT | AUTOFS_TYPE_OFFSET;
Ian Kent34ca9592006-03-27 01:14:54 -0800276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 default:
278 return 1;
279 }
280 }
281 return (*pipefd < 0);
282}
283
284static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
285{
286 struct autofs_info *ino;
287
288 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
289 if (!ino)
290 return NULL;
291
292 return ino;
293}
294
Ian Kent34ca9592006-03-27 01:14:54 -0800295void autofs4_dentry_release(struct dentry *);
296static struct dentry_operations autofs4_sb_dentry_operations = {
297 .d_release = autofs4_dentry_release,
298};
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300int autofs4_fill_super(struct super_block *s, void *data, int silent)
301{
302 struct inode * root_inode;
303 struct dentry * root;
304 struct file * pipe;
305 int pipefd;
306 struct autofs_sb_info *sbi;
307 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
310 if ( !sbi )
311 goto fail_unlock;
312 DPRINTK("starting up, sbi = %p",sbi);
313
314 memset(sbi, 0, sizeof(*sbi));
315
316 s->s_fs_info = sbi;
317 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kent104e49f2005-07-27 11:43:45 -0700318 sbi->root = NULL;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800319 sbi->pipefd = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 sbi->catatonic = 0;
321 sbi->exp_timeout = 0;
322 sbi->oz_pgrp = process_group(current);
323 sbi->sb = s;
324 sbi->version = 0;
325 sbi->sub_version = 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800326 sbi->type = 0;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800327 sbi->min_proto = 0;
328 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800329 mutex_init(&sbi->wq_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700330 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 sbi->queues = NULL;
332 s->s_blocksize = 1024;
333 s->s_blocksize_bits = 10;
334 s->s_magic = AUTOFS_SUPER_MAGIC;
335 s->s_op = &autofs4_sops;
336 s->s_time_gran = 1;
337
338 /*
339 * Get the root inode and dentry, but defer checking for errors.
340 */
341 ino = autofs4_mkroot(sbi);
342 if (!ino)
343 goto fail_free;
344 root_inode = autofs4_get_inode(s, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!root_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800346 goto fail_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 root = d_alloc_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!root)
350 goto fail_iput;
Ian Kent34ca9592006-03-27 01:14:54 -0800351 pipe = NULL;
352
353 root->d_op = &autofs4_sb_dentry_operations;
354 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* Can this call block? */
357 if (parse_options(data, &pipefd,
358 &root_inode->i_uid, &root_inode->i_gid,
Ian Kent34ca9592006-03-27 01:14:54 -0800359 &sbi->oz_pgrp, &sbi->type,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800360 &sbi->min_proto, &sbi->max_proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 printk("autofs: called with bogus options\n");
362 goto fail_dput;
363 }
364
Ian Kent34ca9592006-03-27 01:14:54 -0800365 root_inode->i_fop = &autofs4_root_operations;
Ian Kent44d53eb2006-03-27 01:14:56 -0800366 root_inode->i_op = sbi->type & AUTOFS_TYPE_DIRECT ?
Ian Kent34ca9592006-03-27 01:14:54 -0800367 &autofs4_direct_root_inode_operations :
368 &autofs4_indirect_root_inode_operations;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800371 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
372 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 printk("autofs: kernel does not match daemon version "
374 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800375 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
377 goto fail_dput;
378 }
379
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800380 /* Establish highest kernel protocol version */
381 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
382 sbi->version = AUTOFS_MAX_PROTO_VERSION;
383 else
384 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
386
387 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
388 pipe = fget(pipefd);
389
390 if ( !pipe ) {
391 printk("autofs: could not open pipe file descriptor\n");
392 goto fail_dput;
393 }
394 if ( !pipe->f_op || !pipe->f_op->write )
395 goto fail_fput;
396 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800397 sbi->pipefd = pipefd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /*
Ian Kent104e49f2005-07-27 11:43:45 -0700400 * Take a reference to the root dentry so we get a chance to
401 * clean up the dentry tree on umount.
402 * See autofs4_force_release.
403 */
404 sbi->root = dget(root);
405
406 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * Success! Install the root dentry now to indicate completion.
408 */
409 s->s_root = root;
410 return 0;
411
412 /*
413 * Failure ... clean up.
414 */
415fail_fput:
416 printk("autofs: pipe file descriptor does not contain proper ops\n");
417 fput(pipe);
418 /* fall through */
419fail_dput:
420 dput(root);
421 goto fail_free;
422fail_iput:
423 printk("autofs: get root dentry failed\n");
424 iput(root_inode);
Ian Kent34ca9592006-03-27 01:14:54 -0800425fail_ino:
426 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427fail_free:
428 kfree(sbi);
429fail_unlock:
430 return -EINVAL;
431}
432
433struct inode *autofs4_get_inode(struct super_block *sb,
434 struct autofs_info *inf)
435{
436 struct inode *inode = new_inode(sb);
437
438 if (inode == NULL)
439 return NULL;
440
441 inf->inode = inode;
442 inode->i_mode = inf->mode;
443 if (sb->s_root) {
444 inode->i_uid = sb->s_root->d_inode->i_uid;
445 inode->i_gid = sb->s_root->d_inode->i_gid;
446 } else {
447 inode->i_uid = 0;
448 inode->i_gid = 0;
449 }
450 inode->i_blksize = PAGE_CACHE_SIZE;
451 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}