Whamcloud - gitweb
15b9033a7121667fbff88d411ba09be3d7d3d8ca
[fs/lustre-release.git] / lustre / lov / lov_page.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * Implementation of cl_page for LOV layer.
40  *
41  *   Author: Nikita Danilov <nikita.danilov@sun.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_LOV
45
46 #include "lov_cl_internal.h"
47
48 /** \addtogroup lov
49  *  @{
50  */
51
52 /*****************************************************************************
53  *
54  * Lov page operations.
55  *
56  */
57
58 static int lov_page_invariant(const struct cl_page_slice *slice)
59 {
60         const struct cl_page  *page = slice->cpl_page;
61         const struct cl_page  *sub  = lov_sub_page(slice);
62
63         return ergo(sub != NULL,
64                     page->cp_child == sub &&
65                     sub->cp_parent == page &&
66                     page->cp_state == sub->cp_state);
67 }
68
69 static void lov_page_fini(const struct lu_env *env,
70                           struct cl_page_slice *slice)
71 {
72         struct lov_page *lp  = cl2lov_page(slice);
73         struct cl_page  *sub = lov_sub_page(slice);
74
75         LINVRNT(lov_page_invariant(slice));
76         ENTRY;
77
78         if (sub != NULL) {
79                 LASSERT(sub->cp_state == CPS_FREEING);
80                 lu_ref_del(&sub->cp_reference, "lov", sub->cp_parent);
81                 sub->cp_parent = NULL;
82                 slice->cpl_page->cp_child = NULL;
83                 cl_page_put(env, sub);
84         }
85         OBD_SLAB_FREE_PTR(lp, lov_page_kmem);
86         EXIT;
87 }
88
89 static int lov_page_own(const struct lu_env *env,
90                         const struct cl_page_slice *slice, struct cl_io *io,
91                         int nonblock)
92 {
93         struct lov_io     *lio = lov_env_io(env);
94         struct lov_io_sub *sub;
95
96         LINVRNT(lov_page_invariant(slice));
97         LINVRNT(!cl2lov_page(slice)->lps_invalid);
98         ENTRY;
99
100         sub = lov_page_subio(env, lio, slice);
101         if (!IS_ERR(sub)) {
102                 lov_sub_page(slice)->cp_owner = sub->sub_io;
103                 lov_sub_put(sub);
104         } else
105                 LBUG(); /* Arrgh */
106         RETURN(0);
107 }
108
109 static void lov_page_assume(const struct lu_env *env,
110                             const struct cl_page_slice *slice, struct cl_io *io)
111 {
112         lov_page_own(env, slice, io, 0);
113 }
114
115 static int lov_page_print(const struct lu_env *env,
116                           const struct cl_page_slice *slice,
117                           void *cookie, lu_printer_t printer)
118 {
119         struct lov_page *lp = cl2lov_page(slice);
120
121         return (*printer)(env, cookie, LUSTRE_LOV_NAME"-page@%p\n", lp);
122 }
123
124 static const struct cl_page_operations lov_page_ops = {
125         .cpo_fini   = lov_page_fini,
126         .cpo_own    = lov_page_own,
127         .cpo_assume = lov_page_assume,
128         .cpo_print  = lov_page_print
129 };
130
131 static void lov_empty_page_fini(const struct lu_env *env,
132                                 struct cl_page_slice *slice)
133 {
134         struct lov_page *lp  = cl2lov_page(slice);
135
136         LASSERT(slice->cpl_page->cp_child == NULL);
137         ENTRY;
138         OBD_SLAB_FREE_PTR(lp, lov_page_kmem);
139         EXIT;
140 }
141
142 struct cl_page *lov_page_init_raid0(const struct lu_env *env,
143                                     struct cl_object *obj, struct cl_page *page,
144                                     cfs_page_t *vmpage)
145 {
146         struct lov_object *loo = cl2lov(obj);
147         struct lov_layout_raid0 *r0 = lov_r0(loo);
148         struct lov_io     *lio = lov_env_io(env);
149         struct cl_page    *subpage;
150         struct cl_object  *subobj;
151         struct lov_io_sub *sub;
152         struct lov_page   *lpg;
153         struct cl_page    *result;
154         loff_t             offset;
155         obd_off            suboff;
156         int                stripe;
157         int                rc;
158         ENTRY;
159
160         offset = cl_offset(obj, page->cp_index);
161         stripe = lov_stripe_number(r0->lo_lsm, offset);
162         LASSERT(stripe < r0->lo_nr);
163         rc = lov_stripe_offset(r0->lo_lsm, offset, stripe,
164                                    &suboff);
165         LASSERT(rc == 0);
166
167         OBD_SLAB_ALLOC_PTR_GFP(lpg, lov_page_kmem, CFS_ALLOC_IO);
168         if (lpg == NULL)
169                 GOTO(out, result = ERR_PTR(-ENOMEM));
170
171         lpg->lps_invalid = 1;
172         cl_page_slice_add(page, &lpg->lps_cl, obj, &lov_page_ops);
173
174         sub = lov_sub_get(env, lio, stripe);
175         if (IS_ERR(sub))
176                 GOTO(out, result = (struct cl_page *)sub);
177
178         subobj = lovsub2cl(r0->lo_sub[stripe]);
179         subpage = cl_page_find_sub(sub->sub_env, subobj,
180                                    cl_index(subobj, suboff), vmpage, page);
181         lov_sub_put(sub);
182         if (IS_ERR(subpage))
183                 GOTO(out, result = subpage);
184
185         if (likely(subpage->cp_parent == page)) {
186                 lu_ref_add(&subpage->cp_reference, "lov", page);
187                 lpg->lps_invalid = 0;
188                 result = NULL;
189         } else {
190                 CL_PAGE_DEBUG(D_ERROR, env, page, "parent page\n");
191                 CL_PAGE_DEBUG(D_ERROR, env, subpage, "child page\n");
192                 LASSERT(0);
193         }
194
195         EXIT;
196 out:
197         return(result);
198 }
199
200
201 static const struct cl_page_operations lov_empty_page_ops = {
202         .cpo_fini   = lov_empty_page_fini,
203         .cpo_print  = lov_page_print
204 };
205
206 struct cl_page *lov_page_init_empty(const struct lu_env *env,
207                                     struct cl_object *obj, struct cl_page *page,
208                                     cfs_page_t *vmpage)
209 {
210         struct lov_page   *lpg;
211         int result = -ENOMEM;
212         ENTRY;
213
214         OBD_SLAB_ALLOC_PTR_GFP(lpg, lov_page_kmem, CFS_ALLOC_IO);
215         if (lpg != NULL) {
216                 void *addr;
217                 cl_page_slice_add(page, &lpg->lps_cl,
218                                   obj, &lov_empty_page_ops);
219                 addr = cfs_kmap(vmpage);
220                 memset(addr, 0, cl_page_size(obj));
221                 cfs_kunmap(vmpage);
222                 cl_page_export(env, page, 1);
223                 result = 0;
224         }
225         RETURN(ERR_PTR(result));
226 }
227
228
229 /** @} lov */
230