blob: 9c48250fd726406fc040816c96b4fa42e8d305ce [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
Jiri Kosinac949d4e2006-12-06 20:39:38 -0800155 * just call kill_anon_super when we are called from
156 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -0800157 */
158 if (!sbi)
Jiri Kosinac949d4e2006-12-06 20:39:38 -0800159 goto out_kill_sb;
Ian Kentba8df432006-11-14 02:03:29 -0800160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 sb->s_fs_info = NULL;
162
163 if ( !sbi->catatonic )
164 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
165
Ian Kent104e49f2005-07-27 11:43:45 -0700166 /* Clean up and release dangling references */
Dave Jones3370c742006-03-27 01:14:57 -0800167 autofs4_force_release(sbi);
Ian Kent104e49f2005-07-27 11:43:45 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 kfree(sbi);
170
Jiri Kosinac949d4e2006-12-06 20:39:38 -0800171out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 DPRINTK("shutting down");
David Howells6ce31522006-10-11 01:22:15 -0700173 kill_anon_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800176static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
177{
178 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
179
180 if (!sbi)
181 return 0;
182
183 seq_printf(m, ",fd=%d", sbi->pipefd);
184 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
185 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
186 seq_printf(m, ",minproto=%d", sbi->min_proto);
187 seq_printf(m, ",maxproto=%d", sbi->max_proto);
188
Ian Kent44d53eb2006-03-27 01:14:56 -0800189 if (sbi->type & AUTOFS_TYPE_OFFSET)
Ian Kent34ca9592006-03-27 01:14:54 -0800190 seq_printf(m, ",offset");
Ian Kent44d53eb2006-03-27 01:14:56 -0800191 else if (sbi->type & AUTOFS_TYPE_DIRECT)
Ian Kent34ca9592006-03-27 01:14:54 -0800192 seq_printf(m, ",direct");
193 else
194 seq_printf(m, ",indirect");
195
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800196 return 0;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800201 .show_options = autofs4_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Ian Kent34ca9592006-03-27 01:14:54 -0800204enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
205 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207static match_table_t tokens = {
208 {Opt_fd, "fd=%u"},
209 {Opt_uid, "uid=%u"},
210 {Opt_gid, "gid=%u"},
211 {Opt_pgrp, "pgrp=%u"},
212 {Opt_minproto, "minproto=%u"},
213 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800214 {Opt_indirect, "indirect"},
215 {Opt_direct, "direct"},
216 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 {Opt_err, NULL}
218};
219
220static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
Ian Kent34ca9592006-03-27 01:14:54 -0800221 pid_t *pgrp, unsigned int *type,
222 int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 char *p;
225 substring_t args[MAX_OPT_ARGS];
226 int option;
227
228 *uid = current->uid;
229 *gid = current->gid;
230 *pgrp = process_group(current);
231
232 *minproto = AUTOFS_MIN_PROTO_VERSION;
233 *maxproto = AUTOFS_MAX_PROTO_VERSION;
234
235 *pipefd = -1;
236
237 if (!options)
238 return 1;
239
240 while ((p = strsep(&options, ",")) != NULL) {
241 int token;
242 if (!*p)
243 continue;
244
245 token = match_token(p, tokens, args);
246 switch (token) {
247 case Opt_fd:
248 if (match_int(args, pipefd))
249 return 1;
250 break;
251 case Opt_uid:
252 if (match_int(args, &option))
253 return 1;
254 *uid = option;
255 break;
256 case Opt_gid:
257 if (match_int(args, &option))
258 return 1;
259 *gid = option;
260 break;
261 case Opt_pgrp:
262 if (match_int(args, &option))
263 return 1;
264 *pgrp = option;
265 break;
266 case Opt_minproto:
267 if (match_int(args, &option))
268 return 1;
269 *minproto = option;
270 break;
271 case Opt_maxproto:
272 if (match_int(args, &option))
273 return 1;
274 *maxproto = option;
275 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800276 case Opt_indirect:
Ian Kent44d53eb2006-03-27 01:14:56 -0800277 *type = AUTOFS_TYPE_INDIRECT;
Ian Kent34ca9592006-03-27 01:14:54 -0800278 break;
279 case Opt_direct:
Ian Kent44d53eb2006-03-27 01:14:56 -0800280 *type = AUTOFS_TYPE_DIRECT;
Ian Kent34ca9592006-03-27 01:14:54 -0800281 break;
282 case Opt_offset:
Ian Kent44d53eb2006-03-27 01:14:56 -0800283 *type = AUTOFS_TYPE_DIRECT | AUTOFS_TYPE_OFFSET;
Ian Kent34ca9592006-03-27 01:14:54 -0800284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 default:
286 return 1;
287 }
288 }
289 return (*pipefd < 0);
290}
291
292static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
293{
294 struct autofs_info *ino;
295
296 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
297 if (!ino)
298 return NULL;
299
300 return ino;
301}
302
Ian Kent34ca9592006-03-27 01:14:54 -0800303static struct dentry_operations autofs4_sb_dentry_operations = {
304 .d_release = autofs4_dentry_release,
305};
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307int autofs4_fill_super(struct super_block *s, void *data, int silent)
308{
309 struct inode * root_inode;
310 struct dentry * root;
311 struct file * pipe;
312 int pipefd;
313 struct autofs_sb_info *sbi;
314 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
317 if ( !sbi )
318 goto fail_unlock;
319 DPRINTK("starting up, sbi = %p",sbi);
320
321 memset(sbi, 0, sizeof(*sbi));
322
323 s->s_fs_info = sbi;
324 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800325 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800326 sbi->pipe = NULL;
327 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 sbi->exp_timeout = 0;
329 sbi->oz_pgrp = process_group(current);
330 sbi->sb = s;
331 sbi->version = 0;
332 sbi->sub_version = 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800333 sbi->type = 0;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800334 sbi->min_proto = 0;
335 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800336 mutex_init(&sbi->wq_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700337 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 sbi->queues = NULL;
339 s->s_blocksize = 1024;
340 s->s_blocksize_bits = 10;
341 s->s_magic = AUTOFS_SUPER_MAGIC;
342 s->s_op = &autofs4_sops;
343 s->s_time_gran = 1;
344
345 /*
346 * Get the root inode and dentry, but defer checking for errors.
347 */
348 ino = autofs4_mkroot(sbi);
349 if (!ino)
350 goto fail_free;
351 root_inode = autofs4_get_inode(s, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!root_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800353 goto fail_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 root = d_alloc_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!root)
357 goto fail_iput;
Ian Kent34ca9592006-03-27 01:14:54 -0800358 pipe = NULL;
359
360 root->d_op = &autofs4_sb_dentry_operations;
361 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* Can this call block? */
364 if (parse_options(data, &pipefd,
365 &root_inode->i_uid, &root_inode->i_gid,
Ian Kent34ca9592006-03-27 01:14:54 -0800366 &sbi->oz_pgrp, &sbi->type,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800367 &sbi->min_proto, &sbi->max_proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 printk("autofs: called with bogus options\n");
369 goto fail_dput;
370 }
371
Ian Kent34ca9592006-03-27 01:14:54 -0800372 root_inode->i_fop = &autofs4_root_operations;
Ian Kent44d53eb2006-03-27 01:14:56 -0800373 root_inode->i_op = sbi->type & AUTOFS_TYPE_DIRECT ?
Ian Kent34ca9592006-03-27 01:14:54 -0800374 &autofs4_direct_root_inode_operations :
375 &autofs4_indirect_root_inode_operations;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800378 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
379 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 printk("autofs: kernel does not match daemon version "
381 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800382 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
384 goto fail_dput;
385 }
386
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800387 /* Establish highest kernel protocol version */
388 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
389 sbi->version = AUTOFS_MAX_PROTO_VERSION;
390 else
391 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
393
394 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
395 pipe = fget(pipefd);
396
397 if ( !pipe ) {
398 printk("autofs: could not open pipe file descriptor\n");
399 goto fail_dput;
400 }
401 if ( !pipe->f_op || !pipe->f_op->write )
402 goto fail_fput;
403 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800404 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800405 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * Success! Install the root dentry now to indicate completion.
409 */
410 s->s_root = root;
411 return 0;
412
413 /*
414 * Failure ... clean up.
415 */
416fail_fput:
417 printk("autofs: pipe file descriptor does not contain proper ops\n");
418 fput(pipe);
419 /* fall through */
420fail_dput:
421 dput(root);
422 goto fail_free;
423fail_iput:
424 printk("autofs: get root dentry failed\n");
425 iput(root_inode);
Ian Kent34ca9592006-03-27 01:14:54 -0800426fail_ino:
427 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428fail_free:
429 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800430 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431fail_unlock:
432 return -EINVAL;
433}
434
435struct inode *autofs4_get_inode(struct super_block *sb,
436 struct autofs_info *inf)
437{
438 struct inode *inode = new_inode(sb);
439
440 if (inode == NULL)
441 return NULL;
442
443 inf->inode = inode;
444 inode->i_mode = inf->mode;
445 if (sb->s_root) {
446 inode->i_uid = sb->s_root->d_inode->i_uid;
447 inode->i_gid = sb->s_root->d_inode->i_gid;
448 } else {
449 inode->i_uid = 0;
450 inode->i_gid = 0;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 inode->i_blocks = 0;
453 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
454
455 if (S_ISDIR(inf->mode)) {
456 inode->i_nlink = 2;
457 inode->i_op = &autofs4_dir_inode_operations;
458 inode->i_fop = &autofs4_dir_operations;
459 } else if (S_ISLNK(inf->mode)) {
460 inode->i_size = inf->size;
461 inode->i_op = &autofs4_symlink_inode_operations;
462 }
463
464 return inode;
465}