blob: 9e1a9dad23e16663fc69937ebc47dd7f286529f2 [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;
Ian Kent25767372008-07-23 21:30:12 -070048 ino->dentry = NULL;
49 ino->size = 0;
50 INIT_LIST_HEAD(&ino->active);
Ian Kent4f8427d2009-12-15 16:45:42 -080051 ino->active_count = 0;
Ian Kent25767372008-07-23 21:30:12 -070052 INIT_LIST_HEAD(&ino->expiring);
53 atomic_set(&ino->count, 0);
54 }
55
Ian Kentc0f54d32008-10-15 22:02:52 -070056 ino->uid = 0;
57 ino->gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 ino->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 ino->last_used = jiffies;
60
61 ino->sbi = sbi;
62
63 if (reinit && ino->free)
64 (ino->free)(ino);
65
66 memset(&ino->u, 0, sizeof(ino->u));
67
68 ino->free = NULL;
69
70 if (S_ISLNK(mode))
71 ino->free = ino_lnkfree;
72
73 return ino;
74}
75
76void autofs4_free_ino(struct autofs_info *ino)
77{
78 if (ino->dentry) {
79 ino->dentry->d_fsdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 ino->dentry = NULL;
81 }
82 if (ino->free)
83 (ino->free)(ino);
84 kfree(ino);
85}
86
David Howells6ce31522006-10-11 01:22:15 -070087void autofs4_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct autofs_sb_info *sbi = autofs4_sbi(sb);
90
Ian Kentba8df432006-11-14 02:03:29 -080091 /*
92 * In the event of a failure in get_sb_nodev the superblock
93 * info is not present so nothing else has been setup, so
Jiri Kosinac949d4e2006-12-06 20:39:38 -080094 * just call kill_anon_super when we are called from
95 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -080096 */
97 if (!sbi)
Jiri Kosinac949d4e2006-12-06 20:39:38 -080098 goto out_kill_sb;
Ian Kentba8df432006-11-14 02:03:29 -080099
Ian Kent5a11d4d2008-07-23 21:30:17 -0700100 /* Free wait queues, close pipe */
101 autofs4_catatonic_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Ian Kentf50b6f82007-02-20 13:58:10 -0800103 sb->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 kfree(sbi);
105
Jiri Kosinac949d4e2006-12-06 20:39:38 -0800106out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 DPRINTK("shutting down");
Al Viro5b7e9342010-01-24 00:28:52 -0500108 kill_litter_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800111static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
112{
113 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
Miklos Szerediaef97cb2008-02-08 04:21:37 -0800114 struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800115
116 if (!sbi)
117 return 0;
118
119 seq_printf(m, ",fd=%d", sbi->pipefd);
Miklos Szerediaef97cb2008-02-08 04:21:37 -0800120 if (root_inode->i_uid != 0)
121 seq_printf(m, ",uid=%u", root_inode->i_uid);
122 if (root_inode->i_gid != 0)
123 seq_printf(m, ",gid=%u", root_inode->i_gid);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800124 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
125 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
126 seq_printf(m, ",minproto=%d", sbi->min_proto);
127 seq_printf(m, ",maxproto=%d", sbi->max_proto);
128
Ian Kenta92daf62009-01-06 14:42:08 -0800129 if (autofs_type_offset(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -0800130 seq_printf(m, ",offset");
Ian Kenta92daf62009-01-06 14:42:08 -0800131 else if (autofs_type_direct(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -0800132 seq_printf(m, ",direct");
133 else
134 seq_printf(m, ",indirect");
135
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800136 return 0;
137}
138
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800139static const struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800141 .show_options = autofs4_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
Ian Kent34ca9592006-03-27 01:14:54 -0800144enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
145 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Steven Whitehousea447c092008-10-13 10:46:57 +0100147static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 {Opt_fd, "fd=%u"},
149 {Opt_uid, "uid=%u"},
150 {Opt_gid, "gid=%u"},
151 {Opt_pgrp, "pgrp=%u"},
152 {Opt_minproto, "minproto=%u"},
153 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800154 {Opt_indirect, "indirect"},
155 {Opt_direct, "direct"},
156 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 {Opt_err, NULL}
158};
159
160static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700161 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 char *p;
164 substring_t args[MAX_OPT_ARGS];
165 int option;
166
David Howells0eb790e2008-11-14 10:38:46 +1100167 *uid = current_uid();
168 *gid = current_gid();
Pavel Emelianova47afb02007-10-18 23:39:46 -0700169 *pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 *minproto = AUTOFS_MIN_PROTO_VERSION;
172 *maxproto = AUTOFS_MAX_PROTO_VERSION;
173
174 *pipefd = -1;
175
176 if (!options)
177 return 1;
178
179 while ((p = strsep(&options, ",")) != NULL) {
180 int token;
181 if (!*p)
182 continue;
183
184 token = match_token(p, tokens, args);
185 switch (token) {
186 case Opt_fd:
187 if (match_int(args, pipefd))
188 return 1;
189 break;
190 case Opt_uid:
191 if (match_int(args, &option))
192 return 1;
193 *uid = option;
194 break;
195 case Opt_gid:
196 if (match_int(args, &option))
197 return 1;
198 *gid = option;
199 break;
200 case Opt_pgrp:
201 if (match_int(args, &option))
202 return 1;
203 *pgrp = option;
204 break;
205 case Opt_minproto:
206 if (match_int(args, &option))
207 return 1;
208 *minproto = option;
209 break;
210 case Opt_maxproto:
211 if (match_int(args, &option))
212 return 1;
213 *maxproto = option;
214 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800215 case Opt_indirect:
Ian Kenta92daf62009-01-06 14:42:08 -0800216 set_autofs_type_indirect(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800217 break;
218 case Opt_direct:
Ian Kenta92daf62009-01-06 14:42:08 -0800219 set_autofs_type_direct(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800220 break;
221 case Opt_offset:
Ian Kenta92daf62009-01-06 14:42:08 -0800222 set_autofs_type_offset(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 default:
225 return 1;
226 }
227 }
228 return (*pipefd < 0);
229}
230
231static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
232{
233 struct autofs_info *ino;
234
235 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
236 if (!ino)
237 return NULL;
238
239 return ino;
240}
241
242int autofs4_fill_super(struct super_block *s, void *data, int silent)
243{
244 struct inode * root_inode;
245 struct dentry * root;
246 struct file * pipe;
247 int pipefd;
248 struct autofs_sb_info *sbi;
249 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Mariusz Kozlowskib63d50c2007-10-16 23:26:44 -0700251 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700252 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto fail_unlock;
254 DPRINTK("starting up, sbi = %p",sbi);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 s->s_fs_info = sbi;
257 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800258 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800259 sbi->pipe = NULL;
260 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 sbi->exp_timeout = 0;
Pavel Emelianova47afb02007-10-18 23:39:46 -0700262 sbi->oz_pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 sbi->sb = s;
264 sbi->version = 0;
265 sbi->sub_version = 0;
Ian Kenta92daf62009-01-06 14:42:08 -0800266 set_autofs_type_indirect(&sbi->type);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800267 sbi->min_proto = 0;
268 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800269 mutex_init(&sbi->wq_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700270 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 sbi->queues = NULL;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700272 spin_lock_init(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700273 INIT_LIST_HEAD(&sbi->active_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700274 INIT_LIST_HEAD(&sbi->expiring_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 s->s_blocksize = 1024;
276 s->s_blocksize_bits = 10;
277 s->s_magic = AUTOFS_SUPER_MAGIC;
278 s->s_op = &autofs4_sops;
David Howellsb650c852011-01-15 10:51:57 +0000279 s->s_d_op = &autofs4_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 s->s_time_gran = 1;
281
282 /*
283 * Get the root inode and dentry, but defer checking for errors.
284 */
285 ino = autofs4_mkroot(sbi);
286 if (!ino)
287 goto fail_free;
288 root_inode = autofs4_get_inode(s, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!root_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800290 goto fail_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 root = d_alloc_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (!root)
294 goto fail_iput;
Ian Kent34ca9592006-03-27 01:14:54 -0800295 pipe = NULL;
296
Ian Kent34ca9592006-03-27 01:14:54 -0800297 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /* Can this call block? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700300 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
301 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
302 &sbi->max_proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 printk("autofs: called with bogus options\n");
304 goto fail_dput;
305 }
306
David Howellsb650c852011-01-15 10:51:57 +0000307 if (autofs_type_trigger(sbi->type))
Ian Kentb5b80172011-01-14 18:46:03 +0000308 __managed_dentry_set_managed(root);
Ian Kent10584212011-01-14 18:45:58 +0000309
Ian Kent34ca9592006-03-27 01:14:54 -0800310 root_inode->i_fop = &autofs4_root_operations;
Ian Kente61da202011-01-14 18:46:14 +0000311 root_inode->i_op = &autofs4_dir_inode_operations;
Ian Kent34ca9592006-03-27 01:14:54 -0800312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800314 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
315 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 printk("autofs: kernel does not match daemon version "
317 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800318 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
320 goto fail_dput;
321 }
322
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800323 /* Establish highest kernel protocol version */
324 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
325 sbi->version = AUTOFS_MAX_PROTO_VERSION;
326 else
327 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
329
330 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
331 pipe = fget(pipefd);
332
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700333 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 printk("autofs: could not open pipe file descriptor\n");
335 goto fail_dput;
336 }
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700337 if (!pipe->f_op || !pipe->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 goto fail_fput;
339 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800340 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800341 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * Success! Install the root dentry now to indicate completion.
345 */
346 s->s_root = root;
347 return 0;
348
349 /*
350 * Failure ... clean up.
351 */
352fail_fput:
353 printk("autofs: pipe file descriptor does not contain proper ops\n");
354 fput(pipe);
355 /* fall through */
356fail_dput:
357 dput(root);
358 goto fail_free;
359fail_iput:
360 printk("autofs: get root dentry failed\n");
361 iput(root_inode);
Ian Kent34ca9592006-03-27 01:14:54 -0800362fail_ino:
363 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364fail_free:
365 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800366 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367fail_unlock:
368 return -EINVAL;
369}
370
371struct inode *autofs4_get_inode(struct super_block *sb,
372 struct autofs_info *inf)
373{
374 struct inode *inode = new_inode(sb);
375
376 if (inode == NULL)
377 return NULL;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 inode->i_mode = inf->mode;
380 if (sb->s_root) {
381 inode->i_uid = sb->s_root->d_inode->i_uid;
382 inode->i_gid = sb->s_root->d_inode->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400385 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (S_ISDIR(inf->mode)) {
388 inode->i_nlink = 2;
389 inode->i_op = &autofs4_dir_inode_operations;
390 inode->i_fop = &autofs4_dir_operations;
391 } else if (S_ISLNK(inf->mode)) {
392 inode->i_size = inf->size;
393 inode->i_op = &autofs4_symlink_inode_operations;
394 }
395
396 return inode;
397}