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