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