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