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