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