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