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