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