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