blob: cf8abc793d5051c4e8f0db5a3db2613c02b18377 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
26 struct autofs_sb_info *sbi, mode_t mode)
27{
28 int reinit = 1;
29
30 if (ino == NULL) {
31 reinit = 0;
32 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
33 }
34
35 if (ino == NULL)
36 return NULL;
37
Ian Kent25767372008-07-23 21:30:12 -070038 if (!reinit) {
39 ino->flags = 0;
Ian Kent25767372008-07-23 21:30:12 -070040 ino->dentry = NULL;
41 ino->size = 0;
42 INIT_LIST_HEAD(&ino->active);
Ian Kent4f8427d2009-12-15 16:45:42 -080043 ino->active_count = 0;
Ian Kent25767372008-07-23 21:30:12 -070044 INIT_LIST_HEAD(&ino->expiring);
45 atomic_set(&ino->count, 0);
46 }
47
Ian Kentc0f54d32008-10-15 22:02:52 -070048 ino->uid = 0;
49 ino->gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 ino->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 ino->last_used = jiffies;
52
53 ino->sbi = sbi;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return ino;
56}
57
58void autofs4_free_ino(struct autofs_info *ino)
59{
60 if (ino->dentry) {
61 ino->dentry->d_fsdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 ino->dentry = NULL;
63 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 kfree(ino);
65}
66
David Howells6ce31522006-10-11 01:22:15 -070067void autofs4_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct autofs_sb_info *sbi = autofs4_sbi(sb);
70
Ian Kentba8df432006-11-14 02:03:29 -080071 /*
72 * In the event of a failure in get_sb_nodev the superblock
73 * info is not present so nothing else has been setup, so
Jiri Kosinac949d4e2006-12-06 20:39:38 -080074 * just call kill_anon_super when we are called from
75 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -080076 */
77 if (!sbi)
Jiri Kosinac949d4e2006-12-06 20:39:38 -080078 goto out_kill_sb;
Ian Kentba8df432006-11-14 02:03:29 -080079
Ian Kent5a11d4d2008-07-23 21:30:17 -070080 /* Free wait queues, close pipe */
81 autofs4_catatonic_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Ian Kentf50b6f82007-02-20 13:58:10 -080083 sb->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 kfree(sbi);
85
Jiri Kosinac949d4e2006-12-06 20:39:38 -080086out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 DPRINTK("shutting down");
Al Viro5b7e9342010-01-24 00:28:52 -050088 kill_litter_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Ian Kentd7c4a5f2006-03-27 01:14:49 -080091static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
92{
93 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
Miklos Szerediaef97cb2008-02-08 04:21:37 -080094 struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
Ian Kentd7c4a5f2006-03-27 01:14:49 -080095
96 if (!sbi)
97 return 0;
98
99 seq_printf(m, ",fd=%d", sbi->pipefd);
Miklos Szerediaef97cb2008-02-08 04:21:37 -0800100 if (root_inode->i_uid != 0)
101 seq_printf(m, ",uid=%u", root_inode->i_uid);
102 if (root_inode->i_gid != 0)
103 seq_printf(m, ",gid=%u", root_inode->i_gid);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800104 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
105 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
106 seq_printf(m, ",minproto=%d", sbi->min_proto);
107 seq_printf(m, ",maxproto=%d", sbi->max_proto);
108
Ian Kenta92daf62009-01-06 14:42:08 -0800109 if (autofs_type_offset(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -0800110 seq_printf(m, ",offset");
Ian Kenta92daf62009-01-06 14:42:08 -0800111 else if (autofs_type_direct(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -0800112 seq_printf(m, ",direct");
113 else
114 seq_printf(m, ",indirect");
115
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800116 return 0;
117}
118
Al Viro292c5ee2011-01-17 00:47:38 -0500119static void autofs4_evict_inode(struct inode *inode)
120{
121 end_writeback(inode);
122 kfree(inode->i_private);
123}
124
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800125static const struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800127 .show_options = autofs4_show_options,
Al Viro292c5ee2011-01-17 00:47:38 -0500128 .evict_inode = autofs4_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
Ian Kent34ca9592006-03-27 01:14:54 -0800131enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
132 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Steven Whitehousea447c092008-10-13 10:46:57 +0100134static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 {Opt_fd, "fd=%u"},
136 {Opt_uid, "uid=%u"},
137 {Opt_gid, "gid=%u"},
138 {Opt_pgrp, "pgrp=%u"},
139 {Opt_minproto, "minproto=%u"},
140 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800141 {Opt_indirect, "indirect"},
142 {Opt_direct, "direct"},
143 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 {Opt_err, NULL}
145};
146
147static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700148 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 char *p;
151 substring_t args[MAX_OPT_ARGS];
152 int option;
153
David Howells0eb790e2008-11-14 10:38:46 +1100154 *uid = current_uid();
155 *gid = current_gid();
Pavel Emelianova47afb02007-10-18 23:39:46 -0700156 *pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 *minproto = AUTOFS_MIN_PROTO_VERSION;
159 *maxproto = AUTOFS_MAX_PROTO_VERSION;
160
161 *pipefd = -1;
162
163 if (!options)
164 return 1;
165
166 while ((p = strsep(&options, ",")) != NULL) {
167 int token;
168 if (!*p)
169 continue;
170
171 token = match_token(p, tokens, args);
172 switch (token) {
173 case Opt_fd:
174 if (match_int(args, pipefd))
175 return 1;
176 break;
177 case Opt_uid:
178 if (match_int(args, &option))
179 return 1;
180 *uid = option;
181 break;
182 case Opt_gid:
183 if (match_int(args, &option))
184 return 1;
185 *gid = option;
186 break;
187 case Opt_pgrp:
188 if (match_int(args, &option))
189 return 1;
190 *pgrp = option;
191 break;
192 case Opt_minproto:
193 if (match_int(args, &option))
194 return 1;
195 *minproto = option;
196 break;
197 case Opt_maxproto:
198 if (match_int(args, &option))
199 return 1;
200 *maxproto = option;
201 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800202 case Opt_indirect:
Ian Kenta92daf62009-01-06 14:42:08 -0800203 set_autofs_type_indirect(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800204 break;
205 case Opt_direct:
Ian Kenta92daf62009-01-06 14:42:08 -0800206 set_autofs_type_direct(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800207 break;
208 case Opt_offset:
Ian Kenta92daf62009-01-06 14:42:08 -0800209 set_autofs_type_offset(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 default:
212 return 1;
213 }
214 }
215 return (*pipefd < 0);
216}
217
218static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
219{
220 struct autofs_info *ino;
221
222 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
223 if (!ino)
224 return NULL;
225
226 return ino;
227}
228
229int autofs4_fill_super(struct super_block *s, void *data, int silent)
230{
231 struct inode * root_inode;
232 struct dentry * root;
233 struct file * pipe;
234 int pipefd;
235 struct autofs_sb_info *sbi;
236 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Mariusz Kozlowskib63d50c2007-10-16 23:26:44 -0700238 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700239 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto fail_unlock;
241 DPRINTK("starting up, sbi = %p",sbi);
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 s->s_fs_info = sbi;
244 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800245 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800246 sbi->pipe = NULL;
247 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 sbi->exp_timeout = 0;
Pavel Emelianova47afb02007-10-18 23:39:46 -0700249 sbi->oz_pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 sbi->sb = s;
251 sbi->version = 0;
252 sbi->sub_version = 0;
Ian Kenta92daf62009-01-06 14:42:08 -0800253 set_autofs_type_indirect(&sbi->type);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800254 sbi->min_proto = 0;
255 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800256 mutex_init(&sbi->wq_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700257 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 sbi->queues = NULL;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700259 spin_lock_init(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700260 INIT_LIST_HEAD(&sbi->active_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700261 INIT_LIST_HEAD(&sbi->expiring_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 s->s_blocksize = 1024;
263 s->s_blocksize_bits = 10;
264 s->s_magic = AUTOFS_SUPER_MAGIC;
265 s->s_op = &autofs4_sops;
David Howellsb650c852011-01-15 10:51:57 +0000266 s->s_d_op = &autofs4_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 s->s_time_gran = 1;
268
269 /*
270 * Get the root inode and dentry, but defer checking for errors.
271 */
272 ino = autofs4_mkroot(sbi);
273 if (!ino)
274 goto fail_free;
275 root_inode = autofs4_get_inode(s, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (!root_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800277 goto fail_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 root = d_alloc_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!root)
281 goto fail_iput;
Ian Kent34ca9592006-03-27 01:14:54 -0800282 pipe = NULL;
283
Ian Kent34ca9592006-03-27 01:14:54 -0800284 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* Can this call block? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700287 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
288 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
289 &sbi->max_proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 printk("autofs: called with bogus options\n");
291 goto fail_dput;
292 }
293
David Howellsb650c852011-01-15 10:51:57 +0000294 if (autofs_type_trigger(sbi->type))
Ian Kentb5b80172011-01-14 18:46:03 +0000295 __managed_dentry_set_managed(root);
Ian Kent10584212011-01-14 18:45:58 +0000296
Ian Kent34ca9592006-03-27 01:14:54 -0800297 root_inode->i_fop = &autofs4_root_operations;
Ian Kente61da202011-01-14 18:46:14 +0000298 root_inode->i_op = &autofs4_dir_inode_operations;
Ian Kent34ca9592006-03-27 01:14:54 -0800299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800301 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
302 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 printk("autofs: kernel does not match daemon version "
304 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800305 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
307 goto fail_dput;
308 }
309
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800310 /* Establish highest kernel protocol version */
311 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
312 sbi->version = AUTOFS_MAX_PROTO_VERSION;
313 else
314 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
316
317 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
318 pipe = fget(pipefd);
319
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700320 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 printk("autofs: could not open pipe file descriptor\n");
322 goto fail_dput;
323 }
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700324 if (!pipe->f_op || !pipe->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto fail_fput;
326 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800327 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800328 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * Success! Install the root dentry now to indicate completion.
332 */
333 s->s_root = root;
334 return 0;
335
336 /*
337 * Failure ... clean up.
338 */
339fail_fput:
340 printk("autofs: pipe file descriptor does not contain proper ops\n");
341 fput(pipe);
342 /* fall through */
343fail_dput:
344 dput(root);
345 goto fail_free;
346fail_iput:
347 printk("autofs: get root dentry failed\n");
348 iput(root_inode);
Ian Kent34ca9592006-03-27 01:14:54 -0800349fail_ino:
350 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351fail_free:
352 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800353 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354fail_unlock:
355 return -EINVAL;
356}
357
358struct inode *autofs4_get_inode(struct super_block *sb,
359 struct autofs_info *inf)
360{
361 struct inode *inode = new_inode(sb);
362
363 if (inode == NULL)
364 return NULL;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 inode->i_mode = inf->mode;
367 if (sb->s_root) {
368 inode->i_uid = sb->s_root->d_inode->i_uid;
369 inode->i_gid = sb->s_root->d_inode->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400372 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (S_ISDIR(inf->mode)) {
375 inode->i_nlink = 2;
376 inode->i_op = &autofs4_dir_inode_operations;
377 inode->i_fop = &autofs4_dir_operations;
378 } else if (S_ISLNK(inf->mode)) {
379 inode->i_size = inf->size;
380 inode->i_op = &autofs4_symlink_inode_operations;
381 }
382
383 return inode;
384}