blob: e7204d71acc94660f4a50a3d2c10eb2a6a816e63 [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
David Howells0e7d7382006-10-19 23:28:36 -070023void autofs_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 struct autofs_sb_info *sbi = autofs_sbi(sb);
26 unsigned int n;
27
Ian Kentba8df432006-11-14 02:03:29 -080028 /*
29 * In the event of a failure in get_sb_nodev the superblock
30 * info is not present so nothing else has been setup, so
Jiri Kosinac949d4e2006-12-06 20:39:38 -080031 * just call kill_anon_super when we are called from
32 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -080033 */
34 if (!sbi)
Jiri Kosinac949d4e2006-12-06 20:39:38 -080035 goto out_kill_sb;
Ian Kentba8df432006-11-14 02:03:29 -080036
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -070037 if (!sbi->catatonic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 autofs_catatonic_mode(sbi); /* Free wait queues, close pipe */
39
Sukadev Bhattiprolufa0334f2007-05-10 22:23:08 -070040 put_pid(sbi->oz_pgrp);
41
Alexander Krizhanovskyf76baf92005-09-09 13:01:59 -070042 autofs_hash_nuke(sbi);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -070043 for (n = 0; n < AUTOFS_MAX_SYMLINKS; n++) {
44 if (test_bit(n, sbi->symlink_bitmap))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 kfree(sbi->symlink[n].data);
46 }
47
48 kfree(sb->s_fs_info);
49
Jiri Kosinac949d4e2006-12-06 20:39:38 -080050out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 DPRINTK(("autofs: shutting down\n"));
David Howells0e7d7382006-10-19 23:28:36 -070052 kill_anon_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55static void autofs_read_inode(struct inode *inode);
56
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080057static const struct super_operations autofs_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 .read_inode = autofs_read_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 .statfs = simple_statfs,
60};
61
62enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
63
64static match_table_t autofs_tokens = {
65 {Opt_fd, "fd=%u"},
66 {Opt_uid, "uid=%u"},
67 {Opt_gid, "gid=%u"},
68 {Opt_pgrp, "pgrp=%u"},
69 {Opt_minproto, "minproto=%u"},
70 {Opt_maxproto, "maxproto=%u"},
71 {Opt_err, NULL}
72};
73
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -070074static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
75 pid_t *pgrp, int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 char *p;
78 substring_t args[MAX_OPT_ARGS];
79 int option;
80
81 *uid = current->uid;
82 *gid = current->gid;
83 *pgrp = process_group(current);
84
85 *minproto = *maxproto = AUTOFS_PROTO_VERSION;
86
87 *pipefd = -1;
88
89 if (!options)
90 return 1;
91
92 while ((p = strsep(&options, ",")) != NULL) {
93 int token;
94 if (!*p)
95 continue;
96
97 token = match_token(p, autofs_tokens, args);
98 switch (token) {
99 case Opt_fd:
100 if (match_int(&args[0], &option))
101 return 1;
102 *pipefd = option;
103 break;
104 case Opt_uid:
105 if (match_int(&args[0], &option))
106 return 1;
107 *uid = option;
108 break;
109 case Opt_gid:
110 if (match_int(&args[0], &option))
111 return 1;
112 *gid = option;
113 break;
114 case Opt_pgrp:
115 if (match_int(&args[0], &option))
116 return 1;
117 *pgrp = option;
118 break;
119 case Opt_minproto:
120 if (match_int(&args[0], &option))
121 return 1;
122 *minproto = option;
123 break;
124 case Opt_maxproto:
125 if (match_int(&args[0], &option))
126 return 1;
127 *maxproto = option;
128 break;
129 default:
130 return 1;
131 }
132 }
133 return (*pipefd < 0);
134}
135
136int autofs_fill_super(struct super_block *s, void *data, int silent)
137{
138 struct inode * root_inode;
139 struct dentry * root;
140 struct file * pipe;
141 int pipefd;
142 struct autofs_sb_info *sbi;
143 int minproto, maxproto;
Sukadev Bhattiprolufa0334f2007-05-10 22:23:08 -0700144 pid_t pgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700146 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700147 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 goto fail_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 DPRINTK(("autofs: starting up, sbi = %p\n",sbi));
150
151 s->s_fs_info = sbi;
152 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentba8df432006-11-14 02:03:29 -0800153 sbi->pipe = NULL;
154 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 sbi->exp_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 autofs_initialize_hash(&sbi->dirhash);
157 sbi->queues = NULL;
158 memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN);
159 sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO;
160 s->s_blocksize = 1024;
161 s->s_blocksize_bits = 10;
162 s->s_magic = AUTOFS_SUPER_MAGIC;
163 s->s_op = &autofs_sops;
164 s->s_time_gran = 1;
Alexander Krizhanovskyf76baf92005-09-09 13:01:59 -0700165 sbi->sb = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 root_inode = iget(s, AUTOFS_ROOT_INO);
168 root = d_alloc_root(root_inode);
169 pipe = NULL;
170
171 if (!root)
172 goto fail_iput;
173
174 /* Can this call block? - WTF cares? s is locked. */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700175 if (parse_options(data, &pipefd, &root_inode->i_uid,
Sukadev Bhattiprolufa0334f2007-05-10 22:23:08 -0700176 &root_inode->i_gid, &pgid, &minproto,
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700177 &maxproto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 printk("autofs: called with bogus options\n");
179 goto fail_dput;
180 }
181
182 /* Couldn't this be tested earlier? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700183 if (minproto > AUTOFS_PROTO_VERSION ||
184 maxproto < AUTOFS_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 printk("autofs: kernel does not match daemon version\n");
186 goto fail_dput;
187 }
188
Sukadev Bhattiprolufa0334f2007-05-10 22:23:08 -0700189 DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, pgid));
190 sbi->oz_pgrp = find_get_pid(pgid);
191
192 if (!sbi->oz_pgrp) {
193 printk("autofs: could not find process group %d\n", pgid);
194 goto fail_dput;
195 }
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 pipe = fget(pipefd);
198
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700199 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 printk("autofs: could not open pipe file descriptor\n");
Sukadev Bhattiprolufa0334f2007-05-10 22:23:08 -0700201 goto fail_put_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Sukadev Bhattiprolufa0334f2007-05-10 22:23:08 -0700203
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700204 if (!pipe->f_op || !pipe->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto fail_fput;
206 sbi->pipe = pipe;
Ian Kentba8df432006-11-14 02:03:29 -0800207 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /*
210 * Success! Install the root dentry now to indicate completion.
211 */
212 s->s_root = root;
213 return 0;
214
215fail_fput:
216 printk("autofs: pipe file descriptor does not contain proper ops\n");
217 fput(pipe);
Sukadev Bhattiprolufa0334f2007-05-10 22:23:08 -0700218fail_put_pid:
219 put_pid(sbi->oz_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220fail_dput:
221 dput(root);
222 goto fail_free;
223fail_iput:
224 printk("autofs: get root dentry failed\n");
225 iput(root_inode);
226fail_free:
227 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800228 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229fail_unlock:
230 return -EINVAL;
231}
232
233static void autofs_read_inode(struct inode *inode)
234{
235 ino_t ino = inode->i_ino;
236 unsigned int n;
237 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
238
239 /* Initialize to the default case (stub directory) */
240
241 inode->i_op = &simple_dir_inode_operations;
242 inode->i_fop = &simple_dir_operations;
243 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
244 inode->i_nlink = 2;
245 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
246 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700248 if (ino == AUTOFS_ROOT_INO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
250 inode->i_op = &autofs_root_inode_operations;
251 inode->i_fop = &autofs_root_operations;
252 inode->i_uid = inode->i_gid = 0; /* Changed in read_super */
253 return;
254 }
255
256 inode->i_uid = inode->i_sb->s_root->d_inode->i_uid;
257 inode->i_gid = inode->i_sb->s_root->d_inode->i_gid;
258
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700259 if (ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Symlink inode - should be in symlink list */
261 struct autofs_symlink *sl;
262
263 n = ino - AUTOFS_FIRST_SYMLINK;
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700264 if (n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino);
266 return;
267 }
268
269 inode->i_op = &autofs_symlink_inode_operations;
270 sl = &sbi->symlink[n];
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700271 inode->i_private = sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 inode->i_mode = S_IFLNK | S_IRWXUGO;
273 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime;
274 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
275 inode->i_size = sl->len;
276 inode->i_nlink = 1;
277 }
278}