Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / afs / file.c
1 /* 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>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/pagemap.h>
19 #include <linux/pagevec.h>
20 #include <linux/buffer_head.h>
21 #include "volume.h"
22 #include "vnode.h"
23 #include <rxrpc/call.h>
24 #include "internal.h"
25
26 #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
27
28 #if 0
29 static int afs_file_open(struct inode *inode, struct file *file);
30 static int afs_file_release(struct inode *inode, struct file *file);
31 #endif
32
33 static int afs_file_readpage(struct file *file, struct page *page);
34 static void afs_file_invalidatepage(struct page *page, unsigned long offset);
35 static int afs_file_releasepage(struct page *page, gfp_t gfp_flags);
36 static int afs_file_mmap(struct file * file, struct vm_area_struct * vma);
37
38 #ifdef CONFIG_AFS_FSCACHE
39 static int afs_file_readpages(struct file *filp, struct address_space *mapping,
40                               struct list_head *pages, unsigned nr_pages);
41 static int afs_file_page_mkwrite(struct vm_area_struct *vma, struct page *page);
42 #endif
43
44 struct inode_operations afs_file_inode_operations = {
45         .getattr        = afs_inode_getattr,
46 };
47
48 const struct file_operations afs_file_file_operations = {
49         .llseek         = generic_file_llseek,
50         .read           = generic_file_read,
51         .mmap           = afs_file_mmap,
52         .sendfile       = generic_file_sendfile,
53 };
54
55 const struct address_space_operations afs_fs_aops = {
56         .readpage       = afs_file_readpage,
57 #ifdef CONFIG_AFS_FSCACHE
58         .readpages      = afs_file_readpages,
59 #endif
60         .sync_page      = block_sync_page,
61         .set_page_dirty = __set_page_dirty_nobuffers,
62         .releasepage    = afs_file_releasepage,
63         .invalidatepage = afs_file_invalidatepage,
64 };
65
66 static struct vm_operations_struct afs_fs_vm_operations = {
67         .nopage         = filemap_nopage,
68         .populate       = filemap_populate,
69 #ifdef CONFIG_AFS_FSCACHE
70         .page_mkwrite   = afs_file_page_mkwrite,
71 #endif
72 };
73
74 /*****************************************************************************/
75 /*
76  * set up a memory mapping on an AFS file
77  * - we set our own VMA ops so that we can catch the page becoming writable for
78  *   userspace for shared-writable mmap
79  */
80 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
81 {
82         _enter("");
83
84         file_accessed(file);
85         vma->vm_ops = &afs_fs_vm_operations;
86         return 0;
87
88 } /* end afs_file_mmap() */
89
90 /*****************************************************************************/
91 /*
92  * deal with notification that a page was read from the cache
93  */
94 #ifdef CONFIG_AFS_FSCACHE
95 static void afs_file_readpage_read_complete(struct page *page,
96                                             void *data,
97                                             int error)
98 {
99         _enter("%p,%p,%d", page, data, error);
100
101         /* if the read completes with an error, we just unlock the page and let
102          * the VM reissue the readpage */
103         if (!error)
104                 SetPageUptodate(page);
105         unlock_page(page);
106
107 } /* end afs_file_readpage_read_complete() */
108 #endif
109
110 /*****************************************************************************/
111 /*
112  * deal with notification that a page was written to the cache
113  */
114 #ifdef CONFIG_AFS_FSCACHE
115 static void afs_file_readpage_write_complete(struct page *page,
116                                              void *data,
117                                              int error)
118 {
119         _enter("%p,%p,%d", page, data, error);
120
121         /* note that the page has been written to the cache and can now be
122          * modified */
123         end_page_fs_misc(page);
124
125 } /* end afs_file_readpage_write_complete() */
126 #endif
127
128 /*****************************************************************************/
129 /*
130  * AFS read page from file (or symlink)
131  */
132 static int afs_file_readpage(struct file *file, struct page *page)
133 {
134         struct afs_rxfs_fetch_descriptor desc;
135         struct afs_vnode *vnode;
136         struct inode *inode;
137         int ret;
138
139         inode = page->mapping->host;
140
141         _enter("{%lu},%p{%lu}", inode->i_ino, page, page->index);
142
143         vnode = AFS_FS_I(inode);
144
145         BUG_ON(!PageLocked(page));
146
147         ret = -ESTALE;
148         if (vnode->flags & AFS_VNODE_DELETED)
149                 goto error;
150
151 #ifdef CONFIG_AFS_FSCACHE
152         /* is it cached? */
153         ret = fscache_read_or_alloc_page(vnode->cache,
154                                          page,
155                                          afs_file_readpage_read_complete,
156                                          NULL,
157                                          GFP_KERNEL);
158 #else
159         ret = -ENOBUFS;
160 #endif
161
162         switch (ret) {
163                 /* read BIO submitted (page in cache) */
164         case 0:
165                 break;
166
167                 /* page not yet cached */
168         case -ENODATA:
169                 _debug("cache said ENODATA");
170                 goto go_on;
171
172                 /* page will not be cached */
173         case -ENOBUFS:
174                 _debug("cache said ENOBUFS");
175         default:
176         go_on:
177                 desc.fid        = vnode->fid;
178                 desc.offset     = page->index << PAGE_CACHE_SHIFT;
179                 desc.size       = min((size_t) (inode->i_size - desc.offset),
180                                       (size_t) PAGE_SIZE);
181                 desc.buffer     = kmap(page);
182
183                 clear_page(desc.buffer);
184
185                 /* read the contents of the file from the server into the
186                  * page */
187                 ret = afs_vnode_fetch_data(vnode, &desc);
188                 kunmap(page);
189                 if (ret < 0) {
190                         if (ret == -ENOENT) {
191                                 kdebug("got NOENT from server"
192                                        " - marking file deleted and stale");
193                                 vnode->flags |= AFS_VNODE_DELETED;
194                                 ret = -ESTALE;
195                         }
196
197 #ifdef CONFIG_AFS_FSCACHE
198                         fscache_uncache_page(vnode->cache, page);
199                         ClearPagePrivate(page);
200 #endif
201                         goto error;
202                 }
203
204                 SetPageUptodate(page);
205
206                 /* send the page to the cache */
207 #ifdef CONFIG_AFS_FSCACHE
208                 if (PagePrivate(page)) {
209                         if (TestSetPageFsMisc(page))
210                                 BUG();
211                         if (fscache_write_page(vnode->cache,
212                                                page,
213                                                afs_file_readpage_write_complete,
214                                                NULL,
215                                                GFP_KERNEL) != 0
216                             ) {
217                                 fscache_uncache_page(vnode->cache, page);
218                                 ClearPagePrivate(page);
219                                 end_page_fs_misc(page);
220                         }
221                 }
222 #endif
223                 unlock_page(page);
224         }
225
226         _leave(" = 0");
227         return 0;
228
229  error:
230         SetPageError(page);
231         unlock_page(page);
232
233         _leave(" = %d", ret);
234         return ret;
235
236 } /* end afs_file_readpage() */
237
238 /*****************************************************************************/
239 /*
240  * read a set of pages
241  */
242 #ifdef CONFIG_AFS_FSCACHE
243 static int afs_file_readpages(struct file *filp, struct address_space *mapping,
244                               struct list_head *pages, unsigned nr_pages)
245 {
246         struct afs_vnode *vnode;
247 #if 0
248         struct pagevec lru_pvec;
249         unsigned page_idx;
250 #endif
251         int ret = 0;
252
253         _enter(",{%lu},,%d", mapping->host->i_ino, nr_pages);
254
255         vnode = AFS_FS_I(mapping->host);
256         if (vnode->flags & AFS_VNODE_DELETED) {
257                 _leave(" = -ESTALE");
258                 return -ESTALE;
259         }
260
261         /* attempt to read as many of the pages as possible */
262         ret = fscache_read_or_alloc_pages(vnode->cache,
263                                           mapping,
264                                           pages,
265                                           &nr_pages,
266                                           afs_file_readpage_read_complete,
267                                           NULL,
268                                           mapping_gfp_mask(mapping));
269
270         switch (ret) {
271                 /* all pages are being read from the cache */
272         case 0:
273                 BUG_ON(!list_empty(pages));
274                 BUG_ON(nr_pages != 0);
275                 _leave(" = 0 [reading all]");
276                 return 0;
277
278                 /* there were pages that couldn't be read from the cache */
279         case -ENODATA:
280         case -ENOBUFS:
281                 break;
282
283                 /* other error */
284         default:
285                 _leave(" = %d", ret);
286                 return ret;
287         }
288
289         /* load the missing pages from the network */
290         ret = read_cache_pages(mapping, pages,
291                                (void *) afs_file_readpage, NULL);
292
293         _leave(" = %d [netting]", ret);
294         return ret;
295
296 } /* end afs_file_readpages() */
297 #endif
298
299 /*****************************************************************************/
300 /*
301  * invalidate part or all of a page
302  */
303 static void afs_file_invalidatepage(struct page *page, unsigned long offset)
304 {
305         _enter("{%lu},%lu", page->index, offset);
306
307         BUG_ON(!PageLocked(page));
308
309         if (PagePrivate(page)) {
310                 /* We release buffers only if the entire page is being
311                  * invalidated.
312                  * The get_block cached value has been unconditionally
313                  * invalidated, so real IO is not possible anymore.
314                  */
315                 if (offset == 0 && !PageWriteback(page))
316                         page->mapping->a_ops->releasepage(page, 0);
317         }
318
319         _leave("");
320
321 } /* end afs_file_invalidatepage() */
322
323 /*****************************************************************************/
324 /*
325  * release a page and cleanup its private data
326  */
327 static int afs_file_releasepage(struct page *page, gfp_t gfp_flags)
328 {
329         _enter("{%lu},%x", page->index, gfp_flags);
330
331 #ifdef CONFIG_AFS_FSCACHE
332         wait_on_page_fs_misc(page);
333         fscache_uncache_page(AFS_FS_I(page->mapping->host)->cache, page);
334         ClearPagePrivate(page);
335 #endif
336
337         /* indicate that the page can be released */
338         _leave(" = 1");
339         return 1;
340
341 } /* end afs_file_releasepage() */
342
343 /*****************************************************************************/
344 /*
345  * wait for the disc cache to finish writing before permitting modification of
346  * our page in the page cache
347  */
348 #ifdef CONFIG_AFS_FSCACHE
349 static int afs_file_page_mkwrite(struct vm_area_struct *vma, struct page *page)
350 {
351         wait_on_page_fs_misc(page);
352         return 0;
353
354 } /* end afs_file_page_mkwrite() */
355 #endif