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