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