Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
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 | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
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. | |
1da177e4 | 13 | * |
7b718769 NS |
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 | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
ef14f0c1 | 21 | #include "xfs_acl.h" |
a844f451 | 22 | #include "xfs_bit.h" |
1da177e4 | 23 | #include "xfs_log.h" |
a844f451 | 24 | #include "xfs_inum.h" |
1da177e4 LT |
25 | #include "xfs_trans.h" |
26 | #include "xfs_sb.h" | |
27 | #include "xfs_ag.h" | |
1da177e4 LT |
28 | #include "xfs_dir2.h" |
29 | #include "xfs_dmapi.h" | |
30 | #include "xfs_mount.h" | |
1da177e4 | 31 | #include "xfs_bmap_btree.h" |
a844f451 | 32 | #include "xfs_alloc_btree.h" |
1da177e4 | 33 | #include "xfs_ialloc_btree.h" |
1da177e4 | 34 | #include "xfs_dir2_sf.h" |
a844f451 | 35 | #include "xfs_attr_sf.h" |
1da177e4 LT |
36 | #include "xfs_dinode.h" |
37 | #include "xfs_inode.h" | |
a844f451 NS |
38 | #include "xfs_btree.h" |
39 | #include "xfs_ialloc.h" | |
1da177e4 LT |
40 | #include "xfs_quota.h" |
41 | #include "xfs_utils.h" | |
783a2f65 DC |
42 | #include "xfs_trans_priv.h" |
43 | #include "xfs_inode_item.h" | |
24f211ba CH |
44 | #include "xfs_bmap.h" |
45 | #include "xfs_btree_trace.h" | |
46 | #include "xfs_dir2_trace.h" | |
47 | ||
48 | ||
49 | /* | |
50 | * Allocate and initialise an xfs_inode. | |
51 | */ | |
52 | STATIC struct xfs_inode * | |
53 | xfs_inode_alloc( | |
54 | struct xfs_mount *mp, | |
55 | xfs_ino_t ino) | |
56 | { | |
57 | struct xfs_inode *ip; | |
58 | ||
59 | /* | |
60 | * if this didn't occur in transactions, we could use | |
61 | * KM_MAYFAIL and return NULL here on ENOMEM. Set the | |
62 | * code up to do this anyway. | |
63 | */ | |
64 | ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP); | |
65 | if (!ip) | |
66 | return NULL; | |
54e34621 CH |
67 | if (inode_init_always(mp->m_super, VFS_I(ip))) { |
68 | kmem_zone_free(xfs_inode_zone, ip); | |
69 | return NULL; | |
70 | } | |
24f211ba CH |
71 | |
72 | ASSERT(atomic_read(&ip->i_iocount) == 0); | |
73 | ASSERT(atomic_read(&ip->i_pincount) == 0); | |
74 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); | |
75 | ASSERT(completion_done(&ip->i_flush)); | |
76 | ||
24f211ba CH |
77 | /* initialise the xfs inode */ |
78 | ip->i_ino = ino; | |
79 | ip->i_mount = mp; | |
80 | memset(&ip->i_imap, 0, sizeof(struct xfs_imap)); | |
81 | ip->i_afp = NULL; | |
82 | memset(&ip->i_df, 0, sizeof(xfs_ifork_t)); | |
83 | ip->i_flags = 0; | |
84 | ip->i_update_core = 0; | |
24f211ba CH |
85 | ip->i_delayed_blks = 0; |
86 | memset(&ip->i_d, 0, sizeof(xfs_icdinode_t)); | |
87 | ip->i_size = 0; | |
88 | ip->i_new_size = 0; | |
89 | ||
90 | /* | |
91 | * Initialize inode's trace buffers. | |
92 | */ | |
93 | #ifdef XFS_INODE_TRACE | |
94 | ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_NOFS); | |
95 | #endif | |
96 | #ifdef XFS_BMAP_TRACE | |
97 | ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_NOFS); | |
98 | #endif | |
99 | #ifdef XFS_BTREE_TRACE | |
100 | ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_NOFS); | |
101 | #endif | |
102 | #ifdef XFS_RW_TRACE | |
103 | ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_NOFS); | |
104 | #endif | |
105 | #ifdef XFS_ILOCK_TRACE | |
106 | ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_NOFS); | |
107 | #endif | |
108 | #ifdef XFS_DIR2_TRACE | |
109 | ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_NOFS); | |
110 | #endif | |
705db3fd DC |
111 | |
112 | /* prevent anyone from using this yet */ | |
113 | VFS_I(ip)->i_state = I_NEW|I_LOCK; | |
24f211ba CH |
114 | |
115 | return ip; | |
116 | } | |
1da177e4 | 117 | |
b36ec042 CH |
118 | STATIC void |
119 | xfs_inode_free( | |
120 | struct xfs_inode *ip) | |
121 | { | |
122 | switch (ip->i_d.di_mode & S_IFMT) { | |
123 | case S_IFREG: | |
124 | case S_IFDIR: | |
125 | case S_IFLNK: | |
126 | xfs_idestroy_fork(ip, XFS_DATA_FORK); | |
127 | break; | |
128 | } | |
129 | ||
130 | if (ip->i_afp) | |
131 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); | |
132 | ||
133 | #ifdef XFS_INODE_TRACE | |
134 | ktrace_free(ip->i_trace); | |
135 | #endif | |
136 | #ifdef XFS_BMAP_TRACE | |
137 | ktrace_free(ip->i_xtrace); | |
138 | #endif | |
139 | #ifdef XFS_BTREE_TRACE | |
140 | ktrace_free(ip->i_btrace); | |
141 | #endif | |
142 | #ifdef XFS_RW_TRACE | |
143 | ktrace_free(ip->i_rwtrace); | |
144 | #endif | |
145 | #ifdef XFS_ILOCK_TRACE | |
146 | ktrace_free(ip->i_lock_trace); | |
147 | #endif | |
148 | #ifdef XFS_DIR2_TRACE | |
149 | ktrace_free(ip->i_dir_trace); | |
150 | #endif | |
151 | ||
152 | if (ip->i_itemp) { | |
153 | /* | |
154 | * Only if we are shutting down the fs will we see an | |
155 | * inode still in the AIL. If it is there, we should remove | |
156 | * it to prevent a use-after-free from occurring. | |
157 | */ | |
158 | xfs_log_item_t *lip = &ip->i_itemp->ili_item; | |
159 | struct xfs_ail *ailp = lip->li_ailp; | |
160 | ||
161 | ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) || | |
162 | XFS_FORCED_SHUTDOWN(ip->i_mount)); | |
163 | if (lip->li_flags & XFS_LI_IN_AIL) { | |
164 | spin_lock(&ailp->xa_lock); | |
165 | if (lip->li_flags & XFS_LI_IN_AIL) | |
166 | xfs_trans_ail_delete(ailp, lip); | |
167 | else | |
168 | spin_unlock(&ailp->xa_lock); | |
169 | } | |
170 | xfs_inode_item_destroy(ip); | |
171 | ip->i_itemp = NULL; | |
172 | } | |
173 | ||
174 | /* asserts to verify all state is correct here */ | |
175 | ASSERT(atomic_read(&ip->i_iocount) == 0); | |
176 | ASSERT(atomic_read(&ip->i_pincount) == 0); | |
177 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); | |
178 | ASSERT(completion_done(&ip->i_flush)); | |
179 | ||
180 | kmem_zone_free(xfs_inode_zone, ip); | |
181 | } | |
182 | ||
1da177e4 | 183 | /* |
6441e549 | 184 | * Check the validity of the inode we just found it the cache |
1da177e4 | 185 | */ |
6441e549 DC |
186 | static int |
187 | xfs_iget_cache_hit( | |
6441e549 DC |
188 | struct xfs_perag *pag, |
189 | struct xfs_inode *ip, | |
190 | int flags, | |
191 | int lock_flags) __releases(pag->pag_ici_lock) | |
1da177e4 | 192 | { |
bc990f5c | 193 | struct inode *inode = VFS_I(ip); |
6441e549 | 194 | struct xfs_mount *mp = ip->i_mount; |
bc990f5c CH |
195 | int error; |
196 | ||
197 | spin_lock(&ip->i_flags_lock); | |
da353b0d | 198 | |
6441e549 | 199 | /* |
bc990f5c CH |
200 | * If we are racing with another cache hit that is currently |
201 | * instantiating this inode or currently recycling it out of | |
202 | * reclaimabe state, wait for the initialisation to complete | |
203 | * before continuing. | |
204 | * | |
205 | * XXX(hch): eventually we should do something equivalent to | |
206 | * wait_on_inode to wait for these flags to be cleared | |
207 | * instead of polling for it. | |
6441e549 | 208 | */ |
bc990f5c | 209 | if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) { |
6441e549 | 210 | XFS_STATS_INC(xs_ig_frecycle); |
bc990f5c | 211 | error = EAGAIN; |
6441e549 DC |
212 | goto out_error; |
213 | } | |
da353b0d | 214 | |
bc990f5c CH |
215 | /* |
216 | * If lookup is racing with unlink return an error immediately. | |
217 | */ | |
218 | if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) { | |
219 | error = ENOENT; | |
220 | goto out_error; | |
221 | } | |
bf904248 | 222 | |
bc990f5c CH |
223 | /* |
224 | * If IRECLAIMABLE is set, we've torn down the VFS inode already. | |
225 | * Need to carefully get it back into useable state. | |
226 | */ | |
227 | if (ip->i_flags & XFS_IRECLAIMABLE) { | |
bf904248 | 228 | xfs_itrace_exit_tag(ip, "xfs_iget.alloc"); |
da353b0d | 229 | |
6441e549 | 230 | /* |
bc990f5c CH |
231 | * We need to set XFS_INEW atomically with clearing the |
232 | * reclaimable tag so that we do have an indicator of the | |
233 | * inode still being initialized. | |
6441e549 | 234 | */ |
bc990f5c CH |
235 | ip->i_flags |= XFS_INEW; |
236 | ip->i_flags &= ~XFS_IRECLAIMABLE; | |
237 | __xfs_inode_clear_reclaim_tag(mp, pag, ip); | |
238 | ||
239 | spin_unlock(&ip->i_flags_lock); | |
240 | read_unlock(&pag->pag_ici_lock); | |
241 | ||
242 | error = -inode_init_always(mp->m_super, inode); | |
243 | if (error) { | |
244 | /* | |
245 | * Re-initializing the inode failed, and we are in deep | |
246 | * trouble. Try to re-add it to the reclaim list. | |
247 | */ | |
248 | read_lock(&pag->pag_ici_lock); | |
249 | spin_lock(&ip->i_flags_lock); | |
250 | ||
251 | ip->i_flags &= ~XFS_INEW; | |
252 | ip->i_flags |= XFS_IRECLAIMABLE; | |
253 | __xfs_inode_set_reclaim_tag(pag, ip); | |
6441e549 | 254 | goto out_error; |
da353b0d | 255 | } |
bc990f5c CH |
256 | inode->i_state = I_LOCK|I_NEW; |
257 | } else { | |
bf904248 | 258 | /* If the VFS inode is being torn down, pause and try again. */ |
bc990f5c CH |
259 | if (!igrab(inode)) { |
260 | error = EAGAIN; | |
261 | goto out_error; | |
262 | } | |
1da177e4 | 263 | |
bc990f5c CH |
264 | /* We've got a live one. */ |
265 | spin_unlock(&ip->i_flags_lock); | |
266 | read_unlock(&pag->pag_ici_lock); | |
6441e549 | 267 | } |
da353b0d | 268 | |
6441e549 DC |
269 | if (lock_flags != 0) |
270 | xfs_ilock(ip, lock_flags); | |
da353b0d | 271 | |
6441e549 DC |
272 | xfs_iflags_clear(ip, XFS_ISTALE); |
273 | xfs_itrace_exit_tag(ip, "xfs_iget.found"); | |
274 | XFS_STATS_INC(xs_ig_found); | |
275 | return 0; | |
1da177e4 | 276 | |
6441e549 | 277 | out_error: |
bc990f5c | 278 | spin_unlock(&ip->i_flags_lock); |
da353b0d | 279 | read_unlock(&pag->pag_ici_lock); |
6441e549 DC |
280 | return error; |
281 | } | |
282 | ||
283 | ||
284 | static int | |
285 | xfs_iget_cache_miss( | |
286 | struct xfs_mount *mp, | |
287 | struct xfs_perag *pag, | |
288 | xfs_trans_t *tp, | |
289 | xfs_ino_t ino, | |
290 | struct xfs_inode **ipp, | |
291 | xfs_daddr_t bno, | |
292 | int flags, | |
293 | int lock_flags) __releases(pag->pag_ici_lock) | |
294 | { | |
295 | struct xfs_inode *ip; | |
296 | int error; | |
297 | unsigned long first_index, mask; | |
298 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); | |
1da177e4 | 299 | |
24f211ba CH |
300 | ip = xfs_inode_alloc(mp, ino); |
301 | if (!ip) | |
302 | return ENOMEM; | |
303 | ||
304 | error = xfs_iread(mp, tp, ip, bno, flags); | |
6441e549 | 305 | if (error) |
24f211ba | 306 | goto out_destroy; |
1da177e4 | 307 | |
15947f2d | 308 | xfs_itrace_exit_tag(ip, "xfs_iget.alloc"); |
1da177e4 | 309 | |
745b1f47 | 310 | if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) { |
6441e549 DC |
311 | error = ENOENT; |
312 | goto out_destroy; | |
1da177e4 LT |
313 | } |
314 | ||
315 | /* | |
bad55843 | 316 | * Preload the radix tree so we can insert safely under the |
56e73ec4 DC |
317 | * write spinlock. Note that we cannot sleep inside the preload |
318 | * region. | |
1da177e4 | 319 | */ |
da353b0d | 320 | if (radix_tree_preload(GFP_KERNEL)) { |
6441e549 | 321 | error = EAGAIN; |
ed93ec39 CH |
322 | goto out_destroy; |
323 | } | |
324 | ||
325 | /* | |
326 | * Because the inode hasn't been added to the radix-tree yet it can't | |
327 | * be found by another thread, so we can do the non-sleeping lock here. | |
328 | */ | |
329 | if (lock_flags) { | |
330 | if (!xfs_ilock_nowait(ip, lock_flags)) | |
331 | BUG(); | |
da353b0d | 332 | } |
f338f903 | 333 | |
da353b0d DC |
334 | mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1); |
335 | first_index = agino & mask; | |
336 | write_lock(&pag->pag_ici_lock); | |
6441e549 DC |
337 | |
338 | /* insert the new inode */ | |
da353b0d DC |
339 | error = radix_tree_insert(&pag->pag_ici_root, agino, ip); |
340 | if (unlikely(error)) { | |
6441e549 | 341 | WARN_ON(error != -EEXIST); |
da353b0d | 342 | XFS_STATS_INC(xs_ig_dup); |
6441e549 | 343 | error = EAGAIN; |
56e73ec4 | 344 | goto out_preload_end; |
1da177e4 LT |
345 | } |
346 | ||
6441e549 | 347 | /* These values _must_ be set before releasing the radix tree lock! */ |
1da177e4 | 348 | ip->i_udquot = ip->i_gdquot = NULL; |
7a18c386 | 349 | xfs_iflags_set(ip, XFS_INEW); |
1da177e4 | 350 | |
da353b0d DC |
351 | write_unlock(&pag->pag_ici_lock); |
352 | radix_tree_preload_end(); | |
6441e549 DC |
353 | *ipp = ip; |
354 | return 0; | |
355 | ||
56e73ec4 | 356 | out_preload_end: |
6441e549 DC |
357 | write_unlock(&pag->pag_ici_lock); |
358 | radix_tree_preload_end(); | |
56e73ec4 DC |
359 | if (lock_flags) |
360 | xfs_iunlock(ip, lock_flags); | |
6441e549 | 361 | out_destroy: |
b36ec042 CH |
362 | __destroy_inode(VFS_I(ip)); |
363 | xfs_inode_free(ip); | |
6441e549 DC |
364 | return error; |
365 | } | |
366 | ||
367 | /* | |
368 | * Look up an inode by number in the given file system. | |
369 | * The inode is looked up in the cache held in each AG. | |
bf904248 DC |
370 | * If the inode is found in the cache, initialise the vfs inode |
371 | * if necessary. | |
6441e549 DC |
372 | * |
373 | * If it is not in core, read it in from the file system's device, | |
bf904248 | 374 | * add it to the cache and initialise the vfs inode. |
6441e549 DC |
375 | * |
376 | * The inode is locked according to the value of the lock_flags parameter. | |
377 | * This flag parameter indicates how and if the inode's IO lock and inode lock | |
378 | * should be taken. | |
379 | * | |
380 | * mp -- the mount point structure for the current file system. It points | |
381 | * to the inode hash table. | |
382 | * tp -- a pointer to the current transaction if there is one. This is | |
383 | * simply passed through to the xfs_iread() call. | |
384 | * ino -- the number of the inode desired. This is the unique identifier | |
385 | * within the file system for the inode being requested. | |
386 | * lock_flags -- flags indicating how to lock the inode. See the comment | |
387 | * for xfs_ilock() for a list of valid values. | |
388 | * bno -- the block number starting the buffer containing the inode, | |
389 | * if known (as by bulkstat), else 0. | |
390 | */ | |
bf904248 DC |
391 | int |
392 | xfs_iget( | |
6441e549 DC |
393 | xfs_mount_t *mp, |
394 | xfs_trans_t *tp, | |
395 | xfs_ino_t ino, | |
396 | uint flags, | |
397 | uint lock_flags, | |
398 | xfs_inode_t **ipp, | |
399 | xfs_daddr_t bno) | |
400 | { | |
401 | xfs_inode_t *ip; | |
402 | int error; | |
403 | xfs_perag_t *pag; | |
404 | xfs_agino_t agino; | |
405 | ||
406 | /* the radix tree exists only in inode capable AGs */ | |
407 | if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi) | |
408 | return EINVAL; | |
409 | ||
410 | /* get the perag structure and ensure that it's inode capable */ | |
411 | pag = xfs_get_perag(mp, ino); | |
412 | if (!pag->pagi_inodeok) | |
413 | return EINVAL; | |
414 | ASSERT(pag->pag_ici_init); | |
415 | agino = XFS_INO_TO_AGINO(mp, ino); | |
416 | ||
417 | again: | |
418 | error = 0; | |
419 | read_lock(&pag->pag_ici_lock); | |
420 | ip = radix_tree_lookup(&pag->pag_ici_root, agino); | |
421 | ||
422 | if (ip) { | |
bf904248 | 423 | error = xfs_iget_cache_hit(pag, ip, flags, lock_flags); |
6441e549 DC |
424 | if (error) |
425 | goto out_error_or_again; | |
426 | } else { | |
427 | read_unlock(&pag->pag_ici_lock); | |
428 | XFS_STATS_INC(xs_ig_missed); | |
429 | ||
430 | error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno, | |
431 | flags, lock_flags); | |
432 | if (error) | |
433 | goto out_error_or_again; | |
434 | } | |
da353b0d | 435 | xfs_put_perag(mp, pag); |
1da177e4 | 436 | |
1da177e4 LT |
437 | *ipp = ip; |
438 | ||
bf904248 DC |
439 | ASSERT(ip->i_df.if_ext_max == |
440 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t)); | |
1da177e4 LT |
441 | /* |
442 | * If we have a real type for an on-disk inode, we can set ops(&unlock) | |
443 | * now. If it's a new inode being created, xfs_ialloc will handle it. | |
444 | */ | |
bf904248 | 445 | if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0) |
41be8bed | 446 | xfs_setup_inode(ip); |
1da177e4 | 447 | return 0; |
6441e549 DC |
448 | |
449 | out_error_or_again: | |
450 | if (error == EAGAIN) { | |
451 | delay(1); | |
452 | goto again; | |
453 | } | |
454 | xfs_put_perag(mp, pag); | |
455 | return error; | |
1da177e4 LT |
456 | } |
457 | ||
1da177e4 LT |
458 | /* |
459 | * Decrement reference count of an inode structure and unlock it. | |
460 | * | |
461 | * ip -- the inode being released | |
462 | * lock_flags -- this parameter indicates the inode's locks to be | |
463 | * to be released. See the comment on xfs_iunlock() for a list | |
464 | * of valid values. | |
465 | */ | |
466 | void | |
467 | xfs_iput(xfs_inode_t *ip, | |
468 | uint lock_flags) | |
469 | { | |
cf441eeb | 470 | xfs_itrace_entry(ip); |
1da177e4 | 471 | xfs_iunlock(ip, lock_flags); |
10090be2 | 472 | IRELE(ip); |
1da177e4 LT |
473 | } |
474 | ||
475 | /* | |
476 | * Special iput for brand-new inodes that are still locked | |
477 | */ | |
478 | void | |
01651646 DC |
479 | xfs_iput_new( |
480 | xfs_inode_t *ip, | |
481 | uint lock_flags) | |
1da177e4 | 482 | { |
01651646 | 483 | struct inode *inode = VFS_I(ip); |
1da177e4 | 484 | |
cf441eeb | 485 | xfs_itrace_entry(ip); |
1da177e4 LT |
486 | |
487 | if ((ip->i_d.di_mode == 0)) { | |
7a18c386 | 488 | ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE)); |
10090be2 | 489 | make_bad_inode(inode); |
1da177e4 LT |
490 | } |
491 | if (inode->i_state & I_NEW) | |
492 | unlock_new_inode(inode); | |
493 | if (lock_flags) | |
494 | xfs_iunlock(ip, lock_flags); | |
10090be2 | 495 | IRELE(ip); |
1da177e4 LT |
496 | } |
497 | ||
1da177e4 | 498 | /* |
5cafdeb2 CH |
499 | * This is called free all the memory associated with an inode. |
500 | * It must free the inode itself and any buffers allocated for | |
501 | * if_extents/if_data and if_broot. It must also free the lock | |
502 | * associated with the inode. | |
503 | * | |
504 | * Note: because we don't initialise everything on reallocation out | |
505 | * of the zone, we must ensure we nullify everything correctly before | |
506 | * freeing the structure. | |
1da177e4 LT |
507 | */ |
508 | void | |
5cafdeb2 CH |
509 | xfs_ireclaim( |
510 | struct xfs_inode *ip) | |
1da177e4 | 511 | { |
5cafdeb2 CH |
512 | struct xfs_mount *mp = ip->i_mount; |
513 | struct xfs_perag *pag; | |
1da177e4 | 514 | |
5cafdeb2 | 515 | XFS_STATS_INC(xs_ig_reclaims); |
1da177e4 LT |
516 | |
517 | /* | |
5cafdeb2 CH |
518 | * Remove the inode from the per-AG radix tree. It doesn't matter |
519 | * if it was never added to it because radix_tree_delete can deal | |
520 | * with that case just fine. | |
1da177e4 | 521 | */ |
5cafdeb2 CH |
522 | pag = xfs_get_perag(mp, ip->i_ino); |
523 | write_lock(&pag->pag_ici_lock); | |
524 | radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino)); | |
525 | write_unlock(&pag->pag_ici_lock); | |
526 | xfs_put_perag(mp, pag); | |
1da177e4 LT |
527 | |
528 | /* | |
5cafdeb2 CH |
529 | * Here we do an (almost) spurious inode lock in order to coordinate |
530 | * with inode cache radix tree lookups. This is because the lookup | |
531 | * can reference the inodes in the cache without taking references. | |
532 | * | |
533 | * We make that OK here by ensuring that we wait until the inode is | |
534 | * unlocked after the lookup before we go ahead and free it. We get | |
535 | * both the ilock and the iolock because the code may need to drop the | |
536 | * ilock one but will still hold the iolock. | |
1da177e4 | 537 | */ |
5cafdeb2 | 538 | xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
7d095257 | 539 | xfs_qm_dqdetach(ip); |
439b8434 | 540 | xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
1da177e4 | 541 | |
b36ec042 | 542 | xfs_inode_free(ip); |
1da177e4 LT |
543 | } |
544 | ||
545 | /* | |
546 | * This is a wrapper routine around the xfs_ilock() routine | |
547 | * used to centralize some grungy code. It is used in places | |
548 | * that wish to lock the inode solely for reading the extents. | |
549 | * The reason these places can't just call xfs_ilock(SHARED) | |
550 | * is that the inode lock also guards to bringing in of the | |
551 | * extents from disk for a file in b-tree format. If the inode | |
552 | * is in b-tree format, then we need to lock the inode exclusively | |
553 | * until the extents are read in. Locking it exclusively all | |
554 | * the time would limit our parallelism unnecessarily, though. | |
555 | * What we do instead is check to see if the extents have been | |
556 | * read in yet, and only lock the inode exclusively if they | |
557 | * have not. | |
558 | * | |
559 | * The function returns a value which should be given to the | |
560 | * corresponding xfs_iunlock_map_shared(). This value is | |
561 | * the mode in which the lock was actually taken. | |
562 | */ | |
563 | uint | |
564 | xfs_ilock_map_shared( | |
565 | xfs_inode_t *ip) | |
566 | { | |
567 | uint lock_mode; | |
568 | ||
569 | if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) && | |
570 | ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) { | |
571 | lock_mode = XFS_ILOCK_EXCL; | |
572 | } else { | |
573 | lock_mode = XFS_ILOCK_SHARED; | |
574 | } | |
575 | ||
576 | xfs_ilock(ip, lock_mode); | |
577 | ||
578 | return lock_mode; | |
579 | } | |
580 | ||
581 | /* | |
582 | * This is simply the unlock routine to go with xfs_ilock_map_shared(). | |
583 | * All it does is call xfs_iunlock() with the given lock_mode. | |
584 | */ | |
585 | void | |
586 | xfs_iunlock_map_shared( | |
587 | xfs_inode_t *ip, | |
588 | unsigned int lock_mode) | |
589 | { | |
590 | xfs_iunlock(ip, lock_mode); | |
591 | } | |
592 | ||
593 | /* | |
594 | * The xfs inode contains 2 locks: a multi-reader lock called the | |
595 | * i_iolock and a multi-reader lock called the i_lock. This routine | |
596 | * allows either or both of the locks to be obtained. | |
597 | * | |
598 | * The 2 locks should always be ordered so that the IO lock is | |
599 | * obtained first in order to prevent deadlock. | |
600 | * | |
601 | * ip -- the inode being locked | |
602 | * lock_flags -- this parameter indicates the inode's locks | |
603 | * to be locked. It can be: | |
604 | * XFS_IOLOCK_SHARED, | |
605 | * XFS_IOLOCK_EXCL, | |
606 | * XFS_ILOCK_SHARED, | |
607 | * XFS_ILOCK_EXCL, | |
608 | * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, | |
609 | * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, | |
610 | * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, | |
611 | * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL | |
612 | */ | |
613 | void | |
579aa9ca CH |
614 | xfs_ilock( |
615 | xfs_inode_t *ip, | |
616 | uint lock_flags) | |
1da177e4 LT |
617 | { |
618 | /* | |
619 | * You can't set both SHARED and EXCL for the same lock, | |
620 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
621 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
622 | */ | |
623 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
624 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
625 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | |
626 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
f7c66ce3 | 627 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
1da177e4 | 628 | |
579aa9ca | 629 | if (lock_flags & XFS_IOLOCK_EXCL) |
f7c66ce3 | 630 | mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
579aa9ca | 631 | else if (lock_flags & XFS_IOLOCK_SHARED) |
f7c66ce3 | 632 | mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
579aa9ca CH |
633 | |
634 | if (lock_flags & XFS_ILOCK_EXCL) | |
f7c66ce3 | 635 | mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
579aa9ca | 636 | else if (lock_flags & XFS_ILOCK_SHARED) |
f7c66ce3 | 637 | mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
579aa9ca | 638 | |
1da177e4 LT |
639 | xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address); |
640 | } | |
641 | ||
642 | /* | |
643 | * This is just like xfs_ilock(), except that the caller | |
644 | * is guaranteed not to sleep. It returns 1 if it gets | |
645 | * the requested locks and 0 otherwise. If the IO lock is | |
646 | * obtained but the inode lock cannot be, then the IO lock | |
647 | * is dropped before returning. | |
648 | * | |
649 | * ip -- the inode being locked | |
650 | * lock_flags -- this parameter indicates the inode's locks to be | |
651 | * to be locked. See the comment for xfs_ilock() for a list | |
652 | * of valid values. | |
1da177e4 LT |
653 | */ |
654 | int | |
579aa9ca CH |
655 | xfs_ilock_nowait( |
656 | xfs_inode_t *ip, | |
657 | uint lock_flags) | |
1da177e4 | 658 | { |
1da177e4 LT |
659 | /* |
660 | * You can't set both SHARED and EXCL for the same lock, | |
661 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
662 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
663 | */ | |
664 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
665 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
666 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | |
667 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
f7c66ce3 | 668 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
1da177e4 | 669 | |
1da177e4 | 670 | if (lock_flags & XFS_IOLOCK_EXCL) { |
579aa9ca CH |
671 | if (!mrtryupdate(&ip->i_iolock)) |
672 | goto out; | |
1da177e4 | 673 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
579aa9ca CH |
674 | if (!mrtryaccess(&ip->i_iolock)) |
675 | goto out; | |
1da177e4 LT |
676 | } |
677 | if (lock_flags & XFS_ILOCK_EXCL) { | |
579aa9ca CH |
678 | if (!mrtryupdate(&ip->i_lock)) |
679 | goto out_undo_iolock; | |
1da177e4 | 680 | } else if (lock_flags & XFS_ILOCK_SHARED) { |
579aa9ca CH |
681 | if (!mrtryaccess(&ip->i_lock)) |
682 | goto out_undo_iolock; | |
1da177e4 LT |
683 | } |
684 | xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address); | |
685 | return 1; | |
579aa9ca CH |
686 | |
687 | out_undo_iolock: | |
688 | if (lock_flags & XFS_IOLOCK_EXCL) | |
689 | mrunlock_excl(&ip->i_iolock); | |
690 | else if (lock_flags & XFS_IOLOCK_SHARED) | |
691 | mrunlock_shared(&ip->i_iolock); | |
692 | out: | |
693 | return 0; | |
1da177e4 LT |
694 | } |
695 | ||
696 | /* | |
697 | * xfs_iunlock() is used to drop the inode locks acquired with | |
698 | * xfs_ilock() and xfs_ilock_nowait(). The caller must pass | |
699 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so | |
700 | * that we know which locks to drop. | |
701 | * | |
702 | * ip -- the inode being unlocked | |
703 | * lock_flags -- this parameter indicates the inode's locks to be | |
704 | * to be unlocked. See the comment for xfs_ilock() for a list | |
705 | * of valid values for this parameter. | |
706 | * | |
707 | */ | |
708 | void | |
579aa9ca CH |
709 | xfs_iunlock( |
710 | xfs_inode_t *ip, | |
711 | uint lock_flags) | |
1da177e4 LT |
712 | { |
713 | /* | |
714 | * You can't set both SHARED and EXCL for the same lock, | |
715 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, | |
716 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. | |
717 | */ | |
718 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != | |
719 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); | |
720 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != | |
721 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); | |
f7c66ce3 LM |
722 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY | |
723 | XFS_LOCK_DEP_MASK)) == 0); | |
1da177e4 LT |
724 | ASSERT(lock_flags != 0); |
725 | ||
579aa9ca CH |
726 | if (lock_flags & XFS_IOLOCK_EXCL) |
727 | mrunlock_excl(&ip->i_iolock); | |
728 | else if (lock_flags & XFS_IOLOCK_SHARED) | |
729 | mrunlock_shared(&ip->i_iolock); | |
1da177e4 | 730 | |
579aa9ca CH |
731 | if (lock_flags & XFS_ILOCK_EXCL) |
732 | mrunlock_excl(&ip->i_lock); | |
733 | else if (lock_flags & XFS_ILOCK_SHARED) | |
734 | mrunlock_shared(&ip->i_lock); | |
1da177e4 | 735 | |
579aa9ca CH |
736 | if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) && |
737 | !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) { | |
1da177e4 LT |
738 | /* |
739 | * Let the AIL know that this item has been unlocked in case | |
740 | * it is in the AIL and anyone is waiting on it. Don't do | |
741 | * this if the caller has asked us not to. | |
742 | */ | |
783a2f65 | 743 | xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp, |
579aa9ca | 744 | (xfs_log_item_t*)(ip->i_itemp)); |
1da177e4 LT |
745 | } |
746 | xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address); | |
747 | } | |
748 | ||
749 | /* | |
750 | * give up write locks. the i/o lock cannot be held nested | |
751 | * if it is being demoted. | |
752 | */ | |
753 | void | |
579aa9ca CH |
754 | xfs_ilock_demote( |
755 | xfs_inode_t *ip, | |
756 | uint lock_flags) | |
1da177e4 LT |
757 | { |
758 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); | |
759 | ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); | |
760 | ||
579aa9ca | 761 | if (lock_flags & XFS_ILOCK_EXCL) |
1da177e4 | 762 | mrdemote(&ip->i_lock); |
579aa9ca | 763 | if (lock_flags & XFS_IOLOCK_EXCL) |
1da177e4 | 764 | mrdemote(&ip->i_iolock); |
579aa9ca CH |
765 | } |
766 | ||
767 | #ifdef DEBUG | |
768 | /* | |
769 | * Debug-only routine, without additional rw_semaphore APIs, we can | |
770 | * now only answer requests regarding whether we hold the lock for write | |
771 | * (reader state is outside our visibility, we only track writer state). | |
772 | * | |
773 | * Note: this means !xfs_isilocked would give false positives, so don't do that. | |
774 | */ | |
775 | int | |
776 | xfs_isilocked( | |
777 | xfs_inode_t *ip, | |
778 | uint lock_flags) | |
779 | { | |
780 | if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) == | |
781 | XFS_ILOCK_EXCL) { | |
782 | if (!ip->i_lock.mr_writer) | |
783 | return 0; | |
1da177e4 | 784 | } |
579aa9ca CH |
785 | |
786 | if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) == | |
787 | XFS_IOLOCK_EXCL) { | |
788 | if (!ip->i_iolock.mr_writer) | |
789 | return 0; | |
790 | } | |
791 | ||
792 | return 1; | |
1da177e4 | 793 | } |
579aa9ca | 794 | #endif |
1da177e4 | 795 | |
5a8d0f3c CH |
796 | #ifdef XFS_INODE_TRACE |
797 | ||
798 | #define KTRACE_ENTER(ip, vk, s, line, ra) \ | |
799 | ktrace_enter((ip)->i_trace, \ | |
800 | /* 0 */ (void *)(__psint_t)(vk), \ | |
801 | /* 1 */ (void *)(s), \ | |
802 | /* 2 */ (void *)(__psint_t) line, \ | |
803 | /* 3 */ (void *)(__psint_t)atomic_read(&VFS_I(ip)->i_count), \ | |
804 | /* 4 */ (void *)(ra), \ | |
805 | /* 5 */ NULL, \ | |
806 | /* 6 */ (void *)(__psint_t)current_cpu(), \ | |
807 | /* 7 */ (void *)(__psint_t)current_pid(), \ | |
808 | /* 8 */ (void *)__return_address, \ | |
809 | /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL) | |
810 | ||
811 | /* | |
812 | * Vnode tracing code. | |
813 | */ | |
814 | void | |
815 | _xfs_itrace_entry(xfs_inode_t *ip, const char *func, inst_t *ra) | |
816 | { | |
817 | KTRACE_ENTER(ip, INODE_KTRACE_ENTRY, func, 0, ra); | |
818 | } | |
819 | ||
820 | void | |
821 | _xfs_itrace_exit(xfs_inode_t *ip, const char *func, inst_t *ra) | |
822 | { | |
823 | KTRACE_ENTER(ip, INODE_KTRACE_EXIT, func, 0, ra); | |
824 | } | |
825 | ||
826 | void | |
827 | xfs_itrace_hold(xfs_inode_t *ip, char *file, int line, inst_t *ra) | |
828 | { | |
829 | KTRACE_ENTER(ip, INODE_KTRACE_HOLD, file, line, ra); | |
830 | } | |
831 | ||
832 | void | |
833 | _xfs_itrace_ref(xfs_inode_t *ip, char *file, int line, inst_t *ra) | |
834 | { | |
835 | KTRACE_ENTER(ip, INODE_KTRACE_REF, file, line, ra); | |
836 | } | |
837 | ||
838 | void | |
839 | xfs_itrace_rele(xfs_inode_t *ip, char *file, int line, inst_t *ra) | |
840 | { | |
841 | KTRACE_ENTER(ip, INODE_KTRACE_RELE, file, line, ra); | |
842 | } | |
843 | #endif /* XFS_INODE_TRACE */ |