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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/fid/fid_store.c
34 * Lustre Sequence Manager
36 * Author: Yury Umanets <umka@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_FID
41 #include <libcfs/libcfs.h>
42 #include <dt_object.h>
43 #include <obd_support.h>
44 #include <lustre_fid.h>
45 #include <lustre_fld.h>
46 #include "fid_internal.h"
48 static struct lu_buf *seq_store_buf(struct seq_thread_info *info)
53 buf->lb_buf = &info->sti_space;
54 buf->lb_len = sizeof(info->sti_space);
58 struct seq_update_callback {
59 struct dt_txn_commit_cb suc_cb;
60 struct lu_server_seq *suc_seq;
63 void seq_update_cb(struct lu_env *env, struct thandle *th,
64 struct dt_txn_commit_cb *cb, int err)
66 struct seq_update_callback *ccb;
68 ccb = container_of0(cb, struct seq_update_callback, suc_cb);
70 LASSERT(ccb->suc_seq != NULL);
72 ccb->suc_seq->lss_need_sync = 0;
76 int seq_update_cb_add(struct thandle *th, struct lu_server_seq *seq)
78 struct seq_update_callback *ccb;
79 struct dt_txn_commit_cb *dcb;
87 seq->lss_need_sync = 1;
90 dcb->dcb_func = seq_update_cb;
91 INIT_LIST_HEAD(&dcb->dcb_linkage);
92 strlcpy(dcb->dcb_name, "seq_update_cb", sizeof(dcb->dcb_name));
94 rc = dt_trans_cb_add(th, dcb);
100 /* This function implies that caller takes care about locking. */
101 int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
102 struct lu_seq_range *out, int sync)
104 struct dt_device *dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
105 struct seq_thread_info *info;
110 info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
111 LASSERT(info != NULL);
113 th = dt_trans_create(env, dt_dev);
117 /* Store ranges in le format. */
118 range_cpu_to_le(&info->sti_space, &seq->lss_space);
120 rc = dt_declare_record_write(env, seq->lss_obj,
121 seq_store_buf(info), 0, th);
126 rc = fld_declare_server_create(env,
127 seq->lss_site->ss_server_fld,
133 rc = dt_trans_start_local(env, dt_dev, th);
137 rc = dt_record_write(env, seq->lss_obj, seq_store_buf(info), &pos, th);
139 CERROR("%s: Can't write space data, rc %d\n",
142 } else if (out != NULL) {
143 rc = fld_server_create(env, seq->lss_site->ss_server_fld, out,
146 CERROR("%s: Can't Update fld database, rc %d\n",
151 /* next sequence update will need sync until this update is committed
152 * in case of sync operation this is not needed obviously */
154 /* if callback can't be added then sync always */
155 sync = !!seq_update_cb_add(th, seq);
159 dt_trans_stop(env, dt_dev, th);
164 * This function implies that caller takes care about locking or locking is not
165 * needed (init time).
167 int seq_store_read(struct lu_server_seq *seq,
168 const struct lu_env *env)
170 struct seq_thread_info *info;
175 info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
176 LASSERT(info != NULL);
178 rc = dt_read(env, seq->lss_obj, seq_store_buf(info), &pos);
180 if (rc == sizeof(info->sti_space)) {
181 range_le_to_cpu(&seq->lss_space, &info->sti_space);
182 CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
183 seq->lss_name, PRANGE(&seq->lss_space));
185 } else if (rc == 0) {
188 CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
189 rc, (int)sizeof(info->sti_space));
196 int seq_store_init(struct lu_server_seq *seq,
197 const struct lu_env *env,
198 struct dt_device *dt)
200 struct dt_object *dt_obj;
203 struct dt_object_format dof;
208 name = seq->lss_type == LUSTRE_SEQ_SERVER ?
209 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
211 if (seq->lss_type == LUSTRE_SEQ_SERVER)
212 lu_local_obj_fid(&fid, FID_SEQ_SRV_OID);
214 lu_local_obj_fid(&fid, FID_SEQ_CTL_OID);
216 memset(&attr, 0, sizeof(attr));
217 attr.la_valid = LA_MODE;
218 attr.la_mode = S_IFREG | 0666;
219 dof.dof_type = DFT_REGULAR;
221 dt_obj = dt_find_or_create(env, dt, &fid, &dof, &attr);
222 if (!IS_ERR(dt_obj)) {
223 seq->lss_obj = dt_obj;
226 CERROR("%s: Can't find \"%s\" obj %d\n",
227 seq->lss_name, name, (int)PTR_ERR(dt_obj));
228 rc = PTR_ERR(dt_obj);
234 void seq_store_fini(struct lu_server_seq *seq,
235 const struct lu_env *env)
239 if (seq->lss_obj != NULL) {
240 if (!IS_ERR(seq->lss_obj))
241 lu_object_put(env, &seq->lss_obj->do_lu);