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