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