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 #define CMM_NO_SPLIT_EXPECTED 0
44 #define CMM_EXPECT_SPLIT 1
45 #define CMM_NO_SPLITTABLE 2
51 static int cmm_expect_splitting(const struct lu_env *env,
55 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
56 struct lu_fid *fid = NULL;
57 int rc = CMM_EXPECT_SPLIT;
60 if (cmm->cmm_tgt_count == 0)
61 GOTO(cleanup, rc = CMM_NO_SPLIT_EXPECTED);
63 if (ma->ma_attr.la_size < SPLIT_SIZE)
64 GOTO(cleanup, rc = CMM_NO_SPLIT_EXPECTED);
67 GOTO(cleanup, rc = CMM_NO_SPLIT_EXPECTED);
69 rc = cmm_child_ops(cmm)->mdo_root_get(env, cmm->cmm_child, fid);
73 rc = CMM_EXPECT_SPLIT;
75 if (lu_fid_eq(fid, cmm2fid(md2cmm_obj(mo))))
76 GOTO(cleanup, rc = CMM_NO_SPLIT_EXPECTED);
85 #define cmm_md_size(stripes) \
86 (sizeof(struct lmv_stripe_md) + (stripes) * sizeof(struct lu_fid))
88 struct cmm_object *cmm_object_find(const struct lu_env *env,
90 const struct lu_fid *f)
96 o = lu_object_find(env, d->cmm_md_dev.md_lu_dev.ld_site, f);
98 m = (struct cmm_object *)o;
100 m = lu2cmm_obj(lu_object_locate(o->lo_header,
101 d->cmm_md_dev.md_lu_dev.ld_type));
105 static inline void cmm_object_put(const struct lu_env *env,
106 struct cmm_object *o)
108 lu_object_put(env, &o->cmo_obj.mo_lu);
111 static int cmm_object_create(const struct lu_env *env,
112 struct cmm_device *cmm,
115 struct lmv_stripe_md *lmv,
118 struct md_create_spec *spec;
119 struct cmm_object *obj;
123 obj = cmm_object_find(env, cmm, fid);
125 RETURN(PTR_ERR(obj));
129 spec->u.sp_ea.fid = fid;
130 spec->u.sp_ea.eadata = lmv;
131 spec->u.sp_ea.eadatalen = lmv_size;
132 spec->sp_cr_flags |= MDS_CREATE_SLAVE_OBJ;
133 rc = mo_object_create(env, md_object_next(&obj->cmo_obj),
137 cmm_object_put(env, obj);
141 static int cmm_fid_alloc(const struct lu_env *env,
142 struct cmm_device *cmm,
143 struct mdc_device *mc,
149 LASSERT(cmm != NULL);
151 LASSERT(fid != NULL);
153 down(&mc->mc_fid_sem);
155 /* Alloc new fid on @mc. */
156 rc = obd_fid_alloc(mc->mc_desc.cl_exp, fid, NULL);
158 /* Setup FLD for new sequenceif needed. */
159 rc = fld_client_create(cmm->cmm_fld, fid_seq(fid),
162 CERROR("Can't create fld entry, rc %d\n", rc);
169 static int cmm_slaves_create(const struct lu_env *env,
170 struct md_object *mo,
173 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
174 struct lmv_stripe_md *lmv = NULL, *slave_lmv = NULL;
175 struct lu_fid *lf = cmm2fid(md2cmm_obj(mo));
176 struct mdc_device *mc, *tmp;
177 int lmv_size, i = 1, rc = 0;
180 lmv_size = cmm_md_size(cmm->cmm_tgt_count + 1);
182 /* This lmv will free after finish splitting. */
183 OBD_ALLOC(lmv, lmv_size);
187 lmv->mea_master = cmm->cmm_local_num;
188 lmv->mea_magic = MEA_MAGIC_HASH_SEGMENT;
189 lmv->mea_count = cmm->cmm_tgt_count + 1;
191 /* Store master FID to local node idx number. */
192 lmv->mea_ids[0] = *lf;
194 OBD_ALLOC_PTR(slave_lmv);
196 GOTO(cleanup, rc = -ENOMEM);
198 slave_lmv->mea_master = cmm->cmm_local_num;
199 slave_lmv->mea_magic = MEA_MAGIC_HASH_SEGMENT;
200 slave_lmv->mea_count = 0;
202 list_for_each_entry_safe(mc, tmp, &cmm->cmm_targets, mc_linkage) {
203 /* Alloc fid for slave object. */
204 rc = cmm_fid_alloc(env, cmm, mc, &lmv->mea_ids[i]);
206 CERROR("Can't alloc fid for slave "LPU64", rc %d\n",
211 /* Create slave on remote MDT. */
212 rc = cmm_object_create(env, cmm, &lmv->mea_ids[i], ma,
213 slave_lmv, sizeof(*slave_lmv));
219 ma->ma_lmv_size = lmv_size;
224 OBD_FREE_PTR(slave_lmv);
226 OBD_FREE(lmv, lmv_size);
233 static int cmm_send_split_pages(const struct lu_env *env,
234 struct md_object *mo,
235 struct lu_rdpg *rdpg,
236 struct lu_fid *fid, int len)
238 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
239 struct cmm_object *obj;
243 obj = cmm_object_find(env, cmm, fid);
245 RETURN(PTR_ERR(obj));
247 rc = mdc_send_page(cmm, env, md_object_next(&obj->cmo_obj),
248 rdpg->rp_pages[0], len);
249 cmm_object_put(env, obj);
253 static int cmm_remove_dir_ent(const struct lu_env *env,
254 struct md_object *mo,
255 struct lu_dirent *ent)
257 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
258 struct cmm_object *obj;
263 if (!strncmp(ent->lde_name, ".", ent->lde_namelen) ||
264 !strncmp(ent->lde_name, "..", ent->lde_namelen))
267 obj = cmm_object_find(env, cmm, &ent->lde_fid);
269 RETURN(PTR_ERR(obj));
271 if (lu_object_exists(&obj->cmo_obj.mo_lu) > 0)
272 is_dir = S_ISDIR(lu_object_attr(&obj->cmo_obj.mo_lu));
274 /* XXX: is this correct? */
277 OBD_ALLOC(name, ent->lde_namelen + 1);
279 GOTO(cleanup, rc = -ENOMEM);
281 memcpy(name, ent->lde_name, ent->lde_namelen);
282 rc = mdo_name_remove(env, md_object_next(mo),
284 OBD_FREE(name, ent->lde_namelen + 1);
289 * This ent will be transferred to slave MDS and insert it there, so in
290 * the slave MDS, we should know whether this object is dir or not, so
291 * use the highest bit of the hash to indicate that (because we do not
292 * use highest bit of hash).
295 ent->lde_hash |= MAX_HASH_HIGHEST_BIT;
297 cmm_object_put(env, obj);
302 static int cmm_remove_entries(const struct lu_env *env,
303 struct md_object *mo, struct lu_rdpg *rdpg,
304 __u32 hash_end, __u32 *len)
306 struct lu_dirpage *dp;
307 struct lu_dirent *ent;
311 kmap(rdpg->rp_pages[0]);
312 dp = page_address(rdpg->rp_pages[0]);
313 for (ent = lu_dirent_start(dp); ent != NULL;
314 ent = lu_dirent_next(ent)) {
315 if (ent->lde_hash < hash_end) {
316 rc = cmm_remove_dir_ent(env, mo, ent);
318 CERROR("Can not del %s rc %d\n", ent->lde_name,
323 if (ent != lu_dirent_start(dp))
324 *len = (int)((__u32)ent - (__u32)dp);
330 *len = CFS_PAGE_SIZE;
333 kunmap(rdpg->rp_pages[0]);
337 static int cmm_split_entries(const struct lu_env *env,
338 struct md_object *mo, struct lu_rdpg *rdpg,
339 struct lu_fid *lf, __u32 end)
344 LASSERTF(rdpg->rp_npages == 1, "Now Only support split 1 page each time"
345 "npages %d\n", rdpg->rp_npages);
347 /* Read split page and send them to the slave master. */
349 struct lu_dirpage *ldp;
352 /* init page with '0' */
353 memset(kmap(rdpg->rp_pages[0]), 0, CFS_PAGE_SIZE);
354 kunmap(rdpg->rp_pages[0]);
356 rc = mo_readpage(env, md_object_next(mo), rdpg);
360 /* Remove the old entries */
361 rc = cmm_remove_entries(env, mo, rdpg, end, &len);
365 /* Send page to slave object */
367 rc = cmm_send_split_pages(env, mo, rdpg, lf, len);
372 kmap(rdpg->rp_pages[0]);
373 ldp = page_address(rdpg->rp_pages[0]);
374 if (ldp->ldp_hash_end >= end) {
377 rdpg->rp_hash = ldp->ldp_hash_end;
378 kunmap(rdpg->rp_pages[0]);
384 #define SPLIT_PAGE_COUNT 1
386 static int cmm_scan_and_split(const struct lu_env *env,
387 struct md_object *mo,
390 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
391 struct lu_rdpg *rdpg = NULL;
399 rdpg->rp_npages = SPLIT_PAGE_COUNT;
400 rdpg->rp_count = CFS_PAGE_SIZE * rdpg->rp_npages;
402 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
403 if (rdpg->rp_pages == NULL)
404 GOTO(free_rdpg, rc = -ENOMEM);
406 for (i = 0; i < rdpg->rp_npages; i++) {
407 rdpg->rp_pages[i] = alloc_pages(GFP_KERNEL, 0);
408 if (rdpg->rp_pages[i] == NULL)
409 GOTO(cleanup, rc = -ENOMEM);
412 hash_segement = MAX_HASH_SIZE / (cmm->cmm_tgt_count + 1);
413 for (i = 1; i < cmm->cmm_tgt_count + 1; i++) {
417 lf = &ma->ma_lmv->mea_ids[i];
419 rdpg->rp_hash = i * hash_segement;
420 hash_end = rdpg->rp_hash + hash_segement;
421 rc = cmm_split_entries(env, mo, rdpg, lf, hash_end);
427 for (i = 0; i < rdpg->rp_npages; i++)
428 if (rdpg->rp_pages[i] != NULL)
429 __free_pages(rdpg->rp_pages[i], 0);
431 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages *
432 sizeof rdpg->rp_pages[0]);
440 static struct lu_buf *cmm_buf_get(const struct lu_env *env, void *area,
445 buf = &cmm_env_info(env)->cmi_buf;
451 int cml_try_to_split(const struct lu_env *env, struct md_object *mo)
453 struct cmm_device *cmm = cmm_obj2dev(md2cmm_obj(mo));
454 struct md_attr *ma = &cmm_env_info(env)->cmi_ma;
459 LASSERT(S_ISDIR(lu_object_attr(&mo->mo_lu)));
461 memset(ma, 0, sizeof(*ma));
462 ma->ma_need = MA_INODE | MA_LMV;
463 rc = mo_attr_get(env, mo, ma);
467 /* step1: checking whether the dir need to be splitted */
468 rc = cmm_expect_splitting(env, mo, ma);
469 if (rc != CMM_EXPECT_SPLIT)
470 GOTO(cleanup, rc = 0);
473 * Disable trans for splitting, since there will be so many trans in
474 * this one ops, confilct with current recovery design.
476 rc = cmm_upcall(env, &cmm->cmm_md_dev, MD_NO_TRANS);
478 GOTO(cleanup, rc = 0);
480 /* step2: create slave objects */
481 rc = cmm_slaves_create(env, mo, ma);
485 /* step3: scan and split the object */
486 rc = cmm_scan_and_split(env, mo, ma);
490 buf = cmm_buf_get(env, ma->ma_lmv, ma->ma_lmv_size);
492 /* step4: set mea to the master object */
493 rc = mo_xattr_set(env, md_object_next(mo), buf, MDS_LMV_MD_NAME, 0);
495 CWARN("Dir "DFID" has been split\n",
496 PFID(lu_object_fid(&mo->mo_lu)));
499 if (ma->ma_lmv_size && ma->ma_lmv)
500 OBD_FREE(ma->ma_lmv, ma->ma_lmv_size);