Whamcloud - gitweb
8bef0431b076879b005b8f9e44a22cacd0b885ab
[fs/lustre-release.git] / lustre / mdt / mdt_mds.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,
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.
15
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
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, 2012 Intel Corporation
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/mdt/mdt_mds.c
32  *
33  * Lustre Metadata Service Layer
34  *
35  * Author: Di Wang <di.wang@whamcloud.com>
36  **/
37
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <linux/module.h>
41
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 */
48 #include <obd.h>
49 /* lu2dt_dev() */
50 #include <dt_object.h>
51 #include <lustre_mds.h>
52 #include <lustre_mdt.h>
53 #include "mdt_internal.h"
54 #ifdef HAVE_QUOTA_SUPPORT
55 # include <lustre_quota.h>
56 #endif
57 #include <lustre_acl.h>
58 #include <lustre_param.h>
59 #include <lustre_fsfilt.h>
60
61 struct mds_device {
62         /* super-class */
63         struct md_device           mds_md_dev;
64         struct ptlrpc_service     *mds_regular_service;
65         struct ptlrpc_service     *mds_readpage_service;
66         struct ptlrpc_service     *mds_setattr_service;
67         struct ptlrpc_service     *mds_mdsc_service;
68         struct ptlrpc_service     *mds_mdss_service;
69         struct ptlrpc_service     *mds_fld_service;
70 };
71
72 /*
73  *  * Initialized in mdt_mod_init().
74  *   */
75 static unsigned long mdt_num_threads;
76 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
77                 "number of MDS service threads to start "
78                 "(deprecated in favor of mds_num_threads)");
79
80 static unsigned long mds_num_threads;
81 CFS_MODULE_PARM(mds_num_threads, "ul", ulong, 0444,
82                 "number of MDS service threads to start");
83
84 static char *mds_num_cpts;
85 CFS_MODULE_PARM(mds_num_cpts, "c", charp, 0444,
86                 "CPU partitions MDS threads should run on");
87
88 static unsigned long mds_rdpg_num_threads;
89 CFS_MODULE_PARM(mds_rdpg_num_threads, "ul", ulong, 0444,
90                 "number of MDS readpage service threads to start");
91
92 static char *mds_rdpg_num_cpts;
93 CFS_MODULE_PARM(mds_rdpg_num_cpts, "c", charp, 0444,
94                 "CPU partitions MDS readpage threads should run on");
95
96 /* NB: these two should be removed along with setattr service in the future */
97 static unsigned long mds_attr_num_threads;
98 CFS_MODULE_PARM(mds_attr_num_threads, "ul", ulong, 0444,
99                 "number of MDS setattr service threads to start");
100
101 static char *mds_attr_num_cpts;
102 CFS_MODULE_PARM(mds_attr_num_cpts, "c", charp, 0444,
103                 "CPU partitions MDS setattr threads should run on");
104
105 #define DEFINE_RPC_HANDLER(base, flags, opc, fn, fmt)                   \
106 [opc - base] = {                                                        \
107         .mh_name        = #opc,                                         \
108         .mh_fail_id     = OBD_FAIL_ ## opc ## _NET,                     \
109         .mh_opc         = opc,                                          \
110         .mh_flags       = flags,                                        \
111         .mh_act         = fn,                                           \
112         .mh_fmt         = fmt                                           \
113 }
114
115 /* Request with a format known in advance */
116 #define DEF_MDT_HDL(flags, name, fn)                                    \
117         DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, &RQF_ ## name)
118
119 /* Request with a format we do not yet know */
120 #define DEF_MDT_HDL_VAR(flags, name, fn)                                \
121         DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, NULL)
122
123 /* Map one non-standard request format handler.  This should probably get
124  * a common OBD_SET_INFO RPC opcode instead of this mismatch. */
125 #define RQF_MDS_SET_INFO RQF_OBD_SET_INFO
126
127 static struct mdt_handler mdt_mds_ops[] = {
128 DEF_MDT_HDL(0,                          MDS_CONNECT,      mdt_connect),
129 DEF_MDT_HDL(0,                          MDS_DISCONNECT,   mdt_disconnect),
130 DEF_MDT_HDL(0,                          MDS_SET_INFO,     mdt_set_info),
131 DEF_MDT_HDL(0,                          MDS_GET_INFO,     mdt_get_info),
132 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_GETSTATUS,    mdt_getstatus),
133 DEF_MDT_HDL(HABEO_CORPUS,               MDS_GETATTR,      mdt_getattr),
134 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME, mdt_getattr_name),
135 DEF_MDT_HDL(HABEO_CORPUS,               MDS_GETXATTR,     mdt_getxattr),
136 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_STATFS,       mdt_statfs),
137 DEF_MDT_HDL(0           | MUTABOR,      MDS_REINT,        mdt_reint),
138 DEF_MDT_HDL(HABEO_CORPUS,               MDS_CLOSE,        mdt_close),
139 DEF_MDT_HDL(HABEO_CORPUS,               MDS_DONE_WRITING, mdt_done_writing),
140 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_PIN,          mdt_pin),
141 DEF_MDT_HDL_VAR(0,                      MDS_SYNC,         mdt_sync),
142 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR,    mdt_is_subdir),
143 DEF_MDT_HDL(0,                          MDS_QUOTACHECK,   mdt_quotacheck),
144 DEF_MDT_HDL(0,                          MDS_QUOTACTL,     mdt_quotactl),
145 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_HSM_PROGRESS, mdt_hsm_progress),
146 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_HSM_CT_REGISTER,
147                                                 mdt_hsm_ct_register),
148 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_HSM_CT_UNREGISTER,
149                                                 mdt_hsm_ct_unregister),
150 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
151                                                 mdt_hsm_state_get),
152 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_SET,
153                                                 mdt_hsm_state_set),
154 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION,
155                                                 mdt_hsm_action),
156 };
157
158 #define DEF_OBD_HDL(flags, name, fn)                                    \
159         DEFINE_RPC_HANDLER(OBD_PING, flags, name, fn, NULL)
160
161 static struct mdt_handler mdt_obd_ops[] = {
162 DEF_OBD_HDL(0,                          OBD_PING,         mdt_obd_ping),
163 DEF_OBD_HDL(0,                          OBD_LOG_CANCEL,   mdt_obd_log_cancel),
164 DEF_OBD_HDL(0,                          OBD_QC_CALLBACK,  mdt_obd_qc_callback),
165 DEF_OBD_HDL(0,                          OBD_IDX_READ,     mdt_obd_idx_read)
166 };
167
168 #define DEF_DLM_HDL_VAR(flags, name, fn)                                \
169         DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, NULL)
170 #define DEF_DLM_HDL(flags, name, fn)                                    \
171         DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, &RQF_ ## name)
172
173 static struct mdt_handler mdt_dlm_ops[] = {
174 DEF_DLM_HDL    (HABEO_CLAVIS,           LDLM_ENQUEUE,     mdt_enqueue),
175 DEF_DLM_HDL_VAR(HABEO_CLAVIS,           LDLM_CONVERT,     mdt_convert),
176 DEF_DLM_HDL_VAR(0,                      LDLM_BL_CALLBACK, mdt_bl_callback),
177 DEF_DLM_HDL_VAR(0,                      LDLM_CP_CALLBACK, mdt_cp_callback)
178 };
179
180 #define DEF_LLOG_HDL(flags, name, fn)                                   \
181         DEFINE_RPC_HANDLER(LLOG_ORIGIN_HANDLE_CREATE, flags, name, fn, NULL)
182
183 static struct mdt_handler mdt_llog_ops[] = {
184 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_CREATE,        mdt_llog_create),
185 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_NEXT_BLOCK,    mdt_llog_next_block),
186 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_READ_HEADER,   mdt_llog_read_header),
187 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_WRITE_REC,     NULL),
188 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_CLOSE,         NULL),
189 DEF_LLOG_HDL(0,         LLOG_ORIGIN_CONNECT,              NULL),
190 DEF_LLOG_HDL(0,         LLOG_CATINFO,                     NULL),
191 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_PREV_BLOCK,    mdt_llog_prev_block),
192 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_DESTROY,       mdt_llog_destroy),
193 };
194
195 #define DEF_SEC_HDL(flags, name, fn)                                    \
196         DEFINE_RPC_HANDLER(SEC_CTX_INIT, flags, name, fn, NULL)
197
198 static struct mdt_handler mdt_sec_ctx_ops[] = {
199 DEF_SEC_HDL(0,                          SEC_CTX_INIT,     mdt_sec_ctx_handle),
200 DEF_SEC_HDL(0,                          SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
201 DEF_SEC_HDL(0,                          SEC_CTX_FINI,     mdt_sec_ctx_handle)
202 };
203
204 #define DEF_QUOTA_HDL(flags, name, fn)                          \
205         DEFINE_RPC_HANDLER(QUOTA_DQACQ, flags, name, fn, &RQF_ ## name)
206
207 static struct mdt_handler mdt_quota_ops[] = {
208 DEF_QUOTA_HDL(HABEO_REFERO,             QUOTA_DQACQ,      mdt_quota_dqacq),
209 };
210
211 struct mdt_opc_slice mdt_regular_handlers[] = {
212         {
213                 .mos_opc_start  = MDS_GETATTR,
214                 .mos_opc_end    = MDS_LAST_OPC,
215                 .mos_hs         = mdt_mds_ops
216         },
217         {
218                 .mos_opc_start  = OBD_PING,
219                 .mos_opc_end    = OBD_LAST_OPC,
220                 .mos_hs         = mdt_obd_ops
221         },
222         {
223                 .mos_opc_start  = LDLM_ENQUEUE,
224                 .mos_opc_end    = LDLM_LAST_OPC,
225                 .mos_hs         = mdt_dlm_ops
226         },
227         {
228                 .mos_opc_start  = LLOG_ORIGIN_HANDLE_CREATE,
229                 .mos_opc_end    = LLOG_LAST_OPC,
230                 .mos_hs         = mdt_llog_ops
231         },
232         {
233                 .mos_opc_start  = SEC_CTX_INIT,
234                 .mos_opc_end    = SEC_LAST_OPC,
235                 .mos_hs         = mdt_sec_ctx_ops
236         },
237         {
238                 .mos_opc_start  = QUOTA_DQACQ,
239                 .mos_opc_end    = QUOTA_LAST_OPC,
240                 .mos_hs         = mdt_quota_ops
241         },
242         {
243                 .mos_hs         = NULL
244         }
245 };
246
247 /* Readpage/readdir handlers */
248 static struct mdt_handler mdt_readpage_ops[] = {
249 DEF_MDT_HDL(0,                  MDS_CONNECT,  mdt_connect),
250 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO, MDS_READPAGE, mdt_readpage),
251 /* XXX: this is ugly and should be fixed one day, see mdc_close() for
252  * detailed comments. --umka */
253 DEF_MDT_HDL(HABEO_CORPUS,               MDS_CLOSE,        mdt_close),
254 DEF_MDT_HDL(HABEO_CORPUS,               MDS_DONE_WRITING, mdt_done_writing),
255 };
256
257 static struct mdt_opc_slice mdt_readpage_handlers[] = {
258         {
259                 .mos_opc_start = MDS_GETATTR,
260                 .mos_opc_end   = MDS_LAST_OPC,
261                 .mos_hs = mdt_readpage_ops
262         },
263         {
264                 .mos_opc_start = OBD_FIRST_OPC,
265                 .mos_opc_end   = OBD_LAST_OPC,
266                 .mos_hs = mdt_obd_ops
267         },
268         {
269                 .mos_hs = NULL
270         }
271 };
272
273 /* Sequence service handlers */
274 #define DEF_SEQ_HDL(flags, name, fn)                                    \
275         DEFINE_RPC_HANDLER(SEQ_QUERY, flags, name, fn, &RQF_ ## name)
276
277 static struct mdt_handler mdt_seq_ops[] = {
278 DEF_SEQ_HDL(0,                          SEQ_QUERY,        (void *)seq_query),
279 };
280
281 struct mdt_opc_slice mdt_seq_handlers[] = {
282         {
283                 .mos_opc_start = SEQ_QUERY,
284                 .mos_opc_end   = SEQ_LAST_OPC,
285                 .mos_hs = mdt_seq_ops
286         },
287         {
288                 .mos_hs = NULL
289         }
290 };
291
292 /* FID Location Database handlers */
293 #define DEF_FLD_HDL(flags, name, fn)                                    \
294         DEFINE_RPC_HANDLER(FLD_QUERY, flags, name, fn, &RQF_ ## name)
295
296 static struct mdt_handler mdt_fld_ops[] = {
297 DEF_FLD_HDL(0,                          FLD_QUERY,        (void *)fld_query),
298 };
299
300 struct mdt_opc_slice mdt_fld_handlers[] = {
301         {
302                 .mos_opc_start = FLD_QUERY,
303                 .mos_opc_end   = FLD_LAST_OPC,
304                 .mos_hs = mdt_fld_ops
305         },
306         {
307                 .mos_hs = NULL
308         }
309 };
310
311 static int mds_regular_handle(struct ptlrpc_request *req)
312 {
313         return mdt_handle_common(req, mdt_regular_handlers);
314 }
315
316 static int mds_readpage_handle(struct ptlrpc_request *req)
317 {
318         return mdt_handle_common(req, mdt_readpage_handlers);
319 }
320
321 static int mds_mdsc_handle(struct ptlrpc_request *req)
322 {
323         return mdt_handle_common(req, mdt_seq_handlers);
324 }
325
326 static int mds_mdss_handle(struct ptlrpc_request *req)
327 {
328         return mdt_handle_common(req, mdt_seq_handlers);
329 }
330
331 static int mds_fld_handle(struct ptlrpc_request *req)
332 {
333         return mdt_handle_common(req, mdt_fld_handlers);
334 }
335
336 /* device init/fini methods */
337 static void mds_stop_ptlrpc_service(struct mds_device *m)
338 {
339         ENTRY;
340         if (m->mds_regular_service != NULL) {
341                 ptlrpc_unregister_service(m->mds_regular_service);
342                 m->mds_regular_service = NULL;
343         }
344         if (m->mds_readpage_service != NULL) {
345                 ptlrpc_unregister_service(m->mds_readpage_service);
346                 m->mds_readpage_service = NULL;
347         }
348         if (m->mds_setattr_service != NULL) {
349                 ptlrpc_unregister_service(m->mds_setattr_service);
350                 m->mds_setattr_service = NULL;
351         }
352         if (m->mds_mdsc_service != NULL) {
353                 ptlrpc_unregister_service(m->mds_mdsc_service);
354                 m->mds_mdsc_service = NULL;
355         }
356         if (m->mds_mdss_service != NULL) {
357                 ptlrpc_unregister_service(m->mds_mdss_service);
358                 m->mds_mdss_service = NULL;
359         }
360         if (m->mds_fld_service != NULL) {
361                 ptlrpc_unregister_service(m->mds_fld_service);
362                 m->mds_fld_service = NULL;
363         }
364         EXIT;
365 }
366
367 static int mds_start_ptlrpc_service(struct mds_device *m)
368 {
369         static struct ptlrpc_service_conf conf;
370         struct obd_device *obd = m->mds_md_dev.md_lu_dev.ld_obd;
371         cfs_proc_dir_entry_t *procfs_entry;
372         int rc = 0;
373         ENTRY;
374
375         procfs_entry = obd->obd_proc_entry;
376         LASSERT(procfs_entry != NULL);
377
378         conf = (typeof(conf)) {
379                 .psc_name               = LUSTRE_MDT_NAME,
380                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
381                 .psc_buf                = {
382                         .bc_nbufs               = MDS_NBUFS,
383                         .bc_buf_size            = MDS_BUFSIZE,
384                         .bc_req_max_size        = MDS_MAXREQSIZE,
385                         .bc_rep_max_size        = MDS_MAXREPSIZE,
386                         .bc_req_portal          = MDS_REQUEST_PORTAL,
387                         .bc_rep_portal          = MDC_REPLY_PORTAL,
388                 },
389                 /*
390                  * We'd like to have a mechanism to set this on a per-device
391                  * basis, but alas...
392                  */
393                 .psc_thr                = {
394                         .tc_thr_name            = LUSTRE_MDT_NAME,
395                         .tc_thr_factor          = MDS_THR_FACTOR,
396                         .tc_nthrs_init          = MDS_NTHRS_INIT,
397                         .tc_nthrs_base          = MDS_NTHRS_BASE,
398                         .tc_nthrs_max           = MDS_NTHRS_MAX,
399                         .tc_nthrs_user          = mds_num_threads,
400                         .tc_cpu_affinity        = 1,
401                         .tc_ctx_tags            = LCT_MD_THREAD,
402                 },
403                 .psc_cpt                = {
404                         .cc_pattern             = mds_num_cpts,
405                 },
406                 .psc_ops                = {
407                         .so_req_handler         = mds_regular_handle,
408                         .so_req_printer         = target_print_req,
409                         .so_hpreq_handler       = ptlrpc_hpreq_handler,
410                 },
411         };
412         m->mds_regular_service = ptlrpc_register_service(&conf, procfs_entry);
413         if (IS_ERR(m->mds_regular_service)) {
414                 rc = PTR_ERR(m->mds_regular_service);
415                 CERROR("failed to start regular mdt service: %d\n", rc);
416                 m->mds_regular_service = NULL;
417
418                 RETURN(rc);
419         }
420
421         /*
422          * readpage service configuration. Parameters have to be adjusted,
423          * ideally.
424          */
425         memset(&conf, 0, sizeof(conf));
426         conf = (typeof(conf)) {
427                 .psc_name               = LUSTRE_MDT_NAME "_readpage",
428                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
429                 .psc_buf                = {
430                         .bc_nbufs               = MDS_NBUFS,
431                         .bc_buf_size            = MDS_BUFSIZE,
432                         .bc_req_max_size        = MDS_MAXREQSIZE,
433                         .bc_rep_max_size        = MDS_MAXREPSIZE,
434                         .bc_req_portal          = MDS_READPAGE_PORTAL,
435                         .bc_rep_portal          = MDC_REPLY_PORTAL,
436                 },
437                 .psc_thr                = {
438                         .tc_thr_name            = LUSTRE_MDT_NAME "_rdpg",
439                         .tc_thr_factor          = MDS_RDPG_THR_FACTOR,
440                         .tc_nthrs_init          = MDS_RDPG_NTHRS_INIT,
441                         .tc_nthrs_base          = MDS_RDPG_NTHRS_BASE,
442                         .tc_nthrs_max           = MDS_RDPG_NTHRS_MAX,
443                         .tc_nthrs_user          = mds_rdpg_num_threads,
444                         .tc_cpu_affinity        = 1,
445                         .tc_ctx_tags            = LCT_MD_THREAD,
446                 },
447                 .psc_cpt                = {
448                         .cc_pattern             = mds_rdpg_num_cpts,
449                 },
450                 .psc_ops                = {
451                         .so_req_handler         = mds_readpage_handle,
452                         .so_req_printer         = target_print_req,
453                 },
454         };
455         m->mds_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
456         if (IS_ERR(m->mds_readpage_service)) {
457                 rc = PTR_ERR(m->mds_readpage_service);
458                 CERROR("failed to start readpage service: %d\n", rc);
459                 m->mds_readpage_service = NULL;
460
461                 GOTO(err_mds_svc, rc);
462         }
463
464         /*
465          * setattr service configuration.
466          *
467          * XXX To keep the compatibility with old client(< 2.2), we need to
468          * preserve this portal for a certain time, it should be removed
469          * eventually. LU-617.
470          */
471         memset(&conf, 0, sizeof(conf));
472         conf = (typeof(conf)) {
473                 .psc_name               = LUSTRE_MDT_NAME "_setattr",
474                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
475                 .psc_buf                = {
476                         .bc_nbufs               = MDS_NBUFS,
477                         .bc_buf_size            = MDS_BUFSIZE,
478                         .bc_req_max_size        = MDS_MAXREQSIZE,
479                         .bc_rep_max_size        = MDS_MAXREPSIZE,
480                         .bc_req_portal          = MDS_SETATTR_PORTAL,
481                         .bc_rep_portal          = MDC_REPLY_PORTAL,
482                 },
483                 .psc_thr                = {
484                         .tc_thr_name            = LUSTRE_MDT_NAME "_attr",
485                         .tc_thr_factor          = MDS_SETA_THR_FACTOR,
486                         .tc_nthrs_init          = MDS_SETA_NTHRS_INIT,
487                         .tc_nthrs_base          = MDS_SETA_NTHRS_BASE,
488                         .tc_nthrs_max           = MDS_SETA_NTHRS_MAX,
489                         .tc_nthrs_user          = mds_attr_num_threads,
490                         .tc_cpu_affinity        = 1,
491                         .tc_ctx_tags            = LCT_MD_THREAD,
492                 },
493                 .psc_cpt                = {
494                         .cc_pattern             = mds_attr_num_cpts,
495                 },
496                 .psc_ops                = {
497                         .so_req_handler         = mds_regular_handle,
498                         .so_req_printer         = target_print_req,
499                         .so_hpreq_handler       = NULL,
500                 },
501         };
502         m->mds_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
503         if (IS_ERR(m->mds_setattr_service)) {
504                 rc = PTR_ERR(m->mds_setattr_service);
505                 CERROR("failed to start setattr service: %d\n", rc);
506                 m->mds_setattr_service = NULL;
507
508                 GOTO(err_mds_svc, rc);
509         }
510
511         /*
512          * sequence controller service configuration
513          */
514         memset(&conf, 0, sizeof(conf));
515         conf = (typeof(conf)) {
516                 .psc_name               = LUSTRE_MDT_NAME "_seqs",
517                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
518                 .psc_buf                = {
519                         .bc_nbufs               = MDS_NBUFS,
520                         .bc_buf_size            = MDS_BUFSIZE,
521                         .bc_req_max_size        = SEQ_MAXREQSIZE,
522                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
523                         .bc_req_portal          = SEQ_CONTROLLER_PORTAL,
524                         .bc_rep_portal          = MDC_REPLY_PORTAL,
525                 },
526                 .psc_thr                = {
527                         .tc_thr_name            = LUSTRE_MDT_NAME "_seqs",
528                         .tc_nthrs_init          = MDS_OTHR_NTHRS_INIT,
529                         .tc_nthrs_max           = MDS_OTHR_NTHRS_MAX,
530                         .tc_ctx_tags            = LCT_MD_THREAD,
531                 },
532                 .psc_ops                = {
533                         .so_req_handler         = mds_mdsc_handle,
534                         .so_req_printer         = target_print_req,
535                         .so_hpreq_handler       = NULL,
536                 },
537         };
538         m->mds_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
539         if (IS_ERR(m->mds_mdsc_service)) {
540                 rc = PTR_ERR(m->mds_mdsc_service);
541                 CERROR("failed to start seq controller service: %d\n", rc);
542                 m->mds_mdsc_service = NULL;
543
544                 GOTO(err_mds_svc, rc);
545         }
546
547         /*
548          * metadata sequence server service configuration
549          */
550         memset(&conf, 0, sizeof(conf));
551         conf = (typeof(conf)) {
552                 .psc_name               = LUSTRE_MDT_NAME "_seqm",
553                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
554                 .psc_buf                = {
555                         .bc_nbufs               = MDS_NBUFS,
556                         .bc_buf_size            = MDS_BUFSIZE,
557                         .bc_req_max_size        = SEQ_MAXREQSIZE,
558                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
559                         .bc_req_portal          = SEQ_METADATA_PORTAL,
560                         .bc_rep_portal          = MDC_REPLY_PORTAL,
561                 },
562                 .psc_thr                = {
563                         .tc_thr_name            = LUSTRE_MDT_NAME "_seqm",
564                         .tc_nthrs_init          = MDS_OTHR_NTHRS_INIT,
565                         .tc_nthrs_max           = MDS_OTHR_NTHRS_MAX,
566                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
567                 },
568                 .psc_ops                = {
569                         .so_req_handler         = mds_mdss_handle,
570                         .so_req_printer         = target_print_req,
571                         .so_hpreq_handler       = NULL,
572                 },
573         };
574         m->mds_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
575         if (IS_ERR(m->mds_mdss_service)) {
576                 rc = PTR_ERR(m->mds_mdss_service);
577                 CERROR("failed to start metadata seq server service: %d\n", rc);
578                 m->mds_mdss_service = NULL;
579
580                 GOTO(err_mds_svc, rc);
581         }
582
583         /* FLD service start */
584         memset(&conf, 0, sizeof(conf));
585         conf = (typeof(conf)) {
586                 .psc_name            = LUSTRE_MDT_NAME "_fld",
587                 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
588                 .psc_buf                = {
589                         .bc_nbufs               = MDS_NBUFS,
590                         .bc_buf_size            = MDS_BUFSIZE,
591                         .bc_req_max_size        = FLD_MAXREQSIZE,
592                         .bc_rep_max_size        = FLD_MAXREPSIZE,
593                         .bc_req_portal          = FLD_REQUEST_PORTAL,
594                         .bc_rep_portal          = MDC_REPLY_PORTAL,
595                 },
596                 .psc_thr                = {
597                         .tc_thr_name            = LUSTRE_MDT_NAME "_fld",
598                         .tc_nthrs_init          = MDS_OTHR_NTHRS_INIT,
599                         .tc_nthrs_max           = MDS_OTHR_NTHRS_MAX,
600                         .tc_ctx_tags            = LCT_DT_THREAD | LCT_MD_THREAD
601                 },
602                 .psc_ops                = {
603                         .so_req_handler         = mds_fld_handle,
604                         .so_req_printer         = target_print_req,
605                         .so_hpreq_handler       = NULL,
606                 },
607         };
608         m->mds_fld_service = ptlrpc_register_service(&conf, procfs_entry);
609         if (IS_ERR(m->mds_fld_service)) {
610                 rc = PTR_ERR(m->mds_fld_service);
611                 CERROR("failed to start fld service: %d\n", rc);
612                 m->mds_fld_service = NULL;
613
614                 GOTO(err_mds_svc, rc);
615         }
616
617         EXIT;
618 err_mds_svc:
619         if (rc)
620                 mds_stop_ptlrpc_service(m);
621
622         return rc;
623 }
624
625 static inline struct mds_device *mds_dev(struct lu_device *d)
626 {
627         return container_of0(d, struct mds_device, mds_md_dev.md_lu_dev);
628 }
629
630 static struct lu_device *mds_device_fini(const struct lu_env *env,
631                                          struct lu_device *d)
632 {
633         struct mds_device *m = mds_dev(d);
634         struct obd_device *obd = d->ld_obd;
635         ENTRY;
636
637         mds_stop_ptlrpc_service(m);
638         lprocfs_obd_cleanup(obd);
639         RETURN(NULL);
640 }
641
642 static struct lu_device *mds_device_free(const struct lu_env *env,
643                                          struct lu_device *d)
644 {
645         struct mds_device *m = mds_dev(d);
646         ENTRY;
647
648         md_device_fini(&m->mds_md_dev);
649         OBD_FREE_PTR(m);
650         RETURN(NULL);
651 }
652
653 static struct lu_device *mds_device_alloc(const struct lu_env *env,
654                                           struct lu_device_type *t,
655                                           struct lustre_cfg *cfg)
656 {
657         struct mds_device        *m;
658         struct obd_device        *obd;
659         struct lu_device          *l;
660         int rc;
661
662         OBD_ALLOC_PTR(m);
663         if (m == NULL)
664                 return ERR_PTR(-ENOMEM);
665
666         md_device_init(&m->mds_md_dev, t);
667         l = &m->mds_md_dev.md_lu_dev;
668
669         obd = class_name2obd(lustre_cfg_string(cfg, 0));
670         LASSERT(obd != NULL);
671
672         l->ld_obd = obd;
673         /* set this lu_device to obd, because error handling need it */
674         obd->obd_lu_dev = l;
675
676         rc = lprocfs_obd_setup(obd, lprocfs_mds_obd_vars);
677         if (rc != 0) {
678                 mds_device_free(env, l);
679                 l = ERR_PTR(rc);
680                 return l;
681         }
682
683         rc = mds_start_ptlrpc_service(m);
684
685         if (rc != 0) {
686                 mds_device_free(env, l);
687                 l = ERR_PTR(rc);
688                 return l;
689         }
690
691         return l;
692 }
693
694 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
695 LU_TYPE_INIT_FINI(mds, &mdt_thread_key);
696
697 static struct lu_device_type_operations mds_device_type_ops = {
698         .ldto_init = mds_type_init,
699         .ldto_fini = mds_type_fini,
700
701         .ldto_start = mds_type_start,
702         .ldto_stop  = mds_type_stop,
703
704         .ldto_device_alloc = mds_device_alloc,
705         .ldto_device_free  = mds_device_free,
706         .ldto_device_fini  = mds_device_fini
707 };
708
709 static struct lu_device_type mds_device_type = {
710         .ldt_tags     = LU_DEVICE_MD,
711         .ldt_name     = LUSTRE_MDS_NAME,
712         .ldt_ops      = &mds_device_type_ops,
713         .ldt_ctx_tags = LCT_MD_THREAD
714 };
715
716 static struct obd_ops mds_obd_device_ops = {
717         .o_owner           = THIS_MODULE,
718 };
719
720 int mds_mod_init(void)
721 {
722         int rc;
723
724         if (mdt_num_threads != 0 && mds_num_threads == 0) {
725                 LCONSOLE_INFO("mdt_num_threads module parameter is deprecated, "
726                               "use mds_num_threads instead or unset both for "
727                               "dynamic thread startup\n");
728                 mds_num_threads = mdt_num_threads;
729         }
730
731         rc = class_register_type(&mds_obd_device_ops, NULL,
732                                  lprocfs_mds_module_vars, LUSTRE_MDS_NAME,
733                                  &mds_device_type);
734         return rc;
735 }
736
737 void mds_mod_exit(void)
738 {
739         class_unregister_type(LUSTRE_MDS_NAME);
740 }