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