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