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, 2012, 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  */
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 
lov_page_invariant(const struct cl_page_slice * slice)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 
lov_page_fini(const struct lu_env * env,struct cl_page_slice * slice)66 static void lov_page_fini(const struct lu_env *env,
67 			  struct cl_page_slice *slice)
68 {
69 	struct cl_page  *sub = lov_sub_page(slice);
70 
71 	LINVRNT(lov_page_invariant(slice));
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 }
81 
lov_page_own(const struct lu_env * env,const struct cl_page_slice * slice,struct cl_io * io,int nonblock)82 static int lov_page_own(const struct lu_env *env,
83 			const struct cl_page_slice *slice, struct cl_io *io,
84 			int nonblock)
85 {
86 	struct lov_io     *lio = lov_env_io(env);
87 	struct lov_io_sub *sub;
88 
89 	LINVRNT(lov_page_invariant(slice));
90 	LINVRNT(!cl2lov_page(slice)->lps_invalid);
91 
92 	sub = lov_page_subio(env, lio, slice);
93 	if (!IS_ERR(sub)) {
94 		lov_sub_page(slice)->cp_owner = sub->sub_io;
95 		lov_sub_put(sub);
96 	} else
97 		LBUG(); /* Arrgh */
98 	return 0;
99 }
100 
lov_page_assume(const struct lu_env * env,const struct cl_page_slice * slice,struct cl_io * io)101 static void lov_page_assume(const struct lu_env *env,
102 			    const struct cl_page_slice *slice, struct cl_io *io)
103 {
104 	lov_page_own(env, slice, io, 0);
105 }
106 
lov_page_cache_add(const struct lu_env * env,const struct cl_page_slice * slice,struct cl_io * io)107 static int lov_page_cache_add(const struct lu_env *env,
108 			      const struct cl_page_slice *slice,
109 			      struct cl_io *io)
110 {
111 	struct lov_io     *lio = lov_env_io(env);
112 	struct lov_io_sub *sub;
113 	int rc = 0;
114 
115 	LINVRNT(lov_page_invariant(slice));
116 	LINVRNT(!cl2lov_page(slice)->lps_invalid);
117 
118 	sub = lov_page_subio(env, lio, slice);
119 	if (!IS_ERR(sub)) {
120 		rc = cl_page_cache_add(sub->sub_env, sub->sub_io,
121 				       slice->cpl_page->cp_child, CRT_WRITE);
122 		lov_sub_put(sub);
123 	} else {
124 		rc = PTR_ERR(sub);
125 		CL_PAGE_DEBUG(D_ERROR, env, slice->cpl_page, "rc = %d\n", rc);
126 	}
127 	return rc;
128 }
129 
lov_page_print(const struct lu_env * env,const struct cl_page_slice * slice,void * cookie,lu_printer_t printer)130 static int lov_page_print(const struct lu_env *env,
131 			  const struct cl_page_slice *slice,
132 			  void *cookie, lu_printer_t printer)
133 {
134 	struct lov_page *lp = cl2lov_page(slice);
135 
136 	return (*printer)(env, cookie, LUSTRE_LOV_NAME"-page@%p\n", lp);
137 }
138 
139 static const struct cl_page_operations lov_page_ops = {
140 	.cpo_fini   = lov_page_fini,
141 	.cpo_own    = lov_page_own,
142 	.cpo_assume = lov_page_assume,
143 	.io = {
144 		[CRT_WRITE] = {
145 			.cpo_cache_add = lov_page_cache_add
146 		}
147 	},
148 	.cpo_print  = lov_page_print
149 };
150 
lov_empty_page_fini(const struct lu_env * env,struct cl_page_slice * slice)151 static void lov_empty_page_fini(const struct lu_env *env,
152 				struct cl_page_slice *slice)
153 {
154 	LASSERT(slice->cpl_page->cp_child == NULL);
155 }
156 
lov_page_init_raid0(const struct lu_env * env,struct cl_object * obj,struct cl_page * page,struct page * vmpage)157 int lov_page_init_raid0(const struct lu_env *env, struct cl_object *obj,
158 			struct cl_page *page, struct page *vmpage)
159 {
160 	struct lov_object *loo = cl2lov(obj);
161 	struct lov_layout_raid0 *r0 = lov_r0(loo);
162 	struct lov_io     *lio = lov_env_io(env);
163 	struct cl_page    *subpage;
164 	struct cl_object  *subobj;
165 	struct lov_io_sub *sub;
166 	struct lov_page   *lpg = cl_object_page_slice(obj, page);
167 	loff_t	     offset;
168 	u64	    suboff;
169 	int		stripe;
170 	int		rc;
171 
172 	offset = cl_offset(obj, page->cp_index);
173 	stripe = lov_stripe_number(loo->lo_lsm, offset);
174 	LASSERT(stripe < r0->lo_nr);
175 	rc = lov_stripe_offset(loo->lo_lsm, offset, stripe,
176 				   &suboff);
177 	LASSERT(rc == 0);
178 
179 	lpg->lps_invalid = 1;
180 	cl_page_slice_add(page, &lpg->lps_cl, obj, &lov_page_ops);
181 
182 	sub = lov_sub_get(env, lio, stripe);
183 	if (IS_ERR(sub)) {
184 		rc = PTR_ERR(sub);
185 		goto out;
186 	}
187 
188 	subobj = lovsub2cl(r0->lo_sub[stripe]);
189 	subpage = cl_page_find_sub(sub->sub_env, subobj,
190 				   cl_index(subobj, suboff), vmpage, page);
191 	lov_sub_put(sub);
192 	if (IS_ERR(subpage)) {
193 		rc = PTR_ERR(subpage);
194 		goto out;
195 	}
196 
197 	if (likely(subpage->cp_parent == page)) {
198 		lu_ref_add(&subpage->cp_reference, "lov", page);
199 		lpg->lps_invalid = 0;
200 		rc = 0;
201 	} else {
202 		CL_PAGE_DEBUG(D_ERROR, env, page, "parent page\n");
203 		CL_PAGE_DEBUG(D_ERROR, env, subpage, "child page\n");
204 		LASSERT(0);
205 	}
206 
207 out:
208 	return rc;
209 }
210 
211 static const struct cl_page_operations lov_empty_page_ops = {
212 	.cpo_fini   = lov_empty_page_fini,
213 	.cpo_print  = lov_page_print
214 };
215 
lov_page_init_empty(const struct lu_env * env,struct cl_object * obj,struct cl_page * page,struct page * vmpage)216 int lov_page_init_empty(const struct lu_env *env, struct cl_object *obj,
217 			struct cl_page *page, struct page *vmpage)
218 {
219 	struct lov_page *lpg = cl_object_page_slice(obj, page);
220 	void *addr;
221 
222 	cl_page_slice_add(page, &lpg->lps_cl, obj, &lov_empty_page_ops);
223 	addr = kmap(vmpage);
224 	memset(addr, 0, cl_page_size(obj));
225 	kunmap(vmpage);
226 	cl_page_export(env, page, 1);
227 	return 0;
228 }
229 
230 /** @} lov */
231