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