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