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