Whamcloud - gitweb
206b5bde87fd199a8625e860d5918f5d923264e9
[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         rc = mdt_check_ucred(info);
1351         if (rc)
1352                 RETURN(err_serious(rc));
1353
1354         /*
1355          * prepare @rdpg before calling lower layers and transfer itself. Here
1356          * reqbody->size contains offset of where to start to read and
1357          * reqbody->nlink contains number bytes to read.
1358          */
1359         rdpg->rp_hash = reqbody->size;
1360         if ((__u64)rdpg->rp_hash != reqbody->size) {
1361                 CERROR("Invalid hash: %#llx != %#llx\n",
1362                        (__u64)rdpg->rp_hash, reqbody->size);
1363                 RETURN(-EFAULT);
1364         }
1365         rdpg->rp_count  = reqbody->nlink;
1366         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1367         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1368         if (rdpg->rp_pages == NULL)
1369                 RETURN(-ENOMEM);
1370
1371         for (i = 0; i < rdpg->rp_npages; ++i) {
1372                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1373                 if (rdpg->rp_pages[i] == NULL)
1374                         GOTO(free_rdpg, rc = -ENOMEM);
1375         }
1376
1377         /* call lower layers to fill allocated pages with directory data */
1378         rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1379         if (rc)
1380                 GOTO(free_rdpg, rc);
1381
1382         /* send pages to client */
1383         rc = mdt_sendpage(info, rdpg);
1384
1385         EXIT;
1386 free_rdpg:
1387
1388         for (i = 0; i < rdpg->rp_npages; i++)
1389                 if (rdpg->rp_pages[i] != NULL)
1390                         __cfs_free_page(rdpg->rp_pages[i]);
1391         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1392
1393         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1394                 RETURN(0);
1395
1396         return rc;
1397 }
1398
1399 static int mdt_reint_internal(struct mdt_thread_info *info,
1400                               struct mdt_lock_handle *lhc,
1401                               __u32 op)
1402 {
1403         struct req_capsule      *pill = &info->mti_pill;
1404         struct mdt_device       *mdt = info->mti_mdt;
1405         struct mdt_body         *repbody;
1406         int                      need_shrink = 0;
1407         int                      rc;
1408         ENTRY;
1409
1410         /* pack reply */
1411         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
1412                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1413                                      mdt->mdt_max_mdsize);
1414                 need_shrink = 1;
1415         }
1416         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER)) {
1417                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1418                                      mdt->mdt_max_cookiesize);
1419                 need_shrink = 1;
1420         }
1421         rc = req_capsule_pack(pill);
1422         if (rc != 0) {
1423                 CERROR("Can't pack response, rc %d\n", rc);
1424                 RETURN(err_serious(rc));
1425         }
1426
1427         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1428                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1429                 LASSERT(repbody);
1430                 repbody->eadatasize = 0;
1431                 repbody->aclsize = 0;
1432         }
1433
1434         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1435                 GOTO(out_shrink, rc = err_serious(-EFAULT));
1436
1437         rc = mdt_reint_unpack(info, op);
1438         if (rc != 0) {
1439                 CERROR("Can't unpack reint, rc %d\n", rc);
1440                 GOTO(out_shrink, rc = err_serious(rc));
1441         }
1442
1443         rc = mdt_init_ucred_reint(info);
1444         if (rc)
1445                 GOTO(out_shrink, rc);
1446
1447         rc = mdt_fix_attr_ucred(info, op);
1448         if (rc != 0)
1449                 GOTO(out_ucred, rc = err_serious(rc));
1450
1451         need_shrink = 0;
1452         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1453                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1454                 GOTO(out_ucred, rc);
1455         }
1456         
1457         rc = mdt_reint_rec(info, lhc);
1458         EXIT;
1459 out_ucred:
1460         mdt_exit_ucred(info);
1461 out_shrink:
1462         if (need_shrink)
1463                 mdt_shrink_reply(info);
1464         return rc;
1465 }
1466
1467 static long mdt_reint_opcode(struct mdt_thread_info *info,
1468                              const struct req_format **fmt)
1469 {
1470         __u32 *ptr;
1471         long opc;
1472
1473         opc = err_serious(-EFAULT);
1474         ptr = req_capsule_client_get(&info->mti_pill, &RMF_REINT_OPC);
1475         if (ptr != NULL) {
1476                 opc = *ptr;
1477                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1478                 if (opc < REINT_MAX && fmt[opc] != NULL)
1479                         req_capsule_extend(&info->mti_pill, fmt[opc]);
1480                 else {
1481                         CERROR("Unsupported opc: %ld\n", opc);
1482                         opc = err_serious(opc);
1483                 }
1484         }
1485         return opc;
1486 }
1487
1488 static int mdt_reint(struct mdt_thread_info *info)
1489 {
1490         long opc;
1491         int  rc;
1492
1493         static const struct req_format *reint_fmts[REINT_MAX] = {
1494                 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1495                 [REINT_CREATE]  = &RQF_MDS_REINT_CREATE,
1496                 [REINT_LINK]    = &RQF_MDS_REINT_LINK,
1497                 [REINT_UNLINK]  = &RQF_MDS_REINT_UNLINK,
1498                 [REINT_RENAME]  = &RQF_MDS_REINT_RENAME,
1499                 [REINT_OPEN]    = &RQF_MDS_REINT_OPEN
1500         };
1501
1502         ENTRY;
1503
1504         opc = mdt_reint_opcode(info, reint_fmts);
1505         if (opc >= 0) {
1506                 /*
1507                  * No lock possible here from client to pass it to reint code
1508                  * path.
1509                  */
1510                 rc = mdt_reint_internal(info, NULL, opc);
1511         } else {
1512                 rc = opc;
1513         }
1514
1515         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1516         RETURN(rc);
1517 }
1518
1519 /* TODO these two methods not available now. */
1520
1521 /* this should sync the whole device */
1522 static int mdt_device_sync(struct mdt_thread_info *info)
1523 {
1524         return 0;
1525 }
1526
1527 /* this should sync this object */
1528 static int mdt_object_sync(struct mdt_thread_info *info)
1529 {
1530         return 0;
1531 }
1532
1533 static int mdt_sync(struct mdt_thread_info *info)
1534 {
1535         struct req_capsule *pill = &info->mti_pill;
1536         struct mdt_body *body;
1537         int rc;
1538         ENTRY;
1539
1540         /* The fid may be zero, so we req_capsule_set manually */
1541         req_capsule_set(pill, &RQF_MDS_SYNC);
1542
1543         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1544         if (body == NULL)
1545                 RETURN(err_serious(-EINVAL));
1546
1547         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1548                 RETURN(err_serious(-ENOMEM));
1549
1550         if (fid_seq(&body->fid1) == 0) {
1551                 /* sync the whole device */
1552                 rc = req_capsule_pack(pill);
1553                 if (rc == 0)
1554                         rc = mdt_device_sync(info);
1555                 else
1556                         rc = err_serious(rc);
1557         } else {
1558                 /* sync an object */
1559                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1560                 if (rc == 0) {
1561                         rc = mdt_object_sync(info);
1562                         if (rc == 0) {
1563                                 struct md_object *next;
1564                                 const struct lu_fid *fid;
1565                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1566
1567                                 next = mdt_object_child(info->mti_object);
1568                                 info->mti_attr.ma_need = MA_INODE;
1569                                 info->mti_attr.ma_valid = 0;
1570                                 rc = mo_attr_get(info->mti_env, next,
1571                                                  &info->mti_attr);
1572                                 if (rc == 0) {
1573                                         body = req_capsule_server_get(pill,
1574                                                                 &RMF_MDT_BODY);
1575                                         fid = mdt_object_fid(info->mti_object);
1576                                         mdt_pack_attr2body(info, body, la, fid);
1577                                 }
1578                         }
1579                 } else
1580                         rc = err_serious(rc);
1581         }
1582         RETURN(rc);
1583 }
1584
1585 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1586 {
1587         return err_serious(-EOPNOTSUPP);
1588 }
1589
1590 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1591 {
1592         return err_serious(-EOPNOTSUPP);
1593 }
1594
1595 /*
1596  * OBD PING and other handlers.
1597  */
1598 static int mdt_obd_ping(struct mdt_thread_info *info)
1599 {
1600         int rc;
1601         ENTRY;
1602         rc = target_handle_ping(mdt_info_req(info));
1603         if (rc < 0)
1604                 rc = err_serious(rc);
1605         RETURN(rc);
1606 }
1607
1608 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1609 {
1610         return err_serious(-EOPNOTSUPP);
1611 }
1612
1613 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1614 {
1615         return err_serious(-EOPNOTSUPP);
1616 }
1617
1618
1619 /*
1620  * DLM handlers.
1621  */
1622 static struct ldlm_callback_suite cbs = {
1623         .lcs_completion = ldlm_server_completion_ast,
1624         .lcs_blocking   = ldlm_server_blocking_ast,
1625         .lcs_glimpse    = NULL
1626 };
1627
1628 static int mdt_enqueue(struct mdt_thread_info *info)
1629 {
1630         struct ptlrpc_request *req;
1631         __u64 req_bits;
1632         int rc;
1633
1634         /*
1635          * info->mti_dlm_req already contains swapped and (if necessary)
1636          * converted dlm request.
1637          */
1638         LASSERT(info->mti_dlm_req != NULL);
1639
1640         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE)) {
1641                 info->mti_fail_id = OBD_FAIL_LDLM_ENQUEUE;
1642                 return 0;
1643         }
1644
1645         req = mdt_info_req(info);
1646
1647         /*
1648          * Lock without inodebits makes no sense and will oops later in
1649          * ldlm. Let's check it now to see if we have wrong lock from client or
1650          * bits get corrupted somewhere in mdt_intent_policy().
1651          */
1652         req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1653         /* This is disabled because we need to support liblustre flock.
1654          * LASSERT(req_bits != 0);
1655          */
1656
1657         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1658                                   req, info->mti_dlm_req, &cbs);
1659         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1660         return rc ? err_serious(rc) : req->rq_status;
1661 }
1662
1663 static int mdt_convert(struct mdt_thread_info *info)
1664 {
1665         int rc;
1666         struct ptlrpc_request *req;
1667
1668         LASSERT(info->mti_dlm_req);
1669         req = mdt_info_req(info);
1670         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1671         return rc ? err_serious(rc) : req->rq_status;
1672 }
1673
1674 static int mdt_bl_callback(struct mdt_thread_info *info)
1675 {
1676         CERROR("bl callbacks should not happen on MDS\n");
1677         LBUG();
1678         return err_serious(-EOPNOTSUPP);
1679 }
1680
1681 static int mdt_cp_callback(struct mdt_thread_info *info)
1682 {
1683         CERROR("cp callbacks should not happen on MDS\n");
1684         LBUG();
1685         return err_serious(-EOPNOTSUPP);
1686 }
1687
1688 /*
1689  * sec context handlers
1690  */
1691 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
1692 {
1693         int rc;
1694
1695         rc = mdt_handle_idmap(info);
1696
1697         if (unlikely(rc)) {
1698                 struct ptlrpc_request *req = mdt_info_req(info);
1699                 __u32                  opc;
1700
1701                 opc = lustre_msg_get_opc(req->rq_reqmsg);
1702                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
1703                         sptlrpc_svc_ctx_invalidate(req);
1704         }
1705
1706         return rc;
1707 }
1708
1709 static struct mdt_object *mdt_obj(struct lu_object *o)
1710 {
1711         LASSERT(lu_device_is_mdt(o->lo_dev));
1712         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
1713 }
1714
1715 struct mdt_object *mdt_object_find(const struct lu_env *env,
1716                                    struct mdt_device *d,
1717                                    const struct lu_fid *f)
1718 {
1719         struct lu_object *o;
1720         struct mdt_object *m;
1721         ENTRY;
1722
1723         o = lu_object_find(env, d->mdt_md_dev.md_lu_dev.ld_site, f);
1724         if (unlikely(IS_ERR(o)))
1725                 m = (struct mdt_object *)o;
1726         else
1727                 m = mdt_obj(o);
1728         RETURN(m);
1729 }
1730
1731 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
1732                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
1733 {
1734         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1735         ldlm_policy_data_t *policy = &info->mti_policy;
1736         struct ldlm_res_id *res_id = &info->mti_res_id;
1737         int rc;
1738         ENTRY;
1739
1740         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1741         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1742         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
1743         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
1744
1745         if (mdt_object_exists(o) < 0) {
1746                 if (locality == MDT_CROSS_LOCK) {
1747                         /* cross-ref object fix */
1748                         ibits &= ~MDS_INODELOCK_UPDATE;
1749                         ibits |= MDS_INODELOCK_LOOKUP;
1750                 } else {
1751                         LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
1752                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
1753                 }
1754                 /* No PDO lock on remote object */
1755                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
1756         }
1757
1758         memset(policy, 0, sizeof(*policy));
1759         fid_build_reg_res_name(mdt_object_fid(o), res_id);
1760
1761         /*
1762          * Take PDO lock on whole directory and build correct @res_id for lock
1763          * on part of directory.
1764          */
1765         if (lh->mlh_pdo_hash != 0) {
1766                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
1767                 mdt_lock_pdo_mode(info, o, lh);
1768                 if (lh->mlh_pdo_mode != LCK_NL) {
1769                         /*
1770                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
1771                          * is never going to be sent to client and we do not
1772                          * want it slowed down due to possible cancels.
1773                          */
1774                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1775                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
1776                                           policy, res_id, LDLM_FL_ATOMIC_CB);
1777                         if (unlikely(rc))
1778                                 RETURN(rc);
1779                 }
1780
1781                 /*
1782                  * Finish res_id initializing by name hash marking patr of
1783                  * directory which is taking modification.
1784                  */
1785                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
1786         }
1787
1788         policy->l_inodebits.bits = ibits;
1789
1790         /*
1791          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1792          * going to be sent to client. If it is - mdt_intent_policy() path will
1793          * fix it up and turns FL_LOCAL flag off.
1794          */
1795         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1796                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB);
1797
1798         if (rc)
1799                 GOTO(out, rc);
1800
1801         if (lh->mlh_type == MDT_PDO_LOCK) {
1802                 /* check for exists after object is locked */
1803                 if (mdt_object_exists(o) == 0) {
1804                         /* Non-existent object shouldn't have PDO lock */
1805                         rc = -ESTALE;
1806                 } else {
1807                         /* Non-dir object shouldn't have PDO lock */
1808                         LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
1809                 }
1810         }
1811 out:
1812         if (rc)
1813                 mdt_object_unlock(info, o, lh, 1);
1814
1815
1816         RETURN(rc);
1817 }
1818
1819 /*
1820  * Just call ldlm_lock_decref() if decref, else we only call ptlrpc_save_lock()
1821  * to save this lock in req.  when transaction committed, req will be released,
1822  * and lock will, too.
1823  */
1824 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
1825                        struct mdt_lock_handle *lh, int decref)
1826 {
1827         struct ptlrpc_request *req = mdt_info_req(info);
1828         ENTRY;
1829
1830         if (lustre_handle_is_used(&lh->mlh_pdo_lh)) {
1831                 /* Do not save PDO locks to request, just decref. */
1832                 mdt_fid_unlock(&lh->mlh_pdo_lh,
1833                                lh->mlh_pdo_mode);
1834                 lh->mlh_pdo_lh.cookie = 0ull;
1835         }
1836
1837         if (lustre_handle_is_used(&lh->mlh_reg_lh)) {
1838                 if (decref) {
1839                         mdt_fid_unlock(&lh->mlh_reg_lh,
1840                                        lh->mlh_reg_mode);
1841                 } else {
1842                         ptlrpc_save_lock(req, &lh->mlh_reg_lh,
1843                                          lh->mlh_reg_mode);
1844                 }
1845                 lh->mlh_reg_lh.cookie = 0ull;
1846         }
1847
1848         EXIT;
1849 }
1850
1851 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
1852                                         const struct lu_fid *f,
1853                                         struct mdt_lock_handle *lh,
1854                                         __u64 ibits)
1855 {
1856         struct mdt_object *o;
1857
1858         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
1859         if (!IS_ERR(o)) {
1860                 int rc;
1861
1862                 rc = mdt_object_lock(info, o, lh, ibits,
1863                                      MDT_LOCAL_LOCK);
1864                 if (rc != 0) {
1865                         mdt_object_put(info->mti_env, o);
1866                         o = ERR_PTR(rc);
1867                 }
1868         }
1869         return o;
1870 }
1871
1872 void mdt_object_unlock_put(struct mdt_thread_info * info,
1873                            struct mdt_object * o,
1874                            struct mdt_lock_handle *lh,
1875                            int decref)
1876 {
1877         mdt_object_unlock(info, o, lh, decref);
1878         mdt_object_put(info->mti_env, o);
1879 }
1880
1881 static struct mdt_handler *mdt_handler_find(__u32 opc,
1882                                             struct mdt_opc_slice *supported)
1883 {
1884         struct mdt_opc_slice *s;
1885         struct mdt_handler   *h;
1886
1887         h = NULL;
1888         for (s = supported; s->mos_hs != NULL; s++) {
1889                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
1890                         h = s->mos_hs + (opc - s->mos_opc_start);
1891                         if (likely(h->mh_opc != 0))
1892                                 LASSERT(h->mh_opc == opc);
1893                         else
1894                                 h = NULL; /* unsupported opc */
1895                         break;
1896                 }
1897         }
1898         return h;
1899 }
1900
1901 static int mdt_lock_resname_compat(struct mdt_device *m,
1902                                    struct ldlm_request *req)
1903 {
1904         /* XXX something... later. */
1905         return 0;
1906 }
1907
1908 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
1909 {
1910         /* XXX something... later. */
1911         return 0;
1912 }
1913
1914 /*
1915  * Generic code handling requests that have struct mdt_body passed in:
1916  *
1917  *  - extract mdt_body from request and save it in @info, if present;
1918  *
1919  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
1920  *  @info;
1921  *
1922  *  - if HABEO_CORPUS flag is set for this request type check whether object
1923  *  actually exists on storage (lu_object_exists()).
1924  *
1925  */
1926 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
1927 {
1928         const struct mdt_body    *body;
1929         struct mdt_object        *obj;
1930         const struct lu_env      *env;
1931         struct req_capsule       *pill;
1932         int                       rc;
1933
1934         env = info->mti_env;
1935         pill = &info->mti_pill;
1936
1937         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1938         if (body == NULL)
1939                 return -EFAULT;
1940
1941         if (!fid_is_sane(&body->fid1)) {
1942                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
1943                 return -EINVAL;
1944         }
1945
1946         /*
1947          * Do not get size or any capa fields before we check that request
1948          * contains capa actually. There are some requests which do not, for
1949          * instance MDS_IS_SUBDIR.
1950          */
1951         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
1952             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1953                 mdt_set_capainfo(info, 0, &body->fid1,
1954                                  req_capsule_client_get(pill, &RMF_CAPA1));
1955
1956         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
1957         if (!IS_ERR(obj)) {
1958                 if ((flags & HABEO_CORPUS) &&
1959                     !mdt_object_exists(obj)) {
1960                         mdt_object_put(env, obj);
1961                         /* for capability renew ENOENT will be handled in
1962                          * mdt_renew_capa */
1963                         if (body->valid & OBD_MD_FLOSSCAPA)
1964                                 rc = 0;
1965                         else
1966                                 rc = -ENOENT;
1967                 } else {
1968                         info->mti_object = obj;
1969                         rc = 0;
1970                 }
1971         } else
1972                 rc = PTR_ERR(obj);
1973
1974         return rc;
1975 }
1976
1977 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
1978 {
1979         struct req_capsule *pill;
1980         int rc;
1981
1982         ENTRY;
1983         pill = &info->mti_pill;
1984
1985         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
1986                 rc = mdt_body_unpack(info, flags);
1987         else
1988                 rc = 0;
1989
1990         if (rc == 0 && (flags & HABEO_REFERO)) {
1991                 struct mdt_device *mdt = info->mti_mdt;
1992
1993                 /* Pack reply. */
1994                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1995                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1996                                              mdt->mdt_max_mdsize);
1997                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1998                         req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1999                                              mdt->mdt_max_cookiesize);
2000
2001                 rc = req_capsule_pack(pill);
2002         }
2003         RETURN(rc);
2004 }
2005
2006 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2007 {
2008         struct md_device *next = m->mdt_child;
2009
2010         return next->md_ops->mdo_init_capa_ctxt(env, next,
2011                                                 m->mdt_opts.mo_mds_capa,
2012                                                 m->mdt_capa_timeout,
2013                                                 m->mdt_capa_alg,
2014                                                 m->mdt_capa_keys);
2015 }
2016
2017 /*
2018  * Invoke handler for this request opc. Also do necessary preprocessing
2019  * (according to handler ->mh_flags), and post-processing (setting of
2020  * ->last_{xid,committed}).
2021  */
2022 static int mdt_req_handle(struct mdt_thread_info *info,
2023                           struct mdt_handler *h, struct ptlrpc_request *req)
2024 {
2025         int   rc, serious = 0;
2026         __u32 flags;
2027
2028         ENTRY;
2029
2030         LASSERT(h->mh_act != NULL);
2031         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2032         LASSERT(current->journal_info == NULL);
2033
2034         /*
2035          * Mask out OBD_FAIL_ONCE, because that will stop
2036          * correct handling of failed req later in ldlm due to doing
2037          * obd_fail_loc |= OBD_FAIL_ONCE without actually
2038          * correct actions like it is done in target_send_reply_msg().
2039          */
2040         if (h->mh_fail_id != 0) {
2041                 /*
2042                  * Set to info->mti_fail_id to handler fail_id, it will be used
2043                  * later, and better than use default fail_id.
2044                  */
2045                 if (OBD_FAIL_CHECK_RESET(h->mh_fail_id && OBD_FAIL_MASK_LOC,
2046                                          h->mh_fail_id & ~OBD_FAILED)) {
2047                         info->mti_fail_id = h->mh_fail_id;
2048                         RETURN(0);
2049                 }
2050         }
2051
2052         rc = 0;
2053         flags = h->mh_flags;
2054         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2055
2056         if (h->mh_fmt != NULL) {
2057                 req_capsule_set(&info->mti_pill, h->mh_fmt);
2058                 rc = mdt_unpack_req_pack_rep(info, flags);
2059         }
2060
2061         if (rc == 0 && flags & MUTABOR &&
2062             req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2063                 /* should it be rq_status? */
2064                 rc = -EROFS;
2065
2066         if (rc == 0 && flags & HABEO_CLAVIS) {
2067                 struct ldlm_request *dlm_req;
2068
2069                 LASSERT(h->mh_fmt != NULL);
2070
2071                 dlm_req = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
2072                 if (dlm_req != NULL) {
2073                         if (info->mti_mdt->mdt_opts.mo_compat_resname)
2074                                 rc = mdt_lock_resname_compat(info->mti_mdt,
2075                                                              dlm_req);
2076                         info->mti_dlm_req = dlm_req;
2077                 } else {
2078                         CERROR("Can't unpack dlm request\n");
2079                         rc = -EFAULT;
2080                 }
2081         }
2082
2083         /* capability setting changed via /proc, needs reinitialize ctxt */
2084         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2085                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2086                 info->mti_mdt->mdt_capa_conf = 0;
2087         }
2088
2089         if (likely(rc == 0)) {
2090                 /*
2091                  * Process request, there can be two types of rc:
2092                  * 1) errors with msg unpack/pack, other failures outside the
2093                  * operation itself. This is counted as serious errors;
2094                  * 2) errors during fs operation, should be placed in rq_status
2095                  * only
2096                  */
2097                 rc = h->mh_act(info);
2098                 serious = is_serious(rc);
2099                 rc = clear_serious(rc);
2100         } else
2101                 serious = 1;
2102
2103         req->rq_status = rc;
2104
2105         /*
2106          * ELDLM_* codes which > 0 should be in rq_status only as well as
2107          * all non-serious errors.
2108          */
2109         if (rc > 0 || !serious)
2110                 rc = 0;
2111
2112         LASSERT(current->journal_info == NULL);
2113
2114         if (rc == 0 && (flags & HABEO_CLAVIS) &&
2115             info->mti_mdt->mdt_opts.mo_compat_resname) {
2116                 struct ldlm_reply *dlmrep;
2117
2118                 dlmrep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2119                 if (dlmrep != NULL)
2120                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2121         }
2122
2123         /* If we're DISCONNECTing, the mdt_export_data is already freed */
2124         if (likely(rc == 0 && h->mh_opc != MDS_DISCONNECT))
2125                 target_committed_to_req(req);
2126
2127         if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2128                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2129                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2130                 LBUG();
2131         }
2132
2133         RETURN(rc);
2134 }
2135
2136 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2137 {
2138         lh->mlh_type = MDT_NUL_LOCK;
2139         lh->mlh_reg_lh.cookie = 0ull;
2140         lh->mlh_reg_mode = LCK_MINMODE;
2141         lh->mlh_pdo_lh.cookie = 0ull;
2142         lh->mlh_pdo_mode = LCK_MINMODE;
2143 }
2144
2145 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2146 {
2147         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2148         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2149 }
2150
2151 /*
2152  * Initialize fields of struct mdt_thread_info. Other fields are left in
2153  * uninitialized state, because it's too expensive to zero out whole
2154  * mdt_thread_info (> 1K) on each request arrival.
2155  */
2156 static void mdt_thread_info_init(struct ptlrpc_request *req,
2157                                  struct mdt_thread_info *info)
2158 {
2159         int i;
2160         struct md_capainfo *ci;
2161
2162         info->mti_rep_buf_nr = ARRAY_SIZE(info->mti_rep_buf_size);
2163         for (i = 0; i < ARRAY_SIZE(info->mti_rep_buf_size); i++)
2164                 info->mti_rep_buf_size[i] = -1;
2165         req_capsule_init(&info->mti_pill, req, RCL_SERVER,
2166                          info->mti_rep_buf_size);
2167
2168         /* lock handle */
2169         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2170                 mdt_lock_handle_init(&info->mti_lh[i]);
2171
2172         /* mdt device: it can be NULL while CONNECT */
2173         if (req->rq_export) {
2174                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2175                 info->mti_exp = req->rq_export;
2176         } else
2177                 info->mti_mdt = NULL;
2178         info->mti_env = req->rq_svc_thread->t_env;
2179         ci = md_capainfo(info->mti_env);
2180         memset(ci, 0, sizeof *ci);
2181
2182         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2183         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2184
2185         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2186         info->mti_body = NULL;
2187         info->mti_object = NULL;
2188         info->mti_dlm_req = NULL;
2189         info->mti_has_trans = 0;
2190         info->mti_no_need_trans = 0;
2191         info->mti_cross_ref = 0;
2192         info->mti_opdata = 0;
2193
2194         /* To not check for split by default. */
2195         info->mti_spec.sp_ck_split = 0;
2196 }
2197
2198 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2199 {
2200         int i;
2201
2202         req_capsule_fini(&info->mti_pill);
2203         if (info->mti_object != NULL) {
2204                 mdt_object_put(info->mti_env, info->mti_object);
2205                 info->mti_object = NULL;
2206         }
2207         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2208                 mdt_lock_handle_fini(&info->mti_lh[i]);
2209         info->mti_env = NULL;
2210 }
2211
2212 /* mds/handler.c */
2213 extern int mds_filter_recovery_request(struct ptlrpc_request *req,
2214                                        struct obd_device *obd, int *process);
2215 /*
2216  * Handle recovery. Return:
2217  *        +1: continue request processing;
2218  *       -ve: abort immediately with the given error code;
2219  *         0: send reply with error code in req->rq_status;
2220  */
2221 static int mdt_recovery(struct mdt_thread_info *info)
2222 {
2223         struct ptlrpc_request *req = mdt_info_req(info);
2224         int recovering;
2225         struct obd_device *obd;
2226
2227         ENTRY;
2228
2229         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2230         case MDS_CONNECT:
2231         case SEC_CTX_INIT:
2232         case SEC_CTX_INIT_CONT:
2233         case SEC_CTX_FINI:
2234                 {
2235 #if 0
2236                         int rc;
2237
2238                         rc = mdt_handle_idmap(info);
2239                         if (rc)
2240                                 RETURN(rc);
2241                         else
2242 #endif
2243                                 RETURN(+1);
2244                 }
2245         }
2246
2247         if (unlikely(req->rq_export == NULL)) {
2248                 CERROR("operation %d on unconnected MDS from %s\n",
2249                        lustre_msg_get_opc(req->rq_reqmsg),
2250                        libcfs_id2str(req->rq_peer));
2251                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2252                  * mds_A will get -ENOTCONN(especially for ping req),
2253                  * which will cause that mds_A deactive timeout, then when
2254                  * mds_A cleanup, the cleanup process will be suspended since
2255                  * deactive timeout is not zero.
2256                  */
2257                 req->rq_status = -ENOTCONN;
2258                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2259                 RETURN(0);
2260         }
2261
2262         /* sanity check: if the xid matches, the request must be marked as a
2263          * resent or replayed */
2264         if (req_xid_is_last(req)) {
2265                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2266                       (MSG_RESENT | MSG_REPLAY))) {
2267                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2268                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2269                                   lustre_msg_get_flags(req->rq_reqmsg));
2270                         LBUG();
2271                         req->rq_status = -ENOTCONN;
2272                         RETURN(-ENOTCONN);
2273                 }
2274         }
2275
2276         /* else: note the opposite is not always true; a RESENT req after a
2277          * failover will usually not match the last_xid, since it was likely
2278          * never committed. A REPLAYed request will almost never match the
2279          * last xid, however it could for a committed, but still retained,
2280          * open. */
2281
2282         obd = req->rq_export->exp_obd;
2283
2284         /* Check for aborted recovery... */
2285         spin_lock_bh(&obd->obd_processing_task_lock);
2286         recovering = obd->obd_recovering;
2287         spin_unlock_bh(&obd->obd_processing_task_lock);
2288         if (unlikely(recovering)) {
2289                 int rc;
2290                 int should_process;
2291                 DEBUG_REQ(D_INFO, req, "Got new replay");
2292                 rc = mds_filter_recovery_request(req, obd, &should_process);
2293                 if (rc != 0 || !should_process)
2294                         RETURN(rc);
2295                 else if (should_process < 0) {
2296                         req->rq_status = should_process;
2297                         rc = ptlrpc_error(req);
2298                         RETURN(rc);
2299                 }
2300         }
2301         RETURN(+1);
2302 }
2303
2304 static int mdt_reply(struct ptlrpc_request *req, int rc,
2305                      struct mdt_thread_info *info)
2306 {
2307         ENTRY;
2308
2309 #if 0
2310         if (req->rq_reply_state == NULL && rc == 0) {
2311                 req->rq_status = rc;
2312                 lustre_pack_reply(req, 1, NULL, NULL);
2313         }
2314 #endif
2315         target_send_reply(req, rc, info->mti_fail_id);
2316         RETURN(0);
2317 }
2318
2319 /* mds/handler.c */
2320 extern int mds_msg_check_version(struct lustre_msg *msg);
2321
2322 static int mdt_handle0(struct ptlrpc_request *req,
2323                        struct mdt_thread_info *info,
2324                        struct mdt_opc_slice *supported)
2325 {
2326         struct mdt_handler *h;
2327         struct lustre_msg  *msg;
2328         int                 rc;
2329
2330         ENTRY;
2331
2332         if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2333                 RETURN(0);
2334
2335         LASSERT(current->journal_info == NULL);
2336
2337         msg = req->rq_reqmsg;
2338         rc = mds_msg_check_version(msg);
2339         if (likely(rc == 0)) {
2340                 rc = mdt_recovery(info);
2341                 if (likely(rc == +1)) {
2342                         h = mdt_handler_find(lustre_msg_get_opc(msg),
2343                                              supported);
2344                         if (likely(h != NULL)) {
2345                                 rc = mdt_req_handle(info, h, req);
2346                                 rc = mdt_reply(req, rc, info);
2347                         } else {
2348                                 CERROR("The unsupported opc: 0x%x\n", lustre_msg_get_opc(msg) );
2349                                 req->rq_status = -ENOTSUPP;
2350                                 rc = ptlrpc_error(req);
2351                                 RETURN(rc);
2352                         }
2353                 }
2354         } else
2355                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2356         RETURN(rc);
2357 }
2358
2359 /*
2360  * MDT handler function called by ptlrpc service thread when request comes.
2361  *
2362  * XXX common "target" functionality should be factored into separate module
2363  * shared by mdt, ost and stand-alone services like fld.
2364  */
2365 static int mdt_handle_common(struct ptlrpc_request *req,
2366                              struct mdt_opc_slice *supported)
2367 {
2368         struct lu_env          *env;
2369         struct mdt_thread_info *info;
2370         int                     rc;
2371         ENTRY;
2372
2373         env = req->rq_svc_thread->t_env;
2374         LASSERT(env != NULL);
2375         LASSERT(env->le_ses != NULL);
2376         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2377         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2378         LASSERT(info != NULL);
2379
2380         mdt_thread_info_init(req, info);
2381
2382         rc = mdt_handle0(req, info, supported);
2383
2384         mdt_thread_info_fini(info);
2385         RETURN(rc);
2386 }
2387
2388 /*
2389  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2390  * as well.
2391  */
2392 int mdt_recovery_handle(struct ptlrpc_request *req)
2393 {
2394         int rc;
2395         ENTRY;
2396
2397         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2398         case FLD_QUERY:
2399                 rc = mdt_handle_common(req, mdt_fld_handlers);
2400                 break;
2401         case SEQ_QUERY:
2402                 rc = mdt_handle_common(req, mdt_seq_handlers);
2403                 break;
2404         default:
2405                 rc = mdt_handle_common(req, mdt_regular_handlers);
2406                 break;
2407         }
2408
2409         RETURN(rc);
2410 }
2411
2412 static int mdt_regular_handle(struct ptlrpc_request *req)
2413 {
2414         return mdt_handle_common(req, mdt_regular_handlers);
2415 }
2416
2417 static int mdt_readpage_handle(struct ptlrpc_request *req)
2418 {
2419         return mdt_handle_common(req, mdt_readpage_handlers);
2420 }
2421
2422 static int mdt_xmds_handle(struct ptlrpc_request *req)
2423 {
2424         return mdt_handle_common(req, mdt_xmds_handlers);
2425 }
2426
2427 static int mdt_mdsc_handle(struct ptlrpc_request *req)
2428 {
2429         return mdt_handle_common(req, mdt_seq_handlers);
2430 }
2431
2432 static int mdt_mdss_handle(struct ptlrpc_request *req)
2433 {
2434         return mdt_handle_common(req, mdt_seq_handlers);
2435 }
2436
2437 static int mdt_dtss_handle(struct ptlrpc_request *req)
2438 {
2439         return mdt_handle_common(req, mdt_seq_handlers);
2440 }
2441
2442 static int mdt_fld_handle(struct ptlrpc_request *req)
2443 {
2444         return mdt_handle_common(req, mdt_fld_handlers);
2445 }
2446
2447 enum mdt_it_code {
2448         MDT_IT_OPEN,
2449         MDT_IT_OCREAT,
2450         MDT_IT_CREATE,
2451         MDT_IT_GETATTR,
2452         MDT_IT_READDIR,
2453         MDT_IT_LOOKUP,
2454         MDT_IT_UNLINK,
2455         MDT_IT_TRUNC,
2456         MDT_IT_GETXATTR,
2457         MDT_IT_NR
2458 };
2459
2460 static int mdt_intent_getattr(enum mdt_it_code opcode,
2461                               struct mdt_thread_info *info,
2462                               struct ldlm_lock **,
2463                               int);
2464 static int mdt_intent_reint(enum mdt_it_code opcode,
2465                             struct mdt_thread_info *info,
2466                             struct ldlm_lock **,
2467                             int);
2468
2469 static struct mdt_it_flavor {
2470         const struct req_format *it_fmt;
2471         __u32                    it_flags;
2472         int                    (*it_act)(enum mdt_it_code ,
2473                                          struct mdt_thread_info *,
2474                                          struct ldlm_lock **,
2475                                          int);
2476         long                     it_reint;
2477 } mdt_it_flavor[] = {
2478         [MDT_IT_OPEN]     = {
2479                 .it_fmt   = &RQF_LDLM_INTENT,
2480                 /*.it_flags = HABEO_REFERO,*/
2481                 .it_flags = 0,
2482                 .it_act   = mdt_intent_reint,
2483                 .it_reint = REINT_OPEN
2484         },
2485         [MDT_IT_OCREAT]   = {
2486                 .it_fmt   = &RQF_LDLM_INTENT,
2487                 .it_flags = MUTABOR,
2488                 .it_act   = mdt_intent_reint,
2489                 .it_reint = REINT_OPEN
2490         },
2491         [MDT_IT_CREATE]   = {
2492                 .it_fmt   = &RQF_LDLM_INTENT,
2493                 .it_flags = MUTABOR,
2494                 .it_act   = mdt_intent_reint,
2495                 .it_reint = REINT_CREATE
2496         },
2497         [MDT_IT_GETATTR]  = {
2498                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2499                 .it_flags = HABEO_REFERO,
2500                 .it_act   = mdt_intent_getattr
2501         },
2502         [MDT_IT_READDIR]  = {
2503                 .it_fmt   = NULL,
2504                 .it_flags = 0,
2505                 .it_act   = NULL
2506         },
2507         [MDT_IT_LOOKUP]   = {
2508                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2509                 .it_flags = HABEO_REFERO,
2510                 .it_act   = mdt_intent_getattr
2511         },
2512         [MDT_IT_UNLINK]   = {
2513                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
2514                 .it_flags = MUTABOR,
2515                 .it_act   = NULL,
2516                 .it_reint = REINT_UNLINK
2517         },
2518         [MDT_IT_TRUNC]    = {
2519                 .it_fmt   = NULL,
2520                 .it_flags = MUTABOR,
2521                 .it_act   = NULL
2522         },
2523         [MDT_IT_GETXATTR] = {
2524                 .it_fmt   = NULL,
2525                 .it_flags = 0,
2526                 .it_act   = NULL
2527         }
2528 };
2529
2530 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2531                             struct ldlm_lock **lockp,
2532                             struct ldlm_lock *new_lock,
2533                             struct mdt_lock_handle *lh,
2534                             int flags)
2535 {
2536         struct ptlrpc_request  *req = mdt_info_req(info);
2537         struct ldlm_lock       *lock = *lockp;
2538
2539         /*
2540          * Get new lock only for cases when possible resent did not find any
2541          * lock.
2542          */
2543         if (new_lock == NULL)
2544                 new_lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2545
2546         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2547                 lh->mlh_reg_lh.cookie = 0;
2548                 RETURN(0);
2549         }
2550
2551         LASSERTF(new_lock != NULL,
2552                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2553
2554         /*
2555          * If we've already given this lock to a client once, then we should
2556          * have no readers or writers.  Otherwise, we should have one reader
2557          * _or_ writer ref (which will be zeroed below) before returning the
2558          * lock to a client.
2559          */
2560         if (new_lock->l_export == req->rq_export) {
2561                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2562         } else {
2563                 LASSERT(new_lock->l_export == NULL);
2564                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2565         }
2566
2567         *lockp = new_lock;
2568
2569         if (new_lock->l_export == req->rq_export) {
2570                 /*
2571                  * Already gave this to the client, which means that we
2572                  * reconstructed a reply.
2573                  */
2574                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2575                         MSG_RESENT);
2576                 lh->mlh_reg_lh.cookie = 0;
2577                 RETURN(ELDLM_LOCK_REPLACED);
2578         }
2579
2580         /* This lock might already be given to the client by an resent req,
2581          * in this case we should return ELDLM_LOCK_ABORTED,
2582          * so we should check led_held_locks here, but it will affect
2583          * performance, FIXME
2584          */
2585         /* Fixup the lock to be given to the client */
2586         lock_res_and_lock(new_lock);
2587         new_lock->l_readers = 0;
2588         new_lock->l_writers = 0;
2589
2590         new_lock->l_export = class_export_get(req->rq_export);
2591         spin_lock(&req->rq_export->exp_ldlm_data.led_lock);
2592         list_add(&new_lock->l_export_chain,
2593                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
2594         spin_unlock(&req->rq_export->exp_ldlm_data.led_lock);
2595
2596         new_lock->l_blocking_ast = lock->l_blocking_ast;
2597         new_lock->l_completion_ast = lock->l_completion_ast;
2598         new_lock->l_remote_handle = lock->l_remote_handle;
2599         new_lock->l_flags &= ~LDLM_FL_LOCAL;
2600
2601         unlock_res_and_lock(new_lock);
2602         LDLM_LOCK_PUT(new_lock);
2603         lh->mlh_reg_lh.cookie = 0;
2604
2605         RETURN(ELDLM_LOCK_REPLACED);
2606 }
2607
2608 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2609                                     struct ldlm_lock *new_lock,
2610                                     struct ldlm_lock **old_lock,
2611                                     struct mdt_lock_handle *lh)
2612 {
2613         struct ptlrpc_request  *req = mdt_info_req(info);
2614         struct obd_export      *exp = req->rq_export;
2615         struct lustre_handle    remote_hdl;
2616         struct ldlm_request    *dlmreq;
2617         struct list_head       *iter;
2618
2619         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2620                 return;
2621
2622         dlmreq = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
2623         remote_hdl = dlmreq->lock_handle[0];
2624
2625         spin_lock(&exp->exp_ldlm_data.led_lock);
2626         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2627                 struct ldlm_lock *lock;
2628                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2629                 if (lock == new_lock)
2630                         continue;
2631                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2632                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2633                         lh->mlh_reg_mode = lock->l_granted_mode;
2634
2635                         LDLM_DEBUG(lock, "restoring lock cookie");
2636                         DEBUG_REQ(D_DLMTRACE, req,
2637                                   "restoring lock cookie "LPX64,
2638                                   lh->mlh_reg_lh.cookie);
2639                         if (old_lock)
2640                                 *old_lock = LDLM_LOCK_GET(lock);
2641                         spin_unlock(&exp->exp_ldlm_data.led_lock);
2642                         return;
2643                 }
2644         }
2645         spin_unlock(&exp->exp_ldlm_data.led_lock);
2646
2647         /*
2648          * If the xid matches, then we know this is a resent request, and allow
2649          * it. (It's probably an OPEN, for which we don't send a lock.
2650          */
2651         if (req_xid_is_last(req))
2652                 return;
2653
2654         /*
2655          * This remote handle isn't enqueued, so we never received or processed
2656          * this request.  Clear MSG_RESENT, because it can be handled like any
2657          * normal request now.
2658          */
2659         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2660
2661         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2662                   remote_hdl.cookie);
2663 }
2664
2665 static int mdt_intent_getattr(enum mdt_it_code opcode,
2666                               struct mdt_thread_info *info,
2667                               struct ldlm_lock **lockp,
2668                               int flags)
2669 {
2670         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2671         struct ldlm_lock       *new_lock = NULL;
2672         __u64                   child_bits;
2673         struct ldlm_reply      *ldlm_rep;
2674         struct ptlrpc_request  *req;
2675         struct mdt_body        *reqbody;
2676         struct mdt_body        *repbody;
2677         int                     rc;
2678         ENTRY;
2679
2680         reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
2681         LASSERT(reqbody);
2682
2683         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
2684         LASSERT(repbody);
2685
2686         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
2687         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
2688         repbody->eadatasize = 0;
2689         repbody->aclsize = 0;
2690
2691         switch (opcode) {
2692         case MDT_IT_LOOKUP:
2693                 child_bits = MDS_INODELOCK_LOOKUP;
2694                 break;
2695         case MDT_IT_GETATTR:
2696                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2697                 break;
2698         default:
2699                 CERROR("Unhandled till now");
2700                 GOTO(out, rc = -EINVAL);
2701         }
2702
2703         rc = mdt_init_ucred(info, reqbody);
2704         if (rc)
2705                 GOTO(out, rc);
2706
2707         req = info->mti_pill.rc_req;
2708         ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2709         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
2710
2711         /* Get lock from request for possible resent case. */
2712         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
2713
2714         ldlm_rep->lock_policy_res2 =
2715                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
2716
2717         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
2718                 ldlm_rep->lock_policy_res2 = 0;
2719         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
2720             ldlm_rep->lock_policy_res2) {
2721                 lhc->mlh_reg_lh.cookie = 0ull;
2722                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
2723         }
2724
2725         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
2726         EXIT;
2727 out_ucred:
2728         mdt_exit_ucred(info);
2729 out:
2730         mdt_shrink_reply(info);
2731         return rc;
2732 }
2733
2734 static int mdt_intent_reint(enum mdt_it_code opcode,
2735                             struct mdt_thread_info *info,
2736                             struct ldlm_lock **lockp,
2737                             int flags)
2738 {
2739         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2740         struct ldlm_reply      *rep = NULL;
2741         long                    opc;
2742         int                     rc;
2743
2744         static const struct req_format *intent_fmts[REINT_MAX] = {
2745                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
2746                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
2747         };
2748
2749         ENTRY;
2750
2751         opc = mdt_reint_opcode(info, intent_fmts);
2752         if (opc < 0)
2753                 GOTO(out, rc = opc);
2754
2755         if (mdt_it_flavor[opcode].it_reint != opc) {
2756                 CERROR("Reint code %ld doesn't match intent: %d\n",
2757                        opc, opcode);
2758                 GOTO(out, rc = err_serious(-EPROTO));
2759         }
2760
2761         /* Get lock from request for possible resent case. */
2762         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
2763
2764         rc = mdt_reint_internal(info, lhc, opc);
2765
2766         /* Check whether the reply has been packed successfully. */
2767         if (mdt_info_req(info)->rq_repmsg != NULL)
2768                 rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2769         if (rep == NULL)
2770                 GOTO(out, rc = err_serious(-EFAULT));
2771
2772         /* MDC expects this in any case */
2773         if (rc != 0)
2774                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
2775
2776         /* Cross-ref case, the lock should be returned to the client */
2777         if (rc == -EREMOTE) {
2778                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
2779                 rep->lock_policy_res2 = 0;
2780                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
2781                 GOTO(out, rc);
2782         }
2783         rep->lock_policy_res2 = clear_serious(rc);
2784
2785         lhc->mlh_reg_lh.cookie = 0ull;
2786         rc = ELDLM_LOCK_ABORTED;
2787         EXIT;
2788 out:
2789         return rc;
2790 }
2791
2792 static int mdt_intent_code(long itcode)
2793 {
2794         int rc;
2795
2796         switch(itcode) {
2797         case IT_OPEN:
2798                 rc = MDT_IT_OPEN;
2799                 break;
2800         case IT_OPEN|IT_CREAT:
2801                 rc = MDT_IT_OCREAT;
2802                 break;
2803         case IT_CREAT:
2804                 rc = MDT_IT_CREATE;
2805                 break;
2806         case IT_READDIR:
2807                 rc = MDT_IT_READDIR;
2808                 break;
2809         case IT_GETATTR:
2810                 rc = MDT_IT_GETATTR;
2811                 break;
2812         case IT_LOOKUP:
2813                 rc = MDT_IT_LOOKUP;
2814                 break;
2815         case IT_UNLINK:
2816                 rc = MDT_IT_UNLINK;
2817                 break;
2818         case IT_TRUNC:
2819                 rc = MDT_IT_TRUNC;
2820                 break;
2821         case IT_GETXATTR:
2822                 rc = MDT_IT_GETXATTR;
2823                 break;
2824         default:
2825                 CERROR("Unknown intent opcode: %ld\n", itcode);
2826                 rc = -EINVAL;
2827                 break;
2828         }
2829         return rc;
2830 }
2831
2832 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
2833                           struct ldlm_lock **lockp, int flags)
2834 {
2835         struct req_capsule   *pill;
2836         struct mdt_it_flavor *flv;
2837         int opc;
2838         int rc;
2839         ENTRY;
2840
2841         opc = mdt_intent_code(itopc);
2842         if (opc < 0)
2843                 RETURN(-EINVAL);
2844
2845         pill = &info->mti_pill;
2846         flv  = &mdt_it_flavor[opc];
2847
2848         if (flv->it_fmt != NULL)
2849                 req_capsule_extend(pill, flv->it_fmt);
2850
2851         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
2852         if (rc == 0) {
2853                 struct ptlrpc_request *req = mdt_info_req(info);
2854                 if (flv->it_flags & MUTABOR &&
2855                     req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2856                         rc = -EROFS;
2857         }
2858         if (rc == 0 && flv->it_act != NULL) {
2859                 /* execute policy */
2860                 rc = flv->it_act(opc, info, lockp, flags);
2861         } else
2862                 rc = -EOPNOTSUPP;
2863         RETURN(rc);
2864 }
2865
2866 static int mdt_intent_policy(struct ldlm_namespace *ns,
2867                              struct ldlm_lock **lockp, void *req_cookie,
2868                              ldlm_mode_t mode, int flags, void *data)
2869 {
2870         struct mdt_thread_info *info;
2871         struct ptlrpc_request  *req  =  req_cookie;
2872         struct ldlm_intent     *it;
2873         struct req_capsule     *pill;
2874         int rc;
2875
2876         ENTRY;
2877
2878         LASSERT(req != NULL);
2879
2880         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
2881                                   &mdt_thread_key);
2882         LASSERT(info != NULL);
2883         pill = &info->mti_pill;
2884         LASSERT(pill->rc_req == req);
2885
2886         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
2887                 req_capsule_extend(pill, &RQF_LDLM_INTENT);
2888                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
2889                 if (it != NULL) {
2890                         const struct ldlm_request *dlmreq;
2891                         __u64 req_bits;
2892 #if 0
2893                         struct ldlm_lock       *lock = *lockp;
2894
2895                         LDLM_DEBUG(lock, "intent policy opc: %s\n",
2896                                    ldlm_it2str(it->opc));
2897 #endif
2898
2899                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
2900                         if (rc == 0)
2901                                 rc = ELDLM_OK;
2902
2903                         /*
2904                          * Lock without inodebits makes no sense and will oops
2905                          * later in ldlm. Let's check it now to see if we have
2906                          * wrong lock from client or bits get corrupted
2907                          * somewhere in mdt_intent_opc().
2908                          */
2909                         dlmreq = info->mti_dlm_req;
2910                         req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
2911                         LASSERT(req_bits != 0);
2912
2913                 } else
2914                         rc = err_serious(-EFAULT);
2915         } else {
2916                 /* No intent was provided */
2917                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
2918                 rc = req_capsule_pack(pill);
2919                 if (rc)
2920                         rc = err_serious(rc);
2921         }
2922         RETURN(rc);
2923 }
2924
2925 /*
2926  * Seq wrappers
2927  */
2928 static void mdt_seq_adjust(const struct lu_env *env,
2929                           struct mdt_device *m, int lost)
2930 {
2931         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2932         struct lu_range out;
2933         ENTRY;
2934
2935         LASSERT(ls && ls->ls_server_seq);
2936         LASSERT(lost >= 0);
2937         /* get extra seq from seq_server, moving it's range up */
2938         while (lost-- > 0) {
2939                 seq_server_alloc_meta(ls->ls_server_seq, NULL, &out, env);
2940         }
2941         EXIT;
2942 }
2943
2944 static int mdt_seq_fini(const struct lu_env *env,
2945                         struct mdt_device *m)
2946 {
2947         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2948         ENTRY;
2949
2950         if (ls && ls->ls_server_seq) {
2951                 seq_server_fini(ls->ls_server_seq, env);
2952                 OBD_FREE_PTR(ls->ls_server_seq);
2953                 ls->ls_server_seq = NULL;
2954         }
2955
2956         if (ls && ls->ls_control_seq) {
2957                 seq_server_fini(ls->ls_control_seq, env);
2958                 OBD_FREE_PTR(ls->ls_control_seq);
2959                 ls->ls_control_seq = NULL;
2960         }
2961
2962         if (ls && ls->ls_client_seq) {
2963                 seq_client_fini(ls->ls_client_seq);
2964                 OBD_FREE_PTR(ls->ls_client_seq);
2965                 ls->ls_client_seq = NULL;
2966         }
2967
2968         RETURN(0);
2969 }
2970
2971 static int mdt_seq_init(const struct lu_env *env,
2972                         const char *uuid,
2973                         struct mdt_device *m)
2974 {
2975         struct lu_site *ls;
2976         char *prefix;
2977         int rc;
2978         ENTRY;
2979
2980         ls = m->mdt_md_dev.md_lu_dev.ld_site;
2981
2982         /*
2983          * This is sequence-controller node. Init seq-controller server on local
2984          * MDT.
2985          */
2986         if (ls->ls_node_id == 0) {
2987                 LASSERT(ls->ls_control_seq == NULL);
2988
2989                 OBD_ALLOC_PTR(ls->ls_control_seq);
2990                 if (ls->ls_control_seq == NULL)
2991                         RETURN(-ENOMEM);
2992
2993                 rc = seq_server_init(ls->ls_control_seq,
2994                                      m->mdt_bottom, uuid,
2995                                      LUSTRE_SEQ_CONTROLLER,
2996                                      env);
2997
2998                 if (rc)
2999                         GOTO(out_seq_fini, rc);
3000
3001                 OBD_ALLOC_PTR(ls->ls_client_seq);
3002                 if (ls->ls_client_seq == NULL)
3003                         GOTO(out_seq_fini, rc = -ENOMEM);
3004
3005                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3006                 if (prefix == NULL) {
3007                         OBD_FREE_PTR(ls->ls_client_seq);
3008                         GOTO(out_seq_fini, rc = -ENOMEM);
3009                 }
3010
3011                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3012                          uuid);
3013
3014                 /*
3015                  * Init seq-controller client after seq-controller server is
3016                  * ready. Pass ls->ls_control_seq to it for direct talking.
3017                  */
3018                 rc = seq_client_init(ls->ls_client_seq, NULL,
3019                                      LUSTRE_SEQ_METADATA, prefix,
3020                                      ls->ls_control_seq);
3021                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3022
3023                 if (rc)
3024                         GOTO(out_seq_fini, rc);
3025         }
3026
3027         /* Init seq-server on local MDT */
3028         LASSERT(ls->ls_server_seq == NULL);
3029
3030         OBD_ALLOC_PTR(ls->ls_server_seq);
3031         if (ls->ls_server_seq == NULL)
3032                 GOTO(out_seq_fini, rc = -ENOMEM);
3033
3034         rc = seq_server_init(ls->ls_server_seq,
3035                              m->mdt_bottom, uuid,
3036                              LUSTRE_SEQ_SERVER,
3037                              env);
3038         if (rc)
3039                 GOTO(out_seq_fini, rc = -ENOMEM);
3040
3041         /* Assign seq-controller client to local seq-server. */
3042         if (ls->ls_node_id == 0) {
3043                 LASSERT(ls->ls_client_seq != NULL);
3044
3045                 rc = seq_server_set_cli(ls->ls_server_seq,
3046                                         ls->ls_client_seq,
3047                                         env);
3048         }
3049
3050         EXIT;
3051 out_seq_fini:
3052         if (rc)
3053                 mdt_seq_fini(env, m);
3054
3055         return rc;
3056 }
3057 /*
3058  * Init client sequence manager which is used by local MDS to talk to sequence
3059  * controller on remote node.
3060  */
3061 static int mdt_seq_init_cli(const struct lu_env *env,
3062                             struct mdt_device *m,
3063                             struct lustre_cfg *cfg)
3064 {
3065         struct lu_site    *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3066         struct obd_device *mdc;
3067         struct obd_uuid   *uuidp, *mdcuuidp;
3068         char              *uuid_str, *mdc_uuid_str;
3069         int                rc;
3070         int                index;
3071         struct mdt_thread_info *info;
3072         char *p, *index_string = lustre_cfg_string(cfg, 2);
3073         ENTRY;
3074
3075         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3076         uuidp = &info->mti_u.uuid[0];
3077         mdcuuidp = &info->mti_u.uuid[1];
3078
3079         LASSERT(index_string);
3080
3081         index = simple_strtol(index_string, &p, 10);
3082         if (*p) {
3083                 CERROR("Invalid index in lustre_cgf, offset 2\n");
3084                 RETURN(-EINVAL);
3085         }
3086
3087         /* check if this is adding the first MDC and controller is not yet
3088          * initialized. */
3089         if (index != 0 || ls->ls_client_seq)
3090                 RETURN(0);
3091
3092         uuid_str = lustre_cfg_string(cfg, 1);
3093         mdc_uuid_str = lustre_cfg_string(cfg, 4);
3094         obd_str2uuid(uuidp, uuid_str);
3095         obd_str2uuid(mdcuuidp, mdc_uuid_str);
3096
3097         mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3098         if (!mdc) {
3099                 CERROR("can't find controller MDC by uuid %s\n",
3100                        uuid_str);
3101                 rc = -ENOENT;
3102         } else if (!mdc->obd_set_up) {
3103                 CERROR("target %s not set up\n", mdc->obd_name);
3104                 rc = -EINVAL;
3105         } else {
3106                 LASSERT(ls->ls_control_exp);
3107                 OBD_ALLOC_PTR(ls->ls_client_seq);
3108                 if (ls->ls_client_seq != NULL) {
3109                         char *prefix;
3110
3111                         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3112                         if (!prefix)
3113                                 RETURN(-ENOMEM);
3114
3115                         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3116                                  mdc->obd_name);
3117
3118                         rc = seq_client_init(ls->ls_client_seq,
3119                                              ls->ls_control_exp,
3120                                              LUSTRE_SEQ_METADATA,
3121                                              prefix, NULL);
3122                         OBD_FREE(prefix, MAX_OBD_NAME + 5);
3123                 } else
3124                         rc = -ENOMEM;
3125
3126                 if (rc)
3127                         RETURN(rc);
3128
3129                 LASSERT(ls->ls_server_seq != NULL);
3130                 rc = seq_server_set_cli(ls->ls_server_seq, ls->ls_client_seq,
3131                                         env);
3132         }
3133
3134         RETURN(rc);
3135 }
3136
3137 static void mdt_seq_fini_cli(struct mdt_device *m)
3138 {
3139         struct lu_site *ls;
3140
3141         ENTRY;
3142
3143         ls = m->mdt_md_dev.md_lu_dev.ld_site;
3144
3145         if (ls && ls->ls_server_seq)
3146                 seq_server_set_cli(ls->ls_server_seq,
3147                                    NULL, NULL);
3148
3149         if (ls && ls->ls_control_exp) {
3150                 class_export_put(ls->ls_control_exp);
3151                 ls->ls_control_exp = NULL;
3152         }
3153         EXIT;
3154 }
3155
3156 /*
3157  * FLD wrappers
3158  */
3159 static int mdt_fld_fini(const struct lu_env *env,
3160                         struct mdt_device *m)
3161 {
3162         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3163         ENTRY;
3164
3165         if (ls && ls->ls_server_fld) {
3166                 fld_server_fini(ls->ls_server_fld, env);
3167                 OBD_FREE_PTR(ls->ls_server_fld);
3168                 ls->ls_server_fld = NULL;
3169         }
3170
3171         RETURN(0);
3172 }
3173
3174 static int mdt_fld_init(const struct lu_env *env,
3175                         const char *uuid,
3176                         struct mdt_device *m)
3177 {
3178         struct lu_site *ls;
3179         int rc;
3180         ENTRY;
3181
3182         ls = m->mdt_md_dev.md_lu_dev.ld_site;
3183
3184         OBD_ALLOC_PTR(ls->ls_server_fld);
3185         if (ls->ls_server_fld == NULL)
3186                 RETURN(rc = -ENOMEM);
3187
3188         rc = fld_server_init(ls->ls_server_fld,
3189                              m->mdt_bottom, uuid, env);
3190         if (rc) {
3191                 OBD_FREE_PTR(ls->ls_server_fld);
3192                 ls->ls_server_fld = NULL;
3193                 RETURN(rc);
3194         }
3195
3196         RETURN(0);
3197 }
3198
3199 /* device init/fini methods */
3200 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3201 {
3202         ENTRY;
3203         if (m->mdt_regular_service != NULL) {
3204                 ptlrpc_unregister_service(m->mdt_regular_service);
3205                 m->mdt_regular_service = NULL;
3206         }
3207         if (m->mdt_readpage_service != NULL) {
3208                 ptlrpc_unregister_service(m->mdt_readpage_service);
3209                 m->mdt_readpage_service = NULL;
3210         }
3211         if (m->mdt_xmds_service != NULL) {
3212                 ptlrpc_unregister_service(m->mdt_xmds_service);
3213                 m->mdt_xmds_service = NULL;
3214         }
3215         if (m->mdt_setattr_service != NULL) {
3216                 ptlrpc_unregister_service(m->mdt_setattr_service);
3217                 m->mdt_setattr_service = NULL;
3218         }
3219         if (m->mdt_mdsc_service != NULL) {
3220                 ptlrpc_unregister_service(m->mdt_mdsc_service);
3221                 m->mdt_mdsc_service = NULL;
3222         }
3223         if (m->mdt_mdss_service != NULL) {
3224                 ptlrpc_unregister_service(m->mdt_mdss_service);
3225                 m->mdt_mdss_service = NULL;
3226         }
3227         if (m->mdt_dtss_service != NULL) {
3228                 ptlrpc_unregister_service(m->mdt_dtss_service);
3229                 m->mdt_dtss_service = NULL;
3230         }
3231         if (m->mdt_fld_service != NULL) {
3232                 ptlrpc_unregister_service(m->mdt_fld_service);
3233                 m->mdt_fld_service = NULL;
3234         }
3235         ENTRY;
3236 }
3237
3238 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3239 {
3240         int rc;
3241         static struct ptlrpc_service_conf conf;
3242         cfs_proc_dir_entry_t *procfs_entry;
3243         ENTRY;
3244
3245         procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3246
3247         conf = (typeof(conf)) {
3248                 .psc_nbufs            = MDS_NBUFS,
3249                 .psc_bufsize          = MDS_BUFSIZE,
3250                 .psc_max_req_size     = MDS_MAXREQSIZE,
3251                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3252                 .psc_req_portal       = MDS_REQUEST_PORTAL,
3253                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3254                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3255                 /*
3256                  * We'd like to have a mechanism to set this on a per-device
3257                  * basis, but alas...
3258                  */
3259                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3260                                        MDT_MAX_THREADS),
3261                 .psc_max_threads   = MDT_MAX_THREADS,
3262                 .psc_ctx_tags      = LCT_MD_THREAD
3263         };
3264
3265         m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3266         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3267                            "mdt_ldlm_client", m->mdt_ldlm_client);
3268
3269         m->mdt_regular_service =
3270                 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3271                                      procfs_entry, NULL, LUSTRE_MDT_NAME);
3272         if (m->mdt_regular_service == NULL)
3273                 RETURN(-ENOMEM);
3274
3275         rc = ptlrpc_start_threads(NULL, m->mdt_regular_service);
3276         if (rc)
3277                 GOTO(err_mdt_svc, rc);
3278
3279         /*
3280          * readpage service configuration. Parameters have to be adjusted,
3281          * ideally.
3282          */
3283         conf = (typeof(conf)) {
3284                 .psc_nbufs            = MDS_NBUFS,
3285                 .psc_bufsize          = MDS_BUFSIZE,
3286                 .psc_max_req_size     = MDS_MAXREQSIZE,
3287                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3288                 .psc_req_portal       = MDS_READPAGE_PORTAL,
3289                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3290                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3291                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3292                                        MDT_MAX_THREADS),
3293                 .psc_max_threads   = MDT_MAX_THREADS,
3294                 .psc_ctx_tags      = LCT_MD_THREAD
3295         };
3296         m->mdt_readpage_service =
3297                 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3298                                      LUSTRE_MDT_NAME "_readpage",
3299                                      procfs_entry, NULL, "mdt_rdpg");
3300
3301         if (m->mdt_readpage_service == NULL) {
3302                 CERROR("failed to start readpage service\n");
3303                 GOTO(err_mdt_svc, rc = -ENOMEM);
3304         }
3305
3306         rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service);
3307
3308         /*
3309          * setattr service configuration.
3310          */
3311         conf = (typeof(conf)) {
3312                 .psc_nbufs            = MDS_NBUFS,
3313                 .psc_bufsize          = MDS_BUFSIZE,
3314                 .psc_max_req_size     = MDS_MAXREQSIZE,
3315                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3316                 .psc_req_portal       = MDS_SETATTR_PORTAL,
3317                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3318                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3319                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3320                                        MDT_MAX_THREADS),
3321                 .psc_max_threads   = MDT_MAX_THREADS,
3322                 .psc_ctx_tags      = LCT_MD_THREAD
3323         };
3324
3325         m->mdt_setattr_service =
3326                 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3327                                      LUSTRE_MDT_NAME "_setattr",
3328                                      procfs_entry, NULL, "mdt_attr");
3329
3330         if (!m->mdt_setattr_service) {
3331                 CERROR("failed to start setattr service\n");
3332                 GOTO(err_mdt_svc, rc = -ENOMEM);
3333         }
3334
3335         rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service);
3336         if (rc)
3337                 GOTO(err_mdt_svc, rc);
3338
3339         /*
3340          * sequence controller service configuration
3341          */
3342         conf = (typeof(conf)) {
3343                 .psc_nbufs = MDS_NBUFS,
3344                 .psc_bufsize = MDS_BUFSIZE,
3345                 .psc_max_req_size = SEQ_MAXREQSIZE,
3346                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3347                 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3348                 .psc_rep_portal = MDC_REPLY_PORTAL,
3349                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3350                 .psc_min_threads = SEQ_NUM_THREADS,
3351                 .psc_max_threads = SEQ_NUM_THREADS,
3352                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3353         };
3354
3355         m->mdt_mdsc_service =
3356                 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3357                                      LUSTRE_MDT_NAME"_mdsc",
3358                                      procfs_entry, NULL, "mdt_mdsc");
3359         if (!m->mdt_mdsc_service) {
3360                 CERROR("failed to start seq controller service\n");
3361                 GOTO(err_mdt_svc, rc = -ENOMEM);
3362         }
3363
3364         rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service);
3365         if (rc)
3366                 GOTO(err_mdt_svc, rc);
3367
3368         /*
3369          * metadata sequence server service configuration
3370          */
3371         conf = (typeof(conf)) {
3372                 .psc_nbufs = MDS_NBUFS,
3373                 .psc_bufsize = MDS_BUFSIZE,
3374                 .psc_max_req_size = SEQ_MAXREQSIZE,
3375                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3376                 .psc_req_portal = SEQ_METADATA_PORTAL,
3377                 .psc_rep_portal = MDC_REPLY_PORTAL,
3378                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3379                 .psc_min_threads = SEQ_NUM_THREADS,
3380                 .psc_max_threads = SEQ_NUM_THREADS,
3381                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3382         };
3383
3384         m->mdt_mdss_service =
3385                 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
3386                                      LUSTRE_MDT_NAME"_mdss",
3387                                      procfs_entry, NULL, "mdt_mdss");
3388         if (!m->mdt_mdss_service) {
3389                 CERROR("failed to start metadata seq server service\n");
3390                 GOTO(err_mdt_svc, rc = -ENOMEM);
3391         }
3392
3393         rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service);
3394         if (rc)
3395                 GOTO(err_mdt_svc, rc);
3396
3397
3398         /*
3399          * Data sequence server service configuration. We want to have really
3400          * cluster-wide sequences space. This is why we start only one sequence
3401          * controller which manages space.
3402          */
3403         conf = (typeof(conf)) {
3404                 .psc_nbufs = MDS_NBUFS,
3405                 .psc_bufsize = MDS_BUFSIZE,
3406                 .psc_max_req_size = SEQ_MAXREQSIZE,
3407                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3408                 .psc_req_portal = SEQ_DATA_PORTAL,
3409                 .psc_rep_portal = OSC_REPLY_PORTAL,
3410                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3411                 .psc_min_threads = SEQ_NUM_THREADS,
3412                 .psc_max_threads = SEQ_NUM_THREADS,
3413                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3414         };
3415
3416         m->mdt_dtss_service =
3417                 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
3418                                      LUSTRE_MDT_NAME"_dtss",
3419                                      procfs_entry, NULL, "mdt_dtss");
3420         if (!m->mdt_dtss_service) {
3421                 CERROR("failed to start data seq server service\n");
3422                 GOTO(err_mdt_svc, rc = -ENOMEM);
3423         }
3424
3425         rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service);
3426         if (rc)
3427                 GOTO(err_mdt_svc, rc);
3428
3429         /* FLD service start */
3430         conf = (typeof(conf)) {
3431                 .psc_nbufs            = MDS_NBUFS,
3432                 .psc_bufsize          = MDS_BUFSIZE,
3433                 .psc_max_req_size     = FLD_MAXREQSIZE,
3434                 .psc_max_reply_size   = FLD_MAXREPSIZE,
3435                 .psc_req_portal       = FLD_REQUEST_PORTAL,
3436                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3437                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3438                 .psc_min_threads      = FLD_NUM_THREADS,
3439                 .psc_max_threads      = FLD_NUM_THREADS,
3440                 .psc_ctx_tags         = LCT_DT_THREAD|LCT_MD_THREAD
3441         };
3442
3443         m->mdt_fld_service =
3444                 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
3445                                      LUSTRE_MDT_NAME"_fld",
3446                                      procfs_entry, NULL, "mdt_fld");
3447         if (!m->mdt_fld_service) {
3448                 CERROR("failed to start fld service\n");
3449                 GOTO(err_mdt_svc, rc = -ENOMEM);
3450         }
3451
3452         rc = ptlrpc_start_threads(NULL, m->mdt_fld_service);
3453         if (rc)
3454                 GOTO(err_mdt_svc, rc);
3455
3456         /*
3457          * mds-mds service configuration. Separate portal is used to allow
3458          * mds-mds requests be not blocked during recovery.
3459          */
3460         conf = (typeof(conf)) {
3461                 .psc_nbufs            = MDS_NBUFS,
3462                 .psc_bufsize          = MDS_BUFSIZE,
3463                 .psc_max_req_size     = MDS_MAXREQSIZE,
3464                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3465                 .psc_req_portal       = MDS_MDS_PORTAL,
3466                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3467                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3468                 .psc_min_threads      = min(max(mdt_num_threads, MDT_MIN_THREADS),
3469                                             MDT_MAX_THREADS),
3470                 .psc_max_threads      = MDT_MAX_THREADS,
3471                 .psc_ctx_tags         = LCT_MD_THREAD
3472         };
3473         m->mdt_xmds_service = ptlrpc_init_svc_conf(&conf, mdt_xmds_handle,
3474                                                   LUSTRE_MDT_NAME "_mds",
3475                                                   procfs_entry, NULL, "mdt_xmds");
3476
3477         if (m->mdt_xmds_service == NULL) {
3478                 CERROR("failed to start readpage service\n");
3479                 GOTO(err_mdt_svc, rc = -ENOMEM);
3480         }
3481
3482         rc = ptlrpc_start_threads(NULL, m->mdt_xmds_service);
3483         if (rc)
3484                 GOTO(err_mdt_svc, rc);
3485
3486         EXIT;
3487 err_mdt_svc:
3488         if (rc)
3489                 mdt_stop_ptlrpc_service(m);
3490
3491         return rc;
3492 }
3493
3494 static void mdt_stack_fini(const struct lu_env *env,
3495                            struct mdt_device *m, struct lu_device *top)
3496 {
3497         struct lu_device        *d = top, *n;
3498         struct obd_device       *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3499         struct lustre_cfg_bufs  *bufs;
3500         struct lustre_cfg       *lcfg;
3501         struct mdt_thread_info  *info;
3502         char flags[3]="";
3503         ENTRY;
3504
3505         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3506         LASSERT(info != NULL);
3507
3508         bufs = &info->mti_u.bufs;
3509         /* process cleanup, pass mdt obd name to get obd umount flags */
3510         lustre_cfg_bufs_reset(bufs, obd->obd_name);
3511         if (obd->obd_force)
3512                 strcat(flags, "F");
3513         if (obd->obd_fail)
3514                 strcat(flags, "A");
3515         lustre_cfg_bufs_set_string(bufs, 1, flags);
3516         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3517         if (!lcfg) {
3518                 CERROR("Cannot alloc lcfg!\n");
3519                 return;
3520         }
3521
3522         LASSERT(top);
3523         top->ld_ops->ldo_process_config(env, top, lcfg);
3524         lustre_cfg_free(lcfg);
3525
3526         lu_site_purge(env, top->ld_site, ~0);
3527         while (d != NULL) {
3528                 struct obd_type *type;
3529                 struct lu_device_type *ldt = d->ld_type;
3530
3531                 /* each fini() returns next device in stack of layers
3532                  * * so we can avoid the recursion */
3533                 n = ldt->ldt_ops->ldto_device_fini(env, d);
3534                 lu_device_put(d);
3535                 ldt->ldt_ops->ldto_device_free(env, d);
3536                 type = ldt->ldt_obd_type;
3537                 type->typ_refcnt--;
3538                 class_put_type(type);
3539
3540                 /* switch to the next device in the layer */
3541                 d = n;
3542         }
3543         m->mdt_child = NULL;
3544         m->mdt_bottom = NULL;
3545 }
3546
3547 static struct lu_device *mdt_layer_setup(const struct lu_env *env,
3548                                          const char *typename,
3549                                          struct lu_device *child,
3550                                          struct lustre_cfg *cfg)
3551 {
3552         const char            *dev = lustre_cfg_string(cfg, 0);
3553         struct obd_type       *type;
3554         struct lu_device_type *ldt;
3555         struct lu_device      *d;
3556         int rc;
3557         ENTRY;
3558
3559         /* find the type */
3560         type = class_get_type(typename);
3561         if (!type) {
3562                 CERROR("Unknown type: '%s'\n", typename);
3563                 GOTO(out, rc = -ENODEV);
3564         }
3565
3566         rc = lu_context_refill(&env->le_ctx);
3567         if (rc != 0) {
3568                 CERROR("Failure to refill context: '%d'\n", rc);
3569                 GOTO(out_type, rc);
3570         }
3571
3572         if (env->le_ses != NULL) {
3573                 rc = lu_context_refill(env->le_ses);
3574                 if (rc != 0) {
3575                         CERROR("Failure to refill session: '%d'\n", rc);
3576                         GOTO(out_type, rc);
3577                 }
3578         }
3579
3580         ldt = type->typ_lu;
3581         if (ldt == NULL) {
3582                 CERROR("type: '%s'\n", typename);
3583                 GOTO(out_type, rc = -EINVAL);
3584         }
3585
3586         ldt->ldt_obd_type = type;
3587         d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
3588         if (IS_ERR(d)) {
3589                 CERROR("Cannot allocate device: '%s'\n", typename);
3590                 GOTO(out_type, rc = -ENODEV);
3591         }
3592
3593         LASSERT(child->ld_site);
3594         d->ld_site = child->ld_site;
3595
3596         type->typ_refcnt++;
3597         rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
3598         if (rc) {
3599                 CERROR("can't init device '%s', rc %d\n", typename, rc);
3600                 GOTO(out_alloc, rc);
3601         }
3602         lu_device_get(d);
3603
3604         RETURN(d);
3605
3606 out_alloc:
3607         ldt->ldt_ops->ldto_device_free(env, d);
3608         type->typ_refcnt--;
3609 out_type:
3610         class_put_type(type);
3611 out:
3612         return ERR_PTR(rc);
3613 }
3614
3615 static int mdt_stack_init(const struct lu_env *env,
3616                           struct mdt_device *m, struct lustre_cfg *cfg)
3617 {
3618         struct lu_device  *d = &m->mdt_md_dev.md_lu_dev;
3619         struct lu_device  *tmp;
3620         struct md_device  *md;
3621         int rc;
3622         ENTRY;
3623
3624         /* init the stack */
3625         tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
3626         if (IS_ERR(tmp)) {
3627                 RETURN(PTR_ERR(tmp));
3628         }
3629         m->mdt_bottom = lu2dt_dev(tmp);
3630         d = tmp;
3631         tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
3632         if (IS_ERR(tmp)) {
3633                 GOTO(out, rc = PTR_ERR(tmp));
3634         }
3635         d = tmp;
3636         md = lu2md_dev(d);
3637
3638         tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
3639         if (IS_ERR(tmp)) {
3640                 GOTO(out, rc = PTR_ERR(tmp));
3641         }
3642         d = tmp;
3643         /*set mdd upcall device*/
3644         md_upcall_dev_set(md, lu2md_dev(d));
3645
3646         md = lu2md_dev(d);
3647         /*set cmm upcall device*/
3648         md_upcall_dev_set(md, &m->mdt_md_dev);
3649
3650         m->mdt_child = lu2md_dev(d);
3651
3652         /* process setup config */
3653         tmp = &m->mdt_md_dev.md_lu_dev;
3654         rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
3655         GOTO(out, rc);
3656 out:
3657         /* fini from last known good lu_device */
3658         if (rc)
3659                 mdt_stack_fini(env, m, d);
3660
3661         return rc;
3662 }
3663
3664 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
3665 {
3666         struct md_device *next = m->mdt_child;
3667         struct lu_device *d    = &m->mdt_md_dev.md_lu_dev;
3668         struct lu_site   *ls   = d->ld_site;
3669         struct obd_device *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3670         ENTRY;
3671
3672         ping_evictor_stop();
3673         
3674         target_recovery_fini(obd);
3675         mdt_stop_ptlrpc_service(m);
3676
3677         mdt_fs_cleanup(env, m);
3678
3679         upcall_cache_cleanup(m->mdt_rmtacl_cache);
3680         m->mdt_rmtacl_cache = NULL;
3681
3682         upcall_cache_cleanup(m->mdt_identity_cache);
3683         m->mdt_identity_cache = NULL;
3684
3685         if (m->mdt_namespace != NULL) {
3686                 ldlm_namespace_free(m->mdt_namespace, d->ld_obd->obd_force);
3687                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
3688         }
3689
3690         mdt_seq_fini(env, m);
3691         mdt_seq_fini_cli(m);
3692         mdt_fld_fini(env, m);
3693         mdt_procfs_fini(m);
3694         ptlrpc_lprocfs_unregister_obd(d->ld_obd);
3695         lprocfs_obd_cleanup(d->ld_obd);
3696
3697         if (m->mdt_rootsquash_info) {
3698                 OBD_FREE_PTR(m->mdt_rootsquash_info);
3699                 m->mdt_rootsquash_info = NULL;
3700         }
3701
3702         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
3703         del_timer(&m->mdt_ck_timer);
3704         mdt_ck_thread_stop(m);
3705
3706         /* finish the stack */
3707         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3708
3709         if (ls) {
3710                 if (!list_empty(&ls->ls_lru) || ls->ls_total != 0) {
3711                         /*
3712                          * Uh-oh, objects still exist.
3713                          */
3714                         static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
3715
3716                         lu_site_print(env, ls, &cookie, lu_cdebug_printer);
3717                 }
3718
3719                 lu_site_fini(ls);
3720                 OBD_FREE_PTR(ls);
3721                 d->ld_site = NULL;
3722         }
3723         LASSERT(atomic_read(&d->ld_ref) == 0);
3724         md_device_fini(&m->mdt_md_dev);
3725
3726         EXIT;
3727 }
3728
3729 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
3730 {
3731         char *p = options;
3732
3733         if (!options)
3734                 return;
3735
3736         while (*options) {
3737                 int len;
3738
3739                 while (*p && *p != ',')
3740                         p++;
3741
3742                 len = p - options;
3743                 if ((len == sizeof("user_xattr") - 1) &&
3744                     (memcmp(options, "user_xattr", len) == 0)) {
3745                         m->mdt_opts.mo_user_xattr = 1;
3746                         LCONSOLE_INFO("Enabling user_xattr\n");
3747                 } else if ((len == sizeof("nouser_xattr") - 1) &&
3748                            (memcmp(options, "nouser_xattr", len) == 0)) {
3749                         m->mdt_opts.mo_user_xattr = 0;
3750                         LCONSOLE_INFO("Disabling user_xattr\n");
3751                 } else if ((len == sizeof("acl") - 1) &&
3752                            (memcmp(options, "acl", len) == 0)) {
3753 #ifdef CONFIG_FS_POSIX_ACL
3754                         m->mdt_opts.mo_acl = 1;
3755                         LCONSOLE_INFO("Enabling ACL\n");
3756 #else
3757                         m->mdt_opts.mo_acl = 0;
3758                         CWARN("ignoring unsupported acl mount option\n");
3759                         LCONSOLE_INFO("Disabling ACL\n");
3760 #endif
3761                 } else if ((len == sizeof("noacl") - 1) &&
3762                            (memcmp(options, "noacl", len) == 0)) {
3763                         m->mdt_opts.mo_acl = 0;
3764                         LCONSOLE_INFO("Disabling ACL\n");
3765                 }
3766
3767                 options = ++p;
3768         }
3769 }
3770
3771 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
3772
3773 static int mdt_init0(const struct lu_env *env, struct m