Whamcloud - gitweb
Branch HEAD
[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  2008 Sun Microsystems, Inc. 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         loff_t new_size;
80         ENTRY;
81         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) to %Lu=%#Lx\n",inode->i_ino,
82                inode->i_generation, inode, i_size_read(inode),
83                i_size_read(inode));
84
85         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_TRUNC, 1);
86         if (lli->lli_size_sem_owner != cfs_current()) {
87                 EXIT;
88                 return;
89         }
90
91         if (!lli->lli_smd) {
92                 CDEBUG(D_INODE, "truncate on inode %lu with no objects\n",
93                        inode->i_ino);
94                 GOTO(out_unlock, 0);
95         }
96         LASSERT_SEM_LOCKED(&lli->lli_size_sem);
97
98         if (unlikely((ll_i2sbi(inode)->ll_flags & LL_SBI_CHECKSUM) &&
99                      (i_size_read(inode) & ~CFS_PAGE_MASK))) {
100                 /* If the truncate leaves a partial page, update its checksum */
101                 struct page *page = find_get_page(inode->i_mapping,
102                                                   i_size_read(inode) >>
103                                                   CFS_PAGE_SHIFT);
104                 if (page != NULL) {
105 #if 0 /* XXX */
106                         struct ll_async_page *llap = llap_cast_private(page);
107                         if (llap != NULL) {
108                                 char *kaddr = kmap_atomic(page, KM_USER0);
109                                 llap->llap_checksum =
110                                         init_checksum(OSC_DEFAULT_CKSUM);
111                                 llap->llap_checksum =
112                                         compute_checksum(llap->llap_checksum,
113                                                          kaddr, CFS_PAGE_SIZE,
114                                                          OSC_DEFAULT_CKSUM);
115                                 kunmap_atomic(kaddr, KM_USER0);
116                         }
117                         page_cache_release(page);
118 #endif
119                 }
120         }
121
122         new_size = i_size_read(inode);
123         ll_inode_size_unlock(inode, 0);
124
125         EXIT;
126         return;
127
128  out_unlock:
129         ll_inode_size_unlock(inode, 0);
130 } /* ll_truncate */
131
132 /**
133  * Initializes common cl-data at the typical address_space operation entry
134  * point.
135  */
136 static int ll_cl_init(struct file *file, struct page *vmpage,
137                       struct lu_env **env,
138                       struct cl_io **io, struct cl_page **page, int *refcheck)
139 {
140         struct lu_env    *_env;
141         struct cl_io     *_io;
142         struct cl_page   *_page;
143         struct cl_object *clob;
144
145         int result;
146
147         *env  = NULL;
148         *io   = NULL;
149         *page = NULL;
150
151         clob = ll_i2info(vmpage->mapping->host)->lli_clob;
152         LASSERT(clob != NULL);
153
154         _env = cl_env_get(refcheck);
155         if (!IS_ERR(env)) {
156                 struct ccc_io *cio = ccc_env_io(_env);
157
158                 *env = _env;
159                 *io  = _io = cio->cui_cl.cis_io;
160                 if (_io != NULL) {
161                         LASSERT(_io->ci_state == CIS_IO_GOING);
162                         LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
163                         _page = cl_page_find(_env, clob, vmpage->index, vmpage,
164                                              CPT_CACHEABLE);
165                         if (!IS_ERR(_page)) {
166                                 *page = _page;
167                                 lu_ref_add(&_page->cp_reference, "cl_io", _io);
168                                 result = 0;
169                         } else
170                                 result = PTR_ERR(_page);
171                 } else
172                         /*
173                          * This is for a case where operation can be called
174                          * either with or without cl_io created by the upper
175                          * layer (e.g., ->prepare_write() called directly from
176                          * loop-back driver).
177                          */
178                         result = -EALREADY;
179         } else
180                 result = PTR_ERR(_env);
181         CDEBUG(D_VFSTRACE, "%lu@"DFID" -> %i %p %p %p\n",
182                vmpage->index, PFID(lu_object_fid(&clob->co_lu)), result,
183                *env, *io, *page);
184         return result;
185 }
186
187 /**
188  * Finalizes cl-data before exiting typical address_space operation. Dual to
189  * ll_cl_init().
190  */
191 static void ll_cl_fini(struct lu_env *env,
192                        struct cl_io *io, struct cl_page *page, int *refcheck)
193 {
194         if (page != NULL) {
195                 lu_ref_del(&page->cp_reference, "cl_io", io);
196                 cl_page_put(env, page);
197         }
198         if (env != NULL) {
199                 struct vvp_io *vio;
200
201                 vio = vvp_env_io(env);
202                 LASSERT(vio->cui_oneshot >= 0);
203                 if (vio->cui_oneshot > 0) {
204                         if (--vio->cui_oneshot == 0) {
205                                 cl_io_end(env, io);
206                                 cl_io_unlock(env, io);
207                                 cl_io_iter_fini(env, io);
208                                 cl_io_fini(env, io);
209                                 /* to trigger assertion above, if ll_cl_fini()
210                                  * is called against freed io. */
211                                 vio->cui_oneshot = -1;
212                         }
213                         /* additional reference on env was acquired by io,
214                          * disable refcheck */
215                         refcheck = NULL;
216                 }
217                 cl_env_put(env, refcheck);
218         } else
219                 LASSERT(io == NULL);
220 }
221
222 /**
223  * Initializes one-shot cl_io for the case when loop driver calls
224  * ->{prepare,commit}_write() methods directly.
225  */
226 static int ll_prepare_loop(struct lu_env *env, struct cl_io *io,
227                            struct file *file, struct page *vmpage,
228                            unsigned from, unsigned to)
229 {
230         struct vvp_io *vio;
231         struct ccc_io *cio;
232         int result;
233         loff_t pos;
234
235         vio = vvp_env_io(env);
236         cio = ccc_env_io(env);
237         ll_io_init(io, file, 1);
238         pos = (vmpage->index << CFS_PAGE_SHIFT) + from;
239         /*
240          * Create IO and quickly drive it through CIS_{INIT,IT_STARTED,LOCKED}
241          * states. DLM locks are not taken for vio->cui_oneshot IO---we cannot
242          * take DLM locks here, because page is already locked. With new
243          * ->write_{being,end}() address_space operations lustre might be
244          * luckier.
245          */
246         result = cl_io_rw_init(env, io, CIT_WRITE, pos, from - to);
247         if (result == 0) {
248                 cio->cui_fd = LUSTRE_FPRIVATE(file);
249                 vio->cui_oneshot = 1;
250                 result = cl_io_iter_init(env, io);
251                 if (result == 0) {
252                         result = cl_io_lock(env, io);
253                         if (result == 0)
254                                 result = cl_io_start(env, io);
255                 }
256         } else
257                 result = io->ci_result;
258         return result;
259 }
260
261 /**
262  * ->prepare_write() address space operation called by generic_file_write()
263  * for every page during write.
264  */
265 int ll_prepare_write(struct file *file, struct page *vmpage, unsigned from,
266                      unsigned to)
267 {
268         struct lu_env    *env;
269         struct cl_io     *io;
270         struct cl_page   *page;
271         int result;
272         int refcheck;
273         ENTRY;
274
275         result = ll_cl_init(file, vmpage, &env, &io, &page, &refcheck);
276         /*
277          * Loop-back driver calls ->prepare_write() and ->sendfile() methods
278          * directly, bypassing file system ->write() operation, so cl_io has
279          * to be created here.
280          */
281         if (result == -EALREADY) {
282                 io = &ccc_env_info(env)->cti_io;
283                 result = ll_prepare_loop(env, io, file, vmpage, from, to);
284                 if (result == 0) {
285                         result = ll_cl_init(file, vmpage,
286                                             &env, &io, &page, &refcheck);
287                         cl_env_put(env, NULL);
288                 }
289         }
290         if (result == 0) {
291                 cl_page_assume(env, io, page);
292                 result = cl_io_prepare_write(env, io, page, from, to);
293                 if (result == 0) {
294                         struct vvp_io *vio;
295
296                         /*
297                          * Add a reference, so that page is not evicted from
298                          * the cache until ->commit_write() is called.
299                          */
300                         cl_page_get(page);
301                         lu_ref_add(&page->cp_reference, "prepare_write",
302                                    cfs_current());
303                         vio = vvp_env_io(env);
304                         if (vio->cui_oneshot > 0)
305                                 vio->cui_oneshot++;
306                 } else
307                         cl_page_unassume(env, io, page);
308         }
309         ll_cl_fini(env, io, page, &refcheck);
310         RETURN(result);
311 }
312
313 int ll_commit_write(struct file *file, struct page *vmpage, unsigned from,
314                     unsigned to)
315 {
316         struct lu_env    *env;
317         struct cl_io     *io;
318         struct cl_page   *page;
319         int result;
320         int refcheck;
321         ENTRY;
322
323         result = ll_cl_init(file, vmpage, &env, &io, &page, &refcheck);
324         LASSERT(result != -EALREADY);
325         if (result == 0) {
326                 LASSERT(cl_page_is_owned(page, io));
327                 result = cl_io_commit_write(env, io, page, from, to);
328                 if (cl_page_is_owned(page, io))
329                         cl_page_unassume(env, io, page);
330                 /*
331                  * Release reference acquired by cl_io_prepare_write().
332                  */
333                 lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
334                 cl_page_put(env, page);
335         }
336         ll_cl_fini(env, io, page, &refcheck);
337         RETURN(result);
338 }
339
340 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
341 {
342         __u64 opc;
343
344         opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
345         return ll_osscapa_get(inode, opc);
346 }
347
348 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
349
350 /* WARNING: This algorithm is used to reduce the contention on
351  * sbi->ll_lock. It should work well if the ra_max_pages is much
352  * greater than the single file's read-ahead window.
353  *
354  * TODO: There may exist a `global sync problem' in this implementation.
355  * Considering the global ra window is 100M, and each file's ra window is 10M,
356  * there are over 10 files trying to get its ra budget and reach
357  * ll_ra_count_get at the exactly same time. All of them will get a zero ra
358  * window, although the global window is 100M. -jay
359  */
360 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
361 {
362         struct ll_ra_info *ra = &sbi->ll_ra_info;
363         unsigned long ret;
364         ENTRY;
365
366         ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), len);
367         if ((int)ret < 0)
368                 GOTO(out, ret = 0);
369
370         if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
371                 atomic_sub(ret, &ra->ra_cur_pages);
372                 ret = 0;
373         }
374 out:
375         RETURN(ret);
376 }
377
378 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
379 {
380         struct ll_ra_info *ra = &sbi->ll_ra_info;
381         atomic_sub(len, &ra->ra_cur_pages);
382 }
383
384 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
385 {
386         LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
387         lprocfs_counter_incr(sbi->ll_ra_stats, which);
388 }
389
390 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
391 {
392         struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
393         ll_ra_stats_inc_sbi(sbi, which);
394 }
395
396 #define RAS_CDEBUG(ras) \
397         CDEBUG(D_READA,                                                      \
398                "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu"    \
399                "csr %lu sf %lu sp %lu sl %lu \n",                            \
400                ras->ras_last_readpage, ras->ras_consecutive_requests,        \
401                ras->ras_consecutive_pages, ras->ras_window_start,            \
402                ras->ras_window_len, ras->ras_next_readahead,                 \
403                ras->ras_requests, ras->ras_request_index,                    \
404                ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
405                ras->ras_stride_pages, ras->ras_stride_length)
406
407 static int index_in_window(unsigned long index, unsigned long point,
408                            unsigned long before, unsigned long after)
409 {
410         unsigned long start = point - before, end = point + after;
411
412         if (start > point)
413                start = 0;
414         if (end < point)
415                end = ~0;
416
417         return start <= index && index <= end;
418 }
419
420 static struct ll_readahead_state *ll_ras_get(struct file *f)
421 {
422         struct ll_file_data       *fd;
423
424         fd = LUSTRE_FPRIVATE(f);
425         return &fd->fd_ras;
426 }
427
428 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
429 {
430         struct ll_readahead_state *ras;
431
432         ras = ll_ras_get(f);
433
434         spin_lock(&ras->ras_lock);
435         ras->ras_requests++;
436         ras->ras_request_index = 0;
437         ras->ras_consecutive_requests++;
438         rar->lrr_reader = current;
439
440         list_add(&rar->lrr_linkage, &ras->ras_read_beads);
441         spin_unlock(&ras->ras_lock);
442 }
443
444 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
445 {
446         struct ll_readahead_state *ras;
447
448         ras = ll_ras_get(f);
449
450         spin_lock(&ras->ras_lock);
451         list_del_init(&rar->lrr_linkage);
452         spin_unlock(&ras->ras_lock);
453 }
454
455 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
456 {
457         struct ll_ra_read *scan;
458
459         list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
460                 if (scan->lrr_reader == current)
461                         return scan;
462         }
463         return NULL;
464 }
465
466 struct ll_ra_read *ll_ra_read_get(struct file *f)
467 {
468         struct ll_readahead_state *ras;
469         struct ll_ra_read         *bead;
470
471         ras = ll_ras_get(f);
472
473         spin_lock(&ras->ras_lock);
474         bead = ll_ra_read_get_locked(ras);
475         spin_unlock(&ras->ras_lock);
476         return bead;
477 }
478
479 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
480                               struct cl_page_list *queue, struct cl_page *page,
481                               struct page *vmpage)
482 {
483         struct ccc_page *cp;
484         int              rc;
485
486         ENTRY;
487
488         rc = 0;
489         cl_page_assume(env, io, page);
490         lu_ref_add(&page->cp_reference, "ra", cfs_current());
491         cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
492         if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
493                 rc = cl_page_is_under_lock(env, io, page);
494                 if (rc == -EBUSY) {
495                         cp->cpg_defer_uptodate = 1;
496                         cp->cpg_ra_used = 0;
497                         cl_page_list_add(queue, page);
498                         rc = 1;
499                 } else {
500                         cl_page_delete(env, page);
501                         rc = -ENOLCK;
502                 }
503         } else
504                 /* skip completed pages */
505                 cl_page_unassume(env, io, page);
506         lu_ref_del(&page->cp_reference, "ra", cfs_current());
507         cl_page_put(env, page);
508         RETURN(rc);
509 }
510
511 /**
512  * Initiates read-ahead of a page with given index.
513  *
514  * \retval     +ve: page was added to \a queue.
515  *
516  * \retval -ENOLCK: there is no extent lock for this part of a file, stop
517  *                  read-ahead.
518  *
519  * \retval  -ve, 0: page wasn't added to \a queue for other reason.
520  */
521 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
522                               struct cl_page_list *queue,
523                               int index, struct address_space *mapping)
524 {
525         struct page      *vmpage;
526         struct cl_object *clob  = ll_i2info(mapping->host)->lli_clob;
527         struct cl_page   *page;
528         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
529         unsigned int      gfp_mask;
530         int               rc    = 0;
531         const char       *msg   = NULL;
532
533         ENTRY;
534
535         gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
536 #ifdef __GFP_NOWARN
537         gfp_mask |= __GFP_NOWARN;
538 #endif
539         vmpage = grab_cache_page_nowait_gfp(mapping, index, gfp_mask);
540         if (vmpage != NULL) {
541                 /* Check if vmpage was truncated or reclaimed */
542                 if (vmpage->mapping == mapping) {
543                         page = cl_page_find(env, clob, vmpage->index,
544                                             vmpage, CPT_CACHEABLE);
545                         if (!IS_ERR(page)) {
546                                 rc = cl_read_ahead_page(env, io, queue,
547                                                         page, vmpage);
548                                 if (rc == -ENOLCK) {
549                                         which = RA_STAT_FAILED_MATCH;
550                                         msg   = "lock match failed";
551                                 }
552                         } else {
553                                 which = RA_STAT_FAILED_GRAB_PAGE;
554                                 msg   = "cl_page_find failed";
555                         }
556                 } else {
557                         which = RA_STAT_WRONG_GRAB_PAGE;
558                         msg   = "g_c_p_n returned invalid page";
559                 }
560                 if (rc != 1)
561                         unlock_page(vmpage);
562                 page_cache_release(vmpage);
563         } else {
564                 which = RA_STAT_FAILED_GRAB_PAGE;
565                 msg   = "g_c_p_n failed";
566         }
567         if (msg != NULL) {
568                 ll_ra_stats_inc(mapping, which);
569                 CDEBUG(D_READA, "%s\n", msg);
570         }
571         RETURN(rc);
572 }
573
574 #define RIA_DEBUG(ria)                                                       \
575         CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n",       \
576         ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
577         ria->ria_pages)
578
579 #define RAS_INCREASE_STEP (1024 * 1024 >> CFS_PAGE_SHIFT)
580
581 static inline int stride_io_mode(struct ll_readahead_state *ras)
582 {
583         return ras->ras_consecutive_stride_requests > 1;
584 }
585
586 /* The function calculates how much pages will be read in
587  * [off, off + length], which will be read by stride I/O mode,
588  * stride_offset = st_off, stride_lengh = st_len,
589  * stride_pages = st_pgs
590  */
591 static unsigned long
592 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
593                 unsigned long off, unsigned length)
594 {
595         unsigned long cont_len = st_off > off ?  st_off - off : 0;
596         __u64 stride_len = length + off > st_off ?
597                            length + off + 1 - st_off : 0;
598         unsigned long left, pg_count;
599
600         if (st_len == 0 || length == 0)
601                 return length;
602
603         left = do_div(stride_len, st_len);
604         left = min(left, st_pgs);
605
606         pg_count = left + stride_len * st_pgs + cont_len;
607
608         LASSERT(pg_count >= left);
609
610         CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %u"
611                "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
612
613         return pg_count;
614 }
615
616 static int ria_page_count(struct ra_io_arg *ria)
617 {
618         __u64 length = ria->ria_end >= ria->ria_start ?
619                        ria->ria_end - ria->ria_start + 1 : 0;
620
621         return stride_pg_count(ria->ria_stoff, ria->ria_length,
622                                ria->ria_pages, ria->ria_start,
623                                length);
624 }
625
626 /*Check whether the index is in the defined ra-window */
627 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
628 {
629         /* If ria_length == ria_pages, it means non-stride I/O mode,
630          * idx should always inside read-ahead window in this case
631          * For stride I/O mode, just check whether the idx is inside
632          * the ria_pages. */
633         return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
634                (idx - ria->ria_stoff) % ria->ria_length < ria->ria_pages;
635 }
636
637 static int ll_read_ahead_pages(const struct lu_env *env,
638                                struct cl_io *io, struct cl_page_list *queue,
639                                struct ra_io_arg *ria,
640                                unsigned long *reserved_pages,
641                                struct address_space *mapping,
642                                unsigned long *ra_end)
643 {
644         int rc, count = 0, stride_ria;
645         unsigned long page_idx;
646
647         LASSERT(ria != NULL);
648         RIA_DEBUG(ria);
649
650         stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
651         for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
652                         *reserved_pages > 0; page_idx++) {
653                 if (ras_inside_ra_window(page_idx, ria)) {
654                         /* If the page is inside the read-ahead window*/
655                         rc = ll_read_ahead_page(env, io, queue,
656                                                 page_idx, mapping);
657                         if (rc == 1) {
658                                 (*reserved_pages)--;
659                                 count ++;
660                         } else if (rc == -ENOLCK)
661                                 break;
662                 } else if (stride_ria) {
663                         /* If it is not in the read-ahead window, and it is
664                          * read-ahead mode, then check whether it should skip
665                          * the stride gap */
666                         pgoff_t offset;
667                         /* FIXME: This assertion only is valid when it is for
668                          * forward read-ahead, it will be fixed when backward
669                          * read-ahead is implemented */
670                         LASSERTF(page_idx > ria->ria_stoff, "since %lu in the"
671                                 " gap of ra window,it should bigger than stride"
672                                 " offset %lu \n", page_idx, ria->ria_stoff);
673
674                         offset = page_idx - ria->ria_stoff;
675                         offset = offset % (ria->ria_length);
676                         if (offset > ria->ria_pages) {
677                                 page_idx += ria->ria_length - offset;
678                                 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
679                                        ria->ria_length - offset);
680                                 continue;
681                         }
682                 }
683         }
684         *ra_end = page_idx;
685         return count;
686 }
687
688 int ll_readahead(const struct lu_env *env, struct cl_io *io,
689                  struct ll_readahead_state *ras, struct address_space *mapping,
690                  struct cl_page_list *queue, int flags)
691 {
692         struct vvp_io *vio = vvp_env_io(env);
693         struct vvp_thread_info *vti = vvp_env_info(env);
694         struct ccc_thread_info *cti = ccc_env_info(env);
695         unsigned long start = 0, end = 0, reserved;
696         unsigned long ra_end, len;
697         struct inode *inode;
698         struct ll_ra_read *bead;
699         struct ra_io_arg *ria = &vti->vti_ria;
700         struct ll_inode_info *lli;
701         struct cl_object *clob;
702         struct cl_attr   *attr = &cti->cti_attr;
703         int ret = 0;
704         __u64 kms;
705         ENTRY;
706
707         inode = mapping->host;
708         lli = ll_i2info(inode);
709         clob = lli->lli_clob;
710
711         memset(ria, 0, sizeof *ria);
712
713         cl_object_attr_lock(clob);
714         ret = cl_object_attr_get(env, clob, attr);
715         cl_object_attr_unlock(clob);
716
717         if (ret != 0)
718                 RETURN(ret);
719         kms = attr->cat_kms;
720         if (kms == 0) {
721                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
722                 RETURN(0);
723         }
724
725         spin_lock(&ras->ras_lock);
726         if (vio->cui_ra_window_set)
727                 bead = &vio->cui_bead;
728         else
729                 bead = NULL;
730
731         /* Enlarge the RA window to encompass the full read */
732         if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
733             bead->lrr_start + bead->lrr_count) {
734                 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
735                                       ras->ras_window_start;
736         }
737         /* Reserve a part of the read-ahead window that we'll be issuing */
738         if (ras->ras_window_len) {
739                 start = ras->ras_next_readahead;
740                 end = ras->ras_window_start + ras->ras_window_len - 1;
741         }
742         if (end != 0) {
743                 /* Truncate RA window to end of file */
744                 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
745                 ras->ras_next_readahead = max(end, end + 1);
746                 RAS_CDEBUG(ras);
747         }
748         ria->ria_start = start;
749         ria->ria_end = end;
750         /* If stride I/O mode is detected, get stride window*/
751         if (stride_io_mode(ras)) {
752                 ria->ria_stoff = ras->ras_stride_offset;
753                 ria->ria_length = ras->ras_stride_length;
754                 ria->ria_pages = ras->ras_stride_pages;
755         }
756         spin_unlock(&ras->ras_lock);
757
758         if (end == 0) {
759                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
760                 RETURN(0);
761         }
762         len = ria_page_count(ria);
763         if (len == 0)
764                 RETURN(0);
765
766         reserved = ll_ra_count_get(ll_i2sbi(inode), len);
767
768         if (reserved < len)
769                 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
770
771         CDEBUG(D_READA, "reserved page %lu \n", reserved);
772
773         ret = ll_read_ahead_pages(env, io, queue,
774                                   ria, &reserved, mapping, &ra_end);
775
776         LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
777         if (reserved != 0)
778                 ll_ra_count_put(ll_i2sbi(inode), reserved);
779
780         if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
781                 ll_ra_stats_inc(mapping, RA_STAT_EOF);
782
783         /* if we didn't get to the end of the region we reserved from
784          * the ras we need to go back and update the ras so that the
785          * next read-ahead tries from where we left off.  we only do so
786          * if the region we failed to issue read-ahead on is still ahead
787          * of the app and behind the next index to start read-ahead from */
788         CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
789                ra_end, end, ria->ria_end);
790
791         if (ra_end != end + 1) {
792                 spin_lock(&ras->ras_lock);
793                 if (ra_end < ras->ras_next_readahead &&
794                     index_in_window(ra_end, ras->ras_window_start, 0,
795                                     ras->ras_window_len)) {
796                         ras->ras_next_readahead = ra_end;
797                                RAS_CDEBUG(ras);
798                 }
799                 spin_unlock(&ras->ras_lock);
800         }
801
802         RETURN(ret);
803 }
804
805 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
806 {
807         ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
808 }
809
810 /* called with the ras_lock held or from places where it doesn't matter */
811 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
812 {
813         ras->ras_last_readpage = index;
814         ras->ras_consecutive_requests = 0;
815         ras->ras_consecutive_pages = 0;
816         ras->ras_window_len = 0;
817         ras_set_start(ras, index);
818         ras->ras_next_readahead = max(ras->ras_window_start, index);
819
820         RAS_CDEBUG(ras);
821 }
822
823 /* called with the ras_lock held or from places where it doesn't matter */
824 static void ras_stride_reset(struct ll_readahead_state *ras)
825 {
826         ras->ras_consecutive_stride_requests = 0;
827         ras->ras_stride_length = 0;
828         ras->ras_stride_pages = 0;
829         RAS_CDEBUG(ras);
830 }
831
832 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
833 {
834         spin_lock_init(&ras->ras_lock);
835         ras_reset(ras, 0);
836         ras->ras_requests = 0;
837         INIT_LIST_HEAD(&ras->ras_read_beads);
838 }
839
840 /*
841  * Check whether the read request is in the stride window.
842  * If it is in the stride window, return 1, otherwise return 0.
843  */
844 static int index_in_stride_window(unsigned long index,
845                                   struct ll_readahead_state *ras,
846                                   struct inode *inode)
847 {
848         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
849
850         if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0)
851                 return 0;
852
853         /* If it is contiguous read */
854         if (stride_gap == 0)
855                 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
856
857         /* Otherwise check the stride by itself */
858         return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
859                 ras->ras_consecutive_pages == ras->ras_stride_pages;
860 }
861
862 static void ras_update_stride_detector(struct ll_readahead_state *ras,
863                                        unsigned long index)
864 {
865         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
866
867         if (!stride_io_mode(ras) && (stride_gap != 0 ||
868              ras->ras_consecutive_stride_requests == 0)) {
869                 ras->ras_stride_pages = ras->ras_consecutive_pages;
870                 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
871         }
872         RAS_CDEBUG(ras);
873 }
874
875 static unsigned long
876 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
877 {
878         return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
879                                ras->ras_stride_pages, ras->ras_stride_offset,
880                                len);
881 }
882
883 /* Stride Read-ahead window will be increased inc_len according to
884  * stride I/O pattern */
885 static void ras_stride_increase_window(struct ll_readahead_state *ras,
886                                        struct ll_ra_info *ra,
887                                        unsigned long inc_len)
888 {
889         unsigned long left, step, window_len;
890         unsigned long stride_len;
891
892         LASSERT(ras->ras_stride_length > 0);
893         LASSERTF(ras->ras_window_start + ras->ras_window_len 
894                  >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
895                  " stride_offset %lu\n", ras->ras_window_start,
896                  ras->ras_window_len, ras->ras_stride_offset);
897
898         stride_len = ras->ras_window_start + ras->ras_window_len -
899                      ras->ras_stride_offset;
900
901         left = stride_len % ras->ras_stride_length;
902         window_len = ras->ras_window_len - left;
903
904         if (left < ras->ras_stride_pages)
905                 left += inc_len;
906         else
907                 left = ras->ras_stride_pages + inc_len;
908
909         LASSERT(ras->ras_stride_pages != 0);
910
911         step = left / ras->ras_stride_pages;
912         left %= ras->ras_stride_pages;
913
914         window_len += step * ras->ras_stride_length + left;
915
916         if (stride_page_count(ras, window_len) <= ra->ra_max_pages)
917                 ras->ras_window_len = window_len;
918
919         RAS_CDEBUG(ras);
920 }
921
922 /* Set stride I/O read-ahead window start offset */
923 static void ras_set_stride_offset(struct ll_readahead_state *ras)
924 {
925         unsigned long window_len = ras->ras_next_readahead -
926                                    ras->ras_window_start;
927         unsigned long left;
928
929         LASSERT(ras->ras_stride_length != 0);
930
931         left = window_len % ras->ras_stride_length;
932
933         ras->ras_stride_offset = ras->ras_next_readahead - left;
934
935         RAS_CDEBUG(ras);
936 }
937
938 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
939                 struct ll_readahead_state *ras, unsigned long index,
940                 unsigned hit)
941 {
942         struct ll_ra_info *ra = &sbi->ll_ra_info;
943         int zero = 0, stride_detect = 0, ra_miss = 0;
944         ENTRY;
945
946         spin_lock(&sbi->ll_lock);
947         spin_lock(&ras->ras_lock);
948
949         ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
950
951         /* reset the read-ahead window in two cases.  First when the app seeks
952          * or reads to some other part of the file.  Secondly if we get a
953          * read-ahead miss that we think we've previously issued.  This can
954          * be a symptom of there being so many read-ahead pages that the VM is
955          * reclaiming it before we get to it. */
956         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
957                 zero = 1;
958                 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
959         } else if (!hit && ras->ras_window_len &&
960                    index < ras->ras_next_readahead &&
961                    index_in_window(index, ras->ras_window_start, 0,
962                                    ras->ras_window_len)) {
963                 ra_miss = 1;
964                 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
965         }
966
967         /* On the second access to a file smaller than the tunable
968          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
969          * file up to ra_max_pages.  This is simply a best effort and
970          * only occurs once per open file.  Normal RA behavior is reverted
971          * to for subsequent IO.  The mmap case does not increment
972          * ras_requests and thus can never trigger this behavior. */
973         if (ras->ras_requests == 2 && !ras->ras_request_index) {
974                 __u64 kms_pages;
975
976                 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
977                             CFS_PAGE_SHIFT;
978
979                 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
980                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages);
981
982                 if (kms_pages &&
983                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
984                         ras->ras_window_start = 0;
985                         ras->ras_last_readpage = 0;
986                         ras->ras_next_readahead = 0;
987                         ras->ras_window_len = min(ra->ra_max_pages,
988                                 ra->ra_max_read_ahead_whole_pages);
989                         GOTO(out_unlock, 0);
990                 }
991         }
992         if (zero) {
993                 /* check whether it is in stride I/O mode*/
994                 if (!index_in_stride_window(index, ras, inode)) {
995                         ras_reset(ras, index);
996                         ras->ras_consecutive_pages++;
997                         ras_stride_reset(ras);
998                         GOTO(out_unlock, 0);
999                 } else {
1000                         ras->ras_consecutive_requests = 0;
1001                         if (++ras->ras_consecutive_stride_requests > 1)
1002                                 stride_detect = 1;
1003                         RAS_CDEBUG(ras);
1004                 }
1005         } else {
1006                 if (ra_miss) {
1007                         if (index_in_stride_window(index, ras, inode) &&
1008                             stride_io_mode(ras)) {
1009                                 /*If stride-RA hit cache miss, the stride dector
1010                                  *will not be reset to avoid the overhead of
1011                                  *redetecting read-ahead mode */
1012                                 if (index != ras->ras_last_readpage + 1)
1013                                        ras->ras_consecutive_pages = 0;
1014                                 RAS_CDEBUG(ras);
1015                         } else {
1016                                 /* Reset both stride window and normal RA window */
1017                                 ras_reset(ras, index);
1018                                 ras->ras_consecutive_pages++;
1019                                 ras_stride_reset(ras);
1020                                 GOTO(out_unlock, 0);
1021                         }
1022                 } else if (stride_io_mode(ras)) {
1023                         /* If this is contiguous read but in stride I/O mode
1024                          * currently, check whether stride step still is valid,
1025                          * if invalid, it will reset the stride ra window*/     
1026                         if (!index_in_stride_window(index, ras, inode)) {
1027                                 /* Shrink stride read-ahead window to be zero */
1028                                 ras_stride_reset(ras);
1029                                 ras->ras_window_len = 0;
1030                                 ras->ras_next_readahead = index;
1031                         }
1032                 }
1033         }
1034         ras->ras_consecutive_pages++;
1035         ras_update_stride_detector(ras, index);
1036         ras->ras_last_readpage = index;
1037         ras_set_start(ras, index);
1038         ras->ras_next_readahead = max(ras->ras_window_start,
1039                                       ras->ras_next_readahead);
1040         RAS_CDEBUG(ras);
1041
1042         /* Trigger RA in the mmap case where ras_consecutive_requests
1043          * is not incremented and thus can't be used to trigger RA */
1044         if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1045                 ras->ras_window_len = RAS_INCREASE_STEP;
1046                 GOTO(out_unlock, 0);
1047         }
1048
1049         /* Initially reset the stride window offset to next_readahead*/
1050         if (ras->ras_consecutive_stride_requests == 2 && stride_detect)
1051                 ras_set_stride_offset(ras);
1052
1053         /* The initial ras_window_len is set to the request size.  To avoid
1054          * uselessly reading and discarding pages for random IO the window is
1055          * only increased once per consecutive request received. */
1056         if ((ras->ras_consecutive_requests > 1 &&
1057             !ras->ras_request_index) || stride_detect) {
1058                 if (stride_io_mode(ras))
1059                         ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
1060                 else
1061                         ras->ras_window_len = min(ras->ras_window_len +
1062                                                   RAS_INCREASE_STEP,
1063                                                   ra->ra_max_pages);
1064         }
1065         EXIT;
1066 out_unlock:
1067         RAS_CDEBUG(ras);
1068         ras->ras_request_index++;
1069         spin_unlock(&ras->ras_lock);
1070         spin_unlock(&sbi->ll_lock);
1071         return;
1072 }
1073
1074 int ll_writepage(struct page *vmpage, struct writeback_control *_)
1075 {
1076         struct inode           *inode = vmpage->mapping->host;
1077         struct lu_env          *env;
1078         struct cl_io           *io;
1079         struct cl_page         *page;
1080         struct cl_object       *clob;
1081         struct cl_2queue       *queue;
1082         struct cl_env_nest      nest;
1083         int result;
1084         ENTRY;
1085
1086         LASSERT(PageLocked(vmpage));
1087         LASSERT(!PageWriteback(vmpage));
1088
1089         if (ll_i2dtexp(inode) == NULL)
1090                 RETURN(-EINVAL);
1091
1092         env = cl_env_nested_get(&nest);
1093         if (IS_ERR(env))
1094                 RETURN(PTR_ERR(env));
1095
1096         io    = &ccc_env_info(env)->cti_io;
1097         queue = &vvp_env_info(env)->vti_queue;
1098         clob  = ll_i2info(inode)->lli_clob;
1099         LASSERT(clob != NULL);
1100
1101         io->ci_obj = clob;
1102         result = cl_io_init(env, io, CIT_MISC, clob);
1103         if (result == 0) {
1104                 page = cl_page_find(env, clob, vmpage->index,
1105                                     vmpage, CPT_CACHEABLE);
1106                 if (!IS_ERR(page)) {
1107                         lu_ref_add(&page->cp_reference, "writepage",
1108                                    cfs_current());
1109                         cl_page_assume(env, io, page);
1110                         /*
1111                          * Mark page dirty, because this is what
1112                          * ->vio_submit()->cpo_prep_write() assumes.
1113                          *
1114                          * XXX better solution is to detect this from within
1115                          * cl_io_submit_rw() somehow.
1116                          */
1117                         set_page_dirty(vmpage);
1118                         cl_2queue_init_page(queue, page);
1119                         result = cl_io_submit_rw(env, io, CRT_WRITE,
1120                                                  queue, CRP_NORMAL);
1121                         cl_page_list_disown(env, io, &queue->c2_qin);
1122                         if (result != 0) {
1123                                 /*
1124                                  * There is no need to clear PG_writeback, as
1125                                  * cl_io_submit_rw() calls completion callback
1126                                  * on failure.
1127                                  */
1128                                 /*
1129                                  * Re-dirty page on error so it retries write,
1130                                  * but not in case when IO has actually
1131                                  * occurred and completed with an error.
1132                                  */
1133                                 if (!PageError(vmpage))
1134                                         set_page_dirty(vmpage);
1135                         }
1136                         LASSERT(!cl_page_is_owned(page, io));
1137                         lu_ref_del(&page->cp_reference,
1138                                    "writepage", cfs_current());
1139                         cl_page_put(env, page);
1140                         cl_2queue_fini(env, queue);
1141                 }
1142         }
1143         cl_io_fini(env, io);
1144         cl_env_nested_put(&nest, env);
1145         RETURN(result);
1146 }
1147
1148 int ll_readpage(struct file *file, struct page *vmpage)
1149 {
1150         struct lu_env    *env;
1151         struct cl_io     *io;
1152         struct cl_page   *page;
1153         int result;
1154         int refcheck;
1155         ENTRY;
1156
1157         result = ll_cl_init(file, vmpage, &env, &io, &page, &refcheck);
1158         if (result == 0) {
1159                 LASSERT(page->cp_type == CPT_CACHEABLE);
1160                 if (likely(!PageUptodate(vmpage))) {
1161                         cl_page_assume(env, io, page);
1162                         result = cl_io_read_page(env, io, page);
1163                 } else {
1164                         /* Page from a non-object file. */
1165                         LASSERT(!ll_i2info(vmpage->mapping->host)->lli_smd);
1166                         unlock_page(vmpage);
1167                         result = 0;
1168                 }
1169         }
1170         LASSERT(!cl_page_is_owned(page, io));
1171         ll_cl_fini(env, io, page, &refcheck);
1172         RETURN(result);
1173 }
1174