]>
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 IK |
58 | */ |
59 | if (!sbi) | |
c949d4eb | 60 | goto out_kill_sb; |
ba8df43c | 61 | |
5a11d4d0 IK |
62 | /* Free wait queues, close pipe */ |
63 | autofs4_catatonic_mode(sbi); | |
1da177e4 | 64 | |
f50b6f86 | 65 | sb->s_fs_info = NULL; |
1da177e4 LT |
66 | kfree(sbi); |
67 | ||
c949d4eb | 68 | out_kill_sb: |
1da177e4 | 69 | DPRINTK("shutting down"); |
5b7e934d | 70 | kill_litter_super(sb); |
1da177e4 LT |
71 | } |
72 | ||
34c80b1d | 73 | static int autofs4_show_options(struct seq_file *m, struct dentry *root) |
d7c4a5f1 | 74 | { |
34c80b1d AV |
75 | struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb); |
76 | struct inode *root_inode = root->d_sb->s_root->d_inode; | |
d7c4a5f1 IK |
77 | |
78 | if (!sbi) | |
79 | return 0; | |
80 | ||
81 | seq_printf(m, ",fd=%d", sbi->pipefd); | |
45634cd8 EB |
82 | 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)); | |
d7c4a5f1 IK |
88 | 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 | ||
a92daf6b | 93 | if (autofs_type_offset(sbi->type)) |
34ca959c | 94 | seq_printf(m, ",offset"); |
a92daf6b | 95 | else if (autofs_type_direct(sbi->type)) |
34ca959c IK |
96 | seq_printf(m, ",direct"); |
97 | else | |
98 | seq_printf(m, ",indirect"); | |
99 | ||
d7c4a5f1 IK |
100 | return 0; |
101 | } | |
102 | ||
292c5ee8 AV |
103 | static void autofs4_evict_inode(struct inode *inode) |
104 | { | |
dbd5768f | 105 | clear_inode(inode); |
292c5ee8 AV |
106 | kfree(inode->i_private); |
107 | } | |
108 | ||
ee9b6d61 | 109 | static const struct super_operations autofs4_sops = { |
1da177e4 | 110 | .statfs = simple_statfs, |
d7c4a5f1 | 111 | .show_options = autofs4_show_options, |
292c5ee8 | 112 | .evict_inode = autofs4_evict_inode, |
1da177e4 LT |
113 | }; |
114 | ||
34ca959c IK |
115 | enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto, |
116 | Opt_indirect, Opt_direct, Opt_offset}; | |
1da177e4 | 117 | |
a447c093 | 118 | static const match_table_t tokens = { |
1da177e4 LT |
119 | {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"}, | |
34ca959c IK |
125 | {Opt_indirect, "indirect"}, |
126 | {Opt_direct, "direct"}, | |
127 | {Opt_offset, "offset"}, | |
1da177e4 LT |
128 | {Opt_err, NULL} |
129 | }; | |
130 | ||
45634cd8 | 131 | static int parse_options(char *options, int *pipefd, kuid_t *uid, kgid_t *gid, |
d78e53c8 | 132 | pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto) |
1da177e4 LT |
133 | { |
134 | char *p; | |
135 | substring_t args[MAX_OPT_ARGS]; | |
136 | int option; | |
137 | ||
0eb790e3 DH |
138 | *uid = current_uid(); |
139 | *gid = current_gid(); | |
a47afb0f | 140 | *pgrp = task_pgrp_nr(current); |
1da177e4 LT |
141 | |
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; | |
45634cd8 EB |
164 | *uid = make_kuid(current_user_ns(), option); |
165 | if (!uid_valid(*uid)) | |
166 | return 1; | |
1da177e4 LT |
167 | break; |
168 | case Opt_gid: | |
169 | if (match_int(args, &option)) | |
170 | return 1; | |
45634cd8 EB |
171 | *gid = make_kgid(current_user_ns(), option); |
172 | if (!gid_valid(*gid)) | |
173 | return 1; | |
1da177e4 LT |
174 | 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; | |
34ca959c | 190 | case Opt_indirect: |
a92daf6b | 191 | set_autofs_type_indirect(type); |
34ca959c IK |
192 | break; |
193 | case Opt_direct: | |
a92daf6b | 194 | set_autofs_type_direct(type); |
34ca959c IK |
195 | break; |
196 | case Opt_offset: | |
a92daf6b | 197 | set_autofs_type_offset(type); |
34ca959c | 198 | break; |
1da177e4 LT |
199 | default: |
200 | return 1; | |
201 | } | |
202 | } | |
203 | return (*pipefd < 0); | |
204 | } | |
205 | ||
1da177e4 LT |
206 | int 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; | |
1da177e4 | 214 | |
b63d50c4 | 215 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); |
d78e53c8 | 216 | if (!sbi) |
1da177e4 LT |
217 | goto fail_unlock; |
218 | DPRINTK("starting up, sbi = %p",sbi); | |
219 | ||
1da177e4 LT |
220 | s->s_fs_info = sbi; |
221 | sbi->magic = AUTOFS_SBI_MAGIC; | |
d7c4a5f1 | 222 | sbi->pipefd = -1; |
ba8df43c IK |
223 | sbi->pipe = NULL; |
224 | sbi->catatonic = 1; | |
1da177e4 | 225 | sbi->exp_timeout = 0; |
a47afb0f | 226 | sbi->oz_pgrp = task_pgrp_nr(current); |
1da177e4 LT |
227 | sbi->sb = s; |
228 | sbi->version = 0; | |
229 | sbi->sub_version = 0; | |
a92daf6b | 230 | set_autofs_type_indirect(&sbi->type); |
d7c4a5f1 IK |
231 | sbi->min_proto = 0; |
232 | sbi->max_proto = 0; | |
1d5599e3 | 233 | mutex_init(&sbi->wq_mutex); |
d668dc56 | 234 | mutex_init(&sbi->pipe_mutex); |
3a9720ce | 235 | spin_lock_init(&sbi->fs_lock); |
1da177e4 | 236 | sbi->queues = NULL; |
5f6f4f28 | 237 | spin_lock_init(&sbi->lookup_lock); |
25767378 | 238 | INIT_LIST_HEAD(&sbi->active_list); |
5f6f4f28 | 239 | INIT_LIST_HEAD(&sbi->expiring_list); |
1da177e4 LT |
240 | s->s_blocksize = 1024; |
241 | s->s_blocksize_bits = 10; | |
242 | s->s_magic = AUTOFS_SUPER_MAGIC; | |
243 | s->s_op = &autofs4_sops; | |
b650c858 | 244 | s->s_d_op = &autofs4_dentry_operations; |
1da177e4 LT |
245 | s->s_time_gran = 1; |
246 | ||
247 | /* | |
248 | * Get the root inode and dentry, but defer checking for errors. | |
249 | */ | |
26e6c910 | 250 | ino = autofs4_new_ino(sbi); |
1da177e4 LT |
251 | if (!ino) |
252 | goto fail_free; | |
726a5e06 | 253 | root_inode = autofs4_get_inode(s, S_IFDIR | 0755); |
48fde701 | 254 | root = d_make_root(root_inode); |
1da177e4 | 255 | if (!root) |
48fde701 | 256 | goto fail_ino; |
34ca959c IK |
257 | pipe = NULL; |
258 | ||
34ca959c | 259 | root->d_fsdata = ino; |
1da177e4 LT |
260 | |
261 | /* Can this call block? */ | |
d78e53c8 SB |
262 | 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)) { | |
1da177e4 LT |
265 | printk("autofs: called with bogus options\n"); |
266 | goto fail_dput; | |
267 | } | |
268 | ||
b650c858 | 269 | if (autofs_type_trigger(sbi->type)) |
b5b80177 | 270 | __managed_dentry_set_managed(root); |
10584211 | 271 | |
34ca959c | 272 | root_inode->i_fop = &autofs4_root_operations; |
e61da20a | 273 | root_inode->i_op = &autofs4_dir_inode_operations; |
34ca959c | 274 | |
1da177e4 | 275 | /* Couldn't this be tested earlier? */ |
d7c4a5f1 IK |
276 | if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION || |
277 | sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) { | |
1da177e4 LT |
278 | printk("autofs: kernel does not match daemon version " |
279 | "daemon (%d, %d) kernel (%d, %d)\n", | |
d7c4a5f1 | 280 | sbi->min_proto, sbi->max_proto, |
1da177e4 LT |
281 | AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION); |
282 | goto fail_dput; | |
283 | } | |
284 | ||
d7c4a5f1 IK |
285 | /* 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; | |
1da177e4 LT |
290 | sbi->sub_version = AUTOFS_PROTO_SUBVERSION; |
291 | ||
292 | DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp); | |
293 | pipe = fget(pipefd); | |
294 | ||
d78e53c8 | 295 | if (!pipe) { |
1da177e4 LT |
296 | printk("autofs: could not open pipe file descriptor\n"); |
297 | goto fail_dput; | |
298 | } | |
64f371bc | 299 | if (autofs_prepare_pipe(pipe) < 0) |
1da177e4 LT |
300 | goto fail_fput; |
301 | sbi->pipe = pipe; | |
d7c4a5f1 | 302 | sbi->pipefd = pipefd; |
ba8df43c | 303 | sbi->catatonic = 0; |
1da177e4 LT |
304 | |
305 | /* | |
306 | * 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 | */ | |
314 | fail_fput: | |
315 | printk("autofs: pipe file descriptor does not contain proper ops\n"); | |
316 | fput(pipe); | |
317 | /* fall through */ | |
318 | fail_dput: | |
319 | dput(root); | |
320 | goto fail_free; | |
34ca959c IK |
321 | fail_ino: |
322 | kfree(ino); | |
1da177e4 LT |
323 | fail_free: |
324 | kfree(sbi); | |
ba8df43c | 325 | s->s_fs_info = NULL; |
1da177e4 LT |
326 | fail_unlock: |
327 | return -EINVAL; | |
328 | } | |
329 | ||
030a8ba4 | 330 | struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode) |
1da177e4 LT |
331 | { |
332 | struct inode *inode = new_inode(sb); | |
333 | ||
334 | if (inode == NULL) | |
335 | return NULL; | |
336 | ||
09f12c03 | 337 | inode->i_mode = mode; |
1da177e4 LT |
338 | 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; | |
1da177e4 | 341 | } |
1da177e4 | 342 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
85fe4025 | 343 | inode->i_ino = get_next_ino(); |
1da177e4 | 344 | |
09f12c03 | 345 | if (S_ISDIR(mode)) { |
bfe86848 | 346 | set_nlink(inode, 2); |
1da177e4 LT |
347 | inode->i_op = &autofs4_dir_inode_operations; |
348 | inode->i_fop = &autofs4_dir_operations; | |
09f12c03 | 349 | } else if (S_ISLNK(mode)) { |
1da177e4 LT |
350 | inode->i_op = &autofs4_symlink_inode_operations; |
351 | } | |
352 | ||
353 | return inode; | |
354 | } |