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