afs: Raise fake free space reporting
[openafs.git] / src / afs / LINUX24 / 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 #include "h/locks.h"
25 #if defined(AFS_LINUX24_ENV)
26 #include "h/smp_lock.h"
27 #endif
28
29
30 struct vcache *afs_globalVp = 0;
31 struct vfs *afs_globalVFS = 0;
32 #if defined(AFS_LINUX24_ENV)
33 struct vfsmount *afs_cacheMnt;
34 #endif
35 int afs_was_mounted = 0;        /* Used to force reload if mount/unmount/mount */
36
37 extern struct super_operations afs_sops;
38 extern afs_rwlock_t afs_xvcache;
39 extern struct afs_q VLRU;
40
41 extern struct dentry_operations afs_dentry_operations;
42 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
43
44 /* Forward declarations */
45 static int afs_root(struct super_block *afsp);
46 struct super_block *afs_read_super(struct super_block *sb, void *data, int silent);
47 int afs_fill_super(struct super_block *sb, void *data, int silent);
48
49 /* afs_file_system
50  * VFS entry for Linux - installed in init_module
51  * Linux mounts file systems by:
52  * 1) register_filesystem(&afs_file_system) - done in init_module
53  * 2) Mount call comes to us via do_mount -> read_super -> afs_read_super.
54  *    We are expected to setup the super_block. See afs_read_super.
55  */
56
57
58 /* afs_read_super
59  * read the "super block" for AFS - roughly eguivalent to struct vfs.
60  * dev, covered, s_rd_only, s_dirt, and s_type will be set by read_super.
61  */
62 #if defined(AFS_LINUX24_ENV)
63 DECLARE_FSTYPE(afs_fs_type, "afs", afs_read_super, 0);
64 #else
65 struct file_system_type afs_fs_type = {
66     "afs",                      /* name - used by mount operation. */
67     0,                          /* requires_dev - no for network filesystems. mount() will 
68                                  * pass us an "unnamed" device. */
69     afs_read_super,             /* wrapper to afs_mount */
70     NULL                        /* pointer to next file_system_type once registered. */
71 };
72 #endif
73
74 struct super_block *
75 afs_read_super(struct super_block *sb, void *data, int silent)
76 {
77     int code = 0;
78
79     AFS_GLOCK();
80     if (afs_was_mounted) {
81         printf
82             ("You must reload the AFS kernel extensions before remounting AFS.\n");
83         AFS_GUNLOCK();
84         return NULL;
85     }
86     afs_was_mounted = 1;
87
88     /* Set basics of super_block */
89 #if !defined(AFS_LINUX24_ENV)
90     lock_super(sb);
91 #endif
92     MOD_INC_USE_COUNT;
93
94     afs_globalVFS = sb;
95     sb->s_flags |= MS_NOATIME;
96     sb->s_blocksize = 1024;
97     sb->s_blocksize_bits = 10;
98     sb->s_magic = AFS_VFSMAGIC;
99     sb->s_op = &afs_sops;       /* Super block (vfs) ops */
100 #if defined(MAX_NON_LFS)
101 #ifdef AFS_64BIT_CLIENT
102 #if !defined(MAX_LFS_FILESIZE)
103 #if BITS_PER_LONG==32
104 #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1) 
105 #elif BITS_PER_LONG==64
106 #define MAX_LFS_FILESIZE 0x7fffffffffffffff
107 #endif
108 #endif
109     sb->s_maxbytes = MAX_LFS_FILESIZE;
110 #else
111     sb->s_maxbytes = MAX_NON_LFS;
112 #endif
113 #endif
114     code = afs_root(sb);
115     if (code) {
116         afs_globalVFS = NULL;
117         MOD_DEC_USE_COUNT;
118     }
119
120 #if !defined(AFS_LINUX24_ENV)
121     unlock_super(sb);
122 #endif
123
124     AFS_GUNLOCK();
125     return code ? NULL : sb;
126 }
127
128
129 /* afs_root - stat the root of the file system. AFS global held on entry. */
130 static int
131 afs_root(struct super_block *afsp)
132 {
133     afs_int32 code = 0;
134     struct vrequest treq;
135     struct vcache *tvp = 0;
136
137     AFS_STATCNT(afs_root);
138     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
139         tvp = afs_globalVp;
140     } else {
141         cred_t *credp = crref();
142
143         if (afs_globalVp) {
144             afs_PutVCache(afs_globalVp);
145             afs_globalVp = NULL;
146         }
147
148         if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
149             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
150             if (tvp) {
151                 struct inode *ip = AFSTOV(tvp);
152                 struct vattr vattr;
153
154                 afs_getattr(tvp, &vattr, credp);
155                 afs_fill_inode(ip, &vattr);
156
157                 /* setup super_block and mount point inode. */
158                 afs_globalVp = tvp;
159 #if defined(AFS_LINUX24_ENV)
160                 afsp->s_root = d_alloc_root(ip);
161 #else
162                 afsp->s_root = d_alloc_root(ip, NULL);
163 #endif
164                 afsp->s_root->d_op = &afs_dentry_operations;
165             } else
166                 code = ENOENT;
167         }
168         crfree(credp);
169     }
170
171     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
172                ICL_TYPE_INT32, code);
173     return code;
174 }
175
176 /* super_operations */
177
178 #if defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
179 #if defined(HAVE_KMEM_CACHE_T)
180 static kmem_cache_t *afs_inode_cachep;
181 #else
182 struct kmem_cache *afs_inode_cachep;
183 #endif
184
185 static struct inode *
186 afs_alloc_inode(struct super_block *sb)
187 {
188     struct vcache *vcp;
189
190 #if defined(SLAB_KERNEL)
191     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, SLAB_KERNEL);
192 #else
193     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
194 #endif
195     if (!vcp)
196         return NULL;
197
198     return AFSTOV(vcp);
199 }
200
201 static void
202 afs_destroy_inode(struct inode *inode)
203 {
204     kmem_cache_free(afs_inode_cachep, inode);
205 }
206
207 static void
208 #if defined(HAVE_KMEM_CACHE_T)
209 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
210 #else
211 #if defined(KMEM_CACHE_INIT)
212 init_once(struct kmem_cache * cachep, void * foo)
213 #else
214 init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
215 #endif
216 #endif
217 {
218     struct vcache *vcp = (struct vcache *) foo;
219
220 #if defined(SLAB_CTOR_VERIFY)
221     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
222         SLAB_CTOR_CONSTRUCTOR)
223 #endif
224         inode_init_once(AFSTOV(vcp));
225 }
226
227 int
228 afs_init_inodecache(void)
229 {
230 #ifndef SLAB_RECLAIM_ACCOUNT
231 #define SLAB_RECLAIM_ACCOUNT 0
232 #endif
233
234 #if defined(KMEM_CACHE_TAKES_DTOR)
235     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
236                                          sizeof(struct vcache),
237                                          0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
238                                          init_once, NULL);
239 #else
240     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
241                                          sizeof(struct vcache),
242                                          0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
243                                          init_once);
244 #endif
245     if (afs_inode_cachep == NULL)
246         return -ENOMEM;
247     return 0;
248 }
249
250 void
251 afs_destroy_inodecache(void)
252 {
253     if (afs_inode_cachep)
254         (void) kmem_cache_destroy(afs_inode_cachep);
255 }
256 #else
257 int
258 afs_init_inodecache(void)
259 {
260     return 0;
261 }
262
263 void
264 afs_destroy_inodecache(void)
265 {
266     return;
267 }
268 #endif
269
270 static void
271 afs_clear_inode(struct inode *ip)
272 {
273     struct vcache *vcp = VTOAFS(ip);
274
275     if (vcp->vlruq.prev || vcp->vlruq.next)
276         osi_Panic("inode freed while on LRU");
277     if (vcp->hnext)
278         osi_Panic("inode freed while still hashed");
279
280 #if !defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
281     afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
282 #endif
283 }
284
285 /* afs_put_super
286  * Called from unmount to release super_block. */
287 static void
288 afs_put_super(struct super_block *sbp)
289 {
290     AFS_GLOCK();
291     AFS_STATCNT(afs_unmount);
292
293     if (!suser()) {
294         AFS_GUNLOCK();
295         return;
296     }
297
298     afs_globalVFS = 0;
299     afs_globalVp = 0;
300
301     afs_shutdown();
302 #if defined(AFS_LINUX24_ENV)
303     mntput(afs_cacheMnt);
304 #endif
305
306     osi_linux_verify_alloced_memory();
307     AFS_GUNLOCK();
308
309     sbp->s_dev = 0;
310     MOD_DEC_USE_COUNT;
311 }
312
313
314 /* afs_statfs
315  * statp is in user space, so we need to cobble together a statfs, then
316  * copy it.
317  */
318 #if defined(AFS_LINUX24_ENV)
319 int
320 afs_statfs(struct super_block *sbp, struct statfs *statp)
321 #else
322 int
323 afs_statfs(struct super_block *sbp, struct statfs *__statp, int size)
324 #endif
325 {
326 #if !defined(AFS_LINUX24_ENV)
327     struct statfs stat, *statp;
328
329     if (size < sizeof(struct statfs))
330         return;
331
332     memset(&stat, 0, size);
333     statp = &stat;
334 #else
335     memset(statp, 0, sizeof(*statp));
336 #endif
337
338     AFS_STATCNT(afs_statfs);
339
340     /* hardcode in case that which is giveth is taken away */
341     statp->f_type = 0x5346414F;
342 #if defined(STATFS_TAKES_DENTRY)
343     statp->f_bsize = dentry->d_sb->s_blocksize;
344 #else
345     statp->f_bsize = sbp->s_blocksize;
346 #endif
347     statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
348         statp->f_ffree = AFS_VFS_FAKEFREE;
349     statp->f_fsid.val[0] = AFS_VFSMAGIC;
350     statp->f_fsid.val[1] = AFS_VFSFSID;
351     statp->f_namelen = 256;
352
353 #if !defined(AFS_LINUX24_ENV)
354     memcpy_tofs(__statp, &stat, size);
355 #endif
356     return 0;
357 }
358
359 struct super_operations afs_sops = {
360 #if defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
361   .alloc_inode =        afs_alloc_inode,
362   .destroy_inode =      afs_destroy_inode,
363 #endif
364   .clear_inode =        afs_clear_inode,
365   .put_super =          afs_put_super,
366   .statfs =             afs_statfs,
367 #if !defined(AFS_LINUX24_ENV)
368   .notify_change =      afs_notify_change,
369 #endif
370 };