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