blob: 06858d955120ed43fab44309f1bc978f11c24124 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/inode.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
Ian Kent34ca9592006-03-27 01:14:54 -08006 * Copyright 2005-2006 Ian Kent <raven@themaw.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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>
Ian Kentd7c4a5f2006-03-27 01:14:49 -080017#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/pagemap.h>
19#include <linux/parser.h>
20#include <linux/bitops.h>
Jeff Garzike18fa702006-09-24 11:13:19 -040021#include <linux/magic.h>
Ian Kenta32744d2012-02-22 20:45:44 +080022#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "autofs_i.h"
24#include <linux/module.h>
25
Al Viro26e6c912011-01-16 18:43:40 -050026struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Al Viro26e6c912011-01-16 18:43:40 -050028 struct autofs_info *ino = kzalloc(sizeof(*ino), GFP_KERNEL);
29 if (ino) {
Ian Kent25767372008-07-23 21:30:12 -070030 INIT_LIST_HEAD(&ino->active);
31 INIT_LIST_HEAD(&ino->expiring);
Al Viro26e6c912011-01-16 18:43:40 -050032 ino->last_used = jiffies;
33 ino->sbi = sbi;
Ian Kent25767372008-07-23 21:30:12 -070034 }
Al Viro26e6c912011-01-16 18:43:40 -050035 return ino;
36}
Ian Kent25767372008-07-23 21:30:12 -070037
Al Viro26e6c912011-01-16 18:43:40 -050038void autofs4_clean_ino(struct autofs_info *ino)
39{
Ian Kentc0f54d32008-10-15 22:02:52 -070040 ino->uid = 0;
41 ino->gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 ino->last_used = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45void autofs4_free_ino(struct autofs_info *ino)
46{
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 kfree(ino);
48}
49
David Howells6ce31522006-10-11 01:22:15 -070050void autofs4_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 struct autofs_sb_info *sbi = autofs4_sbi(sb);
53
Ian Kentba8df432006-11-14 02:03:29 -080054 /*
55 * In the event of a failure in get_sb_nodev the superblock
56 * info is not present so nothing else has been setup, so
Jiri Kosinac949d4e2006-12-06 20:39:38 -080057 * just call kill_anon_super when we are called from
58 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -080059 */
60 if (!sbi)
Jiri Kosinac949d4e2006-12-06 20:39:38 -080061 goto out_kill_sb;
Ian Kentba8df432006-11-14 02:03:29 -080062
Ian Kent5a11d4d2008-07-23 21:30:17 -070063 /* Free wait queues, close pipe */
64 autofs4_catatonic_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Ian Kentf50b6f82007-02-20 13:58:10 -080066 sb->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 kfree(sbi);
68
Jiri Kosinac949d4e2006-12-06 20:39:38 -080069out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 DPRINTK("shutting down");
Al Viro5b7e9342010-01-24 00:28:52 -050071 kill_litter_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Al Viro34c80b12011-12-08 21:32:45 -050074static int autofs4_show_options(struct seq_file *m, struct dentry *root)
Ian Kentd7c4a5f2006-03-27 01:14:49 -080075{
Al Viro34c80b12011-12-08 21:32:45 -050076 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
77 struct inode *root_inode = root->d_sb->s_root->d_inode;
Ian Kentd7c4a5f2006-03-27 01:14:49 -080078
79 if (!sbi)
80 return 0;
81
82 seq_printf(m, ",fd=%d", sbi->pipefd);
Miklos Szerediaef97cb2008-02-08 04:21:37 -080083 if (root_inode->i_uid != 0)
84 seq_printf(m, ",uid=%u", root_inode->i_uid);
85 if (root_inode->i_gid != 0)
86 seq_printf(m, ",gid=%u", root_inode->i_gid);
Ian Kentd7c4a5f2006-03-27 01:14:49 -080087 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
88 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
89 seq_printf(m, ",minproto=%d", sbi->min_proto);
90 seq_printf(m, ",maxproto=%d", sbi->max_proto);
91
Ian Kenta92daf62009-01-06 14:42:08 -080092 if (autofs_type_offset(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -080093 seq_printf(m, ",offset");
Ian Kenta92daf62009-01-06 14:42:08 -080094 else if (autofs_type_direct(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -080095 seq_printf(m, ",direct");
96 else
97 seq_printf(m, ",indirect");
98
Ian Kentd7c4a5f2006-03-27 01:14:49 -080099 return 0;
100}
101
Al Viro292c5ee2011-01-17 00:47:38 -0500102static void autofs4_evict_inode(struct inode *inode)
103{
104 end_writeback(inode);
105 kfree(inode->i_private);
106}
107
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800108static const struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800110 .show_options = autofs4_show_options,
Al Viro292c5ee2011-01-17 00:47:38 -0500111 .evict_inode = autofs4_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Ian Kent34ca9592006-03-27 01:14:54 -0800114enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
115 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Steven Whitehousea447c092008-10-13 10:46:57 +0100117static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 {Opt_fd, "fd=%u"},
119 {Opt_uid, "uid=%u"},
120 {Opt_gid, "gid=%u"},
121 {Opt_pgrp, "pgrp=%u"},
122 {Opt_minproto, "minproto=%u"},
123 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800124 {Opt_indirect, "indirect"},
125 {Opt_direct, "direct"},
126 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 {Opt_err, NULL}
128};
129
130static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700131 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 char *p;
134 substring_t args[MAX_OPT_ARGS];
135 int option;
136
David Howells0eb790e2008-11-14 10:38:46 +1100137 *uid = current_uid();
138 *gid = current_gid();
Pavel Emelianova47afb02007-10-18 23:39:46 -0700139 *pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 *minproto = AUTOFS_MIN_PROTO_VERSION;
142 *maxproto = AUTOFS_MAX_PROTO_VERSION;
143
144 *pipefd = -1;
145
146 if (!options)
147 return 1;
148
149 while ((p = strsep(&options, ",")) != NULL) {
150 int token;
151 if (!*p)
152 continue;
153
154 token = match_token(p, tokens, args);
155 switch (token) {
156 case Opt_fd:
157 if (match_int(args, pipefd))
158 return 1;
159 break;
160 case Opt_uid:
161 if (match_int(args, &option))
162 return 1;
163 *uid = option;
164 break;
165 case Opt_gid:
166 if (match_int(args, &option))
167 return 1;
168 *gid = option;
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;
Ian Kent34ca9592006-03-27 01:14:54 -0800185 case Opt_indirect:
Ian Kenta92daf62009-01-06 14:42:08 -0800186 set_autofs_type_indirect(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800187 break;
188 case Opt_direct:
Ian Kenta92daf62009-01-06 14:42:08 -0800189 set_autofs_type_direct(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800190 break;
191 case Opt_offset:
Ian Kenta92daf62009-01-06 14:42:08 -0800192 set_autofs_type_offset(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 default:
195 return 1;
196 }
197 }
198 return (*pipefd < 0);
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201int 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Mariusz Kozlowskib63d50c2007-10-16 23:26:44 -0700210 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700211 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 goto fail_unlock;
213 DPRINTK("starting up, sbi = %p",sbi);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 s->s_fs_info = sbi;
216 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800217 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800218 sbi->pipe = NULL;
219 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 sbi->exp_timeout = 0;
Pavel Emelianova47afb02007-10-18 23:39:46 -0700221 sbi->oz_pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 sbi->sb = s;
223 sbi->version = 0;
224 sbi->sub_version = 0;
Ian Kenta92daf62009-01-06 14:42:08 -0800225 set_autofs_type_indirect(&sbi->type);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800226 sbi->min_proto = 0;
227 sbi->max_proto = 0;
Ian Kenta32744d2012-02-22 20:45:44 +0800228 sbi->compat_daemon = is_compat_task();
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800229 mutex_init(&sbi->wq_mutex);
Al Virod668dc52012-01-10 22:35:38 -0500230 mutex_init(&sbi->pipe_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700231 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 sbi->queues = NULL;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700233 spin_lock_init(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700234 INIT_LIST_HEAD(&sbi->active_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700235 INIT_LIST_HEAD(&sbi->expiring_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 s->s_blocksize = 1024;
237 s->s_blocksize_bits = 10;
238 s->s_magic = AUTOFS_SUPER_MAGIC;
239 s->s_op = &autofs4_sops;
David Howellsb650c852011-01-15 10:51:57 +0000240 s->s_d_op = &autofs4_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 s->s_time_gran = 1;
242
243 /*
244 * Get the root inode and dentry, but defer checking for errors.
245 */
Al Viro26e6c912011-01-16 18:43:40 -0500246 ino = autofs4_new_ino(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!ino)
248 goto fail_free;
Al Viro726a5e02011-01-16 17:43:52 -0500249 root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (!root_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800251 goto fail_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 root = d_alloc_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!root)
255 goto fail_iput;
Ian Kent34ca9592006-03-27 01:14:54 -0800256 pipe = NULL;
257
Ian Kent34ca9592006-03-27 01:14:54 -0800258 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /* Can this call block? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700261 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
262 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
263 &sbi->max_proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 printk("autofs: called with bogus options\n");
265 goto fail_dput;
266 }
267
David Howellsb650c852011-01-15 10:51:57 +0000268 if (autofs_type_trigger(sbi->type))
Ian Kentb5b80172011-01-14 18:46:03 +0000269 __managed_dentry_set_managed(root);
Ian Kent10584212011-01-14 18:45:58 +0000270
Ian Kent34ca9592006-03-27 01:14:54 -0800271 root_inode->i_fop = &autofs4_root_operations;
Ian Kente61da202011-01-14 18:46:14 +0000272 root_inode->i_op = &autofs4_dir_inode_operations;
Ian Kent34ca9592006-03-27 01:14:54 -0800273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800275 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
276 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 printk("autofs: kernel does not match daemon version "
278 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800279 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
281 goto fail_dput;
282 }
283
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800284 /* Establish highest kernel protocol version */
285 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
286 sbi->version = AUTOFS_MAX_PROTO_VERSION;
287 else
288 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
290
291 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
292 pipe = fget(pipefd);
293
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700294 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 printk("autofs: could not open pipe file descriptor\n");
296 goto fail_dput;
297 }
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700298 if (!pipe->f_op || !pipe->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto fail_fput;
300 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800301 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800302 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * Success! Install the root dentry now to indicate completion.
306 */
307 s->s_root = root;
308 return 0;
309
310 /*
311 * Failure ... clean up.
312 */
313fail_fput:
314 printk("autofs: pipe file descriptor does not contain proper ops\n");
315 fput(pipe);
316 /* fall through */
317fail_dput:
318 dput(root);
319 goto fail_free;
320fail_iput:
321 printk("autofs: get root dentry failed\n");
322 iput(root_inode);
Ian Kent34ca9592006-03-27 01:14:54 -0800323fail_ino:
324 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325fail_free:
326 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800327 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328fail_unlock:
329 return -EINVAL;
330}
331
Al Viro030a8ba2011-07-26 03:29:03 -0400332struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct inode *inode = new_inode(sb);
335
336 if (inode == NULL)
337 return NULL;
338
Al Viro09f12c02011-01-16 17:20:23 -0500339 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (sb->s_root) {
341 inode->i_uid = sb->s_root->d_inode->i_uid;
342 inode->i_gid = sb->s_root->d_inode->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400345 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Al Viro09f12c02011-01-16 17:20:23 -0500347 if (S_ISDIR(mode)) {
Miklos Szeredibfe86842011-10-28 14:13:29 +0200348 set_nlink(inode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 inode->i_op = &autofs4_dir_inode_operations;
350 inode->i_fop = &autofs4_dir_operations;
Al Viro09f12c02011-01-16 17:20:23 -0500351 } else if (S_ISLNK(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 inode->i_op = &autofs4_symlink_inode_operations;
353 }
354
355 return inode;
356}