Rationalise some #ifdefs in the LINUX osi layer
[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 #include "h/smp_lock.h"
25
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 void iattr2vattr(struct vattr *vattrp, struct iattr *iattrp);
43 static int afs_root(struct super_block *afsp);
44 struct super_block *afs_read_super(struct super_block *sb, void *data, int silent);
45 int afs_fill_super(struct super_block *sb, void *data, int silent);
46
47 /* afs_file_system
48  * VFS entry for Linux - installed in init_module
49  * Linux mounts file systems by:
50  * 1) register_filesystem(&afs_file_system) - done in init_module
51  * 2) Mount call comes to us via do_mount -> read_super -> afs_read_super.
52  *    We are expected to setup the super_block. See afs_read_super.
53  */
54
55
56 /* afs_read_super
57  * read the "super block" for AFS - roughly eguivalent to struct vfs.
58  * dev, covered, s_rd_only, s_dirt, and s_type will be set by read_super.
59  */
60 #ifdef GET_SB_HAS_STRUCT_VFSMOUNT
61 static int
62 afs_get_sb(struct file_system_type *fs_type, int flags,
63            const char *dev_name, void *data, struct vfsmount *mnt) {
64     return get_sb_nodev(fs_type, flags, data, afs_fill_super, mnt);
65 }
66 #else
67 static struct superblock *
68 afs_get_sb(struct file_system_type *fs_type, int flags,
69            const char *dev_name, void *data)
70     return get_sb_nodev(fs_type, flags, data, afs_fill_super);
71 }
72 #endif
73
74 struct file_system_type afs_fs_type = {
75     .owner = THIS_MODULE,
76     .name = "afs",
77     .get_sb = afs_get_sb,
78     .kill_sb = kill_anon_super,
79     .fs_flags = FS_BINARY_MOUNTDATA,
80 };
81
82 struct backing_dev_info afs_backing_dev_info = {
83     .ra_pages           = 32,
84 };
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_blocksize = 1024;
105     sb->s_blocksize_bits = 10;
106     sb->s_magic = AFS_VFSMAGIC;
107     sb->s_op = &afs_sops;       /* Super block (vfs) ops */
108 #if defined(HAVE_BDI_INIT)
109     bdi_init(&afs_backing_dev_info);
110 #endif
111 #if !defined(AFS_NONFSTRANS)
112     sb->s_export_op = &afs_export_ops;
113 #endif
114 #if defined(MAX_NON_LFS)
115 #ifdef AFS_64BIT_CLIENT
116 #if !defined(MAX_LFS_FILESIZE)
117 #if BITS_PER_LONG==32
118 #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1) 
119 #elif BITS_PER_LONG==64
120 #define MAX_LFS_FILESIZE 0x7fffffffffffffff
121 #endif
122 #endif
123     sb->s_maxbytes = MAX_LFS_FILESIZE;
124 #else
125     sb->s_maxbytes = MAX_NON_LFS;
126 #endif
127 #endif
128     code = afs_root(sb);
129     if (code) {
130         afs_globalVFS = NULL;
131         module_put(THIS_MODULE);
132     }
133
134     AFS_GUNLOCK();
135     return code ? -EINVAL : 0;
136 }
137
138
139 /* afs_root - stat the root of the file system. AFS global held on entry. */
140 static int
141 afs_root(struct super_block *afsp)
142 {
143     register afs_int32 code = 0;
144     struct vrequest treq;
145     register struct vcache *tvp = 0;
146
147     AFS_STATCNT(afs_root);
148     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
149         tvp = afs_globalVp;
150     } else {
151         cred_t *credp = crref();
152
153         if (afs_globalVp) {
154             afs_PutVCache(afs_globalVp);
155             afs_globalVp = NULL;
156         }
157
158         if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
159             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
160             if (tvp) {
161                 struct inode *ip = AFSTOV(tvp);
162                 struct vattr vattr;
163
164                 afs_getattr(tvp, &vattr, credp);
165                 afs_fill_inode(ip, &vattr);
166
167                 /* setup super_block and mount point inode. */
168                 afs_globalVp = tvp;
169                 afsp->s_root = d_alloc_root(ip);
170                 afsp->s_root->d_op = &afs_dentry_operations;
171             } else
172                 code = ENOENT;
173         }
174         crfree(credp);
175     }
176
177     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
178                ICL_TYPE_INT32, code);
179     return code;
180 }
181
182 /* super_operations */
183
184 /* afs_notify_change
185  * Linux version of setattr call. What to change is in the iattr struct.
186  * We need to set bits in both the Linux inode as well as the vcache.
187  */
188 int
189 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
190 {
191     struct vattr vattr;
192     cred_t *credp = crref();
193     struct inode *ip = dp->d_inode;
194     int code;
195
196     VATTR_NULL(&vattr);
197     iattr2vattr(&vattr, iattrp);        /* Convert for AFS vnodeops call. */
198
199     lock_kernel();
200     AFS_GLOCK();
201     code = afs_setattr(VTOAFS(ip), &vattr, credp);
202     if (!code) {
203         afs_getattr(VTOAFS(ip), &vattr, credp);
204         vattr2inode(ip, &vattr);
205     }
206     AFS_GUNLOCK();
207     unlock_kernel();
208     crfree(credp);
209     return -code;
210 }
211
212
213 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
214 #if defined(HAVE_KMEM_CACHE_T)
215 static kmem_cache_t *afs_inode_cachep;
216 #else
217 struct kmem_cache *afs_inode_cachep;
218 #endif
219
220 static struct inode *
221 afs_alloc_inode(struct super_block *sb)
222 {
223     struct vcache *vcp;
224
225 #if defined(SLAB_KERNEL)
226     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, SLAB_KERNEL);
227 #else
228     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
229 #endif
230     if (!vcp)
231         return NULL;
232
233     return AFSTOV(vcp);
234 }
235
236 static void
237 afs_destroy_inode(struct inode *inode)
238 {
239     kmem_cache_free(afs_inode_cachep, inode);
240 }
241
242 static void
243 #if defined(HAVE_KMEM_CACHE_T)
244 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
245 #elif defined(KMEM_CACHE_INIT)
246 init_once(struct kmem_cache * cachep, void * foo)
247 #else
248 init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
249 #endif
250 {
251     struct vcache *vcp = (struct vcache *) foo;
252
253 #if defined(SLAB_CTOR_VERIFY)
254     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
255         SLAB_CTOR_CONSTRUCTOR)
256 #endif
257         inode_init_once(AFSTOV(vcp));
258 }
259
260 int
261 afs_init_inodecache(void)
262 {
263 #ifndef SLAB_RECLAIM_ACCOUNT
264 #define SLAB_RECLAIM_ACCOUNT 0
265 #endif
266
267 #if defined(KMEM_CACHE_TAKES_DTOR)
268     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
269                                          sizeof(struct vcache),
270                                          0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
271                                          init_once, NULL);
272 #else
273     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
274                                          sizeof(struct vcache),
275                                          0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
276                                          init_once);
277 #endif
278     if (afs_inode_cachep == NULL)
279         return -ENOMEM;
280     return 0;
281 }
282
283 void
284 afs_destroy_inodecache(void)
285 {
286     if (afs_inode_cachep)
287         (void) kmem_cache_destroy(afs_inode_cachep);
288 }
289 #else
290 int
291 afs_init_inodecache(void)
292 {
293     return 0;
294 }
295
296 void
297 afs_destroy_inodecache(void)
298 {
299     return;
300 }
301 #endif
302
303 static void
304 afs_clear_inode(struct inode *ip)
305 {
306     struct vcache *vcp = VTOAFS(ip);
307
308     if (vcp->vlruq.prev || vcp->vlruq.next)
309         osi_Panic("inode freed while on LRU");
310     if (vcp->hnext)
311         osi_Panic("inode freed while still hashed");
312
313 #if !defined(STRUCT_SUPER_HAS_ALLOC_INODE)
314     afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
315 #endif
316 }
317
318 /* afs_put_super
319  * Called from unmount to release super_block. */
320 static void
321 afs_put_super(struct super_block *sbp)
322 {
323     AFS_GLOCK();
324     AFS_STATCNT(afs_unmount);
325
326     afs_globalVFS = 0;
327     afs_globalVp = 0;
328
329     osi_linux_free_inode_pages();       /* invalidate and release remaining AFS inodes. */
330     afs_shutdown();
331     mntput(afs_cacheMnt);
332
333     osi_linux_verify_alloced_memory();
334 #if defined(HAVE_BDI_INIT)
335     bdi_destroy(&afs_backing_dev_info);
336 #endif
337     AFS_GUNLOCK();
338
339     sbp->s_dev = 0;
340     module_put(THIS_MODULE);
341 }
342
343
344 /* afs_statfs
345  * statp is in user space, so we need to cobble together a statfs, then
346  * copy it.
347  */
348 int
349 #if defined(STATFS_TAKES_DENTRY)
350 afs_statfs(struct dentry *dentry, struct kstatfs *statp)
351 #else
352 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
353 #endif
354 {
355     memset(statp, 0, sizeof(*statp));
356
357     AFS_STATCNT(afs_statfs);
358
359     /* hardcode in case that which is giveth is taken away */
360     statp->f_type = 0x5346414F;
361 #if defined(STATFS_TAKES_DENTRY)
362     statp->f_bsize = dentry->d_sb->s_blocksize;
363 #else
364     statp->f_bsize = sbp->s_blocksize;
365 #endif
366     statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
367         statp->f_ffree = 9000000;
368     statp->f_fsid.val[0] = AFS_VFSMAGIC;
369     statp->f_fsid.val[1] = AFS_VFSFSID;
370     statp->f_namelen = 256;
371
372     return 0;
373 }
374
375 struct super_operations afs_sops = {
376 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
377   .alloc_inode =        afs_alloc_inode,
378   .destroy_inode =      afs_destroy_inode,
379 #endif
380   .clear_inode =        afs_clear_inode,
381   .put_super =          afs_put_super,
382   .statfs =             afs_statfs,
383 };
384
385 /************** Support routines ************************/
386
387 /* vattr_setattr
388  * Set iattr data into vattr. Assume vattr cleared before call.
389  */
390 static void
391 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
392 {
393     vattrp->va_mask = iattrp->ia_valid;
394     if (iattrp->ia_valid & ATTR_MODE)
395         vattrp->va_mode = iattrp->ia_mode;
396     if (iattrp->ia_valid & ATTR_UID)
397         vattrp->va_uid = iattrp->ia_uid;
398     if (iattrp->ia_valid & ATTR_GID)
399         vattrp->va_gid = iattrp->ia_gid;
400     if (iattrp->ia_valid & ATTR_SIZE)
401         vattrp->va_size = iattrp->ia_size;
402     if (iattrp->ia_valid & ATTR_ATIME) {
403         vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
404         vattrp->va_atime.tv_usec = 0;
405     }
406     if (iattrp->ia_valid & ATTR_MTIME) {
407         vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
408         vattrp->va_mtime.tv_usec = 0;
409     }
410     if (iattrp->ia_valid & ATTR_CTIME) {
411         vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
412         vattrp->va_ctime.tv_usec = 0;
413     }
414 }
415
416 /* vattr2inode
417  * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
418  */
419 void
420 vattr2inode(struct inode *ip, struct vattr *vp)
421 {
422     ip->i_ino = vp->va_nodeid;
423     ip->i_nlink = vp->va_nlink;
424     ip->i_blocks = vp->va_blocks;
425 #ifdef STRUCT_INODE_HAS_I_BLKBITS
426     ip->i_blkbits = AFS_BLKBITS;
427 #endif
428 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
429     ip->i_blksize = vp->va_blocksize;
430 #endif
431     ip->i_rdev = vp->va_rdev;
432     ip->i_mode = vp->va_mode;
433     ip->i_uid = vp->va_uid;
434     ip->i_gid = vp->va_gid;
435     i_size_write(ip, vp->va_size);
436     ip->i_atime.tv_sec = vp->va_atime.tv_sec;
437     ip->i_atime.tv_nsec = 0;
438     ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
439     /* Set the mtime nanoseconds to the sysname generation number.
440      * This convinces NFS clients that all directories have changed
441      * any time the sysname list changes.
442      */
443     ip->i_mtime.tv_nsec = afs_sysnamegen;
444     ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
445     ip->i_ctime.tv_nsec = 0;
446 }
447
448 /* osi_linux_free_inode_pages
449  *
450  * Free all vnodes remaining in the afs hash.  Must be done before
451  * shutting down afs and freeing all memory.
452  */
453 void
454 osi_linux_free_inode_pages(void)
455 {
456     int i;
457     struct vcache *tvc, *nvc;
458     extern struct vcache *afs_vhashT[VCSIZE];
459
460     for (i = 0; i < VCSIZE; i++) {
461         for (tvc = afs_vhashT[i]; tvc; ) {
462             int slept;
463         
464             nvc = tvc->hnext;
465             if (afs_FlushVCache(tvc, &slept))           /* slept always 0 for linux? */
466                 printf("Failed to invalidate all pages on inode 0x%p\n", tvc);
467             tvc = nvc;
468         }
469     }
470 }