4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * This file is part of Lustre, http://www.lustre.org/
25 * lustre/mdt/mdt_restriper.c
27 * Lustre directory restripe and auto-split
30 #define DEBUG_SUBSYSTEM S_MDS
32 #include <linux/sched.h>
33 #include <linux/kthread.h>
34 #include "mdt_internal.h"
36 /* add directory into splitting list and wake up restripe thread */
37 void mdt_auto_split_add(struct mdt_thread_info *info, struct mdt_object *o)
39 struct mdt_device *mdt = info->mti_mdt;
40 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
42 spin_lock(&restriper->mdr_lock);
43 if (mdt->mdt_enable_dir_auto_split && !o->mot_restriping) {
44 o->mot_restriping = 1;
45 mdt_object_get(NULL, o);
46 LASSERT(list_empty(&o->mot_restripe_linkage));
47 list_add_tail(&o->mot_restripe_linkage,
48 &restriper->mdr_auto_splitting);
50 CDEBUG(D_INFO, "add "DFID" into auto split list.\n",
51 PFID(mdt_object_fid(o)));
53 spin_unlock(&restriper->mdr_lock);
55 wake_up_process(restriper->mdr_task);
58 void mdt_restripe_migrate_add(struct mdt_thread_info *info,
61 struct mdt_device *mdt = info->mti_mdt;
62 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
64 spin_lock(&restriper->mdr_lock);
65 if (!o->mot_restriping) {
66 o->mot_restriping = 1;
67 o->mot_restripe_offset = 0;
68 mdt_object_get(NULL, o);
69 LASSERT(list_empty(&o->mot_restripe_linkage));
70 list_add_tail(&o->mot_restripe_linkage,
71 &restriper->mdr_migrating);
73 CDEBUG(D_INFO, "add "DFID" into migrate list.\n",
74 PFID(mdt_object_fid(o)));
76 spin_unlock(&restriper->mdr_lock);
78 wake_up_process(restriper->mdr_task);
81 void mdt_restripe_update_add(struct mdt_thread_info *info,
84 struct mdt_device *mdt = info->mti_mdt;
85 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
87 spin_lock(&restriper->mdr_lock);
88 if (!o->mot_restriping) {
90 o->mot_restriping = 1;
91 mdt_object_get(NULL, o);
92 if (list_empty(&restriper->mdr_updating))
93 restriper->mdr_update_time = ktime_get_real_seconds();
94 LASSERT(list_empty(&o->mot_restripe_linkage));
95 list_add_tail(&o->mot_restripe_linkage,
96 &restriper->mdr_updating);
98 CDEBUG(D_INFO, "add "DFID" into update list.\n",
99 PFID(mdt_object_fid(o)));
101 spin_unlock(&restriper->mdr_lock);
103 wake_up_process(restriper->mdr_task);
106 static inline int mdt_fid_alloc(const struct lu_env *env,
107 struct mdt_device *mdt,
109 struct mdt_object *parent,
110 const struct lu_name *name)
112 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
113 struct lu_object *o = lu_object_next(&parent->mot_obj);
115 return next->ld_ops->ldo_fid_alloc(env, next, fid, o, name);
118 static void mdt_auto_split_prep(struct mdt_thread_info *info,
119 struct md_op_spec *spec,
121 u32 lum_stripe_count)
123 struct lu_attr *attr = &ma->ma_attr;
124 struct lmv_user_md_v1 *lum;
126 attr->la_ctime = attr->la_mtime = ktime_get_real_seconds();
127 attr->la_valid = LA_CTIME | LA_MTIME;
129 lum = &info->mti_mdt->mdt_restriper.mdr_lmv.lmv_user_md;
130 lum->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
131 lum->lum_stripe_count = cpu_to_le32(lum_stripe_count);
132 lum->lum_stripe_offset = cpu_to_le32(LMV_OFFSET_DEFAULT);
133 lum->lum_hash_type = 0;
135 spec->u.sp_ea.eadatalen = sizeof(*lum);
136 spec->u.sp_ea.eadata = lum;
137 spec->sp_cr_flags = MDS_OPEN_HAS_EA;
139 spec->sp_migrate_close = 0;
142 /* restripe directory: split or merge stripes */
143 int mdt_restripe_internal(struct mdt_thread_info *info,
144 struct mdt_object *parent,
145 struct mdt_object *child,
146 const struct lu_name *lname,
148 struct md_op_spec *spec,
151 const struct lu_env *env = info->mti_env;
152 struct mdt_device *mdt = info->mti_mdt;
153 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
154 struct lmv_mds_md_v1 *lmv;
155 u32 lmv_stripe_count = 0;
160 rc = mdt_stripe_get(info, child, ma, XATTR_NAME_LMV);
164 if (ma->ma_valid & MA_LMV) {
165 lmv = &ma->ma_lmv->lmv_md_v1;
166 if (!lmv_is_sane(lmv))
169 /* don't allow restripe if dir layout is changing */
170 if (lmv_is_layout_changing(lmv))
173 /* check whether stripe count and hash unchanged */
174 if (lum->lum_stripe_count == lmv->lmv_stripe_count &&
175 lum->lum_hash_type == lmv->lmv_hash_type)
178 lmv_stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
179 } else if (le32_to_cpu(lum->lum_stripe_count) < 2) {
180 /* stripe count unchanged for plain directory */
184 if (le32_to_cpu(lum->lum_stripe_count) > lmv_stripe_count) {
186 struct md_layout_change *mlc = &info->mti_mlc;
187 struct mdt_object *tobj = NULL;
188 s64 mtime = ma->ma_attr.la_mtime;
190 ma->ma_need = MA_INODE;
192 rc = mdt_attr_get_complex(info, child, ma);
196 if (!(ma->ma_valid & MA_INODE))
199 /* mtime is from from client or set outside */
200 ma->ma_attr.la_mtime = mtime;
202 if (!lmv_stripe_count) {
203 /* if child is plain directory, allocate @tobj as the
204 * master object, and make child the first stripe of
207 tobj = mdt_object_new(env, mdt, tfid);
208 if (unlikely(IS_ERR(tobj)))
209 RETURN(PTR_ERR(tobj));
212 mlc->mlc_opc = MD_LAYOUT_SPLIT;
213 mlc->mlc_parent = mdt_object_child(parent);
214 mlc->mlc_target = tobj ? mdt_object_child(tobj) : NULL;
215 mlc->mlc_attr = &ma->ma_attr;
216 mlc->mlc_name = lname;
217 mlc->mlc_spec = spec;
218 rc = mo_layout_change(env, mdt_object_child(child), mlc);
220 /* FID and attr need to be replied to client for manual
223 ma->ma_need = MA_INODE;
225 rc = mdt_attr_get_complex(info,
226 lmv_stripe_count ? child : tobj, ma);
229 mdt_object_put(env, tobj);
231 *tfid = *mdt_object_fid(child);
233 /* merge only needs to override LMV */
234 struct lu_buf *buf = &info->mti_buf;
237 LASSERT(ma->ma_valid & MA_LMV);
238 lmv = &ma->ma_lmv->lmv_md_v1;
239 version = cpu_to_le32(lmv->lmv_layout_version);
242 if (lum->lum_stripe_count == 0)
243 lum->lum_stripe_count = cpu_to_le32(1);
245 lmv->lmv_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MERGE |
246 LMV_HASH_FLAG_MIGRATION);
247 lmv->lmv_hash_type |= lum->lum_hash_type &
248 cpu_to_le32(LMV_HASH_FLAG_FIXED);
249 lmv->lmv_merge_offset = lum->lum_stripe_count;
250 lmv->lmv_merge_hash = lum->lum_hash_type;
251 lmv->lmv_layout_version = cpu_to_le32(++version);
254 buf->lb_len = sizeof(*lmv);
255 rc = mo_xattr_set(env, mdt_object_child(child), buf,
256 XATTR_NAME_LMV, LU_XATTR_REPLACE);
260 *tfid = *mdt_object_fid(child);
261 ma->ma_need = MA_INODE;
263 rc = mdt_attr_get_complex(info, child, ma);
269 static int mdt_auto_split(struct mdt_thread_info *info)
271 const struct lu_env *env = info->mti_env;
272 struct mdt_device *mdt = info->mti_mdt;
273 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
274 struct md_attr *ma = &info->mti_attr;
275 struct md_op_spec *spec = &info->mti_spec;
276 struct lu_name *lname = &info->mti_name;
277 struct lu_fid *fid = &info->mti_tmp_fid2;
278 struct mdt_object *parent = NULL;
279 struct mdt_object *child = NULL;
280 struct mdt_object *stripe = NULL;
281 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
282 struct mdt_lock_handle *lhp;
283 struct mdt_lock_handle *lhc;
284 u32 lmv_stripe_count = 0;
285 u32 lum_stripe_count = 0;
290 if (!atomic_read(&mdt->mdt_mds_mds_conns))
293 spin_lock(&restriper->mdr_lock);
294 if (!list_empty(&restriper->mdr_auto_splitting)) {
295 child = list_entry(restriper->mdr_auto_splitting.next,
296 typeof(*child), mot_restripe_linkage);
297 list_del_init(&child->mot_restripe_linkage);
299 spin_unlock(&restriper->mdr_lock);
304 LASSERT(child->mot_restriping);
306 rc = mdt_stripe_get(info, child, ma, XATTR_NAME_LMV);
310 if (ma->ma_valid & MA_LMV) {
311 /* stripe dirent exceeds threshold, find its master object */
312 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
314 /* auto-split won't be done on striped directory master object
315 * directly, because it's triggered when dirent count exceeds
316 * threshold, however dirent count of master object is its
319 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_STRIPE)
320 GOTO(out, rc = -EINVAL);
322 /* race with migrate? */
323 if (lmv_hash_is_migrating(cpu_to_le32(lmv->lmv_hash_type)))
324 GOTO(out, rc = -EBUSY);
326 lmv_stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
328 /* save stripe to clear 'restriping' flag in the end to avoid
329 * trigger auto-split multiple times.
334 /* get master object FID from linkea */
335 rc = mdt_attr_get_pfid(info, stripe, &ma->ma_pfid);
339 child = mdt_object_find(env, mdt, &ma->ma_pfid);
341 GOTO(out, rc = PTR_ERR(child));
343 spin_lock(&restriper->mdr_lock);
344 if (child->mot_restriping) {
346 spin_unlock(&restriper->mdr_lock);
347 GOTO(out, rc = -EBUSY);
349 child->mot_restriping = 1;
350 spin_unlock(&restriper->mdr_lock);
352 /* skip if master object is remote, let the first stripe
353 * to start splitting because dir split needs to be done
354 * on where master object is.
356 if (mdt_object_remote(child))
357 GOTO(restriping_clear, rc = -EREMOTE);
360 /* striped directory split adds mdr_auto_split_delta stripes */
361 lum_stripe_count = min_t(unsigned int,
363 mdt->mdt_restriper.mdr_dir_split_delta,
364 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
365 if (lmv_stripe_count >= lum_stripe_count)
366 GOTO(restriping_clear, rc = -EALREADY);
368 /* get dir name and parent FID */
369 rc = mdt_attr_get_pfid_name(info, child, fid, lname);
371 GOTO(restriping_clear, rc);
373 /* copy name out because mti_linkea will be used later, and name should
376 memcpy(info->mti_filename, lname->ln_name, lname->ln_namelen);
377 info->mti_filename[lname->ln_namelen] = '\0';
378 lname->ln_name = info->mti_filename;
379 CDEBUG(D_INFO, "split "DFID"/"DNAME" to count %u (MDT count %d)\n",
380 PFID(fid), PNAME(lname), lum_stripe_count,
381 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
383 parent = mdt_object_find(env, mdt, fid);
385 GOTO(restriping_clear, rc = PTR_ERR(parent));
387 rc = mdt_fid_alloc(env, mdt, fid, child, NULL);
389 GOTO(restriping_clear, rc);
391 lhp = &info->mti_lh[MDT_LH_PARENT];
392 rc = mdt_parent_lock(info, parent, lhp, lname, LCK_PW);
394 GOTO(restriping_clear, rc);
396 lhc = &info->mti_lh[MDT_LH_CHILD];
397 rc = mdt_object_stripes_lock(info, parent, child, lhc, einfo,
398 MDS_INODELOCK_FULL, LCK_EX);
400 GOTO(unlock_parent, rc);
402 mdt_auto_split_prep(info, spec, ma, lum_stripe_count);
404 rc = mdt_restripe_internal(info, parent, child, lname, fid, spec, ma);
407 mdt_object_stripes_unlock(info, child, lhc, einfo, rc);
409 mdt_object_unlock(info, parent, lhp, rc);
411 child->mot_restriping = 0;
412 LASSERT(list_empty(&child->mot_restripe_linkage));
414 /* -EALREADY: dir is split already.
415 * -EBUSY: dir is opened, or is splitting by others.
416 * -EREMOTE: dir is remote.
418 if (rc && rc != -EALREADY && rc != -EBUSY && rc != -EREMOTE)
419 CERROR("%s: split "DFID"/"DNAME" to count %u failed: rc = %d\n",
420 mdt_obd_name(mdt), PFID(mdt_object_fid(child)),
421 PNAME(lname), lum_stripe_count, rc);
423 if (!IS_ERR_OR_NULL(child))
424 mdt_object_put(env, child);
427 LASSERT(stripe->mot_restriping);
428 LASSERT(list_empty(&stripe->mot_restripe_linkage));
429 stripe->mot_restriping = 0;
430 /* lock may not be taken, don't cache stripe LMV */
431 mo_invalidate(env, mdt_object_child(stripe));
432 mdt_object_put(env, stripe);
435 if (!IS_ERR_OR_NULL(parent))
436 mdt_object_put(env, parent);
441 /* sub-files under one stripe are migrated, clear MIGRATION flag in its LMV */
442 static int mdt_restripe_migrate_finish(struct mdt_thread_info *info,
443 struct mdt_object *stripe,
444 struct lmv_mds_md_v1 *lmv)
446 struct mdt_device *mdt = info->mti_mdt;
448 struct mdt_lock_handle *lh;
453 LASSERT(le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE);
454 LASSERT(lmv_is_restriping(lmv));
456 lmv->lmv_hash_type &= ~cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
458 buf.lb_len = sizeof(*lmv);
460 lh = &info->mti_lh[MDT_LH_PARENT];
461 rc = mdt_object_lock(info, stripe, lh, MDS_INODELOCK_XATTR, LCK_EX);
463 rc = mo_xattr_set(info->mti_env, mdt_object_child(stripe), &buf,
464 XATTR_NAME_LMV, LU_XATTR_REPLACE);
465 mdt_object_unlock(info, stripe, lh, rc);
467 CERROR("%s: update "DFID" LMV failed: rc = %d\n",
468 mdt_obd_name(mdt), PFID(mdt_object_fid(stripe)), rc);
470 LASSERT(!list_empty(&stripe->mot_restripe_linkage));
471 LASSERT(stripe->mot_restriping);
473 spin_lock(&mdt->mdt_lock);
474 stripe->mot_restriping = 0;
475 list_del_init(&stripe->mot_restripe_linkage);
476 spin_unlock(&mdt->mdt_lock);
478 mdt_object_put(info->mti_env, stripe);
483 static void mdt_restripe_migrate_prep(struct mdt_thread_info *info,
484 const struct lu_fid *fid1,
485 const struct lu_fid *fid2,
486 const struct lu_name *lname,
488 const struct lmv_mds_md_v1 *lmv)
490 struct lu_attr *attr = &info->mti_attr.ma_attr;
491 struct mdt_reint_record *rr = &info->mti_rr;
492 struct md_op_spec *spec = &info->mti_spec;
493 struct lmv_user_md_v1 *lum;
495 attr->la_ctime = attr->la_mtime = ktime_get_real_seconds();
496 attr->la_valid = LA_CTIME | LA_MTIME;
500 rr->rr_name = *lname;
502 lum = &info->mti_mdt->mdt_restriper.mdr_lmv.lmv_user_md;
503 lum->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
504 lum->lum_stripe_offset = cpu_to_le32(LMV_OFFSET_DEFAULT);
505 if (lmv_is_splitting(lmv)) {
506 lum->lum_stripe_count = lmv->lmv_stripe_count;
508 lmv->lmv_hash_type & le32_to_cpu(LMV_HASH_TYPE_MASK);
509 } else if (lmv_is_merging(lmv)) {
510 lum->lum_stripe_count = lmv->lmv_merge_offset;
511 lum->lum_hash_type = lmv->lmv_merge_hash;
514 spec->u.sp_ea.eadatalen = sizeof(*lum);
515 spec->u.sp_ea.eadata = lum;
516 spec->sp_cr_flags = MDS_OPEN_HAS_EA;
518 spec->sp_migrate_close = 0;
519 /* if 'nsonly' is set, don't migrate inode */
521 spec->sp_migrate_nsonly = 1;
523 spec->sp_migrate_nsonly =
524 info->mti_mdt->mdt_dir_restripe_nsonly;
527 /* migrate sub-file from @mdr_restripe_offset */
528 static int mdt_restripe_migrate(struct mdt_thread_info *info)
530 const struct lu_env *env = info->mti_env;
531 struct mdt_device *mdt = info->mti_mdt;
532 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
533 struct mdt_object *stripe = NULL;
534 struct mdt_object *master = NULL;
535 struct md_attr *ma = &info->mti_attr;
536 struct lmv_mds_md_v1 *lmv;
537 struct lu_name *lname = &info->mti_name;
538 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
541 struct lu_dirpage *dp;
542 struct lu_dirent *ent;
543 const char *name = NULL;
552 if (list_empty(&restriper->mdr_migrating))
555 stripe = list_entry(restriper->mdr_migrating.next, typeof(*stripe),
556 mot_restripe_linkage);
558 /* get master object FID and stripe name */
559 rc = mdt_attr_get_pfid_name(info, stripe, &fid1, lname);
563 snprintf(info->mti_filename, sizeof(info->mti_filename), DFID,
564 PFID(mdt_object_fid(stripe)));
565 len = strlen(info->mti_filename) + 1;
566 if (len >= lname->ln_namelen)
567 GOTO(out, rc = -EBADF);
569 while (len < lname->ln_namelen) {
570 if (!isdigit(lname->ln_name[len]))
571 GOTO(out, rc = -EBADF);
573 idx = idx * 10 + lname->ln_name[len++] - '0';
576 /* check whether stripe is newly created in split */
577 rc = mdt_stripe_get(info, stripe, ma, XATTR_NAME_LMV);
581 if (!(ma->ma_valid & MA_LMV))
582 GOTO(out, rc = -ENODATA);
584 lmv = &ma->ma_lmv->lmv_md_v1;
585 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_STRIPE)
586 GOTO(out, rc = -EBADF);
588 if (!lmv_is_restriping(lmv))
589 GOTO(out, rc = -EINVAL);
591 if ((lmv_is_splitting(lmv) &&
592 idx >= le32_to_cpu(lmv->lmv_split_offset)) ||
593 (lmv_is_merging(lmv) &&
594 ((le32_to_cpu(lmv->lmv_hash_type) & LMV_HASH_TYPE_MASK) ==
595 LMV_HASH_TYPE_CRUSH ||
596 (le32_to_cpu(lmv->lmv_hash_type) & LMV_HASH_TYPE_MASK) ==
597 LMV_HASH_TYPE_CRUSH2) &&
598 idx < le32_to_cpu(lmv->lmv_merge_offset))) {
599 /* new stripes doesn't need to migrate sub files in dir
600 * split, neither for target stripes in dir merge if hash type
601 * is CRUSH or CRUSH2.
603 rc = mdt_restripe_migrate_finish(info, stripe, lmv);
607 /* get sub file name @mot_restripe_offset.
608 * TODO: read one dirent instead of whole page.
610 rdpg->rp_hash = stripe->mot_restripe_offset;
611 rdpg->rp_count = PAGE_SIZE;
613 rdpg->rp_attrs = LUDA_64BITHASH | LUDA_FID | LUDA_TYPE;
614 rdpg->rp_pages = &restriper->mdr_page;
615 rc = mo_readpage(env, mdt_object_child(stripe), rdpg);
619 dp = page_address(restriper->mdr_page);
620 for (ent = lu_dirent_start(dp); ent; ent = lu_dirent_next(ent)) {
621 LASSERT(le64_to_cpu(ent->lde_hash) >= rdpg->rp_hash);
623 if (unlikely(!(le32_to_cpu(ent->lde_attrs) & LUDA_TYPE)))
624 GOTO(out, rc = -EINVAL);
626 namelen = le16_to_cpu(ent->lde_namelen);
630 if (name_is_dot_or_dotdot(ent->lde_name, namelen))
633 name = ent->lde_name;
634 type = lu_dirent_type_get(ent);
639 if (le64_to_cpu(dp->ldp_hash_end) == MDS_DIR_END_OFF) {
640 rc = mdt_restripe_migrate_finish(info, stripe, lmv);
644 GOTO(out, rc = -EBADF);
647 /* copy name out because it should end with '\0' */
648 memcpy(info->mti_filename, name, namelen);
649 info->mti_filename[namelen] = '\0';
650 lname->ln_name = info->mti_filename;
651 lname->ln_namelen = namelen;
653 CDEBUG(D_INFO, "migrate "DFID"/"DNAME" type %ho\n",
654 PFID(&fid1), PNAME(lname), type);
656 master = mdt_object_find(env, mdt, &fid1);
658 GOTO(out, rc = PTR_ERR(master));
660 rc = mdt_fid_alloc(env, mdt, &fid2, master, lname);
661 mdt_object_put(env, master);
665 mdt_restripe_migrate_prep(info, &fid1, &fid2, lname, type, lmv);
667 rc = mdt_reint_migrate(info, NULL);
668 /* mti_big_buf is allocated in XATTR migration */
669 if (unlikely(info->mti_big_buf.lb_buf))
670 lu_buf_free(&info->mti_big_buf);
678 ent = lu_dirent_next(ent);
682 namelen = le16_to_cpu(ent->lde_namelen);
683 } while (namelen == 0); /* Skip dummy record */
686 stripe->mot_restripe_offset = le64_to_cpu(ent->lde_hash);
688 stripe->mot_restripe_offset = le64_to_cpu(dp->ldp_hash_end);
693 /* -EBUSY: file is opened by others */
695 CERROR("%s: migrate "DFID"/"DNAME" failed: rc = %d\n",
696 mdt_obd_name(mdt), PFID(&fid1), PNAME(lname),
699 spin_lock(&mdt->mdt_lock);
700 stripe->mot_restriping = 0;
701 list_del_init(&stripe->mot_restripe_linkage);
702 spin_unlock(&mdt->mdt_lock);
704 mdt_object_put(env, stripe);
710 static inline bool mdt_restripe_update_pending(struct mdt_thread_info *info)
712 struct mdt_device *mdt = info->mti_mdt;
714 if (list_empty(&mdt->mdt_restriper.mdr_updating))
717 return mdt->mdt_restriper.mdr_update_time < ktime_get_real_seconds();
720 static void mdt_restripe_layout_update_prep(struct mdt_thread_info *info,
721 const struct lu_fid *fid,
722 const struct lmv_mds_md_v1 *lmv)
724 struct lu_attr *attr = &info->mti_attr.ma_attr;
725 struct mdt_reint_record *rr = &info->mti_rr;
726 struct lmv_user_md_v1 *lum;
728 attr->la_ctime = attr->la_mtime = ktime_get_real_seconds();
729 attr->la_valid = LA_CTIME | LA_MTIME;
731 strncpy(info->mti_filename, XATTR_NAME_LMV,
732 sizeof(info->mti_filename));
734 lum = &info->mti_mdt->mdt_restriper.mdr_lmv.lmv_user_md;
735 lum->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
736 lum->lum_stripe_offset = cpu_to_le32(LMV_OFFSET_DEFAULT);
737 if (lmv_is_splitting(lmv)) {
738 lum->lum_stripe_count = lmv->lmv_stripe_count;
740 lmv->lmv_hash_type & le32_to_cpu(LMV_HASH_TYPE_MASK);
741 } else if (lmv_is_merging(lmv)) {
742 lum->lum_stripe_count = lmv->lmv_merge_offset;
743 lum->lum_hash_type = lmv->lmv_merge_hash;
746 rr->rr_opcode = REINT_SETXATTR;
748 rr->rr_name.ln_name = info->mti_filename;
749 rr->rr_name.ln_namelen = strlen(info->mti_filename);
751 rr->rr_eadatalen = sizeof(*lum);
754 static int mdt_restripe_layout_update(struct mdt_thread_info *info)
756 const struct lu_env *env = info->mti_env;
757 struct mdt_device *mdt = info->mti_mdt;
758 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
759 struct md_attr *ma = &info->mti_attr;
760 struct lu_fid *fid = &info->mti_tmp_fid1;
761 struct mdt_object *master;
762 struct mdt_object *stripe;
763 struct lmv_mds_md_v1 *lmv;
769 if (list_empty(&restriper->mdr_updating))
772 master = list_entry(restriper->mdr_updating.next, typeof(*master),
773 mot_restripe_linkage);
775 rc = mdt_stripe_get(info, master, ma, XATTR_NAME_LMV);
779 if (!(ma->ma_valid & MA_LMV))
780 GOTO(out, rc = -ENODATA);
782 lmv = &ma->ma_lmv->lmv_md_v1;
783 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
784 GOTO(out, rc = -EBADF);
786 if (!lmv_is_restriping(lmv))
787 GOTO(out, rc = -EINVAL);
789 /* use different buffer to store stripe LMV */
790 ma->ma_lmv = &restriper->mdr_lmv;
791 ma->ma_lmv_size = sizeof(restriper->mdr_lmv);
792 for (i = 0; i < le32_to_cpu(lmv->lmv_stripe_count); i++) {
793 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[i]);
794 stripe = mdt_object_find(env, mdt, fid);
796 GOTO(out, rc = PTR_ERR(stripe));
799 rc = __mdt_stripe_get(info, stripe, ma, XATTR_NAME_LMV);
800 /* LMV is checked without lock, don't cache it */
801 mo_invalidate(env, mdt_object_child(stripe));
802 mdt_object_put(env, stripe);
806 if (!(ma->ma_valid & MA_LMV))
807 GOTO(out, rc = -ENODATA);
809 /* check MIGRATION flag cleared on all stripes */
810 if (lmv_is_restriping(&ma->ma_lmv->lmv_md_v1))
811 GOTO(out, rc = -EINPROGRESS);
814 mdt_restripe_layout_update_prep(info, mdt_object_fid(master), lmv);
816 rc = mdt_dir_layout_update(info);
818 CERROR("update "DFID" layout failed: rc = %d\n",
819 PFID(mdt_object_fid(master)), rc);
824 LASSERT(!list_empty(&master->mot_restripe_linkage));
825 if (rc == -EINPROGRESS) {
826 restriper->mdr_update_time = ktime_get_real_seconds() + 5;
828 spin_lock(&restriper->mdr_lock);
829 master->mot_restriping = 0;
830 list_del_init(&master->mot_restripe_linkage);
831 spin_unlock(&restriper->mdr_lock);
833 mdt_object_put(env, master);
839 static int mdt_restriper_main(void *arg)
841 struct mdt_thread_info *info = arg;
842 struct mdt_device *mdt = info->mti_mdt;
843 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
847 while (({set_current_state(TASK_IDLE);
848 !kthread_should_stop(); })) {
849 if (!list_empty(&restriper->mdr_auto_splitting)) {
850 __set_current_state(TASK_RUNNING);
851 mdt_auto_split(info);
853 } else if (mdt_restripe_update_pending(info)) {
854 __set_current_state(TASK_RUNNING);
855 mdt_restripe_layout_update(info);
857 } else if (!list_empty(&restriper->mdr_migrating)) {
858 __set_current_state(TASK_RUNNING);
859 mdt_restripe_migrate(info);
865 __set_current_state(TASK_RUNNING);
870 int mdt_restriper_start(struct mdt_device *mdt)
872 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
873 struct task_struct *task;
874 struct mdt_thread_info *info;
880 spin_lock_init(&restriper->mdr_lock);
881 INIT_LIST_HEAD(&restriper->mdr_auto_splitting);
882 INIT_LIST_HEAD(&restriper->mdr_migrating);
883 INIT_LIST_HEAD(&restriper->mdr_updating);
884 restriper->mdr_dir_split_count = DIR_SPLIT_COUNT_DEFAULT;
885 restriper->mdr_dir_split_delta = DIR_SPLIT_DELTA_DEFAULT;
887 restriper->mdr_page = alloc_page(GFP_KERNEL);
888 if (!restriper->mdr_page)
891 rc = lu_env_init(&restriper->mdr_env, LCT_MD_THREAD);
895 rc = lu_context_init(&restriper->mdr_session, LCT_SERVER_SESSION);
899 lu_context_enter(&restriper->mdr_session);
900 restriper->mdr_env.le_ses = &restriper->mdr_session;
902 info = lu_context_key_get(&restriper->mdr_env.le_ctx, &mdt_thread_key);
903 info->mti_env = &restriper->mdr_env;
905 info->mti_pill = NULL;
906 info->mti_dlm_req = NULL;
908 uc = mdt_ucred(info);
909 uc->uc_valid = UCRED_OLD;
918 uc->uc_suppgids[0] = -1;
919 uc->uc_suppgids[1] = -1;
920 uc->uc_cap = cap_combine(CAP_FS_SET, CAP_NFSD_SET);
923 uc->uc_identity = NULL;
924 /* do not let rbac interfere with restriper internal processing */
925 uc->uc_rbac_file_perms = 1;
926 uc->uc_rbac_dne_ops = 1;
927 uc->uc_rbac_quota_ops = 1;
928 uc->uc_rbac_byfid_ops = 1;
929 uc->uc_rbac_chlg_ops = 1;
930 uc->uc_rbac_fscrypt_admin = 1;
932 task = kthread_create(mdt_restriper_main, info, "mdt_restriper_%03d",
933 mdt_seq_site(mdt)->ss_node_id);
936 CERROR("%s: Can't start directory restripe thread: rc %d\n",
937 mdt_obd_name(mdt), rc);
940 restriper->mdr_task = task;
941 wake_up_process(task);
946 lu_context_exit(restriper->mdr_env.le_ses);
947 lu_context_fini(restriper->mdr_env.le_ses);
949 lu_env_fini(&restriper->mdr_env);
951 __free_page(restriper->mdr_page);
956 void mdt_restriper_stop(struct mdt_device *mdt)
958 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
959 struct lu_env *env = &restriper->mdr_env;
960 struct mdt_object *mo, *next;
962 if (!restriper->mdr_task)
965 kthread_stop(restriper->mdr_task);
966 restriper->mdr_task = NULL;
968 list_for_each_entry_safe(mo, next, &restriper->mdr_auto_splitting,
969 mot_restripe_linkage) {
970 list_del_init(&mo->mot_restripe_linkage);
971 mdt_object_put(env, mo);
974 list_for_each_entry_safe(mo, next, &restriper->mdr_migrating,
975 mot_restripe_linkage) {
976 list_del_init(&mo->mot_restripe_linkage);
977 mdt_object_put(env, mo);
980 list_for_each_entry_safe(mo, next, &restriper->mdr_updating,
981 mot_restripe_linkage) {
982 list_del_init(&mo->mot_restripe_linkage);
983 mdt_object_put(env, mo);
986 __free_page(restriper->mdr_page);
988 lu_context_exit(env->le_ses);
989 lu_context_fini(env->le_ses);