1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata join handler file
7 * Copyright (c) 2001-2005 Cluster File Systems, Inc.
8 * Author: Wang Di <wangdi@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_MDS
31 #include <linux/jbd.h>
32 #include <linux/ext3_fs.h>
33 #include <obd_support.h>
34 #include <obd_class.h>
36 #include <lustre_lib.h>
37 #include <lustre/lustre_idl.h>
38 #include <lustre_mds.h>
39 #include <lustre_dlm.h>
40 #include <lustre_log.h>
41 #include <lustre_fsfilt.h>
42 #include <lustre_lite.h>
44 #include "mds_internal.h"
46 struct mdsea_cb_data {
47 struct llog_handle *mc_llh;
48 struct lov_mds_md *mc_lmm;
49 struct lov_mds_md_join *mc_lmm_join;
54 static int mdsea_iterate(struct llog_handle *llh_tail, llog_cb_t cb,
57 return llog_process(llh_tail, cb, cbdata, NULL);
60 static int mds_insert_join_lmm(struct llog_handle *llh,
61 struct lov_mds_md *lmm,
62 __u64 start, __u64 len,
63 struct lov_mds_md_join *lmmj)
65 struct llog_rec_hdr rec;
66 struct mds_extent_desc *med;
71 sz_med = lov_mds_md_size(le32_to_cpu(lmm->lmm_stripe_count));
72 sz_med += 2 * sizeof(__u64);
73 sz_med = size_round(sz_med);
75 rec.lrh_len = cpu_to_le32(sz_med);
76 rec.lrh_type = cpu_to_le32(LLOG_JOIN_REC);
78 CDEBUG(D_INFO, "insert extent "LPU64":"LPU64" lmm \n", start, len);
80 OBD_ALLOC(med, sz_med);
84 med->med_start = start;
86 memcpy(&med->med_lmm, lmm,
87 lov_mds_md_size(le32_to_cpu(lmm->lmm_stripe_count)));
89 rc = llog_write_rec(llh, &rec, NULL, 0, med, -1);
90 OBD_FREE(med, sz_med);
93 /*modify lmmj for join stripe info*/
94 lmmj->lmmj_md.lmm_stripe_count += lmm->lmm_stripe_count;
95 lmmj->lmmj_extent_count ++;
101 static int mdsea_append_extent(struct llog_handle *llh_tail,
102 struct llog_rec_hdr *rec_in_tail,
103 struct mdsea_cb_data *cbdata)
105 struct mds_extent_desc *med =
106 &((struct llog_array_rec *)rec_in_tail)->lmr_med;
110 CDEBUG(D_INODE, "insert lmm extent: "LPU64":"LPU64" \n",
111 med->med_start, med->med_len);
112 rc = mds_insert_join_lmm(cbdata->mc_llh, &med->med_lmm,
113 med->med_start + cbdata->mc_headfile_sz,
114 med->med_len, cbdata->mc_lmm_join);
116 CERROR("error %d insert the lmm \n", rc);
119 RETURN(LLOG_DEL_RECORD);
122 static void mds_init_stripe_join(struct lov_mds_md_join *lmmj,
123 struct lov_mds_md *lmm,
124 struct llog_logid *logid)
126 lmmj->lmmj_md.lmm_magic = cpu_to_le32(LOV_MAGIC_JOIN);
127 lmmj->lmmj_md.lmm_object_id = lmm->lmm_object_id;
128 lmmj->lmmj_md.lmm_object_gr = lmm->lmm_object_gr;
129 lmmj->lmmj_md.lmm_pattern = lmm->lmm_pattern;
130 lmmj->lmmj_md.lmm_stripe_size = lmm->lmm_stripe_size;
131 lmmj->lmmj_md.lmm_stripe_count = 0;
132 lmmj->lmmj_extent_count = 0;
133 lmmj->lmmj_array_id = *logid;
136 static int mdsea_cancel_last_extent(struct llog_handle *llh_tail,
137 struct llog_rec_hdr *rec_in_tail,
138 struct mdsea_cb_data *cbdata)
140 struct mds_extent_desc *med =
141 &((struct llog_array_rec *)rec_in_tail)->lmr_med;
143 CDEBUG(D_INODE, "extent: "LPU64":"LPU64" \n", med->med_start,
146 LASSERTF(cbdata->mc_offset == med->med_start,
147 "A hole in the extent "LPU64"--"LPU64"\n",
148 cbdata->mc_offset, med->med_start);
150 if (med->med_len != -1)
151 cbdata->mc_offset = med->med_start + med->med_len;
153 if (med->med_start > cbdata->mc_headfile_sz || (med->med_len == -1)) {
154 CDEBUG(D_INFO, "del rec offset"LPU64", head size "LPU64" \n",
155 med->med_start, cbdata->mc_headfile_sz);
156 if (!cbdata->mc_lmm) {
157 int stripe = le32_to_cpu(med->med_lmm.lmm_stripe_count);
158 OBD_ALLOC(cbdata->mc_lmm, lov_mds_md_size(stripe));
161 memcpy(cbdata->mc_lmm, &med->med_lmm,
162 lov_mds_md_size(stripe));
164 RETURN(LLOG_DEL_RECORD);
169 static int mds_adjust_last_extent(struct llog_handle *llh_head,
172 struct mdsea_cb_data *cbdata;
176 OBD_ALLOC_PTR(cbdata);
181 cbdata->mc_headfile_sz = head_size;
182 /*Find the last extent and cancel the record in the lmm*/
183 rc = mdsea_iterate(llh_head, (llog_cb_t)mdsea_cancel_last_extent,
187 CERROR("can not find the last extent rc=%d\n", rc);
191 LASSERT(cbdata->mc_lmm);
193 CDEBUG(D_INODE, "insert lmm extent: "LPU64":"LPU64" \n",
194 cbdata->mc_offset, (head_size - cbdata->mc_offset));
196 rc = mds_insert_join_lmm(llh_head, cbdata->mc_lmm,
198 (head_size - cbdata->mc_offset),
201 CERROR("error insert the lmm rc %d \n", rc);
203 if (cbdata && cbdata->mc_lmm)
204 OBD_FREE(cbdata->mc_lmm,
205 lov_mds_md_size(cbdata->mc_lmm->lmm_stripe_count));
207 OBD_FREE_PTR(cbdata);
212 static void mds_finish_join(struct mds_obd *mds, struct ptlrpc_request *req,
213 struct inode *inode, struct lov_mds_md_join *lmmj)
215 struct mds_body *body = (struct mds_body *)
216 lustre_msg_buf(req->rq_repmsg, 1, 0);
217 int max_cookiesize = lmmj->lmmj_md.lmm_stripe_count *
218 sizeof(struct llog_cookie);
219 int max_easize = sizeof(*lmmj);
221 CDEBUG(D_INFO, "change the max md size from %d to "LPSZ"\n",
222 mds->mds_max_mdsize, sizeof(*lmmj));
224 if (mds->mds_max_mdsize < max_easize ||
225 mds->mds_max_cookiesize < max_cookiesize) {
226 body->max_mdsize = mds->mds_max_mdsize > max_easize ?
227 mds->mds_max_mdsize : max_easize;
228 mds->mds_max_mdsize = body->max_mdsize;
229 body->max_cookiesize = mds->mds_max_cookiesize > max_cookiesize?
230 mds->mds_max_cookiesize : max_cookiesize;
231 mds->mds_max_cookiesize = body->max_cookiesize;
232 body->valid |= OBD_MD_FLMODEASIZE;
235 if (body->valid & OBD_MD_FLMODEASIZE)
236 CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
237 mds->mds_max_mdsize, mds->mds_max_cookiesize);
239 mds_pack_inode2fid(&body->fid1, inode);
240 mds_pack_inode2body(body, inode);
243 static inline void mds_pack_fid(struct ll_fid *fid, obd_id ino, __u32 gen, int type)
246 fid->generation = gen;
250 static int mds_join_unlink_tail_inode(struct mds_update_record *rec,
251 struct ptlrpc_request *req,
252 struct mds_rec_join *join_rec,
253 struct lov_mds_md *tail_lmm,
254 int lmm_size, struct dentry *dchild,
255 void **handle,struct lustre_handle *lockh)
257 struct mds_obd *mds = mds_req2mds(req);
258 struct obd_device *obd = req->rq_export->exp_obd;
259 struct inode *tail_inode, *head_inode;
260 struct dentry *de_tailparent = NULL, *de_tail = NULL, *de_head = NULL;
261 struct lustre_handle dlm_handles[4] = {{0}, {0}, {0}, {0}};
262 struct ll_fid head_fid;
267 ldlm_lock_decref(lockh, LCK_EX);
269 head_inode = dchild->d_inode;
270 mds_pack_fid(&head_fid, head_inode->i_ino, head_inode->i_generation,
271 head_inode->i_mode & S_IFMT);
273 rc = mds_get_parents_children_locked(obd, mds, &join_rec->jr_fid,
274 &de_tailparent, &head_fid,
275 &de_head, LCK_EX, rec->ur_name,
276 rec->ur_namelen, &de_tail,
277 NULL, 0, NULL, dlm_handles,
282 *lockh = dlm_handles[1];
283 LASSERT(de_tailparent);
284 tail_inode = de_tail->d_inode;
285 if (tail_inode == NULL) {
286 CERROR("tail inode doesn't exist(dir %lu,name %s)!\n",
287 de_tailparent? de_tailparent->d_inode->i_ino : 0,
289 GOTO(cleanup, rc = -ENOENT);
292 if (!S_ISREG(tail_inode->i_mode)) {
293 CERROR("tail file is not a regular file (dir %lu, name %s)!\n",
294 de_tailparent? de_tailparent->d_inode->i_ino : 0,
296 GOTO(cleanup, rc = -EINVAL);
299 *handle = fsfilt_start(obd, head_inode, FSFILT_OP_JOIN, NULL);
300 if (IS_ERR(*handle)) {
301 rc = PTR_ERR(*handle);
305 rc = mds_get_md(obd, tail_inode, tail_lmm, &lmm_size, 1);
306 if (rc < 0) /* get md fails */
309 LASSERT(le32_to_cpu(tail_lmm->lmm_magic) == LOV_MAGIC_JOIN ||
310 le32_to_cpu(tail_lmm->lmm_magic) == LOV_MAGIC);
312 LASSERT(de_tailparent);
313 rc = vfs_unlink(de_tailparent->d_inode, de_tail);
316 CDEBUG(D_INODE, "delete the tail inode %lu/%u \n",
317 tail_inode->i_ino, tail_inode->i_generation);
320 if (dlm_handles[2].cookie != 0)
321 ldlm_lock_decref(&dlm_handles[2], LCK_EX);
323 if (dlm_handles[0].cookie != 0) {
325 ldlm_lock_decref(&dlm_handles[0], LCK_EX);
327 ptlrpc_save_lock(req, &dlm_handles[0], LCK_EX);
333 l_dput(de_tailparent);
341 int mds_join_file(struct mds_update_record *rec, struct ptlrpc_request *req,
342 struct dentry *de_head, struct lustre_handle *lockh)
344 struct mds_obd *mds = mds_req2mds(req);
345 struct obd_device *obd = req->rq_export->exp_obd;
346 struct inode *head_inode = NULL;
347 struct lvfs_run_ctxt saved;
349 struct lov_mds_md *head_lmm, *tail_lmm;
350 struct lov_mds_md_join *head_lmmj = NULL, *tail_lmmj = NULL;
351 int lmm_size, rc = 0, cleanup_phase = 0, size;
352 struct llog_handle *llh_head = NULL, *llh_tail = NULL;
353 struct llog_ctxt *ctxt;
354 struct mds_rec_join *join_rec;
357 join_rec = lustre_swab_reqbuf(req, DLM_INTENT_REC_OFF + 3,
359 lustre_swab_mds_rec_join);
360 if (join_rec == NULL)
363 DEBUG_REQ(D_INODE, req,"head "LPU64"/%u, ptail ino "LPU64"/%u, tail %s",
364 rec->ur_fid1->id, rec->ur_fid1->generation,
365 join_rec->jr_fid.id, join_rec->jr_fid.generation,
368 size = mds->mds_max_mdsize;
369 lmm_size = mds->mds_max_mdsize;
370 OBD_ALLOC(head_lmm, lmm_size);
371 OBD_ALLOC(tail_lmm, lmm_size);
372 if (!head_lmm || !tail_lmm)
373 GOTO(cleanup, rc = -ENOMEM);
375 /* acquire head's dentry */
377 head_inode = de_head->d_inode;
378 if (head_inode == NULL) {
379 CERROR("head inode doesn't exist!\n");
380 GOTO(cleanup, rc = -ENOENT);
383 /*Unlink tail inode and get the lmm back*/
384 rc = mds_join_unlink_tail_inode(rec, req, join_rec, tail_lmm, lmm_size,
385 de_head, &handle, lockh);
387 CERROR("unlink tail_inode error %d\n", rc);
391 LOCK_INODE_MUTEX(head_inode);
393 rc = mds_get_md(obd, head_inode, head_lmm, &size, 0);
397 LASSERT(le32_to_cpu(head_lmm->lmm_magic) == LOV_MAGIC_JOIN ||
398 le32_to_cpu(head_lmm->lmm_magic) == LOV_MAGIC);
400 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
401 ctxt = llog_get_context(obd, LLOG_LOVEA_ORIG_CTXT);
403 if (le32_to_cpu(head_lmm->lmm_magic) == LOV_MAGIC) { /*simple file */
404 struct llog_logid *llog_array;
406 rc = llog_create(ctxt, &llh_head, NULL, NULL);
408 CERROR("cannot create new log, error = %d\n", rc);
412 llog_array = &llh_head->lgh_id;
413 CDEBUG(D_INFO,"create arrary for %lu with id "LPU64":"LPU64"\n",
414 head_inode->i_ino, llog_array->lgl_oid,
415 llog_array->lgl_ogr);
416 rc = llog_init_handle(llh_head, LLOG_F_IS_PLAIN, NULL);
419 OBD_ALLOC_PTR(head_lmmj);
420 if (head_lmmj == NULL)
421 GOTO(cleanup, rc = -ENOMEM);
422 mds_init_stripe_join(head_lmmj, head_lmm, llog_array);
423 mds_insert_join_lmm(llh_head, head_lmm, 0,join_rec->jr_headsize,
425 } else { /*head lmm is join file */
426 head_lmmj = (struct lov_mds_md_join *)head_lmm;
427 /* construct and fill extent llog object */
428 rc = llog_create(ctxt, &llh_head,
429 &head_lmmj->lmmj_array_id, NULL);
431 CERROR("cannot open existing log, error = %d\n", rc);
435 rc = llog_init_handle(llh_head, LLOG_F_IS_PLAIN, NULL);
438 rc = mds_adjust_last_extent(llh_head, join_rec->jr_headsize);
440 CERROR("can't adjust last extent of obj rc=%d\n", rc);
445 if (le32_to_cpu(tail_lmm->lmm_magic) != LOV_MAGIC_JOIN) {
446 mds_insert_join_lmm(llh_head, tail_lmm, join_rec->jr_headsize,
449 struct mdsea_cb_data cbdata;
450 tail_lmmj = (struct lov_mds_md_join *)tail_lmm;
452 rc = llog_create(ctxt,&llh_tail,&tail_lmmj->lmmj_array_id,NULL);
454 CERROR("cannot open existing log, error = %d\n", rc);
457 rc = llog_init_handle(llh_tail, LLOG_F_IS_PLAIN, NULL);
459 llog_close(llh_tail);
462 cbdata.mc_llh = llh_head;
463 cbdata.mc_headfile_sz = join_rec->jr_headsize;
464 cbdata.mc_lmm_join = head_lmmj;
465 rc = mdsea_iterate(llh_tail, (llog_cb_t)mdsea_append_extent,
468 llog_close(llh_tail);
469 CERROR("can not append extent log error %d \n", rc);
472 rc = llog_destroy(llh_tail);
474 llog_close(llh_tail);
475 CERROR("can not destroy log error %d \n", rc);
478 llog_free_handle(llh_tail);
481 CDEBUG(D_INODE, "join finish, set lmm V2 to inode %lu \n",
483 fsfilt_set_md(obd, head_inode, handle, head_lmmj,
484 sizeof(struct lov_mds_md_join), "lov");
485 mds_finish_join(mds, req, head_inode, head_lmmj);
487 rc = mds_finish_transno(mds, head_inode, handle, req, rc, 0);
488 switch(cleanup_phase){
490 llog_close(llh_head);
492 if (head_lmmj && ((void*)head_lmmj != (void*)head_lmm))
493 OBD_FREE_PTR(head_lmmj);
495 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
497 UNLOCK_INODE_MUTEX(head_inode);
499 if (tail_lmm != NULL)
500 OBD_FREE(tail_lmm, lmm_size);
501 if (head_lmm != NULL)
502 OBD_FREE(head_lmm, lmm_size);
505 CERROR("invalid cleanup_phase %d\n", cleanup_phase);