Whamcloud - gitweb
49a87dd63a12f13ec21cb61707a8db5a9bc2af19
[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 };
155
156 #define DEF_OBD_HDL(flags, name, fn)                                    \
157         DEFINE_RPC_HANDLER(OBD_PING, flags, name, fn, NULL)
158
159 static struct mdt_handler mdt_obd_ops[] = {
160 DEF_OBD_HDL(0,                          OBD_PING,         mdt_obd_ping),
161 DEF_OBD_HDL(0,                          OBD_LOG_CANCEL,   mdt_obd_log_cancel),
162 DEF_OBD_HDL(0,                          OBD_QC_CALLBACK,  mdt_obd_qc_callback),
163 DEF_OBD_HDL(0,                          OBD_IDX_READ,     mdt_obd_idx_read)
164 };
165
166 #define DEF_DLM_HDL_VAR(flags, name, fn)                                \
167         DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, NULL)
168 #define DEF_DLM_HDL(flags, name, fn)                                    \
169         DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, &RQF_ ## name)
170
171 static struct mdt_handler mdt_dlm_ops[] = {
172 DEF_DLM_HDL    (HABEO_CLAVIS,           LDLM_ENQUEUE,     mdt_enqueue),
173 DEF_DLM_HDL_VAR(HABEO_CLAVIS,           LDLM_CONVERT,     mdt_convert),
174 DEF_DLM_HDL_VAR(0,                      LDLM_BL_CALLBACK, mdt_bl_callback),
175 DEF_DLM_HDL_VAR(0,                      LDLM_CP_CALLBACK, mdt_cp_callback)
176 };
177
178 #define DEF_LLOG_HDL(flags, name, fn)                                   \
179         DEFINE_RPC_HANDLER(LLOG_ORIGIN_HANDLE_CREATE, flags, name, fn, NULL)
180
181 static struct mdt_handler mdt_llog_ops[] = {
182 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_CREATE,        mdt_llog_create),
183 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_NEXT_BLOCK,    mdt_llog_next_block),
184 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_READ_HEADER,   mdt_llog_read_header),
185 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_WRITE_REC,     NULL),
186 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_CLOSE,         NULL),
187 DEF_LLOG_HDL(0,         LLOG_ORIGIN_CONNECT,              NULL),
188 DEF_LLOG_HDL(0,         LLOG_CATINFO,                     NULL),
189 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_PREV_BLOCK,    mdt_llog_prev_block),
190 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_DESTROY,       mdt_llog_destroy),
191 };
192
193 #define DEF_SEC_HDL(flags, name, fn)                                    \
194         DEFINE_RPC_HANDLER(SEC_CTX_INIT, flags, name, fn, NULL)
195
196 static struct mdt_handler mdt_sec_ctx_ops[] = {
197 DEF_SEC_HDL(0,                          SEC_CTX_INIT,     mdt_sec_ctx_handle),
198 DEF_SEC_HDL(0,                          SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
199 DEF_SEC_HDL(0,                          SEC_CTX_FINI,     mdt_sec_ctx_handle)
200 };
201
202 #define DEF_QUOTA_HDL(flags, name, fn)                          \
203         DEFINE_RPC_HANDLER(QUOTA_DQACQ, flags, name, fn, &RQF_ ## name)
204
205 static struct mdt_handler mdt_quota_ops[] = {
206 DEF_QUOTA_HDL(HABEO_REFERO,             QUOTA_DQACQ,      mdt_quota_dqacq),
207 };
208
209 struct mdt_opc_slice mdt_regular_handlers[] = {
210         {
211                 .mos_opc_start  = MDS_GETATTR,
212                 .mos_opc_end    = MDS_LAST_OPC,
213                 .mos_hs         = mdt_mds_ops
214         },
215         {
216                 .mos_opc_start  = OBD_PING,
217                 .mos_opc_end    = OBD_LAST_OPC,
218                 .mos_hs         = mdt_obd_ops
219         },
220         {
221                 .mos_opc_start  = LDLM_ENQUEUE,
222                 .mos_opc_end    = LDLM_LAST_OPC,
223                 .mos_hs         = mdt_dlm_ops
224         },
225         {
226                 .mos_opc_start  = LLOG_ORIGIN_HANDLE_CREATE,
227                 .mos_opc_end    = LLOG_LAST_OPC,
228                 .mos_hs         = mdt_llog_ops
229         },
230         {
231                 .mos_opc_start  = SEC_CTX_INIT,
232                 .mos_opc_end    = SEC_LAST_OPC,
233                 .mos_hs         = mdt_sec_ctx_ops
234         },
235         {
236                 .mos_opc_start  = QUOTA_DQACQ,
237                 .mos_opc_end    = QUOTA_LAST_OPC,
238                 .mos_hs         = mdt_quota_ops
239         },
240         {
241                 .mos_hs         = NULL
242         }
243 };
244
245 /* Readpage/readdir handlers */
246 static struct mdt_handler mdt_readpage_ops[] = {
247 DEF_MDT_HDL(0,                  MDS_CONNECT,  mdt_connect),
248 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO, MDS_READPAGE, mdt_readpage),
249 /* XXX: this is ugly and should be fixed one day, see mdc_close() for
250  * detailed comments. --umka */
251 DEF_MDT_HDL(HABEO_CORPUS,               MDS_CLOSE,        mdt_close),
252 DEF_MDT_HDL(HABEO_CORPUS,               MDS_DONE_WRITING, mdt_done_writing),
253 };
254
255 static struct mdt_opc_slice mdt_readpage_handlers[] = {
256         {
257                 .mos_opc_start = MDS_GETATTR,
258                 .mos_opc_end   = MDS_LAST_OPC,
259                 .mos_hs = mdt_readpage_ops
260         },
261         {
262                 .mos_opc_start = OBD_FIRST_OPC,
263                 .mos_opc_end   = OBD_LAST_OPC,
264                 .mos_hs = mdt_obd_ops
265         },
266         {
267                 .mos_hs = NULL
268         }
269 };
270
271 /* Sequence service handlers */
272 #define DEF_SEQ_HDL(flags, name, fn)                                    \
273         DEFINE_RPC_HANDLER(SEQ_QUERY, flags, name, fn, &RQF_ ## name)
274
275 static struct mdt_handler mdt_seq_ops[] = {
276 DEF_SEQ_HDL(0,                          SEQ_QUERY,        (void *)seq_query),
277 };
278
279 struct mdt_opc_slice mdt_seq_handlers[] = {
280         {
281                 .mos_opc_start = SEQ_QUERY,
282                 .mos_opc_end   = SEQ_LAST_OPC,
283                 .mos_hs = mdt_seq_ops
284         },
285         {
286                 .mos_hs = NULL
287         }
288 };
289
290 /* FID Location Database handlers */
291 #define DEF_FLD_HDL(flags, name, fn)                                    \
292         DEFINE_RPC_HANDLER(FLD_QUERY, flags, name, fn, &RQF_ ## name)
293
294 static struct mdt_handler mdt_fld_ops[] = {
295 DEF_FLD_HDL(0,                          FLD_QUERY,        (void *)fld_query),
296 };
297
298 struct mdt_opc_slice mdt_fld_handlers[] = {
299         {
300                 .mos_opc_start = FLD_QUERY,
301                 .mos_opc_end   = FLD_LAST_OPC,
302                 .mos_hs = mdt_fld_ops
303         },
304         {
305                 .mos_hs = NULL
306         }
307 };
308
309 static int mds_regular_handle(struct ptlrpc_request *req)
310 {
311         return mdt_handle_common(req, mdt_regular_handlers);
312 }
313
314 static int mds_readpage_handle(struct ptlrpc_request *req)
315 {
316         return mdt_handle_common(req, mdt_readpage_handlers);
317 }
318
319 static int mds_mdsc_handle(struct ptlrpc_request *req)
320 {
321         return mdt_handle_common(req, mdt_seq_handlers);
322 }
323
324 static int mds_mdss_handle(struct ptlrpc_request *req)
325 {
326         return mdt_handle_common(req, mdt_seq_handlers);
327 }
328
329 static int mds_fld_handle(struct ptlrpc_request *req)
330 {
331         return mdt_handle_common(req, mdt_fld_handlers);
332 }
333
334 /* device init/fini methods */
335 static void mds_stop_ptlrpc_service(struct mds_device *m)
336 {
337         ENTRY;
338         if (m->mds_regular_service != NULL) {
339                 ptlrpc_unregister_service(m->mds_regular_service);
340                 m->mds_regular_service = NULL;
341         }
342         if (m->mds_readpage_service != NULL) {
343                 ptlrpc_unregister_service(m->mds_readpage_service);
344                 m->mds_readpage_service = NULL;
345         }
346         if (m->mds_setattr_service != NULL) {
347                 ptlrpc_unregister_service(m->mds_setattr_service);
348                 m->mds_setattr_service = NULL;
349         }
350         if (m->mds_mdsc_service != NULL) {
351                 ptlrpc_unregister_service(m->mds_mdsc_service);
352                 m->mds_mdsc_service = NULL;
353         }
354         if (m->mds_mdss_service != NULL) {
355                 ptlrpc_unregister_service(m->mds_mdss_service);
356                 m->mds_mdss_service = NULL;
357         }
358         if (m->mds_fld_service != NULL) {
359                 ptlrpc_unregister_service(m->mds_fld_service);
360                 m->mds_fld_service = NULL;
361         }
362         EXIT;
363 }
364
365 static int mds_start_ptlrpc_service(struct mds_device *m)
366 {
367         static struct ptlrpc_service_conf conf;
368         struct obd_device *obd = m->mds_md_dev.md_lu_dev.ld_obd;
369         cfs_proc_dir_entry_t *procfs_entry;
370         int rc = 0;
371         ENTRY;
372
373         procfs_entry = obd->obd_proc_entry;
374         LASSERT(procfs_entry != NULL);
375
376         conf = (typeof(conf)) {
377                 .psc_name               = LUSTRE_MDT_NAME,
378                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
379                 .psc_buf                = {
380                         .bc_nbufs               = MDS_NBUFS,
381                         .bc_buf_size            = MDS_BUFSIZE,
382                         .bc_req_max_size        = MDS_MAXREQSIZE,
383                         .bc_rep_max_size        = MDS_MAXREPSIZE,
384                         .bc_req_portal          = MDS_REQUEST_PORTAL,
385                         .bc_rep_portal          = MDC_REPLY_PORTAL,
386                 },
387                 /*
388                  * We'd like to have a mechanism to set this on a per-device
389                  * basis, but alas...
390                  */
391                 .psc_thr                = {
392                         .tc_thr_name            = LUSTRE_MDT_NAME,
393                         .tc_thr_factor          = MDS_THR_FACTOR,
394                         .tc_nthrs_init          = MDS_NTHRS_INIT,
395                         .tc_nthrs_base          = MDS_NTHRS_BASE,
396                         .tc_nthrs_max           = MDS_NTHRS_MAX,
397                         .tc_nthrs_user          = mds_num_threads,
398                         .tc_cpu_affinity        = 1,
399                         .tc_ctx_tags            = LCT_MD_THREAD,
400                 },
401                 .psc_cpt                = {
402                         .cc_pattern             = mds_num_cpts,
403                 },
404                 .psc_ops                = {
405                         .so_req_handler         = mds_regular_handle,
406                         .so_req_printer         = target_print_req,
407                         .so_hpreq_handler       = ptlrpc_hpreq_handler,
408                 },
409         };
410         m->mds_regular_service = ptlrpc_register_service(&conf, procfs_entry);
411         if (IS_ERR(m->mds_regular_service)) {
412                 rc = PTR_ERR(m->mds_regular_service);
413                 CERROR("failed to start regular mdt service: %d\n", rc);
414                 m->mds_regular_service = NULL;
415
416                 RETURN(rc);
417         }
418
419         /*
420          * readpage service configuration. Parameters have to be adjusted,
421          * ideally.
422          */
423         memset(&conf, 0, sizeof(conf));
424         conf = (typeof(conf)) {
425                 .psc_name               = LUSTRE_MDT_NAME "_readpage",
426                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
427                 .psc_buf                = {
428                         .bc_nbufs               = MDS_NBUFS,
429                         .bc_buf_size            = MDS_BUFSIZE,
430                         .bc_req_max_size        = MDS_MAXREQSIZE,
431                         .bc_rep_max_size        = MDS_MAXREPSIZE,
432                         .bc_req_portal          = MDS_READPAGE_PORTAL,
433                         .bc_rep_portal          = MDC_REPLY_PORTAL,
434                 },
435                 .psc_thr                = {
436                         .tc_thr_name            = LUSTRE_MDT_NAME "_rdpg",
437                         .tc_thr_factor          = MDS_RDPG_THR_FACTOR,
438                         .tc_nthrs_init          = MDS_RDPG_NTHRS_INIT,
439                         .tc_nthrs_base          = MDS_RDPG_NTHRS_BASE,
440                         .tc_nthrs_max           = MDS_RDPG_NTHRS_MAX,
441                         .tc_nthrs_user          = mds_rdpg_num_threads,
442                         .tc_cpu_affinity        = 1,
443                         .tc_ctx_tags            = LCT_MD_THREAD,
444                 },
445                 .psc_cpt                = {
446                         .cc_pattern             = mds_rdpg_num_cpts,
447                 },
448                 .psc_ops                = {
449                         .so_req_handler         = mds_readpage_handle,
450                         .so_req_printer         = target_print_req,
451                 },
452         };
453         m->mds_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
454         if (IS_ERR(m->mds_readpage_service)) {
455                 rc = PTR_ERR(m->mds_readpage_service);
456                 CERROR("failed to start readpage service: %d\n", rc);
457                 m->mds_readpage_service = NULL;
458
459                 GOTO(err_mds_svc, rc);
460         }
461
462         /*
463          * setattr service configuration.
464          *
465          * XXX To keep the compatibility with old client(< 2.2), we need to
466          * preserve this portal for a certain time, it should be removed
467          * eventually. LU-617.
468          */
469         memset(&conf, 0, sizeof(conf));
470         conf = (typeof(conf)) {
471                 .psc_name               = LUSTRE_MDT_NAME "_setattr",
472                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
473                 .psc_buf                = {
474                         .bc_nbufs               = MDS_NBUFS,
475                         .bc_buf_size            = MDS_BUFSIZE,
476                         .bc_req_max_size        = MDS_MAXREQSIZE,
477                         .bc_rep_max_size        = MDS_MAXREPSIZE,
478                         .bc_req_portal          = MDS_SETATTR_PORTAL,
479                         .bc_rep_portal          = MDC_REPLY_PORTAL,
480                 },
481                 .psc_thr                = {
482                         .tc_thr_name            = LUSTRE_MDT_NAME "_attr",
483                         .tc_thr_factor          = MDS_SETA_THR_FACTOR,
484                         .tc_nthrs_init          = MDS_SETA_NTHRS_INIT,
485                         .tc_nthrs_base          = MDS_SETA_NTHRS_BASE,
486                         .tc_nthrs_max           = MDS_SETA_NTHRS_MAX,
487                         .tc_nthrs_user          = mds_attr_num_threads,
488                         .tc_cpu_affinity        = 1,
489                         .tc_ctx_tags            = LCT_MD_THREAD,
490                 },
491                 .psc_cpt                = {
492                         .cc_pattern             = mds_attr_num_cpts,
493                 },
494                 .psc_ops                = {
495                         .so_req_handler         = mds_regular_handle,
496                         .so_req_printer         = target_print_req,
497                         .so_hpreq_handler       = NULL,
498                 },
499         };
500         m->mds_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
501         if (IS_ERR(m->mds_setattr_service)) {
502                 rc = PTR_ERR(m->mds_setattr_service);
503                 CERROR("failed to start setattr service: %d\n", rc);
504                 m->mds_setattr_service = NULL;
505
506                 GOTO(err_mds_svc, rc);
507         }
508
509         /*
510          * sequence controller service configuration
511          */
512         memset(&conf, 0, sizeof(conf));
513         conf = (typeof(conf)) {
514                 .psc_name               = LUSTRE_MDT_NAME "_seqs",
515                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
516                 .psc_buf                = {
517                         .bc_nbufs               = MDS_NBUFS,
518                         .bc_buf_size            = MDS_BUFSIZE,
519                         .bc_req_max_size        = SEQ_MAXREQSIZE,
520                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
521                         .bc_req_portal          = SEQ_CONTROLLER_PORTAL,
522                         .bc_rep_portal          = MDC_REPLY_PORTAL,
523                 },
524                 .psc_thr                = {
525                         .tc_thr_name            = LUSTRE_MDT_NAME "_seqs",
526                         .tc_nthrs_init          = MDS_OTHR_NTHRS_INIT,
527                         .tc_nthrs_max           = MDS_OTHR_NTHRS_MAX,
528                         .tc_ctx_tags            = LCT_MD_THREAD,
529                 },
530                 .psc_ops                = {
531                         .so_req_handler         = mds_mdsc_handle,
532                         .so_req_printer         = target_print_req,
533                         .so_hpreq_handler       = NULL,
534                 },
535         };
536         m->mds_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
537         if (IS_ERR(m->mds_mdsc_service)) {
538                 rc = PTR_ERR(m->mds_mdsc_service);
539                 CERROR("failed to start seq controller service: %d\n", rc);
540                 m->mds_mdsc_service = NULL;
541
542                 GOTO(err_mds_svc, rc);
543         }
544
545         /*
546          * metadata sequence server service configuration
547          */
548         memset(&conf, 0, sizeof(conf));
549         conf = (typeof(conf)) {
550                 .psc_name               = LUSTRE_MDT_NAME "_seqm",
551                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
552                 .psc_buf                = {
553                         .bc_nbufs               = MDS_NBUFS,
554                         .bc_buf_size            = MDS_BUFSIZE,
555                         .bc_req_max_size        = SEQ_MAXREQSIZE,
556                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
557                         .bc_req_portal          = SEQ_METADATA_PORTAL,
558                         .bc_rep_portal          = MDC_REPLY_PORTAL,
559                 },
560                 .psc_thr                = {
561                         .tc_thr_name            = LUSTRE_MDT_NAME "_seqm",
562                         .tc_nthrs_init          = MDS_OTHR_NTHRS_INIT,
563                         .tc_nthrs_max           = MDS_OTHR_NTHRS_MAX,
564                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
565                 },
566                 .psc_ops                = {
567                         .so_req_handler         = mds_mdss_handle,
568                         .so_req_printer         = target_print_req,
569                         .so_hpreq_handler       = NULL,
570                 },
571         };
572         m->mds_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
573         if (IS_ERR(m->mds_mdss_service)) {
574                 rc = PTR_ERR(m->mds_mdss_service);
575                 CERROR("failed to start metadata seq server service: %d\n", rc);
576                 m->mds_mdss_service = NULL;
577
578                 GOTO(err_mds_svc, rc);
579         }
580
581         /* FLD service start */
582         memset(&conf, 0, sizeof(conf));
583         conf = (typeof(conf)) {
584                 .psc_name            = LUSTRE_MDT_NAME "_fld",
585                 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
586                 .psc_buf                = {
587                         .bc_nbufs               = MDS_NBUFS,
588                         .bc_buf_size            = MDS_BUFSIZE,
589                         .bc_req_max_size        = FLD_MAXREQSIZE,
590                         .bc_rep_max_size        = FLD_MAXREPSIZE,
591                         .bc_req_portal          = FLD_REQUEST_PORTAL,
592                         .bc_rep_portal          = MDC_REPLY_PORTAL,
593                 },
594                 .psc_thr                = {
595                         .tc_thr_name            = LUSTRE_MDT_NAME "_fld",
596                         .tc_nthrs_init          = MDS_OTHR_NTHRS_INIT,
597                         .tc_nthrs_max           = MDS_OTHR_NTHRS_MAX,
598                         .tc_ctx_tags            = LCT_DT_THREAD | LCT_MD_THREAD
599                 },
600                 .psc_ops                = {
601                         .so_req_handler         = mds_fld_handle,
602                         .so_req_printer         = target_print_req,
603                         .so_hpreq_handler       = NULL,
604                 },
605         };
606         m->mds_fld_service = ptlrpc_register_service(&conf, procfs_entry);
607         if (IS_ERR(m->mds_fld_service)) {
608                 rc = PTR_ERR(m->mds_fld_service);
609                 CERROR("failed to start fld service: %d\n", rc);
610                 m->mds_fld_service = NULL;
611
612                 GOTO(err_mds_svc, rc);
613         }
614
615         EXIT;
616 err_mds_svc:
617         if (rc)
618                 mds_stop_ptlrpc_service(m);
619
620         return rc;
621 }
622
623 static inline struct mds_device *mds_dev(struct lu_device *d)
624 {
625         return container_of0(d, struct mds_device, mds_md_dev.md_lu_dev);
626 }
627
628 static struct lu_device *mds_device_fini(const struct lu_env *env,
629                                          struct lu_device *d)
630 {
631         struct mds_device *m = mds_dev(d);
632         struct obd_device *obd = d->ld_obd;
633         ENTRY;
634
635         mds_stop_ptlrpc_service(m);
636         lprocfs_obd_cleanup(obd);
637         RETURN(NULL);
638 }
639
640 static struct lu_device *mds_device_free(const struct lu_env *env,
641                                          struct lu_device *d)
642 {
643         struct mds_device *m = mds_dev(d);
644         ENTRY;
645
646         md_device_fini(&m->mds_md_dev);
647         OBD_FREE_PTR(m);
648         RETURN(NULL);
649 }
650
651 static struct lu_device *mds_device_alloc(const struct lu_env *env,
652                                           struct lu_device_type *t,
653                                           struct lustre_cfg *cfg)
654 {
655         struct mds_device        *m;
656         struct obd_device        *obd;
657         struct lu_device          *l;
658         int rc;
659
660         OBD_ALLOC_PTR(m);
661         if (m == NULL)
662                 return ERR_PTR(-ENOMEM);
663
664         md_device_init(&m->mds_md_dev, t);
665         l = &m->mds_md_dev.md_lu_dev;
666
667         obd = class_name2obd(lustre_cfg_string(cfg, 0));
668         LASSERT(obd != NULL);
669
670         l->ld_obd = obd;
671         /* set this lu_device to obd, because error handling need it */
672         obd->obd_lu_dev = l;
673
674         rc = lprocfs_obd_setup(obd, lprocfs_mds_obd_vars);
675         if (rc != 0) {
676                 mds_device_free(env, l);
677                 l = ERR_PTR(rc);
678                 return l;
679         }
680
681         rc = mds_start_ptlrpc_service(m);
682
683         if (rc != 0) {
684                 mds_device_free(env, l);
685                 l = ERR_PTR(rc);
686                 return l;
687         }
688
689         return l;
690 }
691
692 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
693 LU_TYPE_INIT_FINI(mds, &mdt_thread_key);
694
695 static struct lu_device_type_operations mds_device_type_ops = {
696         .ldto_init = mds_type_init,
697         .ldto_fini = mds_type_fini,
698
699         .ldto_start = mds_type_start,
700         .ldto_stop  = mds_type_stop,
701
702         .ldto_device_alloc = mds_device_alloc,
703         .ldto_device_free  = mds_device_free,
704         .ldto_device_fini  = mds_device_fini
705 };
706
707 static struct lu_device_type mds_device_type = {
708         .ldt_tags     = LU_DEVICE_MD,
709         .ldt_name     = LUSTRE_MDS_NAME,
710         .ldt_ops      = &mds_device_type_ops,
711         .ldt_ctx_tags = LCT_MD_THREAD
712 };
713
714 static struct obd_ops mds_obd_device_ops = {
715         .o_owner           = THIS_MODULE,
716 };
717
718 int mds_mod_init(void)
719 {
720         int rc;
721
722         if (mdt_num_threads != 0 && mds_num_threads == 0) {
723                 LCONSOLE_INFO("mdt_num_threads module parameter is deprecated, "
724                               "use mds_num_threads instead or unset both for "
725                               "dynamic thread startup\n");
726                 mds_num_threads = mdt_num_threads;
727         }
728
729         rc = class_register_type(&mds_obd_device_ops, NULL,
730                                  lprocfs_mds_module_vars, LUSTRE_MDS_NAME,
731                                  &mds_device_type);
732         return rc;
733 }
734
735 void mds_mod_exit(void)
736 {
737         class_unregister_type(LUSTRE_MDS_NAME);
738 }