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