autofs4: Add d_manage() dentry operation
[firefly-linux-kernel-4.4.55.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 <linux/magic.h>
22 #include "autofs_i.h"
23 #include <linux/module.h>
24
25 static void ino_lnkfree(struct autofs_info *ino)
26 {
27         if (ino->u.symlink) {
28                 kfree(ino->u.symlink);
29                 ino->u.symlink = NULL;
30         }
31 }
32
33 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
34                                      struct autofs_sb_info *sbi, mode_t mode)
35 {
36         int reinit = 1;
37
38         if (ino == NULL) {
39                 reinit = 0;
40                 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
41         }
42
43         if (ino == NULL)
44                 return NULL;
45
46         if (!reinit) {
47                 ino->flags = 0;
48                 ino->inode = NULL;
49                 ino->dentry = NULL;
50                 ino->size = 0;
51                 INIT_LIST_HEAD(&ino->active);
52                 ino->active_count = 0;
53                 INIT_LIST_HEAD(&ino->expiring);
54                 atomic_set(&ino->count, 0);
55         }
56
57         ino->uid = 0;
58         ino->gid = 0;
59         ino->mode = mode;
60         ino->last_used = jiffies;
61
62         ino->sbi = sbi;
63
64         if (reinit && ino->free)
65                 (ino->free)(ino);
66
67         memset(&ino->u, 0, sizeof(ino->u));
68
69         ino->free = NULL;
70
71         if (S_ISLNK(mode))
72                 ino->free = ino_lnkfree;
73
74         return ino;
75 }
76
77 void autofs4_free_ino(struct autofs_info *ino)
78 {
79         struct autofs_info *p_ino;
80
81         if (ino->dentry) {
82                 ino->dentry->d_fsdata = NULL;
83                 if (ino->dentry->d_inode) {
84                         struct dentry *parent = ino->dentry->d_parent;
85                         if (atomic_dec_and_test(&ino->count)) {
86                                 p_ino = autofs4_dentry_ino(parent);
87                                 if (p_ino && parent != ino->dentry)
88                                         atomic_dec(&p_ino->count);
89                         }
90                         dput(ino->dentry);
91                 }
92                 ino->dentry = NULL;
93         }
94         if (ino->free)
95                 (ino->free)(ino);
96         kfree(ino);
97 }
98
99 void autofs4_kill_sb(struct super_block *sb)
100 {
101         struct autofs_sb_info *sbi = autofs4_sbi(sb);
102
103         /*
104          * In the event of a failure in get_sb_nodev the superblock
105          * info is not present so nothing else has been setup, so
106          * just call kill_anon_super when we are called from
107          * deactivate_super.
108          */
109         if (!sbi)
110                 goto out_kill_sb;
111
112         /* Free wait queues, close pipe */
113         autofs4_catatonic_mode(sbi);
114
115         sb->s_fs_info = NULL;
116         kfree(sbi);
117
118 out_kill_sb:
119         DPRINTK("shutting down");
120         kill_litter_super(sb);
121 }
122
123 static int autofs4_show_options(struct seq_file *m, struct vfsmount *mnt)
124 {
125         struct autofs_sb_info *sbi = autofs4_sbi(mnt->mnt_sb);
126         struct inode *root_inode = mnt->mnt_sb->s_root->d_inode;
127
128         if (!sbi)
129                 return 0;
130
131         seq_printf(m, ",fd=%d", sbi->pipefd);
132         if (root_inode->i_uid != 0)
133                 seq_printf(m, ",uid=%u", root_inode->i_uid);
134         if (root_inode->i_gid != 0)
135                 seq_printf(m, ",gid=%u", root_inode->i_gid);
136         seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
137         seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
138         seq_printf(m, ",minproto=%d", sbi->min_proto);
139         seq_printf(m, ",maxproto=%d", sbi->max_proto);
140
141         if (autofs_type_offset(sbi->type))
142                 seq_printf(m, ",offset");
143         else if (autofs_type_direct(sbi->type))
144                 seq_printf(m, ",direct");
145         else
146                 seq_printf(m, ",indirect");
147
148         return 0;
149 }
150
151 static const struct super_operations autofs4_sops = {
152         .statfs         = simple_statfs,
153         .show_options   = autofs4_show_options,
154 };
155
156 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
157         Opt_indirect, Opt_direct, Opt_offset};
158
159 static const match_table_t tokens = {
160         {Opt_fd, "fd=%u"},
161         {Opt_uid, "uid=%u"},
162         {Opt_gid, "gid=%u"},
163         {Opt_pgrp, "pgrp=%u"},
164         {Opt_minproto, "minproto=%u"},
165         {Opt_maxproto, "maxproto=%u"},
166         {Opt_indirect, "indirect"},
167         {Opt_direct, "direct"},
168         {Opt_offset, "offset"},
169         {Opt_err, NULL}
170 };
171
172 static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
173                 pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
174 {
175         char *p;
176         substring_t args[MAX_OPT_ARGS];
177         int option;
178
179         *uid = current_uid();
180         *gid = current_gid();
181         *pgrp = task_pgrp_nr(current);
182
183         *minproto = AUTOFS_MIN_PROTO_VERSION;
184         *maxproto = AUTOFS_MAX_PROTO_VERSION;
185
186         *pipefd = -1;
187
188         if (!options)
189                 return 1;
190
191         while ((p = strsep(&options, ",")) != NULL) {
192                 int token;
193                 if (!*p)
194                         continue;
195
196                 token = match_token(p, tokens, args);
197                 switch (token) {
198                 case Opt_fd:
199                         if (match_int(args, pipefd))
200                                 return 1;
201                         break;
202                 case Opt_uid:
203                         if (match_int(args, &option))
204                                 return 1;
205                         *uid = option;
206                         break;
207                 case Opt_gid:
208                         if (match_int(args, &option))
209                                 return 1;
210                         *gid = option;
211                         break;
212                 case Opt_pgrp:
213                         if (match_int(args, &option))
214                                 return 1;
215                         *pgrp = option;
216                         break;
217                 case Opt_minproto:
218                         if (match_int(args, &option))
219                                 return 1;
220                         *minproto = option;
221                         break;
222                 case Opt_maxproto:
223                         if (match_int(args, &option))
224                                 return 1;
225                         *maxproto = option;
226                         break;
227                 case Opt_indirect:
228                         set_autofs_type_indirect(type);
229                         break;
230                 case Opt_direct:
231                         set_autofs_type_direct(type);
232                         break;
233                 case Opt_offset:
234                         set_autofs_type_offset(type);
235                         break;
236                 default:
237                         return 1;
238                 }
239         }
240         return (*pipefd < 0);
241 }
242
243 static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
244 {
245         struct autofs_info *ino;
246
247         ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
248         if (!ino)
249                 return NULL;
250
251         return ino;
252 }
253
254 static const struct dentry_operations autofs4_sb_dentry_operations = {
255         .d_automount    = autofs4_d_automount,
256         .d_manage       = autofs4_d_manage,
257         .d_release      = autofs4_dentry_release,
258 };
259
260 int autofs4_fill_super(struct super_block *s, void *data, int silent)
261 {
262         struct inode * root_inode;
263         struct dentry * root;
264         struct file * pipe;
265         int pipefd;
266         struct autofs_sb_info *sbi;
267         struct autofs_info *ino;
268
269         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
270         if (!sbi)
271                 goto fail_unlock;
272         DPRINTK("starting up, sbi = %p",sbi);
273
274         s->s_fs_info = sbi;
275         sbi->magic = AUTOFS_SBI_MAGIC;
276         sbi->pipefd = -1;
277         sbi->pipe = NULL;
278         sbi->catatonic = 1;
279         sbi->exp_timeout = 0;
280         sbi->oz_pgrp = task_pgrp_nr(current);
281         sbi->sb = s;
282         sbi->version = 0;
283         sbi->sub_version = 0;
284         set_autofs_type_indirect(&sbi->type);
285         sbi->min_proto = 0;
286         sbi->max_proto = 0;
287         mutex_init(&sbi->wq_mutex);
288         spin_lock_init(&sbi->fs_lock);
289         sbi->queues = NULL;
290         spin_lock_init(&sbi->lookup_lock);
291         INIT_LIST_HEAD(&sbi->active_list);
292         INIT_LIST_HEAD(&sbi->expiring_list);
293         s->s_blocksize = 1024;
294         s->s_blocksize_bits = 10;
295         s->s_magic = AUTOFS_SUPER_MAGIC;
296         s->s_op = &autofs4_sops;
297         s->s_time_gran = 1;
298
299         /*
300          * Get the root inode and dentry, but defer checking for errors.
301          */
302         ino = autofs4_mkroot(sbi);
303         if (!ino)
304                 goto fail_free;
305         root_inode = autofs4_get_inode(s, ino);
306         if (!root_inode)
307                 goto fail_ino;
308
309         root = d_alloc_root(root_inode);
310         if (!root)
311                 goto fail_iput;
312         pipe = NULL;
313
314         d_set_d_op(root, &autofs4_sb_dentry_operations);
315         root->d_fsdata = ino;
316
317         /* Can this call block? */
318         if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
319                                 &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
320                                 &sbi->max_proto)) {
321                 printk("autofs: called with bogus options\n");
322                 goto fail_dput;
323         }
324
325         if (autofs_type_trigger(sbi->type))
326                 __managed_dentry_set_managed(root);
327
328         root_inode->i_fop = &autofs4_root_operations;
329         root_inode->i_op = autofs_type_trigger(sbi->type) ?
330                         &autofs4_direct_root_inode_operations :
331                         &autofs4_indirect_root_inode_operations;
332
333         /* Couldn't this be tested earlier? */
334         if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
335             sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
336                 printk("autofs: kernel does not match daemon version "
337                        "daemon (%d, %d) kernel (%d, %d)\n",
338                         sbi->min_proto, sbi->max_proto,
339                         AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
340                 goto fail_dput;
341         }
342
343         /* Establish highest kernel protocol version */
344         if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
345                 sbi->version = AUTOFS_MAX_PROTO_VERSION;
346         else
347                 sbi->version = sbi->max_proto;
348         sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
349
350         DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
351         pipe = fget(pipefd);
352         
353         if (!pipe) {
354                 printk("autofs: could not open pipe file descriptor\n");
355                 goto fail_dput;
356         }
357         if (!pipe->f_op || !pipe->f_op->write)
358                 goto fail_fput;
359         sbi->pipe = pipe;
360         sbi->pipefd = pipefd;
361         sbi->catatonic = 0;
362
363         /*
364          * Success! Install the root dentry now to indicate completion.
365          */
366         s->s_root = root;
367         return 0;
368         
369         /*
370          * Failure ... clean up.
371          */
372 fail_fput:
373         printk("autofs: pipe file descriptor does not contain proper ops\n");
374         fput(pipe);
375         /* fall through */
376 fail_dput:
377         dput(root);
378         goto fail_free;
379 fail_iput:
380         printk("autofs: get root dentry failed\n");
381         iput(root_inode);
382 fail_ino:
383         kfree(ino);
384 fail_free:
385         kfree(sbi);
386         s->s_fs_info = NULL;
387 fail_unlock:
388         return -EINVAL;
389 }
390
391 struct inode *autofs4_get_inode(struct super_block *sb,
392                                 struct autofs_info *inf)
393 {
394         struct inode *inode = new_inode(sb);
395
396         if (inode == NULL)
397                 return NULL;
398
399         inf->inode = inode;
400         inode->i_mode = inf->mode;
401         if (sb->s_root) {
402                 inode->i_uid = sb->s_root->d_inode->i_uid;
403                 inode->i_gid = sb->s_root->d_inode->i_gid;
404         }
405         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
406         inode->i_ino = get_next_ino();
407
408         if (S_ISDIR(inf->mode)) {
409                 inode->i_nlink = 2;
410                 inode->i_op = &autofs4_dir_inode_operations;
411                 inode->i_fop = &autofs4_dir_operations;
412         } else if (S_ISLNK(inf->mode)) {
413                 inode->i_size = inf->size;
414                 inode->i_op = &autofs4_symlink_inode_operations;
415         }
416
417         return inode;
418 }