Whamcloud - gitweb
LU-13799 lov: Cache stripe offset calculation
[fs/lustre-release.git] / lustre / lov / lov_page.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * Implementation of cl_page for LOV layer.
32  *
33  *   Author: Nikita Danilov <nikita.danilov@sun.com>
34  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOV
38
39 #include "lov_cl_internal.h"
40
41 /** \addtogroup lov
42  *  @{
43  */
44
45 /*****************************************************************************
46  *
47  * Lov page operations.
48  *
49  */
50
51 static int lov_comp_page_print(const struct lu_env *env,
52                                const struct cl_page_slice *slice,
53                                void *cookie, lu_printer_t printer)
54 {
55         struct lov_page *lp = cl2lov_page(slice);
56
57         return (*printer)(env, cookie,
58                           LUSTRE_LOV_NAME"-page@%p\n", lp);
59 }
60
61 static const struct cl_page_operations lov_comp_page_ops = {
62         .cpo_print = lov_comp_page_print
63 };
64
65 int lov_page_init_composite(const struct lu_env *env, struct cl_object *obj,
66                             struct cl_page *page, pgoff_t index)
67 {
68         struct lov_object *loo = cl2lov(obj);
69         struct lov_io *lio = lov_env_io(env);
70         struct cl_object *subobj;
71         struct cl_object *o;
72         struct lov_io_sub *sub;
73         struct lov_page *lpg = cl_object_page_slice(obj, page);
74         struct lov_layout_raid0 *r0;
75         loff_t offset;
76         loff_t suboff;
77         bool stripe_cached = false;
78         int entry;
79         int stripe;
80         int rc;
81
82         ENTRY;
83
84         /* Direct i/o (CPT_TRANSIENT) is split strictly to stripes, so we can
85          * cache the stripe information.  Buffered i/o is differently
86          * organized, and stripe calculation isn't a significant cost for
87          * buffered i/o, so we only cache this for direct i/o.
88          */
89         stripe_cached = lio->lis_cached_entry != LIS_CACHE_ENTRY_NONE &&
90                         page->cp_type == CPT_TRANSIENT;
91
92         offset = cl_offset(obj, index);
93
94         if (stripe_cached) {
95                 entry = lio->lis_cached_entry;
96                 stripe = lio->lis_cached_stripe;
97                 /* Offset can never go backwards in an i/o, so this is valid */
98                 suboff = lio->lis_cached_suboff + offset - lio->lis_cached_off;
99         } else {
100                 entry = lov_io_layout_at(lio, offset);
101
102                 stripe = lov_stripe_number(loo->lo_lsm, entry, offset);
103                 rc = lov_stripe_offset(loo->lo_lsm, entry, offset, stripe,
104                                        &suboff);
105                 LASSERT(rc == 0);
106                 lio->lis_cached_entry = entry;
107                 lio->lis_cached_stripe = stripe;
108                 lio->lis_cached_off = offset;
109                 lio->lis_cached_suboff = suboff;
110         }
111
112         if (entry < 0 || !lsm_entry_inited(loo->lo_lsm, entry)) {
113                 /* non-existing layout component */
114                 lov_page_init_empty(env, obj, page, index);
115                 RETURN(0);
116         }
117
118         CDEBUG(D_PAGE, "offset %llu, entry %d, stripe %d, suboff %llu\n",
119                offset, entry, stripe, suboff);
120
121         page->cp_lov_index = lov_comp_index(entry, stripe);
122         cl_page_slice_add(page, &lpg->lps_cl, obj, &lov_comp_page_ops);
123
124         if (!stripe_cached) {
125                 sub = lov_sub_get(env, lio, page->cp_lov_index);
126                 if (IS_ERR(sub))
127                         RETURN(PTR_ERR(sub));
128         } else {
129                 sub = lio->lis_cached_sub;
130         }
131
132         lio->lis_cached_sub = sub;
133
134         r0 = lov_r0(loo, entry);
135         LASSERT(stripe < r0->lo_nr);
136
137         subobj = lovsub2cl(r0->lo_sub[stripe]);
138         cl_object_for_each(o, subobj) {
139                 if (o->co_ops->coo_page_init) {
140                         rc = o->co_ops->coo_page_init(sub->sub_env, o, page,
141                                                       cl_index(subobj, suboff));
142                         if (rc != 0)
143                                 break;
144                 }
145         }
146
147         RETURN(rc);
148 }
149
150 static int lov_empty_page_print(const struct lu_env *env,
151                                 const struct cl_page_slice *slice,
152                                 void *cookie, lu_printer_t printer)
153 {
154         struct lov_page *lp = cl2lov_page(slice);
155
156         return (*printer)(env, cookie, LUSTRE_LOV_NAME"-page@%p, empty.\n", lp);
157 }
158
159 static const struct cl_page_operations lov_empty_page_ops = {
160         .cpo_print = lov_empty_page_print
161 };
162
163 int lov_page_init_empty(const struct lu_env *env, struct cl_object *obj,
164                         struct cl_page *page, pgoff_t index)
165 {
166         struct lov_page *lpg = cl_object_page_slice(obj, page);
167         void *addr;
168
169         ENTRY;
170
171         page->cp_lov_index = ~0;
172         cl_page_slice_add(page, &lpg->lps_cl, obj, &lov_empty_page_ops);
173         addr = kmap(page->cp_vmpage);
174         memset(addr, 0, cl_page_size(obj));
175         kunmap(page->cp_vmpage);
176         cl_page_export(env, page, 1);
177         RETURN(0);
178 }
179
180 int lov_page_init_foreign(const struct lu_env *env, struct cl_object *obj,
181                         struct cl_page *page, pgoff_t index)
182 {
183         CDEBUG(D_PAGE, DFID" has no data\n", PFID(lu_object_fid(&obj->co_lu)));
184         RETURN(-ENODATA);
185 }
186
187 bool lov_page_is_empty(const struct cl_page *page)
188 {
189         const struct cl_page_slice *slice = cl_page_at(page, &lov_device_type);
190
191         LASSERT(slice != NULL);
192         return slice->cpl_ops == &lov_empty_page_ops;
193 }
194
195
196 /** @} lov */
197