Whamcloud - gitweb
LU-911 osd: fix th_sync and OSD_EXEC_OP issues
[fs/lustre-release.git] / lustre / fid / fid_store.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, 2012, 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  * lustre/fid/fid_store.c
39  *
40  * Lustre Sequence Manager
41  *
42  * Author: Yury Umanets <umka@clusterfs.com>
43  */
44
45 #ifndef EXPORT_SYMTAB
46 # define EXPORT_SYMTAB
47 #endif
48 #define DEBUG_SUBSYSTEM S_FID
49
50 #ifdef __KERNEL__
51 # include <libcfs/libcfs.h>
52 # include <linux/module.h>
53 #else /* __KERNEL__ */
54 # include <liblustre.h>
55 #endif
56
57 #include <obd.h>
58 #include <obd_class.h>
59 #include <dt_object.h>
60 #include <md_object.h>
61 #include <obd_support.h>
62 #include <lustre_req_layout.h>
63 #include <lustre_fid.h>
64 #include "fid_internal.h"
65
66 #ifdef __KERNEL__
67
68 static struct lu_buf *seq_store_buf(struct seq_thread_info *info)
69 {
70         struct lu_buf *buf;
71
72         buf = &info->sti_buf;
73         buf->lb_buf = &info->sti_space;
74         buf->lb_len = sizeof(info->sti_space);
75         return buf;
76 }
77
78 struct seq_update_callback {
79         struct dt_txn_commit_cb suc_cb;
80         struct lu_server_seq   *suc_seq;
81 };
82
83 void seq_update_cb(struct lu_env *env, struct thandle *th,
84                    struct dt_txn_commit_cb *cb, int err)
85 {
86         struct seq_update_callback *ccb;
87         ccb = container_of0(cb, struct seq_update_callback, suc_cb);
88         ccb->suc_seq->lss_need_sync = 0;
89         cfs_list_del(&ccb->suc_cb.dcb_linkage);
90         OBD_FREE_PTR(ccb);
91 }
92
93 struct thandle *seq_store_trans_create(struct lu_server_seq *seq,
94                                        const struct lu_env *env)
95 {
96         struct dt_device *dt_dev;
97
98         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
99         return dt_trans_create(env, dt_dev);
100 }
101
102 int seq_store_trans_start(struct lu_server_seq *seq, const struct lu_env *env,
103                           struct thandle *th)
104 {
105         struct dt_device *dt_dev;
106         ENTRY;
107
108         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
109
110         return dt_trans_start(env, dt_dev, th);
111 }
112
113 int seq_update_cb_add(struct thandle *th, struct lu_server_seq *seq)
114 {
115         struct seq_update_callback *ccb;
116         int rc;
117         OBD_ALLOC_PTR(ccb);
118         if (ccb == NULL)
119                 return -ENOMEM;
120
121         ccb->suc_cb.dcb_func = seq_update_cb;
122         CFS_INIT_LIST_HEAD(&ccb->suc_cb.dcb_linkage);
123         ccb->suc_seq = seq;
124         seq->lss_need_sync = 1;
125         rc = dt_trans_cb_add(th, &ccb->suc_cb);
126         if (rc)
127                 OBD_FREE_PTR(ccb);
128         return rc;
129 }
130
131 int seq_declare_store_write(struct lu_server_seq *seq,
132                             const struct lu_env *env,
133                             struct thandle *th)
134 {
135         struct dt_object *dt_obj = seq->lss_obj;
136         int rc;
137         ENTRY;
138
139         rc = dt_obj->do_body_ops->dbo_declare_write(env, dt_obj,
140                                                     sizeof(struct lu_seq_range),
141                                                     0, th);
142         return rc;
143 }
144
145 /* This function implies that caller takes care about locking. */
146 int seq_store_write(struct lu_server_seq *seq,
147                     const struct lu_env *env,
148                     struct thandle *th)
149 {
150         struct dt_object *dt_obj = seq->lss_obj;
151         struct seq_thread_info *info;
152         loff_t pos = 0;
153         int rc;
154         ENTRY;
155
156         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
157         LASSERT(info != NULL);
158
159         /* Store ranges in le format. */
160         range_cpu_to_le(&info->sti_space, &seq->lss_space);
161
162         rc = dt_obj->do_body_ops->dbo_write(env, dt_obj,
163                                             seq_store_buf(info),
164                                             &pos, th, BYPASS_CAPA, 1);
165         if (rc == sizeof(info->sti_space)) {
166                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
167                        seq->lss_name, PRANGE(&seq->lss_space));
168                 rc = 0;
169         } else if (rc >= 0) {
170                 rc = -EIO;
171         }
172
173
174         RETURN(rc);
175 }
176
177 int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
178                      struct lu_seq_range *out, int sync)
179 {
180         struct dt_device *dt_dev;
181         struct thandle *th;
182         int rc;
183         ENTRY;
184
185         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
186
187         th = seq_store_trans_create(seq, env);
188         if (IS_ERR(th))
189                 RETURN(PTR_ERR(th));
190
191         rc = seq_declare_store_write(seq, env, th);
192         if (rc)
193                 GOTO(exit, rc);
194
195         if (out != NULL) {
196                 rc = fld_declare_server_create(seq->lss_site->ms_server_fld,
197                                                env, th);
198                 if (rc)
199                         GOTO(exit, rc);
200         }
201
202         rc = seq_store_trans_start(seq, env, th);
203         if (rc)
204                 GOTO(exit, rc);
205
206         rc = seq_store_write(seq, env, th);
207         if (rc) {
208                 CERROR("%s: Can't write space data, rc %d\n",
209                        seq->lss_name, rc);
210                 GOTO(exit,rc);
211         } else if (out != NULL) {
212                 rc = fld_server_create(seq->lss_site->ms_server_fld,
213                                        env, out, th);
214                 if (rc) {
215                         CERROR("%s: Can't Update fld database, rc %d\n",
216                                seq->lss_name, rc);
217                         GOTO(exit,rc);
218                 }
219         }
220
221         /* next sequence update will need sync until this update is committed
222          * in case of sync operation this is not needed obviously */
223         if (!sync)
224                 /* if callback can't be added then sync always */
225                 sync = !!seq_update_cb_add(th, seq);
226
227         th->th_sync |= sync;
228 exit:
229         dt_trans_stop(env, dt_dev, th);
230         return rc;
231 }
232
233 /*
234  * This function implies that caller takes care about locking or locking is not
235  * needed (init time).
236  */
237 int seq_store_read(struct lu_server_seq *seq,
238                    const struct lu_env *env)
239 {
240         struct dt_object *dt_obj = seq->lss_obj;
241         struct seq_thread_info *info;
242         loff_t pos = 0;
243         int rc;
244         ENTRY;
245
246         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
247         LASSERT(info != NULL);
248
249         rc = dt_obj->do_body_ops->dbo_read(env, dt_obj, seq_store_buf(info),
250                                            &pos, BYPASS_CAPA);
251
252         if (rc == sizeof(info->sti_space)) {
253                 range_le_to_cpu(&seq->lss_space, &info->sti_space);
254                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
255                        seq->lss_name, PRANGE(&seq->lss_space));
256                 rc = 0;
257         } else if (rc == 0) {
258                 rc = -ENODATA;
259         } else if (rc >= 0) {
260                 CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
261                        rc, (int)sizeof(info->sti_space));
262                 rc = -EIO;
263         }
264
265         RETURN(rc);
266 }
267
268 int seq_store_init(struct lu_server_seq *seq,
269                    const struct lu_env *env,
270                    struct dt_device *dt)
271 {
272         struct dt_object *dt_obj;
273         struct lu_fid fid;
274         const char *name;
275         int rc;
276         ENTRY;
277
278         name = seq->lss_type == LUSTRE_SEQ_SERVER ?
279                 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
280
281         dt_obj = dt_store_open(env, dt, "", name, &fid);
282         if (!IS_ERR(dt_obj)) {
283                 seq->lss_obj = dt_obj;
284                 rc = 0;
285         } else {
286                 CERROR("%s: Can't find \"%s\" obj %d\n",
287                        seq->lss_name, name, (int)PTR_ERR(dt_obj));
288                 rc = PTR_ERR(dt_obj);
289         }
290
291         RETURN(rc);
292 }
293
294 void seq_store_fini(struct lu_server_seq *seq,
295                     const struct lu_env *env)
296 {
297         ENTRY;
298
299         if (seq->lss_obj != NULL) {
300                 if (!IS_ERR(seq->lss_obj))
301                         lu_object_put(env, &seq->lss_obj->do_lu);
302                 seq->lss_obj = NULL;
303         }
304
305         EXIT;
306 }
307 #endif