Whamcloud - gitweb
file jbd-2.4.19-pre1-jcberr.patch was initially added on branch b_devel.
[fs/lustre-release.git] / lustre / llite / iod.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  *  Copyright (C) 2002, 2003  Cluster File Systems, Inc
22  *
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.
26  */
27
28 #include <linux/version.h>
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/fs.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>
37 #include <linux/mm.h>
38 #include <linux/rbtree.h>
39 #include <linux/seq_file.h>
40 #include <linux/time.h>
41
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>
45 #endif
46
47 #define DEBUG_SUBSYSTEM S_LLITE
48 #include <linux/lustre_lite.h>
49 #include "llite_internal.h"
50
51 #ifndef list_for_each_prev_safe
52 #define list_for_each_prev_safe(pos, n, head) \
53         for (pos = (head)->prev, n = pos->prev; pos != (head); \
54                 pos = n, n = pos->prev )
55 #endif
56
57 extern spinlock_t inode_lock;
58
59 /*
60  * check to see if we're racing with truncate and put the page in
61  * the brw_page array.  returns 0 if there is more room and 1
62  * if the array is full.
63  */
64 static int llwp_consume_page(struct ll_writeback_pages *llwp,
65                              struct inode *inode, struct page *page)
66 {
67         obd_off off = ((obd_off)page->index) << PAGE_SHIFT;
68         struct brw_page *pg;
69
70         /* we raced with truncate? */
71         if ( off >= inode->i_size ) {
72                 int rc;
73                 rc = ll_clear_dirty_pages(ll_i2obdconn(inode),
74                                           ll_i2info(inode)->lli_smd,
75                                           page->index, page->index);
76
77                 LASSERT(rc == 0);
78                 CDEBUG(D_CACHE, "offset "LPU64" (index %lu) > i_size %llu\n",
79                        off, page->index, inode->i_size);
80                 unlock_page(page);
81                 return 0;
82         }
83
84         page_cache_get(page);
85         pg = &llwp->pga[llwp->npgs];
86         llwp->npgs++;
87         LASSERT(llwp->npgs <= llwp->max);
88
89         pg->pg = page;
90         pg->off = off;
91         pg->flag = OBD_BRW_CREATE|OBD_BRW_FROM_GRANT;
92         pg->count = PAGE_CACHE_SIZE;
93
94         /* catch partial writes for files that end mid-page */
95         if (pg->off + pg->count > inode->i_size)
96                 pg->count = inode->i_size & ~PAGE_CACHE_MASK;
97
98         /*
99          * matches ptlrpc_bulk_get assert that trickles down
100          * from a 0 page length going through niobuf and into
101          * the buffer regions being posted
102          */
103         LASSERT(pg->count >= 0);
104
105         CDEBUG(D_CACHE, "brw_page %p: off "LPU64" cnt %d, page %p: ind %ld"
106                         " i_size: %llu\n", pg, pg->off, pg->count, page,
107                         page->index, inode->i_size);
108
109         return llwp->npgs == llwp->max;
110 }
111
112 /*
113  * returns the number of pages that it added to the pgs array
114  *
115  * this duplicates filemap_fdatasync and gives us an opportunity to grab lots
116  * of dirty pages..
117  */
118 static void ll_get_dirty_pages(struct inode *inode,
119                                struct ll_writeback_pages *llwp)
120 {
121         struct address_space *mapping = inode->i_mapping;
122         struct page *page;
123         struct list_head *pos, *n;
124         ENTRY;
125
126         PGCACHE_WRLOCK(mapping);
127
128         list_for_each_prev_safe(pos, n, &mapping->dirty_pages) {
129                 page = list_entry(pos, struct page, list);
130
131                 if (TryLockPage(page))
132                         continue;
133
134                 list_del(&page->list);
135                 list_add(&page->list, &mapping->locked_pages);
136
137                 if (!PageDirty(page)) {
138                         unlock_page(page);
139                         continue;
140                 }
141                 ClearPageDirty(page);
142
143                 if (llwp_consume_page(llwp, inode, page) != 0)
144                         break;
145         }
146
147         PGCACHE_WRUNLOCK(mapping);
148         EXIT;
149 }
150
151 static void ll_writeback(struct inode *inode, struct obdo *oa,
152                          struct ll_writeback_pages *llwp)
153 {
154         struct ptlrpc_request_set *set;
155         int rc, i;
156         ENTRY;
157
158         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),bytes=%u\n",
159                inode->i_ino, inode->i_generation, inode,
160                ((llwp->npgs-1) << PAGE_SHIFT) + llwp->pga[llwp->npgs-1].count);
161
162         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
163         set = ptlrpc_prep_set();
164         if (set == NULL) {
165                 CERROR ("Can't create request set\n");
166                 rc = -ENOMEM;
167         } else {
168                 rc = obd_brw_async(OBD_BRW_WRITE, ll_i2obdconn(inode), oa,
169                                    ll_i2info(inode)->lli_smd, llwp->npgs,
170                                    llwp->pga, set, NULL);
171                 if (rc == 0)
172                         rc = ptlrpc_set_wait(set);
173                 if (rc == 0) {
174                         /* bug 1598: don't clobber blksize */
175                         oa->o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
176                         obdo_refresh_inode(inode, oa, oa->o_valid);
177                 }
178                 ptlrpc_set_destroy (set);
179         }
180         /*
181          * b=1038, we need to pass _brw errors up so that writeback
182          * doesn't get stuck in recovery leaving processes stuck in
183          * D waiting for pages
184          */
185         if (rc) {
186                 CERROR("error from obd_brw_async: rc = %d\n", rc);
187                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
188                                     LPROC_LL_WB_FAIL, llwp->npgs);
189         } else {
190                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
191                                     LPROC_LL_WB_OK, (llwp->npgs));
192         }
193
194         for (i = 0 ; i < llwp->npgs ; i++) {
195                 struct page *page = llwp->pga[i].pg;
196
197                 CDEBUG(D_CACHE, "finished page %p at index %lu\n", page,
198                        page->index);
199                 LASSERT(PageLocked(page));
200
201                 rc = ll_clear_dirty_pages(ll_i2obdconn(inode),
202                                           ll_i2info(inode)->lli_smd,
203                                           page->index, page->index);
204                 LASSERT(rc == 0);
205                 unlock_page(page);
206                 page_cache_release(page);
207         }
208
209         EXIT;
210 }
211
212 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
213
214 #ifndef PG_inactive_clean
215 #ifdef CONFIG_DISCONTIGMEM
216 #error "sorry, we don't support DISCONTIGMEM yet"
217 #endif
218
219 /*
220  * __alloc_pages marks a zone as needing balancing if an allocation is
221  * performed when the zone has fewer free pages than its 'low' water
222  * mark.  its cleared when try_to_free_pages makes progress.
223  */
224 static int zones_need_balancing(void)
225 {
226         pg_data_t * pgdat;
227         zone_t *zone;
228         int i;
229
230         for ( pgdat = pgdat_list ; pgdat != NULL ; pgdat = pgdat->node_next ) {
231                 for ( i = pgdat->nr_zones-1 ; i >= 0 ; i-- ) {
232                         zone = &pgdat->node_zones[i];
233
234                         if ( zone->need_balance )
235                                 return 1;
236                 }
237         }
238         return 0;
239 }
240 #endif
241 /* 2.4 doesn't give us a way to find out how many pages we have
242  * cached 'cause we're not using buffer_heads.  we are very
243  * conservative here and flush the superblock of all dirty data
244  * when the vm (rmap or stock) thinks that it is running low
245  * and kswapd would have done work.  kupdated isn't good enough
246  * because writers (dbench) can dirty _very quickly_, and we
247  * allocate under writepage..
248  *
249  * 2.5 gets this right, see the {inc,dec}_page_state(nr_dirty, )
250  */
251 static int should_writeback(void)
252 {
253 #ifdef PG_inactive_clean
254         if (free_high(ALL_ZONES) > 0 || free_low(ANY_ZONE) > 0)
255 #else
256         if (zones_need_balancing())
257 #endif
258                 return 1;
259         return 0;
260 }
261
262 static int ll_alloc_brw(struct inode *inode, struct ll_writeback_pages *llwp)
263 {
264         memset(llwp, 0, sizeof(struct ll_writeback_pages));
265
266         llwp->max = inode->i_blksize >> PAGE_CACHE_SHIFT;
267         if (llwp->max == 0) {
268                 CERROR("forcing llwp->max to 1.  blksize: %lu\n",
269                        inode->i_blksize);
270                 llwp->max = 1;
271         }
272         llwp->pga = kmalloc(llwp->max * sizeof(*llwp->pga), GFP_ATOMIC);
273         if (llwp->pga == NULL)
274                 RETURN(-ENOMEM);
275         RETURN(0);
276 }
277
278 int ll_check_dirty(struct super_block *sb)
279 {
280         unsigned long old_flags; /* hack? */
281         int making_progress;
282         struct inode *inode;
283         struct obdo oa;
284         int rc = 0;
285         ENTRY;
286
287         if (!should_writeback())
288                 return 0;
289
290         old_flags = current->flags;
291         current->flags |= PF_MEMALLOC;
292
293         spin_lock(&inode_lock);
294
295         /*
296          * first we try and write back dirty pages from dirty inodes
297          * until the VM thinkgs we're ok again..
298          */
299         do {
300                 struct ll_writeback_pages llwp;
301                 struct list_head *pos;
302                 inode = NULL;
303                 making_progress = 0;
304
305                 list_for_each_prev(pos, &sb->s_dirty) {
306                         inode = list_entry(pos, struct inode, i_list);
307
308                         if (!(inode->i_state & I_DIRTY_PAGES)) {
309                                 inode = NULL;
310                                 continue;
311                         }
312                         break;
313                 }
314
315                 if (inode == NULL)
316                         break;
317
318                 /* duplicate __sync_one, *sigh* */
319                 list_del(&inode->i_list);
320                 list_add(&inode->i_list, &inode->i_sb->s_locked_inodes);
321                 inode->i_state |= I_LOCK;
322                 inode->i_state &= ~I_DIRTY_PAGES;
323
324                 spin_unlock(&inode_lock);
325
326                 rc = ll_alloc_brw(inode, &llwp);
327                 if (rc != 0)
328                         GOTO(cleanup, rc);
329
330                 do {
331                         llwp.npgs = 0;
332                         ll_get_dirty_pages(inode, &llwp);
333                         if (llwp.npgs) {
334                                 oa.o_id =
335                                       ll_i2info(inode)->lli_smd->lsm_object_id;
336                                 oa.o_valid = OBD_MD_FLID;
337                                 obdo_from_inode(&oa, inode,
338                                                 OBD_MD_FLTYPE | OBD_MD_FLATIME|
339                                                 OBD_MD_FLMTIME| OBD_MD_FLCTIME);
340                                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
341                                                     LPROC_LL_WB_PRESSURE,
342                                                     llwp.npgs);
343                                 ll_writeback(inode, &oa, &llwp);
344                                 rc += llwp.npgs;
345                                 making_progress = 1;
346                         }
347                 } while (llwp.npgs && should_writeback());
348
349                 spin_lock(&inode_lock);
350
351                 if (!list_empty(&inode->i_mapping->dirty_pages))
352                         inode->i_state |= I_DIRTY_PAGES;
353
354                 inode->i_state &= ~I_LOCK;
355                 /*
356                  * we are sneaky and leave the inode on the dirty list,
357                  * even though it might not still be..
358                  */
359                 if (!(inode->i_state & I_FREEING)) {
360                         list_del(&inode->i_list);
361                         list_add(&inode->i_list, &inode->i_sb->s_dirty);
362                 }
363                 wake_up(&inode->i_wait);
364                 kfree(llwp.pga);
365         } while (making_progress && should_writeback());
366
367         /*
368          * and if that didn't work, we sleep on any data that might
369          * be under writeback..
370          */
371         while (should_writeback()) {
372                 if (list_empty(&sb->s_locked_inodes))
373                         break;
374
375                 inode = list_entry(sb->s_locked_inodes.next, struct inode,
376                                    i_list);
377
378                 atomic_inc(&inode->i_count); /* XXX hack? */
379                 spin_unlock(&inode_lock);
380                 wait_event(inode->i_wait, !(inode->i_state & I_LOCK));
381                 iput(inode);
382                 spin_lock(&inode_lock);
383         }
384
385         spin_unlock(&inode_lock);
386
387 cleanup:
388         current->flags = old_flags;
389
390         RETURN(rc);
391 }
392 #endif /* linux 2.5 */
393
394 int ll_batch_writepage(struct inode *inode, struct obdo *oa, struct page *page)
395 {
396         unsigned long old_flags; /* hack? */
397         struct ll_writeback_pages llwp;
398         int rc = 0;
399         ENTRY;
400
401         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
402         old_flags = current->flags;
403         current->flags |= PF_MEMALLOC;
404         rc = ll_alloc_brw(inode, &llwp);
405         if (rc != 0)
406                 GOTO(restore_flags, rc);
407
408         if (llwp_consume_page(&llwp, inode, page) == 0)
409                 ll_get_dirty_pages(inode, &llwp);
410
411         if (llwp.npgs) {
412                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
413                                     LPROC_LL_WB_WRITEPAGE, llwp.npgs);
414                 ll_writeback(inode, oa, &llwp);
415         }
416         kfree(llwp.pga);
417
418 restore_flags:
419         current->flags = old_flags;
420         RETURN(rc);
421 }