Whamcloud - gitweb
Land b_smallfix onto HEAD (20040512_1806)
[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.disk_offset = pg.page_offset = ((obd_off)page->index) << PAGE_SHIFT;
69
70         if (cmd == OBD_BRW_WRITE &&
71             (pg.disk_offset + PAGE_SIZE > inode->i_size))
72                 pg.count = inode->i_size % PAGE_SIZE;
73         else
74                 pg.count = PAGE_SIZE;
75
76         CDEBUG(D_PAGE, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
77                cmd & OBD_BRW_WRITE ? "write" : "read", pg.count, inode->i_ino,
78                pg.disk_offset, pg.disk_offset);
79         if (pg.count == 0) {
80                 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
81                        LPU64"\n", inode->i_ino, inode, inode->i_size,
82                        page->mapping->host, page->mapping->host->i_size,
83                        page->index, pg.disk_offset);
84         }
85
86         pg.flag = flags;
87
88         if (cmd == OBD_BRW_WRITE)
89                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
90                                     LPROC_LL_BRW_WRITE, pg.count);
91         else
92                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
93                                     LPROC_LL_BRW_READ, pg.count);
94         rc = obd_brw(cmd, ll_i2obdexp(inode), oa, lsm, 1, &pg, NULL);
95         if (rc == 0)
96                 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
97         else if (rc != -EIO)
98                 CERROR("error from obd_brw: rc = %d\n", rc);
99         RETURN(rc);
100 }
101
102 /* this isn't where truncate starts.   roughly:
103  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
104  * we grab the lock back in setattr_raw to avoid races. */
105 void ll_truncate(struct inode *inode)
106 {
107         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
108         struct obdo oa;
109         int rc;
110         ENTRY;
111         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
112                inode->i_generation, inode);
113
114         if (!lsm) {
115                 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
116                        inode->i_ino);
117                 EXIT;
118                 return;
119         }
120
121         oa.o_id = lsm->lsm_object_id;
122         oa.o_valid = OBD_MD_FLID;
123         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
124                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
125
126         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
127                oa.o_id, inode->i_size);
128
129         /* truncate == punch from new size to absolute end of file */
130         /* NB: obd_punch must be called with i_sem held!  It updates the kms! */
131         rc = obd_punch(ll_i2obdexp(inode), &oa, lsm, inode->i_size,
132                        OBD_OBJECT_EOF, NULL);
133         if (rc)
134                 CERROR("obd_truncate fails (%d) ino %lu\n", rc, inode->i_ino);
135         else
136                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
137                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
138                                           OBD_MD_FLCTIME);
139
140         EXIT;
141         return;
142 } /* ll_truncate */
143
144 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
145 int ll_prepare_write(struct file *file, struct page *page, unsigned from,
146                      unsigned to)
147 {
148         struct inode *inode = page->mapping->host;
149         struct ll_inode_info *lli = ll_i2info(inode);
150         struct lov_stripe_md *lsm = lli->lli_smd;
151         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
152         struct brw_page pga;
153         struct obdo oa;
154         __u64 kms;
155         int rc = 0;
156         ENTRY;
157
158         LASSERT(PageLocked(page));
159         (void)llap_cast_private(page); /* assertion */
160
161         /* Check to see if we should return -EIO right away */
162         pga.pg = page;
163         pga.disk_offset = pga.page_offset = offset;
164         pga.count = PAGE_SIZE;
165         pga.flag = 0;
166
167         oa.o_id = lsm->lsm_object_id;
168         oa.o_mode = inode->i_mode;
169         oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
170
171         rc = obd_brw(OBD_BRW_CHECK, ll_i2obdexp(inode), &oa, lsm, 1, &pga,
172                      NULL);
173         if (rc)
174                 RETURN(rc);
175
176         if (PageUptodate(page))
177                 RETURN(0);
178
179         /* We're completely overwriting an existing page, so _don't_ set it up
180          * to date until commit_write */
181         if (from == 0 && to == PAGE_SIZE) {
182                 POISON_PAGE(page, 0x11);
183                 RETURN(0);
184         }
185
186         /* If are writing to a new page, no need to read old data.  The extent
187          * locking will have updated the KMS, and for our purposes here we can
188          * treat it like i_size. */
189         kms = lov_merge_size(lsm, 1);
190         if (kms <= offset) {
191                 memset(kmap(page), 0, PAGE_SIZE);
192                 kunmap(page);
193                 GOTO(prepare_done, rc = 0);
194         }
195
196         /* XXX could be an async ocp read.. read-ahead? */
197         rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
198         if (rc == 0) {
199                 /* bug 1598: don't clobber blksize */
200                 oa.o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
201                 obdo_refresh_inode(inode, &oa, oa.o_valid);
202         }
203
204         EXIT;
205  prepare_done:
206         if (rc == 0)
207                 SetPageUptodate(page);
208
209         return rc;
210 }
211
212 struct ll_async_page *llap_from_cookie(void *cookie)
213 {
214         struct ll_async_page *llap = cookie;
215         if (llap->llap_magic != LLAP_MAGIC)
216                 return ERR_PTR(-EINVAL);
217         return llap;
218 };
219
220 static int ll_ap_make_ready(void *data, int cmd)
221 {
222         struct ll_async_page *llap;
223         struct page *page;
224         ENTRY;
225
226         llap = llap_from_cookie(data);
227         if (IS_ERR(llap))
228                 RETURN(-EINVAL);
229
230         page = llap->llap_page;
231
232         LASSERT(cmd != OBD_BRW_READ);
233
234         /* we're trying to write, but the page is locked.. come back later */
235         if (TryLockPage(page))
236                 RETURN(-EAGAIN);
237
238         LL_CDEBUG_PAGE(D_PAGE, page, "made ready\n");
239         page_cache_get(page);
240
241         /* if we left PageDirty we might get another writepage call
242          * in the future.  list walkers are bright enough
243          * to check page dirty so we can leave it on whatever list
244          * its on.  XXX also, we're called with the cli list so if
245          * we got the page cache list we'd create a lock inversion
246          * with the removepage path which gets the page lock then the
247          * cli lock */
248         clear_page_dirty(page);
249         RETURN(0);
250 }
251
252 /* We have two reasons for giving llite the opportunity to change the 
253  * write length of a given queued page as it builds the RPC containing
254  * the page: 
255  *
256  * 1) Further extending writes may have landed in the page cache
257  *    since a partial write first queued this page requiring us
258  *    to write more from the page cache.
259  * 2) We might have raced with truncate and want to avoid performing
260  *    write RPCs that are just going to be thrown away by the 
261  *    truncate's punch on the storage targets.
262  *
263  * The kms serves these purposes as it is set at both truncate and extending
264  * writes.
265  */
266 static int ll_ap_refresh_count(void *data, int cmd)
267 {
268         struct ll_async_page *llap;
269         struct lov_stripe_md *lsm;
270         struct page *page;
271         __u64 kms;
272         ENTRY;
273
274         /* readpage queues with _COUNT_STABLE, shouldn't get here. */
275         LASSERT(cmd != OBD_BRW_READ);
276
277         llap = llap_from_cookie(data);
278         if (IS_ERR(llap))
279                 RETURN(PTR_ERR(llap));
280
281         page = llap->llap_page;
282         lsm = ll_i2info(page->mapping->host)->lli_smd;
283         kms = lov_merge_size(lsm, 1);
284
285         /* catch race with truncate */
286         if (((__u64)page->index << PAGE_SHIFT) >= kms)
287                 return 0;
288
289         /* catch sub-page write at end of file */
290         if (((__u64)page->index << PAGE_SHIFT) + PAGE_SIZE > kms)
291                 return kms % PAGE_SIZE;
292
293         return PAGE_SIZE;
294 }
295
296 void ll_inode_fill_obdo(struct inode *inode, int cmd, struct obdo *oa)
297 {
298         struct lov_stripe_md *lsm;
299         obd_flag valid_flags;
300
301         lsm = ll_i2info(inode)->lli_smd;
302
303         oa->o_id = lsm->lsm_object_id;
304         oa->o_valid = OBD_MD_FLID;
305         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
306         if (cmd == OBD_BRW_WRITE) {
307                 oa->o_valid |= OBD_MD_FLIFID | OBD_MD_FLEPOCH;
308                 mdc_pack_fid(obdo_fid(oa), inode->i_ino, 0, inode->i_mode);
309                 oa->o_easize = ll_i2info(inode)->lli_io_epoch;
310
311                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
312         }
313
314         obdo_from_inode(oa, inode, valid_flags);
315 }
316
317 static void ll_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
318 {
319         struct ll_async_page *llap;
320         ENTRY;
321
322         llap = llap_from_cookie(data);
323         if (IS_ERR(llap)) {
324                 EXIT;
325                 return;
326         }
327
328         ll_inode_fill_obdo(llap->llap_page->mapping->host, cmd, oa);
329         EXIT;
330 }
331
332 static struct obd_async_page_ops ll_async_page_ops = {
333         .ap_make_ready =        ll_ap_make_ready,
334         .ap_refresh_count =     ll_ap_refresh_count,
335         .ap_fill_obdo =         ll_ap_fill_obdo,
336         .ap_completion =        ll_ap_completion,
337 };
338
339 struct ll_async_page *llap_cast_private(struct page *page)
340 {
341         struct ll_async_page *llap = (struct ll_async_page *)page->private;
342
343         LASSERTF(llap == NULL || llap->llap_magic == LLAP_MAGIC,
344                  "page %p private %lu gave magic %d which != %d\n",
345                  page, page->private, llap->llap_magic, LLAP_MAGIC);
346
347         return llap;
348 }
349
350 /* XXX have the exp be an argument? */
351 struct ll_async_page *llap_from_page(struct page *page)
352 {
353         struct ll_async_page *llap;
354         struct obd_export *exp;
355         struct inode *inode = page->mapping->host;
356         struct ll_sb_info *sbi = ll_i2sbi(inode);
357         int rc;
358         ENTRY;
359
360         llap = llap_cast_private(page);
361         if (llap != NULL)
362                 RETURN(llap);
363
364         exp = ll_i2obdexp(page->mapping->host);
365         if (exp == NULL)
366                 RETURN(ERR_PTR(-EINVAL));
367
368         OBD_ALLOC(llap, sizeof(*llap));
369         if (llap == NULL)
370                 RETURN(ERR_PTR(-ENOMEM));
371         llap->llap_magic = LLAP_MAGIC;
372         rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd, NULL, page,
373                                  (obd_off)page->index << PAGE_SHIFT,
374                                  &ll_async_page_ops, llap, &llap->llap_cookie);
375         if (rc) {
376                 OBD_FREE(llap, sizeof(*llap));
377                 RETURN(ERR_PTR(rc));
378         }
379
380         CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap,
381                page, llap->llap_cookie, (obd_off)page->index << PAGE_SHIFT);
382         /* also zeroing the PRIVBITS low order bitflags */
383         page->private = (unsigned long)llap;
384         llap->llap_page = page;
385
386         spin_lock(&sbi->ll_lock);
387         sbi->ll_pglist_gen++;
388         list_add_tail(&llap->llap_proc_item, &sbi->ll_pglist);
389         spin_unlock(&sbi->ll_lock);
390
391         RETURN(llap);
392 }
393
394 void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
395                       obd_off size);
396 /* update our write count to account for i_size increases that may have
397  * happened since we've queued the page for io. */
398
399 /* be careful not to return success without setting the page Uptodate or
400  * the next pass through prepare_write will read in stale data from disk. */
401 int ll_commit_write(struct file *file, struct page *page, unsigned from,
402                     unsigned to)
403 {
404         struct inode *inode = page->mapping->host;
405         struct ll_inode_info *lli = ll_i2info(inode);
406         struct lov_stripe_md *lsm = lli->lli_smd;
407         struct obd_export *exp = NULL;
408         struct ll_async_page *llap;
409         loff_t size;
410         int rc = 0;
411         ENTRY;
412
413         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
414         LASSERT(inode == file->f_dentry->d_inode);
415         LASSERT(PageLocked(page));
416
417         CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
418                inode, page, from, to, page->index);
419
420         llap = llap_from_page(page);
421         if (IS_ERR(llap))
422                 RETURN(PTR_ERR(llap));
423
424         /* queue a write for some time in the future the first time we
425          * dirty the page */
426         if (!PageDirty(page)) {
427                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
428                                      LPROC_LL_DIRTY_MISSES);
429
430                 exp = ll_i2obdexp(inode);
431                 if (exp == NULL)
432                         RETURN(-EINVAL);
433
434                 /* _make_ready only sees llap once we've unlocked the page */
435                 llap->llap_write_queued = 1;
436                 rc = obd_queue_async_io(exp, lsm, NULL, llap->llap_cookie,
437                                         OBD_BRW_WRITE, 0, 0, 0, 0);
438                 if (rc != 0) { /* async failed, try sync.. */
439                         struct obd_io_group *oig;
440                         rc = oig_init(&oig);
441                         if (rc)
442                                 GOTO(out, rc);
443
444                         llap->llap_write_queued = 0;
445                         rc = obd_queue_group_io(exp, lsm, NULL, oig,
446                                                 llap->llap_cookie,
447                                                 OBD_BRW_WRITE, 0, to, 0,
448                                                 ASYNC_READY | ASYNC_URGENT |
449                                                 ASYNC_COUNT_STABLE |
450                                                 ASYNC_GROUP_SYNC);
451
452                         if (rc)
453                                 GOTO(free_oig, rc);
454
455                         rc = obd_trigger_group_io(exp, lsm, NULL, oig);
456                         if (rc)
457                                 GOTO(free_oig, rc);
458
459                         rc = oig_wait(oig);
460 free_oig:
461                         oig_release(oig);
462                         GOTO(out, rc);
463                 }
464                 LL_CDEBUG_PAGE(D_PAGE, page, "write queued\n");
465                 //llap_write_pending(inode, llap);
466         } else {
467                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
468                                      LPROC_LL_DIRTY_HITS);
469         }
470
471         /* put the page in the page cache, from now on ll_removepage is
472          * responsible for cleaning up the llap */
473         set_page_dirty(page);
474
475 out:
476         if (rc == 0) {
477                 size = (((obd_off)page->index) << PAGE_SHIFT) + to;
478                 lov_increase_kms(exp, lsm, size);
479                 if (size > inode->i_size)
480                         inode->i_size = size;
481                 SetPageUptodate(page);
482         }
483         RETURN(rc);
484 }
485
486 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
487 {
488         unsigned long ret;
489         ENTRY;
490
491         spin_lock(&sbi->ll_lock);
492         ret = min(sbi->ll_max_read_ahead_pages - sbi->ll_read_ahead_pages,
493                   len);
494         sbi->ll_read_ahead_pages += ret;
495         spin_unlock(&sbi->ll_lock);
496
497         RETURN(ret);
498 }
499
500 static void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
501 {
502         spin_lock(&sbi->ll_lock);
503         LASSERTF(sbi->ll_read_ahead_pages >= len, "r_a_p %lu len %lu\n",
504                  sbi->ll_read_ahead_pages, len);
505         sbi->ll_read_ahead_pages -= len;
506         spin_unlock(&sbi->ll_lock);
507 }
508
509 /* called for each page in a completed rpc.*/
510 void ll_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
511 {
512         struct ll_async_page *llap;
513         struct page *page;
514         ENTRY;
515
516         llap = llap_from_cookie(data);
517         if (IS_ERR(llap)) {
518                 EXIT;
519                 return;
520         }
521
522         page = llap->llap_page;
523         LASSERT(PageLocked(page));
524
525         LL_CDEBUG_PAGE(D_PAGE, page, "completing cmd %d with %d\n", cmd, rc);
526
527         if (cmd == OBD_BRW_READ && llap->llap_defer_uptodate)
528                 ll_ra_count_put(ll_i2sbi(page->mapping->host), 1);
529
530         if (rc == 0)  {
531                 if (cmd == OBD_BRW_READ) {
532                         if (!llap->llap_defer_uptodate)
533                                 SetPageUptodate(page);
534                 } else {
535                         llap->llap_write_queued = 0;
536                 }
537                 ClearPageError(page);
538         } else {
539                 if (cmd == OBD_BRW_READ)
540                         llap->llap_defer_uptodate = 0;
541                 SetPageError(page);
542         }
543
544         unlock_page(page);
545
546         if (0 && cmd == OBD_BRW_WRITE) {
547                 llap_write_complete(page->mapping->host, llap);
548                 ll_try_done_writing(page->mapping->host);
549         }
550
551         page_cache_release(page);
552         EXIT;
553 }
554
555 /* the kernel calls us here when a page is unhashed from the page cache.
556  * the page will be locked and the kernel is holding a spinlock, so
557  * we need to be careful.  we're just tearing down our book-keeping
558  * here. */
559 void ll_removepage(struct page *page)
560 {
561         struct inode *inode = page->mapping->host;
562         struct obd_export *exp;
563         struct ll_async_page *llap;
564         struct ll_sb_info *sbi = ll_i2sbi(inode);
565         int rc;
566         ENTRY;
567
568         LASSERT(!in_interrupt());
569
570         /* sync pages or failed read pages can leave pages in the page
571          * cache that don't have our data associated with them anymore */
572         if (page->private == 0) {
573                 EXIT;
574                 return;
575         }
576
577         LL_CDEBUG_PAGE(D_PAGE, page, "being evicted\n");
578
579         exp = ll_i2obdexp(inode);
580         if (exp == NULL) {
581                 CERROR("page %p ind %lu gave null export\n", page, page->index);
582                 EXIT;
583                 return;
584         }
585
586         llap = llap_from_page(page);
587         if (IS_ERR(llap)) {
588                 CERROR("page %p ind %lu couldn't find llap: %ld\n", page,
589                        page->index, PTR_ERR(llap));
590                 EXIT;
591                 return;
592         }
593
594         //llap_write_complete(inode, llap);
595         rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL,
596                                      llap->llap_cookie);
597         if (rc != 0)
598                 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
599
600         /* this unconditional free is only safe because the page lock
601          * is providing exclusivity to memory pressure/truncate/writeback..*/
602         page->private = 0;
603
604         spin_lock(&sbi->ll_lock);
605         if (!list_empty(&llap->llap_proc_item))
606                 list_del_init(&llap->llap_proc_item);
607         sbi->ll_pglist_gen++;
608         spin_unlock(&sbi->ll_lock);
609         OBD_FREE(llap, sizeof(*llap));
610         EXIT;
611 }
612
613 static int ll_page_matches(struct page *page, int fd_flags)
614 {
615         struct lustre_handle match_lockh = {0};
616         struct inode *inode = page->mapping->host;
617         ldlm_policy_data_t page_extent;
618         int flags, matches;
619         ENTRY;
620
621         if (fd_flags & LL_FILE_GROUP_LOCKED)
622                 RETURN(1);
623
624         page_extent.l_extent.start = (__u64)page->index << PAGE_CACHE_SHIFT;
625         page_extent.l_extent.end =
626                 page_extent.l_extent.start + PAGE_CACHE_SIZE - 1;
627         flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
628         matches = obd_match(ll_i2sbi(inode)->ll_osc_exp,
629                             ll_i2info(inode)->lli_smd, LDLM_EXTENT,
630                             &page_extent, LCK_PR | LCK_PW, &flags, inode,
631                             &match_lockh);
632         RETURN(matches);
633 }
634
635 static int ll_issue_page_read(struct obd_export *exp,
636                               struct ll_async_page *llap,
637                               struct obd_io_group *oig, int defer)
638 {
639         struct page *page = llap->llap_page;
640         int rc;
641
642         page_cache_get(page);
643         llap->llap_defer_uptodate = defer;
644         rc = obd_queue_group_io(exp, ll_i2info(page->mapping->host)->lli_smd,
645                                 NULL, oig, llap->llap_cookie, OBD_BRW_READ, 0,
646                                 PAGE_SIZE, 0, ASYNC_COUNT_STABLE | ASYNC_READY
647                                               | ASYNC_URGENT);
648         if (rc) {
649                 LL_CDEBUG_PAGE(D_ERROR, page, "read queue failed: rc %d\n", rc);
650                 page_cache_release(page);
651         }
652         RETURN(rc);
653 }
654
655 #define RAS_CDEBUG(ras) \
656         CDEBUG(D_READA, "lrp %lu c %lu ws %lu wl %lu nra %lu\n",        \
657                ras->ras_last_readpage, ras->ras_consecutive,            \
658                ras->ras_window_start, ras->ras_window_len,              \
659                ras->ras_next_readahead);
660
661 static int ll_readahead(struct ll_readahead_state *ras,
662                          struct obd_export *exp, struct address_space *mapping,
663                          struct obd_io_group *oig, int flags)
664 {
665         unsigned long i, start = 0, end = 0, reserved;
666         struct ll_async_page *llap;
667         struct page *page;
668         int rc, ret = 0;
669         __u64 kms;
670         ENTRY;
671
672         kms = lov_merge_size(ll_i2info(mapping->host)->lli_smd, 1);
673         if (kms == 0)
674                 RETURN(0);
675
676         spin_lock(&ras->ras_lock);
677
678         if (ras->ras_window_len) {
679                 start = ras->ras_next_readahead;
680                 end = ras->ras_window_start + ras->ras_window_len - 1;
681                 end = min(end, (unsigned long)(kms >> PAGE_CACHE_SHIFT));
682                 ras->ras_next_readahead = max(end, end + 1);
683
684                 RAS_CDEBUG(ras);
685         }
686
687         spin_unlock(&ras->ras_lock);
688
689         if (end == 0)
690                 RETURN(0);
691
692         reserved = ll_ra_count_get(ll_i2sbi(mapping->host), end - start + 1);
693
694         for (i = start; reserved > 0 && i <= end; i++) {
695                 /* skip locked pages from previous readpage calls */
696                 page = grab_cache_page_nowait(mapping, i);
697                 if (page == NULL) {
698                         CDEBUG(D_READA, "g_c_p_n failed\n");
699                         continue;
700                 }
701                 
702                 /* we do this first so that we can see the page in the /proc
703                  * accounting */
704                 llap = llap_from_page(page);
705                 if (IS_ERR(llap) || llap->llap_defer_uptodate)
706                         goto next_page;
707
708                 /* skip completed pages */
709                 if (Page_Uptodate(page))
710                         goto next_page;
711
712                 /* bail when we hit the end of the lock. */
713                 if ((rc = ll_page_matches(page, flags)) <= 0) {
714                         LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
715                                        "lock match failed: rc %d\n", rc);
716                         i = end;
717                         goto next_page;
718                 }
719
720                 rc = ll_issue_page_read(exp, llap, oig, 1);
721                 if (rc == 0) {
722                         reserved--;
723                         ret++;
724                         LL_CDEBUG_PAGE(D_READA| D_PAGE, page, 
725                                        "started read-ahead\n");
726                 }
727                 if (rc) {
728         next_page:
729                         LL_CDEBUG_PAGE(D_READA | D_PAGE, page, 
730                                        "skipping read-ahead\n");
731
732                         unlock_page(page);
733                 }
734                 page_cache_release(page);
735         }
736
737         LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
738         if (reserved != 0)
739                 ll_ra_count_put(ll_i2sbi(mapping->host), reserved);
740         RETURN(ret);
741 }
742
743 static void ras_set_start(struct ll_readahead_state *ras,
744                                unsigned long index)
745 {
746         ras->ras_window_start = index & (~(PTLRPC_MAX_BRW_PAGES - 1));
747         ras->ras_next_readahead = max(ras->ras_window_start,
748                                       ras->ras_next_readahead);
749 }
750
751 /* called with the ras_lock held or from places where it doesn't matter */
752 static void ras_reset(struct ll_readahead_state *ras,
753                       unsigned long index)
754 {
755         ras->ras_last_readpage = index;
756         ras->ras_consecutive = 1;
757         ras->ras_window_len = 0;
758         ras_set_start(ras, index);
759         ras->ras_next_readahead = ras->ras_window_start;
760
761         RAS_CDEBUG(ras);
762 }
763
764 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
765 {
766         spin_lock_init(&ras->ras_lock);
767         ras_reset(ras, 0);
768 }
769
770 static void ras_update(struct ll_readahead_state *ras,
771                        unsigned long index, unsigned long max)
772 {
773         ENTRY;
774
775         spin_lock(&ras->ras_lock);
776
777         if (index != ras->ras_last_readpage + 1) {
778                 ras_reset(ras, index);
779                 GOTO(out_unlock, 0);
780         }
781
782         ras->ras_last_readpage = index;
783         ras->ras_consecutive++;
784         ras_set_start(ras, index);
785
786         if (ras->ras_consecutive == 2) {
787                 ras->ras_window_len = PTLRPC_MAX_BRW_PAGES;
788                 GOTO(out_unlock, 0);
789         }
790
791         /* we need to increase the window sometimes.  we'll arbitrarily
792          * do it half-way through the pages in an rpc */
793         if ((index & (PTLRPC_MAX_BRW_PAGES - 1)) == 
794             (PTLRPC_MAX_BRW_PAGES >> 1)) {
795                 ras->ras_window_len += PTLRPC_MAX_BRW_PAGES;
796                 ras->ras_window_len = min(ras->ras_window_len, max);
797         }
798
799         EXIT;
800 out_unlock:
801         RAS_CDEBUG(ras);
802         spin_unlock(&ras->ras_lock);
803         return;
804 }
805
806 /*
807  * for now we do our readpage the same on both 2.4 and 2.5.  The kernel's
808  * read-ahead assumes it is valid to issue readpage all the way up to
809  * i_size, but our dlm locks make that not the case.  We disable the
810  * kernel's read-ahead and do our own by walking ahead in the page cache
811  * checking for dlm lock coverage.  the main difference between 2.4 and
812  * 2.6 is how read-ahead gets batched and issued, but we're using our own,
813  * so they look the same.
814  */
815 int ll_readpage(struct file *filp, struct page *page)
816 {
817         struct ll_file_data *fd = filp->private_data;
818         struct inode *inode = page->mapping->host;
819         struct obd_export *exp;
820         struct ll_async_page *llap;
821         struct obd_io_group *oig = NULL;
822         int rc;
823         ENTRY;
824
825         LASSERT(PageLocked(page));
826         LASSERT(!PageUptodate(page));
827         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset="LPX64"\n",
828                inode->i_ino, inode->i_generation, inode,
829                (((obd_off)page->index) << PAGE_SHIFT));
830         LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
831
832         rc = oig_init(&oig);
833         if (rc < 0)
834                 GOTO(out, rc);
835
836         exp = ll_i2obdexp(inode);
837         if (exp == NULL)
838                 GOTO(out, rc = -EINVAL);
839
840         llap = llap_from_page(page);
841         if (IS_ERR(llap))
842                 GOTO(out, rc = PTR_ERR(llap));
843
844         if (ll_i2sbi(inode)->ll_flags & LL_SBI_READAHEAD)
845                 ras_update(&fd->fd_ras, page->index, 
846                            ll_i2sbi(inode)->ll_max_read_ahead_pages);
847
848         if (llap->llap_defer_uptodate) {
849                 rc = ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
850                                   fd->fd_flags);
851                 if (rc > 0)
852                         obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, 
853                                              NULL, oig);
854                 LL_CDEBUG_PAGE(D_PAGE, page, "marking uptodate from defer\n");
855                 SetPageUptodate(page);
856                 unlock_page(page);
857                 GOTO(out_oig, rc = 0);
858         }
859
860         rc = ll_page_matches(page, fd->fd_flags);
861         if (rc < 0) {
862                 LL_CDEBUG_PAGE(D_ERROR, page, "lock match failed: rc %d\n", rc);
863                 GOTO(out, rc);
864         }
865
866         if (rc == 0) {
867                 static unsigned long next_print;
868                 CDEBUG(D_INODE, "ino %lu page %lu (%llu) didn't match a lock\n",
869                        inode->i_ino, page->index,
870                        (long long)page->index << PAGE_CACHE_SHIFT);
871                 if (time_after(jiffies, next_print)) {
872                         CWARN("ino %lu page %lu (%llu) not covered by "
873                                "a lock (mmap?).  check debug logs.\n",
874                                inode->i_ino, page->index,
875                                (long long)page->index << PAGE_CACHE_SHIFT);
876                         next_print = jiffies + 30 * HZ;
877                 }
878         }
879
880         rc = ll_issue_page_read(exp, llap, oig, 0);
881         if (rc)
882                 GOTO(out, rc);
883
884         LL_CDEBUG_PAGE(D_PAGE, page, "queued readpage\n");
885         if (ll_i2sbi(inode)->ll_flags & LL_SBI_READAHEAD)
886                 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
887                              fd->fd_flags);
888
889         rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
890
891 out:
892         if (rc)
893                 unlock_page(page);
894 out_oig:
895         if (oig != NULL)
896                 oig_release(oig);
897         RETURN(rc);
898 }