GNU Linux-libre 4.4.284-gnu1
[releases.git] / fs / autofs4 / inode.c
1 /* -*- c -*- --------------------------------------------------------------- *
2  *
3  * linux/fs/autofs/inode.c
4  *
5  *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6  *  Copyright 2005-2006 Ian Kent <raven@themaw.net>
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>
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>
23
24 struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
25 {
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;
32         }
33         return ino;
34 }
35
36 void autofs4_clean_ino(struct autofs_info *ino)
37 {
38         ino->uid = GLOBAL_ROOT_UID;
39         ino->gid = GLOBAL_ROOT_GID;
40         ino->last_used = jiffies;
41 }
42
43 void autofs4_free_ino(struct autofs_info *ino)
44 {
45         kfree(ino);
46 }
47
48 void autofs4_kill_sb(struct super_block *sb)
49 {
50         struct autofs_sb_info *sbi = autofs4_sbi(sb);
51
52         /*
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.
57          */
58         if (sbi) {
59                 /* Free wait queues, close pipe */
60                 autofs4_catatonic_mode(sbi);
61                 put_pid(sbi->oz_pgrp);
62         }
63
64         DPRINTK("shutting down");
65         kill_litter_super(sb);
66         if (sbi)
67                 kfree_rcu(sbi, rcu);
68 }
69
70 static int autofs4_show_options(struct seq_file *m, struct dentry *root)
71 {
72         struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
73         struct inode *root_inode = d_inode(root->d_sb->s_root);
74
75         if (!sbi)
76                 return 0;
77
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);
89
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");
96
97         return 0;
98 }
99
100 static void autofs4_evict_inode(struct inode *inode)
101 {
102         clear_inode(inode);
103         kfree(inode->i_private);
104 }
105
106 static const struct super_operations autofs4_sops = {
107         .statfs         = simple_statfs,
108         .show_options   = autofs4_show_options,
109         .evict_inode    = autofs4_evict_inode,
110 };
111
112 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
113         Opt_indirect, Opt_direct, Opt_offset};
114
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}
126 };
127
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)
131 {
132         char *p;
133         substring_t args[MAX_OPT_ARGS];
134         int option;
135
136         *uid = current_uid();
137         *gid = current_gid();
138
139         *minproto = AUTOFS_MIN_PROTO_VERSION;
140         *maxproto = AUTOFS_MAX_PROTO_VERSION;
141
142         *pipefd = -1;
143
144         if (!options)
145                 return 1;
146
147         while ((p = strsep(&options, ",")) != NULL) {
148                 int token;
149                 if (!*p)
150                         continue;
151
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;
199                 }
200         }
201         return (*pipefd < 0);
202 }
203
204 int autofs4_fill_super(struct super_block *s, void *data, int silent)
205 {
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;
215
216         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
217         if (!sbi)
218                 return -ENOMEM;
219         DPRINTK("starting up, sbi = %p",sbi);
220
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;
247
248         /*
249          * Get the root inode and dentry, but defer checking for errors.
250          */
251         ino = autofs4_new_ino(sbi);
252         if (!ino) {
253                 ret = -ENOMEM;
254                 goto fail_free;
255         }
256         root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
257         root = d_make_root(root_inode);
258         if (!root) {
259                 ret = -ENOMEM;
260                 goto fail_ino;
261         }
262         pipe = NULL;
263
264         root->d_fsdata = ino;
265
266         /* Can this call block? */
267         if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
268                           &pgrp, &pgrp_set, &sbi->type, &sbi->min_proto,
269                           &sbi->max_proto)) {
270                 printk("autofs: called with bogus options\n");
271                 goto fail_dput;
272         }
273
274         if (pgrp_set) {
275                 sbi->oz_pgrp = find_get_pid(pgrp);
276                 if (!sbi->oz_pgrp) {
277                         pr_warn("autofs: could not find process group %d\n",
278                                 pgrp);
279                         goto fail_dput;
280                 }
281         } else {
282                 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
283         }
284
285         if (autofs_type_trigger(sbi->type))
286                 __managed_dentry_set_managed(root);
287
288         root_inode->i_fop = &autofs4_root_operations;
289         root_inode->i_op = &autofs4_dir_inode_operations;
290
291         /* Couldn't this be tested earlier? */
292         if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
293             sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
294                 printk("autofs: kernel does not match daemon version "
295                        "daemon (%d, %d) kernel (%d, %d)\n",
296                         sbi->min_proto, sbi->max_proto,
297                         AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
298                 goto fail_dput;
299         }
300
301         /* Establish highest kernel protocol version */
302         if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
303                 sbi->version = AUTOFS_MAX_PROTO_VERSION;
304         else
305                 sbi->version = sbi->max_proto;
306         sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
307
308         DPRINTK("pipe fd = %d, pgrp = %u", pipefd, pid_nr(sbi->oz_pgrp));
309         pipe = fget(pipefd);
310
311         if (!pipe) {
312                 printk("autofs: could not open pipe file descriptor\n");
313                 goto fail_dput;
314         }
315         ret = autofs_prepare_pipe(pipe);
316         if (ret < 0)
317                 goto fail_fput;
318         sbi->pipe = pipe;
319         sbi->pipefd = pipefd;
320         sbi->catatonic = 0;
321
322         /*
323          * Success! Install the root dentry now to indicate completion.
324          */
325         s->s_root = root;
326         return 0;
327         
328         /*
329          * Failure ... clean up.
330          */
331 fail_fput:
332         printk("autofs: pipe file descriptor does not contain proper ops\n");
333         fput(pipe);
334         /* fall through */
335 fail_dput:
336         dput(root);
337         goto fail_free;
338 fail_ino:
339         kfree(ino);
340 fail_free:
341         put_pid(sbi->oz_pgrp);
342         kfree(sbi);
343         s->s_fs_info = NULL;
344         return ret;
345 }
346
347 struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode)
348 {
349         struct inode *inode = new_inode(sb);
350
351         if (inode == NULL)
352                 return NULL;
353
354         inode->i_mode = mode;
355         if (sb->s_root) {
356                 inode->i_uid = d_inode(sb->s_root)->i_uid;
357                 inode->i_gid = d_inode(sb->s_root)->i_gid;
358         }
359         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
360         inode->i_ino = get_next_ino();
361
362         if (S_ISDIR(mode)) {
363                 set_nlink(inode, 2);
364                 inode->i_op = &autofs4_dir_inode_operations;
365                 inode->i_fop = &autofs4_dir_operations;
366         } else if (S_ISLNK(mode)) {
367                 inode->i_op = &autofs4_symlink_inode_operations;
368         }
369
370         return inode;
371 }