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