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