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