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