Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* -*- c -*- --------------------------------------------------------------- * |
2 | * | |
3 | * linux/fs/autofs/inode.c | |
4 | * | |
5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | |
6 | * | |
7 | * This file is part of the Linux kernel and is made available under | |
8 | * the terms of the GNU General Public License, version 2, or at your | |
9 | * option, any later version, incorporated herein by reference. | |
10 | * | |
11 | * ------------------------------------------------------------------------- */ | |
12 | ||
13 | #include <linux/kernel.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/file.h> | |
16 | #include <linux/pagemap.h> | |
17 | #include <linux/parser.h> | |
18 | #include <linux/bitops.h> | |
104e49fc | 19 | #include <linux/smp_lock.h> |
1da177e4 LT |
20 | #include "autofs_i.h" |
21 | #include <linux/module.h> | |
22 | ||
23 | static void ino_lnkfree(struct autofs_info *ino) | |
24 | { | |
f99d49ad JJ |
25 | kfree(ino->u.symlink); |
26 | ino->u.symlink = NULL; | |
1da177e4 LT |
27 | } |
28 | ||
29 | struct autofs_info *autofs4_init_ino(struct autofs_info *ino, | |
30 | struct autofs_sb_info *sbi, mode_t mode) | |
31 | { | |
32 | int reinit = 1; | |
33 | ||
34 | if (ino == NULL) { | |
35 | reinit = 0; | |
36 | ino = kmalloc(sizeof(*ino), GFP_KERNEL); | |
37 | } | |
38 | ||
39 | if (ino == NULL) | |
40 | return NULL; | |
41 | ||
42 | ino->flags = 0; | |
43 | ino->mode = mode; | |
44 | ino->inode = NULL; | |
45 | ino->dentry = NULL; | |
46 | ino->size = 0; | |
47 | ||
48 | ino->last_used = jiffies; | |
1aff3c8b | 49 | atomic_set(&ino->count, 0); |
1da177e4 LT |
50 | |
51 | ino->sbi = sbi; | |
52 | ||
53 | if (reinit && ino->free) | |
54 | (ino->free)(ino); | |
55 | ||
56 | memset(&ino->u, 0, sizeof(ino->u)); | |
57 | ||
58 | ino->free = NULL; | |
59 | ||
60 | if (S_ISLNK(mode)) | |
61 | ino->free = ino_lnkfree; | |
62 | ||
63 | return ino; | |
64 | } | |
65 | ||
66 | void autofs4_free_ino(struct autofs_info *ino) | |
67 | { | |
1aff3c8b IK |
68 | struct autofs_info *p_ino; |
69 | ||
1da177e4 LT |
70 | if (ino->dentry) { |
71 | ino->dentry->d_fsdata = NULL; | |
1aff3c8b IK |
72 | if (ino->dentry->d_inode) { |
73 | struct dentry *parent = ino->dentry->d_parent; | |
74 | if (atomic_dec_and_test(&ino->count)) { | |
75 | p_ino = autofs4_dentry_ino(parent); | |
76 | if (p_ino && parent != ino->dentry) | |
77 | atomic_dec(&p_ino->count); | |
78 | } | |
1da177e4 | 79 | dput(ino->dentry); |
1aff3c8b | 80 | } |
1da177e4 LT |
81 | ino->dentry = NULL; |
82 | } | |
83 | if (ino->free) | |
84 | (ino->free)(ino); | |
85 | kfree(ino); | |
86 | } | |
87 | ||
104e49fc IK |
88 | /* |
89 | * Deal with the infamous "Busy inodes after umount ..." message. | |
90 | * | |
91 | * Clean up the dentry tree. This happens with autofs if the user | |
92 | * space program goes away due to a SIGKILL, SIGSEGV etc. | |
93 | */ | |
94 | static void autofs4_force_release(struct autofs_sb_info *sbi) | |
95 | { | |
96 | struct dentry *this_parent = sbi->root; | |
97 | struct list_head *next; | |
98 | ||
99 | spin_lock(&dcache_lock); | |
100 | repeat: | |
101 | next = this_parent->d_subdirs.next; | |
102 | resume: | |
103 | while (next != &this_parent->d_subdirs) { | |
5160ee6f | 104 | struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child); |
104e49fc IK |
105 | |
106 | /* Negative dentry - don`t care */ | |
107 | if (!simple_positive(dentry)) { | |
108 | next = next->next; | |
109 | continue; | |
110 | } | |
111 | ||
112 | if (!list_empty(&dentry->d_subdirs)) { | |
113 | this_parent = dentry; | |
114 | goto repeat; | |
115 | } | |
116 | ||
117 | next = next->next; | |
118 | spin_unlock(&dcache_lock); | |
119 | ||
120 | DPRINTK("dentry %p %.*s", | |
121 | dentry, (int)dentry->d_name.len, dentry->d_name.name); | |
122 | ||
123 | dput(dentry); | |
124 | spin_lock(&dcache_lock); | |
125 | } | |
126 | ||
127 | if (this_parent != sbi->root) { | |
128 | struct dentry *dentry = this_parent; | |
129 | ||
5160ee6f | 130 | next = this_parent->d_u.d_child.next; |
104e49fc IK |
131 | this_parent = this_parent->d_parent; |
132 | spin_unlock(&dcache_lock); | |
133 | DPRINTK("parent dentry %p %.*s", | |
134 | dentry, (int)dentry->d_name.len, dentry->d_name.name); | |
135 | dput(dentry); | |
136 | spin_lock(&dcache_lock); | |
137 | goto resume; | |
138 | } | |
139 | spin_unlock(&dcache_lock); | |
140 | ||
141 | dput(sbi->root); | |
142 | sbi->root = NULL; | |
143 | shrink_dcache_sb(sbi->sb); | |
144 | ||
145 | return; | |
146 | } | |
147 | ||
1da177e4 LT |
148 | static void autofs4_put_super(struct super_block *sb) |
149 | { | |
150 | struct autofs_sb_info *sbi = autofs4_sbi(sb); | |
151 | ||
152 | sb->s_fs_info = NULL; | |
153 | ||
154 | if ( !sbi->catatonic ) | |
155 | autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */ | |
156 | ||
104e49fc IK |
157 | /* Clean up and release dangling references */ |
158 | if (sbi) | |
159 | autofs4_force_release(sbi); | |
160 | ||
1da177e4 LT |
161 | kfree(sbi); |
162 | ||
163 | DPRINTK("shutting down"); | |
164 | } | |
165 | ||
166 | static struct super_operations autofs4_sops = { | |
167 | .put_super = autofs4_put_super, | |
168 | .statfs = simple_statfs, | |
169 | }; | |
170 | ||
171 | enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto}; | |
172 | ||
173 | static match_table_t tokens = { | |
174 | {Opt_fd, "fd=%u"}, | |
175 | {Opt_uid, "uid=%u"}, | |
176 | {Opt_gid, "gid=%u"}, | |
177 | {Opt_pgrp, "pgrp=%u"}, | |
178 | {Opt_minproto, "minproto=%u"}, | |
179 | {Opt_maxproto, "maxproto=%u"}, | |
180 | {Opt_err, NULL} | |
181 | }; | |
182 | ||
183 | static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid, | |
184 | pid_t *pgrp, int *minproto, int *maxproto) | |
185 | { | |
186 | char *p; | |
187 | substring_t args[MAX_OPT_ARGS]; | |
188 | int option; | |
189 | ||
190 | *uid = current->uid; | |
191 | *gid = current->gid; | |
192 | *pgrp = process_group(current); | |
193 | ||
194 | *minproto = AUTOFS_MIN_PROTO_VERSION; | |
195 | *maxproto = AUTOFS_MAX_PROTO_VERSION; | |
196 | ||
197 | *pipefd = -1; | |
198 | ||
199 | if (!options) | |
200 | return 1; | |
201 | ||
202 | while ((p = strsep(&options, ",")) != NULL) { | |
203 | int token; | |
204 | if (!*p) | |
205 | continue; | |
206 | ||
207 | token = match_token(p, tokens, args); | |
208 | switch (token) { | |
209 | case Opt_fd: | |
210 | if (match_int(args, pipefd)) | |
211 | return 1; | |
212 | break; | |
213 | case Opt_uid: | |
214 | if (match_int(args, &option)) | |
215 | return 1; | |
216 | *uid = option; | |
217 | break; | |
218 | case Opt_gid: | |
219 | if (match_int(args, &option)) | |
220 | return 1; | |
221 | *gid = option; | |
222 | break; | |
223 | case Opt_pgrp: | |
224 | if (match_int(args, &option)) | |
225 | return 1; | |
226 | *pgrp = option; | |
227 | break; | |
228 | case Opt_minproto: | |
229 | if (match_int(args, &option)) | |
230 | return 1; | |
231 | *minproto = option; | |
232 | break; | |
233 | case Opt_maxproto: | |
234 | if (match_int(args, &option)) | |
235 | return 1; | |
236 | *maxproto = option; | |
237 | break; | |
238 | default: | |
239 | return 1; | |
240 | } | |
241 | } | |
242 | return (*pipefd < 0); | |
243 | } | |
244 | ||
245 | static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi) | |
246 | { | |
247 | struct autofs_info *ino; | |
248 | ||
249 | ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755); | |
250 | if (!ino) | |
251 | return NULL; | |
252 | ||
253 | return ino; | |
254 | } | |
255 | ||
256 | int autofs4_fill_super(struct super_block *s, void *data, int silent) | |
257 | { | |
258 | struct inode * root_inode; | |
259 | struct dentry * root; | |
260 | struct file * pipe; | |
261 | int pipefd; | |
262 | struct autofs_sb_info *sbi; | |
263 | struct autofs_info *ino; | |
264 | int minproto, maxproto; | |
265 | ||
266 | sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL); | |
267 | if ( !sbi ) | |
268 | goto fail_unlock; | |
269 | DPRINTK("starting up, sbi = %p",sbi); | |
270 | ||
271 | memset(sbi, 0, sizeof(*sbi)); | |
272 | ||
273 | s->s_fs_info = sbi; | |
274 | sbi->magic = AUTOFS_SBI_MAGIC; | |
104e49fc | 275 | sbi->root = NULL; |
1da177e4 LT |
276 | sbi->catatonic = 0; |
277 | sbi->exp_timeout = 0; | |
278 | sbi->oz_pgrp = process_group(current); | |
279 | sbi->sb = s; | |
280 | sbi->version = 0; | |
281 | sbi->sub_version = 0; | |
1d5599e3 | 282 | mutex_init(&sbi->wq_mutex); |
3a9720ce | 283 | spin_lock_init(&sbi->fs_lock); |
1da177e4 LT |
284 | sbi->queues = NULL; |
285 | s->s_blocksize = 1024; | |
286 | s->s_blocksize_bits = 10; | |
287 | s->s_magic = AUTOFS_SUPER_MAGIC; | |
288 | s->s_op = &autofs4_sops; | |
289 | s->s_time_gran = 1; | |
290 | ||
291 | /* | |
292 | * Get the root inode and dentry, but defer checking for errors. | |
293 | */ | |
294 | ino = autofs4_mkroot(sbi); | |
295 | if (!ino) | |
296 | goto fail_free; | |
297 | root_inode = autofs4_get_inode(s, ino); | |
298 | kfree(ino); | |
299 | if (!root_inode) | |
300 | goto fail_free; | |
301 | ||
302 | root_inode->i_op = &autofs4_root_inode_operations; | |
303 | root_inode->i_fop = &autofs4_root_operations; | |
304 | root = d_alloc_root(root_inode); | |
305 | pipe = NULL; | |
306 | ||
307 | if (!root) | |
308 | goto fail_iput; | |
309 | ||
310 | /* Can this call block? */ | |
311 | if (parse_options(data, &pipefd, | |
312 | &root_inode->i_uid, &root_inode->i_gid, | |
313 | &sbi->oz_pgrp, | |
314 | &minproto, &maxproto)) { | |
315 | printk("autofs: called with bogus options\n"); | |
316 | goto fail_dput; | |
317 | } | |
318 | ||
319 | /* Couldn't this be tested earlier? */ | |
320 | if (maxproto < AUTOFS_MIN_PROTO_VERSION || | |
321 | minproto > AUTOFS_MAX_PROTO_VERSION) { | |
322 | printk("autofs: kernel does not match daemon version " | |
323 | "daemon (%d, %d) kernel (%d, %d)\n", | |
324 | minproto, maxproto, | |
325 | AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION); | |
326 | goto fail_dput; | |
327 | } | |
328 | ||
329 | sbi->version = maxproto > AUTOFS_MAX_PROTO_VERSION ? AUTOFS_MAX_PROTO_VERSION : maxproto; | |
330 | sbi->sub_version = AUTOFS_PROTO_SUBVERSION; | |
331 | ||
332 | DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp); | |
333 | pipe = fget(pipefd); | |
334 | ||
335 | if ( !pipe ) { | |
336 | printk("autofs: could not open pipe file descriptor\n"); | |
337 | goto fail_dput; | |
338 | } | |
339 | if ( !pipe->f_op || !pipe->f_op->write ) | |
340 | goto fail_fput; | |
341 | sbi->pipe = pipe; | |
342 | ||
104e49fc IK |
343 | /* |
344 | * Take a reference to the root dentry so we get a chance to | |
345 | * clean up the dentry tree on umount. | |
346 | * See autofs4_force_release. | |
347 | */ | |
348 | sbi->root = dget(root); | |
349 | ||
1da177e4 LT |
350 | /* |
351 | * Success! Install the root dentry now to indicate completion. | |
352 | */ | |
353 | s->s_root = root; | |
354 | return 0; | |
355 | ||
356 | /* | |
357 | * Failure ... clean up. | |
358 | */ | |
359 | fail_fput: | |
360 | printk("autofs: pipe file descriptor does not contain proper ops\n"); | |
361 | fput(pipe); | |
362 | /* fall through */ | |
363 | fail_dput: | |
364 | dput(root); | |
365 | goto fail_free; | |
366 | fail_iput: | |
367 | printk("autofs: get root dentry failed\n"); | |
368 | iput(root_inode); | |
369 | fail_free: | |
370 | kfree(sbi); | |
371 | fail_unlock: | |
372 | return -EINVAL; | |
373 | } | |
374 | ||
375 | struct inode *autofs4_get_inode(struct super_block *sb, | |
376 | struct autofs_info *inf) | |
377 | { | |
378 | struct inode *inode = new_inode(sb); | |
379 | ||
380 | if (inode == NULL) | |
381 | return NULL; | |
382 | ||
383 | inf->inode = inode; | |
384 | inode->i_mode = inf->mode; | |
385 | if (sb->s_root) { | |
386 | inode->i_uid = sb->s_root->d_inode->i_uid; | |
387 | inode->i_gid = sb->s_root->d_inode->i_gid; | |
388 | } else { | |
389 | inode->i_uid = 0; | |
390 | inode->i_gid = 0; | |
391 | } | |
392 | inode->i_blksize = PAGE_CACHE_SIZE; | |
393 | inode->i_blocks = 0; | |
394 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
395 | ||
396 | if (S_ISDIR(inf->mode)) { | |
397 | inode->i_nlink = 2; | |
398 | inode->i_op = &autofs4_dir_inode_operations; | |
399 | inode->i_fop = &autofs4_dir_operations; | |
400 | } else if (S_ISLNK(inf->mode)) { | |
401 | inode->i_size = inf->size; | |
402 | inode->i_op = &autofs4_symlink_inode_operations; | |
403 | } | |
404 | ||
405 | return inode; | |
406 | } |