Commit | Line | Data |
---|---|---|
e9a7c2f1 IK |
1 | /* |
2 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | |
3 | * Copyright 2005-2006 Ian Kent <raven@themaw.net> | |
1da177e4 LT |
4 | * |
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. | |
e9a7c2f1 | 8 | */ |
1da177e4 LT |
9 | |
10 | #include <linux/kernel.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/file.h> | |
d7c4a5f1 | 13 | #include <linux/seq_file.h> |
1da177e4 LT |
14 | #include <linux/pagemap.h> |
15 | #include <linux/parser.h> | |
16 | #include <linux/bitops.h> | |
e18fa700 | 17 | #include <linux/magic.h> |
1da177e4 LT |
18 | #include "autofs_i.h" |
19 | #include <linux/module.h> | |
20 | ||
26e6c910 | 21 | struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi) |
1da177e4 | 22 | { |
e9a7c2f1 IK |
23 | struct autofs_info *ino; |
24 | ||
25 | ino = kzalloc(sizeof(*ino), GFP_KERNEL); | |
26e6c910 | 26 | if (ino) { |
25767378 IK |
27 | INIT_LIST_HEAD(&ino->active); |
28 | INIT_LIST_HEAD(&ino->expiring); | |
26e6c910 AV |
29 | ino->last_used = jiffies; |
30 | ino->sbi = sbi; | |
25767378 | 31 | } |
26e6c910 AV |
32 | return ino; |
33 | } | |
f50b6f86 | 34 | |
26e6c910 AV |
35 | void autofs4_clean_ino(struct autofs_info *ino) |
36 | { | |
45634cd8 EB |
37 | ino->uid = GLOBAL_ROOT_UID; |
38 | ino->gid = GLOBAL_ROOT_GID; | |
1da177e4 | 39 | ino->last_used = jiffies; |
1da177e4 LT |
40 | } |
41 | ||
42 | void autofs4_free_ino(struct autofs_info *ino) | |
43 | { | |
1da177e4 LT |
44 | kfree(ino); |
45 | } | |
46 | ||
6ce31523 | 47 | void autofs4_kill_sb(struct super_block *sb) |
1da177e4 LT |
48 | { |
49 | struct autofs_sb_info *sbi = autofs4_sbi(sb); | |
50 | ||
ba8df43c IK |
51 | /* |
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 | |
c949d4eb JK |
54 | * just call kill_anon_super when we are called from |
55 | * deactivate_super. | |
ba8df43c | 56 | */ |
6eaba35b SB |
57 | if (sbi) { |
58 | /* Free wait queues, close pipe */ | |
baa40671 | 59 | autofs4_catatonic_mode(sbi); |
6eaba35b SB |
60 | put_pid(sbi->oz_pgrp); |
61 | } | |
1da177e4 | 62 | |
8a78d593 | 63 | pr_debug("shutting down\n"); |
5b7e934d | 64 | kill_litter_super(sb); |
baa40671 AV |
65 | if (sbi) |
66 | kfree_rcu(sbi, rcu); | |
1da177e4 LT |
67 | } |
68 | ||
34c80b1d | 69 | static int autofs4_show_options(struct seq_file *m, struct dentry *root) |
d7c4a5f1 | 70 | { |
34c80b1d | 71 | struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb); |
2b0143b5 | 72 | struct inode *root_inode = d_inode(root->d_sb->s_root); |
d7c4a5f1 IK |
73 | |
74 | if (!sbi) | |
75 | return 0; | |
76 | ||
77 | seq_printf(m, ",fd=%d", sbi->pipefd); | |
45634cd8 EB |
78 | 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)); | |
6eaba35b | 84 | seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp)); |
d7c4a5f1 IK |
85 | 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 | ||
a92daf6b | 89 | if (autofs_type_offset(sbi->type)) |
34ca959c | 90 | seq_printf(m, ",offset"); |
a92daf6b | 91 | else if (autofs_type_direct(sbi->type)) |
34ca959c IK |
92 | seq_printf(m, ",direct"); |
93 | else | |
94 | seq_printf(m, ",indirect"); | |
c83aa55d SK |
95 | #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 | |
d7c4a5f1 IK |
101 | return 0; |
102 | } | |
103 | ||
292c5ee8 AV |
104 | static void autofs4_evict_inode(struct inode *inode) |
105 | { | |
dbd5768f | 106 | clear_inode(inode); |
292c5ee8 AV |
107 | kfree(inode->i_private); |
108 | } | |
109 | ||
ee9b6d61 | 110 | static const struct super_operations autofs4_sops = { |
1da177e4 | 111 | .statfs = simple_statfs, |
d7c4a5f1 | 112 | .show_options = autofs4_show_options, |
292c5ee8 | 113 | .evict_inode = autofs4_evict_inode, |
1da177e4 LT |
114 | }; |
115 | ||
34ca959c IK |
116 | enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto, |
117 | Opt_indirect, Opt_direct, Opt_offset}; | |
1da177e4 | 118 | |
a447c093 | 119 | static const match_table_t tokens = { |
1da177e4 LT |
120 | {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"}, | |
34ca959c IK |
126 | {Opt_indirect, "indirect"}, |
127 | {Opt_direct, "direct"}, | |
128 | {Opt_offset, "offset"}, | |
1da177e4 LT |
129 | {Opt_err, NULL} |
130 | }; | |
131 | ||
45634cd8 | 132 | static int parse_options(char *options, int *pipefd, kuid_t *uid, kgid_t *gid, |
6eaba35b SB |
133 | int *pgrp, bool *pgrp_set, unsigned int *type, |
134 | int *minproto, int *maxproto) | |
1da177e4 LT |
135 | { |
136 | char *p; | |
137 | substring_t args[MAX_OPT_ARGS]; | |
138 | int option; | |
139 | ||
0eb790e3 DH |
140 | *uid = current_uid(); |
141 | *gid = current_gid(); | |
1da177e4 LT |
142 | |
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; | |
e9a7c2f1 | 153 | |
1da177e4 LT |
154 | 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; | |
45634cd8 EB |
166 | *uid = make_kuid(current_user_ns(), option); |
167 | if (!uid_valid(*uid)) | |
168 | return 1; | |
1da177e4 LT |
169 | break; |
170 | case Opt_gid: | |
171 | if (match_int(args, &option)) | |
172 | return 1; | |
45634cd8 EB |
173 | *gid = make_kgid(current_user_ns(), option); |
174 | if (!gid_valid(*gid)) | |
175 | return 1; | |
1da177e4 LT |
176 | break; |
177 | case Opt_pgrp: | |
178 | if (match_int(args, &option)) | |
179 | return 1; | |
180 | *pgrp = option; | |
6eaba35b | 181 | *pgrp_set = true; |
1da177e4 LT |
182 | 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; | |
34ca959c | 193 | case Opt_indirect: |
a92daf6b | 194 | set_autofs_type_indirect(type); |
34ca959c IK |
195 | break; |
196 | case Opt_direct: | |
a92daf6b | 197 | set_autofs_type_direct(type); |
34ca959c IK |
198 | break; |
199 | case Opt_offset: | |
a92daf6b | 200 | set_autofs_type_offset(type); |
34ca959c | 201 | break; |
1da177e4 LT |
202 | default: |
203 | return 1; | |
204 | } | |
205 | } | |
206 | return (*pipefd < 0); | |
207 | } | |
208 | ||
1da177e4 LT |
209 | int autofs4_fill_super(struct super_block *s, void *data, int silent) |
210 | { | |
e9a7c2f1 IK |
211 | struct inode *root_inode; |
212 | struct dentry *root; | |
213 | struct file *pipe; | |
1da177e4 LT |
214 | int pipefd; |
215 | struct autofs_sb_info *sbi; | |
216 | struct autofs_info *ino; | |
571ff473 | 217 | int pgrp = 0; |
6eaba35b | 218 | bool pgrp_set = false; |
da29b754 | 219 | int ret = -EINVAL; |
1da177e4 | 220 | |
b63d50c4 | 221 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); |
d78e53c8 | 222 | if (!sbi) |
da29b754 | 223 | return -ENOMEM; |
8a78d593 | 224 | pr_debug("starting up, sbi = %p\n", sbi); |
1da177e4 | 225 | |
1da177e4 LT |
226 | s->s_fs_info = sbi; |
227 | sbi->magic = AUTOFS_SBI_MAGIC; | |
d7c4a5f1 | 228 | sbi->pipefd = -1; |
ba8df43c IK |
229 | sbi->pipe = NULL; |
230 | sbi->catatonic = 1; | |
1da177e4 | 231 | sbi->exp_timeout = 0; |
6eaba35b | 232 | sbi->oz_pgrp = NULL; |
1da177e4 LT |
233 | sbi->sb = s; |
234 | sbi->version = 0; | |
235 | sbi->sub_version = 0; | |
a92daf6b | 236 | set_autofs_type_indirect(&sbi->type); |
d7c4a5f1 IK |
237 | sbi->min_proto = 0; |
238 | sbi->max_proto = 0; | |
1d5599e3 | 239 | mutex_init(&sbi->wq_mutex); |
d668dc56 | 240 | mutex_init(&sbi->pipe_mutex); |
3a9720ce | 241 | spin_lock_init(&sbi->fs_lock); |
1da177e4 | 242 | sbi->queues = NULL; |
5f6f4f28 | 243 | spin_lock_init(&sbi->lookup_lock); |
25767378 | 244 | INIT_LIST_HEAD(&sbi->active_list); |
5f6f4f28 | 245 | INIT_LIST_HEAD(&sbi->expiring_list); |
1da177e4 LT |
246 | s->s_blocksize = 1024; |
247 | s->s_blocksize_bits = 10; | |
248 | s->s_magic = AUTOFS_SUPER_MAGIC; | |
249 | s->s_op = &autofs4_sops; | |
b650c858 | 250 | s->s_d_op = &autofs4_dentry_operations; |
1da177e4 LT |
251 | s->s_time_gran = 1; |
252 | ||
253 | /* | |
254 | * Get the root inode and dentry, but defer checking for errors. | |
255 | */ | |
26e6c910 | 256 | ino = autofs4_new_ino(sbi); |
da29b754 RX |
257 | if (!ino) { |
258 | ret = -ENOMEM; | |
1da177e4 | 259 | goto fail_free; |
da29b754 | 260 | } |
726a5e06 | 261 | root_inode = autofs4_get_inode(s, S_IFDIR | 0755); |
48fde701 | 262 | root = d_make_root(root_inode); |
1da177e4 | 263 | if (!root) |
48fde701 | 264 | goto fail_ino; |
34ca959c IK |
265 | pipe = NULL; |
266 | ||
34ca959c | 267 | root->d_fsdata = ino; |
1da177e4 LT |
268 | |
269 | /* Can this call block? */ | |
d78e53c8 | 270 | if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid, |
6eaba35b SB |
271 | &pgrp, &pgrp_set, &sbi->type, &sbi->min_proto, |
272 | &sbi->max_proto)) { | |
8a78d593 | 273 | pr_err("called with bogus options\n"); |
1da177e4 LT |
274 | goto fail_dput; |
275 | } | |
276 | ||
6eaba35b SB |
277 | if (pgrp_set) { |
278 | sbi->oz_pgrp = find_get_pid(pgrp); | |
279 | if (!sbi->oz_pgrp) { | |
8a78d593 | 280 | pr_err("could not find process group %d\n", |
6eaba35b SB |
281 | pgrp); |
282 | goto fail_dput; | |
283 | } | |
284 | } else { | |
285 | sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID); | |
286 | } | |
287 | ||
b650c858 | 288 | if (autofs_type_trigger(sbi->type)) |
b5b80177 | 289 | __managed_dentry_set_managed(root); |
10584211 | 290 | |
34ca959c | 291 | root_inode->i_fop = &autofs4_root_operations; |
e61da20a | 292 | root_inode->i_op = &autofs4_dir_inode_operations; |
34ca959c | 293 | |
1da177e4 | 294 | /* Couldn't this be tested earlier? */ |
d7c4a5f1 IK |
295 | if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION || |
296 | sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) { | |
8a78d593 IK |
297 | pr_err("kernel does not match daemon version " |
298 | "daemon (%d, %d) kernel (%d, %d)\n", | |
299 | sbi->min_proto, sbi->max_proto, | |
300 | AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION); | |
1da177e4 LT |
301 | goto fail_dput; |
302 | } | |
303 | ||
d7c4a5f1 IK |
304 | /* Establish highest kernel protocol version */ |
305 | if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION) | |
306 | sbi->version = AUTOFS_MAX_PROTO_VERSION; | |
307 | else | |
308 | sbi->version = sbi->max_proto; | |
1da177e4 LT |
309 | sbi->sub_version = AUTOFS_PROTO_SUBVERSION; |
310 | ||
8a78d593 | 311 | pr_debug("pipe fd = %d, pgrp = %u\n", pipefd, pid_nr(sbi->oz_pgrp)); |
1da177e4 | 312 | pipe = fget(pipefd); |
6eaba35b | 313 | |
d78e53c8 | 314 | if (!pipe) { |
8a78d593 | 315 | pr_err("could not open pipe file descriptor\n"); |
1da177e4 LT |
316 | goto fail_dput; |
317 | } | |
da29b754 RX |
318 | ret = autofs_prepare_pipe(pipe); |
319 | if (ret < 0) | |
1da177e4 LT |
320 | goto fail_fput; |
321 | sbi->pipe = pipe; | |
d7c4a5f1 | 322 | sbi->pipefd = pipefd; |
ba8df43c | 323 | sbi->catatonic = 0; |
1da177e4 LT |
324 | |
325 | /* | |
326 | * Success! Install the root dentry now to indicate completion. | |
327 | */ | |
328 | s->s_root = root; | |
329 | return 0; | |
0266725a | 330 | |
1da177e4 LT |
331 | /* |
332 | * Failure ... clean up. | |
333 | */ | |
334 | fail_fput: | |
8a78d593 | 335 | pr_err("pipe file descriptor does not contain proper ops\n"); |
1da177e4 LT |
336 | fput(pipe); |
337 | /* fall through */ | |
338 | fail_dput: | |
339 | dput(root); | |
340 | goto fail_free; | |
34ca959c IK |
341 | fail_ino: |
342 | kfree(ino); | |
1da177e4 | 343 | fail_free: |
6eaba35b | 344 | put_pid(sbi->oz_pgrp); |
1da177e4 | 345 | kfree(sbi); |
ba8df43c | 346 | s->s_fs_info = NULL; |
da29b754 | 347 | return ret; |
1da177e4 LT |
348 | } |
349 | ||
030a8ba4 | 350 | struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode) |
1da177e4 LT |
351 | { |
352 | struct inode *inode = new_inode(sb); | |
353 | ||
354 | if (inode == NULL) | |
355 | return NULL; | |
356 | ||
09f12c03 | 357 | inode->i_mode = mode; |
1da177e4 | 358 | if (sb->s_root) { |
2b0143b5 DH |
359 | inode->i_uid = d_inode(sb->s_root)->i_uid; |
360 | inode->i_gid = d_inode(sb->s_root)->i_gid; | |
1da177e4 | 361 | } |
1da177e4 | 362 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
85fe4025 | 363 | inode->i_ino = get_next_ino(); |
1da177e4 | 364 | |
09f12c03 | 365 | if (S_ISDIR(mode)) { |
bfe86848 | 366 | set_nlink(inode, 2); |
1da177e4 LT |
367 | inode->i_op = &autofs4_dir_inode_operations; |
368 | inode->i_fop = &autofs4_dir_operations; | |
09f12c03 | 369 | } else if (S_ISLNK(mode)) { |
1da177e4 LT |
370 | inode->i_op = &autofs4_symlink_inode_operations; |
371 | } | |
372 | ||
373 | return inode; | |
374 | } |