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