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