Whamcloud - gitweb
72f4a864ac832a5ad4da95470865510ff66012f7
[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 #ifdef HAVE_KERNEL_CONFIG_H
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 <asm/segment.h>
41 #include <linux/mm.h>
42 #include <linux/pagemap.h>
43 #include <linux/smp_lock.h>
44
45 #define DEBUG_SUBSYSTEM S_LLITE
46
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 kmem_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 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 > inode->i_size))
75                 pg.count = inode->i_size % 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",
85                        inode->i_ino, inode, inode->i_size, page->mapping->host,
86                        page->mapping->host->i_size, page->index, pg.off);
87         }
88
89         pg.flag = flags;
90
91         if (cmd & OBD_BRW_WRITE)
92                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
93                                     LPROC_LL_BRW_WRITE, pg.count);
94         else
95                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
96                                     LPROC_LL_BRW_READ, pg.count);
97         oinfo.oi_oa = oa;
98         oinfo.oi_md = lsm;
99         rc = obd_brw(cmd, ll_i2obdexp(inode), &oinfo, 1, &pg, NULL);
100         if (rc == 0)
101                 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
102         else if (rc != -EIO)
103                 CERROR("error from obd_brw: rc = %d\n", rc);
104         RETURN(rc);
105 }
106
107 /* this isn't where truncate starts.   roughly:
108  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate. setattr_raw grabs
109  * DLM lock on [size, EOF], i_mutex, ->lli_size_sem, and WRITE_I_ALLOC_SEM to
110  * avoid races.
111  *
112  * must be called under ->lli_size_sem */
113 void ll_truncate(struct inode *inode)
114 {
115         struct ll_inode_info *lli = ll_i2info(inode);
116         struct obd_info oinfo = { { { 0 } } };
117         struct ost_lvb lvb;
118         struct obdo oa;
119         int rc;
120         ENTRY;
121         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %Lu=%#Lx\n",inode->i_ino,
122                inode->i_generation, inode, inode->i_size, inode->i_size);
123
124         ll_vfs_ops_tally(ll_i2sbi(inode), VFS_OPS_TRUNCATE);
125         if (lli->lli_size_sem_owner != current) {
126                 EXIT;
127                 return;
128         }
129
130         if (!lli->lli_smd) {
131                 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
132                        inode->i_ino);
133                 GOTO(out_unlock, 0);
134         }
135
136         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
137
138         /* XXX I'm pretty sure this is a hack to paper over a more fundamental
139          * race condition. */
140         lov_stripe_lock(lli->lli_smd);
141         inode_init_lvb(inode, &lvb);
142         obd_merge_lvb(ll_i2obdexp(inode), lli->lli_smd, &lvb, 0);
143         if (lvb.lvb_size == inode->i_size) {
144                 CDEBUG(D_VFSTRACE, "skipping punch for obj "LPX64", %Lu=%#Lx\n",
145                        lli->lli_smd->lsm_object_id,inode->i_size,inode->i_size);
146                 lov_stripe_unlock(lli->lli_smd);
147                 GOTO(out_unlock, 0);
148         }
149
150         obd_adjust_kms(ll_i2obdexp(inode), lli->lli_smd, inode->i_size, 1);
151         lov_stripe_unlock(lli->lli_smd);
152
153         if (unlikely((ll_i2sbi(inode)->ll_flags & LL_SBI_CHECKSUM) &&
154                      (inode->i_size & ~CFS_PAGE_MASK))) {
155                 /* If the truncate leaves behind a partial page, update its
156                  * checksum. */
157                 struct page *page = find_get_page(inode->i_mapping,
158                                                   inode->i_size >> CFS_PAGE_SHIFT);
159                 if (page != NULL) {
160                         struct ll_async_page *llap = llap_cast_private(page);
161                         if (llap != NULL) {
162                                 llap->llap_checksum =
163                                         crc32_le(0, kmap(page), CFS_PAGE_SIZE);
164                                 kunmap(page);
165                         }
166                         page_cache_release(page);
167                 }
168         }
169
170         CDEBUG(D_INFO, "calling punch for "LPX64" (new size %Lu=%#Lx)\n",
171                lli->lli_smd->lsm_object_id, inode->i_size, inode->i_size);
172
173         oinfo.oi_md = lli->lli_smd;
174         oinfo.oi_policy.l_extent.start = inode->i_size;
175         oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
176         oinfo.oi_oa = &oa;
177         oa.o_id = lli->lli_smd->lsm_object_id;
178         oa.o_valid = OBD_MD_FLID;
179
180         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLMODE |OBD_MD_FLFID|
181                         OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME |
182                         OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLGENER | 
183                         OBD_MD_FLBLOCKS);
184
185         ll_inode_size_unlock(inode, 0);
186
187         rc = obd_punch_rqset(ll_i2obdexp(inode), &oinfo, NULL);
188         if (rc)
189                 CERROR("obd_truncate fails (%d) ino %lu\n", rc, inode->i_ino);
190         else
191                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
192                               OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME);
193         EXIT;
194         return;
195
196  out_unlock:
197         ll_inode_size_unlock(inode, 0);
198 } /* ll_truncate */
199
200 int ll_prepare_write(struct file *file, struct page *page, unsigned from,
201                      unsigned to)
202 {
203         struct inode *inode = page->mapping->host;
204         struct ll_inode_info *lli = ll_i2info(inode);
205         struct lov_stripe_md *lsm = lli->lli_smd;
206         obd_off offset = ((obd_off)page->index) << CFS_PAGE_SHIFT;
207         struct obd_info oinfo = { { { 0 } } };
208         struct brw_page pga;
209         struct obdo oa;
210         struct ost_lvb lvb;
211         int rc = 0;
212         ENTRY;
213
214         LASSERT(PageLocked(page));
215         (void)llap_cast_private(page); /* assertion */
216
217         /* Check to see if we should return -EIO right away */
218         pga.pg = page;
219         pga.off = offset;
220         pga.count = CFS_PAGE_SIZE;
221         pga.flag = 0;
222
223         oa.o_mode = inode->i_mode;
224         oa.o_id = lsm->lsm_object_id;
225         oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
226         obdo_from_inode(&oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
227
228         oinfo.oi_oa = &oa;
229         oinfo.oi_md = lsm;
230         rc = obd_brw(OBD_BRW_CHECK, ll_i2obdexp(inode), &oinfo, 1, &pga, NULL);
231         if (rc)
232                 RETURN(rc);
233
234         if (PageUptodate(page)) {
235                 LL_CDEBUG_PAGE(D_PAGE, page, "uptodate\n");
236                 RETURN(0);
237         }
238
239         /* We're completely overwriting an existing page, so _don't_ set it up
240          * to date until commit_write */
241         if (from == 0 && to == CFS_PAGE_SIZE) {
242                 LL_CDEBUG_PAGE(D_PAGE, page, "full page write\n");
243                 POISON_PAGE(page, 0x11);
244                 RETURN(0);
245         }
246
247         /* If are writing to a new page, no need to read old data.  The extent
248          * locking will have updated the KMS, and for our purposes here we can
249          * treat it like i_size. */
250         lov_stripe_lock(lsm);
251         inode_init_lvb(inode, &lvb);
252         obd_merge_lvb(ll_i2obdexp(inode), lsm, &lvb, 1);
253         lov_stripe_unlock(lsm);
254         if (lvb.lvb_size <= offset) {
255                 LL_CDEBUG_PAGE(D_PAGE, page, "kms "LPU64" <= offset "LPU64"\n",
256                                lvb.lvb_size, offset);
257                 memset(kmap(page), 0, CFS_PAGE_SIZE);
258                 kunmap(page);
259                 GOTO(prepare_done, rc = 0);
260         }
261
262         /* XXX could be an async ocp read.. read-ahead? */
263         rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
264         if (rc == 0) {
265                 /* bug 1598: don't clobber blksize */
266                 oa.o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
267                 obdo_refresh_inode(inode, &oa, oa.o_valid);
268         }
269
270         EXIT;
271  prepare_done:
272         if (rc == 0)
273                 SetPageUptodate(page);
274
275         return rc;
276 }
277
278 static int ll_ap_make_ready(void *data, int cmd)
279 {
280         struct ll_async_page *llap;
281         struct page *page;
282         ENTRY;
283
284         llap = LLAP_FROM_COOKIE(data);
285         page = llap->llap_page;
286
287         LASSERTF(!(cmd & OBD_BRW_READ), "cmd %x page %p ino %lu index %lu\n", cmd, page,
288                  page->mapping->host->i_ino, page->index);
289
290         /* we're trying to write, but the page is locked.. come back later */
291         if (TryLockPage(page))
292                 RETURN(-EAGAIN);
293
294         LL_CDEBUG_PAGE(D_PAGE, page, "made ready\n");
295         page_cache_get(page);
296
297         /* if we left PageDirty we might get another writepage call
298          * in the future.  list walkers are bright enough
299          * to check page dirty so we can leave it on whatever list
300          * its on.  XXX also, we're called with the cli list so if
301          * we got the page cache list we'd create a lock inversion
302          * with the removepage path which gets the page lock then the
303          * cli lock */
304         clear_page_dirty(page);
305         RETURN(0);
306 }
307
308 /* We have two reasons for giving llite the opportunity to change the
309  * write length of a given queued page as it builds the RPC containing
310  * the page:
311  *
312  * 1) Further extending writes may have landed in the page cache
313  *    since a partial write first queued this page requiring us
314  *    to write more from the page cache.  (No further races are possible, since
315  *    by the time this is called, the page is locked.)
316  * 2) We might have raced with truncate and want to avoid performing
317  *    write RPCs that are just going to be thrown away by the
318  *    truncate's punch on the storage targets.
319  *
320  * The kms serves these purposes as it is set at both truncate and extending
321  * writes.
322  */
323 static int ll_ap_refresh_count(void *data, int cmd)
324 {
325         struct ll_inode_info *lli;
326         struct ll_async_page *llap;
327         struct lov_stripe_md *lsm;
328         struct page *page;
329         struct inode *inode;
330         struct ost_lvb lvb;
331         __u64 kms;
332         ENTRY;
333
334         /* readpage queues with _COUNT_STABLE, shouldn't get here. */
335         LASSERT(cmd != OBD_BRW_READ);
336
337         llap = LLAP_FROM_COOKIE(data);
338         page = llap->llap_page;
339         inode = page->mapping->host;
340         lli = ll_i2info(inode);
341         lsm = lli->lli_smd;
342
343         lov_stripe_lock(lsm);
344         inode_init_lvb(inode, &lvb);
345         obd_merge_lvb(ll_i2obdexp(inode), lsm, &lvb, 1);
346         kms = lvb.lvb_size;
347         lov_stripe_unlock(lsm);
348
349         /* catch race with truncate */
350         if (((__u64)page->index << CFS_PAGE_SHIFT) >= kms)
351                 return 0;
352
353         /* catch sub-page write at end of file */
354         if (((__u64)page->index << CFS_PAGE_SHIFT) + CFS_PAGE_SIZE > kms)
355                 return kms % CFS_PAGE_SIZE;
356
357         return CFS_PAGE_SIZE;
358 }
359
360 void ll_inode_fill_obdo(struct inode *inode, int cmd, struct obdo *oa)
361 {
362         struct lov_stripe_md *lsm;
363         obd_flag valid_flags;
364
365         lsm = ll_i2info(inode)->lli_smd;
366
367         oa->o_id = lsm->lsm_object_id;
368         oa->o_valid = OBD_MD_FLID;
369         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
370         if (cmd & OBD_BRW_WRITE) {
371                 oa->o_valid |= OBD_MD_FLEPOCH;
372                 oa->o_easize = ll_i2info(inode)->lli_io_epoch;
373
374                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
375                         OBD_MD_FLUID | OBD_MD_FLGID |
376                         OBD_MD_FLFID | OBD_MD_FLGENER;
377         }
378
379         obdo_from_inode(oa, inode, valid_flags);
380 }
381
382 static void ll_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
383 {
384         struct ll_async_page *llap;
385         ENTRY;
386
387         llap = LLAP_FROM_COOKIE(data);
388         ll_inode_fill_obdo(llap->llap_page->mapping->host, cmd, oa);
389
390         EXIT;
391 }
392
393 static void ll_ap_update_obdo(void *data, int cmd, struct obdo *oa,
394                               obd_valid valid)
395 {
396         struct ll_async_page *llap;
397         ENTRY;
398
399         llap = LLAP_FROM_COOKIE(data);
400         obdo_from_inode(oa, llap->llap_page->mapping->host, valid);
401
402         EXIT;
403 }
404
405 static struct obd_async_page_ops ll_async_page_ops = {
406         .ap_make_ready =        ll_ap_make_ready,
407         .ap_refresh_count =     ll_ap_refresh_count,
408         .ap_fill_obdo =         ll_ap_fill_obdo,
409         .ap_update_obdo =       ll_ap_update_obdo,
410         .ap_completion =        ll_ap_completion,
411 };
412
413 struct ll_async_page *llap_cast_private(struct page *page)
414 {
415         struct ll_async_page *llap = (struct ll_async_page *)page_private(page);
416
417         LASSERTF(llap == NULL || llap->llap_magic == LLAP_MAGIC,
418                  "page %p private %lu gave magic %d which != %d\n",
419                  page, page_private(page), llap->llap_magic, LLAP_MAGIC);
420
421         return llap;
422 }
423
424 /* Try to shrink the page cache for the @sbi filesystem by 1/@shrink_fraction.
425  *
426  * There is an llap attached onto every page in lustre, linked off @sbi.
427  * We add an llap to the list so we don't lose our place during list walking.
428  * If llaps in the list are being moved they will only move to the end
429  * of the LRU, and we aren't terribly interested in those pages here (we
430  * start at the beginning of the list where the least-used llaps are.
431  */
432 int llap_shrink_cache(struct ll_sb_info *sbi, int shrink_fraction)
433 {
434         struct ll_async_page *llap, dummy_llap = { .llap_magic = 0xd11ad11a };
435         unsigned long total, want, count = 0;
436
437         total = sbi->ll_async_page_count;
438
439         /* There can be a large number of llaps (600k or more in a large
440          * memory machine) so the VM 1/6 shrink ratio is likely too much.
441          * Since we are freeing pages also, we don't necessarily want to
442          * shrink so much.  Limit to 40MB of pages + llaps per call. */
443         if (shrink_fraction == 0)
444                 want = sbi->ll_async_page_count - sbi->ll_async_page_max + 32;
445         else
446                 want = (total + shrink_fraction - 1) / shrink_fraction;
447
448         if (want > 40 << (20 - CFS_PAGE_SHIFT))
449                 want = 40 << (20 - CFS_PAGE_SHIFT);
450
451         CDEBUG(D_CACHE, "shrinking %lu of %lu pages (1/%d)\n",
452                want, total, shrink_fraction);
453
454         spin_lock(&sbi->ll_lock);
455         list_add(&dummy_llap.llap_pglist_item, &sbi->ll_pglist);
456
457         while (--total >= 0 && count < want) {
458                 struct page *page;
459                 int keep;
460
461                 if (unlikely(need_resched())) {
462                         spin_unlock(&sbi->ll_lock);
463                         cond_resched();
464                         spin_lock(&sbi->ll_lock);
465                 }
466
467                 llap = llite_pglist_next_llap(sbi,&dummy_llap.llap_pglist_item);
468                 list_del_init(&dummy_llap.llap_pglist_item);
469                 if (llap == NULL)
470                         break;
471
472                 page = llap->llap_page;
473                 LASSERT(page != NULL);
474
475                 list_add(&dummy_llap.llap_pglist_item, &llap->llap_pglist_item);
476
477                 /* Page needs/undergoing IO */
478                 if (TryLockPage(page)) {
479                         LL_CDEBUG_PAGE(D_PAGE, page, "can't lock\n");
480                         continue;
481                 }
482
483                 if (llap->llap_write_queued || PageDirty(page) ||
484                     (!PageUptodate(page) &&
485                      llap->llap_origin != LLAP_ORIGIN_READAHEAD))
486                         keep = 1;
487                 else
488                         keep = 0;
489
490                 LL_CDEBUG_PAGE(D_PAGE, page,"%s LRU page: %s%s%s%s origin %s\n",
491                                keep ? "keep" : "drop",
492                                llap->llap_write_queued ? "wq " : "",
493                                PageDirty(page) ? "pd " : "",
494                                PageUptodate(page) ? "" : "!pu ",
495                                llap->llap_defer_uptodate ? "" : "!du",
496                                llap_origins[llap->llap_origin]);
497
498                 /* If page is dirty or undergoing IO don't discard it */
499                 if (keep) {
500                         unlock_page(page);
501                         continue;
502                 }
503
504                 page_cache_get(page);
505                 spin_unlock(&sbi->ll_lock);
506
507                 if (page->mapping != NULL) {
508                         ll_teardown_mmaps(page->mapping,
509                                          (__u64)page->index << CFS_PAGE_SHIFT,
510                                          ((__u64)page->index << CFS_PAGE_SHIFT)|
511                                           ~CFS_PAGE_MASK);
512                         if (!PageDirty(page) && !page_mapped(page)) {
513                                 ll_ra_accounting(llap, page->mapping);
514                                 ll_truncate_complete_page(page);
515                                 ++count;
516                         } else {
517                                 LL_CDEBUG_PAGE(D_PAGE, page, "Not dropping page"
518                                                              " because it is "
519                                                              "%s\n",
520                                                               PageDirty(page)?
521                                                               "dirty":"mapped");
522                         }
523                 }
524                 unlock_page(page);
525                 page_cache_release(page);
526
527                 spin_lock(&sbi->ll_lock);
528         }
529         list_del(&dummy_llap.llap_pglist_item);
530         spin_unlock(&sbi->ll_lock);
531
532         CDEBUG(D_CACHE, "shrank %lu/%lu and left %lu unscanned\n",
533                count, want, total);
534
535         return count;
536 }
537
538 static struct ll_async_page *llap_from_page(struct page *page, unsigned origin)
539 {
540         struct ll_async_page *llap;
541         struct obd_export *exp;
542         struct inode *inode = page->mapping->host;
543         struct ll_sb_info *sbi;
544         int rc;
545         ENTRY;
546
547         if (!inode) {
548                 static int triggered;
549
550                 if (!triggered) {
551                         LL_CDEBUG_PAGE(D_ERROR, page, "Bug 10047. Wrong anon "
552                                        "page received\n");
553                         libcfs_debug_dumpstack(NULL);
554                         triggered = 1;
555                 }
556                 RETURN(ERR_PTR(-EINVAL));
557         }
558         sbi = ll_i2sbi(inode);
559         LASSERT(ll_async_page_slab);
560         LASSERTF(origin < LLAP__ORIGIN_MAX, "%u\n", origin);
561
562         llap = llap_cast_private(page);
563         if (llap != NULL) {
564                 /* move to end of LRU list, except when page is just about to
565                  * die */
566                 if (origin != LLAP_ORIGIN_REMOVEPAGE) {
567                         spin_lock(&sbi->ll_lock);
568                         sbi->ll_pglist_gen++;
569                         list_del_init(&llap->llap_pglist_item);
570                         list_add_tail(&llap->llap_pglist_item, &sbi->ll_pglist);
571                         spin_unlock(&sbi->ll_lock);
572                 }
573                 GOTO(out, llap);
574         }
575
576         exp = ll_i2obdexp(page->mapping->host);
577         if (exp == NULL)
578                 RETURN(ERR_PTR(-EINVAL));
579
580         /* limit the number of lustre-cached pages */
581         if (sbi->ll_async_page_count >= sbi->ll_async_page_max)
582                 llap_shrink_cache(sbi, 0);
583
584         OBD_SLAB_ALLOC(llap, ll_async_page_slab, SLAB_KERNEL,
585                        ll_async_page_slab_size);
586         if (llap == NULL)
587                 RETURN(ERR_PTR(-ENOMEM));
588         llap->llap_magic = LLAP_MAGIC;
589         llap->llap_cookie = (void *)llap + size_round(sizeof(*llap));
590
591         rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd, NULL, page,
592                                  (obd_off)page->index << CFS_PAGE_SHIFT,
593                                  &ll_async_page_ops, llap, &llap->llap_cookie);
594         if (rc) {
595                 OBD_SLAB_FREE(llap, ll_async_page_slab,
596                               ll_async_page_slab_size);
597                 RETURN(ERR_PTR(rc));
598         }
599
600         CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap,
601                page, llap->llap_cookie, (obd_off)page->index << CFS_PAGE_SHIFT);
602         /* also zeroing the PRIVBITS low order bitflags */
603         __set_page_ll_data(page, llap);
604         llap->llap_page = page;
605
606         spin_lock(&sbi->ll_lock);
607         sbi->ll_pglist_gen++;
608         sbi->ll_async_page_count++;
609         list_add_tail(&llap->llap_pglist_item, &sbi->ll_pglist);
610         spin_unlock(&sbi->ll_lock);
611
612  out:
613         if (unlikely(sbi->ll_flags & LL_SBI_CHECKSUM)) {
614                 __u32 csum = 0;
615                 csum = crc32_le(csum, kmap(page), CFS_PAGE_SIZE);
616                 kunmap(page);
617                 if (origin == LLAP_ORIGIN_READAHEAD ||
618                     origin == LLAP_ORIGIN_READPAGE) {
619                         llap->llap_checksum = 0;
620                 } else if (origin == LLAP_ORIGIN_COMMIT_WRITE ||
621                            llap->llap_checksum == 0) {
622                         llap->llap_checksum = csum;
623                         CDEBUG(D_PAGE, "page %p cksum %x\n", page, csum);
624                 } else if (llap->llap_checksum == csum) {
625                         /* origin == LLAP_ORIGIN_WRITEPAGE */
626                         CDEBUG(D_PAGE, "page %p cksum %x confirmed\n",
627                                page, csum);
628                 } else {
629                         /* origin == LLAP_ORIGIN_WRITEPAGE */
630                         LL_CDEBUG_PAGE(D_ERROR, page, "old cksum %x != new "
631                                        "%x!\n", llap->llap_checksum, csum);
632                 }
633         }
634
635         llap->llap_origin = origin;
636         RETURN(llap);
637 }
638
639 static int queue_or_sync_write(struct obd_export *exp, struct inode *inode,
640                                struct ll_async_page *llap,
641                                unsigned to, obd_flag async_flags)
642 {
643         unsigned long size_index = inode->i_size >> CFS_PAGE_SHIFT;
644         struct obd_io_group *oig;
645         struct ll_sb_info *sbi = ll_i2sbi(inode);
646         int rc, noquot = llap->llap_ignore_quota ? OBD_BRW_NOQUOTA : 0;
647         ENTRY;
648
649         /* _make_ready only sees llap once we've unlocked the page */
650         llap->llap_write_queued = 1;
651         rc = obd_queue_async_io(exp, ll_i2info(inode)->lli_smd, NULL,
652                                 llap->llap_cookie, OBD_BRW_WRITE | noquot,
653                                 0, 0, 0, async_flags);
654         if (rc == 0) {
655                 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "write queued\n");
656                 //llap_write_pending(inode, llap);
657                 GOTO(out, 0);
658         }
659
660         llap->llap_write_queued = 0;
661
662         rc = oig_init(&oig);
663         if (rc)
664                 GOTO(out, rc);
665
666         /* make full-page requests if we are not at EOF (bug 4410) */
667         if (to != CFS_PAGE_SIZE && llap->llap_page->index < size_index) {
668                 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
669                                "sync write before EOF: size_index %lu, to %d\n",
670                                size_index, to);
671                 to = CFS_PAGE_SIZE;
672         } else if (to != CFS_PAGE_SIZE && llap->llap_page->index == size_index) {
673                 int size_to = inode->i_size & ~CFS_PAGE_MASK;
674                 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page,
675                                "sync write at EOF: size_index %lu, to %d/%d\n",
676                                size_index, to, size_to);
677                 if (to < size_to)
678                         to = size_to;
679         }
680
681         /* compare the checksum once before the page leaves llite */
682         if (unlikely((sbi->ll_flags & LL_SBI_CHECKSUM) &&
683                      llap->llap_checksum != 0)) {
684                 __u32 csum = 0;
685                 struct page *page = llap->llap_page;
686                 csum = crc32_le(csum, kmap(page), CFS_PAGE_SIZE);
687                 kunmap(page);
688                 if (llap->llap_checksum == csum) {
689                         CDEBUG(D_PAGE, "page %p cksum %x confirmed\n",
690                                page, csum);
691                 } else {
692                         CERROR("page %p old cksum %x != new cksum %x!\n",
693                                page, llap->llap_checksum, csum);
694                 }
695         }
696
697         rc = obd_queue_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig,
698                                 llap->llap_cookie, OBD_BRW_WRITE | noquot,
699                                 0, to, 0, ASYNC_READY | ASYNC_URGENT |
700                                 ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
701         if (rc)
702                 GOTO(free_oig, rc);
703
704         rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
705         if (rc)
706                 GOTO(free_oig, rc);
707
708         rc = oig_wait(oig);
709
710         if (!rc && async_flags & ASYNC_READY)
711                 unlock_page(llap->llap_page);
712
713         LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "sync write returned %d\n", rc);
714
715 free_oig:
716         oig_release(oig);
717 out:
718         RETURN(rc);
719 }
720
721 /* update our write count to account for i_size increases that may have
722  * happened since we've queued the page for io. */
723
724 /* be careful not to return success without setting the page Uptodate or
725  * the next pass through prepare_write will read in stale data from disk. */
726 int ll_commit_write(struct file *file, struct page *page, unsigned from,
727                     unsigned to)
728 {
729         struct inode *inode = page->mapping->host;
730         struct ll_inode_info *lli = ll_i2info(inode);
731         struct lov_stripe_md *lsm = lli->lli_smd;
732         struct obd_export *exp;
733         struct ll_async_page *llap;
734         loff_t size;
735         int rc = 0;
736         ENTRY;
737
738         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
739         LASSERT(inode == file->f_dentry->d_inode);
740         LASSERT(PageLocked(page));
741
742         CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
743                inode, page, from, to, page->index);
744
745         llap = llap_from_page(page, LLAP_ORIGIN_COMMIT_WRITE);
746         if (IS_ERR(llap))
747                 RETURN(PTR_ERR(llap));
748
749         exp = ll_i2obdexp(inode);
750         if (exp == NULL)
751                 RETURN(-EINVAL);
752
753         llap->llap_ignore_quota = capable(CAP_SYS_RESOURCE);
754
755         /* queue a write for some time in the future the first time we
756          * dirty the page */
757         if (!PageDirty(page)) {
758                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
759                                      LPROC_LL_DIRTY_MISSES);
760
761                 rc = queue_or_sync_write(exp, inode, llap, to, 0);
762                 if (rc)
763                         GOTO(out, rc);
764         } else {
765                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
766                                      LPROC_LL_DIRTY_HITS);
767         }
768
769         /* put the page in the page cache, from now on ll_removepage is
770          * responsible for cleaning up the llap.
771          * only set page dirty when it's queued to be write out */
772         if (llap->llap_write_queued)
773                 set_page_dirty(page);
774
775 out:
776         size = (((obd_off)page->index) << CFS_PAGE_SHIFT) + to;
777         ll_inode_size_lock(inode, 0);
778         if (rc == 0) {
779                 lov_stripe_lock(lsm);
780                 obd_adjust_kms(exp, lsm, size, 0);
781                 lov_stripe_unlock(lsm);
782                 if (size > inode->i_size)
783                         inode->i_size = size;
784                 SetPageUptodate(page);
785         } else if (size > inode->i_size) {
786                 /* this page beyond the pales of i_size, so it can't be
787                  * truncated in ll_p_r_e during lock revoking. we must
788                  * teardown our book-keeping here. */
789                 ll_removepage(page);
790         }
791         ll_inode_size_unlock(inode, 0);
792         RETURN(rc);
793 }
794
795 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
796 {
797         struct ll_ra_info *ra = &sbi->ll_ra_info;
798         unsigned long ret;
799         ENTRY;
800
801         spin_lock(&sbi->ll_lock);
802         ret = min(ra->ra_max_pages - ra->ra_cur_pages, len);
803         ra->ra_cur_pages += ret;
804         spin_unlock(&sbi->ll_lock);
805
806         RETURN(ret);
807 }
808
809 static void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
810 {
811         struct ll_ra_info *ra = &sbi->ll_ra_info;
812         spin_lock(&sbi->ll_lock);
813         LASSERTF(ra->ra_cur_pages >= len, "r_c_p %lu len %lu\n",
814                  ra->ra_cur_pages, len);
815         ra->ra_cur_pages -= len;
816         spin_unlock(&sbi->ll_lock);
817 }
818
819 /* called for each page in a completed rpc.*/
820 int ll_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
821 {
822         struct ll_async_page *llap;
823         struct page *page;
824         int ret = 0;
825         ENTRY;
826
827         llap = LLAP_FROM_COOKIE(data);
828         page = llap->llap_page;
829         LASSERT(PageLocked(page));
830
831         LL_CDEBUG_PAGE(D_PAGE, page, "completing cmd %d with %d\n", cmd, rc);
832
833         if (cmd & OBD_BRW_READ && llap->llap_defer_uptodate)
834                 ll_ra_count_put(ll_i2sbi(page->mapping->host), 1);
835
836         if (rc == 0)  {
837                 if (cmd & OBD_BRW_READ) {
838                         if (!llap->llap_defer_uptodate)
839                                 SetPageUptodate(page);
840                 } else {
841                         llap->llap_write_queued = 0;
842                 }
843                 ClearPageError(page);
844         } else {
845                 if (cmd & OBD_BRW_READ) {
846                         llap->llap_defer_uptodate = 0;
847                 } else {
848                         ll_redirty_page(page);
849                         ret = 1;
850                 }
851                 SetPageError(page);
852         }
853
854         unlock_page(page);
855
856         if (0 && cmd & OBD_BRW_WRITE) {
857                 llap_write_complete(page->mapping->host, llap);
858                 ll_try_done_writing(page->mapping->host);
859         }
860
861         if (PageWriteback(page)) {
862                 end_page_writeback(page);
863         }
864         page_cache_release(page);
865
866         RETURN(ret);
867 }
868
869 /* the kernel calls us here when a page is unhashed from the page cache.
870  * the page will be locked and the kernel is holding a spinlock, so
871  * we need to be careful.  we're just tearing down our book-keeping
872  * here. */
873 void ll_removepage(struct page *page)
874 {
875         struct inode *inode = page->mapping->host;
876         struct obd_export *exp;
877         struct ll_async_page *llap;
878         struct ll_sb_info *sbi = ll_i2sbi(inode);
879         int rc;
880         ENTRY;
881
882         LASSERT(!in_interrupt());
883
884         /* sync pages or failed read pages can leave pages in the page
885          * cache that don't have our data associated with them anymore */
886         if (page_private(page) == 0) {
887                 EXIT;
888                 return;
889         }
890
891         LL_CDEBUG_PAGE(D_PAGE, page, "being evicted\n");
892
893         exp = ll_i2obdexp(inode);
894         if (exp == NULL) {
895                 CERROR("page %p ind %lu gave null export\n", page, page->index);
896                 EXIT;
897                 return;
898         }
899
900         llap = llap_from_page(page, 0);
901         if (IS_ERR(llap)) {
902                 CERROR("page %p ind %lu couldn't find llap: %ld\n", page,
903                        page->index, PTR_ERR(llap));
904                 EXIT;
905                 return;
906         }
907
908         //llap_write_complete(inode, llap);
909         rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL,
910                                      llap->llap_cookie);
911         if (rc != 0)
912                 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
913
914         /* this unconditional free is only safe because the page lock
915          * is providing exclusivity to memory pressure/truncate/writeback..*/
916         __clear_page_ll_data(page);
917
918         spin_lock(&sbi->ll_lock);
919         if (!list_empty(&llap->llap_pglist_item))
920                 list_del_init(&llap->llap_pglist_item);
921         sbi->ll_pglist_gen++;
922         sbi->ll_async_page_count--;
923         spin_unlock(&sbi->ll_lock);
924         OBD_SLAB_FREE(llap, ll_async_page_slab, ll_async_page_slab_size);
925         EXIT;
926 }
927
928 static int ll_page_matches(struct page *page, int fd_flags)
929 {
930         struct lustre_handle match_lockh = {0};
931         struct inode *inode = page->mapping->host;
932         ldlm_policy_data_t page_extent;
933         int flags, matches;
934         ENTRY;
935
936         if (unlikely(fd_flags & LL_FILE_GROUP_LOCKED))
937                 RETURN(1);
938
939         page_extent.l_extent.start = (__u64)page->index << CFS_PAGE_SHIFT;
940         page_extent.l_extent.end =
941                 page_extent.l_extent.start + CFS_PAGE_SIZE - 1;
942         flags = LDLM_FL_TEST_LOCK | LDLM_FL_BLOCK_GRANTED;
943         if (!(fd_flags & LL_FILE_READAHEAD))
944                 flags |= LDLM_FL_CBPENDING;
945         matches = obd_match(ll_i2sbi(inode)->ll_osc_exp,
946                             ll_i2info(inode)->lli_smd, LDLM_EXTENT,
947                             &page_extent, LCK_PR | LCK_PW, &flags, inode,
948                             &match_lockh);
949         RETURN(matches);
950 }
951
952 static int ll_issue_page_read(struct obd_export *exp,
953                               struct ll_async_page *llap,
954                               struct obd_io_group *oig, int defer)
955 {
956         struct page *page = llap->llap_page;
957         int rc;
958
959         page_cache_get(page);
960         llap->llap_defer_uptodate = defer;
961         llap->llap_ra_used = 0;
962         rc = obd_queue_group_io(exp, ll_i2info(page->mapping->host)->lli_smd,
963                                 NULL, oig, llap->llap_cookie, OBD_BRW_READ, 0,
964                                 CFS_PAGE_SIZE, 0, ASYNC_COUNT_STABLE | ASYNC_READY |
965                                               ASYNC_URGENT);
966         if (rc) {
967                 LL_CDEBUG_PAGE(D_ERROR, page, "read queue failed: rc %d\n", rc);
968                 page_cache_release(page);
969         }
970         RETURN(rc);
971 }
972
973 static void ll_ra_stats_inc_unlocked(struct ll_ra_info *ra, enum ra_stat which)
974 {
975         LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
976         ra->ra_stats[which]++;
977 }
978
979 static void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
980 {
981         struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
982         struct ll_ra_info *ra = &ll_i2sbi(mapping->host)->ll_ra_info;
983
984         spin_lock(&sbi->ll_lock);
985         ll_ra_stats_inc_unlocked(ra, which);
986         spin_unlock(&sbi->ll_lock);
987 }
988
989 void ll_ra_accounting(struct ll_async_page *llap, struct address_space *mapping)
990 {
991         if (!llap->llap_defer_uptodate || llap->llap_ra_used)
992                 return;
993
994         ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
995 }
996
997 #define RAS_CDEBUG(ras) \
998         CDEBUG(D_READA,                                                      \
999                "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu\n", \
1000                ras->ras_last_readpage, ras->ras_consecutive_requests,        \
1001                ras->ras_consecutive_pages, ras->ras_window_start,            \
1002                ras->ras_window_len, ras->ras_next_readahead,                 \
1003                ras->ras_requests, ras->ras_request_index);
1004
1005 static int index_in_window(unsigned long index, unsigned long point,
1006                            unsigned long before, unsigned long after)
1007 {
1008         unsigned long start = point - before, end = point + after;
1009
1010         if (start > point)
1011                start = 0;
1012         if (end < point)
1013                end = ~0;
1014
1015         return start <= index && index <= end;
1016 }
1017
1018 static struct ll_readahead_state *ll_ras_get(struct file *f)
1019 {
1020         struct ll_file_data       *fd;
1021
1022         fd = LUSTRE_FPRIVATE(f);
1023         return &fd->fd_ras;
1024 }
1025
1026 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
1027 {
1028         struct ll_readahead_state *ras;
1029
1030         ras = ll_ras_get(f);
1031
1032         spin_lock(&ras->ras_lock);
1033         ras->ras_requests++;
1034         ras->ras_request_index = 0;
1035         ras->ras_consecutive_requests++;
1036         rar->lrr_reader = current;
1037
1038         list_add(&rar->lrr_linkage, &ras->ras_read_beads);
1039         spin_unlock(&ras->ras_lock);
1040 }
1041
1042 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
1043 {
1044         struct ll_readahead_state *ras;
1045
1046         ras = ll_ras_get(f);
1047
1048         spin_lock(&ras->ras_lock);
1049         list_del_init(&rar->lrr_linkage);
1050         spin_unlock(&ras->ras_lock);
1051 }
1052
1053 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
1054 {
1055         struct ll_ra_read *scan;
1056
1057         list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
1058                 if (scan->lrr_reader == current)
1059                         return scan;
1060         }
1061         return NULL;
1062 }
1063
1064 struct ll_ra_read *ll_ra_read_get(struct file *f)
1065 {
1066         struct ll_readahead_state *ras;
1067         struct ll_ra_read         *bead;
1068
1069         ras = ll_ras_get(f);
1070
1071         spin_lock(&ras->ras_lock);
1072         bead = ll_ra_read_get_locked(ras);
1073         spin_unlock(&ras->ras_lock);
1074         return bead;
1075 }
1076
1077 static int ll_readahead(struct ll_readahead_state *ras,
1078                          struct obd_export *exp, struct address_space *mapping,
1079                          struct obd_io_group *oig, int flags)
1080 {
1081         unsigned long i, start = 0, end = 0, reserved;
1082         struct ll_async_page *llap;
1083         struct page *page;
1084         int rc, ret = 0, match_failed = 0;
1085         __u64 kms;
1086         unsigned int gfp_mask;
1087         struct inode *inode;
1088         struct lov_stripe_md *lsm;
1089         struct ll_ra_read *bead;
1090         struct ost_lvb lvb;
1091         ENTRY;
1092
1093         inode = mapping->host;
1094         lsm = ll_i2info(inode)->lli_smd;
1095
1096         lov_stripe_lock(lsm);
1097         inode_init_lvb(inode, &lvb);
1098         obd_merge_lvb(ll_i2obdexp(inode), lsm, &lvb, 1);
1099         kms = lvb.lvb_size;
1100         lov_stripe_unlock(lsm);
1101         if (kms == 0) {
1102                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
1103                 RETURN(0);
1104         }
1105
1106         spin_lock(&ras->ras_lock);
1107         bead = ll_ra_read_get_locked(ras);
1108         /* Enlarge the RA window to encompass the full read */
1109         if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
1110             bead->lrr_start + bead->lrr_count) {
1111                 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
1112                                       ras->ras_window_start;
1113         }
1114         /* Reserve a part of the read-ahead window that we'll be issuing */
1115         if (ras->ras_window_len) {
1116                 start = ras->ras_next_readahead;
1117                 end = ras->ras_window_start + ras->ras_window_len - 1;
1118         }
1119         if (end != 0) {
1120                 /* Truncate RA window to end of file */
1121                 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
1122                 ras->ras_next_readahead = max(end, end + 1);
1123                 RAS_CDEBUG(ras);
1124         }
1125         spin_unlock(&ras->ras_lock);
1126
1127         if (end == 0) {
1128                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
1129                 RETURN(0);
1130         }
1131
1132         reserved = ll_ra_count_get(ll_i2sbi(inode), end - start + 1);
1133         if (reserved < end - start + 1)
1134                 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
1135
1136         gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
1137 #ifdef __GFP_NOWARN
1138         gfp_mask |= __GFP_NOWARN;
1139 #endif
1140
1141         for (i = start; reserved > 0 && !match_failed && i <= end; i++) {
1142                 /* skip locked pages from previous readpage calls */
1143                 page = grab_cache_page_nowait_gfp(mapping, i, gfp_mask);
1144                 if (page == NULL) {
1145                         ll_ra_stats_inc(mapping, RA_STAT_FAILED_GRAB_PAGE);
1146                         CDEBUG(D_READA, "g_c_p_n failed\n");
1147                         continue;
1148                 }
1149
1150                 /* Check if page was truncated or reclaimed */
1151                 if (page->mapping != mapping) {
1152                         ll_ra_stats_inc(mapping, RA_STAT_WRONG_GRAB_PAGE);
1153                         CDEBUG(D_READA, "g_c_p_n returned invalid page\n");
1154                         goto next_page;
1155                 }
1156
1157                 /* we do this first so that we can see the page in the /proc
1158                  * accounting */
1159                 llap = llap_from_page(page, LLAP_ORIGIN_READAHEAD);
1160                 if (IS_ERR(llap) || llap->llap_defer_uptodate)
1161                         goto next_page;
1162
1163                 /* skip completed pages */
1164                 if (Page_Uptodate(page))
1165                         goto next_page;
1166
1167                 /* bail when we hit the end of the lock. */
1168                 if ((rc = ll_page_matches(page, flags|LL_FILE_READAHEAD)) <= 0){
1169                         LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
1170                                        "lock match failed: rc %d\n", rc);
1171                         ll_ra_stats_inc(mapping, RA_STAT_FAILED_MATCH);
1172                         match_failed = 1;
1173                         goto next_page;
1174                 }
1175
1176                 rc = ll_issue_page_read(exp, llap, oig, 1);
1177                 if (rc == 0) {
1178                         reserved--;
1179                         ret++;
1180                         LL_CDEBUG_PAGE(D_READA| D_PAGE, page,
1181                                        "started read-ahead\n");
1182                 } else {
1183         next_page:
1184                         LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
1185                                        "skipping read-ahead\n");
1186
1187                         unlock_page(page);
1188                 }
1189                 page_cache_release(page);
1190         }
1191
1192         LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
1193         if (reserved != 0)
1194                 ll_ra_count_put(ll_i2sbi(inode), reserved);
1195         if (i == end + 1 && end == (kms >> CFS_PAGE_SHIFT))
1196                 ll_ra_stats_inc(mapping, RA_STAT_EOF);
1197
1198         /* if we didn't get to the end of the region we reserved from
1199          * the ras we need to go back and update the ras so that the
1200          * next read-ahead tries from where we left off.  we only do so
1201          * if the region we failed to issue read-ahead on is still ahead
1202          * of the app and behind the next index to start read-ahead from */
1203         if (i != end + 1) {
1204                 spin_lock(&ras->ras_lock);
1205                 if (i < ras->ras_next_readahead &&
1206                     index_in_window(i, ras->ras_window_start, 0,
1207                                     ras->ras_window_len)) {
1208                         ras->ras_next_readahead = i;
1209                         RAS_CDEBUG(ras);
1210                 }
1211                 spin_unlock(&ras->ras_lock);
1212         }
1213
1214         RETURN(ret);
1215 }
1216
1217 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
1218 {
1219         ras->ras_window_start = index & (~((1024 * 1024 >> CFS_PAGE_SHIFT) - 1));
1220 }
1221
1222 /* called with the ras_lock held or from places where it doesn't matter */
1223 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
1224 {
1225         ras->ras_last_readpage = index;
1226         ras->ras_consecutive_requests = 0;
1227         ras->ras_consecutive_pages = 0;
1228         ras->ras_window_len = 0;
1229         ras_set_start(ras, index);
1230         ras->ras_next_readahead = max(ras->ras_window_start, index);
1231
1232         RAS_CDEBUG(ras);
1233 }
1234
1235 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
1236 {
1237         spin_lock_init(&ras->ras_lock);
1238         ras_reset(ras, 0);
1239         ras->ras_requests = 0;
1240         INIT_LIST_HEAD(&ras->ras_read_beads);
1241 }
1242
1243 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1244                        struct ll_readahead_state *ras, unsigned long index,
1245                        unsigned hit)
1246 {
1247         struct ll_ra_info *ra = &sbi->ll_ra_info;
1248         int zero = 0;
1249         ENTRY;
1250
1251         spin_lock(&sbi->ll_lock);
1252         spin_lock(&ras->ras_lock);
1253
1254         ll_ra_stats_inc_unlocked(ra, hit ? RA_STAT_HIT : RA_STAT_MISS);
1255
1256         /* reset the read-ahead window in two cases.  First when the app seeks
1257          * or reads to some other part of the file.  Secondly if we get a
1258          * read-ahead miss that we think we've previously issued.  This can
1259          * be a symptom of there being so many read-ahead pages that the VM is
1260          * reclaiming it before we get to it. */
1261         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1262                 zero = 1;
1263                 ll_ra_stats_inc_unlocked(ra, RA_STAT_DISTANT_READPAGE);
1264         } else if (!hit && ras->ras_window_len &&
1265                    index < ras->ras_next_readahead &&
1266                    index_in_window(index, ras->ras_window_start, 0,
1267                                    ras->ras_window_len)) {
1268                 zero = 1;
1269                 ll_ra_stats_inc_unlocked(ra, RA_STAT_MISS_IN_WINDOW);
1270         }
1271
1272         /* On the second access to a file smaller than the tunable
1273          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1274          * file up to ra_max_pages.  This is simply a best effort and
1275          * only occurs once per open file.  Normal RA behavior is reverted
1276          * to for subsequent IO.  The mmap case does not increment
1277          * ras_requests and thus can never trigger this behavior. */
1278         if (ras->ras_requests == 2 && !ras->ras_request_index) {
1279                 __u64 kms_pages;
1280
1281                 kms_pages = (inode->i_size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
1282
1283                 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1284                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages);
1285
1286                 if (kms_pages &&
1287                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1288                         ras->ras_window_start = 0;
1289                         ras->ras_last_readpage = 0;
1290                         ras->ras_next_readahead = 0;
1291                         ras->ras_window_len = min(ra->ra_max_pages,
1292                                 ra->ra_max_read_ahead_whole_pages);
1293                         GOTO(out_unlock, 0);
1294                 }
1295         }
1296
1297         if (zero) {
1298                 ras_reset(ras, index);
1299                 GOTO(out_unlock, 0);
1300         }
1301
1302         ras->ras_last_readpage = index;
1303         ras->ras_consecutive_pages++;
1304         ras_set_start(ras, index);
1305         ras->ras_next_readahead = max(ras->ras_window_start,
1306                                       ras->ras_next_readahead);
1307
1308         /* Trigger RA in the mmap case where ras_consecutive_requests
1309          * is not incremented and thus can't be used to trigger RA */
1310         if (!ras->ras_window_len && ras->ras_consecutive_pages == 3) {
1311                 ras->ras_window_len = 1024 * 1024 >> CFS_PAGE_SHIFT;
1312                 GOTO(out_unlock, 0);
1313         }
1314
1315         /* The initial ras_window_len is set to the request size.  To avoid
1316          * uselessly reading and discarding pages for random IO the window is
1317          * only increased once per consecutive request received. */
1318         if (ras->ras_consecutive_requests > 1 && !ras->ras_request_index) {
1319                 ras->ras_window_len = min(ras->ras_window_len +
1320                                           (1024 * 1024 >> CFS_PAGE_SHIFT),
1321                                           ra->ra_max_pages);
1322         }
1323
1324         EXIT;
1325 out_unlock:
1326         RAS_CDEBUG(ras);
1327         ras->ras_request_index++;
1328         spin_unlock(&ras->ras_lock);
1329         spin_unlock(&sbi->ll_lock);
1330         return;
1331 }
1332
1333 int ll_writepage(struct page *page)
1334 {
1335         struct inode *inode = page->mapping->host;
1336         struct ll_inode_info *lli = ll_i2info(inode);
1337         struct obd_export *exp;
1338         struct ll_async_page *llap;
1339         int rc = 0;
1340         ENTRY;
1341
1342         LASSERT(!PageDirty(page));
1343         LASSERT(PageLocked(page));
1344
1345         exp = ll_i2obdexp(inode);
1346         if (exp == NULL)
1347                 GOTO(out, rc = -EINVAL);
1348
1349         llap = llap_from_page(page, LLAP_ORIGIN_WRITEPAGE);
1350         if (IS_ERR(llap))
1351                 GOTO(out, rc = PTR_ERR(llap));
1352
1353         page_cache_get(page);
1354         if (llap->llap_write_queued) {
1355                 LL_CDEBUG_PAGE(D_PAGE, page, "marking urgent\n");
1356                 rc = obd_set_async_flags(exp, lli->lli_smd, NULL,
1357                                          llap->llap_cookie,
1358                                          ASYNC_READY | ASYNC_URGENT);
1359         } else {
1360                 rc = queue_or_sync_write(exp, inode, llap, CFS_PAGE_SIZE,
1361                                          ASYNC_READY | ASYNC_URGENT);
1362         }
1363         if (rc)
1364                 page_cache_release(page);
1365 out:
1366         if (rc) {
1367                 if (!lli->lli_async_rc)
1368                         lli->lli_async_rc = rc;
1369                 /* re-dirty page on error so it retries write */
1370                 ll_redirty_page(page);
1371                 unlock_page(page);
1372         }
1373         RETURN(rc);
1374 }
1375
1376 /*
1377  * for now we do our readpage the same on both 2.4 and 2.5.  The kernel's
1378  * read-ahead assumes it is valid to issue readpage all the way up to
1379  * i_size, but our dlm locks make that not the case.  We disable the
1380  * kernel's read-ahead and do our own by walking ahead in the page cache
1381  * checking for dlm lock coverage.  the main difference between 2.4 and
1382  * 2.6 is how read-ahead gets batched and issued, but we're using our own,
1383  * so they look the same.
1384  */
1385 int ll_readpage(struct file *filp, struct page *page)
1386 {
1387         struct ll_file_data *fd = LUSTRE_FPRIVATE(filp);
1388         struct inode *inode = page->mapping->host;
1389         struct obd_export *exp;
1390         struct ll_async_page *llap;
1391         struct obd_io_group *oig = NULL;
1392         int rc;
1393         ENTRY;
1394
1395         LASSERT(PageLocked(page));
1396         LASSERT(!PageUptodate(page));
1397         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset=%Lu=%#Lx\n",
1398                inode->i_ino, inode->i_generation, inode,
1399                (((loff_t)page->index) << CFS_PAGE_SHIFT),
1400                (((loff_t)page->index) << CFS_PAGE_SHIFT));
1401         LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
1402
1403         if (!ll_i2info(inode)->lli_smd) {
1404                 /* File with no objects - one big hole */
1405                 /* We use this just for remove_from_page_cache that is not
1406                  * exported, we'd make page back up to date. */
1407                 ll_truncate_complete_page(page);
1408                 clear_page(kmap(page));
1409                 kunmap(page);
1410                 SetPageUptodate(page);
1411                 unlock_page(page);
1412                 RETURN(0);
1413         }
1414
1415         rc = oig_init(&oig);
1416         if (rc < 0)
1417                 GOTO(out, rc);
1418
1419         exp = ll_i2obdexp(inode);
1420         if (exp == NULL)
1421                 GOTO(out, rc = -EINVAL);
1422
1423         llap = llap_from_page(page, LLAP_ORIGIN_READPAGE);
1424         if (IS_ERR(llap))
1425                 GOTO(out, rc = PTR_ERR(llap));
1426
1427         if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages)
1428                 ras_update(ll_i2sbi(inode), inode, &fd->fd_ras, page->index,
1429                            llap->llap_defer_uptodate);
1430
1431         if (llap->llap_defer_uptodate) {
1432                 llap->llap_ra_used = 1;
1433                 rc = ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1434                                   fd->fd_flags);
1435                 if (rc > 0)
1436                         obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd,
1437                                              NULL, oig);
1438                 LL_CDEBUG_PAGE(D_PAGE, page, "marking uptodate from defer\n");
1439                 SetPageUptodate(page);
1440                 unlock_page(page);
1441                 GOTO(out_oig, rc = 0);
1442         }
1443
1444         if (likely((fd->fd_flags & LL_FILE_IGNORE_LOCK) == 0)) {
1445                 rc = ll_page_matches(page, fd->fd_flags);
1446                 if (rc < 0) {
1447                         LL_CDEBUG_PAGE(D_ERROR, page, "lock match failed: rc %d\n", rc);
1448                         GOTO(out, rc);
1449                 }
1450
1451                 if (rc == 0) {
1452                         CWARN("ino %lu page %lu (%llu) not covered by "
1453                               "a lock (mmap?).  check debug logs.\n",
1454                               inode->i_ino, page->index,
1455                               (long long)page->index << CFS_PAGE_SHIFT);
1456                 }
1457         }
1458
1459         rc = ll_issue_page_read(exp, llap, oig, 0);
1460         if (rc)
1461                 GOTO(out, rc);
1462
1463         LL_CDEBUG_PAGE(D_PAGE, page, "queued readpage\n");
1464         if (ll_i2sbi(inode)->ll_ra_info.ra_max_pages)
1465                 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1466                              fd->fd_flags);
1467
1468         rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
1469
1470 out:
1471         if (rc)
1472                 unlock_page(page);
1473 out_oig:
1474         if (oig != NULL)
1475                 oig_release(oig);
1476         RETURN(rc);
1477 }