1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Copyright (C) 2002, 2003 Cluster File Systems, Inc
23 * this started as an implementation of an io daemon that woke regularly
24 * to force writeback.. the throttling in prepare_write and kupdate's usual
25 * writeback pressure got rid of our thread, but the file name remains.
28 #include <linux/version.h>
29 #include <linux/config.h>
30 #include <linux/module.h>
32 #include <linux/stat.h>
33 #include <linux/sched.h>
34 #include <linux/smp_lock.h>
35 #include <linux/kmod.h>
36 #include <linux/pagemap.h>
38 #include <linux/rbtree.h>
39 #include <linux/seq_file.h>
40 #include <linux/time.h>
42 /* PG_inactive_clean is shorthand for rmap, we want free_high/low here.. */
43 #ifdef PG_inactive_clean
44 #include <linux/mm_inline.h>
47 #define DEBUG_SUBSYSTEM S_LLITE
48 #include <linux/lustre_lite.h>
50 #ifndef list_for_each_prev_safe
51 #define list_for_each_prev_safe(pos, n, head) \
52 for (pos = (head)->prev, n = pos->prev; pos != (head); \
53 pos = n, n = pos->prev )
56 extern spinlock_t inode_lock;
58 struct ll_writeback_pages {
64 * check to see if we're racing with truncate and put the page in
65 * the brw_page array. returns 0 if there is more room and 1
66 * if the array is full.
68 static int llwp_consume_page(struct ll_writeback_pages *llwp,
69 struct inode *inode, struct page *page)
71 obd_off off = ((obd_off)page->index) << PAGE_SHIFT;
74 /* we raced with truncate? */
75 if ( off >= inode->i_size ) {
76 ll_remove_dirty(inode, page->index, page->index);
82 pg = &llwp->pga[llwp->npgs];
84 LASSERT(llwp->npgs <= llwp->max);
88 pg->flag = OBD_BRW_CREATE;
89 pg->count = PAGE_CACHE_SIZE;
91 /* catch partial writes for files that end mid-page */
92 if (pg->off + pg->count > inode->i_size)
93 pg->count = inode->i_size & ~PAGE_CACHE_MASK;
96 * matches ptlrpc_bulk_get assert that trickles down
97 * from a 0 page length going through niobuf and into
98 * the buffer regions being posted
100 LASSERT(pg->count >= 0);
102 CDEBUG(D_CACHE, "brw_page %p: off "LPU64" cnt %d, page %p: ind %ld"
103 " i_size: %llu\n", pg, pg->off, pg->count, page,
104 page->index, inode->i_size);
106 return llwp->npgs == llwp->max;
110 * returns the number of pages that it added to the pgs array
112 * this duplicates filemap_fdatasync and gives us an opportunity to grab lots
115 static void ll_get_dirty_pages(struct inode *inode,
116 struct ll_writeback_pages *llwp)
118 struct address_space *mapping = inode->i_mapping;
120 struct list_head *pos, *n;
123 PGCACHE_WRLOCK(mapping);
125 list_for_each_prev_safe(pos, n, &mapping->dirty_pages) {
126 page = list_entry(pos, struct page, list);
128 if (TryLockPage(page))
131 list_del(&page->list);
132 list_add(&page->list, &mapping->locked_pages);
134 if ( ! PageDirty(page) ) {
138 ClearPageDirty(page);
140 if ( llwp_consume_page(llwp, inode, page) != 0)
144 PGCACHE_WRUNLOCK(mapping);
148 static void ll_writeback(struct inode *inode, struct ll_writeback_pages *llwp)
151 struct ptlrpc_request_set *set;
154 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),bytes=%u\n",
155 inode->i_ino, inode->i_generation, inode,
156 ((llwp->npgs-1) << PAGE_SHIFT) + llwp->pga[llwp->npgs-1].count);
158 set = ptlrpc_prep_set();
160 CERROR ("Can't create request set\n");
163 rc = obd_brw_async(OBD_BRW_WRITE, ll_i2obdconn(inode),
164 ll_i2info(inode)->lli_smd, llwp->npgs,
165 llwp->pga, set, NULL);
167 rc = ptlrpc_set_wait (set);
168 ptlrpc_set_destroy (set);
171 * b=1038, we need to pass _brw errors up so that writeback
172 * doesn't get stuck in recovery leaving processes stuck in
173 * D waiting for pages
176 CERROR("error from obd_brw_async: rc = %d\n", rc);
177 INODE_IO_STAT_ADD(inode, wb_fail, llwp->npgs);
179 INODE_IO_STAT_ADD(inode, wb_ok, llwp->npgs);
182 for (i = 0 ; i < llwp->npgs ; i++) {
183 struct page *page = llwp->pga[i].pg;
185 CDEBUG(D_CACHE, "finished page %p at index %lu\n", page,
187 LASSERT(PageLocked(page));
188 ll_remove_dirty(inode, page->index, page->index);
190 page_cache_release(page);
196 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
198 #ifndef PG_inactive_clean
199 #ifdef CONFIG_DISCONTIGMEM
200 #error "sorry, we don't support DISCONTIGMEM yet"
204 * __alloc_pages marks a zone as needing balancing if an allocation is
205 * performed when the zone has fewer free pages than its 'low' water
206 * mark. its cleared when try_to_free_pages makes progress.
208 static int zones_need_balancing(void)
214 for ( pgdat = pgdat_list ; pgdat != NULL ; pgdat = pgdat->node_next ) {
215 for ( i = pgdat->nr_zones-1 ; i >= 0 ; i-- ) {
216 zone = &pgdat->node_zones[i];
218 if ( zone->need_balance )
225 /* 2.4 doesn't give us a way to find out how many pages we have
226 * cached 'cause we're not using buffer_heads. we are very
227 * conservative here and flush the superblock of all dirty data
228 * when the vm (rmap or stock) thinks that it is running low
229 * and kswapd would have done work. kupdated isn't good enough
230 * because writers (dbench) can dirty _very quickly_, and we
231 * allocate under writepage..
233 * 2.5 gets this right, see the {inc,dec}_page_state(nr_dirty, )
235 static int should_writeback(void)
237 #ifdef PG_inactive_clean
238 if (free_high(ALL_ZONES) > 0 || free_low(ANY_ZONE) > 0)
240 if (zones_need_balancing())
246 static int ll_alloc_brw(struct inode *inode, struct ll_writeback_pages *llwp)
248 memset(llwp, 0, sizeof(struct ll_writeback_pages));
250 llwp->max = inode->i_blksize >> PAGE_CACHE_SHIFT;
251 if (llwp->max == 0) {
252 CERROR("forcing llwp->max to 1. blksize: %lu\n",
256 llwp->pga = kmalloc(llwp->max * sizeof(*llwp->pga), GFP_ATOMIC);
257 if (llwp->pga == NULL)
262 int ll_check_dirty(struct super_block *sb)
264 unsigned long old_flags; /* hack? */
270 if (!should_writeback())
273 old_flags = current->flags;
274 current->flags |= PF_MEMALLOC;
276 spin_lock(&inode_lock);
279 * first we try and write back dirty pages from dirty inodes
280 * until the VM thinkgs we're ok again..
283 struct ll_writeback_pages llwp;
284 struct list_head *pos;
288 list_for_each_prev(pos, &sb->s_dirty) {
289 inode = list_entry(pos, struct inode, i_list);
291 if (!(inode->i_state & I_DIRTY_PAGES)) {
301 /* duplicate __sync_one, *sigh* */
302 list_del(&inode->i_list);
303 list_add(&inode->i_list, &inode->i_sb->s_locked_inodes);
304 inode->i_state |= I_LOCK;
305 inode->i_state &= ~I_DIRTY_PAGES;
307 spin_unlock(&inode_lock);
309 rc = ll_alloc_brw(inode, &llwp);
315 ll_get_dirty_pages(inode, &llwp);
317 INODE_IO_STAT_ADD(inode, wb_from_pressure,
319 ll_writeback(inode, &llwp);
323 } while (llwp.npgs && should_writeback());
325 spin_lock(&inode_lock);
327 if (!list_empty(&inode->i_mapping->dirty_pages))
328 inode->i_state |= I_DIRTY_PAGES;
330 inode->i_state &= ~I_LOCK;
332 * we are sneaky and leave the inode on the dirty list,
333 * even though it might not still be..
335 if (!(inode->i_state & I_FREEING)) {
336 list_del(&inode->i_list);
337 list_add(&inode->i_list, &inode->i_sb->s_dirty);
339 wake_up(&inode->i_wait);
341 } while (making_progress && should_writeback());
344 * and if that didn't work, we sleep on any data that might
345 * be under writeback..
347 while (should_writeback()) {
348 if (list_empty(&sb->s_locked_inodes))
351 inode = list_entry(sb->s_locked_inodes.next, struct inode,
354 atomic_inc(&inode->i_count); /* XXX hack? */
355 spin_unlock(&inode_lock);
356 wait_event(inode->i_wait, !(inode->i_state & I_LOCK));
358 spin_lock(&inode_lock);
361 spin_unlock(&inode_lock);
364 current->flags = old_flags;
368 #endif /* linux 2.5 */
370 int ll_batch_writepage(struct inode *inode, struct page *page)
372 unsigned long old_flags; /* hack? */
373 struct ll_writeback_pages llwp;
377 old_flags = current->flags;
378 current->flags |= PF_MEMALLOC;
379 rc = ll_alloc_brw(inode, &llwp);
383 if (llwp_consume_page(&llwp, inode, page) == 0)
384 ll_get_dirty_pages(inode, &llwp);
387 INODE_IO_STAT_ADD(inode, wb_from_writepage, llwp.npgs);
388 ll_writeback(inode, &llwp);
393 current->flags = old_flags;
398 * we aggressively track offsets of pages that have been dirtied. we need this
399 * to make file size decisions around lock acquisition and cancelation. all
400 * extents include the offsets at their endpoints.
402 struct offset_extent {
404 unsigned long oe_start, oe_end;
407 static struct offset_extent *ll_find_oe(rb_root_t *root,
408 struct offset_extent *needle)
410 struct rb_node_s *node = root->rb_node;
411 struct offset_extent *oe;
414 CDEBUG(D_INODE, "searching [%lu -> %lu]\n", needle->oe_start,
418 oe = rb_entry(node, struct offset_extent, oe_node);
419 if (needle->oe_end < oe->oe_start)
420 node = node->rb_left;
421 else if (needle->oe_start > oe->oe_end)
422 node = node->rb_right;
424 CDEBUG(D_INODE, "returning [%lu -> %lu]\n",
425 oe->oe_start, oe->oe_end);
432 /* do the rbtree mechanics to insert a node, callers are responsible
433 * for making sure that this new node doesn't overlap with existing
435 static void ll_insert_oe(rb_root_t *root, struct offset_extent *new_oe)
437 rb_node_t ** p = &root->rb_node;
438 rb_node_t * parent = NULL;
439 struct offset_extent *oe;
442 LASSERT(new_oe->oe_start <= new_oe->oe_end);
446 oe = rb_entry(parent, struct offset_extent, oe_node);
447 if ( new_oe->oe_end < oe->oe_start )
449 else if ( new_oe->oe_start > oe->oe_end )
454 rb_link_node(&new_oe->oe_node, parent, p);
455 rb_insert_color(&new_oe->oe_node, root);
459 static inline void lldo_dirty_add(struct inode *inode,
460 struct ll_dirty_offsets *lldo,
463 lldo->do_num_dirty += val;
464 INODE_IO_STAT_ADD(inode, dirty_pages, val);
467 void ll_record_dirty(struct inode *inode, unsigned long offset)
469 struct ll_dirty_offsets *lldo = &ll_i2info(inode)->lli_dirty;
470 struct offset_extent needle, *oe, *new_oe;
474 /* will allocate more intelligently later */
475 OBD_ALLOC(new_oe, sizeof(*new_oe));
476 LASSERT(new_oe); /* will have to do for now :/ */
478 spin_lock(&lldo->do_lock);
480 /* find neighbours that we might glom on to */
481 needle.oe_start = (offset > 0) ? offset - 1 : offset;
482 needle.oe_end = (offset < ~0) ? offset + 1 : offset;
483 oe = ll_find_oe(&lldo->do_root, &needle);
485 new_oe->oe_start = offset;
486 new_oe->oe_end = offset;
487 ll_insert_oe(&lldo->do_root, new_oe);
488 lldo_dirty_add(inode, lldo, 1);
493 /* already recorded */
494 if ( offset >= oe->oe_start && offset <= oe->oe_end )
497 /* ok, need to check for adjacent neighbours */
498 needle.oe_start = offset;
499 needle.oe_end = offset;
500 if (ll_find_oe(&lldo->do_root, &needle))
503 /* ok, its safe to extend the oe we found */
504 if ( offset == oe->oe_start - 1 )
506 else if ( offset == oe->oe_end + 1 )
510 lldo_dirty_add(inode, lldo, 1);
513 CDEBUG(D_INODE, "%lu now dirty\n", lldo->do_num_dirty);
514 spin_unlock(&lldo->do_lock);
516 OBD_FREE(new_oe, sizeof(*new_oe));
521 void ll_remove_dirty(struct inode *inode, unsigned long start,
524 struct ll_dirty_offsets *lldo = &ll_i2info(inode)->lli_dirty;
525 struct offset_extent needle, *oe, *new_oe;
528 /* will allocate more intelligently later */
529 OBD_ALLOC(new_oe, sizeof(*new_oe));
530 LASSERT(new_oe); /* will have to do for now :/ */
532 needle.oe_start = start;
535 spin_lock(&lldo->do_lock);
536 for ( ; (oe = ll_find_oe(&lldo->do_root, &needle)) ; ) {
538 /* see if we're punching a hole and need to create a node */
539 if (oe->oe_start < start && oe->oe_end > end) {
540 new_oe->oe_start = end + 1;
541 new_oe->oe_end = oe->oe_end;
542 oe->oe_end = start - 1;
543 ll_insert_oe(&lldo->do_root, new_oe);
545 lldo_dirty_add(inode, lldo, -(end - start + 1));
549 /* overlapping edges */
550 if (oe->oe_start < start && oe->oe_end <= end) {
551 lldo_dirty_add(inode, lldo, -(oe->oe_end - start + 1));
552 oe->oe_end = start - 1;
556 if (oe->oe_end > end && oe->oe_start >= start) {
557 lldo_dirty_add(inode, lldo, -(end - oe->oe_start + 1));
558 oe->oe_start = end + 1;
563 /* an extent entirely within the one we're clearing */
564 rb_erase(&oe->oe_node, &lldo->do_root);
565 lldo_dirty_add(inode, lldo, -(oe->oe_end - oe->oe_start + 1));
566 spin_unlock(&lldo->do_lock);
567 OBD_FREE(oe, sizeof(*oe));
568 spin_lock(&lldo->do_lock);
570 CDEBUG(D_INODE, "%lu now dirty\n", lldo->do_num_dirty);
571 spin_unlock(&lldo->do_lock);
573 OBD_FREE(new_oe, sizeof(*new_oe));
577 int ll_find_dirty(struct ll_dirty_offsets *lldo, unsigned long *start,
580 struct offset_extent needle, *oe;
584 needle.oe_start = *start;
585 needle.oe_end = *end;
587 spin_lock(&lldo->do_lock);
588 oe = ll_find_oe(&lldo->do_root, &needle);
590 *start = oe->oe_start;
594 spin_unlock(&lldo->do_lock);
599 int ll_farthest_dirty(struct ll_dirty_offsets *lldo, unsigned long *farthest)
601 struct rb_node_s *last, *node;
602 struct offset_extent *oe;
606 spin_lock(&lldo->do_lock);
607 for (node = lldo->do_root.rb_node, last = NULL;
609 last = node, node = node->rb_right)
613 oe = rb_entry(last, struct offset_extent, oe_node);
614 *farthest = oe->oe_end;
617 spin_unlock(&lldo->do_lock);
621 void ll_lldo_init(struct ll_dirty_offsets *lldo)
623 spin_lock_init(&lldo->do_lock);
624 lldo->do_num_dirty = 0;
625 lldo->do_root.rb_node = NULL;
628 /* seq file export of some page cache tracking stats */
629 static int ll_pgcache_seq_show(struct seq_file *seq, void *v)
632 struct ll_sb_info *sbi = seq->private;
633 do_gettimeofday(&now);
635 seq_printf(seq, "snapshot_time: %lu:%lu (secs:usecs)\n",
636 now.tv_sec, now.tv_usec);
637 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
638 seq_printf(seq, "VM_under_pressure: %s\n",
639 should_writeback() ? "yes" : "no");
641 seq_printf(seq, "dirty_pages: "LPU64"\n",
642 sbi->ll_iostats.fis_dirty_pages);
643 seq_printf(seq, "dirty_page_hits: "LPU64"\n",
644 sbi->ll_iostats.fis_dirty_hits);
645 seq_printf(seq, "dirty_page_misses: "LPU64"\n",
646 sbi->ll_iostats.fis_dirty_misses);
647 seq_printf(seq, "writeback_from_writepage: "LPU64"\n",
648 sbi->ll_iostats.fis_wb_from_writepage);
649 seq_printf(seq, "writeback_from_pressure: "LPU64"\n",
650 sbi->ll_iostats.fis_wb_from_pressure);
651 seq_printf(seq, "writeback_ok_pages: "LPU64"\n",
652 sbi->ll_iostats.fis_wb_ok);
653 seq_printf(seq, "writeback_failed_pages: "LPU64"\n",
654 sbi->ll_iostats.fis_wb_fail);
658 static void *ll_pgcache_seq_start(struct seq_file *p, loff_t *pos)
664 static void *ll_pgcache_seq_next(struct seq_file *p, void *v, loff_t *pos)
669 static void ll_pgcache_seq_stop(struct seq_file *p, void *v)
673 struct seq_operations ll_pgcache_seq_sops = {
674 .start = ll_pgcache_seq_start,
675 .stop = ll_pgcache_seq_stop,
676 .next = ll_pgcache_seq_next,
677 .show = ll_pgcache_seq_show,
680 static int ll_pgcache_seq_open(struct inode *inode, struct file *file)
682 struct proc_dir_entry *dp = inode->u.generic_ip;
683 struct seq_file *seq;
686 rc = seq_open(file, &ll_pgcache_seq_sops);
689 seq = file->private_data;
690 seq->private = dp->data;
694 struct file_operations ll_pgcache_seq_fops = {
695 .open = ll_pgcache_seq_open,
698 .release = seq_release,