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