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