[PATCH] hrtimers: prevent possible itimer DoS
[linux-2.6.22.y-op.git] / fs / afs / file.c
blobb17634541f677f573971f426aa59bb40c862ac4a
1 /* file.c: AFS filesystem file handling
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/fs.h>
17 #include <linux/pagemap.h>
18 #include "volume.h"
19 #include "vnode.h"
20 #include <rxrpc/call.h>
21 #include "internal.h"
23 #if 0
24 static int afs_file_open(struct inode *inode, struct file *file);
25 static int afs_file_release(struct inode *inode, struct file *file);
26 #endif
28 static int afs_file_readpage(struct file *file, struct page *page);
29 static void afs_file_invalidatepage(struct page *page, unsigned long offset);
30 static int afs_file_releasepage(struct page *page, gfp_t gfp_flags);
32 const struct inode_operations afs_file_inode_operations = {
33 .getattr = afs_inode_getattr,
36 const struct address_space_operations afs_fs_aops = {
37 .readpage = afs_file_readpage,
38 .set_page_dirty = __set_page_dirty_nobuffers,
39 .releasepage = afs_file_releasepage,
40 .invalidatepage = afs_file_invalidatepage,
43 /*****************************************************************************/
45 * deal with notification that a page was read from the cache
47 #ifdef AFS_CACHING_SUPPORT
48 static void afs_file_readpage_read_complete(void *cookie_data,
49 struct page *page,
50 void *data,
51 int error)
53 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
55 if (error)
56 SetPageError(page);
57 else
58 SetPageUptodate(page);
59 unlock_page(page);
61 } /* end afs_file_readpage_read_complete() */
62 #endif
64 /*****************************************************************************/
66 * deal with notification that a page was written to the cache
68 #ifdef AFS_CACHING_SUPPORT
69 static void afs_file_readpage_write_complete(void *cookie_data,
70 struct page *page,
71 void *data,
72 int error)
74 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
76 unlock_page(page);
78 } /* end afs_file_readpage_write_complete() */
79 #endif
81 /*****************************************************************************/
83 * AFS read page from file (or symlink)
85 static int afs_file_readpage(struct file *file, struct page *page)
87 struct afs_rxfs_fetch_descriptor desc;
88 #ifdef AFS_CACHING_SUPPORT
89 struct cachefs_page *pageio;
90 #endif
91 struct afs_vnode *vnode;
92 struct inode *inode;
93 int ret;
95 inode = page->mapping->host;
97 _enter("{%lu},{%lu}", inode->i_ino, page->index);
99 vnode = AFS_FS_I(inode);
101 BUG_ON(!PageLocked(page));
103 ret = -ESTALE;
104 if (vnode->flags & AFS_VNODE_DELETED)
105 goto error;
107 #ifdef AFS_CACHING_SUPPORT
108 ret = cachefs_page_get_private(page, &pageio, GFP_NOIO);
109 if (ret < 0)
110 goto error;
112 /* is it cached? */
113 ret = cachefs_read_or_alloc_page(vnode->cache,
114 page,
115 afs_file_readpage_read_complete,
116 NULL,
117 GFP_KERNEL);
118 #else
119 ret = -ENOBUFS;
120 #endif
122 switch (ret) {
123 /* read BIO submitted and wb-journal entry found */
124 case 1:
125 BUG(); // TODO - handle wb-journal match
127 /* read BIO submitted (page in cache) */
128 case 0:
129 break;
131 /* no page available in cache */
132 case -ENOBUFS:
133 case -ENODATA:
134 default:
135 desc.fid = vnode->fid;
136 desc.offset = page->index << PAGE_CACHE_SHIFT;
137 desc.size = min((size_t) (inode->i_size - desc.offset),
138 (size_t) PAGE_SIZE);
139 desc.buffer = kmap(page);
141 clear_page(desc.buffer);
143 /* read the contents of the file from the server into the
144 * page */
145 ret = afs_vnode_fetch_data(vnode, &desc);
146 kunmap(page);
147 if (ret < 0) {
148 if (ret==-ENOENT) {
149 _debug("got NOENT from server"
150 " - marking file deleted and stale");
151 vnode->flags |= AFS_VNODE_DELETED;
152 ret = -ESTALE;
155 #ifdef AFS_CACHING_SUPPORT
156 cachefs_uncache_page(vnode->cache, page);
157 #endif
158 goto error;
161 SetPageUptodate(page);
163 #ifdef AFS_CACHING_SUPPORT
164 if (cachefs_write_page(vnode->cache,
165 page,
166 afs_file_readpage_write_complete,
167 NULL,
168 GFP_KERNEL) != 0
170 cachefs_uncache_page(vnode->cache, page);
171 unlock_page(page);
173 #else
174 unlock_page(page);
175 #endif
178 _leave(" = 0");
179 return 0;
181 error:
182 SetPageError(page);
183 unlock_page(page);
185 _leave(" = %d", ret);
186 return ret;
188 } /* end afs_file_readpage() */
190 /*****************************************************************************/
192 * get a page cookie for the specified page
194 #ifdef AFS_CACHING_SUPPORT
195 int afs_cache_get_page_cookie(struct page *page,
196 struct cachefs_page **_page_cookie)
198 int ret;
200 _enter("");
201 ret = cachefs_page_get_private(page,_page_cookie, GFP_NOIO);
203 _leave(" = %d", ret);
204 return ret;
205 } /* end afs_cache_get_page_cookie() */
206 #endif
208 /*****************************************************************************/
210 * invalidate part or all of a page
212 static void afs_file_invalidatepage(struct page *page, unsigned long offset)
214 int ret = 1;
216 _enter("{%lu},%lu", page->index, offset);
218 BUG_ON(!PageLocked(page));
220 if (PagePrivate(page)) {
221 #ifdef AFS_CACHING_SUPPORT
222 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
223 cachefs_uncache_page(vnode->cache,page);
224 #endif
226 /* We release buffers only if the entire page is being
227 * invalidated.
228 * The get_block cached value has been unconditionally
229 * invalidated, so real IO is not possible anymore.
231 if (offset == 0) {
232 BUG_ON(!PageLocked(page));
234 ret = 0;
235 if (!PageWriteback(page))
236 ret = page->mapping->a_ops->releasepage(page,
238 /* possibly should BUG_ON(!ret); - neilb */
242 _leave(" = %d", ret);
243 } /* end afs_file_invalidatepage() */
245 /*****************************************************************************/
247 * release a page and cleanup its private data
249 static int afs_file_releasepage(struct page *page, gfp_t gfp_flags)
251 struct cachefs_page *pageio;
253 _enter("{%lu},%x", page->index, gfp_flags);
255 if (PagePrivate(page)) {
256 #ifdef AFS_CACHING_SUPPORT
257 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
258 cachefs_uncache_page(vnode->cache, page);
259 #endif
261 pageio = (struct cachefs_page *) page_private(page);
262 set_page_private(page, 0);
263 ClearPagePrivate(page);
265 kfree(pageio);
268 _leave(" = 0");
269 return 0;
270 } /* end afs_file_releasepage() */