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