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