Linux 3.13: Check return value from bdi_init
[openafs.git] / src / afs / LINUX / osi_vfsops.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * VFS operations for Linux
12  *
13  * super_block operations should return negated errno to Linux.
14  */
15 #include <afsconfig.h>
16 #include "afs/param.h"
17
18
19 #define __NO_VERSION__          /* don't define kernel_version in module.h */
20 #include <linux/module.h> /* early to avoid printf->printk mapping */
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "afs/afs_stats.h"
24
25 #include "osi_compat.h"
26
27 struct vcache *afs_globalVp = 0;
28 struct vfs *afs_globalVFS = 0;
29 struct vfsmount *afs_cacheMnt;
30 int afs_was_mounted = 0;        /* Used to force reload if mount/unmount/mount */
31
32 extern struct super_operations afs_sops;
33 #if !defined(AFS_NONFSTRANS)
34 extern struct export_operations afs_export_ops;
35 #endif
36 extern afs_rwlock_t afs_xvcache;
37 extern struct afs_q VLRU;
38
39 extern struct dentry_operations afs_dentry_operations;
40
41 /* Forward declarations */
42 static int afs_root(struct super_block *afsp);
43 int afs_fill_super(struct super_block *sb, void *data, int silent);
44
45
46 /*
47  * afs_mount (2.6.37+) and afs_get_sb (2.6.36-) are the entry
48  * points from the vfs when mounting afs.  The super block
49  * structure is setup in the afs_fill_super callback function.
50  */
51
52 #if defined(STRUCT_FILE_SYSTEM_TYPE_HAS_MOUNT)
53 static struct dentry *
54 afs_mount(struct file_system_type *fs_type, int flags,
55            const char *dev_name, void *data) {
56     return mount_nodev(fs_type, flags, data, afs_fill_super);
57 }
58 #elif defined(GET_SB_HAS_STRUCT_VFSMOUNT)
59 static int
60 afs_get_sb(struct file_system_type *fs_type, int flags,
61            const char *dev_name, void *data, struct vfsmount *mnt) {
62     return get_sb_nodev(fs_type, flags, data, afs_fill_super, mnt);
63 }
64 #else
65 static struct super_block *
66 afs_get_sb(struct file_system_type *fs_type, int flags,
67            const char *dev_name, void *data) {
68     return get_sb_nodev(fs_type, flags, data, afs_fill_super);
69 }
70 #endif
71
72 struct file_system_type afs_fs_type = {
73     .owner = THIS_MODULE,
74     .name = "afs",
75 #if defined(STRUCT_FILE_SYSTEM_TYPE_HAS_MOUNT)
76     .mount = afs_mount,
77 #else
78     .get_sb = afs_get_sb,
79 #endif
80     .kill_sb = kill_anon_super,
81     .fs_flags = FS_BINARY_MOUNTDATA,
82 };
83
84 struct backing_dev_info *afs_backing_dev_info;
85
86 int
87 afs_fill_super(struct super_block *sb, void *data, int silent)
88 {
89     int code = 0;
90 #if defined(HAVE_LINUX_BDI_INIT)
91     int bdi_init_done = 0;
92 #endif
93
94     AFS_GLOCK();
95     if (afs_was_mounted) {
96         printf
97             ("You must reload the AFS kernel extensions before remounting AFS.\n");
98         AFS_GUNLOCK();
99         return -EINVAL;
100     }
101     afs_was_mounted = 1;
102
103     /* Set basics of super_block */
104    __module_get(THIS_MODULE);
105
106     afs_globalVFS = sb;
107     sb->s_flags |= MS_NOATIME;
108     sb->s_blocksize = 1024;
109     sb->s_blocksize_bits = 10;
110     sb->s_magic = AFS_VFSMAGIC;
111     sb->s_op = &afs_sops;       /* Super block (vfs) ops */
112
113 #if defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
114     sb->s_d_op = &afs_dentry_operations;
115 #endif
116
117     /* used for inodes backing_dev_info field, also */
118     afs_backing_dev_info = kzalloc(sizeof(struct backing_dev_info), GFP_NOFS);
119 #if defined(HAVE_LINUX_BDI_INIT)
120     code = bdi_init(afs_backing_dev_info);
121     if (code)
122         goto out;
123     bdi_init_done = 1;
124 #endif
125 #if defined(STRUCT_BACKING_DEV_INFO_HAS_NAME)
126     afs_backing_dev_info->name = "openafs";
127 #endif
128     afs_backing_dev_info->ra_pages = 32;
129 #if defined (STRUCT_SUPER_BLOCK_HAS_S_BDI)
130     sb->s_bdi = afs_backing_dev_info;
131     /* The name specified here will appear in the flushing thread name - flush-afs */
132     bdi_register(afs_backing_dev_info, NULL, "afs");
133 #endif
134 #if !defined(AFS_NONFSTRANS)
135     sb->s_export_op = &afs_export_ops;
136 #endif
137 #if defined(MAX_NON_LFS)
138 #ifdef AFS_64BIT_CLIENT
139 #if !defined(MAX_LFS_FILESIZE)
140 #if BITS_PER_LONG==32
141 #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1) 
142 #elif BITS_PER_LONG==64
143 #define MAX_LFS_FILESIZE 0x7fffffffffffffff
144 #endif
145 #endif
146     sb->s_maxbytes = MAX_LFS_FILESIZE;
147 #else
148     sb->s_maxbytes = MAX_NON_LFS;
149 #endif
150 #endif
151     code = afs_root(sb);
152 out:
153     if (code) {
154         afs_globalVFS = NULL;
155         afs_FlushAllVCaches();
156 #if defined(HAVE_LINUX_BDI_INIT)
157         if (bdi_init_done)
158             bdi_destroy(afs_backing_dev_info);
159 #endif
160         kfree(afs_backing_dev_info);
161         module_put(THIS_MODULE);
162     }
163
164     AFS_GUNLOCK();
165     return code ? -EINVAL : 0;
166 }
167
168
169 /* afs_root - stat the root of the file system. AFS global held on entry. */
170 static int
171 afs_root(struct super_block *afsp)
172 {
173     afs_int32 code = 0;
174     struct vrequest treq;
175     struct vcache *tvp = 0;
176
177     AFS_STATCNT(afs_root);
178     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
179         tvp = afs_globalVp;
180     } else {
181         cred_t *credp = crref();
182
183         if (afs_globalVp) {
184             afs_PutVCache(afs_globalVp);
185             afs_globalVp = NULL;
186         }
187
188         if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
189             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
190             if (tvp) {
191                 struct inode *ip = AFSTOV(tvp);
192                 struct vattr vattr;
193
194                 afs_getattr(tvp, &vattr, credp);
195                 afs_fill_inode(ip, &vattr);
196
197                 /* setup super_block and mount point inode. */
198                 afs_globalVp = tvp;
199 #if defined(HAVE_LINUX_D_MAKE_ROOT)
200                 afsp->s_root = d_make_root(ip);
201 #else
202                 afsp->s_root = d_alloc_root(ip);
203 #endif
204 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
205                 afsp->s_root->d_op = &afs_dentry_operations;
206 #endif
207             } else
208                 code = ENOENT;
209         }
210         crfree(credp);
211     }
212
213     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
214                ICL_TYPE_INT32, code);
215     return code;
216 }
217
218 /* super_operations */
219
220 #if defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
221 static afs_kmem_cache_t *afs_inode_cachep;
222
223 static struct inode *
224 afs_alloc_inode(struct super_block *sb)
225 {
226     struct vcache *vcp;
227
228     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, KALLOC_TYPE);
229     if (!vcp)
230         return NULL;
231
232     return AFSTOV(vcp);
233 }
234
235 static void
236 afs_destroy_inode(struct inode *inode)
237 {
238     kmem_cache_free(afs_inode_cachep, inode);
239 }
240
241 void
242 init_once(void * foo)
243 {
244     struct vcache *vcp = (struct vcache *) foo;
245
246     inode_init_once(AFSTOV(vcp));
247 }
248
249 int
250 afs_init_inodecache(void)
251 {
252 #if defined(KMEM_CACHE_TAKES_DTOR)
253     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
254                 sizeof(struct vcache), 0,
255                 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func, NULL);
256 #else
257     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
258                 sizeof(struct vcache), 0,
259                 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func);
260 #endif
261     if (afs_inode_cachep == NULL)
262         return -ENOMEM;
263     return 0;
264 }
265
266 void
267 afs_destroy_inodecache(void)
268 {
269     if (afs_inode_cachep)
270         (void) kmem_cache_destroy(afs_inode_cachep);
271 }
272 #else
273 int
274 afs_init_inodecache(void)
275 {
276     return 0;
277 }
278
279 void
280 afs_destroy_inodecache(void)
281 {
282     return;
283 }
284 #endif
285
286 #if defined(STRUCT_SUPER_OPERATIONS_HAS_EVICT_INODE)
287 static void
288 afs_evict_inode(struct inode *ip)
289 {
290     struct vcache *vcp = VTOAFS(ip);
291
292     if (vcp->vlruq.prev || vcp->vlruq.next)
293         osi_Panic("inode freed while on LRU");
294     if (vcp->hnext)
295         osi_Panic("inode freed while still hashed");
296
297     truncate_inode_pages(&ip->i_data, 0);
298 #if defined(HAVE_LINUX_CLEAR_INODE)
299     clear_inode(ip);
300 #else
301     end_writeback(ip);
302 #endif
303
304 #if !defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
305     afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
306 #endif
307 }
308 #else
309 static void
310 afs_clear_inode(struct inode *ip)
311 {
312     struct vcache *vcp = VTOAFS(ip);
313
314     if (vcp->vlruq.prev || vcp->vlruq.next)
315         osi_Panic("inode freed while on LRU");
316     if (vcp->hnext)
317         osi_Panic("inode freed while still hashed");
318
319 #if !defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
320     afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
321 #endif
322 }
323 #endif
324
325 /* afs_put_super
326  * Called from unmount to release super_block. */
327 static void
328 afs_put_super(struct super_block *sbp)
329 {
330     AFS_GLOCK();
331     AFS_STATCNT(afs_unmount);
332
333     afs_globalVFS = 0;
334     afs_globalVp = 0;
335
336     afs_shutdown();
337     mntput(afs_cacheMnt);
338
339     osi_linux_verify_alloced_memory();
340 #if defined(HAVE_LINUX_BDI_INIT)
341     bdi_destroy(afs_backing_dev_info);
342 #endif
343     kfree(afs_backing_dev_info);
344     AFS_GUNLOCK();
345
346     sbp->s_dev = 0;
347     module_put(THIS_MODULE);
348 }
349
350
351 /* afs_statfs
352  * statp is in user space, so we need to cobble together a statfs, then
353  * copy it.
354  */
355 int
356 #if defined(STATFS_TAKES_DENTRY)
357 afs_statfs(struct dentry *dentry, struct kstatfs *statp)
358 #else
359 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
360 #endif
361 {
362     memset(statp, 0, sizeof(*statp));
363
364     AFS_STATCNT(afs_statfs);
365
366     /* hardcode in case that which is giveth is taken away */
367     statp->f_type = 0x5346414F;
368 #if defined(STATFS_TAKES_DENTRY)
369     statp->f_bsize = dentry->d_sb->s_blocksize;
370 #else
371     statp->f_bsize = sbp->s_blocksize;
372 #endif
373     statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
374         statp->f_ffree = 9000000;
375     statp->f_fsid.val[0] = AFS_VFSMAGIC;
376     statp->f_fsid.val[1] = AFS_VFSFSID;
377     statp->f_namelen = 256;
378
379     return 0;
380 }
381
382 struct super_operations afs_sops = {
383 #if defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
384   .alloc_inode =        afs_alloc_inode,
385   .destroy_inode =      afs_destroy_inode,
386 #endif
387 #if defined(STRUCT_SUPER_OPERATIONS_HAS_EVICT_INODE)
388   .evict_inode =        afs_evict_inode,
389 #else
390   .clear_inode =        afs_clear_inode,
391 #endif
392   .put_super =          afs_put_super,
393   .statfs =             afs_statfs,
394 };