4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/fid/fid_store.c
38 * Lustre Sequence Manager
40 * Author: Yury Umanets <umka@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_FID
46 # include <libcfs/libcfs.h>
47 # include <linux/module.h>
48 #else /* __KERNEL__ */
49 # include <liblustre.h>
53 #include <obd_class.h>
54 #include <dt_object.h>
55 #include <md_object.h>
56 #include <obd_support.h>
57 #include <lustre_req_layout.h>
58 #include <lustre_fid.h>
59 #include "fid_internal.h"
63 static struct lu_buf *seq_store_buf(struct seq_thread_info *info)
68 buf->lb_buf = &info->sti_space;
69 buf->lb_len = sizeof(info->sti_space);
73 struct seq_update_callback {
74 struct dt_txn_commit_cb suc_cb;
75 struct lu_server_seq *suc_seq;
78 void seq_update_cb(struct lu_env *env, struct thandle *th,
79 struct dt_txn_commit_cb *cb, int err)
81 struct seq_update_callback *ccb;
82 ccb = container_of0(cb, struct seq_update_callback, suc_cb);
83 ccb->suc_seq->lss_need_sync = 0;
87 struct thandle *seq_store_trans_create(struct lu_server_seq *seq,
88 const struct lu_env *env)
90 struct dt_device *dt_dev;
92 dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
93 return dt_trans_create(env, dt_dev);
96 int seq_store_trans_start(struct lu_server_seq *seq, const struct lu_env *env,
99 struct dt_device *dt_dev;
102 dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
104 return dt_trans_start(env, dt_dev, th);
107 int seq_update_cb_add(struct thandle *th, struct lu_server_seq *seq)
109 struct seq_update_callback *ccb;
115 ccb->suc_cb.dcb_func = seq_update_cb;
116 CFS_INIT_LIST_HEAD(&ccb->suc_cb.dcb_linkage);
118 seq->lss_need_sync = 1;
119 rc = dt_trans_cb_add(th, &ccb->suc_cb);
125 int seq_declare_store_write(struct lu_server_seq *seq,
126 const struct lu_env *env,
129 struct dt_object *dt_obj = seq->lss_obj;
133 rc = dt_obj->do_body_ops->dbo_declare_write(env, dt_obj,
134 sizeof(struct lu_seq_range),
139 /* This function implies that caller takes care about locking. */
140 int seq_store_write(struct lu_server_seq *seq,
141 const struct lu_env *env,
144 struct dt_object *dt_obj = seq->lss_obj;
145 struct seq_thread_info *info;
150 info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
151 LASSERT(info != NULL);
153 /* Store ranges in le format. */
154 range_cpu_to_le(&info->sti_space, &seq->lss_space);
156 rc = dt_obj->do_body_ops->dbo_write(env, dt_obj,
158 &pos, th, BYPASS_CAPA, 1);
159 if (rc == sizeof(info->sti_space)) {
160 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
161 seq->lss_name, PRANGE(&seq->lss_space));
163 } else if (rc >= 0) {
171 int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
172 struct lu_seq_range *out, int sync)
174 struct dt_device *dt_dev;
179 dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
181 th = seq_store_trans_create(seq, env);
185 rc = seq_declare_store_write(seq, env, th);
190 rc = fld_declare_server_create(seq->lss_site->ms_server_fld,
196 rc = seq_store_trans_start(seq, env, th);
200 rc = seq_store_write(seq, env, th);
202 CERROR("%s: Can't write space data, rc %d\n",
205 } else if (out != NULL) {
206 rc = fld_server_create(seq->lss_site->ms_server_fld,
209 CERROR("%s: Can't Update fld database, rc %d\n",
215 /* next sequence update will need sync until this update is committed
216 * in case of sync operation this is not needed obviously */
218 /* if callback can't be added then sync always */
219 sync = !!seq_update_cb_add(th, seq);
223 dt_trans_stop(env, dt_dev, th);
228 * This function implies that caller takes care about locking or locking is not
229 * needed (init time).
231 int seq_store_read(struct lu_server_seq *seq,
232 const struct lu_env *env)
234 struct dt_object *dt_obj = seq->lss_obj;
235 struct seq_thread_info *info;
240 info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
241 LASSERT(info != NULL);
243 rc = dt_obj->do_body_ops->dbo_read(env, dt_obj, seq_store_buf(info),
246 if (rc == sizeof(info->sti_space)) {
247 range_le_to_cpu(&seq->lss_space, &info->sti_space);
248 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
249 seq->lss_name, PRANGE(&seq->lss_space));
251 } else if (rc == 0) {
253 } else if (rc >= 0) {
254 CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
255 rc, (int)sizeof(info->sti_space));
262 int seq_store_init(struct lu_server_seq *seq,
263 const struct lu_env *env,
264 struct dt_device *dt)
266 struct dt_object *dt_obj;
272 name = seq->lss_type == LUSTRE_SEQ_SERVER ?
273 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
275 dt_obj = dt_store_open(env, dt, "", name, &fid);
276 if (!IS_ERR(dt_obj)) {
277 seq->lss_obj = dt_obj;
280 CERROR("%s: Can't find \"%s\" obj %d\n",
281 seq->lss_name, name, (int)PTR_ERR(dt_obj));
282 rc = PTR_ERR(dt_obj);
288 void seq_store_fini(struct lu_server_seq *seq,
289 const struct lu_env *env)
293 if (seq->lss_obj != NULL) {
294 if (!IS_ERR(seq->lss_obj))
295 lu_object_put(env, &seq->lss_obj->do_lu);