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