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#define DEBUG_SUBSYSTEM S_FID
44
45#include "../../include/linux/libcfs/libcfs.h"
46#include <linux/module.h>
47
48#include "../include/obd.h"
49#include "../include/obd_class.h"
50#include "../include/obd_support.h"
51#include "../include/lustre_fid.h"
52
53#include "../include/lustre_mdc.h"
54#include "fid_internal.h"
55
56static struct dentry *seq_debugfs_dir;
57
58static int seq_client_rpc(struct lu_client_seq *seq,
59 struct lu_seq_range *output, __u32 opc,
60 const char *opcname)
61{
62 struct obd_export *exp = seq->lcs_exp;
63 struct ptlrpc_request *req;
64 struct lu_seq_range *out, *in;
65 __u32 *op;
66 unsigned int debug_mask;
67 int rc;
68
69 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
70 LUSTRE_MDS_VERSION, SEQ_QUERY);
71 if (!req)
72 return -ENOMEM;
73
74
75 op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
76 *op = opc;
77
78
79 in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
80 range_init(in);
81
82 ptlrpc_request_set_replen(req);
83
84 in->lsr_index = seq->lcs_space.lsr_index;
85 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
86 fld_range_set_mdt(in);
87 else
88 fld_range_set_ost(in);
89
90 if (opc == SEQ_ALLOC_SUPER) {
91 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
92 req->rq_reply_portal = MDC_REPLY_PORTAL;
93
94
95
96
97
98
99
100 if (seq->lcs_type == LUSTRE_SEQ_DATA)
101 req->rq_no_delay = req->rq_no_resend = 1;
102 debug_mask = D_CONSOLE;
103 } else {
104 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
105 req->rq_request_portal = SEQ_METADATA_PORTAL;
106 else
107 req->rq_request_portal = SEQ_DATA_PORTAL;
108 debug_mask = D_INFO;
109 }
110
111 ptlrpc_at_set_req_timeout(req);
112
113 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
114 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
115 rc = ptlrpc_queue_wait(req);
116 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
117 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
118 if (rc)
119 goto out_req;
120
121 out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
122 *output = *out;
123
124 if (!range_is_sane(output)) {
125 CERROR("%s: Invalid range received from server: "
126 DRANGE"\n", seq->lcs_name, PRANGE(output));
127 rc = -EINVAL;
128 goto out_req;
129 }
130
131 if (range_is_exhausted(output)) {
132 CERROR("%s: Range received from server is exhausted: "
133 DRANGE"]\n", seq->lcs_name, PRANGE(output));
134 rc = -EINVAL;
135 goto out_req;
136 }
137
138 CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
139 seq->lcs_name, opcname, PRANGE(output));
140
141out_req:
142 ptlrpc_req_finished(req);
143 return rc;
144}
145
146
147static int seq_client_alloc_meta(const struct lu_env *env,
148 struct lu_client_seq *seq)
149{
150 int rc;
151
152 do {
153
154
155
156
157
158 rc = seq_client_rpc(seq, &seq->lcs_space,
159 SEQ_ALLOC_META, "meta");
160 } while (rc == -EINPROGRESS || rc == -EAGAIN);
161
162 return rc;
163}
164
165
166static int seq_client_alloc_seq(const struct lu_env *env,
167 struct lu_client_seq *seq, u64 *seqnr)
168{
169 int rc;
170
171 LASSERT(range_is_sane(&seq->lcs_space));
172
173 if (range_is_exhausted(&seq->lcs_space)) {
174 rc = seq_client_alloc_meta(env, seq);
175 if (rc) {
176 CERROR("%s: Can't allocate new meta-sequence, rc %d\n",
177 seq->lcs_name, rc);
178 return rc;
179 }
180 CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
181 seq->lcs_name, PRANGE(&seq->lcs_space));
182 } else {
183 rc = 0;
184 }
185
186 LASSERT(!range_is_exhausted(&seq->lcs_space));
187 *seqnr = seq->lcs_space.lsr_start;
188 seq->lcs_space.lsr_start += 1;
189
190 CDEBUG(D_INFO, "%s: Allocated sequence [%#llx]\n", seq->lcs_name,
191 *seqnr);
192
193 return rc;
194}
195
196static int seq_fid_alloc_prep(struct lu_client_seq *seq,
197 wait_queue_t *link)
198{
199 if (seq->lcs_update) {
200 add_wait_queue(&seq->lcs_waitq, link);
201 set_current_state(TASK_UNINTERRUPTIBLE);
202 mutex_unlock(&seq->lcs_mutex);
203
204 schedule();
205
206 mutex_lock(&seq->lcs_mutex);
207 remove_wait_queue(&seq->lcs_waitq, link);
208 set_current_state(TASK_RUNNING);
209 return -EAGAIN;
210 }
211 ++seq->lcs_update;
212 mutex_unlock(&seq->lcs_mutex);
213 return 0;
214}
215
216static void seq_fid_alloc_fini(struct lu_client_seq *seq)
217{
218 LASSERT(seq->lcs_update == 1);
219 mutex_lock(&seq->lcs_mutex);
220 --seq->lcs_update;
221 wake_up(&seq->lcs_waitq);
222}
223
224
225int seq_client_alloc_fid(const struct lu_env *env,
226 struct lu_client_seq *seq, struct lu_fid *fid)
227{
228 wait_queue_t link;
229 int rc;
230
231 LASSERT(seq);
232 LASSERT(fid);
233
234 init_waitqueue_entry(&link, current);
235 mutex_lock(&seq->lcs_mutex);
236
237 if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
238 seq->lcs_fid.f_oid = seq->lcs_width;
239
240 while (1) {
241 u64 seqnr;
242
243 if (!fid_is_zero(&seq->lcs_fid) &&
244 fid_oid(&seq->lcs_fid) < seq->lcs_width) {
245
246 seq->lcs_fid.f_oid += 1;
247 rc = 0;
248 break;
249 }
250
251 rc = seq_fid_alloc_prep(seq, &link);
252 if (rc)
253 continue;
254
255 rc = seq_client_alloc_seq(env, seq, &seqnr);
256 if (rc) {
257 CERROR("%s: Can't allocate new sequence, rc %d\n",
258 seq->lcs_name, rc);
259 seq_fid_alloc_fini(seq);
260 mutex_unlock(&seq->lcs_mutex);
261 return rc;
262 }
263
264 CDEBUG(D_INFO, "%s: Switch to sequence [0x%16.16Lx]\n",
265 seq->lcs_name, seqnr);
266
267 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
268 seq->lcs_fid.f_seq = seqnr;
269 seq->lcs_fid.f_ver = 0;
270
271
272
273
274
275 rc = 1;
276
277 seq_fid_alloc_fini(seq);
278 break;
279 }
280
281 *fid = seq->lcs_fid;
282 mutex_unlock(&seq->lcs_mutex);
283
284 CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name, PFID(fid));
285 return rc;
286}
287EXPORT_SYMBOL(seq_client_alloc_fid);
288
289
290
291
292
293void seq_client_flush(struct lu_client_seq *seq)
294{
295 wait_queue_t link;
296
297 LASSERT(seq);
298 init_waitqueue_entry(&link, current);
299 mutex_lock(&seq->lcs_mutex);
300
301 while (seq->lcs_update) {
302 add_wait_queue(&seq->lcs_waitq, &link);
303 set_current_state(TASK_UNINTERRUPTIBLE);
304 mutex_unlock(&seq->lcs_mutex);
305
306 schedule();
307
308 mutex_lock(&seq->lcs_mutex);
309 remove_wait_queue(&seq->lcs_waitq, &link);
310 set_current_state(TASK_RUNNING);
311 }
312
313 fid_zero(&seq->lcs_fid);
314
315
316
317
318
319 seq->lcs_space.lsr_index = -1;
320
321 range_init(&seq->lcs_space);
322 mutex_unlock(&seq->lcs_mutex);
323}
324EXPORT_SYMBOL(seq_client_flush);
325
326static void seq_client_debugfs_fini(struct lu_client_seq *seq)
327{
328 if (!IS_ERR_OR_NULL(seq->lcs_debugfs_entry))
329 ldebugfs_remove(&seq->lcs_debugfs_entry);
330}
331
332static int seq_client_debugfs_init(struct lu_client_seq *seq)
333{
334 int rc;
335
336 seq->lcs_debugfs_entry = ldebugfs_register(seq->lcs_name,
337 seq_debugfs_dir,
338 NULL, NULL);
339
340 if (IS_ERR_OR_NULL(seq->lcs_debugfs_entry)) {
341 CERROR("%s: LdebugFS failed in seq-init\n", seq->lcs_name);
342 rc = seq->lcs_debugfs_entry ? PTR_ERR(seq->lcs_debugfs_entry)
343 : -ENOMEM;
344 seq->lcs_debugfs_entry = NULL;
345 return rc;
346 }
347
348 rc = ldebugfs_add_vars(seq->lcs_debugfs_entry,
349 seq_client_debugfs_list, seq);
350 if (rc) {
351 CERROR("%s: Can't init sequence manager debugfs, rc %d\n",
352 seq->lcs_name, rc);
353 goto out_cleanup;
354 }
355
356 return 0;
357
358out_cleanup:
359 seq_client_debugfs_fini(seq);
360 return rc;
361}
362
363static void seq_client_fini(struct lu_client_seq *seq)
364{
365 seq_client_debugfs_fini(seq);
366
367 if (seq->lcs_exp) {
368 class_export_put(seq->lcs_exp);
369 seq->lcs_exp = NULL;
370 }
371}
372
373static int seq_client_init(struct lu_client_seq *seq,
374 struct obd_export *exp,
375 enum lu_cli_type type,
376 const char *prefix)
377{
378 int rc;
379
380 LASSERT(seq);
381 LASSERT(prefix);
382
383 seq->lcs_type = type;
384
385 mutex_init(&seq->lcs_mutex);
386 if (type == LUSTRE_SEQ_METADATA)
387 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
388 else
389 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
390
391 init_waitqueue_head(&seq->lcs_waitq);
392
393 seq_client_flush(seq);
394
395 seq->lcs_exp = class_export_get(exp);
396
397 snprintf(seq->lcs_name, sizeof(seq->lcs_name),
398 "cli-%s", prefix);
399
400 rc = seq_client_debugfs_init(seq);
401 if (rc)
402 seq_client_fini(seq);
403 return rc;
404}
405
406int client_fid_init(struct obd_device *obd,
407 struct obd_export *exp, enum lu_cli_type type)
408{
409 struct client_obd *cli = &obd->u.cli;
410 char *prefix;
411 int rc;
412
413 cli->cl_seq = kzalloc(sizeof(*cli->cl_seq), GFP_NOFS);
414 if (!cli->cl_seq)
415 return -ENOMEM;
416
417 prefix = kzalloc(MAX_OBD_NAME + 5, GFP_NOFS);
418 if (!prefix) {
419 rc = -ENOMEM;
420 goto out_free_seq;
421 }
422
423 snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
424
425
426 rc = seq_client_init(cli->cl_seq, exp, type, prefix);
427 kfree(prefix);
428 if (rc)
429 goto out_free_seq;
430
431 return rc;
432out_free_seq:
433 kfree(cli->cl_seq);
434 cli->cl_seq = NULL;
435 return rc;
436}
437EXPORT_SYMBOL(client_fid_init);
438
439int client_fid_fini(struct obd_device *obd)
440{
441 struct client_obd *cli = &obd->u.cli;
442
443 if (cli->cl_seq) {
444 seq_client_fini(cli->cl_seq);
445 kfree(cli->cl_seq);
446 cli->cl_seq = NULL;
447 }
448
449 return 0;
450}
451EXPORT_SYMBOL(client_fid_fini);
452
453static int __init fid_init(void)
454{
455 seq_debugfs_dir = ldebugfs_register(LUSTRE_SEQ_NAME,
456 debugfs_lustre_root,
457 NULL, NULL);
458 return PTR_ERR_OR_ZERO(seq_debugfs_dir);
459}
460
461static void __exit fid_exit(void)
462{
463 if (!IS_ERR_OR_NULL(seq_debugfs_dir))
464 ldebugfs_remove(&seq_debugfs_dir);
465}
466
467MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
468MODULE_DESCRIPTION("Lustre File IDentifier");
469MODULE_VERSION(LUSTRE_VERSION_STRING);
470MODULE_LICENSE("GPL");
471
472module_init(fid_init);
473module_exit(fid_exit);
474