Whamcloud - gitweb
LU-957 scrub: trigger OI scrub if found bad OI entry
[fs/lustre-release.git] / lustre / mdt / mdt_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_handler.c
37  *
38  * Lustre Metadata Target (mdt) request handler
39  *
40  * Author: Peter Braam <braam@clusterfs.com>
41  * Author: Andreas Dilger <adilger@clusterfs.com>
42  * Author: Phil Schwan <phil@clusterfs.com>
43  * Author: Mike Shaver <shaver@clusterfs.com>
44  * Author: Nikita Danilov <nikita@clusterfs.com>
45  * Author: Huang Hua <huanghua@clusterfs.com>
46  * Author: Yury Umanets <umka@clusterfs.com>
47  */
48
49 #define DEBUG_SUBSYSTEM S_MDS
50
51 #include <linux/module.h>
52 /*
53  * struct OBD_{ALLOC,FREE}*()
54  */
55 #include <obd_support.h>
56 /* struct ptlrpc_request */
57 #include <lustre_net.h>
58 /* struct obd_export */
59 #include <lustre_export.h>
60 /* struct obd_device */
61 #include <obd.h>
62 /* lu2dt_dev() */
63 #include <dt_object.h>
64 #include <lustre_mds.h>
65 #include <lustre_mdt.h>
66 #include "mdt_internal.h"
67 #ifdef HAVE_QUOTA_SUPPORT
68 # include <lustre_quota.h>
69 #endif
70 #include <lustre_acl.h>
71 #include <lustre_param.h>
72 #include <lustre_fsfilt.h>
73
74 mdl_mode_t mdt_mdl_lock_modes[] = {
75         [LCK_MINMODE] = MDL_MINMODE,
76         [LCK_EX]      = MDL_EX,
77         [LCK_PW]      = MDL_PW,
78         [LCK_PR]      = MDL_PR,
79         [LCK_CW]      = MDL_CW,
80         [LCK_CR]      = MDL_CR,
81         [LCK_NL]      = MDL_NL,
82         [LCK_GROUP]   = MDL_GROUP
83 };
84
85 ldlm_mode_t mdt_dlm_lock_modes[] = {
86         [MDL_MINMODE] = LCK_MINMODE,
87         [MDL_EX]      = LCK_EX,
88         [MDL_PW]      = LCK_PW,
89         [MDL_PR]      = LCK_PR,
90         [MDL_CW]      = LCK_CW,
91         [MDL_CR]      = LCK_CR,
92         [MDL_NL]      = LCK_NL,
93         [MDL_GROUP]   = LCK_GROUP
94 };
95
96 /*
97  * Initialized in mdt_mod_init().
98  */
99 static unsigned long mdt_num_threads;
100
101 /* ptlrpc request handler for MDT. All handlers are
102  * grouped into several slices - struct mdt_opc_slice,
103  * and stored in an array - mdt_handlers[].
104  */
105 struct mdt_handler {
106         /* The name of this handler. */
107         const char *mh_name;
108         /* Fail id for this handler, checked at the beginning of this handler*/
109         int         mh_fail_id;
110         /* Operation code for this handler */
111         __u32       mh_opc;
112         /* flags are listed in enum mdt_handler_flags below. */
113         __u32       mh_flags;
114         /* The actual handler function to execute. */
115         int (*mh_act)(struct mdt_thread_info *info);
116         /* Request format for this request. */
117         const struct req_format *mh_fmt;
118 };
119
120 enum mdt_handler_flags {
121         /*
122          * struct mdt_body is passed in the incoming message, and object
123          * identified by this fid exists on disk.
124          *
125          * "habeo corpus" == "I have a body"
126          */
127         HABEO_CORPUS = (1 << 0),
128         /*
129          * struct ldlm_request is passed in the incoming message.
130          *
131          * "habeo clavis" == "I have a key"
132          */
133         HABEO_CLAVIS = (1 << 1),
134         /*
135          * this request has fixed reply format, so that reply message can be
136          * packed by generic code.
137          *
138          * "habeo refero" == "I have a reply"
139          */
140         HABEO_REFERO = (1 << 2),
141         /*
142          * this request will modify something, so check whether the filesystem
143          * is readonly or not, then return -EROFS to client asap if necessary.
144          *
145          * "mutabor" == "I shall modify"
146          */
147         MUTABOR      = (1 << 3)
148 };
149
150 struct mdt_opc_slice {
151         __u32               mos_opc_start;
152         int                 mos_opc_end;
153         struct mdt_handler *mos_hs;
154 };
155
156 static struct mdt_opc_slice mdt_regular_handlers[];
157 static struct mdt_opc_slice mdt_readpage_handlers[];
158 static struct mdt_opc_slice mdt_xmds_handlers[];
159 static struct mdt_opc_slice mdt_seq_handlers[];
160 static struct mdt_opc_slice mdt_fld_handlers[];
161
162 static struct mdt_device *mdt_dev(struct lu_device *d);
163 static int mdt_regular_handle(struct ptlrpc_request *req);
164 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
165 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
166                         struct getinfo_fid2path *fp);
167
168 static const struct lu_object_operations mdt_obj_ops;
169
170 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
171 {
172         if (!rep)
173                 return 0;
174         return (rep->lock_policy_res1 & flag);
175 }
176
177 void mdt_clear_disposition(struct mdt_thread_info *info,
178                            struct ldlm_reply *rep, int flag)
179 {
180         if (info)
181                 info->mti_opdata &= ~flag;
182         if (rep)
183                 rep->lock_policy_res1 &= ~flag;
184 }
185
186 void mdt_set_disposition(struct mdt_thread_info *info,
187                          struct ldlm_reply *rep, int flag)
188 {
189         if (info)
190                 info->mti_opdata |= flag;
191         if (rep)
192                 rep->lock_policy_res1 |= flag;
193 }
194
195 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
196 {
197         lh->mlh_pdo_hash = 0;
198         lh->mlh_reg_mode = lm;
199         lh->mlh_type = MDT_REG_LOCK;
200 }
201
202 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
203                        const char *name, int namelen)
204 {
205         lh->mlh_reg_mode = lm;
206         lh->mlh_type = MDT_PDO_LOCK;
207
208         if (name != NULL && (name[0] != '\0')) {
209                 LASSERT(namelen > 0);
210                 lh->mlh_pdo_hash = full_name_hash(name, namelen);
211         } else {
212                 LASSERT(namelen == 0);
213                 lh->mlh_pdo_hash = 0ull;
214         }
215 }
216
217 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
218                               struct mdt_lock_handle *lh)
219 {
220         mdl_mode_t mode;
221         ENTRY;
222
223         /*
224          * Any dir access needs couple of locks:
225          *
226          * 1) on part of dir we gonna take lookup/modify;
227          *
228          * 2) on whole dir to protect it from concurrent splitting and/or to
229          * flush client's cache for readdir().
230          *
231          * so, for a given mode and object this routine decides what lock mode
232          * to use for lock #2:
233          *
234          * 1) if caller's gonna lookup in dir then we need to protect dir from
235          * being splitted only - LCK_CR
236          *
237          * 2) if caller's gonna modify dir then we need to protect dir from
238          * being splitted and to flush cache - LCK_CW
239          *
240          * 3) if caller's gonna modify dir and that dir seems ready for
241          * splitting then we need to protect it from any type of access
242          * (lookup/modify/split) - LCK_EX --bzzz
243          */
244
245         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
246         LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
247
248         /*
249          * Ask underlaying level its opinion about preferable PDO lock mode
250          * having access type passed as regular lock mode:
251          *
252          * - MDL_MINMODE means that lower layer does not want to specify lock
253          * mode;
254          *
255          * - MDL_NL means that no PDO lock should be taken. This is used in some
256          * cases. Say, for non-splittable directories no need to use PDO locks
257          * at all.
258          */
259         mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
260                              mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
261
262         if (mode != MDL_MINMODE) {
263                 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
264         } else {
265                 /*
266                  * Lower layer does not want to specify locking mode. We do it
267                  * our selves. No special protection is needed, just flush
268                  * client's cache on modification and allow concurrent
269                  * mondification.
270                  */
271                 switch (lh->mlh_reg_mode) {
272                 case LCK_EX:
273                         lh->mlh_pdo_mode = LCK_EX;
274                         break;
275                 case LCK_PR:
276                         lh->mlh_pdo_mode = LCK_CR;
277                         break;
278                 case LCK_PW:
279                         lh->mlh_pdo_mode = LCK_CW;
280                         break;
281                 default:
282                         CERROR("Not expected lock type (0x%x)\n",
283                                (int)lh->mlh_reg_mode);
284                         LBUG();
285                 }
286         }
287
288         LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
289         EXIT;
290 }
291
292 static int mdt_getstatus(struct mdt_thread_info *info)
293 {
294         struct mdt_device *mdt  = info->mti_mdt;
295         struct md_device  *next = mdt->mdt_child;
296         struct mdt_body   *repbody;
297         int                rc;
298
299         ENTRY;
300
301         rc = mdt_check_ucred(info);
302         if (rc)
303                 RETURN(err_serious(rc));
304
305         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
306                 RETURN(err_serious(-ENOMEM));
307
308         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
309         rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
310         if (rc != 0)
311                 RETURN(rc);
312
313         repbody->valid |= OBD_MD_FLID;
314
315         if (mdt->mdt_opts.mo_mds_capa &&
316             info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
317                 struct mdt_object  *root;
318                 struct lustre_capa *capa;
319
320                 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
321                 if (IS_ERR(root))
322                         RETURN(PTR_ERR(root));
323
324                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
325                 LASSERT(capa);
326                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
327                 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
328                                  0);
329                 mdt_object_put(info->mti_env, root);
330                 if (rc == 0)
331                         repbody->valid |= OBD_MD_FLMDSCAPA;
332         }
333
334         RETURN(rc);
335 }
336
337 static int mdt_statfs(struct mdt_thread_info *info)
338 {
339         struct ptlrpc_request *req = mdt_info_req(info);
340         struct md_device      *next  = info->mti_mdt->mdt_child;
341         struct ptlrpc_service *svc;
342         struct obd_statfs     *osfs;
343         int                    rc;
344
345         ENTRY;
346
347         svc = info->mti_pill->rc_req->rq_rqbd->rqbd_service;
348
349         /* This will trigger a watchdog timeout */
350         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
351                          (MDT_SERVICE_WATCHDOG_FACTOR *
352                           at_get(&svc->srv_at_estimate)) + 1);
353
354         rc = mdt_check_ucred(info);
355         if (rc)
356                 RETURN(err_serious(rc));
357
358         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
359                 rc = err_serious(-ENOMEM);
360         } else {
361                 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
362                 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
363         }
364
365         if (rc == 0)
366                 mdt_counter_incr(req, LPROC_MDT_STATFS);
367
368         RETURN(rc);
369 }
370
371 /**
372  * Pack SOM attributes into the reply.
373  * Call under a DLM UPDATE lock.
374  */
375 static void mdt_pack_size2body(struct mdt_thread_info *info,
376                                struct mdt_object *mo)
377 {
378         struct mdt_body *b;
379         struct md_attr *ma = &info->mti_attr;
380
381         LASSERT(ma->ma_attr.la_valid & LA_MODE);
382         b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
383
384         /* Check if Size-on-MDS is supported, if this is a regular file,
385          * if SOM is enabled on the object and if SOM cache exists and valid.
386          * Otherwise do not pack Size-on-MDS attributes to the reply. */
387         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
388             !S_ISREG(ma->ma_attr.la_mode) ||
389             !mdt_object_is_som_enabled(mo) ||
390             !(ma->ma_valid & MA_SOM))
391                 return;
392
393         b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
394         b->size = ma->ma_som->msd_size;
395         b->blocks = ma->ma_som->msd_blocks;
396 }
397
398 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
399                         const struct lu_attr *attr, const struct lu_fid *fid)
400 {
401         struct md_attr *ma = &info->mti_attr;
402
403         LASSERT(ma->ma_valid & MA_INODE);
404
405         b->atime      = attr->la_atime;
406         b->mtime      = attr->la_mtime;
407         b->ctime      = attr->la_ctime;
408         b->mode       = attr->la_mode;
409         b->size       = attr->la_size;
410         b->blocks     = attr->la_blocks;
411         b->uid        = attr->la_uid;
412         b->gid        = attr->la_gid;
413         b->flags      = attr->la_flags;
414         b->nlink      = attr->la_nlink;
415         b->rdev       = attr->la_rdev;
416
417         /*XXX should pack the reply body according to lu_valid*/
418         b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID   |
419                     OBD_MD_FLGID   | OBD_MD_FLTYPE  |
420                     OBD_MD_FLMODE  | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
421                     OBD_MD_FLATIME | OBD_MD_FLMTIME ;
422
423         if (!S_ISREG(attr->la_mode)) {
424                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
425         } else if (ma->ma_need & MA_LOV && ma->ma_lmm_size == 0) {
426                 /* means no objects are allocated on osts. */
427                 LASSERT(!(ma->ma_valid & MA_LOV));
428                 /* just ignore blocks occupied by extend attributes on MDS */
429                 b->blocks = 0;
430                 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
431                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
432         }
433
434         if (fid) {
435                 b->fid1 = *fid;
436                 b->valid |= OBD_MD_FLID;
437
438                 /* FIXME: these should be fixed when new igif ready.*/
439                 b->ino  =  fid_oid(fid);       /* 1.6 compatibility */
440                 b->generation = fid_ver(fid);  /* 1.6 compatibility */
441                 b->valid |= OBD_MD_FLGENER;    /* 1.6 compatibility */
442
443                 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
444                                 PFID(fid), b->nlink, b->mode, b->size);
445         }
446
447         if (info)
448                 mdt_body_reverse_idmap(info, b);
449
450         if (b->valid & OBD_MD_FLSIZE)
451                 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
452                        PFID(fid), (unsigned long long)b->size);
453 }
454
455 static inline int mdt_body_has_lov(const struct lu_attr *la,
456                                    const struct mdt_body *body)
457 {
458         return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
459                 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
460 }
461
462 void mdt_client_compatibility(struct mdt_thread_info *info)
463 {
464         struct mdt_body       *body;
465         struct ptlrpc_request *req = mdt_info_req(info);
466         struct obd_export     *exp = req->rq_export;
467         struct md_attr        *ma = &info->mti_attr;
468         struct lu_attr        *la = &ma->ma_attr;
469         ENTRY;
470
471         if (exp->exp_connect_flags & OBD_CONNECT_LAYOUTLOCK)
472                 /* the client can deal with 16-bit lmm_stripe_count */
473                 RETURN_EXIT;
474
475         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
476
477         if (!mdt_body_has_lov(la, body))
478                 RETURN_EXIT;
479
480         /* now we have a reply with a lov for a client not compatible with the
481          * layout lock so we have to clean the layout generation number */
482         if (S_ISREG(la->la_mode))
483                 ma->ma_lmm->lmm_layout_gen = 0;
484         EXIT;
485 }
486
487
488 static int mdt_getattr_internal(struct mdt_thread_info *info,
489                                 struct mdt_object *o, int ma_need)
490 {
491         struct md_object        *next = mdt_object_child(o);
492         const struct mdt_body   *reqbody = info->mti_body;
493         struct ptlrpc_request   *req = mdt_info_req(info);
494         struct md_attr          *ma = &info->mti_attr;
495         struct lu_attr          *la = &ma->ma_attr;
496         struct req_capsule      *pill = info->mti_pill;
497         const struct lu_env     *env = info->mti_env;
498         struct mdt_body         *repbody;
499         struct lu_buf           *buffer = &info->mti_buf;
500         int                     rc;
501         ENTRY;
502
503         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
504                 RETURN(err_serious(-ENOMEM));
505
506         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
507
508         ma->ma_valid = 0;
509
510         rc = mdt_object_exists(o);
511         if (rc < 0) {
512                 /* This object is located on remote node.*/
513                 repbody->fid1 = *mdt_object_fid(o);
514                 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
515                 GOTO(out, rc = 0);
516         }
517
518         buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
519         buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
520
521         /* If it is dir object and client require MEA, then we got MEA */
522         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
523             reqbody->valid & OBD_MD_MEA) {
524                 /* Assumption: MDT_MD size is enough for lmv size. */
525                 ma->ma_lmv = buffer->lb_buf;
526                 ma->ma_lmv_size = buffer->lb_len;
527                 ma->ma_need = MA_LMV | MA_INODE;
528         } else {
529                 ma->ma_lmm = buffer->lb_buf;
530                 ma->ma_lmm_size = buffer->lb_len;
531                 ma->ma_need = MA_LOV | MA_INODE;
532         }
533
534         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
535             reqbody->valid & OBD_MD_FLDIREA  &&
536             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
537                 /* get default stripe info for this dir. */
538                 ma->ma_need |= MA_LOV_DEF;
539         }
540         ma->ma_need |= ma_need;
541         if (ma->ma_need & MA_SOM)
542                 ma->ma_som = &info->mti_u.som.data;
543
544         rc = mo_attr_get(env, next, ma);
545         if (unlikely(rc)) {
546                 CERROR("getattr error for "DFID": %d\n",
547                         PFID(mdt_object_fid(o)), rc);
548                 RETURN(rc);
549         }
550
551         if (likely(ma->ma_valid & MA_INODE))
552                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
553         else
554                 RETURN(-EFAULT);
555
556         if (mdt_body_has_lov(la, reqbody)) {
557                 if (ma->ma_valid & MA_LOV) {
558                         LASSERT(ma->ma_lmm_size);
559                         mdt_dump_lmm(D_INFO, ma->ma_lmm);
560                         repbody->eadatasize = ma->ma_lmm_size;
561                         if (S_ISDIR(la->la_mode))
562                                 repbody->valid |= OBD_MD_FLDIREA;
563                         else
564                                 repbody->valid |= OBD_MD_FLEASIZE;
565                 }
566                 if (ma->ma_valid & MA_LMV) {
567                         LASSERT(S_ISDIR(la->la_mode));
568                         repbody->eadatasize = ma->ma_lmv_size;
569                         repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
570                 }
571         } else if (S_ISLNK(la->la_mode) &&
572                    reqbody->valid & OBD_MD_LINKNAME) {
573                 buffer->lb_buf = ma->ma_lmm;
574                 /* eadatasize from client includes NULL-terminator, so
575                  * there is no need to read it */
576                 buffer->lb_len = reqbody->eadatasize - 1;
577                 rc = mo_readlink(env, next, buffer);
578                 if (unlikely(rc <= 0)) {
579                         CERROR("readlink failed: %d\n", rc);
580                         rc = -EFAULT;
581                 } else {
582                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
583                                  rc -= 2;
584                         repbody->valid |= OBD_MD_LINKNAME;
585                         /* we need to report back size with NULL-terminator
586                          * because client expects that */
587                         repbody->eadatasize = rc + 1;
588                         if (repbody->eadatasize != reqbody->eadatasize)
589                                 CERROR("Read shorter link %d than expected "
590                                        "%d\n", rc, reqbody->eadatasize - 1);
591                         /* NULL terminate */
592                         ((char*)ma->ma_lmm)[rc] = 0;
593                         CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
594                                (char*)ma->ma_lmm, rc);
595                         rc = 0;
596                 }
597         }
598
599         if (reqbody->valid & OBD_MD_FLMODEASIZE) {
600                 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
601                 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
602                 repbody->valid |= OBD_MD_FLMODEASIZE;
603                 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
604                        "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
605                        repbody->max_cookiesize);
606         }
607
608         if (exp_connect_rmtclient(info->mti_exp) &&
609             reqbody->valid & OBD_MD_FLRMTPERM) {
610                 void *buf = req_capsule_server_get(pill, &RMF_ACL);
611
612                 /* mdt_getattr_lock only */
613                 rc = mdt_pack_remote_perm(info, o, buf);
614                 if (rc) {
615                         repbody->valid &= ~OBD_MD_FLRMTPERM;
616                         repbody->aclsize = 0;
617                         RETURN(rc);
618                 } else {
619                         repbody->valid |= OBD_MD_FLRMTPERM;
620                         repbody->aclsize = sizeof(struct mdt_remote_perm);
621                 }
622         }
623 #ifdef CONFIG_FS_POSIX_ACL
624         else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
625                  (reqbody->valid & OBD_MD_FLACL)) {
626                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
627                 buffer->lb_len = req_capsule_get_size(pill,
628                                                       &RMF_ACL, RCL_SERVER);
629                 if (buffer->lb_len > 0) {
630                         rc = mo_xattr_get(env, next, buffer,
631                                           XATTR_NAME_ACL_ACCESS);
632                         if (rc < 0) {
633                                 if (rc == -ENODATA) {
634                                         repbody->aclsize = 0;
635                                         repbody->valid |= OBD_MD_FLACL;
636                                         rc = 0;
637                                 } else if (rc == -EOPNOTSUPP) {
638                                         rc = 0;
639                                 } else {
640                                         CERROR("got acl size: %d\n", rc);
641                                 }
642                         } else {
643                                 repbody->aclsize = rc;
644                                 repbody->valid |= OBD_MD_FLACL;
645                                 rc = 0;
646                         }
647                 }
648         }
649 #endif
650
651         if (reqbody->valid & OBD_MD_FLMDSCAPA &&
652             info->mti_mdt->mdt_opts.mo_mds_capa &&
653             info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
654                 struct lustre_capa *capa;
655
656                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
657                 LASSERT(capa);
658                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
659                 rc = mo_capa_get(env, next, capa, 0);
660                 if (rc)
661                         RETURN(rc);
662                 repbody->valid |= OBD_MD_FLMDSCAPA;
663         }
664
665 out:
666         if (rc == 0)
667                 mdt_counter_incr(req, LPROC_MDT_GETATTR);
668
669         RETURN(rc);
670 }
671
672 static int mdt_renew_capa(struct mdt_thread_info *info)
673 {
674         struct mdt_object  *obj = info->mti_object;
675         struct mdt_body    *body;
676         struct lustre_capa *capa, *c;
677         int rc;
678         ENTRY;
679
680         /* if object doesn't exist, or server has disabled capability,
681          * return directly, client will find body->valid OBD_MD_FLOSSCAPA
682          * flag not set.
683          */
684         if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
685             !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
686                 RETURN(0);
687
688         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
689         LASSERT(body != NULL);
690
691         c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
692         LASSERT(c);
693
694         capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
695         LASSERT(capa);
696
697         *capa = *c;
698         rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
699         if (rc == 0)
700                 body->valid |= OBD_MD_FLOSSCAPA;
701         RETURN(rc);
702 }
703
704 static int mdt_getattr(struct mdt_thread_info *info)
705 {
706         struct mdt_object       *obj = info->mti_object;
707         struct req_capsule      *pill = info->mti_pill;
708         struct mdt_body         *reqbody;
709         struct mdt_body         *repbody;
710         mode_t                   mode;
711         int rc, rc2;
712         ENTRY;
713
714         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
715         LASSERT(reqbody);
716
717         if (reqbody->valid & OBD_MD_FLOSSCAPA) {
718                 rc = req_capsule_server_pack(pill);
719                 if (unlikely(rc))
720                         RETURN(err_serious(rc));
721                 rc = mdt_renew_capa(info);
722                 GOTO(out_shrink, rc);
723         }
724
725         LASSERT(obj != NULL);
726         LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
727
728         mode = lu_object_attr(&obj->mot_obj.mo_lu);
729
730         /* old clients may not report needed easize, use max value then */
731         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
732                              reqbody->eadatasize == 0 ?
733                              info->mti_mdt->mdt_max_mdsize :
734                              reqbody->eadatasize);
735
736         rc = req_capsule_server_pack(pill);
737         if (unlikely(rc != 0))
738                 RETURN(err_serious(rc));
739
740         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
741         LASSERT(repbody != NULL);
742         repbody->eadatasize = 0;
743         repbody->aclsize = 0;
744
745         if (reqbody->valid & OBD_MD_FLRMTPERM)
746                 rc = mdt_init_ucred(info, reqbody);
747         else
748                 rc = mdt_check_ucred(info);
749         if (unlikely(rc))
750                 GOTO(out_shrink, rc);
751
752         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
753         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
754
755         /*
756          * Don't check capability at all, because rename might getattr for
757          * remote obj, and at that time no capability is available.
758          */
759         mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
760         rc = mdt_getattr_internal(info, obj, 0);
761         if (reqbody->valid & OBD_MD_FLRMTPERM)
762                 mdt_exit_ucred(info);
763         EXIT;
764 out_shrink:
765         mdt_client_compatibility(info);
766         rc2 = mdt_fix_reply(info);
767         if (rc == 0)
768                 rc = rc2;
769         return rc;
770 }
771
772 static int mdt_is_subdir(struct mdt_thread_info *info)
773 {
774         struct mdt_object     *o = info->mti_object;
775         struct req_capsule    *pill = info->mti_pill;
776         const struct mdt_body *body = info->mti_body;
777         struct mdt_body       *repbody;
778         int                    rc;
779         ENTRY;
780
781         LASSERT(o != NULL);
782
783         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
784
785         /*
786          * We save last checked parent fid to @repbody->fid1 for remote
787          * directory case.
788          */
789         LASSERT(fid_is_sane(&body->fid2));
790         LASSERT(mdt_object_exists(o) > 0);
791         rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
792                            &body->fid2, &repbody->fid1);
793         if (rc == 0 || rc == -EREMOTE)
794                 repbody->valid |= OBD_MD_FLID;
795
796         RETURN(rc);
797 }
798
799 static int mdt_raw_lookup(struct mdt_thread_info *info,
800                           struct mdt_object *parent,
801                           const struct lu_name *lname,
802                           struct ldlm_reply *ldlm_rep)
803 {
804         struct md_object *next = mdt_object_child(info->mti_object);
805         const struct mdt_body *reqbody = info->mti_body;
806         struct lu_fid *child_fid = &info->mti_tmp_fid1;
807         struct mdt_body *repbody;
808         int rc;
809         ENTRY;
810
811         if (reqbody->valid != OBD_MD_FLID)
812                 RETURN(0);
813
814         LASSERT(!info->mti_cross_ref);
815
816         /* Only got the fid of this obj by name */
817         fid_zero(child_fid);
818         rc = mdo_lookup(info->mti_env, next, lname, child_fid,
819                         &info->mti_spec);
820 #if 0
821         /* XXX is raw_lookup possible as intent operation? */
822         if (rc != 0) {
823                 if (rc == -ENOENT)
824                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
825                 RETURN(rc);
826         } else
827                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
828
829         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
830 #endif
831         if (rc == 0) {
832                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
833                 repbody->fid1 = *child_fid;
834                 repbody->valid = OBD_MD_FLID;
835         }
836         RETURN(1);
837 }
838
839 /*
840  * UPDATE lock should be taken against parent, and be release before exit;
841  * child_bits lock should be taken against child, and be returned back:
842  *            (1)normal request should release the child lock;
843  *            (2)intent request will grant the lock to client.
844  */
845 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
846                                  struct mdt_lock_handle *lhc,
847                                  __u64 child_bits,
848                                  struct ldlm_reply *ldlm_rep)
849 {
850         struct ptlrpc_request  *req       = mdt_info_req(info);
851         struct mdt_body        *reqbody   = NULL;
852         struct mdt_object      *parent    = info->mti_object;
853         struct mdt_object      *child;
854         struct md_object       *next      = mdt_object_child(parent);
855         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
856         struct lu_name         *lname     = NULL;
857         const char             *name      = NULL;
858         int                     namelen   = 0;
859         struct mdt_lock_handle *lhp       = NULL;
860         struct ldlm_lock       *lock;
861         struct ldlm_res_id     *res_id;
862         int                     is_resent;
863         int                     ma_need = 0;
864         int                     rc;
865
866         ENTRY;
867
868         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
869         LASSERT(ergo(is_resent,
870                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
871
872         LASSERT(parent != NULL);
873         name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
874         if (name == NULL)
875                 RETURN(err_serious(-EFAULT));
876
877         namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
878                                        RCL_CLIENT) - 1;
879         if (!info->mti_cross_ref) {
880                 /*
881                  * XXX: Check for "namelen == 0" is for getattr by fid
882                  * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
883                  * that is the name must contain at least one character and
884                  * the terminating '\0'
885                  */
886                 if (namelen == 0) {
887                         reqbody = req_capsule_client_get(info->mti_pill,
888                                                          &RMF_MDT_BODY);
889                         if (unlikely(reqbody == NULL))
890                                 RETURN(err_serious(-EFAULT));
891
892                         if (unlikely(!fid_is_sane(&reqbody->fid2)))
893                                 RETURN(err_serious(-EINVAL));
894
895                         name = NULL;
896                         CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
897                                "ldlm_rep = %p\n",
898                                PFID(mdt_object_fid(parent)),
899                                PFID(&reqbody->fid2), ldlm_rep);
900                 } else {
901                         lname = mdt_name(info->mti_env, (char *)name, namelen);
902                         CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
903                                "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
904                                name, ldlm_rep);
905                 }
906         }
907         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
908
909         rc = mdt_object_exists(parent);
910         if (unlikely(rc == 0)) {
911                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
912                                 &parent->mot_obj.mo_lu,
913                                 "Parent doesn't exist!\n");
914                 RETURN(-ESTALE);
915         } else if (!info->mti_cross_ref) {
916                 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
917                          PFID(mdt_object_fid(parent)));
918         }
919         if (lname) {
920                 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
921                 if (rc != 0) {
922                         if (rc > 0)
923                                 rc = 0;
924                         RETURN(rc);
925                 }
926         }
927
928         if (info->mti_cross_ref) {
929                 /* Only getattr on the child. Parent is on another node. */
930                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
931                 child = parent;
932                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
933                        "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
934
935                 if (is_resent) {
936                         /* Do not take lock for resent case. */
937                         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
938                         LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
939                                  lhc->mlh_reg_lh.cookie);
940                         LASSERT(fid_res_name_eq(mdt_object_fid(child),
941                                                 &lock->l_resource->lr_name));
942                         LDLM_LOCK_PUT(lock);
943                         rc = 0;
944                 } else {
945                         mdt_lock_handle_init(lhc);
946                         mdt_lock_reg_init(lhc, LCK_PR);
947
948                         /*
949                          * Object's name is on another MDS, no lookup lock is
950                          * needed here but update is.
951                          */
952                         child_bits &= ~MDS_INODELOCK_LOOKUP;
953                         child_bits |= MDS_INODELOCK_UPDATE;
954
955                         rc = mdt_object_lock(info, child, lhc, child_bits,
956                                              MDT_LOCAL_LOCK);
957                 }
958                 if (rc == 0) {
959                         /* Finally, we can get attr for child. */
960                         mdt_set_capainfo(info, 0, mdt_object_fid(child),
961                                          BYPASS_CAPA);
962                         rc = mdt_getattr_internal(info, child, 0);
963                         if (unlikely(rc != 0))
964                                 mdt_object_unlock(info, child, lhc, 1);
965                 }
966                 RETURN(rc);
967         }
968
969         if (lname) {
970                 /* step 1: lock parent only if parent is a directory */
971                 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
972                         lhp = &info->mti_lh[MDT_LH_PARENT];
973                         mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
974                         rc = mdt_object_lock(info, parent, lhp,
975                                              MDS_INODELOCK_UPDATE,
976                                              MDT_LOCAL_LOCK);
977                         if (unlikely(rc != 0))
978                                 RETURN(rc);
979                 }
980
981                 /* step 2: lookup child's fid by name */
982                 fid_zero(child_fid);
983                 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
984                                 &info->mti_spec);
985
986                 if (rc != 0) {
987                         if (rc == -ENOENT)
988                                 mdt_set_disposition(info, ldlm_rep,
989                                                     DISP_LOOKUP_NEG);
990                         GOTO(out_parent, rc);
991                 } else
992                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
993         } else {
994                 *child_fid = reqbody->fid2;
995                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
996         }
997
998         /*
999          *step 3: find the child object by fid & lock it.
1000          *        regardless if it is local or remote.
1001          */
1002         child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1003
1004         if (unlikely(IS_ERR(child)))
1005                 GOTO(out_parent, rc = PTR_ERR(child));
1006         if (is_resent) {
1007                 /* Do not take lock for resent case. */
1008                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1009                 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1010                          lhc->mlh_reg_lh.cookie);
1011
1012                 res_id = &lock->l_resource->lr_name;
1013                 if (!fid_res_name_eq(mdt_object_fid(child),
1014                                     &lock->l_resource->lr_name)) {
1015                          LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1016                                                  &lock->l_resource->lr_name),
1017                                  "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1018                                  (unsigned long)res_id->name[0],
1019                                  (unsigned long)res_id->name[1],
1020                                  (unsigned long)res_id->name[2],
1021                                  PFID(mdt_object_fid(parent)));
1022                           CWARN("Although resent, but still not get child lock"
1023                                 "parent:"DFID" child:"DFID"\n",
1024                                 PFID(mdt_object_fid(parent)),
1025                                 PFID(mdt_object_fid(child)));
1026                           lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1027                           LDLM_LOCK_PUT(lock);
1028                           GOTO(relock, 0);
1029                 }
1030                 LDLM_LOCK_PUT(lock);
1031                 rc = 0;
1032         } else {
1033 relock:
1034                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1035                 mdt_lock_handle_init(lhc);
1036                 if (child_bits == MDS_INODELOCK_LAYOUT)
1037                         mdt_lock_reg_init(lhc, LCK_CR);
1038                 else
1039                         mdt_lock_reg_init(lhc, LCK_PR);
1040
1041                 if (mdt_object_exists(child) == 0) {
1042                         LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1043                                         &child->mot_obj.mo_lu,
1044                                         "Object doesn't exist!\n");
1045                         GOTO(out_child, rc = -ENOENT);
1046                 }
1047
1048                 if (!(child_bits & MDS_INODELOCK_UPDATE)) {
1049                         struct md_attr *ma = &info->mti_attr;
1050
1051                         ma->ma_valid = 0;
1052                         ma->ma_need = MA_INODE;
1053                         rc = mo_attr_get(info->mti_env,
1054                                          mdt_object_child(child), ma);
1055                         if (unlikely(rc != 0))
1056                                 GOTO(out_child, rc);
1057
1058                         /* layout lock is used only on regular files */
1059                         if ((ma->ma_valid & MA_INODE) &&
1060                             (ma->ma_attr.la_valid & LA_MODE) &&
1061                             !S_ISREG(ma->ma_attr.la_mode))
1062                                 child_bits &= ~MDS_INODELOCK_LAYOUT;
1063
1064                         /* If the file has not been changed for some time, we
1065                          * return not only a LOOKUP lock, but also an UPDATE
1066                          * lock and this might save us RPC on later STAT. For
1067                          * directories, it also let negative dentry starts
1068                          * working for this dir. */
1069                         if (ma->ma_valid & MA_INODE &&
1070                             ma->ma_attr.la_valid & LA_CTIME &&
1071                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1072                                 ma->ma_attr.la_ctime < cfs_time_current_sec())
1073                                 child_bits |= MDS_INODELOCK_UPDATE;
1074                 }
1075
1076                 rc = mdt_object_lock(info, child, lhc, child_bits,
1077                                      MDT_CROSS_LOCK);
1078
1079                 if (unlikely(rc != 0))
1080                         GOTO(out_child, rc);
1081         }
1082
1083         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1084         /* Get MA_SOM attributes if update lock is given. */
1085         if (lock &&
1086             lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1087             S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1088                 ma_need = MA_SOM;
1089
1090         /* finally, we can get attr for child. */
1091         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1092         rc = mdt_getattr_internal(info, child, ma_need);
1093         if (unlikely(rc != 0)) {
1094                 mdt_object_unlock(info, child, lhc, 1);
1095         } else if (lock) {
1096                 /* Debugging code. */
1097                 res_id = &lock->l_resource->lr_name;
1098                 LDLM_DEBUG(lock, "Returning lock to client");
1099                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1100                                          &lock->l_resource->lr_name),
1101                          "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1102                          (unsigned long)res_id->name[0],
1103                          (unsigned long)res_id->name[1],
1104                          (unsigned long)res_id->name[2],
1105                          PFID(mdt_object_fid(child)));
1106                 mdt_pack_size2body(info, child);
1107         }
1108         if (lock)
1109                 LDLM_LOCK_PUT(lock);
1110
1111         EXIT;
1112 out_child:
1113         mdt_object_put(info->mti_env, child);
1114 out_parent:
1115         if (lhp)
1116                 mdt_object_unlock(info, parent, lhp, 1);
1117         return rc;
1118 }
1119
1120 /* normal handler: should release the child lock */
1121 static int mdt_getattr_name(struct mdt_thread_info *info)
1122 {
1123         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1124         struct mdt_body        *reqbody;
1125         struct mdt_body        *repbody;
1126         int rc, rc2;
1127         ENTRY;
1128
1129         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1130         LASSERT(reqbody != NULL);
1131         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1132         LASSERT(repbody != NULL);
1133
1134         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1135         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1136         repbody->eadatasize = 0;
1137         repbody->aclsize = 0;
1138
1139         rc = mdt_init_ucred(info, reqbody);
1140         if (unlikely(rc))
1141                 GOTO(out_shrink, rc);
1142
1143         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1144         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1145                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1146                 lhc->mlh_reg_lh.cookie = 0;
1147         }
1148         mdt_exit_ucred(info);
1149         EXIT;
1150 out_shrink:
1151         mdt_client_compatibility(info);
1152         rc2 = mdt_fix_reply(info);
1153         if (rc == 0)
1154                 rc = rc2;
1155         return rc;
1156 }
1157
1158 static const struct lu_device_operations mdt_lu_ops;
1159
1160 static int lu_device_is_mdt(struct lu_device *d)
1161 {
1162         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1163 }
1164
1165 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1166                          void *karg, void *uarg);
1167
1168 static int mdt_set_info(struct mdt_thread_info *info)
1169 {
1170         struct ptlrpc_request *req = mdt_info_req(info);
1171         char *key;
1172         void *val;
1173         int keylen, vallen, rc = 0;
1174         ENTRY;
1175
1176         rc = req_capsule_server_pack(info->mti_pill);
1177         if (rc)
1178                 RETURN(rc);
1179
1180         key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1181         if (key == NULL) {
1182                 DEBUG_REQ(D_HA, req, "no set_info key");
1183                 RETURN(-EFAULT);
1184         }
1185
1186         keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1187                                       RCL_CLIENT);
1188
1189         val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1190         if (val == NULL) {
1191                 DEBUG_REQ(D_HA, req, "no set_info val");
1192                 RETURN(-EFAULT);
1193         }
1194
1195         vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1196                                       RCL_CLIENT);
1197
1198         /* Swab any part of val you need to here */
1199         if (KEY_IS(KEY_READ_ONLY)) {
1200                 req->rq_status = 0;
1201                 lustre_msg_set_status(req->rq_repmsg, 0);
1202
1203                 cfs_spin_lock(&req->rq_export->exp_lock);
1204                 if (*(__u32 *)val)
1205                         req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1206                 else
1207                         req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1208                 cfs_spin_unlock(&req->rq_export->exp_lock);
1209
1210         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1211                 struct changelog_setinfo *cs =
1212                         (struct changelog_setinfo *)val;
1213                 if (vallen != sizeof(*cs)) {
1214                         CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1215                         RETURN(-EINVAL);
1216                 }
1217                 if (ptlrpc_req_need_swab(req)) {
1218                         __swab64s(&cs->cs_recno);
1219                         __swab32s(&cs->cs_id);
1220                 }
1221
1222                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1223                                    vallen, val, NULL);
1224                 lustre_msg_set_status(req->rq_repmsg, rc);
1225
1226         } else {
1227                 RETURN(-EINVAL);
1228         }
1229         RETURN(0);
1230 }
1231
1232 static int mdt_connect(struct mdt_thread_info *info)
1233 {
1234         int rc;
1235         struct ptlrpc_request *req;
1236
1237         req = mdt_info_req(info);
1238         rc = target_handle_connect(req);
1239         if (rc == 0) {
1240                 LASSERT(req->rq_export != NULL);
1241                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1242                 rc = mdt_init_sec_level(info);
1243                 if (rc == 0)
1244                         rc = mdt_init_idmap(info);
1245                 if (rc != 0)
1246                         obd_disconnect(class_export_get(req->rq_export));
1247         } else {
1248                 rc = err_serious(rc);
1249         }
1250         return rc;
1251 }
1252
1253 static int mdt_disconnect(struct mdt_thread_info *info)
1254 {
1255         int rc;
1256         ENTRY;
1257
1258         rc = target_handle_disconnect(mdt_info_req(info));
1259         if (rc)
1260                 rc = err_serious(rc);
1261         RETURN(rc);
1262 }
1263
1264 static int mdt_sendpage(struct mdt_thread_info *info,
1265                         struct lu_rdpg *rdpg, int nob)
1266 {
1267         struct ptlrpc_request   *req = mdt_info_req(info);
1268         struct obd_export       *exp = req->rq_export;
1269         struct ptlrpc_bulk_desc *desc;
1270         struct l_wait_info      *lwi = &info->mti_u.rdpg.mti_wait_info;
1271         int                      tmpcount;
1272         int                      tmpsize;
1273         int                      i;
1274         int                      rc;
1275         ENTRY;
1276
1277         desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1278                                     MDS_BULK_PORTAL);
1279         if (desc == NULL)
1280                 RETURN(-ENOMEM);
1281
1282         for (i = 0, tmpcount = nob;
1283                 i < rdpg->rp_npages && tmpcount > 0; i++, tmpcount -= tmpsize) {
1284                 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1285                 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1286         }
1287
1288         LASSERT(desc->bd_nob == nob);
1289         rc = target_bulk_io(exp, desc, lwi);
1290         ptlrpc_free_bulk(desc);
1291         RETURN(rc);
1292 }
1293
1294 #ifdef HAVE_SPLIT_SUPPORT
1295 /*
1296  * Retrieve dir entry from the page and insert it to the slave object, actually,
1297  * this should be in osd layer, but since it will not in the final product, so
1298  * just do it here and do not define more moo api anymore for this.
1299  */
1300 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1301                               int size)
1302 {
1303         struct mdt_object *object = info->mti_object;
1304         struct lu_fid *lf = &info->mti_tmp_fid2;
1305         struct md_attr *ma = &info->mti_attr;
1306         struct lu_dirpage *dp;
1307         struct lu_dirent *ent;
1308         int rc = 0, offset = 0;
1309         ENTRY;
1310
1311         /* Make sure we have at least one entry. */
1312         if (size == 0)
1313                 RETURN(-EINVAL);
1314
1315         /*
1316          * Disable trans for this name insert, since it will include many trans
1317          * for this.
1318          */
1319         info->mti_no_need_trans = 1;
1320         /*
1321          * When write_dir_page, no need update parent's ctime,
1322          * and no permission check for name_insert.
1323          */
1324         ma->ma_attr.la_ctime = 0;
1325         ma->ma_attr.la_valid = LA_MODE;
1326         ma->ma_valid = MA_INODE;
1327
1328         cfs_kmap(page);
1329         dp = page_address(page);
1330         offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1331
1332         for (ent = lu_dirent_start(dp); ent != NULL;
1333              ent = lu_dirent_next(ent)) {
1334                 struct lu_name *lname;
1335                 char *name;
1336
1337                 if (le16_to_cpu(ent->lde_namelen) == 0)
1338                         continue;
1339
1340                 fid_le_to_cpu(lf, &ent->lde_fid);
1341                 if (le64_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1342                         ma->ma_attr.la_mode = S_IFDIR;
1343                 else
1344                         ma->ma_attr.la_mode = 0;
1345                 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1346                 if (name == NULL)
1347                         GOTO(out, rc = -ENOMEM);
1348
1349                 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1350                 lname = mdt_name(info->mti_env, name,
1351                                  le16_to_cpu(ent->lde_namelen));
1352                 ma->ma_attr_flags |= (MDS_PERM_BYPASS | MDS_QUOTA_IGNORE);
1353                 rc = mdo_name_insert(info->mti_env,
1354                                      md_object_next(&object->mot_obj),
1355                                      lname, lf, ma);
1356                 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1357                 if (rc) {
1358                         CERROR("Can't insert %*.*s, rc %d\n",
1359                                le16_to_cpu(ent->lde_namelen),
1360                                le16_to_cpu(ent->lde_namelen),
1361                                ent->lde_name, rc);
1362                         GOTO(out, rc);
1363                 }
1364
1365                 offset += lu_dirent_size(ent);
1366                 if (offset >= size)
1367                         break;
1368         }
1369         EXIT;
1370 out:
1371         cfs_kunmap(page);
1372         return rc;
1373 }
1374
1375 static int mdt_bulk_timeout(void *data)
1376 {
1377         ENTRY;
1378
1379         CERROR("mdt bulk transfer timeout \n");
1380
1381         RETURN(1);
1382 }
1383
1384 static int mdt_writepage(struct mdt_thread_info *info)
1385 {
1386         struct ptlrpc_request   *req = mdt_info_req(info);
1387         struct mdt_body         *reqbody;
1388         struct l_wait_info      *lwi;
1389         struct ptlrpc_bulk_desc *desc;
1390         struct page             *page;
1391         int                rc;
1392         ENTRY;
1393
1394
1395         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1396         if (reqbody == NULL)
1397                 RETURN(err_serious(-EFAULT));
1398
1399         desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1400         if (desc == NULL)
1401                 RETURN(err_serious(-ENOMEM));
1402
1403         /* allocate the page for the desc */
1404         page = cfs_alloc_page(CFS_ALLOC_STD);
1405         if (page == NULL)
1406                 GOTO(desc_cleanup, rc = -ENOMEM);
1407
1408         CDEBUG(D_INFO, "Received page offset %d size %d \n",
1409                (int)reqbody->size, (int)reqbody->nlink);
1410
1411         ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1412                               (int)reqbody->nlink);
1413
1414         rc = sptlrpc_svc_prep_bulk(req, desc);
1415         if (rc != 0)
1416                 GOTO(cleanup_page, rc);
1417         /*
1418          * Check if client was evicted while we were doing i/o before touching
1419          * network.
1420          */
1421         OBD_ALLOC_PTR(lwi);
1422         if (!lwi)
1423                 GOTO(cleanup_page, rc = -ENOMEM);
1424
1425         if (desc->bd_export->exp_failed)
1426                 rc = -ENOTCONN;
1427         else
1428                 rc = ptlrpc_start_bulk_transfer (desc);
1429         if (rc == 0) {
1430                 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * CFS_HZ / 4, CFS_HZ,
1431                                             mdt_bulk_timeout, desc);
1432                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1433                                   desc->bd_export->exp_failed, lwi);
1434                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1435                 if (rc == -ETIMEDOUT) {
1436                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1437                         ptlrpc_abort_bulk(desc);
1438                 } else if (desc->bd_export->exp_failed) {
1439                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1440                         rc = -ENOTCONN;
1441                         ptlrpc_abort_bulk(desc);
1442                 } else if (!desc->bd_success ||
1443                            desc->bd_nob_transferred != desc->bd_nob) {
1444                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1445                                   desc->bd_success ?
1446                                   "truncated" : "network error on",
1447                                   desc->bd_nob_transferred, desc->bd_nob);
1448                         /* XXX should this be a different errno? */
1449                         rc = -ETIMEDOUT;
1450                 }
1451         } else {
1452                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1453         }
1454         if (rc)
1455                 GOTO(cleanup_lwi, rc);
1456         rc = mdt_write_dir_page(info, page, reqbody->nlink);
1457
1458 cleanup_lwi:
1459         OBD_FREE_PTR(lwi);
1460 cleanup_page:
1461         cfs_free_page(page);
1462 desc_cleanup:
1463         ptlrpc_free_bulk(desc);
1464         RETURN(rc);
1465 }
1466 #endif
1467
1468 static int mdt_readpage(struct mdt_thread_info *info)
1469 {
1470         struct mdt_object *object = info->mti_object;
1471         struct lu_rdpg    *rdpg = &info->mti_u.rdpg.mti_rdpg;
1472         struct mdt_body   *reqbody;
1473         struct mdt_body   *repbody;
1474         int                rc;
1475         int                i;
1476         ENTRY;
1477
1478         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1479                 RETURN(err_serious(-ENOMEM));
1480
1481         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1482         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1483         if (reqbody == NULL || repbody == NULL)
1484                 RETURN(err_serious(-EFAULT));
1485
1486         /*
1487          * prepare @rdpg before calling lower layers and transfer itself. Here
1488          * reqbody->size contains offset of where to start to read and
1489          * reqbody->nlink contains number bytes to read.
1490          */
1491         rdpg->rp_hash = reqbody->size;
1492         if (rdpg->rp_hash != reqbody->size) {
1493                 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1494                        rdpg->rp_hash, reqbody->size);
1495                 RETURN(-EFAULT);
1496         }
1497
1498         rdpg->rp_attrs = reqbody->mode;
1499         if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
1500                 rdpg->rp_attrs |= LUDA_64BITHASH;
1501         rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
1502                                 PTLRPC_MAX_BRW_SIZE);
1503         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1504                           CFS_PAGE_SHIFT;
1505         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1506         if (rdpg->rp_pages == NULL)
1507                 RETURN(-ENOMEM);
1508
1509         for (i = 0; i < rdpg->rp_npages; ++i) {
1510                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1511                 if (rdpg->rp_pages[i] == NULL)
1512                         GOTO(free_rdpg, rc = -ENOMEM);
1513         }
1514
1515         /* call lower layers to fill allocated pages with directory data */
1516         rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1517         if (rc < 0)
1518                 GOTO(free_rdpg, rc);
1519
1520         /* send pages to client */
1521         rc = mdt_sendpage(info, rdpg, rc);
1522
1523         EXIT;
1524 free_rdpg:
1525
1526         for (i = 0; i < rdpg->rp_npages; i++)
1527                 if (rdpg->rp_pages[i] != NULL)
1528                         cfs_free_page(rdpg->rp_pages[i]);
1529         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1530
1531         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1532                 RETURN(0);
1533
1534         return rc;
1535 }
1536
1537 static int mdt_reint_internal(struct mdt_thread_info *info,
1538                               struct mdt_lock_handle *lhc,
1539                               __u32 op)
1540 {
1541         struct req_capsule      *pill = info->mti_pill;
1542         struct md_quota         *mq = md_quota(info->mti_env);
1543         struct mdt_body         *repbody;
1544         int                      rc = 0, rc2;
1545         ENTRY;
1546
1547
1548         rc = mdt_reint_unpack(info, op);
1549         if (rc != 0) {
1550                 CERROR("Can't unpack reint, rc %d\n", rc);
1551                 RETURN(err_serious(rc));
1552         }
1553
1554         /* for replay (no_create) lmm is not needed, client has it already */
1555         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1556                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1557                                      info->mti_rr.rr_eadatalen);
1558
1559         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1560                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1561                                      info->mti_mdt->mdt_max_cookiesize);
1562
1563         rc = req_capsule_server_pack(pill);
1564         if (rc != 0) {
1565                 CERROR("Can't pack response, rc %d\n", rc);
1566                 RETURN(err_serious(rc));
1567         }
1568
1569         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1570                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1571                 LASSERT(repbody);
1572                 repbody->eadatasize = 0;
1573                 repbody->aclsize = 0;
1574         }
1575
1576         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1577
1578         /* for replay no cookkie / lmm need, because client have this already */
1579         if (info->mti_spec.no_create)
1580                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1581                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1582
1583         rc = mdt_init_ucred_reint(info);
1584         if (rc)
1585                 GOTO(out_shrink, rc);
1586
1587         rc = mdt_fix_attr_ucred(info, op);
1588         if (rc != 0)
1589                 GOTO(out_ucred, rc = err_serious(rc));
1590
1591         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1592                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1593                 GOTO(out_ucred, rc);
1594         }
1595         mq->mq_exp = info->mti_exp;
1596         rc = mdt_reint_rec(info, lhc);
1597         EXIT;
1598 out_ucred:
1599         mdt_exit_ucred(info);
1600 out_shrink:
1601         mdt_client_compatibility(info);
1602         rc2 = mdt_fix_reply(info);
1603         if (rc == 0)
1604                 rc = rc2;
1605         return rc;
1606 }
1607
1608 static long mdt_reint_opcode(struct mdt_thread_info *info,
1609                              const struct req_format **fmt)
1610 {
1611         struct mdt_rec_reint *rec;
1612         long opc;
1613
1614         opc = err_serious(-EFAULT);
1615         rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1616         if (rec != NULL) {
1617                 opc = rec->rr_opcode;
1618                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1619                 if (opc < REINT_MAX && fmt[opc] != NULL)
1620                         req_capsule_extend(info->mti_pill, fmt[opc]);
1621                 else {
1622                         CERROR("Unsupported opc: %ld\n", opc);
1623                         opc = err_serious(opc);
1624                 }
1625         }
1626         return opc;
1627 }
1628
1629 static int mdt_reint(struct mdt_thread_info *info)
1630 {
1631         long opc;
1632         int  rc;
1633
1634         static const struct req_format *reint_fmts[REINT_MAX] = {
1635                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
1636                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
1637                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
1638                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
1639                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
1640                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
1641                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1642         };
1643
1644         ENTRY;
1645
1646         opc = mdt_reint_opcode(info, reint_fmts);
1647         if (opc >= 0) {
1648                 /*
1649                  * No lock possible here from client to pass it to reint code
1650                  * path.
1651                  */
1652                 rc = mdt_reint_internal(info, NULL, opc);
1653         } else {
1654                 rc = opc;
1655         }
1656
1657         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1658         RETURN(rc);
1659 }
1660
1661 /* this should sync the whole device */
1662 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1663 {
1664         struct dt_device *dt = mdt->mdt_bottom;
1665         int rc;
1666         ENTRY;
1667
1668         rc = dt->dd_ops->dt_sync(env, dt);
1669         RETURN(rc);
1670 }
1671
1672 /* this should sync this object */
1673 static int mdt_object_sync(struct mdt_thread_info *info)
1674 {
1675         struct md_object *next;
1676         int rc;
1677         ENTRY;
1678
1679         if (!mdt_object_exists(info->mti_object)) {
1680                 CWARN("Non existing object  "DFID"!\n",
1681                       PFID(mdt_object_fid(info->mti_object)));
1682                 RETURN(-ESTALE);
1683         }
1684         next = mdt_object_child(info->mti_object);
1685         rc = mo_object_sync(info->mti_env, next);
1686
1687         RETURN(rc);
1688 }
1689
1690 static int mdt_sync(struct mdt_thread_info *info)
1691 {
1692         struct ptlrpc_request *req = mdt_info_req(info);
1693         struct req_capsule *pill = info->mti_pill;
1694         struct mdt_body *body;
1695         int rc;
1696         ENTRY;
1697
1698         /* The fid may be zero, so we req_capsule_set manually */
1699         req_capsule_set(pill, &RQF_MDS_SYNC);
1700
1701         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1702         if (body == NULL)
1703                 RETURN(err_serious(-EINVAL));
1704
1705         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1706                 RETURN(err_serious(-ENOMEM));
1707
1708         if (fid_seq(&body->fid1) == 0) {
1709                 /* sync the whole device */
1710                 rc = req_capsule_server_pack(pill);
1711                 if (rc == 0)
1712                         rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1713                 else
1714                         rc = err_serious(rc);
1715         } else {
1716                 /* sync an object */
1717                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1718                 if (rc == 0) {
1719                         rc = mdt_object_sync(info);
1720                         if (rc == 0) {
1721                                 struct md_object *next;
1722                                 const struct lu_fid *fid;
1723                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1724
1725                                 next = mdt_object_child(info->mti_object);
1726                                 info->mti_attr.ma_need = MA_INODE;
1727                                 info->mti_attr.ma_valid = 0;
1728                                 rc = mo_attr_get(info->mti_env, next,
1729                                                  &info->mti_attr);
1730                                 if (rc == 0) {
1731                                         body = req_capsule_server_get(pill,
1732                                                                 &RMF_MDT_BODY);
1733                                         fid = mdt_object_fid(info->mti_object);
1734                                         mdt_pack_attr2body(info, body, la, fid);
1735                                 }
1736                         }
1737                 } else
1738                         rc = err_serious(rc);
1739         }
1740         if (rc == 0)
1741                 mdt_counter_incr(req, LPROC_MDT_SYNC);
1742
1743         RETURN(rc);
1744 }
1745
1746 #ifdef HAVE_QUOTA_SUPPORT
1747 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1748 {
1749         struct obd_quotactl *oqctl;
1750         struct req_capsule *pill = info->mti_pill;
1751         struct obd_export *exp = info->mti_exp;
1752         struct md_quota *mq = md_quota(info->mti_env);
1753         struct md_device *next = info->mti_mdt->mdt_child;
1754         int rc;
1755         ENTRY;
1756
1757         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1758         if (oqctl == NULL)
1759                 RETURN(-EPROTO);
1760
1761         /* remote client has no permission for quotacheck */
1762         if (unlikely(exp_connect_rmtclient(exp)))
1763                 RETURN(-EPERM);
1764
1765         rc = req_capsule_server_pack(pill);
1766         if (rc)
1767                 RETURN(rc);
1768
1769         mq->mq_exp = exp;
1770         rc = next->md_ops->mdo_quota.mqo_check(info->mti_env, next,
1771                                                oqctl->qc_type);
1772         RETURN(rc);
1773 }
1774
1775 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1776 {
1777         struct obd_quotactl *oqctl, *repoqc;
1778         struct req_capsule *pill = info->mti_pill;
1779         struct obd_export *exp = info->mti_exp;
1780         struct md_quota *mq = md_quota(info->mti_env);
1781         struct md_device *next = info->mti_mdt->mdt_child;
1782         const struct md_quota_operations *mqo = &next->md_ops->mdo_quota;
1783         int id, rc;
1784         ENTRY;
1785
1786         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1787         if (oqctl == NULL)
1788                 RETURN(-EPROTO);
1789
1790         id = oqctl->qc_id;
1791         if (exp_connect_rmtclient(exp)) {
1792                 struct ptlrpc_request *req = mdt_info_req(info);
1793                 struct mdt_export_data *med = mdt_req2med(req);
1794                 struct lustre_idmap_table *idmap = med->med_idmap;
1795
1796                 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1797                              oqctl->qc_cmd != Q_GETINFO))
1798                         RETURN(-EPERM);
1799
1800
1801                 if (oqctl->qc_type == USRQUOTA)
1802                         id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1803                                                      oqctl->qc_id);
1804                 else if (oqctl->qc_type == GRPQUOTA)
1805                         id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1806                                                      oqctl->qc_id);
1807                 else
1808                         RETURN(-EINVAL);
1809
1810                 if (id == CFS_IDMAP_NOTFOUND) {
1811                         CDEBUG(D_QUOTA, "no mapping for id %u\n",
1812                                oqctl->qc_id);
1813                         RETURN(-EACCES);
1814                 }
1815         }
1816
1817         rc = req_capsule_server_pack(pill);
1818         if (rc)
1819                 RETURN(rc);
1820
1821         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1822         LASSERT(repoqc != NULL);
1823
1824         mq->mq_exp = exp;
1825         switch (oqctl->qc_cmd) {
1826         case Q_QUOTAON:
1827                 rc = mqo->mqo_on(info->mti_env, next, oqctl->qc_type);
1828                 break;
1829         case Q_QUOTAOFF:
1830                 rc = mqo->mqo_off(info->mti_env, next, oqctl->qc_type);
1831                 break;
1832         case Q_SETINFO:
1833                 rc = mqo->mqo_setinfo(info->mti_env, next, oqctl->qc_type, id,
1834                                       &oqctl->qc_dqinfo);
1835                 break;
1836         case Q_GETINFO:
1837                 rc = mqo->mqo_getinfo(info->mti_env, next, oqctl->qc_type, id,
1838                                       &oqctl->qc_dqinfo);
1839                 break;
1840         case Q_SETQUOTA:
1841                 rc = mqo->mqo_setquota(info->mti_env, next, oqctl->qc_type, id,
1842                                        &oqctl->qc_dqblk);
1843                 break;
1844         case Q_GETQUOTA:
1845                 rc = mqo->mqo_getquota(info->mti_env, next, oqctl->qc_type, id,
1846                                        &oqctl->qc_dqblk);
1847                 break;
1848         case Q_GETOINFO:
1849                 rc = mqo->mqo_getoinfo(info->mti_env, next, oqctl->qc_type, id,
1850                                        &oqctl->qc_dqinfo);
1851                 break;
1852         case Q_GETOQUOTA:
1853                 rc = mqo->mqo_getoquota(info->mti_env, next, oqctl->qc_type, id,
1854                                         &oqctl->qc_dqblk);
1855                 break;
1856         case LUSTRE_Q_INVALIDATE:
1857                 rc = mqo->mqo_invalidate(info->mti_env, next, oqctl->qc_type);
1858                 break;
1859         case LUSTRE_Q_FINVALIDATE:
1860                 rc = mqo->mqo_finvalidate(info->mti_env, next, oqctl->qc_type);
1861                 break;
1862         default:
1863                 CERROR("unsupported mdt_quotactl command: %d\n",
1864                        oqctl->qc_cmd);
1865                 RETURN(-EFAULT);
1866         }
1867
1868         *repoqc = *oqctl;
1869         RETURN(rc);
1870 }
1871 #endif
1872
1873
1874 /*
1875  * OBD PING and other handlers.
1876  */
1877 static int mdt_obd_ping(struct mdt_thread_info *info)
1878 {
1879         int rc;
1880         ENTRY;
1881
1882         req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1883
1884         rc = target_handle_ping(mdt_info_req(info));
1885         if (rc < 0)
1886                 rc = err_serious(rc);
1887         RETURN(rc);
1888 }
1889
1890 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1891 {
1892         return err_serious(-EOPNOTSUPP);
1893 }
1894
1895 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1896 {
1897         return err_serious(-EOPNOTSUPP);
1898 }
1899
1900
1901 /*
1902  * LLOG handlers.
1903  */
1904
1905 /** clone llog ctxt from child (mdd)
1906  * This allows remote llog (replicator) access.
1907  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
1908  * context was originally set up, or we can handle them directly.
1909  * I choose the latter, but that means I need any llog
1910  * contexts set up by child to be accessable by the mdt.  So we clone the
1911  * context into our context list here.
1912  */
1913 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
1914                                int idx)
1915 {
1916         struct md_device  *next = mdt->mdt_child;
1917         struct llog_ctxt *ctxt;
1918         int rc;
1919
1920         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
1921                 return 0;
1922
1923         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
1924         if (rc || ctxt == NULL) {
1925                 CERROR("Can't get mdd ctxt %d\n", rc);
1926                 return rc;
1927         }
1928
1929         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
1930         if (rc)
1931                 CERROR("Can't set mdt ctxt %d\n", rc);
1932
1933         return rc;
1934 }
1935
1936 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
1937                                  struct mdt_device *mdt, int idx)
1938 {
1939         struct llog_ctxt *ctxt;
1940
1941         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
1942         if (ctxt == NULL)
1943                 return 0;
1944         /* Put once for the get we just did, and once for the clone */
1945         llog_ctxt_put(ctxt);
1946         llog_ctxt_put(ctxt);
1947         return 0;
1948 }
1949
1950 static int mdt_llog_create(struct mdt_thread_info *info)
1951 {
1952         int rc;
1953
1954         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1955         rc = llog_origin_handle_create(mdt_info_req(info));
1956         return (rc < 0 ? err_serious(rc) : rc);
1957 }
1958
1959 static int mdt_llog_destroy(struct mdt_thread_info *info)
1960 {
1961         int rc;
1962
1963         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
1964         rc = llog_origin_handle_destroy(mdt_info_req(info));
1965         return (rc < 0 ? err_serious(rc) : rc);
1966 }
1967
1968 static int mdt_llog_read_header(struct mdt_thread_info *info)
1969 {
1970         int rc;
1971
1972         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1973         rc = llog_origin_handle_read_header(mdt_info_req(info));
1974         return (rc < 0 ? err_serious(rc) : rc);
1975 }
1976
1977 static int mdt_llog_next_block(struct mdt_thread_info *info)
1978 {
1979         int rc;
1980
1981         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1982         rc = llog_origin_handle_next_block(mdt_info_req(info));
1983         return (rc < 0 ? err_serious(rc) : rc);
1984 }
1985
1986 static int mdt_llog_prev_block(struct mdt_thread_info *info)
1987 {
1988         int rc;
1989
1990         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
1991         rc = llog_origin_handle_prev_block(mdt_info_req(info));
1992         return (rc < 0 ? err_serious(rc) : rc);
1993 }
1994
1995
1996 /*
1997  * DLM handlers.
1998  */
1999 static struct ldlm_callback_suite cbs = {
2000         .lcs_completion = ldlm_server_completion_ast,
2001         .lcs_blocking   = ldlm_server_blocking_ast,
2002         .lcs_glimpse    = NULL
2003 };
2004
2005 static int mdt_enqueue(struct mdt_thread_info *info)
2006 {
2007         struct ptlrpc_request *req;
2008         int rc;
2009
2010         /*
2011          * info->mti_dlm_req already contains swapped and (if necessary)
2012          * converted dlm request.
2013          */
2014         LASSERT(info->mti_dlm_req != NULL);
2015
2016         req = mdt_info_req(info);
2017         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2018                                   req, info->mti_dlm_req, &cbs);
2019         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2020         return rc ? err_serious(rc) : req->rq_status;
2021 }
2022
2023 static int mdt_convert(struct mdt_thread_info *info)
2024 {
2025         int rc;
2026         struct ptlrpc_request *req;
2027
2028         LASSERT(info->mti_dlm_req);
2029         req = mdt_info_req(info);
2030         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2031         return rc ? err_serious(rc) : req->rq_status;
2032 }
2033
2034 static int mdt_bl_callback(struct mdt_thread_info *info)
2035 {
2036         CERROR("bl callbacks should not happen on MDS\n");
2037         LBUG();
2038         return err_serious(-EOPNOTSUPP);
2039 }
2040
2041 static int mdt_cp_callback(struct mdt_thread_info *info)
2042 {
2043         CERROR("cp callbacks should not happen on MDS\n");
2044         LBUG();
2045         return err_serious(-EOPNOTSUPP);
2046 }
2047
2048 /*
2049  * sec context handlers
2050  */
2051 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2052 {
2053         int rc;
2054
2055         rc = mdt_handle_idmap(info);
2056
2057         if (unlikely(rc)) {
2058                 struct ptlrpc_request *req = mdt_info_req(info);
2059                 __u32                  opc;
2060
2061                 opc = lustre_msg_get_opc(req->rq_reqmsg);
2062                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2063                         sptlrpc_svc_ctx_invalidate(req);
2064         }
2065
2066         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2067
2068         return rc;
2069 }
2070
2071 static struct mdt_object *mdt_obj(struct lu_object *o)
2072 {
2073         LASSERT(lu_device_is_mdt(o->lo_dev));
2074         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2075 }
2076
2077 struct mdt_object *mdt_object_new(const struct lu_env *env,
2078                                   struct mdt_device *d,
2079                                   const struct lu_fid *f)
2080 {
2081         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2082         struct lu_object *o;
2083         struct mdt_object *m;
2084         ENTRY;
2085
2086         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2087         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2088         if (unlikely(IS_ERR(o)))
2089                 m = (struct mdt_object *)o;
2090         else
2091                 m = mdt_obj(o);
2092         RETURN(m);
2093 }
2094
2095 struct mdt_object *mdt_object_find(const struct lu_env *env,
2096                                    struct mdt_device *d,
2097                                    const struct lu_fid *f)
2098 {
2099         struct lu_object *o;
2100         struct mdt_object *m;
2101         ENTRY;
2102
2103         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2104         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2105         if (unlikely(IS_ERR(o)))
2106                 m = (struct mdt_object *)o;
2107         else
2108                 m = mdt_obj(o);
2109         RETURN(m);
2110 }
2111
2112 /**
2113  * Asyncronous commit for mdt device.
2114  *
2115  * Pass asynchonous commit call down the MDS stack.
2116  *
2117  * \param env environment
2118  * \param mdt the mdt device
2119  */
2120 static void mdt_device_commit_async(const struct lu_env *env,
2121                                     struct mdt_device *mdt)
2122 {
2123         struct dt_device *dt = mdt->mdt_bottom;
2124         int rc;
2125
2126         rc = dt->dd_ops->dt_commit_async(env, dt);
2127         if (unlikely(rc != 0))
2128                 CWARN("async commit start failed with rc = %d", rc);
2129 }
2130
2131 /**
2132  * Mark the lock as "synchonous".
2133  *
2134  * Mark the lock to deffer transaction commit to the unlock time.
2135  *
2136  * \param lock the lock to mark as "synchonous"
2137  *
2138  * \see mdt_is_lock_sync
2139  * \see mdt_save_lock
2140  */
2141 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2142 {
2143         lock->l_ast_data = (void*)1;
2144 }
2145
2146 /**
2147  * Check whehter the lock "synchonous" or not.
2148  *
2149  * \param lock the lock to check
2150  * \retval 1 the lock is "synchonous"
2151  * \retval 0 the lock isn't "synchronous"
2152  *
2153  * \see mdt_set_lock_sync
2154  * \see mdt_save_lock
2155  */
2156 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2157 {
2158         return lock->l_ast_data != NULL;
2159 }
2160
2161 /**
2162  * Blocking AST for mdt locks.
2163  *
2164  * Starts transaction commit if in case of COS lock conflict or
2165  * deffers such a commit to the mdt_save_lock.
2166  *
2167  * \param lock the lock which blocks a request or cancelling lock
2168  * \param desc unused
2169  * \param data unused
2170  * \param flag indicates whether this cancelling or blocking callback
2171  * \retval 0
2172  * \see ldlm_blocking_ast_nocheck
2173  */
2174 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2175                      void *data, int flag)
2176 {
2177         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2178         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2179         int rc;
2180         ENTRY;
2181
2182         if (flag == LDLM_CB_CANCELING)
2183                 RETURN(0);
2184         lock_res_and_lock(lock);
2185         if (lock->l_blocking_ast != mdt_blocking_ast) {
2186                 unlock_res_and_lock(lock);
2187                 RETURN(0);
2188         }
2189         if (mdt_cos_is_enabled(mdt) &&
2190             lock->l_req_mode & (LCK_PW | LCK_EX) &&
2191             lock->l_blocking_lock != NULL &&
2192             lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2193                 mdt_set_lock_sync(lock);
2194         }
2195         rc = ldlm_blocking_ast_nocheck(lock);
2196
2197         /* There is no lock conflict if l_blocking_lock == NULL,
2198          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2199          * when the last reference to a local lock was released */
2200         if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2201                 struct lu_env env;
2202
2203                 rc = lu_env_init(&env, LCT_LOCAL);
2204                 if (unlikely(rc != 0))
2205                         CWARN("lu_env initialization failed with rc = %d,"
2206                               "cannot start asynchronous commit\n", rc);
2207                 else
2208                         mdt_device_commit_async(&env, mdt);
2209                 lu_env_fini(&env);
2210         }
2211         RETURN(rc);
2212 }
2213
2214 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2215                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
2216 {
2217         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2218         ldlm_policy_data_t *policy = &info->mti_policy;
2219         struct ldlm_res_id *res_id = &info->mti_res_id;
2220         int rc;
2221         ENTRY;
2222
2223         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2224         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2225         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2226         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2227
2228         if (mdt_object_exists(o) < 0) {
2229                 if (locality == MDT_CROSS_LOCK) {
2230                         /* cross-ref object fix */
2231                         ibits &= ~MDS_INODELOCK_UPDATE;
2232                         ibits |= MDS_INODELOCK_LOOKUP;
2233                 } else {
2234                         LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2235                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2236                 }
2237                 /* No PDO lock on remote object */
2238                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2239         }
2240
2241         if (lh->mlh_type == MDT_PDO_LOCK) {
2242                 /* check for exists after object is locked */
2243                 if (mdt_object_exists(o) == 0) {
2244                         /* Non-existent object shouldn't have PDO lock */
2245                         RETURN(-ESTALE);
2246                 } else {
2247                         /* Non-dir object shouldn't have PDO lock */
2248                         if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2249                                 RETURN(-ENOTDIR);
2250                 }
2251         }
2252
2253         memset(policy, 0, sizeof(*policy));
2254         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2255
2256         /*
2257          * Take PDO lock on whole directory and build correct @res_id for lock
2258          * on part of directory.
2259          */
2260         if (lh->mlh_pdo_hash != 0) {
2261                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2262                 mdt_lock_pdo_mode(info, o, lh);
2263                 if (lh->mlh_pdo_mode != LCK_NL) {
2264                         /*
2265                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2266                          * is never going to be sent to client and we do not
2267                          * want it slowed down due to possible cancels.
2268                          */
2269                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2270                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2271                                           policy, res_id, LDLM_FL_ATOMIC_CB,
2272                                           &info->mti_exp->exp_handle.h_cookie);
2273                         if (unlikely(rc))
2274                                 RETURN(rc);
2275                 }
2276
2277                 /*
2278                  * Finish res_id initializing by name hash marking part of
2279                  * directory which is taking modification.
2280                  */
2281                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2282         }
2283
2284         policy->l_inodebits.bits = ibits;
2285
2286         /*
2287          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2288          * going to be sent to client. If it is - mdt_intent_policy() path will
2289          * fix it up and turn FL_LOCAL flag off.
2290          */
2291         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2292                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2293                           &info->mti_exp->exp_handle.h_cookie);
2294         if (rc)
2295                 mdt_object_unlock(info, o, lh, 1);
2296         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2297                  lh->mlh_pdo_hash != 0 &&
2298                  (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2299                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2300         }
2301
2302         RETURN(rc);
2303 }
2304
2305 /**
2306  * Save a lock within request object.
2307  *
2308  * Keep the lock referenced until whether client ACK or transaction
2309  * commit happens or release the lock immediately depending on input
2310  * parameters. If COS is ON, a write lock is converted to COS lock
2311  * before saving.
2312  *
2313  * \param info thead info object
2314  * \param h lock handle
2315  * \param mode lock mode
2316  * \param decref force immediate lock releasing
2317  */
2318 static
2319 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2320                    ldlm_mode_t mode, int decref)
2321 {
2322         ENTRY;
2323
2324         if (lustre_handle_is_used(h)) {
2325                 if (decref || !info->mti_has_trans ||
2326                     !(mode & (LCK_PW | LCK_EX))){
2327                         mdt_fid_unlock(h, mode);
2328                 } else {
2329                         struct mdt_device *mdt = info->mti_mdt;
2330                         struct ldlm_lock *lock = ldlm_handle2lock(h);
2331                         struct ptlrpc_request *req = mdt_info_req(info);
2332                         int no_ack = 0;
2333
2334                         LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2335                                  h->cookie);
2336                         CDEBUG(D_HA, "request = %p reply state = %p"
2337                                " transno = "LPD64"\n",
2338                                req, req->rq_reply_state, req->rq_transno);
2339                         if (mdt_cos_is_enabled(mdt)) {
2340                                 no_ack = 1;
2341                                 ldlm_lock_downgrade(lock, LCK_COS);
2342                                 mode = LCK_COS;
2343                         }
2344                         ptlrpc_save_lock(req, h, mode, no_ack);
2345                         if (mdt_is_lock_sync(lock)) {
2346                                 CDEBUG(D_HA, "found sync-lock,"
2347                                        " async commit started\n");
2348                                 mdt_device_commit_async(info->mti_env,
2349                                                         mdt);
2350                         }
2351                         LDLM_LOCK_PUT(lock);
2352                 }
2353                 h->cookie = 0ull;
2354         }
2355
2356         EXIT;
2357 }
2358
2359 /**
2360  * Unlock mdt object.
2361  *
2362  * Immeditely release the regular lock and the PDO lock or save the
2363  * lock in reqeuest and keep them referenced until client ACK or
2364  * transaction commit.
2365  *
2366  * \param info thread info object
2367  * \param o mdt object
2368  * \param lh mdt lock handle referencing regular and PDO locks
2369  * \param decref force immediate lock releasing
2370  */
2371 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2372                        struct mdt_lock_handle *lh, int decref)
2373 {
2374         ENTRY;
2375
2376         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2377         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2378
2379         EXIT;
2380 }
2381
2382 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2383                                         const struct lu_fid *f,
2384                                         struct mdt_lock_handle *lh,
2385                                         __u64 ibits)
2386 {
2387         struct mdt_object *o;
2388
2389         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2390         if (!IS_ERR(o)) {
2391                 int rc;
2392
2393                 rc = mdt_object_lock(info, o, lh, ibits,
2394                                      MDT_LOCAL_LOCK);
2395                 if (rc != 0) {
2396                         mdt_object_put(info->mti_env, o);
2397                         o = ERR_PTR(rc);
2398                 }
2399         }
2400         return o;
2401 }
2402
2403 void mdt_object_unlock_put(struct mdt_thread_info * info,
2404                            struct mdt_object * o,
2405                            struct mdt_lock_handle *lh,
2406                            int decref)
2407 {
2408         mdt_object_unlock(info, o, lh, decref);
2409         mdt_object_put(info->mti_env, o);
2410 }
2411
2412 static struct mdt_handler *mdt_handler_find(__u32 opc,
2413                                             struct mdt_opc_slice *supported)
2414 {
2415         struct mdt_opc_slice *s;
2416         struct mdt_handler   *h;
2417
2418         h = NULL;
2419         for (s = supported; s->mos_hs != NULL; s++) {
2420                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2421                         h = s->mos_hs + (opc - s->mos_opc_start);
2422                         if (likely(h->mh_opc != 0))
2423                                 LASSERTF(h->mh_opc == opc,
2424                                          "opcode mismatch %d != %d\n",
2425                                          h->mh_opc, opc);
2426                         else
2427                                 h = NULL; /* unsupported opc */
2428                         break;
2429                 }
2430         }
2431         return h;
2432 }
2433
2434 static int mdt_lock_resname_compat(struct mdt_device *m,
2435                                    struct ldlm_request *req)
2436 {
2437         /* XXX something... later. */
2438         return 0;
2439 }
2440
2441 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2442 {
2443         /* XXX something... later. */
2444         return 0;
2445 }
2446
2447 /*
2448  * Generic code handling requests that have struct mdt_body passed in:
2449  *
2450  *  - extract mdt_body from request and save it in @info, if present;
2451  *
2452  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
2453  *  @info;
2454  *
2455  *  - if HABEO_CORPUS flag is set for this request type check whether object
2456  *  actually exists on storage (lu_object_exists()).
2457  *
2458  */
2459 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2460 {
2461         const struct mdt_body    *body;
2462         struct mdt_object        *obj;
2463         const struct lu_env      *env;
2464         struct req_capsule       *pill;
2465         int                       rc;
2466         ENTRY;
2467
2468         env = info->mti_env;
2469         pill = info->mti_pill;
2470
2471         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2472         if (body == NULL)
2473                 RETURN(-EFAULT);
2474
2475         if (!(body->valid & OBD_MD_FLID))
2476                 RETURN(0);
2477
2478         if (!fid_is_sane(&body->fid1)) {
2479                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2480                 RETURN(-EINVAL);
2481         }
2482
2483         /*
2484          * Do not get size or any capa fields before we check that request
2485          * contains capa actually. There are some requests which do not, for
2486          * instance MDS_IS_SUBDIR.
2487          */
2488         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2489             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2490                 mdt_set_capainfo(info, 0, &body->fid1,
2491                                  req_capsule_client_get(pill, &RMF_CAPA1));
2492
2493         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2494         if (!IS_ERR(obj)) {
2495                 if ((flags & HABEO_CORPUS) &&
2496                     !mdt_object_exists(obj)) {
2497                         mdt_object_put(env, obj);
2498                         /* for capability renew ENOENT will be handled in
2499                          * mdt_renew_capa */
2500                         if (body->valid & OBD_MD_FLOSSCAPA)
2501                                 rc = 0;
2502                         else
2503                                 rc = -ENOENT;
2504                 } else {
2505                         info->mti_object = obj;
2506                         rc = 0;
2507                 }
2508         } else
2509                 rc = PTR_ERR(obj);
2510
2511         RETURN(rc);
2512 }
2513
2514 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2515 {
2516         struct req_capsule *pill = info->mti_pill;
2517         int rc;
2518         ENTRY;
2519
2520         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2521                 rc = mdt_body_unpack(info, flags);
2522         else
2523                 rc = 0;
2524
2525         if (rc == 0 && (flags & HABEO_REFERO)) {
2526                 /* Pack reply. */
2527                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2528                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2529                                              info->mti_body->eadatasize);
2530                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2531                         req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2532                                              info->mti_mdt->mdt_max_cookiesize);
2533
2534                 rc = req_capsule_server_pack(pill);
2535         }
2536         RETURN(rc);
2537 }
2538
2539 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2540 {
2541         struct md_device *next = m->mdt_child;
2542
2543         return next->md_ops->mdo_init_capa_ctxt(env, next,
2544                                                 m->mdt_opts.mo_mds_capa,
2545                                                 m->mdt_capa_timeout,
2546                                                 m->mdt_capa_alg,
2547                                                 m->mdt_capa_keys);
2548 }
2549
2550 /*
2551  * Invoke handler for this request opc. Also do necessary preprocessing
2552  * (according to handler ->mh_flags), and post-processing (setting of
2553  * ->last_{xid,committed}).
2554  */
2555 static int mdt_req_handle(struct mdt_thread_info *info,
2556                           struct mdt_handler *h, struct ptlrpc_request *req)
2557 {
2558         int   rc, serious = 0;
2559         __u32 flags;
2560
2561         ENTRY;
2562
2563         LASSERT(h->mh_act != NULL);
2564         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2565         LASSERT(current->journal_info == NULL);
2566
2567         /*
2568          * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2569          * to put same checks into handlers like mdt_close(), mdt_reint(),
2570          * etc., without talking to mdt authors first. Checking same thing
2571          * there again is useless and returning 0 error without packing reply
2572          * is buggy! Handlers either pack reply or return error.
2573          *
2574          * We return 0 here and do not send any reply in order to emulate
2575          * network failure. Do not send any reply in case any of NET related
2576          * fail_id has occured.
2577          */
2578         if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2579                 RETURN(0);
2580
2581         rc = 0;
2582         flags = h->mh_flags;
2583         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2584
2585         if (h->mh_fmt != NULL) {
2586                 req_capsule_set(info->mti_pill, h->mh_fmt);
2587                 rc = mdt_unpack_req_pack_rep(info, flags);
2588         }
2589
2590         if (rc == 0 && flags & MUTABOR &&
2591             req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2592                 /* should it be rq_status? */
2593                 rc = -EROFS;
2594
2595         if (rc == 0 && flags & HABEO_CLAVIS) {
2596                 struct ldlm_request *dlm_req;
2597
2598                 LASSERT(h->mh_fmt != NULL);
2599
2600                 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2601                 if (dlm_req != NULL) {
2602                         if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2603                                         LDLM_IBITS &&
2604                                      dlm_req->lock_desc.l_policy_data.\
2605                                         l_inodebits.bits == 0)) {
2606                                 /*
2607                                  * Lock without inodebits makes no sense and
2608                                  * will oops later in ldlm. If client miss to
2609                                  * set such bits, do not trigger ASSERTION.
2610                                  *
2611                                  * For liblustre flock case, it maybe zero.
2612                                  */
2613                                 rc = -EPROTO;
2614                         } else {
2615                                 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2616                                         rc = mdt_lock_resname_compat(
2617                                                                 info->mti_mdt,
2618                                                                 dlm_req);
2619                                 info->mti_dlm_req = dlm_req;
2620                         }
2621                 } else {
2622                         rc = -EFAULT;
2623                 }
2624         }
2625
2626         /* capability setting changed via /proc, needs reinitialize ctxt */
2627         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2628                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2629                 info->mti_mdt->mdt_capa_conf = 0;
2630         }
2631
2632         if (likely(rc == 0)) {
2633                 /*
2634                  * Process request, there can be two types of rc:
2635                  * 1) errors with msg unpack/pack, other failures outside the
2636                  * operation itself. This is counted as serious errors;
2637                  * 2) errors during fs operation, should be placed in rq_status
2638                  * only
2639                  */
2640                 rc = h->mh_act(info);
2641                 if (rc == 0 &&
2642                     !req->rq_no_reply && req->rq_reply_state == NULL) {
2643                         DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2644                                   "pack reply and returned 0 error\n",
2645                                   h->mh_name);
2646                         LBUG();
2647                 }
2648                 serious = is_serious(rc);
2649                 rc = clear_serious(rc);
2650         } else
2651                 serious = 1;
2652
2653         req->rq_status = rc;
2654
2655         /*
2656          * ELDLM_* codes which > 0 should be in rq_status only as well as
2657          * all non-serious errors.
2658          */
2659         if (rc > 0 || !serious)
2660                 rc = 0;
2661
2662         LASSERT(current->journal_info == NULL);
2663
2664         if (rc == 0 && (flags & HABEO_CLAVIS) &&
2665             info->mti_mdt->mdt_opts.mo_compat_resname) {
2666                 struct ldlm_reply *dlmrep;
2667
2668                 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2669                 if (dlmrep != NULL)
2670                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2671         }
2672
2673         /* If we're DISCONNECTing, the mdt_export_data is already freed */
2674         if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2675                 target_committed_to_req(req);
2676
2677         if (unlikely(req_is_replay(req) &&
2678                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2679                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2680                 LBUG();
2681         }
2682
2683         target_send_reply(req, rc, info->mti_fail_id);
2684         RETURN(0);
2685 }
2686
2687 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2688 {
2689         lh->mlh_type = MDT_NUL_LOCK;
2690         lh->mlh_reg_lh.cookie = 0ull;
2691         lh->mlh_reg_mode = LCK_MINMODE;
2692         lh->mlh_pdo_lh.cookie = 0ull;
2693         lh->mlh_pdo_mode = LCK_MINMODE;
2694 }
2695
2696 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2697 {
2698         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2699         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2700 }
2701
2702 /*
2703  * Initialize fields of struct mdt_thread_info. Other fields are left in
2704  * uninitialized state, because it's too expensive to zero out whole
2705  * mdt_thread_info (> 1K) on each request arrival.
2706  */
2707 static void mdt_thread_info_init(struct ptlrpc_request *req,
2708                                  struct mdt_thread_info *info)
2709 {
2710         int i;
2711         struct md_capainfo *ci;
2712
2713         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2714         info->mti_pill = &req->rq_pill;
2715
2716         /* lock handle */
2717         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2718                 mdt_lock_handle_init(&info->mti_lh[i]);
2719
2720         /* mdt device: it can be NULL while CONNECT */
2721         if (req->rq_export) {
2722                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2723                 info->mti_exp = req->rq_export;
2724         } else
2725                 info->mti_mdt = NULL;
2726         info->mti_env = req->rq_svc_thread->t_env;
2727         ci = md_capainfo(info->mti_env);
2728         memset(ci, 0, sizeof *ci);
2729         if (req->rq_export) {
2730                 if (exp_connect_rmtclient(req->rq_export))
2731                         ci->mc_auth = LC_ID_CONVERT;
2732                 else if (req->rq_export->exp_connect_flags &
2733                          OBD_CONNECT_MDS_CAPA)
2734                         ci->mc_auth = LC_ID_PLAIN;
2735                 else
2736                         ci->mc_auth = LC_ID_NONE;
2737         }
2738
2739         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2740         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2741         info->mti_mos = NULL;
2742
2743         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2744         info->mti_body = NULL;
2745         info->mti_object = NULL;
2746         info->mti_dlm_req = NULL;
2747         info->mti_has_trans = 0;
2748         info->mti_no_need_trans = 0;
2749         info->mti_cross_ref = 0;
2750         info->mti_opdata = 0;
2751
2752         /* To not check for split by default. */
2753         info->mti_spec.sp_ck_split = 0;
2754         info->mti_spec.no_create = 0;
2755 }
2756
2757 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2758 {
2759         int i;
2760
2761         req_capsule_fini(info->mti_pill);
2762         if (info->mti_object != NULL) {
2763                 /*
2764                  * freeing an object may lead to OSD level transaction, do not
2765                  * let it mess with MDT. bz19385.
2766                  */
2767                 info->mti_no_need_trans = 1;
2768                 mdt_object_put(info->mti_env, info->mti_object);
2769                 info->mti_object = NULL;
2770         }
2771         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2772                 mdt_lock_handle_fini(&info->mti_lh[i]);
2773         info->mti_env = NULL;
2774 }
2775
2776 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2777                                        struct obd_device *obd, int *process)
2778 {
2779         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2780         case MDS_CONNECT: /* This will never get here, but for completeness. */
2781         case OST_CONNECT: /* This will never get here, but for completeness. */
2782         case MDS_DISCONNECT:
2783         case OST_DISCONNECT:
2784                *process = 1;
2785                RETURN(0);
2786
2787         case MDS_CLOSE:
2788         case MDS_DONE_WRITING:
2789         case MDS_SYNC: /* used in unmounting */
2790         case OBD_PING:
2791         case MDS_REINT:
2792         case SEQ_QUERY:
2793         case FLD_QUERY:
2794         case LDLM_ENQUEUE:
2795                 *process = target_queue_recovery_request(req, obd);
2796                 RETURN(0);
2797
2798         default:
2799                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2800                 *process = -EAGAIN;
2801                 RETURN(0);
2802         }
2803 }
2804
2805 /*
2806  * Handle recovery. Return:
2807  *        +1: continue request processing;
2808  *       -ve: abort immediately with the given error code;
2809  *         0: send reply with error code in req->rq_status;
2810  */
2811 static int mdt_recovery(struct mdt_thread_info *info)
2812 {
2813         struct ptlrpc_request *req = mdt_info_req(info);
2814         struct obd_device *obd;
2815
2816         ENTRY;
2817
2818         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2819         case MDS_CONNECT:
2820         case SEC_CTX_INIT:
2821         case SEC_CTX_INIT_CONT:
2822         case SEC_CTX_FINI:
2823                 {
2824 #if 0
2825                         int rc;
2826
2827                         rc = mdt_handle_idmap(info);
2828                         if (rc)
2829                                 RETURN(rc);
2830                         else
2831 #endif
2832                                 RETURN(+1);
2833                 }
2834         }
2835
2836         if (unlikely(!class_connected_export(req->rq_export))) {
2837                 CERROR("operation %d on unconnected MDS from %s\n",
2838                        lustre_msg_get_opc(req->rq_reqmsg),
2839                        libcfs_id2str(req->rq_peer));
2840                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2841                  * mds_A will get -ENOTCONN(especially for ping req),
2842                  * which will cause that mds_A deactive timeout, then when
2843                  * mds_A cleanup, the cleanup process will be suspended since
2844                  * deactive timeout is not zero.
2845                  */
2846                 req->rq_status = -ENOTCONN;
2847                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2848                 RETURN(0);
2849         }
2850
2851         /* sanity check: if the xid matches, the request must be marked as a
2852          * resent or replayed */
2853         if (req_xid_is_last(req)) {
2854                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2855                       (MSG_RESENT | MSG_REPLAY))) {
2856                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2857                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2858                                   lustre_msg_get_flags(req->rq_reqmsg));
2859                         LBUG();
2860                         req->rq_status = -ENOTCONN;
2861                         RETURN(-ENOTCONN);
2862                 }
2863         }
2864
2865         /* else: note the opposite is not always true; a RESENT req after a
2866          * failover will usually not match the last_xid, since it was likely
2867          * never committed. A REPLAYed request will almost never match the
2868          * last xid, however it could for a committed, but still retained,
2869          * open. */
2870
2871         obd = req->rq_export->exp_obd;
2872
2873         /* Check for aborted recovery... */
2874         if (unlikely(obd->obd_recovering)) {
2875                 int rc;
2876                 int should_process;
2877                 DEBUG_REQ(D_INFO, req, "Got new replay");
2878                 rc = mdt_filter_recovery_request(req, obd, &should_process);
2879                 if (rc != 0 || !should_process)
2880                         RETURN(rc);
2881                 else if (should_process < 0) {
2882                         req->rq_status = should_process;
2883                         rc = ptlrpc_error(req);
2884                         RETURN(rc);
2885                 }
2886         }
2887         RETURN(+1);
2888 }
2889
2890 static int mdt_msg_check_version(struct lustre_msg *msg)
2891 {
2892         int rc;
2893
2894         switch (lustre_msg_get_opc(msg)) {
2895         case MDS_CONNECT:
2896         case MDS_DISCONNECT:
2897         case OBD_PING:
2898         case SEC_CTX_INIT:
2899         case SEC_CTX_INIT_CONT:
2900         case SEC_CTX_FINI:
2901                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2902                 if (rc)
2903                         CERROR("bad opc %u version %08x, expecting %08x\n",
2904                                lustre_msg_get_opc(msg),
2905                                lustre_msg_get_version(msg),
2906                                LUSTRE_OBD_VERSION);
2907                 break;
2908         case MDS_GETSTATUS:
2909         case MDS_GETATTR:
2910         case MDS_GETATTR_NAME:
2911         case MDS_STATFS:
2912         case MDS_READPAGE:
2913         case MDS_WRITEPAGE:
2914         case MDS_IS_SUBDIR:
2915         case MDS_REINT:
2916         case MDS_CLOSE:
2917         case MDS_DONE_WRITING:
2918         case MDS_PIN:
2919         case MDS_SYNC:
2920         case MDS_GETXATTR:
2921         case MDS_SETXATTR:
2922         case MDS_SET_INFO:
2923         case MDS_GET_INFO:
2924         case MDS_QUOTACHECK:
2925         case MDS_QUOTACTL:
2926         case QUOTA_DQACQ:
2927         case QUOTA_DQREL:
2928         case SEQ_QUERY:
2929         case FLD_QUERY:
2930                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2931                 if (rc)
2932                         CERROR("bad opc %u version %08x, expecting %08x\n",
2933                                lustre_msg_get_opc(msg),
2934                                lustre_msg_get_version(msg),
2935                                LUSTRE_MDS_VERSION);
2936                 break;
2937         case LDLM_ENQUEUE:
2938         case LDLM_CONVERT:
2939         case LDLM_BL_CALLBACK:
2940         case LDLM_CP_CALLBACK:
2941                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2942                 if (rc)
2943                         CERROR("bad opc %u version %08x, expecting %08x\n",
2944                                lustre_msg_get_opc(msg),
2945                                lustre_msg_get_version(msg),
2946                                LUSTRE_DLM_VERSION);
2947                 break;
2948         case OBD_LOG_CANCEL:
2949         case LLOG_ORIGIN_HANDLE_CREATE:
2950         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2951         case LLOG_ORIGIN_HANDLE_READ_HEADER:
2952         case LLOG_ORIGIN_HANDLE_CLOSE:
2953         case LLOG_ORIGIN_HANDLE_DESTROY:
2954         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2955         case LLOG_CATINFO:
2956                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2957                 if (rc)
2958                         CERROR("bad opc %u version %08x, expecting %08x\n",
2959                                lustre_msg_get_opc(msg),
2960                                lustre_msg_get_version(msg),
2961                                LUSTRE_LOG_VERSION);
2962                 break;
2963         default:
2964                 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
2965                 rc = -ENOTSUPP;
2966         }
2967         return rc;
2968 }
2969
2970 static int mdt_handle0(struct ptlrpc_request *req,
2971                        struct mdt_thread_info *info,
2972                        struct mdt_opc_slice *supported)
2973 {
2974         struct mdt_handler *h;
2975         struct lustre_msg  *msg;
2976         int                 rc;
2977
2978         ENTRY;
2979
2980         if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2981                 RETURN(0);
2982
2983         LASSERT(current->journal_info == NULL);
2984
2985         msg = req->rq_reqmsg;
2986         rc = mdt_msg_check_version(msg);
2987         if (likely(rc == 0)) {
2988                 rc = mdt_recovery(info);
2989                 if (likely(rc == +1)) {
2990                         h = mdt_handler_find(lustre_msg_get_opc(msg),
2991                                              supported);
2992                         if (likely(h != NULL)) {
2993                                 rc = mdt_req_handle(info, h, req);
2994                         } else {
2995                                 CERROR("The unsupported opc: 0x%x\n",
2996                                        lustre_msg_get_opc(msg) );
2997                                 req->rq_status = -ENOTSUPP;
2998                                 rc = ptlrpc_error(req);
2999                                 RETURN(rc);
3000                         }
3001                 }
3002         } else
3003                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3004         RETURN(rc);
3005 }
3006
3007 /*
3008  * MDT handler function called by ptlrpc service thread when request comes.
3009  *
3010  * XXX common "target" functionality should be factored into separate module
3011  * shared by mdt, ost and stand-alone services like fld.
3012  */
3013 static int mdt_handle_common(struct ptlrpc_request *req,
3014                              struct mdt_opc_slice *supported)
3015 {
3016         struct lu_env          *env;
3017         struct mdt_thread_info *info;
3018         int                     rc;
3019         ENTRY;
3020
3021         env = req->rq_svc_thread->t_env;
3022         LASSERT(env != NULL);
3023         LASSERT(env->le_ses != NULL);
3024         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3025         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3026         LASSERT(info != NULL);
3027
3028         mdt_thread_info_init(req, info);
3029
3030         rc = mdt_handle0(req, info, supported);
3031
3032         mdt_thread_info_fini(info);
3033         RETURN(rc);
3034 }
3035
3036 /*
3037  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3038  * as well.
3039  */
3040 int mdt_recovery_handle(struct ptlrpc_request *req)
3041 {
3042         int rc;
3043         ENTRY;
3044
3045         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3046         case FLD_QUERY:
3047                 rc = mdt_handle_common(req, mdt_fld_handlers);
3048                 break;
3049         case SEQ_QUERY:
3050                 rc = mdt_handle_common(req, mdt_seq_handlers);
3051                 break;
3052         default:
3053                 rc = mdt_handle_common(req, mdt_regular_handlers);
3054                 break;
3055         }
3056
3057         RETURN(rc);
3058 }
3059
3060 static int mdt_regular_handle(struct ptlrpc_request *req)
3061 {
3062         return mdt_handle_common(req, mdt_regular_handlers);
3063 }
3064
3065 static int mdt_readpage_handle(struct ptlrpc_request *req)
3066 {
3067         return mdt_handle_common(req, mdt_readpage_handlers);
3068 }
3069
3070 static int mdt_xmds_handle(struct ptlrpc_request *req)
3071 {
3072         return mdt_handle_common(req, mdt_xmds_handlers);
3073 }
3074
3075 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3076 {
3077         return mdt_handle_common(req, mdt_seq_handlers);
3078 }
3079
3080 static int mdt_mdss_handle(struct ptlrpc_request *req)
3081 {
3082         return mdt_handle_common(req, mdt_seq_handlers);
3083 }
3084
3085 static int mdt_dtss_handle(struct ptlrpc_request *req)
3086 {
3087         return mdt_handle_common(req, mdt_seq_handlers);
3088 }
3089
3090 static int mdt_fld_handle(struct ptlrpc_request *req)
3091 {
3092         return mdt_handle_common(req, mdt_fld_handlers);
3093 }
3094
3095 enum mdt_it_code {
3096         MDT_IT_OPEN,
3097         MDT_IT_OCREAT,
3098         MDT_IT_CREATE,
3099         MDT_IT_GETATTR,
3100         MDT_IT_READDIR,
3101         MDT_IT_LOOKUP,
3102         MDT_IT_UNLINK,
3103         MDT_IT_TRUNC,
3104         MDT_IT_GETXATTR,
3105         MDT_IT_LAYOUT,
3106         MDT_IT_NR
3107 };
3108
3109 static int mdt_intent_getattr(enum mdt_it_code opcode,
3110                               struct mdt_thread_info *info,
3111                               struct ldlm_lock **,
3112                               int);
3113 static int mdt_intent_reint(enum mdt_it_code opcode,
3114                             struct mdt_thread_info *info,
3115                             struct ldlm_lock **,
3116                             int);
3117
3118 static struct mdt_it_flavor {
3119         const struct req_format *it_fmt;
3120         __u32                    it_flags;
3121         int                    (*it_act)(enum mdt_it_code ,
3122                                          struct mdt_thread_info *,
3123                                          struct ldlm_lock **,
3124                                          int);
3125         long                     it_reint;
3126 } mdt_it_flavor[] = {
3127         [MDT_IT_OPEN]     = {
3128                 .it_fmt   = &RQF_LDLM_INTENT,
3129                 /*.it_flags = HABEO_REFERO,*/
3130                 .it_flags = 0,
3131                 .it_act   = mdt_intent_reint,
3132                 .it_reint = REINT_OPEN
3133         },
3134         [MDT_IT_OCREAT]   = {
3135                 .it_fmt   = &RQF_LDLM_INTENT,
3136                 .it_flags = MUTABOR,
3137                 .it_act   = mdt_intent_reint,
3138                 .it_reint = REINT_OPEN
3139         },
3140         [MDT_IT_CREATE]   = {
3141                 .it_fmt   = &RQF_LDLM_INTENT,
3142                 .it_flags = MUTABOR,
3143                 .it_act   = mdt_intent_reint,
3144                 .it_reint = REINT_CREATE
3145         },
3146         [MDT_IT_GETATTR]  = {
3147                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3148                 .it_flags = HABEO_REFERO,
3149                 .it_act   = mdt_intent_getattr
3150         },
3151         [MDT_IT_READDIR]  = {
3152                 .it_fmt   = NULL,
3153                 .it_flags = 0,
3154                 .it_act   = NULL
3155         },
3156         [MDT_IT_LOOKUP]   = {
3157                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3158                 .it_flags = HABEO_REFERO,
3159                 .it_act   = mdt_intent_getattr
3160         },
3161         [MDT_IT_UNLINK]   = {
3162                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
3163                 .it_flags = MUTABOR,
3164                 .it_act   = NULL,
3165                 .it_reint = REINT_UNLINK
3166         },
3167         [MDT_IT_TRUNC]    = {
3168                 .it_fmt   = NULL,
3169                 .it_flags = MUTABOR,
3170                 .it_act   = NULL
3171         },
3172         [MDT_IT_GETXATTR] = {
3173                 .it_fmt   = NULL,
3174                 .it_flags = 0,
3175                 .it_act   = NULL
3176         },
3177         [MDT_IT_LAYOUT] = {
3178                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3179                 .it_flags = HABEO_REFERO,
3180                 .it_act   = mdt_intent_getattr
3181         }
3182 };
3183
3184 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3185                             struct ldlm_lock **lockp,
3186                             struct ldlm_lock *new_lock,
3187                             struct mdt_lock_handle *lh,
3188                             int flags)
3189 {
3190         struct ptlrpc_request  *req = mdt_info_req(info);
3191         struct ldlm_lock       *lock = *lockp;
3192
3193         /*
3194          * Get new lock only for cases when possible resent did not find any
3195          * lock.
3196          */
3197         if (new_lock == NULL)
3198                 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3199
3200         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3201                 lh->mlh_reg_lh.cookie = 0;
3202                 RETURN(0);
3203         }
3204
3205         LASSERTF(new_lock != NULL,
3206                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3207
3208         /*
3209          * If we've already given this lock to a client once, then we should
3210          * have no readers or writers.  Otherwise, we should have one reader
3211          * _or_ writer ref (which will be zeroed below) before returning the
3212          * lock to a client.
3213          */
3214         if (new_lock->l_export == req->rq_export) {
3215                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3216         } else {
3217                 LASSERT(new_lock->l_export == NULL);
3218                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3219         }
3220
3221         *lockp = new_lock;
3222
3223         if (new_lock->l_export == req->rq_export) {
3224                 /*
3225                  * Already gave this to the client, which means that we
3226                  * reconstructed a reply.
3227                  */
3228                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3229                         MSG_RESENT);
3230                 lh->mlh_reg_lh.cookie = 0;
3231                 RETURN(ELDLM_LOCK_REPLACED);
3232         }
3233
3234         /*
3235          * Fixup the lock to be given to the client.
3236          */
3237         lock_res_and_lock(new_lock);
3238         /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3239          * possible blocking AST. */
3240         while (new_lock->l_readers > 0) {
3241                 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3242                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3243                 new_lock->l_readers--;
3244         }
3245         while (new_lock->l_writers > 0) {
3246                 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3247                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3248                 new_lock->l_writers--;
3249         }
3250
3251         new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3252         new_lock->l_blocking_ast = lock->l_blocking_ast;
3253         new_lock->l_completion_ast = lock->l_completion_ast;
3254         new_lock->l_remote_handle = lock->l_remote_handle;
3255         new_lock->l_flags &= ~LDLM_FL_LOCAL;
3256
3257         unlock_res_and_lock(new_lock);
3258
3259         cfs_hash_add(new_lock->l_export->exp_lock_hash,
3260                      &new_lock->l_remote_handle,
3261                      &new_lock->l_exp_hash);
3262
3263         LDLM_LOCK_RELEASE(new_lock);
3264         lh->mlh_reg_lh.cookie = 0;
3265
3266         RETURN(ELDLM_LOCK_REPLACED);
3267 }
3268
3269 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3270                                     struct ldlm_lock *new_lock,
3271                                     struct ldlm_lock **old_lock,
3272                                     struct mdt_lock_handle *lh)
3273 {
3274         struct ptlrpc_request  *req = mdt_info_req(info);
3275         struct obd_export      *exp = req->rq_export;
3276         struct lustre_handle    remote_hdl;
3277         struct ldlm_request    *dlmreq;
3278         struct ldlm_lock       *lock;
3279
3280         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3281                 return;
3282
3283         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3284         remote_hdl = dlmreq->lock_handle[0];
3285
3286         lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3287         if (lock) {
3288                 if (lock != new_lock) {
3289                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3290                         lh->mlh_reg_mode = lock->l_granted_mode;
3291
3292                         LDLM_DEBUG(lock, "Restoring lock cookie");
3293                         DEBUG_REQ(D_DLMTRACE, req,
3294                                   "restoring lock cookie "LPX64,
3295                                   lh->mlh_reg_lh.cookie);
3296                         if (old_lock)
3297                                 *old_lock = LDLM_LOCK_GET(lock);
3298                         cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3299                         return;
3300                 }
3301
3302                 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3303         }
3304
3305         /*
3306          * If the xid matches, then we know this is a resent request, and allow
3307          * it. (It's probably an OPEN, for which we don't send a lock.
3308          */
3309         if (req_xid_is_last(req))
3310                 return;
3311
3312         /*
3313          * This remote handle isn't enqueued, so we never received or processed
3314          * this request.  Clear MSG_RESENT, because it can be handled like any
3315          * normal request now.
3316          */
3317         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3318
3319         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3320                   remote_hdl.cookie);
3321 }
3322
3323 static int mdt_intent_getattr(enum mdt_it_code opcode,
3324                               struct mdt_thread_info *info,
3325                               struct ldlm_lock **lockp,
3326                               int flags)
3327 {
3328         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3329         struct ldlm_lock       *new_lock = NULL;
3330         __u64                   child_bits;
3331         struct ldlm_reply      *ldlm_rep;
3332         struct ptlrpc_request  *req;
3333         struct mdt_body        *reqbody;
3334         struct mdt_body        *repbody;
3335         int                     rc, rc2;
3336         ENTRY;
3337
3338         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3339         LASSERT(reqbody);
3340
3341         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3342         LASSERT(repbody);
3343
3344         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
3345         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3346         repbody->eadatasize = 0;
3347         repbody->aclsize = 0;
3348
3349         switch (opcode) {
3350         case MDT_IT_LOOKUP:
3351                 child_bits = MDS_INODELOCK_LOOKUP;
3352                 break;
3353         case MDT_IT_GETATTR:
3354                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3355                 break;
3356         case MDT_IT_LAYOUT: {
3357                 static int printed = 0;
3358
3359                 if (!printed) {
3360                         CERROR("layout lock not supported by this version\n");
3361                         printed = 1;
3362                 }
3363                 GOTO(out_shrink, rc = -EINVAL);
3364                 break;
3365         }
3366         default:
3367                 CERROR("Unsupported intent (%d)\n", opcode);
3368                 GOTO(out_shrink, rc = -EINVAL);
3369         }
3370
3371         rc = mdt_init_ucred(info, reqbody);
3372         if (rc)
3373                 GOTO(out_shrink, rc);
3374
3375         req = info->mti_pill->rc_req;
3376         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3377         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3378
3379         /* Get lock from request for possible resent case. */
3380         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3381
3382         ldlm_rep->lock_policy_res2 =
3383                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3384
3385         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3386                 ldlm_rep->lock_policy_res2 = 0;
3387         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3388             ldlm_rep->lock_policy_res2) {
3389                 lhc->mlh_reg_lh.cookie = 0ull;
3390                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3391         }
3392
3393         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3394         EXIT;
3395 out_ucred:
3396         mdt_exit_ucred(info);
3397 out_shrink:
3398         mdt_client_compatibility(info);
3399         rc2 = mdt_fix_reply(info);
3400         if (rc == 0)
3401                 rc = rc2;
3402         return rc;
3403 }
3404
3405 static int mdt_intent_reint(enum mdt_it_code opcode,
3406                             struct mdt_thread_info *info,
3407                             struct ldlm_lock **lockp,
3408                             int flags)
3409 {
3410         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3411         struct ldlm_reply      *rep = NULL;
3412         long                    opc;
3413         int                     rc;
3414
3415         static const struct req_format *intent_fmts[REINT_MAX] = {
3416                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
3417                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
3418         };
3419
3420         ENTRY;
3421
3422         opc = mdt_reint_opcode(info, intent_fmts);
3423         if (opc < 0)
3424                 RETURN(opc);
3425
3426         if (mdt_it_flavor[opcode].it_reint != opc) {
3427                 CERROR("Reint code %ld doesn't match intent: %d\n",
3428                        opc, opcode);
3429                 RETURN(err_serious(-EPROTO));
3430         }
3431
3432         /* Get lock from request for possible resent case. */
3433         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3434
3435         rc = mdt_reint_internal(info, lhc, opc);
3436
3437         /* Check whether the reply has been packed successfully. */
3438         if (mdt_info_req(info)->rq_repmsg != NULL)
3439                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3440         if (rep == NULL)
3441                 RETURN(err_serious(-EFAULT));
3442
3443         /* MDC expects this in any case */
3444         if (rc != 0)
3445                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3446
3447         /* Cross-ref case, the lock should be returned to the client */
3448         if (rc == -EREMOTE) {
3449                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3450                 rep->lock_policy_res2 = 0;
3451                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3452                 RETURN(rc);
3453         }
3454         rep->lock_policy_res2 = clear_serious(rc);
3455
3456         if (rep->lock_policy_res2 == -ENOENT &&
3457             mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3458                 rep->lock_policy_res2 = 0;
3459
3460         if (rc == -ENOTCONN || rc == -ENODEV ||
3461             rc == -EOVERFLOW) { /**< if VBR failure then return error */
3462                 /*
3463                  * If it is the disconnect error (ENODEV & ENOCONN), the error
3464                  * will be returned by rq_status, and client at ptlrpc layer
3465                  * will detect this, then disconnect, reconnect the import
3466                  * immediately, instead of impacting the following the rpc.
3467                  */
3468                 lhc->mlh_reg_lh.cookie = 0ull;
3469                 RETURN(rc);
3470         } else {
3471                 /*
3472                  * For other cases, the error will be returned by intent.
3473                  * and client will retrieve the result from intent.
3474                  */
3475                  /*
3476                   * FIXME: when open lock is finished, that should be
3477                   * checked here.
3478                   */
3479                 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3480                         LASSERTF(rc == 0, "Error occurred but lock handle "
3481                                  "is still in use\n");
3482                         rep->lock_policy_res2 = 0;
3483                         rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3484                         RETURN(rc);
3485                 } else {
3486                         lhc->mlh_reg_lh.cookie = 0ull;
3487                         RETURN(ELDLM_LOCK_ABORTED);
3488                 }
3489         }
3490 }
3491
3492 static int mdt_intent_code(long itcode)
3493 {
3494         int rc;
3495
3496         switch(itcode) {
3497         case IT_OPEN:
3498                 rc = MDT_IT_OPEN;
3499                 break;
3500         case IT_OPEN|IT_CREAT:
3501                 rc = MDT_IT_OCREAT;
3502                 break;
3503         case IT_CREAT:
3504                 rc = MDT_IT_CREATE;
3505                 break;
3506         case IT_READDIR:
3507                 rc = MDT_IT_READDIR;
3508                 break;
3509         case IT_GETATTR:
3510                 rc = MDT_IT_GETATTR;
3511                 break;
3512         case IT_LOOKUP:
3513                 rc = MDT_IT_LOOKUP;
3514                 break;
3515         case IT_UNLINK:
3516                 rc = MDT_IT_UNLINK;
3517                 break;
3518         case IT_TRUNC:
3519                 rc = MDT_IT_TRUNC;
3520                 break;
3521         case IT_GETXATTR:
3522                 rc = MDT_IT_GETXATTR;
3523                 break;
3524         case IT_LAYOUT:
3525                 rc = MDT_IT_LAYOUT;
3526                 break;
3527         default:
3528                 CERROR("Unknown intent opcode: %ld\n", itcode);
3529                 rc = -EINVAL;
3530                 break;
3531         }
3532         return rc;
3533 }
3534
3535 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3536                           struct ldlm_lock **lockp, int flags)
3537 {
3538         struct req_capsule   *pill;
3539         struct mdt_it_flavor *flv;
3540         int opc;
3541         int rc;
3542         ENTRY;
3543
3544         opc = mdt_intent_code(itopc);
3545         if (opc < 0)
3546                 RETURN(-EINVAL);
3547
3548         pill = info->mti_pill;
3549         flv  = &mdt_it_flavor[opc];
3550
3551         if (flv->it_fmt != NULL)
3552                 req_capsule_extend(pill, flv->it_fmt);
3553
3554         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3555         if (rc == 0) {
3556                 struct ptlrpc_request *req = mdt_info_req(info);
3557                 if (flv->it_flags & MUTABOR &&
3558                     req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3559                         RETURN(-EROFS);
3560         }
3561         if (rc == 0 && flv->it_act != NULL) {
3562                 /* execute policy */
3563                 rc = flv->it_act(opc, info, lockp, flags);
3564         } else {
3565                 rc = -EOPNOTSUPP;
3566         }
3567         RETURN(rc);
3568 }
3569
3570 static int mdt_intent_policy(struct ldlm_namespace *ns,
3571                              struct ldlm_lock **lockp, void *req_cookie,
3572                              ldlm_mode_t mode, int flags, void *data)
3573 {
3574         struct mdt_thread_info *info;
3575         struct ptlrpc_request  *req  =  req_cookie;
3576         struct ldlm_intent     *it;
3577         struct req_capsule     *pill;
3578         int rc;
3579
3580         ENTRY;
3581
3582         LASSERT(req != NULL);
3583
3584         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3585                                   &mdt_thread_key);
3586         LASSERT(info != NULL);
3587         pill = info->mti_pill;
3588         LASSERT(pill->rc_req == req);
3589
3590         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3591                 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3592                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3593                 if (it != NULL) {
3594                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
3595                         if (rc == 0)
3596                                 rc = ELDLM_OK;
3597
3598                         /* Lock without inodebits makes no sense and will oops
3599                          * later in ldlm. Let's check it now to see if we have
3600                          * ibits corrupted somewhere in mdt_intent_opc().
3601                          * The case for client miss to set ibits has been
3602                          * processed by others. */
3603                         LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3604                                         lr_type == LDLM_IBITS,
3605                                      info->mti_dlm_req->lock_desc.\
3606                                         l_policy_data.l_inodebits.bits != 0));
3607                 } else
3608                         rc = err_serious(-EFAULT);
3609         } else {
3610                 /* No intent was provided */
3611                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3612                 rc = req_capsule_server_pack(pill);
3613                 if (rc)
3614                         rc = err_serious(rc);
3615         }
3616         RETURN(rc);
3617 }
3618
3619 static int mdt_seq_fini(const struct lu_env *env,
3620                         struct mdt_device *m)
3621 {
3622         struct md_site *ms = mdt_md_site(m);
3623         ENTRY;
3624
3625         if (ms != NULL) {
3626                 if (ms->ms_server_seq) {
3627                         seq_server_fini(ms->ms_server_seq, env);
3628                         OBD_FREE_PTR(ms->ms_server_seq);
3629                         ms->ms_server_seq = NULL;
3630         }
3631
3632                 if (ms->ms_control_seq) {
3633                         seq_server_fini(ms->ms_control_seq, env);
3634                         OBD_FREE_PTR(ms->ms_control_seq);
3635                         ms->ms_control_seq = NULL;
3636         }
3637
3638                 if (ms->ms_client_seq) {
3639                         seq_client_fini(ms->ms_client_seq);
3640                         OBD_FREE_PTR(ms->ms_client_seq);
3641                         ms->ms_client_seq = NULL;
3642                 }
3643         }
3644
3645         RETURN(0);
3646 }
3647
3648 static int mdt_seq_init(const struct lu_env *env,
3649                         const char *uuid,
3650                         struct mdt_device *m)
3651 {
3652         struct md_site *ms;
3653         char *prefix;
3654         int rc;
3655         ENTRY;
3656
3657         ms = mdt_md_site(m);
3658
3659         /*
3660          * This is sequence-controller node. Init seq-controller server on local
3661          * MDT.
3662          */
3663         if (ms->ms_node_id == 0) {
3664                 LASSERT(ms->ms_control_seq == NULL);
3665
3666                 OBD_ALLOC_PTR(ms->ms_control_seq);
3667                 if (ms->ms_control_seq == NULL)
3668                         RETURN(-ENOMEM);
3669
3670                 rc = seq_server_init(ms->ms_control_seq,
3671                                      m->mdt_bottom, uuid,
3672                                      LUSTRE_SEQ_CONTROLLER,
3673                                      ms,
3674                                      env);
3675
3676                 if (rc)
3677                         GOTO(out_seq_fini, rc);
3678
3679                 OBD_ALLOC_PTR(ms->ms_client_seq);
3680                 if (ms->ms_client_seq == NULL)
3681                         GOTO(out_seq_fini, rc = -ENOMEM);
3682
3683                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3684                 if (prefix == NULL) {
3685                         OBD_FREE_PTR(ms->ms_client_seq);
3686                         GOTO(out_seq_fini, rc = -ENOMEM);
3687                 }
3688
3689                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3690                          uuid);
3691
3692                 /*
3693                  * Init seq-controller client after seq-controller server is
3694                  * ready. Pass ms->ms_control_seq to it for direct talking.
3695                  */
3696                 rc = seq_client_init(ms->ms_client_seq, NULL,
3697                                      LUSTRE_SEQ_METADATA, prefix,
3698                                      ms->ms_control_seq);
3699                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3700
3701                 if (rc)
3702                         GOTO(out_seq_fini, rc);
3703         }
3704
3705         /* Init seq-server on local MDT */
3706         LASSERT(ms->ms_server_seq == NULL);
3707
3708         OBD_ALLOC_PTR(ms->ms_server_seq);
3709         if (ms->ms_server_seq == NULL)
3710                 GOTO(out_seq_fini, rc = -ENOMEM);
3711
3712         rc = seq_server_init(ms->ms_server_seq,
3713                              m->mdt_bottom, uuid,
3714                              LUSTRE_SEQ_SERVER,
3715                              ms,
3716                              env);
3717         if (rc)
3718                 GOTO(out_seq_fini, rc = -ENOMEM);
3719
3720         /* Assign seq-controller client to local seq-server. */
3721         if (ms->ms_node_id == 0) {
3722                 LASSERT(ms->ms_client_seq != NULL);
3723
3724                 rc = seq_server_set_cli(ms->ms_server_seq,
3725                                         ms->ms_client_seq,
3726                                         env);
3727         }
3728
3729         EXIT;
3730 out_seq_fini:
3731         if (rc)
3732                 mdt_seq_fini(env, m);
3733
3734         return rc;
3735 }
3736 /*
3737  * Init client sequence manager which is used by local MDS to talk to sequence
3738  * controller on remote node.
3739  */
3740 static int mdt_seq_init_cli(const struct lu_env *env,
3741                             struct mdt_device *m,
3742                             struct lustre_cfg *cfg)
3743 {
3744         struct md_site    *ms = mdt_md_site(m);
3745         struct obd_device *mdc;
3746         struct obd_uuid   *uuidp, *mdcuuidp;
3747         char              *uuid_str, *mdc_uuid_str;
3748         int                rc;
3749         int                index;
3750         struct mdt_thread_info *info;
3751         char *p, *index_string = lustre_cfg_string(cfg, 2);
3752         ENTRY;
3753
3754         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3755         uuidp = &info->mti_u.uuid[0];
3756         mdcuuidp = &info->mti_u.uuid[1];
3757
3758         LASSERT(index_string);
3759
3760         index = simple_strtol(index_string, &p, 10);
3761         if (*p) {
3762                 CERROR("Invalid index in lustre_cgf, offset 2\n");
3763                 RETURN(-EINVAL);
3764         }
3765
3766         /* check if this is adding the first MDC and controller is not yet
3767          * initialized. */
3768         if (index != 0 || ms->ms_client_seq)
3769                 RETURN(0);
3770
3771         uuid_str = lustre_cfg_string(cfg, 1);
3772         mdc_uuid_str = lustre_cfg_string(cfg, 4);
3773         obd_str2uuid(uuidp, uuid_str);
3774         obd_str2uuid(mdcuuidp, mdc_uuid_str);
3775
3776         mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3777         if (!mdc) {
3778                 CERROR("can't find controller MDC by uuid %s\n",
3779                        uuid_str);
3780                 rc = -ENOENT;
3781         } else if (!mdc->obd_set_up) {
3782                 CERROR("target %s not set up\n", mdc->obd_name);
3783                 rc = -EINVAL;
3784         } else {
3785                 LASSERT(ms->ms_control_exp);
3786                 OBD_ALLOC_PTR(ms->ms_client_seq);
3787                 if (ms->ms_client_seq != NULL) {
3788                         char *prefix;
3789
3790                         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3791                         if (!prefix)
3792                                 RETURN(-ENOMEM);
3793
3794                         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3795                                  mdc->obd_name);
3796
3797                         rc = seq_client_init(ms->ms_client_seq,
3798                                              ms->ms_control_exp,
3799                                              LUSTRE_SEQ_METADATA,
3800                                              prefix, NULL);
3801                         OBD_FREE(prefix, MAX_OBD_NAME + 5);
3802                 } else
3803                         rc = -ENOMEM;
3804
3805                 if (rc)
3806                         RETURN(rc);
3807
3808                 LASSERT(ms->ms_server_seq != NULL);
3809                 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
3810                                         env);
3811         }
3812
3813         RETURN(rc);
3814 }
3815
3816 static void mdt_seq_fini_cli(struct mdt_device *m)
3817 {
3818         struct md_site *ms;
3819
3820         ENTRY;
3821
3822         ms = mdt_md_site(m);
3823
3824         if (ms != NULL) {
3825                 if (ms->ms_server_seq)
3826                         seq_server_set_cli(ms->ms_server_seq,
3827                                    NULL, NULL);
3828
3829                 if (ms->ms_control_exp) {
3830                         class_export_put(ms->ms_control_exp);
3831                         ms->ms_control_exp = NULL;
3832                 }
3833         }
3834         EXIT;
3835 }
3836
3837 /*
3838  * FLD wrappers
3839  */
3840 static int mdt_fld_fini(const struct lu_env *env,
3841                         struct mdt_device *m)
3842 {
3843         struct md_site *ms = mdt_md_site(m);
3844         ENTRY;
3845
3846         if (ms && ms->ms_server_fld) {
3847                 fld_server_fini(ms->ms_server_fld, env);
3848                 OBD_FREE_PTR(ms->ms_server_fld);
3849                 ms->ms_server_fld = NULL;
3850         }
3851
3852         RETURN(0);
3853 }
3854
3855 static int mdt_fld_init(const struct lu_env *env,
3856                         const char *uuid,
3857                         struct mdt_device *m)
3858 {
3859         struct md_site *ms;
3860         int rc;
3861         ENTRY;
3862
3863         ms = mdt_md_site(m);
3864
3865         OBD_ALLOC_PTR(ms->ms_server_fld);
3866         if (ms->ms_server_fld == NULL)
3867                 RETURN(rc = -ENOMEM);
3868
3869         rc = fld_server_init(ms->ms_server_fld,
3870                              m->mdt_bottom, uuid,
3871                              env, ms->ms_node_id);
3872         if (rc) {
3873                 OBD_FREE_PTR(ms->ms_server_fld);
3874                 ms->ms_server_fld = NULL;
3875                 RETURN(rc);
3876         }
3877
3878         RETURN(0);
3879 }
3880
3881 /* device init/fini methods */
3882 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3883 {
3884         ENTRY;
3885         if (m->mdt_regular_service != NULL) {
3886                 ptlrpc_unregister_service(m->mdt_regular_service);
3887                 m->mdt_regular_service = NULL;
3888         }
3889         if (m->mdt_readpage_service != NULL) {
3890                 ptlrpc_unregister_service(m->mdt_readpage_service);
3891                 m->mdt_readpage_service = NULL;
3892         }
3893         if (m->mdt_xmds_service != NULL) {
3894                 ptlrpc_unregister_service(m->mdt_xmds_service);
3895                 m->mdt_xmds_service = NULL;
3896         }
3897         if (m->mdt_setattr_service != NULL) {
3898                 ptlrpc_unregister_service(m->mdt_setattr_service);
3899                 m->mdt_setattr_service = NULL;
3900         }
3901         if (m->mdt_mdsc_service != NULL) {
3902                 ptlrpc_unregister_service(m->mdt_mdsc_service);
3903                 m->mdt_mdsc_service = NULL;
3904         }
3905         if (m->mdt_mdss_service != NULL) {
3906                 ptlrpc_unregister_service(m->mdt_mdss_service);
3907                 m->mdt_mdss_service = NULL;
3908         }
3909         if (m->mdt_dtss_service != NULL) {
3910                 ptlrpc_unregister_service(m->mdt_dtss_service);
3911                 m->mdt_dtss_service = NULL;
3912         }
3913         if (m->mdt_fld_service != NULL) {
3914                 ptlrpc_unregister_service(m->mdt_fld_service);
3915                 m->mdt_fld_service = NULL;
3916         }
3917         EXIT;
3918 }
3919
3920 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3921 {
3922         static struct ptlrpc_service_conf conf;
3923         cfs_proc_dir_entry_t *procfs_entry;
3924         int rc = 0;
3925         ENTRY;
3926
3927         m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3928         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3929                            "mdt_ldlm_client", m->mdt_ldlm_client);
3930
3931         procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3932
3933         conf = (typeof(conf)) {
3934                 .psc_name               = LUSTRE_MDT_NAME,
3935                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
3936                 .psc_buf                = {
3937                         .bc_nbufs               = MDS_NBUFS,
3938                         .bc_buf_size            = MDS_BUFSIZE,
3939                         .bc_req_max_size        = MDS_MAXREQSIZE,
3940                         .bc_rep_max_size        = MDS_MAXREPSIZE,
3941                         .bc_req_portal          = MDS_REQUEST_PORTAL,
3942                         .bc_rep_portal          = MDC_REPLY_PORTAL,
3943                 },
3944                 /*
3945                  * We'd like to have a mechanism to set this on a per-device
3946                  * basis, but alas...
3947                  */
3948                 .psc_thr                = {
3949                         .tc_thr_name            = LUSTRE_MDT_NAME,
3950                         .tc_nthrs_min           = MDT_MIN_THREADS,
3951                         .tc_nthrs_max           = MDT_MAX_THREADS,
3952                         .tc_nthrs_user          = mdt_num_threads,
3953                         .tc_ctx_tags            = LCT_MD_THREAD,
3954                 },
3955                 .psc_ops                = {
3956                         .so_req_handler         = mdt_regular_handle,
3957                         .so_req_printer         = target_print_req,
3958                 },
3959         };
3960         m->mdt_regular_service = ptlrpc_register_service(&conf, procfs_entry);
3961         if (IS_ERR(m->mdt_regular_service)) {
3962                 rc = PTR_ERR(m->mdt_regular_service);
3963                 CERROR("failed to start regular mdt service: %d\n", rc);
3964                 m->mdt_regular_service = NULL;
3965
3966                 RETURN(rc);
3967         }
3968
3969         /*
3970          * readpage service configuration. Parameters have to be adjusted,
3971          * ideally.
3972          */
3973         memset(&conf, 0, sizeof(conf));
3974         conf = (typeof(conf)) {
3975                 .psc_name               = LUSTRE_MDT_NAME "_readpage",
3976                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
3977                 .psc_buf                = {
3978                         .bc_nbufs               = MDS_NBUFS,
3979                         .bc_buf_size            = MDS_BUFSIZE,
3980                         .bc_req_max_size        = MDS_MAXREQSIZE,
3981                         .bc_rep_max_size        = MDS_MAXREPSIZE,
3982                         .bc_req_portal          = MDS_READPAGE_PORTAL,
3983                         .bc_rep_portal          = MDC_REPLY_PORTAL,
3984                 },
3985                 .psc_thr                = {
3986                         .tc_thr_name            = "mdt_rdpg",
3987                         .tc_nthrs_min           = MDT_MIN_THREADS,
3988                         .tc_nthrs_max           = MDT_MAX_THREADS,
3989                         .tc_nthrs_user          = mdt_num_threads,
3990                         .tc_ctx_tags            = LCT_MD_THREAD,
3991                 },
3992                 .psc_ops                = {
3993                         .so_req_handler         = mdt_readpage_handle,
3994                         .so_req_printer         = target_print_req,
3995                 },
3996         };
3997         m->mdt_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
3998         if (IS_ERR(m->mdt_readpage_service)) {
3999                 rc = PTR_ERR(m->mdt_readpage_service);
4000                 CERROR("failed to start readpage service: %d\n", rc);
4001                 m->mdt_readpage_service = NULL;
4002
4003                 GOTO(err_mdt_svc, rc);
4004         }
4005
4006         /*
4007          * setattr service configuration.
4008          *
4009          * XXX To keep the compatibility with old client(< 2.2), we need to
4010          * preserve this portal for a certain time, it should be removed
4011          * eventually. LU-617.
4012          */
4013         memset(&conf, 0, sizeof(conf));
4014         conf = (typeof(conf)) {
4015                 .psc_name               = LUSTRE_MDT_NAME "_setattr",
4016                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4017                 .psc_buf                = {
4018                         .bc_nbufs               = MDS_NBUFS,
4019                         .bc_buf_size            = MDS_BUFSIZE,
4020                         .bc_req_max_size        = MDS_MAXREQSIZE,
4021                         .bc_rep_max_size        = MDS_MAXREPSIZE,
4022                         .bc_req_portal          = MDS_SETATTR_PORTAL,
4023                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4024                 },
4025                 .psc_thr                = {
4026                         .tc_thr_name            = "mdt_attr",
4027                         .tc_nthrs_min           = MDT_MIN_THREADS,
4028                         .tc_nthrs_max           = MDT_MAX_THREADS,
4029                         .tc_nthrs_user          = mdt_num_threads,
4030                         .tc_ctx_tags            = LCT_MD_THREAD,
4031                 },
4032                 .psc_ops                = {
4033                         .so_req_handler         = mdt_regular_handle,
4034                         .so_req_printer         = target_print_req,
4035                 },
4036         };
4037         m->mdt_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
4038         if (IS_ERR(m->mdt_setattr_service)) {
4039                 rc = PTR_ERR(m->mdt_setattr_service);
4040                 CERROR("failed to start setattr service: %d\n", rc);
4041                 m->mdt_setattr_service = NULL;
4042
4043                 GOTO(err_mdt_svc, rc);
4044         }
4045
4046         /*
4047          * sequence controller service configuration
4048          */
4049         memset(&conf, 0, sizeof(conf));
4050         conf = (typeof(conf)) {
4051                 .psc_name               = LUSTRE_MDT_NAME "_mdsc",
4052                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4053                 .psc_buf                = {
4054                         .bc_nbufs               = MDS_NBUFS,
4055                         .bc_buf_size            = MDS_BUFSIZE,
4056                         .bc_req_max_size        = SEQ_MAXREQSIZE,
4057                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
4058                         .bc_req_portal          = SEQ_CONTROLLER_PORTAL,
4059                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4060                 },
4061                 .psc_thr                = {
4062                         .tc_thr_name            = "mdt_mdsc",
4063                         .tc_nthrs_min           = MDT_MIN_THREADS,
4064                         .tc_nthrs_max           = MDT_MAX_THREADS,
4065                         .tc_nthrs_user          = mdt_num_threads,
4066                         .tc_ctx_tags            = LCT_MD_THREAD,
4067                 },
4068                 .psc_ops                = {
4069                         .so_req_handler         = mdt_mdsc_handle,
4070                         .so_req_printer         = target_print_req,
4071                 },
4072         };
4073         m->mdt_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
4074         if (IS_ERR(m->mdt_mdsc_service)) {
4075                 rc = PTR_ERR(m->mdt_mdsc_service);
4076                 CERROR("failed to start seq controller service: %d\n", rc);
4077                 m->mdt_mdsc_service = NULL;
4078
4079                 GOTO(err_mdt_svc, rc);
4080         }
4081
4082         /*
4083          * metadata sequence server service configuration
4084          */
4085         memset(&conf, 0, sizeof(conf));
4086         conf = (typeof(conf)) {
4087                 .psc_name               = LUSTRE_MDT_NAME "_mdss",
4088                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4089                 .psc_buf                = {
4090                         .bc_nbufs               = MDS_NBUFS,
4091                         .bc_buf_size            = MDS_BUFSIZE,
4092                         .bc_req_max_size        = SEQ_MAXREQSIZE,
4093                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
4094                         .bc_req_portal          = SEQ_METADATA_PORTAL,
4095                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4096                 },
4097                 .psc_thr                = {
4098                         .tc_thr_name            = "mdt_mdss",
4099                         .tc_nthrs_min           = MDT_MIN_THREADS,
4100                         .tc_nthrs_max           = MDT_MAX_THREADS,
4101                         .tc_nthrs_user          = mdt_num_threads,
4102                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
4103                 },
4104                 .psc_ops                = {
4105                         .so_req_handler         = mdt_mdss_handle,
4106                         .so_req_printer         = target_print_req,
4107                 },
4108         };
4109         m->mdt_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
4110         if (IS_ERR(m->mdt_mdss_service)) {
4111                 rc = PTR_ERR(m->mdt_mdss_service);
4112                 CERROR("failed to start metadata seq server service: %d\n", rc);
4113                 m->mdt_mdss_service = NULL;
4114
4115                 GOTO(err_mdt_svc, rc);
4116         }
4117
4118         /*
4119          * Data sequence server service configuration. We want to have really
4120          * cluster-wide sequences space. This is why we start only one sequence
4121          * controller which manages space.
4122          */
4123         memset(&conf, 0, sizeof(conf));
4124         conf = (typeof(conf)) {
4125                 .psc_name               = LUSTRE_MDT_NAME "_dtss",
4126                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4127                 .psc_buf                = {
4128                         .bc_nbufs               = MDS_NBUFS,
4129                         .bc_buf_size            = MDS_BUFSIZE,
4130                         .bc_req_max_size        = SEQ_MAXREQSIZE,
4131                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
4132                         .bc_req_portal          = SEQ_DATA_PORTAL,
4133                         .bc_rep_portal          = OSC_REPLY_PORTAL,
4134                 },
4135                 .psc_thr                = {
4136                         .tc_thr_name            = "mdt_dtss",
4137                         .tc_nthrs_min           = MDT_MIN_THREADS,
4138                         .tc_nthrs_max           = MDT_MAX_THREADS,
4139                         .tc_nthrs_user          = mdt_num_threads,
4140                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
4141                 },
4142                 .psc_ops                = {
4143                         .so_req_handler         = mdt_dtss_handle,
4144                         .so_req_printer         = target_print_req,
4145                 },
4146         };
4147         m->mdt_dtss_service = ptlrpc_register_service(&conf, procfs_entry);
4148         if (IS_ERR(m->mdt_dtss_service)) {
4149                 rc = PTR_ERR(m->mdt_dtss_service);
4150                 CERROR("failed to start data seq server service: %d\n", rc);
4151                 m->mdt_dtss_service = NULL;
4152
4153                 GOTO(err_mdt_svc, rc);
4154         }
4155
4156         /* FLD service start */
4157         memset(&conf, 0, sizeof(conf));
4158         conf = (typeof(conf)) {
4159                 .psc_name            = LUSTRE_MDT_NAME "_fld",
4160                 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4161                 .psc_buf                = {
4162                         .bc_nbufs               = MDS_NBUFS,
4163                         .bc_buf_size            = MDS_BUFSIZE,
4164                         .bc_req_max_size        = FLD_MAXREQSIZE,
4165                         .bc_rep_max_size        = FLD_MAXREPSIZE,
4166                         .bc_req_portal          = FLD_REQUEST_PORTAL,
4167                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4168                 },
4169                 .psc_thr                = {
4170                         .tc_thr_name            = "mdt_fld",
4171                         .tc_nthrs_min           = MDT_MIN_THREADS,
4172                         .tc_nthrs_max           = MDT_MAX_THREADS,
4173                         .tc_nthrs_user          = mdt_num_threads,
4174                         .tc_ctx_tags            = LCT_DT_THREAD | LCT_MD_THREAD
4175                 },
4176                 .psc_ops                = {
4177                         .so_req_handler         = mdt_fld_handle,
4178                         .so_req_printer         = target_print_req,
4179                 },
4180         };
4181         m->mdt_fld_service = ptlrpc_register_service(&conf, procfs_entry);
4182         if (IS_ERR(m->mdt_fld_service)) {
4183                 rc = PTR_ERR(m->mdt_fld_service);
4184                 CERROR("failed to start fld service: %d\n", rc);
4185                 m->mdt_fld_service = NULL;
4186
4187                 GOTO(err_mdt_svc, rc);
4188         }
4189
4190         /*
4191          * mds-mds service configuration. Separate portal is used to allow
4192          * mds-mds requests be not blocked during recovery.
4193          */
4194         memset(&conf, 0, sizeof(conf));
4195         conf = (typeof(conf)) {
4196                 .psc_name               = LUSTRE_MDT_NAME "_mds",
4197                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4198                 .psc_buf                = {
4199                         .bc_nbufs               = MDS_NBUFS,
4200                         .bc_buf_size            = MDS_BUFSIZE,
4201                         .bc_req_max_size        = MDS_MAXREQSIZE,
4202                         .bc_rep_max_size        = MDS_MAXREPSIZE,
4203                         .bc_req_portal          = MDS_MDS_PORTAL,
4204                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4205                 },
4206                 .psc_thr                = {
4207                         .tc_thr_name            = "mdt_mds",
4208                         .tc_nthrs_min           = MDT_MIN_THREADS,
4209                         .tc_nthrs_max           = MDT_MAX_THREADS,
4210                         .tc_nthrs_user          = mdt_num_threads,
4211                         .tc_ctx_tags            = LCT_MD_THREAD,
4212                 },
4213                 .psc_ops                = {
4214                         .so_req_handler         = mdt_xmds_handle,
4215                         .so_req_printer         = target_print_req,
4216                 },
4217         };
4218         m->mdt_xmds_service = ptlrpc_register_service(&conf, procfs_entry);
4219         if (IS_ERR(m->mdt_xmds_service)) {
4220                 rc = PTR_ERR(m->mdt_xmds_service);
4221                 CERROR("failed to start xmds service: %d\n", rc);
4222                 m->mdt_xmds_service = NULL;
4223
4224                 GOTO(err_mdt_svc, rc);
4225         }
4226
4227         EXIT;
4228 err_mdt_svc:
4229         if (rc)
4230                 mdt_stop_ptlrpc_service(m);
4231
4232         return rc;
4233 }
4234
4235 static void mdt_stack_fini(const struct lu_env *env,
4236                            struct mdt_device *m, struct lu_device *top)
4237 {
4238         struct obd_device       *obd = mdt2obd_dev(m);
4239         struct lustre_cfg_bufs  *bufs;
4240         struct lustre_cfg       *lcfg;
4241         struct mdt_thread_info  *info;
4242         char flags[3]="";
4243         ENTRY;
4244
4245         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4246         LASSERT(info != NULL);
4247
4248         bufs = &info->mti_u.bufs;
4249         /* process cleanup, pass mdt obd name to get obd umount flags */
4250         lustre_cfg_bufs_reset(bufs, obd->obd_name);
4251         if (obd->obd_force)
4252                 strcat(flags, "F");
4253         if (obd->obd_fail)
4254                 strcat(flags, "A");
4255         lustre_cfg_bufs_set_string(bufs, 1, flags);
4256         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4257         if (!lcfg) {
4258                 CERROR("Cannot alloc lcfg!\n");
4259                 return;
4260         }
4261
4262         LASSERT(top);
4263         top->ld_ops->ldo_process_config(env, top, lcfg);
4264         lustre_cfg_free(lcfg);
4265
4266         lu_stack_fini(env, top);
4267         m->mdt_child = NULL;
4268         m->mdt_bottom = NULL;
4269 }
4270
4271 static struct lu_device *mdt_layer_setup(struct lu_env *env,
4272                                          const char *typename,
4273                                          struct lu_device *child,
4274                                          struct lustre_cfg *cfg)
4275 {
4276         const char            *dev = lustre_cfg_string(cfg, 0);
4277         struct obd_type       *type;
4278         struct lu_device_type *ldt;
4279         struct lu_device      *d;
4280         int rc;
4281         ENTRY;
4282
4283         /* find the type */
4284         type = class_get_type(typename);
4285         if (!type) {
4286                 CERROR("Unknown type: '%s'\n", typename);
4287                 GOTO(out, rc = -ENODEV);
4288         }
4289
4290         rc = lu_env_refill((struct lu_env *)env);
4291         if (rc != 0) {
4292                 CERROR("Failure to refill session: '%d'\n", rc);
4293                 GOTO(out_type, rc);
4294         }
4295
4296         ldt = type->typ_lu;
4297         if (ldt == NULL) {
4298                 CERROR("type: '%s'\n", typename);
4299                 GOTO(out_type, rc = -EINVAL);
4300         }
4301
4302         ldt->ldt_obd_type = type;
4303         d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
4304         if (IS_ERR(d)) {
4305                 CERROR("Cannot allocate device: '%s'\n", typename);
4306                 GOTO(out_type, rc = -ENODEV);
4307         }
4308
4309         LASSERT(child->ld_site);
4310         d->ld_site = child->ld_site;
4311
4312         type->typ_refcnt++;
4313         rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
4314         if (rc) {
4315                 CERROR("can't init device '%s', rc %d\n", typename, rc);
4316                 GOTO(out_alloc, rc);
4317         }
4318         lu_device_get(d);
4319         lu_ref_add(&d->ld_reference, "lu-stack", &lu_site_init);
4320
4321         cfs_spin_lock(&d->ld_site->ls_ld_lock);
4322         cfs_list_add_tail(&d->ld_linkage, &d->ld_site->ls_ld_linkage);
4323         cfs_spin_unlock(&d->ld_site->ls_ld_lock);
4324
4325         RETURN(d);
4326 out_alloc:
4327         ldt->ldt_ops->ldto_device_free(env, d);
4328         type->typ_refcnt--;
4329 out_type:
4330         class_put_type(type);
4331 out:
4332         return ERR_PTR(rc);
4333 }
4334
4335 static int mdt_stack_init(struct lu_env *env,
4336                           struct mdt_device *m,
4337                           struct lustre_cfg *cfg,
4338                           struct lustre_mount_info  *lmi)
4339 {
4340         struct lu_device  *d = &m->mdt_md_dev.md_lu_dev;
4341         struct lu_device  *tmp;
4342         struct md_device  *md;
4343         struct lu_device  *child_lu_dev;
4344         int rc;
4345         ENTRY;
4346
4347         /* init the stack */
4348         tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
4349         if (IS_ERR(tmp)) {
4350                 RETURN(PTR_ERR(tmp));
4351         }
4352         m->mdt_bottom = lu2dt_dev(tmp);
4353         d = tmp;
4354         tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
4355         if (IS_ERR(tmp)) {
4356                 GOTO(out, rc = PTR_ERR(tmp));
4357         }
4358         d = tmp;
4359         md = lu2md_dev(d);
4360
4361         tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
4362         if (IS_ERR(tmp)) {
4363                 GOTO(out, rc = PTR_ERR(tmp));
4364         }
4365         d = tmp;
4366         /*set mdd upcall device*/
4367         md_upcall_dev_set(md, lu2md_dev(d));
4368
4369         md = lu2md_dev(d);
4370         /*set cmm upcall device*/
4371         md_upcall_dev_set(md, &m->mdt_md_dev);
4372
4373         m->mdt_child = lu2md_dev(d);
4374
4375         /* process setup config */
4376         tmp = &m->mdt_md_dev.md_lu_dev;
4377         rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
4378         if (rc)
4379                 GOTO(out, rc);
4380
4381         /* initialize local objects */
4382         child_lu_dev = &m->mdt_child->md_lu_dev;
4383
4384         rc = child_lu_dev->ld_ops->ldo_prepare(env,
4385                                                &m->mdt_md_dev.md_lu_dev,
4386                                                child_lu_dev);
4387 out:
4388         /* fini from last known good lu_device */
4389         if (rc)
4390                 mdt_stack_fini(env, m, d);
4391
4392         return rc;
4393 }
4394
4395 /**
4396  * setup CONFIG_ORIG context, used to access local config log.
4397  * this may need to be rewrite as part of llog rewrite for lu-api.
4398  */
4399 static int mdt_obd_llog_setup(struct obd_device *obd,
4400                               struct lustre_sb_info *lsi)
4401 {
4402         int     rc;
4403
4404         LASSERT(obd->obd_fsops == NULL);
4405
4406         obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
4407         if (IS_ERR(obd->obd_fsops))
4408                 return PTR_ERR(obd->obd_fsops);
4409
4410         rc = fsfilt_setup(obd, lsi->lsi_srv_mnt->mnt_sb);
4411         if (rc) {
4412                 fsfilt_put_ops(obd->obd_fsops);
4413                 return rc;
4414         }
4415
4416         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
4417         obd->obd_lvfs_ctxt.pwdmnt = lsi->lsi_srv_mnt;
4418         obd->obd_lvfs_ctxt.pwd = lsi->lsi_srv_mnt->mnt_root;
4419         obd->obd_lvfs_ctxt.fs = get_ds();
4420
4421         rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, obd,
4422                         0, NULL, &llog_lvfs_ops);
4423         if (rc) {
4424                 CERROR("llog_setup() failed: %d\n", rc);
4425                 fsfilt_put_ops(obd->obd_fsops);
4426         }
4427
4428         return rc;
4429 }
4430
4431 static void mdt_obd_llog_cleanup(struct obd_device *obd)
4432 {
4433         struct llog_ctxt *ctxt;
4434
4435         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
4436         if (ctxt)
4437                 llog_cleanup(ctxt);
4438
4439         if (obd->obd_fsops) {
4440                 fsfilt_put_ops(obd->obd_fsops);
4441                 obd->obd_fsops = NULL;
4442         }
4443 }
4444
4445 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4446 {
4447         struct md_device  *next = m->mdt_child;
4448         struct lu_device  *d    = &m->mdt_md_dev.md_lu_dev;
4449         struct lu_site    *ls   = d->ld_site;
4450         struct obd_device *obd = mdt2obd_dev(m);
4451         ENTRY;
4452
4453         target_recovery_fini(obd);
4454
4455         ping_evictor_stop();
4456
4457         mdt_stop_ptlrpc_service(m);
4458         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4459         mdt_obd_llog_cleanup(obd);
4460         obd_exports_barrier(obd);
4461         obd_zombie_barrier();
4462
4463         mdt_procfs_fini(m);
4464
4465 #ifdef HAVE_QUOTA_SUPPORT
4466         next->md_ops->mdo_quota.mqo_cleanup(env, next);
4467 #endif
4468         lut_fini(env, &m->mdt_lut);
4469         mdt_fs_cleanup(env, m);
4470         upcall_cache_cleanup(m->mdt_identity_cache);
4471         m->mdt_identity_cache = NULL;
4472
4473         if (m->mdt_namespace != NULL) {
4474                 ldlm_namespace_free(m->mdt_namespace, NULL,
4475                                     d->ld_obd->obd_force);
4476                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4477         }
4478
4479         cfs_free_nidlist(&m->mdt_nosquash_nids);
4480         if (m->mdt_nosquash_str) {
4481                 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4482                 m->mdt_nosquash_str = NULL;
4483                 m->mdt_nosquash_strlen = 0;
4484         }
4485
4486         mdt_seq_fini(env, m);
4487         mdt_seq_fini_cli(m);
4488         mdt_fld_fini(env, m);
4489         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4490
4491         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4492         cfs_timer_disarm(&m->mdt_ck_timer);
4493         mdt_ck_thread_stop(m);
4494
4495         /*
4496          * Finish the stack
4497          */
4498         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4499
4500         if (ls) {
4501                 struct md_site *mite;
4502
4503                 lu_site_fini(ls);
4504                 mite = lu_site2md(ls);
4505                 OBD_FREE_PTR(mite);
4506                 d->ld_site = NULL;
4507         }
4508         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4509
4510         EXIT;
4511 }
4512
4513 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4514 {
4515         struct mdt_device       *m = mdt_dev(obd->obd_lu_dev);
4516         struct sptlrpc_rule_set  tmp_rset;
4517         int                      rc;
4518
4519         sptlrpc_rule_set_init(&tmp_rset);
4520         rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4521         if (rc) {
4522                 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4523                        obd->obd_name, rc);
4524                 return rc;
4525         }
4526
4527         sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4528
4529         cfs_write_lock(&m->mdt_sptlrpc_lock);
4530         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4531         m->mdt_sptlrpc_rset = tmp_rset;
4532         cfs_write_unlock(&m->mdt_sptlrpc_lock);
4533
4534         return 0;
4535 }
4536
4537 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4538
4539 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4540                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
4541 {
4542         struct mdt_thread_info    *info;
4543         struct obd_device         *obd;
4544         const char                *dev = lustre_cfg_string(cfg, 0);
4545         const char                *num = lustre_cfg_string(cfg, 2);
4546         struct lustre_mount_info  *lmi = NULL;
4547         struct lustre_sb_info     *lsi;
4548         struct lustre_disk_data   *ldd;
4549         struct lu_site            *s;
4550         struct md_site            *mite;
4551         const char                *identity_upcall = "NONE";
4552         struct md_device          *next;
4553         int                        rc;
4554         int                        node_id;
4555         mntopt_t                   mntopts;
4556         ENTRY;
4557
4558         md_device_init(&m->mdt_md_dev, ldt);
4559         /*
4560          * Environment (env) might be missing mdt_thread_key values at that
4561          * point, if device is allocated when mdt_thread_key is in QUIESCENT
4562          * mode.
4563          *
4564          * Usually device allocation path doesn't use module key values, but
4565          * mdt has to do a lot of work here, so allocate key value.
4566          */
4567         rc = lu_env_refill((struct lu_env *)env);
4568         if (rc != 0)
4569                 RETURN(rc);
4570
4571         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4572         LASSERT(info != NULL);
4573
4574         obd = class_name2obd(dev);
4575         LASSERT(obd != NULL);
4576
4577         m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4578         m->mdt_max_cookiesize = sizeof(struct llog_cookie);
4579
4580         m->mdt_som_conf = 0;
4581
4582         m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4583         lmi = server_get_mount_2(dev);
4584         if (lmi == NULL) {
4585                 CERROR("Cannot get mount info for %s!\n", dev);
4586                 RETURN(-EFAULT);
4587         } else {
4588                 lsi = s2lsi(lmi->lmi_sb);
4589                 /* CMD is supported only in IAM mode */
4590                 ldd = lsi->lsi_ldd;
4591                 LASSERT(num);
4592                 node_id = simple_strtol(num, NULL, 10);
4593                 if (!(ldd->ldd_flags & LDD_F_IAM_DIR) && node_id) {
4594                         CERROR("CMD Operation not allowed in IOP mode\n");
4595                         GOTO(err_lmi, rc = -EINVAL);
4596                 }
4597
4598                 obd->u.obt.obt_magic = OBT_MAGIC;
4599         }
4600
4601         cfs_rwlock_init(&m->mdt_sptlrpc_lock);
4602         sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4603
4604         cfs_spin_lock_init(&m->mdt_ioepoch_lock);
4605         m->mdt_opts.mo_compat_resname = 0;
4606         m->mdt_opts.mo_mds_capa = 1;
4607         m->mdt_opts.mo_oss_capa = 1;
4608         m->mdt_capa_timeout = CAPA_TIMEOUT;
4609         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4610         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4611         m->mdt_squash_uid = 0;
4612         m->mdt_squash_gid = 0;
4613         CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4614         m->mdt_nosquash_str = NULL;
4615         m->mdt_nosquash_strlen = 0;
4616         cfs_init_rwsem(&m->mdt_squash_sem);
4617
4618         OBD_ALLOC_PTR(mite);
4619         if (mite == NULL)
4620                 GOTO(err_lmi, rc = -ENOMEM);
4621
4622         s = &mite->ms_lu;
4623
4624         m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
4625         m->mdt_md_dev.md_lu_dev.ld_obd = obd;
4626         /* set this lu_device to obd, because error handling need it */
4627         obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
4628
4629         rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
4630         if (rc) {
4631                 CERROR("Can't init lu_site, rc %d\n", rc);
4632                 GOTO(err_free_site, rc);
4633         }
4634
4635         /* set server index */
4636         lu_site2md(s)->ms_node_id = node_id;
4637
4638         /* failover is the default
4639          * FIXME: we do not failout mds0/mgs, which may cause some problems.
4640          * assumed whose ms_node_id == 0 XXX
4641          * */
4642         obd->obd_replayable = 1;
4643         /* No connection accepted until configurations will finish */
4644         obd->obd_no_conn = 1;
4645
4646         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4647                 char *str = lustre_cfg_string(cfg, 4);
4648                 if (strchr(str, 'n')) {
4649                         CWARN("%s: recovery disabled\n", obd->obd_name);
4650                         obd->obd_replayable = 0;
4651                 }
4652         }
4653
4654         /* init the stack */
4655         rc = mdt_stack_init((struct lu_env *)env, m, cfg, lmi);
4656         if (rc) {
4657                 CERROR("Can't init device stack, rc %d\n", rc);
4658                 GOTO(err_lu_site, rc);
4659         }
4660
4661         rc = lut_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4662         if (rc)
4663                 GOTO(err_fini_stack, rc);
4664
4665         rc = mdt_fld_init(env, obd->obd_name, m);
4666         if (rc)
4667                 GOTO(err_lut, rc);
4668
4669         rc = mdt_seq_init(env, obd->obd_name, m);
4670         if (rc)
4671                 GOTO(err_fini_fld, rc);
4672
4673         snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
4674                  LUSTRE_MDT_NAME"-%p", m);
4675         m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4676                                               LDLM_NAMESPACE_SERVER,
4677                                               LDLM_NAMESPACE_GREEDY,
4678                                               LDLM_NS_TYPE_MDT);
4679         if (m->mdt_namespace == NULL)
4680                 GOTO(err_fini_seq, rc = -ENOMEM);
4681
4682         ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4683         /* set obd_namespace for compatibility with old code */
4684         obd->obd_namespace = m->mdt_namespace;
4685
4686         cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4687
4688         rc = mdt_ck_thread_start(m);
4689         if (rc)
4690                 GOTO(err_free_ns, rc);
4691
4692         rc = mdt_fs_setup(env, m, obd, lsi);
4693         if (rc)
4694                 GOTO(err_capa, rc);
4695
4696         rc = mdt_obd_llog_setup(obd, lsi);
4697         if (rc)
4698                 GOTO(err_fs_cleanup, rc);
4699
4700         rc = mdt_llog_ctxt_clone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4701         if (rc)
4702                 GOTO(err_llog_cleanup, rc);
4703
4704         mdt_adapt_sptlrpc_conf(obd, 1);
4705
4706         next = m->mdt_child;
4707 #ifdef HAVE_QUOTA_SUPPORT
4708         rc = next->md_ops->mdo_quota.mqo_setup(env, next, lmi->lmi_mnt);
4709         if (rc)
4710                 GOTO(err_llog_cleanup, rc);
4711 #endif
4712
4713         server_put_mount_2(dev, lmi->lmi_mnt);
4714         lmi = NULL;
4715
4716         rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4717                                          &mntopts);
4718         if (rc)
4719                 GOTO(err_quota, rc);
4720
4721         if (mntopts & MNTOPT_USERXATTR)
4722                 m->mdt_opts.mo_user_xattr = 1;
4723         else
4724                 m->mdt_opts.mo_user_xattr = 0;
4725
4726         if (mntopts & MNTOPT_ACL)
4727                 m->mdt_opts.mo_acl = 1;
4728         else
4729                 m->mdt_opts.mo_acl = 0;
4730
4731         /* XXX: to support suppgid for ACL, we enable identity_upcall
4732          * by default, otherwise, maybe got unexpected -EACCESS. */
4733         if (m->mdt_opts.mo_acl)
4734                 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4735
4736         m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
4737                                                 &mdt_identity_upcall_cache_ops);
4738         if (IS_ERR(m->mdt_identity_cache)) {
4739                 rc = PTR_ERR(m->mdt_identity_cache);
4740                 m->mdt_identity_cache = NULL;
4741                 GOTO(err_quota, rc);
4742         }
4743
4744         target_recovery_init(&m->mdt_lut, mdt_recovery_handle);
4745
4746         rc = mdt_procfs_init(m, dev);
4747         if (rc) {
4748                 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4749                 GOTO(err_recovery, rc);
4750         }
4751
4752         rc = mdt_start_ptlrpc_service(m);
4753         if (rc)
4754                 GOTO(err_procfs, rc);
4755
4756         ping_evictor_start();
4757
4758         rc = lu_site_init_finish(s);
4759         if (rc)
4760                 GOTO(err_stop_service, rc);
4761
4762         if (obd->obd_recovering == 0)
4763                 mdt_postrecov(env, m);
4764
4765         mdt_init_capa_ctxt(env, m);
4766
4767         /* Reduce the initial timeout on an MDS because it doesn't need such
4768          * a long timeout as an OST does. Adaptive timeouts will adjust this
4769          * value appropriately. */
4770         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4771                 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4772
4773         RETURN(0);
4774
4775 err_stop_service:
4776         ping_evictor_stop();
4777         mdt_stop_ptlrpc_service(m);
4778 err_procfs:
4779         mdt_procfs_fini(m);
4780 err_recovery:
4781         target_recovery_fini(obd);
4782         upcall_cache_cleanup(m->mdt_identity_cache);
4783         m->mdt_identity_cache = NULL;
4784 err_quota:
4785 #ifdef HAVE_QUOTA_SUPPORT
4786         next->md_ops->mdo_quota.mqo_cleanup(env, next);
4787 #endif
4788 err_llog_cleanup:
4789         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4790         mdt_obd_llog_cleanup(obd);
4791 err_fs_cleanup:
4792         mdt_fs_cleanup(env, m);
4793 err_capa:
4794         cfs_timer_disarm(&m->mdt_ck_timer);
4795         mdt_ck_thread_stop(m);
4796 err_free_ns:
4797         ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4798         obd->obd_namespace = m->mdt_namespace = NULL;
4799 err_fini_seq:
4800         mdt_seq_fini(env, m);
4801 err_fini_fld:
4802         mdt_fld_fini(env, m);
4803 err_lut:
4804         lut_fini(env, &m->mdt_lut);
4805 err_fini_stack:
4806         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4807 err_lu_site:
4808         lu_site_fini(s);
4809 err_free_site:
4810         OBD_FREE_PTR(mite);
4811 err_lmi:
4812         if (lmi)
4813                 server_put_mount_2(dev, lmi->lmi_mnt);
4814         return (rc);
4815 }
4816
4817 /* used by MGS to process specific configurations */
4818 static int mdt_process_config(const struct lu_env *env,
4819                               struct lu_device *d, struct lustre_cfg *cfg)
4820 {
4821         struct mdt_device *m = mdt_dev(d);
4822         struct md_device *md_next = m->mdt_child;
4823         struct lu_device *next = md2lu_dev(md_next);
4824         int rc = 0;
4825         ENTRY;
4826
4827         switch (cfg->lcfg_command) {
4828         case LCFG_PARAM: {
4829                 struct lprocfs_static_vars lvars;
4830                 struct obd_device *obd = d->ld_obd;
4831
4832                 /*
4833                  * For interoperability between 1.8 and 2.0,
4834                  */
4835                 {
4836                         /* Skip old "mdt.group_upcall" param. */
4837                         char *param = lustre_cfg_string(cfg, 1);
4838                         if (param && !strncmp("mdt.group_upcall", param, 16)) {
4839                                 CWARN("For 1.8 interoperability, skip this"
4840                                        " mdt.group_upcall. It is obsolete\n");
4841                                 break;
4842                         }
4843                         /* Rename old "mdt.quota_type" to "mdd.quota_type. */
4844                         if (param && !strncmp("mdt.quota_type", param, 14)) {
4845                                 CWARN("Found old param mdt.quota_type, changed"
4846                                       " it to mdd.quota_type.\n");
4847                                 param[2] = 'd';
4848                         }
4849                 }
4850
4851                 lprocfs_mdt_init_vars(&lvars);
4852                 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4853                                               cfg, obd);
4854                 if (rc > 0 || rc == -ENOSYS)
4855                         /* we don't understand; pass it on */
4856                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
4857                 break;
4858         }
4859         case LCFG_ADD_MDC:
4860                 /*
4861                  * Add mdc hook to get first MDT uuid and connect it to
4862                  * ls->controller to use for seq manager.
4863                  */
4864                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4865                 if (rc)
4866                         CERROR("Can't add mdc, rc %d\n", rc);
4867                 else
4868                         rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
4869                 break;
4870         default:
4871                 /* others are passed further */
4872                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4873                 break;
4874         }
4875         RETURN(rc);
4876 }
4877
4878 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4879                                           const struct lu_object_header *hdr,
4880                                           struct lu_device *d)
4881 {
4882         struct mdt_object *mo;
4883
4884         ENTRY;
4885
4886         OBD_ALLOC_PTR(mo);
4887         if (mo != NULL) {
4888                 struct lu_object *o;
4889                 struct lu_object_header *h;
4890
4891                 o = &mo->mot_obj.mo_lu;
4892                 h = &mo->mot_header;
4893                 lu_object_header_init(h);
4894                 lu_object_init(o, h, d);
4895                 lu_object_add_top(h, o);
4896                 o->lo_ops = &mdt_obj_ops;
4897                 cfs_mutex_init(&mo->mot_ioepoch_mutex);
4898                 cfs_mutex_init(&mo->mot_lov_mutex);
4899                 RETURN(o);
4900         } else
4901                 RETURN(NULL);
4902 }
4903
4904 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4905                            const struct lu_object_conf *unused)
4906 {
4907         struct mdt_device *d = mdt_dev(o->lo_dev);
4908         struct lu_device  *under;
4909         struct lu_object  *below;
4910         int                rc = 0;
4911         ENTRY;
4912
4913         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4914                PFID(lu_object_fid(o)));
4915
4916         under = &d->mdt_child->md_lu_dev;
4917         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4918         if (below != NULL) {
4919                 lu_object_add(o, below);
4920         } else
4921                 rc = -ENOMEM;
4922
4923         RETURN(rc);
4924 }
4925
4926 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4927 {
4928         struct mdt_object *mo = mdt_obj(o);
4929         struct lu_object_header *h;
4930         ENTRY;
4931
4932         h = o->lo_header;
4933         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4934                PFID(lu_object_fid(o)));
4935
4936         lu_object_fini(o);
4937         lu_object_header_fini(h);
4938         OBD_FREE_PTR(mo);
4939         EXIT;
4940 }
4941
4942 static int mdt_object_print(const struct lu_env *env, void *cookie,
4943                             lu_printer_t p, const struct lu_object *o)
4944 {
4945         struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4946         return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4947                     "flags="LPX64", epochcount=%d, writecount=%d)",
4948                     mdto, mdto->mot_ioepoch, mdto->mot_flags,
4949                     mdto->mot_ioepoch_count, mdto->mot_writecount);
4950 }
4951
4952 static const struct lu_device_operations mdt_lu_ops = {
4953         .ldo_object_alloc   = mdt_object_alloc,
4954         .ldo_process_config = mdt_process_config,
4955 };
4956
4957 static const struct lu_object_operations mdt_obj_ops = {
4958         .loo_object_init    = mdt_object_init,
4959         .loo_object_free    = mdt_object_free,
4960         .loo_object_print   = mdt_object_print
4961 };
4962
4963 static int mdt_obd_set_info_async(const struct lu_env *env,
4964                                   struct obd_export *exp,
4965                                   __u32 keylen, void *key,
4966                                   __u32 vallen, void *val,
4967                                   struct ptlrpc_request_set *set)
4968 {
4969         struct obd_device     *obd = exp->exp_obd;
4970         int                    rc;
4971         ENTRY;
4972
4973         LASSERT(obd);
4974
4975         if (KEY_IS(KEY_SPTLRPC_CONF)) {
4976                 rc = mdt_adapt_sptlrpc_conf(obd, 0);
4977                 RETURN(rc);
4978         }
4979
4980         RETURN(0);
4981 }
4982
4983 /* mds_connect_internal */
4984 static int mdt_connect_internal(struct obd_export *exp,
4985                                 struct mdt_device *mdt,
4986                                 struct obd_connect_data *data)
4987 {
4988         if (data != NULL) {
4989                 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4990                 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4991
4992                 /* If no known bits (which should not happen, probably,
4993                    as everybody should support LOOKUP and UPDATE bits at least)
4994                    revert to compat mode with plain locks. */
4995                 if (!data->ocd_ibits_known &&
4996                     data->ocd_connect_flags & OBD_CONNECT_IBITS)
4997                         data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4998
4999                 if (!mdt->mdt_opts.mo_acl)
5000                         data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5001
5002                 if (!mdt->mdt_opts.mo_user_xattr)
5003                         data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5004
5005                 if (!mdt->mdt_som_conf)
5006                         data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5007
5008                 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5009                         data->ocd_brw_size = min(data->ocd_brw_size,
5010                                (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
5011                         if (data->ocd_brw_size == 0) {
5012                                 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5013                                        " ocd_version: %x ocd_grant: %d "
5014                                        "ocd_index: %u ocd_brw_size is "
5015                                        "unexpectedly zero, network data "
5016                                        "corruption? Refusing connection of this"
5017                                        " client\n",
5018                                        exp->exp_obd->obd_name,
5019                                        exp->exp_client_uuid.uuid,
5020                                        exp, data->ocd_connect_flags, data->ocd_version,
5021                                        data->ocd_grant, data->ocd_index);
5022                                 return -EPROTO;
5023                         }
5024                 }
5025
5026                 cfs_spin_lock(&exp->exp_lock);
5027                 exp->exp_connect_flags = data->ocd_connect_flags;
5028                 cfs_spin_unlock(&exp->exp_lock);
5029                 data->ocd_version = LUSTRE_VERSION_CODE;
5030                 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5031         }
5032
5033 #if 0
5034         if (mdt->mdt_opts.mo_acl &&
5035             ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
5036                 CWARN("%s: MDS requires ACL support but client does not\n",
5037                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5038                 return -EBADE;
5039         }
5040 #endif
5041
5042         if ((exp->exp_connect_flags & OBD_CONNECT_FID) == 0) {
5043                 CWARN("%s: MDS requires FID support, but client not\n",
5044                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5045                 return -EBADE;
5046         }
5047
5048         if (mdt->mdt_som_conf && !exp_connect_som(exp) &&
5049             !(exp->exp_connect_flags & OBD_CONNECT_MDS_MDS)) {
5050                 CWARN("%s: MDS has SOM enabled, but client does not support "
5051                       "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5052                 return -EBADE;
5053         }
5054
5055         return 0;
5056 }
5057
5058 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5059                                      struct obd_export *exp,
5060                                      struct ptlrpc_request *req)
5061 {
5062         struct sptlrpc_flavor   flvr;
5063         int                     rc = 0;
5064
5065         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5066                 cfs_read_lock(&mdt->mdt_sptlrpc_lock);
5067                 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5068                                              req->rq_sp_from,
5069                                              req->rq_peer.nid,
5070                                              &flvr);
5071                 cfs_read_unlock(&mdt->mdt_sptlrpc_lock);
5072
5073                 cfs_spin_lock(&exp->exp_lock);
5074
5075                 exp->exp_sp_peer = req->rq_sp_from;
5076                 exp->exp_flvr = flvr;
5077
5078                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5079                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5080                         CERROR("unauthorized rpc flavor %x from %s, "
5081                                "expect %x\n", req->rq_flvr.sf_rpc,
5082                                libcfs_nid2str(req->rq_peer.nid),
5083                                exp->exp_flvr.sf_rpc);
5084                         rc = -EACCES;
5085                 }
5086
5087                 cfs_spin_unlock(&exp->exp_lock);
5088         } else {
5089                 if (exp->exp_sp_peer != req->rq_sp_from) {
5090                         CERROR("RPC source %s doesn't match %s\n",
5091                                sptlrpc_part2name(req->rq_sp_from),
5092                                sptlrpc_part2name(exp->exp_sp_peer));
5093                         rc = -EACCES;
5094                 } else {
5095                         rc = sptlrpc_target_export_check(exp, req);
5096                 }
5097         }
5098
5099         return rc;
5100 }
5101
5102 /* mds_connect copy */
5103 static int mdt_obd_connect(const struct lu_env *env,
5104                            struct obd_export **exp, struct obd_device *obd,
5105                            struct obd_uuid *cluuid,
5106                            struct obd_connect_data *data,
5107                            void *localdata)
5108 {
5109         struct mdt_thread_info *info;
5110         struct obd_export      *lexp;
5111         struct lustre_handle    conn = { 0 };
5112         struct mdt_device      *mdt;
5113         struct ptlrpc_request  *req;
5114         int                     rc;
5115         ENTRY;
5116
5117         LASSERT(env != NULL);
5118         if (!exp || !obd || !cluuid)
5119                 RETURN(-EINVAL);
5120
5121         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5122         req = info->mti_pill->rc_req;
5123         mdt = mdt_dev(obd->obd_lu_dev);
5124
5125         rc = class_connect(&conn, obd, cluuid);
5126         if (rc)
5127                 RETURN(rc);
5128
5129         lexp = class_conn2export(&conn);
5130         LASSERT(lexp != NULL);
5131
5132         rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5133         if (rc)
5134                 GOTO(out, rc);
5135
5136         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5137                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5138
5139         rc = mdt_connect_internal(lexp, mdt, data);
5140         if (rc == 0) {
5141                 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5142
5143                 LASSERT(lcd);
5144                 info->mti_exp = lexp;
5145                 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5146                 rc = lut_client_new(env, lexp);
5147                 if (rc == 0)
5148                         mdt_export_stats_init(obd, lexp, localdata);
5149         }
5150
5151 out:
5152         if (rc != 0) {
5153                 class_disconnect(lexp);
5154                 *exp = NULL;
5155         } else {
5156                 *exp = lexp;
5157         }
5158
5159         RETURN(rc);
5160 }
5161
5162 static int mdt_obd_reconnect(const struct lu_env *env,
5163                              struct obd_export *exp, struct obd_device *obd,
5164                              struct obd_uuid *cluuid,
5165                              struct obd_connect_data *data,
5166                              void *localdata)
5167 {
5168         struct mdt_thread_info *info;
5169         struct mdt_device      *mdt;
5170         struct ptlrpc_request  *req;
5171         int                     rc;
5172         ENTRY;
5173
5174         if (exp == NULL || obd == NULL || cluuid == NULL)
5175                 RETURN(-EINVAL);
5176
5177         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5178         req = info->mti_pill->rc_req;
5179         mdt = mdt_dev(obd->obd_lu_dev);
5180
5181         rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5182         if (rc)
5183                 RETURN(rc);
5184
5185         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5186         if (rc == 0)
5187                 mdt_export_stats_init(obd, exp, localdata);
5188
5189         RETURN(rc);
5190 }
5191
5192 static int mdt_export_cleanup(struct obd_export *exp)
5193 {
5194         struct mdt_export_data *med = &exp->exp_mdt_data;
5195         struct obd_device      *obd = exp->exp_obd;
5196         struct mdt_device      *mdt;
5197         struct mdt_thread_info *info;
5198         struct lu_env           env;
5199         CFS_LIST_HEAD(closing_list);
5200         struct mdt_file_data *mfd, *n;
5201         int rc = 0;
5202         ENTRY;
5203
5204         cfs_spin_lock(&med->med_open_lock);
5205         while (!cfs_list_empty(&med->med_open_head)) {
5206                 cfs_list_t *tmp = med->med_open_head.next;
5207                 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5208
5209                 /* Remove mfd handle so it can't be found again.
5210                  * We are consuming the mfd_list reference here. */
5211                 class_handle_unhash(&mfd->mfd_handle);
5212                 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5213         }
5214         cfs_spin_unlock(&med->med_open_lock);
5215         mdt = mdt_dev(obd->obd_lu_dev);
5216         LASSERT(mdt != NULL);
5217
5218         rc = lu_env_init(&env, LCT_MD_THREAD);
5219         if (rc)
5220                 RETURN(rc);
5221
5222         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5223         LASSERT(info != NULL);
5224         memset(info, 0, sizeof *info);
5225         info->mti_env = &env;
5226         info->mti_mdt = mdt;
5227         info->mti_exp = exp;
5228
5229         if (!cfs_list_empty(&closing_list)) {
5230                 struct md_attr *ma = &info->mti_attr;
5231                 int lmm_size;
5232                 int cookie_size;
5233
5234                 lmm_size = mdt->mdt_max_mdsize;
5235                 OBD_ALLOC_LARGE(ma->ma_lmm, lmm_size);
5236                 if (ma->ma_lmm == NULL)
5237                         GOTO(out_lmm, rc = -ENOMEM);
5238
5239                 cookie_size = mdt->mdt_max_cookiesize;
5240                 OBD_ALLOC_LARGE(ma->ma_cookie, cookie_size);
5241                 if (ma->ma_cookie == NULL)
5242                         GOTO(out_cookie, rc = -ENOMEM);
5243
5244                 /* Close any open files (which may also cause orphan unlinking). */
5245                 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5246                         cfs_list_del_init(&mfd->mfd_list);
5247                         memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
5248                         ma->ma_lmm_size = lmm_size;
5249                         ma->ma_cookie_size = cookie_size;
5250                         ma->ma_need = 0;
5251                         /* It is not for setattr, just tell MDD to send
5252                          * DESTROY RPC to OSS if needed */
5253                         ma->ma_valid = MA_FLAGS;
5254                         ma->ma_attr_flags = MDS_CLOSE_CLEANUP;
5255                         /* Don't unlink orphan on failover umount, LU-184 */
5256                         if (exp->exp_flags & OBD_OPT_FAILOVER)
5257                                 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5258                         mdt_mfd_close(info, mfd);
5259                 }
5260                 OBD_FREE_LARGE(ma->ma_cookie, cookie_size);
5261                 ma->ma_cookie = NULL;
5262 out_cookie:
5263                 OBD_FREE_LARGE(ma->ma_lmm, lmm_size);
5264                 ma->ma_lmm = NULL;
5265         }
5266 out_lmm:
5267         info->mti_mdt = NULL;
5268         /* cleanup client slot early */
5269         /* Do not erase record for recoverable client. */
5270         if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5271                 lut_client_del(&env, exp);
5272         lu_env_fini(&env);
5273
5274         RETURN(rc);
5275 }
5276
5277 static int mdt_obd_disconnect(struct obd_export *exp)
5278 {
5279         int rc;
5280         ENTRY;
5281
5282         LASSERT(exp);
5283         class_export_get(exp);
5284
5285         rc = server_disconnect_export(exp);
5286         if (rc != 0)
5287                 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5288
5289         rc = mdt_export_cleanup(exp);
5290         class_export_put(exp);
5291         RETURN(rc);
5292 }
5293
5294 /* FIXME: Can we avoid using these two interfaces? */
5295 static int mdt_init_export(struct obd_export *exp)
5296 {
5297         struct mdt_export_data *med = &exp->exp_mdt_data;
5298         int                     rc;
5299         ENTRY;
5300
5301         CFS_INIT_LIST_HEAD(&med->med_open_head);
5302         cfs_spin_lock_init(&med->med_open_lock);
5303         cfs_mutex_init(&med->med_idmap_mutex);
5304         med->med_idmap = NULL;
5305         cfs_spin_lock(&exp->exp_lock);
5306         exp->exp_connecting = 1;
5307         cfs_spin_unlock(&exp->exp_lock);
5308
5309         /* self-export doesn't need client data and ldlm initialization */
5310         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5311                                      &exp->exp_client_uuid)))
5312                 RETURN(0);
5313
5314         rc = lut_client_alloc(exp);
5315         if (rc == 0)
5316                 rc = ldlm_init_export(exp);
5317         if (rc)
5318                 CERROR("%s: Error %d while initializing export\n",
5319                        exp->exp_obd->obd_name, rc);
5320         RETURN(rc);
5321 }
5322
5323 static int mdt_destroy_export(struct obd_export *exp)
5324 {
5325         ENTRY;
5326
5327         if (exp_connect_rmtclient(exp))
5328                 mdt_cleanup_idmap(&exp->exp_mdt_data);
5329
5330         target_destroy_export(exp);
5331         /* destroy can be called from failed obd_setup, so
5332          * checking uuid is safer than obd_self_export */
5333         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5334                                      &exp->exp_client_uuid)))
5335                 RETURN(0);
5336
5337         ldlm_destroy_export(exp);
5338         lut_client_free(exp);
5339
5340         LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5341         LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5342
5343         RETURN(0);
5344 }
5345
5346 static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
5347 {
5348         if (flag & CONFIG_LOG)
5349                 cfs_set_bit(MDT_FL_CFGLOG, &m->mdt_state);
5350
5351         /* also notify active event */
5352         if (flag & CONFIG_SYNC)
5353                 cfs_set_bit(MDT_FL_SYNCED, &m->mdt_state);
5354
5355         if (cfs_test_bit(MDT_FL_CFGLOG, &m->mdt_state) &&
5356             cfs_test_bit(MDT_FL_SYNCED, &m->mdt_state)) {
5357                 struct obd_device *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
5358
5359                 /* Open for clients */
5360                 if (obd->obd_no_conn) {
5361                         cfs_spin_lock(&obd->obd_dev_lock);
5362                         obd->obd_no_conn = 0;
5363                         cfs_spin_unlock(&obd->obd_dev_lock);
5364                 }
5365         }
5366 }
5367
5368 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
5369                       enum md_upcall_event ev, void *data)
5370 {
5371         struct mdt_device *m = mdt_dev(&md->md_lu_dev);
5372         struct md_device  *next  = m->mdt_child;
5373         struct mdt_thread_info *mti;
5374         int rc = 0;
5375         ENTRY;
5376
5377         switch (ev) {
5378                 case MD_LOV_SYNC:
5379                         rc = next->md_ops->mdo_maxsize_get(env, next,
5380                                         &m->mdt_max_mdsize,
5381                                         &m->mdt_max_cookiesize);
5382                         CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
5383                                      m->mdt_max_mdsize, m->mdt_max_cookiesize);
5384                         mdt_allow_cli(m, CONFIG_SYNC);
5385                         if (data)
5386                                 (*(__u64 *)data) =
5387                                       m->mdt_lut.lut_obd->u.obt.obt_mount_count;
5388                         break;
5389                 case MD_NO_TRANS:
5390                         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5391                         mti->mti_no_need_trans = 1;
5392                         CDEBUG(D_INFO, "disable mdt trans for this thread\n");
5393                         break;
5394                 case MD_LOV_CONFIG:
5395                         /* Check that MDT is not yet configured */
5396                         LASSERT(!cfs_test_bit(MDT_FL_CFGLOG, &m->mdt_state));
5397                         break;
5398 #ifdef HAVE_QUOTA_SUPPORT
5399                 case MD_LOV_QUOTA:
5400                         if (md->md_lu_dev.ld_obd->obd_recovering == 0 &&
5401                             likely(md->md_lu_dev.ld_obd->obd_stopping == 0))
5402                                 next->md_ops->mdo_quota.mqo_recovery(env, next);
5403                         break;
5404 #endif
5405                 default:
5406                         CERROR("invalid event\n");
5407                         rc = -EINVAL;
5408                         break;
5409         }
5410         RETURN(rc);
5411 }
5412
5413 static int mdt_obd_notify(struct obd_device *obd,
5414                           struct obd_device *watched,
5415                           enum obd_notify_event ev, void *data)
5416 {
5417         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5418 #ifdef HAVE_QUOTA_SUPPORT
5419         struct md_device *next = mdt->mdt_child;
5420 #endif
5421         ENTRY;
5422
5423         switch (ev) {
5424         case OBD_NOTIFY_CONFIG:
5425                 mdt_allow_cli(mdt, (unsigned long)data);
5426
5427 #ifdef HAVE_QUOTA_SUPPORT
5428                /* quota_type has been processed, we can now handle
5429                 * incoming quota requests */
5430                 next->md_ops->mdo_quota.mqo_notify(NULL, next);
5431 #endif
5432                 break;
5433         default:
5434                 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
5435         }
5436         RETURN(0);
5437 }
5438
5439 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5440                             void *val, int vallen)
5441 {
5442         struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5443         struct getinfo_fid2path *fpout, *fpin;
5444         int rc = 0;
5445
5446         fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5447         fpout = val;
5448
5449         if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5450                 lustre_swab_fid2path(fpin);
5451
5452         memcpy(fpout, fpin, sizeof(*fpin));
5453         if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5454                 RETURN(-EINVAL);
5455
5456         rc = mdt_fid2path(info->mti_env, mdt, fpout);
5457         RETURN(rc);
5458 }
5459
5460 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5461                         struct getinfo_fid2path *fp)
5462 {
5463         struct mdt_object *obj;
5464         int    rc;
5465         ENTRY;
5466
5467         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5468                PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5469
5470         if (!fid_is_sane(&fp->gf_fid))
5471                 RETURN(-EINVAL);
5472
5473         obj = mdt_object_find(env, mdt, &fp->gf_fid);
5474         if (obj == NULL || IS_ERR(obj)) {
5475                 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5476                        PTR_ERR(obj));
5477                 RETURN(-EINVAL);
5478         }
5479
5480         rc = lu_object_exists(&obj->mot_obj.mo_lu);
5481         if (rc <= 0) {
5482                 if (rc == -1)
5483                         rc = -EREMOTE;
5484                 else
5485                         rc = -ENOENT;
5486                 mdt_object_put(env, obj);
5487                 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5488                        PFID(&fp->gf_fid), rc);
5489                 RETURN(rc);
5490         }
5491
5492         rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5493                      fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5494         mdt_object_put(env, obj);
5495
5496         RETURN(rc);
5497 }
5498
5499 static int mdt_get_info(struct mdt_thread_info *info)
5500 {
5501         struct ptlrpc_request *req = mdt_info_req(info);
5502         char *key;
5503         int keylen;
5504         __u32 *vallen;
5505         void *valout;
5506         int rc;
5507         ENTRY;
5508
5509         key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5510         if (key == NULL) {
5511                 CDEBUG(D_IOCTL, "No GETINFO key");
5512                 RETURN(-EFAULT);
5513         }
5514         keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5515                                       RCL_CLIENT);
5516
5517         vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5518         if (vallen == NULL) {
5519                 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5520                 RETURN(-EFAULT);
5521         }
5522
5523         req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5524                              *vallen);
5525         rc = req_capsule_server_pack(info->mti_pill);
5526         valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5527         if (valout == NULL) {
5528                 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5529                 RETURN(-EFAULT);
5530         }
5531
5532         if (KEY_IS(KEY_FID2PATH))
5533                 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5534         else
5535                 rc = -EINVAL;
5536
5537         lustre_msg_set_status(req->rq_repmsg, rc);
5538
5539         RETURN(rc);
5540 }
5541
5542 /* Pass the ioc down */
5543 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5544                          unsigned int cmd, int len, void *data)
5545 {
5546         struct lu_context ioctl_session;
5547         struct md_device *next = mdt->mdt_child;
5548         int rc;
5549         ENTRY;
5550
5551         rc = lu_context_init(&ioctl_session, LCT_SESSION);
5552         if (rc)
5553                 RETURN(rc);
5554         ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5555         lu_context_enter(&ioctl_session);
5556         env->le_ses = &ioctl_session;
5557
5558         LASSERT(next->md_ops->mdo_iocontrol);
5559         rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5560
5561         lu_context_exit(&ioctl_session);
5562         lu_context_fini(&ioctl_session);
5563         RETURN(rc);
5564 }
5565
5566 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5567 {
5568         struct obd_ioctl_data *data = karg;
5569         struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5570         __u64 version;
5571         struct mdt_object *obj;
5572         struct mdt_lock_handle  *lh;
5573         int rc;
5574         ENTRY;
5575
5576         CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5577         if (!fid_is_sane(fid))
5578                 RETURN(-EINVAL);
5579
5580         lh = &mti->mti_lh[MDT_LH_PARENT];
5581         mdt_lock_reg_init(lh, LCK_CR);
5582
5583         obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5584         if (IS_ERR(obj))
5585                 RETURN(PTR_ERR(obj));
5586
5587         rc = mdt_object_exists(obj);
5588         if (rc < 0) {
5589                 rc = -EREMOTE;
5590                 /**
5591                  * before calling version get the correct MDS should be
5592                  * fid, this is error to find remote object here
5593                  */
5594                 CERROR("nonlocal object "DFID"\n", PFID(fid));
5595         } else if (rc == 0) {
5596                 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5597                 rc = -ENOENT;
5598         } else {
5599                 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5600                *(__u64 *)data->ioc_inlbuf2 = version;
5601                 rc = 0;
5602         }
5603         mdt_object_unlock_put(mti, obj, lh, 1);
5604         RETURN(rc);
5605 }
5606
5607 /* ioctls on obd dev */
5608 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5609                          void *karg, void *uarg)
5610 {
5611         struct lu_env      env;
5612         struct obd_device *obd = exp->exp_obd;
5613         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5614         struct dt_device  *dt = mdt->mdt_bottom;
5615         int rc;
5616
5617         ENTRY;
5618         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5619         rc = lu_env_init(&env, LCT_MD_THREAD);
5620         if (rc)
5621                 RETURN(rc);
5622
5623         switch (cmd) {
5624         case OBD_IOC_SYNC:
5625                 rc = mdt_device_sync(&env, mdt);
5626                 break;
5627         case OBD_IOC_SET_READONLY:
5628                 rc = dt->dd_ops->dt_ro(&env, dt);
5629                 break;
5630         case OBD_IOC_ABORT_RECOVERY:
5631                 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5632                 target_stop_recovery_thread(obd);
5633                 rc = 0;
5634                 break;
5635         case OBD_IOC_CHANGELOG_REG:
5636         case OBD_IOC_CHANGELOG_DEREG:
5637         case OBD_IOC_CHANGELOG_CLEAR:
5638                 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5639                 break;
5640         case OBD_IOC_GET_OBJ_VERSION: {
5641                 struct mdt_thread_info *mti;
5642                 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5643                 memset(mti, 0, sizeof *mti);
5644                 mti->mti_env = &env;
5645                 mti->mti_mdt = mdt;
5646                 mti->mti_exp = exp;
5647
5648                 rc = mdt_ioc_version_get(mti, karg);
5649                 break;
5650         }
5651         default:
5652                 CERROR("Not supported cmd = %d for device %s\n",
5653                        cmd, obd->obd_name);
5654                 rc = -EOPNOTSUPP;
5655         }
5656
5657         lu_env_fini(&env);
5658         RETURN(rc);
5659 }
5660
5661 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5662 {
5663         struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5664 #ifdef HAVE_QUOTA_SUPPORT
5665         struct obd_device *obd = mdt2obd_dev(mdt);
5666         struct md_device *next = mdt->mdt_child;
5667 #endif
5668         int rc;
5669         ENTRY;
5670
5671         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5672 #ifdef HAVE_QUOTA_SUPPORT
5673         if (likely(obd->obd_stopping == 0))
5674                 next->md_ops->mdo_quota.mqo_recovery(env, next);
5675 #endif
5676         RETURN(rc);
5677 }
5678
5679 int mdt_obd_postrecov(struct obd_device *obd)
5680 {
5681         struct lu_env env;
5682         int rc;
5683
5684         rc = lu_env_init(&env, LCT_MD_THREAD);
5685         if (rc)
5686                 RETURN(rc);
5687         rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5688         lu_env_fini(&env);
5689         return rc;
5690 }
5691
5692 /**
5693  * Send a copytool req to a client
5694  * Note this sends a request RPC from a server (MDT) to a client (MDC),
5695  * backwards of normal comms.
5696  */
5697 int mdt_hsm_copytool_send(struct obd_export *exp)
5698 {
5699         struct kuc_hdr *lh;
5700         struct hsm_action_list *hal;
5701         struct hsm_action_item *hai;
5702         int rc, len;
5703         ENTRY;
5704
5705         CWARN("%s: writing to mdc at %s\n", exp->exp_obd->obd_name,
5706               libcfs_nid2str(exp->exp_connection->c_peer.nid));
5707
5708         len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
5709                 /* for mockup below */ 2 * cfs_size_round(sizeof(*hai));
5710         OBD_ALLOC(lh, len);
5711         if (lh == NULL)
5712                 RETURN(-ENOMEM);
5713
5714         lh->kuc_magic = KUC_MAGIC;
5715         lh->kuc_transport = KUC_TRANSPORT_HSM;
5716         lh->kuc_msgtype = HMT_ACTION_LIST;
5717         lh->kuc_msglen = len;
5718
5719         hal = (struct hsm_action_list *)(lh + 1);
5720         hal->hal_version = HAL_VERSION;
5721         hal->hal_archive_num = 1;
5722         obd_uuid2fsname(hal->hal_fsname, exp->exp_obd->obd_name,
5723                         MTI_NAME_MAXLEN);
5724
5725         /* mock up an action list */
5726         hal->hal_count = 2;
5727         hai = hai_zero(hal);
5728         hai->hai_action = HSMA_ARCHIVE;
5729         hai->hai_fid.f_oid = 0xA00A;
5730         hai->hai_len = sizeof(*hai);
5731         hai = hai_next(hai);
5732         hai->hai_action = HSMA_RESTORE;
5733         hai->hai_fid.f_oid = 0xB00B;
5734         hai->hai_len = sizeof(*hai);
5735
5736         /* Uses the ldlm reverse import; this rpc will be seen by
5737           the ldlm_callback_handler */
5738         rc = do_set_info_async(exp->exp_imp_reverse,
5739                                LDLM_SET_INFO, LUSTRE_OBD_VERSION,
5740                                sizeof(KEY_HSM_COPYTOOL_SEND),
5741                                KEY_HSM_COPYTOOL_SEND,
5742                                len, lh, NULL);
5743
5744         OBD_FREE(lh, len);
5745
5746         RETURN(rc);
5747 }
5748
5749 static struct obd_ops mdt_obd_device_ops = {
5750         .o_owner          = THIS_MODULE,
5751         .o_set_info_async = mdt_obd_set_info_async,
5752         .o_connect        = mdt_obd_connect,
5753         .o_reconnect      = mdt_obd_reconnect,
5754         .o_disconnect     = mdt_obd_disconnect,
5755         .o_init_export    = mdt_init_export,
5756         .o_destroy_export = mdt_destroy_export,
5757         .o_iocontrol      = mdt_iocontrol,
5758         .o_postrecov      = mdt_obd_postrecov,
5759         .o_notify         = mdt_obd_notify
5760 };
5761
5762 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5763                                          struct lu_device *d)
5764 {
5765         struct mdt_device *m = mdt_dev(d);
5766         ENTRY;
5767
5768         mdt_fini(env, m);
5769         RETURN(NULL);
5770 }
5771
5772 static struct lu_device *mdt_device_free(const struct lu_env *env,
5773                                          struct lu_device *d)
5774 {
5775         struct mdt_device *m = mdt_dev(d);
5776         ENTRY;
5777
5778         md_device_fini(&m->mdt_md_dev);
5779         OBD_FREE_PTR(m);
5780         RETURN(NULL);
5781 }
5782
5783 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5784                                           struct lu_device_type *t,
5785                                           struct lustre_cfg *cfg)
5786 {
5787         struct lu_device  *l;
5788         struct mdt_device *m;
5789
5790         OBD_ALLOC_PTR(m);
5791         if (m != NULL) {
5792                 int rc;
5793
5794                 l = &m->mdt_md_dev.md_lu_dev;
5795                 rc = mdt_init0(env, m, t, cfg);
5796                 if (rc != 0) {
5797                         mdt_device_free(env, l);
5798                         l = ERR_PTR(rc);
5799                         return l;
5800                 }
5801                 md_upcall_init(&m->mdt_md_dev, mdt_upcall);
5802         } else
5803                 l = ERR_PTR(-ENOMEM);
5804         return l;
5805 }
5806
5807 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5808 LU_KEY_INIT_FINI(mdt, struct mdt_thread_info);
5809
5810 /* context key: mdt_thread_key */
5811 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5812
5813 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
5814 {
5815         return md_ucred(info->mti_env);
5816 }
5817
5818 /**
5819  * Enable/disable COS (Commit On Sharing).
5820  *
5821  * Set/Clear the COS flag in mdt options.
5822  *
5823  * \param mdt mdt device
5824  * \param val 0 disables COS, other values enable COS
5825  */
5826 void mdt_enable_cos(struct mdt_device *mdt, int val)
5827 {
5828         struct lu_env env;
5829         int rc;
5830
5831         mdt->mdt_opts.mo_cos = !!val;
5832         rc = lu_env_init(&env, LCT_LOCAL);
5833         if (unlikely(rc != 0)) {
5834                 CWARN("lu_env initialization failed with rc = %d,"
5835                       "cannot sync\n", rc);
5836                 return;
5837         }
5838         mdt_device_sync(&env, mdt);
5839         lu_env_fini(&env);
5840 }
5841
5842 /**
5843  * Check COS (Commit On Sharing) status.
5844  *
5845  * Return COS flag status.
5846  *
5847  * \param mdt mdt device
5848  */
5849 int mdt_cos_is_enabled(struct mdt_device *mdt)
5850 {
5851         return mdt->mdt_opts.mo_cos != 0;
5852 }
5853
5854 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
5855 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key);
5856
5857 static struct lu_device_type_operations mdt_device_type_ops = {
5858         .ldto_init = mdt_type_init,
5859         .ldto_fini = mdt_type_fini,
5860
5861         .ldto_start = mdt_type_start,
5862         .ldto_stop  = mdt_type_stop,
5863
5864         .ldto_device_alloc = mdt_device_alloc,
5865         .ldto_device_free  = mdt_device_free,
5866         .ldto_device_fini  = mdt_device_fini
5867 };
5868
5869 static struct lu_device_type mdt_device_type = {
5870         .ldt_tags     = LU_DEVICE_MD,
5871         .ldt_name     = LUSTRE_MDT_NAME,
5872         .ldt_ops      = &mdt_device_type_ops,
5873         .ldt_ctx_tags = LCT_MD_THREAD
5874 };
5875
5876 static int __init mdt_mod_init(void)
5877 {
5878         struct lprocfs_static_vars lvars;
5879         int rc;
5880
5881         lprocfs_mdt_init_vars(&lvars);
5882         rc = class_register_type(&mdt_obd_device_ops, NULL,
5883                                  lvars.module_vars, LUSTRE_MDT_NAME,
5884                                  &mdt_device_type);
5885
5886         return rc;
5887 }
5888
5889 static void __exit mdt_mod_exit(void)
5890 {
5891         class_unregister_type(LUSTRE_MDT_NAME);
5892 }
5893
5894
5895 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt)             \
5896 [prefix ## _ ## opc - prefix ## _ ## base] = {                          \
5897         .mh_name    = #opc,                                             \
5898         .mh_fail_id = OBD_FAIL_ ## prefix ## _  ## opc ## suffix,       \
5899         .mh_opc     = prefix ## _  ## opc,                              \
5900         .mh_flags   = flags,                                            \
5901         .mh_act     = fn,                                               \
5902         .mh_fmt     = fmt                                               \
5903 }
5904
5905 #define DEF_MDT_HNDL(flags, name, fn, fmt)                                  \
5906         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
5907
5908 #define DEF_SEQ_HNDL(flags, name, fn, fmt)                      \
5909         DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
5910
5911 #define DEF_FLD_HNDL(flags, name, fn, fmt)                      \
5912         DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
5913 /*
5914  * Request with a format known in advance
5915  */
5916 #define DEF_MDT_HNDL_F(flags, name, fn)                                 \
5917         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
5918
5919 #define DEF_SEQ_HNDL_F(flags, name, fn)                                 \
5920         DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
5921
5922 #define DEF_FLD_HNDL_F(flags, name, fn)                                 \
5923         DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
5924 /*
5925  * Request with a format we do not yet know
5926  */
5927 #define DEF_MDT_HNDL_0(flags, name, fn)                                 \
5928         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
5929
5930 static struct mdt_handler mdt_mds_ops[] = {
5931 DEF_MDT_HNDL_F(0,                         CONNECT,      mdt_connect),
5932 DEF_MDT_HNDL_F(0,                         DISCONNECT,   mdt_disconnect),
5933 DEF_MDT_HNDL  (0,                         SET_INFO,     mdt_set_info,
5934                                                              &RQF_OBD_SET_INFO),
5935 DEF_MDT_HNDL_F(0,                         GET_INFO,     mdt_get_info),
5936 DEF_MDT_HNDL_F(0           |HABEO_REFERO, GETSTATUS,    mdt_getstatus),
5937 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETATTR,      mdt_getattr),
5938 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
5939 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETXATTR,     mdt_getxattr),
5940 DEF_MDT_HNDL_F(0           |HABEO_REFERO, STATFS,       mdt_statfs),
5941 DEF_MDT_HNDL_F(0           |MUTABOR,      REINT,        mdt_reint),
5942 DEF_MDT_HNDL_F(HABEO_CORPUS,              CLOSE,        mdt_close),
5943 DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING, mdt_done_writing),
5944 DEF_MDT_HNDL_F(0           |HABEO_REFERO, PIN,          mdt_pin),
5945 DEF_MDT_HNDL_0(0,                         SYNC,         mdt_sync),
5946 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
5947 #ifdef HAVE_QUOTA_SUPPORT
5948 DEF_MDT_HNDL_F(0,                         QUOTACHECK,   mdt_quotacheck_handle),
5949 DEF_MDT_HNDL_F(0,                         QUOTACTL,     mdt_quotactl_handle)
5950 #endif
5951 };
5952
5953 #define DEF_OBD_HNDL(flags, name, fn)                   \
5954         DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
5955
5956
5957 static struct mdt_handler mdt_obd_ops[] = {
5958         DEF_OBD_HNDL(0, PING,           mdt_obd_ping),
5959         DEF_OBD_HNDL(0, LOG_CANCEL,     mdt_obd_log_cancel),
5960         DEF_OBD_HNDL(0, QC_CALLBACK,    mdt_obd_qc_callback)
5961 };
5962
5963 #define DEF_DLM_HNDL_0(flags, name, fn)                   \
5964         DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
5965 #define DEF_DLM_HNDL_F(flags, name, fn)                   \
5966         DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
5967
5968 static struct mdt_handler mdt_dlm_ops[] = {
5969         DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE,        mdt_enqueue),
5970         DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT,        mdt_convert),
5971         DEF_DLM_HNDL_0(0,            BL_CALLBACK,    mdt_bl_callback),
5972         DEF_DLM_HNDL_0(0,            CP_CALLBACK,    mdt_cp_callback)
5973 };
5974
5975 #define DEF_LLOG_HNDL(flags, name, fn)                   \
5976         DEF_HNDL(LLOG, ORIGIN_HANDLE_CREATE, _NET, flags, name, fn, NULL)
5977
5978 static struct mdt_handler mdt_llog_ops[] = {
5979         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CREATE,      mdt_llog_create),
5980         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_NEXT_BLOCK,  mdt_llog_next_block),
5981         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_READ_HEADER, mdt_llog_read_header),
5982         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_WRITE_REC,   NULL),
5983         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CLOSE,       NULL),
5984         DEF_LLOG_HNDL(0, ORIGIN_CONNECT,            NULL),
5985         DEF_LLOG_HNDL(0, CATINFO,                   NULL),
5986         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_PREV_BLOCK,  mdt_llog_prev_block),
5987         DEF_LLOG_HNDL(0, ORIGIN_HANDLE_DESTROY,     mdt_llog_destroy),
5988 };
5989
5990 #define DEF_SEC_CTX_HNDL(name, fn)                      \
5991         DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
5992
5993 static struct mdt_handler mdt_sec_ctx_ops[] = {
5994         DEF_SEC_CTX_HNDL(INIT,          mdt_sec_ctx_handle),
5995         DEF_SEC_CTX_HNDL(INIT_CONT,     mdt_sec_ctx_handle),
5996         DEF_SEC_CTX_HNDL(FINI,          mdt_sec_ctx_handle)
5997 };
5998
5999 static struct mdt_opc_slice mdt_regular_handlers[] = {
6000         {
6001                 .mos_opc_start = MDS_GETATTR,
6002                 .mos_opc_end   = MDS_LAST_OPC,
6003                 .mos_hs        = mdt_mds_ops
6004         },
6005         {
6006                 .mos_opc_start = OBD_PING,
6007                 .mos_opc_end   = OBD_LAST_OPC,
6008                 .mos_hs        = mdt_obd_ops
6009         },
6010         {
6011                 .mos_opc_start = LDLM_ENQUEUE,
6012                 .mos_opc_end   = LDLM_LAST_OPC,
6013                 .mos_hs        = mdt_dlm_ops
6014         },
6015         {
6016                 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
6017                 .mos_opc_end   = LLOG_LAST_OPC,
6018                 .mos_hs        = mdt_llog_ops
6019         },
6020         {
6021                 .mos_opc_start = SEC_CTX_INIT,
6022                 .mos_opc_end   = SEC_LAST_OPC,
6023                 .mos_hs        = mdt_sec_ctx_ops
6024         },
6025         {
6026                 .mos_hs        = NULL
6027         }
6028 };
6029
6030 static struct mdt_handler mdt_readpage_ops[] = {
6031         DEF_MDT_HNDL_F(0,                         CONNECT,  mdt_connect),
6032         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
6033 #ifdef HAVE_SPLIT_SUPPORT
6034         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
6035 #endif
6036
6037         /*
6038          * XXX: this is ugly and should be fixed one day, see mdc_close() for
6039          * detailed comments. --umka
6040          */
6041         DEF_MDT_HNDL_F(HABEO_CORPUS,              CLOSE,    mdt_close),
6042         DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING,    mdt_done_writing),
6043 };
6044
6045 static struct mdt_opc_slice mdt_readpage_handlers[] = {
6046         {
6047                 .mos_opc_start = MDS_GETATTR,
6048                 .mos_opc_end   = MDS_LAST_OPC,
6049                 .mos_hs        = mdt_readpage_ops
6050         },
6051         {
6052                 .mos_hs        = NULL
6053         }
6054 };
6055
6056 static struct mdt_handler mdt_xmds_ops[] = {
6057         DEF_MDT_HNDL_F(0,                         CONNECT,      mdt_connect),
6058         DEF_MDT_HNDL_F(HABEO_CORPUS             , GETATTR,      mdt_getattr),
6059         DEF_MDT_HNDL_F(0 | MUTABOR              , REINT,        mdt_reint),
6060         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
6061 };
6062
6063 static struct mdt_opc_slice mdt_xmds_handlers[] = {
6064         {
6065                 .mos_opc_start = MDS_GETATTR,
6066                 .mos_opc_end   = MDS_LAST_OPC,
6067                 .mos_hs        = mdt_xmds_ops
6068         },
6069         {
6070                 .mos_opc_start = OBD_PING,
6071                 .mos_opc_end   = OBD_LAST_OPC,
6072                 .mos_hs        = mdt_obd_ops
6073         },
6074         {
6075                 .mos_opc_start = SEC_CTX_INIT,
6076                 .mos_opc_end   = SEC_LAST_OPC,
6077                 .mos_hs        = mdt_sec_ctx_ops
6078         },
6079         {
6080                 .mos_hs        = NULL
6081         }
6082 };
6083
6084 static struct mdt_handler mdt_seq_ops[] = {
6085         DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
6086 };
6087
6088 static struct mdt_opc_slice mdt_seq_handlers[] = {
6089         {
6090                 .mos_opc_start = SEQ_QUERY,
6091                 .mos_opc_end   = SEQ_LAST_OPC,
6092                 .mos_hs        = mdt_seq_ops
6093         },
6094         {
6095                 .mos_hs        = NULL
6096         }
6097 };
6098
6099 static struct mdt_handler mdt_fld_ops[] = {
6100         DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
6101 };
6102
6103 static struct mdt_opc_slice mdt_fld_handlers[] = {
6104         {
6105                 .mos_opc_start = FLD_QUERY,
6106                 .mos_opc_end   = FLD_LAST_OPC,
6107                 .mos_hs        = mdt_fld_ops
6108         },
6109         {
6110                 .mos_hs        = NULL
6111         }
6112 };
6113
6114 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6115 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
6116 MODULE_LICENSE("GPL");
6117
6118 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
6119                 "number of mdt service threads to start");
6120
6121 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);