blob: d8315e61731e55beac25b151aa929f0ceb6b9b26 [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
Alexander Krizhanovskyf76baf92005-09-09 13:01:59 -070040 autofs_hash_nuke(sbi);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -070041 for (n = 0; n < AUTOFS_MAX_SYMLINKS; n++) {
42 if (test_bit(n, sbi->symlink_bitmap))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 kfree(sbi->symlink[n].data);
44 }
45
46 kfree(sb->s_fs_info);
47
Jiri Kosinac949d4e2006-12-06 20:39:38 -080048out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 DPRINTK(("autofs: shutting down\n"));
David Howells0e7d7382006-10-19 23:28:36 -070050 kill_anon_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53static void autofs_read_inode(struct inode *inode);
54
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080055static const struct super_operations autofs_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .read_inode = autofs_read_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .statfs = simple_statfs,
58};
59
60enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
61
62static match_table_t autofs_tokens = {
63 {Opt_fd, "fd=%u"},
64 {Opt_uid, "uid=%u"},
65 {Opt_gid, "gid=%u"},
66 {Opt_pgrp, "pgrp=%u"},
67 {Opt_minproto, "minproto=%u"},
68 {Opt_maxproto, "maxproto=%u"},
69 {Opt_err, NULL}
70};
71
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -070072static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
73 pid_t *pgrp, int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 char *p;
76 substring_t args[MAX_OPT_ARGS];
77 int option;
78
79 *uid = current->uid;
80 *gid = current->gid;
81 *pgrp = process_group(current);
82
83 *minproto = *maxproto = AUTOFS_PROTO_VERSION;
84
85 *pipefd = -1;
86
87 if (!options)
88 return 1;
89
90 while ((p = strsep(&options, ",")) != NULL) {
91 int token;
92 if (!*p)
93 continue;
94
95 token = match_token(p, autofs_tokens, args);
96 switch (token) {
97 case Opt_fd:
98 if (match_int(&args[0], &option))
99 return 1;
100 *pipefd = option;
101 break;
102 case Opt_uid:
103 if (match_int(&args[0], &option))
104 return 1;
105 *uid = option;
106 break;
107 case Opt_gid:
108 if (match_int(&args[0], &option))
109 return 1;
110 *gid = option;
111 break;
112 case Opt_pgrp:
113 if (match_int(&args[0], &option))
114 return 1;
115 *pgrp = option;
116 break;
117 case Opt_minproto:
118 if (match_int(&args[0], &option))
119 return 1;
120 *minproto = option;
121 break;
122 case Opt_maxproto:
123 if (match_int(&args[0], &option))
124 return 1;
125 *maxproto = option;
126 break;
127 default:
128 return 1;
129 }
130 }
131 return (*pipefd < 0);
132}
133
134int autofs_fill_super(struct super_block *s, void *data, int silent)
135{
136 struct inode * root_inode;
137 struct dentry * root;
138 struct file * pipe;
139 int pipefd;
140 struct autofs_sb_info *sbi;
141 int minproto, maxproto;
142
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700143 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700144 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto fail_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 DPRINTK(("autofs: starting up, sbi = %p\n",sbi));
147
148 s->s_fs_info = sbi;
149 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentba8df432006-11-14 02:03:29 -0800150 sbi->pipe = NULL;
151 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 sbi->exp_timeout = 0;
153 sbi->oz_pgrp = process_group(current);
154 autofs_initialize_hash(&sbi->dirhash);
155 sbi->queues = NULL;
156 memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN);
157 sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO;
158 s->s_blocksize = 1024;
159 s->s_blocksize_bits = 10;
160 s->s_magic = AUTOFS_SUPER_MAGIC;
161 s->s_op = &autofs_sops;
162 s->s_time_gran = 1;
Alexander Krizhanovskyf76baf92005-09-09 13:01:59 -0700163 sbi->sb = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 root_inode = iget(s, AUTOFS_ROOT_INO);
166 root = d_alloc_root(root_inode);
167 pipe = NULL;
168
169 if (!root)
170 goto fail_iput;
171
172 /* Can this call block? - WTF cares? s is locked. */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700173 if (parse_options(data, &pipefd, &root_inode->i_uid,
174 &root_inode->i_gid, &sbi->oz_pgrp, &minproto,
175 &maxproto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 printk("autofs: called with bogus options\n");
177 goto fail_dput;
178 }
179
180 /* Couldn't this be tested earlier? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700181 if (minproto > AUTOFS_PROTO_VERSION ||
182 maxproto < AUTOFS_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 printk("autofs: kernel does not match daemon version\n");
184 goto fail_dput;
185 }
186
187 DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, sbi->oz_pgrp));
188 pipe = fget(pipefd);
189
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700190 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 printk("autofs: could not open pipe file descriptor\n");
192 goto fail_dput;
193 }
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700194 if (!pipe->f_op || !pipe->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto fail_fput;
196 sbi->pipe = pipe;
Ian Kentba8df432006-11-14 02:03:29 -0800197 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /*
200 * Success! Install the root dentry now to indicate completion.
201 */
202 s->s_root = root;
203 return 0;
204
205fail_fput:
206 printk("autofs: pipe file descriptor does not contain proper ops\n");
207 fput(pipe);
208fail_dput:
209 dput(root);
210 goto fail_free;
211fail_iput:
212 printk("autofs: get root dentry failed\n");
213 iput(root_inode);
214fail_free:
215 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800216 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217fail_unlock:
218 return -EINVAL;
219}
220
221static void autofs_read_inode(struct inode *inode)
222{
223 ino_t ino = inode->i_ino;
224 unsigned int n;
225 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
226
227 /* Initialize to the default case (stub directory) */
228
229 inode->i_op = &simple_dir_inode_operations;
230 inode->i_fop = &simple_dir_operations;
231 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
232 inode->i_nlink = 2;
233 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
234 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700236 if (ino == AUTOFS_ROOT_INO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
238 inode->i_op = &autofs_root_inode_operations;
239 inode->i_fop = &autofs_root_operations;
240 inode->i_uid = inode->i_gid = 0; /* Changed in read_super */
241 return;
242 }
243
244 inode->i_uid = inode->i_sb->s_root->d_inode->i_uid;
245 inode->i_gid = inode->i_sb->s_root->d_inode->i_gid;
246
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700247 if (ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* Symlink inode - should be in symlink list */
249 struct autofs_symlink *sl;
250
251 n = ino - AUTOFS_FIRST_SYMLINK;
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700252 if (n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino);
254 return;
255 }
256
257 inode->i_op = &autofs_symlink_inode_operations;
258 sl = &sbi->symlink[n];
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700259 inode->i_private = sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 inode->i_mode = S_IFLNK | S_IRWXUGO;
261 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime;
262 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
263 inode->i_size = sl->len;
264 inode->i_nlink = 1;
265 }
266}