Whamcloud - gitweb
LU-4860 lov: Handle the case of stripe size is not power 2
[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.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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, 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  * Implementation of cl_page for LOV layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LOV
43
44 #include "lov_cl_internal.h"
45
46 /** \addtogroup lov
47  *  @{
48  */
49
50 /*****************************************************************************
51  *
52  * Lov page operations.
53  *
54  */
55
56 /**
57  * Adjust the stripe index by layout of raid0. @max_index is the maximum
58  * page index covered by an underlying DLM lock.
59  * This function converts max_index from stripe level to file level, and make
60  * sure it's not beyond one stripe.
61  */
62 static int lov_raid0_page_is_under_lock(const struct lu_env *env,
63                                         const struct cl_page_slice *slice,
64                                         struct cl_io *unused,
65                                         pgoff_t *max_index)
66 {
67         struct lov_object *loo = cl2lov(slice->cpl_obj);
68         struct lov_layout_raid0 *r0 = lov_r0(loo);
69         pgoff_t index = *max_index;
70         unsigned int pps; /* pages per stripe */
71         ENTRY;
72
73         CDEBUG(D_READA, DFID "*max_index = %lu, nr = %d\n",
74                PFID(lu_object_fid(lov2lu(loo))), index, r0->lo_nr);
75
76         if (index == 0) /* the page is not covered by any lock */
77                 RETURN(0);
78
79         if (r0->lo_nr == 1) /* single stripe file */
80                 RETURN(0);
81
82         /* max_index is stripe level, convert it into file level */
83         if (index != CL_PAGE_EOF) {
84                 int stripeno = lov_page_stripe(slice->cpl_page);
85                 *max_index = lov_stripe_pgoff(loo->lo_lsm, index, stripeno);
86         }
87
88         /* calculate the end of current stripe */
89         pps = loo->lo_lsm->lsm_stripe_size >> PAGE_CACHE_SHIFT;
90         index = slice->cpl_index + pps - slice->cpl_index % pps - 1;
91
92         CDEBUG(D_READA, DFID "*max_index = %lu, index = %lu, pps = %u, "
93                "stripe_size = %u, stripe no = %u, page index = %lu\n",
94                PFID(lu_object_fid(lov2lu(loo))), *max_index, index, pps,
95                loo->lo_lsm->lsm_stripe_size, lov_page_stripe(slice->cpl_page),
96                slice->cpl_index);
97
98         /* never exceed the end of the stripe */
99         *max_index = min_t(pgoff_t, *max_index, index);
100         RETURN(0);
101 }
102
103 static int lov_raid0_page_print(const struct lu_env *env,
104                                 const struct cl_page_slice *slice,
105                                 void *cookie, lu_printer_t printer)
106 {
107         struct lov_page *lp = cl2lov_page(slice);
108
109         return (*printer)(env, cookie, LUSTRE_LOV_NAME"-page@%p, raid0\n", lp);
110 }
111
112 static const struct cl_page_operations lov_raid0_page_ops = {
113         .cpo_is_under_lock = lov_raid0_page_is_under_lock,
114         .cpo_print = lov_raid0_page_print
115 };
116
117 int lov_page_init_raid0(const struct lu_env *env, struct cl_object *obj,
118                         struct cl_page *page, pgoff_t index)
119 {
120         struct lov_object *loo = cl2lov(obj);
121         struct lov_layout_raid0 *r0 = lov_r0(loo);
122         struct lov_io     *lio = lov_env_io(env);
123         struct cl_object  *subobj;
124         struct cl_object  *o;
125         struct lov_io_sub *sub;
126         struct lov_page   *lpg = cl_object_page_slice(obj, page);
127         loff_t             offset;
128         obd_off            suboff;
129         int                stripe;
130         int                rc;
131         ENTRY;
132
133         offset = cl_offset(obj, index);
134         stripe = lov_stripe_number(loo->lo_lsm, offset);
135         LASSERT(stripe < r0->lo_nr);
136         rc = lov_stripe_offset(loo->lo_lsm, offset, stripe,
137                                &suboff);
138         LASSERT(rc == 0);
139
140         cl_page_slice_add(page, &lpg->lps_cl, obj, index, &lov_raid0_page_ops);
141
142         sub = lov_sub_get(env, lio, stripe);
143         if (IS_ERR(sub))
144                 RETURN(PTR_ERR(sub));
145
146         subobj = lovsub2cl(r0->lo_sub[stripe]);
147         cfs_list_for_each_entry(o, &subobj->co_lu.lo_header->loh_layers,
148                                 co_lu.lo_linkage) {
149                 if (o->co_ops->coo_page_init != NULL) {
150                         rc = o->co_ops->coo_page_init(sub->sub_env, o, page,
151                                                       cl_index(subobj, suboff));
152                         if (rc != 0)
153                                 break;
154                 }
155         }
156         lov_sub_put(sub);
157         RETURN(rc);
158 }
159
160 static int lov_empty_page_print(const struct lu_env *env,
161                                 const struct cl_page_slice *slice,
162                                 void *cookie, lu_printer_t printer)
163 {
164         struct lov_page *lp = cl2lov_page(slice);
165
166         return (*printer)(env, cookie, LUSTRE_LOV_NAME"-page@%p, empty.\n", lp);
167 }
168
169 static const struct cl_page_operations lov_empty_page_ops = {
170         .cpo_print = lov_empty_page_print
171 };
172
173 int lov_page_init_empty(const struct lu_env *env, struct cl_object *obj,
174                         struct cl_page *page, pgoff_t index)
175 {
176         struct lov_page *lpg = cl_object_page_slice(obj, page);
177         void *addr;
178         ENTRY;
179
180         cl_page_slice_add(page, &lpg->lps_cl, obj, index, &lov_empty_page_ops);
181         addr = kmap(page->cp_vmpage);
182         memset(addr, 0, cl_page_size(obj));
183         kunmap(page->cp_vmpage);
184         cl_page_export(env, page, 1);
185         RETURN(0);
186 }
187
188
189 /** @} lov */
190