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