12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130 |
- #include "xfs.h"
- #include "xfs_fs.h"
- #include "xfs_format.h"
- #include "xfs_log_format.h"
- #include "xfs_shared.h"
- #include "xfs_trans_resv.h"
- #include "xfs_mount.h"
- #include "xfs_error.h"
- #include "xfs_alloc.h"
- #include "xfs_extent_busy.h"
- #include "xfs_discard.h"
- #include "xfs_trans.h"
- #include "xfs_trans_priv.h"
- #include "xfs_log.h"
- #include "xfs_log_priv.h"
- static struct xlog_ticket *
- xlog_cil_ticket_alloc(
- struct xlog *log)
- {
- struct xlog_ticket *tic;
- tic = xlog_ticket_alloc(log, 0, 1, XFS_TRANSACTION, 0,
- KM_SLEEP|KM_NOFS);
-
- tic->t_curr_res = 0;
- return tic;
- }
- void
- xlog_cil_init_post_recovery(
- struct xlog *log)
- {
- log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log);
- log->l_cilp->xc_ctx->sequence = 1;
- }
- static inline int
- xlog_cil_iovec_space(
- uint niovecs)
- {
- return round_up((sizeof(struct xfs_log_vec) +
- niovecs * sizeof(struct xfs_log_iovec)),
- sizeof(uint64_t));
- }
- static void
- xlog_cil_alloc_shadow_bufs(
- struct xlog *log,
- struct xfs_trans *tp)
- {
- struct xfs_log_item_desc *lidp;
- list_for_each_entry(lidp, &tp->t_items, lid_trans) {
- struct xfs_log_item *lip = lidp->lid_item;
- struct xfs_log_vec *lv;
- int niovecs = 0;
- int nbytes = 0;
- int buf_size;
- bool ordered = false;
-
- if (!(lidp->lid_flags & XFS_LID_DIRTY))
- continue;
-
- lip->li_ops->iop_size(lip, &niovecs, &nbytes);
-
- if (niovecs == XFS_LOG_VEC_ORDERED) {
- ordered = true;
- niovecs = 0;
- nbytes = 0;
- }
-
- nbytes += niovecs * sizeof(uint64_t);
- nbytes = round_up(nbytes, sizeof(uint64_t));
-
- buf_size = nbytes + xlog_cil_iovec_space(niovecs);
-
- if (!lip->li_lv_shadow ||
- buf_size > lip->li_lv_shadow->lv_size) {
-
- kmem_free(lip->li_lv_shadow);
- lv = kmem_alloc(buf_size, KM_SLEEP|KM_NOFS);
- memset(lv, 0, xlog_cil_iovec_space(niovecs));
- lv->lv_item = lip;
- lv->lv_size = buf_size;
- if (ordered)
- lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
- else
- lv->lv_iovecp = (struct xfs_log_iovec *)&lv[1];
- lip->li_lv_shadow = lv;
- } else {
-
- lv = lip->li_lv_shadow;
- if (ordered)
- lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
- else
- lv->lv_buf_len = 0;
- lv->lv_bytes = 0;
- lv->lv_next = NULL;
- }
-
- lv->lv_niovecs = niovecs;
-
- lv->lv_buf = (char *)lv + xlog_cil_iovec_space(niovecs);
- }
- }
- STATIC void
- xfs_cil_prepare_item(
- struct xlog *log,
- struct xfs_log_vec *lv,
- struct xfs_log_vec *old_lv,
- int *diff_len,
- int *diff_iovecs)
- {
-
- if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED) {
- *diff_len += lv->lv_bytes;
- *diff_iovecs += lv->lv_niovecs;
- }
-
- if (!old_lv) {
- lv->lv_item->li_ops->iop_pin(lv->lv_item);
- lv->lv_item->li_lv_shadow = NULL;
- } else if (old_lv != lv) {
- ASSERT(lv->lv_buf_len != XFS_LOG_VEC_ORDERED);
- *diff_len -= old_lv->lv_bytes;
- *diff_iovecs -= old_lv->lv_niovecs;
- lv->lv_item->li_lv_shadow = old_lv;
- }
-
- lv->lv_item->li_lv = lv;
-
- if (!lv->lv_item->li_seq)
- lv->lv_item->li_seq = log->l_cilp->xc_ctx->sequence;
- }
- static void
- xlog_cil_insert_format_items(
- struct xlog *log,
- struct xfs_trans *tp,
- int *diff_len,
- int *diff_iovecs)
- {
- struct xfs_log_item_desc *lidp;
-
- if (list_empty(&tp->t_items)) {
- ASSERT(0);
- return;
- }
- list_for_each_entry(lidp, &tp->t_items, lid_trans) {
- struct xfs_log_item *lip = lidp->lid_item;
- struct xfs_log_vec *lv;
- struct xfs_log_vec *old_lv = NULL;
- struct xfs_log_vec *shadow;
- bool ordered = false;
-
- if (!(lidp->lid_flags & XFS_LID_DIRTY))
- continue;
-
- shadow = lip->li_lv_shadow;
- if (shadow->lv_buf_len == XFS_LOG_VEC_ORDERED)
- ordered = true;
-
- if (!shadow->lv_niovecs && !ordered)
- continue;
-
- old_lv = lip->li_lv;
- if (lip->li_lv && shadow->lv_size <= lip->li_lv->lv_size) {
-
- lv = lip->li_lv;
- lv->lv_next = NULL;
- if (ordered)
- goto insert;
-
- *diff_iovecs -= lv->lv_niovecs;
- *diff_len -= lv->lv_bytes;
-
- lv->lv_niovecs = shadow->lv_niovecs;
-
- lv->lv_buf_len = 0;
- lv->lv_bytes = 0;
- lv->lv_buf = (char *)lv +
- xlog_cil_iovec_space(lv->lv_niovecs);
- } else {
-
- lv = shadow;
- lv->lv_item = lip;
- if (ordered) {
-
- ASSERT(lip->li_lv == NULL);
- goto insert;
- }
- }
- ASSERT(IS_ALIGNED((unsigned long)lv->lv_buf, sizeof(uint64_t)));
- lip->li_ops->iop_format(lip, lv);
- insert:
- xfs_cil_prepare_item(log, lv, old_lv, diff_len, diff_iovecs);
- }
- }
- static void
- xlog_cil_insert_items(
- struct xlog *log,
- struct xfs_trans *tp)
- {
- struct xfs_cil *cil = log->l_cilp;
- struct xfs_cil_ctx *ctx = cil->xc_ctx;
- struct xfs_log_item_desc *lidp;
- int len = 0;
- int diff_iovecs = 0;
- int iclog_space;
- ASSERT(tp);
-
- xlog_cil_insert_format_items(log, tp, &len, &diff_iovecs);
-
- spin_lock(&cil->xc_cil_lock);
- list_for_each_entry(lidp, &tp->t_items, lid_trans) {
- struct xfs_log_item *lip = lidp->lid_item;
-
- if (!(lidp->lid_flags & XFS_LID_DIRTY))
- continue;
-
- if (!list_is_last(&lip->li_cil, &cil->xc_cil))
- list_move_tail(&lip->li_cil, &cil->xc_cil);
- }
-
- len += diff_iovecs * sizeof(xlog_op_header_t);
- ctx->nvecs += diff_iovecs;
-
- if (!list_empty(&tp->t_busy))
- list_splice_init(&tp->t_busy, &ctx->busy_extents);
-
- if (ctx->ticket->t_curr_res == 0) {
- ctx->ticket->t_curr_res = ctx->ticket->t_unit_res;
- tp->t_ticket->t_curr_res -= ctx->ticket->t_unit_res;
- }
-
- iclog_space = log->l_iclog_size - log->l_iclog_hsize;
- if (len > 0 && (ctx->space_used / iclog_space !=
- (ctx->space_used + len) / iclog_space)) {
- int hdrs;
- hdrs = (len + iclog_space - 1) / iclog_space;
-
- hdrs *= log->l_iclog_hsize + sizeof(struct xlog_op_header);
- ctx->ticket->t_unit_res += hdrs;
- ctx->ticket->t_curr_res += hdrs;
- tp->t_ticket->t_curr_res -= hdrs;
- ASSERT(tp->t_ticket->t_curr_res >= len);
- }
- tp->t_ticket->t_curr_res -= len;
- ctx->space_used += len;
- spin_unlock(&cil->xc_cil_lock);
- }
- static void
- xlog_cil_free_logvec(
- struct xfs_log_vec *log_vector)
- {
- struct xfs_log_vec *lv;
- for (lv = log_vector; lv; ) {
- struct xfs_log_vec *next = lv->lv_next;
- kmem_free(lv);
- lv = next;
- }
- }
- static void
- xlog_cil_committed(
- void *args,
- int abort)
- {
- struct xfs_cil_ctx *ctx = args;
- struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
- xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, ctx->lv_chain,
- ctx->start_lsn, abort);
- xfs_extent_busy_sort(&ctx->busy_extents);
- xfs_extent_busy_clear(mp, &ctx->busy_extents,
- (mp->m_flags & XFS_MOUNT_DISCARD) && !abort);
-
- spin_lock(&ctx->cil->xc_push_lock);
- if (abort)
- wake_up_all(&ctx->cil->xc_commit_wait);
- list_del(&ctx->committing);
- spin_unlock(&ctx->cil->xc_push_lock);
- xlog_cil_free_logvec(ctx->lv_chain);
- if (!list_empty(&ctx->busy_extents)) {
- ASSERT(mp->m_flags & XFS_MOUNT_DISCARD);
- xfs_discard_extents(mp, &ctx->busy_extents);
- xfs_extent_busy_clear(mp, &ctx->busy_extents, false);
- }
- kmem_free(ctx);
- }
- STATIC int
- xlog_cil_push(
- struct xlog *log)
- {
- struct xfs_cil *cil = log->l_cilp;
- struct xfs_log_vec *lv;
- struct xfs_cil_ctx *ctx;
- struct xfs_cil_ctx *new_ctx;
- struct xlog_in_core *commit_iclog;
- struct xlog_ticket *tic;
- int num_iovecs;
- int error = 0;
- struct xfs_trans_header thdr;
- struct xfs_log_iovec lhdr;
- struct xfs_log_vec lvhdr = { NULL };
- xfs_lsn_t commit_lsn;
- xfs_lsn_t push_seq;
- if (!cil)
- return 0;
- new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_SLEEP|KM_NOFS);
- new_ctx->ticket = xlog_cil_ticket_alloc(log);
- down_write(&cil->xc_ctx_lock);
- ctx = cil->xc_ctx;
- spin_lock(&cil->xc_push_lock);
- push_seq = cil->xc_push_seq;
- ASSERT(push_seq <= ctx->sequence);
-
- if (list_empty(&cil->xc_cil)) {
- cil->xc_push_seq = 0;
- spin_unlock(&cil->xc_push_lock);
- goto out_skip;
- }
-
- if (push_seq < cil->xc_ctx->sequence) {
- spin_unlock(&cil->xc_push_lock);
- goto out_skip;
- }
-
- list_add(&ctx->committing, &cil->xc_committing);
- spin_unlock(&cil->xc_push_lock);
-
- lv = NULL;
- num_iovecs = 0;
- while (!list_empty(&cil->xc_cil)) {
- struct xfs_log_item *item;
- item = list_first_entry(&cil->xc_cil,
- struct xfs_log_item, li_cil);
- list_del_init(&item->li_cil);
- if (!ctx->lv_chain)
- ctx->lv_chain = item->li_lv;
- else
- lv->lv_next = item->li_lv;
- lv = item->li_lv;
- item->li_lv = NULL;
- num_iovecs += lv->lv_niovecs;
- }
-
- INIT_LIST_HEAD(&new_ctx->committing);
- INIT_LIST_HEAD(&new_ctx->busy_extents);
- new_ctx->sequence = ctx->sequence + 1;
- new_ctx->cil = cil;
- cil->xc_ctx = new_ctx;
-
- spin_lock(&cil->xc_push_lock);
- cil->xc_current_sequence = new_ctx->sequence;
- spin_unlock(&cil->xc_push_lock);
- up_write(&cil->xc_ctx_lock);
-
- tic = ctx->ticket;
- thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
- thdr.th_type = XFS_TRANS_CHECKPOINT;
- thdr.th_tid = tic->t_tid;
- thdr.th_num_items = num_iovecs;
- lhdr.i_addr = &thdr;
- lhdr.i_len = sizeof(xfs_trans_header_t);
- lhdr.i_type = XLOG_REG_TYPE_TRANSHDR;
- tic->t_curr_res -= lhdr.i_len + sizeof(xlog_op_header_t);
- lvhdr.lv_niovecs = 1;
- lvhdr.lv_iovecp = &lhdr;
- lvhdr.lv_next = ctx->lv_chain;
- error = xlog_write(log, &lvhdr, tic, &ctx->start_lsn, NULL, 0);
- if (error)
- goto out_abort_free_ticket;
-
- restart:
- spin_lock(&cil->xc_push_lock);
- list_for_each_entry(new_ctx, &cil->xc_committing, committing) {
-
- if (XLOG_FORCED_SHUTDOWN(log)) {
- spin_unlock(&cil->xc_push_lock);
- goto out_abort_free_ticket;
- }
-
- if (new_ctx->sequence >= ctx->sequence)
- continue;
- if (!new_ctx->commit_lsn) {
-
- xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
- goto restart;
- }
- }
- spin_unlock(&cil->xc_push_lock);
-
- commit_lsn = xfs_log_done(log->l_mp, tic, &commit_iclog, false);
- if (commit_lsn == -1)
- goto out_abort;
-
- ctx->log_cb.cb_func = xlog_cil_committed;
- ctx->log_cb.cb_arg = ctx;
- error = xfs_log_notify(log->l_mp, commit_iclog, &ctx->log_cb);
- if (error)
- goto out_abort;
-
- spin_lock(&cil->xc_push_lock);
- ctx->commit_lsn = commit_lsn;
- wake_up_all(&cil->xc_commit_wait);
- spin_unlock(&cil->xc_push_lock);
-
- return xfs_log_release_iclog(log->l_mp, commit_iclog);
- out_skip:
- up_write(&cil->xc_ctx_lock);
- xfs_log_ticket_put(new_ctx->ticket);
- kmem_free(new_ctx);
- return 0;
- out_abort_free_ticket:
- xfs_log_ticket_put(tic);
- out_abort:
- xlog_cil_committed(ctx, XFS_LI_ABORTED);
- return -EIO;
- }
- static void
- xlog_cil_push_work(
- struct work_struct *work)
- {
- struct xfs_cil *cil = container_of(work, struct xfs_cil,
- xc_push_work);
- xlog_cil_push(cil->xc_log);
- }
- static void
- xlog_cil_push_background(
- struct xlog *log)
- {
- struct xfs_cil *cil = log->l_cilp;
-
- ASSERT(!list_empty(&cil->xc_cil));
-
- if (cil->xc_ctx->space_used < XLOG_CIL_SPACE_LIMIT(log))
- return;
- spin_lock(&cil->xc_push_lock);
- if (cil->xc_push_seq < cil->xc_current_sequence) {
- cil->xc_push_seq = cil->xc_current_sequence;
- queue_work(log->l_mp->m_cil_workqueue, &cil->xc_push_work);
- }
- spin_unlock(&cil->xc_push_lock);
- }
- static void
- xlog_cil_push_now(
- struct xlog *log,
- xfs_lsn_t push_seq)
- {
- struct xfs_cil *cil = log->l_cilp;
- if (!cil)
- return;
- ASSERT(push_seq && push_seq <= cil->xc_current_sequence);
-
- flush_work(&cil->xc_push_work);
-
- spin_lock(&cil->xc_push_lock);
- if (list_empty(&cil->xc_cil) || push_seq <= cil->xc_push_seq) {
- spin_unlock(&cil->xc_push_lock);
- return;
- }
- cil->xc_push_seq = push_seq;
- queue_work(log->l_mp->m_cil_workqueue, &cil->xc_push_work);
- spin_unlock(&cil->xc_push_lock);
- }
- bool
- xlog_cil_empty(
- struct xlog *log)
- {
- struct xfs_cil *cil = log->l_cilp;
- bool empty = false;
- spin_lock(&cil->xc_push_lock);
- if (list_empty(&cil->xc_cil))
- empty = true;
- spin_unlock(&cil->xc_push_lock);
- return empty;
- }
- void
- xfs_log_commit_cil(
- struct xfs_mount *mp,
- struct xfs_trans *tp,
- xfs_lsn_t *commit_lsn,
- bool regrant)
- {
- struct xlog *log = mp->m_log;
- struct xfs_cil *cil = log->l_cilp;
-
- xlog_cil_alloc_shadow_bufs(log, tp);
-
- down_read(&cil->xc_ctx_lock);
- xlog_cil_insert_items(log, tp);
-
- if (tp->t_ticket->t_curr_res < 0)
- xlog_print_tic_res(mp, tp->t_ticket);
- tp->t_commit_lsn = cil->xc_ctx->sequence;
- if (commit_lsn)
- *commit_lsn = tp->t_commit_lsn;
- xfs_log_done(mp, tp->t_ticket, NULL, regrant);
- xfs_trans_unreserve_and_mod_sb(tp);
-
- xfs_trans_free_items(tp, tp->t_commit_lsn, false);
- xlog_cil_push_background(log);
- up_read(&cil->xc_ctx_lock);
- }
- xfs_lsn_t
- xlog_cil_force_lsn(
- struct xlog *log,
- xfs_lsn_t sequence)
- {
- struct xfs_cil *cil = log->l_cilp;
- struct xfs_cil_ctx *ctx;
- xfs_lsn_t commit_lsn = NULLCOMMITLSN;
- ASSERT(sequence <= cil->xc_current_sequence);
-
- restart:
- xlog_cil_push_now(log, sequence);
-
- spin_lock(&cil->xc_push_lock);
- list_for_each_entry(ctx, &cil->xc_committing, committing) {
-
- if (XLOG_FORCED_SHUTDOWN(log))
- goto out_shutdown;
- if (ctx->sequence > sequence)
- continue;
- if (!ctx->commit_lsn) {
-
- xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
- goto restart;
- }
- if (ctx->sequence != sequence)
- continue;
-
- commit_lsn = ctx->commit_lsn;
- }
-
- if (sequence == cil->xc_current_sequence &&
- !list_empty(&cil->xc_cil)) {
- spin_unlock(&cil->xc_push_lock);
- goto restart;
- }
- spin_unlock(&cil->xc_push_lock);
- return commit_lsn;
-
- out_shutdown:
- spin_unlock(&cil->xc_push_lock);
- return 0;
- }
- bool
- xfs_log_item_in_current_chkpt(
- struct xfs_log_item *lip)
- {
- struct xfs_cil_ctx *ctx;
- if (list_empty(&lip->li_cil))
- return false;
- ctx = lip->li_mountp->m_log->l_cilp->xc_ctx;
-
- if (XFS_LSN_CMP(lip->li_seq, ctx->sequence) != 0)
- return false;
- return true;
- }
- int
- xlog_cil_init(
- struct xlog *log)
- {
- struct xfs_cil *cil;
- struct xfs_cil_ctx *ctx;
- cil = kmem_zalloc(sizeof(*cil), KM_SLEEP|KM_MAYFAIL);
- if (!cil)
- return -ENOMEM;
- ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP|KM_MAYFAIL);
- if (!ctx) {
- kmem_free(cil);
- return -ENOMEM;
- }
- INIT_WORK(&cil->xc_push_work, xlog_cil_push_work);
- INIT_LIST_HEAD(&cil->xc_cil);
- INIT_LIST_HEAD(&cil->xc_committing);
- spin_lock_init(&cil->xc_cil_lock);
- spin_lock_init(&cil->xc_push_lock);
- init_rwsem(&cil->xc_ctx_lock);
- init_waitqueue_head(&cil->xc_commit_wait);
- INIT_LIST_HEAD(&ctx->committing);
- INIT_LIST_HEAD(&ctx->busy_extents);
- ctx->sequence = 1;
- ctx->cil = cil;
- cil->xc_ctx = ctx;
- cil->xc_current_sequence = ctx->sequence;
- cil->xc_log = log;
- log->l_cilp = cil;
- return 0;
- }
- void
- xlog_cil_destroy(
- struct xlog *log)
- {
- if (log->l_cilp->xc_ctx) {
- if (log->l_cilp->xc_ctx->ticket)
- xfs_log_ticket_put(log->l_cilp->xc_ctx->ticket);
- kmem_free(log->l_cilp->xc_ctx);
- }
- ASSERT(list_empty(&log->l_cilp->xc_cil));
- kmem_free(log->l_cilp);
- }
|