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