blob: 4b0c1881d6d59a1a10e363e21f28167e7289981f [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 */
David Chinner7a18c382006-11-11 18:04:54 +1100218 if (xfs_iflags_test(ip, XFS_INEW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 read_unlock(&ih->ih_lock);
220 delay(1);
221 XFS_STATS_INC(xs_ig_frecycle);
222
223 goto again;
224 }
225
226 inode_vp = XFS_ITOV_NULL(ip);
227 if (inode_vp == NULL) {
228 /*
229 * If IRECLAIM is set this inode is
230 * on its way out of the system,
231 * we need to pause and try again.
232 */
David Chinner7a18c382006-11-11 18:04:54 +1100233 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 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 Chinner7a18c382006-11-11 18:04:54 +1100246 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
Nathan Scott71bce252005-05-05 13:23:27 -0700247 version = ih->ih_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 read_unlock(&ih->ih_lock);
Nathan Scott71bce252005-05-05 13:23:27 -0700249 xfs_ihash_promote(ih, ip, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 XFS_MOUNT_ILOCK(mp);
252 list_del_init(&ip->i_reclaim);
253 XFS_MOUNT_IUNLOCK(mp);
254
255 goto finish_inode;
256
257 } else if (vp != inode_vp) {
Nathan Scottec86dc02006-03-17 17:25:36 +1100258 struct inode *inode = vn_to_inode(inode_vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /* The inode is being torn down, pause and
261 * try again.
262 */
263 if (inode->i_state & (I_FREEING | I_CLEAR)) {
264 read_unlock(&ih->ih_lock);
265 delay(1);
266 XFS_STATS_INC(xs_ig_frecycle);
267
268 goto again;
269 }
270/* Chances are the other vnode (the one in the inode) is being torn
271 * down right now, and we landed on top of it. Question is, what do
272 * we do? Unhook the old inode and hook up the new one?
273 */
274 cmn_err(CE_PANIC,
275 "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
276 inode_vp, vp);
277 }
278
Nathan Scott71bce252005-05-05 13:23:27 -0700279 /*
280 * Inode cache hit: if ip is not at the front of
281 * its hash chain, move it there now.
282 * Do this with the lock held for update, but
283 * do statistics after releasing the lock.
284 */
285 version = ih->ih_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 read_unlock(&ih->ih_lock);
Nathan Scott71bce252005-05-05 13:23:27 -0700287 xfs_ihash_promote(ih, ip, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 XFS_STATS_INC(xs_ig_found);
289
290finish_inode:
291 if (ip->i_d.di_mode == 0) {
Nathan Scott745b1f472006-09-28 11:02:23 +1000292 if (!(flags & XFS_IGET_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return ENOENT;
294 xfs_iocore_inode_reinit(ip);
295 }
Nathan Scott745b1f472006-09-28 11:02:23 +1000296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (lock_flags != 0)
298 xfs_ilock(ip, lock_flags);
299
David Chinner7a18c382006-11-11 18:04:54 +1100300 xfs_iflags_clear(ip, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 vn_trace_exit(vp, "xfs_iget.found",
302 (inst_t *)__return_address);
303 goto return_ip;
304 }
305 }
306
307 /*
308 * Inode cache miss: save the hash chain version stamp and unlock
309 * the chain, so we don't deadlock in vn_alloc.
310 */
311 XFS_STATS_INC(xs_ig_missed);
312
313 version = ih->ih_version;
314
315 read_unlock(&ih->ih_lock);
316
317 /*
318 * Read the disk inode attributes into a new inode structure and get
319 * a new vnode for it. This should also initialize i_ino and i_mount.
320 */
Nathan Scott745b1f472006-09-28 11:02:23 +1000321 error = xfs_iread(mp, tp, ino, &ip, bno,
322 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
323 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
327
328 xfs_inode_lock_init(ip, vp);
329 xfs_iocore_inode_init(ip);
330
Nathan Scott745b1f472006-09-28 11:02:23 +1000331 if (lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xfs_ilock(ip, lock_flags);
Nathan Scott745b1f472006-09-28 11:02:23 +1000333
334 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 xfs_idestroy(ip);
336 return ENOENT;
337 }
338
339 /*
340 * Put ip on its hash chain, unless someone else hashed a duplicate
341 * after we released the hash lock.
342 */
343 write_lock(&ih->ih_lock);
344
345 if (ih->ih_version != version) {
346 for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) {
347 if (iq->i_ino == ino) {
348 write_unlock(&ih->ih_lock);
349 xfs_idestroy(ip);
350
351 XFS_STATS_INC(xs_ig_dup);
352 goto again;
353 }
354 }
355 }
356
357 /*
358 * These values _must_ be set before releasing ihlock!
359 */
360 ip->i_hash = ih;
361 if ((iq = ih->ih_next)) {
362 iq->i_prevp = &ip->i_next;
363 }
364 ip->i_next = iq;
365 ip->i_prevp = &ih->ih_next;
366 ih->ih_next = ip;
367 ip->i_udquot = ip->i_gdquot = NULL;
368 ih->ih_version++;
David Chinner7a18c382006-11-11 18:04:54 +1100369 xfs_iflags_set(ip, XFS_INEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 write_unlock(&ih->ih_lock);
371
372 /*
373 * put ip on its cluster's hash chain
374 */
375 ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
376 ip->i_cnext == NULL);
377
378 chlnew = NULL;
379 ch = XFS_CHASH(mp, ip->i_blkno);
380 chlredo:
381 s = mutex_spinlock(&ch->ch_lock);
382 for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
383 if (chl->chl_blkno == ip->i_blkno) {
384
385 /* insert this inode into the doubly-linked list
386 * where chl points */
387 if ((iq = chl->chl_ip)) {
388 ip->i_cprev = iq->i_cprev;
389 iq->i_cprev->i_cnext = ip;
390 iq->i_cprev = ip;
391 ip->i_cnext = iq;
392 } else {
393 ip->i_cnext = ip;
394 ip->i_cprev = ip;
395 }
396 chl->chl_ip = ip;
397 ip->i_chash = chl;
398 break;
399 }
400 }
401
402 /* no hash list found for this block; add a new hash list */
403 if (chl == NULL) {
404 if (chlnew == NULL) {
405 mutex_spinunlock(&ch->ch_lock, s);
406 ASSERT(xfs_chashlist_zone != NULL);
407 chlnew = (xfs_chashlist_t *)
408 kmem_zone_alloc(xfs_chashlist_zone,
409 KM_SLEEP);
410 ASSERT(chlnew != NULL);
411 goto chlredo;
412 } else {
413 ip->i_cnext = ip;
414 ip->i_cprev = ip;
415 ip->i_chash = chlnew;
416 chlnew->chl_ip = ip;
417 chlnew->chl_blkno = ip->i_blkno;
David Chinner1fc5d952006-04-11 15:11:12 +1000418 if (ch->ch_list)
419 ch->ch_list->chl_prev = chlnew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 chlnew->chl_next = ch->ch_list;
David Chinner1fc5d952006-04-11 15:11:12 +1000421 chlnew->chl_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 ch->ch_list = chlnew;
423 chlnew = NULL;
424 }
425 } else {
426 if (chlnew != NULL) {
427 kmem_zone_free(xfs_chashlist_zone, chlnew);
428 }
429 }
430
431 mutex_spinunlock(&ch->ch_lock, s);
432
433
434 /*
435 * Link ip to its mount and thread it on the mount's inode list.
436 */
437 XFS_MOUNT_ILOCK(mp);
438 if ((iq = mp->m_inodes)) {
439 ASSERT(iq->i_mprev->i_mnext == iq);
440 ip->i_mprev = iq->i_mprev;
441 iq->i_mprev->i_mnext = ip;
442 iq->i_mprev = ip;
443 ip->i_mnext = iq;
444 } else {
445 ip->i_mnext = ip;
446 ip->i_mprev = ip;
447 }
448 mp->m_inodes = ip;
449
450 XFS_MOUNT_IUNLOCK(mp);
451
452 return_ip:
453 ASSERT(ip->i_df.if_ext_max ==
454 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
455
456 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
457 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
458
459 *ipp = ip;
460
461 /*
462 * If we have a real type for an on-disk inode, we can set ops(&unlock)
463 * now. If it's a new inode being created, xfs_ialloc will handle it.
464 */
Nathan Scottb83bd132006-06-09 16:48:30 +1000465 bhv_vfs_init_vnode(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 return 0;
468}
469
470
471/*
472 * The 'normal' internal xfs_iget, if needed it will
473 * 'allocate', or 'get', the vnode.
474 */
475int
476xfs_iget(
477 xfs_mount_t *mp,
478 xfs_trans_t *tp,
479 xfs_ino_t ino,
480 uint flags,
481 uint lock_flags,
482 xfs_inode_t **ipp,
483 xfs_daddr_t bno)
484{
485 struct inode *inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000486 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int error;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 XFS_STATS_INC(xs_ig_attempts);
490
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000491retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Nathan Scottec86dc02006-03-17 17:25:36 +1100495 vp = vn_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (inode->i_state & I_NEW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 vn_initialize(inode);
498 error = xfs_iget_core(vp, mp, tp, ino, flags,
499 lock_flags, ipp, bno);
500 if (error) {
501 vn_mark_bad(vp);
502 if (inode->i_state & I_NEW)
503 unlock_new_inode(inode);
504 iput(inode);
505 }
506 } else {
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000507 /*
508 * If the inode is not fully constructed due to
Nathan Scottc41564b2006-03-29 08:55:14 +1000509 * filehandle mismatches wait for the inode to go
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000510 * away and try again.
511 *
512 * iget_locked will call __wait_on_freeing_inode
513 * to wait for the inode to go away.
514 */
515 if (is_bad_inode(inode) ||
Christoph Hellwig75e17b32006-01-11 20:58:44 +1100516 ((ip = xfs_vtoi(vp)) == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 iput(inode);
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000518 delay(1);
519 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (lock_flags != 0)
523 xfs_ilock(ip, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 XFS_STATS_INC(xs_ig_found);
525 *ipp = ip;
526 error = 0;
527 }
528 } else
529 error = ENOMEM; /* If we got no inode we are out of memory */
530
531 return error;
532}
533
534/*
535 * Do the setup for the various locks within the incore inode.
536 */
537void
538xfs_inode_lock_init(
539 xfs_inode_t *ip,
Nathan Scott67fcaa72006-06-09 17:00:52 +1000540 bhv_vnode_t *vp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
543 "xfsino", (long)vp->v_number);
544 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number);
545 init_waitqueue_head(&ip->i_ipin_wait);
546 atomic_set(&ip->i_pincount, 0);
Eric Sandeen7ae67d72006-09-28 11:05:46 +1000547 initnsema(&ip->i_flock, 1, "xfsfino");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/*
551 * Look for the inode corresponding to the given ino in the hash table.
552 * If it is there and its i_transp pointer matches tp, return it.
553 * Otherwise, return NULL.
554 */
555xfs_inode_t *
556xfs_inode_incore(xfs_mount_t *mp,
557 xfs_ino_t ino,
558 xfs_trans_t *tp)
559{
560 xfs_ihash_t *ih;
561 xfs_inode_t *ip;
Nathan Scott71bce252005-05-05 13:23:27 -0700562 ulong version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 ih = XFS_IHASH(mp, ino);
565 read_lock(&ih->ih_lock);
566 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
567 if (ip->i_ino == ino) {
568 /*
569 * If we find it and tp matches, return it.
Nathan Scott71bce252005-05-05 13:23:27 -0700570 * Also move it to the front of the hash list
571 * if we find it and it is not already there.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * Otherwise break from the loop and return
573 * NULL.
574 */
575 if (ip->i_transp == tp) {
Nathan Scott71bce252005-05-05 13:23:27 -0700576 version = ih->ih_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 read_unlock(&ih->ih_lock);
Nathan Scott71bce252005-05-05 13:23:27 -0700578 xfs_ihash_promote(ih, ip, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return (ip);
580 }
581 break;
582 }
583 }
584 read_unlock(&ih->ih_lock);
585 return (NULL);
586}
587
588/*
589 * Decrement reference count of an inode structure and unlock it.
590 *
591 * ip -- the inode being released
592 * lock_flags -- this parameter indicates the inode's locks to be
593 * to be released. See the comment on xfs_iunlock() for a list
594 * of valid values.
595 */
596void
597xfs_iput(xfs_inode_t *ip,
598 uint lock_flags)
599{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000600 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 xfs_iunlock(ip, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 VN_RELE(vp);
605}
606
607/*
608 * Special iput for brand-new inodes that are still locked
609 */
610void
611xfs_iput_new(xfs_inode_t *ip,
612 uint lock_flags)
613{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000614 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scottec86dc02006-03-17 17:25:36 +1100615 struct inode *inode = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address);
618
619 if ((ip->i_d.di_mode == 0)) {
David Chinner7a18c382006-11-11 18:04:54 +1100620 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 vn_mark_bad(vp);
622 }
623 if (inode->i_state & I_NEW)
624 unlock_new_inode(inode);
625 if (lock_flags)
626 xfs_iunlock(ip, lock_flags);
627 VN_RELE(vp);
628}
629
630
631/*
632 * This routine embodies the part of the reclaim code that pulls
633 * the inode from the inode hash table and the mount structure's
634 * inode list.
635 * This should only be called from xfs_reclaim().
636 */
637void
638xfs_ireclaim(xfs_inode_t *ip)
639{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000640 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /*
643 * Remove from old hash list and mount list.
644 */
645 XFS_STATS_INC(xs_ig_reclaims);
646
647 xfs_iextract(ip);
648
649 /*
650 * Here we do a spurious inode lock in order to coordinate with
651 * xfs_sync(). This is because xfs_sync() references the inodes
652 * in the mount list without taking references on the corresponding
653 * vnodes. We make that OK here by ensuring that we wait until
654 * the inode is unlocked in xfs_sync() before we go ahead and
655 * free it. We get both the regular lock and the io lock because
656 * the xfs_sync() code may need to drop the regular one but will
657 * still hold the io lock.
658 */
659 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
660
661 /*
662 * Release dquots (and their references) if any. An inode may escape
663 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
664 */
665 XFS_QM_DQDETACH(ip->i_mount, ip);
666
667 /*
668 * Pull our behavior descriptor from the vnode chain.
669 */
670 vp = XFS_ITOV_NULL(ip);
671 if (vp) {
672 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
673 }
674
675 /*
676 * Free all memory associated with the inode.
677 */
Tim Shimmin439b8432006-11-11 18:04:34 +1100678 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 xfs_idestroy(ip);
680}
681
682/*
683 * This routine removes an about-to-be-destroyed inode from
684 * all of the lists in which it is located with the exception
685 * of the behavior chain.
686 */
687void
688xfs_iextract(
689 xfs_inode_t *ip)
690{
691 xfs_ihash_t *ih;
692 xfs_inode_t *iq;
693 xfs_mount_t *mp;
694 xfs_chash_t *ch;
695 xfs_chashlist_t *chl, *chm;
696 SPLDECL(s);
697
698 ih = ip->i_hash;
699 write_lock(&ih->ih_lock);
700 if ((iq = ip->i_next)) {
701 iq->i_prevp = ip->i_prevp;
702 }
703 *ip->i_prevp = iq;
Nathan Scott71bce252005-05-05 13:23:27 -0700704 ih->ih_version++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 write_unlock(&ih->ih_lock);
706
707 /*
708 * Remove from cluster hash list
709 * 1) delete the chashlist if this is the last inode on the chashlist
710 * 2) unchain from list of inodes
711 * 3) point chashlist->chl_ip to 'chl_next' if to this inode.
712 */
713 mp = ip->i_mount;
714 ch = XFS_CHASH(mp, ip->i_blkno);
715 s = mutex_spinlock(&ch->ch_lock);
716
717 if (ip->i_cnext == ip) {
718 /* Last inode on chashlist */
719 ASSERT(ip->i_cnext == ip && ip->i_cprev == ip);
720 ASSERT(ip->i_chash != NULL);
721 chm=NULL;
David Chinner1fc5d952006-04-11 15:11:12 +1000722 chl = ip->i_chash;
723 if (chl->chl_prev)
724 chl->chl_prev->chl_next = chl->chl_next;
725 else
726 ch->ch_list = chl->chl_next;
727 if (chl->chl_next)
728 chl->chl_next->chl_prev = chl->chl_prev;
729 kmem_zone_free(xfs_chashlist_zone, chl);
730 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /* delete one inode from a non-empty list */
732 iq = ip->i_cnext;
733 iq->i_cprev = ip->i_cprev;
734 ip->i_cprev->i_cnext = iq;
735 if (ip->i_chash->chl_ip == ip) {
736 ip->i_chash->chl_ip = iq;
737 }
738 ip->i_chash = __return_address;
739 ip->i_cprev = __return_address;
740 ip->i_cnext = __return_address;
741 }
742 mutex_spinunlock(&ch->ch_lock, s);
743
744 /*
745 * Remove from mount's inode list.
746 */
747 XFS_MOUNT_ILOCK(mp);
748 ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
749 iq = ip->i_mnext;
750 iq->i_mprev = ip->i_mprev;
751 ip->i_mprev->i_mnext = iq;
752
753 /*
754 * Fix up the head pointer if it points to the inode being deleted.
755 */
756 if (mp->m_inodes == ip) {
757 if (ip == iq) {
758 mp->m_inodes = NULL;
759 } else {
760 mp->m_inodes = iq;
761 }
762 }
763
764 /* Deal with the deleted inodes list */
765 list_del_init(&ip->i_reclaim);
766
767 mp->m_ireclaims++;
768 XFS_MOUNT_IUNLOCK(mp);
769}
770
771/*
772 * This is a wrapper routine around the xfs_ilock() routine
773 * used to centralize some grungy code. It is used in places
774 * that wish to lock the inode solely for reading the extents.
775 * The reason these places can't just call xfs_ilock(SHARED)
776 * is that the inode lock also guards to bringing in of the
777 * extents from disk for a file in b-tree format. If the inode
778 * is in b-tree format, then we need to lock the inode exclusively
779 * until the extents are read in. Locking it exclusively all
780 * the time would limit our parallelism unnecessarily, though.
781 * What we do instead is check to see if the extents have been
782 * read in yet, and only lock the inode exclusively if they
783 * have not.
784 *
785 * The function returns a value which should be given to the
786 * corresponding xfs_iunlock_map_shared(). This value is
787 * the mode in which the lock was actually taken.
788 */
789uint
790xfs_ilock_map_shared(
791 xfs_inode_t *ip)
792{
793 uint lock_mode;
794
795 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
796 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
797 lock_mode = XFS_ILOCK_EXCL;
798 } else {
799 lock_mode = XFS_ILOCK_SHARED;
800 }
801
802 xfs_ilock(ip, lock_mode);
803
804 return lock_mode;
805}
806
807/*
808 * This is simply the unlock routine to go with xfs_ilock_map_shared().
809 * All it does is call xfs_iunlock() with the given lock_mode.
810 */
811void
812xfs_iunlock_map_shared(
813 xfs_inode_t *ip,
814 unsigned int lock_mode)
815{
816 xfs_iunlock(ip, lock_mode);
817}
818
819/*
820 * The xfs inode contains 2 locks: a multi-reader lock called the
821 * i_iolock and a multi-reader lock called the i_lock. This routine
822 * allows either or both of the locks to be obtained.
823 *
824 * The 2 locks should always be ordered so that the IO lock is
825 * obtained first in order to prevent deadlock.
826 *
827 * ip -- the inode being locked
828 * lock_flags -- this parameter indicates the inode's locks
829 * to be locked. It can be:
830 * XFS_IOLOCK_SHARED,
831 * XFS_IOLOCK_EXCL,
832 * XFS_ILOCK_SHARED,
833 * XFS_ILOCK_EXCL,
834 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
835 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
836 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
837 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
838 */
839void
840xfs_ilock(xfs_inode_t *ip,
841 uint lock_flags)
842{
843 /*
844 * You can't set both SHARED and EXCL for the same lock,
845 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
846 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
847 */
848 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
849 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
850 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
851 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
852 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
853
854 if (lock_flags & XFS_IOLOCK_EXCL) {
855 mrupdate(&ip->i_iolock);
856 } else if (lock_flags & XFS_IOLOCK_SHARED) {
857 mraccess(&ip->i_iolock);
858 }
859 if (lock_flags & XFS_ILOCK_EXCL) {
860 mrupdate(&ip->i_lock);
861 } else if (lock_flags & XFS_ILOCK_SHARED) {
862 mraccess(&ip->i_lock);
863 }
864 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
865}
866
867/*
868 * This is just like xfs_ilock(), except that the caller
869 * is guaranteed not to sleep. It returns 1 if it gets
870 * the requested locks and 0 otherwise. If the IO lock is
871 * obtained but the inode lock cannot be, then the IO lock
872 * is dropped before returning.
873 *
874 * ip -- the inode being locked
875 * lock_flags -- this parameter indicates the inode's locks to be
876 * to be locked. See the comment for xfs_ilock() for a list
877 * of valid values.
878 *
879 */
880int
881xfs_ilock_nowait(xfs_inode_t *ip,
882 uint lock_flags)
883{
884 int iolocked;
885 int ilocked;
886
887 /*
888 * You can't set both SHARED and EXCL for the same lock,
889 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
890 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
891 */
892 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
893 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
894 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
895 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
896 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
897
898 iolocked = 0;
899 if (lock_flags & XFS_IOLOCK_EXCL) {
900 iolocked = mrtryupdate(&ip->i_iolock);
901 if (!iolocked) {
902 return 0;
903 }
904 } else if (lock_flags & XFS_IOLOCK_SHARED) {
905 iolocked = mrtryaccess(&ip->i_iolock);
906 if (!iolocked) {
907 return 0;
908 }
909 }
910 if (lock_flags & XFS_ILOCK_EXCL) {
911 ilocked = mrtryupdate(&ip->i_lock);
912 if (!ilocked) {
913 if (iolocked) {
914 mrunlock(&ip->i_iolock);
915 }
916 return 0;
917 }
918 } else if (lock_flags & XFS_ILOCK_SHARED) {
919 ilocked = mrtryaccess(&ip->i_lock);
920 if (!ilocked) {
921 if (iolocked) {
922 mrunlock(&ip->i_iolock);
923 }
924 return 0;
925 }
926 }
927 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
928 return 1;
929}
930
931/*
932 * xfs_iunlock() is used to drop the inode locks acquired with
933 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
934 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
935 * that we know which locks to drop.
936 *
937 * ip -- the inode being unlocked
938 * lock_flags -- this parameter indicates the inode's locks to be
939 * to be unlocked. See the comment for xfs_ilock() for a list
940 * of valid values for this parameter.
941 *
942 */
943void
944xfs_iunlock(xfs_inode_t *ip,
945 uint lock_flags)
946{
947 /*
948 * You can't set both SHARED and EXCL for the same lock,
949 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
950 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
951 */
952 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
953 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
954 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
955 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
956 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0);
957 ASSERT(lock_flags != 0);
958
959 if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
960 ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
961 (ismrlocked(&ip->i_iolock, MR_ACCESS)));
962 ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
963 (ismrlocked(&ip->i_iolock, MR_UPDATE)));
964 mrunlock(&ip->i_iolock);
965 }
966
967 if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
968 ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
969 (ismrlocked(&ip->i_lock, MR_ACCESS)));
970 ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
971 (ismrlocked(&ip->i_lock, MR_UPDATE)));
972 mrunlock(&ip->i_lock);
973
974 /*
975 * Let the AIL know that this item has been unlocked in case
976 * it is in the AIL and anyone is waiting on it. Don't do
977 * this if the caller has asked us not to.
978 */
979 if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
980 ip->i_itemp != NULL) {
981 xfs_trans_unlocked_item(ip->i_mount,
982 (xfs_log_item_t*)(ip->i_itemp));
983 }
984 }
985 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
986}
987
988/*
989 * give up write locks. the i/o lock cannot be held nested
990 * if it is being demoted.
991 */
992void
993xfs_ilock_demote(xfs_inode_t *ip,
994 uint lock_flags)
995{
996 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
997 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
998
999 if (lock_flags & XFS_ILOCK_EXCL) {
1000 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
1001 mrdemote(&ip->i_lock);
1002 }
1003 if (lock_flags & XFS_IOLOCK_EXCL) {
1004 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1005 mrdemote(&ip->i_iolock);
1006 }
1007}
1008
1009/*
1010 * The following three routines simply manage the i_flock
1011 * semaphore embedded in the inode. This semaphore synchronizes
1012 * processes attempting to flush the in-core inode back to disk.
1013 */
1014void
1015xfs_iflock(xfs_inode_t *ip)
1016{
1017 psema(&(ip->i_flock), PINOD|PLTWAIT);
1018}
1019
1020int
1021xfs_iflock_nowait(xfs_inode_t *ip)
1022{
1023 return (cpsema(&(ip->i_flock)));
1024}
1025
1026void
1027xfs_ifunlock(xfs_inode_t *ip)
1028{
Al Viro0d8fee32006-06-19 08:41:30 +10001029 ASSERT(issemalocked(&(ip->i_flock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 vsema(&(ip->i_flock));
1031}