Whamcloud - gitweb
LU-12580 lov: fix out of bound usercopy
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Implementation of cl_page for LOV layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_LOV
39
40 #include "lov_cl_internal.h"
41
42 /** \addtogroup lov
43  *  @{
44  */
45
46 /*****************************************************************************
47  *
48  * Lov page operations.
49  *
50  */
51
52 static int lov_comp_page_print(const struct lu_env *env,
53                                const struct cl_page_slice *slice,
54                                void *cookie, lu_printer_t printer)
55 {
56         struct lov_page *lp = cl2lov_page(slice);
57
58         return (*printer)(env, cookie,
59                           LUSTRE_LOV_NAME"-page@%p, gen: %u\n",
60                           lp, lp->lps_layout_gen);
61 }
62
63 static const struct cl_page_operations lov_comp_page_ops = {
64         .cpo_print = lov_comp_page_print
65 };
66
67 int lov_page_init_composite(const struct lu_env *env, struct cl_object *obj,
68                             struct cl_page *page, pgoff_t index)
69 {
70         struct lov_object *loo = cl2lov(obj);
71         struct lov_io *lio = lov_env_io(env);
72         struct cl_object *subobj;
73         struct cl_object *o;
74         struct lov_io_sub *sub;
75         struct lov_page *lpg = cl_object_page_slice(obj, page);
76         struct lov_layout_raid0 *r0;
77         loff_t offset;
78         loff_t suboff;
79         int entry;
80         int stripe;
81         int rc;
82
83         ENTRY;
84
85         offset = cl_offset(obj, index);
86         entry = lov_io_layout_at(lio, offset);
87         if (entry < 0 || !lsm_entry_inited(loo->lo_lsm, entry)) {
88                 /* non-existing layout component */
89                 lov_page_init_empty(env, obj, page, index);
90                 RETURN(0);
91         }
92
93         r0 = lov_r0(loo, entry);
94         stripe = lov_stripe_number(loo->lo_lsm, entry, offset);
95         LASSERT(stripe < r0->lo_nr);
96         rc = lov_stripe_offset(loo->lo_lsm, entry, offset, stripe, &suboff);
97         LASSERT(rc == 0);
98
99         page->cp_lov_index = lov_comp_index(entry, stripe);
100         lpg->lps_layout_gen = loo->lo_lsm->lsm_layout_gen;
101         cl_page_slice_add(page, &lpg->lps_cl, obj, index, &lov_comp_page_ops);
102
103         sub = lov_sub_get(env, lio, page->cp_lov_index);
104         if (IS_ERR(sub))
105                 RETURN(PTR_ERR(sub));
106
107         subobj = lovsub2cl(r0->lo_sub[stripe]);
108         list_for_each_entry(o, &subobj->co_lu.lo_header->loh_layers,
109                             co_lu.lo_linkage) {
110                 if (o->co_ops->coo_page_init) {
111                         rc = o->co_ops->coo_page_init(sub->sub_env, o, page,
112                                                       cl_index(subobj, suboff));
113                         if (rc != 0)
114                                 break;
115                 }
116         }
117
118         RETURN(rc);
119 }
120
121 static int lov_empty_page_print(const struct lu_env *env,
122                                 const struct cl_page_slice *slice,
123                                 void *cookie, lu_printer_t printer)
124 {
125         struct lov_page *lp = cl2lov_page(slice);
126
127         return (*printer)(env, cookie, LUSTRE_LOV_NAME"-page@%p, empty.\n", lp);
128 }
129
130 static const struct cl_page_operations lov_empty_page_ops = {
131         .cpo_print = lov_empty_page_print
132 };
133
134 int lov_page_init_empty(const struct lu_env *env, struct cl_object *obj,
135                         struct cl_page *page, pgoff_t index)
136 {
137         struct lov_page *lpg = cl_object_page_slice(obj, page);
138         void *addr;
139
140         ENTRY;
141
142         page->cp_lov_index = ~0;
143         cl_page_slice_add(page, &lpg->lps_cl, obj, index, &lov_empty_page_ops);
144         addr = kmap(page->cp_vmpage);
145         memset(addr, 0, cl_page_size(obj));
146         kunmap(page->cp_vmpage);
147         cl_page_export(env, page, 1);
148         RETURN(0);
149 }
150
151 int lov_page_init_foreign(const struct lu_env *env, struct cl_object *obj,
152                         struct cl_page *page, pgoff_t index)
153 {
154         CDEBUG(D_PAGE, DFID" has no data\n", PFID(lu_object_fid(&obj->co_lu)));
155         RETURN(-ENODATA);
156 }
157
158 bool lov_page_is_empty(const struct cl_page *page)
159 {
160         const struct cl_page_slice *slice = cl_page_at(page, &lov_device_type);
161
162         LASSERT(slice != NULL);
163         return slice->cpl_ops == &lov_empty_page_ops;
164 }
165
166
167 /** @} lov */
168