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