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