cb3b5691629b85eeaffad6d6a8f4fbf812a231c3
[openafs.git] / src / afs / LINUX / osi_vm.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 #include <afsconfig.h>
11 #include "../afs/param.h"
12
13 RCSID("$Header$");
14
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h"   /* statistics */
18
19 /* Linux VM operations
20  *
21  * The general model for Linux is to treat vm as a cache that's:
22  * 1) explicitly updated by AFS when AFS writes the data to the cache file.
23  * 2) reads go through the cache. A cache miss is satisfied by the filesystem.
24  *
25  * This means flushing VM is not required on this OS.
26  */
27
28 /* Try to discard pages, in order to recycle a vcache entry.
29  *
30  * We also make some sanity checks:  ref count, open count, held locks.
31  *
32  * We also do some non-VM-related chores, such as releasing the cred pointer
33  * (for AIX and Solaris) and releasing the gnode (for AIX).
34  *
35  * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
36  *   *slept should be set to warn the caller.
37  *
38  * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
39  * is not dropped and re-acquired for any platform.  It may be that *slept is
40  * therefore obsolescent.
41  */
42 int osi_VM_FlushVCache(struct vcache *avc, int *slept)
43 {
44     struct inode *ip = (struct inode*)avc;
45
46     if (VREFCOUNT(avc) != 0)
47         return EBUSY;
48
49     if (avc->opens != 0)
50         return EBUSY;
51
52 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
53     truncate_inode_pages(&ip->i_data, 0);
54 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,15)
55     truncate_inode_pages(ip, 0);
56 #else
57     invalidate_inode_pages(ip);
58 #endif
59     return 0;
60 }
61
62 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
63  * try to free pages, when deleting a file.
64  *
65  * Locking:  the vcache entry's lock is held.  It may be dropped and 
66  * re-obtained.
67  *
68  * Since we drop and re-obtain the lock, we can't guarantee that there won't
69  * be some pages around when we return, newly created by concurrent activity.
70  */
71 void osi_VM_TryToSmush(struct vcache *avc, struct AFS_UCRED *acred, int sync)
72 {
73     invalidate_inode_pages((struct inode *)avc);
74 }
75
76 /* Flush and invalidate pages, for fsync() with INVAL flag
77  *
78  * Locking:  only the global lock is held.
79  */
80 void osi_VM_FSyncInval(struct vcache *avc)
81 {
82
83 }
84
85 /* Try to store pages to cache, in order to store a file back to the server.
86  *
87  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
88  * re-obtained.
89  */
90 void osi_VM_StoreAllSegments(struct vcache *avc)
91 {
92     struct inode *ip = (struct inode *) avc;
93
94 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,5)
95     /* filemap_fdatasync() only exported in 2.4.5 and above */
96     ReleaseWriteLock(&avc->lock);
97     AFS_GUNLOCK();
98     filemap_fdatasync(ip->i_mapping);
99     filemap_fdatawait(ip->i_mapping);
100     AFS_GLOCK();
101     ObtainWriteLock(&avc->lock, 121);
102 #endif
103 }
104
105 /* Purge VM for a file when its callback is revoked.
106  *
107  * Locking:  No lock is held, not even the global lock.
108  */
109 void osi_VM_FlushPages(struct vcache *avc, struct AFS_UCRED *credp)
110 {
111 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
112     struct inode *ip = (struct inode*)avc;
113
114     truncate_inode_pages(&ip->i_data, 0);
115 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,15)
116     struct inode *ip = (struct inode*)avc;
117
118     truncate_inode_pages(ip, 0);
119 #else
120     invalidate_inode_pages((struct inode*)avc);
121 #endif
122 }
123
124 /* Purge pages beyond end-of-file, when truncating a file.
125  *
126  * Locking:  no lock is held, not even the global lock.
127  * activeV is raised.  This is supposed to block pageins, but at present
128  * it only works on Solaris.
129  */
130 void osi_VM_Truncate(struct vcache *avc, int alen, struct AFS_UCRED *acred)
131 {
132 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
133     struct inode *ip = (struct inode*)avc;
134
135     truncate_inode_pages(&ip->i_data, alen);
136 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,15)
137     struct inode *ip = (struct inode*)avc;
138
139     truncate_inode_pages(ip, alen);
140 #else
141     invalidate_inode_pages((struct inode*)avc);
142 #endif
143 }