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