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