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