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