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