1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 <linux/uaccess.h>
45
46#include <linux/fs.h>
47#include <linux/pagemap.h>
48
49#include <linux/swap.h>
50#include <linux/bvec.h>
51
52#define DEBUG_SUBSYSTEM S_LLITE
53
54#include "../include/obd_cksum.h"
55#include "llite_internal.h"
56
57static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
79 struct ra_io_arg *ria,
80 unsigned long pages, unsigned long min)
81{
82 struct ll_ra_info *ra = &sbi->ll_ra_info;
83 long ret;
84
85
86
87
88
89 ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
90 if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages)) {
91 ret = 0;
92 goto out;
93 }
94
95 if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
96 atomic_sub(ret, &ra->ra_cur_pages);
97 ret = 0;
98 }
99
100out:
101 if (ret < min) {
102
103 atomic_add(min - ret, &ra->ra_cur_pages);
104 ret = min;
105 }
106 return ret;
107}
108
109void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
110{
111 struct ll_ra_info *ra = &sbi->ll_ra_info;
112
113 atomic_sub(len, &ra->ra_cur_pages);
114}
115
116static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
117{
118 LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
119 lprocfs_counter_incr(sbi->ll_ra_stats, which);
120}
121
122void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
123{
124 struct ll_sb_info *sbi = ll_i2sbi(inode);
125
126 ll_ra_stats_inc_sbi(sbi, which);
127}
128
129#define RAS_CDEBUG(ras) \
130 CDEBUG(D_READA, \
131 "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu rpc %lu " \
132 "r %lu ri %lu csr %lu sf %lu sp %lu sl %lu\n", \
133 ras->ras_last_readpage, ras->ras_consecutive_requests, \
134 ras->ras_consecutive_pages, ras->ras_window_start, \
135 ras->ras_window_len, ras->ras_next_readahead, \
136 ras->ras_rpc_size, \
137 ras->ras_requests, ras->ras_request_index, \
138 ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
139 ras->ras_stride_pages, ras->ras_stride_length)
140
141static int index_in_window(unsigned long index, unsigned long point,
142 unsigned long before, unsigned long after)
143{
144 unsigned long start = point - before, end = point + after;
145
146 if (start > point)
147 start = 0;
148 if (end < point)
149 end = ~0;
150
151 return start <= index && index <= end;
152}
153
154void ll_ras_enter(struct file *f)
155{
156 struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
157 struct ll_readahead_state *ras = &fd->fd_ras;
158
159 spin_lock(&ras->ras_lock);
160 ras->ras_requests++;
161 ras->ras_request_index = 0;
162 ras->ras_consecutive_requests++;
163 spin_unlock(&ras->ras_lock);
164}
165
166
167
168
169
170
171
172
173
174static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
175 struct cl_page_list *queue, pgoff_t index)
176{
177 enum ra_stat which = _NR_RA_STAT;
178 struct cl_object *clob = io->ci_obj;
179 struct inode *inode = vvp_object_inode(clob);
180 const char *msg = NULL;
181 struct cl_page *page;
182 struct vvp_page *vpg;
183 struct page *vmpage;
184 int rc = 0;
185
186 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
187 if (!vmpage) {
188 which = RA_STAT_FAILED_GRAB_PAGE;
189 msg = "g_c_p_n failed";
190 rc = -EBUSY;
191 goto out;
192 }
193
194
195 if (vmpage->mapping != inode->i_mapping) {
196 which = RA_STAT_WRONG_GRAB_PAGE;
197 msg = "g_c_p_n returned invalid page";
198 rc = -EBUSY;
199 goto out;
200 }
201
202 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
203 if (IS_ERR(page)) {
204 which = RA_STAT_FAILED_GRAB_PAGE;
205 msg = "cl_page_find failed";
206 rc = PTR_ERR(page);
207 goto out;
208 }
209
210 lu_ref_add(&page->cp_reference, "ra", current);
211 cl_page_assume(env, io, page);
212 vpg = cl2vvp_page(cl_object_page_slice(clob, page));
213 if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
214 vpg->vpg_defer_uptodate = 1;
215 vpg->vpg_ra_used = 0;
216 cl_page_list_add(queue, page);
217 } else {
218
219 cl_page_unassume(env, io, page);
220
221
222
223 rc = 1;
224 }
225
226 lu_ref_del(&page->cp_reference, "ra", current);
227 cl_page_put(env, page);
228out:
229 if (vmpage) {
230 if (rc)
231 unlock_page(vmpage);
232 put_page(vmpage);
233 }
234 if (msg) {
235 ll_ra_stats_inc(inode, which);
236 CDEBUG(D_READA, "%s\n", msg);
237 }
238 return rc;
239}
240
241#define RIA_DEBUG(ria) \
242 CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n", \
243 ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
244 ria->ria_pages)
245
246static inline int stride_io_mode(struct ll_readahead_state *ras)
247{
248 return ras->ras_consecutive_stride_requests > 1;
249}
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269static unsigned long
270stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
271 unsigned long off, unsigned long length)
272{
273 __u64 start = off > st_off ? off - st_off : 0;
274 __u64 end = off + length > st_off ? off + length - st_off : 0;
275 unsigned long start_left = 0;
276 unsigned long end_left = 0;
277 unsigned long pg_count;
278
279 if (st_len == 0 || length == 0 || end == 0)
280 return length;
281
282 start_left = do_div(start, st_len);
283 if (start_left < st_pgs)
284 start_left = st_pgs - start_left;
285 else
286 start_left = 0;
287
288 end_left = do_div(end, st_len);
289 if (end_left > st_pgs)
290 end_left = st_pgs;
291
292 CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
293 start, end, start_left, end_left);
294
295 if (start == end)
296 pg_count = end_left - (st_pgs - start_left);
297 else
298 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
299
300 CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu pgcount %lu\n",
301 st_off, st_len, st_pgs, off, length, pg_count);
302
303 return pg_count;
304}
305
306static int ria_page_count(struct ra_io_arg *ria)
307{
308 __u64 length = ria->ria_end >= ria->ria_start ?
309 ria->ria_end - ria->ria_start + 1 : 0;
310
311 return stride_pg_count(ria->ria_stoff, ria->ria_length,
312 ria->ria_pages, ria->ria_start,
313 length);
314}
315
316static unsigned long ras_align(struct ll_readahead_state *ras,
317 unsigned long index,
318 unsigned long *remainder)
319{
320 unsigned long rem = index % ras->ras_rpc_size;
321
322 if (remainder)
323 *remainder = rem;
324 return index - rem;
325}
326
327
328static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
329{
330
331
332
333
334
335 return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
336 (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
337 ria->ria_length < ria->ria_pages);
338}
339
340static unsigned long
341ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
342 struct cl_page_list *queue, struct ll_readahead_state *ras,
343 struct ra_io_arg *ria)
344{
345 struct cl_read_ahead ra = { 0 };
346 unsigned long ra_end = 0;
347 bool stride_ria;
348 pgoff_t page_idx;
349 int rc;
350
351 LASSERT(ria);
352 RIA_DEBUG(ria);
353
354 stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
355 for (page_idx = ria->ria_start;
356 page_idx <= ria->ria_end && ria->ria_reserved > 0; page_idx++) {
357 if (ras_inside_ra_window(page_idx, ria)) {
358 if (!ra.cra_end || ra.cra_end < page_idx) {
359 unsigned long end;
360
361 cl_read_ahead_release(env, &ra);
362
363 rc = cl_io_read_ahead(env, io, page_idx, &ra);
364 if (rc < 0)
365 break;
366
367 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
368 page_idx, ra.cra_end, ra.cra_rpc_size);
369 LASSERTF(ra.cra_end >= page_idx,
370 "object: %p, indcies %lu / %lu\n",
371 io->ci_obj, ra.cra_end, page_idx);
372
373
374
375
376 if (ras->ras_rpc_size > ra.cra_rpc_size &&
377 ra.cra_rpc_size > 0)
378 ras->ras_rpc_size = ra.cra_rpc_size;
379
380 end = ras_align(ras, ria->ria_end + 1, NULL);
381 if (end > 0 && !ria->ria_eof)
382 ria->ria_end = end - 1;
383 if (ria->ria_end < ria->ria_end_min)
384 ria->ria_end = ria->ria_end_min;
385 if (ria->ria_end > ra.cra_end)
386 ria->ria_end = ra.cra_end;
387 }
388
389
390 rc = ll_read_ahead_page(env, io, queue, page_idx);
391 if (rc < 0)
392 break;
393
394 ra_end = page_idx;
395 if (!rc)
396 ria->ria_reserved--;
397 } else if (stride_ria) {
398
399
400
401
402 pgoff_t offset;
403
404
405
406
407 LASSERTF(page_idx >= ria->ria_stoff, "Invalid page_idx %lu rs %lu re %lu ro %lu rl %lu rp %lu\n",
408 page_idx,
409 ria->ria_start, ria->ria_end, ria->ria_stoff,
410 ria->ria_length, ria->ria_pages);
411 offset = page_idx - ria->ria_stoff;
412 offset = offset % (ria->ria_length);
413 if (offset > ria->ria_pages) {
414 page_idx += ria->ria_length - offset;
415 CDEBUG(D_READA, "i %lu skip %lu\n", page_idx,
416 ria->ria_length - offset);
417 continue;
418 }
419 }
420 }
421 cl_read_ahead_release(env, &ra);
422
423 return ra_end;
424}
425
426static int ll_readahead(const struct lu_env *env, struct cl_io *io,
427 struct cl_page_list *queue,
428 struct ll_readahead_state *ras, bool hit)
429{
430 struct vvp_io *vio = vvp_env_io(env);
431 struct ll_thread_info *lti = ll_env_info(env);
432 struct cl_attr *attr = vvp_env_thread_attr(env);
433 unsigned long len, mlen = 0;
434 pgoff_t ra_end, start = 0, end = 0;
435 struct inode *inode;
436 struct ra_io_arg *ria = <i->lti_ria;
437 struct cl_object *clob;
438 int ret = 0;
439 __u64 kms;
440
441 clob = io->ci_obj;
442 inode = vvp_object_inode(clob);
443
444 memset(ria, 0, sizeof(*ria));
445
446 cl_object_attr_lock(clob);
447 ret = cl_object_attr_get(env, clob, attr);
448 cl_object_attr_unlock(clob);
449
450 if (ret != 0)
451 return ret;
452 kms = attr->cat_kms;
453 if (kms == 0) {
454 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
455 return 0;
456 }
457
458 spin_lock(&ras->ras_lock);
459
460
461
462
463
464
465
466
467 if (stride_io_mode(ras))
468 start = max(ras->ras_next_readahead, ras->ras_stride_offset);
469 else
470 start = ras->ras_next_readahead;
471
472 if (ras->ras_window_len > 0)
473 end = ras->ras_window_start + ras->ras_window_len - 1;
474
475
476 if (vio->vui_ra_valid &&
477 end < vio->vui_ra_start + vio->vui_ra_count - 1)
478 end = vio->vui_ra_start + vio->vui_ra_count - 1;
479
480 if (end) {
481 unsigned long end_index;
482
483
484 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
485 if (end_index <= end) {
486 end = end_index;
487 ria->ria_eof = true;
488 }
489
490 ras->ras_next_readahead = max(end, end + 1);
491 RAS_CDEBUG(ras);
492 }
493 ria->ria_start = start;
494 ria->ria_end = end;
495
496 if (stride_io_mode(ras)) {
497 ria->ria_stoff = ras->ras_stride_offset;
498 ria->ria_length = ras->ras_stride_length;
499 ria->ria_pages = ras->ras_stride_pages;
500 }
501 spin_unlock(&ras->ras_lock);
502
503 if (end == 0) {
504 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
505 return 0;
506 }
507 len = ria_page_count(ria);
508 if (len == 0) {
509 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
510 return 0;
511 }
512
513 CDEBUG(D_READA, DFID ": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
514 PFID(lu_object_fid(&clob->co_lu)),
515 ria->ria_start, ria->ria_end,
516 vio->vui_ra_valid ? vio->vui_ra_start : 0,
517 vio->vui_ra_valid ? vio->vui_ra_count : 0,
518 hit);
519
520
521 if (!hit && vio->vui_ra_valid &&
522 vio->vui_ra_start + vio->vui_ra_count > ria->ria_start) {
523 unsigned long remainder;
524
525
526 mlen = vio->vui_ra_start + vio->vui_ra_count - ria->ria_start;
527
528 ras_align(ras, ria->ria_start, &remainder);
529 mlen = min(mlen, ras->ras_rpc_size - remainder);
530 ria->ria_end_min = ria->ria_start + mlen;
531 }
532
533 ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
534 if (ria->ria_reserved < len)
535 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
536
537 CDEBUG(D_READA, "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 ra_end = ll_read_ahead_pages(env, io, queue, ras, ria);
543
544 if (ria->ria_reserved)
545 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
546
547 if (ra_end == end && ra_end == (kms >> PAGE_SHIFT))
548 ll_ra_stats_inc(inode, RA_STAT_EOF);
549
550
551
552
553
554
555
556 CDEBUG(D_READA, "ra_end = %lu end = %lu stride end = %lu pages = %d\n",
557 ra_end, end, ria->ria_end, ret);
558
559 if (ra_end > 0 && ra_end != end) {
560 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
561 spin_lock(&ras->ras_lock);
562 if (ra_end <= ras->ras_next_readahead &&
563 index_in_window(ra_end, ras->ras_window_start, 0,
564 ras->ras_window_len)) {
565 ras->ras_next_readahead = ra_end + 1;
566 RAS_CDEBUG(ras);
567 }
568 spin_unlock(&ras->ras_lock);
569 }
570
571 return ret;
572}
573
574static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
575 unsigned long index)
576{
577 ras->ras_window_start = ras_align(ras, index, NULL);
578}
579
580
581static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
582 unsigned long index)
583{
584 ras->ras_last_readpage = index;
585 ras->ras_consecutive_requests = 0;
586 ras->ras_consecutive_pages = 0;
587 ras->ras_window_len = 0;
588 ras_set_start(inode, ras, index);
589 ras->ras_next_readahead = max(ras->ras_window_start, index + 1);
590
591 RAS_CDEBUG(ras);
592}
593
594
595static void ras_stride_reset(struct ll_readahead_state *ras)
596{
597 ras->ras_consecutive_stride_requests = 0;
598 ras->ras_stride_length = 0;
599 ras->ras_stride_pages = 0;
600 RAS_CDEBUG(ras);
601}
602
603void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
604{
605 spin_lock_init(&ras->ras_lock);
606 ras->ras_rpc_size = PTLRPC_MAX_BRW_PAGES;
607 ras_reset(inode, ras, 0);
608 ras->ras_requests = 0;
609}
610
611
612
613
614
615static int index_in_stride_window(struct ll_readahead_state *ras,
616 unsigned long index)
617{
618 unsigned long stride_gap;
619
620 if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
621 ras->ras_stride_pages == ras->ras_stride_length)
622 return 0;
623
624 stride_gap = index - ras->ras_last_readpage - 1;
625
626
627 if (stride_gap == 0)
628 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
629
630
631 return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
632 ras->ras_consecutive_pages == ras->ras_stride_pages;
633}
634
635static void ras_update_stride_detector(struct ll_readahead_state *ras,
636 unsigned long index)
637{
638 unsigned long stride_gap = index - ras->ras_last_readpage - 1;
639
640 if ((stride_gap != 0 || ras->ras_consecutive_stride_requests == 0) &&
641 !stride_io_mode(ras)) {
642 ras->ras_stride_pages = ras->ras_consecutive_pages;
643 ras->ras_stride_length = ras->ras_consecutive_pages +
644 stride_gap;
645 }
646 LASSERT(ras->ras_request_index == 0);
647 LASSERT(ras->ras_consecutive_stride_requests == 0);
648
649 if (index <= ras->ras_last_readpage) {
650
651 ras_stride_reset(ras);
652 return;
653 }
654
655 ras->ras_stride_pages = ras->ras_consecutive_pages;
656 ras->ras_stride_length = stride_gap + ras->ras_consecutive_pages;
657
658 RAS_CDEBUG(ras);
659}
660
661
662
663
664static void ras_stride_increase_window(struct ll_readahead_state *ras,
665 struct ll_ra_info *ra,
666 unsigned long inc_len)
667{
668 unsigned long left, step, window_len;
669 unsigned long stride_len;
670
671 LASSERT(ras->ras_stride_length > 0);
672 LASSERTF(ras->ras_window_start + ras->ras_window_len
673 >= ras->ras_stride_offset, "window_start %lu, window_len %lu stride_offset %lu\n",
674 ras->ras_window_start,
675 ras->ras_window_len, ras->ras_stride_offset);
676
677 stride_len = ras->ras_window_start + ras->ras_window_len -
678 ras->ras_stride_offset;
679
680 left = stride_len % ras->ras_stride_length;
681 window_len = ras->ras_window_len - left;
682
683 if (left < ras->ras_stride_pages)
684 left += inc_len;
685 else
686 left = ras->ras_stride_pages + inc_len;
687
688 LASSERT(ras->ras_stride_pages != 0);
689
690 step = left / ras->ras_stride_pages;
691 left %= ras->ras_stride_pages;
692
693 window_len += step * ras->ras_stride_length + left;
694
695 if (stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
696 ras->ras_stride_pages, ras->ras_stride_offset,
697 window_len) <= ra->ra_max_pages_per_file)
698 ras->ras_window_len = window_len;
699
700 RAS_CDEBUG(ras);
701}
702
703static void ras_increase_window(struct inode *inode,
704 struct ll_readahead_state *ras,
705 struct ll_ra_info *ra)
706{
707
708
709
710
711 if (stride_io_mode(ras)) {
712 ras_stride_increase_window(ras, ra, ras->ras_rpc_size);
713 } else {
714 unsigned long wlen;
715
716 wlen = min(ras->ras_window_len + ras->ras_rpc_size,
717 ra->ra_max_pages_per_file);
718 ras->ras_window_len = ras_align(ras, wlen, NULL);
719 }
720}
721
722static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
723 struct ll_readahead_state *ras, unsigned long index,
724 enum ras_update_flags flags)
725{
726 struct ll_ra_info *ra = &sbi->ll_ra_info;
727 int zero = 0, stride_detect = 0, ra_miss = 0;
728 bool hit = flags & LL_RAS_HIT;
729
730 spin_lock(&ras->ras_lock);
731
732 if (!hit)
733 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
734 PFID(ll_inode2fid(inode)), index);
735
736 ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
737
738
739
740
741
742
743
744 if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
745 zero = 1;
746 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
747 } else if (!hit && ras->ras_window_len &&
748 index < ras->ras_next_readahead &&
749 index_in_window(index, ras->ras_window_start, 0,
750 ras->ras_window_len)) {
751 ra_miss = 1;
752 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
753 }
754
755
756
757
758
759
760
761
762 if (ras->ras_requests >= 2 && !ras->ras_request_index) {
763 __u64 kms_pages;
764
765 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
766 PAGE_SHIFT;
767
768 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
769 ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
770
771 if (kms_pages &&
772 kms_pages <= ra->ra_max_read_ahead_whole_pages) {
773 ras->ras_window_start = 0;
774 ras->ras_next_readahead = index + 1;
775 ras->ras_window_len = min(ra->ra_max_pages_per_file,
776 ra->ra_max_read_ahead_whole_pages);
777 goto out_unlock;
778 }
779 }
780 if (zero) {
781
782 if (!index_in_stride_window(ras, index)) {
783 if (ras->ras_consecutive_stride_requests == 0 &&
784 ras->ras_request_index == 0) {
785 ras_update_stride_detector(ras, index);
786 ras->ras_consecutive_stride_requests++;
787 } else {
788 ras_stride_reset(ras);
789 }
790 ras_reset(inode, ras, index);
791 ras->ras_consecutive_pages++;
792 goto out_unlock;
793 } else {
794 ras->ras_consecutive_pages = 0;
795 ras->ras_consecutive_requests = 0;
796 if (++ras->ras_consecutive_stride_requests > 1)
797 stride_detect = 1;
798 RAS_CDEBUG(ras);
799 }
800 } else {
801 if (ra_miss) {
802 if (index_in_stride_window(ras, index) &&
803 stride_io_mode(ras)) {
804 if (index != ras->ras_last_readpage + 1)
805 ras->ras_consecutive_pages = 0;
806 ras_reset(inode, ras, index);
807
808
809
810
811
812
813
814
815 if (ras->ras_window_start <
816 ras->ras_stride_offset)
817 ras_stride_reset(ras);
818 RAS_CDEBUG(ras);
819 } else {
820
821
822
823 ras_reset(inode, ras, index);
824 ras->ras_consecutive_pages++;
825 ras_stride_reset(ras);
826 goto out_unlock;
827 }
828 } else if (stride_io_mode(ras)) {
829
830
831
832
833 if (!index_in_stride_window(ras, index)) {
834
835 ras_stride_reset(ras);
836 ras->ras_window_len = 0;
837 ras->ras_next_readahead = index;
838 }
839 }
840 }
841 ras->ras_consecutive_pages++;
842 ras->ras_last_readpage = index;
843 ras_set_start(inode, ras, index);
844
845 if (stride_io_mode(ras)) {
846
847
848
849
850 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
851 ras->ras_window_start = max(ras->ras_stride_offset,
852 ras->ras_window_start);
853 } else {
854 if (ras->ras_next_readahead < ras->ras_window_start)
855 ras->ras_next_readahead = ras->ras_window_start;
856 if (!hit)
857 ras->ras_next_readahead = index + 1;
858 }
859 RAS_CDEBUG(ras);
860
861
862
863
864 if (ras->ras_consecutive_pages >= 4 && flags & LL_RAS_MMAP) {
865 ras_increase_window(inode, ras, ra);
866
867
868
869
870 ras->ras_consecutive_pages = 0;
871 goto out_unlock;
872 }
873
874
875 if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
876
877
878
879
880 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
881 ras->ras_stride_offset = index;
882 ras->ras_window_start = max(index, ras->ras_window_start);
883 }
884
885
886
887
888 if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
889 !ras->ras_request_index)
890 ras_increase_window(inode, ras, ra);
891out_unlock:
892 RAS_CDEBUG(ras);
893 ras->ras_request_index++;
894 spin_unlock(&ras->ras_lock);
895}
896
897int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
898{
899 struct inode *inode = vmpage->mapping->host;
900 struct ll_inode_info *lli = ll_i2info(inode);
901 struct lu_env *env;
902 struct cl_io *io;
903 struct cl_page *page;
904 struct cl_object *clob;
905 bool redirtied = false;
906 bool unlocked = false;
907 int result;
908 u16 refcheck;
909
910 LASSERT(PageLocked(vmpage));
911 LASSERT(!PageWriteback(vmpage));
912
913 LASSERT(ll_i2dtexp(inode));
914
915 env = cl_env_get(&refcheck);
916 if (IS_ERR(env)) {
917 result = PTR_ERR(env);
918 goto out;
919 }
920
921 clob = ll_i2info(inode)->lli_clob;
922 LASSERT(clob);
923
924 io = vvp_env_thread_io(env);
925 io->ci_obj = clob;
926 io->ci_ignore_layout = 1;
927 result = cl_io_init(env, io, CIT_MISC, clob);
928 if (result == 0) {
929 page = cl_page_find(env, clob, vmpage->index,
930 vmpage, CPT_CACHEABLE);
931 if (!IS_ERR(page)) {
932 lu_ref_add(&page->cp_reference, "writepage",
933 current);
934 cl_page_assume(env, io, page);
935 result = cl_page_flush(env, io, page);
936 if (result != 0) {
937
938
939
940
941
942 if (!PageError(vmpage)) {
943 redirty_page_for_writepage(wbc, vmpage);
944 result = 0;
945 redirtied = true;
946 }
947 }
948 cl_page_disown(env, io, page);
949 unlocked = true;
950 lu_ref_del(&page->cp_reference,
951 "writepage", current);
952 cl_page_put(env, page);
953 } else {
954 result = PTR_ERR(page);
955 }
956 }
957 cl_io_fini(env, io);
958
959 if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
960 loff_t offset = cl_offset(clob, vmpage->index);
961
962
963
964
965
966
967 result = cl_sync_file_range(inode, offset,
968 offset + PAGE_SIZE - 1,
969 CL_FSYNC_LOCAL, 1);
970 if (result > 0) {
971
972
973
974
975 wbc->nr_to_write -= result - 1;
976 result = 0;
977 }
978 }
979
980 cl_env_put(env, &refcheck);
981 goto out;
982
983out:
984 if (result < 0) {
985 if (!lli->lli_async_rc)
986 lli->lli_async_rc = result;
987 SetPageError(vmpage);
988 if (!unlocked)
989 unlock_page(vmpage);
990 }
991 return result;
992}
993
994int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
995{
996 struct inode *inode = mapping->host;
997 struct ll_sb_info *sbi = ll_i2sbi(inode);
998 loff_t start;
999 loff_t end;
1000 enum cl_fsync_mode mode;
1001 int range_whole = 0;
1002 int result;
1003 int ignore_layout = 0;
1004
1005 if (wbc->range_cyclic) {
1006 start = mapping->writeback_index << PAGE_SHIFT;
1007 end = OBD_OBJECT_EOF;
1008 } else {
1009 start = wbc->range_start;
1010 end = wbc->range_end;
1011 if (end == LLONG_MAX) {
1012 end = OBD_OBJECT_EOF;
1013 range_whole = start == 0;
1014 }
1015 }
1016
1017 mode = CL_FSYNC_NONE;
1018 if (wbc->sync_mode == WB_SYNC_ALL)
1019 mode = CL_FSYNC_LOCAL;
1020
1021 if (sbi->ll_umounting)
1022
1023
1024
1025
1026 ignore_layout = 1;
1027
1028 if (!ll_i2info(inode)->lli_clob)
1029 return 0;
1030
1031 result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1032 if (result > 0) {
1033 wbc->nr_to_write -= result;
1034 result = 0;
1035 }
1036
1037 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1038 if (end == OBD_OBJECT_EOF)
1039 mapping->writeback_index = 0;
1040 else
1041 mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1042 }
1043 return result;
1044}
1045
1046struct ll_cl_context *ll_cl_find(struct file *file)
1047{
1048 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1049 struct ll_cl_context *lcc;
1050 struct ll_cl_context *found = NULL;
1051
1052 read_lock(&fd->fd_lock);
1053 list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1054 if (lcc->lcc_cookie == current) {
1055 found = lcc;
1056 break;
1057 }
1058 }
1059 read_unlock(&fd->fd_lock);
1060
1061 return found;
1062}
1063
1064void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io)
1065{
1066 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1067 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1068
1069 memset(lcc, 0, sizeof(*lcc));
1070 INIT_LIST_HEAD(&lcc->lcc_list);
1071 lcc->lcc_cookie = current;
1072 lcc->lcc_env = env;
1073 lcc->lcc_io = io;
1074
1075 write_lock(&fd->fd_lock);
1076 list_add(&lcc->lcc_list, &fd->fd_lccs);
1077 write_unlock(&fd->fd_lock);
1078}
1079
1080void ll_cl_remove(struct file *file, const struct lu_env *env)
1081{
1082 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1083 struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1084
1085 write_lock(&fd->fd_lock);
1086 list_del_init(&lcc->lcc_list);
1087 write_unlock(&fd->fd_lock);
1088}
1089
1090static int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1091 struct cl_page *page)
1092{
1093 struct inode *inode = vvp_object_inode(page->cp_obj);
1094 struct ll_file_data *fd = vvp_env_io(env)->vui_fd;
1095 struct ll_readahead_state *ras = &fd->fd_ras;
1096 struct cl_2queue *queue = &io->ci_queue;
1097 struct ll_sb_info *sbi = ll_i2sbi(inode);
1098 struct vvp_page *vpg;
1099 bool uptodate;
1100 int rc = 0;
1101
1102 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1103 uptodate = vpg->vpg_defer_uptodate;
1104
1105 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1106 sbi->ll_ra_info.ra_max_pages > 0) {
1107 struct vvp_io *vio = vvp_env_io(env);
1108 enum ras_update_flags flags = 0;
1109
1110 if (uptodate)
1111 flags |= LL_RAS_HIT;
1112 if (!vio->vui_ra_valid)
1113 flags |= LL_RAS_MMAP;
1114 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1115 }
1116
1117 cl_2queue_init(queue);
1118 if (uptodate) {
1119 vpg->vpg_ra_used = 1;
1120 cl_page_export(env, page, 1);
1121 cl_page_disown(env, io, page);
1122 } else {
1123 cl_page_list_add(&queue->c2_qin, page);
1124 }
1125
1126 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1127 sbi->ll_ra_info.ra_max_pages > 0) {
1128 int rc2;
1129
1130 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1131 uptodate);
1132 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1133 PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1134 }
1135
1136 if (queue->c2_qin.pl_nr > 0)
1137 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1138
1139
1140
1141
1142 cl_page_list_disown(env, io, &queue->c2_qin);
1143 cl_2queue_fini(env, queue);
1144
1145 return rc;
1146}
1147
1148int ll_readpage(struct file *file, struct page *vmpage)
1149{
1150 struct cl_object *clob = ll_i2info(file_inode(file))->lli_clob;
1151 struct ll_cl_context *lcc;
1152 const struct lu_env *env;
1153 struct cl_io *io;
1154 struct cl_page *page;
1155 int result;
1156
1157 lcc = ll_cl_find(file);
1158 if (!lcc) {
1159 unlock_page(vmpage);
1160 return -EIO;
1161 }
1162
1163 env = lcc->lcc_env;
1164 io = lcc->lcc_io;
1165 LASSERT(io->ci_state == CIS_IO_GOING);
1166 page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1167 if (!IS_ERR(page)) {
1168 LASSERT(page->cp_type == CPT_CACHEABLE);
1169 if (likely(!PageUptodate(vmpage))) {
1170 cl_page_assume(env, io, page);
1171 result = ll_io_read_page(env, io, page);
1172 } else {
1173
1174 unlock_page(vmpage);
1175 result = 0;
1176 }
1177 cl_page_put(env, page);
1178 } else {
1179 unlock_page(vmpage);
1180 result = PTR_ERR(page);
1181 }
1182 return result;
1183}
1184
1185int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1186 struct cl_page *page, enum cl_req_type crt)
1187{
1188 struct cl_2queue *queue;
1189 int result;
1190
1191 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1192
1193 queue = &io->ci_queue;
1194 cl_2queue_init_page(queue, page);
1195
1196 result = cl_io_submit_sync(env, io, crt, queue, 0);
1197 LASSERT(cl_page_is_owned(page, io));
1198
1199 if (crt == CRT_READ)
1200
1201
1202
1203
1204 cl_page_list_disown(env, io, &queue->c2_qin);
1205 cl_2queue_fini(env, queue);
1206
1207 return result;
1208}
1209