Whamcloud - gitweb
LU-3319 lprocfs: client side cleanups
[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_reply_portal = MDC_REPLY_PORTAL;
109                         req->rq_request_portal = SEQ_METADATA_PORTAL;
110                 } else {
111                         req->rq_reply_portal = OSC_REPLY_PORTAL;
112                         req->rq_request_portal = SEQ_DATA_PORTAL;
113                 }
114
115                 debug_mask = D_INFO;
116         }
117
118         ptlrpc_at_set_req_timeout(req);
119
120         if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
121                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
122
123         rc = ptlrpc_queue_wait(req);
124
125         if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
126                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
127         if (rc)
128                 GOTO(out_req, rc);
129
130         out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
131         *output = *out;
132
133         if (!range_is_sane(output)) {
134                 CERROR("%s: Invalid range received from server: "
135                        DRANGE"\n", seq->lcs_name, PRANGE(output));
136                 GOTO(out_req, rc = -EINVAL);
137         }
138
139         if (range_is_exhausted(output)) {
140                 CERROR("%s: Range received from server is exhausted: "
141                        DRANGE"]\n", seq->lcs_name, PRANGE(output));
142                 GOTO(out_req, rc = -EINVAL);
143         }
144
145         CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
146                      seq->lcs_name, opcname, PRANGE(output));
147
148         EXIT;
149 out_req:
150         ptlrpc_req_finished(req);
151         return rc;
152 }
153
154 /* Request sequence-controller node to allocate new super-sequence. */
155 int seq_client_alloc_super(struct lu_client_seq *seq,
156                            const struct lu_env *env)
157 {
158         int rc;
159         ENTRY;
160
161         mutex_lock(&seq->lcs_mutex);
162
163         if (seq->lcs_srv) {
164 #ifdef HAVE_SEQ_SERVER
165                 LASSERT(env != NULL);
166                 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
167                                             env);
168 #else
169                 rc = 0;
170 #endif
171         } else {
172                 /* Check whether the connection to seq controller has been
173                  * setup (lcs_exp != NULL) */
174                 if (seq->lcs_exp == NULL) {
175                         mutex_unlock(&seq->lcs_mutex);
176                         RETURN(-EINPROGRESS);
177                 }
178
179                 rc = seq_client_rpc(seq, &seq->lcs_space,
180                                     SEQ_ALLOC_SUPER, "super");
181         }
182         mutex_unlock(&seq->lcs_mutex);
183         RETURN(rc);
184 }
185
186 /* Request sequence-controller node to allocate new meta-sequence. */
187 static int seq_client_alloc_meta(const struct lu_env *env,
188                                  struct lu_client_seq *seq)
189 {
190         int rc;
191         ENTRY;
192
193         if (seq->lcs_srv) {
194 #ifdef HAVE_SEQ_SERVER
195                 LASSERT(env != NULL);
196                 rc = seq_server_alloc_meta(seq->lcs_srv, &seq->lcs_space, env);
197 #else
198                 rc = 0;
199 #endif
200         } else {
201                 do {
202                         /* If meta server return -EINPROGRESS or EAGAIN,
203                          * it means meta server might not be ready to
204                          * allocate super sequence from sequence controller
205                          * (MDT0)yet */
206                         rc = seq_client_rpc(seq, &seq->lcs_space,
207                                             SEQ_ALLOC_META, "meta");
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, seqno_t *seqnr)
217 {
218         int rc;
219         ENTRY;
220
221         LASSERT(range_is_sane(&seq->lcs_space));
222
223         if (range_is_exhausted(&seq->lcs_space)) {
224                 rc = seq_client_alloc_meta(env, seq);
225                 if (rc) {
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(!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 ["LPX64"]\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                 waitq_wait(link, TASK_UNINTERRUPTIBLE);
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         ++seq->lcs_update;
263         mutex_unlock(&seq->lcs_mutex);
264         return 0;
265 }
266
267 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
268 {
269         LASSERT(seq->lcs_update == 1);
270         mutex_lock(&seq->lcs_mutex);
271         --seq->lcs_update;
272         wake_up(&seq->lcs_waitq);
273 }
274
275 /**
276  * Allocate the whole seq to the caller.
277  **/
278 int seq_client_get_seq(const struct lu_env *env,
279                        struct lu_client_seq *seq, seqno_t *seqnr)
280 {
281         wait_queue_t link;
282         int rc;
283
284         LASSERT(seqnr != NULL);
285         mutex_lock(&seq->lcs_mutex);
286         init_waitqueue_entry_current(&link);
287
288         while (1) {
289                 rc = seq_fid_alloc_prep(seq, &link);
290                 if (rc == 0)
291                         break;
292         }
293
294         rc = seq_client_alloc_seq(env, seq, seqnr);
295         if (rc) {
296                 CERROR("%s: Can't allocate new sequence, "
297                        "rc %d\n", seq->lcs_name, rc);
298                 seq_fid_alloc_fini(seq);
299                 mutex_unlock(&seq->lcs_mutex);
300                 return rc;
301         }
302
303         CDEBUG(D_INFO, "%s: allocate sequence "
304                "[0x%16.16"LPF64"x]\n", seq->lcs_name, *seqnr);
305
306         /* Since the caller require the whole seq,
307          * so marked this seq to be used */
308         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
309                 seq->lcs_fid.f_oid = LUSTRE_METADATA_SEQ_MAX_WIDTH;
310         else
311                 seq->lcs_fid.f_oid = LUSTRE_DATA_SEQ_MAX_WIDTH;
312
313         seq->lcs_fid.f_seq = *seqnr;
314         seq->lcs_fid.f_ver = 0;
315         /*
316          * Inform caller that sequence switch is performed to allow it
317          * to setup FLD for it.
318          */
319         seq_fid_alloc_fini(seq);
320         mutex_unlock(&seq->lcs_mutex);
321
322         return rc;
323 }
324 EXPORT_SYMBOL(seq_client_get_seq);
325
326 /* Allocate new fid on passed client @seq and save it to @fid. */
327 int seq_client_alloc_fid(const struct lu_env *env,
328                          struct lu_client_seq *seq, struct lu_fid *fid)
329 {
330         wait_queue_t link;
331         int rc;
332         ENTRY;
333
334         LASSERT(seq != NULL);
335         LASSERT(fid != NULL);
336
337         init_waitqueue_entry_current(&link);
338         mutex_lock(&seq->lcs_mutex);
339
340         if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
341                 seq->lcs_fid.f_oid = seq->lcs_width;
342
343         while (1) {
344                 seqno_t seqnr;
345
346                 if (!fid_is_zero(&seq->lcs_fid) &&
347                     fid_oid(&seq->lcs_fid) < seq->lcs_width) {
348                         /* Just bump last allocated fid and return to caller. */
349                         seq->lcs_fid.f_oid += 1;
350                         rc = 0;
351                         break;
352                 }
353
354                 rc = seq_fid_alloc_prep(seq, &link);
355                 if (rc)
356                         continue;
357
358                 rc = seq_client_alloc_seq(env, seq, &seqnr);
359                 if (rc) {
360                         CERROR("%s: Can't allocate new sequence, "
361                                "rc %d\n", seq->lcs_name, rc);
362                         seq_fid_alloc_fini(seq);
363                         mutex_unlock(&seq->lcs_mutex);
364                         RETURN(rc);
365                 }
366
367                 CDEBUG(D_INFO, "%s: Switch to sequence "
368                        "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
369
370                 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
371                 seq->lcs_fid.f_seq = seqnr;
372                 seq->lcs_fid.f_ver = 0;
373
374                 /*
375                  * Inform caller that sequence switch is performed to allow it
376                  * to setup FLD for it.
377                  */
378                 rc = 1;
379
380                 seq_fid_alloc_fini(seq);
381                 break;
382         }
383
384         *fid = seq->lcs_fid;
385         mutex_unlock(&seq->lcs_mutex);
386
387         CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name,  PFID(fid));
388         RETURN(rc);
389 }
390 EXPORT_SYMBOL(seq_client_alloc_fid);
391
392 /*
393  * Finish the current sequence due to disconnect.
394  * See mdc_import_event()
395  */
396 void seq_client_flush(struct lu_client_seq *seq)
397 {
398         wait_queue_t link;
399
400         LASSERT(seq != NULL);
401         init_waitqueue_entry_current(&link);
402         mutex_lock(&seq->lcs_mutex);
403
404         while (seq->lcs_update) {
405                 add_wait_queue(&seq->lcs_waitq, &link);
406                 set_current_state(TASK_UNINTERRUPTIBLE);
407                 mutex_unlock(&seq->lcs_mutex);
408
409                 waitq_wait(&link, TASK_UNINTERRUPTIBLE);
410
411                 mutex_lock(&seq->lcs_mutex);
412                 remove_wait_queue(&seq->lcs_waitq, &link);
413                 set_current_state(TASK_RUNNING);
414         }
415
416         fid_zero(&seq->lcs_fid);
417         /**
418          * this id shld not be used for seq range allocation.
419          * set to -1 for dgb check.
420          */
421
422         seq->lcs_space.lsr_index = -1;
423
424         range_init(&seq->lcs_space);
425         mutex_unlock(&seq->lcs_mutex);
426 }
427 EXPORT_SYMBOL(seq_client_flush);
428
429 static void seq_client_proc_fini(struct lu_client_seq *seq)
430 {
431 #ifdef LPROCFS
432         ENTRY;
433         if (seq->lcs_proc_dir) {
434                 if (!IS_ERR(seq->lcs_proc_dir))
435                         lprocfs_remove(&seq->lcs_proc_dir);
436                 seq->lcs_proc_dir = NULL;
437         }
438         EXIT;
439 #endif /* LPROCFS */
440 }
441
442 static int seq_client_proc_init(struct lu_client_seq *seq)
443 {
444 #ifdef LPROCFS
445         int rc;
446         ENTRY;
447
448         seq->lcs_proc_dir = lprocfs_seq_register(seq->lcs_name,
449                                                  seq_type_proc_dir,
450                                                  NULL, NULL);
451         if (IS_ERR(seq->lcs_proc_dir)) {
452                 CERROR("%s: LProcFS failed in seq-init\n",
453                        seq->lcs_name);
454                 rc = PTR_ERR(seq->lcs_proc_dir);
455                 RETURN(rc);
456         }
457
458         rc = lprocfs_seq_add_vars(seq->lcs_proc_dir,
459                                   seq_client_proc_list, seq);
460         if (rc) {
461                 CERROR("%s: Can't init sequence manager "
462                        "proc, rc %d\n", seq->lcs_name, rc);
463                 GOTO(out_cleanup, rc);
464         }
465
466         RETURN(0);
467
468 out_cleanup:
469         seq_client_proc_fini(seq);
470         return rc;
471
472 #else /* LPROCFS */
473         return 0;
474 #endif
475 }
476
477 int seq_client_init(struct lu_client_seq *seq,
478                     struct obd_export *exp,
479                     enum lu_cli_type type,
480                     const char *prefix,
481                     struct lu_server_seq *srv)
482 {
483         int rc;
484         ENTRY;
485
486         LASSERT(seq != NULL);
487         LASSERT(prefix != NULL);
488
489         seq->lcs_srv = srv;
490         seq->lcs_type = type;
491
492         mutex_init(&seq->lcs_mutex);
493         if (type == LUSTRE_SEQ_METADATA)
494                 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
495         else
496                 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
497
498         init_waitqueue_head(&seq->lcs_waitq);
499         /* Make sure that things are clear before work is started. */
500         seq_client_flush(seq);
501
502         if (exp != NULL)
503                 seq->lcs_exp = class_export_get(exp);
504
505         snprintf(seq->lcs_name, sizeof(seq->lcs_name),
506                  "cli-%s", prefix);
507
508         rc = seq_client_proc_init(seq);
509         if (rc)
510                 seq_client_fini(seq);
511         RETURN(rc);
512 }
513 EXPORT_SYMBOL(seq_client_init);
514
515 void seq_client_fini(struct lu_client_seq *seq)
516 {
517         ENTRY;
518
519         seq_client_proc_fini(seq);
520
521         if (seq->lcs_exp != NULL) {
522                 class_export_put(seq->lcs_exp);
523                 seq->lcs_exp = NULL;
524         }
525
526         seq->lcs_srv = NULL;
527         EXIT;
528 }
529 EXPORT_SYMBOL(seq_client_fini);
530
531 int client_fid_init(struct obd_device *obd,
532                     struct obd_export *exp, enum lu_cli_type type)
533 {
534         struct client_obd *cli = &obd->u.cli;
535         char *prefix;
536         int rc;
537         ENTRY;
538
539         OBD_ALLOC_PTR(cli->cl_seq);
540         if (cli->cl_seq == NULL)
541                 RETURN(-ENOMEM);
542
543         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
544         if (prefix == NULL)
545                 GOTO(out_free_seq, rc = -ENOMEM);
546
547         snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
548
549         /* Init client side sequence-manager */
550         rc = seq_client_init(cli->cl_seq, exp, type, prefix, NULL);
551         OBD_FREE(prefix, MAX_OBD_NAME + 5);
552         if (rc)
553                 GOTO(out_free_seq, rc);
554
555         RETURN(rc);
556 out_free_seq:
557         OBD_FREE_PTR(cli->cl_seq);
558         cli->cl_seq = NULL;
559         return rc;
560 }
561 EXPORT_SYMBOL(client_fid_init);
562
563 int client_fid_fini(struct obd_device *obd)
564 {
565         struct client_obd *cli = &obd->u.cli;
566         ENTRY;
567
568         if (cli->cl_seq != NULL) {
569                 seq_client_fini(cli->cl_seq);
570                 OBD_FREE_PTR(cli->cl_seq);
571                 cli->cl_seq = NULL;
572         }
573
574         RETURN(0);
575 }
576 EXPORT_SYMBOL(client_fid_fini);
577
578 #ifdef __KERNEL__
579 struct proc_dir_entry *seq_type_proc_dir;
580
581 static int __init fid_mod_init(void)
582 {
583         seq_type_proc_dir = lprocfs_seq_register(LUSTRE_SEQ_NAME,
584                                                  proc_lustre_root,
585                                                  NULL, NULL);
586         if (IS_ERR(seq_type_proc_dir))
587                 return PTR_ERR(seq_type_proc_dir);
588
589 # ifdef HAVE_SERVER_SUPPORT
590         fid_server_mod_init();
591 # endif
592
593         return 0;
594 }
595
596 static void __exit fid_mod_exit(void)
597 {
598 # ifdef HAVE_SERVER_SUPPORT
599         fid_server_mod_exit();
600 # endif
601
602         if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
603                 lprocfs_remove(&seq_type_proc_dir);
604                 seq_type_proc_dir = NULL;
605         }
606 }
607
608 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
609 MODULE_DESCRIPTION("Lustre FID Module");
610 MODULE_LICENSE("GPL");
611
612 cfs_module(fid, "0.1.0", fid_mod_init, fid_mod_exit);
613 #endif /* __KERNEL__ */