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