Whamcloud - gitweb
LU-11652 kernel: kernel update [SLES12 SP3 4.4.162-94.69]
[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         struct lu_env _env;
92         int rc = 0;
93         ENTRY;
94
95         LASSERT(res);
96         LASSERT(mutex_is_locked(&res->lr_lvb_mutex));
97
98         if (res->lr_lvb_data != NULL)
99                 RETURN(0);
100
101         ofd = ldlm_res_to_ns(res)->ns_lvbp;
102         LASSERT(ofd != NULL);
103
104         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_OST_LVB))
105                 RETURN(-ENOMEM);
106
107         if (!env) {
108                 rc = lu_env_init(&_env, LCT_DT_THREAD);
109                 if (rc)
110                         RETURN(rc);
111                 env = &_env;
112         }
113
114         OBD_ALLOC_PTR(lvb);
115         if (lvb == NULL)
116                 GOTO(out, rc = -ENOMEM);
117
118         info = ofd_info(env);
119         res->lr_lvb_data = lvb;
120         res->lr_lvb_len = sizeof(*lvb);
121
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);
125         if (IS_ERR(fo))
126                 GOTO(out_lvb, rc = PTR_ERR(fo));
127
128         rc = ofd_attr_get(env, fo, &info->fti_attr);
129         if (rc)
130                 GOTO(out_obj, rc);
131
132         lvb->lvb_size = info->fti_attr.la_size;
133         lvb->lvb_blocks = info->fti_attr.la_blocks;
134         lvb->lvb_mtime = info->fti_attr.la_mtime;
135         lvb->lvb_atime = info->fti_attr.la_atime;
136         lvb->lvb_ctime = info->fti_attr.la_ctime;
137
138         CDEBUG(D_DLMTRACE, "res: "DFID" initial lvb size: %llu, "
139                "mtime: %#llx, blocks: %#llx\n",
140                PFID(&info->fti_fid), lvb->lvb_size,
141                lvb->lvb_mtime, lvb->lvb_blocks);
142
143         info->fti_attr.la_valid = 0;
144
145         EXIT;
146 out_obj:
147         ofd_object_put(env, fo);
148 out_lvb:
149         if (rc != 0)
150                 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
151 out:
152         /* Don't free lvb data on lookup error */
153         if (env && env == &_env)
154                 lu_env_fini(&_env);
155         return rc;
156 }
157
158 /**
159  * Implementation of ldlm_valblock_ops::lvbo_update for OFD.
160  *
161  * When a client generates a glimpse enqueue, it wants to get the current
162  * file size and updated attributes for a stat() type operation, but these
163  * attributes may be writeback cached on another client. The client with
164  * the DLM extent lock at the highest offset is asked for its current
165  * attributes via a glimpse callback on its extent lock, on the assumption
166  * that it has the highest file size and the newest timestamps. The timestamps
167  * are guaranteed to be correct if there is only a single writer on the file,
168  * but may be slightly inaccurate if there are multiple concurrent writers on
169  * the same object. In order to avoid race conditions between the glimpse AST
170  * and the client cancelling the lock, ofd_lvbo_update() also updates
171  * the attributes from the local object. If the last client hasn't done any
172  * writes yet, or has already written its data and cancelled its lock before
173  * it processed the glimpse, then the local inode will have more uptodate
174  * information.
175  *
176  * This is called in two ways:
177  *  \a req != NULL : called by the DLM itself after a glimpse callback
178  *  \a req == NULL : called by the OFD after a disk write
179  *
180  * \param[in] lock              LDLM lock
181  * \param[in] req               PTLRPC request
182  * \param[in] increase_only     don't allow LVB values to decrease
183  *
184  * \retval              0 on successful setup
185  * \retval              negative value on error
186  */
187 static int ofd_lvbo_update(const struct lu_env *env, struct ldlm_resource *res,
188                            struct ldlm_lock *lock, struct ptlrpc_request *req,
189                            int increase_only)
190 {
191         struct ofd_thread_info  *info;
192         struct ofd_device       *ofd;
193         struct ofd_object       *fo;
194         struct ost_lvb          *lvb;
195         int                      rc = 0;
196
197         ENTRY;
198
199         LASSERT(env);
200         info = ofd_info(env);
201         LASSERT(res != NULL);
202
203         ofd = ldlm_res_to_ns(res)->ns_lvbp;
204         LASSERT(ofd != NULL);
205
206         fid_extract_from_res_name(&info->fti_fid, &res->lr_name);
207
208         lvb = res->lr_lvb_data;
209         if (lvb == NULL) {
210                 CERROR("%s: no LVB data for "DFID"\n",
211                        ofd_name(ofd), PFID(&info->fti_fid));
212                 GOTO(out, rc = 0);
213         }
214
215         /* Update the LVB from the network message */
216         if (req != NULL) {
217                 struct ost_lvb *rpc_lvb;
218                 bool lvb_type;
219
220                 if (req->rq_import != NULL)
221                         lvb_type = imp_connect_lvb_type(req->rq_import);
222                 else
223                         lvb_type = exp_connect_lvb_type(req->rq_export);
224
225                 if (!lvb_type) {
226                         struct ost_lvb_v1 *lvb_v1;
227
228                         lvb_v1 = req_capsule_server_swab_get(&req->rq_pill,
229                                         &RMF_DLM_LVB, lustre_swab_ost_lvb_v1);
230                         if (lvb_v1 == NULL)
231                                 goto disk_update;
232
233                         rpc_lvb = &info->fti_lvb;
234                         memcpy(rpc_lvb, lvb_v1, sizeof *lvb_v1);
235                         rpc_lvb->lvb_mtime_ns = 0;
236                         rpc_lvb->lvb_atime_ns = 0;
237                         rpc_lvb->lvb_ctime_ns = 0;
238                 } else {
239                         rpc_lvb = req_capsule_server_swab_get(&req->rq_pill,
240                                                               &RMF_DLM_LVB,
241                                                         lustre_swab_ost_lvb);
242                         if (rpc_lvb == NULL)
243                                 goto disk_update;
244                 }
245
246                 lock_res(res);
247                 if (rpc_lvb->lvb_size > lvb->lvb_size || !increase_only) {
248                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb size: "
249                                "%llu -> %llu\n", PFID(&info->fti_fid),
250                                lvb->lvb_size, rpc_lvb->lvb_size);
251                         lvb->lvb_size = rpc_lvb->lvb_size;
252                 }
253                 if (rpc_lvb->lvb_mtime > lvb->lvb_mtime || !increase_only) {
254                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb mtime: "
255                                "%llu -> %llu\n", PFID(&info->fti_fid),
256                                lvb->lvb_mtime, rpc_lvb->lvb_mtime);
257                         lvb->lvb_mtime = rpc_lvb->lvb_mtime;
258                 }
259                 if (rpc_lvb->lvb_atime > lvb->lvb_atime || !increase_only) {
260                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb atime: "
261                                "%llu -> %llu\n", PFID(&info->fti_fid),
262                                lvb->lvb_atime, rpc_lvb->lvb_atime);
263                         lvb->lvb_atime = rpc_lvb->lvb_atime;
264                 }
265                 if (rpc_lvb->lvb_ctime > lvb->lvb_ctime || !increase_only) {
266                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb ctime: "
267                                "%llu -> %llu\n", PFID(&info->fti_fid),
268                                lvb->lvb_ctime, rpc_lvb->lvb_ctime);
269                         lvb->lvb_ctime = rpc_lvb->lvb_ctime;
270                 }
271                 if (rpc_lvb->lvb_blocks > lvb->lvb_blocks || !increase_only) {
272                         CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb blocks: "
273                                "%llu -> %llu\n", PFID(&info->fti_fid),
274                                lvb->lvb_blocks, rpc_lvb->lvb_blocks);
275                         lvb->lvb_blocks = rpc_lvb->lvb_blocks;
276                 }
277                 unlock_res(res);
278         }
279
280 disk_update:
281         /* Update the LVB from the disk inode */
282         ost_fid_from_resid(&info->fti_fid, &res->lr_name,
283                            ofd->ofd_lut.lut_lsd.lsd_osd_index);
284         fo = ofd_object_find(env, ofd, &info->fti_fid);
285         if (IS_ERR(fo))
286                 GOTO(out, rc = PTR_ERR(fo));
287
288         rc = ofd_attr_get(env, fo, &info->fti_attr);
289         if (rc)
290                 GOTO(out_obj, rc);
291
292         lock_res(res);
293         if (info->fti_attr.la_size > lvb->lvb_size || !increase_only) {
294                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb size from disk: "
295                        "%llu -> %llu\n", PFID(&info->fti_fid),
296                        lvb->lvb_size, info->fti_attr.la_size);
297                 lvb->lvb_size = info->fti_attr.la_size;
298         }
299
300         if (info->fti_attr.la_mtime >lvb->lvb_mtime || !increase_only) {
301                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb mtime from disk: "
302                        "%llu -> %llu\n", PFID(&info->fti_fid),
303                        lvb->lvb_mtime, info->fti_attr.la_mtime);
304                 lvb->lvb_mtime = info->fti_attr.la_mtime;
305         }
306         if (info->fti_attr.la_atime >lvb->lvb_atime || !increase_only) {
307                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb atime from disk: "
308                        "%llu -> %llu\n", PFID(&info->fti_fid),
309                        lvb->lvb_atime, info->fti_attr.la_atime);
310                 lvb->lvb_atime = info->fti_attr.la_atime;
311         }
312         if (info->fti_attr.la_ctime >lvb->lvb_ctime || !increase_only) {
313                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb ctime from disk: "
314                        "%llu -> %llu\n", PFID(&info->fti_fid),
315                        lvb->lvb_ctime, info->fti_attr.la_ctime);
316                 lvb->lvb_ctime = info->fti_attr.la_ctime;
317         }
318         if (info->fti_attr.la_blocks > lvb->lvb_blocks || !increase_only) {
319                 CDEBUG(D_DLMTRACE, "res: "DFID" updating lvb blocks from disk: "
320                        "%llu -> %llu\n", PFID(&info->fti_fid), lvb->lvb_blocks,
321                        (unsigned long long)info->fti_attr.la_blocks);
322                 lvb->lvb_blocks = info->fti_attr.la_blocks;
323         }
324         unlock_res(res);
325
326 out_obj:
327         ofd_object_put(env, fo);
328 out:
329         return rc;
330 }
331
332 /**
333  * Implementation of ldlm_valblock_ops::lvbo_size for OFD.
334  *
335  * This function returns size of LVB data so appropriate RPC size will be
336  * reserved. This is used for compatibility needs between server and client
337  * of different Lustre versions.
338  *
339  * \param[in] lock      LDLM lock
340  *
341  * \retval              size of LVB data
342  */
343 static int ofd_lvbo_size(struct ldlm_lock *lock)
344 {
345         if (lock->l_export != NULL && exp_connect_lvb_type(lock->l_export))
346                 return sizeof(struct ost_lvb);
347         else
348                 return sizeof(struct ost_lvb_v1);
349 }
350
351 /**
352  * Implementation of ldlm_valblock_ops::lvbo_fill for OFD.
353  *
354  * This function is called to fill the given RPC buffer \a buf with LVB data
355  *
356  * \param[in] env       execution environment
357  * \param[in] lock      LDLM lock
358  * \param[in] buf       RPC buffer to fill
359  * \param[in] buflen    buffer length
360  *
361  * \retval              size of LVB data written into \a buf buffer
362  */
363 static int ofd_lvbo_fill(const struct lu_env *env, struct ldlm_lock *lock,
364                          void *buf, int *buflen)
365 {
366         struct ldlm_resource *res = lock->l_resource;
367         int lvb_len;
368
369         /* Former lvbo_init not allocate the "LVB". */
370         if (unlikely(res->lr_lvb_len == 0))
371                 return 0;
372
373         lvb_len = ofd_lvbo_size(lock);
374         LASSERT(lvb_len <= res->lr_lvb_len);
375
376         if (lvb_len > *buflen)
377                 lvb_len = *buflen;
378
379         lock_res(res);
380         memcpy(buf, res->lr_lvb_data, lvb_len);
381         unlock_res(res);
382
383         return lvb_len;
384 }
385
386 struct ldlm_valblock_ops ofd_lvbo = {
387         .lvbo_init      = ofd_lvbo_init,
388         .lvbo_update    = ofd_lvbo_update,
389         .lvbo_free      = ofd_lvbo_free,
390         .lvbo_size      = ofd_lvbo_size,
391         .lvbo_fill      = ofd_lvbo_fill
392 };