Whamcloud - gitweb
LU-2955 tests: make replay-ost-single/8b SLOW for ZFS
[fs/lustre-release.git] / lustre / ofd / ofd_lvb.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.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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 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  * lustre/ofd/ofd_lvb.c
37  *
38  * Author: Mikhail Pershin <tappro@whamcloud.com>
39  * Author: Alexey Zhuravlev <bzzz@whamcloud.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_FILTER
43
44 #include "ofd_internal.h"
45
46 static int ofd_lvbo_free(struct ldlm_resource *res)
47 {
48         if (res->lr_lvb_data)
49                 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
50
51         return 0;
52 }
53
54 /* Called with res->lr_lvb_sem held */
55 static int ofd_lvbo_init(struct ldlm_resource *res)
56 {
57         struct ost_lvb          *lvb = NULL;
58         struct ofd_device       *ofd;
59         struct ofd_object       *fo;
60         struct ofd_thread_info  *info;
61         struct lu_env            env;
62         int                      rc = 0;
63
64         ENTRY;
65
66         LASSERT(res);
67         LASSERT_MUTEX_LOCKED(&res->lr_lvb_mutex);
68
69         if (res->lr_lvb_data != NULL)
70                 RETURN(0);
71
72         ofd = ldlm_res_to_ns(res)->ns_lvbp;
73         LASSERT(ofd != NULL);
74
75         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_OST_LVB))
76                 RETURN(-ENOMEM);
77
78         rc = lu_env_init(&env, LCT_DT_THREAD);
79         if (rc)
80                 RETURN(rc);
81
82         OBD_ALLOC_PTR(lvb);
83         if (lvb == NULL)
84                 GOTO(out, rc = -ENOMEM);
85
86         res->lr_lvb_data = lvb;
87         res->lr_lvb_len = sizeof(*lvb);
88
89         info = ofd_info_init(&env, NULL);
90         ofd_fid_from_resid(&info->fti_fid, &res->lr_name);
91         fo = ofd_object_find(&env, ofd, &info->fti_fid);
92         if (IS_ERR(fo))
93                 GOTO(out, rc = PTR_ERR(fo));
94
95         rc = ofd_attr_get(&env, fo, &info->fti_attr);
96         if (rc)
97                 GOTO(out_put, rc);
98
99         lvb->lvb_size = info->fti_attr.la_size;
100         lvb->lvb_blocks = info->fti_attr.la_blocks;
101         lvb->lvb_mtime = info->fti_attr.la_mtime;
102         lvb->lvb_atime = info->fti_attr.la_atime;
103         lvb->lvb_ctime = info->fti_attr.la_ctime;
104
105         CDEBUG(D_DLMTRACE, "res: "LPX64" initial lvb size: "LPX64", "
106                "mtime: "LPX64", blocks: "LPX64"\n",
107                res->lr_name.name[0], lvb->lvb_size,
108                lvb->lvb_mtime, lvb->lvb_blocks);
109
110         EXIT;
111 out_put:
112         ofd_object_put(&env, fo);
113 out:
114         lu_env_fini(&env);
115         if (rc && lvb != NULL)
116                 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
117         /* Don't free lvb data on lookup error */
118         return rc;
119 }
120
121 /* This will be called in two ways:
122  *
123  *   r != NULL : called by the DLM itself after a glimpse callback
124  *   r == NULL : called by the ofd after a disk write
125  *
126  *   If 'increase_only' is true, don't allow values to move backwards.
127  */
128 static int ofd_lvbo_update(struct ldlm_resource *res,
129                            struct ptlrpc_request *req, int increase_only)
130 {
131         struct ofd_device       *ofd;
132         struct ofd_object       *fo;
133         struct ofd_thread_info  *info;
134         struct ost_lvb          *lvb;
135         struct lu_env            env;
136         int                      rc = 0;
137
138         ENTRY;
139
140         LASSERT(res != NULL);
141
142         ofd = ldlm_res_to_ns(res)->ns_lvbp;
143         LASSERT(ofd != NULL);
144
145         lvb = res->lr_lvb_data;
146         if (lvb == NULL) {
147                 CERROR("%s: no lvb when running lvbo_update, res: "LPU64"!\n",
148                        ofd_obd(ofd)->obd_name, res->lr_name.name[0]);
149                 RETURN(0);
150         }
151
152         rc = lu_env_init(&env, LCT_DT_THREAD);
153         if (rc)
154                 GOTO(out_unlock, rc);
155
156         info = ofd_info_init(&env, NULL);
157         /* Update the LVB from the network message */
158         if (req != NULL) {
159                 struct ost_lvb *rpc_lvb;
160                 bool lvb_type;
161
162                 if (req->rq_import != NULL)
163                         lvb_type = imp_connect_lvb_type(req->rq_import);
164                 else
165                         lvb_type = exp_connect_lvb_type(req->rq_export);
166
167                 if (!lvb_type) {
168                         struct ost_lvb_v1 *lvb_v1;
169
170                         lvb_v1 = req_capsule_server_swab_get(&req->rq_pill,
171                                         &RMF_DLM_LVB, lustre_swab_ost_lvb_v1);
172                         if (lvb_v1 == NULL)
173                                 goto disk_update;
174
175                         rpc_lvb = &info->fti_lvb;
176                         memcpy(rpc_lvb, lvb_v1, sizeof *lvb_v1);
177                         rpc_lvb->lvb_mtime_ns = 0;
178                         rpc_lvb->lvb_atime_ns = 0;
179                         rpc_lvb->lvb_ctime_ns = 0;
180                 } else {
181                         rpc_lvb = req_capsule_server_swab_get(&req->rq_pill,
182                                                               &RMF_DLM_LVB,
183                                                         lustre_swab_ost_lvb);
184                         if (rpc_lvb == NULL)
185                                 goto disk_update;
186                 }
187
188                 lock_res(res);
189                 if (rpc_lvb->lvb_size > lvb->lvb_size || !increase_only) {
190                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
191                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
192                                lvb->lvb_size, rpc_lvb->lvb_size);
193                         lvb->lvb_size = rpc_lvb->lvb_size;
194                 }
195                 if (rpc_lvb->lvb_mtime > lvb->lvb_mtime || !increase_only) {
196                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
197                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
198                                lvb->lvb_mtime, rpc_lvb->lvb_mtime);
199                         lvb->lvb_mtime = rpc_lvb->lvb_mtime;
200                 }
201                 if (rpc_lvb->lvb_atime > lvb->lvb_atime || !increase_only) {
202                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime: "
203                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
204                                lvb->lvb_atime, rpc_lvb->lvb_atime);
205                         lvb->lvb_atime = rpc_lvb->lvb_atime;
206                 }
207                 if (rpc_lvb->lvb_ctime > lvb->lvb_ctime || !increase_only) {
208                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime: "
209                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
210                                lvb->lvb_ctime, rpc_lvb->lvb_ctime);
211                         lvb->lvb_ctime = rpc_lvb->lvb_ctime;
212                 }
213                 if (rpc_lvb->lvb_blocks > lvb->lvb_blocks || !increase_only) {
214                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb blocks: "
215                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
216                                lvb->lvb_blocks, rpc_lvb->lvb_blocks);
217                         lvb->lvb_blocks = rpc_lvb->lvb_blocks;
218                 }
219                 unlock_res(res);
220         }
221
222 disk_update:
223         /* Update the LVB from the disk inode */
224         ofd_fid_from_resid(&info->fti_fid, &res->lr_name);
225         fo = ofd_object_find(&env, ofd, &info->fti_fid);
226         if (IS_ERR(fo))
227                 GOTO(out_env, rc = PTR_ERR(fo));
228
229         rc = ofd_attr_get(&env, fo, &info->fti_attr);
230         if (rc)
231                 GOTO(out_obj, rc);
232
233         lock_res(res);
234         if (info->fti_attr.la_size > lvb->lvb_size || !increase_only) {
235                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
236                        LPU64" -> %llu\n", res->lr_name.name[0],
237                        lvb->lvb_size, info->fti_attr.la_size);
238                 lvb->lvb_size = info->fti_attr.la_size;
239         }
240
241         if (info->fti_attr.la_mtime >lvb->lvb_mtime || !increase_only) {
242                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
243                        LPU64" -> "LPU64"\n", res->lr_name.name[0],
244                        lvb->lvb_mtime, info->fti_attr.la_mtime);
245                 lvb->lvb_mtime = info->fti_attr.la_mtime;
246         }
247         if (info->fti_attr.la_atime >lvb->lvb_atime || !increase_only) {
248                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime from disk: "
249                        LPU64" -> "LPU64"\n", res->lr_name.name[0],
250                        lvb->lvb_atime, info->fti_attr.la_atime);
251                 lvb->lvb_atime = info->fti_attr.la_atime;
252         }
253         if (info->fti_attr.la_ctime >lvb->lvb_ctime || !increase_only) {
254                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime from disk: "
255                        LPU64" -> "LPU64"\n", res->lr_name.name[0],
256                        lvb->lvb_ctime, info->fti_attr.la_ctime);
257                 lvb->lvb_ctime = info->fti_attr.la_ctime;
258         }
259         if (info->fti_attr.la_blocks > lvb->lvb_blocks || !increase_only) {
260                 CDEBUG(D_DLMTRACE,"res: "LPU64" updating lvb blocks from disk: "
261                        LPU64" -> %llu\n", res->lr_name.name[0],
262                        lvb->lvb_blocks,
263                        (unsigned long long)info->fti_attr.la_blocks);
264                 lvb->lvb_blocks = info->fti_attr.la_blocks;
265         }
266         unlock_res(res);
267
268 out_obj:
269         ofd_object_put(&env, fo);
270 out_env:
271         lu_env_fini(&env);
272 out_unlock:
273         return rc;
274 }
275
276 static int ofd_lvbo_size(struct ldlm_lock *lock)
277 {
278         if (lock->l_export != NULL && exp_connect_lvb_type(lock->l_export))
279                 return sizeof(struct ost_lvb);
280         else
281                 return sizeof(struct ost_lvb_v1);
282 }
283
284 static int ofd_lvbo_fill(struct ldlm_lock *lock, void *buf, int buflen)
285 {
286         struct ldlm_resource *res = lock->l_resource;
287         int lvb_len;
288
289         /* Former lvbo_init not allocate the "LVB". */
290         if (unlikely(res->lr_lvb_len == 0))
291                 return 0;
292
293         lvb_len = ofd_lvbo_size(lock);
294         LASSERT(lvb_len <= res->lr_lvb_len);
295
296         if (lvb_len > buflen)
297                 lvb_len = buflen;
298
299         lock_res(res);
300         memcpy(buf, res->lr_lvb_data, lvb_len);
301         unlock_res(res);
302
303         return lvb_len;
304 }
305
306 struct ldlm_valblock_ops ofd_lvbo = {
307         lvbo_init:      ofd_lvbo_init,
308         lvbo_update:    ofd_lvbo_update,
309         lvbo_free:      ofd_lvbo_free,
310         lvbo_size:      ofd_lvbo_size,
311         lvbo_fill:      ofd_lvbo_fill
312 };