NFSv4.1: Reinitialise sequence results before retransmitting a request
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / fs / autofs4 / inode.c
1 /*
2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 * Copyright 2005-2006 Ian Kent <raven@themaw.net>
4 *
5 * This file is part of the Linux kernel and is made available under
6 * the terms of the GNU General Public License, version 2, or at your
7 * option, any later version, incorporated herein by reference.
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/file.h>
13 #include <linux/seq_file.h>
14 #include <linux/pagemap.h>
15 #include <linux/parser.h>
16 #include <linux/bitops.h>
17 #include "autofs_i.h"
18 #include <linux/module.h>
19
20 struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
21 {
22 struct autofs_info *ino;
23
24 ino = kzalloc(sizeof(*ino), GFP_KERNEL);
25 if (ino) {
26 INIT_LIST_HEAD(&ino->active);
27 INIT_LIST_HEAD(&ino->expiring);
28 ino->last_used = jiffies;
29 ino->sbi = sbi;
30 }
31 return ino;
32 }
33
34 void autofs4_clean_ino(struct autofs_info *ino)
35 {
36 ino->uid = GLOBAL_ROOT_UID;
37 ino->gid = GLOBAL_ROOT_GID;
38 ino->last_used = jiffies;
39 }
40
41 void autofs4_free_ino(struct autofs_info *ino)
42 {
43 kfree(ino);
44 }
45
46 void autofs4_kill_sb(struct super_block *sb)
47 {
48 struct autofs_sb_info *sbi = autofs4_sbi(sb);
49
50 /*
51 * In the event of a failure in get_sb_nodev the superblock
52 * info is not present so nothing else has been setup, so
53 * just call kill_anon_super when we are called from
54 * deactivate_super.
55 */
56 if (sbi) {
57 /* Free wait queues, close pipe */
58 autofs4_catatonic_mode(sbi);
59 put_pid(sbi->oz_pgrp);
60 }
61
62 pr_debug("shutting down\n");
63 kill_litter_super(sb);
64 if (sbi)
65 kfree_rcu(sbi, rcu);
66 }
67
68 static int autofs4_show_options(struct seq_file *m, struct dentry *root)
69 {
70 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
71 struct inode *root_inode = d_inode(root->d_sb->s_root);
72
73 if (!sbi)
74 return 0;
75
76 seq_printf(m, ",fd=%d", sbi->pipefd);
77 if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
78 seq_printf(m, ",uid=%u",
79 from_kuid_munged(&init_user_ns, root_inode->i_uid));
80 if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
81 seq_printf(m, ",gid=%u",
82 from_kgid_munged(&init_user_ns, root_inode->i_gid));
83 seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp));
84 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
85 seq_printf(m, ",minproto=%d", sbi->min_proto);
86 seq_printf(m, ",maxproto=%d", sbi->max_proto);
87
88 if (autofs_type_offset(sbi->type))
89 seq_printf(m, ",offset");
90 else if (autofs_type_direct(sbi->type))
91 seq_printf(m, ",direct");
92 else
93 seq_printf(m, ",indirect");
94 #ifdef CONFIG_CHECKPOINT_RESTORE
95 if (sbi->pipe)
96 seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino);
97 else
98 seq_printf(m, ",pipe_ino=-1");
99 #endif
100 return 0;
101 }
102
103 static void autofs4_evict_inode(struct inode *inode)
104 {
105 clear_inode(inode);
106 kfree(inode->i_private);
107 }
108
109 static const struct super_operations autofs4_sops = {
110 .statfs = simple_statfs,
111 .show_options = autofs4_show_options,
112 .evict_inode = autofs4_evict_inode,
113 };
114
115 enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
116 Opt_indirect, Opt_direct, Opt_offset};
117
118 static const match_table_t tokens = {
119 {Opt_fd, "fd=%u"},
120 {Opt_uid, "uid=%u"},
121 {Opt_gid, "gid=%u"},
122 {Opt_pgrp, "pgrp=%u"},
123 {Opt_minproto, "minproto=%u"},
124 {Opt_maxproto, "maxproto=%u"},
125 {Opt_indirect, "indirect"},
126 {Opt_direct, "direct"},
127 {Opt_offset, "offset"},
128 {Opt_err, NULL}
129 };
130
131 static int parse_options(char *options, int *pipefd, kuid_t *uid, kgid_t *gid,
132 int *pgrp, bool *pgrp_set, unsigned int *type,
133 int *minproto, int *maxproto)
134 {
135 char *p;
136 substring_t args[MAX_OPT_ARGS];
137 int option;
138
139 *uid = current_uid();
140 *gid = current_gid();
141
142 *minproto = AUTOFS_MIN_PROTO_VERSION;
143 *maxproto = AUTOFS_MAX_PROTO_VERSION;
144
145 *pipefd = -1;
146
147 if (!options)
148 return 1;
149
150 while ((p = strsep(&options, ",")) != NULL) {
151 int token;
152
153 if (!*p)
154 continue;
155
156 token = match_token(p, tokens, args);
157 switch (token) {
158 case Opt_fd:
159 if (match_int(args, pipefd))
160 return 1;
161 break;
162 case Opt_uid:
163 if (match_int(args, &option))
164 return 1;
165 *uid = make_kuid(current_user_ns(), option);
166 if (!uid_valid(*uid))
167 return 1;
168 break;
169 case Opt_gid:
170 if (match_int(args, &option))
171 return 1;
172 *gid = make_kgid(current_user_ns(), option);
173 if (!gid_valid(*gid))
174 return 1;
175 break;
176 case Opt_pgrp:
177 if (match_int(args, &option))
178 return 1;
179 *pgrp = option;
180 *pgrp_set = true;
181 break;
182 case Opt_minproto:
183 if (match_int(args, &option))
184 return 1;
185 *minproto = option;
186 break;
187 case Opt_maxproto:
188 if (match_int(args, &option))
189 return 1;
190 *maxproto = option;
191 break;
192 case Opt_indirect:
193 set_autofs_type_indirect(type);
194 break;
195 case Opt_direct:
196 set_autofs_type_direct(type);
197 break;
198 case Opt_offset:
199 set_autofs_type_offset(type);
200 break;
201 default:
202 return 1;
203 }
204 }
205 return (*pipefd < 0);
206 }
207
208 int autofs4_fill_super(struct super_block *s, void *data, int silent)
209 {
210 struct inode *root_inode;
211 struct dentry *root;
212 struct file *pipe;
213 int pipefd;
214 struct autofs_sb_info *sbi;
215 struct autofs_info *ino;
216 int pgrp = 0;
217 bool pgrp_set = false;
218 int ret = -EINVAL;
219
220 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
221 if (!sbi)
222 return -ENOMEM;
223 pr_debug("starting up, sbi = %p\n", sbi);
224
225 s->s_fs_info = sbi;
226 sbi->magic = AUTOFS_SBI_MAGIC;
227 sbi->pipefd = -1;
228 sbi->pipe = NULL;
229 sbi->catatonic = 1;
230 sbi->exp_timeout = 0;
231 sbi->oz_pgrp = NULL;
232 sbi->sb = s;
233 sbi->version = 0;
234 sbi->sub_version = 0;
235 set_autofs_type_indirect(&sbi->type);
236 sbi->min_proto = 0;
237 sbi->max_proto = 0;
238 mutex_init(&sbi->wq_mutex);
239 mutex_init(&sbi->pipe_mutex);
240 spin_lock_init(&sbi->fs_lock);
241 sbi->queues = NULL;
242 spin_lock_init(&sbi->lookup_lock);
243 INIT_LIST_HEAD(&sbi->active_list);
244 INIT_LIST_HEAD(&sbi->expiring_list);
245 s->s_blocksize = 1024;
246 s->s_blocksize_bits = 10;
247 s->s_magic = AUTOFS_SUPER_MAGIC;
248 s->s_op = &autofs4_sops;
249 s->s_d_op = &autofs4_dentry_operations;
250 s->s_time_gran = 1;
251
252 /*
253 * Get the root inode and dentry, but defer checking for errors.
254 */
255 ino = autofs4_new_ino(sbi);
256 if (!ino) {
257 ret = -ENOMEM;
258 goto fail_free;
259 }
260 root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
261 root = d_make_root(root_inode);
262 if (!root) {
263 ret = -ENOMEM;
264 goto fail_ino;
265 }
266 pipe = NULL;
267
268 root->d_fsdata = ino;
269
270 /* Can this call block? */
271 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
272 &pgrp, &pgrp_set, &sbi->type, &sbi->min_proto,
273 &sbi->max_proto)) {
274 pr_err("called with bogus options\n");
275 goto fail_dput;
276 }
277
278 /* Test versions first */
279 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
280 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
281 pr_err("kernel does not match daemon version "
282 "daemon (%d, %d) kernel (%d, %d)\n",
283 sbi->min_proto, sbi->max_proto,
284 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
285 goto fail_dput;
286 }
287
288 /* Establish highest kernel protocol version */
289 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
290 sbi->version = AUTOFS_MAX_PROTO_VERSION;
291 else
292 sbi->version = sbi->max_proto;
293 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
294
295 if (pgrp_set) {
296 sbi->oz_pgrp = find_get_pid(pgrp);
297 if (!sbi->oz_pgrp) {
298 pr_err("could not find process group %d\n",
299 pgrp);
300 goto fail_dput;
301 }
302 } else {
303 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
304 }
305
306 if (autofs_type_trigger(sbi->type))
307 __managed_dentry_set_managed(root);
308
309 root_inode->i_fop = &autofs4_root_operations;
310 root_inode->i_op = &autofs4_dir_inode_operations;
311
312 pr_debug("pipe fd = %d, pgrp = %u\n", pipefd, pid_nr(sbi->oz_pgrp));
313 pipe = fget(pipefd);
314
315 if (!pipe) {
316 pr_err("could not open pipe file descriptor\n");
317 goto fail_put_pid;
318 }
319 ret = autofs_prepare_pipe(pipe);
320 if (ret < 0)
321 goto fail_fput;
322 sbi->pipe = pipe;
323 sbi->pipefd = pipefd;
324 sbi->catatonic = 0;
325
326 /*
327 * Success! Install the root dentry now to indicate completion.
328 */
329 s->s_root = root;
330 return 0;
331
332 /*
333 * Failure ... clean up.
334 */
335 fail_fput:
336 pr_err("pipe file descriptor does not contain proper ops\n");
337 fput(pipe);
338 fail_put_pid:
339 put_pid(sbi->oz_pgrp);
340 fail_dput:
341 dput(root);
342 goto fail_free;
343 fail_ino:
344 autofs4_free_ino(ino);
345 fail_free:
346 kfree(sbi);
347 s->s_fs_info = NULL;
348 return ret;
349 }
350
351 struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode)
352 {
353 struct inode *inode = new_inode(sb);
354
355 if (inode == NULL)
356 return NULL;
357
358 inode->i_mode = mode;
359 if (sb->s_root) {
360 inode->i_uid = d_inode(sb->s_root)->i_uid;
361 inode->i_gid = d_inode(sb->s_root)->i_gid;
362 }
363 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
364 inode->i_ino = get_next_ino();
365
366 if (S_ISDIR(mode)) {
367 set_nlink(inode, 2);
368 inode->i_op = &autofs4_dir_inode_operations;
369 inode->i_fop = &autofs4_dir_operations;
370 } else if (S_ISLNK(mode)) {
371 inode->i_op = &autofs4_symlink_inode_operations;
372 } else
373 WARN_ON(1);
374
375 return inode;
376 }