blob: 689173c0a682fd2861a6836bc097e49e15696abc [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>
28#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "internal.h"
30
31#define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
32
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070033static void afs_i_init_once(void *foo);
Al Virof7442b32010-07-26 14:16:21 +040034static struct dentry *afs_mount(struct file_system_type *fs_type,
35 int flags, const char *dev_name, void *data);
Al Virodde194a2011-06-12 16:01:21 -040036static void afs_kill_super(struct super_block *sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static struct inode *afs_alloc_inode(struct super_block *sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static void afs_destroy_inode(struct inode *inode);
David Howells45222b92007-05-10 22:22:20 -070039static int afs_statfs(struct dentry *dentry, struct kstatfs *buf);
David Howells677018a2017-07-05 16:25:23 +010040static int afs_show_devname(struct seq_file *m, struct dentry *root);
41static int afs_show_options(struct seq_file *m, struct dentry *root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -040043struct file_system_type afs_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .owner = THIS_MODULE,
45 .name = "afs",
Al Virof7442b32010-07-26 14:16:21 +040046 .mount = afs_mount,
Al Virodde194a2011-06-12 16:01:21 -040047 .kill_sb = afs_kill_super,
David Howells80c72fe2007-05-03 03:11:29 -070048 .fs_flags = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
Eric W. Biederman7f78e032013-03-02 19:39:14 -080050MODULE_ALIAS_FS("afs");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080052static const struct super_operations afs_super_ops = {
David Howells45222b92007-05-10 22:22:20 -070053 .statfs = afs_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .alloc_inode = afs_alloc_inode,
wangleibec5eb62010-08-11 09:38:04 +010055 .drop_inode = afs_drop_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .destroy_inode = afs_destroy_inode,
Al Virob57922d2010-06-07 14:34:48 -040057 .evict_inode = afs_evict_inode,
David Howells677018a2017-07-05 16:25:23 +010058 .show_devname = afs_show_devname,
59 .show_options = afs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Christoph Lametere18b8902006-12-06 20:33:20 -080062static struct kmem_cache *afs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static atomic_t afs_count_active_inodes;
64
David Howells80c72fe2007-05-03 03:11:29 -070065enum {
66 afs_no_opt,
67 afs_opt_cell,
68 afs_opt_rwpath,
69 afs_opt_vol,
wangleibec5eb62010-08-11 09:38:04 +010070 afs_opt_autocell,
David Howells80c72fe2007-05-03 03:11:29 -070071};
72
Steven Whitehousea447c092008-10-13 10:46:57 +010073static const match_table_t afs_options_list = {
David Howells80c72fe2007-05-03 03:11:29 -070074 { afs_opt_cell, "cell=%s" },
75 { afs_opt_rwpath, "rwpath" },
76 { afs_opt_vol, "vol=%s" },
wangleibec5eb62010-08-11 09:38:04 +010077 { afs_opt_autocell, "autocell" },
David Howells80c72fe2007-05-03 03:11:29 -070078 { afs_no_opt, NULL },
79};
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * initialise the filesystem
83 */
84int __init afs_fs_init(void)
85{
86 int ret;
87
88 _enter("");
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /* create ourselves an inode cache */
91 atomic_set(&afs_count_active_inodes, 0);
92
93 ret = -ENOMEM;
94 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
95 sizeof(struct afs_vnode),
96 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -080097 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
Paul Mundt20c2df82007-07-20 10:11:58 +090098 afs_i_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (!afs_inode_cachep) {
100 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
101 return ret;
102 }
103
104 /* now export our filesystem to lesser mortals */
105 ret = register_filesystem(&afs_fs_type);
106 if (ret < 0) {
107 kmem_cache_destroy(afs_inode_cachep);
David Howells08e0e7c2007-04-26 15:55:03 -0700108 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return ret;
110 }
111
David Howells08e0e7c2007-04-26 15:55:03 -0700112 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
David Howellsec268152007-04-26 15:49:28 -0700114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * clean up the filesystem
118 */
119void __exit afs_fs_exit(void)
120{
David Howells08e0e7c2007-04-26 15:55:03 -0700121 _enter("");
122
123 afs_mntpt_kill_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unregister_filesystem(&afs_fs_type);
125
126 if (atomic_read(&afs_count_active_inodes) != 0) {
127 printk("kAFS: %d active inode objects still present\n",
128 atomic_read(&afs_count_active_inodes));
129 BUG();
130 }
131
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000132 /*
133 * Make sure all delayed rcu free inodes are flushed before we
134 * destroy cache.
135 */
136 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 kmem_cache_destroy(afs_inode_cachep);
David Howells08e0e7c2007-04-26 15:55:03 -0700138 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
David Howells677018a2017-07-05 16:25:23 +0100142 * Display the mount device name in /proc/mounts.
143 */
144static int afs_show_devname(struct seq_file *m, struct dentry *root)
145{
146 struct afs_super_info *as = root->d_sb->s_fs_info;
147 struct afs_volume *volume = as->volume;
148 struct afs_cell *cell = volume->cell;
149 const char *suf = "";
150 char pref = '%';
151
152 switch (volume->type) {
153 case AFSVL_RWVOL:
154 break;
155 case AFSVL_ROVOL:
156 pref = '#';
157 if (volume->type_force)
158 suf = ".readonly";
159 break;
160 case AFSVL_BACKVOL:
161 pref = '#';
162 suf = ".backup";
163 break;
164 }
165
166 seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->vlocation->vldb.name, suf);
167 return 0;
168}
169
170/*
171 * Display the mount options in /proc/mounts.
172 */
173static int afs_show_options(struct seq_file *m, struct dentry *root)
174{
175 if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
176 seq_puts(m, "autocell");
177 return 0;
178}
179
180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * parse the mount options
182 * - this function has been shamelessly adapted from the ext3 fs which
183 * shamelessly adapted it from the msdos fs
184 */
David Howells00d3b7a2007-04-26 15:57:07 -0700185static int afs_parse_options(struct afs_mount_params *params,
186 char *options, const char **devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
David Howells08e0e7c2007-04-26 15:55:03 -0700188 struct afs_cell *cell;
David Howells80c72fe2007-05-03 03:11:29 -0700189 substring_t args[MAX_OPT_ARGS];
190 char *p;
191 int token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 _enter("%s", options);
194
195 options[PAGE_SIZE - 1] = 0;
196
David Howells80c72fe2007-05-03 03:11:29 -0700197 while ((p = strsep(&options, ","))) {
198 if (!*p)
199 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
David Howells80c72fe2007-05-03 03:11:29 -0700201 token = match_token(p, afs_options_list, args);
202 switch (token) {
203 case afs_opt_cell:
204 cell = afs_cell_lookup(args[0].from,
wangleibec5eb62010-08-11 09:38:04 +0100205 args[0].to - args[0].from,
206 false);
David Howells08e0e7c2007-04-26 15:55:03 -0700207 if (IS_ERR(cell))
208 return PTR_ERR(cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700209 afs_put_cell(params->cell);
210 params->cell = cell;
David Howells80c72fe2007-05-03 03:11:29 -0700211 break;
212
213 case afs_opt_rwpath:
214 params->rwpath = 1;
215 break;
216
217 case afs_opt_vol:
218 *devname = args[0].from;
219 break;
220
wangleibec5eb62010-08-11 09:38:04 +0100221 case afs_opt_autocell:
222 params->autocell = 1;
223 break;
224
David Howells80c72fe2007-05-03 03:11:29 -0700225 default:
226 printk(KERN_ERR "kAFS:"
227 " Unknown or invalid mount option: '%s'\n", p);
228 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231
David Howells80c72fe2007-05-03 03:11:29 -0700232 _leave(" = 0");
233 return 0;
David Howellsec268152007-04-26 15:49:28 -0700234}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/*
David Howells00d3b7a2007-04-26 15:57:07 -0700237 * parse a device name to get cell name, volume name, volume type and R/W
238 * selector
239 * - this can be one of the following:
240 * "%[cell:]volume[.]" R/W volume
241 * "#[cell:]volume[.]" R/O or R/W volume (rwpath=0),
242 * or R/W (rwpath=1) volume
243 * "%[cell:]volume.readonly" R/O volume
244 * "#[cell:]volume.readonly" R/O volume
245 * "%[cell:]volume.backup" Backup volume
246 * "#[cell:]volume.backup" Backup volume
247 */
248static int afs_parse_device_name(struct afs_mount_params *params,
249 const char *name)
250{
251 struct afs_cell *cell;
252 const char *cellname, *suffix;
253 int cellnamesz;
254
255 _enter(",%s", name);
256
257 if (!name) {
258 printk(KERN_ERR "kAFS: no volume name specified\n");
259 return -EINVAL;
260 }
261
262 if ((name[0] != '%' && name[0] != '#') || !name[1]) {
263 printk(KERN_ERR "kAFS: unparsable volume name\n");
264 return -EINVAL;
265 }
266
267 /* determine the type of volume we're looking for */
268 params->type = AFSVL_ROVOL;
269 params->force = false;
270 if (params->rwpath || name[0] == '%') {
271 params->type = AFSVL_RWVOL;
272 params->force = true;
273 }
274 name++;
275
276 /* split the cell name out if there is one */
277 params->volname = strchr(name, ':');
278 if (params->volname) {
279 cellname = name;
280 cellnamesz = params->volname - name;
281 params->volname++;
282 } else {
283 params->volname = name;
284 cellname = NULL;
285 cellnamesz = 0;
286 }
287
288 /* the volume type is further affected by a possible suffix */
289 suffix = strrchr(params->volname, '.');
290 if (suffix) {
291 if (strcmp(suffix, ".readonly") == 0) {
292 params->type = AFSVL_ROVOL;
293 params->force = true;
294 } else if (strcmp(suffix, ".backup") == 0) {
295 params->type = AFSVL_BACKVOL;
296 params->force = true;
297 } else if (suffix[1] == 0) {
298 } else {
299 suffix = NULL;
300 }
301 }
302
303 params->volnamesz = suffix ?
304 suffix - params->volname : strlen(params->volname);
305
306 _debug("cell %*.*s [%p]",
307 cellnamesz, cellnamesz, cellname ?: "", params->cell);
308
309 /* lookup the cell record */
310 if (cellname || !params->cell) {
wangleibec5eb62010-08-11 09:38:04 +0100311 cell = afs_cell_lookup(cellname, cellnamesz, true);
David Howells00d3b7a2007-04-26 15:57:07 -0700312 if (IS_ERR(cell)) {
wangleibec5eb62010-08-11 09:38:04 +0100313 printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
314 cellnamesz, cellnamesz, cellname ?: "");
David Howells00d3b7a2007-04-26 15:57:07 -0700315 return PTR_ERR(cell);
316 }
317 afs_put_cell(params->cell);
318 params->cell = cell;
319 }
320
321 _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
322 params->cell->name, params->cell,
323 params->volnamesz, params->volnamesz, params->volname,
324 suffix ?: "-", params->type, params->force ? " FORCE" : "");
325
326 return 0;
327}
328
329/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * check a superblock to see if it's the one we're looking for
331 */
332static int afs_test_super(struct super_block *sb, void *data)
333{
Al Virodde194a2011-06-12 16:01:21 -0400334 struct afs_super_info *as1 = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct afs_super_info *as = sb->s_fs_info;
336
Al Virodde194a2011-06-12 16:01:21 -0400337 return as->volume == as1->volume;
338}
339
340static int afs_set_super(struct super_block *sb, void *data)
341{
342 sb->s_fs_info = data;
343 return set_anon_super(sb, NULL);
David Howellsec268152007-04-26 15:49:28 -0700344}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
347 * fill in the superblock
348 */
Al Virodde194a2011-06-12 16:01:21 -0400349static int afs_fill_super(struct super_block *sb,
350 struct afs_mount_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Al Virodde194a2011-06-12 16:01:21 -0400352 struct afs_super_info *as = sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct afs_fid fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct inode *inode = NULL;
355 int ret;
356
David Howells08e0e7c2007-04-26 15:55:03 -0700357 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* fill in the superblock */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300360 sb->s_blocksize = PAGE_SIZE;
361 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 sb->s_magic = AFS_FS_MAGIC;
363 sb->s_op = &afs_super_ops;
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100364 sb->s_xattr = afs_xattr_handlers;
Jan Karaedd3ba92017-04-12 12:24:36 +0200365 ret = super_setup_bdi(sb);
366 if (ret)
367 return ret;
368 sb->s_bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
David Howells2e41ae22011-06-14 00:38:44 +0100369 strlcpy(sb->s_id, as->volume->vlocation->vldb.name, sizeof(sb->s_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* allocate the root inode and dentry */
372 fid.vid = as->volume->vid;
373 fid.vnode = 1;
374 fid.unique = 1;
David Howells260a9802007-04-26 15:59:35 -0700375 inode = afs_iget(sb, params->key, &fid, NULL, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700376 if (IS_ERR(inode))
Al Virodde194a2011-06-12 16:01:21 -0400377 return PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
wangleibec5eb62010-08-11 09:38:04 +0100379 if (params->autocell)
380 set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -0500383 sb->s_root = d_make_root(inode);
384 if (!sb->s_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto error;
386
Al Virod61dcce2011-01-12 20:04:20 -0500387 sb->s_d_op = &afs_fs_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
David Howells08e0e7c2007-04-26 15:55:03 -0700389 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391
David Howellsec268152007-04-26 15:49:28 -0700392error:
David Howells08e0e7c2007-04-26 15:55:03 -0700393 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return ret;
David Howellsec268152007-04-26 15:49:28 -0700395}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/*
398 * get an AFS superblock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Al Virof7442b32010-07-26 14:16:21 +0400400static struct dentry *afs_mount(struct file_system_type *fs_type,
401 int flags, const char *dev_name, void *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct afs_mount_params params;
404 struct super_block *sb;
David Howells08e0e7c2007-04-26 15:55:03 -0700405 struct afs_volume *vol;
David Howells00d3b7a2007-04-26 15:57:07 -0700406 struct key *key;
Miklos Szeredi969729d2008-02-08 04:21:37 -0800407 char *new_opts = kstrdup(options, GFP_KERNEL);
Al Virodde194a2011-06-12 16:01:21 -0400408 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int ret;
410
411 _enter(",,%s,%p", dev_name, options);
412
413 memset(&params, 0, sizeof(params));
414
Eric W. Biedermanf74f70f2013-01-31 04:23:54 -0800415 ret = -EINVAL;
416 if (current->nsproxy->net_ns != &init_net)
417 goto error;
418
David Howells00d3b7a2007-04-26 15:57:07 -0700419 /* parse the options and device name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (options) {
David Howells00d3b7a2007-04-26 15:57:07 -0700421 ret = afs_parse_options(&params, options, &dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (ret < 0)
423 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
David Howells00d3b7a2007-04-26 15:57:07 -0700426 ret = afs_parse_device_name(&params, dev_name);
427 if (ret < 0)
428 goto error;
429
430 /* try and do the mount securely */
431 key = afs_request_key(params.cell);
432 if (IS_ERR(key)) {
433 _leave(" = %ld [key]", PTR_ERR(key));
434 ret = PTR_ERR(key);
435 goto error;
436 }
437 params.key = key;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* parse the device name */
David Howells00d3b7a2007-04-26 15:57:07 -0700440 vol = afs_volume_lookup(&params);
David Howells08e0e7c2007-04-26 15:55:03 -0700441 if (IS_ERR(vol)) {
442 ret = PTR_ERR(vol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 goto error;
David Howells08e0e7c2007-04-26 15:55:03 -0700444 }
Al Virodde194a2011-06-12 16:01:21 -0400445
446 /* allocate a superblock info record */
447 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
448 if (!as) {
449 ret = -ENOMEM;
450 afs_put_volume(vol);
451 goto error;
452 }
453 as->volume = vol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* allocate a deviceless superblock */
David Howells9249e172012-06-25 12:55:37 +0100456 sb = sget(fs_type, afs_test_super, afs_set_super, flags, as);
David Howells08e0e7c2007-04-26 15:55:03 -0700457 if (IS_ERR(sb)) {
458 ret = PTR_ERR(sb);
Al Virodde194a2011-06-12 16:01:21 -0400459 afs_put_volume(vol);
460 kfree(as);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 goto error;
David Howells08e0e7c2007-04-26 15:55:03 -0700462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
David Howells436058a2007-04-26 15:56:24 -0700464 if (!sb->s_root) {
465 /* initial superblock/root creation */
466 _debug("create");
David Howells436058a2007-04-26 15:56:24 -0700467 ret = afs_fill_super(sb, &params);
468 if (ret < 0) {
Al Viro6f5bbff2009-05-06 01:34:22 -0400469 deactivate_locked_super(sb);
David Howells436058a2007-04-26 15:56:24 -0700470 goto error;
471 }
472 sb->s_flags |= MS_ACTIVE;
473 } else {
474 _debug("reuse");
475 ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
Al Virodde194a2011-06-12 16:01:21 -0400476 afs_put_volume(vol);
477 kfree(as);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David Howells00d3b7a2007-04-26 15:57:07 -0700480 afs_put_cell(params.cell);
Al Viro2a32ceb2009-05-08 16:05:57 -0400481 kfree(new_opts);
David Howells08e0e7c2007-04-26 15:55:03 -0700482 _leave(" = 0 [%p]", sb);
Al Virof7442b32010-07-26 14:16:21 +0400483 return dget(sb->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
David Howellsec268152007-04-26 15:49:28 -0700485error:
David Howells00d3b7a2007-04-26 15:57:07 -0700486 afs_put_cell(params.cell);
487 key_put(params.key);
Miklos Szeredi969729d2008-02-08 04:21:37 -0800488 kfree(new_opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 _leave(" = %d", ret);
Al Virof7442b32010-07-26 14:16:21 +0400490 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700491}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Al Virodde194a2011-06-12 16:01:21 -0400493static void afs_kill_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct afs_super_info *as = sb->s_fs_info;
Al Virodde194a2011-06-12 16:01:21 -0400496 kill_anon_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 afs_put_volume(as->volume);
Al Virodde194a2011-06-12 16:01:21 -0400498 kfree(as);
David Howellsec268152007-04-26 15:49:28 -0700499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
502 * initialise an inode cache slab element prior to any use
503 */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700504static void afs_i_init_once(void *_vnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
David Howellsec268152007-04-26 15:49:28 -0700506 struct afs_vnode *vnode = _vnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Christoph Lametera35afb82007-05-16 22:10:57 -0700508 memset(vnode, 0, sizeof(*vnode));
509 inode_init_once(&vnode->vfs_inode);
510 init_waitqueue_head(&vnode->update_waitq);
511 mutex_init(&vnode->permits_lock);
512 mutex_init(&vnode->validate_lock);
513 spin_lock_init(&vnode->writeback_lock);
514 spin_lock_init(&vnode->lock);
515 INIT_LIST_HEAD(&vnode->writebacks);
David Howellse8d6c552007-07-15 23:40:12 -0700516 INIT_LIST_HEAD(&vnode->pending_locks);
517 INIT_LIST_HEAD(&vnode->granted_locks);
518 INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
Christoph Lametera35afb82007-05-16 22:10:57 -0700519 INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
David Howellsec268152007-04-26 15:49:28 -0700520}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*
523 * allocate an AFS inode struct from our slab cache
524 */
525static struct inode *afs_alloc_inode(struct super_block *sb)
526{
527 struct afs_vnode *vnode;
528
David Howellsec268152007-04-26 15:49:28 -0700529 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (!vnode)
531 return NULL;
532
533 atomic_inc(&afs_count_active_inodes);
534
535 memset(&vnode->fid, 0, sizeof(vnode->fid));
536 memset(&vnode->status, 0, sizeof(vnode->status));
537
538 vnode->volume = NULL;
539 vnode->update_cnt = 0;
David Howells260a9802007-04-26 15:59:35 -0700540 vnode->flags = 1 << AFS_VNODE_UNSET;
David Howells08e0e7c2007-04-26 15:55:03 -0700541 vnode->cb_promised = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
David Howells0f300ca2007-05-10 22:22:20 -0700543 _leave(" = %p", &vnode->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return &vnode->vfs_inode;
David Howellsec268152007-04-26 15:49:28 -0700545}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100547static void afs_i_callback(struct rcu_head *head)
548{
549 struct inode *inode = container_of(head, struct inode, i_rcu);
550 struct afs_vnode *vnode = AFS_FS_I(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100551 kmem_cache_free(afs_inode_cachep, vnode);
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/*
555 * destroy an AFS inode struct
556 */
557static void afs_destroy_inode(struct inode *inode)
558{
David Howells08e0e7c2007-04-26 15:55:03 -0700559 struct afs_vnode *vnode = AFS_FS_I(inode);
560
David Howells0f300ca2007-05-10 22:22:20 -0700561 _enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
David Howells08e0e7c2007-04-26 15:55:03 -0700563 _debug("DESTROY INODE %p", inode);
564
565 ASSERTCMP(vnode->server, ==, NULL);
566
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100567 call_rcu(&inode->i_rcu, afs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 atomic_dec(&afs_count_active_inodes);
David Howellsec268152007-04-26 15:49:28 -0700569}
David Howells45222b92007-05-10 22:22:20 -0700570
571/*
572 * return information about an AFS volume
573 */
574static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
575{
576 struct afs_volume_status vs;
David Howells2b0143b2015-03-17 22:25:59 +0000577 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
David Howells45222b92007-05-10 22:22:20 -0700578 struct key *key;
579 int ret;
580
581 key = afs_request_key(vnode->volume->cell);
582 if (IS_ERR(key))
583 return PTR_ERR(key);
584
585 ret = afs_vnode_get_volume_status(vnode, key, &vs);
586 key_put(key);
587 if (ret < 0) {
588 _leave(" = %d", ret);
589 return ret;
590 }
591
592 buf->f_type = dentry->d_sb->s_magic;
593 buf->f_bsize = AFS_BLOCK_SIZE;
594 buf->f_namelen = AFSNAMEMAX - 1;
595
596 if (vs.max_quota == 0)
597 buf->f_blocks = vs.part_max_blocks;
598 else
599 buf->f_blocks = vs.max_quota;
600 buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
601 return 0;
602}