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