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