summaryrefslogtreecommitdiff
path: root/drivers/staging/lustre/lustre/fid/fid_store.c
blob: a90e6e37d68990326ee4bfe2a91cc332cb6d4e0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * GPL HEADER START
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License version 2 for more details (a copy is included
 * in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; If not, see
 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 * GPL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright (c) 2011, 2013, Intel Corporation.
 */
/*
 * This file is part of Lustre, http://www.lustre.org/
 * Lustre is a trademark of Sun Microsystems, Inc.
 *
 * lustre/fid/fid_store.c
 *
 * Lustre Sequence Manager
 *
 * Author: Yury Umanets <umka@clusterfs.com>
 */

#define DEBUG_SUBSYSTEM S_FID

# include <linux/libcfs/libcfs.h>
# include <linux/module.h>

#include <obd.h>
#include <obd_class.h>
#include <dt_object.h>
#include <md_object.h>
#include <obd_support.h>
#include <lustre_req_layout.h>
#include <lustre_fid.h>
#include "fid_internal.h"


static struct lu_buf *seq_store_buf(struct seq_thread_info *info)
{
	struct lu_buf *buf;

	buf = &info->sti_buf;
	buf->lb_buf = &info->sti_space;
	buf->lb_len = sizeof(info->sti_space);
	return buf;
}

struct seq_update_callback {
	struct dt_txn_commit_cb suc_cb;
	struct lu_server_seq   *suc_seq;
};

void seq_update_cb(struct lu_env *env, struct thandle *th,
		   struct dt_txn_commit_cb *cb, int err)
{
	struct seq_update_callback *ccb;

	ccb = container_of0(cb, struct seq_update_callback, suc_cb);

	LASSERT(ccb->suc_seq != NULL);

	ccb->suc_seq->lss_need_sync = 0;
	OBD_FREE_PTR(ccb);
}

int seq_update_cb_add(struct thandle *th, struct lu_server_seq *seq)
{
	struct seq_update_callback *ccb;
	struct dt_txn_commit_cb	   *dcb;
	int			   rc;

	OBD_ALLOC_PTR(ccb);
	if (ccb == NULL)
		return -ENOMEM;

	ccb->suc_seq	   = seq;
	seq->lss_need_sync = 1;

	dcb	       = &ccb->suc_cb;
	dcb->dcb_func  = seq_update_cb;
	INIT_LIST_HEAD(&dcb->dcb_linkage);
	strncpy(dcb->dcb_name, "seq_update_cb", MAX_COMMIT_CB_STR_LEN);
	dcb->dcb_name[MAX_COMMIT_CB_STR_LEN - 1] = '\0';

	rc = dt_trans_cb_add(th, dcb);
	if (rc)
		OBD_FREE_PTR(ccb);
	return rc;
}

/* This function implies that caller takes care about locking. */
int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
		     struct lu_seq_range *out, int sync)
{
	struct dt_device *dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
	struct seq_thread_info *info;
	struct thandle *th;
	loff_t pos = 0;
	int rc;

	info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
	LASSERT(info != NULL);

	th = dt_trans_create(env, dt_dev);
	if (IS_ERR(th))
		RETURN(PTR_ERR(th));

	rc = dt_declare_record_write(env, seq->lss_obj,
				     sizeof(struct lu_seq_range), 0, th);
	if (rc)
		GOTO(exit, rc);

	if (out != NULL) {
		rc = fld_declare_server_create(env,
					       seq->lss_site->ss_server_fld,
					       out, th);
		if (rc)
			GOTO(exit, rc);
	}

	rc = dt_trans_start_local(env, dt_dev, th);
	if (rc)
		GOTO(exit, rc);

	/* Store ranges in le format. */
	range_cpu_to_le(&info->sti_space, &seq->lss_space);

	rc = dt_record_write(env, seq->lss_obj, seq_store_buf(info), &pos, th);
	if (rc) {
		CERROR("%s: Can't write space data, rc %d\n",
		       seq->lss_name, rc);
		GOTO(exit, rc);
	} else if (out != NULL) {
		rc = fld_server_create(env, seq->lss_site->ss_server_fld, out,
				       th);
		if (rc) {
			CERROR("%s: Can't Update fld database, rc %d\n",
				seq->lss_name, rc);
			GOTO(exit, rc);
		}
	}
	/* next sequence update will need sync until this update is committed
	 * in case of sync operation this is not needed obviously */
	if (!sync)
		/* if callback can't be added then sync always */
		sync = !!seq_update_cb_add(th, seq);

	th->th_sync |= sync;
exit:
	dt_trans_stop(env, dt_dev, th);
	return rc;
}

/*
 * This function implies that caller takes care about locking or locking is not
 * needed (init time).
 */
int seq_store_read(struct lu_server_seq *seq,
		   const struct lu_env *env)
{
	struct seq_thread_info *info;
	loff_t pos = 0;
	int rc;
	ENTRY;

	info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
	LASSERT(info != NULL);

	rc = seq->lss_obj->do_body_ops->dbo_read(env, seq->lss_obj,
						 seq_store_buf(info),
						 &pos, BYPASS_CAPA);

	if (rc == sizeof(info->sti_space)) {
		range_le_to_cpu(&seq->lss_space, &info->sti_space);
		CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
		       seq->lss_name, PRANGE(&seq->lss_space));
		rc = 0;
	} else if (rc == 0) {
		rc = -ENODATA;
	} else if (rc > 0) {
		CERROR("%s: Read only %d bytes of %d\n", seq->lss_name,
		       rc, (int)sizeof(info->sti_space));
		rc = -EIO;
	}

	RETURN(rc);
}

int seq_store_init(struct lu_server_seq *seq,
		   const struct lu_env *env,
		   struct dt_device *dt)
{
	struct dt_object *dt_obj;
	struct lu_fid fid;
	struct lu_attr attr;
	struct dt_object_format dof;
	const char *name;
	int rc;
	ENTRY;

	name = seq->lss_type == LUSTRE_SEQ_SERVER ?
		LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;

	if (seq->lss_type == LUSTRE_SEQ_SERVER)
		lu_local_obj_fid(&fid, FID_SEQ_SRV_OID);
	else
		lu_local_obj_fid(&fid, FID_SEQ_CTL_OID);

	memset(&attr, 0, sizeof(attr));
	attr.la_valid = LA_MODE;
	attr.la_mode = S_IFREG | 0666;
	dof.dof_type = DFT_REGULAR;

	dt_obj = dt_find_or_create(env, dt, &fid, &dof, &attr);
	if (!IS_ERR(dt_obj)) {
		seq->lss_obj = dt_obj;
		rc = 0;
	} else {
		CERROR("%s: Can't find \"%s\" obj %d\n",
		       seq->lss_name, name, (int)PTR_ERR(dt_obj));
		rc = PTR_ERR(dt_obj);
	}

	RETURN(rc);
}

void seq_store_fini(struct lu_server_seq *seq,
		    const struct lu_env *env)
{
	ENTRY;

	if (seq->lss_obj != NULL) {
		if (!IS_ERR(seq->lss_obj))
			lu_object_put(env, &seq->lss_obj->do_lu);
		seq->lss_obj = NULL;
	}

	EXIT;
}