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