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