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