Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/mdt/mdt_handler.c
5  *  Lustre Metadata Target (mdt) request handler
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *   Author: Mike Shaver <shaver@clusterfs.com>
12  *   Author: Nikita Danilov <nikita@clusterfs.com>
13  *   Author: Huang Hua <huanghua@clusterfs.com>
14  *   Author: Yury Umanets <umka@clusterfs.com>
15  *
16  *   This file is part of the Lustre file system, http://www.lustre.org
17  *   Lustre is a trademark of Cluster File Systems, Inc.
18  *
19  *   You may have signed or agreed to another license before downloading
20  *   this software.  If so, you are bound by the terms and conditions
21  *   of that agreement, and the following does not apply to you.  See the
22  *   LICENSE file included with this distribution for more information.
23  *
24  *   If you did not agree to a different license, then this copy of Lustre
25  *   is open source software; you can redistribute it and/or modify it
26  *   under the terms of version 2 of the GNU General Public License as
27  *   published by the Free Software Foundation.
28  *
29  *   In either case, Lustre is distributed in the hope that it will be
30  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
31  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  *   license text for more details.
33  */
34
35 #ifndef EXPORT_SYMTAB
36 # define EXPORT_SYMTAB
37 #endif
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <linux/module.h>
41 /*
42  * struct OBD_{ALLOC,FREE}*()
43  */
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         OBD_FAIL_RETURN(OBD_FAIL_MDS_SENDPAGE, 0);
1388
1389         return rc;
1390 }
1391
1392 static int mdt_reint_internal(struct mdt_thread_info *info,
1393                               struct mdt_lock_handle *lhc,
1394                               __u32 op)
1395 {
1396         struct req_capsule      *pill = &info->mti_pill;
1397         struct mdt_device       *mdt = info->mti_mdt;
1398         struct mdt_body         *repbody;
1399         int                      need_shrink = 0;
1400         int                      rc;
1401         ENTRY;
1402
1403         /* pack reply */
1404         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
1405                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1406                                      mdt->mdt_max_mdsize);
1407                 need_shrink = 1;
1408         }
1409         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
1410                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1411                                      mdt->mdt_max_cookiesize);
1412                 need_shrink = 1;
1413         }
1414         rc = req_capsule_pack(pill);
1415         if (rc != 0) {
1416                 CERROR("Can't pack response, rc %d\n", rc);
1417                 RETURN(err_serious(rc));
1418         }
1419
1420         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1421                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1422                 LASSERT(repbody);
1423                 repbody->eadatasize = 0;
1424                 repbody->aclsize = 0;
1425         }
1426
1427         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1428                 GOTO(out_shrink, rc = err_serious(-EFAULT));
1429
1430         rc = mdt_reint_unpack(info, op);
1431         if (rc != 0) {
1432                 CERROR("Can't unpack reint, rc %d\n", rc);
1433                 GOTO(out_shrink, rc = err_serious(rc));
1434         }
1435
1436         rc = mdt_init_ucred_reint(info);
1437         if (rc)
1438                 GOTO(out_shrink, rc);
1439
1440         rc = mdt_fix_attr_ucred(info, op);
1441         if (rc != 0)
1442                 GOTO(out_ucred, rc = err_serious(rc));
1443
1444         need_shrink = 0;
1445         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1446                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1447                 GOTO(out_ucred, rc);
1448         }
1449         
1450         rc = mdt_reint_rec(info, lhc);
1451         EXIT;
1452 out_ucred:
1453         mdt_exit_ucred(info);
1454 out_shrink:
1455         if (need_shrink)
1456                 mdt_shrink_reply(info);
1457         return rc;
1458 }
1459
1460 static long mdt_reint_opcode(struct mdt_thread_info *info,
1461                              const struct req_format **fmt)
1462 {
1463         __u32 *ptr;
1464         long opc;
1465
1466         opc = err_serious(-EFAULT);
1467         ptr = req_capsule_client_get(&info->mti_pill, &RMF_REINT_OPC);
1468         if (ptr != NULL) {
1469                 opc = *ptr;
1470                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1471                 if (opc < REINT_MAX && fmt[opc] != NULL)
1472                         req_capsule_extend(&info->mti_pill, fmt[opc]);
1473                 else {
1474                         CERROR("Unsupported opc: %ld\n", opc);
1475                         opc = err_serious(opc);
1476                 }
1477         }
1478         return opc;
1479 }
1480
1481 static int mdt_reint(struct mdt_thread_info *info)
1482 {
1483         long opc;
1484         int  rc;
1485
1486         static const struct req_format *reint_fmts[REINT_MAX] = {
1487                 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1488                 [REINT_CREATE]  = &RQF_MDS_REINT_CREATE,
1489                 [REINT_LINK]    = &RQF_MDS_REINT_LINK,
1490                 [REINT_UNLINK]  = &RQF_MDS_REINT_UNLINK,
1491                 [REINT_RENAME]  = &RQF_MDS_REINT_RENAME,
1492                 [REINT_OPEN]    = &RQF_MDS_REINT_OPEN
1493         };
1494
1495         ENTRY;
1496
1497         opc = mdt_reint_opcode(info, reint_fmts);
1498         if (opc >= 0) {
1499                 /*
1500                  * No lock possible here from client to pass it to reint code
1501                  * path.
1502                  */
1503                 rc = mdt_reint_internal(info, NULL, opc);
1504         } else {
1505                 rc = opc;
1506         }
1507
1508         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1509         RETURN(rc);
1510 }
1511
1512 /* TODO these two methods not available now. */
1513
1514 /* this should sync the whole device */
1515 static int mdt_device_sync(struct mdt_thread_info *info)
1516 {
1517         return 0;
1518 }
1519
1520 /* this should sync this object */
1521 static int mdt_object_sync(struct mdt_thread_info *info)
1522 {
1523         return 0;
1524 }
1525
1526 static int mdt_sync(struct mdt_thread_info *info)
1527 {
1528         struct req_capsule *pill = &info->mti_pill;
1529         struct mdt_body *body;
1530         int rc;
1531         ENTRY;
1532
1533         /* The fid may be zero, so we req_capsule_set manually */
1534         req_capsule_set(pill, &RQF_MDS_SYNC);
1535
1536         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1537         if (body == NULL)
1538                 RETURN(err_serious(-EINVAL));
1539
1540         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1541                 RETURN(err_serious(-ENOMEM));
1542
1543         if (fid_seq(&body->fid1) == 0) {
1544                 /* sync the whole device */
1545                 rc = req_capsule_pack(pill);
1546                 if (rc == 0)
1547                         rc = mdt_device_sync(info);
1548                 else
1549                         rc = err_serious(rc);
1550         } else {
1551                 /* sync an object */
1552                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1553                 if (rc == 0) {
1554                         rc = mdt_object_sync(info);
1555                         if (rc == 0) {
1556                                 struct md_object *next;
1557                                 const struct lu_fid *fid;
1558                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1559
1560                                 next = mdt_object_child(info->mti_object);
1561                                 info->mti_attr.ma_need = MA_INODE;
1562                                 info->mti_attr.ma_valid = 0;
1563                                 rc = mo_attr_get(info->mti_env, next,
1564                                                  &info->mti_attr);
1565                                 if (rc == 0) {
1566                                         body = req_capsule_server_get(pill,
1567                                                                 &RMF_MDT_BODY);
1568                                         fid = mdt_object_fid(info->mti_object);
1569                                         mdt_pack_attr2body(info, body, la, fid);
1570                                 }
1571                         }
1572                 } else
1573                         rc = err_serious(rc);
1574         }
1575         RETURN(rc);
1576 }
1577
1578 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1579 {
1580         return err_serious(-EOPNOTSUPP);
1581 }
1582
1583 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1584 {
1585         return err_serious(-EOPNOTSUPP);
1586 }
1587
1588 /*
1589  * OBD PING and other handlers.
1590  */
1591 static int mdt_obd_ping(struct mdt_thread_info *info)
1592 {
1593         int rc;
1594         ENTRY;
1595         rc = target_handle_ping(mdt_info_req(info));
1596         if (rc < 0)
1597                 rc = err_serious(rc);
1598         RETURN(rc);
1599 }
1600
1601 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1602 {
1603         return err_serious(-EOPNOTSUPP);
1604 }
1605
1606 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1607 {
1608         return err_serious(-EOPNOTSUPP);
1609 }
1610
1611
1612 /*
1613  * DLM handlers.
1614  */
1615 static struct ldlm_callback_suite cbs = {
1616         .lcs_completion = ldlm_server_completion_ast,
1617         .lcs_blocking   = ldlm_server_blocking_ast,
1618         .lcs_glimpse    = NULL
1619 };
1620
1621 static int mdt_enqueue(struct mdt_thread_info *info)
1622 {
1623         struct ptlrpc_request *req;
1624         __u64 req_bits;
1625         int rc;
1626
1627         /*
1628          * info->mti_dlm_req already contains swapped and (if necessary)
1629          * converted dlm request.
1630          */
1631         LASSERT(info->mti_dlm_req != NULL);
1632
1633         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE)) {
1634                 info->mti_fail_id = OBD_FAIL_LDLM_ENQUEUE;
1635                 return 0;
1636         }
1637
1638         req = mdt_info_req(info);
1639
1640         /*
1641          * Lock without inodebits makes no sense and will oops later in
1642          * ldlm. Let's check it now to see if we have wrong lock from client or
1643          * bits get corrupted somewhere in mdt_intent_policy().
1644          */
1645         req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1646         /* This is disabled because we need to support liblustre flock.
1647          * LASSERT(req_bits != 0);
1648          */
1649
1650         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1651                                   req, info->mti_dlm_req, &cbs);
1652         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1653         return rc ? err_serious(rc) : req->rq_status;
1654 }
1655
1656 static int mdt_convert(struct mdt_thread_info *info)
1657 {
1658         int rc;
1659         struct ptlrpc_request *req;
1660
1661         LASSERT(info->mti_dlm_req);
1662         req = mdt_info_req(info);
1663         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1664         return rc ? err_serious(rc) : req->rq_status;
1665 }
1666
1667 static int mdt_bl_callback(struct mdt_thread_info *info)
1668 {
1669         CERROR("bl callbacks should not happen on MDS\n");
1670         LBUG();
1671         return err_serious(-EOPNOTSUPP);
1672 }
1673
1674 static int mdt_cp_callback(struct mdt_thread_info *info)
1675 {
1676         CERROR("cp callbacks should not happen on MDS\n");
1677         LBUG();
1678         return err_serious(-EOPNOTSUPP);
1679 }
1680
1681 /*
1682  * sec context handlers
1683  */
1684 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
1685 {
1686         int rc;
1687
1688         rc = mdt_handle_idmap(info);
1689
1690         if (unlikely(rc)) {
1691                 struct ptlrpc_request *req = mdt_info_req(info);
1692                 __u32                  opc;
1693
1694                 opc = lustre_msg_get_opc(req->rq_reqmsg);
1695                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
1696                         sptlrpc_svc_ctx_invalidate(req);
1697         }
1698
1699         return rc;
1700 }
1701
1702 static struct mdt_object *mdt_obj(struct lu_object *o)
1703 {
1704         LASSERT(lu_device_is_mdt(o->lo_dev));
1705         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
1706 }
1707
1708 struct mdt_object *mdt_object_find(const struct lu_env *env,
1709                                    struct mdt_device *d,
1710                                    const struct lu_fid *f)
1711 {
1712         struct lu_object *o;
1713         struct mdt_object *m;
1714         ENTRY;
1715
1716         o = lu_object_find(env, d->mdt_md_dev.md_lu_dev.ld_site, f);
1717         if (unlikely(IS_ERR(o)))
1718                 m = (struct mdt_object *)o;
1719         else
1720                 m = mdt_obj(o);
1721         RETURN(m);
1722 }
1723
1724 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
1725                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
1726 {
1727         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1728         ldlm_policy_data_t *policy = &info->mti_policy;
1729         struct ldlm_res_id *res_id = &info->mti_res_id;
1730         int rc;
1731         ENTRY;
1732
1733         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1734         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1735         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
1736         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
1737
1738         if (mdt_object_exists(o) < 0) {
1739                 if (locality == MDT_CROSS_LOCK) {
1740                         /* cross-ref object fix */
1741                         ibits &= ~MDS_INODELOCK_UPDATE;
1742                         ibits |= MDS_INODELOCK_LOOKUP;
1743                 } else {
1744                         LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
1745                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
1746                 }
1747                 /* No PDO lock on remote object */
1748                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
1749         }
1750
1751         memset(policy, 0, sizeof(*policy));
1752         fid_build_reg_res_name(mdt_object_fid(o), res_id);
1753
1754         /*
1755          * Take PDO lock on whole directory and build correct @res_id for lock
1756          * on part of directory.
1757          */
1758         if (lh->mlh_pdo_hash != 0) {
1759                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
1760                 mdt_lock_pdo_mode(info, o, lh);
1761                 if (lh->mlh_pdo_mode != LCK_NL) {
1762                         /*
1763                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
1764                          * is never going to be sent to client and we do not
1765                          * want it slowed down due to possible cancels.
1766                          */
1767                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1768                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
1769                                           policy, res_id, LDLM_FL_ATOMIC_CB);
1770                         if (unlikely(rc))
1771                                 RETURN(rc);
1772                 }
1773
1774                 /*
1775                  * Finish res_id initializing by name hash marking patr of
1776                  * directory which is taking modification.
1777                  */
1778                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
1779         }
1780
1781         policy->l_inodebits.bits = ibits;
1782
1783         /*
1784          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1785          * going to be sent to client. If it is - mdt_intent_policy() path will
1786          * fix it up and turns FL_LOCAL flag off.
1787          */
1788         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1789                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB);
1790
1791         if (rc)
1792                 GOTO(out, rc);
1793
1794         if (lh->mlh_type == MDT_PDO_LOCK) {
1795                 /* check for exists after object is locked */
1796                 if (mdt_object_exists(o) == 0) {
1797                         /* Non-existent object shouldn't have PDO lock */
1798                         rc = -ESTALE;
1799                 } else {
1800                         /* Non-dir object shouldn't have PDO lock */
1801                         LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
1802                 }
1803         }
1804 out:
1805         if (rc)
1806                 mdt_object_unlock(info, o, lh, 1);
1807
1808
1809         RETURN(rc);
1810 }
1811
1812 /*
1813  * Just call ldlm_lock_decref() if decref, else we only call ptlrpc_save_lock()
1814  * to save this lock in req.  when transaction committed, req will be released,
1815  * and lock will, too.
1816  */
1817 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
1818                        struct mdt_lock_handle *lh, int decref)
1819 {
1820         struct ptlrpc_request *req = mdt_info_req(info);
1821         ENTRY;
1822
1823         if (lustre_handle_is_used(&lh->mlh_pdo_lh)) {
1824                 /* Do not save PDO locks to request, just decref. */
1825                 mdt_fid_unlock(&lh->mlh_pdo_lh,
1826                                lh->mlh_pdo_mode);
1827                 lh->mlh_pdo_lh.cookie = 0ull;
1828         }
1829
1830         if (lustre_handle_is_used(&lh->mlh_reg_lh)) {
1831                 if (decref) {
1832                         mdt_fid_unlock(&lh->mlh_reg_lh,
1833                                        lh->mlh_reg_mode);
1834                 } else {
1835                         ptlrpc_save_lock(req, &lh->mlh_reg_lh,
1836                                          lh->mlh_reg_mode);
1837                 }
1838                 lh->mlh_reg_lh.cookie = 0ull;
1839         }
1840
1841         EXIT;
1842 }
1843
1844 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
1845                                         const struct lu_fid *f,
1846                                         struct mdt_lock_handle *lh,
1847                                         __u64 ibits)
1848 {
1849         struct mdt_object *o;
1850
1851         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
1852         if (!IS_ERR(o)) {
1853                 int rc;
1854
1855                 rc = mdt_object_lock(info, o, lh, ibits,
1856                                      MDT_LOCAL_LOCK);
1857                 if (rc != 0) {
1858                         mdt_object_put(info->mti_env, o);
1859                         o = ERR_PTR(rc);
1860                 }
1861         }
1862         return o;
1863 }
1864
1865 void mdt_object_unlock_put(struct mdt_thread_info * info,
1866                            struct mdt_object * o,
1867                            struct mdt_lock_handle *lh,
1868                            int decref)
1869 {
1870         mdt_object_unlock(info, o, lh, decref);
1871         mdt_object_put(info->mti_env, o);
1872 }
1873
1874 static struct mdt_handler *mdt_handler_find(__u32 opc,
1875                                             struct mdt_opc_slice *supported)
1876 {
1877         struct mdt_opc_slice *s;
1878         struct mdt_handler   *h;
1879
1880         h = NULL;
1881         for (s = supported; s->mos_hs != NULL; s++) {
1882                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
1883                         h = s->mos_hs + (opc - s->mos_opc_start);
1884                         if (likely(h->mh_opc != 0))
1885                                 LASSERT(h->mh_opc == opc);
1886                         else
1887                                 h = NULL; /* unsupported opc */
1888                         break;
1889                 }
1890         }
1891         return h;
1892 }
1893
1894 static int mdt_lock_resname_compat(struct mdt_device *m,
1895                                    struct ldlm_request *req)
1896 {
1897         /* XXX something... later. */
1898         return 0;
1899 }
1900
1901 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
1902 {
1903         /* XXX something... later. */
1904         return 0;
1905 }
1906
1907 /*
1908  * Generic code handling requests that have struct mdt_body passed in:
1909  *
1910  *  - extract mdt_body from request and save it in @info, if present;
1911  *
1912  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
1913  *  @info;
1914  *
1915  *  - if HABEO_CORPUS flag is set for this request type check whether object
1916  *  actually exists on storage (lu_object_exists()).
1917  *
1918  */
1919 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
1920 {
1921         const struct mdt_body    *body;
1922         struct mdt_object        *obj;
1923         const struct lu_env      *env;
1924         struct req_capsule       *pill;
1925         int                       rc;
1926
1927         env = info->mti_env;
1928         pill = &info->mti_pill;
1929
1930         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1931         if (body == NULL)
1932                 return -EFAULT;
1933
1934         if (!fid_is_sane(&body->fid1)) {
1935                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
1936                 return -EINVAL;
1937         }
1938
1939         /*
1940          * Do not get size or any capa fields before we check that request
1941          * contains capa actually. There are some requests which do not, for
1942          * instance MDS_IS_SUBDIR.
1943          */
1944         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
1945             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1946                 mdt_set_capainfo(info, 0, &body->fid1,
1947                                  req_capsule_client_get(pill, &RMF_CAPA1));
1948
1949         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
1950         if (!IS_ERR(obj)) {
1951                 if ((flags & HABEO_CORPUS) &&
1952                     !mdt_object_exists(obj)) {
1953                         mdt_object_put(env, obj);
1954                         /* for capability renew ENOENT will be handled in
1955                          * mdt_renew_capa */
1956                         if (body->valid & OBD_MD_FLOSSCAPA)
1957                                 rc = 0;
1958                         else
1959                                 rc = -ENOENT;
1960                 } else {
1961                         info->mti_object = obj;
1962                         rc = 0;
1963                 }
1964         } else
1965                 rc = PTR_ERR(obj);
1966
1967         return rc;
1968 }
1969
1970 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
1971 {
1972         struct req_capsule *pill;
1973         int rc;
1974
1975         ENTRY;
1976         pill = &info->mti_pill;
1977
1978         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
1979                 rc = mdt_body_unpack(info, flags);
1980         else
1981                 rc = 0;
1982
1983         if (rc == 0 && (flags & HABEO_REFERO)) {
1984                 struct mdt_device *mdt = info->mti_mdt;
1985
1986                 /* Pack reply. */
1987                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1988                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1989                                              mdt->mdt_max_mdsize);
1990                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1991                         req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1992                                              mdt->mdt_max_cookiesize);
1993
1994                 rc = req_capsule_pack(pill);
1995         }
1996         RETURN(rc);
1997 }
1998
1999 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2000 {
2001         struct md_device *next = m->mdt_child;
2002
2003         return next->md_ops->mdo_init_capa_ctxt(env, next,
2004                                                 m->mdt_opts.mo_mds_capa,
2005                                                 m->mdt_capa_timeout,
2006                                                 m->mdt_capa_alg,
2007                                                 m->mdt_capa_keys);
2008 }
2009
2010 /*
2011  * Invoke handler for this request opc. Also do necessary preprocessing
2012  * (according to handler ->mh_flags), and post-processing (setting of
2013  * ->last_{xid,committed}).
2014  */
2015 static int mdt_req_handle(struct mdt_thread_info *info,
2016                           struct mdt_handler *h, struct ptlrpc_request *req)
2017 {
2018         int   rc, serious = 0;
2019         __u32 flags;
2020
2021         ENTRY;
2022
2023         LASSERT(h->mh_act != NULL);
2024         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2025         LASSERT(current->journal_info == NULL);
2026
2027         /*
2028          * Mask out OBD_FAIL_ONCE, because that will stop
2029          * correct handling of failed req later in ldlm due to doing
2030          * obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED without actually
2031          * correct actions like it is done in target_send_reply_msg().
2032          */
2033         if (h->mh_fail_id != 0) {
2034                 /*
2035                  * Set to info->mti_fail_id to handler fail_id, it will be used
2036                  * later, and better than use default fail_id.
2037                  */
2038                 if (OBD_FAIL_CHECK(h->mh_fail_id && OBD_FAIL_MASK_LOC)) {
2039                         info->mti_fail_id = h->mh_fail_id;
2040                         RETURN(0);
2041                 }
2042         }
2043
2044         rc = 0;
2045         flags = h->mh_flags;
2046         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2047
2048         if (h->mh_fmt != NULL) {
2049                 req_capsule_set(&info->mti_pill, h->mh_fmt);
2050                 rc = mdt_unpack_req_pack_rep(info, flags);
2051         }
2052
2053         if (rc == 0 && flags & MUTABOR &&
2054             req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2055                 /* should it be rq_status? */
2056                 rc = -EROFS;
2057
2058         if (rc == 0 && flags & HABEO_CLAVIS) {
2059                 struct ldlm_request *dlm_req;
2060
2061                 LASSERT(h->mh_fmt != NULL);
2062
2063                 dlm_req = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
2064                 if (dlm_req != NULL) {
2065                         if (info->mti_mdt->mdt_opts.mo_compat_resname)
2066                                 rc = mdt_lock_resname_compat(info->mti_mdt,
2067                                                              dlm_req);
2068                         info->mti_dlm_req = dlm_req;
2069                 } else {
2070                         CERROR("Can't unpack dlm request\n");
2071                         rc = -EFAULT;
2072                 }
2073         }
2074
2075         /* capability setting changed via /proc, needs reinitialize ctxt */
2076         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2077                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2078                 info->mti_mdt->mdt_capa_conf = 0;
2079         }
2080
2081         if (likely(rc == 0)) {
2082                 /*
2083                  * Process request, there can be two types of rc:
2084                  * 1) errors with msg unpack/pack, other failures outside the
2085                  * operation itself. This is counted as serious errors;
2086                  * 2) errors during fs operation, should be placed in rq_status
2087                  * only
2088                  */
2089                 rc = h->mh_act(info);
2090                 serious = is_serious(rc);
2091                 rc = clear_serious(rc);
2092         } else
2093                 serious = 1;
2094
2095         req->rq_status = rc;
2096
2097         /*
2098          * ELDLM_* codes which > 0 should be in rq_status only as well as
2099          * all non-serious errors.
2100          */
2101         if (rc > 0 || !serious)
2102                 rc = 0;
2103
2104         LASSERT(current->journal_info == NULL);
2105
2106         if (rc == 0 && (flags & HABEO_CLAVIS) &&
2107             info->mti_mdt->mdt_opts.mo_compat_resname) {
2108                 struct ldlm_reply *dlmrep;
2109
2110                 dlmrep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2111                 if (dlmrep != NULL)
2112                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2113         }
2114
2115         /* If we're DISCONNECTing, the mdt_export_data is already freed */
2116         if (likely(rc == 0 && h->mh_opc != MDS_DISCONNECT))
2117                 target_committed_to_req(req);
2118
2119         if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2120                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2121                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2122                 LBUG();
2123         }
2124
2125         RETURN(rc);
2126 }
2127
2128 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2129 {
2130         lh->mlh_type = MDT_NUL_LOCK;
2131         lh->mlh_reg_lh.cookie = 0ull;
2132         lh->mlh_reg_mode = LCK_MINMODE;
2133         lh->mlh_pdo_lh.cookie = 0ull;
2134         lh->mlh_pdo_mode = LCK_MINMODE;
2135 }
2136
2137 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2138 {
2139         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2140         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2141 }
2142
2143 /*
2144  * Initialize fields of struct mdt_thread_info. Other fields are left in
2145  * uninitialized state, because it's too expensive to zero out whole
2146  * mdt_thread_info (> 1K) on each request arrival.
2147  */
2148 static void mdt_thread_info_init(struct ptlrpc_request *req,
2149                                  struct mdt_thread_info *info)
2150 {
2151         int i;
2152         struct md_capainfo *ci;
2153
2154         info->mti_rep_buf_nr = ARRAY_SIZE(info->mti_rep_buf_size);
2155         for (i = 0; i < ARRAY_SIZE(info->mti_rep_buf_size); i++)
2156                 info->mti_rep_buf_size[i] = -1;
2157         req_capsule_init(&info->mti_pill, req, RCL_SERVER,
2158                          info->mti_rep_buf_size);
2159
2160         /* lock handle */
2161         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2162                 mdt_lock_handle_init(&info->mti_lh[i]);
2163
2164         /* mdt device: it can be NULL while CONNECT */
2165         if (req->rq_export) {
2166                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2167                 info->mti_exp = req->rq_export;
2168         } else
2169                 info->mti_mdt = NULL;
2170         info->mti_env = req->rq_svc_thread->t_env;
2171         ci = md_capainfo(info->mti_env);
2172         memset(ci, 0, sizeof *ci);
2173
2174         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2175         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2176
2177         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2178         info->mti_body = NULL;
2179         info->mti_object = NULL;
2180         info->mti_dlm_req = NULL;
2181         info->mti_has_trans = 0;
2182         info->mti_no_need_trans = 0;
2183         info->mti_cross_ref = 0;
2184         info->mti_opdata = 0;
2185
2186         /* To not check for split by default. */
2187         info->mti_spec.sp_ck_split = 0;
2188 }
2189
2190 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2191 {
2192         int i;
2193
2194         req_capsule_fini(&info->mti_pill);
2195         if (info->mti_object != NULL) {
2196                 mdt_object_put(info->mti_env, info->mti_object);
2197                 info->mti_object = NULL;
2198         }
2199         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2200                 mdt_lock_handle_fini(&info->mti_lh[i]);
2201         info->mti_env = NULL;
2202 }
2203
2204 /* mds/handler.c */
2205 extern int mds_filter_recovery_request(struct ptlrpc_request *req,
2206                                        struct obd_device *obd, int *process);
2207 /*
2208  * Handle recovery. Return:
2209  *        +1: continue request processing;
2210  *       -ve: abort immediately with the given error code;
2211  *         0: send reply with error code in req->rq_status;
2212  */
2213 static int mdt_recovery(struct mdt_thread_info *info)
2214 {
2215         struct ptlrpc_request *req = mdt_info_req(info);
2216         int recovering;
2217         struct obd_device *obd;
2218
2219         ENTRY;
2220
2221         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2222         case MDS_CONNECT:
2223         case SEC_CTX_INIT:
2224         case SEC_CTX_INIT_CONT:
2225         case SEC_CTX_FINI:
2226                 {
2227 #if 0
2228                         int rc;
2229
2230                         rc = mdt_handle_idmap(info);
2231                         if (rc)
2232                                 RETURN(rc);
2233                         else
2234 #endif
2235                                 RETURN(+1);
2236                 }
2237         }
2238
2239         if (unlikely(req->rq_export == NULL)) {
2240                 CERROR("operation %d on unconnected MDS from %s\n",
2241                        lustre_msg_get_opc(req->rq_reqmsg),
2242                        libcfs_id2str(req->rq_peer));
2243                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2244                  * mds_A will get -ENOTCONN(especially for ping req),
2245                  * which will cause that mds_A deactive timeout, then when
2246                  * mds_A cleanup, the cleanup process will be suspended since
2247                  * deactive timeout is not zero.
2248                  */
2249                 req->rq_status = -ENOTCONN;
2250                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2251                 RETURN(0);
2252         }
2253
2254         /* sanity check: if the xid matches, the request must be marked as a
2255          * resent or replayed */
2256         if (req_xid_is_last(req)) {
2257                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2258                       (MSG_RESENT | MSG_REPLAY))) {
2259                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2260                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2261                                   lustre_msg_get_flags(req->rq_reqmsg));
2262                         LBUG();
2263                         req->rq_status = -ENOTCONN;
2264                         RETURN(-ENOTCONN);
2265                 }
2266         }
2267
2268         /* else: note the opposite is not always true; a RESENT req after a
2269          * failover will usually not match the last_xid, since it was likely
2270          * never committed. A REPLAYed request will almost never match the
2271          * last xid, however it could for a committed, but still retained,
2272          * open. */
2273
2274         obd = req->rq_export->exp_obd;
2275
2276         /* Check for aborted recovery... */
2277         spin_lock_bh(&obd->obd_processing_task_lock);
2278         recovering = obd->obd_recovering;
2279         spin_unlock_bh(&obd->obd_processing_task_lock);
2280         if (unlikely(recovering)) {
2281                 int rc;
2282                 int should_process;
2283                 DEBUG_REQ(D_INFO, req, "Got new replay");
2284                 rc = mds_filter_recovery_request(req, obd, &should_process);
2285                 if (rc != 0 || !should_process)
2286                         RETURN(rc);
2287                 else if (should_process < 0) {
2288                         req->rq_status = should_process;
2289                         rc = ptlrpc_error(req);
2290                         RETURN(rc);
2291                 }
2292         }
2293         RETURN(+1);
2294 }
2295
2296 static int mdt_reply(struct ptlrpc_request *req, int rc,
2297                      struct mdt_thread_info *info)
2298 {
2299         ENTRY;
2300
2301 #if 0
2302         if (req->rq_reply_state == NULL && rc == 0) {
2303                 req->rq_status = rc;
2304                 lustre_pack_reply(req, 1, NULL, NULL);
2305         }
2306 #endif
2307         target_send_reply(req, rc, info->mti_fail_id);
2308         RETURN(0);
2309 }
2310
2311 /* mds/handler.c */
2312 extern int mds_msg_check_version(struct lustre_msg *msg);
2313
2314 static int mdt_handle0(struct ptlrpc_request *req,
2315                        struct mdt_thread_info *info,
2316                        struct mdt_opc_slice *supported)
2317 {
2318         struct mdt_handler *h;
2319         struct lustre_msg  *msg;
2320         int                 rc;
2321
2322         ENTRY;
2323
2324         OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
2325
2326         LASSERT(current->journal_info == NULL);
2327
2328         msg = req->rq_reqmsg;
2329         rc = mds_msg_check_version(msg);
2330         if (likely(rc == 0)) {
2331                 rc = mdt_recovery(info);
2332                 if (likely(rc == +1)) {
2333                         h = mdt_handler_find(lustre_msg_get_opc(msg),
2334                                              supported);
2335                         if (likely(h != NULL)) {
2336                                 rc = mdt_req_handle(info, h, req);
2337                                 rc = mdt_reply(req, rc, info);
2338                         } else {
2339                                 CERROR("The unsupported opc: 0x%x\n", lustre_msg_get_opc(msg) );
2340                                 req->rq_status = -ENOTSUPP;
2341                                 rc = ptlrpc_error(req);
2342                                 RETURN(rc);
2343                         }
2344                 }
2345         } else
2346                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2347         RETURN(rc);
2348 }
2349
2350 /*
2351  * MDT handler function called by ptlrpc service thread when request comes.
2352  *
2353  * XXX common "target" functionality should be factored into separate module
2354  * shared by mdt, ost and stand-alone services like fld.
2355  */
2356 static int mdt_handle_common(struct ptlrpc_request *req,
2357                              struct mdt_opc_slice *supported)
2358 {
2359         struct lu_env          *env;
2360         struct mdt_thread_info *info;
2361         int                     rc;
2362         ENTRY;
2363
2364         env = req->rq_svc_thread->t_env;
2365         LASSERT(env != NULL);
2366         LASSERT(env->le_ses != NULL);
2367         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2368         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2369         LASSERT(info != NULL);
2370
2371         mdt_thread_info_init(req, info);
2372
2373         rc = mdt_handle0(req, info, supported);
2374
2375         mdt_thread_info_fini(info);
2376         RETURN(rc);
2377 }
2378
2379 /*
2380  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2381  * as well.
2382  */
2383 int mdt_recovery_handle(struct ptlrpc_request *req)
2384 {
2385         int rc;
2386         ENTRY;
2387
2388         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2389         case FLD_QUERY:
2390                 rc = mdt_handle_common(req, mdt_fld_handlers);
2391                 break;
2392         case SEQ_QUERY:
2393                 rc = mdt_handle_common(req, mdt_seq_handlers);
2394                 break;
2395         default:
2396                 rc = mdt_handle_common(req, mdt_regular_handlers);
2397                 break;
2398         }
2399
2400         RETURN(rc);
2401 }
2402
2403 static int mdt_regular_handle(struct ptlrpc_request *req)
2404 {
2405         return mdt_handle_common(req, mdt_regular_handlers);
2406 }
2407
2408 static int mdt_readpage_handle(struct ptlrpc_request *req)
2409 {
2410         return mdt_handle_common(req, mdt_readpage_handlers);
2411 }
2412
2413 static int mdt_xmds_handle(struct ptlrpc_request *req)
2414 {
2415         return mdt_handle_common(req, mdt_xmds_handlers);
2416 }
2417
2418 static int mdt_mdsc_handle(struct ptlrpc_request *req)
2419 {
2420         return mdt_handle_common(req, mdt_seq_handlers);
2421 }
2422
2423 static int mdt_mdss_handle(struct ptlrpc_request *req)
2424 {
2425         return mdt_handle_common(req, mdt_seq_handlers);
2426 }
2427
2428 static int mdt_dtss_handle(struct ptlrpc_request *req)
2429 {
2430         return mdt_handle_common(req, mdt_seq_handlers);
2431 }
2432
2433 static int mdt_fld_handle(struct ptlrpc_request *req)
2434 {
2435         return mdt_handle_common(req, mdt_fld_handlers);
2436 }
2437
2438 enum mdt_it_code {
2439         MDT_IT_OPEN,
2440         MDT_IT_OCREAT,
2441         MDT_IT_CREATE,
2442         MDT_IT_GETATTR,
2443         MDT_IT_READDIR,
2444         MDT_IT_LOOKUP,
2445         MDT_IT_UNLINK,
2446         MDT_IT_TRUNC,
2447         MDT_IT_GETXATTR,
2448         MDT_IT_NR
2449 };
2450
2451 static int mdt_intent_getattr(enum mdt_it_code opcode,
2452                               struct mdt_thread_info *info,
2453                               struct ldlm_lock **,
2454                               int);
2455 static int mdt_intent_reint(enum mdt_it_code opcode,
2456                             struct mdt_thread_info *info,
2457                             struct ldlm_lock **,
2458                             int);
2459
2460 static struct mdt_it_flavor {
2461         const struct req_format *it_fmt;
2462         __u32                    it_flags;
2463         int                    (*it_act)(enum mdt_it_code ,
2464                                          struct mdt_thread_info *,
2465                                          struct ldlm_lock **,
2466                                          int);
2467         long                     it_reint;
2468 } mdt_it_flavor[] = {
2469         [MDT_IT_OPEN]     = {
2470                 .it_fmt   = &RQF_LDLM_INTENT,
2471                 /*.it_flags = HABEO_REFERO,*/
2472                 .it_flags = 0,
2473                 .it_act   = mdt_intent_reint,
2474                 .it_reint = REINT_OPEN
2475         },
2476         [MDT_IT_OCREAT]   = {
2477                 .it_fmt   = &RQF_LDLM_INTENT,
2478                 .it_flags = MUTABOR,
2479                 .it_act   = mdt_intent_reint,
2480                 .it_reint = REINT_OPEN
2481         },
2482         [MDT_IT_CREATE]   = {
2483                 .it_fmt   = &RQF_LDLM_INTENT,
2484                 .it_flags = MUTABOR,
2485                 .it_act   = mdt_intent_reint,
2486                 .it_reint = REINT_CREATE
2487         },
2488         [MDT_IT_GETATTR]  = {
2489                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2490                 .it_flags = HABEO_REFERO,
2491                 .it_act   = mdt_intent_getattr
2492         },
2493         [MDT_IT_READDIR]  = {
2494                 .it_fmt   = NULL,
2495                 .it_flags = 0,
2496                 .it_act   = NULL
2497         },
2498         [MDT_IT_LOOKUP]   = {
2499                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2500                 .it_flags = HABEO_REFERO,
2501                 .it_act   = mdt_intent_getattr
2502         },
2503         [MDT_IT_UNLINK]   = {
2504                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
2505                 .it_flags = MUTABOR,
2506                 .it_act   = NULL,
2507                 .it_reint = REINT_UNLINK
2508         },
2509         [MDT_IT_TRUNC]    = {
2510                 .it_fmt   = NULL,
2511                 .it_flags = MUTABOR,
2512                 .it_act   = NULL
2513         },
2514         [MDT_IT_GETXATTR] = {
2515                 .it_fmt   = NULL,
2516                 .it_flags = 0,
2517                 .it_act   = NULL
2518         }
2519 };
2520
2521 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2522                             struct ldlm_lock **lockp,
2523                             struct ldlm_lock *new_lock,
2524                             struct mdt_lock_handle *lh,
2525                             int flags)
2526 {
2527         struct ptlrpc_request  *req = mdt_info_req(info);
2528         struct ldlm_lock       *lock = *lockp;
2529
2530         /*
2531          * Get new lock only for cases when possible resent did not find any
2532          * lock.
2533          */
2534         if (new_lock == NULL)
2535                 new_lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2536
2537         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2538                 lh->mlh_reg_lh.cookie = 0;
2539                 RETURN(0);
2540         }
2541
2542         LASSERTF(new_lock != NULL,
2543                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2544
2545         /*
2546          * If we've already given this lock to a client once, then we should
2547          * have no readers or writers.  Otherwise, we should have one reader
2548          * _or_ writer ref (which will be zeroed below) before returning the
2549          * lock to a client.
2550          */
2551         if (new_lock->l_export == req->rq_export) {
2552                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2553         } else {
2554                 LASSERT(new_lock->l_export == NULL);
2555                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2556         }
2557
2558         *lockp = new_lock;
2559
2560         if (new_lock->l_export == req->rq_export) {
2561                 /*
2562                  * Already gave this to the client, which means that we
2563                  * reconstructed a reply.
2564                  */
2565                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2566                         MSG_RESENT);
2567                 lh->mlh_reg_lh.cookie = 0;
2568                 RETURN(ELDLM_LOCK_REPLACED);
2569         }
2570
2571         /* This lock might already be given to the client by an resent req,
2572          * in this case we should return ELDLM_LOCK_ABORTED,
2573          * so we should check led_held_locks here, but it will affect
2574          * performance, FIXME
2575          */
2576         /* Fixup the lock to be given to the client */
2577         lock_res_and_lock(new_lock);
2578         new_lock->l_readers = 0;
2579         new_lock->l_writers = 0;
2580
2581         new_lock->l_export = class_export_get(req->rq_export);
2582         spin_lock(&req->rq_export->exp_ldlm_data.led_lock);
2583         list_add(&new_lock->l_export_chain,
2584                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
2585         spin_unlock(&req->rq_export->exp_ldlm_data.led_lock);
2586
2587         new_lock->l_blocking_ast = lock->l_blocking_ast;
2588         new_lock->l_completion_ast = lock->l_completion_ast;
2589         new_lock->l_remote_handle = lock->l_remote_handle;
2590         new_lock->l_flags &= ~LDLM_FL_LOCAL;
2591
2592         unlock_res_and_lock(new_lock);
2593         LDLM_LOCK_PUT(new_lock);
2594         lh->mlh_reg_lh.cookie = 0;
2595
2596         RETURN(ELDLM_LOCK_REPLACED);
2597 }
2598
2599 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2600                                     struct ldlm_lock *new_lock,
2601                                     struct ldlm_lock **old_lock,
2602                                     struct mdt_lock_handle *lh)
2603 {
2604         struct ptlrpc_request  *req = mdt_info_req(info);
2605         struct obd_export      *exp = req->rq_export;
2606         struct lustre_handle    remote_hdl;
2607         struct ldlm_request    *dlmreq;
2608         struct list_head       *iter;
2609
2610         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2611                 return;
2612
2613         dlmreq = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
2614         remote_hdl = dlmreq->lock_handle[0];
2615
2616         spin_lock(&exp->exp_ldlm_data.led_lock);
2617         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2618                 struct ldlm_lock *lock;
2619                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2620                 if (lock == new_lock)
2621                         continue;
2622                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2623                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2624                         lh->mlh_reg_mode = lock->l_granted_mode;
2625
2626                         LDLM_DEBUG(lock, "restoring lock cookie");
2627                         DEBUG_REQ(D_DLMTRACE, req,
2628                                   "restoring lock cookie "LPX64,
2629                                   lh->mlh_reg_lh.cookie);
2630                         if (old_lock)
2631                                 *old_lock = LDLM_LOCK_GET(lock);
2632                         spin_unlock(&exp->exp_ldlm_data.led_lock);
2633                         return;
2634                 }
2635         }
2636         spin_unlock(&exp->exp_ldlm_data.led_lock);
2637
2638         /*
2639          * If the xid matches, then we know this is a resent request, and allow
2640          * it. (It's probably an OPEN, for which we don't send a lock.
2641          */
2642         if (req_xid_is_last(req))
2643                 return;
2644
2645         /*
2646          * This remote handle isn't enqueued, so we never received or processed
2647          * this request.  Clear MSG_RESENT, because it can be handled like any
2648          * normal request now.
2649          */
2650         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2651
2652         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2653                   remote_hdl.cookie);
2654 }
2655
2656 static int mdt_intent_getattr(enum mdt_it_code opcode,
2657                               struct mdt_thread_info *info,
2658                               struct ldlm_lock **lockp,
2659                               int flags)
2660 {
2661         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2662         struct ldlm_lock       *new_lock = NULL;
2663         __u64                   child_bits;
2664         struct ldlm_reply      *ldlm_rep;
2665         struct ptlrpc_request  *req;
2666         struct mdt_body        *reqbody;
2667         struct mdt_body        *repbody;
2668         int                     rc;
2669         ENTRY;
2670
2671         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
2672         LASSERT(reqbody);
2673
2674         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
2675         LASSERT(repbody);
2676
2677         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
2678         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
2679         repbody->eadatasize = 0;
2680         repbody->aclsize = 0;
2681
2682         switch (opcode) {
2683         case MDT_IT_LOOKUP:
2684                 child_bits = MDS_INODELOCK_LOOKUP;
2685                 break;
2686         case MDT_IT_GETATTR:
2687                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2688                 break;
2689         default:
2690                 CERROR("Unhandled till now");
2691                 GOTO(out, rc = -EINVAL);
2692         }
2693
2694         rc = mdt_init_ucred(info, reqbody);
2695         if (rc)
2696                 GOTO(out, rc);
2697
2698         req = info->mti_pill.rc_req;
2699         ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2700         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
2701
2702         /* Get lock from request for possible resent case. */
2703         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
2704
2705         ldlm_rep->lock_policy_res2 =
2706                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
2707
2708         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
2709                 ldlm_rep->lock_policy_res2 = 0;
2710         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
2711             ldlm_rep->lock_policy_res2) {
2712                 lhc->mlh_reg_lh.cookie = 0ull;
2713                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
2714         }
2715
2716         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
2717         EXIT;
2718 out_ucred:
2719         mdt_exit_ucred(info);
2720 out:
2721         mdt_shrink_reply(info);
2722         return rc;
2723 }
2724
2725 static int mdt_intent_reint(enum mdt_it_code opcode,
2726                             struct mdt_thread_info *info,
2727                             struct ldlm_lock **lockp,
2728                             int flags)
2729 {
2730         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2731         struct ldlm_reply      *rep = NULL;
2732         long                    opc;
2733         int                     rc;
2734
2735         static const struct req_format *intent_fmts[REINT_MAX] = {
2736                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
2737                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
2738         };
2739
2740         ENTRY;
2741
2742         opc = mdt_reint_opcode(info, intent_fmts);
2743         if (opc < 0)
2744                 GOTO(out, rc = opc);
2745
2746         if (mdt_it_flavor[opcode].it_reint != opc) {
2747                 CERROR("Reint code %ld doesn't match intent: %d\n",
2748                        opc, opcode);
2749                 GOTO(out, rc = err_serious(-EPROTO));
2750         }
2751
2752         /* Get lock from request for possible resent case. */
2753         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
2754
2755         rc = mdt_reint_internal(info, lhc, opc);
2756
2757         /* Check whether the reply has been packed successfully. */
2758         if (mdt_info_req(info)->rq_repmsg != NULL)
2759                 rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2760         if (rep == NULL)
2761                 GOTO(out, rc = err_serious(-EFAULT));
2762
2763         /* MDC expects this in any case */
2764         if (rc != 0)
2765                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
2766
2767         /* Cross-ref case, the lock should be returned to the client */
2768         if (rc == -EREMOTE) {
2769                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
2770                 rep->lock_policy_res2 = 0;
2771                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
2772                 GOTO(out, rc);
2773         }
2774         rep->lock_policy_res2 = clear_serious(rc);
2775
2776         lhc->mlh_reg_lh.cookie = 0ull;
2777         rc = ELDLM_LOCK_ABORTED;
2778         EXIT;
2779 out:
2780         return rc;
2781 }
2782
2783 static int mdt_intent_code(long itcode)
2784 {
2785         int rc;
2786
2787         switch(itcode) {
2788         case IT_OPEN:
2789                 rc = MDT_IT_OPEN;
2790                 break;
2791         case IT_OPEN|IT_CREAT:
2792                 rc = MDT_IT_OCREAT;
2793                 break;
2794         case IT_CREAT:
2795                 rc = MDT_IT_CREATE;
2796                 break;
2797         case IT_READDIR:
2798                 rc = MDT_IT_READDIR;
2799                 break;
2800         case IT_GETATTR:
2801                 rc = MDT_IT_GETATTR;
2802                 break;
2803         case IT_LOOKUP:
2804                 rc = MDT_IT_LOOKUP;
2805                 break;
2806         case IT_UNLINK:
2807                 rc = MDT_IT_UNLINK;
2808                 break;
2809         case IT_TRUNC:
2810                 rc = MDT_IT_TRUNC;
2811                 break;
2812         case IT_GETXATTR:
2813                 rc = MDT_IT_GETXATTR;
2814                 break;
2815         default:
2816                 CERROR("Unknown intent opcode: %ld\n", itcode);
2817                 rc = -EINVAL;
2818                 break;
2819         }
2820         return rc;
2821 }
2822
2823 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
2824                           struct ldlm_lock **lockp, int flags)
2825 {
2826         struct req_capsule   *pill;
2827         struct mdt_it_flavor *flv;
2828         int opc;
2829         int rc;
2830         ENTRY;
2831
2832         opc = mdt_intent_code(itopc);
2833         if (opc < 0)
2834                 RETURN(-EINVAL);
2835
2836         pill = &info->mti_pill;
2837         flv  = &mdt_it_flavor[opc];
2838
2839         if (flv->it_fmt != NULL)
2840                 req_capsule_extend(pill, flv->it_fmt);
2841
2842         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
2843         if (rc == 0) {
2844                 struct ptlrpc_request *req = mdt_info_req(info);
2845                 if (flv->it_flags & MUTABOR &&
2846                     req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2847                         rc = -EROFS;
2848         }
2849         if (rc == 0 && flv->it_act != NULL) {
2850                 /* execute policy */
2851                 rc = flv->it_act(opc, info, lockp, flags);
2852         } else
2853                 rc = -EOPNOTSUPP;
2854         RETURN(rc);
2855 }
2856
2857 static int mdt_intent_policy(struct ldlm_namespace *ns,
2858                              struct ldlm_lock **lockp, void *req_cookie,
2859                              ldlm_mode_t mode, int flags, void *data)
2860 {
2861         struct mdt_thread_info *info;
2862         struct ptlrpc_request  *req  =  req_cookie;
2863         struct ldlm_intent     *it;
2864         struct req_capsule     *pill;
2865         int rc;
2866
2867         ENTRY;
2868
2869         LASSERT(req != NULL);
2870
2871         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
2872                                   &mdt_thread_key);
2873         LASSERT(info != NULL);
2874         pill = &info->mti_pill;
2875         LASSERT(pill->rc_req == req);
2876
2877         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
2878                 req_capsule_extend(pill, &RQF_LDLM_INTENT);
2879                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
2880                 if (it != NULL) {
2881                         const struct ldlm_request *dlmreq;
2882                         __u64 req_bits;
2883 #if 0
2884                         struct ldlm_lock       *lock = *lockp;
2885
2886                         LDLM_DEBUG(lock, "intent policy opc: %s\n",
2887                                    ldlm_it2str(it->opc));
2888 #endif
2889
2890                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
2891                         if (rc == 0)
2892                                 rc = ELDLM_OK;
2893
2894                         /*
2895                          * Lock without inodebits makes no sense and will oops
2896                          * later in ldlm. Let's check it now to see if we have
2897                          * wrong lock from client or bits get corrupted
2898                          * somewhere in mdt_intent_opc().
2899                          */
2900                         dlmreq = info->mti_dlm_req;
2901                         req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
2902                         LASSERT(req_bits != 0);
2903
2904                 } else
2905                         rc = err_serious(-EFAULT);
2906         } else {
2907                 /* No intent was provided */
2908                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
2909                 rc = req_capsule_pack(pill);
2910                 if (rc)
2911                         rc = err_serious(rc);
2912         }
2913         RETURN(rc);
2914 }
2915
2916 /*
2917  * Seq wrappers
2918  */
2919 static void mdt_seq_adjust(const struct lu_env *env,
2920                           struct mdt_device *m, int lost)
2921 {
2922         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2923         struct lu_range out;
2924         ENTRY;
2925
2926         LASSERT(ls && ls->ls_server_seq);
2927         LASSERT(lost >= 0);
2928         /* get extra seq from seq_server, moving it's range up */
2929         while (lost-- > 0) {
2930                 seq_server_alloc_meta(ls->ls_server_seq, NULL, &out, env);
2931         }
2932         EXIT;
2933 }
2934
2935 static int mdt_seq_fini(const struct lu_env *env,
2936                         struct mdt_device *m)
2937 {
2938         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2939         ENTRY;
2940
2941         if (ls && ls->ls_server_seq) {
2942                 seq_server_fini(ls->ls_server_seq, env);
2943                 OBD_FREE_PTR(ls->ls_server_seq);
2944                 ls->ls_server_seq = NULL;
2945         }
2946
2947         if (ls && ls->ls_control_seq) {
2948                 seq_server_fini(ls->ls_control_seq, env);
2949                 OBD_FREE_PTR(ls->ls_control_seq);
2950                 ls->ls_control_seq = NULL;
2951         }
2952
2953         if (ls && ls->ls_client_seq) {
2954                 seq_client_fini(ls->ls_client_seq);
2955                 OBD_FREE_PTR(ls->ls_client_seq);
2956                 ls->ls_client_seq = NULL;
2957         }
2958
2959         RETURN(0);
2960 }
2961
2962 static int mdt_seq_init(const struct lu_env *env,
2963                         const char *uuid,
2964                         struct mdt_device *m)
2965 {
2966         struct lu_site *ls;
2967         char *prefix;
2968         int rc;
2969         ENTRY;
2970
2971         ls = m->mdt_md_dev.md_lu_dev.ld_site;
2972
2973         /*
2974          * This is sequence-controller node. Init seq-controller server on local
2975          * MDT.
2976          */
2977         if (ls->ls_node_id == 0) {
2978                 LASSERT(ls->ls_control_seq == NULL);
2979
2980                 OBD_ALLOC_PTR(ls->ls_control_seq);
2981                 if (ls->ls_control_seq == NULL)
2982                         RETURN(-ENOMEM);
2983
2984                 rc = seq_server_init(ls->ls_control_seq,
2985                                      m->mdt_bottom, uuid,
2986                                      LUSTRE_SEQ_CONTROLLER,
2987                                      env);
2988
2989                 if (rc)
2990                         GOTO(out_seq_fini, rc);
2991
2992                 OBD_ALLOC_PTR(ls->ls_client_seq);
2993                 if (ls->ls_client_seq == NULL)
2994                         GOTO(out_seq_fini, rc = -ENOMEM);
2995
2996                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
2997                 if (prefix == NULL) {
2998                         OBD_FREE_PTR(ls->ls_client_seq);
2999                         GOTO(out_seq_fini, rc = -ENOMEM);
3000                 }
3001
3002                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3003                          uuid);
3004
3005                 /*
3006                  * Init seq-controller client after seq-controller server is
3007                  * ready. Pass ls->ls_control_seq to it for direct talking.
3008                  */
3009                 rc = seq_client_init(ls->ls_client_seq, NULL,
3010                                      LUSTRE_SEQ_METADATA, prefix,
3011                                      ls->ls_control_seq);
3012                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3013
3014                 if (rc)
3015                         GOTO(out_seq_fini, rc);
3016         }
3017
3018         /* Init seq-server on local MDT */
3019         LASSERT(ls->ls_server_seq == NULL);
3020
3021         OBD_ALLOC_PTR(ls->ls_server_seq);
3022         if (ls->ls_server_seq == NULL)
3023                 GOTO(out_seq_fini, rc = -ENOMEM);
3024
3025         rc = seq_server_init(ls->ls_server_seq,
3026                              m->mdt_bottom, uuid,
3027                              LUSTRE_SEQ_SERVER,
3028                              env);
3029         if (rc)
3030                 GOTO(out_seq_fini, rc = -ENOMEM);
3031
3032         /* Assign seq-controller client to local seq-server. */
3033         if (ls->ls_node_id == 0) {
3034                 LASSERT(ls->ls_client_seq != NULL);
3035
3036                 rc = seq_server_set_cli(ls->ls_server_seq,
3037                                         ls->ls_client_seq,
3038                                         env);
3039         }
3040
3041         EXIT;
3042 out_seq_fini:
3043         if (rc)
3044                 mdt_seq_fini(env, m);
3045
3046         return rc;
3047 }
3048 /*
3049  * Init client sequence manager which is used by local MDS to talk to sequence
3050  * controller on remote node.
3051  */
3052 static int mdt_seq_init_cli(const struct lu_env *env,
3053                             struct mdt_device *m,
3054                             struct lustre_cfg *cfg)
3055 {
3056         struct lu_site    *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3057         struct obd_device *mdc;
3058         struct obd_uuid   *uuidp, *mdcuuidp;
3059         char              *uuid_str, *mdc_uuid_str;
3060         int                rc;
3061         int                index;
3062         struct mdt_thread_info *info;
3063         char *p, *index_string = lustre_cfg_string(cfg, 2);
3064         ENTRY;
3065
3066         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3067         uuidp = &info->mti_u.uuid[0];
3068         mdcuuidp = &info->mti_u.uuid[1];
3069
3070         LASSERT(index_string);
3071
3072         index = simple_strtol(index_string, &p, 10);
3073         if (*p) {
3074                 CERROR("Invalid index in lustre_cgf, offset 2\n");
3075                 RETURN(-EINVAL);
3076         }
3077
3078         /* check if this is adding the first MDC and controller is not yet
3079          * initialized. */
3080         if (index != 0 || ls->ls_client_seq)
3081                 RETURN(0);
3082
3083         uuid_str = lustre_cfg_string(cfg, 1);
3084         mdc_uuid_str = lustre_cfg_string(cfg, 4);
3085         obd_str2uuid(uuidp, uuid_str);
3086         obd_str2uuid(mdcuuidp, mdc_uuid_str);
3087
3088         mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3089         if (!mdc) {
3090                 CERROR("can't find controller MDC by uuid %s\n",
3091                        uuid_str);
3092                 rc = -ENOENT;
3093         } else if (!mdc->obd_set_up) {
3094                 CERROR("target %s not set up\n", mdc->obd_name);
3095                 rc = -EINVAL;
3096         } else {
3097                 LASSERT(ls->ls_control_exp);
3098                 OBD_ALLOC_PTR(ls->ls_client_seq);
3099                 if (ls->ls_client_seq != NULL) {
3100                         char *prefix;
3101
3102                         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3103                         if (!prefix)
3104                                 RETURN(-ENOMEM);
3105
3106                         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3107                                  mdc->obd_name);
3108
3109                         rc = seq_client_init(ls->ls_client_seq,
3110                                              ls->ls_control_exp,
3111                                              LUSTRE_SEQ_METADATA,
3112                                              prefix, NULL);
3113                         OBD_FREE(prefix, MAX_OBD_NAME + 5);
3114                 } else
3115                         rc = -ENOMEM;
3116
3117                 if (rc)
3118                         RETURN(rc);
3119
3120                 LASSERT(ls->ls_server_seq != NULL);
3121                 rc = seq_server_set_cli(ls->ls_server_seq, ls->ls_client_seq,
3122                                         env);
3123         }
3124
3125         RETURN(rc);
3126 }
3127
3128 static void mdt_seq_fini_cli(struct mdt_device *m)
3129 {
3130         struct lu_site *ls;
3131
3132         ENTRY;
3133
3134         ls = m->mdt_md_dev.md_lu_dev.ld_site;
3135
3136         if (ls && ls->ls_server_seq)
3137                 seq_server_set_cli(ls->ls_server_seq,
3138                                    NULL, NULL);
3139
3140         if (ls && ls->ls_control_exp) {
3141                 class_export_put(ls->ls_control_exp);
3142                 ls->ls_control_exp = NULL;
3143         }
3144         EXIT;
3145 }
3146
3147 /*
3148  * FLD wrappers
3149  */
3150 static int mdt_fld_fini(const struct lu_env *env,
3151                         struct mdt_device *m)
3152 {
3153         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3154         ENTRY;
3155
3156         if (ls && ls->ls_server_fld) {
3157                 fld_server_fini(ls->ls_server_fld, env);
3158                 OBD_FREE_PTR(ls->ls_server_fld);
3159                 ls->ls_server_fld = NULL;
3160         }
3161
3162         RETURN(0);
3163 }
3164
3165 static int mdt_fld_init(const struct lu_env *env,
3166                         const char *uuid,
3167                         struct mdt_device *m)
3168 {
3169         struct lu_site *ls;
3170         int rc;
3171         ENTRY;
3172
3173         ls = m->mdt_md_dev.md_lu_dev.ld_site;
3174
3175         OBD_ALLOC_PTR(ls->ls_server_fld);
3176         if (ls->ls_server_fld == NULL)
3177                 RETURN(rc = -ENOMEM);
3178
3179         rc = fld_server_init(ls->ls_server_fld,
3180                              m->mdt_bottom, uuid, env);
3181         if (rc) {
3182                 OBD_FREE_PTR(ls->ls_server_fld);
3183                 ls->ls_server_fld = NULL;
3184                 RETURN(rc);
3185         }
3186
3187         RETURN(0);
3188 }
3189
3190 /* device init/fini methods */
3191 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3192 {
3193         ENTRY;
3194         if (m->mdt_regular_service != NULL) {
3195                 ptlrpc_unregister_service(m->mdt_regular_service);
3196                 m->mdt_regular_service = NULL;
3197         }
3198         if (m->mdt_readpage_service != NULL) {
3199                 ptlrpc_unregister_service(m->mdt_readpage_service);
3200                 m->mdt_readpage_service = NULL;
3201         }
3202         if (m->mdt_xmds_service != NULL) {
3203                 ptlrpc_unregister_service(m->mdt_xmds_service);
3204                 m->mdt_xmds_service = NULL;
3205         }
3206         if (m->mdt_setattr_service != NULL) {
3207                 ptlrpc_unregister_service(m->mdt_setattr_service);
3208                 m->mdt_setattr_service = NULL;
3209         }
3210         if (m->mdt_mdsc_service != NULL) {
3211                 ptlrpc_unregister_service(m->mdt_mdsc_service);
3212                 m->mdt_mdsc_service = NULL;
3213         }
3214         if (m->mdt_mdss_service != NULL) {
3215                 ptlrpc_unregister_service(m->mdt_mdss_service);
3216                 m->mdt_mdss_service = NULL;
3217         }
3218         if (m->mdt_dtss_service != NULL) {
3219                 ptlrpc_unregister_service(m->mdt_dtss_service);
3220                 m->mdt_dtss_service = NULL;
3221         }
3222         if (m->mdt_fld_service != NULL) {
3223                 ptlrpc_unregister_service(m->mdt_fld_service);
3224                 m->mdt_fld_service = NULL;
3225         }
3226         ENTRY;
3227 }
3228
3229 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3230 {
3231         int rc;
3232         static struct ptlrpc_service_conf conf;
3233         cfs_proc_dir_entry_t *procfs_entry;
3234         ENTRY;
3235
3236         procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3237
3238         conf = (typeof(conf)) {
3239                 .psc_nbufs            = MDS_NBUFS,
3240                 .psc_bufsize          = MDS_BUFSIZE,
3241                 .psc_max_req_size     = MDS_MAXREQSIZE,
3242                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3243                 .psc_req_portal       = MDS_REQUEST_PORTAL,
3244                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3245                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3246                 /*
3247                  * We'd like to have a mechanism to set this on a per-device
3248                  * basis, but alas...
3249                  */
3250                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3251                                        MDT_MAX_THREADS),
3252                 .psc_max_threads   = MDT_MAX_THREADS,
3253                 .psc_ctx_tags      = LCT_MD_THREAD
3254         };
3255
3256         m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3257         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3258                            "mdt_ldlm_client", m->mdt_ldlm_client);
3259
3260         m->mdt_regular_service =
3261                 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3262                                      procfs_entry, NULL, LUSTRE_MDT_NAME);
3263         if (m->mdt_regular_service == NULL)
3264                 RETURN(-ENOMEM);
3265
3266         rc = ptlrpc_start_threads(NULL, m->mdt_regular_service);
3267         if (rc)
3268                 GOTO(err_mdt_svc, rc);
3269
3270         /*
3271          * readpage service configuration. Parameters have to be adjusted,
3272          * ideally.
3273          */
3274         conf = (typeof(conf)) {
3275                 .psc_nbufs            = MDS_NBUFS,
3276                 .psc_bufsize          = MDS_BUFSIZE,
3277                 .psc_max_req_size     = MDS_MAXREQSIZE,
3278                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3279                 .psc_req_portal       = MDS_READPAGE_PORTAL,
3280                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3281                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3282                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3283                                        MDT_MAX_THREADS),
3284                 .psc_max_threads   = MDT_MAX_THREADS,
3285                 .psc_ctx_tags      = LCT_MD_THREAD
3286         };
3287         m->mdt_readpage_service =
3288                 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3289                                      LUSTRE_MDT_NAME "_readpage",
3290                                      procfs_entry, NULL, "mdt_rdpg");
3291
3292         if (m->mdt_readpage_service == NULL) {
3293                 CERROR("failed to start readpage service\n");
3294                 GOTO(err_mdt_svc, rc = -ENOMEM);
3295         }
3296
3297         rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service);
3298
3299         /*
3300          * setattr service configuration.
3301          */
3302         conf = (typeof(conf)) {
3303                 .psc_nbufs            = MDS_NBUFS,
3304                 .psc_bufsize          = MDS_BUFSIZE,
3305                 .psc_max_req_size     = MDS_MAXREQSIZE,
3306                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3307                 .psc_req_portal       = MDS_SETATTR_PORTAL,
3308                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3309                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3310                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3311                                        MDT_MAX_THREADS),
3312                 .psc_max_threads   = MDT_MAX_THREADS,
3313                 .psc_ctx_tags      = LCT_MD_THREAD
3314         };
3315
3316         m->mdt_setattr_service =
3317                 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3318                                      LUSTRE_MDT_NAME "_setattr",
3319                                      procfs_entry, NULL, "mdt_attr");
3320
3321         if (!m->mdt_setattr_service) {
3322                 CERROR("failed to start setattr service\n");
3323                 GOTO(err_mdt_svc, rc = -ENOMEM);
3324         }
3325
3326         rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service);
3327         if (rc)
3328                 GOTO(err_mdt_svc, rc);
3329
3330         /*
3331          * sequence controller service configuration
3332          */
3333         conf = (typeof(conf)) {
3334                 .psc_nbufs = MDS_NBUFS,
3335                 .psc_bufsize = MDS_BUFSIZE,
3336                 .psc_max_req_size = SEQ_MAXREQSIZE,
3337                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3338                 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3339                 .psc_rep_portal = MDC_REPLY_PORTAL,
3340                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3341                 .psc_min_threads = SEQ_NUM_THREADS,
3342                 .psc_max_threads = SEQ_NUM_THREADS,
3343                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3344         };
3345
3346         m->mdt_mdsc_service =
3347                 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3348                                      LUSTRE_MDT_NAME"_mdsc",
3349                                      procfs_entry, NULL, "mdt_mdsc");
3350         if (!m->mdt_mdsc_service) {
3351                 CERROR("failed to start seq controller service\n");
3352                 GOTO(err_mdt_svc, rc = -ENOMEM);
3353         }
3354
3355         rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service);
3356         if (rc)
3357                 GOTO(err_mdt_svc, rc);
3358
3359         /*
3360          * metadata sequence server service configuration
3361          */
3362         conf = (typeof(conf)) {
3363                 .psc_nbufs = MDS_NBUFS,
3364                 .psc_bufsize = MDS_BUFSIZE,
3365                 .psc_max_req_size = SEQ_MAXREQSIZE,
3366                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3367                 .psc_req_portal = SEQ_METADATA_PORTAL,
3368                 .psc_rep_portal = MDC_REPLY_PORTAL,
3369                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3370                 .psc_min_threads = SEQ_NUM_THREADS,
3371                 .psc_max_threads = SEQ_NUM_THREADS,
3372                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3373         };
3374
3375         m->mdt_mdss_service =
3376                 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
3377                                      LUSTRE_MDT_NAME"_mdss",
3378                                      procfs_entry, NULL, "mdt_mdss");
3379         if (!m->mdt_mdss_service) {
3380                 CERROR("failed to start metadata seq server service\n");
3381                 GOTO(err_mdt_svc, rc = -ENOMEM);
3382         }
3383
3384         rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service);
3385         if (rc)
3386                 GOTO(err_mdt_svc, rc);
3387
3388
3389         /*
3390          * Data sequence server service configuration. We want to have really
3391          * cluster-wide sequences space. This is why we start only one sequence
3392          * controller which manages space.
3393          */
3394         conf = (typeof(conf)) {
3395                 .psc_nbufs = MDS_NBUFS,
3396                 .psc_bufsize = MDS_BUFSIZE,
3397                 .psc_max_req_size = SEQ_MAXREQSIZE,
3398                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3399                 .psc_req_portal = SEQ_DATA_PORTAL,
3400                 .psc_rep_portal = OSC_REPLY_PORTAL,
3401                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3402                 .psc_min_threads = SEQ_NUM_THREADS,
3403                 .psc_max_threads = SEQ_NUM_THREADS,
3404                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3405         };
3406
3407         m->mdt_dtss_service =
3408                 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
3409                                      LUSTRE_MDT_NAME"_dtss",
3410                                      procfs_entry, NULL, "mdt_dtss");
3411         if (!m->mdt_dtss_service) {
3412                 CERROR("failed to start data seq server service\n");
3413                 GOTO(err_mdt_svc, rc = -ENOMEM);
3414         }
3415
3416         rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service);
3417         if (rc)
3418                 GOTO(err_mdt_svc, rc);
3419
3420         /* FLD service start */
3421         conf = (typeof(conf)) {
3422                 .psc_nbufs            = MDS_NBUFS,
3423                 .psc_bufsize          = MDS_BUFSIZE,
3424                 .psc_max_req_size     = FLD_MAXREQSIZE,
3425                 .psc_max_reply_size   = FLD_MAXREPSIZE,
3426                 .psc_req_portal       = FLD_REQUEST_PORTAL,
3427                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3428                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3429                 .psc_min_threads      = FLD_NUM_THREADS,
3430                 .psc_max_threads      = FLD_NUM_THREADS,
3431                 .psc_ctx_tags         = LCT_DT_THREAD|LCT_MD_THREAD
3432         };
3433
3434         m->mdt_fld_service =
3435                 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
3436                                      LUSTRE_MDT_NAME"_fld",
3437                                      procfs_entry, NULL, "mdt_fld");
3438         if (!m->mdt_fld_service) {
3439                 CERROR("failed to start fld service\n");
3440                 GOTO(err_mdt_svc, rc = -ENOMEM);
3441         }
3442
3443         rc = ptlrpc_start_threads(NULL, m->mdt_fld_service);
3444         if (rc)
3445                 GOTO(err_mdt_svc, rc);
3446
3447         /*
3448          * mds-mds service configuration. Separate portal is used to allow
3449          * mds-mds requests be not blocked during recovery.
3450          */
3451         conf = (typeof(conf)) {
3452                 .psc_nbufs            = MDS_NBUFS,
3453                 .psc_bufsize          = MDS_BUFSIZE,
3454                 .psc_max_req_size     = MDS_MAXREQSIZE,
3455                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3456                 .psc_req_portal       = MDS_MDS_PORTAL,
3457                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3458                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3459                 .psc_min_threads      = min(max(mdt_num_threads, MDT_MIN_THREADS),
3460                                             MDT_MAX_THREADS),
3461                 .psc_max_threads      = MDT_MAX_THREADS,
3462                 .psc_ctx_tags         = LCT_MD_THREAD
3463         };
3464         m->mdt_xmds_service = ptlrpc_init_svc_conf(&conf, mdt_xmds_handle,
3465                                                   LUSTRE_MDT_NAME "_mds",
3466                                                   procfs_entry, NULL, "mdt_xmds");
3467
3468         if (m->mdt_xmds_service == NULL) {
3469                 CERROR("failed to start readpage service\n");
3470                 GOTO(err_mdt_svc, rc = -ENOMEM);
3471         }
3472
3473         rc = ptlrpc_start_threads(NULL, m->mdt_xmds_service);
3474         if (rc)
3475                 GOTO(err_mdt_svc, rc);
3476
3477         EXIT;
3478 err_mdt_svc:
3479         if (rc)
3480                 mdt_stop_ptlrpc_service(m);
3481
3482         return rc;
3483 }
3484
3485 static void mdt_stack_fini(const struct lu_env *env,
3486                            struct mdt_device *m, struct lu_device *top)
3487 {
3488         struct lu_device        *d = top, *n;
3489         struct obd_device       *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3490         struct lustre_cfg_bufs  *bufs;
3491         struct lustre_cfg       *lcfg;
3492         struct mdt_thread_info  *info;
3493         char flags[3]="";
3494         ENTRY;
3495
3496         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3497         LASSERT(info != NULL);
3498
3499         bufs = &info->mti_u.bufs;
3500         /* process cleanup, pass mdt obd name to get obd umount flags */
3501         lustre_cfg_bufs_reset(bufs, obd->obd_name);
3502         if (obd->obd_force)
3503                 strcat(flags, "F");
3504         if (obd->obd_fail)
3505                 strcat(flags, "A");
3506         lustre_cfg_bufs_set_string(bufs, 1, flags);
3507         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3508         if (!lcfg) {
3509                 CERROR("Cannot alloc lcfg!\n");
3510                 return;
3511         }
3512
3513         LASSERT(top);
3514         top->ld_ops->ldo_process_config(env, top, lcfg);
3515         lustre_cfg_free(lcfg);
3516
3517         lu_site_purge(env, top->ld_site, ~0);
3518         while (d != NULL) {
3519                 struct obd_type *type;
3520                 struct lu_device_type *ldt = d->ld_type;
3521
3522                 /* each fini() returns next device in stack of layers
3523                  * * so we can avoid the recursion */
3524                 n = ldt->ldt_ops->ldto_device_fini(env, d);
3525                 lu_device_put(d);
3526                 ldt->ldt_ops->ldto_device_free(env, d);
3527                 type = ldt->ldt_obd_type;
3528                 type->typ_refcnt--;
3529                 class_put_type(type);
3530
3531                 /* switch to the next device in the layer */
3532                 d = n;
3533         }
3534         m->mdt_child = NULL;
3535         m->mdt_bottom = NULL;
3536 }
3537
3538 static struct lu_device *mdt_layer_setup(const struct lu_env *env,
3539                                          const char *typename,
3540                                          struct lu_device *child,
3541                                          struct lustre_cfg *cfg)
3542 {
3543         const char            *dev = lustre_cfg_string(cfg, 0);
3544         struct obd_type       *type;
3545         struct lu_device_type *ldt;
3546         struct lu_device      *d;
3547         int rc;
3548         ENTRY;
3549
3550         /* find the type */
3551         type = class_get_type(typename);
3552         if (!type) {
3553                 CERROR("Unknown type: '%s'\n", typename);
3554                 GOTO(out, rc = -ENODEV);
3555         }
3556
3557         rc = lu_context_refill(&env->le_ctx);
3558         if (rc != 0) {
3559                 CERROR("Failure to refill context: '%d'\n", rc);
3560                 GOTO(out_type, rc);
3561         }
3562
3563         if (env->le_ses != NULL) {
3564                 rc = lu_context_refill(env->le_ses);
3565                 if (rc != 0) {
3566                         CERROR("Failure to refill session: '%d'\n", rc);
3567                         GOTO(out_type, rc);
3568                 }
3569         }
3570
3571         ldt = type->typ_lu;
3572         if (ldt == NULL) {
3573                 CERROR("type: '%s'\n", typename);
3574                 GOTO(out_type, rc = -EINVAL);
3575         }
3576
3577         ldt->ldt_obd_type = type;
3578         d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
3579         if (IS_ERR(d)) {
3580                 CERROR("Cannot allocate device: '%s'\n", typename);
3581                 GOTO(out_type, rc = -ENODEV);
3582         }
3583
3584         LASSERT(child->ld_site);
3585         d->ld_site = child->ld_site;
3586
3587         type->typ_refcnt++;
3588         rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
3589         if (rc) {
3590                 CERROR("can't init device '%s', rc %d\n", typename, rc);
3591                 GOTO(out_alloc, rc);
3592         }
3593         lu_device_get(d);
3594
3595         RETURN(d);
3596
3597 out_alloc:
3598         ldt->ldt_ops->ldto_device_free(env, d);
3599         type->typ_refcnt--;
3600 out_type:
3601         class_put_type(type);
3602 out:
3603         return ERR_PTR(rc);
3604 }
3605
3606 static int mdt_stack_init(const struct lu_env *env,
3607                           struct mdt_device *m, struct lustre_cfg *cfg)
3608 {
3609         struct lu_device  *d = &m->mdt_md_dev.md_lu_dev;
3610         struct lu_device  *tmp;
3611         struct md_device  *md;
3612         int rc;
3613         ENTRY;
3614
3615         /* init the stack */
3616         tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
3617         if (IS_ERR(tmp)) {
3618                 RETURN(PTR_ERR(tmp));
3619         }
3620         m->mdt_bottom = lu2dt_dev(tmp);
3621         d = tmp;
3622         tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
3623         if (IS_ERR(tmp)) {
3624                 GOTO(out, rc = PTR_ERR(tmp));
3625         }
3626         d = tmp;
3627         md = lu2md_dev(d);
3628
3629         tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
3630         if (IS_ERR(tmp)) {
3631                 GOTO(out, rc = PTR_ERR(tmp));
3632         }
3633         d = tmp;
3634         /*set mdd upcall device*/
3635         md_upcall_dev_set(md, lu2md_dev(d));
3636
3637         md = lu2md_dev(d);
3638         /*set cmm upcall device*/
3639         md_upcall_dev_set(md, &m->mdt_md_dev);
3640
3641         m->mdt_child = lu2md_dev(d);
3642
3643         /* process setup config */
3644         tmp = &m->mdt_md_dev.md_lu_dev;
3645         rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
3646         GOTO(out, rc);
3647 out:
3648         /* fini from last known good lu_device */
3649         if (rc)
3650                 mdt_stack_fini(env, m, d);
3651
3652         return rc;
3653 }
3654
3655 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
3656 {
3657         struct md_device *next = m->mdt_child;
3658         struct lu_device *d    = &m->mdt_md_dev.md_lu_dev;
3659         struct lu_site   *ls   = d->ld_site;
3660         struct obd_device *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3661         ENTRY;
3662
3663         ping_evictor_stop();
3664         
3665         target_recovery_fini(obd);
3666         mdt_stop_ptlrpc_service(m);
3667
3668         mdt_fs_cleanup(env, m);
3669
3670         upcall_cache_cleanup(m->mdt_rmtacl_cache);
3671         m->mdt_rmtacl_cache = NULL;
3672
3673         upcall_cache_cleanup(m->mdt_identity_cache);
3674         m->mdt_identity_cache = NULL;
3675
3676         if (m->mdt_namespace != NULL) {
3677                 ldlm_namespace_free(m->mdt_namespace, d->ld_obd->obd_force);
3678                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
3679         }
3680
3681         mdt_seq_fini(env, m);
3682         mdt_seq_fini_cli(m);
3683         mdt_fld_fini(env, m);
3684         mdt_procfs_fini(m);
3685         ptlrpc_lprocfs_unregister_obd(d->ld_obd);
3686         lprocfs_obd_cleanup(d->ld_obd);
3687
3688         if (m->mdt_rootsquash_info) {
3689                 OBD_FREE_PTR(m->mdt_rootsquash_info);
3690                 m->mdt_rootsquash_info = NULL;
3691         }
3692
3693         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
3694         del_timer(&m->mdt_ck_timer);
3695         mdt_ck_thread_stop(m);
3696
3697         /* finish the stack */
3698         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3699
3700         if (ls) {
3701                 if (!list_empty(&ls->ls_lru) || ls->ls_total != 0) {
3702                         /*
3703                          * Uh-oh, objects still exist.
3704                          */
3705                         static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
3706
3707                         lu_site_print(env, ls, &cookie, lu_cdebug_printer);
3708                 }
3709
3710                 lu_site_fini(ls);
3711                 OBD_FREE_PTR(ls);
3712                 d->ld_site = NULL;
3713         }
3714         LASSERT(atomic_read(&d->ld_ref) == 0);
3715         md_device_fini(&m->mdt_md_dev);
3716
3717         EXIT;
3718 }
3719
3720 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
3721 {
3722         char *p = options;
3723
3724         if (!options)
3725                 return;
3726
3727         while (*options) {
3728                 int len;
3729
3730                 while (*p && *p != ',')
3731                         p++;
3732
3733                 len = p - options;
3734                 if ((len == sizeof("user_xattr") - 1) &&
3735                     (memcmp(options, "user_xattr", len) == 0)) {
3736                         m->mdt_opts.mo_user_xattr = 1;
3737                         LCONSOLE_INFO("Enabling user_xattr\n");
3738                 } else if ((len == sizeof("nouser_xattr") - 1) &&
3739                            (memcmp(options, "nouser_xattr", len) == 0)) {
3740                         m->mdt_opts.mo_user_xattr = 0;
3741                         LCONSOLE_INFO("Disabling user_xattr\n");
3742                 } else if ((len == sizeof("acl") - 1) &&
3743                            (memcmp(options, "acl", len) == 0)) {
3744 #ifdef CONFIG_FS_POSIX_ACL
3745                         m->mdt_opts.mo_acl = 1;
3746                         LCONSOLE_INFO("Enabling ACL\n");
3747 #else
3748                         m->mdt_opts.mo_acl = 0;
3749                         CWARN("ignoring unsupported acl mount option\n");
3750                         LCONSOLE_INFO("Disabling ACL\n");
3751 #endif
3752                 } else if ((len == sizeof("noacl") - 1) &&
3753                            (memcmp(options, "noacl", len) == 0)) {
3754                         m->mdt_opts.mo_acl = 0;
3755                         LCONSOLE_INFO("Disabling ACL\n");
3756                 }
3757
3758                 options = ++p;
3759         }
3760 }
3761
3762 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
3763
3764 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
3765                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
3766 {
3767         struct lprocfs_static_vars lvars;
3768         struct mdt_thread_info    *info;
3769         struct obd_device         *obd;
3770         const char                *dev = lustre_cfg_string(cfg, 0);
3771         const char                *num = lustre_cfg_string(cfg, 2);
3772         struct lustre_mount_info  *lmi;
3773         struct lustre_sb_info     *lsi;
3774         struct lu_site            *s;
3775         int                        rc;
3776         ENTRY;
3777
3778         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3779         LASSERT(info != NULL);
3780
3781         obd = class_name2obd(dev);
3782         LASSERT(obd != NULL);
3783
3784         spin_lock_init(&m->mdt_transno_lock);
3785
3786         m->mdt_max_mdsize = MAX_MD_SIZE;
3787         m->mdt_max_cookiesize = sizeof(struct llog_cookie);
3788
3789         m->mdt_opts.mo_user_xattr = 0;
3790         m->mdt_opts.mo_acl = 0;
3791         lmi = server_get_mount_2(dev);
3792         if (lmi == NULL) {
3793                 CERROR("Cannot get mount info for %s!\n", dev);
3794                 RETURN(-EFAULT);
3795         } else {
3796                 lsi = s2lsi(lmi->lmi_sb);
3797                 fsoptions_to_mdt_flags(m, lsi->lsi_lmd->lmd_opts);
3798                 server_put_mount_2(dev, lmi->lmi_mnt);
3799         }
3800
3801         spin_lock_init(&m->mdt_ioepoch_lock);
3802         m->mdt_opts.mo_compat_resname = 0;
3803         m->mdt_capa_timeout = CAPA_TIMEOUT;
3804         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
3805         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
3806
3807         spin_lock_init(&m->mdt_client_bitmap_lock);
3808
3809         OBD_ALLOC_PTR(s);
3810         if (s == NULL)
3811                 RETURN(-ENOMEM);
3812
3813         md_device_init(&m->mdt_md_dev, ldt);
3814         m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
3815         m->mdt_md_dev.md_lu_dev.ld_obd = obd;
3816         /* set this lu_device to obd, because error handling need it */
3817         obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
3818
3819         rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
3820         if (rc) {
3821                 CERROR("Can't init lu_site, rc %d\n", rc);
3822                 GOTO(err_free_site, rc);
3823         }
3824
3825         lprocfs_init_vars(mdt, &lvars);
3826         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
3827         if (rc) {
3828                 CERROR("Can't init lprocfs, rc %d\n", rc);
3829                 GOTO(err_fini_site, rc);
3830         }
3831         ptlrpc_lprocfs_register_obd(obd);
3832
3833         rc = mdt_procfs_init(m, dev);
3834         if (rc) {
3835                 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
3836                 GOTO(err_fini_proc, rc);
3837         }
3838
3839         /* set server index */
3840         LASSERT(num);
3841         s->ls_node_id = simple_strtol(num, NULL, 10);
3842
3843         /* failover is the default
3844          * FIXME: we do not failout mds0/mgs, which may cause some problems.
3845          * assumed whose ls_node_id == 0 XXX
3846          * */
3847         obd->obd_replayable = 1;
3848         /* No connection accepted until configurations will finish */
3849         obd->obd_no_conn = 1;
3850
3851         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
3852                 char *str = lustre_cfg_string(cfg, 4);
3853                 if (strchr(str, 'n')) {
3854                         CWARN("%s: recovery disabled\n", obd->obd_name);
3855                         obd->obd_replayable = 0;
3856                 }
3857         }
3858
3859         /* init the stack */
3860         rc = mdt_stack_init(env, m, cfg);
3861         if (rc) {
3862                 CERROR("Can't init device stack, rc %d\n", rc);
3863                 GOTO(err_fini_proc, rc);
3864         }
3865
3866         rc = mdt_fld_init(env, obd->obd_name, m);
3867         if (rc)
3868                 GOTO(err_fini_stack, rc);
3869
3870         rc = mdt_seq_init(env, obd->obd_name, m);
3871         if (rc)
3872                 GOTO(err_fini_fld, rc);
3873
3874         snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
3875                  LUSTRE_MDT_NAME"-%p", m);
3876         m->mdt_namespace = ldlm_namespace_new(info->mti_u.ns_name,
3877                                               LDLM_NAMESPACE_SERVER,
3878                                               LDLM_NAMESPACE_GREEDY);
3879         if (m->mdt_namespace == NULL)
3880                 GOTO(err_fini_seq, rc = -ENOMEM);
3881
3882         ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
3883         /* set obd_namespace for compatibility with old code */
3884         obd->obd_namespace = m->mdt_namespace;
3885
3886         m->mdt_identity_cache = upcall_cache_init(obd->obd_name,
3887                                                   "NONE",
3888                                                   &mdt_identity_upcall_cache_ops);
3889         if (IS_ERR(m->mdt_identity_cache)) {
3890                 rc = PTR_ERR(m->mdt_identity_cache);
3891                 m->mdt_identity_cache = NULL;
3892                 GOTO(err_free_ns, rc);
3893         }
3894
3895         m->mdt_rmtacl_cache = upcall_cache_init(obd->obd_name,
3896                                                 MDT_RMTACL_UPCALL_PATH,
3897                                                 &mdt_rmtacl_upcall_cache_ops);
3898         if (IS_ERR(m->mdt_rmtacl_cache)) {
3899                 rc = PTR_ERR(m->mdt_rmtacl_cache);
3900                 m->mdt_rmtacl_cache = NULL;
3901                 GOTO(err_free_ns, rc);
3902         }
3903
3904         m->mdt_ck_timer.function = mdt_ck_timer_callback;
3905         m->mdt_ck_timer.data = (unsigned long)m;
3906         init_timer(&m->mdt_ck_timer);
3907         rc = mdt_ck_thread_start(m);
3908         if (rc)
3909                 GOTO(err_free_ns, rc);
3910
3911         rc = mdt_fs_setup(env, m, obd);
3912         if (rc)
3913                 GOTO(err_capa, rc);
3914
3915         target_recovery_init(obd, mdt_recovery_handle);
3916
3917         rc = mdt_start_ptlrpc_service(m);
3918         if (rc)
3919                 GOTO(err_fs_cleanup, rc);
3920
3921         ping_evictor_start();
3922
3923         rc = lu_site_init_finish(s);
3924         if (rc)
3925                 GOTO(err_stop_service, rc);
3926
3927         if (obd->obd_recovering == 0)
3928                 mdt_postrecov(env, m);
3929
3930         mdt_init_capa_ctxt(env, m);
3931
3932         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
3933                 ldlm_timeout = 6;
3934
3935         RETURN(0);
3936
3937 err_stop_service:
3938         ping_evictor_stop();
3939         mdt_stop_ptlrpc_service(m);
3940 err_fs_cleanup:
3941         target_recovery_fini(obd);
3942         mdt_fs_cleanup(env, m);
3943 err_capa:
3944         del_timer(&m->mdt_ck_timer);
3945         mdt_ck_thread_stop(m);
3946 err_free_ns:
3947         upcall_cache_cleanup(m->mdt_rmtacl_cache);
3948         m->mdt_rmtacl_cache = NULL;
3949         upcall_cache_cleanup(m->mdt_identity_cache);
3950         m->mdt_identity_cache = NULL;
3951         ldlm_namespace_free(m->mdt_namespace, 0);
3952         obd->obd_namespace = m->mdt_namespace = NULL;
3953 err_fini_seq:
3954         mdt_seq_fini(env, m);
3955 err_fini_fld:
3956         mdt_fld_fini(env, m);
3957 err_fini_stack:
3958         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3959 err_fini_proc:
3960         mdt_procfs_fini(m);
3961         lprocfs_obd_cleanup(obd);
3962 err_fini_site:
3963         lu_site_fini(s);
3964 err_free_site:
3965         OBD_FREE_PTR(s);
3966
3967         md_device_fini(&m->mdt_md_dev);
3968         return (rc);
3969 }
3970
3971 /* used by MGS to process specific configurations */
3972 static int mdt_process_config(const struct lu_env *env,
3973                               struct lu_device *d, struct lustre_cfg *cfg)
3974 {
3975         struct mdt_device *m = mdt_dev(d);
3976         struct md_device *md_next = m->mdt_child;
3977         struct lu_device *next = md2lu_dev(md_next);
3978         int rc = 0;
3979         ENTRY;
3980
3981         switch (cfg->lcfg_command) {
3982         case LCFG_PARAM: {
3983                 struct lprocfs_static_vars lvars;
3984                 struct obd_device *obd = d->ld_obd;
3985
3986                 lprocfs_init_vars(mdt, &lvars);
3987                 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars, cfg, obd);
3988                 if (rc)
3989                         /* others are passed further */
3990                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
3991                 break;
3992         }
3993         case LCFG_ADD_MDC:
3994                 /*
3995                  * Add mdc hook to get first MDT uuid and connect it to
3996                  * ls->controller to use for seq manager.
3997                  */
3998                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
3999                 if (rc)
4000                         CERROR("Can't add mdc, rc %d\n", rc);
4001                 else
4002                         rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
4003                 break;
4004         default:
4005                 /* others are passed further */
4006                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4007                 break;
4008         }
4009         RETURN(rc);
4010 }
4011
4012 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4013                                           const struct lu_object_header *hdr,
4014                                           struct lu_device *d)
4015 {
4016         struct mdt_object *mo;
4017
4018         ENTRY;
4019
4020         OBD_ALLOC_PTR(mo);
4021         if (mo != NULL) {
4022                 struct lu_object *o;
4023                 struct lu_object_header *h;
4024
4025                 o = &mo->mot_obj.mo_lu;
4026                 h = &mo->mot_header;
4027                 lu_object_header_init(h);
4028                 lu_object_init(o, h, d);
4029                 lu_object_add_top(h, o);
4030                 o->lo_ops = &mdt_obj_ops;
4031                 RETURN(o);
4032         } else
4033                 RETURN(NULL);
4034 }
4035
4036 static int mdt_object_init(const struct lu_env *env, struct lu_object *o)
4037 {
4038         struct mdt_device *d = mdt_dev(o->lo_dev);
4039         struct lu_device  *under;
4040         struct lu_object  *below;
4041         int                rc = 0;
4042         ENTRY;
4043
4044         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4045                PFID(lu_object_fid(o)));
4046
4047         under = &d->mdt_child->md_lu_dev;
4048         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4049         if (below != NULL) {
4050                 lu_object_add(o, below);
4051         } else
4052                 rc = -ENOMEM;
4053
4054         RETURN(rc);
4055 }
4056
4057 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4058 {
4059         struct mdt_object *mo = mdt_obj(o);
4060         struct lu_object_header *h;
4061         ENTRY;
4062
4063         h = o->lo_header;
4064         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4065                PFID(lu_object_fid(o)));
4066
4067         lu_object_fini(o);
4068         lu_object_header_fini(h);
4069         OBD_FREE_PTR(mo);
4070         EXIT;
4071 }
4072
4073 static int mdt_object_print(const struct lu_env *env, void *cookie,
4074                             lu_printer_t p, const struct lu_object *o)
4075 {
4076         return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p", o);
4077 }
4078
4079 static struct lu_device_operations mdt_lu_ops = {
4080         .ldo_object_alloc   = mdt_object_alloc,
4081         .ldo_process_config = mdt_process_config
4082 };
4083
4084 static struct lu_object_operations mdt_obj_ops = {
4085         .loo_object_init    = mdt_object_init,
4086         .loo_object_free    = mdt_object_free,
4087         .loo_object_print   = mdt_object_print
4088 };
4089
4090 /* mds_connect_internal */
4091 static int mdt_connect_internal(struct obd_export *exp,
4092                                 struct mdt_device *mdt,
4093                                 struct obd_connect_data *data)
4094 {
4095         __u64 flags;
4096
4097         if (data != NULL) {
4098                 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4099                 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4100
4101                 /* If no known bits (which should not happen, probably,
4102                    as everybody should support LOOKUP and UPDATE bits at least)
4103                    revert to compat mode with plain locks. */
4104                 if (!data->ocd_ibits_known &&
4105                     data->ocd_connect_flags & OBD_CONNECT_IBITS)
4106                         data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4107
4108                 if (!mdt->mdt_opts.mo_acl)
4109                         data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4110
4111                 if (!mdt->mdt_opts.mo_user_xattr)
4112                         data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4113
4114                 if (!mdt->mdt_opts.mo_mds_capa)
4115                         data->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
4116
4117                 if (!mdt->mdt_opts.mo_oss_capa)
4118                         data->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
4119
4120                 spin_lock(&exp->exp_lock);
4121                 exp->exp_connect_flags = data->ocd_connect_flags;
4122                 spin_unlock(&exp->exp_lock);
4123                 data->ocd_version = LUSTRE_VERSION_CODE;
4124                 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
4125         }
4126
4127 #if 0
4128         if (mdt->mdt_opts.mo_acl &&
4129             ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
4130                 CWARN("%s: MDS requires ACL support but client does not\n",
4131                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4132                 return -EBADE;
4133         }
4134 #endif
4135
4136         flags = OBD_CONNECT_LCL_CLIENT | OBD_CONNECT_RMT_CLIENT;
4137         if ((exp->exp_connect_flags & flags) == flags) {
4138                 CWARN("%s: both local and remote client flags are set\n",
4139                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4140                 return -EBADE;
4141         }
4142
4143         if (mdt->mdt_opts.mo_mds_capa &&
4144             ((exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) == 0)) {
4145                 CWARN("%s: MDS requires capability support, but client not\n",
4146                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4147                 return -EBADE;
4148         }
4149
4150         if (mdt->mdt_opts.mo_oss_capa &&
4151             ((exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA) == 0)) {
4152                 CWARN("%s: MDS requires OSS capability support, "
4153                       "but client not\n",
4154                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4155                 return -EBADE;
4156         }
4157
4158         return 0;
4159 }
4160
4161 /* mds_connect copy */
4162 static int mdt_obd_connect(const struct lu_env *env,
4163                            struct lustre_handle *conn, struct obd_device *obd,
4164                            struct obd_uuid *cluuid,
4165                            struct obd_connect_data *data)
4166 {
4167         struct mdt_client_data *mcd;
4168         struct obd_export      *exp;
4169         struct mdt_device      *mdt;
4170         int                     rc;
4171         ENTRY;
4172
4173         LASSERT(env != NULL);
4174         if (!conn || !obd || !cluuid)
4175                 RETURN(-EINVAL);
4176
4177         mdt = mdt_dev(obd->obd_lu_dev);
4178
4179         rc = class_connect(conn, obd, cluuid);
4180         if (rc)
4181                 RETURN(rc);
4182
4183         exp = class_conn2export(conn);
4184         LASSERT(exp != NULL);
4185
4186         rc = mdt_connect_internal(exp, mdt, data);
4187         if (rc == 0) {
4188                 OBD_ALLOC_PTR(mcd);
4189                 if (mcd != NULL) {
4190                         struct mdt_thread_info *mti;
4191                         mti = lu_context_key_get(&env->le_ctx,
4192                                                  &mdt_thread_key);
4193                         LASSERT(mti != NULL);
4194                         mti->mti_exp = exp;
4195                         memcpy(mcd->mcd_uuid, cluuid, sizeof mcd->mcd_uuid);
4196                         exp->exp_mdt_data.med_mcd = mcd;
4197                         rc = mdt_client_new(env, mdt);
4198                         if (rc != 0) {
4199                                 OBD_FREE_PTR(mcd);
4200                                 exp->exp_mdt_data.med_mcd = NULL;
4201                         }
4202                 } else
4203                         rc = -ENOMEM;
4204         }
4205
4206         if (rc != 0)
4207                 class_disconnect(exp);
4208         else
4209                 class_export_put(exp);
4210
4211         RETURN(rc);
4212 }
4213
4214 static int mdt_obd_reconnect(struct obd_export *exp, struct obd_device *obd,
4215                              struct obd_uuid *cluuid,
4216                              struct obd_connect_data *data)
4217 {
4218         int rc;
4219         ENTRY;
4220
4221         if (exp == NULL || obd == NULL || cluuid == NULL)
4222                 RETURN(-EINVAL);
4223
4224         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
4225
4226         RETURN(rc);
4227 }
4228
4229 static int mdt_obd_disconnect(struct obd_export *exp)
4230 {
4231         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
4232         int rc;
4233         ENTRY;
4234
4235         LASSERT(exp);
4236         class_export_get(exp);
4237
4238         /* Disconnect early so that clients can't keep using export */
4239         rc = class_disconnect(exp);
4240         if (mdt->mdt_namespace != NULL || exp->exp_obd->obd_namespace != NULL)
4241                 ldlm_cancel_locks_for_export(exp);
4242
4243         /* complete all outstanding replies */
4244         spin_lock(&exp->exp_lock);
4245         while (!list_empty(&exp->exp_outstanding_replies)) {
4246                 struct ptlrpc_reply_state *rs =
4247                         list_entry(exp->exp_outstanding_replies.next,
4248                                    struct ptlrpc_reply_state, rs_exp_list);
4249                 struct ptlrpc_service *svc = rs->rs_service;
4250
4251                 spin_lock(&svc->srv_lock);
4252                 list_del_init(&rs->rs_exp_list);
4253                 ptlrpc_schedule_difficult_reply(rs);
4254                 spin_unlock(&svc->srv_lock);
4255         }
4256         spin_unlock(&exp->exp_lock);
4257
4258         class_export_put(exp);
4259         RETURN(rc);
4260 }
4261
4262 /* FIXME: Can we avoid using these two interfaces? */
4263 static int mdt_init_export(struct obd_export *exp)
4264 {
4265         struct mdt_export_data *med = &exp->exp_mdt_data;
4266         ENTRY;
4267
4268         INIT_LIST_HEAD(&med->med_open_head);
4269         spin_lock_init(&med->med_open_lock);
4270         spin_lock(&exp->exp_lock);
4271         exp->exp_connecting = 1;
4272         spin_unlock(&exp->exp_lock);
4273         RETURN(0);
4274 }
4275
4276 static int mdt_destroy_export(struct obd_export *export)
4277 {
4278         struct mdt_export_data *med;
4279         struct obd_device      *obd = export->exp_obd;
4280         struct mdt_device      *mdt;
4281         struct mdt_thread_info *info;
4282         struct lu_env           env;
4283         struct md_attr         *ma;
4284         int lmm_size;
4285         int cookie_size;
4286         int rc = 0;
4287         ENTRY;
4288
4289         med = &export->exp_mdt_data;
4290         if (med->med_rmtclient)
4291                 mdt_cleanup_idmap(med);
4292
4293         target_destroy_export(export);
4294
4295         if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
4296                 RETURN(0);
4297
4298         mdt = mdt_dev(obd->obd_lu_dev);
4299         LASSERT(mdt != NULL);
4300
4301         rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4302         if (rc)
4303                 RETURN(rc);
4304
4305         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
4306         LASSERT(info != NULL);
4307         memset(info, 0, sizeof *info);
4308         info->mti_env = &env;
4309         info->mti_mdt = mdt;
4310         info->mti_exp = export;
4311
4312         ma = &info->mti_attr;
4313         lmm_size = ma->ma_lmm_size = mdt->mdt_max_mdsize;
4314         cookie_size = ma->ma_cookie_size = mdt->mdt_max_cookiesize;
4315         OBD_ALLOC(ma->ma_lmm, lmm_size);
4316         OBD_ALLOC(ma->ma_cookie, cookie_size);
4317
4318         if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
4319                 GOTO(out, rc = -ENOMEM);
4320         ma->ma_need = MA_LOV | MA_COOKIE;
4321         ma->ma_valid = 0;
4322         /* Close any open files (which may also cause orphan unlinking). */
4323         spin_lock(&med->med_open_lock);
4324         while (!list_empty(&med->med_open_head)) {
4325                 struct list_head *tmp = med->med_open_head.next;
4326                 struct mdt_file_data *mfd =
4327                         list_entry(tmp, struct mdt_file_data, mfd_list);
4328
4329                 /* Remove mfd handle so it can't be found again.
4330                  * We are consuming the mfd_list reference here. */
4331                 class_handle_unhash(&mfd->mfd_handle);
4332                 list_del_init(&mfd->mfd_list);
4333                 spin_unlock(&med->med_open_lock);
4334                 mdt_mfd_close(info, mfd);
4335                 /* TODO: if we close the unlinked file,
4336                  * we need to remove it's objects from OST */
4337                 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
4338                 spin_lock(&med->med_open_lock);
4339                 ma->ma_lmm_size = lmm_size;
4340                 ma->ma_cookie_size = cookie_size;
4341                 ma->ma_need = MA_LOV | MA_COOKIE;
4342                 ma->ma_valid = 0;
4343         }
4344         spin_unlock(&med->med_open_lock);
4345         info->mti_mdt = NULL;
4346         mdt_client_del(&env, mdt);
4347
4348         EXIT;
4349 out:
4350         if (lmm_size) {
4351                 OBD_FREE(ma->ma_lmm, lmm_size);
4352                 ma->ma_lmm = NULL;
4353         }
4354         if (cookie_size) {
4355                 OBD_FREE(ma->ma_cookie, cookie_size);
4356                 ma->ma_cookie = NULL;
4357         }
4358         lu_env_fini(&env);
4359
4360         return rc;
4361 }
4362
4363 static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
4364 {
4365         if (flag & CONFIG_LOG)
4366                 m->mdt_fl_cfglog = 1;
4367         if (flag & CONFIG_SYNC)
4368                 m->mdt_fl_synced = 1;
4369
4370         if (m->mdt_fl_cfglog /* bz11778: && m->mdt_fl_synced */)
4371                 /* Open for clients */
4372                 m->mdt_md_dev.md_lu_dev.ld_obd->obd_no_conn = 0;
4373 }
4374
4375 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
4376                       enum md_upcall_event ev)
4377 {
4378         struct mdt_device *m = mdt_dev(&md->md_lu_dev);
4379         struct md_device  *next  = m->mdt_child;
4380         struct mdt_thread_info *mti;
4381         int rc = 0;
4382         ENTRY;
4383
4384         switch (ev) {
4385                 case MD_LOV_SYNC:
4386                         rc = next->md_ops->mdo_maxsize_get(env, next,
4387                                         &m->mdt_max_mdsize,
4388                                         &m->mdt_max_cookiesize);
4389                         CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
4390                                      m->mdt_max_mdsize, m->mdt_max_cookiesize);
4391                         mdt_allow_cli(m, CONFIG_SYNC);
4392                         break;
4393                 case MD_NO_TRANS:
4394                         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4395                         mti->mti_no_need_trans = 1;
4396                         CDEBUG(D_INFO, "disable mdt trans for this thread\n");
4397                         break;
4398                 case MD_LOV_CONFIG:
4399                         /* Check that MDT is not yet configured */
4400                         LASSERT(!m->mdt_fl_cfglog);
4401                         break;
4402                 default:
4403                         CERROR("invalid event\n");
4404                         rc = -EINVAL;
4405                         break;
4406         }
4407         RETURN(rc);
4408 }
4409
4410 static int mdt_obd_notify(struct obd_device *host,
4411                           struct obd_device *watched,
4412                           enum obd_notify_event ev, void *data)
4413 {
4414         ENTRY;
4415
4416         switch (ev) {
4417         case OBD_NOTIFY_CONFIG:
4418                 mdt_allow_cli(mdt_dev(host->obd_lu_dev), (unsigned int)data);
4419                 break;
4420         default:
4421                 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
4422         }
4423         RETURN(0);
4424 }
4425
4426 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
4427                          void *karg, void *uarg)
4428 {
4429         struct lu_env      env;
4430         struct obd_device *obd= exp->exp_obd;
4431         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
4432         struct dt_device  *dt = mdt->mdt_bottom;
4433         int rc;
4434
4435         ENTRY;
4436         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
4437         rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4438         if (rc)
4439                 RETURN(rc);
4440
4441         switch (cmd) {
4442         case OBD_IOC_SYNC:
4443                 rc = dt->dd_ops->dt_sync(&env, dt);
4444                 break;
4445         case OBD_IOC_SET_READONLY:
4446                 rc = dt->dd_ops->dt_sync(&env, dt);
4447                 dt->dd_ops->dt_ro(&env, dt);
4448                 break;
4449         case OBD_IOC_ABORT_RECOVERY:
4450                 CERROR("Aborting recovery for device %s\n", obd->obd_name);
4451                 target_stop_recovery_thread(obd);
4452                 rc = 0;
4453                 break;
4454         default:
4455                 CERROR("Not supported cmd = %d for device %s\n",
4456                        cmd, obd->obd_name);
4457                 rc = -EOPNOTSUPP;
4458         }
4459
4460         lu_env_fini(&env);
4461         RETURN(rc);
4462 }
4463
4464 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
4465 {
4466         struct lu_device *ld = md2lu_dev(mdt->mdt_child);
4467         struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
4468         int rc, lost;
4469         ENTRY;
4470         /* if some clients didn't participate in recovery then we can possibly 
4471          * lost sequence. Now we should increase sequence for safe value */
4472         lost = obd->obd_max_recoverable_clients - obd->obd_connected_clients;
4473         mdt_seq_adjust(env, mdt, lost);
4474         
4475         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
4476         RETURN(rc);
4477 }
4478
4479 int mdt_obd_postrecov(struct obd_device *obd)
4480 {
4481         struct lu_env env;
4482         int rc;
4483
4484         rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4485         if (rc)
4486                 RETURN(rc);
4487         rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
4488         lu_env_fini(&env);
4489         return rc;
4490 }
4491
4492 static struct obd_ops mdt_obd_device_ops = {
4493         .o_owner          = THIS_MODULE,
4494         .o_connect        = mdt_obd_connect,
4495         .o_reconnect      = mdt_obd_reconnect,
4496         .o_disconnect     = mdt_obd_disconnect,
4497         .o_init_export    = mdt_init_export,
4498         .o_destroy_export = mdt_destroy_export,
4499         .o_iocontrol      = mdt_iocontrol,
4500         .o_postrecov      = mdt_obd_postrecov,
4501         .o_notify         = mdt_obd_notify
4502 };
4503
4504 static struct lu_device* mdt_device_fini(const struct lu_env *env,
4505                                          struct lu_device *d)
4506 {
4507         struct mdt_device *m = mdt_dev(d);
4508         ENTRY;
4509
4510         mdt_fini(env, m);
4511         RETURN(NULL);
4512 }
4513
4514 static void mdt_device_free(const struct lu_env *env, struct lu_device *d)
4515 {
4516         struct mdt_device *m = mdt_dev(d);
4517
4518         OBD_FREE_PTR(m);
4519 }
4520
4521 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
4522                                           struct lu_device_type *t,
4523                                           struct lustre_cfg *cfg)
4524 {
4525         struct lu_device  *l;
4526         struct mdt_device *m;
4527
4528         OBD_ALLOC_PTR(m);
4529         if (m != NULL) {
4530                 int rc;
4531
4532                 l = &m->mdt_md_dev.md_lu_dev;
4533                 rc = mdt_init0(env, m, t, cfg);
4534                 if (rc != 0) {
4535                         OBD_FREE_PTR(m);
4536                         l = ERR_PTR(rc);
4537                         return l;
4538                 }
4539                 md_upcall_init(&m->mdt_md_dev, mdt_upcall);
4540         } else
4541                 l = ERR_PTR(-ENOMEM);
4542         return l;
4543 }
4544
4545 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
4546 LU_KEY_INIT_FINI(mdt, struct mdt_thread_info);
4547
4548 /* context key: mdt_thread_key */
4549 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
4550
4551 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
4552 LU_KEY_INIT_FINI(mdt_txn, struct mdt_txn_info);
4553
4554 struct lu_context_key mdt_txn_key = {
4555         .lct_tags = LCT_TX_HANDLE,
4556         .lct_init = mdt_txn_key_init,
4557         .lct_fini = mdt_txn_key_fini
4558 };
4559
4560 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
4561 {
4562         return md_ucred(info->mti_env);
4563 }
4564
4565 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
4566 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key, &mdt_txn_key);
4567
4568 static struct lu_device_type_operations mdt_device_type_ops = {
4569         .ldto_init = mdt_type_init,
4570         .ldto_fini = mdt_type_fini,
4571
4572         .ldto_device_alloc = mdt_device_alloc,
4573         .ldto_device_free  = mdt_device_free,
4574         .ldto_device_fini  = mdt_device_fini
4575 };
4576
4577 static struct lu_device_type mdt_device_type = {
4578         .ldt_tags     = LU_DEVICE_MD,
4579         .ldt_name     = LUSTRE_MDT_NAME,
4580         .ldt_ops      = &mdt_device_type_ops,
4581         .ldt_ctx_tags = LCT_MD_THREAD
4582 };
4583
4584 static int __init mdt_mod_init(void)
4585 {
4586         struct lprocfs_static_vars lvars;
4587         int rc;
4588
4589         mdt_num_threads = MDT_NUM_THREADS;
4590         lprocfs_init_vars(mdt, &lvars);
4591         rc = class_register_type(&mdt_obd_device_ops, NULL,
4592                                  lvars.module_vars, LUSTRE_MDT_NAME,
4593                                  &mdt_device_type);
4594
4595         return rc;
4596 }
4597
4598 static void __exit mdt_mod_exit(void)
4599 {
4600         class_unregister_type(LUSTRE_MDT_NAME);
4601 }
4602
4603
4604 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt)             \
4605 [prefix ## _ ## opc - prefix ## _ ## base] = {                          \
4606         .mh_name    = #opc,                                             \
4607         .mh_fail_id = OBD_FAIL_ ## prefix ## _  ## opc ## suffix,       \
4608         .mh_opc     = prefix ## _  ## opc,                              \
4609         .mh_flags   = flags,                                            \
4610         .mh_act     = fn,                                               \
4611         .mh_fmt     = fmt                                               \
4612 }
4613
4614 #define DEF_MDT_HNDL(flags, name, fn, fmt)                                  \
4615         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
4616
4617 #define DEF_SEQ_HNDL(flags, name, fn, fmt)                      \
4618         DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
4619
4620 #define DEF_FLD_HNDL(flags, name, fn, fmt)                      \
4621         DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
4622 /*
4623  * Request with a format known in advance
4624  */
4625 #define DEF_MDT_HNDL_F(flags, name, fn)                                 \
4626         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
4627
4628 #define DEF_SEQ_HNDL_F(flags, name, fn)                                 \
4629         DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
4630
4631 #define DEF_FLD_HNDL_F(flags, name, fn)                                 \
4632         DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
4633 /*
4634  * Request with a format we do not yet know
4635  */
4636 #define DEF_MDT_HNDL_0(flags, name, fn)                                 \
4637         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
4638
4639 static struct mdt_handler mdt_mds_ops[] = {
4640 DEF_MDT_HNDL_F(0,                         CONNECT,      mdt_connect),
4641 DEF_MDT_HNDL_F(0,                         DISCONNECT,   mdt_disconnect),
4642 DEF_MDT_HNDL_F(0,                         SET_INFO,     mdt_set_info),
4643 DEF_MDT_HNDL_F(0           |HABEO_REFERO, GETSTATUS,    mdt_getstatus),
4644 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETATTR,      mdt_getattr),
4645 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
4646 DEF_MDT_HNDL_F(HABEO_CORPUS|MUTABOR,      SETXATTR,     mdt_setxattr),
4647 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETXATTR,     mdt_getxattr),
4648 DEF_MDT_HNDL_F(0           |HABEO_REFERO, STATFS,       mdt_statfs),
4649 DEF_MDT_HNDL_F(0           |MUTABOR,      REINT,        mdt_reint),
4650 DEF_MDT_HNDL_F(HABEO_CORPUS,              CLOSE,        mdt_close),
4651 DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING, mdt_done_writing),
4652 DEF_MDT_HNDL_F(0           |HABEO_REFERO, PIN,          mdt_pin),
4653 DEF_MDT_HNDL_0(0,                         SYNC,         mdt_sync),
4654 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
4655 DEF_MDT_HNDL_0(0,                         QUOTACHECK,   mdt_quotacheck_handle),
4656 DEF_MDT_HNDL_0(0,                         QUOTACTL,     mdt_quotactl_handle)
4657 };
4658
4659 #define DEF_OBD_HNDL(flags, name, fn)                   \
4660         DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
4661
4662
4663 static struct mdt_handler mdt_obd_ops[] = {
4664         DEF_OBD_HNDL(0, PING,           mdt_obd_ping),
4665         DEF_OBD_HNDL(0, LOG_CANCEL,     mdt_obd_log_cancel),
4666         DEF_OBD_HNDL(0, QC_CALLBACK,    mdt_obd_qc_callback)
4667 };
4668
4669 #define DEF_DLM_HNDL_0(flags, name, fn)                   \
4670         DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
4671 #define DEF_DLM_HNDL_F(flags, name, fn)                   \
4672         DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
4673
4674 static struct mdt_handler mdt_dlm_ops[] = {
4675         DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE,        mdt_enqueue),
4676         DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT,        mdt_convert),
4677         DEF_DLM_HNDL_0(0,            BL_CALLBACK,    mdt_bl_callback),
4678         DEF_DLM_HNDL_0(0,            CP_CALLBACK,    mdt_cp_callback)
4679 };
4680
4681 static struct mdt_handler mdt_llog_ops[] = {
4682 };
4683
4684 #define DEF_SEC_CTX_HNDL(name, fn)                      \
4685         DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
4686
4687 static struct mdt_handler mdt_sec_ctx_ops[] = {
4688         DEF_SEC_CTX_HNDL(INIT,          mdt_sec_ctx_handle),
4689         DEF_SEC_CTX_HNDL(INIT_CONT,     mdt_sec_ctx_handle),
4690         DEF_SEC_CTX_HNDL(FINI,          mdt_sec_ctx_handle)
4691 };
4692
4693 static struct mdt_opc_slice mdt_regular_handlers[] = {
4694         {
4695                 .mos_opc_start = MDS_GETATTR,
4696                 .mos_opc_end   = MDS_LAST_OPC,
4697                 .mos_hs        = mdt_mds_ops
4698         },
4699         {
4700                 .mos_opc_start = OBD_PING,
4701                 .mos_opc_end   = OBD_LAST_OPC,
4702                 .mos_hs        = mdt_obd_ops
4703         },
4704         {
4705                 .mos_opc_start = LDLM_ENQUEUE,
4706                 .mos_opc_end   = LDLM_LAST_OPC,
4707                 .mos_hs        = mdt_dlm_ops
4708         },
4709         {
4710                 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
4711                 .mos_opc_end   = LLOG_LAST_OPC,
4712                 .mos_hs        = mdt_llog_ops
4713         },
4714         {
4715                 .mos_opc_start = SEC_CTX_INIT,
4716                 .mos_opc_end   = SEC_LAST_OPC,
4717                 .mos_hs        = mdt_sec_ctx_ops
4718         },
4719         {
4720                 .mos_hs        = NULL
4721         }
4722 };
4723
4724 static struct mdt_handler mdt_readpage_ops[] = {
4725         DEF_MDT_HNDL_F(0,                         CONNECT,  mdt_connect),
4726         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
4727 #ifdef HAVE_SPLIT_SUPPORT
4728         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
4729 #endif
4730
4731         /*
4732          * XXX: this is ugly and should be fixed one day, see mdc_close() for
4733          * detailed comments. --umka
4734          */
4735         DEF_MDT_HNDL_F(HABEO_CORPUS,              CLOSE,    mdt_close),
4736         DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING,    mdt_done_writing),
4737 };
4738
4739 static struct mdt_opc_slice mdt_readpage_handlers[] = {
4740         {
4741                 .mos_opc_start = MDS_GETATTR,
4742                 .mos_opc_end   = MDS_LAST_OPC,
4743                 .mos_hs        = mdt_readpage_ops
4744         },
4745         {
4746                 .mos_hs        = NULL
4747         }
4748 };
4749
4750 static struct mdt_handler mdt_xmds_ops[] = {
4751         DEF_MDT_HNDL_F(0,                         CONNECT,      mdt_connect),
4752         DEF_MDT_HNDL_F(HABEO_CORPUS             , GETATTR,      mdt_getattr),
4753         DEF_MDT_HNDL_F(0 | MUTABOR              , REINT,        mdt_reint),
4754         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
4755 };
4756
4757 static struct mdt_opc_slice mdt_xmds_handlers[] = {
4758         {
4759                 .mos_opc_start = MDS_GETATTR,
4760                 .mos_opc_end   = MDS_LAST_OPC,
4761                 .mos_hs        = mdt_xmds_ops
4762         },
4763         {
4764                 .mos_opc_start = OBD_PING,
4765                 .mos_opc_end   = OBD_LAST_OPC,
4766                 .mos_hs        = mdt_obd_ops
4767         },
4768         {
4769                 .mos_hs        = NULL
4770         }
4771 };
4772
4773 static struct mdt_handler mdt_seq_ops[] = {
4774         DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
4775 };
4776
4777 static struct mdt_opc_slice mdt_seq_handlers[] = {
4778         {
4779                 .mos_opc_start = SEQ_QUERY,
4780                 .mos_opc_end   = SEQ_LAST_OPC,
4781                 .mos_hs        = mdt_seq_ops
4782         },
4783         {
4784                 .mos_hs        = NULL
4785         }
4786 };
4787
4788 static struct mdt_handler mdt_fld_ops[] = {
4789         DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
4790 };
4791
4792 static struct mdt_opc_slice mdt_fld_handlers[] = {
4793         {
4794                 .mos_opc_start = FLD_QUERY,
4795                 .mos_opc_end   = FLD_LAST_OPC,
4796                 .mos_hs        = mdt_fld_ops
4797         },
4798         {
4799                 .mos_hs        = NULL
4800         }
4801 };
4802
4803 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
4804 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
4805 MODULE_LICENSE("GPL");
4806
4807 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
4808                 "number of mdt service threads to start");
4809
4810 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);