blob: 1562ac2dd67ca6ced73e85c523ef1046d5016e05 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_quota.h"
40#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
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 */
49void
50xfs_ihash_init(xfs_mount_t *mp)
51{
52 __uint64_t icount;
Nathan Scott77e46352006-09-28 11:03:27 +100053 uint i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
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 Scott77e46352006-09-28 11:03:27 +100064 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 Torvalds1da177e2005-04-16 15:20:36 -070070 rwlock_init(&(mp->m_ihash[i].ih_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73/*
74 * Free up structures allocated by xfs_ihash_init, at unmount time.
75 */
76void
77xfs_ihash_free(xfs_mount_t *mp)
78{
Nathan Scott77e46352006-09-28 11:03:27 +100079 kmem_free(mp->m_ihash, mp->m_ihsize * sizeof(xfs_ihash_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 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 */
87void
88xfs_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 Scottefb8ad72006-09-28 11:03:05 +100097 KM_SLEEP | KM_LARGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 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 */
106void
107xfs_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 Scott71bce252005-05-05 13:23:27 -0700120 * 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 */
126STATIC void
127xfs_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 Torvalds1da177e2005-04-16 15:20:36 -0700154 * 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 */
184STATIC int
185xfs_iget_core(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000186 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 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 Scott67fcaa72006-06-09 17:00:52 +1000198 bhv_vnode_t *inode_vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 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
209again:
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 */
218 if (ip->i_flags & XFS_INEW) {
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 */
233 if (ip->i_flags & XFS_IRECLAIM) {
234 read_unlock(&ih->ih_lock);
235 delay(1);
236 XFS_STATS_INC(xs_ig_frecycle);
237
238 goto again;
239 }
240
241 vn_trace_exit(vp, "xfs_iget.alloc",
242 (inst_t *)__return_address);
243
244 XFS_STATS_INC(xs_ig_found);
245
David Chinnerf273ab82006-09-28 11:06:03 +1000246 spin_lock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 ip->i_flags &= ~XFS_IRECLAIMABLE;
David Chinnerf273ab82006-09-28 11:06:03 +1000248 spin_unlock(&ip->i_flags_lock);
Nathan Scott71bce252005-05-05 13:23:27 -0700249 version = ih->ih_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 read_unlock(&ih->ih_lock);
Nathan Scott71bce252005-05-05 13:23:27 -0700251 xfs_ihash_promote(ih, ip, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 XFS_MOUNT_ILOCK(mp);
254 list_del_init(&ip->i_reclaim);
255 XFS_MOUNT_IUNLOCK(mp);
256
257 goto finish_inode;
258
259 } else if (vp != inode_vp) {
Nathan Scottec86dc02006-03-17 17:25:36 +1100260 struct inode *inode = vn_to_inode(inode_vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 /* The inode is being torn down, pause and
263 * try again.
264 */
265 if (inode->i_state & (I_FREEING | I_CLEAR)) {
266 read_unlock(&ih->ih_lock);
267 delay(1);
268 XFS_STATS_INC(xs_ig_frecycle);
269
270 goto again;
271 }
272/* Chances are the other vnode (the one in the inode) is being torn
273 * down right now, and we landed on top of it. Question is, what do
274 * we do? Unhook the old inode and hook up the new one?
275 */
276 cmn_err(CE_PANIC,
277 "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
278 inode_vp, vp);
279 }
280
Nathan Scott71bce252005-05-05 13:23:27 -0700281 /*
282 * Inode cache hit: if ip is not at the front of
283 * its hash chain, move it there now.
284 * Do this with the lock held for update, but
285 * do statistics after releasing the lock.
286 */
287 version = ih->ih_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 read_unlock(&ih->ih_lock);
Nathan Scott71bce252005-05-05 13:23:27 -0700289 xfs_ihash_promote(ih, ip, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 XFS_STATS_INC(xs_ig_found);
291
292finish_inode:
293 if (ip->i_d.di_mode == 0) {
Nathan Scott745b1f472006-09-28 11:02:23 +1000294 if (!(flags & XFS_IGET_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return ENOENT;
296 xfs_iocore_inode_reinit(ip);
297 }
Nathan Scott745b1f472006-09-28 11:02:23 +1000298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (lock_flags != 0)
300 xfs_ilock(ip, lock_flags);
301
David Chinnerf273ab82006-09-28 11:06:03 +1000302 spin_lock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 ip->i_flags &= ~XFS_ISTALE;
David Chinnerf273ab82006-09-28 11:06:03 +1000304 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 vn_trace_exit(vp, "xfs_iget.found",
307 (inst_t *)__return_address);
308 goto return_ip;
309 }
310 }
311
312 /*
313 * Inode cache miss: save the hash chain version stamp and unlock
314 * the chain, so we don't deadlock in vn_alloc.
315 */
316 XFS_STATS_INC(xs_ig_missed);
317
318 version = ih->ih_version;
319
320 read_unlock(&ih->ih_lock);
321
322 /*
323 * Read the disk inode attributes into a new inode structure and get
324 * a new vnode for it. This should also initialize i_ino and i_mount.
325 */
Nathan Scott745b1f472006-09-28 11:02:23 +1000326 error = xfs_iread(mp, tp, ino, &ip, bno,
327 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
328 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
332
333 xfs_inode_lock_init(ip, vp);
334 xfs_iocore_inode_init(ip);
335
Nathan Scott745b1f472006-09-28 11:02:23 +1000336 if (lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 xfs_ilock(ip, lock_flags);
Nathan Scott745b1f472006-09-28 11:02:23 +1000338
339 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 xfs_idestroy(ip);
341 return ENOENT;
342 }
343
344 /*
345 * Put ip on its hash chain, unless someone else hashed a duplicate
346 * after we released the hash lock.
347 */
348 write_lock(&ih->ih_lock);
349
350 if (ih->ih_version != version) {
351 for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) {
352 if (iq->i_ino == ino) {
353 write_unlock(&ih->ih_lock);
354 xfs_idestroy(ip);
355
356 XFS_STATS_INC(xs_ig_dup);
357 goto again;
358 }
359 }
360 }
361
362 /*
363 * These values _must_ be set before releasing ihlock!
364 */
365 ip->i_hash = ih;
366 if ((iq = ih->ih_next)) {
367 iq->i_prevp = &ip->i_next;
368 }
369 ip->i_next = iq;
370 ip->i_prevp = &ih->ih_next;
371 ih->ih_next = ip;
372 ip->i_udquot = ip->i_gdquot = NULL;
373 ih->ih_version++;
David Chinnerf273ab82006-09-28 11:06:03 +1000374 spin_lock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 ip->i_flags |= XFS_INEW;
David Chinnerf273ab82006-09-28 11:06:03 +1000376 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 write_unlock(&ih->ih_lock);
379
380 /*
381 * put ip on its cluster's hash chain
382 */
383 ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
384 ip->i_cnext == NULL);
385
386 chlnew = NULL;
387 ch = XFS_CHASH(mp, ip->i_blkno);
388 chlredo:
389 s = mutex_spinlock(&ch->ch_lock);
390 for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
391 if (chl->chl_blkno == ip->i_blkno) {
392
393 /* insert this inode into the doubly-linked list
394 * where chl points */
395 if ((iq = chl->chl_ip)) {
396 ip->i_cprev = iq->i_cprev;
397 iq->i_cprev->i_cnext = ip;
398 iq->i_cprev = ip;
399 ip->i_cnext = iq;
400 } else {
401 ip->i_cnext = ip;
402 ip->i_cprev = ip;
403 }
404 chl->chl_ip = ip;
405 ip->i_chash = chl;
406 break;
407 }
408 }
409
410 /* no hash list found for this block; add a new hash list */
411 if (chl == NULL) {
412 if (chlnew == NULL) {
413 mutex_spinunlock(&ch->ch_lock, s);
414 ASSERT(xfs_chashlist_zone != NULL);
415 chlnew = (xfs_chashlist_t *)
416 kmem_zone_alloc(xfs_chashlist_zone,
417 KM_SLEEP);
418 ASSERT(chlnew != NULL);
419 goto chlredo;
420 } else {
421 ip->i_cnext = ip;
422 ip->i_cprev = ip;
423 ip->i_chash = chlnew;
424 chlnew->chl_ip = ip;
425 chlnew->chl_blkno = ip->i_blkno;
David Chinner1fc5d952006-04-11 15:11:12 +1000426 if (ch->ch_list)
427 ch->ch_list->chl_prev = chlnew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 chlnew->chl_next = ch->ch_list;
David Chinner1fc5d952006-04-11 15:11:12 +1000429 chlnew->chl_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ch->ch_list = chlnew;
431 chlnew = NULL;
432 }
433 } else {
434 if (chlnew != NULL) {
435 kmem_zone_free(xfs_chashlist_zone, chlnew);
436 }
437 }
438
439 mutex_spinunlock(&ch->ch_lock, s);
440
441
442 /*
443 * Link ip to its mount and thread it on the mount's inode list.
444 */
445 XFS_MOUNT_ILOCK(mp);
446 if ((iq = mp->m_inodes)) {
447 ASSERT(iq->i_mprev->i_mnext == iq);
448 ip->i_mprev = iq->i_mprev;
449 iq->i_mprev->i_mnext = ip;
450 iq->i_mprev = ip;
451 ip->i_mnext = iq;
452 } else {
453 ip->i_mnext = ip;
454 ip->i_mprev = ip;
455 }
456 mp->m_inodes = ip;
457
458 XFS_MOUNT_IUNLOCK(mp);
459
460 return_ip:
461 ASSERT(ip->i_df.if_ext_max ==
462 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
463
464 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
465 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
466
467 *ipp = ip;
468
469 /*
470 * If we have a real type for an on-disk inode, we can set ops(&unlock)
471 * now. If it's a new inode being created, xfs_ialloc will handle it.
472 */
Nathan Scottb83bd132006-06-09 16:48:30 +1000473 bhv_vfs_init_vnode(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 return 0;
476}
477
478
479/*
480 * The 'normal' internal xfs_iget, if needed it will
481 * 'allocate', or 'get', the vnode.
482 */
483int
484xfs_iget(
485 xfs_mount_t *mp,
486 xfs_trans_t *tp,
487 xfs_ino_t ino,
488 uint flags,
489 uint lock_flags,
490 xfs_inode_t **ipp,
491 xfs_daddr_t bno)
492{
493 struct inode *inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000494 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int error;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 XFS_STATS_INC(xs_ig_attempts);
498
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000499retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Nathan Scottec86dc02006-03-17 17:25:36 +1100503 vp = vn_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (inode->i_state & I_NEW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 vn_initialize(inode);
506 error = xfs_iget_core(vp, mp, tp, ino, flags,
507 lock_flags, ipp, bno);
508 if (error) {
509 vn_mark_bad(vp);
510 if (inode->i_state & I_NEW)
511 unlock_new_inode(inode);
512 iput(inode);
513 }
514 } else {
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000515 /*
516 * If the inode is not fully constructed due to
Nathan Scottc41564b2006-03-29 08:55:14 +1000517 * filehandle mismatches wait for the inode to go
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000518 * away and try again.
519 *
520 * iget_locked will call __wait_on_freeing_inode
521 * to wait for the inode to go away.
522 */
523 if (is_bad_inode(inode) ||
Christoph Hellwig75e17b32006-01-11 20:58:44 +1100524 ((ip = xfs_vtoi(vp)) == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 iput(inode);
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000526 delay(1);
527 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (lock_flags != 0)
531 xfs_ilock(ip, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 XFS_STATS_INC(xs_ig_found);
533 *ipp = ip;
534 error = 0;
535 }
536 } else
537 error = ENOMEM; /* If we got no inode we are out of memory */
538
539 return error;
540}
541
542/*
543 * Do the setup for the various locks within the incore inode.
544 */
545void
546xfs_inode_lock_init(
547 xfs_inode_t *ip,
Nathan Scott67fcaa72006-06-09 17:00:52 +1000548 bhv_vnode_t *vp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
551 "xfsino", (long)vp->v_number);
552 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number);
553 init_waitqueue_head(&ip->i_ipin_wait);
554 atomic_set(&ip->i_pincount, 0);
Eric Sandeen7ae67d72006-09-28 11:05:46 +1000555 initnsema(&ip->i_flock, 1, "xfsfino");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558/*
559 * Look for the inode corresponding to the given ino in the hash table.
560 * If it is there and its i_transp pointer matches tp, return it.
561 * Otherwise, return NULL.
562 */
563xfs_inode_t *
564xfs_inode_incore(xfs_mount_t *mp,
565 xfs_ino_t ino,
566 xfs_trans_t *tp)
567{
568 xfs_ihash_t *ih;
569 xfs_inode_t *ip;
Nathan Scott71bce252005-05-05 13:23:27 -0700570 ulong version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 ih = XFS_IHASH(mp, ino);
573 read_lock(&ih->ih_lock);
574 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
575 if (ip->i_ino == ino) {
576 /*
577 * If we find it and tp matches, return it.
Nathan Scott71bce252005-05-05 13:23:27 -0700578 * Also move it to the front of the hash list
579 * if we find it and it is not already there.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * Otherwise break from the loop and return
581 * NULL.
582 */
583 if (ip->i_transp == tp) {
Nathan Scott71bce252005-05-05 13:23:27 -0700584 version = ih->ih_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 read_unlock(&ih->ih_lock);
Nathan Scott71bce252005-05-05 13:23:27 -0700586 xfs_ihash_promote(ih, ip, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return (ip);
588 }
589 break;
590 }
591 }
592 read_unlock(&ih->ih_lock);
593 return (NULL);
594}
595
596/*
597 * Decrement reference count of an inode structure and unlock it.
598 *
599 * ip -- the inode being released
600 * lock_flags -- this parameter indicates the inode's locks to be
601 * to be released. See the comment on xfs_iunlock() for a list
602 * of valid values.
603 */
604void
605xfs_iput(xfs_inode_t *ip,
606 uint lock_flags)
607{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000608 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 xfs_iunlock(ip, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 VN_RELE(vp);
613}
614
615/*
616 * Special iput for brand-new inodes that are still locked
617 */
618void
619xfs_iput_new(xfs_inode_t *ip,
620 uint lock_flags)
621{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000622 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scottec86dc02006-03-17 17:25:36 +1100623 struct inode *inode = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address);
626
627 if ((ip->i_d.di_mode == 0)) {
628 ASSERT(!(ip->i_flags & XFS_IRECLAIMABLE));
629 vn_mark_bad(vp);
630 }
631 if (inode->i_state & I_NEW)
632 unlock_new_inode(inode);
633 if (lock_flags)
634 xfs_iunlock(ip, lock_flags);
635 VN_RELE(vp);
636}
637
638
639/*
640 * This routine embodies the part of the reclaim code that pulls
641 * the inode from the inode hash table and the mount structure's
642 * inode list.
643 * This should only be called from xfs_reclaim().
644 */
645void
646xfs_ireclaim(xfs_inode_t *ip)
647{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000648 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /*
651 * Remove from old hash list and mount list.
652 */
653 XFS_STATS_INC(xs_ig_reclaims);
654
655 xfs_iextract(ip);
656
657 /*
658 * Here we do a spurious inode lock in order to coordinate with
659 * xfs_sync(). This is because xfs_sync() references the inodes
660 * in the mount list without taking references on the corresponding
661 * vnodes. We make that OK here by ensuring that we wait until
662 * the inode is unlocked in xfs_sync() before we go ahead and
663 * free it. We get both the regular lock and the io lock because
664 * the xfs_sync() code may need to drop the regular one but will
665 * still hold the io lock.
666 */
667 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
668
669 /*
670 * Release dquots (and their references) if any. An inode may escape
671 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
672 */
673 XFS_QM_DQDETACH(ip->i_mount, ip);
674
675 /*
676 * Pull our behavior descriptor from the vnode chain.
677 */
678 vp = XFS_ITOV_NULL(ip);
679 if (vp) {
680 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
681 }
682
683 /*
684 * Free all memory associated with the inode.
685 */
Tim Shimmin439b8432006-11-11 18:04:34 +1100686 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 xfs_idestroy(ip);
688}
689
690/*
691 * This routine removes an about-to-be-destroyed inode from
692 * all of the lists in which it is located with the exception
693 * of the behavior chain.
694 */
695void
696xfs_iextract(
697 xfs_inode_t *ip)
698{
699 xfs_ihash_t *ih;
700 xfs_inode_t *iq;
701 xfs_mount_t *mp;
702 xfs_chash_t *ch;
703 xfs_chashlist_t *chl, *chm;
704 SPLDECL(s);
705
706 ih = ip->i_hash;
707 write_lock(&ih->ih_lock);
708 if ((iq = ip->i_next)) {
709 iq->i_prevp = ip->i_prevp;
710 }
711 *ip->i_prevp = iq;
Nathan Scott71bce252005-05-05 13:23:27 -0700712 ih->ih_version++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 write_unlock(&ih->ih_lock);
714
715 /*
716 * Remove from cluster hash list
717 * 1) delete the chashlist if this is the last inode on the chashlist
718 * 2) unchain from list of inodes
719 * 3) point chashlist->chl_ip to 'chl_next' if to this inode.
720 */
721 mp = ip->i_mount;
722 ch = XFS_CHASH(mp, ip->i_blkno);
723 s = mutex_spinlock(&ch->ch_lock);
724
725 if (ip->i_cnext == ip) {
726 /* Last inode on chashlist */
727 ASSERT(ip->i_cnext == ip && ip->i_cprev == ip);
728 ASSERT(ip->i_chash != NULL);
729 chm=NULL;
David Chinner1fc5d952006-04-11 15:11:12 +1000730 chl = ip->i_chash;
731 if (chl->chl_prev)
732 chl->chl_prev->chl_next = chl->chl_next;
733 else
734 ch->ch_list = chl->chl_next;
735 if (chl->chl_next)
736 chl->chl_next->chl_prev = chl->chl_prev;
737 kmem_zone_free(xfs_chashlist_zone, chl);
738 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* delete one inode from a non-empty list */
740 iq = ip->i_cnext;
741 iq->i_cprev = ip->i_cprev;
742 ip->i_cprev->i_cnext = iq;
743 if (ip->i_chash->chl_ip == ip) {
744 ip->i_chash->chl_ip = iq;
745 }
746 ip->i_chash = __return_address;
747 ip->i_cprev = __return_address;
748 ip->i_cnext = __return_address;
749 }
750 mutex_spinunlock(&ch->ch_lock, s);
751
752 /*
753 * Remove from mount's inode list.
754 */
755 XFS_MOUNT_ILOCK(mp);
756 ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
757 iq = ip->i_mnext;
758 iq->i_mprev = ip->i_mprev;
759 ip->i_mprev->i_mnext = iq;
760
761 /*
762 * Fix up the head pointer if it points to the inode being deleted.
763 */
764 if (mp->m_inodes == ip) {
765 if (ip == iq) {
766 mp->m_inodes = NULL;
767 } else {
768 mp->m_inodes = iq;
769 }
770 }
771
772 /* Deal with the deleted inodes list */
773 list_del_init(&ip->i_reclaim);
774
775 mp->m_ireclaims++;
776 XFS_MOUNT_IUNLOCK(mp);
777}
778
779/*
780 * This is a wrapper routine around the xfs_ilock() routine
781 * used to centralize some grungy code. It is used in places
782 * that wish to lock the inode solely for reading the extents.
783 * The reason these places can't just call xfs_ilock(SHARED)
784 * is that the inode lock also guards to bringing in of the
785 * extents from disk for a file in b-tree format. If the inode
786 * is in b-tree format, then we need to lock the inode exclusively
787 * until the extents are read in. Locking it exclusively all
788 * the time would limit our parallelism unnecessarily, though.
789 * What we do instead is check to see if the extents have been
790 * read in yet, and only lock the inode exclusively if they
791 * have not.
792 *
793 * The function returns a value which should be given to the
794 * corresponding xfs_iunlock_map_shared(). This value is
795 * the mode in which the lock was actually taken.
796 */
797uint
798xfs_ilock_map_shared(
799 xfs_inode_t *ip)
800{
801 uint lock_mode;
802
803 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
804 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
805 lock_mode = XFS_ILOCK_EXCL;
806 } else {
807 lock_mode = XFS_ILOCK_SHARED;
808 }
809
810 xfs_ilock(ip, lock_mode);
811
812 return lock_mode;
813}
814
815/*
816 * This is simply the unlock routine to go with xfs_ilock_map_shared().
817 * All it does is call xfs_iunlock() with the given lock_mode.
818 */
819void
820xfs_iunlock_map_shared(
821 xfs_inode_t *ip,
822 unsigned int lock_mode)
823{
824 xfs_iunlock(ip, lock_mode);
825}
826
827/*
828 * The xfs inode contains 2 locks: a multi-reader lock called the
829 * i_iolock and a multi-reader lock called the i_lock. This routine
830 * allows either or both of the locks to be obtained.
831 *
832 * The 2 locks should always be ordered so that the IO lock is
833 * obtained first in order to prevent deadlock.
834 *
835 * ip -- the inode being locked
836 * lock_flags -- this parameter indicates the inode's locks
837 * to be locked. It can be:
838 * XFS_IOLOCK_SHARED,
839 * XFS_IOLOCK_EXCL,
840 * XFS_ILOCK_SHARED,
841 * XFS_ILOCK_EXCL,
842 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
843 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
844 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
845 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
846 */
847void
848xfs_ilock(xfs_inode_t *ip,
849 uint lock_flags)
850{
851 /*
852 * You can't set both SHARED and EXCL for the same lock,
853 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
854 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
855 */
856 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
857 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
858 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
859 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
860 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
861
862 if (lock_flags & XFS_IOLOCK_EXCL) {
863 mrupdate(&ip->i_iolock);
864 } else if (lock_flags & XFS_IOLOCK_SHARED) {
865 mraccess(&ip->i_iolock);
866 }
867 if (lock_flags & XFS_ILOCK_EXCL) {
868 mrupdate(&ip->i_lock);
869 } else if (lock_flags & XFS_ILOCK_SHARED) {
870 mraccess(&ip->i_lock);
871 }
872 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
873}
874
875/*
876 * This is just like xfs_ilock(), except that the caller
877 * is guaranteed not to sleep. It returns 1 if it gets
878 * the requested locks and 0 otherwise. If the IO lock is
879 * obtained but the inode lock cannot be, then the IO lock
880 * is dropped before returning.
881 *
882 * ip -- the inode being locked
883 * lock_flags -- this parameter indicates the inode's locks to be
884 * to be locked. See the comment for xfs_ilock() for a list
885 * of valid values.
886 *
887 */
888int
889xfs_ilock_nowait(xfs_inode_t *ip,
890 uint lock_flags)
891{
892 int iolocked;
893 int ilocked;
894
895 /*
896 * You can't set both SHARED and EXCL for the same lock,
897 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
898 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
899 */
900 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
901 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
902 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
903 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
904 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
905
906 iolocked = 0;
907 if (lock_flags & XFS_IOLOCK_EXCL) {
908 iolocked = mrtryupdate(&ip->i_iolock);
909 if (!iolocked) {
910 return 0;
911 }
912 } else if (lock_flags & XFS_IOLOCK_SHARED) {
913 iolocked = mrtryaccess(&ip->i_iolock);
914 if (!iolocked) {
915 return 0;
916 }
917 }
918 if (lock_flags & XFS_ILOCK_EXCL) {
919 ilocked = mrtryupdate(&ip->i_lock);
920 if (!ilocked) {
921 if (iolocked) {
922 mrunlock(&ip->i_iolock);
923 }
924 return 0;
925 }
926 } else if (lock_flags & XFS_ILOCK_SHARED) {
927 ilocked = mrtryaccess(&ip->i_lock);
928 if (!ilocked) {
929 if (iolocked) {
930 mrunlock(&ip->i_iolock);
931 }
932 return 0;
933 }
934 }
935 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
936 return 1;
937}
938
939/*
940 * xfs_iunlock() is used to drop the inode locks acquired with
941 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
942 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
943 * that we know which locks to drop.
944 *
945 * ip -- the inode being unlocked
946 * lock_flags -- this parameter indicates the inode's locks to be
947 * to be unlocked. See the comment for xfs_ilock() for a list
948 * of valid values for this parameter.
949 *
950 */
951void
952xfs_iunlock(xfs_inode_t *ip,
953 uint lock_flags)
954{
955 /*
956 * You can't set both SHARED and EXCL for the same lock,
957 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
958 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
959 */
960 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
961 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
962 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
963 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
964 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0);
965 ASSERT(lock_flags != 0);
966
967 if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
968 ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
969 (ismrlocked(&ip->i_iolock, MR_ACCESS)));
970 ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
971 (ismrlocked(&ip->i_iolock, MR_UPDATE)));
972 mrunlock(&ip->i_iolock);
973 }
974
975 if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
976 ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
977 (ismrlocked(&ip->i_lock, MR_ACCESS)));
978 ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
979 (ismrlocked(&ip->i_lock, MR_UPDATE)));
980 mrunlock(&ip->i_lock);
981
982 /*
983 * Let the AIL know that this item has been unlocked in case
984 * it is in the AIL and anyone is waiting on it. Don't do
985 * this if the caller has asked us not to.
986 */
987 if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
988 ip->i_itemp != NULL) {
989 xfs_trans_unlocked_item(ip->i_mount,
990 (xfs_log_item_t*)(ip->i_itemp));
991 }
992 }
993 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
994}
995
996/*
997 * give up write locks. the i/o lock cannot be held nested
998 * if it is being demoted.
999 */
1000void
1001xfs_ilock_demote(xfs_inode_t *ip,
1002 uint lock_flags)
1003{
1004 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
1005 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
1006
1007 if (lock_flags & XFS_ILOCK_EXCL) {
1008 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
1009 mrdemote(&ip->i_lock);
1010 }
1011 if (lock_flags & XFS_IOLOCK_EXCL) {
1012 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1013 mrdemote(&ip->i_iolock);
1014 }
1015}
1016
1017/*
1018 * The following three routines simply manage the i_flock
1019 * semaphore embedded in the inode. This semaphore synchronizes
1020 * processes attempting to flush the in-core inode back to disk.
1021 */
1022void
1023xfs_iflock(xfs_inode_t *ip)
1024{
1025 psema(&(ip->i_flock), PINOD|PLTWAIT);
1026}
1027
1028int
1029xfs_iflock_nowait(xfs_inode_t *ip)
1030{
1031 return (cpsema(&(ip->i_flock)));
1032}
1033
1034void
1035xfs_ifunlock(xfs_inode_t *ip)
1036{
Al Viro0d8fee32006-06-19 08:41:30 +10001037 ASSERT(issemalocked(&(ip->i_flock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 vsema(&(ip->i_flock));
1039}