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