4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ofd/ofd_lvb.c
34 * This file contains methods for OBD Filter Device (OFD)
35 * Lock Value Block (LVB) operations.
37 * LVB is special opaque (to LDLM) data that is associated with an LDLM lock
38 * and transferred from client to server and back. OFD LVBs are used to
39 * maintain current object size/times.
41 * Author: Andreas Dilger <andreas.dilger@intel.com>
42 * Author: Mikhail Pershin <mike.pershin@intel.com>
43 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
46 #define DEBUG_SUBSYSTEM S_FILTER
48 #include <lustre_swab.h>
49 #include "ofd_internal.h"
52 * Implementation of ldlm_valblock_ops::lvbo_free for OFD.
54 * This function frees allocated LVB data if it associated with the given
57 * \param[in] res LDLM resource
59 * \retval 0 on successful setup
60 * \retval negative value on error
62 static int ofd_lvbo_free(struct ldlm_resource *res)
65 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
71 * Implementation of ldlm_valblock_ops::lvbo_init for OFD.
73 * This function allocates and initializes new LVB data for the given
74 * LDLM resource if it is not allocated yet. New LVB is filled with attributes
75 * of the object associated with that resource. Function does nothing if LVB
76 * for the given LDLM resource is allocated already.
78 * Called with res->lr_lvb_sem held.
80 * \param[in] lock LDLM lock on resource
82 * \retval 0 on successful setup
83 * \retval negative value on error
85 static int ofd_lvbo_init(const struct lu_env *env, struct ldlm_resource *res)
88 struct ofd_device *ofd;
89 struct ofd_object *fo;
90 struct ofd_thread_info *info;
96 LASSERT(mutex_is_locked(&res->lr_lvb_mutex));
98 if (res->lr_lvb_data != NULL)
101 ofd = ldlm_res_to_ns(res)->ns_lvbp;
102 LASSERT(ofd != NULL);
104 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_OST_LVB))
108 rc = lu_env_init(&_env, LCT_DT_THREAD);
116 GOTO(out, rc = -ENOMEM);
118 info = ofd_info(env);
119 res->lr_lvb_data = lvb;
120 res->lr_lvb_len = sizeof(*lvb);
122 ost_fid_from_resid(&info->fti_fid, &res->lr_name,
123 ofd->ofd_lut.lut_lsd.lsd_osd_index);
124 fo = ofd_object_find(env, ofd, &info->fti_fid);
126 GOTO(out_lvb, rc = PTR_ERR(fo));
128 rc = ofd_attr_get(env, fo, &info->fti_attr);
130 struct ofd_seq *oseq;
133 /* Object could be recreated during the first
134 * CLEANUP_ORPHAN request. */
136 seq = fid_seq(&info->fti_fid);
137 oseq = ofd_seq_load(env, ofd, fid_seq_is_idif(seq) ?
138 FID_SEQ_OST_MDT0 : seq);
139 if (!IS_ERR_OR_NULL(oseq)) {
140 if (!oseq->os_last_id_synced)
142 ofd_seq_put(env, oseq);
148 lvb->lvb_size = info->fti_attr.la_size;
149 lvb->lvb_blocks = info->fti_attr.la_blocks;
150 lvb->lvb_mtime = info->fti_attr.la_mtime;
151 lvb->lvb_atime = info->fti_attr.la_atime;
152 lvb->lvb_ctime = info->fti_attr.la_ctime;
154 CDEBUG(D_DLMTRACE, "res: "DFID" initial lvb size: %llu, "
155 "mtime: %#llx, blocks: %#llx\n",
156 PFID(&info->fti_fid), lvb->lvb_size,
157 lvb->lvb_mtime, lvb->lvb_blocks);
159 info->fti_attr.la_valid = 0;
163 ofd_object_put(env, fo);
166 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
168 /* Don't free lvb data on lookup error */
169 if (env && env == &_env)
175 * Implementation of ldlm_valblock_ops::lvbo_update for OFD.
177 * When a client generates a glimpse enqueue, it wants to get the current
178 * file size and updated attributes for a stat() type operation, but these
179 * attributes may be writeback cached on another client. The client with
180 * the DLM extent lock at the highest offset is asked for its current
181 * attributes via a glimpse callback on its extent lock, on the assumption
182 * that it has the highest file size and the newest timestamps. The timestamps
183 * are guaranteed to be correct if there is only a single writer on the file,
184 * but may be slightly inaccurate if there are multiple concurrent writers on
185 * the same object. In order to avoid race conditions between the glimpse AST
186 * and the client cancelling the lock, ofd_lvbo_update() also updates
187 * the attributes from the local object. If the last client hasn't done any
188 * writes yet, or has already written its data and cancelled its lock before
189 * it processed the glimpse, then the local inode will have more uptodate
192 * This is called in two ways:
193 * \a req != NULL : called by the DLM itself after a glimpse callback
194 * \a req == NULL : called by the OFD after a disk write
196 * \param[in] lock LDLM lock
197 * \param[in] req PTLRPC request
198 * \param[in] increase_only don't allow LVB values to decrease
200 * \retval 0 on successful setup
201 * \retval negative value on error
203 static int ofd_lvbo_update(const struct lu_env *env, struct ldlm_resource *res,
204 struct ldlm_lock *lock, struct ptlrpc_request *req,
207 struct ofd_thread_info *info;
208 struct ofd_device *ofd;
209 struct ofd_object *fo;
216 info = ofd_info(env);
217 LASSERT(res != NULL);
219 ofd = ldlm_res_to_ns(res)->ns_lvbp;
220 LASSERT(ofd != NULL);
222 fid_extract_from_res_name(&info->fti_fid, &res->lr_name);
224 lvb = res->lr_lvb_data;
226 CERROR("%s: no LVB data for "DFID"\n",
227 ofd_name(ofd), PFID(&info->fti_fid));
231 /* Update the LVB from the network message */
233 struct ost_lvb *rpc_lvb;
236 if (req->rq_import != NULL)
237 lvb_type = imp_connect_lvb_type(req->rq_import);
239 lvb_type = exp_connect_lvb_type(req->rq_export);
242 struct ost_lvb_v1 *lvb_v1;
244 lvb_v1 = req_capsule_server_swab_get(&req->rq_pill,
245 &RMF_DLM_LVB, lustre_swab_ost_lvb_v1);
249 rpc_lvb = &info->fti_lvb;
250 memcpy(rpc_lvb, lvb_v1, sizeof *lvb_v1);
251 rpc_lvb->lvb_mtime_ns = 0;
252 rpc_lvb->lvb_atime_ns = 0;
253 rpc_lvb->lvb_ctime_ns = 0;
255 rpc_lvb = req_capsule_server_swab_get(&req->rq_pill,
257 lustre_swab_ost_lvb);
263 if (rpc_lvb->lvb_size > lvb->lvb_size || !increase_only) {
264 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb size: "
265 "%llu -> %llu\n", PFID(&info->fti_fid),
266 lvb->lvb_size, rpc_lvb->lvb_size);
267 lvb->lvb_size = rpc_lvb->lvb_size;
269 if (rpc_lvb->lvb_mtime > lvb->lvb_mtime || !increase_only) {
270 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb mtime: "
271 "%llu -> %llu\n", PFID(&info->fti_fid),
272 lvb->lvb_mtime, rpc_lvb->lvb_mtime);
273 lvb->lvb_mtime = rpc_lvb->lvb_mtime;
275 if (rpc_lvb->lvb_atime > lvb->lvb_atime || !increase_only) {
276 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb atime: "
277 "%llu -> %llu\n", PFID(&info->fti_fid),
278 lvb->lvb_atime, rpc_lvb->lvb_atime);
279 lvb->lvb_atime = rpc_lvb->lvb_atime;
281 if (rpc_lvb->lvb_ctime > lvb->lvb_ctime || !increase_only) {
282 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb ctime: "
283 "%llu -> %llu\n", PFID(&info->fti_fid),
284 lvb->lvb_ctime, rpc_lvb->lvb_ctime);
285 lvb->lvb_ctime = rpc_lvb->lvb_ctime;
287 if (rpc_lvb->lvb_blocks > lvb->lvb_blocks || !increase_only) {
288 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb blocks: "
289 "%llu -> %llu\n", PFID(&info->fti_fid),
290 lvb->lvb_blocks, rpc_lvb->lvb_blocks);
291 lvb->lvb_blocks = rpc_lvb->lvb_blocks;
297 /* Update the LVB from the disk inode */
298 ost_fid_from_resid(&info->fti_fid, &res->lr_name,
299 ofd->ofd_lut.lut_lsd.lsd_osd_index);
300 fo = ofd_object_find(env, ofd, &info->fti_fid);
302 GOTO(out, rc = PTR_ERR(fo));
304 rc = ofd_attr_get(env, fo, &info->fti_attr);
309 if (info->fti_attr.la_size > lvb->lvb_size || !increase_only) {
310 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb size from disk: "
311 "%llu -> %llu\n", PFID(&info->fti_fid),
312 lvb->lvb_size, info->fti_attr.la_size);
313 lvb->lvb_size = info->fti_attr.la_size;
316 if (info->fti_attr.la_mtime >lvb->lvb_mtime || !increase_only) {
317 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb mtime from disk: "
318 "%llu -> %llu\n", PFID(&info->fti_fid),
319 lvb->lvb_mtime, info->fti_attr.la_mtime);
320 lvb->lvb_mtime = info->fti_attr.la_mtime;
322 if (info->fti_attr.la_atime >lvb->lvb_atime || !increase_only) {
323 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb atime from disk: "
324 "%llu -> %llu\n", PFID(&info->fti_fid),
325 lvb->lvb_atime, info->fti_attr.la_atime);
326 lvb->lvb_atime = info->fti_attr.la_atime;
328 if (info->fti_attr.la_ctime >lvb->lvb_ctime || !increase_only) {
329 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb ctime from disk: "
330 "%llu -> %llu\n", PFID(&info->fti_fid),
331 lvb->lvb_ctime, info->fti_attr.la_ctime);
332 lvb->lvb_ctime = info->fti_attr.la_ctime;
334 if (info->fti_attr.la_blocks > lvb->lvb_blocks || !increase_only) {
335 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb blocks from disk: "
336 "%llu -> %llu\n", PFID(&info->fti_fid), lvb->lvb_blocks,
337 (unsigned long long)info->fti_attr.la_blocks);
338 lvb->lvb_blocks = info->fti_attr.la_blocks;
343 ofd_object_put(env, fo);
349 * Implementation of ldlm_valblock_ops::lvbo_size for OFD.
351 * This function returns size of LVB data so appropriate RPC size will be
352 * reserved. This is used for compatibility needs between server and client
353 * of different Lustre versions.
355 * \param[in] lock LDLM lock
357 * \retval size of LVB data
359 static int ofd_lvbo_size(struct ldlm_lock *lock)
361 if (lock->l_export != NULL && exp_connect_lvb_type(lock->l_export))
362 return sizeof(struct ost_lvb);
364 return sizeof(struct ost_lvb_v1);
368 * Implementation of ldlm_valblock_ops::lvbo_fill for OFD.
370 * This function is called to fill the given RPC buffer \a buf with LVB data
372 * \param[in] env execution environment
373 * \param[in] lock LDLM lock
374 * \param[in] buf RPC buffer to fill
375 * \param[in] buflen buffer length
377 * \retval size of LVB data written into \a buf buffer
379 static int ofd_lvbo_fill(const struct lu_env *env, struct ldlm_lock *lock,
380 void *buf, int *buflen)
382 struct ldlm_resource *res = lock->l_resource;
385 /* Former lvbo_init not allocate the "LVB". */
386 if (unlikely(res->lr_lvb_len == 0))
389 lvb_len = ofd_lvbo_size(lock);
390 LASSERT(lvb_len <= res->lr_lvb_len);
392 if (lvb_len > *buflen)
396 memcpy(buf, res->lr_lvb_data, lvb_len);
402 struct ldlm_valblock_ops ofd_lvbo = {
403 .lvbo_init = ofd_lvbo_init,
404 .lvbo_update = ofd_lvbo_update,
405 .lvbo_free = ofd_lvbo_free,
406 .lvbo_size = ofd_lvbo_size,
407 .lvbo_fill = ofd_lvbo_fill