Whamcloud - gitweb
528bed7c0055b89bdf199bba5a46448d025f3156
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/fid/fid_request.c
37  *
38  * Lustre Sequence Manager
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FID
44
45 #include <linux/module.h>
46 #include <libcfs/libcfs.h>
47 #include <obd.h>
48 #include <obd_class.h>
49 #include <obd_support.h>
50 #include <lustre_fid.h>
51 /* mdc RPC locks */
52 #include <lustre_mdc.h>
53 #include "fid_internal.h"
54
55 static int seq_client_rpc(struct lu_client_seq *seq,
56                           struct lu_seq_range *output, __u32 opc,
57                           const char *opcname)
58 {
59         struct obd_export     *exp = seq->lcs_exp;
60         struct ptlrpc_request *req;
61         struct lu_seq_range   *out, *in;
62         __u32                 *op;
63         unsigned int           debug_mask;
64         int                    rc;
65         ENTRY;
66
67         LASSERT(exp != NULL && !IS_ERR(exp));
68         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
69                                         LUSTRE_MDS_VERSION, SEQ_QUERY);
70         if (req == NULL)
71                 RETURN(-ENOMEM);
72
73         /* Init operation code */
74         op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
75         *op = opc;
76
77         /* Zero out input range, this is not recovery yet. */
78         in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
79         range_init(in);
80
81         ptlrpc_request_set_replen(req);
82
83         in->lsr_index = seq->lcs_space.lsr_index;
84         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
85                 fld_range_set_mdt(in);
86         else
87                 fld_range_set_ost(in);
88
89         if (opc == SEQ_ALLOC_SUPER) {
90                 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
91                 req->rq_reply_portal = MDC_REPLY_PORTAL;
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                 if (seq->lcs_type == LUSTRE_SEQ_DATA)
99                         req->rq_no_delay = req->rq_no_resend = 1;
100                 debug_mask = D_CONSOLE;
101         } else {
102                 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
103                         req->rq_reply_portal = MDC_REPLY_PORTAL;
104                         req->rq_request_portal = SEQ_METADATA_PORTAL;
105                 } else {
106                         req->rq_reply_portal = OSC_REPLY_PORTAL;
107                         req->rq_request_portal = SEQ_DATA_PORTAL;
108                 }
109
110                 debug_mask = D_INFO;
111         }
112
113         /* Allow seq client RPC during recovery time. */
114         req->rq_allow_replay = 1;
115
116         ptlrpc_at_set_req_timeout(req);
117
118         if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
119                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
120
121         rc = ptlrpc_queue_wait(req);
122
123         if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
124                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
125         if (rc)
126                 GOTO(out_req, rc);
127
128         out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
129         *output = *out;
130
131         if (!range_is_sane(output)) {
132                 CERROR("%s: Invalid range received from server: "
133                        DRANGE"\n", seq->lcs_name, PRANGE(output));
134                 GOTO(out_req, rc = -EINVAL);
135         }
136
137         if (range_is_exhausted(output)) {
138                 CERROR("%s: Range received from server is exhausted: "
139                        DRANGE"]\n", seq->lcs_name, PRANGE(output));
140                 GOTO(out_req, rc = -EINVAL);
141         }
142
143         CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
144                      seq->lcs_name, opcname, PRANGE(output));
145
146         EXIT;
147 out_req:
148         ptlrpc_req_finished(req);
149         return rc;
150 }
151
152 /* Request sequence-controller node to allocate new super-sequence. */
153 int seq_client_alloc_super(struct lu_client_seq *seq,
154                            const struct lu_env *env)
155 {
156         int rc;
157         ENTRY;
158
159         mutex_lock(&seq->lcs_mutex);
160
161         if (seq->lcs_srv) {
162 #ifdef HAVE_SEQ_SERVER
163                 LASSERT(env != NULL);
164                 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
165                                             env);
166 #else
167                 rc = 0;
168 #endif
169         } else {
170                 /* Check whether the connection to seq controller has been
171                  * setup (lcs_exp != NULL) */
172                 if (seq->lcs_exp == NULL) {
173                         mutex_unlock(&seq->lcs_mutex);
174                         RETURN(-EINPROGRESS);
175                 }
176
177                 rc = seq_client_rpc(seq, &seq->lcs_space,
178                                     SEQ_ALLOC_SUPER, "super");
179         }
180         mutex_unlock(&seq->lcs_mutex);
181         RETURN(rc);
182 }
183
184 /* Request sequence-controller node to allocate new meta-sequence. */
185 static int seq_client_alloc_meta(const struct lu_env *env,
186                                  struct lu_client_seq *seq)
187 {
188         int rc;
189         ENTRY;
190
191         if (seq->lcs_srv) {
192 #ifdef HAVE_SEQ_SERVER
193                 LASSERT(env != NULL);
194                 rc = seq_server_alloc_meta(seq->lcs_srv, &seq->lcs_space, env);
195 #else
196                 rc = 0;
197 #endif
198         } else {
199                 do {
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                         rc = seq_client_rpc(seq, &seq->lcs_space,
205                                             SEQ_ALLOC_META, "meta");
206                 } while (rc == -EINPROGRESS || rc == -EAGAIN);
207         }
208
209         RETURN(rc);
210 }
211
212 /* Allocate new sequence for client. */
213 static int seq_client_alloc_seq(const struct lu_env *env,
214                                 struct lu_client_seq *seq, u64 *seqnr)
215 {
216         int rc;
217         ENTRY;
218
219         LASSERT(range_is_sane(&seq->lcs_space));
220
221         if (range_is_exhausted(&seq->lcs_space)) {
222                 rc = seq_client_alloc_meta(env, seq);
223                 if (rc) {
224                         CERROR("%s: Can't allocate new meta-sequence,"
225                                "rc %d\n", 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(!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 ["LPX64"]\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_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         ++seq->lcs_update;
261         mutex_unlock(&seq->lcs_mutex);
262         return 0;
263 }
264
265 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
266 {
267         LASSERT(seq->lcs_update == 1);
268         mutex_lock(&seq->lcs_mutex);
269         --seq->lcs_update;
270         wake_up(&seq->lcs_waitq);
271 }
272
273 /**
274  * Allocate the whole seq to the caller.
275  **/
276 int seq_client_get_seq(const struct lu_env *env,
277                        struct lu_client_seq *seq, u64 *seqnr)
278 {
279         wait_queue_t link;
280         int rc;
281
282         LASSERT(seqnr != NULL);
283         mutex_lock(&seq->lcs_mutex);
284         init_waitqueue_entry(&link, current);
285
286         while (1) {
287                 rc = seq_fid_alloc_prep(seq, &link);
288                 if (rc == 0)
289                         break;
290         }
291
292         rc = seq_client_alloc_seq(env, seq, seqnr);
293         if (rc) {
294                 CERROR("%s: Can't allocate new sequence, "
295                        "rc %d\n", seq->lcs_name, rc);
296                 seq_fid_alloc_fini(seq);
297                 mutex_unlock(&seq->lcs_mutex);
298                 return rc;
299         }
300
301         CDEBUG(D_INFO, "%s: allocate sequence "
302                "[0x%16.16"LPF64"x]\n", seq->lcs_name, *seqnr);
303
304         /* Since the caller require the whole seq,
305          * so marked this seq to be used */
306         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
307                 seq->lcs_fid.f_oid = LUSTRE_METADATA_SEQ_MAX_WIDTH;
308         else
309                 seq->lcs_fid.f_oid = LUSTRE_DATA_SEQ_MAX_WIDTH;
310
311         seq->lcs_fid.f_seq = *seqnr;
312         seq->lcs_fid.f_ver = 0;
313         /*
314          * Inform caller that sequence switch is performed to allow it
315          * to setup FLD for it.
316          */
317         seq_fid_alloc_fini(seq);
318         mutex_unlock(&seq->lcs_mutex);
319
320         return rc;
321 }
322 EXPORT_SYMBOL(seq_client_get_seq);
323
324 /* Allocate new fid on passed client @seq and save it to @fid. */
325 int seq_client_alloc_fid(const struct lu_env *env,
326                          struct lu_client_seq *seq, struct lu_fid *fid)
327 {
328         wait_queue_t link;
329         int rc;
330         ENTRY;
331
332         LASSERT(seq != NULL);
333         LASSERT(fid != NULL);
334
335         init_waitqueue_entry(&link, current);
336         mutex_lock(&seq->lcs_mutex);
337
338         if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
339                 seq->lcs_fid.f_oid = seq->lcs_width;
340
341         while (1) {
342                 u64 seqnr;
343
344                 if (!fid_is_zero(&seq->lcs_fid) &&
345                     fid_oid(&seq->lcs_fid) < seq->lcs_width) {
346                         /* Just bump last allocated fid and return to caller. */
347                         seq->lcs_fid.f_oid += 1;
348                         rc = 0;
349                         break;
350                 }
351
352                 rc = seq_fid_alloc_prep(seq, &link);
353                 if (rc)
354                         continue;
355
356                 rc = seq_client_alloc_seq(env, seq, &seqnr);
357                 if (rc) {
358                         CERROR("%s: Can't allocate new sequence, "
359                                "rc %d\n", seq->lcs_name, rc);
360                         seq_fid_alloc_fini(seq);
361                         mutex_unlock(&seq->lcs_mutex);
362                         RETURN(rc);
363                 }
364
365                 CDEBUG(D_INFO, "%s: Switch to sequence "
366                        "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
367
368                 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
369                 seq->lcs_fid.f_seq = seqnr;
370                 seq->lcs_fid.f_ver = 0;
371
372                 /*
373                  * Inform caller that sequence switch is performed to allow it
374                  * to setup FLD for it.
375                  */
376                 rc = 1;
377
378                 seq_fid_alloc_fini(seq);
379                 break;
380         }
381
382         *fid = seq->lcs_fid;
383         mutex_unlock(&seq->lcs_mutex);
384
385         CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name,  PFID(fid));
386         RETURN(rc);
387 }
388 EXPORT_SYMBOL(seq_client_alloc_fid);
389
390 /*
391  * Finish the current sequence due to disconnect.
392  * See mdc_import_event()
393  */
394 void seq_client_flush(struct lu_client_seq *seq)
395 {
396         wait_queue_t link;
397
398         LASSERT(seq != NULL);
399         init_waitqueue_entry(&link, current);
400         mutex_lock(&seq->lcs_mutex);
401
402         while (seq->lcs_update) {
403                 add_wait_queue(&seq->lcs_waitq, &link);
404                 set_current_state(TASK_UNINTERRUPTIBLE);
405                 mutex_unlock(&seq->lcs_mutex);
406
407                 schedule();
408
409                 mutex_lock(&seq->lcs_mutex);
410                 remove_wait_queue(&seq->lcs_waitq, &link);
411                 set_current_state(TASK_RUNNING);
412         }
413
414         fid_zero(&seq->lcs_fid);
415         /**
416          * this id shld not be used for seq range allocation.
417          * set to -1 for dgb check.
418          */
419
420         seq->lcs_space.lsr_index = -1;
421
422         range_init(&seq->lcs_space);
423         mutex_unlock(&seq->lcs_mutex);
424 }
425 EXPORT_SYMBOL(seq_client_flush);
426
427 static void seq_client_proc_fini(struct lu_client_seq *seq)
428 {
429 #ifdef CONFIG_PROC_FS
430         ENTRY;
431         if (seq->lcs_proc_dir) {
432                 if (!IS_ERR(seq->lcs_proc_dir))
433                         lprocfs_remove(&seq->lcs_proc_dir);
434                 seq->lcs_proc_dir = NULL;
435         }
436         EXIT;
437 #endif /* CONFIG_PROC_FS */
438 }
439
440 static int seq_client_proc_init(struct lu_client_seq *seq)
441 {
442 #ifdef CONFIG_PROC_FS
443         int rc;
444         ENTRY;
445
446         seq->lcs_proc_dir = lprocfs_register(seq->lcs_name, seq_type_proc_dir,
447                                              NULL, NULL);
448         if (IS_ERR(seq->lcs_proc_dir)) {
449                 CERROR("%s: LProcFS failed in seq-init\n",
450                        seq->lcs_name);
451                 rc = PTR_ERR(seq->lcs_proc_dir);
452                 RETURN(rc);
453         }
454
455         rc = lprocfs_add_vars(seq->lcs_proc_dir, seq_client_proc_list, seq);
456         if (rc) {
457                 CERROR("%s: Can't init sequence manager "
458                        "proc, rc %d\n", seq->lcs_name, rc);
459                 GOTO(out_cleanup, rc);
460         }
461
462         RETURN(0);
463
464 out_cleanup:
465         seq_client_proc_fini(seq);
466         return rc;
467
468 #else /* !CONFIG_PROC_FS */
469         return 0;
470 #endif /* CONFIG_PROC_FS */
471 }
472
473 int seq_client_init(struct lu_client_seq *seq,
474                     struct obd_export *exp,
475                     enum lu_cli_type type,
476                     const char *prefix,
477                     struct lu_server_seq *srv)
478 {
479         int rc;
480         ENTRY;
481
482         LASSERT(seq != NULL);
483         LASSERT(prefix != NULL);
484
485         seq->lcs_srv = srv;
486         seq->lcs_type = type;
487
488         mutex_init(&seq->lcs_mutex);
489         if (type == LUSTRE_SEQ_METADATA)
490                 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
491         else
492                 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
493
494         init_waitqueue_head(&seq->lcs_waitq);
495         /* Make sure that things are clear before work is started. */
496         seq_client_flush(seq);
497
498         if (exp != NULL)
499                 seq->lcs_exp = class_export_get(exp);
500
501         snprintf(seq->lcs_name, sizeof(seq->lcs_name),
502                  "cli-%s", prefix);
503
504         rc = seq_client_proc_init(seq);
505         if (rc)
506                 seq_client_fini(seq);
507         RETURN(rc);
508 }
509 EXPORT_SYMBOL(seq_client_init);
510
511 void seq_client_fini(struct lu_client_seq *seq)
512 {
513         ENTRY;
514
515         seq_client_proc_fini(seq);
516
517         if (seq->lcs_exp != NULL) {
518                 class_export_put(seq->lcs_exp);
519                 seq->lcs_exp = NULL;
520         }
521
522         seq->lcs_srv = NULL;
523         EXIT;
524 }
525 EXPORT_SYMBOL(seq_client_fini);
526
527 int client_fid_init(struct obd_device *obd,
528                     struct obd_export *exp, enum lu_cli_type type)
529 {
530         struct client_obd *cli = &obd->u.cli;
531         char *prefix;
532         int rc;
533         ENTRY;
534
535         OBD_ALLOC_PTR(cli->cl_seq);
536         if (cli->cl_seq == NULL)
537                 RETURN(-ENOMEM);
538
539         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
540         if (prefix == NULL)
541                 GOTO(out_free_seq, rc = -ENOMEM);
542
543         snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
544
545         /* Init client side sequence-manager */
546         rc = seq_client_init(cli->cl_seq, exp, type, prefix, NULL);
547         OBD_FREE(prefix, MAX_OBD_NAME + 5);
548         if (rc)
549                 GOTO(out_free_seq, rc);
550
551         RETURN(rc);
552 out_free_seq:
553         OBD_FREE_PTR(cli->cl_seq);
554         cli->cl_seq = NULL;
555         return rc;
556 }
557 EXPORT_SYMBOL(client_fid_init);
558
559 int client_fid_fini(struct obd_device *obd)
560 {
561         struct client_obd *cli = &obd->u.cli;
562         ENTRY;
563
564         if (cli->cl_seq != NULL) {
565                 seq_client_fini(cli->cl_seq);
566                 OBD_FREE_PTR(cli->cl_seq);
567                 cli->cl_seq = NULL;
568         }
569
570         RETURN(0);
571 }
572 EXPORT_SYMBOL(client_fid_fini);
573
574 struct proc_dir_entry *seq_type_proc_dir;
575
576 static int __init fid_mod_init(void)
577 {
578         seq_type_proc_dir = lprocfs_register(LUSTRE_SEQ_NAME,
579                                              proc_lustre_root,
580                                              NULL, NULL);
581         if (IS_ERR(seq_type_proc_dir))
582                 return PTR_ERR(seq_type_proc_dir);
583
584 # ifdef HAVE_SERVER_SUPPORT
585         fid_server_mod_init();
586 # endif
587
588         return 0;
589 }
590
591 static void __exit fid_mod_exit(void)
592 {
593 # ifdef HAVE_SERVER_SUPPORT
594         fid_server_mod_exit();
595 # endif
596
597         if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
598                 lprocfs_remove(&seq_type_proc_dir);
599                 seq_type_proc_dir = NULL;
600         }
601 }
602
603 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
604 MODULE_DESCRIPTION("Lustre FID Module");
605 MODULE_VERSION(LUSTRE_VERSION_STRING);
606 MODULE_LICENSE("GPL");
607
608 module_init(fid_mod_init);
609 module_exit(fid_mod_exit);