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