Whamcloud - gitweb
45be62670864a9bee422426201709d1ac4019a9f
[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                         repbody->mbo_eadatasize = ma->ma_lmv_size;
1088                         repbody->mbo_valid |= (OBD_MD_FLDIREA |
1089                                                OBD_MD_DEFAULT_MEA);
1090                 }
1091         } else if (S_ISLNK(la->la_mode) &&
1092                    reqbody->mbo_valid & OBD_MD_LINKNAME) {
1093                 buffer->lb_buf = ma->ma_lmm;
1094                 /* eadatasize from client includes NULL-terminator, so
1095                  * there is no need to read it */
1096                 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1097                 rc = mo_readlink(env, next, buffer);
1098                 if (unlikely(rc <= 0)) {
1099                         CERROR("%s: readlink failed for "DFID": rc = %d\n",
1100                                mdt_obd_name(info->mti_mdt),
1101                                PFID(mdt_object_fid(o)), rc);
1102                         rc = -EFAULT;
1103                 } else {
1104                         int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1105
1106                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1107                                 rc -= 2;
1108                         repbody->mbo_valid |= OBD_MD_LINKNAME;
1109                         /* we need to report back size with NULL-terminator
1110                          * because client expects that */
1111                         repbody->mbo_eadatasize = rc + 1;
1112                         if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1113                                 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1114                                        "on "DFID ", expected %d\n",
1115                                        mdt_obd_name(info->mti_mdt),
1116                                        rc, PFID(mdt_object_fid(o)),
1117                                        reqbody->mbo_eadatasize - 1);
1118                         /* NULL terminate */
1119                         ((char *)ma->ma_lmm)[rc] = 0;
1120
1121                         /* If the total CDEBUG() size is larger than a page, it
1122                          * will print a warning to the console, avoid this by
1123                          * printing just the last part of the symlink. */
1124                         CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1125                                print_limit < rc ? "..." : "", print_limit,
1126                                (char *)ma->ma_lmm + rc - print_limit, rc);
1127                         rc = 0;
1128                 }
1129         }
1130
1131         if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1132                 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1133                 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1134                 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1135                        repbody->mbo_max_mdsize);
1136         }
1137
1138 #ifdef CONFIG_FS_POSIX_ACL
1139         if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1140                  (reqbody->mbo_valid & OBD_MD_FLACL)) {
1141                 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1142                 if (IS_ERR(nodemap))
1143                         RETURN(PTR_ERR(nodemap));
1144
1145                 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1146                 nodemap_putref(nodemap);
1147         }
1148 #endif
1149
1150 out:
1151         if (rc == 0)
1152                 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1153
1154         RETURN(rc);
1155 }
1156
1157 static int mdt_getattr(struct tgt_session_info *tsi)
1158 {
1159         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1160         struct mdt_object       *obj = info->mti_object;
1161         struct req_capsule      *pill = info->mti_pill;
1162         struct mdt_body         *reqbody;
1163         struct mdt_body         *repbody;
1164         int rc, rc2;
1165         ENTRY;
1166
1167         reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1168         LASSERT(reqbody);
1169         LASSERT(obj != NULL);
1170         LASSERT(lu_object_assert_exists(&obj->mot_obj));
1171
1172         /* Unlike intent case where we need to pre-fill out buffers early on
1173          * in intent policy for ldlm reasons, here we can have a much better
1174          * guess at EA size by just reading it from disk.
1175          * Exceptions are readdir and (missing) directory striping */
1176         /* Readlink */
1177         if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1178                 /* No easy way to know how long is the symlink, but it cannot
1179                  * be more than PATH_MAX, so we allocate +1 */
1180                 rc = PATH_MAX + 1;
1181
1182         /* A special case for fs ROOT: getattr there might fetch
1183          * default EA for entire fs, not just for this dir!
1184          */
1185         } else if (lu_fid_eq(mdt_object_fid(obj),
1186                              &info->mti_mdt->mdt_md_root_fid) &&
1187                    (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1188                    (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1189                                                                  MDS_GETATTR)) {
1190                 /* Should the default strping be bigger, mdt_fix_reply
1191                  * will reallocate */
1192                 rc = DEF_REP_MD_SIZE;
1193         } else {
1194                 /* Read the actual EA size from disk */
1195                 rc = mdt_attr_get_eabuf_size(info, obj);
1196         }
1197
1198         if (rc < 0)
1199                 GOTO(out, rc = err_serious(rc));
1200
1201         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1202
1203         rc = req_capsule_server_pack(pill);
1204         if (unlikely(rc != 0))
1205                 GOTO(out, rc = err_serious(rc));
1206
1207         repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1208         LASSERT(repbody != NULL);
1209         repbody->mbo_eadatasize = 0;
1210         repbody->mbo_aclsize = 0;
1211
1212         rc = mdt_check_ucred(info);
1213         if (unlikely(rc))
1214                 GOTO(out_shrink, rc);
1215
1216         info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1217
1218         rc = mdt_getattr_internal(info, obj, 0);
1219         EXIT;
1220 out_shrink:
1221         mdt_client_compatibility(info);
1222         rc2 = mdt_fix_reply(info);
1223         if (rc == 0)
1224                 rc = rc2;
1225 out:
1226         mdt_thread_info_fini(info);
1227         return rc;
1228 }
1229
1230 /**
1231  * Exchange MOF_LOV_CREATED flags between two objects after a
1232  * layout swap. No assumption is made on whether o1 or o2 have
1233  * created objects or not.
1234  *
1235  * \param[in,out] o1    First swap layout object
1236  * \param[in,out] o2    Second swap layout object
1237  */
1238 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1239 {
1240         unsigned int o1_lov_created = o1->mot_lov_created;
1241
1242         mutex_lock(&o1->mot_lov_mutex);
1243         mutex_lock(&o2->mot_lov_mutex);
1244
1245         o1->mot_lov_created = o2->mot_lov_created;
1246         o2->mot_lov_created = o1_lov_created;
1247
1248         mutex_unlock(&o2->mot_lov_mutex);
1249         mutex_unlock(&o1->mot_lov_mutex);
1250 }
1251
1252 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1253 {
1254         struct mdt_thread_info  *info;
1255         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1256         struct obd_export       *exp = req->rq_export;
1257         struct mdt_object       *o1, *o2, *o;
1258         struct mdt_lock_handle  *lh1, *lh2;
1259         struct mdc_swap_layouts *msl;
1260         int                      rc;
1261         ENTRY;
1262
1263         /* client does not support layout lock, so layout swaping
1264          * is disabled.
1265          * FIXME: there is a problem for old clients which don't support
1266          * layout lock yet. If those clients have already opened the file
1267          * they won't be notified at all so that old layout may still be
1268          * used to do IO. This can be fixed after file release is landed by
1269          * doing exclusive open and taking full EX ibits lock. - Jinshan */
1270         if (!exp_connect_layout(exp))
1271                 RETURN(-EOPNOTSUPP);
1272
1273         info = tsi2mdt_info(tsi);
1274
1275         if (info->mti_dlm_req != NULL)
1276                 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1277
1278         o1 = info->mti_object;
1279         o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1280                                 &info->mti_body->mbo_fid2);
1281         if (IS_ERR(o))
1282                 GOTO(out, rc = PTR_ERR(o));
1283
1284         if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1285                 GOTO(put, rc = -ENOENT);
1286
1287         rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1288         if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1289                 GOTO(put, rc);
1290
1291         if (rc < 0)
1292                 swap(o1, o2);
1293
1294         /* permission check. Make sure the calling process having permission
1295          * to write both files. */
1296         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1297                                 MAY_WRITE);
1298         if (rc < 0)
1299                 GOTO(put, rc);
1300
1301         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1302                                 MAY_WRITE);
1303         if (rc < 0)
1304                 GOTO(put, rc);
1305
1306         msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1307         if (msl == NULL)
1308                 GOTO(put, rc = -EPROTO);
1309
1310         lh1 = &info->mti_lh[MDT_LH_NEW];
1311         mdt_lock_reg_init(lh1, LCK_EX);
1312         lh2 = &info->mti_lh[MDT_LH_OLD];
1313         mdt_lock_reg_init(lh2, LCK_EX);
1314
1315         rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1316                              MDS_INODELOCK_XATTR);
1317         if (rc < 0)
1318                 GOTO(put, rc);
1319
1320         rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1321                              MDS_INODELOCK_XATTR);
1322         if (rc < 0)
1323                 GOTO(unlock1, rc);
1324
1325         rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1326                              mdt_object_child(o2), msl->msl_flags);
1327         if (rc < 0)
1328                 GOTO(unlock2, rc);
1329
1330         mdt_swap_lov_flag(o1, o2);
1331
1332 unlock2:
1333         mdt_object_unlock(info, o2, lh2, rc);
1334 unlock1:
1335         mdt_object_unlock(info, o1, lh1, rc);
1336 put:
1337         mdt_object_put(info->mti_env, o);
1338 out:
1339         mdt_thread_info_fini(info);
1340         RETURN(rc);
1341 }
1342
1343 static int mdt_raw_lookup(struct mdt_thread_info *info,
1344                           struct mdt_object *parent,
1345                           const struct lu_name *lname,
1346                           struct ldlm_reply *ldlm_rep)
1347 {
1348         struct lu_fid   *child_fid = &info->mti_tmp_fid1;
1349         int              rc;
1350         ENTRY;
1351
1352         LASSERT(!info->mti_cross_ref);
1353
1354         /* Only got the fid of this obj by name */
1355         fid_zero(child_fid);
1356         rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1357                         lname, child_fid, &info->mti_spec);
1358         if (rc == 0) {
1359                 struct mdt_body *repbody;
1360
1361                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1362                 repbody->mbo_fid1 = *child_fid;
1363                 repbody->mbo_valid = OBD_MD_FLID;
1364                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1365         } else if (rc == -ENOENT) {
1366                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1367         }
1368
1369         RETURN(rc);
1370 }
1371
1372 /*
1373  * UPDATE lock should be taken against parent, and be released before exit;
1374  * child_bits lock should be taken against child, and be returned back:
1375  *            (1)normal request should release the child lock;
1376  *            (2)intent request will grant the lock to client.
1377  */
1378 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1379                                  struct mdt_lock_handle *lhc,
1380                                  __u64 child_bits,
1381                                  struct ldlm_reply *ldlm_rep)
1382 {
1383         struct ptlrpc_request  *req       = mdt_info_req(info);
1384         struct mdt_body        *reqbody   = NULL;
1385         struct mdt_object      *parent    = info->mti_object;
1386         struct mdt_object      *child;
1387         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
1388         struct lu_name         *lname     = NULL;
1389         struct mdt_lock_handle *lhp       = NULL;
1390         struct ldlm_lock       *lock;
1391         bool                    is_resent;
1392         bool                    try_layout;
1393         int                     ma_need = 0;
1394         int                     rc;
1395         ENTRY;
1396
1397         is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1398         LASSERT(ergo(is_resent,
1399                      lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1400
1401         if (parent == NULL)
1402                 RETURN(-ENOENT);
1403
1404         if (info->mti_cross_ref) {
1405                 /* Only getattr on the child. Parent is on another node. */
1406                 mdt_set_disposition(info, ldlm_rep,
1407                                     DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1408                 child = parent;
1409                 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1410                        "ldlm_rep = %p\n",
1411                        PFID(mdt_object_fid(child)), ldlm_rep);
1412
1413                 rc = mdt_check_resent_lock(info, child, lhc);
1414                 if (rc < 0) {
1415                         RETURN(rc);
1416                 } else if (rc > 0) {
1417                         mdt_lock_handle_init(lhc);
1418                         mdt_lock_reg_init(lhc, LCK_PR);
1419
1420                         /*
1421                          * Object's name is on another MDS, no lookup or layout
1422                          * lock is needed here but update lock is.
1423                          */
1424                         child_bits &= ~(MDS_INODELOCK_LOOKUP |
1425                                         MDS_INODELOCK_LAYOUT);
1426                         child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1427
1428                         rc = mdt_object_lock(info, child, lhc, child_bits);
1429                         if (rc < 0)
1430                                 RETURN(rc);
1431                 }
1432
1433                 /* Finally, we can get attr for child. */
1434                 if (!mdt_object_exists(child)) {
1435                         LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1436                                         &child->mot_obj,
1437                                         "remote object doesn't exist.");
1438                         mdt_object_unlock(info, child, lhc, 1);
1439                         RETURN(-ENOENT);
1440                 }
1441
1442                 rc = mdt_getattr_internal(info, child, 0);
1443                 if (unlikely(rc != 0))
1444                         mdt_object_unlock(info, child, lhc, 1);
1445
1446                 RETURN(rc);
1447         }
1448
1449         lname = &info->mti_name;
1450         mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1451
1452         if (lu_name_is_valid(lname)) {
1453                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1454                        "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1455                        PNAME(lname), ldlm_rep);
1456         } else {
1457                 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1458                 if (unlikely(reqbody == NULL))
1459                         RETURN(err_serious(-EPROTO));
1460
1461                 *child_fid = reqbody->mbo_fid2;
1462
1463                 if (unlikely(!fid_is_sane(child_fid)))
1464                         RETURN(err_serious(-EINVAL));
1465
1466                 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1467                        "ldlm_rep = %p\n",
1468                        PFID(mdt_object_fid(parent)),
1469                        PFID(&reqbody->mbo_fid2), ldlm_rep);
1470         }
1471
1472         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1473
1474         if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1475                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1476                                 &parent->mot_obj,
1477                                 "Parent doesn't exist!");
1478                 RETURN(-ESTALE);
1479         }
1480
1481         if (mdt_object_remote(parent)) {
1482                 CERROR("%s: parent "DFID" is on remote target\n",
1483                        mdt_obd_name(info->mti_mdt),
1484                        PFID(mdt_object_fid(parent)));
1485                 RETURN(-EIO);
1486         }
1487
1488         if (lu_name_is_valid(lname)) {
1489                 /* Always allow to lookup ".." */
1490                 if (unlikely(lname->ln_namelen == 2 &&
1491                              lname->ln_name[0] == '.' &&
1492                              lname->ln_name[1] == '.'))
1493                         info->mti_spec.sp_permitted = 1;
1494
1495                 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1496                         rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1497
1498                         RETURN(rc);
1499                 }
1500
1501                 /* step 1: lock parent only if parent is a directory */
1502                 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1503                         lhp = &info->mti_lh[MDT_LH_PARENT];
1504                         mdt_lock_pdo_init(lhp, LCK_PR, lname);
1505                         rc = mdt_object_lock(info, parent, lhp,
1506                                              MDS_INODELOCK_UPDATE);
1507                         if (unlikely(rc != 0))
1508                                 RETURN(rc);
1509                 }
1510
1511                 /* step 2: lookup child's fid by name */
1512                 fid_zero(child_fid);
1513                 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1514                                 child_fid, &info->mti_spec);
1515                 if (rc == -ENOENT)
1516                         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1517
1518                 if (rc != 0)
1519                         GOTO(out_parent, rc);
1520         }
1521
1522         mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1523
1524         /*
1525          *step 3: find the child object by fid & lock it.
1526          *        regardless if it is local or remote.
1527          *
1528          *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1529          *      set parent dir fid the same as child fid in getattr by fid case
1530          *      we should not lu_object_find() the object again, could lead
1531          *      to hung if there is a concurrent unlink destroyed the object.
1532          */
1533         if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1534                 mdt_object_get(info->mti_env, parent);
1535                 child = parent;
1536         } else {
1537                 child = mdt_object_find(info->mti_env, info->mti_mdt,
1538                                         child_fid);
1539         }
1540
1541         if (unlikely(IS_ERR(child)))
1542                 GOTO(out_parent, rc = PTR_ERR(child));
1543
1544         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1545         if (!mdt_object_exists(child)) {
1546                 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1547                                 &child->mot_obj,
1548                                 "Object doesn't exist!");
1549                 GOTO(out_child, rc = -ENOENT);
1550         }
1551
1552         rc = mdt_check_resent_lock(info, child, lhc);
1553         if (rc < 0) {
1554                 GOTO(out_child, rc);
1555         } else if (rc > 0) {
1556                 mdt_lock_handle_init(lhc);
1557                 mdt_lock_reg_init(lhc, LCK_PR);
1558                 try_layout = false;
1559
1560                 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1561                       mdt_object_exists(child) && !mdt_object_remote(child)) {
1562                         struct md_attr *ma = &info->mti_attr;
1563
1564                         ma->ma_valid = 0;
1565                         ma->ma_need = MA_INODE;
1566                         rc = mdt_attr_get_complex(info, child, ma);
1567                         if (unlikely(rc != 0))
1568                                 GOTO(out_child, rc);
1569
1570                         /* If the file has not been changed for some time, we
1571                          * return not only a LOOKUP lock, but also an UPDATE
1572                          * lock and this might save us RPC on later STAT. For
1573                          * directories, it also let negative dentry cache start
1574                          * working for this dir. */
1575                         if (ma->ma_valid & MA_INODE &&
1576                             ma->ma_attr.la_valid & LA_CTIME &&
1577                             info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1578                                 ma->ma_attr.la_ctime < cfs_time_current_sec())
1579                                 child_bits |= MDS_INODELOCK_UPDATE;
1580                 }
1581
1582                 /* layout lock must be granted in a best-effort way
1583                  * for IT operations */
1584                 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1585                 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1586                     exp_connect_layout(info->mti_exp) &&
1587                     S_ISREG(lu_object_attr(&child->mot_obj)) &&
1588                     !mdt_object_remote(child) && ldlm_rep != NULL) {
1589                         /* try to grant layout lock for regular file. */
1590                         try_layout = true;
1591                 }
1592
1593                 rc = 0;
1594                 if (try_layout) {
1595                         child_bits |= MDS_INODELOCK_LAYOUT;
1596                         /* try layout lock, it may fail to be granted due to
1597                          * contention at LOOKUP or UPDATE */
1598                         if (!mdt_object_lock_try(info, child, lhc,
1599                                                  child_bits)) {
1600                                 child_bits &= ~MDS_INODELOCK_LAYOUT;
1601                                 LASSERT(child_bits != 0);
1602                                 rc = mdt_object_lock(info, child, lhc,
1603                                                      child_bits);
1604                         } else {
1605                                 ma_need |= MA_LOV;
1606                         }
1607                 } else {
1608                         /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1609                          * client will enqueue the lock to the remote MDT */
1610                         if (mdt_object_remote(child))
1611                                 child_bits &= ~MDS_INODELOCK_UPDATE;
1612                         rc = mdt_object_lock(info, child, lhc, child_bits);
1613                 }
1614                 if (unlikely(rc != 0))
1615                         GOTO(out_child, rc);
1616         }
1617
1618         lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1619
1620         /* finally, we can get attr for child. */
1621         rc = mdt_getattr_internal(info, child, ma_need);
1622         if (unlikely(rc != 0)) {
1623                 mdt_object_unlock(info, child, lhc, 1);
1624         } else if (lock) {
1625                 /* Debugging code. */
1626                 LDLM_DEBUG(lock, "Returning lock to client");
1627                 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1628                                          &lock->l_resource->lr_name),
1629                          "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1630                          PLDLMRES(lock->l_resource),
1631                          PFID(mdt_object_fid(child)));
1632         }
1633         if (lock)
1634                 LDLM_LOCK_PUT(lock);
1635
1636         EXIT;
1637 out_child:
1638         mdt_object_put(info->mti_env, child);
1639 out_parent:
1640         if (lhp)
1641                 mdt_object_unlock(info, parent, lhp, 1);
1642         return rc;
1643 }
1644
1645 /* normal handler: should release the child lock */
1646 static int mdt_getattr_name(struct tgt_session_info *tsi)
1647 {
1648         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
1649         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1650         struct mdt_body        *reqbody;
1651         struct mdt_body        *repbody;
1652         int rc, rc2;
1653         ENTRY;
1654
1655         reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1656         LASSERT(reqbody != NULL);
1657         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1658         LASSERT(repbody != NULL);
1659
1660         info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1661         repbody->mbo_eadatasize = 0;
1662         repbody->mbo_aclsize = 0;
1663
1664         rc = mdt_init_ucred_intent_getattr(info, reqbody);
1665         if (unlikely(rc))
1666                 GOTO(out_shrink, rc);
1667
1668         rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1669         if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1670                 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1671                 lhc->mlh_reg_lh.cookie = 0;
1672         }
1673         mdt_exit_ucred(info);
1674         EXIT;
1675 out_shrink:
1676         mdt_client_compatibility(info);
1677         rc2 = mdt_fix_reply(info);
1678         if (rc == 0)
1679                 rc = rc2;
1680         mdt_thread_info_fini(info);
1681         return rc;
1682 }
1683
1684 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1685                          void *karg, void __user *uarg);
1686
1687 static int mdt_set_info(struct tgt_session_info *tsi)
1688 {
1689         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1690         char                    *key;
1691         void                    *val;
1692         int                      keylen, vallen, rc = 0;
1693
1694         ENTRY;
1695
1696         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1697         if (key == NULL) {
1698                 DEBUG_REQ(D_HA, req, "no set_info key");
1699                 RETURN(err_serious(-EFAULT));
1700         }
1701
1702         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1703                                       RCL_CLIENT);
1704
1705         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1706         if (val == NULL) {
1707                 DEBUG_REQ(D_HA, req, "no set_info val");
1708                 RETURN(err_serious(-EFAULT));
1709         }
1710
1711         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1712                                       RCL_CLIENT);
1713
1714         /* Swab any part of val you need to here */
1715         if (KEY_IS(KEY_READ_ONLY)) {
1716                 spin_lock(&req->rq_export->exp_lock);
1717                 if (*(__u32 *)val)
1718                         *exp_connect_flags_ptr(req->rq_export) |=
1719                                 OBD_CONNECT_RDONLY;
1720                 else
1721                         *exp_connect_flags_ptr(req->rq_export) &=
1722                                 ~OBD_CONNECT_RDONLY;
1723                 spin_unlock(&req->rq_export->exp_lock);
1724         } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1725                 struct changelog_setinfo *cs = val;
1726
1727                 if (vallen != sizeof(*cs)) {
1728                         CERROR("%s: bad changelog_clear setinfo size %d\n",
1729                                tgt_name(tsi->tsi_tgt), vallen);
1730                         RETURN(-EINVAL);
1731                 }
1732                 if (ptlrpc_req_need_swab(req)) {
1733                         __swab64s(&cs->cs_recno);
1734                         __swab32s(&cs->cs_id);
1735                 }
1736
1737                 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1738                                    vallen, val, NULL);
1739         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1740                 if (vallen > 0)
1741                         obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1742         } else {
1743                 RETURN(-EINVAL);
1744         }
1745         RETURN(rc);
1746 }
1747
1748 static int mdt_readpage(struct tgt_session_info *tsi)
1749 {
1750         struct mdt_thread_info  *info = mdt_th_info(tsi->tsi_env);
1751         struct mdt_object       *object = mdt_obj(tsi->tsi_corpus);
1752         struct lu_rdpg          *rdpg = &info->mti_u.rdpg.mti_rdpg;
1753         const struct mdt_body   *reqbody = tsi->tsi_mdt_body;
1754         struct mdt_body         *repbody;
1755         int                      rc;
1756         int                      i;
1757
1758         ENTRY;
1759
1760         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1761                 RETURN(err_serious(-ENOMEM));
1762
1763         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1764         if (repbody == NULL || reqbody == NULL)
1765                 RETURN(err_serious(-EFAULT));
1766
1767         /*
1768          * prepare @rdpg before calling lower layers and transfer itself. Here
1769          * reqbody->size contains offset of where to start to read and
1770          * reqbody->nlink contains number bytes to read.
1771          */
1772         rdpg->rp_hash = reqbody->mbo_size;
1773         if (rdpg->rp_hash != reqbody->mbo_size) {
1774                 CERROR("Invalid hash: %#llx != %#llx\n",
1775                        rdpg->rp_hash, reqbody->mbo_size);
1776                 RETURN(-EFAULT);
1777         }
1778
1779         rdpg->rp_attrs = reqbody->mbo_mode;
1780         if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1781                 rdpg->rp_attrs |= LUDA_64BITHASH;
1782         rdpg->rp_count  = min_t(unsigned int, reqbody->mbo_nlink,
1783                                 exp_max_brw_size(tsi->tsi_exp));
1784         rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1785                           PAGE_SHIFT;
1786         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1787         if (rdpg->rp_pages == NULL)
1788                 RETURN(-ENOMEM);
1789
1790         for (i = 0; i < rdpg->rp_npages; ++i) {
1791                 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1792                 if (rdpg->rp_pages[i] == NULL)
1793                         GOTO(free_rdpg, rc = -ENOMEM);
1794         }
1795
1796         /* call lower layers to fill allocated pages with directory data */
1797         rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1798         if (rc < 0)
1799                 GOTO(free_rdpg, rc);
1800
1801         /* send pages to client */
1802         rc = tgt_sendpage(tsi, rdpg, rc);
1803
1804         EXIT;
1805 free_rdpg:
1806
1807         for (i = 0; i < rdpg->rp_npages; i++)
1808                 if (rdpg->rp_pages[i] != NULL)
1809                         __free_page(rdpg->rp_pages[i]);
1810         OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1811
1812         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1813                 RETURN(0);
1814
1815         return rc;
1816 }
1817
1818 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1819 {
1820         struct lu_ucred *uc = mdt_ucred_check(info);
1821         struct lu_attr *attr = &info->mti_attr.ma_attr;
1822
1823         if (uc == NULL)
1824                 return -EINVAL;
1825
1826         if (op != REINT_SETATTR) {
1827                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1828                         attr->la_uid = uc->uc_fsuid;
1829                 /* for S_ISGID, inherit gid from his parent, such work will be
1830                  * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1831                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1832                         attr->la_gid = uc->uc_fsgid;
1833         }
1834
1835         return 0;
1836 }
1837
1838 static int mdt_reint_internal(struct mdt_thread_info *info,
1839                               struct mdt_lock_handle *lhc,
1840                               __u32 op)
1841 {
1842         struct req_capsule      *pill = info->mti_pill;
1843         struct mdt_body         *repbody;
1844         int                      rc = 0, rc2;
1845
1846         ENTRY;
1847
1848         rc = mdt_reint_unpack(info, op);
1849         if (rc != 0) {
1850                 CERROR("Can't unpack reint, rc %d\n", rc);
1851                 RETURN(err_serious(rc));
1852         }
1853
1854         /* for replay (no_create) lmm is not needed, client has it already */
1855         if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1856                 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1857                                      DEF_REP_MD_SIZE);
1858
1859         /* llog cookies are always 0, the field is kept for compatibility */
1860         if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1861                 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1862
1863         rc = req_capsule_server_pack(pill);
1864         if (rc != 0) {
1865                 CERROR("Can't pack response, rc %d\n", rc);
1866                 RETURN(err_serious(rc));
1867         }
1868
1869         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1870                 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1871                 LASSERT(repbody);
1872                 repbody->mbo_eadatasize = 0;
1873                 repbody->mbo_aclsize = 0;
1874         }
1875
1876         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1877
1878         /* for replay no cookkie / lmm need, because client have this already */
1879         if (info->mti_spec.no_create)
1880                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1881                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1882
1883         rc = mdt_init_ucred_reint(info);
1884         if (rc)
1885                 GOTO(out_shrink, rc);
1886
1887         rc = mdt_fix_attr_ucred(info, op);
1888         if (rc != 0)
1889                 GOTO(out_ucred, rc = err_serious(rc));
1890
1891         rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1892         if (rc < 0) {
1893                 GOTO(out_ucred, rc);
1894         } else if (rc == 1) {
1895                 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1896                 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1897                 GOTO(out_ucred, rc);
1898         }
1899         rc = mdt_reint_rec(info, lhc);
1900         EXIT;
1901 out_ucred:
1902         mdt_exit_ucred(info);
1903 out_shrink:
1904         mdt_client_compatibility(info);
1905         rc2 = mdt_fix_reply(info);
1906         if (rc == 0)
1907                 rc = rc2;
1908         return rc;
1909 }
1910
1911 static long mdt_reint_opcode(struct ptlrpc_request *req,
1912                              const struct req_format **fmt)
1913 {
1914         struct mdt_device       *mdt;
1915         struct mdt_rec_reint    *rec;
1916         long                     opc;
1917
1918         rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1919         if (rec != NULL) {
1920                 opc = rec->rr_opcode;
1921                 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1922                 if (opc < REINT_MAX && fmt[opc] != NULL)
1923                         req_capsule_extend(&req->rq_pill, fmt[opc]);
1924                 else {
1925                         mdt = mdt_exp2dev(req->rq_export);
1926                         CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1927                                " rc = %d\n", req->rq_export->exp_obd->obd_name,
1928                                opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1929                         opc = err_serious(-EFAULT);
1930                 }
1931         } else {
1932                 opc = err_serious(-EFAULT);
1933         }
1934         return opc;
1935 }
1936
1937 static int mdt_reint(struct tgt_session_info *tsi)
1938 {
1939         long opc;
1940         int  rc;
1941         static const struct req_format *reint_fmts[REINT_MAX] = {
1942                 [REINT_SETATTR]  = &RQF_MDS_REINT_SETATTR,
1943                 [REINT_CREATE]   = &RQF_MDS_REINT_CREATE,
1944                 [REINT_LINK]     = &RQF_MDS_REINT_LINK,
1945                 [REINT_UNLINK]   = &RQF_MDS_REINT_UNLINK,
1946                 [REINT_RENAME]   = &RQF_MDS_REINT_RENAME,
1947                 [REINT_OPEN]     = &RQF_MDS_REINT_OPEN,
1948                 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1949                 [REINT_RMENTRY]  = &RQF_MDS_REINT_UNLINK,
1950                 [REINT_MIGRATE]  = &RQF_MDS_REINT_RENAME
1951         };
1952
1953         ENTRY;
1954
1955         opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1956         if (opc >= 0) {
1957                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1958                 /*
1959                  * No lock possible here from client to pass it to reint code
1960                  * path.
1961                  */
1962                 rc = mdt_reint_internal(info, NULL, opc);
1963                 mdt_thread_info_fini(info);
1964         } else {
1965                 rc = opc;
1966         }
1967
1968         tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1969         RETURN(rc);
1970 }
1971
1972 /* this should sync the whole device */
1973 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1974 {
1975         struct dt_device *dt = mdt->mdt_bottom;
1976         int rc;
1977         ENTRY;
1978
1979         rc = dt->dd_ops->dt_sync(env, dt);
1980         RETURN(rc);
1981 }
1982
1983 /* this should sync this object */
1984 static int mdt_object_sync(struct mdt_thread_info *info)
1985 {
1986         struct md_object *next;
1987         int rc;
1988         ENTRY;
1989
1990         if (!mdt_object_exists(info->mti_object)) {
1991                 CWARN("%s: non existing object "DFID": rc = %d\n",
1992                       mdt_obd_name(info->mti_mdt),
1993                       PFID(mdt_object_fid(info->mti_object)), -ESTALE);
1994                 RETURN(-ESTALE);
1995         }
1996         next = mdt_object_child(info->mti_object);
1997         rc = mo_object_sync(info->mti_env, next);
1998
1999         RETURN(rc);
2000 }
2001
2002 static int mdt_sync(struct tgt_session_info *tsi)
2003 {
2004         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2005         struct req_capsule      *pill = tsi->tsi_pill;
2006         struct mdt_body         *body;
2007         int                      rc;
2008
2009         ENTRY;
2010
2011         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2012                 RETURN(err_serious(-ENOMEM));
2013
2014         if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2015                 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2016         } else {
2017                 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2018
2019                 /* sync an object */
2020                 rc = mdt_object_sync(info);
2021                 if (rc == 0) {
2022                         const struct lu_fid *fid;
2023                         struct lu_attr *la = &info->mti_attr.ma_attr;
2024
2025                         info->mti_attr.ma_need = MA_INODE;
2026                         info->mti_attr.ma_valid = 0;
2027                         rc = mdt_attr_get_complex(info, info->mti_object,
2028                                                   &info->mti_attr);
2029                         if (rc == 0) {
2030                                 body = req_capsule_server_get(pill,
2031                                                               &RMF_MDT_BODY);
2032                                 fid = mdt_object_fid(info->mti_object);
2033                                 mdt_pack_attr2body(info, body, la, fid);
2034                         }
2035                 }
2036                 mdt_thread_info_fini(info);
2037         }
2038         if (rc == 0)
2039                 mdt_counter_incr(req, LPROC_MDT_SYNC);
2040
2041         RETURN(rc);
2042 }
2043
2044 /*
2045  * Handle quota control requests to consult current usage/limit, but also
2046  * to configure quota enforcement
2047  */
2048 static int mdt_quotactl(struct tgt_session_info *tsi)
2049 {
2050         struct obd_export       *exp  = tsi->tsi_exp;
2051         struct req_capsule      *pill = tsi->tsi_pill;
2052         struct obd_quotactl     *oqctl, *repoqc;
2053         int                      id, rc;
2054         struct mdt_device       *mdt = mdt_exp2dev(exp);
2055         struct lu_device        *qmt = mdt->mdt_qmt_dev;
2056         struct lu_nodemap       *nodemap;
2057         ENTRY;
2058
2059         oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2060         if (oqctl == NULL)
2061                 RETURN(err_serious(-EPROTO));
2062
2063         rc = req_capsule_server_pack(pill);
2064         if (rc)
2065                 RETURN(err_serious(rc));
2066
2067         nodemap = nodemap_get_from_exp(exp);
2068         if (IS_ERR(nodemap))
2069                 RETURN(PTR_ERR(nodemap));
2070
2071         switch (oqctl->qc_cmd) {
2072                 /* master quotactl */
2073         case Q_SETINFO:
2074         case Q_SETQUOTA:
2075                 if (!nodemap_can_setquota(nodemap))
2076                         GOTO(out_nodemap, rc = -EPERM);
2077         case Q_GETINFO:
2078         case Q_GETQUOTA:
2079                 if (qmt == NULL)
2080                         GOTO(out_nodemap, rc = -EOPNOTSUPP);
2081                 /* slave quotactl */
2082         case Q_GETOINFO:
2083         case Q_GETOQUOTA:
2084                 break;
2085         default:
2086                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2087                 GOTO(out_nodemap, rc = -EFAULT);
2088         }
2089
2090         id = oqctl->qc_id;
2091         if (oqctl->qc_type == USRQUOTA)
2092                 id = nodemap_map_id(nodemap, NODEMAP_UID,
2093                                     NODEMAP_CLIENT_TO_FS, id);
2094         else if (oqctl->qc_type == GRPQUOTA)
2095                 id = nodemap_map_id(nodemap, NODEMAP_UID,
2096                                     NODEMAP_CLIENT_TO_FS, id);
2097
2098         repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2099         if (repoqc == NULL)
2100                 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2101
2102         if (oqctl->qc_id != id)
2103                 swap(oqctl->qc_id, id);
2104
2105         switch (oqctl->qc_cmd) {
2106
2107         case Q_GETINFO:
2108         case Q_SETINFO:
2109         case Q_SETQUOTA:
2110         case Q_GETQUOTA:
2111                 /* forward quotactl request to QMT */
2112                 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2113                 break;
2114
2115         case Q_GETOINFO:
2116         case Q_GETOQUOTA:
2117                 /* slave quotactl */
2118                 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2119                                    oqctl);
2120                 break;
2121
2122         default:
2123                 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2124                 GOTO(out_nodemap, rc = -EFAULT);
2125         }
2126
2127         if (oqctl->qc_id != id)
2128                 swap(oqctl->qc_id, id);
2129
2130         *repoqc = *oqctl;
2131
2132         EXIT;
2133
2134 out_nodemap:
2135         nodemap_putref(nodemap);
2136
2137         return rc;
2138 }
2139
2140 /** clone llog ctxt from child (mdd)
2141  * This allows remote llog (replicator) access.
2142  * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2143  * context was originally set up, or we can handle them directly.
2144  * I choose the latter, but that means I need any llog
2145  * contexts set up by child to be accessable by the mdt.  So we clone the
2146  * context into our context list here.
2147  */
2148 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2149                                int idx)
2150 {
2151         struct md_device  *next = mdt->mdt_child;
2152         struct llog_ctxt *ctxt;
2153         int rc;
2154
2155         if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2156                 return 0;
2157
2158         rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2159         if (rc || ctxt == NULL) {
2160                 return 0;
2161         }
2162
2163         rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2164         if (rc)
2165                 CERROR("Can't set mdt ctxt %d\n", rc);
2166
2167         return rc;
2168 }
2169
2170 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2171                                  struct mdt_device *mdt, int idx)
2172 {
2173         struct llog_ctxt *ctxt;
2174
2175         ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2176         if (ctxt == NULL)
2177                 return 0;
2178         /* Put once for the get we just did, and once for the clone */
2179         llog_ctxt_put(ctxt);
2180         llog_ctxt_put(ctxt);
2181         return 0;
2182 }
2183
2184 /*
2185  * sec context handlers
2186  */
2187 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2188 {
2189         CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2190
2191         return 0;
2192 }
2193
2194 /*
2195  * quota request handlers
2196  */
2197 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2198 {
2199         struct mdt_device       *mdt = mdt_exp2dev(tsi->tsi_exp);
2200         struct lu_device        *qmt = mdt->mdt_qmt_dev;
2201         int                      rc;
2202         ENTRY;
2203
2204         if (qmt == NULL)
2205                 RETURN(err_serious(-EOPNOTSUPP));
2206
2207         rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2208         RETURN(rc);
2209 }
2210
2211 struct mdt_object *mdt_object_new(const struct lu_env *env,
2212                                   struct mdt_device *d,
2213                                   const struct lu_fid *f)
2214 {
2215         struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2216         struct lu_object *o;
2217         struct mdt_object *m;
2218         ENTRY;
2219
2220         CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2221         o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2222         if (unlikely(IS_ERR(o)))
2223                 m = (struct mdt_object *)o;
2224         else
2225                 m = mdt_obj(o);
2226         RETURN(m);
2227 }
2228
2229 struct mdt_object *mdt_object_find(const struct lu_env *env,
2230                                    struct mdt_device *d,
2231                                    const struct lu_fid *f)
2232 {
2233         struct lu_object *o;
2234         struct mdt_object *m;
2235         ENTRY;
2236
2237         CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2238         o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2239         if (unlikely(IS_ERR(o)))
2240                 m = (struct mdt_object *)o;
2241         else
2242                 m = mdt_obj(o);
2243
2244         RETURN(m);
2245 }
2246
2247 /**
2248  * Asyncronous commit for mdt device.
2249  *
2250  * Pass asynchonous commit call down the MDS stack.
2251  *
2252  * \param env environment
2253  * \param mdt the mdt device
2254  */
2255 static void mdt_device_commit_async(const struct lu_env *env,
2256                                     struct mdt_device *mdt)
2257 {
2258         struct dt_device *dt = mdt->mdt_bottom;
2259         int rc;
2260         ENTRY;
2261
2262         rc = dt->dd_ops->dt_commit_async(env, dt);
2263         if (unlikely(rc != 0))
2264                 CWARN("%s: async commit start failed: rc = %d\n",
2265                       mdt_obd_name(mdt), rc);
2266         atomic_inc(&mdt->mdt_async_commit_count);
2267         EXIT;
2268 }
2269
2270 /**
2271  * Mark the lock as "synchonous".
2272  *
2273  * Mark the lock to deffer transaction commit to the unlock time.
2274  *
2275  * \param lock the lock to mark as "synchonous"
2276  *
2277  * \see mdt_is_lock_sync
2278  * \see mdt_save_lock
2279  */
2280 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2281 {
2282         lock->l_ast_data = (void*)1;
2283 }
2284
2285 /**
2286  * Check whehter the lock "synchonous" or not.
2287  *
2288  * \param lock the lock to check
2289  * \retval 1 the lock is "synchonous"
2290  * \retval 0 the lock isn't "synchronous"
2291  *
2292  * \see mdt_set_lock_sync
2293  * \see mdt_save_lock
2294  */
2295 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2296 {
2297         return lock->l_ast_data != NULL;
2298 }
2299
2300 /**
2301  * Blocking AST for mdt locks.
2302  *
2303  * Starts transaction commit if in case of COS lock conflict or
2304  * deffers such a commit to the mdt_save_lock.
2305  *
2306  * \param lock the lock which blocks a request or cancelling lock
2307  * \param desc unused
2308  * \param data unused
2309  * \param flag indicates whether this cancelling or blocking callback
2310  * \retval 0
2311  * \see ldlm_blocking_ast_nocheck
2312  */
2313 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2314                      void *data, int flag)
2315 {
2316         struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2317         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2318         int rc;
2319         ENTRY;
2320
2321         if (flag == LDLM_CB_CANCELING)
2322                 RETURN(0);
2323
2324         lock_res_and_lock(lock);
2325         if (lock->l_blocking_ast != mdt_blocking_ast) {
2326                 unlock_res_and_lock(lock);
2327                 RETURN(0);
2328         }
2329         if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2330             lock->l_blocking_lock != NULL) {
2331                 if (mdt_cos_is_enabled(mdt) &&
2332                     lock->l_client_cookie !=
2333                     lock->l_blocking_lock->l_client_cookie)
2334                         mdt_set_lock_sync(lock);
2335                 else if (mdt_slc_is_enabled(mdt) &&
2336                          ldlm_is_cos_incompat(lock->l_blocking_lock))
2337                         mdt_set_lock_sync(lock);
2338         }
2339         rc = ldlm_blocking_ast_nocheck(lock);
2340
2341         /* There is no lock conflict if l_blocking_lock == NULL,
2342          * it indicates a blocking ast sent from ldlm_lock_decref_internal
2343          * when the last reference to a local lock was released */
2344         if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2345                 struct lu_env env;
2346
2347                 rc = lu_env_init(&env, LCT_LOCAL);
2348                 if (unlikely(rc != 0))
2349                         CWARN("%s: lu_env initialization failed, cannot "
2350                               "start asynchronous commit: rc = %d\n",
2351                               obd->obd_name, rc);
2352                 else
2353                         mdt_device_commit_async(&env, mdt);
2354                 lu_env_fini(&env);
2355         }
2356         RETURN(rc);
2357 }
2358
2359 /*
2360  * Blocking AST for cross-MDT lock
2361  *
2362  * Discard lock from uncommitted_slc_locks and cancel it.
2363  *
2364  * \param lock  the lock which blocks a request or cancelling lock
2365  * \param desc  unused
2366  * \param data  unused
2367  * \param flag  indicates whether this cancelling or blocking callback
2368  * \retval      0 on success
2369  * \retval      negative number on error
2370  */
2371 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2372                             void *data, int flag)
2373 {
2374         int rc = 0;
2375         ENTRY;
2376
2377         switch (flag) {
2378         case LDLM_CB_BLOCKING: {
2379                 struct lustre_handle lockh;
2380
2381                 ldlm_lock2handle(lock, &lockh);
2382                 rc = ldlm_cli_cancel(&lockh,
2383                         ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2384                 if (rc < 0) {
2385                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2386                         RETURN(rc);
2387                 }
2388                 break;
2389         }
2390         case LDLM_CB_CANCELING: {
2391                 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2392                 struct mdt_device *mdt =
2393                                 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2394
2395                 LDLM_DEBUG(lock, "Revoke remote lock\n");
2396
2397                 /* discard slc lock here so that it can be cleaned anytime,
2398                  * especially for cleanup_resource() */
2399                 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2400
2401                 /* once we cache lock, l_ast_data is set to mdt_object */
2402                 if (lock->l_ast_data != NULL) {
2403                         struct mdt_object *mo = lock->l_ast_data;
2404                         struct lu_env env;
2405
2406                         rc = lu_env_init(&env, LCT_MD_THREAD);
2407                         if (unlikely(rc != 0)) {
2408                                 CWARN("%s: lu_env initialization failed, object"
2409                                       "%p "DFID" is leaked!\n",
2410                                       obd->obd_name, mo,
2411                                       PFID(mdt_object_fid(mo)));
2412                                 RETURN(rc);
2413                         }
2414
2415                         if (lock->l_policy_data.l_inodebits.bits &
2416                             (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2417                                 rc = mo_invalidate(&env, mdt_object_child(mo));
2418                                 mo->mot_cache_attr = 0;
2419                         }
2420                         mdt_object_put(&env, mo);
2421                         lu_env_fini(&env);
2422                 }
2423                 break;
2424         }
2425         default:
2426                 LBUG();
2427         }
2428
2429         RETURN(rc);
2430 }
2431
2432 int mdt_check_resent_lock(struct mdt_thread_info *info,
2433                           struct mdt_object *mo,
2434                           struct mdt_lock_handle *lhc)
2435 {
2436         /* the lock might already be gotten in ldlm_handle_enqueue() */
2437         if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2438                 struct ptlrpc_request *req = mdt_info_req(info);
2439                 struct ldlm_lock      *lock;
2440
2441                 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2442                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2443                 if (lock == NULL) {
2444                         /* Lock is pinned by ldlm_handle_enqueue0() as it is
2445                          * a resend case, however, it could be already destroyed
2446                          * due to client eviction or a raced cancel RPC. */
2447                         LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2448                                           lhc->mlh_reg_lh.cookie);
2449                         RETURN(-ESTALE);
2450                 }
2451
2452                 if (!fid_res_name_eq(mdt_object_fid(mo),
2453                                      &lock->l_resource->lr_name)) {
2454                         CWARN("%s: Although resent, but still not "
2455                               "get child lock:"DFID"\n",
2456                               info->mti_exp->exp_obd->obd_name,
2457                               PFID(mdt_object_fid(mo)));
2458                         LDLM_LOCK_PUT(lock);
2459                         RETURN(-EPROTO);
2460                 }
2461                 LDLM_LOCK_PUT(lock);
2462                 return 0;
2463         }
2464         return 1;
2465 }
2466
2467 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2468                            const struct lu_fid *fid, struct lustre_handle *lh,
2469                            enum ldlm_mode mode, __u64 ibits, bool nonblock,
2470                            bool cache)
2471 {
2472         struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2473         union ldlm_policy_data *policy = &mti->mti_policy;
2474         struct ldlm_res_id *res_id = &mti->mti_res_id;
2475         int rc = 0;
2476         ENTRY;
2477
2478         LASSERT(mdt_object_remote(o));
2479
2480         fid_build_reg_res_name(fid, res_id);
2481
2482         memset(einfo, 0, sizeof(*einfo));
2483         einfo->ei_type = LDLM_IBITS;
2484         einfo->ei_mode = mode;
2485         einfo->ei_cb_bl = mdt_remote_blocking_ast;
2486         einfo->ei_cb_cp = ldlm_completion_ast;
2487         einfo->ei_enq_slave = 0;
2488         einfo->ei_res_id = res_id;
2489         if (nonblock)
2490                 einfo->ei_nonblock = 1;
2491         if (cache) {
2492                 /*
2493                  * if we cache lock, couple lock with mdt_object, so that object
2494                  * can be easily found in lock ASTs.
2495                  */
2496                 mdt_object_get(mti->mti_env, o);
2497                 einfo->ei_cbdata = o;
2498         }
2499
2500         memset(policy, 0, sizeof(*policy));
2501         policy->l_inodebits.bits = ibits;
2502
2503         rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2504                             policy);
2505         if (rc < 0 && cache) {
2506                 mdt_object_put(mti->mti_env, o);
2507                 einfo->ei_cbdata = NULL;
2508         }
2509         RETURN(rc);
2510 }
2511
2512 static int mdt_object_local_lock(struct mdt_thread_info *info,
2513                                  struct mdt_object *o,
2514                                  struct mdt_lock_handle *lh, __u64 ibits,
2515                                  bool nonblock, bool cos_incompat)
2516 {
2517         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2518         union ldlm_policy_data *policy = &info->mti_policy;
2519         struct ldlm_res_id *res_id = &info->mti_res_id;
2520         __u64 dlmflags = 0;
2521         int rc;
2522         ENTRY;
2523
2524         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2525         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2526         LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2527         LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2528
2529         if (cos_incompat) {
2530                 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2531                         lh->mlh_reg_mode == LCK_EX);
2532                 dlmflags |= LDLM_FL_COS_INCOMPAT;
2533         } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2534                 dlmflags |= LDLM_FL_COS_ENABLED;
2535         }
2536
2537         /* Only enqueue LOOKUP lock for remote object */
2538         if (mdt_object_remote(o))
2539                 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2540
2541         if (lh->mlh_type == MDT_PDO_LOCK) {
2542                 /* check for exists after object is locked */
2543                 if (mdt_object_exists(o) == 0) {
2544                         /* Non-existent object shouldn't have PDO lock */
2545                         RETURN(-ESTALE);
2546                 } else {
2547                         /* Non-dir object shouldn't have PDO lock */
2548                         if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2549                                 RETURN(-ENOTDIR);
2550                 }
2551         }
2552
2553         memset(policy, 0, sizeof(*policy));
2554         fid_build_reg_res_name(mdt_object_fid(o), res_id);
2555
2556         dlmflags |= LDLM_FL_ATOMIC_CB;
2557         if (nonblock)
2558                 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2559
2560         /*
2561          * Take PDO lock on whole directory and build correct @res_id for lock
2562          * on part of directory.
2563          */
2564         if (lh->mlh_pdo_hash != 0) {
2565                 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2566                 mdt_lock_pdo_mode(info, o, lh);
2567                 if (lh->mlh_pdo_mode != LCK_NL) {
2568                         /*
2569                          * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2570                          * is never going to be sent to client and we do not
2571                          * want it slowed down due to possible cancels.
2572                          */
2573                         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2574                         rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2575                                           policy, res_id, dlmflags,
2576                                           info->mti_exp == NULL ? NULL :
2577                                           &info->mti_exp->exp_handle.h_cookie);
2578                         if (unlikely(rc != 0))
2579                                 GOTO(out_unlock, rc);
2580                 }
2581
2582                 /*
2583                  * Finish res_id initializing by name hash marking part of
2584                  * directory which is taking modification.
2585                  */
2586                 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2587         }
2588
2589         policy->l_inodebits.bits = ibits;
2590
2591         /*
2592          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2593          * going to be sent to client. If it is - mdt_intent_policy() path will
2594          * fix it up and turn FL_LOCAL flag off.
2595          */
2596         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2597                           res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2598                           info->mti_exp == NULL ? NULL :
2599                           &info->mti_exp->exp_handle.h_cookie);
2600 out_unlock:
2601         if (rc != 0)
2602                 mdt_object_unlock(info, o, lh, 1);
2603         else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2604                    lh->mlh_pdo_hash != 0 &&
2605                    (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2606                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2607
2608         RETURN(rc);
2609 }
2610
2611 static int
2612 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2613                          struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2614                          bool cos_incompat)
2615 {
2616         struct mdt_lock_handle *local_lh = NULL;
2617         int rc;
2618         ENTRY;
2619
2620         if (!mdt_object_remote(o)) {
2621                 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2622                                            cos_incompat);
2623                 RETURN(rc);
2624         }
2625
2626         /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2627         ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2628                    MDS_INODELOCK_XATTR);
2629
2630         /* Only enqueue LOOKUP lock for remote object */
2631         if (ibits & MDS_INODELOCK_LOOKUP) {
2632                 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2633                                            nonblock, cos_incompat);
2634                 if (rc != ELDLM_OK)
2635                         RETURN(rc);
2636
2637                 local_lh = lh;
2638         }
2639
2640         if (ibits & MDS_INODELOCK_UPDATE) {
2641                 /* Sigh, PDO needs to enqueue 2 locks right now, but
2642                  * enqueue RPC can only request 1 lock, to avoid extra
2643                  * RPC, so it will instead enqueue EX lock for remote
2644                  * object anyway XXX*/
2645                 if (lh->mlh_type == MDT_PDO_LOCK &&
2646                     lh->mlh_pdo_hash != 0) {
2647                         CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2648                                "EX lock.\n", mdt_obd_name(info->mti_mdt),
2649                                PFID(mdt_object_fid(o)));
2650                         lh->mlh_pdo_hash = 0;
2651                         lh->mlh_rreg_mode = LCK_EX;
2652                         lh->mlh_type = MDT_REG_LOCK;
2653                 }
2654                 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2655                                             &lh->mlh_rreg_lh,
2656                                             lh->mlh_rreg_mode,
2657                                             MDS_INODELOCK_UPDATE, nonblock,
2658                                             false);
2659                 if (rc != ELDLM_OK) {
2660                         if (local_lh != NULL)
2661                                 mdt_object_unlock(info, o, local_lh, rc);
2662                         RETURN(rc);
2663                 }
2664         }
2665
2666         RETURN(0);
2667 }
2668
2669 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2670                     struct mdt_lock_handle *lh, __u64 ibits)
2671 {
2672         return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2673 }
2674
2675 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2676                           struct mdt_lock_handle *lh, __u64 ibits,
2677                           bool cos_incompat)
2678 {
2679         LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2680         return mdt_object_lock_internal(info, o, lh, ibits, false,
2681                                         cos_incompat);
2682 }
2683
2684 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2685                         struct mdt_lock_handle *lh, __u64 ibits)
2686 {
2687         struct mdt_lock_handle tmp = *lh;
2688         int rc;
2689
2690         rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2691         if (rc == 0)
2692                 *lh = tmp;
2693
2694         return rc == 0;
2695 }
2696
2697 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2698                               struct mdt_object *o, struct mdt_lock_handle *lh,
2699                               __u64 ibits, bool cos_incompat)
2700 {
2701         struct mdt_lock_handle tmp = *lh;
2702         int rc;
2703
2704         LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2705         rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2706         if (rc == 0)
2707                 *lh = tmp;
2708
2709         return rc == 0;
2710 }
2711
2712 /**
2713  * Save a lock within request object.
2714  *
2715  * Keep the lock referenced until whether client ACK or transaction
2716  * commit happens or release the lock immediately depending on input
2717  * parameters. If COS is ON, a write lock is converted to COS lock
2718  * before saving.
2719  *
2720  * \param info thead info object
2721  * \param h lock handle
2722  * \param mode lock mode
2723  * \param decref force immediate lock releasing
2724  */
2725 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2726                           enum ldlm_mode mode, int decref)
2727 {
2728         ENTRY;
2729
2730         if (lustre_handle_is_used(h)) {
2731                 if (decref || !info->mti_has_trans ||
2732                     !(mode & (LCK_PW | LCK_EX))) {
2733                         mdt_fid_unlock(h, mode);
2734                 } else {
2735                         struct mdt_device *mdt = info->mti_mdt;
2736                         struct ldlm_lock *lock = ldlm_handle2lock(h);
2737                         struct ptlrpc_request *req = mdt_info_req(info);
2738                         int cos;
2739
2740                         cos = (mdt_cos_is_enabled(mdt) ||
2741                                mdt_slc_is_enabled(mdt));
2742
2743                         LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
2744                                  h->cookie);
2745
2746                         /* there is no request if mdt_object_unlock() is called
2747                          * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2748                         if (likely(req != NULL)) {
2749                                 CDEBUG(D_HA, "request = %p reply state = %p"
2750                                        " transno = %lld\n", req,
2751                                        req->rq_reply_state, req->rq_transno);
2752                                 if (cos) {
2753                                         ldlm_lock_downgrade(lock, LCK_COS);
2754                                         mode = LCK_COS;
2755                                 }
2756                                 ptlrpc_save_lock(req, h, mode, cos);
2757                         } else {
2758                                 mdt_fid_unlock(h, mode);
2759                         }
2760                         if (mdt_is_lock_sync(lock)) {
2761                                 CDEBUG(D_HA, "found sync-lock,"
2762                                        " async commit started\n");
2763                                 mdt_device_commit_async(info->mti_env,
2764                                                         mdt);
2765                         }
2766                         LDLM_LOCK_PUT(lock);
2767                 }
2768                 h->cookie = 0ull;
2769         }
2770
2771         EXIT;
2772 }
2773
2774 /**
2775  * Save cross-MDT lock in uncommitted_slc_locks
2776  *
2777  * Keep the lock referenced until transaction commit happens or release the lock
2778  * immediately depending on input parameters.
2779  *
2780  * \param info thead info object
2781  * \param h lock handle
2782  * \param mode lock mode
2783  * \param decref force immediate lock releasing
2784  */
2785 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2786                                  struct mdt_object *o, struct lustre_handle *h,
2787                                  enum ldlm_mode mode, int decref)
2788 {
2789         ENTRY;
2790
2791         if (lustre_handle_is_used(h)) {
2792                 struct ldlm_lock *lock = ldlm_handle2lock(h);
2793
2794                 if (o != NULL &&
2795                     (lock->l_policy_data.l_inodebits.bits &
2796                      (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2797                         mo_invalidate(info->mti_env, mdt_object_child(o));
2798
2799                 if (decref || !info->mti_has_trans ||
2800                     !(mode & (LCK_PW | LCK_EX))) {
2801                         ldlm_lock_decref_and_cancel(h, mode);
2802                         LDLM_LOCK_PUT(lock);
2803                 } else {
2804                         struct ptlrpc_request *req = mdt_info_req(info);
2805
2806                         LASSERT(req != NULL);
2807                         tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
2808                                           req->rq_transno);
2809                         ldlm_lock_decref(h, mode);
2810                 }
2811                 h->cookie = 0ull;
2812         }
2813
2814         EXIT;
2815 }
2816
2817 /**
2818  * Unlock mdt object.
2819  *
2820  * Immeditely release the regular lock and the PDO lock or save the
2821  * lock in request and keep them referenced until client ACK or
2822  * transaction commit.
2823  *
2824  * \param info thread info object
2825  * \param o mdt object
2826  * \param lh mdt lock handle referencing regular and PDO locks
2827  * \param decref force immediate lock releasing
2828  *
2829  * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
2830  */
2831 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2832                        struct mdt_lock_handle *lh, int decref)
2833 {
2834         ENTRY;
2835
2836         mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2837         mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2838         mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
2839                              decref);
2840
2841         EXIT;
2842 }
2843
2844 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2845                                         const struct lu_fid *f,
2846                                         struct mdt_lock_handle *lh,
2847                                         __u64 ibits)
2848 {
2849         struct mdt_object *o;
2850
2851         o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2852         if (!IS_ERR(o)) {
2853                 int rc;
2854
2855                 rc = mdt_object_lock(info, o, lh, ibits);
2856                 if (rc != 0) {
2857                         mdt_object_put(info->mti_env, o);
2858                         o = ERR_PTR(rc);
2859                 }
2860         }
2861         return o;
2862 }
2863
2864 void mdt_object_unlock_put(struct mdt_thread_info * info,
2865                            struct mdt_object * o,
2866                            struct mdt_lock_handle *lh,
2867                            int decref)
2868 {
2869         mdt_object_unlock(info, o, lh, decref);
2870         mdt_object_put(info->mti_env, o);
2871 }
2872
2873 /*
2874  * Generic code handling requests that have struct mdt_body passed in:
2875  *
2876  *  - extract mdt_body from request and save it in @info, if present;
2877  *
2878  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
2879  *  @info;
2880  *
2881  *  - if HABEO_CORPUS flag is set for this request type check whether object
2882  *  actually exists on storage (lu_object_exists()).
2883  *
2884  */
2885 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2886 {
2887         const struct mdt_body    *body;
2888         struct mdt_object        *obj;
2889         const struct lu_env      *env;
2890         struct req_capsule       *pill;
2891         int                       rc;
2892         ENTRY;
2893
2894         env = info->mti_env;
2895         pill = info->mti_pill;
2896
2897         body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2898         if (body == NULL)
2899                 RETURN(-EFAULT);
2900
2901         if (!(body->mbo_valid & OBD_MD_FLID))
2902                 RETURN(0);
2903
2904         if (!fid_is_sane(&body->mbo_fid1)) {
2905                 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2906                 RETURN(-EINVAL);
2907         }
2908
2909         obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2910         if (!IS_ERR(obj)) {
2911                 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
2912                         mdt_object_put(env, obj);
2913                         rc = -ENOENT;
2914                 } else {
2915                         info->mti_object = obj;
2916                         rc = 0;
2917                 }
2918         } else
2919                 rc = PTR_ERR(obj);
2920
2921         RETURN(rc);
2922 }
2923
2924 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2925 {
2926         struct req_capsule *pill = info->mti_pill;
2927         int rc;
2928         ENTRY;
2929
2930         if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2931                 rc = mdt_body_unpack(info, flags);
2932         else
2933                 rc = 0;
2934
2935         if (rc == 0 && (flags & HABEO_REFERO)) {
2936                 /* Pack reply. */
2937                 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2938                         req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2939                                              DEF_REP_MD_SIZE);
2940                 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2941                         req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2942                                              RCL_SERVER, 0);
2943
2944                 rc = req_capsule_server_pack(pill);
2945         }
2946         RETURN(rc);
2947 }
2948
2949 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2950 {
2951         lh->mlh_type = MDT_NUL_LOCK;
2952         lh->mlh_reg_lh.cookie = 0ull;
2953         lh->mlh_reg_mode = LCK_MINMODE;
2954         lh->mlh_pdo_lh.cookie = 0ull;
2955         lh->mlh_pdo_mode = LCK_MINMODE;
2956         lh->mlh_rreg_lh.cookie = 0ull;
2957         lh->mlh_rreg_mode = LCK_MINMODE;
2958 }
2959
2960 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2961 {
2962         LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2963         LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2964 }
2965
2966 /*
2967  * Initialize fields of struct mdt_thread_info. Other fields are left in
2968  * uninitialized state, because it's too expensive to zero out whole
2969  * mdt_thread_info (> 1K) on each request arrival.
2970  */
2971 void mdt_thread_info_init(struct ptlrpc_request *req,
2972                           struct mdt_thread_info *info)
2973 {
2974         int i;
2975
2976         info->mti_pill = &req->rq_pill;
2977
2978         /* lock handle */
2979         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2980                 mdt_lock_handle_init(&info->mti_lh[i]);
2981
2982         /* mdt device: it can be NULL while CONNECT */
2983         if (req->rq_export) {
2984                 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2985                 info->mti_exp = req->rq_export;
2986         } else
2987                 info->mti_mdt = NULL;
2988         info->mti_env = req->rq_svc_thread->t_env;
2989         info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2990
2991         memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2992         info->mti_big_buf = LU_BUF_NULL;
2993         info->mti_body = NULL;
2994         info->mti_object = NULL;
2995         info->mti_dlm_req = NULL;
2996         info->mti_has_trans = 0;
2997         info->mti_cross_ref = 0;
2998         info->mti_opdata = 0;
2999         info->mti_big_lmm_used = 0;
3000
3001         info->mti_spec.no_create = 0;
3002         info->mti_spec.sp_rm_entry = 0;
3003         info->mti_spec.sp_permitted = 0;
3004         info->mti_spec.sp_migrate_close = 0;
3005
3006         info->mti_spec.u.sp_ea.eadata = NULL;
3007         info->mti_spec.u.sp_ea.eadatalen = 0;
3008 }
3009
3010 void mdt_thread_info_fini(struct mdt_thread_info *info)
3011 {
3012         int i;
3013
3014         if (info->mti_object != NULL) {
3015                 mdt_object_put(info->mti_env, info->mti_object);
3016                 info->mti_object = NULL;
3017         }
3018
3019         for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3020                 mdt_lock_handle_fini(&info->mti_lh[i]);
3021         info->mti_env = NULL;
3022         info->mti_pill = NULL;
3023         info->mti_exp = NULL;
3024
3025         if (unlikely(info->mti_big_buf.lb_buf != NULL))
3026                 lu_buf_free(&info->mti_big_buf);
3027 }
3028
3029 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3030 {
3031         struct mdt_thread_info  *mti;
3032
3033         mti = mdt_th_info(tsi->tsi_env);
3034         LASSERT(mti != NULL);
3035
3036         mdt_thread_info_init(tgt_ses_req(tsi), mti);
3037         if (tsi->tsi_corpus != NULL) {
3038                 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3039                 lu_object_get(tsi->tsi_corpus);
3040         }
3041         mti->mti_body = tsi->tsi_mdt_body;
3042         mti->mti_dlm_req = tsi->tsi_dlm_req;
3043
3044         return mti;
3045 }
3046
3047 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3048 {
3049         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3050             cfs_fail_val ==
3051             tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3052                 set_current_state(TASK_UNINTERRUPTIBLE);
3053                 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3054         }
3055
3056         return tgt_connect(tsi);
3057 }
3058
3059 enum mdt_it_code {
3060         MDT_IT_OPEN,
3061         MDT_IT_OCREAT,
3062         MDT_IT_CREATE,
3063         MDT_IT_GETATTR,
3064         MDT_IT_READDIR,
3065         MDT_IT_LOOKUP,
3066         MDT_IT_UNLINK,
3067         MDT_IT_TRUNC,
3068         MDT_IT_GETXATTR,
3069         MDT_IT_LAYOUT,
3070         MDT_IT_QUOTA,
3071         MDT_IT_NR
3072 };
3073
3074 static int mdt_intent_getattr(enum mdt_it_code opcode,
3075                               struct mdt_thread_info *info,
3076                               struct ldlm_lock **,
3077                               __u64);
3078
3079 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3080                                 struct mdt_thread_info *info,
3081                                 struct ldlm_lock **lockp,
3082                                 __u64 flags);
3083
3084 static int mdt_intent_layout(enum mdt_it_code opcode,
3085                              struct mdt_thread_info *info,
3086                              struct ldlm_lock **,
3087                              __u64);
3088 static int mdt_intent_reint(enum mdt_it_code opcode,
3089                             struct mdt_thread_info *info,
3090                             struct ldlm_lock **,
3091                             __u64);
3092
3093 static struct mdt_it_flavor {
3094         const struct req_format *it_fmt;
3095         __u32                    it_flags;
3096         int                    (*it_act)(enum mdt_it_code ,
3097                                          struct mdt_thread_info *,
3098                                          struct ldlm_lock **,
3099                                          __u64);
3100         long                     it_reint;
3101 } mdt_it_flavor[] = {
3102         [MDT_IT_OPEN]     = {
3103                 .it_fmt   = &RQF_LDLM_INTENT,
3104                 /*.it_flags = HABEO_REFERO,*/
3105                 .it_flags = 0,
3106                 .it_act   = mdt_intent_reint,
3107                 .it_reint = REINT_OPEN
3108         },
3109         [MDT_IT_OCREAT]   = {
3110                 .it_fmt   = &RQF_LDLM_INTENT,
3111                 /*
3112                  * OCREAT is not a MUTABOR request as if the file
3113                  * already exists.
3114                  * We do the extra check of OBD_CONNECT_RDONLY in
3115                  * mdt_reint_open() when we really need to create
3116                  * the object.
3117                  */
3118                 .it_flags = 0,
3119                 .it_act   = mdt_intent_reint,
3120                 .it_reint = REINT_OPEN
3121         },
3122         [MDT_IT_CREATE]   = {
3123                 .it_fmt   = &RQF_LDLM_INTENT,
3124                 .it_flags = MUTABOR,
3125                 .it_act   = mdt_intent_reint,
3126                 .it_reint = REINT_CREATE
3127         },
3128         [MDT_IT_GETATTR]  = {
3129                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3130                 .it_flags = HABEO_REFERO,
3131                 .it_act   = mdt_intent_getattr
3132         },
3133         [MDT_IT_READDIR]  = {
3134                 .it_fmt   = NULL,
3135                 .it_flags = 0,
3136                 .it_act   = NULL
3137         },
3138         [MDT_IT_LOOKUP]   = {
3139                 .it_fmt   = &RQF_LDLM_INTENT_GETATTR,
3140                 .it_flags = HABEO_REFERO,
3141                 .it_act   = mdt_intent_getattr
3142         },
3143         [MDT_IT_UNLINK]   = {
3144                 .it_fmt   = &RQF_LDLM_INTENT_UNLINK,
3145                 .it_flags = MUTABOR,
3146                 .it_act   = NULL,
3147                 .it_reint = REINT_UNLINK
3148         },
3149         [MDT_IT_TRUNC]    = {
3150                 .it_fmt   = NULL,
3151                 .it_flags = MUTABOR,
3152                 .it_act   = NULL
3153         },
3154         [MDT_IT_GETXATTR] = {
3155                 .it_fmt   = &RQF_LDLM_INTENT_GETXATTR,
3156                 .it_flags = HABEO_CORPUS,
3157                 .it_act   = mdt_intent_getxattr
3158         },
3159         [MDT_IT_LAYOUT] = {
3160                 .it_fmt   = &RQF_LDLM_INTENT_LAYOUT,
3161                 .it_flags = 0,
3162                 .it_act   = mdt_intent_layout
3163         }
3164 };
3165
3166 static int
3167 mdt_intent_lock_replace(struct mdt_thread_info *info,
3168                         struct ldlm_lock **lockp,
3169                         struct mdt_lock_handle *lh,
3170                         __u64 flags, int result)
3171 {
3172         struct ptlrpc_request  *req = mdt_info_req(info);
3173         struct ldlm_lock       *lock = *lockp;
3174         struct ldlm_lock       *new_lock;
3175
3176         /* If possible resent found a lock, @lh is set to its handle */
3177         new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3178
3179         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3180                 lh->mlh_reg_lh.cookie = 0;
3181                 RETURN(0);
3182         }
3183
3184         if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3185                 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3186                  * a resend case, however, it could be already destroyed
3187                  * due to client eviction or a raced cancel RPC. */
3188                 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
3189                                   lh->mlh_reg_lh.cookie);
3190                 lh->mlh_reg_lh.cookie = 0;
3191                 RETURN(-ESTALE);