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