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