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