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