Whamcloud - gitweb
LU-7988 hsm: run HSM coordinator once per second at most
[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         __u64 try_bits = 0;
1493         bool is_resent;
1494         int ma_need = 0;
1495         int rc;
1496
1497         ENTRY;
1498
1499         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1500         LASSERT(ergo(is_resent,
1501                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1502
1503         if (parent == NULL)
1504                 RETURN(-ENOENT);
1505
1506         if (info->mti_cross_ref) {
1507                 /* Only getattr on the child. Parent is on another node. */
1508                 mdt_set_disposition(info, ldlm_rep,
1509                                     DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1510                 child = parent;
1511                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1512                        "ldlm_rep = %p\n",
1513                        PFID(mdt_object_fid(child)), ldlm_rep);
1514
1515                 rc = mdt_check_resent_lock(info, child, lhc);
1516                 if (rc < 0) {
1517                         RETURN(rc);
1518                 } else if (rc > 0) {
1519                         mdt_lock_handle_init(lhc);
1520                         mdt_lock_reg_init(lhc, LCK_PR);
1521
1522                         /*
1523                          * Object's name is on another MDS, no lookup or layout
1524                          * lock is needed here but update lock is.
1525                          */
1526                         child_bits &= ~(MDS_INODELOCK_LOOKUP |
1527                                         MDS_INODELOCK_LAYOUT);
1528                         child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1529
1530                         rc = mdt_object_lock(info, child, lhc, child_bits);
1531                         if (rc < 0)
1532                                 RETURN(rc);
1533                 }
1534
1535                 /* Finally, we can get attr for child. */
1536                 if (!mdt_object_exists(child)) {
1537                         LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1538                                         &child->mot_obj,
1539                                         "remote object doesn't exist.");
1540                         mdt_object_unlock(info, child, lhc, 1);
1541                         RETURN(-ENOENT);
1542                 }
1543
1544                 rc = mdt_getattr_internal(info, child, 0);
1545                 if (unlikely(rc != 0))
1546                         mdt_object_unlock(info, child, lhc, 1);
1547
1548                 RETURN(rc);
1549         }
1550
1551         lname = &info->mti_name;
1552         mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1553
1554         if (lu_name_is_valid(lname)) {
1555                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1556                        "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1557                        PNAME(lname), ldlm_rep);
1558         } else {
1559                 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1560                 if (unlikely(reqbody == NULL))
1561                         RETURN(err_serious(-EPROTO));
1562
1563                 *child_fid = reqbody->mbo_fid2;
1564
1565                 if (unlikely(!fid_is_sane(child_fid)))
1566                         RETURN(err_serious(-EINVAL));
1567
1568                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1569                        "ldlm_rep = %p\n",
1570                        PFID(mdt_object_fid(parent)),
1571                        PFID(&reqbody->mbo_fid2), ldlm_rep);
1572         }
1573
1574         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1575
1576         if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1577                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1578                                 &parent->mot_obj,
1579                                 "Parent doesn't exist!");
1580                 RETURN(-ESTALE);
1581         }
1582
1583         if (mdt_object_remote(parent)) {
1584                 CERROR("%s: parent "DFID" is on remote target\n",
1585                        mdt_obd_name(info->mti_mdt),
1586                        PFID(mdt_object_fid(parent)));
1587                 RETURN(-EIO);
1588         }
1589
1590         if (lu_name_is_valid(lname)) {
1591                 /* Always allow to lookup ".." */
1592                 if (unlikely(lname->ln_namelen == 2 &&
1593                              lname->ln_name[0] == '.' &&
1594                              lname->ln_name[1] == '.'))
1595                         info->mti_spec.sp_permitted = 1;
1596
1597                 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1598                         rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1599
1600                         RETURN(rc);
1601                 }
1602
1603                 /* step 1: lock parent only if parent is a directory */
1604                 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1605                         lhp = &info->mti_lh[MDT_LH_PARENT];
1606                         mdt_lock_pdo_init(lhp, LCK_PR, lname);
1607                         rc = mdt_object_lock(info, parent, lhp,
1608                                              MDS_INODELOCK_UPDATE);
1609                         if (unlikely(rc != 0))
1610                                 RETURN(rc);
1611                 }
1612
1613                 /* step 2: lookup child's fid by name */
1614                 fid_zero(child_fid);
1615                 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1616                                 child_fid, &info->mti_spec);
1617                 if (rc == -ENOENT)
1618                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1619
1620                 if (rc != 0)
1621                         GOTO(out_parent, rc);
1622         }
1623
1624         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1625
1626         /*
1627          *step 3: find the child object by fid & lock it.
1628          *        regardless if it is local or remote.
1629          *
1630          *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1631          *      set parent dir fid the same as child fid in getattr by fid case
1632          *      we should not lu_object_find() the object again, could lead
1633          *      to hung if there is a concurrent unlink destroyed the object.
1634          */
1635         if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1636                 mdt_object_get(info->mti_env, parent);
1637                 child = parent;
1638         } else {
1639                 child = mdt_object_find(info->mti_env, info->mti_mdt,
1640                                         child_fid);
1641         }
1642
1643         if (unlikely(IS_ERR(child)))
1644                 GOTO(out_parent, rc = PTR_ERR(child));
1645
1646         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1647         if (!mdt_object_exists(child)) {
1648                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1649                                 &child->mot_obj,
1650                                 "Object doesn't exist!");
1651                 GOTO(out_child, rc = -ENOENT);
1652         }
1653
1654         rc = mdt_check_resent_lock(info, child, lhc);
1655         if (rc < 0) {
1656                 GOTO(out_child, rc);
1657         } else if (rc > 0) {
1658                 mdt_lock_handle_init(lhc);
1659                 mdt_lock_reg_init(lhc, LCK_PR);
1660
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_bits = MDS_INODELOCK_LAYOUT;
1692                 }
1693
1694                 if (try_bits != 0) {
1695                         /* try layout lock, it may fail to be granted due to
1696                          * contention at LOOKUP or UPDATE */
1697                         rc = mdt_object_lock_try(info, child, lhc, &child_bits,
1698                                                  try_bits, false);
1699                         if (child_bits & MDS_INODELOCK_LAYOUT)
1700                                 ma_need |= MA_LOV;
1701                 } else {
1702                         /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1703                          * client will enqueue the lock to the remote MDT */
1704                         if (mdt_object_remote(child))
1705                                 child_bits &= ~MDS_INODELOCK_UPDATE;
1706                         rc = mdt_object_lock(info, child, lhc, child_bits);
1707                 }
1708                 if (unlikely(rc != 0))
1709                         GOTO(out_child, rc);
1710         }
1711
1712         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1713
1714         /* finally, we can get attr for child. */
1715         rc = mdt_getattr_internal(info, child, ma_need);
1716         if (unlikely(rc != 0)) {
1717                 mdt_object_unlock(info, child, lhc, 1);
1718         } else if (lock) {
1719                 /* Debugging code. */
1720                 LDLM_DEBUG(lock, "Returning lock to client");
1721                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1722                                          &lock->l_resource->lr_name),
1723                          "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1724                          PLDLMRES(lock->l_resource),
1725                          PFID(mdt_object_fid(child)));
1726         }
1727         if (lock)
1728                 LDLM_LOCK_PUT(lock);
1729
1730         EXIT;
1731 out_child:
1732         mdt_object_put(info->mti_env, child);
1733 out_parent:
1734         if (lhp)
1735                 mdt_object_unlock(info, parent, lhp, 1);
1736         return rc;
1737 }
1738
1739 /* normal handler: should release the child lock */
1740 static int mdt_getattr_name(struct tgt_session_info *tsi)
1741 {
1742         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1743         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1744         struct mdt_body        *reqbody;
1745         struct mdt_body        *repbody;
1746         int rc, rc2;
1747         ENTRY;
1748
1749         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1750         LASSERT(reqbody != NULL);
1751         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1752         LASSERT(repbody != NULL);
1753
1754         info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1755         repbody->mbo_eadatasize = 0;
1756         repbody->mbo_aclsize = 0;
1757
1758         rc = mdt_init_ucred_intent_getattr(info, reqbody);
1759         if (unlikely(rc))
1760                 GOTO(out_shrink, rc);
1761
1762         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1763         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1764                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1765                 lhc->mlh_reg_lh.cookie = 0;
1766         }
1767         mdt_exit_ucred(info);
1768         EXIT;
1769 out_shrink:
1770         mdt_client_compatibility(info);
1771         rc2 = mdt_fix_reply(info);
1772         if (rc == 0)
1773                 rc = rc2;
1774         mdt_thread_info_fini(info);
1775         return rc;
1776 }
1777
1778 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1779                          void *karg, void __user *uarg);
1780
1781 static int mdt_set_info(struct tgt_session_info *tsi)
1782 {
1783         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1784         char                    *key;
1785         void                    *val;
1786         int                      keylen, vallen, rc = 0;
1787
1788         ENTRY;
1789
1790         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1791         if (key == NULL) {
1792                 DEBUG_REQ(D_HA, req, "no set_info key");
1793                 RETURN(err_serious(-EFAULT));
1794         }
1795
1796         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1797                                       RCL_CLIENT);
1798
1799         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1800         if (val == NULL) {
1801                 DEBUG_REQ(D_HA, req, "no set_info val");
1802                 RETURN(err_serious(-EFAULT));
1803         }
1804
1805         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1806                                       RCL_CLIENT);
1807
1808         /* Swab any part of val you need to here */
1809         if (KEY_IS(KEY_READ_ONLY)) {
1810                 spin_lock(&req->rq_export->exp_lock);
1811                 if (*(__u32 *)val)
1812                         *exp_connect_flags_ptr(req->rq_export) |=
1813                                 OBD_CONNECT_RDONLY;
1814                 else
1815                         *exp_connect_flags_ptr(req->rq_export) &=
1816                                 ~OBD_CONNECT_RDONLY;
1817                 spin_unlock(&req->rq_export->exp_lock);
1818         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1819                 struct changelog_setinfo *cs = val;
1820
1821                 if (vallen != sizeof(*cs)) {
1822                         CERROR("%s: bad changelog_clear setinfo size %d\n",
1823                                tgt_name(tsi->tsi_tgt), vallen);
1824                         RETURN(-EINVAL);
1825                 }
1826                 if (ptlrpc_req_need_swab(req)) {
1827                         __swab64s(&cs->cs_recno);
1828                         __swab32s(&cs->cs_id);
1829                 }
1830
1831                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1832                                    vallen, val, NULL);
1833         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1834                 if (vallen > 0)
1835                         obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1836         } else {
1837                 RETURN(-EINVAL);
1838         }
1839         RETURN(rc);
1840 }
1841
1842 static int mdt_readpage(struct tgt_session_info *tsi)
1843 {
1844         struct mdt_thread_info  *info = mdt_th_info(tsi->tsi_env);
1845         struct mdt_object       *object = mdt_obj(tsi->tsi_corpus);
1846         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
1847         const struct mdt_body   *reqbody = tsi->tsi_mdt_body;
1848         struct mdt_body         *repbody;
1849         int                      rc;
1850         int                      i;
1851
1852         ENTRY;
1853
1854         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1855                 RETURN(err_serious(-ENOMEM));
1856
1857         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1858         if (repbody == NULL || reqbody == NULL)
1859                 RETURN(err_serious(-EFAULT));
1860
1861         /*
1862          * prepare @rdpg before calling lower layers and transfer itself. Here
1863          * reqbody->size contains offset of where to start to read and
1864          * reqbody->nlink contains number bytes to read.
1865          */
1866         rdpg->rp_hash = reqbody->mbo_size;
1867         if (rdpg->rp_hash != reqbody->mbo_size) {
1868                 CERROR("Invalid hash: %#llx != %#llx\n",
1869                        rdpg->rp_hash, reqbody->mbo_size);
1870                 RETURN(-EFAULT);
1871         }
1872
1873         rdpg->rp_attrs = reqbody->mbo_mode;
1874         if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1875                 rdpg->rp_attrs |= LUDA_64BITHASH;
1876         rdpg->rp_count  = min_t(unsigned int, reqbody->mbo_nlink,
1877                                 exp_max_brw_size(tsi->tsi_exp));
1878         rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1879                           PAGE_SHIFT;
1880         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1881         if (rdpg->rp_pages == NULL)
1882                 RETURN(-ENOMEM);
1883
1884         for (i = 0; i < rdpg->rp_npages; ++i) {
1885                 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1886                 if (rdpg->rp_pages[i] == NULL)
1887                         GOTO(free_rdpg, rc = -ENOMEM);
1888         }
1889
1890         /* call lower layers to fill allocated pages with directory data */
1891         rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1892         if (rc < 0)
1893                 GOTO(free_rdpg, rc);
1894
1895         /* send pages to client */
1896         rc = tgt_sendpage(tsi, rdpg, rc);
1897
1898         EXIT;
1899 free_rdpg:
1900
1901         for (i = 0; i < rdpg->rp_npages; i++)
1902                 if (rdpg->rp_pages[i] != NULL)
1903                         __free_page(rdpg->rp_pages[i]);
1904         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1905
1906         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1907                 RETURN(0);
1908
1909         return rc;
1910 }
1911
1912 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1913 {
1914         struct lu_ucred *uc = mdt_ucred_check(info);
1915         struct lu_attr *attr = &info->mti_attr.ma_attr;
1916
1917         if (uc == NULL)
1918                 return -EINVAL;
1919
1920         if (op != REINT_SETATTR) {
1921                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1922                         attr->la_uid = uc->uc_fsuid;
1923                 /* for S_ISGID, inherit gid from his parent, such work will be
1924                  * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1925                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1926                         attr->la_gid = uc->uc_fsgid;
1927         }
1928
1929         return 0;
1930 }
1931
1932 static int mdt_reint_internal(struct mdt_thread_info *info,
1933                               struct mdt_lock_handle *lhc,
1934                               __u32 op)
1935 {
1936         struct req_capsule      *pill = info->mti_pill;
1937         struct mdt_body         *repbody;
1938         int                      rc = 0, rc2;
1939
1940         ENTRY;
1941
1942         rc = mdt_reint_unpack(info, op);
1943         if (rc != 0) {
1944                 CERROR("Can't unpack reint, rc %d\n", rc);
1945                 RETURN(err_serious(rc));
1946         }
1947
1948         /* for replay (no_create) lmm is not needed, client has it already */
1949         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1950                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1951                                      DEF_REP_MD_SIZE);
1952
1953         /* llog cookies are always 0, the field is kept for compatibility */
1954         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1955                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1956
1957         /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1958          * by default. If the target object has more ACL entries, then
1959          * enlarge the buffer when necessary. */
1960         if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
1961                 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1962                                      LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1963
1964         rc = req_capsule_server_pack(pill);
1965         if (rc != 0) {
1966                 CERROR("Can't pack response, rc %d\n", rc);
1967                 RETURN(err_serious(rc));
1968         }
1969
1970         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1971                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1972                 LASSERT(repbody);
1973                 repbody->mbo_eadatasize = 0;
1974                 repbody->mbo_aclsize = 0;
1975         }
1976
1977         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1978
1979         /* for replay no cookkie / lmm need, because client have this already */
1980         if (info->mti_spec.no_create)
1981                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1982                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1983
1984         rc = mdt_init_ucred_reint(info);
1985         if (rc)
1986                 GOTO(out_shrink, rc);
1987
1988         rc = mdt_fix_attr_ucred(info, op);
1989         if (rc != 0)
1990                 GOTO(out_ucred, rc = err_serious(rc));
1991
1992         rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1993         if (rc < 0) {
1994                 GOTO(out_ucred, rc);
1995         } else if (rc == 1) {
1996                 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1997                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1998                 GOTO(out_ucred, rc);
1999         }
2000         rc = mdt_reint_rec(info, lhc);
2001         EXIT;
2002 out_ucred:
2003         mdt_exit_ucred(info);
2004 out_shrink:
2005         mdt_client_compatibility(info);
2006         rc2 = mdt_fix_reply(info);
2007         if (rc == 0)
2008                 rc = rc2;
2009         return rc;
2010 }
2011
2012 static long mdt_reint_opcode(struct ptlrpc_request *req,
2013                              const struct req_format **fmt)
2014 {
2015         struct mdt_device       *mdt;
2016         struct mdt_rec_reint    *rec;
2017         long                     opc;
2018
2019         rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2020         if (rec != NULL) {
2021                 opc = rec->rr_opcode;
2022                 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2023                 if (opc < REINT_MAX && fmt[opc] != NULL)
2024                         req_capsule_extend(&req->rq_pill, fmt[opc]);
2025                 else {
2026                         mdt = mdt_exp2dev(req->rq_export);
2027                         CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2028                                " rc = %d\n", req->rq_export->exp_obd->obd_name,
2029                                opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2030                         opc = err_serious(-EFAULT);
2031                 }
2032         } else {
2033                 opc = err_serious(-EFAULT);
2034         }
2035         return opc;
2036 }
2037
2038 static int mdt_reint(struct tgt_session_info *tsi)
2039 {
2040         long opc;
2041         int  rc;
2042         static const struct req_format *reint_fmts[REINT_MAX] = {
2043                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
2044                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
2045                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
2046                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
2047                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
2048                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
2049                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2050                 [REINT_RMENTRY]  = &RQF_MDS_REINT_UNLINK,
2051                 [REINT_MIGRATE]  = &RQF_MDS_REINT_RENAME
2052         };
2053
2054         ENTRY;
2055
2056         opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2057         if (opc >= 0) {
2058                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2059                 /*
2060                  * No lock possible here from client to pass it to reint code
2061                  * path.
2062                  */
2063                 rc = mdt_reint_internal(info, NULL, opc);
2064                 mdt_thread_info_fini(info);
2065         } else {
2066                 rc = opc;
2067         }
2068
2069         tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2070         RETURN(rc);
2071 }
2072
2073 /* this should sync the whole device */
2074 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2075 {
2076         struct dt_device *dt = mdt->mdt_bottom;
2077         int rc;
2078         ENTRY;
2079
2080         rc = dt->dd_ops->dt_sync(env, dt);
2081         RETURN(rc);
2082 }
2083
2084 /* this should sync this object */
2085 static int mdt_object_sync(struct mdt_thread_info *info)
2086 {
2087         struct md_object *next;
2088         int rc;
2089         ENTRY;
2090
2091         if (!mdt_object_exists(info->mti_object)) {
2092                 CWARN("%s: non existing object "DFID": rc = %d\n",
2093                       mdt_obd_name(info->mti_mdt),
2094                       PFID(mdt_object_fid(info->mti_object)), -ESTALE);
2095                 RETURN(-ESTALE);
2096         }
2097         next = mdt_object_child(info->mti_object);
2098         rc = mo_object_sync(info->mti_env, next);
2099
2100         RETURN(rc);
2101 }
2102
2103 static int mdt_sync(struct tgt_session_info *tsi)
2104 {
2105         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2106         struct req_capsule      *pill = tsi->tsi_pill;
2107         struct mdt_body         *body;
2108         int                      rc;
2109
2110         ENTRY;
2111
2112         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2113                 RETURN(err_serious(-ENOMEM));
2114
2115         if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2116                 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2117         } else {
2118                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2119
2120                 /* sync an object */
2121                 rc = mdt_object_sync(info);
2122                 if (rc == 0) {
2123                         const struct lu_fid *fid;
2124                         struct lu_attr *la = &info->mti_attr.ma_attr;
2125
2126                         info->mti_attr.ma_need = MA_INODE;
2127                         info->mti_attr.ma_valid = 0;
2128                         rc = mdt_attr_get_complex(info, info->mti_object,
2129                                                   &info->mti_attr);
2130                         if (rc == 0) {
2131                                 body = req_capsule_server_get(pill,
2132                                                               &RMF_MDT_BODY);
2133                                 fid = mdt_object_fid(info->mti_object);
2134                                 mdt_pack_attr2body(info, body, la, fid);
2135                         }
2136                 }
2137                 mdt_thread_info_fini(info);
2138         }
2139         if (rc == 0)
2140                 mdt_counter_incr(req, LPROC_MDT_SYNC);
2141
2142         RETURN(rc);
2143 }
2144
2145 /*
2146  * Handle quota control requests to consult current usage/limit, but also
2147  * to configure quota enforcement
2148  */
2149 static int mdt_quotactl(struct tgt_session_info *tsi)
2150 {
2151         struct obd_export       *exp  = tsi->tsi_exp;
2152         struct req_capsule      *pill = tsi->tsi_pill;
2153         struct obd_quotactl     *oqctl, *repoqc;
2154         int                      id, rc;
2155         struct mdt_device       *mdt = mdt_exp2dev(exp);
2156         struct lu_device        *qmt = mdt->mdt_qmt_dev;
2157         struct lu_nodemap       *nodemap;
2158         ENTRY;
2159
2160         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2161         if (oqctl == NULL)
2162                 RETURN(err_serious(-EPROTO));
2163
2164         rc = req_capsule_server_pack(pill);
2165         if (rc)
2166                 RETURN(err_serious(rc));
2167
2168         nodemap = nodemap_get_from_exp(exp);
2169         if (IS_ERR(nodemap))
2170                 RETURN(PTR_ERR(nodemap));
2171
2172         switch (oqctl->qc_cmd) {
2173                 /* master quotactl */
2174         case Q_SETINFO:
2175         case Q_SETQUOTA:
2176                 if (!nodemap_can_setquota(nodemap))
2177                         GOTO(out_nodemap, rc = -EPERM);
2178         case Q_GETINFO:
2179         case Q_GETQUOTA:
2180                 if (qmt == NULL)
2181                         GOTO(out_nodemap, rc = -EOPNOTSUPP);
2182                 /* slave quotactl */
2183         case Q_GETOINFO:
2184         case Q_GETOQUOTA:
2185                 break;
2186         default:
2187                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2188                 GOTO(out_nodemap, rc = -EFAULT);
2189         }
2190
2191         id = oqctl->qc_id;
2192         switch (oqctl->qc_type) {
2193         case USRQUOTA:
2194                 id = nodemap_map_id(nodemap, NODEMAP_UID,
2195                                     NODEMAP_CLIENT_TO_FS, id);
2196                 break;
2197         case GRPQUOTA:
2198                 id = nodemap_map_id(nodemap, NODEMAP_GID,
2199                                     NODEMAP_CLIENT_TO_FS, id);
2200                 break;
2201         case PRJQUOTA:
2202                 /* todo: check/map project id */
2203                 id = oqctl->qc_id;
2204                 break;
2205         default:
2206                 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2207         }
2208         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2209         if (repoqc == NULL)
2210                 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2211
2212         if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2213                 barrier_exit(tsi->tsi_tgt->lut_bottom);
2214
2215         if (oqctl->qc_id != id)
2216                 swap(oqctl->qc_id, id);
2217
2218         if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2219                 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2220                         RETURN(-EINPROGRESS);
2221         }
2222
2223         switch (oqctl->qc_cmd) {
2224
2225         case Q_GETINFO:
2226         case Q_SETINFO:
2227         case Q_SETQUOTA:
2228         case Q_GETQUOTA:
2229                 /* forward quotactl request to QMT */
2230                 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2231                 break;
2232
2233         case Q_GETOINFO:
2234         case Q_GETOQUOTA:
2235                 /* slave quotactl */
2236                 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2237                                    oqctl);
2238                 break;
2239
2240         default:
2241                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2242                 GOTO(out_nodemap, rc = -EFAULT);
2243         }
2244
2245         if (oqctl->qc_id != id)
2246                 swap(oqctl->qc_id, id);
2247
2248         *repoqc = *oqctl;
2249
2250         EXIT;
2251
2252 out_nodemap:
2253         nodemap_putref(nodemap);
2254
2255         return rc;
2256 }
2257
2258 /** clone llog ctxt from child (mdd)
2259  * This allows remote llog (replicator) access.
2260  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2261  * context was originally set up, or we can handle them directly.
2262  * I choose the latter, but that means I need any llog
2263  * contexts set up by child to be accessable by the mdt.  So we clone the
2264  * context into our context list here.
2265  */
2266 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2267                                int idx)
2268 {
2269         struct md_device  *next = mdt->mdt_child;
2270         struct llog_ctxt *ctxt;
2271         int rc;
2272
2273         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2274                 return 0;
2275
2276         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2277         if (rc || ctxt == NULL) {
2278                 return 0;
2279         }
2280
2281         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2282         if (rc)
2283                 CERROR("Can't set mdt ctxt %d\n", rc);
2284
2285         return rc;
2286 }
2287
2288 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2289                                  struct mdt_device *mdt, int idx)
2290 {
2291         struct llog_ctxt *ctxt;
2292
2293         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2294         if (ctxt == NULL)
2295                 return 0;
2296         /* Put once for the get we just did, and once for the clone */
2297         llog_ctxt_put(ctxt);
2298         llog_ctxt_put(ctxt);
2299         return 0;
2300 }
2301
2302 /*
2303  * sec context handlers
2304  */
2305 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2306 {
2307         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2308
2309         return 0;
2310 }
2311
2312 /*
2313  * quota request handlers
2314  */
2315 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2316 {
2317         struct mdt_device       *mdt = mdt_exp2dev(tsi->tsi_exp);
2318         struct lu_device        *qmt = mdt->mdt_qmt_dev;
2319         int                      rc;
2320         ENTRY;
2321
2322         if (qmt == NULL)
2323                 RETURN(err_serious(-EOPNOTSUPP));
2324
2325         rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2326         RETURN(rc);
2327 }
2328
2329 struct mdt_object *mdt_object_new(const struct lu_env *env,
2330                                   struct mdt_device *d,
2331                                   const struct lu_fid *f)
2332 {
2333         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2334         struct lu_object *o;
2335         struct mdt_object *m;
2336         ENTRY;
2337
2338         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2339         o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2340         if (unlikely(IS_ERR(o)))
2341                 m = (struct mdt_object *)o;
2342         else
2343                 m = mdt_obj(o);
2344         RETURN(m);
2345 }
2346
2347 struct mdt_object *mdt_object_find(const struct lu_env *env,
2348                                    struct mdt_device *d,
2349                                    const struct lu_fid *f)
2350 {
2351         struct lu_object *o;
2352         struct mdt_object *m;
2353         ENTRY;
2354
2355         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2356         o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2357         if (unlikely(IS_ERR(o)))
2358                 m = (struct mdt_object *)o;
2359         else
2360                 m = mdt_obj(o);
2361
2362         RETURN(m);
2363 }
2364
2365 /**
2366  * Asyncronous commit for mdt device.
2367  *
2368  * Pass asynchonous commit call down the MDS stack.
2369  *
2370  * \param env environment
2371  * \param mdt the mdt device
2372  */
2373 static void mdt_device_commit_async(const struct lu_env *env,
2374                                     struct mdt_device *mdt)
2375 {
2376         struct dt_device *dt = mdt->mdt_bottom;
2377         int rc;
2378         ENTRY;
2379
2380         rc = dt->dd_ops->dt_commit_async(env, dt);
2381         if (unlikely(rc != 0))
2382                 CWARN("%s: async commit start failed: rc = %d\n",
2383                       mdt_obd_name(mdt), rc);
2384         atomic_inc(&mdt->mdt_async_commit_count);
2385         EXIT;
2386 }
2387
2388 /**
2389  * Mark the lock as "synchonous".
2390  *
2391  * Mark the lock to deffer transaction commit to the unlock time.
2392  *
2393  * \param lock the lock to mark as "synchonous"
2394  *
2395  * \see mdt_is_lock_sync
2396  * \see mdt_save_lock
2397  */
2398 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2399 {
2400         lock->l_ast_data = (void*)1;
2401 }
2402
2403 /**
2404  * Check whehter the lock "synchonous" or not.
2405  *
2406  * \param lock the lock to check
2407  * \retval 1 the lock is "synchonous"
2408  * \retval 0 the lock isn't "synchronous"
2409  *
2410  * \see mdt_set_lock_sync
2411  * \see mdt_save_lock
2412  */
2413 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2414 {
2415         return lock->l_ast_data != NULL;
2416 }
2417
2418 /**
2419  * Blocking AST for mdt locks.
2420  *
2421  * Starts transaction commit if in case of COS lock conflict or
2422  * deffers such a commit to the mdt_save_lock.
2423  *
2424  * \param lock the lock which blocks a request or cancelling lock
2425  * \param desc unused
2426  * \param data unused
2427  * \param flag indicates whether this cancelling or blocking callback
2428  * \retval 0
2429  * \see ldlm_blocking_ast_nocheck
2430  */
2431 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2432                      void *data, int flag)
2433 {
2434         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2435         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2436         bool commit_async = false;
2437         int rc;
2438         ENTRY;
2439
2440         if (flag == LDLM_CB_CANCELING)
2441                 RETURN(0);
2442
2443         lock_res_and_lock(lock);
2444         if (lock->l_blocking_ast != mdt_blocking_ast) {
2445                 unlock_res_and_lock(lock);
2446                 RETURN(0);
2447         }
2448         /* There is no lock conflict if l_blocking_lock == NULL,
2449          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2450          * when the last reference to a local lock was released */
2451         if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2452             lock->l_blocking_lock != NULL) {
2453                 if (mdt_cos_is_enabled(mdt)) {
2454                         if (lock->l_client_cookie !=
2455                             lock->l_blocking_lock->l_client_cookie)
2456                                 mdt_set_lock_sync(lock);
2457                 } else if (mdt_slc_is_enabled(mdt) &&
2458                            ldlm_is_cos_incompat(lock->l_blocking_lock)) {
2459                         mdt_set_lock_sync(lock);
2460                         /*
2461                          * we may do extra commit here, but there is a small
2462                          * window to miss a commit: lock was unlocked (saved),
2463                          * then a conflict lock queued and we come here, but
2464                          * REP-ACK not received, so lock was not converted to
2465                          * COS mode yet.
2466                          * Fortunately this window is quite small, so the
2467                          * extra commit should be rare (not to say distributed
2468                          * operation is rare too).
2469                          */
2470                         commit_async = true;
2471                 }
2472         } else if (lock->l_req_mode == LCK_COS &&
2473                    lock->l_blocking_lock != NULL) {
2474                 commit_async = true;
2475         }
2476
2477         rc = ldlm_blocking_ast_nocheck(lock);
2478
2479         if (commit_async) {
2480                 struct lu_env env;
2481
2482                 rc = lu_env_init(&env, LCT_LOCAL);
2483                 if (unlikely(rc != 0))
2484                         CWARN("%s: lu_env initialization failed, cannot "
2485                               "start asynchronous commit: rc = %d\n",
2486                               obd->obd_name, rc);
2487                 else
2488                         mdt_device_commit_async(&env, mdt);
2489                 lu_env_fini(&env);
2490         }
2491         RETURN(rc);
2492 }
2493
2494 /*
2495  * Blocking AST for cross-MDT lock
2496  *
2497  * Discard lock from uncommitted_slc_locks and cancel it.
2498  *
2499  * \param lock  the lock which blocks a request or cancelling lock
2500  * \param desc  unused
2501  * \param data  unused
2502  * \param flag  indicates whether this cancelling or blocking callback
2503  * \retval      0 on success
2504  * \retval      negative number on error
2505  */
2506 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2507                             void *data, int flag)
2508 {
2509         int rc = 0;
2510         ENTRY;
2511
2512         switch (flag) {
2513         case LDLM_CB_BLOCKING: {
2514                 struct lustre_handle lockh;
2515
2516                 ldlm_lock2handle(lock, &lockh);
2517                 rc = ldlm_cli_cancel(&lockh,
2518                         ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2519                 if (rc < 0) {
2520                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2521                         RETURN(rc);
2522                 }
2523                 break;
2524         }
2525         case LDLM_CB_CANCELING: {
2526                 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2527                 struct mdt_device *mdt =
2528                                 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2529
2530                 LDLM_DEBUG(lock, "Revoke remote lock\n");
2531
2532                 /* discard slc lock here so that it can be cleaned anytime,
2533                  * especially for cleanup_resource() */
2534                 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2535
2536                 /* once we cache lock, l_ast_data is set to mdt_object */
2537                 if (lock->l_ast_data != NULL) {
2538                         struct mdt_object *mo = lock->l_ast_data;
2539                         struct lu_env env;
2540
2541                         rc = lu_env_init(&env, LCT_MD_THREAD);
2542                         if (unlikely(rc != 0)) {
2543                                 CWARN("%s: lu_env initialization failed, object"
2544                                       "%p "DFID" is leaked!\n",
2545                                       obd->obd_name, mo,
2546                                       PFID(mdt_object_fid(mo)));
2547                                 RETURN(rc);
2548                         }
2549
2550                         if (lock->l_policy_data.l_inodebits.bits &
2551                             (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2552                                 rc = mo_invalidate(&env, mdt_object_child(mo));
2553                                 mo->mot_cache_attr = 0;
2554                         }
2555                         mdt_object_put(&env, mo);
2556                         lu_env_fini(&env);
2557                 }
2558                 break;
2559         }
2560         default:
2561                 LBUG();
2562         }
2563
2564         RETURN(rc);
2565 }
2566
2567 int mdt_check_resent_lock(struct mdt_thread_info *info,
2568                           struct mdt_object *mo,
2569                           struct mdt_lock_handle *lhc)
2570 {
2571         /* the lock might already be gotten in ldlm_handle_enqueue() */
2572         if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2573                 struct ptlrpc_request *req = mdt_info_req(info);
2574                 struct ldlm_lock      *lock;
2575
2576                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2577                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2578                 if (lock == NULL) {
2579                         /* Lock is pinned by ldlm_handle_enqueue0() as it is
2580                          * a resend case, however, it could be already destroyed
2581                          * due to client eviction or a raced cancel RPC. */
2582                         LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2583                                           lhc->mlh_reg_lh.cookie);
2584                         RETURN(-ESTALE);
2585                 }
2586
2587                 if (!fid_res_name_eq(mdt_object_fid(mo),
2588                                      &lock->l_resource->lr_name)) {
2589                         CWARN("%s: Although resent, but still not "
2590                               "get child lock:"DFID"\n",
2591                               info->mti_exp->exp_obd->obd_name,
2592                               PFID(mdt_object_fid(mo)));
2593                         LDLM_LOCK_PUT(lock);
2594                         RETURN(-EPROTO);
2595                 }
2596                 LDLM_LOCK_PUT(lock);
2597                 return 0;
2598         }
2599         return 1;
2600 }
2601
2602 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
2603                                struct mdt_object *o, const struct lu_fid *fid,
2604                                struct lustre_handle *lh, enum ldlm_mode mode,
2605                                __u64 *ibits, __u64 trybits, bool cache)
2606 {
2607         struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2608         union ldlm_policy_data *policy = &mti->mti_policy;
2609         struct ldlm_res_id *res_id = &mti->mti_res_id;
2610         int rc = 0;
2611         ENTRY;
2612
2613         LASSERT(mdt_object_remote(o));
2614
2615         fid_build_reg_res_name(fid, res_id);
2616
2617         memset(einfo, 0, sizeof(*einfo));
2618         einfo->ei_type = LDLM_IBITS;
2619         einfo->ei_mode = mode;
2620         einfo->ei_cb_bl = mdt_remote_blocking_ast;
2621         einfo->ei_cb_cp = ldlm_completion_ast;
2622         einfo->ei_enq_slave = 0;
2623         einfo->ei_res_id = res_id;
2624
2625         if (cache) {
2626                 /*
2627                  * if we cache lock, couple lock with mdt_object, so that object
2628                  * can be easily found in lock ASTs.
2629                  */
2630                 mdt_object_get(mti->mti_env, o);
2631                 einfo->ei_cbdata = o;
2632         }
2633
2634
2635         memset(policy, 0, sizeof(*policy));
2636         policy->l_inodebits.bits = *ibits;
2637         policy->l_inodebits.try_bits = trybits;
2638
2639         rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2640                             policy);
2641         if (rc < 0 && cache) {
2642                 mdt_object_put(mti->mti_env, o);
2643                 einfo->ei_cbdata = NULL;
2644         }
2645
2646         /* Return successfully acquired bits to a caller */
2647         if (rc == 0) {
2648                 struct ldlm_lock *lock = ldlm_handle2lock(lh);
2649
2650                 LASSERT(lock);
2651                 *ibits = lock->l_policy_data.l_inodebits.bits;
2652                 LDLM_LOCK_PUT(lock);
2653         }
2654         RETURN(rc);
2655 }
2656
2657 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2658                            const struct lu_fid *fid, struct lustre_handle *lh,
2659                            enum ldlm_mode mode, __u64 ibits, bool cache)
2660 {
2661         return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
2662                                           cache);
2663 }
2664
2665 static int mdt_object_local_lock(struct mdt_thread_info *info,
2666                                  struct mdt_object *o,
2667                                  struct mdt_lock_handle *lh, __u64 *ibits,
2668                                  __u64 trybits, bool cos_incompat)
2669 {
2670         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2671         union ldlm_policy_data *policy = &info->mti_policy;
2672         struct ldlm_res_id *res_id = &info->mti_res_id;
2673         __u64 dlmflags = 0;
2674         int rc;
2675         ENTRY;
2676
2677         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2678         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2679         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2680         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2681
2682         if (cos_incompat) {
2683                 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2684                         lh->mlh_reg_mode == LCK_EX);
2685                 dlmflags |= LDLM_FL_COS_INCOMPAT;
2686         } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2687                 dlmflags |= LDLM_FL_COS_ENABLED;
2688         }
2689
2690         /* Only enqueue LOOKUP lock for remote object */
2691         if (mdt_object_remote(o)) {
2692                 LASSERT(*ibits == MDS_INODELOCK_LOOKUP);
2693         }
2694
2695         if (lh->mlh_type == MDT_PDO_LOCK) {
2696                 /* check for exists after object is locked */
2697                 if (mdt_object_exists(o) == 0) {
2698                         /* Non-existent object shouldn't have PDO lock */
2699                         RETURN(-ESTALE);
2700                 } else {
2701                         /* Non-dir object shouldn't have PDO lock */
2702                         if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2703                                 RETURN(-ENOTDIR);
2704                 }
2705         }
2706
2707
2708         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2709         dlmflags |= LDLM_FL_ATOMIC_CB;
2710
2711         /*
2712          * Take PDO lock on whole directory and build correct @res_id for lock
2713          * on part of directory.
2714          */
2715         if (lh->mlh_pdo_hash != 0) {
2716                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2717                 mdt_lock_pdo_mode(info, o, lh);
2718                 if (lh->mlh_pdo_mode != LCK_NL) {
2719                         /*
2720                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2721                          * is never going to be sent to client and we do not
2722                          * want it slowed down due to possible cancels.
2723                          */
2724                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2725                         policy->l_inodebits.try_bits = 0;
2726                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2727                                           policy, res_id, dlmflags,
2728                                           info->mti_exp == NULL ? NULL :
2729                                           &info->mti_exp->exp_handle.h_cookie);
2730                         if (unlikely(rc != 0))
2731                                 GOTO(out_unlock, rc);
2732                 }
2733
2734                 /*
2735                  * Finish res_id initializing by name hash marking part of
2736                  * directory which is taking modification.
2737                  */
2738                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2739         }
2740
2741         policy->l_inodebits.bits = *ibits;
2742         policy->l_inodebits.try_bits = trybits;
2743
2744         /*
2745          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2746          * going to be sent to client. If it is - mdt_intent_policy() path will
2747          * fix it up and turn FL_LOCAL flag off.
2748          */
2749         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2750                           res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2751                           info->mti_exp == NULL ? NULL :
2752                           &info->mti_exp->exp_handle.h_cookie);
2753 out_unlock:
2754         if (rc != 0)
2755                 mdt_object_unlock(info, o, lh, 1);
2756         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2757                    lh->mlh_pdo_hash != 0 &&
2758                    (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2759                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2760
2761         /* Return successfully acquired bits to a caller */
2762         if (rc == 0) {
2763                 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2764
2765                 LASSERT(lock);
2766                 *ibits = lock->l_policy_data.l_inodebits.bits;
2767                 LDLM_LOCK_PUT(lock);
2768         }
2769         RETURN(rc);
2770 }
2771
2772 static int
2773 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2774                          struct mdt_lock_handle *lh, __u64 *ibits,
2775                          __u64 trybits, bool cos_incompat)
2776 {
2777         struct mdt_lock_handle *local_lh = NULL;
2778         int rc;
2779         ENTRY;
2780
2781         if (!mdt_object_remote(o)) {
2782                 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
2783                                            cos_incompat);
2784                 RETURN(rc);
2785         }
2786
2787         /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2788         *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2789                     MDS_INODELOCK_XATTR);
2790
2791         /* Only enqueue LOOKUP lock for remote object */
2792         if (*ibits & MDS_INODELOCK_LOOKUP) {
2793                 __u64 local = MDS_INODELOCK_LOOKUP;
2794
2795                 rc = mdt_object_local_lock(info, o, lh, &local, 0,
2796                                            cos_incompat);
2797                 if (rc != ELDLM_OK)
2798                         RETURN(rc);
2799
2800                 local_lh = lh;
2801         }
2802
2803         if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
2804                 /* Sigh, PDO needs to enqueue 2 locks right now, but
2805                  * enqueue RPC can only request 1 lock, to avoid extra
2806                  * RPC, so it will instead enqueue EX lock for remote
2807                  * object anyway XXX*/
2808                 if (lh->mlh_type == MDT_PDO_LOCK &&
2809                     lh->mlh_pdo_hash != 0) {
2810                         CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2811                                "EX lock.\n", mdt_obd_name(info->mti_mdt),
2812                                PFID(mdt_object_fid(o)));
2813                         lh->mlh_pdo_hash = 0;
2814                         lh->mlh_rreg_mode = LCK_EX;
2815                         lh->mlh_type = MDT_REG_LOCK;
2816                 }
2817
2818                 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
2819                                                 &lh->mlh_rreg_lh,
2820                                                 lh->mlh_rreg_mode,
2821                                                 ibits, trybits, false);
2822                 if (rc != ELDLM_OK) {
2823                         if (local_lh != NULL)
2824                                 mdt_object_unlock(info, o, local_lh, rc);
2825                         RETURN(rc);
2826                 }
2827         }
2828
2829         RETURN(0);
2830 }
2831
2832 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2833                     struct mdt_lock_handle *lh, __u64 ibits)
2834 {
2835         return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
2836 }
2837
2838 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2839                           struct mdt_lock_handle *lh, __u64 ibits,
2840                           bool cos_incompat)
2841 {
2842         LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2843         return mdt_object_lock_internal(info, o, lh, &ibits, 0,
2844                                         cos_incompat);
2845 }
2846
2847 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2848                         struct mdt_lock_handle *lh, __u64 *ibits,
2849                         __u64 trybits, bool cos_incompat)
2850 {
2851         return mdt_object_lock_internal(info, o, lh, ibits, trybits,
2852                                         cos_incompat);
2853 }
2854
2855 /**
2856  * Save a lock within request object.
2857  *
2858  * Keep the lock referenced until whether client ACK or transaction
2859  * commit happens or release the lock immediately depending on input
2860  * parameters. If COS is ON, a write lock is converted to COS lock
2861  * before saving.
2862  *
2863  * \param info thead info object
2864  * \param h lock handle
2865  * \param mode lock mode
2866  * \param decref force immediate lock releasing
2867  */
2868 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2869                           enum ldlm_mode mode, int decref)
2870 {
2871         ENTRY;
2872
2873         if (lustre_handle_is_used(h)) {
2874                 if (decref || !info->mti_has_trans ||
2875                     !(mode & (LCK_PW | LCK_EX))) {
2876                         mdt_fid_unlock(h, mode);
2877                 } else {
2878                         struct mdt_device *mdt = info->mti_mdt;
2879                         struct ldlm_lock *lock = ldlm_handle2lock(h);
2880                         struct ptlrpc_request *req = mdt_info_req(info);
2881                         bool cos = mdt_cos_is_enabled(mdt);
2882                         bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
2883
2884                         LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
2885                                  h->cookie);
2886
2887                         /* there is no request if mdt_object_unlock() is called
2888                          * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2889                         if (likely(req != NULL)) {
2890                                 LDLM_DEBUG(lock, "save lock request %p reply "
2891                                         "state %p transno %lld\n", req,
2892                                         req->rq_reply_state, req->rq_transno);
2893                                 if (cos) {
2894                                         ldlm_lock_downgrade(lock, LCK_COS);
2895                                         mode = LCK_COS;
2896                                 }
2897                                 if (req->rq_export->exp_disconnected)
2898                                         mdt_fid_unlock(h, mode);
2899                                 else
2900                                         ptlrpc_save_lock(req, h, mode, cos,
2901                                                          convert_lock);
2902                         } else {
2903                                 mdt_fid_unlock(h, mode);
2904                         }
2905                         if (mdt_is_lock_sync(lock)) {
2906                                 CDEBUG(D_HA, "found sync-lock,"
2907                                        " async commit started\n");
2908                                 mdt_device_commit_async(info->mti_env,
2909                                                         mdt);
2910                         }
2911                         LDLM_LOCK_PUT(lock);
2912                 }
2913                 h->cookie = 0ull;
2914         }
2915
2916         EXIT;
2917 }
2918
2919 /**
2920  * Save cross-MDT lock in uncommitted_slc_locks
2921  *
2922  * Keep the lock referenced until transaction commit happens or release the lock
2923  * immediately depending on input parameters.
2924  *
2925  * \param info thead info object
2926  * \param h lock handle
2927  * \param mode lock mode
2928  * \param decref force immediate lock releasing
2929  */
2930 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2931                                  struct mdt_object *o, struct lustre_handle *h,
2932                                  enum ldlm_mode mode, int decref)
2933 {
2934         ENTRY;
2935
2936         if (lustre_handle_is_used(h)) {
2937                 struct ldlm_lock *lock = ldlm_handle2lock(h);
2938
2939                 if (o != NULL &&
2940                     (lock->l_policy_data.l_inodebits.bits &
2941                      (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2942                         mo_invalidate(info->mti_env, mdt_object_child(o));
2943
2944                 if (decref || !info->mti_has_trans ||
2945                     !(mode & (LCK_PW | LCK_EX))) {
2946                         ldlm_lock_decref_and_cancel(h, mode);
2947                         LDLM_LOCK_PUT(lock);
2948                 } else {
2949                         struct ptlrpc_request *req = mdt_info_req(info);
2950
2951                         LASSERT(req != NULL);
2952                         tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
2953                                           req->rq_transno);
2954                         ldlm_lock_decref(h, mode);
2955                 }
2956                 h->cookie = 0ull;
2957         }
2958
2959         EXIT;
2960 }
2961
2962 /**
2963  * Unlock mdt object.
2964  *
2965  * Immeditely release the regular lock and the PDO lock or save the
2966  * lock in request and keep them referenced until client ACK or
2967  * transaction commit.
2968  *
2969  * \param info thread info object
2970  * \param o mdt object
2971  * \param lh mdt lock handle referencing regular and PDO locks
2972  * \param decref force immediate lock releasing
2973  *
2974  * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
2975  */
2976 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2977                        struct mdt_lock_handle *lh, int decref)
2978 {
2979         ENTRY;
2980
2981         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2982         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2983         mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
2984                              decref);
2985
2986         EXIT;
2987 }
2988
2989 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2990                                         const struct lu_fid *f,
2991                                         struct mdt_lock_handle *lh,
2992                                         __u64 ibits)
2993 {
2994         struct mdt_object *o;
2995
2996         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2997         if (!IS_ERR(o)) {
2998                 int rc;
2999
3000                 rc = mdt_object_lock(info, o, lh, ibits);
3001                 if (rc != 0) {
3002                         mdt_object_put(info->mti_env, o);
3003                         o = ERR_PTR(rc);
3004                 }
3005         }
3006         return o;
3007 }
3008
3009 void mdt_object_unlock_put(struct mdt_thread_info * info,
3010                            struct mdt_object * o,
3011                            struct mdt_lock_handle *lh,
3012                            int decref)
3013 {
3014         mdt_object_unlock(info, o, lh, decref);
3015         mdt_object_put(info->mti_env, o);
3016 }
3017
3018 /*
3019  * Generic code handling requests that have struct mdt_body passed in:
3020  *
3021  *  - extract mdt_body from request and save it in @info, if present;
3022  *
3023  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
3024  *  @info;
3025  *
3026  *  - if HABEO_CORPUS flag is set for this request type check whether object
3027  *  actually exists on storage (lu_object_exists()).
3028  *
3029  */
3030 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
3031 {
3032         const struct mdt_body    *body;
3033         struct mdt_object        *obj;
3034         const struct lu_env      *env;
3035         struct req_capsule       *pill;
3036         int                       rc;
3037         ENTRY;
3038
3039         env = info->mti_env;
3040         pill = info->mti_pill;
3041
3042         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3043         if (body == NULL)
3044                 RETURN(-EFAULT);
3045
3046         if (!(body->mbo_valid & OBD_MD_FLID))
3047                 RETURN(0);
3048
3049         if (!fid_is_sane(&body->mbo_fid1)) {
3050                 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3051                 RETURN(-EINVAL);
3052         }
3053
3054         obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3055         if (!IS_ERR(obj)) {
3056                 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
3057                         mdt_object_put(env, obj);
3058                         rc = -ENOENT;
3059                 } else {
3060                         info->mti_object = obj;
3061                         rc = 0;
3062                 }
3063         } else
3064                 rc = PTR_ERR(obj);
3065
3066         RETURN(rc);
3067 }
3068
3069 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
3070 {
3071         struct req_capsule *pill = info->mti_pill;
3072         int rc;
3073         ENTRY;
3074
3075         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3076                 rc = mdt_body_unpack(info, flags);
3077         else
3078                 rc = 0;
3079
3080         if (rc == 0 && (flags & HABEO_REFERO)) {
3081                 /* Pack reply. */
3082                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3083                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3084                                              DEF_REP_MD_SIZE);
3085                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3086                         req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3087                                              RCL_SERVER, 0);
3088
3089                 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3090                  * by default. If the target object has more ACL entries, then
3091                  * enlarge the buffer when necessary. */
3092                 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3093                         req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3094                                              LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3095
3096                 rc = req_capsule_server_pack(pill);
3097         }
3098         RETURN(rc);
3099 }
3100
3101 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3102 {
3103         lh->mlh_type = MDT_NUL_LOCK;
3104         lh->mlh_reg_lh.cookie = 0ull;
3105         lh->mlh_reg_mode = LCK_MINMODE;
3106         lh->mlh_pdo_lh.cookie = 0ull;
3107         lh->mlh_pdo_mode = LCK_MINMODE;
3108         lh->mlh_rreg_lh.cookie = 0ull;
3109         lh->mlh_rreg_mode = LCK_MINMODE;
3110 }
3111
3112 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3113 {
3114         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3115         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3116 }
3117
3118 /*
3119  * Initialize fields of struct mdt_thread_info. Other fields are left in
3120  * uninitialized state, because it's too expensive to zero out whole
3121  * mdt_thread_info (> 1K) on each request arrival.
3122  */
3123 void mdt_thread_info_init(struct ptlrpc_request *req,
3124                           struct mdt_thread_info *info)
3125 {
3126         int i;
3127
3128         info->mti_pill = &req->rq_pill;
3129
3130         /* lock handle */
3131         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3132                 mdt_lock_handle_init(&info->mti_lh[i]);
3133
3134         /* mdt device: it can be NULL while CONNECT */
3135         if (req->rq_export) {
3136                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3137                 info->mti_exp = req->rq_export;
3138         } else
3139                 info->mti_mdt = NULL;
3140         info->mti_env = req->rq_svc_thread->t_env;
3141         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3142
3143         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3144         info->mti_big_buf = LU_BUF_NULL;
3145         info->mti_body = NULL;
3146         info->mti_object = NULL;
3147         info->mti_dlm_req = NULL;
3148         info->mti_has_trans = 0;
3149         info->mti_cross_ref = 0;
3150         info->mti_opdata = 0;
3151         info->mti_big_lmm_used = 0;
3152         info->mti_big_acl_used = 0;
3153
3154         info->mti_spec.no_create = 0;
3155         info->mti_spec.sp_rm_entry = 0;
3156         info->mti_spec.sp_permitted = 0;
3157         info->mti_spec.sp_migrate_close = 0;
3158
3159         info->mti_spec.u.sp_ea.eadata = NULL;
3160         info->mti_spec.u.sp_ea.eadatalen = 0;
3161 }
3162
3163 void mdt_thread_info_fini(struct mdt_thread_info *info)
3164 {
3165         int i;
3166
3167         if (info->mti_object != NULL) {
3168                 mdt_object_put(info->mti_env, info->mti_object);
3169                 info->mti_object = NULL;
3170         }
3171
3172         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3173                 mdt_lock_handle_fini(&info->mti_lh[i]);
3174         info->mti_env = NULL;
3175         info->mti_pill = NULL;
3176         info->mti_exp = NULL;