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