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