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