Whamcloud - gitweb
Land b_smallfix onto HEAD (20040330_2312)
[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         if (cmd == OBD_BRW_READ)
232                 RETURN(0);
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 /* XXX have the exp be an argument? */
340 struct ll_async_page *llap_from_page(struct page *page)
341 {
342         struct ll_async_page *llap;
343         struct obd_export *exp;
344         struct inode *inode = page->mapping->host;
345         struct ll_sb_info *sbi = ll_i2sbi(inode);
346         int rc;
347         ENTRY;
348
349         llap = (struct ll_async_page *)page->private;
350         if (llap != NULL) {
351                 if (llap->llap_magic != LLAP_MAGIC)
352                         RETURN(ERR_PTR(-EINVAL));
353                 RETURN(llap);
354         }
355
356         exp = ll_i2obdexp(page->mapping->host);
357         if (exp == NULL)
358                 RETURN(ERR_PTR(-EINVAL));
359
360         OBD_ALLOC(llap, sizeof(*llap));
361         if (llap == NULL)
362                 RETURN(ERR_PTR(-ENOMEM));
363         llap->llap_magic = LLAP_MAGIC;
364         rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd, NULL, page,
365                                  (obd_off)page->index << PAGE_SHIFT,
366                                  &ll_async_page_ops, llap, &llap->llap_cookie);
367         if (rc) {
368                 OBD_FREE(llap, sizeof(*llap));
369                 RETURN(ERR_PTR(rc));
370         }
371
372         CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap,
373                page, llap->llap_cookie, (obd_off)page->index << PAGE_SHIFT);
374         /* also zeroing the PRIVBITS low order bitflags */
375         page->private = (unsigned long)llap;
376         llap->llap_page = page;
377
378         spin_lock(&sbi->ll_pglist_lock);
379         sbi->ll_pglist_gen++;
380         list_add_tail(&llap->llap_proc_item, &sbi->ll_pglist);
381         spin_unlock(&sbi->ll_pglist_lock);
382
383         RETURN(llap);
384 }
385
386 void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
387                       obd_off size);
388 /* update our write count to account for i_size increases that may have
389  * happened since we've queued the page for io. */
390
391 /* be careful not to return success without setting the page Uptodate or
392  * the next pass through prepare_write will read in stale data from disk. */
393 int ll_commit_write(struct file *file, struct page *page, unsigned from,
394                     unsigned to)
395 {
396         struct inode *inode = page->mapping->host;
397         struct ll_inode_info *lli = ll_i2info(inode);
398         struct lov_stripe_md *lsm = lli->lli_smd;
399         struct obd_export *exp = NULL;
400         struct ll_async_page *llap;
401         loff_t size;
402         int rc = 0;
403         ENTRY;
404
405         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
406         LASSERT(inode == file->f_dentry->d_inode);
407         LASSERT(PageLocked(page));
408
409         CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
410                inode, page, from, to, page->index);
411
412         llap = llap_from_page(page);
413         if (IS_ERR(llap))
414                 RETURN(PTR_ERR(llap));
415
416         /* queue a write for some time in the future the first time we
417          * dirty the page */
418         if (!PageDirty(page)) {
419                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
420                                      LPROC_LL_DIRTY_MISSES);
421
422                 exp = ll_i2obdexp(inode);
423                 if (exp == NULL)
424                         RETURN(-EINVAL);
425
426                 /* _make_ready only sees llap once we've unlocked the page */
427                 llap->llap_write_queued = 1;
428                 rc = obd_queue_async_io(exp, lsm, NULL, llap->llap_cookie,
429                                         OBD_BRW_WRITE, 0, 0, 0, 0);
430                 if (rc != 0) { /* async failed, try sync.. */
431                         struct obd_io_group *oig;
432                         rc = oig_init(&oig);
433                         if (rc)
434                                 GOTO(out, rc);
435
436                         llap->llap_write_queued = 0;
437                         rc = obd_queue_group_io(exp, lsm, NULL, oig,
438                                                 llap->llap_cookie,
439                                                 OBD_BRW_WRITE, 0, to, 0,
440                                                 ASYNC_READY | ASYNC_URGENT |
441                                                 ASYNC_COUNT_STABLE |
442                                                 ASYNC_GROUP_SYNC);
443
444                         if (rc)
445                                 GOTO(free_oig, rc);
446
447                         rc = obd_trigger_group_io(exp, lsm, NULL, oig);
448                         if (rc)
449                                 GOTO(free_oig, rc);
450
451                         rc = oig_wait(oig);
452 free_oig:
453                         oig_release(oig);
454                         GOTO(out, rc);
455                 }
456                 LL_CDEBUG_PAGE(D_PAGE, page, "write queued\n");
457                 //llap_write_pending(inode, llap);
458         } else {
459                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
460                                      LPROC_LL_DIRTY_HITS);
461         }
462
463         /* put the page in the page cache, from now on ll_removepage is
464          * responsible for cleaning up the llap */
465         set_page_dirty(page);
466
467 out:
468         if (rc == 0) {
469                 size = (((obd_off)page->index) << PAGE_SHIFT) + to;
470                 lov_increase_kms(exp, lsm, size);
471                 if (size > inode->i_size)
472                         inode->i_size = size;
473                 SetPageUptodate(page);
474         }
475         RETURN(rc);
476 }
477
478 /* called for each page in a completed rpc.*/
479 void ll_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
480 {
481         struct ll_async_page *llap;
482         struct page *page;
483         ENTRY;
484
485         llap = llap_from_cookie(data);
486         if (IS_ERR(llap)) {
487                 EXIT;
488                 return;
489         }
490
491         page = llap->llap_page;
492         LASSERT(PageLocked(page));
493
494         LL_CDEBUG_PAGE(D_PAGE, page, "completing cmd %d with %d\n", cmd, rc);
495
496         if (rc == 0)  {
497                 if (cmd == OBD_BRW_READ) {
498                         if (!llap->llap_defer_uptodate)
499                                 SetPageUptodate(page);
500                 } else {
501                         llap->llap_write_queued = 0;
502                 }
503                 ClearPageError(page);
504         } else {
505                 if (cmd == OBD_BRW_READ)
506                         llap->llap_defer_uptodate = 0;
507                 SetPageError(page);
508         }
509
510
511         unlock_page(page);
512
513         if (0 && cmd == OBD_BRW_WRITE) {
514                 llap_write_complete(page->mapping->host, llap);
515                 ll_try_done_writing(page->mapping->host);
516         }
517
518         page_cache_release(page);
519         EXIT;
520 }
521
522 /* the kernel calls us here when a page is unhashed from the page cache.
523  * the page will be locked and the kernel is holding a spinlock, so
524  * we need to be careful.  we're just tearing down our book-keeping
525  * here. */
526 void ll_removepage(struct page *page)
527 {
528         struct inode *inode = page->mapping->host;
529         struct obd_export *exp;
530         struct ll_async_page *llap;
531         struct ll_sb_info *sbi = ll_i2sbi(inode);
532         int rc;
533         ENTRY;
534
535         LASSERT(!in_interrupt());
536
537         /* sync pages or failed read pages can leave pages in the page
538          * cache that don't have our data associated with them anymore */
539         if (page->private == 0) {
540                 EXIT;
541                 return;
542         }
543
544         LL_CDEBUG_PAGE(D_PAGE, page, "being evicted\n");
545
546         exp = ll_i2obdexp(inode);
547         if (exp == NULL) {
548                 CERROR("page %p ind %lu gave null export\n", page, page->index);
549                 EXIT;
550                 return;
551         }
552
553         llap = llap_from_page(page);
554         if (IS_ERR(llap)) {
555                 CERROR("page %p ind %lu couldn't find llap: %ld\n", page,
556                        page->index, PTR_ERR(llap));
557                 EXIT;
558                 return;
559         }
560
561         //llap_write_complete(inode, llap);
562         rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL,
563                                      llap->llap_cookie);
564         if (rc != 0)
565                 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
566
567         /* this unconditional free is only safe because the page lock
568          * is providing exclusivity to memory pressure/truncate/writeback..*/
569         page->private = 0;
570
571         spin_lock(&sbi->ll_pglist_lock);
572         if (!list_empty(&llap->llap_proc_item))
573                 list_del_init(&llap->llap_proc_item);
574         sbi->ll_pglist_gen++;
575         spin_unlock(&sbi->ll_pglist_lock);
576         OBD_FREE(llap, sizeof(*llap));
577         EXIT;
578 }
579
580 static int ll_page_matches(struct page *page, int fd_flags)
581 {
582         struct lustre_handle match_lockh = {0};
583         struct inode *inode = page->mapping->host;
584         ldlm_policy_data_t page_extent;
585         int flags, matches;
586         ENTRY;
587
588         if (fd_flags & LL_FILE_GROUP_LOCKED)
589                 RETURN(1);
590
591         page_extent.l_extent.start = (__u64)page->index << PAGE_CACHE_SHIFT;
592         page_extent.l_extent.end =
593                 page_extent.l_extent.start + PAGE_CACHE_SIZE - 1;
594         flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
595         matches = obd_match(ll_i2sbi(inode)->ll_osc_exp,
596                             ll_i2info(inode)->lli_smd, LDLM_EXTENT,
597                             &page_extent, LCK_PR | LCK_PW, &flags, inode,
598                             &match_lockh);
599         RETURN(matches);
600 }
601
602 static int ll_issue_page_read(struct obd_export *exp,
603                               struct ll_async_page *llap,
604                               struct obd_io_group *oig, int defer)
605 {
606         struct page *page = llap->llap_page;
607         int rc;
608
609         page_cache_get(page);
610         llap->llap_defer_uptodate = defer;
611         rc = obd_queue_group_io(exp, ll_i2info(page->mapping->host)->lli_smd,
612                                 NULL, oig, llap->llap_cookie, OBD_BRW_READ, 0,
613                                 PAGE_SIZE, 0, ASYNC_COUNT_STABLE);
614         if (rc) {
615                 LL_CDEBUG_PAGE(D_ERROR, page, "read queue failed: rc %d\n", rc);
616                 page_cache_release(page);
617         }
618         RETURN(rc);
619 }
620
621 #define LL_RA_MIN(inode) ((unsigned long)PTLRPC_MAX_BRW_PAGES / 2)
622 #define LL_RA_MAX(inode) ((ll_i2info(inode)->lli_smd->lsm_xfersize * 3) >> \
623                           PAGE_CACHE_SHIFT)
624
625 static void ll_readahead(struct ll_readahead_state *ras,
626                          struct obd_export *exp, struct address_space *mapping,
627                          struct obd_io_group *oig, int flags)
628 {
629         unsigned long i, start, end;
630         struct ll_async_page *llap;
631         struct page *page;
632         int rc;
633
634         if (mapping->host->i_size == 0)
635                 return;
636
637         spin_lock(&ras->ras_lock);
638
639         /* make sure to issue a window's worth of read-ahead pages */
640         end = ras->ras_last;
641         start = end - ras->ras_window;
642         if (start > end)
643                 start = 0;
644
645         /* but don't iterate over pages that we've already issued.  this
646          * will set start to end + 1 if we've already read-ahead up to
647          * ras_last sothe for() won't be entered */
648         if (ras->ras_next_index > start)
649                 start = ras->ras_next_index;
650         if (end != ~0UL)
651                 ras->ras_next_index = end + 1;
652
653         CDEBUG(D_READA, "ni %lu last %lu win %lu: reading from %lu to %lu\n",
654                ras->ras_next_index, ras->ras_last, ras->ras_window,
655                start, end);
656
657         spin_unlock(&ras->ras_lock);
658
659         /* clamp to filesize */
660         i = (mapping->host->i_size - 1) >> PAGE_CACHE_SHIFT;
661         end = min(end, i);
662
663         for (i = start; i <= end; i++) {
664                 /* grab_cache_page_nowait returns null if this races with
665                  * truncating the page (page->mapping == NULL) */
666                 page = grab_cache_page_nowait(mapping, i);
667                 if (page == NULL)
668                        break;
669
670                 /* the book-keeping above promises that we've tried
671                  * all the indices from start to end, so we don't
672                  * stop if anyone returns an error. This may not be good. */
673                 if (Page_Uptodate(page))
674                         goto next_page;
675
676                 if ((rc = ll_page_matches(page, flags)) <= 0) {
677                         LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
678                                        "lock match failed: rc %d\n", rc);
679                         goto next_page;
680                 }
681
682                 llap = llap_from_page(page);
683                 if (IS_ERR(llap) || llap->llap_defer_uptodate)
684                         goto next_page;
685
686                 rc = ll_issue_page_read(exp, llap, oig, 1);
687                 if (rc == 0)
688                         LL_CDEBUG_PAGE(D_PAGE, page, "started read-ahead\n");
689                 if (rc) {
690         next_page:
691                         LL_CDEBUG_PAGE(D_PAGE, page, "skipping read-ahead\n");
692
693                         unlock_page(page);
694                 }
695                 page_cache_release(page);
696         }
697 }
698
699 /* called with the ras_lock held or from places where it doesn't matter */
700 static void ll_readahead_set(struct inode *inode,
701                              struct ll_readahead_state *ras,
702                              unsigned long index)
703 {
704         ras->ras_next_index = index;
705         if (ras->ras_next_index != ~0UL)
706                 ras->ras_next_index++;
707         ras->ras_window = LL_RA_MIN(inode);
708         ras->ras_last = ras->ras_next_index + ras->ras_window;
709         if (ras->ras_last < ras->ras_next_index)
710                 ras->ras_last = ~0UL;
711         CDEBUG(D_READA, "ni %lu last %lu win %lu: set %lu\n",
712                ras->ras_next_index, ras->ras_last, ras->ras_window,
713                index);
714 }
715
716 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
717 {
718         spin_lock_init(&ras->ras_lock);
719         ll_readahead_set(inode, ras, 0);
720 }
721
722 static void ll_readahead_update(struct inode *inode,
723                                 struct ll_readahead_state *ras,
724                                 unsigned long index, int hit)
725 {
726         unsigned long issued_start, new_last;
727
728         spin_lock(&ras->ras_lock);
729
730         /* we're interested in noticing the index's relation to the
731          * previously issued read-ahead pages */
732         issued_start = ras->ras_next_index - ras->ras_window - 1;
733         if (issued_start > ras->ras_next_index)
734                 issued_start = 0;
735
736         CDEBUG(D_READA, "ni %lu last %lu win %lu: %s ind %lu start %lu\n",
737                ras->ras_next_index, ras->ras_last, ras->ras_window,
738                hit ? "hit" : "miss", index, issued_start);
739         if (!hit &&
740             index == ras->ras_next_index && index == ras->ras_last + 1) {
741                 /* special case the kernel's read-ahead running into the
742                  * page just beyond our read-ahead window as an extension
743                  * of our read-ahead.  sigh.  wishing it was easier to
744                  * turn off 2.4's read-ahead. */
745                 ras->ras_window = min(LL_RA_MAX(inode), ras->ras_window + 1);
746                 if (index != ~0UL)
747                         ras->ras_next_index = index + 1;
748                 ras->ras_last = index;
749         } else if (!hit &&
750                    (index > issued_start || ras->ras_next_index >= index)) {
751                 /* deal with a miss way out of the window.  we interpret
752                  * this as a seek and restart the window */
753                 ll_readahead_set(inode, ras, index);
754
755         } else if (!hit &&
756                    issued_start <= index && index < ras->ras_next_index) {
757                 /* a miss inside the window?  surely its memory pressure
758                  * evicting our read pages before the app can see them.
759                  * we shrink the window aggressively */
760                 unsigned long old_window = ras->ras_window;
761
762                 ras->ras_window = max(ras->ras_window / 2, LL_RA_MIN(inode));
763                 ras->ras_last -= old_window - ras->ras_window;
764                 if (ras->ras_next_index > ras->ras_last)
765                         ras->ras_next_index = ras->ras_last + 1;
766                 CDEBUG(D_READA, "ni %lu last %lu win %lu: miss inside\n",
767                        ras->ras_next_index, ras->ras_last, ras->ras_window);
768
769         } else if (hit &&
770                    issued_start <= index && index < ras->ras_next_index) {
771                 /* a hit inside the window.  grow the window by twice the
772                  * number of pages that are satisified within the window.  */
773                 ras->ras_window = min(LL_RA_MAX(inode), ras->ras_window + 2);
774
775                 /* we want the next readahead pass to issue a windows worth
776                  * beyond where the app currently is */
777                 new_last = index + ras->ras_window;
778                 if (new_last > ras->ras_last)
779                         ras->ras_last = new_last;
780
781                 CDEBUG(D_READA, "ni %lu last %lu win %lu: extended window/last\n",
782                        ras->ras_next_index, ras->ras_last, ras->ras_window);
783         }
784
785         spin_unlock(&ras->ras_lock);
786 }
787
788 /*
789  * for now we do our readpage the same on both 2.4 and 2.5.  The kernel's
790  * read-ahead assumes it is valid to issue readpage all the way up to
791  * i_size, but our dlm locks make that not the case.  We disable the
792  * kernel's read-ahead and do our own by walking ahead in the page cache
793  * checking for dlm lock coverage.  the main difference between 2.4 and
794  * 2.6 is how read-ahead gets batched and issued, but we're using our own,
795  * so they look the same.
796  */
797 int ll_readpage(struct file *filp, struct page *page)
798 {
799         struct ll_file_data *fd = filp->private_data;
800         struct inode *inode = page->mapping->host;
801         struct obd_export *exp;
802         struct ll_async_page *llap;
803         struct obd_io_group *oig = NULL;
804         int rc;
805         ENTRY;
806
807         LASSERT(PageLocked(page));
808         LASSERT(!PageUptodate(page));
809         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset="LPX64"\n",
810                inode->i_ino, inode->i_generation, inode,
811                (((obd_off)page->index) << PAGE_SHIFT));
812         LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
813
814         rc = oig_init(&oig);
815         if (rc < 0)
816                 GOTO(out, rc);
817
818         exp = ll_i2obdexp(inode);
819         if (exp == NULL)
820                 GOTO(out, rc = -EINVAL);
821
822         llap = llap_from_page(page);
823         if (IS_ERR(llap))
824                 GOTO(out, rc = PTR_ERR(llap));
825
826         if (llap->llap_defer_uptodate) {
827                 ll_readahead_update(inode, &fd->fd_ras, page->index, 1);
828                 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,fd->fd_flags);
829                 obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL,
830                                      oig);
831                 LL_CDEBUG_PAGE(D_PAGE, page, "marking uptodate from defer\n");
832                 SetPageUptodate(page);
833                 unlock_page(page);
834                 GOTO(out_oig, rc = 0);
835         }
836
837         ll_readahead_update(inode, &fd->fd_ras, page->index, 0);
838
839         rc = ll_page_matches(page, fd->fd_flags);
840         if (rc < 0) {
841                 LL_CDEBUG_PAGE(D_ERROR, page, "lock match failed: rc %d\n", rc);
842                 GOTO(out, rc);
843         }
844
845         if (rc == 0) {
846                 static unsigned long next_print;
847                 CDEBUG(D_INODE, "ino %lu page %lu (%llu) didn't match a lock\n",
848                        inode->i_ino, page->index,
849                        (long long)page->index << PAGE_CACHE_SHIFT);
850                 if (time_after(jiffies, next_print)) {
851                         CERROR("ino %lu page %lu (%llu) not covered by "
852                                "a lock (mmap?).  check debug logs.\n",
853                                inode->i_ino, page->index,
854                                (long long)page->index << PAGE_CACHE_SHIFT);
855                         ldlm_dump_all_namespaces();
856                         if (next_print == 0) {
857                                 CERROR("%s\n", portals_debug_dumpstack());
858                                 portals_debug_dumplog();
859                         }
860                         next_print = jiffies + 30 * HZ;
861                 }
862         }
863
864         rc = ll_issue_page_read(exp, llap, oig, 0);
865         if (rc)
866                 GOTO(out, rc);
867
868         LL_CDEBUG_PAGE(D_PAGE, page, "queued readpage\n");
869         if ((ll_i2sbi(inode)->ll_flags & LL_SBI_READAHEAD))
870                 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,fd->fd_flags);
871
872         rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
873
874 out:
875         if (rc)
876                 unlock_page(page);
877 out_oig:
878         if (oig != NULL)
879                 oig_release(oig);
880         RETURN(rc);
881 }
882
883 #if 0
884 /* this is for read pages.  we issue them as ready but not urgent.  when
885  * someone waits on them we fire them off, hopefully merged with adjacent
886  * reads that were queued by read-ahead.  */
887 int ll_sync_page(struct page *page)
888 {
889         struct obd_export *exp;
890         struct ll_async_page *llap;
891         int rc;
892         ENTRY;
893
894         /* we're using a low bit flag to signify that a queued read should
895          * be issued once someone goes to lock it.  it is also cleared
896          * as the page is built into an RPC */
897         if (!test_and_clear_bit(LL_PRIVBITS_READ, &page->private))
898                 RETURN(0);
899
900         /* careful to only deref page->mapping after checking the bit */
901         exp = ll_i2obdexp(page->mapping->host);
902         if (exp == NULL)
903                 RETURN(-EINVAL);
904
905         llap = llap_from_page(page);
906         if (IS_ERR(llap))
907                 RETURN(PTR_ERR(llap));
908
909         LL_CDEBUG_PAGE(D_PAGE, page, "setting ready|urgent\n");
910
911         rc = obd_set_async_flags(exp, ll_i2info(page->mapping->host)->lli_smd,
912                                  NULL, llap->llap_cookie,
913                                  ASYNC_READY|ASYNC_URGENT);
914         return rc;
915 }
916 #endif