Whamcloud - gitweb
fa160bc6c6ae1dce6c3cd099a107def1364b053f
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2010, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/mdt/mdt_handler.c
32  *
33  * Lustre Metadata Target (mdt) request handler
34  *
35  * Author: Peter Braam <braam@clusterfs.com>
36  * Author: Andreas Dilger <adilger@clusterfs.com>
37  * Author: Phil Schwan <phil@clusterfs.com>
38  * Author: Mike Shaver <shaver@clusterfs.com>
39  * Author: Nikita Danilov <nikita@clusterfs.com>
40  * Author: Huang Hua <huanghua@clusterfs.com>
41  * Author: Yury Umanets <umka@clusterfs.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_MDS
45
46 #include <linux/module.h>
47 #include <linux/pagemap.h>
48
49 #include <dt_object.h>
50 #include <lustre_acl.h>
51 #include <lustre_export.h>
52 #include <uapi/linux/lustre/lustre_ioctl.h>
53 #include <lustre_lfsck.h>
54 #include <lustre_log.h>
55 #include <lustre_nodemap.h>
56 #include <lustre_mds.h>
57 #include <uapi/linux/lustre/lustre_param.h>
58 #include <lustre_quota.h>
59 #include <lustre_swab.h>
60 #include <lustre_lmv.h>
61 #include <obd.h>
62 #include <obd_support.h>
63 #include <lustre_barrier.h>
64 #include <obd_cksum.h>
65 #include <llog_swab.h>
66
67 #include "mdt_internal.h"
68
69 static unsigned int max_mod_rpcs_per_client = 8;
70 module_param(max_mod_rpcs_per_client, uint, 0644);
71 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
72
73 mdl_mode_t mdt_mdl_lock_modes[] = {
74         [LCK_MINMODE] = MDL_MINMODE,
75         [LCK_EX]      = MDL_EX,
76         [LCK_PW]      = MDL_PW,
77         [LCK_PR]      = MDL_PR,
78         [LCK_CW]      = MDL_CW,
79         [LCK_CR]      = MDL_CR,
80         [LCK_NL]      = MDL_NL,
81         [LCK_GROUP]   = MDL_GROUP
82 };
83
84 enum ldlm_mode mdt_dlm_lock_modes[] = {
85         [MDL_MINMODE]   = LCK_MINMODE,
86         [MDL_EX]        = LCK_EX,
87         [MDL_PW]        = LCK_PW,
88         [MDL_PR]        = LCK_PR,
89         [MDL_CW]        = LCK_CW,
90         [MDL_CR]        = LCK_CR,
91         [MDL_NL]        = LCK_NL,
92         [MDL_GROUP]     = LCK_GROUP
93 };
94
95 static struct mdt_device *mdt_dev(struct lu_device *d);
96
97 static const struct lu_object_operations mdt_obj_ops;
98
99 /* Slab for MDT object allocation */
100 static struct kmem_cache *mdt_object_kmem;
101
102 /* For HSM restore handles */
103 struct kmem_cache *mdt_hsm_cdt_kmem;
104
105 /* For HSM request handles */
106 struct kmem_cache *mdt_hsm_car_kmem;
107
108 static struct lu_kmem_descr mdt_caches[] = {
109         {
110                 .ckd_cache = &mdt_object_kmem,
111                 .ckd_name  = "mdt_obj",
112                 .ckd_size  = sizeof(struct mdt_object)
113         },
114         {
115                 .ckd_cache      = &mdt_hsm_cdt_kmem,
116                 .ckd_name       = "mdt_cdt_restore_handle",
117                 .ckd_size       = sizeof(struct cdt_restore_handle)
118         },
119         {
120                 .ckd_cache      = &mdt_hsm_car_kmem,
121                 .ckd_name       = "mdt_cdt_agent_req",
122                 .ckd_size       = sizeof(struct cdt_agent_req)
123         },
124         {
125                 .ckd_cache = NULL
126         }
127 };
128
129 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
130 {
131         if (!rep)
132                 return 0;
133         return rep->lock_policy_res1 & op_flag;
134 }
135
136 void mdt_clear_disposition(struct mdt_thread_info *info,
137                            struct ldlm_reply *rep, __u64 op_flag)
138 {
139         if (info) {
140                 info->mti_opdata &= ~op_flag;
141                 tgt_opdata_clear(info->mti_env, op_flag);
142         }
143         if (rep)
144                 rep->lock_policy_res1 &= ~op_flag;
145 }
146
147 void mdt_set_disposition(struct mdt_thread_info *info,
148                          struct ldlm_reply *rep, __u64 op_flag)
149 {
150         if (info) {
151                 info->mti_opdata |= op_flag;
152                 tgt_opdata_set(info->mti_env, op_flag);
153         }
154         if (rep)
155                 rep->lock_policy_res1 |= op_flag;
156 }
157
158 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
159 {
160         lh->mlh_pdo_hash = 0;
161         lh->mlh_reg_mode = lm;
162         lh->mlh_rreg_mode = lm;
163         lh->mlh_type = MDT_REG_LOCK;
164 }
165
166 void mdt_lh_reg_init(struct mdt_lock_handle *lh, struct ldlm_lock *lock)
167 {
168         mdt_lock_reg_init(lh, lock->l_req_mode);
169         if (lock->l_req_mode == LCK_GROUP)
170                 lh->mlh_gid = lock->l_policy_data.l_inodebits.li_gid;
171 }
172
173 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
174                        const struct lu_name *lname)
175 {
176         lh->mlh_reg_mode = lock_mode;
177         lh->mlh_pdo_mode = LCK_MINMODE;
178         lh->mlh_rreg_mode = lock_mode;
179         lh->mlh_type = MDT_PDO_LOCK;
180
181         if (lu_name_is_valid(lname)) {
182                 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
183                                                      lname->ln_namelen);
184                 /* XXX Workaround for LU-2856
185                  *
186                  * Zero is a valid return value of full_name_hash, but
187                  * several users of mlh_pdo_hash assume a non-zero
188                  * hash value. We therefore map zero onto an
189                  * arbitrary, but consistent value (1) to avoid
190                  * problems further down the road. */
191                 if (unlikely(lh->mlh_pdo_hash == 0))
192                         lh->mlh_pdo_hash = 1;
193         } else {
194                 lh->mlh_pdo_hash = 0;
195         }
196 }
197
198 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
199                               struct mdt_lock_handle *lh)
200 {
201         mdl_mode_t mode;
202         ENTRY;
203
204         /*
205          * Any dir access needs couple of locks:
206          *
207          * 1) on part of dir we gonna take lookup/modify;
208          *
209          * 2) on whole dir to protect it from concurrent splitting and/or to
210          * flush client's cache for readdir().
211          *
212          * so, for a given mode and object this routine decides what lock mode
213          * to use for lock #2:
214          *
215          * 1) if caller's gonna lookup in dir then we need to protect dir from
216          * being splitted only - LCK_CR
217          *
218          * 2) if caller's gonna modify dir then we need to protect dir from
219          * being splitted and to flush cache - LCK_CW
220          *
221          * 3) if caller's gonna modify dir and that dir seems ready for
222          * splitting then we need to protect it from any type of access
223          * (lookup/modify/split) - LCK_EX --bzzz
224          */
225
226         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
227         LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
228
229         /*
230          * Ask underlaying level its opinion about preferable PDO lock mode
231          * having access type passed as regular lock mode:
232          *
233          * - MDL_MINMODE means that lower layer does not want to specify lock
234          * mode;
235          *
236          * - MDL_NL means that no PDO lock should be taken. This is used in some
237          * cases. Say, for non-splittable directories no need to use PDO locks
238          * at all.
239          */
240         mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
241                              mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
242
243         if (mode != MDL_MINMODE) {
244                 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
245         } else {
246                 /*
247                  * Lower layer does not want to specify locking mode. We do it
248                  * our selves. No special protection is needed, just flush
249                  * client's cache on modification and allow concurrent
250                  * mondification.
251                  */
252                 switch (lh->mlh_reg_mode) {
253                 case LCK_EX:
254                         lh->mlh_pdo_mode = LCK_EX;
255                         break;
256                 case LCK_PR:
257                         lh->mlh_pdo_mode = LCK_CR;
258                         break;
259                 case LCK_PW:
260                         lh->mlh_pdo_mode = LCK_CW;
261                         break;
262                 default:
263                         CERROR("Not expected lock type (0x%x)\n",
264                                (int)lh->mlh_reg_mode);
265                         LBUG();
266                 }
267         }
268
269         LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
270         EXIT;
271 }
272
273 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
274                               struct lu_fid *fid)
275 {
276         struct mdt_device *mdt = info->mti_mdt;
277         struct lu_name *lname = &info->mti_name;
278         const char *start = fileset;
279         char *filename = info->mti_filename;
280         struct mdt_object *parent;
281         u32 mode;
282         int rc = 0;
283
284         LASSERT(!info->mti_cross_ref);
285
286         /*
287          * We may want to allow this to mount a completely separate
288          * fileset from the MDT in the future, but keeping it to
289          * ROOT/ only for now avoid potential security issues.
290          */
291         *fid = mdt->mdt_md_root_fid;
292
293         while (rc == 0 && start != NULL && *start != '\0') {
294                 const char *s1 = start;
295                 const char *s2;
296
297                 while (*++s1 == '/')
298                         ;
299                 s2 = s1;
300                 while (*s2 != '/' && *s2 != '\0')
301                         s2++;
302
303                 if (s2 == s1)
304                         break;
305
306                 start = s2;
307
308                 lname->ln_namelen = s2 - s1;
309                 if (lname->ln_namelen > NAME_MAX) {
310                         rc = -EINVAL;
311                         break;
312                 }
313
314                 /* reject .. as a path component */
315                 if (lname->ln_namelen == 2 &&
316                     strncmp(s1, "..", 2) == 0) {
317                         rc = -EINVAL;
318                         break;
319                 }
320
321                 strncpy(filename, s1, lname->ln_namelen);
322                 filename[lname->ln_namelen] = '\0';
323                 lname->ln_name = filename;
324
325                 parent = mdt_object_find(info->mti_env, mdt, fid);
326                 if (IS_ERR(parent)) {
327                         rc = PTR_ERR(parent);
328                         break;
329                 }
330                 /* Only got the fid of this obj by name */
331                 fid_zero(fid);
332                 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
333                                 fid, &info->mti_spec);
334                 mdt_object_put(info->mti_env, parent);
335         }
336         if (!rc) {
337                 parent = mdt_object_find(info->mti_env, mdt, fid);
338                 if (IS_ERR(parent))
339                         rc = PTR_ERR(parent);
340                 else {
341                         mode = lu_object_attr(&parent->mot_obj);
342                         if (!S_ISDIR(mode)) {
343                                 rc = -ENOTDIR;
344                         } else if (mdt_is_remote_object(info, parent, parent)) {
345                                 if (!mdt->mdt_enable_remote_subdir_mount) {
346                                         rc = -EREMOTE;
347                                         LCONSOLE_WARN("%s: subdir mount '%s' refused because 'enable_remote_subdir_mount=0': rc = %d\n",
348                                                       mdt_obd_name(mdt),
349                                                       fileset, rc);
350                                 } else {
351                                         LCONSOLE_INFO("%s: subdir mount '%s' is remote and may be slow\n",
352                                                       mdt_obd_name(mdt),
353                                                       fileset);
354                                 }
355                         }
356                         mdt_object_put(info->mti_env, parent);
357                 }
358         }
359
360         return rc;
361 }
362
363 static int mdt_get_root(struct tgt_session_info *tsi)
364 {
365         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
366         struct mdt_device       *mdt = info->mti_mdt;
367         struct mdt_body         *repbody;
368         char                    *fileset = NULL, *buffer = NULL;
369         int                      rc;
370         struct obd_export       *exp = info->mti_exp;
371         char                    *nodemap_fileset;
372
373         ENTRY;
374
375         rc = mdt_check_ucred(info);
376         if (rc)
377                 GOTO(out, rc = err_serious(rc));
378
379         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
380                 GOTO(out, rc = err_serious(-ENOMEM));
381
382         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
383         if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
384                 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
385                 if (fileset == NULL)
386                         GOTO(out, rc = err_serious(-EFAULT));
387         }
388
389         nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
390         if (nodemap_fileset && nodemap_fileset[0]) {
391                 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
392                 if (fileset) {
393                         /* consider fileset from client as a sub-fileset
394                          * of the nodemap one */
395                         OBD_ALLOC(buffer, PATH_MAX + 1);
396                         if (buffer == NULL)
397                                 GOTO(out, rc = err_serious(-ENOMEM));
398                         if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
399                                      nodemap_fileset, fileset) >= PATH_MAX + 1)
400                                 GOTO(out, rc = err_serious(-EINVAL));
401                         fileset = buffer;
402                 } else {
403                         /* enforce fileset as specified in the nodemap */
404                         fileset = nodemap_fileset;
405                 }
406         }
407
408         if (fileset) {
409                 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
410                 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
411                 if (rc < 0)
412                         GOTO(out, rc = err_serious(rc));
413         } else {
414                 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
415         }
416         repbody->mbo_valid |= OBD_MD_FLID;
417
418         EXIT;
419 out:
420         mdt_thread_info_fini(info);
421         if (buffer)
422                 OBD_FREE(buffer, PATH_MAX+1);
423         return rc;
424 }
425
426 static int mdt_statfs(struct tgt_session_info *tsi)
427 {
428         struct ptlrpc_request *req = tgt_ses_req(tsi);
429         struct mdt_thread_info *info = tsi2mdt_info(tsi);
430         struct mdt_device *mdt = info->mti_mdt;
431         struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
432         struct md_device *next = mdt->mdt_child;
433         struct ptlrpc_service_part *svcpt;
434         struct obd_statfs *osfs;
435         struct mdt_body *reqbody = NULL;
436         struct mdt_statfs_cache *msf;
437         ktime_t kstart = ktime_get();
438         int rc;
439
440         ENTRY;
441
442         svcpt = req->rq_rqbd->rqbd_svcpt;
443
444         /* This will trigger a watchdog timeout */
445         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
446                          (MDT_SERVICE_WATCHDOG_FACTOR *
447                           at_get(&svcpt->scp_at_estimate)) + 1);
448
449         rc = mdt_check_ucred(info);
450         if (rc)
451                 GOTO(out, rc = err_serious(rc));
452
453         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
454                 GOTO(out, rc = err_serious(-ENOMEM));
455
456         osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
457         if (!osfs)
458                 GOTO(out, rc = -EPROTO);
459
460         if (mdt_is_sum_statfs_client(req->rq_export) &&
461                 lustre_packed_msg_size(req->rq_reqmsg) ==
462                 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
463                                      &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
464                 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
465                 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
466         }
467
468         if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
469                 msf = &mdt->mdt_sum_osfs;
470         else
471                 msf = &mdt->mdt_osfs;
472
473         if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
474                         /** statfs data is too old, get up-to-date one */
475                         if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
476                                 rc = next->md_ops->mdo_statfs(info->mti_env,
477                                                               next, osfs);
478                         else
479                                 rc = dt_statfs(info->mti_env, mdt->mdt_bottom,
480                                                osfs);
481                         if (rc)
482                                 GOTO(out, rc);
483                         spin_lock(&mdt->mdt_lock);
484                         msf->msf_osfs = *osfs;
485                         msf->msf_age = ktime_get_seconds();
486                         spin_unlock(&mdt->mdt_lock);
487         } else {
488                         /** use cached statfs data */
489                         spin_lock(&mdt->mdt_lock);
490                         *osfs = msf->msf_osfs;
491                         spin_unlock(&mdt->mdt_lock);
492         }
493
494         /* at least try to account for cached pages.  its still racy and
495          * might be under-reporting if clients haven't announced their
496          * caches with brw recently */
497         CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
498                " pending %llu free %llu avail %llu\n",
499                tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
500                tgd->tgd_tot_pending,
501                osfs->os_bfree << tgd->tgd_blockbits,
502                osfs->os_bavail << tgd->tgd_blockbits);
503
504         osfs->os_bavail -= min_t(u64, osfs->os_bavail,
505                                  ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
506                                    osfs->os_bsize - 1) >> tgd->tgd_blockbits));
507
508         tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
509         CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
510                "%llu objects: %llu free; state %x\n",
511                osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
512                osfs->os_files, osfs->os_ffree, osfs->os_state);
513
514         if (!exp_grant_param_supp(tsi->tsi_exp) &&
515             tgd->tgd_blockbits > COMPAT_BSIZE_SHIFT) {
516                 /* clients which don't support OBD_CONNECT_GRANT_PARAM
517                  * should not see a block size > page size, otherwise
518                  * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
519                  * block size which is the biggest block size known to work
520                  * with all client's page size. */
521                 osfs->os_blocks <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
522                 osfs->os_bfree  <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
523                 osfs->os_bavail <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
524                 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
525         }
526         if (rc == 0)
527                 mdt_counter_incr(req, LPROC_MDT_STATFS,
528                                  ktime_us_delta(ktime_get(), kstart));
529 out:
530         mdt_thread_info_fini(info);
531         RETURN(rc);
532 }
533
534 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
535 {
536         struct lov_comp_md_v1 *comp_v1;
537         struct lov_mds_md *v1;
538         __u32 off;
539         __u32 dom_stripesize = 0;
540         int i;
541         bool has_ost_stripes = false;
542
543         ENTRY;
544
545         if (is_dom_only)
546                 *is_dom_only = 0;
547
548         if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
549                 RETURN(0);
550
551         comp_v1 = (struct lov_comp_md_v1 *)lmm;
552         off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
553         v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
554
555         /* Fast check for DoM entry with no mirroring, should be the first */
556         if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
557             lov_pattern(le32_to_cpu(v1->lmm_pattern)) != LOV_PATTERN_MDT)
558                 RETURN(0);
559
560         /* check all entries otherwise */
561         for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
562                 struct lov_comp_md_entry_v1 *lcme;
563
564                 lcme = &comp_v1->lcm_entries[i];
565                 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
566                         continue;
567
568                 off = le32_to_cpu(lcme->lcme_offset);
569                 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
570
571                 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) ==
572                     LOV_PATTERN_MDT)
573                         dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
574                 else
575                         has_ost_stripes = true;
576
577                 if (dom_stripesize && has_ost_stripes)
578                         RETURN(dom_stripesize);
579         }
580         /* DoM-only case exits here */
581         if (is_dom_only && dom_stripesize)
582                 *is_dom_only = 1;
583         RETURN(dom_stripesize);
584 }
585
586 /**
587  * Pack size attributes into the reply.
588  */
589 int mdt_pack_size2body(struct mdt_thread_info *info,
590                         const struct lu_fid *fid, struct lustre_handle *lh)
591 {
592         struct mdt_body *b;
593         struct md_attr *ma = &info->mti_attr;
594         __u32 dom_stripe;
595         bool dom_lock = false;
596
597         ENTRY;
598
599         LASSERT(ma->ma_attr.la_valid & LA_MODE);
600
601         if (!S_ISREG(ma->ma_attr.la_mode) ||
602             !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
603                 RETURN(-ENODATA);
604
605         dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
606         /* no DoM stripe, no size in reply */
607         if (!dom_stripe)
608                 RETURN(-ENOENT);
609
610         if (lustre_handle_is_used(lh)) {
611                 struct ldlm_lock *lock;
612
613                 lock = ldlm_handle2lock(lh);
614                 if (lock != NULL) {
615                         dom_lock = ldlm_has_dom(lock);
616                         LDLM_LOCK_PUT(lock);
617                 }
618         }
619
620         /* no DoM lock, no size in reply */
621         if (!dom_lock)
622                 RETURN(0);
623
624         /* Either DoM lock exists or LMM has only DoM stripe then
625          * return size on body. */
626         b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
627
628         mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
629         RETURN(0);
630 }
631
632 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
633 /*
634  * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
635  *
636  * \param       info    thread info object
637  * \param       repbody reply to pack ACLs into
638  * \param       o       mdt object of file to examine
639  * \param       nodemap nodemap of client to reply to
640  * \retval      0       success
641  * \retval      -errno  error getting or parsing ACL from disk
642  */
643 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
644                       struct mdt_object *o, struct lu_nodemap *nodemap)
645 {
646         const struct lu_env     *env = info->mti_env;
647         struct md_object        *next = mdt_object_child(o);
648         struct lu_buf           *buf = &info->mti_buf;
649         struct mdt_device       *mdt = info->mti_mdt;
650         struct req_capsule *pill = info->mti_pill;
651         int rc;
652
653         ENTRY;
654
655         buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
656         buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
657         if (buf->lb_len == 0)
658                 RETURN(0);
659
660         LASSERT(!info->mti_big_acl_used);
661 again:
662         rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
663         if (rc < 0) {
664                 if (rc == -ENODATA) {
665                         repbody->mbo_aclsize = 0;
666                         repbody->mbo_valid |= OBD_MD_FLACL;
667                         rc = 0;
668                 } else if (rc == -EOPNOTSUPP) {
669                         rc = 0;
670                 } else if (rc == -ERANGE) {
671                         if (exp_connect_large_acl(info->mti_exp) &&
672                             !info->mti_big_acl_used) {
673                                 if (info->mti_big_acl == NULL) {
674                                         info->mti_big_aclsize =
675                                                         min_t(unsigned int,
676                                                               mdt->mdt_max_ea_size,
677                                                               XATTR_SIZE_MAX);
678                                         OBD_ALLOC_LARGE(info->mti_big_acl,
679                                                         info->mti_big_aclsize);
680                                         if (info->mti_big_acl == NULL) {
681                                                 info->mti_big_aclsize = 0;
682                                                 CERROR("%s: unable to grow "
683                                                        DFID" ACL buffer\n",
684                                                        mdt_obd_name(mdt),
685                                                        PFID(mdt_object_fid(o)));
686                                                 RETURN(-ENOMEM);
687                                         }
688                                 }
689
690                                 CDEBUG(D_INODE, "%s: grow the "DFID
691                                        " ACL buffer to size %d\n",
692                                        mdt_obd_name(mdt),
693                                        PFID(mdt_object_fid(o)),
694                                        info->mti_big_aclsize);
695
696                                 buf->lb_buf = info->mti_big_acl;
697                                 buf->lb_len = info->mti_big_aclsize;
698                                 info->mti_big_acl_used = 1;
699                                 goto again;
700                         }
701                         /* FS has ACL bigger that our limits */
702                         CDEBUG(D_INODE, "%s: "DFID" ACL can't fit into %d\n",
703                                mdt_obd_name(mdt), PFID(mdt_object_fid(o)),
704                                info->mti_big_aclsize);
705                         rc = -E2BIG;
706                 } else {
707                         CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
708                                mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
709                 }
710         } else {
711                 rc = nodemap_map_acl(nodemap, buf->lb_buf,
712                                      rc, NODEMAP_FS_TO_CLIENT);
713                 /* if all ACLs mapped out, rc is still >= 0 */
714                 if (rc < 0) {
715                         CERROR("%s: nodemap_map_acl unable to parse "DFID
716                                " ACL: rc = %d\n", mdt_obd_name(mdt),
717                                PFID(mdt_object_fid(o)), rc);
718                         repbody->mbo_aclsize = 0;
719                         repbody->mbo_valid &= ~OBD_MD_FLACL;
720                 } else {
721                         repbody->mbo_aclsize = rc;
722                         repbody->mbo_valid |= OBD_MD_FLACL;
723                         rc = 0;
724                 }
725         }
726
727         RETURN(rc);
728 }
729 #endif
730
731 /* XXX Look into layout in MDT layer. */
732 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
733 {
734         struct lov_comp_md_v1   *comp_v1;
735         struct lov_mds_md       *v1;
736         int                      i;
737
738         if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
739                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
740
741                 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
742                         v1 = (struct lov_mds_md *)((char *)comp_v1 +
743                                 comp_v1->lcm_entries[i].lcme_offset);
744                         /* We don't support partial release for now */
745                         if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
746                                 return false;
747                 }
748                 return true;
749         } else {
750                 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
751                         true : false;
752         }
753 }
754
755 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
756                         const struct lu_attr *attr, const struct lu_fid *fid)
757 {
758         struct md_attr *ma = &info->mti_attr;
759         struct obd_export *exp = info->mti_exp;
760         struct lu_nodemap *nodemap = NULL;
761
762         LASSERT(ma->ma_valid & MA_INODE);
763
764         if (attr->la_valid & LA_ATIME) {
765                 b->mbo_atime = attr->la_atime;
766                 b->mbo_valid |= OBD_MD_FLATIME;
767         }
768         if (attr->la_valid & LA_MTIME) {
769                 b->mbo_mtime = attr->la_mtime;
770                 b->mbo_valid |= OBD_MD_FLMTIME;
771         }
772         if (attr->la_valid & LA_CTIME) {
773                 b->mbo_ctime = attr->la_ctime;
774                 b->mbo_valid |= OBD_MD_FLCTIME;
775         }
776         if (attr->la_valid & LA_BTIME) {
777                 b->mbo_btime = attr->la_btime;
778                 b->mbo_valid |= OBD_MD_FLBTIME;
779         }
780         if (attr->la_valid & LA_FLAGS) {
781                 b->mbo_flags = attr->la_flags;
782                 b->mbo_valid |= OBD_MD_FLFLAGS;
783         }
784         if (attr->la_valid & LA_NLINK) {
785                 b->mbo_nlink = attr->la_nlink;
786                 b->mbo_valid |= OBD_MD_FLNLINK;
787         }
788         if (attr->la_valid & (LA_UID|LA_GID)) {
789                 nodemap = nodemap_get_from_exp(exp);
790                 if (IS_ERR(nodemap))
791                         goto out;
792         }
793         if (attr->la_valid & LA_UID) {
794                 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
795                                             NODEMAP_FS_TO_CLIENT,
796                                             attr->la_uid);
797                 b->mbo_valid |= OBD_MD_FLUID;
798         }
799         if (attr->la_valid & LA_GID) {
800                 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
801                                             NODEMAP_FS_TO_CLIENT,
802                                             attr->la_gid);
803                 b->mbo_valid |= OBD_MD_FLGID;
804         }
805
806         if (attr->la_valid & LA_PROJID) {
807                 /* TODO, nodemap for project id */
808                 b->mbo_projid = attr->la_projid;
809                 b->mbo_valid |= OBD_MD_FLPROJID;
810         }
811
812         b->mbo_mode = attr->la_mode;
813         if (attr->la_valid & LA_MODE)
814                 b->mbo_valid |= OBD_MD_FLMODE;
815         if (attr->la_valid & LA_TYPE)
816                 b->mbo_valid |= OBD_MD_FLTYPE;
817
818         if (fid != NULL) {
819                 b->mbo_fid1 = *fid;
820                 b->mbo_valid |= OBD_MD_FLID;
821                 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
822                        PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
823         }
824
825         if (!(attr->la_valid & LA_TYPE))
826                 return;
827
828         b->mbo_rdev   = attr->la_rdev;
829         b->mbo_size   = attr->la_size;
830         b->mbo_blocks = attr->la_blocks;
831
832         if (!S_ISREG(attr->la_mode)) {
833                 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
834         } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
835                 /* means no objects are allocated on osts. */
836                 LASSERT(!(ma->ma_valid & MA_LOV));
837                 /* just ignore blocks occupied by extend attributes on MDS */
838                 b->mbo_blocks = 0;
839                 /* if no object is allocated on osts, the size on mds is valid.
840                  * b=22272 */
841                 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
842         } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
843                 if (mdt_hsm_is_released(ma->ma_lmm)) {
844                         /* A released file stores its size on MDS. */
845                         /* But return 1 block for released file, unless tools
846                          * like tar will consider it fully sparse. (LU-3864)
847                          */
848                         if (unlikely(b->mbo_size == 0))
849                                 b->mbo_blocks = 0;
850                         else
851                                 b->mbo_blocks = 1;
852                         b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
853                 } else if (info->mti_som_valid) { /* som is valid */
854                         b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
855                 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
856                         b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
857                         b->mbo_size = ma->ma_som.ms_size;
858                         b->mbo_blocks = ma->ma_som.ms_blocks;
859                 }
860         }
861
862         if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
863                             b->mbo_valid & OBD_MD_FLLAZYSIZE))
864                 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
865                        PFID(fid), (unsigned long long)b->mbo_size);
866
867 out:
868         if (!IS_ERR_OR_NULL(nodemap))
869                 nodemap_putref(nodemap);
870 }
871
872 static inline int mdt_body_has_lov(const struct lu_attr *la,
873                                    const struct mdt_body *body)
874 {
875         return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
876                (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
877 }
878
879 void mdt_client_compatibility(struct mdt_thread_info *info)
880 {
881         struct mdt_body       *body;
882         struct ptlrpc_request *req = mdt_info_req(info);
883         struct obd_export     *exp = req->rq_export;
884         struct md_attr        *ma = &info->mti_attr;
885         struct lu_attr        *la = &ma->ma_attr;
886         ENTRY;
887
888         if (exp_connect_layout(exp))
889                 /* the client can deal with 16-bit lmm_stripe_count */
890                 RETURN_EXIT;
891
892         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
893
894         if (!mdt_body_has_lov(la, body))
895                 RETURN_EXIT;
896
897         /* now we have a reply with a lov for a client not compatible with the
898          * layout lock so we have to clean the layout generation number */
899         if (S_ISREG(la->la_mode))
900                 ma->ma_lmm->lmm_layout_gen = 0;
901         EXIT;
902 }
903
904 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
905                                    struct mdt_object *o)
906 {
907         const struct lu_env *env = info->mti_env;
908         int rc, rc2;
909
910         rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
911                           XATTR_NAME_LOV);
912
913         if (rc == -ENODATA)
914                 rc = 0;
915
916         if (rc < 0)
917                 goto out;
918
919         /* Is it a directory? Let's check for the LMV as well */
920         if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
921                 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
922                                    XATTR_NAME_LMV);
923
924                 if (rc2 == -ENODATA)
925                         rc2 = mo_xattr_get(env, mdt_object_child(o),
926                                            &LU_BUF_NULL,
927                                            XATTR_NAME_DEFAULT_LMV);
928
929                 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
930                         rc = rc2;
931         }
932
933 out:
934         return rc;
935 }
936
937 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
938                       const char *name)
939 {
940         const struct lu_env *env = info->mti_env;
941         int rc;
942         ENTRY;
943
944         LASSERT(info->mti_big_lmm_used == 0);
945         rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
946         if (rc < 0)
947                 RETURN(rc);
948
949         /* big_lmm may need to be grown */
950         if (info->mti_big_lmmsize < rc) {
951                 int size = size_roundup_power2(rc);
952
953                 if (info->mti_big_lmmsize > 0) {
954                         /* free old buffer */
955                         LASSERT(info->mti_big_lmm);
956                         OBD_FREE_LARGE(info->mti_big_lmm,
957                                        info->mti_big_lmmsize);
958                         info->mti_big_lmm = NULL;
959                         info->mti_big_lmmsize = 0;
960                 }
961
962                 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
963                 if (info->mti_big_lmm == NULL)
964                         RETURN(-ENOMEM);
965                 info->mti_big_lmmsize = size;
966         }
967         LASSERT(info->mti_big_lmmsize >= rc);
968
969         info->mti_buf.lb_buf = info->mti_big_lmm;
970         info->mti_buf.lb_len = info->mti_big_lmmsize;
971         rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
972
973         RETURN(rc);
974 }
975
976 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
977                      struct md_attr *ma, const char *name)
978 {
979         struct md_object *next = mdt_object_child(o);
980         struct lu_buf    *buf = &info->mti_buf;
981         int rc;
982
983         if (strcmp(name, XATTR_NAME_LOV) == 0) {
984                 buf->lb_buf = ma->ma_lmm;
985                 buf->lb_len = ma->ma_lmm_size;
986                 LASSERT(!(ma->ma_valid & MA_LOV));
987         } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
988                 buf->lb_buf = ma->ma_lmv;
989                 buf->lb_len = ma->ma_lmv_size;
990                 LASSERT(!(ma->ma_valid & MA_LMV));
991         } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
992                 buf->lb_buf = ma->ma_default_lmv;
993                 buf->lb_len = ma->ma_default_lmv_size;
994                 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
995         } else {
996                 return -EINVAL;
997         }
998
999         LASSERT(buf->lb_buf);
1000
1001         rc = mo_xattr_get(info->mti_env, next, buf, name);
1002         if (rc > 0) {
1003
1004 got:
1005                 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1006                         if (info->mti_big_lmm_used)
1007                                 ma->ma_lmm = info->mti_big_lmm;
1008
1009                         /* NOT return LOV EA with hole to old client. */
1010                         if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1011                                      LOV_PATTERN_F_HOLE) &&
1012                             !(exp_connect_flags(info->mti_exp) &
1013                               OBD_CONNECT_LFSCK)) {
1014                                 return -EIO;
1015                         } else {
1016                                 ma->ma_lmm_size = rc;
1017                                 ma->ma_valid |= MA_LOV;
1018                         }
1019                 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1020                         if (info->mti_big_lmm_used)
1021                                 ma->ma_lmv = info->mti_big_lmm;
1022
1023                         ma->ma_lmv_size = rc;
1024                         ma->ma_valid |= MA_LMV;
1025                 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1026                         ma->ma_default_lmv_size = rc;
1027                         ma->ma_valid |= MA_LMV_DEF;
1028                 }
1029
1030                 /* Update mdt_max_mdsize so all clients will be aware that */
1031                 if (info->mti_mdt->mdt_max_mdsize < rc)
1032                         info->mti_mdt->mdt_max_mdsize = rc;
1033
1034                 rc = 0;
1035         } else if (rc == -ENODATA) {
1036                 /* no LOV EA */
1037                 rc = 0;
1038         } else if (rc == -ERANGE) {
1039                 /* Default LMV has fixed size, so it must be able to fit
1040                  * in the original buffer */
1041                 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1042                         return rc;
1043                 rc = mdt_big_xattr_get(info, o, name);
1044                 if (rc > 0) {
1045                         info->mti_big_lmm_used = 1;
1046                         goto got;
1047                 }
1048         }
1049
1050         return rc;
1051 }
1052
1053 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1054                    struct md_attr *ma, const char *name)
1055 {
1056         int rc;
1057
1058         if (!info->mti_big_lmm) {
1059                 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1060                 if (!info->mti_big_lmm)
1061                         return -ENOMEM;
1062                 info->mti_big_lmmsize = PAGE_SIZE;
1063         }
1064
1065         if (strcmp(name, XATTR_NAME_LOV) == 0) {
1066                 ma->ma_lmm = info->mti_big_lmm;
1067                 ma->ma_lmm_size = info->mti_big_lmmsize;
1068                 ma->ma_valid &= ~MA_LOV;
1069         } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1070                 ma->ma_lmv = info->mti_big_lmm;
1071                 ma->ma_lmv_size = info->mti_big_lmmsize;
1072                 ma->ma_valid &= ~MA_LMV;
1073         } else {
1074                 LBUG();
1075         }
1076
1077         LASSERT(!info->mti_big_lmm_used);
1078         rc = __mdt_stripe_get(info, o, ma, name);
1079         /* since big_lmm is always used here, clear 'used' flag to avoid
1080          * assertion in mdt_big_xattr_get().
1081          */
1082         info->mti_big_lmm_used = 0;
1083
1084         return rc;
1085 }
1086
1087 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1088                       struct lu_fid *pfid)
1089 {
1090         struct lu_buf           *buf = &info->mti_buf;
1091         struct link_ea_header   *leh;
1092         struct link_ea_entry    *lee;
1093         int                      rc;
1094         ENTRY;
1095
1096         buf->lb_buf = info->mti_big_lmm;
1097         buf->lb_len = info->mti_big_lmmsize;
1098         rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1099                           buf, XATTR_NAME_LINK);
1100         /* ignore errors, MA_PFID won't be set and it is
1101          * up to the caller to treat this as an error */
1102         if (rc == -ERANGE || buf->lb_len == 0) {
1103                 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1104                 buf->lb_buf = info->mti_big_lmm;
1105                 buf->lb_len = info->mti_big_lmmsize;
1106         }
1107
1108         if (rc < 0)
1109                 RETURN(rc);
1110         if (rc < sizeof(*leh)) {
1111                 CERROR("short LinkEA on "DFID": rc = %d\n",
1112                        PFID(mdt_object_fid(o)), rc);
1113                 RETURN(-ENODATA);
1114         }
1115
1116         leh = (struct link_ea_header *) buf->lb_buf;
1117         lee = (struct link_ea_entry *)(leh + 1);
1118         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1119                 leh->leh_magic = LINK_EA_MAGIC;
1120                 leh->leh_reccount = __swab32(leh->leh_reccount);
1121                 leh->leh_len = __swab64(leh->leh_len);
1122         }
1123         if (leh->leh_magic != LINK_EA_MAGIC)
1124                 RETURN(-EINVAL);
1125         if (leh->leh_reccount == 0)
1126                 RETURN(-ENODATA);
1127
1128         memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1129         fid_be_to_cpu(pfid, pfid);
1130
1131         RETURN(0);
1132 }
1133
1134 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1135                            struct lu_fid *pfid, struct lu_name *lname)
1136 {
1137         struct lu_buf *buf = &info->mti_buf;
1138         struct link_ea_header *leh;
1139         struct link_ea_entry *lee;
1140         int reclen;
1141         int rc;
1142
1143         buf->lb_buf = info->mti_xattr_buf;
1144         buf->lb_len = sizeof(info->mti_xattr_buf);
1145         rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1146                           XATTR_NAME_LINK);
1147         if (rc == -ERANGE) {
1148                 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1149                 buf->lb_buf = info->mti_big_lmm;
1150                 buf->lb_len = info->mti_big_lmmsize;
1151         }
1152         if (rc < 0)
1153                 return rc;
1154
1155         if (rc < sizeof(*leh)) {
1156                 CERROR("short LinkEA on "DFID": rc = %d\n",
1157                        PFID(mdt_object_fid(o)), rc);
1158                 return -ENODATA;
1159         }
1160
1161         leh = (struct link_ea_header *)buf->lb_buf;
1162         lee = (struct link_ea_entry *)(leh + 1);
1163         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1164                 leh->leh_magic = LINK_EA_MAGIC;
1165                 leh->leh_reccount = __swab32(leh->leh_reccount);
1166                 leh->leh_len = __swab64(leh->leh_len);
1167         }
1168         if (leh->leh_magic != LINK_EA_MAGIC)
1169                 return -EINVAL;
1170
1171         if (leh->leh_reccount == 0)
1172                 return -ENODATA;
1173
1174         linkea_entry_unpack(lee, &reclen, lname, pfid);
1175
1176         return 0;
1177 }
1178
1179 int mdt_attr_get_complex(struct mdt_thread_info *info,
1180                          struct mdt_object *o, struct md_attr *ma)
1181 {
1182         const struct lu_env *env = info->mti_env;
1183         struct md_object    *next = mdt_object_child(o);
1184         struct lu_buf       *buf = &info->mti_buf;
1185         int                  need = ma->ma_need;
1186         int                  rc = 0, rc2;
1187         u32                  mode;
1188         ENTRY;
1189
1190         ma->ma_valid = 0;
1191
1192         if (mdt_object_exists(o) == 0)
1193                 GOTO(out, rc = -ENOENT);
1194         mode = lu_object_attr(&next->mo_lu);
1195
1196         if (need & MA_INODE) {
1197                 ma->ma_need = MA_INODE;
1198                 rc = mo_attr_get(env, next, ma);
1199                 if (rc)
1200                         GOTO(out, rc);
1201
1202                 if (S_ISREG(mode))
1203                         (void) mdt_get_som(info, o, ma);
1204                 ma->ma_valid |= MA_INODE;
1205         }
1206
1207         if (need & MA_PFID) {
1208                 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1209                 if (rc == 0)
1210                         ma->ma_valid |= MA_PFID;
1211                 /* ignore this error, parent fid is not mandatory */
1212                 rc = 0;
1213         }
1214
1215         if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1216                 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1217                 if (rc)
1218                         GOTO(out, rc);
1219         }
1220
1221         if (need & MA_LMV && S_ISDIR(mode)) {
1222                 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1223                 if (rc != 0)
1224                         GOTO(out, rc);
1225         }
1226
1227         if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1228                 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1229                 if (rc != 0)
1230                         GOTO(out, rc);
1231         }
1232
1233         /*
1234          * In the handle of MA_INODE, we may already get the SOM attr.
1235          */
1236         if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1237                 rc = mdt_get_som(info, o, ma);
1238                 if (rc != 0)
1239                         GOTO(out, rc);
1240         }
1241
1242         if (need & MA_HSM && S_ISREG(mode)) {
1243                 buf->lb_buf = info->mti_xattr_buf;
1244                 buf->lb_len = sizeof(info->mti_xattr_buf);
1245                 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1246                              sizeof(info->mti_xattr_buf));
1247                 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1248                 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1249                 if (rc2 == 0)
1250                         ma->ma_valid |= MA_HSM;
1251                 else if (rc2 < 0 && rc2 != -ENODATA)
1252                         GOTO(out, rc = rc2);
1253         }
1254
1255 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1256         if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1257                 buf->lb_buf = ma->ma_acl;
1258                 buf->lb_len = ma->ma_acl_size;
1259                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1260                 if (rc2 > 0) {
1261                         ma->ma_acl_size = rc2;
1262                         ma->ma_valid |= MA_ACL_DEF;
1263                 } else if (rc2 == -ENODATA) {
1264                         /* no ACLs */
1265                         ma->ma_acl_size = 0;
1266                 } else
1267                         GOTO(out, rc = rc2);
1268         }
1269 #endif
1270 out:
1271         ma->ma_need = need;
1272         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1273                rc, ma->ma_valid, ma->ma_lmm);
1274         RETURN(rc);
1275 }
1276
1277 static int mdt_getattr_internal(struct mdt_thread_info *info,
1278                                 struct mdt_object *o, int ma_need)
1279 {
1280         struct mdt_device *mdt = info->mti_mdt;
1281         struct md_object *next = mdt_object_child(o);
1282         const struct mdt_body *reqbody = info->mti_body;
1283         struct ptlrpc_request *req = mdt_info_req(info);
1284         struct md_attr *ma = &info->mti_attr;
1285         struct lu_attr *la = &ma->ma_attr;
1286         struct req_capsule *pill = info->mti_pill;
1287         const struct lu_env *env = info->mti_env;
1288         struct mdt_body *repbody;
1289         struct lu_buf *buffer = &info->mti_buf;
1290         struct obd_export *exp = info->mti_exp;
1291         ktime_t kstart = ktime_get();
1292         int rc;
1293
1294         ENTRY;
1295
1296         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1297                 RETURN(err_serious(-ENOMEM));
1298
1299         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1300
1301         ma->ma_valid = 0;
1302
1303         if (mdt_object_remote(o)) {
1304                 /* This object is located on remote node.*/
1305                 /* Return -ENOTSUPP for old client */
1306                 if (!mdt_is_dne_client(req->rq_export))
1307                         GOTO(out, rc = -ENOTSUPP);
1308
1309                 repbody->mbo_fid1 = *mdt_object_fid(o);
1310                 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1311                 GOTO(out, rc = 0);
1312         }
1313
1314         if (reqbody->mbo_eadatasize > 0) {
1315                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1316                 if (buffer->lb_buf == NULL)
1317                         GOTO(out, rc = -EPROTO);
1318                 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1319                                                       RCL_SERVER);
1320         } else {
1321                 buffer->lb_buf = NULL;
1322                 buffer->lb_len = 0;
1323                 ma_need &= ~(MA_LOV | MA_LMV);
1324                 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1325                        mdt_obd_name(info->mti_mdt),
1326                        req->rq_export->exp_client_uuid.uuid);
1327         }
1328
1329         /* from 2.12.58 intent_getattr pack default LMV in reply */
1330         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1331             ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1332                     (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1333             req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1334                                   RCL_SERVER)) {
1335                 ma->ma_lmv = buffer->lb_buf;
1336                 ma->ma_lmv_size = buffer->lb_len;
1337                 ma->ma_default_lmv = req_capsule_server_get(pill,
1338                                                 &RMF_DEFAULT_MDT_MD);
1339                 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1340                                                 &RMF_DEFAULT_MDT_MD,
1341                                                 RCL_SERVER);
1342                 ma->ma_need = MA_INODE;
1343                 if (ma->ma_lmv_size > 0)
1344                         ma->ma_need |= MA_LMV;
1345                 if (ma->ma_default_lmv_size > 0)
1346                         ma->ma_need |= MA_LMV_DEF;
1347         } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1348                    (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1349                 /* If it is dir and client require MEA, then we got MEA */
1350                 /* Assumption: MDT_MD size is enough for lmv size. */
1351                 ma->ma_lmv = buffer->lb_buf;
1352                 ma->ma_lmv_size = buffer->lb_len;
1353                 ma->ma_need = MA_INODE;
1354                 if (ma->ma_lmv_size > 0) {
1355                         if (reqbody->mbo_valid & OBD_MD_MEA) {
1356                                 ma->ma_need |= MA_LMV;
1357                         } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1358                                 ma->ma_need |= MA_LMV_DEF;
1359                                 ma->ma_default_lmv = buffer->lb_buf;
1360                                 ma->ma_lmv = NULL;
1361                                 ma->ma_default_lmv_size = buffer->lb_len;
1362                                 ma->ma_lmv_size = 0;
1363                         }
1364                 }
1365         } else {
1366                 ma->ma_lmm = buffer->lb_buf;
1367                 ma->ma_lmm_size = buffer->lb_len;
1368                 ma->ma_need = MA_INODE | MA_HSM;
1369                 if (ma->ma_lmm_size > 0) {
1370                         ma->ma_need |= MA_LOV;
1371                         /* Older clients may crash if they getattr overstriped
1372                          * files
1373                          */
1374                         if (!exp_connect_overstriping(exp) &&
1375                             mdt_lmm_is_overstriping(ma->ma_lmm))
1376                                 RETURN(-EOPNOTSUPP);
1377                 }
1378         }
1379
1380         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1381             reqbody->mbo_valid & OBD_MD_FLDIREA  &&
1382             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1383                 /* get default stripe info for this dir. */
1384                 ma->ma_need |= MA_LOV_DEF;
1385         }
1386         ma->ma_need |= ma_need;
1387
1388         rc = mdt_attr_get_complex(info, o, ma);
1389         if (unlikely(rc)) {
1390                 CDEBUG_LIMIT(rc == -ENOENT ? D_OTHER : D_ERROR,
1391                              "%s: getattr error for "DFID": rc = %d\n",
1392                              mdt_obd_name(info->mti_mdt),
1393                              PFID(mdt_object_fid(o)), rc);
1394                 RETURN(rc);
1395         }
1396
1397         /* if file is released, check if a restore is running */
1398         if (ma->ma_valid & MA_HSM) {
1399                 repbody->mbo_valid |= OBD_MD_TSTATE;
1400                 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1401                     mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1402                         repbody->mbo_t_state = MS_RESTORE;
1403         }
1404
1405         if (unlikely(!(ma->ma_valid & MA_INODE)))
1406                 RETURN(-EFAULT);
1407
1408         mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1409
1410         if (mdt_body_has_lov(la, reqbody)) {
1411                 u32 stripe_count = 1;
1412
1413                 if (ma->ma_valid & MA_LOV) {
1414                         LASSERT(ma->ma_lmm_size);
1415                         repbody->mbo_eadatasize = ma->ma_lmm_size;
1416                         if (S_ISDIR(la->la_mode))
1417                                 repbody->mbo_valid |= OBD_MD_FLDIREA;
1418                         else
1419                                 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1420                         mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1421                 }
1422                 if (ma->ma_valid & MA_LMV) {
1423                         struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1424                         u32 magic = le32_to_cpu(lmv->lmv_magic);
1425
1426                         /* Return -ENOTSUPP for old client */
1427                         if (!mdt_is_striped_client(req->rq_export))
1428                                 RETURN(-ENOTSUPP);
1429
1430                         LASSERT(S_ISDIR(la->la_mode));
1431                         mdt_dump_lmv(D_INFO, ma->ma_lmv);
1432                         repbody->mbo_eadatasize = ma->ma_lmv_size;
1433                         repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1434
1435                         stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1436                         if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1437                                 mdt_restripe_migrate_add(info, o);
1438                         else if (magic == LMV_MAGIC_V1 &&
1439                                  lmv_is_restriping(lmv))
1440                                 mdt_restripe_update_add(info, o);
1441                 }
1442                 if (ma->ma_valid & MA_LMV_DEF) {
1443                         /* Return -ENOTSUPP for old client */
1444                         if (!mdt_is_striped_client(req->rq_export))
1445                                 RETURN(-ENOTSUPP);
1446                         LASSERT(S_ISDIR(la->la_mode));
1447                         /*
1448                          * when ll_dir_getstripe() gets default LMV, it
1449                          * checks mbo_eadatasize.
1450                          */
1451                         if (!(ma->ma_valid & MA_LMV))
1452                                 repbody->mbo_eadatasize =
1453                                         ma->ma_default_lmv_size;
1454                         repbody->mbo_valid |= (OBD_MD_FLDIREA |
1455                                                OBD_MD_DEFAULT_MEA);
1456                 }
1457                 CDEBUG(D_VFSTRACE,
1458                        "dirent count %llu stripe count %u MDT count %d\n",
1459                        ma->ma_attr.la_dirent_count, stripe_count,
1460                        atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1461                 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1462                     ma->ma_attr.la_dirent_count >
1463                         mdt->mdt_restriper.mdr_dir_split_count &&
1464                     !fid_is_root(mdt_object_fid(o)) &&
1465                     mdt->mdt_enable_dir_auto_split &&
1466                     !o->mot_restriping &&
1467                     stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1)
1468                         mdt_auto_split_add(info, o);
1469         } else if (S_ISLNK(la->la_mode) &&
1470                    reqbody->mbo_valid & OBD_MD_LINKNAME) {
1471                 buffer->lb_buf = ma->ma_lmm;
1472                 /* eadatasize from client includes NULL-terminator, so
1473                  * there is no need to read it */
1474                 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1475                 rc = mo_readlink(env, next, buffer);
1476                 if (unlikely(rc <= 0)) {
1477                         CERROR("%s: readlink failed for "DFID": rc = %d\n",
1478                                mdt_obd_name(info->mti_mdt),
1479                                PFID(mdt_object_fid(o)), rc);
1480                         rc = -EFAULT;
1481                 } else {
1482                         int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1483
1484                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1485                                 rc -= 2;
1486                         repbody->mbo_valid |= OBD_MD_LINKNAME;
1487                         /* we need to report back size with NULL-terminator
1488                          * because client expects that */
1489                         repbody->mbo_eadatasize = rc + 1;
1490                         if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1491                                 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1492                                        "on "DFID ", expected %d\n",
1493                                        mdt_obd_name(info->mti_mdt),
1494                                        rc, PFID(mdt_object_fid(o)),
1495                                        reqbody->mbo_eadatasize - 1);
1496                         /* NULL terminate */
1497                         ((char *)ma->ma_lmm)[rc] = 0;
1498
1499                         /* If the total CDEBUG() size is larger than a page, it
1500                          * will print a warning to the console, avoid this by
1501                          * printing just the last part of the symlink. */
1502                         CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1503                                print_limit < rc ? "..." : "", print_limit,
1504                                (char *)ma->ma_lmm + rc - print_limit, rc);
1505                         rc = 0;
1506                 }
1507         }
1508
1509         if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1510                 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1511                 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1512                 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1513                        repbody->mbo_max_mdsize);
1514         }
1515
1516 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1517         if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1518                  (reqbody->mbo_valid & OBD_MD_FLACL)) {
1519                 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1520                 if (IS_ERR(nodemap))
1521                         RETURN(PTR_ERR(nodemap));
1522
1523                 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1524                 nodemap_putref(nodemap);
1525         }
1526 #endif
1527
1528 out:
1529         if (rc == 0)
1530                 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1531                                  ktime_us_delta(ktime_get(), kstart));
1532
1533         RETURN(rc);
1534 }
1535
1536 static int mdt_getattr(struct tgt_session_info *tsi)
1537 {
1538         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1539         struct mdt_object       *obj = info->mti_object;
1540         struct req_capsule      *pill = info->mti_pill;
1541         struct mdt_body         *reqbody;
1542         struct mdt_body         *repbody;
1543         int rc, rc2;
1544         ENTRY;
1545
1546         if (unlikely(info->mti_object == NULL))
1547                 RETURN(-EPROTO);
1548
1549         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1550         LASSERT(reqbody);
1551         LASSERT(lu_object_assert_exists(&obj->mot_obj));
1552
1553         /* Special case for Data-on-MDT files to get data version */
1554         if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1555                 rc = mdt_data_version_get(tsi);
1556                 GOTO(out, rc);
1557         }
1558
1559         /* Unlike intent case where we need to pre-fill out buffers early on
1560          * in intent policy for ldlm reasons, here we can have a much better
1561          * guess at EA size by just reading it from disk.
1562          * Exceptions are readdir and (missing) directory striping */
1563         /* Readlink */
1564         if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1565                 /* No easy way to know how long is the symlink, but it cannot
1566                  * be more than PATH_MAX, so we allocate +1 */
1567                 rc = PATH_MAX + 1;
1568         /* A special case for fs ROOT: getattr there might fetch
1569          * default EA for entire fs, not just for this dir!
1570          */
1571         } else if (lu_fid_eq(mdt_object_fid(obj),
1572                              &info->mti_mdt->mdt_md_root_fid) &&
1573                    (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1574                    (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1575                                                                  MDS_GETATTR)) {
1576                 /* Should the default strping be bigger, mdt_fix_reply
1577                  * will reallocate */
1578                 rc = DEF_REP_MD_SIZE;
1579         } else {
1580                 /* Read the actual EA size from disk */
1581                 rc = mdt_attr_get_eabuf_size(info, obj);
1582         }
1583
1584         if (rc < 0)
1585                 GOTO(out, rc = err_serious(rc));
1586
1587         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1588
1589         /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1590          * by default. If the target object has more ACL entries, then
1591          * enlarge the buffer when necessary. */
1592         req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1593                              LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1594
1595         rc = req_capsule_server_pack(pill);
1596         if (unlikely(rc != 0))
1597                 GOTO(out, rc = err_serious(rc));
1598
1599         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1600         LASSERT(repbody != NULL);
1601         repbody->mbo_eadatasize = 0;
1602         repbody->mbo_aclsize = 0;
1603
1604         rc = mdt_check_ucred(info);
1605         if (unlikely(rc))
1606                 GOTO(out_shrink, rc);
1607
1608         info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1609
1610         rc = mdt_getattr_internal(info, obj, 0);
1611         EXIT;
1612 out_shrink:
1613         mdt_client_compatibility(info);
1614         rc2 = mdt_fix_reply(info);
1615         if (rc == 0)
1616                 rc = rc2;
1617 out:
1618         mdt_thread_info_fini(info);
1619         return rc;
1620 }
1621
1622 /**
1623  * Handler of layout intent RPC requiring the layout modification
1624  *
1625  * \param[in]  info     thread environment
1626  * \param[in]  obj      object
1627  * \param[out] lhc      object ldlm lock handle
1628  * \param[in]  layout   layout change descriptor
1629  *
1630  * \retval 0    on success
1631  * \retval < 0  error code
1632  */
1633 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1634                       struct mdt_lock_handle *lhc,
1635                       struct md_layout_change *layout)
1636 {
1637         int rc;
1638
1639         ENTRY;
1640
1641         if (!mdt_object_exists(obj))
1642                 RETURN(-ENOENT);
1643
1644         if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1645                 RETURN(-EINVAL);
1646
1647         rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1648                            MAY_WRITE);
1649         if (rc)
1650                 RETURN(rc);
1651
1652         rc = mdt_check_resent_lock(info, obj, lhc);
1653         if (rc < 0)
1654                 RETURN(rc);
1655
1656         if (rc > 0) {
1657                 /* not resent */
1658                 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1659
1660                 /* take layout lock to prepare layout change */
1661                 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1662                         lockpart |= MDS_INODELOCK_UPDATE;
1663
1664                 mdt_lock_handle_init(lhc);
1665                 mdt_lock_reg_init(lhc, LCK_EX);
1666                 rc = mdt_reint_object_lock(info, obj, lhc, lockpart, false);
1667                 if (rc)
1668                         RETURN(rc);
1669         }
1670
1671         mutex_lock(&obj->mot_som_mutex);
1672         rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1673         mutex_unlock(&obj->mot_som_mutex);
1674
1675         if (rc)
1676                 mdt_object_unlock(info, obj, lhc, 1);
1677
1678         RETURN(rc);
1679 }
1680
1681 /**
1682  * Exchange MOF_LOV_CREATED flags between two objects after a
1683  * layout swap. No assumption is made on whether o1 or o2 have
1684  * created objects or not.
1685  *
1686  * \param[in,out] o1    First swap layout object
1687  * \param[in,out] o2    Second swap layout object
1688  */
1689 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1690 {
1691         unsigned int o1_lov_created = o1->mot_lov_created;
1692
1693         mutex_lock(&o1->mot_lov_mutex);
1694         mutex_lock(&o2->mot_lov_mutex);
1695
1696         o1->mot_lov_created = o2->mot_lov_created;
1697         o2->mot_lov_created = o1_lov_created;
1698
1699         mutex_unlock(&o2->mot_lov_mutex);
1700         mutex_unlock(&o1->mot_lov_mutex);
1701 }
1702
1703 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1704 {
1705         struct mdt_thread_info  *info;
1706         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1707         struct obd_export       *exp = req->rq_export;
1708         struct mdt_object       *o1, *o2, *o;
1709         struct mdt_lock_handle  *lh1, *lh2;
1710         struct mdc_swap_layouts *msl;
1711         int                      rc;
1712         ENTRY;
1713
1714         /* client does not support layout lock, so layout swaping
1715          * is disabled.
1716          * FIXME: there is a problem for old clients which don't support
1717          * layout lock yet. If those clients have already opened the file
1718          * they won't be notified at all so that old layout may still be
1719          * used to do IO. This can be fixed after file release is landed by
1720          * doing exclusive open and taking full EX ibits lock. - Jinshan */
1721         if (!exp_connect_layout(exp))
1722                 RETURN(-EOPNOTSUPP);
1723
1724         info = tsi2mdt_info(tsi);
1725         if (unlikely(info->mti_object == NULL))
1726                 RETURN(-EPROTO);
1727
1728         if (info->mti_dlm_req != NULL)
1729                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1730
1731         o1 = info->mti_object;
1732         o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1733                                 &info->mti_body->mbo_fid2);
1734         if (IS_ERR(o))
1735                 GOTO(out, rc = PTR_ERR(o));
1736
1737         if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1738                 GOTO(put, rc = -ENOENT);
1739
1740         rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1741         if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1742                 GOTO(put, rc);
1743
1744         if (rc < 0)
1745                 swap(o1, o2);
1746
1747         /* permission check. Make sure the calling process having permission
1748          * to write both files. */
1749         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1750                            MAY_WRITE);
1751         if (rc < 0)
1752                 GOTO(put, rc);
1753
1754         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1755                            MAY_WRITE);
1756         if (rc < 0)
1757                 GOTO(put, rc);
1758
1759         msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1760         if (msl == NULL)
1761                 GOTO(put, rc = -EPROTO);
1762
1763         lh1 = &info->mti_lh[MDT_LH_NEW];
1764         mdt_lock_reg_init(lh1, LCK_EX);
1765         lh2 = &info->mti_lh[MDT_LH_OLD];
1766         mdt_lock_reg_init(lh2, LCK_EX);
1767
1768         rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1769                              MDS_INODELOCK_XATTR);
1770         if (rc < 0)
1771                 GOTO(put, rc);
1772
1773         rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1774                              MDS_INODELOCK_XATTR);
1775         if (rc < 0)
1776                 GOTO(unlock1, rc);
1777
1778         rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1779                              mdt_object_child(o2), msl->msl_flags);
1780         if (rc < 0)
1781                 GOTO(unlock2, rc);
1782
1783         mdt_swap_lov_flag(o1, o2);
1784
1785 unlock2:
1786         mdt_object_unlock(info, o2, lh2, rc);
1787 unlock1:
1788         mdt_object_unlock(info, o1, lh1, rc);
1789 put:
1790         mdt_object_put(info->mti_env, o);
1791 out:
1792         mdt_thread_info_fini(info);
1793         RETURN(rc);
1794 }
1795
1796 static int mdt_raw_lookup(struct mdt_thread_info *info,
1797                           struct mdt_object *parent,
1798                           const struct lu_name *lname,
1799                           struct ldlm_reply *ldlm_rep)
1800 {
1801         struct lu_fid   *child_fid = &info->mti_tmp_fid1;
1802         int              rc;
1803         ENTRY;
1804
1805         LASSERT(!info->mti_cross_ref);
1806
1807         /* Only got the fid of this obj by name */
1808         fid_zero(child_fid);
1809         rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1810                         lname, child_fid, &info->mti_spec);
1811         if (rc == 0) {
1812                 struct mdt_body *repbody;
1813
1814                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1815                 repbody->mbo_fid1 = *child_fid;
1816                 repbody->mbo_valid = OBD_MD_FLID;
1817                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1818         } else if (rc == -ENOENT) {
1819                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1820         }
1821
1822         RETURN(rc);
1823 }
1824
1825 /*
1826  * UPDATE lock should be taken against parent, and be released before exit;
1827  * child_bits lock should be taken against child, and be returned back:
1828  *            (1)normal request should release the child lock;
1829  *            (2)intent request will grant the lock to client.
1830  */
1831 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1832                                  struct mdt_lock_handle *lhc,
1833                                  __u64 child_bits,
1834                                  struct ldlm_reply *ldlm_rep)
1835 {
1836         struct ptlrpc_request *req = mdt_info_req(info);
1837         struct mdt_body *reqbody = NULL;
1838         struct mdt_object *parent = info->mti_object;
1839         struct mdt_object *child = NULL;
1840         struct lu_fid *child_fid = &info->mti_tmp_fid1;
1841         struct lu_name *lname = NULL;
1842         struct mdt_lock_handle *lhp = NULL;
1843         struct ldlm_lock *lock;
1844         struct req_capsule *pill = info->mti_pill;
1845         __u64 try_bits = 0;
1846         bool is_resent;
1847         int ma_need = 0;
1848         int rc;
1849
1850         ENTRY;
1851
1852         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1853         LASSERT(ergo(is_resent,
1854                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1855
1856         if (parent == NULL)
1857                 RETURN(-ENOENT);
1858
1859         if (info->mti_cross_ref) {
1860                 /* Only getattr on the child. Parent is on another node. */
1861                 mdt_set_disposition(info, ldlm_rep,
1862                                     DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1863                 child = parent;
1864                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1865                        "ldlm_rep = %p\n",
1866                        PFID(mdt_object_fid(child)), ldlm_rep);
1867
1868                 rc = mdt_check_resent_lock(info, child, lhc);
1869                 if (rc < 0) {
1870                         RETURN(rc);
1871                 } else if (rc > 0) {
1872                         mdt_lock_handle_init(lhc);
1873                         mdt_lock_reg_init(lhc, LCK_PR);
1874
1875                         /*
1876                          * Object's name entry is on another MDS, it will
1877                          * request PERM lock only because LOOKUP lock is owned
1878                          * by the MDS where name entry resides.
1879                          *
1880                          * TODO: it should try layout lock too. - Jinshan
1881                          */
1882                         child_bits &= ~(MDS_INODELOCK_LOOKUP |
1883                                         MDS_INODELOCK_LAYOUT);
1884                         child_bits |= MDS_INODELOCK_PERM;
1885
1886                         rc = mdt_object_lock(info, child, lhc, child_bits);
1887                         if (rc < 0)
1888                                 RETURN(rc);
1889                 }
1890
1891                 /* Finally, we can get attr for child. */
1892                 if (!mdt_object_exists(child)) {
1893                         LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1894                                         &child->mot_obj,
1895                                         "remote object doesn't exist.");
1896                         mdt_object_unlock(info, child, lhc, 1);
1897                         RETURN(-ENOENT);
1898                 }
1899
1900                 rc = mdt_getattr_internal(info, child, 0);
1901                 if (unlikely(rc != 0)) {
1902                         mdt_object_unlock(info, child, lhc, 1);
1903                         RETURN(rc);
1904                 }
1905
1906                 rc = mdt_pack_secctx_in_reply(info, child);
1907                 if (unlikely(rc)) {
1908                         mdt_object_unlock(info, child, lhc, 1);
1909                         RETURN(rc);
1910                 }
1911
1912                 rc = mdt_pack_encctx_in_reply(info, child);
1913                 if (unlikely(rc))
1914                         mdt_object_unlock(info, child, lhc, 1);
1915                 RETURN(rc);
1916         }
1917
1918         lname = &info->mti_name;
1919         mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
1920
1921         if (lu_name_is_valid(lname)) {
1922                 if (mdt_object_remote(parent)) {
1923                         CERROR("%s: parent "DFID" is on remote target\n",
1924                                mdt_obd_name(info->mti_mdt),
1925                                PFID(mdt_object_fid(parent)));
1926                         RETURN(-EPROTO);
1927                 }
1928
1929                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1930                        "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1931                        PNAME(lname), ldlm_rep);
1932         } else {
1933                 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1934                 if (unlikely(reqbody == NULL))
1935                         RETURN(err_serious(-EPROTO));
1936
1937                 *child_fid = reqbody->mbo_fid2;
1938                 if (unlikely(!fid_is_sane(child_fid)))
1939                         RETURN(err_serious(-EINVAL));
1940
1941                 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1942                         mdt_object_get(info->mti_env, parent);
1943                         child = parent;
1944                 } else {
1945                         child = mdt_object_find(info->mti_env, info->mti_mdt,
1946                                                 child_fid);
1947                         if (IS_ERR(child))
1948                                 RETURN(PTR_ERR(child));
1949                 }
1950
1951                 if (mdt_object_remote(child)) {
1952                         CERROR("%s: child "DFID" is on remote target\n",
1953                                mdt_obd_name(info->mti_mdt),
1954                                PFID(mdt_object_fid(child)));
1955                         GOTO(out_child, rc = -EPROTO);
1956                 }
1957
1958                 /* don't fetch LOOKUP lock if it's remote object */
1959                 rc = mdt_is_remote_object(info, parent, child);
1960                 if (rc < 0)
1961                         GOTO(out_child, rc);
1962                 if (rc)
1963                         child_bits &= ~MDS_INODELOCK_LOOKUP;
1964
1965                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1966                        "ldlm_rep = %p\n",
1967                        PFID(mdt_object_fid(parent)),
1968                        PFID(&reqbody->mbo_fid2), ldlm_rep);
1969         }
1970
1971         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1972
1973         if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1974                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1975                                 &parent->mot_obj,
1976                                 "Parent doesn't exist!");
1977                 GOTO(out_child, rc = -ESTALE);
1978         }
1979
1980         if (lu_name_is_valid(lname)) {
1981                 /* Always allow to lookup ".." */
1982                 if (unlikely(lname->ln_namelen == 2 &&
1983                              lname->ln_name[0] == '.' &&
1984                              lname->ln_name[1] == '.'))
1985                         info->mti_spec.sp_permitted = 1;
1986
1987                 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1988                         rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1989
1990                         RETURN(rc);
1991                 }
1992
1993                 /* step 1: lock parent only if parent is a directory */
1994                 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1995                         lhp = &info->mti_lh[MDT_LH_PARENT];
1996                         mdt_lock_pdo_init(lhp, LCK_PR, lname);
1997                         rc = mdt_object_lock(info, parent, lhp,
1998                                              MDS_INODELOCK_UPDATE);
1999                         if (unlikely(rc != 0))
2000                                 RETURN(rc);
2001                 }
2002
2003                 /* step 2: lookup child's fid by name */
2004                 fid_zero(child_fid);
2005                 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2006                                 child_fid, &info->mti_spec);
2007                 if (rc == -ENOENT)
2008                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2009
2010                 if (rc != 0)
2011                         GOTO(unlock_parent, rc);
2012
2013                 child = mdt_object_find(info->mti_env, info->mti_mdt,
2014                                         child_fid);
2015                 if (unlikely(IS_ERR(child)))
2016                         GOTO(unlock_parent, rc = PTR_ERR(child));
2017         }
2018
2019         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2020
2021         /* step 3: lock child regardless if it is local or remote. */
2022         LASSERT(child);
2023
2024         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2025         if (!mdt_object_exists(child)) {
2026                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2027                                 &child->mot_obj,
2028                                 "Object doesn't exist!");
2029                 GOTO(out_child, rc = -ENOENT);
2030         }
2031
2032         rc = mdt_check_resent_lock(info, child, lhc);
2033         if (rc < 0) {
2034                 GOTO(out_child, rc);
2035         } else if (rc > 0) {
2036                 mdt_lock_handle_init(lhc);
2037                 mdt_lock_reg_init(lhc, LCK_PR);
2038
2039                 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2040                     !mdt_object_remote(child)) {
2041                         struct md_attr *ma = &info->mti_attr;
2042
2043                         ma->ma_valid = 0;
2044                         ma->ma_need = MA_INODE;
2045                         rc = mdt_attr_get_complex(info, child, ma);
2046                         if (unlikely(rc != 0))
2047                                 GOTO(out_child, rc);
2048
2049                         /* If the file has not been changed for some time, we
2050                          * return not only a LOOKUP lock, but also an UPDATE
2051                          * lock and this might save us RPC on later STAT. For
2052                          * directories, it also let negative dentry cache start
2053                          * working for this dir. */
2054                         if (ma->ma_valid & MA_INODE &&
2055                             ma->ma_attr.la_valid & LA_CTIME &&
2056                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2057                             ma->ma_attr.la_ctime < ktime_get_real_seconds())
2058                                 child_bits |= MDS_INODELOCK_UPDATE;
2059                 }
2060
2061                 /* layout lock must be granted in a best-effort way
2062                  * for IT operations */
2063                 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2064                 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2065                     !mdt_object_remote(child) && ldlm_rep != NULL) {
2066                         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2067                             exp_connect_layout(info->mti_exp)) {
2068                                 /* try to grant layout lock for regular file. */
2069                                 try_bits = MDS_INODELOCK_LAYOUT;
2070                         }
2071                         /* Acquire DOM lock in advance for data-on-mdt file */
2072                         if (child != parent)
2073                                 try_bits |= MDS_INODELOCK_DOM;
2074                 }
2075
2076                 if (try_bits != 0) {
2077                         /* try layout lock, it may fail to be granted due to
2078                          * contention at LOOKUP or UPDATE */
2079                         rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2080                                                  try_bits, false);
2081                         if (child_bits & MDS_INODELOCK_LAYOUT)
2082                                 ma_need |= MA_LOV;
2083                 } else {
2084                         /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2085                          * client will enqueue the lock to the remote MDT */
2086                         if (mdt_object_remote(child))
2087                                 child_bits &= ~MDS_INODELOCK_UPDATE;
2088                         rc = mdt_object_lock(info, child, lhc, child_bits);
2089                 }
2090                 if (unlikely(rc != 0))
2091                         GOTO(out_child, rc);
2092         }
2093
2094         /* finally, we can get attr for child. */
2095         rc = mdt_getattr_internal(info, child, ma_need);
2096         if (unlikely(rc != 0)) {
2097                 mdt_object_unlock(info, child, lhc, 1);
2098                 GOTO(out_child, rc);
2099         }
2100
2101         rc = mdt_pack_secctx_in_reply(info, child);
2102         if (unlikely(rc)) {
2103                 mdt_object_unlock(info, child, lhc, 1);
2104                 GOTO(out_child, rc);
2105         }
2106
2107         rc = mdt_pack_encctx_in_reply(info, child);
2108         if (unlikely(rc)) {
2109                 mdt_object_unlock(info, child, lhc, 1);
2110                 GOTO(out_child, rc);
2111         }
2112
2113         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2114         if (lock) {
2115                 /* Debugging code. */
2116                 LDLM_DEBUG(lock, "Returning lock to client");
2117                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2118                                          &lock->l_resource->lr_name),
2119                          "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2120                          PLDLMRES(lock->l_resource),
2121                          PFID(mdt_object_fid(child)));
2122
2123                 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2124                     !mdt_object_remote(child) && child != parent) {
2125                         mdt_object_put(info->mti_env, child);
2126                         rc = mdt_pack_size2body(info, child_fid,
2127                                                 &lhc->mlh_reg_lh);
2128                         if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2129                                 /* DOM lock was taken in advance but this is
2130                                  * not DoM file. Drop the lock.
2131                                  */
2132                                 lock_res_and_lock(lock);
2133                                 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2134                                 unlock_res_and_lock(lock);
2135                         }
2136                         LDLM_LOCK_PUT(lock);
2137                         GOTO(unlock_parent, rc = 0);
2138                 }
2139                 LDLM_LOCK_PUT(lock);
2140         }
2141
2142         EXIT;
2143 out_child:
2144         if (child)
2145                 mdt_object_put(info->mti_env, child);
2146 unlock_parent:
2147         if (lhp)
2148                 mdt_object_unlock(info, parent, lhp, 1);
2149         return rc;
2150 }
2151
2152 /* normal handler: should release the child lock */
2153 static int mdt_getattr_name(struct tgt_session_info *tsi)
2154 {
2155         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
2156         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2157         struct mdt_body *reqbody;
2158         struct mdt_body *repbody;
2159         int rc, rc2;
2160
2161         ENTRY;
2162
2163         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2164         LASSERT(reqbody != NULL);
2165         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2166         LASSERT(repbody != NULL);
2167
2168         info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2169         repbody->mbo_eadatasize = 0;
2170         repbody->mbo_aclsize = 0;
2171
2172         rc = mdt_init_ucred(info, reqbody);
2173         if (unlikely(rc))
2174                 GOTO(out_shrink, rc);
2175
2176         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2177         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2178                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2179                 lhc->mlh_reg_lh.cookie = 0;
2180         }
2181         mdt_exit_ucred(info);
2182         EXIT;
2183 out_shrink:
2184         mdt_client_compatibility(info);
2185         rc2 = mdt_fix_reply(info);
2186         if (rc == 0)
2187                 rc = rc2;
2188         mdt_thread_info_fini(info);
2189         return rc;
2190 }
2191
2192 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2193                             const struct lu_fid *pfid,
2194                             const struct lu_name *name,
2195                             struct mdt_object *obj, s64 ctime)
2196 {
2197         struct lu_fid *child_fid = &info->mti_tmp_fid1;
2198         struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
2199         struct mdt_device *mdt = info->mti_mdt;
2200         struct md_attr *ma = &info->mti_attr;
2201         struct mdt_lock_handle *parent_lh;
2202         struct mdt_lock_handle *child_lh;
2203         struct mdt_object *pobj;
2204         bool cos_incompat = false;
2205         int rc;
2206         ENTRY;
2207
2208         pobj = mdt_object_find(info->mti_env, mdt, pfid);
2209         if (IS_ERR(pobj))
2210                 GOTO(out, rc = PTR_ERR(pobj));
2211
2212         parent_lh = &info->mti_lh[MDT_LH_PARENT];
2213         mdt_lock_pdo_init(parent_lh, LCK_PW, name);
2214         rc = mdt_object_lock(info, pobj, parent_lh, MDS_INODELOCK_UPDATE);
2215         if (rc != 0)
2216                 GOTO(put_parent, rc);
2217
2218         if (mdt_object_remote(pobj))
2219                 cos_incompat = true;
2220
2221         rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2222                         name, child_fid, &info->mti_spec);
2223         if (rc != 0)
2224                 GOTO(unlock_parent, rc);
2225
2226         if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2227                 GOTO(unlock_parent, rc = -EREMCHG);
2228
2229         child_lh = &info->mti_lh[MDT_LH_CHILD];
2230         mdt_lock_reg_init(child_lh, LCK_EX);
2231         rc = mdt_reint_striped_lock(info, obj, child_lh,
2232                                     MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
2233                                     einfo, cos_incompat);
2234         if (rc != 0)
2235                 GOTO(unlock_parent, rc);
2236
2237         if (atomic_read(&obj->mot_open_count)) {
2238                 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2239                        PFID(mdt_object_fid(obj)));
2240                 GOTO(unlock_child, rc = -EBUSY);
2241         }
2242
2243         ma->ma_need = 0;
2244         ma->ma_valid = MA_INODE;
2245         ma->ma_attr.la_valid = LA_CTIME;
2246         ma->ma_attr.la_ctime = ctime;
2247
2248         mutex_lock(&obj->mot_lov_mutex);
2249
2250         rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2251                         mdt_object_child(obj), name, ma, 0);
2252
2253         mutex_unlock(&obj->mot_lov_mutex);
2254
2255 unlock_child:
2256         mdt_reint_striped_unlock(info, obj, child_lh, einfo, 1);
2257 unlock_parent:
2258         mdt_object_unlock(info, pobj, parent_lh, 1);
2259 put_parent:
2260         mdt_object_put(info->mti_env, pobj);
2261 out:
2262         RETURN(rc);
2263 }
2264
2265 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2266                                         struct mdt_object *obj)
2267 {
2268         struct lu_ucred *uc = lu_ucred(info->mti_env);
2269         struct md_attr *ma = &info->mti_attr;
2270         struct lu_attr *la = &ma->ma_attr;
2271         int rc = 0;
2272         ENTRY;
2273
2274         ma->ma_need = MA_INODE;
2275         rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2276         if (rc)
2277                 GOTO(out, rc);
2278
2279         if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2280                         rc = -EACCES;
2281
2282         if (md_capable(uc, CAP_DAC_OVERRIDE))
2283                 RETURN(0);
2284         if (uc->uc_fsuid == la->la_uid) {
2285                 if ((la->la_mode & S_IWUSR) == 0)
2286                         rc = -EACCES;
2287         } else if (uc->uc_fsgid == la->la_gid) {
2288                 if ((la->la_mode & S_IWGRP) == 0)
2289                         rc = -EACCES;
2290         } else if ((la->la_mode & S_IWOTH) == 0) {
2291                         rc = -EACCES;
2292         }
2293
2294 out:
2295         RETURN(rc);
2296 }
2297
2298 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2299                          s64 ctime)
2300 {
2301         struct mdt_device *mdt = info->mti_mdt;
2302         struct mdt_object *obj = NULL;
2303         struct linkea_data ldata = { NULL };
2304         struct lu_buf *buf = &info->mti_big_buf;
2305         struct lu_name *name = &info->mti_name;
2306         struct lu_fid *pfid = &info->mti_tmp_fid1;
2307         struct link_ea_header *leh;
2308         struct link_ea_entry *lee;
2309         int reclen, count, rc = 0;
2310         ENTRY;
2311
2312         if (!fid_is_sane(fid))
2313                 GOTO(out, rc = -EINVAL);
2314
2315         if (!fid_is_namespace_visible(fid))
2316                 GOTO(out, rc = -EINVAL);
2317
2318         obj = mdt_object_find(info->mti_env, mdt, fid);
2319         if (IS_ERR(obj))
2320                 GOTO(out, rc = PTR_ERR(obj));
2321
2322         if (mdt_object_remote(obj))
2323                 GOTO(out, rc = -EREMOTE);
2324         if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2325                 GOTO(out, rc = -ENOENT);
2326
2327         rc = mdt_rmfid_check_permission(info, obj);
2328         if (rc)
2329                 GOTO(out, rc);
2330
2331         /* take LinkEA */
2332         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2333         if (!buf->lb_buf)
2334                 GOTO(out, rc = -ENOMEM);
2335
2336         ldata.ld_buf = buf;
2337         rc = mdt_links_read(info, obj, &ldata);
2338         if (rc)
2339                 GOTO(out, rc);
2340
2341         leh = buf->lb_buf;
2342         lee = (struct link_ea_entry *)(leh + 1);
2343         for (count = 0; count < leh->leh_reccount; count++) {
2344                 /* remove every hardlink */
2345                 linkea_entry_unpack(lee, &reclen, name, pfid);
2346                 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2347                 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2348                 if (rc)
2349                         break;
2350         }
2351
2352 out:
2353         if (obj && !IS_ERR(obj))
2354                 mdt_object_put(info->mti_env, obj);
2355         if (info->mti_big_buf.lb_buf)
2356                 lu_buf_free(&info->mti_big_buf);
2357
2358         RETURN(rc);
2359 }
2360
2361 static int mdt_rmfid(struct tgt_session_info *tsi)
2362 {
2363         struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2364         struct mdt_body *reqbody;
2365         struct lu_fid *fids, *rfids;
2366         int bufsize, rc;
2367         __u32 *rcs;
2368         int i, nr;
2369         ENTRY;
2370
2371         reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2372         if (reqbody == NULL)
2373                 RETURN(-EPROTO);
2374         bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2375                                        RCL_CLIENT);
2376         nr = bufsize / sizeof(struct lu_fid);
2377         if (nr * sizeof(struct lu_fid) != bufsize)
2378                 RETURN(-EINVAL);
2379         req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2380                              RCL_SERVER, nr * sizeof(__u32));
2381         req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2382                              RCL_SERVER, nr * sizeof(struct lu_fid));
2383         rc = req_capsule_server_pack(tsi->tsi_pill);
2384         if (rc)
2385                 GOTO(out, rc = err_serious(rc));
2386         fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2387         if (fids == NULL)
2388                 RETURN(-EPROTO);
2389         rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2390         LASSERT(rcs);
2391         rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2392         LASSERT(rfids);
2393
2394         mdt_init_ucred(mti, reqbody);
2395         for (i = 0; i < nr; i++) {
2396                 rfids[i] = fids[i];
2397                 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2398         }
2399         mdt_exit_ucred(mti);
2400
2401 out:
2402         RETURN(rc);
2403 }
2404
2405 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2406                          void *karg, void __user *uarg);
2407
2408 static int mdt_set_info(struct tgt_session_info *tsi)
2409 {
2410         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2411         char                    *key;
2412         void                    *val;
2413         int                      keylen, vallen, rc = 0;
2414
2415         ENTRY;
2416
2417         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2418         if (key == NULL) {
2419                 DEBUG_REQ(D_HA, req, "no set_info key");
2420                 RETURN(err_serious(-EFAULT));
2421         }
2422
2423         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2424                                       RCL_CLIENT);
2425
2426         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2427         if (val == NULL) {
2428                 DEBUG_REQ(D_HA, req, "no set_info val");
2429                 RETURN(err_serious(-EFAULT));
2430         }
2431
2432         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2433                                       RCL_CLIENT);
2434
2435         /* Swab any part of val you need to here */
2436         if (KEY_IS(KEY_READ_ONLY)) {
2437                 spin_lock(&req->rq_export->exp_lock);
2438                 if (*(__u32 *)val)
2439                         *exp_connect_flags_ptr(req->rq_export) |=
2440                                 OBD_CONNECT_RDONLY;
2441                 else
2442                         *exp_connect_flags_ptr(req->rq_export) &=
2443                                 ~OBD_CONNECT_RDONLY;
2444                 spin_unlock(&req->rq_export->exp_lock);
2445         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2446                 struct changelog_setinfo *cs = val;
2447
2448                 if (vallen != sizeof(*cs)) {
2449                         CERROR("%s: bad changelog_clear setinfo size %d\n",
2450                                tgt_name(tsi->tsi_tgt), vallen);
2451                         RETURN(-EINVAL);
2452                 }
2453                 if (ptlrpc_req_need_swab(req)) {
2454                         __swab64s(&cs->cs_recno);
2455                         __swab32s(&cs->cs_id);
2456                 }
2457
2458                 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
2459                         RETURN(-EACCES);
2460                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2461                                    vallen, val, NULL);
2462         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2463                 if (vallen > 0)
2464                         obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2465         } else {
2466                 RETURN(-EINVAL);
2467         }
2468         RETURN(rc);
2469 }
2470
2471 static int mdt_readpage(struct tgt_session_info *tsi)
2472 {
2473         struct mdt_thread_info  *info = mdt_th_info(tsi->tsi_env);
2474         struct mdt_object       *object = mdt_obj(tsi->tsi_corpus);
2475         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
2476         const struct mdt_body   *reqbody = tsi->tsi_mdt_body;
2477         struct mdt_body         *repbody;
2478         int                      rc;
2479         int                      i;
2480
2481         ENTRY;
2482
2483         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2484                 RETURN(err_serious(-ENOMEM));
2485
2486         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2487         if (repbody == NULL || reqbody == NULL)
2488                 RETURN(err_serious(-EFAULT));
2489
2490         /*
2491          * prepare @rdpg before calling lower layers and transfer itself. Here
2492          * reqbody->size contains offset of where to start to read and
2493          * reqbody->nlink contains number bytes to read.
2494          */
2495         rdpg->rp_hash = reqbody->mbo_size;
2496         if (rdpg->rp_hash != reqbody->mbo_size) {
2497                 CERROR("Invalid hash: %#llx != %#llx\n",
2498                        rdpg->rp_hash, reqbody->mbo_size);
2499                 RETURN(-EFAULT);
2500         }
2501
2502         rdpg->rp_attrs = reqbody->mbo_mode;
2503         if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2504                 rdpg->rp_attrs |= LUDA_64BITHASH;
2505         rdpg->rp_count  = min_t(unsigned int, reqbody->mbo_nlink,
2506                                 exp_max_brw_size(tsi->tsi_exp));
2507         rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2508                           PAGE_SHIFT;
2509         OBD_ALLOC_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2510         if (rdpg->rp_pages == NULL)
2511                 RETURN(-ENOMEM);
2512
2513         for (i = 0; i < rdpg->rp_npages; ++i) {
2514                 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2515                 if (rdpg->rp_pages[i] == NULL)
2516                         GOTO(free_rdpg, rc = -ENOMEM);
2517         }
2518
2519         /* call lower layers to fill allocated pages with directory data */
2520         rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2521         if (rc < 0)
2522                 GOTO(free_rdpg, rc);
2523
2524         /* send pages to client */
2525         rc = tgt_sendpage(tsi, rdpg, rc);
2526
2527         EXIT;
2528 free_rdpg:
2529
2530         for (i = 0; i < rdpg->rp_npages; i++)
2531                 if (rdpg->rp_pages[i] != NULL)
2532                         __free_page(rdpg->rp_pages[i]);
2533         OBD_FREE_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2534
2535         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2536                 RETURN(0);
2537
2538         return rc;
2539 }
2540
2541 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2542 {
2543         struct lu_ucred *uc = mdt_ucred_check(info);
2544         struct lu_attr *attr = &info->mti_attr.ma_attr;
2545
2546         if (uc == NULL)
2547                 return -EINVAL;
2548
2549         if (op != REINT_SETATTR) {
2550                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2551                         attr->la_uid = uc->uc_fsuid;
2552                 /* for S_ISGID, inherit gid from his parent, such work will be
2553                  * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2554                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2555                         attr->la_gid = uc->uc_fsgid;
2556         }
2557
2558         return 0;
2559 }
2560
2561 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2562 {
2563         return op == REINT_OPEN &&
2564              !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2565 }
2566
2567 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2568 {
2569         struct req_capsule *pill = info->mti_pill;
2570
2571         if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2572                                   RCL_SERVER) &&
2573             req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2574                                   RCL_CLIENT)) {
2575                 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2576                                          RCL_CLIENT) != 0)
2577                         /* pre-set size in server part with max size */
2578                         req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2579                                              RCL_SERVER,
2580                                              OBD_MAX_DEFAULT_EA_SIZE);
2581                 else
2582                         req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2583                                              RCL_SERVER, 0);
2584         }
2585 }
2586
2587 static void mdt_preset_encctx_size(struct mdt_thread_info *info)
2588 {
2589         struct req_capsule *pill = info->mti_pill;
2590
2591         if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX,
2592                                   RCL_SERVER))
2593                 /* pre-set size in server part with max size */
2594                 req_capsule_set_size(pill, &RMF_FILE_ENCCTX,
2595                                      RCL_SERVER,
2596                                      info->mti_mdt->mdt_max_mdsize);
2597 }
2598
2599 static int mdt_reint_internal(struct mdt_thread_info *info,
2600                               struct mdt_lock_handle *lhc,
2601                               __u32 op)
2602 {
2603         struct req_capsule      *pill = info->mti_pill;
2604         struct mdt_body         *repbody;
2605         int                      rc = 0, rc2;
2606
2607         ENTRY;
2608
2609         rc = mdt_reint_unpack(info, op);
2610         if (rc != 0) {
2611                 CERROR("Can't unpack reint, rc %d\n", rc);
2612                 RETURN(err_serious(rc));
2613         }
2614
2615
2616         /* check if the file system is set to readonly. O_RDONLY open
2617          * is still allowed even the file system is set to readonly mode */
2618         if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
2619                 RETURN(err_serious(-EROFS));
2620
2621         /* for replay (no_create) lmm is not needed, client has it already */
2622         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2623                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2624                                      DEF_REP_MD_SIZE);
2625
2626         /* llog cookies are always 0, the field is kept for compatibility */
2627         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2628                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2629
2630         /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2631          * by default. If the target object has more ACL entries, then
2632          * enlarge the buffer when necessary. */
2633         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2634                 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2635                                      LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2636
2637         mdt_preset_secctx_size(info);
2638         mdt_preset_encctx_size(info);
2639
2640         rc = req_capsule_server_pack(pill);
2641         if (rc != 0) {
2642                 CERROR("Can't pack response, rc %d\n", rc);
2643                 RETURN(err_serious(rc));
2644         }
2645
2646         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2647                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2648                 LASSERT(repbody);
2649                 repbody->mbo_eadatasize = 0;
2650                 repbody->mbo_aclsize = 0;
2651         }
2652
2653         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2654
2655         /* for replay no cookkie / lmm need, because client have this already */
2656         if (info->mti_spec.no_create)
2657                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2658                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2659
2660         rc = mdt_init_ucred_reint(info);
2661         if (rc)
2662                 GOTO(out_shrink, rc);
2663
2664         rc = mdt_fix_attr_ucred(info, op);
2665         if (rc != 0)
2666                 GOTO(out_ucred, rc = err_serious(rc));
2667
2668         rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2669         if (rc < 0) {
2670                 GOTO(out_ucred, rc);
2671         } else if (rc == 1) {
2672                 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
2673                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2674                 GOTO(out_ucred, rc);
2675         }
2676         rc = mdt_reint_rec(info, lhc);
2677         EXIT;
2678 out_ucred:
2679         mdt_exit_ucred(info);
2680 out_shrink:
2681         mdt_client_compatibility(info);
2682
2683         rc2 = mdt_fix_reply(info);
2684         if (rc == 0)
2685                 rc = rc2;
2686
2687         /*
2688          * Data-on-MDT optimization - read data along with OPEN and return it
2689          * in reply when possible.
2690          */
2691         if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
2692                 rc = mdt_dom_read_on_open(info, info->mti_mdt,
2693                                           &lhc->mlh_reg_lh);
2694
2695         return rc;
2696 }
2697
2698 static long mdt_reint_opcode(struct ptlrpc_request *req,
2699                              const struct req_format **fmt)
2700 {
2701         struct mdt_device       *mdt;
2702         struct mdt_rec_reint    *rec;
2703         long                     opc;
2704
2705         rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2706         if (rec != NULL) {
2707                 opc = rec->rr_opcode;
2708                 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2709                 if (opc < REINT_MAX && fmt[opc] != NULL)
2710                         req_capsule_extend(&req->rq_pill, fmt[opc]);
2711                 else {
2712                         mdt = mdt_exp2dev(req->rq_export);
2713                         CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2714                                " rc = %d\n", req->rq_export->exp_obd->obd_name,
2715                                opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2716                         opc = err_serious(-EFAULT);
2717                 }
2718         } else {
2719                 opc = err_serious(-EFAULT);
2720         }
2721         return opc;
2722 }
2723
2724 static int mdt_reint(struct tgt_session_info *tsi)
2725 {
2726         long opc;
2727         int  rc;
2728         static const struct req_format *reint_fmts[REINT_MAX] = {
2729                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
2730                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
2731                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
2732                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
2733                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
2734                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
2735                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2736                 [REINT_RMENTRY]  = &RQF_MDS_REINT_UNLINK,
2737                 [REINT_MIGRATE]  = &RQF_MDS_REINT_MIGRATE,
2738                 [REINT_RESYNC]   = &RQF_MDS_REINT_RESYNC,
2739         };
2740
2741         ENTRY;
2742
2743         opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2744         if (opc >= 0) {
2745                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2746                 /*
2747                  * No lock possible here from client to pass it to reint code
2748                  * path.
2749                  */
2750                 rc = mdt_reint_internal(info, NULL, opc);
2751                 mdt_thread_info_fini(info);
2752         } else {
2753                 rc = opc;
2754         }
2755
2756         tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2757         RETURN(rc);
2758 }
2759
2760 /* this should sync the whole device */
2761 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2762 {
2763         struct dt_device *dt = mdt->mdt_bottom;
2764         int rc;
2765         ENTRY;
2766
2767         rc = dt->dd_ops->dt_sync(env, dt);
2768         RETURN(rc);
2769 }
2770
2771 /* this should sync this object */
2772 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
2773                            struct mdt_object *mo)
2774 {
2775         int rc = 0;
2776
2777         ENTRY;
2778
2779         if (!mdt_object_exists(mo)) {
2780                 CWARN("%s: non existing object "DFID": rc = %d\n",
2781                       exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
2782                       -ESTALE);
2783                 RETURN(-ESTALE);
2784         }
2785
2786         if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
2787                 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
2788                 dt_obj_version_t version;
2789
2790                 version = dt_version_get(env, mdt_obj2dt(mo));
2791                 if (version > tgt->lut_obd->obd_last_committed)
2792                         rc = mo_object_sync(env, mdt_object_child(mo));
2793         } else {
2794                 rc = mo_object_sync(env, mdt_object_child(mo));
2795         }
2796
2797         RETURN(rc);
2798 }
2799
2800 static int mdt_sync(struct tgt_session_info *tsi)
2801 {
2802         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2803         struct req_capsule      *pill = tsi->tsi_pill;
2804         struct mdt_body         *body;
2805         ktime_t                  kstart = ktime_get();
2806         int                      rc;
2807
2808         ENTRY;
2809
2810         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2811                 RETURN(err_serious(-ENOMEM));
2812
2813         if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2814                 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2815         } else {
2816                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2817
2818                 if (unlikely(info->mti_object == NULL))
2819                         RETURN(-EPROTO);
2820
2821                 /* sync an object */
2822                 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
2823                                      info->mti_object);
2824                 if (rc == 0) {
2825                         const struct lu_fid *fid;
2826                         struct lu_attr *la = &info->mti_attr.ma_attr;
2827
2828                         info->mti_attr.ma_need = MA_INODE;
2829                         info->mti_attr.ma_valid = 0;
2830                         rc = mdt_attr_get_complex(info, info->mti_object,
2831                                                   &info->mti_attr);
2832                         if (rc == 0) {
2833                                 body = req_capsule_server_get(pill,
2834                                                               &RMF_MDT_BODY);
2835                                 fid = mdt_object_fid(info->mti_object);
2836                                 mdt_pack_attr2body(info, body, la, fid);
2837                         }
2838                 }
2839                 mdt_thread_info_fini(info);
2840         }
2841         if (rc == 0)
2842                 mdt_counter_incr(req, LPROC_MDT_SYNC,
2843                                  ktime_us_delta(ktime_get(), kstart));
2844
2845         RETURN(rc);
2846 }
2847
2848 static int mdt_data_sync(struct tgt_session_info *tsi)
2849 {
2850         struct mdt_thread_info *info;
2851         struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2852         struct ost_body *body = tsi->tsi_ost_body;
2853         struct ost_body *repbody;
2854         struct mdt_object *mo = NULL;
2855         struct md_attr *ma;
2856         int rc = 0;
2857
2858         ENTRY;
2859
2860         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2861
2862         /* if no fid is specified then do nothing,
2863          * device sync is done via MDS_SYNC */
2864         if (fid_is_zero(&tsi->tsi_fid))
2865                 RETURN(0);
2866
2867         mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
2868         if (IS_ERR(mo))
2869                 RETURN(PTR_ERR(mo));
2870
2871         rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
2872         if (rc)
2873                 GOTO(put, rc);
2874
2875         repbody->oa.o_oi = body->oa.o_oi;
2876         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2877
2878         info = tsi2mdt_info(tsi);
2879         ma = &info->mti_attr;
2880         ma->ma_need = MA_INODE;
2881         ma->ma_valid = 0;
2882         rc = mdt_attr_get_complex(info, mo, ma);
2883         if (rc == 0)
2884                 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
2885         else
2886                 rc = 0;
2887         mdt_thread_info_fini(info);
2888
2889         EXIT;
2890 put:
2891         if (mo != NULL)
2892                 mdt_object_put(tsi->tsi_env, mo);
2893         return rc;
2894 }
2895
2896 /*
2897  * Handle quota control requests to consult current usage/limit, but also
2898  * to configure quota enforcement
2899  */
2900 static int mdt_quotactl(struct tgt_session_info *tsi)
2901 {
2902         struct obd_export *exp  = tsi->tsi_exp;
2903         struct req_capsule *pill = tsi->tsi_pill;
2904         struct obd_quotactl *oqctl, *repoqc;
2905         int id, rc;
2906         struct mdt_device *mdt = mdt_exp2dev(exp);
2907         struct lu_device *qmt = mdt->mdt_qmt_dev;
2908         struct lu_nodemap *nodemap;
2909         ENTRY;
2910
2911         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2912         if (!oqctl)
2913                 RETURN(err_serious(-EPROTO));
2914
2915         rc = req_capsule_server_pack(pill);
2916         if (rc)
2917                 RETURN(err_serious(rc));
2918
2919         nodemap = nodemap_get_from_exp(exp);
2920         if (IS_ERR(nodemap))
2921                 RETURN(PTR_ERR(nodemap));
2922
2923         switch (oqctl->qc_cmd) {
2924                 /* master quotactl */
2925         case Q_SETINFO:
2926         case Q_SETQUOTA:
2927         case LUSTRE_Q_SETDEFAULT:
2928         case LUSTRE_Q_SETQUOTAPOOL:
2929         case LUSTRE_Q_SETINFOPOOL:
2930                 if (!nodemap_can_setquota(nodemap))
2931                         GOTO(out_nodemap, rc = -EPERM);
2932                 /* fallthrough */
2933         case Q_GETINFO:
2934         case Q_GETQUOTA:
2935         case LUSTRE_Q_GETDEFAULT:
2936         case LUSTRE_Q_GETQUOTAPOOL:
2937         case LUSTRE_Q_GETINFOPOOL:
2938                 if (qmt == NULL)
2939                         GOTO(out_nodemap, rc = -EOPNOTSUPP);
2940                 /* slave quotactl */
2941                 /* fallthrough */
2942         case Q_GETOINFO:
2943         case Q_GETOQUOTA:
2944                 break;
2945         default:
2946                 rc = -EFAULT;
2947                 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
2948                        mdt_obd_name(mdt), oqctl->qc_cmd, rc);
2949                 GOTO(out_nodemap, rc);
2950         }
2951
2952         id = oqctl->qc_id;
2953         switch (oqctl->qc_type) {
2954         case USRQUOTA:
2955                 id = nodemap_map_id(nodemap, NODEMAP_UID,
2956                                     NODEMAP_CLIENT_TO_FS, id);
2957                 break;
2958         case GRPQUOTA:
2959                 id = nodemap_map_id(nodemap, NODEMAP_GID,
2960                                     NODEMAP_CLIENT_TO_FS, id);
2961                 break;
2962         case PRJQUOTA:
2963                 /* todo: check/map project id */
2964                 id = oqctl->qc_id;
2965                 break;
2966         default:
2967                 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2968         }
2969         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2970         if (repoqc == NULL)
2971                 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2972
2973         if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2974                 barrier_exit(tsi->tsi_tgt->lut_bottom);
2975
2976         if (oqctl->qc_id != id)
2977                 swap(oqctl->qc_id, id);
2978
2979         if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2980                 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2981                         RETURN(-EINPROGRESS);
2982         }
2983
2984         switch (oqctl->qc_cmd) {
2985
2986         case Q_GETINFO:
2987         case Q_SETINFO:
2988         case Q_SETQUOTA:
2989         case Q_GETQUOTA:
2990         case LUSTRE_Q_SETDEFAULT:
2991         case LUSTRE_Q_GETDEFAULT:
2992         case LUSTRE_Q_SETQUOTAPOOL:
2993         case LUSTRE_Q_GETQUOTAPOOL:
2994         case LUSTRE_Q_SETINFOPOOL:
2995         case LUSTRE_Q_GETINFOPOOL:
2996                 /* forward quotactl request to QMT */
2997                 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2998                 break;
2999
3000         case Q_GETOINFO:
3001         case Q_GETOQUOTA:
3002                 /* slave quotactl */
3003                 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
3004                                    oqctl);
3005                 break;
3006
3007         default:
3008                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
3009                 GOTO(out_nodemap, rc = -EFAULT);
3010         }
3011
3012         if (oqctl->qc_id != id)
3013                 swap(oqctl->qc_id, id);
3014
3015         QCTL_COPY(repoqc, oqctl);
3016         EXIT;
3017
3018 out_nodemap:
3019         nodemap_putref(nodemap);
3020
3021         return rc;
3022 }
3023
3024 /** clone llog ctxt from child (mdd)
3025  * This allows remote llog (replicator) access.
3026  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3027  * context was originally set up, or we can handle them directly.
3028  * I choose the latter, but that means I need any llog
3029  * contexts set up by child to be accessable by the mdt.  So we clone the
3030  * context into our context list here.
3031  */
3032 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3033                                int idx)
3034 {
3035         struct md_device  *next = mdt->mdt_child;
3036         struct llog_ctxt *ctxt;
3037         int rc;
3038
3039         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3040                 return 0;
3041
3042         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3043         if (rc || ctxt == NULL) {
3044                 return 0;
3045         }
3046
3047         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3048         if (rc)
3049                 CERROR("Can't set mdt ctxt %d\n", rc);
3050
3051         return rc;
3052 }
3053
3054 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3055                                  struct mdt_device *mdt, int idx)
3056 {
3057         struct llog_ctxt *ctxt;
3058
3059         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3060         if (ctxt == NULL)
3061                 return 0;
3062         /* Put once for the get we just did, and once for the clone */
3063         llog_ctxt_put(ctxt);
3064         llog_ctxt_put(ctxt);
3065         return 0;
3066 }
3067
3068 /*
3069  * sec context handlers
3070  */
3071 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3072 {
3073         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3074
3075         return 0;
3076 }
3077
3078 /*
3079  * quota request handlers
3080  */
3081 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3082 {
3083         struct mdt_device       *mdt = mdt_exp2dev(tsi->tsi_exp);
3084         struct lu_device        *qmt = mdt->mdt_qmt_dev;
3085         int                      rc;
3086         ENTRY;
3087
3088         if (qmt == NULL)
3089                 RETURN(err_serious(-EOPNOTSUPP));
3090
3091         rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3092         RETURN(rc);
3093 }
3094
3095 struct mdt_object *mdt_object_new(const struct lu_env *env,
3096                                   struct mdt_device *d,
3097                                   const struct lu_fid *f)
3098 {
3099         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3100         struct lu_object *o;
3101         struct mdt_object *m;
3102         ENTRY;
3103
3104         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3105         o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3106         if (unlikely(IS_ERR(o)))
3107                 m = (struct mdt_object *)o;
3108         else
3109                 m = mdt_obj(o);
3110         RETURN(m);
3111 }
3112
3113 struct mdt_object *mdt_object_find(const struct lu_env *env,
3114                                    struct mdt_device *d,
3115                                    const struct lu_fid *f)
3116 {
3117         struct lu_object *o;
3118         struct mdt_object *m;
3119         ENTRY;
3120
3121         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3122         o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3123         if (unlikely(IS_ERR(o)))
3124                 m = (struct mdt_object *)o;
3125         else
3126                 m = mdt_obj(o);
3127
3128         RETURN(m);
3129 }
3130
3131 /**
3132  * Asyncronous commit for mdt device.
3133  *
3134  * Pass asynchonous commit call down the MDS stack.
3135  *
3136  * \param env environment
3137  * \param mdt the mdt device
3138  */
3139 static void mdt_device_commit_async(const struct lu_env *env,
3140                                     struct mdt_device *mdt)
3141 {
3142         struct dt_device *dt = mdt->mdt_bottom;
3143         int rc;
3144         ENTRY;
3145
3146         rc = dt->dd_ops->dt_commit_async(env, dt);
3147         if (unlikely(rc != 0))
3148                 CWARN("%s: async commit start failed: rc = %d\n",
3149                       mdt_obd_name(mdt), rc);
3150         atomic_inc(&mdt->mdt_async_commit_count);
3151         EXIT;
3152 }
3153
3154 /**
3155  * Mark the lock as "synchonous".
3156  *
3157  * Mark the lock to deffer transaction commit to the unlock time.
3158  *
3159  * \param lock the lock to mark as "synchonous"
3160  *
3161  * \see mdt_is_lock_sync
3162  * \see mdt_save_lock
3163  */
3164 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3165 {
3166         lock->l_ast_data = (void*)1;
3167 }
3168
3169 /**
3170  * Check whehter the lock "synchonous" or not.
3171  *
3172  * \param lock the lock to check
3173  * \retval 1 the lock is "synchonous"
3174  * \retval 0 the lock isn't "synchronous"
3175  *
3176  * \see mdt_set_lock_sync
3177  * \see mdt_save_lock
3178  */
3179 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3180 {
3181         return lock->l_ast_data != NULL;
3182 }
3183
3184 /**
3185  * Blocking AST for mdt locks.
3186  *
3187  * Starts transaction commit if in case of COS lock conflict or
3188  * deffers such a commit to the mdt_save_lock.
3189  *
3190  * \param lock the lock which blocks a request or cancelling lock
3191  * \param desc unused
3192  * \param data unused
3193  * \param flag indicates whether this cancelling or blocking callback
3194  * \retval 0
3195  * \see ldlm_blocking_ast_nocheck
3196  */
3197 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3198                      void *data, int flag)
3199 {
3200         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3201         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3202         struct ldlm_cb_set_arg *arg = data;
3203         bool commit_async = false;
3204         int rc;
3205         ENTRY;
3206
3207         if (flag == LDLM_CB_CANCELING)
3208                 RETURN(0);
3209
3210         lock_res_and_lock(lock);
3211         if (lock->l_blocking_ast != mdt_blocking_ast) {
3212                 unlock_res_and_lock(lock);
3213                 RETURN(0);
3214         }
3215
3216         /* A blocking ast may be sent from ldlm_lock_decref_internal
3217          * when the last reference to a local lock was released and
3218          * during blocking event from ldlm_work_bl_ast_lock().
3219          * The 'data' parameter is l_ast_data in the first case and
3220          * callback arguments in the second one. Distinguish them by that.
3221          */
3222         if (!data || data == lock->l_ast_data || !arg->bl_desc)
3223                 goto skip_cos_checks;
3224
3225         if (lock->l_req_mode & (LCK_PW | LCK_EX)) {
3226                 if (mdt_cos_is_enabled(mdt)) {
3227                         if (!arg->bl_desc->bl_same_client)
3228                                 mdt_set_lock_sync(lock);
3229                 } else if (mdt_slc_is_enabled(mdt) &&
3230                            arg->bl_desc->bl_cos_incompat) {
3231                         mdt_set_lock_sync(lock);
3232                         /*
3233                          * we may do extra commit here, but there is a small
3234                          * window to miss a commit: lock was unlocked (saved),
3235                          * then a conflict lock queued and we come here, but
3236                          * REP-ACK not received, so lock was not converted to
3237                          * COS mode yet.
3238                          * Fortunately this window is quite small, so the
3239                          * extra commit should be rare (not to say distributed
3240                          * operation is rare too).
3241                          */
3242                         commit_async = true;
3243                 }
3244         } else if (lock->l_req_mode == LCK_COS) {
3245                 commit_async = true;
3246         }
3247
3248 skip_cos_checks:
3249         rc = ldlm_blocking_ast_nocheck(lock);
3250
3251         if (commit_async) {
3252                 struct lu_env env;
3253
3254                 rc = lu_env_init(&env, LCT_LOCAL);
3255                 if (unlikely(rc != 0))
3256                         CWARN("%s: lu_env initialization failed, cannot "
3257                               "start asynchronous commit: rc = %d\n",
3258                               obd->obd_name, rc);
3259                 else
3260                         mdt_device_commit_async(&env, mdt);
3261                 lu_env_fini(&env);
3262         }
3263         RETURN(rc);
3264 }
3265
3266 /*
3267  * Blocking AST for cross-MDT lock
3268  *
3269  * Discard lock from uncommitted_slc_locks and cancel it.
3270  *
3271  * \param lock  the lock which blocks a request or cancelling lock
3272  * \param desc  unused
3273  * \param data  unused
3274  * \param flag  indicates whether this cancelling or blocking callback
3275  * \retval      0 on success
3276  * \retval      negative number on error
3277  */
3278 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3279                             void *data, int flag)
3280 {
3281         int rc = 0;
3282         ENTRY;
3283
3284         switch (flag) {
3285         case LDLM_CB_BLOCKING: {
3286                 struct lustre_handle lockh;
3287
3288                 ldlm_lock2handle(lock, &lockh);
3289                 rc = ldlm_cli_cancel(&lockh,
3290                         ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3291                 if (rc < 0) {
3292                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3293                         RETURN(rc);
3294                 }
3295                 break;
3296         }
3297         case LDLM_CB_CANCELING: {
3298                 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3299                 struct mdt_device *mdt =
3300                                 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3301
3302                 LDLM_DEBUG(lock, "Revoke remote lock\n");
3303
3304                 /* discard slc lock here so that it can be cleaned anytime,
3305                  * especially for cleanup_resource() */
3306                 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3307
3308                 /* once we cache lock, l_ast_data is set to mdt_object */
3309                 if (lock->l_ast_data != NULL) {
3310                         struct mdt_object *mo = lock->l_ast_data;
3311                         struct lu_env env;
3312
3313                         rc = lu_env_init(&env, LCT_MD_THREAD);
3314                         if (unlikely(rc != 0)) {
3315                                 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3316                                       obd->obd_name, mo,
3317                                       PFID(mdt_object_fid(mo)), rc);
3318                                 RETURN(rc);
3319                         }
3320
3321                         if (lock->l_policy_data.l_inodebits.bits &
3322                             (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3323                                 rc = mo_invalidate(&env, mdt_object_child(mo));
3324                                 mo->mot_cache_attr = 0;
3325                         }
3326                         mdt_object_put(&env, mo);
3327                         lu_env_fini(&env);
3328                 }
3329                 break;
3330         }
3331         default:
3332                 LBUG();
3333         }
3334
3335         RETURN(rc);
3336 }
3337
3338 int mdt_check_resent_lock(struct mdt_thread_info *info,
3339                           struct mdt_object *mo,
3340                           struct mdt_lock_handle *lhc)
3341 {
3342         /* the lock might already be gotten in ldlm_handle_enqueue() */
3343         if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3344                 struct ptlrpc_request *req = mdt_info_req(info);
3345                 struct ldlm_lock      *lock;
3346
3347                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3348                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3349                 if (lock == NULL) {
3350                         /* Lock is pinned by ldlm_handle_enqueue0() as it is
3351                          * a resend case, however, it could be already destroyed
3352                          * due to client eviction or a raced cancel RPC. */
3353                         LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3354                                           lhc->mlh_reg_lh.cookie);
3355                         RETURN(-ESTALE);
3356                 }
3357
3358                 if (!fid_res_name_eq(mdt_object_fid(mo),
3359                                      &lock->l_resource->lr_name)) {
3360                         CWARN("%s: Although resent, but still not "
3361                               "get child lock:"DFID"\n",
3362                               info->mti_exp->exp_obd->obd_name,
3363                               PFID(mdt_object_fid(mo)));
3364                         LDLM_LOCK_PUT(lock);
3365                         RETURN(-EPROTO);
3366                 }
3367                 LDLM_LOCK_PUT(lock);
3368                 return 0;
3369         }
3370         return 1;
3371 }
3372
3373 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3374 {
3375         mdt_object_get(NULL, lock->l_ast_data);
3376 }
3377
3378 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3379                                struct mdt_object *o, const struct lu_fid *fid,
3380                                struct lustre_handle *lh, enum ldlm_mode mode,
3381                                __u64 *ibits, __u64 trybits, bool cache)
3382 {
3383         struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3384         union ldlm_policy_data *policy = &mti->mti_policy;
3385         struct ldlm_res_id *res_id = &mti->mti_res_id;
3386         int rc = 0;
3387         ENTRY;
3388
3389         LASSERT(mdt_object_remote(o));
3390
3391         fid_build_reg_res_name(fid, res_id);
3392
3393         memset(einfo, 0, sizeof(*einfo));
3394         einfo->ei_type = LDLM_IBITS;
3395         einfo->ei_mode = mode;
3396         einfo->ei_cb_bl = mdt_remote_blocking_ast;
3397         einfo->ei_cb_cp = ldlm_completion_ast;
3398         einfo->ei_enq_slave = 0;
3399         einfo->ei_res_id = res_id;
3400
3401         if (cache) {
3402                 /*
3403                  * if we cache lock, couple lock with mdt_object, so that object
3404                  * can be easily found in lock ASTs.
3405                  */
3406                 einfo->ei_cbdata = o;
3407                 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3408         }
3409
3410         memset(policy, 0, sizeof(*policy));
3411         policy->l_inodebits.bits = *ibits;
3412         policy->l_inodebits.try_bits = trybits;
3413
3414         rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
3415                             policy);
3416
3417         /* Return successfully acquired bits to a caller */
3418         if (rc == 0) {
3419                 struct ldlm_lock *lock = ldlm_handle2lock(lh);
3420
3421                 LASSERT(lock);
3422                 *ibits = lock->l_policy_data.l_inodebits.bits;
3423                 LDLM_LOCK_PUT(lock);
3424         }
3425         RETURN(rc);
3426 }
3427
3428 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
3429                            const struct lu_fid *fid, struct lustre_handle *lh,
3430                            enum ldlm_mode mode, __u64 ibits, bool cache)
3431 {
3432         return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
3433                                           cache);
3434 }
3435
3436 int mdt_object_local_lock(struct mdt_thread_info *info, struct mdt_object *o,
3437                           struct mdt_lock_handle *lh, __u64 *ibits,
3438                           __u64 trybits, bool cos_incompat)
3439 {
3440         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3441         union ldlm_policy_data *policy = &info->mti_policy;
3442         struct ldlm_res_id *res_id = &info->mti_res_id;
3443         __u64 dlmflags = 0, *cookie = NULL;
3444         int rc;
3445         ENTRY;
3446
3447         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3448         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3449         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3450         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3451
3452         if (cos_incompat) {
3453                 LASSERT(lh->mlh_reg_mode == LCK_PW ||
3454                         lh->mlh_reg_mode == LCK_EX);
3455                 dlmflags |= LDLM_FL_COS_INCOMPAT;
3456         } else if (mdt_cos_is_enabled(info->mti_mdt)) {
3457                 dlmflags |= LDLM_FL_COS_ENABLED;
3458         }
3459
3460         /* Only enqueue LOOKUP lock for remote object */
3461         LASSERT(ergo(mdt_object_remote(o), *ibits == MDS_INODELOCK_LOOKUP));
3462
3463         /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3464         if (lh->mlh_type == MDT_REG_LOCK && lh->mlh_reg_mode == LCK_EX &&
3465             *ibits == MDS_INODELOCK_OPEN)
3466                 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3467
3468         if (lh->mlh_type == MDT_PDO_LOCK) {
3469                 /* check for exists after object is locked */
3470                 if (mdt_object_exists(o) == 0) {
3471                         /* Non-existent object shouldn't have PDO lock */
3472                         RETURN(-ESTALE);
3473                 } else {
3474                         /* Non-dir object shouldn't have PDO lock */
3475                         if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
3476                                 RETURN(-ENOTDIR);
3477                 }
3478         }
3479
3480         fid_build_reg_res_name(mdt_object_fid(o), res_id);
3481         dlmflags |= LDLM_FL_ATOMIC_CB;
3482
3483         if (info->mti_exp)
3484                 cookie = &info->mti_exp->exp_handle.h_cookie;
3485
3486         /*
3487          * Take PDO lock on whole directory and build correct @res_id for lock
3488          * on part of directory.
3489          */
3490         if (lh->mlh_pdo_hash != 0) {
3491                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
3492                 mdt_lock_pdo_mode(info, o, lh);
3493                 if (lh->mlh_pdo_mode != LCK_NL) {
3494                         /*
3495                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
3496                          * is never going to be sent to client and we do not
3497                          * want it slowed down due to possible cancels.
3498                          */
3499                         policy->l_inodebits.bits =
3500                                 *ibits & MDS_INODELOCK_UPDATE;
3501                         policy->l_inodebits.try_bits =
3502                                 trybits & MDS_INODELOCK_UPDATE;
3503                         /* at least one of them should be set */
3504                         LASSERT(policy->l_inodebits.bits |
3505                                 policy->l_inodebits.try_bits);
3506                         rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_pdo_lh,
3507                                           lh->mlh_pdo_mode, policy, res_id,
3508                                           dlmflags, cookie);
3509                         if (unlikely(rc != 0))
3510                                 GOTO(out_unlock, rc);
3511                 }
3512
3513                 /*
3514                  * Finish res_id initializing by name hash marking part of
3515                  * directory which is taking modification.
3516                  */
3517                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3518         }
3519
3520         policy->l_inodebits.bits = *ibits;
3521         policy->l_inodebits.try_bits = trybits;
3522         policy->l_inodebits.li_gid = lh->mlh_gid;
3523
3524         /*
3525          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
3526          * going to be sent to client. If it is - mdt_intent_policy() path will
3527          * fix it up and turn FL_LOCAL flag off.
3528          */
3529         rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
3530                           policy, res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
3531                           cookie);
3532 out_unlock:
3533         if (rc != 0)
3534                 mdt_object_unlock(info, o, lh, 1);
3535         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
3536                    lh->mlh_pdo_hash != 0 &&
3537                    (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3538                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3539
3540         /* Return successfully acquired bits to a caller */
3541         if (rc == 0) {
3542                 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
3543
3544                 LASSERT(lock);
3545                 *ibits = lock->l_policy_data.l_inodebits.bits;
3546                 LDLM_LOCK_PUT(lock);
3547         }
3548         RETURN(rc);
3549 }
3550
3551 static int
3552 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
3553                          struct mdt_lock_handle *lh, __u64 *ibits,
3554                          __u64 trybits, bool cos_incompat)
3555 {
3556         struct mdt_lock_handle *local_lh = NULL;
3557         int rc;
3558         ENTRY;
3559
3560         if (!mdt_object_remote(o)) {
3561                 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
3562                                            cos_incompat);
3563                 RETURN(rc);
3564         }
3565
3566         /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
3567         *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
3568                     MDS_INODELOCK_XATTR);
3569
3570         /* Only enqueue LOOKUP lock for remote object */
3571         if (*ibits & MDS_INODELOCK_LOOKUP) {
3572                 __u64 local = MDS_INODELOCK_LOOKUP;
3573
3574                 rc = mdt_object_local_lock(info, o, lh, &local, 0,
3575                                            cos_incompat);
3576                 if (rc != ELDLM_OK)
3577                         RETURN(rc);
3578
3579                 local_lh = lh;
3580         }
3581
3582         if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
3583                 /* Sigh, PDO needs to enqueue 2 locks right now, but
3584                  * enqueue RPC can only request 1 lock, to avoid extra
3585                  * RPC, so it will instead enqueue EX lock for remote
3586                  * object anyway XXX*/
3587                 if (lh->mlh_type == MDT_PDO_LOCK &&
3588                     lh->mlh_pdo_hash != 0) {
3589                         CDEBUG(D_INFO,
3590                                "%s: "DFID" convert PDO lock to EX lock.\n",
3591                                mdt_obd_name(info->mti_mdt),
3592                                PFID(mdt_object_fid(o)));
3593                         lh->mlh_pdo_hash = 0;
3594                         lh->mlh_rreg_mode = LCK_EX;
3595                         lh->mlh_type = MDT_REG_LOCK;
3596                 }
3597
3598                 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
3599                                                 &lh->mlh_rreg_lh,
3600                                                 lh->mlh_rreg_mode,
3601                                                 ibits, trybits, false);
3602                 if (rc != ELDLM_OK) {
3603                         if (local_lh != NULL)
3604                                 mdt_object_unlock(info, o, local_lh, rc);
3605                         RETURN(rc);
3606                 }
3607         }
3608
3609         /* other components like LFSCK can use lockless access
3610          * and populate cache, so we better invalidate it */
3611         mo_invalidate(info->mti_env, mdt_object_child(o));
3612
3613         RETURN(0);
3614 }
3615
3616 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3617                     struct mdt_lock_handle *lh, __u64 ibits)
3618 {
3619         return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
3620 }
3621
3622 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3623                           struct mdt_lock_handle *lh, __u64 ibits,
3624                           bool cos_incompat)
3625 {
3626         LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
3627         return mdt_object_lock_internal(info, o, lh, &ibits, 0,
3628                                         cos_incompat);
3629 }
3630
3631 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
3632                         struct mdt_lock_handle *lh, __u64 *ibits,
3633                         __u64 trybits, bool cos_incompat)
3634 {
3635         bool trylock_only = *ibits == 0;
3636         int rc;
3637
3638         LASSERT(!(*ibits & trybits));
3639         rc = mdt_object_lock_internal(info, o, lh, ibits, trybits,
3640                                       cos_incompat);
3641         if (rc && trylock_only) { /* clear error for try ibits lock only */
3642                 LASSERT(*ibits == 0);
3643                 rc = 0;
3644         }
3645         return rc;
3646 }
3647
3648 /**
3649  * Save a lock within request object.
3650  *
3651  * Keep the lock referenced until whether client ACK or transaction
3652  * commit happens or release the lock immediately depending on input
3653  * parameters. If COS is ON, a write lock is converted to COS lock
3654  * before saving.
3655  *
3656  * \param info thead info object
3657  * \param h lock handle
3658  * \param mode lock mode
3659  * \param decref force immediate lock releasing
3660  */
3661 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
3662                    enum ldlm_mode mode, int decref)
3663 {
3664         ENTRY;
3665
3666         if (lustre_handle_is_used(h)) {
3667                 if (decref || !info->mti_has_trans ||
3668                     !(mode & (LCK_PW | LCK_EX))) {
3669                         mdt_fid_unlock(h, mode);
3670                 } else {
3671                         struct mdt_device *mdt = info->mti_mdt;
3672                         struct ldlm_lock *lock = ldlm_handle2lock(h);
3673                         struct ptlrpc_request *req = mdt_info_req(info);
3674                         bool cos = mdt_cos_is_enabled(mdt);
3675                         bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
3676
3677                         LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
3678                                  h->cookie);
3679
3680                         /* there is no request if mdt_object_unlock() is called
3681                          * from mdt_export_cleanup()->mdt_add_dirty_flag() */
3682                         if (likely(req != NULL)) {
3683                                 LDLM_DEBUG(lock, "save lock request %p reply "
3684                                         "state %p transno %lld\n", req,
3685                                         req->rq_reply_state, req->rq_transno);
3686                                 if (cos) {
3687                                         ldlm_lock_mode_downgrade(lock, LCK_COS);
3688                                         mode = LCK_COS;
3689                                 }
3690                                 if (req->rq_export->exp_disconnected)
3691                                         mdt_fid_unlock(h, mode);
3692                                 else
3693                                         ptlrpc_save_lock(req, h, mode, cos,
3694                                                          convert_lock);
3695                         } else {
3696                                 mdt_fid_unlock(h, mode);
3697                         }
3698                         if (mdt_is_lock_sync(lock)) {
3699                                 CDEBUG(D_HA, "found sync-lock,"
3700                                        " async commit started\n");
3701                                 mdt_device_commit_async(info->mti_env,
3702                                                         mdt);
3703                         }
3704                         LDLM_LOCK_PUT(lock);
3705                 }
3706                 h->cookie = 0ull;
3707         }
3708
3709         EXIT;
3710 }
3711
3712 /**
3713  * Save cross-MDT lock in uncommitted_slc_locks
3714  *
3715  * Keep the lock referenced until transaction commit happens or release the lock
3716  * immediately depending on input parameters.
3717  *
3718  * \param info thead info object
3719  * \param h lock handle
3720  * \param mode lock mode
3721  * \param decref force immediate lock releasing
3722  */
3723 static void mdt_save_remote_lock(struct mdt_thread_info *info,
3724                                  struct mdt_object *o, struct lustre_handle *h,
3725                                  enum ldlm_mode mode, int decref)
3726 {
3727         ENTRY;
3728
3729         if (lustre_handle_is_used(h)) {
3730                 struct ldlm_lock *lock = ldlm_handle2lock(h);
3731                 struct ptlrpc_request *req = mdt_info_req(info);
3732
3733                 if (o != NULL &&
3734                     (lock->l_policy_data.l_inodebits.bits &
3735                      (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
3736                         mo_invalidate(info->mti_env, mdt_object_child(o));
3737
3738                 if (decref || !info->mti_has_trans || !req ||
3739                     !(mode & (LCK_PW | LCK_EX))) {
3740                         ldlm_lock_decref_and_cancel(h, mode);
3741                         LDLM_LOCK_PUT(lock);
3742                 } else {
3743                         tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
3744                                           req->rq_transno);
3745                         ldlm_lock_decref(h, mode);
3746                 }
3747                 h->cookie = 0ull;
3748         }
3749
3750         EXIT;
3751 }
3752
3753 /**
3754  * Unlock mdt object.
3755  *
3756  * Immeditely release the regular lock and the PDO lock or save the
3757  * lock in request and keep them referenced until client ACK or
3758  * transaction commit.
3759  *
3760  * \param info thread info object
3761  * \param o mdt object
3762  * \param lh mdt lock handle referencing regular and PDO locks
3763  * \param decref force immediate lock releasing
3764  *
3765  * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
3766  */
3767 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
3768                        struct mdt_lock_handle *lh, int decref)
3769 {
3770         ENTRY;
3771
3772         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
3773         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
3774         mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
3775                              decref);
3776
3777         EXIT;
3778 }
3779
3780 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
3781                                         const struct lu_fid *f,
3782                                         struct mdt_lock_handle *lh,
3783                                         __u64 ibits)
3784 {
3785         struct mdt_object *o;
3786
3787         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
3788         if (!IS_ERR(o)) {
3789                 int rc;
3790
3791                 rc = mdt_object_lock(info, o, lh, ibits);
3792                 if (rc != 0) {
3793                         mdt_object_put(info->mti_env, o);
3794                         o = ERR_PTR(rc);
3795                 }
3796         }
3797         return o;
3798 }
3799
3800 void mdt_object_unlock_put(struct mdt_thread_info * info,
3801                            struct mdt_object * o,
3802                            struct mdt_lock_handle *lh,
3803                            int decref)
3804 {
3805         mdt_object_unlock(info, o, lh, decref);
3806         mdt_object_put(info->mti_env, o);
3807 }
3808
3809 /*
3810  * Generic code handling requests that have struct mdt_body passed in:
3811  *
3812  *  - extract mdt_body from request and save it in @info, if present;
3813  *
3814  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
3815  *  @info;
3816  *
3817  *  - if HAS_BODY flag is set for this request type check whether object
3818  *  actually exists on storage (lu_object_exists()).
3819  *
3820  */
3821 static int mdt_body_unpack(struct mdt_thread_info *info,
3822                            enum tgt_handler_flags flags)
3823 {
3824         const struct mdt_body    *body;
3825         struct mdt_object        *obj;
3826         const struct lu_env      *env;
3827         struct req_capsule       *pill;
3828         int                       rc;
3829         ENTRY;
3830
3831         env = info->mti_env;
3832         pill = info->mti_pill;
3833
3834         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3835         if (body == NULL)
3836                 RETURN(-EFAULT);
3837
3838         if (!(body->mbo_valid & OBD_MD_FLID))
3839                 RETURN(0);
3840
3841         if (!fid_is_sane(&body->mbo_fid1)) {
3842                 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3843                 RETURN(-EINVAL);
3844         }
3845
3846         obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3847         if (!IS_ERR(obj)) {
3848                 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
3849                         mdt_object_put(env, obj);
3850                         rc = -ENOENT;
3851                 } else {
3852                         info->mti_object = obj;
3853                         rc = 0;
3854                 }
3855         } else
3856                 rc = PTR_ERR(obj);
3857
3858         RETURN(rc);
3859 }
3860
3861 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
3862                                    enum tgt_handler_flags flags)
3863 {
3864         struct req_capsule *pill = info->mti_pill;
3865         int rc;
3866
3867         ENTRY;
3868
3869         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3870                 rc = mdt_body_unpack(info, flags);
3871         else
3872                 rc = 0;
3873
3874         if (rc == 0 && (flags & HAS_REPLY)) {
3875                 /* Pack reply. */
3876                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3877                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3878                                              DEF_REP_MD_SIZE);
3879                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3880                         req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3881                                              RCL_SERVER, 0);
3882
3883                 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3884                  * by default. If the target object has more ACL entries, then
3885                  * enlarge the buffer when necessary. */
3886                 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3887                         req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3888                                              LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3889
3890                 mdt_preset_secctx_size(info);
3891                 mdt_preset_encctx_size(info);
3892
3893                 rc = req_capsule_server_pack(pill);
3894                 if (rc)
3895                         CWARN("%s: cannot pack response: rc = %d\n",
3896                                       mdt_obd_name(info->mti_mdt), rc);
3897         }
3898         RETURN(rc);
3899 }
3900
3901 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3902 {
3903         lh->mlh_type = MDT_NUL_LOCK;
3904         lh->mlh_reg_lh.cookie = 0ull;
3905         lh->mlh_reg_mode = LCK_MINMODE;
3906         lh->mlh_pdo_lh.cookie = 0ull;
3907         lh->mlh_pdo_mode = LCK_MINMODE;
3908         lh->mlh_rreg_lh.cookie = 0ull;
3909         lh->mlh_rreg_mode = LCK_MINMODE;
3910 }
3911
3912 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3913 {
3914         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3915         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3916 }
3917
3918 /*
3919  * Initialize fields of struct mdt_thread_info. Other fields are left in
3920  * uninitialized state, because it's too expensive to zero out whole
3921  * mdt_thread_info (> 1K) on each request arrival.
3922  */
3923 void mdt_thread_info_init(struct ptlrpc_request *req,
3924                           struct mdt_thread_info *info)
3925 {
3926         int i;
3927
3928         info->mti_pill = &req->rq_pill;
3929
3930         /* lock handle */
3931         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3932                 mdt_lock_handle_init(&info->mti_lh[i]);
3933
3934         /* mdt device: it can be NULL while CONNECT */
3935         if (req->rq_export) {
3936                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3937                 info->mti_exp = req->rq_export;
3938         } else
3939                 info->mti_mdt = NULL;
3940         info->mti_env = req->rq_svc_thread->t_env;
3941         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3942
3943         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3944         info->mti_big_buf = LU_BUF_NULL;
3945         info->mti_body = NULL;
3946         info->mti_object = NULL;
3947         info->mti_dlm_req = NULL;
3948         info->mti_has_trans = 0;
3949         info->mti_cross_ref = 0;
3950         info->mti_opdata = 0;
3951         info->mti_big_lmm_used = 0;
3952         info->mti_big_acl_used = 0;
3953         info->mti_som_valid = 0;
3954
3955         info->mti_spec.no_create = 0;
3956         info->mti_spec.sp_rm_entry = 0;
3957         info->mti_spec.sp_permitted = 0;
3958
3959         info->mti_spec.u.sp_ea.eadata = NULL;
3960         info->mti_spec.u.sp_ea.eadatalen = 0;
3961 }
3962
3963 void mdt_thread_info_fini(struct mdt_thread_info *info)
3964 {
3965         int i;
3966
3967         if (info->mti_object != NULL) {
3968                 mdt_object_put(info->mti_env, info->mti_object);
3969                 info->mti_object = NULL;
3970         }
3971
3972         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3973                 mdt_lock_handle_fini(&info->mti_lh[i]);
3974         info->mti_env = NULL;
3975         info->mti_pill = NULL;
3976         info->mti_exp = NULL;
3977         info->mti_mdt = NULL;
3978
3979         if (unlikely(info->mti_big_buf.lb_buf != NULL))
3980                 lu_buf_free(&info->mti_big_buf);
3981 }
3982
3983 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3984 {
3985         struct mdt_thread_info  *mti;
3986
3987         mti = mdt_th_info(tsi->tsi_env);
3988         LASSERT(mti != NULL);
3989
3990         mdt_thread_info_init(tgt_ses_req(tsi), mti);
3991         if (tsi->tsi_corpus != NULL) {
3992                 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3993                 lu_object_get(tsi->tsi_corpus);
3994         }
3995         mti->mti_body = tsi->tsi_mdt_body;
3996         mti->mti_dlm_req = tsi->tsi_dlm_req;
3997
3998         return mti;
3999 }
4000
4001 static int mdt_tgt_connect(struct tgt_session_info *tsi)
4002 {
4003         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
4004             cfs_fail_val ==
4005             tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
4006                 schedule_timeout_uninterruptible(cfs_time_seconds(3));
4007
4008         return tgt_connect(tsi);
4009 }
4010
4011 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
4012                               struct mdt_thread_info *info,
4013                               struct ldlm_lock **lockp, __u64 flags)
4014 {
4015         return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4016                                    lockp, flags);
4017 }
4018 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4019                           struct mdt_thread_info *info,
4020                           struct ldlm_lock **lockp, __u64 flags)
4021 {
4022         return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4023                                lockp, flags);
4024 }
4025
4026 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4027                             struct ldlm_lock **lockp,
4028                             struct mdt_lock_handle *lh,
4029                             __u64 flags, int result)
4030 {
4031         struct ptlrpc_request  *req = mdt_info_req(info);
4032         struct ldlm_lock       *lock = *lockp;
4033         struct ldlm_lock       *new_lock;
4034
4035         /* If possible resent found a lock, @lh is set to its handle */
4036         new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4037
4038         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
4039                 lh->mlh_reg_lh.cookie = 0;
4040                 RETURN(0);
4041         }
4042
4043         if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
4044                 /* Lock is pinned by ldlm_handle_enqueue0() as it is
4045                  * a resend case, however, it could be already destroyed
4046                  * due to client eviction or a raced cancel RPC. */
4047                 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4048                                   lh->mlh_reg_lh.cookie);
4049                 lh->mlh_reg_lh.cookie = 0;
4050                 RETURN(-ESTALE);
4051         }
4052
4053         LASSERTF(new_lock != NULL,
4054                  "lockh %#llx flags %#llx : rc = %d\n",
4055                  lh->mlh_reg_lh.cookie, flags, result);
4056
4057         /*
4058          * If we've already given this lock to a client once, then we should
4059          * have no readers or writers.  Otherwise, we should have one reader
4060          * _or_ writer ref (which will be zeroed below) before returning the
4061          * lock to a client.
4062          */
4063         if (new_lock->l_export == req->rq_export) {
4064                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4065         } else {
4066                 LASSERT(new_lock->l_export == NULL);
4067                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4068         }
4069
4070         *lockp = new_lock;
4071
4072         if (new_lock->l_export == req->rq_export) {
4073                 /*
4074                  * Already gave this to the client, which means that we
4075                  * reconstructed a reply.
4076                  */
4077                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4078                         MSG_RESENT);
4079
4080                 LDLM_LOCK_RELEASE(new_lock);
4081                 lh->mlh_reg_lh.cookie = 0;
4082                 RETURN(ELDLM_LOCK_REPLACED);
4083         }
4084
4085         /*
4086          * Fixup the lock to be given to the client.
4087          */
4088         lock_res_and_lock(new_lock);
4089         /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4090          * possible blocking AST. */
4091         while (new_lock->l_readers > 0) {
4092                 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4093                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4094                 new_lock->l_readers--;
4095         }
4096         while (new_lock->l_writers > 0) {
4097                 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4098                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4099                 new_lock->l_writers--;
4100         }
4101
4102         new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4103         new_lock->l_blocking_ast = lock->l_blocking_ast;
4104         new_lock->l_completion_ast = lock->l_completion_ast;
4105         if (ldlm_has_dom(new_lock))
4106                 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4107         new_lock->l_remote_handle = lock->l_remote_handle;
4108         new_lock->l_flags &= ~LDLM_FL_LOCAL;
4109
4110         unlock_res_and_lock(new_lock);
4111
4112         cfs_hash_add(new_lock->l_export->exp_lock_hash,
4113                      &new_lock->l_remote_handle,
4114                      &new_lock->l_exp_hash);
4115
4116         LDLM_LOCK_RELEASE(new_lock);
4117         lh->mlh_reg_lh.cookie = 0;
4118
4119         RETURN(ELDLM_LOCK_REPLACED);
4120 }
4121
4122 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4123                              struct ldlm_lock *new_lock,
4124                              struct mdt_lock_handle *lh, __u64 flags)
4125 {
4126         struct ptlrpc_request  *req = mdt_info_req(info);
4127         struct ldlm_request    *dlmreq;
4128
4129         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4130                 return;
4131
4132         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4133
4134         /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
4135          * lock was found by ldlm_handle_enqueue(); if so @lh must be
4136          * initialized. */
4137         if (flags & LDLM_FL_RESENT) {
4138                 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4139                 lh->mlh_reg_mode = new_lock->l_granted_mode;
4140
4141                 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4142                 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4143                           lh->mlh_reg_lh.cookie);
4144                 return;
4145         }
4146
4147         /*
4148          * If the xid matches, then we know this is a resent request, and allow
4149          * it. (It's probably an OPEN, for which we don't send a lock.
4150          */
4151         if (req_can_reconstruct(req, NULL) != 0)
4152                 return;
4153
4154         /*
4155          * This remote handle isn't enqueued, so we never received or processed
4156          * this request.  Clear MSG_RESENT, because it can be handled like any
4157          * normal request now.
4158          */
4159         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4160
4161         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4162                   dlmreq->lock_handle[0].cookie);
4163 }
4164
4165 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4166                                struct mdt_thread_info *info,
4167                                struct ldlm_lock **lockp,
4168                                __u64 flags)
4169 {
4170         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4171         struct ldlm_reply      *ldlm_rep = NULL;
4172         int rc;
4173         ENTRY;
4174
4175         /*
4176          * Initialize lhc->mlh_reg_lh either from a previously granted lock
4177          * (for the resend case) or a new lock. Below we will use it to
4178          * replace the original lock.
4179          */
4180         mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4181         if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4182                 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
4183                 rc = mdt_object_lock(info, info->mti_object, lhc,
4184                                      MDS_INODELOCK_XATTR);
4185                 if (rc)
4186                         return rc;
4187         }
4188
4189         rc = mdt_getxattr(info);
4190
4191         if (mdt_info_req(info)->rq_repmsg != NULL)
4192                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4193
4194         if (ldlm_rep == NULL ||
4195             OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4196                 mdt_object_unlock(info,  info->mti_object, lhc, 1);
4197                 if (is_serious(rc))
4198                         RETURN(rc);
4199                 else
4200                         RETURN(err_serious(-EFAULT));
4201         }
4202
4203         ldlm_rep->lock_policy_res2 = clear_serious(rc);
4204
4205         /* This is left for interop instead of adding a new interop flag.
4206          * LU-7433 */
4207 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4208         if (ldlm_rep->lock_policy_res2) {
4209                 mdt_object_unlock(info, info->mti_object, lhc, 1);
4210                 RETURN(ELDLM_LOCK_ABORTED);
4211         }
4212 #endif
4213
4214         rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4215         RETURN(rc);
4216 }
4217
4218 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4219                               struct mdt_thread_info *info,
4220                               struct ldlm_lock **lockp,
4221                               __u64 flags)
4222 {
4223         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4224         __u64                   child_bits;
4225         struct ldlm_reply      *ldlm_rep;
4226         struct mdt_body        *reqbody;
4227         struct mdt_body        *repbody;
4228         int                     rc, rc2;
4229         ENTRY;
4230
4231         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4232         LASSERT(reqbody);
4233
4234         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4235         LASSERT(repbody);
4236
4237         info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4238         repbody->mbo_eadatasize = 0;
4239         repbody->mbo_aclsize = 0;
4240
4241         switch (it_opc) {
4242         case IT_LOOKUP:
4243                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4244                 break;
4245         case IT_GETATTR:
4246                 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4247                              MDS_INODELOCK_PERM;
4248                 break;
4249         default:
4250                 CERROR("%s: unsupported intent %#x\n",
4251                        mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4252                 GOTO(out_shrink, rc = -EINVAL);
4253         }
4254
4255         rc = mdt_init_ucred(info, reqbody);
4256         if (rc)
4257                 GOTO(out_shrink, rc);
4258
4259         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4260         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4261
4262         /* Get lock from request for possible resent case. */
4263         mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4264
4265         rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4266         ldlm_rep->lock_policy_res2 = clear_serious(rc);
4267
4268         if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
4269                 ldlm_rep->lock_policy_res2 = 0;
4270         if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4271             ldlm_rep->lock_policy_res2) {
4272                 lhc->mlh_reg_lh.cookie = 0ull;
4273                 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4274         }
4275
4276         rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4277         EXIT;
4278 out_ucred:
4279         mdt_exit_ucred(info);
4280 out_shrink:
4281         mdt_client_compatibility(info);
4282         rc2 = mdt_fix_reply(info);
4283         if (rc == 0)
4284                 rc = rc2;
4285         return rc;
4286 }
4287
4288 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4289                              struct mdt_thread_info *info,
4290                              struct ldlm_lock **lockp,
4291                              __u64 flags)
4292 {
4293         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4294         struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4295         struct layout_intent *intent;
4296         struct ldlm_reply *ldlm_rep;
4297         struct lu_fid *fid = &info->mti_tmp_fid2;
4298         struct mdt_object *obj = NULL;
4299         int layout_size = 0;
4300         struct lu_buf *buf = &layout.mlc_buf;
4301         int rc = 0;
4302
4303         ENTRY;
4304
4305         fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4306
4307         intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4308         if (intent == NULL)
4309                 RETURN(-EPROTO);
4310
4311         CDEBUG(D_INFO, DFID "got layout change request from client: "
4312                "opc:%u flags:%#x extent "DEXT"\n",
4313                PFID(fid), intent->li_opc, intent->li_flags,
4314                PEXT(&intent->li_extent));
4315
4316         switch (intent->li_opc) {
4317         case LAYOUT_INTENT_TRUNC:
4318         case LAYOUT_INTENT_WRITE:
4319                 layout.mlc_opc = MD_LAYOUT_WRITE;
4320                 layout.mlc_intent = intent;
4321                 break;
4322         case LAYOUT_INTENT_ACCESS:
4323                 break;
4324         case LAYOUT_INTENT_READ:
4325         case LAYOUT_INTENT_GLIMPSE:
4326         case LAYOUT_INTENT_RELEASE:
4327         case LAYOUT_INTENT_RESTORE:
4328                 CERROR("%s: Unsupported layout intent opc %d\n",
4329                        mdt_obd_name(info->mti_mdt), intent->li_opc);
4330                 RETURN(-ENOTSUPP);
4331         default:
4332                 CERROR("%s: Unknown layout intent opc %d\n",
4333                        mdt_obd_name(info->mti_mdt), intent->li_opc);
4334                 RETURN(-EINVAL);
4335         }
4336
4337         obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4338         if (IS_ERR(obj))
4339                 RETURN(PTR_ERR(obj));
4340
4341         if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4342                 /* if layout is going to be changed don't use the current EA
4343                  * size but the maximum one. That buffer will be shrinked
4344                  * to the actual size in req_capsule_shrink() before reply.
4345                  */
4346                 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4347                         layout_size = info->mti_mdt->mdt_max_mdsize;
4348                 } else {
4349                         layout_size = mdt_attr_get_eabuf_size(info, obj);
4350                         if (layout_size < 0)
4351                                 GOTO(out, rc = layout_size);
4352
4353                         if (layout_size > info->mti_mdt->mdt_max_mdsize)
4354                                 info->mti_mdt->mdt_max_mdsize = layout_size;
4355                 }
4356                 CDEBUG(D_INFO, "%s: layout_size %d\n",
4357                        mdt_obd_name(info->mti_mdt), layout_size);
4358         }
4359
4360         /*
4361          * set reply buffer size, so that ldlm_handle_enqueue0()->
4362          * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4363          */
4364         req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4365                              layout_size);
4366         rc = req_capsule_server_pack(info->mti_pill);
4367         if (rc)
4368                 GOTO(out, rc);
4369
4370         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4371         if (!ldlm_rep)
4372                 GOTO(out, rc = -EPROTO);
4373
4374         mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4375
4376         /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4377         if (layout.mlc_opc == MD_LAYOUT_NOP)
4378                 GOTO(out, rc = 0);
4379
4380         rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4381         if (rc < 0)
4382                 GOTO(out, rc);
4383         if (rc == 1) {
4384                 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4385                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4386                 GOTO(out, rc);
4387         }
4388
4389         buf->lb_buf = NULL;
4390         buf->lb_len = 0;
4391         if (unlikely(req_is_replay(mdt_info_req(info)))) {
4392                 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4393                                                      &RMF_EADATA);
4394                 buf->lb_len = req_capsule_get_size(info->mti_pill,
4395                                                      &RMF_EADATA, RCL_CLIENT);
4396                 /*
4397                  * If it's a replay of layout write intent RPC, the client has
4398                  * saved the extended lovea when it get reply then.
4399                  */
4400                 if (buf->lb_len > 0)
4401                         mdt_fix_lov_magic(info, buf->lb_buf);
4402         }
4403
4404         /* Get lock from request for possible resent case. */
4405         mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4406         (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4407
4408         /*
4409          * Instantiate some layout components, if @buf contains lovea, then it's
4410          * a replay of the layout intent write RPC.
4411          */
4412         rc = mdt_layout_change(info, obj, lhc, &layout);
4413         ldlm_rep->lock_policy_res2 = clear_serious(rc);
4414
4415         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4416                 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4417                 if (rc == ELDLM_LOCK_REPLACED &&
4418                     (*lockp)->l_granted_mode == LCK_EX)
4419                         ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4420         }
4421
4422         EXIT;
4423 out:
4424         mdt_object_put(info->mti_env, obj);
4425         return rc;
4426 }
4427
4428 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4429                            struct mdt_thread_info *info,
4430                            struct ldlm_lock **lockp,
4431                            __u64 flags)
4432 {
4433         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4434         struct ldlm_reply      *rep = NULL;
4435         long                    opc;
4436         int                     rc;
4437         struct ptlrpc_request  *req = mdt_info_req(info);
4438
4439         static const struct req_format *intent_fmts[REINT_MAX] = {
4440                 [REINT_CREATE]  = &RQF_LDLM_INTENT_CREATE,
4441                 [REINT_OPEN]    = &RQF_LDLM_INTENT_OPEN
4442         };
4443
4444         ENTRY;
4445
4446         opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4447         if (opc < 0)
4448                 RETURN(opc);
4449
4450         /* Get lock from request for possible resent case. */
4451         mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4452
4453         rc = mdt_reint_internal(info, lhc, opc);
4454
4455         if (rc < 0 && lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
4456                 DEBUG_REQ(D_ERROR, req, "Replay open failed with %d", rc);
4457
4458         /* Check whether the reply has been packed successfully. */
4459         if (mdt_info_req(info)->rq_repmsg != NULL)
4460                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4461         if (rep == NULL) {
4462                 if (is_serious(rc))
4463                         RETURN(rc);
4464                 else
4465                         RETURN(err_serious(-EFAULT));
4466         }
4467
4468         /* MDC expects this in any case */
4469         if (rc != 0)
4470                 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4471
4472         /* the open lock or the lock for cross-ref object should be
4473          * returned to the client */
4474         if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
4475             (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
4476                 rep->lock_policy_res2 = 0;
4477                 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4478                 RETURN(rc);
4479         }
4480
4481         rep->lock_policy_res2 = clear_serious(rc);
4482
4483         if (rep->lock_policy_res2 == -ENOENT &&
4484             mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
4485             !mdt_get_disposition(rep, DISP_OPEN_CREATE))
4486                 rep->lock_policy_res2 = 0;
4487
4488         lhc->mlh_reg_lh.cookie = 0ull;
4489         if (rc == -ENOTCONN || rc == -ENODEV ||
4490             rc == -EOVERFLOW) { /**< if VBR failure then return error */
4491                 /*
4492                  * If it is the disconnect error (ENODEV & ENOCONN), the error
4493                  * will be returned by rq_status, and client at ptlrpc layer
4494                  * will detect this, then disconnect, reconnect the import
4495                  * immediately, instead of impacting the following the rpc.
4496                  */
4497                 RETURN(rc);
4498         }
4499         /*
4500          * For other cases, the error will be returned by intent, and client
4501          * will retrieve the result from intent.
4502          */
4503         RETURN(ELDLM_LOCK_ABORTED);
4504 }
4505
4506 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
4507                           struct mdt_thread_info *info,
4508                           struct ldlm_lock **lockp,
4509                           u64 flags /* LDLM_FL_* */)
4510 {
4511         struct req_capsule *pill = info->mti_pill;
4512         struct ptlrpc_request *req = mdt_info_req(info);
4513         const struct req_format *it_format;
4514         int (*it_handler)(enum ldlm_intent_flags,
4515                           struct mdt_thread_info *,
4516                           struct ldlm_lock **,
4517                           u64);
4518         enum tgt_handler_flags it_handler_flags = 0;
4519         struct ldlm_reply *rep;
4520         bool check_mdt_object = false;
4521         int rc;
4522         ENTRY;
4523
4524         switch (it_opc) {
4525         case IT_OPEN:
4526         case IT_OPEN|IT_CREAT:
4527                 /*
4528                  * OCREAT is not a IS_MUTABLE request since the file may
4529                  * already exist. We do the extra check of
4530                  * OBD_CONNECT_RDONLY in mdt_reint_open() when we
4531                  * really need to create the object.
4532                  */
4533                 it_format = &RQF_LDLM_INTENT;
4534                 it_handler = &mdt_intent_open;
4535                 break;
4536         case IT_GETATTR:
4537                 check_mdt_object = true;
4538                 /* fallthrough */
4539         case IT_LOOKUP:
4540                 it_format = &RQF_LDLM_INTENT_GETATTR;
4541                 it_handler = &mdt_intent_getattr;
4542                 it_handler_flags = HAS_REPLY;
4543                 break;
4544         case IT_GETXATTR:
4545                 check_mdt_object = true;
4546                 it_format = &RQF_LDLM_INTENT_GETXATTR;
4547                 it_handler = &mdt_intent_getxattr;
4548                 it_handler_flags = HAS_BODY;
4549                 break;
4550         case IT_LAYOUT:
4551                 it_format = &RQF_LDLM_INTENT_LAYOUT;
4552                 it_handler = &mdt_intent_layout;
4553                 break;
4554         case IT_GLIMPSE:
4555                 it_format = &RQF_LDLM_INTENT;
4556                 it_handler = &mdt_intent_glimpse;
4557                 break;
4558         case IT_BRW:
4559                 it_format = &RQF_LDLM_INTENT;
4560                 it_handler = &mdt_intent_brw;
4561                 break;
4562         case IT_QUOTA_DQACQ:
4563         case IT_QUOTA_CONN: {
4564                 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4565
4566                 if (qmt == NULL)
4567                         RETURN(-EOPNOTSUPP);
4568
4569                 if (mdt_rdonly(req->rq_export))
4570                         RETURN(-EROFS);
4571
4572                 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4573                 /* pass the request to quota master */
4574                 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4575                                                  mdt_info_req(info), lockp,
4576                                                  flags);
4577                 RETURN(rc);
4578         }
4579         default:
4580                 CERROR("%s: unknown intent code %#x\n",
4581                        mdt_obd_name(info->mti_mdt), it_opc);
4582                 RETURN(-EPROTO);
4583         }
4584
4585         req_capsule_extend(pill, it_format);
4586
4587         rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
4588         if (rc < 0)
4589                 RETURN(rc);
4590
4591         if (unlikely(info->mti_object == NULL && check_mdt_object))
4592                 RETURN(-EPROTO);
4593
4594         if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
4595                 RETURN(-EROFS);
4596
4597         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
4598
4599         /* execute policy */
4600         rc = (*it_handler)(it_opc, info, lockp, flags);
4601
4602         /* Check whether the reply has been packed successfully. */
4603         if (req->rq_repmsg != NULL) {
4604                 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4605                 rep->lock_policy_res2 =
4606                         ptlrpc_status_hton(rep->lock_policy_res2);
4607         }
4608
4609         RETURN(rc);
4610 }
4611
4612 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
4613                                     enum ldlm_intent_flags it_opc)
4614 {
4615         struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
4616
4617         /* update stats when IT code is known */
4618         if (srv_stats != NULL)
4619                 lprocfs_counter_incr(srv_stats,
4620                                 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
4621                                 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
4622 }
4623
4624 static int mdt_intent_policy(const struct lu_env *env,
4625                              struct ldlm_namespace *ns,
4626                              struct ldlm_lock **lockp,
4627                              void *req_cookie,
4628                              enum ldlm_mode mode,
4629                              __u64 flags, void *data)
4630 {
4631         struct tgt_session_info *tsi;
4632         struct mdt_thread_info  *info;
4633         struct ptlrpc_request   *req  =  req_cookie;
4634         struct ldlm_intent      *it;
4635         struct req_capsule      *pill;
4636         const struct ldlm_lock_desc *ldesc;
4637         int rc;
4638
4639         ENTRY;
4640
4641         LASSERT(req != NULL);
4642
4643         tsi = tgt_ses_info(env);
4644
4645         info = tsi2mdt_info(tsi);
4646         LASSERT(info != NULL);
4647         pill = info->mti_pill;
4648         LASSERT(pill->rc_req == req);
4649         ldesc = &info->mti_dlm_req->lock_desc;
4650
4651         if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4652                 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4653                 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4654                 if (it != NULL) {
4655                         mdt_ptlrpc_stats_update(req, it->opc);
4656                         rc = mdt_intent_opc(it->opc, info, lockp, flags);
4657                         if (rc == 0)
4658                                 rc = ELDLM_OK;
4659
4660                         /* Lock without inodebits makes no sense and will oops
4661                          * later in ldlm. Let's check it now to see if we have
4662                          * ibits corrupted somewhere in mdt_intent_opc().
4663                          * The case for client miss to set ibits has been
4664                          * processed by others. */
4665                         LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
4666                                 ldesc->l_policy_data.l_inodebits.bits != 0));
4667                 } else {
4668                         rc = err_serious(-EFAULT);
4669                 }
4670         } else if (ldesc->l_resource.lr_type == LDLM_IBITS &&
4671                    ldesc->l_policy_data.l_inodebits.bits == MDS_INODELOCK_DOM) {
4672                 struct ldlm_reply *rep;
4673
4674                 /* No intent was provided but INTENT flag is set along with
4675                  * DOM bit, this is considered as GLIMPSE request.
4676                  * This logic is common for MDT and OST glimpse
4677                  */
4678                 mdt_ptlrpc_stats_update(req, IT_GLIMPSE);
4679                 rc = mdt_glimpse_enqueue(info, ns, lockp, flags);
4680                 /* Check whether the reply has been packed successfully. */
4681                 if (req->rq_repmsg != NULL) {
4682                         rep = req_capsule_server_get(info->mti_pill,
4683                                                      &RMF_DLM_REP);
4684                         rep->lock_policy_res2 =
4685                                 ptlrpc_status_hton(rep->lock_policy_res2);
4686                 }
4687         } else {
4688                 /* No intent was provided */
4689                 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4690                 rc = req_capsule_server_pack(pill);
4691                 if (rc)
4692                         rc = err_serious(rc);
4693         }
4694         mdt_thread_info_fini(info);
4695         RETURN(rc);
4696 }
4697
4698 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
4699 {
4700         struct seq_server_site  *ss = mdt_seq_site(mdt);
4701
4702         if (ss->ss_node_id == 0)
4703                 return;
4704
4705         if (ss->ss_client_seq != NULL) {
4706                 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4707                 ss->ss_client_seq->lcs_exp = NULL;
4708         }
4709
4710         if (ss->ss_server_fld != NULL) {
4711                 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
4712                 ss->ss_server_fld->lsf_control_exp = NULL;
4713         }
4714 }
4715
4716 static void mdt_seq_fini_cli(struct mdt_device *mdt)
4717 {
4718         struct seq_server_site *ss = mdt_seq_site(mdt);
4719
4720         if (ss == NULL)
4721                 return;
4722
4723         if (ss->ss_server_seq != NULL)
4724                 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
4725 }
4726
4727 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
4728 {
4729         mdt_seq_fini_cli(mdt);
4730         mdt_deregister_seq_exp(mdt);
4731
4732         return seq_site_fini(env, mdt_seq_site(mdt));
4733 }
4734
4735 /**
4736  * It will retrieve its FLDB entries from MDT0, and it only happens
4737  * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
4738  * and it needs to refresh FLDB from the MDT0.
4739  **/
4740 static int mdt_register_lwp_callback(void *data)
4741 {
4742         struct lu_env           env;
4743         struct mdt_device       *mdt = data;
4744         struct lu_server_fld    *fld = mdt_seq_site(mdt)->ss_server_fld;
4745         int                     rc;
4746         ENTRY;
4747
4748         LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
4749
4750         rc = lu_env_init(&env, LCT_MD_THREAD);
4751         if (rc < 0) {
4752                 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
4753                 RETURN(rc);
4754         }
4755
4756         /* Allocate new sequence now to avoid creating local transaction
4757          * in the normal transaction process */
4758         rc = seq_server_check_and_alloc_super(&env,
4759                                               mdt_seq_site(mdt)->ss_server_seq);
4760         if (rc < 0)
4761                 GOTO(out, rc);
4762
4763         if (fld->lsf_new) {
4764                 rc = fld_update_from_controller(&env, fld);
4765                 if (rc != 0) {
4766                         CERROR("%s: cannot update controller: rc = %d\n",
4767                                mdt_obd_name(mdt), rc);
4768                         GOTO(out, rc);
4769                 }
4770         }
4771 out:
4772         lu_env_fini(&env);
4773         RETURN(rc);
4774 }
4775
4776 static int mdt_register_seq_exp(struct mdt_device *mdt)
4777 {
4778         struct seq_server_site  *ss = mdt_seq_site(mdt);
4779         char                    *lwp_name = NULL;
4780         int                     rc;
4781
4782         if (ss->ss_node_id == 0)
4783                 return 0;
4784
4785         OBD_ALLOC(lwp_name, MAX_OBD_NAME);
4786         if (lwp_name == NULL)
4787                 GOTO(out_free, rc = -ENOMEM);
4788
4789         rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
4790         if (rc != 0)
4791                 GOTO(out_free, rc);
4792
4793         rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
4794                                       NULL, NULL);
4795         if (rc != 0)
4796                 GOTO(out_free, rc);
4797
4798         rc = lustre_register_lwp_item(lwp_name,
4799                                       &ss->ss_server_fld->lsf_control_exp,
4800                                       mdt_register_lwp_callback, mdt);
4801         if (rc != 0) {
4802                 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4803                 ss->ss_client_seq->lcs_exp = NULL;
4804                 GOTO(out_free, rc);
4805         }
4806 out_free:
4807         if (lwp_name != NULL)
4808                 OBD_FREE(lwp_name, MAX_OBD_NAME);
4809
4810         return rc;
4811 }
4812
4813 /*
4814  * Init client sequence manager which is used by local MDS to talk to sequence
4815  * controller on remote node.
4816  */
4817 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
4818 {
4819         struct seq_server_site *ss = mdt_seq_site(mdt);
4820         char *prefix;
4821         ENTRY;
4822
4823         /* check if this is adding the first MDC and controller is not yet
4824          * initialized. */
4825         OBD_ALLOC_PTR(ss->ss_client_seq);
4826         if (ss->ss_client_seq == NULL)
4827                 RETURN(-ENOMEM);
4828
4829         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4830         if (prefix == NULL) {
4831                 OBD_FREE_PTR(ss->ss_client_seq);
4832                 ss->ss_client_seq = NULL;
4833                 RETURN(-ENOMEM);
4834         }
4835
4836         /* Note: seq_client_fini will be called in seq_site_fini */
4837         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
4838         seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
4839                         prefix, ss->ss_node_id == 0 ?  ss->ss_control_seq :
4840                                                             NULL);
4841         OBD_FREE(prefix, MAX_OBD_NAME + 5);
4842
4843         RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
4844 }
4845
4846 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4847 {
4848         struct seq_server_site  *ss;
4849         int                     rc;
4850         ENTRY;
4851
4852         ss = mdt_seq_site(mdt);
4853         /* init sequence controller server(MDT0) */
4854         if (ss->ss_node_id == 0) {
4855                 OBD_ALLOC_PTR(ss->ss_control_seq);
4856                 if (ss->ss_control_seq == NULL)
4857                         RETURN(-ENOMEM);
4858
4859                 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4860                                      mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4861                                      ss);
4862                 if (rc)
4863                         GOTO(out_seq_fini, rc);
4864         }
4865
4866         /* Init normal sequence server */
4867         OBD_ALLOC_PTR(ss->ss_server_seq);
4868         if (ss->ss_server_seq == NULL)
4869                 GOTO(out_seq_fini, rc = -ENOMEM);
4870
4871         rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4872                              mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4873         if (rc)
4874                 GOTO(out_seq_fini, rc);
4875
4876         /* init seq client for seq server to talk to seq controller(MDT0) */
4877         rc = mdt_seq_init_cli(env, mdt);
4878         if (rc != 0)
4879                 GOTO(out_seq_fini, rc);
4880
4881         if (ss->ss_node_id != 0)
4882                 /* register controller export through lwp */
4883                 rc = mdt_register_seq_exp(mdt);
4884
4885         EXIT;
4886 out_seq_fini:
4887         if (rc)
4888                 mdt_seq_fini(env, mdt);
4889
4890         return rc;
4891 }
4892
4893 /*
4894  * FLD wrappers
4895  */
4896 static int mdt_fld_fini(const struct lu_env *env,
4897                         struct mdt_device *m)
4898 {
4899         struct seq_server_site *ss = mdt_seq_site(m);
4900         ENTRY;
4901
4902         if (ss && ss->ss_server_fld) {
4903                 fld_server_fini(env, ss->ss_server_fld);
4904                 OBD_FREE_PTR(ss->ss_server_fld);
4905                 ss->ss_server_fld = NULL;
4906         }
4907
4908         RETURN(0);
4909 }
4910
4911 static int mdt_fld_init(const struct lu_env *env,
4912                         const char *uuid,
4913                         struct mdt_device *m)
4914 {
4915         struct seq_server_site *ss;
4916         int rc;
4917         ENTRY;
4918
4919         ss = mdt_seq_site(m);
4920
4921         OBD_ALLOC_PTR(ss->ss_server_fld);
4922         if (ss->ss_server_fld == NULL)
4923                 RETURN(rc = -ENOMEM);
4924
4925         rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4926                              LU_SEQ_RANGE_MDT);
4927         if (rc) {
4928                 OBD_FREE_PTR(ss->ss_server_fld);
4929                 ss->ss_server_fld = NULL;
4930                 RETURN(rc);
4931         }
4932
4933         RETURN(0);
4934 }
4935
4936 static void mdt_stack_pre_fini(const struct lu_env *env,
4937                            struct mdt_device *m, struct lu_device *top)
4938 {
4939         struct lustre_cfg_bufs  *bufs;
4940         struct lustre_cfg       *lcfg;
4941         struct mdt_thread_info  *info;
4942         ENTRY;
4943
4944         LASSERT(top);
4945
4946         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4947         LASSERT(info != NULL);
4948
4949         bufs = &info->mti_u.bufs;
4950
4951         LASSERT(m->mdt_child_exp);
4952         LASSERT(m->mdt_child_exp->exp_obd);
4953
4954         /* process cleanup, pass mdt obd name to get obd umount flags */
4955         /* XXX: this is needed because all layers are referenced by
4956          * objects (some of them are pinned by osd, for example *
4957          * the proper solution should be a model where object used
4958          * by osd only doesn't have mdt/mdd slices -bzzz */
4959         lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4960         lustre_cfg_bufs_set_string(bufs, 1, NULL);
4961         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4962         if (!lcfg)
4963                 RETURN_EXIT;
4964         lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
4965
4966         top->ld_ops->ldo_process_config(env, top, lcfg);
4967         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4968         EXIT;
4969 }
4970
4971 static void mdt_stack_fini(const struct lu_env *env,
4972                            struct mdt_device *m, struct lu_device *top)
4973 {
4974         struct obd_device       *obd = mdt2obd_dev(m);
4975         struct lustre_cfg_bufs  *bufs;
4976         struct lustre_cfg       *lcfg;
4977         struct mdt_thread_info  *info;
4978         char                     flags[3] = "";
4979         ENTRY;
4980
4981         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4982         LASSERT(info != NULL);
4983
4984         lu_dev_del_linkage(top->ld_site, top);
4985
4986         lu_site_purge(env, top->ld_site, -1);
4987
4988         bufs = &info->mti_u.bufs;
4989         /* process cleanup, pass mdt obd name to get obd umount flags */
4990         /* another purpose is to let all layers to release their objects */
4991         lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4992         if (obd->obd_force)
4993                 strcat(flags, "F");
4994         if (obd->obd_fail)
4995                 strcat(flags, "A");
4996         lustre_cfg_bufs_set_string(bufs, 1, flags);
4997         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4998         if (!lcfg)
4999                 RETURN_EXIT;
5000         lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
5001
5002         LASSERT(top);
5003         top->ld_ops->ldo_process_config(env, top, lcfg);
5004         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5005
5006         lu_site_purge(env, top->ld_site, -1);
5007
5008         m->mdt_child = NULL;
5009         m->mdt_bottom = NULL;
5010
5011         obd_disconnect(m->mdt_child_exp);
5012         m->mdt_child_exp = NULL;
5013
5014         obd_disconnect(m->mdt_bottom_exp);
5015         m->mdt_child_exp = NULL;
5016 }
5017
5018 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
5019                                const char *next, struct obd_export **exp)
5020 {
5021         struct obd_connect_data *data = NULL;
5022         struct obd_device       *obd;
5023         int                      rc;
5024         ENTRY;
5025
5026         OBD_ALLOC_PTR(data);
5027         if (data == NULL)
5028                 GOTO(out, rc = -ENOMEM);
5029
5030         obd = class_name2obd(next);
5031         if (obd == NULL) {
5032                 CERROR("%s: can't locate next device: %s\n",
5033                        mdt_obd_name(m), next);
5034                 GOTO(out, rc = -ENOTCONN);
5035         }
5036
5037         data->ocd_connect_flags = OBD_CONNECT_VERSION;
5038         data->ocd_version = LUSTRE_VERSION_CODE;
5039
5040         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5041         if (rc) {
5042                 CERROR("%s: cannot connect to next dev %s (%d)\n",
5043                        mdt_obd_name(m), next, rc);
5044                 GOTO(out, rc);
5045         }
5046
5047 out:
5048         if (data)
5049                 OBD_FREE_PTR(data);
5050         RETURN(rc);
5051 }
5052
5053 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5054                           struct lustre_cfg *cfg)
5055 {
5056         char                   *dev = lustre_cfg_string(cfg, 0);
5057         int                     rc, name_size, uuid_size;
5058         char                   *name, *uuid, *p;
5059         struct lustre_cfg_bufs *bufs;
5060         struct lustre_cfg      *lcfg;
5061         struct obd_device      *obd;
5062         struct lustre_profile  *lprof;
5063         struct lu_site         *site;
5064         ENTRY;
5065
5066         /* in 1.8 we had the only device in the stack - MDS.
5067          * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5068          * in 2.3 OSD is instantiated by obd_mount.c, so we need
5069          * to generate names and setup MDT, MDD. MDT will be using
5070          * generated name to connect to MDD. for MDD the next device
5071          * will be LOD with name taken from so called "profile" which
5072          * is generated by mount_option line
5073          *
5074          * 1.8 MGS generates config. commands like this:
5075          *   #06 (104)mount_option 0:  1:lustre-MDT0000  2:lustre-mdtlov
5076          *   #08 (120)setup   0:lustre-MDT0000  1:dev 2:type 3:lustre-MDT0000
5077          * 2.0 MGS generates config. commands like this:
5078          *   #07 (112)mount_option 0:  1:lustre-MDT0000  2:lustre-MDT0000-mdtlov
5079          *   #08 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
5080          *                    3:lustre-MDT0000-mdtlov  4:f
5081          *
5082          * we generate MDD name from MDT one, just replacing T with D
5083          *
5084          * after all the preparations, the logical equivalent will be
5085          *   #01 (160)setup   0:lustre-MDD0000  1:lustre-MDD0000_UUID  2:0
5086          *                    3:lustre-MDT0000-mdtlov  4:f
5087          *   #02 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
5088          *                    3:lustre-MDD0000  4:f
5089          *
5090          *  notice we build the stack from down to top: MDD first, then MDT */
5091
5092         name_size = MAX_OBD_NAME;
5093         uuid_size = MAX_OBD_NAME;
5094
5095         OBD_ALLOC(name, name_size);
5096         OBD_ALLOC(uuid, uuid_size);
5097         if (name == NULL || uuid == NULL)
5098                 GOTO(cleanup_mem, rc = -ENOMEM);
5099
5100         OBD_ALLOC_PTR(bufs);
5101         if (!bufs)
5102                 GOTO(cleanup_mem, rc = -ENOMEM);
5103
5104         strcpy(name, dev);
5105         p = strstr(name, "-MDT");
5106         if (p == NULL)
5107                 GOTO(free_bufs, rc = -ENOMEM);
5108         p[3] = 'D';
5109
5110         snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5111
5112         lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5113         if (lprof == NULL || lprof->lp_dt == NULL) {
5114                 CERROR("can't find the profile: %s\n",
5115                        lustre_cfg_string(cfg, 0));
5116                 GOTO(free_bufs, rc = -EINVAL);
5117         }
5118
5119         lustre_cfg_bufs_reset(bufs, name);
5120         lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5121         lustre_cfg_bufs_set_string(bufs, 2, uuid);
5122         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5123
5124         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5125         if (!lcfg)
5126                 GOTO(put_profile, rc = -ENOMEM);
5127         lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5128
5129         rc = class_attach(lcfg);
5130         if (rc)
5131                 GOTO(lcfg_cleanup, rc);
5132
5133         obd = class_name2obd(name);
5134         if (!obd) {
5135                 CERROR("Can not find obd %s (%s in config)\n",
5136                        MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5137                 GOTO(lcfg_cleanup, rc = -EINVAL);
5138         }
5139
5140         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5141
5142         lustre_cfg_bufs_reset(bufs, name);
5143         lustre_cfg_bufs_set_string(bufs, 1, uuid);
5144         lustre_cfg_bufs_set_string(bufs, 2, dev);
5145         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5146
5147         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5148         if (!lcfg)
5149                 GOTO(class_detach, rc = -ENOMEM);
5150         lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5151
5152         rc = class_setup(obd, lcfg);
5153         if (rc)
5154                 GOTO(class_detach, rc);
5155
5156         /* connect to MDD we just setup */
5157         rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5158         if (rc)
5159                 GOTO(class_detach, rc);
5160
5161         site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5162         LASSERT(site);
5163         LASSERT(mdt_lu_site(mdt) == NULL);
5164         mdt->mdt_lu_dev.ld_site = site;
5165         site->ls_top_dev = &mdt->mdt_lu_dev;
5166         mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5167
5168         /* now connect to bottom OSD */
5169         snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5170         rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5171         if (rc)
5172                 GOTO(class_detach, rc);
5173         mdt->mdt_bottom =
5174                 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5175
5176         rc = lu_env_refill((struct lu_env *)env);
5177         if (rc != 0)
5178                 CERROR("Failure to refill session: '%d'\n", rc);
5179
5180         lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5181
5182         EXIT;
5183 class_detach:
5184         if (rc)
5185                 class_detach(obd, lcfg);
5186 lcfg_cleanup:
5187         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5188 put_profile:
5189         class_put_profile(lprof);
5190 free_bufs:
5191         OBD_FREE_PTR(bufs);
5192 cleanup_mem:
5193         if (name)
5194                 OBD_FREE(name, name_size);
5195         if (uuid)
5196                 OBD_FREE(uuid, uuid_size);
5197         RETURN(rc);
5198 }
5199
5200 /* setup quota master target on MDT0 */
5201 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5202                           struct lustre_cfg *cfg)
5203 {
5204         struct obd_device       *obd;
5205         char                    *dev = lustre_cfg_string(cfg, 0);
5206         char                    *qmtname, *uuid, *p;
5207         struct lustre_cfg_bufs  *bufs;
5208         struct lustre_cfg       *lcfg;
5209         struct lustre_profile   *lprof;
5210         struct obd_connect_data *data;
5211         int                      rc;
5212         ENTRY;
5213
5214         LASSERT(mdt->mdt_qmt_exp == NULL);
5215         LASSERT(mdt->mdt_qmt_dev == NULL);
5216
5217         /* quota master is on MDT0 only for now */
5218         if (mdt->mdt_seq_site.ss_node_id != 0)
5219                 RETURN(0);
5220
5221         /* MGS generates config commands which look as follows:
5222          *   #01 (160)setup   0:lustre-MDT0000  1:lustre-MDT0000_UUID  2:0
5223          *                    3:lustre-MDT0000-mdtlov  4:f
5224          *
5225          * We generate the QMT name from the MDT one, just replacing MD with QM
5226          * after all the preparations, the logical equivalent will be:
5227          *   #01 (160)setup   0:lustre-QMT0000  1:lustre-QMT0000_UUID  2:0
5228          *                    3:lustre-MDT0000-osd  4:f */
5229         OBD_ALLOC(qmtname, MAX_OBD_NAME);
5230         OBD_ALLOC(uuid, UUID_MAX);
5231         OBD_ALLOC_PTR(bufs);
5232         OBD_ALLOC_PTR(data);
5233         if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5234                 GOTO(cleanup_mem, rc = -ENOMEM);
5235
5236         strcpy(qmtname, dev);
5237         p = strstr(qmtname, "-MDT");
5238         if (p == NULL)
5239                 GOTO(cleanup_mem, rc = -ENOMEM);
5240         /* replace MD with QM */
5241         p[1] = 'Q';
5242         p[2] = 'M';
5243
5244         snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5245
5246         lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5247         if (lprof == NULL || lprof->lp_dt == NULL) {
5248                 CERROR("can't find profile for %s\n",
5249                        lustre_cfg_string(cfg, 0));
5250                 GOTO(cleanup_mem, rc = -EINVAL);
5251         }
5252
5253         lustre_cfg_bufs_reset(bufs, qmtname);
5254         lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5255         lustre_cfg_bufs_set_string(bufs, 2, uuid);
5256         lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5257
5258         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5259         if (!lcfg)
5260                 GOTO(put_profile, rc = -ENOMEM);
5261         lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5262
5263         rc = class_attach(lcfg);
5264         if (rc)
5265                 GOTO(lcfg_cleanup, rc);
5266
5267         obd = class_name2obd(qmtname);
5268         if (!obd) {
5269                 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5270                        lustre_cfg_string(cfg, 0));
5271                 GOTO(lcfg_cleanup, rc = -EINVAL);
5272         }
5273
5274         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5275
5276         lustre_cfg_bufs_reset(bufs, qmtname);
5277         lustre_cfg_bufs_set_string(bufs, 1, uuid);
5278         lustre_cfg_bufs_set_string(bufs, 2, dev);
5279
5280         /* for quota, the next device should be the OSD device */
5281         lustre_cfg_bufs_set_string(bufs, 3,
5282                                    mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5283
5284         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5285         if (!lcfg)
5286                 GOTO(class_detach, rc = -ENOMEM);
5287         lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5288
5289         rc = class_setup(obd, lcfg);
5290         if (rc)
5291                 GOTO(class_detach, rc);
5292
5293         mdt->mdt_qmt_dev = obd->obd_lu_dev;
5294
5295         /* configure local quota objects */
5296         if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5297                 rc = -EBADF;
5298         else
5299                 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5300                                                            &mdt->mdt_lu_dev,
5301                                                            mdt->mdt_qmt_dev);
5302         if (rc)
5303                 GOTO(class_cleanup, rc);
5304
5305         /* connect to quota master target */
5306         data->ocd_connect_flags = OBD_CONNECT_VERSION;
5307         data->ocd_version = LUSTRE_VERSION_CODE;
5308         rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5309                          data, NULL);
5310         if (rc) {
5311                 CERROR("cannot connect to quota master device %s (%d)\n",
5312                        qmtname, rc);
5313                 GOTO(class_cleanup, rc);
5314         }
5315
5316         EXIT;
5317 class_cleanup:
5318         if (rc) {
5319                 class_manual_cleanup(obd);
5320                 mdt->mdt_qmt_dev = NULL;
5321                 GOTO(lcfg_cleanup, rc);
5322         }
5323 class_detach:
5324         if (rc)
5325                 class_detach(obd, lcfg);
5326 lcfg_cleanup:
5327         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5328 put_profile:
5329         class_put_profile(lprof);
5330 cleanup_mem:
5331         if (bufs)
5332                 OBD_FREE_PTR(bufs);
5333         if (qmtname)
5334                 OBD_FREE(qmtname, MAX_OBD_NAME);
5335         if (uuid)
5336                 OBD_FREE(uuid, UUID_MAX);
5337         if (data)
5338                 OBD_FREE_PTR(data);
5339         return rc;
5340 }
5341
5342 /* Shutdown quota master target associated with mdt */
5343 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5344 {
5345         ENTRY;
5346
5347         if (mdt->mdt_qmt_exp == NULL)
5348                 RETURN_EXIT;
5349         LASSERT(mdt->mdt_qmt_dev != NULL);
5350
5351         /* the qmt automatically shuts down when the mdt disconnects */
5352         obd_disconnect(mdt->mdt_qmt_exp);
5353         mdt->mdt_qmt_exp = NULL;
5354         mdt->mdt_qmt_dev = NULL;
5355         EXIT;
5356 }
5357
5358 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5359  * for now. This will be removed along with converting rest of MDT code
5360  * to use tgt_session_info */
5361 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5362 {
5363         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
5364         int                      rc;
5365
5366         if (unlikely(info->mti_object == NULL))
5367                 return -EPROTO;
5368
5369         rc = mdt_getxattr(info);
5370
5371         mdt_thread_info_fini(info);
5372         return rc;
5373 }
5374
5375 static int mdt_llog_open(struct tgt_session_info *tsi)
5376 {
5377         ENTRY;
5378
5379         if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
5380                 RETURN(err_serious(-EACCES));
5381
5382         RETURN(tgt_llog_open(tsi));
5383 }
5384
5385 #define OBD_FAIL_OST_READ_NET   OBD_FAIL_OST_BRW_NET
5386 #define OBD_FAIL_OST_WRITE_NET  OBD_FAIL_OST_BRW_NET
5387 #define OST_BRW_READ    OST_READ
5388 #define OST_BRW_WRITE   OST_WRITE
5389
5390 static struct tgt_handler mdt_tgt_handlers[] = {
5391 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5392                 0,                      MDS_CONNECT,    mdt_tgt_connect,
5393                 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5394 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5395                 0,                      MDS_DISCONNECT, tgt_disconnect,
5396                 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5397 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5398                 HAS_REPLY,              MDS_SET_INFO,   mdt_set_info,
5399                 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5400 TGT_MDT_HDL(0,                          MDS_GET_INFO,   mdt_get_info),
5401 TGT_MDT_HDL(HAS_REPLY,          MDS_GET_ROOT,   mdt_get_root),
5402 TGT_MDT_HDL(HAS_BODY,           MDS_GETATTR,    mdt_getattr),
5403 TGT_MDT_HDL(HAS_BODY | HAS_REPLY,       MDS_GETATTR_NAME,
5404                                                         mdt_getattr_name),
5405 TGT_MDT_HDL(HAS_BODY,           MDS_GETXATTR,   mdt_tgt_getxattr),
5406 TGT_MDT_HDL(HAS_REPLY,          MDS_STATFS,     mdt_statfs),
5407 TGT_MDT_HDL(IS_MUTABLE,         MDS_REINT,      mdt_reint),
5408 TGT_MDT_HDL(HAS_BODY,           MDS_CLOSE,      mdt_close),
5409 TGT_MDT_HDL(HAS_BODY | HAS_REPLY,       MDS_READPAGE,   mdt_readpage),
5410 TGT_MDT_HDL(HAS_BODY | HAS_REPLY,       MDS_SYNC,       mdt_sync),
5411 TGT_MDT_HDL(0,                          MDS_QUOTACTL,   mdt_quotactl),
5412 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5413                                                         mdt_hsm_progress),
5414 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5415                                                         mdt_hsm_ct_register),
5416 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5417                                                         mdt_hsm_ct_unregister),
5418 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5419                                                         mdt_hsm_state_get),
5420 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5421                                                         mdt_hsm_state_set),
5422 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION,       mdt_hsm_action),
5423 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
5424                                                         mdt_hsm_request),
5425 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
5426             MDS_SWAP_LAYOUTS,
5427             mdt_swap_layouts),
5428 TGT_MDT_HDL(IS_MUTABLE,         MDS_RMFID,      mdt_rmfid),
5429 };
5430
5431 static struct tgt_handler mdt_io_ops[] = {
5432 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
5433                                                         mdt_hp_brw),
5434 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE,    OST_BRW_WRITE, tgt_brw_write,
5435                                                         mdt_hp_brw),
5436 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
5437                                          OST_PUNCH,     mdt_punch_hdl,
5438                                                         mdt_hp_punch),
5439 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC,     mdt_data_sync),
5440 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
5441 };
5442
5443 static struct tgt_handler mdt_sec_ctx_ops[] = {
5444 TGT_SEC_HDL_VAR(0,                      SEC_CTX_INIT,     mdt_sec_ctx_handle),
5445 TGT_SEC_HDL_VAR(0,                      SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
5446 TGT_SEC_HDL_VAR(0,                      SEC_CTX_FINI,     mdt_sec_ctx_handle)
5447 };
5448
5449 static struct tgt_handler mdt_quota_ops[] = {
5450 TGT_QUOTA_HDL(HAS_REPLY,                QUOTA_DQACQ,      mdt_quota_dqacq),
5451 };
5452
5453 static struct tgt_handler mdt_llog_handlers[] = {
5454         TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE,      mdt_llog_open),
5455         TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK,  tgt_llog_next_block),
5456         TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
5457         TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK,  tgt_llog_prev_block),
5458 };
5459
5460 static struct tgt_opc_slice mdt_common_slice[] = {
5461         {
5462                 .tos_opc_start  = MDS_FIRST_OPC,
5463                 .tos_opc_end    = MDS_LAST_OPC,
5464                 .tos_hs         = mdt_tgt_handlers
5465         },
5466         {
5467                 .tos_opc_start  = OBD_FIRST_OPC,
5468                 .tos_opc_end    = OBD_LAST_OPC,
5469                 .tos_hs         = tgt_obd_handlers
5470         },
5471         {
5472                 .tos_opc_start  = LDLM_FIRST_OPC,
5473                 .tos_opc_end    = LDLM_LAST_OPC,
5474                 .tos_hs         = tgt_dlm_handlers
5475         },
5476         {
5477                 .tos_opc_start  = SEC_FIRST_OPC,
5478                 .tos_opc_end    = SEC_LAST_OPC,
5479                 .tos_hs         = mdt_sec_ctx_ops
5480         },
5481         {
5482                 .tos_opc_start  = OUT_UPDATE_FIRST_OPC,
5483                 .tos_opc_end    = OUT_UPDATE_LAST_OPC,
5484                 .tos_hs         = tgt_out_handlers
5485         },
5486         {
5487                 .tos_opc_start  = FLD_FIRST_OPC,
5488                 .tos_opc_end    = FLD_LAST_OPC,
5489                 .tos_hs         = fld_handlers
5490         },
5491         {
5492                 .tos_opc_start  = SEQ_FIRST_OPC,
5493                 .tos_opc_end    = SEQ_LAST_OPC,
5494                 .tos_hs         = seq_handlers
5495         },
5496         {
5497                 .tos_opc_start  = QUOTA_DQACQ,
5498                 .tos_opc_end    = QUOTA_LAST_OPC,
5499                 .tos_hs         = mdt_quota_ops
5500         },
5501         {
5502                 .tos_opc_start  = LLOG_FIRST_OPC,
5503                 .tos_opc_end    = LLOG_LAST_OPC,
5504                 .tos_hs         = mdt_llog_handlers
5505         },
5506         {
5507                 .tos_opc_start  = LFSCK_FIRST_OPC,
5508                 .tos_opc_end    = LFSCK_LAST_OPC,
5509                 .tos_hs         = tgt_lfsck_handlers
5510         },
5511         {
5512                 .tos_opc_start  = OST_FIRST_OPC,
5513                 .tos_opc_end    = OST_LAST_OPC,
5514                 .tos_hs         = mdt_io_ops
5515         },
5516         {
5517                 .tos_hs         = NULL
5518         }
5519 };
5520
5521 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
5522 {
5523         struct md_device *next = m->mdt_child;
5524         struct lu_device *d = &m->mdt_lu_dev;
5525         struct obd_device *obd = mdt2obd_dev(m);
5526         struct lfsck_stop stop;
5527
5528         ENTRY;
5529         stop.ls_status = LS_PAUSED;
5530         stop.ls_flags = 0;
5531         next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
5532
5533         mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
5534
5535         mdt_restriper_stop(m);
5536         ping_evictor_stop();
5537
5538         /* Remove the HSM /proc entry so the coordinator cannot be
5539          * restarted by a user while it's shutting down.
5540          */
5541         mdt_hsm_cdt_stop(m);
5542
5543         mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
5544         mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5545
5546         if (m->mdt_namespace != NULL)
5547                 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
5548                                           d->ld_obd->obd_force);
5549
5550         obd_exports_barrier(obd);
5551         obd_zombie_barrier();
5552
5553         mdt_quota_fini(env, m);
5554
5555         cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
5556
5557         /* Calling the cleanup functions in the same order as in the mdt_init0
5558          * error path
5559          */
5560         mdt_tunables_fini(m);
5561
5562         target_recovery_fini(obd);
5563         upcall_cache_cleanup(m->mdt_identity_cache);
5564         m->mdt_identity_cache = NULL;
5565
5566         mdt_fs_cleanup(env, m);
5567
5568         tgt_fini(env, &m->mdt_lut);
5569
5570         mdt_hsm_cdt_fini(m);
5571
5572         if (m->mdt_los != NULL) {
5573                 local_oid_storage_fini(env, m->mdt_los);
5574                 m->mdt_los = NULL;
5575         }
5576
5577         if (m->mdt_namespace != NULL) {
5578                 ldlm_namespace_free_post(m->mdt_namespace);
5579                 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
5580         }
5581
5582         if (m->mdt_md_root != NULL) {
5583                 mdt_object_put(env, m->mdt_md_root);
5584                 m->mdt_md_root = NULL;
5585         }
5586
5587         mdt_seq_fini(env, m);
5588
5589         mdt_fld_fini(env, m);
5590
5591         /*
5592          * Finish the stack
5593          */
5594         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5595
5596         LASSERT(atomic_read(&d->ld_ref) == 0);
5597
5598         server_put_mount(mdt_obd_name(m), true);
5599
5600         EXIT;
5601 }
5602
5603 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
5604
5605 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
5606                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
5607 {
5608         const struct dt_device_param *dt_conf;
5609         struct mdt_thread_info *info;
5610         struct obd_device *obd;
5611         const char *dev = lustre_cfg_string(cfg, 0);
5612         const char *num = lustre_cfg_string(cfg, 2);
5613         struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
5614         struct lustre_mount_info *lmi = NULL;
5615         struct lustre_sb_info *lsi;
5616         struct lu_site *s;
5617         struct seq_server_site *ss_site;
5618         const char *identity_upcall = "NONE";
5619         struct md_device *next;
5620         struct lu_fid fid;
5621         int rc;
5622         long node_id;
5623         mntopt_t mntopts;
5624         ENTRY;
5625
5626         lu_device_init(&m->mdt_lu_dev, ldt);
5627         /*
5628          * Environment (env) might be missing mdt_thread_key values at that
5629          * point, if device is allocated when mdt_thread_key is in QUIESCENT
5630          * mode.
5631          *
5632          * Usually device allocation path doesn't use module key values, but
5633          * mdt has to do a lot of work here, so allocate key value.
5634          */
5635         rc = lu_env_refill((struct lu_env *)env);
5636         if (rc != 0)
5637                 RETURN(rc);
5638
5639         info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5640         LASSERT(info != NULL);
5641
5642         obd = class_name2obd(dev);
5643         LASSERT(obd != NULL);
5644
5645         m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
5646         m->mdt_opts.mo_evict_tgt_nids = 1;
5647         m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5648
5649         lmi = server_get_mount(dev);
5650         if (lmi == NULL) {
5651                 CERROR("Cannot get mount info for %s!\n", dev);
5652                 RETURN(-EFAULT);
5653         } else {
5654                 lsi = s2lsi(lmi->lmi_sb);
5655                 LASSERT(lsi->lsi_lmd);
5656                 /* CMD is supported only in IAM mode */
5657                 LASSERT(num);
5658                 rc = kstrtol(num, 10, &node_id);
5659                 if (rc)
5660                         RETURN(rc);
5661
5662                 obd->u.obt.obt_magic = OBT_MAGIC;
5663                 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
5664                         m->mdt_skip_lfsck = 1;
5665         }
5666
5667         /* Just try to get a DoM lock by default. Otherwise, having a group
5668          * lock granted, it may get blocked for a long time. */
5669         m->mdt_opts.mo_dom_lock = TRYLOCK_DOM_ON_OPEN;
5670         /* DoM files are read at open and data is packed in the reply */
5671         m->mdt_opts.mo_dom_read_open = 1;
5672
5673         m->mdt_squash.rsi_uid = 0;
5674         m->mdt_squash.rsi_gid = 0;
5675         INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
5676         spin_lock_init(&m->mdt_squash.rsi_lock);
5677         spin_lock_init(&m->mdt_lock);
5678         m->mdt_enable_remote_dir = 1;
5679         m->mdt_enable_striped_dir = 1;
5680         m->mdt_enable_dir_migration = 1;
5681         m->mdt_enable_dir_restripe = 0;
5682         m->mdt_enable_dir_auto_split = 0;
5683         m->mdt_enable_remote_dir_gid = 0;
5684         m->mdt_enable_chprojid_gid = 0;
5685         m->mdt_enable_remote_rename = 1;
5686         m->mdt_dir_restripe_nsonly = 1;
5687         m->mdt_enable_remote_subdir_mount = 1;
5688
5689         atomic_set(&m->mdt_mds_mds_conns, 0);
5690         atomic_set(&m->mdt_async_commit_count, 0);
5691
5692         m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
5693         m->mdt_lu_dev.ld_obd = obd;
5694         /* Set this lu_device to obd for error handling purposes. */
5695         obd->obd_lu_dev = &m->mdt_lu_dev;
5696
5697         /* init the stack */
5698         rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5699         if (rc) {
5700                 CERROR("%s: Can't init device stack, rc %d\n",
5701                        mdt_obd_name(m), rc);
5702                 GOTO(err_lmi, rc);
5703         }
5704
5705         s = mdt_lu_site(m);
5706         ss_site = mdt_seq_site(m);
5707         s->ld_seq_site = ss_site;
5708         ss_site->ss_lu = s;
5709
5710         /* set server index */
5711         ss_site->ss_node_id = node_id;
5712
5713         /* failover is the default
5714          * FIXME: we do not failout mds0/mgs, which may cause some problems.
5715          * assumed whose ss_node_id == 0 XXX
5716          * */
5717         obd->obd_replayable = 1;
5718         /* No connection accepted until configurations will finish */
5719         obd->obd_no_conn = 1;
5720
5721         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5722                 char *str = lustre_cfg_string(cfg, 4);
5723                 if (strchr(str, 'n')) {
5724                         CWARN("%s: recovery disabled\n", mdt_obd_name(m));
5725                         obd->obd_replayable = 0;
5726                 }
5727         }
5728
5729         rc = mdt_fld_init(env, mdt_obd_name(m), m);
5730         if (rc)
5731                 GOTO(err_fini_stack, rc);
5732
5733         rc = mdt_seq_init(env, m);
5734         if (rc)
5735                 GOTO(err_fini_fld, rc);
5736
5737         snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
5738                  LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
5739         m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5740                                               LDLM_NAMESPACE_SERVER,
5741                                               LDLM_NAMESPACE_GREEDY,
5742                                               LDLM_NS_TYPE_MDT);
5743         if (IS_ERR(m->mdt_namespace)) {
5744                 rc = PTR_ERR(m->mdt_namespace);
5745                 CERROR("%s: unable to create server namespace: rc = %d\n",
5746                        obd->obd_name, rc);
5747                 m->mdt_namespace = NULL;
5748                 GOTO(err_fini_seq, rc);
5749         }
5750
5751         m->mdt_namespace->ns_lvbp = m;
5752         m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5753
5754         ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5755         /* set obd_namespace for compatibility with old code */
5756         obd->obd_namespace = m->mdt_namespace;
5757
5758         rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
5759                       OBD_FAIL_MDS_ALL_REQUEST_NET,
5760                       OBD_FAIL_MDS_ALL_REPLY_NET);
5761         if (rc)
5762                 GOTO(err_free_ns, rc);
5763
5764         /* Amount of available space excluded from granting and reserved
5765          * for metadata. It is a percentage of the total MDT size. */
5766         tgd->tgd_reserved_pcnt = 10;
5767
5768         if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
5769                 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
5770         else
5771                 m->mdt_brw_size = ONE_MB_BRW_SIZE;
5772
5773         rc = mdt_fs_setup(env, m, obd, lsi);
5774         if (rc)
5775                 GOTO(err_tgt, rc);
5776
5777         fid.f_seq = FID_SEQ_LOCAL_NAME;
5778         fid.f_oid = 1;
5779         fid.f_ver = 0;
5780         rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
5781         if (rc != 0)
5782                 GOTO(err_fs_cleanup, rc);
5783
5784         rc = mdt_hsm_cdt_init(m);
5785         if (rc != 0) {
5786                 CERROR("%s: error initializing coordinator, rc %d\n",
5787                        mdt_obd_name(m), rc);
5788                 GOTO(err_los_fini, rc);
5789         }
5790
5791         tgt_adapt_sptlrpc_conf(&m->mdt_lut);
5792
5793         next = m->mdt_child;
5794         dt_conf = next->md_ops->mdo_dtconf_get(env, next);
5795
5796         mntopts = dt_conf->ddp_mntopts;
5797
5798         if (mntopts & MNTOPT_USERXATTR)
5799                 m->mdt_opts.mo_user_xattr = 1;
5800         else
5801                 m->mdt_opts.mo_user_xattr = 0;
5802
5803         m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
5804
5805         if (mntopts & MNTOPT_ACL)
5806                 m->mdt_opts.mo_acl = 1;
5807         else
5808                 m->mdt_opts.mo_acl = 0;
5809
5810         /* XXX: to support suppgid for ACL, we enable identity_upcall
5811          * by default, otherwise, maybe got unexpected -EACCESS. */
5812         if (m->mdt_opts.mo_acl)
5813                 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5814
5815         m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5816                                                 identity_upcall,
5817                                                 &mdt_identity_upcall_cache_ops);
5818         if (IS_ERR(m->mdt_identity_cache)) {
5819                 rc = PTR_ERR(m->mdt_identity_cache);
5820                 m->mdt_identity_cache = NULL;
5821                 GOTO(err_free_hsm, rc);
5822         }
5823
5824         rc = mdt_tunables_init(m, dev);
5825         if (rc) {
5826                 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5827                 GOTO(err_recovery, rc);
5828         }
5829
5830         rc = mdt_quota_init(env, m, cfg);
5831         if (rc)
5832                 GOTO(err_procfs, rc);
5833
5834         m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5835         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5836                            "mdt_ldlm_client", m->mdt_ldlm_client);
5837
5838         ping_evictor_start();
5839
5840         /* recovery will be started upon mdt_prepare()
5841          * when the whole stack is complete and ready
5842          * to serve the requests */
5843
5844         /* Reduce the initial timeout on an MDS because it doesn't need such
5845          * a long timeout as an OST does. Adaptive timeouts will adjust this
5846          * value appropriately. */
5847         if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5848                 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5849
5850         if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_LOCAL_RECOV))
5851                 m->mdt_lut.lut_local_recovery = 1;
5852
5853         rc = mdt_restriper_start(m);
5854         if (rc)
5855                 GOTO(err_ping_evictor, rc);
5856
5857         RETURN(0);
5858
5859 err_ping_evictor:
5860         ping_evictor_stop();
5861 err_procfs:
5862         mdt_tunables_fini(m);
5863 err_recovery:
5864         upcall_cache_cleanup(m->mdt_identity_cache);
5865         m->mdt_identity_cache = NULL;
5866 err_free_hsm:
5867         mdt_hsm_cdt_fini(m);
5868 err_los_fini:
5869         local_oid_storage_fini(env, m->mdt_los);
5870         m->mdt_los = NULL;
5871 err_fs_cleanup:
5872         mdt_fs_cleanup(env, m);
5873 err_tgt:
5874         /* keep recoverable clients */
5875         obd->obd_fail = 1;
5876         target_recovery_fini(obd);
5877         obd_exports_barrier(obd);
5878         obd_zombie_barrier();
5879         tgt_fini(env, &m->mdt_lut);
5880 err_free_ns:
5881         ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5882         obd->obd_namespace = m->mdt_namespace = NULL;
5883 err_fini_seq:
5884         mdt_seq_fini(env, m);
5885 err_fini_fld:
5886         mdt_fld_fini(env, m);
5887 err_fini_stack:
5888         mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5889 err_lmi:
5890         if (lmi)
5891                 server_put_mount(dev, true);
5892         return(rc);
5893 }
5894
5895 /* For interoperability, the left element is old parameter, the right one
5896  * is the new version of the parameter, if some parameter is deprecated,
5897  * the new version should be set as NULL. */
5898 static struct cfg_interop_param mdt_interop_param[] = {
5899         { "mdt.group_upcall",   NULL },
5900         { "mdt.quota_type",     NULL },
5901         { "mdd.quota_type",     NULL },
5902         { "mdt.som",            NULL },
5903         { "mdt.rootsquash",     "mdt.root_squash" },
5904         { "mdt.nosquash_nid",   "mdt.nosquash_nids" },
5905         { NULL }
5906 };
5907
5908 /* used by MGS to process specific configurations */
5909 static int mdt_process_config(const struct lu_env *env,
5910                               struct lu_device *d, struct lustre_cfg *cfg)
5911 {
5912         struct mdt_device *m = mdt_dev(d);
5913         struct md_device *md_next = m->mdt_child;
5914         struct lu_device *next = md2lu_dev(md_next);
5915         int rc;
5916         ENTRY;
5917
5918         switch (cfg->lcfg_command) {
5919         case LCFG_PARAM: {
5920                 struct obd_device *obd = d->ld_obd;
5921                 /* For interoperability */
5922                 struct cfg_interop_param *ptr = NULL;
5923                 struct lustre_cfg *old_cfg = NULL;
5924                 char *param = NULL;
5925                 ssize_t count;
5926
5927                 param = lustre_cfg_string(cfg, 1);
5928                 if (param == NULL) {
5929                         CERROR("param is empty\n");
5930                         rc = -EINVAL;
5931                         break;
5932                 }
5933
5934                 ptr = class_find_old_param(param, mdt_interop_param);
5935                 if (ptr != NULL) {
5936                         if (ptr->new_param == NULL) {
5937                                 rc = 0;
5938                                 CWARN("For interoperability, skip this %s."
5939                                       " It is obsolete.\n", ptr->old_param);
5940                                 break;
5941                         }
5942
5943                         CWARN("Found old param %s, changed it to %s.\n",
5944                               ptr->old_param, ptr->new_param);
5945
5946                         old_cfg = cfg;
5947                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5948                         if (IS_ERR(cfg)) {
5949                                 rc = PTR_ERR(cfg);
5950                                 break;
5951                         }
5952                 }
5953
5954                 count = class_modify_config(cfg, PARAM_MDT,
5955                                             &obd->obd_kset.kobj);
5956                 if (count < 0) {
5957                         struct coordinator *cdt = &m->mdt_coordinator;
5958
5959                         /* is it an HSM var ? */
5960                         count = class_modify_config(cfg, PARAM_HSM,
5961                                                     &cdt->cdt_hsm_kobj);
5962                         if (count < 0)
5963                                 /* we don't understand; pass it on */
5964                                 rc = next->ld_ops->ldo_process_config(env, next,
5965                                                                       cfg);
5966                         else
5967                                 rc = count > 0 ? 0 : count;
5968                 } else {
5969                         rc = count > 0 ? 0 : count;
5970                 }
5971
5972                 if (old_cfg)
5973                         OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
5974                                                      cfg->lcfg_buflens));
5975                 break;
5976         }
5977         default:
5978                 /* others are passed further */
5979                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5980                 break;
5981         }
5982         RETURN(rc);
5983 }
5984
5985 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5986                                           const struct lu_object_header *hdr,
5987                                           struct lu_device *d)
5988 {
5989         struct mdt_object *mo;
5990
5991         ENTRY;
5992
5993         OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
5994         if (mo != NULL) {
5995                 struct lu_object *o;
5996                 struct lu_object_header *h;
5997
5998                 o = &mo->mot_obj;
5999                 h = &mo->mot_header;
6000                 lu_object_header_init(h);
6001                 lu_object_init(o, h, d);
6002                 lu_object_add_top(h, o);
6003                 o->lo_ops = &mdt_obj_ops;
6004                 spin_lock_init(&mo->mot_write_lock);
6005                 mutex_init(&mo->mot_som_mutex);
6006                 mutex_init(&mo->mot_lov_mutex);
6007                 init_rwsem(&mo->mot_dom_sem);
6008                 init_rwsem(&mo->mot_open_sem);
6009                 atomic_set(&mo->mot_open_count, 0);
6010                 mo->mot_restripe_offset = 0;
6011                 INIT_LIST_HEAD(&mo->mot_restripe_linkage);
6012                 RETURN(o);
6013         }
6014         RETURN(NULL);
6015 }
6016
6017 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
6018                            const struct lu_object_conf *unused)
6019 {
6020         struct mdt_device *d = mdt_dev(o->lo_dev);
6021         struct lu_device  *under;
6022         struct lu_object  *below;
6023         int                rc = 0;
6024         ENTRY;
6025
6026         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
6027                PFID(lu_object_fid(o)));
6028
6029         under = &d->mdt_child->md_lu_dev;
6030         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
6031         if (below != NULL) {
6032                 lu_object_add(o, below);
6033         } else
6034                 rc = -ENOMEM;
6035
6036         RETURN(rc);
6037 }
6038
6039 static void mdt_object_free_rcu(struct rcu_head *head)
6040 {
6041         struct mdt_object *mo = container_of(head, struct mdt_object,
6042                                              mot_header.loh_rcu);
6043
6044         kmem_cache_free(mdt_object_kmem, mo);
6045 }
6046
6047 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
6048 {
6049         struct mdt_object *mo = mdt_obj(o);
6050         struct lu_object_header *h;
6051         ENTRY;
6052
6053         h = o->lo_header;
6054         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
6055                PFID(lu_object_fid(o)));
6056
6057         LASSERT(atomic_read(&mo->mot_open_count) == 0);
6058         LASSERT(atomic_read(&mo->mot_lease_count) == 0);
6059
6060         lu_object_fini(o);
6061         lu_object_header_fini(h);
6062         OBD_FREE_PRE(mo, sizeof(*mo), "slab-freed");
6063         call_rcu(&mo->mot_header.loh_rcu, mdt_object_free_rcu);
6064
6065         EXIT;
6066 }
6067
6068 static int mdt_object_print(const struct lu_env *env, void *cookie,
6069                             lu_printer_t p, const struct lu_object *o)
6070 {
6071         struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
6072
6073         return (*p)(env, cookie,
6074                     LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
6075                     mdto, mdto->mot_lov_created ? "lov_created" : "",
6076                     mdto->mot_cache_attr ? "cache_attr" : "",
6077                     mdto->mot_write_count);
6078 }
6079
6080 static int mdt_prepare(const struct lu_env *env,
6081                 struct lu_device *pdev,
6082                 struct lu_device *cdev)
6083 {
6084         struct mdt_device *mdt = mdt_dev(cdev);
6085         struct lu_device *next = &mdt->mdt_child->md_lu_dev;
6086         struct obd_device *obd = cdev->ld_obd;
6087         int rc;
6088
6089         ENTRY;
6090
6091         LASSERT(obd);
6092
6093         rc = next->ld_ops->ldo_prepare(env, cdev, next);
6094         if (rc)
6095                 RETURN(rc);
6096
6097         rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
6098         if (rc)
6099                 RETURN(rc);
6100
6101         rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
6102         if (rc)
6103                 RETURN(rc);
6104
6105         rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
6106         /* The LFSCK instance is registered just now, so it must be there when
6107          * register the namespace to such instance. */
6108         LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
6109
6110         if (mdt->mdt_seq_site.ss_node_id == 0) {
6111                 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
6112                                                          &mdt->mdt_md_root_fid);
6113                 if (rc)
6114                         RETURN(rc);
6115         }
6116
6117         LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
6118
6119         target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
6120         set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
6121         LASSERT(obd->obd_no_conn);
6122         spin_lock(&obd->obd_dev_lock);
6123         obd->obd_no_conn = 0;
6124         spin_unlock(&obd->obd_dev_lock);
6125
6126         if (obd->obd_recovering == 0)
6127                 mdt_postrecov(env, mdt);
6128
6129         RETURN(rc);
6130 }
6131
6132 const struct lu_device_operations mdt_lu_ops = {
6133         .ldo_object_alloc   = mdt_object_alloc,
6134         .ldo_process_config = mdt_process_config,
6135         .ldo_prepare        = mdt_prepare,
6136 };
6137
6138 static const struct lu_object_operations mdt_obj_ops = {
6139         .loo_object_init    = mdt_object_init,
6140         .loo_object_free    = mdt_object_free,
6141         .loo_object_print   = mdt_object_print
6142 };
6143
6144 static int mdt_obd_set_info_async(const struct lu_env *env,
6145                                   struct obd_export *exp,
6146                                   __u32 keylen, void *key,
6147                                   __u32 vallen, void *val,
6148                                   struct ptlrpc_request_set *set)
6149 {
6150         int rc;
6151
6152         ENTRY;
6153
6154         if (KEY_IS(KEY_SPTLRPC_CONF)) {
6155                 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
6156                 RETURN(rc);
6157         }
6158
6159         RETURN(0);
6160 }
6161
6162 static inline void mdt_enable_slc(struct mdt_device *mdt)
6163 {
6164         if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_NEVER)
6165                 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_BLOCKING;
6166 }
6167
6168 static inline void mdt_disable_slc(struct mdt_device *mdt)
6169 {
6170         if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING)
6171                 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_NEVER;
6172 }
6173
6174 /**
6175  * Match client and server connection feature flags.
6176  *
6177  * Compute the compatibility flags for a connection request based on
6178  * features mutually supported by client and server.
6179  *
6180  * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
6181  * must not be updated here, otherwise a partially initialized value may
6182  * be exposed. After the connection request is successfully processed,
6183  * the top-level MDT connect request handler atomically updates the export
6184  * connect flags from the obd_connect_data::ocd_connect_flags field of the
6185  * reply. \see mdt_connect().
6186  *
6187  * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
6188  * full struct obd_connect_data. So care must be taken when accessing fields
6189  * that are not present in struct obd_connect_data_v1. See LU-16.
6190  *
6191  * \param exp   the obd_export associated with this client/target pair
6192  * \param mdt   the target device for the connection
6193  * \param data  stores data for this connect request
6194  *
6195  * \retval 0       success
6196  * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
6197  * \retval -EBADE  client and server feature requirements are incompatible
6198  */
6199 static int mdt_connect_internal(const struct lu_env *env,
6200                                 struct obd_export *exp,
6201                                 struct mdt_device *mdt,
6202                                 struct obd_connect_data *data, bool reconnect)
6203 {
6204         const char *obd_name = mdt_obd_name(mdt);
6205         LASSERT(data != NULL);
6206
6207         data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
6208
6209         if (mdt->mdt_bottom->dd_rdonly &&
6210             !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6211             !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
6212                 RETURN(-EACCES);
6213
6214         if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
6215                 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
6216
6217         data->ocd_ibits_known &= MDS_INODELOCK_FULL;
6218
6219         if (!mdt->mdt_opts.mo_acl)
6220                 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
6221
6222         if (!mdt->mdt_opts.mo_user_xattr)
6223                 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
6224
6225         if (OCD_HAS_FLAG(data, BRW_SIZE)) {
6226                 data->ocd_brw_size = min(data->ocd_brw_size,
6227                                          mdt->mdt_brw_size);
6228                 if (data->ocd_brw_size == 0) {
6229                         CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
6230                                "ocd_version: %x ocd_grant: %d ocd_index: %u "
6231                                "ocd_brw_size unexpectedly zero, network data "
6232                                "corruption? Refusing to connect this client\n",
6233                                obd_name, exp->exp_client_uuid.uuid,
6234                                exp, data->ocd_connect_flags, data->ocd_version,
6235                                data->ocd_grant, data->ocd_index);
6236                         return -EPROTO;
6237                 }
6238         }
6239
6240         if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
6241                 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
6242
6243                 /* client is reporting its page size, for future use */
6244                 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
6245                 data->ocd_grant_blkbits  = mdt->mdt_lut.lut_tgd.tgd_blockbits;
6246                 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
6247                  * it's LDISKFS_DIR_REC_LEN(20) = 28. */
6248                 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
6249                 /* ocd_grant_tax_kb is in 1K byte blocks */
6250                 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
6251                 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
6252         }
6253
6254         /* Save connect_data we have so far because tgt_grant_connect()
6255          * uses it to calculate grant, and we want to save the client
6256          * version before it is overwritten by LUSTRE_VERSION_CODE. */
6257         exp->exp_connect_data = *data;
6258         if (OCD_HAS_FLAG(data, GRANT))
6259                 tgt_grant_connect(env, exp, data, !reconnect);
6260
6261         if (OCD_HAS_FLAG(data, MAXBYTES))
6262                 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
6263
6264         /* NB: Disregard the rule against updating
6265          * exp_connect_data.ocd_connect_flags in this case, since
6266          * tgt_client_new() needs to know if this is a lightweight
6267          * connection, and it is safe to expose this flag before
6268          * connection processing completes. */
6269         if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
6270                 spin_lock(&exp->exp_lock);
6271                 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
6272                 spin_unlock(&exp->exp_lock);
6273         }
6274
6275         data->ocd_version = LUSTRE_VERSION_CODE;
6276
6277         if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
6278                 CWARN("%s: MDS requires FID support, but client not\n",
6279                       obd_name);
6280                 return -EBADE;
6281         }
6282
6283         if (OCD_HAS_FLAG(data, PINGLESS)) {
6284                 if (ptlrpc_pinger_suppress_pings()) {
6285                         spin_lock(&exp->exp_obd->obd_dev_lock);
6286                         list_del_init(&exp->exp_obd_chain_timed);
6287                         spin_unlock(&exp->exp_obd->obd_dev_lock);
6288                 } else {
6289                         data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
6290                 }
6291         }
6292
6293         data->ocd_max_easize = mdt->mdt_max_ea_size;
6294
6295         /* NB: Disregard the rule against updating
6296          * exp_connect_data.ocd_connect_flags in this case, since
6297          * tgt_client_new() needs to know if this is client supports
6298          * multiple modify RPCs, and it is safe to expose this flag before
6299          * connection processing completes. */
6300         if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
6301                 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
6302                 spin_lock(&exp->exp_lock);
6303                 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
6304                 spin_unlock(&exp->exp_lock);
6305         }
6306
6307         if (OCD_HAS_FLAG(data, CKSUM)) {
6308                 __u32 cksum_types = data->ocd_cksum_types;
6309
6310                 tgt_mask_cksum_types(&mdt->mdt_lut, &data->ocd_cksum_types);
6311
6312                 if (unlikely(data->ocd_cksum_types == 0)) {
6313                         CERROR("%s: Connect with checksum support but no "
6314                                "ocd_cksum_types is set\n",
6315                                exp->exp_obd->obd_name);
6316                         RETURN(-EPROTO);
6317                 }
6318
6319                 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
6320                        "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
6321                        cksum_types, data->ocd_cksum_types);
6322         } else {
6323                 /* This client does not support OBD_CONNECT_CKSUM
6324                  * fall back to CRC32 */
6325                 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
6326                        "OBD_CONNECT_CKSUM, CRC32 will be used\n",
6327                        exp->exp_obd->obd_name, obd_export_nid2str(exp));
6328         }
6329
6330         if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6331             !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
6332                 atomic_inc(&mdt->mdt_mds_mds_conns);
6333                 mdt_enable_slc(mdt);
6334         }
6335
6336         if (!mdt->mdt_lut.lut_dt_conf.ddp_has_lseek_data_hole)
6337                 data->ocd_connect_flags2 &= ~OBD_CONNECT2_LSEEK;
6338
6339         return 0;
6340 }
6341
6342 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
6343                                    struct mdt_thread_info *info,
6344                                    struct mdt_file_data *mfd)
6345 {
6346         struct lu_context ses;
6347         int rc;
6348         ENTRY;
6349
6350         rc = lu_context_init(&ses, LCT_SERVER_SESSION);
6351         if (rc)
6352                 RETURN(rc);
6353
6354         env->le_ses = &ses;
6355         lu_context_enter(&ses);
6356
6357         mdt_ucred(info)->uc_valid = UCRED_OLD;
6358         rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
6359
6360         lu_context_exit(&ses);
6361         lu_context_fini(&ses);
6362         env->le_ses = NULL;
6363
6364         RETURN(rc);
6365 }
6366
6367 static int mdt_export_cleanup(struct obd_export *exp)
6368 {
6369         LIST_HEAD(closing_list);
6370         struct mdt_export_data  *med = &exp->exp_mdt_data;
6371         struct obd_device       *obd = exp->exp_obd;
6372         struct mdt_device       *mdt;
6373         struct mdt_thread_info  *info;
6374         struct lu_env            env;
6375         struct mdt_file_data    *mfd, *n;
6376         int rc = 0;
6377         ENTRY;
6378
6379         spin_lock(&med->med_open_lock);
6380         while (!list_empty(&med->med_open_head)) {
6381                 struct list_head *tmp = med->med_open_head.next;
6382                 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
6383
6384                 /* Remove mfd handle so it can't be found again.
6385                  * We are consuming the mfd_list reference here. */
6386                 class_handle_unhash(&mfd->mfd_open_handle);
6387                 list_move_tail(&mfd->mfd_list, &closing_list);
6388         }
6389         spin_unlock(&med->med_open_lock);
6390         mdt = mdt_dev(obd->obd_lu_dev);
6391         LASSERT(mdt != NULL);
6392
6393         rc = lu_env_init(&env, LCT_MD_THREAD);
6394         if (rc)
6395                 RETURN(rc);
6396
6397         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6398         LASSERT(info != NULL);
6399         memset(info, 0, sizeof *info);
6400         info->mti_env = &env;
6401         info->mti_mdt = mdt;
6402         info->mti_exp = exp;
6403
6404         if (!list_empty(&closing_list)) {
6405                 struct md_attr *ma = &info->mti_attr;
6406
6407                 /* Close any open files (which may also cause orphan
6408                  * unlinking). */
6409                 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
6410                         list_del_init(&mfd->mfd_list);
6411                         ma->ma_need = ma->ma_valid = 0;
6412
6413                         /* This file is being closed due to an eviction, it
6414                          * could have been modified and now dirty regarding to
6415                          * HSM archive, check this!
6416                          * The logic here is to mark a file dirty if there's a
6417                          * chance it was dirtied before the client was evicted,
6418                          * so that we don't have to wait for a release attempt
6419                          * before finding out the file was actually dirty and
6420                          * fail the release. Aggressively marking it dirty here
6421                          * will cause the policy engine to attempt to
6422                          * re-archive it; when rearchiving, we can compare the
6423                          * current version to the HSM data_version and make the
6424                          * archive request into a noop if it's not actually
6425                          * dirty.
6426                          */
6427                         if (mfd->mfd_open_flags & MDS_FMODE_WRITE)
6428                                 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
6429
6430                         /* Don't unlink orphan on failover umount, LU-184 */
6431                         if (exp->exp_flags & OBD_OPT_FAILOVER) {
6432                                 ma->ma_valid = MA_FLAGS;
6433                                 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
6434                         }
6435                         ma->ma_valid |= MA_FORCE_LOG;
6436                         mdt_mfd_close(info, mfd);
6437                 }
6438         }
6439         info->mti_mdt = NULL;
6440         /* cleanup client slot early */
6441         /* Do not erase record for recoverable client. */
6442         if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
6443                 tgt_client_del(&env, exp);
6444         lu_env_fini(&env);
6445
6446         RETURN(rc);
6447 }
6448
6449 static int mdt_obd_disconnect(struct obd_export *exp)
6450 {
6451         int rc;
6452
6453         ENTRY;
6454
6455         LASSERT(exp);
6456         class_export_get(exp);
6457
6458         if (!(exp->exp_flags & OBD_OPT_FORCE))
6459                 tgt_grant_sanity_check(exp->exp_obd, __func__);
6460
6461         if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
6462             !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
6463                 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
6464
6465                 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
6466                         mdt_disable_slc(mdt);
6467         }
6468
6469         rc = server_disconnect_export(exp);
6470         if (rc != 0)
6471                 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
6472
6473         tgt_grant_discard(exp);
6474
6475         rc = mdt_export_cleanup(exp);
6476         nodemap_del_member(exp);
6477         class_export_put(exp);
6478         RETURN(rc);
6479 }
6480
6481 /* mds_connect copy */
6482 static int mdt_obd_connect(const struct lu_env *env,
6483                            struct obd_export **exp, struct obd_device *obd,
6484                            struct obd_uuid *cluuid,
6485                            struct obd_connect_data *data,
6486                            void *localdata)
6487 {
6488         struct obd_export       *lexp;
6489         struct lustre_handle    conn = { 0 };
6490         struct mdt_device       *mdt;
6491         int                      rc;
6492         lnet_nid_t              *client_nid = localdata;
6493         ENTRY;
6494
6495         LASSERT(env != NULL);
6496         LASSERT(data != NULL);
6497
6498         if (!exp || !obd || !cluuid)
6499                 RETURN(-EINVAL);
6500
6501         mdt = mdt_dev(obd->obd_lu_dev);
6502
6503         /*
6504          * first, check whether the stack is ready to handle requests
6505          * XXX: probably not very appropriate method is used now
6506          *      at some point we should find a better one
6507          */
6508         if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
6509             !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
6510             !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
6511                 rc = obd_get_info(env, mdt->mdt_child_exp,
6512                                   sizeof(KEY_OSP_CONNECTED),
6513                                   KEY_OSP_CONNECTED, NULL, NULL);
6514                 if (rc)
6515                         RETURN(-EAGAIN);
6516                 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
6517         }
6518
6519         rc = class_connect(&conn, obd, cluuid);
6520         if (rc)
6521                 RETURN(rc);
6522
6523         lexp = class_conn2export(&conn);
6524         LASSERT(lexp != NULL);
6525
6526         rc = nodemap_add_member(*client_nid, lexp);
6527         if (rc != 0 && rc != -EEXIST)
6528                 GOTO(out, rc);
6529
6530         rc = mdt_connect_internal(env, lexp, mdt, data, false);
6531         if (rc == 0) {
6532                 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
6533
6534                 LASSERT(lcd);
6535                 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
6536                 rc = tgt_client_new(env, lexp);
6537                 if (rc == 0)
6538                         mdt_export_stats_init(obd, lexp, localdata);
6539         }
6540 out:
6541         if (rc != 0) {
6542                 class_disconnect(lexp);
6543                 nodemap_del_member(lexp);
6544                 *exp = NULL;
6545         } else {
6546                 *exp = lexp;
6547                 /* Because we do not want this export to be evicted by pinger,
6548                  * let's not add this export to the timed chain list. */
6549                 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
6550                         spin_lock(&lexp->exp_obd->obd_dev_lock);
6551                         list_del_init(&lexp->exp_obd_chain_timed);
6552                         spin_unlock(&lexp->exp_obd->obd_dev_lock);
6553                 }
6554         }
6555
6556         RETURN(rc);
6557 }
6558
6559 static int mdt_obd_reconnect(const struct lu_env *env,
6560                              struct obd_export *exp, struct obd_device *obd,
6561                              struct obd_uuid *cluuid,
6562                              struct obd_connect_data *data,
6563                              void *localdata)
6564 {
6565         lnet_nid_t             *client_nid = localdata;
6566         int                     rc;
6567         ENTRY;
6568
6569         if (exp == NULL || obd == NULL || cluuid == NULL)
6570                 RETURN(-EINVAL);
6571
6572         rc = nodemap_add_member(*client_nid, exp);
6573         if (rc != 0 && rc != -EEXIST)
6574                 RETURN(rc);
6575
6576         rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
6577                                   true);
6578         if (rc == 0)
6579                 mdt_export_stats_init(obd, exp, localdata);
6580         else
6581                 nodemap_del_member(exp);
6582
6583         RETURN(rc);
6584 }
6585
6586 /* FIXME: Can we avoid using these two interfaces? */
6587 static int mdt_init_export(struct obd_export *exp)
6588 {
6589         struct mdt_export_data *med = &exp->exp_mdt_data;
6590         int                     rc;
6591         ENTRY;
6592
6593         INIT_LIST_HEAD(&med->med_open_head);
6594         spin_lock_init(&med->med_open_lock);
6595         spin_lock(&exp->exp_lock);
6596         exp->exp_connecting = 1;
6597         spin_unlock(&exp->exp_lock);
6598
6599         OBD_ALLOC(exp->exp_used_slots,
6600                   BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6601         if (exp->exp_used_slots == NULL)
6602                 RETURN(-ENOMEM);
6603
6604         /* self-export doesn't need client data and ldlm initialization */
6605         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6606                                      &exp->exp_client_uuid)))
6607                 RETURN(0);
6608
6609         rc = tgt_client_alloc(exp);
6610         if (rc)
6611                 GOTO(err, rc);
6612
6613         rc = ldlm_init_export(exp);
6614         if (rc)
6615                 GOTO(err_free, rc);
6616
6617         RETURN(rc);
6618
6619 err_free:
6620         tgt_client_free(exp);
6621 err:
6622         OBD_FREE(exp->exp_used_slots,
6623                  BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6624         exp->exp_used_slots = NULL;
6625
6626         CERROR("%s: Failed to initialize export: rc = %d\n",
6627                exp->exp_obd->obd_name, rc);
6628         return rc;
6629 }
6630
6631 static int mdt_destroy_export(struct obd_export *exp)
6632 {
6633         ENTRY;
6634
6635         target_destroy_export(exp);
6636         if (exp->exp_used_slots)
6637                 OBD_FREE(exp->exp_used_slots,
6638                          BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6639
6640         /* destroy can be called from failed obd_setup, so
6641          * checking uuid is safer than obd_self_export */
6642         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6643                                      &exp->exp_client_uuid)))
6644                 RETURN(0);
6645
6646         ldlm_destroy_export(exp);
6647         tgt_client_free(exp);
6648
6649         LASSERT(list_empty(&exp->exp_outstanding_replies));
6650         LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
6651
6652         /*
6653          * discard grants once we're sure no more
6654          * interaction with the client is possible
6655          */
6656         tgt_grant_discard(exp);
6657         if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
6658                 exp->exp_obd->u.obt.obt_lut->lut_tgd.tgd_tot_granted_clients--;
6659
6660         if (!(exp->exp_flags & OBD_OPT_FORCE))
6661                 tgt_grant_sanity_check(exp->exp_obd, __func__);
6662
6663         RETURN(0);
6664 }
6665
6666 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
6667                    struct linkea_data *ldata)
6668 {
6669         int rc;
6670
6671         LASSERT(ldata->ld_buf->lb_buf != NULL);
6672
6673         if (!mdt_object_exists(mdt_obj))
6674                 return -ENODATA;
6675
6676         rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6677                           ldata->ld_buf, XATTR_NAME_LINK);
6678         if (rc == -ERANGE) {
6679                 /* Buf was too small, figure out what we need. */
6680                 lu_buf_free(ldata->ld_buf);
6681                 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6682                                   ldata->ld_buf, XATTR_NAME_LINK);
6683                 if (rc < 0)
6684                         return rc;
6685                 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
6686                 if (ldata->ld_buf->lb_buf == NULL)
6687                         return -ENOMEM;
6688                 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6689                                   ldata->ld_buf, XATTR_NAME_LINK);
6690         }
6691         if (rc < 0)
6692                 return rc;
6693
6694         return linkea_init_with_rec(ldata);
6695 }
6696
6697 /**
6698  * Given an MDT object, try to look up the full path to the object.
6699  * Part of the MDT layer implementation of lfs fid2path.
6700  *
6701  * \param[in]     info  Per-thread common data shared by MDT level handlers.
6702  * \param[in]     obj   Object to do path lookup of
6703  * \param[in,out] fp    User-provided struct to store path information
6704  * \param[in]     root_fid Root FID of current path should reach
6705  *
6706  * \retval 0 Lookup successful, path information stored in fp
6707  * \retval -EAGAIN Lookup failed, usually because object is being moved
6708  * \retval negative errno if there was a problem
6709  */
6710 static int mdt_path_current(struct mdt_thread_info *info,
6711                             struct mdt_object *obj,
6712                             struct getinfo_fid2path *fp,
6713                             struct lu_fid *root_fid)
6714 {
6715         struct mdt_device *mdt = info->mti_mdt;
6716         struct lu_name *tmpname = &info->mti_name;
6717         struct lu_fid *tmpfid = &info->mti_tmp_fid1;
6718         struct lu_buf *buf = &info->mti_big_buf;
6719         struct md_attr *ma = &info->mti_attr;
6720         struct linkea_data ldata = { NULL };
6721         bool first = true;
6722         struct mdt_object *mdt_obj;
6723         struct link_ea_header *leh;
6724         struct link_ea_entry *lee;
6725         char *ptr;
6726         int reclen;
6727         int rc = 0;
6728
6729         ENTRY;
6730
6731         /* temp buffer for path element, the buffer will be finally freed
6732          * in mdt_thread_info_fini */
6733         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
6734         if (buf->lb_buf == NULL)
6735                 RETURN(-ENOMEM);
6736
6737         ldata.ld_buf = buf;
6738         ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
6739         *ptr = 0;
6740         --ptr;
6741         *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
6742
6743         while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
6744                 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
6745                     lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
6746                         GOTO(out, rc = -ENOENT);
6747
6748                 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
6749                         mdt_obj = obj;
6750                         mdt_object_get(info->mti_env, mdt_obj);
6751                 } else {
6752                         mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
6753                         if (IS_ERR(mdt_obj))
6754                                 GOTO(out, rc = PTR_ERR(mdt_obj));
6755                 }
6756
6757                 if (!mdt_object_exists(mdt_obj)) {
6758                         mdt_object_put(info->mti_env, mdt_obj);
6759                         GOTO(out, rc = -ENOENT);
6760                 }
6761
6762                 if (mdt_object_remote(mdt_obj)) {
6763                         mdt_object_put(info->mti_env, mdt_obj);
6764                         GOTO(remote_out, rc = -EREMOTE);
6765                 }
6766
6767                 rc = mdt_links_read(info, mdt_obj, &ldata);
6768                 if (rc != 0) {
6769                         mdt_object_put(info->mti_env, mdt_obj);
6770                         GOTO(out, rc);
6771                 }
6772
6773                 leh = buf->lb_buf;
6774                 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
6775                 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
6776                 /* If set, use link #linkno for path lookup, otherwise use
6777                    link #0.  Only do this for the final path element. */
6778                 if (first && fp->gf_linkno < leh->leh_reccount) {
6779                         int count;
6780                         for (count = 0; count < fp->gf_linkno; count++) {
6781                                 lee = (struct link_ea_entry *)
6782                                      ((char *)lee + reclen);
6783                                 linkea_entry_unpack(lee, &reclen, tmpname,
6784                                                     tmpfid);
6785                         }
6786                         if (fp->gf_linkno < leh->leh_reccount - 1)
6787                                 /* indicate to user there are more links */
6788                                 fp->gf_linkno++;
6789                 }
6790
6791                 /* Check if it is slave stripes */
6792                 rc = mdt_stripe_get(info, mdt_obj, ma, XATTR_NAME_LMV);
6793                 mdt_object_put(info->mti_env, mdt_obj);
6794                 if (rc < 0)
6795                         GOTO(out, rc);
6796
6797                 if (ma->ma_valid & MA_LMV) {
6798                         struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
6799
6800                         if (!lmv_is_sane2(lmv))
6801                                 GOTO(out, rc = -EBADF);
6802
6803                         /* For slave stripes, get its master */
6804                         if (le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE) {
6805                                 fp->gf_fid = *tmpfid;
6806                                 continue;
6807                         }
6808                 }
6809
6810                 /* Pack the name in the end of the buffer */
6811                 ptr -= tmpname->ln_namelen;
6812                 if (ptr - 1 <= fp->gf_u.gf_path)
6813                         GOTO(out, rc = -EOVERFLOW);
6814                 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
6815                 *(--ptr) = '/';
6816
6817                 /* keep the last resolved fid to the client, so the
6818                  * client will build the left path on another MDT for
6819                  * remote object */
6820                 fp->gf_fid = *tmpfid;
6821
6822                 first = false;
6823         }
6824
6825         /* non-zero will be treated as an error */
6826         rc = 0;
6827
6828 remote_out:
6829         ptr++; /* skip leading / */
6830         memmove(fp->gf_u.gf_path, ptr,
6831                 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
6832
6833 out:
6834         RETURN(rc);
6835 }
6836
6837 /**
6838  * Given an MDT object, use mdt_path_current to get the path.
6839  * Essentially a wrapper to retry mdt_path_current a set number of times
6840  * if -EAGAIN is returned (usually because an object is being moved).
6841  *
6842  * Part of the MDT layer implementation of lfs fid2path.
6843  *
6844  * \param[in]     info  Per-thread common data shared by mdt level handlers.
6845  * \param[in]     obj   Object to do path lookup of
6846  * \param[in,out] fp    User-provided struct for arguments and to store path
6847  *                      information
6848  *
6849  * \retval 0 Lookup successful, path information stored in fp
6850  * \retval negative errno if there was a problem
6851  */
6852 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
6853                     struct getinfo_fid2path *fp, struct lu_fid *root_fid)
6854 {
6855         struct mdt_device       *mdt = info->mti_mdt;
6856         int                     tries = 3;
6857         int                     rc = -EAGAIN;
6858         ENTRY;
6859
6860         if (fp->gf_pathlen < 3)
6861                 RETURN(-EOVERFLOW);
6862
6863         if (root_fid == NULL)
6864                 root_fid = &mdt->mdt_md_root_fid;
6865
6866         if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
6867                 fp->gf_u.gf_path[0] = '\0';
6868                 RETURN(0);
6869         }
6870
6871         /* Retry multiple times in case file is being moved */
6872         while (tries-- && rc == -EAGAIN)
6873                 rc = mdt_path_current(info, obj, fp, root_fid);
6874
6875         RETURN(rc);
6876 }
6877
6878 /**
6879  * Get the full path of the provided FID, as of changelog record recno.
6880  *
6881  * This checks sanity and looks up object for user provided FID
6882  * before calling the actual path lookup code.
6883  *
6884  * Part of the MDT layer implementation of lfs fid2path.
6885  *
6886  * \param[in]     info  Per-thread common data shared by mdt level handlers.
6887  * \param[in,out] fp    User-provided struct for arguments and to store path
6888  *                      information
6889  *
6890  * \retval 0 Lookup successful, path information and recno stored in fp
6891  * \retval -ENOENT, object does not exist
6892  * \retval negative errno if there was a problem
6893  */
6894 static int mdt_fid2path(struct mdt_thread_info *info,
6895                         struct lu_fid *root_fid,
6896                         struct getinfo_fid2path *fp)
6897 {
6898         struct mdt_device *mdt = info->mti_mdt;
6899         struct mdt_object *obj;
6900         int    rc;
6901         ENTRY;
6902
6903         CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
6904                 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
6905
6906         if (!fid_is_sane(&fp->gf_fid))
6907                 RETURN(-EINVAL);
6908
6909         if (!fid_is_namespace_visible(&fp->gf_fid)) {
6910                 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
6911                        ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
6912                        PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
6913                 RETURN(-EINVAL);
6914         }
6915
6916         obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
6917         if (IS_ERR(obj)) {
6918                 rc = PTR_ERR(obj);
6919                 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
6920                        PFID(&fp->gf_fid), rc);
6921                 RETURN(rc);
6922         }
6923
6924         if (mdt_object_remote(obj))
6925                 rc = -EREMOTE;
6926         else if (!mdt_object_exists(obj))
6927                 rc = -ENOENT;
6928         else
6929                 rc = 0;
6930
6931         if (rc < 0) {
6932                 mdt_object_put(info->mti_env, obj);
6933                 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
6934                        PFID(&fp->gf_fid), rc);
6935                 RETURN(rc);
6936         }
6937
6938         rc = mdt_path(info, obj, fp, root_fid);
6939
6940         CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
6941                PFID(&fp->gf_fid), fp->gf_u.gf_path,
6942                fp->gf_recno, fp->gf_linkno);
6943
6944         mdt_object_put(info->mti_env, obj);
6945
6946         RETURN(rc);
6947 }
6948
6949 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
6950                             void *val, int vallen)
6951 {
6952         struct getinfo_fid2path *fpout, *fpin;
6953         struct lu_fid *root_fid = NULL;
6954         int rc = 0;
6955
6956         fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6957         fpout = val;
6958
6959         if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6960                 lustre_swab_fid2path(fpin);
6961
6962         memcpy(fpout, fpin, sizeof(*fpin));
6963         if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6964                 RETURN(-EINVAL);
6965
6966         if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
6967                       sizeof(struct lu_fid)) {
6968                 /* client sent its root FID, which is normally fileset FID */
6969                 root_fid = fpin->gf_u.gf_root_fid;
6970                 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6971                         lustre_swab_lu_fid(root_fid);
6972
6973                 if (root_fid != NULL && !fid_is_sane(root_fid))
6974                         RETURN(-EINVAL);
6975         }
6976
6977         rc = mdt_fid2path(info, root_fid, fpout);
6978         RETURN(rc);
6979 }
6980
6981 int mdt_get_info(struct tgt_session_info *tsi)
6982 {
6983         char    *key;
6984         int      keylen;
6985         __u32   *vallen;
6986         void    *valout;
6987         int      rc;
6988
6989         ENTRY;
6990
6991         key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
6992         if (key == NULL) {
6993                 CDEBUG(D_IOCTL, "No GETINFO key\n");
6994                 RETURN(err_serious(-EFAULT));
6995         }
6996         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
6997                                       RCL_CLIENT);
6998
6999         vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
7000         if (vallen == NULL) {
7001                 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
7002                                 tgt_name(tsi->tsi_tgt));
7003                 RETURN(err_serious(-EFAULT));
7004         }
7005
7006         req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
7007                              *vallen);
7008         rc = req_capsule_server_pack(tsi->tsi_pill);
7009         if (rc)
7010                 RETURN(err_serious(rc));
7011
7012         valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
7013         if (valout == NULL) {
7014                 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
7015                                 tgt_name(tsi->tsi_tgt));
7016                 RETURN(err_serious(-EFAULT));
7017         }
7018
7019         if (KEY_IS(KEY_FID2PATH)) {
7020                 struct mdt_thread_info  *info = tsi2mdt_info(tsi);
7021
7022                 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
7023                 mdt_thread_info_fini(info);
7024         } else {
7025                 rc = -EINVAL;
7026         }
7027         RETURN(rc);
7028 }
7029
7030 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
7031 {
7032         struct obd_ioctl_data *data = karg;
7033         struct lu_fid *fid;
7034         __u64 version;
7035         struct mdt_object *obj;
7036         struct mdt_lock_handle  *lh;
7037         int rc;
7038         ENTRY;
7039
7040         if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
7041             data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
7042                 RETURN(-EINVAL);
7043
7044         fid = (struct lu_fid *)data->ioc_inlbuf1;
7045
7046         if (!fid_is_sane(fid))
7047                 RETURN(-EINVAL);
7048
7049         CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
7050
7051         lh = &mti->mti_lh[MDT_LH_PARENT];
7052         mdt_lock_reg_init(lh, LCK_CR);
7053
7054         obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
7055         if (IS_ERR(obj))
7056                 RETURN(PTR_ERR(obj));
7057
7058         if (mdt_object_remote(obj)) {
7059                 rc = -EREMOTE;
7060                 /**
7061                  * before calling version get the correct MDS should be
7062                  * fid, this is error to find remote object here
7063                  */
7064                 CERROR("nonlocal object "DFID"\n", PFID(fid));
7065         } else if (!mdt_object_exists(obj)) {
7066                 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
7067                 rc = -ENOENT;
7068         } else {
7069                 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
7070                *(__u64 *)data->ioc_inlbuf2 = version;
7071                 rc = 0;
7072         }
7073         mdt_object_unlock_put(mti, obj, lh, 1);
7074         RETURN(rc);
7075 }
7076
7077 /* ioctls on obd dev */
7078 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
7079                          void *karg, void __user *uarg)
7080 {
7081         struct lu_env      env;
7082         struct obd_device *obd = exp->exp_obd;
7083         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
7084         struct dt_device  *dt = mdt->mdt_bottom;
7085         int rc;
7086
7087         ENTRY;
7088         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
7089         rc = lu_env_init(&env, LCT_MD_THREAD);
7090         if (rc)
7091                 RETURN(rc);
7092
7093         switch (cmd) {
7094         case OBD_IOC_SYNC:
7095                 rc = mdt_device_sync(&env, mdt);
7096                 break;
7097         case OBD_IOC_SET_READONLY:
7098                 rc = dt_sync(&env, dt);
7099                 if (rc == 0)
7100                         rc = dt_ro(&env, dt);
7101                 break;
7102         case OBD_IOC_ABORT_RECOVERY: {
7103                 struct obd_ioctl_data *data = karg;
7104
7105                 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
7106                 if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT)
7107                         obd->obd_abort_recov_mdt = 1;
7108                 else /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
7109                         /* lctl didn't set OBD_FLG_ABORT_RECOV_OST < 2.13.57 */
7110                         obd->obd_abort_recovery = 1;
7111
7112                 target_stop_recovery_thread(obd);
7113                 rc = 0;
7114                 break;
7115         }
7116         case OBD_IOC_CHANGELOG_REG:
7117         case OBD_IOC_CHANGELOG_DEREG:
7118         case OBD_IOC_CHANGELOG_CLEAR:
7119                 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
7120                                                            mdt->mdt_child,
7121                                                            cmd, len, karg);
7122                 break;
7123         case OBD_IOC_START_LFSCK: {
7124                 struct md_device *next = mdt->mdt_child;
7125                 struct obd_ioctl_data *data = karg;
7126                 struct lfsck_start_param lsp;
7127
7128                 if (unlikely(data == NULL)) {
7129                         rc = -EINVAL;
7130                         break;
7131                 }
7132
7133                 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
7134                 lsp.lsp_index_valid = 0;
7135                 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
7136                 break;
7137         }
7138         case OBD_IOC_STOP_LFSCK: {
7139                 struct md_device        *next = mdt->mdt_child;
7140                 struct obd_ioctl_data   *data = karg;
7141                 struct lfsck_stop        stop;
7142
7143                 stop.ls_status = LS_STOPPED;
7144                 /* Old lfsck utils may pass NULL @stop. */
7145                 if (data->ioc_inlbuf1 == NULL)
7146                         stop.ls_flags = 0;
7147                 else
7148                         stop.ls_flags =
7149                         ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
7150
7151                 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
7152                 break;
7153         }
7154         case OBD_IOC_QUERY_LFSCK: {
7155                 struct md_device        *next = mdt->mdt_child;
7156                 struct obd_ioctl_data   *data = karg;
7157
7158                 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
7159                                                  data->ioc_inlbuf1);
7160                 break;
7161         }
7162         case OBD_IOC_GET_OBJ_VERSION: {
7163                 struct mdt_thread_info *mti;
7164                 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7165                 memset(mti, 0, sizeof *mti);
7166                 mti->mti_env = &env;
7167                 mti->mti_mdt = mdt;
7168                 mti->mti_exp = exp;
7169
7170                 rc = mdt_ioc_version_get(mti, karg);
7171                 break;
7172         }
7173         case OBD_IOC_CATLOGLIST: {
7174                 struct mdt_thread_info *mti;
7175
7176                 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7177                 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
7178                 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
7179                                        &mti->mti_tmp_fid1);
7180                 break;
7181          }
7182         default:
7183                 rc = -EOPNOTSUPP;
7184                 CERROR("%s: Not supported cmd = %d, rc = %d\n",
7185                         mdt_obd_name(mdt), cmd, rc);
7186         }
7187
7188         lu_env_fini(&env);
7189         RETURN(rc);
7190 }
7191
7192 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
7193 {
7194         struct lu_device *ld = md2lu_dev(mdt->mdt_child);
7195         int rc;
7196         ENTRY;
7197
7198         if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
7199                 struct lfsck_start_param lsp;
7200
7201                 lsp.lsp_start = NULL;
7202                 lsp.lsp_index_valid = 0;
7203                 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
7204                                                            OBD_IOC_START_LFSCK,
7205                                                            0, &lsp);
7206                 if (rc != 0 && rc != -EALREADY)
7207                         CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
7208                               mdt_obd_name(mdt), rc);
7209         }
7210
7211         rc = ld->ld_ops->ldo_recovery_complete(env, ld);
7212         RETURN(rc);
7213 }
7214
7215 static int mdt_obd_postrecov(struct obd_device *obd)
7216 {
7217         struct lu_env env;
7218         int rc;
7219
7220         rc = lu_env_init(&env, LCT_MD_THREAD);
7221         if (rc)
7222                 RETURN(rc);
7223         rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
7224         lu_env_fini(&env);
7225         return rc;
7226 }
7227
7228 static const struct obd_ops mdt_obd_device_ops = {
7229         .o_owner          = THIS_MODULE,
7230         .o_set_info_async = mdt_obd_set_info_async,
7231         .o_connect        = mdt_obd_connect,
7232         .o_reconnect      = mdt_obd_reconnect,
7233         .o_disconnect     = mdt_obd_disconnect,
7234         .o_init_export    = mdt_init_export,
7235         .o_destroy_export = mdt_destroy_export,
7236         .o_iocontrol      = mdt_iocontrol,
7237         .o_postrecov      = mdt_obd_postrecov,
7238         /* Data-on-MDT IO methods */
7239         .o_preprw         = mdt_obd_preprw,
7240         .o_commitrw       = mdt_obd_commitrw,
7241 };
7242
7243 static struct lu_device* mdt_device_fini(const struct lu_env *env,
7244                                          struct lu_device *d)
7245 {
7246         struct mdt_device *m = mdt_dev(d);
7247         ENTRY;
7248
7249         mdt_fini(env, m);
7250         RETURN(NULL);
7251 }
7252
7253 static struct lu_device *mdt_device_free(const struct lu_env *env,
7254                                          struct lu_device *d)
7255 {
7256         struct mdt_device *m = mdt_dev(d);
7257         ENTRY;
7258
7259         lu_device_fini(&m->mdt_lu_dev);
7260         OBD_FREE_PTR(m);
7261
7262         RETURN(NULL);
7263 }
7264
7265 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
7266                                           struct lu_device_type *t,
7267                                           struct lustre_cfg *cfg)
7268 {
7269         struct lu_device  *l;
7270         struct mdt_device *m;
7271
7272         OBD_ALLOC_PTR(m);
7273         if (m != NULL) {
7274                 int rc;
7275
7276                 l = &m->mdt_lu_dev;
7277                 rc = mdt_init0(env, m, t, cfg);
7278                 if (rc != 0) {
7279                         mdt_device_free(env, l);
7280                         l = ERR_PTR(rc);
7281                         return l;
7282                 }
7283         } else
7284                 l = ERR_PTR(-ENOMEM);
7285         return l;
7286 }
7287
7288 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
7289 LU_KEY_INIT(mdt, struct mdt_thread_info);
7290
7291 static void mdt_key_fini(const struct lu_context *ctx,
7292                          struct lu_context_key *key, void* data)
7293 {
7294         struct mdt_thread_info *info = data;
7295
7296         if (info->mti_big_lmm) {
7297                 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
7298                 info->mti_big_lmm = NULL;
7299                 info->mti_big_lmmsize = 0;
7300         }
7301
7302         if (info->mti_big_acl) {
7303                 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
7304                 info->mti_big_acl = NULL;
7305                 info->mti_big_aclsize = 0;
7306         }
7307
7308         OBD_FREE_PTR(info);
7309 }
7310
7311 /* context key: mdt_thread_key */
7312 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
7313
7314 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
7315 {
7316         return lu_ucred(info->mti_env);
7317 }
7318
7319 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
7320 {
7321         return lu_ucred_check(info->mti_env);
7322 }
7323
7324 /**
7325  * Enable/disable COS (Commit On Sharing).
7326  *
7327  * Set/Clear the COS flag in mdt options.
7328  *
7329  * \param mdt mdt device
7330  * \param val 0 disables COS, other values enable COS
7331  */
7332 void mdt_enable_cos(struct mdt_device *mdt, bool val)
7333 {
7334         struct lu_env env;
7335         int rc;
7336
7337         mdt->mdt_opts.mo_cos = val;
7338         rc = lu_env_init(&env, LCT_LOCAL);
7339         if (unlikely(rc != 0)) {
7340                 CWARN("%s: lu_env initialization failed, cannot "
7341                       "sync: rc = %d\n", mdt_obd_name(mdt), rc);
7342                 return;
7343         }
7344         mdt_device_sync(&env, mdt);
7345         lu_env_fini(&env);
7346 }
7347
7348 /**
7349  * Check COS (Commit On Sharing) status.
7350  *
7351  * Return COS flag status.
7352  *
7353  * \param mdt mdt device
7354  */
7355 int mdt_cos_is_enabled(struct mdt_device *mdt)
7356 {
7357         return mdt->mdt_opts.mo_cos != 0;
7358 }
7359
7360 static const struct lu_device_type_operations mdt_device_type_ops = {
7361         .ldto_device_alloc = mdt_device_alloc,
7362         .ldto_device_free  = mdt_device_free,
7363         .ldto_device_fini  = mdt_device_fini
7364 };
7365
7366 static struct lu_device_type mdt_device_type = {
7367         .ldt_tags     = LU_DEVICE_MD,
7368         .ldt_name     = LUSTRE_MDT_NAME,
7369         .ldt_ops      = &mdt_device_type_ops,
7370         .ldt_ctx_tags = LCT_MD_THREAD
7371 };
7372
7373 static int __init mdt_init(void)
7374 {
7375         int rc;
7376
7377         BUILD_BUG_ON(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") !=
7378                      FID_NOBRACE_LEN + 1);
7379         BUILD_BUG_ON(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") !=
7380                      FID_LEN + 1);
7381         rc = lu_kmem_init(mdt_caches);
7382         if (rc)
7383                 return rc;
7384
7385         rc = mds_mod_init();
7386         if (rc)
7387                 GOTO(lu_fini, rc);
7388
7389         rc = class_register_type(&mdt_obd_device_ops, NULL, true,
7390                                  LUSTRE_MDT_NAME, &mdt_device_type);
7391         if (rc)
7392                 GOTO(mds_fini, rc);
7393 lu_fini:
7394         if (rc)
7395                 lu_kmem_fini(mdt_caches);
7396 mds_fini:
7397         if (rc)
7398                 mds_mod_exit();
7399         return rc;
7400 }
7401
7402 static void __exit mdt_exit(void)
7403 {
7404         class_unregister_type(LUSTRE_MDT_NAME);
7405         mds_mod_exit();
7406         lu_kmem_fini(mdt_caches);
7407 }
7408
7409 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7410 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
7411 MODULE_VERSION(LUSTRE_VERSION_STRING);
7412 MODULE_LICENSE("GPL");
7413
7414 module_init(mdt_init);
7415 module_exit(mdt_exit);