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