Whamcloud - gitweb
b=2262
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/mdt/mdt_handler.c
5  *  Lustre Metadata Target (mdt) request handler
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *   Author: Mike Shaver <shaver@clusterfs.com>
12  *   Author: Nikita Danilov <nikita@clusterfs.com>
13  *   Author: Huang Hua <huanghua@clusterfs.com>
14  *   Author: Yury Umanets <umka@clusterfs.com>
15  *
16  *   This file is part of the Lustre file system, http://www.lustre.org
17  *   Lustre is a trademark of Cluster File Systems, Inc.
18  *
19  *   You may have signed or agreed to another license before downloading
20  *   this software.  If so, you are bound by the terms and conditions
21  *   of that agreement, and the following does not apply to you.  See the
22  *   LICENSE file included with this distribution for more information.
23  *
24  *   If you did not agree to a different license, then this copy of Lustre
25  *   is open source software; you can redistribute it and/or modify it
26  *   under the terms of version 2 of the GNU General Public License as
27  *   published by the Free Software Foundation.
28  *
29  *   In either case, Lustre is distributed in the hope that it will be
30  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
31  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  *   license text for more details.
33  */
34
35 #ifndef EXPORT_SYMTAB
36 # define EXPORT_SYMTAB
37 #endif
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <linux/module.h>
41 /*
42  * struct OBD_{ALLOC,FREE}*()
43  * MDT_FAIL_CHECK
44  */
45 #include <obd_support.h>
46 /* struct ptlrpc_request */
47 #include <lustre_net.h>
48 /* struct obd_export */
49 #include <lustre_export.h>
50 /* struct obd_device */
51 #include <obd.h>
52 /* lu2dt_dev() */
53 #include <dt_object.h>
54 #include <lustre_mds.h>
55 #include <lustre_mdt.h>
56 #include "mdt_internal.h"
57 #include <linux/lustre_acl.h>
58 #include <lustre_param.h>
59
60 mdl_mode_t mdt_mdl_lock_modes[] = {
61         [LCK_MINMODE] = MDL_MINMODE,
62         [LCK_EX]      = MDL_EX,
63         [LCK_PW]      = MDL_PW,
64         [LCK_PR]      = MDL_PR,
65         [LCK_CW]      = MDL_CW,
66         [LCK_CR]      = MDL_CR,
67         [LCK_NL]      = MDL_NL,
68         [LCK_GROUP]   = MDL_GROUP
69 };
70
71 ldlm_mode_t mdt_dlm_lock_modes[] = {
72         [MDL_MINMODE] = LCK_MINMODE,
73         [MDL_EX]      = LCK_EX,
74         [MDL_PW]      = LCK_PW,
75         [MDL_PR]      = LCK_PR,
76         [MDL_CW]      = LCK_CW,
77         [MDL_CR]      = LCK_CR,
78         [MDL_NL]      = LCK_NL,
79         [MDL_GROUP]   = LCK_GROUP
80 };
81
82 /*
83  * Initialized in mdt_mod_init().
84  */
85 unsigned long mdt_num_threads;
86
87 /* ptlrpc request handler for MDT. All handlers are
88  * grouped into several slices - struct mdt_opc_slice,
89  * and stored in an array - mdt_handlers[].
90  */
91 struct mdt_handler {
92         /* The name of this handler. */
93         const char *mh_name;
94         /* Fail id for this handler, checked at the beginning of this handler*/
95         int         mh_fail_id;
96         /* Operation code for this handler */
97         __u32       mh_opc;
98         /* flags are listed in enum mdt_handler_flags below. */
99         __u32       mh_flags;
100         /* The actual handler function to execute. */
101         int (*mh_act)(struct mdt_thread_info *info);
102         /* Request format for this request. */
103         const struct req_format *mh_fmt;
104 };
105
106 enum mdt_handler_flags {
107         /*
108          * struct mdt_body is passed in the incoming message, and object
109          * identified by this fid exists on disk.
110          *
111          * "habeo corpus" == "I have a body"
112          */
113         HABEO_CORPUS = (1 << 0),
114         /*
115          * struct ldlm_request is passed in the incoming message.
116          *
117          * "habeo clavis" == "I have a key"
118          */
119         HABEO_CLAVIS = (1 << 1),
120         /*
121          * this request has fixed reply format, so that reply message can be
122          * packed by generic code.
123          *
124          * "habeo refero" == "I have a reply"
125          */
126         HABEO_REFERO = (1 << 2),
127         /*
128          * this request will modify something, so check whether the filesystem
129          * is readonly or not, then return -EROFS to client asap if necessary.
130          *
131          * "mutabor" == "I shall modify"
132          */
133         MUTABOR      = (1 << 3)
134 };
135
136 struct mdt_opc_slice {
137         __u32               mos_opc_start;
138         int                 mos_opc_end;
139         struct mdt_handler *mos_hs;
140 };
141
142 static struct mdt_opc_slice mdt_regular_handlers[];
143 static struct mdt_opc_slice mdt_readpage_handlers[];
144 static struct mdt_opc_slice mdt_xmds_handlers[];
145 static struct mdt_opc_slice mdt_seq_handlers[];
146 static struct mdt_opc_slice mdt_fld_handlers[];
147
148 static struct mdt_device *mdt_dev(struct lu_device *d);
149 static int mdt_regular_handle(struct ptlrpc_request *req);
150 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
151
152 static struct lu_object_operations mdt_obj_ops;
153
154 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
155 {
156         if (!rep)
157                 return 0;
158         return (rep->lock_policy_res1 & flag);
159 }
160
161 void mdt_clear_disposition(struct mdt_thread_info *info,
162                            struct ldlm_reply *rep, int flag)
163 {
164         if (info)
165                 info->mti_opdata &= ~flag;
166         if (rep)
167                 rep->lock_policy_res1 &= ~flag;
168 }
169
170 void mdt_set_disposition(struct mdt_thread_info *info,
171                          struct ldlm_reply *rep, int flag)
172 {
173         if (info)
174                 info->mti_opdata |= flag;
175         if (rep)
176                 rep->lock_policy_res1 |= flag;
177 }
178
179 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
180 {
181         lh->mlh_pdo_hash = 0;
182         lh->mlh_reg_mode = lm;
183         lh->mlh_type = MDT_REG_LOCK;
184 }
185
186 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
187                        const char *name, int namelen)
188 {
189         lh->mlh_reg_mode = lm;
190         lh->mlh_type = MDT_PDO_LOCK;
191
192         if (name != NULL) {
193                 LASSERT(namelen > 0);
194                 lh->mlh_pdo_hash = full_name_hash(name, namelen);
195         } else {
196                 LASSERT(namelen == 0);
197                 lh->mlh_pdo_hash = 0ull;
198         }
199 }
200
201 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
202                               struct mdt_lock_handle *lh)
203 {
204         mdl_mode_t mode;
205         ENTRY;
206
207         /*
208          * Any dir access needs couple of locks:
209          *
210          * 1) on part of dir we gonna take lookup/modify;
211          *
212          * 2) on whole dir to protect it from concurrent splitting and/or to
213          * flush client's cache for readdir().
214          *
215          * so, for a given mode and object this routine decides what lock mode
216          * to use for lock #2:
217          *
218          * 1) if caller's gonna lookup in dir then we need to protect dir from
219          * being splitted only - LCK_CR
220          *
221          * 2) if caller's gonna modify dir then we need to protect dir from
222          * being splitted and to flush cache - LCK_CW
223          *
224          * 3) if caller's gonna modify dir and that dir seems ready for
225          * splitting then we need to protect it from any type of access
226          * (lookup/modify/split) - LCK_EX --bzzz
227          */
228
229         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
230         LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
231
232         /*
233          * Ask underlaying level its opinion about preferable PDO lock mode
234          * having access type passed as regular lock mode:
235          *
236          * - MDL_MINMODE means that lower layer does not want to specify lock
237          * mode;
238          *
239          * - MDL_NL means that no PDO lock should be taken. This is used in some
240          * cases. Say, for non-splittable directories no need to use PDO locks
241          * at all.
242          */
243         mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
244                              mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
245
246         if (mode != MDL_MINMODE) {
247                 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
248         } else {
249                 /*
250                  * Lower layer does not want to specify locking mode. We do it
251                  * our selves. No special protection is needed, just flush
252                  * client's cache on modification and allow concurrent
253                  * mondification.
254                  */
255                 switch (lh->mlh_reg_mode) {
256                 case LCK_EX:
257                         lh->mlh_pdo_mode = LCK_EX;
258                         break;
259                 case LCK_PR:
260                         lh->mlh_pdo_mode = LCK_CR;
261                         break;
262                 case LCK_PW:
263                         lh->mlh_pdo_mode = LCK_CW;
264                         break;
265                 default:
266                         CERROR("Not expected lock type (0x%x)\n",
267                                (int)lh->mlh_reg_mode);
268                         LBUG();
269                 }
270         }
271
272         LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
273         EXIT;
274 }
275
276 static int mdt_getstatus(struct mdt_thread_info *info)
277 {
278         struct mdt_device *mdt  = info->mti_mdt;
279         struct md_device  *next = mdt->mdt_child;
280         struct mdt_body   *repbody;
281         int                rc;
282
283         ENTRY;
284
285         rc = mdt_check_ucred(info);
286         if (rc)
287                 RETURN(err_serious(rc));
288
289         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
290                 RETURN(err_serious(-ENOMEM));
291
292         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
293         rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
294         if (rc != 0)
295                 RETURN(rc);
296
297         repbody->valid |= OBD_MD_FLID;
298
299         if (mdt->mdt_opts.mo_mds_capa) {
300                 struct mdt_object  *root;
301                 struct lustre_capa *capa;
302
303                 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
304                 if (IS_ERR(root))
305                         RETURN(PTR_ERR(root));
306
307                 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
308                 LASSERT(capa);
309                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
310
311                 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
312                                  0);
313                 mdt_object_put(info->mti_env, root);
314                 if (rc == 0)
315                         repbody->valid |= OBD_MD_FLMDSCAPA;
316         }
317
318         RETURN(rc);
319 }
320
321 static int mdt_statfs(struct mdt_thread_info *info)
322 {
323         struct md_device  *next  = info->mti_mdt->mdt_child;
324         struct obd_statfs *osfs;
325         int                rc;
326
327         ENTRY;
328
329         /* This will trigger a watchdog timeout */
330         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
331                          (MDT_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
332
333         rc = mdt_check_ucred(info);
334         if (rc)
335                 RETURN(err_serious(rc));
336
337         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
338                 rc = err_serious(-ENOMEM);
339         } else {
340                 osfs = req_capsule_server_get(&info->mti_pill,&RMF_OBD_STATFS);
341                 rc = next->md_ops->mdo_statfs(info->mti_env, next,
342                                               &info->mti_u.ksfs);
343                 statfs_pack(osfs, &info->mti_u.ksfs);
344         }
345         RETURN(rc);
346 }
347
348 void mdt_pack_size2body(struct mdt_thread_info *info, struct mdt_object *o)
349 {
350         struct mdt_body *b;
351         struct lu_attr *attr = &info->mti_attr.ma_attr;
352         
353         b = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
354
355         /* Check if Size-on-MDS is enabled. */
356         if ((mdt_conn_flags(info) & OBD_CONNECT_SOM) &&
357             S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
358                 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
359                 b->size = attr->la_size;
360                 b->blocks = attr->la_blocks;
361         }
362 }
363
364 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
365                         const struct lu_attr *attr, const struct lu_fid *fid)
366 {
367         /*XXX should pack the reply body according to lu_valid*/
368         b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID   |
369                     OBD_MD_FLGID   | OBD_MD_FLTYPE  |
370                     OBD_MD_FLMODE  | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
371                     OBD_MD_FLATIME | OBD_MD_FLMTIME ;
372
373         if (!S_ISREG(attr->la_mode))
374                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
375
376         b->atime      = attr->la_atime;
377         b->mtime      = attr->la_mtime;
378         b->ctime      = attr->la_ctime;
379         b->mode       = attr->la_mode;
380         b->size       = attr->la_size;
381         b->blocks     = attr->la_blocks;
382         b->uid        = attr->la_uid;
383         b->gid        = attr->la_gid;
384         b->flags      = attr->la_flags;
385         b->nlink      = attr->la_nlink;
386         b->rdev       = attr->la_rdev;
387
388         if (fid) {
389                 b->fid1 = *fid;
390                 b->valid |= OBD_MD_FLID;
391                 CDEBUG(D_INODE, ""DFID": nlink=%d, mode=%o, size="LPU64"\n",
392                                 PFID(fid), b->nlink, b->mode, b->size);
393         }
394
395         if (info)
396                 mdt_body_reverse_idmap(info, b);
397 }
398
399 static inline int mdt_body_has_lov(const struct lu_attr *la,
400                                    const struct mdt_body *body)
401 {
402         return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
403                 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
404 }
405
406 static int mdt_getattr_internal(struct mdt_thread_info *info,
407                                 struct mdt_object *o)
408 {
409         struct md_object        *next = mdt_object_child(o);
410         const struct mdt_body   *reqbody = info->mti_body;
411         struct ptlrpc_request   *req = mdt_info_req(info);
412         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
413         struct md_attr          *ma = &info->mti_attr;
414         struct lu_attr          *la = &ma->ma_attr;
415         struct req_capsule      *pill = &info->mti_pill;
416         const struct lu_env     *env = info->mti_env;
417         struct mdt_body         *repbody;
418         struct lu_buf           *buffer = &info->mti_buf;
419         int                     rc;
420         ENTRY;
421
422         if (unlikely(MDT_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)))
423                 RETURN(err_serious(-ENOMEM));
424
425         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
426
427         ma->ma_valid = 0;
428
429         rc = mdt_object_exists(o);
430         if (rc < 0) {
431                 /* This object is located on remote node.*/
432                 repbody->fid1 = *mdt_object_fid(o);
433                 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
434                 RETURN(0);
435         }
436
437         buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
438         buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
439
440         /* If it is dir object and client require MEA, then we got MEA */
441         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
442             reqbody->valid & OBD_MD_MEA) {
443                 /* Assumption: MDT_MD size is enough for lmv size. */
444                 ma->ma_lmv = buffer->lb_buf;
445                 ma->ma_lmv_size = buffer->lb_len;
446                 ma->ma_need = MA_LMV | MA_INODE;
447         } else {
448                 ma->ma_lmm = buffer->lb_buf;
449                 ma->ma_lmm_size = buffer->lb_len;
450                 ma->ma_need = MA_LOV | MA_INODE;
451         }
452
453         rc = mo_attr_get(env, next, ma);
454         if (unlikely(rc)) {
455                 CERROR("getattr error for "DFID": %d\n",
456                         PFID(mdt_object_fid(o)), rc);
457                 RETURN(rc);
458         }
459
460         if (likely(ma->ma_valid & MA_INODE))
461                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
462         else
463                 RETURN(-EFAULT);
464
465         if (mdt_body_has_lov(la, reqbody)) {
466                 if (ma->ma_valid & MA_LOV) {
467                         LASSERT(ma->ma_lmm_size);
468                         mdt_dump_lmm(D_INFO, ma->ma_lmm);
469                         repbody->eadatasize = ma->ma_lmm_size;
470                         if (S_ISDIR(la->la_mode))
471                                 repbody->valid |= OBD_MD_FLDIREA;
472                         else
473                                 repbody->valid |= OBD_MD_FLEASIZE;
474                 }
475                 if (ma->ma_valid & MA_LMV) {
476                         LASSERT(S_ISDIR(la->la_mode));
477                         repbody->eadatasize = ma->ma_lmv_size;
478                         repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
479                 }
480                 if (!(ma->ma_valid & MA_LOV) && !(ma->ma_valid & MA_LMV)) {
481                         repbody->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
482                 }
483         } else if (S_ISLNK(la->la_mode) &&
484                    reqbody->valid & OBD_MD_LINKNAME) {
485                 buffer->lb_buf = ma->ma_lmm;
486                 buffer->lb_len = reqbody->eadatasize;
487                 rc = mo_readlink(env, next, buffer);
488                 if (unlikely(rc <= 0)) {
489                         CERROR("readlink failed: %d\n", rc);
490                         rc = -EFAULT;
491                 } else {
492                         repbody->valid |= OBD_MD_LINKNAME;
493                         repbody->eadatasize = rc;
494                         /* NULL terminate */
495                         ((char*)ma->ma_lmm)[rc - 1] = 0;
496                         CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
497                                (char*)ma->ma_lmm, rc);
498                         rc = 0;
499                 }
500         }
501
502         if (reqbody->valid & OBD_MD_FLMODEASIZE) {
503                 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
504                 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
505                 repbody->valid |= OBD_MD_FLMODEASIZE;
506                 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
507                        "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
508                        repbody->max_cookiesize);
509         }
510
511         if (med->med_rmtclient && (reqbody->valid & OBD_MD_FLRMTPERM)) {
512                 void *buf = req_capsule_server_get(pill, &RMF_ACL);
513
514                 /* mdt_getattr_lock only */
515                 rc = mdt_pack_remote_perm(info, o, buf);
516                 if (rc) {
517                         repbody->valid &= ~OBD_MD_FLRMTPERM;
518                         repbody->aclsize = 0;
519                         RETURN(rc);
520                 } else {
521                         repbody->valid |= OBD_MD_FLRMTPERM;
522                         repbody->aclsize = sizeof(struct mdt_remote_perm);
523                 }
524         }
525 #ifdef CONFIG_FS_POSIX_ACL
526         else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
527                  (reqbody->valid & OBD_MD_FLACL)) {
528                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
529                 buffer->lb_len = req_capsule_get_size(pill,
530                                                       &RMF_ACL, RCL_SERVER);
531                 if (buffer->lb_len > 0) {
532                         rc = mo_xattr_get(env, next, buffer,
533                                           XATTR_NAME_ACL_ACCESS);
534                         if (rc < 0) {
535                                 if (rc == -ENODATA) {
536                                         repbody->aclsize = 0;
537                                         repbody->valid |= OBD_MD_FLACL;
538                                         rc = 0;
539                                 } else if (rc == -EOPNOTSUPP) {
540                                         rc = 0;
541                                 } else {
542                                         CERROR("got acl size: %d\n", rc);
543                                 }
544                         } else {
545                                 repbody->aclsize = rc;
546                                 repbody->valid |= OBD_MD_FLACL;
547                                 rc = 0;
548                         }
549                 }
550         }
551 #endif
552
553         if ((reqbody->valid & OBD_MD_FLMDSCAPA) &&
554             info->mti_mdt->mdt_opts.mo_mds_capa) {
555                 struct lustre_capa *capa;
556
557                 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
558                 LASSERT(capa);
559                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
560                 rc = mo_capa_get(env, next, capa, 0);
561                 if (rc)
562                         RETURN(rc);
563                 repbody->valid |= OBD_MD_FLMDSCAPA;
564         }
565         RETURN(rc);
566 }
567
568 static int mdt_renew_capa(struct mdt_thread_info *info)
569 {
570         struct mdt_device  *mdt = info->mti_mdt;
571         struct mdt_object  *obj = info->mti_object;
572         struct mdt_body    *body;
573         struct lustre_capa *capa, *c;
574         int rc;
575         ENTRY;
576
577         /* if object doesn't exist, or server has disabled capability,
578          * return directly, client will find body->valid OBD_MD_FLOSSCAPA
579          * flag not set.
580          */
581         if (!obj || !mdt->mdt_opts.mo_mds_capa)
582                 RETURN(0);
583
584         body = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
585         LASSERT(body != NULL);
586
587         c = req_capsule_client_get(&info->mti_pill, &RMF_CAPA1);
588         LASSERT(c);
589
590         capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
591         LASSERT(capa);
592
593         *capa = *c;
594         rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
595         if (rc == 0)
596                 body->valid |= OBD_MD_FLOSSCAPA;
597
598         RETURN(rc);
599 }
600
601 static int mdt_getattr(struct mdt_thread_info *info)
602 {
603         struct mdt_object       *obj = info->mti_object;
604         struct req_capsule      *pill = &info->mti_pill;
605         struct mdt_body         *reqbody;
606         struct mdt_body         *repbody;
607         mode_t                   mode;
608         int                      md_size;
609         int rc;
610         ENTRY;
611
612         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
613         LASSERT(reqbody);
614
615         if (reqbody->valid & OBD_MD_FLOSSCAPA) {
616                 rc = req_capsule_pack(pill);
617                 if (unlikely(rc))
618                         rc = err_serious(rc);
619                 else {
620                         rc = mdt_renew_capa(info);
621                         mdt_shrink_reply(info);
622                 }
623                 GOTO(out, rc);
624         }
625
626         LASSERT(obj != NULL);
627         LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
628
629         mode = lu_object_attr(&obj->mot_obj.mo_lu);
630         if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
631             (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize))
632                 md_size = reqbody->eadatasize;
633         else
634                 md_size = info->mti_mdt->mdt_max_mdsize;
635
636         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
637
638         rc = req_capsule_pack(pill);
639         if (unlikely(rc != 0))
640                 GOTO(out, rc = err_serious(rc));
641
642         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
643         LASSERT(repbody != NULL);
644         repbody->eadatasize = 0;
645         repbody->aclsize = 0;
646
647         if (reqbody->valid & OBD_MD_FLRMTPERM)
648                 rc = mdt_init_ucred(info, reqbody);
649         else
650                 rc = mdt_check_ucred(info);
651         if (unlikely(rc))
652                 GOTO(out_shrink, rc);
653
654         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
655         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
656
657         /*
658          * Don't check capability at all, because rename might getattr for
659          * remote obj, and at that time no capability is available.
660          */
661         mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
662         rc = mdt_getattr_internal(info, obj);
663         if (reqbody->valid & OBD_MD_FLRMTPERM)
664                 mdt_exit_ucred(info);
665         EXIT;
666 out_shrink:
667         mdt_shrink_reply(info);
668 out:
669         return rc;
670 }
671
672 static int mdt_is_subdir(struct mdt_thread_info *info)
673 {
674         struct mdt_object     *o = info->mti_object;
675         struct req_capsule    *pill = &info->mti_pill;
676         const struct mdt_body *body = info->mti_body;
677         struct mdt_body       *repbody;
678         int                    rc;
679         ENTRY;
680
681         LASSERT(o != NULL);
682
683         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
684
685         /*
686          * We save last checked parent fid to @repbody->fid1 for remote
687          * directory case.
688          */
689         LASSERT(fid_is_sane(&body->fid2));
690         LASSERT(mdt_object_exists(o) > 0);
691         rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
692                            &body->fid2, &repbody->fid1);
693         if (rc == 0 || rc == -EREMOTE)
694                 repbody->valid |= OBD_MD_FLID;
695
696         RETURN(rc);
697 }
698
699 static int mdt_raw_lookup(struct mdt_thread_info *info,
700                           struct mdt_object *parent,
701                           const struct lu_name *lname,
702                           struct ldlm_reply *ldlm_rep)
703 {
704         struct md_object *next = mdt_object_child(info->mti_object);
705         const struct mdt_body *reqbody = info->mti_body;
706         struct lu_fid *child_fid = &info->mti_tmp_fid1;
707         struct mdt_body *repbody;
708         int rc;
709         ENTRY;
710
711         if (reqbody->valid != OBD_MD_FLID)
712                 RETURN(0);
713
714         LASSERT(!info->mti_cross_ref);
715
716         /* Only got the fid of this obj by name */
717         rc = mdo_lookup(info->mti_env, next, lname, child_fid,
718                         &info->mti_spec);
719 #if 0
720         /* XXX is raw_lookup possible as intent operation? */
721         if (rc != 0) {
722                 if (rc == -ENOENT)
723                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
724                 RETURN(rc);
725         } else
726                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
727
728         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
729 #endif
730         if (rc == 0) {
731                 repbody = req_capsule_server_get(&info->mti_pill,
732                                                  &RMF_MDT_BODY);
733                 repbody->fid1 = *child_fid;
734                 repbody->valid = OBD_MD_FLID;
735         }
736         RETURN(1);
737 }
738
739 /*
740  * UPDATE lock should be taken against parent, and be release before exit;
741  * child_bits lock should be taken against child, and be returned back:
742  *            (1)normal request should release the child lock;
743  *            (2)intent request will grant the lock to client.
744  */
745 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
746                                  struct mdt_lock_handle *lhc,
747                                  __u64 child_bits,
748                                  struct ldlm_reply *ldlm_rep)
749 {
750         struct ptlrpc_request  *req       = mdt_info_req(info);
751         struct mdt_body        *reqbody   = NULL;
752         struct mdt_object      *parent    = info->mti_object;
753         struct mdt_object      *child;
754         struct md_object       *next      = mdt_object_child(parent);
755         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
756         struct lu_name         *lname     = NULL;
757         const char             *name;
758         int                     namelen   = 0;
759         struct mdt_lock_handle *lhp;
760         struct ldlm_lock       *lock;
761         struct ldlm_res_id     *res_id;
762         int                     is_resent;
763         int                     rc;
764
765         ENTRY;
766
767         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
768         LASSERT(ergo(is_resent,
769                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
770
771         LASSERT(parent != NULL);
772         name = req_capsule_client_get(&info->mti_pill, &RMF_NAME);
773         if (name == NULL)
774                 RETURN(err_serious(-EFAULT));
775
776         namelen = req_capsule_get_size(&info->mti_pill, &RMF_NAME, 
777                                        RCL_CLIENT) - 1;
778         LASSERT(namelen >= 0);
779
780         /* XXX: "namelen == 0" is for getattr by fid (OBD_CONNECT_ATTRFID),
781          * otherwise do not allow empty name, that is the name must contain
782          * at least one character and the terminating '\0'*/
783         if (namelen == 0) {
784                 reqbody =req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
785                 LASSERT(fid_is_sane(&reqbody->fid2));
786                 name = NULL;
787
788                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
789                        "ldlm_rep = %p\n",
790                        PFID(mdt_object_fid(parent)), PFID(&reqbody->fid2),
791                        ldlm_rep);
792         } else {
793                 lname = mdt_name(info->mti_env, (char *)name, namelen);
794                 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
795                        "ldlm_rep = %p\n",
796                        PFID(mdt_object_fid(parent)), name, ldlm_rep);
797         }
798
799         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
800
801         rc = mdt_object_exists(parent);
802         if (unlikely(rc == 0)) {
803                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
804                                 &parent->mot_obj.mo_lu,
805                                 "Parent doesn't exist!\n");
806                 RETURN(-ESTALE);
807         } else
808                 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
809                          PFID(mdt_object_fid(parent)));
810
811         if (lname) {
812                 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
813                 if (rc != 0) {
814                         if (rc > 0)
815                                 rc = 0;
816                         RETURN(rc);
817                 }
818         }
819
820         if (info->mti_cross_ref) {
821                 /* Only getattr on the child. Parent is on another node. */
822                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
823                 child = parent;
824                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
825                        "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
826
827                 if (is_resent) {
828                         /* Do not take lock for resent case. */
829                         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
830                         LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
831                                  lhc->mlh_reg_lh.cookie);
832                         LASSERT(fid_res_name_eq(mdt_object_fid(child),
833                                                 &lock->l_resource->lr_name));
834                         LDLM_LOCK_PUT(lock);
835                         rc = 0;
836                 } else {
837                         mdt_lock_handle_init(lhc);
838                         mdt_lock_reg_init(lhc, LCK_PR);
839
840                         /*
841                          * Object's name is on another MDS, no lookup lock is
842                          * needed here but update is.
843                          */
844                         child_bits &= ~MDS_INODELOCK_LOOKUP;
845                         child_bits |= MDS_INODELOCK_UPDATE;
846
847                         rc = mdt_object_lock(info, child, lhc, child_bits,
848                                              MDT_LOCAL_LOCK);
849                 }
850                 if (rc == 0) {
851                         /* Finally, we can get attr for child. */
852                         mdt_set_capainfo(info, 0, mdt_object_fid(child),
853                                          BYPASS_CAPA);
854                         rc = mdt_getattr_internal(info, child);
855                         if (unlikely(rc != 0))
856                                 mdt_object_unlock(info, child, lhc, 1);
857                 }
858                 GOTO(out, rc);
859         }
860
861         /* step 1: lock parent */
862         lhp = &info->mti_lh[MDT_LH_PARENT];
863         mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
864         rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
865                              MDT_LOCAL_LOCK);
866
867         if (unlikely(rc != 0))
868                 RETURN(rc);
869
870         if (lname) {
871                 /* step 2: lookup child's fid by name */
872                 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
873                                 &info->mti_spec);
874
875                 if (rc != 0) {
876                         if (rc == -ENOENT)
877                                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
878                         GOTO(out_parent, rc);
879                 } else
880                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
881         } else {
882                 *child_fid = reqbody->fid2;
883                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
884         }
885
886         /*
887          *step 3: find the child object by fid & lock it.
888          *        regardless if it is local or remote.
889          */
890         child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
891
892         if (unlikely(IS_ERR(child)))
893                 GOTO(out_parent, rc = PTR_ERR(child));
894         if (is_resent) {
895                 /* Do not take lock for resent case. */
896                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
897                 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
898                          lhc->mlh_reg_lh.cookie);
899
900                 res_id = &lock->l_resource->lr_name;
901                 if (!fid_res_name_eq(mdt_object_fid(child),
902                                     &lock->l_resource->lr_name)) {
903                          LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
904                                                  &lock->l_resource->lr_name),
905                                  "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
906                                  (unsigned long)res_id->name[0],
907                                  (unsigned long)res_id->name[1],
908                                  (unsigned long)res_id->name[2],
909                                  PFID(mdt_object_fid(parent)));
910                           CWARN("Although resent, but still not get child lock"
911                                 "parent:"DFID" child:"DFID"\n",
912                                 PFID(mdt_object_fid(parent)),
913                                 PFID(mdt_object_fid(child)));
914                           lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
915                           LDLM_LOCK_PUT(lock);
916                           GOTO(relock, 0);
917                 }
918                 LDLM_LOCK_PUT(lock);
919                 rc = 0;
920         } else {
921 relock:
922                 mdt_lock_handle_init(lhc);
923                 mdt_lock_reg_init(lhc, LCK_PR);
924
925                 if (mdt_object_exists(child) == 0) {
926                         LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
927                                         &child->mot_obj.mo_lu,
928                                         "Object doesn't exist!\n");
929                 }
930                 rc = mdt_object_lock(info, child, lhc, child_bits,
931                                      MDT_CROSS_LOCK);
932
933                 if (unlikely(rc != 0))
934                         GOTO(out_child, rc);
935         }
936
937         /* finally, we can get attr for child. */
938         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
939         rc = mdt_getattr_internal(info, child);
940         if (unlikely(rc != 0)) {
941                 mdt_object_unlock(info, child, lhc, 1);
942         } else {
943                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
944                 if (lock) {
945                         struct mdt_body *repbody;
946                         struct lu_attr *ma;
947
948                         /* Debugging code. */
949                         res_id = &lock->l_resource->lr_name;
950                         LDLM_DEBUG(lock, "Returning lock to client\n");
951                         LASSERTF(fid_res_name_eq(mdt_object_fid(child),
952                                                  &lock->l_resource->lr_name),
953                                  "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
954                                  (unsigned long)res_id->name[0],
955                                  (unsigned long)res_id->name[1],
956                                  (unsigned long)res_id->name[2],
957                                  PFID(mdt_object_fid(child)));
958                         /*
959                          * Pack Size-on-MDS inode attributes to the body if
960                          * update lock is given.
961                          */
962                         repbody = req_capsule_server_get(&info->mti_pill,
963                                                          &RMF_MDT_BODY);
964                         ma = &info->mti_attr.ma_attr;
965                         if (lock->l_policy_data.l_inodebits.bits &
966                             MDS_INODELOCK_UPDATE)
967                                 mdt_pack_size2body(info, child);
968                         LDLM_LOCK_PUT(lock);
969                 }
970         }
971         EXIT;
972 out_child:
973         mdt_object_put(info->mti_env, child);
974 out_parent:
975         mdt_object_unlock(info, parent, lhp, 1);
976 out:
977         return rc;
978 }
979
980 /* normal handler: should release the child lock */
981 static int mdt_getattr_name(struct mdt_thread_info *info)
982 {
983         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
984         struct mdt_body        *reqbody;
985         struct mdt_body        *repbody;
986         int rc;
987         ENTRY;
988
989         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
990         LASSERT(reqbody != NULL);
991         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
992         LASSERT(repbody != NULL);
993
994         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
995         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
996         repbody->eadatasize = 0;
997         repbody->aclsize = 0;
998
999         rc = mdt_init_ucred(info, reqbody);
1000         if (unlikely(rc))
1001                 GOTO(out, rc);
1002
1003         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1004         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1005                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1006                 lhc->mlh_reg_lh.cookie = 0;
1007         }
1008         mdt_exit_ucred(info);
1009         EXIT;
1010 out:
1011         mdt_shrink_reply(info);
1012         return rc;
1013 }
1014
1015 static struct lu_device_operations mdt_lu_ops;
1016
1017 static int lu_device_is_mdt(struct lu_device *d)
1018 {
1019         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1020 }
1021
1022 static int mdt_set_info(struct mdt_thread_info *info)
1023 {
1024         struct ptlrpc_request *req = mdt_info_req(info);
1025         char *key;
1026         __u32 *val;
1027         int keylen, rc = 0;
1028         ENTRY;
1029
1030         rc = lustre_pack_reply(req, 1, NULL, NULL);
1031         if (rc)
1032                 RETURN(rc);
1033
1034         key = req_capsule_client_get(&info->mti_pill, &RMF_SETINFO_KEY);
1035         if (key == NULL) {
1036                 DEBUG_REQ(D_HA, req, "no set_info key");
1037                 RETURN(-EFAULT);
1038         }
1039
1040         keylen = req_capsule_get_size(&info->mti_pill, &RMF_SETINFO_KEY,
1041                                       RCL_CLIENT);
1042
1043         val = req_capsule_client_get(&info->mti_pill, &RMF_SETINFO_VAL);
1044         if (val == NULL) {
1045                 DEBUG_REQ(D_HA, req, "no set_info val");
1046                 RETURN(-EFAULT);
1047         }
1048
1049         if (keylen != (sizeof(KEY_READ_ONLY) - 1) ||
1050             memcmp(key, KEY_READ_ONLY, keylen) != 0)
1051                 RETURN(-EINVAL);
1052
1053         req->rq_status = 0;
1054         lustre_msg_set_status(req->rq_repmsg, 0);
1055
1056         spin_lock(&req->rq_export->exp_lock);
1057         if (*val)
1058                 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1059         else
1060                 req->rq_export->exp_connect_flags &= ~OBD_CONNECT_RDONLY;
1061         spin_unlock(&req->rq_export->exp_lock);
1062
1063         RETURN(0);
1064 }
1065
1066 static int mdt_connect(struct mdt_thread_info *info)
1067 {
1068         int rc;
1069         struct ptlrpc_request *req;
1070
1071         req = mdt_info_req(info);
1072         rc = target_handle_connect(req);
1073         if (rc == 0) {
1074                 LASSERT(req->rq_export != NULL);
1075                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1076                 rc = mdt_init_idmap(info);
1077                 if (rc != 0)
1078                         /* if mdt_init_idmap failed, revocation for connect */
1079                         obd_disconnect(class_export_get(req->rq_export));
1080         } else
1081                 rc = err_serious(rc);
1082         return rc;
1083 }
1084
1085 static int mdt_disconnect(struct mdt_thread_info *info)
1086 {
1087         int rc;
1088         ENTRY;
1089
1090         rc = target_handle_disconnect(mdt_info_req(info));
1091         if (rc)
1092                 rc = err_serious(rc);
1093         RETURN(rc);
1094 }
1095
1096 static int mdt_sendpage(struct mdt_thread_info *info,
1097                         struct lu_rdpg *rdpg)
1098 {
1099         struct ptlrpc_request   *req = mdt_info_req(info);
1100         struct ptlrpc_bulk_desc *desc;
1101         struct l_wait_info      *lwi = &info->mti_u.rdpg.mti_wait_info;
1102         int                      tmpcount;
1103         int                      tmpsize;
1104         int                      i;
1105         int                      rc;
1106         ENTRY;
1107
1108         desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1109                                     MDS_BULK_PORTAL);
1110         if (desc == NULL)
1111                 GOTO(out, rc = -ENOMEM);
1112
1113         for (i = 0, tmpcount = rdpg->rp_count;
1114                 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
1115                 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1116                 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1117         }
1118
1119         LASSERT(desc->bd_nob == rdpg->rp_count);
1120         rc = ptlrpc_start_bulk_transfer(desc);
1121         if (rc)
1122                 GOTO(free_desc, rc);
1123
1124         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1125                 GOTO(abort_bulk, rc);
1126
1127         *lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
1128         rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), lwi);
1129         LASSERT (rc == 0 || rc == -ETIMEDOUT);
1130
1131         if (rc == 0) {
1132                 if (desc->bd_success &&
1133                     desc->bd_nob_transferred == rdpg->rp_count)
1134                         GOTO(free_desc, rc);
1135
1136                 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
1137         }
1138
1139         DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s",
1140                   (rc == -ETIMEDOUT) ? "timeout" : "network error",
1141                   desc->bd_nob_transferred, rdpg->rp_count,
1142                   req->rq_export->exp_client_uuid.uuid,
1143                   req->rq_export->exp_connection->c_remote_uuid.uuid);
1144
1145         class_fail_export(req->rq_export);
1146
1147         EXIT;
1148 abort_bulk:
1149         ptlrpc_abort_bulk(desc);
1150 free_desc:
1151         ptlrpc_free_bulk(desc);
1152 out:
1153         return rc;
1154 }
1155
1156 #ifdef HAVE_SPLIT_SUPPORT
1157 /*
1158  * Retrieve dir entry from the page and insert it to the slave object, actually,
1159  * this should be in osd layer, but since it will not in the final product, so
1160  * just do it here and do not define more moo api anymore for this.
1161  */
1162 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1163                               int size)
1164 {
1165         struct mdt_object *object = info->mti_object;
1166         struct lu_fid *lf = &info->mti_tmp_fid2;
1167         struct md_attr *ma = &info->mti_attr;
1168         struct lu_dirpage *dp;
1169         struct lu_dirent *ent;
1170         int rc = 0, offset = 0;
1171         ENTRY;
1172
1173         /* Make sure we have at least one entry. */
1174         if (size == 0)
1175                 RETURN(-EINVAL);
1176
1177         /*
1178          * Disable trans for this name insert, since it will include many trans
1179          * for this.
1180          */
1181         info->mti_no_need_trans = 1;
1182         /*
1183          * When write_dir_page, no need update parent's ctime,
1184          * and no permission check for name_insert.
1185          */
1186         ma->ma_attr.la_ctime = 0;
1187         ma->ma_attr.la_valid = LA_MODE;
1188         ma->ma_valid = MA_INODE;
1189
1190         kmap(page);
1191         dp = page_address(page);
1192         offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1193
1194         for (ent = lu_dirent_start(dp); ent != NULL;
1195              ent = lu_dirent_next(ent)) {
1196                 struct lu_name *lname;
1197                 char *name;
1198
1199                 if (le16_to_cpu(ent->lde_namelen) == 0)
1200                         continue;
1201
1202                 fid_le_to_cpu(lf, &ent->lde_fid);
1203                 if (le32_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1204                         ma->ma_attr.la_mode = S_IFDIR;
1205                 else
1206                         ma->ma_attr.la_mode = 0;
1207                 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1208                 if (name == NULL)
1209                         GOTO(out, rc = -ENOMEM);
1210
1211                 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1212                 lname = mdt_name(info->mti_env, name,
1213                                  le16_to_cpu(ent->lde_namelen) + 1);
1214                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1215                 rc = mdo_name_insert(info->mti_env,
1216                                      md_object_next(&object->mot_obj),
1217                                      lname, lf, ma);
1218                 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1219                 if (rc) {
1220                         CERROR("Can't insert %*.*s, rc %d\n",
1221                                le16_to_cpu(ent->lde_namelen),
1222                                le16_to_cpu(ent->lde_namelen),
1223                                ent->lde_name, rc);
1224                         GOTO(out, rc);
1225                 }
1226
1227                 offset += lu_dirent_size(ent);
1228                 if (offset >= size)
1229                         break;
1230         }
1231         EXIT;
1232 out:
1233         kunmap(page);
1234         return rc;
1235 }
1236
1237 static int mdt_bulk_timeout(void *data)
1238 {
1239         ENTRY;
1240
1241         CERROR("mdt bulk transfer timeout \n");
1242
1243         RETURN(1);
1244 }
1245
1246 static int mdt_writepage(struct mdt_thread_info *info)
1247 {
1248         struct ptlrpc_request   *req = mdt_info_req(info);
1249         struct mdt_body         *reqbody;
1250         struct l_wait_info      *lwi;
1251         struct ptlrpc_bulk_desc *desc;
1252         struct page             *page;
1253         int                rc;
1254         ENTRY;
1255
1256
1257         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
1258         if (reqbody == NULL)
1259                 RETURN(err_serious(-EFAULT));
1260
1261         desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1262         if (desc == NULL)
1263                 RETURN(err_serious(-ENOMEM));
1264
1265         /* allocate the page for the desc */
1266         page = cfs_alloc_page(CFS_ALLOC_STD);
1267         if (page == NULL)
1268                 GOTO(desc_cleanup, rc = -ENOMEM);
1269
1270         CDEBUG(D_INFO, "Received page offset %d size %d \n",
1271                (int)reqbody->size, (int)reqbody->nlink);
1272
1273         ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1274                               (int)reqbody->nlink);
1275
1276         /*
1277          * Check if client was evicted while we were doing i/o before touching
1278          * network.
1279          */
1280         OBD_ALLOC_PTR(lwi);
1281         if (!lwi)
1282                 GOTO(cleanup_page, rc = -ENOMEM);
1283
1284         if (desc->bd_export->exp_failed)
1285                 rc = -ENOTCONN;
1286         else
1287                 rc = ptlrpc_start_bulk_transfer (desc);
1288         if (rc == 0) {
1289                 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1290                                             mdt_bulk_timeout, desc);
1291                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1292                                   desc->bd_export->exp_failed, lwi);
1293                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1294                 if (rc == -ETIMEDOUT) {
1295                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1296                         ptlrpc_abort_bulk(desc);
1297                 } else if (desc->bd_export->exp_failed) {
1298                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1299                         rc = -ENOTCONN;
1300                         ptlrpc_abort_bulk(desc);
1301                 } else if (!desc->bd_success ||
1302                            desc->bd_nob_transferred != desc->bd_nob) {
1303                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1304                                   desc->bd_success ?
1305                                   "truncated" : "network error on",
1306                                   desc->bd_nob_transferred, desc->bd_nob);
1307                         /* XXX should this be a different errno? */
1308                         rc = -ETIMEDOUT;
1309                 }
1310         } else {
1311                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1312         }
1313         if (rc)
1314                 GOTO(cleanup_lwi, rc);
1315         rc = mdt_write_dir_page(info, page, reqbody->nlink);
1316
1317 cleanup_lwi:
1318         OBD_FREE_PTR(lwi);
1319 cleanup_page:
1320         __cfs_free_page(page);
1321 desc_cleanup:
1322         ptlrpc_free_bulk(desc);
1323         RETURN(rc);
1324 }
1325 #endif
1326
1327 static int mdt_readpage(struct mdt_thread_info *info)
1328 {
1329         struct mdt_object *object = info->mti_object;
1330         struct lu_rdpg    *rdpg = &info->mti_u.rdpg.mti_rdpg;
1331         struct mdt_body   *reqbody;
1332         struct mdt_body   *repbody;
1333         int                rc;
1334         int                i;
1335         ENTRY;
1336
1337         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1338                 RETURN(err_serious(-ENOMEM));
1339
1340         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
1341         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
1342         if (reqbody == NULL || repbody == NULL)
1343                 RETURN(err_serious(-EFAULT));
1344
1345         rc = mdt_check_ucred(info);
1346         if (rc)
1347                 RETURN(err_serious(rc));
1348
1349         /*
1350          * prepare @rdpg before calling lower layers and transfer itself. Here
1351          * reqbody->size contains offset of where to start to read and
1352          * reqbody->nlink contains number bytes to read.
1353          */
1354         rdpg->rp_hash = reqbody->size;
1355         if ((__u64)rdpg->rp_hash != reqbody->size) {
1356                 CERROR("Invalid hash: %#llx != %#llx\n",
1357                        (__u64)rdpg->rp_hash, reqbody->size);
1358                 RETURN(-EFAULT);
1359         }
1360         rdpg->rp_count  = reqbody->nlink;
1361         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1362         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1363         if (rdpg->rp_pages == NULL)
1364                 RETURN(-ENOMEM);
1365
1366         for (i = 0; i < rdpg->rp_npages; ++i) {
1367                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1368                 if (rdpg->rp_pages[i] == NULL)
1369                         GOTO(free_rdpg, rc = -ENOMEM);
1370         }
1371
1372         /* call lower layers to fill allocated pages with directory data */
1373         rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1374         if (rc)
1375                 GOTO(free_rdpg, rc);
1376
1377         /* send pages to client */
1378         rc = mdt_sendpage(info, rdpg);
1379
1380         EXIT;
1381 free_rdpg:
1382
1383         for (i = 0; i < rdpg->rp_npages; i++)
1384                 if (rdpg->rp_pages[i] != NULL)
1385                         __cfs_free_page(rdpg->rp_pages[i]);
1386         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1387
1388         MDT_FAIL_RETURN(OBD_FAIL_MDS_SENDPAGE, 0);
1389
1390         return rc;
1391 }
1392
1393 static int mdt_reint_internal(struct mdt_thread_info *info,
1394                               struct mdt_lock_handle *lhc,
1395                               __u32 op)
1396 {
1397         struct req_capsule      *pill = &info->mti_pill;
1398         struct mdt_device       *mdt = info->mti_mdt;
1399         struct mdt_body         *repbody;
1400         int                      need_shrink = 0;
1401         int                      rc;
1402         ENTRY;
1403
1404         /* pack reply */
1405         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
1406                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1407                                      mdt->mdt_max_mdsize);
1408                 need_shrink = 1;
1409         }
1410         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
1411                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1412                                      mdt->mdt_max_cookiesize);
1413                 need_shrink = 1;
1414         }
1415         rc = req_capsule_pack(pill);
1416         if (rc != 0) {
1417                 CERROR("Can't pack response, rc %d\n", rc);
1418                 RETURN(err_serious(rc));
1419         }
1420
1421         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1422                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1423                 LASSERT(repbody);
1424                 repbody->eadatasize = 0;
1425                 repbody->aclsize = 0;
1426         }
1427
1428         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1429                 GOTO(out_shrink, rc = err_serious(-EFAULT));
1430
1431         rc = mdt_reint_unpack(info, op);
1432         if (rc != 0) {
1433                 CERROR("Can't unpack reint, rc %d\n", rc);
1434                 GOTO(out_shrink, rc = err_serious(rc));
1435         }
1436
1437         rc = mdt_init_ucred_reint(info);
1438         if (rc)
1439                 GOTO(out_shrink, rc);
1440
1441         rc = mdt_fix_attr_ucred(info, op);
1442         if (rc != 0)
1443                 GOTO(out_ucred, rc = err_serious(rc));
1444
1445         need_shrink = 0;
1446         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1447                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1448                 GOTO(out_ucred, rc);
1449         }
1450         
1451         rc = mdt_reint_rec(info, lhc);
1452         EXIT;
1453 out_ucred:
1454         mdt_exit_ucred(info);
1455 out_shrink:
1456         if (need_shrink)
1457                 mdt_shrink_reply(info);
1458         return rc;
1459 }
1460
1461 static long mdt_reint_opcode(struct mdt_thread_info *info,
1462                              const struct req_format **fmt)
1463 {
1464         __u32 *ptr;
1465         long opc;
1466
1467         opc = err_serious(-EFAULT);
1468         ptr = req_capsule_client_get(&info->mti_pill, &RMF_REINT_OPC);
1469         if (ptr != NULL) {
1470                 opc = *ptr;
1471                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1472                 if (opc < REINT_MAX && fmt[opc] != NULL)
1473                         req_capsule_extend(&info->mti_pill, fmt[opc]);
1474                 else {
1475                         CERROR("Unsupported opc: %ld\n", opc);
1476                         opc = err_serious(opc);
1477                 }
1478         }
1479         return opc;
1480 }
1481
1482 static int mdt_reint(struct mdt_thread_info *info)
1483 {
1484         long opc;
1485         int  rc;
1486
1487         static const struct req_format *reint_fmts[REINT_MAX] = {
1488                 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1489                 [REINT_CREATE]  = &RQF_MDS_REINT_CREATE,
1490                 [REINT_LINK]    = &RQF_MDS_REINT_LINK,
1491                 [REINT_UNLINK]  = &RQF_MDS_REINT_UNLINK,
1492                 [REINT_RENAME]  = &RQF_MDS_REINT_RENAME,
1493                 [REINT_OPEN]    = &RQF_MDS_REINT_OPEN
1494         };
1495
1496         ENTRY;
1497
1498         opc = mdt_reint_opcode(info, reint_fmts);
1499         if (opc >= 0) {
1500                 /*
1501                  * No lock possible here from client to pass it to reint code
1502                  * path.
1503                  */
1504                 rc = mdt_reint_internal(info, NULL, opc);
1505         } else {
1506                 rc = opc;
1507         }
1508
1509         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1510         RETURN(rc);
1511 }
1512
1513 /* TODO these two methods not available now. */
1514
1515 /* this should sync the whole device */
1516 static int mdt_device_sync(struct mdt_thread_info *info)
1517 {
1518         return 0;
1519 }
1520
1521 /* this should sync this object */
1522 static int mdt_object_sync(struct mdt_thread_info *info)
1523 {
1524         return 0;
1525 }
1526
1527 static int mdt_sync(struct mdt_thread_info *info)
1528 {
1529         struct req_capsule *pill = &info->mti_pill;
1530         struct mdt_body *body;
1531         int rc;
1532         ENTRY;
1533
1534         /* The fid may be zero, so we req_capsule_set manually */
1535         req_capsule_set(pill, &RQF_MDS_SYNC);
1536
1537         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1538         if (body == NULL)
1539                 RETURN(err_serious(-EINVAL));
1540
1541         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1542                 RETURN(err_serious(-ENOMEM));
1543
1544         if (fid_seq(&body->fid1) == 0) {
1545                 /* sync the whole device */
1546                 rc = req_capsule_pack(pill);
1547                 if (rc == 0)
1548                         rc = mdt_device_sync(info);
1549                 else
1550                         rc = err_serious(rc);
1551         } else {
1552                 /* sync an object */
1553                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1554                 if (rc == 0) {
1555                         rc = mdt_object_sync(info);
1556                         if (rc == 0) {
1557                                 struct md_object *next;
1558                                 const struct lu_fid *fid;
1559                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1560
1561                                 next = mdt_object_child(info->mti_object);
1562                                 info->mti_attr.ma_need = MA_INODE;
1563                                 info->mti_attr.ma_valid = 0;
1564                                 rc = mo_attr_get(info->mti_env, next,
1565                                                  &info->mti_attr);
1566                                 if (rc == 0) {
1567                                         body = req_capsule_server_get(pill,
1568                                                                 &RMF_MDT_BODY);
1569                                         fid = mdt_object_fid(info->mti_object);
1570                                         mdt_pack_attr2body(info, body, la, fid);
1571                                 }
1572                         }
1573                 } else
1574                         rc = err_serious(rc);
1575         }
1576         RETURN(rc);
1577 }
1578
1579 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1580 {
1581         return err_serious(-EOPNOTSUPP);
1582 }
1583
1584 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1585 {
1586         return err_serious(-EOPNOTSUPP);
1587 }
1588
1589 /*
1590  * OBD PING and other handlers.
1591  */
1592 static int mdt_obd_ping(struct mdt_thread_info *info)
1593 {
1594         int rc;
1595         ENTRY;
1596         rc = target_handle_ping(mdt_info_req(info));
1597         if (rc < 0)
1598                 rc = err_serious(rc);
1599         RETURN(rc);
1600 }
1601
1602 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1603 {
1604         return err_serious(-EOPNOTSUPP);
1605 }
1606
1607 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1608 {
1609         return err_serious(-EOPNOTSUPP);
1610 }
1611
1612
1613 /*
1614  * DLM handlers.
1615  */
1616 static struct ldlm_callback_suite cbs = {
1617         .lcs_completion = ldlm_server_completion_ast,
1618         .lcs_blocking   = ldlm_server_blocking_ast,
1619         .lcs_glimpse    = NULL
1620 };
1621
1622 static int mdt_enqueue(struct mdt_thread_info *info)
1623 {
1624         struct ptlrpc_request *req;
1625         __u64 req_bits;
1626         int rc;
1627
1628         /*
1629          * info->mti_dlm_req already contains swapped and (if necessary)
1630          * converted dlm request.
1631          */
1632         LASSERT(info->mti_dlm_req != NULL);
1633
1634         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE)) {
1635                 info->mti_fail_id = OBD_FAIL_LDLM_ENQUEUE;
1636                 return 0;
1637         }
1638
1639         req = mdt_info_req(info);
1640
1641         /*
1642          * Lock without inodebits makes no sense and will oops later in
1643          * ldlm. Let's check it now to see if we have wrong lock from client or
1644          * bits get corrupted somewhere in mdt_intent_policy().
1645          */
1646         req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1647         LASSERT(req_bits != 0);
1648
1649         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1650                                   req, info->mti_dlm_req, &cbs);
1651         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1652         return rc ? err_serious(rc) : req->rq_status;
1653 }
1654
1655 static int mdt_convert(struct mdt_thread_info *info)
1656 {
1657         int rc;
1658         struct ptlrpc_request *req;
1659
1660         LASSERT(info->mti_dlm_req);
1661         req = mdt_info_req(info);
1662         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1663         return rc ? err_serious(rc) : req->rq_status;
1664 }
1665
1666 static int mdt_bl_callback(struct mdt_thread_info *info)
1667 {
1668         CERROR("bl callbacks should not happen on MDS\n");
1669         LBUG();
1670         return err_serious(-EOPNOTSUPP);
1671 }
1672
1673 static int mdt_cp_callback(struct mdt_thread_info *info)
1674 {
1675         CERROR("cp callbacks should not happen on MDS\n");
1676         LBUG();
1677         return err_serious(-EOPNOTSUPP);
1678 }
1679
1680 /*
1681  * sec context handlers
1682  */
1683 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
1684 {
1685         int rc;
1686
1687         rc = mdt_handle_idmap(info);
1688
1689         if (unlikely(rc)) {
1690                 struct ptlrpc_request *req = mdt_info_req(info);
1691                 __u32                  opc;
1692
1693                 opc = lustre_msg_get_opc(req->rq_reqmsg);
1694                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
1695                         sptlrpc_svc_ctx_invalidate(req);
1696         }
1697
1698         return rc;
1699 }
1700
1701 static struct mdt_object *mdt_obj(struct lu_object *o)
1702 {
1703         LASSERT(lu_device_is_mdt(o->lo_dev));
1704         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
1705 }
1706
1707 struct mdt_object *mdt_object_find(const struct lu_env *env,
1708                                    struct mdt_device *d,
1709                                    const struct lu_fid *f)
1710 {
1711         struct lu_object *o;
1712         struct mdt_object *m;
1713         ENTRY;
1714
1715         o = lu_object_find(env, d->mdt_md_dev.md_lu_dev.ld_site, f);
1716         if (unlikely(IS_ERR(o)))
1717                 m = (struct mdt_object *)o;
1718         else
1719                 m = mdt_obj(o);
1720         RETURN(m);
1721 }
1722
1723 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
1724                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
1725 {
1726         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1727         ldlm_policy_data_t *policy = &info->mti_policy;
1728         struct ldlm_res_id *res_id = &info->mti_res_id;
1729         int rc;
1730         ENTRY;
1731
1732         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1733         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1734         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
1735         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
1736
1737         if (mdt_object_exists(o) < 0) {
1738                 if (locality == MDT_CROSS_LOCK) {
1739                         /* cross-ref object fix */
1740                         ibits &= ~MDS_INODELOCK_UPDATE;
1741                         ibits |= MDS_INODELOCK_LOOKUP;
1742                 } else {
1743                         LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
1744                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
1745                 }
1746                 /* No PDO lock on remote object */
1747                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
1748         }
1749
1750         memset(policy, 0, sizeof(*policy));
1751         fid_build_reg_res_name(mdt_object_fid(o), res_id);
1752
1753         /*
1754          * Take PDO lock on whole directory and build correct @res_id for lock
1755          * on part of directory.
1756          */
1757         if (lh->mlh_pdo_hash != 0) {
1758                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
1759                 mdt_lock_pdo_mode(info, o, lh);
1760                 if (lh->mlh_pdo_mode != LCK_NL) {
1761                         /*
1762                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
1763                          * is never going to be sent to client and we do not
1764                          * want it slowed down due to possible cancels.
1765                          */
1766                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1767                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
1768                                           policy, res_id, LDLM_FL_ATOMIC_CB);
1769                         if (unlikely(rc))
1770                                 RETURN(rc);
1771                 }
1772
1773                 /*
1774                  * Finish res_id initializing by name hash marking patr of
1775                  * directory which is taking modification.
1776                  */
1777                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
1778         }
1779
1780         policy->l_inodebits.bits = ibits;
1781
1782         /*
1783          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1784          * going to be sent to client. If it is - mdt_intent_policy() path will
1785          * fix it up and turns FL_LOCAL flag off.
1786          */
1787         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1788                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB);
1789
1790         if (rc)
1791                 GOTO(out, rc);
1792
1793         if (lh->mlh_type == MDT_PDO_LOCK) {
1794                 /* check for exists after object is locked */
1795                 if (mdt_object_exists(o) == 0) {
1796                         /* Non-existent object shouldn't have PDO lock */
1797                         rc = -ESTALE;
1798                 } else {
1799                         /* Non-dir object shouldn't have PDO lock */
1800                         LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
1801                 }
1802         }
1803 out:
1804         if (rc)
1805                 mdt_object_unlock(info, o, lh, 1);
1806
1807
1808         RETURN(rc);
1809 }
1810
1811 /*
1812  * Just call ldlm_lock_decref() if decref, else we only call ptlrpc_save_lock()
1813  * to save this lock in req.  when transaction committed, req will be released,
1814  * and lock will, too.
1815  */
1816 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
1817                        struct mdt_lock_handle *lh, int decref)
1818 {
1819         struct ptlrpc_request *req = mdt_info_req(info);
1820         ENTRY;
1821
1822         if (lustre_handle_is_used(&lh->mlh_pdo_lh)) {
1823                 /* Do not save PDO locks to request, just decref. */
1824                 mdt_fid_unlock(&lh->mlh_pdo_lh,
1825                                lh->mlh_pdo_mode);
1826                 lh->mlh_pdo_lh.cookie = 0ull;
1827         }
1828
1829         if (lustre_handle_is_used(&lh->mlh_reg_lh)) {
1830                 if (decref) {
1831                         mdt_fid_unlock(&lh->mlh_reg_lh,
1832                                        lh->mlh_reg_mode);
1833                 } else {
1834                         ptlrpc_save_lock(req, &lh->mlh_reg_lh,
1835                                          lh->mlh_reg_mode);
1836                 }
1837                 lh->mlh_reg_lh.cookie = 0ull;
1838         }
1839
1840         EXIT;
1841 }
1842
1843 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
1844                                         const struct lu_fid *f,
1845                                         struct mdt_lock_handle *lh,
1846                                         __u64 ibits)
1847 {
1848         struct mdt_object *o;
1849
1850         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
1851         if (!IS_ERR(o)) {
1852                 int rc;
1853
1854                 rc = mdt_object_lock(info, o, lh, ibits,
1855                                      MDT_LOCAL_LOCK);
1856                 if (rc != 0) {
1857                         mdt_object_put(info->mti_env, o);
1858                         o = ERR_PTR(rc);
1859                 }
1860         }
1861         return o;
1862 }
1863
1864 void mdt_object_unlock_put(struct mdt_thread_info * info,
1865                            struct mdt_object * o,
1866                            struct mdt_lock_handle *lh,
1867                            int decref)
1868 {
1869         mdt_object_unlock(info, o, lh, decref);
1870         mdt_object_put(info->mti_env, o);
1871 }
1872
1873 static struct mdt_handler *mdt_handler_find(__u32 opc,
1874                                             struct mdt_opc_slice *supported)
1875 {
1876         struct mdt_opc_slice *s;
1877         struct mdt_handler   *h;
1878
1879         h = NULL;
1880         for (s = supported; s->mos_hs != NULL; s++) {
1881                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
1882                         h = s->mos_hs + (opc - s->mos_opc_start);
1883                         if (likely(h->mh_opc != 0))
1884                                 LASSERT(h->mh_opc == opc);
1885                         else
1886                                 h = NULL; /* unsupported opc */
1887                         break;
1888                 }
1889         }
1890         return h;
1891 }
1892
1893 static int mdt_lock_resname_compat(struct mdt_device *m,
1894                                    struct ldlm_request *req)
1895 {
1896         /* XXX something... later. */
1897         return 0;
1898 }
1899
1900 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
1901 {
1902         /* XXX something... later. */
1903         return 0;
1904 }
1905
1906 /*
1907  * Generic code handling requests that have struct mdt_body passed in:
1908  *
1909  *  - extract mdt_body from request and save it in @info, if present;
1910  *
1911  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
1912  *  @info;
1913  *
1914  *  - if HABEO_CORPUS flag is set for this request type check whether object
1915  *  actually exists on storage (lu_object_exists()).
1916  *
1917  */
1918 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
1919 {
1920         const struct mdt_body    *body;
1921         struct mdt_object        *obj;
1922         const struct lu_env      *env;
1923         struct req_capsule       *pill;
1924         int                       rc;
1925
1926         env = info->mti_env;
1927         pill = &info->mti_pill;
1928
1929         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1930         if (body == NULL)
1931                 return -EFAULT;
1932
1933         if (!fid_is_sane(&body->fid1)) {
1934                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
1935                 return -EINVAL;
1936         }
1937
1938         /*
1939          * Do not get size or any capa fields before we check that request
1940          * contains capa actually. There are some requests which do not, for
1941          * instance MDS_IS_SUBDIR.
1942          */
1943         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
1944             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1945                 mdt_set_capainfo(info, 0, &body->fid1,
1946                                  req_capsule_client_get(pill, &RMF_CAPA1));
1947
1948         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
1949         if (!IS_ERR(obj)) {
1950                 if ((flags & HABEO_CORPUS) &&
1951                     !mdt_object_exists(obj)) {
1952                         mdt_object_put(env, obj);
1953                         /* for capability renew ENOENT will be handled in
1954                          * mdt_renew_capa */
1955                         if (body->valid & OBD_MD_FLOSSCAPA)
1956                                 rc = 0;
1957                         else
1958                                 rc = -ENOENT;
1959                 } else {
1960                         info->mti_object = obj;
1961                         rc = 0;
1962                 }
1963         } else
1964                 rc = PTR_ERR(obj);
1965
1966         return rc;
1967 }
1968
1969 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
1970 {
1971         struct req_capsule *pill;
1972         int rc;
1973
1974         ENTRY;
1975         pill = &info->mti_pill;
1976
1977         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
1978                 rc = mdt_body_unpack(info, flags);
1979         else
1980                 rc = 0;
1981
1982         if (rc == 0 && (flags & HABEO_REFERO)) {
1983                 struct mdt_device *mdt = info->mti_mdt;
1984
1985                 /* Pack reply. */
1986                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1987                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1988                                              mdt->mdt_max_mdsize);
1989                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1990                         req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1991                                              mdt->mdt_max_cookiesize);
1992
1993                 rc = req_capsule_pack(pill);
1994         }
1995         RETURN(rc);
1996 }
1997
1998 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
1999 {
2000         struct md_device *next = m->mdt_child;
2001
2002         return next->md_ops->mdo_init_capa_ctxt(env, next,
2003                                                 m->mdt_opts.mo_mds_capa,
2004                                                 m->mdt_capa_timeout,
2005                                                 m->mdt_capa_alg,
2006                                                 m->mdt_capa_keys);
2007 }
2008
2009 /*
2010  * Invoke handler for this request opc. Also do necessary preprocessing
2011  * (according to handler ->mh_flags), and post-processing (setting of
2012  * ->last_{xid,committed}).
2013  */
2014 static int mdt_req_handle(struct mdt_thread_info *info,
2015                           struct mdt_handler *h, struct ptlrpc_request *req)
2016 {
2017         int   rc, serious = 0;
2018         __u32 flags;
2019
2020         ENTRY;
2021
2022         LASSERT(h->mh_act != NULL);
2023         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2024         LASSERT(current->journal_info == NULL);
2025
2026         /*
2027          * Do not use *_FAIL_CHECK_ONCE() macros, because they will stop
2028          * correct handling of failed req later in ldlm due to doing
2029          * obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED without actually
2030          * correct actions like it is done in target_send_reply_msg().
2031          */
2032         if (h->mh_fail_id != 0) {
2033                 /*
2034                  * Set to info->mti_fail_id to handler fail_id, it will be used
2035                  * later, and better than use default fail_id.
2036                  */
2037                 if (OBD_FAIL_CHECK(h->mh_fail_id)) {
2038                         info->mti_fail_id = h->mh_fail_id;
2039                         RETURN(0);
2040                 }
2041         }
2042
2043         rc = 0;
2044         flags = h->mh_flags;
2045         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2046
2047         if (h->mh_fmt != NULL) {
2048                 req_capsule_set(&info->mti_pill, h->mh_fmt);
2049                 rc = mdt_unpack_req_pack_rep(info, flags);
2050         }
2051
2052         if (rc == 0 && flags & MUTABOR &&
2053             req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2054                 /* should it be rq_status? */
2055                 rc = -EROFS;
2056
2057         if (rc == 0 && flags & HABEO_CLAVIS) {
2058                 struct ldlm_request *dlm_req;
2059
2060                 LASSERT(h->mh_fmt != NULL);
2061
2062                 dlm_req = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
2063                 if (dlm_req != NULL) {
2064                         if (info->mti_mdt->mdt_opts.mo_compat_resname)
2065                                 rc = mdt_lock_resname_compat(info->mti_mdt,
2066                                                              dlm_req);
2067                         info->mti_dlm_req = dlm_req;
2068                 } else {
2069                         CERROR("Can't unpack dlm request\n");
2070                         rc = -EFAULT;
2071                 }
2072         }
2073
2074         /* capability setting changed via /proc, needs reinitialize ctxt */
2075         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2076                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2077                 info->mti_mdt->mdt_capa_conf = 0;
2078         }
2079
2080         if (likely(rc == 0)) {
2081                 /*
2082                  * Process request, there can be two types of rc:
2083                  * 1) errors with msg unpack/pack, other failures outside the
2084                  * operation itself. This is counted as serious errors;
2085                  * 2) errors during fs operation, should be placed in rq_status
2086                  * only
2087                  */
2088                 rc = h->mh_act(info);
2089                 serious = is_serious(rc);
2090                 rc = clear_serious(rc);
2091         } else
2092                 serious = 1;
2093
2094         req->rq_status = rc;
2095
2096         /*
2097          * ELDLM_* codes which > 0 should be in rq_status only as well as
2098          * all non-serious errors.
2099          */
2100         if (rc > 0 || !serious)
2101                 rc = 0;
2102
2103         LASSERT(current->journal_info == NULL);
2104
2105         if (rc == 0 && (flags & HABEO_CLAVIS) &&
2106             info->mti_mdt->mdt_opts.mo_compat_resname) {
2107                 struct ldlm_reply *dlmrep;
2108
2109                 dlmrep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2110                 if (dlmrep != NULL)
2111                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2112         }
2113
2114         /* If we're DISCONNECTing, the mdt_export_data is already freed */
2115         if (likely(rc == 0 && h->mh_opc != MDS_DISCONNECT))
2116                 target_committed_to_req(req);
2117
2118         if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2119                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2120                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2121                 LBUG();
2122         }
2123
2124         RETURN(rc);
2125 }
2126
2127 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2128 {
2129         lh->mlh_type = MDT_NUL_LOCK;
2130         lh->mlh_reg_lh.cookie = 0ull;
2131         lh->mlh_reg_mode = LCK_MINMODE;
2132         lh->mlh_pdo_lh.cookie = 0ull;
2133         lh->mlh_pdo_mode = LCK_MINMODE;
2134 }
2135
2136 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2137 {
2138         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2139         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2140 }
2141
2142 /*
2143  * Initialize fields of struct mdt_thread_info. Other fields are left in
2144  * uninitialized state, because it's too expensive to zero out whole
2145  * mdt_thread_info (> 1K) on each request arrival.
2146  */
2147 static void mdt_thread_info_init(struct ptlrpc_request *req,
2148                                  struct mdt_thread_info *info)
2149 {
2150         int i;
2151         struct md_capainfo *ci;
2152
2153         info->mti_rep_buf_nr = ARRAY_SIZE(info->mti_rep_buf_size);
2154         for (i = 0; i < ARRAY_SIZE(info->mti_rep_buf_size); i++)
2155                 info->mti_rep_buf_size[i] = -1;
2156         req_capsule_init(&info->mti_pill, req, RCL_SERVER,
2157                          info->mti_rep_buf_size);
2158
2159         /* lock handle */
2160         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2161                 mdt_lock_handle_init(&info->mti_lh[i]);
2162
2163         /* mdt device: it can be NULL while CONNECT */
2164         if (req->rq_export) {
2165                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2166                 info->mti_exp = req->rq_export;
2167         }         else
2168                 info->mti_mdt = NULL;
2169         info->mti_env = req->rq_svc_thread->t_env;
2170         ci = md_capainfo(info->mti_env);
2171         memset(ci, 0, sizeof *ci);
2172
2173         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2174         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2175
2176         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2177         info->mti_body = NULL;
2178         info->mti_object = NULL;
2179         info->mti_dlm_req = NULL;
2180         info->mti_has_trans = 0;
2181         info->mti_no_need_trans = 0;
2182         info->mti_cross_ref = 0;
2183         info->mti_opdata = 0;
2184
2185         /* To not check for split by default. */
2186         info->mti_spec.sp_ck_split = 0;
2187 }
2188
2189 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2190 {
2191         int i;
2192
2193         req_capsule_fini(&info->mti_pill);
2194         if (info->mti_object != NULL) {
2195                 mdt_object_put(info->mti_env, info->mti_object);
2196                 info->mti_object = NULL;
2197         }
2198         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2199                 mdt_lock_handle_fini(&info->mti_lh[i]);
2200         info->mti_env = NULL;
2201 }
2202
2203 /* mds/handler.c */
2204 extern int mds_filter_recovery_request(struct ptlrpc_request *req,
2205                                        struct obd_device *obd, int *process);
2206 /*
2207  * Handle recovery. Return:
2208  *        +1: continue request processing;
2209  *       -ve: abort immediately with the given error code;
2210  *         0: send reply with error code in req->rq_status;
2211  */
2212 static int mdt_recovery(struct mdt_thread_info *info)
2213 {
2214         struct ptlrpc_request *req = mdt_info_req(info);
2215         int recovering;
2216         struct obd_device *obd;
2217
2218         ENTRY;
2219
2220         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2221         case MDS_CONNECT:
2222         case SEC_CTX_INIT:
2223         case SEC_CTX_INIT_CONT:
2224         case SEC_CTX_FINI:
2225                 {
2226 #if 0
2227                         int rc;
2228
2229                         rc = mdt_handle_idmap(info);
2230                         if (rc)
2231                                 RETURN(rc);
2232                         else
2233 #endif
2234                                 RETURN(+1);
2235                 }
2236         }
2237
2238         if (unlikely(req->rq_export == NULL)) {
2239                 CERROR("operation %d on unconnected MDS from %s\n",
2240                        lustre_msg_get_opc(req->rq_reqmsg),
2241                        libcfs_id2str(req->rq_peer));
2242                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2243                  * mds_A will get -ENOTCONN(especially for ping req),
2244                  * which will cause that mds_A deactive timeout, then when
2245                  * mds_A cleanup, the cleanup process will be suspended since
2246                  * deactive timeout is not zero.
2247                  */
2248                 req->rq_status = -ENOTCONN;
2249                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2250                 RETURN(0);
2251         }
2252
2253         /* sanity check: if the xid matches, the request must be marked as a
2254          * resent or replayed */
2255         if (req_xid_is_last(req)) {
2256                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2257                       (MSG_RESENT | MSG_REPLAY))) {
2258                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2259                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2260                                   lustre_msg_get_flags(req->rq_reqmsg));
2261                         LBUG();
2262                         req->rq_status = -ENOTCONN;
2263                         RETURN(-ENOTCONN);
2264                 }
2265         }
2266
2267         /* else: note the opposite is not always true; a RESENT req after a
2268          * failover will usually not match the last_xid, since it was likely
2269          * never committed. A REPLAYed request will almost never match the
2270          * last xid, however it could for a committed, but still retained,
2271          * open. */
2272
2273         obd = req->rq_export->exp_obd;
2274
2275         /* Check for aborted recovery... */
2276         spin_lock_bh(&obd->obd_processing_task_lock);
2277         recovering = obd->obd_recovering;
2278         spin_unlock_bh(&obd->obd_processing_task_lock);
2279         if (unlikely(recovering)) {
2280                 int rc;
2281                 int should_process;
2282                 DEBUG_REQ(D_INFO, req, "Got new replay");
2283                 rc = mds_filter_recovery_request(req, obd, &should_process);
2284                 if (rc != 0 || !should_process)
2285                         RETURN(rc);
2286                 else if (should_process < 0) {
2287                         req->rq_status = should_process;
2288                         rc = ptlrpc_error(req);
2289                         RETURN(rc);
2290                 }
2291         }
2292         RETURN(+1);
2293 }
2294
2295 static int mdt_reply(struct ptlrpc_request *req, int rc,
2296                      struct mdt_thread_info *info)
2297 {
2298         ENTRY;
2299
2300 #if 0
2301         if (req->rq_reply_state == NULL && rc == 0) {
2302                 req->rq_status = rc;
2303                 lustre_pack_reply(req, 1, NULL, NULL);
2304         }
2305 #endif
2306         target_send_reply(req, rc, info->mti_fail_id);
2307         RETURN(0);
2308 }
2309
2310 /* mds/handler.c */
2311 extern int mds_msg_check_version(struct lustre_msg *msg);
2312
2313 static int mdt_handle0(struct ptlrpc_request *req,
2314                        struct mdt_thread_info *info,
2315                        struct mdt_opc_slice *supported)
2316 {
2317         struct mdt_handler *h;
2318         struct lustre_msg  *msg;
2319         int                 rc;
2320
2321         ENTRY;
2322
2323         MDT_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
2324
2325         LASSERT(current->journal_info == NULL);
2326
2327         msg = req->rq_reqmsg;
2328         rc = mds_msg_check_version(msg);
2329         if (likely(rc == 0)) {
2330                 rc = mdt_recovery(info);
2331                 if (likely(rc == +1)) {
2332                         h = mdt_handler_find(lustre_msg_get_opc(msg),
2333                                              supported);
2334                         if (likely(h != NULL)) {
2335                                 rc = mdt_req_handle(info, h, req);
2336                                 rc = mdt_reply(req, rc, info);
2337                         } else {
2338                                 CERROR("The unsupported opc: 0x%x\n", lustre_msg_get_opc(msg) );
2339                                 req->rq_status = -ENOTSUPP;
2340                                 rc = ptlrpc_error(req);
2341                                 RETURN(rc);
2342                         }
2343                 }
2344         } else
2345                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2346         RETURN(rc);
2347 }
2348
2349 /*
2350  * MDT handler function called by ptlrpc service thread when request comes.
2351  *
2352  * XXX common "target" functionality should be factored into separate module
2353  * shared by mdt, ost and stand-alone services like fld.
2354  */
2355 static int mdt_handle_common(struct ptlrpc_request *req,
2356                              struct mdt_opc_slice *supported)
2357 {
2358         struct lu_env          *env;
2359         struct mdt_thread_info *info;
2360         int                     rc;
2361         ENTRY;
2362
2363         env = req->rq_svc_thread->t_env;
2364         LASSERT(env != NULL);
2365         LASSERT(env->le_ses != NULL);
2366         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2367         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2368         LASSERT(info != NULL);
2369
2370         mdt_thread_info_init(req, info);
2371
2372         rc = mdt_handle0(req, info, supported);
2373
2374         mdt_thread_info_fini(info);
2375         RETURN(rc);
2376 }
2377
2378 /*
2379  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2380  * as well.
2381  */
2382 int mdt_recovery_handle(struct ptlrpc_request *req)
2383 {
2384         int rc;
2385         ENTRY;
2386
2387         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2388         case FLD_QUERY:
2389                 rc = mdt_handle_common(req, mdt_fld_handlers);
2390                 break;
2391         case SEQ_QUERY:
2392                 rc = mdt_handle_common(req, mdt_seq_handlers);
2393                 break;
2394         default:
2395                 rc = mdt_handle_common(req, mdt_regular_handlers);
2396                 break;
2397         }
2398
2399         RETURN(rc);
2400 }
2401
2402 static int mdt_regular_handle(struct ptlrpc_request *req)
2403 {
2404         return mdt_handle_common(req, mdt_regular_handlers);
2405 }
2406
2407 static int mdt_readpage_handle(struct ptlrpc_request *req)
2408 {
2409         return mdt_handle_common(req, mdt_readpage_handlers);
2410 }
2411
2412 static int mdt_xmds_handle(struct ptlrpc_request *req)
2413 {
2414         return mdt_handle_common(req, mdt_xmds_handlers);
2415 }
2416
2417 static int mdt_mdsc_handle(struct ptlrpc_request *req)
2418 {
2419         return mdt_handle_common(req, mdt_seq_handlers);
2420 }
2421
2422 static int mdt_mdss_handle(struct ptlrpc_request *req)
2423 {
2424         return mdt_handle_common(req, mdt_seq_handlers);
2425 }
2426
2427 static int mdt_dtss_handle(struct ptlrpc_request *req)
2428 {
2429         return mdt_handle_common(req, mdt_seq_handlers);
2430 }
2431
2432 static int mdt_fld_handle(struct ptlrpc_request *req)
2433 {
2434         return mdt_handle_common(req, mdt_fld_handlers);
2435 }
2436
2437 enum mdt_it_code {
2438         MDT_IT_OPEN,
2439         MDT_IT_OCREAT,
2440         MDT_IT_CREATE,
2441         MDT_IT_GETATTR,
2442         MDT_IT_READDIR,
2443         MDT_IT_LOOKUP,
2444         MDT_IT_UNLINK,
2445         MDT_IT_TRUNC,
2446         MDT_IT_GETXATTR,
2447         MDT_IT_NR
2448 };
2449
2450 static int mdt_intent_getattr(enum mdt_it_code opcode,
2451                               struct mdt_thread_info *info,
2452                               struct ldlm_lock **,
2453                               int);
2454 static int mdt_intent_reint(enum mdt_it_code opcode,
2455                             struct mdt_thread_info *info,
2456                             struct ldlm_lock **,
2457                             int);
2458
2459 static struct mdt_it_flavor {
2460         const struct req_format *it_fmt;
2461         __u32                    it_flags;
2462         int                    (*it_act)(enum mdt_it_code ,
2463                                          struct mdt_thread_info *,
2464                                          struct ldlm_lock **,
2465                                          int);
2466         long                     it_reint;
2467 } mdt_it_flavor[] = {
2468         [MDT_IT_OPEN]     = {
2469                 .it_fmt   = &RQF_LDLM_INTENT,
2470                 /*.it_flags = HABEO_REFERO,*/
2471                 .it_flags = 0,
2472                 .it_act   = mdt_intent_reint,
2473                 .it_reint = REINT_OPEN
2474         },
2475         [MDT_IT_OCREAT]   = {
2476                 .it_fmt   = &RQF_LDLM_INTENT,
2477                 .it_flags = MUTABOR,
2478                 .it_act   = mdt_intent_reint,
2479                 .it_reint = REINT_OPEN
2480         },
2481         [MDT_IT_CREATE]   = {
2482                 .it_fmt   = &RQF_LDLM_INTENT,
2483                 .it_flags = MUTABOR,
2484                 .it_act   = mdt_intent_reint,
2485                 .it_reint = REINT_CREATE
2486         },
2487         [MDT_IT_GETATTR]  = {
2488                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2489                 .it_flags = HABEO_REFERO,
2490                 .it_act   = mdt_intent_getattr
2491         },
2492         [MDT_IT_READDIR]  = {
2493                 .it_fmt   = NULL,
2494                 .it_flags = 0,
2495                 .it_act   = NULL
2496         },
2497         [MDT_IT_LOOKUP]   = {
2498                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2499                 .it_flags = HABEO_REFERO,
2500                 .it_act   = mdt_intent_getattr
2501         },
2502         [MDT_IT_UNLINK]   = {
2503                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
2504                 .it_flags = MUTABOR,
2505                 .it_act   = NULL,
2506                 .it_reint = REINT_UNLINK
2507         },
2508         [MDT_IT_TRUNC]    = {
2509                 .it_fmt   = NULL,
2510                 .it_flags = MUTABOR,
2511                 .it_act   = NULL
2512         },
2513         [MDT_IT_GETXATTR] = {
2514                 .it_fmt   = NULL,
2515                 .it_flags = 0,
2516                 .it_act   = NULL
2517         }
2518 };
2519
2520 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2521                             struct ldlm_lock **lockp,
2522                             struct ldlm_lock *new_lock,
2523                             struct mdt_lock_handle *lh,
2524                             int flags)
2525 {
2526         struct ptlrpc_request  *req = mdt_info_req(info);
2527         struct ldlm_lock       *lock = *lockp;
2528
2529         /*
2530          * Get new lock only for cases when possible resent did not find any
2531          * lock.
2532          */
2533         if (new_lock == NULL)
2534                 new_lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2535
2536         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2537                 lh->mlh_reg_lh.cookie = 0;
2538                 RETURN(0);
2539         }
2540
2541         LASSERTF(new_lock != NULL,
2542                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2543
2544         /*
2545          * If we've already given this lock to a client once, then we should
2546          * have no readers or writers.  Otherwise, we should have one reader
2547          * _or_ writer ref (which will be zeroed below) before returning the
2548          * lock to a client.
2549          */
2550         if (new_lock->l_export == req->rq_export) {
2551                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2552         } else {
2553                 LASSERT(new_lock->l_export == NULL);
2554                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2555         }
2556
2557         *lockp = new_lock;
2558
2559         if (new_lock->l_export == req->rq_export) {
2560                 /*
2561                  * Already gave this to the client, which means that we
2562                  * reconstructed a reply.
2563                  */
2564                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2565                         MSG_RESENT);
2566                 lh->mlh_reg_lh.cookie = 0;
2567                 RETURN(ELDLM_LOCK_REPLACED);
2568         }
2569
2570         /* This lock might already be given to the client by an resent req,
2571          * in this case we should return ELDLM_LOCK_ABORTED,
2572          * so we should check led_held_locks here, but it will affect
2573          * performance, FIXME
2574          */
2575         /* Fixup the lock to be given to the client */
2576         lock_res_and_lock(new_lock);
2577         new_lock->l_readers = 0;
2578         new_lock->l_writers = 0;
2579
2580         new_lock->l_export = class_export_get(req->rq_export);
2581         spin_lock(&req->rq_export->exp_ldlm_data.led_lock);
2582         list_add(&new_lock->l_export_chain,
2583                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
2584         spin_unlock(&req->rq_export->exp_ldlm_data.led_lock);
2585
2586         new_lock->l_blocking_ast = lock->l_blocking_ast;
2587         new_lock->l_completion_ast = lock->l_completion_ast;
2588         new_lock->l_remote_handle = lock->l_remote_handle;
2589         new_lock->l_flags &= ~LDLM_FL_LOCAL;
2590
2591         unlock_res_and_lock(new_lock);
2592         LDLM_LOCK_PUT(new_lock);
2593         lh->mlh_reg_lh.cookie = 0;
2594
2595         RETURN(ELDLM_LOCK_REPLACED);
2596 }
2597
2598 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2599                                     struct ldlm_lock *new_lock,
2600                                     struct ldlm_lock **old_lock,
2601                                     struct mdt_lock_handle *lh)
2602 {
2603         struct ptlrpc_request  *req = mdt_info_req(info);
2604         struct obd_export      *exp = req->rq_export;
2605         struct lustre_handle    remote_hdl;
2606         struct ldlm_request    *dlmreq;
2607         struct list_head       *iter;
2608
2609         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2610                 return;
2611
2612         dlmreq = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
2613         remote_hdl = dlmreq->lock_handle[0];
2614
2615         spin_lock(&exp->exp_ldlm_data.led_lock);
2616         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2617                 struct ldlm_lock *lock;
2618                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2619                 if (lock == new_lock)
2620                         continue;
2621                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2622                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2623                         lh->mlh_reg_mode = lock->l_granted_mode;
2624
2625                         LDLM_DEBUG(lock, "restoring lock cookie");
2626                         DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
2627                                   lh->mlh_reg_lh.cookie);
2628                         if (old_lock)
2629                                 *old_lock = LDLM_LOCK_GET(lock);
2630                         spin_unlock(&exp->exp_ldlm_data.led_lock);
2631                         return;
2632                 }
2633         }
2634         spin_unlock(&exp->exp_ldlm_data.led_lock);
2635
2636         /*
2637          * If the xid matches, then we know this is a resent request, and allow
2638          * it. (It's probably an OPEN, for which we don't send a lock.
2639          */
2640         if (req_xid_is_last(req))
2641                 return;
2642
2643         /*
2644          * This remote handle isn't enqueued, so we never received or processed
2645          * this request.  Clear MSG_RESENT, because it can be handled like any
2646          * normal request now.
2647          */
2648         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2649
2650         DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
2651                   remote_hdl.cookie);
2652 }
2653
2654 static int mdt_intent_getattr(enum mdt_it_code opcode,
2655                               struct mdt_thread_info *info,
2656                               struct ldlm_lock **lockp,
2657                               int flags)
2658 {
2659         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2660         struct ldlm_lock       *new_lock = NULL;
2661         __u64                   child_bits;
2662         struct ldlm_reply      *ldlm_rep;
2663         struct ptlrpc_request  *req;
2664         struct mdt_body        *reqbody;
2665         struct mdt_body        *repbody;
2666         int                     rc;
2667         ENTRY;
2668
2669         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
2670         LASSERT(reqbody);
2671
2672         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
2673         LASSERT(repbody);
2674
2675         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
2676         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
2677         repbody->eadatasize = 0;
2678         repbody->aclsize = 0;
2679
2680         switch (opcode) {
2681         case MDT_IT_LOOKUP:
2682                 child_bits = MDS_INODELOCK_LOOKUP;
2683                 break;
2684         case MDT_IT_GETATTR:
2685                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2686                 break;
2687         default:
2688                 CERROR("Unhandled till now");
2689                 GOTO(out, rc = -EINVAL);
2690         }
2691
2692         rc = mdt_init_ucred(info, reqbody);
2693         if (rc)
2694                 GOTO(out, rc);
2695
2696         req = info->mti_pill.rc_req;
2697         ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2698         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
2699
2700         /* Get lock from request for possible resent case. */
2701         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
2702
2703         ldlm_rep->lock_policy_res2 =
2704                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
2705
2706         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
2707                 ldlm_rep->lock_policy_res2 = 0;
2708         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
2709             ldlm_rep->lock_policy_res2) {
2710                 lhc->mlh_reg_lh.cookie = 0ull;
2711                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
2712         }
2713
2714         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
2715         EXIT;
2716 out_ucred:
2717         mdt_exit_ucred(info);
2718 out:
2719         mdt_shrink_reply(info);
2720         return rc;
2721 }
2722
2723 static int mdt_intent_reint(enum mdt_it_code opcode,
2724                             struct mdt_thread_info *info,
2725                             struct ldlm_lock **lockp,
2726                             int flags)
2727 {
2728         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2729         struct ldlm_reply      *rep = NULL;
2730         long                    opc;
2731         int                     rc;
2732
2733         static const struct req_format *intent_fmts[REINT_MAX] = {
2734                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
2735                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
2736         };
2737
2738         ENTRY;
2739
2740         opc = mdt_reint_opcode(info, intent_fmts);
2741         if (opc < 0)
2742                 GOTO(out, rc = opc);
2743
2744         if (mdt_it_flavor[opcode].it_reint != opc) {
2745                 CERROR("Reint code %ld doesn't match intent: %d\n",
2746                        opc, opcode);
2747                 GOTO(out, rc = err_serious(-EPROTO));
2748         }
2749
2750         /* Get lock from request for possible resent case. */
2751         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
2752
2753         rc = mdt_reint_internal(info, lhc, opc);
2754
2755         /* Check whether the reply has been packed successfully. */
2756         if (mdt_info_req(info)->rq_repmsg != NULL)
2757                 rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2758         if (rep == NULL)
2759                 GOTO(out, rc = err_serious(-EFAULT));
2760
2761         /* MDC expects this in any case */
2762         if (rc != 0)
2763                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
2764
2765         /* Cross-ref case, the lock should be returned to the client */
2766         if (rc == -EREMOTE) {
2767                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
2768                 rep->lock_policy_res2 = 0;
2769                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
2770                 GOTO(out, rc);
2771         }
2772         rep->lock_policy_res2 = clear_serious(rc);
2773
2774         lhc->mlh_reg_lh.cookie = 0ull;
2775         rc = ELDLM_LOCK_ABORTED;
2776         EXIT;
2777 out:
2778         return rc;
2779 }
2780
2781 static int mdt_intent_code(long itcode)
2782 {
2783         int rc;
2784
2785         switch(itcode) {
2786         case IT_OPEN:
2787                 rc = MDT_IT_OPEN;
2788                 break;
2789         case IT_OPEN|IT_CREAT:
2790                 rc = MDT_IT_OCREAT;
2791                 break;
2792         case IT_CREAT:
2793                 rc = MDT_IT_CREATE;
2794                 break;
2795         case IT_READDIR:
2796                 rc = MDT_IT_READDIR;
2797                 break;
2798         case IT_GETATTR:
2799                 rc = MDT_IT_GETATTR;
2800                 break;
2801         case IT_LOOKUP:
2802                 rc = MDT_IT_LOOKUP;
2803                 break;
2804         case IT_UNLINK:
2805                 rc = MDT_IT_UNLINK;
2806                 break;
2807         case IT_TRUNC:
2808                 rc = MDT_IT_TRUNC;
2809                 break;
2810         case IT_GETXATTR:
2811                 rc = MDT_IT_GETXATTR;
2812                 break;
2813         default:
2814                 CERROR("Unknown intent opcode: %ld\n", itcode);
2815                 rc = -EINVAL;
2816                 break;
2817         }
2818         return rc;
2819 }
2820
2821 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
2822                           struct ldlm_lock **lockp, int flags)
2823 {
2824         struct req_capsule   *pill;
2825         struct mdt_it_flavor *flv;
2826         int opc;
2827         int rc;
2828         ENTRY;
2829
2830         opc = mdt_intent_code(itopc);
2831         if (opc < 0)
2832                 RETURN(-EINVAL);
2833
2834         pill = &info->mti_pill;
2835         flv  = &mdt_it_flavor[opc];
2836
2837         if (flv->it_fmt != NULL)
2838                 req_capsule_extend(pill, flv->it_fmt);
2839
2840         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
2841         if (rc == 0) {
2842                 struct ptlrpc_request *req = mdt_info_req(info);
2843                 if (flv->it_flags & MUTABOR &&
2844                     req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2845                         rc = -EROFS;
2846         }
2847         if (rc == 0 && flv->it_act != NULL) {
2848                 /* execute policy */
2849                 rc = flv->it_act(opc, info, lockp, flags);
2850         } else
2851                 rc = -EOPNOTSUPP;
2852         RETURN(rc);
2853 }
2854
2855 static int mdt_intent_policy(struct ldlm_namespace *ns,
2856                              struct ldlm_lock **lockp, void *req_cookie,
2857                              ldlm_mode_t mode, int flags, void *data)
2858 {
2859         struct mdt_thread_info *info;
2860         struct ptlrpc_request  *req  =  req_cookie;
2861         struct ldlm_intent     *it;
2862         struct req_capsule     *pill;
2863         int rc;
2864
2865         ENTRY;
2866
2867         LASSERT(req != NULL);
2868
2869         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
2870                                   &mdt_thread_key);
2871         LASSERT(info != NULL);
2872         pill = &info->mti_pill;
2873         LASSERT(pill->rc_req == req);
2874
2875         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
2876                 req_capsule_extend(pill, &RQF_LDLM_INTENT);
2877                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
2878                 if (it != NULL) {
2879                         const struct ldlm_request *dlmreq;
2880                         __u64 req_bits;
2881 #if 0
2882                         struct ldlm_lock       *lock = *lockp;
2883
2884                         LDLM_DEBUG(lock, "intent policy opc: %s\n",
2885                                    ldlm_it2str(it->opc));
2886 #endif
2887
2888                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
2889                         if (rc == 0)
2890                                 rc = ELDLM_OK;
2891
2892                         /*
2893                          * Lock without inodebits makes no sense and will oops
2894                          * later in ldlm. Let's check it now to see if we have
2895                          * wrong lock from client or bits get corrupted
2896                          * somewhere in mdt_intent_opc().
2897                          */
2898                         dlmreq = info->mti_dlm_req;
2899                         req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
2900                         LASSERT(req_bits != 0);
2901
2902                 } else
2903                         rc = err_serious(-EFAULT);
2904         } else {
2905                 /* No intent was provided */
2906                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
2907                 rc = req_capsule_pack(pill);
2908                 if (rc)
2909                         rc = err_serious(rc);
2910         }
2911         RETURN(rc);
2912 }
2913
2914 /*
2915  * Seq wrappers
2916  */
2917 static void mdt_seq_adjust(const struct lu_env *env,
2918                           struct mdt_device *m, int lost)
2919 {
2920         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2921         struct lu_range out;
2922         ENTRY;
2923
2924         LASSERT(ls && ls->ls_server_seq);
2925         LASSERT(lost >= 0);
2926         /* get extra seq from seq_server, moving it's range up */
2927         while (lost-- > 0) {
2928                 seq_server_alloc_meta(ls->ls_server_seq, NULL, &out, env);
2929         }
2930         EXIT;
2931 }
2932
2933 static int mdt_seq_fini(const struct lu_env *env,
2934                         struct mdt_device *m)
2935 {
2936         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2937         ENTRY;
2938
2939         if (ls && ls->ls_server_seq) {
2940                 seq_server_fini(ls->ls_server_seq, env);
2941                 OBD_FREE_PTR(ls->ls_server_seq);
2942                 ls->ls_server_seq = NULL;
2943         }
2944
2945         if (ls && ls->ls_control_seq) {
2946                 seq_server_fini(ls->ls_control_seq, env);
2947                 OBD_FREE_PTR(ls->ls_control_seq);
2948                 ls->ls_control_seq = NULL;
2949         }
2950
2951         if (ls && ls->ls_client_seq) {
2952                 seq_client_fini(ls->ls_client_seq);
2953                 OBD_FREE_PTR(ls->ls_client_seq);
2954                 ls->ls_client_seq = NULL;
2955         }
2956
2957         RETURN(0);
2958 }
2959
2960 static int mdt_seq_init(const struct lu_env *env,
2961                         const char *uuid,
2962                         struct mdt_device *m)
2963 {
2964         struct lu_site *ls;
2965         char *prefix;
2966         int rc;
2967         ENTRY;
2968
2969         ls = m->mdt_md_dev.md_lu_dev.ld_site;
2970
2971         /*
2972          * This is sequence-controller node. Init seq-controller server on local
2973          * MDT.
2974          */
2975         if (ls->ls_node_id == 0) {
2976                 LASSERT(ls->ls_control_seq == NULL);
2977
2978                 OBD_ALLOC_PTR(ls->ls_control_seq);
2979                 if (ls->ls_control_seq == NULL)
2980                         RETURN(-ENOMEM);
2981
2982                 rc = seq_server_init(ls->ls_control_seq,
2983                                      m->mdt_bottom, uuid,
2984                                      LUSTRE_SEQ_CONTROLLER,
2985                                      env);
2986
2987                 if (rc)
2988                         GOTO(out_seq_fini, rc);
2989
2990                 OBD_ALLOC_PTR(ls->ls_client_seq);
2991                 if (ls->ls_client_seq == NULL)
2992                         GOTO(out_seq_fini, rc = -ENOMEM);
2993
2994                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
2995                 if (prefix == NULL) {
2996                         OBD_FREE_PTR(ls->ls_client_seq);
2997                         GOTO(out_seq_fini, rc = -ENOMEM);
2998                 }
2999
3000                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3001                          uuid);
3002
3003                 /*
3004                  * Init seq-controller client after seq-controller server is
3005                  * ready. Pass ls->ls_control_seq to it for direct talking.
3006                  */
3007                 rc = seq_client_init(ls->ls_client_seq, NULL,
3008                                      LUSTRE_SEQ_METADATA, prefix,
3009                                      ls->ls_control_seq);
3010                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3011
3012                 if (rc)
3013                         GOTO(out_seq_fini, rc);
3014         }
3015
3016         /* Init seq-server on local MDT */
3017         LASSERT(ls->ls_server_seq == NULL);
3018
3019         OBD_ALLOC_PTR(ls->ls_server_seq);
3020         if (ls->ls_server_seq == NULL)
3021                 GOTO(out_seq_fini, rc = -ENOMEM);
3022
3023         rc = seq_server_init(ls->ls_server_seq,
3024                              m->mdt_bottom, uuid,
3025                              LUSTRE_SEQ_SERVER,
3026                              env);
3027         if (rc)
3028                 GOTO(out_seq_fini, rc = -ENOMEM);
3029
3030         /* Assign seq-controller client to local seq-server. */
3031         if (ls->ls_node_id == 0) {
3032                 LASSERT(ls->ls_client_seq != NULL);
3033
3034                 rc = seq_server_set_cli(ls->ls_server_seq,
3035                                         ls->ls_client_seq,
3036                                         env);
3037         }
3038
3039         EXIT;
3040 out_seq_fini:
3041         if (rc)
3042                 mdt_seq_fini(env, m);
3043
3044         return rc;
3045 }
3046 /*
3047  * Init client sequence manager which is used by local MDS to talk to sequence
3048  * controller on remote node.
3049  */
3050 static int mdt_seq_init_cli(const struct lu_env *env,
3051                             struct mdt_device *m,
3052                             struct lustre_cfg *cfg)
3053 {
3054         struct lu_site    *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3055         struct obd_device *mdc;
3056         struct obd_uuid   *uuidp, *mdcuuidp;
3057         char              *uuid_str, *mdc_uuid_str;
3058         int                rc;
3059         int                index;
3060         struct mdt_thread_info *info;
3061         char *p, *index_string = lustre_cfg_string(cfg, 2);
3062         ENTRY;
3063
3064         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3065         uuidp = &info->mti_u.uuid[0];
3066         mdcuuidp = &info->mti_u.uuid[1];
3067
3068         LASSERT(index_string);
3069
3070         index = simple_strtol(index_string, &p, 10);
3071         if (*p) {
3072                 CERROR("Invalid index in lustre_cgf, offset 2\n");
3073                 RETURN(-EINVAL);
3074         }
3075
3076         /* check if this is adding the first MDC and controller is not yet
3077          * initialized. */
3078         if (index != 0 || ls->ls_client_seq)
3079                 RETURN(0);
3080
3081         uuid_str = lustre_cfg_string(cfg, 1);
3082         mdc_uuid_str = lustre_cfg_string(cfg, 4);
3083         obd_str2uuid(uuidp, uuid_str);
3084         obd_str2uuid(mdcuuidp, mdc_uuid_str);
3085
3086         mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3087         if (!mdc) {
3088                 CERROR("can't find controller MDC by uuid %s\n",
3089                        uuid_str);
3090                 rc = -ENOENT;
3091         } else if (!mdc->obd_set_up) {
3092                 CERROR("target %s not set up\n", mdc->obd_name);
3093                 rc = -EINVAL;
3094         } else {
3095                 LASSERT(ls->ls_control_exp);
3096                 OBD_ALLOC_PTR(ls->ls_client_seq);
3097                 if (ls->ls_client_seq != NULL) {
3098                         char *prefix;
3099
3100                         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3101                         if (!prefix)
3102                                 RETURN(-ENOMEM);
3103
3104                         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3105                                  mdc->obd_name);
3106
3107                         rc = seq_client_init(ls->ls_client_seq,
3108                                              ls->ls_control_exp,
3109                                              LUSTRE_SEQ_METADATA,
3110                                              prefix, NULL);
3111                         OBD_FREE(prefix, MAX_OBD_NAME + 5);
3112                 } else
3113                         rc = -ENOMEM;
3114
3115                 if (rc)
3116                         RETURN(rc);
3117
3118                 LASSERT(ls->ls_server_seq != NULL);
3119                 rc = seq_server_set_cli(ls->ls_server_seq, ls->ls_client_seq,
3120                                         env);
3121         }
3122
3123         RETURN(rc);
3124 }
3125
3126 static void mdt_seq_fini_cli(struct mdt_device *m)
3127 {
3128         struct lu_site *ls;
3129
3130         ENTRY;
3131
3132         ls = m->mdt_md_dev.md_lu_dev.ld_site;
3133
3134         if (ls && ls->ls_server_seq)
3135                 seq_server_set_cli(ls->ls_server_seq,
3136                                    NULL, NULL);
3137
3138         if (ls && ls->ls_control_exp) {
3139                 class_export_put(ls->ls_control_exp);
3140                 ls->ls_control_exp = NULL;
3141         }
3142         EXIT;
3143 }
3144
3145 /*
3146  * FLD wrappers
3147  */
3148 static int mdt_fld_fini(const struct lu_env *env,
3149                         struct mdt_device *m)
3150 {
3151         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3152         ENTRY;
3153
3154         if (ls && ls->ls_server_fld) {
3155                 fld_server_fini(ls->ls_server_fld, env);
3156                 OBD_FREE_PTR(ls->ls_server_fld);
3157                 ls->ls_server_fld = NULL;
3158         }
3159
3160         RETURN(0);
3161 }
3162
3163 static int mdt_fld_init(const struct lu_env *env,
3164                         const char *uuid,
3165                         struct mdt_device *m)
3166 {
3167         struct lu_site *ls;
3168         int rc;
3169         ENTRY;
3170
3171         ls = m->mdt_md_dev.md_lu_dev.ld_site;
3172
3173         OBD_ALLOC_PTR(ls->ls_server_fld);
3174         if (ls->ls_server_fld == NULL)
3175                 RETURN(rc = -ENOMEM);
3176
3177         rc = fld_server_init(ls->ls_server_fld,
3178                              m->mdt_bottom, uuid, env);
3179         if (rc) {
3180                 OBD_FREE_PTR(ls->ls_server_fld);
3181                 ls->ls_server_fld = NULL;
3182                 RETURN(rc);
3183         }
3184
3185         RETURN(0);
3186 }
3187
3188 /* device init/fini methods */
3189 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3190 {
3191         ENTRY;
3192         if (m->mdt_regular_service != NULL) {
3193                 ptlrpc_unregister_service(m->mdt_regular_service);
3194                 m->mdt_regular_service = NULL;
3195         }
3196         if (m->mdt_readpage_service != NULL) {
3197                 ptlrpc_unregister_service(m->mdt_readpage_service);
3198                 m->mdt_readpage_service = NULL;
3199         }
3200         if (m->mdt_xmds_service != NULL) {
3201                 ptlrpc_unregister_service(m->mdt_xmds_service);
3202                 m->mdt_xmds_service = NULL;
3203         }
3204         if (m->mdt_setattr_service != NULL) {
3205                 ptlrpc_unregister_service(m->mdt_setattr_service);
3206                 m->mdt_setattr_service = NULL;
3207         }
3208         if (m->mdt_mdsc_service != NULL) {
3209                 ptlrpc_unregister_service(m->mdt_mdsc_service);
3210                 m->mdt_mdsc_service = NULL;
3211         }
3212         if (m->mdt_mdss_service != NULL) {
3213                 ptlrpc_unregister_service(m->mdt_mdss_service);
3214                 m->mdt_mdss_service = NULL;
3215         }
3216         if (m->mdt_dtss_service != NULL) {
3217                 ptlrpc_unregister_service(m->mdt_dtss_service);
3218                 m->mdt_dtss_service = NULL;
3219         }
3220         if (m->mdt_fld_service != NULL) {
3221                 ptlrpc_unregister_service(m->mdt_fld_service);
3222                 m->mdt_fld_service = NULL;
3223         }
3224         ENTRY;
3225 }
3226
3227 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3228 {
3229         int rc;
3230         static struct ptlrpc_service_conf conf;
3231         cfs_proc_dir_entry_t *procfs_entry;
3232         ENTRY;
3233
3234         procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3235
3236         conf = (typeof(conf)) {
3237                 .psc_nbufs            = MDS_NBUFS,
3238                 .psc_bufsize          = MDS_BUFSIZE,
3239                 .psc_max_req_size     = MDS_MAXREQSIZE,
3240                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3241                 .psc_req_portal       = MDS_REQUEST_PORTAL,
3242                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3243                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3244                 /*
3245                  * We'd like to have a mechanism to set this on a per-device
3246                  * basis, but alas...
3247                  */
3248                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3249                                        MDT_MAX_THREADS),
3250                 .psc_max_threads   = MDT_MAX_THREADS,
3251                 .psc_ctx_tags      = LCT_MD_THREAD
3252         };
3253
3254         m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3255         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3256                            "mdt_ldlm_client", m->mdt_ldlm_client);
3257
3258         m->mdt_regular_service =
3259                 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3260                                      procfs_entry, NULL, LUSTRE_MDT_NAME);
3261         if (m->mdt_regular_service == NULL)
3262                 RETURN(-ENOMEM);
3263
3264         rc = ptlrpc_start_threads(NULL, m->mdt_regular_service);
3265         if (rc)
3266                 GOTO(err_mdt_svc, rc);
3267
3268         /*
3269          * readpage service configuration. Parameters have to be adjusted,
3270          * ideally.
3271          */
3272         conf = (typeof(conf)) {
3273                 .psc_nbufs            = MDS_NBUFS,
3274                 .psc_bufsize          = MDS_BUFSIZE,
3275                 .psc_max_req_size     = MDS_MAXREQSIZE,
3276                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3277                 .psc_req_portal       = MDS_READPAGE_PORTAL,
3278                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3279                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3280                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3281                                        MDT_MAX_THREADS),
3282                 .psc_max_threads   = MDT_MAX_THREADS,
3283                 .psc_ctx_tags      = LCT_MD_THREAD
3284         };
3285         m->mdt_readpage_service =
3286                 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3287                                      LUSTRE_MDT_NAME "_readpage",
3288                                      procfs_entry, NULL, "mdt_rdpg");
3289
3290         if (m->mdt_readpage_service == NULL) {
3291                 CERROR("failed to start readpage service\n");
3292                 GOTO(err_mdt_svc, rc = -ENOMEM);
3293         }
3294
3295         rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service);
3296
3297         /*
3298          * setattr service configuration.
3299          */
3300         conf = (typeof(conf)) {
3301                 .psc_nbufs            = MDS_NBUFS,
3302                 .psc_bufsize          = MDS_BUFSIZE,
3303                 .psc_max_req_size     = MDS_MAXREQSIZE,
3304                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3305                 .psc_req_portal       = MDS_SETATTR_PORTAL,
3306                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3307                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3308                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3309                                        MDT_MAX_THREADS),
3310                 .psc_max_threads   = MDT_MAX_THREADS,
3311                 .psc_ctx_tags      = LCT_MD_THREAD
3312         };
3313
3314         m->mdt_setattr_service =
3315                 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3316                                      LUSTRE_MDT_NAME "_setattr",
3317                                      procfs_entry, NULL, "mdt_attr");
3318
3319         if (!m->mdt_setattr_service) {
3320                 CERROR("failed to start setattr service\n");
3321                 GOTO(err_mdt_svc, rc = -ENOMEM);
3322         }
3323
3324         rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service);
3325         if (rc)
3326                 GOTO(err_mdt_svc, rc);
3327
3328         /*
3329          * sequence controller service configuration
3330          */
3331         conf = (typeof(conf)) {
3332                 .psc_nbufs = MDS_NBUFS,
3333                 .psc_bufsize = MDS_BUFSIZE,
3334                 .psc_max_req_size = SEQ_MAXREQSIZE,
3335                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3336                 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3337                 .psc_rep_portal = MDC_REPLY_PORTAL,
3338                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3339                 .psc_min_threads = SEQ_NUM_THREADS,
3340                 .psc_max_threads = SEQ_NUM_THREADS,
3341                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3342         };
3343
3344         m->mdt_mdsc_service =
3345                 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3346                                      LUSTRE_MDT_NAME"_mdsc",
3347                                      procfs_entry, NULL, "mdt_mdsc");
3348         if (!m->mdt_mdsc_service) {
3349                 CERROR("failed to start seq controller service\n");
3350                 GOTO(err_mdt_svc, rc = -ENOMEM);
3351         }
3352
3353         rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service);
3354         if (rc)
3355                 GOTO(err_mdt_svc, rc);
3356
3357         /*
3358          * metadata sequence server service configuration
3359          */
3360         conf = (typeof(conf)) {
3361                 .psc_nbufs = MDS_NBUFS,
3362                 .psc_bufsize = MDS_BUFSIZE,
3363                 .psc_max_req_size = SEQ_MAXREQSIZE,
3364                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3365                 .psc_req_portal = SEQ_METADATA_PORTAL,
3366                 .psc_rep_portal = MDC_REPLY_PORTAL,
3367                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3368                 .psc_min_threads = SEQ_NUM_THREADS,
3369                 .psc_max_threads = SEQ_NUM_THREADS,
3370                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3371         };
3372
3373         m->mdt_mdss_service =
3374                 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
3375                                      LUSTRE_MDT_NAME"_mdss",
3376                                      procfs_entry, NULL, "mdt_mdss");
3377         if (!m->mdt_mdss_service) {
3378                 CERROR("failed to start metadata seq server service\n");
3379                 GOTO(err_mdt_svc, rc = -ENOMEM);
3380         }
3381
3382         rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service);
3383         if (rc)
3384                 GOTO(err_mdt_svc, rc);
3385
3386
3387         /*
3388          * Data sequence server service configuration. We want to have really
3389          * cluster-wide sequences space. This is why we start only one sequence
3390          * controller which manages space.
3391          */
3392         conf = (typeof(conf)) {
3393                 .psc_nbufs = MDS_NBUFS,
3394                 .psc_bufsize = MDS_BUFSIZE,
3395                 .psc_max_req_size = SEQ_MAXREQSIZE,
3396                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3397                 .psc_req_portal = SEQ_DATA_PORTAL,
3398                 .psc_rep_portal = OSC_REPLY_PORTAL,
3399                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3400                 .psc_min_threads = SEQ_NUM_THREADS,
3401                 .psc_max_threads = SEQ_NUM_THREADS,
3402                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3403         };
3404
3405         m->mdt_dtss_service =
3406                 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
3407                                      LUSTRE_MDT_NAME"_dtss",
3408                                      procfs_entry, NULL, "mdt_dtss");
3409         if (!m->mdt_dtss_service) {
3410                 CERROR("failed to start data seq server service\n");
3411                 GOTO(err_mdt_svc, rc = -ENOMEM);
3412         }
3413
3414         rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service);
3415         if (rc)
3416                 GOTO(err_mdt_svc, rc);
3417
3418         /* FLD service start */
3419         conf = (typeof(conf)) {
3420                 .psc_nbufs            = MDS_NBUFS,
3421                 .psc_bufsize          = MDS_BUFSIZE,
3422                 .psc_max_req_size     = FLD_MAXREQSIZE,
3423                 .psc_max_reply_size   = FLD_MAXREPSIZE,
3424                 .psc_req_portal       = FLD_REQUEST_PORTAL,
3425                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3426                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3427                 .psc_min_threads      = FLD_NUM_THREADS,
3428                 .psc_max_threads      = FLD_NUM_THREADS,
3429                 .psc_ctx_tags         = LCT_DT_THREAD|LCT_MD_THREAD
3430         };
3431
3432         m->mdt_fld_service =
3433                 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
3434                                      LUSTRE_MDT_NAME"_fld",
3435                                      procfs_entry, NULL, "mdt_fld");
3436         if (!m->mdt_fld_service) {
3437                 CERROR("failed to start fld service\n");
3438                 GOTO(err_mdt_svc, rc = -ENOMEM);
3439         }
3440
3441         rc = ptlrpc_start_threads(NULL, m->mdt_fld_service);
3442         if (rc)
3443                 GOTO(err_mdt_svc, rc);
3444
3445         /*
3446          * mds-mds service configuration. Separate portal is used to allow
3447          * mds-mds requests be not blocked during recovery.
3448          */
3449         conf = (typeof(conf)) {
3450                 .psc_nbufs            = MDS_NBUFS,
3451                 .psc_bufsize          = MDS_BUFSIZE,
3452                 .psc_max_req_size     = MDS_MAXREQSIZE,
3453                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3454                 .psc_req_portal       = MDS_MDS_PORTAL,
3455                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3456                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3457                 .psc_min_threads      = min(max(mdt_num_threads, MDT_MIN_THREADS),
3458                                             MDT_MAX_THREADS),
3459                 .psc_max_threads      = MDT_MAX_THREADS,
3460                 .psc_ctx_tags         = LCT_MD_THREAD
3461         };
3462         m->mdt_xmds_service = ptlrpc_init_svc_conf(&conf, mdt_xmds_handle,
3463                                                   LUSTRE_MDT_NAME "_mds",
3464                                                   procfs_entry, NULL, "mdt_xmds");
3465
3466         if (m->mdt_xmds_service == NULL) {
3467                 CERROR("failed to start readpage service\n");
3468                 GOTO(err_mdt_svc, rc = -ENOMEM);
3469         }
3470
3471         rc = ptlrpc_start_threads(NULL, m->mdt_xmds_service);
3472         if (rc)
3473                 GOTO(err_mdt_svc, rc);
3474
3475         EXIT;
3476 err_mdt_svc:
3477         if (rc)
3478                 mdt_stop_ptlrpc_service(m);
3479
3480         return rc;
3481 }
3482
3483 static void mdt_stack_fini(const struct lu_env *env,
3484                            struct mdt_device *m, struct lu_device *top)
3485 {
3486         struct lu_device        *d = top, *n;
3487         struct obd_device       *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3488         struct lustre_cfg_bufs  *bufs;
3489         struct lustre_cfg       *lcfg;
3490         struct mdt_thread_info  *info;
3491         char flags[3]="";
3492         ENTRY;
3493
3494         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3495         LASSERT(info != NULL);
3496
3497         bufs = &info->mti_u.bufs;
3498         /* process cleanup, pass mdt obd name to get obd umount flags */
3499         lustre_cfg_bufs_reset(bufs, obd->obd_name);
3500         if (obd->obd_force)
3501                 strcat(flags, "F");
3502         if (obd->obd_fail)
3503                 strcat(flags, "A");
3504         lustre_cfg_bufs_set_string(bufs, 1, flags);
3505         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3506         if (!lcfg) {
3507                 CERROR("Cannot alloc lcfg!\n");
3508                 return;
3509         }
3510
3511         LASSERT(top);
3512         top->ld_ops->ldo_process_config(env, top, lcfg);
3513         lustre_cfg_free(lcfg);
3514
3515         lu_site_purge(env, top->ld_site, ~0);
3516         while (d != NULL) {
3517                 struct obd_type *type;
3518                 struct lu_device_type *ldt = d->ld_type;
3519
3520                 /* each fini() returns next device in stack of layers
3521                  * * so we can avoid the recursion */
3522                 n = ldt->ldt_ops->ldto_device_fini(env, d);
3523                 lu_device_put(d);
3524                 ldt->ldt_ops->ldto_device_free(env, d);
3525                 type = ldt->ldt_obd_type;
3526                 type->typ_refcnt--;
3527                 class_put_type(type);
3528
3529                 /* switch to the next device in the layer */
3530                 d = n;
3531         }
3532         m->mdt_child = NULL;
3533         m->mdt_bottom = NULL;
3534 }
3535
3536 static struct lu_device *mdt_layer_setup(const struct lu_env *env,
3537                                          const char *typename,
3538                                          struct lu_device *child,
3539                                          struct lustre_cfg *cfg)
3540 {
3541         const char            *dev = lustre_cfg_string(cfg, 0);
3542         struct obd_type       *type;
3543         struct lu_device_type *ldt;
3544         struct lu_device      *d;
3545         int rc;
3546         ENTRY;
3547
3548         /* find the type */
3549         type = class_get_type(typename);
3550         if (!type) {
3551                 CERROR("Unknown type: '%s'\n", typename);
3552                 GOTO(out, rc = -ENODEV);
3553         }
3554
3555         rc = lu_context_refill(&env->le_ctx);
3556         if (rc != 0) {
3557                 CERROR("Failure to refill context: '%d'\n", rc);
3558                 GOTO(out_type, rc);
3559         }
3560
3561         if (env->le_ses != NULL) {
3562                 rc = lu_context_refill(env->le_ses);
3563                 if (rc != 0) {
3564                         CERROR("Failure to refill session: '%d'\n", rc);
3565                         GOTO(out_type, rc);
3566                 }
3567         }
3568
3569         ldt = type->typ_lu;
3570         if (ldt == NULL) {
3571                 CERROR("type: '%s'\n", typename);
3572                 GOTO(out_type, rc = -EINVAL);
3573         }
3574
3575         ldt->ldt_obd_type = type;
3576         d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
3577         if (IS_ERR(d)) {
3578                 CERROR("Cannot allocate device: '%s'\n", typename);
3579                 GOTO(out_type, rc = -ENODEV);
3580         }
3581
3582         LASSERT(child->ld_site);
3583         d->ld_site = child->ld_site;
3584
3585         type->typ_refcnt++;
3586         rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
3587         if (rc) {
3588                 CERROR("can't init device '%s', rc %d\n", typename, rc);
3589                 GOTO(out_alloc, rc);
3590         }
3591         lu_device_get(d);
3592
3593         RETURN(d);
3594
3595 out_alloc:
3596         ldt->ldt_ops->ldto_device_free(env, d);
3597         type->typ_refcnt--;
3598 out_type:
3599         class_put_type(type);
3600 out:
3601         return ERR_PTR(rc);
3602 }
3603
3604 static int mdt_stack_init(const struct lu_env *env,
3605                           struct mdt_device *m, struct lustre_cfg *cfg)
3606 {
3607         struct lu_device  *d = &m->mdt_md_dev.md_lu_dev;
3608         struct lu_device  *tmp;
3609         struct md_device  *md;
3610         int rc;
3611         ENTRY;
3612
3613         /* init the stack */
3614         tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
3615         if (IS_ERR(tmp)) {
3616                 RETURN(PTR_ERR(tmp));
3617         }
3618         m->mdt_bottom = lu2dt_dev(tmp);
3619         d = tmp;
3620         tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
3621         if (IS_ERR(tmp)) {
3622                 GOTO(out, rc = PTR_ERR(tmp));
3623         }
3624         d = tmp;
3625         md = lu2md_dev(d);
3626
3627         tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
3628         if (IS_ERR(tmp)) {
3629                 GOTO(out, rc = PTR_ERR(tmp));
3630         }
3631         d = tmp;
3632         /*set mdd upcall device*/
3633         md_upcall_dev_set(md, lu2md_dev(d));
3634
3635         md = lu2md_dev(d);
3636         /*set cmm upcall device*/
3637         md_upcall_dev_set(md, &m->mdt_md_dev);
3638
3639         m->mdt_child = lu2md_dev(d);
3640
3641         /* process setup config */
3642         tmp = &m->mdt_md_dev.md_lu_dev;
3643         rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
3644         GOTO(out, rc);
3645 out:
3646         /* fini from last known good lu_device */
3647         if (rc)
3648                 mdt_stack_fini(env, m, d);
3649
3650         return rc;
3651 }
3652
3653 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
3654 {
3655         struct md_device *next = m->mdt_child;
3656         struct lu_device *d    = &m->mdt_md_dev.md_lu_dev;
3657         struct lu_site   *ls   = d->ld_site;
3658         struct obd_device *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3659         ENTRY;
3660
3661         ping_evictor_stop();
3662         
3663         target_recovery_fini(obd);
3664         mdt_stop_ptlrpc_service(m);
3665
3666         mdt_fs_cleanup(env, m);
3667
3668         upcall_cache_cleanup(m->mdt_rmtacl_cache);
3669         m->mdt_rmtacl_cache = NULL;
3670
3671         upcall_cache_cleanup(m->mdt_identity_cache);
3672         m->mdt_identity_cache = NULL;
3673
3674         if (m->mdt_namespace != NULL) {
3675                 ldlm_namespace_free(m->mdt_namespace, d->ld_obd->obd_force);
3676                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
3677         }
3678
3679         mdt_seq_fini(env, m);
3680         mdt_seq_fini_cli(m);
3681         mdt_fld_fini(env, m);
3682         mdt_procfs_fini(m);
3683         ptlrpc_lprocfs_unregister_obd(d->ld_obd);
3684         lprocfs_obd_cleanup(d->ld_obd);
3685
3686         if (m->mdt_rootsquash_info) {
3687                 OBD_FREE_PTR(m->mdt_rootsquash_info);
3688                 m->mdt_rootsquash_info = NULL;
3689         }
3690
3691         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
3692         del_timer(&m->mdt_ck_timer);
3693         mdt_ck_thread_stop(m);
3694
3695         /* finish the stack */
3696         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3697
3698         if (ls) {
3699                 if (!list_empty(&ls->ls_lru) || ls->ls_total != 0) {
3700                         /*
3701                          * Uh-oh, objects still exist.
3702                          */
3703                         static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
3704
3705                         lu_site_print(env, ls, &cookie, lu_cdebug_printer);
3706                 }
3707
3708                 lu_site_fini(ls);
3709                 OBD_FREE_PTR(ls);
3710                 d->ld_site = NULL;
3711         }
3712         LASSERT(atomic_read(&d->ld_ref) == 0);
3713         md_device_fini(&m->mdt_md_dev);
3714
3715         EXIT;
3716 }
3717
3718 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
3719 {
3720         char *p = options;
3721
3722         if (!options)
3723                 return;
3724
3725         while (*options) {
3726                 int len;
3727
3728                 while (*p && *p != ',')
3729                         p++;
3730
3731                 len = p - options;
3732                 if ((len == sizeof("user_xattr") - 1) &&
3733                     (memcmp(options, "user_xattr", len) == 0)) {
3734                         m->mdt_opts.mo_user_xattr = 1;
3735                         LCONSOLE_INFO("Enabling user_xattr\n");
3736                 } else if ((len == sizeof("nouser_xattr") - 1) &&
3737                            (memcmp(options, "nouser_xattr", len) == 0)) {
3738                         m->mdt_opts.mo_user_xattr = 0;
3739                         LCONSOLE_INFO("Disabling user_xattr\n");
3740                 } else if ((len == sizeof("acl") - 1) &&
3741                            (memcmp(options, "acl", len) == 0)) {
3742 #ifdef CONFIG_FS_POSIX_ACL
3743                         m->mdt_opts.mo_acl = 1;
3744                         LCONSOLE_INFO("Enabling ACL\n");
3745 #else
3746                         m->mdt_opts.mo_acl = 0;
3747                         CWARN("ignoring unsupported acl mount option\n");
3748                         LCONSOLE_INFO("Disabling ACL\n");
3749 #endif
3750                 } else if ((len == sizeof("noacl") - 1) &&
3751                            (memcmp(options, "noacl", len) == 0)) {
3752                         m->mdt_opts.mo_acl = 0;
3753                         LCONSOLE_INFO("Disabling ACL\n");
3754                 }
3755
3756                 options = ++p;
3757         }
3758 }
3759
3760 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
3761
3762 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
3763                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
3764 {
3765         struct lprocfs_static_vars lvars;
3766         struct mdt_thread_info    *info;
3767         struct obd_device         *obd;
3768         const char                *dev = lustre_cfg_string(cfg, 0);
3769         const char                *num = lustre_cfg_string(cfg, 2);
3770         struct lustre_mount_info  *lmi;
3771         struct lustre_sb_info     *lsi;
3772         struct lu_site            *s;
3773         int                        rc;
3774         ENTRY;
3775
3776         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3777         LASSERT(info != NULL);
3778
3779         obd = class_name2obd(dev);
3780         LASSERT(obd != NULL);
3781
3782         spin_lock_init(&m->mdt_transno_lock);
3783
3784         m->mdt_max_mdsize = MAX_MD_SIZE;
3785         m->mdt_max_cookiesize = sizeof(struct llog_cookie);
3786
3787         m->mdt_opts.mo_user_xattr = 0;
3788         m->mdt_opts.mo_acl = 0;
3789         lmi = server_get_mount_2(dev);
3790         if (lmi == NULL) {
3791                 CERROR("Cannot get mount info for %s!\n", dev);
3792                 RETURN(-EFAULT);
3793         } else {
3794                 lsi = s2lsi(lmi->lmi_sb);
3795                 fsoptions_to_mdt_flags(m, lsi->lsi_lmd->lmd_opts);
3796                 server_put_mount_2(dev, lmi->lmi_mnt);
3797         }
3798
3799         spin_lock_init(&m->mdt_ioepoch_lock);
3800         m->mdt_opts.mo_compat_resname = 0;
3801         m->mdt_capa_timeout = CAPA_TIMEOUT;
3802         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
3803         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
3804
3805         spin_lock_init(&m->mdt_client_bitmap_lock);
3806
3807         OBD_ALLOC_PTR(s);
3808         if (s == NULL)
3809                 RETURN(-ENOMEM);
3810
3811         md_device_init(&m->mdt_md_dev, ldt);
3812         m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
3813         m->mdt_md_dev.md_lu_dev.ld_obd = obd;
3814         /* set this lu_device to obd, because error handling need it */
3815         obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
3816
3817         rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
3818         if (rc) {
3819                 CERROR("Can't init lu_site, rc %d\n", rc);
3820                 GOTO(err_free_site, rc);
3821         }
3822
3823         lprocfs_init_vars(mdt, &lvars);
3824         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
3825         if (rc) {
3826                 CERROR("Can't init lprocfs, rc %d\n", rc);
3827                 GOTO(err_fini_site, rc);
3828         }
3829         ptlrpc_lprocfs_register_obd(obd);
3830
3831         rc = mdt_procfs_init(m, dev);
3832         if (rc) {
3833                 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
3834                 GOTO(err_fini_proc, rc);
3835         }
3836
3837         /* set server index */
3838         LASSERT(num);
3839         s->ls_node_id = simple_strtol(num, NULL, 10);
3840
3841         /* failover is the default
3842          * FIXME: we do not failout mds0/mgs, which may cause some problems.
3843          * assumed whose ls_node_id == 0 XXX
3844          * */
3845         obd->obd_replayable = 1;
3846         /* No connection accepted until configurations will finish */
3847         obd->obd_no_conn = 1;
3848
3849         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
3850                 char *str = lustre_cfg_string(cfg, 4);
3851                 if (strchr(str, 'n')) {
3852                         CWARN("%s: recovery disabled\n", obd->obd_name);
3853                         obd->obd_replayable = 0;
3854                 }
3855         }
3856
3857         /* init the stack */
3858         rc = mdt_stack_init(env, m, cfg);
3859         if (rc) {
3860                 CERROR("Can't init device stack, rc %d\n", rc);
3861                 GOTO(err_fini_proc, rc);
3862         }
3863
3864         rc = mdt_fld_init(env, obd->obd_name, m);
3865         if (rc)
3866                 GOTO(err_fini_stack, rc);
3867
3868         rc = mdt_seq_init(env, obd->obd_name, m);
3869         if (rc)
3870                 GOTO(err_fini_fld, rc);
3871
3872         snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
3873                  LUSTRE_MDT_NAME"-%p", m);
3874         m->mdt_namespace = ldlm_namespace_new(info->mti_u.ns_name,
3875                                               LDLM_NAMESPACE_SERVER,
3876                                               LDLM_NAMESPACE_GREEDY);
3877         if (m->mdt_namespace == NULL)
3878                 GOTO(err_fini_seq, rc = -ENOMEM);
3879
3880         ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
3881         /* set obd_namespace for compatibility with old code */
3882         obd->obd_namespace = m->mdt_namespace;
3883
3884         m->mdt_identity_cache = upcall_cache_init(obd->obd_name,
3885                                                   "NONE",
3886                                                   &mdt_identity_upcall_cache_ops);
3887         if (IS_ERR(m->mdt_identity_cache)) {
3888                 rc = PTR_ERR(m->mdt_identity_cache);
3889                 m->mdt_identity_cache = NULL;
3890                 GOTO(err_free_ns, rc);
3891         }
3892
3893         m->mdt_rmtacl_cache = upcall_cache_init(obd->obd_name,
3894                                                 MDT_RMTACL_UPCALL_PATH,
3895                                                 &mdt_rmtacl_upcall_cache_ops);
3896         if (IS_ERR(m->mdt_rmtacl_cache)) {
3897                 rc = PTR_ERR(m->mdt_rmtacl_cache);
3898                 m->mdt_rmtacl_cache = NULL;
3899                 GOTO(err_free_ns, rc);
3900         }
3901
3902         m->mdt_ck_timer.function = mdt_ck_timer_callback;
3903         m->mdt_ck_timer.data = (unsigned long)m;
3904         init_timer(&m->mdt_ck_timer);
3905         rc = mdt_ck_thread_start(m);
3906         if (rc)
3907                 GOTO(err_free_ns, rc);
3908
3909         rc = mdt_fs_setup(env, m, obd);
3910         if (rc)
3911                 GOTO(err_capa, rc);
3912
3913         target_recovery_init(obd, mdt_recovery_handle);
3914
3915         rc = mdt_start_ptlrpc_service(m);
3916         if (rc)
3917                 GOTO(err_fs_cleanup, rc);
3918
3919         ping_evictor_start();
3920
3921         rc = lu_site_init_finish(s);
3922         if (rc)
3923                 GOTO(err_stop_service, rc);
3924
3925         if (obd->obd_recovering == 0)
3926                 mdt_postrecov(env, m);
3927
3928         mdt_init_capa_ctxt(env, m);
3929
3930         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
3931                 ldlm_timeout = 6;
3932
3933         RETURN(0);
3934
3935 err_stop_service:
3936         ping_evictor_stop();
3937         mdt_stop_ptlrpc_service(m);
3938 err_fs_cleanup:
3939         target_recovery_fini(obd);
3940         mdt_fs_cleanup(env, m);
3941 err_capa:
3942         del_timer(&m->mdt_ck_timer);
3943         mdt_ck_thread_stop(m);
3944 err_free_ns:
3945         upcall_cache_cleanup(m->mdt_rmtacl_cache);
3946         m->mdt_rmtacl_cache = NULL;
3947         upcall_cache_cleanup(m->mdt_identity_cache);
3948         m->mdt_identity_cache = NULL;
3949         ldlm_namespace_free(m->mdt_namespace, 0);
3950         obd->obd_namespace = m->mdt_namespace = NULL;
3951 err_fini_seq:
3952         mdt_seq_fini(env, m);
3953 err_fini_fld:
3954         mdt_fld_fini(env, m);
3955 err_fini_stack:
3956         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3957 err_fini_proc:
3958         mdt_procfs_fini(m);
3959         lprocfs_obd_cleanup(obd);
3960 err_fini_site:
3961         lu_site_fini(s);
3962 err_free_site:
3963         OBD_FREE_PTR(s);
3964
3965         md_device_fini(&m->mdt_md_dev);
3966         return (rc);
3967 }
3968
3969 /* used by MGS to process specific configurations */
3970 static int mdt_process_config(const struct lu_env *env,
3971                               struct lu_device *d, struct lustre_cfg *cfg)
3972 {
3973         struct mdt_device *m = mdt_dev(d);
3974         struct md_device *md_next = m->mdt_child;
3975         struct lu_device *next = md2lu_dev(md_next);
3976         int rc = 0;
3977         ENTRY;
3978
3979         switch (cfg->lcfg_command) {
3980         case LCFG_PARAM: {
3981                 struct lprocfs_static_vars lvars;
3982                 struct obd_device *obd = d->ld_obd;
3983
3984                 lprocfs_init_vars(mdt, &lvars);
3985                 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars, cfg, obd);
3986                 if (rc)
3987                         /* others are passed further */
3988                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
3989                 break;
3990         }
3991         case LCFG_ADD_MDC:
3992                 /*
3993                  * Add mdc hook to get first MDT uuid and connect it to
3994                  * ls->controller to use for seq manager.
3995                  */
3996                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
3997                 if (rc)
3998                         CERROR("Can't add mdc, rc %d\n", rc);
3999                 else
4000                         rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
4001                 break;
4002         default:
4003                 /* others are passed further */
4004                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4005                 break;
4006         }
4007         RETURN(rc);
4008 }
4009
4010 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4011                                           const struct lu_object_header *hdr,
4012                                           struct lu_device *d)
4013 {
4014         struct mdt_object *mo;
4015
4016         ENTRY;
4017
4018         OBD_ALLOC_PTR(mo);
4019         if (mo != NULL) {
4020                 struct lu_object *o;
4021                 struct lu_object_header *h;
4022
4023                 o = &mo->mot_obj.mo_lu;
4024                 h = &mo->mot_header;
4025                 lu_object_header_init(h);
4026                 lu_object_init(o, h, d);
4027                 lu_object_add_top(h, o);
4028                 o->lo_ops = &mdt_obj_ops;
4029                 RETURN(o);
4030         } else
4031                 RETURN(NULL);
4032 }
4033
4034 static int mdt_object_init(const struct lu_env *env, struct lu_object *o)
4035 {
4036         struct mdt_device *d = mdt_dev(o->lo_dev);
4037         struct lu_device  *under;
4038         struct lu_object  *below;
4039         int                rc = 0;
4040         ENTRY;
4041
4042         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4043                PFID(lu_object_fid(o)));
4044
4045         under = &d->mdt_child->md_lu_dev;
4046         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4047         if (below != NULL) {
4048                 lu_object_add(o, below);
4049         } else
4050                 rc = -ENOMEM;
4051
4052         RETURN(rc);
4053 }
4054
4055 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4056 {
4057         struct mdt_object *mo = mdt_obj(o);
4058         struct lu_object_header *h;
4059         ENTRY;
4060
4061         h = o->lo_header;
4062         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4063                PFID(lu_object_fid(o)));
4064
4065         lu_object_fini(o);
4066         lu_object_header_fini(h);
4067         OBD_FREE_PTR(mo);
4068         EXIT;
4069 }
4070
4071 static int mdt_object_print(const struct lu_env *env, void *cookie,
4072                             lu_printer_t p, const struct lu_object *o)
4073 {
4074         return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p", o);
4075 }
4076
4077 static struct lu_device_operations mdt_lu_ops = {
4078         .ldo_object_alloc   = mdt_object_alloc,
4079         .ldo_process_config = mdt_process_config
4080 };
4081
4082 static struct lu_object_operations mdt_obj_ops = {
4083         .loo_object_init    = mdt_object_init,
4084         .loo_object_free    = mdt_object_free,
4085         .loo_object_print   = mdt_object_print
4086 };
4087
4088 /* mds_connect_internal */
4089 static int mdt_connect_internal(struct obd_export *exp,
4090                                 struct mdt_device *mdt,
4091                                 struct obd_connect_data *data)
4092 {
4093         __u64 flags;
4094
4095         if (data != NULL) {
4096                 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4097                 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4098
4099                 /* If no known bits (which should not happen, probably,
4100                    as everybody should support LOOKUP and UPDATE bits at least)
4101                    revert to compat mode with plain locks. */
4102                 if (!data->ocd_ibits_known &&
4103                     data->ocd_connect_flags & OBD_CONNECT_IBITS)
4104                         data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4105
4106                 if (!mdt->mdt_opts.mo_acl)
4107                         data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4108
4109                 if (!mdt->mdt_opts.mo_user_xattr)
4110                         data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4111
4112                 if (!mdt->mdt_opts.mo_mds_capa)
4113                         data->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
4114
4115                 if (!mdt->mdt_opts.mo_oss_capa)
4116                         data->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
4117
4118                 spin_lock(&exp->exp_lock);
4119                 exp->exp_connect_flags = data->ocd_connect_flags;
4120                 spin_unlock(&exp->exp_lock);
4121                 data->ocd_version = LUSTRE_VERSION_CODE;
4122                 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
4123         }
4124
4125 #if 0
4126         if (mdt->mdt_opts.mo_acl &&
4127             ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
4128                 CWARN("%s: MDS requires ACL support but client does not\n",
4129                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4130                 return -EBADE;
4131         }
4132 #endif
4133
4134         flags = OBD_CONNECT_LCL_CLIENT | OBD_CONNECT_RMT_CLIENT;
4135         if ((exp->exp_connect_flags & flags) == flags) {
4136                 CWARN("%s: both local and remote client flags are set\n",
4137                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4138                 return -EBADE;
4139         }
4140
4141         if (mdt->mdt_opts.mo_mds_capa &&
4142             ((exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) == 0)) {
4143                 CWARN("%s: MDS requires capability support, but client not\n",
4144                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4145                 return -EBADE;
4146         }
4147
4148         if (mdt->mdt_opts.mo_oss_capa &&
4149             ((exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA) == 0)) {
4150                 CWARN("%s: MDS requires OSS capability support, "
4151                       "but client not\n",
4152                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4153                 return -EBADE;
4154         }
4155
4156         return 0;
4157 }
4158
4159 /* mds_connect copy */
4160 static int mdt_obd_connect(const struct lu_env *env,
4161                            struct lustre_handle *conn, struct obd_device *obd,
4162                            struct obd_uuid *cluuid,
4163                            struct obd_connect_data *data)
4164 {
4165         struct mdt_client_data *mcd;
4166         struct obd_export      *exp;
4167         struct mdt_device      *mdt;
4168         int                     rc;
4169         ENTRY;
4170
4171         LASSERT(env != NULL);
4172         if (!conn || !obd || !cluuid)
4173                 RETURN(-EINVAL);
4174
4175         mdt = mdt_dev(obd->obd_lu_dev);
4176
4177         rc = class_connect(conn, obd, cluuid);
4178         if (rc)
4179                 RETURN(rc);
4180
4181         exp = class_conn2export(conn);
4182         LASSERT(exp != NULL);
4183
4184         rc = mdt_connect_internal(exp, mdt, data);
4185         if (rc == 0) {
4186                 OBD_ALLOC_PTR(mcd);
4187                 if (mcd != NULL) {
4188                         struct mdt_thread_info *mti;
4189                         mti = lu_context_key_get(&env->le_ctx,
4190                                                  &mdt_thread_key);
4191                         LASSERT(mti != NULL);
4192                         mti->mti_exp = exp;
4193                         memcpy(mcd->mcd_uuid, cluuid, sizeof mcd->mcd_uuid);
4194                         exp->exp_mdt_data.med_mcd = mcd;
4195                         rc = mdt_client_new(env, mdt);
4196                         if (rc != 0) {
4197                                 OBD_FREE_PTR(mcd);
4198                                 exp->exp_mdt_data.med_mcd = NULL;
4199                         }
4200                 } else
4201                         rc = -ENOMEM;
4202         }
4203
4204         if (rc != 0)
4205                 class_disconnect(exp);
4206         else
4207                 class_export_put(exp);
4208
4209         RETURN(rc);
4210 }
4211
4212 static int mdt_obd_reconnect(struct obd_export *exp, struct obd_device *obd,
4213                              struct obd_uuid *cluuid,
4214                              struct obd_connect_data *data)
4215 {
4216         int rc;
4217         ENTRY;
4218
4219         if (exp == NULL || obd == NULL || cluuid == NULL)
4220                 RETURN(-EINVAL);
4221
4222         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
4223
4224         RETURN(rc);
4225 }
4226
4227 static int mdt_obd_disconnect(struct obd_export *exp)
4228 {
4229         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
4230         int rc;
4231         ENTRY;
4232
4233         LASSERT(exp);
4234         class_export_get(exp);
4235
4236         /* Disconnect early so that clients can't keep using export */
4237         rc = class_disconnect(exp);
4238         if (mdt->mdt_namespace != NULL || exp->exp_obd->obd_namespace != NULL)
4239                 ldlm_cancel_locks_for_export(exp);
4240
4241         /* complete all outstanding replies */
4242         spin_lock(&exp->exp_lock);
4243         while (!list_empty(&exp->exp_outstanding_replies)) {
4244                 struct ptlrpc_reply_state *rs =
4245                         list_entry(exp->exp_outstanding_replies.next,
4246                                    struct ptlrpc_reply_state, rs_exp_list);
4247                 struct ptlrpc_service *svc = rs->rs_service;
4248
4249                 spin_lock(&svc->srv_lock);
4250                 list_del_init(&rs->rs_exp_list);
4251                 ptlrpc_schedule_difficult_reply(rs);
4252                 spin_unlock(&svc->srv_lock);
4253         }
4254         spin_unlock(&exp->exp_lock);
4255
4256         class_export_put(exp);
4257         RETURN(rc);
4258 }
4259
4260 /* FIXME: Can we avoid using these two interfaces? */
4261 static int mdt_init_export(struct obd_export *exp)
4262 {
4263         struct mdt_export_data *med = &exp->exp_mdt_data;
4264         ENTRY;
4265
4266         INIT_LIST_HEAD(&med->med_open_head);
4267         spin_lock_init(&med->med_open_lock);
4268         spin_lock(&exp->exp_lock);
4269         exp->exp_connecting = 1;
4270         spin_unlock(&exp->exp_lock);
4271         RETURN(0);
4272 }
4273
4274 static int mdt_destroy_export(struct obd_export *export)
4275 {
4276         struct mdt_export_data *med;
4277         struct obd_device      *obd = export->exp_obd;
4278         struct mdt_device      *mdt;
4279         struct mdt_thread_info *info;
4280         struct lu_env           env;
4281         struct md_attr         *ma;
4282         int lmm_size;
4283         int cookie_size;
4284         int rc = 0;
4285         ENTRY;
4286
4287         med = &export->exp_mdt_data;
4288         if (med->med_rmtclient)
4289                 mdt_cleanup_idmap(med);
4290
4291         target_destroy_export(export);
4292
4293         if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
4294                 RETURN(0);
4295
4296         mdt = mdt_dev(obd->obd_lu_dev);
4297         LASSERT(mdt != NULL);
4298
4299         rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4300         if (rc)
4301                 RETURN(rc);
4302
4303         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
4304         LASSERT(info != NULL);
4305         memset(info, 0, sizeof *info);
4306         info->mti_env = &env;
4307         info->mti_mdt = mdt;
4308         info->mti_exp = export;
4309
4310         ma = &info->mti_attr;
4311         lmm_size = ma->ma_lmm_size = mdt->mdt_max_mdsize;
4312         cookie_size = ma->ma_cookie_size = mdt->mdt_max_cookiesize;
4313         OBD_ALLOC(ma->ma_lmm, lmm_size);
4314         OBD_ALLOC(ma->ma_cookie, cookie_size);
4315
4316         if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
4317                 GOTO(out, rc = -ENOMEM);
4318         ma->ma_need = MA_LOV | MA_COOKIE;
4319         ma->ma_valid = 0;
4320         /* Close any open files (which may also cause orphan unlinking). */
4321         spin_lock(&med->med_open_lock);
4322         while (!list_empty(&med->med_open_head)) {
4323                 struct list_head *tmp = med->med_open_head.next;
4324                 struct mdt_file_data *mfd =
4325                         list_entry(tmp, struct mdt_file_data, mfd_list);
4326
4327                 /* Remove mfd handle so it can't be found again.
4328                  * We are consuming the mfd_list reference here. */
4329                 class_handle_unhash(&mfd->mfd_handle);
4330                 list_del_init(&mfd->mfd_list);
4331                 spin_unlock(&med->med_open_lock);
4332                 mdt_mfd_close(info, mfd);
4333                 /* TODO: if we close the unlinked file,
4334                  * we need to remove it's objects from OST */
4335                 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
4336                 spin_lock(&med->med_open_lock);
4337                 ma->ma_lmm_size = lmm_size;
4338                 ma->ma_cookie_size = cookie_size;
4339                 ma->ma_need = MA_LOV | MA_COOKIE;
4340                 ma->ma_valid = 0;
4341         }
4342         spin_unlock(&med->med_open_lock);
4343         info->mti_mdt = NULL;
4344         mdt_client_del(&env, mdt);
4345
4346         EXIT;
4347 out:
4348         if (lmm_size) {
4349                 OBD_FREE(ma->ma_lmm, lmm_size);
4350                 ma->ma_lmm = NULL;
4351         }
4352         if (cookie_size) {
4353                 OBD_FREE(ma->ma_cookie, cookie_size);
4354                 ma->ma_cookie = NULL;
4355         }
4356         lu_env_fini(&env);
4357
4358         return rc;
4359 }
4360
4361 static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
4362 {
4363         if (flag & CONFIG_LOG)
4364                 m->mdt_fl_cfglog = 1;
4365         if (flag & CONFIG_SYNC)
4366                 m->mdt_fl_synced = 1;
4367
4368         if (m->mdt_fl_cfglog && m->mdt_fl_synced)
4369                 /* Open for clients */
4370                 m->mdt_md_dev.md_lu_dev.ld_obd->obd_no_conn = 0;
4371 }
4372
4373 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
4374                       enum md_upcall_event ev)
4375 {
4376         struct mdt_device *m = mdt_dev(&md->md_lu_dev);
4377         struct md_device  *next  = m->mdt_child;
4378         struct mdt_thread_info *mti;
4379         int rc = 0;
4380         ENTRY;
4381
4382         switch (ev) {
4383                 case MD_LOV_SYNC:
4384                         rc = next->md_ops->mdo_maxsize_get(env, next,
4385                                         &m->mdt_max_mdsize,
4386                                         &m->mdt_max_cookiesize);
4387                         CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
4388                                      m->mdt_max_mdsize, m->mdt_max_cookiesize);
4389                         mdt_allow_cli(m, CONFIG_SYNC);
4390                         break;
4391                 case MD_NO_TRANS:
4392                         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4393                         mti->mti_no_need_trans = 1;
4394                         CDEBUG(D_INFO, "disable mdt trans for this thread\n");
4395                         break;
4396                 case MD_LOV_CONFIG:
4397                         /* Check that MDT is not yet configured */
4398                         LASSERT(!m->mdt_fl_cfglog);
4399                         break;
4400                 default:
4401                         CERROR("invalid event\n");
4402                         rc = -EINVAL;
4403                         break;
4404         }
4405         RETURN(rc);
4406 }
4407
4408 static int mdt_obd_notify(struct obd_device *host,
4409                           struct obd_device *watched,
4410                           enum obd_notify_event ev, void *data)
4411 {
4412         ENTRY;
4413
4414         switch (ev) {
4415         case OBD_NOTIFY_CONFIG:
4416                 mdt_allow_cli(mdt_dev(host->obd_lu_dev), (unsigned int)data);
4417                 break;
4418         default:
4419                 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
4420         }
4421         RETURN(0);
4422 }
4423
4424 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
4425                          void *karg, void *uarg)
4426 {
4427         struct lu_env      env;
4428         struct obd_device *obd= exp->exp_obd;
4429         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
4430         struct dt_device  *dt = mdt->mdt_bottom;
4431         int rc;
4432
4433         ENTRY;
4434         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
4435         rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4436         if (rc)
4437                 RETURN(rc);
4438
4439         switch (cmd) {
4440         case OBD_IOC_SYNC:
4441                 rc = dt->dd_ops->dt_sync(&env, dt);
4442                 break;
4443         case OBD_IOC_SET_READONLY:
4444                 rc = dt->dd_ops->dt_sync(&env, dt);
4445                 dt->dd_ops->dt_ro(&env, dt);
4446                 break;
4447         case OBD_IOC_ABORT_RECOVERY:
4448                 CERROR("Aborting recovery for device %s\n", obd->obd_name);
4449                 target_stop_recovery_thread(obd);
4450                 rc = 0;
4451                 break;
4452         default:
4453                 CERROR("Not supported cmd = %d for device %s\n",
4454                        cmd, obd->obd_name);
4455                 rc = -EOPNOTSUPP;
4456         }
4457
4458         lu_env_fini(&env);
4459         RETURN(rc);
4460 }
4461
4462 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
4463 {
4464         struct lu_device *ld = md2lu_dev(mdt->mdt_child);
4465         struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
4466         int rc, lost;
4467         ENTRY;
4468         /* if some clients didn't participate in recovery then we can possibly 
4469          * lost sequence. Now we should increase sequence for safe value */
4470         lost = obd->obd_max_recoverable_clients - obd->obd_connected_clients;
4471         mdt_seq_adjust(env, mdt, lost);
4472         
4473         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
4474         RETURN(rc);
4475 }
4476
4477 int mdt_obd_postrecov(struct obd_device *obd)
4478 {
4479         struct lu_env env;
4480         int rc;
4481
4482         rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4483         if (rc)
4484                 RETURN(rc);
4485         rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
4486         lu_env_fini(&env);
4487         return rc;
4488 }
4489
4490 static struct obd_ops mdt_obd_device_ops = {
4491         .o_owner          = THIS_MODULE,
4492         .o_connect        = mdt_obd_connect,
4493         .o_reconnect      = mdt_obd_reconnect,
4494         .o_disconnect     = mdt_obd_disconnect,
4495         .o_init_export    = mdt_init_export,
4496         .o_destroy_export = mdt_destroy_export,
4497         .o_iocontrol      = mdt_iocontrol,
4498         .o_postrecov      = mdt_obd_postrecov,
4499         .o_notify         = mdt_obd_notify
4500 };
4501
4502 static struct lu_device* mdt_device_fini(const struct lu_env *env,
4503                                          struct lu_device *d)
4504 {
4505         struct mdt_device *m = mdt_dev(d);
4506         ENTRY;
4507
4508         mdt_fini(env, m);
4509         RETURN(NULL);
4510 }
4511
4512 static void mdt_device_free(const struct lu_env *env, struct lu_device *d)
4513 {
4514         struct mdt_device *m = mdt_dev(d);
4515
4516         OBD_FREE_PTR(m);
4517 }
4518
4519 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
4520                                           struct lu_device_type *t,
4521                                           struct lustre_cfg *cfg)
4522 {
4523         struct lu_device  *l;
4524         struct mdt_device *m;
4525
4526         OBD_ALLOC_PTR(m);
4527         if (m != NULL) {
4528                 int rc;
4529
4530                 l = &m->mdt_md_dev.md_lu_dev;
4531                 rc = mdt_init0(env, m, t, cfg);
4532                 if (rc != 0) {
4533                         OBD_FREE_PTR(m);
4534                         l = ERR_PTR(rc);
4535                         return l;
4536                 }
4537                 md_upcall_init(&m->mdt_md_dev, mdt_upcall);
4538         } else
4539                 l = ERR_PTR(-ENOMEM);
4540         return l;
4541 }
4542
4543 /*
4544  * context key constructor/destructor
4545  */
4546 LU_KEY_INIT_FINI(mdt, struct mdt_thread_info);
4547
4548 struct lu_context_key mdt_thread_key = {
4549         .lct_tags = LCT_MD_THREAD,
4550         .lct_init = mdt_key_init,
4551         .lct_fini = mdt_key_fini
4552 };
4553
4554 LU_KEY_INIT_FINI(mdt_txn, struct mdt_txn_info);
4555
4556 struct lu_context_key mdt_txn_key = {
4557         .lct_tags = LCT_TX_HANDLE,
4558         .lct_init = mdt_txn_key_init,
4559         .lct_fini = mdt_txn_key_fini
4560 };
4561
4562 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
4563 {
4564         return md_ucred(info->mti_env);
4565 }
4566
4567 static int mdt_type_init(struct lu_device_type *t)
4568 {
4569         int rc;
4570
4571         LU_CONTEXT_KEY_INIT(&mdt_thread_key);
4572         rc = lu_context_key_register(&mdt_thread_key);
4573         if (rc == 0) {
4574                 LU_CONTEXT_KEY_INIT(&mdt_txn_key);
4575                 rc = lu_context_key_register(&mdt_txn_key);
4576         }
4577         return rc;
4578 }
4579
4580 static void mdt_type_fini(struct lu_device_type *t)
4581 {
4582         lu_context_key_degister(&mdt_thread_key);
4583         lu_context_key_degister(&mdt_txn_key);
4584 }
4585
4586 static struct lu_device_type_operations mdt_device_type_ops = {
4587         .ldto_init = mdt_type_init,
4588         .ldto_fini = mdt_type_fini,
4589
4590         .ldto_device_alloc = mdt_device_alloc,
4591         .ldto_device_free  = mdt_device_free,
4592         .ldto_device_fini  = mdt_device_fini
4593 };
4594
4595 static struct lu_device_type mdt_device_type = {
4596         .ldt_tags     = LU_DEVICE_MD,
4597         .ldt_name     = LUSTRE_MDT_NAME,
4598         .ldt_ops      = &mdt_device_type_ops,
4599         .ldt_ctx_tags = LCT_MD_THREAD
4600 };
4601
4602 static int __init mdt_mod_init(void)
4603 {
4604         struct lprocfs_static_vars lvars;
4605         int rc;
4606
4607         mdt_num_threads = MDT_NUM_THREADS;
4608         lprocfs_init_vars(mdt, &lvars);
4609         rc = class_register_type(&mdt_obd_device_ops, NULL,
4610                                  lvars.module_vars, LUSTRE_MDT_NAME,
4611                                  &mdt_device_type);
4612
4613         return rc;
4614 }
4615
4616 static void __exit mdt_mod_exit(void)
4617 {
4618         class_unregister_type(LUSTRE_MDT_NAME);
4619 }
4620
4621
4622 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt)             \
4623 [prefix ## _ ## opc - prefix ## _ ## base] = {                          \
4624         .mh_name    = #opc,                                             \
4625         .mh_fail_id = OBD_FAIL_ ## prefix ## _  ## opc ## suffix,       \
4626         .mh_opc     = prefix ## _  ## opc,                              \
4627         .mh_flags   = flags,                                            \
4628         .mh_act     = fn,                                               \
4629         .mh_fmt     = fmt                                               \
4630 }
4631
4632 #define DEF_MDT_HNDL(flags, name, fn, fmt)                                  \
4633         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
4634
4635 #define DEF_SEQ_HNDL(flags, name, fn, fmt)                      \
4636         DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
4637
4638 #define DEF_FLD_HNDL(flags, name, fn, fmt)                      \
4639         DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
4640 /*
4641  * Request with a format known in advance
4642  */
4643 #define DEF_MDT_HNDL_F(flags, name, fn)                                 \
4644         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
4645
4646 #define DEF_SEQ_HNDL_F(flags, name, fn)                                 \
4647         DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
4648
4649 #define DEF_FLD_HNDL_F(flags, name, fn)                                 \
4650         DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
4651 /*
4652  * Request with a format we do not yet know
4653  */
4654 #define DEF_MDT_HNDL_0(flags, name, fn)                                 \
4655         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
4656
4657 static struct mdt_handler mdt_mds_ops[] = {
4658 DEF_MDT_HNDL_F(0,                         CONNECT,      mdt_connect),
4659 DEF_MDT_HNDL_F(0,                         DISCONNECT,   mdt_disconnect),
4660 DEF_MDT_HNDL_F(0,                         SET_INFO,     mdt_set_info),
4661 DEF_MDT_HNDL_F(0           |HABEO_REFERO, GETSTATUS,    mdt_getstatus),
4662 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETATTR,      mdt_getattr),
4663 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
4664 DEF_MDT_HNDL_F(HABEO_CORPUS|MUTABOR,      SETXATTR,     mdt_setxattr),
4665 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETXATTR,     mdt_getxattr),
4666 DEF_MDT_HNDL_F(0           |HABEO_REFERO, STATFS,       mdt_statfs),
4667 DEF_MDT_HNDL_F(0           |MUTABOR,      REINT,        mdt_reint),
4668 DEF_MDT_HNDL_F(HABEO_CORPUS,              CLOSE,        mdt_close),
4669 DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING, mdt_done_writing),
4670 DEF_MDT_HNDL_F(0           |HABEO_REFERO, PIN,          mdt_pin),
4671 DEF_MDT_HNDL_0(0,                         SYNC,         mdt_sync),
4672 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
4673 DEF_MDT_HNDL_0(0,                         QUOTACHECK,   mdt_quotacheck_handle),
4674 DEF_MDT_HNDL_0(0,                         QUOTACTL,     mdt_quotactl_handle)
4675 };
4676
4677 #define DEF_OBD_HNDL(flags, name, fn)                   \
4678         DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
4679
4680
4681 static struct mdt_handler mdt_obd_ops[] = {
4682         DEF_OBD_HNDL(0, PING,           mdt_obd_ping),
4683         DEF_OBD_HNDL(0, LOG_CANCEL,     mdt_obd_log_cancel),
4684         DEF_OBD_HNDL(0, QC_CALLBACK,    mdt_obd_qc_callback)
4685 };
4686
4687 #define DEF_DLM_HNDL_0(flags, name, fn)                   \
4688         DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
4689 #define DEF_DLM_HNDL_F(flags, name, fn)                   \
4690         DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
4691
4692 static struct mdt_handler mdt_dlm_ops[] = {
4693         DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE,        mdt_enqueue),
4694         DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT,        mdt_convert),
4695         DEF_DLM_HNDL_0(0,            BL_CALLBACK,    mdt_bl_callback),
4696         DEF_DLM_HNDL_0(0,            CP_CALLBACK,    mdt_cp_callback)
4697 };
4698
4699 static struct mdt_handler mdt_llog_ops[] = {
4700 };
4701
4702 #define DEF_SEC_CTX_HNDL(name, fn)                      \
4703         DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
4704
4705 static struct mdt_handler mdt_sec_ctx_ops[] = {
4706         DEF_SEC_CTX_HNDL(INIT,          mdt_sec_ctx_handle),
4707         DEF_SEC_CTX_HNDL(INIT_CONT,     mdt_sec_ctx_handle),
4708         DEF_SEC_CTX_HNDL(FINI,          mdt_sec_ctx_handle)
4709 };
4710
4711 static struct mdt_opc_slice mdt_regular_handlers[] = {
4712         {
4713                 .mos_opc_start = MDS_GETATTR,
4714                 .mos_opc_end   = MDS_LAST_OPC,
4715                 .mos_hs        = mdt_mds_ops
4716         },
4717         {
4718                 .mos_opc_start = OBD_PING,
4719                 .mos_opc_end   = OBD_LAST_OPC,
4720                 .mos_hs        = mdt_obd_ops
4721         },
4722         {
4723                 .mos_opc_start = LDLM_ENQUEUE,
4724                 .mos_opc_end   = LDLM_LAST_OPC,
4725                 .mos_hs        = mdt_dlm_ops
4726         },
4727         {
4728                 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
4729                 .mos_opc_end   = LLOG_LAST_OPC,
4730                 .mos_hs        = mdt_llog_ops
4731         },
4732         {
4733                 .mos_opc_start = SEC_CTX_INIT,
4734                 .mos_opc_end   = SEC_LAST_OPC,
4735                 .mos_hs        = mdt_sec_ctx_ops
4736         },
4737         {
4738                 .mos_hs        = NULL
4739         }
4740 };
4741
4742 static struct mdt_handler mdt_readpage_ops[] = {
4743         DEF_MDT_HNDL_F(0,                         CONNECT,  mdt_connect),
4744         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
4745 #ifdef HAVE_SPLIT_SUPPORT
4746         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
4747 #endif
4748
4749         /*
4750          * XXX: this is ugly and should be fixed one day, see mdc_close() for
4751          * detailed comments. --umka
4752          */
4753         DEF_MDT_HNDL_F(HABEO_CORPUS,              CLOSE,    mdt_close),
4754         DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING,    mdt_done_writing),
4755 };
4756
4757 static struct mdt_opc_slice mdt_readpage_handlers[] = {
4758         {
4759                 .mos_opc_start = MDS_GETATTR,
4760                 .mos_opc_end   = MDS_LAST_OPC,
4761                 .mos_hs        = mdt_readpage_ops
4762         },
4763         {
4764                 .mos_hs        = NULL
4765         }
4766 };
4767
4768 static struct mdt_handler mdt_xmds_ops[] = {
4769         DEF_MDT_HNDL_F(0,                         CONNECT,      mdt_connect),
4770         DEF_MDT_HNDL_F(HABEO_CORPUS             , GETATTR,      mdt_getattr),
4771         DEF_MDT_HNDL_F(0 | MUTABOR              , REINT,        mdt_reint),
4772         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
4773 };
4774
4775 static struct mdt_opc_slice mdt_xmds_handlers[] = {
4776         {
4777                 .mos_opc_start = MDS_GETATTR,
4778                 .mos_opc_end   = MDS_LAST_OPC,
4779                 .mos_hs        = mdt_xmds_ops
4780         },
4781         {
4782                 .mos_opc_start = OBD_PING,
4783                 .mos_opc_end   = OBD_LAST_OPC,
4784                 .mos_hs        = mdt_obd_ops
4785         },
4786         {
4787                 .mos_hs        = NULL
4788         }
4789 };
4790
4791 static struct mdt_handler mdt_seq_ops[] = {
4792         DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
4793 };
4794
4795 static struct mdt_opc_slice mdt_seq_handlers[] = {
4796         {
4797                 .mos_opc_start = SEQ_QUERY,
4798                 .mos_opc_end   = SEQ_LAST_OPC,
4799                 .mos_hs        = mdt_seq_ops
4800         },
4801         {
4802                 .mos_hs        = NULL
4803         }
4804 };
4805
4806 static struct mdt_handler mdt_fld_ops[] = {
4807         DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
4808 };
4809
4810 static struct mdt_opc_slice mdt_fld_handlers[] = {
4811         {
4812                 .mos_opc_start = FLD_QUERY,
4813                 .mos_opc_end   = FLD_LAST_OPC,
4814                 .mos_hs        = mdt_fld_ops
4815         },
4816         {
4817                 .mos_hs        = NULL
4818         }
4819 };
4820
4821 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
4822 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
4823 MODULE_LICENSE("GPL");
4824
4825 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
4826                 "number of mdt service threads to start");
4827
4828 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);