Whamcloud - gitweb
Branch HEAD
[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  *  lustre/mdt/mdt_handler.c
5  *  Lustre Metadata Target (mdt) request handler
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *   Author: Mike Shaver <shaver@clusterfs.com>
12  *   Author: Nikita Danilov <nikita@clusterfs.com>
13  *   Author: Huang Hua <huanghua@clusterfs.com>
14  *   Author: Yury Umanets <umka@clusterfs.com>
15  *
16  *   This file is part of the Lustre file system, http://www.lustre.org
17  *   Lustre is a trademark of Cluster File Systems, Inc.
18  *
19  *   You may have signed or agreed to another license before downloading
20  *   this software.  If so, you are bound by the terms and conditions
21  *   of that agreement, and the following does not apply to you.  See the
22  *   LICENSE file included with this distribution for more information.
23  *
24  *   If you did not agree to a different license, then this copy of Lustre
25  *   is open source software; you can redistribute it and/or modify it
26  *   under the terms of version 2 of the GNU General Public License as
27  *   published by the Free Software Foundation.
28  *
29  *   In either case, Lustre is distributed in the hope that it will be
30  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
31  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  *   license text for more details.
33  */
34
35 #ifndef EXPORT_SYMTAB
36 # define EXPORT_SYMTAB
37 #endif
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <linux/module.h>
41 /*
42  * struct OBD_{ALLOC,FREE}*()
43  */
44 #include <obd_support.h>
45 /* struct ptlrpc_request */
46 #include <lustre_net.h>
47 /* struct obd_export */
48 #include <lustre_export.h>
49 /* struct obd_device */
50 #include <obd.h>
51 /* lu2dt_dev() */
52 #include <dt_object.h>
53 #include <lustre_mds.h>
54 #include <lustre_mdt.h>
55 #include "mdt_internal.h"
56 #include <linux/lustre_acl.h>
57 #include <lustre_param.h>
58
59 mdl_mode_t mdt_mdl_lock_modes[] = {
60         [LCK_MINMODE] = MDL_MINMODE,
61         [LCK_EX]      = MDL_EX,
62         [LCK_PW]      = MDL_PW,
63         [LCK_PR]      = MDL_PR,
64         [LCK_CW]      = MDL_CW,
65         [LCK_CR]      = MDL_CR,
66         [LCK_NL]      = MDL_NL,
67         [LCK_GROUP]   = MDL_GROUP
68 };
69
70 ldlm_mode_t mdt_dlm_lock_modes[] = {
71         [MDL_MINMODE] = LCK_MINMODE,
72         [MDL_EX]      = LCK_EX,
73         [MDL_PW]      = LCK_PW,
74         [MDL_PR]      = LCK_PR,
75         [MDL_CW]      = LCK_CW,
76         [MDL_CR]      = LCK_CR,
77         [MDL_NL]      = LCK_NL,
78         [MDL_GROUP]   = LCK_GROUP
79 };
80
81 /*
82  * Initialized in mdt_mod_init().
83  */
84 unsigned long mdt_num_threads;
85
86 /* ptlrpc request handler for MDT. All handlers are
87  * grouped into several slices - struct mdt_opc_slice,
88  * and stored in an array - mdt_handlers[].
89  */
90 struct mdt_handler {
91         /* The name of this handler. */
92         const char *mh_name;
93         /* Fail id for this handler, checked at the beginning of this handler*/
94         int         mh_fail_id;
95         /* Operation code for this handler */
96         __u32       mh_opc;
97         /* flags are listed in enum mdt_handler_flags below. */
98         __u32       mh_flags;
99         /* The actual handler function to execute. */
100         int (*mh_act)(struct mdt_thread_info *info);
101         /* Request format for this request. */
102         const struct req_format *mh_fmt;
103 };
104
105 enum mdt_handler_flags {
106         /*
107          * struct mdt_body is passed in the incoming message, and object
108          * identified by this fid exists on disk.
109          *
110          * "habeo corpus" == "I have a body"
111          */
112         HABEO_CORPUS = (1 << 0),
113         /*
114          * struct ldlm_request is passed in the incoming message.
115          *
116          * "habeo clavis" == "I have a key"
117          */
118         HABEO_CLAVIS = (1 << 1),
119         /*
120          * this request has fixed reply format, so that reply message can be
121          * packed by generic code.
122          *
123          * "habeo refero" == "I have a reply"
124          */
125         HABEO_REFERO = (1 << 2),
126         /*
127          * this request will modify something, so check whether the filesystem
128          * is readonly or not, then return -EROFS to client asap if necessary.
129          *
130          * "mutabor" == "I shall modify"
131          */
132         MUTABOR      = (1 << 3)
133 };
134
135 struct mdt_opc_slice {
136         __u32               mos_opc_start;
137         int                 mos_opc_end;
138         struct mdt_handler *mos_hs;
139 };
140
141 static struct mdt_opc_slice mdt_regular_handlers[];
142 static struct mdt_opc_slice mdt_readpage_handlers[];
143 static struct mdt_opc_slice mdt_xmds_handlers[];
144 static struct mdt_opc_slice mdt_seq_handlers[];
145 static struct mdt_opc_slice mdt_fld_handlers[];
146
147 static struct mdt_device *mdt_dev(struct lu_device *d);
148 static int mdt_regular_handle(struct ptlrpc_request *req);
149 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
150
151 static struct lu_object_operations mdt_obj_ops;
152
153 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
154 {
155         if (!rep)
156                 return 0;
157         return (rep->lock_policy_res1 & flag);
158 }
159
160 void mdt_clear_disposition(struct mdt_thread_info *info,
161                            struct ldlm_reply *rep, int flag)
162 {
163         if (info)
164                 info->mti_opdata &= ~flag;
165         if (rep)
166                 rep->lock_policy_res1 &= ~flag;
167 }
168
169 void mdt_set_disposition(struct mdt_thread_info *info,
170                          struct ldlm_reply *rep, int flag)
171 {
172         if (info)
173                 info->mti_opdata |= flag;
174         if (rep)
175                 rep->lock_policy_res1 |= flag;
176 }
177
178 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
179 {
180         lh->mlh_pdo_hash = 0;
181         lh->mlh_reg_mode = lm;
182         lh->mlh_type = MDT_REG_LOCK;
183 }
184
185 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
186                        const char *name, int namelen)
187 {
188         lh->mlh_reg_mode = lm;
189         lh->mlh_type = MDT_PDO_LOCK;
190
191         if (name != NULL) {
192                 LASSERT(namelen > 0);
193                 lh->mlh_pdo_hash = full_name_hash(name, namelen);
194         } else {
195                 LASSERT(namelen == 0);
196                 lh->mlh_pdo_hash = 0ull;
197         }
198 }
199
200 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
201                               struct mdt_lock_handle *lh)
202 {
203         mdl_mode_t mode;
204         ENTRY;
205
206         /*
207          * Any dir access needs couple of locks:
208          *
209          * 1) on part of dir we gonna take lookup/modify;
210          *
211          * 2) on whole dir to protect it from concurrent splitting and/or to
212          * flush client's cache for readdir().
213          *
214          * so, for a given mode and object this routine decides what lock mode
215          * to use for lock #2:
216          *
217          * 1) if caller's gonna lookup in dir then we need to protect dir from
218          * being splitted only - LCK_CR
219          *
220          * 2) if caller's gonna modify dir then we need to protect dir from
221          * being splitted and to flush cache - LCK_CW
222          *
223          * 3) if caller's gonna modify dir and that dir seems ready for
224          * splitting then we need to protect it from any type of access
225          * (lookup/modify/split) - LCK_EX --bzzz
226          */
227
228         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
229         LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
230
231         /*
232          * Ask underlaying level its opinion about preferable PDO lock mode
233          * having access type passed as regular lock mode:
234          *
235          * - MDL_MINMODE means that lower layer does not want to specify lock
236          * mode;
237          *
238          * - MDL_NL means that no PDO lock should be taken. This is used in some
239          * cases. Say, for non-splittable directories no need to use PDO locks
240          * at all.
241          */
242         mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
243                              mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
244
245         if (mode != MDL_MINMODE) {
246                 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
247         } else {
248                 /*
249                  * Lower layer does not want to specify locking mode. We do it
250                  * our selves. No special protection is needed, just flush
251                  * client's cache on modification and allow concurrent
252                  * mondification.
253                  */
254                 switch (lh->mlh_reg_mode) {
255                 case LCK_EX:
256                         lh->mlh_pdo_mode = LCK_EX;
257                         break;
258                 case LCK_PR:
259                         lh->mlh_pdo_mode = LCK_CR;
260                         break;
261                 case LCK_PW:
262                         lh->mlh_pdo_mode = LCK_CW;
263                         break;
264                 default:
265                         CERROR("Not expected lock type (0x%x)\n",
266                                (int)lh->mlh_reg_mode);
267                         LBUG();
268                 }
269         }
270
271         LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
272         EXIT;
273 }
274
275 static int mdt_getstatus(struct mdt_thread_info *info)
276 {
277         struct mdt_device *mdt  = info->mti_mdt;
278         struct md_device  *next = mdt->mdt_child;
279         struct mdt_body   *repbody;
280         int                rc;
281
282         ENTRY;
283
284         rc = mdt_check_ucred(info);
285         if (rc)
286                 RETURN(err_serious(rc));
287
288         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
289                 RETURN(err_serious(-ENOMEM));
290
291         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
292         rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
293         if (rc != 0)
294                 RETURN(rc);
295
296         repbody->valid |= OBD_MD_FLID;
297
298         if (mdt->mdt_opts.mo_mds_capa) {
299                 struct mdt_object  *root;
300                 struct lustre_capa *capa;
301
302                 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
303                 if (IS_ERR(root))
304                         RETURN(PTR_ERR(root));
305
306                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
307                 LASSERT(capa);
308                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
309
310                 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
311                                  0);
312                 mdt_object_put(info->mti_env, root);
313                 if (rc == 0)
314                         repbody->valid |= OBD_MD_FLMDSCAPA;
315         }
316
317         RETURN(rc);
318 }
319
320 static int mdt_statfs(struct mdt_thread_info *info)
321 {
322         struct md_device  *next  = info->mti_mdt->mdt_child;
323         struct obd_statfs *osfs;
324         int                rc;
325
326         ENTRY;
327
328         /* This will trigger a watchdog timeout */
329         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
330                          (MDT_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
331
332         rc = mdt_check_ucred(info);
333         if (rc)
334                 RETURN(err_serious(rc));
335
336         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
337                 rc = err_serious(-ENOMEM);
338         } else {
339                 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
340                 rc = next->md_ops->mdo_statfs(info->mti_env, next,
341                                               &info->mti_u.ksfs);
342                 statfs_pack(osfs, &info->mti_u.ksfs);
343         }
344         RETURN(rc);
345 }
346
347 void mdt_pack_size2body(struct mdt_thread_info *info, struct mdt_object *o)
348 {
349         struct mdt_body *b;
350         struct lu_attr *attr = &info->mti_attr.ma_attr;
351
352         b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
353
354         /* Check if Size-on-MDS is enabled. */
355         if ((mdt_conn_flags(info) & OBD_CONNECT_SOM) &&
356             S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
357                 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
358                 b->size = attr->la_size;
359                 b->blocks = attr->la_blocks;
360         }
361 }
362
363 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
364                         const struct lu_attr *attr, const struct lu_fid *fid)
365 {
366         /*XXX should pack the reply body according to lu_valid*/
367         b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID   |
368                     OBD_MD_FLGID   | OBD_MD_FLTYPE  |
369                     OBD_MD_FLMODE  | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
370                     OBD_MD_FLATIME | OBD_MD_FLMTIME ;
371
372         if (!S_ISREG(attr->la_mode))
373                 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
374
375         b->atime      = attr->la_atime;
376         b->mtime      = attr->la_mtime;
377         b->ctime      = attr->la_ctime;
378         b->mode       = attr->la_mode;
379         b->size       = attr->la_size;
380         b->blocks     = attr->la_blocks;
381         b->uid        = attr->la_uid;
382         b->gid        = attr->la_gid;
383         b->flags      = attr->la_flags;
384         b->nlink      = attr->la_nlink;
385         b->rdev       = attr->la_rdev;
386
387         if (fid) {
388                 b->fid1 = *fid;
389                 b->valid |= OBD_MD_FLID;
390
391                 /* FIXME: these should be fixed when new igif ready.*/
392                 b->ino  =  fid_oid(fid);       /* 1.6 compatibility */
393                 b->generation = fid_ver(fid);  /* 1.6 compatibility */
394                 b->valid |= OBD_MD_FLGENER;    /* 1.6 compatibility */
395
396                 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
397                                 PFID(fid), b->nlink, b->mode, b->size);
398         }
399
400         if (info)
401                 mdt_body_reverse_idmap(info, b);
402 }
403
404 static inline int mdt_body_has_lov(const struct lu_attr *la,
405                                    const struct mdt_body *body)
406 {
407         return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
408                 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
409 }
410
411 static int mdt_getattr_internal(struct mdt_thread_info *info,
412                                 struct mdt_object *o)
413 {
414         struct md_object        *next = mdt_object_child(o);
415         const struct mdt_body   *reqbody = info->mti_body;
416         struct ptlrpc_request   *req = mdt_info_req(info);
417         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
418         struct md_attr          *ma = &info->mti_attr;
419         struct lu_attr          *la = &ma->ma_attr;
420         struct req_capsule      *pill = info->mti_pill;
421         const struct lu_env     *env = info->mti_env;
422         struct mdt_body         *repbody;
423         struct lu_buf           *buffer = &info->mti_buf;
424         int                     rc;
425         ENTRY;
426
427         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
428                 RETURN(err_serious(-ENOMEM));
429
430         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
431
432         ma->ma_valid = 0;
433
434         rc = mdt_object_exists(o);
435         if (rc < 0) {
436                 /* This object is located on remote node.*/
437                 repbody->fid1 = *mdt_object_fid(o);
438                 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
439                 RETURN(0);
440         }
441
442         buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
443         buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
444
445         /* If it is dir object and client require MEA, then we got MEA */
446         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
447             reqbody->valid & OBD_MD_MEA) {
448                 /* Assumption: MDT_MD size is enough for lmv size. */
449                 ma->ma_lmv = buffer->lb_buf;
450                 ma->ma_lmv_size = buffer->lb_len;
451                 ma->ma_need = MA_LMV | MA_INODE;
452         } else {
453                 ma->ma_lmm = buffer->lb_buf;
454                 ma->ma_lmm_size = buffer->lb_len;
455                 ma->ma_need = MA_LOV | MA_INODE;
456         }
457
458         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
459             reqbody->valid & OBD_MD_FLDIREA  &&
460             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
461                 /* get default stripe info for this dir. */
462                 ma->ma_need |= MA_LOV_DEF;
463         }
464         rc = mo_attr_get(env, next, ma);
465         if (unlikely(rc)) {
466                 CERROR("getattr error for "DFID": %d\n",
467                         PFID(mdt_object_fid(o)), rc);
468                 RETURN(rc);
469         }
470
471         if (likely(ma->ma_valid & MA_INODE))
472                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
473         else
474                 RETURN(-EFAULT);
475
476         if (mdt_body_has_lov(la, reqbody)) {
477                 if (ma->ma_valid & MA_LOV) {
478                         LASSERT(ma->ma_lmm_size);
479                         mdt_dump_lmm(D_INFO, ma->ma_lmm);
480                         repbody->eadatasize = ma->ma_lmm_size;
481                         if (S_ISDIR(la->la_mode))
482                                 repbody->valid |= OBD_MD_FLDIREA;
483                         else
484                                 repbody->valid |= OBD_MD_FLEASIZE;
485                 }
486                 if (ma->ma_valid & MA_LMV) {
487                         LASSERT(S_ISDIR(la->la_mode));
488                         repbody->eadatasize = ma->ma_lmv_size;
489                         repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
490                 }
491                 if (!(ma->ma_valid & MA_LOV) && !(ma->ma_valid & MA_LMV)) {
492                         repbody->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
493                 }
494         } else if (S_ISLNK(la->la_mode) &&
495                    reqbody->valid & OBD_MD_LINKNAME) {
496                 buffer->lb_buf = ma->ma_lmm;
497                 buffer->lb_len = reqbody->eadatasize;
498                 rc = mo_readlink(env, next, buffer);
499                 if (unlikely(rc <= 0)) {
500                         CERROR("readlink failed: %d\n", rc);
501                         rc = -EFAULT;
502                 } else {
503                         repbody->valid |= OBD_MD_LINKNAME;
504                         repbody->eadatasize = rc;
505                         /* NULL terminate */
506                         ((char*)ma->ma_lmm)[rc - 1] = 0;
507                         CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
508                                (char*)ma->ma_lmm, rc);
509                         rc = 0;
510                 }
511         }
512
513         if (reqbody->valid & OBD_MD_FLMODEASIZE) {
514                 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
515                 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
516                 repbody->valid |= OBD_MD_FLMODEASIZE;
517                 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
518                        "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
519                        repbody->max_cookiesize);
520         }
521
522         if (med->med_rmtclient && (reqbody->valid & OBD_MD_FLRMTPERM)) {
523                 void *buf = req_capsule_server_get(pill, &RMF_ACL);
524
525                 /* mdt_getattr_lock only */
526                 rc = mdt_pack_remote_perm(info, o, buf);
527                 if (rc) {
528                         repbody->valid &= ~OBD_MD_FLRMTPERM;
529                         repbody->aclsize = 0;
530                         RETURN(rc);
531                 } else {
532                         repbody->valid |= OBD_MD_FLRMTPERM;
533                         repbody->aclsize = sizeof(struct mdt_remote_perm);
534                 }
535         }
536 #ifdef CONFIG_FS_POSIX_ACL
537         else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
538                  (reqbody->valid & OBD_MD_FLACL)) {
539                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
540                 buffer->lb_len = req_capsule_get_size(pill,
541                                                       &RMF_ACL, RCL_SERVER);
542                 if (buffer->lb_len > 0) {
543                         rc = mo_xattr_get(env, next, buffer,
544                                           XATTR_NAME_ACL_ACCESS);
545                         if (rc < 0) {
546                                 if (rc == -ENODATA) {
547                                         repbody->aclsize = 0;
548                                         repbody->valid |= OBD_MD_FLACL;
549                                         rc = 0;
550                                 } else if (rc == -EOPNOTSUPP) {
551                                         rc = 0;
552                                 } else {
553                                         CERROR("got acl size: %d\n", rc);
554                                 }
555                         } else {
556                                 repbody->aclsize = rc;
557                                 repbody->valid |= OBD_MD_FLACL;
558                                 rc = 0;
559                         }
560                 }
561         }
562 #endif
563
564         if ((reqbody->valid & OBD_MD_FLMDSCAPA) &&
565             info->mti_mdt->mdt_opts.mo_mds_capa) {
566                 struct lustre_capa *capa;
567
568                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
569                 LASSERT(capa);
570                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
571                 rc = mo_capa_get(env, next, capa, 0);
572                 if (rc)
573                         RETURN(rc);
574                 repbody->valid |= OBD_MD_FLMDSCAPA;
575         }
576         RETURN(rc);
577 }
578
579 static int mdt_renew_capa(struct mdt_thread_info *info)
580 {
581         struct mdt_device  *mdt = info->mti_mdt;
582         struct mdt_object  *obj = info->mti_object;
583         struct mdt_body    *body;
584         struct lustre_capa *capa, *c;
585         int rc;
586         ENTRY;
587
588         /* if object doesn't exist, or server has disabled capability,
589          * return directly, client will find body->valid OBD_MD_FLOSSCAPA
590          * flag not set.
591          */
592         if (!obj || !mdt->mdt_opts.mo_mds_capa)
593                 RETURN(0);
594
595         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
596         LASSERT(body != NULL);
597
598         c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
599         LASSERT(c);
600
601         capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
602         LASSERT(capa);
603
604         *capa = *c;
605         rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
606         if (rc == 0)
607                 body->valid |= OBD_MD_FLOSSCAPA;
608         RETURN(rc);
609 }
610
611 static int mdt_getattr(struct mdt_thread_info *info)
612 {
613         struct mdt_object       *obj = info->mti_object;
614         struct req_capsule      *pill = info->mti_pill;
615         struct mdt_body         *reqbody;
616         struct mdt_body         *repbody;
617         mode_t                   mode;
618         int                      md_size;
619         int rc;
620         ENTRY;
621
622         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
623         LASSERT(reqbody);
624
625         if (reqbody->valid & OBD_MD_FLOSSCAPA) {
626                 rc = req_capsule_server_pack(pill);
627                 if (unlikely(rc))
628                         RETURN(err_serious(rc));
629                 rc = mdt_renew_capa(info);
630                 GOTO(out_shrink, rc);
631         }
632
633         LASSERT(obj != NULL);
634         LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
635
636         mode = lu_object_attr(&obj->mot_obj.mo_lu);
637         if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
638             (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize))
639                 md_size = reqbody->eadatasize;
640         else
641                 md_size = info->mti_mdt->mdt_max_mdsize;
642
643         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
644
645         rc = req_capsule_server_pack(pill);
646         if (unlikely(rc != 0))
647                 RETURN(err_serious(rc));
648
649         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
650         LASSERT(repbody != NULL);
651         repbody->eadatasize = 0;
652         repbody->aclsize = 0;
653
654         if (reqbody->valid & OBD_MD_FLRMTPERM)
655                 rc = mdt_init_ucred(info, reqbody);
656         else
657                 rc = mdt_check_ucred(info);
658         if (unlikely(rc))
659                 GOTO(out_shrink, rc);
660
661         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
662         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
663
664         /*
665          * Don't check capability at all, because rename might getattr for
666          * remote obj, and at that time no capability is available.
667          */
668         mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
669         rc = mdt_getattr_internal(info, obj);
670         if (reqbody->valid & OBD_MD_FLRMTPERM)
671                 mdt_exit_ucred(info);
672         EXIT;
673 out_shrink:
674         mdt_shrink_reply(info);
675         return rc;
676 }
677
678 static int mdt_is_subdir(struct mdt_thread_info *info)
679 {
680         struct mdt_object     *o = info->mti_object;
681         struct req_capsule    *pill = info->mti_pill;
682         const struct mdt_body *body = info->mti_body;
683         struct mdt_body       *repbody;
684         int                    rc;
685         ENTRY;
686
687         LASSERT(o != NULL);
688
689         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
690
691         /*
692          * We save last checked parent fid to @repbody->fid1 for remote
693          * directory case.
694          */
695         LASSERT(fid_is_sane(&body->fid2));
696         LASSERT(mdt_object_exists(o) > 0);
697         rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
698                            &body->fid2, &repbody->fid1);
699         if (rc == 0 || rc == -EREMOTE)
700                 repbody->valid |= OBD_MD_FLID;
701
702         RETURN(rc);
703 }
704
705 static int mdt_raw_lookup(struct mdt_thread_info *info,
706                           struct mdt_object *parent,
707                           const struct lu_name *lname,
708                           struct ldlm_reply *ldlm_rep)
709 {
710         struct md_object *next = mdt_object_child(info->mti_object);
711         const struct mdt_body *reqbody = info->mti_body;
712         struct lu_fid *child_fid = &info->mti_tmp_fid1;
713         struct mdt_body *repbody;
714         int rc;
715         ENTRY;
716
717         if (reqbody->valid != OBD_MD_FLID)
718                 RETURN(0);
719
720         LASSERT(!info->mti_cross_ref);
721
722         /* Only got the fid of this obj by name */
723         rc = mdo_lookup(info->mti_env, next, lname, child_fid,
724                         &info->mti_spec);
725 #if 0
726         /* XXX is raw_lookup possible as intent operation? */
727         if (rc != 0) {
728                 if (rc == -ENOENT)
729                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
730                 RETURN(rc);
731         } else
732                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
733
734         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
735 #endif
736         if (rc == 0) {
737                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
738                 repbody->fid1 = *child_fid;
739                 repbody->valid = OBD_MD_FLID;
740         }
741         RETURN(1);
742 }
743
744 /*
745  * UPDATE lock should be taken against parent, and be release before exit;
746  * child_bits lock should be taken against child, and be returned back:
747  *            (1)normal request should release the child lock;
748  *            (2)intent request will grant the lock to client.
749  */
750 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
751                                  struct mdt_lock_handle *lhc,
752                                  __u64 child_bits,
753                                  struct ldlm_reply *ldlm_rep)
754 {
755         struct ptlrpc_request  *req       = mdt_info_req(info);
756         struct mdt_body        *reqbody   = NULL;
757         struct mdt_object      *parent    = info->mti_object;
758         struct mdt_object      *child;
759         struct md_object       *next      = mdt_object_child(parent);
760         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
761         struct lu_name         *lname     = NULL;
762         const char             *name;
763         int                     namelen   = 0;
764         struct mdt_lock_handle *lhp;
765         struct ldlm_lock       *lock;
766         struct ldlm_res_id     *res_id;
767         int                     is_resent;
768         int                     rc;
769
770         ENTRY;
771
772         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
773         LASSERT(ergo(is_resent,
774                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
775
776         LASSERT(parent != NULL);
777         name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
778         if (name == NULL)
779                 RETURN(err_serious(-EFAULT));
780
781         namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
782                                        RCL_CLIENT) - 1;
783         LASSERT(namelen >= 0);
784
785         /* XXX: "namelen == 0" is for getattr by fid (OBD_CONNECT_ATTRFID),
786          * otherwise do not allow empty name, that is the name must contain
787          * at least one character and the terminating '\0'*/
788         if (namelen == 0) {
789                 reqbody =req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
790                 LASSERT(fid_is_sane(&reqbody->fid2));
791                 name = NULL;
792
793                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
794                        "ldlm_rep = %p\n",
795                        PFID(mdt_object_fid(parent)), PFID(&reqbody->fid2),
796                        ldlm_rep);
797         } else {
798                 lname = mdt_name(info->mti_env, (char *)name, namelen);
799                 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
800                        "ldlm_rep = %p\n",
801                        PFID(mdt_object_fid(parent)), name, ldlm_rep);
802         }
803
804         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
805
806         rc = mdt_object_exists(parent);
807         if (unlikely(rc == 0)) {
808                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
809                                 &parent->mot_obj.mo_lu,
810                                 "Parent doesn't exist!\n");
811                 RETURN(-ESTALE);
812         } else
813                 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
814                          PFID(mdt_object_fid(parent)));
815
816         if (lname) {
817                 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
818                 if (rc != 0) {
819                         if (rc > 0)
820                                 rc = 0;
821                         RETURN(rc);
822                 }
823         }
824
825         if (info->mti_cross_ref) {
826                 /* Only getattr on the child. Parent is on another node. */
827                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
828                 child = parent;
829                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
830                        "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
831
832                 if (is_resent) {
833                         /* Do not take lock for resent case. */
834                         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
835                         LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
836                                  lhc->mlh_reg_lh.cookie);
837                         LASSERT(fid_res_name_eq(mdt_object_fid(child),
838                                                 &lock->l_resource->lr_name));
839                         LDLM_LOCK_PUT(lock);
840                         rc = 0;
841                 } else {
842                         mdt_lock_handle_init(lhc);
843                         mdt_lock_reg_init(lhc, LCK_PR);
844
845                         /*
846                          * Object's name is on another MDS, no lookup lock is
847                          * needed here but update is.
848                          */
849                         child_bits &= ~MDS_INODELOCK_LOOKUP;
850                         child_bits |= MDS_INODELOCK_UPDATE;
851
852                         rc = mdt_object_lock(info, child, lhc, child_bits,
853                                              MDT_LOCAL_LOCK);
854                 }
855                 if (rc == 0) {
856                         /* Finally, we can get attr for child. */
857                         mdt_set_capainfo(info, 0, mdt_object_fid(child),
858                                          BYPASS_CAPA);
859                         rc = mdt_getattr_internal(info, child);
860                         if (unlikely(rc != 0))
861                                 mdt_object_unlock(info, child, lhc, 1);
862                 }
863                 RETURN(rc);
864         }
865
866         /* step 1: lock parent */
867         lhp = &info->mti_lh[MDT_LH_PARENT];
868         mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
869         rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
870                              MDT_LOCAL_LOCK);
871
872         if (unlikely(rc != 0))
873                 RETURN(rc);
874
875         if (lname) {
876                 /* step 2: lookup child's fid by name */
877                 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
878                                 &info->mti_spec);
879
880                 if (rc != 0) {
881                         if (rc == -ENOENT)
882                                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
883                         GOTO(out_parent, rc);
884                 } else
885                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
886         } else {
887                 *child_fid = reqbody->fid2;
888                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
889         }
890
891         /*
892          *step 3: find the child object by fid & lock it.
893          *        regardless if it is local or remote.
894          */
895         child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
896
897         if (unlikely(IS_ERR(child)))
898                 GOTO(out_parent, rc = PTR_ERR(child));
899         if (is_resent) {
900                 /* Do not take lock for resent case. */
901                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
902                 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
903                          lhc->mlh_reg_lh.cookie);
904
905                 res_id = &lock->l_resource->lr_name;
906                 if (!fid_res_name_eq(mdt_object_fid(child),
907                                     &lock->l_resource->lr_name)) {
908                          LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
909                                                  &lock->l_resource->lr_name),
910                                  "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
911                                  (unsigned long)res_id->name[0],
912                                  (unsigned long)res_id->name[1],
913                                  (unsigned long)res_id->name[2],
914                                  PFID(mdt_object_fid(parent)));
915                           CWARN("Although resent, but still not get child lock"
916                                 "parent:"DFID" child:"DFID"\n",
917                                 PFID(mdt_object_fid(parent)),
918                                 PFID(mdt_object_fid(child)));
919                           lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
920                           LDLM_LOCK_PUT(lock);
921                           GOTO(relock, 0);
922                 }
923                 LDLM_LOCK_PUT(lock);
924                 rc = 0;
925         } else {
926                 struct md_attr *ma = &info->mti_attr;
927 relock:
928                 mdt_lock_handle_init(lhc);
929                 mdt_lock_reg_init(lhc, LCK_PR);
930
931                 if (mdt_object_exists(child) == 0) {
932                         LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
933                                         &child->mot_obj.mo_lu,
934                                         "Object doesn't exist!\n");
935                 }
936
937                 ma->ma_valid = 0;
938                 ma->ma_need = MA_INODE;
939                 rc = mo_attr_get(info->mti_env, next, ma);
940                 if (unlikely(rc != 0))
941                         GOTO(out_child, rc);
942
943                 /* If the file has not been changed for some time, we return
944                  * not only a LOOKUP lock, but also an UPDATE lock and this
945                  * might save us RPC on later STAT. For directories, it also
946                  * let negative dentry starts working for this dir. */
947                 if (ma->ma_valid & MA_INODE &&
948                     ma->ma_attr.la_valid & LA_CTIME &&
949                     info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
950                     ma->ma_attr.la_ctime < cfs_time_current_sec())
951                         child_bits |= MDS_INODELOCK_UPDATE;
952
953                 rc = mdt_object_lock(info, child, lhc, child_bits,
954                                      MDT_CROSS_LOCK);
955
956                 if (unlikely(rc != 0))
957                         GOTO(out_child, rc);
958         }
959
960         /* finally, we can get attr for child. */
961         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
962         rc = mdt_getattr_internal(info, child);
963         if (unlikely(rc != 0)) {
964                 mdt_object_unlock(info, child, lhc, 1);
965         } else {
966                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
967                 if (lock) {
968                         struct mdt_body *repbody;
969                         struct lu_attr *ma;
970
971                         /* Debugging code. */
972                         res_id = &lock->l_resource->lr_name;
973                         LDLM_DEBUG(lock, "Returning lock to client\n");
974                         LASSERTF(fid_res_name_eq(mdt_object_fid(child),
975                                                  &lock->l_resource->lr_name),
976                                  "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
977                                  (unsigned long)res_id->name[0],
978                                  (unsigned long)res_id->name[1],
979                                  (unsigned long)res_id->name[2],
980                                  PFID(mdt_object_fid(child)));
981                         /*
982                          * Pack Size-on-MDS inode attributes to the body if
983                          * update lock is given.
984                          */
985                         repbody = req_capsule_server_get(info->mti_pill,
986                                                          &RMF_MDT_BODY);
987                         ma = &info->mti_attr.ma_attr;
988                         if (lock->l_policy_data.l_inodebits.bits &
989                             MDS_INODELOCK_UPDATE)
990                                 mdt_pack_size2body(info, child);
991                         LDLM_LOCK_PUT(lock);
992                 }
993         }
994         EXIT;
995 out_child:
996         mdt_object_put(info->mti_env, child);
997 out_parent:
998         mdt_object_unlock(info, parent, lhp, 1);
999         return rc;
1000 }
1001
1002 /* normal handler: should release the child lock */
1003 static int mdt_getattr_name(struct mdt_thread_info *info)
1004 {
1005         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1006         struct mdt_body        *reqbody;
1007         struct mdt_body        *repbody;
1008         int rc;
1009         ENTRY;
1010
1011         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1012         LASSERT(reqbody != NULL);
1013         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1014         LASSERT(repbody != NULL);
1015
1016         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1017         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1018         repbody->eadatasize = 0;
1019         repbody->aclsize = 0;
1020
1021         rc = mdt_init_ucred(info, reqbody);
1022         if (unlikely(rc))
1023                 GOTO(out_shrink, rc);
1024
1025         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1026         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1027                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1028                 lhc->mlh_reg_lh.cookie = 0;
1029         }
1030         mdt_exit_ucred(info);
1031         EXIT;
1032 out_shrink:
1033         mdt_shrink_reply(info);
1034         return rc;
1035 }
1036
1037 static struct lu_device_operations mdt_lu_ops;
1038
1039 static int lu_device_is_mdt(struct lu_device *d)
1040 {
1041         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1042 }
1043
1044 static int mdt_set_info(struct mdt_thread_info *info)
1045 {
1046         struct ptlrpc_request *req = mdt_info_req(info);
1047         char *key;
1048         __u32 *val;
1049         int keylen, rc = 0;
1050         ENTRY;
1051
1052         rc = req_capsule_server_pack(info->mti_pill);
1053         if (rc)
1054                 RETURN(rc);
1055
1056         key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1057         if (key == NULL) {
1058                 DEBUG_REQ(D_HA, req, "no set_info key");
1059                 RETURN(-EFAULT);
1060         }
1061
1062         keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1063                                       RCL_CLIENT);
1064
1065         val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1066         if (val == NULL) {
1067                 DEBUG_REQ(D_HA, req, "no set_info val");
1068                 RETURN(-EFAULT);
1069         }
1070
1071         if (keylen != (sizeof(KEY_READ_ONLY) - 1) ||
1072             memcmp(key, KEY_READ_ONLY, keylen) != 0)
1073                 RETURN(-EINVAL);
1074
1075         req->rq_status = 0;
1076         lustre_msg_set_status(req->rq_repmsg, 0);
1077
1078         spin_lock(&req->rq_export->exp_lock);
1079         if (*val)
1080                 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1081         else
1082                 req->rq_export->exp_connect_flags &= ~OBD_CONNECT_RDONLY;
1083         spin_unlock(&req->rq_export->exp_lock);
1084
1085         RETURN(0);
1086 }
1087
1088 static int mdt_connect(struct mdt_thread_info *info)
1089 {
1090         int rc;
1091         struct ptlrpc_request *req;
1092
1093         req = mdt_info_req(info);
1094         rc = target_handle_connect(req);
1095         if (rc == 0) {
1096                 LASSERT(req->rq_export != NULL);
1097                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1098                 rc = mdt_init_idmap(info);
1099                 if (rc != 0)
1100                         /* if mdt_init_idmap failed, revocation for connect */
1101                         obd_disconnect(class_export_get(req->rq_export));
1102         } else
1103                 rc = err_serious(rc);
1104         return rc;
1105 }
1106
1107 static int mdt_disconnect(struct mdt_thread_info *info)
1108 {
1109         int rc;
1110         ENTRY;
1111
1112         rc = target_handle_disconnect(mdt_info_req(info));
1113         if (rc)
1114                 rc = err_serious(rc);
1115         RETURN(rc);
1116 }
1117
1118 static int mdt_sendpage(struct mdt_thread_info *info,
1119                         struct lu_rdpg *rdpg)
1120 {
1121         struct ptlrpc_request   *req = mdt_info_req(info);
1122         struct ptlrpc_bulk_desc *desc;
1123         struct l_wait_info      *lwi = &info->mti_u.rdpg.mti_wait_info;
1124         int                      tmpcount;
1125         int                      tmpsize;
1126         int                      i;
1127         int                      rc;
1128         ENTRY;
1129
1130         desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1131                                     MDS_BULK_PORTAL);
1132         if (desc == NULL)
1133                 RETURN(-ENOMEM);
1134
1135         for (i = 0, tmpcount = rdpg->rp_count;
1136                 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
1137                 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1138                 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1139         }
1140
1141         LASSERT(desc->bd_nob == rdpg->rp_count);
1142         rc = ptlrpc_start_bulk_transfer(desc);
1143         if (rc)
1144                 GOTO(free_desc, rc);
1145
1146         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1147                 GOTO(abort_bulk, rc);
1148
1149         *lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
1150         rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), lwi);
1151         LASSERT (rc == 0 || rc == -ETIMEDOUT);
1152
1153         if (rc == 0) {
1154                 if (desc->bd_success &&
1155                     desc->bd_nob_transferred == rdpg->rp_count)
1156                         GOTO(free_desc, rc);
1157
1158                 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
1159         }
1160
1161         DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s",
1162                   (rc == -ETIMEDOUT) ? "timeout" : "network error",
1163                   desc->bd_nob_transferred, rdpg->rp_count,
1164                   req->rq_export->exp_client_uuid.uuid,
1165                   req->rq_export->exp_connection->c_remote_uuid.uuid);
1166
1167         class_fail_export(req->rq_export);
1168
1169         EXIT;
1170 abort_bulk:
1171         ptlrpc_abort_bulk(desc);
1172 free_desc:
1173         ptlrpc_free_bulk(desc);
1174         return rc;
1175 }
1176
1177 #ifdef HAVE_SPLIT_SUPPORT
1178 /*
1179  * Retrieve dir entry from the page and insert it to the slave object, actually,
1180  * this should be in osd layer, but since it will not in the final product, so
1181  * just do it here and do not define more moo api anymore for this.
1182  */
1183 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1184                               int size)
1185 {
1186         struct mdt_object *object = info->mti_object;
1187         struct lu_fid *lf = &info->mti_tmp_fid2;
1188         struct md_attr *ma = &info->mti_attr;
1189         struct lu_dirpage *dp;
1190         struct lu_dirent *ent;
1191         int rc = 0, offset = 0;
1192         ENTRY;
1193
1194         /* Make sure we have at least one entry. */
1195         if (size == 0)
1196                 RETURN(-EINVAL);
1197
1198         /*
1199          * Disable trans for this name insert, since it will include many trans
1200          * for this.
1201          */
1202         info->mti_no_need_trans = 1;
1203         /*
1204          * When write_dir_page, no need update parent's ctime,
1205          * and no permission check for name_insert.
1206          */
1207         ma->ma_attr.la_ctime = 0;
1208         ma->ma_attr.la_valid = LA_MODE;
1209         ma->ma_valid = MA_INODE;
1210
1211         kmap(page);
1212         dp = page_address(page);
1213         offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1214
1215         for (ent = lu_dirent_start(dp); ent != NULL;
1216              ent = lu_dirent_next(ent)) {
1217                 struct lu_name *lname;
1218                 char *name;
1219
1220                 if (le16_to_cpu(ent->lde_namelen) == 0)
1221                         continue;
1222
1223                 fid_le_to_cpu(lf, &ent->lde_fid);
1224                 if (le32_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1225                         ma->ma_attr.la_mode = S_IFDIR;
1226                 else
1227                         ma->ma_attr.la_mode = 0;
1228                 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1229                 if (name == NULL)
1230                         GOTO(out, rc = -ENOMEM);
1231
1232                 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1233                 lname = mdt_name(info->mti_env, name,
1234                                  le16_to_cpu(ent->lde_namelen) + 1);
1235                 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1236                 rc = mdo_name_insert(info->mti_env,
1237                                      md_object_next(&object->mot_obj),
1238                                      lname, lf, ma);
1239                 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1240                 if (rc) {
1241                         CERROR("Can't insert %*.*s, rc %d\n",
1242                                le16_to_cpu(ent->lde_namelen),
1243                                le16_to_cpu(ent->lde_namelen),
1244                                ent->lde_name, rc);
1245                         GOTO(out, rc);
1246                 }
1247
1248                 offset += lu_dirent_size(ent);
1249                 if (offset >= size)
1250                         break;
1251         }
1252         EXIT;
1253 out:
1254         kunmap(page);
1255         return rc;
1256 }
1257
1258 static int mdt_bulk_timeout(void *data)
1259 {
1260         ENTRY;
1261
1262         CERROR("mdt bulk transfer timeout \n");
1263
1264         RETURN(1);
1265 }
1266
1267 static int mdt_writepage(struct mdt_thread_info *info)
1268 {
1269         struct ptlrpc_request   *req = mdt_info_req(info);
1270         struct mdt_body         *reqbody;
1271         struct l_wait_info      *lwi;
1272         struct ptlrpc_bulk_desc *desc;
1273         struct page             *page;
1274         int                rc;
1275         ENTRY;
1276
1277
1278         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1279         if (reqbody == NULL)
1280                 RETURN(err_serious(-EFAULT));
1281
1282         desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1283         if (desc == NULL)
1284                 RETURN(err_serious(-ENOMEM));
1285
1286         /* allocate the page for the desc */
1287         page = cfs_alloc_page(CFS_ALLOC_STD);
1288         if (page == NULL)
1289                 GOTO(desc_cleanup, rc = -ENOMEM);
1290
1291         CDEBUG(D_INFO, "Received page offset %d size %d \n",
1292                (int)reqbody->size, (int)reqbody->nlink);
1293
1294         ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1295                               (int)reqbody->nlink);
1296
1297         /*
1298          * Check if client was evicted while we were doing i/o before touching
1299          * network.
1300          */
1301         OBD_ALLOC_PTR(lwi);
1302         if (!lwi)
1303                 GOTO(cleanup_page, rc = -ENOMEM);
1304
1305         if (desc->bd_export->exp_failed)
1306                 rc = -ENOTCONN;
1307         else
1308                 rc = ptlrpc_start_bulk_transfer (desc);
1309         if (rc == 0) {
1310                 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1311                                             mdt_bulk_timeout, desc);
1312                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1313                                   desc->bd_export->exp_failed, lwi);
1314                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1315                 if (rc == -ETIMEDOUT) {
1316                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1317                         ptlrpc_abort_bulk(desc);
1318                 } else if (desc->bd_export->exp_failed) {
1319                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1320                         rc = -ENOTCONN;
1321                         ptlrpc_abort_bulk(desc);
1322                 } else if (!desc->bd_success ||
1323                            desc->bd_nob_transferred != desc->bd_nob) {
1324                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1325                                   desc->bd_success ?
1326                                   "truncated" : "network error on",
1327                                   desc->bd_nob_transferred, desc->bd_nob);
1328                         /* XXX should this be a different errno? */
1329                         rc = -ETIMEDOUT;
1330                 }
1331         } else {
1332                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1333         }
1334         if (rc)
1335                 GOTO(cleanup_lwi, rc);
1336         rc = mdt_write_dir_page(info, page, reqbody->nlink);
1337
1338 cleanup_lwi:
1339         OBD_FREE_PTR(lwi);
1340 cleanup_page:
1341         cfs_free_page(page);
1342 desc_cleanup:
1343         ptlrpc_free_bulk(desc);
1344         RETURN(rc);
1345 }
1346 #endif
1347
1348 static int mdt_readpage(struct mdt_thread_info *info)
1349 {
1350         struct mdt_object *object = info->mti_object;
1351         struct lu_rdpg    *rdpg = &info->mti_u.rdpg.mti_rdpg;
1352         struct mdt_body   *reqbody;
1353         struct mdt_body   *repbody;
1354         int                rc;
1355         int                i;
1356         ENTRY;
1357
1358         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1359                 RETURN(err_serious(-ENOMEM));
1360
1361         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1362         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1363         if (reqbody == NULL || repbody == NULL)
1364                 RETURN(err_serious(-EFAULT));
1365
1366         /*
1367          * prepare @rdpg before calling lower layers and transfer itself. Here
1368          * reqbody->size contains offset of where to start to read and
1369          * reqbody->nlink contains number bytes to read.
1370          */
1371         rdpg->rp_hash = reqbody->size;
1372         if ((__u64)rdpg->rp_hash != reqbody->size) {
1373                 CERROR("Invalid hash: %#llx != %#llx\n",
1374                        (__u64)rdpg->rp_hash, reqbody->size);
1375                 RETURN(-EFAULT);
1376         }
1377         rdpg->rp_count  = reqbody->nlink;
1378         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1379         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1380         if (rdpg->rp_pages == NULL)
1381                 RETURN(-ENOMEM);
1382
1383         for (i = 0; i < rdpg->rp_npages; ++i) {
1384                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1385                 if (rdpg->rp_pages[i] == NULL)
1386                         GOTO(free_rdpg, rc = -ENOMEM);
1387         }
1388
1389         /* call lower layers to fill allocated pages with directory data */
1390         rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1391         if (rc)
1392                 GOTO(free_rdpg, rc);
1393
1394         /* send pages to client */
1395         rc = mdt_sendpage(info, rdpg);
1396
1397         EXIT;
1398 free_rdpg:
1399
1400         for (i = 0; i < rdpg->rp_npages; i++)
1401                 if (rdpg->rp_pages[i] != NULL)
1402                         cfs_free_page(rdpg->rp_pages[i]);
1403         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1404
1405         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1406                 RETURN(0);
1407
1408         return rc;
1409 }
1410
1411 static int mdt_reint_internal(struct mdt_thread_info *info,
1412                               struct mdt_lock_handle *lhc,
1413                               __u32 op)
1414 {
1415         struct req_capsule      *pill = info->mti_pill;
1416         struct mdt_device       *mdt = info->mti_mdt;
1417         struct mdt_body         *repbody;
1418         int                      rc = 0;
1419         ENTRY;
1420
1421         /* pack reply */
1422         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1423                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1424                                      mdt->mdt_max_mdsize);
1425         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1426                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1427                                      mdt->mdt_max_cookiesize);
1428
1429         rc = req_capsule_server_pack(pill);
1430         if (rc != 0) {
1431                 CERROR("Can't pack response, rc %d\n", rc);
1432                 RETURN(err_serious(rc));
1433         }
1434
1435         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1436                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1437                 LASSERT(repbody);
1438                 repbody->eadatasize = 0;
1439                 repbody->aclsize = 0;
1440         }
1441
1442         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1443                 GOTO(out_shrink, rc = err_serious(-EFAULT));
1444
1445         rc = mdt_reint_unpack(info, op);
1446         if (rc != 0) {
1447                 CERROR("Can't unpack reint, rc %d\n", rc);
1448                 GOTO(out_shrink, rc = err_serious(rc));
1449         }
1450
1451         rc = mdt_init_ucred_reint(info);
1452         if (rc)
1453                 GOTO(out_shrink, rc);
1454
1455         rc = mdt_fix_attr_ucred(info, op);
1456         if (rc != 0)
1457                 GOTO(out_ucred, rc = err_serious(rc));
1458
1459         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1460                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1461                 GOTO(out_ucred, rc);
1462         }
1463         rc = mdt_reint_rec(info, lhc);
1464         EXIT;
1465 out_ucred:
1466         mdt_exit_ucred(info);
1467 out_shrink:
1468         mdt_shrink_reply(info);
1469         return rc;
1470 }
1471
1472 static long mdt_reint_opcode(struct mdt_thread_info *info,
1473                              const struct req_format **fmt)
1474 {
1475         struct mdt_rec_reint *rec;
1476         long opc;
1477
1478         opc = err_serious(-EFAULT);
1479         rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1480         if (rec != NULL) {
1481                 opc = rec->rr_opcode;
1482                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1483                 if (opc < REINT_MAX && fmt[opc] != NULL)
1484                         req_capsule_extend(info->mti_pill, fmt[opc]);
1485                 else {
1486                         CERROR("Unsupported opc: %ld\n", opc);
1487                         opc = err_serious(opc);
1488                 }
1489         }
1490         return opc;
1491 }
1492
1493 static int mdt_reint(struct mdt_thread_info *info)
1494 {
1495         long opc;
1496         int  rc;
1497
1498         static const struct req_format *reint_fmts[REINT_MAX] = {
1499                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
1500                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
1501                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
1502                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
1503                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
1504                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
1505                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1506         };
1507
1508         ENTRY;
1509
1510         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_REINT_NET,
1511                                  OBD_FAIL_MDS_REINT_NET)) {
1512                 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET;
1513                 RETURN(0);
1514         }
1515
1516         opc = mdt_reint_opcode(info, reint_fmts);
1517         if (opc >= 0) {
1518                 /*
1519                  * No lock possible here from client to pass it to reint code
1520                  * path.
1521                  */
1522                 rc = mdt_reint_internal(info, NULL, opc);
1523         } else {
1524                 rc = opc;
1525         }
1526
1527         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1528         RETURN(rc);
1529 }
1530
1531 /* TODO these two methods not available now. */
1532
1533 /* this should sync the whole device */
1534 static int mdt_device_sync(struct mdt_thread_info *info)
1535 {
1536         return 0;
1537 }
1538
1539 /* this should sync this object */
1540 static int mdt_object_sync(struct mdt_thread_info *info)
1541 {
1542         return 0;
1543 }
1544
1545 static int mdt_sync(struct mdt_thread_info *info)
1546 {
1547         struct req_capsule *pill = info->mti_pill;
1548         struct mdt_body *body;
1549         int rc;
1550         ENTRY;
1551
1552         /* The fid may be zero, so we req_capsule_set manually */
1553         req_capsule_set(pill, &RQF_MDS_SYNC);
1554
1555         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1556         if (body == NULL)
1557                 RETURN(err_serious(-EINVAL));
1558
1559         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1560                 RETURN(err_serious(-ENOMEM));
1561
1562         if (fid_seq(&body->fid1) == 0) {
1563                 /* sync the whole device */
1564                 rc = req_capsule_server_pack(pill);
1565                 if (rc == 0)
1566                         rc = mdt_device_sync(info);
1567                 else
1568                         rc = err_serious(rc);
1569         } else {
1570                 /* sync an object */
1571                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1572                 if (rc == 0) {
1573                         rc = mdt_object_sync(info);
1574                         if (rc == 0) {
1575                                 struct md_object *next;
1576                                 const struct lu_fid *fid;
1577                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1578
1579                                 next = mdt_object_child(info->mti_object);
1580                                 info->mti_attr.ma_need = MA_INODE;
1581                                 info->mti_attr.ma_valid = 0;
1582                                 rc = mo_attr_get(info->mti_env, next,
1583                                                  &info->mti_attr);
1584                                 if (rc == 0) {
1585                                         body = req_capsule_server_get(pill,
1586                                                                 &RMF_MDT_BODY);
1587                                         fid = mdt_object_fid(info->mti_object);
1588                                         mdt_pack_attr2body(info, body, la, fid);
1589                                 }
1590                         }
1591                 } else
1592                         rc = err_serious(rc);
1593         }
1594         RETURN(rc);
1595 }
1596
1597 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1598 {
1599         return err_serious(-EOPNOTSUPP);
1600 }
1601
1602 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1603 {
1604         return err_serious(-EOPNOTSUPP);
1605 }
1606
1607 /*
1608  * OBD PING and other handlers.
1609  */
1610 static int mdt_obd_ping(struct mdt_thread_info *info)
1611 {
1612         int rc;
1613         ENTRY;
1614
1615         req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1616
1617         rc = target_handle_ping(mdt_info_req(info));
1618         if (rc < 0)
1619                 rc = err_serious(rc);
1620         RETURN(rc);
1621 }
1622
1623 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1624 {
1625         return err_serious(-EOPNOTSUPP);
1626 }
1627
1628 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1629 {
1630         return err_serious(-EOPNOTSUPP);
1631 }
1632
1633
1634 /*
1635  * DLM handlers.
1636  */
1637 static struct ldlm_callback_suite cbs = {
1638         .lcs_completion = ldlm_server_completion_ast,
1639         .lcs_blocking   = ldlm_server_blocking_ast,
1640         .lcs_glimpse    = NULL
1641 };
1642
1643 static int mdt_enqueue(struct mdt_thread_info *info)
1644 {
1645         struct ptlrpc_request *req;
1646         __u64 req_bits;
1647         int rc;
1648
1649         /*
1650          * info->mti_dlm_req already contains swapped and (if necessary)
1651          * converted dlm request.
1652          */
1653         LASSERT(info->mti_dlm_req != NULL);
1654
1655         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE)) {
1656                 info->mti_fail_id = OBD_FAIL_LDLM_ENQUEUE;
1657                 return 0;
1658         }
1659
1660         req = mdt_info_req(info);
1661
1662         /*
1663          * Lock without inodebits makes no sense and will oops later in
1664          * ldlm. Let's check it now to see if we have wrong lock from client or
1665          * bits get corrupted somewhere in mdt_intent_policy().
1666          */
1667         req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1668         /* This is disabled because we need to support liblustre flock.
1669          * LASSERT(req_bits != 0);
1670          */
1671
1672         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1673                                   req, info->mti_dlm_req, &cbs);
1674         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1675         return rc ? err_serious(rc) : req->rq_status;
1676 }
1677
1678 static int mdt_convert(struct mdt_thread_info *info)
1679 {
1680         int rc;
1681         struct ptlrpc_request *req;
1682
1683         LASSERT(info->mti_dlm_req);
1684         req = mdt_info_req(info);
1685         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1686         return rc ? err_serious(rc) : req->rq_status;
1687 }
1688
1689 static int mdt_bl_callback(struct mdt_thread_info *info)
1690 {
1691         CERROR("bl callbacks should not happen on MDS\n");
1692         LBUG();
1693         return err_serious(-EOPNOTSUPP);
1694 }
1695
1696 static int mdt_cp_callback(struct mdt_thread_info *info)
1697 {
1698         CERROR("cp callbacks should not happen on MDS\n");
1699         LBUG();
1700         return err_serious(-EOPNOTSUPP);
1701 }
1702
1703 /*
1704  * sec context handlers
1705  */
1706 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
1707 {
1708         int rc;
1709
1710         rc = mdt_handle_idmap(info);
1711
1712         if (unlikely(rc)) {
1713                 struct ptlrpc_request *req = mdt_info_req(info);
1714                 __u32                  opc;
1715
1716                 opc = lustre_msg_get_opc(req->rq_reqmsg);
1717                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
1718                         sptlrpc_svc_ctx_invalidate(req);
1719         }
1720
1721         return rc;
1722 }
1723
1724 static struct mdt_object *mdt_obj(struct lu_object *o)
1725 {
1726         LASSERT(lu_device_is_mdt(o->lo_dev));
1727         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
1728 }
1729
1730 struct mdt_object *mdt_object_find(const struct lu_env *env,
1731                                    struct mdt_device *d,
1732                                    const struct lu_fid *f)
1733 {
1734         struct lu_object *o;
1735         struct mdt_object *m;
1736         ENTRY;
1737
1738         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
1739         o = lu_object_find(env, d->mdt_md_dev.md_lu_dev.ld_site, f);
1740         if (unlikely(IS_ERR(o)))
1741                 m = (struct mdt_object *)o;
1742         else
1743                 m = mdt_obj(o);
1744         RETURN(m);
1745 }
1746
1747 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
1748                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
1749 {
1750         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1751         ldlm_policy_data_t *policy = &info->mti_policy;
1752         struct ldlm_res_id *res_id = &info->mti_res_id;
1753         int rc;
1754         ENTRY;
1755
1756         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1757         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1758         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
1759         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
1760
1761         if (mdt_object_exists(o) < 0) {
1762                 if (locality == MDT_CROSS_LOCK) {
1763                         /* cross-ref object fix */
1764                         ibits &= ~MDS_INODELOCK_UPDATE;
1765                         ibits |= MDS_INODELOCK_LOOKUP;
1766                 } else {
1767                         LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
1768                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
1769                 }
1770                 /* No PDO lock on remote object */
1771                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
1772         }
1773
1774         memset(policy, 0, sizeof(*policy));
1775         fid_build_reg_res_name(mdt_object_fid(o), res_id);
1776
1777         /*
1778          * Take PDO lock on whole directory and build correct @res_id for lock
1779          * on part of directory.
1780          */
1781         if (lh->mlh_pdo_hash != 0) {
1782                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
1783                 mdt_lock_pdo_mode(info, o, lh);
1784                 if (lh->mlh_pdo_mode != LCK_NL) {
1785                         /*
1786                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
1787                          * is never going to be sent to client and we do not
1788                          * want it slowed down due to possible cancels.
1789                          */
1790                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1791                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
1792                                           policy, res_id, LDLM_FL_ATOMIC_CB);
1793                         if (unlikely(rc))
1794                                 RETURN(rc);
1795                 }
1796
1797                 /*
1798                  * Finish res_id initializing by name hash marking patr of
1799                  * directory which is taking modification.
1800                  */
1801                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
1802         }
1803
1804         policy->l_inodebits.bits = ibits;
1805
1806         /*
1807          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1808          * going to be sent to client. If it is - mdt_intent_policy() path will
1809          * fix it up and turns FL_LOCAL flag off.
1810          */
1811         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1812                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB);
1813
1814         if (rc)
1815                 GOTO(out, rc);
1816
1817         if (lh->mlh_type == MDT_PDO_LOCK) {
1818                 /* check for exists after object is locked */
1819                 if (mdt_object_exists(o) == 0) {
1820                         /* Non-existent object shouldn't have PDO lock */
1821                         rc = -ESTALE;
1822                 } else {
1823                         /* Non-dir object shouldn't have PDO lock */
1824                         LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
1825                 }
1826         }
1827 out:
1828         if (rc)
1829                 mdt_object_unlock(info, o, lh, 1);
1830
1831
1832         RETURN(rc);
1833 }
1834
1835 /*
1836  * Just call ldlm_lock_decref() if decref, else we only call ptlrpc_save_lock()
1837  * to save this lock in req.  when transaction committed, req will be released,
1838  * and lock will, too.
1839  */
1840 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
1841                        struct mdt_lock_handle *lh, int decref)
1842 {
1843         struct ptlrpc_request *req = mdt_info_req(info);
1844         ENTRY;
1845
1846         if (lustre_handle_is_used(&lh->mlh_pdo_lh)) {
1847                 /* Do not save PDO locks to request, just decref. */
1848                 mdt_fid_unlock(&lh->mlh_pdo_lh,
1849                                lh->mlh_pdo_mode);
1850                 lh->mlh_pdo_lh.cookie = 0ull;
1851         }
1852
1853         if (lustre_handle_is_used(&lh->mlh_reg_lh)) {
1854                 if (decref) {
1855                         mdt_fid_unlock(&lh->mlh_reg_lh,
1856                                        lh->mlh_reg_mode);
1857                 } else {
1858                         ptlrpc_save_lock(req, &lh->mlh_reg_lh,
1859                                          lh->mlh_reg_mode);
1860                 }
1861                 lh->mlh_reg_lh.cookie = 0ull;
1862         }
1863
1864         EXIT;
1865 }
1866
1867 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
1868                                         const struct lu_fid *f,
1869                                         struct mdt_lock_handle *lh,
1870                                         __u64 ibits)
1871 {
1872         struct mdt_object *o;
1873
1874         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
1875         if (!IS_ERR(o)) {
1876                 int rc;
1877
1878                 rc = mdt_object_lock(info, o, lh, ibits,
1879                                      MDT_LOCAL_LOCK);
1880                 if (rc != 0) {
1881                         mdt_object_put(info->mti_env, o);
1882                         o = ERR_PTR(rc);
1883                 }
1884         }
1885         return o;
1886 }
1887
1888 void mdt_object_unlock_put(struct mdt_thread_info * info,
1889                            struct mdt_object * o,
1890                            struct mdt_lock_handle *lh,
1891                            int decref)
1892 {
1893         mdt_object_unlock(info, o, lh, decref);
1894         mdt_object_put(info->mti_env, o);
1895 }
1896
1897 static struct mdt_handler *mdt_handler_find(__u32 opc,
1898                                             struct mdt_opc_slice *supported)
1899 {
1900         struct mdt_opc_slice *s;
1901         struct mdt_handler   *h;
1902
1903         h = NULL;
1904         for (s = supported; s->mos_hs != NULL; s++) {
1905                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
1906                         h = s->mos_hs + (opc - s->mos_opc_start);
1907                         if (likely(h->mh_opc != 0))
1908                                 LASSERT(h->mh_opc == opc);
1909                         else
1910                                 h = NULL; /* unsupported opc */
1911                         break;
1912                 }
1913         }
1914         return h;
1915 }
1916
1917 static int mdt_lock_resname_compat(struct mdt_device *m,
1918                                    struct ldlm_request *req)
1919 {
1920         /* XXX something... later. */
1921         return 0;
1922 }
1923
1924 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
1925 {
1926         /* XXX something... later. */
1927         return 0;
1928 }
1929
1930 /*
1931  * Generic code handling requests that have struct mdt_body passed in:
1932  *
1933  *  - extract mdt_body from request and save it in @info, if present;
1934  *
1935  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
1936  *  @info;
1937  *
1938  *  - if HABEO_CORPUS flag is set for this request type check whether object
1939  *  actually exists on storage (lu_object_exists()).
1940  *
1941  */
1942 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
1943 {
1944         const struct mdt_body    *body;
1945         struct mdt_object        *obj;
1946         const struct lu_env      *env;
1947         struct req_capsule       *pill;
1948         int                       rc;
1949         ENTRY;
1950
1951         env = info->mti_env;
1952         pill = info->mti_pill;
1953
1954         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1955         if (body == NULL)
1956                 RETURN(-EFAULT);
1957
1958         if (!(body->valid & OBD_MD_FLID))
1959                 RETURN(0);
1960
1961         if (!fid_is_sane(&body->fid1)) {
1962                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
1963                 RETURN(-EINVAL);
1964         }
1965
1966         /*
1967          * Do not get size or any capa fields before we check that request
1968          * contains capa actually. There are some requests which do not, for
1969          * instance MDS_IS_SUBDIR.
1970          */
1971         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
1972             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1973                 mdt_set_capainfo(info, 0, &body->fid1,
1974                                  req_capsule_client_get(pill, &RMF_CAPA1));
1975
1976         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
1977         if (!IS_ERR(obj)) {
1978                 if ((flags & HABEO_CORPUS) &&
1979                     !mdt_object_exists(obj)) {
1980                         mdt_object_put(env, obj);
1981                         /* for capability renew ENOENT will be handled in
1982                          * mdt_renew_capa */
1983                         if (body->valid & OBD_MD_FLOSSCAPA)
1984                                 rc = 0;
1985                         else
1986                                 rc = -ENOENT;
1987                 } else {
1988                         info->mti_object = obj;
1989                         rc = 0;
1990                 }
1991         } else
1992                 rc = PTR_ERR(obj);
1993
1994         RETURN(rc);
1995 }
1996
1997 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
1998 {
1999         struct req_capsule *pill = info->mti_pill;
2000         int rc;
2001         ENTRY;
2002
2003         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2004                 rc = mdt_body_unpack(info, flags);
2005         else
2006                 rc = 0;
2007
2008         if (rc == 0 && (flags & HABEO_REFERO)) {
2009                 struct mdt_device *mdt = info->mti_mdt;
2010
2011                 /* Pack reply. */
2012                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2013                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2014                                              mdt->mdt_max_mdsize);
2015                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2016                         req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2017                                              mdt->mdt_max_cookiesize);
2018
2019                 rc = req_capsule_server_pack(pill);
2020         }
2021         RETURN(rc);
2022 }
2023
2024 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2025 {
2026         struct md_device *next = m->mdt_child;
2027
2028         return next->md_ops->mdo_init_capa_ctxt(env, next,
2029                                                 m->mdt_opts.mo_mds_capa,
2030                                                 m->mdt_capa_timeout,
2031                                                 m->mdt_capa_alg,
2032                                                 m->mdt_capa_keys);
2033 }
2034
2035 /*
2036  * Invoke handler for this request opc. Also do necessary preprocessing
2037  * (according to handler ->mh_flags), and post-processing (setting of
2038  * ->last_{xid,committed}).
2039  */
2040 static int mdt_req_handle(struct mdt_thread_info *info,
2041                           struct mdt_handler *h, struct ptlrpc_request *req)
2042 {
2043         int   rc, serious = 0;
2044         __u32 flags;
2045
2046         ENTRY;
2047
2048         LASSERT(h->mh_act != NULL);
2049         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2050         LASSERT(current->journal_info == NULL);
2051
2052         /*
2053          * Mask out OBD_FAIL_ONCE, because that will stop
2054          * correct handling of failed req later in ldlm due to doing
2055          * obd_fail_loc |= OBD_FAIL_ONCE without actually
2056          * correct actions like it is done in target_send_reply_msg().
2057          */
2058         if (h->mh_fail_id != 0) {
2059                 /*
2060                  * Set to info->mti_fail_id to handler fail_id, it will be used
2061                  * later, and better than use default fail_id.
2062                  */
2063                 if (OBD_FAIL_CHECK_RESET(h->mh_fail_id && OBD_FAIL_MASK_LOC,
2064                                          h->mh_fail_id & ~OBD_FAILED)) {
2065                         info->mti_fail_id = h->mh_fail_id;
2066                         RETURN(0);
2067                 }
2068         }
2069
2070         rc = 0;
2071         flags = h->mh_flags;
2072         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2073
2074         if (h->mh_fmt != NULL) {
2075                 req_capsule_set(info->mti_pill, h->mh_fmt);
2076                 rc = mdt_unpack_req_pack_rep(info, flags);
2077         }
2078
2079         if (rc == 0 && flags & MUTABOR &&
2080             req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2081                 /* should it be rq_status? */
2082                 rc = -EROFS;
2083
2084         if (rc == 0 && flags & HABEO_CLAVIS) {
2085                 struct ldlm_request *dlm_req;
2086
2087                 LASSERT(h->mh_fmt != NULL);
2088
2089                 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2090                 if (dlm_req != NULL) {
2091                         if (info->mti_mdt->mdt_opts.mo_compat_resname)
2092                                 rc = mdt_lock_resname_compat(info->mti_mdt,
2093                                                              dlm_req);
2094                         info->mti_dlm_req = dlm_req;
2095                 } else {
2096                         rc = -EFAULT;
2097                 }
2098         }
2099
2100         /* capability setting changed via /proc, needs reinitialize ctxt */
2101         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2102                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2103                 info->mti_mdt->mdt_capa_conf = 0;
2104         }
2105
2106         if (likely(rc == 0)) {
2107                 /*
2108                  * Process request, there can be two types of rc:
2109                  * 1) errors with msg unpack/pack, other failures outside the
2110                  * operation itself. This is counted as serious errors;
2111                  * 2) errors during fs operation, should be placed in rq_status
2112                  * only
2113                  */
2114                 rc = h->mh_act(info);
2115                 serious = is_serious(rc);
2116                 rc = clear_serious(rc);
2117         } else
2118                 serious = 1;
2119
2120         req->rq_status = rc;
2121
2122         /*
2123          * ELDLM_* codes which > 0 should be in rq_status only as well as
2124          * all non-serious errors.
2125          */
2126         if (rc > 0 || !serious)
2127                 rc = 0;
2128
2129         LASSERT(current->journal_info == NULL);
2130
2131         if (rc == 0 && (flags & HABEO_CLAVIS) &&
2132             info->mti_mdt->mdt_opts.mo_compat_resname) {
2133                 struct ldlm_reply *dlmrep;
2134
2135                 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2136                 if (dlmrep != NULL)
2137                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2138         }
2139
2140         /* If we're DISCONNECTing, the mdt_export_data is already freed */
2141         if (likely(rc == 0 && h->mh_opc != MDS_DISCONNECT))
2142                 target_committed_to_req(req);
2143
2144         if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2145                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2146                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2147                 LBUG();
2148         }
2149
2150         RETURN(rc);
2151 }
2152
2153 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2154 {
2155         lh->mlh_type = MDT_NUL_LOCK;
2156         lh->mlh_reg_lh.cookie = 0ull;
2157         lh->mlh_reg_mode = LCK_MINMODE;
2158         lh->mlh_pdo_lh.cookie = 0ull;
2159         lh->mlh_pdo_mode = LCK_MINMODE;
2160 }
2161
2162 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2163 {
2164         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2165         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2166 }
2167
2168 /*
2169  * Initialize fields of struct mdt_thread_info. Other fields are left in
2170  * uninitialized state, because it's too expensive to zero out whole
2171  * mdt_thread_info (> 1K) on each request arrival.
2172  */
2173 static void mdt_thread_info_init(struct ptlrpc_request *req,
2174                                  struct mdt_thread_info *info)
2175 {
2176         int i;
2177         struct md_capainfo *ci;
2178
2179         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2180         info->mti_pill = &req->rq_pill;
2181
2182         /* lock handle */
2183         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2184                 mdt_lock_handle_init(&info->mti_lh[i]);
2185
2186         /* mdt device: it can be NULL while CONNECT */
2187         if (req->rq_export) {
2188                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2189                 info->mti_exp = req->rq_export;
2190         } else
2191                 info->mti_mdt = NULL;
2192         info->mti_env = req->rq_svc_thread->t_env;
2193         ci = md_capainfo(info->mti_env);
2194         memset(ci, 0, sizeof *ci);
2195
2196         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2197         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2198
2199         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2200         info->mti_body = NULL;
2201         info->mti_object = NULL;
2202         info->mti_dlm_req = NULL;
2203         info->mti_has_trans = 0;
2204         info->mti_no_need_trans = 0;
2205         info->mti_cross_ref = 0;
2206         info->mti_opdata = 0;
2207
2208         /* To not check for split by default. */
2209         info->mti_spec.sp_ck_split = 0;
2210 }
2211
2212 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2213 {
2214         int i;
2215
2216         req_capsule_fini(info->mti_pill);
2217         if (info->mti_object != NULL) {
2218                 mdt_object_put(info->mti_env, info->mti_object);
2219                 info->mti_object = NULL;
2220         }
2221         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2222                 mdt_lock_handle_fini(&info->mti_lh[i]);
2223         info->mti_env = NULL;
2224 }
2225
2226 /* mds/handler.c */
2227 extern int mds_filter_recovery_request(struct ptlrpc_request *req,
2228                                        struct obd_device *obd, int *process);
2229 /*
2230  * Handle recovery. Return:
2231  *        +1: continue request processing;
2232  *       -ve: abort immediately with the given error code;
2233  *         0: send reply with error code in req->rq_status;
2234  */
2235 static int mdt_recovery(struct mdt_thread_info *info)
2236 {
2237         struct ptlrpc_request *req = mdt_info_req(info);
2238         int recovering;
2239         struct obd_device *obd;
2240
2241         ENTRY;
2242
2243         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2244         case MDS_CONNECT:
2245         case SEC_CTX_INIT:
2246         case SEC_CTX_INIT_CONT:
2247         case SEC_CTX_FINI:
2248                 {
2249 #if 0
2250                         int rc;
2251
2252                         rc = mdt_handle_idmap(info);
2253                         if (rc)
2254                                 RETURN(rc);
2255                         else
2256 #endif
2257                                 RETURN(+1);
2258                 }
2259         }
2260
2261         if (unlikely(req->rq_export == NULL)) {
2262                 CERROR("operation %d on unconnected MDS from %s\n",
2263                        lustre_msg_get_opc(req->rq_reqmsg),
2264                        libcfs_id2str(req->rq_peer));
2265                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2266                  * mds_A will get -ENOTCONN(especially for ping req),
2267                  * which will cause that mds_A deactive timeout, then when
2268                  * mds_A cleanup, the cleanup process will be suspended since
2269                  * deactive timeout is not zero.
2270                  */
2271                 req->rq_status = -ENOTCONN;
2272                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2273                 RETURN(0);
2274         }
2275
2276         /* sanity check: if the xid matches, the request must be marked as a
2277          * resent or replayed */
2278         if (req_xid_is_last(req)) {
2279                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2280                       (MSG_RESENT | MSG_REPLAY))) {
2281                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2282                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2283                                   lustre_msg_get_flags(req->rq_reqmsg));
2284                         LBUG();
2285                         req->rq_status = -ENOTCONN;
2286                         RETURN(-ENOTCONN);
2287                 }
2288         }
2289
2290         /* else: note the opposite is not always true; a RESENT req after a
2291          * failover will usually not match the last_xid, since it was likely
2292          * never committed. A REPLAYed request will almost never match the
2293          * last xid, however it could for a committed, but still retained,
2294          * open. */
2295
2296         obd = req->rq_export->exp_obd;
2297
2298         /* Check for aborted recovery... */
2299         spin_lock_bh(&obd->obd_processing_task_lock);
2300         recovering = obd->obd_recovering;
2301         spin_unlock_bh(&obd->obd_processing_task_lock);
2302         if (unlikely(recovering)) {
2303                 int rc;
2304                 int should_process;
2305                 DEBUG_REQ(D_INFO, req, "Got new replay");
2306                 rc = mds_filter_recovery_request(req, obd, &should_process);
2307                 if (rc != 0 || !should_process)
2308                         RETURN(rc);
2309                 else if (should_process < 0) {
2310                         req->rq_status = should_process;
2311                         rc = ptlrpc_error(req);
2312                         RETURN(rc);
2313                 }
2314         }
2315         RETURN(+1);
2316 }
2317
2318 static int mdt_reply(struct ptlrpc_request *req, int rc,
2319                      struct mdt_thread_info *info)
2320 {
2321         ENTRY;
2322
2323 #if 0
2324         if (req->rq_reply_state == NULL && rc == 0) {
2325                 req->rq_status = rc;
2326                 lustre_pack_reply(req, 1, NULL, NULL);
2327         }
2328 #endif
2329         target_send_reply(req, rc, info->mti_fail_id);
2330         RETURN(0);
2331 }
2332
2333 /* mds/handler.c */
2334 extern int mds_msg_check_version(struct lustre_msg *msg);
2335
2336 static int mdt_handle0(struct ptlrpc_request *req,
2337                        struct mdt_thread_info *info,
2338                        struct mdt_opc_slice *supported)
2339 {
2340         struct mdt_handler *h;
2341         struct lustre_msg  *msg;
2342         int                 rc;
2343
2344         ENTRY;
2345
2346         if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2347                 RETURN(0);
2348
2349         LASSERT(current->journal_info == NULL);
2350
2351         msg = req->rq_reqmsg;
2352         rc = mds_msg_check_version(msg);
2353         if (likely(rc == 0)) {
2354                 rc = mdt_recovery(info);
2355                 if (likely(rc == +1)) {
2356                         h = mdt_handler_find(lustre_msg_get_opc(msg),
2357                                              supported);
2358                         if (likely(h != NULL)) {
2359                                 rc = mdt_req_handle(info, h, req);
2360                                 rc = mdt_reply(req, rc, info);
2361                         } else {
2362                                 CERROR("The unsupported opc: 0x%x\n", lustre_msg_get_opc(msg) );
2363                                 req->rq_status = -ENOTSUPP;
2364                                 rc = ptlrpc_error(req);
2365                                 RETURN(rc);
2366                         }
2367                 }
2368         } else
2369                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2370         RETURN(rc);
2371 }
2372
2373 /*
2374  * MDT handler function called by ptlrpc service thread when request comes.
2375  *
2376  * XXX common "target" functionality should be factored into separate module
2377  * shared by mdt, ost and stand-alone services like fld.
2378  */
2379 static int mdt_handle_common(struct ptlrpc_request *req,
2380                              struct mdt_opc_slice *supported)
2381 {
2382         struct lu_env          *env;
2383         struct mdt_thread_info *info;
2384         int                     rc;
2385         ENTRY;
2386
2387         env = req->rq_svc_thread->t_env;
2388         LASSERT(env != NULL);
2389         LASSERT(env->le_ses != NULL);
2390         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2391         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2392         LASSERT(info != NULL);
2393
2394         mdt_thread_info_init(req, info);
2395
2396         rc = mdt_handle0(req, info, supported);
2397
2398         mdt_thread_info_fini(info);
2399         RETURN(rc);
2400 }
2401
2402 /*
2403  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2404  * as well.
2405  */
2406 int mdt_recovery_handle(struct ptlrpc_request *req)
2407 {
2408         int rc;
2409         ENTRY;
2410
2411         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2412         case FLD_QUERY:
2413                 rc = mdt_handle_common(req, mdt_fld_handlers);
2414                 break;
2415         case SEQ_QUERY:
2416                 rc = mdt_handle_common(req, mdt_seq_handlers);
2417                 break;
2418         default:
2419                 rc = mdt_handle_common(req, mdt_regular_handlers);
2420                 break;
2421         }
2422
2423         RETURN(rc);
2424 }
2425
2426 static int mdt_regular_handle(struct ptlrpc_request *req)
2427 {
2428         return mdt_handle_common(req, mdt_regular_handlers);
2429 }
2430
2431 static int mdt_readpage_handle(struct ptlrpc_request *req)
2432 {
2433         return mdt_handle_common(req, mdt_readpage_handlers);
2434 }
2435
2436 static int mdt_xmds_handle(struct ptlrpc_request *req)
2437 {
2438         return mdt_handle_common(req, mdt_xmds_handlers);
2439 }
2440
2441 static int mdt_mdsc_handle(struct ptlrpc_request *req)
2442 {
2443         return mdt_handle_common(req, mdt_seq_handlers);
2444 }
2445
2446 static int mdt_mdss_handle(struct ptlrpc_request *req)
2447 {
2448         return mdt_handle_common(req, mdt_seq_handlers);
2449 }
2450
2451 static int mdt_dtss_handle(struct ptlrpc_request *req)
2452 {
2453         return mdt_handle_common(req, mdt_seq_handlers);
2454 }
2455
2456 static int mdt_fld_handle(struct ptlrpc_request *req)
2457 {
2458         return mdt_handle_common(req, mdt_fld_handlers);
2459 }
2460
2461 enum mdt_it_code {
2462         MDT_IT_OPEN,
2463         MDT_IT_OCREAT,
2464         MDT_IT_CREATE,
2465         MDT_IT_GETATTR,
2466         MDT_IT_READDIR,
2467         MDT_IT_LOOKUP,
2468         MDT_IT_UNLINK,
2469         MDT_IT_TRUNC,
2470         MDT_IT_GETXATTR,
2471         MDT_IT_NR
2472 };
2473
2474 static int mdt_intent_getattr(enum mdt_it_code opcode,
2475                               struct mdt_thread_info *info,
2476                               struct ldlm_lock **,
2477                               int);
2478 static int mdt_intent_reint(enum mdt_it_code opcode,
2479                             struct mdt_thread_info *info,
2480                             struct ldlm_lock **,
2481                             int);
2482
2483 static struct mdt_it_flavor {
2484         const struct req_format *it_fmt;
2485         __u32                    it_flags;
2486         int                    (*it_act)(enum mdt_it_code ,
2487                                          struct mdt_thread_info *,
2488                                          struct ldlm_lock **,
2489                                          int);
2490         long                     it_reint;
2491 } mdt_it_flavor[] = {
2492         [MDT_IT_OPEN]     = {
2493                 .it_fmt   = &RQF_LDLM_INTENT,
2494                 /*.it_flags = HABEO_REFERO,*/
2495                 .it_flags = 0,
2496                 .it_act   = mdt_intent_reint,
2497                 .it_reint = REINT_OPEN
2498         },
2499         [MDT_IT_OCREAT]   = {
2500                 .it_fmt   = &RQF_LDLM_INTENT,
2501                 .it_flags = MUTABOR,
2502                 .it_act   = mdt_intent_reint,
2503                 .it_reint = REINT_OPEN
2504         },
2505         [MDT_IT_CREATE]   = {
2506                 .it_fmt   = &RQF_LDLM_INTENT,
2507                 .it_flags = MUTABOR,
2508                 .it_act   = mdt_intent_reint,
2509                 .it_reint = REINT_CREATE
2510         },
2511         [MDT_IT_GETATTR]  = {
2512                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2513                 .it_flags = HABEO_REFERO,
2514                 .it_act   = mdt_intent_getattr
2515         },
2516         [MDT_IT_READDIR]  = {
2517                 .it_fmt   = NULL,
2518                 .it_flags = 0,
2519                 .it_act   = NULL
2520         },
2521         [MDT_IT_LOOKUP]   = {
2522                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
2523                 .it_flags = HABEO_REFERO,
2524                 .it_act   = mdt_intent_getattr
2525         },
2526         [MDT_IT_UNLINK]   = {
2527                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
2528                 .it_flags = MUTABOR,
2529                 .it_act   = NULL,
2530                 .it_reint = REINT_UNLINK
2531         },
2532         [MDT_IT_TRUNC]    = {
2533                 .it_fmt   = NULL,
2534                 .it_flags = MUTABOR,
2535                 .it_act   = NULL
2536         },
2537         [MDT_IT_GETXATTR] = {
2538                 .it_fmt   = NULL,
2539                 .it_flags = 0,
2540                 .it_act   = NULL
2541         }
2542 };
2543
2544 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2545                             struct ldlm_lock **lockp,
2546                             struct ldlm_lock *new_lock,
2547                             struct mdt_lock_handle *lh,
2548                             int flags)
2549 {
2550         struct ptlrpc_request  *req = mdt_info_req(info);
2551         struct ldlm_lock       *lock = *lockp;
2552
2553         /*
2554          * Get new lock only for cases when possible resent did not find any
2555          * lock.
2556          */
2557         if (new_lock == NULL)
2558                 new_lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2559
2560         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2561                 lh->mlh_reg_lh.cookie = 0;
2562                 RETURN(0);
2563         }
2564
2565         LASSERTF(new_lock != NULL,
2566                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2567
2568         /*
2569          * If we've already given this lock to a client once, then we should
2570          * have no readers or writers.  Otherwise, we should have one reader
2571          * _or_ writer ref (which will be zeroed below) before returning the
2572          * lock to a client.
2573          */
2574         if (new_lock->l_export == req->rq_export) {
2575                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2576         } else {
2577                 LASSERT(new_lock->l_export == NULL);
2578                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2579         }
2580
2581         *lockp = new_lock;
2582
2583         if (new_lock->l_export == req->rq_export) {
2584                 /*
2585                  * Already gave this to the client, which means that we
2586                  * reconstructed a reply.
2587                  */
2588                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2589                         MSG_RESENT);
2590                 lh->mlh_reg_lh.cookie = 0;
2591                 RETURN(ELDLM_LOCK_REPLACED);
2592         }
2593
2594         /* This lock might already be given to the client by an resent req,
2595          * in this case we should return ELDLM_LOCK_ABORTED,
2596          * so we should check led_held_locks here, but it will affect
2597          * performance, FIXME
2598          */
2599         /* Fixup the lock to be given to the client */
2600         lock_res_and_lock(new_lock);
2601         new_lock->l_readers = 0;
2602         new_lock->l_writers = 0;
2603
2604         new_lock->l_export = class_export_get(req->rq_export);
2605         spin_lock(&req->rq_export->exp_ldlm_data.led_lock);
2606         list_add(&new_lock->l_export_chain,
2607                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
2608         spin_unlock(&req->rq_export->exp_ldlm_data.led_lock);
2609
2610         new_lock->l_blocking_ast = lock->l_blocking_ast;
2611         new_lock->l_completion_ast = lock->l_completion_ast;
2612         new_lock->l_remote_handle = lock->l_remote_handle;
2613         new_lock->l_flags &= ~LDLM_FL_LOCAL;
2614
2615         unlock_res_and_lock(new_lock);
2616         LDLM_LOCK_PUT(new_lock);
2617         lh->mlh_reg_lh.cookie = 0;
2618
2619         RETURN(ELDLM_LOCK_REPLACED);
2620 }
2621
2622 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2623                                     struct ldlm_lock *new_lock,
2624                                     struct ldlm_lock **old_lock,
2625                                     struct mdt_lock_handle *lh)
2626 {
2627         struct ptlrpc_request  *req = mdt_info_req(info);
2628         struct obd_export      *exp = req->rq_export;
2629         struct lustre_handle    remote_hdl;
2630         struct ldlm_request    *dlmreq;
2631         struct list_head       *iter;
2632
2633         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2634                 return;
2635
2636         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2637         remote_hdl = dlmreq->lock_handle[0];
2638
2639         spin_lock(&exp->exp_ldlm_data.led_lock);
2640         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2641                 struct ldlm_lock *lock;
2642                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2643                 if (lock == new_lock)
2644                         continue;
2645                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2646                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2647                         lh->mlh_reg_mode = lock->l_granted_mode;
2648
2649                         LDLM_DEBUG(lock, "restoring lock cookie");
2650                         DEBUG_REQ(D_DLMTRACE, req,
2651                                   "restoring lock cookie "LPX64,
2652                                   lh->mlh_reg_lh.cookie);
2653                         if (old_lock)
2654                                 *old_lock = LDLM_LOCK_GET(lock);
2655                         spin_unlock(&exp->exp_ldlm_data.led_lock);
2656                         return;
2657                 }
2658         }
2659         spin_unlock(&exp->exp_ldlm_data.led_lock);
2660
2661         /*
2662          * If the xid matches, then we know this is a resent request, and allow
2663          * it. (It's probably an OPEN, for which we don't send a lock.
2664          */
2665         if (req_xid_is_last(req))
2666                 return;
2667
2668         /*
2669          * This remote handle isn't enqueued, so we never received or processed
2670          * this request.  Clear MSG_RESENT, because it can be handled like any
2671          * normal request now.
2672          */
2673         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2674
2675         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2676                   remote_hdl.cookie);
2677 }
2678
2679 static int mdt_intent_getattr(enum mdt_it_code opcode,
2680                               struct mdt_thread_info *info,
2681                               struct ldlm_lock **lockp,
2682                               int flags)
2683 {
2684         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2685         struct ldlm_lock       *new_lock = NULL;
2686         __u64                   child_bits;
2687         struct ldlm_reply      *ldlm_rep;
2688         struct ptlrpc_request  *req;
2689         struct mdt_body        *reqbody;
2690         struct mdt_body        *repbody;
2691         int                     rc;
2692         ENTRY;
2693
2694         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2695         LASSERT(reqbody);
2696
2697         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2698         LASSERT(repbody);
2699
2700         info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
2701         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
2702         repbody->eadatasize = 0;
2703         repbody->aclsize = 0;
2704
2705         switch (opcode) {
2706         case MDT_IT_LOOKUP:
2707                 child_bits = MDS_INODELOCK_LOOKUP;
2708                 break;
2709         case MDT_IT_GETATTR:
2710                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2711                 break;
2712         default:
2713                 CERROR("Unhandled till now");
2714                 GOTO(out_shrink, rc = -EINVAL);
2715         }
2716
2717         rc = mdt_init_ucred(info, reqbody);
2718         if (rc)
2719                 GOTO(out_shrink, rc);
2720
2721         req = info->mti_pill->rc_req;
2722         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2723         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
2724
2725         /* Get lock from request for possible resent case. */
2726         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
2727
2728         ldlm_rep->lock_policy_res2 =
2729                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
2730
2731         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
2732                 ldlm_rep->lock_policy_res2 = 0;
2733         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
2734             ldlm_rep->lock_policy_res2) {
2735                 lhc->mlh_reg_lh.cookie = 0ull;
2736                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
2737         }
2738
2739         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
2740         EXIT;
2741 out_ucred:
2742         mdt_exit_ucred(info);
2743 out_shrink:
2744         mdt_shrink_reply(info);
2745         return rc;
2746 }
2747
2748 static int mdt_intent_reint(enum mdt_it_code opcode,
2749                             struct mdt_thread_info *info,
2750                             struct ldlm_lock **lockp,
2751                             int flags)
2752 {
2753         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2754         struct ldlm_reply      *rep = NULL;
2755         long                    opc;
2756         int                     rc;
2757
2758         static const struct req_format *intent_fmts[REINT_MAX] = {
2759                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
2760                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
2761         };
2762
2763         ENTRY;
2764
2765         opc = mdt_reint_opcode(info, intent_fmts);
2766         if (opc < 0)
2767                 RETURN(opc);
2768
2769         if (mdt_it_flavor[opcode].it_reint != opc) {
2770                 CERROR("Reint code %ld doesn't match intent: %d\n",
2771                        opc, opcode);
2772                 RETURN(err_serious(-EPROTO));
2773         }
2774
2775         /* Get lock from request for possible resent case. */
2776         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
2777
2778         rc = mdt_reint_internal(info, lhc, opc);
2779
2780         /* Check whether the reply has been packed successfully. */
2781         if (mdt_info_req(info)->rq_repmsg != NULL)
2782                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2783         if (rep == NULL)
2784                 RETURN(err_serious(-EFAULT));
2785
2786         /* MDC expects this in any case */
2787         if (rc != 0)
2788                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
2789
2790         /* Cross-ref case, the lock should be returned to the client */
2791         if (rc == -EREMOTE) {
2792                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
2793                 rep->lock_policy_res2 = 0;
2794                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
2795                 RETURN(rc);
2796         }
2797         rep->lock_policy_res2 = clear_serious(rc);
2798
2799         lhc->mlh_reg_lh.cookie = 0ull;
2800         rc = ELDLM_LOCK_ABORTED;
2801         RETURN(rc);
2802 }
2803
2804 static int mdt_intent_code(long itcode)
2805 {
2806         int rc;
2807
2808         switch(itcode) {
2809         case IT_OPEN:
2810                 rc = MDT_IT_OPEN;
2811                 break;
2812         case IT_OPEN|IT_CREAT:
2813                 rc = MDT_IT_OCREAT;
2814                 break;
2815         case IT_CREAT:
2816                 rc = MDT_IT_CREATE;
2817                 break;
2818         case IT_READDIR:
2819                 rc = MDT_IT_READDIR;
2820                 break;
2821         case IT_GETATTR:
2822                 rc = MDT_IT_GETATTR;
2823                 break;
2824         case IT_LOOKUP:
2825                 rc = MDT_IT_LOOKUP;
2826                 break;
2827         case IT_UNLINK:
2828                 rc = MDT_IT_UNLINK;
2829                 break;
2830         case IT_TRUNC:
2831                 rc = MDT_IT_TRUNC;
2832                 break;
2833         case IT_GETXATTR:
2834                 rc = MDT_IT_GETXATTR;
2835                 break;
2836         default:
2837                 CERROR("Unknown intent opcode: %ld\n", itcode);
2838                 rc = -EINVAL;
2839                 break;
2840         }
2841         return rc;
2842 }
2843
2844 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
2845                           struct ldlm_lock **lockp, int flags)
2846 {
2847         struct req_capsule   *pill;
2848         struct mdt_it_flavor *flv;
2849         int opc;
2850         int rc;
2851         ENTRY;
2852
2853         opc = mdt_intent_code(itopc);
2854         if (opc < 0)
2855                 RETURN(-EINVAL);
2856
2857         pill = info->mti_pill;
2858         flv  = &mdt_it_flavor[opc];
2859
2860         if (flv->it_fmt != NULL)
2861                 req_capsule_extend(pill, flv->it_fmt);
2862
2863         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
2864         if (rc == 0) {
2865                 struct ptlrpc_request *req = mdt_info_req(info);
2866                 if (flv->it_flags & MUTABOR &&
2867                     req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2868                         RETURN(-EROFS);
2869         }
2870         if (rc == 0 && flv->it_act != NULL) {
2871                 /* execute policy */
2872                 rc = flv->it_act(opc, info, lockp, flags);
2873         } else {
2874                 rc = -EOPNOTSUPP;
2875         }
2876         RETURN(rc);
2877 }
2878
2879 static int mdt_intent_policy(struct ldlm_namespace *ns,
2880                              struct ldlm_lock **lockp, void *req_cookie,
2881                              ldlm_mode_t mode, int flags, void *data)
2882 {
2883         struct mdt_thread_info *info;
2884         struct ptlrpc_request  *req  =  req_cookie;
2885         struct ldlm_intent     *it;
2886         struct req_capsule     *pill;
2887         int rc;
2888
2889         ENTRY;
2890
2891         LASSERT(req != NULL);
2892
2893         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
2894                                   &mdt_thread_key);
2895         LASSERT(info != NULL);
2896         pill = info->mti_pill;
2897         LASSERT(pill->rc_req == req);
2898
2899         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
2900                 req_capsule_extend(pill, &RQF_LDLM_INTENT);
2901                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
2902                 if (it != NULL) {
2903                         const struct ldlm_request *dlmreq;
2904                         __u64 req_bits;
2905 #if 0
2906                         struct ldlm_lock       *lock = *lockp;
2907
2908                         LDLM_DEBUG(lock, "intent policy opc: %s\n",
2909                                    ldlm_it2str(it->opc));
2910 #endif
2911
2912                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
2913                         if (rc == 0)
2914                                 rc = ELDLM_OK;
2915
2916                         /*
2917                          * Lock without inodebits makes no sense and will oops
2918                          * later in ldlm. Let's check it now to see if we have
2919                          * wrong lock from client or bits get corrupted
2920                          * somewhere in mdt_intent_opc().
2921                          */
2922                         dlmreq = info->mti_dlm_req;
2923                         req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
2924                         LASSERT(req_bits != 0);
2925
2926                 } else
2927                         rc = err_serious(-EFAULT);
2928         } else {
2929                 /* No intent was provided */
2930                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
2931                 rc = req_capsule_server_pack(pill);
2932                 if (rc)
2933                         rc = err_serious(rc);
2934         }
2935         RETURN(rc);
2936 }
2937
2938 /*
2939  * Seq wrappers
2940  */
2941 static void mdt_seq_adjust(const struct lu_env *env,
2942                           struct mdt_device *m, int lost)
2943 {
2944         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2945         struct lu_range out;
2946         ENTRY;
2947
2948         LASSERT(ls && ls->ls_server_seq);
2949         LASSERT(lost >= 0);
2950         /* get extra seq from seq_server, moving it's range up */
2951         while (lost-- > 0) {
2952                 seq_server_alloc_meta(ls->ls_server_seq, NULL, &out, env);
2953         }
2954         EXIT;
2955 }
2956
2957 static int mdt_seq_fini(const struct lu_env *env,
2958                         struct mdt_device *m)
2959 {
2960         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2961         ENTRY;
2962
2963         if (ls && ls->ls_server_seq) {
2964                 seq_server_fini(ls->ls_server_seq, env);
2965                 OBD_FREE_PTR(ls->ls_server_seq);
2966                 ls->ls_server_seq = NULL;
2967         }
2968
2969         if (ls && ls->ls_control_seq) {
2970                 seq_server_fini(ls->ls_control_seq, env);
2971                 OBD_FREE_PTR(ls->ls_control_seq);
2972                 ls->ls_control_seq = NULL;
2973         }
2974
2975         if (ls && ls->ls_client_seq) {
2976                 seq_client_fini(ls->ls_client_seq);
2977                 OBD_FREE_PTR(ls->ls_client_seq);
2978                 ls->ls_client_seq = NULL;
2979         }
2980
2981         RETURN(0);
2982 }
2983
2984 static int mdt_seq_init(const struct lu_env *env,
2985                         const char *uuid,
2986                         struct mdt_device *m)
2987 {
2988         struct lu_site *ls;
2989         char *prefix;
2990         int rc;
2991         ENTRY;
2992
2993         ls = m->mdt_md_dev.md_lu_dev.ld_site;
2994
2995         /*
2996          * This is sequence-controller node. Init seq-controller server on local
2997          * MDT.
2998          */
2999         if (ls->ls_node_id == 0) {
3000                 LASSERT(ls->ls_control_seq == NULL);
3001
3002                 OBD_ALLOC_PTR(ls->ls_control_seq);
3003                 if (ls->ls_control_seq == NULL)
3004                         RETURN(-ENOMEM);
3005
3006                 rc = seq_server_init(ls->ls_control_seq,
3007                                      m->mdt_bottom, uuid,
3008                                      LUSTRE_SEQ_CONTROLLER,
3009                                      env);
3010
3011                 if (rc)
3012                         GOTO(out_seq_fini, rc);
3013
3014                 OBD_ALLOC_PTR(ls->ls_client_seq);
3015                 if (ls->ls_client_seq == NULL)
3016                         GOTO(out_seq_fini, rc = -ENOMEM);
3017
3018                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3019                 if (prefix == NULL) {
3020                         OBD_FREE_PTR(ls->ls_client_seq);
3021                         GOTO(out_seq_fini, rc = -ENOMEM);
3022                 }
3023
3024                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3025                          uuid);
3026
3027                 /*
3028                  * Init seq-controller client after seq-controller server is
3029                  * ready. Pass ls->ls_control_seq to it for direct talking.
3030                  */
3031                 rc = seq_client_init(ls->ls_client_seq, NULL,
3032                                      LUSTRE_SEQ_METADATA, prefix,
3033                                      ls->ls_control_seq);
3034                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3035
3036                 if (rc)
3037                         GOTO(out_seq_fini, rc);
3038         }
3039
3040         /* Init seq-server on local MDT */
3041         LASSERT(ls->ls_server_seq == NULL);
3042
3043         OBD_ALLOC_PTR(ls->ls_server_seq);
3044         if (ls->ls_server_seq == NULL)
3045                 GOTO(out_seq_fini, rc = -ENOMEM);
3046
3047         rc = seq_server_init(ls->ls_server_seq,
3048                              m->mdt_bottom, uuid,
3049                              LUSTRE_SEQ_SERVER,
3050                              env);
3051         if (rc)
3052                 GOTO(out_seq_fini, rc = -ENOMEM);
3053
3054         /* Assign seq-controller client to local seq-server. */
3055         if (ls->ls_node_id == 0) {
3056                 LASSERT(ls->ls_client_seq != NULL);
3057
3058                 rc = seq_server_set_cli(ls->ls_server_seq,
3059                                         ls->ls_client_seq,
3060                                         env);
3061         }
3062
3063         EXIT;
3064 out_seq_fini:
3065         if (rc)
3066                 mdt_seq_fini(env, m);
3067
3068         return rc;
3069 }
3070 /*
3071  * Init client sequence manager which is used by local MDS to talk to sequence
3072  * controller on remote node.
3073  */
3074 static int mdt_seq_init_cli(const struct lu_env *env,
3075                             struct mdt_device *m,
3076                             struct lustre_cfg *cfg)
3077 {
3078         struct lu_site    *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3079         struct obd_device *mdc;
3080         struct obd_uuid   *uuidp, *mdcuuidp;
3081         char              *uuid_str, *mdc_uuid_str;
3082         int                rc;
3083         int                index;
3084         struct mdt_thread_info *info;
3085         char *p, *index_string = lustre_cfg_string(cfg, 2);
3086         ENTRY;
3087
3088         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3089         uuidp = &info->mti_u.uuid[0];
3090         mdcuuidp = &info->mti_u.uuid[1];
3091
3092         LASSERT(index_string);
3093
3094         index = simple_strtol(index_string, &p, 10);
3095         if (*p) {
3096                 CERROR("Invalid index in lustre_cgf, offset 2\n");
3097                 RETURN(-EINVAL);
3098         }
3099
3100         /* check if this is adding the first MDC and controller is not yet
3101          * initialized. */
3102         if (index != 0 || ls->ls_client_seq)
3103                 RETURN(0);
3104
3105         uuid_str = lustre_cfg_string(cfg, 1);
3106         mdc_uuid_str = lustre_cfg_string(cfg, 4);
3107         obd_str2uuid(uuidp, uuid_str);
3108         obd_str2uuid(mdcuuidp, mdc_uuid_str);
3109
3110         mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3111         if (!mdc) {
3112                 CERROR("can't find controller MDC by uuid %s\n",
3113                        uuid_str);
3114                 rc = -ENOENT;
3115         } else if (!mdc->obd_set_up) {
3116                 CERROR("target %s not set up\n", mdc->obd_name);
3117                 rc = -EINVAL;
3118         } else {
3119                 LASSERT(ls->ls_control_exp);
3120                 OBD_ALLOC_PTR(ls->ls_client_seq);
3121                 if (ls->ls_client_seq != NULL) {
3122                         char *prefix;
3123
3124                         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3125                         if (!prefix)
3126                                 RETURN(-ENOMEM);
3127
3128                         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3129                                  mdc->obd_name);
3130
3131                         rc = seq_client_init(ls->ls_client_seq,
3132                                              ls->ls_control_exp,
3133                                              LUSTRE_SEQ_METADATA,
3134                                              prefix, NULL);
3135                         OBD_FREE(prefix, MAX_OBD_NAME + 5);
3136                 } else
3137                         rc = -ENOMEM;
3138
3139                 if (rc)
3140                         RETURN(rc);
3141
3142                 LASSERT(ls->ls_server_seq != NULL);
3143                 rc = seq_server_set_cli(ls->ls_server_seq, ls->ls_client_seq,
3144                                         env);
3145         }
3146
3147         RETURN(rc);
3148 }
3149
3150 static void mdt_seq_fini_cli(struct mdt_device *m)
3151 {
3152         struct lu_site *ls;
3153
3154         ENTRY;
3155
3156         ls = m->mdt_md_dev.md_lu_dev.ld_site;
3157
3158         if (ls && ls->ls_server_seq)
3159                 seq_server_set_cli(ls->ls_server_seq,
3160                                    NULL, NULL);
3161
3162         if (ls && ls->ls_control_exp) {
3163                 class_export_put(ls->ls_control_exp);
3164                 ls->ls_control_exp = NULL;
3165         }
3166         EXIT;
3167 }
3168
3169 /*
3170  * FLD wrappers
3171  */
3172 static int mdt_fld_fini(const struct lu_env *env,
3173                         struct mdt_device *m)
3174 {
3175         struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3176         ENTRY;
3177
3178         if (ls && ls->ls_server_fld) {
3179                 fld_server_fini(ls->ls_server_fld, env);
3180                 OBD_FREE_PTR(ls->ls_server_fld);
3181                 ls->ls_server_fld = NULL;
3182         }
3183
3184         RETURN(0);
3185 }
3186
3187 static int mdt_fld_init(const struct lu_env *env,
3188                         const char *uuid,
3189                         struct mdt_device *m)
3190 {
3191         struct lu_site *ls;
3192         int rc;
3193         ENTRY;
3194
3195         ls = m->mdt_md_dev.md_lu_dev.ld_site;
3196
3197         OBD_ALLOC_PTR(ls->ls_server_fld);
3198         if (ls->ls_server_fld == NULL)
3199                 RETURN(rc = -ENOMEM);
3200
3201         rc = fld_server_init(ls->ls_server_fld,
3202                              m->mdt_bottom, uuid, env);
3203         if (rc) {
3204                 OBD_FREE_PTR(ls->ls_server_fld);
3205                 ls->ls_server_fld = NULL;
3206                 RETURN(rc);
3207         }
3208
3209         RETURN(0);
3210 }
3211
3212 /* device init/fini methods */
3213 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3214 {
3215         ENTRY;
3216         if (m->mdt_regular_service != NULL) {
3217                 ptlrpc_unregister_service(m->mdt_regular_service);
3218                 m->mdt_regular_service = NULL;
3219         }
3220         if (m->mdt_readpage_service != NULL) {
3221                 ptlrpc_unregister_service(m->mdt_readpage_service);
3222                 m->mdt_readpage_service = NULL;
3223         }
3224         if (m->mdt_xmds_service != NULL) {
3225                 ptlrpc_unregister_service(m->mdt_xmds_service);
3226                 m->mdt_xmds_service = NULL;
3227         }
3228         if (m->mdt_setattr_service != NULL) {
3229                 ptlrpc_unregister_service(m->mdt_setattr_service);
3230                 m->mdt_setattr_service = NULL;
3231         }
3232         if (m->mdt_mdsc_service != NULL) {
3233                 ptlrpc_unregister_service(m->mdt_mdsc_service);
3234                 m->mdt_mdsc_service = NULL;
3235         }
3236         if (m->mdt_mdss_service != NULL) {
3237                 ptlrpc_unregister_service(m->mdt_mdss_service);
3238                 m->mdt_mdss_service = NULL;
3239         }
3240         if (m->mdt_dtss_service != NULL) {
3241                 ptlrpc_unregister_service(m->mdt_dtss_service);
3242                 m->mdt_dtss_service = NULL;
3243         }
3244         if (m->mdt_fld_service != NULL) {
3245                 ptlrpc_unregister_service(m->mdt_fld_service);
3246                 m->mdt_fld_service = NULL;
3247         }
3248         ENTRY;
3249 }
3250
3251 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3252 {
3253         int rc;
3254         static struct ptlrpc_service_conf conf;
3255         cfs_proc_dir_entry_t *procfs_entry;
3256         ENTRY;
3257
3258         procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3259
3260         conf = (typeof(conf)) {
3261                 .psc_nbufs            = MDS_NBUFS,
3262                 .psc_bufsize          = MDS_BUFSIZE,
3263                 .psc_max_req_size     = MDS_MAXREQSIZE,
3264                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3265                 .psc_req_portal       = MDS_REQUEST_PORTAL,
3266                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3267                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3268                 /*
3269                  * We'd like to have a mechanism to set this on a per-device
3270                  * basis, but alas...
3271                  */
3272                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3273                                        MDT_MAX_THREADS),
3274                 .psc_max_threads   = MDT_MAX_THREADS,
3275                 .psc_ctx_tags      = LCT_MD_THREAD
3276         };
3277
3278         m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3279         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3280                            "mdt_ldlm_client", m->mdt_ldlm_client);
3281
3282         m->mdt_regular_service =
3283                 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3284                                      procfs_entry, NULL, LUSTRE_MDT_NAME);
3285         if (m->mdt_regular_service == NULL)
3286                 RETURN(-ENOMEM);
3287
3288         rc = ptlrpc_start_threads(NULL, m->mdt_regular_service);
3289         if (rc)
3290                 GOTO(err_mdt_svc, rc);
3291
3292         /*
3293          * readpage service configuration. Parameters have to be adjusted,
3294          * ideally.
3295          */
3296         conf = (typeof(conf)) {
3297                 .psc_nbufs            = MDS_NBUFS,
3298                 .psc_bufsize          = MDS_BUFSIZE,
3299                 .psc_max_req_size     = MDS_MAXREQSIZE,
3300                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3301                 .psc_req_portal       = MDS_READPAGE_PORTAL,
3302                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3303                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3304                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3305                                        MDT_MAX_THREADS),
3306                 .psc_max_threads   = MDT_MAX_THREADS,
3307                 .psc_ctx_tags      = LCT_MD_THREAD
3308         };
3309         m->mdt_readpage_service =
3310                 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3311                                      LUSTRE_MDT_NAME "_readpage",
3312                                      procfs_entry, NULL, "mdt_rdpg");
3313
3314         if (m->mdt_readpage_service == NULL) {
3315                 CERROR("failed to start readpage service\n");
3316                 GOTO(err_mdt_svc, rc = -ENOMEM);
3317         }
3318
3319         rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service);
3320
3321         /*
3322          * setattr service configuration.
3323          */
3324         conf = (typeof(conf)) {
3325                 .psc_nbufs            = MDS_NBUFS,
3326                 .psc_bufsize          = MDS_BUFSIZE,
3327                 .psc_max_req_size     = MDS_MAXREQSIZE,
3328                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3329                 .psc_req_portal       = MDS_SETATTR_PORTAL,
3330                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3331                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3332                 .psc_min_threads   = min(max(mdt_num_threads, MDT_MIN_THREADS),
3333                                        MDT_MAX_THREADS),
3334                 .psc_max_threads   = MDT_MAX_THREADS,
3335                 .psc_ctx_tags      = LCT_MD_THREAD
3336         };
3337
3338         m->mdt_setattr_service =
3339                 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3340                                      LUSTRE_MDT_NAME "_setattr",
3341                                      procfs_entry, NULL, "mdt_attr");
3342
3343         if (!m->mdt_setattr_service) {
3344                 CERROR("failed to start setattr service\n");
3345                 GOTO(err_mdt_svc, rc = -ENOMEM);
3346         }
3347
3348         rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service);
3349         if (rc)
3350                 GOTO(err_mdt_svc, rc);
3351
3352         /*
3353          * sequence controller service configuration
3354          */
3355         conf = (typeof(conf)) {
3356                 .psc_nbufs = MDS_NBUFS,
3357                 .psc_bufsize = MDS_BUFSIZE,
3358                 .psc_max_req_size = SEQ_MAXREQSIZE,
3359                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3360                 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3361                 .psc_rep_portal = MDC_REPLY_PORTAL,
3362                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3363                 .psc_min_threads = SEQ_NUM_THREADS,
3364                 .psc_max_threads = SEQ_NUM_THREADS,
3365                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3366         };
3367
3368         m->mdt_mdsc_service =
3369                 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3370                                      LUSTRE_MDT_NAME"_mdsc",
3371                                      procfs_entry, NULL, "mdt_mdsc");
3372         if (!m->mdt_mdsc_service) {
3373                 CERROR("failed to start seq controller service\n");
3374                 GOTO(err_mdt_svc, rc = -ENOMEM);
3375         }
3376
3377         rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service);
3378         if (rc)
3379                 GOTO(err_mdt_svc, rc);
3380
3381         /*
3382          * metadata sequence server service configuration
3383          */
3384         conf = (typeof(conf)) {
3385                 .psc_nbufs = MDS_NBUFS,
3386                 .psc_bufsize = MDS_BUFSIZE,
3387                 .psc_max_req_size = SEQ_MAXREQSIZE,
3388                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3389                 .psc_req_portal = SEQ_METADATA_PORTAL,
3390                 .psc_rep_portal = MDC_REPLY_PORTAL,
3391                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3392                 .psc_min_threads = SEQ_NUM_THREADS,
3393                 .psc_max_threads = SEQ_NUM_THREADS,
3394                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3395         };
3396
3397         m->mdt_mdss_service =
3398                 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
3399                                      LUSTRE_MDT_NAME"_mdss",
3400                                      procfs_entry, NULL, "mdt_mdss");
3401         if (!m->mdt_mdss_service) {
3402                 CERROR("failed to start metadata seq server service\n");
3403                 GOTO(err_mdt_svc, rc = -ENOMEM);
3404         }
3405
3406         rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service);
3407         if (rc)
3408                 GOTO(err_mdt_svc, rc);
3409
3410
3411         /*
3412          * Data sequence server service configuration. We want to have really
3413          * cluster-wide sequences space. This is why we start only one sequence
3414          * controller which manages space.
3415          */
3416         conf = (typeof(conf)) {
3417                 .psc_nbufs = MDS_NBUFS,
3418                 .psc_bufsize = MDS_BUFSIZE,
3419                 .psc_max_req_size = SEQ_MAXREQSIZE,
3420                 .psc_max_reply_size = SEQ_MAXREPSIZE,
3421                 .psc_req_portal = SEQ_DATA_PORTAL,
3422                 .psc_rep_portal = OSC_REPLY_PORTAL,
3423                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3424                 .psc_min_threads = SEQ_NUM_THREADS,
3425                 .psc_max_threads = SEQ_NUM_THREADS,
3426                 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3427         };
3428
3429         m->mdt_dtss_service =
3430                 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
3431                                      LUSTRE_MDT_NAME"_dtss",
3432                                      procfs_entry, NULL, "mdt_dtss");
3433         if (!m->mdt_dtss_service) {
3434                 CERROR("failed to start data seq server service\n");
3435                 GOTO(err_mdt_svc, rc = -ENOMEM);
3436         }
3437
3438         rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service);
3439         if (rc)
3440                 GOTO(err_mdt_svc, rc);
3441
3442         /* FLD service start */
3443         conf = (typeof(conf)) {
3444                 .psc_nbufs            = MDS_NBUFS,
3445                 .psc_bufsize          = MDS_BUFSIZE,
3446                 .psc_max_req_size     = FLD_MAXREQSIZE,
3447                 .psc_max_reply_size   = FLD_MAXREPSIZE,
3448                 .psc_req_portal       = FLD_REQUEST_PORTAL,
3449                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3450                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3451                 .psc_min_threads      = FLD_NUM_THREADS,
3452                 .psc_max_threads      = FLD_NUM_THREADS,
3453                 .psc_ctx_tags         = LCT_DT_THREAD|LCT_MD_THREAD
3454         };
3455
3456         m->mdt_fld_service =
3457                 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
3458                                      LUSTRE_MDT_NAME"_fld",
3459                                      procfs_entry, NULL, "mdt_fld");
3460         if (!m->mdt_fld_service) {
3461                 CERROR("failed to start fld service\n");
3462                 GOTO(err_mdt_svc, rc = -ENOMEM);
3463         }
3464
3465         rc = ptlrpc_start_threads(NULL, m->mdt_fld_service);
3466         if (rc)
3467                 GOTO(err_mdt_svc, rc);
3468
3469         /*
3470          * mds-mds service configuration. Separate portal is used to allow
3471          * mds-mds requests be not blocked during recovery.
3472          */
3473         conf = (typeof(conf)) {
3474                 .psc_nbufs            = MDS_NBUFS,
3475                 .psc_bufsize          = MDS_BUFSIZE,
3476                 .psc_max_req_size     = MDS_MAXREQSIZE,
3477                 .psc_max_reply_size   = MDS_MAXREPSIZE,
3478                 .psc_req_portal       = MDS_MDS_PORTAL,
3479                 .psc_rep_portal       = MDC_REPLY_PORTAL,
3480                 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3481                 .psc_min_threads      = min(max(mdt_num_threads, MDT_MIN_THREADS),
3482                                             MDT_MAX_THREADS),
3483                 .psc_max_threads      = MDT_MAX_THREADS,
3484                 .psc_ctx_tags         = LCT_MD_THREAD
3485         };
3486         m->mdt_xmds_service = ptlrpc_init_svc_conf(&conf, mdt_xmds_handle,
3487                                                   LUSTRE_MDT_NAME "_mds",
3488                                                   procfs_entry, NULL, "mdt_xmds");
3489
3490         if (m->mdt_xmds_service == NULL) {
3491                 CERROR("failed to start readpage service\n");
3492                 GOTO(err_mdt_svc, rc = -ENOMEM);
3493         }
3494
3495         rc = ptlrpc_start_threads(NULL, m->mdt_xmds_service);
3496         if (rc)
3497                 GOTO(err_mdt_svc, rc);
3498
3499         EXIT;
3500 err_mdt_svc:
3501         if (rc)
3502                 mdt_stop_ptlrpc_service(m);
3503
3504         return rc;
3505 }
3506
3507 static void mdt_stack_fini(const struct lu_env *env,
3508                            struct mdt_device *m, struct lu_device *top)
3509 {
3510         struct obd_device       *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3511         struct lustre_cfg_bufs  *bufs;
3512         struct lustre_cfg       *lcfg;
3513         struct mdt_thread_info  *info;
3514         char flags[3]="";
3515         ENTRY;
3516
3517         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3518         LASSERT(info != NULL);
3519
3520         bufs = &info->mti_u.bufs;
3521         /* process cleanup, pass mdt obd name to get obd umount flags */
3522         lustre_cfg_bufs_reset(bufs, obd->obd_name);
3523         if (obd->obd_force)
3524                 strcat(flags, "F");
3525         if (obd->obd_fail)
3526                 strcat(flags, "A");
3527         lustre_cfg_bufs_set_string(bufs, 1, flags);
3528         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3529         if (!lcfg) {
3530                 CERROR("Cannot alloc lcfg!\n");
3531                 return;
3532         }
3533
3534         LASSERT(top);
3535         top->ld_ops->ldo_process_config(env, top, lcfg);
3536         lustre_cfg_free(lcfg);
3537
3538         lu_stack_fini(env, top);
3539         m->mdt_child = NULL;
3540         m->mdt_bottom = NULL;
3541 }
3542
3543 static struct lu_device *mdt_layer_setup(const struct lu_env *env,
3544                                          const char *typename,
3545                                          struct lu_device *child,
3546                                          struct lustre_cfg *cfg)
3547 {
3548         const char            *dev = lustre_cfg_string(cfg, 0);
3549         struct obd_type       *type;
3550         struct lu_device_type *ldt;
3551         struct lu_device      *d;
3552         int rc;
3553         ENTRY;
3554
3555         /* find the type */
3556         type = class_get_type(typename);
3557         if (!type) {
3558                 CERROR("Unknown type: '%s'\n", typename);
3559                 GOTO(out, rc = -ENODEV);
3560         }
3561
3562         rc = lu_context_refill(&env->le_ctx);
3563         if (rc != 0) {
3564                 CERROR("Failure to refill context: '%d'\n", rc);
3565                 GOTO(out_type, rc);
3566         }
3567
3568         if (env->le_ses != NULL) {
3569                 rc = lu_context_refill(env->le_ses);
3570                 if (rc != 0) {
3571                         CERROR("Failure to refill session: '%d'\n", rc);
3572                         GOTO(out_type, rc);
3573                 }
3574         }
3575
3576         ldt = type->typ_lu;
3577         if (ldt == NULL) {
3578                 CERROR("type: '%s'\n", typename);
3579                 GOTO(out_type, rc = -EINVAL);
3580         }
3581
3582         ldt->ldt_obd_type = type;
3583         d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
3584         if (IS_ERR(d)) {
3585                 CERROR("Cannot allocate device: '%s'\n", typename);
3586                 GOTO(out_type, rc = -ENODEV);
3587         }
3588
3589         LASSERT(child->ld_site);
3590         d->ld_site = child->ld_site;
3591
3592         type->typ_refcnt++;
3593         rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
3594         if (rc) {
3595                 CERROR("can't init device '%s', rc %d\n", typename, rc);
3596                 GOTO(out_alloc, rc);
3597         }
3598         lu_device_get(d);
3599
3600         RETURN(d);
3601
3602 out_alloc:
3603         ldt->ldt_ops->ldto_device_free(env, d);
3604         type->typ_refcnt--;
3605 out_type:
3606         class_put_type(type);
3607 out:
3608         return ERR_PTR(rc);
3609 }
3610
3611 static int mdt_stack_init(const struct lu_env *env,
3612                           struct mdt_device *m, struct lustre_cfg *cfg)
3613 {
3614         struct lu_device  *d = &m->mdt_md_dev.md_lu_dev;
3615         struct lu_device  *tmp;
3616         struct md_device  *md;
3617         int rc;
3618         ENTRY;
3619
3620         /* init the stack */
3621         tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
3622         if (IS_ERR(tmp)) {
3623                 RETURN(PTR_ERR(tmp));
3624         }
3625         m->mdt_bottom = lu2dt_dev(tmp);
3626         d = tmp;
3627         tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
3628         if (IS_ERR(tmp)) {
3629                 GOTO(out, rc = PTR_ERR(tmp));
3630         }
3631         d = tmp;
3632         md = lu2md_dev(d);
3633
3634         tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
3635         if (IS_ERR(tmp)) {
3636                 GOTO(out, rc = PTR_ERR(tmp));
3637         }
3638         d = tmp;
3639         /*set mdd upcall device*/
3640         md_upcall_dev_set(md, lu2md_dev(d));
3641
3642         md = lu2md_dev(d);
3643         /*set cmm upcall device*/
3644         md_upcall_dev_set(md, &m->mdt_md_dev);
3645
3646         m->mdt_child = lu2md_dev(d);
3647
3648         /* process setup config */
3649         tmp = &m->mdt_md_dev.md_lu_dev;
3650         rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
3651         GOTO(out, rc);
3652 out:
3653         /* fini from last known good lu_device */
3654         if (rc)
3655                 mdt_stack_fini(env, m, d);
3656
3657         return rc;
3658 }
3659
3660 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
3661 {
3662         struct md_device *next = m->mdt_child;
3663         struct lu_device *d    = &m->mdt_md_dev.md_lu_dev;
3664         struct lu_site   *ls   = d->ld_site;
3665         struct obd_device *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3666         ENTRY;
3667
3668         ping_evictor_stop();
3669
3670         target_recovery_fini(obd);
3671         mdt_stop_ptlrpc_service(m);
3672
3673         mdt_fs_cleanup(env, m);
3674
3675         upcall_cache_cleanup(m->mdt_identity_cache);
3676         m->mdt_identity_cache = NULL;
3677
3678         if (m->mdt_namespace != NULL) {
3679                 ldlm_namespace_free(m->mdt_namespace, d->ld_obd->obd_force);
3680                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
3681         }
3682
3683         mdt_seq_fini(env, m);
3684         mdt_seq_fini_cli(m);
3685         mdt_fld_fini(env, m);
3686         mdt_procfs_fini(m);
3687         ptlrpc_lprocfs_unregister_obd(d->ld_obd);
3688         lprocfs_obd_cleanup(d->ld_obd);
3689
3690         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
3691
3692         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
3693         del_timer(&m->mdt_ck_timer);
3694         mdt_ck_thread_stop(m);
3695
3696         /* finish the stack */
3697         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3698
3699         if (ls) {
3700                 lu_site_fini(ls);
3701                 OBD_FREE_PTR(ls);
3702                 d->ld_site = NULL;
3703         }
3704         LASSERT(atomic_read(&d->ld_ref) == 0);
3705         md_device_fini(&m->mdt_md_dev);
3706
3707         EXIT;
3708 }
3709
3710 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
3711 {
3712         char *p = options;
3713
3714 #ifdef CONFIG_FS_POSIX_ACL
3715         /* ACLs should be enabled by default (b=13829) */
3716         m->mdt_opts.mo_acl = 1;
3717         LCONSOLE_INFO("Enabling ACL\n");
3718 #else
3719         m->mdt_opts.mo_acl = 0;
3720         LCONSOLE_INFO("Disabling ACL\n");
3721 #endif
3722
3723         if (!options)
3724                 return;
3725
3726         while (*options) {
3727                 int len;
3728
3729                 while (*p && *p != ',')
3730                         p++;
3731
3732                 len = p - options;
3733                 if ((len == sizeof("user_xattr") - 1) &&
3734                     (memcmp(options, "user_xattr", len) == 0)) {
3735                         m->mdt_opts.mo_user_xattr = 1;
3736                         LCONSOLE_INFO("Enabling user_xattr\n");
3737                 } else if ((len == sizeof("nouser_xattr") - 1) &&
3738                            (memcmp(options, "nouser_xattr", len) == 0)) {
3739                         m->mdt_opts.mo_user_xattr = 0;
3740                         LCONSOLE_INFO("Disabling user_xattr\n");
3741                 } else if ((len == sizeof("noacl") - 1) &&
3742                            (memcmp(options, "noacl", len) == 0)) {
3743                         m->mdt_opts.mo_acl = 0;
3744                         LCONSOLE_INFO("Disabling ACL\n");
3745                 }
3746
3747                 options = ++p;
3748         }
3749 }
3750
3751 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
3752
3753 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
3754                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
3755 {
3756         struct lprocfs_static_vars lvars;
3757         struct mdt_thread_info    *info;
3758         struct obd_device         *obd;
3759         const char                *dev = lustre_cfg_string(cfg, 0);
3760         const char                *num = lustre_cfg_string(cfg, 2);
3761         struct lustre_mount_info  *lmi;
3762         struct lustre_sb_info     *lsi;
3763         struct lu_site            *s;
3764         const char                *identity_upcall = "NONE";
3765         int                        rc;
3766         ENTRY;
3767
3768         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3769         LASSERT(info != NULL);
3770
3771         obd = class_name2obd(dev);
3772         LASSERT(obd != NULL);
3773
3774         spin_lock_init(&m->mdt_transno_lock);
3775
3776         m->mdt_max_mdsize = MAX_MD_SIZE;
3777         m->mdt_max_cookiesize = sizeof(struct llog_cookie);
3778
3779         m->mdt_opts.mo_user_xattr = 0;
3780         m->mdt_opts.mo_acl = 0;
3781         lmi = server_get_mount_2(dev);
3782         if (lmi == NULL) {
3783                 CERROR("Cannot get mount info for %s!\n", dev);
3784                 RETURN(-EFAULT);
3785         } else {
3786                 lsi = s2lsi(lmi->lmi_sb);
3787                 fsoptions_to_mdt_flags(m, lsi->lsi_lmd->lmd_opts);
3788                 server_put_mount_2(dev, lmi->lmi_mnt);
3789         }
3790
3791         m->mdt_sptlrpc_lock = RW_LOCK_UNLOCKED;
3792         sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
3793
3794         spin_lock_init(&m->mdt_ioepoch_lock);
3795         m->mdt_opts.mo_compat_resname = 0;
3796         m->mdt_capa_timeout = CAPA_TIMEOUT;
3797         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
3798         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
3799
3800         spin_lock_init(&m->mdt_client_bitmap_lock);
3801
3802         OBD_ALLOC_PTR(s);
3803         if (s == NULL)
3804                 RETURN(-ENOMEM);
3805
3806         md_device_init(&m->mdt_md_dev, ldt);
3807         m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
3808         m->mdt_md_dev.md_lu_dev.ld_obd = obd;
3809         /* set this lu_device to obd, because error handling need it */
3810         obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
3811
3812         rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
3813         if (rc) {
3814                 CERROR("Can't init lu_site, rc %d\n", rc);
3815                 GOTO(err_free_site, rc);
3816         }
3817
3818         lprocfs_mdt_init_vars(&lvars);
3819         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
3820         if (rc) {
3821                 CERROR("Can't init lprocfs, rc %d\n", rc);
3822                 GOTO(err_fini_site, rc);
3823         }
3824         ptlrpc_lprocfs_register_obd(obd);
3825
3826         rc = mdt_procfs_init(m, dev);
3827         if (rc) {
3828                 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
3829                 GOTO(err_fini_proc, rc);
3830         }
3831
3832         /* set server index */
3833         LASSERT(num);
3834         s->ls_node_id = simple_strtol(num, NULL, 10);
3835
3836         /* failover is the default
3837          * FIXME: we do not failout mds0/mgs, which may cause some problems.
3838          * assumed whose ls_node_id == 0 XXX
3839          * */
3840         obd->obd_replayable = 1;
3841         /* No connection accepted until configurations will finish */
3842         obd->obd_no_conn = 1;
3843
3844         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
3845                 char *str = lustre_cfg_string(cfg, 4);
3846                 if (strchr(str, 'n')) {
3847                         CWARN("%s: recovery disabled\n", obd->obd_name);
3848                         obd->obd_replayable = 0;
3849                 }
3850         }
3851
3852         /* init the stack */
3853         rc = mdt_stack_init(env, m, cfg);
3854         if (rc) {
3855                 CERROR("Can't init device stack, rc %d\n", rc);
3856                 GOTO(err_fini_proc, rc);
3857         }
3858
3859         rc = mdt_fld_init(env, obd->obd_name, m);
3860         if (rc)
3861                 GOTO(err_fini_stack, rc);
3862
3863         rc = mdt_seq_init(env, obd->obd_name, m);
3864         if (rc)
3865                 GOTO(err_fini_fld, rc);
3866
3867         snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
3868                  LUSTRE_MDT_NAME"-%p", m);
3869         m->mdt_namespace = ldlm_namespace_new(info->mti_u.ns_name,
3870                                               LDLM_NAMESPACE_SERVER,
3871                                               LDLM_NAMESPACE_GREEDY);
3872         if (m->mdt_namespace == NULL)
3873                 GOTO(err_fini_seq, rc = -ENOMEM);
3874
3875         ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
3876         /* set obd_namespace for compatibility with old code */
3877         obd->obd_namespace = m->mdt_namespace;
3878
3879         /* XXX: to support suppgid for ACL, we enable identity_upcall
3880          * by default, otherwise, maybe got unexpected -EACCESS. */
3881         if (m->mdt_opts.mo_acl)
3882                 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
3883
3884         m->mdt_identity_cache = upcall_cache_init(obd->obd_name, identity_upcall,
3885                                                   &mdt_identity_upcall_cache_ops);
3886         if (IS_ERR(m->mdt_identity_cache)) {
3887                 rc = PTR_ERR(m->mdt_identity_cache);
3888                 m->mdt_identity_cache = NULL;
3889                 GOTO(err_free_ns, rc);
3890         }
3891
3892         m->mdt_ck_timer.function = mdt_ck_timer_callback;
3893         m->mdt_ck_timer.data = (unsigned long)m;
3894         init_timer(&m->mdt_ck_timer);
3895         rc = mdt_ck_thread_start(m);
3896         if (rc)
3897                 GOTO(err_free_ns, rc);
3898
3899         rc = mdt_fs_setup(env, m, obd);
3900         if (rc)
3901                 GOTO(err_capa, rc);
3902
3903         target_recovery_init(obd, mdt_recovery_handle);
3904
3905         rc = mdt_start_ptlrpc_service(m);
3906         if (rc)
3907                 GOTO(err_fs_cleanup, rc);
3908
3909         ping_evictor_start();
3910
3911         rc = lu_site_init_finish(s);
3912         if (rc)
3913                 GOTO(err_stop_service, rc);
3914
3915         if (obd->obd_recovering == 0)
3916                 mdt_postrecov(env, m);
3917
3918         mdt_init_capa_ctxt(env, m);
3919
3920         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
3921                 ldlm_timeout = 6;
3922
3923         RETURN(0);
3924
3925 err_stop_service:
3926         ping_evictor_stop();
3927         mdt_stop_ptlrpc_service(m);
3928 err_fs_cleanup:
3929         target_recovery_fini(obd);
3930         mdt_fs_cleanup(env, m);
3931 err_capa:
3932         del_timer(&m->mdt_ck_timer);
3933         mdt_ck_thread_stop(m);
3934 err_free_ns:
3935         upcall_cache_cleanup(m->mdt_identity_cache);
3936         m->mdt_identity_cache = NULL;
3937         ldlm_namespace_free(m->mdt_namespace, 0);
3938         obd->obd_namespace = m->mdt_namespace = NULL;
3939 err_fini_seq:
3940         mdt_seq_fini(env, m);
3941 err_fini_fld:
3942         mdt_fld_fini(env, m);
3943 err_fini_stack:
3944         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3945 err_fini_proc:
3946         mdt_procfs_fini(m);
3947         lprocfs_obd_cleanup(obd);
3948 err_fini_site:
3949         lu_site_fini(s);
3950 err_free_site:
3951         OBD_FREE_PTR(s);
3952
3953         md_device_fini(&m->mdt_md_dev);
3954         return (rc);
3955 }
3956
3957 /* used by MGS to process specific configurations */
3958 static int mdt_process_config(const struct lu_env *env,
3959                               struct lu_device *d, struct lustre_cfg *cfg)
3960 {
3961         struct mdt_device *m = mdt_dev(d);
3962         struct md_device *md_next = m->mdt_child;
3963         struct lu_device *next = md2lu_dev(md_next);
3964         int rc = 0;
3965         ENTRY;
3966
3967         switch (cfg->lcfg_command) {
3968         case LCFG_SPTLRPC_CONF: {
3969                 struct sptlrpc_conf_log *log;
3970                 struct sptlrpc_rule_set  tmp_rset;
3971
3972                 log = sptlrpc_conf_log_extract(cfg);
3973                 if (IS_ERR(log)) {
3974                         rc = PTR_ERR(log);
3975                         break;
3976                 }
3977
3978                 sptlrpc_rule_set_init(&tmp_rset);
3979
3980                 rc = sptlrpc_rule_set_from_log(&tmp_rset, log);
3981                 if (rc) {
3982                         CERROR("mdt %p: failed get sptlrpc rules: %d\n", m, rc);
3983                         break;
3984                 }
3985
3986                 write_lock(&m->mdt_sptlrpc_lock);
3987                 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
3988                 m->mdt_sptlrpc_rset = tmp_rset;
3989                 write_unlock(&m->mdt_sptlrpc_lock);
3990
3991                 sptlrpc_target_update_exp_flavor(
3992                                 md2lu_dev(&m->mdt_md_dev)->ld_obd, &tmp_rset);
3993
3994                 break;
3995         }
3996         case LCFG_PARAM: {
3997                 struct lprocfs_static_vars lvars;
3998                 struct obd_device *obd = d->ld_obd;
3999
4000                 lprocfs_mdt_init_vars(&lvars);
4001                 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars, cfg, obd);
4002                 if (rc)
4003                         /* others are passed further */
4004                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
4005                 break;
4006         }
4007         case LCFG_ADD_MDC:
4008                 /*
4009                  * Add mdc hook to get first MDT uuid and connect it to
4010                  * ls->controller to use for seq manager.
4011                  */
4012                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4013                 if (rc)
4014                         CERROR("Can't add mdc, rc %d\n", rc);
4015                 else
4016                         rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
4017                 break;
4018         default:
4019                 /* others are passed further */
4020                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4021                 break;
4022         }
4023         RETURN(rc);
4024 }
4025
4026 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4027                                           const struct lu_object_header *hdr,
4028                                           struct lu_device *d)
4029 {
4030         struct mdt_object *mo;
4031
4032         ENTRY;
4033
4034         OBD_ALLOC_PTR(mo);
4035         if (mo != NULL) {
4036                 struct lu_object *o;
4037                 struct lu_object_header *h;
4038
4039                 o = &mo->mot_obj.mo_lu;
4040                 h = &mo->mot_header;
4041                 lu_object_header_init(h);
4042                 lu_object_init(o, h, d);
4043                 lu_object_add_top(h, o);
4044                 o->lo_ops = &mdt_obj_ops;
4045                 RETURN(o);
4046         } else
4047                 RETURN(NULL);
4048 }
4049
4050 static int mdt_object_init(const struct lu_env *env, struct lu_object *o)
4051 {
4052         struct mdt_device *d = mdt_dev(o->lo_dev);
4053         struct lu_device  *under;
4054         struct lu_object  *below;
4055         int                rc = 0;
4056         ENTRY;
4057
4058         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4059                PFID(lu_object_fid(o)));
4060
4061         under = &d->mdt_child->md_lu_dev;
4062         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4063         if (below != NULL) {
4064                 lu_object_add(o, below);
4065         } else
4066                 rc = -ENOMEM;
4067
4068         RETURN(rc);
4069 }
4070
4071 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4072 {
4073         struct mdt_object *mo = mdt_obj(o);
4074         struct lu_object_header *h;
4075         ENTRY;
4076
4077         h = o->lo_header;
4078         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4079                PFID(lu_object_fid(o)));
4080
4081         lu_object_fini(o);
4082         lu_object_header_fini(h);
4083         OBD_FREE_PTR(mo);
4084         EXIT;
4085 }
4086
4087 static int mdt_object_print(const struct lu_env *env, void *cookie,
4088                             lu_printer_t p, const struct lu_object *o)
4089 {
4090         return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p", o);
4091 }
4092
4093 static struct lu_device_operations mdt_lu_ops = {
4094         .ldo_object_alloc   = mdt_object_alloc,
4095         .ldo_process_config = mdt_process_config
4096 };
4097
4098 static struct lu_object_operations mdt_obj_ops = {
4099         .loo_object_init    = mdt_object_init,
4100         .loo_object_free    = mdt_object_free,
4101         .loo_object_print   = mdt_object_print
4102 };
4103
4104 /* mds_connect_internal */
4105 static int mdt_connect_internal(struct obd_export *exp,
4106                                 struct mdt_device *mdt,
4107                                 struct obd_connect_data *data)
4108 {
4109         __u64 flags;
4110
4111         if (data != NULL) {
4112                 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4113                 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4114
4115                 /* If no known bits (which should not happen, probably,
4116                    as everybody should support LOOKUP and UPDATE bits at least)
4117                    revert to compat mode with plain locks. */
4118                 if (!data->ocd_ibits_known &&
4119                     data->ocd_connect_flags & OBD_CONNECT_IBITS)
4120                         data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4121
4122                 if (!mdt->mdt_opts.mo_acl)
4123                         data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4124
4125                 if (!mdt->mdt_opts.mo_user_xattr)
4126                         data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4127
4128                 if (!mdt->mdt_opts.mo_mds_capa)
4129                         data->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
4130
4131                 if (!mdt->mdt_opts.mo_oss_capa)
4132                         data->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
4133
4134                 spin_lock(&exp->exp_lock);
4135                 exp->exp_connect_flags = data->ocd_connect_flags;
4136                 spin_unlock(&exp->exp_lock);
4137                 data->ocd_version = LUSTRE_VERSION_CODE;
4138                 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
4139         }
4140
4141 #if 0
4142         if (mdt->mdt_opts.mo_acl &&
4143             ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
4144                 CWARN("%s: MDS requires ACL support but client does not\n",
4145                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4146                 return -EBADE;
4147         }
4148 #endif
4149
4150         flags = OBD_CONNECT_LCL_CLIENT | OBD_CONNECT_RMT_CLIENT;
4151         if ((exp->exp_connect_flags & flags) == flags) {
4152                 CWARN("%s: both local and remote client flags are set\n",
4153                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4154                 return -EBADE;
4155         }
4156
4157         if (mdt->mdt_opts.mo_mds_capa &&
4158             ((exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) == 0)) {
4159                 CWARN("%s: MDS requires capability support, but client not\n",
4160                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4161                 return -EBADE;
4162         }
4163
4164         if (mdt->mdt_opts.mo_oss_capa &&
4165             ((exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA) == 0)) {
4166                 CWARN("%s: MDS requires OSS capability support, "
4167                       "but client not\n",
4168                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4169                 return -EBADE;
4170         }
4171
4172         if ((exp->exp_connect_flags & OBD_CONNECT_FID) == 0) {
4173                 CWARN("%s: MDS requires FID support, but client not\n",
4174                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4175                 return -EBADE;
4176         }
4177
4178         return 0;
4179 }
4180
4181 /* mds_connect copy */
4182 static int mdt_obd_connect(const struct lu_env *env,
4183                            struct lustre_handle *conn, struct obd_device *obd,
4184                            struct obd_uuid *cluuid,
4185                            struct obd_connect_data *data)
4186 {
4187         struct mdt_thread_info *info;
4188         struct mdt_client_data *mcd;
4189         struct obd_export      *exp;
4190         struct mdt_device      *mdt;
4191         struct ptlrpc_request  *req;
4192         int                     rc;
4193         ENTRY;
4194
4195         LASSERT(env != NULL);
4196         if (!conn || !obd || !cluuid)
4197                 RETURN(-EINVAL);
4198
4199         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4200         req = info->mti_pill->rc_req;
4201         mdt = mdt_dev(obd->obd_lu_dev);
4202
4203         rc = class_connect(conn, obd, cluuid);
4204         if (rc)
4205                 RETURN(rc);
4206
4207         exp = class_conn2export(conn);
4208         LASSERT(exp != NULL);
4209
4210         CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
4211
4212         spin_lock(&exp->exp_lock);
4213         exp->exp_sp_peer = req->rq_sp_from;
4214
4215         read_lock(&mdt->mdt_sptlrpc_lock);
4216         sptlrpc_rule_set_choose(&mdt->mdt_sptlrpc_rset, exp->exp_sp_peer,
4217                                 req->rq_peer.nid, &exp->exp_flvr);
4218         read_unlock(&mdt->mdt_sptlrpc_lock);
4219
4220         if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
4221                 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
4222                        req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
4223                        libcfs_nid2str(req->rq_peer.nid));
4224                 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
4225                 spin_unlock(&exp->exp_lock);
4226                 RETURN(-EACCES);
4227         }
4228         spin_unlock(&exp->exp_lock);
4229
4230         rc = mdt_connect_internal(exp, mdt, data);
4231         if (rc == 0) {
4232                 OBD_ALLOC_PTR(mcd);
4233                 if (mcd != NULL) {
4234                         struct mdt_thread_info *mti;
4235                         mti = lu_context_key_get(&env->le_ctx,
4236                                                  &mdt_thread_key);
4237                         LASSERT(mti != NULL);
4238                         mti->mti_exp = exp;
4239                         memcpy(mcd->mcd_uuid, cluuid, sizeof mcd->mcd_uuid);
4240                         exp->exp_mdt_data.med_mcd = mcd;
4241                         rc = mdt_client_new(env, mdt);
4242                         if (rc != 0) {
4243                                 OBD_FREE_PTR(mcd);
4244                                 exp->exp_mdt_data.med_mcd = NULL;
4245                         }
4246                 } else
4247                         rc = -ENOMEM;
4248         }
4249
4250         if (rc != 0)
4251                 class_disconnect(exp);
4252         else
4253                 class_export_put(exp);
4254
4255         RETURN(rc);
4256 }
4257
4258 static int mdt_obd_reconnect(const struct lu_env *env,
4259                              struct obd_export *exp, struct obd_device *obd,
4260                              struct obd_uuid *cluuid,
4261                              struct obd_connect_data *data)
4262 {
4263         struct mdt_thread_info *info;
4264         struct mdt_device      *mdt;
4265         struct ptlrpc_request  *req;
4266         int                     rc;
4267         ENTRY;
4268
4269         if (exp == NULL || obd == NULL || cluuid == NULL)
4270                 RETURN(-EINVAL);
4271
4272         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4273         req = info->mti_pill->rc_req;
4274         mdt = mdt_dev(obd->obd_lu_dev);
4275
4276         CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
4277
4278         spin_lock(&exp->exp_lock);
4279         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
4280                 exp->exp_sp_peer = req->rq_sp_from;
4281
4282                 read_lock(&mdt->mdt_sptlrpc_lock);
4283                 sptlrpc_rule_set_choose(&mdt->mdt_sptlrpc_rset,
4284                                         exp->exp_sp_peer,
4285                                         req->rq_peer.nid, &exp->exp_flvr);
4286                 read_unlock(&mdt->mdt_sptlrpc_lock);
4287
4288                 if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
4289                         CERROR("invalid rpc flavor %x, expect %x, from %s\n",
4290                                req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
4291                                libcfs_nid2str(req->rq_peer.nid));
4292                         exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
4293                         spin_unlock(&exp->exp_lock);
4294                         RETURN(-EACCES);
4295                 }
4296         }
4297         spin_unlock(&exp->exp_lock);
4298
4299         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
4300
4301         RETURN(rc);
4302 }
4303
4304 static int mdt_obd_disconnect(struct obd_export *exp)
4305 {
4306         struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
4307         int rc;
4308         ENTRY;
4309
4310         LASSERT(exp);
4311         class_export_get(exp);
4312
4313         /* Disconnect early so that clients can't keep using export */
4314         rc = class_disconnect(exp);
4315         if (mdt->mdt_namespace != NULL || exp->exp_obd->obd_namespace != NULL)
4316                 ldlm_cancel_locks_for_export(exp);
4317
4318         /* complete all outstanding replies */
4319         spin_lock(&exp->exp_lock);
4320         while (!list_empty(&exp->exp_outstanding_replies)) {
4321                 struct ptlrpc_reply_state *rs =
4322                         list_entry(exp->exp_outstanding_replies.next,
4323                                    struct ptlrpc_reply_state, rs_exp_list);
4324                 struct ptlrpc_service *svc = rs->rs_service;
4325
4326                 spin_lock(&svc->srv_lock);
4327                 list_del_init(&rs->rs_exp_list);
4328                 ptlrpc_schedule_difficult_reply(rs);
4329                 spin_unlock(&svc->srv_lock);
4330         }
4331         spin_unlock(&exp->exp_lock);
4332
4333         class_export_put(exp);
4334         RETURN(rc);
4335 }
4336
4337 /* FIXME: Can we avoid using these two interfaces? */
4338 static int mdt_init_export(struct obd_export *exp)
4339 {
4340         struct mdt_export_data *med = &exp->exp_mdt_data;
4341         ENTRY;
4342
4343         INIT_LIST_HEAD(&med->med_open_head);
4344         spin_lock_init(&med->med_open_lock);
4345         sema_init(&med->med_idmap_sem, 1);
4346         med->med_idmap = NULL;
4347         spin_lock(&exp->exp_lock);
4348         exp->exp_connecting = 1;
4349         spin_unlock(&exp->exp_lock);
4350         RETURN(0);
4351 }
4352
4353 static int mdt_destroy_export(struct obd_export *export)
4354 {
4355         struct mdt_export_data *med;
4356         struct obd_device      *obd = export->exp_obd;
4357         struct mdt_device      *mdt;
4358         struct mdt_thread_info *info;
4359         struct lu_env           env;
4360         struct md_attr         *ma;
4361         int lmm_size;
4362         int cookie_size;
4363         int rc = 0;
4364         ENTRY;
4365
4366         med = &export->exp_mdt_data;
4367         if (med->med_rmtclient)
4368                 mdt_cleanup_idmap(med);
4369
4370         target_destroy_export(export);
4371
4372         if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
4373                 RETURN(0);
4374
4375         mdt = mdt_dev(obd->obd_lu_dev);
4376         LASSERT(mdt != NULL);
4377
4378         rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4379         if (rc)
4380                 RETURN(rc);
4381
4382         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
4383         LASSERT(info != NULL);
4384         memset(info, 0, sizeof *info);
4385         info->mti_env = &env;
4386         info->mti_mdt = mdt;
4387         info->mti_exp = export;
4388
4389         ma = &info->mti_attr;
4390         lmm_size = ma->ma_lmm_size = mdt->mdt_max_mdsize;
4391         cookie_size = ma->ma_cookie_size = mdt->mdt_max_cookiesize;
4392         OBD_ALLOC(ma->ma_lmm, lmm_size);
4393         OBD_ALLOC(ma->ma_cookie, cookie_size);
4394
4395         if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
4396                 GOTO(out, rc = -ENOMEM);
4397         ma->ma_need = MA_LOV | MA_COOKIE;
4398         ma->ma_valid = 0;
4399         /* Close any open files (which may also cause orphan unlinking). */
4400         spin_lock(&med->med_open_lock);
4401         while (!list_empty(&med->med_open_head)) {
4402                 struct list_head *tmp = med->med_open_head.next;
4403                 struct mdt_file_data *mfd =
4404                         list_entry(tmp, struct mdt_file_data, mfd_list);
4405
4406                 /* Remove mfd handle so it can't be found again.
4407                  * We are consuming the mfd_list reference here. */
4408                 class_handle_unhash(&mfd->mfd_handle);
4409                 list_del_init(&mfd->mfd_list);
4410                 spin_unlock(&med->med_open_lock);
4411                 mdt_mfd_close(info, mfd);
4412                 /* TODO: if we close the unlinked file,
4413                  * we need to remove it's objects from OST */
4414                 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
4415                 spin_lock(&med->med_open_lock);
4416                 ma->ma_lmm_size = lmm_size;
4417                 ma->ma_cookie_size = cookie_size;
4418                 ma->ma_need = MA_LOV | MA_COOKIE;
4419                 ma->ma_valid = 0;
4420         }
4421         spin_unlock(&med->med_open_lock);
4422         info->mti_mdt = NULL;
4423         mdt_client_del(&env, mdt);
4424
4425         EXIT;
4426 out:
4427         if (lmm_size) {
4428                 OBD_FREE(ma->ma_lmm, lmm_size);
4429                 ma->ma_lmm = NULL;
4430         }
4431         if (cookie_size) {
4432                 OBD_FREE(ma->ma_cookie, cookie_size);
4433                 ma->ma_cookie = NULL;
4434         }
4435         lu_env_fini(&env);
4436
4437         return rc;
4438 }
4439
4440 static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
4441 {
4442         if (flag & CONFIG_LOG)
4443                 m->mdt_fl_cfglog = 1;
4444         if (flag & CONFIG_SYNC)
4445                 m->mdt_fl_synced = 1;
4446
4447         if (m->mdt_fl_cfglog /* bz11778: && m->mdt_fl_synced */)
4448                 /* Open for clients */
4449                 m->mdt_md_dev.md_lu_dev.ld_obd->obd_no_conn = 0;
4450 }
4451
4452 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
4453                       enum md_upcall_event ev)
4454 {
4455         struct mdt_device *m = mdt_dev(&md->md_lu_dev);
4456         struct md_device  *next  = m->mdt_child;
4457         struct mdt_thread_info *mti;
4458         int rc = 0;
4459         ENTRY;
4460
4461         switch (ev) {
4462                 case MD_LOV_SYNC:
4463                         rc = next->md_ops->mdo_maxsize_get(env, next,
4464                                         &m->mdt_max_mdsize,
4465                                         &m->mdt_max_cookiesize);
4466                         CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
4467                                      m->mdt_max_mdsize, m->mdt_max_cookiesize);
4468                         mdt_allow_cli(m, CONFIG_SYNC);
4469                         break;
4470                 case MD_NO_TRANS:
4471                         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4472                         mti->mti_no_need_trans = 1;
4473                         CDEBUG(D_INFO, "disable mdt trans for this thread\n");
4474                         break;
4475                 case MD_LOV_CONFIG:
4476                         /* Check that MDT is not yet configured */
4477                         LASSERT(!m->mdt_fl_cfglog);
4478                         break;
4479                 default:
4480                         CERROR("invalid event\n");
4481                         rc = -EINVAL;
4482                         break;
4483         }
4484         RETURN(rc);
4485 }
4486
4487 static int mdt_obd_notify(struct obd_device *host,
4488                           struct obd_device *watched,
4489                           enum obd_notify_event ev, void *data)
4490 {
4491         ENTRY;
4492
4493         switch (ev) {
4494         case OBD_NOTIFY_CONFIG:
4495                 mdt_allow_cli(mdt_dev(host->obd_lu_dev), (unsigned int)data);
4496                 break;
4497         default:
4498                 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
4499         }
4500         RETURN(0);
4501 }
4502
4503 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
4504                          void *karg, void *uarg)
4505 {
4506         struct lu_env      env;
4507         struct obd_device *obd= exp->exp_obd;
4508         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
4509         struct dt_device  *dt = mdt->mdt_bottom;
4510         int rc;
4511
4512         ENTRY;
4513         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
4514         rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4515         if (rc)
4516                 RETURN(rc);
4517
4518         switch (cmd) {
4519         case OBD_IOC_SYNC:
4520                 rc = dt->dd_ops->dt_sync(&env, dt);
4521                 break;
4522         case OBD_IOC_SET_READONLY:
4523                 rc = dt->dd_ops->dt_sync(&env, dt);
4524                 dt->dd_ops->dt_ro(&env, dt);
4525                 break;
4526         case OBD_IOC_ABORT_RECOVERY:
4527                 CERROR("Aborting recovery for device %s\n", obd->obd_name);
4528                 target_stop_recovery_thread(obd);
4529                 rc = 0;
4530                 break;
4531         default:
4532                 CERROR("Not supported cmd = %d for device %s\n",
4533                        cmd, obd->obd_name);
4534                 rc = -EOPNOTSUPP;
4535         }
4536
4537         lu_env_fini(&env);
4538         RETURN(rc);
4539 }
4540
4541 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
4542 {
4543         struct lu_device *ld = md2lu_dev(mdt->mdt_child);
4544         struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
4545         int rc, lost;
4546         ENTRY;
4547         /* if some clients didn't participate in recovery then we can possibly
4548          * lost sequence. Now we should increase sequence for safe value */
4549         lost = obd->obd_max_recoverable_clients - obd->obd_connected_clients;
4550         mdt_seq_adjust(env, mdt, lost);
4551
4552         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
4553         RETURN(rc);
4554 }
4555
4556 int mdt_obd_postrecov(struct obd_device *obd)
4557 {
4558         struct lu_env env;
4559         int rc;
4560
4561         rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4562         if (rc)
4563                 RETURN(rc);
4564         rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
4565         lu_env_fini(&env);
4566         return rc;
4567 }
4568
4569 static struct obd_ops mdt_obd_device_ops = {
4570         .o_owner          = THIS_MODULE,
4571         .o_connect        = mdt_obd_connect,
4572         .o_reconnect      = mdt_obd_reconnect,
4573         .o_disconnect     = mdt_obd_disconnect,
4574         .o_init_export    = mdt_init_export,
4575         .o_destroy_export = mdt_destroy_export,
4576         .o_iocontrol      = mdt_iocontrol,
4577         .o_postrecov      = mdt_obd_postrecov,
4578         .o_notify         = mdt_obd_notify
4579 };
4580
4581 static struct lu_device* mdt_device_fini(const struct lu_env *env,
4582                                          struct lu_device *d)
4583 {
4584         struct mdt_device *m = mdt_dev(d);
4585         ENTRY;
4586
4587         mdt_fini(env, m);
4588         RETURN(NULL);
4589 }
4590
4591 static struct lu_device *mdt_device_free(const struct lu_env *env,
4592                                          struct lu_device *d)
4593 {
4594         struct mdt_device *m = mdt_dev(d);
4595         ENTRY;
4596
4597         OBD_FREE_PTR(m);
4598         RETURN(NULL);
4599 }
4600
4601 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
4602                                           struct lu_device_type *t,
4603                                           struct lustre_cfg *cfg)
4604 {
4605         struct lu_device  *l;
4606         struct mdt_device *m;
4607
4608         OBD_ALLOC_PTR(m);
4609         if (m != NULL) {
4610                 int rc;
4611
4612                 l = &m->mdt_md_dev.md_lu_dev;
4613                 rc = mdt_init0(env, m, t, cfg);
4614                 if (rc != 0) {
4615                         OBD_FREE_PTR(m);
4616                         l = ERR_PTR(rc);
4617                         return l;
4618                 }
4619                 md_upcall_init(&m->mdt_md_dev, mdt_upcall);
4620         } else
4621                 l = ERR_PTR(-ENOMEM);
4622         return l;
4623 }
4624
4625 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
4626 LU_KEY_INIT_FINI(mdt, struct mdt_thread_info);
4627
4628 /* context key: mdt_thread_key */
4629 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
4630
4631 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
4632 LU_KEY_INIT_FINI(mdt_txn, struct mdt_txn_info);
4633
4634 struct lu_context_key mdt_txn_key = {
4635         .lct_tags = LCT_TX_HANDLE,
4636         .lct_init = mdt_txn_key_init,
4637         .lct_fini = mdt_txn_key_fini
4638 };
4639
4640 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
4641 {
4642         return md_ucred(info->mti_env);
4643 }
4644
4645 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
4646 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key, &mdt_txn_key);
4647
4648 static struct lu_device_type_operations mdt_device_type_ops = {
4649         .ldto_init = mdt_type_init,
4650         .ldto_fini = mdt_type_fini,
4651
4652         .ldto_device_alloc = mdt_device_alloc,
4653         .ldto_device_free  = mdt_device_free,
4654         .ldto_device_fini  = mdt_device_fini
4655 };
4656
4657 static struct lu_device_type mdt_device_type = {
4658         .ldt_tags     = LU_DEVICE_MD,
4659         .ldt_name     = LUSTRE_MDT_NAME,
4660         .ldt_ops      = &mdt_device_type_ops,
4661         .ldt_ctx_tags = LCT_MD_THREAD
4662 };
4663
4664 static int __init mdt_mod_init(void)
4665 {
4666         struct lprocfs_static_vars lvars;
4667         int rc;
4668
4669         mdt_num_threads = MDT_NUM_THREADS;
4670         lprocfs_mdt_init_vars(&lvars);
4671         rc = class_register_type(&mdt_obd_device_ops, NULL,
4672                                  lvars.module_vars, LUSTRE_MDT_NAME,
4673                                  &mdt_device_type);
4674
4675         return rc;
4676 }
4677
4678 static void __exit mdt_mod_exit(void)
4679 {
4680         class_unregister_type(LUSTRE_MDT_NAME);
4681 }
4682
4683
4684 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt)             \
4685 [prefix ## _ ## opc - prefix ## _ ## base] = {                          \
4686         .mh_name    = #opc,                                             \
4687         .mh_fail_id = OBD_FAIL_ ## prefix ## _  ## opc ## suffix,       \
4688         .mh_opc     = prefix ## _  ## opc,                              \
4689         .mh_flags   = flags,                                            \
4690         .mh_act     = fn,                                               \
4691         .mh_fmt     = fmt                                               \
4692 }
4693
4694 #define DEF_MDT_HNDL(flags, name, fn, fmt)                                  \
4695         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
4696
4697 #define DEF_SEQ_HNDL(flags, name, fn, fmt)                      \
4698         DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
4699
4700 #define DEF_FLD_HNDL(flags, name, fn, fmt)                      \
4701         DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
4702 /*
4703  * Request with a format known in advance
4704  */
4705 #define DEF_MDT_HNDL_F(flags, name, fn)                                 \
4706         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
4707
4708 #define DEF_SEQ_HNDL_F(flags, name, fn)                                 \
4709         DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
4710
4711 #define DEF_FLD_HNDL_F(flags, name, fn)                                 \
4712         DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
4713 /*
4714  * Request with a format we do not yet know
4715  */
4716 #define DEF_MDT_HNDL_0(flags, name, fn)                                 \
4717         DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
4718
4719 static struct mdt_handler mdt_mds_ops[] = {
4720 DEF_MDT_HNDL_F(0,                         CONNECT,      mdt_connect),
4721 DEF_MDT_HNDL_F(0,                         DISCONNECT,   mdt_disconnect),
4722 DEF_MDT_HNDL_F(0,                         SET_INFO,     mdt_set_info),
4723 DEF_MDT_HNDL_F(0           |HABEO_REFERO, GETSTATUS,    mdt_getstatus),
4724 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETATTR,      mdt_getattr),
4725 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
4726 DEF_MDT_HNDL_F(HABEO_CORPUS,              GETXATTR,     mdt_getxattr),
4727 DEF_MDT_HNDL_F(0           |HABEO_REFERO, STATFS,       mdt_statfs),
4728 DEF_MDT_HNDL_F(0           |MUTABOR,      REINT,        mdt_reint),
4729 DEF_MDT_HNDL_F(HABEO_CORPUS,              CLOSE,        mdt_close),
4730 DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING, mdt_done_writing),
4731 DEF_MDT_HNDL_F(0           |HABEO_REFERO, PIN,          mdt_pin),
4732 DEF_MDT_HNDL_0(0,                         SYNC,         mdt_sync),
4733 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
4734 DEF_MDT_HNDL_F(0,                         QUOTACHECK,   mdt_quotacheck_handle),
4735 DEF_MDT_HNDL_F(0,                         QUOTACTL,     mdt_quotactl_handle)
4736 };
4737
4738 #define DEF_OBD_HNDL(flags, name, fn)                   \
4739         DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
4740
4741
4742 static struct mdt_handler mdt_obd_ops[] = {
4743         DEF_OBD_HNDL(0, PING,           mdt_obd_ping),
4744         DEF_OBD_HNDL(0, LOG_CANCEL,     mdt_obd_log_cancel),
4745         DEF_OBD_HNDL(0, QC_CALLBACK,    mdt_obd_qc_callback)
4746 };
4747
4748 #define DEF_DLM_HNDL_0(flags, name, fn)                   \
4749         DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
4750 #define DEF_DLM_HNDL_F(flags, name, fn)                   \
4751         DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
4752
4753 static struct mdt_handler mdt_dlm_ops[] = {
4754         DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE,        mdt_enqueue),
4755         DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT,        mdt_convert),
4756         DEF_DLM_HNDL_0(0,            BL_CALLBACK,    mdt_bl_callback),
4757         DEF_DLM_HNDL_0(0,            CP_CALLBACK,    mdt_cp_callback)
4758 };
4759
4760 static struct mdt_handler mdt_llog_ops[] = {
4761 };
4762
4763 #define DEF_SEC_CTX_HNDL(name, fn)                      \
4764         DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
4765
4766 static struct mdt_handler mdt_sec_ctx_ops[] = {
4767         DEF_SEC_CTX_HNDL(INIT,          mdt_sec_ctx_handle),
4768         DEF_SEC_CTX_HNDL(INIT_CONT,     mdt_sec_ctx_handle),
4769         DEF_SEC_CTX_HNDL(FINI,          mdt_sec_ctx_handle)
4770 };
4771
4772 static struct mdt_opc_slice mdt_regular_handlers[] = {
4773         {
4774                 .mos_opc_start = MDS_GETATTR,
4775                 .mos_opc_end   = MDS_LAST_OPC,
4776                 .mos_hs        = mdt_mds_ops
4777         },
4778         {
4779                 .mos_opc_start = OBD_PING,
4780                 .mos_opc_end   = OBD_LAST_OPC,
4781                 .mos_hs        = mdt_obd_ops
4782         },
4783         {
4784                 .mos_opc_start = LDLM_ENQUEUE,
4785                 .mos_opc_end   = LDLM_LAST_OPC,
4786                 .mos_hs        = mdt_dlm_ops
4787         },
4788         {
4789                 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
4790                 .mos_opc_end   = LLOG_LAST_OPC,
4791                 .mos_hs        = mdt_llog_ops
4792         },
4793         {
4794                 .mos_opc_start = SEC_CTX_INIT,
4795                 .mos_opc_end   = SEC_LAST_OPC,
4796                 .mos_hs        = mdt_sec_ctx_ops
4797         },
4798         {
4799                 .mos_hs        = NULL
4800         }
4801 };
4802
4803 static struct mdt_handler mdt_readpage_ops[] = {
4804         DEF_MDT_HNDL_F(0,                         CONNECT,  mdt_connect),
4805         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
4806 #ifdef HAVE_SPLIT_SUPPORT
4807         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
4808 #endif
4809
4810         /*
4811          * XXX: this is ugly and should be fixed one day, see mdc_close() for
4812          * detailed comments. --umka
4813          */
4814         DEF_MDT_HNDL_F(HABEO_CORPUS,              CLOSE,    mdt_close),
4815         DEF_MDT_HNDL_F(HABEO_CORPUS,              DONE_WRITING,    mdt_done_writing),
4816 };
4817
4818 static struct mdt_opc_slice mdt_readpage_handlers[] = {
4819         {
4820                 .mos_opc_start = MDS_GETATTR,
4821                 .mos_opc_end   = MDS_LAST_OPC,
4822                 .mos_hs        = mdt_readpage_ops
4823         },
4824         {
4825                 .mos_hs        = NULL
4826         }
4827 };
4828
4829 static struct mdt_handler mdt_xmds_ops[] = {
4830         DEF_MDT_HNDL_F(0,                         CONNECT,      mdt_connect),
4831         DEF_MDT_HNDL_F(HABEO_CORPUS             , GETATTR,      mdt_getattr),
4832         DEF_MDT_HNDL_F(0 | MUTABOR              , REINT,        mdt_reint),
4833         DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR,    mdt_is_subdir),
4834 };
4835
4836 static struct mdt_opc_slice mdt_xmds_handlers[] = {
4837         {
4838                 .mos_opc_start = MDS_GETATTR,
4839                 .mos_opc_end   = MDS_LAST_OPC,
4840                 .mos_hs        = mdt_xmds_ops
4841         },
4842         {
4843                 .mos_opc_start = OBD_PING,
4844                 .mos_opc_end   = OBD_LAST_OPC,
4845                 .mos_hs        = mdt_obd_ops
4846         },
4847         {
4848                 .mos_opc_start = SEC_CTX_INIT,
4849                 .mos_opc_end   = SEC_LAST_OPC,
4850                 .mos_hs        = mdt_sec_ctx_ops
4851         },
4852         {
4853                 .mos_hs        = NULL
4854         }
4855 };
4856
4857 static struct mdt_handler mdt_seq_ops[] = {
4858         DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
4859 };
4860
4861 static struct mdt_opc_slice mdt_seq_handlers[] = {
4862         {
4863                 .mos_opc_start = SEQ_QUERY,
4864                 .mos_opc_end   = SEQ_LAST_OPC,
4865                 .mos_hs        = mdt_seq_ops
4866         },
4867         {
4868                 .mos_hs        = NULL
4869         }
4870 };
4871
4872 static struct mdt_handler mdt_fld_ops[] = {
4873         DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
4874 };
4875
4876 static struct mdt_opc_slice mdt_fld_handlers[] = {
4877         {
4878                 .mos_opc_start = FLD_QUERY,
4879                 .mos_opc_end   = FLD_LAST_OPC,
4880                 .mos_hs        = mdt_fld_ops
4881         },
4882         {
4883                 .mos_hs        = NULL
4884         }
4885 };
4886
4887 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
4888 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
4889 MODULE_LICENSE("GPL");
4890
4891 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
4892                 "number of mdt service threads to start");
4893
4894 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);