Whamcloud - gitweb
0b4359d3852b58de574380ff6fae505f59bb85d8
[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         rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3712         ldlm_rep->lock_policy_res2 = clear_serious(rc);
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_extract_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                 struct ldlm_reply *rep;
3968
3969                 /* execute policy */
3970                 rc = flv->it_act(opc, info, lockp, flags);
3971
3972                 rep = req_capsule_server_get(pill, &RMF_DLM_REP);
3973                 rep->lock_policy_res2 =
3974                         ptlrpc_status_hton(rep->lock_policy_res2);
3975         } else {
3976                 rc = -EOPNOTSUPP;
3977         }
3978         RETURN(rc);
3979 }
3980
3981 static int mdt_intent_policy(struct ldlm_namespace *ns,
3982                              struct ldlm_lock **lockp, void *req_cookie,
3983                              ldlm_mode_t mode, __u64 flags, void *data)
3984 {
3985         struct mdt_thread_info *info;
3986         struct ptlrpc_request  *req  =  req_cookie;
3987         struct ldlm_intent     *it;
3988         struct req_capsule     *pill;
3989         int rc;
3990
3991         ENTRY;
3992
3993         LASSERT(req != NULL);
3994
3995         info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3996                                   &mdt_thread_key);
3997         LASSERT(info != NULL);
3998         pill = info->mti_pill;
3999         LASSERT(pill->rc_req == req);
4000
4001         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4002                 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4003                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4004                 if (it != NULL) {
4005                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
4006                         if (rc == 0)
4007                                 rc = ELDLM_OK;
4008
4009                         /* Lock without inodebits makes no sense and will oops
4010                          * later in ldlm. Let's check it now to see if we have
4011                          * ibits corrupted somewhere in mdt_intent_opc().
4012                          * The case for client miss to set ibits has been
4013                          * processed by others. */
4014                         LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
4015                                         lr_type == LDLM_IBITS,
4016                                      info->mti_dlm_req->lock_desc.\
4017                                         l_policy_data.l_inodebits.bits != 0));
4018                 } else
4019                         rc = err_serious(-EFAULT);
4020         } else {
4021                 /* No intent was provided */
4022                 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
4023                 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4024                 rc = req_capsule_server_pack(pill);
4025                 if (rc)
4026                         rc = err_serious(rc);
4027         }
4028         RETURN(rc);
4029 }
4030
4031 static int mdt_seq_fini(const struct lu_env *env,
4032                         struct mdt_device *m)
4033 {
4034         return seq_site_fini(env, mdt_seq_site(m));
4035 }
4036
4037 static int mdt_seq_init(const struct lu_env *env,
4038                         const char *uuid,
4039                         struct mdt_device *m)
4040 {
4041         struct seq_server_site *ss;
4042         char *prefix;
4043         int rc;
4044         ENTRY;
4045
4046         ss = mdt_seq_site(m);
4047
4048         /*
4049          * This is sequence-controller node. Init seq-controller server on local
4050          * MDT.
4051          */
4052         if (ss->ss_node_id == 0) {
4053                 LASSERT(ss->ss_control_seq == NULL);
4054
4055                 OBD_ALLOC_PTR(ss->ss_control_seq);
4056                 if (ss->ss_control_seq == NULL)
4057                         RETURN(-ENOMEM);
4058
4059                 rc = seq_server_init(ss->ss_control_seq,
4060                                      m->mdt_bottom, uuid,
4061                                      LUSTRE_SEQ_CONTROLLER,
4062                                      ss,
4063                                      env);
4064
4065                 if (rc)
4066                         GOTO(out_seq_fini, rc);
4067
4068                 OBD_ALLOC_PTR(ss->ss_client_seq);
4069                 if (ss->ss_client_seq == NULL)
4070                         GOTO(out_seq_fini, rc = -ENOMEM);
4071
4072                 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4073                 if (prefix == NULL) {
4074                         OBD_FREE_PTR(ss->ss_client_seq);
4075                         GOTO(out_seq_fini, rc = -ENOMEM);
4076                 }
4077
4078                 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4079                          uuid);
4080
4081                 /*
4082                  * Init seq-controller client after seq-controller server is
4083                  * ready. Pass ss->ss_control_seq to it for direct talking.
4084                  */
4085                 rc = seq_client_init(ss->ss_client_seq, NULL,
4086                                      LUSTRE_SEQ_METADATA, prefix,
4087                                      ss->ss_control_seq);
4088                 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4089
4090                 if (rc)
4091                         GOTO(out_seq_fini, rc);
4092         }
4093
4094         /* Init seq-server on local MDT */
4095         LASSERT(ss->ss_server_seq == NULL);
4096
4097         OBD_ALLOC_PTR(ss->ss_server_seq);
4098         if (ss->ss_server_seq == NULL)
4099                 GOTO(out_seq_fini, rc = -ENOMEM);
4100
4101         rc = seq_server_init(ss->ss_server_seq,
4102                              m->mdt_bottom, uuid,
4103                              LUSTRE_SEQ_SERVER,
4104                              ss,
4105                              env);
4106         if (rc)
4107                 GOTO(out_seq_fini, rc = -ENOMEM);
4108
4109         /* Assign seq-controller client to local seq-server. */
4110         if (ss->ss_node_id == 0) {
4111                 LASSERT(ss->ss_client_seq != NULL);
4112
4113                 rc = seq_server_set_cli(ss->ss_server_seq,
4114                                         ss->ss_client_seq,
4115                                         env);
4116         }
4117
4118         EXIT;
4119 out_seq_fini:
4120         if (rc)
4121                 mdt_seq_fini(env, m);
4122
4123         return rc;
4124 }
4125
4126 /*
4127  * FLD wrappers
4128  */
4129 static int mdt_fld_fini(const struct lu_env *env,
4130                         struct mdt_device *m)
4131 {
4132         struct seq_server_site *ss = mdt_seq_site(m);
4133         ENTRY;
4134
4135         if (ss && ss->ss_server_fld) {
4136                 fld_server_fini(env, ss->ss_server_fld);
4137                 OBD_FREE_PTR(ss->ss_server_fld);
4138                 ss->ss_server_fld = NULL;
4139         }
4140
4141         RETURN(0);
4142 }
4143
4144 static int mdt_fld_init(const struct lu_env *env,
4145                         const char *uuid,
4146                         struct mdt_device *m)
4147 {
4148         struct seq_server_site *ss;
4149         int rc;
4150         ENTRY;
4151
4152         ss = mdt_seq_site(m);
4153
4154         OBD_ALLOC_PTR(ss->ss_server_fld);
4155         if (ss->ss_server_fld == NULL)
4156                 RETURN(rc = -ENOMEM);
4157
4158         rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4159                              ss->ss_node_id, LU_SEQ_RANGE_MDT);
4160         if (rc) {
4161                 OBD_FREE_PTR(ss->ss_server_fld);
4162                 ss->ss_server_fld = NULL;
4163                 RETURN(rc);
4164         }
4165
4166         RETURN(0);
4167 }
4168
4169 static void mdt_stack_pre_fini(const struct lu_env *env,
4170                            struct mdt_device *m, struct lu_device *top)
4171 {
4172         struct obd_device       *obd = mdt2obd_dev(m);
4173         struct lustre_cfg_bufs  *bufs;
4174         struct lustre_cfg       *lcfg;
4175         struct mdt_thread_info  *info;
4176         ENTRY;
4177
4178         LASSERT(top);
4179
4180         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4181         LASSERT(info != NULL);
4182
4183         bufs = &info->mti_u.bufs;
4184
4185         LASSERT(m->mdt_child_exp);
4186         LASSERT(m->mdt_child_exp->exp_obd);
4187         obd = m->mdt_child_exp->exp_obd;
4188
4189         /* process cleanup, pass mdt obd name to get obd umount flags */
4190         /* XXX: this is needed because all layers are referenced by
4191          * objects (some of them are pinned by osd, for example *
4192          * the proper solution should be a model where object used
4193          * by osd only doesn't have mdt/mdd slices -bzzz */
4194         lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4195         lustre_cfg_bufs_set_string(bufs, 1, NULL);
4196         lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4197         if (!lcfg) {
4198                 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
4199                 return;
4200         }
4201         top->ld_ops->ldo_process_config(env, top, lcfg);
4202         lustre_cfg_free(lcfg);
4203         EXIT;
4204 }
4205
4206 static void mdt_stack_fini(const struct lu_env *env,
4207                            struct mdt_device *m, struct lu_device *top)
4208 {
4209         struct obd_device       *obd = mdt2obd_dev(m);
4210         struct lustre_cfg_bufs  *bufs;
4211         struct lustre_cfg       *lcfg;
4212         struct mdt_thread_info  *info;
4213         char                     flags[3] = "";
4214         ENTRY;
4215
4216         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4217         LASSERT(info != NULL);
4218
4219         lu_dev_del_linkage(top->ld_site, top);
4220
4221         lu_site_purge(env, top->ld_site, -1);
4222
4223         bufs = &info->mti_u.bufs;
4224         /* process cleanup, pass mdt obd name to get obd umount flags */
4225         /* another purpose is to let all layers to release their objects */
4226         lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4227         if (obd->obd_force)
4228                 strcat(flags, "F");
4229         if (obd->obd_fail)
4230                 strcat(flags, "A");
4231         lustre_cfg_bufs_set_string(bufs, 1, flags);
4232         lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4233         if (!lcfg) {
4234                 CERROR("Cannot alloc lcfg!\n");
4235                 return;
4236         }
4237         LASSERT(top);
4238         top->ld_ops->ldo_process_config(env, top, lcfg);
4239         lustre_cfg_free(lcfg);
4240
4241         lu_site_purge(env, top->ld_site, -1);
4242
4243         m->mdt_child = NULL;
4244         m->mdt_bottom = NULL;
4245
4246         obd_disconnect(m->mdt_child_exp);
4247         m->mdt_child_exp = NULL;
4248
4249         obd_disconnect(m->mdt_bottom_exp);
4250         m->mdt_child_exp = NULL;
4251 }
4252
4253 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4254                                const char *next, struct obd_export **exp)
4255 {
4256         struct obd_connect_data *data = NULL;
4257         struct obd_device       *obd;
4258         int                      rc;
4259         ENTRY;
4260
4261         OBD_ALLOC_PTR(data);
4262         if (data == NULL)
4263                 GOTO(out, rc = -ENOMEM);
4264
4265         obd = class_name2obd(next);
4266         if (obd == NULL) {
4267                 CERROR("%s: can't locate next device: %s\n",
4268                        mdt_obd_name(m), next);
4269                 GOTO(out, rc = -ENOTCONN);
4270         }
4271
4272         data->ocd_connect_flags = OBD_CONNECT_VERSION;
4273         data->ocd_version = LUSTRE_VERSION_CODE;
4274
4275         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4276         if (rc) {
4277                 CERROR("%s: cannot connect to next dev %s (%d)\n",
4278                        mdt_obd_name(m), next, rc);
4279                 GOTO(out, rc);
4280         }
4281
4282 out:
4283         if (data)
4284                 OBD_FREE_PTR(data);
4285         RETURN(rc);
4286 }
4287
4288 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4289                           struct lustre_cfg *cfg)
4290 {
4291         char                   *dev = lustre_cfg_string(cfg, 0);
4292         int                     rc, name_size, uuid_size;
4293         char                   *name, *uuid, *p;
4294         struct lustre_cfg_bufs *bufs;
4295         struct lustre_cfg      *lcfg;
4296         struct obd_device      *obd;
4297         struct lustre_profile  *lprof;
4298         struct lu_site         *site;
4299         ENTRY;
4300
4301         /* in 1.8 we had the only device in the stack - MDS.
4302          * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4303          * in 2.3 OSD is instantiated by obd_mount.c, so we need
4304          * to generate names and setup MDT, MDD. MDT will be using
4305          * generated name to connect to MDD. for MDD the next device
4306          * will be LOD with name taken from so called "profile" which
4307          * is generated by mount_option line
4308          *
4309          * 1.8 MGS generates config. commands like this:
4310          *   #06 (104)mount_option 0:  1:lustre-MDT0000  2:lustre-mdtlov
4311          *   #08 (120)setup   0:lustre-MDT0000  1:dev 2:type 3:lustre-MDT0000
4312          * 2.0 MGS generates config. commands like this:
4313          *   #07 (112)mount_option 0:  1:lustre-MDT0000  2:lustre-MDT0000-mdtlov
4314          *   #08 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4315          *                    3:lustre-MDT0000-mdtlov  4:f
4316          *
4317          * we generate MDD name from MDT one, just replacing T with D
4318          *
4319          * after all the preparations, the logical equivalent will be
4320          *   #01 (160)setup   0:lustre-MDD0000  1:lustre-MDD0000_UUID  2:0
4321          *                    3:lustre-MDT0000-mdtlov  4:f
4322          *   #02 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4323          *                    3:lustre-MDD0000  4:f
4324          *
4325          *  notice we build the stack from down to top: MDD first, then MDT */
4326
4327         name_size = MAX_OBD_NAME;
4328         uuid_size = MAX_OBD_NAME;
4329
4330         OBD_ALLOC(name, name_size);
4331         OBD_ALLOC(uuid, uuid_size);
4332         if (name == NULL || uuid == NULL)
4333                 GOTO(cleanup_mem, rc = -ENOMEM);
4334
4335         OBD_ALLOC_PTR(bufs);
4336         if (!bufs)
4337                 GOTO(cleanup_mem, rc = -ENOMEM);
4338
4339         strcpy(name, dev);
4340         p = strstr(name, "-MDT");
4341         if (p == NULL)
4342                 GOTO(cleanup_mem, rc = -ENOMEM);
4343         p[3] = 'D';
4344
4345         snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4346
4347         lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4348         if (lprof == NULL || lprof->lp_dt == NULL) {
4349                 CERROR("can't find the profile: %s\n",
4350                        lustre_cfg_string(cfg, 0));
4351                 GOTO(cleanup_mem, rc = -EINVAL);
4352         }
4353
4354         lustre_cfg_bufs_reset(bufs, name);
4355         lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4356         lustre_cfg_bufs_set_string(bufs, 2, uuid);
4357         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4358
4359         lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4360         if (!lcfg)
4361                 GOTO(free_bufs, rc = -ENOMEM);
4362
4363         rc = class_attach(lcfg);
4364         if (rc)
4365                 GOTO(lcfg_cleanup, rc);
4366
4367         obd = class_name2obd(name);
4368         if (!obd) {
4369                 CERROR("Can not find obd %s (%s in config)\n",
4370                        MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4371                 GOTO(class_detach, rc = -EINVAL);
4372         }
4373
4374         lustre_cfg_free(lcfg);
4375
4376         lustre_cfg_bufs_reset(bufs, name);
4377         lustre_cfg_bufs_set_string(bufs, 1, uuid);
4378         lustre_cfg_bufs_set_string(bufs, 2, dev);
4379         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4380
4381         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4382
4383         rc = class_setup(obd, lcfg);
4384         if (rc)
4385                 GOTO(class_detach, rc);
4386
4387         /* connect to MDD we just setup */
4388         rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4389         if (rc)
4390                 RETURN(rc);
4391
4392         site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4393         LASSERT(site);
4394         LASSERT(mdt_lu_site(mdt) == NULL);
4395         mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4396         site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4397         mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4398
4399
4400         /* now connect to bottom OSD */
4401         snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4402         rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4403         if (rc)
4404                 RETURN(rc);
4405         mdt->mdt_bottom =
4406                 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4407
4408
4409         rc = lu_env_refill((struct lu_env *)env);
4410         if (rc != 0)
4411                 CERROR("Failure to refill session: '%d'\n", rc);
4412
4413         lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4414
4415         EXIT;
4416 class_detach:
4417         if (rc)
4418                 class_detach(obd, lcfg);
4419 lcfg_cleanup:
4420         lustre_cfg_free(lcfg);
4421 free_bufs:
4422         OBD_FREE_PTR(bufs);
4423 cleanup_mem:
4424         if (name)
4425                 OBD_FREE(name, name_size);
4426         if (uuid)
4427                 OBD_FREE(uuid, uuid_size);
4428         RETURN(rc);
4429 }
4430
4431 /* setup quota master target on MDT0 */
4432 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4433                           struct lustre_cfg *cfg)
4434 {
4435         struct obd_device       *obd;
4436         char                    *dev = lustre_cfg_string(cfg, 0);
4437         char                    *qmtname, *uuid, *p;
4438         struct lustre_cfg_bufs  *bufs;
4439         struct lustre_cfg       *lcfg;
4440         struct lustre_profile   *lprof;
4441         struct obd_connect_data *data;
4442         int                      rc;
4443         ENTRY;
4444
4445         LASSERT(mdt->mdt_qmt_exp == NULL);
4446         LASSERT(mdt->mdt_qmt_dev == NULL);
4447
4448         /* quota master is on MDT0 only for now */
4449         if (mdt->mdt_seq_site.ss_node_id != 0)
4450                 RETURN(0);
4451
4452         /* MGS generates config commands which look as follows:
4453          *   #01 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
4454          *                    3:lustre-MDT0000-mdtlov  4:f
4455          *
4456          * We generate the QMT name from the MDT one, just replacing MD with QM
4457          * after all the preparations, the logical equivalent will be:
4458          *   #01 (160)setup   0:lustre-QMT0000  1:lustre-QMT0000_UUID  2:0
4459          *                    3:lustre-MDT0000-osd  4:f */
4460         OBD_ALLOC(qmtname, MAX_OBD_NAME);
4461         OBD_ALLOC(uuid, UUID_MAX);
4462         OBD_ALLOC_PTR(bufs);
4463         OBD_ALLOC_PTR(data);
4464         if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4465                 GOTO(cleanup_mem, rc = -ENOMEM);
4466
4467         strcpy(qmtname, dev);
4468         p = strstr(qmtname, "-MDT");
4469         if (p == NULL)
4470                 GOTO(cleanup_mem, rc = -ENOMEM);
4471         /* replace MD with QM */
4472         p[1] = 'Q';
4473         p[2] = 'M';
4474
4475         snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4476
4477         lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4478         if (lprof == NULL || lprof->lp_dt == NULL) {
4479                 CERROR("can't find profile for %s\n",
4480                        lustre_cfg_string(cfg, 0));
4481                 GOTO(cleanup_mem, rc = -EINVAL);
4482         }
4483
4484         lustre_cfg_bufs_reset(bufs, qmtname);
4485         lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4486         lustre_cfg_bufs_set_string(bufs, 2, uuid);
4487         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4488
4489         lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4490         if (!lcfg)
4491                 GOTO(cleanup_mem, rc = -ENOMEM);
4492
4493         rc = class_attach(lcfg);
4494         if (rc)
4495                 GOTO(lcfg_cleanup, rc);
4496
4497         obd = class_name2obd(qmtname);
4498         if (!obd) {
4499                 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4500                        lustre_cfg_string(cfg, 0));
4501                 GOTO(class_detach, rc = -EINVAL);
4502         }
4503
4504         lustre_cfg_free(lcfg);
4505
4506         lustre_cfg_bufs_reset(bufs, qmtname);
4507         lustre_cfg_bufs_set_string(bufs, 1, uuid);
4508         lustre_cfg_bufs_set_string(bufs, 2, dev);
4509
4510         /* for quota, the next device should be the OSD device */
4511         lustre_cfg_bufs_set_string(bufs, 3,
4512                                    mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4513
4514         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4515
4516         rc = class_setup(obd, lcfg);
4517         if (rc)
4518                 GOTO(class_detach, rc);
4519
4520         mdt->mdt_qmt_dev = obd->obd_lu_dev;
4521
4522         /* configure local quota objects */
4523         rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4524                                                    &mdt->mdt_md_dev.md_lu_dev,
4525                                                    mdt->mdt_qmt_dev);
4526         if (rc)
4527                 GOTO(class_cleanup, rc);
4528
4529         /* connect to quota master target */
4530         data->ocd_connect_flags = OBD_CONNECT_VERSION;
4531         data->ocd_version = LUSTRE_VERSION_CODE;
4532         rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4533                          data, NULL);
4534         if (rc) {
4535                 CERROR("cannot connect to quota master device %s (%d)\n",
4536                        qmtname, rc);
4537                 GOTO(class_cleanup, rc);
4538         }
4539
4540         EXIT;
4541 class_cleanup:
4542         if (rc) {
4543                 class_manual_cleanup(obd);
4544                 mdt->mdt_qmt_dev = NULL;
4545         }
4546 class_detach:
4547         if (rc)
4548                 class_detach(obd, lcfg);
4549 lcfg_cleanup:
4550         lustre_cfg_free(lcfg);
4551 cleanup_mem:
4552         if (bufs)
4553                 OBD_FREE_PTR(bufs);
4554         if (qmtname)
4555                 OBD_FREE(qmtname, MAX_OBD_NAME);
4556         if (uuid)
4557                 OBD_FREE(uuid, UUID_MAX);
4558         if (data)
4559                 OBD_FREE_PTR(data);
4560         return rc;
4561 }
4562
4563 /* Shutdown quota master target associated with mdt */
4564 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4565 {
4566         ENTRY;
4567
4568         if (mdt->mdt_qmt_exp == NULL)
4569                 RETURN_EXIT;
4570         LASSERT(mdt->mdt_qmt_dev != NULL);
4571
4572         /* the qmt automatically shuts down when the mdt disconnects */
4573         obd_disconnect(mdt->mdt_qmt_exp);
4574         mdt->mdt_qmt_exp = NULL;
4575         mdt->mdt_qmt_dev = NULL;
4576         EXIT;
4577 }
4578
4579 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4580 {
4581         struct md_device  *next = m->mdt_child;
4582         struct lu_device  *d    = &m->mdt_md_dev.md_lu_dev;
4583         struct obd_device *obd = mdt2obd_dev(m);
4584         ENTRY;
4585
4586         target_recovery_fini(obd);
4587
4588         ping_evictor_stop();
4589
4590
4591         mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4592         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4593         obd_exports_barrier(obd);
4594         obd_zombie_barrier();
4595
4596         mdt_procfs_fini(m);
4597
4598         tgt_fini(env, &m->mdt_lut);
4599         mdt_fs_cleanup(env, m);
4600         upcall_cache_cleanup(m->mdt_identity_cache);
4601         m->mdt_identity_cache = NULL;
4602
4603         if (m->mdt_namespace != NULL) {
4604                 ldlm_namespace_free(m->mdt_namespace, NULL,
4605                                     d->ld_obd->obd_force);
4606                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4607         }
4608
4609         mdt_quota_fini(env, m);
4610
4611         cfs_free_nidlist(&m->mdt_nosquash_nids);
4612         if (m->mdt_nosquash_str) {
4613                 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4614                 m->mdt_nosquash_str = NULL;
4615                 m->mdt_nosquash_strlen = 0;
4616         }
4617
4618         next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK,
4619                                     0, NULL);
4620         mdt_seq_fini(env, m);
4621         mdt_fld_fini(env, m);
4622         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4623
4624         next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4625         cfs_timer_disarm(&m->mdt_ck_timer);
4626         mdt_ck_thread_stop(m);
4627
4628         /*
4629          * Finish the stack
4630          */
4631         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4632
4633         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4634
4635         server_put_mount(mdt_obd_name(m), NULL);
4636
4637         EXIT;
4638 }
4639
4640 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4641 {
4642         struct mdt_device       *m = mdt_dev(obd->obd_lu_dev);
4643         struct sptlrpc_rule_set  tmp_rset;
4644         int                      rc;
4645
4646         sptlrpc_rule_set_init(&tmp_rset);
4647         rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4648         if (rc) {
4649                 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4650                        mdt_obd_name(m), rc);
4651                 return rc;
4652         }
4653
4654         sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4655
4656         write_lock(&m->mdt_sptlrpc_lock);
4657         sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4658         m->mdt_sptlrpc_rset = tmp_rset;
4659         write_unlock(&m->mdt_sptlrpc_lock);
4660
4661         return 0;
4662 }
4663
4664 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4665
4666 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4667                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
4668 {
4669         struct mdt_thread_info    *info;
4670         struct obd_device         *obd;
4671         const char                *dev = lustre_cfg_string(cfg, 0);
4672         const char                *num = lustre_cfg_string(cfg, 2);
4673         struct lustre_mount_info  *lmi = NULL;
4674         struct lustre_sb_info     *lsi;
4675         struct lu_site            *s;
4676         struct seq_server_site    *ss_site;
4677         const char                *identity_upcall = "NONE";
4678         struct md_device          *next;
4679         int                        rc;
4680         int                        node_id;
4681         mntopt_t                   mntopts;
4682         ENTRY;
4683
4684         md_device_init(&m->mdt_md_dev, ldt);
4685         /*
4686          * Environment (env) might be missing mdt_thread_key values at that
4687          * point, if device is allocated when mdt_thread_key is in QUIESCENT
4688          * mode.
4689          *
4690          * Usually device allocation path doesn't use module key values, but
4691          * mdt has to do a lot of work here, so allocate key value.
4692          */
4693         rc = lu_env_refill((struct lu_env *)env);
4694         if (rc != 0)
4695                 RETURN(rc);
4696
4697         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4698         LASSERT(info != NULL);
4699
4700         obd = class_name2obd(dev);
4701         LASSERT(obd != NULL);
4702
4703         m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4704
4705         m->mdt_som_conf = 0;
4706
4707         m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4708         lmi = server_get_mount(dev);
4709         if (lmi == NULL) {
4710                 CERROR("Cannot get mount info for %s!\n", dev);
4711                 RETURN(-EFAULT);
4712         } else {
4713                 lsi = s2lsi(lmi->lmi_sb);
4714                 /* CMD is supported only in IAM mode */
4715                 LASSERT(num);
4716                 node_id = simple_strtol(num, NULL, 10);
4717                 obd->u.obt.obt_magic = OBT_MAGIC;
4718         }
4719
4720         rwlock_init(&m->mdt_sptlrpc_lock);
4721         sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4722
4723         spin_lock_init(&m->mdt_ioepoch_lock);
4724         m->mdt_opts.mo_compat_resname = 0;
4725         m->mdt_opts.mo_mds_capa = 1;
4726         m->mdt_opts.mo_oss_capa = 1;
4727         m->mdt_capa_timeout = CAPA_TIMEOUT;
4728         m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4729         m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4730         m->mdt_squash_uid = 0;
4731         m->mdt_squash_gid = 0;
4732         CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4733         m->mdt_nosquash_str = NULL;
4734         m->mdt_nosquash_strlen = 0;
4735         init_rwsem(&m->mdt_squash_sem);
4736         spin_lock_init(&m->mdt_osfs_lock);
4737         m->mdt_osfs_age = cfs_time_shift_64(-1000);
4738         m->mdt_enable_remote_dir = 0;
4739         m->mdt_enable_remote_dir_gid = 0;
4740
4741         m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
4742         m->mdt_md_dev.md_lu_dev.ld_obd = obd;
4743         /* set this lu_device to obd, because error handling need it */
4744         obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
4745
4746         /* init the stack */
4747         rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4748         if (rc) {
4749                 CERROR("%s: Can't init device stack, rc %d\n",
4750                        mdt_obd_name(m), rc);
4751                 GOTO(err_lmi, rc);
4752         }
4753
4754         s = mdt_lu_site(m);
4755         ss_site = mdt_seq_site(m);
4756         s->ld_seq_site = ss_site;
4757         ss_site->ss_lu = s;
4758
4759         /* set server index */
4760         ss_site->ss_node_id = node_id;
4761
4762         /* failover is the default
4763          * FIXME: we do not failout mds0/mgs, which may cause some problems.
4764          * assumed whose ss_node_id == 0 XXX
4765          * */
4766         obd->obd_replayable = 1;
4767         /* No connection accepted until configurations will finish */
4768         obd->obd_no_conn = 1;
4769
4770         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4771                 char *str = lustre_cfg_string(cfg, 4);
4772                 if (strchr(str, 'n')) {
4773                         CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4774                         obd->obd_replayable = 0;
4775                 }
4776         }
4777
4778         rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4779         if (rc)
4780                 GOTO(err_fini_stack, rc);
4781
4782         rc = mdt_fld_init(env, mdt_obd_name(m), m);
4783         if (rc)
4784                 GOTO(err_lut, rc);
4785
4786         rc = mdt_seq_init(env, mdt_obd_name(m), m);
4787         if (rc)
4788                 GOTO(err_fini_fld, rc);
4789
4790         snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4791                  LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4792         m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4793                                               LDLM_NAMESPACE_SERVER,
4794                                               LDLM_NAMESPACE_GREEDY,
4795                                               LDLM_NS_TYPE_MDT);
4796         if (m->mdt_namespace == NULL)
4797                 GOTO(err_fini_seq, rc = -ENOMEM);
4798
4799         m->mdt_namespace->ns_lvbp = m;
4800         m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4801
4802         ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4803         /* set obd_namespace for compatibility with old code */
4804         obd->obd_namespace = m->mdt_namespace;
4805
4806         cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4807
4808         rc = mdt_ck_thread_start(m);
4809         if (rc)
4810                 GOTO(err_free_ns, rc);
4811
4812         rc = mdt_fs_setup(env, m, obd, lsi);
4813         if (rc)
4814                 GOTO(err_capa, rc);
4815
4816         mdt_adapt_sptlrpc_conf(obd, 1);
4817
4818         next = m->mdt_child;
4819         rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4820                                          &mntopts);
4821         if (rc)
4822                 GOTO(err_llog_cleanup, rc);
4823
4824         if (mntopts & MNTOPT_USERXATTR)
4825                 m->mdt_opts.mo_user_xattr = 1;
4826         else
4827                 m->mdt_opts.mo_user_xattr = 0;
4828
4829         if (mntopts & MNTOPT_ACL)
4830                 m->mdt_opts.mo_acl = 1;
4831         else
4832                 m->mdt_opts.mo_acl = 0;
4833
4834         /* XXX: to support suppgid for ACL, we enable identity_upcall
4835          * by default, otherwise, maybe got unexpected -EACCESS. */
4836         if (m->mdt_opts.mo_acl)
4837                 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4838
4839         m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4840                                                 identity_upcall,
4841                                                 &mdt_identity_upcall_cache_ops);
4842         if (IS_ERR(m->mdt_identity_cache)) {
4843                 rc = PTR_ERR(m->mdt_identity_cache);
4844                 m->mdt_identity_cache = NULL;
4845                 GOTO(err_llog_cleanup, rc);
4846         }
4847
4848         rc = mdt_procfs_init(m, dev);
4849         if (rc) {
4850                 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4851                 GOTO(err_recovery, rc);
4852         }
4853
4854         rc = mdt_quota_init(env, m, cfg);
4855         if (rc)
4856                 GOTO(err_procfs, rc);
4857
4858         m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4859         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4860                            "mdt_ldlm_client", m->mdt_ldlm_client);
4861
4862         ping_evictor_start();
4863
4864         /* recovery will be started upon mdt_prepare()
4865          * when the whole stack is complete and ready
4866          * to serve the requests */
4867
4868         mdt_init_capa_ctxt(env, m);
4869
4870         /* Reduce the initial timeout on an MDS because it doesn't need such
4871          * a long timeout as an OST does. Adaptive timeouts will adjust this
4872          * value appropriately. */
4873         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4874                 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4875
4876         RETURN(0);
4877
4878 err_procfs:
4879         mdt_procfs_fini(m);
4880 err_recovery:
4881         target_recovery_fini(obd);
4882         upcall_cache_cleanup(m->mdt_identity_cache);
4883         m->mdt_identity_cache = NULL;
4884 err_llog_cleanup:
4885         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4886         mdt_fs_cleanup(env, m);
4887 err_capa:
4888         cfs_timer_disarm(&m->mdt_ck_timer);
4889         mdt_ck_thread_stop(m);
4890 err_free_ns:
4891         ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4892         obd->obd_namespace = m->mdt_namespace = NULL;
4893 err_fini_seq:
4894         mdt_seq_fini(env, m);
4895 err_fini_fld:
4896         mdt_fld_fini(env, m);
4897 err_lut:
4898         tgt_fini(env, &m->mdt_lut);
4899 err_fini_stack:
4900         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4901 err_lmi:
4902         if (lmi)
4903                 server_put_mount(dev, lmi->lmi_mnt);
4904         return (rc);
4905 }
4906
4907 /* For interoperability, the left element is old parameter, the right one
4908  * is the new version of the parameter, if some parameter is deprecated,
4909  * the new version should be set as NULL. */
4910 static struct cfg_interop_param mdt_interop_param[] = {
4911         { "mdt.group_upcall",   NULL },
4912         { "mdt.quota_type",     NULL },
4913         { "mdd.quota_type",     NULL },
4914         { "mdt.rootsquash",     "mdt.root_squash" },
4915         { "mdt.nosquash_nid",   "mdt.nosquash_nids" },
4916         { NULL }
4917 };
4918
4919 /* used by MGS to process specific configurations */
4920 static int mdt_process_config(const struct lu_env *env,
4921                               struct lu_device *d, struct lustre_cfg *cfg)
4922 {
4923         struct mdt_device *m = mdt_dev(d);
4924         struct md_device *md_next = m->mdt_child;
4925         struct lu_device *next = md2lu_dev(md_next);
4926         int rc;
4927         ENTRY;
4928
4929         switch (cfg->lcfg_command) {
4930         case LCFG_PARAM: {
4931                 struct lprocfs_static_vars  lvars;
4932                 struct obd_device          *obd = d->ld_obd;
4933
4934                 /* For interoperability */
4935                 struct cfg_interop_param   *ptr = NULL;
4936                 struct lustre_cfg          *old_cfg = NULL;
4937                 char                       *param = NULL;
4938
4939                 param = lustre_cfg_string(cfg, 1);
4940                 if (param == NULL) {
4941                         CERROR("param is empty\n");
4942                         rc = -EINVAL;
4943                         break;
4944                 }
4945
4946                 ptr = class_find_old_param(param, mdt_interop_param);
4947                 if (ptr != NULL) {
4948                         if (ptr->new_param == NULL) {
4949                                 rc = 0;
4950                                 CWARN("For interoperability, skip this %s."
4951                                       " It is obsolete.\n", ptr->old_param);
4952                                 break;
4953                         }
4954
4955                         CWARN("Found old param %s, changed it to %s.\n",
4956                               ptr->old_param, ptr->new_param);
4957
4958                         old_cfg = cfg;
4959                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4960                         if (IS_ERR(cfg)) {
4961                                 rc = PTR_ERR(cfg);
4962                                 break;
4963                         }
4964                 }
4965
4966                 lprocfs_mdt_init_vars(&lvars);
4967                 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4968                                               cfg, obd);
4969                 if (rc > 0 || rc == -ENOSYS)
4970                         /* we don't understand; pass it on */
4971                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
4972
4973                 if (old_cfg != NULL)
4974                         lustre_cfg_free(cfg);
4975
4976                 break;
4977         }
4978         default:
4979                 /* others are passed further */
4980                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4981                 break;
4982         }
4983         RETURN(rc);
4984 }
4985
4986 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4987                                           const struct lu_object_header *hdr,
4988                                           struct lu_device *d)
4989 {
4990         struct mdt_object *mo;
4991
4992         ENTRY;
4993
4994         OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
4995         if (mo != NULL) {
4996                 struct lu_object *o;
4997                 struct lu_object_header *h;
4998
4999                 o = &mo->mot_obj.mo_lu;
5000                 h = &mo->mot_header;
5001                 lu_object_header_init(h);
5002                 lu_object_init(o, h, d);
5003                 lu_object_add_top(h, o);
5004                 o->lo_ops = &mdt_obj_ops;
5005                 mutex_init(&mo->mot_ioepoch_mutex);
5006                 mutex_init(&mo->mot_lov_mutex);
5007                 RETURN(o);
5008         } else
5009                 RETURN(NULL);
5010 }
5011
5012 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5013                            const struct lu_object_conf *unused)
5014 {
5015         struct mdt_device *d = mdt_dev(o->lo_dev);
5016         struct lu_device  *under;
5017         struct lu_object  *below;
5018         int                rc = 0;
5019         ENTRY;
5020
5021         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5022                PFID(lu_object_fid(o)));
5023
5024         under = &d->mdt_child->md_lu_dev;
5025         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5026         if (below != NULL) {
5027                 lu_object_add(o, below);
5028         } else
5029                 rc = -ENOMEM;
5030
5031         RETURN(rc);
5032 }
5033
5034 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5035 {
5036         struct mdt_object *mo = mdt_obj(o);
5037         struct lu_object_header *h;
5038         ENTRY;
5039
5040         h = o->lo_header;
5041         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5042                PFID(lu_object_fid(o)));
5043
5044         lu_object_fini(o);
5045         lu_object_header_fini(h);
5046         OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5047
5048         EXIT;
5049 }
5050
5051 static int mdt_object_print(const struct lu_env *env, void *cookie,
5052                             lu_printer_t p, const struct lu_object *o)
5053 {
5054         struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5055         return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5056                     "flags="LPX64", epochcount=%d, writecount=%d)",
5057                     mdto, mdto->mot_ioepoch, mdto->mot_flags,
5058                     mdto->mot_ioepoch_count, mdto->mot_writecount);
5059 }
5060
5061 static int mdt_prepare(const struct lu_env *env,
5062                 struct lu_device *pdev,
5063                 struct lu_device *cdev)
5064 {
5065         struct mdt_device *mdt = mdt_dev(cdev);
5066         struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5067         struct obd_device *obd = cdev->ld_obd;
5068         int rc;
5069
5070         ENTRY;
5071
5072         LASSERT(obd);
5073
5074         rc = next->ld_ops->ldo_prepare(env, cdev, next);
5075         if (rc)
5076                 RETURN(rc);
5077
5078         rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5079         if (rc)
5080                 RETURN(rc);
5081
5082         rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5083                                                    OBD_IOC_START_LFSCK,
5084                                                    0, NULL);
5085         if (rc != 0) {
5086                 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5087                       mdt_obd_name(mdt), rc);
5088                 rc = 0;
5089         }
5090
5091         if (mdt->mdt_seq_site.ss_node_id == 0) {
5092                 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5093                                                          &mdt->mdt_md_root_fid);
5094                 if (rc)
5095                         RETURN(rc);
5096         }
5097         LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5098         target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5099         set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5100         LASSERT(obd->obd_no_conn);
5101         spin_lock(&obd->obd_dev_lock);
5102         obd->obd_no_conn = 0;
5103         spin_unlock(&obd->obd_dev_lock);
5104
5105         if (obd->obd_recovering == 0)
5106                 mdt_postrecov(env, mdt);
5107
5108         RETURN(rc);
5109 }
5110
5111 const struct lu_device_operations mdt_lu_ops = {
5112         .ldo_object_alloc   = mdt_object_alloc,
5113         .ldo_process_config = mdt_process_config,
5114         .ldo_prepare        = mdt_prepare,
5115 };
5116
5117 static const struct lu_object_operations mdt_obj_ops = {
5118         .loo_object_init    = mdt_object_init,
5119         .loo_object_free    = mdt_object_free,
5120         .loo_object_print   = mdt_object_print
5121 };
5122
5123 static int mdt_obd_set_info_async(const struct lu_env *env,
5124                                   struct obd_export *exp,
5125                                   __u32 keylen, void *key,
5126                                   __u32 vallen, void *val,
5127                                   struct ptlrpc_request_set *set)
5128 {
5129         struct obd_device     *obd = exp->exp_obd;
5130         int                    rc;
5131         ENTRY;
5132
5133         LASSERT(obd);
5134
5135         if (KEY_IS(KEY_SPTLRPC_CONF)) {
5136                 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5137                 RETURN(rc);
5138         }
5139
5140         RETURN(0);
5141 }
5142
5143 /**
5144  * Match client and server connection feature flags.
5145  *
5146  * Compute the compatibility flags for a connection request based on
5147  * features mutually supported by client and server.
5148  *
5149  * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5150  * must not be updated here, otherwise a partially initialized value may
5151  * be exposed. After the connection request is successfully processed,
5152  * the top-level MDT connect request handler atomically updates the export
5153  * connect flags from the obd_connect_data::ocd_connect_flags field of the
5154  * reply. \see mdt_connect().
5155  *
5156  * \param exp   the obd_export associated with this client/target pair
5157  * \param mdt   the target device for the connection
5158  * \param data  stores data for this connect request
5159  *
5160  * \retval 0       success
5161  * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5162  * \retval -EBADE  client and server feature requirements are incompatible
5163  */
5164 static int mdt_connect_internal(struct obd_export *exp,
5165                                 struct mdt_device *mdt,
5166                                 struct obd_connect_data *data)
5167 {
5168         LASSERT(data != NULL);
5169
5170         data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5171         data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5172
5173         /* If no known bits (which should not happen, probably,
5174            as everybody should support LOOKUP and UPDATE bits at least)
5175            revert to compat mode with plain locks. */
5176         if (!data->ocd_ibits_known &&
5177             data->ocd_connect_flags & OBD_CONNECT_IBITS)
5178                 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5179
5180         if (!mdt->mdt_opts.mo_acl)
5181                 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5182
5183         if (!mdt->mdt_opts.mo_user_xattr)
5184                 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5185
5186         if (!mdt->mdt_som_conf)
5187                 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5188
5189         if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5190                 data->ocd_brw_size = min(data->ocd_brw_size,
5191                                          (__u32)MD_MAX_BRW_SIZE);
5192                 if (data->ocd_brw_size == 0) {
5193                         CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5194                                " ocd_version: %x ocd_grant: %d "
5195                                "ocd_index: %u ocd_brw_size is "
5196                                "unexpectedly zero, network data "
5197                                "corruption? Refusing connection of this"
5198                                " client\n",
5199                                mdt_obd_name(mdt),
5200                                exp->exp_client_uuid.uuid,
5201                                exp, data->ocd_connect_flags, data->ocd_version,
5202                                data->ocd_grant, data->ocd_index);
5203                         return -EPROTO;
5204                 }
5205         }
5206
5207         /* NB: Disregard the rule against updating
5208          * exp_connect_data.ocd_connect_flags in this case, since
5209          * tgt_client_new() needs to know if this is a lightweight
5210          * connection, and it is safe to expose this flag before
5211          * connection processing completes. */
5212         if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5213                 spin_lock(&exp->exp_lock);
5214                 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5215                 spin_unlock(&exp->exp_lock);
5216         }
5217
5218         data->ocd_version = LUSTRE_VERSION_CODE;
5219         exp->exp_connect_data = *data;
5220         exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5221
5222         if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5223                 CWARN("%s: MDS requires FID support, but client not\n",
5224                       mdt_obd_name(mdt));
5225                 return -EBADE;
5226         }
5227
5228         if (mdt->mdt_som_conf &&
5229             !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5230                                          OBD_CONNECT_MDS_MDS |
5231                                          OBD_CONNECT_SOM))) {
5232                 CWARN("%s: MDS has SOM enabled, but client does not support "
5233                       "it\n", mdt_obd_name(mdt));
5234                 return -EBADE;
5235         }
5236
5237         if (OCD_HAS_FLAG(data, PINGLESS)) {
5238                 if (ptlrpc_pinger_suppress_pings()) {
5239                         spin_lock(&exp->exp_obd->obd_dev_lock);
5240                         list_del_init(&exp->exp_obd_chain_timed);
5241                         spin_unlock(&exp->exp_obd->obd_dev_lock);
5242                 } else {
5243                         data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5244                 }
5245         }
5246
5247         return 0;
5248 }
5249
5250 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5251                                      struct obd_export *exp,
5252                                      struct ptlrpc_request *req)
5253 {
5254         struct sptlrpc_flavor   flvr;
5255         int                     rc = 0;
5256
5257         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5258                 read_lock(&mdt->mdt_sptlrpc_lock);
5259                 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5260                                              req->rq_sp_from,
5261                                              req->rq_peer.nid,
5262                                              &flvr);
5263                 read_unlock(&mdt->mdt_sptlrpc_lock);
5264
5265                 spin_lock(&exp->exp_lock);
5266
5267                 exp->exp_sp_peer = req->rq_sp_from;
5268                 exp->exp_flvr = flvr;
5269
5270                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5271                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5272                         CERROR("unauthorized rpc flavor %x from %s, "
5273                                "expect %x\n", req->rq_flvr.sf_rpc,
5274                                libcfs_nid2str(req->rq_peer.nid),
5275                                exp->exp_flvr.sf_rpc);
5276                         rc = -EACCES;
5277                 }
5278
5279                 spin_unlock(&exp->exp_lock);
5280         } else {
5281                 if (exp->exp_sp_peer != req->rq_sp_from) {
5282                         CERROR("RPC source %s doesn't match %s\n",
5283                                sptlrpc_part2name(req->rq_sp_from),
5284                                sptlrpc_part2name(exp->exp_sp_peer));
5285                         rc = -EACCES;
5286                 } else {
5287                         rc = sptlrpc_target_export_check(exp, req);
5288                 }
5289         }
5290
5291         return rc;
5292 }
5293
5294 /* mds_connect copy */
5295 static int mdt_obd_connect(const struct lu_env *env,
5296                            struct obd_export **exp, struct obd_device *obd,
5297                            struct obd_uuid *cluuid,
5298                            struct obd_connect_data *data,
5299                            void *localdata)
5300 {
5301         struct mdt_thread_info *info;
5302         struct obd_export      *lexp;
5303         struct lustre_handle    conn = { 0 };
5304         struct mdt_device      *mdt;
5305         struct ptlrpc_request  *req;
5306         int                     rc;
5307         ENTRY;
5308
5309         LASSERT(env != NULL);
5310         if (!exp || !obd || !cluuid)
5311                 RETURN(-EINVAL);
5312
5313         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5314         req = info->mti_pill->rc_req;
5315         mdt = mdt_dev(obd->obd_lu_dev);
5316
5317         /*
5318          * first, check whether the stack is ready to handle requests
5319          * XXX: probably not very appropriate method is used now
5320          *      at some point we should find a better one
5321          */
5322         if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5323             !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5324                 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5325                 if (rc)
5326                         RETURN(-EAGAIN);
5327                 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5328         }
5329
5330         rc = class_connect(&conn, obd, cluuid);
5331         if (rc)
5332                 RETURN(rc);
5333
5334         lexp = class_conn2export(&conn);
5335         LASSERT(lexp != NULL);
5336
5337         rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5338         if (rc)
5339                 GOTO(out, rc);
5340
5341         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5342                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5343
5344         rc = mdt_connect_internal(lexp, mdt, data);
5345         if (rc == 0) {
5346                 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5347
5348                 LASSERT(lcd);
5349                 info->mti_exp = lexp;
5350                 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5351                 rc = tgt_client_new(env, lexp);
5352                 if (rc == 0)
5353                         mdt_export_stats_init(obd, lexp, localdata);
5354         }
5355
5356 out:
5357         if (rc != 0) {
5358                 class_disconnect(lexp);
5359                 *exp = NULL;
5360         } else {
5361                 *exp = lexp;
5362         }
5363
5364         RETURN(rc);
5365 }
5366
5367 static int mdt_obd_reconnect(const struct lu_env *env,
5368                              struct obd_export *exp, struct obd_device *obd,
5369                              struct obd_uuid *cluuid,
5370                              struct obd_connect_data *data,
5371                              void *localdata)
5372 {
5373         struct mdt_thread_info *info;
5374         struct mdt_device      *mdt;
5375         struct ptlrpc_request  *req;
5376         int                     rc;
5377         ENTRY;
5378
5379         if (exp == NULL || obd == NULL || cluuid == NULL)
5380                 RETURN(-EINVAL);
5381
5382         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5383         req = info->mti_pill->rc_req;
5384         mdt = mdt_dev(obd->obd_lu_dev);
5385
5386         rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5387         if (rc)
5388                 RETURN(rc);
5389
5390         rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5391         if (rc == 0)
5392                 mdt_export_stats_init(obd, exp, localdata);
5393
5394         RETURN(rc);
5395 }
5396
5397 static int mdt_export_cleanup(struct obd_export *exp)
5398 {
5399         struct mdt_export_data *med = &exp->exp_mdt_data;
5400         struct obd_device      *obd = exp->exp_obd;
5401         struct mdt_device      *mdt;
5402         struct mdt_thread_info *info;
5403         struct lu_env           env;
5404         CFS_LIST_HEAD(closing_list);
5405         struct mdt_file_data *mfd, *n;
5406         int rc = 0;
5407         ENTRY;
5408
5409         spin_lock(&med->med_open_lock);
5410         while (!cfs_list_empty(&med->med_open_head)) {
5411                 cfs_list_t *tmp = med->med_open_head.next;
5412                 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5413
5414                 /* Remove mfd handle so it can't be found again.
5415                  * We are consuming the mfd_list reference here. */
5416                 class_handle_unhash(&mfd->mfd_handle);
5417                 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5418         }
5419         spin_unlock(&med->med_open_lock);
5420         mdt = mdt_dev(obd->obd_lu_dev);
5421         LASSERT(mdt != NULL);
5422
5423         rc = lu_env_init(&env, LCT_MD_THREAD);
5424         if (rc)
5425                 RETURN(rc);
5426
5427         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5428         LASSERT(info != NULL);
5429         memset(info, 0, sizeof *info);
5430         info->mti_env = &env;
5431         info->mti_mdt = mdt;
5432         info->mti_exp = exp;
5433
5434         if (!cfs_list_empty(&closing_list)) {
5435                 struct md_attr *ma = &info->mti_attr;
5436
5437                 /* Close any open files (which may also cause orphan unlinking). */
5438                 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5439                         cfs_list_del_init(&mfd->mfd_list);
5440                         ma->ma_need = ma->ma_valid = 0;
5441                         /* Don't unlink orphan on failover umount, LU-184 */
5442                         if (exp->exp_flags & OBD_OPT_FAILOVER) {
5443                                 ma->ma_valid = MA_FLAGS;
5444                                 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5445                         }
5446                         mdt_mfd_close(info, mfd);
5447                 }
5448         }
5449         info->mti_mdt = NULL;
5450         /* cleanup client slot early */
5451         /* Do not erase record for recoverable client. */
5452         if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5453                 tgt_client_del(&env, exp);
5454         lu_env_fini(&env);
5455
5456         RETURN(rc);
5457 }
5458
5459 static int mdt_obd_disconnect(struct obd_export *exp)
5460 {
5461         int rc;
5462         ENTRY;
5463
5464         LASSERT(exp);
5465         class_export_get(exp);
5466
5467         rc = server_disconnect_export(exp);
5468         if (rc != 0)
5469                 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5470
5471         rc = mdt_export_cleanup(exp);
5472         class_export_put(exp);
5473         RETURN(rc);
5474 }
5475
5476 /* FIXME: Can we avoid using these two interfaces? */
5477 static int mdt_init_export(struct obd_export *exp)
5478 {
5479         struct mdt_export_data *med = &exp->exp_mdt_data;
5480         int                     rc;
5481         ENTRY;
5482
5483         CFS_INIT_LIST_HEAD(&med->med_open_head);
5484         spin_lock_init(&med->med_open_lock);
5485         mutex_init(&med->med_idmap_mutex);
5486         med->med_idmap = NULL;
5487         spin_lock(&exp->exp_lock);
5488         exp->exp_connecting = 1;
5489         spin_unlock(&exp->exp_lock);
5490
5491         /* self-export doesn't need client data and ldlm initialization */
5492         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5493                                      &exp->exp_client_uuid)))
5494                 RETURN(0);
5495
5496         rc = tgt_client_alloc(exp);
5497         if (rc)
5498                 GOTO(err, rc);
5499
5500         rc = ldlm_init_export(exp);
5501         if (rc)
5502                 GOTO(err_free, rc);
5503
5504         RETURN(rc);
5505
5506 err_free:
5507         tgt_client_free(exp);
5508 err:
5509         CERROR("%s: Failed to initialize export: rc = %d\n",
5510                exp->exp_obd->obd_name, rc);
5511         return rc;
5512 }
5513
5514 static int mdt_destroy_export(struct obd_export *exp)
5515 {
5516         ENTRY;
5517
5518         if (exp_connect_rmtclient(exp))
5519                 mdt_cleanup_idmap(&exp->exp_mdt_data);
5520
5521         target_destroy_export(exp);
5522         /* destroy can be called from failed obd_setup, so
5523          * checking uuid is safer than obd_self_export */
5524         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5525                                      &exp->exp_client_uuid)))
5526                 RETURN(0);
5527
5528         ldlm_destroy_export(exp);
5529         tgt_client_free(exp);
5530
5531         LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5532         LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5533
5534         RETURN(0);
5535 }
5536
5537 /** The maximum depth that fid2path() will search.
5538  * This is limited only because we want to store the fids for
5539  * historical path lookup purposes.
5540  */
5541 #define MAX_PATH_DEPTH 100
5542
5543 /** mdt_path() lookup structure. */
5544 struct path_lookup_info {
5545         __u64                   pli_recno;      /**< history point */
5546         __u64                   pli_currec;     /**< current record */
5547         struct lu_fid           pli_fid;
5548         struct lu_fid           pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5549         struct mdt_object       *pli_mdt_obj;
5550         char                    *pli_path;      /**< full path */
5551         int                     pli_pathlen;
5552         int                     pli_linkno;     /**< which hardlink to follow */
5553         int                     pli_fidcount;   /**< number of \a pli_fids */
5554 };
5555
5556 static int mdt_links_read(struct mdt_thread_info *info,
5557                           struct mdt_object *mdt_obj, struct linkea_data *ldata)
5558 {
5559         int rc;
5560
5561         LASSERT(ldata->ld_buf->lb_buf != NULL);
5562
5563         if (!mdt_object_exists(mdt_obj))
5564                 return -ENODATA;
5565
5566         rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5567                           ldata->ld_buf, XATTR_NAME_LINK);
5568         if (rc == -ERANGE) {
5569                 /* Buf was too small, figure out what we need. */
5570                 lu_buf_free(ldata->ld_buf);
5571                 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5572                                   ldata->ld_buf, XATTR_NAME_LINK);
5573                 if (rc < 0)
5574                         return rc;
5575                 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5576                 if (ldata->ld_buf->lb_buf == NULL)
5577                         return -ENOMEM;
5578                 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5579                                   ldata->ld_buf, XATTR_NAME_LINK);
5580         }
5581         if (rc < 0)
5582                 return rc;
5583
5584         linkea_init(ldata);
5585
5586         return 0;
5587 }
5588
5589 static int mdt_path_current(struct mdt_thread_info *info,
5590                             struct path_lookup_info *pli)
5591 {
5592         struct mdt_device       *mdt = info->mti_mdt;
5593         struct mdt_object       *mdt_obj;
5594         struct link_ea_header   *leh;
5595         struct link_ea_entry    *lee;
5596         struct lu_name          *tmpname = &info->mti_name;
5597         struct lu_fid           *tmpfid = &info->mti_tmp_fid1;
5598         struct lu_buf           *buf = &info->mti_big_buf;
5599         char                    *ptr;
5600         int                     reclen;
5601         struct linkea_data      ldata = { 0 };
5602         int                     rc = 0;
5603         ENTRY;
5604
5605         /* temp buffer for path element, the buffer will be finally freed
5606          * in mdt_thread_info_fini */
5607         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5608         if (buf->lb_buf == NULL)
5609                 RETURN(-ENOMEM);
5610
5611         ldata.ld_buf = buf;
5612         ptr = pli->pli_path + pli->pli_pathlen - 1;
5613         *ptr = 0;
5614         --ptr;
5615         pli->pli_fidcount = 0;
5616         pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5617
5618         /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5619          * so checking root_fid can only happen on MDT0. */
5620         while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5621                           &pli->pli_fids[pli->pli_fidcount])) {
5622                 mdt_obj = mdt_object_find(info->mti_env, mdt,
5623                                           &pli->pli_fids[pli->pli_fidcount]);
5624                 if (IS_ERR(mdt_obj))
5625                         GOTO(out, rc = PTR_ERR(mdt_obj));
5626                 if (mdt_object_remote(mdt_obj)) {
5627                         mdt_object_put(info->mti_env, mdt_obj);
5628                         GOTO(remote_out, rc = -EREMOTE);
5629                 }
5630                 if (!mdt_object_exists(mdt_obj)) {
5631                         mdt_object_put(info->mti_env, mdt_obj);
5632                         GOTO(out, rc = -ENOENT);
5633                 }
5634
5635                 rc = mdt_links_read(info, mdt_obj, &ldata);
5636                 mdt_object_put(info->mti_env, mdt_obj);
5637                 if (rc != 0)
5638                         GOTO(out, rc = PTR_ERR(buf));
5639
5640                 leh = buf->lb_buf;
5641                 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5642                 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5643                 /* If set, use link #linkno for path lookup, otherwise use
5644                    link #0.  Only do this for the final path element. */
5645                 if ((pli->pli_fidcount == 0) &&
5646                     (pli->pli_linkno < leh->leh_reccount)) {
5647                         int count;
5648                         for (count = 0; count < pli->pli_linkno; count++) {
5649                                 lee = (struct link_ea_entry *)
5650                                      ((char *)lee + reclen);
5651                                 linkea_entry_unpack(lee, &reclen, tmpname,
5652                                                     tmpfid);
5653                         }
5654                         if (pli->pli_linkno < leh->leh_reccount - 1)
5655                                 /* indicate to user there are more links */
5656                                 pli->pli_linkno++;
5657                 }
5658
5659                 /* Pack the name in the end of the buffer */
5660                 ptr -= tmpname->ln_namelen;
5661                 if (ptr - 1 <= pli->pli_path)
5662                         GOTO(out, rc = -EOVERFLOW);
5663                 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5664                 *(--ptr) = '/';
5665
5666                 /* Store the parent fid for historic lookup */
5667                 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5668                         GOTO(out, rc = -EOVERFLOW);
5669                 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5670         }
5671
5672 remote_out:
5673         ptr++; /* skip leading / */
5674         memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5675
5676         EXIT;
5677 out:
5678         return rc;
5679 }
5680
5681 /* Returns the full path to this fid, as of changelog record recno. */
5682 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5683                     char *path, int pathlen, __u64 *recno, int *linkno,
5684                     struct lu_fid *fid)
5685 {
5686         struct mdt_device       *mdt = info->mti_mdt;
5687         struct path_lookup_info *pli;
5688         int                     tries = 3;
5689         int                     rc = -EAGAIN;
5690         ENTRY;
5691
5692         if (pathlen < 3)
5693                 RETURN(-EOVERFLOW);
5694
5695         if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5696                 path[0] = '\0';
5697                 RETURN(0);
5698         }
5699
5700         OBD_ALLOC_PTR(pli);
5701         if (pli == NULL)
5702                 RETURN(-ENOMEM);
5703
5704         pli->pli_mdt_obj = obj;
5705         pli->pli_recno = *recno;
5706         pli->pli_path = path;
5707         pli->pli_pathlen = pathlen;
5708         pli->pli_linkno = *linkno;
5709
5710         /* Retry multiple times in case file is being moved */
5711         while (tries-- && rc == -EAGAIN)
5712                 rc = mdt_path_current(info, pli);
5713
5714         /* return the last resolved fids to the client, so the client will
5715          * build the left path on another MDT for remote object */
5716         *fid = pli->pli_fids[pli->pli_fidcount];
5717
5718         *recno = pli->pli_currec;
5719         /* Return next link index to caller */
5720         *linkno = pli->pli_linkno;
5721
5722         OBD_FREE_PTR(pli);
5723
5724         RETURN(rc);
5725 }
5726
5727 static int mdt_fid2path(struct mdt_thread_info *info,
5728                         struct getinfo_fid2path *fp)
5729 {
5730         struct mdt_device *mdt = info->mti_mdt;
5731         struct mdt_object *obj;
5732         int    rc;
5733         ENTRY;
5734
5735         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5736                 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5737
5738         if (!fid_is_sane(&fp->gf_fid))
5739                 RETURN(-EINVAL);
5740
5741         if (!fid_is_namespace_visible(&fp->gf_fid)) {
5742                 CWARN("%s: "DFID" is invalid, sequence should be "
5743                       ">= "LPX64"\n", mdt_obd_name(mdt),
5744                       PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5745                 RETURN(-EINVAL);
5746         }
5747
5748         obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5749         if (obj == NULL || IS_ERR(obj)) {
5750                 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5751                        PTR_ERR(obj));
5752                 RETURN(-EINVAL);
5753         }
5754
5755         if (mdt_object_remote(obj))
5756                 rc = -EREMOTE;
5757         else if (!mdt_object_exists(obj))
5758                 rc = -ENOENT;
5759         else
5760                 rc = 0;
5761
5762         if (rc < 0) {
5763                 mdt_object_put(info->mti_env, obj);
5764                 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5765                        PFID(&fp->gf_fid), rc);
5766                 RETURN(rc);
5767         }
5768
5769         rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5770                       &fp->gf_linkno, &fp->gf_fid);
5771
5772         CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5773                PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5774
5775         mdt_object_put(info->mti_env, obj);
5776
5777         RETURN(rc);
5778 }
5779
5780 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5781                             void *val, int vallen)
5782 {
5783         struct getinfo_fid2path *fpout, *fpin;
5784         int rc = 0;
5785
5786         fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5787         fpout = val;
5788
5789         if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5790                 lustre_swab_fid2path(fpin);
5791
5792         memcpy(fpout, fpin, sizeof(*fpin));
5793         if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5794                 RETURN(-EINVAL);
5795
5796         rc = mdt_fid2path(info, fpout);
5797         RETURN(rc);
5798 }
5799
5800 int mdt_get_info(struct mdt_thread_info *info)
5801 {
5802         struct ptlrpc_request *req = mdt_info_req(info);
5803         char *key;
5804         int keylen;
5805         __u32 *vallen;
5806         void *valout;
5807         int rc;
5808         ENTRY;
5809
5810         key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5811         if (key == NULL) {
5812                 CDEBUG(D_IOCTL, "No GETINFO key");
5813                 RETURN(-EFAULT);
5814         }
5815         keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5816                                       RCL_CLIENT);
5817
5818         vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5819         if (vallen == NULL) {
5820                 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5821                 RETURN(-EFAULT);
5822         }
5823
5824         req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5825                              *vallen);
5826         rc = req_capsule_server_pack(info->mti_pill);
5827         valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5828         if (valout == NULL) {
5829                 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5830                 RETURN(-EFAULT);
5831         }
5832
5833         if (KEY_IS(KEY_FID2PATH))
5834                 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5835         else
5836                 rc = -EINVAL;
5837
5838         lustre_msg_set_status(req->rq_repmsg, rc);
5839
5840         RETURN(rc);
5841 }
5842
5843 /* Pass the ioc down */
5844 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5845                          unsigned int cmd, int len, void *data)
5846 {
5847         struct lu_context ioctl_session;
5848         struct md_device *next = mdt->mdt_child;
5849         int rc;
5850         ENTRY;
5851
5852         rc = lu_context_init(&ioctl_session, LCT_SESSION);
5853         if (rc)
5854                 RETURN(rc);
5855         ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5856         lu_context_enter(&ioctl_session);
5857         env->le_ses = &ioctl_session;
5858
5859         LASSERT(next->md_ops->mdo_iocontrol);
5860         rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5861
5862         lu_context_exit(&ioctl_session);
5863         lu_context_fini(&ioctl_session);
5864         RETURN(rc);
5865 }
5866
5867 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5868 {
5869         struct obd_ioctl_data *data = karg;
5870         struct lu_fid *fid;
5871         __u64 version;
5872         struct mdt_object *obj;
5873         struct mdt_lock_handle  *lh;
5874         int rc;
5875         ENTRY;
5876
5877         if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5878             data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5879                 RETURN(-EINVAL);
5880
5881         fid = (struct lu_fid *)data->ioc_inlbuf1;
5882
5883         if (!fid_is_sane(fid))
5884                 RETURN(-EINVAL);
5885
5886         CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5887
5888         lh = &mti->mti_lh[MDT_LH_PARENT];
5889         mdt_lock_reg_init(lh, LCK_CR);
5890
5891         obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5892         if (IS_ERR(obj))
5893                 RETURN(PTR_ERR(obj));
5894
5895         if (mdt_object_remote(obj)) {
5896                 rc = -EREMOTE;
5897                 /**
5898                  * before calling version get the correct MDS should be
5899                  * fid, this is error to find remote object here
5900                  */
5901                 CERROR("nonlocal object "DFID"\n", PFID(fid));
5902         } else if (!mdt_object_exists(obj)) {
5903                 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5904                 rc = -ENOENT;
5905         } else {
5906                 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5907                *(__u64 *)data->ioc_inlbuf2 = version;
5908                 rc = 0;
5909         }
5910         mdt_object_unlock_put(mti, obj, lh, 1);
5911         RETURN(rc);
5912 }
5913
5914 /* ioctls on obd dev */
5915 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5916                          void *karg, void *uarg)
5917 {
5918         struct lu_env      env;
5919         struct obd_device *obd = exp->exp_obd;
5920         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5921         struct dt_device  *dt = mdt->mdt_bottom;
5922         int rc;
5923
5924         ENTRY;
5925         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5926         rc = lu_env_init(&env, LCT_MD_THREAD);
5927         if (rc)
5928                 RETURN(rc);
5929
5930         switch (cmd) {
5931         case OBD_IOC_SYNC:
5932                 rc = mdt_device_sync(&env, mdt);
5933                 break;
5934         case OBD_IOC_SET_READONLY:
5935                 rc = dt->dd_ops->dt_ro(&env, dt);
5936                 break;
5937         case OBD_IOC_ABORT_RECOVERY:
5938                 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5939                 target_stop_recovery_thread(obd);
5940                 rc = 0;
5941                 break;
5942         case OBD_IOC_CHANGELOG_REG:
5943         case OBD_IOC_CHANGELOG_DEREG:
5944         case OBD_IOC_CHANGELOG_CLEAR:
5945                 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5946                 break;
5947         case OBD_IOC_START_LFSCK:
5948         case OBD_IOC_STOP_LFSCK: {
5949                 struct md_device *next = mdt->mdt_child;
5950                 struct obd_ioctl_data *data = karg;
5951
5952                 if (unlikely(data == NULL)) {
5953                         rc = -EINVAL;
5954                         break;
5955                 }
5956
5957                 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
5958                                                  data->ioc_inllen1,
5959                                                  data->ioc_inlbuf1);
5960                 break;
5961         }
5962         case OBD_IOC_GET_OBJ_VERSION: {
5963                 struct mdt_thread_info *mti;
5964                 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5965                 memset(mti, 0, sizeof *mti);
5966                 mti->mti_env = &env;
5967                 mti->mti_mdt = mdt;
5968                 mti->mti_exp = exp;
5969
5970                 rc = mdt_ioc_version_get(mti, karg);
5971                 break;
5972         }
5973         default:
5974                 rc = -EOPNOTSUPP;
5975                 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5976                         mdt_obd_name(mdt), cmd, rc);
5977         }
5978
5979         lu_env_fini(&env);
5980         RETURN(rc);
5981 }
5982
5983 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5984 {
5985         struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5986         int rc;
5987         ENTRY;
5988
5989         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5990         RETURN(rc);
5991 }
5992
5993 int mdt_obd_postrecov(struct obd_device *obd)
5994 {
5995         struct lu_env env;
5996         int rc;
5997
5998         rc = lu_env_init(&env, LCT_MD_THREAD);
5999         if (rc)
6000                 RETURN(rc);
6001         rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6002         lu_env_fini(&env);
6003         return rc;
6004 }
6005
6006 static struct obd_ops mdt_obd_device_ops = {
6007         .o_owner          = THIS_MODULE,
6008         .o_set_info_async = mdt_obd_set_info_async,
6009         .o_connect        = mdt_obd_connect,
6010         .o_reconnect      = mdt_obd_reconnect,
6011         .o_disconnect     = mdt_obd_disconnect,
6012         .o_init_export    = mdt_init_export,
6013         .o_destroy_export = mdt_destroy_export,
6014         .o_iocontrol      = mdt_iocontrol,
6015         .o_postrecov      = mdt_obd_postrecov,
6016 };
6017
6018 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6019                                          struct lu_device *d)
6020 {
6021         struct mdt_device *m = mdt_dev(d);
6022         ENTRY;
6023
6024         mdt_fini(env, m);
6025         RETURN(NULL);
6026 }
6027
6028 static struct lu_device *mdt_device_free(const struct lu_env *env,
6029                                          struct lu_device *d)
6030 {
6031         struct mdt_device *m = mdt_dev(d);
6032         ENTRY;
6033
6034         md_device_fini(&m->mdt_md_dev);
6035         OBD_FREE_PTR(m);
6036         RETURN(NULL);
6037 }
6038
6039 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6040                                           struct lu_device_type *t,
6041                                           struct lustre_cfg *cfg)
6042 {
6043         struct lu_device  *l;
6044         struct mdt_device *m;
6045
6046         OBD_ALLOC_PTR(m);
6047         if (m != NULL) {
6048                 int rc;
6049
6050                 l = &m->mdt_md_dev.md_lu_dev;
6051                 rc = mdt_init0(env, m, t, cfg);
6052                 if (rc != 0) {
6053                         mdt_device_free(env, l);
6054                         l = ERR_PTR(rc);
6055                         return l;
6056                 }
6057         } else
6058                 l = ERR_PTR(-ENOMEM);
6059         return l;
6060 }
6061
6062 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6063 LU_KEY_INIT(mdt, struct mdt_thread_info);
6064
6065 static void mdt_key_fini(const struct lu_context *ctx,
6066                          struct lu_context_key *key, void* data)
6067 {
6068         struct mdt_thread_info *info = data;
6069
6070         if (info->mti_big_lmm) {
6071                 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6072                 info->mti_big_lmm = NULL;
6073                 info->mti_big_lmmsize = 0;
6074         }
6075         OBD_FREE_PTR(info);
6076 }
6077
6078 /* context key: mdt_thread_key */
6079 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6080
6081 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6082 {
6083         return lu_ucred(info->mti_env);
6084 }
6085
6086 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6087 {
6088         return lu_ucred_check(info->mti_env);
6089 }
6090
6091 /**
6092  * Enable/disable COS (Commit On Sharing).
6093  *
6094  * Set/Clear the COS flag in mdt options.
6095  *
6096  * \param mdt mdt device
6097  * \param val 0 disables COS, other values enable COS
6098  */
6099 void mdt_enable_cos(struct mdt_device *mdt, int val)
6100 {
6101         struct lu_env env;
6102         int rc;
6103
6104         mdt->mdt_opts.mo_cos = !!val;
6105         rc = lu_env_init(&env, LCT_LOCAL);
6106         if (unlikely(rc != 0)) {
6107                 CWARN("lu_env initialization failed with rc = %d,"
6108                       "cannot sync\n", rc);
6109                 return;
6110         }
6111         mdt_device_sync(&env, mdt);
6112         lu_env_fini(&env);
6113 }
6114
6115 /**
6116  * Check COS (Commit On Sharing) status.
6117  *
6118  * Return COS flag status.
6119  *
6120  * \param mdt mdt device
6121  */
6122 int mdt_cos_is_enabled(struct mdt_device *mdt)
6123 {
6124         return mdt->mdt_opts.mo_cos != 0;
6125 }
6126
6127 static struct lu_device_type_operations mdt_device_type_ops = {
6128         .ldto_device_alloc = mdt_device_alloc,
6129         .ldto_device_free  = mdt_device_free,
6130         .ldto_device_fini  = mdt_device_fini
6131 };
6132
6133 static struct lu_device_type mdt_device_type = {
6134         .ldt_tags     = LU_DEVICE_MD,
6135         .ldt_name     = LUSTRE_MDT_NAME,
6136         .ldt_ops      = &mdt_device_type_ops,
6137         .ldt_ctx_tags = LCT_MD_THREAD
6138 };
6139
6140 static int __init mdt_mod_init(void)
6141 {
6142         struct lprocfs_static_vars lvars;
6143         int rc;
6144
6145         CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6146                  FID_NOBRACE_LEN + 1);
6147         CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6148                  FID_LEN + 1);
6149         rc = lu_kmem_init(mdt_caches);
6150         if (rc)
6151                 return rc;
6152
6153         rc = mds_mod_init();
6154         if (rc)
6155                 GOTO(lu_fini, rc);
6156
6157         lprocfs_mdt_init_vars(&lvars);
6158         rc = class_register_type(&mdt_obd_device_ops, NULL,
6159                                  lvars.module_vars, LUSTRE_MDT_NAME,
6160                                  &mdt_device_type);
6161         if (rc)
6162                 GOTO(mds_fini, rc);
6163 lu_fini:
6164         if (rc)
6165                 lu_kmem_fini(mdt_caches);
6166 mds_fini:
6167         if (rc)
6168                 mds_mod_exit();
6169         return rc;
6170 }
6171
6172 static void __exit mdt_mod_exit(void)
6173 {
6174         class_unregister_type(LUSTRE_MDT_NAME);
6175         mds_mod_exit();
6176         lu_kmem_fini(mdt_caches);
6177 }
6178
6179 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6180 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6181 MODULE_LICENSE("GPL");
6182
6183 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);