Whamcloud - gitweb
LU-3285 mds: add IO locking to the MDC and MDT
[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, 2015, 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         ENTRY;
204
205         CDEBUG(D_DLMTRACE, "obj: %p/%p, lock %p\n",
206                 data, lock->l_ast_data, lock);
207
208         LASSERT(lock->l_granted_mode == lock->l_req_mode);
209         if (lock->l_ast_data == data)
210                 lock->l_ast_data = NULL;
211         RETURN(LDLM_ITER_CONTINUE);
212 }
213
214 int osc_object_prune(const struct lu_env *env, struct cl_object *obj)
215 {
216         struct osc_object  *osc = cl2osc(obj);
217         struct ldlm_res_id *resname = &osc_env_info(env)->oti_resname;
218
219         /* DLM locks don't hold a reference of osc_object so we have to
220          * clear it before the object is being destroyed. */
221         osc_build_res_name(osc, resname);
222         ldlm_resource_iterate(osc_export(osc)->exp_obd->obd_namespace, resname,
223                               osc_object_ast_clear, osc);
224         return 0;
225 }
226 EXPORT_SYMBOL(osc_object_prune);
227
228 static int osc_object_fiemap(const struct lu_env *env, struct cl_object *obj,
229                              struct ll_fiemap_info_key *fmkey,
230                              struct fiemap *fiemap, size_t *buflen)
231 {
232         struct obd_export *exp = osc_export(cl2osc(obj));
233         struct ldlm_res_id resid;
234         union ldlm_policy_data policy;
235         struct lustre_handle lockh;
236         enum ldlm_mode mode = LCK_MINMODE;
237         struct ptlrpc_request *req;
238         struct fiemap *reply;
239         char *tmp;
240         int rc;
241         ENTRY;
242
243         fmkey->lfik_oa.o_oi = cl2osc(obj)->oo_oinfo->loi_oi;
244         if (!(fmkey->lfik_fiemap.fm_flags & FIEMAP_FLAG_SYNC))
245                 goto skip_locking;
246
247         policy.l_extent.start = fmkey->lfik_fiemap.fm_start & PAGE_MASK;
248
249         if (OBD_OBJECT_EOF - fmkey->lfik_fiemap.fm_length <=
250             fmkey->lfik_fiemap.fm_start + PAGE_SIZE - 1)
251                 policy.l_extent.end = OBD_OBJECT_EOF;
252         else
253                 policy.l_extent.end = (fmkey->lfik_fiemap.fm_start +
254                                        fmkey->lfik_fiemap.fm_length +
255                                        PAGE_SIZE - 1) & PAGE_MASK;
256
257         ostid_build_res_name(&fmkey->lfik_oa.o_oi, &resid);
258         mode = ldlm_lock_match(exp->exp_obd->obd_namespace,
259                                LDLM_FL_BLOCK_GRANTED | LDLM_FL_LVB_READY,
260                                &resid, LDLM_EXTENT, &policy,
261                                LCK_PR | LCK_PW, &lockh, 0);
262         if (mode) { /* lock is cached on client */
263                 if (mode != LCK_PR) {
264                         ldlm_lock_addref(&lockh, LCK_PR);
265                         ldlm_lock_decref(&lockh, LCK_PW);
266                 }
267         } else { /* no cached lock, needs acquire lock on server side */
268                 fmkey->lfik_oa.o_valid |= OBD_MD_FLFLAGS;
269                 fmkey->lfik_oa.o_flags |= OBD_FL_SRVLOCK;
270         }
271
272 skip_locking:
273         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
274                                    &RQF_OST_GET_INFO_FIEMAP);
275         if (req == NULL)
276                 GOTO(drop_lock, rc = -ENOMEM);
277
278         req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_KEY, RCL_CLIENT,
279                              sizeof(*fmkey));
280         req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_VAL, RCL_CLIENT,
281                              *buflen);
282         req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_VAL, RCL_SERVER,
283                              *buflen);
284
285         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
286         if (rc != 0) {
287                 ptlrpc_request_free(req);
288                 GOTO(drop_lock, rc);
289         }
290         tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_KEY);
291         memcpy(tmp, fmkey, sizeof(*fmkey));
292         tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_VAL);
293         memcpy(tmp, fiemap, *buflen);
294         ptlrpc_request_set_replen(req);
295
296         rc = ptlrpc_queue_wait(req);
297         if (rc != 0)
298                 GOTO(fini_req, rc);
299
300         reply = req_capsule_server_get(&req->rq_pill, &RMF_FIEMAP_VAL);
301         if (reply == NULL)
302                 GOTO(fini_req, rc = -EPROTO);
303
304         memcpy(fiemap, reply, *buflen);
305 fini_req:
306         ptlrpc_req_finished(req);
307 drop_lock:
308         if (mode)
309                 ldlm_lock_decref(&lockh, LCK_PR);
310         RETURN(rc);
311 }
312
313 int osc_object_is_contended(struct osc_object *obj)
314 {
315         struct osc_device *dev  = lu2osc_dev(obj->oo_cl.co_lu.lo_dev);
316         int osc_contention_time = dev->od_contention_time;
317         cfs_time_t cur_time     = cfs_time_current();
318         cfs_time_t retry_time;
319
320         if (OBD_FAIL_CHECK(OBD_FAIL_OSC_OBJECT_CONTENTION))
321                 return 1;
322
323         if (!obj->oo_contended)
324                 return 0;
325
326         /*
327          * I like copy-paste. the code is copied from
328          * ll_file_is_contended.
329          */
330         retry_time = cfs_time_add(obj->oo_contention_time,
331                                   cfs_time_seconds(osc_contention_time));
332         if (cfs_time_after(cur_time, retry_time)) {
333                 osc_object_clear_contended(obj);
334                 return 0;
335         }
336         return 1;
337 }
338 EXPORT_SYMBOL(osc_object_is_contended);
339
340 /**
341  * Implementation of struct cl_object_operations::coo_req_attr_set() for osc
342  * layer. osc is responsible for struct obdo::o_id and struct obdo::o_seq
343  * fields.
344  */
345 static void osc_req_attr_set(const struct lu_env *env, struct cl_object *obj,
346                              struct cl_req_attr *attr)
347 {
348         struct lov_oinfo *oinfo;
349         struct obdo      *oa;
350         struct ost_lvb   *lvb;
351         u64               flags = attr->cra_flags;
352
353         oinfo   = cl2osc(obj)->oo_oinfo;
354         lvb     = &oinfo->loi_lvb;
355         oa      = attr->cra_oa;
356
357         if ((flags & OBD_MD_FLMTIME) != 0) {
358                 oa->o_mtime = lvb->lvb_mtime;
359                 oa->o_valid |= OBD_MD_FLMTIME;
360         }
361         if ((flags & OBD_MD_FLATIME) != 0) {
362                 oa->o_atime = lvb->lvb_atime;
363                 oa->o_valid |= OBD_MD_FLATIME;
364         }
365         if ((flags & OBD_MD_FLCTIME) != 0) {
366                 oa->o_ctime = lvb->lvb_ctime;
367                 oa->o_valid |= OBD_MD_FLCTIME;
368         }
369         if (flags & OBD_MD_FLGROUP) {
370                 ostid_set_seq(&oa->o_oi, ostid_seq(&oinfo->loi_oi));
371                 oa->o_valid |= OBD_MD_FLGROUP;
372         }
373         if (flags & OBD_MD_FLID) {
374                 int rc;
375
376                 rc = ostid_set_id(&oa->o_oi, ostid_id(&oinfo->loi_oi));
377                 if (rc) {
378                         CERROR("Bad %llu to set " DOSTID " : rc %d\n",
379                                (unsigned long long)ostid_id(&oinfo->loi_oi),
380                                POSTID(&oa->o_oi), rc);
381                 }
382                 oa->o_valid |= OBD_MD_FLID;
383         }
384         if (flags & OBD_MD_FLHANDLE) {
385                 struct ldlm_lock *lock;
386                 struct osc_page *opg;
387
388                 opg = osc_cl_page_osc(attr->cra_page, cl2osc(obj));
389                 lock = osc_dlmlock_at_pgoff(env, cl2osc(obj), osc_index(opg),
390                                 OSC_DAP_FL_TEST_LOCK | OSC_DAP_FL_CANCELING);
391                 if (lock == NULL && !opg->ops_srvlock) {
392                         struct ldlm_resource *res;
393                         struct ldlm_res_id *resname;
394
395                         CL_PAGE_DEBUG(D_ERROR, env, attr->cra_page,
396                                       "uncovered page!\n");
397
398                         resname = &osc_env_info(env)->oti_resname;
399                         ostid_build_res_name(&oinfo->loi_oi, resname);
400                         res = ldlm_resource_get(
401                                 osc_export(cl2osc(obj))->exp_obd->obd_namespace,
402                                 NULL, resname, LDLM_EXTENT, 0);
403                         ldlm_resource_dump(D_ERROR, res);
404
405                         libcfs_debug_dumpstack(NULL);
406                         LBUG();
407                 }
408
409                 /* check for lockless io. */
410                 if (lock != NULL) {
411                         oa->o_handle = lock->l_remote_handle;
412                         oa->o_valid |= OBD_MD_FLHANDLE;
413                         LDLM_LOCK_PUT(lock);
414                 }
415         }
416 }
417
418 static const struct cl_object_operations osc_ops = {
419         .coo_page_init    = osc_page_init,
420         .coo_lock_init    = osc_lock_init,
421         .coo_io_init      = osc_io_init,
422         .coo_attr_get     = osc_attr_get,
423         .coo_attr_update  = osc_attr_update,
424         .coo_glimpse      = osc_object_glimpse,
425         .coo_prune        = osc_object_prune,
426         .coo_fiemap       = osc_object_fiemap,
427         .coo_req_attr_set = osc_req_attr_set
428 };
429
430 static const struct lu_object_operations osc_lu_obj_ops = {
431         .loo_object_init      = osc_object_init,
432         .loo_object_release   = NULL,
433         .loo_object_free      = osc_object_free,
434         .loo_object_print     = osc_object_print,
435         .loo_object_invariant = NULL
436 };
437
438 struct lu_object *osc_object_alloc(const struct lu_env *env,
439                                    const struct lu_object_header *unused,
440                                    struct lu_device *dev)
441 {
442         struct osc_object *osc;
443         struct lu_object  *obj;
444
445         OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, GFP_NOFS);
446         if (osc != NULL) {
447                 obj = osc2lu(osc);
448                 lu_object_init(obj, NULL, dev);
449                 osc->oo_cl.co_ops = &osc_ops;
450                 obj->lo_ops = &osc_lu_obj_ops;
451                 osc->oo_obj_ops = &osc_object_ops;
452         } else
453                 obj = NULL;
454         return obj;
455 }
456
457 int osc_object_invalidate(const struct lu_env *env, struct osc_object *osc)
458 {
459         struct l_wait_info lwi = { 0 };
460         ENTRY;
461
462         CDEBUG(D_INODE, "Invalidate osc object: %p, # of active IOs: %d\n",
463                osc, atomic_read(&osc->oo_nr_ios));
464
465         l_wait_event(osc->oo_io_waitq, atomic_read(&osc->oo_nr_ios) == 0, &lwi);
466
467         /* Discard all dirty pages of this object. */
468         osc_cache_truncate_start(env, osc, 0, NULL);
469
470         /* Discard all caching pages */
471         osc_lock_discard_pages(env, osc, 0, CL_PAGE_EOF, true);
472
473         /* Clear ast data of dlm lock. Do this after discarding all pages */
474         osc_object_prune(env, osc2cl(osc));
475
476         RETURN(0);
477 }
478 EXPORT_SYMBOL(osc_object_invalidate);
479 /** @} osc */