Whamcloud - gitweb
LU-4684 migrate: replace PFID via source
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ofd/ofd_lvb.c
33  *
34  * This file contains methods for OBD Filter Device (OFD)
35  * Lock Value Block (LVB) operations.
36  *
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.
40  *
41  * Author: Andreas Dilger <andreas.dilger@intel.com>
42  * Author: Mikhail Pershin <mike.pershin@intel.com>
43  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
44  */
45
46 #define DEBUG_SUBSYSTEM S_FILTER
47
48 #include <lustre_swab.h>
49 #include "ofd_internal.h"
50
51 /**
52  * Implementation of ldlm_valblock_ops::lvbo_free for OFD.
53  *
54  * This function frees allocated LVB data if it associated with the given
55  * LDLM resource.
56  *
57  * \param[in] res       LDLM resource
58  *
59  * \retval              0 on successful setup
60  * \retval              negative value on error
61  */
62 static int ofd_lvbo_free(struct ldlm_resource *res)
63 {
64         if (res->lr_lvb_data)
65                 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
66
67         return 0;
68 }
69
70 /**
71  * Implementation of ldlm_valblock_ops::lvbo_init for OFD.
72  *
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.
77  *
78  * Called with res->lr_lvb_sem held.
79  *
80  * \param[in] lock      LDLM lock on resource
81  *
82  * \retval              0 on successful setup
83  * \retval              negative value on error
84  */
85 static int ofd_lvbo_init(const struct lu_env *env, struct ldlm_resource *res)
86 {
87         struct ost_lvb          *lvb;
88         struct ofd_device       *ofd;
89         struct ofd_object       *fo;
90         struct ofd_thread_info  *info;
91         int                      rc = 0;
92
93         ENTRY;
94
95         LASSERT(env);
96         info = ofd_info(env);
97         LASSERT(res);
98         LASSERT(mutex_is_locked(&res->lr_lvb_mutex));
99
100         if (res->lr_lvb_data != NULL)
101                 RETURN(0);
102
103         ofd = ldlm_res_to_ns(res)->ns_lvbp;
104         LASSERT(ofd != NULL);
105
106         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_OST_LVB))
107                 RETURN(-ENOMEM);
108
109         OBD_ALLOC_PTR(lvb);
110         if (lvb == NULL)
111                 GOTO(out, rc = -ENOMEM);
112
113         res->lr_lvb_data = lvb;
114         res->lr_lvb_len = sizeof(*lvb);
115
116         ost_fid_from_resid(&info->fti_fid, &res->lr_name,
117                            ofd->ofd_lut.lut_lsd.lsd_osd_index);
118         fo = ofd_object_find(env, ofd, &info->fti_fid);
119         if (IS_ERR(fo))
120                 GOTO(out_lvb, rc = PTR_ERR(fo));
121
122         rc = ofd_attr_get(env, fo, &info->fti_attr);
123         if (rc)
124                 GOTO(out_obj, rc);
125
126         lvb->lvb_size = info->fti_attr.la_size;
127         lvb->lvb_blocks = info->fti_attr.la_blocks;
128         lvb->lvb_mtime = info->fti_attr.la_mtime;
129         lvb->lvb_atime = info->fti_attr.la_atime;
130         lvb->lvb_ctime = info->fti_attr.la_ctime;
131
132         CDEBUG(D_DLMTRACE, "res: "DFID" initial lvb size: %llu, "
133                "mtime: %#llx, blocks: %#llx\n",
134                PFID(&info->fti_fid), lvb->lvb_size,
135                lvb->lvb_mtime, lvb->lvb_blocks);
136
137         info->fti_attr.la_valid = 0;
138
139         EXIT;
140 out_obj:
141         ofd_object_put(env, fo);
142 out_lvb:
143         if (rc != 0)
144                 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
145 out:
146         /* Don't free lvb data on lookup error */
147         return rc;
148 }
149
150 /**
151  * Implementation of ldlm_valblock_ops::lvbo_update for OFD.
152  *
153  * When a client generates a glimpse enqueue, it wants to get the current
154  * file size and updated attributes for a stat() type operation, but these
155  * attributes may be writeback cached on another client. The client with
156  * the DLM extent lock at the highest offset is asked for its current
157  * attributes via a glimpse callback on its extent lock, on the assumption
158  * that it has the highest file size and the newest timestamps. The timestamps
159  * are guaranteed to be correct if there is only a single writer on the file,
160  * but may be slightly inaccurate if there are multiple concurrent writers on
161  * the same object. In order to avoid race conditions between the glimpse AST
162  * and the client cancelling the lock, ofd_lvbo_update() also updates
163  * the attributes from the local object. If the last client hasn't done any
164  * writes yet, or has already written its data and cancelled its lock before
165  * it processed the glimpse, then the local inode will have more uptodate
166  * information.
167  *
168  * This is called in two ways:
169  *  \a req != NULL : called by the DLM itself after a glimpse callback
170  *  \a req == NULL : called by the OFD after a disk write
171  *
172  * \param[in] lock              LDLM lock
173  * \param[in] req               PTLRPC request
174  * \param[in] increase_only     don't allow LVB values to decrease
175  *
176  * \retval              0 on successful setup
177  * \retval              negative value on error
178  */
179 static int ofd_lvbo_update(const struct lu_env *env, struct ldlm_resource *res,
180                            struct ldlm_lock *lock, struct ptlrpc_request *req,
181                            int increase_only)
182 {
183         struct ofd_thread_info  *info;
184         struct ofd_device       *ofd;
185         struct ofd_object       *fo;
186         struct ost_lvb          *lvb;
187         int                      rc = 0;
188
189         ENTRY;
190
191         LASSERT(env);
192         info = ofd_info(env);
193         LASSERT(res != NULL);
194
195         ofd = ldlm_res_to_ns(res)->ns_lvbp;
196         LASSERT(ofd != NULL);
197
198         fid_extract_from_res_name(&info->fti_fid, &res->lr_name);
199
200         lvb = res->lr_lvb_data;
201         if (lvb == NULL) {
202                 CERROR("%s: no LVB data for "DFID"\n",
203                        ofd_name(ofd), PFID(&info->fti_fid));
204                 GOTO(out, rc = 0);
205         }
206
207         /* Update the LVB from the network message */
208         if (req != NULL) {
209                 struct ost_lvb *rpc_lvb;
210                 bool lvb_type;
211
212                 if (req->rq_import != NULL)
213                         lvb_type = imp_connect_lvb_type(req->rq_import);
214                 else
215                         lvb_type = exp_connect_lvb_type(req->rq_export);
216
217                 if (!lvb_type) {
218                         struct ost_lvb_v1 *lvb_v1;
219
220                         lvb_v1 = req_capsule_server_swab_get(&req->rq_pill,
221                                         &RMF_DLM_LVB, lustre_swab_ost_lvb_v1);
222                         if (lvb_v1 == NULL)
223                                 goto disk_update;
224
225                         rpc_lvb = &info->fti_lvb;
226                         memcpy(rpc_lvb, lvb_v1, sizeof *lvb_v1);
227                         rpc_lvb->lvb_mtime_ns = 0;
228                         rpc_lvb->lvb_atime_ns = 0;
229                         rpc_lvb->lvb_ctime_ns = 0;
230                 } else {
231                         rpc_lvb = req_capsule_server_swab_get(&req->rq_pill,
232                                                               &RMF_DLM_LVB,
233                                                         lustre_swab_ost_lvb);
234                         if (rpc_lvb == NULL)
235                                 goto disk_update;
236                 }
237
238                 lock_res(res);
239                 if (rpc_lvb->lvb_size > lvb->lvb_size || !increase_only) {
240                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb size: "
241                                "%llu -> %llu\n", PFID(&info->fti_fid),
242                                lvb->lvb_size, rpc_lvb->lvb_size);
243                         lvb->lvb_size = rpc_lvb->lvb_size;
244                 }
245                 if (rpc_lvb->lvb_mtime > lvb->lvb_mtime || !increase_only) {
246                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb mtime: "
247                                "%llu -> %llu\n", PFID(&info->fti_fid),
248                                lvb->lvb_mtime, rpc_lvb->lvb_mtime);
249                         lvb->lvb_mtime = rpc_lvb->lvb_mtime;
250                 }
251                 if (rpc_lvb->lvb_atime > lvb->lvb_atime || !increase_only) {
252                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb atime: "
253                                "%llu -> %llu\n", PFID(&info->fti_fid),
254                                lvb->lvb_atime, rpc_lvb->lvb_atime);
255                         lvb->lvb_atime = rpc_lvb->lvb_atime;
256                 }
257                 if (rpc_lvb->lvb_ctime > lvb->lvb_ctime || !increase_only) {
258                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb ctime: "
259                                "%llu -> %llu\n", PFID(&info->fti_fid),
260                                lvb->lvb_ctime, rpc_lvb->lvb_ctime);
261                         lvb->lvb_ctime = rpc_lvb->lvb_ctime;
262                 }
263                 if (rpc_lvb->lvb_blocks > lvb->lvb_blocks || !increase_only) {
264                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb blocks: "
265                                "%llu -> %llu\n", PFID(&info->fti_fid),
266                                lvb->lvb_blocks, rpc_lvb->lvb_blocks);
267                         lvb->lvb_blocks = rpc_lvb->lvb_blocks;
268                 }
269                 unlock_res(res);
270         }
271
272 disk_update:
273         /* Update the LVB from the disk inode */
274         ost_fid_from_resid(&info->fti_fid, &res->lr_name,
275                            ofd->ofd_lut.lut_lsd.lsd_osd_index);
276         fo = ofd_object_find(env, ofd, &info->fti_fid);
277         if (IS_ERR(fo))
278                 GOTO(out, rc = PTR_ERR(fo));
279
280         rc = ofd_attr_get(env, fo, &info->fti_attr);
281         if (rc)
282                 GOTO(out_obj, rc);
283
284         lock_res(res);
285         if (info->fti_attr.la_size > lvb->lvb_size || !increase_only) {
286                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb size from disk: "
287                        "%llu -> %llu\n", PFID(&info->fti_fid),
288                        lvb->lvb_size, info->fti_attr.la_size);
289                 lvb->lvb_size = info->fti_attr.la_size;
290         }
291
292         if (info->fti_attr.la_mtime >lvb->lvb_mtime || !increase_only) {
293                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb mtime from disk: "
294                        "%llu -> %llu\n", PFID(&info->fti_fid),
295                        lvb->lvb_mtime, info->fti_attr.la_mtime);
296                 lvb->lvb_mtime = info->fti_attr.la_mtime;
297         }
298         if (info->fti_attr.la_atime >lvb->lvb_atime || !increase_only) {
299                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb atime from disk: "
300                        "%llu -> %llu\n", PFID(&info->fti_fid),
301                        lvb->lvb_atime, info->fti_attr.la_atime);
302                 lvb->lvb_atime = info->fti_attr.la_atime;
303         }
304         if (info->fti_attr.la_ctime >lvb->lvb_ctime || !increase_only) {
305                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb ctime from disk: "
306                        "%llu -> %llu\n", PFID(&info->fti_fid),
307                        lvb->lvb_ctime, info->fti_attr.la_ctime);
308                 lvb->lvb_ctime = info->fti_attr.la_ctime;
309         }
310         if (info->fti_attr.la_blocks > lvb->lvb_blocks || !increase_only) {
311                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb blocks from disk: "
312                        "%llu -> %llu\n", PFID(&info->fti_fid), lvb->lvb_blocks,
313                        (unsigned long long)info->fti_attr.la_blocks);
314                 lvb->lvb_blocks = info->fti_attr.la_blocks;
315         }
316         unlock_res(res);
317
318 out_obj:
319         ofd_object_put(env, fo);
320 out:
321         return rc;
322 }
323
324 /**
325  * Implementation of ldlm_valblock_ops::lvbo_size for OFD.
326  *
327  * This function returns size of LVB data so appropriate RPC size will be
328  * reserved. This is used for compatibility needs between server and client
329  * of different Lustre versions.
330  *
331  * \param[in] lock      LDLM lock
332  *
333  * \retval              size of LVB data
334  */
335 static int ofd_lvbo_size(struct ldlm_lock *lock)
336 {
337         if (lock->l_export != NULL && exp_connect_lvb_type(lock->l_export))
338                 return sizeof(struct ost_lvb);
339         else
340                 return sizeof(struct ost_lvb_v1);
341 }
342
343 /**
344  * Implementation of ldlm_valblock_ops::lvbo_fill for OFD.
345  *
346  * This function is called to fill the given RPC buffer \a buf with LVB data
347  *
348  * \param[in] lock      LDLM lock
349  * \param[in] buf       RPC buffer to fill
350  * \param[in] buflen    buffer length
351  *
352  * \retval              size of LVB data written into \a buf buffer
353  */
354 static int ofd_lvbo_fill(const struct lu_env *env, struct ldlm_lock *lock,
355                          void *buf, int buflen)
356 {
357         struct ldlm_resource *res = lock->l_resource;
358         int lvb_len;
359
360         /* Former lvbo_init not allocate the "LVB". */
361         if (unlikely(res->lr_lvb_len == 0))
362                 return 0;
363
364         lvb_len = ofd_lvbo_size(lock);
365         LASSERT(lvb_len <= res->lr_lvb_len);
366
367         if (lvb_len > buflen)
368                 lvb_len = buflen;
369
370         lock_res(res);
371         memcpy(buf, res->lr_lvb_data, lvb_len);
372         unlock_res(res);
373
374         return lvb_len;
375 }
376
377 struct ldlm_valblock_ops ofd_lvbo = {
378         .lvbo_init      = ofd_lvbo_init,
379         .lvbo_update    = ofd_lvbo_update,
380         .lvbo_free      = ofd_lvbo_free,
381         .lvbo_size      = ofd_lvbo_size,
382         .lvbo_fill      = ofd_lvbo_fill
383 };