Whamcloud - gitweb
LU-3866 hsm: permission checks on HSM operations
[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 "mdt_internal.h"
53 #include <lustre_quota.h>
54 #include <lustre_acl.h>
55 #include <lustre_param.h>
56
57 struct mds_device {
58         /* super-class */
59         struct md_device           mds_md_dev;
60         struct ptlrpc_service     *mds_regular_service;
61         struct ptlrpc_service     *mds_readpage_service;
62         struct ptlrpc_service     *mds_out_service;
63         struct ptlrpc_service     *mds_setattr_service;
64         struct ptlrpc_service     *mds_mdsc_service;
65         struct ptlrpc_service     *mds_mdss_service;
66         struct ptlrpc_service     *mds_fld_service;
67 };
68
69 /*
70  *  * Initialized in mdt_mod_init().
71  *   */
72 static unsigned long mdt_num_threads;
73 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
74                 "number of MDS service threads to start "
75                 "(deprecated in favor of mds_num_threads)");
76
77 static unsigned long mds_num_threads;
78 CFS_MODULE_PARM(mds_num_threads, "ul", ulong, 0444,
79                 "number of MDS service threads to start");
80
81 static char *mds_num_cpts;
82 CFS_MODULE_PARM(mds_num_cpts, "c", charp, 0444,
83                 "CPU partitions MDS threads should run on");
84
85 static unsigned long mds_rdpg_num_threads;
86 CFS_MODULE_PARM(mds_rdpg_num_threads, "ul", ulong, 0444,
87                 "number of MDS readpage service threads to start");
88
89 static char *mds_rdpg_num_cpts;
90 CFS_MODULE_PARM(mds_rdpg_num_cpts, "c", charp, 0444,
91                 "CPU partitions MDS readpage threads should run on");
92
93 /* NB: these two should be removed along with setattr service in the future */
94 static unsigned long mds_attr_num_threads;
95 CFS_MODULE_PARM(mds_attr_num_threads, "ul", ulong, 0444,
96                 "number of MDS setattr service threads to start");
97
98 static char *mds_attr_num_cpts;
99 CFS_MODULE_PARM(mds_attr_num_cpts, "c", charp, 0444,
100                 "CPU partitions MDS setattr threads should run on");
101
102 #define DEFINE_RPC_HANDLER(base, flags, opc, fn, fmt)                   \
103 [opc - base] = {                                                        \
104         .mh_name        = #opc,                                         \
105         .mh_fail_id     = OBD_FAIL_ ## opc ## _NET,                     \
106         .mh_opc         = opc,                                          \
107         .mh_flags       = flags,                                        \
108         .mh_act         = fn,                                           \
109         .mh_fmt         = fmt                                           \
110 }
111
112 /* Request with a format known in advance */
113 #define DEF_MDT_HDL(flags, name, fn)                                    \
114         DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, &RQF_ ## name)
115
116 /* Request with a format we do not yet know */
117 #define DEF_MDT_HDL_VAR(flags, name, fn)                                \
118         DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, NULL)
119
120 /* Map one non-standard request format handler.  This should probably get
121  * a common OBD_SET_INFO RPC opcode instead of this mismatch. */
122 #define RQF_MDS_SET_INFO RQF_OBD_SET_INFO
123
124 static struct mdt_handler mdt_mds_ops[] = {
125 DEF_MDT_HDL(0,                          MDS_CONNECT,      mdt_connect),
126 DEF_MDT_HDL(0,                          MDS_DISCONNECT,   mdt_disconnect),
127 DEF_MDT_HDL(0,                          MDS_SET_INFO,     mdt_set_info),
128 DEF_MDT_HDL(0,                          MDS_GET_INFO,     mdt_get_info),
129 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_GETSTATUS,    mdt_getstatus),
130 DEF_MDT_HDL(HABEO_CORPUS,               MDS_GETATTR,      mdt_getattr),
131 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME, mdt_getattr_name),
132 DEF_MDT_HDL(HABEO_CORPUS,               MDS_GETXATTR,     mdt_getxattr),
133 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_STATFS,       mdt_statfs),
134 DEF_MDT_HDL(0           | MUTABOR,      MDS_REINT,        mdt_reint),
135 DEF_MDT_HDL(HABEO_CORPUS,               MDS_CLOSE,        mdt_close),
136 DEF_MDT_HDL(HABEO_CORPUS,               MDS_DONE_WRITING, mdt_done_writing),
137 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_PIN,          mdt_pin),
138 DEF_MDT_HDL_VAR(0,                      MDS_SYNC,         mdt_sync),
139 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR,    mdt_is_subdir),
140 DEF_MDT_HDL(0,                          MDS_QUOTACHECK,   mdt_quotacheck),
141 DEF_MDT_HDL(0,                          MDS_QUOTACTL,     mdt_quotactl),
142 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
143             mdt_hsm_progress),
144 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
145             mdt_hsm_ct_register),
146 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
147             mdt_hsm_ct_unregister),
148 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO, MDS_HSM_STATE_GET, mdt_hsm_state_get),
149 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
150             mdt_hsm_state_set),
151 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
152 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO, MDS_HSM_REQUEST, mdt_hsm_request),
153 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO | MUTABOR, MDS_SWAP_LAYOUTS,
154             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 static int mds_regular_handle(struct ptlrpc_request *req)
273 {
274         return mdt_handle_common(req, mdt_regular_handlers);
275 }
276
277 static int mds_readpage_handle(struct ptlrpc_request *req)
278 {
279         return mdt_handle_common(req, mdt_readpage_handlers);
280 }
281
282 /* device init/fini methods */
283 static void mds_stop_ptlrpc_service(struct mds_device *m)
284 {
285         ENTRY;
286         if (m->mds_regular_service != NULL) {
287                 ptlrpc_unregister_service(m->mds_regular_service);
288                 m->mds_regular_service = NULL;
289         }
290         if (m->mds_readpage_service != NULL) {
291                 ptlrpc_unregister_service(m->mds_readpage_service);
292                 m->mds_readpage_service = NULL;
293         }
294         if (m->mds_out_service != NULL) {
295                 ptlrpc_unregister_service(m->mds_out_service);
296                 m->mds_out_service = NULL;
297         }
298         if (m->mds_setattr_service != NULL) {
299                 ptlrpc_unregister_service(m->mds_setattr_service);
300                 m->mds_setattr_service = NULL;
301         }
302         if (m->mds_mdsc_service != NULL) {
303                 ptlrpc_unregister_service(m->mds_mdsc_service);
304                 m->mds_mdsc_service = NULL;
305         }
306         if (m->mds_mdss_service != NULL) {
307                 ptlrpc_unregister_service(m->mds_mdss_service);
308                 m->mds_mdss_service = NULL;
309         }
310         if (m->mds_fld_service != NULL) {
311                 ptlrpc_unregister_service(m->mds_fld_service);
312                 m->mds_fld_service = NULL;
313         }
314         EXIT;
315 }
316
317 static int mds_start_ptlrpc_service(struct mds_device *m)
318 {
319         static struct ptlrpc_service_conf conf;
320         struct obd_device *obd = m->mds_md_dev.md_lu_dev.ld_obd;
321         cfs_proc_dir_entry_t *procfs_entry;
322         int rc = 0;
323         ENTRY;
324
325         procfs_entry = obd->obd_proc_entry;
326         LASSERT(procfs_entry != NULL);
327
328         conf = (typeof(conf)) {
329                 .psc_name               = LUSTRE_MDT_NAME,
330                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
331                 .psc_buf                = {
332                         .bc_nbufs               = MDS_NBUFS,
333                         .bc_buf_size            = MDS_REG_BUFSIZE,
334                         .bc_req_max_size        = MDS_REG_MAXREQSIZE,
335                         .bc_rep_max_size        = MDS_REG_MAXREPSIZE,
336                         .bc_req_portal          = MDS_REQUEST_PORTAL,
337                         .bc_rep_portal          = MDC_REPLY_PORTAL,
338                 },
339                 /*
340                  * We'd like to have a mechanism to set this on a per-device
341                  * basis, but alas...
342                  */
343                 .psc_thr                = {
344                         .tc_thr_name            = LUSTRE_MDT_NAME,
345                         .tc_thr_factor          = MDS_THR_FACTOR,
346                         .tc_nthrs_init          = MDS_NTHRS_INIT,
347                         .tc_nthrs_base          = MDS_NTHRS_BASE,
348                         .tc_nthrs_max           = MDS_NTHRS_MAX,
349                         .tc_nthrs_user          = mds_num_threads,
350                         .tc_cpu_affinity        = 1,
351                         .tc_ctx_tags            = LCT_MD_THREAD,
352                 },
353                 .psc_cpt                = {
354                         .cc_pattern             = mds_num_cpts,
355                 },
356                 .psc_ops                = {
357                         .so_req_handler         = mds_regular_handle,
358                         .so_req_printer         = target_print_req,
359                         .so_hpreq_handler       = ptlrpc_hpreq_handler,
360                 },
361         };
362         m->mds_regular_service = ptlrpc_register_service(&conf, procfs_entry);
363         if (IS_ERR(m->mds_regular_service)) {
364                 rc = PTR_ERR(m->mds_regular_service);
365                 CERROR("failed to start regular mdt service: %d\n", rc);
366                 m->mds_regular_service = NULL;
367
368                 RETURN(rc);
369         }
370
371         /*
372          * readpage service configuration. Parameters have to be adjusted,
373          * ideally.
374          */
375         memset(&conf, 0, sizeof(conf));
376         conf = (typeof(conf)) {
377                 .psc_name               = LUSTRE_MDT_NAME "_readpage",
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_READPAGE_PORTAL,
385                         .bc_rep_portal          = MDC_REPLY_PORTAL,
386                 },
387                 .psc_thr                = {
388                         .tc_thr_name            = LUSTRE_MDT_NAME "_rdpg",
389                         .tc_thr_factor          = MDS_RDPG_THR_FACTOR,
390                         .tc_nthrs_init          = MDS_RDPG_NTHRS_INIT,
391                         .tc_nthrs_base          = MDS_RDPG_NTHRS_BASE,
392                         .tc_nthrs_max           = MDS_RDPG_NTHRS_MAX,
393                         .tc_nthrs_user          = mds_rdpg_num_threads,
394                         .tc_cpu_affinity        = 1,
395                         .tc_ctx_tags            = LCT_MD_THREAD,
396                 },
397                 .psc_cpt                = {
398                         .cc_pattern             = mds_rdpg_num_cpts,
399                 },
400                 .psc_ops                = {
401                         .so_req_handler         = mds_readpage_handle,
402                         .so_req_printer         = target_print_req,
403                 },
404         };
405         m->mds_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
406         if (IS_ERR(m->mds_readpage_service)) {
407                 rc = PTR_ERR(m->mds_readpage_service);
408                 CERROR("failed to start readpage service: %d\n", rc);
409                 m->mds_readpage_service = NULL;
410
411                 GOTO(err_mds_svc, rc);
412         }
413
414         /*
415          * setattr service configuration.
416          *
417          * XXX To keep the compatibility with old client(< 2.2), we need to
418          * preserve this portal for a certain time, it should be removed
419          * eventually. LU-617.
420          */
421         memset(&conf, 0, sizeof(conf));
422         conf = (typeof(conf)) {
423                 .psc_name               = LUSTRE_MDT_NAME "_setattr",
424                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
425                 .psc_buf                = {
426                         .bc_nbufs               = MDS_NBUFS,
427                         .bc_buf_size            = MDS_BUFSIZE,
428                         .bc_req_max_size        = MDS_MAXREQSIZE,
429                         .bc_rep_max_size        = MDS_LOV_MAXREPSIZE,
430                         .bc_req_portal          = MDS_SETATTR_PORTAL,
431                         .bc_rep_portal          = MDC_REPLY_PORTAL,
432                 },
433                 .psc_thr                = {
434                         .tc_thr_name            = LUSTRE_MDT_NAME "_attr",
435                         .tc_thr_factor          = MDS_SETA_THR_FACTOR,
436                         .tc_nthrs_init          = MDS_SETA_NTHRS_INIT,
437                         .tc_nthrs_base          = MDS_SETA_NTHRS_BASE,
438                         .tc_nthrs_max           = MDS_SETA_NTHRS_MAX,
439                         .tc_nthrs_user          = mds_attr_num_threads,
440                         .tc_cpu_affinity        = 1,
441                         .tc_ctx_tags            = LCT_MD_THREAD,
442                 },
443                 .psc_cpt                = {
444                         .cc_pattern             = mds_attr_num_cpts,
445                 },
446                 .psc_ops                = {
447                         .so_req_handler         = mds_regular_handle,
448                         .so_req_printer         = target_print_req,
449                         .so_hpreq_handler       = NULL,
450                 },
451         };
452         m->mds_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
453         if (IS_ERR(m->mds_setattr_service)) {
454                 rc = PTR_ERR(m->mds_setattr_service);
455                 CERROR("failed to start setattr service: %d\n", rc);
456                 m->mds_setattr_service = NULL;
457
458                 GOTO(err_mds_svc, rc);
459         }
460
461         /* Object update service */
462         conf = (typeof(conf)) {
463                 .psc_name               = LUSTRE_MDT_NAME "_out",
464                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
465                 .psc_buf                = {
466                         .bc_nbufs               = MDS_NBUFS,
467                         .bc_buf_size            = OUT_BUFSIZE,
468                         .bc_req_max_size        = OUT_MAXREQSIZE,
469                         .bc_rep_max_size        = OUT_MAXREPSIZE,
470                         .bc_req_portal          = OUT_PORTAL,
471                         .bc_rep_portal          = MDC_REPLY_PORTAL,
472                 },
473                 /*
474                  * We'd like to have a mechanism to set this on a per-device
475                  * basis, but alas...
476                  */
477                 .psc_thr                = {
478                         .tc_thr_name            = LUSTRE_MDT_NAME "_out",
479                         .tc_thr_factor          = MDS_THR_FACTOR,
480                         .tc_nthrs_init          = MDS_NTHRS_INIT,
481                         .tc_nthrs_base          = MDS_NTHRS_BASE,
482                         .tc_nthrs_max           = MDS_NTHRS_MAX,
483                         .tc_nthrs_user          = mds_num_threads,
484                         .tc_cpu_affinity        = 1,
485                         .tc_ctx_tags            = LCT_MD_THREAD,
486                 },
487                 .psc_cpt                = {
488                         .cc_pattern             = mds_num_cpts,
489                 },
490                 .psc_ops                = {
491                         .so_req_handler         = tgt_request_handle,
492                         .so_req_printer         = target_print_req,
493                         .so_hpreq_handler       = NULL,
494                 },
495         };
496         m->mds_out_service = ptlrpc_register_service(&conf, procfs_entry);
497         if (IS_ERR(m->mds_out_service)) {
498                 rc = PTR_ERR(m->mds_out_service);
499                 CERROR("failed to start out service: %d\n", rc);
500                 m->mds_out_service = NULL;
501                 GOTO(err_mds_svc, rc);
502         }
503
504         /*
505          * sequence controller service configuration
506          */
507         memset(&conf, 0, sizeof(conf));
508         conf = (typeof(conf)) {
509                 .psc_name               = LUSTRE_MDT_NAME "_seqs",
510                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
511                 .psc_buf                = {
512                         .bc_nbufs               = MDS_NBUFS,
513                         .bc_buf_size            = SEQ_BUFSIZE,
514                         .bc_req_max_size        = SEQ_MAXREQSIZE,
515                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
516                         .bc_req_portal          = SEQ_CONTROLLER_PORTAL,
517                         .bc_rep_portal          = MDC_REPLY_PORTAL,
518                 },
519                 .psc_thr                = {
520                         .tc_thr_name            = LUSTRE_MDT_NAME "_seqs",
521                         .tc_nthrs_init          = MDS_OTHR_NTHRS_INIT,
522                         .tc_nthrs_max           = MDS_OTHR_NTHRS_MAX,
523                         .tc_ctx_tags            = LCT_MD_THREAD,
524                 },
525                 .psc_ops                = {
526                         .so_req_handler         = tgt_request_handle,
527                         .so_req_printer         = target_print_req,
528                         .so_hpreq_handler       = NULL,
529                 },
530         };
531         m->mds_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
532         if (IS_ERR(m->mds_mdsc_service)) {
533                 rc = PTR_ERR(m->mds_mdsc_service);
534                 CERROR("failed to start seq controller service: %d\n", rc);
535                 m->mds_mdsc_service = NULL;
536
537                 GOTO(err_mds_svc, rc);
538         }
539
540         /*
541          * metadata sequence server service configuration
542          */
543         memset(&conf, 0, sizeof(conf));
544         conf = (typeof(conf)) {
545                 .psc_name               = LUSTRE_MDT_NAME "_seqm",
546                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
547                 .psc_buf                = {
548                         .bc_nbufs               = MDS_NBUFS,
549                         .bc_buf_size            = SEQ_BUFSIZE,
550                         .bc_req_max_size        = SEQ_MAXREQSIZE,
551                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
552                         .bc_req_portal          = SEQ_METADATA_PORTAL,
553                         .bc_rep_portal          = MDC_REPLY_PORTAL,
554                 },
555                 .psc_thr                = {
556                         .tc_thr_name            = LUSTRE_MDT_NAME "_seqm",
557                         .tc_nthrs_init          = MDS_OTHR_NTHRS_INIT,
558                         .tc_nthrs_max           = MDS_OTHR_NTHRS_MAX,
559                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
560                 },
561                 .psc_ops                = {
562                         .so_req_handler         = tgt_request_handle,
563                         .so_req_printer         = target_print_req,
564                         .so_hpreq_handler       = NULL,
565                 },
566         };
567         m->mds_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
568         if (IS_ERR(m->mds_mdss_service)) {
569                 rc = PTR_ERR(m->mds_mdss_service);
570                 CERROR("failed to start metadata seq server service: %d\n", rc);
571                 m->mds_mdss_service = NULL;
572
573                 GOTO(err_mds_svc, rc);
574         }
575
576         /* FLD service start */
577         memset(&conf, 0, sizeof(conf));
578         conf = (typeof(conf)) {
579                 .psc_name            = LUSTRE_MDT_NAME "_fld",
580                 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
581                 .psc_buf                = {
582                         .bc_nbufs               = MDS_NBUFS,
583                         .bc_buf_size            = FLD_BUFSIZE,
584                         .bc_req_max_size        = FLD_MAXREQSIZE,
585                         .bc_rep_max_size        = FLD_MAXREPSIZE,
586                         .bc_req_portal          = FLD_REQUEST_PORTAL,
587                         .bc_rep_portal          = MDC_REPLY_PORTAL,
588                 },
589                 .psc_thr                = {
590                         .tc_thr_name            = LUSTRE_MDT_NAME "_fld",
591                         .tc_nthrs_init          = MDS_OTHR_NTHRS_INIT,
592                         .tc_nthrs_max           = MDS_OTHR_NTHRS_MAX,
593                         .tc_ctx_tags            = LCT_DT_THREAD | LCT_MD_THREAD,
594                 },
595                 .psc_ops                = {
596                         .so_req_handler         = tgt_request_handle,
597                         .so_req_printer         = target_print_req,
598                         .so_hpreq_handler       = NULL,
599                 },
600         };
601         m->mds_fld_service = ptlrpc_register_service(&conf, procfs_entry);
602         if (IS_ERR(m->mds_fld_service)) {
603                 rc = PTR_ERR(m->mds_fld_service);
604                 CERROR("failed to start fld service: %d\n", rc);
605                 m->mds_fld_service = NULL;
606
607                 GOTO(err_mds_svc, rc);
608         }
609
610         EXIT;
611 err_mds_svc:
612         if (rc)
613                 mds_stop_ptlrpc_service(m);
614
615         return rc;
616 }
617
618 static inline struct mds_device *mds_dev(struct lu_device *d)
619 {
620         return container_of0(d, struct mds_device, mds_md_dev.md_lu_dev);
621 }
622
623 static struct lu_device *mds_device_fini(const struct lu_env *env,
624                                          struct lu_device *d)
625 {
626         struct mds_device *m = mds_dev(d);
627         struct obd_device *obd = d->ld_obd;
628         ENTRY;
629
630         mds_stop_ptlrpc_service(m);
631         lprocfs_obd_cleanup(obd);
632         RETURN(NULL);
633 }
634
635 static struct lu_device *mds_device_free(const struct lu_env *env,
636                                          struct lu_device *d)
637 {
638         struct mds_device *m = mds_dev(d);
639         ENTRY;
640
641         md_device_fini(&m->mds_md_dev);
642         OBD_FREE_PTR(m);
643         RETURN(NULL);
644 }
645
646 static struct lu_device *mds_device_alloc(const struct lu_env *env,
647                                           struct lu_device_type *t,
648                                           struct lustre_cfg *cfg)
649 {
650         struct mds_device        *m;
651         struct obd_device        *obd;
652         struct lu_device          *l;
653         int rc;
654
655         OBD_ALLOC_PTR(m);
656         if (m == NULL)
657                 return ERR_PTR(-ENOMEM);
658
659         md_device_init(&m->mds_md_dev, t);
660         l = &m->mds_md_dev.md_lu_dev;
661
662         obd = class_name2obd(lustre_cfg_string(cfg, 0));
663         LASSERT(obd != NULL);
664
665         l->ld_obd = obd;
666         /* set this lu_device to obd, because error handling need it */
667         obd->obd_lu_dev = l;
668
669         rc = lprocfs_obd_setup(obd, lprocfs_mds_obd_vars);
670         if (rc != 0) {
671                 mds_device_free(env, l);
672                 l = ERR_PTR(rc);
673                 return l;
674         }
675
676         rc = mds_start_ptlrpc_service(m);
677
678         if (rc != 0) {
679                 mds_device_free(env, l);
680                 l = ERR_PTR(rc);
681                 return l;
682         }
683         return l;
684 }
685
686 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
687 LU_TYPE_INIT_FINI(mds, &mdt_thread_key);
688
689 static struct lu_device_type_operations mds_device_type_ops = {
690         .ldto_init = mds_type_init,
691         .ldto_fini = mds_type_fini,
692
693         .ldto_start = mds_type_start,
694         .ldto_stop  = mds_type_stop,
695
696         .ldto_device_alloc = mds_device_alloc,
697         .ldto_device_free  = mds_device_free,
698         .ldto_device_fini  = mds_device_fini
699 };
700
701 static struct lu_device_type mds_device_type = {
702         .ldt_tags     = LU_DEVICE_MD,
703         .ldt_name     = LUSTRE_MDS_NAME,
704         .ldt_ops      = &mds_device_type_ops,
705         .ldt_ctx_tags = LCT_MD_THREAD
706 };
707
708 static struct obd_ops mds_obd_device_ops = {
709         .o_owner           = THIS_MODULE,
710 };
711
712 int mds_mod_init(void)
713 {
714         int rc;
715
716         if (mdt_num_threads != 0 && mds_num_threads == 0) {
717                 LCONSOLE_INFO("mdt_num_threads module parameter is deprecated, "
718                               "use mds_num_threads instead or unset both for "
719                               "dynamic thread startup\n");
720                 mds_num_threads = mdt_num_threads;
721         }
722
723         rc = class_register_type(&mds_obd_device_ops, NULL,
724                                  lprocfs_mds_module_vars, LUSTRE_MDS_NAME,
725                                  &mds_device_type);
726         return rc;
727 }
728
729 void mds_mod_exit(void)
730 {
731         class_unregister_type(LUSTRE_MDS_NAME);
732 }