blob: 875b5eb02242a0ae29b008b149770f4f6e1d3985 [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
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080051static const struct super_operations afs_super_ops = {
David Howells45222b92007-05-10 22:22:20 -070052 .statfs = afs_statfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .alloc_inode = afs_alloc_inode,
wangleibec5eb62010-08-11 09:38:04 +010054 .drop_inode = afs_drop_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .destroy_inode = afs_destroy_inode,
Al Virob57922d2010-06-07 14:34:48 -040056 .evict_inode = afs_evict_inode,
David Howells677018a2017-07-05 16:25:23 +010057 .show_devname = afs_show_devname,
58 .show_options = afs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Christoph Lametere18b8902006-12-06 20:33:20 -080061static struct kmem_cache *afs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static atomic_t afs_count_active_inodes;
63
David Howells80c72fe2007-05-03 03:11:29 -070064enum {
65 afs_no_opt,
66 afs_opt_cell,
67 afs_opt_rwpath,
68 afs_opt_vol,
wangleibec5eb62010-08-11 09:38:04 +010069 afs_opt_autocell,
David Howells80c72fe2007-05-03 03:11:29 -070070};
71
Steven Whitehousea447c092008-10-13 10:46:57 +010072static const match_table_t afs_options_list = {
David Howells80c72fe2007-05-03 03:11:29 -070073 { afs_opt_cell, "cell=%s" },
74 { afs_opt_rwpath, "rwpath" },
75 { afs_opt_vol, "vol=%s" },
wangleibec5eb62010-08-11 09:38:04 +010076 { afs_opt_autocell, "autocell" },
David Howells80c72fe2007-05-03 03:11:29 -070077 { afs_no_opt, NULL },
78};
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * initialise the filesystem
82 */
83int __init afs_fs_init(void)
84{
85 int ret;
86
87 _enter("");
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* create ourselves an inode cache */
90 atomic_set(&afs_count_active_inodes, 0);
91
92 ret = -ENOMEM;
93 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
94 sizeof(struct afs_vnode),
95 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -080096 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
Paul Mundt20c2df82007-07-20 10:11:58 +090097 afs_i_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (!afs_inode_cachep) {
99 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
100 return ret;
101 }
102
103 /* now export our filesystem to lesser mortals */
104 ret = register_filesystem(&afs_fs_type);
105 if (ret < 0) {
106 kmem_cache_destroy(afs_inode_cachep);
David Howells08e0e7c2007-04-26 15:55:03 -0700107 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return ret;
109 }
110
David Howells08e0e7c2007-04-26 15:55:03 -0700111 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
David Howellsec268152007-04-26 15:49:28 -0700113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/*
116 * clean up the filesystem
117 */
118void __exit afs_fs_exit(void)
119{
David Howells08e0e7c2007-04-26 15:55:03 -0700120 _enter("");
121
122 afs_mntpt_kill_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unregister_filesystem(&afs_fs_type);
124
125 if (atomic_read(&afs_count_active_inodes) != 0) {
126 printk("kAFS: %d active inode objects still present\n",
127 atomic_read(&afs_count_active_inodes));
128 BUG();
129 }
130
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000131 /*
132 * Make sure all delayed rcu free inodes are flushed before we
133 * destroy cache.
134 */
135 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 kmem_cache_destroy(afs_inode_cachep);
David Howells08e0e7c2007-04-26 15:55:03 -0700137 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
David Howells677018a2017-07-05 16:25:23 +0100141 * Display the mount device name in /proc/mounts.
142 */
143static int afs_show_devname(struct seq_file *m, struct dentry *root)
144{
David Howellsd2ddc772017-11-02 15:27:50 +0000145 struct afs_super_info *as = AFS_FS_S(root->d_sb);
David Howells677018a2017-07-05 16:25:23 +0100146 struct afs_volume *volume = as->volume;
David Howellsd2ddc772017-11-02 15:27:50 +0000147 struct afs_cell *cell = as->cell;
David Howells677018a2017-07-05 16:25:23 +0100148 const char *suf = "";
149 char pref = '%';
150
151 switch (volume->type) {
152 case AFSVL_RWVOL:
153 break;
154 case AFSVL_ROVOL:
155 pref = '#';
156 if (volume->type_force)
157 suf = ".readonly";
158 break;
159 case AFSVL_BACKVOL:
160 pref = '#';
161 suf = ".backup";
162 break;
163 }
164
David Howellsd2ddc772017-11-02 15:27:50 +0000165 seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->name, suf);
David Howells677018a2017-07-05 16:25:23 +0100166 return 0;
167}
168
169/*
170 * Display the mount options in /proc/mounts.
171 */
172static int afs_show_options(struct seq_file *m, struct dentry *root)
173{
174 if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
175 seq_puts(m, "autocell");
176 return 0;
177}
178
179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * parse the mount options
181 * - this function has been shamelessly adapted from the ext3 fs which
182 * shamelessly adapted it from the msdos fs
183 */
David Howells00d3b7a2007-04-26 15:57:07 -0700184static int afs_parse_options(struct afs_mount_params *params,
185 char *options, const char **devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
David Howells08e0e7c2007-04-26 15:55:03 -0700187 struct afs_cell *cell;
David Howells80c72fe2007-05-03 03:11:29 -0700188 substring_t args[MAX_OPT_ARGS];
189 char *p;
190 int token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 _enter("%s", options);
193
194 options[PAGE_SIZE - 1] = 0;
195
David Howells80c72fe2007-05-03 03:11:29 -0700196 while ((p = strsep(&options, ","))) {
197 if (!*p)
198 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
David Howells80c72fe2007-05-03 03:11:29 -0700200 token = match_token(p, afs_options_list, args);
201 switch (token) {
202 case afs_opt_cell:
David Howells989782d2017-11-02 15:27:50 +0000203 rcu_read_lock();
204 cell = afs_lookup_cell_rcu(params->net,
205 args[0].from,
206 args[0].to - args[0].from);
207 rcu_read_unlock();
David Howells08e0e7c2007-04-26 15:55:03 -0700208 if (IS_ERR(cell))
209 return PTR_ERR(cell);
David Howells9ed900b2017-11-02 15:27:46 +0000210 afs_put_cell(params->net, params->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700211 params->cell = cell;
David Howells80c72fe2007-05-03 03:11:29 -0700212 break;
213
214 case afs_opt_rwpath:
215 params->rwpath = 1;
216 break;
217
218 case afs_opt_vol:
219 *devname = args[0].from;
220 break;
221
wangleibec5eb62010-08-11 09:38:04 +0100222 case afs_opt_autocell:
223 params->autocell = 1;
224 break;
225
David Howells80c72fe2007-05-03 03:11:29 -0700226 default:
227 printk(KERN_ERR "kAFS:"
228 " Unknown or invalid mount option: '%s'\n", p);
229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
David Howells80c72fe2007-05-03 03:11:29 -0700233 _leave(" = 0");
234 return 0;
David Howellsec268152007-04-26 15:49:28 -0700235}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/*
David Howells00d3b7a2007-04-26 15:57:07 -0700238 * parse a device name to get cell name, volume name, volume type and R/W
239 * selector
240 * - this can be one of the following:
241 * "%[cell:]volume[.]" R/W volume
242 * "#[cell:]volume[.]" R/O or R/W volume (rwpath=0),
243 * or R/W (rwpath=1) volume
244 * "%[cell:]volume.readonly" R/O volume
245 * "#[cell:]volume.readonly" R/O volume
246 * "%[cell:]volume.backup" Backup volume
247 * "#[cell:]volume.backup" Backup volume
248 */
249static int afs_parse_device_name(struct afs_mount_params *params,
250 const char *name)
251{
252 struct afs_cell *cell;
253 const char *cellname, *suffix;
254 int cellnamesz;
255
256 _enter(",%s", name);
257
258 if (!name) {
259 printk(KERN_ERR "kAFS: no volume name specified\n");
260 return -EINVAL;
261 }
262
263 if ((name[0] != '%' && name[0] != '#') || !name[1]) {
264 printk(KERN_ERR "kAFS: unparsable volume name\n");
265 return -EINVAL;
266 }
267
268 /* determine the type of volume we're looking for */
269 params->type = AFSVL_ROVOL;
270 params->force = false;
271 if (params->rwpath || name[0] == '%') {
272 params->type = AFSVL_RWVOL;
273 params->force = true;
274 }
275 name++;
276
277 /* split the cell name out if there is one */
278 params->volname = strchr(name, ':');
279 if (params->volname) {
280 cellname = name;
281 cellnamesz = params->volname - name;
282 params->volname++;
283 } else {
284 params->volname = name;
285 cellname = NULL;
286 cellnamesz = 0;
287 }
288
289 /* the volume type is further affected by a possible suffix */
290 suffix = strrchr(params->volname, '.');
291 if (suffix) {
292 if (strcmp(suffix, ".readonly") == 0) {
293 params->type = AFSVL_ROVOL;
294 params->force = true;
295 } else if (strcmp(suffix, ".backup") == 0) {
296 params->type = AFSVL_BACKVOL;
297 params->force = true;
298 } else if (suffix[1] == 0) {
299 } else {
300 suffix = NULL;
301 }
302 }
303
304 params->volnamesz = suffix ?
305 suffix - params->volname : strlen(params->volname);
306
307 _debug("cell %*.*s [%p]",
308 cellnamesz, cellnamesz, cellname ?: "", params->cell);
309
310 /* lookup the cell record */
311 if (cellname || !params->cell) {
David Howells989782d2017-11-02 15:27:50 +0000312 cell = afs_lookup_cell(params->net, cellname, cellnamesz,
313 NULL, false);
David Howells00d3b7a2007-04-26 15:57:07 -0700314 if (IS_ERR(cell)) {
wangleibec5eb62010-08-11 09:38:04 +0100315 printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
316 cellnamesz, cellnamesz, cellname ?: "");
David Howells00d3b7a2007-04-26 15:57:07 -0700317 return PTR_ERR(cell);
318 }
David Howells9ed900b2017-11-02 15:27:46 +0000319 afs_put_cell(params->net, params->cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700320 params->cell = cell;
321 }
322
323 _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
324 params->cell->name, params->cell,
325 params->volnamesz, params->volnamesz, params->volname,
326 suffix ?: "-", params->type, params->force ? " FORCE" : "");
327
328 return 0;
329}
330
331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * check a superblock to see if it's the one we're looking for
333 */
334static int afs_test_super(struct super_block *sb, void *data)
335{
Al Virodde194a2011-06-12 16:01:21 -0400336 struct afs_super_info *as1 = data;
David Howellsd2ddc772017-11-02 15:27:50 +0000337 struct afs_super_info *as = AFS_FS_S(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
David Howellsd2ddc772017-11-02 15:27:50 +0000339 return as->net == as1->net && as->volume->vid == as1->volume->vid;
Al Virodde194a2011-06-12 16:01:21 -0400340}
341
342static int afs_set_super(struct super_block *sb, void *data)
343{
David Howellsd2ddc772017-11-02 15:27:50 +0000344 struct afs_super_info *as = data;
345
346 sb->s_fs_info = as;
Al Virodde194a2011-06-12 16:01:21 -0400347 return set_anon_super(sb, NULL);
David Howellsec268152007-04-26 15:49:28 -0700348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/*
351 * fill in the superblock
352 */
Al Virodde194a2011-06-12 16:01:21 -0400353static int afs_fill_super(struct super_block *sb,
354 struct afs_mount_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
David Howellsd2ddc772017-11-02 15:27:50 +0000356 struct afs_super_info *as = AFS_FS_S(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct afs_fid fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct inode *inode = NULL;
359 int ret;
360
David Howells08e0e7c2007-04-26 15:55:03 -0700361 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /* fill in the superblock */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300364 sb->s_blocksize = PAGE_SIZE;
365 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 sb->s_magic = AFS_FS_MAGIC;
367 sb->s_op = &afs_super_ops;
David Howellsd3e3b7ea2017-07-06 15:50:27 +0100368 sb->s_xattr = afs_xattr_handlers;
Jan Karaedd3ba92017-04-12 12:24:36 +0200369 ret = super_setup_bdi(sb);
370 if (ret)
371 return ret;
372 sb->s_bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
David Howellsd2ddc772017-11-02 15:27:50 +0000373 sprintf(sb->s_id, "%u", as->volume->vid);
374
375 afs_activate_volume(as->volume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* allocate the root inode and dentry */
378 fid.vid = as->volume->vid;
379 fid.vnode = 1;
380 fid.unique = 1;
David Howellsd2ddc772017-11-02 15:27:50 +0000381 inode = afs_iget(sb, params->key, &fid, NULL, NULL, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700382 if (IS_ERR(inode))
Al Virodde194a2011-06-12 16:01:21 -0400383 return PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
wangleibec5eb62010-08-11 09:38:04 +0100385 if (params->autocell)
386 set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -0500389 sb->s_root = d_make_root(inode);
390 if (!sb->s_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 goto error;
392
Al Virod61dcce2011-01-12 20:04:20 -0500393 sb->s_d_op = &afs_fs_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David Howells08e0e7c2007-04-26 15:55:03 -0700395 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0;
397
David Howellsec268152007-04-26 15:49:28 -0700398error:
David Howells08e0e7c2007-04-26 15:55:03 -0700399 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return ret;
David Howellsec268152007-04-26 15:49:28 -0700401}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David Howells49566f62017-11-02 15:27:46 +0000403static struct afs_super_info *afs_alloc_sbi(struct afs_mount_params *params)
404{
405 struct afs_super_info *as;
406
407 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
408 if (as) {
409 as->net = afs_get_net(params->net);
410 as->cell = afs_get_cell(params->cell);
411 }
412 return as;
413}
414
415static void afs_destroy_sbi(struct afs_super_info *as)
416{
417 if (as) {
David Howells9ed900b2017-11-02 15:27:46 +0000418 afs_put_volume(as->cell, as->volume);
419 afs_put_cell(as->net, as->cell);
David Howells49566f62017-11-02 15:27:46 +0000420 afs_put_net(as->net);
421 kfree(as);
422 }
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/*
426 * get an AFS superblock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Al Virof7442b32010-07-26 14:16:21 +0400428static struct dentry *afs_mount(struct file_system_type *fs_type,
David Howells49566f62017-11-02 15:27:46 +0000429 int flags, const char *dev_name, void *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct afs_mount_params params;
432 struct super_block *sb;
David Howellsd2ddc772017-11-02 15:27:50 +0000433 struct afs_volume *candidate;
David Howells00d3b7a2007-04-26 15:57:07 -0700434 struct key *key;
Al Virodde194a2011-06-12 16:01:21 -0400435 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int ret;
437
438 _enter(",,%s,%p", dev_name, options);
439
440 memset(&params, 0, sizeof(params));
David Howellsf044c882017-11-02 15:27:45 +0000441 params.net = &__afs_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Eric W. Biedermanf74f70f2013-01-31 04:23:54 -0800443 ret = -EINVAL;
444 if (current->nsproxy->net_ns != &init_net)
445 goto error;
446
David Howells00d3b7a2007-04-26 15:57:07 -0700447 /* parse the options and device name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (options) {
David Howells00d3b7a2007-04-26 15:57:07 -0700449 ret = afs_parse_options(&params, options, &dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (ret < 0)
451 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
David Howells00d3b7a2007-04-26 15:57:07 -0700454 ret = afs_parse_device_name(&params, dev_name);
455 if (ret < 0)
456 goto error;
457
458 /* try and do the mount securely */
459 key = afs_request_key(params.cell);
460 if (IS_ERR(key)) {
461 _leave(" = %ld [key]", PTR_ERR(key));
462 ret = PTR_ERR(key);
463 goto error;
464 }
465 params.key = key;
466
David Howells49566f62017-11-02 15:27:46 +0000467 /* allocate a superblock info record */
468 ret = -ENOMEM;
469 as = afs_alloc_sbi(&params);
470 if (!as)
David Howellsd2ddc772017-11-02 15:27:50 +0000471 goto error_key;
David Howells49566f62017-11-02 15:27:46 +0000472
David Howellsd2ddc772017-11-02 15:27:50 +0000473 /* Assume we're going to need a volume record; at the very least we can
474 * use it to update the volume record if we have one already. This
475 * checks that the volume exists within the cell.
476 */
477 candidate = afs_create_volume(&params);
478 if (IS_ERR(candidate)) {
479 ret = PTR_ERR(candidate);
480 goto error_as;
David Howells08e0e7c2007-04-26 15:55:03 -0700481 }
David Howellsd2ddc772017-11-02 15:27:50 +0000482
483 as->volume = candidate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* allocate a deviceless superblock */
David Howells9249e172012-06-25 12:55:37 +0100486 sb = sget(fs_type, afs_test_super, afs_set_super, flags, as);
David Howells08e0e7c2007-04-26 15:55:03 -0700487 if (IS_ERR(sb)) {
488 ret = PTR_ERR(sb);
David Howellsf044c882017-11-02 15:27:45 +0000489 goto error_as;
David Howells08e0e7c2007-04-26 15:55:03 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David Howells436058a2007-04-26 15:56:24 -0700492 if (!sb->s_root) {
493 /* initial superblock/root creation */
494 _debug("create");
David Howells436058a2007-04-26 15:56:24 -0700495 ret = afs_fill_super(sb, &params);
David Howellsf044c882017-11-02 15:27:45 +0000496 if (ret < 0)
497 goto error_sb;
David Howells49566f62017-11-02 15:27:46 +0000498 as = NULL;
David Howells436058a2007-04-26 15:56:24 -0700499 sb->s_flags |= MS_ACTIVE;
500 } else {
501 _debug("reuse");
502 ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
David Howells49566f62017-11-02 15:27:46 +0000503 afs_destroy_sbi(as);
504 as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
David Howells9ed900b2017-11-02 15:27:46 +0000507 afs_put_cell(params.net, params.cell);
David Howells49566f62017-11-02 15:27:46 +0000508 key_put(params.key);
David Howells08e0e7c2007-04-26 15:55:03 -0700509 _leave(" = 0 [%p]", sb);
Al Virof7442b32010-07-26 14:16:21 +0400510 return dget(sb->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
David Howellsf044c882017-11-02 15:27:45 +0000512error_sb:
513 deactivate_locked_super(sb);
David Howellsd2ddc772017-11-02 15:27:50 +0000514 goto error_key;
David Howellsf044c882017-11-02 15:27:45 +0000515error_as:
David Howells49566f62017-11-02 15:27:46 +0000516 afs_destroy_sbi(as);
David Howellsd2ddc772017-11-02 15:27:50 +0000517error_key:
518 key_put(params.key);
David Howellsec268152007-04-26 15:49:28 -0700519error:
David Howells9ed900b2017-11-02 15:27:46 +0000520 afs_put_cell(params.net, params.cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 _leave(" = %d", ret);
Al Virof7442b32010-07-26 14:16:21 +0400522 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700523}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Al Virodde194a2011-06-12 16:01:21 -0400525static void afs_kill_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
David Howellsc435ee32017-11-02 15:27:49 +0000527 struct afs_super_info *as = AFS_FS_S(sb);
David Howellsf044c882017-11-02 15:27:45 +0000528
David Howellsc435ee32017-11-02 15:27:49 +0000529 /* Clear the callback interests (which will do ilookup5) before
530 * deactivating the superblock.
531 */
David Howellsd2ddc772017-11-02 15:27:50 +0000532 afs_clear_callback_interests(as->net, as->volume->servers);
Al Virodde194a2011-06-12 16:01:21 -0400533 kill_anon_super(sb);
David Howellsd2ddc772017-11-02 15:27:50 +0000534 afs_deactivate_volume(as->volume);
David Howells49566f62017-11-02 15:27:46 +0000535 afs_destroy_sbi(as);
David Howellsec268152007-04-26 15:49:28 -0700536}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/*
539 * initialise an inode cache slab element prior to any use
540 */
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700541static void afs_i_init_once(void *_vnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
David Howellsec268152007-04-26 15:49:28 -0700543 struct afs_vnode *vnode = _vnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Christoph Lametera35afb82007-05-16 22:10:57 -0700545 memset(vnode, 0, sizeof(*vnode));
546 inode_init_once(&vnode->vfs_inode);
David Howellsd2ddc772017-11-02 15:27:50 +0000547 mutex_init(&vnode->io_lock);
Christoph Lametera35afb82007-05-16 22:10:57 -0700548 mutex_init(&vnode->validate_lock);
David Howells4343d002017-11-02 15:27:52 +0000549 spin_lock_init(&vnode->wb_lock);
Christoph Lametera35afb82007-05-16 22:10:57 -0700550 spin_lock_init(&vnode->lock);
David Howells4343d002017-11-02 15:27:52 +0000551 INIT_LIST_HEAD(&vnode->wb_keys);
David Howellse8d6c552007-07-15 23:40:12 -0700552 INIT_LIST_HEAD(&vnode->pending_locks);
553 INIT_LIST_HEAD(&vnode->granted_locks);
554 INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
David Howellsc435ee32017-11-02 15:27:49 +0000555 seqlock_init(&vnode->cb_lock);
David Howellsec268152007-04-26 15:49:28 -0700556}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * allocate an AFS inode struct from our slab cache
560 */
561static struct inode *afs_alloc_inode(struct super_block *sb)
562{
563 struct afs_vnode *vnode;
564
David Howellsec268152007-04-26 15:49:28 -0700565 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!vnode)
567 return NULL;
568
569 atomic_inc(&afs_count_active_inodes);
570
571 memset(&vnode->fid, 0, sizeof(vnode->fid));
572 memset(&vnode->status, 0, sizeof(vnode->status));
573
574 vnode->volume = NULL;
David Howells260a9802007-04-26 15:59:35 -0700575 vnode->flags = 1 << AFS_VNODE_UNSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
David Howells0f300ca2007-05-10 22:22:20 -0700577 _leave(" = %p", &vnode->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return &vnode->vfs_inode;
David Howellsec268152007-04-26 15:49:28 -0700579}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100581static void afs_i_callback(struct rcu_head *head)
582{
583 struct inode *inode = container_of(head, struct inode, i_rcu);
584 struct afs_vnode *vnode = AFS_FS_I(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100585 kmem_cache_free(afs_inode_cachep, vnode);
586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/*
589 * destroy an AFS inode struct
590 */
591static void afs_destroy_inode(struct inode *inode)
592{
David Howells08e0e7c2007-04-26 15:55:03 -0700593 struct afs_vnode *vnode = AFS_FS_I(inode);
594
David Howells0f300ca2007-05-10 22:22:20 -0700595 _enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
David Howells08e0e7c2007-04-26 15:55:03 -0700597 _debug("DESTROY INODE %p", inode);
598
David Howellsc435ee32017-11-02 15:27:49 +0000599 ASSERTCMP(vnode->cb_interest, ==, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700600
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100601 call_rcu(&inode->i_rcu, afs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 atomic_dec(&afs_count_active_inodes);
David Howellsec268152007-04-26 15:49:28 -0700603}
David Howells45222b92007-05-10 22:22:20 -0700604
605/*
606 * return information about an AFS volume
607 */
608static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
609{
David Howellsd2ddc772017-11-02 15:27:50 +0000610 struct afs_fs_cursor fc;
David Howells45222b92007-05-10 22:22:20 -0700611 struct afs_volume_status vs;
David Howells2b0143b2015-03-17 22:25:59 +0000612 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
David Howells45222b92007-05-10 22:22:20 -0700613 struct key *key;
614 int ret;
615
616 key = afs_request_key(vnode->volume->cell);
617 if (IS_ERR(key))
618 return PTR_ERR(key);
619
David Howellsd2ddc772017-11-02 15:27:50 +0000620 ret = -ERESTARTSYS;
621 if (afs_begin_vnode_operation(&fc, vnode, key)) {
622 fc.flags |= AFS_FS_CURSOR_NO_VSLEEP;
623 while (afs_select_fileserver(&fc)) {
624 fc.cb_break = vnode->cb_break + vnode->cb_s_break;
625 afs_fs_get_volume_status(&fc, &vs);
626 }
627
628 afs_check_for_remote_deletion(&fc, fc.vnode);
629 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
630 ret = afs_end_vnode_operation(&fc);
David Howells45222b92007-05-10 22:22:20 -0700631 }
632
David Howellsd2ddc772017-11-02 15:27:50 +0000633 key_put(key);
David Howells45222b92007-05-10 22:22:20 -0700634
David Howellsd2ddc772017-11-02 15:27:50 +0000635 if (ret == 0) {
636 buf->f_type = dentry->d_sb->s_magic;
637 buf->f_bsize = AFS_BLOCK_SIZE;
638 buf->f_namelen = AFSNAMEMAX - 1;
639
640 if (vs.max_quota == 0)
641 buf->f_blocks = vs.part_max_blocks;
642 else
643 buf->f_blocks = vs.max_quota;
644 buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
645 }
646
647 return ret;
David Howells45222b92007-05-10 22:22:20 -0700648}