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