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