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