Linux 4.13: use designated initializers where required
[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 #if defined(HAVE_LINUX_SUPER_SETUP_BDI)
117     code = super_setup_bdi(sb);
118     if (code)
119         goto out;
120     sb->s_bdi->name = "openafs";
121     sb->s_bdi->ra_pages = 32;
122 #else
123     /* used for inodes backing_dev_info field, also */
124     afs_backing_dev_info = kzalloc(sizeof(struct backing_dev_info), GFP_NOFS);
125 #if defined(HAVE_LINUX_BDI_INIT)
126     code = bdi_init(afs_backing_dev_info);
127     if (code)
128         goto out;
129     bdi_init_done = 1;
130 #endif
131 #if defined(STRUCT_BACKING_DEV_INFO_HAS_NAME)
132     afs_backing_dev_info->name = "openafs";
133 #endif
134     afs_backing_dev_info->ra_pages = 32;
135 #if defined (STRUCT_SUPER_BLOCK_HAS_S_BDI)
136     sb->s_bdi = afs_backing_dev_info;
137     /* The name specified here will appear in the flushing thread name - flush-afs */
138     bdi_register(afs_backing_dev_info, NULL, "afs");
139 #endif
140 #endif /* HAVE_LINUX_SUPER_SETUP_BDI */
141 #if !defined(AFS_NONFSTRANS)
142     sb->s_export_op = &afs_export_ops;
143 #endif
144 #if defined(MAX_NON_LFS)
145 #ifdef AFS_64BIT_CLIENT
146 #if !defined(MAX_LFS_FILESIZE)
147 #if BITS_PER_LONG==32
148 #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1) 
149 #elif BITS_PER_LONG==64
150 #define MAX_LFS_FILESIZE 0x7fffffffffffffff
151 #endif
152 #endif
153     sb->s_maxbytes = MAX_LFS_FILESIZE;
154 #else
155     sb->s_maxbytes = MAX_NON_LFS;
156 #endif
157 #endif
158     code = afs_root(sb);
159 out:
160     if (code) {
161         afs_globalVFS = NULL;
162         afs_FlushAllVCaches();
163 #if defined(HAVE_LINUX_BDI_INIT)
164         if (bdi_init_done)
165             bdi_destroy(afs_backing_dev_info);
166 #endif
167 #if !defined(HAVE_LINUX_SUPER_SETUP_BDI)
168         kfree(afs_backing_dev_info);
169 #endif
170         module_put(THIS_MODULE);
171     }
172
173     AFS_GUNLOCK();
174     return code ? -EINVAL : 0;
175 }
176
177
178 /* afs_root - stat the root of the file system. AFS global held on entry. */
179 static int
180 afs_root(struct super_block *afsp)
181 {
182     afs_int32 code = 0;
183     struct vcache *tvp = 0;
184
185     AFS_STATCNT(afs_root);
186     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
187         tvp = afs_globalVp;
188     } else {
189         struct vrequest *treq = NULL;
190         cred_t *credp = crref();
191
192         if (afs_globalVp) {
193             afs_PutVCache(afs_globalVp);
194             afs_globalVp = NULL;
195         }
196
197         if (!(code = afs_CreateReq(&treq, credp)) && !(code = afs_CheckInit())) {
198             tvp = afs_GetVCache(&afs_rootFid, treq, NULL, NULL);
199             if (tvp) {
200                 struct inode *ip = AFSTOV(tvp);
201                 struct vattr *vattr = NULL;
202
203                 code = afs_CreateAttr(&vattr);
204                 if (!code) {
205                     afs_getattr(tvp, vattr, credp);
206                     afs_fill_inode(ip, vattr);
207
208                     /* setup super_block and mount point inode. */
209                     afs_globalVp = tvp;
210 #if defined(HAVE_LINUX_D_MAKE_ROOT)
211                     afsp->s_root = d_make_root(ip);
212 #else
213                     afsp->s_root = d_alloc_root(ip);
214 #endif
215 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
216                     afsp->s_root->d_op = &afs_dentry_operations;
217 #endif
218                     afs_DestroyAttr(vattr);
219                 }
220             } else
221                 code = EIO;
222         }
223         crfree(credp);
224         afs_DestroyReq(treq);
225     }
226
227     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
228                ICL_TYPE_INT32, code);
229     return code;
230 }
231
232 /* super_operations */
233
234 #if defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
235 static afs_kmem_cache_t *afs_inode_cachep;
236
237 static struct inode *
238 afs_alloc_inode(struct super_block *sb)
239 {
240     struct vcache *vcp;
241
242     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, KALLOC_TYPE);
243     if (!vcp)
244         return NULL;
245
246     return AFSTOV(vcp);
247 }
248
249 static void
250 afs_destroy_inode(struct inode *inode)
251 {
252     kmem_cache_free(afs_inode_cachep, inode);
253 }
254
255 void
256 init_once(void * foo)
257 {
258     struct vcache *vcp = (struct vcache *) foo;
259
260     inode_init_once(AFSTOV(vcp));
261 }
262
263 int
264 afs_init_inodecache(void)
265 {
266 #if defined(KMEM_CACHE_TAKES_DTOR)
267     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
268                 sizeof(struct vcache), 0,
269                 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func, NULL);
270 #else
271     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
272                 sizeof(struct vcache), 0,
273                 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func);
274 #endif
275     if (afs_inode_cachep == NULL)
276         return -ENOMEM;
277     return 0;
278 }
279
280 void
281 afs_destroy_inodecache(void)
282 {
283     if (afs_inode_cachep)
284         (void) kmem_cache_destroy(afs_inode_cachep);
285 }
286 #else
287 int
288 afs_init_inodecache(void)
289 {
290     return 0;
291 }
292
293 void
294 afs_destroy_inodecache(void)
295 {
296     return;
297 }
298 #endif
299
300 #if defined(STRUCT_SUPER_OPERATIONS_HAS_EVICT_INODE)
301 static void
302 afs_evict_inode(struct inode *ip)
303 {
304     struct vcache *vcp = VTOAFS(ip);
305
306     if (vcp->vlruq.prev || vcp->vlruq.next)
307         osi_Panic("inode freed while on LRU");
308     if (vcp->hnext)
309         osi_Panic("inode freed while still hashed");
310
311     truncate_inode_pages(&ip->i_data, 0);
312 #if defined(HAVE_LINUX_CLEAR_INODE)
313     clear_inode(ip);
314 #else
315     end_writeback(ip);
316 #endif
317
318 #if !defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
319     afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
320 #endif
321 }
322 #else
323 static void
324 afs_clear_inode(struct inode *ip)
325 {
326     struct vcache *vcp = VTOAFS(ip);
327
328     if (vcp->vlruq.prev || vcp->vlruq.next)
329         osi_Panic("inode freed while on LRU");
330     if (vcp->hnext)
331         osi_Panic("inode freed while still hashed");
332
333 #if !defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
334     afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
335 #endif
336 }
337 #endif
338
339 /* afs_put_super
340  * Called from unmount to release super_block. */
341 static void
342 afs_put_super(struct super_block *sbp)
343 {
344     AFS_GLOCK();
345     AFS_STATCNT(afs_unmount);
346
347     afs_globalVFS = 0;
348     afs_globalVp = 0;
349
350     afs_shutdown();
351     mntput(afs_cacheMnt);
352
353     osi_linux_verify_alloced_memory();
354 #if defined(HAVE_LINUX_BDI_INIT)
355     bdi_destroy(afs_backing_dev_info);
356 #endif
357     kfree(afs_backing_dev_info);
358     AFS_GUNLOCK();
359
360     sbp->s_dev = 0;
361     module_put(THIS_MODULE);
362 }
363
364
365 /* afs_statfs
366  * statp is in user space, so we need to cobble together a statfs, then
367  * copy it.
368  */
369 int
370 #if defined(STATFS_TAKES_DENTRY)
371 afs_statfs(struct dentry *dentry, struct kstatfs *statp)
372 #else
373 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
374 #endif
375 {
376     memset(statp, 0, sizeof(*statp));
377
378     AFS_STATCNT(afs_statfs);
379
380     /* hardcode in case that which is giveth is taken away */
381     statp->f_type = 0x5346414F;
382 #if defined(STATFS_TAKES_DENTRY)
383     statp->f_bsize = dentry->d_sb->s_blocksize;
384 #else
385     statp->f_bsize = sbp->s_blocksize;
386 #endif
387     statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
388         statp->f_ffree = AFS_VFS_FAKEFREE;
389     statp->f_fsid.val[0] = AFS_VFSMAGIC;
390     statp->f_fsid.val[1] = AFS_VFSFSID;
391     statp->f_namelen = 256;
392
393     return 0;
394 }
395
396 struct super_operations afs_sops = {
397 #if defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
398   .alloc_inode =        afs_alloc_inode,
399   .destroy_inode =      afs_destroy_inode,
400 #endif
401 #if defined(STRUCT_SUPER_OPERATIONS_HAS_EVICT_INODE)
402   .evict_inode =        afs_evict_inode,
403 #else
404   .clear_inode =        afs_clear_inode,
405 #endif
406   .put_super =          afs_put_super,
407   .statfs =             afs_statfs,
408 };