Whamcloud - gitweb
52e1437c381e8e74f92cccea57a31af1e2ab8e48
[fs/lustre-release.git] / lustre / llite / rw.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Lite I/O page cache routines shared by different kernel revs
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/config.h>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/errno.h>
30 #include <linux/smp_lock.h>
31 #include <linux/unistd.h>
32 #include <linux/version.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35
36 #include <linux/fs.h>
37 #include <linux/stat.h>
38 #include <asm/uaccess.h>
39 #include <asm/segment.h>
40 #include <linux/mm.h>
41 #include <linux/pagemap.h>
42 #include <linux/smp_lock.h>
43
44 #define DEBUG_SUBSYSTEM S_LLITE
45
46 #include <linux/lustre_mds.h>
47 #include <linux/lustre_lite.h>
48 #include "llite_internal.h"
49 #include <linux/lustre_compat25.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 /* SYNCHRONOUS I/O to object storage for an inode */
58 static int ll_brw(int cmd, struct inode *inode, struct obdo *oa, 
59                   struct page *page, int flags)
60 {
61         struct ll_inode_info *lli = ll_i2info(inode);
62         struct lov_stripe_md *lsm = lli->lli_smd;
63         struct brw_page pg;
64         int rc;
65         ENTRY;
66
67         pg.pg = page;
68         pg.off = ((obd_off)page->index) << PAGE_SHIFT;
69
70         if (cmd == OBD_BRW_WRITE && (pg.off + PAGE_SIZE > inode->i_size))
71                 pg.count = inode->i_size % PAGE_SIZE;
72         else
73                 pg.count = PAGE_SIZE;
74
75         CDEBUG(D_PAGE, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
76                cmd & OBD_BRW_WRITE ? "write" : "read", pg.count, inode->i_ino,
77                pg.off, pg.off);
78         if (pg.count == 0) {
79                 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
80                        LPU64"\n",
81                        inode->i_ino, inode, inode->i_size, page->mapping->host,
82                        page->mapping->host->i_size, page->index, pg.off);
83         }
84
85         pg.flag = flags;
86
87         if (cmd == OBD_BRW_WRITE)
88                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
89                                     LPROC_LL_BRW_WRITE, pg.count);
90         else
91                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
92                                     LPROC_LL_BRW_READ, pg.count);
93         rc = obd_brw(cmd, ll_i2obdexp(inode), oa, lsm, 1, &pg, NULL);
94         if (rc == 0)
95                 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
96         else if (rc != -EIO)
97                 CERROR("error from obd_brw: rc = %d\n", rc);
98         RETURN(rc);
99 }
100
101 /* this isn't where truncate starts.   roughly:
102  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
103  * we grab the lock back in setattr_raw to avoid races. */
104 void ll_truncate(struct inode *inode)
105 {
106         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
107         struct obdo oa;
108         int rc;
109         ENTRY;
110         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
111                inode->i_generation, inode);
112
113         if (!lsm) {
114                 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
115                        inode->i_ino);
116                 EXIT;
117                 return;
118         }
119
120         oa.o_id = lsm->lsm_object_id;
121         oa.o_valid = OBD_MD_FLID;
122         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
123                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
124
125         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
126                oa.o_id, inode->i_size);
127
128         /* truncate == punch from new size to absolute end of file */
129         rc = obd_punch(ll_i2obdexp(inode), &oa, lsm, inode->i_size,
130                        OBD_OBJECT_EOF, NULL);
131         if (rc)
132                 CERROR("obd_truncate fails (%d) ino %lu\n", rc, inode->i_ino);
133         else
134                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
135                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
136                                           OBD_MD_FLCTIME);
137
138         EXIT;
139         return;
140 } /* ll_truncate */
141
142 int ll_prepare_write(struct file *file, struct page *page, unsigned from,
143                      unsigned to)
144 {
145         struct inode *inode = page->mapping->host;
146         struct ll_inode_info *lli = ll_i2info(inode);
147         struct lov_stripe_md *lsm = lli->lli_smd;
148         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
149         struct brw_page pga;
150         struct obdo oa;
151         int rc = 0;
152         ENTRY;
153
154         if (!PageLocked(page))
155                 LBUG();
156
157         /* Check to see if we should return -EIO right away */
158         pga.pg = page;
159         pga.off = offset;
160         pga.count = PAGE_SIZE;
161         pga.flag = 0;
162
163         oa.o_id = lsm->lsm_object_id;
164         oa.o_mode = inode->i_mode;
165         oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
166
167         rc = obd_brw(OBD_BRW_CHECK, ll_i2obdexp(inode), &oa, lsm, 1, &pga,
168                      NULL);
169         if (rc)
170                 RETURN(rc);
171
172         if (PageUptodate(page))
173                 RETURN(0);
174
175         /* We're completely overwriting an existing page, so _don't_ set it up
176          * to date until commit_write */
177         if (from == 0 && to == PAGE_SIZE) {
178                 POISON_PAGE(page, 0x11);
179                 RETURN(0);
180         }
181
182         /* If are writing to a new page, no need to read old data.
183          * the extent locking and getattr procedures in ll_file_write have
184          * guaranteed that i_size is stable enough for our zeroing needs */
185         if (inode->i_size <= offset) {
186                 memset(kmap(page), 0, PAGE_SIZE);
187                 kunmap(page);
188                 GOTO(prepare_done, rc = 0);
189         }
190
191         /* XXX could be an async ocp read.. read-ahead? */
192         rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
193         if (rc == 0) {
194                 /* bug 1598: don't clobber blksize */
195                 oa.o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
196                 obdo_refresh_inode(inode, &oa, oa.o_valid);
197         }
198
199         EXIT;
200  prepare_done:
201         if (rc == 0)
202                 SetPageUptodate(page);
203
204         return rc;
205 }
206
207 int ll_write_count(struct page *page)
208 {
209         struct inode *inode = page->mapping->host;
210
211         /* catch race with truncate */
212         if (((loff_t)page->index << PAGE_SHIFT) >= inode->i_size)
213                 return 0;
214
215         /* catch sub-page write at end of file */
216         if (((loff_t)page->index << PAGE_SHIFT) + PAGE_SIZE > inode->i_size)
217                 return inode->i_size % PAGE_SIZE;
218
219         return PAGE_SIZE;
220 }
221
222 struct ll_async_page *llap_from_cookie(void *cookie)
223 {
224         struct ll_async_page *llap = cookie;
225         if (llap->llap_magic != LLAP_MAGIC)
226                 return ERR_PTR(-EINVAL);
227         return llap;
228 };
229
230 static int ll_ap_make_ready(void *data, int cmd)
231 {
232         struct ll_async_page *llap;
233         struct page *page;
234         ENTRY;
235
236         llap = llap_from_cookie(data);
237         if (IS_ERR(llap))
238                 RETURN(-EINVAL);
239
240         page = llap->llap_page;
241
242         if (cmd == OBD_BRW_READ) {
243                 /* _sync_page beat us to it and is about to call
244                  * _set_async_flags which will fire off rpcs again */
245                 if (!test_and_clear_bit(LL_PRIVBITS_READ, &page->private))
246                         RETURN(-EAGAIN);
247                 RETURN(0);
248         }
249
250         /* we're trying to write, but the page is locked.. come back later */
251         if (TryLockPage(page))
252                 RETURN(-EAGAIN);
253
254         LL_CDEBUG_PAGE(page, "made ready\n");
255         page_cache_get(page);
256
257         /* if we left PageDirty we might get another writepage call
258          * in the future.  list walkers are bright enough
259          * to check page dirty so we can leave it on whatever list
260          * its on.  XXX also, we're called with the cli list so if
261          * we got the page cache list we'd create a lock inversion
262          * with the removepage path which gets the page lock then the
263          * cli lock */
264         clear_page_dirty(page);
265         RETURN(0);
266 }
267
268 static int ll_ap_refresh_count(void *data, int cmd)
269 {
270         struct ll_async_page *llap;
271         ENTRY;
272
273         /* readpage queues with _COUNT_STABLE, shouldn't get here. */
274         LASSERT(cmd != OBD_BRW_READ);
275
276         llap = llap_from_cookie(data);
277         if (IS_ERR(llap))
278                 RETURN(PTR_ERR(llap));
279
280         return ll_write_count(llap->llap_page);
281 }
282
283 void ll_inode_fill_obdo(struct inode *inode, int cmd, struct obdo *oa)
284 {
285         struct lov_stripe_md *lsm;
286         obd_flag valid_flags;
287
288         lsm = ll_i2info(inode)->lli_smd;
289
290         oa->o_id = lsm->lsm_object_id;
291         oa->o_valid = OBD_MD_FLID;
292         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
293         if (cmd == OBD_BRW_WRITE) {
294                 oa->o_valid |= OBD_MD_FLIFID | OBD_MD_FLEPOCH;
295                 mdc_pack_fid(obdo_fid(oa), inode->i_ino, 0, inode->i_mode);
296                 oa->o_easize = ll_i2info(inode)->lli_io_epoch;
297
298                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
299         }
300
301         obdo_from_inode(oa, inode, valid_flags);
302 }
303
304 static void ll_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
305 {
306         struct ll_async_page *llap;
307         ENTRY;
308
309         llap = llap_from_cookie(data);
310         if (IS_ERR(llap)) {
311                 EXIT;
312                 return;
313         }
314
315         ll_inode_fill_obdo(llap->llap_page->mapping->host, cmd, oa);
316         EXIT;
317 }
318
319 static struct obd_async_page_ops ll_async_page_ops = {
320         .ap_make_ready =        ll_ap_make_ready,
321         .ap_refresh_count =     ll_ap_refresh_count,
322         .ap_fill_obdo =         ll_ap_fill_obdo,
323         .ap_completion =        ll_ap_completion,
324 };
325
326 #define page_llap(page) \
327         ((struct ll_async_page *)((page)->private & ~LL_PRIVBITS_MASK))
328
329 /* XXX have the exp be an argument? */
330 struct ll_async_page *llap_from_page(struct page *page)
331 {
332         struct ll_async_page *llap;
333         struct obd_export *exp;
334         struct inode *inode = page->mapping->host;
335         struct ll_sb_info *sbi = ll_i2sbi(inode);
336         int rc;
337         ENTRY;
338
339         llap = page_llap(page);
340         if (llap != NULL) {
341                 if (llap->llap_magic != LLAP_MAGIC)
342                         RETURN(ERR_PTR(-EINVAL));
343                 RETURN(llap);
344         } 
345
346         exp = ll_i2obdexp(page->mapping->host);
347         if (exp == NULL)
348                 RETURN(ERR_PTR(-EINVAL));
349
350         OBD_ALLOC(llap, sizeof(*llap));
351         if (llap == NULL)
352                 RETURN(ERR_PTR(-ENOMEM));
353         llap->llap_magic = LLAP_MAGIC;
354         rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd,
355                                  NULL, page, 
356                                  (obd_off)page->index << PAGE_SHIFT,
357                                  &ll_async_page_ops, llap, &llap->llap_cookie);
358         if (rc) {
359                 OBD_FREE(llap, sizeof(*llap));
360                 RETURN(ERR_PTR(rc));
361         }
362
363         CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap, 
364                page, llap->llap_cookie, (obd_off)page->index << PAGE_SHIFT);
365         /* also zeroing the PRIVBITS low order bitflags */ 
366         page->private = (unsigned long)llap;
367         llap->llap_page = page;
368
369         spin_lock(&sbi->ll_pglist_lock);
370         sbi->ll_pglist_gen++;
371         list_add_tail(&llap->llap_proc_item, &sbi->ll_pglist);
372         spin_unlock(&sbi->ll_pglist_lock);
373
374         RETURN(llap);
375 }
376
377 /* update our write count to account for i_size increases that may have
378  * happened since we've queued the page for io. */
379
380 /* be careful not to return success without setting the page Uptodate or
381  * the next pass through prepare_write will read in stale data from disk. */
382 int ll_commit_write(struct file *file, struct page *page, unsigned from,
383                     unsigned to)
384 {
385         struct inode *inode = page->mapping->host;
386         struct ll_inode_info *lli = ll_i2info(inode);
387         struct lov_stripe_md *lsm = lli->lli_smd;
388         struct obd_export *exp = NULL;
389         struct ll_async_page *llap;
390         loff_t size;
391         int rc = 0;
392         ENTRY;
393
394         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
395         LASSERT(inode == file->f_dentry->d_inode);
396         LASSERT(PageLocked(page));
397
398         CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
399                inode, page, from, to, page->index);
400
401         llap = llap_from_page(page);
402         if (IS_ERR(llap))
403                 RETURN(PTR_ERR(llap));
404
405         /* queue a write for some time in the future the first time we
406          * dirty the page */
407         if (!PageDirty(page)) {
408                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
409                                      LPROC_LL_DIRTY_MISSES);
410
411                 exp = ll_i2obdexp(inode);
412                 if (exp == NULL)
413                         RETURN(-EINVAL);
414
415                 /* _make_ready only sees llap once we've unlocked the page */
416                 llap->llap_write_queued = 1;
417                 rc = obd_queue_async_io(exp, lsm, NULL, llap->llap_cookie,
418                                         OBD_BRW_WRITE, 0, 0, 0, 0);
419                 if (rc != 0) { /* async failed, try sync.. */
420                         struct obd_sync_io_container *osic;
421                         osic_init(&osic);
422
423                         llap->llap_write_queued = 0;
424                         rc = obd_queue_sync_io(exp, lsm, NULL, osic,
425                                                llap->llap_cookie,
426                                                OBD_BRW_WRITE, 0, to, 0);
427                         if (rc)
428                                 GOTO(free_osic, rc);
429
430                         rc = obd_trigger_sync_io(exp, lsm, NULL, osic);
431                         if (rc)
432                                 GOTO(free_osic, rc);
433
434                         rc = osic_wait(osic);
435 free_osic:
436                         osic_release(osic);
437                         GOTO(out, rc);
438                 }
439                 LL_CDEBUG_PAGE(page, "write queued\n");
440                 //llap_write_pending(inode, llap);
441         } else {
442                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
443                                      LPROC_LL_DIRTY_HITS);
444         }
445
446         /* put the page in the page cache, from now on ll_removepage is 
447          * responsible for cleaning up the llap */
448         set_page_dirty(page);
449
450 out:
451         if (rc == 0) {
452                 /* XXX needs to be pushed down to the OSC as EOC */
453                 size = (((obd_off)page->index) << PAGE_SHIFT) + to;
454                 if (size > inode->i_size) {
455                         inode->i_size = size;
456                         /* see commentary in file.c:ll_inode_getattr() */
457                         set_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
458                 }
459                 SetPageUptodate(page);
460         }
461         RETURN(rc);
462 }
463
464 /* the kernel calls us here when a page is unhashed from the page cache.
465  * the page will be locked and the kernel is holding a spinlock, so
466  * we need to be careful.  we're just tearing down our book-keeping
467  * here. */
468 void ll_removepage(struct page *page)
469 {
470         struct inode *inode = page->mapping->host;
471         struct obd_export *exp;
472         struct ll_async_page *llap;
473         struct ll_sb_info *sbi = ll_i2sbi(inode);
474         int rc;
475         ENTRY;
476
477         LASSERT(!in_interrupt());
478
479         /* sync pages or failed read pages can leave pages in the page
480          * cache that don't have our data associated with them anymore */
481         if (page->private == 0) {
482                 EXIT;
483                 return;
484         }
485
486         LL_CDEBUG_PAGE(page, "being evicted\n");
487
488         exp = ll_i2obdexp(inode);
489         if (exp == NULL) {
490                 CERROR("page %p ind %lu gave null export\n", page, 
491                        page->index);
492                 EXIT;
493                 return;
494         }
495
496         llap = llap_from_page(page);
497         if (IS_ERR(llap)) {
498                 CERROR("page %p ind %lu couldn't find llap: %ld\n", page, 
499                        page->index, PTR_ERR(llap));
500                 EXIT;
501                 return;
502         }
503
504         //llap_write_complete(inode, llap);
505         rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL, 
506                                      llap->llap_cookie);
507         if (rc != 0)
508                 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
509
510         /* this unconditional free is only safe because the page lock
511          * is providing exclusivity to memory pressure/truncate/writeback..*/
512         page->private = 0;
513
514         spin_lock(&sbi->ll_pglist_lock);
515         if (!list_empty(&llap->llap_proc_item))
516                 list_del_init(&llap->llap_proc_item);
517         sbi->ll_pglist_gen++;
518         spin_unlock(&sbi->ll_pglist_lock);
519         OBD_FREE(llap, sizeof(*llap));
520         EXIT;
521 }
522
523 static int ll_page_matches(struct page *page)
524 {
525         struct lustre_handle match_lockh = {0};
526         struct inode *inode = page->mapping->host;
527         struct ldlm_extent page_extent;
528         int flags, matches;
529         ENTRY;
530
531         page_extent.start = (__u64)page->index << PAGE_CACHE_SHIFT;
532         page_extent.end = page_extent.start + PAGE_CACHE_SIZE - 1;
533         flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
534         matches = obd_match(ll_i2sbi(inode)->ll_osc_exp, 
535                             ll_i2info(inode)->lli_smd, LDLM_EXTENT, 
536                             &page_extent, sizeof(page_extent), 
537                             LCK_PR, &flags, inode, &match_lockh);
538         if (matches < 0) {
539                 LL_CDEBUG_PAGE(page, "lock match failed\n");
540                 RETURN(matches);
541         } 
542         if (matches) {
543                 obd_cancel(ll_i2sbi(inode)->ll_osc_exp, 
544                            ll_i2info(inode)->lli_smd, LCK_PR, &match_lockh);
545         }
546         RETURN(matches);
547 }
548
549 static int ll_issue_page_read(struct obd_export *exp,
550                               struct ll_async_page *llap, int defer_uptodate)
551 {
552         struct page *page = llap->llap_page;
553         int rc;
554
555         /* we don't issue this page as URGENT so that it can be batched
556          * with other pages by the kernel's read-ahead.  We have a strong
557          * requirement that readpage() callers must call wait_on_page()
558          * or lock_page() to get into ->sync_page() to trigger the IO */
559         llap->llap_defer_uptodate = defer_uptodate;
560         page_cache_get(page);
561         set_bit(LL_PRIVBITS_READ, &page->private); /* see ll_sync_page() */
562         rc = obd_queue_async_io(exp, ll_i2info(page->mapping->host)->lli_smd,
563                                 NULL, llap->llap_cookie, OBD_BRW_READ, 0,
564                                 PAGE_SIZE, 0, ASYNC_COUNT_STABLE);
565         if (rc) {
566                 LL_CDEBUG_PAGE(page, "read queueing failed\n");
567                 clear_bit(LL_PRIVBITS_READ, &page->private);
568                 page_cache_release(page);
569         }
570         RETURN(rc);
571 }
572
573 static void ll_readahead(struct ll_readahead_state *ras, 
574                          struct obd_export *exp, struct address_space *mapping)
575 {
576         unsigned long i, start, end;
577         struct ll_async_page *llap;
578         struct page *page;
579         int rc;
580
581         if (mapping->host->i_size == 0)
582                 return;
583
584         spin_lock(&ras->ras_lock);
585
586         /* make sure to issue a window's worth of read-ahead pages */
587         end = ras->ras_last;
588         start = end - ras->ras_window;
589         if (start > end)
590                 start = 0;
591
592         /* but don't iterate over pages that we've already issued.  this
593          * will set start to end + 1 if we've already read-ahead up to
594          * ras_last sothe for() won't be entered */
595         if (ras->ras_next_index > start)
596                 start = ras->ras_next_index;
597         if (end != ~0UL)
598                 ras->ras_next_index = end + 1;
599
600         CDEBUG(D_READA, "ni %lu last %lu win %lu: reading from %lu to %lu\n",
601                ras->ras_next_index, ras->ras_last, ras->ras_window,
602                start, end); 
603
604         spin_unlock(&ras->ras_lock);
605
606         /* clamp to filesize */
607         i = (mapping->host->i_size - 1) >> PAGE_CACHE_SHIFT;
608         end = min(end, i);
609
610         for (i = start; i <= end; i++) {
611                 /* grab_cache_page_nowait returns null if this races with
612                  * truncating the page (page->mapping == NULL) */
613                 page = grab_cache_page_nowait(mapping, i);
614                 if (page == NULL)
615                        continue;
616   
617                 /* the book-keeping above promises that we've tried
618                  * all the indices from start to end, so we don't
619                  * stop if anyone returns an error. This may not be good. */
620                 if (Page_Uptodate(page) || ll_page_matches(page) <= 0)
621                         goto next_page;
622
623                 llap = llap_from_page(page);
624                 if (IS_ERR(llap) || llap->llap_defer_uptodate)
625                         goto next_page;
626
627                 rc = ll_issue_page_read(exp, llap, 1);
628                 if (rc == 0)
629                         LL_CDEBUG_PAGE(page, "started read-ahead\n");
630                 if (rc) {
631         next_page:
632                         LL_CDEBUG_PAGE(page, "skipping read-ahead\n");
633
634                         unlock_page(page);
635                 }
636                 page_cache_release(page);
637         }
638 }
639
640 /* XXX this should really bubble up somehow.  */
641 #define LL_RA_MIN ((unsigned long)PTL_MD_MAX_PAGES / 2)
642 #define LL_RA_MAX ((unsigned long)(32 * PTL_MD_MAX_PAGES))
643
644 /* called with the ras_lock held or from places where it doesn't matter */
645 static void ll_readahead_set(struct ll_readahead_state *ras, 
646                              unsigned long index)
647 {
648         ras->ras_next_index = index;
649         if (ras->ras_next_index != ~0UL)
650                 ras->ras_next_index++;
651         ras->ras_window = LL_RA_MIN;
652         ras->ras_last = ras->ras_next_index + ras->ras_window;
653         if (ras->ras_last < ras->ras_next_index)
654                 ras->ras_last = ~0UL;
655         CDEBUG(D_READA, "ni %lu last %lu win %lu: set %lu\n",
656                ras->ras_next_index, ras->ras_last, ras->ras_window,
657                index);
658 }
659
660 void ll_readahead_init(struct ll_readahead_state *ras)
661 {
662         spin_lock_init(&ras->ras_lock);
663         ll_readahead_set(ras, 0);
664 }
665
666 static void ll_readahead_update(struct ll_readahead_state *ras, 
667                                 unsigned long index, int hit)
668 {
669         unsigned long issued_start, new_last;
670
671         spin_lock(&ras->ras_lock);
672
673         /* we're interested in noticing the index's relation to the 
674          * previously issued read-ahead pages */
675         issued_start = ras->ras_next_index - ras->ras_window - 1;
676         if (issued_start > ras->ras_next_index)
677                 issued_start = 0;
678
679         CDEBUG(D_READA, "ni %lu last %lu win %lu: %s ind %lu start %lu\n", 
680                ras->ras_next_index, ras->ras_last, ras->ras_window,
681                hit ? "hit" : "miss", index, issued_start);
682         if (!hit && 
683             index == ras->ras_next_index && index == ras->ras_last + 1) {
684                 /* special case the kernel's read-ahead running into the
685                  * page just beyond our read-ahead window as an extension
686                  * of our read-ahead.  sigh.  wishing it was easier to
687                  * turn off 2.4's read-ahead. */
688                 ras->ras_window = min(LL_RA_MAX, ras->ras_window + 1);
689                 if (index != ~0UL)
690                         ras->ras_next_index = index + 1;
691                 ras->ras_last = index;
692         } else if (!hit && 
693                    (index > issued_start || ras->ras_next_index >= index)) {
694                 /* deal with a miss way out of the window.  we interpret
695                  * this as a seek and restart the window */
696                 ll_readahead_set(ras, index);
697
698         } else if (!hit && 
699                    issued_start <= index && index < ras->ras_next_index) {
700                 /* a miss inside the window?  surely its memory pressure
701                  * evicting our read pages before the app can see them.
702                  * we shrink the window aggressively */
703                 unsigned long old_window = ras->ras_window;
704
705                 ras->ras_window = max(ras->ras_window / 2, LL_RA_MIN);
706                 ras->ras_last -= old_window - ras->ras_window;
707                 if (ras->ras_next_index > ras->ras_last)
708                         ras->ras_next_index = ras->ras_last + 1;
709                 CDEBUG(D_READA, "ni %lu last %lu win %lu: miss inside\n",
710                        ras->ras_next_index, ras->ras_last, ras->ras_window);
711
712         } else if (hit && 
713                    issued_start <= index && index < ras->ras_next_index) {
714                 /* a hit inside the window.  grow the window by twice the 
715                  * number of pages that are satisified within the window.  */
716                 ras->ras_window = min(LL_RA_MAX, ras->ras_window + 2);
717
718                 /* we want the next readahead pass to issue a windows worth
719                  * beyond where the app currently is */
720                 new_last = index + ras->ras_window;
721                 if (new_last > ras->ras_last)
722                         ras->ras_last = new_last;
723
724                 CDEBUG(D_READA, "ni %lu last %lu win %lu: extended window/last\n",
725                        ras->ras_next_index, ras->ras_last, ras->ras_window);
726         }
727
728         spin_unlock(&ras->ras_lock);
729 }
730
731 /*
732  * for now we do our readpage the same on both 2.4 and 2.5.  The kernel's
733  * read-ahead assumes it is valid to issue readpage all the way up to
734  * i_size, but our dlm locks make that not the case.  We disable the
735  * kernel's read-ahead and do our own by walking ahead in the page cache
736  * checking for dlm lock coverage.  the main difference between 2.4 and
737  * 2.6 is how read-ahead gets batched and issued, but we're using our own,
738  * so they look the same.
739  */
740 int ll_readpage(struct file *filp, struct page *page)
741 {
742         struct ll_file_data *fd = filp->private_data;
743         struct inode *inode = page->mapping->host;
744         struct obd_export *exp;
745         int rc;
746         struct ll_async_page *llap;
747         ENTRY;
748
749         LASSERT(PageLocked(page));
750         LASSERT(!PageUptodate(page));
751         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset="LPX64"\n",
752                inode->i_ino, inode->i_generation, inode,
753                (((obd_off)page->index) << PAGE_SHIFT));
754         LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
755
756         exp = ll_i2obdexp(inode);
757         if (exp == NULL)
758                 GOTO(out, rc = -EINVAL);
759
760         llap = llap_from_page(page);
761         if (IS_ERR(llap))
762                 GOTO(out, rc = PTR_ERR(llap));
763
764         if (llap->llap_defer_uptodate) {
765                 ll_readahead_update(&fd->fd_ras, page->index, 1);
766                 LL_CDEBUG_PAGE(page, "marking uptodate from defer\n");
767                 SetPageUptodate(page);
768                 ll_readahead(&fd->fd_ras, exp, page->mapping);
769                 unlock_page(page);
770                 RETURN(0);
771         }
772
773         ll_readahead_update(&fd->fd_ras, page->index, 0);
774
775         rc = ll_page_matches(page);
776         if (rc < 0)
777                 GOTO(out, rc);
778
779         if (rc == 0) {
780                 static unsigned long next_print;
781                 CDEBUG(D_INODE, "didn't match a lock");
782                 if (time_after(jiffies, next_print)) {
783                         next_print = jiffies + 30 * HZ;
784                         CERROR("not covered by a lock (mmap?).  check debug "
785                                "logs.\n");
786                 }
787         }
788
789         rc = ll_issue_page_read(exp, llap, 0);
790         if (rc == 0) {
791                 LL_CDEBUG_PAGE(page, "queued readpage\n");
792                 if ((ll_i2sbi(inode)->ll_flags & LL_SBI_READAHEAD))
793                         ll_readahead(&fd->fd_ras, exp, page->mapping);
794         }
795 out:
796         if (rc) 
797                 unlock_page(page);
798         RETURN(rc);
799 }
800
801 /* this is for read pages.  we issue them as ready but not urgent.  when
802  * someone waits on them we fire them off, hopefully merged with adjacent
803  * reads that were queued by read-ahead.  */
804 int ll_sync_page(struct page *page)
805 {
806         struct obd_export *exp;
807         struct ll_async_page *llap;
808         int rc;
809         ENTRY;
810
811         /* we're using a low bit flag to signify that a queued read should
812          * be issued once someone goes to lock it.  it is also cleared
813          * as the page is built into an RPC */
814         if (!test_and_clear_bit(LL_PRIVBITS_READ, &page->private))
815                 RETURN(0);
816
817         /* careful to only deref page->mapping after checking the bit */
818         exp = ll_i2obdexp(page->mapping->host);
819         if (exp == NULL)
820                 RETURN(-EINVAL);
821   
822         llap = llap_from_page(page);
823         if (IS_ERR(llap))
824                 RETURN(PTR_ERR(llap));
825
826         LL_CDEBUG_PAGE(page, "setting ready|urgent\n");
827
828         rc = obd_set_async_flags(exp, ll_i2info(page->mapping->host)->lli_smd, 
829                                  NULL, llap->llap_cookie, 
830                                  ASYNC_READY|ASYNC_URGENT);
831         return rc;
832 }