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" |
| 27 | #include "xfs_dir.h" |
| 28 | #include "xfs_dir2.h" |
| 29 | #include "xfs_dmapi.h" |
| 30 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dir_sf.h" |
| 35 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 36 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 39 | #include "xfs_btree.h" |
| 40 | #include "xfs_ialloc.h" |
| 41 | #include "xfs_trans_priv.h" |
| 42 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #ifdef XFS_TRANS_DEBUG |
| 45 | STATIC void |
| 46 | xfs_trans_inode_broot_debug( |
| 47 | xfs_inode_t *ip); |
| 48 | #else |
| 49 | #define xfs_trans_inode_broot_debug(ip) |
| 50 | #endif |
| 51 | |
| 52 | |
| 53 | /* |
| 54 | * Get and lock the inode for the caller if it is not already |
| 55 | * locked within the given transaction. If it is already locked |
| 56 | * within the transaction, just increment its lock recursion count |
| 57 | * and return a pointer to it. |
| 58 | * |
| 59 | * For an inode to be locked in a transaction, the inode lock, as |
| 60 | * opposed to the io lock, must be taken exclusively. This ensures |
| 61 | * that the inode can be involved in only 1 transaction at a time. |
| 62 | * Lock recursion is handled on the io lock, but only for lock modes |
| 63 | * of equal or lesser strength. That is, you can recur on the io lock |
| 64 | * held EXCL with a SHARED request but not vice versa. Also, if |
| 65 | * the inode is already a part of the transaction then you cannot |
| 66 | * go from not holding the io lock to having it EXCL or SHARED. |
| 67 | * |
| 68 | * Use the inode cache routine xfs_inode_incore() to find the inode |
| 69 | * if it is already owned by this transaction. |
| 70 | * |
| 71 | * If we don't already own the inode, use xfs_iget() to get it. |
| 72 | * Since the inode log item structure is embedded in the incore |
| 73 | * inode structure and is initialized when the inode is brought |
| 74 | * into memory, there is nothing to do with it here. |
| 75 | * |
| 76 | * If the given transaction pointer is NULL, just call xfs_iget(). |
| 77 | * This simplifies code which must handle both cases. |
| 78 | */ |
| 79 | int |
| 80 | xfs_trans_iget( |
| 81 | xfs_mount_t *mp, |
| 82 | xfs_trans_t *tp, |
| 83 | xfs_ino_t ino, |
| 84 | uint flags, |
| 85 | uint lock_flags, |
| 86 | xfs_inode_t **ipp) |
| 87 | { |
| 88 | int error; |
| 89 | xfs_inode_t *ip; |
| 90 | xfs_inode_log_item_t *iip; |
| 91 | |
| 92 | /* |
| 93 | * If the transaction pointer is NULL, just call the normal |
| 94 | * xfs_iget(). |
| 95 | */ |
| 96 | if (tp == NULL) |
| 97 | return xfs_iget(mp, NULL, ino, flags, lock_flags, ipp, 0); |
| 98 | |
| 99 | /* |
| 100 | * If we find the inode in core with this transaction |
| 101 | * pointer in its i_transp field, then we know we already |
| 102 | * have it locked. In this case we just increment the lock |
| 103 | * recursion count and return the inode to the caller. |
| 104 | * Assert that the inode is already locked in the mode requested |
| 105 | * by the caller. We cannot do lock promotions yet, so |
| 106 | * die if someone gets this wrong. |
| 107 | */ |
| 108 | if ((ip = xfs_inode_incore(tp->t_mountp, ino, tp)) != NULL) { |
| 109 | /* |
| 110 | * Make sure that the inode lock is held EXCL and |
| 111 | * that the io lock is never upgraded when the inode |
| 112 | * is already a part of the transaction. |
| 113 | */ |
| 114 | ASSERT(ip->i_itemp != NULL); |
| 115 | ASSERT(lock_flags & XFS_ILOCK_EXCL); |
| 116 | ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE)); |
| 117 | ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) || |
| 118 | ismrlocked(&ip->i_iolock, MR_UPDATE)); |
| 119 | ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) || |
| 120 | (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_EXCL)); |
| 121 | ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) || |
| 122 | ismrlocked(&ip->i_iolock, (MR_UPDATE | MR_ACCESS))); |
| 123 | ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) || |
| 124 | (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_ANY)); |
| 125 | |
| 126 | if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) { |
| 127 | ip->i_itemp->ili_iolock_recur++; |
| 128 | } |
| 129 | if (lock_flags & XFS_ILOCK_EXCL) { |
| 130 | ip->i_itemp->ili_ilock_recur++; |
| 131 | } |
| 132 | *ipp = ip; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | ASSERT(lock_flags & XFS_ILOCK_EXCL); |
| 137 | error = xfs_iget(tp->t_mountp, tp, ino, flags, lock_flags, &ip, 0); |
| 138 | if (error) { |
| 139 | return error; |
| 140 | } |
| 141 | ASSERT(ip != NULL); |
| 142 | |
| 143 | /* |
| 144 | * Get a log_item_desc to point at the new item. |
| 145 | */ |
| 146 | if (ip->i_itemp == NULL) |
| 147 | xfs_inode_item_init(ip, mp); |
| 148 | iip = ip->i_itemp; |
| 149 | (void) xfs_trans_add_item(tp, (xfs_log_item_t *)(iip)); |
| 150 | |
| 151 | xfs_trans_inode_broot_debug(ip); |
| 152 | |
| 153 | /* |
| 154 | * If the IO lock has been acquired, mark that in |
| 155 | * the inode log item so we'll know to unlock it |
| 156 | * when the transaction commits. |
| 157 | */ |
| 158 | ASSERT(iip->ili_flags == 0); |
| 159 | if (lock_flags & XFS_IOLOCK_EXCL) { |
| 160 | iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL; |
| 161 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
| 162 | iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Initialize i_transp so we can find it with xfs_inode_incore() |
| 167 | * above. |
| 168 | */ |
| 169 | ip->i_transp = tp; |
| 170 | |
| 171 | *ipp = ip; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Add the locked inode to the transaction. |
| 177 | * The inode must be locked, and it cannot be associated with any |
| 178 | * transaction. The caller must specify the locks already held |
| 179 | * on the inode. |
| 180 | */ |
| 181 | void |
| 182 | xfs_trans_ijoin( |
| 183 | xfs_trans_t *tp, |
| 184 | xfs_inode_t *ip, |
| 185 | uint lock_flags) |
| 186 | { |
| 187 | xfs_inode_log_item_t *iip; |
| 188 | |
| 189 | ASSERT(ip->i_transp == NULL); |
| 190 | ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE)); |
| 191 | ASSERT(lock_flags & XFS_ILOCK_EXCL); |
| 192 | if (ip->i_itemp == NULL) |
| 193 | xfs_inode_item_init(ip, ip->i_mount); |
| 194 | iip = ip->i_itemp; |
| 195 | ASSERT(iip->ili_flags == 0); |
| 196 | ASSERT(iip->ili_ilock_recur == 0); |
| 197 | ASSERT(iip->ili_iolock_recur == 0); |
| 198 | |
| 199 | /* |
| 200 | * Get a log_item_desc to point at the new item. |
| 201 | */ |
| 202 | (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(iip)); |
| 203 | |
| 204 | xfs_trans_inode_broot_debug(ip); |
| 205 | |
| 206 | /* |
| 207 | * If the IO lock is already held, mark that in the inode log item. |
| 208 | */ |
| 209 | if (lock_flags & XFS_IOLOCK_EXCL) { |
| 210 | iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL; |
| 211 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
| 212 | iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Initialize i_transp so we can find it with xfs_inode_incore() |
| 217 | * in xfs_trans_iget() above. |
| 218 | */ |
| 219 | ip->i_transp = tp; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | |
| 224 | /* |
| 225 | * Mark the inode as not needing to be unlocked when the inode item's |
| 226 | * IOP_UNLOCK() routine is called. The inode must already be locked |
| 227 | * and associated with the given transaction. |
| 228 | */ |
| 229 | /*ARGSUSED*/ |
| 230 | void |
| 231 | xfs_trans_ihold( |
| 232 | xfs_trans_t *tp, |
| 233 | xfs_inode_t *ip) |
| 234 | { |
| 235 | ASSERT(ip->i_transp == tp); |
| 236 | ASSERT(ip->i_itemp != NULL); |
| 237 | ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE)); |
| 238 | |
| 239 | ip->i_itemp->ili_flags |= XFS_ILI_HOLD; |
| 240 | } |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * This is called to mark the fields indicated in fieldmask as needing |
| 245 | * to be logged when the transaction is committed. The inode must |
| 246 | * already be associated with the given transaction. |
| 247 | * |
| 248 | * The values for fieldmask are defined in xfs_inode_item.h. We always |
| 249 | * log all of the core inode if any of it has changed, and we always log |
| 250 | * all of the inline data/extents/b-tree root if any of them has changed. |
| 251 | */ |
| 252 | void |
| 253 | xfs_trans_log_inode( |
| 254 | xfs_trans_t *tp, |
| 255 | xfs_inode_t *ip, |
| 256 | uint flags) |
| 257 | { |
| 258 | xfs_log_item_desc_t *lidp; |
| 259 | |
| 260 | ASSERT(ip->i_transp == tp); |
| 261 | ASSERT(ip->i_itemp != NULL); |
| 262 | ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE)); |
| 263 | |
| 264 | lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(ip->i_itemp)); |
| 265 | ASSERT(lidp != NULL); |
| 266 | |
| 267 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 268 | lidp->lid_flags |= XFS_LID_DIRTY; |
| 269 | |
| 270 | /* |
| 271 | * Always OR in the bits from the ili_last_fields field. |
| 272 | * This is to coordinate with the xfs_iflush() and xfs_iflush_done() |
| 273 | * routines in the eventual clearing of the ilf_fields bits. |
| 274 | * See the big comment in xfs_iflush() for an explanation of |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 275 | * this coordination mechanism. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | */ |
| 277 | flags |= ip->i_itemp->ili_last_fields; |
| 278 | ip->i_itemp->ili_format.ilf_fields |= flags; |
| 279 | } |
| 280 | |
| 281 | #ifdef XFS_TRANS_DEBUG |
| 282 | /* |
| 283 | * Keep track of the state of the inode btree root to make sure we |
| 284 | * log it properly. |
| 285 | */ |
| 286 | STATIC void |
| 287 | xfs_trans_inode_broot_debug( |
| 288 | xfs_inode_t *ip) |
| 289 | { |
| 290 | xfs_inode_log_item_t *iip; |
| 291 | |
| 292 | ASSERT(ip->i_itemp != NULL); |
| 293 | iip = ip->i_itemp; |
| 294 | if (iip->ili_root_size != 0) { |
| 295 | ASSERT(iip->ili_orig_root != NULL); |
| 296 | kmem_free(iip->ili_orig_root, iip->ili_root_size); |
| 297 | iip->ili_root_size = 0; |
| 298 | iip->ili_orig_root = NULL; |
| 299 | } |
| 300 | if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) { |
| 301 | ASSERT((ip->i_df.if_broot != NULL) && |
| 302 | (ip->i_df.if_broot_bytes > 0)); |
| 303 | iip->ili_root_size = ip->i_df.if_broot_bytes; |
| 304 | iip->ili_orig_root = |
| 305 | (char*)kmem_alloc(iip->ili_root_size, KM_SLEEP); |
| 306 | memcpy(iip->ili_orig_root, (char*)(ip->i_df.if_broot), |
| 307 | iip->ili_root_size); |
| 308 | } |
| 309 | } |
| 310 | #endif |