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