11da177e4SLinus Torvalds /* 27b718769SNathan Scott * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. 37b718769SNathan Scott * All Rights Reserved. 41da177e4SLinus Torvalds * 57b718769SNathan Scott * This program is free software; you can redistribute it and/or 67b718769SNathan Scott * modify it under the terms of the GNU General Public License as 71da177e4SLinus Torvalds * published by the Free Software Foundation. 81da177e4SLinus Torvalds * 97b718769SNathan Scott * This program is distributed in the hope that it would be useful, 107b718769SNathan Scott * but WITHOUT ANY WARRANTY; without even the implied warranty of 117b718769SNathan Scott * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 127b718769SNathan Scott * GNU General Public License for more details. 131da177e4SLinus Torvalds * 147b718769SNathan Scott * You should have received a copy of the GNU General Public License 157b718769SNathan Scott * along with this program; if not, write the Free Software Foundation, 167b718769SNathan Scott * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds #ifndef __XFS_LOG_PRIV_H__ 191da177e4SLinus Torvalds #define __XFS_LOG_PRIV_H__ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds struct xfs_buf; 221da177e4SLinus Torvalds struct log; 23a844f451SNathan Scott struct xlog_ticket; 241da177e4SLinus Torvalds struct xfs_mount; 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds /* 271da177e4SLinus Torvalds * Macros, structures, prototypes for internal log manager use. 281da177e4SLinus Torvalds */ 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #define XLOG_MIN_ICLOGS 2 311da177e4SLinus Torvalds #define XLOG_MAX_ICLOGS 8 321da177e4SLinus Torvalds #define XLOG_HEADER_MAGIC_NUM 0xFEEDbabe /* Invalid cycle number */ 331da177e4SLinus Torvalds #define XLOG_VERSION_1 1 341da177e4SLinus Torvalds #define XLOG_VERSION_2 2 /* Large IClogs, Log sunit */ 351da177e4SLinus Torvalds #define XLOG_VERSION_OKBITS (XLOG_VERSION_1 | XLOG_VERSION_2) 36dcb3b83fSEric Sandeen #define XLOG_MIN_RECORD_BSIZE (16*1024) /* eventually 32k */ 371da177e4SLinus Torvalds #define XLOG_BIG_RECORD_BSIZE (32*1024) /* 32k buffers */ 381da177e4SLinus Torvalds #define XLOG_MAX_RECORD_BSIZE (256*1024) 391da177e4SLinus Torvalds #define XLOG_HEADER_CYCLE_SIZE (32*1024) /* cycle data in header */ 40dcb3b83fSEric Sandeen #define XLOG_MIN_RECORD_BSHIFT 14 /* 16384 == 1 << 14 */ 411da177e4SLinus Torvalds #define XLOG_BIG_RECORD_BSHIFT 15 /* 32k == 1 << 15 */ 421da177e4SLinus Torvalds #define XLOG_MAX_RECORD_BSHIFT 18 /* 256k == 1 << 18 */ 431da177e4SLinus Torvalds #define XLOG_BTOLSUNIT(log, b) (((b)+(log)->l_mp->m_sb.sb_logsunit-1) / \ 441da177e4SLinus Torvalds (log)->l_mp->m_sb.sb_logsunit) 451da177e4SLinus Torvalds #define XLOG_LSUNITTOB(log, su) ((su) * (log)->l_mp->m_sb.sb_logsunit) 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #define XLOG_HEADER_SIZE 512 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #define XLOG_REC_SHIFT(log) \ 5062118709SEric Sandeen BTOBB(1 << (xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? \ 511da177e4SLinus Torvalds XLOG_MAX_RECORD_BSHIFT : XLOG_BIG_RECORD_BSHIFT)) 521da177e4SLinus Torvalds #define XLOG_TOTAL_REC_SHIFT(log) \ 5362118709SEric Sandeen BTOBB(XLOG_MAX_ICLOGS << (xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? \ 541da177e4SLinus Torvalds XLOG_MAX_RECORD_BSHIFT : XLOG_BIG_RECORD_BSHIFT)) 551da177e4SLinus Torvalds 5603bea6feSChristoph Hellwig static inline xfs_lsn_t xlog_assign_lsn(uint cycle, uint block) 5703bea6feSChristoph Hellwig { 5803bea6feSChristoph Hellwig return ((xfs_lsn_t)cycle << 32) | block; 591da177e4SLinus Torvalds } 6003bea6feSChristoph Hellwig 6103bea6feSChristoph Hellwig static inline uint xlog_get_cycle(char *ptr) 6203bea6feSChristoph Hellwig { 63b53e675dSChristoph Hellwig if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM) 64b53e675dSChristoph Hellwig return be32_to_cpu(*((__be32 *)ptr + 1)); 6503bea6feSChristoph Hellwig else 66b53e675dSChristoph Hellwig return be32_to_cpu(*(__be32 *)ptr); 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1) 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds #ifdef __KERNEL__ 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* 741da177e4SLinus Torvalds * get client id from packed copy. 751da177e4SLinus Torvalds * 761da177e4SLinus Torvalds * this hack is here because the xlog_pack code copies four bytes 771da177e4SLinus Torvalds * of xlog_op_header containing the fields oh_clientid, oh_flags 781da177e4SLinus Torvalds * and oh_res2 into the packed copy. 791da177e4SLinus Torvalds * 801da177e4SLinus Torvalds * later on this four byte chunk is treated as an int and the 811da177e4SLinus Torvalds * client id is pulled out. 821da177e4SLinus Torvalds * 831da177e4SLinus Torvalds * this has endian issues, of course. 841da177e4SLinus Torvalds */ 85b53e675dSChristoph Hellwig static inline uint xlog_get_client_id(__be32 i) 8603bea6feSChristoph Hellwig { 87b53e675dSChristoph Hellwig return be32_to_cpu(i) >> 24; 8803bea6feSChristoph Hellwig } 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds #define xlog_panic(args...) cmn_err(CE_PANIC, ## args) 911da177e4SLinus Torvalds #define xlog_exit(args...) cmn_err(CE_PANIC, ## args) 921da177e4SLinus Torvalds #define xlog_warn(args...) cmn_err(CE_WARN, ## args) 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds /* 951da177e4SLinus Torvalds * In core log state 961da177e4SLinus Torvalds */ 971da177e4SLinus Torvalds #define XLOG_STATE_ACTIVE 0x0001 /* Current IC log being written to */ 981da177e4SLinus Torvalds #define XLOG_STATE_WANT_SYNC 0x0002 /* Want to sync this iclog; no more writes */ 991da177e4SLinus Torvalds #define XLOG_STATE_SYNCING 0x0004 /* This IC log is syncing */ 1001da177e4SLinus Torvalds #define XLOG_STATE_DONE_SYNC 0x0008 /* Done syncing to disk */ 1011da177e4SLinus Torvalds #define XLOG_STATE_DO_CALLBACK \ 1021da177e4SLinus Torvalds 0x0010 /* Process callback functions */ 1031da177e4SLinus Torvalds #define XLOG_STATE_CALLBACK 0x0020 /* Callback functions now */ 1041da177e4SLinus Torvalds #define XLOG_STATE_DIRTY 0x0040 /* Dirty IC log, not ready for ACTIVE status*/ 1051da177e4SLinus Torvalds #define XLOG_STATE_IOERROR 0x0080 /* IO error happened in sync'ing log */ 1061da177e4SLinus Torvalds #define XLOG_STATE_ALL 0x7FFF /* All possible valid flags */ 1071da177e4SLinus Torvalds #define XLOG_STATE_NOTUSED 0x8000 /* This IC log not being used */ 1081da177e4SLinus Torvalds #endif /* __KERNEL__ */ 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* 1111da177e4SLinus Torvalds * Flags to log operation header 1121da177e4SLinus Torvalds * 1131da177e4SLinus Torvalds * The first write of a new transaction will be preceded with a start 1141da177e4SLinus Torvalds * record, XLOG_START_TRANS. Once a transaction is committed, a commit 1151da177e4SLinus Torvalds * record is written, XLOG_COMMIT_TRANS. If a single region can not fit into 1161da177e4SLinus Torvalds * the remainder of the current active in-core log, it is split up into 1171da177e4SLinus Torvalds * multiple regions. Each partial region will be marked with a 1181da177e4SLinus Torvalds * XLOG_CONTINUE_TRANS until the last one, which gets marked with XLOG_END_TRANS. 1191da177e4SLinus Torvalds * 1201da177e4SLinus Torvalds */ 1211da177e4SLinus Torvalds #define XLOG_START_TRANS 0x01 /* Start a new transaction */ 1221da177e4SLinus Torvalds #define XLOG_COMMIT_TRANS 0x02 /* Commit this transaction */ 1231da177e4SLinus Torvalds #define XLOG_CONTINUE_TRANS 0x04 /* Cont this trans into new region */ 1241da177e4SLinus Torvalds #define XLOG_WAS_CONT_TRANS 0x08 /* Cont this trans into new region */ 1251da177e4SLinus Torvalds #define XLOG_END_TRANS 0x10 /* End a continued transaction */ 1261da177e4SLinus Torvalds #define XLOG_UNMOUNT_TRANS 0x20 /* Unmount a filesystem transaction */ 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds #ifdef __KERNEL__ 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * Flags to log ticket 1311da177e4SLinus Torvalds */ 1321da177e4SLinus Torvalds #define XLOG_TIC_INITED 0x1 /* has been initialized */ 1331da177e4SLinus Torvalds #define XLOG_TIC_PERM_RESERV 0x2 /* permanent reservation */ 1340b1b213fSChristoph Hellwig 1350b1b213fSChristoph Hellwig #define XLOG_TIC_FLAGS \ 1360b1b213fSChristoph Hellwig { XLOG_TIC_INITED, "XLOG_TIC_INITED" }, \ 13710547941SDave Chinner { XLOG_TIC_PERM_RESERV, "XLOG_TIC_PERM_RESERV" } 1380b1b213fSChristoph Hellwig 1391da177e4SLinus Torvalds #endif /* __KERNEL__ */ 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds #define XLOG_UNMOUNT_TYPE 0x556e /* Un for Unmount */ 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds /* 1441da177e4SLinus Torvalds * Flags for log structure 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds #define XLOG_CHKSUM_MISMATCH 0x1 /* used only during recovery */ 1471da177e4SLinus Torvalds #define XLOG_ACTIVE_RECOVERY 0x2 /* in the middle of recovery */ 1481da177e4SLinus Torvalds #define XLOG_RECOVERY_NEEDED 0x4 /* log was recovered */ 1491da177e4SLinus Torvalds #define XLOG_IO_ERROR 0x8 /* log hit an I/O error, and being 1501da177e4SLinus Torvalds shutdown */ 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds #ifdef __KERNEL__ 1531da177e4SLinus Torvalds /* 1541da177e4SLinus Torvalds * Below are states for covering allocation transactions. 1551da177e4SLinus Torvalds * By covering, we mean changing the h_tail_lsn in the last on-disk 1561da177e4SLinus Torvalds * log write such that no allocation transactions will be re-done during 1571da177e4SLinus Torvalds * recovery after a system crash. Recovery starts at the last on-disk 1581da177e4SLinus Torvalds * log write. 1591da177e4SLinus Torvalds * 1601da177e4SLinus Torvalds * These states are used to insert dummy log entries to cover 1611da177e4SLinus Torvalds * space allocation transactions which can undo non-transactional changes 1621da177e4SLinus Torvalds * after a crash. Writes to a file with space 1631da177e4SLinus Torvalds * already allocated do not result in any transactions. Allocations 1641da177e4SLinus Torvalds * might include space beyond the EOF. So if we just push the EOF a 1651da177e4SLinus Torvalds * little, the last transaction for the file could contain the wrong 1661da177e4SLinus Torvalds * size. If there is no file system activity, after an allocation 1671da177e4SLinus Torvalds * transaction, and the system crashes, the allocation transaction 1681da177e4SLinus Torvalds * will get replayed and the file will be truncated. This could 1691da177e4SLinus Torvalds * be hours/days/... after the allocation occurred. 1701da177e4SLinus Torvalds * 1711da177e4SLinus Torvalds * The fix for this is to do two dummy transactions when the 1721da177e4SLinus Torvalds * system is idle. We need two dummy transaction because the h_tail_lsn 1731da177e4SLinus Torvalds * in the log record header needs to point beyond the last possible 1741da177e4SLinus Torvalds * non-dummy transaction. The first dummy changes the h_tail_lsn to 1751da177e4SLinus Torvalds * the first transaction before the dummy. The second dummy causes 1761da177e4SLinus Torvalds * h_tail_lsn to point to the first dummy. Recovery starts at h_tail_lsn. 1771da177e4SLinus Torvalds * 1781da177e4SLinus Torvalds * These dummy transactions get committed when everything 1791da177e4SLinus Torvalds * is idle (after there has been some activity). 1801da177e4SLinus Torvalds * 1811da177e4SLinus Torvalds * There are 5 states used to control this. 1821da177e4SLinus Torvalds * 1831da177e4SLinus Torvalds * IDLE -- no logging has been done on the file system or 1841da177e4SLinus Torvalds * we are done covering previous transactions. 1851da177e4SLinus Torvalds * NEED -- logging has occurred and we need a dummy transaction 1861da177e4SLinus Torvalds * when the log becomes idle. 1871da177e4SLinus Torvalds * DONE -- we were in the NEED state and have committed a dummy 1881da177e4SLinus Torvalds * transaction. 1891da177e4SLinus Torvalds * NEED2 -- we detected that a dummy transaction has gone to the 1901da177e4SLinus Torvalds * on disk log with no other transactions. 1911da177e4SLinus Torvalds * DONE2 -- we committed a dummy transaction when in the NEED2 state. 1921da177e4SLinus Torvalds * 1931da177e4SLinus Torvalds * There are two places where we switch states: 1941da177e4SLinus Torvalds * 1951da177e4SLinus Torvalds * 1.) In xfs_sync, when we detect an idle log and are in NEED or NEED2. 1961da177e4SLinus Torvalds * We commit the dummy transaction and switch to DONE or DONE2, 1971da177e4SLinus Torvalds * respectively. In all other states, we don't do anything. 1981da177e4SLinus Torvalds * 1991da177e4SLinus Torvalds * 2.) When we finish writing the on-disk log (xlog_state_clean_log). 2001da177e4SLinus Torvalds * 2011da177e4SLinus Torvalds * No matter what state we are in, if this isn't the dummy 2021da177e4SLinus Torvalds * transaction going out, the next state is NEED. 2031da177e4SLinus Torvalds * So, if we aren't in the DONE or DONE2 states, the next state 2041da177e4SLinus Torvalds * is NEED. We can't be finishing a write of the dummy record 2051da177e4SLinus Torvalds * unless it was committed and the state switched to DONE or DONE2. 2061da177e4SLinus Torvalds * 2071da177e4SLinus Torvalds * If we are in the DONE state and this was a write of the 2081da177e4SLinus Torvalds * dummy transaction, we move to NEED2. 2091da177e4SLinus Torvalds * 2101da177e4SLinus Torvalds * If we are in the DONE2 state and this was a write of the 2111da177e4SLinus Torvalds * dummy transaction, we move to IDLE. 2121da177e4SLinus Torvalds * 2131da177e4SLinus Torvalds * 2141da177e4SLinus Torvalds * Writing only one dummy transaction can get appended to 2151da177e4SLinus Torvalds * one file space allocation. When this happens, the log recovery 2161da177e4SLinus Torvalds * code replays the space allocation and a file could be truncated. 2171da177e4SLinus Torvalds * This is why we have the NEED2 and DONE2 states before going idle. 2181da177e4SLinus Torvalds */ 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds #define XLOG_STATE_COVER_IDLE 0 2211da177e4SLinus Torvalds #define XLOG_STATE_COVER_NEED 1 2221da177e4SLinus Torvalds #define XLOG_STATE_COVER_DONE 2 2231da177e4SLinus Torvalds #define XLOG_STATE_COVER_NEED2 3 2241da177e4SLinus Torvalds #define XLOG_STATE_COVER_DONE2 4 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds #define XLOG_COVER_OPS 5 2271da177e4SLinus Torvalds 2287e9c6396STim Shimmin 2297e9c6396STim Shimmin /* Ticket reservation region accounting */ 2307e9c6396STim Shimmin #define XLOG_TIC_LEN_MAX 15 2317e9c6396STim Shimmin 2327e9c6396STim Shimmin /* 2337e9c6396STim Shimmin * Reservation region 2347e9c6396STim Shimmin * As would be stored in xfs_log_iovec but without the i_addr which 2357e9c6396STim Shimmin * we don't care about. 2367e9c6396STim Shimmin */ 2377e9c6396STim Shimmin typedef struct xlog_res { 2381259845dSTim Shimmin uint r_len; /* region length :4 */ 2391259845dSTim Shimmin uint r_type; /* region's transaction type :4 */ 2407e9c6396STim Shimmin } xlog_res_t; 2417e9c6396STim Shimmin 2421da177e4SLinus Torvalds typedef struct xlog_ticket { 243eb40a875SDave Chinner wait_queue_head_t t_wait; /* ticket wait queue */ 24410547941SDave Chinner struct list_head t_queue; /* reserve/write queue */ 2451da177e4SLinus Torvalds xlog_tid_t t_tid; /* transaction identifier : 4 */ 246cc09c0dcSDave Chinner atomic_t t_ref; /* ticket reference count : 4 */ 2471da177e4SLinus Torvalds int t_curr_res; /* current reservation in bytes : 4 */ 2481da177e4SLinus Torvalds int t_unit_res; /* unit reservation in bytes : 4 */ 2497e9c6396STim Shimmin char t_ocnt; /* original count : 1 */ 2507e9c6396STim Shimmin char t_cnt; /* current count : 1 */ 2517e9c6396STim Shimmin char t_clientid; /* who does this belong to; : 1 */ 2527e9c6396STim Shimmin char t_flags; /* properties of reservation : 1 */ 2537e9c6396STim Shimmin uint t_trans_type; /* transaction type : 4 */ 2547e9c6396STim Shimmin 2557e9c6396STim Shimmin /* reservation array fields */ 2567e9c6396STim Shimmin uint t_res_num; /* num in array : 4 */ 2577e9c6396STim Shimmin uint t_res_num_ophdrs; /* num op hdrs : 4 */ 2587e9c6396STim Shimmin uint t_res_arr_sum; /* array sum : 4 */ 2597e9c6396STim Shimmin uint t_res_o_flow; /* sum overflow : 4 */ 2601259845dSTim Shimmin xlog_res_t t_res_arr[XLOG_TIC_LEN_MAX]; /* array of res : 8 * 15 */ 2611da177e4SLinus Torvalds } xlog_ticket_t; 2627e9c6396STim Shimmin 2631da177e4SLinus Torvalds #endif 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds typedef struct xlog_op_header { 26767fcb7bfSChristoph Hellwig __be32 oh_tid; /* transaction id of operation : 4 b */ 26867fcb7bfSChristoph Hellwig __be32 oh_len; /* bytes in data region : 4 b */ 26967fcb7bfSChristoph Hellwig __u8 oh_clientid; /* who sent me this : 1 b */ 27067fcb7bfSChristoph Hellwig __u8 oh_flags; /* : 1 b */ 27167fcb7bfSChristoph Hellwig __u16 oh_res2; /* 32 bit align : 2 b */ 2721da177e4SLinus Torvalds } xlog_op_header_t; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* valid values for h_fmt */ 2761da177e4SLinus Torvalds #define XLOG_FMT_UNKNOWN 0 2771da177e4SLinus Torvalds #define XLOG_FMT_LINUX_LE 1 2781da177e4SLinus Torvalds #define XLOG_FMT_LINUX_BE 2 2791da177e4SLinus Torvalds #define XLOG_FMT_IRIX_BE 3 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds /* our fmt */ 282f016bad6SNathan Scott #ifdef XFS_NATIVE_HOST 2831da177e4SLinus Torvalds #define XLOG_FMT XLOG_FMT_LINUX_BE 2841da177e4SLinus Torvalds #else 285f016bad6SNathan Scott #define XLOG_FMT XLOG_FMT_LINUX_LE 2861da177e4SLinus Torvalds #endif 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds typedef struct xlog_rec_header { 289b53e675dSChristoph Hellwig __be32 h_magicno; /* log record (LR) identifier : 4 */ 290b53e675dSChristoph Hellwig __be32 h_cycle; /* write cycle of log : 4 */ 291b53e675dSChristoph Hellwig __be32 h_version; /* LR version : 4 */ 292b53e675dSChristoph Hellwig __be32 h_len; /* len in bytes; should be 64-bit aligned: 4 */ 293b53e675dSChristoph Hellwig __be64 h_lsn; /* lsn of this LR : 8 */ 294b53e675dSChristoph Hellwig __be64 h_tail_lsn; /* lsn of 1st LR w/ buffers not committed: 8 */ 295b53e675dSChristoph Hellwig __be32 h_chksum; /* may not be used; non-zero if used : 4 */ 296b53e675dSChristoph Hellwig __be32 h_prev_block; /* block number to previous LR : 4 */ 297b53e675dSChristoph Hellwig __be32 h_num_logops; /* number of log operations in this LR : 4 */ 298b53e675dSChristoph Hellwig __be32 h_cycle_data[XLOG_HEADER_CYCLE_SIZE / BBSIZE]; 2991da177e4SLinus Torvalds /* new fields */ 300b53e675dSChristoph Hellwig __be32 h_fmt; /* format of log record : 4 */ 3011da177e4SLinus Torvalds uuid_t h_fs_uuid; /* uuid of FS : 16 */ 302b53e675dSChristoph Hellwig __be32 h_size; /* iclog size : 4 */ 3031da177e4SLinus Torvalds } xlog_rec_header_t; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds typedef struct xlog_rec_ext_header { 306b53e675dSChristoph Hellwig __be32 xh_cycle; /* write cycle of log : 4 */ 307b53e675dSChristoph Hellwig __be32 xh_cycle_data[XLOG_HEADER_CYCLE_SIZE / BBSIZE]; /* : 256 */ 3081da177e4SLinus Torvalds } xlog_rec_ext_header_t; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds #ifdef __KERNEL__ 311b28708d6SChristoph Hellwig 312b28708d6SChristoph Hellwig /* 313b28708d6SChristoph Hellwig * Quite misnamed, because this union lays out the actual on-disk log buffer. 314b28708d6SChristoph Hellwig */ 315b28708d6SChristoph Hellwig typedef union xlog_in_core2 { 316b28708d6SChristoph Hellwig xlog_rec_header_t hic_header; 317b28708d6SChristoph Hellwig xlog_rec_ext_header_t hic_xheader; 318b28708d6SChristoph Hellwig char hic_sector[XLOG_HEADER_SIZE]; 319b28708d6SChristoph Hellwig } xlog_in_core_2_t; 320b28708d6SChristoph Hellwig 3211da177e4SLinus Torvalds /* 3221da177e4SLinus Torvalds * - A log record header is 512 bytes. There is plenty of room to grow the 3231da177e4SLinus Torvalds * xlog_rec_header_t into the reserved space. 3241da177e4SLinus Torvalds * - ic_data follows, so a write to disk can start at the beginning of 3251da177e4SLinus Torvalds * the iclog. 32612017fafSDavid Chinner * - ic_forcewait is used to implement synchronous forcing of the iclog to disk. 3271da177e4SLinus Torvalds * - ic_next is the pointer to the next iclog in the ring. 3281da177e4SLinus Torvalds * - ic_bp is a pointer to the buffer used to write this incore log to disk. 3291da177e4SLinus Torvalds * - ic_log is a pointer back to the global log structure. 3301da177e4SLinus Torvalds * - ic_callback is a linked list of callback function/argument pairs to be 3311da177e4SLinus Torvalds * called after an iclog finishes writing. 3321da177e4SLinus Torvalds * - ic_size is the full size of the header plus data. 3331da177e4SLinus Torvalds * - ic_offset is the current number of bytes written to in this iclog. 3341da177e4SLinus Torvalds * - ic_refcnt is bumped when someone is writing to the log. 3351da177e4SLinus Torvalds * - ic_state is the state of the iclog. 336114d23aaSDavid Chinner * 337114d23aaSDavid Chinner * Because of cacheline contention on large machines, we need to separate 338114d23aaSDavid Chinner * various resources onto different cachelines. To start with, make the 339114d23aaSDavid Chinner * structure cacheline aligned. The following fields can be contended on 340114d23aaSDavid Chinner * by independent processes: 341114d23aaSDavid Chinner * 342114d23aaSDavid Chinner * - ic_callback_* 343114d23aaSDavid Chinner * - ic_refcnt 344114d23aaSDavid Chinner * - fields protected by the global l_icloglock 345114d23aaSDavid Chinner * 346114d23aaSDavid Chinner * so we need to ensure that these fields are located in separate cachelines. 347114d23aaSDavid Chinner * We'll put all the read-only and l_icloglock fields in the first cacheline, 348114d23aaSDavid Chinner * and move everything else out to subsequent cachelines. 3491da177e4SLinus Torvalds */ 350b28708d6SChristoph Hellwig typedef struct xlog_in_core { 351eb40a875SDave Chinner wait_queue_head_t ic_force_wait; 352eb40a875SDave Chinner wait_queue_head_t ic_write_wait; 3531da177e4SLinus Torvalds struct xlog_in_core *ic_next; 3541da177e4SLinus Torvalds struct xlog_in_core *ic_prev; 3551da177e4SLinus Torvalds struct xfs_buf *ic_bp; 3561da177e4SLinus Torvalds struct log *ic_log; 3571da177e4SLinus Torvalds int ic_size; 3581da177e4SLinus Torvalds int ic_offset; 3591da177e4SLinus Torvalds int ic_bwritecnt; 360a5687787SChristoph Hellwig unsigned short ic_state; 3611da177e4SLinus Torvalds char *ic_datap; /* pointer to iclog data */ 362114d23aaSDavid Chinner 363114d23aaSDavid Chinner /* Callback structures need their own cacheline */ 364114d23aaSDavid Chinner spinlock_t ic_callback_lock ____cacheline_aligned_in_smp; 365114d23aaSDavid Chinner xfs_log_callback_t *ic_callback; 366114d23aaSDavid Chinner xfs_log_callback_t **ic_callback_tail; 367114d23aaSDavid Chinner 368114d23aaSDavid Chinner /* reference counts need their own cacheline */ 369114d23aaSDavid Chinner atomic_t ic_refcnt ____cacheline_aligned_in_smp; 370b28708d6SChristoph Hellwig xlog_in_core_2_t *ic_data; 371b28708d6SChristoph Hellwig #define ic_header ic_data->hic_header 3721da177e4SLinus Torvalds } xlog_in_core_t; 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds /* 37571e330b5SDave Chinner * The CIL context is used to aggregate per-transaction details as well be 37671e330b5SDave Chinner * passed to the iclog for checkpoint post-commit processing. After being 37771e330b5SDave Chinner * passed to the iclog, another context needs to be allocated for tracking the 37871e330b5SDave Chinner * next set of transactions to be aggregated into a checkpoint. 37971e330b5SDave Chinner */ 38071e330b5SDave Chinner struct xfs_cil; 38171e330b5SDave Chinner 38271e330b5SDave Chinner struct xfs_cil_ctx { 38371e330b5SDave Chinner struct xfs_cil *cil; 38471e330b5SDave Chinner xfs_lsn_t sequence; /* chkpt sequence # */ 38571e330b5SDave Chinner xfs_lsn_t start_lsn; /* first LSN of chkpt commit */ 38671e330b5SDave Chinner xfs_lsn_t commit_lsn; /* chkpt commit record lsn */ 38771e330b5SDave Chinner struct xlog_ticket *ticket; /* chkpt ticket */ 38871e330b5SDave Chinner int nvecs; /* number of regions */ 38971e330b5SDave Chinner int space_used; /* aggregate size of regions */ 39071e330b5SDave Chinner struct list_head busy_extents; /* busy extents in chkpt */ 39171e330b5SDave Chinner struct xfs_log_vec *lv_chain; /* logvecs being pushed */ 39271e330b5SDave Chinner xfs_log_callback_t log_cb; /* completion callback hook. */ 39371e330b5SDave Chinner struct list_head committing; /* ctx committing list */ 39471e330b5SDave Chinner }; 39571e330b5SDave Chinner 39671e330b5SDave Chinner /* 39771e330b5SDave Chinner * Committed Item List structure 39871e330b5SDave Chinner * 39971e330b5SDave Chinner * This structure is used to track log items that have been committed but not 40071e330b5SDave Chinner * yet written into the log. It is used only when the delayed logging mount 40171e330b5SDave Chinner * option is enabled. 40271e330b5SDave Chinner * 40371e330b5SDave Chinner * This structure tracks the list of committing checkpoint contexts so 40471e330b5SDave Chinner * we can avoid the problem of having to hold out new transactions during a 40571e330b5SDave Chinner * flush until we have a the commit record LSN of the checkpoint. We can 40671e330b5SDave Chinner * traverse the list of committing contexts in xlog_cil_push_lsn() to find a 40771e330b5SDave Chinner * sequence match and extract the commit LSN directly from there. If the 40871e330b5SDave Chinner * checkpoint is still in the process of committing, we can block waiting for 40971e330b5SDave Chinner * the commit LSN to be determined as well. This should make synchronous 41071e330b5SDave Chinner * operations almost as efficient as the old logging methods. 41171e330b5SDave Chinner */ 41271e330b5SDave Chinner struct xfs_cil { 41371e330b5SDave Chinner struct log *xc_log; 41471e330b5SDave Chinner struct list_head xc_cil; 41571e330b5SDave Chinner spinlock_t xc_cil_lock; 41671e330b5SDave Chinner struct xfs_cil_ctx *xc_ctx; 41771e330b5SDave Chinner struct rw_semaphore xc_ctx_lock; 41871e330b5SDave Chinner struct list_head xc_committing; 419eb40a875SDave Chinner wait_queue_head_t xc_commit_wait; 420a44f13edSDave Chinner xfs_lsn_t xc_current_sequence; 42171e330b5SDave Chinner }; 42271e330b5SDave Chinner 42371e330b5SDave Chinner /* 42480168676SDave Chinner * The amount of log space we allow the CIL to aggregate is difficult to size. 42580168676SDave Chinner * Whatever we choose, we have to make sure we can get a reservation for the 42680168676SDave Chinner * log space effectively, that it is large enough to capture sufficient 42780168676SDave Chinner * relogging to reduce log buffer IO significantly, but it is not too large for 42880168676SDave Chinner * the log or induces too much latency when writing out through the iclogs. We 42980168676SDave Chinner * track both space consumed and the number of vectors in the checkpoint 43080168676SDave Chinner * context, so we need to decide which to use for limiting. 431df806158SDave Chinner * 432df806158SDave Chinner * Every log buffer we write out during a push needs a header reserved, which 433df806158SDave Chinner * is at least one sector and more for v2 logs. Hence we need a reservation of 434df806158SDave Chinner * at least 512 bytes per 32k of log space just for the LR headers. That means 435df806158SDave Chinner * 16KB of reservation per megabyte of delayed logging space we will consume, 436df806158SDave Chinner * plus various headers. The number of headers will vary based on the num of 437df806158SDave Chinner * io vectors, so limiting on a specific number of vectors is going to result 438df806158SDave Chinner * in transactions of varying size. IOWs, it is more consistent to track and 439df806158SDave Chinner * limit space consumed in the log rather than by the number of objects being 440df806158SDave Chinner * logged in order to prevent checkpoint ticket overruns. 441df806158SDave Chinner * 442df806158SDave Chinner * Further, use of static reservations through the log grant mechanism is 443df806158SDave Chinner * problematic. It introduces a lot of complexity (e.g. reserve grant vs write 444df806158SDave Chinner * grant) and a significant deadlock potential because regranting write space 445df806158SDave Chinner * can block on log pushes. Hence if we have to regrant log space during a log 446df806158SDave Chinner * push, we can deadlock. 447df806158SDave Chinner * 448df806158SDave Chinner * However, we can avoid this by use of a dynamic "reservation stealing" 449df806158SDave Chinner * technique during transaction commit whereby unused reservation space in the 450df806158SDave Chinner * transaction ticket is transferred to the CIL ctx commit ticket to cover the 451df806158SDave Chinner * space needed by the checkpoint transaction. This means that we never need to 452df806158SDave Chinner * specifically reserve space for the CIL checkpoint transaction, nor do we 453df806158SDave Chinner * need to regrant space once the checkpoint completes. This also means the 454df806158SDave Chinner * checkpoint transaction ticket is specific to the checkpoint context, rather 455df806158SDave Chinner * than the CIL itself. 456df806158SDave Chinner * 45780168676SDave Chinner * With dynamic reservations, we can effectively make up arbitrary limits for 45880168676SDave Chinner * the checkpoint size so long as they don't violate any other size rules. 45980168676SDave Chinner * Recovery imposes a rule that no transaction exceed half the log, so we are 46080168676SDave Chinner * limited by that. Furthermore, the log transaction reservation subsystem 46180168676SDave Chinner * tries to keep 25% of the log free, so we need to keep below that limit or we 46280168676SDave Chinner * risk running out of free log space to start any new transactions. 46380168676SDave Chinner * 46480168676SDave Chinner * In order to keep background CIL push efficient, we will set a lower 46580168676SDave Chinner * threshold at which background pushing is attempted without blocking current 46680168676SDave Chinner * transaction commits. A separate, higher bound defines when CIL pushes are 46780168676SDave Chinner * enforced to ensure we stay within our maximum checkpoint size bounds. 46880168676SDave Chinner * threshold, yet give us plenty of space for aggregation on large logs. 469df806158SDave Chinner */ 47080168676SDave Chinner #define XLOG_CIL_SPACE_LIMIT(log) (log->l_logsize >> 3) 47180168676SDave Chinner #define XLOG_CIL_HARD_SPACE_LIMIT(log) (3 * (log->l_logsize >> 4)) 472df806158SDave Chinner 473df806158SDave Chinner /* 4741da177e4SLinus Torvalds * The reservation head lsn is not made up of a cycle number and block number. 4751da177e4SLinus Torvalds * Instead, it uses a cycle number and byte number. Logs don't expect to 4761da177e4SLinus Torvalds * overflow 31 bits worth of byte offset, so using a byte number will mean 4771da177e4SLinus Torvalds * that round off problems won't occur when releasing partial reservations. 4781da177e4SLinus Torvalds */ 4791da177e4SLinus Torvalds typedef struct log { 4804679b2d3SDavid Chinner /* The following fields don't need locking */ 4814679b2d3SDavid Chinner struct xfs_mount *l_mp; /* mount point */ 482a9c21c1bSDavid Chinner struct xfs_ail *l_ailp; /* AIL log is working with */ 48371e330b5SDave Chinner struct xfs_cil *l_cilp; /* CIL log is working with */ 4844679b2d3SDavid Chinner struct xfs_buf *l_xbuf; /* extra buffer for log 4854679b2d3SDavid Chinner * wrapping */ 4864679b2d3SDavid Chinner struct xfs_buftarg *l_targ; /* buftarg of log */ 4874679b2d3SDavid Chinner uint l_flags; 4884679b2d3SDavid Chinner uint l_quotaoffs_flag; /* XFS_DQ_*, for QUOTAOFFs */ 489d5689eaaSChristoph Hellwig struct list_head *l_buf_cancel_table; 4904679b2d3SDavid Chinner int l_iclog_hsize; /* size of iclog header */ 4914679b2d3SDavid Chinner int l_iclog_heads; /* # of iclog header sectors */ 49248389ef1SAlex Elder uint l_sectBBsize; /* sector size in BBs (2^n) */ 4934679b2d3SDavid Chinner int l_iclog_size; /* size of log in bytes */ 4944679b2d3SDavid Chinner int l_iclog_size_log; /* log power size of log */ 4954679b2d3SDavid Chinner int l_iclog_bufs; /* number of iclog buffers */ 4964679b2d3SDavid Chinner xfs_daddr_t l_logBBstart; /* start block of log */ 4974679b2d3SDavid Chinner int l_logsize; /* size of log in bytes */ 4984679b2d3SDavid Chinner int l_logBBsize; /* size of log in BB chunks */ 4994679b2d3SDavid Chinner 5001da177e4SLinus Torvalds /* The following block of fields are changed while holding icloglock */ 501eb40a875SDave Chinner wait_queue_head_t l_flush_wait ____cacheline_aligned_in_smp; 502d748c623SMatthew Wilcox /* waiting for iclog flush */ 5031da177e4SLinus Torvalds int l_covered_state;/* state of "covering disk 5041da177e4SLinus Torvalds * log entries" */ 5051da177e4SLinus Torvalds xlog_in_core_t *l_iclog; /* head log queue */ 506b22cd72cSEric Sandeen spinlock_t l_icloglock; /* grab to change iclog state */ 5071da177e4SLinus Torvalds int l_curr_cycle; /* Cycle number of log writes */ 5081da177e4SLinus Torvalds int l_prev_cycle; /* Cycle number before last 5091da177e4SLinus Torvalds * block increment */ 5101da177e4SLinus Torvalds int l_curr_block; /* current logical log block */ 5111da177e4SLinus Torvalds int l_prev_block; /* previous logical log block */ 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds /* The following block of fields are changed while holding grant_lock */ 5144679b2d3SDavid Chinner spinlock_t l_grant_lock ____cacheline_aligned_in_smp; 51510547941SDave Chinner struct list_head l_reserveq; 51610547941SDave Chinner struct list_head l_writeq; 517a69ed03cSDave Chinner int64_t l_grant_reserve_head; 518a69ed03cSDave Chinner int64_t l_grant_write_head; 5191da177e4SLinus Torvalds 52084f3c683SDave Chinner /* 521*1c3cb9ecSDave Chinner * l_last_sync_lsn and l_tail_lsn are atomics so they can be set and 522*1c3cb9ecSDave Chinner * read without needing to hold specific locks. To avoid operations 523*1c3cb9ecSDave Chinner * contending with other hot objects, place each of them on a separate 524*1c3cb9ecSDave Chinner * cacheline. 52584f3c683SDave Chinner */ 52684f3c683SDave Chinner /* lsn of last LR on disk */ 52784f3c683SDave Chinner atomic64_t l_last_sync_lsn ____cacheline_aligned_in_smp; 528*1c3cb9ecSDave Chinner /* lsn of 1st LR with unflushed * buffers */ 529*1c3cb9ecSDave Chinner atomic64_t l_tail_lsn ____cacheline_aligned_in_smp; 53084f3c683SDave Chinner 5314679b2d3SDavid Chinner /* The following field are used for debugging; need to hold icloglock */ 5324679b2d3SDavid Chinner #ifdef DEBUG 5334679b2d3SDavid Chinner char *l_iclog_bak[XLOG_MAX_ICLOGS]; 5344679b2d3SDavid Chinner #endif 5354679b2d3SDavid Chinner 5361da177e4SLinus Torvalds } xlog_t; 5371da177e4SLinus Torvalds 538d5689eaaSChristoph Hellwig #define XLOG_BUF_CANCEL_BUCKET(log, blkno) \ 539d5689eaaSChristoph Hellwig ((log)->l_buf_cancel_table + ((__uint64_t)blkno % XLOG_BC_TABLE_SIZE)) 540d5689eaaSChristoph Hellwig 541cfcbbbd0SNathan Scott #define XLOG_FORCED_SHUTDOWN(log) ((log)->l_flags & XLOG_IO_ERROR) 542cfcbbbd0SNathan Scott 5431da177e4SLinus Torvalds /* common routines */ 5441da177e4SLinus Torvalds extern xfs_lsn_t xlog_assign_tail_lsn(struct xfs_mount *mp); 54565be6054SEric Sandeen extern int xlog_recover(xlog_t *log); 5464249023aSChristoph Hellwig extern int xlog_recover_finish(xlog_t *log); 5471da177e4SLinus Torvalds extern void xlog_pack_data(xlog_t *log, xlog_in_core_t *iclog, int); 5481da177e4SLinus Torvalds 549eb01c9cdSDavid Chinner extern kmem_zone_t *xfs_log_ticket_zone; 55071e330b5SDave Chinner struct xlog_ticket *xlog_ticket_alloc(struct log *log, int unit_bytes, 55171e330b5SDave Chinner int count, char client, uint xflags, 55271e330b5SDave Chinner int alloc_flags); 55371e330b5SDave Chinner 554eb01c9cdSDavid Chinner 555e6b1f273SChristoph Hellwig static inline void 556e6b1f273SChristoph Hellwig xlog_write_adv_cnt(void **ptr, int *len, int *off, size_t bytes) 557e6b1f273SChristoph Hellwig { 558e6b1f273SChristoph Hellwig *ptr += bytes; 559e6b1f273SChristoph Hellwig *len -= bytes; 560e6b1f273SChristoph Hellwig *off += bytes; 561e6b1f273SChristoph Hellwig } 562e6b1f273SChristoph Hellwig 56371e330b5SDave Chinner void xlog_print_tic_res(struct xfs_mount *mp, struct xlog_ticket *ticket); 56471e330b5SDave Chinner int xlog_write(struct log *log, struct xfs_log_vec *log_vector, 56571e330b5SDave Chinner struct xlog_ticket *tic, xfs_lsn_t *start_lsn, 56671e330b5SDave Chinner xlog_in_core_t **commit_iclog, uint flags); 56771e330b5SDave Chinner 56871e330b5SDave Chinner /* 569*1c3cb9ecSDave Chinner * When we crack an atomic LSN, we sample it first so that the value will not 570*1c3cb9ecSDave Chinner * change while we are cracking it into the component values. This means we 571*1c3cb9ecSDave Chinner * will always get consistent component values to work from. This should always 572*1c3cb9ecSDave Chinner * be used to smaple and crack LSNs taht are stored and updated in atomic 573*1c3cb9ecSDave Chinner * variables. 574*1c3cb9ecSDave Chinner */ 575*1c3cb9ecSDave Chinner static inline void 576*1c3cb9ecSDave Chinner xlog_crack_atomic_lsn(atomic64_t *lsn, uint *cycle, uint *block) 577*1c3cb9ecSDave Chinner { 578*1c3cb9ecSDave Chinner xfs_lsn_t val = atomic64_read(lsn); 579*1c3cb9ecSDave Chinner 580*1c3cb9ecSDave Chinner *cycle = CYCLE_LSN(val); 581*1c3cb9ecSDave Chinner *block = BLOCK_LSN(val); 582*1c3cb9ecSDave Chinner } 583*1c3cb9ecSDave Chinner 584*1c3cb9ecSDave Chinner /* 585*1c3cb9ecSDave Chinner * Calculate and assign a value to an atomic LSN variable from component pieces. 586*1c3cb9ecSDave Chinner */ 587*1c3cb9ecSDave Chinner static inline void 588*1c3cb9ecSDave Chinner xlog_assign_atomic_lsn(atomic64_t *lsn, uint cycle, uint block) 589*1c3cb9ecSDave Chinner { 590*1c3cb9ecSDave Chinner atomic64_set(lsn, xlog_assign_lsn(cycle, block)); 591*1c3cb9ecSDave Chinner } 592*1c3cb9ecSDave Chinner 593*1c3cb9ecSDave Chinner /* 594a69ed03cSDave Chinner * When we crack the grrant head, we sample it first so that the value will not 595a69ed03cSDave Chinner * change while we are cracking it into the component values. This means we 596a69ed03cSDave Chinner * will always get consistent component values to work from. 597a69ed03cSDave Chinner */ 598a69ed03cSDave Chinner static inline void 599a69ed03cSDave Chinner xlog_crack_grant_head(int64_t *head, int *cycle, int *space) 600a69ed03cSDave Chinner { 601a69ed03cSDave Chinner int64_t val = *head; 602a69ed03cSDave Chinner 603a69ed03cSDave Chinner *cycle = val >> 32; 604a69ed03cSDave Chinner *space = val & 0xffffffff; 605a69ed03cSDave Chinner } 606a69ed03cSDave Chinner 607a69ed03cSDave Chinner static inline void 608a69ed03cSDave Chinner xlog_assign_grant_head(int64_t *head, int cycle, int space) 609a69ed03cSDave Chinner { 610a69ed03cSDave Chinner *head = ((int64_t)cycle << 32) | space; 611a69ed03cSDave Chinner } 612a69ed03cSDave Chinner 613a69ed03cSDave Chinner /* 61471e330b5SDave Chinner * Committed Item List interfaces 61571e330b5SDave Chinner */ 61671e330b5SDave Chinner int xlog_cil_init(struct log *log); 61771e330b5SDave Chinner void xlog_cil_init_post_recovery(struct log *log); 61871e330b5SDave Chinner void xlog_cil_destroy(struct log *log); 61971e330b5SDave Chinner 620a44f13edSDave Chinner /* 621a44f13edSDave Chinner * CIL force routines 622a44f13edSDave Chinner */ 623a44f13edSDave Chinner xfs_lsn_t xlog_cil_force_lsn(struct log *log, xfs_lsn_t sequence); 624a44f13edSDave Chinner 625a44f13edSDave Chinner static inline void 626a44f13edSDave Chinner xlog_cil_force(struct log *log) 627a44f13edSDave Chinner { 628a44f13edSDave Chinner xlog_cil_force_lsn(log, log->l_cilp->xc_current_sequence); 629a44f13edSDave Chinner } 63071e330b5SDave Chinner 631955e47adSTim Shimmin /* 632955e47adSTim Shimmin * Unmount record type is used as a pseudo transaction type for the ticket. 633955e47adSTim Shimmin * It's value must be outside the range of XFS_TRANS_* values. 634955e47adSTim Shimmin */ 635955e47adSTim Shimmin #define XLOG_UNMOUNT_REC_TYPE (-1U) 636955e47adSTim Shimmin 637eb40a875SDave Chinner /* 638eb40a875SDave Chinner * Wrapper function for waiting on a wait queue serialised against wakeups 639eb40a875SDave Chinner * by a spinlock. This matches the semantics of all the wait queues used in the 640eb40a875SDave Chinner * log code. 641eb40a875SDave Chinner */ 642eb40a875SDave Chinner static inline void xlog_wait(wait_queue_head_t *wq, spinlock_t *lock) 643eb40a875SDave Chinner { 644eb40a875SDave Chinner DECLARE_WAITQUEUE(wait, current); 645eb40a875SDave Chinner 646eb40a875SDave Chinner add_wait_queue_exclusive(wq, &wait); 647eb40a875SDave Chinner __set_current_state(TASK_UNINTERRUPTIBLE); 648eb40a875SDave Chinner spin_unlock(lock); 649eb40a875SDave Chinner schedule(); 650eb40a875SDave Chinner remove_wait_queue(wq, &wait); 651eb40a875SDave Chinner } 6521da177e4SLinus Torvalds #endif /* __KERNEL__ */ 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds #endif /* __XFS_LOG_PRIV_H__ */ 655