blob: d0a3de247458aae03f045bf6788b64e5045ad950 [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);
Ian Kent213614d2009-12-15 16:45:51 -080052 INIT_LIST_HEAD(&ino->rehash_list);
Ian Kent4f8427d2009-12-15 16:45:42 -080053 ino->active_count = 0;
Ian Kent25767372008-07-23 21:30:12 -070054 INIT_LIST_HEAD(&ino->expiring);
55 atomic_set(&ino->count, 0);
56 }
57
Ian Kentc0f54d32008-10-15 22:02:52 -070058 ino->uid = 0;
59 ino->gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 ino->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 ino->last_used = jiffies;
62
63 ino->sbi = sbi;
64
65 if (reinit && ino->free)
66 (ino->free)(ino);
67
68 memset(&ino->u, 0, sizeof(ino->u));
69
70 ino->free = NULL;
71
72 if (S_ISLNK(mode))
73 ino->free = ino_lnkfree;
74
75 return ino;
76}
77
78void autofs4_free_ino(struct autofs_info *ino)
79{
Ian Kent1aff3c82006-03-27 01:14:46 -080080 struct autofs_info *p_ino;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (ino->dentry) {
83 ino->dentry->d_fsdata = NULL;
Ian Kent1aff3c82006-03-27 01:14:46 -080084 if (ino->dentry->d_inode) {
85 struct dentry *parent = ino->dentry->d_parent;
86 if (atomic_dec_and_test(&ino->count)) {
87 p_ino = autofs4_dentry_ino(parent);
88 if (p_ino && parent != ino->dentry)
89 atomic_dec(&p_ino->count);
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 dput(ino->dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -080092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 ino->dentry = NULL;
94 }
95 if (ino->free)
96 (ino->free)(ino);
97 kfree(ino);
98}
99
Ian Kent104e49f2005-07-27 11:43:45 -0700100/*
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 */
106static void autofs4_force_release(struct autofs_sb_info *sbi)
107{
David Howells6ce31522006-10-11 01:22:15 -0700108 struct dentry *this_parent = sbi->sb->s_root;
Ian Kent104e49f2005-07-27 11:43:45 -0700109 struct list_head *next;
110
Ian Kentba8df432006-11-14 02:03:29 -0800111 if (!sbi->sb->s_root)
112 return;
113
Ian Kent104e49f2005-07-27 11:43:45 -0700114 spin_lock(&dcache_lock);
115repeat:
116 next = this_parent->d_subdirs.next;
117resume:
118 while (next != &this_parent->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800119 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
Ian Kent104e49f2005-07-27 11:43:45 -0700120
121 /* Negative dentry - don`t care */
122 if (!simple_positive(dentry)) {
123 next = next->next;
124 continue;
125 }
126
127 if (!list_empty(&dentry->d_subdirs)) {
128 this_parent = dentry;
129 goto repeat;
130 }
131
132 next = next->next;
133 spin_unlock(&dcache_lock);
134
135 DPRINTK("dentry %p %.*s",
136 dentry, (int)dentry->d_name.len, dentry->d_name.name);
137
138 dput(dentry);
139 spin_lock(&dcache_lock);
140 }
141
David Howells6ce31522006-10-11 01:22:15 -0700142 if (this_parent != sbi->sb->s_root) {
Ian Kent104e49f2005-07-27 11:43:45 -0700143 struct dentry *dentry = this_parent;
144
Eric Dumazet5160ee62006-01-08 01:03:32 -0800145 next = this_parent->d_u.d_child.next;
Ian Kent104e49f2005-07-27 11:43:45 -0700146 this_parent = this_parent->d_parent;
147 spin_unlock(&dcache_lock);
148 DPRINTK("parent dentry %p %.*s",
149 dentry, (int)dentry->d_name.len, dentry->d_name.name);
150 dput(dentry);
151 spin_lock(&dcache_lock);
152 goto resume;
153 }
154 spin_unlock(&dcache_lock);
Ian Kent104e49f2005-07-27 11:43:45 -0700155}
156
David Howells6ce31522006-10-11 01:22:15 -0700157void autofs4_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct autofs_sb_info *sbi = autofs4_sbi(sb);
160
Ian Kentba8df432006-11-14 02:03:29 -0800161 /*
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
Jiri Kosinac949d4e2006-12-06 20:39:38 -0800164 * just call kill_anon_super when we are called from
165 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -0800166 */
167 if (!sbi)
Jiri Kosinac949d4e2006-12-06 20:39:38 -0800168 goto out_kill_sb;
Ian Kentba8df432006-11-14 02:03:29 -0800169
Ian Kent5a11d4d2008-07-23 21:30:17 -0700170 /* Free wait queues, close pipe */
171 autofs4_catatonic_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Ian Kent104e49f2005-07-27 11:43:45 -0700173 /* Clean up and release dangling references */
Dave Jones3370c742006-03-27 01:14:57 -0800174 autofs4_force_release(sbi);
Ian Kent104e49f2005-07-27 11:43:45 -0700175
Ian Kentf50b6f82007-02-20 13:58:10 -0800176 sb->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 kfree(sbi);
178
Jiri Kosinac949d4e2006-12-06 20:39:38 -0800179out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 DPRINTK("shutting down");
David Howells6ce31522006-10-11 01:22:15 -0700181 kill_anon_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800184static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
185{
186 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
Miklos Szerediaef97cb2008-02-08 04:21:37 -0800187 struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800188
189 if (!sbi)
190 return 0;
191
192 seq_printf(m, ",fd=%d", sbi->pipefd);
Miklos Szerediaef97cb2008-02-08 04:21:37 -0800193 if (root_inode->i_uid != 0)
194 seq_printf(m, ",uid=%u", root_inode->i_uid);
195 if (root_inode->i_gid != 0)
196 seq_printf(m, ",gid=%u", root_inode->i_gid);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800197 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
198 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
199 seq_printf(m, ",minproto=%d", sbi->min_proto);
200 seq_printf(m, ",maxproto=%d", sbi->max_proto);
201
Ian Kenta92daf62009-01-06 14:42:08 -0800202 if (autofs_type_offset(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -0800203 seq_printf(m, ",offset");
Ian Kenta92daf62009-01-06 14:42:08 -0800204 else if (autofs_type_direct(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -0800205 seq_printf(m, ",direct");
206 else
207 seq_printf(m, ",indirect");
208
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800209 return 0;
210}
211
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800212static const struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800214 .show_options = autofs4_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
Ian Kent34ca9592006-03-27 01:14:54 -0800217enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
218 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Steven Whitehousea447c092008-10-13 10:46:57 +0100220static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 {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"},
Ian Kent34ca9592006-03-27 01:14:54 -0800227 {Opt_indirect, "indirect"},
228 {Opt_direct, "direct"},
229 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 {Opt_err, NULL}
231};
232
233static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700234 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 char *p;
237 substring_t args[MAX_OPT_ARGS];
238 int option;
239
David Howells0eb790e2008-11-14 10:38:46 +1100240 *uid = current_uid();
241 *gid = current_gid();
Pavel Emelianova47afb02007-10-18 23:39:46 -0700242 *pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 *minproto = AUTOFS_MIN_PROTO_VERSION;
245 *maxproto = AUTOFS_MAX_PROTO_VERSION;
246
247 *pipefd = -1;
248
249 if (!options)
250 return 1;
251
252 while ((p = strsep(&options, ",")) != NULL) {
253 int token;
254 if (!*p)
255 continue;
256
257 token = match_token(p, tokens, args);
258 switch (token) {
259 case Opt_fd:
260 if (match_int(args, pipefd))
261 return 1;
262 break;
263 case Opt_uid:
264 if (match_int(args, &option))
265 return 1;
266 *uid = option;
267 break;
268 case Opt_gid:
269 if (match_int(args, &option))
270 return 1;
271 *gid = option;
272 break;
273 case Opt_pgrp:
274 if (match_int(args, &option))
275 return 1;
276 *pgrp = option;
277 break;
278 case Opt_minproto:
279 if (match_int(args, &option))
280 return 1;
281 *minproto = option;
282 break;
283 case Opt_maxproto:
284 if (match_int(args, &option))
285 return 1;
286 *maxproto = option;
287 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800288 case Opt_indirect:
Ian Kenta92daf62009-01-06 14:42:08 -0800289 set_autofs_type_indirect(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800290 break;
291 case Opt_direct:
Ian Kenta92daf62009-01-06 14:42:08 -0800292 set_autofs_type_direct(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800293 break;
294 case Opt_offset:
Ian Kenta92daf62009-01-06 14:42:08 -0800295 set_autofs_type_offset(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 default:
298 return 1;
299 }
300 }
301 return (*pipefd < 0);
302}
303
304static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
305{
306 struct autofs_info *ino;
307
308 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
309 if (!ino)
310 return NULL;
311
312 return ino;
313}
314
Al Viro08f11512009-02-20 05:56:19 +0000315static const struct dentry_operations autofs4_sb_dentry_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -0800316 .d_release = autofs4_dentry_release,
317};
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319int autofs4_fill_super(struct super_block *s, void *data, int silent)
320{
321 struct inode * root_inode;
322 struct dentry * root;
323 struct file * pipe;
324 int pipefd;
325 struct autofs_sb_info *sbi;
326 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Mariusz Kozlowskib63d50c2007-10-16 23:26:44 -0700328 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700329 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 goto fail_unlock;
331 DPRINTK("starting up, sbi = %p",sbi);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 s->s_fs_info = sbi;
334 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800335 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800336 sbi->pipe = NULL;
337 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 sbi->exp_timeout = 0;
Pavel Emelianova47afb02007-10-18 23:39:46 -0700339 sbi->oz_pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sbi->sb = s;
341 sbi->version = 0;
342 sbi->sub_version = 0;
Ian Kenta92daf62009-01-06 14:42:08 -0800343 set_autofs_type_indirect(&sbi->type);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800344 sbi->min_proto = 0;
345 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800346 mutex_init(&sbi->wq_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700347 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 sbi->queues = NULL;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700349 spin_lock_init(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700350 INIT_LIST_HEAD(&sbi->active_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700351 INIT_LIST_HEAD(&sbi->expiring_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 s->s_blocksize = 1024;
353 s->s_blocksize_bits = 10;
354 s->s_magic = AUTOFS_SUPER_MAGIC;
355 s->s_op = &autofs4_sops;
356 s->s_time_gran = 1;
357
358 /*
359 * Get the root inode and dentry, but defer checking for errors.
360 */
361 ino = autofs4_mkroot(sbi);
362 if (!ino)
363 goto fail_free;
364 root_inode = autofs4_get_inode(s, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!root_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800366 goto fail_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 root = d_alloc_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (!root)
370 goto fail_iput;
Ian Kent34ca9592006-03-27 01:14:54 -0800371 pipe = NULL;
372
373 root->d_op = &autofs4_sb_dentry_operations;
374 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* Can this call block? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700377 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 printk("autofs: called with bogus options\n");
381 goto fail_dput;
382 }
383
Ian Kent34ca9592006-03-27 01:14:54 -0800384 root_inode->i_fop = &autofs4_root_operations;
Ian Kenta92daf62009-01-06 14:42:08 -0800385 root_inode->i_op = autofs_type_trigger(sbi->type) ?
Ian Kent34ca9592006-03-27 01:14:54 -0800386 &autofs4_direct_root_inode_operations :
387 &autofs4_indirect_root_inode_operations;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800390 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
391 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 printk("autofs: kernel does not match daemon version "
393 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800394 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
396 goto fail_dput;
397 }
398
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800399 /* Establish highest kernel protocol version */
400 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
401 sbi->version = AUTOFS_MAX_PROTO_VERSION;
402 else
403 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
405
406 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
407 pipe = fget(pipefd);
408
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700409 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 printk("autofs: could not open pipe file descriptor\n");
411 goto fail_dput;
412 }
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700413 if (!pipe->f_op || !pipe->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 goto fail_fput;
415 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800416 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800417 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * Success! Install the root dentry now to indicate completion.
421 */
422 s->s_root = root;
423 return 0;
424
425 /*
426 * Failure ... clean up.
427 */
428fail_fput:
429 printk("autofs: pipe file descriptor does not contain proper ops\n");
430 fput(pipe);
431 /* fall through */
432fail_dput:
433 dput(root);
434 goto fail_free;
435fail_iput:
436 printk("autofs: get root dentry failed\n");
437 iput(root_inode);
Ian Kent34ca9592006-03-27 01:14:54 -0800438fail_ino:
439 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440fail_free:
441 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800442 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443fail_unlock:
444 return -EINVAL;
445}
446
447struct inode *autofs4_get_inode(struct super_block *sb,
448 struct autofs_info *inf)
449{
450 struct inode *inode = new_inode(sb);
451
452 if (inode == NULL)
453 return NULL;
454
455 inf->inode = inode;
456 inode->i_mode = inf->mode;
457 if (sb->s_root) {
458 inode->i_uid = sb->s_root->d_inode->i_uid;
459 inode->i_gid = sb->s_root->d_inode->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 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}