1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/cmm/cmm_split.c
7 * Copyright (c) 2006 Cluster File Systems, Inc.
8 * Author: Alex thomas <alex@clusterfs.com>
9 * Wang Di <wangdi@clusterfs.com>
11 * This file is part of the Lustre file system, http://www.lustre.org
12 * Lustre is a trademark of Cluster File Systems, Inc.
14 * You may have signed or agreed to another license before downloading
15 * this software. If so, you are bound by the terms and conditions
16 * of that agreement, and the following does not apply to you. See the
17 * LICENSE file included with this distribution for more information.
19 * If you did not agree to a different license, then this copy of Lustre
20 * is open source software; you can redistribute it and/or modify it
21 * under the terms of version 2 of the GNU General Public License as
22 * published by the Free Software Foundation.
24 * In either case, Lustre is distributed in the hope that it will be
25 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * license text for more details.
31 # define EXPORT_SYMTAB
34 #define DEBUG_SUBSYSTEM S_MDS
36 #include <obd_class.h>
37 #include <lustre_fid.h>
38 #include <lustre_mds.h>
39 #include <lustre/lustre_idl.h>
40 #include "cmm_internal.h"
41 #include "mdc_internal.h"
43 static struct lu_buf *cmm_buf_get(const struct lu_env *env, void *area,
48 buf = &cmm_env_info(env)->cmi_buf;
54 int cmm_mdsnum_check(const struct lu_env *env, struct md_object *mp,
57 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mp));
58 struct md_attr *ma = &cmm_env_info(env)->cmi_ma;
62 if (cmm->cmm_tgt_count == 0)
65 /* Try to get the LMV EA size */
66 memset(ma, 0, sizeof(*ma));
68 rc = mo_attr_get(env, mp, ma);
72 if (ma->ma_valid & MA_LMV) {
75 OBD_ALLOC(ma->ma_lmv, ma->ma_lmv_size);
76 if (ma->ma_lmv == NULL)
81 rc = mo_attr_get(env, mp, ma);
83 /* Skip checking the slave dirs (mea_count is 0) */
84 if (rc == 0 && ma->ma_lmv->mea_count != 0) {
86 * Get stripe by name to check the name belongs to
87 * master dir, otherwise return the -ERESTART
89 stripe = mea_name2idx(ma->ma_lmv, name, strlen(name));
91 /* Master stripe is always 0 */
95 OBD_FREE(ma->ma_lmv, ma->ma_lmv_size);
100 int cmm_expect_splitting(const struct lu_env *env, struct md_object *mo,
101 struct md_attr *ma, int *split)
103 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
104 struct lu_fid root_fid;
109 * Check first most light things like tgt count and root fid. For some
110 * case this style should yeild better performance.
112 if (cmm->cmm_tgt_count == 0) {
113 *split = CMM_NO_SPLIT_EXPECTED;
117 rc = cmm_child_ops(cmm)->mdo_root_get(env, cmm->cmm_child,
122 if (lu_fid_eq(&root_fid, cmm2fid(md2cmm_obj(mo)))) {
123 *split = CMM_NOT_SPLITTABLE;
127 /* MA_INODE is needed to check inode size. */
128 ma->ma_need = MA_INODE | MA_LMV;
129 rc = mo_attr_get(env, mo, ma);
133 if (ma->ma_valid & MA_LMV) {
134 *split = CMM_NOT_SPLITTABLE;
138 if (ma->ma_attr.la_size < CMM_SPLIT_SIZE) {
139 *split = CMM_NO_SPLIT_EXPECTED;
143 *split = CMM_EXPECT_SPLIT;
147 #define cmm_md_size(stripes) \
148 (sizeof(struct lmv_stripe_md) + (stripes) * sizeof(struct lu_fid))
150 struct cmm_object *cmm_object_find(const struct lu_env *env,
151 struct cmm_device *d,
152 const struct lu_fid *f)
155 struct cmm_object *m;
158 o = lu_object_find(env, d->cmm_md_dev.md_lu_dev.ld_site, f);
160 m = (struct cmm_object *)o;
162 m = lu2cmm_obj(lu_object_locate(o->lo_header,
163 d->cmm_md_dev.md_lu_dev.ld_type));
167 static inline void cmm_object_put(const struct lu_env *env,
168 struct cmm_object *o)
170 lu_object_put(env, &o->cmo_obj.mo_lu);
173 static int cmm_object_create(const struct lu_env *env,
174 struct cmm_device *cmm,
177 struct lmv_stripe_md *lmv,
180 struct md_create_spec *spec;
181 struct cmm_object *obj;
185 obj = cmm_object_find(env, cmm, fid);
187 RETURN(PTR_ERR(obj));
191 spec->u.sp_ea.fid = fid;
192 spec->u.sp_ea.eadata = lmv;
193 spec->u.sp_ea.eadatalen = lmv_size;
194 spec->sp_cr_flags |= MDS_CREATE_SLAVE_OBJ;
195 rc = mo_object_create(env, md_object_next(&obj->cmo_obj),
199 cmm_object_put(env, obj);
203 static int cmm_fid_alloc(const struct lu_env *env,
204 struct cmm_device *cmm,
205 struct mdc_device *mc,
211 LASSERT(cmm != NULL);
213 LASSERT(fid != NULL);
215 down(&mc->mc_fid_sem);
217 /* Alloc new fid on @mc. */
218 rc = obd_fid_alloc(mc->mc_desc.cl_exp, fid, NULL);
220 /* Setup FLD for new sequenceif needed. */
221 rc = fld_client_create(cmm->cmm_fld, fid_seq(fid),
224 CERROR("Can't create fld entry, rc %d\n", rc);
231 static int cmm_slaves_create(const struct lu_env *env,
232 struct md_object *mo,
235 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
236 struct lmv_stripe_md *lmv = NULL, *slave_lmv = NULL;
237 struct lu_fid *lf = cmm2fid(md2cmm_obj(mo));
238 struct mdc_device *mc, *tmp;
239 int lmv_size, i = 1, rc = 0;
242 lmv_size = cmm_md_size(cmm->cmm_tgt_count + 1);
244 /* This lmv will free after finish splitting. */
245 OBD_ALLOC(lmv, lmv_size);
249 lmv->mea_master = cmm->cmm_local_num;
250 lmv->mea_magic = MEA_MAGIC_HASH_SEGMENT;
251 lmv->mea_count = cmm->cmm_tgt_count + 1;
253 /* Store master FID to local node idx number. */
254 lmv->mea_ids[0] = *lf;
256 OBD_ALLOC_PTR(slave_lmv);
258 GOTO(cleanup, rc = -ENOMEM);
260 slave_lmv->mea_master = cmm->cmm_local_num;
261 slave_lmv->mea_magic = MEA_MAGIC_HASH_SEGMENT;
262 slave_lmv->mea_count = 0;
264 list_for_each_entry_safe(mc, tmp, &cmm->cmm_targets, mc_linkage) {
265 /* Alloc fid for slave object. */
266 rc = cmm_fid_alloc(env, cmm, mc, &lmv->mea_ids[i]);
268 CERROR("Can't alloc fid for slave "LPU64", rc %d\n",
273 /* Create slave on remote MDT. */
274 rc = cmm_object_create(env, cmm, &lmv->mea_ids[i], ma,
275 slave_lmv, sizeof(*slave_lmv));
281 ma->ma_lmv_size = lmv_size;
286 OBD_FREE_PTR(slave_lmv);
288 OBD_FREE(lmv, lmv_size);
295 static int cmm_send_split_pages(const struct lu_env *env,
296 struct md_object *mo,
297 struct lu_rdpg *rdpg,
298 struct lu_fid *fid, int len)
300 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
301 struct cmm_object *obj;
305 obj = cmm_object_find(env, cmm, fid);
307 RETURN(PTR_ERR(obj));
309 rc = mdc_send_page(cmm, env, md_object_next(&obj->cmo_obj),
310 rdpg->rp_pages[0], len);
311 cmm_object_put(env, obj);
315 static int cmm_remove_dir_ent(const struct lu_env *env,
316 struct md_object *mo,
317 struct lu_dirent *ent)
319 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
320 struct cmm_object *obj;
325 if (!strncmp(ent->lde_name, ".", ent->lde_namelen) ||
326 !strncmp(ent->lde_name, "..", ent->lde_namelen))
329 obj = cmm_object_find(env, cmm, &ent->lde_fid);
331 RETURN(PTR_ERR(obj));
333 if (lu_object_exists(&obj->cmo_obj.mo_lu) > 0)
334 is_dir = S_ISDIR(lu_object_attr(&obj->cmo_obj.mo_lu));
336 /* XXX: is this correct? */
339 OBD_ALLOC(name, ent->lde_namelen + 1);
341 GOTO(cleanup, rc = -ENOMEM);
343 memcpy(name, ent->lde_name, ent->lde_namelen);
344 rc = mdo_name_remove(env, md_object_next(mo),
346 OBD_FREE(name, ent->lde_namelen + 1);
351 * This ent will be transferred to slave MDS and insert it there, so in
352 * the slave MDS, we should know whether this object is dir or not, so
353 * use the highest bit of the hash to indicate that (because we do not
354 * use highest bit of hash).
357 ent->lde_hash |= MAX_HASH_HIGHEST_BIT;
359 cmm_object_put(env, obj);
364 static int cmm_remove_entries(const struct lu_env *env,
365 struct md_object *mo, struct lu_rdpg *rdpg,
366 __u32 hash_end, __u32 *len)
368 struct lu_dirpage *dp;
369 struct lu_dirent *ent;
373 kmap(rdpg->rp_pages[0]);
374 dp = page_address(rdpg->rp_pages[0]);
375 for (ent = lu_dirent_start(dp); ent != NULL;
376 ent = lu_dirent_next(ent)) {
377 if (ent->lde_hash < hash_end) {
378 rc = cmm_remove_dir_ent(env, mo, ent);
380 CERROR("Can not del %s rc %d\n", ent->lde_name,
385 if (ent != lu_dirent_start(dp))
386 *len = (int)((__u32)ent - (__u32)dp);
392 *len = CFS_PAGE_SIZE;
395 kunmap(rdpg->rp_pages[0]);
399 static int cmm_split_entries(const struct lu_env *env,
400 struct md_object *mo, struct lu_rdpg *rdpg,
401 struct lu_fid *lf, __u32 end)
406 LASSERTF(rdpg->rp_npages == 1, "Now Only support split 1 page each time"
407 "npages %d\n", rdpg->rp_npages);
409 /* Read split page and send them to the slave master. */
411 struct lu_dirpage *ldp;
414 /* init page with '0' */
415 memset(kmap(rdpg->rp_pages[0]), 0, CFS_PAGE_SIZE);
416 kunmap(rdpg->rp_pages[0]);
418 rc = mo_readpage(env, md_object_next(mo), rdpg);
422 /* Remove the old entries */
423 rc = cmm_remove_entries(env, mo, rdpg, end, &len);
427 /* Send page to slave object */
429 rc = cmm_send_split_pages(env, mo, rdpg, lf, len);
434 kmap(rdpg->rp_pages[0]);
435 ldp = page_address(rdpg->rp_pages[0]);
436 if (ldp->ldp_hash_end >= end) {
439 rdpg->rp_hash = ldp->ldp_hash_end;
440 kunmap(rdpg->rp_pages[0]);
446 #define SPLIT_PAGE_COUNT 1
448 static int cmm_scan_and_split(const struct lu_env *env,
449 struct md_object *mo,
452 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
453 struct lu_rdpg *rdpg = NULL;
461 rdpg->rp_npages = SPLIT_PAGE_COUNT;
462 rdpg->rp_count = CFS_PAGE_SIZE * rdpg->rp_npages;
464 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
465 if (rdpg->rp_pages == NULL)
466 GOTO(free_rdpg, rc = -ENOMEM);
468 for (i = 0; i < rdpg->rp_npages; i++) {
469 rdpg->rp_pages[i] = alloc_pages(GFP_KERNEL, 0);
470 if (rdpg->rp_pages[i] == NULL)
471 GOTO(cleanup, rc = -ENOMEM);
474 hash_segement = MAX_HASH_SIZE / (cmm->cmm_tgt_count + 1);
475 for (i = 1; i < cmm->cmm_tgt_count + 1; i++) {
479 lf = &ma->ma_lmv->mea_ids[i];
481 rdpg->rp_hash = i * hash_segement;
482 hash_end = rdpg->rp_hash + hash_segement;
483 rc = cmm_split_entries(env, mo, rdpg, lf, hash_end);
489 for (i = 0; i < rdpg->rp_npages; i++)
490 if (rdpg->rp_pages[i] != NULL)
491 __free_pages(rdpg->rp_pages[i], 0);
493 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages *
494 sizeof rdpg->rp_pages[0]);
502 int cmm_try_to_split(const struct lu_env *env, struct md_object *mo)
504 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
505 struct md_attr *ma = &cmm_env_info(env)->cmi_ma;
510 LASSERT(S_ISDIR(lu_object_attr(&mo->mo_lu)));
511 memset(ma, 0, sizeof(*ma));
513 /* Step1: Checking whether the dir needs to be split. */
514 rc = cmm_expect_splitting(env, mo, ma, &split);
518 if (split != CMM_EXPECT_SPLIT)
519 GOTO(cleanup, rc = 0);
522 * Disable trans for splitting, since there will be so many trans in
523 * this one ops, confilct with current recovery design.
525 rc = cmm_upcall(env, &cmm->cmm_md_dev, MD_NO_TRANS);
527 GOTO(cleanup, rc = 0);
529 /* Step2: Create slave objects (on slave MDTs) */
530 rc = cmm_slaves_create(env, mo, ma);
534 /* Step3: Scan and split the object. */
535 rc = cmm_scan_and_split(env, mo, ma);
539 buf = cmm_buf_get(env, ma->ma_lmv, ma->ma_lmv_size);
541 /* Step4: Set mea to the master object. */
542 rc = mo_xattr_set(env, md_object_next(mo), buf,
545 CWARN("Dir "DFID" has been split\n",
546 PFID(lu_object_fid(&mo->mo_lu)));
551 if (ma->ma_lmv_size && ma->ma_lmv)
552 OBD_FREE(ma->ma_lmv, ma->ma_lmv_size);