Whamcloud - gitweb
db0c852564370d5c2d8afef623373ea6d42191d9
[fs/lustre-release.git] / 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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * Implementation of cl_page for LOV layer.
32  *
33  *   Author: Nikita Danilov <nikita.danilov@sun.com>
34  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOV
38
39 #include "lov_cl_internal.h"
40 #include <linux/bug.h>
41 #include <linux/compiler.h>
42
43 /** \addtogroup lov
44  *  @{
45  */
46
47 /**
48  * Lov page operations.
49  */
50 int lov_page_init_composite(const struct lu_env *env, struct cl_object *obj,
51                             struct cl_page *page, pgoff_t index)
52 {
53         struct lov_object *loo = cl2lov(obj);
54         struct lov_io *lio = lov_env_io(env);
55         struct cl_object *subobj;
56         struct cl_object *o;
57         struct lov_io_sub *sub;
58         struct lov_layout_raid0 *r0;
59         loff_t offset;
60         loff_t suboff;
61         bool stripe_cached = false;
62         int entry;
63         int stripe;
64         int rc;
65
66         ENTRY;
67
68         /* Direct i/o (CPT_TRANSIENT) is split strictly to stripes, so we can
69          * cache the stripe information.  Buffered i/o is differently
70          * organized, and stripe calculation isn't a significant cost for
71          * buffered i/o, so we only cache this for direct i/o.
72          */
73         stripe_cached = lio->lis_cached_entry != LIS_CACHE_ENTRY_NONE &&
74                         page->cp_type == CPT_TRANSIENT;
75
76         offset = index << PAGE_SHIFT;
77
78         if (stripe_cached) {
79                 entry = lio->lis_cached_entry;
80                 /* if there's no layout at this offset, we'll end up here with
81                  * a cached layout entry, so we must verify the layout includes
82                  * this offset
83                  */
84                 if (!lov_io_layout_at_confirm(lio, entry, offset))
85                         return -ENODATA;
86                 stripe = lio->lis_cached_stripe;
87                 /* Offset can never go backwards in an i/o, so this is valid */
88                 suboff = lio->lis_cached_suboff + offset - lio->lis_cached_off;
89         } else {
90                 entry = lov_io_layout_at(lio, offset);
91                 if (entry < 0)
92                         return -ENODATA;
93
94                 stripe = lov_stripe_number(loo->lo_lsm, entry, offset);
95                 rc = lov_stripe_offset(loo->lo_lsm, entry, offset, stripe,
96                                        &suboff);
97                 LASSERT(rc == 0);
98                 lio->lis_cached_entry = entry;
99                 lio->lis_cached_stripe = stripe;
100                 lio->lis_cached_off = offset;
101                 lio->lis_cached_suboff = suboff;
102         }
103
104         if (entry < 0 || !lsm_entry_inited(loo->lo_lsm, entry)) {
105                 /* non-existing layout component */
106                 lov_page_init_empty(env, obj, page, index);
107                 RETURN(0);
108         }
109
110         CDEBUG(D_PAGE, "offset %llu, entry %d, stripe %d, suboff %llu\n",
111                offset, entry, stripe, suboff);
112
113         page->cp_lov_index = lov_comp_index(entry, stripe);
114         LASSERT(page->cp_lov_index != CP_LOV_INDEX_EMPTY);
115
116         if (!stripe_cached) {
117                 sub = lov_sub_get(env, lio, page->cp_lov_index);
118                 if (IS_ERR(sub))
119                         RETURN(PTR_ERR(sub));
120         } else {
121                 sub = lio->lis_cached_sub;
122         }
123
124         lio->lis_cached_sub = sub;
125
126         r0 = lov_r0(loo, entry);
127         LASSERT(stripe < r0->lo_nr);
128
129         subobj = lovsub2cl(r0->lo_sub[stripe]);
130         cl_object_for_each(o, subobj) {
131                 if (o->co_ops->coo_page_init) {
132                         rc = o->co_ops->coo_page_init(sub->sub_env, o, page,
133                                                       suboff >> PAGE_SHIFT);
134                         if (rc != 0)
135                                 break;
136                 }
137         }
138
139         RETURN(rc);
140 }
141
142 int lov_page_init_empty(const struct lu_env *env, struct cl_object *obj,
143                         struct cl_page *cl_page, pgoff_t index)
144 {
145         void *addr;
146
147         ENTRY;
148         BUILD_BUG_ON(!__same_type(cl_page->cp_lov_index, CP_LOV_INDEX_EMPTY));
149         cl_page->cp_lov_index = CP_LOV_INDEX_EMPTY;
150
151         addr = kmap(cl_page->cp_vmpage);
152         memset(addr, 0, PAGE_SIZE);
153         kunmap(cl_page->cp_vmpage);
154         SetPageUptodate(cl_page->cp_vmpage);
155         RETURN(0);
156 }
157
158 int lov_page_init_foreign(const struct lu_env *env, struct cl_object *obj,
159                         struct cl_page *page, pgoff_t index)
160 {
161         CDEBUG(D_PAGE, DFID" has no data\n", PFID(lu_object_fid(&obj->co_lu)));
162         RETURN(-ENODATA);
163 }
164
165 /** @} lov */