[SCSI] 2.6.13.3; add Pioneer DRM-624x to drivers/scsi/scsi_devinfo.c
[linux-2.6/libata-dev.git] / fs / autofs4 / inode.c
blob0a3c05d101679ea0dd24d2794a9a66f64ab5ba10
1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/inode.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
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.
11 * ------------------------------------------------------------------------- */
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>
19 #include <linux/smp_lock.h>
20 #include "autofs_i.h"
21 #include <linux/module.h>
23 static void ino_lnkfree(struct autofs_info *ino)
25 if (ino->u.symlink) {
26 kfree(ino->u.symlink);
27 ino->u.symlink = NULL;
31 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
32 struct autofs_sb_info *sbi, mode_t mode)
34 int reinit = 1;
36 if (ino == NULL) {
37 reinit = 0;
38 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
41 if (ino == NULL)
42 return NULL;
44 ino->flags = 0;
45 ino->mode = mode;
46 ino->inode = NULL;
47 ino->dentry = NULL;
48 ino->size = 0;
50 ino->last_used = jiffies;
52 ino->sbi = sbi;
54 if (reinit && ino->free)
55 (ino->free)(ino);
57 memset(&ino->u, 0, sizeof(ino->u));
59 ino->free = NULL;
61 if (S_ISLNK(mode))
62 ino->free = ino_lnkfree;
64 return ino;
67 void autofs4_free_ino(struct autofs_info *ino)
69 if (ino->dentry) {
70 ino->dentry->d_fsdata = NULL;
71 if (ino->dentry->d_inode)
72 dput(ino->dentry);
73 ino->dentry = NULL;
75 if (ino->free)
76 (ino->free)(ino);
77 kfree(ino);
81 * Deal with the infamous "Busy inodes after umount ..." message.
83 * Clean up the dentry tree. This happens with autofs if the user
84 * space program goes away due to a SIGKILL, SIGSEGV etc.
86 static void autofs4_force_release(struct autofs_sb_info *sbi)
88 struct dentry *this_parent = sbi->root;
89 struct list_head *next;
91 spin_lock(&dcache_lock);
92 repeat:
93 next = this_parent->d_subdirs.next;
94 resume:
95 while (next != &this_parent->d_subdirs) {
96 struct dentry *dentry = list_entry(next, struct dentry, d_child);
98 /* Negative dentry - don`t care */
99 if (!simple_positive(dentry)) {
100 next = next->next;
101 continue;
104 if (!list_empty(&dentry->d_subdirs)) {
105 this_parent = dentry;
106 goto repeat;
109 next = next->next;
110 spin_unlock(&dcache_lock);
112 DPRINTK("dentry %p %.*s",
113 dentry, (int)dentry->d_name.len, dentry->d_name.name);
115 dput(dentry);
116 spin_lock(&dcache_lock);
119 if (this_parent != sbi->root) {
120 struct dentry *dentry = this_parent;
122 next = this_parent->d_child.next;
123 this_parent = this_parent->d_parent;
124 spin_unlock(&dcache_lock);
125 DPRINTK("parent dentry %p %.*s",
126 dentry, (int)dentry->d_name.len, dentry->d_name.name);
127 dput(dentry);
128 spin_lock(&dcache_lock);
129 goto resume;
131 spin_unlock(&dcache_lock);
133 dput(sbi->root);
134 sbi->root = NULL;
135 shrink_dcache_sb(sbi->sb);
137 return;
140 static void autofs4_put_super(struct super_block *sb)
142 struct autofs_sb_info *sbi = autofs4_sbi(sb);
144 sb->s_fs_info = NULL;
146 if ( !sbi->catatonic )
147 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
149 /* Clean up and release dangling references */
150 if (sbi)
151 autofs4_force_release(sbi);
153 kfree(sbi);
155 DPRINTK("shutting down");
158 static struct super_operations autofs4_sops = {
159 .put_super = autofs4_put_super,
160 .statfs = simple_statfs,
163 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
165 static match_table_t tokens = {
166 {Opt_fd, "fd=%u"},
167 {Opt_uid, "uid=%u"},
168 {Opt_gid, "gid=%u"},
169 {Opt_pgrp, "pgrp=%u"},
170 {Opt_minproto, "minproto=%u"},
171 {Opt_maxproto, "maxproto=%u"},
172 {Opt_err, NULL}
175 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
176 pid_t *pgrp, int *minproto, int *maxproto)
178 char *p;
179 substring_t args[MAX_OPT_ARGS];
180 int option;
182 *uid = current->uid;
183 *gid = current->gid;
184 *pgrp = process_group(current);
186 *minproto = AUTOFS_MIN_PROTO_VERSION;
187 *maxproto = AUTOFS_MAX_PROTO_VERSION;
189 *pipefd = -1;
191 if (!options)
192 return 1;
194 while ((p = strsep(&options, ",")) != NULL) {
195 int token;
196 if (!*p)
197 continue;
199 token = match_token(p, tokens, args);
200 switch (token) {
201 case Opt_fd:
202 if (match_int(args, pipefd))
203 return 1;
204 break;
205 case Opt_uid:
206 if (match_int(args, &option))
207 return 1;
208 *uid = option;
209 break;
210 case Opt_gid:
211 if (match_int(args, &option))
212 return 1;
213 *gid = option;
214 break;
215 case Opt_pgrp:
216 if (match_int(args, &option))
217 return 1;
218 *pgrp = option;
219 break;
220 case Opt_minproto:
221 if (match_int(args, &option))
222 return 1;
223 *minproto = option;
224 break;
225 case Opt_maxproto:
226 if (match_int(args, &option))
227 return 1;
228 *maxproto = option;
229 break;
230 default:
231 return 1;
234 return (*pipefd < 0);
237 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
239 struct autofs_info *ino;
241 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
242 if (!ino)
243 return NULL;
245 return ino;
248 int autofs4_fill_super(struct super_block *s, void *data, int silent)
250 struct inode * root_inode;
251 struct dentry * root;
252 struct file * pipe;
253 int pipefd;
254 struct autofs_sb_info *sbi;
255 struct autofs_info *ino;
256 int minproto, maxproto;
258 sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
259 if ( !sbi )
260 goto fail_unlock;
261 DPRINTK("starting up, sbi = %p",sbi);
263 memset(sbi, 0, sizeof(*sbi));
265 s->s_fs_info = sbi;
266 sbi->magic = AUTOFS_SBI_MAGIC;
267 sbi->root = NULL;
268 sbi->catatonic = 0;
269 sbi->exp_timeout = 0;
270 sbi->oz_pgrp = process_group(current);
271 sbi->sb = s;
272 sbi->version = 0;
273 sbi->sub_version = 0;
274 init_MUTEX(&sbi->wq_sem);
275 spin_lock_init(&sbi->fs_lock);
276 sbi->queues = NULL;
277 s->s_blocksize = 1024;
278 s->s_blocksize_bits = 10;
279 s->s_magic = AUTOFS_SUPER_MAGIC;
280 s->s_op = &autofs4_sops;
281 s->s_time_gran = 1;
284 * Get the root inode and dentry, but defer checking for errors.
286 ino = autofs4_mkroot(sbi);
287 if (!ino)
288 goto fail_free;
289 root_inode = autofs4_get_inode(s, ino);
290 kfree(ino);
291 if (!root_inode)
292 goto fail_free;
294 root_inode->i_op = &autofs4_root_inode_operations;
295 root_inode->i_fop = &autofs4_root_operations;
296 root = d_alloc_root(root_inode);
297 pipe = NULL;
299 if (!root)
300 goto fail_iput;
302 /* Can this call block? */
303 if (parse_options(data, &pipefd,
304 &root_inode->i_uid, &root_inode->i_gid,
305 &sbi->oz_pgrp,
306 &minproto, &maxproto)) {
307 printk("autofs: called with bogus options\n");
308 goto fail_dput;
311 /* Couldn't this be tested earlier? */
312 if (maxproto < AUTOFS_MIN_PROTO_VERSION ||
313 minproto > AUTOFS_MAX_PROTO_VERSION) {
314 printk("autofs: kernel does not match daemon version "
315 "daemon (%d, %d) kernel (%d, %d)\n",
316 minproto, maxproto,
317 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
318 goto fail_dput;
321 sbi->version = maxproto > AUTOFS_MAX_PROTO_VERSION ? AUTOFS_MAX_PROTO_VERSION : maxproto;
322 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
324 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
325 pipe = fget(pipefd);
327 if ( !pipe ) {
328 printk("autofs: could not open pipe file descriptor\n");
329 goto fail_dput;
331 if ( !pipe->f_op || !pipe->f_op->write )
332 goto fail_fput;
333 sbi->pipe = pipe;
336 * Take a reference to the root dentry so we get a chance to
337 * clean up the dentry tree on umount.
338 * See autofs4_force_release.
340 sbi->root = dget(root);
343 * Success! Install the root dentry now to indicate completion.
345 s->s_root = root;
346 return 0;
349 * Failure ... clean up.
351 fail_fput:
352 printk("autofs: pipe file descriptor does not contain proper ops\n");
353 fput(pipe);
354 /* fall through */
355 fail_dput:
356 dput(root);
357 goto fail_free;
358 fail_iput:
359 printk("autofs: get root dentry failed\n");
360 iput(root_inode);
361 fail_free:
362 kfree(sbi);
363 fail_unlock:
364 return -EINVAL;
367 struct inode *autofs4_get_inode(struct super_block *sb,
368 struct autofs_info *inf)
370 struct inode *inode = new_inode(sb);
372 if (inode == NULL)
373 return NULL;
375 inf->inode = inode;
376 inode->i_mode = inf->mode;
377 if (sb->s_root) {
378 inode->i_uid = sb->s_root->d_inode->i_uid;
379 inode->i_gid = sb->s_root->d_inode->i_gid;
380 } else {
381 inode->i_uid = 0;
382 inode->i_gid = 0;
384 inode->i_blksize = PAGE_CACHE_SIZE;
385 inode->i_blocks = 0;
386 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
388 if (S_ISDIR(inf->mode)) {
389 inode->i_nlink = 2;
390 inode->i_op = &autofs4_dir_inode_operations;
391 inode->i_fop = &autofs4_dir_operations;
392 } else if (S_ISLNK(inf->mode)) {
393 inode->i_size = inf->size;
394 inode->i_op = &autofs4_symlink_inode_operations;
397 return inode;