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