blob: 438b5bf675b6a2a65651b4b06a397865d9d94af1 [file] [log] [blame]
Ian Kente9a7c2f2016-03-15 14:58:25 -07001/*
2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 * Copyright 2005-2006 Ian Kent <raven@themaw.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is part of the Linux kernel and is made available under
6 * the terms of the GNU General Public License, version 2, or at your
7 * option, any later version, incorporated herein by reference.
Ian Kente9a7c2f2016-03-15 14:58:25 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/file.h>
Ian Kentd7c4a5f2006-03-27 01:14:49 -080013#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/pagemap.h>
15#include <linux/parser.h>
16#include <linux/bitops.h>
Jeff Garzike18fa702006-09-24 11:13:19 -040017#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "autofs_i.h"
19#include <linux/module.h>
20
Al Viro26e6c912011-01-16 18:43:40 -050021struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Ian Kente9a7c2f2016-03-15 14:58:25 -070023 struct autofs_info *ino;
24
25 ino = kzalloc(sizeof(*ino), GFP_KERNEL);
Al Viro26e6c912011-01-16 18:43:40 -050026 if (ino) {
Ian Kent25767372008-07-23 21:30:12 -070027 INIT_LIST_HEAD(&ino->active);
28 INIT_LIST_HEAD(&ino->expiring);
Al Viro26e6c912011-01-16 18:43:40 -050029 ino->last_used = jiffies;
30 ino->sbi = sbi;
Ian Kent25767372008-07-23 21:30:12 -070031 }
Al Viro26e6c912011-01-16 18:43:40 -050032 return ino;
33}
Ian Kent25767372008-07-23 21:30:12 -070034
Al Viro26e6c912011-01-16 18:43:40 -050035void autofs4_clean_ino(struct autofs_info *ino)
36{
Eric W. Biederman45634cd2012-02-07 16:18:52 -080037 ino->uid = GLOBAL_ROOT_UID;
38 ino->gid = GLOBAL_ROOT_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 ino->last_used = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42void autofs4_free_ino(struct autofs_info *ino)
43{
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 kfree(ino);
45}
46
David Howells6ce31522006-10-11 01:22:15 -070047void autofs4_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 struct autofs_sb_info *sbi = autofs4_sbi(sb);
50
Ian Kentba8df432006-11-14 02:03:29 -080051 /*
52 * In the event of a failure in get_sb_nodev the superblock
53 * info is not present so nothing else has been setup, so
Jiri Kosinac949d4e2006-12-06 20:39:38 -080054 * just call kill_anon_super when we are called from
55 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -080056 */
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -080057 if (sbi) {
58 /* Free wait queues, close pipe */
Al Virobaa40672013-10-03 12:46:44 -040059 autofs4_catatonic_mode(sbi);
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -080060 put_pid(sbi->oz_pgrp);
61 }
Ian Kentba8df432006-11-14 02:03:29 -080062
Ian Kent8a78d592016-03-15 14:58:45 -070063 pr_debug("shutting down\n");
Al Viro5b7e9342010-01-24 00:28:52 -050064 kill_litter_super(sb);
Al Virobaa40672013-10-03 12:46:44 -040065 if (sbi)
66 kfree_rcu(sbi, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Al Viro34c80b12011-12-08 21:32:45 -050069static int autofs4_show_options(struct seq_file *m, struct dentry *root)
Ian Kentd7c4a5f2006-03-27 01:14:49 -080070{
Al Viro34c80b12011-12-08 21:32:45 -050071 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +000072 struct inode *root_inode = d_inode(root->d_sb->s_root);
Ian Kentd7c4a5f2006-03-27 01:14:49 -080073
74 if (!sbi)
75 return 0;
76
77 seq_printf(m, ",fd=%d", sbi->pipefd);
Eric W. Biederman45634cd2012-02-07 16:18:52 -080078 if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
79 seq_printf(m, ",uid=%u",
80 from_kuid_munged(&init_user_ns, root_inode->i_uid));
81 if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
82 seq_printf(m, ",gid=%u",
83 from_kgid_munged(&init_user_ns, root_inode->i_gid));
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -080084 seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp));
Ian Kentd7c4a5f2006-03-27 01:14:49 -080085 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
86 seq_printf(m, ",minproto=%d", sbi->min_proto);
87 seq_printf(m, ",maxproto=%d", sbi->max_proto);
88
Ian Kenta92daf62009-01-06 14:42:08 -080089 if (autofs_type_offset(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -080090 seq_printf(m, ",offset");
Ian Kenta92daf62009-01-06 14:42:08 -080091 else if (autofs_type_direct(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -080092 seq_printf(m, ",direct");
93 else
94 seq_printf(m, ",indirect");
Stanislav Kinsburskiyc83aa552016-03-15 14:58:22 -070095#ifdef CONFIG_CHECKPOINT_RESTORE
96 if (sbi->pipe)
97 seq_printf(m, ",pipe_ino=%ld", sbi->pipe->f_inode->i_ino);
98 else
99 seq_printf(m, ",pipe_ino=-1");
100#endif
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800101 return 0;
102}
103
Al Viro292c5ee2011-01-17 00:47:38 -0500104static void autofs4_evict_inode(struct inode *inode)
105{
Jan Karadbd57682012-05-03 14:48:02 +0200106 clear_inode(inode);
Al Viro292c5ee2011-01-17 00:47:38 -0500107 kfree(inode->i_private);
108}
109
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800110static const struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800112 .show_options = autofs4_show_options,
Al Viro292c5ee2011-01-17 00:47:38 -0500113 .evict_inode = autofs4_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Ian Kent34ca9592006-03-27 01:14:54 -0800116enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
117 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Steven Whitehousea447c092008-10-13 10:46:57 +0100119static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 {Opt_fd, "fd=%u"},
121 {Opt_uid, "uid=%u"},
122 {Opt_gid, "gid=%u"},
123 {Opt_pgrp, "pgrp=%u"},
124 {Opt_minproto, "minproto=%u"},
125 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800126 {Opt_indirect, "indirect"},
127 {Opt_direct, "direct"},
128 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 {Opt_err, NULL}
130};
131
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800132static int parse_options(char *options, int *pipefd, kuid_t *uid, kgid_t *gid,
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800133 int *pgrp, bool *pgrp_set, unsigned int *type,
134 int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 char *p;
137 substring_t args[MAX_OPT_ARGS];
138 int option;
139
David Howells0eb790e2008-11-14 10:38:46 +1100140 *uid = current_uid();
141 *gid = current_gid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 *minproto = AUTOFS_MIN_PROTO_VERSION;
144 *maxproto = AUTOFS_MAX_PROTO_VERSION;
145
146 *pipefd = -1;
147
148 if (!options)
149 return 1;
150
151 while ((p = strsep(&options, ",")) != NULL) {
152 int token;
Ian Kente9a7c2f2016-03-15 14:58:25 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!*p)
155 continue;
156
157 token = match_token(p, tokens, args);
158 switch (token) {
159 case Opt_fd:
160 if (match_int(args, pipefd))
161 return 1;
162 break;
163 case Opt_uid:
164 if (match_int(args, &option))
165 return 1;
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800166 *uid = make_kuid(current_user_ns(), option);
167 if (!uid_valid(*uid))
168 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 case Opt_gid:
171 if (match_int(args, &option))
172 return 1;
Eric W. Biederman45634cd2012-02-07 16:18:52 -0800173 *gid = make_kgid(current_user_ns(), option);
174 if (!gid_valid(*gid))
175 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177 case Opt_pgrp:
178 if (match_int(args, &option))
179 return 1;
180 *pgrp = option;
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800181 *pgrp_set = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 break;
183 case Opt_minproto:
184 if (match_int(args, &option))
185 return 1;
186 *minproto = option;
187 break;
188 case Opt_maxproto:
189 if (match_int(args, &option))
190 return 1;
191 *maxproto = option;
192 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800193 case Opt_indirect:
Ian Kenta92daf62009-01-06 14:42:08 -0800194 set_autofs_type_indirect(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800195 break;
196 case Opt_direct:
Ian Kenta92daf62009-01-06 14:42:08 -0800197 set_autofs_type_direct(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800198 break;
199 case Opt_offset:
Ian Kenta92daf62009-01-06 14:42:08 -0800200 set_autofs_type_offset(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 default:
203 return 1;
204 }
205 }
206 return (*pipefd < 0);
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209int autofs4_fill_super(struct super_block *s, void *data, int silent)
210{
Ian Kente9a7c2f2016-03-15 14:58:25 -0700211 struct inode *root_inode;
212 struct dentry *root;
213 struct file *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int pipefd;
215 struct autofs_sb_info *sbi;
216 struct autofs_info *ino;
Ian Kent571ff472014-07-02 15:22:35 -0700217 int pgrp = 0;
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800218 bool pgrp_set = false;
Rui Xiangda29b752014-01-23 15:54:59 -0800219 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Mariusz Kozlowskib63d50c2007-10-16 23:26:44 -0700221 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700222 if (!sbi)
Rui Xiangda29b752014-01-23 15:54:59 -0800223 return -ENOMEM;
Ian Kent8a78d592016-03-15 14:58:45 -0700224 pr_debug("starting up, sbi = %p\n", sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 s->s_fs_info = sbi;
227 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800228 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800229 sbi->pipe = NULL;
230 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 sbi->exp_timeout = 0;
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800232 sbi->oz_pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 sbi->sb = s;
234 sbi->version = 0;
235 sbi->sub_version = 0;
Ian Kenta92daf62009-01-06 14:42:08 -0800236 set_autofs_type_indirect(&sbi->type);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800237 sbi->min_proto = 0;
238 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800239 mutex_init(&sbi->wq_mutex);
Al Virod668dc52012-01-10 22:35:38 -0500240 mutex_init(&sbi->pipe_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700241 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 sbi->queues = NULL;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700243 spin_lock_init(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700244 INIT_LIST_HEAD(&sbi->active_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700245 INIT_LIST_HEAD(&sbi->expiring_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 s->s_blocksize = 1024;
247 s->s_blocksize_bits = 10;
248 s->s_magic = AUTOFS_SUPER_MAGIC;
249 s->s_op = &autofs4_sops;
David Howellsb650c852011-01-15 10:51:57 +0000250 s->s_d_op = &autofs4_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 s->s_time_gran = 1;
252
253 /*
254 * Get the root inode and dentry, but defer checking for errors.
255 */
Al Viro26e6c912011-01-16 18:43:40 -0500256 ino = autofs4_new_ino(sbi);
Rui Xiangda29b752014-01-23 15:54:59 -0800257 if (!ino) {
258 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto fail_free;
Rui Xiangda29b752014-01-23 15:54:59 -0800260 }
Al Viro726a5e02011-01-16 17:43:52 -0500261 root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
Al Viro48fde702012-01-08 22:15:13 -0500262 root = d_make_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!root)
Al Viro48fde702012-01-08 22:15:13 -0500264 goto fail_ino;
Ian Kent34ca9592006-03-27 01:14:54 -0800265 pipe = NULL;
266
Ian Kent34ca9592006-03-27 01:14:54 -0800267 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* Can this call block? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700270 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800271 &pgrp, &pgrp_set, &sbi->type, &sbi->min_proto,
272 &sbi->max_proto)) {
Ian Kent8a78d592016-03-15 14:58:45 -0700273 pr_err("called with bogus options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 goto fail_dput;
275 }
276
Tomohiro Kusumi749800e2016-10-11 13:52:31 -0700277 /* Test versions first */
278 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
279 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
280 pr_err("kernel does not match daemon version "
281 "daemon (%d, %d) kernel (%d, %d)\n",
282 sbi->min_proto, sbi->max_proto,
283 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
284 goto fail_dput;
285 }
286
287 /* Establish highest kernel protocol version */
288 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
289 sbi->version = AUTOFS_MAX_PROTO_VERSION;
290 else
291 sbi->version = sbi->max_proto;
292 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
293
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800294 if (pgrp_set) {
295 sbi->oz_pgrp = find_get_pid(pgrp);
296 if (!sbi->oz_pgrp) {
Ian Kent8a78d592016-03-15 14:58:45 -0700297 pr_err("could not find process group %d\n",
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800298 pgrp);
299 goto fail_dput;
300 }
301 } else {
302 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
303 }
304
David Howellsb650c852011-01-15 10:51:57 +0000305 if (autofs_type_trigger(sbi->type))
Ian Kentb5b80172011-01-14 18:46:03 +0000306 __managed_dentry_set_managed(root);
Ian Kent10584212011-01-14 18:45:58 +0000307
Ian Kent34ca9592006-03-27 01:14:54 -0800308 root_inode->i_fop = &autofs4_root_operations;
Ian Kente61da202011-01-14 18:46:14 +0000309 root_inode->i_op = &autofs4_dir_inode_operations;
Ian Kent34ca9592006-03-27 01:14:54 -0800310
Ian Kent8a78d592016-03-15 14:58:45 -0700311 pr_debug("pipe fd = %d, pgrp = %u\n", pipefd, pid_nr(sbi->oz_pgrp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 pipe = fget(pipefd);
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800313
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700314 if (!pipe) {
Ian Kent8a78d592016-03-15 14:58:45 -0700315 pr_err("could not open pipe file descriptor\n");
Ian Kent1973a122016-10-11 13:52:33 -0700316 goto fail_put_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Rui Xiangda29b752014-01-23 15:54:59 -0800318 ret = autofs_prepare_pipe(pipe);
319 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto fail_fput;
321 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800322 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800323 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * Success! Install the root dentry now to indicate completion.
327 */
328 s->s_root = root;
329 return 0;
Ian Kent02667252016-03-15 14:58:36 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
332 * Failure ... clean up.
333 */
334fail_fput:
Ian Kent8a78d592016-03-15 14:58:45 -0700335 pr_err("pipe file descriptor does not contain proper ops\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 fput(pipe);
Ian Kent1973a122016-10-11 13:52:33 -0700337fail_put_pid:
338 put_pid(sbi->oz_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339fail_dput:
340 dput(root);
341 goto fail_free;
Ian Kent34ca9592006-03-27 01:14:54 -0800342fail_ino:
Tomohiro Kusumica552592016-10-11 13:52:42 -0700343 autofs4_free_ino(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344fail_free:
345 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800346 s->s_fs_info = NULL;
Rui Xiangda29b752014-01-23 15:54:59 -0800347 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Al Viro030a8ba2011-07-26 03:29:03 -0400350struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct inode *inode = new_inode(sb);
353
354 if (inode == NULL)
355 return NULL;
356
Al Viro09f12c02011-01-16 17:20:23 -0500357 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (sb->s_root) {
David Howells2b0143b2015-03-17 22:25:59 +0000359 inode->i_uid = d_inode(sb->s_root)->i_uid;
360 inode->i_gid = d_inode(sb->s_root)->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Deepa Dinamani078cd822016-09-14 07:48:04 -0700362 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400363 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Al Viro09f12c02011-01-16 17:20:23 -0500365 if (S_ISDIR(mode)) {
Miklos Szeredibfe86842011-10-28 14:13:29 +0200366 set_nlink(inode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 inode->i_op = &autofs4_dir_inode_operations;
368 inode->i_fop = &autofs4_dir_operations;
Al Viro09f12c02011-01-16 17:20:23 -0500369 } else if (S_ISLNK(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 inode->i_op = &autofs4_symlink_inode_operations;
Tomohiro Kusumi97537b32016-10-11 13:52:36 -0700371 } else
372 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 return inode;
375}