Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[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, Whamcloud, Inc.
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  */
40
41 #define DEBUG_SUBSYSTEM S_LOV
42
43 #include "lov_cl_internal.h"
44
45 /** \addtogroup lov
46  *  @{
47  */
48
49 /*****************************************************************************
50  *
51  * Lov page operations.
52  *
53  */
54
55 static int lov_page_invariant(const struct cl_page_slice *slice)
56 {
57         const struct cl_page  *page = slice->cpl_page;
58         const struct cl_page  *sub  = lov_sub_page(slice);
59
60         return ergo(sub != NULL,
61                     page->cp_child == sub &&
62                     sub->cp_parent == page &&
63                     page->cp_state == sub->cp_state);
64 }
65
66 static void lov_page_fini(const struct lu_env *env,
67                           struct cl_page_slice *slice)
68 {
69         struct lov_page *lp  = cl2lov_page(slice);
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         OBD_SLAB_FREE_PTR(lp, lov_page_kmem);
83         EXIT;
84 }
85
86 static int lov_page_own(const struct lu_env *env,
87                         const struct cl_page_slice *slice, struct cl_io *io,
88                         int nonblock)
89 {
90         struct lov_io     *lio = lov_env_io(env);
91         struct lov_io_sub *sub;
92
93         LINVRNT(lov_page_invariant(slice));
94         LINVRNT(!cl2lov_page(slice)->lps_invalid);
95         ENTRY;
96
97         sub = lov_page_subio(env, lio, slice);
98         if (!IS_ERR(sub)) {
99                 lov_sub_page(slice)->cp_owner = sub->sub_io;
100                 lov_sub_put(sub);
101         } else
102                 LBUG(); /* Arrgh */
103         RETURN(0);
104 }
105
106 static void lov_page_assume(const struct lu_env *env,
107                             const struct cl_page_slice *slice, struct cl_io *io)
108 {
109         lov_page_own(env, slice, io, 0);
110 }
111
112 static int lov_page_print(const struct lu_env *env,
113                           const struct cl_page_slice *slice,
114                           void *cookie, lu_printer_t printer)
115 {
116         struct lov_page *lp = cl2lov_page(slice);
117
118         return (*printer)(env, cookie, LUSTRE_LOV_NAME"-page@%p\n", lp);
119 }
120
121 static const struct cl_page_operations lov_page_ops = {
122         .cpo_fini   = lov_page_fini,
123         .cpo_own    = lov_page_own,
124         .cpo_assume = lov_page_assume,
125         .cpo_print  = lov_page_print
126 };
127
128 static void lov_empty_page_fini(const struct lu_env *env,
129                                 struct cl_page_slice *slice)
130 {
131         struct lov_page *lp  = cl2lov_page(slice);
132
133         LASSERT(slice->cpl_page->cp_child == NULL);
134         ENTRY;
135         OBD_SLAB_FREE_PTR(lp, lov_page_kmem);
136         EXIT;
137 }
138
139 struct cl_page *lov_page_init_raid0(const struct lu_env *env,
140                                     struct cl_object *obj, struct cl_page *page,
141                                     cfs_page_t *vmpage)
142 {
143         struct lov_object *loo = cl2lov(obj);
144         struct lov_layout_raid0 *r0 = lov_r0(loo);
145         struct lov_io     *lio = lov_env_io(env);
146         struct cl_page    *subpage;
147         struct cl_object  *subobj;
148         struct lov_io_sub *sub;
149         struct lov_page   *lpg;
150         struct cl_page    *result;
151         loff_t             offset;
152         obd_off            suboff;
153         int                stripe;
154         int                rc;
155         ENTRY;
156
157         offset = cl_offset(obj, page->cp_index);
158         stripe = lov_stripe_number(r0->lo_lsm, offset);
159         LASSERT(stripe < r0->lo_nr);
160         rc = lov_stripe_offset(r0->lo_lsm, offset, stripe,
161                                    &suboff);
162         LASSERT(rc == 0);
163
164         OBD_SLAB_ALLOC_PTR_GFP(lpg, lov_page_kmem, CFS_ALLOC_IO);
165         if (lpg == NULL)
166                 GOTO(out, result = ERR_PTR(-ENOMEM));
167
168         lpg->lps_invalid = 1;
169         cl_page_slice_add(page, &lpg->lps_cl, obj, &lov_page_ops);
170
171         sub = lov_sub_get(env, lio, stripe);
172         if (IS_ERR(sub))
173                 GOTO(out, result = (struct cl_page *)sub);
174
175         subobj = lovsub2cl(r0->lo_sub[stripe]);
176         subpage = cl_page_find_sub(sub->sub_env, subobj,
177                                    cl_index(subobj, suboff), vmpage, page);
178         lov_sub_put(sub);
179         if (IS_ERR(subpage))
180                 GOTO(out, result = subpage);
181
182         if (likely(subpage->cp_parent == page)) {
183                 lu_ref_add(&subpage->cp_reference, "lov", page);
184                 lpg->lps_invalid = 0;
185                 result = NULL;
186         } else {
187                 CL_PAGE_DEBUG(D_ERROR, env, page, "parent page\n");
188                 CL_PAGE_DEBUG(D_ERROR, env, subpage, "child page\n");
189                 LASSERT(0);
190         }
191
192         EXIT;
193 out:
194         return(result);
195 }
196
197
198 static const struct cl_page_operations lov_empty_page_ops = {
199         .cpo_fini   = lov_empty_page_fini,
200         .cpo_print  = lov_page_print
201 };
202
203 struct cl_page *lov_page_init_empty(const struct lu_env *env,
204                                     struct cl_object *obj, struct cl_page *page,
205                                     cfs_page_t *vmpage)
206 {
207         struct lov_page   *lpg;
208         int result = -ENOMEM;
209         ENTRY;
210
211         OBD_SLAB_ALLOC_PTR_GFP(lpg, lov_page_kmem, CFS_ALLOC_IO);
212         if (lpg != NULL) {
213                 void *addr;
214                 cl_page_slice_add(page, &lpg->lps_cl,
215                                   obj, &lov_empty_page_ops);
216                 addr = cfs_kmap(vmpage);
217                 memset(addr, 0, cl_page_size(obj));
218                 cfs_kunmap(vmpage);
219                 cl_page_export(env, page, 1);
220                 result = 0;
221         }
222         RETURN(ERR_PTR(result));
223 }
224
225
226 /** @} lov */
227