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