blob: b3f9477c974533fc8c7c4a20ef1356ba978d7ebf [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,
Al Viro09f12c02011-01-16 17:20:23 -050026 struct autofs_sb_info *sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
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;
Ian Kent25767372008-07-23 21:30:12 -070041 INIT_LIST_HEAD(&ino->active);
Ian Kent4f8427d2009-12-15 16:45:42 -080042 ino->active_count = 0;
Ian Kent25767372008-07-23 21:30:12 -070043 INIT_LIST_HEAD(&ino->expiring);
44 atomic_set(&ino->count, 0);
45 }
46
Ian Kentc0f54d32008-10-15 22:02:52 -070047 ino->uid = 0;
48 ino->gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 ino->last_used = jiffies;
50
51 ino->sbi = sbi;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return ino;
54}
55
56void autofs4_free_ino(struct autofs_info *ino)
57{
58 if (ino->dentry) {
59 ino->dentry->d_fsdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 ino->dentry = NULL;
61 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 kfree(ino);
63}
64
David Howells6ce31522006-10-11 01:22:15 -070065void autofs4_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 struct autofs_sb_info *sbi = autofs4_sbi(sb);
68
Ian Kentba8df432006-11-14 02:03:29 -080069 /*
70 * In the event of a failure in get_sb_nodev the superblock
71 * info is not present so nothing else has been setup, so
Jiri Kosinac949d4e2006-12-06 20:39:38 -080072 * just call kill_anon_super when we are called from
73 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -080074 */
75 if (!sbi)
Jiri Kosinac949d4e2006-12-06 20:39:38 -080076 goto out_kill_sb;
Ian Kentba8df432006-11-14 02:03:29 -080077
Ian Kent5a11d4d2008-07-23 21:30:17 -070078 /* Free wait queues, close pipe */
79 autofs4_catatonic_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Ian Kentf50b6f82007-02-20 13:58:10 -080081 sb->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 kfree(sbi);
83
Jiri Kosinac949d4e2006-12-06 20:39:38 -080084out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 DPRINTK("shutting down");
Al Viro5b7e9342010-01-24 00:28:52 -050086 kill_litter_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Ian Kentd7c4a5f2006-03-27 01:14:49 -080089static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
90{
91 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
Miklos Szerediaef97cb2008-02-08 04:21:37 -080092 struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
Ian Kentd7c4a5f2006-03-27 01:14:49 -080093
94 if (!sbi)
95 return 0;
96
97 seq_printf(m, ",fd=%d", sbi->pipefd);
Miklos Szerediaef97cb2008-02-08 04:21:37 -080098 if (root_inode->i_uid != 0)
99 seq_printf(m, ",uid=%u", root_inode->i_uid);
100 if (root_inode->i_gid != 0)
101 seq_printf(m, ",gid=%u", root_inode->i_gid);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800102 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
103 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
104 seq_printf(m, ",minproto=%d", sbi->min_proto);
105 seq_printf(m, ",maxproto=%d", sbi->max_proto);
106
Ian Kenta92daf62009-01-06 14:42:08 -0800107 if (autofs_type_offset(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -0800108 seq_printf(m, ",offset");
Ian Kenta92daf62009-01-06 14:42:08 -0800109 else if (autofs_type_direct(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -0800110 seq_printf(m, ",direct");
111 else
112 seq_printf(m, ",indirect");
113
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800114 return 0;
115}
116
Al Viro292c5ee2011-01-17 00:47:38 -0500117static void autofs4_evict_inode(struct inode *inode)
118{
119 end_writeback(inode);
120 kfree(inode->i_private);
121}
122
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800123static const struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800125 .show_options = autofs4_show_options,
Al Viro292c5ee2011-01-17 00:47:38 -0500126 .evict_inode = autofs4_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Ian Kent34ca9592006-03-27 01:14:54 -0800129enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
130 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Steven Whitehousea447c092008-10-13 10:46:57 +0100132static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 {Opt_fd, "fd=%u"},
134 {Opt_uid, "uid=%u"},
135 {Opt_gid, "gid=%u"},
136 {Opt_pgrp, "pgrp=%u"},
137 {Opt_minproto, "minproto=%u"},
138 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800139 {Opt_indirect, "indirect"},
140 {Opt_direct, "direct"},
141 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 {Opt_err, NULL}
143};
144
145static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700146 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 char *p;
149 substring_t args[MAX_OPT_ARGS];
150 int option;
151
David Howells0eb790e2008-11-14 10:38:46 +1100152 *uid = current_uid();
153 *gid = current_gid();
Pavel Emelianova47afb02007-10-18 23:39:46 -0700154 *pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 *minproto = AUTOFS_MIN_PROTO_VERSION;
157 *maxproto = AUTOFS_MAX_PROTO_VERSION;
158
159 *pipefd = -1;
160
161 if (!options)
162 return 1;
163
164 while ((p = strsep(&options, ",")) != NULL) {
165 int token;
166 if (!*p)
167 continue;
168
169 token = match_token(p, tokens, args);
170 switch (token) {
171 case Opt_fd:
172 if (match_int(args, pipefd))
173 return 1;
174 break;
175 case Opt_uid:
176 if (match_int(args, &option))
177 return 1;
178 *uid = option;
179 break;
180 case Opt_gid:
181 if (match_int(args, &option))
182 return 1;
183 *gid = option;
184 break;
185 case Opt_pgrp:
186 if (match_int(args, &option))
187 return 1;
188 *pgrp = option;
189 break;
190 case Opt_minproto:
191 if (match_int(args, &option))
192 return 1;
193 *minproto = option;
194 break;
195 case Opt_maxproto:
196 if (match_int(args, &option))
197 return 1;
198 *maxproto = option;
199 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800200 case Opt_indirect:
Ian Kenta92daf62009-01-06 14:42:08 -0800201 set_autofs_type_indirect(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800202 break;
203 case Opt_direct:
Ian Kenta92daf62009-01-06 14:42:08 -0800204 set_autofs_type_direct(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800205 break;
206 case Opt_offset:
Ian Kenta92daf62009-01-06 14:42:08 -0800207 set_autofs_type_offset(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 default:
210 return 1;
211 }
212 }
213 return (*pipefd < 0);
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216int autofs4_fill_super(struct super_block *s, void *data, int silent)
217{
218 struct inode * root_inode;
219 struct dentry * root;
220 struct file * pipe;
221 int pipefd;
222 struct autofs_sb_info *sbi;
223 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Mariusz Kozlowskib63d50c2007-10-16 23:26:44 -0700225 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700226 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto fail_unlock;
228 DPRINTK("starting up, sbi = %p",sbi);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 s->s_fs_info = sbi;
231 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800232 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800233 sbi->pipe = NULL;
234 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 sbi->exp_timeout = 0;
Pavel Emelianova47afb02007-10-18 23:39:46 -0700236 sbi->oz_pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 sbi->sb = s;
238 sbi->version = 0;
239 sbi->sub_version = 0;
Ian Kenta92daf62009-01-06 14:42:08 -0800240 set_autofs_type_indirect(&sbi->type);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800241 sbi->min_proto = 0;
242 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800243 mutex_init(&sbi->wq_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700244 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 sbi->queues = NULL;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700246 spin_lock_init(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700247 INIT_LIST_HEAD(&sbi->active_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700248 INIT_LIST_HEAD(&sbi->expiring_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 s->s_blocksize = 1024;
250 s->s_blocksize_bits = 10;
251 s->s_magic = AUTOFS_SUPER_MAGIC;
252 s->s_op = &autofs4_sops;
David Howellsb650c852011-01-15 10:51:57 +0000253 s->s_d_op = &autofs4_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 s->s_time_gran = 1;
255
256 /*
257 * Get the root inode and dentry, but defer checking for errors.
258 */
Al Viro09f12c02011-01-16 17:20:23 -0500259 ino = autofs4_init_ino(NULL, sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!ino)
261 goto fail_free;
Al Viro726a5e02011-01-16 17:43:52 -0500262 root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!root_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800264 goto fail_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 root = d_alloc_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (!root)
268 goto fail_iput;
Ian Kent34ca9592006-03-27 01:14:54 -0800269 pipe = NULL;
270
Ian Kent34ca9592006-03-27 01:14:54 -0800271 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /* Can this call block? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700274 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
275 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
276 &sbi->max_proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 printk("autofs: called with bogus options\n");
278 goto fail_dput;
279 }
280
David Howellsb650c852011-01-15 10:51:57 +0000281 if (autofs_type_trigger(sbi->type))
Ian Kentb5b80172011-01-14 18:46:03 +0000282 __managed_dentry_set_managed(root);
Ian Kent10584212011-01-14 18:45:58 +0000283
Ian Kent34ca9592006-03-27 01:14:54 -0800284 root_inode->i_fop = &autofs4_root_operations;
Ian Kente61da202011-01-14 18:46:14 +0000285 root_inode->i_op = &autofs4_dir_inode_operations;
Ian Kent34ca9592006-03-27 01:14:54 -0800286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800288 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
289 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 printk("autofs: kernel does not match daemon version "
291 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800292 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
294 goto fail_dput;
295 }
296
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800297 /* Establish highest kernel protocol version */
298 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
299 sbi->version = AUTOFS_MAX_PROTO_VERSION;
300 else
301 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
303
304 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
305 pipe = fget(pipefd);
306
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700307 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 printk("autofs: could not open pipe file descriptor\n");
309 goto fail_dput;
310 }
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700311 if (!pipe->f_op || !pipe->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto fail_fput;
313 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800314 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800315 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * Success! Install the root dentry now to indicate completion.
319 */
320 s->s_root = root;
321 return 0;
322
323 /*
324 * Failure ... clean up.
325 */
326fail_fput:
327 printk("autofs: pipe file descriptor does not contain proper ops\n");
328 fput(pipe);
329 /* fall through */
330fail_dput:
331 dput(root);
332 goto fail_free;
333fail_iput:
334 printk("autofs: get root dentry failed\n");
335 iput(root_inode);
Ian Kent34ca9592006-03-27 01:14:54 -0800336fail_ino:
337 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338fail_free:
339 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800340 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341fail_unlock:
342 return -EINVAL;
343}
344
Al Viro726a5e02011-01-16 17:43:52 -0500345struct inode *autofs4_get_inode(struct super_block *sb, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct inode *inode = new_inode(sb);
348
349 if (inode == NULL)
350 return NULL;
351
Al Viro09f12c02011-01-16 17:20:23 -0500352 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (sb->s_root) {
354 inode->i_uid = sb->s_root->d_inode->i_uid;
355 inode->i_gid = sb->s_root->d_inode->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400358 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Al Viro09f12c02011-01-16 17:20:23 -0500360 if (S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 inode->i_nlink = 2;
362 inode->i_op = &autofs4_dir_inode_operations;
363 inode->i_fop = &autofs4_dir_operations;
Al Viro09f12c02011-01-16 17:20:23 -0500364 } else if (S_ISLNK(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 inode->i_op = &autofs4_symlink_inode_operations;
366 }
367
368 return inode;
369}