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