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