Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[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         cfs_list_del(&ccb->suc_cb.dcb_linkage);
88         OBD_FREE_PTR(ccb);
89 }
90
91 struct thandle *seq_store_trans_create(struct lu_server_seq *seq,
92                                        const struct lu_env *env)
93 {
94         struct dt_device *dt_dev;
95
96         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
97         return dt_trans_create(env, dt_dev);
98 }
99
100 int seq_store_trans_start(struct lu_server_seq *seq, const struct lu_env *env,
101                           struct thandle *th)
102 {
103         struct dt_device *dt_dev;
104         ENTRY;
105
106         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
107
108         return dt_trans_start(env, dt_dev, th);
109 }
110
111 int seq_update_cb_add(struct thandle *th, struct lu_server_seq *seq)
112 {
113         struct seq_update_callback *ccb;
114         int rc;
115         OBD_ALLOC_PTR(ccb);
116         if (ccb == NULL)
117                 return -ENOMEM;
118
119         ccb->suc_cb.dcb_func = seq_update_cb;
120         CFS_INIT_LIST_HEAD(&ccb->suc_cb.dcb_linkage);
121         ccb->suc_seq = seq;
122         seq->lss_need_sync = 1;
123         rc = dt_trans_cb_add(th, &ccb->suc_cb);
124         if (rc)
125                 OBD_FREE_PTR(ccb);
126         return rc;
127 }
128
129 int seq_declare_store_write(struct lu_server_seq *seq,
130                             const struct lu_env *env,
131                             struct thandle *th)
132 {
133         struct dt_object *dt_obj = seq->lss_obj;
134         int rc;
135         ENTRY;
136
137         rc = dt_obj->do_body_ops->dbo_declare_write(env, dt_obj,
138                                                     sizeof(struct lu_seq_range),
139                                                     0, th);
140         return rc;
141 }
142
143 /* This function implies that caller takes care about locking. */
144 int seq_store_write(struct lu_server_seq *seq,
145                     const struct lu_env *env,
146                     struct thandle *th)
147 {
148         struct dt_object *dt_obj = seq->lss_obj;
149         struct seq_thread_info *info;
150         loff_t pos = 0;
151         int rc;
152         ENTRY;
153
154         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
155         LASSERT(info != NULL);
156
157         /* Store ranges in le format. */
158         range_cpu_to_le(&info->sti_space, &seq->lss_space);
159
160         rc = dt_obj->do_body_ops->dbo_write(env, dt_obj,
161                                             seq_store_buf(info),
162                                             &pos, th, BYPASS_CAPA, 1);
163         if (rc == sizeof(info->sti_space)) {
164                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
165                        seq->lss_name, PRANGE(&seq->lss_space));
166                 rc = 0;
167         } else if (rc >= 0) {
168                 rc = -EIO;
169         }
170
171
172         RETURN(rc);
173 }
174
175 int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
176                      struct lu_seq_range *out, int sync)
177 {
178         struct dt_device *dt_dev;
179         struct thandle *th;
180         int rc;
181         ENTRY;
182
183         dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
184
185         th = seq_store_trans_create(seq, env);
186         if (IS_ERR(th))
187                 RETURN(PTR_ERR(th));
188
189         rc = seq_declare_store_write(seq, env, th);
190         if (rc)
191                 GOTO(exit, rc);
192
193         if (out != NULL) {
194                 rc = fld_declare_server_create(seq->lss_site->ms_server_fld,
195                                                env, th);
196                 if (rc)
197                         GOTO(exit, rc);
198         }
199
200         rc = seq_store_trans_start(seq, env, th);
201         if (rc)
202                 GOTO(exit, rc);
203
204         rc = seq_store_write(seq, env, th);
205         if (rc) {
206                 CERROR("%s: Can't write space data, rc %d\n",
207                        seq->lss_name, rc);
208                 GOTO(exit,rc);
209         } else if (out != NULL) {
210                 rc = fld_server_create(seq->lss_site->ms_server_fld,
211                                        env, out, th);
212                 if (rc) {
213                         CERROR("%s: Can't Update fld database, rc %d\n",
214                                seq->lss_name, rc);
215                         GOTO(exit,rc);
216                 }
217         }
218
219         /* next sequence update will need sync until this update is committed
220          * in case of sync operation this is not needed obviously */
221         if (!sync)
222                 /* if callback can't be added then sync always */
223                 sync = !!seq_update_cb_add(th, seq);
224
225         th->th_sync |= sync;
226 exit:
227         dt_trans_stop(env, dt_dev, th);
228         return rc;
229 }
230
231 /*
232  * This function implies that caller takes care about locking or locking is not
233  * needed (init time).
234  */
235 int seq_store_read(struct lu_server_seq *seq,
236                    const struct lu_env *env)
237 {
238         struct dt_object *dt_obj = seq->lss_obj;
239         struct seq_thread_info *info;
240         loff_t pos = 0;
241         int rc;
242         ENTRY;
243
244         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
245         LASSERT(info != NULL);
246
247         rc = dt_obj->do_body_ops->dbo_read(env, dt_obj, seq_store_buf(info),
248                                            &pos, BYPASS_CAPA);
249
250         if (rc == sizeof(info->sti_space)) {
251                 range_le_to_cpu(&seq->lss_space, &info->sti_space);
252                 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
253                        seq->lss_name, PRANGE(&seq->lss_space));
254                 rc = 0;
255         } else if (rc == 0) {
256                 rc = -ENODATA;
257         } else if (rc >= 0) {
258                 CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
259                        rc, (int)sizeof(info->sti_space));
260                 rc = -EIO;
261         }
262
263         RETURN(rc);
264 }
265
266 int seq_store_init(struct lu_server_seq *seq,
267                    const struct lu_env *env,
268                    struct dt_device *dt)
269 {
270         struct dt_object *dt_obj;
271         struct lu_fid fid;
272         const char *name;
273         int rc;
274         ENTRY;
275
276         name = seq->lss_type == LUSTRE_SEQ_SERVER ?
277                 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
278
279         dt_obj = dt_store_open(env, dt, "", name, &fid);
280         if (!IS_ERR(dt_obj)) {
281                 seq->lss_obj = dt_obj;
282                 rc = 0;
283         } else {
284                 CERROR("%s: Can't find \"%s\" obj %d\n",
285                        seq->lss_name, name, (int)PTR_ERR(dt_obj));
286                 rc = PTR_ERR(dt_obj);
287         }
288
289         RETURN(rc);
290 }
291
292 void seq_store_fini(struct lu_server_seq *seq,
293                     const struct lu_env *env)
294 {
295         ENTRY;
296
297         if (seq->lss_obj != NULL) {
298                 if (!IS_ERR(seq->lss_obj))
299                         lu_object_put(env, &seq->lss_obj->do_lu);
300                 seq->lss_obj = NULL;
301         }
302
303         EXIT;
304 }
305 #endif