blob: af2efbbb5d76ef7a6db921ba43316b5570c4dadf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- linux-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/mm.h>
15#include <linux/slab.h>
16#include <linux/file.h>
17#include <linux/parser.h>
18#include <linux/bitops.h>
Jeff Garzike18fa702006-09-24 11:13:19 -040019#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "autofs_i.h"
21#include <linux/module.h>
22
23static void autofs_put_super(struct super_block *sb)
24{
25 struct autofs_sb_info *sbi = autofs_sbi(sb);
26 unsigned int n;
27
28 if ( !sbi->catatonic )
29 autofs_catatonic_mode(sbi); /* Free wait queues, close pipe */
30
Alexander Krizhanovskyf76baf92005-09-09 13:01:59 -070031 autofs_hash_nuke(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 for ( n = 0 ; n < AUTOFS_MAX_SYMLINKS ; n++ ) {
33 if ( test_bit(n, sbi->symlink_bitmap) )
34 kfree(sbi->symlink[n].data);
35 }
36
37 kfree(sb->s_fs_info);
38
39 DPRINTK(("autofs: shutting down\n"));
40}
41
42static void autofs_read_inode(struct inode *inode);
43
44static struct super_operations autofs_sops = {
45 .read_inode = autofs_read_inode,
46 .put_super = autofs_put_super,
47 .statfs = simple_statfs,
48};
49
50enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
51
52static match_table_t autofs_tokens = {
53 {Opt_fd, "fd=%u"},
54 {Opt_uid, "uid=%u"},
55 {Opt_gid, "gid=%u"},
56 {Opt_pgrp, "pgrp=%u"},
57 {Opt_minproto, "minproto=%u"},
58 {Opt_maxproto, "maxproto=%u"},
59 {Opt_err, NULL}
60};
61
62static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid, pid_t *pgrp, int *minproto, int *maxproto)
63{
64 char *p;
65 substring_t args[MAX_OPT_ARGS];
66 int option;
67
68 *uid = current->uid;
69 *gid = current->gid;
70 *pgrp = process_group(current);
71
72 *minproto = *maxproto = AUTOFS_PROTO_VERSION;
73
74 *pipefd = -1;
75
76 if (!options)
77 return 1;
78
79 while ((p = strsep(&options, ",")) != NULL) {
80 int token;
81 if (!*p)
82 continue;
83
84 token = match_token(p, autofs_tokens, args);
85 switch (token) {
86 case Opt_fd:
87 if (match_int(&args[0], &option))
88 return 1;
89 *pipefd = option;
90 break;
91 case Opt_uid:
92 if (match_int(&args[0], &option))
93 return 1;
94 *uid = option;
95 break;
96 case Opt_gid:
97 if (match_int(&args[0], &option))
98 return 1;
99 *gid = option;
100 break;
101 case Opt_pgrp:
102 if (match_int(&args[0], &option))
103 return 1;
104 *pgrp = option;
105 break;
106 case Opt_minproto:
107 if (match_int(&args[0], &option))
108 return 1;
109 *minproto = option;
110 break;
111 case Opt_maxproto:
112 if (match_int(&args[0], &option))
113 return 1;
114 *maxproto = option;
115 break;
116 default:
117 return 1;
118 }
119 }
120 return (*pipefd < 0);
121}
122
123int autofs_fill_super(struct super_block *s, void *data, int silent)
124{
125 struct inode * root_inode;
126 struct dentry * root;
127 struct file * pipe;
128 int pipefd;
129 struct autofs_sb_info *sbi;
130 int minproto, maxproto;
131
132 sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
133 if ( !sbi )
134 goto fail_unlock;
135 memset(sbi, 0, sizeof(*sbi));
136 DPRINTK(("autofs: starting up, sbi = %p\n",sbi));
137
138 s->s_fs_info = sbi;
139 sbi->magic = AUTOFS_SBI_MAGIC;
140 sbi->catatonic = 0;
141 sbi->exp_timeout = 0;
142 sbi->oz_pgrp = process_group(current);
143 autofs_initialize_hash(&sbi->dirhash);
144 sbi->queues = NULL;
145 memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN);
146 sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO;
147 s->s_blocksize = 1024;
148 s->s_blocksize_bits = 10;
149 s->s_magic = AUTOFS_SUPER_MAGIC;
150 s->s_op = &autofs_sops;
151 s->s_time_gran = 1;
Alexander Krizhanovskyf76baf92005-09-09 13:01:59 -0700152 sbi->sb = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 root_inode = iget(s, AUTOFS_ROOT_INO);
155 root = d_alloc_root(root_inode);
156 pipe = NULL;
157
158 if (!root)
159 goto fail_iput;
160
161 /* Can this call block? - WTF cares? s is locked. */
162 if ( parse_options(data,&pipefd,&root_inode->i_uid,&root_inode->i_gid,&sbi->oz_pgrp,&minproto,&maxproto) ) {
163 printk("autofs: called with bogus options\n");
164 goto fail_dput;
165 }
166
167 /* Couldn't this be tested earlier? */
168 if ( minproto > AUTOFS_PROTO_VERSION ||
169 maxproto < AUTOFS_PROTO_VERSION ) {
170 printk("autofs: kernel does not match daemon version\n");
171 goto fail_dput;
172 }
173
174 DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, sbi->oz_pgrp));
175 pipe = fget(pipefd);
176
177 if ( !pipe ) {
178 printk("autofs: could not open pipe file descriptor\n");
179 goto fail_dput;
180 }
181 if ( !pipe->f_op || !pipe->f_op->write )
182 goto fail_fput;
183 sbi->pipe = pipe;
184
185 /*
186 * Success! Install the root dentry now to indicate completion.
187 */
188 s->s_root = root;
189 return 0;
190
191fail_fput:
192 printk("autofs: pipe file descriptor does not contain proper ops\n");
193 fput(pipe);
194fail_dput:
195 dput(root);
196 goto fail_free;
197fail_iput:
198 printk("autofs: get root dentry failed\n");
199 iput(root_inode);
200fail_free:
201 kfree(sbi);
202fail_unlock:
203 return -EINVAL;
204}
205
206static void autofs_read_inode(struct inode *inode)
207{
208 ino_t ino = inode->i_ino;
209 unsigned int n;
210 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
211
212 /* Initialize to the default case (stub directory) */
213
214 inode->i_op = &simple_dir_inode_operations;
215 inode->i_fop = &simple_dir_operations;
216 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
217 inode->i_nlink = 2;
218 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
219 inode->i_blocks = 0;
220 inode->i_blksize = 1024;
221
222 if ( ino == AUTOFS_ROOT_INO ) {
223 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
224 inode->i_op = &autofs_root_inode_operations;
225 inode->i_fop = &autofs_root_operations;
226 inode->i_uid = inode->i_gid = 0; /* Changed in read_super */
227 return;
228 }
229
230 inode->i_uid = inode->i_sb->s_root->d_inode->i_uid;
231 inode->i_gid = inode->i_sb->s_root->d_inode->i_gid;
232
233 if ( ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO ) {
234 /* Symlink inode - should be in symlink list */
235 struct autofs_symlink *sl;
236
237 n = ino - AUTOFS_FIRST_SYMLINK;
238 if ( n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) {
239 printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino);
240 return;
241 }
242
243 inode->i_op = &autofs_symlink_inode_operations;
244 sl = &sbi->symlink[n];
245 inode->u.generic_ip = sl;
246 inode->i_mode = S_IFLNK | S_IRWXUGO;
247 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime;
248 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
249 inode->i_size = sl->len;
250 inode->i_nlink = 1;
251 }
252}