blob: 4d3e274207fb7aa05aa320b957a03911984cf67d [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS superblock handling
2 *
David Howells08e0e7c2007-04-26 15:55:03 -07003 * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This software may be freely redistributed under the terms of the
6 * GNU General Public License.
7 *
8 * You should have received a copy of the GNU General Public License
9 * along with this program; if not, write to the Free Software
10 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
11 *
12 * Authors: David Howells <dhowells@redhat.com>
David Woodhouse44d1b982008-06-05 22:46:18 -070013 * David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
wangleibec5eb62010-08-11 09:38:04 +010019#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/slab.h>
22#include <linux/fs.h>
23#include <linux/pagemap.h>
David Howells80c72fe2007-05-03 03:11:29 -070024#include <linux/parser.h>
David Howells45222b92007-05-10 22:22:20 -070025#include <linux/statfs.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Eric W. Biedermanf74f70f2013-01-31 04:23:54 -080027#include <linux/nsproxy.h>
David Howellsf044c882017-11-02 15:27:45 +000028#include <linux/magic.h>
Eric W. Biedermanf74f70f2013-01-31 04:23:54 -080029#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "internal.h"
31
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070032static void afs_i_init_once(void *foo);
Al Virof7442b32010-07-26 14:16:21 +040033static struct dentry *afs_mount(struct file_system_type *fs_type,
34 int flags, const char *dev_name, void *data);
Al Virodde194a2011-06-12 16:01:21 -040035static void afs_kill_super(struct super_block *sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static struct inode *afs_alloc_inode(struct super_block *sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static void afs_destroy_inode(struct inode *inode);
David Howells45222b92007-05-10 22:22:20 -070038static int afs_statfs(struct dentry *dentry, struct kstatfs *buf);
David Howells677018a2017-07-05 16:25:23 +010039static int afs_show_devname(struct seq_file *m, struct dentry *root);
40static int afs_show_options(struct seq_file *m, struct dentry *root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -040042struct file_system_type afs_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .owner = THIS_MODULE,
44 .name = "afs",
Al Virof7442b32010-07-26 14:16:21 +040045 .mount = afs_mount,
Al Virodde194a2011-06-12 16:01:21 -040046 .kill_sb = afs_kill_super,
David Howells80c72fe2007-05-03 03:11:29 -070047 .fs_flags = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
Eric W. Biederman7f78e032013-03-02 19:39:14 -080049MODULE_ALIAS_FS("afs");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
David Howells5b86d4f2018-05-18 11:46:15 +010051int afs_net_id;
52
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080053static const struct super_operations afs_super_ops = {
David Howells45222b92007-05-10 22:22:20 -070054 .statfs = afs_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .alloc_inode = afs_alloc_inode,
wangleibec5eb62010-08-11 09:38:04 +010056 .drop_inode = afs_drop_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .destroy_inode = afs_destroy_inode,
Al Virob57922d2010-06-07 14:34:48 -040058 .evict_inode = afs_evict_inode,
David Howells677018a2017-07-05 16:25:23 +010059 .show_devname = afs_show_devname,
60 .show_options = afs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Christoph Lametere18b8902006-12-06 20:33:20 -080063static struct kmem_cache *afs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static atomic_t afs_count_active_inodes;
65
David Howells80c72fe2007-05-03 03:11:29 -070066enum {
67 afs_no_opt,
68 afs_opt_cell,
David Howells4d673da2018-02-06 06:26:30 +000069 afs_opt_dyn,
David Howells80c72fe2007-05-03 03:11:29 -070070 afs_opt_rwpath,
71 afs_opt_vol,
wangleibec5eb62010-08-11 09:38:04 +010072 afs_opt_autocell,
David Howells80c72fe2007-05-03 03:11:29 -070073};
74
Steven Whitehousea447c092008-10-13 10:46:57 +010075static const match_table_t afs_options_list = {
David Howells80c72fe2007-05-03 03:11:29 -070076 { afs_opt_cell, "cell=%s" },
David Howells4d673da2018-02-06 06:26:30 +000077 { afs_opt_dyn, "dyn" },
David Howells80c72fe2007-05-03 03:11:29 -070078 { afs_opt_rwpath, "rwpath" },
79 { afs_opt_vol, "vol=%s" },
wangleibec5eb62010-08-11 09:38:04 +010080 { afs_opt_autocell, "autocell" },
David Howells80c72fe2007-05-03 03:11:29 -070081 { afs_no_opt, NULL },
82};
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * initialise the filesystem
86 */
87int __init afs_fs_init(void)
88{
89 int ret;
90
91 _enter("");
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* create ourselves an inode cache */
94 atomic_set(&afs_count_active_inodes, 0);
95
96 ret = -ENOMEM;
97 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
98 sizeof(struct afs_vnode),
99 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -0800100 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
Paul Mundt20c2df82007-07-20 10:11:58 +0900101 afs_i_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!afs_inode_cachep) {
103 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
104 return ret;
105 }
106
107 /* now export our filesystem to lesser mortals */
108 ret = register_filesystem(&afs_fs_type);
109 if (ret < 0) {
110 kmem_cache_destroy(afs_inode_cachep);
David Howells08e0e7c2007-04-26 15:55:03 -0700111 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return ret;
113 }
114
David Howells08e0e7c2007-04-26 15:55:03 -0700115 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
David Howellsec268152007-04-26 15:49:28 -0700117}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * clean up the filesystem
121 */
David Howells5b86d4f2018-05-18 11:46:15 +0100122void afs_fs_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
David Howells08e0e7c2007-04-26 15:55:03 -0700124 _enter("");
125
126 afs_mntpt_kill_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 unregister_filesystem(&afs_fs_type);
128
129 if (atomic_read(&afs_count_active_inodes) != 0) {
130 printk("kAFS: %d active inode objects still present\n",
131 atomic_read(&afs_count_active_inodes));
132 BUG();
133 }
134
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000135 /*
136 * Make sure all delayed rcu free inodes are flushed before we
137 * destroy cache.
138 */
139 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 kmem_cache_destroy(afs_inode_cachep);
David Howells08e0e7c2007-04-26 15:55:03 -0700141 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
David Howells677018a2017-07-05 16:25:23 +0100145 * Display the mount device name in /proc/mounts.
146 */
147static int afs_show_devname(struct seq_file *m, struct dentry *root)
148{
David Howellsd2ddc772017-11-02 15:27:50 +0000149 struct afs_super_info *as = AFS_FS_S(root->d_sb);
David Howells677018a2017-07-05 16:25:23 +0100150 struct afs_volume *volume = as->volume;
David Howellsd2ddc772017-11-02 15:27:50 +0000151 struct afs_cell *cell = as->cell;
David Howells677018a2017-07-05 16:25:23 +0100152 const char *suf = "";
153 char pref = '%';
154
David Howells4d673da2018-02-06 06:26:30 +0000155 if (as->dyn_root) {
156 seq_puts(m, "none");
157 return 0;
158 }
David Howells66c7e1d2018-04-06 14:17:25 +0100159
David Howells677018a2017-07-05 16:25:23 +0100160 switch (volume->type) {
161 case AFSVL_RWVOL:
162 break;
163 case AFSVL_ROVOL:
164 pref = '#';
165 if (volume->type_force)
166 suf = ".readonly";
167 break;
168 case AFSVL_BACKVOL:
169 pref = '#';
170 suf = ".backup";
171 break;
172 }
173
David Howellsd2ddc772017-11-02 15:27:50 +0000174 seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->name, suf);
David Howells677018a2017-07-05 16:25:23 +0100175 return 0;
176}
177
178/*
179 * Display the mount options in /proc/mounts.
180 */
181static int afs_show_options(struct seq_file *m, struct dentry *root)
182{
David Howells4d673da2018-02-06 06:26:30 +0000183 struct afs_super_info *as = AFS_FS_S(root->d_sb);
184
185 if (as->dyn_root)
186 seq_puts(m, ",dyn");
David Howells677018a2017-07-05 16:25:23 +0100187 if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
David Howells4d673da2018-02-06 06:26:30 +0000188 seq_puts(m, ",autocell");
David Howells677018a2017-07-05 16:25:23 +0100189 return 0;
190}
191
192/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * parse the mount options
194 * - this function has been shamelessly adapted from the ext3 fs which
195 * shamelessly adapted it from the msdos fs
196 */
David Howells00d3b7a2007-04-26 15:57:07 -0700197static int afs_parse_options(struct afs_mount_params *params,
198 char *options, const char **devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Howells08e0e7c2007-04-26 15:55:03 -0700200 struct afs_cell *cell;
David Howells80c72fe2007-05-03 03:11:29 -0700201 substring_t args[MAX_OPT_ARGS];
202 char *p;
203 int token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 _enter("%s", options);
206
207 options[PAGE_SIZE - 1] = 0;
208
David Howells80c72fe2007-05-03 03:11:29 -0700209 while ((p = strsep(&options, ","))) {
210 if (!*p)
211 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
David Howells80c72fe2007-05-03 03:11:29 -0700213 token = match_token(p, afs_options_list, args);
214 switch (token) {
215 case afs_opt_cell:
David Howells989782d2017-11-02 15:27:50 +0000216 rcu_read_lock();
217 cell = afs_lookup_cell_rcu(params->net,
218 args[0].from,
219 args[0].to - args[0].from);
220 rcu_read_unlock();
David Howells08e0e7c2007-04-26 15:55:03 -0700221 if (IS_ERR(cell))
222 return PTR_ERR(cell);
David Howells9ed900b2017-11-02 15:27:46 +0000223 afs_put_cell(params->net, params->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700224 params->cell = cell;
David Howells80c72fe2007-05-03 03:11:29 -0700225 break;
226
227 case afs_opt_rwpath:
David Howells4d673da2018-02-06 06:26:30 +0000228 params->rwpath = true;
David Howells80c72fe2007-05-03 03:11:29 -0700229 break;
230
231 case afs_opt_vol:
232 *devname = args[0].from;
233 break;
234
wangleibec5eb62010-08-11 09:38:04 +0100235 case afs_opt_autocell:
David Howells4d673da2018-02-06 06:26:30 +0000236 params->autocell = true;
237 break;
238
239 case afs_opt_dyn:
240 params->dyn_root = true;
wangleibec5eb62010-08-11 09:38:04 +0100241 break;
242
David Howells80c72fe2007-05-03 03:11:29 -0700243 default:
244 printk(KERN_ERR "kAFS:"
245 " Unknown or invalid mount option: '%s'\n", p);
246 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
David Howells80c72fe2007-05-03 03:11:29 -0700250 _leave(" = 0");
251 return 0;
David Howellsec268152007-04-26 15:49:28 -0700252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/*
David Howells00d3b7a2007-04-26 15:57:07 -0700255 * parse a device name to get cell name, volume name, volume type and R/W
256 * selector
257 * - this can be one of the following:
258 * "%[cell:]volume[.]" R/W volume
259 * "#[cell:]volume[.]" R/O or R/W volume (rwpath=0),
260 * or R/W (rwpath=1) volume
261 * "%[cell:]volume.readonly" R/O volume
262 * "#[cell:]volume.readonly" R/O volume
263 * "%[cell:]volume.backup" Backup volume
264 * "#[cell:]volume.backup" Backup volume
265 */
266static int afs_parse_device_name(struct afs_mount_params *params,
267 const char *name)
268{
269 struct afs_cell *cell;
270 const char *cellname, *suffix;
271 int cellnamesz;
272
273 _enter(",%s", name);
David Howells66c7e1d2018-04-06 14:17:25 +0100274
David Howells00d3b7a2007-04-26 15:57:07 -0700275 if (!name) {
276 printk(KERN_ERR "kAFS: no volume name specified\n");
277 return -EINVAL;
278 }
279
280 if ((name[0] != '%' && name[0] != '#') || !name[1]) {
281 printk(KERN_ERR "kAFS: unparsable volume name\n");
282 return -EINVAL;
283 }
284
285 /* determine the type of volume we're looking for */
286 params->type = AFSVL_ROVOL;
287 params->force = false;
288 if (params->rwpath || name[0] == '%') {
289 params->type = AFSVL_RWVOL;
290 params->force = true;
291 }
292 name++;
293
294 /* split the cell name out if there is one */
295 params->volname = strchr(name, ':');
296 if (params->volname) {
297 cellname = name;
298 cellnamesz = params->volname - name;
299 params->volname++;
300 } else {
301 params->volname = name;
302 cellname = NULL;
303 cellnamesz = 0;
304 }
305
306 /* the volume type is further affected by a possible suffix */
307 suffix = strrchr(params->volname, '.');
308 if (suffix) {
309 if (strcmp(suffix, ".readonly") == 0) {
310 params->type = AFSVL_ROVOL;
311 params->force = true;
312 } else if (strcmp(suffix, ".backup") == 0) {
313 params->type = AFSVL_BACKVOL;
314 params->force = true;
315 } else if (suffix[1] == 0) {
316 } else {
317 suffix = NULL;
318 }
319 }
320
321 params->volnamesz = suffix ?
322 suffix - params->volname : strlen(params->volname);
323
324 _debug("cell %*.*s [%p]",
325 cellnamesz, cellnamesz, cellname ?: "", params->cell);
326
327 /* lookup the cell record */
328 if (cellname || !params->cell) {
David Howells989782d2017-11-02 15:27:50 +0000329 cell = afs_lookup_cell(params->net, cellname, cellnamesz,
330 NULL, false);
David Howells00d3b7a2007-04-26 15:57:07 -0700331 if (IS_ERR(cell)) {
wangleibec5eb62010-08-11 09:38:04 +0100332 printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
333 cellnamesz, cellnamesz, cellname ?: "");
David Howells00d3b7a2007-04-26 15:57:07 -0700334 return PTR_ERR(cell);
335 }
David Howells9ed900b2017-11-02 15:27:46 +0000336 afs_put_cell(params->net, params->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700337 params->cell = cell;
338 }
339
340 _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
341 params->cell->name, params->cell,
342 params->volnamesz, params->volnamesz, params->volname,
343 suffix ?: "-", params->type, params->force ? " FORCE" : "");
344
345 return 0;
346}
347
348/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * check a superblock to see if it's the one we're looking for
350 */
351static int afs_test_super(struct super_block *sb, void *data)
352{
Al Virodde194a2011-06-12 16:01:21 -0400353 struct afs_super_info *as1 = data;
David Howellsd2ddc772017-11-02 15:27:50 +0000354 struct afs_super_info *as = AFS_FS_S(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
David Howells5b86d4f2018-05-18 11:46:15 +0100356 return (as->net_ns == as1->net_ns &&
David Howells4d673da2018-02-06 06:26:30 +0000357 as->volume &&
David Howells0da0b7f2018-06-15 15:19:22 +0100358 as->volume->vid == as1->volume->vid &&
359 !as->dyn_root);
David Howells4d673da2018-02-06 06:26:30 +0000360}
361
362static int afs_dynroot_test_super(struct super_block *sb, void *data)
363{
David Howells0da0b7f2018-06-15 15:19:22 +0100364 struct afs_super_info *as1 = data;
365 struct afs_super_info *as = AFS_FS_S(sb);
366
367 return (as->net_ns == as1->net_ns &&
368 as->dyn_root);
Al Virodde194a2011-06-12 16:01:21 -0400369}
370
371static int afs_set_super(struct super_block *sb, void *data)
372{
David Howellsd2ddc772017-11-02 15:27:50 +0000373 struct afs_super_info *as = data;
374
375 sb->s_fs_info = as;
Al Virodde194a2011-06-12 16:01:21 -0400376 return set_anon_super(sb, NULL);
David Howellsec268152007-04-26 15:49:28 -0700377}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/*
380 * fill in the superblock
381 */
Al Virodde194a2011-06-12 16:01:21 -0400382static int afs_fill_super(struct super_block *sb,
383 struct afs_mount_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
David Howellsd2ddc772017-11-02 15:27:50 +0000385 struct afs_super_info *as = AFS_FS_S(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct afs_fid fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct inode *inode = NULL;
388 int ret;
389
David Howells08e0e7c2007-04-26 15:55:03 -0700390 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* fill in the superblock */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300393 sb->s_blocksize = PAGE_SIZE;
394 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sb->s_magic = AFS_FS_MAGIC;
396 sb->s_op = &afs_super_ops;
David Howells4d673da2018-02-06 06:26:30 +0000397 if (!as->dyn_root)
398 sb->s_xattr = afs_xattr_handlers;
Jan Karaedd3ba92017-04-12 12:24:36 +0200399 ret = super_setup_bdi(sb);
400 if (ret)
401 return ret;
402 sb->s_bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* allocate the root inode and dentry */
David Howells4d673da2018-02-06 06:26:30 +0000405 if (as->dyn_root) {
406 inode = afs_iget_pseudo_dir(sb, true);
407 sb->s_flags |= SB_RDONLY;
408 } else {
409 sprintf(sb->s_id, "%u", as->volume->vid);
410 afs_activate_volume(as->volume);
411 fid.vid = as->volume->vid;
412 fid.vnode = 1;
413 fid.unique = 1;
414 inode = afs_iget(sb, params->key, &fid, NULL, NULL, NULL);
415 }
416
David Howells08e0e7c2007-04-26 15:55:03 -0700417 if (IS_ERR(inode))
Al Virodde194a2011-06-12 16:01:21 -0400418 return PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
David Howells4d673da2018-02-06 06:26:30 +0000420 if (params->autocell || params->dyn_root)
wangleibec5eb62010-08-11 09:38:04 +0100421 set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -0500424 sb->s_root = d_make_root(inode);
425 if (!sb->s_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto error;
427
David Howells0da0b7f2018-06-15 15:19:22 +0100428 if (as->dyn_root) {
David Howells66c7e1d2018-04-06 14:17:25 +0100429 sb->s_d_op = &afs_dynroot_dentry_operations;
David Howells0da0b7f2018-06-15 15:19:22 +0100430 ret = afs_dynroot_populate(sb);
431 if (ret < 0)
432 goto error;
433 } else {
David Howells66c7e1d2018-04-06 14:17:25 +0100434 sb->s_d_op = &afs_fs_dentry_operations;
David Howells0da0b7f2018-06-15 15:19:22 +0100435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
David Howells08e0e7c2007-04-26 15:55:03 -0700437 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439
David Howellsec268152007-04-26 15:49:28 -0700440error:
David Howells08e0e7c2007-04-26 15:55:03 -0700441 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return ret;
David Howellsec268152007-04-26 15:49:28 -0700443}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
David Howells49566f62017-11-02 15:27:46 +0000445static struct afs_super_info *afs_alloc_sbi(struct afs_mount_params *params)
446{
447 struct afs_super_info *as;
448
449 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
450 if (as) {
David Howells5b86d4f2018-05-18 11:46:15 +0100451 as->net_ns = get_net(params->net_ns);
David Howells4d673da2018-02-06 06:26:30 +0000452 if (params->dyn_root)
453 as->dyn_root = true;
454 else
455 as->cell = afs_get_cell(params->cell);
David Howells49566f62017-11-02 15:27:46 +0000456 }
457 return as;
458}
459
460static void afs_destroy_sbi(struct afs_super_info *as)
461{
462 if (as) {
David Howells9ed900b2017-11-02 15:27:46 +0000463 afs_put_volume(as->cell, as->volume);
David Howells5b86d4f2018-05-18 11:46:15 +0100464 afs_put_cell(afs_net(as->net_ns), as->cell);
465 put_net(as->net_ns);
David Howells49566f62017-11-02 15:27:46 +0000466 kfree(as);
467 }
468}
469
David Howells0da0b7f2018-06-15 15:19:22 +0100470static void afs_kill_super(struct super_block *sb)
471{
472 struct afs_super_info *as = AFS_FS_S(sb);
473 struct afs_net *net = afs_net(as->net_ns);
474
475 if (as->dyn_root)
476 afs_dynroot_depopulate(sb);
477
478 /* Clear the callback interests (which will do ilookup5) before
479 * deactivating the superblock.
480 */
481 if (as->volume)
482 afs_clear_callback_interests(net, as->volume->servers);
483 kill_anon_super(sb);
484 if (as->volume)
485 afs_deactivate_volume(as->volume);
486 afs_destroy_sbi(as);
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
490 * get an AFS superblock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
Al Virof7442b32010-07-26 14:16:21 +0400492static struct dentry *afs_mount(struct file_system_type *fs_type,
David Howells49566f62017-11-02 15:27:46 +0000493 int flags, const char *dev_name, void *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct afs_mount_params params;
496 struct super_block *sb;
David Howellsd2ddc772017-11-02 15:27:50 +0000497 struct afs_volume *candidate;
David Howells00d3b7a2007-04-26 15:57:07 -0700498 struct key *key;
Al Virodde194a2011-06-12 16:01:21 -0400499 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 int ret;
501
502 _enter(",,%s,%p", dev_name, options);
503
504 memset(&params, 0, sizeof(params));
505
Eric W. Biedermanf74f70f2013-01-31 04:23:54 -0800506 ret = -EINVAL;
507 if (current->nsproxy->net_ns != &init_net)
508 goto error;
David Howells5b86d4f2018-05-18 11:46:15 +0100509 params.net_ns = current->nsproxy->net_ns;
510 params.net = afs_net(params.net_ns);
511
David Howells00d3b7a2007-04-26 15:57:07 -0700512 /* parse the options and device name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (options) {
David Howells00d3b7a2007-04-26 15:57:07 -0700514 ret = afs_parse_options(&params, options, &dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (ret < 0)
516 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
David Howells4d673da2018-02-06 06:26:30 +0000519 if (!params.dyn_root) {
520 ret = afs_parse_device_name(&params, dev_name);
521 if (ret < 0)
522 goto error;
David Howells00d3b7a2007-04-26 15:57:07 -0700523
David Howells4d673da2018-02-06 06:26:30 +0000524 /* try and do the mount securely */
525 key = afs_request_key(params.cell);
526 if (IS_ERR(key)) {
527 _leave(" = %ld [key]", PTR_ERR(key));
528 ret = PTR_ERR(key);
529 goto error;
530 }
531 params.key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700532 }
David Howells00d3b7a2007-04-26 15:57:07 -0700533
David Howells49566f62017-11-02 15:27:46 +0000534 /* allocate a superblock info record */
535 ret = -ENOMEM;
536 as = afs_alloc_sbi(&params);
537 if (!as)
David Howellsd2ddc772017-11-02 15:27:50 +0000538 goto error_key;
David Howells49566f62017-11-02 15:27:46 +0000539
David Howells4d673da2018-02-06 06:26:30 +0000540 if (!params.dyn_root) {
541 /* Assume we're going to need a volume record; at the very
542 * least we can use it to update the volume record if we have
543 * one already. This checks that the volume exists within the
544 * cell.
545 */
546 candidate = afs_create_volume(&params);
547 if (IS_ERR(candidate)) {
548 ret = PTR_ERR(candidate);
549 goto error_as;
550 }
551
552 as->volume = candidate;
David Howells08e0e7c2007-04-26 15:55:03 -0700553 }
David Howellsd2ddc772017-11-02 15:27:50 +0000554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* allocate a deviceless superblock */
David Howells4d673da2018-02-06 06:26:30 +0000556 sb = sget(fs_type,
557 as->dyn_root ? afs_dynroot_test_super : afs_test_super,
558 afs_set_super, flags, as);
David Howells08e0e7c2007-04-26 15:55:03 -0700559 if (IS_ERR(sb)) {
560 ret = PTR_ERR(sb);
David Howellsf044c882017-11-02 15:27:45 +0000561 goto error_as;
David Howells08e0e7c2007-04-26 15:55:03 -0700562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
David Howells436058a2007-04-26 15:56:24 -0700564 if (!sb->s_root) {
565 /* initial superblock/root creation */
566 _debug("create");
David Howells436058a2007-04-26 15:56:24 -0700567 ret = afs_fill_super(sb, &params);
David Howellsf044c882017-11-02 15:27:45 +0000568 if (ret < 0)
569 goto error_sb;
David Howells49566f62017-11-02 15:27:46 +0000570 as = NULL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800571 sb->s_flags |= SB_ACTIVE;
David Howells436058a2007-04-26 15:56:24 -0700572 } else {
573 _debug("reuse");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800574 ASSERTCMP(sb->s_flags, &, SB_ACTIVE);
David Howells49566f62017-11-02 15:27:46 +0000575 afs_destroy_sbi(as);
576 as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
David Howells9ed900b2017-11-02 15:27:46 +0000579 afs_put_cell(params.net, params.cell);
David Howells49566f62017-11-02 15:27:46 +0000580 key_put(params.key);
David Howells08e0e7c2007-04-26 15:55:03 -0700581 _leave(" = 0 [%p]", sb);
Al Virof7442b32010-07-26 14:16:21 +0400582 return dget(sb->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
David Howellsf044c882017-11-02 15:27:45 +0000584error_sb:
585 deactivate_locked_super(sb);
David Howellsd2ddc772017-11-02 15:27:50 +0000586 goto error_key;
David Howellsf044c882017-11-02 15:27:45 +0000587error_as:
David Howells49566f62017-11-02 15:27:46 +0000588 afs_destroy_sbi(as);
David Howellsd2ddc772017-11-02 15:27:50 +0000589error_key:
590 key_put(params.key);
David Howellsec268152007-04-26 15:49:28 -0700591error:
David Howells9ed900b2017-11-02 15:27:46 +0000592 afs_put_cell(params.net, params.cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 _leave(" = %d", ret);
Al Virof7442b32010-07-26 14:16:21 +0400594 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700595}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/*
David Howellsf8de4832017-12-01 11:40:43 +0000598 * Initialise an inode cache slab element prior to any use. Note that
599 * afs_alloc_inode() *must* reset anything that could incorrectly leak from one
600 * inode to another.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700602static void afs_i_init_once(void *_vnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
David Howellsec268152007-04-26 15:49:28 -0700604 struct afs_vnode *vnode = _vnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Christoph Lametera35afb82007-05-16 22:10:57 -0700606 memset(vnode, 0, sizeof(*vnode));
607 inode_init_once(&vnode->vfs_inode);
David Howellsd2ddc772017-11-02 15:27:50 +0000608 mutex_init(&vnode->io_lock);
David Howellsb61f7dc2018-04-27 20:46:22 +0100609 init_rwsem(&vnode->validate_lock);
David Howells4343d002017-11-02 15:27:52 +0000610 spin_lock_init(&vnode->wb_lock);
Christoph Lametera35afb82007-05-16 22:10:57 -0700611 spin_lock_init(&vnode->lock);
David Howells4343d002017-11-02 15:27:52 +0000612 INIT_LIST_HEAD(&vnode->wb_keys);
David Howellse8d6c552007-07-15 23:40:12 -0700613 INIT_LIST_HEAD(&vnode->pending_locks);
614 INIT_LIST_HEAD(&vnode->granted_locks);
615 INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
David Howellsc435ee32017-11-02 15:27:49 +0000616 seqlock_init(&vnode->cb_lock);
David Howellsec268152007-04-26 15:49:28 -0700617}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
620 * allocate an AFS inode struct from our slab cache
621 */
622static struct inode *afs_alloc_inode(struct super_block *sb)
623{
624 struct afs_vnode *vnode;
625
David Howellsec268152007-04-26 15:49:28 -0700626 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!vnode)
628 return NULL;
629
630 atomic_inc(&afs_count_active_inodes);
631
David Howellsf8de4832017-12-01 11:40:43 +0000632 /* Reset anything that shouldn't leak from one inode to the next. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 memset(&vnode->fid, 0, sizeof(vnode->fid));
634 memset(&vnode->status, 0, sizeof(vnode->status));
635
636 vnode->volume = NULL;
David Howellsf8de4832017-12-01 11:40:43 +0000637 vnode->lock_key = NULL;
638 vnode->permit_cache = NULL;
639 vnode->cb_interest = NULL;
640#ifdef CONFIG_AFS_FSCACHE
641 vnode->cache = NULL;
642#endif
643
David Howells260a9802007-04-26 15:59:35 -0700644 vnode->flags = 1 << AFS_VNODE_UNSET;
David Howellsf8de4832017-12-01 11:40:43 +0000645 vnode->cb_type = 0;
646 vnode->lock_state = AFS_VNODE_LOCK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
David Howells0f300ca2007-05-10 22:22:20 -0700648 _leave(" = %p", &vnode->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return &vnode->vfs_inode;
David Howellsec268152007-04-26 15:49:28 -0700650}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100652static void afs_i_callback(struct rcu_head *head)
653{
654 struct inode *inode = container_of(head, struct inode, i_rcu);
655 struct afs_vnode *vnode = AFS_FS_I(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100656 kmem_cache_free(afs_inode_cachep, vnode);
657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/*
660 * destroy an AFS inode struct
661 */
662static void afs_destroy_inode(struct inode *inode)
663{
David Howells08e0e7c2007-04-26 15:55:03 -0700664 struct afs_vnode *vnode = AFS_FS_I(inode);
665
David Howells0f300ca2007-05-10 22:22:20 -0700666 _enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
David Howells08e0e7c2007-04-26 15:55:03 -0700668 _debug("DESTROY INODE %p", inode);
669
David Howellsc435ee32017-11-02 15:27:49 +0000670 ASSERTCMP(vnode->cb_interest, ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700671
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100672 call_rcu(&inode->i_rcu, afs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 atomic_dec(&afs_count_active_inodes);
David Howellsec268152007-04-26 15:49:28 -0700674}
David Howells45222b92007-05-10 22:22:20 -0700675
676/*
677 * return information about an AFS volume
678 */
679static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
680{
David Howells4d673da2018-02-06 06:26:30 +0000681 struct afs_super_info *as = AFS_FS_S(dentry->d_sb);
David Howellsd2ddc772017-11-02 15:27:50 +0000682 struct afs_fs_cursor fc;
David Howells45222b92007-05-10 22:22:20 -0700683 struct afs_volume_status vs;
David Howells2b0143b2015-03-17 22:25:59 +0000684 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
David Howells45222b92007-05-10 22:22:20 -0700685 struct key *key;
686 int ret;
687
David Howells4d673da2018-02-06 06:26:30 +0000688 buf->f_type = dentry->d_sb->s_magic;
689 buf->f_bsize = AFS_BLOCK_SIZE;
690 buf->f_namelen = AFSNAMEMAX - 1;
691
692 if (as->dyn_root) {
693 buf->f_blocks = 1;
694 buf->f_bavail = 0;
695 buf->f_bfree = 0;
696 return 0;
697 }
David Howells66c7e1d2018-04-06 14:17:25 +0100698
David Howells45222b92007-05-10 22:22:20 -0700699 key = afs_request_key(vnode->volume->cell);
700 if (IS_ERR(key))
701 return PTR_ERR(key);
702
David Howellsd2ddc772017-11-02 15:27:50 +0000703 ret = -ERESTARTSYS;
704 if (afs_begin_vnode_operation(&fc, vnode, key)) {
705 fc.flags |= AFS_FS_CURSOR_NO_VSLEEP;
706 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +0100707 fc.cb_break = afs_calc_vnode_cb_break(vnode);
David Howellsd2ddc772017-11-02 15:27:50 +0000708 afs_fs_get_volume_status(&fc, &vs);
709 }
710
711 afs_check_for_remote_deletion(&fc, fc.vnode);
712 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
713 ret = afs_end_vnode_operation(&fc);
David Howells45222b92007-05-10 22:22:20 -0700714 }
715
David Howellsd2ddc772017-11-02 15:27:50 +0000716 key_put(key);
David Howells45222b92007-05-10 22:22:20 -0700717
David Howellsd2ddc772017-11-02 15:27:50 +0000718 if (ret == 0) {
David Howellsd2ddc772017-11-02 15:27:50 +0000719 if (vs.max_quota == 0)
720 buf->f_blocks = vs.part_max_blocks;
721 else
722 buf->f_blocks = vs.max_quota;
723 buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
724 }
725
726 return ret;
David Howells45222b92007-05-10 22:22:20 -0700727}