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