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