init: fix possible format string bug
[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
26e6c910 25struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
1da177e4 26{
26e6c910
AV
27 struct autofs_info *ino = kzalloc(sizeof(*ino), GFP_KERNEL);
28 if (ino) {
25767378
IK
29 INIT_LIST_HEAD(&ino->active);
30 INIT_LIST_HEAD(&ino->expiring);
26e6c910
AV
31 ino->last_used = jiffies;
32 ino->sbi = sbi;
25767378 33 }
26e6c910
AV
34 return ino;
35}
f50b6f86 36
26e6c910
AV
37void autofs4_clean_ino(struct autofs_info *ino)
38{
45634cd8
EB
39 ino->uid = GLOBAL_ROOT_UID;
40 ino->gid = GLOBAL_ROOT_GID;
1da177e4 41 ino->last_used = jiffies;
1da177e4
LT
42}
43
44void autofs4_free_ino(struct autofs_info *ino)
45{
1da177e4
LT
46 kfree(ino);
47}
48
6ce31523 49void autofs4_kill_sb(struct super_block *sb)
1da177e4
LT
50{
51 struct autofs_sb_info *sbi = autofs4_sbi(sb);
52
ba8df43c
IK
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
c949d4eb
JK
56 * just call kill_anon_super when we are called from
57 * deactivate_super.
ba8df43c 58 */
baa40671
AV
59 if (sbi) /* Free wait queues, close pipe */
60 autofs4_catatonic_mode(sbi);
1da177e4
LT
61
62 DPRINTK("shutting down");
5b7e934d 63 kill_litter_super(sb);
baa40671
AV
64 if (sbi)
65 kfree_rcu(sbi, rcu);
1da177e4
LT
66}
67
34c80b1d 68static int autofs4_show_options(struct seq_file *m, struct dentry *root)
d7c4a5f1 69{
34c80b1d
AV
70 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
71 struct inode *root_inode = root->d_sb->s_root->d_inode;
d7c4a5f1
IK
72
73 if (!sbi)
74 return 0;
75
76 seq_printf(m, ",fd=%d", sbi->pipefd);
45634cd8
EB
77 if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
78 seq_printf(m, ",uid=%u",
79 from_kuid_munged(&init_user_ns, root_inode->i_uid));
80 if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
81 seq_printf(m, ",gid=%u",
82 from_kgid_munged(&init_user_ns, root_inode->i_gid));
d7c4a5f1
IK
83 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
84 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
85 seq_printf(m, ",minproto=%d", sbi->min_proto);
86 seq_printf(m, ",maxproto=%d", sbi->max_proto);
87
a92daf6b 88 if (autofs_type_offset(sbi->type))
34ca959c 89 seq_printf(m, ",offset");
a92daf6b 90 else if (autofs_type_direct(sbi->type))
34ca959c
IK
91 seq_printf(m, ",direct");
92 else
93 seq_printf(m, ",indirect");
94
d7c4a5f1
IK
95 return 0;
96}
97
292c5ee8
AV
98static void autofs4_evict_inode(struct inode *inode)
99{
dbd5768f 100 clear_inode(inode);
292c5ee8
AV
101 kfree(inode->i_private);
102}
103
ee9b6d61 104static const struct super_operations autofs4_sops = {
1da177e4 105 .statfs = simple_statfs,
d7c4a5f1 106 .show_options = autofs4_show_options,
292c5ee8 107 .evict_inode = autofs4_evict_inode,
1da177e4
LT
108};
109
34ca959c
IK
110enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
111 Opt_indirect, Opt_direct, Opt_offset};
1da177e4 112
a447c093 113static const match_table_t tokens = {
1da177e4
LT
114 {Opt_fd, "fd=%u"},
115 {Opt_uid, "uid=%u"},
116 {Opt_gid, "gid=%u"},
117 {Opt_pgrp, "pgrp=%u"},
118 {Opt_minproto, "minproto=%u"},
119 {Opt_maxproto, "maxproto=%u"},
34ca959c
IK
120 {Opt_indirect, "indirect"},
121 {Opt_direct, "direct"},
122 {Opt_offset, "offset"},
1da177e4
LT
123 {Opt_err, NULL}
124};
125
45634cd8 126static int parse_options(char *options, int *pipefd, kuid_t *uid, kgid_t *gid,
d78e53c8 127 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
1da177e4
LT
128{
129 char *p;
130 substring_t args[MAX_OPT_ARGS];
131 int option;
132
0eb790e3
DH
133 *uid = current_uid();
134 *gid = current_gid();
a47afb0f 135 *pgrp = task_pgrp_nr(current);
1da177e4
LT
136
137 *minproto = AUTOFS_MIN_PROTO_VERSION;
138 *maxproto = AUTOFS_MAX_PROTO_VERSION;
139
140 *pipefd = -1;
141
142 if (!options)
143 return 1;
144
145 while ((p = strsep(&options, ",")) != NULL) {
146 int token;
147 if (!*p)
148 continue;
149
150 token = match_token(p, tokens, args);
151 switch (token) {
152 case Opt_fd:
153 if (match_int(args, pipefd))
154 return 1;
155 break;
156 case Opt_uid:
157 if (match_int(args, &option))
158 return 1;
45634cd8
EB
159 *uid = make_kuid(current_user_ns(), option);
160 if (!uid_valid(*uid))
161 return 1;
1da177e4
LT
162 break;
163 case Opt_gid:
164 if (match_int(args, &option))
165 return 1;
45634cd8
EB
166 *gid = make_kgid(current_user_ns(), option);
167 if (!gid_valid(*gid))
168 return 1;
1da177e4
LT
169 break;
170 case Opt_pgrp:
171 if (match_int(args, &option))
172 return 1;
173 *pgrp = option;
174 break;
175 case Opt_minproto:
176 if (match_int(args, &option))
177 return 1;
178 *minproto = option;
179 break;
180 case Opt_maxproto:
181 if (match_int(args, &option))
182 return 1;
183 *maxproto = option;
184 break;
34ca959c 185 case Opt_indirect:
a92daf6b 186 set_autofs_type_indirect(type);
34ca959c
IK
187 break;
188 case Opt_direct:
a92daf6b 189 set_autofs_type_direct(type);
34ca959c
IK
190 break;
191 case Opt_offset:
a92daf6b 192 set_autofs_type_offset(type);
34ca959c 193 break;
1da177e4
LT
194 default:
195 return 1;
196 }
197 }
198 return (*pipefd < 0);
199}
200
1da177e4
LT
201int autofs4_fill_super(struct super_block *s, void *data, int silent)
202{
203 struct inode * root_inode;
204 struct dentry * root;
205 struct file * pipe;
206 int pipefd;
207 struct autofs_sb_info *sbi;
208 struct autofs_info *ino;
1da177e4 209
b63d50c4 210 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
d78e53c8 211 if (!sbi)
1da177e4
LT
212 goto fail_unlock;
213 DPRINTK("starting up, sbi = %p",sbi);
214
1da177e4
LT
215 s->s_fs_info = sbi;
216 sbi->magic = AUTOFS_SBI_MAGIC;
d7c4a5f1 217 sbi->pipefd = -1;
ba8df43c
IK
218 sbi->pipe = NULL;
219 sbi->catatonic = 1;
1da177e4 220 sbi->exp_timeout = 0;
a47afb0f 221 sbi->oz_pgrp = task_pgrp_nr(current);
1da177e4
LT
222 sbi->sb = s;
223 sbi->version = 0;
224 sbi->sub_version = 0;
a92daf6b 225 set_autofs_type_indirect(&sbi->type);
d7c4a5f1
IK
226 sbi->min_proto = 0;
227 sbi->max_proto = 0;
1d5599e3 228 mutex_init(&sbi->wq_mutex);
d668dc56 229 mutex_init(&sbi->pipe_mutex);
3a9720ce 230 spin_lock_init(&sbi->fs_lock);
1da177e4 231 sbi->queues = NULL;
5f6f4f28 232 spin_lock_init(&sbi->lookup_lock);
25767378 233 INIT_LIST_HEAD(&sbi->active_list);
5f6f4f28 234 INIT_LIST_HEAD(&sbi->expiring_list);
1da177e4
LT
235 s->s_blocksize = 1024;
236 s->s_blocksize_bits = 10;
237 s->s_magic = AUTOFS_SUPER_MAGIC;
238 s->s_op = &autofs4_sops;
b650c858 239 s->s_d_op = &autofs4_dentry_operations;
1da177e4
LT
240 s->s_time_gran = 1;
241
242 /*
243 * Get the root inode and dentry, but defer checking for errors.
244 */
26e6c910 245 ino = autofs4_new_ino(sbi);
1da177e4
LT
246 if (!ino)
247 goto fail_free;
726a5e06 248 root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
48fde701 249 root = d_make_root(root_inode);
1da177e4 250 if (!root)
48fde701 251 goto fail_ino;
34ca959c
IK
252 pipe = NULL;
253
34ca959c 254 root->d_fsdata = ino;
1da177e4
LT
255
256 /* Can this call block? */
d78e53c8
SB
257 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
258 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
259 &sbi->max_proto)) {
1da177e4
LT
260 printk("autofs: called with bogus options\n");
261 goto fail_dput;
262 }
263
b650c858 264 if (autofs_type_trigger(sbi->type))
b5b80177 265 __managed_dentry_set_managed(root);
10584211 266
34ca959c 267 root_inode->i_fop = &autofs4_root_operations;
e61da20a 268 root_inode->i_op = &autofs4_dir_inode_operations;
34ca959c 269
1da177e4 270 /* Couldn't this be tested earlier? */
d7c4a5f1
IK
271 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
272 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
1da177e4
LT
273 printk("autofs: kernel does not match daemon version "
274 "daemon (%d, %d) kernel (%d, %d)\n",
d7c4a5f1 275 sbi->min_proto, sbi->max_proto,
1da177e4
LT
276 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
277 goto fail_dput;
278 }
279
d7c4a5f1
IK
280 /* Establish highest kernel protocol version */
281 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
282 sbi->version = AUTOFS_MAX_PROTO_VERSION;
283 else
284 sbi->version = sbi->max_proto;
1da177e4
LT
285 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
286
287 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
288 pipe = fget(pipefd);
289
d78e53c8 290 if (!pipe) {
1da177e4
LT
291 printk("autofs: could not open pipe file descriptor\n");
292 goto fail_dput;
293 }
64f371bc 294 if (autofs_prepare_pipe(pipe) < 0)
1da177e4
LT
295 goto fail_fput;
296 sbi->pipe = pipe;
d7c4a5f1 297 sbi->pipefd = pipefd;
ba8df43c 298 sbi->catatonic = 0;
1da177e4
LT
299
300 /*
301 * Success! Install the root dentry now to indicate completion.
302 */
303 s->s_root = root;
304 return 0;
305
306 /*
307 * Failure ... clean up.
308 */
309fail_fput:
310 printk("autofs: pipe file descriptor does not contain proper ops\n");
311 fput(pipe);
312 /* fall through */
313fail_dput:
314 dput(root);
315 goto fail_free;
34ca959c
IK
316fail_ino:
317 kfree(ino);
1da177e4
LT
318fail_free:
319 kfree(sbi);
ba8df43c 320 s->s_fs_info = NULL;
1da177e4
LT
321fail_unlock:
322 return -EINVAL;
323}
324
030a8ba4 325struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode)
1da177e4
LT
326{
327 struct inode *inode = new_inode(sb);
328
329 if (inode == NULL)
330 return NULL;
331
09f12c03 332 inode->i_mode = mode;
1da177e4
LT
333 if (sb->s_root) {
334 inode->i_uid = sb->s_root->d_inode->i_uid;
335 inode->i_gid = sb->s_root->d_inode->i_gid;
1da177e4 336 }
1da177e4 337 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
85fe4025 338 inode->i_ino = get_next_ino();
1da177e4 339
09f12c03 340 if (S_ISDIR(mode)) {
bfe86848 341 set_nlink(inode, 2);
1da177e4
LT
342 inode->i_op = &autofs4_dir_inode_operations;
343 inode->i_fop = &autofs4_dir_operations;
09f12c03 344 } else if (S_ISLNK(mode)) {
1da177e4
LT
345 inode->i_op = &autofs4_symlink_inode_operations;
346 }
347
348 return inode;
349}
This page took 0.710962 seconds and 5 git commands to generate.