Whamcloud - gitweb
LU-1030 osc: new IO engine implementation
[fs/lustre-release.git] / lustre / llite / rw.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/rw.c
37  *
38  * Lustre Lite I/O page cache routines shared by different kernel revs
39  */
40
41 #include <linux/kernel.h>
42 #include <linux/mm.h>
43 #include <linux/string.h>
44 #include <linux/stat.h>
45 #include <linux/errno.h>
46 #include <linux/smp_lock.h>
47 #include <linux/unistd.h>
48 #include <linux/version.h>
49 #include <asm/system.h>
50 #include <asm/uaccess.h>
51
52 #include <linux/fs.h>
53 #include <linux/stat.h>
54 #include <asm/uaccess.h>
55 #include <linux/mm.h>
56 #include <linux/pagemap.h>
57 #include <linux/smp_lock.h>
58 /* current_is_kswapd() */
59 #include <linux/swap.h>
60
61 #define DEBUG_SUBSYSTEM S_LLITE
62
63 #include <lustre_lite.h>
64 #include <obd_cksum.h>
65 #include "llite_internal.h"
66 #include <linux/lustre_compat25.h>
67
68 /* this isn't where truncate starts.   roughly:
69  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate. setattr_raw grabs
70  * DLM lock on [size, EOF], i_mutex, ->lli_size_sem, and WRITE_I_ALLOC_SEM to
71  * avoid races.
72  *
73  * must be called under ->lli_size_sem */
74 void ll_truncate(struct inode *inode)
75 {
76         ENTRY;
77
78         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %llu\n", inode->i_ino,
79                inode->i_generation, inode, i_size_read(inode));
80
81         EXIT;
82         return;
83 } /* ll_truncate */
84
85 /**
86  * Finalizes cl-data before exiting typical address_space operation. Dual to
87  * ll_cl_init().
88  */
89 static void ll_cl_fini(struct ll_cl_context *lcc)
90 {
91         struct lu_env  *env  = lcc->lcc_env;
92         struct cl_io   *io   = lcc->lcc_io;
93         struct cl_page *page = lcc->lcc_page;
94
95         LASSERT(lcc->lcc_cookie == current);
96         LASSERT(env != NULL);
97
98         if (page != NULL) {
99                 lu_ref_del(&page->cp_reference, "cl_io", io);
100                 cl_page_put(env, page);
101         }
102
103         if (io && lcc->lcc_created) {
104                 cl_io_end(env, io);
105                 cl_io_unlock(env, io);
106                 cl_io_iter_fini(env, io);
107                 cl_io_fini(env, io);
108         }
109         cl_env_put(env, &lcc->lcc_refcheck);
110 }
111
112 /**
113  * Initializes common cl-data at the typical address_space operation entry
114  * point.
115  */
116 static struct ll_cl_context *ll_cl_init(struct file *file,
117                                         struct page *vmpage, int create)
118 {
119         struct ll_cl_context *lcc;
120         struct lu_env    *env;
121         struct cl_io     *io;
122         struct cl_object *clob;
123         struct ccc_io    *cio;
124
125         int refcheck;
126         int result = 0;
127
128         clob = ll_i2info(vmpage->mapping->host)->lli_clob;
129         LASSERT(clob != NULL);
130
131         env = cl_env_get(&refcheck);
132         if (IS_ERR(env))
133                 return ERR_PTR(PTR_ERR(env));
134
135         lcc = &vvp_env_info(env)->vti_io_ctx;
136         memset(lcc, 0, sizeof(*lcc));
137         lcc->lcc_env = env;
138         lcc->lcc_refcheck = refcheck;
139         lcc->lcc_cookie = current;
140
141         cio = ccc_env_io(env);
142         io = cio->cui_cl.cis_io;
143         if (io == NULL && create) {
144                 struct inode *inode = vmpage->mapping->host;
145                 loff_t pos;
146
147                 if (TRYLOCK_INODE_MUTEX(inode)) {
148                         UNLOCK_INODE_MUTEX(inode);
149
150                         /* this is too bad. Someone is trying to write the
151                          * page w/o holding inode mutex. This means we can
152                          * add dirty pages into cache during truncate */
153                         CERROR("Proc %s is dirting page w/o inode lock, this"
154                                "will break truncate.\n", cfs_current()->comm);
155                         libcfs_debug_dumpstack(NULL);
156                         LBUG();
157                         return ERR_PTR(-EIO);
158                 }
159
160                 /*
161                  * Loop-back driver calls ->prepare_write() and ->sendfile()
162                  * methods directly, bypassing file system ->write() operation,
163                  * so cl_io has to be created here.
164                  */
165                 io = ccc_env_thread_io(env);
166                 ll_io_init(io, file, 1);
167
168                 /* No lock at all for this kind of IO - we can't do it because
169                  * we have held page lock, it would cause deadlock.
170                  * XXX: This causes poor performance to loop device - One page
171                  *      per RPC.
172                  *      In order to get better performance, users should use
173                  *      lloop driver instead.
174                  */
175                 io->ci_lockreq = CILR_NEVER;
176
177                 pos = (vmpage->index << CFS_PAGE_SHIFT);
178
179                 /* Create a temp IO to serve write. */
180                 result = cl_io_rw_init(env, io, CIT_WRITE, pos, CFS_PAGE_SIZE);
181                 if (result == 0) {
182                         cio->cui_fd = LUSTRE_FPRIVATE(file);
183                         cio->cui_iov = NULL;
184                         cio->cui_nrsegs = 0;
185                         result = cl_io_iter_init(env, io);
186                         if (result == 0) {
187                                 result = cl_io_lock(env, io);
188                                 if (result == 0)
189                                         result = cl_io_start(env, io);
190                         }
191                 } else
192                         result = io->ci_result;
193                 lcc->lcc_created = 1;
194         }
195
196         lcc->lcc_io = io;
197         if (io == NULL)
198                 result = -EIO;
199         if (result == 0) {
200                 struct cl_page   *page;
201
202                 LASSERT(io != NULL);
203                 LASSERT(io->ci_state == CIS_IO_GOING);
204                 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
205                 page = cl_page_find(env, clob, vmpage->index, vmpage,
206                                     CPT_CACHEABLE);
207                 if (!IS_ERR(page)) {
208                         lcc->lcc_page = page;
209                         lu_ref_add(&page->cp_reference, "cl_io", io);
210                         result = 0;
211                 } else
212                         result = PTR_ERR(page);
213         }
214         if (result) {
215                 ll_cl_fini(lcc);
216                 lcc = ERR_PTR(result);
217         }
218
219         CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %d %p %p\n",
220                vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
221                env, io);
222         return lcc;
223 }
224
225 static struct ll_cl_context *ll_cl_get(void)
226 {
227         struct ll_cl_context *lcc;
228         struct lu_env *env;
229         int refcheck;
230
231         env = cl_env_get(&refcheck);
232         LASSERT(!IS_ERR(env));
233         lcc = &vvp_env_info(env)->vti_io_ctx;
234         LASSERT(env == lcc->lcc_env);
235         LASSERT(current == lcc->lcc_cookie);
236         cl_env_put(env, &refcheck);
237
238         /* env has got in ll_cl_init, so it is still usable. */
239         return lcc;
240 }
241
242 /**
243  * ->prepare_write() address space operation called by generic_file_write()
244  * for every page during write.
245  */
246 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
247                      unsigned to)
248 {
249         struct ll_cl_context *lcc;
250         int result;
251         ENTRY;
252
253         lcc = ll_cl_init(file, vmpage, 1);
254         if (!IS_ERR(lcc)) {
255                 struct lu_env  *env = lcc->lcc_env;
256                 struct cl_io   *io  = lcc->lcc_io;
257                 struct cl_page *page = lcc->lcc_page;
258
259                 cl_page_assume(env, io, page);
260                 if (cl_io_is_append(io)) {
261                         struct cl_object   *obj   = io->ci_obj;
262                         struct inode       *inode = ccc_object_inode(obj);
263                         /**
264                          * In VFS file->page write loop, for appending, the
265                          * write offset might be reset according to the new
266                          * file size before holding i_mutex. So crw_pos should
267                          * be reset here. BUG:17711.
268                          */
269                         io->u.ci_wr.wr.crw_pos = i_size_read(inode);
270                 }
271                 result = cl_io_prepare_write(env, io, page, from, to);
272                 if (result == 0) {
273                         /*
274                          * Add a reference, so that page is not evicted from
275                          * the cache until ->commit_write() is called.
276                          */
277                         cl_page_get(page);
278                         lu_ref_add(&page->cp_reference, "prepare_write",
279                                    cfs_current());
280                 } else {
281                         cl_page_unassume(env, io, page);
282                         ll_cl_fini(lcc);
283                 }
284                 /* returning 0 in prepare assumes commit must be called
285                  * afterwards */
286         } else {
287                 result = PTR_ERR(lcc);
288         }
289         RETURN(result);
290 }
291
292 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
293                     unsigned to)
294 {
295         struct ll_cl_context *lcc;
296         struct lu_env    *env;
297         struct cl_io     *io;
298         struct cl_page   *page;
299         int result = 0;
300         ENTRY;
301
302         lcc  = ll_cl_get();
303         env  = lcc->lcc_env;
304         page = lcc->lcc_page;
305         io   = lcc->lcc_io;
306
307         LASSERT(cl_page_is_owned(page, io));
308         LASSERT(from <= to);
309         if (from != to) /* handle short write case. */
310                 result = cl_io_commit_write(env, io, page, from, to);
311         if (cl_page_is_owned(page, io))
312                 cl_page_unassume(env, io, page);
313
314         /*
315          * Release reference acquired by ll_prepare_write().
316          */
317         lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
318         cl_page_put(env, page);
319         ll_cl_fini(lcc);
320         RETURN(result);
321 }
322
323 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
324 {
325         __u64 opc;
326
327         opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
328         return ll_osscapa_get(inode, opc);
329 }
330
331 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
332
333 /**
334  * Get readahead pages from the filesystem readahead pool of the client for a
335  * thread.
336  *
337  * /param sbi superblock for filesystem readahead state ll_ra_info
338  * /param ria per-thread readahead state
339  * /param pages number of pages requested for readahead for the thread.
340  *
341  * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
342  * It should work well if the ra_max_pages is much greater than the single
343  * file's read-ahead window, and not too many threads contending for
344  * these readahead pages.
345  *
346  * TODO: There may be a 'global sync problem' if many threads are trying
347  * to get an ra budget that is larger than the remaining readahead pages
348  * and reach here at exactly the same time. They will compute /a ret to
349  * consume the remaining pages, but will fail at atomic_add_return() and
350  * get a zero ra window, although there is still ra space remaining. - Jay */
351
352 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
353                                      struct ra_io_arg *ria,
354                                      unsigned long pages)
355 {
356         struct ll_ra_info *ra = &sbi->ll_ra_info;
357         long ret;
358         ENTRY;
359
360         /* If read-ahead pages left are less than 1M, do not do read-ahead,
361          * otherwise it will form small read RPC(< 1M), which hurt server
362          * performance a lot. */
363         ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), pages);
364         if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
365                 GOTO(out, ret = 0);
366
367         /* If the non-strided (ria_pages == 0) readahead window
368          * (ria_start + ret) has grown across an RPC boundary, then trim
369          * readahead size by the amount beyond the RPC so it ends on an
370          * RPC boundary. If the readahead window is already ending on
371          * an RPC boundary (beyond_rpc == 0), or smaller than a full
372          * RPC (beyond_rpc < ret) the readahead size is unchanged.
373          * The (beyond_rpc != 0) check is skipped since the conditional
374          * branch is more expensive than subtracting zero from the result.
375          *
376          * Strided read is left unaligned to avoid small fragments beyond
377          * the RPC boundary from needing an extra read RPC. */
378         if (ria->ria_pages == 0) {
379                 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
380                 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
381                         ret -= beyond_rpc;
382         }
383
384         if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
385                 cfs_atomic_sub(ret, &ra->ra_cur_pages);
386                 ret = 0;
387         }
388
389 out:
390         RETURN(ret);
391 }
392
393 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
394 {
395         struct ll_ra_info *ra = &sbi->ll_ra_info;
396         cfs_atomic_sub(len, &ra->ra_cur_pages);
397 }
398
399 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
400 {
401         LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
402         lprocfs_counter_incr(sbi->ll_ra_stats, which);
403 }
404
405 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
406 {
407         struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
408         ll_ra_stats_inc_sbi(sbi, which);
409 }
410
411 #define RAS_CDEBUG(ras) \
412         CDEBUG(D_READA,                                                      \
413                "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu"    \
414                "csr %lu sf %lu sp %lu sl %lu \n",                            \
415                ras->ras_last_readpage, ras->ras_consecutive_requests,        \
416                ras->ras_consecutive_pages, ras->ras_window_start,            \
417                ras->ras_window_len, ras->ras_next_readahead,                 \
418                ras->ras_requests, ras->ras_request_index,                    \
419                ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
420                ras->ras_stride_pages, ras->ras_stride_length)
421
422 static int index_in_window(unsigned long index, unsigned long point,
423                            unsigned long before, unsigned long after)
424 {
425         unsigned long start = point - before, end = point + after;
426
427         if (start > point)
428                start = 0;
429         if (end < point)
430                end = ~0;
431
432         return start <= index && index <= end;
433 }
434
435 static struct ll_readahead_state *ll_ras_get(struct file *f)
436 {
437         struct ll_file_data       *fd;
438
439         fd = LUSTRE_FPRIVATE(f);
440         return &fd->fd_ras;
441 }
442
443 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
444 {
445         struct ll_readahead_state *ras;
446
447         ras = ll_ras_get(f);
448
449         cfs_spin_lock(&ras->ras_lock);
450         ras->ras_requests++;
451         ras->ras_request_index = 0;
452         ras->ras_consecutive_requests++;
453         rar->lrr_reader = current;
454
455         cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
456         cfs_spin_unlock(&ras->ras_lock);
457 }
458
459 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
460 {
461         struct ll_readahead_state *ras;
462
463         ras = ll_ras_get(f);
464
465         cfs_spin_lock(&ras->ras_lock);
466         cfs_list_del_init(&rar->lrr_linkage);
467         cfs_spin_unlock(&ras->ras_lock);
468 }
469
470 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
471 {
472         struct ll_ra_read *scan;
473
474         cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
475                 if (scan->lrr_reader == current)
476                         return scan;
477         }
478         return NULL;
479 }
480
481 struct ll_ra_read *ll_ra_read_get(struct file *f)
482 {
483         struct ll_readahead_state *ras;
484         struct ll_ra_read         *bead;
485
486         ras = ll_ras_get(f);
487
488         cfs_spin_lock(&ras->ras_lock);
489         bead = ll_ra_read_get_locked(ras);
490         cfs_spin_unlock(&ras->ras_lock);
491         return bead;
492 }
493
494 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
495                               struct cl_page_list *queue, struct cl_page *page,
496                               struct page *vmpage)
497 {
498         struct ccc_page *cp;
499         int              rc;
500
501         ENTRY;
502
503         rc = 0;
504         cl_page_assume(env, io, page);
505         lu_ref_add(&page->cp_reference, "ra", cfs_current());
506         cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
507         if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
508                 rc = cl_page_is_under_lock(env, io, page);
509                 if (rc == -EBUSY) {
510                         cp->cpg_defer_uptodate = 1;
511                         cp->cpg_ra_used = 0;
512                         cl_page_list_add(queue, page);
513                         rc = 1;
514                 } else {
515                         cl_page_delete(env, page);
516                         rc = -ENOLCK;
517                 }
518         } else
519                 /* skip completed pages */
520                 cl_page_unassume(env, io, page);
521         lu_ref_del(&page->cp_reference, "ra", cfs_current());
522         cl_page_put(env, page);
523         RETURN(rc);
524 }
525
526 /**
527  * Initiates read-ahead of a page with given index.
528  *
529  * \retval     +ve: page was added to \a queue.
530  *
531  * \retval -ENOLCK: there is no extent lock for this part of a file, stop
532  *                  read-ahead.
533  *
534  * \retval  -ve, 0: page wasn't added to \a queue for other reason.
535  */
536 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
537                               struct cl_page_list *queue,
538                               pgoff_t index, struct address_space *mapping)
539 {
540         struct page      *vmpage;
541         struct cl_object *clob  = ll_i2info(mapping->host)->lli_clob;
542         struct cl_page   *page;
543         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
544         unsigned int      gfp_mask;
545         int               rc    = 0;
546         const char       *msg   = NULL;
547
548         ENTRY;
549
550         gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
551 #ifdef __GFP_NOWARN
552         gfp_mask |= __GFP_NOWARN;
553 #endif
554         vmpage = grab_cache_page_nowait(mapping, index);
555         if (vmpage != NULL) {
556                 /* Check if vmpage was truncated or reclaimed */
557                 if (vmpage->mapping == mapping) {
558                         page = cl_page_find(env, clob, vmpage->index,
559                                             vmpage, CPT_CACHEABLE);
560                         if (!IS_ERR(page)) {
561                                 rc = cl_read_ahead_page(env, io, queue,
562                                                         page, vmpage);
563                                 if (rc == -ENOLCK) {
564                                         which = RA_STAT_FAILED_MATCH;
565                                         msg   = "lock match failed";
566                                 }
567                         } else {
568                                 which = RA_STAT_FAILED_GRAB_PAGE;
569                                 msg   = "cl_page_find failed";
570                         }
571                 } else {
572                         which = RA_STAT_WRONG_GRAB_PAGE;
573                         msg   = "g_c_p_n returned invalid page";
574                 }
575                 if (rc != 1)
576                         unlock_page(vmpage);
577                 page_cache_release(vmpage);
578         } else {
579                 which = RA_STAT_FAILED_GRAB_PAGE;
580                 msg   = "g_c_p_n failed";
581         }
582         if (msg != NULL) {
583                 ll_ra_stats_inc(mapping, which);
584                 CDEBUG(D_READA, "%s\n", msg);
585         }
586         RETURN(rc);
587 }
588
589 #define RIA_DEBUG(ria)                                                       \
590         CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n",       \
591         ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
592         ria->ria_pages)
593
594 #define RAS_INCREASE_STEP PTLRPC_MAX_BRW_PAGES
595
596 static inline int stride_io_mode(struct ll_readahead_state *ras)
597 {
598         return ras->ras_consecutive_stride_requests > 1;
599 }
600 /* The function calculates how much pages will be read in
601  * [off, off + length], in such stride IO area,
602  * stride_offset = st_off, stride_lengh = st_len,
603  * stride_pages = st_pgs
604  *
605  *   |------------------|*****|------------------|*****|------------|*****|....
606  * st_off
607  *   |--- st_pgs     ---|
608  *   |-----     st_len   -----|
609  *
610  *              How many pages it should read in such pattern
611  *              |-------------------------------------------------------------|
612  *              off
613  *              |<------                  length                      ------->|
614  *
615  *          =   |<----->|  +  |-------------------------------------| +   |---|
616  *             start_left                 st_pgs * i                    end_left
617  */
618 static unsigned long
619 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
620                 unsigned long off, unsigned long length)
621 {
622         __u64 start = off > st_off ? off - st_off : 0;
623         __u64 end = off + length > st_off ? off + length - st_off : 0;
624         unsigned long start_left = 0;
625         unsigned long end_left = 0;
626         unsigned long pg_count;
627
628         if (st_len == 0 || length == 0 || end == 0)
629                 return length;
630
631         start_left = do_div(start, st_len);
632         if (start_left < st_pgs)
633                 start_left = st_pgs - start_left;
634         else
635                 start_left = 0;
636
637         end_left = do_div(end, st_len);
638         if (end_left > st_pgs)
639                 end_left = st_pgs;
640
641         CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
642                start, end, start_left, end_left);
643
644         if (start == end)
645                 pg_count = end_left - (st_pgs - start_left);
646         else
647                 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
648
649         CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
650                "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
651
652         return pg_count;
653 }
654
655 static int ria_page_count(struct ra_io_arg *ria)
656 {
657         __u64 length = ria->ria_end >= ria->ria_start ?
658                        ria->ria_end - ria->ria_start + 1 : 0;
659
660         return stride_pg_count(ria->ria_stoff, ria->ria_length,
661                                ria->ria_pages, ria->ria_start,
662                                length);
663 }
664
665 /*Check whether the index is in the defined ra-window */
666 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
667 {
668         /* If ria_length == ria_pages, it means non-stride I/O mode,
669          * idx should always inside read-ahead window in this case
670          * For stride I/O mode, just check whether the idx is inside
671          * the ria_pages. */
672         return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
673                (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
674                 ria->ria_length < ria->ria_pages);
675 }
676
677 static int ll_read_ahead_pages(const struct lu_env *env,
678                                struct cl_io *io, struct cl_page_list *queue,
679                                struct ra_io_arg *ria,
680                                unsigned long *reserved_pages,
681                                struct address_space *mapping,
682                                unsigned long *ra_end)
683 {
684         int rc, count = 0, stride_ria;
685         unsigned long page_idx;
686
687         LASSERT(ria != NULL);
688         RIA_DEBUG(ria);
689
690         stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
691         for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
692                         *reserved_pages > 0; page_idx++) {
693                 if (ras_inside_ra_window(page_idx, ria)) {
694                         /* If the page is inside the read-ahead window*/
695                         rc = ll_read_ahead_page(env, io, queue,
696                                                 page_idx, mapping);
697                         if (rc == 1) {
698                                 (*reserved_pages)--;
699                                 count ++;
700                         } else if (rc == -ENOLCK)
701                                 break;
702                 } else if (stride_ria) {
703                         /* If it is not in the read-ahead window, and it is
704                          * read-ahead mode, then check whether it should skip
705                          * the stride gap */
706                         pgoff_t offset;
707                         /* FIXME: This assertion only is valid when it is for
708                          * forward read-ahead, it will be fixed when backward
709                          * read-ahead is implemented */
710                         LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
711                                 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
712                                 ria->ria_start, ria->ria_end, ria->ria_stoff,
713                                 ria->ria_length, ria->ria_pages);
714                         offset = page_idx - ria->ria_stoff;
715                         offset = offset % (ria->ria_length);
716                         if (offset > ria->ria_pages) {
717                                 page_idx += ria->ria_length - offset;
718                                 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
719                                        ria->ria_length - offset);
720                                 continue;
721                         }
722                 }
723         }
724         *ra_end = page_idx;
725         return count;
726 }
727
728 int ll_readahead(const struct lu_env *env, struct cl_io *io,
729                  struct ll_readahead_state *ras, struct address_space *mapping,
730                  struct cl_page_list *queue, int flags)
731 {
732         struct vvp_io *vio = vvp_env_io(env);
733         struct vvp_thread_info *vti = vvp_env_info(env);
734         struct cl_attr *attr = ccc_env_thread_attr(env);
735         unsigned long start = 0, end = 0, reserved;
736         unsigned long ra_end, len;
737         struct inode *inode;
738         struct ll_ra_read *bead;
739         struct ra_io_arg *ria = &vti->vti_ria;
740         struct ll_inode_info *lli;
741         struct cl_object *clob;
742         int ret = 0;
743         __u64 kms;
744         ENTRY;
745
746         inode = mapping->host;
747         lli = ll_i2info(inode);
748         clob = lli->lli_clob;
749
750         memset(ria, 0, sizeof *ria);
751
752         cl_object_attr_lock(clob);
753         ret = cl_object_attr_get(env, clob, attr);
754         cl_object_attr_unlock(clob);
755
756         if (ret != 0)
757                 RETURN(ret);
758         kms = attr->cat_kms;
759         if (kms == 0) {
760                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
761                 RETURN(0);
762         }
763
764         cfs_spin_lock(&ras->ras_lock);
765         if (vio->cui_ra_window_set)
766                 bead = &vio->cui_bead;
767         else
768                 bead = NULL;
769
770         /* Enlarge the RA window to encompass the full read */
771         if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
772             bead->lrr_start + bead->lrr_count) {
773                 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
774                                       ras->ras_window_start;
775         }
776         /* Reserve a part of the read-ahead window that we'll be issuing */
777         if (ras->ras_window_len) {
778                 start = ras->ras_next_readahead;
779                 end = ras->ras_window_start + ras->ras_window_len - 1;
780         }
781         if (end != 0) {
782                 unsigned long rpc_boundary;
783                 /*
784                  * Align RA window to an optimal boundary.
785                  *
786                  * XXX This would be better to align to cl_max_pages_per_rpc
787                  * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
788                  * be aligned to the RAID stripe size in the future and that
789                  * is more important than the RPC size.
790                  */
791                 /* Note: we only trim the RPC, instead of extending the RPC
792                  * to the boundary, so to avoid reading too much pages during
793                  * random reading. */
794                 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
795                 if (rpc_boundary > 0)
796                         rpc_boundary--;
797
798                 if (rpc_boundary  > start)
799                         end = rpc_boundary;
800
801                 /* Truncate RA window to end of file */
802                 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
803
804                 ras->ras_next_readahead = max(end, end + 1);
805                 RAS_CDEBUG(ras);
806         }
807         ria->ria_start = start;
808         ria->ria_end = end;
809         /* If stride I/O mode is detected, get stride window*/
810         if (stride_io_mode(ras)) {
811                 ria->ria_stoff = ras->ras_stride_offset;
812                 ria->ria_length = ras->ras_stride_length;
813                 ria->ria_pages = ras->ras_stride_pages;
814         }
815         cfs_spin_unlock(&ras->ras_lock);
816
817         if (end == 0) {
818                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
819                 RETURN(0);
820         }
821         len = ria_page_count(ria);
822         if (len == 0)
823                 RETURN(0);
824
825         reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len);
826         if (reserved < len)
827                 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
828
829         CDEBUG(D_READA, "reserved page %lu ra_cur %d ra_max %lu\n", reserved,
830                cfs_atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
831                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
832
833         ret = ll_read_ahead_pages(env, io, queue,
834                                   ria, &reserved, mapping, &ra_end);
835
836         LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
837         if (reserved != 0)
838                 ll_ra_count_put(ll_i2sbi(inode), reserved);
839
840         if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
841                 ll_ra_stats_inc(mapping, RA_STAT_EOF);
842
843         /* if we didn't get to the end of the region we reserved from
844          * the ras we need to go back and update the ras so that the
845          * next read-ahead tries from where we left off.  we only do so
846          * if the region we failed to issue read-ahead on is still ahead
847          * of the app and behind the next index to start read-ahead from */
848         CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
849                ra_end, end, ria->ria_end);
850
851         if (ra_end != end + 1) {
852                 cfs_spin_lock(&ras->ras_lock);
853                 if (ra_end < ras->ras_next_readahead &&
854                     index_in_window(ra_end, ras->ras_window_start, 0,
855                                     ras->ras_window_len)) {
856                         ras->ras_next_readahead = ra_end;
857                                RAS_CDEBUG(ras);
858                 }
859                 cfs_spin_unlock(&ras->ras_lock);
860         }
861
862         RETURN(ret);
863 }
864
865 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
866 {
867         ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
868 }
869
870 /* called with the ras_lock held or from places where it doesn't matter */
871 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
872 {
873         ras->ras_last_readpage = index;
874         ras->ras_consecutive_requests = 0;
875         ras->ras_consecutive_pages = 0;
876         ras->ras_window_len = 0;
877         ras_set_start(ras, index);
878         ras->ras_next_readahead = max(ras->ras_window_start, index);
879
880         RAS_CDEBUG(ras);
881 }
882
883 /* called with the ras_lock held or from places where it doesn't matter */
884 static void ras_stride_reset(struct ll_readahead_state *ras)
885 {
886         ras->ras_consecutive_stride_requests = 0;
887         ras->ras_stride_length = 0;
888         ras->ras_stride_pages = 0;
889         RAS_CDEBUG(ras);
890 }
891
892 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
893 {
894         cfs_spin_lock_init(&ras->ras_lock);
895         ras_reset(ras, 0);
896         ras->ras_requests = 0;
897         CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
898 }
899
900 /*
901  * Check whether the read request is in the stride window.
902  * If it is in the stride window, return 1, otherwise return 0.
903  */
904 static int index_in_stride_window(unsigned long index,
905                                   struct ll_readahead_state *ras,
906                                   struct inode *inode)
907 {
908         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
909
910         if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
911             ras->ras_stride_pages == ras->ras_stride_length)
912                 return 0;
913
914         /* If it is contiguous read */
915         if (stride_gap == 0)
916                 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
917
918         /*Otherwise check the stride by itself */
919         return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
920              ras->ras_consecutive_pages == ras->ras_stride_pages;
921 }
922
923 static void ras_update_stride_detector(struct ll_readahead_state *ras,
924                                        unsigned long index)
925 {
926         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
927
928         if (!stride_io_mode(ras) && (stride_gap != 0 ||
929              ras->ras_consecutive_stride_requests == 0)) {
930                 ras->ras_stride_pages = ras->ras_consecutive_pages;
931                 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
932         }
933         LASSERT(ras->ras_request_index == 0);
934         LASSERT(ras->ras_consecutive_stride_requests == 0);
935
936         if (index <= ras->ras_last_readpage) {
937                 /*Reset stride window for forward read*/
938                 ras_stride_reset(ras);
939                 return;
940         }
941
942         ras->ras_stride_pages = ras->ras_consecutive_pages;
943         ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
944
945         RAS_CDEBUG(ras);
946         return;
947 }
948
949 static unsigned long
950 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
951 {
952         return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
953                                ras->ras_stride_pages, ras->ras_stride_offset,
954                                len);
955 }
956
957 /* Stride Read-ahead window will be increased inc_len according to
958  * stride I/O pattern */
959 static void ras_stride_increase_window(struct ll_readahead_state *ras,
960                                        struct ll_ra_info *ra,
961                                        unsigned long inc_len)
962 {
963         unsigned long left, step, window_len;
964         unsigned long stride_len;
965
966         LASSERT(ras->ras_stride_length > 0);
967         LASSERTF(ras->ras_window_start + ras->ras_window_len
968                  >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
969                  " stride_offset %lu\n", ras->ras_window_start,
970                  ras->ras_window_len, ras->ras_stride_offset);
971
972         stride_len = ras->ras_window_start + ras->ras_window_len -
973                      ras->ras_stride_offset;
974
975         left = stride_len % ras->ras_stride_length;
976         window_len = ras->ras_window_len - left;
977
978         if (left < ras->ras_stride_pages)
979                 left += inc_len;
980         else
981                 left = ras->ras_stride_pages + inc_len;
982
983         LASSERT(ras->ras_stride_pages != 0);
984
985         step = left / ras->ras_stride_pages;
986         left %= ras->ras_stride_pages;
987
988         window_len += step * ras->ras_stride_length + left;
989
990         if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
991                 ras->ras_window_len = window_len;
992
993         RAS_CDEBUG(ras);
994 }
995
996 static void ras_increase_window(struct ll_readahead_state *ras,
997                                 struct ll_ra_info *ra, struct inode *inode)
998 {
999         /* The stretch of ra-window should be aligned with max rpc_size
1000          * but current clio architecture does not support retrieve such
1001          * information from lower layer. FIXME later
1002          */
1003         if (stride_io_mode(ras))
1004                 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
1005         else
1006                 ras->ras_window_len = min(ras->ras_window_len +
1007                                           RAS_INCREASE_STEP,
1008                                           ra->ra_max_pages_per_file);
1009 }
1010
1011 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1012                 struct ll_readahead_state *ras, unsigned long index,
1013                 unsigned hit)
1014 {
1015         struct ll_ra_info *ra = &sbi->ll_ra_info;
1016         int zero = 0, stride_detect = 0, ra_miss = 0;
1017         ENTRY;
1018
1019         cfs_spin_lock(&ras->ras_lock);
1020
1021         ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1022
1023         /* reset the read-ahead window in two cases.  First when the app seeks
1024          * or reads to some other part of the file.  Secondly if we get a
1025          * read-ahead miss that we think we've previously issued.  This can
1026          * be a symptom of there being so many read-ahead pages that the VM is
1027          * reclaiming it before we get to it. */
1028         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
1029                 zero = 1;
1030                 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1031         } else if (!hit && ras->ras_window_len &&
1032                    index < ras->ras_next_readahead &&
1033                    index_in_window(index, ras->ras_window_start, 0,
1034                                    ras->ras_window_len)) {
1035                 ra_miss = 1;
1036                 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1037         }
1038
1039         /* On the second access to a file smaller than the tunable
1040          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1041          * file up to ra_max_pages_per_file.  This is simply a best effort
1042          * and only occurs once per open file.  Normal RA behavior is reverted
1043          * to for subsequent IO.  The mmap case does not increment
1044          * ras_requests and thus can never trigger this behavior. */
1045         if (ras->ras_requests == 2 && !ras->ras_request_index) {
1046                 __u64 kms_pages;
1047
1048                 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
1049                             CFS_PAGE_SHIFT;
1050
1051                 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
1052                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1053
1054                 if (kms_pages &&
1055                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1056                         ras->ras_window_start = 0;
1057                         ras->ras_last_readpage = 0;
1058                         ras->ras_next_readahead = 0;
1059                         ras->ras_window_len = min(ra->ra_max_pages_per_file,
1060                                 ra->ra_max_read_ahead_whole_pages);
1061                         GOTO(out_unlock, 0);
1062                 }
1063         }
1064         if (zero) {
1065                 /* check whether it is in stride I/O mode*/
1066                 if (!index_in_stride_window(index, ras, inode)) {
1067                         if (ras->ras_consecutive_stride_requests == 0 &&
1068                             ras->ras_request_index == 0) {
1069                                 ras_update_stride_detector(ras, index);
1070                                 ras->ras_consecutive_stride_requests ++;
1071                         } else {
1072                                 ras_stride_reset(ras);
1073                         }
1074                         ras_reset(ras, index);
1075                         ras->ras_consecutive_pages++;
1076                         GOTO(out_unlock, 0);
1077                 } else {
1078                         ras->ras_consecutive_pages = 0;
1079                         ras->ras_consecutive_requests = 0;
1080                         if (++ras->ras_consecutive_stride_requests > 1)
1081                                 stride_detect = 1;
1082                         RAS_CDEBUG(ras);
1083                 }
1084         } else {
1085                 if (ra_miss) {
1086                         if (index_in_stride_window(index, ras, inode) &&
1087                             stride_io_mode(ras)) {
1088                                 /*If stride-RA hit cache miss, the stride dector
1089                                  *will not be reset to avoid the overhead of
1090                                  *redetecting read-ahead mode */
1091                                 if (index != ras->ras_last_readpage + 1)
1092                                        ras->ras_consecutive_pages = 0;
1093                                 ras_reset(ras, index);
1094                                 RAS_CDEBUG(ras);
1095                         } else {
1096                                 /* Reset both stride window and normal RA
1097                                  * window */
1098                                 ras_reset(ras, index);
1099                                 ras->ras_consecutive_pages++;
1100                                 ras_stride_reset(ras);
1101                                 GOTO(out_unlock, 0);
1102                         }
1103                 } else if (stride_io_mode(ras)) {
1104                         /* If this is contiguous read but in stride I/O mode
1105                          * currently, check whether stride step still is valid,
1106                          * if invalid, it will reset the stride ra window*/
1107                         if (!index_in_stride_window(index, ras, inode)) {
1108                                 /* Shrink stride read-ahead window to be zero */
1109                                 ras_stride_reset(ras);
1110                                 ras->ras_window_len = 0;
1111                                 ras->ras_next_readahead = index;
1112                         }
1113                 }
1114         }
1115         ras->ras_consecutive_pages++;
1116         ras->ras_last_readpage = index;
1117         ras_set_start(ras, index);
1118
1119         if (stride_io_mode(ras))
1120                 /* Since stride readahead is sentivite to the offset
1121                  * of read-ahead, so we use original offset here,
1122                  * instead of ras_window_start, which is 1M aligned*/
1123                 ras->ras_next_readahead = max(index,
1124                                               ras->ras_next_readahead);
1125         else
1126                 ras->ras_next_readahead = max(ras->ras_window_start,
1127                                               ras->ras_next_readahead);
1128         RAS_CDEBUG(ras);
1129
1130         /* Trigger RA in the mmap case where ras_consecutive_requests
1131          * is not incremented and thus can't be used to trigger RA */
1132         if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1133                 ras->ras_window_len = RAS_INCREASE_STEP;
1134                 GOTO(out_unlock, 0);
1135         }
1136
1137         /* Initially reset the stride window offset to next_readahead*/
1138         if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1139                 /**
1140                  * Once stride IO mode is detected, next_readahead should be
1141                  * reset to make sure next_readahead > stride offset
1142                  */
1143                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1144                 ras->ras_stride_offset = index;
1145                 ras->ras_window_len = RAS_INCREASE_STEP;
1146         }
1147
1148         /* The initial ras_window_len is set to the request size.  To avoid
1149          * uselessly reading and discarding pages for random IO the window is
1150          * only increased once per consecutive request received. */
1151         if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1152             !ras->ras_request_index)
1153                 ras_increase_window(ras, ra, inode);
1154         EXIT;
1155 out_unlock:
1156         RAS_CDEBUG(ras);
1157         ras->ras_request_index++;
1158         cfs_spin_unlock(&ras->ras_lock);
1159         return;
1160 }
1161
1162 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1163 {
1164         struct inode           *inode = vmpage->mapping->host;
1165         struct lu_env          *env;
1166         struct cl_io           *io;
1167         struct cl_page         *page;
1168         struct cl_object       *clob;
1169         struct cl_env_nest      nest;
1170         int redirtied = 0;
1171         int result;
1172         ENTRY;
1173
1174         LASSERT(PageLocked(vmpage));
1175         LASSERT(!PageWriteback(vmpage));
1176
1177         if (ll_i2dtexp(inode) == NULL)
1178                 RETURN(-EINVAL);
1179
1180         env = cl_env_nested_get(&nest);
1181         if (IS_ERR(env))
1182                 RETURN(PTR_ERR(env));
1183
1184         clob  = ll_i2info(inode)->lli_clob;
1185         LASSERT(clob != NULL);
1186
1187         io = ccc_env_thread_io(env);
1188         io->ci_obj = clob;
1189         result = cl_io_init(env, io, CIT_MISC, clob);
1190         if (result == 0) {
1191                 page = cl_page_find(env, clob, vmpage->index,
1192                                     vmpage, CPT_CACHEABLE);
1193                 if (!IS_ERR(page)) {
1194                         lu_ref_add(&page->cp_reference, "writepage",
1195                                    cfs_current());
1196                         cl_page_assume(env, io, page);
1197                         result = cl_page_flush(env, io, page);
1198                         if (result != 0) {
1199                                 /*
1200                                  * Re-dirty page on error so it retries write,
1201                                  * but not in case when IO has actually
1202                                  * occurred and completed with an error.
1203                                  */
1204                                 if (!PageError(vmpage)) {
1205                                         redirty_page_for_writepage(wbc, vmpage);
1206                                         result = 0;
1207                                         redirtied = 1;
1208                                 }
1209                         }
1210                         cl_page_disown(env, io, page);
1211                         lu_ref_del(&page->cp_reference,
1212                                    "writepage", cfs_current());
1213                         cl_page_put(env, page);
1214                 }
1215         }
1216         cl_io_fini(env, io);
1217
1218         if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1219                 loff_t offset = cl_offset(clob, vmpage->index);
1220
1221                 /* Flush page failed because the extent is being written out.
1222                  * Wait for the write of extent to be finished to avoid
1223                  * breaking kernel which assumes ->writepage should mark
1224                  * PageWriteback or clean the page. */
1225                 result = cl_sync_file_range(inode, offset,
1226                                             offset + CFS_PAGE_SIZE - 1,
1227                                             CL_FSYNC_LOCAL);
1228                 if (result > 0) {
1229                         /* actually we may have written more than one page.
1230                          * decreasing this page because the caller will count
1231                          * it. */
1232                         wbc->nr_to_write -= result - 1;
1233                         result = 0;
1234                 }
1235         }
1236
1237         cl_env_nested_put(&nest, env);
1238         RETURN(result);
1239 }
1240
1241 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1242 {
1243         struct inode *inode = mapping->host;
1244         loff_t start;
1245         loff_t end;
1246         enum cl_fsync_mode mode;
1247         int range_whole = 0;
1248         int result;
1249         ENTRY;
1250
1251         if (wbc->range_cyclic) {
1252                 start = mapping->writeback_index << CFS_PAGE_SHIFT;
1253                 end = OBD_OBJECT_EOF;
1254         } else {
1255                 start = wbc->range_start;
1256                 end = wbc->range_end;
1257                 if (end == LLONG_MAX) {
1258                         end = OBD_OBJECT_EOF;
1259                         range_whole = start == 0;
1260                 }
1261         }
1262
1263         mode = CL_FSYNC_NONE;
1264         if (wbc->sync_mode == WB_SYNC_ALL)
1265                 mode = CL_FSYNC_LOCAL;
1266
1267         result = cl_sync_file_range(inode, start, end, mode);
1268         if (result > 0) {
1269                 wbc->nr_to_write -= result;
1270                 result = 0;
1271          }
1272
1273         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1274                 if (end == OBD_OBJECT_EOF)
1275                         end = i_size_read(inode);
1276                 mapping->writeback_index = (end >> CFS_PAGE_SHIFT) + 1;
1277         }
1278         RETURN(result);
1279 }
1280
1281 int ll_readpage(struct file *file, struct page *vmpage)
1282 {
1283         struct ll_cl_context *lcc;
1284         int result;
1285         ENTRY;
1286
1287         lcc = ll_cl_init(file, vmpage, 0);
1288         if (!IS_ERR(lcc)) {
1289                 struct lu_env  *env  = lcc->lcc_env;
1290                 struct cl_io   *io   = lcc->lcc_io;
1291                 struct cl_page *page = lcc->lcc_page;
1292
1293                 LASSERT(page->cp_type == CPT_CACHEABLE);
1294                 if (likely(!PageUptodate(vmpage))) {
1295                         cl_page_assume(env, io, page);
1296                         result = cl_io_read_page(env, io, page);
1297                 } else {
1298                         /* Page from a non-object file. */
1299                         LASSERT(!ll_i2info(vmpage->mapping->host)->lli_smd);
1300                         unlock_page(vmpage);
1301                         result = 0;
1302                 }
1303                 ll_cl_fini(lcc);
1304         } else {
1305                 unlock_page(vmpage);
1306                 result = PTR_ERR(lcc);
1307         }
1308         RETURN(result);
1309 }
1310