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; |
| 88 | xfs_inode_log_item_t *iip; |
| 89 | |
| 90 | /* |
| 91 | * If the transaction pointer is NULL, just call the normal |
| 92 | * xfs_iget(). |
| 93 | */ |
| 94 | if (tp == NULL) |
| 95 | return xfs_iget(mp, NULL, ino, flags, lock_flags, ipp, 0); |
| 96 | |
| 97 | /* |
| 98 | * If we find the inode in core with this transaction |
| 99 | * pointer in its i_transp field, then we know we already |
| 100 | * have it locked. In this case we just increment the lock |
| 101 | * recursion count and return the inode to the caller. |
| 102 | * Assert that the inode is already locked in the mode requested |
| 103 | * by the caller. We cannot do lock promotions yet, so |
| 104 | * die if someone gets this wrong. |
| 105 | */ |
| 106 | if ((ip = xfs_inode_incore(tp->t_mountp, ino, tp)) != NULL) { |
| 107 | /* |
| 108 | * Make sure that the inode lock is held EXCL and |
| 109 | * that the io lock is never upgraded when the inode |
| 110 | * is already a part of the transaction. |
| 111 | */ |
| 112 | ASSERT(ip->i_itemp != NULL); |
| 113 | ASSERT(lock_flags & XFS_ILOCK_EXCL); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 114 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) || |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 116 | xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) || |
| 118 | (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_EXCL)); |
| 119 | ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) || |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 120 | xfs_isilocked(ip, XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) || |
| 122 | (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_ANY)); |
| 123 | |
| 124 | if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) { |
| 125 | ip->i_itemp->ili_iolock_recur++; |
| 126 | } |
| 127 | if (lock_flags & XFS_ILOCK_EXCL) { |
| 128 | ip->i_itemp->ili_ilock_recur++; |
| 129 | } |
| 130 | *ipp = ip; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | ASSERT(lock_flags & XFS_ILOCK_EXCL); |
| 135 | error = xfs_iget(tp->t_mountp, tp, ino, flags, lock_flags, &ip, 0); |
| 136 | if (error) { |
| 137 | return error; |
| 138 | } |
| 139 | ASSERT(ip != NULL); |
| 140 | |
| 141 | /* |
| 142 | * Get a log_item_desc to point at the new item. |
| 143 | */ |
| 144 | if (ip->i_itemp == NULL) |
| 145 | xfs_inode_item_init(ip, mp); |
| 146 | iip = ip->i_itemp; |
| 147 | (void) xfs_trans_add_item(tp, (xfs_log_item_t *)(iip)); |
| 148 | |
| 149 | xfs_trans_inode_broot_debug(ip); |
| 150 | |
| 151 | /* |
| 152 | * If the IO lock has been acquired, mark that in |
| 153 | * the inode log item so we'll know to unlock it |
| 154 | * when the transaction commits. |
| 155 | */ |
| 156 | ASSERT(iip->ili_flags == 0); |
| 157 | if (lock_flags & XFS_IOLOCK_EXCL) { |
| 158 | iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL; |
| 159 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
| 160 | iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Initialize i_transp so we can find it with xfs_inode_incore() |
| 165 | * above. |
| 166 | */ |
| 167 | ip->i_transp = tp; |
| 168 | |
| 169 | *ipp = ip; |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Add the locked inode to the transaction. |
| 175 | * The inode must be locked, and it cannot be associated with any |
| 176 | * transaction. The caller must specify the locks already held |
| 177 | * on the inode. |
| 178 | */ |
| 179 | void |
| 180 | xfs_trans_ijoin( |
| 181 | xfs_trans_t *tp, |
| 182 | xfs_inode_t *ip, |
| 183 | uint lock_flags) |
| 184 | { |
| 185 | xfs_inode_log_item_t *iip; |
| 186 | |
| 187 | ASSERT(ip->i_transp == NULL); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 188 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | ASSERT(lock_flags & XFS_ILOCK_EXCL); |
| 190 | if (ip->i_itemp == NULL) |
| 191 | xfs_inode_item_init(ip, ip->i_mount); |
| 192 | iip = ip->i_itemp; |
| 193 | ASSERT(iip->ili_flags == 0); |
| 194 | ASSERT(iip->ili_ilock_recur == 0); |
| 195 | ASSERT(iip->ili_iolock_recur == 0); |
| 196 | |
| 197 | /* |
| 198 | * Get a log_item_desc to point at the new item. |
| 199 | */ |
| 200 | (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(iip)); |
| 201 | |
| 202 | xfs_trans_inode_broot_debug(ip); |
| 203 | |
| 204 | /* |
| 205 | * If the IO lock is already held, mark that in the inode log item. |
| 206 | */ |
| 207 | if (lock_flags & XFS_IOLOCK_EXCL) { |
| 208 | iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL; |
| 209 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
| 210 | iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Initialize i_transp so we can find it with xfs_inode_incore() |
| 215 | * in xfs_trans_iget() above. |
| 216 | */ |
| 217 | ip->i_transp = tp; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | /* |
| 223 | * Mark the inode as not needing to be unlocked when the inode item's |
| 224 | * IOP_UNLOCK() routine is called. The inode must already be locked |
| 225 | * and associated with the given transaction. |
| 226 | */ |
| 227 | /*ARGSUSED*/ |
| 228 | void |
| 229 | xfs_trans_ihold( |
| 230 | xfs_trans_t *tp, |
| 231 | xfs_inode_t *ip) |
| 232 | { |
| 233 | ASSERT(ip->i_transp == tp); |
| 234 | ASSERT(ip->i_itemp != NULL); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 235 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | ip->i_itemp->ili_flags |= XFS_ILI_HOLD; |
| 238 | } |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * This is called to mark the fields indicated in fieldmask as needing |
| 243 | * to be logged when the transaction is committed. The inode must |
| 244 | * already be associated with the given transaction. |
| 245 | * |
| 246 | * The values for fieldmask are defined in xfs_inode_item.h. We always |
| 247 | * log all of the core inode if any of it has changed, and we always log |
| 248 | * all of the inline data/extents/b-tree root if any of them has changed. |
| 249 | */ |
| 250 | void |
| 251 | xfs_trans_log_inode( |
| 252 | xfs_trans_t *tp, |
| 253 | xfs_inode_t *ip, |
| 254 | uint flags) |
| 255 | { |
| 256 | xfs_log_item_desc_t *lidp; |
| 257 | |
| 258 | ASSERT(ip->i_transp == tp); |
| 259 | ASSERT(ip->i_itemp != NULL); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 260 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(ip->i_itemp)); |
| 263 | ASSERT(lidp != NULL); |
| 264 | |
| 265 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 266 | lidp->lid_flags |= XFS_LID_DIRTY; |
| 267 | |
| 268 | /* |
| 269 | * Always OR in the bits from the ili_last_fields field. |
| 270 | * This is to coordinate with the xfs_iflush() and xfs_iflush_done() |
| 271 | * routines in the eventual clearing of the ilf_fields bits. |
| 272 | * See the big comment in xfs_iflush() for an explanation of |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 273 | * this coordination mechanism. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | */ |
| 275 | flags |= ip->i_itemp->ili_last_fields; |
| 276 | ip->i_itemp->ili_format.ilf_fields |= flags; |
| 277 | } |
| 278 | |
| 279 | #ifdef XFS_TRANS_DEBUG |
| 280 | /* |
| 281 | * Keep track of the state of the inode btree root to make sure we |
| 282 | * log it properly. |
| 283 | */ |
| 284 | STATIC void |
| 285 | xfs_trans_inode_broot_debug( |
| 286 | xfs_inode_t *ip) |
| 287 | { |
| 288 | xfs_inode_log_item_t *iip; |
| 289 | |
| 290 | ASSERT(ip->i_itemp != NULL); |
| 291 | iip = ip->i_itemp; |
| 292 | if (iip->ili_root_size != 0) { |
| 293 | ASSERT(iip->ili_orig_root != NULL); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 294 | kmem_free(iip->ili_orig_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | iip->ili_root_size = 0; |
| 296 | iip->ili_orig_root = NULL; |
| 297 | } |
| 298 | if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) { |
| 299 | ASSERT((ip->i_df.if_broot != NULL) && |
| 300 | (ip->i_df.if_broot_bytes > 0)); |
| 301 | iip->ili_root_size = ip->i_df.if_broot_bytes; |
| 302 | iip->ili_orig_root = |
| 303 | (char*)kmem_alloc(iip->ili_root_size, KM_SLEEP); |
| 304 | memcpy(iip->ili_orig_root, (char*)(ip->i_df.if_broot), |
| 305 | iip->ili_root_size); |
| 306 | } |
| 307 | } |
| 308 | #endif |