Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write the Free Software Foundation, Inc., 59 |
| 21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ |
| 31 | */ |
| 32 | |
| 33 | #include "xfs.h" |
| 34 | |
| 35 | |
| 36 | uint64_t vn_generation; /* vnode generation number */ |
| 37 | DEFINE_SPINLOCK(vnumber_lock); |
| 38 | |
| 39 | /* |
| 40 | * Dedicated vnode inactive/reclaim sync semaphores. |
| 41 | * Prime number of hash buckets since address is used as the key. |
| 42 | */ |
| 43 | #define NVSYNC 37 |
| 44 | #define vptosync(v) (&vsync[((unsigned long)v) % NVSYNC]) |
Christoph Hellwig | 51c91ed | 2005-09-02 16:58:38 +1000 | [diff] [blame^] | 45 | STATIC wait_queue_head_t vsync[NVSYNC]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | void |
| 49 | vn_init(void) |
| 50 | { |
Christoph Hellwig | 51c91ed | 2005-09-02 16:58:38 +1000 | [diff] [blame^] | 51 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Christoph Hellwig | 51c91ed | 2005-09-02 16:58:38 +1000 | [diff] [blame^] | 53 | for (i = 0; i < NVSYNC; i++) |
| 54 | init_waitqueue_head(&vsync[i]); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | vn_iowait( |
| 59 | struct vnode *vp) |
| 60 | { |
| 61 | wait_queue_head_t *wq = vptosync(vp); |
| 62 | |
| 63 | wait_event(*wq, (atomic_read(&vp->v_iocount) == 0)); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | vn_iowake( |
| 68 | struct vnode *vp) |
| 69 | { |
| 70 | if (atomic_dec_and_test(&vp->v_iocount)) |
| 71 | wake_up(vptosync(vp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Clean a vnode of filesystem-specific data and prepare it for reuse. |
| 76 | */ |
| 77 | STATIC int |
| 78 | vn_reclaim( |
| 79 | struct vnode *vp) |
| 80 | { |
| 81 | int error; |
| 82 | |
| 83 | XFS_STATS_INC(vn_reclaim); |
| 84 | vn_trace_entry(vp, "vn_reclaim", (inst_t *)__return_address); |
| 85 | |
| 86 | /* |
| 87 | * Only make the VOP_RECLAIM call if there are behaviors |
| 88 | * to call. |
| 89 | */ |
| 90 | if (vp->v_fbhv) { |
| 91 | VOP_RECLAIM(vp, error); |
| 92 | if (error) |
| 93 | return -error; |
| 94 | } |
| 95 | ASSERT(vp->v_fbhv == NULL); |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | vp->v_fbhv = NULL; |
| 98 | |
| 99 | #ifdef XFS_VNODE_TRACE |
| 100 | ktrace_free(vp->v_trace); |
| 101 | vp->v_trace = NULL; |
| 102 | #endif |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | struct vnode * |
| 108 | vn_initialize( |
| 109 | struct inode *inode) |
| 110 | { |
| 111 | struct vnode *vp = LINVFS_GET_VP(inode); |
| 112 | |
| 113 | XFS_STATS_INC(vn_active); |
| 114 | XFS_STATS_INC(vn_alloc); |
| 115 | |
| 116 | vp->v_flag = VMODIFIED; |
| 117 | spinlock_init(&vp->v_lock, "v_lock"); |
| 118 | |
| 119 | spin_lock(&vnumber_lock); |
| 120 | if (!++vn_generation) /* v_number shouldn't be zero */ |
| 121 | vn_generation++; |
| 122 | vp->v_number = vn_generation; |
| 123 | spin_unlock(&vnumber_lock); |
| 124 | |
| 125 | ASSERT(VN_CACHED(vp) == 0); |
| 126 | |
| 127 | /* Initialize the first behavior and the behavior chain head. */ |
| 128 | vn_bhv_head_init(VN_BHV_HEAD(vp), "vnode"); |
| 129 | |
Christoph Hellwig | 51c91ed | 2005-09-02 16:58:38 +1000 | [diff] [blame^] | 130 | atomic_set(&vp->v_iocount, 0); |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | #ifdef XFS_VNODE_TRACE |
| 133 | vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | #endif /* XFS_VNODE_TRACE */ |
| 135 | |
| 136 | vn_trace_exit(vp, "vn_initialize", (inst_t *)__return_address); |
| 137 | return vp; |
| 138 | } |
| 139 | |
| 140 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * Revalidate the Linux inode from the vattr. |
| 142 | * Note: i_size _not_ updated; we must hold the inode |
| 143 | * semaphore when doing that - callers responsibility. |
| 144 | */ |
| 145 | void |
| 146 | vn_revalidate_core( |
| 147 | struct vnode *vp, |
| 148 | vattr_t *vap) |
| 149 | { |
| 150 | struct inode *inode = LINVFS_GET_IP(vp); |
| 151 | |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 152 | inode->i_mode = vap->va_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | inode->i_nlink = vap->va_nlink; |
| 154 | inode->i_uid = vap->va_uid; |
| 155 | inode->i_gid = vap->va_gid; |
| 156 | inode->i_blocks = vap->va_nblocks; |
| 157 | inode->i_mtime = vap->va_mtime; |
| 158 | inode->i_ctime = vap->va_ctime; |
| 159 | inode->i_atime = vap->va_atime; |
| 160 | if (vap->va_xflags & XFS_XFLAG_IMMUTABLE) |
| 161 | inode->i_flags |= S_IMMUTABLE; |
| 162 | else |
| 163 | inode->i_flags &= ~S_IMMUTABLE; |
| 164 | if (vap->va_xflags & XFS_XFLAG_APPEND) |
| 165 | inode->i_flags |= S_APPEND; |
| 166 | else |
| 167 | inode->i_flags &= ~S_APPEND; |
| 168 | if (vap->va_xflags & XFS_XFLAG_SYNC) |
| 169 | inode->i_flags |= S_SYNC; |
| 170 | else |
| 171 | inode->i_flags &= ~S_SYNC; |
| 172 | if (vap->va_xflags & XFS_XFLAG_NOATIME) |
| 173 | inode->i_flags |= S_NOATIME; |
| 174 | else |
| 175 | inode->i_flags &= ~S_NOATIME; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Revalidate the Linux inode from the vnode. |
| 180 | */ |
| 181 | int |
| 182 | vn_revalidate( |
| 183 | struct vnode *vp) |
| 184 | { |
| 185 | vattr_t va; |
| 186 | int error; |
| 187 | |
| 188 | vn_trace_entry(vp, "vn_revalidate", (inst_t *)__return_address); |
| 189 | ASSERT(vp->v_fbhv != NULL); |
| 190 | |
| 191 | va.va_mask = XFS_AT_STAT|XFS_AT_XFLAGS; |
| 192 | VOP_GETATTR(vp, &va, 0, NULL, error); |
| 193 | if (!error) { |
| 194 | vn_revalidate_core(vp, &va); |
| 195 | VUNMODIFY(vp); |
| 196 | } |
| 197 | return -error; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * purge a vnode from the cache |
| 202 | * At this point the vnode is guaranteed to have no references (vn_count == 0) |
| 203 | * The caller has to make sure that there are no ways someone could |
| 204 | * get a handle (via vn_get) on the vnode (usually done via a mount/vfs lock). |
| 205 | */ |
| 206 | void |
| 207 | vn_purge( |
| 208 | struct vnode *vp, |
| 209 | vmap_t *vmap) |
| 210 | { |
| 211 | vn_trace_entry(vp, "vn_purge", (inst_t *)__return_address); |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | /* |
| 214 | * Check whether vp has already been reclaimed since our caller |
| 215 | * sampled its version while holding a filesystem cache lock that |
| 216 | * its VOP_RECLAIM function acquires. |
| 217 | */ |
| 218 | VN_LOCK(vp); |
| 219 | if (vp->v_number != vmap->v_number) { |
| 220 | VN_UNLOCK(vp, 0); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | * Another process could have raced in and gotten this vnode... |
| 226 | */ |
| 227 | if (vn_count(vp) > 0) { |
| 228 | VN_UNLOCK(vp, 0); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | XFS_STATS_DEC(vn_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | VN_UNLOCK(vp, 0); |
| 234 | |
| 235 | /* |
| 236 | * Call VOP_RECLAIM and clean vp. The FSYNC_INVAL flag tells |
| 237 | * vp's filesystem to flush and invalidate all cached resources. |
| 238 | * When vn_reclaim returns, vp should have no private data, |
| 239 | * either in a system cache or attached to v_data. |
| 240 | */ |
| 241 | if (vn_reclaim(vp) != 0) |
| 242 | panic("vn_purge: cannot reclaim"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Add a reference to a referenced vnode. |
| 247 | */ |
| 248 | struct vnode * |
| 249 | vn_hold( |
| 250 | struct vnode *vp) |
| 251 | { |
| 252 | struct inode *inode; |
| 253 | |
| 254 | XFS_STATS_INC(vn_hold); |
| 255 | |
| 256 | VN_LOCK(vp); |
| 257 | inode = igrab(LINVFS_GET_IP(vp)); |
| 258 | ASSERT(inode); |
| 259 | VN_UNLOCK(vp, 0); |
| 260 | |
| 261 | return vp; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Call VOP_INACTIVE on last reference. |
| 266 | */ |
| 267 | void |
| 268 | vn_rele( |
| 269 | struct vnode *vp) |
| 270 | { |
| 271 | int vcnt; |
| 272 | int cache; |
| 273 | |
| 274 | XFS_STATS_INC(vn_rele); |
| 275 | |
| 276 | VN_LOCK(vp); |
| 277 | |
| 278 | vn_trace_entry(vp, "vn_rele", (inst_t *)__return_address); |
| 279 | vcnt = vn_count(vp); |
| 280 | |
| 281 | /* |
| 282 | * Since we always get called from put_inode we know |
| 283 | * that i_count won't be decremented after we |
| 284 | * return. |
| 285 | */ |
| 286 | if (!vcnt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | VN_UNLOCK(vp, 0); |
| 288 | |
| 289 | /* |
| 290 | * Do not make the VOP_INACTIVE call if there |
| 291 | * are no behaviors attached to the vnode to call. |
| 292 | */ |
| 293 | if (vp->v_fbhv) |
| 294 | VOP_INACTIVE(vp, NULL, cache); |
| 295 | |
| 296 | VN_LOCK(vp); |
Christoph Hellwig | 592cb26 | 2005-09-02 16:56:14 +1000 | [diff] [blame] | 297 | vp->v_flag &= ~VMODIFIED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | VN_UNLOCK(vp, 0); |
| 301 | |
| 302 | vn_trace_exit(vp, "vn_rele", (inst_t *)__return_address); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Finish the removal of a vnode. |
| 307 | */ |
| 308 | void |
| 309 | vn_remove( |
| 310 | struct vnode *vp) |
| 311 | { |
| 312 | vmap_t vmap; |
| 313 | |
| 314 | /* Make sure we don't do this to the same vnode twice */ |
| 315 | if (!(vp->v_fbhv)) |
| 316 | return; |
| 317 | |
| 318 | XFS_STATS_INC(vn_remove); |
| 319 | vn_trace_exit(vp, "vn_remove", (inst_t *)__return_address); |
| 320 | |
| 321 | /* |
| 322 | * After the following purge the vnode |
| 323 | * will no longer exist. |
| 324 | */ |
| 325 | VMAP(vp, vmap); |
| 326 | vn_purge(vp, &vmap); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | #ifdef XFS_VNODE_TRACE |
| 331 | |
| 332 | #define KTRACE_ENTER(vp, vk, s, line, ra) \ |
| 333 | ktrace_enter( (vp)->v_trace, \ |
| 334 | /* 0 */ (void *)(__psint_t)(vk), \ |
| 335 | /* 1 */ (void *)(s), \ |
| 336 | /* 2 */ (void *)(__psint_t) line, \ |
Christoph Hellwig | 02de1f0 | 2005-06-21 15:33:48 +1000 | [diff] [blame] | 337 | /* 3 */ (void *)(__psint_t)(vn_count(vp)), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | /* 4 */ (void *)(ra), \ |
| 339 | /* 5 */ (void *)(__psunsigned_t)(vp)->v_flag, \ |
| 340 | /* 6 */ (void *)(__psint_t)current_cpu(), \ |
| 341 | /* 7 */ (void *)(__psint_t)current_pid(), \ |
| 342 | /* 8 */ (void *)__return_address, \ |
Christoph Hellwig | 02de1f0 | 2005-06-21 15:33:48 +1000 | [diff] [blame] | 343 | /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | /* |
| 346 | * Vnode tracing code. |
| 347 | */ |
| 348 | void |
Eric Sandeen | 764433b | 2005-05-05 13:29:17 -0700 | [diff] [blame] | 349 | vn_trace_entry(vnode_t *vp, const char *func, inst_t *ra) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
| 351 | KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra); |
| 352 | } |
| 353 | |
| 354 | void |
Eric Sandeen | 764433b | 2005-05-05 13:29:17 -0700 | [diff] [blame] | 355 | vn_trace_exit(vnode_t *vp, const char *func, inst_t *ra) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
| 357 | KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra); |
| 358 | } |
| 359 | |
| 360 | void |
| 361 | vn_trace_hold(vnode_t *vp, char *file, int line, inst_t *ra) |
| 362 | { |
| 363 | KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra); |
| 364 | } |
| 365 | |
| 366 | void |
| 367 | vn_trace_ref(vnode_t *vp, char *file, int line, inst_t *ra) |
| 368 | { |
| 369 | KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra); |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | vn_trace_rele(vnode_t *vp, char *file, int line, inst_t *ra) |
| 374 | { |
| 375 | KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra); |
| 376 | } |
| 377 | #endif /* XFS_VNODE_TRACE */ |