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