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