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