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