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