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