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