Whamcloud - gitweb
- unland b_fid to HEAD
[fs/lustre-release.git] / lustre / llite / rw.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Lite I/O page cache routines shared by different kernel revs
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #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 /* SYNCHRONOUS I/O to object storage for an inode */
58 static int ll_brw(int cmd, struct inode *inode, struct obdo *oa,
59                   struct page *page, int flags)
60 {
61         struct ll_inode_info *lli = ll_i2info(inode);
62         struct lov_stripe_md *lsm = lli->lli_smd;
63         struct timeval start;
64         struct brw_page pg;
65         int rc;
66         ENTRY;
67
68         do_gettimeofday(&start);
69
70         pg.pg = page;
71         pg.disk_offset = pg.page_offset = ((obd_off)page->index) << PAGE_SHIFT;
72
73         if (cmd == OBD_BRW_WRITE &&
74             (pg.disk_offset + PAGE_SIZE > inode->i_size))
75                 pg.count = inode->i_size % PAGE_SIZE;
76         else
77                 pg.count = PAGE_SIZE;
78
79         CDEBUG(D_PAGE, "%s %d bytes ino %lu at "LPU64"/"LPX64"\n",
80                cmd & OBD_BRW_WRITE ? "write" : "read", pg.count, inode->i_ino,
81                pg.disk_offset, pg.disk_offset);
82         if (pg.count == 0) {
83                 CERROR("ZERO COUNT: ino %lu: size %p:%Lu(%p:%Lu) idx %lu off "
84                        LPU64"\n", inode->i_ino, inode, inode->i_size,
85                        page->mapping->host, page->mapping->host->i_size,
86                        page->index, pg.disk_offset);
87         }
88
89         pg.flag = flags;
90
91         if (cmd == OBD_BRW_WRITE)
92                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
93                                     LPROC_LL_BRW_WRITE, pg.count);
94         else
95                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
96                                     LPROC_LL_BRW_READ, pg.count);
97         rc = obd_brw(cmd, ll_i2obdexp(inode), oa, lsm, 1, &pg, NULL);
98         if (rc == 0)
99                 obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS);
100         else if (rc != -EIO)
101                 CERROR("error from obd_brw: rc = %d\n", rc);
102         ll_stime_record(ll_i2sbi(inode), &start,
103                         &ll_i2sbi(inode)->ll_brw_stime);
104         RETURN(rc);
105 }
106
107 /* this isn't where truncate starts.   roughly:
108  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
109  * we grab the lock back in setattr_raw to avoid races. */
110 void ll_truncate(struct inode *inode)
111 {
112         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
113         struct obdo oa;
114         int rc;
115         ENTRY;
116         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
117                inode->i_generation, inode);
118
119         if (!lsm) {
120                 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
121                        inode->i_ino);
122                 EXIT;
123                 return;
124         }
125
126         oa.o_id = lsm->lsm_object_id;
127         oa.o_gr = lsm->lsm_object_gr;
128         oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
129         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
130                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
131
132         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
133                oa.o_id, inode->i_size);
134
135         /* truncate == punch from new size to absolute end of file */
136         /* NB: obd_punch must be called with i_sem held!  It updates the kms! */
137         rc = obd_punch(ll_i2obdexp(inode), &oa, lsm, inode->i_size,
138                        OBD_OBJECT_EOF, NULL);
139         if (rc)
140                 CERROR("obd_truncate fails (%d) ino %lu\n", rc, inode->i_ino);
141         else
142                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
143                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
144                                           OBD_MD_FLCTIME);
145
146         EXIT;
147         return;
148 } /* ll_truncate */
149
150 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
151 int ll_prepare_write(struct file *file, struct page *page, unsigned from,
152                      unsigned to)
153 {
154         struct inode *inode = page->mapping->host;
155         struct ll_inode_info *lli = ll_i2info(inode);
156         struct lov_stripe_md *lsm = lli->lli_smd;
157         obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
158         struct brw_page pga;
159         struct obdo oa;
160         __u64 kms;
161         int rc = 0;
162         ENTRY;
163
164         LASSERT(PageLocked(page));
165         (void)llap_cast_private(page); /* assertion */
166
167         /* Check to see if we should return -EIO right away */
168         pga.pg = page;
169         pga.disk_offset = pga.page_offset = offset;
170         pga.count = PAGE_SIZE;
171         pga.flag = 0;
172
173         oa.o_id = lsm->lsm_object_id;
174         oa.o_gr = lsm->lsm_object_gr;
175         oa.o_mode = inode->i_mode;
176         oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE
177                                 | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
178
179         rc = obd_brw(OBD_BRW_CHECK, ll_i2obdexp(inode), &oa, lsm, 1, &pga,
180                      NULL);
181         if (rc)
182                 RETURN(rc);
183
184         if (PageUptodate(page))
185                 RETURN(0);
186
187         /* We're completely overwriting an existing page, so _don't_ set it up
188          * to date until commit_write */
189         if (from == 0 && to == PAGE_SIZE) {
190                 POISON_PAGE(page, 0x11);
191                 RETURN(0);
192         }
193
194         /* If are writing to a new page, no need to read old data.  The extent
195          * locking will have updated the KMS, and for our purposes here we can
196          * treat it like i_size. */
197         kms = lov_merge_size(lsm, 1);
198         if (kms <= offset) {
199                 memset(kmap(page), 0, PAGE_SIZE);
200                 kunmap(page);
201                 GOTO(prepare_done, rc = 0);
202         }
203
204         /* XXX could be an async ocp read.. read-ahead? */
205         rc = ll_brw(OBD_BRW_READ, inode, &oa, page, 0);
206         if (rc == 0) {
207                 /* bug 1598: don't clobber blksize */
208                 oa.o_valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLKSZ);
209                 obdo_refresh_inode(inode, &oa, oa.o_valid);
210         }
211
212         EXIT;
213  prepare_done:
214         if (rc == 0)
215                 SetPageUptodate(page);
216
217         return rc;
218 }
219
220 struct ll_async_page *llap_from_cookie(void *cookie)
221 {
222         struct ll_async_page *llap = cookie;
223         if (llap->llap_magic != LLAP_MAGIC)
224                 return ERR_PTR(-EINVAL);
225         return llap;
226 };
227
228 static int ll_ap_make_ready(void *data, int cmd)
229 {
230         struct ll_async_page *llap;
231         struct page *page;
232         ENTRY;
233
234         llap = llap_from_cookie(data);
235         if (IS_ERR(llap))
236                 RETURN(-EINVAL);
237
238         page = llap->llap_page;
239
240         LASSERT(cmd != OBD_BRW_READ);
241
242         /* we're trying to write, but the page is locked.. come back later */
243         if (TryLockPage(page))
244                 RETURN(-EAGAIN);
245
246         LL_CDEBUG_PAGE(D_PAGE, page, "made ready\n");
247         page_cache_get(page);
248
249         /* if we left PageDirty we might get another writepage call
250          * in the future.  list walkers are bright enough
251          * to check page dirty so we can leave it on whatever list
252          * its on.  XXX also, we're called with the cli list so if
253          * we got the page cache list we'd create a lock inversion
254          * with the removepage path which gets the page lock then the
255          * cli lock */
256         clear_page_dirty(page);
257         RETURN(0);
258 }
259
260 /* We have two reasons for giving llite the opportunity to change the 
261  * write length of a given queued page as it builds the RPC containing
262  * the page: 
263  *
264  * 1) Further extending writes may have landed in the page cache
265  *    since a partial write first queued this page requiring us
266  *    to write more from the page cache.
267  * 2) We might have raced with truncate and want to avoid performing
268  *    write RPCs that are just going to be thrown away by the 
269  *    truncate's punch on the storage targets.
270  *
271  * The kms serves these purposes as it is set at both truncate and extending
272  * writes.
273  */
274 static int ll_ap_refresh_count(void *data, int cmd)
275 {
276         struct ll_async_page *llap;
277         struct lov_stripe_md *lsm;
278         struct page *page;
279         __u64 kms;
280         ENTRY;
281
282         /* readpage queues with _COUNT_STABLE, shouldn't get here. */
283         LASSERT(cmd != OBD_BRW_READ);
284
285         llap = llap_from_cookie(data);
286         if (IS_ERR(llap))
287                 RETURN(PTR_ERR(llap));
288
289         page = llap->llap_page;
290         lsm = ll_i2info(page->mapping->host)->lli_smd;
291         kms = lov_merge_size(lsm, 1);
292
293         /* catch race with truncate */
294         if (((__u64)page->index << PAGE_SHIFT) >= kms)
295                 return 0;
296
297         /* catch sub-page write at end of file */
298         if (((__u64)page->index << PAGE_SHIFT) + PAGE_SIZE > kms)
299                 return kms % PAGE_SIZE;
300
301         return PAGE_SIZE;
302 }
303
304 void ll_inode_fill_obdo(struct inode *inode, int cmd, struct obdo *oa)
305 {
306         struct lov_stripe_md *lsm;
307         obd_flag valid_flags;
308
309         lsm = ll_i2info(inode)->lli_smd;
310
311         oa->o_id = lsm->lsm_object_id;
312         oa->o_gr = lsm->lsm_object_gr;
313         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
314         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
315         if (cmd == OBD_BRW_WRITE) {
316                 oa->o_valid |= OBD_MD_FLIFID | OBD_MD_FLEPOCH;
317                 mdc_pack_fid(obdo_fid(oa), inode->i_ino, 0, inode->i_mode);
318                 obdo_fid(oa)->mds = ll_i2info(inode)->lli_mds;
319                 oa->o_easize = ll_i2info(inode)->lli_io_epoch;
320
321                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
322         }
323
324         obdo_from_inode(oa, inode, valid_flags);
325 }
326
327 static void ll_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
328 {
329         struct ll_async_page *llap;
330         ENTRY;
331
332         llap = llap_from_cookie(data);
333         if (IS_ERR(llap)) {
334                 EXIT;
335                 return;
336         }
337
338         ll_inode_fill_obdo(llap->llap_page->mapping->host, cmd, oa);
339         EXIT;
340 }
341
342 static struct obd_async_page_ops ll_async_page_ops = {
343         .ap_make_ready =        ll_ap_make_ready,
344         .ap_refresh_count =     ll_ap_refresh_count,
345         .ap_fill_obdo =         ll_ap_fill_obdo,
346         .ap_completion =        ll_ap_completion,
347 };
348
349 struct ll_async_page *llap_cast_private(struct page *page)
350 {
351         struct ll_async_page *llap = (struct ll_async_page *)page->private;
352
353         LASSERTF(llap == NULL || llap->llap_magic == LLAP_MAGIC,
354                  "page %p private %lu gave magic %d which != %d\n",
355                  page, page->private, llap->llap_magic, LLAP_MAGIC);
356
357         return llap;
358 }
359
360 /* XXX have the exp be an argument? */
361 struct ll_async_page *llap_from_page(struct page *page)
362 {
363         struct ll_async_page *llap;
364         struct obd_export *exp;
365         struct inode *inode = page->mapping->host;
366         struct ll_sb_info *sbi = ll_i2sbi(inode);
367         int rc;
368         ENTRY;
369
370         llap = llap_cast_private(page);
371         if (llap != NULL)
372                 RETURN(llap);
373
374         exp = ll_i2obdexp(page->mapping->host);
375         if (exp == NULL)
376                 RETURN(ERR_PTR(-EINVAL));
377
378         OBD_ALLOC(llap, sizeof(*llap));
379         if (llap == NULL)
380                 RETURN(ERR_PTR(-ENOMEM));
381         llap->llap_magic = LLAP_MAGIC;
382         rc = obd_prep_async_page(exp, ll_i2info(inode)->lli_smd, NULL, page,
383                                  (obd_off)page->index << PAGE_SHIFT,
384                                  &ll_async_page_ops, llap, &llap->llap_cookie);
385         if (rc) {
386                 OBD_FREE(llap, sizeof(*llap));
387                 RETURN(ERR_PTR(rc));
388         }
389
390         CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n", llap,
391                page, llap->llap_cookie, (obd_off)page->index << PAGE_SHIFT);
392         /* also zeroing the PRIVBITS low order bitflags */
393         __set_page_ll_data(page, llap);
394         llap->llap_page = page;
395
396         spin_lock(&sbi->ll_lock);
397         sbi->ll_pglist_gen++;
398         list_add_tail(&llap->llap_proc_item, &sbi->ll_pglist);
399         spin_unlock(&sbi->ll_lock);
400
401         RETURN(llap);
402 }
403
404 static int queue_or_sync_write(struct obd_export *exp,
405                                struct lov_stripe_md *lsm,
406                                struct ll_async_page *llap,
407                                unsigned to,
408                                obd_flag async_flags)
409 {
410         struct obd_io_group *oig;
411         int rc;
412         ENTRY;
413
414         /* _make_ready only sees llap once we've unlocked the page */
415         llap->llap_write_queued = 1;
416         rc = obd_queue_async_io(exp, lsm, NULL, llap->llap_cookie,
417                                 OBD_BRW_WRITE, 0, 0, 0, async_flags);
418         if (rc == 0) {
419                 LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "write queued\n");
420                 //llap_write_pending(inode, llap);
421                 GOTO(out, 0);
422         }
423
424         llap->llap_write_queued = 0;
425
426         rc = oig_init(&oig);
427         if (rc)
428                 GOTO(out, rc);
429
430         rc = obd_queue_group_io(exp, lsm, NULL, oig, llap->llap_cookie,
431                                 OBD_BRW_WRITE, 0, to, 0, ASYNC_READY |
432                                 ASYNC_URGENT | ASYNC_COUNT_STABLE |
433                                 ASYNC_GROUP_SYNC);
434         if (rc)
435                 GOTO(free_oig, rc);
436
437         rc = obd_trigger_group_io(exp, lsm, NULL, oig);
438         if (rc)
439                 GOTO(free_oig, rc);
440
441         rc = oig_wait(oig);
442
443         if (!rc && async_flags & ASYNC_READY)
444                 unlock_page(llap->llap_page);
445
446         LL_CDEBUG_PAGE(D_PAGE, llap->llap_page, "sync write returned %d\n",
447                        rc);
448
449 free_oig:
450         oig_release(oig);
451 out:
452         RETURN(rc);
453 }
454
455 void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
456                       obd_off size);
457
458 /* be careful not to return success without setting the page Uptodate or
459  * the next pass through prepare_write will read in stale data from disk. */
460 int ll_commit_write(struct file *file, struct page *page, unsigned from,
461                     unsigned to)
462 {
463         struct inode *inode = page->mapping->host;
464         struct ll_inode_info *lli = ll_i2info(inode);
465         struct lov_stripe_md *lsm = lli->lli_smd;
466         struct obd_export *exp = NULL;
467         struct ll_async_page *llap;
468         loff_t size;
469         int rc = 0;
470         ENTRY;
471
472         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
473         LASSERT(inode == file->f_dentry->d_inode);
474         LASSERT(PageLocked(page));
475
476         CDEBUG(D_INODE, "inode %p is writing page %p from %d to %d at %lu\n",
477                inode, page, from, to, page->index);
478
479         llap = llap_from_page(page);
480         if (IS_ERR(llap))
481                 RETURN(PTR_ERR(llap));
482
483         /* queue a write for some time in the future the first time we
484          * dirty the page */
485         if (!PageDirty(page)) {
486                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
487                                      LPROC_LL_DIRTY_MISSES);
488
489                 exp = ll_i2obdexp(inode);
490                 if (exp == NULL)
491                         RETURN(-EINVAL);
492
493                 rc = queue_or_sync_write(exp, ll_i2info(inode)->lli_smd, llap,
494                                          to, 0);
495                 if (rc)
496                         GOTO(out, rc);
497         } else {
498                 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats,
499                                      LPROC_LL_DIRTY_HITS);
500         }
501
502         /* put the page in the page cache, from now on ll_removepage is
503          * responsible for cleaning up the llap.
504          * don't dirty the page if it has been write out in q_o_s_w */
505         if (llap->llap_write_queued)
506                 set_page_dirty(page);
507
508 out:
509         if (rc == 0) {
510                 size = (((obd_off)page->index) << PAGE_SHIFT) + to;
511                 lov_increase_kms(exp, lsm, size);
512                 if (size > inode->i_size)
513                         inode->i_size = size;
514                 SetPageUptodate(page);
515         }
516         RETURN(rc);
517 }
518                                                                                                                                                                                                      
519 int ll_writepage(struct page *page)
520 {
521         struct inode *inode = page->mapping->host;
522         struct obd_export *exp;
523         struct ll_async_page *llap;
524         int rc = 0;
525         ENTRY;
526                                                                                                                                                                                                      
527         LASSERT(!PageDirty(page));
528         LASSERT(PageLocked(page));
529                                                                                                                                                                                                      
530         exp = ll_i2obdexp(inode);
531         if (exp == NULL)
532                 GOTO(out, rc = -EINVAL);
533                                                                                                                                                                                                      
534         llap = llap_from_page(page);
535         if (IS_ERR(llap))
536                 GOTO(out, rc = PTR_ERR(llap));
537                                                                                                                                                                                                      
538         page_cache_get(page);
539         if (llap->llap_write_queued) {
540                 LL_CDEBUG_PAGE(D_PAGE, page, "marking urgent\n");
541                 rc = obd_set_async_flags(exp, ll_i2info(inode)->lli_smd, NULL,
542                                          llap->llap_cookie,
543                                          ASYNC_READY | ASYNC_URGENT);
544         } else {
545                 rc = queue_or_sync_write(exp, ll_i2info(inode)->lli_smd, llap,
546                                          PAGE_SIZE, ASYNC_READY |
547                                          ASYNC_URGENT);
548         }
549         if (rc)
550                 page_cache_release(page);
551 out:
552         if (rc)
553                 unlock_page(page);
554         RETURN(rc);
555 }
556
557 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
558 {
559         struct ll_ra_info *ra = &sbi->ll_ra_info;
560         unsigned long ret;
561         ENTRY;
562
563         spin_lock(&sbi->ll_lock);
564         ret = min(ra->ra_max_pages - ra->ra_cur_pages, len);
565         ra->ra_cur_pages += ret;
566         spin_unlock(&sbi->ll_lock);
567
568         RETURN(ret);
569 }
570
571 static void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
572 {
573         struct ll_ra_info *ra = &sbi->ll_ra_info;
574         spin_lock(&sbi->ll_lock);
575         LASSERTF(ra->ra_cur_pages >= len, "r_c_p %lu len %lu\n",
576                  ra->ra_cur_pages, len);
577         ra->ra_cur_pages -= len;
578         spin_unlock(&sbi->ll_lock);
579 }
580
581 /* called for each page in a completed rpc.*/
582 void ll_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
583 {
584         struct ll_async_page *llap;
585         struct page *page;
586         ENTRY;
587
588         llap = llap_from_cookie(data);
589         if (IS_ERR(llap)) {
590                 EXIT;
591                 return;
592         }
593
594         page = llap->llap_page;
595         LASSERT(PageLocked(page));
596
597         LL_CDEBUG_PAGE(D_PAGE, page, "completing cmd %d with %d\n", cmd, rc);
598
599         if (cmd == OBD_BRW_READ && llap->llap_defer_uptodate)
600                 ll_ra_count_put(ll_i2sbi(page->mapping->host), 1);
601
602         if (rc == 0)  {
603                 if (cmd == OBD_BRW_READ) {
604                         if (!llap->llap_defer_uptodate)
605                                 SetPageUptodate(page);
606                 } else {
607                         llap->llap_write_queued = 0;
608                 }
609                 ClearPageError(page);
610         } else {
611                 if (cmd == OBD_BRW_READ)
612                         llap->llap_defer_uptodate = 0;
613                 SetPageError(page);
614         }
615
616         unlock_page(page);
617
618         if (0 && cmd == OBD_BRW_WRITE) {
619                 llap_write_complete(page->mapping->host, llap);
620                 ll_try_done_writing(page->mapping->host);
621         }
622         
623         if (PageWriteback(page)) {
624                 end_page_writeback(page);
625         }
626         page_cache_release(page);
627         EXIT;
628 }
629
630 /* the kernel calls us here when a page is unhashed from the page cache.
631  * the page will be locked and the kernel is holding a spinlock, so
632  * we need to be careful.  we're just tearing down our book-keeping
633  * here. */
634 void ll_removepage(struct page *page)
635 {
636         struct inode *inode = page->mapping->host;
637         struct obd_export *exp;
638         struct ll_async_page *llap;
639         struct ll_sb_info *sbi = ll_i2sbi(inode);
640         int rc;
641         ENTRY;
642
643         LASSERT(!in_interrupt());
644
645         /* sync pages or failed read pages can leave pages in the page
646          * cache that don't have our data associated with them anymore */
647         if (page->private == 0) {
648                 EXIT;
649                 return;
650         }
651
652         LL_CDEBUG_PAGE(D_PAGE, page, "being evicted\n");
653
654         exp = ll_i2obdexp(inode);
655         if (exp == NULL) {
656                 CERROR("page %p ind %lu gave null export\n", page, page->index);
657                 EXIT;
658                 return;
659         }
660
661         llap = llap_from_page(page);
662         if (IS_ERR(llap)) {
663                 CERROR("page %p ind %lu couldn't find llap: %ld\n", page,
664                        page->index, PTR_ERR(llap));
665                 EXIT;
666                 return;
667         }
668
669         //llap_write_complete(inode, llap);
670         rc = obd_teardown_async_page(exp, ll_i2info(inode)->lli_smd, NULL,
671                                      llap->llap_cookie);
672         if (rc != 0)
673                 CERROR("page %p ind %lu failed: %d\n", page, page->index, rc);
674
675         /* this unconditional free is only safe because the page lock
676          * is providing exclusivity to memory pressure/truncate/writeback..*/
677         __clear_page_ll_data(page);
678
679         spin_lock(&sbi->ll_lock);
680         if (!list_empty(&llap->llap_proc_item))
681                 list_del_init(&llap->llap_proc_item);
682         sbi->ll_pglist_gen++;
683         spin_unlock(&sbi->ll_lock);
684         OBD_FREE(llap, sizeof(*llap));
685         EXIT;
686 }
687
688 static int ll_page_matches(struct page *page, int fd_flags)
689 {
690         struct lustre_handle match_lockh = {0};
691         struct inode *inode = page->mapping->host;
692         ldlm_policy_data_t page_extent;
693         int flags, matches;
694         ENTRY;
695
696         if (fd_flags & LL_FILE_GROUP_LOCKED)
697                 RETURN(1);
698
699         page_extent.l_extent.start = (__u64)page->index << PAGE_CACHE_SHIFT;
700         page_extent.l_extent.end =
701                 page_extent.l_extent.start + PAGE_CACHE_SIZE - 1;
702         flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
703         matches = obd_match(ll_i2sbi(inode)->ll_osc_exp,
704                             ll_i2info(inode)->lli_smd, LDLM_EXTENT,
705                             &page_extent, LCK_PR | LCK_PW, &flags, inode,
706                             &match_lockh);
707         RETURN(matches);
708 }
709
710 static int ll_issue_page_read(struct obd_export *exp,
711                               struct ll_async_page *llap,
712                               struct obd_io_group *oig, int defer)
713 {
714         struct page *page = llap->llap_page;
715         int rc;
716
717         page_cache_get(page);
718         llap->llap_defer_uptodate = defer;
719         llap->llap_ra_used = 0;
720         rc = obd_queue_group_io(exp, ll_i2info(page->mapping->host)->lli_smd,
721                                 NULL, oig, llap->llap_cookie, OBD_BRW_READ, 0,
722                                 PAGE_SIZE, 0, ASYNC_COUNT_STABLE | ASYNC_READY
723                                               | ASYNC_URGENT);
724         if (rc) {
725                 LL_CDEBUG_PAGE(D_ERROR, page, "read queue failed: rc %d\n", rc);
726                 page_cache_release(page);
727         }
728         RETURN(rc);
729 }
730
731 static void ll_ra_stats_inc_unlocked(struct ll_ra_info *ra, enum ra_stat which)
732 {
733         LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
734         ra->ra_stats[which]++;
735 }
736
737 static void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
738 {
739         struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
740         struct ll_ra_info *ra = &ll_i2sbi(mapping->host)->ll_ra_info;
741
742         spin_lock(&sbi->ll_lock);
743         ll_ra_stats_inc_unlocked(ra, which);
744         spin_unlock(&sbi->ll_lock);
745 }
746
747 void ll_ra_accounting(struct page *page, struct address_space *mapping)
748 {
749         struct ll_async_page *llap;
750
751         llap = llap_from_page(page);
752         if (IS_ERR(llap))
753                 return;
754
755         if (!llap->llap_defer_uptodate || llap->llap_ra_used)
756                 return;
757
758         ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
759 }
760
761 #define RAS_CDEBUG(ras) \
762         CDEBUG(D_READA, "lrp %lu c %lu ws %lu wl %lu nra %lu\n",        \
763                ras->ras_last_readpage, ras->ras_consecutive,            \
764                ras->ras_window_start, ras->ras_window_len,              \
765                ras->ras_next_readahead);
766
767 static int index_in_window(unsigned long index, unsigned long point,
768                            unsigned long before, unsigned long after)
769 {
770         unsigned long start = point - before, end = point + after;
771
772         if (start > point)
773                start = 0;
774         if (end < point)
775                end = ~0;
776
777         return start <= index && index <= end;
778 }
779
780 static int ll_readahead(struct ll_readahead_state *ras,
781                          struct obd_export *exp, struct address_space *mapping,
782                          struct obd_io_group *oig, int flags)
783 {
784         unsigned long i, start = 0, end = 0, reserved;
785         struct ll_async_page *llap;
786         struct page *page;
787         int rc, ret = 0, match_failed = 0;
788         __u64 kms;
789         ENTRY;
790
791         kms = lov_merge_size(ll_i2info(mapping->host)->lli_smd, 1);
792         if (kms == 0) {
793                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
794                 RETURN(0);
795         }
796         spin_lock(&ras->ras_lock);
797
798         /* reserve a part of the read-ahead window that we'll be issuing */
799         if (ras->ras_window_len) {
800                 start = ras->ras_next_readahead;
801                 end = ras->ras_window_start + ras->ras_window_len - 1;
802                 end = min(end, (unsigned long)(kms >> PAGE_CACHE_SHIFT));
803                 ras->ras_next_readahead = max(end, end + 1);
804
805                 RAS_CDEBUG(ras);
806         }
807
808         spin_unlock(&ras->ras_lock);
809
810         if (end == 0) {
811                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
812                 RETURN(0);
813         }
814
815         reserved = ll_ra_count_get(ll_i2sbi(mapping->host), end - start + 1);
816         if (reserved < end - start + 1)
817                 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
818
819         for (i = start; reserved > 0 && !match_failed && i <= end; i++) {
820                 /* skip locked pages from previous readpage calls */
821                 page = grab_cache_page_nowait(mapping, i);
822                 if (page == NULL) {
823                         CDEBUG(D_READA, "g_c_p_n failed\n");
824                         continue;
825                 }
826                 
827                 /* we do this first so that we can see the page in the /proc
828                  * accounting */
829                 llap = llap_from_page(page);
830                 if (IS_ERR(llap) || llap->llap_defer_uptodate)
831                         goto next_page;
832
833                 /* skip completed pages */
834                 if (Page_Uptodate(page))
835                         goto next_page;
836
837                 /* bail when we hit the end of the lock. */
838                 if ((rc = ll_page_matches(page, flags)) <= 0) {
839                         LL_CDEBUG_PAGE(D_READA | D_PAGE, page,
840                                        "lock match failed: rc %d\n", rc);
841                         ll_ra_stats_inc(mapping, RA_STAT_FAILED_MATCH);
842                         match_failed = 1;
843                         goto next_page;
844                 }
845
846                 rc = ll_issue_page_read(exp, llap, oig, 1);
847                 if (rc == 0) {
848                         reserved--;
849                         ret++;
850                         LL_CDEBUG_PAGE(D_READA| D_PAGE, page, 
851                                        "started read-ahead\n");
852                 }
853                 if (rc) {
854         next_page:
855                         LL_CDEBUG_PAGE(D_READA | D_PAGE, page, 
856                                        "skipping read-ahead\n");
857
858                         unlock_page(page);
859                 }
860                 page_cache_release(page);
861         }
862
863         LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
864         if (reserved != 0)
865                 ll_ra_count_put(ll_i2sbi(mapping->host), reserved);
866
867         if (i == end + 1 && end == (kms >> PAGE_CACHE_SHIFT))
868                 ll_ra_stats_inc(mapping, RA_STAT_EOF);
869
870         /* if we didn't get to the end of the region we reserved from
871          * the ras we need to go back and update the ras so that the
872          * next read-ahead tries from where we left off.  we only do so
873          * if the region we failed to issue read-ahead on is still ahead
874          * of the app and behind the next index to start read-ahead from */
875         if (i != end + 1) {
876                 spin_lock(&ras->ras_lock);
877                 if (i < ras->ras_next_readahead &&
878                     index_in_window(i, ras->ras_window_start, 0,
879                                     ras->ras_window_len)) {
880                         ras->ras_next_readahead = i;
881                         RAS_CDEBUG(ras);
882                 }
883                 spin_unlock(&ras->ras_lock);
884         }
885
886         RETURN(ret);
887 }
888
889 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
890 {
891         ras->ras_window_start = index & (~(PTLRPC_MAX_BRW_PAGES - 1));
892 }
893
894 /* called with the ras_lock held or from places where it doesn't matter */
895 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
896 {
897         ras->ras_last_readpage = index;
898         ras->ras_consecutive = 1;
899         ras->ras_window_len = 0;
900         ras_set_start(ras, index);
901         ras->ras_next_readahead = ras->ras_window_start;
902
903         RAS_CDEBUG(ras);
904 }
905
906 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
907 {
908         spin_lock_init(&ras->ras_lock);
909         ras_reset(ras, 0);
910 }
911
912 static void ras_update(struct ll_sb_info *sbi, struct ll_readahead_state *ras,
913                        unsigned long index, unsigned hit)
914 {
915         struct ll_ra_info *ra = &sbi->ll_ra_info;
916         int zero = 0;
917         ENTRY;
918
919         spin_lock(&sbi->ll_lock);
920         spin_lock(&ras->ras_lock);
921         
922         ll_ra_stats_inc_unlocked(ra, hit ? RA_STAT_HIT : RA_STAT_MISS);
923
924         /* reset the read-ahead window in two cases.  First when the app seeks
925          * or reads to some other part of the file.  Secondly if we get a
926          * read-ahead miss that we think we've previously issued.  This can
927          * be a symptom of there being so many read-ahead pages that the VM is
928          * reclaiming it before we get to it. */
929         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
930                 zero = 1;
931                 ll_ra_stats_inc_unlocked(ra, RA_STAT_DISTANT_READPAGE);
932         } else if (!hit && ras->ras_window_len &&
933                    index < ras->ras_next_readahead &&
934                    index_in_window(index, ras->ras_window_start, 0,
935                                    ras->ras_window_len)) {
936                 zero = 1;
937                 ll_ra_stats_inc_unlocked(ra, RA_STAT_MISS_IN_WINDOW);
938         }
939
940         if (zero) {
941                 ras_reset(ras, index);
942                 GOTO(out_unlock, 0);
943         }
944
945         ras->ras_last_readpage = index;
946         ras->ras_consecutive++;
947         ras_set_start(ras, index);
948         ras->ras_next_readahead = max(ras->ras_window_start,
949                                       ras->ras_next_readahead);
950
951         /* wait for a few pages to arrive before issuing readahead to avoid
952          * the worst overutilization */
953         if (ras->ras_consecutive == 3) {
954                 ras->ras_window_len = PTLRPC_MAX_BRW_PAGES;
955                 GOTO(out_unlock, 0);
956         }
957
958         /* we need to increase the window sometimes.  we'll arbitrarily
959          * do it half-way through the pages in an rpc */
960         if ((index & (PTLRPC_MAX_BRW_PAGES - 1)) == 
961             (PTLRPC_MAX_BRW_PAGES >> 1)) {
962                 ras->ras_window_len += PTLRPC_MAX_BRW_PAGES;
963                 ras->ras_window_len = min(ras->ras_window_len,
964                                           ra->ra_max_pages);
965
966         }
967
968         EXIT;
969 out_unlock:
970         RAS_CDEBUG(ras);
971         spin_unlock(&ras->ras_lock);
972         spin_unlock(&sbi->ll_lock);
973         return;
974 }
975
976 /*
977  * for now we do our readpage the same on both 2.4 and 2.5.  The kernel's
978  * read-ahead assumes it is valid to issue readpage all the way up to
979  * i_size, but our dlm locks make that not the case.  We disable the
980  * kernel's read-ahead and do our own by walking ahead in the page cache
981  * checking for dlm lock coverage.  the main difference between 2.4 and
982  * 2.6 is how read-ahead gets batched and issued, but we're using our own,
983  * so they look the same.
984  */
985 int ll_readpage(struct file *filp, struct page *page)
986 {
987         struct ll_file_data *fd = filp->private_data;
988         struct inode *inode = page->mapping->host;
989         struct obd_export *exp;
990         struct ll_async_page *llap;
991         struct obd_io_group *oig = NULL;
992         int rc;
993         ENTRY;
994
995         LASSERT(PageLocked(page));
996         LASSERT(!PageUptodate(page));
997         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),offset="LPX64"\n",
998                inode->i_ino, inode->i_generation, inode,
999                (((obd_off)page->index) << PAGE_SHIFT));
1000         LASSERT(atomic_read(&filp->f_dentry->d_inode->i_count) > 0);
1001
1002         rc = oig_init(&oig);
1003         if (rc < 0)
1004                 GOTO(out, rc);
1005
1006         exp = ll_i2obdexp(inode);
1007         if (exp == NULL)
1008                 GOTO(out, rc = -EINVAL);
1009
1010         llap = llap_from_page(page);
1011         if (IS_ERR(llap))
1012                 GOTO(out, rc = PTR_ERR(llap));
1013
1014         if (ll_i2sbi(inode)->ll_flags & LL_SBI_READAHEAD)
1015                 ras_update(ll_i2sbi(inode), &fd->fd_ras, page->index,
1016                            llap->llap_defer_uptodate);
1017         
1018         if (llap->llap_defer_uptodate) {
1019                 llap->llap_ra_used = 1;
1020                 rc = ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1021                                   fd->fd_flags);
1022                 if (rc > 0)
1023                         obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, 
1024                                              NULL, oig);
1025                 LL_CDEBUG_PAGE(D_PAGE, page, "marking uptodate from defer\n");
1026                 SetPageUptodate(page);
1027                 unlock_page(page);
1028                 GOTO(out_oig, rc = 0);
1029         }
1030
1031         rc = ll_page_matches(page, fd->fd_flags);
1032         if (rc < 0) {
1033                 LL_CDEBUG_PAGE(D_ERROR, page, "lock match failed: rc %d\n", rc);
1034                 GOTO(out, rc);
1035         }
1036
1037         if (rc == 0) {
1038                 CWARN("ino %lu page %lu (%llu) not covered by "
1039                       "a lock (mmap?).  check debug logs.\n",
1040                       inode->i_ino, page->index,
1041                       (long long)page->index << PAGE_CACHE_SHIFT);
1042         }
1043
1044         rc = ll_issue_page_read(exp, llap, oig, 0);
1045         if (rc)
1046                 GOTO(out, rc);
1047
1048         LL_CDEBUG_PAGE(D_PAGE, page, "queued readpage\n");
1049         if (ll_i2sbi(inode)->ll_flags & LL_SBI_READAHEAD)
1050                 ll_readahead(&fd->fd_ras, exp, page->mapping, oig,
1051                              fd->fd_flags);
1052
1053         rc = obd_trigger_group_io(exp, ll_i2info(inode)->lli_smd, NULL, oig);
1054
1055 out:
1056         if (rc)
1057                 unlock_page(page);
1058 out_oig:
1059         if (oig != NULL)
1060                 oig_release(oig);
1061         RETURN(rc);
1062 }