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