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