Whamcloud - gitweb
LU-1445 fid: start ptlrpc service for OST FID
[fs/lustre-release.git] / lustre / fid / fid_request.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, Intel Corporation.
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_request.c
37  *
38  * Lustre Sequence Manager
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FID
44
45 #ifdef __KERNEL__
46 # include <libcfs/libcfs.h>
47 # include <linux/module.h>
48 #else /* __KERNEL__ */
49 # include <liblustre.h>
50 #endif
51
52 #include <obd.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 /* mdc RPC locks */
60 #include <lustre_mdc.h>
61 #include "fid_internal.h"
62
63 static int seq_client_rpc(struct lu_client_seq *seq,
64                           struct lu_seq_range *output, __u32 opc,
65                           const char *opcname)
66 {
67         struct obd_export     *exp = seq->lcs_exp;
68         struct ptlrpc_request *req;
69         struct lu_seq_range   *out, *in;
70         __u32                 *op;
71         unsigned int           debug_mask;
72         int                    rc;
73         ENTRY;
74
75         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
76                                         LUSTRE_MDS_VERSION, SEQ_QUERY);
77         if (req == NULL)
78                 RETURN(-ENOMEM);
79
80         /* Init operation code */
81         op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
82         *op = opc;
83
84         /* Zero out input range, this is not recovery yet. */
85         in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
86         range_init(in);
87
88         ptlrpc_request_set_replen(req);
89
90         in->lsr_index = seq->lcs_space.lsr_index;
91         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
92                 in->lsr_flags = LU_SEQ_RANGE_MDT;
93         else
94                 in->lsr_flags = LU_SEQ_RANGE_OST;
95
96         if (opc == SEQ_ALLOC_SUPER) {
97                 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
98                 req->rq_reply_portal = MDC_REPLY_PORTAL;
99                 /* During allocating super sequence for data object,
100                  * the current thread might hold the export of MDT0(MDT0
101                  * precreating objects on this OST), and it will send the
102                  * request to MDT0 here, so we can not keep resending the
103                  * request here, otherwise if MDT0 is failed(umounted),
104                  * it can not release the export of MDT0 */
105                 if (seq->lcs_type == LUSTRE_SEQ_DATA)
106                         req->rq_no_delay = req->rq_no_resend = 1;
107                 debug_mask = D_CONSOLE;
108         } else {
109                 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
110                         req->rq_request_portal = SEQ_METADATA_PORTAL;
111                 else
112                         req->rq_request_portal = SEQ_DATA_PORTAL;
113                 debug_mask = D_INFO;
114         }
115
116         ptlrpc_at_set_req_timeout(req);
117
118         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
119                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
120         rc = ptlrpc_queue_wait(req);
121         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
122                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
123         if (rc)
124                 GOTO(out_req, rc);
125
126         out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
127         *output = *out;
128
129         if (!range_is_sane(output)) {
130                 CERROR("%s: Invalid range received from server: "
131                        DRANGE"\n", seq->lcs_name, PRANGE(output));
132                 GOTO(out_req, rc = -EINVAL);
133         }
134
135         if (range_is_exhausted(output)) {
136                 CERROR("%s: Range received from server is exhausted: "
137                        DRANGE"]\n", seq->lcs_name, PRANGE(output));
138                 GOTO(out_req, rc = -EINVAL);
139         }
140
141         CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
142                      seq->lcs_name, opcname, PRANGE(output));
143
144         EXIT;
145 out_req:
146         ptlrpc_req_finished(req);
147         return rc;
148 }
149
150 /* Request sequence-controller node to allocate new super-sequence. */
151 int seq_client_alloc_super(struct lu_client_seq *seq,
152                            const struct lu_env *env)
153 {
154         int rc;
155         ENTRY;
156
157         mutex_lock(&seq->lcs_mutex);
158
159 #ifdef __KERNEL__
160         if (seq->lcs_srv) {
161                 LASSERT(env != NULL);
162                 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
163                                             env);
164         } else {
165 #endif
166                 /* Check whether the connection to seq controller has been
167                  * setup (lcs_exp != NULL) */
168                 if (seq->lcs_exp == NULL) {
169                         mutex_unlock(&seq->lcs_mutex);
170                         RETURN(-EINPROGRESS);
171                 }
172
173                 rc = seq_client_rpc(seq, &seq->lcs_space,
174                                     SEQ_ALLOC_SUPER, "super");
175 #ifdef __KERNEL__
176         }
177 #endif
178         mutex_unlock(&seq->lcs_mutex);
179         RETURN(rc);
180 }
181
182 /* Request sequence-controller node to allocate new meta-sequence. */
183 static int seq_client_alloc_meta(const struct lu_env *env,
184                                  struct lu_client_seq *seq)
185 {
186         int rc;
187         ENTRY;
188
189 #ifdef __KERNEL__
190         if (seq->lcs_srv) {
191                 LASSERT(env != NULL);
192                 rc = seq_server_alloc_meta(seq->lcs_srv, &seq->lcs_space, env);
193         } else {
194 #endif
195                 do {
196                         /* If meta server return -EINPROGRESS or EAGAIN,
197                          * it means meta server might not be ready to
198                          * allocate super sequence from sequence controller
199                          * (MDT0)yet */
200                         rc = seq_client_rpc(seq, &seq->lcs_space,
201                                             SEQ_ALLOC_META, "meta");
202                 } while (rc == -EINPROGRESS || rc == -EAGAIN);
203 #ifdef __KERNEL__
204         }
205 #endif
206         RETURN(rc);
207 }
208
209 /* Allocate new sequence for client. */
210 static int seq_client_alloc_seq(const struct lu_env *env,
211                                 struct lu_client_seq *seq, seqno_t *seqnr)
212 {
213         int rc;
214         ENTRY;
215
216         LASSERT(range_is_sane(&seq->lcs_space));
217
218         if (range_is_exhausted(&seq->lcs_space)) {
219                 rc = seq_client_alloc_meta(env, seq);
220                 if (rc) {
221                         CERROR("%s: Can't allocate new meta-sequence,"
222                                "rc %d\n", seq->lcs_name, rc);
223                         RETURN(rc);
224                 } else {
225                         CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
226                                seq->lcs_name, PRANGE(&seq->lcs_space));
227                 }
228         } else {
229                 rc = 0;
230         }
231
232         LASSERT(!range_is_exhausted(&seq->lcs_space));
233         *seqnr = seq->lcs_space.lsr_start;
234         seq->lcs_space.lsr_start += 1;
235
236         CDEBUG(D_INFO, "%s: Allocated sequence ["LPX64"]\n", seq->lcs_name,
237                *seqnr);
238
239         RETURN(rc);
240 }
241
242 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
243                               cfs_waitlink_t *link)
244 {
245         if (seq->lcs_update) {
246                 cfs_waitq_add(&seq->lcs_waitq, link);
247                 cfs_set_current_state(CFS_TASK_UNINT);
248                 mutex_unlock(&seq->lcs_mutex);
249
250                 cfs_waitq_wait(link, CFS_TASK_UNINT);
251
252                 mutex_lock(&seq->lcs_mutex);
253                 cfs_waitq_del(&seq->lcs_waitq, link);
254                 cfs_set_current_state(CFS_TASK_RUNNING);
255                 return -EAGAIN;
256         }
257         ++seq->lcs_update;
258         mutex_unlock(&seq->lcs_mutex);
259         return 0;
260 }
261
262 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
263 {
264         LASSERT(seq->lcs_update == 1);
265         mutex_lock(&seq->lcs_mutex);
266         --seq->lcs_update;
267         cfs_waitq_signal(&seq->lcs_waitq);
268 }
269
270 /**
271  * Allocate the whole seq to the caller, currently it would be
272  * only used by echo client to access MDT
273  **/
274 int seq_client_get_seq(const struct lu_env *env,
275                        struct lu_client_seq *seq, seqno_t *seqnr)
276 {
277         cfs_waitlink_t link;
278         int rc;
279
280         LASSERT(seqnr != NULL);
281         mutex_lock(&seq->lcs_mutex);
282         cfs_waitlink_init(&link);
283
284         while (1) {
285                 rc = seq_fid_alloc_prep(seq, &link);
286                 if (rc == 0)
287                         break;
288         }
289
290         rc = seq_client_alloc_seq(env, seq, seqnr);
291         if (rc) {
292                 CERROR("%s: Can't allocate new sequence, "
293                        "rc %d\n", seq->lcs_name, rc);
294                 seq_fid_alloc_fini(seq);
295                 mutex_unlock(&seq->lcs_mutex);
296                 return rc;
297         }
298
299         CDEBUG(D_INFO, "%s: allocate sequence "
300                "[0x%16.16"LPF64"x]\n", seq->lcs_name, *seqnr);
301
302         /* Since the caller require the whole seq,
303          * so marked this seq to be used */
304         LASSERT(seq->lcs_type == LUSTRE_SEQ_METADATA);
305         seq->lcs_fid.f_oid = LUSTRE_METADATA_SEQ_MAX_WIDTH;
306         seq->lcs_fid.f_seq = *seqnr;
307         seq->lcs_fid.f_ver = 0;
308
309         /*
310          * Inform caller that sequence switch is performed to allow it
311          * to setup FLD for it.
312          */
313         seq_fid_alloc_fini(seq);
314         mutex_unlock(&seq->lcs_mutex);
315
316         return rc;
317 }
318 EXPORT_SYMBOL(seq_client_get_seq);
319
320 /* Allocate new fid on passed client @seq and save it to @fid. */
321 int seq_client_alloc_fid(const struct lu_env *env,
322                          struct lu_client_seq *seq, struct lu_fid *fid)
323 {
324         cfs_waitlink_t link;
325         int rc;
326         ENTRY;
327
328         LASSERT(seq != NULL);
329         LASSERT(fid != NULL);
330
331         cfs_waitlink_init(&link);
332         mutex_lock(&seq->lcs_mutex);
333
334         if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
335                 seq->lcs_fid.f_oid = seq->lcs_width;
336
337         while (1) {
338                 seqno_t seqnr;
339
340                 if (!fid_is_zero(&seq->lcs_fid) &&
341                     fid_oid(&seq->lcs_fid) < seq->lcs_width) {
342                         /* Just bump last allocated fid and return to caller. */
343                         seq->lcs_fid.f_oid += 1;
344                         rc = 0;
345                         break;
346                 }
347
348                 rc = seq_fid_alloc_prep(seq, &link);
349                 if (rc)
350                         continue;
351
352                 rc = seq_client_alloc_seq(env, seq, &seqnr);
353                 if (rc) {
354                         CERROR("%s: Can't allocate new sequence, "
355                                "rc %d\n", seq->lcs_name, rc);
356                         seq_fid_alloc_fini(seq);
357                         mutex_unlock(&seq->lcs_mutex);
358                         RETURN(rc);
359                 }
360
361                 CDEBUG(D_INFO, "%s: Switch to sequence "
362                        "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
363
364                 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
365                 seq->lcs_fid.f_seq = seqnr;
366                 seq->lcs_fid.f_ver = 0;
367
368                 /*
369                  * Inform caller that sequence switch is performed to allow it
370                  * to setup FLD for it.
371                  */
372                 rc = 1;
373
374                 seq_fid_alloc_fini(seq);
375                 break;
376         }
377
378         *fid = seq->lcs_fid;
379         mutex_unlock(&seq->lcs_mutex);
380
381         CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name,  PFID(fid));
382         RETURN(rc);
383 }
384 EXPORT_SYMBOL(seq_client_alloc_fid);
385
386 /*
387  * Finish the current sequence due to disconnect.
388  * See mdc_import_event()
389  */
390 void seq_client_flush(struct lu_client_seq *seq)
391 {
392         cfs_waitlink_t link;
393
394         LASSERT(seq != NULL);
395         cfs_waitlink_init(&link);
396         mutex_lock(&seq->lcs_mutex);
397
398         while (seq->lcs_update) {
399                 cfs_waitq_add(&seq->lcs_waitq, &link);
400                 cfs_set_current_state(CFS_TASK_UNINT);
401                 mutex_unlock(&seq->lcs_mutex);
402
403                 cfs_waitq_wait(&link, CFS_TASK_UNINT);
404
405                 mutex_lock(&seq->lcs_mutex);
406                 cfs_waitq_del(&seq->lcs_waitq, &link);
407                 cfs_set_current_state(CFS_TASK_RUNNING);
408         }
409
410         fid_zero(&seq->lcs_fid);
411         /**
412          * this id shld not be used for seq range allocation.
413          * set to -1 for dgb check.
414          */
415
416         seq->lcs_space.lsr_index = -1;
417
418         range_init(&seq->lcs_space);
419         mutex_unlock(&seq->lcs_mutex);
420 }
421 EXPORT_SYMBOL(seq_client_flush);
422
423 static void seq_client_proc_fini(struct lu_client_seq *seq);
424
425 #ifdef LPROCFS
426 static int seq_client_proc_init(struct lu_client_seq *seq)
427 {
428         int rc;
429         ENTRY;
430
431         seq->lcs_proc_dir = lprocfs_register(seq->lcs_name,
432                                              seq_type_proc_dir,
433                                              NULL, NULL);
434
435         if (IS_ERR(seq->lcs_proc_dir)) {
436                 CERROR("%s: LProcFS failed in seq-init\n",
437                        seq->lcs_name);
438                 rc = PTR_ERR(seq->lcs_proc_dir);
439                 RETURN(rc);
440         }
441
442         rc = lprocfs_add_vars(seq->lcs_proc_dir,
443                               seq_client_proc_list, seq);
444         if (rc) {
445                 CERROR("%s: Can't init sequence manager "
446                        "proc, rc %d\n", seq->lcs_name, rc);
447                 GOTO(out_cleanup, rc);
448         }
449
450         RETURN(0);
451
452 out_cleanup:
453         seq_client_proc_fini(seq);
454         return rc;
455 }
456
457 static void seq_client_proc_fini(struct lu_client_seq *seq)
458 {
459         ENTRY;
460         if (seq->lcs_proc_dir) {
461                 if (!IS_ERR(seq->lcs_proc_dir))
462                         lprocfs_remove(&seq->lcs_proc_dir);
463                 seq->lcs_proc_dir = NULL;
464         }
465         EXIT;
466 }
467 #else
468 static int seq_client_proc_init(struct lu_client_seq *seq)
469 {
470         return 0;
471 }
472
473 static void seq_client_proc_fini(struct lu_client_seq *seq)
474 {
475         return;
476 }
477 #endif
478
479 int seq_client_init(struct lu_client_seq *seq,
480                     struct obd_export *exp,
481                     enum lu_cli_type type,
482                     const char *prefix,
483                     struct lu_server_seq *srv)
484 {
485         int rc;
486         ENTRY;
487
488         LASSERT(seq != NULL);
489         LASSERT(prefix != NULL);
490
491         seq->lcs_srv = srv;
492         seq->lcs_type = type;
493
494         mutex_init(&seq->lcs_mutex);
495         if (type == LUSTRE_SEQ_METADATA)
496                 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
497         else
498                 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
499
500         cfs_waitq_init(&seq->lcs_waitq);
501         /* Make sure that things are clear before work is started. */
502         seq_client_flush(seq);
503
504         if (exp != NULL)
505                 seq->lcs_exp = class_export_get(exp);
506         else if (type == LUSTRE_SEQ_METADATA)
507                 LASSERT(seq->lcs_srv != NULL);
508
509         snprintf(seq->lcs_name, sizeof(seq->lcs_name),
510                  "cli-%s", prefix);
511
512         rc = seq_client_proc_init(seq);
513         if (rc)
514                 seq_client_fini(seq);
515         RETURN(rc);
516 }
517 EXPORT_SYMBOL(seq_client_init);
518
519 void seq_client_fini(struct lu_client_seq *seq)
520 {
521         ENTRY;
522
523         seq_client_proc_fini(seq);
524
525         if (seq->lcs_exp != NULL) {
526                 class_export_put(seq->lcs_exp);
527                 seq->lcs_exp = NULL;
528         }
529
530         seq->lcs_srv = NULL;
531         EXIT;
532 }
533 EXPORT_SYMBOL(seq_client_fini);