diff options
Diffstat (limited to 'fs/ceph/addr.c')
| -rw-r--r-- | fs/ceph/addr.c | 1194 | 
1 files changed, 1194 insertions, 0 deletions
| diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c new file mode 100644 index 000000000000..ce8ef6107727 --- /dev/null +++ b/fs/ceph/addr.c | |||
| @@ -0,0 +1,1194 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/backing-dev.h> | ||
| 4 | #include <linux/fs.h> | ||
| 5 | #include <linux/mm.h> | ||
| 6 | #include <linux/pagemap.h> | ||
| 7 | #include <linux/writeback.h> /* generic_writepages */ | ||
| 8 | #include <linux/pagevec.h> | ||
| 9 | #include <linux/task_io_accounting_ops.h> | ||
| 10 | |||
| 11 | #include "super.h" | ||
| 12 | #include "osd_client.h" | ||
| 13 | |||
| 14 | /* | ||
| 15 | * Ceph address space ops. | ||
| 16 | * | ||
| 17 | * There are a few funny things going on here. | ||
| 18 | * | ||
| 19 | * The page->private field is used to reference a struct | ||
| 20 | * ceph_snap_context for _every_ dirty page. This indicates which | ||
| 21 | * snapshot the page was logically dirtied in, and thus which snap | ||
| 22 | * context needs to be associated with the osd write during writeback. | ||
| 23 | * | ||
| 24 | * Similarly, struct ceph_inode_info maintains a set of counters to | ||
| 25 | * count dirty pages on the inode. In the absense of snapshots, | ||
| 26 | * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. | ||
| 27 | * | ||
| 28 | * When a snapshot is taken (that is, when the client receives | ||
| 29 | * notification that a snapshot was taken), each inode with caps and | ||
| 30 | * with dirty pages (dirty pages implies there is a cap) gets a new | ||
| 31 | * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending | ||
| 32 | * order, new snaps go to the tail). The i_wrbuffer_ref_head count is | ||
| 33 | * moved to capsnap->dirty. (Unless a sync write is currently in | ||
| 34 | * progress. In that case, the capsnap is said to be "pending", new | ||
| 35 | * writes cannot start, and the capsnap isn't "finalized" until the | ||
| 36 | * write completes (or fails) and a final size/mtime for the inode for | ||
| 37 | * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0. | ||
| 38 | * | ||
| 39 | * On writeback, we must submit writes to the osd IN SNAP ORDER. So, | ||
| 40 | * we look for the first capsnap in i_cap_snaps and write out pages in | ||
| 41 | * that snap context _only_. Then we move on to the next capsnap, | ||
| 42 | * eventually reaching the "live" or "head" context (i.e., pages that | ||
| 43 | * are not yet snapped) and are writing the most recently dirtied | ||
| 44 | * pages. | ||
| 45 | * | ||
| 46 | * Invalidate and so forth must take care to ensure the dirty page | ||
| 47 | * accounting is preserved. | ||
| 48 | */ | ||
| 49 | |||
| 50 | #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10)) | ||
| 51 | #define CONGESTION_OFF_THRESH(congestion_kb) \ | ||
| 52 | (CONGESTION_ON_THRESH(congestion_kb) - \ | ||
| 53 | (CONGESTION_ON_THRESH(congestion_kb) >> 2)) | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | /* | ||
| 58 | * Dirty a page. Optimistically adjust accounting, on the assumption | ||
| 59 | * that we won't race with invalidate. If we do, readjust. | ||
| 60 | */ | ||
| 61 | static int ceph_set_page_dirty(struct page *page) | ||
| 62 | { | ||
| 63 | struct address_space *mapping = page->mapping; | ||
| 64 | struct inode *inode; | ||
| 65 | struct ceph_inode_info *ci; | ||
| 66 | int undo = 0; | ||
| 67 | struct ceph_snap_context *snapc; | ||
| 68 | |||
| 69 | if (unlikely(!mapping)) | ||
| 70 | return !TestSetPageDirty(page); | ||
| 71 | |||
| 72 | if (TestSetPageDirty(page)) { | ||
| 73 | dout("%p set_page_dirty %p idx %lu -- already dirty\n", | ||
| 74 | mapping->host, page, page->index); | ||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | |||
| 78 | inode = mapping->host; | ||
| 79 | ci = ceph_inode(inode); | ||
| 80 | |||
| 81 | /* | ||
| 82 | * Note that we're grabbing a snapc ref here without holding | ||
| 83 | * any locks! | ||
| 84 | */ | ||
| 85 | snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context); | ||
| 86 | |||
| 87 | /* dirty the head */ | ||
| 88 | spin_lock(&inode->i_lock); | ||
| 89 | if (ci->i_wrbuffer_ref_head == 0) | ||
| 90 | ci->i_head_snapc = ceph_get_snap_context(snapc); | ||
| 91 | ++ci->i_wrbuffer_ref_head; | ||
| 92 | if (ci->i_wrbuffer_ref == 0) | ||
| 93 | igrab(inode); | ||
| 94 | ++ci->i_wrbuffer_ref; | ||
| 95 | dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d " | ||
| 96 | "snapc %p seq %lld (%d snaps)\n", | ||
| 97 | mapping->host, page, page->index, | ||
| 98 | ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1, | ||
| 99 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, | ||
| 100 | snapc, snapc->seq, snapc->num_snaps); | ||
| 101 | spin_unlock(&inode->i_lock); | ||
| 102 | |||
| 103 | /* now adjust page */ | ||
| 104 | spin_lock_irq(&mapping->tree_lock); | ||
| 105 | if (page->mapping) { /* Race with truncate? */ | ||
| 106 | WARN_ON_ONCE(!PageUptodate(page)); | ||
| 107 | |||
| 108 | if (mapping_cap_account_dirty(mapping)) { | ||
| 109 | __inc_zone_page_state(page, NR_FILE_DIRTY); | ||
| 110 | __inc_bdi_stat(mapping->backing_dev_info, | ||
| 111 | BDI_RECLAIMABLE); | ||
| 112 | task_io_account_write(PAGE_CACHE_SIZE); | ||
| 113 | } | ||
| 114 | radix_tree_tag_set(&mapping->page_tree, | ||
| 115 | page_index(page), PAGECACHE_TAG_DIRTY); | ||
| 116 | |||
| 117 | /* | ||
| 118 | * Reference snap context in page->private. Also set | ||
| 119 | * PagePrivate so that we get invalidatepage callback. | ||
| 120 | */ | ||
| 121 | page->private = (unsigned long)snapc; | ||
| 122 | SetPagePrivate(page); | ||
| 123 | } else { | ||
| 124 | dout("ANON set_page_dirty %p (raced truncate?)\n", page); | ||
| 125 | undo = 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | spin_unlock_irq(&mapping->tree_lock); | ||
| 129 | |||
| 130 | if (undo) | ||
| 131 | /* whoops, we failed to dirty the page */ | ||
| 132 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | ||
| 133 | |||
| 134 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); | ||
| 135 | |||
| 136 | BUG_ON(!PageDirty(page)); | ||
| 137 | return 1; | ||
| 138 | } | ||
| 139 | |||
| 140 | /* | ||
| 141 | * If we are truncating the full page (i.e. offset == 0), adjust the | ||
| 142 | * dirty page counters appropriately. Only called if there is private | ||
| 143 | * data on the page. | ||
| 144 | */ | ||
| 145 | static void ceph_invalidatepage(struct page *page, unsigned long offset) | ||
| 146 | { | ||
| 147 | struct inode *inode; | ||
| 148 | struct ceph_inode_info *ci; | ||
| 149 | struct ceph_snap_context *snapc = (void *)page->private; | ||
| 150 | |||
| 151 | BUG_ON(!PageLocked(page)); | ||
| 152 | BUG_ON(!page->private); | ||
| 153 | BUG_ON(!PagePrivate(page)); | ||
| 154 | BUG_ON(!page->mapping); | ||
| 155 | |||
| 156 | inode = page->mapping->host; | ||
| 157 | |||
| 158 | /* | ||
| 159 | * We can get non-dirty pages here due to races between | ||
| 160 | * set_page_dirty and truncate_complete_page; just spit out a | ||
| 161 | * warning, in case we end up with accounting problems later. | ||
| 162 | */ | ||
| 163 | if (!PageDirty(page)) | ||
| 164 | pr_err("%p invalidatepage %p page not dirty\n", inode, page); | ||
| 165 | |||
| 166 | if (offset == 0) | ||
| 167 | ClearPageChecked(page); | ||
| 168 | |||
| 169 | ci = ceph_inode(inode); | ||
| 170 | if (offset == 0) { | ||
| 171 | dout("%p invalidatepage %p idx %lu full dirty page %lu\n", | ||
| 172 | inode, page, page->index, offset); | ||
| 173 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | ||
| 174 | ceph_put_snap_context(snapc); | ||
| 175 | page->private = 0; | ||
| 176 | ClearPagePrivate(page); | ||
| 177 | } else { | ||
| 178 | dout("%p invalidatepage %p idx %lu partial dirty page\n", | ||
| 179 | inode, page, page->index); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | /* just a sanity check */ | ||
| 184 | static int ceph_releasepage(struct page *page, gfp_t g) | ||
| 185 | { | ||
| 186 | struct inode *inode = page->mapping ? page->mapping->host : NULL; | ||
| 187 | dout("%p releasepage %p idx %lu\n", inode, page, page->index); | ||
| 188 | WARN_ON(PageDirty(page)); | ||
| 189 | WARN_ON(page->private); | ||
| 190 | WARN_ON(PagePrivate(page)); | ||
| 191 | return 0; | ||
| 192 | } | ||
| 193 | |||
| 194 | /* | ||
| 195 | * read a single page, without unlocking it. | ||
| 196 | */ | ||
| 197 | static int readpage_nounlock(struct file *filp, struct page *page) | ||
| 198 | { | ||
| 199 | struct inode *inode = filp->f_dentry->d_inode; | ||
| 200 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 201 | struct ceph_osd_client *osdc = &ceph_inode_to_client(inode)->osdc; | ||
| 202 | int err = 0; | ||
| 203 | u64 len = PAGE_CACHE_SIZE; | ||
| 204 | |||
| 205 | dout("readpage inode %p file %p page %p index %lu\n", | ||
| 206 | inode, filp, page, page->index); | ||
| 207 | err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, | ||
| 208 | page->index << PAGE_CACHE_SHIFT, &len, | ||
| 209 | ci->i_truncate_seq, ci->i_truncate_size, | ||
| 210 | &page, 1); | ||
| 211 | if (err == -ENOENT) | ||
| 212 | err = 0; | ||
| 213 | if (err < 0) { | ||
| 214 | SetPageError(page); | ||
| 215 | goto out; | ||
| 216 | } else if (err < PAGE_CACHE_SIZE) { | ||
| 217 | /* zero fill remainder of page */ | ||
| 218 | zero_user_segment(page, err, PAGE_CACHE_SIZE); | ||
| 219 | } | ||
| 220 | SetPageUptodate(page); | ||
| 221 | |||
| 222 | out: | ||
| 223 | return err < 0 ? err : 0; | ||
| 224 | } | ||
| 225 | |||
| 226 | static int ceph_readpage(struct file *filp, struct page *page) | ||
| 227 | { | ||
| 228 | int r = readpage_nounlock(filp, page); | ||
| 229 | unlock_page(page); | ||
| 230 | return r; | ||
| 231 | } | ||
| 232 | |||
| 233 | /* | ||
| 234 | * Build a vector of contiguous pages from the provided page list. | ||
| 235 | */ | ||
| 236 | static struct page **page_vector_from_list(struct list_head *page_list, | ||
| 237 | unsigned *nr_pages) | ||
| 238 | { | ||
| 239 | struct page **pages; | ||
| 240 | struct page *page; | ||
| 241 | int next_index, contig_pages = 0; | ||
| 242 | |||
| 243 | /* build page vector */ | ||
| 244 | pages = kmalloc(sizeof(*pages) * *nr_pages, GFP_NOFS); | ||
| 245 | if (!pages) | ||
| 246 | return ERR_PTR(-ENOMEM); | ||
| 247 | |||
| 248 | BUG_ON(list_empty(page_list)); | ||
| 249 | next_index = list_entry(page_list->prev, struct page, lru)->index; | ||
| 250 | list_for_each_entry_reverse(page, page_list, lru) { | ||
| 251 | if (page->index == next_index) { | ||
| 252 | dout("readpages page %d %p\n", contig_pages, page); | ||
| 253 | pages[contig_pages] = page; | ||
| 254 | contig_pages++; | ||
| 255 | next_index++; | ||
| 256 | } else { | ||
| 257 | break; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | *nr_pages = contig_pages; | ||
| 261 | return pages; | ||
| 262 | } | ||
| 263 | |||
| 264 | /* | ||
| 265 | * Read multiple pages. Leave pages we don't read + unlock in page_list; | ||
| 266 | * the caller (VM) cleans them up. | ||
| 267 | */ | ||
| 268 | static int ceph_readpages(struct file *file, struct address_space *mapping, | ||
| 269 | struct list_head *page_list, unsigned nr_pages) | ||
| 270 | { | ||
| 271 | struct inode *inode = file->f_dentry->d_inode; | ||
| 272 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 273 | struct ceph_osd_client *osdc = &ceph_inode_to_client(inode)->osdc; | ||
| 274 | int rc = 0; | ||
| 275 | struct page **pages; | ||
| 276 | struct pagevec pvec; | ||
| 277 | loff_t offset; | ||
| 278 | u64 len; | ||
| 279 | |||
| 280 | dout("readpages %p file %p nr_pages %d\n", | ||
| 281 | inode, file, nr_pages); | ||
| 282 | |||
| 283 | pages = page_vector_from_list(page_list, &nr_pages); | ||
| 284 | if (IS_ERR(pages)) | ||
| 285 | return PTR_ERR(pages); | ||
| 286 | |||
| 287 | /* guess read extent */ | ||
| 288 | offset = pages[0]->index << PAGE_CACHE_SHIFT; | ||
| 289 | len = nr_pages << PAGE_CACHE_SHIFT; | ||
| 290 | rc = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, | ||
| 291 | offset, &len, | ||
| 292 | ci->i_truncate_seq, ci->i_truncate_size, | ||
| 293 | pages, nr_pages); | ||
| 294 | if (rc == -ENOENT) | ||
| 295 | rc = 0; | ||
| 296 | if (rc < 0) | ||
| 297 | goto out; | ||
| 298 | |||
| 299 | /* set uptodate and add to lru in pagevec-sized chunks */ | ||
| 300 | pagevec_init(&pvec, 0); | ||
| 301 | for (; !list_empty(page_list) && len > 0; | ||
| 302 | rc -= PAGE_CACHE_SIZE, len -= PAGE_CACHE_SIZE) { | ||
| 303 | struct page *page = | ||
| 304 | list_entry(page_list->prev, struct page, lru); | ||
| 305 | |||
| 306 | list_del(&page->lru); | ||
| 307 | |||
| 308 | if (rc < (int)PAGE_CACHE_SIZE) { | ||
| 309 | /* zero (remainder of) page */ | ||
| 310 | int s = rc < 0 ? 0 : rc; | ||
| 311 | zero_user_segment(page, s, PAGE_CACHE_SIZE); | ||
| 312 | } | ||
| 313 | |||
| 314 | if (add_to_page_cache(page, mapping, page->index, GFP_NOFS)) { | ||
| 315 | page_cache_release(page); | ||
| 316 | dout("readpages %p add_to_page_cache failed %p\n", | ||
| 317 | inode, page); | ||
| 318 | continue; | ||
| 319 | } | ||
| 320 | dout("readpages %p adding %p idx %lu\n", inode, page, | ||
| 321 | page->index); | ||
| 322 | flush_dcache_page(page); | ||
| 323 | SetPageUptodate(page); | ||
| 324 | unlock_page(page); | ||
| 325 | if (pagevec_add(&pvec, page) == 0) | ||
| 326 | pagevec_lru_add_file(&pvec); /* add to lru */ | ||
| 327 | } | ||
| 328 | pagevec_lru_add_file(&pvec); | ||
| 329 | rc = 0; | ||
| 330 | |||
| 331 | out: | ||
| 332 | kfree(pages); | ||
| 333 | return rc; | ||
| 334 | } | ||
| 335 | |||
| 336 | /* | ||
| 337 | * Get ref for the oldest snapc for an inode with dirty data... that is, the | ||
| 338 | * only snap context we are allowed to write back. | ||
| 339 | * | ||
| 340 | * Caller holds i_lock. | ||
| 341 | */ | ||
| 342 | static struct ceph_snap_context *__get_oldest_context(struct inode *inode, | ||
| 343 | u64 *snap_size) | ||
| 344 | { | ||
| 345 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 346 | struct ceph_snap_context *snapc = NULL; | ||
| 347 | struct ceph_cap_snap *capsnap = NULL; | ||
| 348 | |||
| 349 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { | ||
| 350 | dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, | ||
| 351 | capsnap->context, capsnap->dirty_pages); | ||
| 352 | if (capsnap->dirty_pages) { | ||
| 353 | snapc = ceph_get_snap_context(capsnap->context); | ||
| 354 | if (snap_size) | ||
| 355 | *snap_size = capsnap->size; | ||
| 356 | break; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | if (!snapc && ci->i_snap_realm) { | ||
| 360 | snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context); | ||
| 361 | dout(" head snapc %p has %d dirty pages\n", | ||
| 362 | snapc, ci->i_wrbuffer_ref_head); | ||
| 363 | } | ||
| 364 | return snapc; | ||
| 365 | } | ||
| 366 | |||
| 367 | static struct ceph_snap_context *get_oldest_context(struct inode *inode, | ||
| 368 | u64 *snap_size) | ||
| 369 | { | ||
| 370 | struct ceph_snap_context *snapc = NULL; | ||
| 371 | |||
| 372 | spin_lock(&inode->i_lock); | ||
| 373 | snapc = __get_oldest_context(inode, snap_size); | ||
| 374 | spin_unlock(&inode->i_lock); | ||
| 375 | return snapc; | ||
| 376 | } | ||
| 377 | |||
| 378 | /* | ||
| 379 | * Write a single page, but leave the page locked. | ||
| 380 | * | ||
| 381 | * If we get a write error, set the page error bit, but still adjust the | ||
| 382 | * dirty page accounting (i.e., page is no longer dirty). | ||
| 383 | */ | ||
| 384 | static int writepage_nounlock(struct page *page, struct writeback_control *wbc) | ||
| 385 | { | ||
| 386 | struct inode *inode; | ||
| 387 | struct ceph_inode_info *ci; | ||
| 388 | struct ceph_client *client; | ||
| 389 | struct ceph_osd_client *osdc; | ||
| 390 | loff_t page_off = page->index << PAGE_CACHE_SHIFT; | ||
| 391 | int len = PAGE_CACHE_SIZE; | ||
| 392 | loff_t i_size; | ||
| 393 | int err = 0; | ||
| 394 | struct ceph_snap_context *snapc; | ||
| 395 | u64 snap_size = 0; | ||
| 396 | long writeback_stat; | ||
| 397 | |||
| 398 | dout("writepage %p idx %lu\n", page, page->index); | ||
| 399 | |||
| 400 | if (!page->mapping || !page->mapping->host) { | ||
| 401 | dout("writepage %p - no mapping\n", page); | ||
| 402 | return -EFAULT; | ||
| 403 | } | ||
| 404 | inode = page->mapping->host; | ||
| 405 | ci = ceph_inode(inode); | ||
| 406 | client = ceph_inode_to_client(inode); | ||
| 407 | osdc = &client->osdc; | ||
| 408 | |||
| 409 | /* verify this is a writeable snap context */ | ||
| 410 | snapc = (void *)page->private; | ||
| 411 | if (snapc == NULL) { | ||
| 412 | dout("writepage %p page %p not dirty?\n", inode, page); | ||
| 413 | goto out; | ||
| 414 | } | ||
| 415 | if (snapc != get_oldest_context(inode, &snap_size)) { | ||
| 416 | dout("writepage %p page %p snapc %p not writeable - noop\n", | ||
| 417 | inode, page, (void *)page->private); | ||
| 418 | /* we should only noop if called by kswapd */ | ||
| 419 | WARN_ON((current->flags & PF_MEMALLOC) == 0); | ||
| 420 | goto out; | ||
| 421 | } | ||
| 422 | |||
| 423 | /* is this a partial page at end of file? */ | ||
| 424 | if (snap_size) | ||
| 425 | i_size = snap_size; | ||
| 426 | else | ||
| 427 | i_size = i_size_read(inode); | ||
| 428 | if (i_size < page_off + len) | ||
| 429 | len = i_size - page_off; | ||
| 430 | |||
| 431 | dout("writepage %p page %p index %lu on %llu~%u\n", | ||
| 432 | inode, page, page->index, page_off, len); | ||
| 433 | |||
| 434 | writeback_stat = atomic_long_inc_return(&client->writeback_count); | ||
| 435 | if (writeback_stat > | ||
| 436 | CONGESTION_ON_THRESH(client->mount_args->congestion_kb)) | ||
| 437 | set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC); | ||
| 438 | |||
| 439 | set_page_writeback(page); | ||
| 440 | err = ceph_osdc_writepages(osdc, ceph_vino(inode), | ||
| 441 | &ci->i_layout, snapc, | ||
| 442 | page_off, len, | ||
| 443 | ci->i_truncate_seq, ci->i_truncate_size, | ||
| 444 | &inode->i_mtime, | ||
| 445 | &page, 1, 0, 0, true); | ||
| 446 | if (err < 0) { | ||
| 447 | dout("writepage setting page/mapping error %d %p\n", err, page); | ||
| 448 | SetPageError(page); | ||
| 449 | mapping_set_error(&inode->i_data, err); | ||
| 450 | if (wbc) | ||
| 451 | wbc->pages_skipped++; | ||
| 452 | } else { | ||
| 453 | dout("writepage cleaned page %p\n", page); | ||
| 454 | err = 0; /* vfs expects us to return 0 */ | ||
| 455 | } | ||
| 456 | page->private = 0; | ||
| 457 | ClearPagePrivate(page); | ||
| 458 | end_page_writeback(page); | ||
| 459 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | ||
| 460 | ceph_put_snap_context(snapc); | ||
| 461 | out: | ||
| 462 | return err; | ||
| 463 | } | ||
| 464 | |||
| 465 | static int ceph_writepage(struct page *page, struct writeback_control *wbc) | ||
| 466 | { | ||
| 467 | int err; | ||
| 468 | struct inode *inode = page->mapping->host; | ||
| 469 | BUG_ON(!inode); | ||
| 470 | igrab(inode); | ||
| 471 | err = writepage_nounlock(page, wbc); | ||
| 472 | unlock_page(page); | ||
| 473 | iput(inode); | ||
| 474 | return err; | ||
| 475 | } | ||
| 476 | |||
| 477 | |||
| 478 | /* | ||
| 479 | * lame release_pages helper. release_pages() isn't exported to | ||
| 480 | * modules. | ||
| 481 | */ | ||
| 482 | static void ceph_release_pages(struct page **pages, int num) | ||
| 483 | { | ||
| 484 | struct pagevec pvec; | ||
| 485 | int i; | ||
| 486 | |||
| 487 | pagevec_init(&pvec, 0); | ||
| 488 | for (i = 0; i < num; i++) { | ||
| 489 | if (pagevec_add(&pvec, pages[i]) == 0) | ||
| 490 | pagevec_release(&pvec); | ||
| 491 | } | ||
| 492 | pagevec_release(&pvec); | ||
| 493 | } | ||
| 494 | |||
| 495 | |||
| 496 | /* | ||
| 497 | * async writeback completion handler. | ||
| 498 | * | ||
| 499 | * If we get an error, set the mapping error bit, but not the individual | ||
| 500 | * page error bits. | ||
| 501 | */ | ||
| 502 | static void writepages_finish(struct ceph_osd_request *req, | ||
| 503 | struct ceph_msg *msg) | ||
| 504 | { | ||
| 505 | struct inode *inode = req->r_inode; | ||
| 506 | struct ceph_osd_reply_head *replyhead; | ||
| 507 | struct ceph_osd_op *op; | ||
| 508 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 509 | unsigned wrote; | ||
| 510 | struct page *page; | ||
| 511 | int i; | ||
| 512 | struct ceph_snap_context *snapc = req->r_snapc; | ||
| 513 | struct address_space *mapping = inode->i_mapping; | ||
| 514 | struct writeback_control *wbc = req->r_wbc; | ||
| 515 | __s32 rc = -EIO; | ||
| 516 | u64 bytes = 0; | ||
| 517 | struct ceph_client *client = ceph_inode_to_client(inode); | ||
| 518 | long writeback_stat; | ||
| 519 | unsigned issued = __ceph_caps_issued(ci, NULL); | ||
| 520 | |||
| 521 | /* parse reply */ | ||
| 522 | replyhead = msg->front.iov_base; | ||
| 523 | WARN_ON(le32_to_cpu(replyhead->num_ops) == 0); | ||
| 524 | op = (void *)(replyhead + 1); | ||
| 525 | rc = le32_to_cpu(replyhead->result); | ||
| 526 | bytes = le64_to_cpu(op->extent.length); | ||
| 527 | |||
| 528 | if (rc >= 0) { | ||
| 529 | /* | ||
| 530 | * Assume we wrote the pages we originally sent. The | ||
| 531 | * osd might reply with fewer pages if our writeback | ||
| 532 | * raced with a truncation and was adjusted at the osd, | ||
| 533 | * so don't believe the reply. | ||
| 534 | */ | ||
| 535 | wrote = req->r_num_pages; | ||
| 536 | } else { | ||
| 537 | wrote = 0; | ||
| 538 | mapping_set_error(mapping, rc); | ||
| 539 | } | ||
| 540 | dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n", | ||
| 541 | inode, rc, bytes, wrote); | ||
| 542 | |||
| 543 | /* clean all pages */ | ||
| 544 | for (i = 0; i < req->r_num_pages; i++) { | ||
| 545 | page = req->r_pages[i]; | ||
| 546 | BUG_ON(!page); | ||
| 547 | WARN_ON(!PageUptodate(page)); | ||
| 548 | |||
| 549 | writeback_stat = | ||
| 550 | atomic_long_dec_return(&client->writeback_count); | ||
| 551 | if (writeback_stat < | ||
| 552 | CONGESTION_OFF_THRESH(client->mount_args->congestion_kb)) | ||
| 553 | clear_bdi_congested(&client->backing_dev_info, | ||
| 554 | BLK_RW_ASYNC); | ||
| 555 | |||
| 556 | if (i >= wrote) { | ||
| 557 | dout("inode %p skipping page %p\n", inode, page); | ||
| 558 | wbc->pages_skipped++; | ||
| 559 | } | ||
| 560 | page->private = 0; | ||
| 561 | ClearPagePrivate(page); | ||
| 562 | ceph_put_snap_context(snapc); | ||
| 563 | dout("unlocking %d %p\n", i, page); | ||
| 564 | end_page_writeback(page); | ||
| 565 | |||
| 566 | /* | ||
| 567 | * We lost the cache cap, need to truncate the page before | ||
| 568 | * it is unlocked, otherwise we'd truncate it later in the | ||
| 569 | * page truncation thread, possibly losing some data that | ||
| 570 | * raced its way in | ||
| 571 | */ | ||
| 572 | if ((issued & CEPH_CAP_FILE_CACHE) == 0) | ||
| 573 | generic_error_remove_page(inode->i_mapping, page); | ||
| 574 | |||
| 575 | unlock_page(page); | ||
| 576 | } | ||
| 577 | dout("%p wrote+cleaned %d pages\n", inode, wrote); | ||
| 578 | ceph_put_wrbuffer_cap_refs(ci, req->r_num_pages, snapc); | ||
| 579 | |||
| 580 | ceph_release_pages(req->r_pages, req->r_num_pages); | ||
| 581 | if (req->r_pages_from_pool) | ||
| 582 | mempool_free(req->r_pages, | ||
| 583 | ceph_client(inode->i_sb)->wb_pagevec_pool); | ||
| 584 | else | ||
| 585 | kfree(req->r_pages); | ||
| 586 | ceph_osdc_put_request(req); | ||
| 587 | } | ||
| 588 | |||
| 589 | /* | ||
| 590 | * allocate a page vec, either directly, or if necessary, via a the | ||
| 591 | * mempool. we avoid the mempool if we can because req->r_num_pages | ||
| 592 | * may be less than the maximum write size. | ||
| 593 | */ | ||
| 594 | static void alloc_page_vec(struct ceph_client *client, | ||
| 595 | struct ceph_osd_request *req) | ||
| 596 | { | ||
| 597 | req->r_pages = kmalloc(sizeof(struct page *) * req->r_num_pages, | ||
| 598 | GFP_NOFS); | ||
| 599 | if (!req->r_pages) { | ||
| 600 | req->r_pages = mempool_alloc(client->wb_pagevec_pool, GFP_NOFS); | ||
| 601 | req->r_pages_from_pool = 1; | ||
| 602 | WARN_ON(!req->r_pages); | ||
| 603 | } | ||
| 604 | } | ||
| 605 | |||
| 606 | /* | ||
| 607 | * initiate async writeback | ||
| 608 | */ | ||
| 609 | static int ceph_writepages_start(struct address_space *mapping, | ||
| 610 | struct writeback_control *wbc) | ||
| 611 | { | ||
| 612 | struct inode *inode = mapping->host; | ||
| 613 | struct backing_dev_info *bdi = mapping->backing_dev_info; | ||
| 614 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 615 | struct ceph_client *client; | ||
| 616 | pgoff_t index, start, end; | ||
| 617 | int range_whole = 0; | ||
| 618 | int should_loop = 1; | ||
| 619 | pgoff_t max_pages = 0, max_pages_ever = 0; | ||
| 620 | struct ceph_snap_context *snapc = NULL, *last_snapc = NULL; | ||
| 621 | struct pagevec pvec; | ||
| 622 | int done = 0; | ||
| 623 | int rc = 0; | ||
| 624 | unsigned wsize = 1 << inode->i_blkbits; | ||
| 625 | struct ceph_osd_request *req = NULL; | ||
| 626 | int do_sync; | ||
| 627 | u64 snap_size = 0; | ||
| 628 | |||
| 629 | /* | ||
| 630 | * Include a 'sync' in the OSD request if this is a data | ||
| 631 | * integrity write (e.g., O_SYNC write or fsync()), or if our | ||
| 632 | * cap is being revoked. | ||
| 633 | */ | ||
| 634 | do_sync = wbc->sync_mode == WB_SYNC_ALL; | ||
| 635 | if (ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER)) | ||
| 636 | do_sync = 1; | ||
| 637 | dout("writepages_start %p dosync=%d (mode=%s)\n", | ||
| 638 | inode, do_sync, | ||
| 639 | wbc->sync_mode == WB_SYNC_NONE ? "NONE" : | ||
| 640 | (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); | ||
| 641 | |||
| 642 | client = ceph_inode_to_client(inode); | ||
| 643 | if (client->mount_state == CEPH_MOUNT_SHUTDOWN) { | ||
| 644 | pr_warning("writepage_start %p on forced umount\n", inode); | ||
| 645 | return -EIO; /* we're in a forced umount, don't write! */ | ||
| 646 | } | ||
| 647 | if (client->mount_args->wsize && client->mount_args->wsize < wsize) | ||
| 648 | wsize = client->mount_args->wsize; | ||
| 649 | if (wsize < PAGE_CACHE_SIZE) | ||
| 650 | wsize = PAGE_CACHE_SIZE; | ||
| 651 | max_pages_ever = wsize >> PAGE_CACHE_SHIFT; | ||
| 652 | |||
| 653 | pagevec_init(&pvec, 0); | ||
| 654 | |||
| 655 | /* ?? */ | ||
| 656 | if (wbc->nonblocking && bdi_write_congested(bdi)) { | ||
| 657 | dout(" writepages congested\n"); | ||
| 658 | wbc->encountered_congestion = 1; | ||
| 659 | goto out_final; | ||
| 660 | } | ||
| 661 | |||
| 662 | /* where to start/end? */ | ||
| 663 | if (wbc->range_cyclic) { | ||
| 664 | start = mapping->writeback_index; /* Start from prev offset */ | ||
| 665 | end = -1; | ||
| 666 | dout(" cyclic, start at %lu\n", start); | ||
| 667 | } else { | ||
| 668 | start = wbc->range_start >> PAGE_CACHE_SHIFT; | ||
| 669 | end = wbc->range_end >> PAGE_CACHE_SHIFT; | ||
| 670 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) | ||
| 671 | range_whole = 1; | ||
| 672 | should_loop = 0; | ||
| 673 | dout(" not cyclic, %lu to %lu\n", start, end); | ||
| 674 | } | ||
| 675 | index = start; | ||
| 676 | |||
| 677 | retry: | ||
| 678 | /* find oldest snap context with dirty data */ | ||
| 679 | ceph_put_snap_context(snapc); | ||
| 680 | snapc = get_oldest_context(inode, &snap_size); | ||
| 681 | if (!snapc) { | ||
| 682 | /* hmm, why does writepages get called when there | ||
| 683 | is no dirty data? */ | ||
| 684 | dout(" no snap context with dirty data?\n"); | ||
| 685 | goto out; | ||
| 686 | } | ||
| 687 | dout(" oldest snapc is %p seq %lld (%d snaps)\n", | ||
| 688 | snapc, snapc->seq, snapc->num_snaps); | ||
| 689 | if (last_snapc && snapc != last_snapc) { | ||
| 690 | /* if we switched to a newer snapc, restart our scan at the | ||
| 691 | * start of the original file range. */ | ||
| 692 | dout(" snapc differs from last pass, restarting at %lu\n", | ||
| 693 | index); | ||
| 694 | index = start; | ||
| 695 | } | ||
| 696 | last_snapc = snapc; | ||
| 697 | |||
| 698 | while (!done && index <= end) { | ||
| 699 | unsigned i; | ||
| 700 | int first; | ||
| 701 | pgoff_t next; | ||
| 702 | int pvec_pages, locked_pages; | ||
| 703 | struct page *page; | ||
| 704 | int want; | ||
| 705 | u64 offset, len; | ||
| 706 | struct ceph_osd_request_head *reqhead; | ||
| 707 | struct ceph_osd_op *op; | ||
| 708 | long writeback_stat; | ||
| 709 | |||
| 710 | next = 0; | ||
| 711 | locked_pages = 0; | ||
| 712 | max_pages = max_pages_ever; | ||
| 713 | |||
| 714 | get_more_pages: | ||
| 715 | first = -1; | ||
| 716 | want = min(end - index, | ||
| 717 | min((pgoff_t)PAGEVEC_SIZE, | ||
| 718 | max_pages - (pgoff_t)locked_pages) - 1) | ||
| 719 | + 1; | ||
| 720 | pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index, | ||
| 721 | PAGECACHE_TAG_DIRTY, | ||
| 722 | want); | ||
| 723 | dout("pagevec_lookup_tag got %d\n", pvec_pages); | ||
| 724 | if (!pvec_pages && !locked_pages) | ||
| 725 | break; | ||
| 726 | for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { | ||
| 727 | page = pvec.pages[i]; | ||
| 728 | dout("? %p idx %lu\n", page, page->index); | ||
| 729 | if (locked_pages == 0) | ||
| 730 | lock_page(page); /* first page */ | ||
| 731 | else if (!trylock_page(page)) | ||
| 732 | break; | ||
| 733 | |||
| 734 | /* only dirty pages, or our accounting breaks */ | ||
| 735 | if (unlikely(!PageDirty(page)) || | ||
| 736 | unlikely(page->mapping != mapping)) { | ||
| 737 | dout("!dirty or !mapping %p\n", page); | ||
| 738 | unlock_page(page); | ||
| 739 | break; | ||
| 740 | } | ||
| 741 | if (!wbc->range_cyclic && page->index > end) { | ||
| 742 | dout("end of range %p\n", page); | ||
| 743 | done = 1; | ||
| 744 | unlock_page(page); | ||
| 745 | break; | ||
| 746 | } | ||
| 747 | if (next && (page->index != next)) { | ||
| 748 | dout("not consecutive %p\n", page); | ||
| 749 | unlock_page(page); | ||
| 750 | break; | ||
| 751 | } | ||
| 752 | if (wbc->sync_mode != WB_SYNC_NONE) { | ||
| 753 | dout("waiting on writeback %p\n", page); | ||
| 754 | wait_on_page_writeback(page); | ||
| 755 | } | ||
| 756 | if ((snap_size && page_offset(page) > snap_size) || | ||
| 757 | (!snap_size && | ||
| 758 | page_offset(page) > i_size_read(inode))) { | ||
| 759 | dout("%p page eof %llu\n", page, snap_size ? | ||
| 760 | snap_size : i_size_read(inode)); | ||
| 761 | done = 1; | ||
| 762 | unlock_page(page); | ||
| 763 | break; | ||
| 764 | } | ||
| 765 | if (PageWriteback(page)) { | ||
| 766 | dout("%p under writeback\n", page); | ||
| 767 | unlock_page(page); | ||
| 768 | break; | ||
| 769 | } | ||
| 770 | |||
| 771 | /* only if matching snap context */ | ||
| 772 | if (snapc != (void *)page->private) { | ||
| 773 | dout("page snapc %p != oldest %p\n", | ||
| 774 | (void *)page->private, snapc); | ||
| 775 | unlock_page(page); | ||
| 776 | if (!locked_pages) | ||
| 777 | continue; /* keep looking for snap */ | ||
| 778 | break; | ||
| 779 | } | ||
| 780 | |||
| 781 | if (!clear_page_dirty_for_io(page)) { | ||
| 782 | dout("%p !clear_page_dirty_for_io\n", page); | ||
| 783 | unlock_page(page); | ||
| 784 | break; | ||
| 785 | } | ||
| 786 | |||
| 787 | /* ok */ | ||
| 788 | if (locked_pages == 0) { | ||
| 789 | /* prepare async write request */ | ||
| 790 | offset = page->index << PAGE_CACHE_SHIFT; | ||
| 791 | len = wsize; | ||
| 792 | req = ceph_osdc_new_request(&client->osdc, | ||
| 793 | &ci->i_layout, | ||
| 794 | ceph_vino(inode), | ||
| 795 | offset, &len, | ||
| 796 | CEPH_OSD_OP_WRITE, | ||
| 797 | CEPH_OSD_FLAG_WRITE | | ||
| 798 | CEPH_OSD_FLAG_ONDISK, | ||
| 799 | snapc, do_sync, | ||
| 800 | ci->i_truncate_seq, | ||
| 801 | ci->i_truncate_size, | ||
| 802 | &inode->i_mtime, true, 1); | ||
| 803 | max_pages = req->r_num_pages; | ||
| 804 | |||
| 805 | alloc_page_vec(client, req); | ||
| 806 | req->r_callback = writepages_finish; | ||
| 807 | req->r_inode = inode; | ||
| 808 | req->r_wbc = wbc; | ||
| 809 | } | ||
| 810 | |||
| 811 | /* note position of first page in pvec */ | ||
| 812 | if (first < 0) | ||
| 813 | first = i; | ||
| 814 | dout("%p will write page %p idx %lu\n", | ||
| 815 | inode, page, page->index); | ||
| 816 | |||
| 817 | writeback_stat = atomic_long_inc_return(&client->writeback_count); | ||
| 818 | if (writeback_stat > CONGESTION_ON_THRESH(client->mount_args->congestion_kb)) { | ||
| 819 | set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC); | ||
| 820 | } | ||
| 821 | |||
| 822 | set_page_writeback(page); | ||
| 823 | req->r_pages[locked_pages] = page; | ||
| 824 | locked_pages++; | ||
| 825 | next = page->index + 1; | ||
| 826 | } | ||
| 827 | |||
| 828 | /* did we get anything? */ | ||
| 829 | if (!locked_pages) | ||
| 830 | goto release_pvec_pages; | ||
| 831 | if (i) { | ||
| 832 | int j; | ||
| 833 | BUG_ON(!locked_pages || first < 0); | ||
| 834 | |||
| 835 | if (pvec_pages && i == pvec_pages && | ||
| 836 | locked_pages < max_pages) { | ||
| 837 | dout("reached end pvec, trying for more\n"); | ||
| 838 | pagevec_reinit(&pvec); | ||
| 839 | goto get_more_pages; | ||
| 840 | } | ||
| 841 | |||
| 842 | /* shift unused pages over in the pvec... we | ||
| 843 | * will need to release them below. */ | ||
| 844 | for (j = i; j < pvec_pages; j++) { | ||
| 845 | dout(" pvec leftover page %p\n", | ||
| 846 | pvec.pages[j]); | ||
| 847 | pvec.pages[j-i+first] = pvec.pages[j]; | ||
| 848 | } | ||
| 849 | pvec.nr -= i-first; | ||
| 850 | } | ||
| 851 | |||
| 852 | /* submit the write */ | ||
| 853 | offset = req->r_pages[0]->index << PAGE_CACHE_SHIFT; | ||
| 854 | len = min((snap_size ? snap_size : i_size_read(inode)) - offset, | ||
| 855 | (u64)locked_pages << PAGE_CACHE_SHIFT); | ||
| 856 | dout("writepages got %d pages at %llu~%llu\n", | ||
| 857 | locked_pages, offset, len); | ||
| 858 | |||
| 859 | /* revise final length, page count */ | ||
| 860 | req->r_num_pages = locked_pages; | ||
| 861 | reqhead = req->r_request->front.iov_base; | ||
| 862 | op = (void *)(reqhead + 1); | ||
| 863 | op->extent.length = cpu_to_le64(len); | ||
| 864 | op->payload_len = cpu_to_le32(len); | ||
| 865 | req->r_request->hdr.data_len = cpu_to_le32(len); | ||
| 866 | |||
| 867 | ceph_osdc_start_request(&client->osdc, req, true); | ||
| 868 | req = NULL; | ||
| 869 | |||
| 870 | /* continue? */ | ||
| 871 | index = next; | ||
| 872 | wbc->nr_to_write -= locked_pages; | ||
| 873 | if (wbc->nr_to_write <= 0) | ||
| 874 | done = 1; | ||
| 875 | |||
| 876 | release_pvec_pages: | ||
| 877 | dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, | ||
| 878 | pvec.nr ? pvec.pages[0] : NULL); | ||
| 879 | pagevec_release(&pvec); | ||
| 880 | |||
| 881 | if (locked_pages && !done) | ||
| 882 | goto retry; | ||
| 883 | } | ||
| 884 | |||
| 885 | if (should_loop && !done) { | ||
| 886 | /* more to do; loop back to beginning of file */ | ||
| 887 | dout("writepages looping back to beginning of file\n"); | ||
| 888 | should_loop = 0; | ||
| 889 | index = 0; | ||
| 890 | goto retry; | ||
| 891 | } | ||
| 892 | |||
| 893 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | ||
| 894 | mapping->writeback_index = index; | ||
| 895 | |||
| 896 | out: | ||
| 897 | if (req) | ||
| 898 | ceph_osdc_put_request(req); | ||
| 899 | if (rc > 0) | ||
| 900 | rc = 0; /* vfs expects us to return 0 */ | ||
| 901 | ceph_put_snap_context(snapc); | ||
| 902 | dout("writepages done, rc = %d\n", rc); | ||
| 903 | out_final: | ||
| 904 | return rc; | ||
| 905 | } | ||
| 906 | |||
| 907 | |||
| 908 | |||
| 909 | /* | ||
| 910 | * See if a given @snapc is either writeable, or already written. | ||
| 911 | */ | ||
| 912 | static int context_is_writeable_or_written(struct inode *inode, | ||
| 913 | struct ceph_snap_context *snapc) | ||
| 914 | { | ||
| 915 | struct ceph_snap_context *oldest = get_oldest_context(inode, NULL); | ||
| 916 | return !oldest || snapc->seq <= oldest->seq; | ||
| 917 | } | ||
| 918 | |||
| 919 | /* | ||
| 920 | * We are only allowed to write into/dirty the page if the page is | ||
| 921 | * clean, or already dirty within the same snap context. | ||
| 922 | * | ||
| 923 | * called with page locked. | ||
| 924 | * return success with page locked, | ||
| 925 | * or any failure (incl -EAGAIN) with page unlocked. | ||
| 926 | */ | ||
| 927 | static int ceph_update_writeable_page(struct file *file, | ||
| 928 | loff_t pos, unsigned len, | ||
| 929 | struct page *page) | ||
| 930 | { | ||
| 931 | struct inode *inode = file->f_dentry->d_inode; | ||
| 932 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 933 | struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; | ||
| 934 | loff_t page_off = pos & PAGE_CACHE_MASK; | ||
| 935 | int pos_in_page = pos & ~PAGE_CACHE_MASK; | ||
| 936 | int end_in_page = pos_in_page + len; | ||
| 937 | loff_t i_size; | ||
| 938 | struct ceph_snap_context *snapc; | ||
| 939 | int r; | ||
| 940 | |||
| 941 | retry_locked: | ||
| 942 | /* writepages currently holds page lock, but if we change that later, */ | ||
| 943 | wait_on_page_writeback(page); | ||
| 944 | |||
| 945 | /* check snap context */ | ||
| 946 | BUG_ON(!ci->i_snap_realm); | ||
| 947 | down_read(&mdsc->snap_rwsem); | ||
| 948 | BUG_ON(!ci->i_snap_realm->cached_context); | ||
| 949 | if (page->private && | ||
| 950 | (void *)page->private != ci->i_snap_realm->cached_context) { | ||
| 951 | /* | ||
| 952 | * this page is already dirty in another (older) snap | ||
| 953 | * context! is it writeable now? | ||
| 954 | */ | ||
| 955 | snapc = get_oldest_context(inode, NULL); | ||
| 956 | up_read(&mdsc->snap_rwsem); | ||
| 957 | |||
| 958 | if (snapc != (void *)page->private) { | ||
| 959 | dout(" page %p snapc %p not current or oldest\n", | ||
| 960 | page, (void *)page->private); | ||
| 961 | /* | ||
| 962 | * queue for writeback, and wait for snapc to | ||
| 963 | * be writeable or written | ||
| 964 | */ | ||
| 965 | snapc = ceph_get_snap_context((void *)page->private); | ||
| 966 | unlock_page(page); | ||
| 967 | ceph_queue_writeback(inode); | ||
| 968 | r = wait_event_interruptible(ci->i_cap_wq, | ||
| 969 | context_is_writeable_or_written(inode, snapc)); | ||
| 970 | ceph_put_snap_context(snapc); | ||
| 971 | if (r == -ERESTARTSYS) | ||
| 972 | return r; | ||
| 973 | return -EAGAIN; | ||
| 974 | } | ||
| 975 | |||
| 976 | /* yay, writeable, do it now (without dropping page lock) */ | ||
| 977 | dout(" page %p snapc %p not current, but oldest\n", | ||
| 978 | page, snapc); | ||
| 979 | if (!clear_page_dirty_for_io(page)) | ||
| 980 | goto retry_locked; | ||
| 981 | r = writepage_nounlock(page, NULL); | ||
| 982 | if (r < 0) | ||
| 983 | goto fail_nosnap; | ||
| 984 | goto retry_locked; | ||
| 985 | } | ||
| 986 | |||
| 987 | if (PageUptodate(page)) { | ||
| 988 | dout(" page %p already uptodate\n", page); | ||
| 989 | return 0; | ||
| 990 | } | ||
| 991 | |||
| 992 | /* full page? */ | ||
| 993 | if (pos_in_page == 0 && len == PAGE_CACHE_SIZE) | ||
| 994 | return 0; | ||
| 995 | |||
| 996 | /* past end of file? */ | ||
| 997 | i_size = inode->i_size; /* caller holds i_mutex */ | ||
| 998 | |||
| 999 | if (i_size + len > inode->i_sb->s_maxbytes) { | ||
| 1000 | /* file is too big */ | ||
| 1001 | r = -EINVAL; | ||
| 1002 | goto fail; | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | if (page_off >= i_size || | ||
| 1006 | (pos_in_page == 0 && (pos+len) >= i_size && | ||
| 1007 | end_in_page - pos_in_page != PAGE_CACHE_SIZE)) { | ||
| 1008 | dout(" zeroing %p 0 - %d and %d - %d\n", | ||
| 1009 | page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE); | ||
| 1010 | zero_user_segments(page, | ||
| 1011 | 0, pos_in_page, | ||
| 1012 | end_in_page, PAGE_CACHE_SIZE); | ||
| 1013 | return 0; | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | /* we need to read it. */ | ||
| 1017 | up_read(&mdsc->snap_rwsem); | ||
| 1018 | r = readpage_nounlock(file, page); | ||
| 1019 | if (r < 0) | ||
| 1020 | goto fail_nosnap; | ||
| 1021 | goto retry_locked; | ||
| 1022 | |||
| 1023 | fail: | ||
| 1024 | up_read(&mdsc->snap_rwsem); | ||
| 1025 | fail_nosnap: | ||
| 1026 | unlock_page(page); | ||
| 1027 | return r; | ||
| 1028 | } | ||
| 1029 | |||
| 1030 | /* | ||
| 1031 | * We are only allowed to write into/dirty the page if the page is | ||
| 1032 | * clean, or already dirty within the same snap context. | ||
| 1033 | */ | ||
| 1034 | static int ceph_write_begin(struct file *file, struct address_space *mapping, | ||
| 1035 | loff_t pos, unsigned len, unsigned flags, | ||
| 1036 | struct page **pagep, void **fsdata) | ||
| 1037 | { | ||
| 1038 | struct inode *inode = file->f_dentry->d_inode; | ||
| 1039 | struct page *page; | ||
| 1040 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | ||
| 1041 | int r; | ||
| 1042 | |||
| 1043 | do { | ||
| 1044 | /* get a page */ | ||
| 1045 | page = grab_cache_page_write_begin(mapping, index, 0); | ||
| 1046 | if (!page) | ||
| 1047 | return -ENOMEM; | ||
| 1048 | *pagep = page; | ||
| 1049 | |||
| 1050 | dout("write_begin file %p inode %p page %p %d~%d\n", file, | ||
| 1051 | inode, page, (int)pos, (int)len); | ||
| 1052 | |||
| 1053 | r = ceph_update_writeable_page(file, pos, len, page); | ||
| 1054 | } while (r == -EAGAIN); | ||
| 1055 | |||
| 1056 | return r; | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | /* | ||
| 1060 | * we don't do anything in here that simple_write_end doesn't do | ||
| 1061 | * except adjust dirty page accounting and drop read lock on | ||
| 1062 | * mdsc->snap_rwsem. | ||
| 1063 | */ | ||
| 1064 | static int ceph_write_end(struct file *file, struct address_space *mapping, | ||
| 1065 | loff_t pos, unsigned len, unsigned copied, | ||
| 1066 | struct page *page, void *fsdata) | ||
| 1067 | { | ||
| 1068 | struct inode *inode = file->f_dentry->d_inode; | ||
| 1069 | struct ceph_client *client = ceph_inode_to_client(inode); | ||
| 1070 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 1071 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); | ||
| 1072 | int check_cap = 0; | ||
| 1073 | |||
| 1074 | dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, | ||
| 1075 | inode, page, (int)pos, (int)copied, (int)len); | ||
| 1076 | |||
| 1077 | /* zero the stale part of the page if we did a short copy */ | ||
| 1078 | if (copied < len) | ||
| 1079 | zero_user_segment(page, from+copied, len); | ||
| 1080 | |||
| 1081 | /* did file size increase? */ | ||
| 1082 | /* (no need for i_size_read(); we caller holds i_mutex */ | ||
| 1083 | if (pos+copied > inode->i_size) | ||
| 1084 | check_cap = ceph_inode_set_size(inode, pos+copied); | ||
| 1085 | |||
| 1086 | if (!PageUptodate(page)) | ||
| 1087 | SetPageUptodate(page); | ||
| 1088 | |||
| 1089 | set_page_dirty(page); | ||
| 1090 | |||
| 1091 | unlock_page(page); | ||
| 1092 | up_read(&mdsc->snap_rwsem); | ||
| 1093 | page_cache_release(page); | ||
| 1094 | |||
| 1095 | if (check_cap) | ||
| 1096 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); | ||
| 1097 | |||
| 1098 | return copied; | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | /* | ||
| 1102 | * we set .direct_IO to indicate direct io is supported, but since we | ||
| 1103 | * intercept O_DIRECT reads and writes early, this function should | ||
| 1104 | * never get called. | ||
| 1105 | */ | ||
| 1106 | static ssize_t ceph_direct_io(int rw, struct kiocb *iocb, | ||
| 1107 | const struct iovec *iov, | ||
| 1108 | loff_t pos, unsigned long nr_segs) | ||
| 1109 | { | ||
| 1110 | WARN_ON(1); | ||
| 1111 | return -EINVAL; | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | const struct address_space_operations ceph_aops = { | ||
| 1115 | .readpage = ceph_readpage, | ||
| 1116 | .readpages = ceph_readpages, | ||
| 1117 | .writepage = ceph_writepage, | ||
| 1118 | .writepages = ceph_writepages_start, | ||
| 1119 | .write_begin = ceph_write_begin, | ||
| 1120 | .write_end = ceph_write_end, | ||
| 1121 | .set_page_dirty = ceph_set_page_dirty, | ||
| 1122 | .invalidatepage = ceph_invalidatepage, | ||
| 1123 | .releasepage = ceph_releasepage, | ||
| 1124 | .direct_IO = ceph_direct_io, | ||
| 1125 | }; | ||
| 1126 | |||
| 1127 | |||
| 1128 | /* | ||
| 1129 | * vm ops | ||
| 1130 | */ | ||
| 1131 | |||
| 1132 | /* | ||
| 1133 | * Reuse write_begin here for simplicity. | ||
| 1134 | */ | ||
| 1135 | static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) | ||
| 1136 | { | ||
| 1137 | struct inode *inode = vma->vm_file->f_dentry->d_inode; | ||
| 1138 | struct page *page = vmf->page; | ||
| 1139 | struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; | ||
| 1140 | loff_t off = page->index << PAGE_CACHE_SHIFT; | ||
| 1141 | loff_t size, len; | ||
| 1142 | int ret; | ||
| 1143 | |||
| 1144 | size = i_size_read(inode); | ||
| 1145 | if (off + PAGE_CACHE_SIZE <= size) | ||
| 1146 | len = PAGE_CACHE_SIZE; | ||
| 1147 | else | ||
| 1148 | len = size & ~PAGE_CACHE_MASK; | ||
| 1149 | |||
| 1150 | dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode, | ||
| 1151 | off, len, page, page->index); | ||
| 1152 | |||
| 1153 | lock_page(page); | ||
| 1154 | |||
| 1155 | ret = VM_FAULT_NOPAGE; | ||
| 1156 | if ((off > size) || | ||
| 1157 | (page->mapping != inode->i_mapping)) | ||
| 1158 | goto out; | ||
| 1159 | |||
| 1160 | ret = ceph_update_writeable_page(vma->vm_file, off, len, page); | ||
| 1161 | if (ret == 0) { | ||
| 1162 | /* success. we'll keep the page locked. */ | ||
| 1163 | set_page_dirty(page); | ||
| 1164 | up_read(&mdsc->snap_rwsem); | ||
| 1165 | ret = VM_FAULT_LOCKED; | ||
| 1166 | } else { | ||
| 1167 | if (ret == -ENOMEM) | ||
| 1168 | ret = VM_FAULT_OOM; | ||
| 1169 | else | ||
| 1170 | ret = VM_FAULT_SIGBUS; | ||
| 1171 | } | ||
| 1172 | out: | ||
| 1173 | dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret); | ||
| 1174 | if (ret != VM_FAULT_LOCKED) | ||
| 1175 | unlock_page(page); | ||
| 1176 | return ret; | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | static struct vm_operations_struct ceph_vmops = { | ||
| 1180 | .fault = filemap_fault, | ||
| 1181 | .page_mkwrite = ceph_page_mkwrite, | ||
| 1182 | }; | ||
| 1183 | |||
| 1184 | int ceph_mmap(struct file *file, struct vm_area_struct *vma) | ||
| 1185 | { | ||
| 1186 | struct address_space *mapping = file->f_mapping; | ||
| 1187 | |||
| 1188 | if (!mapping->a_ops->readpage) | ||
| 1189 | return -ENOEXEC; | ||
| 1190 | file_accessed(file); | ||
| 1191 | vma->vm_ops = &ceph_vmops; | ||
| 1192 | vma->vm_flags |= VM_CAN_NONLINEAR; | ||
| 1193 | return 0; | ||
| 1194 | } | ||
