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_merge_offset = lum->lum_stripe_count;
248 lmv->lmv_merge_hash = lum->lum_hash_type;
249 lmv->lmv_layout_version = cpu_to_le32(++version);
252 buf->lb_len = sizeof(*lmv);
253 rc = mo_xattr_set(env, mdt_object_child(child), buf,
254 XATTR_NAME_LMV, LU_XATTR_REPLACE);
258 *tfid = *mdt_object_fid(child);
259 ma->ma_need = MA_INODE;
261 rc = mdt_attr_get_complex(info, child, ma);
267 static int mdt_auto_split(struct mdt_thread_info *info)
269 const struct lu_env *env = info->mti_env;
270 struct mdt_device *mdt = info->mti_mdt;
271 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
272 struct md_attr *ma = &info->mti_attr;
273 struct md_op_spec *spec = &info->mti_spec;
274 struct lu_name *lname = &info->mti_name;
275 struct lu_fid *fid = &info->mti_tmp_fid2;
276 struct mdt_object *parent = NULL;
277 struct mdt_object *child = NULL;
278 struct mdt_object *stripe = NULL;
279 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
280 struct mdt_lock_handle *lhp;
281 struct mdt_lock_handle *lhc;
282 u32 lmv_stripe_count = 0;
283 u32 lum_stripe_count = 0;
288 if (!atomic_read(&mdt->mdt_mds_mds_conns))
291 spin_lock(&restriper->mdr_lock);
292 if (!list_empty(&restriper->mdr_auto_splitting)) {
293 child = list_entry(restriper->mdr_auto_splitting.next,
294 typeof(*child), mot_restripe_linkage);
295 list_del_init(&child->mot_restripe_linkage);
297 spin_unlock(&restriper->mdr_lock);
302 LASSERT(child->mot_restriping);
304 rc = mdt_stripe_get(info, child, ma, XATTR_NAME_LMV);
308 if (ma->ma_valid & MA_LMV) {
309 /* stripe dirent exceeds threshold, find its master object */
310 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
312 /* auto-split won't be done on striped directory master object
313 * directly, because it's triggered when dirent count exceeds
314 * threshold, however dirent count of master object is its
317 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_STRIPE)
318 GOTO(out, rc = -EINVAL);
320 /* race with migrate? */
321 if (lmv_hash_is_migrating(cpu_to_le32(lmv->lmv_hash_type)))
322 GOTO(out, rc = -EBUSY);
324 lmv_stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
326 /* save stripe to clear 'restriping' flag in the end to avoid
327 * trigger auto-split multiple times.
332 /* get master object FID from linkea */
333 rc = mdt_attr_get_pfid(info, stripe, &ma->ma_pfid);
337 child = mdt_object_find(env, mdt, &ma->ma_pfid);
339 GOTO(out, rc = PTR_ERR(child));
341 spin_lock(&restriper->mdr_lock);
342 if (child->mot_restriping) {
344 spin_unlock(&restriper->mdr_lock);
345 GOTO(out, rc = -EBUSY);
347 child->mot_restriping = 1;
348 spin_unlock(&restriper->mdr_lock);
350 /* skip if master object is remote, let the first stripe
351 * to start splitting because dir split needs to be done
352 * on where master object is.
354 if (mdt_object_remote(child))
355 GOTO(restriping_clear, rc = -EREMOTE);
358 /* striped directory split adds mdr_auto_split_delta stripes */
359 lum_stripe_count = min_t(unsigned int,
361 mdt->mdt_restriper.mdr_dir_split_delta,
362 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
363 if (lmv_stripe_count >= lum_stripe_count)
364 GOTO(restriping_clear, rc = -EALREADY);
366 /* get dir name and parent FID */
367 rc = mdt_attr_get_pfid_name(info, child, fid, lname);
369 GOTO(restriping_clear, rc);
371 /* copy name out because mti_linkea will be used later, and name should
374 memcpy(info->mti_filename, lname->ln_name, lname->ln_namelen);
375 info->mti_filename[lname->ln_namelen] = '\0';
376 lname->ln_name = info->mti_filename;
377 CDEBUG(D_INFO, "split "DFID"/"DNAME" to count %u (MDT count %d)\n",
378 PFID(fid), PNAME(lname), lum_stripe_count,
379 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
381 parent = mdt_object_find(env, mdt, fid);
383 GOTO(restriping_clear, rc = PTR_ERR(parent));
385 rc = mdt_fid_alloc(env, mdt, fid, child, NULL);
387 GOTO(restriping_clear, rc);
389 lhp = &info->mti_lh[MDT_LH_PARENT];
390 mdt_lock_pdo_init(lhp, LCK_PW, lname);
391 rc = mdt_reint_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
394 GOTO(restriping_clear, rc);
396 lhc = &info->mti_lh[MDT_LH_CHILD];
397 mdt_lock_reg_init(lhc, LCK_EX);
398 if (mdt_object_remote(parent)) {
399 /* enqueue object remote LOOKUP lock */
400 rc = mdt_remote_object_lock(info, parent, mdt_object_fid(child),
403 MDS_INODELOCK_LOOKUP, false);
405 GOTO(unlock_parent, rc);
408 rc = mdt_reint_striped_lock(info, child, lhc, MDS_INODELOCK_FULL, einfo,
411 GOTO(unlock_child, rc);
413 mdt_auto_split_prep(info, spec, ma, lum_stripe_count);
415 rc = mdt_restripe_internal(info, parent, child, lname, fid, spec, ma);
419 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
421 mdt_object_unlock(info, parent, lhp, rc);
423 child->mot_restriping = 0;
424 LASSERT(list_empty(&child->mot_restripe_linkage));
426 /* -EALREADY: dir is split already.
427 * -EBUSY: dir is opened, or is splitting by others.
428 * -EREMOTE: dir is remote.
430 if (rc && rc != -EALREADY && rc != -EBUSY && rc != -EREMOTE)
431 CERROR("%s: split "DFID"/"DNAME" to count %u failed: rc = %d\n",
432 mdt_obd_name(mdt), PFID(mdt_object_fid(child)),
433 PNAME(lname), lum_stripe_count, rc);
435 if (!IS_ERR_OR_NULL(child))
436 mdt_object_put(env, child);
439 LASSERT(stripe->mot_restriping);
440 LASSERT(list_empty(&stripe->mot_restripe_linkage));
441 stripe->mot_restriping = 0;
442 /* lock may not be taken, don't cache stripe LMV */
443 mo_invalidate(env, mdt_object_child(stripe));
444 mdt_object_put(env, stripe);
447 if (!IS_ERR_OR_NULL(parent))
448 mdt_object_put(env, parent);
453 /* sub-files under one stripe are migrated, clear MIGRATION flag in its LMV */
454 static int mdt_restripe_migrate_finish(struct mdt_thread_info *info,
455 struct mdt_object *stripe,
456 struct lmv_mds_md_v1 *lmv)
458 struct mdt_device *mdt = info->mti_mdt;
460 struct mdt_lock_handle *lh;
465 LASSERT(le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE);
466 LASSERT(lmv_is_restriping(lmv));
468 lmv->lmv_hash_type &= ~cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
470 buf.lb_len = sizeof(*lmv);
472 lh = &info->mti_lh[MDT_LH_PARENT];
473 mdt_lock_reg_init(lh, LCK_EX);
474 rc = mdt_reint_object_lock(info, stripe, lh, MDS_INODELOCK_XATTR,
477 rc = mo_xattr_set(info->mti_env, mdt_object_child(stripe), &buf,
478 XATTR_NAME_LMV, LU_XATTR_REPLACE);
479 mdt_object_unlock(info, stripe, lh, rc);
481 CERROR("%s: update "DFID" LMV failed: rc = %d\n",
482 mdt_obd_name(mdt), PFID(mdt_object_fid(stripe)), rc);
484 LASSERT(!list_empty(&stripe->mot_restripe_linkage));
485 LASSERT(stripe->mot_restriping);
487 spin_lock(&mdt->mdt_lock);
488 stripe->mot_restriping = 0;
489 list_del_init(&stripe->mot_restripe_linkage);
490 spin_unlock(&mdt->mdt_lock);
492 mdt_object_put(info->mti_env, stripe);
497 static void mdt_restripe_migrate_prep(struct mdt_thread_info *info,
498 const struct lu_fid *fid1,
499 const struct lu_fid *fid2,
500 const struct lu_name *lname,
502 const struct lmv_mds_md_v1 *lmv)
504 struct lu_attr *attr = &info->mti_attr.ma_attr;
505 struct mdt_reint_record *rr = &info->mti_rr;
506 struct md_op_spec *spec = &info->mti_spec;
507 struct lmv_user_md_v1 *lum;
509 attr->la_ctime = attr->la_mtime = ktime_get_real_seconds();
510 attr->la_valid = LA_CTIME | LA_MTIME;
514 rr->rr_name = *lname;
516 lum = &info->mti_mdt->mdt_restriper.mdr_lmv.lmv_user_md;
517 lum->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
518 lum->lum_stripe_offset = cpu_to_le32(LMV_OFFSET_DEFAULT);
519 if (lmv_is_splitting(lmv)) {
520 lum->lum_stripe_count = lmv->lmv_stripe_count;
522 lmv->lmv_hash_type & le32_to_cpu(LMV_HASH_TYPE_MASK);
523 } else if (lmv_is_merging(lmv)) {
524 lum->lum_stripe_count = lmv->lmv_merge_offset;
525 lum->lum_hash_type = lmv->lmv_merge_hash;
528 spec->u.sp_ea.eadatalen = sizeof(*lum);
529 spec->u.sp_ea.eadata = lum;
530 spec->sp_cr_flags = MDS_OPEN_HAS_EA;
532 spec->sp_migrate_close = 0;
533 /* if 'nsonly' is set, don't migrate inode */
535 spec->sp_migrate_nsonly = 1;
537 spec->sp_migrate_nsonly =
538 info->mti_mdt->mdt_dir_restripe_nsonly;
541 /* migrate sub-file from @mdr_restripe_offset */
542 static int mdt_restripe_migrate(struct mdt_thread_info *info)
544 const struct lu_env *env = info->mti_env;
545 struct mdt_device *mdt = info->mti_mdt;
546 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
547 struct mdt_object *stripe = NULL;
548 struct mdt_object *master = NULL;
549 struct md_attr *ma = &info->mti_attr;
550 struct lmv_mds_md_v1 *lmv;
551 struct lu_name *lname = &info->mti_name;
552 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
555 struct lu_dirpage *dp;
556 struct lu_dirent *ent;
557 const char *name = NULL;
566 if (list_empty(&restriper->mdr_migrating))
569 stripe = list_entry(restriper->mdr_migrating.next, typeof(*stripe),
570 mot_restripe_linkage);
572 /* get master object FID and stripe name */
573 rc = mdt_attr_get_pfid_name(info, stripe, &fid1, lname);
577 snprintf(info->mti_filename, sizeof(info->mti_filename), DFID,
578 PFID(mdt_object_fid(stripe)));
579 len = strlen(info->mti_filename) + 1;
580 if (len >= lname->ln_namelen)
581 GOTO(out, rc = -EBADF);
583 while (len < lname->ln_namelen) {
584 if (!isdigit(lname->ln_name[len]))
585 GOTO(out, rc = -EBADF);
587 idx = idx * 10 + lname->ln_name[len++] - '0';
590 /* check whether stripe is newly created in split */
591 rc = mdt_stripe_get(info, stripe, ma, XATTR_NAME_LMV);
595 if (!(ma->ma_valid & MA_LMV))
596 GOTO(out, rc = -ENODATA);
598 lmv = &ma->ma_lmv->lmv_md_v1;
599 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_STRIPE)
600 GOTO(out, rc = -EBADF);
602 if (!lmv_is_restriping(lmv))
603 GOTO(out, rc = -EINVAL);
605 if ((lmv_is_splitting(lmv) &&
606 idx >= le32_to_cpu(lmv->lmv_split_offset)) ||
607 (lmv_is_merging(lmv) &&
608 le32_to_cpu(lmv->lmv_hash_type) == LMV_HASH_TYPE_CRUSH &&
609 idx < le32_to_cpu(lmv->lmv_merge_offset))) {
610 /* new stripes doesn't need to migrate sub files in dir
611 * split, neither for target stripes in dir merge if hash type
614 rc = mdt_restripe_migrate_finish(info, stripe, lmv);
618 /* get sub file name @mot_restripe_offset.
619 * TODO: read one dirent instead of whole page.
621 rdpg->rp_hash = stripe->mot_restripe_offset;
622 rdpg->rp_count = PAGE_SIZE;
624 rdpg->rp_attrs = LUDA_64BITHASH | LUDA_FID | LUDA_TYPE;
625 rdpg->rp_pages = &restriper->mdr_page;
626 rc = mo_readpage(env, mdt_object_child(stripe), rdpg);
630 dp = page_address(restriper->mdr_page);
631 for (ent = lu_dirent_start(dp); ent; ent = lu_dirent_next(ent)) {
632 LASSERT(le64_to_cpu(ent->lde_hash) >= rdpg->rp_hash);
634 if (unlikely(!(le32_to_cpu(ent->lde_attrs) & LUDA_TYPE)))
635 GOTO(out, rc = -EINVAL);
637 namelen = le16_to_cpu(ent->lde_namelen);
641 if (name_is_dot_or_dotdot(ent->lde_name, namelen))
644 name = ent->lde_name;
645 type = lu_dirent_type_get(ent);
650 if (le64_to_cpu(dp->ldp_hash_end) == MDS_DIR_END_OFF) {
651 rc = mdt_restripe_migrate_finish(info, stripe, lmv);
655 GOTO(out, rc = -EBADF);
658 /* copy name out because it should end with '\0' */
659 memcpy(info->mti_filename, name, namelen);
660 info->mti_filename[namelen] = '\0';
661 lname->ln_name = info->mti_filename;
662 lname->ln_namelen = namelen;
664 CDEBUG(D_INFO, "migrate "DFID"/"DNAME" type %ho\n",
665 PFID(&fid1), PNAME(lname), type);
667 master = mdt_object_find(env, mdt, &fid1);
669 GOTO(out, rc = PTR_ERR(master));
671 rc = mdt_fid_alloc(env, mdt, &fid2, master, lname);
672 mdt_object_put(env, master);
676 mdt_restripe_migrate_prep(info, &fid1, &fid2, lname, type, lmv);
678 rc = mdt_reint_migrate(info, NULL);
679 /* mti_big_buf is allocated in XATTR migration */
680 if (unlikely(info->mti_big_buf.lb_buf))
681 lu_buf_free(&info->mti_big_buf);
689 ent = lu_dirent_next(ent);
693 namelen = le16_to_cpu(ent->lde_namelen);
694 } while (namelen == 0); /* Skip dummy record */
697 stripe->mot_restripe_offset = le64_to_cpu(ent->lde_hash);
699 stripe->mot_restripe_offset = le64_to_cpu(dp->ldp_hash_end);
704 /* -EBUSY: file is opened by others */
706 CERROR("%s: migrate "DFID"/"DNAME" failed: rc = %d\n",
707 mdt_obd_name(mdt), PFID(&fid1), PNAME(lname),
710 spin_lock(&mdt->mdt_lock);
711 stripe->mot_restriping = 0;
712 list_del_init(&stripe->mot_restripe_linkage);
713 spin_unlock(&mdt->mdt_lock);
715 mdt_object_put(env, stripe);
721 static inline bool mdt_restripe_update_pending(struct mdt_thread_info *info)
723 struct mdt_device *mdt = info->mti_mdt;
725 if (list_empty(&mdt->mdt_restriper.mdr_updating))
728 return mdt->mdt_restriper.mdr_update_time < ktime_get_real_seconds();
731 static void mdt_restripe_layout_update_prep(struct mdt_thread_info *info,
732 const struct lu_fid *fid,
733 const struct lmv_mds_md_v1 *lmv)
735 struct lu_attr *attr = &info->mti_attr.ma_attr;
736 struct mdt_reint_record *rr = &info->mti_rr;
737 struct lmv_user_md_v1 *lum;
739 attr->la_ctime = attr->la_mtime = ktime_get_real_seconds();
740 attr->la_valid = LA_CTIME | LA_MTIME;
742 strncpy(info->mti_filename, XATTR_NAME_LMV,
743 sizeof(info->mti_filename));
745 lum = &info->mti_mdt->mdt_restriper.mdr_lmv.lmv_user_md;
746 lum->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
747 lum->lum_stripe_offset = cpu_to_le32(LMV_OFFSET_DEFAULT);
748 if (lmv_is_splitting(lmv)) {
749 lum->lum_stripe_count = lmv->lmv_stripe_count;
751 lmv->lmv_hash_type & le32_to_cpu(LMV_HASH_TYPE_MASK);
752 } else if (lmv_is_merging(lmv)) {
753 lum->lum_stripe_count = lmv->lmv_merge_offset;
754 lum->lum_hash_type = lmv->lmv_merge_hash;
757 rr->rr_opcode = REINT_SETXATTR;
759 rr->rr_name.ln_name = info->mti_filename;
760 rr->rr_name.ln_namelen = strlen(info->mti_filename);
762 rr->rr_eadatalen = sizeof(*lum);
765 static int mdt_restripe_layout_update(struct mdt_thread_info *info)
767 const struct lu_env *env = info->mti_env;
768 struct mdt_device *mdt = info->mti_mdt;
769 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
770 struct md_attr *ma = &info->mti_attr;
771 struct lu_fid *fid = &info->mti_tmp_fid1;
772 struct mdt_object *master;
773 struct mdt_object *stripe;
774 struct lmv_mds_md_v1 *lmv;
780 if (list_empty(&restriper->mdr_updating))
783 master = list_entry(restriper->mdr_updating.next, typeof(*master),
784 mot_restripe_linkage);
786 rc = mdt_stripe_get(info, master, ma, XATTR_NAME_LMV);
790 if (!(ma->ma_valid & MA_LMV))
791 GOTO(out, rc = -ENODATA);
793 lmv = &ma->ma_lmv->lmv_md_v1;
794 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
795 GOTO(out, rc = -EBADF);
797 if (!lmv_is_restriping(lmv))
798 GOTO(out, rc = -EINVAL);
800 /* use different buffer to store stripe LMV */
801 ma->ma_lmv = &restriper->mdr_lmv;
802 ma->ma_lmv_size = sizeof(restriper->mdr_lmv);
803 for (i = 0; i < le32_to_cpu(lmv->lmv_stripe_count); i++) {
804 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[i]);
805 stripe = mdt_object_find(env, mdt, fid);
807 GOTO(out, rc = PTR_ERR(stripe));
810 rc = __mdt_stripe_get(info, stripe, ma, XATTR_NAME_LMV);
811 /* LMV is checked without lock, don't cache it */
812 mo_invalidate(env, mdt_object_child(stripe));
813 mdt_object_put(env, stripe);
817 if (!(ma->ma_valid & MA_LMV))
818 GOTO(out, rc = -ENODATA);
820 /* check MIGRATION flag cleared on all stripes */
821 if (lmv_is_restriping(&ma->ma_lmv->lmv_md_v1))
822 GOTO(out, rc = -EINPROGRESS);
825 mdt_restripe_layout_update_prep(info, mdt_object_fid(master), lmv);
827 rc = mdt_dir_layout_update(info);
829 CERROR("update "DFID" layout failed: rc = %d\n",
830 PFID(mdt_object_fid(master)), rc);
835 LASSERT(!list_empty(&master->mot_restripe_linkage));
836 if (rc == -EINPROGRESS) {
837 restriper->mdr_update_time = ktime_get_real_seconds() + 5;
839 spin_lock(&restriper->mdr_lock);
840 master->mot_restriping = 0;
841 list_del_init(&master->mot_restripe_linkage);
842 spin_unlock(&restriper->mdr_lock);
844 mdt_object_put(env, master);
850 static int mdt_restriper_main(void *arg)
852 struct mdt_thread_info *info = arg;
853 struct mdt_device *mdt = info->mti_mdt;
854 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
858 while (({set_current_state(TASK_IDLE);
859 !kthread_should_stop(); })) {
860 if (!list_empty(&restriper->mdr_auto_splitting)) {
861 __set_current_state(TASK_RUNNING);
862 mdt_auto_split(info);
864 } else if (mdt_restripe_update_pending(info)) {
865 __set_current_state(TASK_RUNNING);
866 mdt_restripe_layout_update(info);
868 } else if (!list_empty(&restriper->mdr_migrating)) {
869 __set_current_state(TASK_RUNNING);
870 mdt_restripe_migrate(info);
876 __set_current_state(TASK_RUNNING);
881 int mdt_restriper_start(struct mdt_device *mdt)
883 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
884 kernel_cap_t kcap = cap_combine(CAP_FS_SET, CAP_NFSD_SET);
885 struct task_struct *task;
886 struct mdt_thread_info *info;
892 spin_lock_init(&restriper->mdr_lock);
893 INIT_LIST_HEAD(&restriper->mdr_auto_splitting);
894 INIT_LIST_HEAD(&restriper->mdr_migrating);
895 INIT_LIST_HEAD(&restriper->mdr_updating);
896 restriper->mdr_dir_split_count = DIR_SPLIT_COUNT_DEFAULT;
897 restriper->mdr_dir_split_delta = DIR_SPLIT_DELTA_DEFAULT;
899 restriper->mdr_page = alloc_page(GFP_KERNEL);
900 if (!restriper->mdr_page)
903 rc = lu_env_init(&restriper->mdr_env, LCT_MD_THREAD);
907 rc = lu_context_init(&restriper->mdr_session, LCT_SERVER_SESSION);
911 lu_context_enter(&restriper->mdr_session);
912 restriper->mdr_env.le_ses = &restriper->mdr_session;
914 info = lu_context_key_get(&restriper->mdr_env.le_ctx, &mdt_thread_key);
915 info->mti_env = &restriper->mdr_env;
917 info->mti_pill = NULL;
918 info->mti_dlm_req = NULL;
920 uc = mdt_ucred(info);
921 uc->uc_valid = UCRED_OLD;
930 uc->uc_suppgids[0] = -1;
931 uc->uc_suppgids[1] = -1;
932 uc->uc_cap = kcap.cap[0];
935 uc->uc_identity = NULL;
937 task = kthread_create(mdt_restriper_main, info, "mdt_restriper_%03d",
938 mdt_seq_site(mdt)->ss_node_id);
941 CERROR("%s: Can't start directory restripe thread: rc %d\n",
942 mdt_obd_name(mdt), rc);
945 restriper->mdr_task = task;
946 wake_up_process(task);
951 lu_context_exit(restriper->mdr_env.le_ses);
952 lu_context_fini(restriper->mdr_env.le_ses);
954 lu_env_fini(&restriper->mdr_env);
956 __free_page(restriper->mdr_page);
961 void mdt_restriper_stop(struct mdt_device *mdt)
963 struct mdt_dir_restriper *restriper = &mdt->mdt_restriper;
964 struct lu_env *env = &restriper->mdr_env;
965 struct mdt_object *mo, *next;
967 if (!restriper->mdr_task)
970 kthread_stop(restriper->mdr_task);
971 restriper->mdr_task = NULL;
973 list_for_each_entry_safe(mo, next, &restriper->mdr_auto_splitting,
974 mot_restripe_linkage) {
975 list_del_init(&mo->mot_restripe_linkage);
976 mdt_object_put(env, mo);
979 list_for_each_entry_safe(mo, next, &restriper->mdr_migrating,
980 mot_restripe_linkage) {
981 list_del_init(&mo->mot_restripe_linkage);
982 mdt_object_put(env, mo);
985 list_for_each_entry_safe(mo, next, &restriper->mdr_updating,
986 mot_restripe_linkage) {
987 list_del_init(&mo->mot_restripe_linkage);
988 mdt_object_put(env, mo);
991 __free_page(restriper->mdr_page);
993 lu_context_exit(env->le_ses);
994 lu_context_fini(env->le_ses);