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