Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_dir2.h" |
| 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 37 | #include "xfs_btree.h" |
| 38 | #include "xfs_ialloc.h" |
| 39 | #include "xfs_trans_priv.h" |
| 40 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #ifdef XFS_TRANS_DEBUG |
| 43 | STATIC void |
| 44 | xfs_trans_inode_broot_debug( |
| 45 | xfs_inode_t *ip); |
| 46 | #else |
| 47 | #define xfs_trans_inode_broot_debug(ip) |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | /* |
| 52 | * Get and lock the inode for the caller if it is not already |
| 53 | * locked within the given transaction. If it is already locked |
| 54 | * within the transaction, just increment its lock recursion count |
| 55 | * and return a pointer to it. |
| 56 | * |
| 57 | * For an inode to be locked in a transaction, the inode lock, as |
| 58 | * opposed to the io lock, must be taken exclusively. This ensures |
| 59 | * that the inode can be involved in only 1 transaction at a time. |
| 60 | * Lock recursion is handled on the io lock, but only for lock modes |
| 61 | * of equal or lesser strength. That is, you can recur on the io lock |
| 62 | * held EXCL with a SHARED request but not vice versa. Also, if |
| 63 | * the inode is already a part of the transaction then you cannot |
| 64 | * go from not holding the io lock to having it EXCL or SHARED. |
| 65 | * |
| 66 | * Use the inode cache routine xfs_inode_incore() to find the inode |
| 67 | * if it is already owned by this transaction. |
| 68 | * |
| 69 | * If we don't already own the inode, use xfs_iget() to get it. |
| 70 | * Since the inode log item structure is embedded in the incore |
| 71 | * inode structure and is initialized when the inode is brought |
| 72 | * into memory, there is nothing to do with it here. |
| 73 | * |
| 74 | * If the given transaction pointer is NULL, just call xfs_iget(). |
| 75 | * This simplifies code which must handle both cases. |
| 76 | */ |
| 77 | int |
| 78 | xfs_trans_iget( |
| 79 | xfs_mount_t *mp, |
| 80 | xfs_trans_t *tp, |
| 81 | xfs_ino_t ino, |
| 82 | uint flags, |
| 83 | uint lock_flags, |
| 84 | xfs_inode_t **ipp) |
| 85 | { |
| 86 | int error; |
| 87 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * If the transaction pointer is NULL, just call the normal |
| 91 | * xfs_iget(). |
| 92 | */ |
| 93 | if (tp == NULL) |
| 94 | return xfs_iget(mp, NULL, ino, flags, lock_flags, ipp, 0); |
| 95 | |
| 96 | /* |
| 97 | * If we find the inode in core with this transaction |
| 98 | * pointer in its i_transp field, then we know we already |
| 99 | * have it locked. In this case we just increment the lock |
| 100 | * recursion count and return the inode to the caller. |
| 101 | * Assert that the inode is already locked in the mode requested |
| 102 | * by the caller. We cannot do lock promotions yet, so |
| 103 | * die if someone gets this wrong. |
| 104 | */ |
| 105 | if ((ip = xfs_inode_incore(tp->t_mountp, ino, tp)) != NULL) { |
| 106 | /* |
| 107 | * Make sure that the inode lock is held EXCL and |
| 108 | * that the io lock is never upgraded when the inode |
| 109 | * is already a part of the transaction. |
| 110 | */ |
| 111 | ASSERT(ip->i_itemp != NULL); |
| 112 | ASSERT(lock_flags & XFS_ILOCK_EXCL); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 113 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) || |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 115 | xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) || |
| 117 | (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_EXCL)); |
| 118 | ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) || |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 119 | xfs_isilocked(ip, XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) || |
| 121 | (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_ANY)); |
| 122 | |
| 123 | if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) { |
| 124 | ip->i_itemp->ili_iolock_recur++; |
| 125 | } |
| 126 | if (lock_flags & XFS_ILOCK_EXCL) { |
| 127 | ip->i_itemp->ili_ilock_recur++; |
| 128 | } |
| 129 | *ipp = ip; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | ASSERT(lock_flags & XFS_ILOCK_EXCL); |
| 134 | error = xfs_iget(tp->t_mountp, tp, ino, flags, lock_flags, &ip, 0); |
| 135 | if (error) { |
| 136 | return error; |
| 137 | } |
| 138 | ASSERT(ip != NULL); |
| 139 | |
Christoph Hellwig | 070c461 | 2008-12-03 12:20:35 +0100 | [diff] [blame] | 140 | xfs_trans_ijoin(tp, ip, lock_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | *ipp = ip; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Add the locked inode to the transaction. |
| 147 | * The inode must be locked, and it cannot be associated with any |
| 148 | * transaction. The caller must specify the locks already held |
| 149 | * on the inode. |
| 150 | */ |
| 151 | void |
| 152 | xfs_trans_ijoin( |
| 153 | xfs_trans_t *tp, |
| 154 | xfs_inode_t *ip, |
| 155 | uint lock_flags) |
| 156 | { |
| 157 | xfs_inode_log_item_t *iip; |
| 158 | |
| 159 | ASSERT(ip->i_transp == NULL); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 160 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | ASSERT(lock_flags & XFS_ILOCK_EXCL); |
| 162 | if (ip->i_itemp == NULL) |
| 163 | xfs_inode_item_init(ip, ip->i_mount); |
| 164 | iip = ip->i_itemp; |
| 165 | ASSERT(iip->ili_flags == 0); |
| 166 | ASSERT(iip->ili_ilock_recur == 0); |
| 167 | ASSERT(iip->ili_iolock_recur == 0); |
| 168 | |
| 169 | /* |
| 170 | * Get a log_item_desc to point at the new item. |
| 171 | */ |
| 172 | (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(iip)); |
| 173 | |
| 174 | xfs_trans_inode_broot_debug(ip); |
| 175 | |
| 176 | /* |
| 177 | * If the IO lock is already held, mark that in the inode log item. |
| 178 | */ |
| 179 | if (lock_flags & XFS_IOLOCK_EXCL) { |
| 180 | iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL; |
| 181 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
| 182 | iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Initialize i_transp so we can find it with xfs_inode_incore() |
| 187 | * in xfs_trans_iget() above. |
| 188 | */ |
| 189 | ip->i_transp = tp; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | |
| 194 | /* |
| 195 | * Mark the inode as not needing to be unlocked when the inode item's |
| 196 | * IOP_UNLOCK() routine is called. The inode must already be locked |
| 197 | * and associated with the given transaction. |
| 198 | */ |
| 199 | /*ARGSUSED*/ |
| 200 | void |
| 201 | xfs_trans_ihold( |
| 202 | xfs_trans_t *tp, |
| 203 | xfs_inode_t *ip) |
| 204 | { |
| 205 | ASSERT(ip->i_transp == tp); |
| 206 | ASSERT(ip->i_itemp != NULL); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 207 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | ip->i_itemp->ili_flags |= XFS_ILI_HOLD; |
| 210 | } |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * This is called to mark the fields indicated in fieldmask as needing |
| 215 | * to be logged when the transaction is committed. The inode must |
| 216 | * already be associated with the given transaction. |
| 217 | * |
| 218 | * The values for fieldmask are defined in xfs_inode_item.h. We always |
| 219 | * log all of the core inode if any of it has changed, and we always log |
| 220 | * all of the inline data/extents/b-tree root if any of them has changed. |
| 221 | */ |
| 222 | void |
| 223 | xfs_trans_log_inode( |
| 224 | xfs_trans_t *tp, |
| 225 | xfs_inode_t *ip, |
| 226 | uint flags) |
| 227 | { |
| 228 | xfs_log_item_desc_t *lidp; |
| 229 | |
| 230 | ASSERT(ip->i_transp == tp); |
| 231 | ASSERT(ip->i_itemp != NULL); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 232 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(ip->i_itemp)); |
| 235 | ASSERT(lidp != NULL); |
| 236 | |
| 237 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 238 | lidp->lid_flags |= XFS_LID_DIRTY; |
| 239 | |
| 240 | /* |
| 241 | * Always OR in the bits from the ili_last_fields field. |
| 242 | * This is to coordinate with the xfs_iflush() and xfs_iflush_done() |
| 243 | * routines in the eventual clearing of the ilf_fields bits. |
| 244 | * See the big comment in xfs_iflush() for an explanation of |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 245 | * this coordination mechanism. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | */ |
| 247 | flags |= ip->i_itemp->ili_last_fields; |
| 248 | ip->i_itemp->ili_format.ilf_fields |= flags; |
| 249 | } |
| 250 | |
| 251 | #ifdef XFS_TRANS_DEBUG |
| 252 | /* |
| 253 | * Keep track of the state of the inode btree root to make sure we |
| 254 | * log it properly. |
| 255 | */ |
| 256 | STATIC void |
| 257 | xfs_trans_inode_broot_debug( |
| 258 | xfs_inode_t *ip) |
| 259 | { |
| 260 | xfs_inode_log_item_t *iip; |
| 261 | |
| 262 | ASSERT(ip->i_itemp != NULL); |
| 263 | iip = ip->i_itemp; |
| 264 | if (iip->ili_root_size != 0) { |
| 265 | ASSERT(iip->ili_orig_root != NULL); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 266 | kmem_free(iip->ili_orig_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | iip->ili_root_size = 0; |
| 268 | iip->ili_orig_root = NULL; |
| 269 | } |
| 270 | if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) { |
| 271 | ASSERT((ip->i_df.if_broot != NULL) && |
| 272 | (ip->i_df.if_broot_bytes > 0)); |
| 273 | iip->ili_root_size = ip->i_df.if_broot_bytes; |
| 274 | iip->ili_orig_root = |
| 275 | (char*)kmem_alloc(iip->ili_root_size, KM_SLEEP); |
| 276 | memcpy(iip->ili_orig_root, (char*)(ip->i_df.if_broot), |
| 277 | iip->ili_root_size); |
| 278 | } |
| 279 | } |
| 280 | #endif |