Whamcloud - gitweb
083caaada6e302c858c0b453d8ff8bc069adae27
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/llite/rw.c
33  *
34  * Lustre Lite I/O page cache routines shared by different kernel revs
35  */
36
37 #include <linux/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
43 #include <linux/writeback.h>
44 #include <asm/uaccess.h>
45
46 #include <linux/fs.h>
47 #include <linux/file.h>
48 #include <linux/stat.h>
49 #include <asm/uaccess.h>
50 #include <linux/mm.h>
51 #include <linux/pagemap.h>
52 /* current_is_kswapd() */
53 #include <linux/swap.h>
54 #include <linux/task_io_accounting_ops.h>
55
56 #define DEBUG_SUBSYSTEM S_LLITE
57
58 #include <obd_cksum.h>
59 #include "llite_internal.h"
60 #include <lustre_compat.h>
61
62 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
63
64 /**
65  * Get readahead pages from the filesystem readahead pool of the client for a
66  * thread.
67  *
68  * /param sbi superblock for filesystem readahead state ll_ra_info
69  * /param ria per-thread readahead state
70  * /param pages number of pages requested for readahead for the thread.
71  *
72  * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
73  * It should work well if the ra_max_pages is much greater than the single
74  * file's read-ahead window, and not too many threads contending for
75  * these readahead pages.
76  *
77  * TODO: There may be a 'global sync problem' if many threads are trying
78  * to get an ra budget that is larger than the remaining readahead pages
79  * and reach here at exactly the same time. They will compute /a ret to
80  * consume the remaining pages, but will fail at atomic_add_return() and
81  * get a zero ra window, although there is still ra space remaining. - Jay */
82
83 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
84                                      struct ra_io_arg *ria,
85                                      unsigned long pages, unsigned long min)
86 {
87         struct ll_ra_info *ra = &sbi->ll_ra_info;
88         long ret;
89         ENTRY;
90
91         /* If read-ahead pages left are less than 1M, do not do read-ahead,
92          * otherwise it will form small read RPC(< 1M), which hurt server
93          * performance a lot. */
94         ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages),
95                   pages);
96         if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
97                 GOTO(out, ret = 0);
98
99         if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
100                 atomic_sub(ret, &ra->ra_cur_pages);
101                 ret = 0;
102         }
103
104 out:
105         if (ret < min) {
106                 /* override ra limit for maximum performance */
107                 atomic_add(min - ret, &ra->ra_cur_pages);
108                 ret = min;
109         }
110         RETURN(ret);
111 }
112
113 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
114 {
115         struct ll_ra_info *ra = &sbi->ll_ra_info;
116         atomic_sub(len, &ra->ra_cur_pages);
117 }
118
119 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
120 {
121         LASSERTF(which < _NR_RA_STAT, "which: %u\n", which);
122         lprocfs_counter_incr(sbi->ll_ra_stats, which);
123 }
124
125 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
126 {
127         struct ll_sb_info *sbi = ll_i2sbi(inode);
128         ll_ra_stats_inc_sbi(sbi, which);
129 }
130
131 #define RAS_CDEBUG(ras) \
132         CDEBUG(D_READA,                                                      \
133                "lre %lu cr %lu cb %lu ws %lu wl %lu nra %lu rpc %lu "        \
134                "r %lu ri %lu csr %lu sf %lu sb %lu sl %lu lr %lu\n", \
135                ras->ras_last_read_end, ras->ras_consecutive_requests,        \
136                ras->ras_consecutive_bytes, ras->ras_window_start,            \
137                ras->ras_window_len, ras->ras_next_readahead,                 \
138                ras->ras_rpc_size,                                            \
139                ras->ras_requests, ras->ras_request_index,                    \
140                ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
141                ras->ras_stride_bytes, ras->ras_stride_length,                \
142                ras->ras_async_last_readpage)
143
144 static int pos_in_window(unsigned long pos, unsigned long point,
145                          unsigned long before, unsigned long after)
146 {
147         unsigned long start = point - before, end = point + after;
148
149         if (start > point)
150                start = 0;
151         if (end < point)
152                end = ~0;
153
154         return start <= pos && pos <= end;
155 }
156
157 void ll_ras_enter(struct file *f)
158 {
159         struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
160         struct ll_readahead_state *ras = &fd->fd_ras;
161
162         spin_lock(&ras->ras_lock);
163         ras->ras_requests++;
164         ras->ras_request_index = 0;
165         ras->ras_consecutive_requests++;
166         spin_unlock(&ras->ras_lock);
167 }
168
169 /**
170  * Initiates read-ahead of a page with given index.
171  *
172  * \retval +ve: page was already uptodate so it will be skipped
173  *              from being added;
174  * \retval -ve: page wasn't added to \a queue for error;
175  * \retval   0: page was added into \a queue for read ahead.
176  */
177 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
178                               struct cl_page_list *queue, pgoff_t index)
179 {
180         struct cl_object *clob  = io->ci_obj;
181         struct inode     *inode = vvp_object_inode(clob);
182         struct page      *vmpage;
183         struct cl_page   *page;
184         struct vvp_page  *vpg;
185         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
186         int               rc    = 0;
187         const char       *msg   = NULL;
188         ENTRY;
189
190         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
191         if (vmpage == NULL) {
192                 which = RA_STAT_FAILED_GRAB_PAGE;
193                 msg   = "g_c_p_n failed";
194                 GOTO(out, rc = -EBUSY);
195         }
196
197         /* Check if vmpage was truncated or reclaimed */
198         if (vmpage->mapping != inode->i_mapping) {
199                 which = RA_STAT_WRONG_GRAB_PAGE;
200                 msg   = "g_c_p_n returned invalid page";
201                 GOTO(out, rc = -EBUSY);
202         }
203
204         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
205         if (IS_ERR(page)) {
206                 which = RA_STAT_FAILED_GRAB_PAGE;
207                 msg   = "cl_page_find failed";
208                 GOTO(out, rc = PTR_ERR(page));
209         }
210
211         lu_ref_add(&page->cp_reference, "ra", current);
212         cl_page_assume(env, io, page);
213         vpg = cl2vvp_page(cl_object_page_slice(clob, page));
214         if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
215                 vpg->vpg_defer_uptodate = 1;
216                 vpg->vpg_ra_used = 0;
217                 cl_page_list_add(queue, page);
218         } else {
219                 /* skip completed pages */
220                 cl_page_unassume(env, io, page);
221                 /* This page is already uptodate, returning a positive number
222                  * to tell the callers about this */
223                 rc = 1;
224         }
225
226         lu_ref_del(&page->cp_reference, "ra", current);
227         cl_page_put(env, page);
228
229 out:
230         if (vmpage != NULL) {
231                 if (rc != 0)
232                         unlock_page(vmpage);
233                 put_page(vmpage);
234         }
235         if (msg != NULL) {
236                 ll_ra_stats_inc(inode, which);
237                 CDEBUG(D_READA, "%s\n", msg);
238
239         }
240
241         RETURN(rc);
242 }
243
244 #define RIA_DEBUG(ria)                                          \
245         CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rb %lu\n", \
246                ria->ria_start, ria->ria_end, ria->ria_stoff,    \
247                ria->ria_length, ria->ria_bytes)
248
249 static inline int stride_io_mode(struct ll_readahead_state *ras)
250 {
251         return ras->ras_consecutive_stride_requests > 1;
252 }
253
254 /* The function calculates how much pages will be read in
255  * [off, off + length], in such stride IO area,
256  * stride_offset = st_off, stride_lengh = st_len,
257  * stride_bytes = st_bytes
258  *
259  *   |------------------|*****|------------------|*****|------------|*****|....
260  * st_off
261  *   |--- st_bytes     ---|
262  *   |-----     st_len   -----|
263  *
264  *              How many bytes it should read in such pattern
265  *              |-------------------------------------------------------------|
266  *              off
267  *              |<------                  length                      ------->|
268  *
269  *          =   |<----->|  +  |-------------------------------------| +   |---|
270  *             start_left                 st_bytes * i                 end_left
271  */
272 static unsigned long
273 stride_byte_count(unsigned long st_off, unsigned long st_len,
274                   unsigned long st_bytes, unsigned long off,
275                   unsigned long length)
276 {
277         __u64 start = off > st_off ? off - st_off : 0;
278         __u64 end = off + length > st_off ? off + length - st_off : 0;
279         unsigned long start_left = 0;
280         unsigned long end_left = 0;
281         unsigned long bytes_count;
282
283         if (st_len == 0 || length == 0 || end == 0)
284                 return length;
285
286         start_left = do_div(start, st_len);
287         if (start_left < st_bytes)
288                 start_left = st_bytes - start_left;
289         else
290                 start_left = 0;
291
292         end_left = do_div(end, st_len);
293         if (end_left > st_bytes)
294                 end_left = st_bytes;
295
296         CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
297                start, end, start_left, end_left);
298
299         if (start == end)
300                 bytes_count = end_left - (st_bytes - start_left);
301         else
302                 bytes_count = start_left +
303                         st_bytes * (end - start - 1) + end_left;
304
305         CDEBUG(D_READA,
306                "st_off %lu, st_len %lu st_bytes %lu off %lu length %lu bytescount %lu\n",
307                st_off, st_len, st_bytes, off, length, bytes_count);
308
309         return bytes_count;
310 }
311
312 static int ria_page_count(struct ra_io_arg *ria)
313 {
314         __u64 length = ria->ria_end >= ria->ria_start ?
315                        ria->ria_end - ria->ria_start + 1 : 0;
316         unsigned int bytes_count;
317
318         bytes_count = stride_byte_count(ria->ria_stoff, ria->ria_length,
319                                          ria->ria_bytes, ria->ria_start,
320                                          length << PAGE_SHIFT);
321         return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
322
323 }
324
325 static unsigned long ras_align(struct ll_readahead_state *ras,
326                                pgoff_t index, unsigned long *remainder)
327 {
328         unsigned long rem = index % ras->ras_rpc_size;
329         if (remainder != NULL)
330                 *remainder = rem;
331         return index - rem;
332 }
333
334 /*Check whether the index is in the defined ra-window */
335 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
336 {
337         /* If ria_length == ria_pages, it means non-stride I/O mode,
338          * idx should always inside read-ahead window in this case
339          * For stride I/O mode, just check whether the idx is inside
340          * the ria_pages. */
341         return ria->ria_length == 0 || ria->ria_length == ria->ria_bytes ||
342                (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
343                 ria->ria_length < ria->ria_bytes);
344 }
345
346 static unsigned long
347 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
348                     struct cl_page_list *queue, struct ll_readahead_state *ras,
349                     struct ra_io_arg *ria, pgoff_t *ra_end)
350 {
351         struct cl_read_ahead ra = { 0 };
352         int rc = 0, count = 0;
353         bool stride_ria;
354         pgoff_t page_idx;
355
356         LASSERT(ria != NULL);
357         RIA_DEBUG(ria);
358
359         stride_ria = ria->ria_length > ria->ria_bytes && ria->ria_bytes > 0;
360         for (page_idx = ria->ria_start;
361              page_idx <= ria->ria_end && ria->ria_reserved > 0; page_idx++) {
362                 if (ras_inside_ra_window(page_idx, ria)) {
363                         if (ra.cra_end == 0 || ra.cra_end < page_idx) {
364                                 unsigned long end;
365
366                                 cl_read_ahead_release(env, &ra);
367
368                                 rc = cl_io_read_ahead(env, io, page_idx, &ra);
369                                 if (rc < 0)
370                                         break;
371
372                                 /* Do not shrink the ria_end at any case until
373                                  * the minimum end of current read is covered.
374                                  * And only shrink the ria_end if the matched
375                                  * LDLM lock doesn't cover more. */
376                                 if (page_idx > ra.cra_end ||
377                                     (ra.cra_contention &&
378                                      page_idx > ria->ria_end_min)) {
379                                         ria->ria_end = ra.cra_end;
380                                         break;
381                                 }
382
383                                 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
384                                        page_idx, ra.cra_end, ra.cra_rpc_size);
385                                 LASSERTF(ra.cra_end >= page_idx,
386                                          "object: %p, indcies %lu / %lu\n",
387                                          io->ci_obj, ra.cra_end, page_idx);
388                                 /* update read ahead RPC size.
389                                  * NB: it's racy but doesn't matter */
390                                 if (ras->ras_rpc_size != ra.cra_rpc_size &&
391                                     ra.cra_rpc_size > 0)
392                                         ras->ras_rpc_size = ra.cra_rpc_size;
393                                 /* trim it to align with optimal RPC size */
394                                 end = ras_align(ras, ria->ria_end + 1, NULL);
395                                 if (end > 0 && !ria->ria_eof)
396                                         ria->ria_end = end - 1;
397                                 if (ria->ria_end < ria->ria_end_min)
398                                         ria->ria_end = ria->ria_end_min;
399                         }
400                         if (page_idx > ria->ria_end)
401                                 break;
402
403                         /* If the page is inside the read-ahead window */
404                         rc = ll_read_ahead_page(env, io, queue, page_idx);
405                         if (rc < 0)
406                                 break;
407
408                         *ra_end = page_idx;
409                         /* Only subtract from reserve & count the page if we
410                          * really did readahead on that page. */
411                         if (rc == 0) {
412                                 ria->ria_reserved--;
413                                 count++;
414                         }
415                 } else if (stride_ria) {
416                         /* If it is not in the read-ahead window, and it is
417                          * read-ahead mode, then check whether it should skip
418                          * the stride gap.
419                          */
420                         unsigned long offset;
421                         unsigned long pos = page_idx << PAGE_SHIFT;
422
423                         offset = (pos - ria->ria_stoff) % ria->ria_length;
424                         if (offset >= ria->ria_bytes) {
425                                 pos += (ria->ria_length - offset);
426                                 page_idx = (pos >> PAGE_SHIFT) - 1;
427                                 CDEBUG(D_READA,
428                                        "Stride: jump %lu pages to %lu\n",
429                                        ria->ria_length - offset, page_idx);
430                                 continue;
431                         }
432                 }
433         }
434
435         cl_read_ahead_release(env, &ra);
436
437         return count;
438 }
439
440 static void ll_readahead_work_free(struct ll_readahead_work *work)
441 {
442         fput(work->lrw_file);
443         OBD_FREE_PTR(work);
444 }
445
446 static void ll_readahead_handle_work(struct work_struct *wq);
447 static void ll_readahead_work_add(struct inode *inode,
448                                   struct ll_readahead_work *work)
449 {
450         INIT_WORK(&work->lrw_readahead_work, ll_readahead_handle_work);
451         queue_work(ll_i2sbi(inode)->ll_ra_info.ll_readahead_wq,
452                    &work->lrw_readahead_work);
453 }
454
455 static int ll_readahead_file_kms(const struct lu_env *env,
456                                 struct cl_io *io, __u64 *kms)
457 {
458         struct cl_object *clob;
459         struct inode *inode;
460         struct cl_attr *attr = vvp_env_thread_attr(env);
461         int ret;
462
463         clob = io->ci_obj;
464         inode = vvp_object_inode(clob);
465
466         cl_object_attr_lock(clob);
467         ret = cl_object_attr_get(env, clob, attr);
468         cl_object_attr_unlock(clob);
469
470         if (ret != 0)
471                 RETURN(ret);
472
473         *kms = attr->cat_kms;
474         return 0;
475 }
476
477 static void ll_readahead_handle_work(struct work_struct *wq)
478 {
479         struct ll_readahead_work *work;
480         struct lu_env *env;
481         __u16 refcheck;
482         struct ra_io_arg *ria;
483         struct inode *inode;
484         struct ll_file_data *fd;
485         struct ll_readahead_state *ras;
486         struct cl_io *io;
487         struct cl_2queue *queue;
488         pgoff_t ra_end = 0;
489         unsigned long len, mlen = 0;
490         struct file *file;
491         __u64 kms;
492         int rc;
493         unsigned long end_index;
494
495         work = container_of(wq, struct ll_readahead_work,
496                             lrw_readahead_work);
497         fd = LUSTRE_FPRIVATE(work->lrw_file);
498         ras = &fd->fd_ras;
499         file = work->lrw_file;
500         inode = file_inode(file);
501
502         env = cl_env_alloc(&refcheck, LCT_NOREF);
503         if (IS_ERR(env))
504                 GOTO(out_free_work, rc = PTR_ERR(env));
505
506         io = vvp_env_thread_io(env);
507         ll_io_init(io, file, CIT_READ, NULL);
508
509         rc = ll_readahead_file_kms(env, io, &kms);
510         if (rc != 0)
511                 GOTO(out_put_env, rc);
512
513         if (kms == 0) {
514                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
515                 GOTO(out_put_env, rc = 0);
516         }
517
518         ria = &ll_env_info(env)->lti_ria;
519         memset(ria, 0, sizeof(*ria));
520
521         ria->ria_start = work->lrw_start;
522         /* Truncate RA window to end of file */
523         end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
524         if (end_index <= work->lrw_end) {
525                 work->lrw_end = end_index;
526                 ria->ria_eof = true;
527         }
528         if (work->lrw_end <= work->lrw_start)
529                 GOTO(out_put_env, rc = 0);
530
531         ria->ria_end = work->lrw_end;
532         len = ria->ria_end - ria->ria_start + 1;
533         ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria,
534                                             ria_page_count(ria), mlen);
535
536         CDEBUG(D_READA,
537                "async reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
538                ria->ria_reserved, len, mlen,
539                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
540                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
541
542         if (ria->ria_reserved < len) {
543                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
544                 if (PAGES_TO_MiB(ria->ria_reserved) < 1) {
545                         ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
546                         GOTO(out_put_env, rc = 0);
547                 }
548         }
549
550         rc = cl_io_rw_init(env, io, CIT_READ, ria->ria_start, len);
551         if (rc)
552                 GOTO(out_put_env, rc);
553
554         vvp_env_io(env)->vui_io_subtype = IO_NORMAL;
555         vvp_env_io(env)->vui_fd = fd;
556         io->ci_state = CIS_LOCKED;
557         io->ci_async_readahead = true;
558         rc = cl_io_start(env, io);
559         if (rc)
560                 GOTO(out_io_fini, rc);
561
562         queue = &io->ci_queue;
563         cl_2queue_init(queue);
564
565         rc = ll_read_ahead_pages(env, io, &queue->c2_qin, ras, ria, &ra_end);
566         if (ria->ria_reserved != 0)
567                 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
568         if (queue->c2_qin.pl_nr > 0) {
569                 int count = queue->c2_qin.pl_nr;
570
571                 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
572                 if (rc == 0)
573                         task_io_account_read(PAGE_SIZE * count);
574         }
575         if (ria->ria_end == ra_end && ra_end == (kms >> PAGE_SHIFT))
576                 ll_ra_stats_inc(inode, RA_STAT_EOF);
577
578         if (ra_end != ria->ria_end)
579                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
580
581         /* TODO: discard all pages until page reinit route is implemented */
582         cl_page_list_discard(env, io, &queue->c2_qin);
583
584         /* Unlock unsent read pages in case of error. */
585         cl_page_list_disown(env, io, &queue->c2_qin);
586
587         cl_2queue_fini(env, queue);
588 out_io_fini:
589         cl_io_end(env, io);
590         cl_io_fini(env, io);
591 out_put_env:
592         cl_env_put(env, &refcheck);
593 out_free_work:
594         if (ra_end > 0)
595                 ll_ra_stats_inc_sbi(ll_i2sbi(inode), RA_STAT_ASYNC);
596         ll_readahead_work_free(work);
597 }
598
599 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
600                         struct cl_page_list *queue,
601                         struct ll_readahead_state *ras, bool hit,
602                         struct file *file)
603 {
604         struct vvp_io *vio = vvp_env_io(env);
605         struct ll_thread_info *lti = ll_env_info(env);
606         unsigned long len, mlen = 0;
607         pgoff_t ra_end = 0, start = 0, end = 0;
608         struct inode *inode;
609         struct ra_io_arg *ria = &lti->lti_ria;
610         struct cl_object *clob;
611         int ret = 0;
612         __u64 kms;
613         ENTRY;
614
615         clob = io->ci_obj;
616         inode = vvp_object_inode(clob);
617
618         memset(ria, 0, sizeof *ria);
619         ret = ll_readahead_file_kms(env, io, &kms);
620         if (ret != 0)
621                 RETURN(ret);
622
623         if (kms == 0) {
624                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
625                 RETURN(0);
626         }
627
628         spin_lock(&ras->ras_lock);
629
630         /**
631          * Note: other thread might rollback the ras_next_readahead,
632          * if it can not get the full size of prepared pages, see the
633          * end of this function. For stride read ahead, it needs to
634          * make sure the offset is no less than ras_stride_offset,
635          * so that stride read ahead can work correctly.
636          */
637         if (stride_io_mode(ras))
638                 start = max(ras->ras_next_readahead,
639                             ras->ras_stride_offset >> PAGE_SHIFT);
640         else
641                 start = ras->ras_next_readahead;
642
643         if (ras->ras_window_len > 0)
644                 end = ras->ras_window_start + ras->ras_window_len - 1;
645
646         /* Enlarge the RA window to encompass the full read */
647         if (vio->vui_ra_valid &&
648             end < vio->vui_ra_start + vio->vui_ra_count - 1)
649                 end = vio->vui_ra_start + vio->vui_ra_count - 1;
650
651         if (end != 0) {
652                 unsigned long end_index;
653
654                 /* Truncate RA window to end of file */
655                 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
656                 if (end_index <= end) {
657                         end = end_index;
658                         ria->ria_eof = true;
659                 }
660         }
661         ria->ria_start = start;
662         ria->ria_end = end;
663         /* If stride I/O mode is detected, get stride window*/
664         if (stride_io_mode(ras)) {
665                 ria->ria_stoff = ras->ras_stride_offset;
666                 ria->ria_length = ras->ras_stride_length;
667                 ria->ria_bytes = ras->ras_stride_bytes;
668         }
669         spin_unlock(&ras->ras_lock);
670
671         if (end == 0) {
672                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
673                 RETURN(0);
674         }
675         len = ria_page_count(ria);
676         if (len == 0) {
677                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
678                 RETURN(0);
679         }
680
681         RAS_CDEBUG(ras);
682         CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
683                PFID(lu_object_fid(&clob->co_lu)),
684                ria->ria_start, ria->ria_end,
685                vio->vui_ra_valid ? vio->vui_ra_start : 0,
686                vio->vui_ra_valid ? vio->vui_ra_count : 0,
687                hit);
688
689         /* at least to extend the readahead window to cover current read */
690         if (!hit && vio->vui_ra_valid &&
691             vio->vui_ra_start + vio->vui_ra_count > ria->ria_start)
692                 ria->ria_end_min = vio->vui_ra_start + vio->vui_ra_count - 1;
693
694         ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
695         if (ria->ria_reserved < len)
696                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
697
698         CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
699                ria->ria_reserved, len, mlen,
700                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
701                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
702
703         ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end);
704
705         if (ria->ria_reserved != 0)
706                 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
707
708         if (ra_end == end && ra_end == (kms >> PAGE_SHIFT))
709                 ll_ra_stats_inc(inode, RA_STAT_EOF);
710
711         CDEBUG(D_READA, "ra_end = %lu end = %lu stride end = %lu pages = %d\n",
712                ra_end, end, ria->ria_end, ret);
713
714         if (ra_end != end)
715                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
716         if (ra_end > 0) {
717                 /* update the ras so that the next read-ahead tries from
718                  * where we left off. */
719                 spin_lock(&ras->ras_lock);
720                 ras->ras_next_readahead = ra_end + 1;
721                 spin_unlock(&ras->ras_lock);
722                 RAS_CDEBUG(ras);
723         }
724
725         RETURN(ret);
726 }
727
728 static void ras_set_start(struct ll_readahead_state *ras, pgoff_t index)
729 {
730         ras->ras_window_start = ras_align(ras, index, NULL);
731 }
732
733 /* called with the ras_lock held or from places where it doesn't matter */
734 static void ras_reset(struct ll_readahead_state *ras, pgoff_t index)
735 {
736         ras->ras_consecutive_requests = 0;
737         ras->ras_consecutive_bytes = 0;
738         ras->ras_window_len = 0;
739         ras_set_start(ras, index);
740         ras->ras_next_readahead = max(ras->ras_window_start, index + 1);
741
742         RAS_CDEBUG(ras);
743 }
744
745 /* called with the ras_lock held or from places where it doesn't matter */
746 static void ras_stride_reset(struct ll_readahead_state *ras)
747 {
748         ras->ras_consecutive_stride_requests = 0;
749         ras->ras_stride_length = 0;
750         ras->ras_stride_bytes = 0;
751         RAS_CDEBUG(ras);
752 }
753
754 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
755 {
756         spin_lock_init(&ras->ras_lock);
757         ras->ras_rpc_size = PTLRPC_MAX_BRW_PAGES;
758         ras_reset(ras, 0);
759         ras->ras_last_read_end = 0;
760         ras->ras_requests = 0;
761 }
762
763 /*
764  * Check whether the read request is in the stride window.
765  * If it is in the stride window, return true, otherwise return false.
766  */
767 static bool index_in_stride_window(struct ll_readahead_state *ras,
768                                    pgoff_t index)
769 {
770         unsigned long stride_gap;
771         unsigned long pos = index << PAGE_SHIFT;
772
773         if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
774             ras->ras_stride_bytes == ras->ras_stride_length)
775                 return false;
776
777         stride_gap = pos - ras->ras_last_read_end - 1;
778
779         /* If it is contiguous read */
780         if (stride_gap == 0)
781                 return ras->ras_consecutive_bytes + PAGE_SIZE <=
782                         ras->ras_stride_bytes;
783
784         /* Otherwise check the stride by itself */
785         return (ras->ras_stride_length - ras->ras_stride_bytes) == stride_gap &&
786                 ras->ras_consecutive_bytes == ras->ras_stride_bytes;
787 }
788
789 static void ras_init_stride_detector(struct ll_readahead_state *ras,
790                                      unsigned long pos, unsigned long count)
791 {
792         unsigned long stride_gap = pos - ras->ras_last_read_end - 1;
793
794         if (!stride_io_mode(ras) && (stride_gap != 0 ||
795              ras->ras_consecutive_stride_requests == 0)) {
796                 ras->ras_stride_bytes = ras->ras_consecutive_bytes;
797                 ras->ras_stride_length = stride_gap + ras->ras_consecutive_bytes;
798         }
799         LASSERT(ras->ras_request_index == 0);
800         LASSERT(ras->ras_consecutive_stride_requests == 0);
801
802         if (pos <= ras->ras_last_read_end) {
803                 /*Reset stride window for forward read*/
804                 ras_stride_reset(ras);
805                 return;
806         }
807
808         ras->ras_stride_bytes = ras->ras_consecutive_bytes;
809         ras->ras_stride_length = stride_gap + ras->ras_consecutive_bytes;
810
811         RAS_CDEBUG(ras);
812 }
813
814 static unsigned long
815 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
816 {
817         unsigned int bytes_count =
818                 stride_byte_count(ras->ras_stride_offset,
819                                   ras->ras_stride_length, ras->ras_stride_bytes,
820                                    ras->ras_stride_offset, len);
821
822         return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
823 }
824
825 /* Stride Read-ahead window will be increased inc_len according to
826  * stride I/O pattern */
827 static void ras_stride_increase_window(struct ll_readahead_state *ras,
828                                        struct ll_ra_info *ra,
829                                        unsigned long inc_len)
830 {
831         unsigned long left, step, window_len;
832         unsigned long stride_len;
833         unsigned long end = ras->ras_window_start + ras->ras_window_len;
834
835         LASSERT(ras->ras_stride_length > 0);
836         LASSERTF(end >= (ras->ras_stride_offset >> PAGE_SHIFT),
837                  "window_start %lu, window_len %lu stride_offset %lu\n",
838                  ras->ras_window_start, ras->ras_window_len,
839                  ras->ras_stride_offset);
840
841         end <<= PAGE_SHIFT;
842         if (end < ras->ras_stride_offset)
843                 stride_len = 0;
844         else
845                 stride_len = end - ras->ras_stride_offset;
846
847         left = stride_len % ras->ras_stride_length;
848         window_len = (ras->ras_window_len << PAGE_SHIFT) - left;
849
850         if (left < ras->ras_stride_bytes)
851                 left += inc_len;
852         else
853                 left = ras->ras_stride_bytes + inc_len;
854
855         LASSERT(ras->ras_stride_bytes != 0);
856
857         step = left / ras->ras_stride_bytes;
858         left %= ras->ras_stride_bytes;
859
860         window_len += step * ras->ras_stride_length + left;
861
862         if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
863                 ras->ras_window_len = (window_len >> PAGE_SHIFT);
864
865         RAS_CDEBUG(ras);
866 }
867
868 static void ras_increase_window(struct inode *inode,
869                                 struct ll_readahead_state *ras,
870                                 struct ll_ra_info *ra)
871 {
872         /* The stretch of ra-window should be aligned with max rpc_size
873          * but current clio architecture does not support retrieve such
874          * information from lower layer. FIXME later
875          */
876         if (stride_io_mode(ras)) {
877                 ras_stride_increase_window(ras, ra,
878                                 ras->ras_rpc_size << PAGE_SHIFT);
879         } else {
880                 unsigned long wlen;
881
882                 wlen = min(ras->ras_window_len + ras->ras_rpc_size,
883                            ra->ra_max_pages_per_file);
884                 if (wlen < ras->ras_rpc_size)
885                         ras->ras_window_len = wlen;
886                 else
887                         ras->ras_window_len = ras_align(ras, wlen, NULL);
888         }
889 }
890
891 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
892                        struct ll_readahead_state *ras, unsigned long index,
893                        enum ras_update_flags flags)
894 {
895         struct ll_ra_info *ra = &sbi->ll_ra_info;
896         bool hit = flags & LL_RAS_HIT;
897         int zero = 0, stride_detect = 0, ra_miss = 0;
898         unsigned long pos = index << PAGE_SHIFT;
899         ENTRY;
900
901         spin_lock(&ras->ras_lock);
902
903         if (!hit)
904                 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
905                        PFID(ll_inode2fid(inode)), index);
906         ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
907
908         /* reset the read-ahead window in two cases.  First when the app seeks
909          * or reads to some other part of the file.  Secondly if we get a
910          * read-ahead miss that we think we've previously issued.  This can
911          * be a symptom of there being so many read-ahead pages that the VM is
912          * reclaiming it before we get to it. */
913         if (!pos_in_window(pos, ras->ras_last_read_end,
914                            8 << PAGE_SHIFT, 8 << PAGE_SHIFT)) {
915                 zero = 1;
916                 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
917         } else if (!hit && ras->ras_window_len &&
918                    index < ras->ras_next_readahead &&
919                    pos_in_window(index, ras->ras_window_start, 0,
920                                  ras->ras_window_len)) {
921                 ra_miss = 1;
922                 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
923         }
924
925         /* On the second access to a file smaller than the tunable
926          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
927          * file up to ra_max_pages_per_file.  This is simply a best effort
928          * and only occurs once per open file.  Normal RA behavior is reverted
929          * to for subsequent IO.  The mmap case does not increment
930          * ras_requests and thus can never trigger this behavior. */
931         if (ras->ras_requests >= 2 && !ras->ras_request_index) {
932                 __u64 kms_pages;
933
934                 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
935                             PAGE_SHIFT;
936
937                 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
938                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
939
940                 if (kms_pages &&
941                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
942                         ras->ras_window_start = 0;
943                         ras->ras_next_readahead = index + 1;
944                         ras->ras_window_len = min(ra->ra_max_pages_per_file,
945                                 ra->ra_max_read_ahead_whole_pages);
946                         GOTO(out_unlock, 0);
947                 }
948         }
949         if (zero) {
950                 /* check whether it is in stride I/O mode*/
951                 if (!index_in_stride_window(ras, index)) {
952                         if (ras->ras_consecutive_stride_requests == 0 &&
953                             ras->ras_request_index == 0) {
954                                 ras_init_stride_detector(ras, pos, PAGE_SIZE);
955                                 ras->ras_consecutive_stride_requests++;
956                         } else {
957                                 ras_stride_reset(ras);
958                         }
959                         ras_reset(ras, index);
960                         ras->ras_consecutive_bytes += PAGE_SIZE;
961                         GOTO(out_unlock, 0);
962                 } else {
963                         ras->ras_consecutive_bytes = 0;
964                         ras->ras_consecutive_requests = 0;
965                         if (++ras->ras_consecutive_stride_requests > 1)
966                                 stride_detect = 1;
967                         RAS_CDEBUG(ras);
968                 }
969         } else {
970                 if (ra_miss) {
971                         if (index_in_stride_window(ras, index) &&
972                             stride_io_mode(ras)) {
973                                 if (index != (ras->ras_last_read_end >>
974                                               PAGE_SHIFT) + 1)
975                                         ras->ras_consecutive_bytes = 0;
976                                 ras_reset(ras, index);
977
978                                 /* If stride-RA hit cache miss, the stride
979                                  * detector will not be reset to avoid the
980                                  * overhead of redetecting read-ahead mode,
981                                  * but on the condition that the stride window
982                                  * is still intersect with normal sequential
983                                  * read-ahead window. */
984                                 if (ras->ras_window_start <
985                                     (ras->ras_stride_offset >> PAGE_SHIFT))
986                                         ras_stride_reset(ras);
987                                 RAS_CDEBUG(ras);
988                         } else {
989                                 /* Reset both stride window and normal RA
990                                  * window */
991                                 ras_reset(ras, index);
992                                 ras->ras_consecutive_bytes += PAGE_SIZE;
993                                 ras_stride_reset(ras);
994                                 GOTO(out_unlock, 0);
995                         }
996                 } else if (stride_io_mode(ras)) {
997                         /* If this is contiguous read but in stride I/O mode
998                          * currently, check whether stride step still is valid,
999                          * if invalid, it will reset the stride ra window*/
1000                         if (!index_in_stride_window(ras, index)) {
1001                                 /* Shrink stride read-ahead window to be zero */
1002                                 ras_stride_reset(ras);
1003                                 ras->ras_window_len = 0;
1004                                 ras->ras_next_readahead = index;
1005                         }
1006                 }
1007         }
1008         ras->ras_consecutive_bytes += PAGE_SIZE;
1009         ras_set_start(ras, index);
1010
1011         if (stride_io_mode(ras)) {
1012                 /* Since stride readahead is sentivite to the offset
1013                  * of read-ahead, so we use original offset here,
1014                  * instead of ras_window_start, which is RPC aligned */
1015                 ras->ras_next_readahead = max(index + 1,
1016                                               ras->ras_next_readahead);
1017                 ras->ras_window_start =
1018                                 max(ras->ras_stride_offset >> PAGE_SHIFT,
1019                                     ras->ras_window_start);
1020         } else {
1021                 if (ras->ras_next_readahead < ras->ras_window_start)
1022                         ras->ras_next_readahead = ras->ras_window_start;
1023                 if (!hit)
1024                         ras->ras_next_readahead = index + 1;
1025         }
1026         RAS_CDEBUG(ras);
1027
1028         /* Trigger RA in the mmap case where ras_consecutive_requests
1029          * is not incremented and thus can't be used to trigger RA */
1030         if (ras->ras_consecutive_bytes >= (4 << PAGE_SHIFT) &&
1031             flags & LL_RAS_MMAP) {
1032                 ras_increase_window(inode, ras, ra);
1033                 /* reset consecutive pages so that the readahead window can
1034                  * grow gradually. */
1035                 ras->ras_consecutive_bytes = 0;
1036                 GOTO(out_unlock, 0);
1037         }
1038
1039         /* Initially reset the stride window offset to next_readahead*/
1040         if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1041                 /**
1042                  * Once stride IO mode is detected, next_readahead should be
1043                  * reset to make sure next_readahead > stride offset
1044                  */
1045                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1046                 ras->ras_stride_offset = index << PAGE_SHIFT;
1047                 ras->ras_window_start = max(index, ras->ras_window_start);
1048         }
1049
1050         /* The initial ras_window_len is set to the request size.  To avoid
1051          * uselessly reading and discarding pages for random IO the window is
1052          * only increased once per consecutive request received. */
1053         if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1054             !ras->ras_request_index)
1055                 ras_increase_window(inode, ras, ra);
1056         EXIT;
1057 out_unlock:
1058         RAS_CDEBUG(ras);
1059         ras->ras_request_index++;
1060         ras->ras_last_read_end = pos + PAGE_SIZE - 1;
1061         spin_unlock(&ras->ras_lock);
1062 }
1063
1064 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1065 {
1066         struct inode           *inode = vmpage->mapping->host;
1067         struct ll_inode_info   *lli   = ll_i2info(inode);
1068         struct lu_env          *env;
1069         struct cl_io           *io;
1070         struct cl_page         *page;
1071         struct cl_object       *clob;
1072         bool redirtied = false;
1073         bool unlocked = false;
1074         int result;
1075         __u16 refcheck;
1076         ENTRY;
1077
1078         LASSERT(PageLocked(vmpage));
1079         LASSERT(!PageWriteback(vmpage));
1080
1081         LASSERT(ll_i2dtexp(inode) != NULL);
1082
1083         env = cl_env_get(&refcheck);
1084         if (IS_ERR(env))
1085                 GOTO(out, result = PTR_ERR(env));
1086
1087         clob  = ll_i2info(inode)->lli_clob;
1088         LASSERT(clob != NULL);
1089
1090         io = vvp_env_thread_io(env);
1091         io->ci_obj = clob;
1092         io->ci_ignore_layout = 1;
1093         result = cl_io_init(env, io, CIT_MISC, clob);
1094         if (result == 0) {
1095                 page = cl_page_find(env, clob, vmpage->index,
1096                                     vmpage, CPT_CACHEABLE);
1097                 if (!IS_ERR(page)) {
1098                         lu_ref_add(&page->cp_reference, "writepage",
1099                                    current);
1100                         cl_page_assume(env, io, page);
1101                         result = cl_page_flush(env, io, page);
1102                         if (result != 0) {
1103                                 /*
1104                                  * Re-dirty page on error so it retries write,
1105                                  * but not in case when IO has actually
1106                                  * occurred and completed with an error.
1107                                  */
1108                                 if (!PageError(vmpage)) {
1109                                         redirty_page_for_writepage(wbc, vmpage);
1110                                         result = 0;
1111                                         redirtied = true;
1112                                 }
1113                         }
1114                         cl_page_disown(env, io, page);
1115                         unlocked = true;
1116                         lu_ref_del(&page->cp_reference,
1117                                    "writepage", current);
1118                         cl_page_put(env, page);
1119                 } else {
1120                         result = PTR_ERR(page);
1121                 }
1122         }
1123         cl_io_fini(env, io);
1124
1125         if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1126                 loff_t offset = cl_offset(clob, vmpage->index);
1127
1128                 /* Flush page failed because the extent is being written out.
1129                  * Wait for the write of extent to be finished to avoid
1130                  * breaking kernel which assumes ->writepage should mark
1131                  * PageWriteback or clean the page. */
1132                 result = cl_sync_file_range(inode, offset,
1133                                             offset + PAGE_SIZE - 1,
1134                                             CL_FSYNC_LOCAL, 1);
1135                 if (result > 0) {
1136                         /* actually we may have written more than one page.
1137                          * decreasing this page because the caller will count
1138                          * it. */
1139                         wbc->nr_to_write -= result - 1;
1140                         result = 0;
1141                 }
1142         }
1143
1144         cl_env_put(env, &refcheck);
1145         GOTO(out, result);
1146
1147 out:
1148         if (result < 0) {
1149                 if (!lli->lli_async_rc)
1150                         lli->lli_async_rc = result;
1151                 SetPageError(vmpage);
1152                 if (!unlocked)
1153                         unlock_page(vmpage);
1154         }
1155         return result;
1156 }
1157
1158 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1159 {
1160         struct inode *inode = mapping->host;
1161         loff_t start;
1162         loff_t end;
1163         enum cl_fsync_mode mode;
1164         int range_whole = 0;
1165         int result;
1166         ENTRY;
1167
1168         if (wbc->range_cyclic) {
1169                 start = mapping->writeback_index << PAGE_SHIFT;
1170                 end = OBD_OBJECT_EOF;
1171         } else {
1172                 start = wbc->range_start;
1173                 end = wbc->range_end;
1174                 if (end == LLONG_MAX) {
1175                         end = OBD_OBJECT_EOF;
1176                         range_whole = start == 0;
1177                 }
1178         }
1179
1180         mode = CL_FSYNC_NONE;
1181         if (wbc->sync_mode == WB_SYNC_ALL)
1182                 mode = CL_FSYNC_LOCAL;
1183
1184         if (ll_i2info(inode)->lli_clob == NULL)
1185                 RETURN(0);
1186
1187         /* for directio, it would call writepages() to evict cached pages
1188          * inside the IO context of write, which will cause deadlock at
1189          * layout_conf since it waits for active IOs to complete. */
1190         result = cl_sync_file_range(inode, start, end, mode, 1);
1191         if (result > 0) {
1192                 wbc->nr_to_write -= result;
1193                 result = 0;
1194          }
1195
1196         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1197                 if (end == OBD_OBJECT_EOF)
1198                         mapping->writeback_index = 0;
1199                 else
1200                         mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1201         }
1202         RETURN(result);
1203 }
1204
1205 struct ll_cl_context *ll_cl_find(struct file *file)
1206 {
1207         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1208         struct ll_cl_context *lcc;
1209         struct ll_cl_context *found = NULL;
1210
1211         read_lock(&fd->fd_lock);
1212         list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1213                 if (lcc->lcc_cookie == current) {
1214                         found = lcc;
1215                         break;
1216                 }
1217         }
1218         read_unlock(&fd->fd_lock);
1219
1220         return found;
1221 }
1222
1223 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
1224                enum lcc_type type)
1225 {
1226         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1227         struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1228
1229         memset(lcc, 0, sizeof(*lcc));
1230         INIT_LIST_HEAD(&lcc->lcc_list);
1231         lcc->lcc_cookie = current;
1232         lcc->lcc_env = env;
1233         lcc->lcc_io = io;
1234         lcc->lcc_type = type;
1235
1236         write_lock(&fd->fd_lock);
1237         list_add(&lcc->lcc_list, &fd->fd_lccs);
1238         write_unlock(&fd->fd_lock);
1239 }
1240
1241 void ll_cl_remove(struct file *file, const struct lu_env *env)
1242 {
1243         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1244         struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1245
1246         write_lock(&fd->fd_lock);
1247         list_del_init(&lcc->lcc_list);
1248         write_unlock(&fd->fd_lock);
1249 }
1250
1251 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1252                            struct cl_page *page, struct file *file)
1253 {
1254         struct inode              *inode  = vvp_object_inode(page->cp_obj);
1255         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
1256         struct ll_file_data       *fd     = LUSTRE_FPRIVATE(file);
1257         struct ll_readahead_state *ras    = &fd->fd_ras;
1258         struct cl_2queue          *queue  = &io->ci_queue;
1259         struct cl_sync_io         *anchor = NULL;
1260         struct vvp_page           *vpg;
1261         int                        rc = 0;
1262         bool                       uptodate;
1263         ENTRY;
1264
1265         vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1266         uptodate = vpg->vpg_defer_uptodate;
1267
1268         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1269             sbi->ll_ra_info.ra_max_pages > 0 &&
1270             !vpg->vpg_ra_updated) {
1271                 struct vvp_io *vio = vvp_env_io(env);
1272                 enum ras_update_flags flags = 0;
1273
1274                 if (uptodate)
1275                         flags |= LL_RAS_HIT;
1276                 if (!vio->vui_ra_valid)
1277                         flags |= LL_RAS_MMAP;
1278                 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1279         }
1280
1281         cl_2queue_init(queue);
1282         if (uptodate) {
1283                 vpg->vpg_ra_used = 1;
1284                 cl_page_export(env, page, 1);
1285                 cl_page_disown(env, io, page);
1286         } else {
1287                 anchor = &vvp_env_info(env)->vti_anchor;
1288                 cl_sync_io_init(anchor, 1);
1289                 page->cp_sync_io = anchor;
1290
1291                 cl_2queue_add(queue, page);
1292         }
1293
1294         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1295             sbi->ll_ra_info.ra_max_pages > 0) {
1296                 int rc2;
1297
1298                 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1299                                    uptodate, file);
1300                 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1301                        PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1302         }
1303
1304         if (queue->c2_qin.pl_nr > 0) {
1305                 int count = queue->c2_qin.pl_nr;
1306                 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1307                 if (rc == 0)
1308                         task_io_account_read(PAGE_SIZE * count);
1309         }
1310
1311
1312         if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1313                 rc = cl_sync_io_wait(env, anchor, 0);
1314
1315                 cl_page_assume(env, io, page);
1316                 cl_page_list_del(env, &queue->c2_qout, page);
1317
1318                 if (!PageUptodate(cl_page_vmpage(page))) {
1319                         /* Failed to read a mirror, discard this page so that
1320                          * new page can be created with new mirror.
1321                          *
1322                          * TODO: this is not needed after page reinit
1323                          * route is implemented */
1324                         cl_page_discard(env, io, page);
1325                 }
1326                 cl_page_disown(env, io, page);
1327         }
1328
1329         /* TODO: discard all pages until page reinit route is implemented */
1330         cl_page_list_discard(env, io, &queue->c2_qin);
1331
1332         /* Unlock unsent read pages in case of error. */
1333         cl_page_list_disown(env, io, &queue->c2_qin);
1334
1335         cl_2queue_fini(env, queue);
1336
1337         RETURN(rc);
1338 }
1339
1340 /*
1341  * Possible return value:
1342  * 0 no async readahead triggered and fast read could not be used.
1343  * 1 no async readahead, but fast read could be used.
1344  * 2 async readahead triggered and fast read could be used too.
1345  * < 0 on error.
1346  */
1347 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1348 {
1349         struct ll_readahead_work *lrw;
1350         struct inode *inode = file_inode(file);
1351         struct ll_sb_info *sbi = ll_i2sbi(inode);
1352         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1353         struct ll_readahead_state *ras = &fd->fd_ras;
1354         struct ll_ra_info *ra = &sbi->ll_ra_info;
1355         unsigned long throttle;
1356         unsigned long start = ras_align(ras, ras->ras_next_readahead, NULL);
1357         unsigned long end = start + pages - 1;
1358
1359         throttle = min(ra->ra_async_pages_per_file_threshold,
1360                        ra->ra_max_pages_per_file);
1361         /*
1362          * If this is strided i/o or the window is smaller than the
1363          * throttle limit, we do not do async readahead. Otherwise,
1364          * we do async readahead, allowing the user thread to do fast i/o.
1365          */
1366         if (stride_io_mode(ras) || !throttle ||
1367             ras->ras_window_len < throttle)
1368                 return 0;
1369
1370         if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1371                 return 0;
1372
1373         if (ras->ras_async_last_readpage == start)
1374                 return 1;
1375
1376         /* ll_readahead_work_free() free it */
1377         OBD_ALLOC_PTR(lrw);
1378         if (lrw) {
1379                 lrw->lrw_file = get_file(file);
1380                 lrw->lrw_start = start;
1381                 lrw->lrw_end = end;
1382                 spin_lock(&ras->ras_lock);
1383                 ras->ras_next_readahead = end + 1;
1384                 ras->ras_async_last_readpage = start;
1385                 spin_unlock(&ras->ras_lock);
1386                 ll_readahead_work_add(inode, lrw);
1387         } else {
1388                 return -ENOMEM;
1389         }
1390
1391         return 2;
1392 }
1393
1394 int ll_readpage(struct file *file, struct page *vmpage)
1395 {
1396         struct inode *inode = file_inode(file);
1397         struct cl_object *clob = ll_i2info(inode)->lli_clob;
1398         struct ll_cl_context *lcc;
1399         const struct lu_env  *env = NULL;
1400         struct cl_io   *io = NULL;
1401         struct cl_page *page;
1402         struct ll_sb_info *sbi = ll_i2sbi(inode);
1403         int result;
1404         ENTRY;
1405
1406         lcc = ll_cl_find(file);
1407         if (lcc != NULL) {
1408                 env = lcc->lcc_env;
1409                 io  = lcc->lcc_io;
1410         }
1411
1412         if (io == NULL) { /* fast read */
1413                 struct inode *inode = file_inode(file);
1414                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1415                 struct ll_readahead_state *ras = &fd->fd_ras;
1416                 struct lu_env  *local_env = NULL;
1417                 unsigned long fast_read_pages =
1418                         max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_size);
1419                 struct vvp_page *vpg;
1420
1421                 result = -ENODATA;
1422
1423                 /* TODO: need to verify the layout version to make sure
1424                  * the page is not invalid due to layout change. */
1425                 page = cl_vmpage_page(vmpage, clob);
1426                 if (page == NULL) {
1427                         unlock_page(vmpage);
1428                         ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1429                         RETURN(result);
1430                 }
1431
1432                 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1433                 if (vpg->vpg_defer_uptodate) {
1434                         enum ras_update_flags flags = LL_RAS_HIT;
1435
1436                         if (lcc && lcc->lcc_type == LCC_MMAP)
1437                                 flags |= LL_RAS_MMAP;
1438
1439                         /* For fast read, it updates read ahead state only
1440                          * if the page is hit in cache because non cache page
1441                          * case will be handled by slow read later. */
1442                         ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1443                         /* avoid duplicate ras_update() call */
1444                         vpg->vpg_ra_updated = 1;
1445
1446                         /* Check if we can issue a readahead RPC, if that is
1447                          * the case, we can't do fast IO because we will need
1448                          * a cl_io to issue the RPC. */
1449                         if (ras->ras_window_start + ras->ras_window_len <
1450                             ras->ras_next_readahead + fast_read_pages ||
1451                             kickoff_async_readahead(file, fast_read_pages) > 0)
1452                                 result = 0;
1453                 }
1454
1455                 if (!env) {
1456                         local_env = cl_env_percpu_get();
1457                         env = local_env;
1458                 }
1459
1460                 /* export the page and skip io stack */
1461                 if (result == 0) {
1462                         vpg->vpg_ra_used = 1;
1463                         cl_page_export(env, page, 1);
1464                 } else {
1465                         ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1466                 }
1467                 /* release page refcount before unlocking the page to ensure
1468                  * the object won't be destroyed in the calling path of
1469                  * cl_page_put(). Please see comment in ll_releasepage(). */
1470                 cl_page_put(env, page);
1471                 unlock_page(vmpage);
1472                 if (local_env)
1473                         cl_env_percpu_put(local_env);
1474
1475                 RETURN(result);
1476         }
1477
1478         LASSERT(io->ci_state == CIS_IO_GOING);
1479         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1480         if (!IS_ERR(page)) {
1481                 LASSERT(page->cp_type == CPT_CACHEABLE);
1482                 if (likely(!PageUptodate(vmpage))) {
1483                         cl_page_assume(env, io, page);
1484
1485                         result = ll_io_read_page(env, io, page, file);
1486                 } else {
1487                         /* Page from a non-object file. */
1488                         unlock_page(vmpage);
1489                         result = 0;
1490                 }
1491                 cl_page_put(env, page);
1492         } else {
1493                 unlock_page(vmpage);
1494                 result = PTR_ERR(page);
1495         }
1496         RETURN(result);
1497 }