blob: 6b6f43f00c468976b9f68d35141370705e03c548 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "autofs_i.h"
23#include <linux/module.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
Al Viro09f12c02011-01-16 17:20:23 -050026 struct autofs_sb_info *sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 int reinit = 1;
29
30 if (ino == NULL) {
31 reinit = 0;
32 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
33 }
34
35 if (ino == NULL)
36 return NULL;
37
Ian Kent25767372008-07-23 21:30:12 -070038 if (!reinit) {
39 ino->flags = 0;
Ian Kent25767372008-07-23 21:30:12 -070040 ino->dentry = NULL;
41 ino->size = 0;
42 INIT_LIST_HEAD(&ino->active);
Ian Kent4f8427d2009-12-15 16:45:42 -080043 ino->active_count = 0;
Ian Kent25767372008-07-23 21:30:12 -070044 INIT_LIST_HEAD(&ino->expiring);
45 atomic_set(&ino->count, 0);
46 }
47
Ian Kentc0f54d32008-10-15 22:02:52 -070048 ino->uid = 0;
49 ino->gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 ino->last_used = jiffies;
51
52 ino->sbi = sbi;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return ino;
55}
56
57void autofs4_free_ino(struct autofs_info *ino)
58{
59 if (ino->dentry) {
60 ino->dentry->d_fsdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 ino->dentry = NULL;
62 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 kfree(ino);
64}
65
David Howells6ce31522006-10-11 01:22:15 -070066void autofs4_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 struct autofs_sb_info *sbi = autofs4_sbi(sb);
69
Ian Kentba8df432006-11-14 02:03:29 -080070 /*
71 * In the event of a failure in get_sb_nodev the superblock
72 * info is not present so nothing else has been setup, so
Jiri Kosinac949d4e2006-12-06 20:39:38 -080073 * just call kill_anon_super when we are called from
74 * deactivate_super.
Ian Kentba8df432006-11-14 02:03:29 -080075 */
76 if (!sbi)
Jiri Kosinac949d4e2006-12-06 20:39:38 -080077 goto out_kill_sb;
Ian Kentba8df432006-11-14 02:03:29 -080078
Ian Kent5a11d4d2008-07-23 21:30:17 -070079 /* Free wait queues, close pipe */
80 autofs4_catatonic_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Ian Kentf50b6f82007-02-20 13:58:10 -080082 sb->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 kfree(sbi);
84
Jiri Kosinac949d4e2006-12-06 20:39:38 -080085out_kill_sb:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 DPRINTK("shutting down");
Al Viro5b7e9342010-01-24 00:28:52 -050087 kill_litter_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Ian Kentd7c4a5f2006-03-27 01:14:49 -080090static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
91{
92 struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
Miklos Szerediaef97cb2008-02-08 04:21:37 -080093 struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
Ian Kentd7c4a5f2006-03-27 01:14:49 -080094
95 if (!sbi)
96 return 0;
97
98 seq_printf(m, ",fd=%d", sbi->pipefd);
Miklos Szerediaef97cb2008-02-08 04:21:37 -080099 if (root_inode->i_uid != 0)
100 seq_printf(m, ",uid=%u", root_inode->i_uid);
101 if (root_inode->i_gid != 0)
102 seq_printf(m, ",gid=%u", root_inode->i_gid);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800103 seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
104 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
105 seq_printf(m, ",minproto=%d", sbi->min_proto);
106 seq_printf(m, ",maxproto=%d", sbi->max_proto);
107
Ian Kenta92daf62009-01-06 14:42:08 -0800108 if (autofs_type_offset(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -0800109 seq_printf(m, ",offset");
Ian Kenta92daf62009-01-06 14:42:08 -0800110 else if (autofs_type_direct(sbi->type))
Ian Kent34ca9592006-03-27 01:14:54 -0800111 seq_printf(m, ",direct");
112 else
113 seq_printf(m, ",indirect");
114
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800115 return 0;
116}
117
Al Viro292c5ee2011-01-17 00:47:38 -0500118static void autofs4_evict_inode(struct inode *inode)
119{
120 end_writeback(inode);
121 kfree(inode->i_private);
122}
123
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800124static const struct super_operations autofs4_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .statfs = simple_statfs,
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800126 .show_options = autofs4_show_options,
Al Viro292c5ee2011-01-17 00:47:38 -0500127 .evict_inode = autofs4_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Ian Kent34ca9592006-03-27 01:14:54 -0800130enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
131 Opt_indirect, Opt_direct, Opt_offset};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Steven Whitehousea447c092008-10-13 10:46:57 +0100133static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 {Opt_fd, "fd=%u"},
135 {Opt_uid, "uid=%u"},
136 {Opt_gid, "gid=%u"},
137 {Opt_pgrp, "pgrp=%u"},
138 {Opt_minproto, "minproto=%u"},
139 {Opt_maxproto, "maxproto=%u"},
Ian Kent34ca9592006-03-27 01:14:54 -0800140 {Opt_indirect, "indirect"},
141 {Opt_direct, "direct"},
142 {Opt_offset, "offset"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 {Opt_err, NULL}
144};
145
146static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700147 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 char *p;
150 substring_t args[MAX_OPT_ARGS];
151 int option;
152
David Howells0eb790e2008-11-14 10:38:46 +1100153 *uid = current_uid();
154 *gid = current_gid();
Pavel Emelianova47afb02007-10-18 23:39:46 -0700155 *pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 *minproto = AUTOFS_MIN_PROTO_VERSION;
158 *maxproto = AUTOFS_MAX_PROTO_VERSION;
159
160 *pipefd = -1;
161
162 if (!options)
163 return 1;
164
165 while ((p = strsep(&options, ",")) != NULL) {
166 int token;
167 if (!*p)
168 continue;
169
170 token = match_token(p, tokens, args);
171 switch (token) {
172 case Opt_fd:
173 if (match_int(args, pipefd))
174 return 1;
175 break;
176 case Opt_uid:
177 if (match_int(args, &option))
178 return 1;
179 *uid = option;
180 break;
181 case Opt_gid:
182 if (match_int(args, &option))
183 return 1;
184 *gid = option;
185 break;
186 case Opt_pgrp:
187 if (match_int(args, &option))
188 return 1;
189 *pgrp = option;
190 break;
191 case Opt_minproto:
192 if (match_int(args, &option))
193 return 1;
194 *minproto = option;
195 break;
196 case Opt_maxproto:
197 if (match_int(args, &option))
198 return 1;
199 *maxproto = option;
200 break;
Ian Kent34ca9592006-03-27 01:14:54 -0800201 case Opt_indirect:
Ian Kenta92daf62009-01-06 14:42:08 -0800202 set_autofs_type_indirect(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800203 break;
204 case Opt_direct:
Ian Kenta92daf62009-01-06 14:42:08 -0800205 set_autofs_type_direct(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800206 break;
207 case Opt_offset:
Ian Kenta92daf62009-01-06 14:42:08 -0800208 set_autofs_type_offset(type);
Ian Kent34ca9592006-03-27 01:14:54 -0800209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 default:
211 return 1;
212 }
213 }
214 return (*pipefd < 0);
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217int autofs4_fill_super(struct super_block *s, void *data, int silent)
218{
219 struct inode * root_inode;
220 struct dentry * root;
221 struct file * pipe;
222 int pipefd;
223 struct autofs_sb_info *sbi;
224 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Mariusz Kozlowskib63d50c2007-10-16 23:26:44 -0700226 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700227 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto fail_unlock;
229 DPRINTK("starting up, sbi = %p",sbi);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 s->s_fs_info = sbi;
232 sbi->magic = AUTOFS_SBI_MAGIC;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800233 sbi->pipefd = -1;
Ian Kentba8df432006-11-14 02:03:29 -0800234 sbi->pipe = NULL;
235 sbi->catatonic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 sbi->exp_timeout = 0;
Pavel Emelianova47afb02007-10-18 23:39:46 -0700237 sbi->oz_pgrp = task_pgrp_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 sbi->sb = s;
239 sbi->version = 0;
240 sbi->sub_version = 0;
Ian Kenta92daf62009-01-06 14:42:08 -0800241 set_autofs_type_indirect(&sbi->type);
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800242 sbi->min_proto = 0;
243 sbi->max_proto = 0;
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800244 mutex_init(&sbi->wq_mutex);
Ian Kent3a9720c2005-05-01 08:59:17 -0700245 spin_lock_init(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 sbi->queues = NULL;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700247 spin_lock_init(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700248 INIT_LIST_HEAD(&sbi->active_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700249 INIT_LIST_HEAD(&sbi->expiring_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 s->s_blocksize = 1024;
251 s->s_blocksize_bits = 10;
252 s->s_magic = AUTOFS_SUPER_MAGIC;
253 s->s_op = &autofs4_sops;
David Howellsb650c852011-01-15 10:51:57 +0000254 s->s_d_op = &autofs4_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 s->s_time_gran = 1;
256
257 /*
258 * Get the root inode and dentry, but defer checking for errors.
259 */
Al Viro09f12c02011-01-16 17:20:23 -0500260 ino = autofs4_init_ino(NULL, sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!ino)
262 goto fail_free;
Al Viro09f12c02011-01-16 17:20:23 -0500263 root_inode = autofs4_get_inode(s, ino, S_IFDIR | 0755);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!root_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800265 goto fail_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 root = d_alloc_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!root)
269 goto fail_iput;
Ian Kent34ca9592006-03-27 01:14:54 -0800270 pipe = NULL;
271
Ian Kent34ca9592006-03-27 01:14:54 -0800272 root->d_fsdata = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 /* Can this call block? */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700275 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
276 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
277 &sbi->max_proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 printk("autofs: called with bogus options\n");
279 goto fail_dput;
280 }
281
David Howellsb650c852011-01-15 10:51:57 +0000282 if (autofs_type_trigger(sbi->type))
Ian Kentb5b80172011-01-14 18:46:03 +0000283 __managed_dentry_set_managed(root);
Ian Kent10584212011-01-14 18:45:58 +0000284
Ian Kent34ca9592006-03-27 01:14:54 -0800285 root_inode->i_fop = &autofs4_root_operations;
Ian Kente61da202011-01-14 18:46:14 +0000286 root_inode->i_op = &autofs4_dir_inode_operations;
Ian Kent34ca9592006-03-27 01:14:54 -0800287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* Couldn't this be tested earlier? */
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800289 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
290 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 printk("autofs: kernel does not match daemon version "
292 "daemon (%d, %d) kernel (%d, %d)\n",
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800293 sbi->min_proto, sbi->max_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
295 goto fail_dput;
296 }
297
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800298 /* Establish highest kernel protocol version */
299 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
300 sbi->version = AUTOFS_MAX_PROTO_VERSION;
301 else
302 sbi->version = sbi->max_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
304
305 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
306 pipe = fget(pipefd);
307
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700308 if (!pipe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 printk("autofs: could not open pipe file descriptor\n");
310 goto fail_dput;
311 }
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700312 if (!pipe->f_op || !pipe->f_op->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto fail_fput;
314 sbi->pipe = pipe;
Ian Kentd7c4a5f2006-03-27 01:14:49 -0800315 sbi->pipefd = pipefd;
Ian Kentba8df432006-11-14 02:03:29 -0800316 sbi->catatonic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * Success! Install the root dentry now to indicate completion.
320 */
321 s->s_root = root;
322 return 0;
323
324 /*
325 * Failure ... clean up.
326 */
327fail_fput:
328 printk("autofs: pipe file descriptor does not contain proper ops\n");
329 fput(pipe);
330 /* fall through */
331fail_dput:
332 dput(root);
333 goto fail_free;
334fail_iput:
335 printk("autofs: get root dentry failed\n");
336 iput(root_inode);
Ian Kent34ca9592006-03-27 01:14:54 -0800337fail_ino:
338 kfree(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339fail_free:
340 kfree(sbi);
Ian Kentba8df432006-11-14 02:03:29 -0800341 s->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342fail_unlock:
343 return -EINVAL;
344}
345
346struct inode *autofs4_get_inode(struct super_block *sb,
Al Viro09f12c02011-01-16 17:20:23 -0500347 struct autofs_info *inf,
348 mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct inode *inode = new_inode(sb);
351
352 if (inode == NULL)
353 return NULL;
354
Al Viro09f12c02011-01-16 17:20:23 -0500355 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (sb->s_root) {
357 inode->i_uid = sb->s_root->d_inode->i_uid;
358 inode->i_gid = sb->s_root->d_inode->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400361 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Al Viro09f12c02011-01-16 17:20:23 -0500363 if (S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 inode->i_nlink = 2;
365 inode->i_op = &autofs4_dir_inode_operations;
366 inode->i_fop = &autofs4_dir_operations;
Al Viro09f12c02011-01-16 17:20:23 -0500367 } else if (S_ISLNK(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 inode->i_size = inf->size;
369 inode->i_op = &autofs4_symlink_inode_operations;
370 }
371
372 return inode;
373}