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