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