linux-kernel-updates-20060804
[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 RCSID
19     ("$Header$");
20
21 #define __NO_VERSION__          /* don't define kernel_version in module.h */
22 #include <linux/module.h> /* early to avoid printf->printk mapping */
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
25 #include "afs/afs_stats.h"
26 #if !defined(AFS_LINUX26_ENV)
27 #include "h/locks.h"
28 #endif
29 #if defined(AFS_LINUX24_ENV)
30 #include "h/smp_lock.h"
31 #endif
32
33
34 struct vcache *afs_globalVp = 0;
35 struct vfs *afs_globalVFS = 0;
36 #if defined(AFS_LINUX24_ENV)
37 struct vfsmount *afs_cacheMnt;
38 #endif
39 int afs_was_mounted = 0;        /* Used to force reload if mount/unmount/mount */
40
41 extern struct super_operations afs_sops;
42 #if defined(AFS_LINUX26_ENV)
43 extern struct export_operations afs_export_ops;
44 #endif
45 extern afs_rwlock_t afs_xvcache;
46 extern struct afs_q VLRU;
47
48 extern struct dentry_operations afs_dentry_operations;
49
50 /* Forward declarations */
51 static void iattr2vattr(struct vattr *vattrp, struct iattr *iattrp);
52 static int afs_root(struct super_block *afsp);
53 struct super_block *afs_read_super(struct super_block *sb, void *data, int silent);
54 int afs_fill_super(struct super_block *sb, void *data, int silent);
55
56 /* afs_file_system
57  * VFS entry for Linux - installed in init_module
58  * Linux mounts file systems by:
59  * 1) register_filesystem(&afs_file_system) - done in init_module
60  * 2) Mount call comes to us via do_mount -> read_super -> afs_read_super.
61  *    We are expected to setup the super_block. See afs_read_super.
62  */
63 #if defined(AFS_LINUX26_ENV)
64 struct backing_dev_info afs_backing_dev_info = {
65         .ra_pages       = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE,
66         .state          = 0,
67 };
68
69
70 /* afs_read_super
71  * read the "super block" for AFS - roughly eguivalent to struct vfs.
72  * dev, covered, s_rd_only, s_dirt, and s_type will be set by read_super.
73  */
74 #if defined(AFS_LINUX26_ENV)
75 static struct super_block *
76 #ifdef GET_SB_HAS_STRUCT_VFSMOUNT
77 afs_get_sb(struct file_system_type *fs_type, int flags,
78            const char *dev_name, void *data, struct vfsmount *mnt)
79 #else
80 afs_get_sb(struct file_system_type *fs_type, int flags,
81            const char *dev_name, void *data)
82 #endif
83 {
84 #ifdef GET_SB_HAS_STRUCT_VFSMOUNT
85     return get_sb_nodev(fs_type, flags, data, afs_fill_super, mnt);
86 #else
87     return get_sb_nodev(fs_type, flags, data, afs_fill_super);
88 #endif
89 }
90
91
92 struct file_system_type afs_fs_type = {
93     .owner = THIS_MODULE,
94     .name = "afs",
95     .get_sb = afs_get_sb,
96     .kill_sb = kill_anon_super,
97     .fs_flags = FS_BINARY_MOUNTDATA,
98 };
99 #elif defined(AFS_LINUX24_ENV)
100 DECLARE_FSTYPE(afs_fs_type, "afs", afs_read_super, 0);
101 #else
102 struct file_system_type afs_fs_type = {
103     "afs",                      /* name - used by mount operation. */
104     0,                          /* requires_dev - no for network filesystems. mount() will 
105                                  * pass us an "unnamed" device. */
106     afs_read_super,             /* wrapper to afs_mount */
107     NULL                        /* pointer to next file_system_type once registered. */
108 };
109 #endif
110
111
112 int
113 afs_fill_super(struct super_block *sb, void *data, int silent)
114 #else
115 struct super_block *
116 afs_read_super(struct super_block *sb, void *data, int silent)
117 #endif
118 {
119     int code = 0;
120
121     AFS_GLOCK();
122     if (afs_was_mounted) {
123         printf
124             ("You must reload the AFS kernel extensions before remounting AFS.\n");
125         AFS_GUNLOCK();
126 #if defined(AFS_LINUX26_ENV)
127         return -EINVAL;
128 #else
129         return NULL;
130 #endif
131     }
132     afs_was_mounted = 1;
133
134     /* Set basics of super_block */
135 #if !defined(AFS_LINUX24_ENV)
136     lock_super(sb);
137 #endif
138 #if defined(AFS_LINUX26_ENV)
139    __module_get(THIS_MODULE);
140 #else
141     MOD_INC_USE_COUNT;
142 #endif
143
144     afs_globalVFS = sb;
145     sb->s_blocksize = 1024;
146     sb->s_blocksize_bits = 10;
147     sb->s_magic = AFS_VFSMAGIC;
148     sb->s_op = &afs_sops;       /* Super block (vfs) ops */
149 #if defined(AFS_LINUX26_ENV)
150     sb->s_export_op = &afs_export_ops;
151 #endif
152 #if defined(MAX_NON_LFS)
153 #ifdef AFS_64BIT_CLIENT
154 #if !defined(MAX_LFS_FILESIZE)
155 #if BITS_PER_LONG==32
156 #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1) 
157 #elif BITS_PER_LONG==64
158 #define MAX_LFS_FILESIZE 0x7fffffffffffffff
159 #endif
160 #endif
161     sb->s_maxbytes = MAX_LFS_FILESIZE;
162 #else
163     sb->s_maxbytes = MAX_NON_LFS;
164 #endif
165 #endif
166     code = afs_root(sb);
167     if (code) {
168         afs_globalVFS = NULL;
169 #if defined(AFS_LINUX26_ENV)
170         module_put(THIS_MODULE);
171 #else
172         MOD_DEC_USE_COUNT;
173 #endif
174     }
175
176 #if !defined(AFS_LINUX24_ENV)
177     unlock_super(sb);
178 #endif
179
180     AFS_GUNLOCK();
181 #if defined(AFS_LINUX26_ENV)
182     return code ? -EINVAL : 0;
183 #else
184     return code ? NULL : sb;
185 #endif
186 }
187
188
189 /* afs_root - stat the root of the file system. AFS global held on entry. */
190 static int
191 afs_root(struct super_block *afsp)
192 {
193     register afs_int32 code = 0;
194     struct vrequest treq;
195     register struct vcache *tvp = 0;
196
197     AFS_STATCNT(afs_root);
198     if (afs_globalVp && (afs_globalVp->states & CStatd)) {
199         tvp = afs_globalVp;
200     } else {
201         cred_t *credp = crref();
202
203         if (afs_globalVp) {
204             afs_PutVCache(afs_globalVp);
205             afs_globalVp = NULL;
206         }
207
208         if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
209             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
210             if (tvp) {
211                 struct inode *ip = AFSTOV(tvp);
212                 struct vattr vattr;
213
214                 afs_getattr(tvp, &vattr, credp);
215                 afs_fill_inode(ip, &vattr);
216
217                 /* setup super_block and mount point inode. */
218                 afs_globalVp = tvp;
219 #if defined(AFS_LINUX24_ENV)
220                 afsp->s_root = d_alloc_root(ip);
221 #else
222                 afsp->s_root = d_alloc_root(ip, NULL);
223 #endif
224                 afsp->s_root->d_op = &afs_dentry_operations;
225             } else
226                 code = ENOENT;
227         }
228         crfree(credp);
229     }
230
231     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
232                ICL_TYPE_INT32, code);
233     return code;
234 }
235
236 /* super_operations */
237
238 /* afs_notify_change
239  * Linux version of setattr call. What to change is in the iattr struct.
240  * We need to set bits in both the Linux inode as well as the vcache.
241  */
242 int
243 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
244 {
245     struct vattr vattr;
246     cred_t *credp = crref();
247     struct inode *ip = dp->d_inode;
248     int code;
249
250     VATTR_NULL(&vattr);
251     iattr2vattr(&vattr, iattrp);        /* Convert for AFS vnodeops call. */
252
253 #if defined(AFS_LINUX26_ENV)
254     lock_kernel();
255 #endif
256     AFS_GLOCK();
257     code = afs_setattr(VTOAFS(ip), &vattr, credp);
258     if (!code) {
259         afs_getattr(VTOAFS(ip), &vattr, credp);
260         vattr2inode(ip, &vattr);
261     }
262     AFS_GUNLOCK();
263 #if defined(AFS_LINUX26_ENV)
264     unlock_kernel();
265 #endif
266     crfree(credp);
267     return -code;
268 }
269
270
271 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
272 static kmem_cache_t *afs_inode_cachep;
273
274 static struct inode *
275 afs_alloc_inode(struct super_block *sb)
276 {
277     struct vcache *vcp;
278
279     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, SLAB_KERNEL);
280     if (!vcp)
281         return NULL;
282
283     return AFSTOV(vcp);
284 }
285
286 static void
287 afs_destroy_inode(struct inode *inode)
288 {
289     kmem_cache_free(afs_inode_cachep, inode);
290 }
291
292 static void
293 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
294 {
295     struct vcache *vcp = (struct vcache *) foo;
296
297     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
298         SLAB_CTOR_CONSTRUCTOR)
299         inode_init_once(AFSTOV(vcp));
300 }
301
302 int
303 afs_init_inodecache(void)
304 {
305 #ifndef SLAB_RECLAIM_ACCOUNT
306 #define SLAB_RECLAIM_ACCOUNT 0
307 #endif
308
309     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
310                                          sizeof(struct vcache),
311                                          0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
312                                          init_once, NULL);
313     if (afs_inode_cachep == NULL)
314         return -ENOMEM;
315     return 0;
316 }
317
318 void
319 afs_destroy_inodecache(void)
320 {
321     if (kmem_cache_destroy(afs_inode_cachep))
322         printk(KERN_INFO "afs_inode_cache: not all structures were freed\n");
323 }
324 #else
325 int
326 afs_init_inodecache(void)
327 {
328     return 0;
329 }
330
331 void
332 afs_destroy_inodecache(void)
333 {
334     return;
335 }
336 #endif
337
338 static void
339 afs_clear_inode(struct inode *ip)
340 {
341     struct vcache *vcp = VTOAFS(ip);
342
343     if (vcp->vlruq.prev || vcp->vlruq.next)
344         osi_Panic("inode freed while on LRU");
345     if (vcp->hnext)
346         osi_Panic("inode freed while still hashed");
347
348 #if !defined(STRUCT_SUPER_HAS_ALLOC_INODE)
349     afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
350 #endif
351 }
352
353 /* afs_put_inode
354  * Linux version of inactive.  When refcount == 2, we are about to
355  * decrement to 1 and the only reference remaining should be for
356  * the VLRU
357  */
358
359 static void
360 afs_put_inode(struct inode *ip)
361 {
362     struct vcache *vcp = VTOAFS(ip);
363
364     AFS_GLOCK();
365     if (VREFCOUNT(vcp) == 2) 
366         afs_InactiveVCache(vcp, NULL);
367     AFS_GUNLOCK();
368 }
369
370 /* afs_put_super
371  * Called from unmount to release super_block. */
372 static void
373 afs_put_super(struct super_block *sbp)
374 {
375     AFS_GLOCK();
376     AFS_STATCNT(afs_unmount);
377
378 #if !defined(AFS_LINUX26_ENV)
379     if (!suser()) {
380         AFS_GUNLOCK();
381         return;
382     }
383 #endif
384
385     afs_globalVFS = 0;
386     afs_globalVp = 0;
387
388     osi_linux_free_inode_pages();       /* invalidate and release remaining AFS inodes. */
389     afs_shutdown();
390 #if defined(AFS_LINUX24_ENV)
391     mntput(afs_cacheMnt);
392 #endif
393
394     osi_linux_verify_alloced_memory();
395     AFS_GUNLOCK();
396
397     sbp->s_dev = 0;
398 #if defined(AFS_LINUX26_ENV)
399     module_put(THIS_MODULE);
400 #else
401     MOD_DEC_USE_COUNT;
402 #endif
403 }
404
405
406 /* afs_statfs
407  * statp is in user space, so we need to cobble together a statfs, then
408  * copy it.
409  */
410 #if defined(AFS_LINUX26_ENV)
411 int
412 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
413 #elif defined(AFS_LINUX24_ENV)
414 int
415 afs_statfs(struct super_block *sbp, struct statfs *statp)
416 #else
417 int
418 afs_statfs(struct super_block *sbp, struct statfs *__statp, int size)
419 #endif
420 {
421 #if !defined(AFS_LINUX24_ENV)
422     struct statfs stat, *statp;
423
424     if (size < sizeof(struct statfs))
425         return;
426
427     memset(&stat, 0, size);
428     statp = &stat;
429 #else
430     memset(statp, 0, sizeof(*statp));
431 #endif
432
433     AFS_STATCNT(afs_statfs);
434
435     statp->f_type = 0;          /* Can we get a real type sometime? */
436     statp->f_bsize = sbp->s_blocksize;
437     statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
438         statp->f_ffree = 9000000;
439     statp->f_fsid.val[0] = AFS_VFSMAGIC;
440     statp->f_fsid.val[1] = AFS_VFSFSID;
441     statp->f_namelen = 256;
442
443 #if !defined(AFS_LINUX24_ENV)
444     memcpy_tofs(__statp, &stat, size);
445 #endif
446     return 0;
447 }
448
449 struct super_operations afs_sops = {
450 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
451   .alloc_inode =        afs_alloc_inode,
452   .destroy_inode =      afs_destroy_inode,
453 #endif
454   .clear_inode =        afs_clear_inode,
455   .put_inode =          afs_put_inode,
456   .put_super =          afs_put_super,
457   .statfs =             afs_statfs,
458 #if !defined(AFS_LINUX24_ENV)
459   .notify_change =      afs_notify_change,
460 #endif
461 };
462
463 /************** Support routines ************************/
464
465 /* vattr_setattr
466  * Set iattr data into vattr. Assume vattr cleared before call.
467  */
468 static void
469 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
470 {
471     vattrp->va_mask = iattrp->ia_valid;
472     if (iattrp->ia_valid & ATTR_MODE)
473         vattrp->va_mode = iattrp->ia_mode;
474     if (iattrp->ia_valid & ATTR_UID)
475         vattrp->va_uid = iattrp->ia_uid;
476     if (iattrp->ia_valid & ATTR_GID)
477         vattrp->va_gid = iattrp->ia_gid;
478     if (iattrp->ia_valid & ATTR_SIZE)
479         vattrp->va_size = iattrp->ia_size;
480     if (iattrp->ia_valid & ATTR_ATIME) {
481 #if defined(AFS_LINUX26_ENV)
482         vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
483 #else
484         vattrp->va_atime.tv_sec = iattrp->ia_atime;
485 #endif
486         vattrp->va_atime.tv_usec = 0;
487     }
488     if (iattrp->ia_valid & ATTR_MTIME) {
489 #if defined(AFS_LINUX26_ENV)
490         vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
491 #else
492         vattrp->va_mtime.tv_sec = iattrp->ia_mtime;
493 #endif
494         vattrp->va_mtime.tv_usec = 0;
495     }
496     if (iattrp->ia_valid & ATTR_CTIME) {
497 #if defined(AFS_LINUX26_ENV)
498         vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
499 #else
500         vattrp->va_ctime.tv_sec = iattrp->ia_ctime;
501 #endif
502         vattrp->va_ctime.tv_usec = 0;
503     }
504 }
505
506 /* vattr2inode
507  * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
508  */
509 void
510 vattr2inode(struct inode *ip, struct vattr *vp)
511 {
512     ip->i_ino = vp->va_nodeid;
513     ip->i_nlink = vp->va_nlink;
514     ip->i_blocks = vp->va_blocks;
515     ip->i_blksize = vp->va_blocksize;
516     ip->i_rdev = vp->va_rdev;
517     ip->i_mode = vp->va_mode;
518     ip->i_uid = vp->va_uid;
519     ip->i_gid = vp->va_gid;
520     ip->i_size = vp->va_size;
521 #if defined(AFS_LINUX26_ENV)
522     ip->i_atime.tv_sec = vp->va_atime.tv_sec;
523     ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
524     /* Set the mtime nanoseconds to the sysname generation number.
525      * This convinces NFS clients that all directories have changed
526      * any time the sysname list changes.
527      */
528     ip->i_mtime.tv_nsec = afs_sysnamegen;
529     ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
530 #else
531     ip->i_atime = vp->va_atime.tv_sec;
532     ip->i_mtime = vp->va_mtime.tv_sec;
533     ip->i_ctime = vp->va_ctime.tv_sec;
534 #endif
535 }
536
537 /* osi_linux_free_inode_pages
538  *
539  * Free all vnodes remaining in the afs hash.  Must be done before
540  * shutting down afs and freeing all memory.
541  */
542 void
543 osi_linux_free_inode_pages(void)
544 {
545     int i;
546     struct vcache *tvc, *nvc;
547     extern struct vcache *afs_vhashT[VCSIZE];
548
549     for (i = 0; i < VCSIZE; i++) {
550         for (tvc = afs_vhashT[i]; tvc; ) {
551             int slept;
552         
553             nvc = tvc->hnext;
554             if (afs_FlushVCache(tvc, &slept))           /* slept always 0 for linux? */
555                 printf("Failed to invalidate all pages on inode 0x%p\n", tvc);
556             tvc = nvc;
557         }
558     }
559 }