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