Whamcloud - gitweb
d10a372b1e3248779c567719cdcb555a0d3fc1c6
[fs/lustre-release.git] / lustre / osc / osc_object.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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         cfs_mutex_init(&osc->oo_debug_mutex);
82 #endif
83         cfs_spin_lock_init(&osc->oo_seatbelt);
84         for (i = 0; i < CRT_NR; ++i)
85                 CFS_INIT_LIST_HEAD(&osc->oo_inflight[i]);
86         return 0;
87 }
88
89 static void osc_object_free(const struct lu_env *env, struct lu_object *obj)
90 {
91         struct osc_object *osc = lu2osc(obj);
92         int i;
93
94         for (i = 0; i < CRT_NR; ++i)
95                 LASSERT(cfs_list_empty(&osc->oo_inflight[i]));
96
97         lu_object_fini(obj);
98         OBD_SLAB_FREE_PTR(osc, osc_object_kmem);
99 }
100
101 int osc_lvb_print(const struct lu_env *env, void *cookie,
102                   lu_printer_t p, const struct ost_lvb *lvb)
103 {
104         return (*p)(env, cookie, "size: "LPU64" mtime: "LPU64" atime: "LPU64" "
105                     "ctime: "LPU64" blocks: "LPU64,
106                     lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
107                     lvb->lvb_ctime, lvb->lvb_blocks);
108 }
109
110 static int osc_object_print(const struct lu_env *env, void *cookie,
111                             lu_printer_t p, const struct lu_object *obj)
112 {
113         struct osc_object   *osc   = lu2osc(obj);
114         struct lov_oinfo    *oinfo = osc->oo_oinfo;
115         struct osc_async_rc *ar    = &oinfo->loi_ar;
116
117         (*p)(env, cookie, "id: "LPU64" gr: "LPU64" "
118              "idx: %d gen: %d kms_valid: %u kms "LPU64" "
119              "rc: %d force_sync: %d min_xid: "LPU64" ",
120              oinfo->loi_id, oinfo->loi_seq, oinfo->loi_ost_idx,
121              oinfo->loi_ost_gen, oinfo->loi_kms_valid, oinfo->loi_kms,
122              ar->ar_rc, ar->ar_force_sync, ar->ar_min_xid);
123         osc_lvb_print(env, cookie, p, &oinfo->loi_lvb);
124         return 0;
125 }
126
127
128 static int osc_attr_get(const struct lu_env *env, struct cl_object *obj,
129                         struct cl_attr *attr)
130 {
131         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
132
133         cl_lvb2attr(attr, &oinfo->loi_lvb);
134         attr->cat_kms = oinfo->loi_kms_valid ? oinfo->loi_kms : 0;
135         return 0;
136 }
137
138 int osc_attr_set(const struct lu_env *env, struct cl_object *obj,
139                  const struct cl_attr *attr, unsigned valid)
140 {
141         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
142         struct ost_lvb   *lvb   = &oinfo->loi_lvb;
143
144         if (valid & CAT_SIZE)
145                 lvb->lvb_size = attr->cat_size;
146         if (valid & CAT_MTIME)
147                 lvb->lvb_mtime = attr->cat_mtime;
148         if (valid & CAT_ATIME)
149                 lvb->lvb_atime = attr->cat_atime;
150         if (valid & CAT_CTIME)
151                 lvb->lvb_ctime = attr->cat_ctime;
152         if (valid & CAT_BLOCKS)
153                 lvb->lvb_blocks = attr->cat_blocks;
154         if (valid & CAT_KMS) {
155                 CDEBUG(D_CACHE, "set kms from "LPU64"to "LPU64"\n",
156                        oinfo->loi_kms, (__u64)attr->cat_kms);
157                 loi_kms_set(oinfo, attr->cat_kms);
158         }
159         return 0;
160 }
161
162 static int osc_object_glimpse(const struct lu_env *env,
163                               const struct cl_object *obj, struct ost_lvb *lvb)
164 {
165         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
166
167         ENTRY;
168         lvb->lvb_size   = oinfo->loi_kms;
169         lvb->lvb_blocks = oinfo->loi_lvb.lvb_blocks;
170         RETURN(0);
171 }
172
173
174 void osc_object_set_contended(struct osc_object *obj)
175 {
176         obj->oo_contention_time = cfs_time_current();
177         /* mb(); */
178         obj->oo_contended = 1;
179 }
180
181 void osc_object_clear_contended(struct osc_object *obj)
182 {
183         obj->oo_contended = 0;
184 }
185
186 int osc_object_is_contended(struct osc_object *obj)
187 {
188         struct osc_device *dev  = lu2osc_dev(obj->oo_cl.co_lu.lo_dev);
189         int osc_contention_time = dev->od_contention_time;
190         cfs_time_t cur_time     = cfs_time_current();
191         cfs_time_t retry_time;
192
193         if (OBD_FAIL_CHECK(OBD_FAIL_OSC_OBJECT_CONTENTION))
194                 return 1;
195
196         if (!obj->oo_contended)
197                 return 0;
198
199         /*
200          * I like copy-paste. the code is copied from
201          * ll_file_is_contended.
202          */
203         retry_time = cfs_time_add(obj->oo_contention_time,
204                                   cfs_time_seconds(osc_contention_time));
205         if (cfs_time_after(cur_time, retry_time)) {
206                 osc_object_clear_contended(obj);
207                 return 0;
208         }
209         return 1;
210 }
211
212 static const struct cl_object_operations osc_ops = {
213         .coo_page_init = osc_page_init,
214         .coo_lock_init = osc_lock_init,
215         .coo_io_init   = osc_io_init,
216         .coo_attr_get  = osc_attr_get,
217         .coo_attr_set  = osc_attr_set,
218         .coo_glimpse   = osc_object_glimpse
219 };
220
221 static const struct lu_object_operations osc_lu_obj_ops = {
222         .loo_object_init      = osc_object_init,
223         .loo_object_delete    = NULL,
224         .loo_object_release   = NULL,
225         .loo_object_free      = osc_object_free,
226         .loo_object_print     = osc_object_print,
227         .loo_object_invariant = NULL
228 };
229
230 struct lu_object *osc_object_alloc(const struct lu_env *env,
231                                    const struct lu_object_header *unused,
232                                    struct lu_device *dev)
233 {
234         struct osc_object *osc;
235         struct lu_object  *obj;
236
237         OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, CFS_ALLOC_IO);
238         if (osc != NULL) {
239                 obj = osc2lu(osc);
240                 lu_object_init(obj, NULL, dev);
241                 osc->oo_cl.co_ops = &osc_ops;
242                 obj->lo_ops = &osc_lu_obj_ops;
243         } else
244                 obj = NULL;
245         return obj;
246 }
247
248 /** @} osc */