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