Merge branch 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / drivers / staging / lustre / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Implementation of cl_page for LOV layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_LOV
39
40 #include "lov_cl_internal.h"
41
42 /** \addtogroup lov
43  *  @{
44  */
45
46 /*****************************************************************************
47  *
48  * Lov page operations.
49  *
50  */
51
52 /**
53  * Adjust the stripe index by layout of raid0. @max_index is the maximum
54  * page index covered by an underlying DLM lock.
55  * This function converts max_index from stripe level to file level, and make
56  * sure it's not beyond one stripe.
57  */
58 static int lov_raid0_page_is_under_lock(const struct lu_env *env,
59                                         const struct cl_page_slice *slice,
60                                         struct cl_io *unused,
61                                         pgoff_t *max_index)
62 {
63         struct lov_object *loo = cl2lov(slice->cpl_obj);
64         struct lov_layout_raid0 *r0 = lov_r0(loo);
65         pgoff_t index = *max_index;
66         unsigned int pps; /* pages per stripe */
67
68         CDEBUG(D_READA, "*max_index = %lu, nr = %d\n", index, r0->lo_nr);
69         if (index == 0) /* the page is not covered by any lock */
70                 return 0;
71
72         if (r0->lo_nr == 1) /* single stripe file */
73                 return 0;
74
75         /* max_index is stripe level, convert it into file level */
76         if (index != CL_PAGE_EOF) {
77                 int stripeno = lov_page_stripe(slice->cpl_page);
78                 *max_index = lov_stripe_pgoff(loo->lo_lsm, index, stripeno);
79         }
80
81         /* calculate the end of current stripe */
82         pps = loo->lo_lsm->lsm_stripe_size >> PAGE_SHIFT;
83         index = ((slice->cpl_index + pps) & ~(pps - 1)) - 1;
84
85         /* never exceed the end of the stripe */
86         *max_index = min_t(pgoff_t, *max_index, index);
87         return 0;
88 }
89
90 static int lov_raid0_page_print(const struct lu_env *env,
91                                 const struct cl_page_slice *slice,
92                                 void *cookie, lu_printer_t printer)
93 {
94         struct lov_page *lp = cl2lov_page(slice);
95
96         return (*printer)(env, cookie, LUSTRE_LOV_NAME "-page@%p, raid0\n", lp);
97 }
98
99 static const struct cl_page_operations lov_raid0_page_ops = {
100         .cpo_is_under_lock = lov_raid0_page_is_under_lock,
101         .cpo_print  = lov_raid0_page_print
102 };
103
104 int lov_page_init_raid0(const struct lu_env *env, struct cl_object *obj,
105                         struct cl_page *page, pgoff_t index)
106 {
107         struct lov_object *loo = cl2lov(obj);
108         struct lov_layout_raid0 *r0 = lov_r0(loo);
109         struct lov_io     *lio = lov_env_io(env);
110         struct cl_object  *subobj;
111         struct cl_object  *o;
112         struct lov_io_sub *sub;
113         struct lov_page   *lpg = cl_object_page_slice(obj, page);
114         loff_t       offset;
115         u64         suboff;
116         int             stripe;
117         int             rc;
118
119         offset = cl_offset(obj, index);
120         stripe = lov_stripe_number(loo->lo_lsm, offset);
121         LASSERT(stripe < r0->lo_nr);
122         rc = lov_stripe_offset(loo->lo_lsm, offset, stripe, &suboff);
123         LASSERT(rc == 0);
124
125         cl_page_slice_add(page, &lpg->lps_cl, obj, index, &lov_raid0_page_ops);
126
127         sub = lov_sub_get(env, lio, stripe);
128         if (IS_ERR(sub))
129                 return PTR_ERR(sub);
130
131         subobj = lovsub2cl(r0->lo_sub[stripe]);
132         list_for_each_entry(o, &subobj->co_lu.lo_header->loh_layers,
133                             co_lu.lo_linkage) {
134                 if (o->co_ops->coo_page_init) {
135                         rc = o->co_ops->coo_page_init(sub->sub_env, o, page,
136                                                       cl_index(subobj, suboff));
137                         if (rc != 0)
138                                 break;
139                 }
140         }
141         lov_sub_put(sub);
142
143         return rc;
144 }
145
146 static int lov_empty_page_print(const struct lu_env *env,
147                                 const struct cl_page_slice *slice,
148                                 void *cookie, lu_printer_t printer)
149 {
150         struct lov_page *lp = cl2lov_page(slice);
151
152         return (*printer)(env, cookie, LUSTRE_LOV_NAME "-page@%p, empty.\n",
153                           lp);
154 }
155
156 static const struct cl_page_operations lov_empty_page_ops = {
157         .cpo_print = lov_empty_page_print
158 };
159
160 int lov_page_init_empty(const struct lu_env *env, struct cl_object *obj,
161                         struct cl_page *page, pgoff_t index)
162 {
163         struct lov_page *lpg = cl_object_page_slice(obj, page);
164         void *addr;
165
166         cl_page_slice_add(page, &lpg->lps_cl, obj, index, &lov_empty_page_ops);
167         addr = kmap(page->cp_vmpage);
168         memset(addr, 0, cl_page_size(obj));
169         kunmap(page->cp_vmpage);
170         cl_page_export(env, page, 1);
171         return 0;
172 }
173
174 /** @} lov */