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 <linux/magic.h>
23 #include <linux/module.h>
25 struct autofs_info
*autofs4_new_ino(struct autofs_sb_info
*sbi
)
27 struct autofs_info
*ino
= kzalloc(sizeof(*ino
), GFP_KERNEL
);
29 INIT_LIST_HEAD(&ino
->active
);
30 INIT_LIST_HEAD(&ino
->expiring
);
31 ino
->last_used
= jiffies
;
37 void autofs4_clean_ino(struct autofs_info
*ino
)
39 ino
->uid
= GLOBAL_ROOT_UID
;
40 ino
->gid
= GLOBAL_ROOT_GID
;
41 ino
->last_used
= jiffies
;
44 void autofs4_free_ino(struct autofs_info
*ino
)
49 void autofs4_kill_sb(struct super_block
*sb
)
51 struct autofs_sb_info
*sbi
= autofs4_sbi(sb
);
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
60 /* Free wait queues, close pipe */
61 autofs4_catatonic_mode(sbi
);
62 put_pid(sbi
->oz_pgrp
);
65 DPRINTK("shutting down");
66 kill_litter_super(sb
);
71 static int autofs4_show_options(struct seq_file
*m
, struct dentry
*root
)
73 struct autofs_sb_info
*sbi
= autofs4_sbi(root
->d_sb
);
74 struct inode
*root_inode
= d_inode(root
->d_sb
->s_root
);
79 seq_printf(m
, ",fd=%d", sbi
->pipefd
);
80 if (!uid_eq(root_inode
->i_uid
, GLOBAL_ROOT_UID
))
81 seq_printf(m
, ",uid=%u",
82 from_kuid_munged(&init_user_ns
, root_inode
->i_uid
));
83 if (!gid_eq(root_inode
->i_gid
, GLOBAL_ROOT_GID
))
84 seq_printf(m
, ",gid=%u",
85 from_kgid_munged(&init_user_ns
, root_inode
->i_gid
));
86 seq_printf(m
, ",pgrp=%d", pid_vnr(sbi
->oz_pgrp
));
87 seq_printf(m
, ",timeout=%lu", sbi
->exp_timeout
/HZ
);
88 seq_printf(m
, ",minproto=%d", sbi
->min_proto
);
89 seq_printf(m
, ",maxproto=%d", sbi
->max_proto
);
91 if (autofs_type_offset(sbi
->type
))
92 seq_printf(m
, ",offset");
93 else if (autofs_type_direct(sbi
->type
))
94 seq_printf(m
, ",direct");
96 seq_printf(m
, ",indirect");
101 static void autofs4_evict_inode(struct inode
*inode
)
104 kfree(inode
->i_private
);
107 static const struct super_operations autofs4_sops
= {
108 .statfs
= simple_statfs
,
109 .show_options
= autofs4_show_options
,
110 .evict_inode
= autofs4_evict_inode
,
113 enum {Opt_err
, Opt_fd
, Opt_uid
, Opt_gid
, Opt_pgrp
, Opt_minproto
, Opt_maxproto
,
114 Opt_indirect
, Opt_direct
, Opt_offset
};
116 static const match_table_t tokens
= {
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"},
129 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
)
134 substring_t args
[MAX_OPT_ARGS
];
137 *uid
= current_uid();
138 *gid
= current_gid();
140 *minproto
= AUTOFS_MIN_PROTO_VERSION
;
141 *maxproto
= AUTOFS_MAX_PROTO_VERSION
;
148 while ((p
= strsep(&options
, ",")) != NULL
) {
153 token
= match_token(p
, tokens
, args
);
156 if (match_int(args
, pipefd
))
160 if (match_int(args
, &option
))
162 *uid
= make_kuid(current_user_ns(), option
);
163 if (!uid_valid(*uid
))
167 if (match_int(args
, &option
))
169 *gid
= make_kgid(current_user_ns(), option
);
170 if (!gid_valid(*gid
))
174 if (match_int(args
, &option
))
180 if (match_int(args
, &option
))
185 if (match_int(args
, &option
))
190 set_autofs_type_indirect(type
);
193 set_autofs_type_direct(type
);
196 set_autofs_type_offset(type
);
202 return (*pipefd
< 0);
205 int autofs4_fill_super(struct super_block
*s
, void *data
, int silent
)
207 struct inode
* root_inode
;
208 struct dentry
* root
;
211 struct autofs_sb_info
*sbi
;
212 struct autofs_info
*ino
;
214 bool pgrp_set
= false;
217 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
220 DPRINTK("starting up, sbi = %p",sbi
);
223 sbi
->magic
= AUTOFS_SBI_MAGIC
;
227 sbi
->exp_timeout
= 0;
231 sbi
->sub_version
= 0;
232 set_autofs_type_indirect(&sbi
->type
);
235 mutex_init(&sbi
->wq_mutex
);
236 mutex_init(&sbi
->pipe_mutex
);
237 spin_lock_init(&sbi
->fs_lock
);
239 spin_lock_init(&sbi
->lookup_lock
);
240 INIT_LIST_HEAD(&sbi
->active_list
);
241 INIT_LIST_HEAD(&sbi
->expiring_list
);
242 s
->s_blocksize
= 1024;
243 s
->s_blocksize_bits
= 10;
244 s
->s_magic
= AUTOFS_SUPER_MAGIC
;
245 s
->s_op
= &autofs4_sops
;
246 s
->s_d_op
= &autofs4_dentry_operations
;
250 * Get the root inode and dentry, but defer checking for errors.
252 ino
= autofs4_new_ino(sbi
);
257 root_inode
= autofs4_get_inode(s
, S_IFDIR
| 0755);
258 root
= d_make_root(root_inode
);
263 root
->d_fsdata
= ino
;
265 /* Can this call block? */
266 if (parse_options(data
, &pipefd
, &root_inode
->i_uid
, &root_inode
->i_gid
,
267 &pgrp
, &pgrp_set
, &sbi
->type
, &sbi
->min_proto
,
269 printk("autofs: called with bogus options\n");
274 sbi
->oz_pgrp
= find_get_pid(pgrp
);
276 pr_warn("autofs: could not find process group %d\n",
281 sbi
->oz_pgrp
= get_task_pid(current
, PIDTYPE_PGID
);
284 if (autofs_type_trigger(sbi
->type
))
285 __managed_dentry_set_managed(root
);
287 root_inode
->i_fop
= &autofs4_root_operations
;
288 root_inode
->i_op
= &autofs4_dir_inode_operations
;
290 /* Couldn't this be tested earlier? */
291 if (sbi
->max_proto
< AUTOFS_MIN_PROTO_VERSION
||
292 sbi
->min_proto
> AUTOFS_MAX_PROTO_VERSION
) {
293 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
);
300 /* Establish highest kernel protocol version */
301 if (sbi
->max_proto
> AUTOFS_MAX_PROTO_VERSION
)
302 sbi
->version
= AUTOFS_MAX_PROTO_VERSION
;
304 sbi
->version
= sbi
->max_proto
;
305 sbi
->sub_version
= AUTOFS_PROTO_SUBVERSION
;
307 DPRINTK("pipe fd = %d, pgrp = %u", pipefd
, pid_nr(sbi
->oz_pgrp
));
311 printk("autofs: could not open pipe file descriptor\n");
314 ret
= autofs_prepare_pipe(pipe
);
318 sbi
->pipefd
= pipefd
;
322 * Success! Install the root dentry now to indicate completion.
328 * Failure ... clean up.
331 printk("autofs: pipe file descriptor does not contain proper ops\n");
340 put_pid(sbi
->oz_pgrp
);
346 struct inode
*autofs4_get_inode(struct super_block
*sb
, umode_t mode
)
348 struct inode
*inode
= new_inode(sb
);
353 inode
->i_mode
= mode
;
355 inode
->i_uid
= d_inode(sb
->s_root
)->i_uid
;
356 inode
->i_gid
= d_inode(sb
->s_root
)->i_gid
;
358 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
359 inode
->i_ino
= get_next_ino();
363 inode
->i_op
= &autofs4_dir_inode_operations
;
364 inode
->i_fop
= &autofs4_dir_operations
;
365 } else if (S_ISLNK(mode
)) {
366 inode
->i_op
= &autofs4_symlink_inode_operations
;