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