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