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