Whamcloud - gitweb
a5f3f9c187d57c8b0079d667868c6a819bf53e1e
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/llite/rw.c
33  *
34  * Lustre Lite I/O page cache routines shared by different kernel revs
35  */
36
37 #include <linux/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
43 #include <linux/writeback.h>
44 #include <asm/uaccess.h>
45
46 #include <linux/fs.h>
47 #include <linux/stat.h>
48 #include <asm/uaccess.h>
49 #include <linux/mm.h>
50 #include <linux/pagemap.h>
51 /* current_is_kswapd() */
52 #include <linux/swap.h>
53 #include <linux/task_io_accounting_ops.h>
54
55 #define DEBUG_SUBSYSTEM S_LLITE
56
57 #include <obd_cksum.h>
58 #include "llite_internal.h"
59 #include <lustre_compat.h>
60
61 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
62
63 /**
64  * Get readahead pages from the filesystem readahead pool of the client for a
65  * thread.
66  *
67  * /param sbi superblock for filesystem readahead state ll_ra_info
68  * /param ria per-thread readahead state
69  * /param pages number of pages requested for readahead for the thread.
70  *
71  * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
72  * It should work well if the ra_max_pages is much greater than the single
73  * file's read-ahead window, and not too many threads contending for
74  * these readahead pages.
75  *
76  * TODO: There may be a 'global sync problem' if many threads are trying
77  * to get an ra budget that is larger than the remaining readahead pages
78  * and reach here at exactly the same time. They will compute /a ret to
79  * consume the remaining pages, but will fail at atomic_add_return() and
80  * get a zero ra window, although there is still ra space remaining. - Jay */
81
82 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
83                                      struct ra_io_arg *ria,
84                                      unsigned long pages, unsigned long min)
85 {
86         struct ll_ra_info *ra = &sbi->ll_ra_info;
87         long ret;
88         ENTRY;
89
90         /* If read-ahead pages left are less than 1M, do not do read-ahead,
91          * otherwise it will form small read RPC(< 1M), which hurt server
92          * performance a lot. */
93         ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages),
94                   pages);
95         if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
96                 GOTO(out, ret = 0);
97
98         if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
99                 atomic_sub(ret, &ra->ra_cur_pages);
100                 ret = 0;
101         }
102
103 out:
104         if (ret < min) {
105                 /* override ra limit for maximum performance */
106                 atomic_add(min - ret, &ra->ra_cur_pages);
107                 ret = min;
108         }
109         RETURN(ret);
110 }
111
112 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
113 {
114         struct ll_ra_info *ra = &sbi->ll_ra_info;
115         atomic_sub(len, &ra->ra_cur_pages);
116 }
117
118 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
119 {
120         LASSERTF(which < _NR_RA_STAT, "which: %u\n", which);
121         lprocfs_counter_incr(sbi->ll_ra_stats, which);
122 }
123
124 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
125 {
126         struct ll_sb_info *sbi = ll_i2sbi(inode);
127         ll_ra_stats_inc_sbi(sbi, which);
128 }
129
130 #define RAS_CDEBUG(ras) \
131         CDEBUG(D_READA,                                                      \
132                "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu rpc %lu "        \
133                "r %lu ri %lu csr %lu sf %lu sp %lu sl %lu\n",                \
134                ras->ras_last_readpage, ras->ras_consecutive_requests,        \
135                ras->ras_consecutive_pages, ras->ras_window_start,            \
136                ras->ras_window_len, ras->ras_next_readahead,                 \
137                ras->ras_rpc_size,                                            \
138                ras->ras_requests, ras->ras_request_index,                    \
139                ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
140                ras->ras_stride_pages, ras->ras_stride_length)
141
142 static int index_in_window(unsigned long index, unsigned long point,
143                            unsigned long before, unsigned long after)
144 {
145         unsigned long start = point - before, end = point + after;
146
147         if (start > point)
148                start = 0;
149         if (end < point)
150                end = ~0;
151
152         return start <= index && index <= end;
153 }
154
155 void ll_ras_enter(struct file *f)
156 {
157         struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
158         struct ll_readahead_state *ras = &fd->fd_ras;
159
160         spin_lock(&ras->ras_lock);
161         ras->ras_requests++;
162         ras->ras_request_index = 0;
163         ras->ras_consecutive_requests++;
164         spin_unlock(&ras->ras_lock);
165 }
166
167 /**
168  * Initiates read-ahead of a page with given index.
169  *
170  * \retval +ve: page was already uptodate so it will be skipped
171  *              from being added;
172  * \retval -ve: page wasn't added to \a queue for error;
173  * \retval   0: page was added into \a queue for read ahead.
174  */
175 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
176                               struct cl_page_list *queue, pgoff_t index)
177 {
178         struct cl_object *clob  = io->ci_obj;
179         struct inode     *inode = vvp_object_inode(clob);
180         struct page      *vmpage;
181         struct cl_page   *page;
182         struct vvp_page  *vpg;
183         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
184         int               rc    = 0;
185         const char       *msg   = NULL;
186         ENTRY;
187
188         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
189         if (vmpage == NULL) {
190                 which = RA_STAT_FAILED_GRAB_PAGE;
191                 msg   = "g_c_p_n failed";
192                 GOTO(out, rc = -EBUSY);
193         }
194
195         /* Check if vmpage was truncated or reclaimed */
196         if (vmpage->mapping != inode->i_mapping) {
197                 which = RA_STAT_WRONG_GRAB_PAGE;
198                 msg   = "g_c_p_n returned invalid page";
199                 GOTO(out, rc = -EBUSY);
200         }
201
202         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
203         if (IS_ERR(page)) {
204                 which = RA_STAT_FAILED_GRAB_PAGE;
205                 msg   = "cl_page_find failed";
206                 GOTO(out, rc = PTR_ERR(page));
207         }
208
209         lu_ref_add(&page->cp_reference, "ra", current);
210         cl_page_assume(env, io, page);
211         vpg = cl2vvp_page(cl_object_page_slice(clob, page));
212         if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
213                 vpg->vpg_defer_uptodate = 1;
214                 vpg->vpg_ra_used = 0;
215                 cl_page_list_add(queue, page);
216         } else {
217                 /* skip completed pages */
218                 cl_page_unassume(env, io, page);
219                 /* This page is already uptodate, returning a positive number
220                  * to tell the callers about this */
221                 rc = 1;
222         }
223
224         lu_ref_del(&page->cp_reference, "ra", current);
225         cl_page_put(env, page);
226
227 out:
228         if (vmpage != NULL) {
229                 if (rc != 0)
230                         unlock_page(vmpage);
231                 put_page(vmpage);
232         }
233         if (msg != NULL) {
234                 ll_ra_stats_inc(inode, which);
235                 CDEBUG(D_READA, "%s\n", msg);
236
237         }
238
239         RETURN(rc);
240 }
241
242 #define RIA_DEBUG(ria)                                                       \
243         CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n",       \
244         ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
245         ria->ria_pages)
246
247 static inline int stride_io_mode(struct ll_readahead_state *ras)
248 {
249         return ras->ras_consecutive_stride_requests > 1;
250 }
251
252 /* The function calculates how much pages will be read in
253  * [off, off + length], in such stride IO area,
254  * stride_offset = st_off, stride_lengh = st_len,
255  * stride_pages = st_pgs
256  *
257  *   |------------------|*****|------------------|*****|------------|*****|....
258  * st_off
259  *   |--- st_pgs     ---|
260  *   |-----     st_len   -----|
261  *
262  *              How many pages it should read in such pattern
263  *              |-------------------------------------------------------------|
264  *              off
265  *              |<------                  length                      ------->|
266  *
267  *          =   |<----->|  +  |-------------------------------------| +   |---|
268  *             start_left                 st_pgs * i                    end_left
269  */
270 static unsigned long
271 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
272                 unsigned long off, unsigned long length)
273 {
274         __u64 start = off > st_off ? off - st_off : 0;
275         __u64 end = off + length > st_off ? off + length - st_off : 0;
276         unsigned long start_left = 0;
277         unsigned long end_left = 0;
278         unsigned long pg_count;
279
280         if (st_len == 0 || length == 0 || end == 0)
281                 return length;
282
283         start_left = do_div(start, st_len);
284         if (start_left < st_pgs)
285                 start_left = st_pgs - start_left;
286         else
287                 start_left = 0;
288
289         end_left = do_div(end, st_len);
290         if (end_left > st_pgs)
291                 end_left = st_pgs;
292
293         CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
294                start, end, start_left, end_left);
295
296         if (start == end)
297                 pg_count = end_left - (st_pgs - start_left);
298         else
299                 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
300
301         CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
302                "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
303
304         return pg_count;
305 }
306
307 static int ria_page_count(struct ra_io_arg *ria)
308 {
309         __u64 length = ria->ria_end >= ria->ria_start ?
310                        ria->ria_end - ria->ria_start + 1 : 0;
311
312         return stride_pg_count(ria->ria_stoff, ria->ria_length,
313                                ria->ria_pages, ria->ria_start,
314                                length);
315 }
316
317 static unsigned long ras_align(struct ll_readahead_state *ras,
318                                unsigned long index,
319                                unsigned long *remainder)
320 {
321         unsigned long rem = index % ras->ras_rpc_size;
322         if (remainder != NULL)
323                 *remainder = rem;
324         return index - rem;
325 }
326
327 /*Check whether the index is in the defined ra-window */
328 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
329 {
330         /* If ria_length == ria_pages, it means non-stride I/O mode,
331          * idx should always inside read-ahead window in this case
332          * For stride I/O mode, just check whether the idx is inside
333          * the ria_pages. */
334         return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
335                (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
336                 ria->ria_length < ria->ria_pages);
337 }
338
339 static unsigned long
340 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
341                     struct cl_page_list *queue, struct ll_readahead_state *ras,
342                     struct ra_io_arg *ria, pgoff_t *ra_end)
343 {
344         struct cl_read_ahead ra = { 0 };
345         int rc = 0, count = 0;
346         bool stride_ria;
347         pgoff_t page_idx;
348
349         LASSERT(ria != NULL);
350         RIA_DEBUG(ria);
351
352         stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
353         for (page_idx = ria->ria_start;
354              page_idx <= ria->ria_end && ria->ria_reserved > 0; page_idx++) {
355                 if (ras_inside_ra_window(page_idx, ria)) {
356                         if (ra.cra_end == 0 || ra.cra_end < page_idx) {
357                                 unsigned long end;
358
359                                 cl_read_ahead_release(env, &ra);
360
361                                 rc = cl_io_read_ahead(env, io, page_idx, &ra);
362                                 if (rc < 0)
363                                         break;
364
365                                 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
366                                        page_idx, ra.cra_end, ra.cra_rpc_size);
367                                 LASSERTF(ra.cra_end >= page_idx,
368                                          "object: %p, indcies %lu / %lu\n",
369                                          io->ci_obj, ra.cra_end, page_idx);
370                                 /* update read ahead RPC size.
371                                  * NB: it's racy but doesn't matter */
372                                 if (ras->ras_rpc_size != ra.cra_rpc_size &&
373                                     ra.cra_rpc_size > 0)
374                                         ras->ras_rpc_size = ra.cra_rpc_size;
375                                 /* trim it to align with optimal RPC size */
376                                 end = ras_align(ras, ria->ria_end + 1, NULL);
377                                 if (end > 0 && !ria->ria_eof)
378                                         ria->ria_end = end - 1;
379                                 if (ria->ria_end < ria->ria_end_min)
380                                         ria->ria_end = ria->ria_end_min;
381                                 if (ria->ria_end > ra.cra_end)
382                                         ria->ria_end = ra.cra_end;
383                         }
384                         if (page_idx > ria->ria_end)
385                                 break;
386
387                         /* If the page is inside the read-ahead window */
388                         rc = ll_read_ahead_page(env, io, queue, page_idx);
389                         if (rc < 0)
390                                 break;
391
392                         *ra_end = page_idx;
393                         /* Only subtract from reserve & count the page if we
394                          * really did readahead on that page. */
395                         if (rc == 0) {
396                                 ria->ria_reserved--;
397                                 count++;
398                         }
399                 } else if (stride_ria) {
400                         /* If it is not in the read-ahead window, and it is
401                          * read-ahead mode, then check whether it should skip
402                          * the stride gap */
403                         pgoff_t offset;
404                         /* FIXME: This assertion only is valid when it is for
405                          * forward read-ahead, it will be fixed when backward
406                          * read-ahead is implemented */
407                         LASSERTF(page_idx >= ria->ria_stoff,
408                                 "Invalid page_idx %lu rs %lu re %lu ro %lu "
409                                 "rl %lu rp %lu\n", page_idx,
410                                 ria->ria_start, ria->ria_end, ria->ria_stoff,
411                                 ria->ria_length, ria->ria_pages);
412                         offset = page_idx - ria->ria_stoff;
413                         offset = offset % (ria->ria_length);
414                         if (offset > ria->ria_pages) {
415                                 page_idx += ria->ria_length - offset;
416                                 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
417                                        ria->ria_length - offset);
418                                 continue;
419                         }
420                 }
421         }
422
423         cl_read_ahead_release(env, &ra);
424
425         return count;
426 }
427
428 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
429                         struct cl_page_list *queue,
430                         struct ll_readahead_state *ras, bool hit)
431 {
432         struct vvp_io *vio = vvp_env_io(env);
433         struct ll_thread_info *lti = ll_env_info(env);
434         struct cl_attr *attr = vvp_env_thread_attr(env);
435         unsigned long len, mlen = 0;
436         pgoff_t ra_end = 0, start = 0, end = 0;
437         struct inode *inode;
438         struct ra_io_arg *ria = &lti->lti_ria;
439         struct cl_object *clob;
440         int ret = 0;
441         __u64 kms;
442         ENTRY;
443
444         clob = io->ci_obj;
445         inode = vvp_object_inode(clob);
446
447         memset(ria, 0, sizeof *ria);
448
449         cl_object_attr_lock(clob);
450         ret = cl_object_attr_get(env, clob, attr);
451         cl_object_attr_unlock(clob);
452
453         if (ret != 0)
454                 RETURN(ret);
455         kms = attr->cat_kms;
456         if (kms == 0) {
457                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
458                 RETURN(0);
459         }
460
461         spin_lock(&ras->ras_lock);
462
463         /**
464          * Note: other thread might rollback the ras_next_readahead,
465          * if it can not get the full size of prepared pages, see the
466          * end of this function. For stride read ahead, it needs to
467          * make sure the offset is no less than ras_stride_offset,
468          * so that stride read ahead can work correctly.
469          */
470         if (stride_io_mode(ras))
471                 start = max(ras->ras_next_readahead, ras->ras_stride_offset);
472         else
473                 start = ras->ras_next_readahead;
474
475         if (ras->ras_window_len > 0)
476                 end = ras->ras_window_start + ras->ras_window_len - 1;
477
478         /* Enlarge the RA window to encompass the full read */
479         if (vio->vui_ra_valid &&
480             end < vio->vui_ra_start + vio->vui_ra_count - 1)
481                 end = vio->vui_ra_start + vio->vui_ra_count - 1;
482
483         if (end != 0) {
484                 unsigned long end_index;
485
486                 /* Truncate RA window to end of file */
487                 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
488                 if (end_index <= end) {
489                         end = end_index;
490                         ria->ria_eof = true;
491                 }
492         }
493         ria->ria_start = start;
494         ria->ria_end = end;
495         /* If stride I/O mode is detected, get stride window*/
496         if (stride_io_mode(ras)) {
497                 ria->ria_stoff = ras->ras_stride_offset;
498                 ria->ria_length = ras->ras_stride_length;
499                 ria->ria_pages = ras->ras_stride_pages;
500         }
501         spin_unlock(&ras->ras_lock);
502
503         if (end == 0) {
504                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
505                 RETURN(0);
506         }
507         len = ria_page_count(ria);
508         if (len == 0) {
509                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
510                 RETURN(0);
511         }
512
513         RAS_CDEBUG(ras);
514         CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
515                PFID(lu_object_fid(&clob->co_lu)),
516                ria->ria_start, ria->ria_end,
517                vio->vui_ra_valid ? vio->vui_ra_start : 0,
518                vio->vui_ra_valid ? vio->vui_ra_count : 0,
519                hit);
520
521         /* at least to extend the readahead window to cover current read */
522         if (!hit && vio->vui_ra_valid &&
523             vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
524                 unsigned long remainder;
525
526                 /* to the end of current read window. */
527                 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
528                 /* trim to RPC boundary */
529                 ras_align(ras, ria->ria_start, &remainder);
530                 mlen = min(mlen, ras->ras_rpc_size - remainder);
531                 ria->ria_end_min = ria->ria_start + mlen;
532         }
533
534         ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
535         if (ria->ria_reserved < len)
536                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
537
538         CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
539                ria->ria_reserved, len, mlen,
540                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
541                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
542
543         ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end);
544
545         if (ria->ria_reserved != 0)
546                 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
547
548         if (ra_end == end && ra_end == (kms >> PAGE_SHIFT))
549                 ll_ra_stats_inc(inode, RA_STAT_EOF);
550
551         CDEBUG(D_READA, "ra_end = %lu end = %lu stride end = %lu pages = %d\n",
552                ra_end, end, ria->ria_end, ret);
553
554         if (ra_end != end)
555                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
556         if (ra_end > 0) {
557                 /* update the ras so that the next read-ahead tries from
558                  * where we left off. */
559                 spin_lock(&ras->ras_lock);
560                 ras->ras_next_readahead = ra_end + 1;
561                 spin_unlock(&ras->ras_lock);
562                 RAS_CDEBUG(ras);
563         }
564
565         RETURN(ret);
566 }
567
568 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
569                           unsigned long index)
570 {
571         ras->ras_window_start = ras_align(ras, index, NULL);
572 }
573
574 /* called with the ras_lock held or from places where it doesn't matter */
575 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
576                       unsigned long index)
577 {
578         ras->ras_last_readpage = index;
579         ras->ras_consecutive_requests = 0;
580         ras->ras_consecutive_pages = 0;
581         ras->ras_window_len = 0;
582         ras_set_start(inode, ras, index);
583         ras->ras_next_readahead = max(ras->ras_window_start, index + 1);
584
585         RAS_CDEBUG(ras);
586 }
587
588 /* called with the ras_lock held or from places where it doesn't matter */
589 static void ras_stride_reset(struct ll_readahead_state *ras)
590 {
591         ras->ras_consecutive_stride_requests = 0;
592         ras->ras_stride_length = 0;
593         ras->ras_stride_pages = 0;
594         RAS_CDEBUG(ras);
595 }
596
597 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
598 {
599         spin_lock_init(&ras->ras_lock);
600         ras->ras_rpc_size = PTLRPC_MAX_BRW_PAGES;
601         ras_reset(inode, ras, 0);
602         ras->ras_requests = 0;
603 }
604
605 /*
606  * Check whether the read request is in the stride window.
607  * If it is in the stride window, return 1, otherwise return 0.
608  */
609 static int index_in_stride_window(struct ll_readahead_state *ras,
610                                   unsigned long index)
611 {
612         unsigned long stride_gap;
613
614         if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
615             ras->ras_stride_pages == ras->ras_stride_length)
616                 return 0;
617
618         stride_gap = index - ras->ras_last_readpage - 1;
619
620         /* If it is contiguous read */
621         if (stride_gap == 0)
622                 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
623
624         /* Otherwise check the stride by itself */
625         return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
626                 ras->ras_consecutive_pages == ras->ras_stride_pages;
627 }
628
629 static void ras_update_stride_detector(struct ll_readahead_state *ras,
630                                        unsigned long index)
631 {
632         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
633
634         if (!stride_io_mode(ras) && (stride_gap != 0 ||
635              ras->ras_consecutive_stride_requests == 0)) {
636                 ras->ras_stride_pages = ras->ras_consecutive_pages;
637                 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
638         }
639         LASSERT(ras->ras_request_index == 0);
640         LASSERT(ras->ras_consecutive_stride_requests == 0);
641
642         if (index <= ras->ras_last_readpage) {
643                 /*Reset stride window for forward read*/
644                 ras_stride_reset(ras);
645                 return;
646         }
647
648         ras->ras_stride_pages = ras->ras_consecutive_pages;
649         ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
650
651         RAS_CDEBUG(ras);
652         return;
653 }
654
655 static unsigned long
656 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
657 {
658         return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
659                                ras->ras_stride_pages, ras->ras_stride_offset,
660                                len);
661 }
662
663 /* Stride Read-ahead window will be increased inc_len according to
664  * stride I/O pattern */
665 static void ras_stride_increase_window(struct ll_readahead_state *ras,
666                                        struct ll_ra_info *ra,
667                                        unsigned long inc_len)
668 {
669         unsigned long left, step, window_len;
670         unsigned long stride_len;
671
672         LASSERT(ras->ras_stride_length > 0);
673         LASSERTF(ras->ras_window_start + ras->ras_window_len
674                  >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
675                  " stride_offset %lu\n", ras->ras_window_start,
676                  ras->ras_window_len, ras->ras_stride_offset);
677
678         stride_len = ras->ras_window_start + ras->ras_window_len -
679                      ras->ras_stride_offset;
680
681         left = stride_len % ras->ras_stride_length;
682         window_len = ras->ras_window_len - left;
683
684         if (left < ras->ras_stride_pages)
685                 left += inc_len;
686         else
687                 left = ras->ras_stride_pages + inc_len;
688
689         LASSERT(ras->ras_stride_pages != 0);
690
691         step = left / ras->ras_stride_pages;
692         left %= ras->ras_stride_pages;
693
694         window_len += step * ras->ras_stride_length + left;
695
696         if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
697                 ras->ras_window_len = window_len;
698
699         RAS_CDEBUG(ras);
700 }
701
702 static void ras_increase_window(struct inode *inode,
703                                 struct ll_readahead_state *ras,
704                                 struct ll_ra_info *ra)
705 {
706         /* The stretch of ra-window should be aligned with max rpc_size
707          * but current clio architecture does not support retrieve such
708          * information from lower layer. FIXME later
709          */
710         if (stride_io_mode(ras)) {
711                 ras_stride_increase_window(ras, ra, ras->ras_rpc_size);
712         } else {
713                 unsigned long wlen;
714
715                 wlen = min(ras->ras_window_len + ras->ras_rpc_size,
716                            ra->ra_max_pages_per_file);
717                 if (wlen < ras->ras_rpc_size)
718                         ras->ras_window_len = wlen;
719                 else
720                         ras->ras_window_len = ras_align(ras, wlen, NULL);
721         }
722 }
723
724 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
725                        struct ll_readahead_state *ras, unsigned long index,
726                        enum ras_update_flags flags)
727 {
728         struct ll_ra_info *ra = &sbi->ll_ra_info;
729         bool hit = flags & LL_RAS_HIT;
730         int zero = 0, stride_detect = 0, ra_miss = 0;
731         ENTRY;
732
733         spin_lock(&ras->ras_lock);
734
735         if (!hit)
736                 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
737                        PFID(ll_inode2fid(inode)), index);
738         ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
739
740         /* reset the read-ahead window in two cases.  First when the app seeks
741          * or reads to some other part of the file.  Secondly if we get a
742          * read-ahead miss that we think we've previously issued.  This can
743          * be a symptom of there being so many read-ahead pages that the VM is
744          * reclaiming it before we get to it. */
745         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
746                 zero = 1;
747                 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
748         } else if (!hit && ras->ras_window_len &&
749                    index < ras->ras_next_readahead &&
750                    index_in_window(index, ras->ras_window_start, 0,
751                                    ras->ras_window_len)) {
752                 ra_miss = 1;
753                 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
754         }
755
756         /* On the second access to a file smaller than the tunable
757          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
758          * file up to ra_max_pages_per_file.  This is simply a best effort
759          * and only occurs once per open file.  Normal RA behavior is reverted
760          * to for subsequent IO.  The mmap case does not increment
761          * ras_requests and thus can never trigger this behavior. */
762         if (ras->ras_requests >= 2 && !ras->ras_request_index) {
763                 __u64 kms_pages;
764
765                 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
766                             PAGE_SHIFT;
767
768                 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
769                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
770
771                 if (kms_pages &&
772                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
773                         ras->ras_window_start = 0;
774                         ras->ras_next_readahead = index + 1;
775                         ras->ras_window_len = min(ra->ra_max_pages_per_file,
776                                 ra->ra_max_read_ahead_whole_pages);
777                         GOTO(out_unlock, 0);
778                 }
779         }
780         if (zero) {
781                 /* check whether it is in stride I/O mode*/
782                 if (!index_in_stride_window(ras, index)) {
783                         if (ras->ras_consecutive_stride_requests == 0 &&
784                             ras->ras_request_index == 0) {
785                                 ras_update_stride_detector(ras, index);
786                                 ras->ras_consecutive_stride_requests++;
787                         } else {
788                                 ras_stride_reset(ras);
789                         }
790                         ras_reset(inode, ras, index);
791                         ras->ras_consecutive_pages++;
792                         GOTO(out_unlock, 0);
793                 } else {
794                         ras->ras_consecutive_pages = 0;
795                         ras->ras_consecutive_requests = 0;
796                         if (++ras->ras_consecutive_stride_requests > 1)
797                                 stride_detect = 1;
798                         RAS_CDEBUG(ras);
799                 }
800         } else {
801                 if (ra_miss) {
802                         if (index_in_stride_window(ras, index) &&
803                             stride_io_mode(ras)) {
804                                 if (index != ras->ras_last_readpage + 1)
805                                         ras->ras_consecutive_pages = 0;
806                                 ras_reset(inode, ras, index);
807
808                                 /* If stride-RA hit cache miss, the stride
809                                  * detector will not be reset to avoid the
810                                  * overhead of redetecting read-ahead mode,
811                                  * but on the condition that the stride window
812                                  * is still intersect with normal sequential
813                                  * read-ahead window. */
814                                 if (ras->ras_window_start <
815                                     ras->ras_stride_offset)
816                                         ras_stride_reset(ras);
817                                 RAS_CDEBUG(ras);
818                         } else {
819                                 /* Reset both stride window and normal RA
820                                  * window */
821                                 ras_reset(inode, ras, index);
822                                 ras->ras_consecutive_pages++;
823                                 ras_stride_reset(ras);
824                                 GOTO(out_unlock, 0);
825                         }
826                 } else if (stride_io_mode(ras)) {
827                         /* If this is contiguous read but in stride I/O mode
828                          * currently, check whether stride step still is valid,
829                          * if invalid, it will reset the stride ra window*/
830                         if (!index_in_stride_window(ras, index)) {
831                                 /* Shrink stride read-ahead window to be zero */
832                                 ras_stride_reset(ras);
833                                 ras->ras_window_len = 0;
834                                 ras->ras_next_readahead = index;
835                         }
836                 }
837         }
838         ras->ras_consecutive_pages++;
839         ras->ras_last_readpage = index;
840         ras_set_start(inode, ras, index);
841
842         if (stride_io_mode(ras)) {
843                 /* Since stride readahead is sentivite to the offset
844                  * of read-ahead, so we use original offset here,
845                  * instead of ras_window_start, which is RPC aligned */
846                 ras->ras_next_readahead = max(index + 1,
847                                               ras->ras_next_readahead);
848                 ras->ras_window_start = max(ras->ras_stride_offset,
849                                             ras->ras_window_start);
850         } else {
851                 if (ras->ras_next_readahead < ras->ras_window_start)
852                         ras->ras_next_readahead = ras->ras_window_start;
853                 if (!hit)
854                         ras->ras_next_readahead = index + 1;
855         }
856         RAS_CDEBUG(ras);
857
858         /* Trigger RA in the mmap case where ras_consecutive_requests
859          * is not incremented and thus can't be used to trigger RA */
860         if (ras->ras_consecutive_pages >= 4 && flags & LL_RAS_MMAP) {
861                 ras_increase_window(inode, ras, ra);
862                 /* reset consecutive pages so that the readahead window can
863                  * grow gradually. */
864                 ras->ras_consecutive_pages = 0;
865                 GOTO(out_unlock, 0);
866         }
867
868         /* Initially reset the stride window offset to next_readahead*/
869         if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
870                 /**
871                  * Once stride IO mode is detected, next_readahead should be
872                  * reset to make sure next_readahead > stride offset
873                  */
874                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
875                 ras->ras_stride_offset = index;
876                 ras->ras_window_start = max(index, ras->ras_window_start);
877         }
878
879         /* The initial ras_window_len is set to the request size.  To avoid
880          * uselessly reading and discarding pages for random IO the window is
881          * only increased once per consecutive request received. */
882         if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
883             !ras->ras_request_index)
884                 ras_increase_window(inode, ras, ra);
885         EXIT;
886 out_unlock:
887         RAS_CDEBUG(ras);
888         ras->ras_request_index++;
889         spin_unlock(&ras->ras_lock);
890         return;
891 }
892
893 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
894 {
895         struct inode           *inode = vmpage->mapping->host;
896         struct ll_inode_info   *lli   = ll_i2info(inode);
897         struct lu_env          *env;
898         struct cl_io           *io;
899         struct cl_page         *page;
900         struct cl_object       *clob;
901         bool redirtied = false;
902         bool unlocked = false;
903         int result;
904         __u16 refcheck;
905         ENTRY;
906
907         LASSERT(PageLocked(vmpage));
908         LASSERT(!PageWriteback(vmpage));
909
910         LASSERT(ll_i2dtexp(inode) != NULL);
911
912         env = cl_env_get(&refcheck);
913         if (IS_ERR(env))
914                 GOTO(out, result = PTR_ERR(env));
915
916         clob  = ll_i2info(inode)->lli_clob;
917         LASSERT(clob != NULL);
918
919         io = vvp_env_thread_io(env);
920         io->ci_obj = clob;
921         io->ci_ignore_layout = 1;
922         result = cl_io_init(env, io, CIT_MISC, clob);
923         if (result == 0) {
924                 page = cl_page_find(env, clob, vmpage->index,
925                                     vmpage, CPT_CACHEABLE);
926                 if (!IS_ERR(page)) {
927                         lu_ref_add(&page->cp_reference, "writepage",
928                                    current);
929                         cl_page_assume(env, io, page);
930                         result = cl_page_flush(env, io, page);
931                         if (result != 0) {
932                                 /*
933                                  * Re-dirty page on error so it retries write,
934                                  * but not in case when IO has actually
935                                  * occurred and completed with an error.
936                                  */
937                                 if (!PageError(vmpage)) {
938                                         redirty_page_for_writepage(wbc, vmpage);
939                                         result = 0;
940                                         redirtied = true;
941                                 }
942                         }
943                         cl_page_disown(env, io, page);
944                         unlocked = true;
945                         lu_ref_del(&page->cp_reference,
946                                    "writepage", current);
947                         cl_page_put(env, page);
948                 } else {
949                         result = PTR_ERR(page);
950                 }
951         }
952         cl_io_fini(env, io);
953
954         if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
955                 loff_t offset = cl_offset(clob, vmpage->index);
956
957                 /* Flush page failed because the extent is being written out.
958                  * Wait for the write of extent to be finished to avoid
959                  * breaking kernel which assumes ->writepage should mark
960                  * PageWriteback or clean the page. */
961                 result = cl_sync_file_range(inode, offset,
962                                             offset + PAGE_SIZE - 1,
963                                             CL_FSYNC_LOCAL, 1);
964                 if (result > 0) {
965                         /* actually we may have written more than one page.
966                          * decreasing this page because the caller will count
967                          * it. */
968                         wbc->nr_to_write -= result - 1;
969                         result = 0;
970                 }
971         }
972
973         cl_env_put(env, &refcheck);
974         GOTO(out, result);
975
976 out:
977         if (result < 0) {
978                 if (!lli->lli_async_rc)
979                         lli->lli_async_rc = result;
980                 SetPageError(vmpage);
981                 if (!unlocked)
982                         unlock_page(vmpage);
983         }
984         return result;
985 }
986
987 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
988 {
989         struct inode *inode = mapping->host;
990         loff_t start;
991         loff_t end;
992         enum cl_fsync_mode mode;
993         int range_whole = 0;
994         int result;
995         ENTRY;
996
997         if (wbc->range_cyclic) {
998                 start = mapping->writeback_index << PAGE_SHIFT;
999                 end = OBD_OBJECT_EOF;
1000         } else {
1001                 start = wbc->range_start;
1002                 end = wbc->range_end;
1003                 if (end == LLONG_MAX) {
1004                         end = OBD_OBJECT_EOF;
1005                         range_whole = start == 0;
1006                 }
1007         }
1008
1009         mode = CL_FSYNC_NONE;
1010         if (wbc->sync_mode == WB_SYNC_ALL)
1011                 mode = CL_FSYNC_LOCAL;
1012
1013         if (ll_i2info(inode)->lli_clob == NULL)
1014                 RETURN(0);
1015
1016         /* for directio, it would call writepages() to evict cached pages
1017          * inside the IO context of write, which will cause deadlock at
1018          * layout_conf since it waits for active IOs to complete. */
1019         result = cl_sync_file_range(inode, start, end, mode, 1);
1020         if (result > 0) {
1021                 wbc->nr_to_write -= result;
1022                 result = 0;
1023          }
1024
1025         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1026                 if (end == OBD_OBJECT_EOF)
1027                         mapping->writeback_index = 0;
1028                 else
1029                         mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1030         }
1031         RETURN(result);
1032 }
1033
1034 struct ll_cl_context *ll_cl_find(struct file *file)
1035 {
1036         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1037         struct ll_cl_context *lcc;
1038         struct ll_cl_context *found = NULL;
1039
1040         read_lock(&fd->fd_lock);
1041         list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1042                 if (lcc->lcc_cookie == current) {
1043                         found = lcc;
1044                         break;
1045                 }
1046         }
1047         read_unlock(&fd->fd_lock);
1048
1049         return found;
1050 }
1051
1052 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
1053                enum lcc_type type)
1054 {
1055         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1056         struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1057
1058         memset(lcc, 0, sizeof(*lcc));
1059         INIT_LIST_HEAD(&lcc->lcc_list);
1060         lcc->lcc_cookie = current;
1061         lcc->lcc_env = env;
1062         lcc->lcc_io = io;
1063         lcc->lcc_type = type;
1064
1065         write_lock(&fd->fd_lock);
1066         list_add(&lcc->lcc_list, &fd->fd_lccs);
1067         write_unlock(&fd->fd_lock);
1068 }
1069
1070 void ll_cl_remove(struct file *file, const struct lu_env *env)
1071 {
1072         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1073         struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1074
1075         write_lock(&fd->fd_lock);
1076         list_del_init(&lcc->lcc_list);
1077         write_unlock(&fd->fd_lock);
1078 }
1079
1080 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1081                            struct cl_page *page, struct file *file)
1082 {
1083         struct inode              *inode  = vvp_object_inode(page->cp_obj);
1084         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
1085         struct ll_file_data       *fd     = LUSTRE_FPRIVATE(file);
1086         struct ll_readahead_state *ras    = &fd->fd_ras;
1087         struct cl_2queue          *queue  = &io->ci_queue;
1088         struct cl_sync_io         *anchor = NULL;
1089         struct vvp_page           *vpg;
1090         int                        rc = 0;
1091         bool                       uptodate;
1092         ENTRY;
1093
1094         vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1095         uptodate = vpg->vpg_defer_uptodate;
1096
1097         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1098             sbi->ll_ra_info.ra_max_pages > 0 &&
1099             !vpg->vpg_ra_updated) {
1100                 struct vvp_io *vio = vvp_env_io(env);
1101                 enum ras_update_flags flags = 0;
1102
1103                 if (uptodate)
1104                         flags |= LL_RAS_HIT;
1105                 if (!vio->vui_ra_valid)
1106                         flags |= LL_RAS_MMAP;
1107                 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1108         }
1109
1110         cl_2queue_init(queue);
1111         if (uptodate) {
1112                 vpg->vpg_ra_used = 1;
1113                 cl_page_export(env, page, 1);
1114                 cl_page_disown(env, io, page);
1115         } else {
1116                 anchor = &vvp_env_info(env)->vti_anchor;
1117                 cl_sync_io_init(anchor, 1, &cl_sync_io_end);
1118                 page->cp_sync_io = anchor;
1119
1120                 cl_2queue_add(queue, page);
1121         }
1122
1123         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1124             sbi->ll_ra_info.ra_max_pages > 0) {
1125                 int rc2;
1126
1127                 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1128                                    uptodate);
1129                 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1130                        PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1131         }
1132
1133         if (queue->c2_qin.pl_nr > 0) {
1134                 int count = queue->c2_qin.pl_nr;
1135                 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1136                 if (rc == 0)
1137                         task_io_account_read(PAGE_SIZE * count);
1138         }
1139
1140
1141         if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1142                 rc = cl_sync_io_wait(env, anchor, 0);
1143
1144                 cl_page_assume(env, io, page);
1145                 cl_page_list_del(env, &queue->c2_qout, page);
1146
1147                 if (!PageUptodate(cl_page_vmpage(page))) {
1148                         /* Failed to read a mirror, discard this page so that
1149                          * new page can be created with new mirror.
1150                          *
1151                          * TODO: this is not needed after page reinit
1152                          * route is implemented */
1153                         cl_page_discard(env, io, page);
1154                 }
1155                 cl_page_disown(env, io, page);
1156         }
1157
1158         /* TODO: discard all pages until page reinit route is implemented */
1159         cl_page_list_discard(env, io, &queue->c2_qin);
1160
1161         /* Unlock unsent read pages in case of error. */
1162         cl_page_list_disown(env, io, &queue->c2_qin);
1163
1164         cl_2queue_fini(env, queue);
1165
1166         RETURN(rc);
1167 }
1168
1169 int ll_readpage(struct file *file, struct page *vmpage)
1170 {
1171         struct inode *inode = file_inode(file);
1172         struct cl_object *clob = ll_i2info(inode)->lli_clob;
1173         struct ll_cl_context *lcc;
1174         const struct lu_env  *env = NULL;
1175         struct cl_io   *io = NULL;
1176         struct cl_page *page;
1177         int result;
1178         ENTRY;
1179
1180         lcc = ll_cl_find(file);
1181         if (lcc != NULL) {
1182                 env = lcc->lcc_env;
1183                 io  = lcc->lcc_io;
1184         }
1185
1186         if (io == NULL) { /* fast read */
1187                 struct inode *inode = file_inode(file);
1188                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1189                 struct ll_readahead_state *ras = &fd->fd_ras;
1190                 struct lu_env  *local_env = NULL;
1191                 unsigned long fast_read_pages =
1192                         max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_size);
1193                 struct vvp_page *vpg;
1194
1195                 result = -ENODATA;
1196
1197                 /* TODO: need to verify the layout version to make sure
1198                  * the page is not invalid due to layout change. */
1199                 page = cl_vmpage_page(vmpage, clob);
1200                 if (page == NULL) {
1201                         unlock_page(vmpage);
1202                         RETURN(result);
1203                 }
1204
1205                 if (!env) {
1206                         local_env = cl_env_percpu_get();
1207                         env = local_env;
1208                 }
1209
1210                 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1211                 if (vpg->vpg_defer_uptodate) {
1212                         enum ras_update_flags flags = LL_RAS_HIT;
1213
1214                         if (lcc && lcc->lcc_type == LCC_MMAP)
1215                                 flags |= LL_RAS_MMAP;
1216
1217                         /* For fast read, it updates read ahead state only
1218                          * if the page is hit in cache because non cache page
1219                          * case will be handled by slow read later. */
1220                         ras_update(ll_i2sbi(inode), inode, ras, vvp_index(vpg),
1221                                    flags);
1222                         /* avoid duplicate ras_update() call */
1223                         vpg->vpg_ra_updated = 1;
1224
1225                         /* Check if we can issue a readahead RPC, if that is
1226                          * the case, we can't do fast IO because we will need
1227                          * a cl_io to issue the RPC. */
1228                         if (ras->ras_window_start + ras->ras_window_len <
1229                             ras->ras_next_readahead + fast_read_pages) {
1230                                 /* export the page and skip io stack */
1231                                 vpg->vpg_ra_used = 1;
1232                                 cl_page_export(env, page, 1);
1233                                 result = 0;
1234                         }
1235                 }
1236
1237                 /* release page refcount before unlocking the page to ensure
1238                  * the object won't be destroyed in the calling path of
1239                  * cl_page_put(). Please see comment in ll_releasepage(). */
1240                 cl_page_put(env, page);
1241                 unlock_page(vmpage);
1242                 if (local_env)
1243                         cl_env_percpu_put(local_env);
1244
1245                 RETURN(result);
1246         }
1247
1248         LASSERT(io->ci_state == CIS_IO_GOING);
1249         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1250         if (!IS_ERR(page)) {
1251                 LASSERT(page->cp_type == CPT_CACHEABLE);
1252                 if (likely(!PageUptodate(vmpage))) {
1253                         cl_page_assume(env, io, page);
1254
1255                         result = ll_io_read_page(env, io, page, file);
1256                 } else {
1257                         /* Page from a non-object file. */
1258                         unlock_page(vmpage);
1259                         result = 0;
1260                 }
1261                 cl_page_put(env, page);
1262         } else {
1263                 unlock_page(vmpage);
1264                 result = PTR_ERR(page);
1265         }
1266         RETURN(result);
1267 }