Whamcloud - gitweb
30347c9e90f077ba38373d39a25431d32b40174f
[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 /**
1504  * Top-level handler for MDT connection requests.
1505  */
1506 static int mdt_connect(struct mdt_thread_info *info)
1507 {
1508         int rc;
1509         struct obd_connect_data *reply;
1510         struct obd_export *exp;
1511         struct ptlrpc_request *req = mdt_info_req(info);
1512
1513         rc = target_handle_connect(req);
1514         if (rc != 0)
1515                 return err_serious(rc);
1516
1517         LASSERT(req->rq_export != NULL);
1518         info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1519         rc = mdt_init_sec_level(info);
1520         if (rc != 0) {
1521                 obd_disconnect(class_export_get(req->rq_export));
1522                 return rc;
1523         }
1524
1525         /* To avoid exposing partially initialized connection flags, changes up
1526          * to this point have been staged in reply->ocd_connect_flags. Now that
1527          * connection handling has completed successfully, atomically update
1528          * the connect flags in the shared export data structure. LU-1623 */
1529         reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1530         exp = req->rq_export;
1531         cfs_spin_lock(&exp->exp_lock);
1532         exp->exp_connect_flags = reply->ocd_connect_flags;
1533         cfs_spin_unlock(&exp->exp_lock);
1534
1535         rc = mdt_init_idmap(info);
1536         if (rc != 0)
1537                 obd_disconnect(class_export_get(req->rq_export));
1538
1539         return rc;
1540 }
1541
1542 static int mdt_disconnect(struct mdt_thread_info *info)
1543 {
1544         int rc;
1545         ENTRY;
1546
1547         rc = target_handle_disconnect(mdt_info_req(info));
1548         if (rc)
1549                 rc = err_serious(rc);
1550         RETURN(rc);
1551 }
1552
1553 static int mdt_sendpage(struct mdt_thread_info *info,
1554                         struct lu_rdpg *rdpg, int nob)
1555 {
1556         struct ptlrpc_request   *req = mdt_info_req(info);
1557         struct obd_export       *exp = req->rq_export;
1558         struct ptlrpc_bulk_desc *desc;
1559         struct l_wait_info      *lwi = &info->mti_u.rdpg.mti_wait_info;
1560         int                      tmpcount;
1561         int                      tmpsize;
1562         int                      i;
1563         int                      rc;
1564         ENTRY;
1565
1566         desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1567                                     MDS_BULK_PORTAL);
1568         if (desc == NULL)
1569                 RETURN(-ENOMEM);
1570
1571         if (!(exp->exp_connect_flags & OBD_CONNECT_BRW_SIZE))
1572                 /* old client requires reply size in it's PAGE_SIZE,
1573                  * which is rdpg->rp_count */
1574                 nob = rdpg->rp_count;
1575
1576         for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1577              i++, tmpcount -= tmpsize) {
1578                 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1579                 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1580         }
1581
1582         LASSERT(desc->bd_nob == nob);
1583         rc = target_bulk_io(exp, desc, lwi);
1584         ptlrpc_free_bulk_pin(desc);
1585         RETURN(rc);
1586 }
1587
1588 static int mdt_readpage(struct mdt_thread_info *info)
1589 {
1590         struct mdt_object *object = info->mti_object;
1591         struct lu_rdpg    *rdpg = &info->mti_u.rdpg.mti_rdpg;
1592         struct mdt_body   *reqbody;
1593         struct mdt_body   *repbody;
1594         int                rc;
1595         int                i;
1596         ENTRY;
1597
1598         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1599                 RETURN(err_serious(-ENOMEM));
1600
1601         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1602         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1603         if (reqbody == NULL || repbody == NULL)
1604                 RETURN(err_serious(-EFAULT));
1605
1606         /*
1607          * prepare @rdpg before calling lower layers and transfer itself. Here
1608          * reqbody->size contains offset of where to start to read and
1609          * reqbody->nlink contains number bytes to read.
1610          */
1611         rdpg->rp_hash = reqbody->size;
1612         if (rdpg->rp_hash != reqbody->size) {
1613                 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1614                        rdpg->rp_hash, reqbody->size);
1615                 RETURN(-EFAULT);
1616         }
1617
1618         rdpg->rp_attrs = reqbody->mode;
1619         if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
1620                 rdpg->rp_attrs |= LUDA_64BITHASH;
1621         rdpg->rp_count  = min_t(unsigned int, reqbody->nlink,
1622                                 PTLRPC_MAX_BRW_SIZE);
1623         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1624                           CFS_PAGE_SHIFT;
1625         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1626         if (rdpg->rp_pages == NULL)
1627                 RETURN(-ENOMEM);
1628
1629         for (i = 0; i < rdpg->rp_npages; ++i) {
1630                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1631                 if (rdpg->rp_pages[i] == NULL)
1632                         GOTO(free_rdpg, rc = -ENOMEM);
1633         }
1634
1635         /* call lower layers to fill allocated pages with directory data */
1636         rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1637         if (rc < 0)
1638                 GOTO(free_rdpg, rc);
1639
1640         /* send pages to client */
1641         rc = mdt_sendpage(info, rdpg, rc);
1642
1643         EXIT;
1644 free_rdpg:
1645
1646         for (i = 0; i < rdpg->rp_npages; i++)
1647                 if (rdpg->rp_pages[i] != NULL)
1648                         cfs_free_page(rdpg->rp_pages[i]);
1649         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1650
1651         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1652                 RETURN(0);
1653
1654         return rc;
1655 }
1656
1657 static int mdt_reint_internal(struct mdt_thread_info *info,
1658                               struct mdt_lock_handle *lhc,
1659                               __u32 op)
1660 {
1661         struct req_capsule      *pill = info->mti_pill;
1662         struct mdt_body         *repbody;
1663         int                      rc = 0, rc2;
1664         ENTRY;
1665
1666
1667         rc = mdt_reint_unpack(info, op);
1668         if (rc != 0) {
1669                 CERROR("Can't unpack reint, rc %d\n", rc);
1670                 RETURN(err_serious(rc));
1671         }
1672
1673         /* for replay (no_create) lmm is not needed, client has it already */
1674         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1675                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1676                                      info->mti_rr.rr_eadatalen);
1677
1678         /* llog cookies are always 0, the field is kept for compatibility */
1679         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1680                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1681
1682         rc = req_capsule_server_pack(pill);
1683         if (rc != 0) {
1684                 CERROR("Can't pack response, rc %d\n", rc);
1685                 RETURN(err_serious(rc));
1686         }
1687
1688         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1689                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1690                 LASSERT(repbody);
1691                 repbody->eadatasize = 0;
1692                 repbody->aclsize = 0;
1693         }
1694
1695         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1696
1697         /* for replay no cookkie / lmm need, because client have this already */
1698         if (info->mti_spec.no_create)
1699                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1700                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1701
1702         rc = mdt_init_ucred_reint(info);
1703         if (rc)
1704                 GOTO(out_shrink, rc);
1705
1706         rc = mdt_fix_attr_ucred(info, op);
1707         if (rc != 0)
1708                 GOTO(out_ucred, rc = err_serious(rc));
1709
1710         if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1711                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1712                 GOTO(out_ucred, rc);
1713         }
1714         rc = mdt_reint_rec(info, lhc);
1715         EXIT;
1716 out_ucred:
1717         mdt_exit_ucred(info);
1718 out_shrink:
1719         mdt_client_compatibility(info);
1720         rc2 = mdt_fix_reply(info);
1721         if (rc == 0)
1722                 rc = rc2;
1723         return rc;
1724 }
1725
1726 static long mdt_reint_opcode(struct mdt_thread_info *info,
1727                              const struct req_format **fmt)
1728 {
1729         struct mdt_rec_reint *rec;
1730         long opc;
1731
1732         opc = err_serious(-EFAULT);
1733         rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1734         if (rec != NULL) {
1735                 opc = rec->rr_opcode;
1736                 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1737                 if (opc < REINT_MAX && fmt[opc] != NULL)
1738                         req_capsule_extend(info->mti_pill, fmt[opc]);
1739                 else {
1740                         CERROR("Unsupported opc: %ld\n", opc);
1741                         opc = err_serious(opc);
1742                 }
1743         }
1744         return opc;
1745 }
1746
1747 static int mdt_reint(struct mdt_thread_info *info)
1748 {
1749         long opc;
1750         int  rc;
1751
1752         static const struct req_format *reint_fmts[REINT_MAX] = {
1753                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
1754                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
1755                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
1756                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
1757                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
1758                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
1759                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1760         };
1761
1762         ENTRY;
1763
1764         opc = mdt_reint_opcode(info, reint_fmts);
1765         if (opc >= 0) {
1766                 /*
1767                  * No lock possible here from client to pass it to reint code
1768                  * path.
1769                  */
1770                 rc = mdt_reint_internal(info, NULL, opc);
1771         } else {
1772                 rc = opc;
1773         }
1774
1775         info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1776         RETURN(rc);
1777 }
1778
1779 /* this should sync the whole device */
1780 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1781 {
1782         struct dt_device *dt = mdt->mdt_bottom;
1783         int rc;
1784         ENTRY;
1785
1786         rc = dt->dd_ops->dt_sync(env, dt);
1787         RETURN(rc);
1788 }
1789
1790 /* this should sync this object */
1791 static int mdt_object_sync(struct mdt_thread_info *info)
1792 {
1793         struct md_object *next;
1794         int rc;
1795         ENTRY;
1796
1797         if (!mdt_object_exists(info->mti_object)) {
1798                 CWARN("Non existing object  "DFID"!\n",
1799                       PFID(mdt_object_fid(info->mti_object)));
1800                 RETURN(-ESTALE);
1801         }
1802         next = mdt_object_child(info->mti_object);
1803         rc = mo_object_sync(info->mti_env, next);
1804
1805         RETURN(rc);
1806 }
1807
1808 static int mdt_sync(struct mdt_thread_info *info)
1809 {
1810         struct ptlrpc_request *req = mdt_info_req(info);
1811         struct req_capsule *pill = info->mti_pill;
1812         struct mdt_body *body;
1813         int rc;
1814         ENTRY;
1815
1816         /* The fid may be zero, so we req_capsule_set manually */
1817         req_capsule_set(pill, &RQF_MDS_SYNC);
1818
1819         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1820         if (body == NULL)
1821                 RETURN(err_serious(-EINVAL));
1822
1823         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1824                 RETURN(err_serious(-ENOMEM));
1825
1826         if (fid_seq(&body->fid1) == 0) {
1827                 /* sync the whole device */
1828                 rc = req_capsule_server_pack(pill);
1829                 if (rc == 0)
1830                         rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1831                 else
1832                         rc = err_serious(rc);
1833         } else {
1834                 /* sync an object */
1835                 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1836                 if (rc == 0) {
1837                         rc = mdt_object_sync(info);
1838                         if (rc == 0) {
1839                                 const struct lu_fid *fid;
1840                                 struct lu_attr *la = &info->mti_attr.ma_attr;
1841
1842                                 info->mti_attr.ma_need = MA_INODE;
1843                                 info->mti_attr.ma_valid = 0;
1844                                 rc = mdt_attr_get_complex(info, info->mti_object,
1845                                                           &info->mti_attr);
1846                                 if (rc == 0) {
1847                                         body = req_capsule_server_get(pill,
1848                                                                 &RMF_MDT_BODY);
1849                                         fid = mdt_object_fid(info->mti_object);
1850                                         mdt_pack_attr2body(info, body, la, fid);
1851                                 }
1852                         }
1853                 } else
1854                         rc = err_serious(rc);
1855         }
1856         if (rc == 0)
1857                 mdt_counter_incr(req, LPROC_MDT_SYNC);
1858
1859         RETURN(rc);
1860 }
1861
1862 /*
1863  * Quotacheck handler.
1864  * in-kernel quotacheck isn't supported any more.
1865  */
1866 static int mdt_quotacheck(struct mdt_thread_info *info)
1867 {
1868         struct obd_quotactl     *oqctl;
1869         int                      rc;
1870         ENTRY;
1871
1872         oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1873         if (oqctl == NULL)
1874                 RETURN(err_serious(-EPROTO));
1875
1876         rc = req_capsule_server_pack(info->mti_pill);
1877         if (rc)
1878                 RETURN(err_serious(rc));
1879
1880         /* deprecated, not used any more */
1881         RETURN(-EOPNOTSUPP);
1882 }
1883
1884 /*
1885  * Handle quota control requests to consult current usage/limit, but also
1886  * to configure quota enforcement
1887  */
1888 static int mdt_quotactl(struct mdt_thread_info *info)
1889 {
1890         struct obd_export       *exp  = info->mti_exp;
1891         struct req_capsule      *pill = info->mti_pill;
1892         struct obd_quotactl     *oqctl, *repoqc;
1893         int                      id, rc;
1894         struct lu_device        *qmt = info->mti_mdt->mdt_qmt_dev;
1895         ENTRY;
1896
1897         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1898         if (oqctl == NULL)
1899                 RETURN(err_serious(-EPROTO));
1900
1901         rc = req_capsule_server_pack(pill);
1902         if (rc)
1903                 RETURN(err_serious(rc));
1904
1905         switch (oqctl->qc_cmd) {
1906         case Q_QUOTACHECK:
1907         case LUSTRE_Q_INVALIDATE:
1908         case LUSTRE_Q_FINVALIDATE:
1909         case Q_QUOTAON:
1910         case Q_QUOTAOFF:
1911         case Q_INITQUOTA:
1912                 /* deprecated, not used any more */
1913                 RETURN(-EOPNOTSUPP);
1914                 /* master quotactl */
1915         case Q_GETINFO:
1916         case Q_SETINFO:
1917         case Q_SETQUOTA:
1918         case Q_GETQUOTA:
1919                 if (qmt == NULL)
1920                         RETURN(-EOPNOTSUPP);
1921                 /* slave quotactl */
1922         case Q_GETOINFO:
1923         case Q_GETOQUOTA:
1924                 break;
1925         default:
1926                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1927                 RETURN(-EFAULT);
1928         }
1929
1930         /* map uid/gid for remote client */
1931         id = oqctl->qc_id;
1932         if (exp_connect_rmtclient(exp)) {
1933                 struct lustre_idmap_table *idmap;
1934
1935                 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
1936
1937                 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1938                              oqctl->qc_cmd != Q_GETINFO))
1939                         RETURN(-EPERM);
1940
1941                 if (oqctl->qc_type == USRQUOTA)
1942                         id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1943                                                      oqctl->qc_id);
1944                 else if (oqctl->qc_type == GRPQUOTA)
1945                         id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1946                                                      oqctl->qc_id);
1947                 else
1948                         RETURN(-EINVAL);
1949
1950                 if (id == CFS_IDMAP_NOTFOUND) {
1951                         CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1952                         RETURN(-EACCES);
1953                 }
1954         }
1955
1956         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1957         if (repoqc == NULL)
1958                 RETURN(err_serious(-EFAULT));
1959
1960         if (oqctl->qc_id != id)
1961                 swap(oqctl->qc_id, id);
1962
1963         switch (oqctl->qc_cmd) {
1964
1965         case Q_GETINFO:
1966         case Q_SETINFO:
1967         case Q_SETQUOTA:
1968         case Q_GETQUOTA:
1969                 /* forward quotactl request to QMT */
1970                 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
1971                 break;
1972
1973         case Q_GETOINFO:
1974         case Q_GETOQUOTA:
1975                 /* slave quotactl */
1976                 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
1977                                    oqctl);
1978                 break;
1979
1980         default:
1981                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1982                 RETURN(-EFAULT);
1983         }
1984
1985         if (oqctl->qc_id != id)
1986                 swap(oqctl->qc_id, id);
1987
1988         *repoqc = *oqctl;
1989         RETURN(rc);
1990 }
1991
1992 /*
1993  * OBD PING and other handlers.
1994  */
1995 static int mdt_obd_ping(struct mdt_thread_info *info)
1996 {
1997         int rc;
1998         ENTRY;
1999
2000         req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2001
2002         rc = target_handle_ping(mdt_info_req(info));
2003         if (rc < 0)
2004                 rc = err_serious(rc);
2005         RETURN(rc);
2006 }
2007
2008 /*
2009  * OBD_IDX_READ handler
2010  */
2011 static int mdt_obd_idx_read(struct mdt_thread_info *info)
2012 {
2013         struct mdt_device       *mdt = info->mti_mdt;
2014         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
2015         struct idx_info         *req_ii, *rep_ii;
2016         int                      rc, i;
2017         ENTRY;
2018
2019         memset(rdpg, 0, sizeof(*rdpg));
2020         req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2021
2022         /* extract idx_info buffer from request & reply */
2023         req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2024         if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2025                 RETURN(err_serious(-EPROTO));
2026
2027         rc = req_capsule_server_pack(info->mti_pill);
2028         if (rc)
2029                 RETURN(err_serious(rc));
2030
2031         rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2032         if (rep_ii == NULL)
2033                 RETURN(err_serious(-EFAULT));
2034         rep_ii->ii_magic = IDX_INFO_MAGIC;
2035
2036         /* extract hash to start with */
2037         rdpg->rp_hash = req_ii->ii_hash_start;
2038
2039         /* extract requested attributes */
2040         rdpg->rp_attrs = req_ii->ii_attrs;
2041
2042         /* check that fid packed in request is valid and supported */
2043         if (!fid_is_sane(&req_ii->ii_fid))
2044                 RETURN(-EINVAL);
2045         rep_ii->ii_fid = req_ii->ii_fid;
2046
2047         /* copy flags */
2048         rep_ii->ii_flags = req_ii->ii_flags;
2049
2050         /* compute number of pages to allocate, ii_count is the number of 4KB
2051          * containers */
2052         if (req_ii->ii_count <= 0)
2053                 GOTO(out, rc = -EFAULT);
2054         rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2055                                PTLRPC_MAX_BRW_SIZE);
2056         rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2057
2058         /* allocate pages to store the containers */
2059         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2060         if (rdpg->rp_pages == NULL)
2061                 GOTO(out, rc = -ENOMEM);
2062         for (i = 0; i < rdpg->rp_npages; i++) {
2063                 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2064                 if (rdpg->rp_pages[i] == NULL)
2065                         GOTO(out, rc = -ENOMEM);
2066         }
2067
2068         /* populate pages with key/record pairs */
2069         rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2070         if (rc < 0)
2071                 GOTO(out, rc);
2072
2073         LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2074                  "asked %d > %d\n", rc, rdpg->rp_count);
2075
2076         /* send pages to client */
2077         rc = mdt_sendpage(info, rdpg, rc);
2078
2079         GOTO(out, rc);
2080 out:
2081         if (rdpg->rp_pages) {
2082                 for (i = 0; i < rdpg->rp_npages; i++)
2083                         if (rdpg->rp_pages[i])
2084                                 cfs_free_page(rdpg->rp_pages[i]);
2085                 OBD_FREE(rdpg->rp_pages,
2086                          rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2087         }
2088         return rc;
2089 }
2090
2091 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
2092 {
2093         return err_serious(-EOPNOTSUPP);
2094 }
2095
2096 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
2097 {
2098         return err_serious(-EOPNOTSUPP);
2099 }
2100
2101
2102 /*
2103  * LLOG handlers.
2104  */
2105
2106 /** clone llog ctxt from child (mdd)
2107  * This allows remote llog (replicator) access.
2108  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2109  * context was originally set up, or we can handle them directly.
2110  * I choose the latter, but that means I need any llog
2111  * contexts set up by child to be accessable by the mdt.  So we clone the
2112  * context into our context list here.
2113  */
2114 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2115                                int idx)
2116 {
2117         struct md_device  *next = mdt->mdt_child;
2118         struct llog_ctxt *ctxt;
2119         int rc;
2120
2121         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2122                 return 0;
2123
2124         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2125         if (rc || ctxt == NULL) {
2126                 return 0;
2127         }
2128
2129         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2130         if (rc)
2131                 CERROR("Can't set mdt ctxt %d\n", rc);
2132
2133         return rc;
2134 }
2135
2136 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2137                                  struct mdt_device *mdt, int idx)
2138 {
2139         struct llog_ctxt *ctxt;
2140
2141         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2142         if (ctxt == NULL)
2143                 return 0;
2144         /* Put once for the get we just did, and once for the clone */
2145         llog_ctxt_put(ctxt);
2146         llog_ctxt_put(ctxt);
2147         return 0;
2148 }
2149
2150 static int mdt_llog_create(struct mdt_thread_info *info)
2151 {
2152         int rc;
2153
2154         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2155         rc = llog_origin_handle_open(mdt_info_req(info));
2156         return (rc < 0 ? err_serious(rc) : rc);
2157 }
2158
2159 static int mdt_llog_destroy(struct mdt_thread_info *info)
2160 {
2161         int rc;
2162
2163         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2164         rc = llog_origin_handle_destroy(mdt_info_req(info));
2165         return (rc < 0 ? err_serious(rc) : rc);
2166 }
2167
2168 static int mdt_llog_read_header(struct mdt_thread_info *info)
2169 {
2170         int rc;
2171
2172         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2173         rc = llog_origin_handle_read_header(mdt_info_req(info));
2174         return (rc < 0 ? err_serious(rc) : rc);
2175 }
2176
2177 static int mdt_llog_next_block(struct mdt_thread_info *info)
2178 {
2179         int rc;
2180
2181         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2182         rc = llog_origin_handle_next_block(mdt_info_req(info));
2183         return (rc < 0 ? err_serious(rc) : rc);
2184 }
2185
2186 static int mdt_llog_prev_block(struct mdt_thread_info *info)
2187 {
2188         int rc;
2189
2190         req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2191         rc = llog_origin_handle_prev_block(mdt_info_req(info));
2192         return (rc < 0 ? err_serious(rc) : rc);
2193 }
2194
2195
2196 /*
2197  * DLM handlers.
2198  */
2199 static struct ldlm_callback_suite cbs = {
2200         .lcs_completion = ldlm_server_completion_ast,
2201         .lcs_blocking   = ldlm_server_blocking_ast,
2202         .lcs_glimpse    = ldlm_server_glimpse_ast
2203 };
2204
2205 static int mdt_enqueue(struct mdt_thread_info *info)
2206 {
2207         struct ptlrpc_request *req;
2208         int rc;
2209
2210         /*
2211          * info->mti_dlm_req already contains swapped and (if necessary)
2212          * converted dlm request.
2213          */
2214         LASSERT(info->mti_dlm_req != NULL);
2215
2216         req = mdt_info_req(info);
2217         rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2218                                   req, info->mti_dlm_req, &cbs);
2219         info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2220         return rc ? err_serious(rc) : req->rq_status;
2221 }
2222
2223 static int mdt_convert(struct mdt_thread_info *info)
2224 {
2225         int rc;
2226         struct ptlrpc_request *req;
2227
2228         LASSERT(info->mti_dlm_req);
2229         req = mdt_info_req(info);
2230         rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2231         return rc ? err_serious(rc) : req->rq_status;
2232 }
2233
2234 static int mdt_bl_callback(struct mdt_thread_info *info)
2235 {
2236         CERROR("bl callbacks should not happen on MDS\n");
2237         LBUG();
2238         return err_serious(-EOPNOTSUPP);
2239 }
2240
2241 static int mdt_cp_callback(struct mdt_thread_info *info)
2242 {
2243         CERROR("cp callbacks should not happen on MDS\n");
2244         LBUG();
2245         return err_serious(-EOPNOTSUPP);
2246 }
2247
2248 /*
2249  * sec context handlers
2250  */
2251 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2252 {
2253         int rc;
2254
2255         rc = mdt_handle_idmap(info);
2256
2257         if (unlikely(rc)) {
2258                 struct ptlrpc_request *req = mdt_info_req(info);
2259                 __u32                  opc;
2260
2261                 opc = lustre_msg_get_opc(req->rq_reqmsg);
2262                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2263                         sptlrpc_svc_ctx_invalidate(req);
2264         }
2265
2266         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2267
2268         return rc;
2269 }
2270
2271 /*
2272  * quota request handlers
2273  */
2274 static int mdt_quota_dqacq(struct mdt_thread_info *info)
2275 {
2276         struct lu_device        *qmt = info->mti_mdt->mdt_qmt_dev;
2277         int                      rc;
2278         ENTRY;
2279
2280         if (qmt == NULL)
2281                 RETURN(err_serious(-EOPNOTSUPP));
2282
2283         rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2284         RETURN(rc);
2285 }
2286
2287 static struct mdt_object *mdt_obj(struct lu_object *o)
2288 {
2289         LASSERT(lu_device_is_mdt(o->lo_dev));
2290         return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2291 }
2292
2293 struct mdt_object *mdt_object_new(const struct lu_env *env,
2294                                   struct mdt_device *d,
2295                                   const struct lu_fid *f)
2296 {
2297         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2298         struct lu_object *o;
2299         struct mdt_object *m;
2300         ENTRY;
2301
2302         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2303         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2304         if (unlikely(IS_ERR(o)))
2305                 m = (struct mdt_object *)o;
2306         else
2307                 m = mdt_obj(o);
2308         RETURN(m);
2309 }
2310
2311 struct mdt_object *mdt_object_find(const struct lu_env *env,
2312                                    struct mdt_device *d,
2313                                    const struct lu_fid *f)
2314 {
2315         struct lu_object *o;
2316         struct mdt_object *m;
2317         ENTRY;
2318
2319         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2320         o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2321         if (unlikely(IS_ERR(o)))
2322                 m = (struct mdt_object *)o;
2323         else
2324                 m = mdt_obj(o);
2325         RETURN(m);
2326 }
2327
2328 /**
2329  * Asyncronous commit for mdt device.
2330  *
2331  * Pass asynchonous commit call down the MDS stack.
2332  *
2333  * \param env environment
2334  * \param mdt the mdt device
2335  */
2336 static void mdt_device_commit_async(const struct lu_env *env,
2337                                     struct mdt_device *mdt)
2338 {
2339         struct dt_device *dt = mdt->mdt_bottom;
2340         int rc;
2341
2342         rc = dt->dd_ops->dt_commit_async(env, dt);
2343         if (unlikely(rc != 0))
2344                 CWARN("async commit start failed with rc = %d", rc);
2345 }
2346
2347 /**
2348  * Mark the lock as "synchonous".
2349  *
2350  * Mark the lock to deffer transaction commit to the unlock time.
2351  *
2352  * \param lock the lock to mark as "synchonous"
2353  *
2354  * \see mdt_is_lock_sync
2355  * \see mdt_save_lock
2356  */
2357 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2358 {
2359         lock->l_ast_data = (void*)1;
2360 }
2361
2362 /**
2363  * Check whehter the lock "synchonous" or not.
2364  *
2365  * \param lock the lock to check
2366  * \retval 1 the lock is "synchonous"
2367  * \retval 0 the lock isn't "synchronous"
2368  *
2369  * \see mdt_set_lock_sync
2370  * \see mdt_save_lock
2371  */
2372 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2373 {
2374         return lock->l_ast_data != NULL;
2375 }
2376
2377 /**
2378  * Blocking AST for mdt locks.
2379  *
2380  * Starts transaction commit if in case of COS lock conflict or
2381  * deffers such a commit to the mdt_save_lock.
2382  *
2383  * \param lock the lock which blocks a request or cancelling lock
2384  * \param desc unused
2385  * \param data unused
2386  * \param flag indicates whether this cancelling or blocking callback
2387  * \retval 0
2388  * \see ldlm_blocking_ast_nocheck
2389  */
2390 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2391                      void *data, int flag)
2392 {
2393         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2394         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2395         int rc;
2396         ENTRY;
2397
2398         if (flag == LDLM_CB_CANCELING)
2399                 RETURN(0);
2400         lock_res_and_lock(lock);
2401         if (lock->l_blocking_ast != mdt_blocking_ast) {
2402                 unlock_res_and_lock(lock);
2403                 RETURN(0);
2404         }
2405         if (mdt_cos_is_enabled(mdt) &&
2406             lock->l_req_mode & (LCK_PW | LCK_EX) &&
2407             lock->l_blocking_lock != NULL &&
2408             lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2409                 mdt_set_lock_sync(lock);
2410         }
2411         rc = ldlm_blocking_ast_nocheck(lock);
2412
2413         /* There is no lock conflict if l_blocking_lock == NULL,
2414          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2415          * when the last reference to a local lock was released */
2416         if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2417                 struct lu_env env;
2418
2419                 rc = lu_env_init(&env, LCT_LOCAL);
2420                 if (unlikely(rc != 0))
2421                         CWARN("lu_env initialization failed with rc = %d,"
2422                               "cannot start asynchronous commit\n", rc);
2423                 else
2424                         mdt_device_commit_async(&env, mdt);
2425                 lu_env_fini(&env);
2426         }
2427         RETURN(rc);
2428 }
2429
2430 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2431                     struct mdt_lock_handle *lh, __u64 ibits, int locality)
2432 {
2433         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2434         ldlm_policy_data_t *policy = &info->mti_policy;
2435         struct ldlm_res_id *res_id = &info->mti_res_id;
2436         int rc;
2437         ENTRY;
2438
2439         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2440         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2441         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2442         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2443
2444         if (mdt_object_exists(o) < 0) {
2445                 if (locality == MDT_CROSS_LOCK) {
2446                         /* cross-ref object fix */
2447                         ibits &= ~MDS_INODELOCK_UPDATE;
2448                         ibits |= MDS_INODELOCK_LOOKUP;
2449                 } else {
2450                         LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2451                         LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2452                 }
2453                 /* No PDO lock on remote object */
2454                 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2455         }
2456
2457         if (lh->mlh_type == MDT_PDO_LOCK) {
2458                 /* check for exists after object is locked */
2459                 if (mdt_object_exists(o) == 0) {
2460                         /* Non-existent object shouldn't have PDO lock */
2461                         RETURN(-ESTALE);
2462                 } else {
2463                         /* Non-dir object shouldn't have PDO lock */
2464                         if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2465                                 RETURN(-ENOTDIR);
2466                 }
2467         }
2468
2469         memset(policy, 0, sizeof(*policy));
2470         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2471
2472         /*
2473          * Take PDO lock on whole directory and build correct @res_id for lock
2474          * on part of directory.
2475          */
2476         if (lh->mlh_pdo_hash != 0) {
2477                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2478                 mdt_lock_pdo_mode(info, o, lh);
2479                 if (lh->mlh_pdo_mode != LCK_NL) {
2480                         /*
2481                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2482                          * is never going to be sent to client and we do not
2483                          * want it slowed down due to possible cancels.
2484                          */
2485                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2486                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2487                                           policy, res_id, LDLM_FL_ATOMIC_CB,
2488                                           &info->mti_exp->exp_handle.h_cookie);
2489                         if (unlikely(rc))
2490                                 RETURN(rc);
2491                 }
2492
2493                 /*
2494                  * Finish res_id initializing by name hash marking part of
2495                  * directory which is taking modification.
2496                  */
2497                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2498         }
2499
2500         policy->l_inodebits.bits = ibits;
2501
2502         /*
2503          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2504          * going to be sent to client. If it is - mdt_intent_policy() path will
2505          * fix it up and turn FL_LOCAL flag off.
2506          */
2507         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2508                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2509                           &info->mti_exp->exp_handle.h_cookie);
2510         if (rc)
2511                 mdt_object_unlock(info, o, lh, 1);
2512         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2513                  lh->mlh_pdo_hash != 0 &&
2514                  (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2515                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2516         }
2517
2518         RETURN(rc);
2519 }
2520
2521 /**
2522  * Save a lock within request object.
2523  *
2524  * Keep the lock referenced until whether client ACK or transaction
2525  * commit happens or release the lock immediately depending on input
2526  * parameters. If COS is ON, a write lock is converted to COS lock
2527  * before saving.
2528  *
2529  * \param info thead info object
2530  * \param h lock handle
2531  * \param mode lock mode
2532  * \param decref force immediate lock releasing
2533  */
2534 static
2535 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2536                    ldlm_mode_t mode, int decref)
2537 {
2538         ENTRY;
2539
2540         if (lustre_handle_is_used(h)) {
2541                 if (decref || !info->mti_has_trans ||
2542                     !(mode & (LCK_PW | LCK_EX))){
2543                         mdt_fid_unlock(h, mode);
2544                 } else {
2545                         struct mdt_device *mdt = info->mti_mdt;
2546                         struct ldlm_lock *lock = ldlm_handle2lock(h);
2547                         struct ptlrpc_request *req = mdt_info_req(info);
2548                         int no_ack = 0;
2549
2550                         LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2551                                  h->cookie);
2552                         CDEBUG(D_HA, "request = %p reply state = %p"
2553                                " transno = "LPD64"\n",
2554                                req, req->rq_reply_state, req->rq_transno);
2555                         if (mdt_cos_is_enabled(mdt)) {
2556                                 no_ack = 1;
2557                                 ldlm_lock_downgrade(lock, LCK_COS);
2558                                 mode = LCK_COS;
2559                         }
2560                         ptlrpc_save_lock(req, h, mode, no_ack);
2561                         if (mdt_is_lock_sync(lock)) {
2562                                 CDEBUG(D_HA, "found sync-lock,"
2563                                        " async commit started\n");
2564                                 mdt_device_commit_async(info->mti_env,
2565                                                         mdt);
2566                         }
2567                         LDLM_LOCK_PUT(lock);
2568                 }
2569                 h->cookie = 0ull;
2570         }
2571
2572         EXIT;
2573 }
2574
2575 /**
2576  * Unlock mdt object.
2577  *
2578  * Immeditely release the regular lock and the PDO lock or save the
2579  * lock in reqeuest and keep them referenced until client ACK or
2580  * transaction commit.
2581  *
2582  * \param info thread info object
2583  * \param o mdt object
2584  * \param lh mdt lock handle referencing regular and PDO locks
2585  * \param decref force immediate lock releasing
2586  */
2587 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2588                        struct mdt_lock_handle *lh, int decref)
2589 {
2590         ENTRY;
2591
2592         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2593         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2594
2595         EXIT;
2596 }
2597
2598 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2599                                         const struct lu_fid *f,
2600                                         struct mdt_lock_handle *lh,
2601                                         __u64 ibits)
2602 {
2603         struct mdt_object *o;
2604
2605         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2606         if (!IS_ERR(o)) {
2607                 int rc;
2608
2609                 rc = mdt_object_lock(info, o, lh, ibits,
2610                                      MDT_LOCAL_LOCK);
2611                 if (rc != 0) {
2612                         mdt_object_put(info->mti_env, o);
2613                         o = ERR_PTR(rc);
2614                 }
2615         }
2616         return o;
2617 }
2618
2619 void mdt_object_unlock_put(struct mdt_thread_info * info,
2620                            struct mdt_object * o,
2621                            struct mdt_lock_handle *lh,
2622                            int decref)
2623 {
2624         mdt_object_unlock(info, o, lh, decref);
2625         mdt_object_put(info->mti_env, o);
2626 }
2627
2628 static struct mdt_handler *mdt_handler_find(__u32 opc,
2629                                             struct mdt_opc_slice *supported)
2630 {
2631         struct mdt_opc_slice *s;
2632         struct mdt_handler   *h;
2633
2634         h = NULL;
2635         for (s = supported; s->mos_hs != NULL; s++) {
2636                 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2637                         h = s->mos_hs + (opc - s->mos_opc_start);
2638                         if (likely(h->mh_opc != 0))
2639                                 LASSERTF(h->mh_opc == opc,
2640                                          "opcode mismatch %d != %d\n",
2641                                          h->mh_opc, opc);
2642                         else
2643                                 h = NULL; /* unsupported opc */
2644                         break;
2645                 }
2646         }
2647         return h;
2648 }
2649
2650 static int mdt_lock_resname_compat(struct mdt_device *m,
2651                                    struct ldlm_request *req)
2652 {
2653         /* XXX something... later. */
2654         return 0;
2655 }
2656
2657 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2658 {
2659         /* XXX something... later. */
2660         return 0;
2661 }
2662
2663 /*
2664  * Generic code handling requests that have struct mdt_body passed in:
2665  *
2666  *  - extract mdt_body from request and save it in @info, if present;
2667  *
2668  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
2669  *  @info;
2670  *
2671  *  - if HABEO_CORPUS flag is set for this request type check whether object
2672  *  actually exists on storage (lu_object_exists()).
2673  *
2674  */
2675 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2676 {
2677         const struct mdt_body    *body;
2678         struct mdt_object        *obj;
2679         const struct lu_env      *env;
2680         struct req_capsule       *pill;
2681         int                       rc;
2682         ENTRY;
2683
2684         env = info->mti_env;
2685         pill = info->mti_pill;
2686
2687         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2688         if (body == NULL)
2689                 RETURN(-EFAULT);
2690
2691         if (!(body->valid & OBD_MD_FLID))
2692                 RETURN(0);
2693
2694         if (!fid_is_sane(&body->fid1)) {
2695                 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2696                 RETURN(-EINVAL);
2697         }
2698
2699         /*
2700          * Do not get size or any capa fields before we check that request
2701          * contains capa actually. There are some requests which do not, for
2702          * instance MDS_IS_SUBDIR.
2703          */
2704         if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2705             req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2706                 mdt_set_capainfo(info, 0, &body->fid1,
2707                                  req_capsule_client_get(pill, &RMF_CAPA1));
2708
2709         obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2710         if (!IS_ERR(obj)) {
2711                 if ((flags & HABEO_CORPUS) &&
2712                     !mdt_object_exists(obj)) {
2713                         mdt_object_put(env, obj);
2714                         /* for capability renew ENOENT will be handled in
2715                          * mdt_renew_capa */
2716                         if (body->valid & OBD_MD_FLOSSCAPA)
2717                                 rc = 0;
2718                         else
2719                                 rc = -ENOENT;
2720                 } else {
2721                         info->mti_object = obj;
2722                         rc = 0;
2723                 }
2724         } else
2725                 rc = PTR_ERR(obj);
2726
2727         RETURN(rc);
2728 }
2729
2730 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2731 {
2732         struct req_capsule *pill = info->mti_pill;
2733         int rc;
2734         ENTRY;
2735
2736         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2737                 rc = mdt_body_unpack(info, flags);
2738         else
2739                 rc = 0;
2740
2741         if (rc == 0 && (flags & HABEO_REFERO)) {
2742                 /* Pack reply. */
2743                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2744                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2745                                              info->mti_body->eadatasize);
2746                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2747                         req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2748                                              RCL_SERVER, 0);
2749
2750                 rc = req_capsule_server_pack(pill);
2751         }
2752         RETURN(rc);
2753 }
2754
2755 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2756 {
2757         struct md_device *next = m->mdt_child;
2758
2759         return next->md_ops->mdo_init_capa_ctxt(env, next,
2760                                                 m->mdt_opts.mo_mds_capa,
2761                                                 m->mdt_capa_timeout,
2762                                                 m->mdt_capa_alg,
2763                                                 m->mdt_capa_keys);
2764 }
2765
2766 /*
2767  * Invoke handler for this request opc. Also do necessary preprocessing
2768  * (according to handler ->mh_flags), and post-processing (setting of
2769  * ->last_{xid,committed}).
2770  */
2771 static int mdt_req_handle(struct mdt_thread_info *info,
2772                           struct mdt_handler *h, struct ptlrpc_request *req)
2773 {
2774         int   rc, serious = 0;
2775         __u32 flags;
2776
2777         ENTRY;
2778
2779         LASSERT(h->mh_act != NULL);
2780         LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2781         LASSERT(current->journal_info == NULL);
2782
2783         /*
2784          * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2785          * to put same checks into handlers like mdt_close(), mdt_reint(),
2786          * etc., without talking to mdt authors first. Checking same thing
2787          * there again is useless and returning 0 error without packing reply
2788          * is buggy! Handlers either pack reply or return error.
2789          *
2790          * We return 0 here and do not send any reply in order to emulate
2791          * network failure. Do not send any reply in case any of NET related
2792          * fail_id has occured.
2793          */
2794         if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2795                 RETURN(0);
2796
2797         rc = 0;
2798         flags = h->mh_flags;
2799         LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2800
2801         if (h->mh_fmt != NULL) {
2802                 req_capsule_set(info->mti_pill, h->mh_fmt);
2803                 rc = mdt_unpack_req_pack_rep(info, flags);
2804         }
2805
2806         if (rc == 0 && flags & MUTABOR &&
2807             req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2808                 /* should it be rq_status? */
2809                 rc = -EROFS;
2810
2811         if (rc == 0 && flags & HABEO_CLAVIS) {
2812                 struct ldlm_request *dlm_req;
2813
2814                 LASSERT(h->mh_fmt != NULL);
2815
2816                 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2817                 if (dlm_req != NULL) {
2818                         if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2819                                         LDLM_IBITS &&
2820                                      dlm_req->lock_desc.l_policy_data.\
2821                                         l_inodebits.bits == 0)) {
2822                                 /*
2823                                  * Lock without inodebits makes no sense and
2824                                  * will oops later in ldlm. If client miss to
2825                                  * set such bits, do not trigger ASSERTION.
2826                                  *
2827                                  * For liblustre flock case, it maybe zero.
2828                                  */
2829                                 rc = -EPROTO;
2830                         } else {
2831                                 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2832                                         rc = mdt_lock_resname_compat(
2833                                                                 info->mti_mdt,
2834                                                                 dlm_req);
2835                                 info->mti_dlm_req = dlm_req;
2836                         }
2837                 } else {
2838                         rc = -EFAULT;
2839                 }
2840         }
2841
2842         /* capability setting changed via /proc, needs reinitialize ctxt */
2843         if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2844                 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2845                 info->mti_mdt->mdt_capa_conf = 0;
2846         }
2847
2848         if (likely(rc == 0)) {
2849                 /*
2850                  * Process request, there can be two types of rc:
2851                  * 1) errors with msg unpack/pack, other failures outside the
2852                  * operation itself. This is counted as serious errors;
2853                  * 2) errors during fs operation, should be placed in rq_status
2854                  * only
2855                  */
2856                 rc = h->mh_act(info);
2857                 if (rc == 0 &&
2858                     !req->rq_no_reply && req->rq_reply_state == NULL) {
2859                         DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2860                                   "pack reply and returned 0 error\n",
2861                                   h->mh_name);
2862                         LBUG();
2863                 }
2864                 serious = is_serious(rc);
2865                 rc = clear_serious(rc);
2866         } else
2867                 serious = 1;
2868
2869         req->rq_status = rc;
2870
2871         /*
2872          * ELDLM_* codes which > 0 should be in rq_status only as well as
2873          * all non-serious errors.
2874          */
2875         if (rc > 0 || !serious)
2876                 rc = 0;
2877
2878         LASSERT(current->journal_info == NULL);
2879
2880         if (rc == 0 && (flags & HABEO_CLAVIS) &&
2881             info->mti_mdt->mdt_opts.mo_compat_resname) {
2882                 struct ldlm_reply *dlmrep;
2883
2884                 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2885                 if (dlmrep != NULL)
2886                         rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2887         }
2888
2889         /* If we're DISCONNECTing, the mdt_export_data is already freed */
2890         if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2891                 target_committed_to_req(req);
2892
2893         if (unlikely(req_is_replay(req) &&
2894                      lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2895                 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2896                 LBUG();
2897         }
2898
2899         target_send_reply(req, rc, info->mti_fail_id);
2900         RETURN(0);
2901 }
2902
2903 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2904 {
2905         lh->mlh_type = MDT_NUL_LOCK;
2906         lh->mlh_reg_lh.cookie = 0ull;
2907         lh->mlh_reg_mode = LCK_MINMODE;
2908         lh->mlh_pdo_lh.cookie = 0ull;
2909         lh->mlh_pdo_mode = LCK_MINMODE;
2910 }
2911
2912 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2913 {
2914         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2915         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2916 }
2917
2918 /*
2919  * Initialize fields of struct mdt_thread_info. Other fields are left in
2920  * uninitialized state, because it's too expensive to zero out whole
2921  * mdt_thread_info (> 1K) on each request arrival.
2922  */
2923 static void mdt_thread_info_init(struct ptlrpc_request *req,
2924                                  struct mdt_thread_info *info)
2925 {
2926         int i;
2927         struct md_capainfo *ci;
2928
2929         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2930         info->mti_pill = &req->rq_pill;
2931
2932         /* lock handle */
2933         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2934                 mdt_lock_handle_init(&info->mti_lh[i]);
2935
2936         /* mdt device: it can be NULL while CONNECT */
2937         if (req->rq_export) {
2938                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2939                 info->mti_exp = req->rq_export;
2940         } else
2941                 info->mti_mdt = NULL;
2942         info->mti_env = req->rq_svc_thread->t_env;
2943         ci = md_capainfo(info->mti_env);
2944         memset(ci, 0, sizeof *ci);
2945         if (req->rq_export) {
2946                 if (exp_connect_rmtclient(req->rq_export))
2947                         ci->mc_auth = LC_ID_CONVERT;
2948                 else if (req->rq_export->exp_connect_flags &
2949                          OBD_CONNECT_MDS_CAPA)
2950                         ci->mc_auth = LC_ID_PLAIN;
2951                 else
2952                         ci->mc_auth = LC_ID_NONE;
2953         }
2954
2955         info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2956         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2957         info->mti_mos = NULL;
2958
2959         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2960         info->mti_body = NULL;
2961         info->mti_object = NULL;
2962         info->mti_dlm_req = NULL;
2963         info->mti_has_trans = 0;
2964         info->mti_cross_ref = 0;
2965         info->mti_opdata = 0;
2966         info->mti_big_lmm_used = 0;
2967
2968         /* To not check for split by default. */
2969         info->mti_spec.no_create = 0;
2970 }
2971
2972 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2973 {
2974         int i;
2975
2976         req_capsule_fini(info->mti_pill);
2977         if (info->mti_object != NULL) {
2978                 mdt_object_put(info->mti_env, info->mti_object);
2979                 info->mti_object = NULL;
2980         }
2981         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2982                 mdt_lock_handle_fini(&info->mti_lh[i]);
2983         info->mti_env = NULL;
2984 }
2985
2986 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2987                                        struct obd_device *obd, int *process)
2988 {
2989         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2990         case MDS_CONNECT: /* This will never get here, but for completeness. */
2991         case OST_CONNECT: /* This will never get here, but for completeness. */
2992         case MDS_DISCONNECT:
2993         case OST_DISCONNECT:
2994         case OBD_IDX_READ:
2995                *process = 1;
2996                RETURN(0);
2997
2998         case MDS_CLOSE:
2999         case MDS_DONE_WRITING:
3000         case MDS_SYNC: /* used in unmounting */
3001         case OBD_PING:
3002         case MDS_REINT:
3003         case SEQ_QUERY:
3004         case FLD_QUERY:
3005         case LDLM_ENQUEUE:
3006                 *process = target_queue_recovery_request(req, obd);
3007                 RETURN(0);
3008
3009         default:
3010                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3011                 *process = -EAGAIN;
3012                 RETURN(0);
3013         }
3014 }
3015
3016 /*
3017  * Handle recovery. Return:
3018  *        +1: continue request processing;
3019  *       -ve: abort immediately with the given error code;
3020  *         0: send reply with error code in req->rq_status;
3021  */
3022 static int mdt_recovery(struct mdt_thread_info *info)
3023 {
3024         struct ptlrpc_request *req = mdt_info_req(info);
3025         struct obd_device *obd;
3026
3027         ENTRY;
3028
3029         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3030         case MDS_CONNECT:
3031         case SEC_CTX_INIT:
3032         case SEC_CTX_INIT_CONT:
3033         case SEC_CTX_FINI:
3034                 {
3035 #if 0
3036                         int rc;
3037
3038                         rc = mdt_handle_idmap(info);
3039                         if (rc)
3040                                 RETURN(rc);
3041                         else
3042 #endif
3043                                 RETURN(+1);
3044                 }
3045         }
3046
3047         if (unlikely(!class_connected_export(req->rq_export))) {
3048                 CERROR("operation %d on unconnected MDS from %s\n",
3049                        lustre_msg_get_opc(req->rq_reqmsg),
3050                        libcfs_id2str(req->rq_peer));
3051                 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3052                  * mds_A will get -ENOTCONN(especially for ping req),
3053                  * which will cause that mds_A deactive timeout, then when
3054                  * mds_A cleanup, the cleanup process will be suspended since
3055                  * deactive timeout is not zero.
3056                  */
3057                 req->rq_status = -ENOTCONN;
3058                 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3059                 RETURN(0);
3060         }
3061
3062         /* sanity check: if the xid matches, the request must be marked as a
3063          * resent or replayed */
3064         if (req_xid_is_last(req)) {
3065                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3066                       (MSG_RESENT | MSG_REPLAY))) {
3067                         DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3068                                   "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3069                                   lustre_msg_get_flags(req->rq_reqmsg));
3070                         LBUG();
3071                         req->rq_status = -ENOTCONN;
3072                         RETURN(-ENOTCONN);
3073                 }
3074         }
3075
3076         /* else: note the opposite is not always true; a RESENT req after a
3077          * failover will usually not match the last_xid, since it was likely
3078          * never committed. A REPLAYed request will almost never match the
3079          * last xid, however it could for a committed, but still retained,
3080          * open. */
3081
3082         obd = req->rq_export->exp_obd;
3083
3084         /* Check for aborted recovery... */
3085         if (unlikely(obd->obd_recovering)) {
3086                 int rc;
3087                 int should_process;
3088                 DEBUG_REQ(D_INFO, req, "Got new replay");
3089                 rc = mdt_filter_recovery_request(req, obd, &should_process);
3090                 if (rc != 0 || !should_process)
3091                         RETURN(rc);
3092                 else if (should_process < 0) {
3093                         req->rq_status = should_process;
3094                         rc = ptlrpc_error(req);
3095                         RETURN(rc);
3096                 }
3097         }
3098         RETURN(+1);
3099 }
3100
3101 static int mdt_msg_check_version(struct lustre_msg *msg)
3102 {
3103         int rc;
3104
3105         switch (lustre_msg_get_opc(msg)) {
3106         case MDS_CONNECT:
3107         case MDS_DISCONNECT:
3108         case OBD_PING:
3109         case SEC_CTX_INIT:
3110         case SEC_CTX_INIT_CONT:
3111         case SEC_CTX_FINI:
3112         case OBD_IDX_READ:
3113                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3114                 if (rc)
3115                         CERROR("bad opc %u version %08x, expecting %08x\n",
3116                                lustre_msg_get_opc(msg),
3117                                lustre_msg_get_version(msg),
3118                                LUSTRE_OBD_VERSION);
3119                 break;
3120         case MDS_GETSTATUS:
3121         case MDS_GETATTR:
3122         case MDS_GETATTR_NAME:
3123         case MDS_STATFS:
3124         case MDS_READPAGE:
3125         case MDS_WRITEPAGE:
3126         case MDS_IS_SUBDIR:
3127         case MDS_REINT:
3128         case MDS_CLOSE:
3129         case MDS_DONE_WRITING:
3130         case MDS_PIN:
3131         case MDS_SYNC:
3132         case MDS_GETXATTR:
3133         case MDS_SETXATTR:
3134         case MDS_SET_INFO:
3135         case MDS_GET_INFO:
3136         case MDS_QUOTACHECK:
3137         case MDS_QUOTACTL:
3138         case QUOTA_DQACQ:
3139         case QUOTA_DQREL:
3140         case SEQ_QUERY:
3141         case FLD_QUERY:
3142                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3143                 if (rc)
3144                         CERROR("bad opc %u version %08x, expecting %08x\n",
3145                                lustre_msg_get_opc(msg),
3146                                lustre_msg_get_version(msg),
3147                                LUSTRE_MDS_VERSION);
3148                 break;
3149         case LDLM_ENQUEUE:
3150         case LDLM_CONVERT:
3151         case LDLM_BL_CALLBACK:
3152         case LDLM_CP_CALLBACK:
3153                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3154                 if (rc)
3155                         CERROR("bad opc %u version %08x, expecting %08x\n",
3156                                lustre_msg_get_opc(msg),
3157                                lustre_msg_get_version(msg),
3158                                LUSTRE_DLM_VERSION);
3159                 break;
3160         case OBD_LOG_CANCEL:
3161         case LLOG_ORIGIN_HANDLE_CREATE:
3162         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3163         case LLOG_ORIGIN_HANDLE_READ_HEADER:
3164         case LLOG_ORIGIN_HANDLE_CLOSE:
3165         case LLOG_ORIGIN_HANDLE_DESTROY:
3166         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3167         case LLOG_CATINFO:
3168                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3169                 if (rc)
3170                         CERROR("bad opc %u version %08x, expecting %08x\n",
3171                                lustre_msg_get_opc(msg),
3172                                lustre_msg_get_version(msg),
3173                                LUSTRE_LOG_VERSION);
3174                 break;
3175         default:
3176                 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3177                 rc = -ENOTSUPP;
3178         }
3179         return rc;
3180 }
3181
3182 static int mdt_handle0(struct ptlrpc_request *req,
3183                        struct mdt_thread_info *info,
3184                        struct mdt_opc_slice *supported)
3185 {
3186         struct mdt_handler *h;
3187         struct lustre_msg  *msg;
3188         int                 rc;
3189
3190         ENTRY;
3191
3192         if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3193                 RETURN(0);
3194
3195         LASSERT(current->journal_info == NULL);
3196
3197         msg = req->rq_reqmsg;
3198         rc = mdt_msg_check_version(msg);
3199         if (likely(rc == 0)) {
3200                 rc = mdt_recovery(info);
3201                 if (likely(rc == +1)) {
3202                         h = mdt_handler_find(lustre_msg_get_opc(msg),
3203                                              supported);
3204                         if (likely(h != NULL)) {
3205                                 rc = mdt_req_handle(info, h, req);
3206                         } else {
3207                                 CERROR("The unsupported opc: 0x%x\n",
3208                                        lustre_msg_get_opc(msg) );
3209                                 req->rq_status = -ENOTSUPP;
3210                                 rc = ptlrpc_error(req);
3211                                 RETURN(rc);
3212                         }
3213                 }
3214         } else
3215                 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3216         RETURN(rc);
3217 }
3218
3219 /*
3220  * MDT handler function called by ptlrpc service thread when request comes.
3221  *
3222  * XXX common "target" functionality should be factored into separate module
3223  * shared by mdt, ost and stand-alone services like fld.
3224  */
3225 static int mdt_handle_common(struct ptlrpc_request *req,
3226                              struct mdt_opc_slice *supported)
3227 {
3228         struct lu_env          *env;
3229         struct mdt_thread_info *info;
3230         int                     rc;
3231         ENTRY;
3232
3233         env = req->rq_svc_thread->t_env;
3234         LASSERT(env != NULL);
3235         LASSERT(env->le_ses != NULL);
3236         LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3237         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3238         LASSERT(info != NULL);
3239
3240         mdt_thread_info_init(req, info);
3241
3242         rc = mdt_handle0(req, info, supported);
3243
3244         mdt_thread_info_fini(info);
3245         RETURN(rc);
3246 }
3247
3248 /*
3249  * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3250  * as well.
3251  */
3252 int mdt_recovery_handle(struct ptlrpc_request *req)
3253 {
3254         int rc;
3255         ENTRY;
3256
3257         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3258         case FLD_QUERY:
3259                 rc = mdt_handle_common(req, mdt_fld_handlers);
3260                 break;
3261         case SEQ_QUERY:
3262                 rc = mdt_handle_common(req, mdt_seq_handlers);
3263                 break;
3264         default:
3265                 rc = mdt_handle_common(req, mdt_regular_handlers);
3266                 break;
3267         }
3268
3269         RETURN(rc);
3270 }
3271
3272 static int mdt_regular_handle(struct ptlrpc_request *req)
3273 {
3274         return mdt_handle_common(req, mdt_regular_handlers);
3275 }
3276
3277 static int mdt_readpage_handle(struct ptlrpc_request *req)
3278 {
3279         return mdt_handle_common(req, mdt_readpage_handlers);
3280 }
3281
3282 static int mdt_xmds_handle(struct ptlrpc_request *req)
3283 {
3284         return mdt_handle_common(req, mdt_xmds_handlers);
3285 }
3286
3287 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3288 {
3289         return mdt_handle_common(req, mdt_seq_handlers);
3290 }
3291
3292 static int mdt_mdss_handle(struct ptlrpc_request *req)
3293 {
3294         return mdt_handle_common(req, mdt_seq_handlers);
3295 }
3296
3297 static int mdt_dtss_handle(struct ptlrpc_request *req)
3298 {
3299         return mdt_handle_common(req, mdt_seq_handlers);
3300 }
3301
3302 static int mdt_fld_handle(struct ptlrpc_request *req)
3303 {
3304         return mdt_handle_common(req, mdt_fld_handlers);
3305 }
3306
3307 enum mdt_it_code {
3308         MDT_IT_OPEN,
3309         MDT_IT_OCREAT,
3310         MDT_IT_CREATE,
3311         MDT_IT_GETATTR,
3312         MDT_IT_READDIR,
3313         MDT_IT_LOOKUP,
3314         MDT_IT_UNLINK,
3315         MDT_IT_TRUNC,
3316         MDT_IT_GETXATTR,
3317         MDT_IT_LAYOUT,
3318         MDT_IT_QUOTA,
3319         MDT_IT_NR
3320 };
3321
3322 static int mdt_intent_getattr(enum mdt_it_code opcode,
3323                               struct mdt_thread_info *info,
3324                               struct ldlm_lock **,
3325                               __u64);
3326 static int mdt_intent_reint(enum mdt_it_code opcode,
3327                             struct mdt_thread_info *info,
3328                             struct ldlm_lock **,
3329                             __u64);
3330
3331 static struct mdt_it_flavor {
3332         const struct req_format *it_fmt;
3333         __u32                    it_flags;
3334         int                    (*it_act)(enum mdt_it_code ,
3335                                          struct mdt_thread_info *,
3336                                          struct ldlm_lock **,
3337                                          __u64);
3338         long                     it_reint;
3339 } mdt_it_flavor[] = {
3340         [MDT_IT_OPEN]     = {
3341                 .it_fmt   = &RQF_LDLM_INTENT,
3342                 /*.it_flags = HABEO_REFERO,*/
3343                 .it_flags = 0,
3344                 .it_act   = mdt_intent_reint,
3345                 .it_reint = REINT_OPEN
3346         },
3347         [MDT_IT_OCREAT]   = {
3348                 .it_fmt   = &RQF_LDLM_INTENT,
3349                 .it_flags = MUTABOR,
3350                 .it_act   = mdt_intent_reint,
3351                 .it_reint = REINT_OPEN
3352         },
3353         [MDT_IT_CREATE]   = {
3354                 .it_fmt   = &RQF_LDLM_INTENT,
3355                 .it_flags = MUTABOR,
3356                 .it_act   = mdt_intent_reint,
3357                 .it_reint = REINT_CREATE
3358         },
3359         [MDT_IT_GETATTR]  = {
3360                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3361                 .it_flags = HABEO_REFERO,
3362                 .it_act   = mdt_intent_getattr
3363         },
3364         [MDT_IT_READDIR]  = {
3365                 .it_fmt   = NULL,
3366                 .it_flags = 0,
3367                 .it_act   = NULL
3368         },
3369         [MDT_IT_LOOKUP]   = {
3370                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3371                 .it_flags = HABEO_REFERO,
3372                 .it_act   = mdt_intent_getattr
3373         },
3374         [MDT_IT_UNLINK]   = {
3375                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
3376                 .it_flags = MUTABOR,
3377                 .it_act   = NULL,
3378                 .it_reint = REINT_UNLINK
3379         },
3380         [MDT_IT_TRUNC]    = {
3381                 .it_fmt   = NULL,
3382                 .it_flags = MUTABOR,
3383                 .it_act   = NULL
3384         },
3385         [MDT_IT_GETXATTR] = {
3386                 .it_fmt   = NULL,
3387                 .it_flags = 0,
3388                 .it_act   = NULL
3389         },
3390         [MDT_IT_LAYOUT] = {
3391                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3392                 .it_flags = HABEO_REFERO,
3393                 .it_act   = mdt_intent_getattr
3394         }
3395 };
3396
3397 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3398                             struct ldlm_lock **lockp,
3399                             struct ldlm_lock *new_lock,
3400                             struct mdt_lock_handle *lh,
3401                             __u64 flags)
3402 {
3403         struct ptlrpc_request  *req = mdt_info_req(info);
3404         struct ldlm_lock       *lock = *lockp;
3405
3406         /*
3407          * Get new lock only for cases when possible resent did not find any
3408          * lock.
3409          */
3410         if (new_lock == NULL)
3411                 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3412
3413         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3414                 lh->mlh_reg_lh.cookie = 0;
3415                 RETURN(0);
3416         }
3417
3418         LASSERTF(new_lock != NULL,
3419                  "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3420
3421         /*
3422          * If we've already given this lock to a client once, then we should
3423          * have no readers or writers.  Otherwise, we should have one reader
3424          * _or_ writer ref (which will be zeroed below) before returning the
3425          * lock to a client.
3426          */
3427         if (new_lock->l_export == req->rq_export) {
3428                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3429         } else {
3430                 LASSERT(new_lock->l_export == NULL);
3431                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3432         }
3433
3434         *lockp = new_lock;
3435
3436         if (new_lock->l_export == req->rq_export) {
3437                 /*
3438                  * Already gave this to the client, which means that we
3439                  * reconstructed a reply.
3440                  */
3441                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3442                         MSG_RESENT);
3443                 lh->mlh_reg_lh.cookie = 0;
3444                 RETURN(ELDLM_LOCK_REPLACED);
3445         }
3446
3447         /*
3448          * Fixup the lock to be given to the client.
3449          */
3450         lock_res_and_lock(new_lock);
3451         /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3452          * possible blocking AST. */
3453         while (new_lock->l_readers > 0) {
3454                 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3455                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3456                 new_lock->l_readers--;
3457         }
3458         while (new_lock->l_writers > 0) {
3459                 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3460                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3461                 new_lock->l_writers--;
3462         }
3463
3464         new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3465         new_lock->l_blocking_ast = lock->l_blocking_ast;
3466         new_lock->l_completion_ast = lock->l_completion_ast;
3467         new_lock->l_remote_handle = lock->l_remote_handle;
3468         new_lock->l_flags &= ~LDLM_FL_LOCAL;
3469
3470         unlock_res_and_lock(new_lock);
3471
3472         cfs_hash_add(new_lock->l_export->exp_lock_hash,
3473                      &new_lock->l_remote_handle,
3474                      &new_lock->l_exp_hash);
3475
3476         LDLM_LOCK_RELEASE(new_lock);
3477         lh->mlh_reg_lh.cookie = 0;
3478
3479         RETURN(ELDLM_LOCK_REPLACED);
3480 }
3481
3482 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3483                                     struct ldlm_lock *new_lock,
3484                                     struct ldlm_lock **old_lock,
3485                                     struct mdt_lock_handle *lh)
3486 {
3487         struct ptlrpc_request  *req = mdt_info_req(info);
3488         struct obd_export      *exp = req->rq_export;
3489         struct lustre_handle    remote_hdl;
3490         struct ldlm_request    *dlmreq;
3491         struct ldlm_lock       *lock;
3492
3493         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3494                 return;
3495
3496         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3497         remote_hdl = dlmreq->lock_handle[0];
3498
3499         /* In the function below, .hs_keycmp resolves to
3500          * ldlm_export_lock_keycmp() */
3501         /* coverity[overrun-buffer-val] */
3502         lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3503         if (lock) {
3504                 if (lock != new_lock) {
3505                         lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3506                         lh->mlh_reg_mode = lock->l_granted_mode;
3507
3508                         LDLM_DEBUG(lock, "Restoring lock cookie");
3509                         DEBUG_REQ(D_DLMTRACE, req,
3510                                   "restoring lock cookie "LPX64,
3511                                   lh->mlh_reg_lh.cookie);
3512                         if (old_lock)
3513                                 *old_lock = LDLM_LOCK_GET(lock);
3514                         cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3515                         return;
3516                 }
3517
3518                 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3519         }
3520
3521         /*
3522          * If the xid matches, then we know this is a resent request, and allow
3523          * it. (It's probably an OPEN, for which we don't send a lock.
3524          */
3525         if (req_xid_is_last(req))
3526                 return;
3527
3528         /*
3529          * This remote handle isn't enqueued, so we never received or processed
3530          * this request.  Clear MSG_RESENT, because it can be handled like any
3531          * normal request now.
3532          */
3533         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3534
3535         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3536                   remote_hdl.cookie);
3537 }
3538
3539 static int mdt_intent_getattr(enum mdt_it_code opcode,
3540                               struct mdt_thread_info *info,
3541                               struct ldlm_lock **lockp,
3542                               __u64 flags)
3543 {
3544         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3545         struct ldlm_lock       *new_lock = NULL;
3546         __u64                   child_bits;
3547         struct ldlm_reply      *ldlm_rep;
3548         struct ptlrpc_request  *req;
3549         struct mdt_body        *reqbody;
3550         struct mdt_body        *repbody;
3551         int                     rc, rc2;
3552         ENTRY;
3553
3554         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3555         LASSERT(reqbody);
3556
3557         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3558         LASSERT(repbody);
3559
3560         info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3561         repbody->eadatasize = 0;
3562         repbody->aclsize = 0;
3563
3564         switch (opcode) {
3565         case MDT_IT_LOOKUP:
3566                 child_bits = MDS_INODELOCK_LOOKUP;
3567                 break;
3568         case MDT_IT_GETATTR:
3569                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3570                 break;
3571         case MDT_IT_LAYOUT: {
3572                 static int printed = 0;
3573
3574                 if (!printed) {
3575                         CERROR("layout lock not supported by this version\n");
3576                         printed = 1;
3577                 }
3578                 GOTO(out_shrink, rc = -EINVAL);
3579                 break;
3580         }
3581         default:
3582                 CERROR("Unsupported intent (%d)\n", opcode);
3583                 GOTO(out_shrink, rc = -EINVAL);
3584         }
3585
3586         rc = mdt_init_ucred(info, reqbody);
3587         if (rc)
3588                 GOTO(out_shrink, rc);
3589
3590         req = info->mti_pill->rc_req;
3591         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3592         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3593
3594         /* Get lock from request for possible resent case. */
3595         mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3596
3597         ldlm_rep->lock_policy_res2 =
3598                 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3599
3600         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3601                 ldlm_rep->lock_policy_res2 = 0;
3602         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3603             ldlm_rep->lock_policy_res2) {
3604                 lhc->mlh_reg_lh.cookie = 0ull;
3605                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3606         }
3607
3608         rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3609         EXIT;
3610 out_ucred:
3611         mdt_exit_ucred(info);
3612 out_shrink:
3613         mdt_client_compatibility(info);
3614         rc2 = mdt_fix_reply(info);
3615         if (rc == 0)
3616                 rc = rc2;
3617         return rc;
3618 }
3619
3620 static int mdt_intent_reint(enum mdt_it_code opcode,
3621                             struct mdt_thread_info *info,
3622                             struct ldlm_lock **lockp,
3623                             __u64 flags)
3624 {
3625         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3626         struct ldlm_reply      *rep = NULL;
3627         long                    opc;
3628         int                     rc;
3629
3630         static const struct req_format *intent_fmts[REINT_MAX] = {
3631                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
3632                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
3633         };
3634
3635         ENTRY;
3636
3637         opc = mdt_reint_opcode(info, intent_fmts);
3638         if (opc < 0)
3639                 RETURN(opc);
3640
3641         if (mdt_it_flavor[opcode].it_reint != opc) {
3642                 CERROR("Reint code %ld doesn't match intent: %d\n",
3643                        opc, opcode);
3644                 RETURN(err_serious(-EPROTO));
3645         }
3646
3647         /* Get lock from request for possible resent case. */
3648         mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3649
3650         rc = mdt_reint_internal(info, lhc, opc);
3651
3652         /* Check whether the reply has been packed successfully. */
3653         if (mdt_info_req(info)->rq_repmsg != NULL)
3654                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3655         if (rep == NULL)
3656                 RETURN(err_serious(-EFAULT));
3657
3658         /* MDC expects this in any case */
3659         if (rc != 0)
3660                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3661
3662         /* Cross-ref case, the lock should be returned to the client */
3663         if (rc == -EREMOTE) {
3664                 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3665                 rep->lock_policy_res2 = 0;
3666                 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3667                 RETURN(rc);
3668         }
3669         rep->lock_policy_res2 = clear_serious(rc);
3670
3671         if (rep->lock_policy_res2 == -ENOENT &&
3672             mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3673                 rep->lock_policy_res2 = 0;
3674
3675         if (rc == -ENOTCONN || rc == -ENODEV ||
3676             rc == -EOVERFLOW) { /**< if VBR failure then return error */
3677                 /*
3678                  * If it is the disconnect error (ENODEV & ENOCONN), the error
3679                  * will be returned by rq_status, and client at ptlrpc layer
3680                  * will detect this, then disconnect, reconnect the import
3681                  * immediately, instead of impacting the following the rpc.
3682                  */
3683                 lhc->mlh_reg_lh.cookie = 0ull;
3684                 RETURN(rc);
3685         } else {
3686                 /*
3687                  * For other cases, the error will be returned by intent.
3688                  * and client will retrieve the result from intent.
3689                  */
3690                  /*
3691                   * FIXME: when open lock is finished, that should be
3692                   * checked here.
3693                   */
3694                 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3695                         LASSERTF(rc == 0, "Error occurred but lock handle "
3696                                  "is still in use\n");
3697                         rep->lock_policy_res2 = 0;
3698                         rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3699                         RETURN(rc);
3700                 } else {
3701                         lhc->mlh_reg_lh.cookie = 0ull;
3702                         RETURN(ELDLM_LOCK_ABORTED);
3703                 }
3704         }
3705 }
3706
3707 static int mdt_intent_code(long itcode)
3708 {
3709         int rc;
3710
3711         switch(itcode) {
3712         case IT_OPEN:
3713                 rc = MDT_IT_OPEN;
3714                 break;
3715         case IT_OPEN|IT_CREAT:
3716                 rc = MDT_IT_OCREAT;
3717                 break;
3718         case IT_CREAT:
3719                 rc = MDT_IT_CREATE;
3720                 break;
3721         case IT_READDIR:
3722                 rc = MDT_IT_READDIR;
3723                 break;
3724         case IT_GETATTR:
3725                 rc = MDT_IT_GETATTR;
3726                 break;
3727         case IT_LOOKUP:
3728                 rc = MDT_IT_LOOKUP;
3729                 break;
3730         case IT_UNLINK:
3731                 rc = MDT_IT_UNLINK;
3732                 break;
3733         case IT_TRUNC:
3734                 rc = MDT_IT_TRUNC;
3735                 break;
3736         case IT_GETXATTR:
3737                 rc = MDT_IT_GETXATTR;
3738                 break;
3739         case IT_LAYOUT:
3740                 rc = MDT_IT_LAYOUT;
3741                 break;
3742         case IT_QUOTA_DQACQ:
3743         case IT_QUOTA_CONN:
3744                 rc = MDT_IT_QUOTA;
3745                 break;
3746         default:
3747                 CERROR("Unknown intent opcode: %ld\n", itcode);
3748                 rc = -EINVAL;
3749                 break;
3750         }
3751         return rc;
3752 }
3753
3754 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3755                           struct ldlm_lock **lockp, __u64 flags)
3756 {
3757         struct req_capsule   *pill;
3758         struct mdt_it_flavor *flv;
3759         int opc;
3760         int rc;
3761         ENTRY;
3762
3763         opc = mdt_intent_code(itopc);
3764         if (opc < 0)
3765                 RETURN(-EINVAL);
3766
3767         pill = info->mti_pill;
3768
3769         if (opc == MDT_IT_QUOTA) {
3770                 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3771
3772                 if (qmt == NULL)
3773                         RETURN(-EOPNOTSUPP);
3774
3775                 /* pass the request to quota master */
3776                 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3777                                                  mdt_info_req(info), lockp,
3778                                                  flags);
3779                 RETURN(rc);
3780         }
3781
3782         flv  = &mdt_it_flavor[opc];
3783         if (flv->it_fmt != NULL)
3784                 req_capsule_extend(pill, flv->it_fmt);
3785
3786         rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3787         if (rc == 0) {
3788                 struct ptlrpc_request *req = mdt_info_req(info);
3789                 if (flv->it_flags & MUTABOR &&
3790                     req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3791                         RETURN(-EROFS);
3792         }
3793         if (rc == 0 && flv->it_act != NULL) {
3794                 /* execute policy */
3795                 rc = flv->it_act(opc, info, lockp, flags);
3796         } else {
3797                 rc = -EOPNOTSUPP;
3798         }
3799         RETURN(rc);
3800 }
3801
3802 static int mdt_intent_policy(struct ldlm_namespace *ns,
3803                              struct ldlm_lock **lockp, void *req_cookie,
3804                              ldlm_mode_t mode, __u64 flags, void *data)
3805 {
3806         struct mdt_thread_info *info;
3807         struct ptlrpc_request  *req  =  req_cookie;
3808         struct ldlm_intent     *it;
3809         struct req_capsule     *pill;
3810         int rc;
3811
3812         ENTRY;
3813
3814         LASSERT(req != NULL);
3815
3816         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3817                                   &mdt_thread_key);
3818         LASSERT(info != NULL);
3819         pill = info->mti_pill;
3820         LASSERT(pill->rc_req == req);
3821
3822         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3823                 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3824                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3825                 if (it != NULL) {
3826                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
3827                         if (rc == 0)
3828                                 rc = ELDLM_OK;
3829
3830                         /* Lock without inodebits makes no sense and will oops
3831                          * later in ldlm. Let's check it now to see if we have
3832                          * ibits corrupted somewhere in mdt_intent_opc().
3833                          * The case for client miss to set ibits has been
3834                          * processed by others. */
3835                         LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3836                                         lr_type == LDLM_IBITS,
3837                                      info->mti_dlm_req->lock_desc.\
3838                                         l_policy_data.l_inodebits.bits != 0));
3839                 } else
3840                         rc = err_serious(-EFAULT);
3841         } else {
3842                 /* No intent was provided */
3843                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3844                 rc = req_capsule_server_pack(pill);
3845                 if (rc)
3846                         rc = err_serious(rc);
3847         }
3848         RETURN(rc);
3849 }
3850
3851 static int mdt_seq_fini(const struct lu_env *env,
3852                         struct mdt_device *m)
3853 {
3854         struct md_site *ms = mdt_md_site(m);
3855         ENTRY;
3856
3857         if (ms != NULL) {
3858                 if (ms->ms_server_seq) {
3859                         seq_server_fini(ms->ms_server_seq, env);
3860                         OBD_FREE_PTR(ms->ms_server_seq);
3861                         ms->ms_server_seq = NULL;
3862         }
3863
3864                 if (ms->ms_control_seq) {
3865                         seq_server_fini(ms->ms_control_seq, env);
3866                         OBD_FREE_PTR(ms->ms_control_seq);
3867                         ms->ms_control_seq = NULL;
3868         }
3869
3870                 if (ms->ms_client_seq) {
3871                         seq_client_fini(ms->ms_client_seq);
3872                         OBD_FREE_PTR(ms->ms_client_seq);
3873                         ms->ms_client_seq = NULL;
3874                 }
3875         }
3876
3877         RETURN(0);
3878 }
3879
3880 static int mdt_seq_init(const struct lu_env *env,
3881                         const char *uuid,
3882                         struct mdt_device *m)
3883 {
3884         struct md_site *ms;
3885         char *prefix;
3886         int rc;
3887         ENTRY;
3888
3889         ms = mdt_md_site(m);
3890
3891         /*
3892          * This is sequence-controller node. Init seq-controller server on local
3893          * MDT.
3894          */
3895         if (ms->ms_node_id == 0) {
3896                 LASSERT(ms->ms_control_seq == NULL);
3897
3898                 OBD_ALLOC_PTR(ms->ms_control_seq);
3899                 if (ms->ms_control_seq == NULL)
3900                         RETURN(-ENOMEM);
3901
3902                 rc = seq_server_init(ms->ms_control_seq,
3903                                      m->mdt_bottom, uuid,
3904                                      LUSTRE_SEQ_CONTROLLER,
3905                                      ms,
3906                                      env);
3907
3908                 if (rc)
3909                         GOTO(out_seq_fini, rc);
3910
3911                 OBD_ALLOC_PTR(ms->ms_client_seq);
3912                 if (ms->ms_client_seq == NULL)
3913                         GOTO(out_seq_fini, rc = -ENOMEM);
3914
3915                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3916                 if (prefix == NULL) {
3917                         OBD_FREE_PTR(ms->ms_client_seq);
3918                         GOTO(out_seq_fini, rc = -ENOMEM);
3919                 }
3920
3921                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3922                          uuid);
3923
3924                 /*
3925                  * Init seq-controller client after seq-controller server is
3926                  * ready. Pass ms->ms_control_seq to it for direct talking.
3927                  */
3928                 rc = seq_client_init(ms->ms_client_seq, NULL,
3929                                      LUSTRE_SEQ_METADATA, prefix,
3930                                      ms->ms_control_seq);
3931                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3932
3933                 if (rc)
3934                         GOTO(out_seq_fini, rc);
3935         }
3936
3937         /* Init seq-server on local MDT */
3938         LASSERT(ms->ms_server_seq == NULL);
3939
3940         OBD_ALLOC_PTR(ms->ms_server_seq);
3941         if (ms->ms_server_seq == NULL)
3942                 GOTO(out_seq_fini, rc = -ENOMEM);
3943
3944         rc = seq_server_init(ms->ms_server_seq,
3945                              m->mdt_bottom, uuid,
3946                              LUSTRE_SEQ_SERVER,
3947                              ms,
3948                              env);
3949         if (rc)
3950                 GOTO(out_seq_fini, rc = -ENOMEM);
3951
3952         /* Assign seq-controller client to local seq-server. */
3953         if (ms->ms_node_id == 0) {
3954                 LASSERT(ms->ms_client_seq != NULL);
3955
3956                 rc = seq_server_set_cli(ms->ms_server_seq,
3957                                         ms->ms_client_seq,
3958                                         env);
3959         }
3960
3961         EXIT;
3962 out_seq_fini:
3963         if (rc)
3964                 mdt_seq_fini(env, m);
3965
3966         return rc;
3967 }
3968 /*
3969  * Init client sequence manager which is used by local MDS to talk to sequence
3970  * controller on remote node.
3971  */
3972 static int mdt_seq_init_cli(const struct lu_env *env,
3973                             struct mdt_device *m,
3974                             struct lustre_cfg *cfg)
3975 {
3976         struct md_site    *ms = mdt_md_site(m);
3977         struct obd_device *mdc;
3978         struct obd_uuid   *uuidp, *mdcuuidp;
3979         char              *uuid_str, *mdc_uuid_str;
3980         int                rc;
3981         int                index;
3982         struct mdt_thread_info *info;
3983         char *p, *index_string = lustre_cfg_string(cfg, 2);
3984         ENTRY;
3985
3986         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3987         uuidp = &info->mti_u.uuid[0];
3988         mdcuuidp = &info->mti_u.uuid[1];
3989
3990         LASSERT(index_string);
3991
3992         index = simple_strtol(index_string, &p, 10);
3993         if (*p) {
3994                 CERROR("Invalid index in lustre_cgf, offset 2\n");
3995                 RETURN(-EINVAL);
3996         }
3997
3998         /* check if this is adding the first MDC and controller is not yet
3999          * initialized. */
4000         if (index != 0 || ms->ms_client_seq)
4001                 RETURN(0);
4002
4003         uuid_str = lustre_cfg_string(cfg, 1);
4004         mdc_uuid_str = lustre_cfg_string(cfg, 4);
4005         obd_str2uuid(uuidp, uuid_str);
4006         obd_str2uuid(mdcuuidp, mdc_uuid_str);
4007
4008         mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
4009         if (!mdc) {
4010                 CERROR("can't find controller MDC by uuid %s\n",
4011                        uuid_str);
4012                 rc = -ENOENT;
4013         } else if (!mdc->obd_set_up) {
4014                 CERROR("target %s not set up\n", mdc->obd_name);
4015                 rc = -EINVAL;
4016         } else {
4017                 LASSERT(ms->ms_control_exp);
4018                 OBD_ALLOC_PTR(ms->ms_client_seq);
4019                 if (ms->ms_client_seq != NULL) {
4020                         char *prefix;
4021
4022                         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4023                         if (!prefix)
4024                                 RETURN(-ENOMEM);
4025
4026                         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4027                                  mdc->obd_name);
4028
4029                         rc = seq_client_init(ms->ms_client_seq,
4030                                              ms->ms_control_exp,
4031                                              LUSTRE_SEQ_METADATA,
4032                                              prefix, NULL);
4033                         OBD_FREE(prefix, MAX_OBD_NAME + 5);
4034                 } else
4035                         rc = -ENOMEM;
4036
4037                 if (rc)
4038                         RETURN(rc);
4039
4040                 LASSERT(ms->ms_server_seq != NULL);
4041                 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
4042                                         env);
4043         }
4044
4045         RETURN(rc);
4046 }
4047
4048 static void mdt_seq_fini_cli(struct mdt_device *m)
4049 {
4050         struct md_site *ms;
4051
4052         ENTRY;
4053
4054         ms = mdt_md_site(m);
4055
4056         if (ms != NULL) {
4057                 if (ms->ms_server_seq)
4058                         seq_server_set_cli(ms->ms_server_seq,
4059                                    NULL, NULL);
4060
4061                 if (ms->ms_control_exp) {
4062                         class_export_put(ms->ms_control_exp);
4063                         ms->ms_control_exp = NULL;
4064                 }
4065         }
4066         EXIT;
4067 }
4068
4069 /*
4070  * FLD wrappers
4071  */
4072 static int mdt_fld_fini(const struct lu_env *env,
4073                         struct mdt_device *m)
4074 {
4075         struct md_site *ms = mdt_md_site(m);
4076         ENTRY;
4077
4078         if (ms && ms->ms_server_fld) {
4079                 fld_server_fini(ms->ms_server_fld, env);
4080                 OBD_FREE_PTR(ms->ms_server_fld);
4081                 ms->ms_server_fld = NULL;
4082         }
4083
4084         RETURN(0);
4085 }
4086
4087 static int mdt_fld_init(const struct lu_env *env,
4088                         const char *uuid,
4089                         struct mdt_device *m)
4090 {
4091         struct md_site *ms;
4092         int rc;
4093         ENTRY;
4094
4095         ms = mdt_md_site(m);
4096
4097         OBD_ALLOC_PTR(ms->ms_server_fld);
4098         if (ms->ms_server_fld == NULL)
4099                 RETURN(rc = -ENOMEM);
4100
4101         rc = fld_server_init(ms->ms_server_fld,
4102                              m->mdt_bottom, uuid,
4103                              env, ms->ms_node_id);
4104         if (rc) {
4105                 OBD_FREE_PTR(ms->ms_server_fld);
4106                 ms->ms_server_fld = NULL;
4107                 RETURN(rc);
4108         }
4109
4110         RETURN(0);
4111 }
4112
4113 /* device init/fini methods */
4114 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
4115 {
4116         ENTRY;
4117         if (m->mdt_regular_service != NULL) {
4118                 ptlrpc_unregister_service(m->mdt_regular_service);
4119                 m->mdt_regular_service = NULL;
4120         }
4121         if (m->mdt_readpage_service != NULL) {
4122                 ptlrpc_unregister_service(m->mdt_readpage_service);
4123                 m->mdt_readpage_service = NULL;
4124         }
4125         if (m->mdt_xmds_service != NULL) {
4126                 ptlrpc_unregister_service(m->mdt_xmds_service);
4127                 m->mdt_xmds_service = NULL;
4128         }
4129         if (m->mdt_setattr_service != NULL) {
4130                 ptlrpc_unregister_service(m->mdt_setattr_service);
4131                 m->mdt_setattr_service = NULL;
4132         }
4133         if (m->mdt_mdsc_service != NULL) {
4134                 ptlrpc_unregister_service(m->mdt_mdsc_service);
4135                 m->mdt_mdsc_service = NULL;
4136         }
4137         if (m->mdt_mdss_service != NULL) {
4138                 ptlrpc_unregister_service(m->mdt_mdss_service);
4139                 m->mdt_mdss_service = NULL;
4140         }
4141         if (m->mdt_dtss_service != NULL) {
4142                 ptlrpc_unregister_service(m->mdt_dtss_service);
4143                 m->mdt_dtss_service = NULL;
4144         }
4145         if (m->mdt_fld_service != NULL) {
4146                 ptlrpc_unregister_service(m->mdt_fld_service);
4147                 m->mdt_fld_service = NULL;
4148         }
4149         EXIT;
4150 }
4151
4152 static int mdt_start_ptlrpc_service(struct mdt_device *m)
4153 {
4154         static struct ptlrpc_service_conf conf;
4155         cfs_proc_dir_entry_t *procfs_entry;
4156         int rc = 0;
4157         ENTRY;
4158
4159         m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
4160         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4161                            "mdt_ldlm_client", m->mdt_ldlm_client);
4162
4163         procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
4164
4165         conf = (typeof(conf)) {
4166                 .psc_name               = LUSTRE_MDT_NAME,
4167                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4168                 .psc_buf                = {
4169                         .bc_nbufs               = MDS_NBUFS,
4170                         .bc_buf_size            = MDS_BUFSIZE,
4171                         .bc_req_max_size        = MDS_MAXREQSIZE,
4172                         .bc_rep_max_size        = MDS_MAXREPSIZE,
4173                         .bc_req_portal          = MDS_REQUEST_PORTAL,
4174                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4175                 },
4176                 /*
4177                  * We'd like to have a mechanism to set this on a per-device
4178                  * basis, but alas...
4179                  */
4180                 .psc_thr                = {
4181                         .tc_thr_name            = LUSTRE_MDT_NAME,
4182                         .tc_thr_factor          = MDT_THR_FACTOR,
4183                         .tc_nthrs_init          = MDT_NTHRS_INIT,
4184                         .tc_nthrs_base          = MDT_NTHRS_BASE,
4185                         .tc_nthrs_max           = MDT_NTHRS_MAX,
4186                         .tc_nthrs_user          = mds_num_threads,
4187                         .tc_cpu_affinity        = 1,
4188                         .tc_ctx_tags            = LCT_MD_THREAD,
4189                 },
4190                 .psc_cpt                = {
4191                         .cc_pattern             = mds_num_cpts,
4192                 },
4193                 .psc_ops                = {
4194                         .so_req_handler         = mdt_regular_handle,
4195                         .so_req_printer         = target_print_req,
4196                         .so_hpreq_handler       = ptlrpc_hpreq_handler,
4197                 },
4198         };
4199         m->mdt_regular_service = ptlrpc_register_service(&conf, procfs_entry);
4200         if (IS_ERR(m->mdt_regular_service)) {
4201                 rc = PTR_ERR(m->mdt_regular_service);
4202                 CERROR("failed to start regular mdt service: %d\n", rc);
4203                 m->mdt_regular_service = NULL;
4204
4205                 RETURN(rc);
4206         }
4207
4208         /*
4209          * readpage service configuration. Parameters have to be adjusted,
4210          * ideally.
4211          */
4212         memset(&conf, 0, sizeof(conf));
4213         conf = (typeof(conf)) {
4214                 .psc_name               = LUSTRE_MDT_NAME "_readpage",
4215                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4216                 .psc_buf                = {
4217                         .bc_nbufs               = MDS_NBUFS,
4218                         .bc_buf_size            = MDS_BUFSIZE,
4219                         .bc_req_max_size        = MDS_MAXREQSIZE,
4220                         .bc_rep_max_size        = MDS_MAXREPSIZE,
4221                         .bc_req_portal          = MDS_READPAGE_PORTAL,
4222                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4223                 },
4224                 .psc_thr                = {
4225                         .tc_thr_name            = "mdt_rdpg",
4226                         .tc_thr_factor          = MDT_RDPG_THR_FACTOR,
4227                         .tc_nthrs_init          = MDT_RDPG_NTHRS_INIT,
4228                         .tc_nthrs_base          = MDT_RDPG_NTHRS_BASE,
4229                         .tc_nthrs_max           = MDT_RDPG_NTHRS_MAX,
4230                         .tc_nthrs_user          = mds_rdpg_num_threads,
4231                         .tc_cpu_affinity        = 1,
4232                         .tc_ctx_tags            = LCT_MD_THREAD,
4233                 },
4234                 .psc_cpt                = {
4235                         .cc_pattern             = mds_rdpg_num_cpts,
4236                 },
4237                 .psc_ops                = {
4238                         .so_req_handler         = mdt_readpage_handle,
4239                         .so_req_printer         = target_print_req,
4240                 },
4241         };
4242         m->mdt_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
4243         if (IS_ERR(m->mdt_readpage_service)) {
4244                 rc = PTR_ERR(m->mdt_readpage_service);
4245                 CERROR("failed to start readpage service: %d\n", rc);
4246                 m->mdt_readpage_service = NULL;
4247
4248                 GOTO(err_mdt_svc, rc);
4249         }
4250
4251         /*
4252          * setattr service configuration.
4253          *
4254          * XXX To keep the compatibility with old client(< 2.2), we need to
4255          * preserve this portal for a certain time, it should be removed
4256          * eventually. LU-617.
4257          */
4258         memset(&conf, 0, sizeof(conf));
4259         conf = (typeof(conf)) {
4260                 .psc_name               = LUSTRE_MDT_NAME "_setattr",
4261                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4262                 .psc_buf                = {
4263                         .bc_nbufs               = MDS_NBUFS,
4264                         .bc_buf_size            = MDS_BUFSIZE,
4265                         .bc_req_max_size        = MDS_MAXREQSIZE,
4266                         .bc_rep_max_size        = MDS_MAXREPSIZE,
4267                         .bc_req_portal          = MDS_SETATTR_PORTAL,
4268                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4269                 },
4270                 .psc_thr                = {
4271                         .tc_thr_name            = "mdt_attr",
4272                         .tc_thr_factor          = MDT_SETA_THR_FACTOR,
4273                         .tc_nthrs_init          = MDT_SETA_NTHRS_INIT,
4274                         .tc_nthrs_base          = MDT_SETA_NTHRS_BASE,
4275                         .tc_nthrs_max           = MDT_SETA_NTHRS_MAX,
4276                         .tc_nthrs_user          = mds_attr_num_threads,
4277                         .tc_cpu_affinity        = 1,
4278                         .tc_ctx_tags            = LCT_MD_THREAD,
4279                 },
4280                 .psc_cpt                = {
4281                         .cc_pattern             = mds_attr_num_cpts,
4282                 },
4283                 .psc_ops                = {
4284                         .so_req_handler         = mdt_regular_handle,
4285                         .so_req_printer         = target_print_req,
4286                 },
4287         };
4288         m->mdt_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
4289         if (IS_ERR(m->mdt_setattr_service)) {
4290                 rc = PTR_ERR(m->mdt_setattr_service);
4291                 CERROR("failed to start setattr service: %d\n", rc);
4292                 m->mdt_setattr_service = NULL;
4293
4294                 GOTO(err_mdt_svc, rc);
4295         }
4296
4297         /*
4298          * sequence controller service configuration
4299          */
4300         memset(&conf, 0, sizeof(conf));
4301         conf = (typeof(conf)) {
4302                 .psc_name               = LUSTRE_MDT_NAME "_mdsc",
4303                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4304                 .psc_buf                = {
4305                         .bc_nbufs               = MDS_NBUFS,
4306                         .bc_buf_size            = MDS_BUFSIZE,
4307                         .bc_req_max_size        = SEQ_MAXREQSIZE,
4308                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
4309                         .bc_req_portal          = SEQ_CONTROLLER_PORTAL,
4310                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4311                 },
4312                 .psc_thr                = {
4313                         .tc_thr_name            = "mdt_mdsc",
4314                         .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
4315                         .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
4316                         .tc_ctx_tags            = LCT_MD_THREAD,
4317                 },
4318                 .psc_ops                = {
4319                         .so_req_handler         = mdt_mdsc_handle,
4320                         .so_req_printer         = target_print_req,
4321                 },
4322         };
4323         m->mdt_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
4324         if (IS_ERR(m->mdt_mdsc_service)) {
4325                 rc = PTR_ERR(m->mdt_mdsc_service);
4326                 CERROR("failed to start seq controller service: %d\n", rc);
4327                 m->mdt_mdsc_service = NULL;
4328
4329                 GOTO(err_mdt_svc, rc);
4330         }
4331
4332         /*
4333          * metadata sequence server service configuration
4334          */
4335         memset(&conf, 0, sizeof(conf));
4336         conf = (typeof(conf)) {
4337                 .psc_name               = LUSTRE_MDT_NAME "_mdss",
4338                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4339                 .psc_buf                = {
4340                         .bc_nbufs               = MDS_NBUFS,
4341                         .bc_buf_size            = MDS_BUFSIZE,
4342                         .bc_req_max_size        = SEQ_MAXREQSIZE,
4343                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
4344                         .bc_req_portal          = SEQ_METADATA_PORTAL,
4345                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4346                 },
4347                 .psc_thr                = {
4348                         .tc_thr_name            = "mdt_mdss",
4349                         .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
4350                         .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
4351                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
4352                 },
4353                 .psc_ops                = {
4354                         .so_req_handler         = mdt_mdss_handle,
4355                         .so_req_printer         = target_print_req,
4356                 },
4357         };
4358         m->mdt_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
4359         if (IS_ERR(m->mdt_mdss_service)) {
4360                 rc = PTR_ERR(m->mdt_mdss_service);
4361                 CERROR("failed to start metadata seq server service: %d\n", rc);
4362                 m->mdt_mdss_service = NULL;
4363
4364                 GOTO(err_mdt_svc, rc);
4365         }
4366
4367         /*
4368          * Data sequence server service configuration. We want to have really
4369          * cluster-wide sequences space. This is why we start only one sequence
4370          * controller which manages space.
4371          */
4372         memset(&conf, 0, sizeof(conf));
4373         conf = (typeof(conf)) {
4374                 .psc_name               = LUSTRE_MDT_NAME "_dtss",
4375                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4376                 .psc_buf                = {
4377                         .bc_nbufs               = MDS_NBUFS,
4378                         .bc_buf_size            = MDS_BUFSIZE,
4379                         .bc_req_max_size        = SEQ_MAXREQSIZE,
4380                         .bc_rep_max_size        = SEQ_MAXREPSIZE,
4381                         .bc_req_portal          = SEQ_DATA_PORTAL,
4382                         .bc_rep_portal          = OSC_REPLY_PORTAL,
4383                 },
4384                 .psc_thr                = {
4385                         .tc_thr_name            = "mdt_dtss",
4386                         .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
4387                         .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
4388                         .tc_ctx_tags            = LCT_MD_THREAD | LCT_DT_THREAD
4389                 },
4390                 .psc_ops                = {
4391                         .so_req_handler         = mdt_dtss_handle,
4392                         .so_req_printer         = target_print_req,
4393                 },
4394         };
4395         m->mdt_dtss_service = ptlrpc_register_service(&conf, procfs_entry);
4396         if (IS_ERR(m->mdt_dtss_service)) {
4397                 rc = PTR_ERR(m->mdt_dtss_service);
4398                 CERROR("failed to start data seq server service: %d\n", rc);
4399                 m->mdt_dtss_service = NULL;
4400
4401                 GOTO(err_mdt_svc, rc);
4402         }
4403
4404         /* FLD service start */
4405         memset(&conf, 0, sizeof(conf));
4406         conf = (typeof(conf)) {
4407                 .psc_name            = LUSTRE_MDT_NAME "_fld",
4408                 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4409                 .psc_buf                = {
4410                         .bc_nbufs               = MDS_NBUFS,
4411                         .bc_buf_size            = MDS_BUFSIZE,
4412                         .bc_req_max_size        = FLD_MAXREQSIZE,
4413                         .bc_rep_max_size        = FLD_MAXREPSIZE,
4414                         .bc_req_portal          = FLD_REQUEST_PORTAL,
4415                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4416                 },
4417                 .psc_thr                = {
4418                         .tc_thr_name            = "mdt_fld",
4419                         .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
4420                         .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
4421                         .tc_ctx_tags            = LCT_DT_THREAD | LCT_MD_THREAD
4422                 },
4423                 .psc_ops                = {
4424                         .so_req_handler         = mdt_fld_handle,
4425                         .so_req_printer         = target_print_req,
4426                 },
4427         };
4428         m->mdt_fld_service = ptlrpc_register_service(&conf, procfs_entry);
4429         if (IS_ERR(m->mdt_fld_service)) {
4430                 rc = PTR_ERR(m->mdt_fld_service);
4431                 CERROR("failed to start fld service: %d\n", rc);
4432                 m->mdt_fld_service = NULL;
4433
4434                 GOTO(err_mdt_svc, rc);
4435         }
4436
4437         /*
4438          * mds-mds service configuration. Separate portal is used to allow
4439          * mds-mds requests be not blocked during recovery.
4440          */
4441         memset(&conf, 0, sizeof(conf));
4442         conf = (typeof(conf)) {
4443                 .psc_name               = LUSTRE_MDT_NAME "_mds",
4444                 .psc_watchdog_factor    = MDT_SERVICE_WATCHDOG_FACTOR,
4445                 .psc_buf                = {
4446                         .bc_nbufs               = MDS_NBUFS,
4447                         .bc_buf_size            = MDS_BUFSIZE,
4448                         .bc_req_max_size        = MDS_MAXREQSIZE,
4449                         .bc_rep_max_size        = MDS_MAXREPSIZE,
4450                         .bc_req_portal          = MDS_MDS_PORTAL,
4451                         .bc_rep_portal          = MDC_REPLY_PORTAL,
4452                 },
4453                 .psc_thr                = {
4454                         .tc_thr_name            = "mdt_mds",
4455                         .tc_nthrs_init          = MDT_OTHR_NTHRS_INIT,
4456                         .tc_nthrs_max           = MDT_OTHR_NTHRS_MAX,
4457                         .tc_ctx_tags            = LCT_MD_THREAD,
4458                 },
4459                 .psc_ops                = {
4460                         .so_req_handler         = mdt_xmds_handle,
4461                         .so_req_printer         = target_print_req,
4462                         .so_hpreq_handler       = ptlrpc_hpreq_handler,
4463                 },
4464         };
4465         m->mdt_xmds_service = ptlrpc_register_service(&conf, procfs_entry);
4466         if (IS_ERR(m->mdt_xmds_service)) {
4467                 rc = PTR_ERR(m->mdt_xmds_service);
4468                 CERROR("failed to start xmds service: %d\n", rc);
4469                 m->mdt_xmds_service = NULL;
4470
4471                 GOTO(err_mdt_svc, rc);
4472         }
4473
4474         EXIT;
4475 err_mdt_svc:
4476         if (rc)
4477                 mdt_stop_ptlrpc_service(m);
4478
4479         return rc;
4480 }
4481
4482 static void mdt_stack_fini(const struct lu_env *env,
4483                            struct mdt_device *m, struct lu_device *top)
4484 {
4485         struct obd_device       *obd = mdt2obd_dev(m);
4486         struct lustre_cfg_bufs  *bufs;
4487         struct lustre_cfg       *lcfg;
4488         struct mdt_thread_info  *info;
4489         char flags[3]="";
4490         ENTRY;
4491
4492         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4493         LASSERT(info != NULL);
4494
4495         lu_dev_del_linkage(top->ld_site, top);
4496
4497         lu_site_purge(env, top->ld_site, -1);
4498
4499         bufs = &info->mti_u.bufs;
4500         /* process cleanup, pass mdt obd name to get obd umount flags */
4501         /* another purpose is to let all layers to release their objects */
4502         lustre_cfg_bufs_reset(bufs, obd->obd_name);
4503         if (obd->obd_force)
4504                 strcat(flags, "F");
4505         if (obd->obd_fail)
4506                 strcat(flags, "A");
4507         lustre_cfg_bufs_set_string(bufs, 1, flags);
4508         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4509         if (!lcfg) {
4510                 CERROR("Cannot alloc lcfg!\n");
4511                 return;
4512         }
4513         LASSERT(top);
4514         top->ld_ops->ldo_process_config(env, top, lcfg);
4515         lustre_cfg_free(lcfg);
4516
4517         lu_site_purge(env, top->ld_site, -1);
4518
4519         m->mdt_child = NULL;
4520         m->mdt_bottom = NULL;
4521
4522         obd_disconnect(m->mdt_child_exp);
4523         m->mdt_child_exp = NULL;
4524
4525         obd_disconnect(m->mdt_bottom_exp);
4526         m->mdt_child_exp = NULL;
4527 }
4528
4529 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4530                                const char *next, struct obd_export **exp)
4531 {
4532         struct obd_connect_data *data = NULL;
4533         struct obd_device       *obd;
4534         int                      rc;
4535         ENTRY;
4536
4537         OBD_ALLOC_PTR(data);
4538         if (data == NULL)
4539                 GOTO(out, rc = -ENOMEM);
4540
4541         obd = class_name2obd(next);
4542         if (obd == NULL) {
4543                 CERROR("%s: can't locate next device: %s\n",
4544                        m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4545                 GOTO(out, rc = -ENOTCONN);
4546         }
4547
4548         data->ocd_connect_flags = OBD_CONNECT_VERSION;
4549         data->ocd_version = LUSTRE_VERSION_CODE;
4550
4551         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4552         if (rc) {
4553                 CERROR("%s: cannot connect to next dev %s (%d)\n",
4554                        m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4555                 GOTO(out, rc);
4556         }
4557
4558 out:
4559         if (data)
4560                 OBD_FREE_PTR(data);
4561         RETURN(rc);
4562 }
4563
4564 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4565                           struct lustre_cfg *cfg)
4566 {
4567         char                   *dev = lustre_cfg_string(cfg, 0);
4568         int                     rc, name_size, uuid_size;
4569         char                   *name, *uuid, *p;
4570         struct lustre_cfg_bufs *bufs;
4571         struct lustre_cfg      *lcfg;
4572         struct obd_device      *obd;
4573         struct lustre_profile  *lprof;
4574         struct lu_site         *site;
4575         ENTRY;
4576
4577         /* in 1.8 we had the only device in the stack - MDS.
4578          * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4579          * in 2.3 OSD is instantiated by obd_mount.c, so we need
4580          * to generate names and setup MDT, MDD. MDT will be using
4581          * generated name to connect to MDD. for MDD the next device
4582          * will be LOD with name taken from so called "profile" which
4583          * is generated by mount_option line
4584          *
4585          * 1.8 MGS generates config. commands like this:
4586          *   #06 (104)mount_option 0:  1:lustre-MDT0000  2:lustre-mdtlov
4587          *   #08 (120)setup   0:lustre-MDT0000  1:dev 2:type 3:lustre-MDT0000
4588          * 2.0 MGS generates config. commands like this:
4589          *   #07 (112)mount_option 0:  1:lustre-MDT0000  2:lustre-MDT0000-mdtlov
4590          *   #08 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4591          *                    3:lustre-MDT0000-mdtlov  4:f
4592          *
4593          * we generate MDD name from MDT one, just replacing T with D
4594          *
4595          * after all the preparations, the logical equivalent will be
4596          *   #01 (160)setup   0:lustre-MDD0000  1:lustre-MDD0000_UUID  2:0
4597          *                    3:lustre-MDT0000-mdtlov  4:f
4598          *   #02 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4599          *                    3:lustre-MDD0000  4:f
4600          *
4601          *  notice we build the stack from down to top: MDD first, then MDT */
4602
4603         name_size = MAX_OBD_NAME;
4604         uuid_size = MAX_OBD_NAME;
4605
4606         OBD_ALLOC(name, name_size);
4607         OBD_ALLOC(uuid, uuid_size);
4608         if (name == NULL || uuid == NULL)
4609                 GOTO(cleanup_mem, rc = -ENOMEM);
4610
4611         OBD_ALLOC_PTR(bufs);
4612         if (!bufs)
4613                 GOTO(cleanup_mem, rc = -ENOMEM);
4614
4615         strcpy(name, dev);
4616         p = strstr(name, "-MDT");
4617         if (p == NULL)
4618                 GOTO(cleanup_mem, rc = -ENOMEM);
4619         p[3] = 'D';
4620
4621         snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4622
4623         lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4624         if (lprof == NULL || lprof->lp_dt == NULL) {
4625                 CERROR("can't find the profile: %s\n",
4626                        lustre_cfg_string(cfg, 0));
4627                 GOTO(cleanup_mem, rc = -EINVAL);
4628         }
4629
4630         lustre_cfg_bufs_reset(bufs, name);
4631         lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4632         lustre_cfg_bufs_set_string(bufs, 2, uuid);
4633         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4634
4635         lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4636         if (!lcfg)
4637                 GOTO(free_bufs, rc = -ENOMEM);
4638
4639         rc = class_attach(lcfg);
4640         if (rc)
4641                 GOTO(lcfg_cleanup, rc);
4642
4643         obd = class_name2obd(name);
4644         if (!obd) {
4645                 CERROR("Can not find obd %s (%s in config)\n",
4646                        MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4647                 GOTO(class_detach, rc = -EINVAL);
4648         }
4649
4650         lustre_cfg_free(lcfg);
4651
4652         lustre_cfg_bufs_reset(bufs, name);
4653         lustre_cfg_bufs_set_string(bufs, 1, uuid);
4654         lustre_cfg_bufs_set_string(bufs, 2, dev);
4655         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4656
4657         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4658
4659         rc = class_setup(obd, lcfg);
4660         if (rc)
4661                 GOTO(class_detach, rc);
4662
4663         /* connect to MDD we just setup */
4664         rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4665         if (rc)
4666                 RETURN(rc);
4667
4668         site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4669         LASSERT(site);
4670         LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
4671         mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4672         site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4673         mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4674
4675
4676         /* now connect to bottom OSD */
4677         snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4678         rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4679         if (rc)
4680                 RETURN(rc);
4681         mdt->mdt_bottom =
4682                 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4683
4684
4685         rc = lu_env_refill((struct lu_env *)env);
4686         if (rc != 0)
4687                 CERROR("Failure to refill session: '%d'\n", rc);
4688
4689         lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4690
4691         EXIT;
4692 class_detach:
4693         if (rc)
4694                 class_detach(obd, lcfg);
4695 lcfg_cleanup:
4696         lustre_cfg_free(lcfg);
4697 free_bufs:
4698         OBD_FREE_PTR(bufs);
4699 cleanup_mem:
4700         if (name)
4701                 OBD_FREE(name, name_size);
4702         if (uuid)
4703                 OBD_FREE(uuid, uuid_size);
4704         RETURN(rc);
4705 }
4706
4707 /* setup quota master target on MDT0 */
4708 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4709                           struct lustre_cfg *cfg)
4710 {
4711         struct obd_device       *obd;
4712         char                    *dev = lustre_cfg_string(cfg, 0);
4713         char                    *qmtname, *uuid, *p;
4714         struct lustre_cfg_bufs  *bufs;
4715         struct lustre_cfg       *lcfg;
4716         struct lustre_profile   *lprof;
4717         struct obd_connect_data *data;
4718         int                      rc;
4719         ENTRY;
4720
4721         LASSERT(mdt->mdt_qmt_exp == NULL);
4722         LASSERT(mdt->mdt_qmt_dev == NULL);
4723
4724         /* quota master is on MDT0 only for now */
4725         if (mdt->mdt_mite.ms_node_id != 0)
4726                 RETURN(0);
4727
4728         /* MGS generates config commands which look as follows:
4729          *   #01 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4730          *                    3:lustre-MDT0000-mdtlov  4:f
4731          *
4732          * We generate the QMT name from the MDT one, just replacing MD with QM
4733          * after all the preparations, the logical equivalent will be:
4734          *   #01 (160)setup   0:lustre-QMT0000  1:lustre-QMT0000_UUID  2:0
4735          *                    3:lustre-MDT0000-osd  4:f */
4736         OBD_ALLOC(qmtname, MAX_OBD_NAME);
4737         OBD_ALLOC(uuid, UUID_MAX);
4738         OBD_ALLOC_PTR(bufs);
4739         OBD_ALLOC_PTR(data);
4740         if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4741                 GOTO(cleanup_mem, rc = -ENOMEM);
4742
4743         strcpy(qmtname, dev);
4744         p = strstr(qmtname, "-MDT");
4745         if (p == NULL)
4746                 GOTO(cleanup_mem, rc = -ENOMEM);
4747         /* replace MD with QM */
4748         p[1] = 'Q';
4749         p[2] = 'M';
4750
4751         snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4752
4753         lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4754         if (lprof == NULL || lprof->lp_dt == NULL) {
4755                 CERROR("can't find profile for %s\n",
4756                        lustre_cfg_string(cfg, 0));
4757                 GOTO(cleanup_mem, rc = -EINVAL);
4758         }
4759
4760         lustre_cfg_bufs_reset(bufs, qmtname);
4761         lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4762         lustre_cfg_bufs_set_string(bufs, 2, uuid);
4763         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4764
4765         lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4766         if (!lcfg)
4767                 GOTO(cleanup_mem, rc = -ENOMEM);
4768
4769         rc = class_attach(lcfg);
4770         if (rc)
4771                 GOTO(lcfg_cleanup, rc);
4772
4773         obd = class_name2obd(qmtname);
4774         if (!obd) {
4775                 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4776                        lustre_cfg_string(cfg, 0));
4777                 GOTO(class_detach, rc = -EINVAL);
4778         }
4779
4780         lustre_cfg_free(lcfg);
4781
4782         lustre_cfg_bufs_reset(bufs, qmtname);
4783         lustre_cfg_bufs_set_string(bufs, 1, uuid);
4784         lustre_cfg_bufs_set_string(bufs, 2, dev);
4785
4786         /* for quota, the next device should be the OSD device */
4787         lustre_cfg_bufs_set_string(bufs, 3,
4788                                    mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4789
4790         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4791
4792         rc = class_setup(obd, lcfg);
4793         if (rc)
4794                 GOTO(class_detach, rc);
4795
4796         mdt->mdt_qmt_dev = obd->obd_lu_dev;
4797
4798         /* configure local quota objects */
4799         rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4800                                                    &mdt->mdt_md_dev.md_lu_dev,
4801                                                    mdt->mdt_qmt_dev);
4802         if (rc)
4803                 GOTO(class_cleanup, rc);
4804
4805         /* connect to quota master target */
4806         data->ocd_connect_flags = OBD_CONNECT_VERSION;
4807         data->ocd_version = LUSTRE_VERSION_CODE;
4808         rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4809                          data, NULL);
4810         if (rc) {
4811                 CERROR("cannot connect to quota master device %s (%d)\n",
4812                        qmtname, rc);
4813                 GOTO(class_cleanup, rc);
4814         }
4815
4816         EXIT;
4817 class_cleanup:
4818         if (rc) {
4819                 class_manual_cleanup(obd);
4820                 mdt->mdt_qmt_dev = NULL;
4821         }
4822 class_detach:
4823         if (rc)
4824                 class_detach(obd, lcfg);
4825 lcfg_cleanup:
4826         lustre_cfg_free(lcfg);
4827 cleanup_mem:
4828         if (bufs)
4829                 OBD_FREE_PTR(bufs);
4830         if (qmtname)
4831                 OBD_FREE(qmtname, MAX_OBD_NAME);
4832         if (uuid)
4833                 OBD_FREE(uuid, UUID_MAX);
4834         if (data)
4835                 OBD_FREE_PTR(data);
4836         return rc;
4837 }
4838
4839 /* Shutdown quota master target associated with mdt */
4840 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4841 {
4842         ENTRY;
4843
4844         if (mdt->mdt_qmt_exp == NULL)
4845                 RETURN_EXIT;
4846         LASSERT(mdt->mdt_qmt_dev != NULL);
4847
4848         /* the qmt automatically shuts down when the mdt disconnects */
4849         obd_disconnect(mdt->mdt_qmt_exp);
4850         mdt->mdt_qmt_exp = NULL;
4851         mdt->mdt_qmt_dev = NULL;
4852         EXIT;
4853 }
4854
4855 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4856 {
4857         struct md_device  *next = m->mdt_child;
4858         struct lu_device  *d    = &m->mdt_md_dev.md_lu_dev;
4859         struct obd_device *obd = mdt2obd_dev(m);
4860         ENTRY;
4861
4862         target_recovery_fini(obd);
4863
4864         ping_evictor_stop();
4865
4866         mdt_stop_ptlrpc_service(m);
4867         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4868         obd_exports_barrier(obd);
4869         obd_zombie_barrier();
4870
4871         mdt_procfs_fini(m);
4872
4873         tgt_fini(env, &m->mdt_lut);
4874         mdt_fs_cleanup(env, m);
4875         upcall_cache_cleanup(m->mdt_identity_cache);
4876         m->mdt_identity_cache = NULL;
4877
4878         if (m->mdt_namespace != NULL) {
4879                 ldlm_namespace_free(m->mdt_namespace, NULL,
4880                                     d->ld_obd->obd_force);
4881                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4882         }
4883
4884         mdt_quota_fini(env, m);
4885
4886         cfs_free_nidlist(&m->mdt_nosquash_nids);
4887         if (m->mdt_nosquash_str) {
4888                 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4889                 m->mdt_nosquash_str = NULL;
4890                 m->mdt_nosquash_strlen = 0;
4891         }
4892
4893         mdt_seq_fini(env, m);
4894         mdt_seq_fini_cli(m);
4895         mdt_fld_fini(env, m);
4896         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4897
4898         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4899         cfs_timer_disarm(&m->mdt_ck_timer);
4900         mdt_ck_thread_stop(m);
4901
4902         /*
4903          * Finish the stack
4904          */
4905         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4906
4907         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4908
4909         server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4910
4911         EXIT;
4912 }
4913
4914 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4915 {
4916         struct mdt_device       *m = mdt_dev(obd->obd_lu_dev);
4917         struct sptlrpc_rule_set  tmp_rset;
4918         int                      rc;
4919
4920         sptlrpc_rule_set_init(&tmp_rset);
4921         rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4922         if (rc) {
4923                 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4924                        obd->obd_name, rc);
4925                 return rc;
4926         }
4927
4928         sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4929
4930         cfs_write_lock(&m->mdt_sptlrpc_lock);
4931         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4932         m->mdt_sptlrpc_rset = tmp_rset;
4933         cfs_write_unlock(&m->mdt_sptlrpc_lock);
4934
4935         return 0;
4936 }
4937
4938 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4939
4940 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4941                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
4942 {
4943         struct mdt_thread_info    *info;
4944         struct obd_device         *obd;
4945         const char                *dev = lustre_cfg_string(cfg, 0);
4946         const char                *num = lustre_cfg_string(cfg, 2);
4947         struct lustre_mount_info  *lmi = NULL;
4948         struct lustre_sb_info     *lsi;
4949         struct lu_site            *s;
4950         struct md_site            *mite;
4951         const char                *identity_upcall = "NONE";
4952         struct md_device          *next;
4953         int                        rc;
4954         int                        node_id;
4955         mntopt_t                   mntopts;
4956         ENTRY;
4957
4958         md_device_init(&m->mdt_md_dev, ldt);
4959         /*
4960          * Environment (env) might be missing mdt_thread_key values at that
4961          * point, if device is allocated when mdt_thread_key is in QUIESCENT
4962          * mode.
4963          *
4964          * Usually device allocation path doesn't use module key values, but
4965          * mdt has to do a lot of work here, so allocate key value.
4966          */
4967         rc = lu_env_refill((struct lu_env *)env);
4968         if (rc != 0)
4969                 RETURN(rc);
4970
4971         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4972         LASSERT(info != NULL);
4973
4974         obd = class_name2obd(dev);
4975         LASSERT(obd != NULL);
4976
4977         m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4978
4979         m->mdt_som_conf = 0;
4980
4981         m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4982         lmi = server_get_mount(dev);
4983         if (lmi == NULL) {
4984                 CERROR("Cannot get mount info for %s!\n", dev);
4985                 RETURN(-EFAULT);
4986         } else {
4987                 lsi = s2lsi(lmi->lmi_sb);
4988                 /* CMD is supported only in IAM mode */
4989                 LASSERT(num);
4990                 node_id = simple_strtol(num, NULL, 10);
4991                 obd->u.obt.obt_magic = OBT_MAGIC;
4992         }
4993
4994         cfs_rwlock_init(&m->mdt_sptlrpc_lock);
4995         sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4996
4997         cfs_spin_lock_init(&m->mdt_ioepoch_lock);
4998         m->mdt_opts.mo_compat_resname = 0;
4999         m->mdt_opts.mo_mds_capa = 1;
5000         m->mdt_opts.mo_oss_capa = 1;
5001         m->mdt_capa_timeout = CAPA_TIMEOUT;
5002         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
5003         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
5004         m->mdt_squash_uid = 0;
5005         m->mdt_squash_gid = 0;
5006         CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
5007         m->mdt_nosquash_str = NULL;
5008         m->mdt_nosquash_strlen = 0;
5009         cfs_init_rwsem(&m->mdt_squash_sem);
5010         cfs_spin_lock_init(&m->mdt_osfs_lock);
5011         m->mdt_osfs_age = cfs_time_shift_64(-1000);
5012
5013         m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
5014         m->mdt_md_dev.md_lu_dev.ld_obd = obd;
5015         /* set this lu_device to obd, because error handling need it */
5016         obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
5017
5018         /* init the stack */
5019         rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5020         if (rc) {
5021                 CERROR("Can't init device stack, rc %d\n", rc);
5022                 GOTO(err_lmi, rc);
5023         }
5024
5025         s = m->mdt_md_dev.md_lu_dev.ld_site;
5026         mite = &m->mdt_mite;
5027         s->ld_md_site = mite;
5028         mite->ms_lu = s;
5029
5030         /* set server index */
5031         mite->ms_node_id = node_id;
5032
5033         /* failover is the default
5034          * FIXME: we do not failout mds0/mgs, which may cause some problems.
5035          * assumed whose ms_node_id == 0 XXX
5036          * */
5037         obd->obd_replayable = 1;
5038         /* No connection accepted until configurations will finish */
5039         obd->obd_no_conn = 1;
5040
5041         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5042                 char *str = lustre_cfg_string(cfg, 4);
5043                 if (strchr(str, 'n')) {
5044                         CWARN("%s: recovery disabled\n", obd->obd_name);
5045                         obd->obd_replayable = 0;
5046                 }
5047         }
5048
5049         rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
5050         if (rc)
5051                 GOTO(err_fini_stack, rc);
5052
5053         snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
5054                  LUSTRE_MDT_NAME"-%p", m);
5055         m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5056                                               LDLM_NAMESPACE_SERVER,
5057                                               LDLM_NAMESPACE_GREEDY,
5058                                               LDLM_NS_TYPE_MDT);
5059         if (m->mdt_namespace == NULL)
5060                 GOTO(err_fini_seq, rc = -ENOMEM);
5061
5062         m->mdt_namespace->ns_lvbp = m;
5063         m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5064
5065         ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5066         /* set obd_namespace for compatibility with old code */
5067         obd->obd_namespace = m->mdt_namespace;
5068
5069         cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
5070
5071         rc = mdt_ck_thread_start(m);
5072         if (rc)
5073                 GOTO(err_free_ns, rc);
5074
5075         rc = mdt_fs_setup(env, m, obd, lsi);
5076         if (rc)
5077                 GOTO(err_capa, rc);
5078
5079         mdt_adapt_sptlrpc_conf(obd, 1);
5080
5081         next = m->mdt_child;
5082         rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
5083                                          &mntopts);
5084         if (rc)
5085                 GOTO(err_llog_cleanup, rc);
5086
5087         if (mntopts & MNTOPT_USERXATTR)
5088                 m->mdt_opts.mo_user_xattr = 1;
5089         else
5090                 m->mdt_opts.mo_user_xattr = 0;
5091
5092         if (mntopts & MNTOPT_ACL)
5093                 m->mdt_opts.mo_acl = 1;
5094         else
5095                 m->mdt_opts.mo_acl = 0;
5096
5097         /* XXX: to support suppgid for ACL, we enable identity_upcall
5098          * by default, otherwise, maybe got unexpected -EACCESS. */
5099         if (m->mdt_opts.mo_acl)
5100                 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5101
5102         m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
5103                                                 &mdt_identity_upcall_cache_ops);
5104         if (IS_ERR(m->mdt_identity_cache)) {
5105                 rc = PTR_ERR(m->mdt_identity_cache);
5106                 m->mdt_identity_cache = NULL;
5107                 GOTO(err_llog_cleanup, rc);
5108         }
5109
5110         rc = mdt_procfs_init(m, dev);
5111         if (rc) {
5112                 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5113                 GOTO(err_recovery, rc);
5114         }
5115
5116         rc = mdt_quota_init(env, m, cfg);
5117         if (rc)
5118                 GOTO(err_procfs, rc);
5119
5120         rc = mdt_start_ptlrpc_service(m);
5121         if (rc)
5122                 GOTO(err_quota, rc);
5123
5124         ping_evictor_start();
5125
5126         /* recovery will be started upon mdt_prepare()
5127          * when the whole stack is complete and ready
5128          * to serve the requests */
5129
5130         mdt_init_capa_ctxt(env, m);
5131
5132         /* Reduce the initial timeout on an MDS because it doesn't need such
5133          * a long timeout as an OST does. Adaptive timeouts will adjust this
5134          * value appropriately. */
5135         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5136                 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5137
5138         RETURN(0);
5139
5140         ping_evictor_stop();
5141         mdt_stop_ptlrpc_service(m);
5142 err_quota:
5143         mdt_quota_fini(env, m);
5144 err_procfs:
5145         mdt_procfs_fini(m);
5146 err_recovery:
5147         target_recovery_fini(obd);
5148         upcall_cache_cleanup(m->mdt_identity_cache);
5149         m->mdt_identity_cache = NULL;
5150 err_llog_cleanup:
5151         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5152         mdt_fs_cleanup(env, m);
5153 err_capa:
5154         cfs_timer_disarm(&m->mdt_ck_timer);
5155         mdt_ck_thread_stop(m);
5156 err_free_ns:
5157         ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5158         obd->obd_namespace = m->mdt_namespace = NULL;
5159 err_fini_seq:
5160         mdt_seq_fini(env, m);
5161         mdt_fld_fini(env, m);
5162         tgt_fini(env, &m->mdt_lut);
5163 err_fini_stack:
5164         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5165 err_lmi:
5166         if (lmi)
5167                 server_put_mount(dev, lmi->lmi_mnt);
5168         return (rc);
5169 }
5170
5171 /* For interoperability, the left element is old parameter, the right one
5172  * is the new version of the parameter, if some parameter is deprecated,
5173  * the new version should be set as NULL. */
5174 static struct cfg_interop_param mdt_interop_param[] = {
5175         { "mdt.group_upcall",   NULL },
5176         { "mdt.quota_type",     NULL },
5177         { "mdd.quota_type",     NULL },
5178         { "mdt.rootsquash",     "mdt.root_squash" },
5179         { "mdt.nosquash_nid",   "mdt.nosquash_nids" },
5180         { NULL }
5181 };
5182
5183 /* used by MGS to process specific configurations */
5184 static int mdt_process_config(const struct lu_env *env,
5185                               struct lu_device *d, struct lustre_cfg *cfg)
5186 {
5187         struct mdt_device *m = mdt_dev(d);
5188         struct md_device *md_next = m->mdt_child;
5189         struct lu_device *next = md2lu_dev(md_next);
5190         int rc;
5191         ENTRY;
5192
5193         switch (cfg->lcfg_command) {
5194         case LCFG_PARAM: {
5195                 struct lprocfs_static_vars  lvars;
5196                 struct obd_device          *obd = d->ld_obd;
5197
5198                 /* For interoperability */
5199                 struct cfg_interop_param   *ptr = NULL;
5200                 struct lustre_cfg          *old_cfg = NULL;
5201                 char                       *param = NULL;
5202
5203                 param = lustre_cfg_string(cfg, 1);
5204                 if (param == NULL) {
5205                         CERROR("param is empty\n");
5206                         rc = -EINVAL;
5207                         break;
5208                 }
5209
5210                 ptr = class_find_old_param(param, mdt_interop_param);
5211                 if (ptr != NULL) {
5212                         if (ptr->new_param == NULL) {
5213                                 rc = 0;
5214                                 CWARN("For interoperability, skip this %s."
5215                                       " It is obsolete.\n", ptr->old_param);
5216                                 break;
5217                         }
5218
5219                         CWARN("Found old param %s, changed it to %s.\n",
5220                               ptr->old_param, ptr->new_param);
5221
5222                         old_cfg = cfg;
5223                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5224                         if (IS_ERR(cfg)) {
5225                                 rc = PTR_ERR(cfg);
5226                                 break;
5227                         }
5228                 }
5229
5230                 lprocfs_mdt_init_vars(&lvars);
5231                 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
5232                                               cfg, obd);
5233                 if (rc > 0 || rc == -ENOSYS)
5234                         /* we don't understand; pass it on */
5235                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
5236
5237                 if (old_cfg != NULL)
5238                         lustre_cfg_free(cfg);
5239
5240                 break;
5241         }
5242         case LCFG_ADD_MDC:
5243                 /*
5244                  * Add mdc hook to get first MDT uuid and connect it to
5245                  * ls->controller to use for seq manager.
5246                  */
5247                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5248                 if (rc)
5249                         CERROR("Can't add mdc, rc %d\n", rc);
5250                 else
5251                         rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
5252                 break;
5253         default:
5254                 /* others are passed further */
5255                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5256                 break;
5257         }
5258         RETURN(rc);
5259 }
5260
5261 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5262                                           const struct lu_object_header *hdr,
5263                                           struct lu_device *d)
5264 {
5265         struct mdt_object *mo;
5266
5267         ENTRY;
5268
5269         OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
5270         if (mo != NULL) {
5271                 struct lu_object *o;
5272                 struct lu_object_header *h;
5273
5274                 o = &mo->mot_obj.mo_lu;
5275                 h = &mo->mot_header;
5276                 lu_object_header_init(h);
5277                 lu_object_init(o, h, d);
5278                 lu_object_add_top(h, o);
5279                 o->lo_ops = &mdt_obj_ops;
5280                 cfs_mutex_init(&mo->mot_ioepoch_mutex);
5281                 cfs_mutex_init(&mo->mot_lov_mutex);
5282                 RETURN(o);
5283         } else
5284                 RETURN(NULL);
5285 }
5286
5287 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5288                            const struct lu_object_conf *unused)
5289 {
5290         struct mdt_device *d = mdt_dev(o->lo_dev);
5291         struct lu_device  *under;
5292         struct lu_object  *below;
5293         int                rc = 0;
5294         ENTRY;
5295
5296         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5297                PFID(lu_object_fid(o)));
5298
5299         under = &d->mdt_child->md_lu_dev;
5300         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5301         if (below != NULL) {
5302                 lu_object_add(o, below);
5303         } else
5304                 rc = -ENOMEM;
5305
5306         RETURN(rc);
5307 }
5308
5309 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5310 {
5311         struct mdt_object *mo = mdt_obj(o);
5312         struct lu_object_header *h;
5313         ENTRY;
5314
5315         h = o->lo_header;
5316         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5317                PFID(lu_object_fid(o)));
5318
5319         lu_object_fini(o);
5320         lu_object_header_fini(h);
5321         OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5322
5323         EXIT;
5324 }
5325
5326 static int mdt_object_print(const struct lu_env *env, void *cookie,
5327                             lu_printer_t p, const struct lu_object *o)
5328 {
5329         struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5330         return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5331                     "flags="LPX64", epochcount=%d, writecount=%d)",
5332                     mdto, mdto->mot_ioepoch, mdto->mot_flags,
5333                     mdto->mot_ioepoch_count, mdto->mot_writecount);
5334 }
5335
5336 static int mdt_prepare(const struct lu_env *env,
5337                 struct lu_device *pdev,
5338                 struct lu_device *cdev)
5339 {
5340         struct mdt_device *mdt = mdt_dev(cdev);
5341         struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5342         struct obd_device *obd = cdev->ld_obd;
5343         int rc;
5344
5345         ENTRY;
5346
5347         LASSERT(obd);
5348
5349         rc = next->ld_ops->ldo_prepare(env, cdev, next);
5350         if (rc)
5351                 RETURN(rc);
5352
5353         rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5354         if (rc)
5355                 RETURN(rc);
5356
5357         rc = mdt_fld_init(env, obd->obd_name, mdt);
5358         if (rc)
5359                 RETURN(rc);
5360
5361         rc = mdt_seq_init(env, obd->obd_name, mdt);
5362         if (rc)
5363                 RETURN(rc);
5364
5365         rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5366                                                   &mdt->mdt_md_root_fid);
5367         if (rc)
5368                 RETURN(rc);
5369
5370         LASSERT(!cfs_test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5371         target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5372         cfs_set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5373         LASSERT(obd->obd_no_conn);
5374         cfs_spin_lock(&obd->obd_dev_lock);
5375         obd->obd_no_conn = 0;
5376         cfs_spin_unlock(&obd->obd_dev_lock);
5377
5378         if (obd->obd_recovering == 0)
5379                 mdt_postrecov(env, mdt);
5380
5381         RETURN(rc);
5382 }
5383
5384 static const struct lu_device_operations mdt_lu_ops = {
5385         .ldo_object_alloc   = mdt_object_alloc,
5386         .ldo_process_config = mdt_process_config,
5387         .ldo_prepare        = mdt_prepare,
5388 };
5389
5390 static const struct lu_object_operations mdt_obj_ops = {
5391         .loo_object_init    = mdt_object_init,
5392         .loo_object_free    = mdt_object_free,
5393         .loo_object_print   = mdt_object_print
5394 };
5395
5396 static int mdt_obd_set_info_async(const struct lu_env *env,
5397                                   struct obd_export *exp,
5398                                   __u32 keylen, void *key,
5399                                   __u32 vallen, void *val,
5400                                   struct ptlrpc_request_set *set)
5401 {
5402         struct obd_device     *obd = exp->exp_obd;
5403         int                    rc;
5404         ENTRY;
5405
5406         LASSERT(obd);
5407
5408         if (KEY_IS(KEY_SPTLRPC_CONF)) {
5409                 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5410                 RETURN(rc);
5411         }
5412
5413         RETURN(0);
5414 }
5415
5416 /**
5417  * Match client and server connection feature flags.
5418  *
5419  * Compute the compatibility flags for a connection request based on
5420  * features mutually supported by client and server.
5421  *
5422  * The obd_export::exp_connect_flags field in \a exp must not be updated
5423  * here, otherwise a partially initialized value may be exposed. After
5424  * the connection request is successfully processed, the top-level MDT
5425  * connect request handler atomically updates the export connect flags
5426  * from the obd_connect_data::ocd_connect_flags field of the reply.
5427  * \see mdt_connect().
5428  *
5429  * \param exp   the obd_export associated with this client/target pair
5430  * \param mdt   the target device for the connection
5431  * \param data  stores data for this connect request
5432  *
5433  * \retval 0       success
5434  * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5435  * \retval -EBADE  client and server feature requirements are incompatible
5436  */
5437 static int mdt_connect_internal(struct obd_export *exp,
5438                                 struct mdt_device *mdt,
5439                                 struct obd_connect_data *data)
5440 {
5441         LASSERT(data != NULL);
5442
5443         data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5444         data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5445
5446         /* If no known bits (which should not happen, probably,
5447            as everybody should support LOOKUP and UPDATE bits at least)
5448            revert to compat mode with plain locks. */
5449         if (!data->ocd_ibits_known &&
5450             data->ocd_connect_flags & OBD_CONNECT_IBITS)
5451                 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5452
5453         if (!mdt->mdt_opts.mo_acl)
5454                 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5455
5456         if (!mdt->mdt_opts.mo_user_xattr)
5457                 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5458
5459         if (!mdt->mdt_som_conf)
5460                 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5461
5462         if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5463                 data->ocd_brw_size = min(data->ocd_brw_size,
5464                         (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
5465                 if (data->ocd_brw_size == 0) {
5466                         CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5467                                " ocd_version: %x ocd_grant: %d "
5468                                "ocd_index: %u ocd_brw_size is "
5469                                "unexpectedly zero, network data "
5470                                "corruption? Refusing connection of this"
5471                                " client\n",
5472                                exp->exp_obd->obd_name,
5473                                exp->exp_client_uuid.uuid,
5474                                exp, data->ocd_connect_flags, data->ocd_version,
5475                                data->ocd_grant, data->ocd_index);
5476                         return -EPROTO;
5477                 }
5478         }
5479
5480         /* NB: Disregard the rule against updating exp_connect_flags in this
5481          * case, since tgt_client_new() needs to know if this is a lightweight
5482          * connection, and it is safe to expose this flag before connection
5483          * processing completes. */
5484         if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5485                 cfs_spin_lock(&exp->exp_lock);
5486                 exp->exp_connect_flags |=  OBD_CONNECT_LIGHTWEIGHT;
5487                 cfs_spin_unlock(&exp->exp_lock);
5488         }
5489
5490         data->ocd_version = LUSTRE_VERSION_CODE;
5491         exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5492
5493         if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5494                 CWARN("%s: MDS requires FID support, but client not\n",
5495                       mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5496                 return -EBADE;
5497         }
5498
5499         if (mdt->mdt_som_conf &&
5500             !(data->ocd_connect_flags & (OBD_CONNECT_MDS_MDS|OBD_CONNECT_SOM))){
5501                 CWARN("%s: MDS has SOM enabled, but client does not support "
5502                       "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5503                 return -EBADE;
5504         }
5505
5506         return 0;
5507 }
5508
5509 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5510                                      struct obd_export *exp,
5511                                      struct ptlrpc_request *req)
5512 {
5513         struct sptlrpc_flavor   flvr;
5514         int                     rc = 0;
5515
5516         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5517                 cfs_read_lock(&mdt->mdt_sptlrpc_lock);
5518                 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5519                                              req->rq_sp_from,
5520                                              req->rq_peer.nid,
5521                                              &flvr);
5522                 cfs_read_unlock(&mdt->mdt_sptlrpc_lock);
5523
5524                 cfs_spin_lock(&exp->exp_lock);
5525
5526                 exp->exp_sp_peer = req->rq_sp_from;
5527                 exp->exp_flvr = flvr;
5528
5529                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5530                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5531                         CERROR("unauthorized rpc flavor %x from %s, "
5532                                "expect %x\n", req->rq_flvr.sf_rpc,
5533                                libcfs_nid2str(req->rq_peer.nid),
5534                                exp->exp_flvr.sf_rpc);
5535                         rc = -EACCES;
5536                 }
5537
5538                 cfs_spin_unlock(&exp->exp_lock);
5539         } else {
5540                 if (exp->exp_sp_peer != req->rq_sp_from) {
5541                         CERROR("RPC source %s doesn't match %s\n",
5542                                sptlrpc_part2name(req->rq_sp_from),
5543                                sptlrpc_part2name(exp->exp_sp_peer));
5544                         rc = -EACCES;
5545                 } else {
5546                         rc = sptlrpc_target_export_check(exp, req);
5547                 }
5548         }
5549
5550         return rc;
5551 }
5552
5553 /* mds_connect copy */
5554 static int mdt_obd_connect(const struct lu_env *env,
5555                            struct obd_export **exp, struct obd_device *obd,
5556                            struct obd_uuid *cluuid,
5557                            struct obd_connect_data *data,
5558                            void *localdata)
5559 {
5560         struct mdt_thread_info *info;
5561         struct obd_export      *lexp;
5562         struct lustre_handle    conn = { 0 };
5563         struct mdt_device      *mdt;
5564         struct ptlrpc_request  *req;
5565         int                     rc;
5566         ENTRY;
5567
5568         LASSERT(env != NULL);
5569         if (!exp || !obd || !cluuid)
5570                 RETURN(-EINVAL);
5571
5572         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5573         req = info->mti_pill->rc_req;
5574         mdt = mdt_dev(obd->obd_lu_dev);
5575
5576         /*
5577          * first, check whether the stack is ready to handle requests
5578          * XXX: probably not very appropriate method is used now
5579          *      at some point we should find a better one
5580          */
5581         if (!cfs_test_bit(MDT_FL_SYNCED, &mdt->mdt_state)) {
5582                 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5583                 if (rc)
5584                         RETURN(-EAGAIN);
5585                 cfs_set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5586         }
5587
5588         rc = class_connect(&conn, obd, cluuid);
5589         if (rc)
5590                 RETURN(rc);
5591
5592         lexp = class_conn2export(&conn);
5593         LASSERT(lexp != NULL);
5594
5595         rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5596         if (rc)
5597                 GOTO(out, rc);
5598
5599         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5600                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5601
5602         rc = mdt_connect_internal(lexp, mdt, data);
5603         if (rc == 0) {
5604                 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5605
5606                 LASSERT(lcd);
5607                 info->mti_exp = lexp;
5608                 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5609                 rc = tgt_client_new(env, lexp);
5610                 if (rc == 0)
5611                         mdt_export_stats_init(obd, lexp, localdata);
5612         }
5613
5614 out:
5615         if (rc != 0) {
5616                 class_disconnect(lexp);
5617                 *exp = NULL;
5618         } else {
5619                 *exp = lexp;
5620         }
5621
5622         RETURN(rc);
5623 }
5624
5625 static int mdt_obd_reconnect(const struct lu_env *env,
5626                              struct obd_export *exp, struct obd_device *obd,
5627                              struct obd_uuid *cluuid,
5628                              struct obd_connect_data *data,
5629                              void *localdata)
5630 {
5631         struct mdt_thread_info *info;
5632         struct mdt_device      *mdt;
5633         struct ptlrpc_request  *req;
5634         int                     rc;
5635         ENTRY;
5636
5637         if (exp == NULL || obd == NULL || cluuid == NULL)
5638                 RETURN(-EINVAL);
5639
5640         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5641         req = info->mti_pill->rc_req;
5642         mdt = mdt_dev(obd->obd_lu_dev);
5643
5644         rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5645         if (rc)
5646                 RETURN(rc);
5647
5648         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5649         if (rc == 0)
5650                 mdt_export_stats_init(obd, exp, localdata);
5651
5652         RETURN(rc);
5653 }
5654
5655 static int mdt_export_cleanup(struct obd_export *exp)
5656 {
5657         struct mdt_export_data *med = &exp->exp_mdt_data;
5658         struct obd_device      *obd = exp->exp_obd;
5659         struct mdt_device      *mdt;
5660         struct mdt_thread_info *info;
5661         struct lu_env           env;
5662         CFS_LIST_HEAD(closing_list);
5663         struct mdt_file_data *mfd, *n;
5664         int rc = 0;
5665         ENTRY;
5666
5667         cfs_spin_lock(&med->med_open_lock);
5668         while (!cfs_list_empty(&med->med_open_head)) {
5669                 cfs_list_t *tmp = med->med_open_head.next;
5670                 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5671
5672                 /* Remove mfd handle so it can't be found again.
5673                  * We are consuming the mfd_list reference here. */
5674                 class_handle_unhash(&mfd->mfd_handle);
5675                 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5676         }
5677         cfs_spin_unlock(&med->med_open_lock);
5678         mdt = mdt_dev(obd->obd_lu_dev);
5679         LASSERT(mdt != NULL);
5680
5681         rc = lu_env_init(&env, LCT_MD_THREAD);
5682         if (rc)
5683                 RETURN(rc);
5684
5685         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5686         LASSERT(info != NULL);
5687         memset(info, 0, sizeof *info);
5688         info->mti_env = &env;
5689         info->mti_mdt = mdt;
5690         info->mti_exp = exp;
5691
5692         if (!cfs_list_empty(&closing_list)) {
5693                 struct md_attr *ma = &info->mti_attr;
5694
5695                 /* Close any open files (which may also cause orphan unlinking). */
5696                 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5697                         cfs_list_del_init(&mfd->mfd_list);
5698                         ma->ma_need = ma->ma_valid = 0;
5699                         /* Don't unlink orphan on failover umount, LU-184 */
5700                         if (exp->exp_flags & OBD_OPT_FAILOVER) {
5701                                 ma->ma_valid = MA_FLAGS;
5702                                 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5703                         }
5704                         mdt_mfd_close(info, mfd);
5705                 }
5706         }
5707         info->mti_mdt = NULL;
5708         /* cleanup client slot early */
5709         /* Do not erase record for recoverable client. */
5710         if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5711                 tgt_client_del(&env, exp);
5712         lu_env_fini(&env);
5713
5714         RETURN(rc);
5715 }
5716
5717 static int mdt_obd_disconnect(struct obd_export *exp)
5718 {
5719         int rc;
5720         ENTRY;
5721
5722         LASSERT(exp);
5723         class_export_get(exp);
5724
5725         rc = server_disconnect_export(exp);
5726         if (rc != 0)
5727                 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5728
5729         rc = mdt_export_cleanup(exp);
5730         class_export_put(exp);
5731         RETURN(rc);
5732 }
5733
5734 /* FIXME: Can we avoid using these two interfaces? */
5735 static int mdt_init_export(struct obd_export *exp)
5736 {
5737         struct mdt_export_data *med = &exp->exp_mdt_data;
5738         int                     rc;
5739         ENTRY;
5740
5741         CFS_INIT_LIST_HEAD(&med->med_open_head);
5742         cfs_spin_lock_init(&med->med_open_lock);
5743         cfs_mutex_init(&med->med_idmap_mutex);
5744         med->med_idmap = NULL;
5745         cfs_spin_lock(&exp->exp_lock);
5746         exp->exp_connecting = 1;
5747         cfs_spin_unlock(&exp->exp_lock);
5748
5749         /* self-export doesn't need client data and ldlm initialization */
5750         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5751                                      &exp->exp_client_uuid)))
5752                 RETURN(0);
5753
5754         rc = tgt_client_alloc(exp);
5755         if (rc)
5756                 GOTO(err, rc);
5757
5758         rc = ldlm_init_export(exp);
5759         if (rc)
5760                 GOTO(err_free, rc);
5761
5762         RETURN(rc);
5763
5764 err_free:
5765         tgt_client_free(exp);
5766 err:
5767         CERROR("%s: Failed to initialize export: rc = %d\n",
5768                exp->exp_obd->obd_name, rc);
5769         return rc;
5770 }
5771
5772 static int mdt_destroy_export(struct obd_export *exp)
5773 {
5774         ENTRY;
5775
5776         if (exp_connect_rmtclient(exp))
5777                 mdt_cleanup_idmap(&exp->exp_mdt_data);
5778
5779         target_destroy_export(exp);
5780         /* destroy can be called from failed obd_setup, so
5781          * checking uuid is safer than obd_self_export */
5782         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5783                                      &exp->exp_client_uuid)))
5784                 RETURN(0);
5785
5786         ldlm_destroy_export(exp);
5787         tgt_client_free(exp);
5788
5789         LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5790         LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5791
5792         RETURN(0);
5793 }
5794
5795 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5796                             void *val, int vallen)
5797 {
5798         struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5799         struct getinfo_fid2path *fpout, *fpin;
5800         int rc = 0;
5801
5802         fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5803         fpout = val;
5804
5805         if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5806                 lustre_swab_fid2path(fpin);
5807
5808         memcpy(fpout, fpin, sizeof(*fpin));
5809         if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5810                 RETURN(-EINVAL);
5811
5812         rc = mdt_fid2path(info->mti_env, mdt, fpout);
5813         RETURN(rc);
5814 }
5815
5816 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5817                 struct getinfo_fid2path *fp)
5818 {
5819         struct mdt_object *obj;
5820         struct obd_device *obd = mdt2obd_dev(mdt);
5821         int    rc;
5822         ENTRY;
5823
5824         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5825                 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5826
5827         if (!fid_is_sane(&fp->gf_fid))
5828                 RETURN(-EINVAL);
5829
5830         if (!fid_is_norm(&fp->gf_fid) && !fid_is_igif(&fp->gf_fid)) {
5831                 CWARN("%s: "DFID" is invalid, sequence should be "
5832                         ">= "LPX64"\n", obd->obd_name,
5833                         PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5834                 RETURN(-EINVAL);
5835         }
5836
5837         obj = mdt_object_find(env, mdt, &fp->gf_fid);
5838         if (obj == NULL || IS_ERR(obj)) {
5839                 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5840                         PTR_ERR(obj));
5841                 RETURN(-EINVAL);
5842         }
5843
5844         rc = lu_object_exists(&obj->mot_obj.mo_lu);
5845         if (rc <= 0) {
5846                 if (rc == -1)
5847                         rc = -EREMOTE;
5848                 else
5849                         rc = -ENOENT;
5850                 mdt_object_put(env, obj);
5851                 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5852                         PFID(&fp->gf_fid), rc);
5853                 RETURN(rc);
5854         }
5855
5856         rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5857                         fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5858         mdt_object_put(env, obj);
5859
5860         RETURN(rc);
5861 }
5862
5863 static int mdt_get_info(struct mdt_thread_info *info)
5864 {
5865         struct ptlrpc_request *req = mdt_info_req(info);
5866         char *key;
5867         int keylen;
5868         __u32 *vallen;
5869         void *valout;
5870         int rc;
5871         ENTRY;
5872
5873         key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5874         if (key == NULL) {
5875                 CDEBUG(D_IOCTL, "No GETINFO key");
5876                 RETURN(-EFAULT);
5877         }
5878         keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5879                                       RCL_CLIENT);
5880
5881         vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5882         if (vallen == NULL) {
5883                 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5884                 RETURN(-EFAULT);
5885         }
5886
5887         req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5888                              *vallen);
5889         rc = req_capsule_server_pack(info->mti_pill);
5890         valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5891         if (valout == NULL) {
5892                 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5893                 RETURN(-EFAULT);
5894         }
5895
5896         if (KEY_IS(KEY_FID2PATH))
5897                 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5898         else
5899                 rc = -EINVAL;
5900
5901         lustre_msg_set_status(req->rq_repmsg, rc);
5902
5903         RETURN(rc);
5904 }
5905
5906 /* Pass the ioc down */
5907 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5908                          unsigned int cmd, int len, void *data)
5909 {
5910         struct lu_context ioctl_session;
5911         struct md_device *next = mdt->mdt_child;
5912         int rc;
5913         ENTRY;
5914
5915         rc = lu_context_init(&ioctl_session, LCT_SESSION);
5916         if (rc)
5917                 RETURN(rc);
5918         ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5919         lu_context_enter(&ioctl_session);
5920         env->le_ses = &ioctl_session;
5921
5922         LASSERT(next->md_ops->mdo_iocontrol);
5923         rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5924
5925         lu_context_exit(&ioctl_session);
5926         lu_context_fini(&ioctl_session);
5927         RETURN(rc);
5928 }
5929
5930 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5931 {
5932         struct obd_ioctl_data *data = karg;
5933         struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5934         __u64 version;
5935         struct mdt_object *obj;
5936         struct mdt_lock_handle  *lh;
5937         int rc;
5938         ENTRY;
5939
5940         CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5941         if (!fid_is_sane(fid))
5942                 RETURN(-EINVAL);
5943
5944         lh = &mti->mti_lh[MDT_LH_PARENT];
5945         mdt_lock_reg_init(lh, LCK_CR);
5946
5947         obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5948         if (IS_ERR(obj))
5949                 RETURN(PTR_ERR(obj));
5950
5951         rc = mdt_object_exists(obj);
5952         if (rc < 0) {
5953                 rc = -EREMOTE;
5954                 /**
5955                  * before calling version get the correct MDS should be
5956                  * fid, this is error to find remote object here
5957                  */
5958                 CERROR("nonlocal object "DFID"\n", PFID(fid));
5959         } else if (rc == 0) {
5960                 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5961                 rc = -ENOENT;
5962         } else {
5963                 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5964                *(__u64 *)data->ioc_inlbuf2 = version;
5965                 rc = 0;
5966         }
5967         mdt_object_unlock_put(mti, obj, lh, 1);
5968         RETURN(rc);
5969 }
5970
5971 /* ioctls on obd dev */
5972 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5973                          void *karg, void *uarg)
5974 {
5975         struct lu_env      env;
5976         struct obd_device *obd = exp->exp_obd;
5977         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5978         struct dt_device  *dt = mdt->mdt_bottom;
5979         int rc;
5980
5981         ENTRY;
5982         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5983         rc = lu_env_init(&env, LCT_MD_THREAD);
5984         if (rc)
5985                 RETURN(rc);
5986
5987         switch (cmd) {
5988         case OBD_IOC_SYNC:
5989                 rc = mdt_device_sync(&env, mdt);
5990                 break;
5991         case OBD_IOC_SET_READONLY:
5992                 rc = dt->dd_ops->dt_ro(&env, dt);
5993                 break;
5994         case OBD_IOC_ABORT_RECOVERY:
5995                 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5996                 target_stop_recovery_thread(obd);
5997                 rc = 0;
5998                 break;
5999         case OBD_IOC_CHANGELOG_REG:
6000         case OBD_IOC_CHANGELOG_DEREG:
6001         case OBD_IOC_CHANGELOG_CLEAR:
6002                 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
6003                 break;
6004         case OBD_IOC_START_LFSCK:
6005         case OBD_IOC_STOP_LFSCK: {
6006                 struct md_device *next = mdt->mdt_child;
6007                 struct obd_ioctl_data *data = karg;
6008
6009                 if (unlikely(data == NULL)) {
6010                         rc = -EINVAL;
6011                         break;
6012                 }
6013
6014                 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
6015                                                  data->ioc_inllen1,
6016                                                  data->ioc_inlbuf1);
6017                 break;
6018         }
6019         case OBD_IOC_GET_OBJ_VERSION: {
6020                 struct mdt_thread_info *mti;
6021                 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6022                 memset(mti, 0, sizeof *mti);
6023                 mti->mti_env = &env;
6024                 mti->mti_mdt = mdt;
6025                 mti->mti_exp = exp;
6026
6027                 rc = mdt_ioc_version_get(mti, karg);
6028                 break;
6029         }
6030         default:
6031                 CERROR("Not supported cmd = %d for device %s\n",
6032                        cmd, obd->obd_name);
6033                 rc = -EOPNOTSUPP;
6034         }
6035
6036         lu_env_fini(&env);
6037         RETURN(rc);
6038 }
6039
6040 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6041 {
6042         struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6043         int rc;
6044         ENTRY;
6045
6046         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6047         RETURN(rc);
6048 }
6049
6050 int mdt_obd_postrecov(struct obd_device *obd)
6051 {
6052         struct lu_env env;
6053         int rc;
6054
6055         rc = lu_env_init(&env, LCT_MD_THREAD);
6056         if (rc)
6057                 RETURN(rc);
6058         rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6059         lu_env_fini(&env);
6060         return rc;
6061 }
6062
6063 /**
6064  * Send a copytool req to a client
6065  * Note this sends a request RPC from a server (MDT) to a client (MDC),
6066  * backwards of normal comms.
6067  */
6068 int mdt_hsm_copytool_send(struct obd_export *exp)
6069 {
6070         struct kuc_hdr *lh;
6071         struct hsm_action_list *hal;
6072         struct hsm_action_item *hai;
6073         int rc, len;
6074         ENTRY;
6075
6076         CWARN("%s: writing to mdc at %s\n", exp->exp_obd->obd_name,
6077               libcfs_nid2str(exp->exp_connection->c_peer.nid));
6078
6079         len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
6080                 /* for mockup below */ 2 * cfs_size_round(sizeof(*hai));
6081         OBD_ALLOC(lh, len);
6082         if (lh == NULL)
6083                 RETURN(-ENOMEM);
6084
6085         lh->kuc_magic = KUC_MAGIC;
6086         lh->kuc_transport = KUC_TRANSPORT_HSM;
6087         lh->kuc_msgtype = HMT_ACTION_LIST;
6088         lh->kuc_msglen = len;
6089
6090         hal = (struct hsm_action_list *)(lh + 1);
6091         hal->hal_version = HAL_VERSION;
6092         hal->hal_archive_num = 1;
6093         obd_uuid2fsname(hal->hal_fsname, exp->exp_obd->obd_name,
6094                         MTI_NAME_MAXLEN);
6095
6096         /* mock up an action list */
6097         hal->hal_count = 2;
6098         hai = hai_zero(hal);
6099         hai->hai_action = HSMA_ARCHIVE;
6100         hai->hai_fid.f_oid = 0xA00A;
6101         hai->hai_len = sizeof(*hai);
6102         hai = hai_next(hai);
6103         hai->hai_action = HSMA_RESTORE;
6104         hai->hai_fid.f_oid = 0xB00B;
6105         hai->hai_len = sizeof(*hai);
6106
6107         /* Uses the ldlm reverse import; this rpc will be seen by
6108           the ldlm_callback_handler */
6109         rc = do_set_info_async(exp->exp_imp_reverse,
6110                                LDLM_SET_INFO, LUSTRE_OBD_VERSION,
6111                                sizeof(KEY_HSM_COPYTOOL_SEND),
6112                                KEY_HSM_COPYTOOL_SEND,
6113                                len, lh, NULL);
6114
6115         OBD_FREE(lh, len);
6116
6117         RETURN(rc);
6118 }
6119
6120 static struct obd_ops mdt_obd_device_ops = {
6121         .o_owner          = THIS_MODULE,
6122         .o_set_info_async = mdt_obd_set_info_async,
6123         .o_connect        = mdt_obd_connect,
6124         .o_reconnect      = mdt_obd_reconnect,
6125         .o_disconnect     = mdt_obd_disconnect,
6126         .o_init_export    = mdt_init_export,
6127         .o_destroy_export = mdt_destroy_export,
6128         .o_iocontrol      = mdt_iocontrol,
6129         .o_postrecov      = mdt_obd_postrecov,
6130 };
6131
6132 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6133                                          struct lu_device *d)
6134 {
6135         struct mdt_device *m = mdt_dev(d);
6136         ENTRY;
6137
6138         mdt_fini(env, m);
6139         RETURN(NULL);
6140 }
6141
6142 static struct lu_device *mdt_device_free(const struct lu_env *env,
6143                                          struct lu_device *d)
6144 {
6145         struct mdt_device *m = mdt_dev(d);
6146         ENTRY;
6147
6148         md_device_fini(&m->mdt_md_dev);
6149         OBD_FREE_PTR(m);
6150         RETURN(NULL);
6151 }
6152
6153 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6154                                           struct lu_device_type *t,
6155                                           struct lustre_cfg *cfg)
6156 {
6157         struct lu_device  *l;
6158         struct mdt_device *m;
6159
6160         OBD_ALLOC_PTR(m);
6161         if (m != NULL) {
6162                 int rc;
6163
6164                 l = &m->mdt_md_dev.md_lu_dev;
6165                 rc = mdt_init0(env, m, t, cfg);
6166                 if (rc != 0) {
6167                         mdt_device_free(env, l);
6168                         l = ERR_PTR(rc);
6169                         return l;
6170                 }
6171         } else
6172                 l = ERR_PTR(-ENOMEM);
6173         return l;
6174 }
6175
6176 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6177 LU_KEY_INIT(mdt, struct mdt_thread_info);
6178
6179 static void mdt_key_fini(const struct lu_context *ctx,
6180                          struct lu_context_key *key, void* data)
6181 {
6182         struct mdt_thread_info *info = data;
6183
6184         if (info->mti_big_lmm) {
6185                 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6186                 info->mti_big_lmm = NULL;
6187                 info->mti_big_lmmsize = 0;
6188         }
6189         OBD_FREE_PTR(info);
6190 }
6191
6192 /* context key: mdt_thread_key */
6193 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6194
6195 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
6196 {
6197         return md_ucred(info->mti_env);
6198 }
6199
6200 /**
6201  * Enable/disable COS (Commit On Sharing).
6202  *
6203  * Set/Clear the COS flag in mdt options.
6204  *
6205  * \param mdt mdt device
6206  * \param val 0 disables COS, other values enable COS
6207  */
6208 void mdt_enable_cos(struct mdt_device *mdt, int val)
6209 {
6210         struct lu_env env;
6211         int rc;
6212
6213         mdt->mdt_opts.mo_cos = !!val;
6214         rc = lu_env_init(&env, LCT_LOCAL);
6215         if (unlikely(rc != 0)) {
6216                 CWARN("lu_env initialization failed with rc = %d,"
6217                       "cannot sync\n", rc);
6218                 return;
6219         }
6220         mdt_device_sync(&env, mdt);
6221         lu_env_fini(&env);
6222 }
6223
6224 /**
6225  * Check COS (Commit On Sharing) status.
6226  *
6227  * Return COS flag status.
6228  *
6229  * \param mdt mdt device
6230  */
6231 int mdt_cos_is_enabled(struct mdt_device *mdt)
6232 {
6233         return mdt->mdt_opts.mo_cos != 0;
6234 }
6235
6236 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
6237 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key);
6238
6239 static struct lu_device_type_operations mdt_device_type_ops = {
6240         .ldto_init = mdt_type_init,
6241         .ldto_fini = mdt_type_fini,
6242
6243         .ldto_start = mdt_type_start,
6244         .ldto_stop  = mdt_type_stop,
6245
6246         .ldto_device_alloc = mdt_device_alloc,
6247         .ldto_device_free  = mdt_device_free,
6248         .ldto_device_fini  = mdt_device_fini
6249 };
6250
6251 static struct lu_device_type mdt_device_type = {
6252         .ldt_tags     = LU_DEVICE_MD,
6253         .ldt_name     = LUSTRE_MDT_NAME,
6254         .ldt_ops      = &mdt_device_type_ops,
6255         .ldt_ctx_tags = LCT_MD_THREAD
6256 };
6257
6258 static int __init mdt_mod_init(void)
6259 {
6260         struct lprocfs_static_vars lvars;
6261         int rc;
6262
6263         rc = lu_kmem_init(mdt_caches);
6264         if (rc)
6265                 return rc;
6266
6267         if (mdt_num_threads != 0 && mds_num_threads == 0) {
6268                 LCONSOLE_INFO("mdt_num_threads module parameter is deprecated,"
6269                               "use mds_num_threads instead or unset both for"
6270                               "dynamic thread startup\n");
6271                 mds_num_threads = mdt_num_threads;
6272         }
6273
6274         lprocfs_mdt_init_vars(&lvars);
6275         rc = class_register_type(&mdt_obd_device_ops, NULL,
6276                                  lvars.module_vars, LUSTRE_MDT_NAME,
6277                                  &mdt_device_type);
6278
6279         if (rc)
6280                 lu_kmem_fini(mdt_caches);
6281         return rc;
6282 }
6283
6284 static void __exit mdt_mod_exit(void)
6285 {
6286         class_unregister_type(LUSTRE_MDT_NAME);
6287         lu_kmem_fini(mdt_caches);
6288 }
6289
6290 #define DEFINE_RPC_HANDLER(base, flags, opc, fn, fmt)                   \
6291 [opc - base] = {                                                        \
6292         .mh_name        = #opc,                                         \
6293         .mh_fail_id     = OBD_FAIL_ ## opc ## _NET,                     \
6294         .mh_opc         = opc,                                          \
6295         .mh_flags       = flags,                                        \
6296         .mh_act         = fn,                                           \
6297         .mh_fmt         = fmt                                           \
6298 }
6299
6300 /* Request with a format known in advance */
6301 #define DEF_MDT_HDL(flags, name, fn)                                    \
6302         DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, &RQF_ ## name)
6303
6304 /* Request with a format we do not yet know */
6305 #define DEF_MDT_HDL_VAR(flags, name, fn)                                \
6306         DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, NULL)
6307
6308 /* Map one non-standard request format handler.  This should probably get
6309  * a common OBD_SET_INFO RPC opcode instead of this mismatch. */
6310 #define RQF_MDS_SET_INFO RQF_OBD_SET_INFO
6311
6312 static struct mdt_handler mdt_mds_ops[] = {
6313 DEF_MDT_HDL(0,                          MDS_CONNECT,      mdt_connect),
6314 DEF_MDT_HDL(0,                          MDS_DISCONNECT,   mdt_disconnect),
6315 DEF_MDT_HDL(0,                          MDS_SET_INFO,     mdt_set_info),
6316 DEF_MDT_HDL(0,                          MDS_GET_INFO,     mdt_get_info),
6317 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_GETSTATUS,    mdt_getstatus),
6318 DEF_MDT_HDL(HABEO_CORPUS,               MDS_GETATTR,      mdt_getattr),
6319 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME, mdt_getattr_name),
6320 DEF_MDT_HDL(HABEO_CORPUS,               MDS_GETXATTR,     mdt_getxattr),
6321 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_STATFS,       mdt_statfs),
6322 DEF_MDT_HDL(0           | MUTABOR,      MDS_REINT,        mdt_reint),
6323 DEF_MDT_HDL(HABEO_CORPUS,               MDS_CLOSE,        mdt_close),
6324 DEF_MDT_HDL(HABEO_CORPUS,               MDS_DONE_WRITING, mdt_done_writing),
6325 DEF_MDT_HDL(0           | HABEO_REFERO, MDS_PIN,          mdt_pin),
6326 DEF_MDT_HDL_VAR(0,                      MDS_SYNC,         mdt_sync),
6327 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR,    mdt_is_subdir),
6328 DEF_MDT_HDL(0,                          MDS_QUOTACHECK,   mdt_quotacheck),
6329 DEF_MDT_HDL(0,                          MDS_QUOTACTL,     mdt_quotactl)
6330 };
6331
6332 #define DEF_OBD_HDL(flags, name, fn)                                    \
6333         DEFINE_RPC_HANDLER(OBD_PING, flags, name, fn, NULL)
6334
6335 static struct mdt_handler mdt_obd_ops[] = {
6336 DEF_OBD_HDL(0,                          OBD_PING,         mdt_obd_ping),
6337 DEF_OBD_HDL(0,                          OBD_LOG_CANCEL,   mdt_obd_log_cancel),
6338 DEF_OBD_HDL(0,                          OBD_QC_CALLBACK,  mdt_obd_qc_callback),
6339 DEF_OBD_HDL(0,                          OBD_IDX_READ,     mdt_obd_idx_read)
6340 };
6341
6342 #define DEF_DLM_HDL_VAR(flags, name, fn)                                \
6343         DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, NULL)
6344 #define DEF_DLM_HDL(flags, name, fn)                                    \
6345         DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, &RQF_ ## name)
6346
6347 static struct mdt_handler mdt_dlm_ops[] = {
6348 DEF_DLM_HDL    (HABEO_CLAVIS,           LDLM_ENQUEUE,     mdt_enqueue),
6349 DEF_DLM_HDL_VAR(HABEO_CLAVIS,           LDLM_CONVERT,     mdt_convert),
6350 DEF_DLM_HDL_VAR(0,                      LDLM_BL_CALLBACK, mdt_bl_callback),
6351 DEF_DLM_HDL_VAR(0,                      LDLM_CP_CALLBACK, mdt_cp_callback)
6352 };
6353
6354 #define DEF_LLOG_HDL(flags, name, fn)                                   \
6355         DEFINE_RPC_HANDLER(LLOG_ORIGIN_HANDLE_CREATE, flags, name, fn, NULL)
6356
6357 static struct mdt_handler mdt_llog_ops[] = {
6358 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_CREATE,        mdt_llog_create),
6359 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_NEXT_BLOCK,    mdt_llog_next_block),
6360 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_READ_HEADER,   mdt_llog_read_header),
6361 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_WRITE_REC,     NULL),
6362 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_CLOSE,         NULL),
6363 DEF_LLOG_HDL(0,         LLOG_ORIGIN_CONNECT,              NULL),
6364 DEF_LLOG_HDL(0,         LLOG_CATINFO,                     NULL),
6365 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_PREV_BLOCK,    mdt_llog_prev_block),
6366 DEF_LLOG_HDL(0,         LLOG_ORIGIN_HANDLE_DESTROY,       mdt_llog_destroy),
6367 };
6368
6369 #define DEF_SEC_HDL(flags, name, fn)                                    \
6370         DEFINE_RPC_HANDLER(SEC_CTX_INIT, flags, name, fn, NULL)
6371
6372 static struct mdt_handler mdt_sec_ctx_ops[] = {
6373 DEF_SEC_HDL(0,                          SEC_CTX_INIT,     mdt_sec_ctx_handle),
6374 DEF_SEC_HDL(0,                          SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
6375 DEF_SEC_HDL(0,                          SEC_CTX_FINI,     mdt_sec_ctx_handle)
6376 };
6377
6378 #define DEF_QUOTA_HDL(flags, name, fn)                          \
6379         DEFINE_RPC_HANDLER(QUOTA_DQACQ, flags, name, fn, &RQF_ ## name)
6380
6381 static struct mdt_handler mdt_quota_ops[] = {
6382 DEF_QUOTA_HDL(HABEO_REFERO,             QUOTA_DQACQ,      mdt_quota_dqacq),
6383 };
6384
6385 static struct mdt_opc_slice mdt_regular_handlers[] = {
6386         {
6387                 .mos_opc_start = MDS_GETATTR,
6388                 .mos_opc_end   = MDS_LAST_OPC,
6389                 .mos_hs        = mdt_mds_ops
6390         },
6391         {
6392                 .mos_opc_start = OBD_PING,
6393                 .mos_opc_end   = OBD_LAST_OPC,
6394                 .mos_hs        = mdt_obd_ops
6395         },
6396         {
6397                 .mos_opc_start = LDLM_ENQUEUE,
6398                 .mos_opc_end   = LDLM_LAST_OPC,
6399                 .mos_hs        = mdt_dlm_ops
6400         },
6401         {
6402                 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
6403                 .mos_opc_end   = LLOG_LAST_OPC,
6404                 .mos_hs        = mdt_llog_ops
6405         },
6406         {
6407                 .mos_opc_start = SEC_CTX_INIT,
6408                 .mos_opc_end   = SEC_LAST_OPC,
6409                 .mos_hs        = mdt_sec_ctx_ops
6410         },
6411         {
6412                 .mos_opc_start = QUOTA_DQACQ,
6413                 .mos_opc_end   = QUOTA_LAST_OPC,
6414                 .mos_hs        = mdt_quota_ops
6415         },
6416         {
6417                 .mos_hs        = NULL
6418         }
6419 };
6420
6421 /* Readpage/readdir handlers */
6422 static struct mdt_handler mdt_readpage_ops[] = {
6423 DEF_MDT_HDL(0,                  MDS_CONNECT,  mdt_connect),
6424 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO, MDS_READPAGE, mdt_readpage),
6425 /* XXX: this is ugly and should be fixed one day, see mdc_close() for
6426  * detailed comments. --umka */
6427 DEF_MDT_HDL(HABEO_CORPUS,               MDS_CLOSE,        mdt_close),
6428 DEF_MDT_HDL(HABEO_CORPUS,               MDS_DONE_WRITING, mdt_done_writing),
6429 };
6430
6431 static struct mdt_opc_slice mdt_readpage_handlers[] = {
6432         {
6433                 .mos_opc_start = MDS_GETATTR,
6434                 .mos_opc_end   = MDS_LAST_OPC,
6435                 .mos_hs        = mdt_readpage_ops
6436         },
6437         {
6438                 .mos_opc_start = OBD_FIRST_OPC,
6439                 .mos_opc_end   = OBD_LAST_OPC,
6440                 .mos_hs        = mdt_obd_ops
6441         },
6442         {
6443                 .mos_hs        = NULL
6444         }
6445 };
6446
6447 /* Cross MDT operation handlers for DNE */
6448 static struct mdt_handler mdt_xmds_ops[] = {
6449 DEF_MDT_HDL(0,                          MDS_CONNECT,      mdt_connect),
6450 DEF_MDT_HDL(HABEO_CORPUS,               MDS_GETATTR,      mdt_getattr),
6451 DEF_MDT_HDL(0           | MUTABOR,      MDS_REINT,        mdt_reint),
6452 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR,    mdt_is_subdir),
6453 };
6454
6455 static struct mdt_opc_slice mdt_xmds_handlers[] = {
6456         {
6457                 .mos_opc_start = MDS_GETATTR,
6458                 .mos_opc_end   = MDS_LAST_OPC,
6459                 .mos_hs        = mdt_xmds_ops
6460         },
6461         {
6462                 .mos_opc_start = OBD_PING,
6463                 .mos_opc_end   = OBD_LAST_OPC,
6464                 .mos_hs        = mdt_obd_ops
6465         },
6466         {
6467                 .mos_opc_start = SEC_CTX_INIT,
6468                 .mos_opc_end   = SEC_LAST_OPC,
6469                 .mos_hs        = mdt_sec_ctx_ops
6470         },
6471         {
6472                 .mos_hs        = NULL
6473         }
6474 };
6475
6476 /* Sequence service handlers */
6477 #define DEF_SEQ_HDL(flags, name, fn)                                    \
6478         DEFINE_RPC_HANDLER(SEQ_QUERY, flags, name, fn, &RQF_ ## name)
6479
6480 static struct mdt_handler mdt_seq_ops[] = {
6481 DEF_SEQ_HDL(0,                          SEQ_QUERY,        (void *)seq_query),
6482 };
6483
6484 static struct mdt_opc_slice mdt_seq_handlers[] = {
6485         {
6486                 .mos_opc_start = SEQ_QUERY,
6487                 .mos_opc_end   = SEQ_LAST_OPC,
6488                 .mos_hs        = mdt_seq_ops
6489         },
6490         {
6491                 .mos_hs        = NULL
6492         }
6493 };
6494
6495 /* FID Location Database handlers */
6496 #define DEF_FLD_HDL(flags, name, fn)                                    \
6497         DEFINE_RPC_HANDLER(FLD_QUERY, flags, name, fn, &RQF_ ## name)
6498
6499 static struct mdt_handler mdt_fld_ops[] = {
6500 DEF_FLD_HDL(0,                          FLD_QUERY,        (void *)fld_query),
6501 };
6502
6503 static struct mdt_opc_slice mdt_fld_handlers[] = {
6504         {
6505                 .mos_opc_start = FLD_QUERY,
6506                 .mos_opc_end   = FLD_LAST_OPC,
6507                 .mos_hs        = mdt_fld_ops
6508         },
6509         {
6510                 .mos_hs        = NULL
6511         }
6512 };
6513
6514 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6515 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6516 MODULE_LICENSE("GPL");
6517
6518 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);