blob: 45d55819203d5f00cc5d9150bc81d7bdd63ca38a [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>
Jeff Garzike18fa702006-09-24 11:13:19 -040021#include <linux/magic.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{
Ian Kent25767372008-07-23 21:30:12 -070027 if (ino->u.symlink) {
28 kfree(ino->u.symlink);
29 ino->u.symlink = NULL;
30 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
33struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
34 struct autofs_sb_info *sbi, mode_t mode)
35{
36 int reinit = 1;
37
38 if (ino == NULL) {
39 reinit = 0;
40 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
41 }
42
43 if (ino == NULL)
44 return NULL;
45
Ian Kent25767372008-07-23 21:30:12 -070046 if (!reinit) {
47 ino->flags = 0;
48 ino->inode = NULL;
49 ino->dentry = NULL;
50 ino->size = 0;
51 INIT_LIST_HEAD(&ino->active);
52 INIT_LIST_HEAD(&ino->expiring);
53 atomic_set(&ino->count, 0);
54 }
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 ino->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 ino->last_used = jiffies;
58
59 ino->sbi = sbi;
60
61 if (reinit && ino->free)
62 (ino->free)(ino);
63
64 memset(&ino->u, 0, sizeof(ino->u));
65
66 ino->free = NULL;
67
68 if (S_ISLNK(mode))
69 ino->free = ino_lnkfree;
70
71 return ino;
72}
73
74void autofs4_free_ino(struct autofs_info *ino)
75{
Ian Kent1aff3c82006-03-27 01:14:46 -080076 struct autofs_info *p_ino;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (ino->dentry) {
79 ino->dentry->d_fsdata = NULL;
Ian Kent1aff3c82006-03-27 01:14:46 -080080 if (ino->dentry->d_inode) {
81 struct dentry *parent = ino->dentry->d_parent;
82 if (atomic_dec_and_test(&ino->count)) {
83 p_ino = autofs4_dentry_ino(parent);
84 if (p_ino && parent != ino->dentry)
85 atomic_dec(&p_ino->count);
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 dput(ino->dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -080088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 ino->dentry = NULL;
90 }
91 if (ino->free)
92 (ino->free)(ino);
93 kfree(ino);
94}
95
Ian Kent104e49f2005-07-27 11:43:45 -070096/*
97 * Deal with the infamous "Busy inodes after umount ..." message.
98 *
99 * Clean up the dentry tree. This happens with autofs if the user
100 * space program goes away due to a SIGKILL, SIGSEGV etc.
101 */
102static void autofs4_force_release(struct autofs_sb_info *sbi)
103{
David Howells6ce31522006-10-11 01:22:15 -0700104 struct dentry *this_parent = sbi->sb->s_root;
Ian Kent104e49f2005-07-27 11:43:45 -0700105 struct list_head *next;
106
Ian Kentba8df432006-11-14 02:03:29 -0800107 if (!sbi->sb->s_root)
108 return;
109
Ian Kent104e49f2005-07-27 11:43:45 -0700110 spin_lock(&dcache_lock);
111repeat:
112 next = this_parent->d_subdirs.next;
113resume:
114 while (next != &this_parent->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800115 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
Ian Kent104e49f2005-07-27 11:43:45 -0700116
117 /* Negative dentry - don`t care */
118 if (!simple_positive(dentry)) {
119 next = next->next;
120 continue;
121 }
122
123 if (!list_empty(&dentry->d_subdirs)) {
124 this_parent = dentry;
125 goto repeat;
126 }
127
128 next = next->next;
129 spin_unlock(&dcache_lock);
130
131 DPRINTK("dentry %p %.*s",
132 dentry, (int)dentry->d_name.len, dentry->d_name.name);
133
134 dput(dentry);
135 spin_lock(&dcache_lock);
136 }
137
David Howells6ce31522006-10-11 01:22:15 -0700138 if (this_parent != sbi->sb->s_root) {
Ian Kent104e49f2005-07-27 11:43:45 -0700139 struct dentry *dentry = this_parent;
140
Eric Dumazet5160ee62006-01-08 01:03:32 -0800141 next = this_parent->d_u.d_child.next;
Ian Kent104e49f2005-07-27 11:43:45 -0700142 this_parent = this_parent->d_parent;
143 spin_unlock(&dcache_lock);
144 DPRINTK("parent dentry %p %.*s",
145 dentry, (int)dentry->d_name.len, dentry->d_name.name);
146 dput(dentry);
147 spin_lock(&dcache_lock);
148 goto resume;
149 }
150 spin_unlock(&dcache_lock);
Ian Kent104e49f2005-07-27 11:43:45 -0700151}
152
David Howells6ce31522006-10-11 01:22:15 -0700153void autofs4_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct autofs_sb_info *sbi = autofs4_sbi(sb);
156
Ian Kentba8df432006-11-14 02:03:29 -0800157 /*
158 * In the event of a failure in get_sb_nodev the superblock
159 * info is not present so nothing else has been setup, so
Jiri Kosinac949d4e2006-12-06 20:39:38 -0800160 * just call kill_anon_super when we are called from
161 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -0800162 */
163 if (!sbi)
Jiri Kosinac949d4e2006-12-06 20:39:38 -0800164 goto out_kill_sb;
Ian Kentba8df432006-11-14 02:03:29 -0800165
Ian Kent5a11d4d2008-07-23 21:30:17 -0700166 /* Free wait queues, close pipe */
167 autofs4_catatonic_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Ian Kent104e49f2005-07-27 11:43:45 -0700169 /* Clean up and release dangling references */
Dave Jones3370c742006-03-27 01:14:57 -0800170 autofs4_force_release(sbi);
Ian Kent104e49f2005-07-27 11:43:45 -0700171
Ian Kentf50b6f82007-02-20 13:58:10 -0800172 sb->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 kfree(sbi);
174
Jiri Kosinac949d4e2006-12-06 20:39:38 -0800175out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 DPRINTK("shutting down");
David Howells6ce31522006-10-11 01:22:15 -0700177 kill_anon_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800180static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
181{
182 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
Miklos Szerediaef97cb2008-02-08 04:21:37 -0800183 struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800184
185 if (!sbi)
186 return 0;
187
188 seq_printf(m, ",fd=%d", sbi->pipefd);
Miklos Szerediaef97cb2008-02-08 04:21:37 -0800189 if (root_inode->i_uid != 0)
190 seq_printf(m, ",uid=%u", root_inode->i_uid);
191 if (root_inode->i_gid != 0)
192 seq_printf(m, ",gid=%u", root_inode->i_gid);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800193 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
194 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
195 seq_printf(m, ",minproto=%d", sbi->min_proto);
196 seq_printf(m, ",maxproto=%d", sbi->max_proto);
197
Ian Kent44d53eb2006-03-27 01:14:56 -0800198 if (sbi->type & AUTOFS_TYPE_OFFSET)
Ian Kent34ca9592006-03-27 01:14:54 -0800199 seq_printf(m, ",offset");
Ian Kent44d53eb2006-03-27 01:14:56 -0800200 else if (sbi->type & AUTOFS_TYPE_DIRECT)
Ian Kent34ca9592006-03-27 01:14:54 -0800201 seq_printf(m, ",direct");
202 else
203 seq_printf(m, ",indirect");
204
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800205 return 0;
206}
207
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800208static const struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800210 .show_options = autofs4_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
Ian Kent34ca9592006-03-27 01:14:54 -0800213enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
214 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Steven Whitehousea447c092008-10-13 10:46:57 +0100216static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 {Opt_fd, "fd=%u"},
218 {Opt_uid, "uid=%u"},
219 {Opt_gid, "gid=%u"},
220 {Opt_pgrp, "pgrp=%u"},
221 {Opt_minproto, "minproto=%u"},
222 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800223 {Opt_indirect, "indirect"},
224 {Opt_direct, "direct"},
225 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 {Opt_err, NULL}
227};
228
229static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700230 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 char *p;
233 substring_t args[MAX_OPT_ARGS];
234 int option;
235
236 *uid = current->uid;
237 *gid = current->gid;
Pavel Emelianova47afb02007-10-18 23:39:46 -0700238 *pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 *minproto = AUTOFS_MIN_PROTO_VERSION;
241 *maxproto = AUTOFS_MAX_PROTO_VERSION;
242
243 *pipefd = -1;
244
245 if (!options)
246 return 1;
247
248 while ((p = strsep(&options, ",")) != NULL) {
249 int token;
250 if (!*p)
251 continue;
252
253 token = match_token(p, tokens, args);
254 switch (token) {
255 case Opt_fd:
256 if (match_int(args, pipefd))
257 return 1;
258 break;
259 case Opt_uid:
260 if (match_int(args, &option))
261 return 1;
262 *uid = option;
263 break;
264 case Opt_gid:
265 if (match_int(args, &option))
266 return 1;
267 *gid = option;
268 break;
269 case Opt_pgrp:
270 if (match_int(args, &option))
271 return 1;
272 *pgrp = option;
273 break;
274 case Opt_minproto:
275 if (match_int(args, &option))
276 return 1;
277 *minproto = option;
278 break;
279 case Opt_maxproto:
280 if (match_int(args, &option))
281 return 1;
282 *maxproto = option;
283 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800284 case Opt_indirect:
Ian Kent44d53eb2006-03-27 01:14:56 -0800285 *type = AUTOFS_TYPE_INDIRECT;
Ian Kent34ca9592006-03-27 01:14:54 -0800286 break;
287 case Opt_direct:
Ian Kent44d53eb2006-03-27 01:14:56 -0800288 *type = AUTOFS_TYPE_DIRECT;
Ian Kent34ca9592006-03-27 01:14:54 -0800289 break;
290 case Opt_offset:
Ian Kent44d53eb2006-03-27 01:14:56 -0800291 *type = AUTOFS_TYPE_DIRECT | AUTOFS_TYPE_OFFSET;
Ian Kent34ca9592006-03-27 01:14:54 -0800292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 default:
294 return 1;
295 }
296 }
297 return (*pipefd < 0);
298}
299
300static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
301{
302 struct autofs_info *ino;
303
304 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
305 if (!ino)
306 return NULL;
307
308 return ino;
309}
310
Ian Kent34ca9592006-03-27 01:14:54 -0800311static struct dentry_operations autofs4_sb_dentry_operations = {
312 .d_release = autofs4_dentry_release,
313};
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315int autofs4_fill_super(struct super_block *s, void *data, int silent)
316{
317 struct inode * root_inode;
318 struct dentry * root;
319 struct file * pipe;
320 int pipefd;
321 struct autofs_sb_info *sbi;
322 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Mariusz Kozlowskib63d50c2007-10-16 23:26:44 -0700324 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700325 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 goto fail_unlock;
327 DPRINTK("starting up, sbi = %p",sbi);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 s->s_fs_info = sbi;
330 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800331 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800332 sbi->pipe = NULL;
333 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 sbi->exp_timeout = 0;
Pavel Emelianova47afb02007-10-18 23:39:46 -0700335 sbi->oz_pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 sbi->sb = s;
337 sbi->version = 0;
338 sbi->sub_version = 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800339 sbi->type = 0;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800340 sbi->min_proto = 0;
341 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800342 mutex_init(&sbi->wq_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700343 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 sbi->queues = NULL;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700345 spin_lock_init(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700346 INIT_LIST_HEAD(&sbi->active_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700347 INIT_LIST_HEAD(&sbi->expiring_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 s->s_blocksize = 1024;
349 s->s_blocksize_bits = 10;
350 s->s_magic = AUTOFS_SUPER_MAGIC;
351 s->s_op = &autofs4_sops;
352 s->s_time_gran = 1;
353
354 /*
355 * Get the root inode and dentry, but defer checking for errors.
356 */
357 ino = autofs4_mkroot(sbi);
358 if (!ino)
359 goto fail_free;
360 root_inode = autofs4_get_inode(s, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!root_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800362 goto fail_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 root = d_alloc_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!root)
366 goto fail_iput;
Ian Kent34ca9592006-03-27 01:14:54 -0800367 pipe = NULL;
368
369 root->d_op = &autofs4_sb_dentry_operations;
370 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* Can this call block? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700373 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
374 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
375 &sbi->max_proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 printk("autofs: called with bogus options\n");
377 goto fail_dput;
378 }
379
Ian Kent34ca9592006-03-27 01:14:54 -0800380 root_inode->i_fop = &autofs4_root_operations;
Ian Kent44d53eb2006-03-27 01:14:56 -0800381 root_inode->i_op = sbi->type & AUTOFS_TYPE_DIRECT ?
Ian Kent34ca9592006-03-27 01:14:54 -0800382 &autofs4_direct_root_inode_operations :
383 &autofs4_indirect_root_inode_operations;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800386 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
387 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 printk("autofs: kernel does not match daemon version "
389 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800390 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
392 goto fail_dput;
393 }
394
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800395 /* Establish highest kernel protocol version */
396 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
397 sbi->version = AUTOFS_MAX_PROTO_VERSION;
398 else
399 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
401
402 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
403 pipe = fget(pipefd);
404
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700405 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 printk("autofs: could not open pipe file descriptor\n");
407 goto fail_dput;
408 }
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700409 if (!pipe->f_op || !pipe->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 goto fail_fput;
411 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800412 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800413 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * Success! Install the root dentry now to indicate completion.
417 */
418 s->s_root = root;
419 return 0;
420
421 /*
422 * Failure ... clean up.
423 */
424fail_fput:
425 printk("autofs: pipe file descriptor does not contain proper ops\n");
426 fput(pipe);
427 /* fall through */
428fail_dput:
429 dput(root);
430 goto fail_free;
431fail_iput:
432 printk("autofs: get root dentry failed\n");
433 iput(root_inode);
Ian Kent34ca9592006-03-27 01:14:54 -0800434fail_ino:
435 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436fail_free:
437 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800438 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439fail_unlock:
440 return -EINVAL;
441}
442
443struct inode *autofs4_get_inode(struct super_block *sb,
444 struct autofs_info *inf)
445{
446 struct inode *inode = new_inode(sb);
447
448 if (inode == NULL)
449 return NULL;
450
451 inf->inode = inode;
452 inode->i_mode = inf->mode;
453 if (sb->s_root) {
454 inode->i_uid = sb->s_root->d_inode->i_uid;
455 inode->i_gid = sb->s_root->d_inode->i_gid;
456 } else {
457 inode->i_uid = 0;
458 inode->i_gid = 0;
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 inode->i_blocks = 0;
461 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
462
463 if (S_ISDIR(inf->mode)) {
464 inode->i_nlink = 2;
465 inode->i_op = &autofs4_dir_inode_operations;
466 inode->i_fop = &autofs4_dir_operations;
467 } else if (S_ISLNK(inf->mode)) {
468 inode->i_size = inf->size;
469 inode->i_op = &autofs4_symlink_inode_operations;
470 }
471
472 return inode;
473}