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