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