David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1 | /* AFS filesystem file handling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 3 | * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "internal.h" |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | static int afs_file_readpage(struct file *file, struct page *page); |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 21 | static void afs_file_invalidatepage(struct page *page, unsigned long offset); |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 22 | static int afs_file_releasepage(struct page *page, gfp_t gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 24 | const struct file_operations afs_file_operations = { |
| 25 | .open = afs_open, |
| 26 | .release = afs_release, |
| 27 | .llseek = generic_file_llseek, |
| 28 | .read = do_sync_read, |
| 29 | .aio_read = generic_file_aio_read, |
| 30 | .mmap = generic_file_readonly_mmap, |
| 31 | .sendfile = generic_file_sendfile, |
| 32 | }; |
| 33 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 34 | const struct inode_operations afs_file_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | .getattr = afs_inode_getattr, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 36 | .permission = afs_permission, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 39 | const struct address_space_operations afs_fs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | .readpage = afs_file_readpage, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | .set_page_dirty = __set_page_dirty_nobuffers, |
| 42 | .releasepage = afs_file_releasepage, |
| 43 | .invalidatepage = afs_file_invalidatepage, |
| 44 | }; |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 47 | * open an AFS file or directory and attach a key to it |
| 48 | */ |
| 49 | int afs_open(struct inode *inode, struct file *file) |
| 50 | { |
| 51 | struct afs_vnode *vnode = AFS_FS_I(inode); |
| 52 | struct key *key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 53 | int ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 54 | |
| 55 | _enter("{%x:%x},", vnode->fid.vid, vnode->fid.vnode); |
| 56 | |
| 57 | key = afs_request_key(vnode->volume->cell); |
| 58 | if (IS_ERR(key)) { |
| 59 | _leave(" = %ld [key]", PTR_ERR(key)); |
| 60 | return PTR_ERR(key); |
| 61 | } |
| 62 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 63 | ret = afs_validate(vnode, key); |
| 64 | if (ret < 0) { |
| 65 | _leave(" = %d [val]", ret); |
| 66 | return ret; |
| 67 | } |
| 68 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 69 | file->private_data = key; |
| 70 | _leave(" = 0"); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * release an AFS file or directory and discard its key |
| 76 | */ |
| 77 | int afs_release(struct inode *inode, struct file *file) |
| 78 | { |
| 79 | struct afs_vnode *vnode = AFS_FS_I(inode); |
| 80 | |
| 81 | _enter("{%x:%x},", vnode->fid.vid, vnode->fid.vnode); |
| 82 | |
| 83 | key_put(file->private_data); |
| 84 | _leave(" = 0"); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * deal with notification that a page was read from the cache |
| 90 | */ |
| 91 | #ifdef AFS_CACHING_SUPPORT |
| 92 | static void afs_file_readpage_read_complete(void *cookie_data, |
| 93 | struct page *page, |
| 94 | void *data, |
| 95 | int error) |
| 96 | { |
| 97 | _enter("%p,%p,%p,%d", cookie_data, page, data, error); |
| 98 | |
| 99 | if (error) |
| 100 | SetPageError(page); |
| 101 | else |
| 102 | SetPageUptodate(page); |
| 103 | unlock_page(page); |
| 104 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | #endif |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | /* |
| 109 | * deal with notification that a page was written to the cache |
| 110 | */ |
| 111 | #ifdef AFS_CACHING_SUPPORT |
| 112 | static void afs_file_readpage_write_complete(void *cookie_data, |
| 113 | struct page *page, |
| 114 | void *data, |
| 115 | int error) |
| 116 | { |
| 117 | _enter("%p,%p,%p,%d", cookie_data, page, data, error); |
| 118 | |
| 119 | unlock_page(page); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | #endif |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* |
| 124 | * AFS read page from file (or symlink) |
| 125 | */ |
| 126 | static int afs_file_readpage(struct file *file, struct page *page) |
| 127 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | struct afs_vnode *vnode; |
| 129 | struct inode *inode; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 130 | struct key *key; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 131 | size_t len; |
| 132 | off_t offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | int ret; |
| 134 | |
| 135 | inode = page->mapping->host; |
| 136 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 137 | ASSERT(file != NULL); |
| 138 | key = file->private_data; |
| 139 | ASSERT(key != NULL); |
| 140 | |
| 141 | _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | vnode = AFS_FS_I(inode); |
| 144 | |
Matt Mackall | cd7619d | 2005-05-01 08:59:01 -0700 | [diff] [blame] | 145 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | ret = -ESTALE; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 148 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | goto error; |
| 150 | |
| 151 | #ifdef AFS_CACHING_SUPPORT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* is it cached? */ |
| 153 | ret = cachefs_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 and wb-journal entry found */ |
| 164 | case 1: |
| 165 | BUG(); // TODO - handle wb-journal match |
| 166 | |
| 167 | /* read BIO submitted (page in cache) */ |
| 168 | case 0: |
| 169 | break; |
| 170 | |
| 171 | /* no page available in cache */ |
| 172 | case -ENOBUFS: |
| 173 | case -ENODATA: |
| 174 | default: |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 175 | offset = page->index << PAGE_CACHE_SHIFT; |
| 176 | len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | /* read the contents of the file from the server into the |
| 179 | * page */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 180 | ret = afs_vnode_fetch_data(vnode, key, offset, len, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if (ret < 0) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 182 | if (ret == -ENOENT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | _debug("got NOENT from server" |
| 184 | " - marking file deleted and stale"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 185 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | ret = -ESTALE; |
| 187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | #ifdef AFS_CACHING_SUPPORT |
| 189 | cachefs_uncache_page(vnode->cache, page); |
| 190 | #endif |
| 191 | goto error; |
| 192 | } |
| 193 | |
| 194 | SetPageUptodate(page); |
| 195 | |
| 196 | #ifdef AFS_CACHING_SUPPORT |
| 197 | if (cachefs_write_page(vnode->cache, |
| 198 | page, |
| 199 | afs_file_readpage_write_complete, |
| 200 | NULL, |
| 201 | GFP_KERNEL) != 0 |
| 202 | ) { |
| 203 | cachefs_uncache_page(vnode->cache, page); |
| 204 | unlock_page(page); |
| 205 | } |
| 206 | #else |
| 207 | unlock_page(page); |
| 208 | #endif |
| 209 | } |
| 210 | |
| 211 | _leave(" = 0"); |
| 212 | return 0; |
| 213 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 214 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | SetPageError(page); |
| 216 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | _leave(" = %d", ret); |
| 218 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | /* |
| 222 | * get a page cookie for the specified page |
| 223 | */ |
| 224 | #ifdef AFS_CACHING_SUPPORT |
| 225 | int afs_cache_get_page_cookie(struct page *page, |
| 226 | struct cachefs_page **_page_cookie) |
| 227 | { |
| 228 | int ret; |
| 229 | |
| 230 | _enter(""); |
| 231 | ret = cachefs_page_get_private(page,_page_cookie, GFP_NOIO); |
| 232 | |
| 233 | _leave(" = %d", ret); |
| 234 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | #endif |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /* |
| 239 | * invalidate part or all of a page |
| 240 | */ |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 241 | static void afs_file_invalidatepage(struct page *page, unsigned long offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
| 243 | int ret = 1; |
| 244 | |
| 245 | _enter("{%lu},%lu", page->index, offset); |
| 246 | |
| 247 | BUG_ON(!PageLocked(page)); |
| 248 | |
| 249 | if (PagePrivate(page)) { |
| 250 | #ifdef AFS_CACHING_SUPPORT |
| 251 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
| 252 | cachefs_uncache_page(vnode->cache,page); |
| 253 | #endif |
| 254 | |
| 255 | /* We release buffers only if the entire page is being |
| 256 | * invalidated. |
| 257 | * The get_block cached value has been unconditionally |
| 258 | * invalidated, so real IO is not possible anymore. |
| 259 | */ |
| 260 | if (offset == 0) { |
| 261 | BUG_ON(!PageLocked(page)); |
| 262 | |
| 263 | ret = 0; |
| 264 | if (!PageWriteback(page)) |
| 265 | ret = page->mapping->a_ops->releasepage(page, |
| 266 | 0); |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 267 | /* possibly should BUG_ON(!ret); - neilb */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | _leave(" = %d", ret); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | /* |
| 275 | * release a page and cleanup its private data |
| 276 | */ |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 277 | static int afs_file_releasepage(struct page *page, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
| 279 | struct cachefs_page *pageio; |
| 280 | |
| 281 | _enter("{%lu},%x", page->index, gfp_flags); |
| 282 | |
| 283 | if (PagePrivate(page)) { |
| 284 | #ifdef AFS_CACHING_SUPPORT |
| 285 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
| 286 | cachefs_uncache_page(vnode->cache, page); |
| 287 | #endif |
| 288 | |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 289 | pageio = (struct cachefs_page *) page_private(page); |
| 290 | set_page_private(page, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | ClearPagePrivate(page); |
| 292 | |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 293 | kfree(pageio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | _leave(" = 0"); |
| 297 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 298 | } |