Whamcloud - gitweb
0ee6b61940eb161eea298a9a4689da35f85ae694
[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 static int lov_page_invariant(const struct cl_page_slice *slice)
57 {
58         const struct cl_page  *page = slice->cpl_page;
59         const struct cl_page  *sub  = lov_sub_page(slice);
60
61         return ergo(sub != NULL,
62                     page->cp_child == sub &&
63                     sub->cp_parent == page &&
64                     page->cp_state == sub->cp_state);
65 }
66
67 static void lov_page_fini(const struct lu_env *env,
68                           struct cl_page_slice *slice)
69 {
70         struct cl_page  *sub = lov_sub_page(slice);
71
72         LINVRNT(lov_page_invariant(slice));
73         ENTRY;
74
75         if (sub != NULL) {
76                 LASSERT(sub->cp_state == CPS_FREEING);
77                 lu_ref_del(&sub->cp_reference, "lov", sub->cp_parent);
78                 sub->cp_parent = NULL;
79                 slice->cpl_page->cp_child = NULL;
80                 cl_page_put(env, sub);
81         }
82         EXIT;
83 }
84
85 static int lov_page_own(const struct lu_env *env,
86                         const struct cl_page_slice *slice, struct cl_io *io,
87                         int nonblock)
88 {
89         struct lov_io     *lio = lov_env_io(env);
90         struct lov_io_sub *sub;
91
92         LINVRNT(lov_page_invariant(slice));
93         LINVRNT(!cl2lov_page(slice)->lps_invalid);
94         ENTRY;
95
96         sub = lov_page_subio(env, lio, slice);
97         if (!IS_ERR(sub)) {
98                 lov_sub_page(slice)->cp_owner = sub->sub_io;
99                 lov_sub_put(sub);
100         } else
101                 LBUG(); /* Arrgh */
102         RETURN(0);
103 }
104
105 static void lov_page_assume(const struct lu_env *env,
106                             const struct cl_page_slice *slice, struct cl_io *io)
107 {
108         lov_page_own(env, slice, io, 0);
109 }
110
111 static int lov_page_print(const struct lu_env *env,
112                           const struct cl_page_slice *slice,
113                           void *cookie, lu_printer_t printer)
114 {
115         struct lov_page *lp = cl2lov_page(slice);
116
117         return (*printer)(env, cookie, LUSTRE_LOV_NAME"-page@%p\n", lp);
118 }
119
120 static const struct cl_page_operations lov_page_ops = {
121         .cpo_fini       = lov_page_fini,
122         .cpo_own        = lov_page_own,
123         .cpo_assume     = lov_page_assume,
124         .cpo_print      = lov_page_print
125 };
126
127 static void lov_empty_page_fini(const struct lu_env *env,
128                                 struct cl_page_slice *slice)
129 {
130         LASSERT(slice->cpl_page->cp_child == NULL);
131 }
132
133 int lov_page_init_raid0(const struct lu_env *env, struct cl_object *obj,
134                         struct cl_page *page, struct page *vmpage)
135 {
136         struct lov_object *loo = cl2lov(obj);
137         struct lov_layout_raid0 *r0 = lov_r0(loo);
138         struct lov_io     *lio = lov_env_io(env);
139         struct cl_page    *subpage;
140         struct cl_object  *subobj;
141         struct lov_io_sub *sub;
142         struct lov_page   *lpg = cl_object_page_slice(obj, page);
143         loff_t             offset;
144         obd_off            suboff;
145         int                stripe;
146         int                rc;
147         ENTRY;
148
149         offset = cl_offset(obj, page->cp_index);
150         stripe = lov_stripe_number(loo->lo_lsm, offset);
151         LASSERT(stripe < r0->lo_nr);
152         rc = lov_stripe_offset(loo->lo_lsm, offset, stripe,
153                                &suboff);
154         LASSERT(rc == 0);
155
156         lpg->lps_invalid = 1;
157         cl_page_slice_add(page, &lpg->lps_cl, obj, &lov_page_ops);
158
159         sub = lov_sub_get(env, lio, stripe);
160         if (IS_ERR(sub))
161                 RETURN(PTR_ERR(sub));
162
163         subobj = lovsub2cl(r0->lo_sub[stripe]);
164         subpage = cl_page_alloc(sub->sub_env, subobj, cl_index(subobj, suboff),
165                                 vmpage, page->cp_type);
166         if (!IS_ERR(subpage)) {
167                 subpage->cp_parent = page;
168                 page->cp_child = subpage;
169                 lpg->lps_invalid = 0;
170         } else
171                 rc = PTR_ERR(subpage);
172         lov_sub_put(sub);
173         RETURN(rc);
174 }
175
176 static const struct cl_page_operations lov_empty_page_ops = {
177         .cpo_fini   = lov_empty_page_fini,
178         .cpo_print  = lov_page_print
179 };
180
181 int lov_page_init_empty(const struct lu_env *env, struct cl_object *obj,
182                         struct cl_page *page, struct page *vmpage)
183 {
184         struct lov_page *lpg = cl_object_page_slice(obj, page);
185         void *addr;
186         ENTRY;
187
188         cl_page_slice_add(page, &lpg->lps_cl, obj, &lov_empty_page_ops);
189         addr = kmap(vmpage);
190         memset(addr, 0, cl_page_size(obj));
191         kunmap(vmpage);
192         cl_page_export(env, page, 1);
193         RETURN(0);
194 }
195
196
197 /** @} lov */
198