blob: 4961d32ccd1e0abeb22e48b0e87c19f87c1620ff [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 &&
David Howellsa51dfaa2019-12-11 08:06:08 +0000359 as->cell == as1->cell &&
David Howells0da0b7f2018-06-15 15:19:22 +0100360 !as->dyn_root);
David Howells4d673da2018-02-06 06:26:30 +0000361}
362
363static int afs_dynroot_test_super(struct super_block *sb, void *data)
364{
David Howells0da0b7f2018-06-15 15:19:22 +0100365 struct afs_super_info *as1 = data;
366 struct afs_super_info *as = AFS_FS_S(sb);
367
368 return (as->net_ns == as1->net_ns &&
369 as->dyn_root);
Al Virodde194a2011-06-12 16:01:21 -0400370}
371
372static int afs_set_super(struct super_block *sb, void *data)
373{
David Howellsd2ddc772017-11-02 15:27:50 +0000374 struct afs_super_info *as = data;
375
376 sb->s_fs_info = as;
Al Virodde194a2011-06-12 16:01:21 -0400377 return set_anon_super(sb, NULL);
David Howellsec268152007-04-26 15:49:28 -0700378}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/*
381 * fill in the superblock
382 */
Al Virodde194a2011-06-12 16:01:21 -0400383static int afs_fill_super(struct super_block *sb,
384 struct afs_mount_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
David Howellsd2ddc772017-11-02 15:27:50 +0000386 struct afs_super_info *as = AFS_FS_S(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct afs_fid fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct inode *inode = NULL;
389 int ret;
390
David Howells08e0e7c2007-04-26 15:55:03 -0700391 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* fill in the superblock */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300394 sb->s_blocksize = PAGE_SIZE;
395 sb->s_blocksize_bits = PAGE_SHIFT;
Marc Dionne97a5c6b2019-11-21 15:37:26 +0000396 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 sb->s_magic = AFS_FS_MAGIC;
398 sb->s_op = &afs_super_ops;
David Howells4d673da2018-02-06 06:26:30 +0000399 if (!as->dyn_root)
400 sb->s_xattr = afs_xattr_handlers;
Jan Karaedd3ba92017-04-12 12:24:36 +0200401 ret = super_setup_bdi(sb);
402 if (ret)
403 return ret;
404 sb->s_bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* allocate the root inode and dentry */
David Howells4d673da2018-02-06 06:26:30 +0000407 if (as->dyn_root) {
408 inode = afs_iget_pseudo_dir(sb, true);
David Howells4d673da2018-02-06 06:26:30 +0000409 } else {
410 sprintf(sb->s_id, "%u", as->volume->vid);
411 afs_activate_volume(as->volume);
412 fid.vid = as->volume->vid;
413 fid.vnode = 1;
414 fid.unique = 1;
415 inode = afs_iget(sb, params->key, &fid, NULL, NULL, NULL);
416 }
417
David Howells08e0e7c2007-04-26 15:55:03 -0700418 if (IS_ERR(inode))
Al Virodde194a2011-06-12 16:01:21 -0400419 return PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
David Howells4d673da2018-02-06 06:26:30 +0000421 if (params->autocell || params->dyn_root)
wangleibec5eb62010-08-11 09:38:04 +0100422 set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -0500425 sb->s_root = d_make_root(inode);
426 if (!sb->s_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 goto error;
428
David Howells0da0b7f2018-06-15 15:19:22 +0100429 if (as->dyn_root) {
David Howells66c7e1d2018-04-06 14:17:25 +0100430 sb->s_d_op = &afs_dynroot_dentry_operations;
David Howells0da0b7f2018-06-15 15:19:22 +0100431 ret = afs_dynroot_populate(sb);
432 if (ret < 0)
433 goto error;
434 } else {
David Howells66c7e1d2018-04-06 14:17:25 +0100435 sb->s_d_op = &afs_fs_dentry_operations;
David Howells0da0b7f2018-06-15 15:19:22 +0100436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
David Howells08e0e7c2007-04-26 15:55:03 -0700438 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440
David Howellsec268152007-04-26 15:49:28 -0700441error:
David Howells08e0e7c2007-04-26 15:55:03 -0700442 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return ret;
David Howellsec268152007-04-26 15:49:28 -0700444}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
David Howells49566f62017-11-02 15:27:46 +0000446static struct afs_super_info *afs_alloc_sbi(struct afs_mount_params *params)
447{
448 struct afs_super_info *as;
449
450 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
451 if (as) {
David Howells5b86d4f2018-05-18 11:46:15 +0100452 as->net_ns = get_net(params->net_ns);
David Howells4d673da2018-02-06 06:26:30 +0000453 if (params->dyn_root)
454 as->dyn_root = true;
455 else
456 as->cell = afs_get_cell(params->cell);
David Howells49566f62017-11-02 15:27:46 +0000457 }
458 return as;
459}
460
461static void afs_destroy_sbi(struct afs_super_info *as)
462{
463 if (as) {
David Howells9ed900b2017-11-02 15:27:46 +0000464 afs_put_volume(as->cell, as->volume);
David Howells5b86d4f2018-05-18 11:46:15 +0100465 afs_put_cell(afs_net(as->net_ns), as->cell);
466 put_net(as->net_ns);
David Howells49566f62017-11-02 15:27:46 +0000467 kfree(as);
468 }
469}
470
David Howells0da0b7f2018-06-15 15:19:22 +0100471static void afs_kill_super(struct super_block *sb)
472{
473 struct afs_super_info *as = AFS_FS_S(sb);
474 struct afs_net *net = afs_net(as->net_ns);
475
476 if (as->dyn_root)
477 afs_dynroot_depopulate(sb);
478
479 /* Clear the callback interests (which will do ilookup5) before
480 * deactivating the superblock.
481 */
482 if (as->volume)
483 afs_clear_callback_interests(net, as->volume->servers);
484 kill_anon_super(sb);
485 if (as->volume)
486 afs_deactivate_volume(as->volume);
487 afs_destroy_sbi(as);
488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
491 * get an AFS superblock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
Al Virof7442b32010-07-26 14:16:21 +0400493static struct dentry *afs_mount(struct file_system_type *fs_type,
David Howells49566f62017-11-02 15:27:46 +0000494 int flags, const char *dev_name, void *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct afs_mount_params params;
497 struct super_block *sb;
David Howellsd2ddc772017-11-02 15:27:50 +0000498 struct afs_volume *candidate;
David Howells00d3b7a2007-04-26 15:57:07 -0700499 struct key *key;
Al Virodde194a2011-06-12 16:01:21 -0400500 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int ret;
502
503 _enter(",,%s,%p", dev_name, options);
504
505 memset(&params, 0, sizeof(params));
506
Eric W. Biedermanf74f70f2013-01-31 04:23:54 -0800507 ret = -EINVAL;
508 if (current->nsproxy->net_ns != &init_net)
509 goto error;
David Howells5b86d4f2018-05-18 11:46:15 +0100510 params.net_ns = current->nsproxy->net_ns;
511 params.net = afs_net(params.net_ns);
512
David Howells00d3b7a2007-04-26 15:57:07 -0700513 /* parse the options and device name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (options) {
David Howells00d3b7a2007-04-26 15:57:07 -0700515 ret = afs_parse_options(&params, options, &dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (ret < 0)
517 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
David Howells4d673da2018-02-06 06:26:30 +0000520 if (!params.dyn_root) {
521 ret = afs_parse_device_name(&params, dev_name);
522 if (ret < 0)
523 goto error;
David Howells00d3b7a2007-04-26 15:57:07 -0700524
David Howells4d673da2018-02-06 06:26:30 +0000525 /* try and do the mount securely */
526 key = afs_request_key(params.cell);
527 if (IS_ERR(key)) {
528 _leave(" = %ld [key]", PTR_ERR(key));
529 ret = PTR_ERR(key);
530 goto error;
531 }
532 params.key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700533 }
David Howells00d3b7a2007-04-26 15:57:07 -0700534
David Howells49566f62017-11-02 15:27:46 +0000535 /* allocate a superblock info record */
536 ret = -ENOMEM;
537 as = afs_alloc_sbi(&params);
538 if (!as)
David Howellsd2ddc772017-11-02 15:27:50 +0000539 goto error_key;
David Howells49566f62017-11-02 15:27:46 +0000540
David Howells4d673da2018-02-06 06:26:30 +0000541 if (!params.dyn_root) {
542 /* Assume we're going to need a volume record; at the very
543 * least we can use it to update the volume record if we have
544 * one already. This checks that the volume exists within the
545 * cell.
546 */
547 candidate = afs_create_volume(&params);
548 if (IS_ERR(candidate)) {
549 ret = PTR_ERR(candidate);
550 goto error_as;
551 }
552
553 as->volume = candidate;
David Howells08e0e7c2007-04-26 15:55:03 -0700554 }
David Howellsd2ddc772017-11-02 15:27:50 +0000555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* allocate a deviceless superblock */
David Howells4d673da2018-02-06 06:26:30 +0000557 sb = sget(fs_type,
558 as->dyn_root ? afs_dynroot_test_super : afs_test_super,
559 afs_set_super, flags, as);
David Howells08e0e7c2007-04-26 15:55:03 -0700560 if (IS_ERR(sb)) {
561 ret = PTR_ERR(sb);
David Howellsf044c882017-11-02 15:27:45 +0000562 goto error_as;
David Howells08e0e7c2007-04-26 15:55:03 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
David Howells436058a2007-04-26 15:56:24 -0700565 if (!sb->s_root) {
566 /* initial superblock/root creation */
567 _debug("create");
David Howells436058a2007-04-26 15:56:24 -0700568 ret = afs_fill_super(sb, &params);
David Howellsf044c882017-11-02 15:27:45 +0000569 if (ret < 0)
570 goto error_sb;
David Howells49566f62017-11-02 15:27:46 +0000571 as = NULL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800572 sb->s_flags |= SB_ACTIVE;
David Howells436058a2007-04-26 15:56:24 -0700573 } else {
574 _debug("reuse");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800575 ASSERTCMP(sb->s_flags, &, SB_ACTIVE);
David Howells49566f62017-11-02 15:27:46 +0000576 afs_destroy_sbi(as);
577 as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
David Howells9ed900b2017-11-02 15:27:46 +0000580 afs_put_cell(params.net, params.cell);
David Howells49566f62017-11-02 15:27:46 +0000581 key_put(params.key);
David Howells08e0e7c2007-04-26 15:55:03 -0700582 _leave(" = 0 [%p]", sb);
Al Virof7442b32010-07-26 14:16:21 +0400583 return dget(sb->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
David Howellsf044c882017-11-02 15:27:45 +0000585error_sb:
586 deactivate_locked_super(sb);
David Howellsd2ddc772017-11-02 15:27:50 +0000587 goto error_key;
David Howellsf044c882017-11-02 15:27:45 +0000588error_as:
David Howells49566f62017-11-02 15:27:46 +0000589 afs_destroy_sbi(as);
David Howellsd2ddc772017-11-02 15:27:50 +0000590error_key:
591 key_put(params.key);
David Howellsec268152007-04-26 15:49:28 -0700592error:
David Howells9ed900b2017-11-02 15:27:46 +0000593 afs_put_cell(params.net, params.cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 _leave(" = %d", ret);
Al Virof7442b32010-07-26 14:16:21 +0400595 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/*
David Howellsf8de4832017-12-01 11:40:43 +0000599 * Initialise an inode cache slab element prior to any use. Note that
600 * afs_alloc_inode() *must* reset anything that could incorrectly leak from one
601 * inode to another.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700603static void afs_i_init_once(void *_vnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
David Howellsec268152007-04-26 15:49:28 -0700605 struct afs_vnode *vnode = _vnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Christoph Lametera35afb82007-05-16 22:10:57 -0700607 memset(vnode, 0, sizeof(*vnode));
608 inode_init_once(&vnode->vfs_inode);
David Howellsd2ddc772017-11-02 15:27:50 +0000609 mutex_init(&vnode->io_lock);
David Howellsb61f7dc2018-04-27 20:46:22 +0100610 init_rwsem(&vnode->validate_lock);
David Howells4343d002017-11-02 15:27:52 +0000611 spin_lock_init(&vnode->wb_lock);
Christoph Lametera35afb82007-05-16 22:10:57 -0700612 spin_lock_init(&vnode->lock);
David Howells4343d002017-11-02 15:27:52 +0000613 INIT_LIST_HEAD(&vnode->wb_keys);
David Howellse8d6c552007-07-15 23:40:12 -0700614 INIT_LIST_HEAD(&vnode->pending_locks);
615 INIT_LIST_HEAD(&vnode->granted_locks);
616 INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
David Howellsc435ee32017-11-02 15:27:49 +0000617 seqlock_init(&vnode->cb_lock);
David Howellsec268152007-04-26 15:49:28 -0700618}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
621 * allocate an AFS inode struct from our slab cache
622 */
623static struct inode *afs_alloc_inode(struct super_block *sb)
624{
625 struct afs_vnode *vnode;
626
David Howellsec268152007-04-26 15:49:28 -0700627 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!vnode)
629 return NULL;
630
631 atomic_inc(&afs_count_active_inodes);
632
David Howellsf8de4832017-12-01 11:40:43 +0000633 /* Reset anything that shouldn't leak from one inode to the next. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 memset(&vnode->fid, 0, sizeof(vnode->fid));
635 memset(&vnode->status, 0, sizeof(vnode->status));
636
637 vnode->volume = NULL;
David Howellsf8de4832017-12-01 11:40:43 +0000638 vnode->lock_key = NULL;
639 vnode->permit_cache = NULL;
640 vnode->cb_interest = NULL;
641#ifdef CONFIG_AFS_FSCACHE
642 vnode->cache = NULL;
643#endif
644
David Howells260a9802007-04-26 15:59:35 -0700645 vnode->flags = 1 << AFS_VNODE_UNSET;
David Howellsf8de4832017-12-01 11:40:43 +0000646 vnode->cb_type = 0;
647 vnode->lock_state = AFS_VNODE_LOCK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
David Howells0f300ca2007-05-10 22:22:20 -0700649 _leave(" = %p", &vnode->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return &vnode->vfs_inode;
David Howellsec268152007-04-26 15:49:28 -0700651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100653static void afs_i_callback(struct rcu_head *head)
654{
655 struct inode *inode = container_of(head, struct inode, i_rcu);
656 struct afs_vnode *vnode = AFS_FS_I(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100657 kmem_cache_free(afs_inode_cachep, vnode);
658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/*
661 * destroy an AFS inode struct
662 */
663static void afs_destroy_inode(struct inode *inode)
664{
David Howells08e0e7c2007-04-26 15:55:03 -0700665 struct afs_vnode *vnode = AFS_FS_I(inode);
666
David Howells0f300ca2007-05-10 22:22:20 -0700667 _enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
David Howells08e0e7c2007-04-26 15:55:03 -0700669 _debug("DESTROY INODE %p", inode);
670
David Howellsc435ee32017-11-02 15:27:49 +0000671 ASSERTCMP(vnode->cb_interest, ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700672
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100673 call_rcu(&inode->i_rcu, afs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 atomic_dec(&afs_count_active_inodes);
David Howellsec268152007-04-26 15:49:28 -0700675}
David Howells45222b92007-05-10 22:22:20 -0700676
677/*
678 * return information about an AFS volume
679 */
680static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
681{
David Howells4d673da2018-02-06 06:26:30 +0000682 struct afs_super_info *as = AFS_FS_S(dentry->d_sb);
David Howellsd2ddc772017-11-02 15:27:50 +0000683 struct afs_fs_cursor fc;
David Howells45222b92007-05-10 22:22:20 -0700684 struct afs_volume_status vs;
David Howells2b0143b2015-03-17 22:25:59 +0000685 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
David Howells45222b92007-05-10 22:22:20 -0700686 struct key *key;
687 int ret;
688
David Howells4d673da2018-02-06 06:26:30 +0000689 buf->f_type = dentry->d_sb->s_magic;
690 buf->f_bsize = AFS_BLOCK_SIZE;
691 buf->f_namelen = AFSNAMEMAX - 1;
692
693 if (as->dyn_root) {
694 buf->f_blocks = 1;
695 buf->f_bavail = 0;
696 buf->f_bfree = 0;
697 return 0;
698 }
David Howells66c7e1d2018-04-06 14:17:25 +0100699
David Howells45222b92007-05-10 22:22:20 -0700700 key = afs_request_key(vnode->volume->cell);
701 if (IS_ERR(key))
702 return PTR_ERR(key);
703
David Howellsd2ddc772017-11-02 15:27:50 +0000704 ret = -ERESTARTSYS;
705 if (afs_begin_vnode_operation(&fc, vnode, key)) {
706 fc.flags |= AFS_FS_CURSOR_NO_VSLEEP;
707 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +0100708 fc.cb_break = afs_calc_vnode_cb_break(vnode);
David Howellsd2ddc772017-11-02 15:27:50 +0000709 afs_fs_get_volume_status(&fc, &vs);
710 }
711
712 afs_check_for_remote_deletion(&fc, fc.vnode);
713 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
714 ret = afs_end_vnode_operation(&fc);
David Howells45222b92007-05-10 22:22:20 -0700715 }
716
David Howellsd2ddc772017-11-02 15:27:50 +0000717 key_put(key);
David Howells45222b92007-05-10 22:22:20 -0700718
David Howellsd2ddc772017-11-02 15:27:50 +0000719 if (ret == 0) {
David Howellsd2ddc772017-11-02 15:27:50 +0000720 if (vs.max_quota == 0)
721 buf->f_blocks = vs.part_max_blocks;
722 else
723 buf->f_blocks = vs.max_quota;
724 buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
725 }
726
727 return ret;
David Howells45222b92007-05-10 22:22:20 -0700728}