Whamcloud - gitweb
2bf87db1eba86a06b0def097da7b4591722b36e9
[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, 2012, 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  */
40
41 #define DEBUG_SUBSYSTEM S_OSC
42
43 #include "osc_cl_internal.h"
44
45 /** \addtogroup osc 
46  *  @{ 
47  */
48
49 /*****************************************************************************
50  *
51  * Type conversions.
52  *
53  */
54
55 static struct lu_object *osc2lu(struct osc_object *osc)
56 {
57         return &osc->oo_cl.co_lu;
58 }
59
60 static struct osc_object *lu2osc(const struct lu_object *obj)
61 {
62         LINVRNT(osc_is_object(obj));
63         return container_of0(obj, struct osc_object, oo_cl.co_lu);
64 }
65
66 /*****************************************************************************
67  *
68  * Object operations.
69  *
70  */
71
72 static int osc_object_init(const struct lu_env *env, struct lu_object *obj,
73                            const struct lu_object_conf *conf)
74 {
75         struct osc_object           *osc   = lu2osc(obj);
76         const struct cl_object_conf *cconf = lu2cl_conf(conf);
77         int i;
78
79         osc->oo_oinfo = cconf->u.coc_oinfo;
80 #ifdef INVARIANT_CHECK
81         mutex_init(&osc->oo_debug_mutex);
82 #endif
83         spin_lock_init(&osc->oo_seatbelt);
84         for (i = 0; i < CRT_NR; ++i)
85                 CFS_INIT_LIST_HEAD(&osc->oo_inflight[i]);
86
87         CFS_INIT_LIST_HEAD(&osc->oo_ready_item);
88         CFS_INIT_LIST_HEAD(&osc->oo_hp_ready_item);
89         CFS_INIT_LIST_HEAD(&osc->oo_write_item);
90         CFS_INIT_LIST_HEAD(&osc->oo_read_item);
91
92         osc->oo_root.rb_node = NULL;
93         CFS_INIT_LIST_HEAD(&osc->oo_hp_exts);
94         CFS_INIT_LIST_HEAD(&osc->oo_urgent_exts);
95         CFS_INIT_LIST_HEAD(&osc->oo_rpc_exts);
96         CFS_INIT_LIST_HEAD(&osc->oo_reading_exts);
97         cfs_atomic_set(&osc->oo_nr_reads, 0);
98         cfs_atomic_set(&osc->oo_nr_writes, 0);
99         spin_lock_init(&osc->oo_lock);
100
101         cl_object_page_init(lu2cl(obj), sizeof(struct osc_page));
102
103         return 0;
104 }
105
106 static void osc_object_free(const struct lu_env *env, struct lu_object *obj)
107 {
108         struct osc_object *osc = lu2osc(obj);
109         int i;
110
111         for (i = 0; i < CRT_NR; ++i)
112                 LASSERT(cfs_list_empty(&osc->oo_inflight[i]));
113
114         LASSERT(cfs_list_empty(&osc->oo_ready_item));
115         LASSERT(cfs_list_empty(&osc->oo_hp_ready_item));
116         LASSERT(cfs_list_empty(&osc->oo_write_item));
117         LASSERT(cfs_list_empty(&osc->oo_read_item));
118
119         LASSERT(osc->oo_root.rb_node == NULL);
120         LASSERT(cfs_list_empty(&osc->oo_hp_exts));
121         LASSERT(cfs_list_empty(&osc->oo_urgent_exts));
122         LASSERT(cfs_list_empty(&osc->oo_rpc_exts));
123         LASSERT(cfs_list_empty(&osc->oo_reading_exts));
124         LASSERT(cfs_atomic_read(&osc->oo_nr_reads) == 0);
125         LASSERT(cfs_atomic_read(&osc->oo_nr_writes) == 0);
126
127         lu_object_fini(obj);
128         OBD_SLAB_FREE_PTR(osc, osc_object_kmem);
129 }
130
131 int osc_lvb_print(const struct lu_env *env, void *cookie,
132                   lu_printer_t p, const struct ost_lvb *lvb)
133 {
134         return (*p)(env, cookie, "size: "LPU64" mtime: "LPU64" atime: "LPU64" "
135                     "ctime: "LPU64" blocks: "LPU64,
136                     lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
137                     lvb->lvb_ctime, lvb->lvb_blocks);
138 }
139
140 static int osc_object_print(const struct lu_env *env, void *cookie,
141                             lu_printer_t p, const struct lu_object *obj)
142 {
143         struct osc_object   *osc   = lu2osc(obj);
144         struct lov_oinfo    *oinfo = osc->oo_oinfo;
145         struct osc_async_rc *ar    = &oinfo->loi_ar;
146
147         (*p)(env, cookie, "id: "LPU64" gr: "LPU64" "
148              "idx: %d gen: %d kms_valid: %u kms "LPU64" "
149              "rc: %d force_sync: %d min_xid: "LPU64" ",
150              oinfo->loi_id, oinfo->loi_seq, oinfo->loi_ost_idx,
151              oinfo->loi_ost_gen, oinfo->loi_kms_valid, oinfo->loi_kms,
152              ar->ar_rc, ar->ar_force_sync, ar->ar_min_xid);
153         osc_lvb_print(env, cookie, p, &oinfo->loi_lvb);
154         return 0;
155 }
156
157
158 static int osc_attr_get(const struct lu_env *env, struct cl_object *obj,
159                         struct cl_attr *attr)
160 {
161         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
162
163         cl_lvb2attr(attr, &oinfo->loi_lvb);
164         attr->cat_kms = oinfo->loi_kms_valid ? oinfo->loi_kms : 0;
165         return 0;
166 }
167
168 int osc_attr_set(const struct lu_env *env, struct cl_object *obj,
169                  const struct cl_attr *attr, unsigned valid)
170 {
171         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
172         struct ost_lvb   *lvb   = &oinfo->loi_lvb;
173
174         if (valid & CAT_SIZE)
175                 lvb->lvb_size = attr->cat_size;
176         if (valid & CAT_MTIME)
177                 lvb->lvb_mtime = attr->cat_mtime;
178         if (valid & CAT_ATIME)
179                 lvb->lvb_atime = attr->cat_atime;
180         if (valid & CAT_CTIME)
181                 lvb->lvb_ctime = attr->cat_ctime;
182         if (valid & CAT_BLOCKS)
183                 lvb->lvb_blocks = attr->cat_blocks;
184         if (valid & CAT_KMS) {
185                 CDEBUG(D_CACHE, "set kms from "LPU64"to "LPU64"\n",
186                        oinfo->loi_kms, (__u64)attr->cat_kms);
187                 loi_kms_set(oinfo, attr->cat_kms);
188         }
189         return 0;
190 }
191
192 static int osc_object_glimpse(const struct lu_env *env,
193                               const struct cl_object *obj, struct ost_lvb *lvb)
194 {
195         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
196
197         ENTRY;
198         lvb->lvb_size   = oinfo->loi_kms;
199         lvb->lvb_blocks = oinfo->loi_lvb.lvb_blocks;
200         RETURN(0);
201 }
202
203
204 void osc_object_set_contended(struct osc_object *obj)
205 {
206         obj->oo_contention_time = cfs_time_current();
207         /* mb(); */
208         obj->oo_contended = 1;
209 }
210
211 void osc_object_clear_contended(struct osc_object *obj)
212 {
213         obj->oo_contended = 0;
214 }
215
216 int osc_object_is_contended(struct osc_object *obj)
217 {
218         struct osc_device *dev  = lu2osc_dev(obj->oo_cl.co_lu.lo_dev);
219         int osc_contention_time = dev->od_contention_time;
220         cfs_time_t cur_time     = cfs_time_current();
221         cfs_time_t retry_time;
222
223         if (OBD_FAIL_CHECK(OBD_FAIL_OSC_OBJECT_CONTENTION))
224                 return 1;
225
226         if (!obj->oo_contended)
227                 return 0;
228
229         /*
230          * I like copy-paste. the code is copied from
231          * ll_file_is_contended.
232          */
233         retry_time = cfs_time_add(obj->oo_contention_time,
234                                   cfs_time_seconds(osc_contention_time));
235         if (cfs_time_after(cur_time, retry_time)) {
236                 osc_object_clear_contended(obj);
237                 return 0;
238         }
239         return 1;
240 }
241
242 static const struct cl_object_operations osc_ops = {
243         .coo_page_init = osc_page_init,
244         .coo_lock_init = osc_lock_init,
245         .coo_io_init   = osc_io_init,
246         .coo_attr_get  = osc_attr_get,
247         .coo_attr_set  = osc_attr_set,
248         .coo_glimpse   = osc_object_glimpse
249 };
250
251 static const struct lu_object_operations osc_lu_obj_ops = {
252         .loo_object_init      = osc_object_init,
253         .loo_object_delete    = NULL,
254         .loo_object_release   = NULL,
255         .loo_object_free      = osc_object_free,
256         .loo_object_print     = osc_object_print,
257         .loo_object_invariant = NULL
258 };
259
260 struct lu_object *osc_object_alloc(const struct lu_env *env,
261                                    const struct lu_object_header *unused,
262                                    struct lu_device *dev)
263 {
264         struct osc_object *osc;
265         struct lu_object  *obj;
266
267         OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, CFS_ALLOC_IO);
268         if (osc != NULL) {
269                 obj = osc2lu(osc);
270                 lu_object_init(obj, NULL, dev);
271                 osc->oo_cl.co_ops = &osc_ops;
272                 obj->lo_ops = &osc_lu_obj_ops;
273         } else
274                 obj = NULL;
275         return obj;
276 }
277
278 /** @} osc */