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