blob: b104726e2d0a7dbdb998e3daa574ec5e0326138c [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
Al Viro26e6c912011-01-16 18:43:40 -050025struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Al Viro26e6c912011-01-16 18:43:40 -050027 struct autofs_info *ino = kzalloc(sizeof(*ino), GFP_KERNEL);
28 if (ino) {
Ian Kent25767372008-07-23 21:30:12 -070029 INIT_LIST_HEAD(&ino->active);
30 INIT_LIST_HEAD(&ino->expiring);
Al Viro26e6c912011-01-16 18:43:40 -050031 ino->last_used = jiffies;
32 ino->sbi = sbi;
Ian Kent25767372008-07-23 21:30:12 -070033 }
Al Viro26e6c912011-01-16 18:43:40 -050034 return ino;
35}
Ian Kent25767372008-07-23 21:30:12 -070036
Al Viro26e6c912011-01-16 18:43:40 -050037void autofs4_clean_ino(struct autofs_info *ino)
38{
Eric W. Biederman45634cd2012-02-07 16:18:52 -080039 ino->uid = GLOBAL_ROOT_UID;
40 ino->gid = GLOBAL_ROOT_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 ino->last_used = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
44void autofs4_free_ino(struct autofs_info *ino)
45{
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 kfree(ino);
47}
48
David Howells6ce31522006-10-11 01:22:15 -070049void autofs4_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 struct autofs_sb_info *sbi = autofs4_sbi(sb);
52
Ian Kentba8df432006-11-14 02:03:29 -080053 /*
54 * In the event of a failure in get_sb_nodev the superblock
55 * info is not present so nothing else has been setup, so
Jiri Kosinac949d4e2006-12-06 20:39:38 -080056 * just call kill_anon_super when we are called from
57 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -080058 */
59 if (!sbi)
Jiri Kosinac949d4e2006-12-06 20:39:38 -080060 goto out_kill_sb;
Ian Kentba8df432006-11-14 02:03:29 -080061
Ian Kent5a11d4d2008-07-23 21:30:17 -070062 /* Free wait queues, close pipe */
63 autofs4_catatonic_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Ian Kentf50b6f82007-02-20 13:58:10 -080065 sb->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 kfree(sbi);
67
Jiri Kosinac949d4e2006-12-06 20:39:38 -080068out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 DPRINTK("shutting down");
Al Viro5b7e9342010-01-24 00:28:52 -050070 kill_litter_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Al Viro34c80b12011-12-08 21:32:45 -050073static int autofs4_show_options(struct seq_file *m, struct dentry *root)
Ian Kentd7c4a5f2006-03-27 01:14:49 -080074{
Al Viro34c80b12011-12-08 21:32:45 -050075 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
76 struct inode *root_inode = root->d_sb->s_root->d_inode;
Ian Kentd7c4a5f2006-03-27 01:14:49 -080077
78 if (!sbi)
79 return 0;
80
81 seq_printf(m, ",fd=%d", sbi->pipefd);
Eric W. Biederman45634cd2012-02-07 16:18:52 -080082 if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
83 seq_printf(m, ",uid=%u",
84 from_kuid_munged(&init_user_ns, root_inode->i_uid));
85 if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
86 seq_printf(m, ",gid=%u",
87 from_kgid_munged(&init_user_ns, root_inode->i_gid));
Ian Kentd7c4a5f2006-03-27 01:14:49 -080088 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
89 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
90 seq_printf(m, ",minproto=%d", sbi->min_proto);
91 seq_printf(m, ",maxproto=%d", sbi->max_proto);
92
Ian Kenta92daf62009-01-06 14:42:08 -080093 if (autofs_type_offset(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -080094 seq_printf(m, ",offset");
Ian Kenta92daf62009-01-06 14:42:08 -080095 else if (autofs_type_direct(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -080096 seq_printf(m, ",direct");
97 else
98 seq_printf(m, ",indirect");
99
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800100 return 0;
101}
102
Al Viro292c5ee2011-01-17 00:47:38 -0500103static void autofs4_evict_inode(struct inode *inode)
104{
Jan Karadbd57682012-05-03 14:48:02 +0200105 clear_inode(inode);
Al Viro292c5ee2011-01-17 00:47:38 -0500106 kfree(inode->i_private);
107}
108
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800109static const struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800111 .show_options = autofs4_show_options,
Al Viro292c5ee2011-01-17 00:47:38 -0500112 .evict_inode = autofs4_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Ian Kent34ca9592006-03-27 01:14:54 -0800115enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
116 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Steven Whitehousea447c092008-10-13 10:46:57 +0100118static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 {Opt_fd, "fd=%u"},
120 {Opt_uid, "uid=%u"},
121 {Opt_gid, "gid=%u"},
122 {Opt_pgrp, "pgrp=%u"},
123 {Opt_minproto, "minproto=%u"},
124 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800125 {Opt_indirect, "indirect"},
126 {Opt_direct, "direct"},
127 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 {Opt_err, NULL}
129};
130
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800131static int parse_options(char *options, int *pipefd, kuid_t *uid, kgid_t *gid,
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700132 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 char *p;
135 substring_t args[MAX_OPT_ARGS];
136 int option;
137
David Howells0eb790e2008-11-14 10:38:46 +1100138 *uid = current_uid();
139 *gid = current_gid();
Pavel Emelianova47afb02007-10-18 23:39:46 -0700140 *pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 *minproto = AUTOFS_MIN_PROTO_VERSION;
143 *maxproto = AUTOFS_MAX_PROTO_VERSION;
144
145 *pipefd = -1;
146
147 if (!options)
148 return 1;
149
150 while ((p = strsep(&options, ",")) != NULL) {
151 int token;
152 if (!*p)
153 continue;
154
155 token = match_token(p, tokens, args);
156 switch (token) {
157 case Opt_fd:
158 if (match_int(args, pipefd))
159 return 1;
160 break;
161 case Opt_uid:
162 if (match_int(args, &option))
163 return 1;
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800164 *uid = make_kuid(current_user_ns(), option);
165 if (!uid_valid(*uid))
166 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168 case Opt_gid:
169 if (match_int(args, &option))
170 return 1;
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800171 *gid = make_kgid(current_user_ns(), option);
172 if (!gid_valid(*gid))
173 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
175 case Opt_pgrp:
176 if (match_int(args, &option))
177 return 1;
178 *pgrp = option;
179 break;
180 case Opt_minproto:
181 if (match_int(args, &option))
182 return 1;
183 *minproto = option;
184 break;
185 case Opt_maxproto:
186 if (match_int(args, &option))
187 return 1;
188 *maxproto = option;
189 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800190 case Opt_indirect:
Ian Kenta92daf62009-01-06 14:42:08 -0800191 set_autofs_type_indirect(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800192 break;
193 case Opt_direct:
Ian Kenta92daf62009-01-06 14:42:08 -0800194 set_autofs_type_direct(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800195 break;
196 case Opt_offset:
Ian Kenta92daf62009-01-06 14:42:08 -0800197 set_autofs_type_offset(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 default:
200 return 1;
201 }
202 }
203 return (*pipefd < 0);
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206int autofs4_fill_super(struct super_block *s, void *data, int silent)
207{
208 struct inode * root_inode;
209 struct dentry * root;
210 struct file * pipe;
211 int pipefd;
212 struct autofs_sb_info *sbi;
213 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Mariusz Kozlowskib63d50c2007-10-16 23:26:44 -0700215 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700216 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto fail_unlock;
218 DPRINTK("starting up, sbi = %p",sbi);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 s->s_fs_info = sbi;
221 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800222 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800223 sbi->pipe = NULL;
224 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 sbi->exp_timeout = 0;
Pavel Emelianova47afb02007-10-18 23:39:46 -0700226 sbi->oz_pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 sbi->sb = s;
228 sbi->version = 0;
229 sbi->sub_version = 0;
Ian Kenta92daf62009-01-06 14:42:08 -0800230 set_autofs_type_indirect(&sbi->type);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800231 sbi->min_proto = 0;
232 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800233 mutex_init(&sbi->wq_mutex);
Al Virod668dc52012-01-10 22:35:38 -0500234 mutex_init(&sbi->pipe_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700235 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 sbi->queues = NULL;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700237 spin_lock_init(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700238 INIT_LIST_HEAD(&sbi->active_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700239 INIT_LIST_HEAD(&sbi->expiring_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 s->s_blocksize = 1024;
241 s->s_blocksize_bits = 10;
242 s->s_magic = AUTOFS_SUPER_MAGIC;
243 s->s_op = &autofs4_sops;
David Howellsb650c852011-01-15 10:51:57 +0000244 s->s_d_op = &autofs4_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 s->s_time_gran = 1;
246
247 /*
248 * Get the root inode and dentry, but defer checking for errors.
249 */
Al Viro26e6c912011-01-16 18:43:40 -0500250 ino = autofs4_new_ino(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!ino)
252 goto fail_free;
Al Viro726a5e02011-01-16 17:43:52 -0500253 root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
Al Viro48fde702012-01-08 22:15:13 -0500254 root = d_make_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!root)
Al Viro48fde702012-01-08 22:15:13 -0500256 goto fail_ino;
Ian Kent34ca9592006-03-27 01:14:54 -0800257 pipe = NULL;
258
Ian Kent34ca9592006-03-27 01:14:54 -0800259 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* Can this call block? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700262 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
263 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
264 &sbi->max_proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 printk("autofs: called with bogus options\n");
266 goto fail_dput;
267 }
268
David Howellsb650c852011-01-15 10:51:57 +0000269 if (autofs_type_trigger(sbi->type))
Ian Kentb5b80172011-01-14 18:46:03 +0000270 __managed_dentry_set_managed(root);
Ian Kent10584212011-01-14 18:45:58 +0000271
Ian Kent34ca9592006-03-27 01:14:54 -0800272 root_inode->i_fop = &autofs4_root_operations;
Ian Kente61da202011-01-14 18:46:14 +0000273 root_inode->i_op = &autofs4_dir_inode_operations;
Ian Kent34ca9592006-03-27 01:14:54 -0800274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800276 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
277 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 printk("autofs: kernel does not match daemon version "
279 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800280 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
282 goto fail_dput;
283 }
284
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800285 /* Establish highest kernel protocol version */
286 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
287 sbi->version = AUTOFS_MAX_PROTO_VERSION;
288 else
289 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
291
292 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
293 pipe = fget(pipefd);
294
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700295 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 printk("autofs: could not open pipe file descriptor\n");
297 goto fail_dput;
298 }
Linus Torvalds64f371b2012-04-29 13:30:08 -0700299 if (autofs_prepare_pipe(pipe) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 goto fail_fput;
301 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800302 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800303 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * Success! Install the root dentry now to indicate completion.
307 */
308 s->s_root = root;
309 return 0;
310
311 /*
312 * Failure ... clean up.
313 */
314fail_fput:
315 printk("autofs: pipe file descriptor does not contain proper ops\n");
316 fput(pipe);
317 /* fall through */
318fail_dput:
319 dput(root);
320 goto fail_free;
Ian Kent34ca9592006-03-27 01:14:54 -0800321fail_ino:
322 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323fail_free:
324 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800325 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326fail_unlock:
327 return -EINVAL;
328}
329
Al Viro030a8ba2011-07-26 03:29:03 -0400330struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct inode *inode = new_inode(sb);
333
334 if (inode == NULL)
335 return NULL;
336
Al Viro09f12c02011-01-16 17:20:23 -0500337 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (sb->s_root) {
339 inode->i_uid = sb->s_root->d_inode->i_uid;
340 inode->i_gid = sb->s_root->d_inode->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400343 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Al Viro09f12c02011-01-16 17:20:23 -0500345 if (S_ISDIR(mode)) {
Miklos Szeredibfe86842011-10-28 14:13:29 +0200346 set_nlink(inode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 inode->i_op = &autofs4_dir_inode_operations;
348 inode->i_fop = &autofs4_dir_operations;
Al Viro09f12c02011-01-16 17:20:23 -0500349 } else if (S_ISLNK(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 inode->i_op = &autofs4_symlink_inode_operations;
351 }
352
353 return inode;
354}