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