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