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