Whamcloud - gitweb
LU-10467 fid: style cleanups in seq_client_alloc_meta()
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/fid/fid_request.c
33  *
34  * Lustre Sequence Manager
35  *
36  * Author: Yury Umanets <umka@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_FID
40
41 #include <linux/err.h>
42 #include <linux/module.h>
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <lustre_fid.h>
47 /* mdc RPC locks */
48 #include <lustre_mdc.h>
49 #include "fid_internal.h"
50
51 struct dentry *seq_debugfs_dir;
52
53 static int seq_client_rpc(struct lu_client_seq *seq,
54                           struct lu_seq_range *output, __u32 opc,
55                           const char *opcname)
56 {
57         struct obd_export     *exp = seq->lcs_exp;
58         struct ptlrpc_request *req;
59         struct lu_seq_range   *out, *in;
60         __u32                 *op;
61         unsigned int           debug_mask;
62         int                    rc;
63         ENTRY;
64
65         LASSERT(exp != NULL && !IS_ERR(exp));
66         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
67                                         LUSTRE_MDS_VERSION, SEQ_QUERY);
68         if (req == NULL)
69                 RETURN(-ENOMEM);
70
71         /* Init operation code */
72         op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
73         *op = opc;
74
75         /* Zero out input range, this is not recovery yet. */
76         in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
77         lu_seq_range_init(in);
78
79         ptlrpc_request_set_replen(req);
80
81         in->lsr_index = seq->lcs_space.lsr_index;
82         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
83                 fld_range_set_mdt(in);
84         else
85                 fld_range_set_ost(in);
86
87         if (opc == SEQ_ALLOC_SUPER) {
88                 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
89                 req->rq_reply_portal = MDC_REPLY_PORTAL;
90                 /* During allocating super sequence for data object,
91                  * the current thread might hold the export of MDT0(MDT0
92                  * precreating objects on this OST), and it will send the
93                  * request to MDT0 here, so we can not keep resending the
94                  * request here, otherwise if MDT0 is failed(umounted),
95                  * it can not release the export of MDT0 */
96                 if (seq->lcs_type == LUSTRE_SEQ_DATA)
97                         req->rq_no_delay = req->rq_no_resend = 1;
98                 debug_mask = D_CONSOLE;
99         } else {
100                 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
101                         req->rq_reply_portal = MDC_REPLY_PORTAL;
102                         req->rq_request_portal = SEQ_METADATA_PORTAL;
103                 } else {
104                         req->rq_reply_portal = OSC_REPLY_PORTAL;
105                         req->rq_request_portal = SEQ_DATA_PORTAL;
106                 }
107
108                 debug_mask = D_INFO;
109         }
110
111         /* Allow seq client RPC during recovery time. */
112         req->rq_allow_replay = 1;
113
114         ptlrpc_at_set_req_timeout(req);
115
116         rc = ptlrpc_queue_wait(req);
117
118         if (rc)
119                 GOTO(out_req, rc);
120
121         out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
122         *output = *out;
123
124         if (!lu_seq_range_is_sane(output)) {
125                 CERROR("%s: Invalid range received from server: "
126                        DRANGE"\n", seq->lcs_name, PRANGE(output));
127                 GOTO(out_req, rc = -EINVAL);
128         }
129
130         if (lu_seq_range_is_exhausted(output)) {
131                 CERROR("%s: Range received from server is exhausted: "
132                        DRANGE"]\n", seq->lcs_name, PRANGE(output));
133                 GOTO(out_req, rc = -EINVAL);
134         }
135
136         CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
137                      seq->lcs_name, opcname, PRANGE(output));
138
139         EXIT;
140 out_req:
141         ptlrpc_req_finished(req);
142         return rc;
143 }
144
145 /* Request sequence-controller node to allocate new super-sequence. */
146 int seq_client_alloc_super(struct lu_client_seq *seq,
147                            const struct lu_env *env)
148 {
149         int rc;
150         ENTRY;
151
152         mutex_lock(&seq->lcs_mutex);
153
154         if (seq->lcs_srv) {
155 #ifdef HAVE_SEQ_SERVER
156                 LASSERT(env != NULL);
157                 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
158                                             env);
159 #else
160                 rc = 0;
161 #endif
162         } else {
163                 /* Check whether the connection to seq controller has been
164                  * setup (lcs_exp != NULL) */
165                 if (seq->lcs_exp == NULL) {
166                         mutex_unlock(&seq->lcs_mutex);
167                         RETURN(-EINPROGRESS);
168                 }
169
170                 rc = seq_client_rpc(seq, &seq->lcs_space,
171                                     SEQ_ALLOC_SUPER, "super");
172         }
173         mutex_unlock(&seq->lcs_mutex);
174         RETURN(rc);
175 }
176
177 /* Request sequence-controller node to allocate new meta-sequence. */
178 static int seq_client_alloc_meta(const struct lu_env *env,
179                                  struct lu_client_seq *seq)
180 {
181         int rc;
182         ENTRY;
183
184         if (seq->lcs_srv) {
185 #ifdef HAVE_SEQ_SERVER
186                 LASSERT(env);
187                 rc = seq_server_alloc_meta(seq->lcs_srv, &seq->lcs_space, env);
188 #else
189                 rc = 0;
190 #endif
191         } else {
192                 do {
193                         /* If meta server return -EINPROGRESS or EAGAIN,
194                          * it means meta server might not be ready to
195                          * allocate super sequence from sequence controller
196                          * (MDT0)yet */
197                         rc = seq_client_rpc(seq, &seq->lcs_space,
198                                             SEQ_ALLOC_META, "meta");
199                         if (rc == -EINPROGRESS || rc == -EAGAIN) {
200                                 wait_queue_head_t waitq;
201                                 struct l_wait_info  lwi;
202
203                                 /* MDT0 is not ready, let's wait for 2
204                                  * seconds and retry. */
205                                 init_waitqueue_head(&waitq);
206                                 lwi = LWI_TIMEOUT(cfs_time_seconds(2), NULL,
207                                                   NULL);
208                                 l_wait_event(waitq, 0, &lwi);
209                         }
210                 } while (rc == -EINPROGRESS || rc == -EAGAIN);
211         }
212
213         RETURN(rc);
214 }
215
216 /* Allocate new sequence for client. */
217 static int seq_client_alloc_seq(const struct lu_env *env,
218                                 struct lu_client_seq *seq, u64 *seqnr)
219 {
220         int rc;
221         ENTRY;
222
223         LASSERT(lu_seq_range_is_sane(&seq->lcs_space));
224
225         if (lu_seq_range_is_exhausted(&seq->lcs_space)) {
226                 rc = seq_client_alloc_meta(env, seq);
227                 if (rc) {
228                         if (rc != -EINPROGRESS)
229                                 CERROR("%s: Can't allocate new meta-sequence,"
230                                        "rc = %d\n", seq->lcs_name, rc);
231                         RETURN(rc);
232                 } else {
233                         CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
234                                seq->lcs_name, PRANGE(&seq->lcs_space));
235                 }
236         } else {
237                 rc = 0;
238         }
239
240         LASSERT(!lu_seq_range_is_exhausted(&seq->lcs_space));
241         *seqnr = seq->lcs_space.lsr_start;
242         seq->lcs_space.lsr_start += 1;
243
244         CDEBUG(D_INFO, "%s: Allocated sequence [%#llx]\n", seq->lcs_name,
245                *seqnr);
246
247         RETURN(rc);
248 }
249
250 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
251                               wait_queue_entry_t *link)
252 {
253         if (seq->lcs_update) {
254                 add_wait_queue(&seq->lcs_waitq, link);
255                 set_current_state(TASK_UNINTERRUPTIBLE);
256                 mutex_unlock(&seq->lcs_mutex);
257
258                 schedule();
259
260                 mutex_lock(&seq->lcs_mutex);
261                 remove_wait_queue(&seq->lcs_waitq, link);
262                 set_current_state(TASK_RUNNING);
263                 return -EAGAIN;
264         }
265
266         ++seq->lcs_update;
267         mutex_unlock(&seq->lcs_mutex);
268
269         return 0;
270 }
271
272 static void seq_fid_alloc_fini(struct lu_client_seq *seq, __u64 seqnr,
273                                bool whole)
274 {
275         LASSERT(seq->lcs_update == 1);
276
277         mutex_lock(&seq->lcs_mutex);
278         if (seqnr != 0) {
279                 CDEBUG(D_INFO, "%s: New sequence [0x%16.16llx]\n",
280                        seq->lcs_name, seqnr);
281
282                 seq->lcs_fid.f_seq = seqnr;
283                 if (whole) {
284                         /* Since the caller require the whole seq,
285                          * so marked this seq to be used */
286                         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
287                                 seq->lcs_fid.f_oid =
288                                         LUSTRE_METADATA_SEQ_MAX_WIDTH;
289                         else
290                                 seq->lcs_fid.f_oid = LUSTRE_DATA_SEQ_MAX_WIDTH;
291                 } else {
292                         seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
293                 }
294                 seq->lcs_fid.f_ver = 0;
295         }
296
297         --seq->lcs_update;
298         wake_up_all(&seq->lcs_waitq);
299 }
300
301 /**
302  * Allocate the whole non-used seq to the caller.
303  *
304  * \param[in] env       pointer to the thread context
305  * \param[in,out] seq   pointer to the client sequence manager
306  * \param[out] seqnr    to hold the new allocated sequence
307  *
308  * \retval              0 for new sequence allocated.
309  * \retval              Negative error number on failure.
310  */
311 int seq_client_get_seq(const struct lu_env *env,
312                        struct lu_client_seq *seq, u64 *seqnr)
313 {
314         wait_queue_entry_t link;
315         int rc;
316
317         LASSERT(seqnr != NULL);
318
319         mutex_lock(&seq->lcs_mutex);
320         init_waitqueue_entry(&link, current);
321
322         /* To guarantee that we can get a whole non-used sequence. */
323         while (seq_fid_alloc_prep(seq, &link) != 0);
324
325         rc = seq_client_alloc_seq(env, seq, seqnr);
326         seq_fid_alloc_fini(seq, rc ? 0 : *seqnr, true);
327         if (rc)
328                 CERROR("%s: Can't allocate new sequence: rc = %d\n",
329                        seq->lcs_name, rc);
330         mutex_unlock(&seq->lcs_mutex);
331
332         return rc;
333 }
334 EXPORT_SYMBOL(seq_client_get_seq);
335
336 /**
337  * Allocate new fid on passed client @seq and save it to @fid.
338  *
339  * \param[in] env       pointer to the thread context
340  * \param[in,out] seq   pointer to the client sequence manager
341  * \param[out] fid      to hold the new allocated fid
342  *
343  * \retval              1 for notify the caller that sequence switch
344  *                      is performed to allow it to setup FLD for it.
345  * \retval              0 for new FID allocated in current sequence.
346  * \retval              Negative error number on failure.
347  */
348 int seq_client_alloc_fid(const struct lu_env *env,
349                          struct lu_client_seq *seq, struct lu_fid *fid)
350 {
351         wait_queue_entry_t link;
352         int rc;
353         ENTRY;
354
355         LASSERT(seq != NULL);
356         LASSERT(fid != NULL);
357
358         init_waitqueue_entry(&link, current);
359         mutex_lock(&seq->lcs_mutex);
360
361         if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
362                 seq->lcs_fid.f_oid = seq->lcs_width;
363
364         while (1) {
365                 u64 seqnr;
366
367                 if (unlikely(!fid_is_zero(&seq->lcs_fid) &&
368                              fid_oid(&seq->lcs_fid) < seq->lcs_width)) {
369                         /* Just bump last allocated fid and return to caller. */
370                         seq->lcs_fid.f_oid++;
371                         rc = 0;
372                         break;
373                 }
374
375                 /* Release seq::lcs_mutex via seq_fid_alloc_prep() to avoid
376                  * deadlock during seq_client_alloc_seq(). */
377                 rc = seq_fid_alloc_prep(seq, &link);
378                 if (rc)
379                         continue;
380
381                 rc = seq_client_alloc_seq(env, seq, &seqnr);
382                 /* Re-take seq::lcs_mutex via seq_fid_alloc_fini(). */
383                 seq_fid_alloc_fini(seq, rc ? 0 : seqnr, false);
384                 if (rc) {
385                         if (rc != -EINPROGRESS)
386                                 CERROR("%s: Can't allocate new sequence: "
387                                        "rc = %d\n", seq->lcs_name, rc);
388                         mutex_unlock(&seq->lcs_mutex);
389
390                         RETURN(rc);
391                 }
392
393                 rc = 1;
394                 break;
395         }
396
397         *fid = seq->lcs_fid;
398         mutex_unlock(&seq->lcs_mutex);
399
400         CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name,  PFID(fid));
401
402         RETURN(rc);
403 }
404 EXPORT_SYMBOL(seq_client_alloc_fid);
405
406 /*
407  * Finish the current sequence due to disconnect.
408  * See mdc_import_event()
409  */
410 void seq_client_flush(struct lu_client_seq *seq)
411 {
412         wait_queue_entry_t link;
413
414         LASSERT(seq != NULL);
415         init_waitqueue_entry(&link, current);
416         mutex_lock(&seq->lcs_mutex);
417
418         while (seq->lcs_update) {
419                 add_wait_queue(&seq->lcs_waitq, &link);
420                 set_current_state(TASK_UNINTERRUPTIBLE);
421                 mutex_unlock(&seq->lcs_mutex);
422
423                 schedule();
424
425                 mutex_lock(&seq->lcs_mutex);
426                 remove_wait_queue(&seq->lcs_waitq, &link);
427                 set_current_state(TASK_RUNNING);
428         }
429
430         fid_zero(&seq->lcs_fid);
431         /**
432          * this id shld not be used for seq range allocation.
433          * set to -1 for dgb check.
434          */
435
436         seq->lcs_space.lsr_index = -1;
437
438         lu_seq_range_init(&seq->lcs_space);
439         mutex_unlock(&seq->lcs_mutex);
440 }
441 EXPORT_SYMBOL(seq_client_flush);
442
443 static void seq_client_debugfs_fini(struct lu_client_seq *seq)
444 {
445         if (!IS_ERR_OR_NULL(seq->lcs_debugfs_entry))
446                 ldebugfs_remove(&seq->lcs_debugfs_entry);
447 }
448
449 static int seq_client_debugfs_init(struct lu_client_seq *seq)
450 {
451         int rc;
452
453         seq->lcs_debugfs_entry = ldebugfs_register(seq->lcs_name,
454                                                    seq_debugfs_dir,
455                                                    NULL, NULL);
456         if (IS_ERR_OR_NULL(seq->lcs_debugfs_entry)) {
457                 CERROR("%s: LdebugFS failed in seq-init\n", seq->lcs_name);
458                 rc = seq->lcs_debugfs_entry ? PTR_ERR(seq->lcs_debugfs_entry)
459                                             : -ENOMEM;
460                 seq->lcs_debugfs_entry = NULL;
461                 RETURN(rc);
462         }
463
464         rc = ldebugfs_add_vars(seq->lcs_debugfs_entry,
465                                seq_client_debugfs_list, seq);
466         if (rc) {
467                 CERROR("%s: Can't init sequence manager debugfs, rc %d\n",
468                        seq->lcs_name, rc);
469                 GOTO(out_cleanup, rc);
470         }
471
472         RETURN(0);
473
474 out_cleanup:
475         seq_client_debugfs_fini(seq);
476         return rc;
477 }
478
479 void seq_client_fini(struct lu_client_seq *seq)
480 {
481         ENTRY;
482
483         seq_client_debugfs_fini(seq);
484
485         if (seq->lcs_exp != NULL) {
486                 class_export_put(seq->lcs_exp);
487                 seq->lcs_exp = NULL;
488         }
489
490         seq->lcs_srv = NULL;
491         EXIT;
492 }
493 EXPORT_SYMBOL(seq_client_fini);
494
495 int seq_client_init(struct lu_client_seq *seq,
496                     struct obd_export *exp,
497                     enum lu_cli_type type,
498                     const char *prefix,
499                     struct lu_server_seq *srv)
500 {
501         int rc;
502         ENTRY;
503
504         LASSERT(seq != NULL);
505         LASSERT(prefix != NULL);
506
507         seq->lcs_srv = srv;
508         seq->lcs_type = type;
509
510         mutex_init(&seq->lcs_mutex);
511         if (type == LUSTRE_SEQ_METADATA)
512                 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
513         else
514                 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
515
516         init_waitqueue_head(&seq->lcs_waitq);
517         /* Make sure that things are clear before work is started. */
518         seq_client_flush(seq);
519
520         if (exp != NULL)
521                 seq->lcs_exp = class_export_get(exp);
522
523         snprintf(seq->lcs_name, sizeof(seq->lcs_name),
524                  "cli-%s", prefix);
525
526         rc = seq_client_debugfs_init(seq);
527         if (rc)
528                 seq_client_fini(seq);
529         RETURN(rc);
530 }
531 EXPORT_SYMBOL(seq_client_init);
532
533 int client_fid_init(struct obd_device *obd,
534                     struct obd_export *exp, enum lu_cli_type type)
535 {
536         struct client_obd *cli = &obd->u.cli;
537         char *prefix;
538         int rc;
539         ENTRY;
540
541         down_write(&cli->cl_seq_rwsem);
542         OBD_ALLOC_PTR(cli->cl_seq);
543         if (!cli->cl_seq)
544                 GOTO(out, rc = -ENOMEM);
545
546         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
547         if (!prefix)
548                 GOTO(out, rc = -ENOMEM);
549
550         snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
551
552         /* Init client side sequence-manager */
553         rc = seq_client_init(cli->cl_seq, exp, type, prefix, NULL);
554         OBD_FREE(prefix, MAX_OBD_NAME + 5);
555
556         GOTO(out, rc);
557
558 out:
559         if (rc && cli->cl_seq) {
560                 OBD_FREE_PTR(cli->cl_seq);
561                 cli->cl_seq = NULL;
562         }
563         up_write(&cli->cl_seq_rwsem);
564
565         return rc;
566 }
567 EXPORT_SYMBOL(client_fid_init);
568
569 int client_fid_fini(struct obd_device *obd)
570 {
571         struct client_obd *cli = &obd->u.cli;
572         ENTRY;
573
574         down_write(&cli->cl_seq_rwsem);
575         if (cli->cl_seq) {
576                 seq_client_fini(cli->cl_seq);
577                 OBD_FREE_PTR(cli->cl_seq);
578                 cli->cl_seq = NULL;
579         }
580         up_write(&cli->cl_seq_rwsem);
581
582         RETURN(0);
583 }
584 EXPORT_SYMBOL(client_fid_fini);
585
586 static int __init fid_init(void)
587 {
588 #ifdef HAVE_SERVER_SUPPORT
589         int rc = fid_server_mod_init();
590
591         if (rc)
592                 return rc;
593 #endif
594         seq_debugfs_dir = ldebugfs_register(LUSTRE_SEQ_NAME,
595                                             debugfs_lustre_root,
596                                             NULL, NULL);
597         return PTR_ERR_OR_ZERO(seq_debugfs_dir);
598 }
599
600 static void __exit fid_exit(void)
601 {
602 # ifdef HAVE_SERVER_SUPPORT
603         fid_server_mod_exit();
604 # endif
605         if (!IS_ERR_OR_NULL(seq_debugfs_dir))
606                 ldebugfs_remove(&seq_debugfs_dir);
607 }
608
609 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
610 MODULE_DESCRIPTION("Lustre File IDentifier");
611 MODULE_VERSION(LUSTRE_VERSION_STRING);
612 MODULE_LICENSE("GPL");
613
614 module_init(fid_init);
615 module_exit(fid_exit);