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