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