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