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