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