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