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