Whamcloud - gitweb
LU-4022 hsm: Set agent_actions start index from -1
[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, 2013, 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 #ifdef __KERNEL__
46 # include <libcfs/libcfs.h>
47 # include <linux/module.h>
48 #else /* __KERNEL__ */
49 # include <liblustre.h>
50 #endif
51
52 #include <obd.h>
53 #include <obd_class.h>
54 #include <obd_support.h>
55 #include <lustre_fid.h>
56 /* mdc RPC locks */
57 #include <lustre_mdc.h>
58 #include "fid_internal.h"
59
60 static int seq_client_rpc(struct lu_client_seq *seq,
61                           struct lu_seq_range *output, __u32 opc,
62                           const char *opcname)
63 {
64         struct obd_export     *exp = seq->lcs_exp;
65         struct ptlrpc_request *req;
66         struct lu_seq_range   *out, *in;
67         __u32                 *op;
68         unsigned int           debug_mask;
69         int                    rc;
70         ENTRY;
71
72         LASSERT(exp != NULL && !IS_ERR(exp));
73         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
74                                         LUSTRE_MDS_VERSION, SEQ_QUERY);
75         if (req == NULL)
76                 RETURN(-ENOMEM);
77
78         /* Init operation code */
79         op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
80         *op = opc;
81
82         /* Zero out input range, this is not recovery yet. */
83         in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
84         range_init(in);
85
86         ptlrpc_request_set_replen(req);
87
88         in->lsr_index = seq->lcs_space.lsr_index;
89         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
90                 fld_range_set_mdt(in);
91         else
92                 fld_range_set_ost(in);
93
94         if (opc == SEQ_ALLOC_SUPER) {
95                 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
96                 req->rq_reply_portal = MDC_REPLY_PORTAL;
97                 /* During allocating super sequence for data object,
98                  * the current thread might hold the export of MDT0(MDT0
99                  * precreating objects on this OST), and it will send the
100                  * request to MDT0 here, so we can not keep resending the
101                  * request here, otherwise if MDT0 is failed(umounted),
102                  * it can not release the export of MDT0 */
103                 if (seq->lcs_type == LUSTRE_SEQ_DATA)
104                         req->rq_no_delay = req->rq_no_resend = 1;
105                 debug_mask = D_CONSOLE;
106         } else {
107                 if (seq->lcs_type == LUSTRE_SEQ_METADATA)
108                         req->rq_request_portal = SEQ_METADATA_PORTAL;
109                 else
110                         req->rq_request_portal = SEQ_DATA_PORTAL;
111                 debug_mask = D_INFO;
112         }
113
114         ptlrpc_at_set_req_timeout(req);
115
116         if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
117                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
118
119         rc = ptlrpc_queue_wait(req);
120
121         if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
122                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
123         if (rc)
124                 GOTO(out_req, rc);
125
126         out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
127         *output = *out;
128
129         if (!range_is_sane(output)) {
130                 CERROR("%s: Invalid range received from server: "
131                        DRANGE"\n", seq->lcs_name, PRANGE(output));
132                 GOTO(out_req, rc = -EINVAL);
133         }
134
135         if (range_is_exhausted(output)) {
136                 CERROR("%s: Range received from server is exhausted: "
137                        DRANGE"]\n", seq->lcs_name, PRANGE(output));
138                 GOTO(out_req, rc = -EINVAL);
139         }
140
141         CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
142                      seq->lcs_name, opcname, PRANGE(output));
143
144         EXIT;
145 out_req:
146         ptlrpc_req_finished(req);
147         return rc;
148 }
149
150 /* Request sequence-controller node to allocate new super-sequence. */
151 int seq_client_alloc_super(struct lu_client_seq *seq,
152                            const struct lu_env *env)
153 {
154         int rc;
155         ENTRY;
156
157         mutex_lock(&seq->lcs_mutex);
158
159         if (seq->lcs_srv) {
160 #ifdef HAVE_SEQ_SERVER
161                 LASSERT(env != NULL);
162                 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
163                                             env);
164 #else
165                 rc = 0;
166 #endif
167         } else {
168                 /* Check whether the connection to seq controller has been
169                  * setup (lcs_exp != NULL) */
170                 if (seq->lcs_exp == NULL) {
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 != NULL);
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                         /* If meta server return -EINPROGRESS or EAGAIN,
199                          * it means meta server might not be ready to
200                          * allocate super sequence from sequence controller
201                          * (MDT0)yet */
202                         rc = seq_client_rpc(seq, &seq->lcs_space,
203                                             SEQ_ALLOC_META, "meta");
204                 } while (rc == -EINPROGRESS || rc == -EAGAIN);
205         }
206
207         RETURN(rc);
208 }
209
210 /* Allocate new sequence for client. */
211 static int seq_client_alloc_seq(const struct lu_env *env,
212                                 struct lu_client_seq *seq, seqno_t *seqnr)
213 {
214         int rc;
215         ENTRY;
216
217         LASSERT(range_is_sane(&seq->lcs_space));
218
219         if (range_is_exhausted(&seq->lcs_space)) {
220                 rc = seq_client_alloc_meta(env, seq);
221                 if (rc) {
222                         CERROR("%s: Can't allocate new meta-sequence,"
223                                "rc %d\n", seq->lcs_name, rc);
224                         RETURN(rc);
225                 } else {
226                         CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
227                                seq->lcs_name, PRANGE(&seq->lcs_space));
228                 }
229         } else {
230                 rc = 0;
231         }
232
233         LASSERT(!range_is_exhausted(&seq->lcs_space));
234         *seqnr = seq->lcs_space.lsr_start;
235         seq->lcs_space.lsr_start += 1;
236
237         CDEBUG(D_INFO, "%s: Allocated sequence ["LPX64"]\n", seq->lcs_name,
238                *seqnr);
239
240         RETURN(rc);
241 }
242
243 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
244                               wait_queue_t *link)
245 {
246         if (seq->lcs_update) {
247                 add_wait_queue(&seq->lcs_waitq, link);
248                 set_current_state(TASK_UNINTERRUPTIBLE);
249                 mutex_unlock(&seq->lcs_mutex);
250
251                 waitq_wait(link, TASK_UNINTERRUPTIBLE);
252
253                 mutex_lock(&seq->lcs_mutex);
254                 remove_wait_queue(&seq->lcs_waitq, link);
255                 set_current_state(TASK_RUNNING);
256                 return -EAGAIN;
257         }
258         ++seq->lcs_update;
259         mutex_unlock(&seq->lcs_mutex);
260         return 0;
261 }
262
263 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
264 {
265         LASSERT(seq->lcs_update == 1);
266         mutex_lock(&seq->lcs_mutex);
267         --seq->lcs_update;
268         wake_up(&seq->lcs_waitq);
269 }
270
271 /**
272  * Allocate the whole seq to the caller.
273  **/
274 int seq_client_get_seq(const struct lu_env *env,
275                        struct lu_client_seq *seq, seqno_t *seqnr)
276 {
277         wait_queue_t link;
278         int rc;
279
280         LASSERT(seqnr != NULL);
281         mutex_lock(&seq->lcs_mutex);
282         init_waitqueue_entry_current(&link);
283
284         while (1) {
285                 rc = seq_fid_alloc_prep(seq, &link);
286                 if (rc == 0)
287                         break;
288         }
289
290         rc = seq_client_alloc_seq(env, seq, seqnr);
291         if (rc) {
292                 CERROR("%s: Can't allocate new sequence, "
293                        "rc %d\n", seq->lcs_name, rc);
294                 seq_fid_alloc_fini(seq);
295                 mutex_unlock(&seq->lcs_mutex);
296                 return rc;
297         }
298
299         CDEBUG(D_INFO, "%s: allocate sequence "
300                "[0x%16.16"LPF64"x]\n", seq->lcs_name, *seqnr);
301
302         /* Since the caller require the whole seq,
303          * so marked this seq to be used */
304         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
305                 seq->lcs_fid.f_oid = LUSTRE_METADATA_SEQ_MAX_WIDTH;
306         else
307                 seq->lcs_fid.f_oid = LUSTRE_DATA_SEQ_MAX_WIDTH;
308
309         seq->lcs_fid.f_seq = *seqnr;
310         seq->lcs_fid.f_ver = 0;
311         /*
312          * Inform caller that sequence switch is performed to allow it
313          * to setup FLD for it.
314          */
315         seq_fid_alloc_fini(seq);
316         mutex_unlock(&seq->lcs_mutex);
317
318         return rc;
319 }
320 EXPORT_SYMBOL(seq_client_get_seq);
321
322 /* Allocate new fid on passed client @seq and save it to @fid. */
323 int seq_client_alloc_fid(const struct lu_env *env,
324                          struct lu_client_seq *seq, struct lu_fid *fid)
325 {
326         wait_queue_t link;
327         int rc;
328         ENTRY;
329
330         LASSERT(seq != NULL);
331         LASSERT(fid != NULL);
332
333         init_waitqueue_entry_current(&link);
334         mutex_lock(&seq->lcs_mutex);
335
336         if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
337                 seq->lcs_fid.f_oid = seq->lcs_width;
338
339         while (1) {
340                 seqno_t seqnr;
341
342                 if (!fid_is_zero(&seq->lcs_fid) &&
343                     fid_oid(&seq->lcs_fid) < seq->lcs_width) {
344                         /* Just bump last allocated fid and return to caller. */
345                         seq->lcs_fid.f_oid += 1;
346                         rc = 0;
347                         break;
348                 }
349
350                 rc = seq_fid_alloc_prep(seq, &link);
351                 if (rc)
352                         continue;
353
354                 rc = seq_client_alloc_seq(env, seq, &seqnr);
355                 if (rc) {
356                         CERROR("%s: Can't allocate new sequence, "
357                                "rc %d\n", seq->lcs_name, rc);
358                         seq_fid_alloc_fini(seq);
359                         mutex_unlock(&seq->lcs_mutex);
360                         RETURN(rc);
361                 }
362
363                 CDEBUG(D_INFO, "%s: Switch to sequence "
364                        "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
365
366                 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
367                 seq->lcs_fid.f_seq = seqnr;
368                 seq->lcs_fid.f_ver = 0;
369
370                 /*
371                  * Inform caller that sequence switch is performed to allow it
372                  * to setup FLD for it.
373                  */
374                 rc = 1;
375
376                 seq_fid_alloc_fini(seq);
377                 break;
378         }
379
380         *fid = seq->lcs_fid;
381         mutex_unlock(&seq->lcs_mutex);
382
383         CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name,  PFID(fid));
384         RETURN(rc);
385 }
386 EXPORT_SYMBOL(seq_client_alloc_fid);
387
388 /*
389  * Finish the current sequence due to disconnect.
390  * See mdc_import_event()
391  */
392 void seq_client_flush(struct lu_client_seq *seq)
393 {
394         wait_queue_t link;
395
396         LASSERT(seq != NULL);
397         init_waitqueue_entry_current(&link);
398         mutex_lock(&seq->lcs_mutex);
399
400         while (seq->lcs_update) {
401                 add_wait_queue(&seq->lcs_waitq, &link);
402                 set_current_state(TASK_UNINTERRUPTIBLE);
403                 mutex_unlock(&seq->lcs_mutex);
404
405                 waitq_wait(&link, TASK_UNINTERRUPTIBLE);
406
407                 mutex_lock(&seq->lcs_mutex);
408                 remove_wait_queue(&seq->lcs_waitq, &link);
409                 set_current_state(TASK_RUNNING);
410         }
411
412         fid_zero(&seq->lcs_fid);
413         /**
414          * this id shld not be used for seq range allocation.
415          * set to -1 for dgb check.
416          */
417
418         seq->lcs_space.lsr_index = -1;
419
420         range_init(&seq->lcs_space);
421         mutex_unlock(&seq->lcs_mutex);
422 }
423 EXPORT_SYMBOL(seq_client_flush);
424
425 static void seq_client_proc_fini(struct lu_client_seq *seq)
426 {
427 #ifdef LPROCFS
428         ENTRY;
429         if (seq->lcs_proc_dir) {
430                 if (!IS_ERR(seq->lcs_proc_dir))
431                         lprocfs_remove(&seq->lcs_proc_dir);
432                 seq->lcs_proc_dir = NULL;
433         }
434         EXIT;
435 #endif /* LPROCFS */
436 }
437
438 static int seq_client_proc_init(struct lu_client_seq *seq)
439 {
440 #ifdef LPROCFS
441         int rc;
442         ENTRY;
443
444         seq->lcs_proc_dir = lprocfs_seq_register(seq->lcs_name,
445                                                  seq_type_proc_dir,
446                                                  NULL, NULL);
447         if (IS_ERR(seq->lcs_proc_dir)) {
448                 CERROR("%s: LProcFS failed in seq-init\n",
449                        seq->lcs_name);
450                 rc = PTR_ERR(seq->lcs_proc_dir);
451                 RETURN(rc);
452         }
453
454         rc = lprocfs_seq_add_vars(seq->lcs_proc_dir,
455                                   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 /* LPROCFS */
469         return 0;
470 #endif
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 #ifdef __KERNEL__
575 struct proc_dir_entry *seq_type_proc_dir;
576
577 static int __init fid_mod_init(void)
578 {
579         seq_type_proc_dir = lprocfs_seq_register(LUSTRE_SEQ_NAME,
580                                                  proc_lustre_root,
581                                                  NULL, NULL);
582         if (IS_ERR(seq_type_proc_dir))
583                 return PTR_ERR(seq_type_proc_dir);
584
585 # ifdef HAVE_SERVER_SUPPORT
586         fid_server_mod_init();
587 # endif
588
589         return 0;
590 }
591
592 static void __exit fid_mod_exit(void)
593 {
594 # ifdef HAVE_SERVER_SUPPORT
595         fid_server_mod_exit();
596 # endif
597
598         if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
599                 lprocfs_remove(&seq_type_proc_dir);
600                 seq_type_proc_dir = NULL;
601         }
602 }
603
604 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
605 MODULE_DESCRIPTION("Lustre FID Module");
606 MODULE_LICENSE("GPL");
607
608 cfs_module(fid, "0.1.0", fid_mod_init, fid_mod_exit);
609 #endif /* __KERNEL__ */