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