Whamcloud - gitweb
e877a6b2e32e649b51c85cae5b8a8cdf37933036
[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         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_info(env)->cti_io;
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" -> %i %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;
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         result = cl_io_commit_write(env, io, page, from, to);
307         if (cl_page_is_owned(page, io))
308                 cl_page_unassume(env, io, page);
309         /*
310          * Release reference acquired by cl_io_prepare_write().
311          */
312         lu_ref_del(&page->cp_reference, "prepare_write", cfs_current());
313         cl_page_put(env, page);
314         ll_cl_fini(lcc);
315         RETURN(result);
316 }
317
318 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
319 {
320         __u64 opc;
321
322         opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
323         return ll_osscapa_get(inode, opc);
324 }
325
326 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
327
328 /* WARNING: This algorithm is used to reduce the contention on
329  * sbi->ll_lock. It should work well if the ra_max_pages is much
330  * greater than the single file's read-ahead window.
331  *
332  * TODO: There may exist a `global sync problem' in this implementation.
333  * Considering the global ra window is 100M, and each file's ra window is 10M,
334  * there are over 10 files trying to get its ra budget and reach
335  * ll_ra_count_get at the exactly same time. All of them will get a zero ra
336  * window, although the global window is 100M. -jay
337  */
338 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi, unsigned long len)
339 {
340         struct ll_ra_info *ra = &sbi->ll_ra_info;
341         unsigned long ret;
342         ENTRY;
343
344         /**
345          * If read-ahead pages left are less than 1M, do not do read-ahead,
346          * otherwise it will form small read RPC(< 1M), which hurt server
347          * performance a lot.
348          */
349         ret = min(ra->ra_max_pages - cfs_atomic_read(&ra->ra_cur_pages), len);
350         if ((int)ret < 0 || ret < min((unsigned long)PTLRPC_MAX_BRW_PAGES, len))
351                 GOTO(out, ret = 0);
352
353         if (cfs_atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
354                 cfs_atomic_sub(ret, &ra->ra_cur_pages);
355                 ret = 0;
356         }
357 out:
358         RETURN(ret);
359 }
360
361 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
362 {
363         struct ll_ra_info *ra = &sbi->ll_ra_info;
364         cfs_atomic_sub(len, &ra->ra_cur_pages);
365 }
366
367 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
368 {
369         LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
370         lprocfs_counter_incr(sbi->ll_ra_stats, which);
371 }
372
373 void ll_ra_stats_inc(struct address_space *mapping, enum ra_stat which)
374 {
375         struct ll_sb_info *sbi = ll_i2sbi(mapping->host);
376         ll_ra_stats_inc_sbi(sbi, which);
377 }
378
379 #define RAS_CDEBUG(ras) \
380         CDEBUG(D_READA,                                                      \
381                "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu"    \
382                "csr %lu sf %lu sp %lu sl %lu \n",                            \
383                ras->ras_last_readpage, ras->ras_consecutive_requests,        \
384                ras->ras_consecutive_pages, ras->ras_window_start,            \
385                ras->ras_window_len, ras->ras_next_readahead,                 \
386                ras->ras_requests, ras->ras_request_index,                    \
387                ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
388                ras->ras_stride_pages, ras->ras_stride_length)
389
390 static int index_in_window(unsigned long index, unsigned long point,
391                            unsigned long before, unsigned long after)
392 {
393         unsigned long start = point - before, end = point + after;
394
395         if (start > point)
396                start = 0;
397         if (end < point)
398                end = ~0;
399
400         return start <= index && index <= end;
401 }
402
403 static struct ll_readahead_state *ll_ras_get(struct file *f)
404 {
405         struct ll_file_data       *fd;
406
407         fd = LUSTRE_FPRIVATE(f);
408         return &fd->fd_ras;
409 }
410
411 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
412 {
413         struct ll_readahead_state *ras;
414
415         ras = ll_ras_get(f);
416
417         cfs_spin_lock(&ras->ras_lock);
418         ras->ras_requests++;
419         ras->ras_request_index = 0;
420         ras->ras_consecutive_requests++;
421         rar->lrr_reader = current;
422
423         cfs_list_add(&rar->lrr_linkage, &ras->ras_read_beads);
424         cfs_spin_unlock(&ras->ras_lock);
425 }
426
427 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
428 {
429         struct ll_readahead_state *ras;
430
431         ras = ll_ras_get(f);
432
433         cfs_spin_lock(&ras->ras_lock);
434         cfs_list_del_init(&rar->lrr_linkage);
435         cfs_spin_unlock(&ras->ras_lock);
436 }
437
438 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
439 {
440         struct ll_ra_read *scan;
441
442         cfs_list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
443                 if (scan->lrr_reader == current)
444                         return scan;
445         }
446         return NULL;
447 }
448
449 struct ll_ra_read *ll_ra_read_get(struct file *f)
450 {
451         struct ll_readahead_state *ras;
452         struct ll_ra_read         *bead;
453
454         ras = ll_ras_get(f);
455
456         cfs_spin_lock(&ras->ras_lock);
457         bead = ll_ra_read_get_locked(ras);
458         cfs_spin_unlock(&ras->ras_lock);
459         return bead;
460 }
461
462 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
463                               struct cl_page_list *queue, struct cl_page *page,
464                               struct page *vmpage)
465 {
466         struct ccc_page *cp;
467         int              rc;
468
469         ENTRY;
470
471         rc = 0;
472         cl_page_assume(env, io, page);
473         lu_ref_add(&page->cp_reference, "ra", cfs_current());
474         cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
475         if (!cp->cpg_defer_uptodate && !Page_Uptodate(vmpage)) {
476                 rc = cl_page_is_under_lock(env, io, page);
477                 if (rc == -EBUSY) {
478                         cp->cpg_defer_uptodate = 1;
479                         cp->cpg_ra_used = 0;
480                         cl_page_list_add(queue, page);
481                         rc = 1;
482                 } else {
483                         cl_page_delete(env, page);
484                         rc = -ENOLCK;
485                 }
486         } else
487                 /* skip completed pages */
488                 cl_page_unassume(env, io, page);
489         lu_ref_del(&page->cp_reference, "ra", cfs_current());
490         cl_page_put(env, page);
491         RETURN(rc);
492 }
493
494 /**
495  * Initiates read-ahead of a page with given index.
496  *
497  * \retval     +ve: page was added to \a queue.
498  *
499  * \retval -ENOLCK: there is no extent lock for this part of a file, stop
500  *                  read-ahead.
501  *
502  * \retval  -ve, 0: page wasn't added to \a queue for other reason.
503  */
504 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
505                               struct cl_page_list *queue,
506                               int index, struct address_space *mapping)
507 {
508         struct page      *vmpage;
509         struct cl_object *clob  = ll_i2info(mapping->host)->lli_clob;
510         struct cl_page   *page;
511         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
512         unsigned int      gfp_mask;
513         int               rc    = 0;
514         const char       *msg   = NULL;
515
516         ENTRY;
517
518         gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
519 #ifdef __GFP_NOWARN
520         gfp_mask |= __GFP_NOWARN;
521 #endif
522         vmpage = grab_cache_page_nowait_gfp(mapping, index, gfp_mask);
523         if (vmpage != NULL) {
524                 /* Check if vmpage was truncated or reclaimed */
525                 if (vmpage->mapping == mapping) {
526                         page = cl_page_find(env, clob, vmpage->index,
527                                             vmpage, CPT_CACHEABLE);
528                         if (!IS_ERR(page)) {
529                                 rc = cl_read_ahead_page(env, io, queue,
530                                                         page, vmpage);
531                                 if (rc == -ENOLCK) {
532                                         which = RA_STAT_FAILED_MATCH;
533                                         msg   = "lock match failed";
534                                 }
535                         } else {
536                                 which = RA_STAT_FAILED_GRAB_PAGE;
537                                 msg   = "cl_page_find failed";
538                         }
539                 } else {
540                         which = RA_STAT_WRONG_GRAB_PAGE;
541                         msg   = "g_c_p_n returned invalid page";
542                 }
543                 if (rc != 1)
544                         unlock_page(vmpage);
545                 page_cache_release(vmpage);
546         } else {
547                 which = RA_STAT_FAILED_GRAB_PAGE;
548                 msg   = "g_c_p_n failed";
549         }
550         if (msg != NULL) {
551                 ll_ra_stats_inc(mapping, which);
552                 CDEBUG(D_READA, "%s\n", msg);
553         }
554         RETURN(rc);
555 }
556
557 #define RIA_DEBUG(ria)                                                       \
558         CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n",       \
559         ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
560         ria->ria_pages)
561
562 #define RAS_INCREASE_STEP PTLRPC_MAX_BRW_PAGES
563
564 static inline int stride_io_mode(struct ll_readahead_state *ras)
565 {
566         return ras->ras_consecutive_stride_requests > 1;
567 }
568 /* The function calculates how much pages will be read in
569  * [off, off + length], in such stride IO area,
570  * stride_offset = st_off, stride_lengh = st_len,
571  * stride_pages = st_pgs
572  *
573  *   |------------------|*****|------------------|*****|------------|*****|....
574  * st_off
575  *   |--- st_pgs     ---|
576  *   |-----     st_len   -----|
577  *
578  *              How many pages it should read in such pattern
579  *              |-------------------------------------------------------------|
580  *              off
581  *              |<------                  length                      ------->|
582  *
583  *          =   |<----->|  +  |-------------------------------------| +   |---|
584  *             start_left                 st_pgs * i                    end_left
585  */
586 static unsigned long
587 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
588                 unsigned long off, unsigned long length)
589 {
590         unsigned long start = off > st_off ? off - st_off : 0;
591         unsigned long end = off + length > st_off ? off + length - st_off : 0;
592         unsigned long start_left = 0;
593         unsigned long end_left = 0;
594         unsigned long pg_count;
595
596         if (st_len == 0 || length == 0 || end == 0)
597                 return length;
598
599         start_left = do_div(start, st_len);
600         if (start_left < st_pgs)
601                 start_left = st_pgs - start_left;
602         else
603                 start_left = 0;
604
605         end_left = do_div(end, st_len);
606         if (end_left > st_pgs)
607                 end_left = st_pgs;
608
609         CDEBUG(D_READA, "start %lu, end %lu start_left %lu end_left %lu \n",
610                start, end, start_left, end_left);
611
612         if (start == end)
613                 pg_count = end_left - (st_pgs - start_left);
614         else
615                 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
616
617         CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
618                "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
619
620         return pg_count;
621 }
622
623 static int ria_page_count(struct ra_io_arg *ria)
624 {
625         __u64 length = ria->ria_end >= ria->ria_start ?
626                        ria->ria_end - ria->ria_start + 1 : 0;
627
628         return stride_pg_count(ria->ria_stoff, ria->ria_length,
629                                ria->ria_pages, ria->ria_start,
630                                length);
631 }
632
633 /*Check whether the index is in the defined ra-window */
634 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
635 {
636         /* If ria_length == ria_pages, it means non-stride I/O mode,
637          * idx should always inside read-ahead window in this case
638          * For stride I/O mode, just check whether the idx is inside
639          * the ria_pages. */
640         return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
641                (idx - ria->ria_stoff) % ria->ria_length < ria->ria_pages;
642 }
643
644 static int ll_read_ahead_pages(const struct lu_env *env,
645                                struct cl_io *io, struct cl_page_list *queue,
646                                struct ra_io_arg *ria,
647                                unsigned long *reserved_pages,
648                                struct address_space *mapping,
649                                unsigned long *ra_end)
650 {
651         int rc, count = 0, stride_ria;
652         unsigned long page_idx;
653
654         LASSERT(ria != NULL);
655         RIA_DEBUG(ria);
656
657         stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
658         for (page_idx = ria->ria_start; page_idx <= ria->ria_end &&
659                         *reserved_pages > 0; page_idx++) {
660                 if (ras_inside_ra_window(page_idx, ria)) {
661                         /* If the page is inside the read-ahead window*/
662                         rc = ll_read_ahead_page(env, io, queue,
663                                                 page_idx, mapping);
664                         if (rc == 1) {
665                                 (*reserved_pages)--;
666                                 count ++;
667                         } else if (rc == -ENOLCK)
668                                 break;
669                 } else if (stride_ria) {
670                         /* If it is not in the read-ahead window, and it is
671                          * read-ahead mode, then check whether it should skip
672                          * the stride gap */
673                         pgoff_t offset;
674                         /* FIXME: This assertion only is valid when it is for
675                          * forward read-ahead, it will be fixed when backward
676                          * read-ahead is implemented */
677                         LASSERTF(page_idx > ria->ria_stoff, "since %lu in the"
678                                 " gap of ra window,it should bigger than stride"
679                                 " offset %lu \n", page_idx, ria->ria_stoff);
680
681                         offset = page_idx - ria->ria_stoff;
682                         offset = offset % (ria->ria_length);
683                         if (offset > ria->ria_pages) {
684                                 page_idx += ria->ria_length - offset;
685                                 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
686                                        ria->ria_length - offset);
687                                 continue;
688                         }
689                 }
690         }
691         *ra_end = page_idx;
692         return count;
693 }
694
695 int ll_readahead(const struct lu_env *env, struct cl_io *io,
696                  struct ll_readahead_state *ras, struct address_space *mapping,
697                  struct cl_page_list *queue, int flags)
698 {
699         struct vvp_io *vio = vvp_env_io(env);
700         struct vvp_thread_info *vti = vvp_env_info(env);
701         struct cl_attr *attr = ccc_env_thread_attr(env);
702         unsigned long start = 0, end = 0, reserved;
703         unsigned long ra_end, len;
704         struct inode *inode;
705         struct ll_ra_read *bead;
706         struct ra_io_arg *ria = &vti->vti_ria;
707         struct ll_inode_info *lli;
708         struct cl_object *clob;
709         int ret = 0;
710         __u64 kms;
711         ENTRY;
712
713         inode = mapping->host;
714         lli = ll_i2info(inode);
715         clob = lli->lli_clob;
716
717         memset(ria, 0, sizeof *ria);
718
719         cl_object_attr_lock(clob);
720         ret = cl_object_attr_get(env, clob, attr);
721         cl_object_attr_unlock(clob);
722
723         if (ret != 0)
724                 RETURN(ret);
725         kms = attr->cat_kms;
726         if (kms == 0) {
727                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_LEN);
728                 RETURN(0);
729         }
730
731         cfs_spin_lock(&ras->ras_lock);
732         if (vio->cui_ra_window_set)
733                 bead = &vio->cui_bead;
734         else
735                 bead = NULL;
736
737         /* Enlarge the RA window to encompass the full read */
738         if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
739             bead->lrr_start + bead->lrr_count) {
740                 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
741                                       ras->ras_window_start;
742         }
743         /* Reserve a part of the read-ahead window that we'll be issuing */
744         if (ras->ras_window_len) {
745                 start = ras->ras_next_readahead;
746                 end = ras->ras_window_start + ras->ras_window_len - 1;
747         }
748         if (end != 0) {
749                 /* Truncate RA window to end of file */
750                 end = min(end, (unsigned long)((kms - 1) >> CFS_PAGE_SHIFT));
751                 ras->ras_next_readahead = max(end, end + 1);
752                 RAS_CDEBUG(ras);
753         }
754         ria->ria_start = start;
755         ria->ria_end = end;
756         /* If stride I/O mode is detected, get stride window*/
757         if (stride_io_mode(ras)) {
758                 ria->ria_stoff = ras->ras_stride_offset;
759                 ria->ria_length = ras->ras_stride_length;
760                 ria->ria_pages = ras->ras_stride_pages;
761         }
762         cfs_spin_unlock(&ras->ras_lock);
763
764         if (end == 0) {
765                 ll_ra_stats_inc(mapping, RA_STAT_ZERO_WINDOW);
766                 RETURN(0);
767         }
768         len = ria_page_count(ria);
769         if (len == 0)
770                 RETURN(0);
771
772         reserved = ll_ra_count_get(ll_i2sbi(inode), len);
773
774         if (reserved < len)
775                 ll_ra_stats_inc(mapping, RA_STAT_MAX_IN_FLIGHT);
776
777         CDEBUG(D_READA, "reserved page %lu \n", reserved);
778
779         ret = ll_read_ahead_pages(env, io, queue,
780                                   ria, &reserved, mapping, &ra_end);
781
782         LASSERTF(reserved >= 0, "reserved %lu\n", reserved);
783         if (reserved != 0)
784                 ll_ra_count_put(ll_i2sbi(inode), reserved);
785
786         if (ra_end == end + 1 && ra_end == (kms >> CFS_PAGE_SHIFT))
787                 ll_ra_stats_inc(mapping, RA_STAT_EOF);
788
789         /* if we didn't get to the end of the region we reserved from
790          * the ras we need to go back and update the ras so that the
791          * next read-ahead tries from where we left off.  we only do so
792          * if the region we failed to issue read-ahead on is still ahead
793          * of the app and behind the next index to start read-ahead from */
794         CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
795                ra_end, end, ria->ria_end);
796
797         if (ra_end != end + 1) {
798                 cfs_spin_lock(&ras->ras_lock);
799                 if (ra_end < ras->ras_next_readahead &&
800                     index_in_window(ra_end, ras->ras_window_start, 0,
801                                     ras->ras_window_len)) {
802                         ras->ras_next_readahead = ra_end;
803                                RAS_CDEBUG(ras);
804                 }
805                 cfs_spin_unlock(&ras->ras_lock);
806         }
807
808         RETURN(ret);
809 }
810
811 static void ras_set_start(struct ll_readahead_state *ras, unsigned long index)
812 {
813         ras->ras_window_start = index & (~(RAS_INCREASE_STEP - 1));
814 }
815
816 /* called with the ras_lock held or from places where it doesn't matter */
817 static void ras_reset(struct ll_readahead_state *ras, unsigned long index)
818 {
819         ras->ras_last_readpage = index;
820         ras->ras_consecutive_requests = 0;
821         ras->ras_consecutive_pages = 0;
822         ras->ras_window_len = 0;
823         ras_set_start(ras, index);
824         ras->ras_next_readahead = max(ras->ras_window_start, index);
825
826         RAS_CDEBUG(ras);
827 }
828
829 /* called with the ras_lock held or from places where it doesn't matter */
830 static void ras_stride_reset(struct ll_readahead_state *ras)
831 {
832         ras->ras_consecutive_stride_requests = 0;
833         ras->ras_stride_length = 0;
834         ras->ras_stride_pages = 0;
835         RAS_CDEBUG(ras);
836 }
837
838 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
839 {
840         cfs_spin_lock_init(&ras->ras_lock);
841         ras_reset(ras, 0);
842         ras->ras_requests = 0;
843         CFS_INIT_LIST_HEAD(&ras->ras_read_beads);
844 }
845
846 /*
847  * Check whether the read request is in the stride window.
848  * If it is in the stride window, return 1, otherwise return 0.
849  */
850 static int index_in_stride_window(unsigned long index,
851                                   struct ll_readahead_state *ras,
852                                   struct inode *inode)
853 {
854         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
855
856         if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
857             ras->ras_stride_pages == ras->ras_stride_length)
858                 return 0;
859
860         /* If it is contiguous read */
861         if (stride_gap == 0)
862                 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
863
864         /*Otherwise check the stride by itself */
865         return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
866              ras->ras_consecutive_pages == ras->ras_stride_pages;
867 }
868
869 static void ras_update_stride_detector(struct ll_readahead_state *ras,
870                                        unsigned long index)
871 {
872         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
873
874         if (!stride_io_mode(ras) && (stride_gap != 0 ||
875              ras->ras_consecutive_stride_requests == 0)) {
876                 ras->ras_stride_pages = ras->ras_consecutive_pages;
877                 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
878         }
879         LASSERT(ras->ras_request_index == 0);
880         LASSERT(ras->ras_consecutive_stride_requests == 0);
881
882         if (index <= ras->ras_last_readpage) {
883                 /*Reset stride window for forward read*/
884                 ras_stride_reset(ras);
885                 return;
886         }
887
888         ras->ras_stride_pages = ras->ras_consecutive_pages;
889         ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
890
891         RAS_CDEBUG(ras);
892         return;
893 }
894
895 static unsigned long
896 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
897 {
898         return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
899                                ras->ras_stride_pages, ras->ras_stride_offset,
900                                len);
901 }
902
903 /* Stride Read-ahead window will be increased inc_len according to
904  * stride I/O pattern */
905 static void ras_stride_increase_window(struct ll_readahead_state *ras,
906                                        struct ll_ra_info *ra,
907                                        unsigned long inc_len)
908 {
909         unsigned long left, step, window_len;
910         unsigned long stride_len;
911
912         LASSERT(ras->ras_stride_length > 0);
913         LASSERTF(ras->ras_window_start + ras->ras_window_len
914                  >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
915                  " stride_offset %lu\n", ras->ras_window_start,
916                  ras->ras_window_len, ras->ras_stride_offset);
917
918         stride_len = ras->ras_window_start + ras->ras_window_len -
919                      ras->ras_stride_offset;
920
921         left = stride_len % ras->ras_stride_length;
922         window_len = ras->ras_window_len - left;
923
924         if (left < ras->ras_stride_pages)
925                 left += inc_len;
926         else
927                 left = ras->ras_stride_pages + inc_len;
928
929         LASSERT(ras->ras_stride_pages != 0);
930
931         step = left / ras->ras_stride_pages;
932         left %= ras->ras_stride_pages;
933
934         window_len += step * ras->ras_stride_length + left;
935
936         if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
937                 ras->ras_window_len = window_len;
938
939         RAS_CDEBUG(ras);
940 }
941
942 static void ras_increase_window(struct ll_readahead_state *ras,
943                                 struct ll_ra_info *ra, struct inode *inode)
944 {
945         /* The stretch of ra-window should be aligned with max rpc_size
946          * but current clio architecture does not support retrieve such
947          * information from lower layer. FIXME later
948          */
949         if (stride_io_mode(ras))
950                 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP);
951         else
952                 ras->ras_window_len = min(ras->ras_window_len +
953                                           RAS_INCREASE_STEP,
954                                           ra->ra_max_pages_per_file);
955 }
956
957 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
958                 struct ll_readahead_state *ras, unsigned long index,
959                 unsigned hit)
960 {
961         struct ll_ra_info *ra = &sbi->ll_ra_info;
962         int zero = 0, stride_detect = 0, ra_miss = 0;
963         ENTRY;
964
965         cfs_spin_lock(&sbi->ll_lock);
966         cfs_spin_lock(&ras->ras_lock);
967
968         ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
969
970         /* reset the read-ahead window in two cases.  First when the app seeks
971          * or reads to some other part of the file.  Secondly if we get a
972          * read-ahead miss that we think we've previously issued.  This can
973          * be a symptom of there being so many read-ahead pages that the VM is
974          * reclaiming it before we get to it. */
975         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
976                 zero = 1;
977                 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
978         } else if (!hit && ras->ras_window_len &&
979                    index < ras->ras_next_readahead &&
980                    index_in_window(index, ras->ras_window_start, 0,
981                                    ras->ras_window_len)) {
982                 ra_miss = 1;
983                 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
984         }
985
986         /* On the second access to a file smaller than the tunable
987          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
988          * file up to ra_max_pages_per_file.  This is simply a best effort
989          * and only occurs once per open file.  Normal RA behavior is reverted
990          * to for subsequent IO.  The mmap case does not increment
991          * ras_requests and thus can never trigger this behavior. */
992         if (ras->ras_requests == 2 && !ras->ras_request_index) {
993                 __u64 kms_pages;
994
995                 kms_pages = (i_size_read(inode) + CFS_PAGE_SIZE - 1) >>
996                             CFS_PAGE_SHIFT;
997
998                 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
999                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
1000
1001                 if (kms_pages &&
1002                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1003                         ras->ras_window_start = 0;
1004                         ras->ras_last_readpage = 0;
1005                         ras->ras_next_readahead = 0;
1006                         ras->ras_window_len = min(ra->ra_max_pages_per_file,
1007                                 ra->ra_max_read_ahead_whole_pages);
1008                         GOTO(out_unlock, 0);
1009                 }
1010         }
1011         if (zero) {
1012                 /* check whether it is in stride I/O mode*/
1013                 if (!index_in_stride_window(index, ras, inode)) {
1014                         if (ras->ras_consecutive_stride_requests == 0 &&
1015                             ras->ras_request_index == 0) {
1016                                 ras_update_stride_detector(ras, index);
1017                                 ras->ras_consecutive_stride_requests ++;
1018                         } else {
1019                                 ras_stride_reset(ras);
1020                         }
1021                         ras_reset(ras, index);
1022                         ras->ras_consecutive_pages++;
1023                         GOTO(out_unlock, 0);
1024                 } else {
1025                         ras->ras_consecutive_pages = 0;
1026                         ras->ras_consecutive_requests = 0;
1027                         if (++ras->ras_consecutive_stride_requests > 1)
1028                                 stride_detect = 1;
1029                         RAS_CDEBUG(ras);
1030                 }
1031         } else {
1032                 if (ra_miss) {
1033                         if (index_in_stride_window(index, ras, inode) &&
1034                             stride_io_mode(ras)) {
1035                                 /*If stride-RA hit cache miss, the stride dector
1036                                  *will not be reset to avoid the overhead of
1037                                  *redetecting read-ahead mode */
1038                                 if (index != ras->ras_last_readpage + 1)
1039                                        ras->ras_consecutive_pages = 0;
1040                                 ras_reset(ras, index);
1041                                 RAS_CDEBUG(ras);
1042                         } else {
1043                                 /* Reset both stride window and normal RA
1044                                  * window */
1045                                 ras_reset(ras, index);
1046                                 ras->ras_consecutive_pages++;
1047                                 ras_stride_reset(ras);
1048                                 GOTO(out_unlock, 0);
1049                         }
1050                 } else if (stride_io_mode(ras)) {
1051                         /* If this is contiguous read but in stride I/O mode
1052                          * currently, check whether stride step still is valid,
1053                          * if invalid, it will reset the stride ra window*/
1054                         if (!index_in_stride_window(index, ras, inode)) {
1055                                 /* Shrink stride read-ahead window to be zero */
1056                                 ras_stride_reset(ras);
1057                                 ras->ras_window_len = 0;
1058                                 ras->ras_next_readahead = index;
1059                         }
1060                 }
1061         }
1062         ras->ras_consecutive_pages++;
1063         ras->ras_last_readpage = index;
1064         ras_set_start(ras, index);
1065         ras->ras_next_readahead = max(ras->ras_window_start,
1066                                       ras->ras_next_readahead);
1067         RAS_CDEBUG(ras);
1068
1069         /* Trigger RA in the mmap case where ras_consecutive_requests
1070          * is not incremented and thus can't be used to trigger RA */
1071         if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1072                 ras->ras_window_len = RAS_INCREASE_STEP;
1073                 GOTO(out_unlock, 0);
1074         }
1075
1076         /* Initially reset the stride window offset to next_readahead*/
1077         if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1078                 /**
1079                  * Once stride IO mode is detected, next_readahead should be
1080                  * reset to make sure next_readahead > stride offset
1081                  */
1082                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1083                 ras->ras_stride_offset = index;
1084                 ras->ras_window_len = RAS_INCREASE_STEP;
1085         }
1086
1087         /* The initial ras_window_len is set to the request size.  To avoid
1088          * uselessly reading and discarding pages for random IO the window is
1089          * only increased once per consecutive request received. */
1090         if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1091             !ras->ras_request_index)
1092                 ras_increase_window(ras, ra, inode);
1093         EXIT;
1094 out_unlock:
1095         RAS_CDEBUG(ras);
1096         ras->ras_request_index++;
1097         cfs_spin_unlock(&ras->ras_lock);
1098         cfs_spin_unlock(&sbi->ll_lock);
1099         return;
1100 }
1101
1102 int ll_writepage(struct page *vmpage, struct writeback_control *unused)
1103 {
1104         struct inode           *inode = vmpage->mapping->host;
1105         struct lu_env          *env;
1106         struct cl_io           *io;
1107         struct cl_page         *page;
1108         struct cl_object       *clob;
1109         struct cl_2queue       *queue;
1110         struct cl_env_nest      nest;
1111         int result;
1112         ENTRY;
1113
1114         LASSERT(PageLocked(vmpage));
1115         LASSERT(!PageWriteback(vmpage));
1116
1117         if (ll_i2dtexp(inode) == NULL)
1118                 RETURN(-EINVAL);
1119
1120         env = cl_env_nested_get(&nest);
1121         if (IS_ERR(env))
1122                 RETURN(PTR_ERR(env));
1123
1124         io    = &ccc_env_info(env)->cti_io;
1125         queue = &vvp_env_info(env)->vti_queue;
1126         clob  = ll_i2info(inode)->lli_clob;
1127         LASSERT(clob != NULL);
1128
1129         io->ci_obj = clob;
1130         result = cl_io_init(env, io, CIT_MISC, clob);
1131         if (result == 0) {
1132                 page = cl_page_find(env, clob, vmpage->index,
1133                                     vmpage, CPT_CACHEABLE);
1134                 if (!IS_ERR(page)) {
1135                         lu_ref_add(&page->cp_reference, "writepage",
1136                                    cfs_current());
1137                         cl_page_assume(env, io, page);
1138                         /*
1139                          * Mark page dirty, because this is what
1140                          * ->vio_submit()->cpo_prep_write() assumes.
1141                          *
1142                          * XXX better solution is to detect this from within
1143                          * cl_io_submit_rw() somehow.
1144                          */
1145                         set_page_dirty(vmpage);
1146                         cl_2queue_init_page(queue, page);
1147                         result = cl_io_submit_rw(env, io, CRT_WRITE,
1148                                                  queue, CRP_NORMAL);
1149                         cl_page_list_disown(env, io, &queue->c2_qin);
1150                         if (result != 0) {
1151                                 /*
1152                                  * There is no need to clear PG_writeback, as
1153                                  * cl_io_submit_rw() calls completion callback
1154                                  * on failure.
1155                                  */
1156                                 /*
1157                                  * Re-dirty page on error so it retries write,
1158                                  * but not in case when IO has actually
1159                                  * occurred and completed with an error.
1160                                  */
1161                                 if (!PageError(vmpage))
1162                                         set_page_dirty(vmpage);
1163                         }
1164                         LASSERT(!cl_page_is_owned(page, io));
1165                         lu_ref_del(&page->cp_reference,
1166                                    "writepage", cfs_current());
1167                         cl_page_put(env, page);
1168                         cl_2queue_fini(env, queue);
1169                 }
1170         }
1171         cl_io_fini(env, io);
1172         cl_env_nested_put(&nest, env);
1173         RETURN(result);
1174 }
1175
1176 int ll_readpage(struct file *file, struct page *vmpage)
1177 {
1178         struct ll_cl_context *lcc;
1179         int result;
1180         ENTRY;
1181
1182         lcc = ll_cl_init(file, vmpage, 0);
1183         if (!IS_ERR(lcc)) {
1184                 struct lu_env  *env  = lcc->lcc_env;
1185                 struct cl_io   *io   = lcc->lcc_io;
1186                 struct cl_page *page = lcc->lcc_page;
1187
1188                 LASSERT(page->cp_type == CPT_CACHEABLE);
1189                 if (likely(!PageUptodate(vmpage))) {
1190                         cl_page_assume(env, io, page);
1191                         result = cl_io_read_page(env, io, page);
1192                 } else {
1193                         /* Page from a non-object file. */
1194                         LASSERT(!ll_i2info(vmpage->mapping->host)->lli_smd);
1195                         unlock_page(vmpage);
1196                         result = 0;
1197                 }
1198                 ll_cl_fini(lcc);
1199         } else {
1200                 result = PTR_ERR(lcc);
1201         }
1202         RETURN(result);
1203 }
1204