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