Whamcloud - gitweb
5c449a0bdeaf33c415e433eb681842f15226c3dc
[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 2008 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
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 lov @{ */
46
47 /*****************************************************************************
48  *
49  * Lov page operations.
50  *
51  */
52
53 static int lov_page_invariant(const struct cl_page_slice *slice)
54 {
55         const struct cl_page  *page = slice->cpl_page;
56         const struct cl_page  *sub  = lov_sub_page(slice);
57
58         return ergo(sub != NULL,
59                     page->cp_child == sub &&
60                     sub->cp_parent == page &&
61                     page->cp_state == sub->cp_state);
62 }
63
64 static void lov_page_fini(const struct lu_env *env,
65                           struct cl_page_slice *slice)
66 {
67         struct lov_page *lp  = cl2lov_page(slice);
68         struct cl_page  *sub = lov_sub_page(slice);
69
70         LINVRNT(lov_page_invariant(slice));
71         ENTRY;
72
73         if (sub != NULL) {
74                 LASSERT(sub->cp_state == CPS_FREEING);
75                 lu_ref_del(&sub->cp_reference, "lov", sub->cp_parent);
76                 sub->cp_parent = NULL;
77                 slice->cpl_page->cp_child = NULL;
78                 cl_page_put(env, sub);
79         }
80         OBD_SLAB_FREE_PTR(lp, lov_page_kmem);
81         EXIT;
82 }
83
84 static int lov_page_own(const struct lu_env *env,
85                         const struct cl_page_slice *slice, struct cl_io *io,
86                         int nonblock)
87 {
88         struct lov_io     *lio = lov_env_io(env);
89         struct lov_io_sub *sub;
90
91         LINVRNT(lov_page_invariant(slice));
92         LINVRNT(!cl2lov_page(slice)->lps_invalid);
93         ENTRY;
94
95         sub = lov_page_subio(env, lio, slice);
96         if (!IS_ERR(sub)) {
97                 lov_sub_page(slice)->cp_owner = sub->sub_io;
98                 lov_sub_put(sub);
99         } else
100                 LBUG(); /* Arrgh */
101         RETURN(0);
102 }
103
104 static void lov_page_assume(const struct lu_env *env,
105                             const struct cl_page_slice *slice, struct cl_io *io)
106 {
107         lov_page_own(env, slice, io, 0);
108 }
109
110 static int lov_page_print(const struct lu_env *env,
111                           const struct cl_page_slice *slice,
112                           void *cookie, lu_printer_t printer)
113 {
114         struct lov_page *lp = cl2lov_page(slice);
115
116         return (*printer)(env, cookie, LUSTRE_LOV_NAME"-page@%p\n", lp);
117 }
118
119 static const struct cl_page_operations lov_page_ops = {
120         .cpo_fini   = lov_page_fini,
121         .cpo_own    = lov_page_own,
122         .cpo_assume = lov_page_assume,
123         .cpo_print  = lov_page_print
124 };
125
126 static void lov_empty_page_fini(const struct lu_env *env,
127                                 struct cl_page_slice *slice)
128 {
129         struct lov_page *lp  = cl2lov_page(slice);
130
131         LASSERT(slice->cpl_page->cp_child == NULL);
132         ENTRY;
133         OBD_SLAB_FREE_PTR(lp, lov_page_kmem);
134         EXIT;
135 }
136
137 struct cl_page *lov_page_init_raid0(const struct lu_env *env,
138                                     struct cl_object *obj, struct cl_page *page,
139                                     cfs_page_t *vmpage)
140 {
141         struct lov_page   *lpg;
142         struct lov_object *loo = cl2lov(obj);
143         int result;
144
145         ENTRY;
146         OBD_SLAB_ALLOC_PTR_GFP(lpg, lov_page_kmem, CFS_ALLOC_IO);
147         if (lpg != NULL) {
148                 loff_t   offset;
149                 int      stripe;
150                 obd_off  suboff;
151                 struct cl_page          *subpage;
152                 struct cl_object        *subobj;
153                 struct lov_layout_raid0 *r0 = lov_r0(loo);
154
155                 offset = cl_offset(obj, page->cp_index);
156                 stripe = lov_stripe_number(r0->lo_lsm, offset);
157                 result = lov_stripe_offset(r0->lo_lsm, offset, stripe,
158                                            &suboff);
159                 LASSERT(stripe < r0->lo_nr);
160                 LASSERT(result == 0);
161
162                 subobj  = lovsub2cl(r0->lo_sub[stripe]);
163                 subpage = cl_page_find(env, subobj,
164                                        cl_index(subobj, suboff), vmpage,
165                                        page->cp_type);
166                 if (!IS_ERR(subpage)) {
167                         if (subpage->cp_parent != NULL) {
168                                 /*
169                                  * This is only possible when TRANSIENT page
170                                  * is being created, and CACHEABLE sub-page
171                                  * (attached to already existing top-page) has
172                                  * been found. Tell cl_page_find() to use
173                                  * existing page.
174                                  */
175                                 LASSERT(subpage->cp_type == CPT_CACHEABLE);
176                                 LASSERT(page->cp_type == CPT_TRANSIENT);
177                                 lpg->lps_invalid = 1;
178                                 cl_page_put(env, subpage);
179                                 /*
180                                  * XXX This assumes that lov is in the topmost
181                                  * cl_page.
182                                  */
183                                 result = PTR_ERR(cl_page_top(subpage));
184                         } else {
185                                 lu_ref_add(&subpage->cp_reference, "lov", page);
186                                 subpage->cp_parent = page;
187                                 page->cp_child = subpage;
188                         }
189                         cl_page_slice_add(page, &lpg->lps_cl,
190                                           obj, &lov_page_ops);
191                 } else
192                         result = PTR_ERR(subpage);
193         } else
194                 result = -ENOMEM;
195         RETURN(ERR_PTR(result));
196 }
197
198
199 static const struct cl_page_operations lov_empty_page_ops = {
200         .cpo_fini   = lov_empty_page_fini,
201         .cpo_print  = lov_page_print
202 };
203
204 struct cl_page *lov_page_init_empty(const struct lu_env *env,
205                                     struct cl_object *obj, struct cl_page *page,
206                                     cfs_page_t *vmpage)
207 {
208         struct lov_page   *lpg;
209         int result = -ENOMEM;
210         ENTRY;
211
212         OBD_SLAB_ALLOC_PTR_GFP(lpg, lov_page_kmem, CFS_ALLOC_IO);
213         if (lpg != NULL) {
214                 void *addr;
215                 cl_page_slice_add(page, &lpg->lps_cl,
216                                   obj, &lov_empty_page_ops);
217                 addr = cfs_kmap(vmpage);
218                 memset(addr, 0, cl_page_size(obj));
219                 cfs_kunmap(vmpage);
220                 cl_page_export(env, page, 1);
221                 result = 0;
222         }
223         RETURN(ERR_PTR(result));
224 }
225
226
227 /** @} lov */
228