Whamcloud - gitweb
LU-3336 lfsck: use rbtree to record OST-object accessing
[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, 2013, 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;
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_is_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_env, 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         ost_fid_from_resid(&info->fti_fid, &res->lr_name,
91                            ofd->ofd_lut.lut_lsd.lsd_osd_index);
92         fo = ofd_object_find(&env, ofd, &info->fti_fid);
93         if (IS_ERR(fo))
94                 GOTO(out_lvb, rc = PTR_ERR(fo));
95
96         rc = ofd_attr_get(&env, fo, &info->fti_attr);
97         if (rc)
98                 GOTO(out_obj, rc);
99
100         lvb->lvb_size = info->fti_attr.la_size;
101         lvb->lvb_blocks = info->fti_attr.la_blocks;
102         lvb->lvb_mtime = info->fti_attr.la_mtime;
103         lvb->lvb_atime = info->fti_attr.la_atime;
104         lvb->lvb_ctime = info->fti_attr.la_ctime;
105
106         CDEBUG(D_DLMTRACE, "res: "DFID" initial lvb size: "LPU64", "
107                "mtime: "LPX64", blocks: "LPX64"\n",
108                PFID(&info->fti_fid), lvb->lvb_size,
109                lvb->lvb_mtime, lvb->lvb_blocks);
110
111         EXIT;
112 out_obj:
113         ofd_object_put(&env, fo);
114 out_lvb:
115         if (rc != 0)
116                 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
117 out_env:
118         lu_env_fini(&env);
119         /* Don't free lvb data on lookup error */
120         return rc;
121 }
122
123 /* This will be called in two ways:
124  *
125  *   r != NULL : called by the DLM itself after a glimpse callback
126  *   r == NULL : called by the ofd after a disk write
127  *
128  *   If 'increase_only' is true, don't allow values to move backwards.
129  */
130 static int ofd_lvbo_update(struct ldlm_resource *res,
131                            struct ptlrpc_request *req, int increase_only)
132 {
133         struct ofd_device       *ofd;
134         struct ofd_object       *fo;
135         struct ofd_thread_info  *info;
136         struct ost_lvb          *lvb;
137         struct lu_env            env;
138         int                      rc = 0;
139
140         ENTRY;
141
142         LASSERT(res != NULL);
143
144         ofd = ldlm_res_to_ns(res)->ns_lvbp;
145         LASSERT(ofd != NULL);
146
147         rc = lu_env_init(&env, LCT_DT_THREAD);
148         if (rc)
149                 RETURN(rc);
150
151         info = ofd_info_init(&env, NULL);
152         fid_extract_from_res_name(&info->fti_fid, &res->lr_name);
153
154         lvb = res->lr_lvb_data;
155         if (lvb == NULL) {
156                 CERROR("%s: no LVB data for "DFID"\n",
157                        ofd_obd(ofd)->obd_name, PFID(&info->fti_fid));
158                 GOTO(out_env, rc = 0);
159         }
160
161         /* Update the LVB from the network message */
162         if (req != NULL) {
163                 struct ost_lvb *rpc_lvb;
164                 bool lvb_type;
165
166                 if (req->rq_import != NULL)
167                         lvb_type = imp_connect_lvb_type(req->rq_import);
168                 else
169                         lvb_type = exp_connect_lvb_type(req->rq_export);
170
171                 if (!lvb_type) {
172                         struct ost_lvb_v1 *lvb_v1;
173
174                         lvb_v1 = req_capsule_server_swab_get(&req->rq_pill,
175                                         &RMF_DLM_LVB, lustre_swab_ost_lvb_v1);
176                         if (lvb_v1 == NULL)
177                                 goto disk_update;
178
179                         rpc_lvb = &info->fti_lvb;
180                         memcpy(rpc_lvb, lvb_v1, sizeof *lvb_v1);
181                         rpc_lvb->lvb_mtime_ns = 0;
182                         rpc_lvb->lvb_atime_ns = 0;
183                         rpc_lvb->lvb_ctime_ns = 0;
184                 } else {
185                         rpc_lvb = req_capsule_server_swab_get(&req->rq_pill,
186                                                               &RMF_DLM_LVB,
187                                                         lustre_swab_ost_lvb);
188                         if (rpc_lvb == NULL)
189                                 goto disk_update;
190                 }
191
192                 lock_res(res);
193                 if (rpc_lvb->lvb_size > lvb->lvb_size || !increase_only) {
194                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb size: "
195                                LPU64" -> "LPU64"\n", PFID(&info->fti_fid),
196                                lvb->lvb_size, rpc_lvb->lvb_size);
197                         lvb->lvb_size = rpc_lvb->lvb_size;
198                 }
199                 if (rpc_lvb->lvb_mtime > lvb->lvb_mtime || !increase_only) {
200                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb mtime: "
201                                LPU64" -> "LPU64"\n", PFID(&info->fti_fid),
202                                lvb->lvb_mtime, rpc_lvb->lvb_mtime);
203                         lvb->lvb_mtime = rpc_lvb->lvb_mtime;
204                 }
205                 if (rpc_lvb->lvb_atime > lvb->lvb_atime || !increase_only) {
206                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb atime: "
207                                LPU64" -> "LPU64"\n", PFID(&info->fti_fid),
208                                lvb->lvb_atime, rpc_lvb->lvb_atime);
209                         lvb->lvb_atime = rpc_lvb->lvb_atime;
210                 }
211                 if (rpc_lvb->lvb_ctime > lvb->lvb_ctime || !increase_only) {
212                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb ctime: "
213                                LPU64" -> "LPU64"\n", PFID(&info->fti_fid),
214                                lvb->lvb_ctime, rpc_lvb->lvb_ctime);
215                         lvb->lvb_ctime = rpc_lvb->lvb_ctime;
216                 }
217                 if (rpc_lvb->lvb_blocks > lvb->lvb_blocks || !increase_only) {
218                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb blocks: "
219                                LPU64" -> "LPU64"\n", PFID(&info->fti_fid),
220                                lvb->lvb_blocks, rpc_lvb->lvb_blocks);
221                         lvb->lvb_blocks = rpc_lvb->lvb_blocks;
222                 }
223                 unlock_res(res);
224         }
225
226 disk_update:
227         /* Update the LVB from the disk inode */
228         ost_fid_from_resid(&info->fti_fid, &res->lr_name,
229                            ofd->ofd_lut.lut_lsd.lsd_osd_index);
230         fo = ofd_object_find(&env, ofd, &info->fti_fid);
231         if (IS_ERR(fo))
232                 GOTO(out_env, rc = PTR_ERR(fo));
233
234         rc = ofd_attr_get(&env, fo, &info->fti_attr);
235         if (rc)
236                 GOTO(out_obj, rc);
237
238         lock_res(res);
239         if (info->fti_attr.la_size > lvb->lvb_size || !increase_only) {
240                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb size from disk: "
241                        LPU64" -> %llu\n", PFID(&info->fti_fid),
242                        lvb->lvb_size, info->fti_attr.la_size);
243                 lvb->lvb_size = info->fti_attr.la_size;
244         }
245
246         if (info->fti_attr.la_mtime >lvb->lvb_mtime || !increase_only) {
247                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb mtime from disk: "
248                        LPU64" -> "LPU64"\n", PFID(&info->fti_fid),
249                        lvb->lvb_mtime, info->fti_attr.la_mtime);
250                 lvb->lvb_mtime = info->fti_attr.la_mtime;
251         }
252         if (info->fti_attr.la_atime >lvb->lvb_atime || !increase_only) {
253                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb atime from disk: "
254                        LPU64" -> "LPU64"\n", PFID(&info->fti_fid),
255                        lvb->lvb_atime, info->fti_attr.la_atime);
256                 lvb->lvb_atime = info->fti_attr.la_atime;
257         }
258         if (info->fti_attr.la_ctime >lvb->lvb_ctime || !increase_only) {
259                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb ctime from disk: "
260                        LPU64" -> "LPU64"\n", PFID(&info->fti_fid),
261                        lvb->lvb_ctime, info->fti_attr.la_ctime);
262                 lvb->lvb_ctime = info->fti_attr.la_ctime;
263         }
264         if (info->fti_attr.la_blocks > lvb->lvb_blocks || !increase_only) {
265                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb blocks from disk: "
266                        LPU64" -> %llu\n", PFID(&info->fti_fid), lvb->lvb_blocks,
267                        (unsigned long long)info->fti_attr.la_blocks);
268                 lvb->lvb_blocks = info->fti_attr.la_blocks;
269         }
270         unlock_res(res);
271
272 out_obj:
273         ofd_object_put(&env, fo);
274 out_env:
275         lu_env_fini(&env);
276         return rc;
277 }
278
279 static int ofd_lvbo_size(struct ldlm_lock *lock)
280 {
281         if (lock->l_export != NULL && exp_connect_lvb_type(lock->l_export))
282                 return sizeof(struct ost_lvb);
283         else
284                 return sizeof(struct ost_lvb_v1);
285 }
286
287 static int ofd_lvbo_fill(struct ldlm_lock *lock, void *buf, int buflen)
288 {
289         struct ldlm_resource *res = lock->l_resource;
290         int lvb_len;
291
292         /* Former lvbo_init not allocate the "LVB". */
293         if (unlikely(res->lr_lvb_len == 0))
294                 return 0;
295
296         lvb_len = ofd_lvbo_size(lock);
297         LASSERT(lvb_len <= res->lr_lvb_len);
298
299         if (lvb_len > buflen)
300                 lvb_len = buflen;
301
302         lock_res(res);
303         memcpy(buf, res->lr_lvb_data, lvb_len);
304         unlock_res(res);
305
306         return lvb_len;
307 }
308
309 struct ldlm_valblock_ops ofd_lvbo = {
310         .lvbo_init      = ofd_lvbo_init,
311         .lvbo_update    = ofd_lvbo_update,
312         .lvbo_free      = ofd_lvbo_free,
313         .lvbo_size      = ofd_lvbo_size,
314         .lvbo_fill      = ofd_lvbo_fill
315 };