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