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