Whamcloud - gitweb
3b96985f2db5226a88f22b1b74c330f1a993a325
[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 #ifndef AUTOCONF_INCLUDED
24 #include <linux/config.h>
25 #endif
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/string.h>
29 #include <linux/stat.h>
30 #include <linux/errno.h>
31 #include <linux/smp_lock.h>
32 #include <linux/unistd.h>
33 #include <linux/version.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36
37 #include <linux/fs.h>
38 #include <linux/stat.h>
39 #include <asm/uaccess.h>
40 #ifdef HAVE_SEGMENT_H
41 # include <asm/segment.h>
42 #endif
43 #include <linux/mm.h>
44 #include <linux/pagemap.h>
45 #include <linux/smp_lock.h>
46
47 #define DEBUG_SUBSYSTEM S_LLITE
48
49 //#include <lustre_mdc.h>
50 #include <lustre_lite.h>
51 #include "llite_internal.h"
52 #include <linux/lustre_compat25.h>
53
54 #ifndef list_for_each_prev_safe
55 #define list_for_each_prev_safe(pos, n, head) \
56         for (pos = (head)->prev, n = pos->prev; pos != (head); \
57                 pos = n, n = pos->prev )
58 #endif
59
60 cfs_mem_cache_t *ll_async_page_slab = NULL;
61 size_t ll_async_page_slab_size = 0;
62
63 /* SYNCHRONOUS I/O to object storage for an inode */
64 static int ll_brw(int cmd, struct inode *inode, struct obdo *oa,
65                   struct page *page, int flags)
66 {
67         struct ll_inode_info *lli = ll_i2info(inode);
68         struct lov_stripe_md *lsm = lli->lli_smd;
69         struct obd_info oinfo = { { { 0 } } };
70         struct brw_page pg;
71         int opc, rc;
72         ENTRY;
73
74         pg.pg = page;
75         pg.off = ((obd_off)page->index) << CFS_PAGE_SHIFT;
76
77         if ((cmd & OBD_BRW_WRITE) && (pg.off+CFS_PAGE_SIZE>i_size_read(inode)))
78                 pg.count = i_size_read(inode) % CFS_PAGE_SIZE;
79         else
80                 pg.count = CFS_PAGE_SIZE;
81
82         LL_CDEBUG_PAGE(D_PAGE, page, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
83                        cmd & OBD_BRW_WRITE ? "write" : "read", pg.count,
84                        inode->i_ino, pg.off, pg.off);
85         if (pg.count == 0) {
86                 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
87                        LPU64"\n", inode->i_ino, inode, i_size_read(inode),
88                        page->mapping->host, i_size_read(page->mapping->host),
89                        page->index, pg.off);
90         }
91
92         pg.flag = flags;
93
94         if (cmd & OBD_BRW_WRITE)
95                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_WRITE,
96                                    pg.count);
97         else
98                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_BRW_READ,
99                                    pg.count);
100         oinfo.oi_oa = oa;
101         oinfo.oi_md = lsm;
102         /* NB partial write, so we might not have CAPA_OPC_OSS_READ capa */
103         opc = cmd & OBD_BRW_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
104         oinfo.oi_capa = ll_osscapa_get(inode, opc);
105         rc = obd_brw(cmd, ll_i2dtexp(inode), &oinfo, 1, &pg, NULL);
106         capa_put(oinfo.oi_capa);
107         if (rc == 0)
108                 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
109         else if (rc != -EIO)
110                 CERROR("error from obd_brw: rc = %d\n", rc);
111         RETURN(rc);
112 }
113
114 /* this isn't where truncate starts.   roughly:
115  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate. setattr_raw grabs
116  * DLM lock on [size, EOF], i_mutex, ->lli_size_sem, and WRITE_I_ALLOC_SEM to
117  * avoid races.
118  *
119  * must be called under ->lli_size_sem */
120 void ll_truncate(struct inode *inode)
121 {
122         struct ll_inode_info *lli = ll_i2info(inode);
123         struct obd_info oinfo = { { { 0 } } };
124         struct ost_lvb lvb;
125         struct obdo oa;
126         int rc;
127         ENTRY;
128         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %Lu=%#Lx\n",inode->i_ino,
129                inode->i_generation, inode, i_size_read(inode),
130                i_size_read(inode));
131
132         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_TRUNC, 1);
133         if (lli->lli_size_sem_owner != current) {
134                 EXIT;
135                 return;
136         }
137
138         if (!lli->lli_smd) {
139                 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
140                        inode->i_ino);
141                 GOTO(out_unlock, 0);
142         }
143
144         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
145
146         /* XXX I'm pretty sure this is a hack to paper over a more fundamental
147          * race condition. */
148         lov_stripe_lock(lli->lli_smd);
149         inode_init_lvb(inode, &lvb);
150         rc = obd_merge_lvb(ll_i2dtexp(inode), lli->lli_smd, &lvb, 0);
151         if (lvb.lvb_size == i_size_read(inode) && rc == 0) {
152                 CDEBUG(D_VFSTRACE, "skipping punch for obj "LPX64", %Lu=%#Lx\n",
153                        lli->lli_smd->lsm_object_id, i_size_read(inode),
154                        i_size_read(inode));
155                 lov_stripe_unlock(lli->lli_smd);
156                 GOTO(out_unlock, 0);
157         }
158
159         obd_adjust_kms(ll_i2dtexp(inode), lli->lli_smd, i_size_read(inode), 1);
160         lov_stripe_unlock(lli->lli_smd);
161
162         if (unlikely((ll_i2sbi(inode)->ll_flags & LL_SBI_CHECKSUM) &&
163                      (i_size_read(inode) & ~CFS_PAGE_MASK))) {
164                 /* If the truncate leaves behind a partial page, update its
165                  * checksum. */
166                 struct page *page = find_get_page(inode->i_mapping,
167                                                   i_size_read(inode) >>
168                                                   CFS_PAGE_SHIFT);
169                 if (page != NULL) {
170                         struct ll_async_page *llap = llap_cast_private(page);
171                         if (llap != NULL) {
172                                 char *kaddr = kmap_atomic(page, KM_USER0);
173                                 llap->llap_checksum =
174                                         init_checksum(OSC_DEFAULT_CKSUM);
175                                 llap->llap_checksum =
176                                         compute_checksum(llap->llap_checksum,
177                                                          kaddr, CFS_PAGE_SIZE,
178                                                          OSC_DEFAULT_CKSUM);
179                                 kunmap_atomic(kaddr, KM_USER0);
180                         }
181                         page_cache_release(page);
182                 }
183         }
184
185         CDEBUG(D_INFO, "calling punch for "LPX64" (new size %Lu=%#Lx)\n",
186                lli->lli_smd->lsm_object_id, i_size_read(inode), i_size_read(inode));
187
188         oinfo.oi_md = lli->lli_smd;
189         oinfo.oi_policy.l_extent.start = i_size_read(inode);
190         oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
191         oinfo.oi_oa = &oa;
192         oa.o_id = lli->lli_smd->lsm_object_id;
193         oa.o_gr = lli->lli_smd->lsm_object_gr;
194         oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
195
196         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLMODE |
197                         OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
198                         OBD_MD_FLFID | OBD_MD_FLGENER);
199
200         ll_inode_size_unlock(inode, 0);
201
202         oinfo.oi_capa = ll_osscapa_get(inode, CAPA_OPC_OSS_TRUNC);
203         rc = obd_punch_rqset(ll_i2dtexp(inode), &oinfo, NULL);
204         ll_truncate_free_capa(oinfo.oi_capa);
205         if (rc)
206                 CERROR("obd_truncate fails (%d) ino %lu\n", rc, inode->i_ino);
207         else
208                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
209                               OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME);
210         EXIT;
211         return;
212
213  out_unlock:
214         ll_inode_size_unlock(inode, 0);
215 } /* ll_truncate */
216
217 int ll_prepare_write(struct file *file, struct page *page, unsigned from,
218                      unsigned to)
219 {
220         struct inode *inode = page->mapping->host;
221         struct ll_inode_info *lli = ll_i2info(inode);
222         struct lov_stripe_md *lsm = lli->lli_smd;
223         obd_off offset = ((obd_off)page->index) << CFS_PAGE_SHIFT;
224         struct obd_info oinfo = { { { 0 } } };
225         struct brw_page pga;
226         struct obdo oa;
227         struct ost_lvb lvb;
228         int rc = 0;
229         ENTRY;
230
231         LASSERT(PageLocked(page));
232         (void)llap_cast_private(page); /* assertion */
233
234         /* Check to see if we should return -EIO right away */
235         pga.pg = page;
236         pga.off = offset;
237         pga.count = CFS_PAGE_SIZE;
238         pga.flag = 0;
239
240         oa.o_mode = inode->i_mode;
241         oa.o_id = lsm->lsm_object_id;
242         oa.o_gr = lsm->lsm_object_gr;
243         oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | 
244                      OBD_MD_FLTYPE | OBD_MD_FLGROUP;
245         obdo_from_inode(&oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
246
247         oinfo.oi_oa = &oa;
248         oinfo.oi_md = lsm;
249         rc = obd_brw(OBD_BRW_CHECK, ll_i2dtexp(inode), &oinfo, 1, &pga, NULL);
250         if (rc)
251                 RETURN(rc);
252
253         if (PageUptodate(page)) {
254                 LL_CDEBUG_PAGE(D_PAGE, page, "uptodate\n");
255                 RETURN(0);
256         }
257
258         /* We're completely overwriting an existing page, so _don't_ set it up
259          * to date until commit_write */
260         if (from == 0 && to == CFS_PAGE_SIZE) {
261                 LL_CDEBUG_PAGE(D_PAGE, page, "full page write\n");
262                 POISON_PAGE(page, 0x11);
263                 RETURN(0);
264         }
265
266         /* If are writing to a new page, no need to read old data.  The extent
267          * locking will have updated the KMS, and for our purposes here we can
268          * treat it like i_size. */
269         lov_stripe_lock(lsm);
270         inode_init_lvb(inode, &lvb);
271         obd_merge_lvb(ll_i2dtexp(inode), lsm, &lvb, 1);
272         lov_stripe_unlock(lsm);
273         if (lvb.lvb_size <= offset) {
274                 char *kaddr = kmap_atomic(page, KM_USER0);
275                 LL_CDEBUG_PAGE(D_PAGE, page, "kms "LPU64" <= offset "LPU64"\n",
276                                lvb.lvb_size, offset);
277                 memset(kaddr, 0, CFS_PAGE_SIZE);
278                 kunmap_atomic(kaddr, KM_USER0);
279                 GOTO(prepare_done, rc = 0);
280         }
281
282         /* XXX could be an async ocp read.. read-ahead? */
283         rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
284         if (rc == 0) {
285                 /* bug 1598: don't clobber blksize */
286                 oa.o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
287                 obdo_refresh_inode(inode, &oa, oa.o_valid);
288         }
289
290         EXIT;
291  prepare_done:
292         if (rc == 0)
293                 SetPageUptodate(page);
294
295         return rc;
296 }
297
298 static int ll_ap_make_ready(void *data, int cmd)
299 {
300         struct ll_async_page *llap;
301         struct page *page;
302         ENTRY;
303
304         llap = LLAP_FROM_COOKIE(data);
305         page = llap->llap_page;
306
307         LASSERTF(!(cmd & OBD_BRW_READ), "cmd %x page %p ino %lu index %lu\n", cmd, page,
308                  page->mapping->host->i_ino, page->index);
309
310         /* we're trying to write, but the page is locked.. come back later */
311         if (TryLockPage(page))
312                 RETURN(-EAGAIN);
313
314         LASSERT(!PageWriteback(page));
315
316         /* if we left PageDirty we might get another writepage call
317          * in the future.  list walkers are bright enough
318          * to check page dirty so we can leave it on whatever list
319          * its on.  XXX also, we're called with the cli list so if
320          * we got the page cache list we'd create a lock inversion
321          * with the removepage path which gets the page lock then the
322          * cli lock */
323         LASSERTF(!PageWriteback(page),"cmd %x page %p ino %lu index %lu\n", cmd, page,
324                  page->mapping->host->i_ino, page->index);
325         clear_page_dirty_for_io(page);
326
327         /* This actually clears the dirty bit in the radix tree.*/
328         set_page_writeback(page);
329
330         LL_CDEBUG_PAGE(D_PAGE, page, "made ready\n");
331         page_cache_get(page);
332
333         RETURN(0);
334 }
335
336 /* We have two reasons for giving llite the opportunity to change the
337  * write length of a given queued page as it builds the RPC containing
338  * the page:
339  *
340  * 1) Further extending writes may have landed in the page cache
341  *    since a partial write first queued this page requiring us
342  *    to write more from the page cache.  (No further races are possible, since
343  *    by the time this is called, the page is locked.)
344  * 2) We might have raced with truncate and want to avoid performing
345  *    write RPCs that are just going to be thrown away by the
346  *    truncate's punch on the storage targets.
347  *
348  * The kms serves these purposes as it is set at both truncate and extending
349  * writes.
350  */
351 static int ll_ap_refresh_count(void *data, int cmd)
352 {
353         struct ll_inode_info *lli;
354         struct ll_async_page *llap;
355         struct lov_stripe_md *lsm;
356         struct page *page;
357         struct inode *inode;
358         struct ost_lvb lvb;
359         __u64 kms;
360         ENTRY;
361
362         /* readpage queues with _COUNT_STABLE, shouldn't get here. */
363         LASSERT(cmd != OBD_BRW_READ);
364
365         llap = LLAP_FROM_COOKIE(data);
366         page = llap->llap_page;
367         inode = page->mapping->host;
368         lli = ll_i2info(inode);
369         lsm = lli->lli_smd;
370
371         lov_stripe_lock(lsm);
372         inode_init_lvb(inode, &lvb);
373         obd_merge_lvb(ll_i2dtexp(inode), lsm, &lvb, 1);
374         kms = lvb.lvb_size;
375         lov_stripe_unlock(lsm);
376
377         /* catch race with truncate */
378         if (((__u64)page->index << CFS_PAGE_SHIFT) >= kms)
379                 return 0;
380
381         /* catch sub-page write at end of file */
382         if (((__u64)page->index << CFS_PAGE_SHIFT) + CFS_PAGE_SIZE > kms)
383                 return kms % CFS_PAGE_SIZE;
384
385         return CFS_PAGE_SIZE;
386 }
387
388 void ll_inode_fill_obdo(struct inode *inode, int cmd, struct obdo *oa)
389 {
390         struct lov_stripe_md *lsm;
391         obd_flag valid_flags;
392
393         lsm = ll_i2info(inode)->lli_smd;
394
395         oa->o_id = lsm->lsm_object_id;
396         oa->o_gr = lsm->lsm_object_gr;
397         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
398         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
399         if (cmd & OBD_BRW_WRITE) {
400                 oa->o_valid |= OBD_MD_FLEPOCH;
401                 oa->o_easize = ll_i2info(inode)->lli_ioepoch;
402
403                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
404                         OBD_MD_FLUID | OBD_MD_FLGID |
405                         OBD_MD_FLFID | OBD_MD_FLGENER;
406         }
407
408         obdo_from_inode(oa, inode, valid_flags);
409 }
410
411 static void ll_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
412 {
413         struct ll_async_page *llap;
414         ENTRY;
415
416         llap = LLAP_FROM_COOKIE(data);
417         ll_inode_fill_obdo(llap->llap_page->mapping->host, cmd, oa);
418
419         EXIT;
420 }
421
422 static void ll_ap_update_obdo(void *data, int cmd, struct obdo *oa,
423                               obd_valid valid)
424 {
425         struct ll_async_page *llap;
426         ENTRY;
427
428         llap = LLAP_FROM_COOKIE(data);
429         obdo_from_inode(oa, llap->llap_page->mapping->host, valid);
430
431         EXIT;
432 }
433
434 static struct obd_capa *ll_ap_lookup_capa(void *data, int cmd)
435 {
436         struct ll_async_page *llap = LLAP_FROM_COOKIE(data);
437         int opc = cmd & OBD_BRW_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
438
439         return ll_osscapa_get(llap->llap_page->mapping->host, opc);
440 }
441
442 static struct obd_async_page_ops ll_async_page_ops = {
443         .ap_make_ready =        ll_ap_make_ready,
444         .ap_refresh_count =     ll_ap_refresh_count,
445         .ap_fill_obdo =         ll_ap_fill_obdo,
446         .ap_update_obdo =       ll_ap_update_obdo,
447         .ap_completion =        ll_ap_completion,
448         .ap_lookup_capa =       ll_ap_lookup_capa,
449 };
450
451 struct ll_async_page *llap_cast_private(struct page *page)
452 {
453         struct ll_async_page *llap = (struct ll_async_page *)page_private(page);
454
455         LASSERTF(llap == NULL || llap->llap_magic == LLAP_MAGIC,
456                  "page %p private %lu gave magic %d which != %d\n",
457                  page, page_private(page), llap->llap_magic, LLAP_MAGIC);
458
459         return llap;
460 }
461
462 /* Try to shrink the page cache for the @sbi filesystem by 1/@shrink_fraction.
463  *
464  * There is an llap attached onto every page in lustre, linked off @sbi.
465  * We add an llap to the list so we don't lose our place during list walking.
466  * If llaps in the list are being moved they will only move to the end
467  * of the LRU, and we aren't terribly interested in those pages here (we
468  * start at the beginning of the list where the least-used llaps are.
469  */
470 int llap_shrink_cache(struct ll_sb_info *sbi, int shrink_fraction)
471 {
472         struct ll_async_page *llap, dummy_llap = { .llap_magic = 0xd11ad11a };
473         unsigned long total, want, count = 0;
474
475         total = sbi->ll_async_page_count;
476
477         /* There can be a large number of llaps (600k or more in a large
478          * memory machine) so the VM 1/6 shrink ratio is likely too much.
479          * Since we are freeing pages also, we don't necessarily want to
480          * shrink so much.  Limit to 40MB of pages + llaps per call. */
481         if (shrink_fraction == 0)
482                 want = sbi->ll_async_page_count - sbi->ll_async_page_max + 32;
483         else
484                 want = (total + shrink_fraction - 1) / shrink_fraction;
485
486         if (want > 40 << (20 - CFS_PAGE_SHIFT))
487                 want = 40 << (20 - CFS_PAGE_SHIFT);
488
489         CDEBUG(D_CACHE, "shrinking %lu of %lu pages (1/%d)\n",
490                want, total, shrink_fraction);
491
492         spin_lock(&sbi->ll_lock);
493         list_add(&dummy_llap.llap_pglist_item, &sbi->ll_pglist);
494
495         while (--total >= 0 && count < want) {
496                 struct page *page;
497                 int keep;
498
499                 if (unlikely(need_resched())) {
500                         spin_unlock(&sbi->ll_lock);
501                         cond_resched();
502                         spin_lock(&sbi->ll_lock);
503                 }
504
505                 llap = llite_pglist_next_llap(sbi,&dummy_llap.llap_pglist_item);
506                 list_del_init(&dummy_llap.llap_pglist_item);
507                 if (llap == NULL)
508                         break;
509
510                 page = llap->llap_page;
511                 LASSERT(page != NULL);
512
513                 list_add(&dummy_llap.llap_pglist_item, &llap->llap_pglist_item);
514
515                 /* Page needs/undergoing IO */
516                 if (TryLockPage(page)) {
517                         LL_CDEBUG_PAGE(D_PAGE, page, "can't lock\n");
518                         continue;
519                 }
520
521                keep = (llap->llap_write_queued || PageDirty(page) ||
522                       PageWriteback(page) || (!PageUptodate(page) &&
523                       llap->llap_origin != LLAP_ORIGIN_READAHEAD));
524
525                 LL_CDEBUG_PAGE(D_PAGE, page,"%s LRU page: %s%s%s%s%s origin %s\n",
526                                keep ? "keep" : "drop",
527                                llap->llap_write_queued ? "wq " : "",
528                                PageDirty(page) ? "pd " : "",
529                                PageUptodate(page) ? "" : "!pu ",
530                                PageWriteback(page) ? "wb" : "",
531                                llap->llap_defer_uptodate ? "" : "!du",
532                                llap_origins[llap->llap_origin]);
533
534                 /* If page is dirty or undergoing IO don't discard it */
535                 if (keep) {
536                         unlock_page(page);
537                         continue;
538                 }
539
540                 page_cache_get(page);
541                 spin_unlock(&sbi->ll_lock);
542
543                 if (page->mapping != NULL) {
544                         ll_teardown_mmaps(page->mapping,
545                                          (__u64)page->index << CFS_PAGE_SHIFT,
546                                          ((__u64)page->index << CFS_PAGE_SHIFT)|
547                                           ~CFS_PAGE_MASK);
548                         if (!PageDirty(page) && !page_mapped(page)) {
549                                 ll_ra_accounting(llap, page->mapping);
550                                 ll_truncate_complete_page(page);
551                                 ++count;
552                         } else {
553                                 LL_CDEBUG_PAGE(D_PAGE, page, "Not dropping page"
554                                                              " because it is "
555                                                              "%s\n",
556                                                               PageDirty(page)?
557                                                               "dirty":"mapped");
558                         }
559                 }
560                 unlock_page(page);
561                 page_cache_release(page);
562
563                 spin_lock(&sbi->ll_lock);
564         }
565         list_del(&dummy_llap.llap_pglist_item);
566         spin_unlock(&sbi->ll_lock);
567
568         CDEBUG(D_CACHE, "shrank %lu/%lu and left %lu unscanned\n",
569                count, want, total);
570
571         return count;
572 }
573
574 struct ll_async_page *llap_from_page(struct page *page, unsigned origin)
575 {
576         struct ll_async_page *llap;
577         struct obd_export *exp;
578         struct inode *inode = page->mapping->host;
579         struct ll_sb_info *sbi;
580         int rc;
581         ENTRY;
582
583         if (!inode) {
584                 static int triggered;
585
586                 if (!triggered) {
587                         LL_CDEBUG_PAGE(D_ERROR, page, "Bug 10047. Wrong anon "
588                                        "page received\n");
589                         libcfs_debug_dumpstack(NULL);
590                         triggered = 1;
591                 }
592                 RETURN(ERR_PTR(-EINVAL));
593         }
594         sbi = ll_i2sbi(inode);
595         LASSERT(ll_async_page_slab);
596         LASSERTF(origin < LLAP__ORIGIN_MAX, "%u\n", origin);
597
598         llap = llap_cast_private(page);
599         if (llap != NULL) {
600                 /* move to end of LRU list, except when page is just about to
601                  * die */
602                 if (origin != LLAP_ORIGIN_REMOVEPAGE) {
603                         spin_lock(&sbi->ll_lock);
604                         sbi->ll_pglist_gen++;
605                         list_del_init(&llap->llap_pglist_item);
606                         list_add_tail(&llap->llap_pglist_item, &sbi->ll_pglist);
607                         spin_unlock(&sbi->ll_lock);
608                 }
609                 GOTO(out, llap);
610         }
611
612         exp = ll_i2dtexp(page->mapping->host);
613         if (exp == NULL)
614                 RETURN(ERR_PTR(-EINVAL));
615
616         /* limit the number of lustre-cached pages */
617         if (sbi->ll_async_page_count >= sbi->ll_async_page_max)
618                 llap_shrink_cache(sbi, 0);
619
620         OBD_SLAB_ALLOC(llap, ll_async_page_slab, CFS_ALLOC_STD,
621                        ll_async_page_slab_size);
622         if (llap == NULL)
623                 RETURN(ERR_PTR(-ENOMEM));
624         llap->llap_magic = LLAP_MAGIC;
625         llap->llap_cookie = (void *)llap + size_round(sizeof(*llap));
626
627         rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd, NULL, page,
628                                  (obd_off)page->index << CFS_PAGE_SHIFT,
629                                  &ll_async_page_ops, llap, &llap->llap_cookie);
630         if (rc) {
631                 OBD_SLAB_FREE(llap, ll_async_page_slab,
632                               ll_async_page_slab_size);
633                 RETURN(ERR_PTR(rc));
634         }
635
636         CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap,
637                page, llap->llap_cookie, (obd_off)page->index << CFS_PAGE_SHIFT);
638         /* also zeroing the PRIVBITS low order bitflags */
639         __set_page_ll_data(page, llap);
640         llap->llap_page = page;
641         spin_lock(&sbi->ll_lock);
642         sbi->ll_pglist_gen++;
643         sbi->ll_async_page_count++;
644         list_add_tail(&llap->llap_pglist_item, &sbi->ll_pglist);
645         INIT_LIST_HEAD(&llap->llap_pending_write);
646         spin_unlock(&sbi->ll_lock);
647
648  out:
649         if (unlikely(sbi->ll_flags & LL_SBI_CHECKSUM)) {
650                 __u32 csum;
651                 char *kaddr = kmap_atomic(page, KM_USER0);
652                 csum = init_checksum(OSC_DEFAULT_CKSUM);
653                 csum = compute_checksum(csum, kaddr, CFS_PAGE_SIZE,
654                                         OSC_DEFAULT_CKSUM);
655                 kunmap_atomic(kaddr, KM_USER0);
656                 if (origin == LLAP_ORIGIN_READAHEAD ||
657                     origin == LLAP_ORIGIN_READPAGE) {
658                         llap->llap_checksum = 0;
659                 } else if (origin == LLAP_ORIGIN_COMMIT_WRITE ||
660                            llap->llap_checksum == 0) {
661                         llap->llap_checksum = csum;
662                         CDEBUG(D_PAGE, "page %p cksum %x\n", page, csum);
663                 } else if (llap->llap_checksum == csum) {
664                         /* origin == LLAP_ORIGIN_WRITEPAGE */
665                         CDEBUG(D_PAGE, "page %p cksum %x confirmed\n",
666                                page, csum);
667                 } else {
668                         /* origin == LLAP_ORIGIN_WRITEPAGE */
669                         LL_CDEBUG_PAGE(D_ERROR, page, "old cksum %x != new "
670                                        "%x!\n", llap->llap_checksum, csum);
671                 }
672         }
673
674         llap->llap_origin = origin;
675         RETURN(llap);
676 }
677
678 static int queue_or_sync_write(struct obd_export *exp, struct inode *inode,
679                                struct ll_async_page *llap,
680                                unsigned to, obd_flag async_flags)
681 {
682         unsigned long size_index = i_size_read(inode) >> CFS_PAGE_SHIFT;
683         struct obd_io_group *oig;
684         struct ll_sb_info *sbi = ll_i2sbi(inode);
685         int rc, noquot = llap->llap_ignore_quota ? OBD_BRW_NOQUOTA : 0;
686         ENTRY;
687
688         /* _make_ready only sees llap once we've unlocked the page */
689         llap->llap_write_queued = 1;
690         rc = obd_queue_async_io(exp, ll_i2info(inode)->lli_smd, NULL,
691                                 llap->llap_cookie, OBD_BRW_WRITE | noquot,
692                                 0, 0, 0, async_flags);
693         if (rc == 0) {
694                 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "write queued\n");
695                 GOTO(out, 0);
696         }
697
698         llap->llap_write_queued = 0;
699         /* Do not pass llap here as it is sync write. */
700         llap_write_pending(inode, NULL);
701         
702         rc = oig_init(&oig);
703         if (rc)
704                 GOTO(out, rc);
705
706         /* make full-page requests if we are not at EOF (bug 4410) */
707         if (to != CFS_PAGE_SIZE && llap->llap_page->index < size_index) {
708                 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
709                                "sync write before EOF: size_index %lu, to %d\n",
710                                size_index, to);
711                 to = CFS_PAGE_SIZE;
712         } else if (to != CFS_PAGE_SIZE && llap->llap_page->index == size_index) {
713                 int size_to = i_size_read(inode) & ~CFS_PAGE_MASK;
714                 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
715                                "sync write at EOF: size_index %lu, to %d/%d\n",
716                                size_index, to, size_to);
717                 if (to < size_to)
718                         to = size_to;
719         }
720
721         /* compare the checksum once before the page leaves llite */
722         if (unlikely((sbi->ll_flags & LL_SBI_CHECKSUM) &&
723                      llap->llap_checksum != 0)) {
724                 __u32 csum;
725                 struct page *page = llap->llap_page;
726                 char *kaddr = kmap_atomic(page, KM_USER0);
727                 csum = init_checksum(OSC_DEFAULT_CKSUM);
728                 csum = compute_checksum(csum, kaddr, CFS_PAGE_SIZE,
729                                         OSC_DEFAULT_CKSUM);
730                 kunmap_atomic(kaddr, KM_USER0);
731                 if (llap->llap_checksum == csum) {
732                         CDEBUG(D_PAGE, "page %p cksum %x confirmed\n",
733                                page, csum);
734                 } else {
735                         CERROR("page %p old cksum %x != new cksum %x!\n",
736                                page, llap->llap_checksum, csum);
737                 }
738         }
739
740         rc = obd_queue_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig,
741                                 llap->llap_cookie, OBD_BRW_WRITE | noquot,
742                                 0, to, 0, ASYNC_READY | ASYNC_URGENT |
743                                 ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
744         if (rc)
745                 GOTO(free_oig, rc);
746
747         rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
748         if (rc)
749                 GOTO(free_oig, rc);
750
751         rc = oig_wait(oig);
752
753         if (!rc && async_flags & ASYNC_READY) {
754                 unlock_page(llap->llap_page);
755                 if (PageWriteback(llap->llap_page)) {
756                         end_page_writeback(llap->llap_page);
757                 }
758         }
759
760         if (rc == 0 && llap_write_complete(inode, llap))
761                 ll_queue_done_writing(inode, 0);
762
763         LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "sync write returned %d\n", rc);
764
765 free_oig:
766         oig_release(oig);
767 out:
768         RETURN(rc);
769 }
770
771 /* update our write count to account for i_size increases that may have
772  * happened since we've queued the page for io. */
773
774 /* be careful not to return success without setting the page Uptodate or
775  * the next pass through prepare_write will read in stale data from disk. */
776 int ll_commit_write(struct file *file, struct page *page, unsigned from,
777                     unsigned to)
778 {
779         struct inode *inode = page->mapping->host;
780         struct ll_inode_info *lli = ll_i2info(inode);
781         struct lov_stripe_md *lsm = lli->lli_smd;
782         struct obd_export *exp;
783         struct ll_async_page *llap;
784         loff_t size;
785         int rc = 0;
786         ENTRY;
787
788         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
789         LASSERT(inode == file->f_dentry->d_inode);
790         LASSERT(PageLocked(page));
791
792         CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
793                inode, page, from, to, page->index);
794
795         llap = llap_from_page(page, LLAP_ORIGIN_COMMIT_WRITE);
796         if (IS_ERR(llap))
797                 RETURN(PTR_ERR(llap));
798
799         exp = ll_i2dtexp(inode);
800         if (exp == NULL)
801                 RETURN(-EINVAL);
802
803         llap->llap_ignore_quota = capable(CAP_SYS_RESOURCE);
804
805         /*
806          * queue a write for some time in the future the first time we
807          * dirty the page.
808          *
809          * This is different from what other file systems do: they usually
810          * just mark page (and some of its buffers) dirty and rely on
811          * balance_dirty_pages() to start a write-back. Lustre wants write-back
812          * to be started earlier for the following reasons:
813          *
814          *     (1) with a large number of clients we need to limit the amount
815          *     of cached data on the clients a lot;
816          *
817          *     (2) large compute jobs generally want compute-only then io-only
818          *     and the IO should complete as quickly as possible;
819          *
820          *     (3) IO is batched up to the RPC size and is async until the
821          *     client max cache is hit
822          *     (/proc/fs/lustre/osc/OSC.../max_dirty_mb)
823          *
824          */
825         if (!PageDirty(page)) {
826                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRTY_MISSES, 1);
827
828                 rc = queue_or_sync_write(exp, inode, llap, to, 0);
829                 if (rc)
830                         GOTO(out, rc);
831         } else {
832                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRTY_HITS, 1);
833         }
834
835         /* put the page in the page cache, from now on ll_removepage is
836          * responsible for cleaning up the llap.
837          * only set page dirty when it's queued to be write out */
838         if (llap->llap_write_queued)
839                 set_page_dirty(page);
840
841 out:
842         size = (((obd_off)page->index) << CFS_PAGE_SHIFT) + to;
843         ll_inode_size_lock(inode, 0);
844         if (rc == 0) {
845                 lov_stripe_lock(lsm);
846                 obd_adjust_kms(exp, lsm, size, 0);
847                 lov_stripe_unlock(lsm);
848                 if (size > i_size_read(inode))
849                         i_size_write(inode, size);
850                 SetPageUptodate(page);
851         } else if (size > i_size_read(inode)) {
852                 /* this page beyond the pales of i_size, so it can't be
853                  * truncated in ll_p_r_e during lock revoking. we must
854                  * teardown our book-keeping here. */
855                 ll_removepage(page);
856         }
857         ll_inode_size_unlock(inode, 0);
858         RETURN(rc);
859 }
860
861 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
862 {
863         struct ll_ra_info *ra = &sbi->ll_ra_info;
864         unsigned long ret;
865         ENTRY;
866
867         spin_lock(&sbi->ll_lock);
868         ret = min(ra->ra_max_pages - ra->ra_cur_pages, len);
869         ra->ra_cur_pages += ret;
870         spin_unlock(&sbi->ll_lock);
871
872         RETURN(ret);
873 }
874
875 static void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
876 {
877         struct ll_ra_info *ra = &sbi->ll_ra_info;
878         spin_lock(&sbi->ll_lock);
879         LASSERTF(ra->ra_cur_pages >= len, "r_c_p %lu len %lu\n",
880                  ra->ra_cur_pages, len);
881         ra->ra_cur_pages -= len;
882         spin_unlock(&sbi->ll_lock);
883 }
884
885 /* called for each page in a completed rpc.*/
886 int ll_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
887 {
888         struct ll_async_page *llap;
889         struct page *page;
890         int ret = 0;
891         ENTRY;
892
893         llap = LLAP_FROM_COOKIE(data);
894         page = llap->llap_page;
895         LASSERT(PageLocked(page));
896         LASSERT(CheckWriteback(page,cmd));
897         
898         LL_CDEBUG_PAGE(D_PAGE, page, "completing cmd %d with %d\n", cmd, rc);
899
900         if (cmd & OBD_BRW_READ && llap->llap_defer_uptodate)
901                 ll_ra_count_put(ll_i2sbi(page->mapping->host), 1);
902
903         if (rc == 0)  {
904                 if (cmd & OBD_BRW_READ) {
905                         if (!llap->llap_defer_uptodate)
906                                 SetPageUptodate(page);
907                 } else {
908                         llap->llap_write_queued = 0;
909                 }
910                 ClearPageError(page);
911         } else {
912                 if (cmd & OBD_BRW_READ) {
913                         llap->llap_defer_uptodate = 0;
914                 }
915                 SetPageError(page);
916                 if (rc == -ENOSPC)
917                         set_bit(AS_ENOSPC, &page->mapping->flags);
918                 else
919                         set_bit(AS_EIO, &page->mapping->flags);
920         }
921
922         unlock_page(page);
923
924         if (cmd & OBD_BRW_WRITE) {
925                 /* Only rc == 0, write succeed, then this page could be deleted
926                  * from the pending_writing list 
927                  */
928                 if (rc == 0 && llap_write_complete(page->mapping->host, llap))
929                         ll_queue_done_writing(page->mapping->host, 0);
930         }
931
932         if (PageWriteback(page)) {
933                 end_page_writeback(page);
934         }
935         page_cache_release(page);
936
937         RETURN(ret);
938 }
939
940 /* the kernel calls us here when a page is unhashed from the page cache.
941  * the page will be locked and the kernel is holding a spinlock, so
942  * we need to be careful.  we're just tearing down our book-keeping
943  * here. */
944 void ll_removepage(struct page *page)
945 {
946         struct inode *inode = page->mapping->host;
947         struct obd_export *exp;
948         struct ll_async_page *llap;
949         struct ll_sb_info *sbi = ll_i2sbi(inode);
950         int rc;
951         ENTRY;
952
953         LASSERT(!in_interrupt());
954
955         /* sync pages or failed read pages can leave pages in the page
956          * cache that don't have our data associated with them anymore */
957         if (page_private(page) == 0) {
958                 EXIT;
959                 return;
960         }
961
962         LL_CDEBUG_PAGE(D_PAGE, page, "being evicted\n");
963
964         exp = ll_i2dtexp(inode);
965         if (exp == NULL) {
966                 CERROR("page %p ind %lu gave null export\n", page, page->index);
967                 EXIT;
968                 return;
969         }
970
971         llap = llap_from_page(page, LLAP_ORIGIN_REMOVEPAGE);
972         if (IS_ERR(llap)) {
973                 CERROR("page %p ind %lu couldn't find llap: %ld\n", page,
974                        page->index, PTR_ERR(llap));
975                 EXIT;
976                 return;
977         }
978
979         if (llap_write_complete(inode, llap))
980                 ll_queue_done_writing(inode, 0);
981
982         rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL,
983                                      llap->llap_cookie);
984         if (rc != 0)
985                 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
986
987         /* this unconditional free is only safe because the page lock
988          * is providing exclusivity to memory pressure/truncate/writeback..*/
989         __clear_page_ll_data(page);
990
991         spin_lock(&sbi->ll_lock);
992         if (!list_empty(&llap->llap_pglist_item))
993                 list_del_init(&llap->llap_pglist_item);
994         sbi->ll_pglist_gen++;
995         sbi->ll_async_page_count--;
996         spin_unlock(&sbi->ll_lock);
997         OBD_SLAB_FREE(llap, ll_async_page_slab, ll_async_page_slab_size);
998         EXIT;
999 }
1000
1001 static int ll_page_matches(struct page *page, int fd_flags)
1002 {
1003         struct lustre_handle match_lockh = {0};
1004         struct inode *inode = page->mapping->host;
1005         ldlm_policy_data_t page_extent;
1006         int flags, matches;
1007         ENTRY;
1008
1009         if (unlikely(fd_flags & LL_FILE_GROUP_LOCKED))
1010                 RETURN(1);
1011
1012         page_extent.l_extent.start = (__u64)page->index << CFS_PAGE_SHIFT;
1013         page_extent.l_extent.end =
1014                 page_extent.l_extent.start + CFS_PAGE_SIZE - 1;
1015         flags = LDLM_FL_TEST_LOCK | LDLM_FL_BLOCK_GRANTED;
1016         if (!(fd_flags & LL_FILE_READAHEAD))
1017                 flags |= LDLM_FL_CBPENDING;
1018         matches = obd_match(ll_i2sbi(inode)->ll_dt_exp,
1019                             ll_i2info(inode)->lli_smd, LDLM_EXTENT,
1020                             &page_extent, LCK_PR | LCK_PW, &flags, inode,
1021                             &match_lockh);
1022         RETURN(matches);
1023 }
1024
1025 static int ll_issue_page_read(struct obd_export *exp,
1026                               struct ll_async_page *llap,
1027                               struct obd_io_group *oig, int defer)
1028 {
1029         struct page *page = llap->llap_page;
1030         int rc;
1031
1032         page_cache_get(page);
1033         llap->llap_defer_uptodate = defer;
1034         llap->llap_ra_used = 0;
1035         rc = obd_queue_group_io(exp, ll_i2info(page->mapping->host)->lli_smd,
1036                                 NULL, oig, llap->llap_cookie, OBD_BRW_READ, 0,
1037                                 CFS_PAGE_SIZE, 0, ASYNC_COUNT_STABLE |
1038                                                   ASYNC_READY | ASYNC_URGENT);
1039         if (rc) {
1040                 LL_CDEBUG_PAGE(D_ERROR, page, "read queue failed: rc %d\n", rc);
1041                 page_cache_release(page);
1042         }
1043         RETURN(rc);
1044 }
1045
1046 static void ll_ra_stats_inc_unlocked(struct ll_ra_info *ra, enum ra_stat which)
1047 {
1048         LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
1049         ra->ra_stats[which]++;
1050 }
1051
1052 static void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
1053 {
1054         struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
1055         struct ll_ra_info *ra = &ll_i2sbi(mapping->host)->ll_ra_info;
1056
1057         spin_lock(&sbi->ll_lock);
1058         ll_ra_stats_inc_unlocked(ra, which);
1059         spin_unlock(&sbi->ll_lock);
1060 }
1061
1062 void ll_ra_accounting(struct ll_async_page *llap, struct address_space *mapping)
1063 {
1064         if (!llap->llap_defer_uptodate || llap->llap_ra_used)
1065                 return;
1066
1067         ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
1068 }
1069
1070 #define RAS_CDEBUG(ras) \
1071         CDEBUG(D_READA,                                                      \
1072                "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu"    \
1073                "csr %lu sf %lu sp %lu sl %lu \n",                            \
1074                ras->ras_last_readpage, ras->ras_consecutive_requests,        \
1075                ras->ras_consecutive_pages, ras->ras_window_start,            \
1076                ras->ras_window_len, ras->ras_next_readahead,                 \
1077                ras->ras_requests, ras->ras_request_index,                    \
1078                ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
1079                ras->ras_stride_pages, ras->ras_stride_length)
1080
1081 static int index_in_window(unsigned long index, unsigned long point,
1082                            unsigned long before, unsigned long after)
1083 {
1084         unsigned long start = point - before, end = point + after;
1085
1086         if (start > point)
1087                start = 0;
1088         if (end < point)
1089                end = ~0;
1090
1091         return start <= index && index <= end;
1092 }
1093
1094 static struct ll_readahead_state *ll_ras_get(struct file *f)
1095 {
1096         struct ll_file_data       *fd;
1097
1098         fd = LUSTRE_FPRIVATE(f);
1099         return &fd->fd_ras;
1100 }
1101
1102 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
1103 {
1104         struct ll_readahead_state *ras;
1105
1106         ras = ll_ras_get(f);
1107
1108         spin_lock(&ras->ras_lock);
1109         ras->ras_requests++;
1110         ras->ras_request_index = 0;
1111         ras->ras_consecutive_requests++;
1112         rar->lrr_reader = current;
1113
1114         list_add(&rar->lrr_linkage, &ras->ras_read_beads);
1115         spin_unlock(&ras->ras_lock);
1116 }
1117
1118 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
1119 {
1120         struct ll_readahead_state *ras;
1121
1122         ras = ll_ras_get(f);
1123
1124         spin_lock(&ras->ras_lock);
1125         list_del_init(&rar->lrr_linkage);
1126         spin_unlock(&ras->ras_lock);
1127 }
1128
1129 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
1130 {
1131         struct ll_ra_read *scan;
1132
1133         list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
1134                 if (scan->lrr_reader == current)
1135                         return scan;
1136         }
1137         return NULL;
1138 }
1139
1140 struct ll_ra_read *ll_ra_read_get(struct file *f)
1141 {
1142         struct ll_readahead_state *ras;
1143         struct ll_ra_read         *bead;
1144
1145         ras = ll_ras_get(f);
1146
1147         spin_lock(&ras->ras_lock);
1148         bead = ll_ra_read_get_locked(ras);
1149         spin_unlock(&ras->ras_lock);
1150         return bead;
1151 }
1152
1153 static int ll_read_ahead_page(struct obd_export *exp, struct obd_io_group *oig, 
1154                               int index, struct address_space *mapping)
1155 {
1156         struct ll_async_page *llap;
1157         struct page *page;
1158         unsigned int gfp_mask = 0;
1159         int rc = 0;       
1160  
1161         gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
1162 #ifdef __GFP_NOWARN
1163         gfp_mask |= __GFP_NOWARN;
1164 #endif
1165         page = grab_cache_page_nowait_gfp(mapping, index, gfp_mask);
1166         if (page == NULL) {
1167                 ll_ra_stats_inc(mapping, RA_STAT_FAILED_GRAB_PAGE);
1168                 CDEBUG(D_READA, "g_c_p_n failed\n");
1169                 return 0;
1170         }
1171
1172         /* Check if page was truncated or reclaimed */
1173         if (page->mapping != mapping) {
1174                 ll_ra_stats_inc(mapping, RA_STAT_WRONG_GRAB_PAGE);
1175                 CDEBUG(D_READA, "g_c_p_n returned invalid page\n");
1176                 GOTO(unlock_page, rc = 0);      
1177         }
1178
1179         /* we do this first so that we can see the page in the /proc
1180          * accounting */
1181         llap = llap_from_page(page, LLAP_ORIGIN_READAHEAD);
1182         if (IS_ERR(llap) || llap->llap_defer_uptodate) {
1183                 if (PTR_ERR(llap) == -ENOLCK) {
1184                         ll_ra_stats_inc(mapping, RA_STAT_FAILED_MATCH);
1185                         CDEBUG(D_READA | D_PAGE,
1186                                "Adding page to cache failed index "
1187                                 "%d\n", index);
1188                                 CDEBUG(D_READA, "nolock page\n");
1189                                 GOTO(unlock_page, rc = -ENOLCK);
1190                 }
1191                 CDEBUG(D_READA, "read-ahead page\n");
1192                 GOTO(unlock_page, rc = 0);      
1193         }
1194
1195         /* skip completed pages */
1196         if (Page_Uptodate(page))
1197                 GOTO(unlock_page, rc = 0);      
1198         
1199         /* bail out when we hit the end of the lock. */
1200         rc = ll_issue_page_read(exp, llap, oig, 1);
1201         if (rc == 0) {
1202                 LL_CDEBUG_PAGE(D_READA | D_PAGE, page, "started read-ahead\n");
1203                 rc = 1;
1204         } else {
1205 unlock_page:    
1206                 unlock_page(page);
1207                 LL_CDEBUG_PAGE(D_READA | D_PAGE, page, "skipping read-ahead\n");
1208         }
1209         page_cache_release(page);
1210         return rc;
1211 }
1212
1213 /* ra_io_arg will be filled in the beginning of ll_readahead with 
1214  * ras_lock, then the following ll_read_ahead_pages will read RA 
1215  * pages according to this arg, all the items in this structure are
1216  * counted by page index.
1217  */
1218 struct ra_io_arg {
1219         unsigned long ria_start;  /* start offset of read-ahead*/
1220         unsigned long ria_end;    /* end offset of read-ahead*/ 
1221         /* If stride read pattern is detected, ria_stoff means where
1222          * stride read is started. Note: for normal read-ahead, the
1223          * value here is meaningless, and also it will not be accessed*/ 
1224         pgoff_t ria_stoff;
1225         /* ria_length and ria_pages are the length and pages length in the
1226          * stride I/O mode. And they will also be used to check whether
1227          * it is stride I/O read-ahead in the read-ahead pages*/ 
1228         unsigned long ria_length;
1229         unsigned long ria_pages;
1230 };
1231
1232 #define RIA_DEBUG(ria)                                                \
1233         CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n",       \
1234         ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
1235         ria->ria_pages)
1236
1237 #define RAS_INCREASE_STEP (1024 * 1024 >> CFS_PAGE_SHIFT)
1238
1239 static inline int stride_io_mode(struct ll_readahead_state *ras)
1240 {
1241         return ras->ras_consecutive_stride_requests > 1; 
1242 }
1243
1244 /* The function calculates how much pages will be read in 
1245  * [off, off + length], which will be read by stride I/O mode,
1246  * stride_offset = st_off, stride_lengh = st_len, 
1247  * stride_pages = st_pgs
1248  */  
1249 static unsigned long
1250 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs, 
1251                 unsigned long off, unsigned length)
1252 {
1253         unsigned long cont_len = st_off > off ?  st_off - off : 0;
1254         unsigned long stride_len = length + off > st_off ?
1255                            length + off + 1 - st_off : 0;
1256         unsigned long left, pg_count;
1257
1258         if (st_len == 0 || length == 0)
1259                 return length;
1260
1261         left = do_div(stride_len, st_len);
1262         left = min(left, st_pgs);
1263
1264         pg_count = left + stride_len * st_pgs + cont_len;
1265
1266         LASSERT(pg_count >= left);
1267
1268         CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %u"
1269                "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count); 
1270
1271         return pg_count;
1272 }
1273
1274 static int ria_page_count(struct ra_io_arg *ria)
1275 {
1276         __u64 length = ria->ria_end >= ria->ria_start ? 
1277                        ria->ria_end - ria->ria_start + 1 : 0;
1278
1279         return stride_pg_count(ria->ria_stoff, ria->ria_length, 
1280                                ria->ria_pages, ria->ria_start,
1281                                length);
1282 }
1283
1284 /*Check whether the index is in the defined ra-window */
1285 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
1286 {
1287         /* If ria_length == ria_pages, it means non-stride I/O mode,
1288          * idx should always inside read-ahead window in this case 
1289          * For stride I/O mode, just check whether the idx is inside
1290          * the ria_pages. */
1291         return ria->ria_length == 0 || ria->ria_length == ria->ria_pages || 
1292                (idx - ria->ria_stoff) % ria->ria_length < ria->ria_pages;
1293 }
1294
1295 static int ll_read_ahead_pages(struct obd_export *exp,
1296                                struct obd_io_group *oig,
1297                                struct ra_io_arg *ria,   
1298                                unsigned long *reserved_pages,
1299                                struct address_space *mapping,
1300                                unsigned long *ra_end)
1301 {
1302         int rc, count = 0, stride_ria;
1303         unsigned long page_idx;
1304
1305         LASSERT(ria != NULL);
1306         RIA_DEBUG(ria);
1307       
1308         stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
1309         for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
1310                         *reserved_pages > 0; page_idx++) {
1311                 if (ras_inside_ra_window(page_idx, ria)) {
1312                         /* If the page is inside the read-ahead window*/
1313                         rc = ll_read_ahead_page(exp, oig, page_idx, mapping);
1314                         if (rc == 1) {
1315                                 (*reserved_pages)--;
1316                                 count ++;
1317                         } else if (rc == -ENOLCK)
1318                                 break;
1319                 } else if (stride_ria) {
1320                         /* If it is not in the read-ahead window, and it is 
1321                          * read-ahead mode, then check whether it should skip
1322                          * the stride gap */  
1323                         pgoff_t offset;
1324                         /* FIXME: This assertion only is valid when it is for 
1325                          * forward read-ahead, it will be fixed when backward 
1326                          * read-ahead is implemented */
1327                         LASSERTF(page_idx > ria->ria_stoff, "since %lu in the"
1328                                 " gap of ra window,it should bigger than stride"
1329                                 " offset %lu \n", page_idx, ria->ria_stoff);
1330                         
1331                         offset = page_idx - ria->ria_stoff;
1332                         offset = offset % (ria->ria_length);
1333                         if (offset > ria->ria_pages) {
1334                                 page_idx += ria->ria_length - offset;
1335                                 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx, 
1336                                        ria->ria_length - offset);
1337                                 continue;
1338                         }
1339                 }
1340         }
1341         *ra_end = page_idx;
1342         return count;
1343 }
1344
1345 static int ll_readahead(struct ll_readahead_state *ras,
1346                          struct obd_export *exp, struct address_space *mapping,
1347                          struct obd_io_group *oig, int flags)
1348 {
1349         unsigned long start = 0, end = 0, reserved;
1350         unsigned long ra_end, len; 
1351         struct inode *inode;
1352         struct lov_stripe_md *lsm;
1353         struct ll_ra_read *bead;
1354         struct ost_lvb lvb;
1355         struct ra_io_arg ria = { 0 }; 
1356         int ret = 0; 
1357         __u64 kms;
1358         ENTRY;
1359
1360         inode = mapping->host;
1361         lsm = ll_i2info(inode)->lli_smd;
1362
1363         lov_stripe_lock(lsm);
1364         inode_init_lvb(inode, &lvb);
1365         obd_merge_lvb(ll_i2dtexp(inode), lsm, &lvb, 1);
1366         kms = lvb.lvb_size;
1367         lov_stripe_unlock(lsm);
1368         if (kms == 0) {
1369                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
1370                 RETURN(0);
1371         }
1372
1373         spin_lock(&ras->ras_lock);
1374         bead = ll_ra_read_get_locked(ras);
1375         /* Enlarge the RA window to encompass the full read */
1376         if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
1377             bead->lrr_start + bead->lrr_count) {
1378                 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
1379                                       ras->ras_window_start;
1380         }
1381         /* Reserve a part of the read-ahead window that we'll be issuing */
1382         if (ras->ras_window_len) {
1383                 start = ras->ras_next_readahead;
1384                 end = ras->ras_window_start + ras->ras_window_len - 1;
1385         }
1386         if (end != 0) {
1387                 /* Truncate RA window to end of file */
1388                 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
1389                 ras->ras_next_readahead = max(end, end + 1);
1390                 RAS_CDEBUG(ras);
1391         }
1392         ria.ria_start = start;
1393         ria.ria_end = end;
1394         /* If stride I/O mode is detected, get stride window*/
1395         if (stride_io_mode(ras)) {
1396                 ria.ria_length = ras->ras_stride_length; 
1397                 ria.ria_pages = ras->ras_stride_pages;
1398         }
1399         spin_unlock(&ras->ras_lock);
1400
1401         if (end == 0) {
1402                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
1403                 RETURN(0);
1404         }
1405         len = ria_page_count(&ria); 
1406         if (len == 0)
1407                 RETURN(0);
1408  
1409         reserved = ll_ra_count_get(ll_i2sbi(inode), len);
1410
1411         if (reserved < end - start + 1)
1412                 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
1413
1414         CDEBUG(D_READA, "reserved page %lu \n", reserved);
1415         
1416         ret = ll_read_ahead_pages(exp, oig, &ria, &reserved, mapping, &ra_end);
1417
1418         LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
1419         if (reserved != 0)
1420                 ll_ra_count_put(ll_i2sbi(inode), reserved);
1421
1422         if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
1423                 ll_ra_stats_inc(mapping, RA_STAT_EOF);
1424
1425         /* if we didn't get to the end of the region we reserved from
1426          * the ras we need to go back and update the ras so that the
1427          * next read-ahead tries from where we left off.  we only do so
1428          * if the region we failed to issue read-ahead on is still ahead
1429          * of the app and behind the next index to start read-ahead from */
1430         CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
1431                ra_end, end, ria.ria_end);
1432
1433         if (ra_end != (end + 1)) {
1434                 spin_lock(&ras->ras_lock);
1435                 if (ra_end < ras->ras_next_readahead && 
1436                     index_in_window(ra_end, ras->ras_window_start, 0, 
1437                                     ras->ras_window_len)) { 
1438                         ras->ras_next_readahead = ra_end;
1439                         RAS_CDEBUG(ras);
1440                 }
1441                 spin_unlock(&ras->ras_lock);
1442         }
1443
1444         RETURN(ret);
1445 }
1446
1447 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
1448 {
1449         ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
1450 }
1451
1452 /* called with the ras_lock held or from places where it doesn't matter */
1453 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
1454 {
1455         ras->ras_last_readpage = index;
1456         ras->ras_consecutive_requests = 0;
1457         ras->ras_consecutive_pages = 0;
1458         ras->ras_window_len = 0;
1459         ras_set_start(ras, index);
1460         ras->ras_next_readahead = max(ras->ras_window_start, index);
1461
1462         RAS_CDEBUG(ras);
1463 }
1464
1465 /* called with the ras_lock held or from places where it doesn't matter */
1466 static void ras_stride_reset(struct ll_readahead_state *ras)
1467 {
1468         ras->ras_consecutive_stride_requests = 0;
1469         RAS_CDEBUG(ras);
1470 }
1471
1472 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
1473 {
1474         spin_lock_init(&ras->ras_lock);
1475         ras_reset(ras, 0);
1476         ras->ras_requests = 0;
1477         INIT_LIST_HEAD(&ras->ras_read_beads);
1478 }
1479
1480 /* Check whether the read request is in the stride window.
1481  * If it is in the stride window, return 1, otherwise return 0.
1482  * and also update stride_gap and stride_pages. 
1483  */
1484 static int index_in_stride_window(unsigned long index, 
1485                                   struct ll_readahead_state *ras,
1486                                   struct inode *inode)
1487 {
1488         int stride_gap = index - ras->ras_last_readpage - 1;
1489         
1490         LASSERT(stride_gap != 0);
1491         
1492         if (ras->ras_consecutive_pages == 0)
1493                 return 0;
1494
1495         /*Otherwise check the stride by itself */
1496         if ((ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
1497             ras->ras_consecutive_pages == ras->ras_stride_pages)
1498                 return 1;
1499
1500         if (stride_gap >= 0) {
1501                 /* 
1502                  * only set stride_pages, stride_length if 
1503                  * it is forward reading ( stride_gap > 0)
1504                  */
1505                 ras->ras_stride_pages = ras->ras_consecutive_pages;
1506                 ras->ras_stride_length = stride_gap + ras->ras_consecutive_pages; 
1507         } else {
1508                 /* 
1509                  * If stride_gap < 0,(back_forward reading),
1510                  * reset the stride_pages/length. 
1511                  * FIXME:back_ward stride I/O read.
1512                  * 
1513                  */
1514                 ras->ras_stride_pages = 0;
1515                 ras->ras_stride_length = 0;
1516         }
1517         RAS_CDEBUG(ras);
1518
1519         return 0;
1520 }
1521
1522 static unsigned long
1523 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
1524 {
1525         return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
1526                                ras->ras_stride_pages, ras->ras_stride_offset,
1527                                len);
1528 }
1529
1530 /* Stride Read-ahead window will be increased inc_len according to
1531  * stride I/O pattern */
1532 static void ras_stride_increase_window(struct ll_readahead_state *ras, 
1533                                        struct ll_ra_info *ra,
1534                                        unsigned long inc_len)
1535 {
1536         unsigned long left, step, window_len;
1537         unsigned long stride_len;
1538
1539         LASSERT(ras->ras_stride_length > 0);
1540
1541         stride_len = ras->ras_window_start + ras->ras_window_len -
1542                      ras->ras_stride_offset;
1543
1544         LASSERTF(stride_len > 0, "window_start %lu, window_len %lu"
1545                 "stride_offset %lu\n", ras->ras_window_start,
1546                  ras->ras_window_len, ras->ras_stride_offset);
1547
1548         left = stride_len % ras->ras_stride_length;
1549
1550         window_len = ras->ras_window_len - left;
1551         
1552         if (left < ras->ras_stride_pages)
1553                 left += inc_len;
1554         else
1555                 left = ras->ras_stride_pages + inc_len; 
1556
1557         LASSERT(ras->ras_stride_pages != 0);
1558
1559         step = left / ras->ras_stride_pages;
1560         left %= ras->ras_stride_pages;
1561
1562         window_len += step * ras->ras_stride_length + left;
1563
1564         if (stride_page_count(ras, window_len) <= ra->ra_max_pages)
1565                 ras->ras_window_len = window_len;
1566
1567         RAS_CDEBUG(ras);
1568 }
1569
1570 /* Set stride I/O read-ahead window start offset */
1571 static void ras_set_stride_offset(struct ll_readahead_state *ras)
1572 {
1573         unsigned long window_len = ras->ras_next_readahead - 
1574                                    ras->ras_window_start;
1575         unsigned long left;       
1576  
1577         LASSERT(ras->ras_stride_length != 0);
1578        
1579         left = window_len % ras->ras_stride_length;
1580  
1581         ras->ras_stride_offset = ras->ras_next_readahead - left;
1582
1583         RAS_CDEBUG(ras);
1584 }
1585
1586 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1587                        struct ll_readahead_state *ras, unsigned long index,
1588                        unsigned hit)
1589 {
1590         struct ll_ra_info *ra = &sbi->ll_ra_info;
1591         int zero = 0, stride_zero = 0, stride_detect = 0, ra_miss = 0;
1592         ENTRY;
1593
1594         spin_lock(&sbi->ll_lock);
1595         spin_lock(&ras->ras_lock);
1596
1597         ll_ra_stats_inc_unlocked(ra, hit ? RA_STAT_HIT : RA_STAT_MISS);
1598
1599         /* reset the read-ahead window in two cases.  First when the app seeks
1600          * or reads to some other part of the file.  Secondly if we get a
1601          * read-ahead miss that we think we've previously issued.  This can
1602          * be a symptom of there being so many read-ahead pages that the VM is
1603          * reclaiming it before we get to it. */
1604         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1605                 zero = 1;
1606                 ll_ra_stats_inc_unlocked(ra, RA_STAT_DISTANT_READPAGE);
1607                 /* check whether it is in stride I/O mode*/
1608                 if (!index_in_stride_window(index, ras, inode))
1609                         stride_zero = 1;
1610         } else if (!hit && ras->ras_window_len &&
1611                    index < ras->ras_next_readahead &&
1612                    index_in_window(index, ras->ras_window_start, 0,
1613                                    ras->ras_window_len)) {
1614                 zero = 1;
1615                 ra_miss = 1;
1616                 /* If it hits read-ahead miss and the stride I/O is still 
1617                  * not detected, reset stride stuff to re-detect the whole
1618                  * stride I/O mode to avoid complication */
1619                 if (!stride_io_mode(ras))
1620                         stride_zero = 1;
1621                 ll_ra_stats_inc_unlocked(ra, RA_STAT_MISS_IN_WINDOW);
1622         }
1623
1624         /* On the second access to a file smaller than the tunable
1625          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1626          * file up to ra_max_pages.  This is simply a best effort and
1627          * only occurs once per open file.  Normal RA behavior is reverted
1628          * to for subsequent IO.  The mmap case does not increment
1629          * ras_requests and thus can never trigger this behavior. */
1630         if (ras->ras_requests == 2 && !ras->ras_request_index) {
1631                 __u64 kms_pages;
1632
1633                 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1634                             CFS_PAGE_SHIFT;
1635
1636                 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1637                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages);
1638
1639                 if (kms_pages &&
1640                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1641                         ras->ras_window_start = 0;
1642                         ras->ras_last_readpage = 0;
1643                         ras->ras_next_readahead = 0;
1644                         ras->ras_window_len = min(ra->ra_max_pages,
1645                                 ra->ra_max_read_ahead_whole_pages);
1646                         GOTO(out_unlock, 0);
1647                 }
1648         }
1649
1650         if (zero) {
1651                 /* If it is discontinuous read, check 
1652                  * whether it is stride I/O mode*/
1653                 if (stride_zero) {
1654                         ras_reset(ras, index);
1655                         ras->ras_consecutive_pages++;
1656                         ras_stride_reset(ras);
1657                         RAS_CDEBUG(ras);
1658                         GOTO(out_unlock, 0);
1659                 } else {
1660                         /* The read is still in stride window or
1661                          * it hits read-ahead miss */ 
1662
1663                         /* If ra-window miss is hitted, which probably means VM 
1664                          * pressure, and some read-ahead pages were reclaimed.So
1665                          * the length of ra-window will not increased, but also  
1666                          * not reset to avoid redetecting the stride I/O mode.*/ 
1667                         ras->ras_consecutive_requests = 0;
1668                         if (!ra_miss) {
1669                                 ras->ras_consecutive_pages = 0;
1670                                 if (++ras->ras_consecutive_stride_requests > 1) 
1671                                         stride_detect = 1;
1672                         }
1673                         RAS_CDEBUG(ras);
1674                 }
1675         } else if (ras->ras_consecutive_stride_requests > 1) {
1676                 /* If this is contiguous read but in stride I/O mode 
1677                  * currently, check whether stride step still is valid,
1678                  * if invalid, it will reset the stride ra window*/     
1679                 if (ras->ras_consecutive_pages + 1 > ras->ras_stride_pages) 
1680                         ras_stride_reset(ras);
1681         }
1682
1683         ras->ras_last_readpage = index;
1684         ras->ras_consecutive_pages++;
1685         ras_set_start(ras, index);
1686         ras->ras_next_readahead = max(ras->ras_window_start,
1687                                       ras->ras_next_readahead);
1688         RAS_CDEBUG(ras);
1689
1690         /* Trigger RA in the mmap case where ras_consecutive_requests
1691          * is not incremented and thus can't be used to trigger RA */
1692         if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1693                 ras->ras_window_len = RAS_INCREASE_STEP;
1694                 GOTO(out_unlock, 0);
1695         }
1696
1697         /* Initially reset the stride window offset to next_readahead*/
1698         if (ras->ras_consecutive_stride_requests == 2 && stride_detect)
1699                 ras_set_stride_offset(ras);
1700
1701         /* The initial ras_window_len is set to the request size.  To avoid
1702          * uselessly reading and discarding pages for random IO the window is
1703          * only increased once per consecutive request received. */
1704         if ((ras->ras_consecutive_requests > 1 && 
1705             !ras->ras_request_index) || stride_detect) {
1706                 if (stride_io_mode(ras))
1707                         ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP); 
1708                 else 
1709                         ras->ras_window_len = min(ras->ras_window_len +
1710                                                   RAS_INCREASE_STEP, 
1711                                                   ra->ra_max_pages);
1712         }
1713         EXIT;
1714 out_unlock:
1715         RAS_CDEBUG(ras);
1716         ras->ras_request_index++;
1717         spin_unlock(&ras->ras_lock);
1718         spin_unlock(&sbi->ll_lock);
1719         return;
1720 }
1721
1722 int ll_writepage(struct page *page)
1723 {
1724         struct inode *inode = page->mapping->host;
1725         struct ll_inode_info *lli = ll_i2info(inode);
1726         struct obd_export *exp;
1727         struct ll_async_page *llap;
1728         int rc = 0;
1729         ENTRY;
1730
1731         LASSERT(PageLocked(page));
1732
1733         exp = ll_i2dtexp(inode);
1734         if (exp == NULL)
1735                 GOTO(out, rc = -EINVAL);
1736
1737         llap = llap_from_page(page, LLAP_ORIGIN_WRITEPAGE);
1738         if (IS_ERR(llap))
1739                 GOTO(out, rc = PTR_ERR(llap));
1740
1741         LASSERT(!PageWriteback(page));
1742         set_page_writeback(page);
1743
1744         page_cache_get(page);
1745         if (llap->llap_write_queued) {
1746                 LL_CDEBUG_PAGE(D_PAGE, page, "marking urgent\n");
1747                 rc = obd_set_async_flags(exp, lli->lli_smd, NULL,
1748                                          llap->llap_cookie,
1749                                          ASYNC_READY | ASYNC_URGENT);
1750         } else {
1751                 rc = queue_or_sync_write(exp, inode, llap, CFS_PAGE_SIZE,
1752                                          ASYNC_READY | ASYNC_URGENT);
1753         }
1754         if (rc)
1755                 page_cache_release(page);
1756 out:
1757         if (rc) {
1758                 if (!lli->lli_async_rc)
1759                         lli->lli_async_rc = rc;
1760                 /* re-dirty page on error so it retries write */
1761                 if (PageWriteback(page)) {
1762                         end_page_writeback(page);
1763                 }
1764                 /* resend page only for not started IO*/
1765                 if (!PageError(page))
1766                         ll_redirty_page(page);
1767                 unlock_page(page);
1768         }
1769         RETURN(rc);
1770 }
1771
1772 /*
1773  * for now we do our readpage the same on both 2.4 and 2.5.  The kernel's
1774  * read-ahead assumes it is valid to issue readpage all the way up to
1775  * i_size, but our dlm locks make that not the case.  We disable the
1776  * kernel's read-ahead and do our own by walking ahead in the page cache
1777  * checking for dlm lock coverage.  the main difference between 2.4 and
1778  * 2.6 is how read-ahead gets batched and issued, but we're using our own,
1779  * so they look the same.
1780  */
1781 int ll_readpage(struct file *filp, struct page *page)
1782 {
1783         struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
1784         struct inode *inode = page->mapping->host;
1785         struct obd_export *exp;
1786         struct ll_async_page *llap;
1787         struct obd_io_group *oig = NULL;
1788         int rc;
1789         ENTRY;
1790
1791         LASSERT(PageLocked(page));
1792         LASSERT(!PageUptodate(page));
1793         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset=%Lu=%#Lx\n",
1794                inode->i_ino, inode->i_generation, inode,
1795                (((loff_t)page->index) << CFS_PAGE_SHIFT),
1796                (((loff_t)page->index) << CFS_PAGE_SHIFT));
1797         LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
1798
1799         if (!ll_i2info(inode)->lli_smd) {
1800                 /* File with no objects - one big hole */
1801                 /* We use this just for remove_from_page_cache that is not
1802                  * exported, we'd make page back up to date. */
1803                 ll_truncate_complete_page(page);
1804                 clear_page(kmap(page));
1805                 kunmap(page);
1806                 SetPageUptodate(page);
1807                 unlock_page(page);
1808                 RETURN(0);
1809         }
1810
1811         rc = oig_init(&oig);
1812         if (rc < 0)
1813                 GOTO(out, rc);
1814
1815         exp = ll_i2dtexp(inode);
1816         if (exp == NULL)
1817                 GOTO(out, rc = -EINVAL);
1818
1819         llap = llap_from_page(page, LLAP_ORIGIN_READPAGE);
1820         if (IS_ERR(llap))
1821                 GOTO(out, rc = PTR_ERR(llap));
1822
1823         if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages)
1824                 ras_update(ll_i2sbi(inode), inode, &fd->fd_ras, page->index,
1825                            llap->llap_defer_uptodate);
1826
1827
1828         if (llap->llap_defer_uptodate) {
1829                 /* This is the callpath if we got the page from a readahead */
1830                 llap->llap_ra_used = 1;
1831                 rc = ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1832                                   fd->fd_flags);
1833                 if (rc > 0)
1834                         obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd,
1835                                              NULL, oig);
1836                 LL_CDEBUG_PAGE(D_PAGE, page, "marking uptodate from defer\n");
1837                 SetPageUptodate(page);
1838                 unlock_page(page);
1839                 GOTO(out_oig, rc = 0);
1840         }
1841
1842         if (likely((fd->fd_flags & LL_FILE_IGNORE_LOCK) == 0)) {
1843                 rc = ll_page_matches(page, fd->fd_flags);
1844                 if (rc < 0) {
1845                         LL_CDEBUG_PAGE(D_ERROR, page,
1846                                        "lock match failed: rc %d\n", rc);
1847                         GOTO(out, rc);
1848                 }
1849
1850                 if (rc == 0) {
1851                         CWARN("ino %lu page %lu (%llu) not covered by "
1852                               "a lock (mmap?).  check debug logs.\n",
1853                               inode->i_ino, page->index,
1854                               (long long)page->index << CFS_PAGE_SHIFT);
1855                 }
1856         }
1857
1858         rc = ll_issue_page_read(exp, llap, oig, 0);
1859         if (rc)
1860                 GOTO(out, rc);
1861
1862         LL_CDEBUG_PAGE(D_PAGE, page, "queued readpage\n");
1863         /* We have just requested the actual page we want, see if we can tack
1864          * on some readahead to that page's RPC before it is sent. */
1865         if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages)
1866                 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1867                              fd->fd_flags);
1868
1869         rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
1870
1871 out:
1872         if (rc)
1873                 unlock_page(page);
1874 out_oig:
1875         if (oig != NULL)
1876                 oig_release(oig);
1877         RETURN(rc);
1878 }