blob: b17634541f677f573971f426aa59bb40c862ac4a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* file.c: AFS filesystem file handling
2 *
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/fs.h>
17#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "volume.h"
19#include "vnode.h"
20#include <rxrpc/call.h>
21#include "internal.h"
22
23#if 0
24static int afs_file_open(struct inode *inode, struct file *file);
25static int afs_file_release(struct inode *inode, struct file *file);
26#endif
27
28static int afs_file_readpage(struct file *file, struct page *page);
NeilBrown2ff28e22006-03-26 01:37:18 -080029static void afs_file_invalidatepage(struct page *page, unsigned long offset);
Al Viro27496a82005-10-21 03:20:48 -040030static int afs_file_releasepage(struct page *page, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Arjan van de Ven754661f2007-02-12 00:55:38 -080032const struct inode_operations afs_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 .getattr = afs_inode_getattr,
34};
35
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070036const struct address_space_operations afs_fs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 .readpage = afs_file_readpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 .set_page_dirty = __set_page_dirty_nobuffers,
39 .releasepage = afs_file_releasepage,
40 .invalidatepage = afs_file_invalidatepage,
41};
42
43/*****************************************************************************/
44/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * deal with notification that a page was read from the cache
46 */
47#ifdef AFS_CACHING_SUPPORT
48static void afs_file_readpage_read_complete(void *cookie_data,
49 struct page *page,
50 void *data,
51 int error)
52{
53 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
54
55 if (error)
56 SetPageError(page);
57 else
58 SetPageUptodate(page);
59 unlock_page(page);
60
61} /* end afs_file_readpage_read_complete() */
62#endif
63
64/*****************************************************************************/
65/*
66 * deal with notification that a page was written to the cache
67 */
68#ifdef AFS_CACHING_SUPPORT
69static void afs_file_readpage_write_complete(void *cookie_data,
70 struct page *page,
71 void *data,
72 int error)
73{
74 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
75
76 unlock_page(page);
77
78} /* end afs_file_readpage_write_complete() */
79#endif
80
81/*****************************************************************************/
82/*
83 * AFS read page from file (or symlink)
84 */
85static int afs_file_readpage(struct file *file, struct page *page)
86{
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;
94
95 inode = page->mapping->host;
96
97 _enter("{%lu},{%lu}", inode->i_ino, page->index);
98
99 vnode = AFS_FS_I(inode);
100
Matt Mackallcd7619d2005-05-01 08:59:01 -0700101 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 ret = -ESTALE;
104 if (vnode->flags & AFS_VNODE_DELETED)
105 goto error;
106
107#ifdef AFS_CACHING_SUPPORT
108 ret = cachefs_page_get_private(page, &pageio, GFP_NOIO);
109 if (ret < 0)
110 goto error;
111
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
121
122 switch (ret) {
123 /* read BIO submitted and wb-journal entry found */
124 case 1:
125 BUG(); // TODO - handle wb-journal match
126
127 /* read BIO submitted (page in cache) */
128 case 0:
129 break;
130
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);
140
141 clear_page(desc.buffer);
142
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;
153 }
154
155#ifdef AFS_CACHING_SUPPORT
156 cachefs_uncache_page(vnode->cache, page);
157#endif
158 goto error;
159 }
160
161 SetPageUptodate(page);
162
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
169 ) {
170 cachefs_uncache_page(vnode->cache, page);
171 unlock_page(page);
172 }
173#else
174 unlock_page(page);
175#endif
176 }
177
178 _leave(" = 0");
179 return 0;
180
181 error:
182 SetPageError(page);
183 unlock_page(page);
184
185 _leave(" = %d", ret);
186 return ret;
187
188} /* end afs_file_readpage() */
189
190/*****************************************************************************/
191/*
192 * get a page cookie for the specified page
193 */
194#ifdef AFS_CACHING_SUPPORT
195int afs_cache_get_page_cookie(struct page *page,
196 struct cachefs_page **_page_cookie)
197{
198 int ret;
199
200 _enter("");
201 ret = cachefs_page_get_private(page,_page_cookie, GFP_NOIO);
202
203 _leave(" = %d", ret);
204 return ret;
205} /* end afs_cache_get_page_cookie() */
206#endif
207
208/*****************************************************************************/
209/*
210 * invalidate part or all of a page
211 */
NeilBrown2ff28e22006-03-26 01:37:18 -0800212static void afs_file_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int ret = 1;
215
216 _enter("{%lu},%lu", page->index, offset);
217
218 BUG_ON(!PageLocked(page));
219
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
225
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.
230 */
231 if (offset == 0) {
232 BUG_ON(!PageLocked(page));
233
234 ret = 0;
235 if (!PageWriteback(page))
236 ret = page->mapping->a_ops->releasepage(page,
237 0);
NeilBrown2ff28e22006-03-26 01:37:18 -0800238 /* possibly should BUG_ON(!ret); - neilb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 }
241
242 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243} /* end afs_file_invalidatepage() */
244
245/*****************************************************************************/
246/*
247 * release a page and cleanup its private data
248 */
Al Viro27496a82005-10-21 03:20:48 -0400249static int afs_file_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct cachefs_page *pageio;
252
253 _enter("{%lu},%x", page->index, gfp_flags);
254
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
260
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700261 pageio = (struct cachefs_page *) page_private(page);
262 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ClearPagePrivate(page);
264
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800265 kfree(pageio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
268 _leave(" = 0");
269 return 0;
270} /* end afs_file_releasepage() */