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