| 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" | 
|  | 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" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_quota.h" | 
|  | 40 | #include "xfs_utils.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | /* | 
|  | 43 | * Initialize the inode hash table for the newly mounted file system. | 
|  | 44 | * Choose an initial table size based on user specified value, else | 
|  | 45 | * use a simple algorithm using the maximum number of inodes as an | 
|  | 46 | * indicator for table size, and clamp it between one and some large | 
|  | 47 | * number of pages. | 
|  | 48 | */ | 
|  | 49 | void | 
|  | 50 | xfs_ihash_init(xfs_mount_t *mp) | 
|  | 51 | { | 
|  | 52 | __uint64_t	icount; | 
| Nathan Scott | 77e4635 | 2006-09-28 11:03:27 +1000 | [diff] [blame] | 53 | uint		i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 |  | 
|  | 55 | if (!mp->m_ihsize) { | 
|  | 56 | icount = mp->m_maxicount ? mp->m_maxicount : | 
|  | 57 | (mp->m_sb.sb_dblocks << mp->m_sb.sb_inopblog); | 
|  | 58 | mp->m_ihsize = 1 << max_t(uint, 8, | 
|  | 59 | (xfs_highbit64(icount) + 1) / 2); | 
|  | 60 | mp->m_ihsize = min_t(uint, mp->m_ihsize, | 
|  | 61 | (64 * NBPP) / sizeof(xfs_ihash_t)); | 
|  | 62 | } | 
|  | 63 |  | 
| Nathan Scott | 77e4635 | 2006-09-28 11:03:27 +1000 | [diff] [blame] | 64 | mp->m_ihash = kmem_zalloc_greedy(&mp->m_ihsize, | 
|  | 65 | NBPC * sizeof(xfs_ihash_t), | 
|  | 66 | mp->m_ihsize * sizeof(xfs_ihash_t), | 
|  | 67 | KM_SLEEP | KM_MAYFAIL | KM_LARGE); | 
|  | 68 | mp->m_ihsize /= sizeof(xfs_ihash_t); | 
|  | 69 | for (i = 0; i < mp->m_ihsize; i++) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | rwlock_init(&(mp->m_ihash[i].ih_lock)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
|  | 73 | /* | 
|  | 74 | * Free up structures allocated by xfs_ihash_init, at unmount time. | 
|  | 75 | */ | 
|  | 76 | void | 
|  | 77 | xfs_ihash_free(xfs_mount_t *mp) | 
|  | 78 | { | 
| Nathan Scott | 77e4635 | 2006-09-28 11:03:27 +1000 | [diff] [blame] | 79 | kmem_free(mp->m_ihash, mp->m_ihsize * sizeof(xfs_ihash_t)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | mp->m_ihash = NULL; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | /* | 
|  | 84 | * Initialize the inode cluster hash table for the newly mounted file system. | 
|  | 85 | * Its size is derived from the ihash table size. | 
|  | 86 | */ | 
|  | 87 | void | 
|  | 88 | xfs_chash_init(xfs_mount_t *mp) | 
|  | 89 | { | 
|  | 90 | uint	i; | 
|  | 91 |  | 
|  | 92 | mp->m_chsize = max_t(uint, 1, mp->m_ihsize / | 
|  | 93 | (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)); | 
|  | 94 | mp->m_chsize = min_t(uint, mp->m_chsize, mp->m_ihsize); | 
|  | 95 | mp->m_chash = (xfs_chash_t *)kmem_zalloc(mp->m_chsize | 
|  | 96 | * sizeof(xfs_chash_t), | 
| Nathan Scott | efb8ad7 | 2006-09-28 11:03:05 +1000 | [diff] [blame] | 97 | KM_SLEEP | KM_LARGE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | for (i = 0; i < mp->m_chsize; i++) { | 
|  | 99 | spinlock_init(&mp->m_chash[i].ch_lock,"xfshash"); | 
|  | 100 | } | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | /* | 
|  | 104 | * Free up structures allocated by xfs_chash_init, at unmount time. | 
|  | 105 | */ | 
|  | 106 | void | 
|  | 107 | xfs_chash_free(xfs_mount_t *mp) | 
|  | 108 | { | 
|  | 109 | int	i; | 
|  | 110 |  | 
|  | 111 | for (i = 0; i < mp->m_chsize; i++) { | 
|  | 112 | spinlock_destroy(&mp->m_chash[i].ch_lock); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | kmem_free(mp->m_chash, mp->m_chsize*sizeof(xfs_chash_t)); | 
|  | 116 | mp->m_chash = NULL; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | /* | 
| Nathan Scott | 71bce25 | 2005-05-05 13:23:27 -0700 | [diff] [blame] | 120 | * Try to move an inode to the front of its hash list if possible | 
|  | 121 | * (and if its not there already).  Called right after obtaining | 
|  | 122 | * the list version number and then dropping the read_lock on the | 
|  | 123 | * hash list in question (which is done right after looking up the | 
|  | 124 | * inode in question...). | 
|  | 125 | */ | 
|  | 126 | STATIC void | 
|  | 127 | xfs_ihash_promote( | 
|  | 128 | xfs_ihash_t	*ih, | 
|  | 129 | xfs_inode_t	*ip, | 
|  | 130 | ulong		version) | 
|  | 131 | { | 
|  | 132 | xfs_inode_t	*iq; | 
|  | 133 |  | 
|  | 134 | if ((ip->i_prevp != &ih->ih_next) && write_trylock(&ih->ih_lock)) { | 
|  | 135 | if (likely(version == ih->ih_version)) { | 
|  | 136 | /* remove from list */ | 
|  | 137 | if ((iq = ip->i_next)) { | 
|  | 138 | iq->i_prevp = ip->i_prevp; | 
|  | 139 | } | 
|  | 140 | *ip->i_prevp = iq; | 
|  | 141 |  | 
|  | 142 | /* insert at list head */ | 
|  | 143 | iq = ih->ih_next; | 
|  | 144 | iq->i_prevp = &ip->i_next; | 
|  | 145 | ip->i_next = iq; | 
|  | 146 | ip->i_prevp = &ih->ih_next; | 
|  | 147 | ih->ih_next = ip; | 
|  | 148 | } | 
|  | 149 | write_unlock(&ih->ih_lock); | 
|  | 150 | } | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | * Look up an inode by number in the given file system. | 
|  | 155 | * The inode is looked up in the hash table for the file system | 
|  | 156 | * represented by the mount point parameter mp.  Each bucket of | 
|  | 157 | * the hash table is guarded by an individual semaphore. | 
|  | 158 | * | 
|  | 159 | * If the inode is found in the hash table, its corresponding vnode | 
|  | 160 | * is obtained with a call to vn_get().  This call takes care of | 
|  | 161 | * coordination with the reclamation of the inode and vnode.  Note | 
|  | 162 | * that the vmap structure is filled in while holding the hash lock. | 
|  | 163 | * This gives us the state of the inode/vnode when we found it and | 
|  | 164 | * is used for coordination in vn_get(). | 
|  | 165 | * | 
|  | 166 | * If it is not in core, read it in from the file system's device and | 
|  | 167 | * add the inode into the hash table. | 
|  | 168 | * | 
|  | 169 | * The inode is locked according to the value of the lock_flags parameter. | 
|  | 170 | * This flag parameter indicates how and if the inode's IO lock and inode lock | 
|  | 171 | * should be taken. | 
|  | 172 | * | 
|  | 173 | * mp -- the mount point structure for the current file system.  It points | 
|  | 174 | *       to the inode hash table. | 
|  | 175 | * tp -- a pointer to the current transaction if there is one.  This is | 
|  | 176 | *       simply passed through to the xfs_iread() call. | 
|  | 177 | * ino -- the number of the inode desired.  This is the unique identifier | 
|  | 178 | *        within the file system for the inode being requested. | 
|  | 179 | * lock_flags -- flags indicating how to lock the inode.  See the comment | 
|  | 180 | *		 for xfs_ilock() for a list of valid values. | 
|  | 181 | * bno -- the block number starting the buffer containing the inode, | 
|  | 182 | *	  if known (as by bulkstat), else 0. | 
|  | 183 | */ | 
|  | 184 | STATIC int | 
|  | 185 | xfs_iget_core( | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 186 | bhv_vnode_t	*vp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | xfs_mount_t	*mp, | 
|  | 188 | xfs_trans_t	*tp, | 
|  | 189 | xfs_ino_t	ino, | 
|  | 190 | uint		flags, | 
|  | 191 | uint		lock_flags, | 
|  | 192 | xfs_inode_t	**ipp, | 
|  | 193 | xfs_daddr_t	bno) | 
|  | 194 | { | 
|  | 195 | xfs_ihash_t	*ih; | 
|  | 196 | xfs_inode_t	*ip; | 
|  | 197 | xfs_inode_t	*iq; | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 198 | bhv_vnode_t	*inode_vp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | ulong		version; | 
|  | 200 | int		error; | 
|  | 201 | /* REFERENCED */ | 
|  | 202 | xfs_chash_t	*ch; | 
|  | 203 | xfs_chashlist_t	*chl, *chlnew; | 
|  | 204 | SPLDECL(s); | 
|  | 205 |  | 
|  | 206 |  | 
|  | 207 | ih = XFS_IHASH(mp, ino); | 
|  | 208 |  | 
|  | 209 | again: | 
|  | 210 | read_lock(&ih->ih_lock); | 
|  | 211 |  | 
|  | 212 | for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) { | 
|  | 213 | if (ip->i_ino == ino) { | 
|  | 214 | /* | 
|  | 215 | * If INEW is set this inode is being set up | 
|  | 216 | * we need to pause and try again. | 
|  | 217 | */ | 
| David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 218 | if (xfs_iflags_test(ip, XFS_INEW)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | read_unlock(&ih->ih_lock); | 
|  | 220 | delay(1); | 
|  | 221 | XFS_STATS_INC(xs_ig_frecycle); | 
|  | 222 |  | 
|  | 223 | goto again; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | inode_vp = XFS_ITOV_NULL(ip); | 
|  | 227 | if (inode_vp == NULL) { | 
|  | 228 | /* | 
|  | 229 | * If IRECLAIM is set this inode is | 
|  | 230 | * on its way out of the system, | 
|  | 231 | * we need to pause and try again. | 
|  | 232 | */ | 
| David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 233 | if (xfs_iflags_test(ip, XFS_IRECLAIM)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | read_unlock(&ih->ih_lock); | 
|  | 235 | delay(1); | 
|  | 236 | XFS_STATS_INC(xs_ig_frecycle); | 
|  | 237 |  | 
|  | 238 | goto again; | 
|  | 239 | } | 
| David Chinner | 4c60658 | 2006-11-11 18:05:00 +1100 | [diff] [blame] | 240 | ASSERT(xfs_iflags_test(ip, XFS_IRECLAIMABLE)); | 
|  | 241 |  | 
|  | 242 | /* | 
|  | 243 | * If lookup is racing with unlink, then we | 
|  | 244 | * should return an error immediately so we | 
|  | 245 | * don't remove it from the reclaim list and | 
|  | 246 | * potentially leak the inode. | 
|  | 247 | */ | 
|  | 248 | if ((ip->i_d.di_mode == 0) && | 
|  | 249 | !(flags & XFS_IGET_CREATE)) { | 
|  | 250 | read_unlock(&ih->ih_lock); | 
|  | 251 | return ENOENT; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | /* | 
|  | 255 | * There may be transactions sitting in the | 
|  | 256 | * incore log buffers or being flushed to disk | 
|  | 257 | * at this time.  We can't clear the | 
|  | 258 | * XFS_IRECLAIMABLE flag until these | 
|  | 259 | * transactions have hit the disk, otherwise we | 
|  | 260 | * will void the guarantee the flag provides | 
|  | 261 | * xfs_iunpin() | 
|  | 262 | */ | 
|  | 263 | if (xfs_ipincount(ip)) { | 
|  | 264 | read_unlock(&ih->ih_lock); | 
|  | 265 | xfs_log_force(mp, 0, | 
|  | 266 | XFS_LOG_FORCE|XFS_LOG_SYNC); | 
|  | 267 | XFS_STATS_INC(xs_ig_frecycle); | 
|  | 268 | goto again; | 
|  | 269 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 |  | 
|  | 271 | vn_trace_exit(vp, "xfs_iget.alloc", | 
|  | 272 | (inst_t *)__return_address); | 
|  | 273 |  | 
|  | 274 | XFS_STATS_INC(xs_ig_found); | 
|  | 275 |  | 
| David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 276 | xfs_iflags_clear(ip, XFS_IRECLAIMABLE); | 
| Nathan Scott | 71bce25 | 2005-05-05 13:23:27 -0700 | [diff] [blame] | 277 | version = ih->ih_version; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | read_unlock(&ih->ih_lock); | 
| Nathan Scott | 71bce25 | 2005-05-05 13:23:27 -0700 | [diff] [blame] | 279 | xfs_ihash_promote(ih, ip, version); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 |  | 
|  | 281 | XFS_MOUNT_ILOCK(mp); | 
|  | 282 | list_del_init(&ip->i_reclaim); | 
|  | 283 | XFS_MOUNT_IUNLOCK(mp); | 
|  | 284 |  | 
|  | 285 | goto finish_inode; | 
|  | 286 |  | 
|  | 287 | } else if (vp != inode_vp) { | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 288 | struct inode *inode = vn_to_inode(inode_vp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 |  | 
|  | 290 | /* The inode is being torn down, pause and | 
|  | 291 | * try again. | 
|  | 292 | */ | 
|  | 293 | if (inode->i_state & (I_FREEING | I_CLEAR)) { | 
|  | 294 | read_unlock(&ih->ih_lock); | 
|  | 295 | delay(1); | 
|  | 296 | XFS_STATS_INC(xs_ig_frecycle); | 
|  | 297 |  | 
|  | 298 | goto again; | 
|  | 299 | } | 
|  | 300 | /* Chances are the other vnode (the one in the inode) is being torn | 
|  | 301 | * down right now, and we landed on top of it. Question is, what do | 
|  | 302 | * we do? Unhook the old inode and hook up the new one? | 
|  | 303 | */ | 
|  | 304 | cmn_err(CE_PANIC, | 
|  | 305 | "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p", | 
|  | 306 | inode_vp, vp); | 
|  | 307 | } | 
|  | 308 |  | 
| Nathan Scott | 71bce25 | 2005-05-05 13:23:27 -0700 | [diff] [blame] | 309 | /* | 
|  | 310 | * Inode cache hit: if ip is not at the front of | 
|  | 311 | * its hash chain, move it there now. | 
|  | 312 | * Do this with the lock held for update, but | 
|  | 313 | * do statistics after releasing the lock. | 
|  | 314 | */ | 
|  | 315 | version = ih->ih_version; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | read_unlock(&ih->ih_lock); | 
| Nathan Scott | 71bce25 | 2005-05-05 13:23:27 -0700 | [diff] [blame] | 317 | xfs_ihash_promote(ih, ip, version); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | XFS_STATS_INC(xs_ig_found); | 
|  | 319 |  | 
|  | 320 | finish_inode: | 
|  | 321 | if (ip->i_d.di_mode == 0) { | 
| Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 322 | if (!(flags & XFS_IGET_CREATE)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | return ENOENT; | 
|  | 324 | xfs_iocore_inode_reinit(ip); | 
|  | 325 | } | 
| Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 326 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | if (lock_flags != 0) | 
|  | 328 | xfs_ilock(ip, lock_flags); | 
|  | 329 |  | 
| David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 330 | xfs_iflags_clear(ip, XFS_ISTALE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | vn_trace_exit(vp, "xfs_iget.found", | 
|  | 332 | (inst_t *)__return_address); | 
|  | 333 | goto return_ip; | 
|  | 334 | } | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | /* | 
|  | 338 | * Inode cache miss: save the hash chain version stamp and unlock | 
|  | 339 | * the chain, so we don't deadlock in vn_alloc. | 
|  | 340 | */ | 
|  | 341 | XFS_STATS_INC(xs_ig_missed); | 
|  | 342 |  | 
|  | 343 | version = ih->ih_version; | 
|  | 344 |  | 
|  | 345 | read_unlock(&ih->ih_lock); | 
|  | 346 |  | 
|  | 347 | /* | 
|  | 348 | * Read the disk inode attributes into a new inode structure and get | 
|  | 349 | * a new vnode for it. This should also initialize i_ino and i_mount. | 
|  | 350 | */ | 
| Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 351 | error = xfs_iread(mp, tp, ino, &ip, bno, | 
|  | 352 | (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0); | 
|  | 353 | if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 |  | 
|  | 356 | vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address); | 
|  | 357 |  | 
|  | 358 | xfs_inode_lock_init(ip, vp); | 
|  | 359 | xfs_iocore_inode_init(ip); | 
|  | 360 |  | 
| Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 361 | if (lock_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | xfs_ilock(ip, lock_flags); | 
| Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 363 |  | 
|  | 364 | if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | xfs_idestroy(ip); | 
|  | 366 | return ENOENT; | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | /* | 
|  | 370 | * Put ip on its hash chain, unless someone else hashed a duplicate | 
|  | 371 | * after we released the hash lock. | 
|  | 372 | */ | 
|  | 373 | write_lock(&ih->ih_lock); | 
|  | 374 |  | 
|  | 375 | if (ih->ih_version != version) { | 
|  | 376 | for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) { | 
|  | 377 | if (iq->i_ino == ino) { | 
|  | 378 | write_unlock(&ih->ih_lock); | 
|  | 379 | xfs_idestroy(ip); | 
|  | 380 |  | 
|  | 381 | XFS_STATS_INC(xs_ig_dup); | 
|  | 382 | goto again; | 
|  | 383 | } | 
|  | 384 | } | 
|  | 385 | } | 
|  | 386 |  | 
|  | 387 | /* | 
|  | 388 | * These values _must_ be set before releasing ihlock! | 
|  | 389 | */ | 
|  | 390 | ip->i_hash = ih; | 
|  | 391 | if ((iq = ih->ih_next)) { | 
|  | 392 | iq->i_prevp = &ip->i_next; | 
|  | 393 | } | 
|  | 394 | ip->i_next = iq; | 
|  | 395 | ip->i_prevp = &ih->ih_next; | 
|  | 396 | ih->ih_next = ip; | 
|  | 397 | ip->i_udquot = ip->i_gdquot = NULL; | 
|  | 398 | ih->ih_version++; | 
| David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 399 | xfs_iflags_set(ip, XFS_INEW); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | write_unlock(&ih->ih_lock); | 
|  | 401 |  | 
|  | 402 | /* | 
|  | 403 | * put ip on its cluster's hash chain | 
|  | 404 | */ | 
|  | 405 | ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL && | 
|  | 406 | ip->i_cnext == NULL); | 
|  | 407 |  | 
|  | 408 | chlnew = NULL; | 
|  | 409 | ch = XFS_CHASH(mp, ip->i_blkno); | 
|  | 410 | chlredo: | 
|  | 411 | s = mutex_spinlock(&ch->ch_lock); | 
|  | 412 | for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) { | 
|  | 413 | if (chl->chl_blkno == ip->i_blkno) { | 
|  | 414 |  | 
|  | 415 | /* insert this inode into the doubly-linked list | 
|  | 416 | * where chl points */ | 
|  | 417 | if ((iq = chl->chl_ip)) { | 
|  | 418 | ip->i_cprev = iq->i_cprev; | 
|  | 419 | iq->i_cprev->i_cnext = ip; | 
|  | 420 | iq->i_cprev = ip; | 
|  | 421 | ip->i_cnext = iq; | 
|  | 422 | } else { | 
|  | 423 | ip->i_cnext = ip; | 
|  | 424 | ip->i_cprev = ip; | 
|  | 425 | } | 
|  | 426 | chl->chl_ip = ip; | 
|  | 427 | ip->i_chash = chl; | 
|  | 428 | break; | 
|  | 429 | } | 
|  | 430 | } | 
|  | 431 |  | 
|  | 432 | /* no hash list found for this block; add a new hash list */ | 
|  | 433 | if (chl == NULL)  { | 
|  | 434 | if (chlnew == NULL) { | 
|  | 435 | mutex_spinunlock(&ch->ch_lock, s); | 
|  | 436 | ASSERT(xfs_chashlist_zone != NULL); | 
|  | 437 | chlnew = (xfs_chashlist_t *) | 
|  | 438 | kmem_zone_alloc(xfs_chashlist_zone, | 
|  | 439 | KM_SLEEP); | 
|  | 440 | ASSERT(chlnew != NULL); | 
|  | 441 | goto chlredo; | 
|  | 442 | } else { | 
|  | 443 | ip->i_cnext = ip; | 
|  | 444 | ip->i_cprev = ip; | 
|  | 445 | ip->i_chash = chlnew; | 
|  | 446 | chlnew->chl_ip = ip; | 
|  | 447 | chlnew->chl_blkno = ip->i_blkno; | 
| David Chinner | 1fc5d95 | 2006-04-11 15:11:12 +1000 | [diff] [blame] | 448 | if (ch->ch_list) | 
|  | 449 | ch->ch_list->chl_prev = chlnew; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | chlnew->chl_next = ch->ch_list; | 
| David Chinner | 1fc5d95 | 2006-04-11 15:11:12 +1000 | [diff] [blame] | 451 | chlnew->chl_prev = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | ch->ch_list = chlnew; | 
|  | 453 | chlnew = NULL; | 
|  | 454 | } | 
|  | 455 | } else { | 
|  | 456 | if (chlnew != NULL) { | 
|  | 457 | kmem_zone_free(xfs_chashlist_zone, chlnew); | 
|  | 458 | } | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | mutex_spinunlock(&ch->ch_lock, s); | 
|  | 462 |  | 
|  | 463 |  | 
|  | 464 | /* | 
|  | 465 | * Link ip to its mount and thread it on the mount's inode list. | 
|  | 466 | */ | 
|  | 467 | XFS_MOUNT_ILOCK(mp); | 
|  | 468 | if ((iq = mp->m_inodes)) { | 
|  | 469 | ASSERT(iq->i_mprev->i_mnext == iq); | 
|  | 470 | ip->i_mprev = iq->i_mprev; | 
|  | 471 | iq->i_mprev->i_mnext = ip; | 
|  | 472 | iq->i_mprev = ip; | 
|  | 473 | ip->i_mnext = iq; | 
|  | 474 | } else { | 
|  | 475 | ip->i_mnext = ip; | 
|  | 476 | ip->i_mprev = ip; | 
|  | 477 | } | 
|  | 478 | mp->m_inodes = ip; | 
|  | 479 |  | 
|  | 480 | XFS_MOUNT_IUNLOCK(mp); | 
|  | 481 |  | 
|  | 482 | return_ip: | 
|  | 483 | ASSERT(ip->i_df.if_ext_max == | 
|  | 484 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t)); | 
|  | 485 |  | 
|  | 486 | ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) == | 
|  | 487 | ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0)); | 
|  | 488 |  | 
|  | 489 | *ipp = ip; | 
|  | 490 |  | 
|  | 491 | /* | 
|  | 492 | * If we have a real type for an on-disk inode, we can set ops(&unlock) | 
|  | 493 | * now.	 If it's a new inode being created, xfs_ialloc will handle it. | 
|  | 494 | */ | 
| Nathan Scott | b83bd13 | 2006-06-09 16:48:30 +1000 | [diff] [blame] | 495 | bhv_vfs_init_vnode(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 |  | 
|  | 497 | return 0; | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 |  | 
|  | 501 | /* | 
|  | 502 | * The 'normal' internal xfs_iget, if needed it will | 
|  | 503 | * 'allocate', or 'get', the vnode. | 
|  | 504 | */ | 
|  | 505 | int | 
|  | 506 | xfs_iget( | 
|  | 507 | xfs_mount_t	*mp, | 
|  | 508 | xfs_trans_t	*tp, | 
|  | 509 | xfs_ino_t	ino, | 
|  | 510 | uint		flags, | 
|  | 511 | uint		lock_flags, | 
|  | 512 | xfs_inode_t	**ipp, | 
|  | 513 | xfs_daddr_t	bno) | 
|  | 514 | { | 
|  | 515 | struct inode	*inode; | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 516 | bhv_vnode_t	*vp = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | int		error; | 
|  | 518 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | XFS_STATS_INC(xs_ig_attempts); | 
|  | 520 |  | 
| Christoph Hellwig | ba403ab | 2005-09-05 08:33:00 +1000 | [diff] [blame] | 521 | retry: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | xfs_inode_t	*ip; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 |  | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 525 | vp = vn_from_inode(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (inode->i_state & I_NEW) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | vn_initialize(inode); | 
|  | 528 | error = xfs_iget_core(vp, mp, tp, ino, flags, | 
|  | 529 | lock_flags, ipp, bno); | 
|  | 530 | if (error) { | 
|  | 531 | vn_mark_bad(vp); | 
|  | 532 | if (inode->i_state & I_NEW) | 
|  | 533 | unlock_new_inode(inode); | 
|  | 534 | iput(inode); | 
|  | 535 | } | 
|  | 536 | } else { | 
| Christoph Hellwig | ba403ab | 2005-09-05 08:33:00 +1000 | [diff] [blame] | 537 | /* | 
|  | 538 | * If the inode is not fully constructed due to | 
| Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 539 | * filehandle mismatches wait for the inode to go | 
| Christoph Hellwig | ba403ab | 2005-09-05 08:33:00 +1000 | [diff] [blame] | 540 | * away and try again. | 
|  | 541 | * | 
|  | 542 | * iget_locked will call __wait_on_freeing_inode | 
|  | 543 | * to wait for the inode to go away. | 
|  | 544 | */ | 
|  | 545 | if (is_bad_inode(inode) || | 
| Christoph Hellwig | 75e17b3 | 2006-01-11 20:58:44 +1100 | [diff] [blame] | 546 | ((ip = xfs_vtoi(vp)) == NULL)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | iput(inode); | 
| Christoph Hellwig | ba403ab | 2005-09-05 08:33:00 +1000 | [diff] [blame] | 548 | delay(1); | 
|  | 549 | goto retry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } | 
|  | 551 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (lock_flags != 0) | 
|  | 553 | xfs_ilock(ip, lock_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | XFS_STATS_INC(xs_ig_found); | 
|  | 555 | *ipp = ip; | 
|  | 556 | error = 0; | 
|  | 557 | } | 
|  | 558 | } else | 
|  | 559 | error = ENOMEM;	/* If we got no inode we are out of memory */ | 
|  | 560 |  | 
|  | 561 | return error; | 
|  | 562 | } | 
|  | 563 |  | 
|  | 564 | /* | 
|  | 565 | * Do the setup for the various locks within the incore inode. | 
|  | 566 | */ | 
|  | 567 | void | 
|  | 568 | xfs_inode_lock_init( | 
|  | 569 | xfs_inode_t	*ip, | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 570 | bhv_vnode_t	*vp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | { | 
|  | 572 | mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER, | 
|  | 573 | "xfsino", (long)vp->v_number); | 
|  | 574 | mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number); | 
|  | 575 | init_waitqueue_head(&ip->i_ipin_wait); | 
|  | 576 | atomic_set(&ip->i_pincount, 0); | 
| Eric Sandeen | 7ae67d7 | 2006-09-28 11:05:46 +1000 | [diff] [blame] | 577 | initnsema(&ip->i_flock, 1, "xfsfino"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } | 
|  | 579 |  | 
|  | 580 | /* | 
|  | 581 | * Look for the inode corresponding to the given ino in the hash table. | 
|  | 582 | * If it is there and its i_transp pointer matches tp, return it. | 
|  | 583 | * Otherwise, return NULL. | 
|  | 584 | */ | 
|  | 585 | xfs_inode_t * | 
|  | 586 | xfs_inode_incore(xfs_mount_t	*mp, | 
|  | 587 | xfs_ino_t	ino, | 
|  | 588 | xfs_trans_t	*tp) | 
|  | 589 | { | 
|  | 590 | xfs_ihash_t	*ih; | 
|  | 591 | xfs_inode_t	*ip; | 
| Nathan Scott | 71bce25 | 2005-05-05 13:23:27 -0700 | [diff] [blame] | 592 | ulong		version; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 |  | 
|  | 594 | ih = XFS_IHASH(mp, ino); | 
|  | 595 | read_lock(&ih->ih_lock); | 
|  | 596 | for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) { | 
|  | 597 | if (ip->i_ino == ino) { | 
|  | 598 | /* | 
|  | 599 | * If we find it and tp matches, return it. | 
| Nathan Scott | 71bce25 | 2005-05-05 13:23:27 -0700 | [diff] [blame] | 600 | * Also move it to the front of the hash list | 
|  | 601 | * if we find it and it is not already there. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | * Otherwise break from the loop and return | 
|  | 603 | * NULL. | 
|  | 604 | */ | 
|  | 605 | if (ip->i_transp == tp) { | 
| Nathan Scott | 71bce25 | 2005-05-05 13:23:27 -0700 | [diff] [blame] | 606 | version = ih->ih_version; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | read_unlock(&ih->ih_lock); | 
| Nathan Scott | 71bce25 | 2005-05-05 13:23:27 -0700 | [diff] [blame] | 608 | xfs_ihash_promote(ih, ip, version); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | return (ip); | 
|  | 610 | } | 
|  | 611 | break; | 
|  | 612 | } | 
|  | 613 | } | 
|  | 614 | read_unlock(&ih->ih_lock); | 
|  | 615 | return (NULL); | 
|  | 616 | } | 
|  | 617 |  | 
|  | 618 | /* | 
|  | 619 | * Decrement reference count of an inode structure and unlock it. | 
|  | 620 | * | 
|  | 621 | * ip -- the inode being released | 
|  | 622 | * lock_flags -- this parameter indicates the inode's locks to be | 
|  | 623 | *       to be released.  See the comment on xfs_iunlock() for a list | 
|  | 624 | *	 of valid values. | 
|  | 625 | */ | 
|  | 626 | void | 
|  | 627 | xfs_iput(xfs_inode_t	*ip, | 
|  | 628 | uint		lock_flags) | 
|  | 629 | { | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 630 | bhv_vnode_t	*vp = XFS_ITOV(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 |  | 
|  | 632 | vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | xfs_iunlock(ip, lock_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | VN_RELE(vp); | 
|  | 635 | } | 
|  | 636 |  | 
|  | 637 | /* | 
|  | 638 | * Special iput for brand-new inodes that are still locked | 
|  | 639 | */ | 
|  | 640 | void | 
|  | 641 | xfs_iput_new(xfs_inode_t	*ip, | 
|  | 642 | uint		lock_flags) | 
|  | 643 | { | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 644 | bhv_vnode_t	*vp = XFS_ITOV(ip); | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 645 | struct inode	*inode = vn_to_inode(vp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 |  | 
|  | 647 | vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address); | 
|  | 648 |  | 
|  | 649 | if ((ip->i_d.di_mode == 0)) { | 
| David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 650 | ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | vn_mark_bad(vp); | 
|  | 652 | } | 
|  | 653 | if (inode->i_state & I_NEW) | 
|  | 654 | unlock_new_inode(inode); | 
|  | 655 | if (lock_flags) | 
|  | 656 | xfs_iunlock(ip, lock_flags); | 
|  | 657 | VN_RELE(vp); | 
|  | 658 | } | 
|  | 659 |  | 
|  | 660 |  | 
|  | 661 | /* | 
|  | 662 | * This routine embodies the part of the reclaim code that pulls | 
|  | 663 | * the inode from the inode hash table and the mount structure's | 
|  | 664 | * inode list. | 
|  | 665 | * This should only be called from xfs_reclaim(). | 
|  | 666 | */ | 
|  | 667 | void | 
|  | 668 | xfs_ireclaim(xfs_inode_t *ip) | 
|  | 669 | { | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 670 | bhv_vnode_t	*vp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 |  | 
|  | 672 | /* | 
|  | 673 | * Remove from old hash list and mount list. | 
|  | 674 | */ | 
|  | 675 | XFS_STATS_INC(xs_ig_reclaims); | 
|  | 676 |  | 
|  | 677 | xfs_iextract(ip); | 
|  | 678 |  | 
|  | 679 | /* | 
|  | 680 | * Here we do a spurious inode lock in order to coordinate with | 
|  | 681 | * xfs_sync().  This is because xfs_sync() references the inodes | 
|  | 682 | * in the mount list without taking references on the corresponding | 
|  | 683 | * vnodes.  We make that OK here by ensuring that we wait until | 
|  | 684 | * the inode is unlocked in xfs_sync() before we go ahead and | 
|  | 685 | * free it.  We get both the regular lock and the io lock because | 
|  | 686 | * the xfs_sync() code may need to drop the regular one but will | 
|  | 687 | * still hold the io lock. | 
|  | 688 | */ | 
|  | 689 | xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | 
|  | 690 |  | 
|  | 691 | /* | 
|  | 692 | * Release dquots (and their references) if any. An inode may escape | 
|  | 693 | * xfs_inactive and get here via vn_alloc->vn_reclaim path. | 
|  | 694 | */ | 
|  | 695 | XFS_QM_DQDETACH(ip->i_mount, ip); | 
|  | 696 |  | 
|  | 697 | /* | 
|  | 698 | * Pull our behavior descriptor from the vnode chain. | 
|  | 699 | */ | 
|  | 700 | vp = XFS_ITOV_NULL(ip); | 
|  | 701 | if (vp) { | 
|  | 702 | vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip)); | 
|  | 703 | } | 
|  | 704 |  | 
|  | 705 | /* | 
|  | 706 | * Free all memory associated with the inode. | 
|  | 707 | */ | 
| Tim Shimmin | 439b843 | 2006-11-11 18:04:34 +1100 | [diff] [blame] | 708 | xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | xfs_idestroy(ip); | 
|  | 710 | } | 
|  | 711 |  | 
|  | 712 | /* | 
|  | 713 | * This routine removes an about-to-be-destroyed inode from | 
|  | 714 | * all of the lists in which it is located with the exception | 
|  | 715 | * of the behavior chain. | 
|  | 716 | */ | 
|  | 717 | void | 
|  | 718 | xfs_iextract( | 
|  | 719 | xfs_inode_t	*ip) | 
|  | 720 | { | 
|  | 721 | xfs_ihash_t	*ih; | 
|  | 722 | xfs_inode_t	*iq; | 
|  | 723 | xfs_mount_t	*mp; | 
|  | 724 | xfs_chash_t	*ch; | 
|  | 725 | xfs_chashlist_t *chl, *chm; | 
|  | 726 | SPLDECL(s); | 
|  | 727 |  | 
|  | 728 | ih = ip->i_hash; | 
|  | 729 | write_lock(&ih->ih_lock); | 
|  | 730 | if ((iq = ip->i_next)) { | 
|  | 731 | iq->i_prevp = ip->i_prevp; | 
|  | 732 | } | 
|  | 733 | *ip->i_prevp = iq; | 
| Nathan Scott | 71bce25 | 2005-05-05 13:23:27 -0700 | [diff] [blame] | 734 | ih->ih_version++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | write_unlock(&ih->ih_lock); | 
|  | 736 |  | 
|  | 737 | /* | 
|  | 738 | * Remove from cluster hash list | 
|  | 739 | *   1) delete the chashlist if this is the last inode on the chashlist | 
|  | 740 | *   2) unchain from list of inodes | 
|  | 741 | *   3) point chashlist->chl_ip to 'chl_next' if to this inode. | 
|  | 742 | */ | 
|  | 743 | mp = ip->i_mount; | 
|  | 744 | ch = XFS_CHASH(mp, ip->i_blkno); | 
|  | 745 | s = mutex_spinlock(&ch->ch_lock); | 
|  | 746 |  | 
|  | 747 | if (ip->i_cnext == ip) { | 
|  | 748 | /* Last inode on chashlist */ | 
|  | 749 | ASSERT(ip->i_cnext == ip && ip->i_cprev == ip); | 
|  | 750 | ASSERT(ip->i_chash != NULL); | 
|  | 751 | chm=NULL; | 
| David Chinner | 1fc5d95 | 2006-04-11 15:11:12 +1000 | [diff] [blame] | 752 | chl = ip->i_chash; | 
|  | 753 | if (chl->chl_prev) | 
|  | 754 | chl->chl_prev->chl_next = chl->chl_next; | 
|  | 755 | else | 
|  | 756 | ch->ch_list = chl->chl_next; | 
|  | 757 | if (chl->chl_next) | 
|  | 758 | chl->chl_next->chl_prev = chl->chl_prev; | 
|  | 759 | kmem_zone_free(xfs_chashlist_zone, chl); | 
|  | 760 | } else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | /* delete one inode from a non-empty list */ | 
|  | 762 | iq = ip->i_cnext; | 
|  | 763 | iq->i_cprev = ip->i_cprev; | 
|  | 764 | ip->i_cprev->i_cnext = iq; | 
|  | 765 | if (ip->i_chash->chl_ip == ip) { | 
|  | 766 | ip->i_chash->chl_ip = iq; | 
|  | 767 | } | 
|  | 768 | ip->i_chash = __return_address; | 
|  | 769 | ip->i_cprev = __return_address; | 
|  | 770 | ip->i_cnext = __return_address; | 
|  | 771 | } | 
|  | 772 | mutex_spinunlock(&ch->ch_lock, s); | 
|  | 773 |  | 
|  | 774 | /* | 
|  | 775 | * Remove from mount's inode list. | 
|  | 776 | */ | 
|  | 777 | XFS_MOUNT_ILOCK(mp); | 
|  | 778 | ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL)); | 
|  | 779 | iq = ip->i_mnext; | 
|  | 780 | iq->i_mprev = ip->i_mprev; | 
|  | 781 | ip->i_mprev->i_mnext = iq; | 
|  | 782 |  | 
|  | 783 | /* | 
|  | 784 | * Fix up the head pointer if it points to the inode being deleted. | 
|  | 785 | */ | 
|  | 786 | if (mp->m_inodes == ip) { | 
|  | 787 | if (ip == iq) { | 
|  | 788 | mp->m_inodes = NULL; | 
|  | 789 | } else { | 
|  | 790 | mp->m_inodes = iq; | 
|  | 791 | } | 
|  | 792 | } | 
|  | 793 |  | 
|  | 794 | /* Deal with the deleted inodes list */ | 
|  | 795 | list_del_init(&ip->i_reclaim); | 
|  | 796 |  | 
|  | 797 | mp->m_ireclaims++; | 
|  | 798 | XFS_MOUNT_IUNLOCK(mp); | 
|  | 799 | } | 
|  | 800 |  | 
|  | 801 | /* | 
|  | 802 | * This is a wrapper routine around the xfs_ilock() routine | 
|  | 803 | * used to centralize some grungy code.  It is used in places | 
|  | 804 | * that wish to lock the inode solely for reading the extents. | 
|  | 805 | * The reason these places can't just call xfs_ilock(SHARED) | 
|  | 806 | * is that the inode lock also guards to bringing in of the | 
|  | 807 | * extents from disk for a file in b-tree format.  If the inode | 
|  | 808 | * is in b-tree format, then we need to lock the inode exclusively | 
|  | 809 | * until the extents are read in.  Locking it exclusively all | 
|  | 810 | * the time would limit our parallelism unnecessarily, though. | 
|  | 811 | * What we do instead is check to see if the extents have been | 
|  | 812 | * read in yet, and only lock the inode exclusively if they | 
|  | 813 | * have not. | 
|  | 814 | * | 
|  | 815 | * The function returns a value which should be given to the | 
|  | 816 | * corresponding xfs_iunlock_map_shared().  This value is | 
|  | 817 | * the mode in which the lock was actually taken. | 
|  | 818 | */ | 
|  | 819 | uint | 
|  | 820 | xfs_ilock_map_shared( | 
|  | 821 | xfs_inode_t	*ip) | 
|  | 822 | { | 
|  | 823 | uint	lock_mode; | 
|  | 824 |  | 
|  | 825 | if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) && | 
|  | 826 | ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) { | 
|  | 827 | lock_mode = XFS_ILOCK_EXCL; | 
|  | 828 | } else { | 
|  | 829 | lock_mode = XFS_ILOCK_SHARED; | 
|  | 830 | } | 
|  | 831 |  | 
|  | 832 | xfs_ilock(ip, lock_mode); | 
|  | 833 |  | 
|  | 834 | return lock_mode; | 
|  | 835 | } | 
|  | 836 |  | 
|  | 837 | /* | 
|  | 838 | * This is simply the unlock routine to go with xfs_ilock_map_shared(). | 
|  | 839 | * All it does is call xfs_iunlock() with the given lock_mode. | 
|  | 840 | */ | 
|  | 841 | void | 
|  | 842 | xfs_iunlock_map_shared( | 
|  | 843 | xfs_inode_t	*ip, | 
|  | 844 | unsigned int	lock_mode) | 
|  | 845 | { | 
|  | 846 | xfs_iunlock(ip, lock_mode); | 
|  | 847 | } | 
|  | 848 |  | 
|  | 849 | /* | 
|  | 850 | * The xfs inode contains 2 locks: a multi-reader lock called the | 
|  | 851 | * i_iolock and a multi-reader lock called the i_lock.  This routine | 
|  | 852 | * allows either or both of the locks to be obtained. | 
|  | 853 | * | 
|  | 854 | * The 2 locks should always be ordered so that the IO lock is | 
|  | 855 | * obtained first in order to prevent deadlock. | 
|  | 856 | * | 
|  | 857 | * ip -- the inode being locked | 
|  | 858 | * lock_flags -- this parameter indicates the inode's locks | 
|  | 859 | *       to be locked.  It can be: | 
|  | 860 | *		XFS_IOLOCK_SHARED, | 
|  | 861 | *		XFS_IOLOCK_EXCL, | 
|  | 862 | *		XFS_ILOCK_SHARED, | 
|  | 863 | *		XFS_ILOCK_EXCL, | 
|  | 864 | *		XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, | 
|  | 865 | *		XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, | 
|  | 866 | *		XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, | 
|  | 867 | *		XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL | 
|  | 868 | */ | 
|  | 869 | void | 
|  | 870 | xfs_ilock(xfs_inode_t	*ip, | 
|  | 871 | uint		lock_flags) | 
|  | 872 | { | 
|  | 873 | /* | 
|  | 874 | * You can't set both SHARED and EXCL for the same lock, | 
|  | 875 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | 
|  | 876 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | 
|  | 877 | */ | 
|  | 878 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | 
|  | 879 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | 
|  | 880 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | 
|  | 881 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | 
|  | 882 | ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0); | 
|  | 883 |  | 
|  | 884 | if (lock_flags & XFS_IOLOCK_EXCL) { | 
|  | 885 | mrupdate(&ip->i_iolock); | 
|  | 886 | } else if (lock_flags & XFS_IOLOCK_SHARED) { | 
|  | 887 | mraccess(&ip->i_iolock); | 
|  | 888 | } | 
|  | 889 | if (lock_flags & XFS_ILOCK_EXCL) { | 
|  | 890 | mrupdate(&ip->i_lock); | 
|  | 891 | } else if (lock_flags & XFS_ILOCK_SHARED) { | 
|  | 892 | mraccess(&ip->i_lock); | 
|  | 893 | } | 
|  | 894 | xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address); | 
|  | 895 | } | 
|  | 896 |  | 
|  | 897 | /* | 
|  | 898 | * This is just like xfs_ilock(), except that the caller | 
|  | 899 | * is guaranteed not to sleep.  It returns 1 if it gets | 
|  | 900 | * the requested locks and 0 otherwise.  If the IO lock is | 
|  | 901 | * obtained but the inode lock cannot be, then the IO lock | 
|  | 902 | * is dropped before returning. | 
|  | 903 | * | 
|  | 904 | * ip -- the inode being locked | 
|  | 905 | * lock_flags -- this parameter indicates the inode's locks to be | 
|  | 906 | *       to be locked.  See the comment for xfs_ilock() for a list | 
|  | 907 | *	 of valid values. | 
|  | 908 | * | 
|  | 909 | */ | 
|  | 910 | int | 
|  | 911 | xfs_ilock_nowait(xfs_inode_t	*ip, | 
|  | 912 | uint		lock_flags) | 
|  | 913 | { | 
|  | 914 | int	iolocked; | 
|  | 915 | int	ilocked; | 
|  | 916 |  | 
|  | 917 | /* | 
|  | 918 | * You can't set both SHARED and EXCL for the same lock, | 
|  | 919 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | 
|  | 920 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | 
|  | 921 | */ | 
|  | 922 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | 
|  | 923 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | 
|  | 924 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | 
|  | 925 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | 
|  | 926 | ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0); | 
|  | 927 |  | 
|  | 928 | iolocked = 0; | 
|  | 929 | if (lock_flags & XFS_IOLOCK_EXCL) { | 
|  | 930 | iolocked = mrtryupdate(&ip->i_iolock); | 
|  | 931 | if (!iolocked) { | 
|  | 932 | return 0; | 
|  | 933 | } | 
|  | 934 | } else if (lock_flags & XFS_IOLOCK_SHARED) { | 
|  | 935 | iolocked = mrtryaccess(&ip->i_iolock); | 
|  | 936 | if (!iolocked) { | 
|  | 937 | return 0; | 
|  | 938 | } | 
|  | 939 | } | 
|  | 940 | if (lock_flags & XFS_ILOCK_EXCL) { | 
|  | 941 | ilocked = mrtryupdate(&ip->i_lock); | 
|  | 942 | if (!ilocked) { | 
|  | 943 | if (iolocked) { | 
|  | 944 | mrunlock(&ip->i_iolock); | 
|  | 945 | } | 
|  | 946 | return 0; | 
|  | 947 | } | 
|  | 948 | } else if (lock_flags & XFS_ILOCK_SHARED) { | 
|  | 949 | ilocked = mrtryaccess(&ip->i_lock); | 
|  | 950 | if (!ilocked) { | 
|  | 951 | if (iolocked) { | 
|  | 952 | mrunlock(&ip->i_iolock); | 
|  | 953 | } | 
|  | 954 | return 0; | 
|  | 955 | } | 
|  | 956 | } | 
|  | 957 | xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address); | 
|  | 958 | return 1; | 
|  | 959 | } | 
|  | 960 |  | 
|  | 961 | /* | 
|  | 962 | * xfs_iunlock() is used to drop the inode locks acquired with | 
|  | 963 | * xfs_ilock() and xfs_ilock_nowait().  The caller must pass | 
|  | 964 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so | 
|  | 965 | * that we know which locks to drop. | 
|  | 966 | * | 
|  | 967 | * ip -- the inode being unlocked | 
|  | 968 | * lock_flags -- this parameter indicates the inode's locks to be | 
|  | 969 | *       to be unlocked.  See the comment for xfs_ilock() for a list | 
|  | 970 | *	 of valid values for this parameter. | 
|  | 971 | * | 
|  | 972 | */ | 
|  | 973 | void | 
|  | 974 | xfs_iunlock(xfs_inode_t	*ip, | 
|  | 975 | uint	lock_flags) | 
|  | 976 | { | 
|  | 977 | /* | 
|  | 978 | * You can't set both SHARED and EXCL for the same lock, | 
|  | 979 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | 
|  | 980 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | 
|  | 981 | */ | 
|  | 982 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | 
|  | 983 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | 
|  | 984 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | 
|  | 985 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | 
|  | 986 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0); | 
|  | 987 | ASSERT(lock_flags != 0); | 
|  | 988 |  | 
|  | 989 | if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) { | 
|  | 990 | ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) || | 
|  | 991 | (ismrlocked(&ip->i_iolock, MR_ACCESS))); | 
|  | 992 | ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) || | 
|  | 993 | (ismrlocked(&ip->i_iolock, MR_UPDATE))); | 
|  | 994 | mrunlock(&ip->i_iolock); | 
|  | 995 | } | 
|  | 996 |  | 
|  | 997 | if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) { | 
|  | 998 | ASSERT(!(lock_flags & XFS_ILOCK_SHARED) || | 
|  | 999 | (ismrlocked(&ip->i_lock, MR_ACCESS))); | 
|  | 1000 | ASSERT(!(lock_flags & XFS_ILOCK_EXCL) || | 
|  | 1001 | (ismrlocked(&ip->i_lock, MR_UPDATE))); | 
|  | 1002 | mrunlock(&ip->i_lock); | 
|  | 1003 |  | 
|  | 1004 | /* | 
|  | 1005 | * Let the AIL know that this item has been unlocked in case | 
|  | 1006 | * it is in the AIL and anyone is waiting on it.  Don't do | 
|  | 1007 | * this if the caller has asked us not to. | 
|  | 1008 | */ | 
|  | 1009 | if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) && | 
|  | 1010 | ip->i_itemp != NULL) { | 
|  | 1011 | xfs_trans_unlocked_item(ip->i_mount, | 
|  | 1012 | (xfs_log_item_t*)(ip->i_itemp)); | 
|  | 1013 | } | 
|  | 1014 | } | 
|  | 1015 | xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address); | 
|  | 1016 | } | 
|  | 1017 |  | 
|  | 1018 | /* | 
|  | 1019 | * give up write locks.  the i/o lock cannot be held nested | 
|  | 1020 | * if it is being demoted. | 
|  | 1021 | */ | 
|  | 1022 | void | 
|  | 1023 | xfs_ilock_demote(xfs_inode_t	*ip, | 
|  | 1024 | uint		lock_flags) | 
|  | 1025 | { | 
|  | 1026 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); | 
|  | 1027 | ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); | 
|  | 1028 |  | 
|  | 1029 | if (lock_flags & XFS_ILOCK_EXCL) { | 
|  | 1030 | ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE)); | 
|  | 1031 | mrdemote(&ip->i_lock); | 
|  | 1032 | } | 
|  | 1033 | if (lock_flags & XFS_IOLOCK_EXCL) { | 
|  | 1034 | ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE)); | 
|  | 1035 | mrdemote(&ip->i_iolock); | 
|  | 1036 | } | 
|  | 1037 | } | 
|  | 1038 |  | 
|  | 1039 | /* | 
|  | 1040 | * The following three routines simply manage the i_flock | 
|  | 1041 | * semaphore embedded in the inode.  This semaphore synchronizes | 
|  | 1042 | * processes attempting to flush the in-core inode back to disk. | 
|  | 1043 | */ | 
|  | 1044 | void | 
|  | 1045 | xfs_iflock(xfs_inode_t *ip) | 
|  | 1046 | { | 
|  | 1047 | psema(&(ip->i_flock), PINOD|PLTWAIT); | 
|  | 1048 | } | 
|  | 1049 |  | 
|  | 1050 | int | 
|  | 1051 | xfs_iflock_nowait(xfs_inode_t *ip) | 
|  | 1052 | { | 
|  | 1053 | return (cpsema(&(ip->i_flock))); | 
|  | 1054 | } | 
|  | 1055 |  | 
|  | 1056 | void | 
|  | 1057 | xfs_ifunlock(xfs_inode_t *ip) | 
|  | 1058 | { | 
| Al Viro | 0d8fee3 | 2006-06-19 08:41:30 +1000 | [diff] [blame] | 1059 | ASSERT(issemalocked(&(ip->i_flock))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | vsema(&(ip->i_flock)); | 
|  | 1061 | } |