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