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