4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2013, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdt/mdt_mds.c
33 * Lustre Metadata Service Layer
35 * Author: Di Wang <di.wang@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <linux/module.h>
42 #include <obd_support.h>
43 /* struct ptlrpc_request */
44 #include <lustre_net.h>
45 /* struct obd_export */
46 #include <lustre_export.h>
47 /* struct obd_device */
50 #include <dt_object.h>
51 #include <lustre_mds.h>
52 #include "mdt_internal.h"
53 #include <lustre_quota.h>
54 #include <lustre_acl.h>
55 #include <uapi/linux/lustre/lustre_param.h>
59 struct md_device mds_md_dev;
60 struct ptlrpc_service *mds_regular_service;
61 struct ptlrpc_service *mds_readpage_service;
62 struct ptlrpc_service *mds_out_service;
63 struct ptlrpc_service *mds_mdsc_service;
64 struct ptlrpc_service *mds_mdss_service;
65 struct ptlrpc_service *mds_fld_service;
66 struct ptlrpc_service *mds_io_service;
67 struct mutex mds_health_mutex;
71 * * Initialized in mds_mod_init().
73 static unsigned long mds_num_threads;
74 module_param(mds_num_threads, ulong, 0444);
75 MODULE_PARM_DESC(mds_num_threads, "number of MDS service threads to start");
77 static unsigned int mds_cpu_bind = 1;
78 module_param(mds_cpu_bind, uint, 0444);
79 MODULE_PARM_DESC(mds_cpu_bind,
80 "bind MDS threads to particular CPU partitions");
82 int mds_max_io_threads = 512;
83 module_param(mds_max_io_threads, int, 0444);
84 MODULE_PARM_DESC(mds_max_io_threads,
85 "maximum number of MDS IO service threads");
87 static unsigned int mds_io_cpu_bind = 1;
88 module_param(mds_io_cpu_bind, uint, 0444);
89 MODULE_PARM_DESC(mds_io_cpu_bind,
90 "bind MDS IO threads to particular CPU partitions");
92 static char *mds_io_num_cpts;
93 module_param(mds_io_num_cpts, charp, 0444);
94 MODULE_PARM_DESC(mds_io_num_cpts,
95 "CPU partitions MDS IO threads should run on");
97 static struct cfs_cpt_table *mdt_io_cptable;
99 static char *mds_num_cpts;
100 module_param(mds_num_cpts, charp, 0444);
101 MODULE_PARM_DESC(mds_num_cpts, "CPU partitions MDS threads should run on");
103 static unsigned long mds_rdpg_num_threads;
104 module_param(mds_rdpg_num_threads, ulong, 0444);
105 MODULE_PARM_DESC(mds_rdpg_num_threads,
106 "number of MDS readpage service threads to start");
108 static unsigned int mds_rdpg_cpu_bind = 1;
109 module_param(mds_rdpg_cpu_bind, uint, 0444);
110 MODULE_PARM_DESC(mds_rdpg_cpu_bind,
111 "bind MDS readpage threads to particular CPU partitions");
113 static char *mds_rdpg_num_cpts;
114 module_param(mds_rdpg_num_cpts, charp, 0444);
115 MODULE_PARM_DESC(mds_rdpg_num_cpts,
116 "CPU partitions MDS readpage threads should run on");
118 /* device init/fini methods */
119 static void mds_stop_ptlrpc_service(struct mds_device *m)
123 mutex_lock(&m->mds_health_mutex);
124 if (m->mds_regular_service != NULL) {
125 ptlrpc_unregister_service(m->mds_regular_service);
126 m->mds_regular_service = NULL;
128 if (m->mds_readpage_service != NULL) {
129 ptlrpc_unregister_service(m->mds_readpage_service);
130 m->mds_readpage_service = NULL;
132 if (m->mds_out_service != NULL) {
133 ptlrpc_unregister_service(m->mds_out_service);
134 m->mds_out_service = NULL;
136 if (m->mds_mdsc_service != NULL) {
137 ptlrpc_unregister_service(m->mds_mdsc_service);
138 m->mds_mdsc_service = NULL;
140 if (m->mds_mdss_service != NULL) {
141 ptlrpc_unregister_service(m->mds_mdss_service);
142 m->mds_mdss_service = NULL;
144 if (m->mds_fld_service != NULL) {
145 ptlrpc_unregister_service(m->mds_fld_service);
146 m->mds_fld_service = NULL;
148 if (m->mds_io_service != NULL) {
149 ptlrpc_unregister_service(m->mds_io_service);
150 m->mds_io_service = NULL;
152 mutex_unlock(&m->mds_health_mutex);
154 if (mdt_io_cptable != NULL) {
155 cfs_cpt_table_free(mdt_io_cptable);
156 mdt_io_cptable = NULL;
162 static int mds_start_ptlrpc_service(struct mds_device *m)
164 static struct ptlrpc_service_conf conf;
165 struct obd_device *obd = m->mds_md_dev.md_lu_dev.ld_obd;
171 conf = (typeof(conf)) {
172 .psc_name = LUSTRE_MDT_NAME,
173 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
175 .bc_nbufs = MDS_NBUFS,
176 .bc_buf_size = MDS_REG_BUFSIZE,
177 .bc_req_max_size = MDS_REG_MAXREQSIZE,
178 .bc_rep_max_size = MDS_REG_MAXREPSIZE,
179 .bc_req_portal = MDS_REQUEST_PORTAL,
180 .bc_rep_portal = MDC_REPLY_PORTAL,
183 * We'd like to have a mechanism to set this on a per-device
187 .tc_thr_name = LUSTRE_MDT_NAME,
188 .tc_thr_factor = MDS_THR_FACTOR,
189 .tc_nthrs_init = MDS_NTHRS_INIT,
190 .tc_nthrs_base = MDS_NTHRS_BASE,
191 .tc_nthrs_max = MDS_NTHRS_MAX,
192 .tc_nthrs_user = mds_num_threads,
193 .tc_cpu_bind = mds_cpu_bind,
194 /* LCT_DT_THREAD is required as MDT threads may scan
195 * all LDLM namespaces (including OFD-originated) to
196 * cancel LDLM locks */
197 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD,
200 .cc_pattern = mds_num_cpts,
204 .so_req_handler = tgt_request_handle,
205 .so_req_printer = target_print_req,
206 .so_hpreq_handler = ptlrpc_hpreq_handler,
209 m->mds_regular_service = ptlrpc_register_service(&conf, &obd->obd_kset,
210 obd->obd_debugfs_entry);
211 if (IS_ERR(m->mds_regular_service)) {
212 rc = PTR_ERR(m->mds_regular_service);
213 CERROR("failed to start regular mdt service: %d\n", rc);
214 m->mds_regular_service = NULL;
220 * readpage service configuration. Parameters have to be adjusted,
223 memset(&conf, 0, sizeof(conf));
224 conf = (typeof(conf)) {
225 .psc_name = LUSTRE_MDT_NAME "_readpage",
226 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
228 .bc_nbufs = MDS_NBUFS,
229 .bc_buf_size = MDS_BUFSIZE,
230 .bc_req_max_size = MDS_MAXREQSIZE,
231 .bc_rep_max_size = MDS_MAXREPSIZE,
232 .bc_req_portal = MDS_READPAGE_PORTAL,
233 .bc_rep_portal = MDC_REPLY_PORTAL,
236 .tc_thr_name = LUSTRE_MDT_NAME "_rdpg",
237 .tc_thr_factor = MDS_RDPG_THR_FACTOR,
238 .tc_nthrs_init = MDS_RDPG_NTHRS_INIT,
239 .tc_nthrs_base = MDS_RDPG_NTHRS_BASE,
240 .tc_nthrs_max = MDS_RDPG_NTHRS_MAX,
241 .tc_nthrs_user = mds_rdpg_num_threads,
242 .tc_cpu_bind = mds_rdpg_cpu_bind,
243 .tc_ctx_tags = LCT_MD_THREAD,
246 .cc_pattern = mds_rdpg_num_cpts,
250 .so_req_handler = tgt_request_handle,
251 .so_req_printer = target_print_req,
254 m->mds_readpage_service = ptlrpc_register_service(&conf, &obd->obd_kset,
255 obd->obd_debugfs_entry);
256 if (IS_ERR(m->mds_readpage_service)) {
257 rc = PTR_ERR(m->mds_readpage_service);
258 CERROR("failed to start readpage service: %d\n", rc);
259 m->mds_readpage_service = NULL;
261 GOTO(err_mds_svc, rc);
264 /* Object update service */
265 conf = (typeof(conf)) {
266 .psc_name = LUSTRE_MDT_NAME "_out",
267 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
269 .bc_nbufs = MDS_NBUFS,
270 .bc_buf_size = OUT_BUFSIZE,
271 .bc_req_max_size = OUT_MAXREQSIZE,
272 .bc_rep_max_size = OUT_MAXREPSIZE,
273 .bc_req_portal = OUT_PORTAL,
274 .bc_rep_portal = OSC_REPLY_PORTAL,
277 * We'd like to have a mechanism to set this on a per-device
281 .tc_thr_name = LUSTRE_MDT_NAME "_out",
282 .tc_thr_factor = MDS_THR_FACTOR,
283 .tc_nthrs_init = MDS_NTHRS_INIT,
284 .tc_nthrs_base = MDS_NTHRS_BASE,
285 .tc_nthrs_max = MDS_NTHRS_MAX,
286 .tc_nthrs_user = mds_num_threads,
287 .tc_cpu_bind = mds_cpu_bind,
288 .tc_ctx_tags = LCT_MD_THREAD |
292 .cc_pattern = mds_num_cpts,
296 .so_req_handler = tgt_request_handle,
297 .so_req_printer = target_print_req,
298 .so_hpreq_handler = NULL,
301 m->mds_out_service = ptlrpc_register_service(&conf, &obd->obd_kset,
302 obd->obd_debugfs_entry);
303 if (IS_ERR(m->mds_out_service)) {
304 rc = PTR_ERR(m->mds_out_service);
305 CERROR("failed to start out service: %d\n", rc);
306 m->mds_out_service = NULL;
307 GOTO(err_mds_svc, rc);
311 * sequence controller service configuration
313 memset(&conf, 0, sizeof(conf));
314 conf = (typeof(conf)) {
315 .psc_name = LUSTRE_MDT_NAME "_seqs",
316 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
318 .bc_nbufs = MDS_NBUFS,
319 .bc_buf_size = SEQ_BUFSIZE,
320 .bc_req_max_size = SEQ_MAXREQSIZE,
321 .bc_rep_max_size = SEQ_MAXREPSIZE,
322 .bc_req_portal = SEQ_CONTROLLER_PORTAL,
323 .bc_rep_portal = MDC_REPLY_PORTAL,
326 .tc_thr_name = LUSTRE_MDT_NAME "_seqs",
327 .tc_nthrs_init = MDS_OTHR_NTHRS_INIT,
328 .tc_nthrs_max = MDS_OTHR_NTHRS_MAX,
329 .tc_ctx_tags = LCT_MD_THREAD,
332 .so_req_handler = tgt_request_handle,
333 .so_req_printer = target_print_req,
334 .so_hpreq_handler = NULL,
337 m->mds_mdsc_service = ptlrpc_register_service(&conf, &obd->obd_kset,
338 obd->obd_debugfs_entry);
339 if (IS_ERR(m->mds_mdsc_service)) {
340 rc = PTR_ERR(m->mds_mdsc_service);
341 CERROR("failed to start seq controller service: %d\n", rc);
342 m->mds_mdsc_service = NULL;
344 GOTO(err_mds_svc, rc);
348 * metadata sequence server service configuration
350 memset(&conf, 0, sizeof(conf));
351 conf = (typeof(conf)) {
352 .psc_name = LUSTRE_MDT_NAME "_seqm",
353 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
355 .bc_nbufs = MDS_NBUFS,
356 .bc_buf_size = SEQ_BUFSIZE,
357 .bc_req_max_size = SEQ_MAXREQSIZE,
358 .bc_rep_max_size = SEQ_MAXREPSIZE,
359 .bc_req_portal = SEQ_METADATA_PORTAL,
360 .bc_rep_portal = MDC_REPLY_PORTAL,
363 .tc_thr_name = LUSTRE_MDT_NAME "_seqm",
364 .tc_nthrs_init = MDS_OTHR_NTHRS_INIT,
365 .tc_nthrs_max = MDS_OTHR_NTHRS_MAX,
366 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
369 .so_req_handler = tgt_request_handle,
370 .so_req_printer = target_print_req,
371 .so_hpreq_handler = NULL,
374 m->mds_mdss_service = ptlrpc_register_service(&conf, &obd->obd_kset,
375 obd->obd_debugfs_entry);
376 if (IS_ERR(m->mds_mdss_service)) {
377 rc = PTR_ERR(m->mds_mdss_service);
378 CERROR("failed to start metadata seq server service: %d\n", rc);
379 m->mds_mdss_service = NULL;
381 GOTO(err_mds_svc, rc);
384 /* FLD service start */
385 memset(&conf, 0, sizeof(conf));
386 conf = (typeof(conf)) {
387 .psc_name = LUSTRE_MDT_NAME "_fld",
388 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
390 .bc_nbufs = MDS_NBUFS,
391 .bc_buf_size = FLD_BUFSIZE,
392 .bc_req_max_size = FLD_MAXREQSIZE,
393 .bc_rep_max_size = FLD_MAXREPSIZE,
394 .bc_req_portal = FLD_REQUEST_PORTAL,
395 .bc_rep_portal = MDC_REPLY_PORTAL,
398 .tc_thr_name = LUSTRE_MDT_NAME "_fld",
399 .tc_nthrs_init = MDS_OTHR_NTHRS_INIT,
400 .tc_nthrs_max = MDS_OTHR_NTHRS_MAX,
401 .tc_ctx_tags = LCT_DT_THREAD | LCT_MD_THREAD,
404 .so_req_handler = tgt_request_handle,
405 .so_req_printer = target_print_req,
406 .so_hpreq_handler = NULL,
409 m->mds_fld_service = ptlrpc_register_service(&conf, &obd->obd_kset,
410 obd->obd_debugfs_entry);
411 if (IS_ERR(m->mds_fld_service)) {
412 rc = PTR_ERR(m->mds_fld_service);
413 CERROR("failed to start fld service: %d\n", rc);
414 m->mds_fld_service = NULL;
416 GOTO(err_mds_svc, rc);
420 mask = cfs_cpt_nodemask(cfs_cpt_tab, CFS_CPT_ANY);
421 /* event CPT feature is disabled in libcfs level by set partition
422 * number to 1, we still want to set node affinity for io service */
423 if (cfs_cpt_number(cfs_cpt_tab) == 1 && nodes_weight(*mask) > 1) {
427 mdt_io_cptable = cfs_cpt_table_alloc(nodes_weight(*mask));
428 for_each_node_mask(i, *mask) {
429 if (mdt_io_cptable == NULL) {
430 CWARN("MDS failed to create CPT table\n");
434 rc = cfs_cpt_set_node(mdt_io_cptable, cpt++, i);
436 CWARN("MDS Failed to set node %d for IO CPT table\n",
438 cfs_cpt_table_free(mdt_io_cptable);
439 mdt_io_cptable = NULL;
445 memset(&conf, 0, sizeof(conf));
446 conf = (typeof(conf)) {
447 .psc_name = LUSTRE_MDT_NAME "_io",
448 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
450 .bc_nbufs = OST_NBUFS,
451 .bc_buf_size = OST_IO_BUFSIZE,
452 .bc_req_max_size = OST_IO_MAXREQSIZE,
453 .bc_rep_max_size = OST_IO_MAXREPSIZE,
454 .bc_req_portal = MDS_IO_PORTAL,
455 .bc_rep_portal = MDC_REPLY_PORTAL,
458 .tc_thr_name = LUSTRE_MDT_NAME "_io",
459 .tc_thr_factor = OSS_THR_FACTOR,
460 .tc_nthrs_init = OSS_NTHRS_INIT,
461 .tc_nthrs_base = OSS_NTHRS_BASE,
462 .tc_nthrs_max = mds_max_io_threads,
463 .tc_nthrs_user = mds_num_threads,
464 .tc_cpu_bind = mds_io_cpu_bind,
465 .tc_ctx_tags = LCT_DT_THREAD | LCT_MD_THREAD,
468 .cc_cptable = mdt_io_cptable,
469 .cc_pattern = mdt_io_cptable == NULL ?
470 mds_io_num_cpts : NULL,
474 .so_thr_init = tgt_io_thread_init,
475 .so_thr_done = tgt_io_thread_done,
476 .so_req_handler = tgt_request_handle,
477 .so_req_printer = target_print_req,
478 .so_hpreq_handler = tgt_hpreq_handler,
481 m->mds_io_service = ptlrpc_register_service(&conf, &obd->obd_kset,
482 obd->obd_debugfs_entry);
483 if (IS_ERR(m->mds_io_service)) {
484 rc = PTR_ERR(m->mds_io_service);
485 CERROR("failed to start MDT I/O service: %d\n", rc);
486 m->mds_io_service = NULL;
487 GOTO(err_mds_svc, rc);
493 mds_stop_ptlrpc_service(m);
498 static inline struct mds_device *mds_dev(struct lu_device *d)
500 return container_of_safe(d, struct mds_device, mds_md_dev.md_lu_dev);
503 static struct lu_device *mds_device_fini(const struct lu_env *env,
506 struct mds_device *m = mds_dev(d);
507 struct obd_device *obd = d->ld_obd;
510 mds_stop_ptlrpc_service(m);
511 lprocfs_obd_cleanup(obd);
515 static struct lu_device *mds_device_free(const struct lu_env *env,
518 struct mds_device *m = mds_dev(d);
521 md_device_fini(&m->mds_md_dev);
526 static struct lu_device *mds_device_alloc(const struct lu_env *env,
527 struct lu_device_type *t,
528 struct lustre_cfg *cfg)
530 struct mds_device *m;
531 struct obd_device *obd;
537 return ERR_PTR(-ENOMEM);
539 md_device_init(&m->mds_md_dev, t);
540 l = &m->mds_md_dev.md_lu_dev;
542 obd = class_name2obd(lustre_cfg_string(cfg, 0));
543 LASSERT(obd != NULL);
546 /* set this lu_device to obd, because error handling need it */
549 rc = lprocfs_obd_setup(obd, true);
551 mds_device_free(env, l);
556 mutex_init(&m->mds_health_mutex);
558 rc = mds_start_ptlrpc_service(m);
560 lprocfs_obd_cleanup(obd);
561 mds_device_free(env, l);
568 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
569 LU_TYPE_INIT_FINI(mds, &mdt_thread_key);
571 static const struct lu_device_type_operations mds_device_type_ops = {
572 .ldto_init = mds_type_init,
573 .ldto_fini = mds_type_fini,
575 .ldto_start = mds_type_start,
576 .ldto_stop = mds_type_stop,
578 .ldto_device_alloc = mds_device_alloc,
579 .ldto_device_free = mds_device_free,
580 .ldto_device_fini = mds_device_fini
583 static struct lu_device_type mds_device_type = {
584 .ldt_tags = LU_DEVICE_MD,
585 .ldt_name = LUSTRE_MDS_NAME,
586 .ldt_ops = &mds_device_type_ops,
587 .ldt_ctx_tags = LCT_MD_THREAD
590 static int mds_health_check(const struct lu_env *env, struct obd_device *obd)
592 struct mds_device *mds = mds_dev(obd->obd_lu_dev);
596 mutex_lock(&mds->mds_health_mutex);
597 rc |= ptlrpc_service_health_check(mds->mds_regular_service);
598 rc |= ptlrpc_service_health_check(mds->mds_readpage_service);
599 rc |= ptlrpc_service_health_check(mds->mds_out_service);
600 rc |= ptlrpc_service_health_check(mds->mds_mdsc_service);
601 rc |= ptlrpc_service_health_check(mds->mds_mdss_service);
602 rc |= ptlrpc_service_health_check(mds->mds_fld_service);
603 rc |= ptlrpc_service_health_check(mds->mds_io_service);
604 mutex_unlock(&mds->mds_health_mutex);
606 return rc != 0 ? 1 : 0;
609 static const struct obd_ops mds_obd_device_ops = {
610 .o_owner = THIS_MODULE,
611 .o_health_check = mds_health_check,
614 int mds_mod_init(void)
616 return class_register_type(&mds_obd_device_ops, NULL, false,
617 LUSTRE_MDS_NAME, &mds_device_type);
620 void mds_mod_exit(void)
622 class_unregister_type(LUSTRE_MDS_NAME);