Whamcloud - gitweb
land b_colibri_devel on HEAD:
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/mdt/mdt_handler.c
5  *  Lustre Metadata Target (mdt) request handler
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *   Author: Mike Shaver <shaver@clusterfs.com>
12  *   Author: Nikita Danilov <nikita@clusterfs.com>
13  *   Author: Huang Hua <huanghua@clusterfs.com>
14  *   Author: Yury Umanets <umka@clusterfs.com>
15  *
16  *   This file is part of the Lustre file system, http://www.lustre.org
17  *   Lustre is a trademark of Cluster File Systems, Inc.
18  *
19  *   You may have signed or agreed to another license before downloading
20  *   this software.  If so, you are bound by the terms and conditions
21  *   of that agreement, and the following does not apply to you.  See the
22  *   LICENSE file included with this distribution for more information.
23  *
24  *   If you did not agree to a different license, then this copy of Lustre
25  *   is open source software; you can redistribute it and/or modify it
26  *   under the terms of version 2 of the GNU General Public License as
27  *   published by the Free Software Foundation.
28  *
29  *   In either case, Lustre is distributed in the hope that it will be
30  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
31  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  *   license text for more details.
33  */
34
35 #ifndef EXPORT_SYMTAB
36 # define EXPORT_SYMTAB
37 #endif
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <linux/module.h>
41 /*
42  * struct OBD_{ALLOC,FREE}*()
43  */
44 #include <obd_support.h>
45 /* struct ptlrpc_request */
46 #include <lustre_net.h>
47 /* struct obd_export */
48 #include <lustre_export.h>
49 /* struct obd_device */
50 #include <obd.h>
51 /* lu2dt_dev() */
52 #include <dt_object.h>
53 #include <lustre_mds.h>
54 #include <lustre_mdt.h>
55 #include "mdt_internal.h"
56 #include <linux/lustre_acl.h>
57 #include <lustre_param.h>
58
59 mdl_mode_t mdt_mdl_lock_modes[] = {
60         [LCK_MINMODE] = MDL_MINMODE,
61         [LCK_EX]      = MDL_EX,
62         [LCK_PW]      = MDL_PW,
63         [LCK_PR]      = MDL_PR,
64         [LCK_CW]      = MDL_CW,
65         [LCK_CR]      = MDL_CR,
66         [LCK_NL]      = MDL_NL,
67         [LCK_GROUP]   = MDL_GROUP
68 };
69
70 ldlm_mode_t mdt_dlm_lock_modes[] = {
71         [MDL_MINMODE] = LCK_MINMODE,
72         [MDL_EX]      = LCK_EX,
73         [MDL_PW]      = LCK_PW,
74         [MDL_PR]      = LCK_PR,
75         [MDL_CW]      = LCK_CW,
76         [MDL_CR]      = LCK_CR,
77         [MDL_NL]      = LCK_NL,
78         [MDL_GROUP]   = LCK_GROUP
79 };
80
81 /*
82  * Initialized in mdt_mod_init().
83  */
84 unsigned long mdt_num_threads;
85
86 /* ptlrpc request handler for MDT. All handlers are
87  * grouped into several slices - struct mdt_opc_slice,
88  * and stored in an array - mdt_handlers[].
89  */
90 struct mdt_handler {
91         /* The name of this handler. */
92         const char *mh_name;
93         /* Fail id for this handler, checked at the beginning of this handler*/
94         int         mh_fail_id;
95         /* Operation code for this handler */
96         __u32       mh_opc;
97         /* flags are listed in enum mdt_handler_flags below. */
98         __u32       mh_flags;
99         /* The actual handler function to execute. */
100         int (*mh_act)(struct mdt_thread_info *info);
101         /* Request format for this request. */
102         const struct req_format *mh_fmt;
103 };
104
105 enum mdt_handler_flags {
106         /*
107          * struct mdt_body is passed in the incoming message, and object
108          * identified by this fid exists on disk.
109          *
110          * "habeo corpus" == "I have a body"
111          */
112         HABEO_CORPUS = (1 << 0),
113         /*
114          * struct ldlm_request is passed in the incoming message.
115          *
116          * "habeo clavis" == "I have a key"
117          */
118         HABEO_CLAVIS = (1 << 1),
119         /*
120          * this request has fixed reply format, so that reply message can be
121          * packed by generic code.
122          *
123          * "habeo refero" == "I have a reply"
124          */
125         HABEO_REFERO = (1 << 2),
126         /*
127          * this request will modify something, so check whether the filesystem
128          * is readonly or not, then return -EROFS to client asap if necessary.
129          *
130          * "mutabor" == "I shall modify"
131          */
132         MUTABOR      = (1 << 3)
133 };
134
135 struct mdt_opc_slice {
136         __u32               mos_opc_start;
137         int                 mos_opc_end;
138         struct mdt_handler *mos_hs;
139 };
140
141 static struct mdt_opc_slice mdt_regular_handlers[];
142 static struct mdt_opc_slice mdt_readpage_handlers[];
143 static struct mdt_opc_slice mdt_xmds_handlers[];
144 static struct mdt_opc_slice mdt_seq_handlers[];
145 static struct mdt_opc_slice mdt_fld_handlers[];
146
147 static struct mdt_device *mdt_dev(struct lu_device *d);
148 static int mdt_regular_handle(struct ptlrpc_request *req);
149 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
150
151 static struct lu_object_operations mdt_obj_ops;
152
153 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
154 {
155         if (!rep)
156                 return 0;
157         return (rep->lock_policy_res1 & flag);
158 }
159
160 void mdt_clear_disposition(struct mdt_thread_info *info,
161                            struct ldlm_reply *rep, int flag)
162 {
163         if (info)
164                 info->mti_opdata &= ~flag;
165         if (rep)
166                 rep->lock_policy_res1 &= ~flag;
167 }
168
169 void mdt_set_disposition(struct mdt_thread_info *info,
170                          struct ldlm_reply *rep, int flag)
171 {
172         if (info)
173                 info->mti_opdata |= flag;
174         if (rep)
175                 rep->lock_policy_res1 |= flag;
176 }
177
178 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
179 {
180         lh->mlh_pdo_hash = 0;
181         lh->mlh_reg_mode = lm;
182         lh->mlh_type = MDT_REG_LOCK;
183 }
184
185 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
186                        const char *name, int namelen)
187 {
188         lh->mlh_reg_mode = lm;
189         lh->mlh_type = MDT_PDO_LOCK;
190
191         if (name != NULL) {
192                 LASSERT(namelen > 0);
193                 lh->mlh_pdo_hash = full_name_hash(name, namelen);
194         } else {
195                 LASSERT(namelen == 0);
196                 lh->mlh_pdo_hash = 0ull;
197         }
198 }
199
200 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
201                               struct mdt_lock_handle *lh)
202 {
203         mdl_mode_t mode;
204         ENTRY;
205
206         /*
207          * Any dir access needs couple of locks:
208          *
209          * 1) on part of dir we gonna take lookup/modify;
210          *
211          * 2) on whole dir to protect it from concurrent splitting and/or to
212          * flush client's cache for readdir().
213          *
214          * so, for a given mode and object this routine decides what lock mode
215          * to use for lock #2:
216          *
217          * 1) if caller's gonna lookup in dir then we need to protect dir from
218          * being splitted only - LCK_CR
219          *
220          * 2) if caller's gonna modify dir then we need to protect dir from
221          * being splitted and to flush cache - LCK_CW
222          *
223          * 3) if caller's gonna modify dir and that dir seems ready for
224          * splitting then we need to protect it from any type of access
225          * (lookup/modify/split) - LCK_EX --bzzz
226          */
227
228         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
229         LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
230
231         /*
232          * Ask underlaying level its opinion about preferable PDO lock mode
233          * having access type passed as regular lock mode:
234          *
235          * - MDL_MINMODE means that lower layer does not want to specify lock
236          * mode;
237          *
238          * - MDL_NL means that no PDO lock should be taken. This is used in some
239          * cases. Say, for non-splittable directories no need to use PDO locks
240          * at all.
241          */
242         mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
243                              mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
244
245         if (mode != MDL_MINMODE) {
246                 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
247         } else {
248                 /*
249                  * Lower layer does not want to specify locking mode. We do it
250                  * our selves. No special protection is needed, just flush
251                  * client's cache on modification and allow concurrent
252                  * mondification.
253                  */
254                 switch (lh->mlh_reg_mode) {
255                 case LCK_EX:
256                         lh->mlh_pdo_mode = LCK_EX;
257                         break;
258                 case LCK_PR:
259                         lh->mlh_pdo_mode = LCK_CR;
260                         break;
261                 case LCK_PW:
262                         lh->mlh_pdo_mode = LCK_CW;
263                         break;
264                 default:
265                         CERROR("Not expected lock type (0x%x)\n",
266                                (int)lh->mlh_reg_mode);
267                         LBUG();
268                 }
269         }
270
271         LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
272         EXIT;
273 }
274
275 static int mdt_getstatus(struct mdt_thread_info *info)
276 {
277         struct mdt_device *mdt  = info->mti_mdt;
278         struct md_device  *next = mdt->mdt_child;
279         struct mdt_body   *repbody;
280         int                rc;
281
282         ENTRY;
283
284         rc = mdt_check_ucred(info);
285         if (rc)
286                 RETURN(err_serious(rc));
287
288         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
289                 RETURN(err_serious(-ENOMEM));
290
291         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
292         rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
293         if (rc != 0)
294                 RETURN(rc);
295
296         repbody->valid |= OBD_MD_FLID;
297
298         if (mdt->mdt_opts.mo_mds_capa) {
299                 struct mdt_object  *root;
300                 struct lustre_capa *capa;
301
302                 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
303                 if (IS_ERR(root))
304                         RETURN(PTR_ERR(root));
305
306                 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
307                 LASSERT(capa);
308                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
309
310                 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
311                                  0);
312                 mdt_object_put(info->mti_env, root);
313                 if (rc == 0)
314                         repbody->valid |= OBD_MD_FLMDSCAPA;
315         }
316
317         RETURN(rc);
318 }
319
320 static int mdt_statfs(struct mdt_thread_info *info)
321 {
322         struct md_device  *next  = info->mti_mdt->mdt_child;
323         struct obd_statfs *osfs;
324         int                rc;
325
326         ENTRY;
327
328         /* This will trigger a watchdog timeout */
329         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
330                          (MDT_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
331
332         rc = mdt_check_ucred(info);
333         if (rc)
334                 RETURN(err_serious(rc));
335
336         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
337                 rc = err_serious(-ENOMEM);
338         } else {
339                 osfs = req_capsule_server_get(&info->mti_pill,&RMF_OBD_STATFS);
340                 rc = next->md_ops->mdo_statfs(info->mti_env, next,
341                                               &info->mti_u.ksfs);
342                 statfs_pack(osfs, &info->mti_u.ksfs);
343         }
344         RETURN(rc);
345 }
346
347 void mdt_pack_size2body(struct mdt_thread_info *info, struct mdt_object *o)
348 {
349         struct mdt_body *b;
350         struct lu_attr *attr = &info->mti_attr.ma_attr;
351         
352         b = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
353
354         /* Check if Size-on-MDS is enabled. */
355         if ((mdt_conn_flags(info) & OBD_CONNECT_SOM) &&
356             S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
357                 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
358                 b->size = attr->la_size;
359                 b->blocks = attr->la_blocks;
360         }
361 }
362
363 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
364                         const struct lu_attr *attr, const struct lu_fid *fid)
365 {
366         /*XXX should pack the reply body according to lu_valid*/
367         b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID   |
368                     OBD_MD_FLGID   | OBD_MD_FLTYPE  |
369                     OBD_MD_FLMODE  | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
370                     OBD_MD_FLATIME | OBD_MD_FLMTIME ;
371
372         if (!S_ISREG(attr->la_mode))
373                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
374
375         b->atime      = attr->la_atime;
376         b->mtime      = attr->la_mtime;
377         b->ctime      = attr->la_ctime;
378         b->mode       = attr->la_mode;
379         b->size       = attr->la_size;
380         b->blocks     = attr->la_blocks;
381         b->uid        = attr->la_uid;
382         b->gid        = attr->la_gid;
383         b->flags      = attr->la_flags;
384         b->nlink      = attr->la_nlink;
385         b->rdev       = attr->la_rdev;
386
387         if (fid) {
388                 b->fid1 = *fid;
389                 b->valid |= OBD_MD_FLID;
390                 CDEBUG(D_INODE, ""DFID": nlink=%d, mode=%o, size="LPU64"\n",
391                                 PFID(fid), b->nlink, b->mode, b->size);
392         }
393
394         if (info)
395                 mdt_body_reverse_idmap(info, b);
396 }
397
398 static inline int mdt_body_has_lov(const struct lu_attr *la,
399                                    const struct mdt_body *body)
400 {
401         return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
402                 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
403 }
404
405 static int mdt_getattr_internal(struct mdt_thread_info *info,
406                                 struct mdt_object *o)
407 {
408         struct md_object        *next = mdt_object_child(o);
409         const struct mdt_body   *reqbody = info->mti_body;
410         struct ptlrpc_request   *req = mdt_info_req(info);
411         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
412         struct md_attr          *ma = &info->mti_attr;
413         struct lu_attr          *la = &ma->ma_attr;
414         struct req_capsule      *pill = &info->mti_pill;
415         const struct lu_env     *env = info->mti_env;
416         struct mdt_body         *repbody;
417         struct lu_buf           *buffer = &info->mti_buf;
418         int                     rc;
419         ENTRY;
420
421         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
422                 RETURN(err_serious(-ENOMEM));
423
424         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
425
426         ma->ma_valid = 0;
427
428         rc = mdt_object_exists(o);
429         if (rc < 0) {
430                 /* This object is located on remote node.*/
431                 repbody->fid1 = *mdt_object_fid(o);
432                 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
433                 RETURN(0);
434         }
435
436         buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
437         buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
438
439         /* If it is dir object and client require MEA, then we got MEA */
440         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
441             reqbody->valid & OBD_MD_MEA) {
442                 /* Assumption: MDT_MD size is enough for lmv size. */
443                 ma->ma_lmv = buffer->lb_buf;
444                 ma->ma_lmv_size = buffer->lb_len;
445                 ma->ma_need = MA_LMV | MA_INODE;
446         } else {
447                 ma->ma_lmm = buffer->lb_buf;
448                 ma->ma_lmm_size = buffer->lb_len;
449                 ma->ma_need = MA_LOV | MA_INODE;
450         }
451
452         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
453             reqbody->valid & OBD_MD_FLDIREA  &&
454             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
455                 /* get default stripe info for this dir. */
456                 ma->ma_need |= MA_LOV_DEF;
457         }
458         rc = mo_attr_get(env, next, ma);
459         if (unlikely(rc)) {
460                 CERROR("getattr error for "DFID": %d\n",
461                         PFID(mdt_object_fid(o)), rc);
462                 RETURN(rc);
463         }
464
465         if (likely(ma->ma_valid & MA_INODE))
466                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
467         else
468                 RETURN(-EFAULT);
469
470         if (mdt_body_has_lov(la, reqbody)) {
471                 if (ma->ma_valid & MA_LOV) {
472                         LASSERT(ma->ma_lmm_size);
473                         mdt_dump_lmm(D_INFO, ma->ma_lmm);
474                         repbody->eadatasize = ma->ma_lmm_size;
475                         if (S_ISDIR(la->la_mode))
476                                 repbody->valid |= OBD_MD_FLDIREA;
477                         else
478                                 repbody->valid |= OBD_MD_FLEASIZE;
479                 }
480                 if (ma->ma_valid & MA_LMV) {
481                         LASSERT(S_ISDIR(la->la_mode));
482                         repbody->eadatasize = ma->ma_lmv_size;
483                         repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
484                 }
485                 if (!(ma->ma_valid & MA_LOV) && !(ma->ma_valid & MA_LMV)) {
486                         repbody->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
487                 }
488         } else if (S_ISLNK(la->la_mode) &&
489                    reqbody->valid & OBD_MD_LINKNAME) {
490                 buffer->lb_buf = ma->ma_lmm;
491                 buffer->lb_len = reqbody->eadatasize;
492                 rc = mo_readlink(env, next, buffer);
493                 if (unlikely(rc <= 0)) {
494                         CERROR("readlink failed: %d\n", rc);
495                         rc = -EFAULT;
496                 } else {
497                         repbody->valid |= OBD_MD_LINKNAME;
498                         repbody->eadatasize = rc;
499                         /* NULL terminate */
500                         ((char*)ma->ma_lmm)[rc - 1] = 0;
501                         CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
502                                (char*)ma->ma_lmm, rc);
503                         rc = 0;
504                 }
505         }
506
507         if (reqbody->valid & OBD_MD_FLMODEASIZE) {
508                 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
509                 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
510                 repbody->valid |= OBD_MD_FLMODEASIZE;
511                 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
512                        "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
513                        repbody->max_cookiesize);
514         }
515
516         if (med->med_rmtclient && (reqbody->valid & OBD_MD_FLRMTPERM)) {
517                 void *buf = req_capsule_server_get(pill, &RMF_ACL);
518
519                 /* mdt_getattr_lock only */
520                 rc = mdt_pack_remote_perm(info, o, buf);
521                 if (rc) {
522                         repbody->valid &= ~OBD_MD_FLRMTPERM;
523                         repbody->aclsize = 0;
524                         RETURN(rc);
525                 } else {
526                         repbody->valid |= OBD_MD_FLRMTPERM;
527                         repbody->aclsize = sizeof(struct mdt_remote_perm);
528                 }
529         }
530 #ifdef CONFIG_FS_POSIX_ACL
531         else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
532                  (reqbody->valid & OBD_MD_FLACL)) {
533                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
534                 buffer->lb_len = req_capsule_get_size(pill,
535                                                       &RMF_ACL, RCL_SERVER);
536                 if (buffer->lb_len > 0) {
537                         rc = mo_xattr_get(env, next, buffer,
538                                           XATTR_NAME_ACL_ACCESS);
539                         if (rc < 0) {
540                                 if (rc == -ENODATA) {
541                                         repbody->aclsize = 0;
542                                         repbody->valid |= OBD_MD_FLACL;
543                                         rc = 0;
544                                 } else if (rc == -EOPNOTSUPP) {
545                                         rc = 0;
546                                 } else {
547                                         CERROR("got acl size: %d\n", rc);
548                                 }
549                         } else {
550                                 repbody->aclsize = rc;
551                                 repbody->valid |= OBD_MD_FLACL;
552                                 rc = 0;
553                         }
554                 }
555         }
556 #endif
557
558         if ((reqbody->valid & OBD_MD_FLMDSCAPA) &&
559             info->mti_mdt->mdt_opts.mo_mds_capa) {
560                 struct lustre_capa *capa;
561
562                 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
563                 LASSERT(capa);
564                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
565                 rc = mo_capa_get(env, next, capa, 0);
566                 if (rc)
567                         RETURN(rc);
568                 repbody->valid |= OBD_MD_FLMDSCAPA;
569         }
570         RETURN(rc);
571 }
572
573 static int mdt_renew_capa(struct mdt_thread_info *info)
574 {
575         struct mdt_device  *mdt = info->mti_mdt;
576         struct mdt_object  *obj = info->mti_object;
577         struct mdt_body    *body;
578         struct lustre_capa *capa, *c;
579         int rc;
580         ENTRY;
581
582         /* if object doesn't exist, or server has disabled capability,
583          * return directly, client will find body->valid OBD_MD_FLOSSCAPA
584          * flag not set.
585          */
586         if (!obj || !mdt->mdt_opts.mo_mds_capa)
587                 RETURN(0);
588
589         body = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
590         LASSERT(body != NULL);
591
592         c = req_capsule_client_get(&info->mti_pill, &RMF_CAPA1);
593         LASSERT(c);
594
595         capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
596         LASSERT(capa);
597
598         *capa = *c;
599         rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
600         if (rc == 0)
601                 body->valid |= OBD_MD_FLOSSCAPA;
602
603         RETURN(rc);
604 }
605
606 static int mdt_getattr(struct mdt_thread_info *info)
607 {
608         struct mdt_object       *obj = info->mti_object;
609         struct req_capsule      *pill = &info->mti_pill;
610         struct mdt_body         *reqbody;
611         struct mdt_body         *repbody;
612         mode_t                   mode;
613         int                      md_size;
614         int rc;
615         ENTRY;
616
617         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
618         LASSERT(reqbody);
619
620         if (reqbody->valid & OBD_MD_FLOSSCAPA) {
621                 rc = req_capsule_pack(pill);
622                 if (unlikely(rc))
623                         rc = err_serious(rc);
624                 else {
625                         rc = mdt_renew_capa(info);
626                         mdt_shrink_reply(info);
627                 }
628                 GOTO(out, rc);
629         }
630
631         LASSERT(obj != NULL);
632         LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
633
634         mode = lu_object_attr(&obj->mot_obj.mo_lu);
635         if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
636             (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize))
637                 md_size = reqbody->eadatasize;
638         else
639                 md_size = info->mti_mdt->mdt_max_mdsize;
640
641         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
642
643         rc = req_capsule_pack(pill);
644         if (unlikely(rc != 0))
645                 GOTO(out, rc = err_serious(rc));
646
647         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
648         LASSERT(repbody != NULL);
649         repbody->eadatasize = 0;
650         repbody->aclsize = 0;
651
652         if (reqbody->valid & OBD_MD_FLRMTPERM)
653                 rc = mdt_init_ucred(info, reqbody);
654         else
655                 rc = mdt_check_ucred(info);
656         if (unlikely(rc))
657                 GOTO(out_shrink, rc);
658
659         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
660         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
661
662         /*
663          * Don't check capability at all, because rename might getattr for
664          * remote obj, and at that time no capability is available.
665          */
666         mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
667         rc = mdt_getattr_internal(info, obj);
668         if (reqbody->valid & OBD_MD_FLRMTPERM)
669                 mdt_exit_ucred(info);
670         EXIT;
671 out_shrink:
672         mdt_shrink_reply(info);
673 out:
674         return rc;
675 }
676
677 static int mdt_is_subdir(struct mdt_thread_info *info)
678 {
679         struct mdt_object     *o = info->mti_object;
680         struct req_capsule    *pill = &info->mti_pill;
681         const struct mdt_body *body = info->mti_body;
682         struct mdt_body       *repbody;
683         int                    rc;
684         ENTRY;
685
686         LASSERT(o != NULL);
687
688         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
689
690         /*
691          * We save last checked parent fid to @repbody->fid1 for remote
692          * directory case.
693          */
694         LASSERT(fid_is_sane(&body->fid2));
695         LASSERT(mdt_object_exists(o) > 0);
696         rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
697                            &body->fid2, &repbody->fid1);
698         if (rc == 0 || rc == -EREMOTE)
699                 repbody->valid |= OBD_MD_FLID;
700
701         RETURN(rc);
702 }
703
704 static int mdt_raw_lookup(struct mdt_thread_info *info,
705                           struct mdt_object *parent,
706                           const struct lu_name *lname,
707                           struct ldlm_reply *ldlm_rep)
708 {
709         struct md_object *next = mdt_object_child(info->mti_object);
710         const struct mdt_body *reqbody = info->mti_body;
711         struct lu_fid *child_fid = &info->mti_tmp_fid1;
712         struct mdt_body *repbody;
713         int rc;
714         ENTRY;
715
716         if (reqbody->valid != OBD_MD_FLID)
717                 RETURN(0);
718
719         LASSERT(!info->mti_cross_ref);
720
721         /* Only got the fid of this obj by name */
722         rc = mdo_lookup(info->mti_env, next, lname, child_fid,
723                         &info->mti_spec);
724 #if 0
725         /* XXX is raw_lookup possible as intent operation? */
726         if (rc != 0) {
727                 if (rc == -ENOENT)
728                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
729                 RETURN(rc);
730         } else
731                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
732
733         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
734 #endif
735         if (rc == 0) {
736                 repbody = req_capsule_server_get(&info->mti_pill,
737                                                  &RMF_MDT_BODY);
738                 repbody->fid1 = *child_fid;
739                 repbody->valid = OBD_MD_FLID;
740         }
741         RETURN(1);
742 }
743
744 /*
745  * UPDATE lock should be taken against parent, and be release before exit;
746  * child_bits lock should be taken against child, and be returned back:
747  *            (1)normal request should release the child lock;
748  *            (2)intent request will grant the lock to client.
749  */
750 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
751                                  struct mdt_lock_handle *lhc,
752                                  __u64 child_bits,
753                                  struct ldlm_reply *ldlm_rep)
754 {
755         struct ptlrpc_request  *req       = mdt_info_req(info);
756         struct mdt_body        *reqbody   = NULL;
757         struct mdt_object      *parent    = info->mti_object;
758         struct mdt_object      *child;
759         struct md_object       *next      = mdt_object_child(parent);
760         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
761         struct lu_name         *lname     = NULL;
762         const char             *name;
763         int                     namelen   = 0;
764         struct mdt_lock_handle *lhp;
765         struct ldlm_lock       *lock;
766         struct ldlm_res_id     *res_id;
767         int                     is_resent;
768         int                     rc;
769
770         ENTRY;
771
772         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
773         LASSERT(ergo(is_resent,
774                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
775
776         LASSERT(parent != NULL);
777         name = req_capsule_client_get(&info->mti_pill, &RMF_NAME);
778         if (name == NULL)
779                 RETURN(err_serious(-EFAULT));
780
781         namelen = req_capsule_get_size(&info->mti_pill, &RMF_NAME, 
782                                        RCL_CLIENT) - 1;
783         LASSERT(namelen >= 0);
784
785         /* XXX: "namelen == 0" is for getattr by fid (OBD_CONNECT_ATTRFID),
786          * otherwise do not allow empty name, that is the name must contain
787          * at least one character and the terminating '\0'*/
788         if (namelen == 0) {
789                 reqbody =req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
790                 LASSERT(fid_is_sane(&reqbody->fid2));
791                 name = NULL;
792
793                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
794                        "ldlm_rep = %p\n",
795                        PFID(mdt_object_fid(parent)), PFID(&reqbody->fid2),
796                        ldlm_rep);
797         } else {
798                 lname = mdt_name(info->mti_env, (char *)name, namelen);
799                 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
800                        "ldlm_rep = %p\n",
801                        PFID(mdt_object_fid(parent)), name, ldlm_rep);
802         }
803
804         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
805
806         rc = mdt_object_exists(parent);
807         if (unlikely(rc == 0)) {
808                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
809                                 &parent->mot_obj.mo_lu,
810                                 "Parent doesn't exist!\n");
811                 RETURN(-ESTALE);
812         } else
813                 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
814                          PFID(mdt_object_fid(parent)));
815
816         if (lname) {
817                 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
818                 if (rc != 0) {
819                         if (rc > 0)
820                                 rc = 0;
821                         RETURN(rc);
822                 }
823         }
824
825         if (info->mti_cross_ref) {
826                 /* Only getattr on the child. Parent is on another node. */
827                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
828                 child = parent;
829                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
830                        "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
831
832                 if (is_resent) {
833                         /* Do not take lock for resent case. */
834                         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
835                         LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
836                                  lhc->mlh_reg_lh.cookie);
837                         LASSERT(fid_res_name_eq(mdt_object_fid(child),
838                                                 &lock->l_resource->lr_name));
839                         LDLM_LOCK_PUT(lock);
840                         rc = 0;
841                 } else {
842                         mdt_lock_handle_init(lhc);
843                         mdt_lock_reg_init(lhc, LCK_PR);
844
845                         /*
846                          * Object's name is on another MDS, no lookup lock is
847                          * needed here but update is.
848                          */
849                         child_bits &= ~MDS_INODELOCK_LOOKUP;
850                         child_bits |= MDS_INODELOCK_UPDATE;
851
852                         rc = mdt_object_lock(info, child, lhc, child_bits,
853                                              MDT_LOCAL_LOCK);
854                 }
855                 if (rc == 0) {
856                         /* Finally, we can get attr for child. */
857                         mdt_set_capainfo(info, 0, mdt_object_fid(child),
858                                          BYPASS_CAPA);
859                         rc = mdt_getattr_internal(info, child);
860                         if (unlikely(rc != 0))
861                                 mdt_object_unlock(info, child, lhc, 1);
862                 }
863                 GOTO(out, rc);
864         }
865
866         /* step 1: lock parent */
867         lhp = &info->mti_lh[MDT_LH_PARENT];
868         mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
869         rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
870                              MDT_LOCAL_LOCK);
871
872         if (unlikely(rc != 0))
873                 RETURN(rc);
874
875         if (lname) {
876                 /* step 2: lookup child's fid by name */
877                 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
878                                 &info->mti_spec);
879
880                 if (rc != 0) {
881                         if (rc == -ENOENT)
882                                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
883                         GOTO(out_parent, rc);
884                 } else
885                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
886         } else {
887                 *child_fid = reqbody->fid2;
888                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
889         }
890
891         /*
892          *step 3: find the child object by fid & lock it.
893          *        regardless if it is local or remote.
894          */
895         child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
896
897         if (unlikely(IS_ERR(child)))
898                 GOTO(out_parent, rc = PTR_ERR(child));
899         if (is_resent) {
900                 /* Do not take lock for resent case. */
901                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
902                 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
903                          lhc->mlh_reg_lh.cookie);
904
905                 res_id = &lock->l_resource->lr_name;
906                 if (!fid_res_name_eq(mdt_object_fid(child),
907                                     &lock->l_resource->lr_name)) {
908                          LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
909                                                  &lock->l_resource->lr_name),
910                                  "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
911                                  (unsigned long)res_id->name[0],
912                                  (unsigned long)res_id->name[1],
913                                  (unsigned long)res_id->name[2],
914                                  PFID(mdt_object_fid(parent)));
915                           CWARN("Although resent, but still not get child lock"
916                                 "parent:"DFID" child:"DFID"\n",
917                                 PFID(mdt_object_fid(parent)),
918                                 PFID(mdt_object_fid(child)));
919                           lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
920                           LDLM_LOCK_PUT(lock);
921                           GOTO(relock, 0);
922                 }
923                 LDLM_LOCK_PUT(lock);
924                 rc = 0;
925         } else {
926 relock:
927                 mdt_lock_handle_init(lhc);
928                 mdt_lock_reg_init(lhc, LCK_PR);
929
930                 if (mdt_object_exists(child) == 0) {
931                         LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
932                                         &child->mot_obj.mo_lu,
933                                         "Object doesn't exist!\n");
934                 }
935                 rc = mdt_object_lock(info, child, lhc, child_bits,
936                                      MDT_CROSS_LOCK);
937
938                 if (unlikely(rc != 0))
939                         GOTO(out_child, rc);
940         }
941
942         /* finally, we can get attr for child. */
943         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
944         rc = mdt_getattr_internal(info, child);
945         if (unlikely(rc != 0)) {
946                 mdt_object_unlock(info, child, lhc, 1);
947         } else {
948                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
949                 if (lock) {
950                         struct mdt_body *repbody;
951                         struct lu_attr *ma;
952
953                         /* Debugging code. */
954                         res_id = &lock->l_resource->lr_name;
955                         LDLM_DEBUG(lock, "Returning lock to client\n");
956                         LASSERTF(fid_res_name_eq(mdt_object_fid(child),
957                                                  &lock->l_resource->lr_name),
958                                  "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
959                                  (unsigned long)res_id->name[0],
960                                  (unsigned long)res_id->name[1],
961                                  (unsigned long)res_id->name[2],
962                                  PFID(mdt_object_fid(child)));
963                         /*
964                          * Pack Size-on-MDS inode attributes to the body if
965                          * update lock is given.
966                          */
967                         repbody = req_capsule_server_get(&info->mti_pill,
968                                                          &RMF_MDT_BODY);
969                         ma = &info->mti_attr.ma_attr;
970                         if (lock->l_policy_data.l_inodebits.bits &
971                             MDS_INODELOCK_UPDATE)
972                                 mdt_pack_size2body(info, child);
973                         LDLM_LOCK_PUT(lock);
974                 }
975         }
976         EXIT;
977 out_child:
978         mdt_object_put(info->mti_env, child);
979 out_parent:
980         mdt_object_unlock(info, parent, lhp, 1);
981 out:
982         return rc;
983 }
984
985 /* normal handler: should release the child lock */
986 static int mdt_getattr_name(struct mdt_thread_info *info)
987 {
988         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
989         struct mdt_body        *reqbody;
990         struct mdt_body        *repbody;
991         int rc;
992         ENTRY;
993
994         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
995         LASSERT(reqbody != NULL);
996         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
997         LASSERT(repbody != NULL);
998
999         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1000         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1001         repbody->eadatasize = 0;
1002         repbody->aclsize = 0;
1003
1004         rc = mdt_init_ucred(info, reqbody);
1005         if (unlikely(rc))
1006                 GOTO(out, rc);
1007
1008         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1009         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1010                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1011                 lhc->mlh_reg_lh.cookie = 0;
1012         }
1013         mdt_exit_ucred(info);
1014         EXIT;
1015 out:
1016         mdt_shrink_reply(info);
1017         return rc;
1018 }
1019
1020 static struct lu_device_operations mdt_lu_ops;
1021
1022 static int lu_device_is_mdt(struct lu_device *d)
1023 {
1024         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1025 }
1026
1027 static int mdt_set_info(struct mdt_thread_info *info)
1028 {
1029         struct ptlrpc_request *req = mdt_info_req(info);
1030         char *key;
1031         __u32 *val;
1032         int keylen, rc = 0;
1033         ENTRY;
1034
1035         rc = lustre_pack_reply(req, 1, NULL, NULL);
1036         if (rc)
1037                 RETURN(rc);
1038
1039         key = req_capsule_client_get(&info->mti_pill, &RMF_SETINFO_KEY);
1040         if (key == NULL) {
1041                 DEBUG_REQ(D_HA, req, "no set_info key");
1042                 RETURN(-EFAULT);
1043         }
1044
1045         keylen = req_capsule_get_size(&info->mti_pill, &RMF_SETINFO_KEY,
1046                                       RCL_CLIENT);
1047
1048         val = req_capsule_client_get(&info->mti_pill, &RMF_SETINFO_VAL);
1049         if (val == NULL) {
1050                 DEBUG_REQ(D_HA, req, "no set_info val");
1051                 RETURN(-EFAULT);
1052         }
1053
1054         if (keylen != (sizeof(KEY_READ_ONLY) - 1) ||
1055             memcmp(key, KEY_READ_ONLY, keylen) != 0)
1056                 RETURN(-EINVAL);
1057
1058         req->rq_status = 0;
1059         lustre_msg_set_status(req->rq_repmsg, 0);
1060
1061         spin_lock(&req->rq_export->exp_lock);
1062         if (*val)
1063                 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1064         else
1065                 req->rq_export->exp_connect_flags &= ~OBD_CONNECT_RDONLY;
1066         spin_unlock(&req->rq_export->exp_lock);
1067
1068         RETURN(0);
1069 }
1070
1071 static int mdt_connect(struct mdt_thread_info *info)
1072 {
1073         int rc;
1074         struct ptlrpc_request *req;
1075
1076         req = mdt_info_req(info);
1077         rc = target_handle_connect(req);
1078         if (rc == 0) {
1079                 LASSERT(req->rq_export != NULL);
1080                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1081                 rc = mdt_init_idmap(info);
1082                 if (rc != 0)
1083                         /* if mdt_init_idmap failed, revocation for connect */
1084                         obd_disconnect(class_export_get(req->rq_export));
1085         } else
1086                 rc = err_serious(rc);
1087         return rc;
1088 }
1089
1090 static int mdt_disconnect(struct mdt_thread_info *info)
1091 {
1092         int rc;
1093         ENTRY;
1094
1095         rc = target_handle_disconnect(mdt_info_req(info));
1096         if (rc)
1097                 rc = err_serious(rc);
1098         RETURN(rc);
1099 }
1100
1101 static int mdt_sendpage(struct mdt_thread_info *info,
1102                         struct lu_rdpg *rdpg)
1103 {
1104         struct ptlrpc_request   *req = mdt_info_req(info);
1105         struct ptlrpc_bulk_desc *desc;
1106         struct l_wait_info      *lwi = &info->mti_u.rdpg.mti_wait_info;
1107         int                      tmpcount;
1108         int                      tmpsize;
1109         int                      i;
1110         int                      rc;
1111         ENTRY;
1112
1113         desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1114                                     MDS_BULK_PORTAL);
1115         if (desc == NULL)
1116                 GOTO(out, rc = -ENOMEM);
1117
1118         for (i = 0, tmpcount = rdpg->rp_count;
1119                 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
1120                 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1121                 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1122         }
1123
1124         LASSERT(desc->bd_nob == rdpg->rp_count);
1125         rc = ptlrpc_start_bulk_transfer(desc);
1126         if (rc)
1127                 GOTO(free_desc, rc);
1128
1129         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1130                 GOTO(abort_bulk, rc);
1131
1132         *lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
1133         rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), lwi);
1134         LASSERT (rc == 0 || rc == -ETIMEDOUT);
1135
1136         if (rc == 0) {
1137                 if (desc->bd_success &&
1138                     desc->bd_nob_transferred == rdpg->rp_count)
1139                         GOTO(free_desc, rc);
1140
1141                 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
1142         }
1143
1144         DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s",
1145                   (rc == -ETIMEDOUT) ? "timeout" : "network error",
1146                   desc->bd_nob_transferred, rdpg->rp_count,
1147                   req->rq_export->exp_client_uuid.uuid,
1148                   req->rq_export->exp_connection->c_remote_uuid.uuid);
1149
1150         class_fail_export(req->rq_export);
1151
1152         EXIT;
1153 abort_bulk:
1154         ptlrpc_abort_bulk(desc);
1155 free_desc:
1156         ptlrpc_free_bulk(desc);
1157 out:
1158         return rc;
1159 }
1160
1161 #ifdef HAVE_SPLIT_SUPPORT
1162 /*
1163  * Retrieve dir entry from the page and insert it to the slave object, actually,
1164  * this should be in osd layer, but since it will not in the final product, so
1165  * just do it here and do not define more moo api anymore for this.
1166  */
1167 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1168                               int size)
1169 {
1170         struct mdt_object *object = info->mti_object;
1171         struct lu_fid *lf = &info->mti_tmp_fid2;
1172         struct md_attr *ma = &info->mti_attr;
1173         struct lu_dirpage *dp;
1174         struct lu_dirent *ent;
1175         int rc = 0, offset = 0;
1176         ENTRY;
1177
1178         /* Make sure we have at least one entry. */
1179         if (size == 0)
1180                 RETURN(-EINVAL);
1181
1182         /*
1183          * Disable trans for this name insert, since it will include many trans
1184          * for this.
1185          */
1186         info->mti_no_need_trans = 1;
1187         /*
1188          * When write_dir_page, no need update parent's ctime,
1189          * and no permission check for name_insert.
1190          */
1191         ma->ma_attr.la_ctime = 0;
1192         ma->ma_attr.la_valid = LA_MODE;
1193         ma->ma_valid = MA_INODE;
1194
1195         kmap(page);
1196         dp = page_address(page);
1197         offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1198
1199         for (ent = lu_dirent_start(dp); ent != NULL;
1200              ent = lu_dirent_next(ent)) {
1201                 struct lu_name *lname;
1202                 char *name;
1203
1204                 if (le16_to_cpu(ent->lde_namelen) == 0)
1205                         continue;
1206
1207                 fid_le_to_cpu(lf, &ent->lde_fid);
1208                 if (le32_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1209                         ma->ma_attr.la_mode = S_IFDIR;
1210                 else
1211                         ma->ma_attr.la_mode = 0;
1212                 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1213                 if (name == NULL)
1214                         GOTO(out, rc = -ENOMEM);
1215
1216                 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1217                 lname = mdt_name(info->mti_env, name,
1218                                  le16_to_cpu(ent->lde_namelen) + 1);
1219                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1220                 rc = mdo_name_insert(info->mti_env,
1221                                      md_object_next(&object->mot_obj),
1222                                      lname, lf, ma);
1223                 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1224                 if (rc) {
1225                         CERROR("Can't insert %*.*s, rc %d\n",
1226                                le16_to_cpu(ent->lde_namelen),
1227                                le16_to_cpu(ent->lde_namelen),
1228                                ent->lde_name, rc);
1229                         GOTO(out, rc);
1230                 }
1231
1232                 offset += lu_dirent_size(ent);
1233                 if (offset >= size)
1234                         break;
1235         }
1236         EXIT;
1237 out:
1238         kunmap(page);
1239         return rc;
1240 }
1241
1242 static int mdt_bulk_timeout(void *data)
1243 {
1244         ENTRY;
1245
1246         CERROR("mdt bulk transfer timeout \n");
1247
1248         RETURN(1);
1249 }
1250
1251 static int mdt_writepage(struct mdt_thread_info *info)
1252 {
1253         struct ptlrpc_request   *req = mdt_info_req(info);
1254         struct mdt_body         *reqbody;
1255         struct l_wait_info      *lwi;
1256         struct ptlrpc_bulk_desc *desc;
1257         struct page             *page;
1258         int                rc;
1259         ENTRY;
1260
1261
1262         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
1263         if (reqbody == NULL)
1264                 RETURN(err_serious(-EFAULT));
1265
1266         desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1267         if (desc == NULL)
1268                 RETURN(err_serious(-ENOMEM));
1269
1270         /* allocate the page for the desc */
1271         page = cfs_alloc_page(CFS_ALLOC_STD);
1272         if (page == NULL)
1273                 GOTO(desc_cleanup, rc = -ENOMEM);
1274
1275         CDEBUG(D_INFO, "Received page offset %d size %d \n",
1276                (int)reqbody->size, (int)reqbody->nlink);
1277
1278         ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1279                               (int)reqbody->nlink);
1280
1281         /*
1282          * Check if client was evicted while we were doing i/o before touching
1283          * network.
1284          */
1285         OBD_ALLOC_PTR(lwi);
1286         if (!lwi)
1287                 GOTO(cleanup_page, rc = -ENOMEM);
1288
1289         if (desc->bd_export->exp_failed)
1290                 rc = -ENOTCONN;
1291         else
1292                 rc = ptlrpc_start_bulk_transfer (desc);
1293         if (rc == 0) {
1294                 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1295                                             mdt_bulk_timeout, desc);
1296                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1297                                   desc->bd_export->exp_failed, lwi);
1298                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1299                 if (rc == -ETIMEDOUT) {
1300                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1301                         ptlrpc_abort_bulk(desc);
1302                 } else if (desc->bd_export->exp_failed) {
1303                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1304                         rc = -ENOTCONN;
1305                         ptlrpc_abort_bulk(desc);
1306                 } else if (!desc->bd_success ||
1307                            desc->bd_nob_transferred != desc->bd_nob) {
1308                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1309                                   desc->bd_success ?
1310                                   "truncated" : "network error on",
1311                                   desc->bd_nob_transferred, desc->bd_nob);
1312                         /* XXX should this be a different errno? */
1313                         rc = -ETIMEDOUT;
1314                 }
1315         } else {
1316                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1317         }
1318         if (rc)
1319                 GOTO(cleanup_lwi, rc);
1320         rc = mdt_write_dir_page(info, page, reqbody->nlink);
1321
1322 cleanup_lwi:
1323         OBD_FREE_PTR(lwi);
1324 cleanup_page:
1325         __cfs_free_page(page);
1326 desc_cleanup:
1327         ptlrpc_free_bulk(desc);
1328         RETURN(rc);
1329 }
1330 #endif
1331
1332 static int mdt_readpage(struct mdt_thread_info *info)
1333 {
1334         struct mdt_object *object = info->mti_object;
1335         struct lu_rdpg    *rdpg = &info->mti_u.rdpg.mti_rdpg;
1336         struct mdt_body   *reqbody;
1337         struct mdt_body   *repbody;
1338         int                rc;
1339         int                i;
1340         ENTRY;
1341
1342         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1343                 RETURN(err_serious(-ENOMEM));
1344
1345         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
1346         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
1347         if (reqbody == NULL || repbody == NULL)
1348                 RETURN(err_serious(-EFAULT));
1349
1350         /*
1351          * prepare @rdpg before calling lower layers and transfer itself. Here
1352          * reqbody->size contains offset of where to start to read and
1353          * reqbody->nlink contains number bytes to read.
1354          */
1355         rdpg->rp_hash = reqbody->size;
1356         if ((__u64)rdpg->rp_hash != reqbody->size) {
1357                 CERROR("Invalid hash: %#llx != %#llx\n",
1358                        (__u64)rdpg->rp_hash, reqbody->size);
1359                 RETURN(-EFAULT);
1360         }
1361         rdpg->rp_count  = reqbody->nlink;
1362         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1363         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1364         if (rdpg->rp_pages == NULL)
1365                 RETURN(-ENOMEM);
1366
1367         for (i = 0; i < rdpg->rp_npages; ++i) {
1368                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1369                 if (rdpg->rp_pages[i] == NULL)
1370                         GOTO(free_rdpg, rc = -ENOMEM);
1371         }
1372
1373         /* call lower layers to fill allocated pages with directory data */
1374         rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1375         if (rc)
1376                 GOTO(free_rdpg, rc);
1377
1378         /* send pages to client */
1379         rc = mdt_sendpage(info, rdpg);
1380
1381         EXIT;
1382 free_rdpg:
1383
1384         for (i = 0; i < rdpg->rp_npages; i++)
1385                 if (rdpg->rp_pages[i] != NULL)
1386                         __cfs_free_page(rdpg->rp_pages[i]);
1387         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1388
1389         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1390                 RETURN(0);
1391
1392         return rc;
1393 }
1394
1395 static int mdt_reint_internal(struct mdt_thread_info *info,
1396                               struct mdt_lock_handle *lhc,
1397                               __u32 op)
1398 {
1399         struct req_capsule      *pill = &info->mti_pill;
1400         struct mdt_device       *mdt = info->mti_mdt;
1401         struct mdt_body         *repbody;
1402         int                      need_shrink = 0;
1403         int                      rc;
1404         ENTRY;
1405
1406         /* pack reply */
1407         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
1408                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1409                                      mdt->mdt_max_mdsize);
1410                 need_shrink = 1;
1411         }
1412         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
1413                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1414                                      mdt->mdt_max_cookiesize);
1415                 need_shrink = 1;
1416         }
1417         rc = req_capsule_pack(pill);
1418         if (rc != 0) {
1419                 CERROR("Can't pack response, rc %d\n", rc);
1420                 RETURN(err_serious(rc));
1421         }
1422
1423         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1424                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1425                 LASSERT(repbody);
1426                 repbody->eadatasize = 0;
1427                 repbody->aclsize = 0;
1428         }
1429
1430         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1431                 GOTO(out_shrink, rc = err_serious(-EFAULT));
1432
1433         rc = mdt_reint_unpack(info, op);
1434         if (rc != 0) {
1435                 CERROR("Can't unpack reint, rc %d\n", rc);
1436                 GOTO(out_shrink, rc = err_serious(rc));
1437         }
1438
1439         rc = mdt_init_ucred_reint(info);
1440         if (rc)
1441                 GOTO(out_shrink, rc);
1442
1443         rc = mdt_fix_attr_ucred(info, op);
1444         if (rc != 0)
1445                 GOTO(out_ucred, rc = err_serious(rc));
1446
1447         need_shrink = 0;
1448         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1449                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1450                 GOTO(out_ucred, rc);
1451         }
1452         
1453         rc = mdt_reint_rec(info, lhc);
1454         EXIT;
1455 out_ucred:
1456         mdt_exit_ucred(info);
1457 out_shrink:
1458         if (need_shrink)
1459                 mdt_shrink_reply(info);
1460         return rc;
1461 }
1462
1463 static long mdt_reint_opcode(struct mdt_thread_info *info,
1464                              const struct req_format **fmt)
1465 {
1466         __u32 *ptr;
1467         long opc;
1468
1469         opc = err_serious(-EFAULT);
1470         ptr = req_capsule_client_get(&info->mti_pill, &RMF_REINT_OPC);
1471         if (ptr != NULL) {
1472                 opc = *ptr;
1473                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1474                 if (opc < REINT_MAX && fmt[opc] != NULL)
1475                         req_capsule_extend(&info->mti_pill, fmt[opc]);
1476                 else {
1477                         CERROR("Unsupported opc: %ld\n", opc);
1478                         opc = err_serious(opc);
1479                 }
1480         }
1481         return opc;
1482 }
1483
1484 static int mdt_reint(struct mdt_thread_info *info)
1485 {
1486         long opc;
1487         int  rc;
1488
1489         static const struct req_format *reint_fmts[REINT_MAX] = {
1490                 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1491                 [REINT_CREATE]  = &RQF_MDS_REINT_CREATE,
1492                 [REINT_LINK]    = &RQF_MDS_REINT_LINK,
1493                 [REINT_UNLINK]  = &RQF_MDS_REINT_UNLINK,
1494                 [REINT_RENAME]  = &RQF_MDS_REINT_RENAME,
1495                 [REINT_OPEN]    = &RQF_MDS_REINT_OPEN
1496         };
1497
1498         ENTRY;
1499
1500         opc = mdt_reint_opcode(info, reint_fmts);
1501         if (opc >= 0) {
1502                 /*
1503                  * No lock possible here from client to pass it to reint code
1504                  * path.
1505                  */
1506                 rc = mdt_reint_internal(info, NULL, opc);
1507         } else {
1508                 rc = opc;
1509         }
1510
1511         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1512         RETURN(rc);
1513 }
1514
1515 /* TODO these two methods not available now. */
1516
1517 /* this should sync the whole device */
1518 static int mdt_device_sync(struct mdt_thread_info *info)
1519 {
1520         return 0;
1521 }
1522
1523 /* this should sync this object */
1524 static int mdt_object_sync(struct mdt_thread_info *info)
1525 {
1526         return 0;
1527 }
1528
1529 static int mdt_sync(struct mdt_thread_info *info)
1530 {
1531         struct req_capsule *pill = &info->mti_pill;
1532         struct mdt_body *body;
1533         int rc;
1534         ENTRY;
1535
1536         /* The fid may be zero, so we req_capsule_set manually */
1537         req_capsule_set(pill, &RQF_MDS_SYNC);
1538
1539         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1540         if (body == NULL)
1541                 RETURN(err_serious(-EINVAL));
1542
1543         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1544                 RETURN(err_serious(-ENOMEM));
1545
1546         if (fid_seq(&body->fid1) == 0) {
1547                 /* sync the whole device */
1548                 rc = req_capsule_pack(pill);
1549                 if (rc == 0)
1550                         rc = mdt_device_sync(info);
1551                 else
1552                         rc = err_serious(rc);
1553         } else {
1554                 /* sync an object */
1555                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1556                 if (rc == 0) {
1557                         rc = mdt_object_sync(info);
1558                         if (rc == 0) {
1559                                 struct md_object *next;
1560                                 const struct lu_fid *fid;
1561                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1562
1563                                 next = mdt_object_child(info->mti_object);
1564                                 info->mti_attr.ma_need = MA_INODE;
1565                                 info->mti_attr.ma_valid = 0;
1566                                 rc = mo_attr_get(info->mti_env, next,
1567                                                  &info->mti_attr);
1568                                 if (rc == 0) {
1569                                         body = req_capsule_server_get(pill,
1570                                                                 &RMF_MDT_BODY);
1571                                         fid = mdt_object_fid(info->mti_object);
1572                                         mdt_pack_attr2body(info, body, la, fid);
1573                                 }
1574                         }
1575                 } else
1576                         rc = err_serious(rc);
1577         }
1578         RETURN(rc);
1579 }
1580
1581 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1582 {
1583         return err_serious(-EOPNOTSUPP);
1584 }
1585
1586 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1587 {
1588         return err_serious(-EOPNOTSUPP);
1589 }
1590
1591 /*
1592  * OBD PING and other handlers.
1593  */
1594 static int mdt_obd_ping(struct mdt_thread_info *info)
1595 {
1596         int rc;
1597         ENTRY;
1598         rc = target_handle_ping(mdt_info_req(info));
1599         if (rc < 0)
1600                 rc = err_serious(rc);
1601         RETURN(rc);
1602 }
1603
1604 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1605 {
1606         return err_serious(-EOPNOTSUPP);
1607 }
1608
1609 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1610 {
1611         return err_serious(-EOPNOTSUPP);
1612 }
1613
1614
1615 /*
1616  * DLM handlers.
1617  */
1618 static struct ldlm_callback_suite cbs = {
1619         .lcs_completion = ldlm_server_completion_ast,
1620         .lcs_blocking   = ldlm_server_blocking_ast,
1621         .lcs_glimpse    = NULL
1622 };
1623
1624 static int mdt_enqueue(struct mdt_thread_info *info)
1625 {
1626         struct ptlrpc_request *req;
1627         __u64 req_bits;
1628         int rc;
1629
1630         /*
1631          * info->mti_dlm_req already contains swapped and (if necessary)
1632          * converted dlm request.
1633          */
1634         LASSERT(info->mti_dlm_req != NULL);
1635
1636         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE)) {
1637                 info->mti_fail_id = OBD_FAIL_LDLM_ENQUEUE;
1638                 return 0;
1639         }
1640
1641         req = mdt_info_req(info);
1642
1643         /*
1644          * Lock without inodebits makes no sense and will oops later in
1645          * ldlm. Let's check it now to see if we have wrong lock from client or
1646          * bits get corrupted somewhere in mdt_intent_policy().
1647          */
1648         req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1649         /* This is disabled because we need to support liblustre flock.
1650          * LASSERT(req_bits != 0);
1651          */
1652
1653         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1654                                   req, info->mti_dlm_req, &cbs);
1655         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1656         return rc ? err_serious(rc) : req->rq_status;
1657 }
1658
1659 static int mdt_convert(struct mdt_thread_info *info)
1660 {
1661         int rc;
1662         struct ptlrpc_request *req;
1663
1664         LASSERT(info->mti_dlm_req);
1665         req = mdt_info_req(info);
1666         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1667         return rc ? err_serious(rc) : req->rq_status;
1668 }
1669
1670 static int mdt_bl_callback(struct mdt_thread_info *info)
1671 {
1672         CERROR("bl callbacks should not happen on MDS\n");
1673         LBUG();
1674         return err_serious(-EOPNOTSUPP);
1675 }
1676
1677 static int mdt_cp_callback(struct mdt_thread_info *info)
1678 {
1679         CERROR("cp callbacks should not happen on MDS\n");
1680         LBUG();
1681         return err_serious(-EOPNOTSUPP);
1682 }
1683
1684 /*
1685  * sec context handlers
1686  */
1687 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
1688 {
1689         int rc;
1690
1691         rc = mdt_handle_idmap(info);
1692
1693         if (unlikely(rc)) {
1694                 struct ptlrpc_request *req = mdt_info_req(info);
1695                 __u32                  opc;
1696
1697                 opc = lustre_msg_get_opc(req->rq_reqmsg);
1698                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
1699                         sptlrpc_svc_ctx_invalidate(req);
1700         }
1701
1702         return rc;
1703 }
1704
1705 static struct mdt_object *mdt_obj(struct lu_object *o)
1706 {
1707         LASSERT(lu_device_is_mdt(o->lo_dev));
1708         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
1709 }
1710
1711 struct mdt_object *mdt_object_find(const struct lu_env *env,
1712                                    struct mdt_device *d,
1713                                    const struct lu_fid *f)
1714 {
1715         struct lu_object *o;
1716         struct mdt_object *m;
1717         ENTRY;
1718
1719         o = lu_object_find(env, d->mdt_md_dev.md_lu_dev.ld_site, f);
1720         if (unlikely(IS_ERR(o)))
1721                 m = (struct mdt_object *)o;
1722         else
1723                 m = mdt_obj(o);
1724         RETURN(m);
1725 }
1726
1727 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
1728                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
1729 {
1730         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1731         ldlm_policy_data_t *policy = &info->mti_policy;
1732         struct ldlm_res_id *res_id = &info->mti_res_id;
1733         int rc;
1734         ENTRY;
1735
1736         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1737         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1738         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
1739         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
1740
1741         if (mdt_object_exists(o) < 0) {
1742                 if (locality == MDT_CROSS_LOCK) {
1743                         /* cross-ref object fix */
1744                         ibits &= ~MDS_INODELOCK_UPDATE;
1745                         ibits |= MDS_INODELOCK_LOOKUP;
1746                 } else {
1747                         LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
1748                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
1749                 }
1750                 /* No PDO lock on remote object */
1751                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
1752         }
1753
1754         memset(policy, 0, sizeof(*policy));
1755         fid_build_reg_res_name(mdt_object_fid(o), res_id);
1756
1757         /*
1758          * Take PDO lock on whole directory and build correct @res_id for lock
1759          * on part of directory.
1760          */
1761         if (lh->mlh_pdo_hash != 0) {
1762                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
1763                 mdt_lock_pdo_mode(info, o, lh);
1764                 if (lh->mlh_pdo_mode != LCK_NL) {
1765                         /*
1766                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
1767                          * is never going to be sent to client and we do not
1768                          * want it slowed down due to possible cancels.
1769                          */
1770                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1771                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
1772                                           policy, res_id, LDLM_FL_ATOMIC_CB);
1773                         if (unlikely(rc))
1774                                 RETURN(rc);
1775                 }
1776
1777                 /*
1778                  * Finish res_id initializing by name hash marking patr of
1779                  * directory which is taking modification.
1780                  */
1781                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
1782         }
1783
1784         policy->l_inodebits.bits = ibits;
1785
1786         /*
1787          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1788          * going to be sent to client. If it is - mdt_intent_policy() path will
1789          * fix it up and turns FL_LOCAL flag off.
1790          */
1791         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1792                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB);
1793
1794         if (rc)
1795                 GOTO(out, rc);
1796
1797         if (lh->mlh_type == MDT_PDO_LOCK) {
1798                 /* check for exists after object is locked */
1799                 if (mdt_object_exists(o) == 0) {
1800                         /* Non-existent object shouldn't have PDO lock */
1801                         rc = -ESTALE;
1802                 } else {
1803                         /* Non-dir object shouldn't have PDO lock */
1804                         LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
1805                 }
1806         }
1807 out:
1808         if (rc)
1809                 mdt_object_unlock(info, o, lh, 1);
1810
1811
1812         RETURN(rc);
1813 }
1814
1815 /*
1816  * Just call ldlm_lock_decref() if decref, else we only call ptlrpc_save_lock()
1817  * to save this lock in req.  when transaction committed, req will be released,
1818  * and lock will, too.
1819  */
1820 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
1821                        struct mdt_lock_handle *lh, int decref)
1822 {
1823         struct ptlrpc_request *req = mdt_info_req(info);
1824         ENTRY;
1825
1826         if (lustre_handle_is_used(&lh->mlh_pdo_lh)) {
1827                 /* Do not save PDO locks to request, just decref. */
1828                 mdt_fid_unlock(&lh->mlh_pdo_lh,
1829                                lh->mlh_pdo_mode);
1830                 lh->mlh_pdo_lh.cookie = 0ull;
1831         }
1832
1833         if (lustre_handle_is_used(&lh->mlh_reg_lh)) {
1834                 if (decref) {
1835                         mdt_fid_unlock(&lh->mlh_reg_lh,
1836                                        lh->mlh_reg_mode);
1837                 } else {
1838                         ptlrpc_save_lock(req, &lh->mlh_reg_lh,
1839                                          lh->mlh_reg_mode);
1840                 }
1841                 lh->mlh_reg_lh.cookie = 0ull;
1842         }
1843
1844         EXIT;
1845 }
1846
1847 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
1848                                         const struct lu_fid *f,
1849                                         struct mdt_lock_handle *lh,
1850                                         __u64 ibits)
1851 {
1852         struct mdt_object *o;
1853
1854         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
1855         if (!IS_ERR(o)) {
1856                 int rc;
1857
1858                 rc = mdt_object_lock(info, o, lh, ibits,
1859                                      MDT_LOCAL_LOCK);
1860                 if (rc != 0) {
1861                         mdt_object_put(info->mti_env, o);
1862                         o = ERR_PTR(rc);
1863                 }
1864         }
1865         return o;
1866 }
1867
1868 void mdt_object_unlock_put(struct mdt_thread_info * info,
1869                            struct mdt_object * o,
1870                            struct mdt_lock_handle *lh,
1871                            int decref)
1872 {
1873         mdt_object_unlock(info, o, lh, decref);
1874         mdt_object_put(info->mti_env, o);
1875 }
1876
1877 static struct mdt_handler *mdt_handler_find(__u32 opc,
1878                                             struct mdt_opc_slice *supported)
1879 {
1880         struct mdt_opc_slice *s;
1881         struct mdt_handler   *h;
1882
1883         h = NULL;
1884         for (s = supported; s->mos_hs != NULL; s++) {
1885                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
1886                         h = s->mos_hs + (opc - s->mos_opc_start);
1887                         if (likely(h->mh_opc != 0))
1888                                 LASSERT(h->mh_opc == opc);
1889                         else
1890                                 h = NULL; /* unsupported opc */
1891                         break;
1892                 }
1893         }
1894         return h;
1895 }
1896
1897 static int mdt_lock_resname_compat(struct mdt_device *m,
1898                                    struct ldlm_request *req)
1899 {
1900         /* XXX something... later. */
1901         return 0;
1902 }
1903
1904 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
1905 {
1906         /* XXX something... later. */
1907         return 0;
1908 }
1909
1910 /*
1911  * Generic code handling requests that have struct mdt_body passed in:
1912  *
1913  *  - extract mdt_body from request and save it in @info, if present;
1914  *
1915  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
1916  *  @info;
1917  *
1918  *  - if HABEO_CORPUS flag is set for this request type check whether object
1919  *  actually exists on storage (lu_object_exists()).
1920  *
1921  */
1922 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
1923 {
1924         const struct mdt_body    *body;
1925         struct mdt_object        *obj;
1926         const struct lu_env      *env;
1927         struct req_capsule       *pill;
1928         int                       rc;
1929
1930         env = info->mti_env;
1931         pill = &info->mti_pill;
1932
1933         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1934         if (body == NULL)
1935                 return -EFAULT;
1936
1937         if (!fid_is_sane(&body->fid1)) {
1938                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
1939                 return -EINVAL;
1940         }
1941
1942         /*
1943          * Do not get size or any capa fields before we check that request
1944          * contains capa actually. There are some requests which do not, for
1945          * instance MDS_IS_SUBDIR.
1946          */
1947         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
1948             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1949                 mdt_set_capainfo(info, 0, &body->fid1,
1950                                  req_capsule_client_get(pill, &RMF_CAPA1));
1951
1952         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
1953         if (!IS_ERR(obj)) {
1954                 if ((flags & HABEO_CORPUS) &&
1955                     !mdt_object_exists(obj)) {
1956                         mdt_object_put(env, obj);
1957                         /* for capability renew ENOENT will be handled in
1958                          * mdt_renew_capa */
1959                         if (body->valid & OBD_MD_FLOSSCAPA)
1960                                 rc = 0;
1961                         else
1962                                 rc = -ENOENT;
1963                 } else {
1964                         info->mti_object = obj;
1965                         rc = 0;
1966                 }
1967         } else
1968                 rc = PTR_ERR(obj);
1969
1970         return rc;
1971 }
1972
1973 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
1974 {
1975         struct req_capsule *pill;
1976         int rc;
1977
1978         ENTRY;
1979         pill = &info->mti_pill;
1980
1981         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
1982                 rc = mdt_body_unpack(info, flags);
1983         else
1984                 rc = 0;
1985
1986         if (rc == 0 && (flags & HABEO_REFERO)) {
1987                 struct mdt_device *mdt = info->mti_mdt;
1988
1989                 /* Pack reply. */
1990                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1991                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1992                                              mdt->mdt_max_mdsize);
1993                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1994                         req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1995                                              mdt->mdt_max_cookiesize);
1996
1997                 rc = req_capsule_pack(pill);
1998         }
1999         RETURN(rc);
2000 }
2001
2002 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2003 {
2004         struct md_device *next = m->mdt_child;
2005
2006         return next->md_ops->mdo_init_capa_ctxt(env, next,
2007                                                 m->mdt_opts.mo_mds_capa,
2008                                                 m->mdt_capa_timeout,
2009                                                 m->mdt_capa_alg,
2010                                                 m->mdt_capa_keys);
2011 }
2012
2013 /*
2014  * Invoke handler for this request opc. Also do necessary preprocessing
2015  * (according to handler ->mh_flags), and post-processing (setting of
2016  * ->last_{xid,committed}).
2017  */
2018 static int mdt_req_handle(struct mdt_thread_info *info,
2019                           struct mdt_handler *h, struct ptlrpc_request *req)
2020 {
2021         int   rc, serious = 0;
2022         __u32 flags;
2023
2024         ENTRY;
2025
2026         LASSERT(h->mh_act != NULL);
2027         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2028         LASSERT(current->journal_info == NULL);
2029
2030         /*
2031          * Mask out OBD_FAIL_ONCE, because that will stop
2032          * correct handling of failed req later in ldlm due to doing
2033          * obd_fail_loc |= OBD_FAIL_ONCE without actually
2034          * correct actions like it is done in target_send_reply_msg().
2035          */
2036         if (h->mh_fail_id != 0) {
2037                 /*
2038                  * Set to info->mti_fail_id to handler fail_id, it will be used
2039                  * later, and better than use default fail_id.
2040                  */
2041                 if (OBD_FAIL_CHECK_RESET(h->mh_fail_id && OBD_FAIL_MASK_LOC,
2042                                          h->mh_fail_id & ~OBD_FAILED)) {
2043                         info->mti_fail_id = h->mh_fail_id;
2044                         RETURN(0);
2045                 }
2046         }
2047
2048         rc = 0;
2049         flags = h->mh_flags;
2050         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2051
2052         if (h->mh_fmt != NULL) {
2053                 req_capsule_set(&info->mti_pill, h->mh_fmt);
2054                 rc = mdt_unpack_req_pack_rep(info, flags);
2055         }
2056
2057         if (rc == 0 && flags & MUTABOR &&
2058             req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2059                 /* should it be rq_status? */
2060                 rc = -EROFS;
2061
2062         if (rc == 0 && flags & HABEO_CLAVIS) {
2063                 struct ldlm_request *dlm_req;
2064
2065                 LASSERT(h->mh_fmt != NULL);
2066
2067                 dlm_req = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
2068                 if (dlm_req != NULL) {
2069                         if (info->mti_mdt->mdt_opts.mo_compat_resname)
2070                                 rc = mdt_lock_resname_compat(info->mti_mdt,
2071                                                              dlm_req);
2072                         info->mti_dlm_req = dlm_req;
2073                 } else {
2074                         CERROR("Can't unpack dlm request\n");
2075                         rc = -EFAULT;
2076                 }
2077         }
2078
2079         /* capability setting changed via /proc, needs reinitialize ctxt */
2080         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2081                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2082                 info->mti_mdt->mdt_capa_conf = 0;
2083         }
2084
2085         if (likely(rc == 0)) {
2086                 /*
2087                  * Process request, there can be two types of rc:
2088                  * 1) errors with msg unpack/pack, other failures outside the
2089                  * operation itself. This is counted as serious errors;
2090                  * 2) errors during fs operation, should be placed in rq_status
2091                  * only
2092                  */
2093                 rc = h->mh_act(info);
2094                 serious = is_serious(rc);
2095                 rc = clear_serious(rc);
2096         } else
2097                 serious = 1;
2098
2099         req->rq_status = rc;
2100
2101         /*
2102          * ELDLM_* codes which > 0 should be in rq_status only as well as
2103          * all non-serious errors.
2104          */
2105         if (rc > 0 || !serious)
2106                 rc = 0;
2107
2108         LASSERT(current->journal_info == NULL);
2109
2110         if (rc == 0 && (flags & HABEO_CLAVIS) &&
2111             info->mti_mdt->mdt_opts.mo_compat_resname) {
2112                 struct ldlm_reply *dlmrep;
2113
2114                 dlmrep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2115                 if (dlmrep != NULL)
2116                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2117         }
2118
2119         /* If we're DISCONNECTing, the mdt_export_data is already freed */
2120         if (likely(rc == 0 && h->mh_opc != MDS_DISCONNECT))
2121                 target_committed_to_req(req);
2122
2123         if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2124                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2125                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2126                 LBUG();
2127         }
2128
2129         RETURN(rc);
2130 }
2131
2132 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2133 {
2134         lh->mlh_type = MDT_NUL_LOCK;
2135         lh->mlh_reg_lh.cookie = 0ull;
2136         lh->mlh_reg_mode = LCK_MINMODE;
2137         lh->mlh_pdo_lh.cookie = 0ull;
2138         lh->mlh_pdo_mode = LCK_MINMODE;
2139 }
2140
2141 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2142 {
2143         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2144         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2145 }
2146
2147 /*
2148  * Initialize fields of struct mdt_thread_info. Other fields are left in
2149  * uninitialized state, because it's too expensive to zero out whole
2150  * mdt_thread_info (> 1K) on each request arrival.
2151  */
2152 static void mdt_thread_info_init(struct ptlrpc_request *req,
2153                                  struct mdt_thread_info *info)
2154 {
2155         int i;
2156         struct md_capainfo *ci;
2157
2158         info->mti_rep_buf_nr = ARRAY_SIZE(info->mti_rep_buf_size);
2159         for (i = 0; i < ARRAY_SIZE(info->mti_rep_buf_size); i++)
2160                 info->mti_rep_buf_size[i] = -1;
2161         req_capsule_init(&info->mti_pill, req, RCL_SERVER,
2162                          info->mti_rep_buf_size);
2163
2164         /* lock handle */
2165         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2166                 mdt_lock_handle_init(&info->mti_lh[i]);
2167
2168         /* mdt device: it can be NULL while CONNECT */
2169         if (req->rq_export) {
2170                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2171                 info->mti_exp = req->rq_export;
2172         } else
2173                 info->mti_mdt = NULL;
2174         info->mti_env = req->rq_svc_thread->t_env;
2175         ci = md_capainfo(info->mti_env);
2176         memset(ci, 0, sizeof *ci);
2177
2178         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2179         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2180
2181         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2182         info->mti_body = NULL;
2183         info->mti_object = NULL;
2184         info->mti_dlm_req = NULL;
2185         info->mti_has_trans = 0;
2186         info->mti_no_need_trans = 0;
2187         info->mti_cross_ref = 0;
2188         info->mti_opdata = 0;
2189
2190         /* To not check for split by default. */
2191         info->mti_spec.sp_ck_split = 0;
2192 }
2193
2194 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2195 {
2196         int i;
2197
2198         req_capsule_fini(&info->mti_pill);
2199         if (info->mti_object != NULL) {
2200                 mdt_object_put(info->mti_env, info->mti_object);
2201                 info->mti_object = NULL;
2202         }
2203         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2204                 mdt_lock_handle_fini(&info->mti_lh[i]);
2205         info->mti_env = NULL;
2206 }
2207
2208 /* mds/handler.c */
2209 extern int mds_filter_recovery_request(struct ptlrpc_request *req,
2210                                        struct obd_device *obd, int *process);
2211 /*
2212  * Handle recovery. Return:
2213  *        +1: continue request processing;
2214  *       -ve: abort immediately with the given error code;
2215  *         0: send reply with error code in req->rq_status;
2216  */
2217 static int mdt_recovery(struct mdt_thread_info *info)
2218 {
2219         struct ptlrpc_request *req = mdt_info_req(info);
2220         int recovering;
2221         struct obd_device *obd;
2222
2223         ENTRY;
2224
2225         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2226         case MDS_CONNECT:
2227         case SEC_CTX_INIT:
2228         case SEC_CTX_INIT_CONT:
2229         case SEC_CTX_FINI:
2230                 {
2231 #if 0
2232                         int rc;
2233
2234                         rc = mdt_handle_idmap(info);
2235                         if (rc)
2236                                 RETURN(rc);
2237                         else
2238 #endif
2239                                 RETURN(+1);
2240                 }
2241         }
2242
2243         if (unlikely(req->rq_export == NULL)) {
2244                 CERROR("operation %d on unconnected MDS from %s\n",
2245                        lustre_msg_get_opc(req->rq_reqmsg),
2246                        libcfs_id2str(req->rq_peer));
2247                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2248                  * mds_A will get -ENOTCONN(especially for ping req),
2249                  * which will cause that mds_A deactive timeout, then when
2250                  * mds_A cleanup, the cleanup process will be suspended since
2251                  * deactive timeout is not zero.
2252                  */
2253                 req->rq_status = -ENOTCONN;
2254                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2255                 RETURN(0);
2256         }
2257
2258         /* sanity check: if the xid matches, the request must be marked as a
2259          * resent or replayed */
2260         if (req_xid_is_last(req)) {
2261                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2262                       (MSG_RESENT | MSG_REPLAY))) {
2263                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2264                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2265                                   lustre_msg_get_flags(req->rq_reqmsg));
2266                         LBUG();
2267                         req->rq_status = -ENOTCONN;
2268                         RETURN(-ENOTCONN);
2269                 }
2270         }
2271
2272         /* else: note the opposite is not always true; a RESENT req after a
2273          * failover will usually not match the last_xid, since it was likely
2274          * never committed. A REPLAYed request will almost never match the
2275          * last xid, however it could for a committed, but still retained,
2276          * open. */
2277
2278         obd = req->rq_export->exp_obd;
2279
2280         /* Check for aborted recovery... */
2281         spin_lock_bh(&obd->obd_processing_task_lock);
2282         recovering = obd->obd_recovering;
2283         spin_unlock_bh(&obd->obd_processing_task_lock);
2284         if (unlikely(recovering)) {
2285                 int rc;
2286                 int should_process;
2287                 DEBUG_REQ(D_INFO, req, "Got new replay");
2288                 rc = mds_filter_recovery_request(req, obd, &should_process);
2289                 if (rc != 0 || !should_process)
2290                         RETURN(rc);
2291                 else if (should_process < 0) {
2292                         req->rq_status = should_process;
2293                         rc = ptlrpc_error(req);
2294                         RETURN(rc);
2295                 }
2296         }
2297         RETURN(+1);
2298 }
2299
2300 static int mdt_reply(struct ptlrpc_request *req, int rc,
2301                      struct mdt_thread_info *info)
2302 {
2303         ENTRY;
2304
2305 #if 0
2306         if (req->rq_reply_state == NULL && rc == 0) {
2307                 req->rq_status = rc;
2308                 lustre_pack_reply(req, 1, NULL, NULL);
2309         }
2310 #endif
2311         target_send_reply(req, rc, info->mti_fail_id);
2312         RETURN(0);
2313 }
2314
2315 /* mds/handler.c */
2316 extern int mds_msg_check_version(struct lustre_msg *msg);
2317
2318 static int mdt_handle0(struct ptlrpc_request *req,
2319                        struct mdt_thread_info *info,
2320                        struct mdt_opc_slice *supported)
2321 {
2322         struct mdt_handler *h;
2323         struct lustre_msg  *msg;
2324         int                 rc;
2325
2326         ENTRY;
2327
2328         if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2329                 RETURN(0);
2330
2331         LASSERT(current->journal_info == NULL);
2332
2333         msg = req->rq_reqmsg;
2334         rc = mds_msg_check_version(msg);
2335         if (likely(rc == 0)) {
2336                 rc = mdt_recovery(info);
2337                 if (likely(rc == +1)) {
2338                         h = mdt_handler_find(lustre_msg_get_opc(msg),
2339                                              supported);
2340                         if (likely(h != NULL)) {
2341                                 rc = mdt_req_handle(info, h, req);
2342                                 rc = mdt_reply(req, rc, info);
2343                         } else {
2344                                 CERROR("The unsupported opc: 0x%x\n", lustre_msg_get_opc(msg) );
2345                                 req->rq_status = -ENOTSUPP;
2346                                 rc = ptlrpc_error(req);
2347                                 RETURN(rc);
2348                         }
2349                 }
2350         } else
2351                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2352         RETURN(rc);
2353 }
2354
2355 /*
2356  * MDT handler function called by ptlrpc service thread when request comes.
2357  *
2358  * XXX common "target" functionality should be factored into separate module
2359  * shared by mdt, ost and stand-alone services like fld.
2360  */
2361 static int mdt_handle_common(struct ptlrpc_request *req,
2362                              struct mdt_opc_slice *supported)
2363 {
2364         struct lu_env          *env;
2365         struct mdt_thread_info *info;
2366         int                     rc;
2367         ENTRY;
2368
2369         env = req->rq_svc_thread->t_env;
2370         LASSERT(env != NULL);
2371         LASSERT(env->le_ses != NULL);
2372         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2373         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2374         LASSERT(info != NULL);
2375
2376         mdt_thread_info_init(req, info);
2377
2378         rc = mdt_handle0(req, info, supported);
2379
2380         mdt_thread_info_fini(info);
2381         RETURN(rc);
2382 }
2383
2384 /*
2385  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2386  * as well.
2387  */
2388 int mdt_recovery_handle(struct ptlrpc_request *req)
2389 {
2390         int rc;
2391         ENTRY;
2392
2393         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2394         case FLD_QUERY:
2395                 rc = mdt_handle_common(req, mdt_fld_handlers);
2396                 break;
2397         case SEQ_QUERY:
2398                 rc = mdt_handle_common(req, mdt_seq_handlers);
2399                 break;
2400         default:
2401                 rc = mdt_handle_common(req, mdt_regular_handlers);
2402                 break;
2403         }
2404
2405         RETURN(rc);
2406 }
2407
2408 static int mdt_regular_handle(struct ptlrpc_request *req)
2409 {
2410         return mdt_handle_common(req, mdt_regular_handlers);
2411 }
2412
2413 static int mdt_readpage_handle(struct ptlrpc_request *req)
2414 {
2415         return mdt_handle_common(req, mdt_readpage_handlers);
2416 }
2417
2418 static int mdt_xmds_handle(struct ptlrpc_request *req)
2419 {
2420         return mdt_handle_common(req, mdt_xmds_handlers);
2421 }
2422
2423 static int mdt_mdsc_handle(struct ptlrpc_request *req)
2424 {
2425         return mdt_handle_common(req, mdt_seq_handlers);
2426 }
2427
2428 static int mdt_mdss_handle(struct ptlrpc_request *req)
2429 {
2430         return mdt_handle_common(req, mdt_seq_handlers);
2431 }
2432
2433 static int mdt_dtss_handle(struct ptlrpc_request *req)
2434 {
2435         return mdt_handle_common(req, mdt_seq_handlers);
2436 }
2437
2438 static int mdt_fld_handle(struct ptlrpc_request *req)
2439 {
2440         return mdt_handle_common(req, mdt_fld_handlers);
2441 }
2442
2443 enum mdt_it_code {
2444         MDT_IT_OPEN,
2445         MDT_IT_OCREAT,
2446         MDT_IT_CREATE,
2447         MDT_IT_GETATTR,
2448         MDT_IT_READDIR,
2449         MDT_IT_LOOKUP,
2450         MDT_IT_UNLINK,
2451         MDT_IT_TRUNC,
2452         MDT_IT_GETXATTR,
2453         MDT_IT_NR
2454 };
2455
2456 static int mdt_intent_getattr(enum mdt_it_code opcode,
2457                               struct mdt_thread_info *info,
2458                               struct ldlm_lock **,
2459                               int);
2460 static int mdt_intent_reint(enum mdt_it_code opcode,
2461                             struct mdt_thread_info *info,
2462                             struct ldlm_lock **,
2463                             int);
2464
2465 static struct mdt_it_flavor {
2466         const struct req_format *it_fmt;
2467         __u32                    it_flags;
2468         int                    (*it_act)(enum mdt_it_code ,
2469                                          struct mdt_thread_info *,
2470                                          struct ldlm_lock **,
2471                                          int);
2472         long                     it_reint;
2473 } mdt_it_flavor[] = {
2474         [MDT_IT_OPEN]     = {
2475                 .it_fmt   = &RQF_LDLM_INTENT,
2476                 /*.it_flags = HABEO_REFERO,*/
2477                 .it_flags = 0,
2478                 .it_act   = mdt_intent_reint,
2479                 .it_reint = REINT_OPEN
2480         },
2481         [MDT_IT_OCREAT]   = {
2482                 .it_fmt   = &RQF_LDLM_INTENT,
2483                 .it_flags = MUTABOR,
2484                 .it_act   = mdt_intent_reint,
2485                 .it_reint = REINT_OPEN
2486         },
2487         [MDT_IT_CREATE]   = {
2488                 .it_fmt   = &RQF_LDLM_INTENT,
2489                 .it_flags = MUTABOR,
2490                 .it_act   = mdt_intent_reint,
2491                 .it_reint = REINT_CREATE
2492         },
2493         [MDT_IT_GETATTR]  = {
2494                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2495                 .it_flags = HABEO_REFERO,
2496                 .it_act   = mdt_intent_getattr
2497         },
2498         [MDT_IT_READDIR]  = {
2499                 .it_fmt   = NULL,
2500                 .it_flags = 0,
2501                 .it_act   = NULL
2502         },
2503         [MDT_IT_LOOKUP]   = {
2504                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2505                 .it_flags = HABEO_REFERO,
2506                 .it_act   = mdt_intent_getattr
2507         },
2508         [MDT_IT_UNLINK]   = {
2509                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
2510                 .it_flags = MUTABOR,
2511                 .it_act   = NULL,
2512                 .it_reint = REINT_UNLINK
2513         },
2514         [MDT_IT_TRUNC]    = {
2515                 .it_fmt   = NULL,
2516                 .it_flags = MUTABOR,
2517                 .it_act   = NULL
2518         },
2519         [MDT_IT_GETXATTR] = {
2520                 .it_fmt   = NULL,
2521                 .it_flags = 0,
2522                 .it_act   = NULL
2523         }
2524 };
2525
2526 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2527                             struct ldlm_lock **lockp,
2528                             struct ldlm_lock *new_lock,
2529                             struct mdt_lock_handle *lh,
2530                             int flags)
2531 {
2532         struct ptlrpc_request  *req = mdt_info_req(info);
2533         struct ldlm_lock       *lock = *lockp;
2534
2535         /*
2536          * Get new lock only for cases when possible resent did not find any
2537          * lock.
2538          */
2539         if (new_lock == NULL)
2540                 new_lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2541
2542         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2543                 lh->mlh_reg_lh.cookie = 0;
2544                 RETURN(0);
2545         }
2546
2547         LASSERTF(new_lock != NULL,
2548                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2549
2550         /*
2551          * If we've already given this lock to a client once, then we should
2552          * have no readers or writers.  Otherwise, we should have one reader
2553          * _or_ writer ref (which will be zeroed below) before returning the
2554          * lock to a client.
2555          */
2556         if (new_lock->l_export == req->rq_export) {
2557                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2558         } else {
2559                 LASSERT(new_lock->l_export == NULL);
2560                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2561         }
2562
2563         *lockp = new_lock;
2564
2565         if (new_lock->l_export == req->rq_export) {
2566                 /*
2567                  * Already gave this to the client, which means that we
2568                  * reconstructed a reply.
2569                  */
2570                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2571                         MSG_RESENT);
2572                 lh->mlh_reg_lh.cookie = 0;
2573                 RETURN(ELDLM_LOCK_REPLACED);
2574         }
2575
2576         /* This lock might already be given to the client by an resent req,
2577          * in this case we should return ELDLM_LOCK_ABORTED,
2578          * so we should check led_held_locks here, but it will affect
2579          * performance, FIXME
2580          */
2581         /* Fixup the lock to be given to the client */
2582         lock_res_and_lock(new_lock);
2583         new_lock->l_readers = 0;
2584         new_lock->l_writers = 0;
2585
2586         new_lock->l_export = class_export_get(req->rq_export);
2587         spin_lock(&req->rq_export->exp_ldlm_data.led_lock);
2588         list_add(&new_lock->l_export_chain,
2589                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
2590         spin_unlock(&req->rq_export->exp_ldlm_data.led_lock);
2591
2592         new_lock->l_blocking_ast = lock->l_blocking_ast;
2593         new_lock->l_completion_ast = lock->l_completion_ast;
2594         new_lock->l_remote_handle = lock->l_remote_handle;
2595         new_lock->l_flags &= ~LDLM_FL_LOCAL;
2596
2597         unlock_res_and_lock(new_lock);
2598         LDLM_LOCK_PUT(new_lock);
2599         lh->mlh_reg_lh.cookie = 0;
2600
2601         RETURN(ELDLM_LOCK_REPLACED);
2602 }
2603
2604 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2605                                     struct ldlm_lock *new_lock,
2606                                     struct ldlm_lock **old_lock,
2607                                     struct mdt_lock_handle *lh)
2608 {
2609         struct ptlrpc_request  *req = mdt_info_req(info);
2610         struct obd_export      *exp = req->rq_export;
2611         struct lustre_handle    remote_hdl;
2612         struct ldlm_request    *dlmreq;
2613         struct list_head       *iter;
2614
2615         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2616                 return;
2617
2618         dlmreq = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
2619         remote_hdl = dlmreq->lock_handle[0];
2620
2621         spin_lock(&exp->exp_ldlm_data.led_lock);
2622         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2623                 struct ldlm_lock *lock;
2624                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2625                 if (lock == new_lock)
2626                         continue;
2627                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2628                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2629                         lh->mlh_reg_mode = lock->l_granted_mode;
2630
2631                         LDLM_DEBUG(lock, "restoring lock cookie");
2632                         DEBUG_REQ(D_DLMTRACE, req,
2633                                   "restoring lock cookie "LPX64,
2634                                   lh->mlh_reg_lh.cookie);
2635                         if (old_lock)
2636                                 *old_lock = LDLM_LOCK_GET(lock);
2637                         spin_unlock(&exp->exp_ldlm_data.led_lock);
2638                         return;
2639                 }
2640         }
2641         spin_unlock(&exp->exp_ldlm_data.led_lock);
2642
2643         /*
2644          * If the xid matches, then we know this is a resent request, and allow
2645          * it. (It's probably an OPEN, for which we don't send a lock.
2646          */
2647         if (req_xid_is_last(req))
2648                 return;
2649
2650         /*
2651          * This remote handle isn't enqueued, so we never received or processed
2652          * this request.  Clear MSG_RESENT, because it can be handled like any
2653          * normal request now.
2654          */
2655         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2656
2657         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2658                   remote_hdl.cookie);
2659 }
2660
2661 static int mdt_intent_getattr(enum mdt_it_code opcode,
2662                               struct mdt_thread_info *info,
2663                               struct ldlm_lock **lockp,
2664                               int flags)
2665 {
2666         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2667         struct ldlm_lock       *new_lock = NULL;
2668         __u64                   child_bits;
2669         struct ldlm_reply      *ldlm_rep;
2670         struct ptlrpc_request  *req;
2671         struct mdt_body        *reqbody;
2672         struct mdt_body        *repbody;
2673         int                     rc;
2674         ENTRY;
2675
2676         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
2677         LASSERT(reqbody);
2678
2679         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
2680         LASSERT(repbody);
2681
2682         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
2683         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
2684         repbody->eadatasize = 0;
2685         repbody->aclsize = 0;
2686
2687         switch (opcode) {
2688         case MDT_IT_LOOKUP:
2689                 child_bits = MDS_INODELOCK_LOOKUP;
2690                 break;
2691         case MDT_IT_GETATTR:
2692                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2693                 break;
2694         default:
2695                 CERROR("Unhandled till now");
2696                 GOTO(out, rc = -EINVAL);
2697         }
2698
2699         rc = mdt_init_ucred(info, reqbody);
2700         if (rc)
2701                 GOTO(out, rc);
2702
2703         req = info->mti_pill.rc_req;
2704         ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2705         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
2706
2707         /* Get lock from request for possible resent case. */
2708         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
2709
2710         ldlm_rep->lock_policy_res2 =
2711                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
2712
2713         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
2714                 ldlm_rep->lock_policy_res2 = 0;
2715         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
2716             ldlm_rep->lock_policy_res2) {
2717                 lhc->mlh_reg_lh.cookie = 0ull;
2718                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
2719         }
2720
2721         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
2722         EXIT;
2723 out_ucred:
2724         mdt_exit_ucred(info);
2725 out:
2726         mdt_shrink_reply(info);
2727         return rc;
2728 }
2729
2730 static int mdt_intent_reint(enum mdt_it_code opcode,
2731                             struct mdt_thread_info *info,
2732                             struct ldlm_lock **lockp,
2733                             int flags)
2734 {
2735         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2736         struct ldlm_reply      *rep = NULL;
2737         long                    opc;
2738         int                     rc;
2739
2740         static const struct req_format *intent_fmts[REINT_MAX] = {
2741                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
2742                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
2743         };
2744
2745         ENTRY;
2746
2747         opc = mdt_reint_opcode(info, intent_fmts);
2748         if (opc < 0)
2749                 GOTO(out, rc = opc);
2750
2751         if (mdt_it_flavor[opcode].it_reint != opc) {
2752                 CERROR("Reint code %ld doesn't match intent: %d\n",
2753                        opc, opcode);
2754                 GOTO(out, rc = err_serious(-EPROTO));
2755         }
2756
2757         /* Get lock from request for possible resent case. */
2758         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
2759
2760         rc = mdt_reint_internal(info, lhc, opc);
2761
2762         /* Check whether the reply has been packed successfully. */
2763         if (mdt_info_req(info)->rq_repmsg != NULL)
2764                 rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2765         if (rep == NULL)
2766                 GOTO(out, rc = err_serious(-EFAULT));
2767
2768         /* MDC expects this in any case */
2769         if (rc != 0)
2770                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
2771
2772         /* Cross-ref case, the lock should be returned to the client */
2773         if (rc == -EREMOTE) {
2774                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
2775                 rep->lock_policy_res2 = 0;
2776                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
2777                 GOTO(out, rc);
2778         }
2779         rep->lock_policy_res2 = clear_serious(rc);
2780
2781         lhc->mlh_reg_lh.cookie = 0ull;
2782         rc = ELDLM_LOCK_ABORTED;
2783         EXIT;
2784 out:
2785         return rc;
2786 }
2787
2788 static int mdt_intent_code(long itcode)
2789 {
2790         int rc;
2791
2792         switch(itcode) {
2793         case IT_OPEN:
2794                 rc = MDT_IT_OPEN;
2795                 break;
2796         case IT_OPEN|IT_CREAT:
2797                 rc = MDT_IT_OCREAT;
2798                 break;
2799         case IT_CREAT:
2800                 rc = MDT_IT_CREATE;
2801                 break;
2802         case IT_READDIR:
2803                 rc = MDT_IT_READDIR;
2804                 break;
2805         case IT_GETATTR:
2806                 rc = MDT_IT_GETATTR;
2807                 break;
2808         case IT_LOOKUP:
2809                 rc = MDT_IT_LOOKUP;
2810                 break;
2811         case IT_UNLINK:
2812                 rc = MDT_IT_UNLINK;
2813                 break;
2814         case IT_TRUNC:
2815                 rc = MDT_IT_TRUNC;
2816                 break;
2817         case IT_GETXATTR:
2818                 rc = MDT_IT_GETXATTR;
2819                 break;
2820         default:
2821                 CERROR("Unknown intent opcode: %ld\n", itcode);
2822                 rc = -EINVAL;
2823                 break;
2824         }
2825         return rc;
2826 }
2827
2828 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
2829                           struct ldlm_lock **lockp, int flags)
2830 {
2831         struct req_capsule   *pill;
2832         struct mdt_it_flavor *flv;
2833         int opc;
2834         int rc;
2835         ENTRY;
2836
2837         opc = mdt_intent_code(itopc);
2838         if (opc < 0)
2839                 RETURN(-EINVAL);
2840
2841         pill = &info->mti_pill;
2842         flv  = &mdt_it_flavor[opc];
2843
2844         if (flv->it_fmt != NULL)
2845                 req_capsule_extend(pill, flv->it_fmt);
2846
2847         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
2848         if (rc == 0) {
2849                 struct ptlrpc_request *req = mdt_info_req(info);
2850                 if (flv->it_flags & MUTABOR &&
2851                     req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2852                         rc = -EROFS;
2853         }
2854         if (rc == 0 && flv->it_act != NULL) {
2855                 /* execute policy */
2856                 rc = flv->it_act(opc, info, lockp, flags);
2857         } else
2858                 rc = -EOPNOTSUPP;
2859         RETURN(rc);
2860 }
2861
2862 static int mdt_intent_policy(struct ldlm_namespace *ns,
2863                              struct ldlm_lock **lockp, void *req_cookie,
2864                              ldlm_mode_t mode, int flags, void *data)
2865 {
2866         struct mdt_thread_info *info;
2867         struct ptlrpc_request  *req  =  req_cookie;
2868         struct ldlm_intent     *it;
2869         struct req_capsule     *pill;
2870         int rc;
2871
2872         ENTRY;
2873
2874         LASSERT(req != NULL);
2875
2876         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
2877                                   &mdt_thread_key);
2878         LASSERT(info != NULL);
2879         pill = &info->mti_pill;
2880         LASSERT(pill->rc_req == req);
2881
2882         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
2883                 req_capsule_extend(pill, &RQF_LDLM_INTENT);
2884                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
2885                 if (it != NULL) {
2886                         const struct ldlm_request *dlmreq;
2887                         __u64 req_bits;
2888 #if 0
2889                         struct ldlm_lock       *lock = *lockp;
2890
2891                         LDLM_DEBUG(lock, "intent policy opc: %s\n",
2892                                    ldlm_it2str(it->opc));
2893 #endif
2894
2895                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
2896                         if (rc == 0)
2897                                 rc = ELDLM_OK;
2898
2899                         /*
2900                          * Lock without inodebits makes no sense and will oops
2901                          * later in ldlm. Let's check it now to see if we have
2902                          * wrong lock from client or bits get corrupted
2903                          * somewhere in mdt_intent_opc().
2904                          */
2905                         dlmreq = info->mti_dlm_req;
2906                         req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
2907                         LASSERT(req_bits != 0);
2908
2909                 } else
2910                         rc = err_serious(-EFAULT);
2911         } else {
2912                 /* No intent was provided */
2913                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
2914                 rc = req_capsule_pack(pill);
2915                 if (rc)
2916                         rc = err_serious(rc);
2917         }
2918         RETURN(rc);
2919 }
2920
2921 /*
2922  * Seq wrappers
2923  */
2924 static void mdt_seq_adjust(const struct lu_env *env,
2925                           struct mdt_device *m, int lost)
2926 {
2927         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2928         struct lu_range out;
2929         ENTRY;
2930
2931         LASSERT(ls && ls->ls_server_seq);
2932         LASSERT(lost >= 0);
2933         /* get extra seq from seq_server, moving it's range up */
2934         while (lost-- > 0) {
2935                 seq_server_alloc_meta(ls->ls_server_seq, NULL, &out, env);
2936         }
2937         EXIT;
2938 }
2939
2940 static int mdt_seq_fini(const struct lu_env *env,
2941                         struct mdt_device *m)
2942 {
2943         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2944         ENTRY;
2945
2946         if (ls && ls->ls_server_seq) {
2947                 seq_server_fini(ls->ls_server_seq, env);
2948                 OBD_FREE_PTR(ls->ls_server_seq);
2949                 ls->ls_server_seq = NULL;
2950         }
2951
2952         if (ls && ls->ls_control_seq) {
2953                 seq_server_fini(ls->ls_control_seq, env);
2954                 OBD_FREE_PTR(ls->ls_control_seq);
2955                 ls->ls_control_seq = NULL;
2956         }
2957
2958         if (ls && ls->ls_client_seq) {
2959                 seq_client_fini(ls->ls_client_seq);
2960                 OBD_FREE_PTR(ls->ls_client_seq);
2961                 ls->ls_client_seq = NULL;
2962         }
2963
2964         RETURN(0);
2965 }
2966
2967 static int mdt_seq_init(const struct lu_env *env,
2968                         const char *uuid,
2969                         struct mdt_device *m)
2970 {
2971         struct lu_site *ls;
2972         char *prefix;
2973         int rc;
2974         ENTRY;
2975
2976         ls = m->mdt_md_dev.md_lu_dev.ld_site;
2977
2978         /*
2979          * This is sequence-controller node. Init seq-controller server on local
2980          * MDT.
2981          */
2982         if (ls->ls_node_id == 0) {
2983                 LASSERT(ls->ls_control_seq == NULL);
2984
2985                 OBD_ALLOC_PTR(ls->ls_control_seq);
2986                 if (ls->ls_control_seq == NULL)
2987                         RETURN(-ENOMEM);
2988
2989                 rc = seq_server_init(ls->ls_control_seq,
2990                                      m->mdt_bottom, uuid,
2991                                      LUSTRE_SEQ_CONTROLLER,
2992                                      env);
2993
2994                 if (rc)
2995                         GOTO(out_seq_fini, rc);
2996
2997                 OBD_ALLOC_PTR(ls->ls_client_seq);
2998                 if (ls->ls_client_seq == NULL)
2999                         GOTO(out_seq_fini, rc = -ENOMEM);
3000
3001                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3002                 if (prefix == NULL) {
3003                         OBD_FREE_PTR(ls->ls_client_seq);
3004                         GOTO(out_seq_fini, rc = -ENOMEM);
3005                 }
3006
3007                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3008                          uuid);
3009
3010                 /*
3011                  * Init seq-controller client after seq-controller server is
3012                  * ready. Pass ls->ls_control_seq to it for direct talking.
3013                  */
3014                 rc = seq_client_init(ls->ls_client_seq, NULL,
3015                                      LUSTRE_SEQ_METADATA, prefix,
3016                                      ls->ls_control_seq);
3017                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3018
3019                 if (rc)
3020                         GOTO(out_seq_fini, rc);
3021         }
3022
3023         /* Init seq-server on local MDT */
3024         LASSERT(ls->ls_server_seq == NULL);
3025
3026         OBD_ALLOC_PTR(ls->ls_server_seq);
3027         if (ls->ls_server_seq == NULL)
3028                 GOTO(out_seq_fini, rc = -ENOMEM);
3029
3030         rc = seq_server_init(ls->ls_server_seq,
3031                              m->mdt_bottom, uuid,
3032                              LUSTRE_SEQ_SERVER,
3033                              env);
3034         if (rc)
3035                 GOTO(out_seq_fini, rc = -ENOMEM);
3036
3037         /* Assign seq-controller client to local seq-server. */
3038         if (ls->ls_node_id == 0) {
3039                 LASSERT(ls->ls_client_seq != NULL);
3040
3041                 rc = seq_server_set_cli(ls->ls_server_seq,
3042                                         ls->ls_client_seq,
3043                                         env);
3044         }
3045
3046         EXIT;
3047 out_seq_fini:
3048         if (rc)
3049                 mdt_seq_fini(env, m);
3050
3051         return rc;
3052 }
3053 /*
3054  * Init client sequence manager which is used by local MDS to talk to sequence
3055  * controller on remote node.
3056  */
3057 static int mdt_seq_init_cli(const struct lu_env *env,
3058                             struct mdt_device *m,
3059                             struct lustre_cfg *cfg)
3060 {
3061         struct lu_site    *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3062         struct obd_device *mdc;
3063         struct obd_uuid   *uuidp, *mdcuuidp;
3064         char              *uuid_str, *mdc_uuid_str;
3065         int                rc;
3066         int                index;
3067         struct mdt_thread_info *info;
3068         char *p, *index_string = lustre_cfg_string(cfg, 2);
3069         ENTRY;
3070
3071         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3072         uuidp = &info->mti_u.uuid[0];
3073         mdcuuidp = &info->mti_u.uuid[1];
3074
3075         LASSERT(index_string);
3076
3077         index = simple_strtol(index_string, &p, 10);
3078         if (*p) {
3079                 CERROR("Invalid index in lustre_cgf, offset 2\n");
3080                 RETURN(-EINVAL);
3081         }
3082
3083         /* check if this is adding the first MDC and controller is not yet
3084          * initialized. */
3085         if (index != 0 || ls->ls_client_seq)
3086                 RETURN(0);
3087
3088         uuid_str = lustre_cfg_string(cfg, 1);
3089         mdc_uuid_str = lustre_cfg_string(cfg, 4);
3090         obd_str2uuid(uuidp, uuid_str);
3091         obd_str2uuid(mdcuuidp, mdc_uuid_str);
3092
3093         mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3094         if (!mdc) {
3095                 CERROR("can't find controller MDC by uuid %s\n",
3096                        uuid_str);
3097                 rc = -ENOENT;
3098         } else if (!mdc->obd_set_up) {
3099                 CERROR("target %s not set up\n", mdc->obd_name);
3100                 rc = -EINVAL;
3101         } else {
3102                 LASSERT(ls->ls_control_exp);
3103                 OBD_ALLOC_PTR(ls->ls_client_seq);
3104                 if (ls->ls_client_seq != NULL) {
3105                         char *prefix;
3106
3107                         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3108                         if (!prefix)
3109                                 RETURN(-ENOMEM);
3110
3111                         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3112                                  mdc->obd_name);
3113
3114                         rc = seq_client_init(ls->ls_client_seq,
3115                                              ls->ls_control_exp,
3116                                              LUSTRE_SEQ_METADATA,
3117                                              prefix, NULL);
3118                         OBD_FREE(prefix, MAX_OBD_NAME + 5);
3119                 } else
3120                         rc = -ENOMEM;
3121
3122                 if (rc)
3123                         RETURN(rc);
3124
3125                 LASSERT(ls->ls_server_seq != NULL);
3126                 rc = seq_server_set_cli(ls->ls_server_seq, ls->ls_client_seq,
3127                                         env);
3128         }
3129
3130         RETURN(rc);
3131 }
3132
3133 static void mdt_seq_fini_cli(struct mdt_device *m)
3134 {
3135         struct lu_site *ls;
3136
3137         ENTRY;
3138
3139         ls = m->mdt_md_dev.md_lu_dev.ld_site;
3140
3141         if (ls && ls->ls_server_seq)
3142                 seq_server_set_cli(ls->ls_server_seq,
3143                                    NULL, NULL);
3144
3145         if (ls && ls->ls_control_exp) {
3146                 class_export_put(ls->ls_control_exp);
3147                 ls->ls_control_exp = NULL;
3148         }
3149         EXIT;
3150 }
3151
3152 /*
3153  * FLD wrappers
3154  */
3155 static int mdt_fld_fini(const struct lu_env *env,
3156                         struct mdt_device *m)
3157 {
3158         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3159         ENTRY;
3160
3161         if (ls && ls->ls_server_fld) {
3162                 fld_server_fini(ls->ls_server_fld, env);
3163                 OBD_FREE_PTR(ls->ls_server_fld);
3164                 ls->ls_server_fld = NULL;
3165         }
3166
3167         RETURN(0);
3168 }
3169
3170 static int mdt_fld_init(const struct lu_env *env,
3171                         const char *uuid,
3172                         struct mdt_device *m)
3173 {
3174         struct lu_site *ls;
3175         int rc;
3176         ENTRY;
3177
3178         ls = m->mdt_md_dev.md_lu_dev.ld_site;
3179
3180         OBD_ALLOC_PTR(ls->ls_server_fld);
3181         if (ls->ls_server_fld == NULL)
3182                 RETURN(rc = -ENOMEM);
3183
3184         rc = fld_server_init(ls->ls_server_fld,
3185                              m->mdt_bottom, uuid, env);
3186         if (rc) {
3187                 OBD_FREE_PTR(ls->ls_server_fld);
3188                 ls->ls_server_fld = NULL;
3189                 RETURN(rc);
3190         }
3191
3192         RETURN(0);
3193 }
3194
3195 /* device init/fini methods */
3196 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3197 {
3198         ENTRY;
3199         if (m->mdt_regular_service != NULL) {
3200                 ptlrpc_unregister_service(m->mdt_regular_service);
3201                 m->mdt_regular_service = NULL;
3202         }
3203         if (m->mdt_readpage_service != NULL) {
3204                 ptlrpc_unregister_service(m->mdt_readpage_service);
3205                 m->mdt_readpage_service = NULL;
3206         }
3207         if (m->mdt_xmds_service != NULL) {
3208                 ptlrpc_unregister_service(m->mdt_xmds_service);
3209                 m->mdt_xmds_service = NULL;
3210         }
3211         if (m->mdt_setattr_service != NULL) {
3212                 ptlrpc_unregister_service(m->mdt_setattr_service);
3213                 m->mdt_setattr_service = NULL;
3214         }
3215         if (m->mdt_mdsc_service != NULL) {
3216                 ptlrpc_unregister_service(m->mdt_mdsc_service);
3217                 m->mdt_mdsc_service = NULL;
3218         }
3219         if (m->mdt_mdss_service != NULL) {
3220                 ptlrpc_unregister_service(m->mdt_mdss_service);
3221                 m->mdt_mdss_service = NULL;
3222         }
3223         if (m->mdt_dtss_service != NULL) {
3224                 ptlrpc_unregister_service(m->mdt_dtss_service);
3225                 m->mdt_dtss_service = NULL;
3226         }
3227         if (m->mdt_fld_service != NULL) {
3228                 ptlrpc_unregister_service(m->mdt_fld_service);
3229                 m->mdt_fld_service = NULL;
3230         }
3231         ENTRY;
3232 }
3233
3234 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3235 {
3236         int rc;
3237         static struct ptlrpc_service_conf conf;
3238         cfs_proc_dir_entry_t *procfs_entry;
3239         ENTRY;
3240
3241         procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3242
3243         conf = (typeof(conf)) {
3244                 .psc_nbufs            = MDS_NBUFS,
3245                 .psc_bufsize          = MDS_BUFSIZE,
3246                 .psc_max_req_size     = MDS_MAXREQSIZE,
3247                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3248                 .psc_req_portal       = MDS_REQUEST_PORTAL,
3249                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3250                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3251                 /*
3252                  * We'd like to have a mechanism to set this on a per-device
3253                  * basis, but alas...
3254                  */
3255                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3256                                        MDT_MAX_THREADS),
3257                 .psc_max_threads   = MDT_MAX_THREADS,
32