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 int mds_join_unlink_tail_inode(struct mds_update_record *rec,
244 struct ptlrpc_request *req,
245 struct mds_rec_join *join_rec,
246 struct lov_mds_md *tail_lmm,
247 int lmm_size, struct dentry *dchild,
248 void **handle,struct lustre_handle *lockh)
250 struct mds_obd *mds = mds_req2mds(req);
251 struct obd_device *obd = req->rq_export->exp_obd;
252 struct inode *tail_inode, *head_inode;
253 struct dentry *de_tailparent = NULL, *de_tail = NULL, *de_head = NULL;
254 struct lustre_handle dlm_handles[4] = {{0}, {0}, {0}, {0}};
255 struct ll_fid head_fid;
260 ldlm_lock_decref(lockh, LCK_EX);
262 head_inode = dchild->d_inode;
263 mdc_pack_fid(&head_fid, head_inode->i_ino, head_inode->i_generation,
264 head_inode->i_mode & S_IFMT);
266 rc = mds_get_parents_children_locked(obd, mds, &join_rec->jr_fid,
267 &de_tailparent, &head_fid,
268 &de_head, LCK_EX, rec->ur_name,
269 rec->ur_namelen, &de_tail,
270 NULL, 0, NULL, dlm_handles,
275 *lockh = dlm_handles[1];
276 LASSERT(de_tailparent);
277 tail_inode = de_tail->d_inode;
278 if (tail_inode == NULL) {
279 CERROR("tail inode doesn't exist(dir %lu,name %s)!\n",
280 de_tailparent? de_tailparent->d_inode->i_ino : 0,
282 GOTO(cleanup, rc = -ENOENT);
285 if (!S_ISREG(tail_inode->i_mode)) {
286 CERROR("tail file is not a regular file (dir %lu, name %s)!\n",
287 de_tailparent? de_tailparent->d_inode->i_ino : 0,
289 GOTO(cleanup, rc = -EINVAL);
292 *handle = fsfilt_start(obd, head_inode, FSFILT_OP_JOIN, NULL);
293 if (IS_ERR(*handle)) {
294 rc = PTR_ERR(*handle);
298 rc = mds_get_md(obd, tail_inode, tail_lmm, &lmm_size, 1);
299 if (rc < 0) /* get md fails */
302 LASSERT(le32_to_cpu(tail_lmm->lmm_magic) == LOV_MAGIC_JOIN ||
303 le32_to_cpu(tail_lmm->lmm_magic) == LOV_MAGIC);
305 LASSERT(de_tailparent);
306 rc = vfs_unlink(de_tailparent->d_inode, de_tail);
309 CDEBUG(D_INODE, "delete the tail inode %lu/%u \n",
310 tail_inode->i_ino, tail_inode->i_generation);
313 if (dlm_handles[2].cookie != 0)
314 ldlm_lock_decref(&dlm_handles[2], LCK_EX);
316 if (dlm_handles[0].cookie != 0) {
318 ldlm_lock_decref(&dlm_handles[0], LCK_EX);
320 ptlrpc_save_lock(req, &dlm_handles[0], LCK_EX);
326 l_dput(de_tailparent);
334 int mds_join_file(struct mds_update_record *rec, struct ptlrpc_request *req,
335 struct dentry *de_head, struct lustre_handle *lockh)
337 struct mds_obd *mds = mds_req2mds(req);
338 struct obd_device *obd = req->rq_export->exp_obd;
339 struct inode *head_inode = NULL;
340 struct lvfs_run_ctxt saved;
342 struct lov_mds_md *head_lmm, *tail_lmm;
343 struct lov_mds_md_join *head_lmmj = NULL, *tail_lmmj = NULL;
344 int lmm_size, rc = 0, cleanup_phase = 0, size;
345 struct llog_handle *llh_head = NULL, *llh_tail = NULL;
346 struct llog_ctxt *ctxt;
347 struct mds_rec_join *join_rec;
350 join_rec = lustre_swab_reqbuf(req, DLM_INTENT_REC_OFF + 3,
352 lustre_swab_mds_rec_join);
353 if (join_rec == NULL)
356 DEBUG_REQ(D_INODE, req,"head "LPU64"/%u, ptail ino "LPU64"/%u, tail %s",
357 rec->ur_fid1->id, rec->ur_fid1->generation,
358 join_rec->jr_fid.id, join_rec->jr_fid.generation,
361 size = mds->mds_max_mdsize;
362 lmm_size = mds->mds_max_mdsize;
363 OBD_ALLOC(head_lmm, lmm_size);
364 OBD_ALLOC(tail_lmm, lmm_size);
365 if (!head_lmm || !tail_lmm)
366 GOTO(cleanup, rc = -ENOMEM);
368 /* acquire head's dentry */
370 head_inode = de_head->d_inode;
371 if (head_inode == NULL) {
372 CERROR("head inode doesn't exist!\n");
373 GOTO(cleanup, rc = -ENOENT);
376 /*Unlink tail inode and get the lmm back*/
377 rc = mds_join_unlink_tail_inode(rec, req, join_rec, tail_lmm, lmm_size,
378 de_head, &handle, lockh);
380 CERROR("unlink tail_inode error %d\n", rc);
384 LOCK_INODE_MUTEX(head_inode);
386 rc = mds_get_md(obd, head_inode, head_lmm, &size, 0);
390 LASSERT(le32_to_cpu(head_lmm->lmm_magic) == LOV_MAGIC_JOIN ||
391 le32_to_cpu(head_lmm->lmm_magic) == LOV_MAGIC);
393 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
394 ctxt = llog_get_context(obd, LLOG_LOVEA_ORIG_CTXT);
396 if (le32_to_cpu(head_lmm->lmm_magic) == LOV_MAGIC) { /*simple file */
397 struct llog_logid *llog_array;
399 rc = llog_create(ctxt, &llh_head, NULL, NULL);
401 CERROR("cannot create new log, error = %d\n", rc);
405 llog_array = &llh_head->lgh_id;
406 CDEBUG(D_INFO,"create arrary for %lu with id "LPU64":"LPU64"\n",
407 head_inode->i_ino, llog_array->lgl_oid,
408 llog_array->lgl_ogr);
409 rc = llog_init_handle(llh_head, LLOG_F_IS_PLAIN, NULL);
412 OBD_ALLOC_PTR(head_lmmj);
413 if (head_lmmj == NULL)
414 GOTO(cleanup, rc = -ENOMEM);
415 mds_init_stripe_join(head_lmmj, head_lmm, llog_array);
416 mds_insert_join_lmm(llh_head, head_lmm, 0,join_rec->jr_headsize,
418 } else { /*head lmm is join file */
419 head_lmmj = (struct lov_mds_md_join *)head_lmm;
420 /* construct and fill extent llog object */
421 rc = llog_create(ctxt, &llh_head,
422 &head_lmmj->lmmj_array_id, NULL);
424 CERROR("cannot open existing log, error = %d\n", rc);
428 rc = llog_init_handle(llh_head, LLOG_F_IS_PLAIN, NULL);
431 rc = mds_adjust_last_extent(llh_head, join_rec->jr_headsize);
433 CERROR("can't adjust last extent of obj rc=%d\n", rc);
438 if (le32_to_cpu(tail_lmm->lmm_magic) != LOV_MAGIC_JOIN) {
439 mds_insert_join_lmm(llh_head, tail_lmm, join_rec->jr_headsize,
442 struct mdsea_cb_data cbdata;
443 tail_lmmj = (struct lov_mds_md_join *)tail_lmm;
445 rc = llog_create(ctxt,&llh_tail,&tail_lmmj->lmmj_array_id,NULL);
447 CERROR("cannot open existing log, error = %d\n", rc);
450 rc = llog_init_handle(llh_tail, LLOG_F_IS_PLAIN, NULL);
452 llog_close(llh_tail);
455 cbdata.mc_llh = llh_head;
456 cbdata.mc_headfile_sz = join_rec->jr_headsize;
457 cbdata.mc_lmm_join = head_lmmj;
458 rc = mdsea_iterate(llh_tail, (llog_cb_t)mdsea_append_extent,
461 llog_close(llh_tail);
462 CERROR("can not append extent log error %d \n", rc);
465 rc = llog_destroy(llh_tail);
467 llog_close(llh_tail);
468 CERROR("can not destroy log error %d \n", rc);
471 llog_free_handle(llh_tail);
474 CDEBUG(D_INODE, "join finish, set lmm V2 to inode %lu \n",
476 fsfilt_set_md(obd, head_inode, handle, head_lmmj,
477 sizeof(struct lov_mds_md_join), "lov");
478 mds_finish_join(mds, req, head_inode, head_lmmj);
480 rc = mds_finish_transno(mds, head_inode, handle, req, rc, 0);
481 switch(cleanup_phase){
483 llog_close(llh_head);
485 if (head_lmmj && ((void*)head_lmmj != (void*)head_lmm))
486 OBD_FREE_PTR(head_lmmj);
488 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
490 UNLOCK_INODE_MUTEX(head_inode);
492 if (tail_lmm != NULL)
493 OBD_FREE(tail_lmm, lmm_size);
494 if (head_lmm != NULL)
495 OBD_FREE(head_lmm, lmm_size);
498 CERROR("invalid cleanup_phase %d\n", cleanup_phase);