Whamcloud - gitweb
LU-12681 osc: wrong cache of LVB attrs, part2
[fs/lustre-release.git] / lustre / osc / osc_object.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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 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  * Implementation of cl_object for OSC layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_OSC
39 #include <lustre_osc.h>
40
41 #include "osc_internal.h"
42
43 /** \addtogroup osc
44  *  @{
45  */
46
47 /*****************************************************************************
48  *
49  * Object operations.
50  *
51  */
52 static void osc_obj_build_res_name(struct osc_object *osc,
53                                    struct ldlm_res_id *resname)
54 {
55         ostid_build_res_name(&osc->oo_oinfo->loi_oi, resname);
56 }
57
58 static const struct osc_object_operations osc_object_ops = {
59         .oto_build_res_name = osc_obj_build_res_name,
60         .oto_dlmlock_at_pgoff = osc_obj_dlmlock_at_pgoff,
61 };
62
63 int osc_object_init(const struct lu_env *env, struct lu_object *obj,
64                     const struct lu_object_conf *conf)
65 {
66         struct osc_object           *osc   = lu2osc(obj);
67         const struct cl_object_conf *cconf = lu2cl_conf(conf);
68
69         osc->oo_oinfo = cconf->u.coc_oinfo;
70 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
71         mutex_init(&osc->oo_debug_mutex);
72 #endif
73         INIT_LIST_HEAD(&osc->oo_ready_item);
74         INIT_LIST_HEAD(&osc->oo_hp_ready_item);
75         INIT_LIST_HEAD(&osc->oo_write_item);
76         INIT_LIST_HEAD(&osc->oo_read_item);
77
78         osc->oo_root.rb_node = NULL;
79         INIT_LIST_HEAD(&osc->oo_hp_exts);
80         INIT_LIST_HEAD(&osc->oo_urgent_exts);
81         INIT_LIST_HEAD(&osc->oo_full_exts);
82         INIT_LIST_HEAD(&osc->oo_reading_exts);
83         atomic_set(&osc->oo_nr_reads, 0);
84         atomic_set(&osc->oo_nr_writes, 0);
85         spin_lock_init(&osc->oo_lock);
86         spin_lock_init(&osc->oo_tree_lock);
87         spin_lock_init(&osc->oo_ol_spin);
88         INIT_LIST_HEAD(&osc->oo_ol_list);
89
90         atomic_set(&osc->oo_nr_ios, 0);
91         init_waitqueue_head(&osc->oo_io_waitq);
92
93         LASSERT(osc->oo_obj_ops != NULL);
94
95         cl_object_page_init(lu2cl(obj), sizeof(struct osc_page));
96
97         return 0;
98 }
99 EXPORT_SYMBOL(osc_object_init);
100
101 void osc_object_free(const struct lu_env *env, struct lu_object *obj)
102 {
103         struct osc_object *osc = lu2osc(obj);
104
105         LASSERT(list_empty(&osc->oo_ready_item));
106         LASSERT(list_empty(&osc->oo_hp_ready_item));
107         LASSERT(list_empty(&osc->oo_write_item));
108         LASSERT(list_empty(&osc->oo_read_item));
109
110         LASSERT(osc->oo_root.rb_node == NULL);
111         LASSERT(list_empty(&osc->oo_hp_exts));
112         LASSERT(list_empty(&osc->oo_urgent_exts));
113         LASSERT(list_empty(&osc->oo_full_exts));
114         LASSERT(list_empty(&osc->oo_reading_exts));
115         LASSERT(atomic_read(&osc->oo_nr_reads) == 0);
116         LASSERT(atomic_read(&osc->oo_nr_writes) == 0);
117         LASSERT(list_empty(&osc->oo_ol_list));
118         LASSERT(atomic_read(&osc->oo_nr_ios) == 0);
119
120         lu_object_fini(obj);
121         OBD_SLAB_FREE_PTR(osc, osc_object_kmem);
122 }
123 EXPORT_SYMBOL(osc_object_free);
124
125 int osc_lvb_print(const struct lu_env *env, void *cookie,
126                   lu_printer_t p, const struct ost_lvb *lvb)
127 {
128         return (*p)(env, cookie, "size: %llu mtime: %llu atime: %llu "
129                     "ctime: %llu blocks: %llu",
130                     lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
131                     lvb->lvb_ctime, lvb->lvb_blocks);
132 }
133 EXPORT_SYMBOL(osc_lvb_print);
134
135 int osc_object_print(const struct lu_env *env, void *cookie,
136                      lu_printer_t p, const struct lu_object *obj)
137 {
138         struct osc_object *osc = lu2osc(obj);
139         struct lov_oinfo *oinfo = osc->oo_oinfo;
140         struct osc_async_rc *ar = &oinfo->loi_ar;
141
142         (*p)(env, cookie, "id: "DOSTID" "
143              "idx: %d gen: %d kms_valid: %u kms %llu "
144              "rc: %d force_sync: %d min_xid: %llu ",
145              POSTID(&oinfo->loi_oi), oinfo->loi_ost_idx,
146              oinfo->loi_ost_gen, oinfo->loi_kms_valid, oinfo->loi_kms,
147              ar->ar_rc, ar->ar_force_sync, ar->ar_min_xid);
148         osc_lvb_print(env, cookie, p, &oinfo->loi_lvb);
149         return 0;
150 }
151 EXPORT_SYMBOL(osc_object_print);
152
153
154 int osc_attr_get(const struct lu_env *env, struct cl_object *obj,
155                  struct cl_attr *attr)
156 {
157         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
158
159         cl_lvb2attr(attr, &oinfo->loi_lvb);
160         attr->cat_kms = oinfo->loi_kms_valid ? oinfo->loi_kms : 0;
161         return 0;
162 }
163 EXPORT_SYMBOL(osc_attr_get);
164
165 int osc_attr_update(const struct lu_env *env, struct cl_object *obj,
166                     const struct cl_attr *attr, unsigned valid)
167 {
168         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
169         struct ost_lvb   *lvb   = &oinfo->loi_lvb;
170
171         if (valid & CAT_SIZE)
172                 lvb->lvb_size = attr->cat_size;
173         if (valid & CAT_MTIME)
174                 lvb->lvb_mtime = attr->cat_mtime;
175         if (valid & CAT_ATIME)
176                 lvb->lvb_atime = attr->cat_atime;
177         if (valid & CAT_CTIME)
178                 lvb->lvb_ctime = attr->cat_ctime;
179         if (valid & CAT_BLOCKS)
180                 lvb->lvb_blocks = attr->cat_blocks;
181         if (valid & CAT_KMS) {
182                 CDEBUG(D_CACHE, "set kms from %lluto %llu\n",
183                        oinfo->loi_kms, (__u64)attr->cat_kms);
184                 loi_kms_set(oinfo, attr->cat_kms);
185         }
186         return 0;
187 }
188 EXPORT_SYMBOL(osc_attr_update);
189
190 int osc_object_glimpse(const struct lu_env *env, const struct cl_object *obj,
191                        struct ost_lvb *lvb)
192 {
193         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
194
195         lvb->lvb_size = oinfo->loi_kms;
196         lvb->lvb_blocks = oinfo->loi_lvb.lvb_blocks;
197         return 0;
198 }
199 EXPORT_SYMBOL(osc_object_glimpse);
200
201 static int osc_object_ast_clear(struct ldlm_lock *lock, void *data)
202 {
203         struct osc_object *osc = (struct osc_object *)data;
204         struct ost_lvb *lvb = lock->l_lvb_data;
205         struct lov_oinfo *oinfo;
206         ENTRY;
207
208         if (lock->l_ast_data == data) {
209                 lock->l_ast_data = NULL;
210
211                 LASSERT(osc != NULL);
212                 LASSERT(osc->oo_oinfo != NULL);
213                 LASSERT(lvb != NULL);
214
215                 /* Updates lvb in lock by the cached oinfo */
216                 oinfo = osc->oo_oinfo;
217
218                 LDLM_DEBUG(lock, "update lock size %llu blocks %llu [cma]time: "
219                            "%llu %llu %llu by oinfo size %llu blocks %llu "
220                            "[cma]time %llu %llu %llu", lvb->lvb_size,
221                            lvb->lvb_blocks, lvb->lvb_ctime, lvb->lvb_mtime,
222                            lvb->lvb_atime, oinfo->loi_lvb.lvb_size,
223                            oinfo->loi_lvb.lvb_blocks, oinfo->loi_lvb.lvb_ctime,
224                            oinfo->loi_lvb.lvb_mtime, oinfo->loi_lvb.lvb_atime);
225                 LASSERT(oinfo->loi_lvb.lvb_size >= oinfo->loi_kms);
226
227                 cl_object_attr_lock(&osc->oo_cl);
228                 memcpy(lvb, &oinfo->loi_lvb, sizeof(oinfo->loi_lvb));
229                 cl_object_attr_unlock(&osc->oo_cl);
230                 ldlm_clear_lvb_cached(lock);
231         }
232         RETURN(LDLM_ITER_CONTINUE);
233 }
234
235 int osc_object_prune(const struct lu_env *env, struct cl_object *obj)
236 {
237         struct osc_object  *osc = cl2osc(obj);
238         struct ldlm_res_id *resname = &osc_env_info(env)->oti_resname;
239
240         /* DLM locks don't hold a reference of osc_object so we have to
241          * clear it before the object is being destroyed. */
242         osc_build_res_name(osc, resname);
243         ldlm_resource_iterate(osc_export(osc)->exp_obd->obd_namespace, resname,
244                               osc_object_ast_clear, osc);
245         return 0;
246 }
247 EXPORT_SYMBOL(osc_object_prune);
248
249 static int osc_object_fiemap(const struct lu_env *env, struct cl_object *obj,
250                              struct ll_fiemap_info_key *fmkey,
251                              struct fiemap *fiemap, size_t *buflen)
252 {
253         struct obd_export *exp = osc_export(cl2osc(obj));
254         struct ldlm_res_id resid;
255         union ldlm_policy_data policy;
256         struct lustre_handle lockh;
257         enum ldlm_mode mode = LCK_MINMODE;
258         struct ptlrpc_request *req;
259         struct fiemap *reply;
260         char *tmp;
261         int rc;
262         ENTRY;
263
264         fmkey->lfik_oa.o_oi = cl2osc(obj)->oo_oinfo->loi_oi;
265         if (!(fmkey->lfik_fiemap.fm_flags & FIEMAP_FLAG_SYNC))
266                 goto skip_locking;
267
268         policy.l_extent.start = fmkey->lfik_fiemap.fm_start & PAGE_MASK;
269
270         if (OBD_OBJECT_EOF - fmkey->lfik_fiemap.fm_length <=
271             fmkey->lfik_fiemap.fm_start + PAGE_SIZE - 1)
272                 policy.l_extent.end = OBD_OBJECT_EOF;
273         else
274                 policy.l_extent.end = (fmkey->lfik_fiemap.fm_start +
275                                        fmkey->lfik_fiemap.fm_length +
276                                        PAGE_SIZE - 1) & PAGE_MASK;
277
278         ostid_build_res_name(&fmkey->lfik_oa.o_oi, &resid);
279         mode = ldlm_lock_match(exp->exp_obd->obd_namespace,
280                                LDLM_FL_BLOCK_GRANTED | LDLM_FL_LVB_READY,
281                                &resid, LDLM_EXTENT, &policy,
282                                LCK_PR | LCK_PW, &lockh, 0);
283         if (mode) { /* lock is cached on client */
284                 if (mode != LCK_PR) {
285                         ldlm_lock_addref(&lockh, LCK_PR);
286                         ldlm_lock_decref(&lockh, LCK_PW);
287                 }
288         } else { /* no cached lock, needs acquire lock on server side */
289                 fmkey->lfik_oa.o_valid |= OBD_MD_FLFLAGS;
290                 fmkey->lfik_oa.o_flags |= OBD_FL_SRVLOCK;
291         }
292
293 skip_locking:
294         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
295                                    &RQF_OST_GET_INFO_FIEMAP);
296         if (req == NULL)
297                 GOTO(drop_lock, rc = -ENOMEM);
298
299         req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_KEY, RCL_CLIENT,
300                              sizeof(*fmkey));
301         req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_VAL, RCL_CLIENT,
302                              *buflen);
303         req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_VAL, RCL_SERVER,
304                              *buflen);
305
306         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
307         if (rc != 0) {
308                 ptlrpc_request_free(req);
309                 GOTO(drop_lock, rc);
310         }
311         tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_KEY);
312         memcpy(tmp, fmkey, sizeof(*fmkey));
313         tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_VAL);
314         memcpy(tmp, fiemap, *buflen);
315         ptlrpc_request_set_replen(req);
316
317         rc = ptlrpc_queue_wait(req);
318         if (rc != 0)
319                 GOTO(fini_req, rc);
320
321         reply = req_capsule_server_get(&req->rq_pill, &RMF_FIEMAP_VAL);
322         if (reply == NULL)
323                 GOTO(fini_req, rc = -EPROTO);
324
325         memcpy(fiemap, reply, *buflen);
326 fini_req:
327         ptlrpc_req_finished(req);
328 drop_lock:
329         if (mode)
330                 ldlm_lock_decref(&lockh, LCK_PR);
331         RETURN(rc);
332 }
333
334 int osc_object_is_contended(struct osc_object *obj)
335 {
336         struct osc_device *dev = lu2osc_dev(obj->oo_cl.co_lu.lo_dev);
337         time64_t osc_contention_time = dev->od_contention_time;
338         ktime_t retry_time;
339
340         if (OBD_FAIL_CHECK(OBD_FAIL_OSC_OBJECT_CONTENTION))
341                 return 1;
342
343         if (!obj->oo_contended)
344                 return 0;
345
346         /*
347          * I like copy-paste. the code is copied from
348          * ll_file_is_contended.
349          */
350         retry_time = ktime_add_ns(obj->oo_contention_time,
351                                   osc_contention_time * NSEC_PER_SEC);
352         if (ktime_after(ktime_get(), retry_time)) {
353                 osc_object_clear_contended(obj);
354                 return 0;
355         }
356         return 1;
357 }
358 EXPORT_SYMBOL(osc_object_is_contended);
359
360 /**
361  * Implementation of struct cl_object_operations::coo_req_attr_set() for osc
362  * layer. osc is responsible for struct obdo::o_id and struct obdo::o_seq
363  * fields.
364  */
365 static void osc_req_attr_set(const struct lu_env *env, struct cl_object *obj,
366                              struct cl_req_attr *attr)
367 {
368         struct lov_oinfo *oinfo;
369         struct obdo      *oa;
370         struct ost_lvb   *lvb;
371         u64               flags = attr->cra_flags;
372
373         oinfo   = cl2osc(obj)->oo_oinfo;
374         lvb     = &oinfo->loi_lvb;
375         oa      = attr->cra_oa;
376
377         if ((flags & OBD_MD_FLMTIME) != 0) {
378                 oa->o_mtime = lvb->lvb_mtime;
379                 oa->o_valid |= OBD_MD_FLMTIME;
380         }
381         if ((flags & OBD_MD_FLATIME) != 0) {
382                 oa->o_atime = lvb->lvb_atime;
383                 oa->o_valid |= OBD_MD_FLATIME;
384         }
385         if ((flags & OBD_MD_FLCTIME) != 0) {
386                 oa->o_ctime = lvb->lvb_ctime;
387                 oa->o_valid |= OBD_MD_FLCTIME;
388         }
389         if (flags & OBD_MD_FLGROUP) {
390                 ostid_set_seq(&oa->o_oi, ostid_seq(&oinfo->loi_oi));
391                 oa->o_valid |= OBD_MD_FLGROUP;
392         }
393         if (flags & OBD_MD_FLID) {
394                 int rc;
395
396                 rc = ostid_set_id(&oa->o_oi, ostid_id(&oinfo->loi_oi));
397                 if (rc) {
398                         CERROR("Bad %llu to set " DOSTID " : rc %d\n",
399                                (unsigned long long)ostid_id(&oinfo->loi_oi),
400                                POSTID(&oa->o_oi), rc);
401                 }
402                 oa->o_valid |= OBD_MD_FLID;
403         }
404         if (flags & OBD_MD_FLHANDLE) {
405                 struct ldlm_lock *lock;
406                 struct osc_page *opg;
407
408                 opg = osc_cl_page_osc(attr->cra_page, cl2osc(obj));
409                 lock = osc_dlmlock_at_pgoff(env, cl2osc(obj), osc_index(opg),
410                                 OSC_DAP_FL_TEST_LOCK | OSC_DAP_FL_CANCELING);
411                 if (lock == NULL && !opg->ops_srvlock) {
412                         struct ldlm_resource *res;
413                         struct ldlm_res_id *resname;
414
415                         CL_PAGE_DEBUG(D_ERROR, env, attr->cra_page,
416                                       "uncovered page!\n");
417
418                         resname = &osc_env_info(env)->oti_resname;
419                         ostid_build_res_name(&oinfo->loi_oi, resname);
420                         res = ldlm_resource_get(
421                                 osc_export(cl2osc(obj))->exp_obd->obd_namespace,
422                                 NULL, resname, LDLM_EXTENT, 0);
423                         ldlm_resource_dump(D_ERROR, res);
424
425                         libcfs_debug_dumpstack(NULL);
426                         LBUG();
427                 }
428
429                 /* check for lockless io. */
430                 if (lock != NULL) {
431                         oa->o_handle = lock->l_remote_handle;
432                         oa->o_valid |= OBD_MD_FLHANDLE;
433                         LDLM_LOCK_PUT(lock);
434                 }
435         }
436 }
437
438 static const struct cl_object_operations osc_ops = {
439         .coo_page_init    = osc_page_init,
440         .coo_lock_init    = osc_lock_init,
441         .coo_io_init      = osc_io_init,
442         .coo_attr_get     = osc_attr_get,
443         .coo_attr_update  = osc_attr_update,
444         .coo_glimpse      = osc_object_glimpse,
445         .coo_prune        = osc_object_prune,
446         .coo_fiemap       = osc_object_fiemap,
447         .coo_req_attr_set = osc_req_attr_set
448 };
449
450 static const struct lu_object_operations osc_lu_obj_ops = {
451         .loo_object_init      = osc_object_init,
452         .loo_object_release   = NULL,
453         .loo_object_free      = osc_object_free,
454         .loo_object_print     = osc_object_print,
455         .loo_object_invariant = NULL
456 };
457
458 struct lu_object *osc_object_alloc(const struct lu_env *env,
459                                    const struct lu_object_header *unused,
460                                    struct lu_device *dev)
461 {
462         struct osc_object *osc;
463         struct lu_object  *obj;
464
465         OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, GFP_NOFS);
466         if (osc != NULL) {
467                 obj = osc2lu(osc);
468                 lu_object_init(obj, NULL, dev);
469                 osc->oo_cl.co_ops = &osc_ops;
470                 obj->lo_ops = &osc_lu_obj_ops;
471                 osc->oo_obj_ops = &osc_object_ops;
472         } else
473                 obj = NULL;
474         return obj;
475 }
476
477 int osc_object_invalidate(const struct lu_env *env, struct osc_object *osc)
478 {
479         struct l_wait_info lwi = { 0 };
480         ENTRY;
481
482         CDEBUG(D_INODE, "Invalidate osc object: %p, # of active IOs: %d\n",
483                osc, atomic_read(&osc->oo_nr_ios));
484
485         l_wait_event(osc->oo_io_waitq, atomic_read(&osc->oo_nr_ios) == 0, &lwi);
486
487         /* Discard all dirty pages of this object. */
488         osc_cache_truncate_start(env, osc, 0, NULL);
489
490         /* Discard all caching pages */
491         osc_lock_discard_pages(env, osc, 0, CL_PAGE_EOF, true);
492
493         /* Clear ast data of dlm lock. Do this after discarding all pages */
494         osc_object_prune(env, osc2cl(osc));
495
496         RETURN(0);
497 }
498 EXPORT_SYMBOL(osc_object_invalidate);
499 /** @} osc */