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