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