blob: f18911e8d77035f4fe757c03343eb8ce06b5e13c [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS superblock handling
2 *
David Howells13fcc682018-11-01 23:07:27 +00003 * Copyright (c) 2002, 2007, 2018 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 Howells13fcc682018-11-01 23:07:27 +000024#include <linux/fs_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 Virodde194a2011-06-12 16:01:21 -040033static void afs_kill_super(struct super_block *sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static struct inode *afs_alloc_inode(struct super_block *sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static void afs_destroy_inode(struct inode *inode);
Al Viro51b9fe42019-04-10 15:05:06 -040036static void afs_free_inode(struct inode *inode);
David Howells45222b92007-05-10 22:22:20 -070037static int afs_statfs(struct dentry *dentry, struct kstatfs *buf);
David Howells677018a2017-07-05 16:25:23 +010038static int afs_show_devname(struct seq_file *m, struct dentry *root);
39static int afs_show_options(struct seq_file *m, struct dentry *root);
David Howells13fcc682018-11-01 23:07:27 +000040static int afs_init_fs_context(struct fs_context *fc);
41static const struct fs_parameter_description afs_fs_parameters;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -040043struct file_system_type afs_fs_type = {
David Howells13fcc682018-11-01 23:07:27 +000044 .owner = THIS_MODULE,
45 .name = "afs",
46 .init_fs_context = afs_init_fs_context,
47 .parameters = &afs_fs_parameters,
48 .kill_sb = afs_kill_super,
David Howells79ddbfa2019-04-25 14:26:51 +010049 .fs_flags = FS_RENAME_DOES_D_MOVE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
Eric W. Biederman7f78e032013-03-02 19:39:14 -080051MODULE_ALIAS_FS("afs");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
David Howells5b86d4f2018-05-18 11:46:15 +010053int afs_net_id;
54
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080055static const struct super_operations afs_super_ops = {
David Howells45222b92007-05-10 22:22:20 -070056 .statfs = afs_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .alloc_inode = afs_alloc_inode,
wangleibec5eb62010-08-11 09:38:04 +010058 .drop_inode = afs_drop_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 .destroy_inode = afs_destroy_inode,
Al Viro51b9fe42019-04-10 15:05:06 -040060 .free_inode = afs_free_inode,
Al Virob57922d2010-06-07 14:34:48 -040061 .evict_inode = afs_evict_inode,
David Howells677018a2017-07-05 16:25:23 +010062 .show_devname = afs_show_devname,
63 .show_options = afs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Christoph Lametere18b8902006-12-06 20:33:20 -080066static struct kmem_cache *afs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static atomic_t afs_count_active_inodes;
68
David Howells13fcc682018-11-01 23:07:27 +000069enum afs_param {
70 Opt_autocell,
David Howells13fcc682018-11-01 23:07:27 +000071 Opt_dyn,
David Howells6c6c1d62019-04-25 14:26:52 +010072 Opt_flock,
David Howells13fcc682018-11-01 23:07:27 +000073 Opt_source,
David Howells80c72fe2007-05-03 03:11:29 -070074};
75
David Howells13fcc682018-11-01 23:07:27 +000076static const struct fs_parameter_spec afs_param_specs[] = {
77 fsparam_flag ("autocell", Opt_autocell),
David Howells13fcc682018-11-01 23:07:27 +000078 fsparam_flag ("dyn", Opt_dyn),
David Howells6c6c1d62019-04-25 14:26:52 +010079 fsparam_enum ("flock", Opt_flock),
David Howells13fcc682018-11-01 23:07:27 +000080 fsparam_string("source", Opt_source),
David Howells13fcc682018-11-01 23:07:27 +000081 {}
82};
83
David Howells6c6c1d62019-04-25 14:26:52 +010084static const struct fs_parameter_enum afs_param_enums[] = {
85 { Opt_flock, "local", afs_flock_mode_local },
86 { Opt_flock, "openafs", afs_flock_mode_openafs },
87 { Opt_flock, "strict", afs_flock_mode_strict },
88 { Opt_flock, "write", afs_flock_mode_write },
89 {}
90};
91
David Howells13fcc682018-11-01 23:07:27 +000092static const struct fs_parameter_description afs_fs_parameters = {
93 .name = "kAFS",
94 .specs = afs_param_specs,
David Howells6c6c1d62019-04-25 14:26:52 +010095 .enums = afs_param_enums,
David Howells80c72fe2007-05-03 03:11:29 -070096};
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
99 * initialise the filesystem
100 */
101int __init afs_fs_init(void)
102{
103 int ret;
104
105 _enter("");
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* create ourselves an inode cache */
108 atomic_set(&afs_count_active_inodes, 0);
109
110 ret = -ENOMEM;
111 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
112 sizeof(struct afs_vnode),
113 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -0800114 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
Paul Mundt20c2df82007-07-20 10:11:58 +0900115 afs_i_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!afs_inode_cachep) {
117 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
118 return ret;
119 }
120
121 /* now export our filesystem to lesser mortals */
122 ret = register_filesystem(&afs_fs_type);
123 if (ret < 0) {
124 kmem_cache_destroy(afs_inode_cachep);
David Howells08e0e7c2007-04-26 15:55:03 -0700125 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return ret;
127 }
128
David Howells08e0e7c2007-04-26 15:55:03 -0700129 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 0;
David Howellsec268152007-04-26 15:49:28 -0700131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
134 * clean up the filesystem
135 */
David Howells5b86d4f2018-05-18 11:46:15 +0100136void afs_fs_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
David Howells08e0e7c2007-04-26 15:55:03 -0700138 _enter("");
139
140 afs_mntpt_kill_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 unregister_filesystem(&afs_fs_type);
142
143 if (atomic_read(&afs_count_active_inodes) != 0) {
144 printk("kAFS: %d active inode objects still present\n",
145 atomic_read(&afs_count_active_inodes));
146 BUG();
147 }
148
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000149 /*
150 * Make sure all delayed rcu free inodes are flushed before we
151 * destroy cache.
152 */
153 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 kmem_cache_destroy(afs_inode_cachep);
David Howells08e0e7c2007-04-26 15:55:03 -0700155 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700156}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
David Howells677018a2017-07-05 16:25:23 +0100159 * Display the mount device name in /proc/mounts.
160 */
161static int afs_show_devname(struct seq_file *m, struct dentry *root)
162{
David Howellsd2ddc772017-11-02 15:27:50 +0000163 struct afs_super_info *as = AFS_FS_S(root->d_sb);
David Howells677018a2017-07-05 16:25:23 +0100164 struct afs_volume *volume = as->volume;
David Howellsd2ddc772017-11-02 15:27:50 +0000165 struct afs_cell *cell = as->cell;
David Howells677018a2017-07-05 16:25:23 +0100166 const char *suf = "";
167 char pref = '%';
168
David Howells4d673da2018-02-06 06:26:30 +0000169 if (as->dyn_root) {
170 seq_puts(m, "none");
171 return 0;
172 }
David Howells66c7e1d2018-04-06 14:17:25 +0100173
David Howells677018a2017-07-05 16:25:23 +0100174 switch (volume->type) {
175 case AFSVL_RWVOL:
176 break;
177 case AFSVL_ROVOL:
178 pref = '#';
179 if (volume->type_force)
180 suf = ".readonly";
181 break;
182 case AFSVL_BACKVOL:
183 pref = '#';
184 suf = ".backup";
185 break;
186 }
187
David Howellsd2ddc772017-11-02 15:27:50 +0000188 seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->name, suf);
David Howells677018a2017-07-05 16:25:23 +0100189 return 0;
190}
191
192/*
193 * Display the mount options in /proc/mounts.
194 */
195static int afs_show_options(struct seq_file *m, struct dentry *root)
196{
David Howells4d673da2018-02-06 06:26:30 +0000197 struct afs_super_info *as = AFS_FS_S(root->d_sb);
David Howells6c6c1d62019-04-25 14:26:52 +0100198 const char *p = NULL;
David Howells4d673da2018-02-06 06:26:30 +0000199
200 if (as->dyn_root)
201 seq_puts(m, ",dyn");
David Howells677018a2017-07-05 16:25:23 +0100202 if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
David Howells4d673da2018-02-06 06:26:30 +0000203 seq_puts(m, ",autocell");
David Howells6c6c1d62019-04-25 14:26:52 +0100204 switch (as->flock_mode) {
205 case afs_flock_mode_unset: break;
206 case afs_flock_mode_local: p = "local"; break;
207 case afs_flock_mode_openafs: p = "openafs"; break;
208 case afs_flock_mode_strict: p = "strict"; break;
209 case afs_flock_mode_write: p = "write"; break;
210 }
211 if (p)
212 seq_printf(m, ",flock=%s", p);
213
David Howells677018a2017-07-05 16:25:23 +0100214 return 0;
215}
216
217/*
David Howells13fcc682018-11-01 23:07:27 +0000218 * Parse the source name to get cell name, volume name, volume type and R/W
219 * selector.
220 *
221 * This can be one of the following:
David Howells00d3b7a2007-04-26 15:57:07 -0700222 * "%[cell:]volume[.]" R/W volume
David Howellsc99c2172018-11-01 23:07:27 +0000223 * "#[cell:]volume[.]" R/O or R/W volume (R/O parent),
224 * or R/W (R/W parent) volume
David Howells00d3b7a2007-04-26 15:57:07 -0700225 * "%[cell:]volume.readonly" R/O volume
226 * "#[cell:]volume.readonly" R/O volume
227 * "%[cell:]volume.backup" Backup volume
228 * "#[cell:]volume.backup" Backup volume
229 */
David Howells13fcc682018-11-01 23:07:27 +0000230static int afs_parse_source(struct fs_context *fc, struct fs_parameter *param)
David Howells00d3b7a2007-04-26 15:57:07 -0700231{
David Howells13fcc682018-11-01 23:07:27 +0000232 struct afs_fs_context *ctx = fc->fs_private;
David Howells00d3b7a2007-04-26 15:57:07 -0700233 struct afs_cell *cell;
David Howells13fcc682018-11-01 23:07:27 +0000234 const char *cellname, *suffix, *name = param->string;
David Howells00d3b7a2007-04-26 15:57:07 -0700235 int cellnamesz;
236
237 _enter(",%s", name);
David Howells66c7e1d2018-04-06 14:17:25 +0100238
David Howells00d3b7a2007-04-26 15:57:07 -0700239 if (!name) {
240 printk(KERN_ERR "kAFS: no volume name specified\n");
241 return -EINVAL;
242 }
243
244 if ((name[0] != '%' && name[0] != '#') || !name[1]) {
David Howells13fcc682018-11-01 23:07:27 +0000245 /* To use dynroot, we don't want to have to provide a source */
246 if (strcmp(name, "none") == 0) {
247 ctx->no_cell = true;
248 return 0;
249 }
David Howells00d3b7a2007-04-26 15:57:07 -0700250 printk(KERN_ERR "kAFS: unparsable volume name\n");
251 return -EINVAL;
252 }
253
254 /* determine the type of volume we're looking for */
David Howellsc99c2172018-11-01 23:07:27 +0000255 if (name[0] == '%') {
David Howells13fcc682018-11-01 23:07:27 +0000256 ctx->type = AFSVL_RWVOL;
257 ctx->force = true;
David Howells00d3b7a2007-04-26 15:57:07 -0700258 }
259 name++;
260
261 /* split the cell name out if there is one */
David Howells13fcc682018-11-01 23:07:27 +0000262 ctx->volname = strchr(name, ':');
263 if (ctx->volname) {
David Howells00d3b7a2007-04-26 15:57:07 -0700264 cellname = name;
David Howells13fcc682018-11-01 23:07:27 +0000265 cellnamesz = ctx->volname - name;
266 ctx->volname++;
David Howells00d3b7a2007-04-26 15:57:07 -0700267 } else {
David Howells13fcc682018-11-01 23:07:27 +0000268 ctx->volname = name;
David Howells00d3b7a2007-04-26 15:57:07 -0700269 cellname = NULL;
270 cellnamesz = 0;
271 }
272
273 /* the volume type is further affected by a possible suffix */
David Howells13fcc682018-11-01 23:07:27 +0000274 suffix = strrchr(ctx->volname, '.');
David Howells00d3b7a2007-04-26 15:57:07 -0700275 if (suffix) {
276 if (strcmp(suffix, ".readonly") == 0) {
David Howells13fcc682018-11-01 23:07:27 +0000277 ctx->type = AFSVL_ROVOL;
278 ctx->force = true;
David Howells00d3b7a2007-04-26 15:57:07 -0700279 } else if (strcmp(suffix, ".backup") == 0) {
David Howells13fcc682018-11-01 23:07:27 +0000280 ctx->type = AFSVL_BACKVOL;
281 ctx->force = true;
David Howells00d3b7a2007-04-26 15:57:07 -0700282 } else if (suffix[1] == 0) {
283 } else {
284 suffix = NULL;
285 }
286 }
287
David Howells13fcc682018-11-01 23:07:27 +0000288 ctx->volnamesz = suffix ?
289 suffix - ctx->volname : strlen(ctx->volname);
David Howells00d3b7a2007-04-26 15:57:07 -0700290
291 _debug("cell %*.*s [%p]",
David Howells13fcc682018-11-01 23:07:27 +0000292 cellnamesz, cellnamesz, cellname ?: "", ctx->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700293
294 /* lookup the cell record */
David Howells13fcc682018-11-01 23:07:27 +0000295 if (cellname) {
296 cell = afs_lookup_cell(ctx->net, cellname, cellnamesz,
David Howells989782d2017-11-02 15:27:50 +0000297 NULL, false);
David Howells00d3b7a2007-04-26 15:57:07 -0700298 if (IS_ERR(cell)) {
David Howells13fcc682018-11-01 23:07:27 +0000299 pr_err("kAFS: unable to lookup cell '%*.*s'\n",
wangleibec5eb62010-08-11 09:38:04 +0100300 cellnamesz, cellnamesz, cellname ?: "");
David Howells00d3b7a2007-04-26 15:57:07 -0700301 return PTR_ERR(cell);
302 }
David Howells13fcc682018-11-01 23:07:27 +0000303 afs_put_cell(ctx->net, ctx->cell);
304 ctx->cell = cell;
David Howells00d3b7a2007-04-26 15:57:07 -0700305 }
306
307 _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
David Howells13fcc682018-11-01 23:07:27 +0000308 ctx->cell->name, ctx->cell,
309 ctx->volnamesz, ctx->volnamesz, ctx->volname,
310 suffix ?: "-", ctx->type, ctx->force ? " FORCE" : "");
311
312 fc->source = param->string;
313 param->string = NULL;
314 return 0;
315}
316
317/*
318 * Parse a single mount parameter.
319 */
320static int afs_parse_param(struct fs_context *fc, struct fs_parameter *param)
321{
322 struct fs_parse_result result;
323 struct afs_fs_context *ctx = fc->fs_private;
David Howells13fcc682018-11-01 23:07:27 +0000324 int opt;
325
326 opt = fs_parse(fc, &afs_fs_parameters, param, &result);
327 if (opt < 0)
328 return opt;
329
330 switch (opt) {
David Howells13fcc682018-11-01 23:07:27 +0000331 case Opt_source:
332 return afs_parse_source(fc, param);
333
334 case Opt_autocell:
335 ctx->autocell = true;
336 break;
337
338 case Opt_dyn:
339 ctx->dyn_root = true;
340 break;
341
David Howells6c6c1d62019-04-25 14:26:52 +0100342 case Opt_flock:
343 ctx->flock_mode = result.uint_32;
344 break;
345
David Howells13fcc682018-11-01 23:07:27 +0000346 default:
347 return -EINVAL;
348 }
349
350 _leave(" = 0");
351 return 0;
352}
353
354/*
355 * Validate the options, get the cell key and look up the volume.
356 */
357static int afs_validate_fc(struct fs_context *fc)
358{
359 struct afs_fs_context *ctx = fc->fs_private;
360 struct afs_volume *volume;
361 struct key *key;
362
363 if (!ctx->dyn_root) {
364 if (ctx->no_cell) {
365 pr_warn("kAFS: Can only specify source 'none' with -o dyn\n");
366 return -EINVAL;
367 }
368
369 if (!ctx->cell) {
370 pr_warn("kAFS: No cell specified\n");
371 return -EDESTADDRREQ;
372 }
373
374 /* We try to do the mount securely. */
375 key = afs_request_key(ctx->cell);
376 if (IS_ERR(key))
377 return PTR_ERR(key);
378
379 ctx->key = key;
380
381 if (ctx->volume) {
382 afs_put_volume(ctx->cell, ctx->volume);
383 ctx->volume = NULL;
384 }
385
386 volume = afs_create_volume(ctx);
387 if (IS_ERR(volume))
388 return PTR_ERR(volume);
389
390 ctx->volume = volume;
391 }
David Howells00d3b7a2007-04-26 15:57:07 -0700392
393 return 0;
394}
395
396/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * check a superblock to see if it's the one we're looking for
398 */
David Howells13fcc682018-11-01 23:07:27 +0000399static int afs_test_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
David Howells13fcc682018-11-01 23:07:27 +0000401 struct afs_fs_context *ctx = fc->fs_private;
David Howellsd2ddc772017-11-02 15:27:50 +0000402 struct afs_super_info *as = AFS_FS_S(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
David Howells13fcc682018-11-01 23:07:27 +0000404 return (as->net_ns == fc->net_ns &&
David Howells4d673da2018-02-06 06:26:30 +0000405 as->volume &&
David Howells13fcc682018-11-01 23:07:27 +0000406 as->volume->vid == ctx->volume->vid &&
David Howells0da0b7f2018-06-15 15:19:22 +0100407 !as->dyn_root);
David Howells4d673da2018-02-06 06:26:30 +0000408}
409
David Howells13fcc682018-11-01 23:07:27 +0000410static int afs_dynroot_test_super(struct super_block *sb, struct fs_context *fc)
David Howells4d673da2018-02-06 06:26:30 +0000411{
David Howells0da0b7f2018-06-15 15:19:22 +0100412 struct afs_super_info *as = AFS_FS_S(sb);
413
David Howells13fcc682018-11-01 23:07:27 +0000414 return (as->net_ns == fc->net_ns &&
David Howells0da0b7f2018-06-15 15:19:22 +0100415 as->dyn_root);
Al Virodde194a2011-06-12 16:01:21 -0400416}
417
David Howells13fcc682018-11-01 23:07:27 +0000418static int afs_set_super(struct super_block *sb, struct fs_context *fc)
Al Virodde194a2011-06-12 16:01:21 -0400419{
Al Virodde194a2011-06-12 16:01:21 -0400420 return set_anon_super(sb, NULL);
David Howellsec268152007-04-26 15:49:28 -0700421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
424 * fill in the superblock
425 */
David Howells13fcc682018-11-01 23:07:27 +0000426static int afs_fill_super(struct super_block *sb, struct afs_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
David Howellsd2ddc772017-11-02 15:27:50 +0000428 struct afs_super_info *as = AFS_FS_S(sb);
David Howellsb8359152019-05-14 12:23:43 +0100429 struct afs_iget_data iget_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct inode *inode = NULL;
431 int ret;
432
David Howells08e0e7c2007-04-26 15:55:03 -0700433 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* fill in the superblock */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300436 sb->s_blocksize = PAGE_SIZE;
437 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 sb->s_magic = AFS_FS_MAGIC;
439 sb->s_op = &afs_super_ops;
David Howells4d673da2018-02-06 06:26:30 +0000440 if (!as->dyn_root)
441 sb->s_xattr = afs_xattr_handlers;
Jan Karaedd3ba92017-04-12 12:24:36 +0200442 ret = super_setup_bdi(sb);
443 if (ret)
444 return ret;
Nikolay Borisovb5420232019-03-11 23:28:13 -0700445 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* allocate the root inode and dentry */
David Howells4d673da2018-02-06 06:26:30 +0000448 if (as->dyn_root) {
449 inode = afs_iget_pseudo_dir(sb, true);
450 sb->s_flags |= SB_RDONLY;
451 } else {
David Howells3b6492d2018-10-20 00:57:57 +0100452 sprintf(sb->s_id, "%llu", as->volume->vid);
David Howells4d673da2018-02-06 06:26:30 +0000453 afs_activate_volume(as->volume);
David Howellsb8359152019-05-14 12:23:43 +0100454 iget_data.fid.vid = as->volume->vid;
455 iget_data.fid.vnode = 1;
456 iget_data.fid.vnode_hi = 0;
457 iget_data.fid.unique = 1;
458 iget_data.cb_v_break = as->volume->cb_v_break;
459 iget_data.cb_s_break = 0;
460 inode = afs_iget(sb, ctx->key, &iget_data, NULL, NULL, NULL);
David Howells4d673da2018-02-06 06:26:30 +0000461 }
462
David Howells08e0e7c2007-04-26 15:55:03 -0700463 if (IS_ERR(inode))
Al Virodde194a2011-06-12 16:01:21 -0400464 return PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
David Howells13fcc682018-11-01 23:07:27 +0000466 if (ctx->autocell || as->dyn_root)
wangleibec5eb62010-08-11 09:38:04 +0100467 set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -0500470 sb->s_root = d_make_root(inode);
471 if (!sb->s_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 goto error;
473
David Howells0da0b7f2018-06-15 15:19:22 +0100474 if (as->dyn_root) {
David Howells66c7e1d2018-04-06 14:17:25 +0100475 sb->s_d_op = &afs_dynroot_dentry_operations;
David Howells0da0b7f2018-06-15 15:19:22 +0100476 ret = afs_dynroot_populate(sb);
477 if (ret < 0)
478 goto error;
479 } else {
David Howells66c7e1d2018-04-06 14:17:25 +0100480 sb->s_d_op = &afs_fs_dentry_operations;
David Howells0da0b7f2018-06-15 15:19:22 +0100481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
David Howells08e0e7c2007-04-26 15:55:03 -0700483 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485
David Howellsec268152007-04-26 15:49:28 -0700486error:
David Howells08e0e7c2007-04-26 15:55:03 -0700487 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return ret;
David Howellsec268152007-04-26 15:49:28 -0700489}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
David Howells13fcc682018-11-01 23:07:27 +0000491static struct afs_super_info *afs_alloc_sbi(struct fs_context *fc)
David Howells49566f62017-11-02 15:27:46 +0000492{
David Howells13fcc682018-11-01 23:07:27 +0000493 struct afs_fs_context *ctx = fc->fs_private;
David Howells49566f62017-11-02 15:27:46 +0000494 struct afs_super_info *as;
495
496 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
497 if (as) {
David Howells13fcc682018-11-01 23:07:27 +0000498 as->net_ns = get_net(fc->net_ns);
David Howells6c6c1d62019-04-25 14:26:52 +0100499 as->flock_mode = ctx->flock_mode;
David Howells13fcc682018-11-01 23:07:27 +0000500 if (ctx->dyn_root) {
David Howells4d673da2018-02-06 06:26:30 +0000501 as->dyn_root = true;
David Howells13fcc682018-11-01 23:07:27 +0000502 } else {
503 as->cell = afs_get_cell(ctx->cell);
504 as->volume = __afs_get_volume(ctx->volume);
505 }
David Howells49566f62017-11-02 15:27:46 +0000506 }
507 return as;
508}
509
510static void afs_destroy_sbi(struct afs_super_info *as)
511{
512 if (as) {
David Howells9ed900b2017-11-02 15:27:46 +0000513 afs_put_volume(as->cell, as->volume);
David Howells5b86d4f2018-05-18 11:46:15 +0100514 afs_put_cell(afs_net(as->net_ns), as->cell);
515 put_net(as->net_ns);
David Howells49566f62017-11-02 15:27:46 +0000516 kfree(as);
517 }
518}
519
David Howells0da0b7f2018-06-15 15:19:22 +0100520static void afs_kill_super(struct super_block *sb)
521{
522 struct afs_super_info *as = AFS_FS_S(sb);
523 struct afs_net *net = afs_net(as->net_ns);
524
525 if (as->dyn_root)
526 afs_dynroot_depopulate(sb);
David Howells13fcc682018-11-01 23:07:27 +0000527
David Howells0da0b7f2018-06-15 15:19:22 +0100528 /* Clear the callback interests (which will do ilookup5) before
529 * deactivating the superblock.
530 */
531 if (as->volume)
532 afs_clear_callback_interests(net, as->volume->servers);
533 kill_anon_super(sb);
534 if (as->volume)
535 afs_deactivate_volume(as->volume);
536 afs_destroy_sbi(as);
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/*
David Howells13fcc682018-11-01 23:07:27 +0000540 * Get an AFS superblock and root directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
David Howells13fcc682018-11-01 23:07:27 +0000542static int afs_get_tree(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
David Howells13fcc682018-11-01 23:07:27 +0000544 struct afs_fs_context *ctx = fc->fs_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct super_block *sb;
Al Virodde194a2011-06-12 16:01:21 -0400546 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 int ret;
548
David Howells13fcc682018-11-01 23:07:27 +0000549 ret = afs_validate_fc(fc);
550 if (ret)
Eric W. Biedermanf74f70f2013-01-31 04:23:54 -0800551 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
David Howells13fcc682018-11-01 23:07:27 +0000553 _enter("");
David Howells00d3b7a2007-04-26 15:57:07 -0700554
David Howells49566f62017-11-02 15:27:46 +0000555 /* allocate a superblock info record */
556 ret = -ENOMEM;
David Howells13fcc682018-11-01 23:07:27 +0000557 as = afs_alloc_sbi(fc);
David Howells49566f62017-11-02 15:27:46 +0000558 if (!as)
David Howells13fcc682018-11-01 23:07:27 +0000559 goto error;
560 fc->s_fs_info = as;
David Howellsd2ddc772017-11-02 15:27:50 +0000561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* allocate a deviceless superblock */
David Howells13fcc682018-11-01 23:07:27 +0000563 sb = sget_fc(fc,
564 as->dyn_root ? afs_dynroot_test_super : afs_test_super,
565 afs_set_super);
David Howells08e0e7c2007-04-26 15:55:03 -0700566 if (IS_ERR(sb)) {
567 ret = PTR_ERR(sb);
David Howells13fcc682018-11-01 23:07:27 +0000568 goto error;
David Howells08e0e7c2007-04-26 15:55:03 -0700569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
David Howells436058a2007-04-26 15:56:24 -0700571 if (!sb->s_root) {
572 /* initial superblock/root creation */
573 _debug("create");
David Howells13fcc682018-11-01 23:07:27 +0000574 ret = afs_fill_super(sb, ctx);
David Howellsf044c882017-11-02 15:27:45 +0000575 if (ret < 0)
576 goto error_sb;
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800577 sb->s_flags |= SB_ACTIVE;
David Howells436058a2007-04-26 15:56:24 -0700578 } else {
579 _debug("reuse");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800580 ASSERTCMP(sb->s_flags, &, SB_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
David Howells13fcc682018-11-01 23:07:27 +0000583 fc->root = dget(sb->s_root);
David Howells80548b02019-04-25 14:26:51 +0100584 trace_afs_get_tree(as->cell, as->volume);
David Howells08e0e7c2007-04-26 15:55:03 -0700585 _leave(" = 0 [%p]", sb);
David Howells13fcc682018-11-01 23:07:27 +0000586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
David Howellsf044c882017-11-02 15:27:45 +0000588error_sb:
589 deactivate_locked_super(sb);
David Howellsec268152007-04-26 15:49:28 -0700590error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 _leave(" = %d", ret);
David Howells13fcc682018-11-01 23:07:27 +0000592 return ret;
593}
594
595static void afs_free_fc(struct fs_context *fc)
596{
597 struct afs_fs_context *ctx = fc->fs_private;
598
599 afs_destroy_sbi(fc->s_fs_info);
600 afs_put_volume(ctx->cell, ctx->volume);
601 afs_put_cell(ctx->net, ctx->cell);
602 key_put(ctx->key);
603 kfree(ctx);
604}
605
606static const struct fs_context_operations afs_context_ops = {
607 .free = afs_free_fc,
608 .parse_param = afs_parse_param,
609 .get_tree = afs_get_tree,
610};
611
612/*
613 * Set up the filesystem mount context.
614 */
615static int afs_init_fs_context(struct fs_context *fc)
616{
617 struct afs_fs_context *ctx;
618 struct afs_cell *cell;
619
David Howells13fcc682018-11-01 23:07:27 +0000620 ctx = kzalloc(sizeof(struct afs_fs_context), GFP_KERNEL);
621 if (!ctx)
622 return -ENOMEM;
623
624 ctx->type = AFSVL_ROVOL;
625 ctx->net = afs_net(fc->net_ns);
626
627 /* Default to the workstation cell. */
628 rcu_read_lock();
629 cell = afs_lookup_cell_rcu(ctx->net, NULL, 0);
630 rcu_read_unlock();
631 if (IS_ERR(cell))
632 cell = NULL;
633 ctx->cell = cell;
634
635 fc->fs_private = ctx;
636 fc->ops = &afs_context_ops;
637 return 0;
David Howellsec268152007-04-26 15:49:28 -0700638}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/*
David Howellsf8de4832017-12-01 11:40:43 +0000641 * Initialise an inode cache slab element prior to any use. Note that
642 * afs_alloc_inode() *must* reset anything that could incorrectly leak from one
643 * inode to another.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700645static void afs_i_init_once(void *_vnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
David Howellsec268152007-04-26 15:49:28 -0700647 struct afs_vnode *vnode = _vnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Christoph Lametera35afb82007-05-16 22:10:57 -0700649 memset(vnode, 0, sizeof(*vnode));
650 inode_init_once(&vnode->vfs_inode);
David Howellsd2ddc772017-11-02 15:27:50 +0000651 mutex_init(&vnode->io_lock);
David Howellsb61f7dc2018-04-27 20:46:22 +0100652 init_rwsem(&vnode->validate_lock);
David Howells4343d002017-11-02 15:27:52 +0000653 spin_lock_init(&vnode->wb_lock);
Christoph Lametera35afb82007-05-16 22:10:57 -0700654 spin_lock_init(&vnode->lock);
David Howells4343d002017-11-02 15:27:52 +0000655 INIT_LIST_HEAD(&vnode->wb_keys);
David Howellse8d6c552007-07-15 23:40:12 -0700656 INIT_LIST_HEAD(&vnode->pending_locks);
657 INIT_LIST_HEAD(&vnode->granted_locks);
658 INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
David Howellsc435ee32017-11-02 15:27:49 +0000659 seqlock_init(&vnode->cb_lock);
David Howellsec268152007-04-26 15:49:28 -0700660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/*
663 * allocate an AFS inode struct from our slab cache
664 */
665static struct inode *afs_alloc_inode(struct super_block *sb)
666{
667 struct afs_vnode *vnode;
668
David Howellsec268152007-04-26 15:49:28 -0700669 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (!vnode)
671 return NULL;
672
673 atomic_inc(&afs_count_active_inodes);
674
David Howellsf8de4832017-12-01 11:40:43 +0000675 /* Reset anything that shouldn't leak from one inode to the next. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 memset(&vnode->fid, 0, sizeof(vnode->fid));
677 memset(&vnode->status, 0, sizeof(vnode->status));
678
679 vnode->volume = NULL;
David Howellsf8de4832017-12-01 11:40:43 +0000680 vnode->lock_key = NULL;
681 vnode->permit_cache = NULL;
David Howellsf6424042019-05-13 16:14:32 +0100682 RCU_INIT_POINTER(vnode->cb_interest, NULL);
David Howellsf8de4832017-12-01 11:40:43 +0000683#ifdef CONFIG_AFS_FSCACHE
684 vnode->cache = NULL;
685#endif
686
David Howells260a9802007-04-26 15:59:35 -0700687 vnode->flags = 1 << AFS_VNODE_UNSET;
David Howellsf8de4832017-12-01 11:40:43 +0000688 vnode->lock_state = AFS_VNODE_LOCK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
David Howells79ddbfa2019-04-25 14:26:51 +0100690 init_rwsem(&vnode->rmdir_lock);
691
David Howells0f300ca2007-05-10 22:22:20 -0700692 _leave(" = %p", &vnode->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return &vnode->vfs_inode;
David Howellsec268152007-04-26 15:49:28 -0700694}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Al Viro51b9fe42019-04-10 15:05:06 -0400696static void afs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100697{
Al Viro51b9fe42019-04-10 15:05:06 -0400698 kmem_cache_free(afs_inode_cachep, AFS_FS_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/*
702 * destroy an AFS inode struct
703 */
704static void afs_destroy_inode(struct inode *inode)
705{
David Howells08e0e7c2007-04-26 15:55:03 -0700706 struct afs_vnode *vnode = AFS_FS_I(inode);
707
David Howells3b6492d2018-10-20 00:57:57 +0100708 _enter("%p{%llx:%llu}", inode, vnode->fid.vid, vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
David Howells08e0e7c2007-04-26 15:55:03 -0700710 _debug("DESTROY INODE %p", inode);
711
David Howellsf6424042019-05-13 16:14:32 +0100712 ASSERTCMP(rcu_access_pointer(vnode->cb_interest), ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 atomic_dec(&afs_count_active_inodes);
David Howellsec268152007-04-26 15:49:28 -0700715}
David Howells45222b92007-05-10 22:22:20 -0700716
717/*
718 * return information about an AFS volume
719 */
720static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
721{
David Howells4d673da2018-02-06 06:26:30 +0000722 struct afs_super_info *as = AFS_FS_S(dentry->d_sb);
David Howellsd2ddc772017-11-02 15:27:50 +0000723 struct afs_fs_cursor fc;
David Howells45222b92007-05-10 22:22:20 -0700724 struct afs_volume_status vs;
David Howells2b0143b2015-03-17 22:25:59 +0000725 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
David Howells45222b92007-05-10 22:22:20 -0700726 struct key *key;
727 int ret;
728
David Howells4d673da2018-02-06 06:26:30 +0000729 buf->f_type = dentry->d_sb->s_magic;
730 buf->f_bsize = AFS_BLOCK_SIZE;
731 buf->f_namelen = AFSNAMEMAX - 1;
732
733 if (as->dyn_root) {
734 buf->f_blocks = 1;
735 buf->f_bavail = 0;
736 buf->f_bfree = 0;
737 return 0;
738 }
David Howells66c7e1d2018-04-06 14:17:25 +0100739
David Howells45222b92007-05-10 22:22:20 -0700740 key = afs_request_key(vnode->volume->cell);
741 if (IS_ERR(key))
742 return PTR_ERR(key);
743
David Howellsd2ddc772017-11-02 15:27:50 +0000744 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +0100745 if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
David Howellsd2ddc772017-11-02 15:27:50 +0000746 fc.flags |= AFS_FS_CURSOR_NO_VSLEEP;
747 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +0100748 fc.cb_break = afs_calc_vnode_cb_break(vnode);
David Howellsd2ddc772017-11-02 15:27:50 +0000749 afs_fs_get_volume_status(&fc, &vs);
750 }
751
752 afs_check_for_remote_deletion(&fc, fc.vnode);
David Howellsd2ddc772017-11-02 15:27:50 +0000753 ret = afs_end_vnode_operation(&fc);
David Howells45222b92007-05-10 22:22:20 -0700754 }
755
David Howellsd2ddc772017-11-02 15:27:50 +0000756 key_put(key);
David Howells45222b92007-05-10 22:22:20 -0700757
David Howellsd2ddc772017-11-02 15:27:50 +0000758 if (ret == 0) {
David Howellsd2ddc772017-11-02 15:27:50 +0000759 if (vs.max_quota == 0)
760 buf->f_blocks = vs.part_max_blocks;
761 else
762 buf->f_blocks = vs.max_quota;
763 buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
764 }
765
766 return ret;
David Howells45222b92007-05-10 22:22:20 -0700767}