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