Whamcloud - gitweb
LU-1154 clio: rename coo_attr_set to coo_attr_update
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Implementation of cl_object for OSC layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_OSC
43
44 #include "osc_cl_internal.h"
45
46 /** \addtogroup osc
47  *  @{
48  */
49
50 /*****************************************************************************
51  *
52  * Type conversions.
53  *
54  */
55
56 static struct lu_object *osc2lu(struct osc_object *osc)
57 {
58         return &osc->oo_cl.co_lu;
59 }
60
61 static struct osc_object *lu2osc(const struct lu_object *obj)
62 {
63         LINVRNT(osc_is_object(obj));
64         return container_of0(obj, struct osc_object, oo_cl.co_lu);
65 }
66
67 /*****************************************************************************
68  *
69  * Object operations.
70  *
71  */
72
73 static int osc_object_init(const struct lu_env *env, struct lu_object *obj,
74                            const struct lu_object_conf *conf)
75 {
76         struct osc_object           *osc   = lu2osc(obj);
77         const struct cl_object_conf *cconf = lu2cl_conf(conf);
78         int i;
79
80         osc->oo_oinfo = cconf->u.coc_oinfo;
81 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
82         mutex_init(&osc->oo_debug_mutex);
83 #endif
84         spin_lock_init(&osc->oo_seatbelt);
85         for (i = 0; i < CRT_NR; ++i)
86                 INIT_LIST_HEAD(&osc->oo_inflight[i]);
87
88         INIT_LIST_HEAD(&osc->oo_ready_item);
89         INIT_LIST_HEAD(&osc->oo_hp_ready_item);
90         INIT_LIST_HEAD(&osc->oo_write_item);
91         INIT_LIST_HEAD(&osc->oo_read_item);
92
93         osc->oo_root.rb_node = NULL;
94         INIT_LIST_HEAD(&osc->oo_hp_exts);
95         INIT_LIST_HEAD(&osc->oo_urgent_exts);
96         INIT_LIST_HEAD(&osc->oo_rpc_exts);
97         INIT_LIST_HEAD(&osc->oo_reading_exts);
98         atomic_set(&osc->oo_nr_reads, 0);
99         atomic_set(&osc->oo_nr_writes, 0);
100         spin_lock_init(&osc->oo_lock);
101         spin_lock_init(&osc->oo_tree_lock);
102         spin_lock_init(&osc->oo_ol_spin);
103         INIT_LIST_HEAD(&osc->oo_ol_list);
104
105         cl_object_page_init(lu2cl(obj), sizeof(struct osc_page));
106
107         return 0;
108 }
109
110 static void osc_object_free(const struct lu_env *env, struct lu_object *obj)
111 {
112         struct osc_object *osc = lu2osc(obj);
113         int i;
114
115         for (i = 0; i < CRT_NR; ++i)
116                 LASSERT(list_empty(&osc->oo_inflight[i]));
117
118         LASSERT(list_empty(&osc->oo_ready_item));
119         LASSERT(list_empty(&osc->oo_hp_ready_item));
120         LASSERT(list_empty(&osc->oo_write_item));
121         LASSERT(list_empty(&osc->oo_read_item));
122
123         LASSERT(osc->oo_root.rb_node == NULL);
124         LASSERT(list_empty(&osc->oo_hp_exts));
125         LASSERT(list_empty(&osc->oo_urgent_exts));
126         LASSERT(list_empty(&osc->oo_rpc_exts));
127         LASSERT(list_empty(&osc->oo_reading_exts));
128         LASSERT(atomic_read(&osc->oo_nr_reads) == 0);
129         LASSERT(atomic_read(&osc->oo_nr_writes) == 0);
130         LASSERT(list_empty(&osc->oo_ol_list));
131
132         lu_object_fini(obj);
133         OBD_SLAB_FREE_PTR(osc, osc_object_kmem);
134 }
135
136 int osc_lvb_print(const struct lu_env *env, void *cookie,
137                   lu_printer_t p, const struct ost_lvb *lvb)
138 {
139         return (*p)(env, cookie, "size: "LPU64" mtime: "LPU64" atime: "LPU64" "
140                     "ctime: "LPU64" blocks: "LPU64,
141                     lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
142                     lvb->lvb_ctime, lvb->lvb_blocks);
143 }
144
145 static int osc_object_print(const struct lu_env *env, void *cookie,
146                             lu_printer_t p, const struct lu_object *obj)
147 {
148         struct osc_object   *osc   = lu2osc(obj);
149         struct lov_oinfo    *oinfo = osc->oo_oinfo;
150         struct osc_async_rc *ar    = &oinfo->loi_ar;
151
152         (*p)(env, cookie, "id: "DOSTID" "
153              "idx: %d gen: %d kms_valid: %u kms "LPU64" "
154              "rc: %d force_sync: %d min_xid: "LPU64" ",
155              POSTID(&oinfo->loi_oi), oinfo->loi_ost_idx,
156              oinfo->loi_ost_gen, oinfo->loi_kms_valid, oinfo->loi_kms,
157              ar->ar_rc, ar->ar_force_sync, ar->ar_min_xid);
158         osc_lvb_print(env, cookie, p, &oinfo->loi_lvb);
159         return 0;
160 }
161
162
163 static int osc_attr_get(const struct lu_env *env, struct cl_object *obj,
164                         struct cl_attr *attr)
165 {
166         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
167
168         cl_lvb2attr(attr, &oinfo->loi_lvb);
169         attr->cat_kms = oinfo->loi_kms_valid ? oinfo->loi_kms : 0;
170         return 0;
171 }
172
173 static int osc_attr_update(const struct lu_env *env, struct cl_object *obj,
174                            const struct cl_attr *attr, unsigned valid)
175 {
176         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
177         struct ost_lvb   *lvb   = &oinfo->loi_lvb;
178
179         if (valid & CAT_SIZE)
180                 lvb->lvb_size = attr->cat_size;
181         if (valid & CAT_MTIME)
182                 lvb->lvb_mtime = attr->cat_mtime;
183         if (valid & CAT_ATIME)
184                 lvb->lvb_atime = attr->cat_atime;
185         if (valid & CAT_CTIME)
186                 lvb->lvb_ctime = attr->cat_ctime;
187         if (valid & CAT_BLOCKS)
188                 lvb->lvb_blocks = attr->cat_blocks;
189         if (valid & CAT_KMS) {
190                 CDEBUG(D_CACHE, "set kms from "LPU64"to "LPU64"\n",
191                        oinfo->loi_kms, (__u64)attr->cat_kms);
192                 loi_kms_set(oinfo, attr->cat_kms);
193         }
194         return 0;
195 }
196
197 static int osc_object_glimpse(const struct lu_env *env,
198                               const struct cl_object *obj, struct ost_lvb *lvb)
199 {
200         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
201
202         ENTRY;
203         lvb->lvb_size   = oinfo->loi_kms;
204         lvb->lvb_blocks = oinfo->loi_lvb.lvb_blocks;
205         RETURN(0);
206 }
207
208 static int osc_object_ast_clear(struct ldlm_lock *lock, void *data)
209 {
210         ENTRY;
211
212         if (lock->l_ast_data == data)
213                 lock->l_ast_data = NULL;
214         RETURN(LDLM_ITER_CONTINUE);
215 }
216
217 static int osc_object_prune(const struct lu_env *env, struct cl_object *obj)
218 {
219         struct osc_object       *osc = cl2osc(obj);
220         struct ldlm_res_id      *resname = &osc_env_info(env)->oti_resname;
221
222         LASSERTF(osc->oo_npages == 0,
223                  DFID "still have %lu pages, obj: %p, osc: %p\n",
224                  PFID(lu_object_fid(&obj->co_lu)), osc->oo_npages, obj, osc);
225
226         /* DLM locks don't hold a reference of osc_object so we have to
227          * clear it before the object is being destroyed. */
228         ostid_build_res_name(&osc->oo_oinfo->loi_oi, resname);
229         ldlm_resource_iterate(osc_export(osc)->exp_obd->obd_namespace, resname,
230                               osc_object_ast_clear, osc);
231         return 0;
232 }
233
234 void osc_object_set_contended(struct osc_object *obj)
235 {
236         obj->oo_contention_time = cfs_time_current();
237         /* mb(); */
238         obj->oo_contended = 1;
239 }
240
241 void osc_object_clear_contended(struct osc_object *obj)
242 {
243         obj->oo_contended = 0;
244 }
245
246 int osc_object_is_contended(struct osc_object *obj)
247 {
248         struct osc_device *dev  = lu2osc_dev(obj->oo_cl.co_lu.lo_dev);
249         int osc_contention_time = dev->od_contention_time;
250         cfs_time_t cur_time     = cfs_time_current();
251         cfs_time_t retry_time;
252
253         if (OBD_FAIL_CHECK(OBD_FAIL_OSC_OBJECT_CONTENTION))
254                 return 1;
255
256         if (!obj->oo_contended)
257                 return 0;
258
259         /*
260          * I like copy-paste. the code is copied from
261          * ll_file_is_contended.
262          */
263         retry_time = cfs_time_add(obj->oo_contention_time,
264                                   cfs_time_seconds(osc_contention_time));
265         if (cfs_time_after(cur_time, retry_time)) {
266                 osc_object_clear_contended(obj);
267                 return 0;
268         }
269         return 1;
270 }
271
272 static const struct cl_object_operations osc_ops = {
273         .coo_page_init    = osc_page_init,
274         .coo_lock_init    = osc_lock_init,
275         .coo_io_init      = osc_io_init,
276         .coo_attr_get     = osc_attr_get,
277         .coo_attr_update  = osc_attr_update,
278         .coo_glimpse      = osc_object_glimpse,
279         .coo_prune        = osc_object_prune
280 };
281
282 static const struct lu_object_operations osc_lu_obj_ops = {
283         .loo_object_init      = osc_object_init,
284         .loo_object_release   = NULL,
285         .loo_object_free      = osc_object_free,
286         .loo_object_print     = osc_object_print,
287         .loo_object_invariant = NULL
288 };
289
290 struct lu_object *osc_object_alloc(const struct lu_env *env,
291                                    const struct lu_object_header *unused,
292                                    struct lu_device *dev)
293 {
294         struct osc_object *osc;
295         struct lu_object  *obj;
296
297         OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, GFP_NOFS);
298         if (osc != NULL) {
299                 obj = osc2lu(osc);
300                 lu_object_init(obj, NULL, dev);
301                 osc->oo_cl.co_ops = &osc_ops;
302                 obj->lo_ops = &osc_lu_obj_ops;
303         } else
304                 obj = NULL;
305         return obj;
306 }
307
308 /** @} osc */