1 /* AFS filesystem file handling
3 * Copyright (C) 2002, 2007 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>
17 #include <linux/pagemap.h>
18 #include <linux/writeback.h>
21 static int afs_readpage(struct file *file, struct page *page);
22 static void afs_invalidatepage(struct page *page, unsigned long offset);
23 static int afs_releasepage(struct page *page, gfp_t gfp_flags);
24 static int afs_launder_page(struct page *page);
26 const struct file_operations afs_file_operations = {
28 .release = afs_release,
29 .llseek = generic_file_llseek,
31 .write = do_sync_write,
32 .aio_read = generic_file_aio_read,
33 .aio_write = afs_file_write,
34 .mmap = generic_file_readonly_mmap,
35 .splice_read = generic_file_splice_read,
41 const struct inode_operations afs_file_inode_operations = {
42 .getattr = afs_getattr,
43 .setattr = afs_setattr,
44 .permission = afs_permission,
47 const struct address_space_operations afs_fs_aops = {
48 .readpage = afs_readpage,
49 .set_page_dirty = afs_set_page_dirty,
50 .launder_page = afs_launder_page,
51 .releasepage = afs_releasepage,
52 .invalidatepage = afs_invalidatepage,
53 .write_begin = afs_write_begin,
54 .write_end = afs_write_end,
55 .writepage = afs_writepage,
56 .writepages = afs_writepages,
60 * open an AFS file or directory and attach a key to it
62 int afs_open(struct inode *inode, struct file *file)
64 struct afs_vnode *vnode = AFS_FS_I(inode);
68 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
70 key = afs_request_key(vnode->volume->cell);
72 _leave(" = %ld [key]", PTR_ERR(key));
76 ret = afs_validate(vnode, key);
78 _leave(" = %d [val]", ret);
82 file->private_data = key;
88 * release an AFS file or directory and discard its key
90 int afs_release(struct inode *inode, struct file *file)
92 struct afs_vnode *vnode = AFS_FS_I(inode);
94 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
96 key_put(file->private_data);
102 * deal with notification that a page was read from the cache
104 #ifdef AFS_CACHING_SUPPORT
105 static void afs_readpage_read_complete(void *cookie_data,
110 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
115 SetPageUptodate(page);
122 * deal with notification that a page was written to the cache
124 #ifdef AFS_CACHING_SUPPORT
125 static void afs_readpage_write_complete(void *cookie_data,
130 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
137 * AFS read page from file, directory or symlink
139 static int afs_readpage(struct file *file, struct page *page)
141 struct afs_vnode *vnode;
148 inode = page->mapping->host;
150 ASSERT(file != NULL);
151 key = file->private_data;
154 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
156 vnode = AFS_FS_I(inode);
158 BUG_ON(!PageLocked(page));
161 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
164 #ifdef AFS_CACHING_SUPPORT
166 ret = cachefs_read_or_alloc_page(vnode->cache,
168 afs_file_readpage_read_complete,
176 /* read BIO submitted and wb-journal entry found */
178 BUG(); // TODO - handle wb-journal match
180 /* read BIO submitted (page in cache) */
184 /* no page available in cache */
188 offset = page->index << PAGE_CACHE_SHIFT;
189 len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
191 /* read the contents of the file from the server into the
193 ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
195 if (ret == -ENOENT) {
196 _debug("got NOENT from server"
197 " - marking file deleted and stale");
198 set_bit(AFS_VNODE_DELETED, &vnode->flags);
201 #ifdef AFS_CACHING_SUPPORT
202 cachefs_uncache_page(vnode->cache, page);
207 SetPageUptodate(page);
209 #ifdef AFS_CACHING_SUPPORT
210 if (cachefs_write_page(vnode->cache,
212 afs_file_readpage_write_complete,
216 cachefs_uncache_page(vnode->cache, page);
230 _leave(" = %d", ret);
235 * invalidate part or all of a page
237 static void afs_invalidatepage(struct page *page, unsigned long offset)
241 _enter("{%lu},%lu", page->index, offset);
243 BUG_ON(!PageLocked(page));
245 if (PagePrivate(page)) {
246 /* We release buffers only if the entire page is being
248 * The get_block cached value has been unconditionally
249 * invalidated, so real IO is not possible anymore.
252 BUG_ON(!PageLocked(page));
255 if (!PageWriteback(page))
256 ret = page->mapping->a_ops->releasepage(page,
258 /* possibly should BUG_ON(!ret); - neilb */
262 _leave(" = %d", ret);
266 * write back a dirty page
268 static int afs_launder_page(struct page *page)
270 _enter("{%lu}", page->index);
276 * release a page and cleanup its private data
278 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
280 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
281 struct afs_writeback *wb;
283 _enter("{{%x:%u}[%lu],%lx},%x",
284 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
287 if (PagePrivate(page)) {
288 wb = (struct afs_writeback *) page_private(page);
290 set_page_private(page, 0);
291 ClearPagePrivate(page);
292 afs_put_writeback(wb);