Whamcloud - gitweb
9f34149d4d5f5b7e6073c3f0314926b60aa82e2a
[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, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdt/mdt_handler.c
33  *
34  * Lustre Metadata Target (mdt) request handler
35  *
36  * Author: Peter Braam <braam@clusterfs.com>
37  * Author: Andreas Dilger <adilger@clusterfs.com>
38  * Author: Phil Schwan <phil@clusterfs.com>
39  * Author: Mike Shaver <shaver@clusterfs.com>
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  * Author: Huang Hua <huanghua@clusterfs.com>
42  * Author: Yury Umanets <umka@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_MDS
46
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
49
50 #include <dt_object.h>
51 #include <lustre_acl.h>
52 #include <lustre_export.h>
53 #include <uapi/linux/lustre/lustre_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_nodemap.h>
57 #include <lustre_mds.h>
58 #include <uapi/linux/lustre/lustre_param.h>
59 #include <lustre_quota.h>
60 #include <lustre_swab.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
70 static unsigned int max_mod_rpcs_per_client = 8;
71 module_param(max_mod_rpcs_per_client, uint, 0644);
72 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
73
74 mdl_mode_t mdt_mdl_lock_modes[] = {
75         [LCK_MINMODE] = MDL_MINMODE,
76         [LCK_EX]      = MDL_EX,
77         [LCK_PW]      = MDL_PW,
78         [LCK_PR]      = MDL_PR,
79         [LCK_CW]      = MDL_CW,
80         [LCK_CR]      = MDL_CR,
81         [LCK_NL]      = MDL_NL,
82         [LCK_GROUP]   = MDL_GROUP
83 };
84
85 enum ldlm_mode mdt_dlm_lock_modes[] = {
86         [MDL_MINMODE]   = LCK_MINMODE,
87         [MDL_EX]        = LCK_EX,
88         [MDL_PW]        = LCK_PW,
89         [MDL_PR]        = LCK_PR,
90         [MDL_CW]        = LCK_CW,
91         [MDL_CR]        = LCK_CR,
92         [MDL_NL]        = LCK_NL,
93         [MDL_GROUP]     = LCK_GROUP
94 };
95
96 static struct mdt_device *mdt_dev(struct lu_device *d);
97 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
98
99 static const struct lu_object_operations mdt_obj_ops;
100
101 /* Slab for MDT object allocation */
102 static struct kmem_cache *mdt_object_kmem;
103
104 /* For HSM restore handles */
105 struct kmem_cache *mdt_hsm_cdt_kmem;
106
107 /* For HSM request handles */
108 struct kmem_cache *mdt_hsm_car_kmem;
109
110 static struct lu_kmem_descr mdt_caches[] = {
111         {
112                 .ckd_cache = &mdt_object_kmem,
113                 .ckd_name  = "mdt_obj",
114                 .ckd_size  = sizeof(struct mdt_object)
115         },
116         {
117                 .ckd_cache      = &mdt_hsm_cdt_kmem,
118                 .ckd_name       = "mdt_cdt_restore_handle",
119                 .ckd_size       = sizeof(struct cdt_restore_handle)
120         },
121         {
122                 .ckd_cache      = &mdt_hsm_car_kmem,
123                 .ckd_name       = "mdt_cdt_agent_req",
124                 .ckd_size       = sizeof(struct cdt_agent_req)
125         },
126         {
127                 .ckd_cache = NULL
128         }
129 };
130
131 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
132 {
133         if (!rep)
134                 return 0;
135         return rep->lock_policy_res1 & op_flag;
136 }
137
138 void mdt_clear_disposition(struct mdt_thread_info *info,
139                            struct ldlm_reply *rep, __u64 op_flag)
140 {
141         if (info) {
142                 info->mti_opdata &= ~op_flag;
143                 tgt_opdata_clear(info->mti_env, op_flag);
144         }
145         if (rep)
146                 rep->lock_policy_res1 &= ~op_flag;
147 }
148
149 void mdt_set_disposition(struct mdt_thread_info *info,
150                          struct ldlm_reply *rep, __u64 op_flag)
151 {
152         if (info) {
153                 info->mti_opdata |= op_flag;
154                 tgt_opdata_set(info->mti_env, op_flag);
155         }
156         if (rep)
157                 rep->lock_policy_res1 |= op_flag;
158 }
159
160 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
161 {
162         lh->mlh_pdo_hash = 0;
163         lh->mlh_reg_mode = lm;
164         lh->mlh_rreg_mode = lm;
165         lh->mlh_type = MDT_REG_LOCK;
166 }
167
168 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
169                        const struct lu_name *lname)
170 {
171         lh->mlh_reg_mode = lock_mode;
172         lh->mlh_pdo_mode = LCK_MINMODE;
173         lh->mlh_rreg_mode = lock_mode;
174         lh->mlh_type = MDT_PDO_LOCK;
175
176         if (lu_name_is_valid(lname)) {
177                 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
178                                                      lname->ln_namelen);
179                 /* XXX Workaround for LU-2856
180                  *
181                  * Zero is a valid return value of full_name_hash, but
182                  * several users of mlh_pdo_hash assume a non-zero
183                  * hash value. We therefore map zero onto an
184                  * arbitrary, but consistent value (1) to avoid
185                  * problems further down the road. */
186                 if (unlikely(lh->mlh_pdo_hash == 0))
187                         lh->mlh_pdo_hash = 1;
188         } else {
189                 lh->mlh_pdo_hash = 0;
190         }
191 }
192
193 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
194                               struct mdt_lock_handle *lh)
195 {
196         mdl_mode_t mode;
197         ENTRY;
198
199         /*
200          * Any dir access needs couple of locks:
201          *
202          * 1) on part of dir we gonna take lookup/modify;
203          *
204          * 2) on whole dir to protect it from concurrent splitting and/or to
205          * flush client's cache for readdir().
206          *
207          * so, for a given mode and object this routine decides what lock mode
208          * to use for lock #2:
209          *
210          * 1) if caller's gonna lookup in dir then we need to protect dir from
211          * being splitted only - LCK_CR
212          *
213          * 2) if caller's gonna modify dir then we need to protect dir from
214          * being splitted and to flush cache - LCK_CW
215          *
216          * 3) if caller's gonna modify dir and that dir seems ready for
217          * splitting then we need to protect it from any type of access
218          * (lookup/modify/split) - LCK_EX --bzzz
219          */
220
221         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
222         LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
223
224         /*
225          * Ask underlaying level its opinion about preferable PDO lock mode
226          * having access type passed as regular lock mode:
227          *
228          * - MDL_MINMODE means that lower layer does not want to specify lock
229          * mode;
230          *
231          * - MDL_NL means that no PDO lock should be taken. This is used in some
232          * cases. Say, for non-splittable directories no need to use PDO locks
233          * at all.
234          */
235         mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
236                              mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
237
238         if (mode != MDL_MINMODE) {
239                 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
240         } else {
241                 /*
242                  * Lower layer does not want to specify locking mode. We do it
243                  * our selves. No special protection is needed, just flush
244                  * client's cache on modification and allow concurrent
245                  * mondification.
246                  */
247                 switch (lh->mlh_reg_mode) {
248                 case LCK_EX:
249                         lh->mlh_pdo_mode = LCK_EX;
250                         break;
251                 case LCK_PR:
252                         lh->mlh_pdo_mode = LCK_CR;
253                         break;
254                 case LCK_PW:
255                         lh->mlh_pdo_mode = LCK_CW;
256                         break;
257                 default:
258                         CERROR("Not expected lock type (0x%x)\n",
259                                (int)lh->mlh_reg_mode);
260                         LBUG();
261                 }
262         }
263
264         LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
265         EXIT;
266 }
267
268 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
269                               struct lu_fid *fid)
270 {
271         struct mdt_device *mdt = info->mti_mdt;
272         struct lu_name *lname = &info->mti_name;
273         char *name = NULL;
274         struct mdt_object *parent;
275         u32 mode;
276         int rc = 0;
277
278         LASSERT(!info->mti_cross_ref);
279
280         OBD_ALLOC(name, NAME_MAX + 1);
281         if (name == NULL)
282                 return -ENOMEM;
283         lname->ln_name = name;
284
285         /*
286          * We may want to allow this to mount a completely separate
287          * fileset from the MDT in the future, but keeping it to
288          * ROOT/ only for now avoid potential security issues.
289          */
290         *fid = mdt->mdt_md_root_fid;
291
292         while (rc == 0 && fileset != NULL && *fileset != '\0') {
293                 const char *s1 = fileset;
294                 const char *s2;
295
296                 while (*++s1 == '/')
297                         ;
298                 s2 = s1;
299                 while (*s2 != '/' && *s2 != '\0')
300                         s2++;
301
302                 if (s2 == s1)
303                         break;
304
305                 fileset = s2;
306
307                 lname->ln_namelen = s2 - s1;
308                 if (lname->ln_namelen > NAME_MAX) {
309                         rc = -EINVAL;
310                         break;
311                 }
312
313                 /* reject .. as a path component */
314                 if (lname->ln_namelen == 2 &&
315                     strncmp(s1, "..", 2) == 0) {
316                         rc = -EINVAL;
317                         break;
318                 }
319
320                 strncpy(name, s1, lname->ln_namelen);
321                 name[lname->ln_namelen] = '\0';
322
323                 parent = mdt_object_find(info->mti_env, mdt, fid);
324                 if (IS_ERR(parent)) {
325                         rc = PTR_ERR(parent);
326                         break;
327                 }
328                 /* Only got the fid of this obj by name */
329                 fid_zero(fid);
330                 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
331                                 fid, &info->mti_spec);
332                 mdt_object_put(info->mti_env, parent);
333         }
334         if (!rc) {
335                 parent = mdt_object_find(info->mti_env, mdt, fid);
336                 if (IS_ERR(parent))
337                         rc = PTR_ERR(parent);
338                 else {
339                         mode = lu_object_attr(&parent->mot_obj);
340                         mdt_object_put(info->mti_env, parent);
341                         if (!S_ISDIR(mode))
342                                 rc = -ENOTDIR;
343                 }
344         }
345
346         OBD_FREE(name, NAME_MAX + 1);
347
348         return rc;
349 }
350
351 static int mdt_get_root(struct tgt_session_info *tsi)
352 {
353         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
354         struct mdt_device       *mdt = info->mti_mdt;
355         struct mdt_body         *repbody;
356         char                    *fileset = NULL, *buffer = NULL;
357         int                      rc;
358         struct obd_export       *exp = info->mti_exp;
359         char                    *nodemap_fileset;
360
361         ENTRY;
362
363         rc = mdt_check_ucred(info);
364         if (rc)
365                 GOTO(out, rc = err_serious(rc));
366
367         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
368                 GOTO(out, rc = err_serious(-ENOMEM));
369
370         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
371         if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
372                 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
373                 if (fileset == NULL)
374                         GOTO(out, rc = err_serious(-EFAULT));
375         }
376
377         nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
378         if (nodemap_fileset && nodemap_fileset[0]) {
379                 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
380                 if (fileset) {
381                         /* consider fileset from client as a sub-fileset
382                          * of the nodemap one */
383                         OBD_ALLOC(buffer, PATH_MAX + 1);
384                         if (buffer == NULL)
385                                 GOTO(out, rc = err_serious(-ENOMEM));
386                         if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
387                                      nodemap_fileset, fileset) >= PATH_MAX + 1)
388                                 GOTO(out, rc = err_serious(-EINVAL));
389                         fileset = buffer;
390                 } else {
391                         /* enforce fileset as specified in the nodemap */
392                         fileset = nodemap_fileset;
393                 }
394         }
395
396         if (fileset) {
397                 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
398                 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
399                 if (rc < 0)
400                         GOTO(out, rc = err_serious(rc));
401         } else {
402                 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
403         }
404         repbody->mbo_valid |= OBD_MD_FLID;
405
406         EXIT;
407 out:
408         mdt_thread_info_fini(info);
409         if (buffer)
410                 OBD_FREE(buffer, PATH_MAX+1);
411         return rc;
412 }
413
414 static int mdt_statfs(struct tgt_session_info *tsi)
415 {
416         struct ptlrpc_request           *req = tgt_ses_req(tsi);
417         struct mdt_thread_info          *info = tsi2mdt_info(tsi);
418         struct mdt_device               *mdt = info->mti_mdt;
419         struct tg_grants_data           *tgd = &mdt->mdt_lut.lut_tgd;
420         struct ptlrpc_service_part      *svcpt;
421         struct obd_statfs               *osfs;
422         int                             rc;
423
424         ENTRY;
425
426         svcpt = req->rq_rqbd->rqbd_svcpt;
427
428         /* This will trigger a watchdog timeout */
429         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
430                          (MDT_SERVICE_WATCHDOG_FACTOR *
431                           at_get(&svcpt->scp_at_estimate)) + 1);
432
433         rc = mdt_check_ucred(info);
434         if (rc)
435                 GOTO(out, rc = err_serious(rc));
436
437         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
438                 GOTO(out, rc = err_serious(-ENOMEM));
439
440         osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
441         if (!osfs)
442                 GOTO(out, rc = -EPROTO);
443
444         rc = tgt_statfs_internal(tsi->tsi_env, &mdt->mdt_lut, osfs,
445                                  cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
446                                  NULL);
447         if (unlikely(rc))
448                 GOTO(out, rc);
449
450         /* at least try to account for cached pages.  its still racy and
451          * might be under-reporting if clients haven't announced their
452          * caches with brw recently */
453         CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
454                " pending %llu free %llu avail %llu\n",
455                tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
456                tgd->tgd_tot_pending,
457                osfs->os_bfree << tgd->tgd_blockbits,
458                osfs->os_bavail << tgd->tgd_blockbits);
459
460         osfs->os_bavail -= min_t(u64, osfs->os_bavail,
461                                  ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
462                                    osfs->os_bsize - 1) >> tgd->tgd_blockbits));
463
464         tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
465         CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
466                "%llu objects: %llu free; state %x\n",
467                osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
468                osfs->os_files, osfs->os_ffree, osfs->os_state);
469
470         if (!exp_grant_param_supp(tsi->tsi_exp) &&
471             tgd->tgd_blockbits > COMPAT_BSIZE_SHIFT) {
472                 /* clients which don't support OBD_CONNECT_GRANT_PARAM
473                  * should not see a block size > page size, otherwise
474                  * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
475                  * block size which is the biggest block size known to work
476                  * with all client's page size. */
477                 osfs->os_blocks <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
478                 osfs->os_bfree  <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
479                 osfs->os_bavail <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
480                 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
481         }
482         if (rc == 0)
483                 mdt_counter_incr(req, LPROC_MDT_STATFS);
484 out:
485         mdt_thread_info_fini(info);
486         RETURN(rc);
487 }
488
489 /**
490  * Pack size attributes into the reply.
491  */
492 int mdt_pack_size2body(struct mdt_thread_info *info,
493                         const struct lu_fid *fid, bool dom_lock)
494 {
495         struct mdt_body *b;
496         struct md_attr *ma = &info->mti_attr;
497         int dom_stripe;
498
499         ENTRY;
500
501         LASSERT(ma->ma_attr.la_valid & LA_MODE);
502
503         if (!S_ISREG(ma->ma_attr.la_mode) ||
504             !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
505                 RETURN(-ENODATA);
506
507         dom_stripe = mdt_lmm_dom_entry(ma->ma_lmm);
508         /* no DoM stripe, no size in reply */
509         if (dom_stripe == LMM_NO_DOM)
510                 RETURN(-ENOENT);
511
512         /* no DoM lock, no size in reply */
513         if (!dom_lock)
514                 RETURN(0);
515
516         /* Either DoM lock exists or LMM has only DoM stripe then
517          * return size on body. */
518         b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
519
520         mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
521         RETURN(0);
522 }
523
524 #ifdef CONFIG_FS_POSIX_ACL
525 /*
526  * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
527  *
528  * \param       info    thread info object
529  * \param       repbody reply to pack ACLs into
530  * \param       o       mdt object of file to examine
531  * \param       nodemap nodemap of client to reply to
532  * \retval      0       success
533  * \retval      -errno  error getting or parsing ACL from disk
534  */
535 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
536                       struct mdt_object *o, struct lu_nodemap *nodemap)
537 {
538         const struct lu_env     *env = info->mti_env;
539         struct md_object        *next = mdt_object_child(o);
540         struct lu_buf           *buf = &info->mti_buf;
541         struct mdt_device       *mdt = info->mti_mdt;
542         int rc;
543
544         ENTRY;
545
546         buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
547         buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
548                                            RCL_SERVER);
549         if (buf->lb_len == 0)
550                 RETURN(0);
551
552 again:
553         rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
554         if (rc < 0) {
555                 if (rc == -ENODATA) {
556                         repbody->mbo_aclsize = 0;
557                         repbody->mbo_valid |= OBD_MD_FLACL;
558                         rc = 0;
559                 } else if (rc == -EOPNOTSUPP) {
560                         rc = 0;
561                 } else {
562                         if (rc == -ERANGE &&
563                             exp_connect_large_acl(info->mti_exp) &&
564                             buf->lb_buf != info->mti_big_acl) {
565                                 if (info->mti_big_acl == NULL) {
566                                         OBD_ALLOC_LARGE(info->mti_big_acl,
567                                                         mdt->mdt_max_ea_size);
568                                         if (info->mti_big_acl == NULL) {
569                                                 CERROR("%s: unable to grow "
570                                                        DFID" ACL buffer\n",
571                                                        mdt_obd_name(mdt),
572                                                        PFID(mdt_object_fid(o)));
573                                                 RETURN(-ENOMEM);
574                                         }
575
576                                         info->mti_big_aclsize =
577                                                         mdt->mdt_max_ea_size;
578                                 }
579
580                                 CDEBUG(D_INODE, "%s: grow the "DFID
581                                        " ACL buffer to size %d\n",
582                                        mdt_obd_name(mdt),
583                                        PFID(mdt_object_fid(o)),
584                                        mdt->mdt_max_ea_size);
585
586                                 buf->lb_buf = info->mti_big_acl;
587                                 buf->lb_len = info->mti_big_aclsize;
588
589                                 goto again;
590                         }
591
592                         CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
593                                mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
594                 }
595         } else {
596                 if (buf->lb_buf == info->mti_big_acl)
597                         info->mti_big_acl_used = 1;
598
599                 rc = nodemap_map_acl(nodemap, buf->lb_buf,
600                                      rc, NODEMAP_FS_TO_CLIENT);
601                 /* if all ACLs mapped out, rc is still >= 0 */
602                 if (rc < 0) {
603                         CERROR("%s: nodemap_map_acl unable to parse "DFID
604                                " ACL: rc = %d\n", mdt_obd_name(mdt),
605                                PFID(mdt_object_fid(o)), rc);
606                 } else {
607                         repbody->mbo_aclsize = rc;
608                         repbody->mbo_valid |= OBD_MD_FLACL;
609                         rc = 0;
610                 }
611         }
612
613         RETURN(rc);
614 }
615 #endif
616
617 /* XXX Look into layout in MDT layer. */
618 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
619 {
620         struct lov_comp_md_v1   *comp_v1;
621         struct lov_mds_md       *v1;
622         int                      i;
623
624         if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
625                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
626
627                 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
628                         v1 = (struct lov_mds_md *)((char *)comp_v1 +
629                                 comp_v1->lcm_entries[i].lcme_offset);
630                         /* We don't support partial release for now */
631                         if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
632                                 return false;
633                 }
634                 return true;
635         } else {
636                 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
637                         true : false;
638         }
639 }
640
641 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
642                         const struct lu_attr *attr, const struct lu_fid *fid)
643 {
644         struct md_attr *ma = &info->mti_attr;
645         struct obd_export *exp = info->mti_exp;
646         struct lu_nodemap *nodemap = NULL;
647
648         LASSERT(ma->ma_valid & MA_INODE);
649
650         if (attr->la_valid & LA_ATIME) {
651                 b->mbo_atime = attr->la_atime;
652                 b->mbo_valid |= OBD_MD_FLATIME;
653         }
654         if (attr->la_valid & LA_MTIME) {
655                 b->mbo_mtime = attr->la_mtime;
656                 b->mbo_valid |= OBD_MD_FLMTIME;
657         }
658         if (attr->la_valid & LA_CTIME) {
659                 b->mbo_ctime = attr->la_ctime;
660                 b->mbo_valid |= OBD_MD_FLCTIME;
661         }
662         if (attr->la_valid & LA_FLAGS) {
663                 b->mbo_flags = attr->la_flags;
664                 b->mbo_valid |= OBD_MD_FLFLAGS;
665         }
666         if (attr->la_valid & LA_NLINK) {
667                 b->mbo_nlink = attr->la_nlink;
668                 b->mbo_valid |= OBD_MD_FLNLINK;
669         }
670         if (attr->la_valid & (LA_UID|LA_GID)) {
671                 nodemap = nodemap_get_from_exp(exp);
672                 if (IS_ERR(nodemap))
673                         goto out;
674         }
675         if (attr->la_valid & LA_UID) {
676                 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
677                                             NODEMAP_FS_TO_CLIENT,
678                                             attr->la_uid);
679                 b->mbo_valid |= OBD_MD_FLUID;
680         }
681         if (attr->la_valid & LA_GID) {
682                 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
683                                             NODEMAP_FS_TO_CLIENT,
684                                             attr->la_gid);
685                 b->mbo_valid |= OBD_MD_FLGID;
686         }
687
688         if (attr->la_valid & LA_PROJID) {
689                 /* TODO, nodemap for project id */
690                 b->mbo_projid = attr->la_projid;
691                 b->mbo_valid |= OBD_MD_FLPROJID;
692         }
693
694         b->mbo_mode = attr->la_mode;
695         if (attr->la_valid & LA_MODE)
696                 b->mbo_valid |= OBD_MD_FLMODE;
697         if (attr->la_valid & LA_TYPE)
698                 b->mbo_valid |= OBD_MD_FLTYPE;
699
700         if (fid != NULL) {
701                 b->mbo_fid1 = *fid;
702                 b->mbo_valid |= OBD_MD_FLID;
703                 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
704                        PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
705         }
706
707         if (!(attr->la_valid & LA_TYPE))
708                 return;
709
710         b->mbo_rdev   = attr->la_rdev;
711         b->mbo_size   = attr->la_size;
712         b->mbo_blocks = attr->la_blocks;
713
714         if (!S_ISREG(attr->la_mode)) {
715                 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
716         } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
717                 /* means no objects are allocated on osts. */
718                 LASSERT(!(ma->ma_valid & MA_LOV));
719                 /* just ignore blocks occupied by extend attributes on MDS */
720                 b->mbo_blocks = 0;
721                 /* if no object is allocated on osts, the size on mds is valid.
722                  * b=22272 */
723                 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
724         } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
725                 if (mdt_hsm_is_released(ma->ma_lmm)) {
726                         /* A released file stores its size on MDS. */
727                         /* But return 1 block for released file, unless tools
728                          * like tar will consider it fully sparse. (LU-3864)
729                          */
730                         if (unlikely(b->mbo_size == 0))
731                                 b->mbo_blocks = 0;
732                         else
733                                 b->mbo_blocks = 1;
734                         b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
735                 } else if (info->mti_som_valid) { /* som is valid */
736                         b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
737                 }
738         }
739
740         if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
741                 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
742                        PFID(fid), (unsigned long long)b->mbo_size);
743
744 out:
745         if (!IS_ERR_OR_NULL(nodemap))
746                 nodemap_putref(nodemap);
747 }
748
749 static inline int mdt_body_has_lov(const struct lu_attr *la,
750                                    const struct mdt_body *body)
751 {
752         return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
753                (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
754 }
755
756 void mdt_client_compatibility(struct mdt_thread_info *info)
757 {
758         struct mdt_body       *body;
759         struct ptlrpc_request *req = mdt_info_req(info);
760         struct obd_export     *exp = req->rq_export;
761         struct md_attr        *ma = &info->mti_attr;
762         struct lu_attr        *la = &ma->ma_attr;
763         ENTRY;
764
765         if (exp_connect_layout(exp))
766                 /* the client can deal with 16-bit lmm_stripe_count */
767                 RETURN_EXIT;
768
769         body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
770
771         if (!mdt_body_has_lov(la, body))
772                 RETURN_EXIT;
773
774         /* now we have a reply with a lov for a client not compatible with the
775          * layout lock so we have to clean the layout generation number */
776         if (S_ISREG(la->la_mode))
777                 ma->ma_lmm->lmm_layout_gen = 0;
778         EXIT;
779 }
780
781 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
782                                    struct mdt_object *o)
783 {
784         const struct lu_env *env = info->mti_env;
785         int rc, rc2;
786
787         rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
788                           XATTR_NAME_LOV);
789
790         if (rc == -ENODATA)
791                 rc = 0;
792
793         if (rc < 0)
794                 goto out;
795
796         /* Is it a directory? Let's check for the LMV as well */
797         if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
798                 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
799                                    XATTR_NAME_LMV);
800
801                 if (rc2 == -ENODATA)
802                         rc2 = mo_xattr_get(env, mdt_object_child(o),
803                                            &LU_BUF_NULL,
804                                            XATTR_NAME_DEFAULT_LMV);
805
806                 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
807                         rc = rc2;
808         }
809
810 out:
811         return rc;
812 }
813
814 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
815                       const char *name)
816 {
817         const struct lu_env *env = info->mti_env;
818         int rc;
819         ENTRY;
820
821         LASSERT(info->mti_big_lmm_used == 0);
822         rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
823         if (rc < 0)
824                 RETURN(rc);
825
826         /* big_lmm may need to be grown */
827         if (info->mti_big_lmmsize < rc) {
828                 int size = size_roundup_power2(rc);
829
830                 if (info->mti_big_lmmsize > 0) {
831                         /* free old buffer */
832                         LASSERT(info->mti_big_lmm);
833                         OBD_FREE_LARGE(info->mti_big_lmm,
834                                        info->mti_big_lmmsize);
835                         info->mti_big_lmm = NULL;
836                         info->mti_big_lmmsize = 0;
837                 }
838
839                 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
840                 if (info->mti_big_lmm == NULL)
841                         RETURN(-ENOMEM);
842                 info->mti_big_lmmsize = size;
843         }
844         LASSERT(info->mti_big_lmmsize >= rc);
845
846         info->mti_buf.lb_buf = info->mti_big_lmm;
847         info->mti_buf.lb_len = info->mti_big_lmmsize;
848         rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
849
850         RETURN(rc);
851 }
852
853 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
854                    struct md_attr *ma, const char *name)
855 {
856         struct md_object *next = mdt_object_child(o);
857         struct lu_buf    *buf = &info->mti_buf;
858         int rc;
859
860         if (strcmp(name, XATTR_NAME_LOV) == 0) {
861                 buf->lb_buf = ma->ma_lmm;
862                 buf->lb_len = ma->ma_lmm_size;
863                 LASSERT(!(ma->ma_valid & MA_LOV));
864         } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
865                 buf->lb_buf = ma->ma_lmv;
866                 buf->lb_len = ma->ma_lmv_size;
867                 LASSERT(!(ma->ma_valid & MA_LMV));
868         } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
869                 buf->lb_buf = ma->ma_lmv;
870                 buf->lb_len = ma->ma_lmv_size;
871                 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
872         } else {
873                 return -EINVAL;
874         }
875
876         rc = mo_xattr_get(info->mti_env, next, buf, name);
877         if (rc > 0) {
878
879 got:
880                 if (strcmp(name, XATTR_NAME_LOV) == 0) {
881                         if (info->mti_big_lmm_used)
882                                 ma->ma_lmm = info->mti_big_lmm;
883
884                         /* NOT return LOV EA with hole to old client. */
885                         if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
886                                      LOV_PATTERN_F_HOLE) &&
887                             !(exp_connect_flags(info->mti_exp) &
888                               OBD_CONNECT_LFSCK)) {
889                                 return -EIO;
890                         } else {
891                                 ma->ma_lmm_size = rc;
892                                 ma->ma_valid |= MA_LOV;
893                         }
894                 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
895                         if (info->mti_big_lmm_used)
896                                 ma->ma_lmv = info->mti_big_lmm;
897
898                         ma->ma_lmv_size = rc;
899                         ma->ma_valid |= MA_LMV;
900                 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
901                         ma->ma_lmv_size = rc;
902                         ma->ma_valid |= MA_LMV_DEF;
903                 }
904
905                 /* Update mdt_max_mdsize so all clients will be aware that */
906                 if (info->mti_mdt->mdt_max_mdsize < rc)
907                         info->mti_mdt->mdt_max_mdsize = rc;
908
909                 rc = 0;
910         } else if (rc == -ENODATA) {
911                 /* no LOV EA */
912                 rc = 0;
913         } else if (rc == -ERANGE) {
914                 /* Default LMV has fixed size, so it must be able to fit
915                  * in the original buffer */
916                 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
917                         return rc;
918                 rc = mdt_big_xattr_get(info, o, name);
919                 if (rc > 0) {
920                         info->mti_big_lmm_used = 1;
921                         goto got;
922                 }
923         }
924
925         return rc;
926 }
927
928 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
929                              struct mdt_object *o, struct lu_fid *pfid)
930 {
931         struct lu_buf           *buf = &info->mti_buf;
932         struct link_ea_header   *leh;
933         struct link_ea_entry    *lee;
934         int                      rc;
935         ENTRY;
936
937         buf->lb_buf = info->mti_big_lmm;
938         buf->lb_len = info->mti_big_lmmsize;
939         rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
940                           buf, XATTR_NAME_LINK);
941         /* ignore errors, MA_PFID won't be set and it is
942          * up to the caller to treat this as an error */
943         if (rc == -ERANGE || buf->lb_len == 0) {
944                 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
945                 buf->lb_buf = info->mti_big_lmm;
946                 buf->lb_len = info->mti_big_lmmsize;
947         }
948
949         if (rc < 0)
950                 RETURN(rc);
951         if (rc < sizeof(*leh)) {
952                 CERROR("short LinkEA on "DFID": rc = %d\n",
953                        PFID(mdt_object_fid(o)), rc);
954                 RETURN(-ENODATA);
955         }
956
957         leh = (struct link_ea_header *) buf->lb_buf;
958         lee = (struct link_ea_entry *)(leh + 1);
959         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
960                 leh->leh_magic = LINK_EA_MAGIC;
961                 leh->leh_reccount = __swab32(leh->leh_reccount);
962                 leh->leh_len = __swab64(leh->leh_len);
963         }
964         if (leh->leh_magic != LINK_EA_MAGIC)
965                 RETURN(-EINVAL);
966         if (leh->leh_reccount == 0)
967                 RETURN(-ENODATA);
968
969         memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
970         fid_be_to_cpu(pfid, pfid);
971
972         RETURN(0);
973 }
974
975 int mdt_attr_get_complex(struct mdt_thread_info *info,
976                          struct mdt_object *o, struct md_attr *ma)
977 {
978         const struct lu_env *env = info->mti_env;
979         struct md_object    *next = mdt_object_child(o);
980         struct lu_buf       *buf = &info->mti_buf;
981         int                  need = ma->ma_need;
982         int                  rc = 0, rc2;
983         u32                  mode;
984         ENTRY;
985
986         ma->ma_valid = 0;
987
988         if (mdt_object_exists(o) == 0)
989                 GOTO(out, rc = -ENOENT);
990         mode = lu_object_attr(&next->mo_lu);
991
992         if (need & MA_INODE) {
993                 ma->ma_need = MA_INODE;
994                 rc = mo_attr_get(env, next, ma);
995                 if (rc)
996                         GOTO(out, rc);
997
998                 if (S_ISREG(mode))
999                         (void) mdt_get_som(info, o, &ma->ma_attr);
1000                 ma->ma_valid |= MA_INODE;
1001         }
1002
1003         if (need & MA_PFID) {
1004                 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1005                 if (rc == 0)
1006                         ma->ma_valid |= MA_PFID;
1007                 /* ignore this error, parent fid is not mandatory */
1008                 rc = 0;
1009         }
1010
1011         if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1012                 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1013                 if (rc)
1014                         GOTO(out, rc);
1015         }
1016
1017         if (need & MA_LMV && S_ISDIR(mode)) {
1018                 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1019                 if (rc != 0)
1020                         GOTO(out, rc);
1021         }
1022
1023         if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1024                 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1025                 if (rc != 0)
1026                         GOTO(out, rc);
1027         }
1028
1029         if (need & MA_HSM && S_ISREG(mode)) {
1030                 buf->lb_buf = info->mti_xattr_buf;
1031                 buf->lb_len = sizeof(info->mti_xattr_buf);
1032                 CLASSERT(sizeof(struct hsm_attrs) <=
1033                          sizeof(info->mti_xattr_buf));
1034                 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1035                 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1036                 if (rc2 == 0)
1037                         ma->ma_valid |= MA_HSM;
1038                 else if (rc2 < 0 && rc2 != -ENODATA)
1039                         GOTO(out, rc = rc2);
1040         }
1041
1042 #ifdef CONFIG_FS_POSIX_ACL
1043         if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1044                 buf->lb_buf = ma->ma_acl;
1045                 buf->lb_len = ma->ma_acl_size;
1046                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1047                 if (rc2 > 0) {
1048                         ma->ma_acl_size = rc2;
1049                         ma->ma_valid |= MA_ACL_DEF;
1050                 } else if (rc2 == -ENODATA) {
1051                         /* no ACLs */
1052                         ma->ma_acl_size = 0;
1053                 } else
1054                         GOTO(out, rc = rc2);
1055         }
1056 #endif
1057 out:
1058         ma->ma_need = need;
1059         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1060                rc, ma->ma_valid, ma->ma_lmm);
1061         RETURN(rc);
1062 }
1063
1064 static int mdt_getattr_internal(struct mdt_thread_info *info,
1065                                 struct mdt_object *o, int ma_need)
1066 {
1067         struct md_object        *next = mdt_object_child(o);
1068         const struct mdt_body   *reqbody = info->mti_body;
1069         struct ptlrpc_request   *req = mdt_info_req(info);
1070         struct md_attr          *ma = &info->mti_attr;
1071         struct lu_attr          *la = &ma->ma_attr;
1072         struct req_capsule      *pill = info->mti_pill;
1073         const struct lu_env     *env = info->mti_env;
1074         struct mdt_body         *repbody;
1075         struct lu_buf           *buffer = &info->mti_buf;
1076         struct obd_export       *exp = info->mti_exp;
1077         int                      rc;
1078         ENTRY;
1079
1080         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1081                 RETURN(err_serious(-ENOMEM));
1082
1083         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1084
1085         ma->ma_valid = 0;
1086
1087         if (mdt_object_remote(o)) {
1088                 /* This object is located on remote node.*/
1089                 /* Return -ENOTSUPP for old client */
1090                 if (!mdt_is_dne_client(req->rq_export))
1091                         GOTO(out, rc = -ENOTSUPP);
1092
1093                 repbody->mbo_fid1 = *mdt_object_fid(o);
1094                 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1095                 GOTO(out, rc = 0);
1096         }
1097
1098         if (reqbody->mbo_eadatasize > 0) {
1099                 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1100                 if (buffer->lb_buf == NULL)
1101                         GOTO(out, rc = -EPROTO);
1102                 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1103                                                       RCL_SERVER);
1104         } else {
1105                 buffer->lb_buf = NULL;
1106                 buffer->lb_len = 0;
1107                 ma_need &= ~(MA_LOV | MA_LMV);
1108                 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1109                        mdt_obd_name(info->mti_mdt),
1110                        req->rq_export->exp_client_uuid.uuid);
1111         }
1112
1113         /* If it is dir object and client require MEA, then we got MEA */
1114         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1115             (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1116                 /* Assumption: MDT_MD size is enough for lmv size. */
1117                 ma->ma_lmv = buffer->lb_buf;
1118                 ma->ma_lmv_size = buffer->lb_len;
1119                 ma->ma_need = MA_INODE;
1120                 if (ma->ma_lmv_size > 0) {
1121                         if (reqbody->mbo_valid & OBD_MD_MEA)
1122                                 ma->ma_need |= MA_LMV;
1123                         else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
1124                                 ma->ma_need |= MA_LMV_DEF;
1125                 }
1126         } else {
1127                 ma->ma_lmm = buffer->lb_buf;
1128                 ma->ma_lmm_size = buffer->lb_len;
1129                 ma->ma_need = MA_INODE | MA_HSM;
1130                 if (ma->ma_lmm_size > 0)
1131                         ma->ma_need |= MA_LOV;
1132         }
1133
1134         if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1135             reqbody->mbo_valid & OBD_MD_FLDIREA  &&
1136             lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1137                 /* get default stripe info for this dir. */
1138                 ma->ma_need |= MA_LOV_DEF;
1139         }
1140         ma->ma_need |= ma_need;
1141
1142         rc = mdt_attr_get_complex(info, o, ma);
1143         if (unlikely(rc)) {
1144                 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1145                        "%s: getattr error for "DFID": rc = %d\n",
1146                        mdt_obd_name(info->mti_mdt),
1147                        PFID(mdt_object_fid(o)), rc);
1148                 RETURN(rc);
1149         }
1150
1151         /* if file is released, check if a restore is running */
1152         if (ma->ma_valid & MA_HSM) {
1153                 repbody->mbo_valid |= OBD_MD_TSTATE;
1154                 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1155                     mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1156                         repbody->mbo_t_state = MS_RESTORE;
1157         }
1158
1159         if (likely(ma->ma_valid & MA_INODE))
1160                 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1161         else
1162                 RETURN(-EFAULT);
1163
1164         if (mdt_body_has_lov(la, reqbody)) {
1165                 if (ma->ma_valid & MA_LOV) {
1166                         LASSERT(ma->ma_lmm_size);
1167                         repbody->mbo_eadatasize = ma->ma_lmm_size;
1168                         if (S_ISDIR(la->la_mode))
1169                                 repbody->mbo_valid |= OBD_MD_FLDIREA;
1170                         else
1171                                 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1172                         mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1173                 }
1174                 if (ma->ma_valid & MA_LMV) {
1175                         /* Return -ENOTSUPP for old client */
1176                         if (!mdt_is_striped_client(req->rq_export))
1177                                 RETURN(-ENOTSUPP);
1178
1179                         LASSERT(S_ISDIR(la->la_mode));
1180                         mdt_dump_lmv(D_INFO, ma->ma_lmv);
1181                         repbody->mbo_eadatasize = ma->ma_lmv_size;
1182                         repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1183                 }
1184                 if (ma->ma_valid & MA_LMV_DEF) {
1185                         /* Return -ENOTSUPP for old client */
1186                         if (!mdt_is_striped_client(req->rq_export))
1187                                 RETURN(-ENOTSUPP);
1188                         LASSERT(S_ISDIR(la->la_mode));
1189                         mdt_dump_lmv(D_INFO, ma->ma_lmv);
1190                         repbody->mbo_eadatasize = ma->ma_lmv_size;
1191                         repbody->mbo_valid |= (OBD_MD_FLDIREA |
1192                                                OBD_MD_DEFAULT_MEA);
1193                 }
1194         } else if (S_ISLNK(la->la_mode) &&
1195                    reqbody->mbo_valid & OBD_MD_LINKNAME) {
1196                 buffer->lb_buf = ma->ma_lmm;
1197                 /* eadatasize from client includes NULL-terminator, so
1198                  * there is no need to read it */
1199                 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1200                 rc = mo_readlink(env, next, buffer);
1201                 if (unlikely(rc <= 0)) {
1202                         CERROR("%s: readlink failed for "DFID": rc = %d\n",
1203                                mdt_obd_name(info->mti_mdt),
1204                                PFID(mdt_object_fid(o)), rc);
1205                         rc = -EFAULT;
1206                 } else {
1207                         int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1208
1209                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1210                                 rc -= 2;
1211                         repbody->mbo_valid |= OBD_MD_LINKNAME;
1212                         /* we need to report back size with NULL-terminator
1213                          * because client expects that */
1214                         repbody->mbo_eadatasize = rc + 1;
1215                         if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1216                                 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1217                                        "on "DFID ", expected %d\n",
1218                                        mdt_obd_name(info->mti_mdt),
1219                                        rc, PFID(mdt_object_fid(o)),
1220                                        reqbody->mbo_eadatasize - 1);
1221                         /* NULL terminate */
1222                         ((char *)ma->ma_lmm)[rc] = 0;
1223
1224                         /* If the total CDEBUG() size is larger than a page, it
1225                          * will print a warning to the console, avoid this by
1226                          * printing just the last part of the symlink. */
1227                         CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1228                                print_limit < rc ? "..." : "", print_limit,
1229                                (char *)ma->ma_lmm + rc - print_limit, rc);
1230                         rc = 0;
1231                 }
1232         }
1233
1234         if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1235                 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1236                 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1237                 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1238                        repbody->mbo_max_mdsize);
1239         }
1240
1241 #ifdef CONFIG_FS_POSIX_ACL
1242         if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1243                  (reqbody->mbo_valid & OBD_MD_FLACL)) {
1244                 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1245                 if (IS_ERR(nodemap))
1246                         RETURN(PTR_ERR(nodemap));
1247
1248                 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1249                 nodemap_putref(nodemap);
1250         }
1251 #endif
1252
1253 out:
1254         if (rc == 0)
1255                 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1256
1257         RETURN(rc);
1258 }
1259
1260 static int mdt_getattr(struct tgt_session_info *tsi)
1261 {
1262         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1263         struct mdt_object       *obj = info->mti_object;
1264         struct req_capsule      *pill = info->mti_pill;
1265         struct mdt_body         *reqbody;
1266         struct mdt_body         *repbody;
1267         int rc, rc2;
1268         ENTRY;
1269
1270         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1271         LASSERT(reqbody);
1272         LASSERT(obj != NULL);
1273         LASSERT(lu_object_assert_exists(&obj->mot_obj));
1274
1275         /* Unlike intent case where we need to pre-fill out buffers early on
1276          * in intent policy for ldlm reasons, here we can have a much better
1277          * guess at EA size by just reading it from disk.
1278          * Exceptions are readdir and (missing) directory striping */
1279         /* Readlink */
1280         if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1281                 /* No easy way to know how long is the symlink, but it cannot
1282                  * be more than PATH_MAX, so we allocate +1 */
1283                 rc = PATH_MAX + 1;
1284
1285         /* A special case for fs ROOT: getattr there might fetch
1286          * default EA for entire fs, not just for this dir!
1287          */
1288         } else if (lu_fid_eq(mdt_object_fid(obj),
1289                              &info->mti_mdt->mdt_md_root_fid) &&
1290                    (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1291                    (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1292                                                                  MDS_GETATTR)) {
1293                 /* Should the default strping be bigger, mdt_fix_reply
1294                  * will reallocate */
1295                 rc = DEF_REP_MD_SIZE;
1296         } else {
1297                 /* Read the actual EA size from disk */
1298                 rc = mdt_attr_get_eabuf_size(info, obj);
1299         }
1300
1301         if (rc < 0)
1302                 GOTO(out, rc = err_serious(rc));
1303
1304         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1305
1306         /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1307          * by default. If the target object has more ACL entries, then
1308          * enlarge the buffer when necessary. */
1309         req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1310                              LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1311
1312         rc = req_capsule_server_pack(pill);
1313         if (unlikely(rc != 0))
1314                 GOTO(out, rc = err_serious(rc));
1315
1316         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1317         LASSERT(repbody != NULL);
1318         repbody->mbo_eadatasize = 0;
1319         repbody->mbo_aclsize = 0;
1320
1321         rc = mdt_check_ucred(info);
1322         if (unlikely(rc))
1323                 GOTO(out_shrink, rc);
1324
1325         info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1326
1327         rc = mdt_getattr_internal(info, obj, 0);
1328         EXIT;
1329 out_shrink:
1330         mdt_client_compatibility(info);
1331         rc2 = mdt_fix_reply(info);
1332         if (rc == 0)
1333                 rc = rc2;
1334 out:
1335         mdt_thread_info_fini(info);
1336         return rc;
1337 }
1338
1339 /**
1340  * Handler of layout intent RPC requiring the layout modification
1341  *
1342  * \param[in] info      thread environment
1343  * \param[in] obj       object
1344  * \param[in] layout    layout intent
1345  * \param[in] buf       buffer containing client's lovea, could be empty
1346  *
1347  * \retval 0    on success
1348  * \retval < 0  error code
1349  */
1350 static int mdt_layout_change(struct mdt_thread_info *info,
1351                              struct mdt_object *obj,
1352                              struct layout_intent *layout,
1353                              const struct lu_buf *buf)
1354 {
1355         struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1356         int rc;
1357         ENTRY;
1358
1359         CDEBUG(D_INFO, "got layout change request from client: "
1360                "opc:%u flags:%#x extent "DEXT"\n",
1361                layout->li_opc, layout->li_flags,
1362                PEXT(&layout->li_extent));
1363
1364         if (layout->li_extent.e_start >= layout->li_extent.e_end) {
1365                 CERROR("Recieved an invalid layout change range "DEXT
1366                        "for "DFID"\n",
1367                        PEXT(&layout->li_extent), PFID(mdt_object_fid(obj)));
1368                 RETURN(-EINVAL);
1369         }
1370
1371         if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1372                 GOTO(out, rc = -EINVAL);
1373
1374         rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1375                            MAY_WRITE);
1376         if (rc)
1377                 GOTO(out, rc);
1378
1379         /* take layout lock to prepare layout change */
1380         mdt_lock_reg_init(lh, LCK_EX);
1381         rc = mdt_object_lock(info, obj, lh,
1382                              MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
1383         if (rc)
1384                 GOTO(out, rc);
1385
1386         rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout,
1387                               buf);
1388
1389         mdt_object_unlock(info, obj, lh, 1);
1390 out:
1391         RETURN(rc);
1392 }
1393
1394 /**
1395  * Exchange MOF_LOV_CREATED flags between two objects after a
1396  * layout swap. No assumption is made on whether o1 or o2 have
1397  * created objects or not.
1398  *
1399  * \param[in,out] o1    First swap layout object
1400  * \param[in,out] o2    Second swap layout object
1401  */
1402 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1403 {
1404         unsigned int o1_lov_created = o1->mot_lov_created;
1405
1406         mutex_lock(&o1->mot_lov_mutex);
1407         mutex_lock(&o2->mot_lov_mutex);
1408
1409         o1->mot_lov_created = o2->mot_lov_created;
1410         o2->mot_lov_created = o1_lov_created;
1411
1412         mutex_unlock(&o2->mot_lov_mutex);
1413         mutex_unlock(&o1->mot_lov_mutex);
1414 }
1415
1416 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1417 {
1418         struct mdt_thread_info  *info;
1419         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1420         struct obd_export       *exp = req->rq_export;
1421         struct mdt_object       *o1, *o2, *o;
1422         struct mdt_lock_handle  *lh1, *lh2;
1423         struct mdc_swap_layouts *msl;
1424         int                      rc;
1425         ENTRY;
1426
1427         /* client does not support layout lock, so layout swaping
1428          * is disabled.
1429          * FIXME: there is a problem for old clients which don't support
1430          * layout lock yet. If those clients have already opened the file
1431          * they won't be notified at all so that old layout may still be
1432          * used to do IO. This can be fixed after file release is landed by
1433          * doing exclusive open and taking full EX ibits lock. - Jinshan */
1434         if (!exp_connect_layout(exp))
1435                 RETURN(-EOPNOTSUPP);
1436
1437         info = tsi2mdt_info(tsi);
1438
1439         if (info->mti_dlm_req != NULL)
1440                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1441
1442         o1 = info->mti_object;
1443         o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1444                                 &info->mti_body->mbo_fid2);
1445         if (IS_ERR(o))
1446                 GOTO(out, rc = PTR_ERR(o));
1447
1448         if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1449                 GOTO(put, rc = -ENOENT);
1450
1451         rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1452         if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1453                 GOTO(put, rc);
1454
1455         if (rc < 0)
1456                 swap(o1, o2);
1457
1458         /* permission check. Make sure the calling process having permission
1459          * to write both files. */
1460         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1461                                 MAY_WRITE);
1462         if (rc < 0)
1463                 GOTO(put, rc);
1464
1465         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1466                                 MAY_WRITE);
1467         if (rc < 0)
1468                 GOTO(put, rc);
1469
1470         msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1471         if (msl == NULL)
1472                 GOTO(put, rc = -EPROTO);
1473
1474         lh1 = &info->mti_lh[MDT_LH_NEW];
1475         mdt_lock_reg_init(lh1, LCK_EX);
1476         lh2 = &info->mti_lh[MDT_LH_OLD];
1477         mdt_lock_reg_init(lh2, LCK_EX);
1478
1479         rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1480                              MDS_INODELOCK_XATTR);
1481         if (rc < 0)
1482                 GOTO(put, rc);
1483
1484         rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1485                              MDS_INODELOCK_XATTR);
1486         if (rc < 0)
1487                 GOTO(unlock1, rc);
1488
1489         rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1490                              mdt_object_child(o2), msl->msl_flags);
1491         if (rc < 0)
1492                 GOTO(unlock2, rc);
1493
1494         mdt_swap_lov_flag(o1, o2);
1495
1496 unlock2:
1497         mdt_object_unlock(info, o2, lh2, rc);
1498 unlock1:
1499         mdt_object_unlock(info, o1, lh1, rc);
1500 put:
1501         mdt_object_put(info->mti_env, o);
1502 out:
1503         mdt_thread_info_fini(info);
1504         RETURN(rc);
1505 }
1506
1507 static int mdt_raw_lookup(struct mdt_thread_info *info,
1508                           struct mdt_object *parent,
1509                           const struct lu_name *lname,
1510                           struct ldlm_reply *ldlm_rep)
1511 {
1512         struct lu_fid   *child_fid = &info->mti_tmp_fid1;
1513         int              rc;
1514         ENTRY;
1515
1516         LASSERT(!info->mti_cross_ref);
1517
1518         /* Only got the fid of this obj by name */
1519         fid_zero(child_fid);
1520         rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1521                         lname, child_fid, &info->mti_spec);
1522         if (rc == 0) {
1523                 struct mdt_body *repbody;
1524
1525                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1526                 repbody->mbo_fid1 = *child_fid;
1527                 repbody->mbo_valid = OBD_MD_FLID;
1528                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1529         } else if (rc == -ENOENT) {
1530                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1531         }
1532
1533         RETURN(rc);
1534 }
1535
1536 /*
1537  * UPDATE lock should be taken against parent, and be released before exit;
1538  * child_bits lock should be taken against child, and be returned back:
1539  *            (1)normal request should release the child lock;
1540  *            (2)intent request will grant the lock to client.
1541  */
1542 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1543                                  struct mdt_lock_handle *lhc,
1544                                  __u64 child_bits,
1545                                  struct ldlm_reply *ldlm_rep)
1546 {
1547         struct ptlrpc_request  *req = mdt_info_req(info);
1548         struct mdt_body        *reqbody = NULL;
1549         struct mdt_object      *parent = info->mti_object;
1550         struct mdt_object      *child;
1551         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
1552         struct lu_name         *lname = NULL;
1553         struct mdt_lock_handle *lhp = NULL;
1554         struct ldlm_lock       *lock;
1555         __u64 try_bits = 0;
1556         bool is_resent;
1557         int ma_need = 0;
1558         int rc;
1559
1560         ENTRY;
1561
1562         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1563         LASSERT(ergo(is_resent,
1564                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1565
1566         if (parent == NULL)
1567                 RETURN(-ENOENT);
1568
1569         if (info->mti_cross_ref) {
1570                 /* Only getattr on the child. Parent is on another node. */
1571                 mdt_set_disposition(info, ldlm_rep,
1572                                     DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1573                 child = parent;
1574                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1575                        "ldlm_rep = %p\n",
1576                        PFID(mdt_object_fid(child)), ldlm_rep);
1577
1578                 rc = mdt_check_resent_lock(info, child, lhc);
1579                 if (rc < 0) {
1580                         RETURN(rc);
1581                 } else if (rc > 0) {
1582                         mdt_lock_handle_init(lhc);
1583                         mdt_lock_reg_init(lhc, LCK_PR);
1584
1585                         /*
1586                          * Object's name is on another MDS, no lookup or layout
1587                          * lock is needed here but update lock is.
1588                          */
1589                         child_bits &= ~(MDS_INODELOCK_LOOKUP |
1590                                         MDS_INODELOCK_LAYOUT);
1591                         child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1592
1593                         rc = mdt_object_lock(info, child, lhc, child_bits);
1594                         if (rc < 0)
1595                                 RETURN(rc);
1596                 }
1597
1598                 /* Finally, we can get attr for child. */
1599                 if (!mdt_object_exists(child)) {
1600                         LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1601                                         &child->mot_obj,
1602                                         "remote object doesn't exist.");
1603                         mdt_object_unlock(info, child, lhc, 1);
1604                         RETURN(-ENOENT);
1605                 }
1606
1607                 rc = mdt_getattr_internal(info, child, 0);
1608                 if (unlikely(rc != 0))
1609                         mdt_object_unlock(info, child, lhc, 1);
1610
1611                 RETURN(rc);
1612         }
1613
1614         lname = &info->mti_name;
1615         mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1616
1617         if (lu_name_is_valid(lname)) {
1618                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1619                        "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1620                        PNAME(lname), ldlm_rep);
1621         } else {
1622                 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1623                 if (unlikely(reqbody == NULL))
1624                         RETURN(err_serious(-EPROTO));
1625
1626                 *child_fid = reqbody->mbo_fid2;
1627
1628                 if (unlikely(!fid_is_sane(child_fid)))
1629                         RETURN(err_serious(-EINVAL));
1630
1631                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1632                        "ldlm_rep = %p\n",
1633                        PFID(mdt_object_fid(parent)),
1634                        PFID(&reqbody->mbo_fid2), ldlm_rep);
1635         }
1636
1637         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1638
1639         if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1640                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1641                                 &parent->mot_obj,
1642                                 "Parent doesn't exist!");
1643                 RETURN(-ESTALE);
1644         }
1645
1646         if (mdt_object_remote(parent)) {
1647                 CERROR("%s: parent "DFID" is on remote target\n",
1648                        mdt_obd_name(info->mti_mdt),
1649                        PFID(mdt_object_fid(parent)));
1650                 RETURN(-EIO);
1651         }
1652
1653         if (lu_name_is_valid(lname)) {
1654                 /* Always allow to lookup ".." */
1655                 if (unlikely(lname->ln_namelen == 2 &&
1656                              lname->ln_name[0] == '.' &&
1657                              lname->ln_name[1] == '.'))
1658                         info->mti_spec.sp_permitted = 1;
1659
1660                 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1661                         rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1662
1663                         RETURN(rc);
1664                 }
1665
1666                 /* step 1: lock parent only if parent is a directory */
1667                 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1668                         lhp = &info->mti_lh[MDT_LH_PARENT];
1669                         mdt_lock_pdo_init(lhp, LCK_PR, lname);
1670                         rc = mdt_object_lock(info, parent, lhp,
1671                                              MDS_INODELOCK_UPDATE);
1672                         if (unlikely(rc != 0))
1673                                 RETURN(rc);
1674                 }
1675
1676                 /* step 2: lookup child's fid by name */
1677                 fid_zero(child_fid);
1678                 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1679                                 child_fid, &info->mti_spec);
1680                 if (rc == -ENOENT)
1681                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1682
1683                 if (rc != 0)
1684                         GOTO(out_parent, rc);
1685         }
1686
1687         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1688
1689         /*
1690          *step 3: find the child object by fid & lock it.
1691          *        regardless if it is local or remote.
1692          *
1693          *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1694          *      set parent dir fid the same as child fid in getattr by fid case
1695          *      we should not lu_object_find() the object again, could lead
1696          *      to hung if there is a concurrent unlink destroyed the object.
1697          */
1698         if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1699                 mdt_object_get(info->mti_env, parent);
1700                 child = parent;
1701         } else {
1702                 child = mdt_object_find(info->mti_env, info->mti_mdt,
1703                                         child_fid);
1704         }
1705
1706         if (unlikely(IS_ERR(child)))
1707                 GOTO(out_parent, rc = PTR_ERR(child));
1708
1709         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1710         if (!mdt_object_exists(child)) {
1711                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1712                                 &child->mot_obj,
1713                                 "Object doesn't exist!");
1714                 GOTO(out_child, rc = -ENOENT);
1715         }
1716
1717         rc = mdt_check_resent_lock(info, child, lhc);
1718         if (rc < 0) {
1719                 GOTO(out_child, rc);
1720         } else if (rc > 0) {
1721                 mdt_lock_handle_init(lhc);
1722                 mdt_lock_reg_init(lhc, LCK_PR);
1723
1724                 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1725                       mdt_object_exists(child) && !mdt_object_remote(child)) {
1726                         struct md_attr *ma = &info->mti_attr;
1727
1728                         ma->ma_valid = 0;
1729                         ma->ma_need = MA_INODE;
1730                         rc = mdt_attr_get_complex(info, child, ma);
1731                         if (unlikely(rc != 0))
1732                                 GOTO(out_child, rc);
1733
1734                         /* If the file has not been changed for some time, we
1735                          * return not only a LOOKUP lock, but also an UPDATE
1736                          * lock and this might save us RPC on later STAT. For
1737                          * directories, it also let negative dentry cache start
1738                          * working for this dir. */
1739                         if (ma->ma_valid & MA_INODE &&
1740                             ma->ma_attr.la_valid & LA_CTIME &&
1741                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1742                                 ma->ma_attr.la_ctime < cfs_time_current_sec())
1743                                 child_bits |= MDS_INODELOCK_UPDATE;
1744                 }
1745
1746                 /* layout lock must be granted in a best-effort way
1747                  * for IT operations */
1748                 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1749                 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
1750                     !mdt_object_remote(child) && ldlm_rep != NULL) {
1751                         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1752                             exp_connect_layout(info->mti_exp)) {
1753                                 /* try to grant layout lock for regular file. */
1754                                 try_bits = MDS_INODELOCK_LAYOUT;
1755                         }
1756                         /* Acquire DOM lock in advance for data-on-mdt file */
1757                         if (child != parent)
1758                                 try_bits |= MDS_INODELOCK_DOM;
1759                 }
1760
1761                 if (try_bits != 0) {
1762                         /* try layout lock, it may fail to be granted due to
1763                          * contention at LOOKUP or UPDATE */
1764                         rc = mdt_object_lock_try(info, child, lhc, &child_bits,
1765                                                  try_bits, false);
1766                         if (child_bits & MDS_INODELOCK_LAYOUT)
1767                                 ma_need |= MA_LOV;
1768                 } else {
1769                         /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1770                          * client will enqueue the lock to the remote MDT */
1771                         if (mdt_object_remote(child))
1772                                 child_bits &= ~MDS_INODELOCK_UPDATE;
1773                         rc = mdt_object_lock(info, child, lhc, child_bits);
1774                 }
1775                 if (unlikely(rc != 0))
1776                         GOTO(out_child, rc);
1777         }
1778
1779         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1780
1781         /* finally, we can get attr for child. */
1782         rc = mdt_getattr_internal(info, child, ma_need);
1783         if (unlikely(rc != 0)) {
1784                 mdt_object_unlock(info, child, lhc, 1);
1785         } else if (lock) {
1786                 /* Debugging code. */
1787                 LDLM_DEBUG(lock, "Returning lock to client");
1788                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1789                                          &lock->l_resource->lr_name),
1790                          "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1791                          PLDLMRES(lock->l_resource),
1792                          PFID(mdt_object_fid(child)));
1793
1794                 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
1795                     mdt_object_exists(child) && !mdt_object_remote(child) &&
1796                     child != parent) {
1797                         LDLM_LOCK_PUT(lock);
1798                         mdt_object_put(info->mti_env, child);
1799                         /* NB: call the mdt_pack_size2body always after
1800                          * mdt_object_put(), that is why this speacial
1801                          * exit path is used. */
1802                         rc = mdt_pack_size2body(info, child_fid,
1803                                                 child_bits & MDS_INODELOCK_DOM);
1804                         if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
1805                                 /* DOM lock was taken in advance but this is
1806                                  * not DoM file. Drop the lock. */
1807                                 lock_res_and_lock(lock);
1808                                 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
1809                                 unlock_res_and_lock(lock);
1810                         }
1811
1812                         GOTO(out_parent, rc = 0);
1813                 }
1814         }
1815         if (lock)
1816                 LDLM_LOCK_PUT(lock);
1817
1818         EXIT;
1819 out_child:
1820         mdt_object_put(info->mti_env, child);
1821 out_parent:
1822         if (lhp)
1823                 mdt_object_unlock(info, parent, lhp, 1);
1824         return rc;
1825 }
1826
1827 /* normal handler: should release the child lock */
1828 static int mdt_getattr_name(struct tgt_session_info *tsi)
1829 {
1830         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1831         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1832         struct mdt_body        *reqbody;
1833         struct mdt_body        *repbody;
1834         int rc, rc2;
1835         ENTRY;
1836
1837         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1838         LASSERT(reqbody != NULL);
1839         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1840         LASSERT(repbody != NULL);
1841
1842         info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1843         repbody->mbo_eadatasize = 0;
1844         repbody->mbo_aclsize = 0;
1845
1846         rc = mdt_init_ucred_intent_getattr(info, reqbody);
1847         if (unlikely(rc))
1848                 GOTO(out_shrink, rc);
1849
1850         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1851         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1852                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1853                 lhc->mlh_reg_lh.cookie = 0;
1854         }
1855         mdt_exit_ucred(info);
1856         EXIT;
1857 out_shrink:
1858         mdt_client_compatibility(info);
1859         rc2 = mdt_fix_reply(info);
1860         if (rc == 0)
1861                 rc = rc2;
1862         mdt_thread_info_fini(info);
1863         return rc;
1864 }
1865
1866 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1867                          void *karg, void __user *uarg);
1868
1869 static int mdt_set_info(struct tgt_session_info *tsi)
1870 {
1871         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1872         char                    *key;
1873         void                    *val;
1874         int                      keylen, vallen, rc = 0;
1875
1876         ENTRY;
1877
1878         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1879         if (key == NULL) {
1880                 DEBUG_REQ(D_HA, req, "no set_info key");
1881                 RETURN(err_serious(-EFAULT));
1882         }
1883
1884         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1885                                       RCL_CLIENT);
1886
1887         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1888         if (val == NULL) {
1889                 DEBUG_REQ(D_HA, req, "no set_info val");
1890                 RETURN(err_serious(-EFAULT));
1891         }
1892
1893         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1894                                       RCL_CLIENT);
1895
1896         /* Swab any part of val you need to here */
1897         if (KEY_IS(KEY_READ_ONLY)) {
1898                 spin_lock(&req->rq_export->exp_lock);
1899                 if (*(__u32 *)val)
1900                         *exp_connect_flags_ptr(req->rq_export) |=
1901                                 OBD_CONNECT_RDONLY;
1902                 else
1903                         *exp_connect_flags_ptr(req->rq_export) &=
1904                                 ~OBD_CONNECT_RDONLY;
1905                 spin_unlock(&req->rq_export->exp_lock);
1906         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1907                 struct changelog_setinfo *cs = val;
1908
1909                 if (vallen != sizeof(*cs)) {
1910                         CERROR("%s: bad changelog_clear setinfo size %d\n",
1911                                tgt_name(tsi->tsi_tgt), vallen);
1912                         RETURN(-EINVAL);
1913                 }
1914                 if (ptlrpc_req_need_swab(req)) {
1915                         __swab64s(&cs->cs_recno);
1916                         __swab32s(&cs->cs_id);
1917                 }
1918
1919                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1920                                    vallen, val, NULL);
1921         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1922                 if (vallen > 0)
1923                         obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1924         } else {
1925                 RETURN(-EINVAL);
1926         }
1927         RETURN(rc);
1928 }
1929
1930 static int mdt_readpage(struct tgt_session_info *tsi)
1931 {
1932         struct mdt_thread_info  *info = mdt_th_info(tsi->tsi_env);
1933         struct mdt_object       *object = mdt_obj(tsi->tsi_corpus);
1934         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
1935         const struct mdt_body   *reqbody = tsi->tsi_mdt_body;
1936         struct mdt_body         *repbody;
1937         int                      rc;
1938         int                      i;
1939
1940         ENTRY;
1941
1942         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1943                 RETURN(err_serious(-ENOMEM));
1944
1945         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1946         if (repbody == NULL || reqbody == NULL)
1947                 RETURN(err_serious(-EFAULT));
1948
1949         /*
1950          * prepare @rdpg before calling lower layers and transfer itself. Here
1951          * reqbody->size contains offset of where to start to read and
1952          * reqbody->nlink contains number bytes to read.
1953          */
1954         rdpg->rp_hash = reqbody->mbo_size;
1955         if (rdpg->rp_hash != reqbody->mbo_size) {
1956                 CERROR("Invalid hash: %#llx != %#llx\n",
1957                        rdpg->rp_hash, reqbody->mbo_size);
1958                 RETURN(-EFAULT);
1959         }
1960
1961         rdpg->rp_attrs = reqbody->mbo_mode;
1962         if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1963                 rdpg->rp_attrs |= LUDA_64BITHASH;
1964         rdpg->rp_count  = min_t(unsigned int, reqbody->mbo_nlink,
1965                                 exp_max_brw_size(tsi->tsi_exp));
1966         rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1967                           PAGE_SHIFT;
1968         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1969         if (rdpg->rp_pages == NULL)
1970                 RETURN(-ENOMEM);
1971
1972         for (i = 0; i < rdpg->rp_npages; ++i) {
1973                 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1974                 if (rdpg->rp_pages[i] == NULL)
1975                         GOTO(free_rdpg, rc = -ENOMEM);
1976         }
1977
1978         /* call lower layers to fill allocated pages with directory data */
1979         rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1980         if (rc < 0)
1981                 GOTO(free_rdpg, rc);
1982
1983         /* send pages to client */
1984         rc = tgt_sendpage(tsi, rdpg, rc);
1985
1986         EXIT;
1987 free_rdpg:
1988
1989         for (i = 0; i < rdpg->rp_npages; i++)
1990                 if (rdpg->rp_pages[i] != NULL)
1991                         __free_page(rdpg->rp_pages[i]);
1992         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1993
1994         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1995                 RETURN(0);
1996
1997         return rc;
1998 }
1999
2000 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2001 {
2002         struct lu_ucred *uc = mdt_ucred_check(info);
2003         struct lu_attr *attr = &info->mti_attr.ma_attr;
2004
2005         if (uc == NULL)
2006                 return -EINVAL;
2007
2008         if (op != REINT_SETATTR) {
2009                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2010                         attr->la_uid = uc->uc_fsuid;
2011                 /* for S_ISGID, inherit gid from his parent, such work will be
2012                  * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2013                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2014                         attr->la_gid = uc->uc_fsgid;
2015         }
2016
2017         return 0;
2018 }
2019
2020 static int mdt_reint_internal(struct mdt_thread_info *info,
2021                               struct mdt_lock_handle *lhc,
2022                               __u32 op)
2023 {
2024         struct req_capsule      *pill = info->mti_pill;
2025         struct mdt_body         *repbody;
2026         int                      rc = 0, rc2;
2027
2028         ENTRY;
2029
2030         rc = mdt_reint_unpack(info, op);
2031         if (rc != 0) {
2032                 CERROR("Can't unpack reint, rc %d\n", rc);
2033                 RETURN(err_serious(rc));
2034         }
2035
2036         /* for replay (no_create) lmm is not needed, client has it already */
2037         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2038                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2039                                      DEF_REP_MD_SIZE);
2040
2041         /* llog cookies are always 0, the field is kept for compatibility */
2042         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2043                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2044
2045         /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2046          * by default. If the target object has more ACL entries, then
2047          * enlarge the buffer when necessary. */
2048         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2049                 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2050                                      LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2051
2052         rc = req_capsule_server_pack(pill);
2053         if (rc != 0) {
2054                 CERROR("Can't pack response, rc %d\n", rc);
2055                 RETURN(err_serious(rc));
2056         }
2057
2058         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2059                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2060                 LASSERT(repbody);
2061                 repbody->mbo_eadatasize = 0;
2062                 repbody->mbo_aclsize = 0;
2063         }
2064
2065         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2066
2067         /* for replay no cookkie / lmm need, because client have this already */
2068         if (info->mti_spec.no_create)
2069                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2070                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2071
2072         rc = mdt_init_ucred_reint(info);
2073         if (rc)
2074                 GOTO(out_shrink, rc);
2075
2076         rc = mdt_fix_attr_ucred(info, op);
2077         if (rc != 0)
2078                 GOTO(out_ucred, rc = err_serious(rc));
2079
2080         rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2081         if (rc < 0) {
2082                 GOTO(out_ucred, rc);
2083         } else if (rc == 1) {
2084                 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
2085                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2086                 GOTO(out_ucred, rc);
2087         }
2088         rc = mdt_reint_rec(info, lhc);
2089         EXIT;
2090 out_ucred:
2091         mdt_exit_ucred(info);
2092 out_shrink:
2093         mdt_client_compatibility(info);
2094         rc2 = mdt_fix_reply(info);
2095         if (rc == 0)
2096                 rc = rc2;
2097         return rc;
2098 }
2099
2100 static long mdt_reint_opcode(struct ptlrpc_request *req,
2101                              const struct req_format **fmt)
2102 {
2103         struct mdt_device       *mdt;
2104         struct mdt_rec_reint    *rec;
2105         long                     opc;
2106
2107         rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2108         if (rec != NULL) {
2109                 opc = rec->rr_opcode;
2110                 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2111                 if (opc < REINT_MAX && fmt[opc] != NULL)
2112                         req_capsule_extend(&req->rq_pill, fmt[opc]);
2113                 else {
2114                         mdt = mdt_exp2dev(req->rq_export);
2115                         CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2116                                " rc = %d\n", req->rq_export->exp_obd->obd_name,
2117                                opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2118                         opc = err_serious(-EFAULT);
2119                 }
2120         } else {
2121                 opc = err_serious(-EFAULT);
2122         }
2123         return opc;
2124 }
2125
2126 static int mdt_reint(struct tgt_session_info *tsi)
2127 {
2128         long opc;
2129         int  rc;
2130         static const struct req_format *reint_fmts[REINT_MAX] = {
2131                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
2132                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
2133                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
2134                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
2135                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
2136                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
2137                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2138                 [REINT_RMENTRY]  = &RQF_MDS_REINT_UNLINK,
2139                 [REINT_MIGRATE]  = &RQF_MDS_REINT_RENAME
2140         };
2141
2142         ENTRY;
2143
2144         opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2145         if (opc >= 0) {
2146                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2147                 /*
2148                  * No lock possible here from client to pass it to reint code
2149                  * path.
2150                  */
2151                 rc = mdt_reint_internal(info, NULL, opc);
2152                 mdt_thread_info_fini(info);
2153         } else {
2154                 rc = opc;
2155         }
2156
2157         tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2158         RETURN(rc);
2159 }
2160
2161 /* this should sync the whole device */
2162 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2163 {
2164         struct dt_device *dt = mdt->mdt_bottom;
2165         int rc;
2166         ENTRY;
2167
2168         rc = dt->dd_ops->dt_sync(env, dt);
2169         RETURN(rc);
2170 }
2171
2172 /* this should sync this object */
2173 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
2174                            struct mdt_object *mo)
2175 {
2176         int rc;
2177
2178         ENTRY;
2179
2180         if (!mdt_object_exists(mo)) {
2181                 CWARN("%s: non existing object "DFID": rc = %d\n",
2182                       exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
2183                       -ESTALE);
2184                 RETURN(-ESTALE);
2185         }
2186
2187         rc = mo_object_sync(env, mdt_object_child(mo));
2188
2189         RETURN(rc);
2190 }
2191
2192 static int mdt_sync(struct tgt_session_info *tsi)
2193 {
2194         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2195         struct req_capsule      *pill = tsi->tsi_pill;
2196         struct mdt_body         *body;
2197         int                      rc;
2198
2199         ENTRY;
2200
2201         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2202                 RETURN(err_serious(-ENOMEM));
2203
2204         if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2205                 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2206         } else {
2207                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2208
2209                 /* sync an object */
2210                 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
2211                                      info->mti_object);
2212                 if (rc == 0) {
2213                         const struct lu_fid *fid;
2214                         struct lu_attr *la = &info->mti_attr.ma_attr;
2215
2216                         info->mti_attr.ma_need = MA_INODE;
2217                         info->mti_attr.ma_valid = 0;
2218                         rc = mdt_attr_get_complex(info, info->mti_object,
2219                                                   &info->mti_attr);
2220                         if (rc == 0) {
2221                                 body = req_capsule_server_get(pill,
2222                                                               &RMF_MDT_BODY);
2223                                 fid = mdt_object_fid(info->mti_object);
2224                                 mdt_pack_attr2body(info, body, la, fid);
2225                         }
2226                 }
2227                 mdt_thread_info_fini(info);
2228         }
2229         if (rc == 0)
2230                 mdt_counter_incr(req, LPROC_MDT_SYNC);
2231
2232         RETURN(rc);
2233 }
2234
2235 static int mdt_data_sync(struct tgt_session_info *tsi)
2236 {
2237         struct mdt_thread_info *info;
2238         struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2239         struct ost_body *body = tsi->tsi_ost_body;
2240         struct ost_body *repbody;
2241         struct mdt_object *mo = NULL;
2242         struct md_attr *ma;
2243         int rc = 0;
2244
2245         ENTRY;
2246
2247         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2248
2249         /* if no fid is specified then do nothing,
2250          * device sync is done via MDS_SYNC */
2251         if (fid_is_zero(&tsi->tsi_fid))
2252                 RETURN(0);
2253
2254         mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
2255         if (IS_ERR(mo))
2256                 RETURN(PTR_ERR(mo));
2257
2258         rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
2259         if (rc)
2260                 GOTO(put, rc);
2261
2262         repbody->oa.o_oi = body->oa.o_oi;
2263         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2264
2265         info = tsi2mdt_info(tsi);
2266         ma = &info->mti_attr;
2267         ma->ma_need = MA_INODE;
2268         ma->ma_valid = 0;
2269         rc = mdt_attr_get_complex(info, mo, ma);
2270         if (rc == 0)
2271                 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
2272         else
2273                 rc = 0;
2274         mdt_thread_info_fini(info);
2275
2276         EXIT;
2277 put:
2278         if (mo != NULL)
2279                 mdt_object_put(tsi->tsi_env, mo);
2280         return rc;
2281 }
2282
2283 /*
2284  * Handle quota control requests to consult current usage/limit, but also
2285  * to configure quota enforcement
2286  */
2287 static int mdt_quotactl(struct tgt_session_info *tsi)
2288 {
2289         struct obd_export       *exp  = tsi->tsi_exp;
2290         struct req_capsule      *pill = tsi->tsi_pill;
2291         struct obd_quotactl     *oqctl, *repoqc;
2292         int                      id, rc;
2293         struct mdt_device       *mdt = mdt_exp2dev(exp);
2294         struct lu_device        *qmt = mdt->mdt_qmt_dev;
2295         struct lu_nodemap       *nodemap;
2296         ENTRY;
2297
2298         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2299         if (oqctl == NULL)
2300                 RETURN(err_serious(-EPROTO));
2301
2302         rc = req_capsule_server_pack(pill);
2303         if (rc)
2304                 RETURN(err_serious(rc));
2305
2306         nodemap = nodemap_get_from_exp(exp);
2307         if (IS_ERR(nodemap))
2308                 RETURN(PTR_ERR(nodemap));
2309
2310         switch (oqctl->qc_cmd) {
2311                 /* master quotactl */
2312         case Q_SETINFO:
2313         case Q_SETQUOTA:
2314                 if (!nodemap_can_setquota(nodemap))
2315                         GOTO(out_nodemap, rc = -EPERM);
2316         case Q_GETINFO:
2317         case Q_GETQUOTA:
2318                 if (qmt == NULL)
2319                         GOTO(out_nodemap, rc = -EOPNOTSUPP);
2320                 /* slave quotactl */
2321         case Q_GETOINFO:
2322         case Q_GETOQUOTA:
2323                 break;
2324         default:
2325                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2326                 GOTO(out_nodemap, rc = -EFAULT);
2327         }
2328
2329         id = oqctl->qc_id;
2330         switch (oqctl->qc_type) {
2331         case USRQUOTA:
2332                 id = nodemap_map_id(nodemap, NODEMAP_UID,
2333                                     NODEMAP_CLIENT_TO_FS, id);
2334                 break;
2335         case GRPQUOTA:
2336                 id = nodemap_map_id(nodemap, NODEMAP_GID,
2337                                     NODEMAP_CLIENT_TO_FS, id);
2338                 break;
2339         case PRJQUOTA:
2340                 /* todo: check/map project id */
2341                 id = oqctl->qc_id;
2342                 break;
2343         default:
2344                 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2345         }
2346         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2347         if (repoqc == NULL)
2348                 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2349
2350         if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2351                 barrier_exit(tsi->tsi_tgt->lut_bottom);
2352
2353         if (oqctl->qc_id != id)
2354                 swap(oqctl->qc_id, id);
2355
2356         if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2357                 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2358                         RETURN(-EINPROGRESS);
2359         }
2360
2361         switch (oqctl->qc_cmd) {
2362
2363         case Q_GETINFO:
2364         case Q_SETINFO:
2365         case Q_SETQUOTA:
2366         case Q_GETQUOTA:
2367                 /* forward quotactl request to QMT */
2368                 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2369                 break;
2370
2371         case Q_GETOINFO:
2372         case Q_GETOQUOTA:
2373                 /* slave quotactl */
2374                 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2375                                    oqctl);
2376                 break;
2377
2378         default:
2379                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2380                 GOTO(out_nodemap, rc = -EFAULT);
2381         }
2382
2383         if (oqctl->qc_id != id)
2384                 swap(oqctl->qc_id, id);
2385
2386         *repoqc = *oqctl;
2387
2388         EXIT;
2389
2390 out_nodemap:
2391         nodemap_putref(nodemap);
2392
2393         return rc;
2394 }
2395
2396 /** clone llog ctxt from child (mdd)
2397  * This allows remote llog (replicator) access.
2398  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2399  * context was originally set up, or we can handle them directly.
2400  * I choose the latter, but that means I need any llog
2401  * contexts set up by child to be accessable by the mdt.  So we clone the
2402  * context into our context list here.
2403  */
2404 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2405                                int idx)
2406 {
2407         struct md_device  *next = mdt->mdt_child;
2408         struct llog_ctxt *ctxt;
2409         int rc;
2410
2411         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2412                 return 0;
2413
2414         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2415         if (rc || ctxt == NULL) {
2416                 return 0;
2417         }
2418
2419         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2420         if (rc)
2421                 CERROR("Can't set mdt ctxt %d\n", rc);
2422
2423         return rc;
2424 }
2425
2426 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2427                                  struct mdt_device *mdt, int idx)
2428 {
2429         struct llog_ctxt *ctxt;
2430
2431         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2432         if (ctxt == NULL)
2433                 return 0;
2434         /* Put once for the get we just did, and once for the clone */
2435         llog_ctxt_put(ctxt);
2436         llog_ctxt_put(ctxt);
2437         return 0;
2438 }
2439
2440 /*
2441  * sec context handlers
2442  */
2443 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2444 {
2445         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2446
2447         return 0;
2448 }
2449
2450 /*
2451  * quota request handlers
2452  */
2453 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2454 {
2455         struct mdt_device       *mdt = mdt_exp2dev(tsi->tsi_exp);
2456         struct lu_device        *qmt = mdt->mdt_qmt_dev;
2457         int                      rc;
2458         ENTRY;
2459
2460         if (qmt == NULL)
2461                 RETURN(err_serious(-EOPNOTSUPP));
2462
2463         rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2464         RETURN(rc);
2465 }
2466
2467 struct mdt_object *mdt_object_new(const struct lu_env *env,
2468                                   struct mdt_device *d,
2469                                   const struct lu_fid *f)
2470 {
2471         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2472         struct lu_object *o;
2473         struct mdt_object *m;
2474         ENTRY;
2475
2476         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2477         o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2478         if (unlikely(IS_ERR(o)))
2479                 m = (struct mdt_object *)o;
2480         else
2481                 m = mdt_obj(o);
2482         RETURN(m);
2483 }
2484
2485 struct mdt_object *mdt_object_find(const struct lu_env *env,
2486                                    struct mdt_device *d,
2487                                    const struct lu_fid *f)
2488 {
2489         struct lu_object *o;
2490         struct mdt_object *m;
2491         ENTRY;
2492
2493         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2494         o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2495         if (unlikely(IS_ERR(o)))
2496                 m = (struct mdt_object *)o;
2497         else
2498                 m = mdt_obj(o);
2499
2500         RETURN(m);
2501 }
2502
2503 /**
2504  * Asyncronous commit for mdt device.
2505  *
2506  * Pass asynchonous commit call down the MDS stack.
2507  *
2508  * \param env environment
2509  * \param mdt the mdt device
2510  */
2511 static void mdt_device_commit_async(const struct lu_env *env,
2512                                     struct mdt_device *mdt)
2513 {
2514         struct dt_device *dt = mdt->mdt_bottom;
2515         int rc;
2516         ENTRY;
2517
2518         rc = dt->dd_ops->dt_commit_async(env, dt);
2519         if (unlikely(rc != 0))
2520                 CWARN("%s: async commit start failed: rc = %d\n",
2521                       mdt_obd_name(mdt), rc);
2522         atomic_inc(&mdt->mdt_async_commit_count);
2523         EXIT;
2524 }
2525
2526 /**
2527  * Mark the lock as "synchonous".
2528  *
2529  * Mark the lock to deffer transaction commit to the unlock time.
2530  *
2531  * \param lock the lock to mark as "synchonous"
2532  *
2533  * \see mdt_is_lock_sync
2534  * \see mdt_save_lock
2535  */
2536 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2537 {
2538         lock->l_ast_data = (void*)1;
2539 }
2540
2541 /**
2542  * Check whehter the lock "synchonous" or not.
2543  *
2544  * \param lock the lock to check
2545  * \retval 1 the lock is "synchonous"
2546  * \retval 0 the lock isn't "synchronous"
2547  *
2548  * \see mdt_set_lock_sync
2549  * \see mdt_save_lock
2550  */
2551 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2552 {
2553         return lock->l_ast_data != NULL;
2554 }
2555
2556 /**
2557  * Blocking AST for mdt locks.
2558  *
2559  * Starts transaction commit if in case of COS lock conflict or
2560  * deffers such a commit to the mdt_save_lock.
2561  *
2562  * \param lock the lock which blocks a request or cancelling lock
2563  * \param desc unused
2564  * \param data unused
2565  * \param flag indicates whether this cancelling or blocking callback
2566  * \retval 0
2567  * \see ldlm_blocking_ast_nocheck
2568  */
2569 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2570                      void *data, int flag)
2571 {
2572         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2573         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2574         bool commit_async = false;
2575         int rc;
2576         ENTRY;
2577
2578         if (flag == LDLM_CB_CANCELING)
2579                 RETURN(0);
2580
2581         lock_res_and_lock(lock);
2582         if (lock->l_blocking_ast != mdt_blocking_ast) {
2583                 unlock_res_and_lock(lock);
2584                 RETURN(0);
2585         }
2586         /* There is no lock conflict if l_blocking_lock == NULL,
2587          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2588          * when the last reference to a local lock was released */
2589         if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2590             lock->l_blocking_lock != NULL) {
2591                 if (mdt_cos_is_enabled(mdt)) {
2592                         if (lock->l_client_cookie !=
2593                             lock->l_blocking_lock->l_client_cookie)
2594                                 mdt_set_lock_sync(lock);
2595                 } else if (mdt_slc_is_enabled(mdt) &&
2596                            ldlm_is_cos_incompat(lock->l_blocking_lock)) {
2597                         mdt_set_lock_sync(lock);
2598                         /*
2599                          * we may do extra commit here, but there is a small
2600                          * window to miss a commit: lock was unlocked (saved),
2601                          * then a conflict lock queued and we come here, but
2602                          * REP-ACK not received, so lock was not converted to
2603                          * COS mode yet.
2604                          * Fortunately this window is quite small, so the
2605                          * extra commit should be rare (not to say distributed
2606                          * operation is rare too).
2607                          */
2608                         commit_async = true;
2609                 }
2610         } else if (lock->l_req_mode == LCK_COS &&
2611                    lock->l_blocking_lock != NULL) {
2612                 commit_async = true;
2613         }
2614
2615         rc = ldlm_blocking_ast_nocheck(lock);
2616
2617         if (commit_async) {
2618                 struct lu_env env;
2619
2620                 rc = lu_env_init(&env, LCT_LOCAL);
2621                 if (unlikely(rc != 0))
2622                         CWARN("%s: lu_env initialization failed, cannot "
2623                               "start asynchronous commit: rc = %d\n",
2624                               obd->obd_name, rc);
2625                 else
2626                         mdt_device_commit_async(&env, mdt);
2627                 lu_env_fini(&env);
2628         }
2629         RETURN(rc);
2630 }
2631
2632 /*
2633  * Blocking AST for cross-MDT lock
2634  *
2635  * Discard lock from uncommitted_slc_locks and cancel it.
2636  *
2637  * \param lock  the lock which blocks a request or cancelling lock
2638  * \param desc  unused
2639  * \param data  unused
2640  * \param flag  indicates whether this cancelling or blocking callback
2641  * \retval      0 on success
2642  * \retval      negative number on error
2643  */
2644 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2645                             void *data, int flag)
2646 {
2647         int rc = 0;
2648         ENTRY;
2649
2650         switch (flag) {
2651         case LDLM_CB_BLOCKING: {
2652                 struct lustre_handle lockh;
2653
2654                 ldlm_lock2handle(lock, &lockh);
2655                 rc = ldlm_cli_cancel(&lockh,
2656                         ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2657                 if (rc < 0) {
2658                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2659                         RETURN(rc);
2660                 }
2661                 break;
2662         }
2663         case LDLM_CB_CANCELING: {
2664                 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2665                 struct mdt_device *mdt =
2666                                 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2667
2668                 LDLM_DEBUG(lock, "Revoke remote lock\n");
2669
2670                 /* discard slc lock here so that it can be cleaned anytime,
2671                  * especially for cleanup_resource() */
2672                 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2673
2674                 /* once we cache lock, l_ast_data is set to mdt_object */
2675                 if (lock->l_ast_data != NULL) {
2676                         struct mdt_object *mo = lock->l_ast_data;
2677                         struct lu_env env;
2678
2679                         rc = lu_env_init(&env, LCT_MD_THREAD);
2680                         if (unlikely(rc != 0)) {
2681                                 CWARN("%s: lu_env initialization failed, object"
2682                                       "%p "DFID" is leaked!\n",
2683                                       obd->obd_name, mo,
2684                                       PFID(mdt_object_fid(mo)));
2685                                 RETURN(rc);
2686                         }
2687
2688                         if (lock->l_policy_data.l_inodebits.bits &
2689                             (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2690                                 rc = mo_invalidate(&env, mdt_object_child(mo));
2691                                 mo->mot_cache_attr = 0;
2692                         }
2693                         mdt_object_put(&env, mo);
2694                         lu_env_fini(&env);
2695                 }
2696                 break;
2697         }
2698         default:
2699                 LBUG();
2700         }
2701
2702         RETURN(rc);
2703 }
2704
2705 int mdt_check_resent_lock(struct mdt_thread_info *info,
2706                           struct mdt_object *mo,
2707                           struct mdt_lock_handle *lhc)
2708 {
2709         /* the lock might already be gotten in ldlm_handle_enqueue() */
2710         if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2711                 struct ptlrpc_request *req = mdt_info_req(info);
2712                 struct ldlm_lock      *lock;
2713
2714                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2715                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2716                 if (lock == NULL) {
2717                         /* Lock is pinned by ldlm_handle_enqueue0() as it is
2718                          * a resend case, however, it could be already destroyed
2719                          * due to client eviction or a raced cancel RPC. */
2720                         LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2721                                           lhc->mlh_reg_lh.cookie);
2722                         RETURN(-ESTALE);
2723                 }
2724
2725                 if (!fid_res_name_eq(mdt_object_fid(mo),
2726                                      &lock->l_resource->lr_name)) {
2727                         CWARN("%s: Although resent, but still not "
2728                               "get child lock:"DFID"\n",
2729                               info->mti_exp->exp_obd->obd_name,
2730                               PFID(mdt_object_fid(mo)));
2731                         LDLM_LOCK_PUT(lock);
2732                         RETURN(-EPROTO);
2733                 }
2734                 LDLM_LOCK_PUT(lock);
2735                 return 0;
2736         }
2737         return 1;
2738 }
2739
2740 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
2741                                struct mdt_object *o, const struct lu_fid *fid,
2742                                struct lustre_handle *lh, enum ldlm_mode mode,
2743                                __u64 *ibits, __u64 trybits, bool cache)
2744 {
2745         struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2746         union ldlm_policy_data *policy = &mti->mti_policy;
2747         struct ldlm_res_id *res_id = &mti->mti_res_id;
2748         int rc = 0;
2749         ENTRY;
2750
2751         LASSERT(mdt_object_remote(o));
2752
2753         fid_build_reg_res_name(fid, res_id);
2754
2755         memset(einfo, 0, sizeof(*einfo));
2756         einfo->ei_type = LDLM_IBITS;
2757         einfo->ei_mode = mode;
2758         einfo->ei_cb_bl = mdt_remote_blocking_ast;
2759         einfo->ei_cb_cp = ldlm_completion_ast;
2760         einfo->ei_enq_slave = 0;
2761         einfo->ei_res_id = res_id;
2762
2763         if (cache) {
2764                 /*
2765                  * if we cache lock, couple lock with mdt_object, so that object
2766                  * can be easily found in lock ASTs.
2767                  */
2768                 mdt_object_get(mti->mti_env, o);
2769                 einfo->ei_cbdata = o;
2770         }
2771
2772
2773         memset(policy, 0, sizeof(*policy));
2774         policy->l_inodebits.bits = *ibits;
2775         policy->l_inodebits.try_bits = trybits;
2776
2777         rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2778                             policy);
2779         if (rc < 0 && cache) {
2780                 mdt_object_put(mti->mti_env, o);
2781                 einfo->ei_cbdata = NULL;
2782         }
2783
2784         /* Return successfully acquired bits to a caller */
2785         if (rc == 0) {
2786                 struct ldlm_lock *lock = ldlm_handle2lock(lh);
2787
2788                 LASSERT(lock);
2789                 *ibits = lock->l_policy_data.l_inodebits.bits;
2790                 LDLM_LOCK_PUT(lock);
2791         }
2792         RETURN(rc);
2793 }
2794
2795 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2796                            const struct lu_fid *fid, struct lustre_handle *lh,
2797                            enum ldlm_mode mode, __u64 ibits, bool cache)
2798 {
2799         return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
2800                                           cache);
2801 }
2802
2803 static int mdt_object_local_lock(struct mdt_thread_info *info,
2804                                  struct mdt_object *o,
2805                                  struct mdt_lock_handle *lh, __u64 *ibits,
2806                                  __u64 trybits, bool cos_incompat)
2807 {
2808         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2809         union ldlm_policy_data *policy = &info->mti_policy;
2810         struct ldlm_res_id *res_id = &info->mti_res_id;
2811         __u64 dlmflags = 0;
2812         int rc;
2813         ENTRY;
2814
2815         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2816         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2817         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2818         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2819
2820         if (cos_incompat) {
2821                 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2822                         lh->mlh_reg_mode == LCK_EX);
2823                 dlmflags |= LDLM_FL_COS_INCOMPAT;
2824         } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2825                 dlmflags |= LDLM_FL_COS_ENABLED;
2826         }
2827
2828         /* Only enqueue LOOKUP lock for remote object */
2829         if (mdt_object_remote(o)) {
2830                 LASSERT(*ibits == MDS_INODELOCK_LOOKUP);
2831         }
2832
2833         if (lh->mlh_type == MDT_PDO_LOCK) {
2834                 /* check for exists after object is locked */
2835                 if (mdt_object_exists(o) == 0) {
2836                         /* Non-existent object shouldn't have PDO lock */
2837                         RETURN(-ESTALE);
2838                 } else {
2839                         /* Non-dir object shouldn't have PDO lock */
2840                         if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2841                                 RETURN(-ENOTDIR);
2842                 }
2843         }
2844
2845
2846         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2847         dlmflags |= LDLM_FL_ATOMIC_CB;
2848
2849         /*
2850          * Take PDO lock on whole directory and build correct @res_id for lock
2851          * on part of directory.
2852          */
2853         if (lh->mlh_pdo_hash != 0) {
2854                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2855                 mdt_lock_pdo_mode(info, o, lh);
2856                 if (lh->mlh_pdo_mode != LCK_NL) {
2857                         /*
2858                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2859                          * is never going to be sent to client and we do not
2860                          * want it slowed down due to possible cancels.
2861                          */
2862                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2863                         policy->l_inodebits.try_bits = 0;
2864                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2865                                           policy, res_id, dlmflags,
2866                                           info->mti_exp == NULL ? NULL :
2867                                           &info->mti_exp->exp_handle.h_cookie);
2868                         if (unlikely(rc != 0))
2869                                 GOTO(out_unlock, rc);
2870                 }
2871
2872                 /*
2873                  * Finish res_id initializing by name hash marking part of
2874                  * directory which is taking modification.
2875                  */
2876                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2877         }
2878
2879         policy->l_inodebits.bits = *ibits;
2880         policy->l_inodebits.try_bits = trybits;
2881
2882         /*
2883          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2884          * going to be sent to client. If it is - mdt_intent_policy() path will
2885          * fix it up and turn FL_LOCAL flag off.
2886          */
2887         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2888                           res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2889                           info->mti_exp == NULL ? NULL :
2890                           &info->mti_exp->exp_handle.h_cookie);
2891 out_unlock:
2892         if (rc != 0)
2893                 mdt_object_unlock(info, o, lh, 1);
2894         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2895                    lh->mlh_pdo_hash != 0 &&
2896                    (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2897                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2898
2899         /* Return successfully acquired bits to a caller */
2900         if (rc == 0) {
2901                 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2902
2903                 LASSERT(lock);
2904                 *ibits = lock->l_policy_data.l_inodebits.bits;
2905                 LDLM_LOCK_PUT(lock);
2906         }
2907         RETURN(rc);
2908 }
2909
2910 static int
2911 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2912                          struct mdt_lock_handle *lh, __u64 *ibits,
2913                          __u64 trybits, bool cos_incompat)
2914 {
2915         struct mdt_lock_handle *local_lh = NULL;
2916         int rc;
2917         ENTRY;
2918
2919         if (!mdt_object_remote(o)) {
2920                 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
2921                                            cos_incompat);
2922                 RETURN(rc);
2923         }
2924
2925         /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2926         *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2927                     MDS_INODELOCK_XATTR);
2928
2929         /* Only enqueue LOOKUP lock for remote object */
2930         if (*ibits & MDS_INODELOCK_LOOKUP) {
2931                 __u64 local = MDS_INODELOCK_LOOKUP;
2932
2933                 rc = mdt_object_local_lock(info, o, lh, &local, 0,
2934                                            cos_incompat);
2935                 if (rc != ELDLM_OK)
2936                         RETURN(rc);
2937
2938                 local_lh = lh;
2939         }
2940
2941         if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
2942                 /* Sigh, PDO needs to enqueue 2 locks right now, but
2943                  * enqueue RPC can only request 1 lock, to avoid extra
2944                  * RPC, so it will instead enqueue EX lock for remote
2945                  * object anyway XXX*/
2946                 if (lh->mlh_type == MDT_PDO_LOCK &&
2947                     lh->mlh_pdo_hash != 0) {
2948                         CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2949                                "EX lock.\n", mdt_obd_name(info->mti_mdt),
2950                                PFID(mdt_object_fid(o)));
2951                         lh->mlh_pdo_hash = 0;
2952                         lh->mlh_rreg_mode = LCK_EX;
2953                         lh->mlh_type = MDT_REG_LOCK;
2954                 }
2955
2956                 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
2957                                                 &lh->mlh_rreg_lh,
2958                                                 lh->mlh_rreg_mode,
2959                                                 ibits, trybits, false);
2960                 if (rc != ELDLM_OK) {
2961                         if (local_lh != NULL)
2962                                 mdt_object_unlock(info, o, local_lh, rc);
2963                         RETURN(rc);
2964                 }
2965         }
2966
2967         RETURN(0);
2968 }
2969
2970 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2971                     struct mdt_lock_handle *lh, __u64 ibits)
2972 {
2973         return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
2974 }
2975
2976 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2977                           struct mdt_lock_handle *lh, __u64 ibits,
2978                           bool cos_incompat)
2979 {
2980         LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2981         return mdt_object_lock_internal(info, o, lh, &ibits, 0,
2982                                         cos_incompat);
2983 }
2984
2985 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2986                         struct mdt_lock_handle *lh, __u64 *ibits,
2987                         __u64 trybits, bool cos_incompat)
2988 {
2989         return mdt_object_lock_internal(info, o, lh, ibits, trybits,
2990                                         cos_incompat);
2991 }
2992
2993 /**
2994  * Save a lock within request object.
2995  *
2996  * Keep the lock referenced until whether client ACK or transaction
2997  * commit happens or release the lock immediately depending on input
2998  * parameters. If COS is ON, a write lock is converted to COS lock
2999  * before saving.
3000  *
3001  * \param info thead info object
3002  * \param h lock handle
3003  * \param mode lock mode
3004  * \param decref force immediate lock releasing
3005  */
3006 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
3007                    enum ldlm_mode mode, int decref)
3008 {
3009         ENTRY;
3010
3011         if (lustre_handle_is_used(h)) {
3012                 if (decref || !info->mti_has_trans ||
3013                     !(mode & (LCK_PW | LCK_EX))) {
3014                         mdt_fid_unlock(h, mode);
3015                 } else {
3016                         struct mdt_device *mdt = info->mti_mdt;
3017                         struct ldlm_lock *lock = ldlm_handle2lock(h);
3018                         struct ptlrpc_request *req = mdt_info_req(info);
3019                         bool cos = mdt_cos_is_enabled(mdt);
3020                         bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
3021
3022                         LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
3023                                  h->cookie);
3024
3025                         /* there is no request if mdt_object_unlock() is called
3026                          * from mdt_export_cleanup()->mdt_add_dirty_flag() */
3027                         if (likely(req != NULL)) {
3028                                 LDLM_DEBUG(lock, "save lock request %p reply "
3029                                         "state %p transno %lld\n", req,
3030                                         req->rq_reply_state, req->rq_transno);
3031                                 if (cos) {
3032                                         ldlm_lock_downgrade(lock, LCK_COS);
3033                                         mode = LCK_COS;
3034                                 }
3035                                 if (req->rq_export->exp_disconnected)
3036                                         mdt_fid_unlock(h, mode);
3037                                 else
3038                                         ptlrpc_save_lock(req, h, mode, cos,
3039                                                          convert_lock);
3040                         } else {
3041                                 mdt_fid_unlock(h, mode);
3042                         }
3043                         if (mdt_is_lock_sync(lock)) {
3044                                 CDEBUG(D_HA, "found sync-lock,"
3045                                        " async commit started\n");
3046                                 mdt_device_commit_async(info->mti_env,
3047                                                         mdt);
3048                         }
3049                         LDLM_LOCK_PUT(lock);
3050                 }
3051                 h->cookie = 0ull;
3052         }
3053
3054         EXIT;
3055 }
3056
3057 /**
3058  * Save cross-MDT lock in uncommitted_slc_locks
3059  *
3060  * Keep the lock referenced until transaction commit happens or release the lock
3061  * immediately depending on input parameters.
3062  *
3063  * \param info thead info object
3064  * \param h lock handle
3065  * \param mode lock mode
3066  * \param decref force immediate lock releasing
3067  */
3068 static void mdt_save_remote_lock(struct mdt_thread_info *info,
3069                                  struct mdt_object *o, struct lustre_handle *h,
3070                                  enum ldlm_mode mode, int decref)
3071 {
3072         ENTRY;
3073
3074         if (lustre_handle_is_used(h)) {
3075                 struct ldlm_lock *lock = ldlm_handle2lock(h);
3076
3077                 if (o != NULL &&
3078                     (lock->l_policy_data.l_inodebits.bits &
3079                      (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
3080                         mo_invalidate(info->mti_env, mdt_object_child(o));
3081
3082                 if (decref || !info->mti_has_trans ||
3083                     !(mode & (LCK_PW | LCK_EX))) {
3084                         ldlm_lock_decref_and_cancel(h, mode);
3085                         LDLM_LOCK_PUT(lock);
3086                 } else {
3087                         struct ptlrpc_request *req = mdt_info_req(info);
3088
3089                         LASSERT(req != NULL);
3090                         tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
3091                                           req->rq_transno);
3092                         ldlm_lock_decref(h, mode);
3093                 }
3094                 h->cookie = 0ull;
3095         }
3096
3097         EXIT;
3098 }
3099
3100 /**
3101  * Unlock mdt object.
3102  *
3103  * Immeditely release the regular lock and the PDO lock or save the
3104  * lock in request and keep them referenced until client ACK or
3105  * transaction commit.
3106  *
3107  * \param info thread info object
3108  * \param o mdt object
3109  * \param lh mdt lock handle referencing regular and PDO locks
3110  * \param decref force immediate lock releasing
3111  *
3112  * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
3113  */
3114 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
3115                        struct mdt_lock_handle *lh, int decref)
3116 {
3117         ENTRY;
3118
3119         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
3120         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
3121         mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
3122                              decref);
3123
3124         EXIT;
3125 }
3126
3127 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
3128                                         const struct lu_fid *f,
3129                                         struct mdt_lock_handle *lh,
3130                                         __u64 ibits)
3131 {
3132         struct mdt_object *o;
3133
3134         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
3135         if (!IS_ERR(o)) {
3136                 int rc;
3137
3138                 rc = mdt_object_lock(info, o, lh, ibits);
3139                 if (rc != 0) {
3140                         mdt_object_put(info->mti_env, o);
3141                         o = ERR_PTR(rc);
3142                 }
3143         }
3144         return o;
3145 }
3146
3147 void mdt_object_unlock_put(struct mdt_thread_info * info,
3148                            struct mdt_object * o,
3149                            struct mdt_lock_handle *lh,
3150                            int decref)
3151 {
3152         mdt_object_unlock(info, o, lh, decref);
3153         mdt_object_put(info->mti_env, o);
3154 }
3155
3156 /*
3157  * Generic code handling requests that have struct mdt_body passed in:
3158  *
3159  *  - extract mdt_body from request and save it in @info, if present;
3160  *
3161  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
3162  *  @info;
3163  *
3164  *  - if HABEO_CORPUS flag is set for this request type check whether object
3165  *  actually exists on storage (lu_object_exists()).
3166  *
3167  */
3168 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
3169 {
3170         const struct mdt_body    *body;
3171         struct mdt_object        *obj;
3172         const struct lu_env      *env;
3173         struct req_capsule       *pill;
3174         int                       rc;
3175         ENTRY;
3176
3177         env = info->mti_env;
3178         pill = info->mti_pill;
3179
3180         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3181         if (body == NULL)
3182                 RETURN(-EFAULT);
3183
3184         if (!(body->mbo_valid & OBD_MD_FLID))
3185                 RETURN(0);
3186
3187         if (!fid_is_sane(&body->mbo_fid1)) {
3188                 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3189                 RETURN(-EINVAL);
3190         }
3191
3192         obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3193         if (!IS_ERR(obj)) {
3194                 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
3195                         mdt_object_put(env, obj);
3196                         rc = -ENOENT;
3197                 } else {
3198                         info->mti_object = obj;
3199                         rc = 0;
3200                 }
3201         } else
3202                 rc = PTR_ERR(obj);
3203
3204         RETURN(rc);
3205 }
3206
3207 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
3208 {
3209         struct req_capsule *pill = info->mti_pill;
3210         int rc;
3211         ENTRY;
3212
3213         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3214                 rc = mdt_body_unpack(info, flags);
3215         else
3216                 rc = 0;
3217
3218         if (rc == 0 && (flags & HABEO_REFERO)) {
3219                 /* Pack reply. */
3220                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3221                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3222                                              DEF_REP_MD_SIZE);
3223                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3224                         req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3225                                              RCL_SERVER, 0);
3226
3227                 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3228                  * by default. If the target object has more ACL entries, then
3229                  * enlarge the buffer when necessary. */
3230                 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3231                         req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3232                                              LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3233
3234                 rc = req_capsule_server_pack(pill);
3235         }
3236         RETURN(rc);
3237 }
3238
3239 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3240 {
3241         lh->mlh_type = MDT_NUL_LOCK;
3242         lh->mlh_reg_lh.cookie = 0ull;
3243         lh->mlh_reg_mode = LCK_MINMODE;
3244         lh->mlh_pdo_lh.cookie = 0ull;
3245         lh->mlh_pdo_mode = LCK_MINMODE;
3246         lh->mlh_rreg_lh.cookie = 0ull;
3247         lh->mlh_rreg_mode = LCK_MINMODE;
3248 }
3249
3250 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3251 {
3252         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3253         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3254 }
3255
3256 /*
3257  * Initialize fields of struct mdt_thread_info. Other fields are left in
3258  * uninitialized state, because it's too expensive to zero out whole
3259  * mdt_thread_info (> 1K) on each request arrival.
3260  */
3261 void mdt_thread_info_init(struct ptlrpc_request *req,
3262                           struct mdt_thread_info *info)
3263 {
3264         int i;
3265
3266         info->mti_pill = &req->rq_pill;
3267
3268         /* lock handle */
3269         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3270                 mdt_lock_handle_init(&info->mti_lh[i]);
3271
3272         /* mdt device: it can be NULL while CONNECT */
3273         if (req->rq_export) {
3274                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3275                 info->mti_exp = req->rq_export;
3276         } else
3277                 info->mti_mdt = NULL;
3278         info->mti_env = req->rq_svc_thread->t_env;
3279         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3280
3281         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3282         info->mti_big_buf = LU_BUF_NULL;
3283         info->mti_body = NULL;
3284         info->mti_object = NULL;
3285         info->mti_dlm_req = NULL;
3286         info->mti_has_trans = 0;
3287         info->mti_cross_ref = 0;
3288         info->mti_opdata = 0;
3289         info->mti_big_lmm_used = 0;
3290         info->mti_big_acl_used = 0;
3291         info->mti_som_valid = 0;
3292
3293         info->mti_spec.no_create = 0;
3294         info->mti_spec.sp_rm_entry = 0;
3295         info->mti_spec.sp_permitted = 0;
3296         info->mti_spec.sp_migrate_close = 0;
3297
3298         info->mti_spec.u.sp_ea.eadata = NULL;
3299         info->mti_spec.u.sp_ea.eadatalen = 0;
3300 }
3301
3302 void mdt_thread_info_fini(struct mdt_thread_info *info)
3303 {
3304         int i;
3305
3306         if (info->mti_object != NULL) {
3307                 mdt_object_put(info->mti_env, info->mti_object);
3308                 info->mti_object = NULL;
3309         }
3310
3311         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3312                 mdt_lock_handle_fini(&info->mti_lh[i]);
3313         info->mti_env = NULL;
3314         info->mti_pill = NULL;
3315         info->mti_exp = NULL;
3316
3317         if (unlikely(info->mti_big_buf.lb_buf != NULL))
3318                 lu_buf_free(&info->mti_big_buf);
3319 }
3320
3321 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3322 {
3323         struct mdt_thread_info  *mti;
3324
3325         mti = mdt_th_info(tsi->tsi_env);
3326         LASSERT(mti != NULL);
3327
3328         mdt_thread_info_init(tgt_ses_req(tsi), mti);
3329         if (tsi->tsi_corpus != NULL) {
3330                 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3331                 lu_object_get(tsi->tsi_corpus);
3332         }
3333         mti->mti_body = tsi->tsi_mdt_body;
3334         mti->mti_dlm_req = tsi->tsi_dlm_req;
3335
3336         return mti;
3337 }
3338
3339 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3340 {
3341         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3342             cfs_fail_val ==
3343             tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3344                 set_current_state(TASK_UNINTERRUPTIBLE);
3345                 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3346         }
3347
3348         return tgt_connect(tsi);
3349 }
3350
3351 enum mdt_it_code {
3352         MDT_IT_OPEN,
3353         MDT_IT_OCREAT,
3354         MDT_IT_CREATE,
3355         MDT_IT_GETATTR,
3356         MDT_IT_READDIR,
3357         MDT_IT_LOOKUP,
3358         MDT_IT_UNLINK,
3359         MDT_IT_TRUNC,
3360         MDT_IT_GETXATTR,
3361         MDT_IT_LAYOUT,
3362         MDT_IT_QUOTA,
3363         MDT_IT_GLIMPSE,
3364         MDT_IT_BRW,
3365         MDT_IT_NR
3366 };
3367
3368 static int mdt_intent_getattr(enum mdt_it_code opcode,
3369                               struct mdt_thread_info *info,
3370                               struct ldlm_lock **, __u64);
3371
3372 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3373                                 struct mdt_thread_info *info,
3374                                 struct ldlm_lock **lockp,
3375                                 __u64 flags);
3376
3377 static int mdt_intent_layout(enum mdt_it_code opcode,
3378                              struct mdt_thread_info *info,
3379                              struct ldlm_lock **,
3380                              __u64);
3381 static int mdt_intent_reint(enum mdt_it_code opcode,
3382                             struct mdt_thread_info *info,
3383                             struct ldlm_lock **,
3384                             __u64);
3385 static int mdt_intent_glimpse(enum mdt_it_code opcode,
3386                               struct mdt_thread_info *info,
3387                               struct ldlm_lock **lockp, __u64 flags)
3388 {
3389         return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
3390                                    lockp, flags);
3391 }
3392 static int mdt_intent_brw(enum mdt_it_code opcode,
3393                           struct mdt_thread_info *info,
3394                           struct ldlm_lock **lockp, __u64 flags)
3395 {
3396         return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
3397                                lockp, flags);
3398 }
3399
3400 static struct mdt_it_flavor {
3401         const struct req_format *it_fmt;
3402         __u32                    it_flags;
3403         int                    (*it_act)(enum mdt_it_code ,
3404                                          struct mdt_thread_info *,
3405                                          struct ldlm_lock **,
3406                                          __u64);
3407         long                     it_reint;
3408 } mdt_it_flavor[] = {
3409         [MDT_IT_OPEN]     = {
3410                 .it_fmt   = &RQF_LDLM_INTENT,
3411                 /*.it_flags = HABEO_REFERO,*/
3412                 .it_flags = 0,
3413                 .it_act   = mdt_intent_reint,
3414                 .it_reint = REINT_OPEN
3415         },
3416         [MDT_IT_OCREAT]   = {
3417                 .it_fmt   = &RQF_LDLM_INTENT,
3418                 /*
3419                  * OCREAT is not a MUTABOR request as if the file
3420                  * already exists.
3421                  * We do the extra check of OBD_CONNECT_RDONLY in
3422                  * mdt_reint_open() when we really need to create
3423                  * the object.
3424                  */
3425                 .it_flags = 0,
3426                 .it_act   = mdt_intent_reint,
3427                 .it_reint = REINT_OPEN
3428         },
3429         [MDT_IT_CREATE]   = {
3430                 .it_fmt   = &RQF_LDLM_INTENT,
3431                 .it_flags = MUTABOR,
3432                 .it_act   = mdt_intent_reint,
3433                 .it_reint = REINT_CREATE
3434         },
3435         [MDT_IT_GETATTR]  = {
3436                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3437                 .it_flags = HABEO_REFERO,
3438                 .it_act   = mdt_intent_getattr
3439         },
3440         [MDT_IT_READDIR]  = {
3441                 .it_fmt   = NULL,
3442                 .it_flags = 0,
3443                 .it_act   = NULL
3444         },
3445         [MDT_IT_LOOKUP]   = {
3446                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3447                 .it_flags = HABEO_REFERO,
3448                 .it_act   = mdt_intent_getattr
3449         },
3450         [MDT_IT_UNLINK]   = {
3451                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
3452                 .it_flags = MUTABOR,
3453                 .it_act   = NULL,
3454                 .it_reint = REINT_UNLINK
3455         },
3456         [MDT_IT_TRUNC]    = {
3457                 .it_fmt   = NULL,
3458                 .it_flags = MUTABOR,
3459                 .it_act   = NULL
3460         },
3461         [MDT_IT_GETXATTR] = {
3462                 .it_fmt   = &RQF_LDLM_INTENT_GETXATTR,
3463                 .it_flags = HABEO_CORPUS,
3464                 .it_act   = mdt_intent_getxattr
3465         },
3466         [MDT_IT_LAYOUT] = {
3467                 .it_fmt   = &RQF_LDLM_INTENT_LAYOUT,
3468                 .it_flags = 0,
3469                 .it_act   = mdt_intent_layout
3470         },
3471         [MDT_IT_GLIMPSE] = {
3472                 .it_fmt = &RQF_LDLM_INTENT,
3473                 .it_flags = 0,
3474                 .it_act = mdt_intent_glimpse,
3475         },
3476         [MDT_IT_BRW] = {
3477                 .it_fmt = &RQF_LDLM_INTENT,
3478                 .it_flags = 0,
3479                 .it_act = mdt_intent_brw,
3480         },
3481
3482 };
3483
3484 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3485                             struct ldlm_lock **lockp,
3486                             struct mdt_lock_handle *lh,
3487                             __u64 flags, int result)
3488 {
3489         struct ptlrpc_request  *req = mdt_info_req(info);
3490         struct ldlm_lock       *lock = *lockp;
3491         struct ldlm_lock       *new_lock;
3492
3493         /* If possible resent found a lock, @lh is set to its handle */
3494         new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3495
3496         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3497                 lh->mlh_reg_lh.cookie = 0;
3498                 RETURN(0);
3499         }
3500
3501         if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3502                 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3503                  * a resend case, however, it could be already destroyed
3504                  * due to client eviction or a raced cancel RPC. */
3505                 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
3506                                   lh->mlh_reg_lh.cookie);
3507                 lh->mlh_reg_lh.cookie = 0;
3508                 RETURN(-ESTALE);
3509         }
3510
3511         LASSERTF(new_lock != NULL,
3512                  "lockh %#llx flags %#llx : rc = %d\n",
3513                  lh->mlh_reg_lh.cookie, flags, result);
3514
3515         /*
3516          * If we've already given this lock to a client once, then we should
3517          * have no readers or writers.  Otherwise, we should have one reader
3518          * _or_ writer ref (which will be zeroed below) before returning the
3519          * lock to a client.
3520          */
3521         if (new_lock->l_export == req->rq_export) {
3522                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3523         } else {
3524                 LASSERT(new_lock->l_export == NULL);
3525                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3526         }
3527
3528         *lockp = new_lock;
3529
3530         if (new_lock->l_export == req->rq_export) {
3531                 /*
3532                  * Already gave this to the client, which means that we
3533                  * reconstructed a reply.
3534                  */
3535                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3536                         MSG_RESENT);
3537
3538                 LDLM_LOCK_RELEASE(new_lock);
3539                 lh->mlh_reg_lh.cookie = 0;
3540                 RETURN(ELDLM_LOCK_REPLACED);
3541         }
3542
3543         /*
3544          * Fixup the lock to be given to the client.
3545          */
3546         lock_res_and_lock(new_lock);
3547         /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3548          * possible blocking AST. */
3549         while (new_lock->l_readers > 0) {
3550                 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3551                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3552                 new_lock->l_readers--;
3553         }
3554         while (new_lock->l_writers > 0) {
3555                 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3556                 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3557                 new_lock->l_writers--;
3558         }
3559
3560         new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3561         new_lock->l_blocking_ast = lock->l_blocking_ast;
3562         new_lock->l_completion_ast = lock->l_completion_ast;
3563         if (ldlm_has_dom(new_lock))
3564                 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
3565         new_lock->l_remote_handle = lock->l_remote_handle;
3566         new_lock->l_flags &= ~LDLM_FL_LOCAL;
3567
3568         unlock_res_and_lock(new_lock);
3569
3570         cfs_hash_add(new_lock->l_export->exp_lock_hash,
3571                      &new_lock->l_remote_handle,
3572                      &new_lock->l_exp_hash);
3573
3574         LDLM_LOCK_RELEASE(new_lock);
3575         lh->mlh_reg_lh.cookie = 0;
3576
3577         RETURN(ELDLM_LOCK_REPLACED);
3578 }
3579
3580 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3581                              struct ldlm_lock *new_lock,
3582                              struct mdt_lock_handle *lh, __u64 flags)
3583 {
3584         struct ptlrpc_request  *req = mdt_info_req(info);
3585         struct ldlm_request    *dlmreq;
3586
3587         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3588                 return;
3589
3590         dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3591
3592         /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3593          * lock was found by ldlm_handle_enqueue(); if so @lh must be
3594          * initialized. */
3595         if (flags & LDLM_FL_RESENT) {
3596                 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3597                 lh->mlh_reg_mode = new_lock->l_granted_mode;
3598
3599                 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3600                 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
3601                           lh->mlh_reg_lh.cookie);
3602                 return;
3603         }
3604
3605         /*
3606          * If the xid matches, then we know this is a resent request, and allow
3607          * it. (It's probably an OPEN, for which we don't send a lock.
3608          */
3609         if (req_can_reconstruct(req, NULL))
3610                 return;
3611
3612         /*
3613          * This remote handle isn't enqueued, so we never received or processed
3614          * this request.  Clear MSG_RESENT, because it can be handled like any
3615          * normal request now.
3616          */
3617         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3618
3619         DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
3620                   dlmreq->lock_handle[0].cookie);
3621 }
3622
3623 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3624                                 struct mdt_thread_info *info,
3625                                 struct ldlm_lock **lockp,
3626                                 __u64 flags)
3627 {
3628         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3629         struct ldlm_reply      *ldlm_rep = NULL;
3630         int rc;
3631         ENTRY;
3632
3633         /*
3634          * Initialize lhc->mlh_reg_lh either from a previously granted lock
3635          * (for the resend case) or a new lock. Below we will use it to
3636          * replace the original lock.
3637          */
3638         mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3639         if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3640                 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3641                 rc = mdt_object_lock(info, info->mti_object, lhc,
3642                                      MDS_INODELOCK_XATTR);
3643                 if (rc)
3644                         return rc;
3645         }
3646
3647         rc = mdt_getxattr(info);
3648
3649         if (mdt_info_req(info)->rq_repmsg != NULL)
3650                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3651
3652         if (ldlm_rep == NULL ||
3653             OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
3654                 mdt_object_unlock(info,  info->mti_object, lhc, 1);
3655                 RETURN(err_serious(-EFAULT));
3656         }
3657
3658         ldlm_rep->lock_policy_res2 = clear_serious(rc);
3659
3660         /* This is left for interop instead of adding a new interop flag.
3661          * LU-7433 */
3662 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
3663         if (ldlm_rep->lock_policy_res2) {
3664                 mdt_object_unlock(info, info->mti_object, lhc, 1);
3665                 RETURN(ELDLM_LOCK_ABORTED);
3666         }
3667 #endif
3668
3669         rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3670         RETURN(rc);
3671 }
3672
3673 static int mdt_intent_getattr(enum mdt_it_code opcode,
3674                               struct mdt_thread_info *info,
3675 &