blob: fc19eedbd11b0addaea5ed62386f2f6447988cd4 [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"
27#include "xfs_dir.h"
28#include "xfs_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir_sf.h"
35#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_dinode.h"
38#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_btree.h"
40#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_quota.h"
42#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * Initialize the inode hash table for the newly mounted file system.
46 * Choose an initial table size based on user specified value, else
47 * use a simple algorithm using the maximum number of inodes as an
48 * indicator for table size, and clamp it between one and some large
49 * number of pages.
50 */
51void
52xfs_ihash_init(xfs_mount_t *mp)
53{
54 __uint64_t icount;
55 uint i, flags = KM_SLEEP | KM_MAYFAIL;
56
57 if (!mp->m_ihsize) {
58 icount = mp->m_maxicount ? mp->m_maxicount :
59 (mp->m_sb.sb_dblocks << mp->m_sb.sb_inopblog);
60 mp->m_ihsize = 1 << max_t(uint, 8,
61 (xfs_highbit64(icount) + 1) / 2);
62 mp->m_ihsize = min_t(uint, mp->m_ihsize,
63 (64 * NBPP) / sizeof(xfs_ihash_t));
64 }
65
66 while (!(mp->m_ihash = (xfs_ihash_t *)kmem_zalloc(mp->m_ihsize *
67 sizeof(xfs_ihash_t), flags))) {
68 if ((mp->m_ihsize >>= 1) <= NBPP)
69 flags = KM_SLEEP;
70 }
71 for (i = 0; i < mp->m_ihsize; i++) {
72 rwlock_init(&(mp->m_ihash[i].ih_lock));
73 }
74}
75
76/*
77 * Free up structures allocated by xfs_ihash_init, at unmount time.
78 */
79void
80xfs_ihash_free(xfs_mount_t *mp)
81{
82 kmem_free(mp->m_ihash, mp->m_ihsize*sizeof(xfs_ihash_t));
83 mp->m_ihash = NULL;
84}
85
86/*
87 * Initialize the inode cluster hash table for the newly mounted file system.
88 * Its size is derived from the ihash table size.
89 */
90void
91xfs_chash_init(xfs_mount_t *mp)
92{
93 uint i;
94
95 mp->m_chsize = max_t(uint, 1, mp->m_ihsize /
96 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog));
97 mp->m_chsize = min_t(uint, mp->m_chsize, mp->m_ihsize);
98 mp->m_chash = (xfs_chash_t *)kmem_zalloc(mp->m_chsize
99 * sizeof(xfs_chash_t),
100 KM_SLEEP);
101 for (i = 0; i < mp->m_chsize; i++) {
102 spinlock_init(&mp->m_chash[i].ch_lock,"xfshash");
103 }
104}
105
106/*
107 * Free up structures allocated by xfs_chash_init, at unmount time.
108 */
109void
110xfs_chash_free(xfs_mount_t *mp)
111{
112 int i;
113
114 for (i = 0; i < mp->m_chsize; i++) {
115 spinlock_destroy(&mp->m_chash[i].ch_lock);
116 }
117
118 kmem_free(mp->m_chash, mp->m_chsize*sizeof(xfs_chash_t));
119 mp->m_chash = NULL;
120}
121
122/*
Nathan Scott71bce252005-05-05 13:23:27 -0700123 * Try to move an inode to the front of its hash list if possible
124 * (and if its not there already). Called right after obtaining
125 * the list version number and then dropping the read_lock on the
126 * hash list in question (which is done right after looking up the
127 * inode in question...).
128 */
129STATIC void
130xfs_ihash_promote(
131 xfs_ihash_t *ih,
132 xfs_inode_t *ip,
133 ulong version)
134{
135 xfs_inode_t *iq;
136
137 if ((ip->i_prevp != &ih->ih_next) && write_trylock(&ih->ih_lock)) {
138 if (likely(version == ih->ih_version)) {
139 /* remove from list */
140 if ((iq = ip->i_next)) {
141 iq->i_prevp = ip->i_prevp;
142 }
143 *ip->i_prevp = iq;
144
145 /* insert at list head */
146 iq = ih->ih_next;
147 iq->i_prevp = &ip->i_next;
148 ip->i_next = iq;
149 ip->i_prevp = &ih->ih_next;
150 ih->ih_next = ip;
151 }
152 write_unlock(&ih->ih_lock);
153 }
154}
155
156/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * Look up an inode by number in the given file system.
158 * The inode is looked up in the hash table for the file system
159 * represented by the mount point parameter mp. Each bucket of
160 * the hash table is guarded by an individual semaphore.
161 *
162 * If the inode is found in the hash table, its corresponding vnode
163 * is obtained with a call to vn_get(). This call takes care of
164 * coordination with the reclamation of the inode and vnode. Note
165 * that the vmap structure is filled in while holding the hash lock.
166 * This gives us the state of the inode/vnode when we found it and
167 * is used for coordination in vn_get().
168 *
169 * If it is not in core, read it in from the file system's device and
170 * add the inode into the hash table.
171 *
172 * The inode is locked according to the value of the lock_flags parameter.
173 * This flag parameter indicates how and if the inode's IO lock and inode lock
174 * should be taken.
175 *
176 * mp -- the mount point structure for the current file system. It points
177 * to the inode hash table.
178 * tp -- a pointer to the current transaction if there is one. This is
179 * simply passed through to the xfs_iread() call.
180 * ino -- the number of the inode desired. This is the unique identifier
181 * within the file system for the inode being requested.
182 * lock_flags -- flags indicating how to lock the inode. See the comment
183 * for xfs_ilock() for a list of valid values.
184 * bno -- the block number starting the buffer containing the inode,
185 * if known (as by bulkstat), else 0.
186 */
187STATIC int
188xfs_iget_core(
189 vnode_t *vp,
190 xfs_mount_t *mp,
191 xfs_trans_t *tp,
192 xfs_ino_t ino,
193 uint flags,
194 uint lock_flags,
195 xfs_inode_t **ipp,
196 xfs_daddr_t bno)
197{
198 xfs_ihash_t *ih;
199 xfs_inode_t *ip;
200 xfs_inode_t *iq;
201 vnode_t *inode_vp;
202 ulong version;
203 int error;
204 /* REFERENCED */
205 xfs_chash_t *ch;
206 xfs_chashlist_t *chl, *chlnew;
207 SPLDECL(s);
208
209
210 ih = XFS_IHASH(mp, ino);
211
212again:
213 read_lock(&ih->ih_lock);
214
215 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
216 if (ip->i_ino == ino) {
217 /*
218 * If INEW is set this inode is being set up
219 * we need to pause and try again.
220 */
221 if (ip->i_flags & XFS_INEW) {
222 read_unlock(&ih->ih_lock);
223 delay(1);
224 XFS_STATS_INC(xs_ig_frecycle);
225
226 goto again;
227 }
228
229 inode_vp = XFS_ITOV_NULL(ip);
230 if (inode_vp == NULL) {
231 /*
232 * If IRECLAIM is set this inode is
233 * on its way out of the system,
234 * we need to pause and try again.
235 */
236 if (ip->i_flags & XFS_IRECLAIM) {
237 read_unlock(&ih->ih_lock);
238 delay(1);
239 XFS_STATS_INC(xs_ig_frecycle);
240
241 goto again;
242 }
243
244 vn_trace_exit(vp, "xfs_iget.alloc",
245 (inst_t *)__return_address);
246
247 XFS_STATS_INC(xs_ig_found);
248
249 ip->i_flags &= ~XFS_IRECLAIMABLE;
Nathan Scott71bce252005-05-05 13:23:27 -0700250 version = ih->ih_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 read_unlock(&ih->ih_lock);
Nathan Scott71bce252005-05-05 13:23:27 -0700252 xfs_ihash_promote(ih, ip, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 XFS_MOUNT_ILOCK(mp);
255 list_del_init(&ip->i_reclaim);
256 XFS_MOUNT_IUNLOCK(mp);
257
258 goto finish_inode;
259
260 } else if (vp != inode_vp) {
261 struct inode *inode = LINVFS_GET_IP(inode_vp);
262
263 /* The inode is being torn down, pause and
264 * try again.
265 */
266 if (inode->i_state & (I_FREEING | I_CLEAR)) {
267 read_unlock(&ih->ih_lock);
268 delay(1);
269 XFS_STATS_INC(xs_ig_frecycle);
270
271 goto again;
272 }
273/* Chances are the other vnode (the one in the inode) is being torn
274 * down right now, and we landed on top of it. Question is, what do
275 * we do? Unhook the old inode and hook up the new one?
276 */
277 cmn_err(CE_PANIC,
278 "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
279 inode_vp, vp);
280 }
281
Nathan Scott71bce252005-05-05 13:23:27 -0700282 /*
283 * Inode cache hit: if ip is not at the front of
284 * its hash chain, move it there now.
285 * Do this with the lock held for update, but
286 * do statistics after releasing the lock.
287 */
288 version = ih->ih_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 read_unlock(&ih->ih_lock);
Nathan Scott71bce252005-05-05 13:23:27 -0700290 xfs_ihash_promote(ih, ip, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 XFS_STATS_INC(xs_ig_found);
292
293finish_inode:
294 if (ip->i_d.di_mode == 0) {
295 if (!(flags & IGET_CREATE))
296 return ENOENT;
297 xfs_iocore_inode_reinit(ip);
298 }
299
300 if (lock_flags != 0)
301 xfs_ilock(ip, lock_flags);
302
303 ip->i_flags &= ~XFS_ISTALE;
304
305 vn_trace_exit(vp, "xfs_iget.found",
306 (inst_t *)__return_address);
307 goto return_ip;
308 }
309 }
310
311 /*
312 * Inode cache miss: save the hash chain version stamp and unlock
313 * the chain, so we don't deadlock in vn_alloc.
314 */
315 XFS_STATS_INC(xs_ig_missed);
316
317 version = ih->ih_version;
318
319 read_unlock(&ih->ih_lock);
320
321 /*
322 * Read the disk inode attributes into a new inode structure and get
323 * a new vnode for it. This should also initialize i_ino and i_mount.
324 */
325 error = xfs_iread(mp, tp, ino, &ip, bno);
326 if (error) {
327 return error;
328 }
329
330 vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
331
332 xfs_inode_lock_init(ip, vp);
333 xfs_iocore_inode_init(ip);
334
335 if (lock_flags != 0) {
336 xfs_ilock(ip, lock_flags);
337 }
338
339 if ((ip->i_d.di_mode == 0) && !(flags & IGET_CREATE)) {
340 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++;
374 ip->i_flags |= XFS_INEW;
375
376 write_unlock(&ih->ih_lock);
377
378 /*
379 * put ip on its cluster's hash chain
380 */
381 ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
382 ip->i_cnext == NULL);
383
384 chlnew = NULL;
385 ch = XFS_CHASH(mp, ip->i_blkno);
386 chlredo:
387 s = mutex_spinlock(&ch->ch_lock);
388 for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
389 if (chl->chl_blkno == ip->i_blkno) {
390
391 /* insert this inode into the doubly-linked list
392 * where chl points */
393 if ((iq = chl->chl_ip)) {
394 ip->i_cprev = iq->i_cprev;
395 iq->i_cprev->i_cnext = ip;
396 iq->i_cprev = ip;
397 ip->i_cnext = iq;
398 } else {
399 ip->i_cnext = ip;
400 ip->i_cprev = ip;
401 }
402 chl->chl_ip = ip;
403 ip->i_chash = chl;
404 break;
405 }
406 }
407
408 /* no hash list found for this block; add a new hash list */
409 if (chl == NULL) {
410 if (chlnew == NULL) {
411 mutex_spinunlock(&ch->ch_lock, s);
412 ASSERT(xfs_chashlist_zone != NULL);
413 chlnew = (xfs_chashlist_t *)
414 kmem_zone_alloc(xfs_chashlist_zone,
415 KM_SLEEP);
416 ASSERT(chlnew != NULL);
417 goto chlredo;
418 } else {
419 ip->i_cnext = ip;
420 ip->i_cprev = ip;
421 ip->i_chash = chlnew;
422 chlnew->chl_ip = ip;
423 chlnew->chl_blkno = ip->i_blkno;
424 chlnew->chl_next = ch->ch_list;
425 ch->ch_list = chlnew;
426 chlnew = NULL;
427 }
428 } else {
429 if (chlnew != NULL) {
430 kmem_zone_free(xfs_chashlist_zone, chlnew);
431 }
432 }
433
434 mutex_spinunlock(&ch->ch_lock, s);
435
436
437 /*
438 * Link ip to its mount and thread it on the mount's inode list.
439 */
440 XFS_MOUNT_ILOCK(mp);
441 if ((iq = mp->m_inodes)) {
442 ASSERT(iq->i_mprev->i_mnext == iq);
443 ip->i_mprev = iq->i_mprev;
444 iq->i_mprev->i_mnext = ip;
445 iq->i_mprev = ip;
446 ip->i_mnext = iq;
447 } else {
448 ip->i_mnext = ip;
449 ip->i_mprev = ip;
450 }
451 mp->m_inodes = ip;
452
453 XFS_MOUNT_IUNLOCK(mp);
454
455 return_ip:
456 ASSERT(ip->i_df.if_ext_max ==
457 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
458
459 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
460 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
461
462 *ipp = ip;
463
464 /*
465 * If we have a real type for an on-disk inode, we can set ops(&unlock)
466 * now. If it's a new inode being created, xfs_ialloc will handle it.
467 */
468 VFS_INIT_VNODE(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
469
470 return 0;
471}
472
473
474/*
475 * The 'normal' internal xfs_iget, if needed it will
476 * 'allocate', or 'get', the vnode.
477 */
478int
479xfs_iget(
480 xfs_mount_t *mp,
481 xfs_trans_t *tp,
482 xfs_ino_t ino,
483 uint flags,
484 uint lock_flags,
485 xfs_inode_t **ipp,
486 xfs_daddr_t bno)
487{
488 struct inode *inode;
489 vnode_t *vp = NULL;
490 int error;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 XFS_STATS_INC(xs_ig_attempts);
493
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000494retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
496 bhv_desc_t *bdp;
497 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 vp = LINVFS_GET_VP(inode);
500 if (inode->i_state & I_NEW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 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 {
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000511 /*
512 * If the inode is not fully constructed due to
513 * filehandle mistmatches wait for the inode to go
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) ||
520 ((bdp = vn_bhv_lookup(VN_BHV_HEAD(vp),
521 &xfs_vnodeops)) == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 iput(inode);
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000523 delay(1);
524 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ip = XFS_BHVTOI(bdp);
528 if (lock_flags != 0)
529 xfs_ilock(ip, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 XFS_STATS_INC(xs_ig_found);
531 *ipp = ip;
532 error = 0;
533 }
534 } else
535 error = ENOMEM; /* If we got no inode we are out of memory */
536
537 return error;
538}
539
540/*
541 * Do the setup for the various locks within the incore inode.
542 */
543void
544xfs_inode_lock_init(
545 xfs_inode_t *ip,
546 vnode_t *vp)
547{
548 mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
549 "xfsino", (long)vp->v_number);
550 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number);
551 init_waitqueue_head(&ip->i_ipin_wait);
552 atomic_set(&ip->i_pincount, 0);
553 init_sema(&ip->i_flock, 1, "xfsfino", vp->v_number);
554}
555
556/*
557 * Look for the inode corresponding to the given ino in the hash table.
558 * If it is there and its i_transp pointer matches tp, return it.
559 * Otherwise, return NULL.
560 */
561xfs_inode_t *
562xfs_inode_incore(xfs_mount_t *mp,
563 xfs_ino_t ino,
564 xfs_trans_t *tp)
565{
566 xfs_ihash_t *ih;
567 xfs_inode_t *ip;
Nathan Scott71bce252005-05-05 13:23:27 -0700568 ulong version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 ih = XFS_IHASH(mp, ino);
571 read_lock(&ih->ih_lock);
572 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
573 if (ip->i_ino == ino) {
574 /*
575 * If we find it and tp matches, return it.
Nathan Scott71bce252005-05-05 13:23:27 -0700576 * Also move it to the front of the hash list
577 * if we find it and it is not already there.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * Otherwise break from the loop and return
579 * NULL.
580 */
581 if (ip->i_transp == tp) {
Nathan Scott71bce252005-05-05 13:23:27 -0700582 version = ih->ih_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 read_unlock(&ih->ih_lock);
Nathan Scott71bce252005-05-05 13:23:27 -0700584 xfs_ihash_promote(ih, ip, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return (ip);
586 }
587 break;
588 }
589 }
590 read_unlock(&ih->ih_lock);
591 return (NULL);
592}
593
594/*
595 * Decrement reference count of an inode structure and unlock it.
596 *
597 * ip -- the inode being released
598 * lock_flags -- this parameter indicates the inode's locks to be
599 * to be released. See the comment on xfs_iunlock() for a list
600 * of valid values.
601 */
602void
603xfs_iput(xfs_inode_t *ip,
604 uint lock_flags)
605{
606 vnode_t *vp = XFS_ITOV(ip);
607
608 vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address);
609
610 xfs_iunlock(ip, lock_flags);
611
612 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{
622 vnode_t *vp = XFS_ITOV(ip);
623 struct inode *inode = LINVFS_GET_IP(vp);
624
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{
648 vnode_t *vp;
649
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 */
686 xfs_idestroy(ip);
687}
688
689/*
690 * This routine removes an about-to-be-destroyed inode from
691 * all of the lists in which it is located with the exception
692 * of the behavior chain.
693 */
694void
695xfs_iextract(
696 xfs_inode_t *ip)
697{
698 xfs_ihash_t *ih;
699 xfs_inode_t *iq;
700 xfs_mount_t *mp;
701 xfs_chash_t *ch;
702 xfs_chashlist_t *chl, *chm;
703 SPLDECL(s);
704
705 ih = ip->i_hash;
706 write_lock(&ih->ih_lock);
707 if ((iq = ip->i_next)) {
708 iq->i_prevp = ip->i_prevp;
709 }
710 *ip->i_prevp = iq;
Nathan Scott71bce252005-05-05 13:23:27 -0700711 ih->ih_version++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 write_unlock(&ih->ih_lock);
713
714 /*
715 * Remove from cluster hash list
716 * 1) delete the chashlist if this is the last inode on the chashlist
717 * 2) unchain from list of inodes
718 * 3) point chashlist->chl_ip to 'chl_next' if to this inode.
719 */
720 mp = ip->i_mount;
721 ch = XFS_CHASH(mp, ip->i_blkno);
722 s = mutex_spinlock(&ch->ch_lock);
723
724 if (ip->i_cnext == ip) {
725 /* Last inode on chashlist */
726 ASSERT(ip->i_cnext == ip && ip->i_cprev == ip);
727 ASSERT(ip->i_chash != NULL);
728 chm=NULL;
729 for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
730 if (chl->chl_blkno == ip->i_blkno) {
731 if (chm == NULL) {
732 /* first item on the list */
733 ch->ch_list = chl->chl_next;
734 } else {
735 chm->chl_next = chl->chl_next;
736 }
737 kmem_zone_free(xfs_chashlist_zone, chl);
738 break;
739 } else {
740 ASSERT(chl->chl_ip != ip);
741 chm = chl;
742 }
743 }
744 ASSERT_ALWAYS(chl != NULL);
745 } else {
746 /* delete one inode from a non-empty list */
747 iq = ip->i_cnext;
748 iq->i_cprev = ip->i_cprev;
749 ip->i_cprev->i_cnext = iq;
750 if (ip->i_chash->chl_ip == ip) {
751 ip->i_chash->chl_ip = iq;
752 }
753 ip->i_chash = __return_address;
754 ip->i_cprev = __return_address;
755 ip->i_cnext = __return_address;
756 }
757 mutex_spinunlock(&ch->ch_lock, s);
758
759 /*
760 * Remove from mount's inode list.
761 */
762 XFS_MOUNT_ILOCK(mp);
763 ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
764 iq = ip->i_mnext;
765 iq->i_mprev = ip->i_mprev;
766 ip->i_mprev->i_mnext = iq;
767
768 /*
769 * Fix up the head pointer if it points to the inode being deleted.
770 */
771 if (mp->m_inodes == ip) {
772 if (ip == iq) {
773 mp->m_inodes = NULL;
774 } else {
775 mp->m_inodes = iq;
776 }
777 }
778
779 /* Deal with the deleted inodes list */
780 list_del_init(&ip->i_reclaim);
781
782 mp->m_ireclaims++;
783 XFS_MOUNT_IUNLOCK(mp);
784}
785
786/*
787 * This is a wrapper routine around the xfs_ilock() routine
788 * used to centralize some grungy code. It is used in places
789 * that wish to lock the inode solely for reading the extents.
790 * The reason these places can't just call xfs_ilock(SHARED)
791 * is that the inode lock also guards to bringing in of the
792 * extents from disk for a file in b-tree format. If the inode
793 * is in b-tree format, then we need to lock the inode exclusively
794 * until the extents are read in. Locking it exclusively all
795 * the time would limit our parallelism unnecessarily, though.
796 * What we do instead is check to see if the extents have been
797 * read in yet, and only lock the inode exclusively if they
798 * have not.
799 *
800 * The function returns a value which should be given to the
801 * corresponding xfs_iunlock_map_shared(). This value is
802 * the mode in which the lock was actually taken.
803 */
804uint
805xfs_ilock_map_shared(
806 xfs_inode_t *ip)
807{
808 uint lock_mode;
809
810 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
811 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
812 lock_mode = XFS_ILOCK_EXCL;
813 } else {
814 lock_mode = XFS_ILOCK_SHARED;
815 }
816
817 xfs_ilock(ip, lock_mode);
818
819 return lock_mode;
820}
821
822/*
823 * This is simply the unlock routine to go with xfs_ilock_map_shared().
824 * All it does is call xfs_iunlock() with the given lock_mode.
825 */
826void
827xfs_iunlock_map_shared(
828 xfs_inode_t *ip,
829 unsigned int lock_mode)
830{
831 xfs_iunlock(ip, lock_mode);
832}
833
834/*
835 * The xfs inode contains 2 locks: a multi-reader lock called the
836 * i_iolock and a multi-reader lock called the i_lock. This routine
837 * allows either or both of the locks to be obtained.
838 *
839 * The 2 locks should always be ordered so that the IO lock is
840 * obtained first in order to prevent deadlock.
841 *
842 * ip -- the inode being locked
843 * lock_flags -- this parameter indicates the inode's locks
844 * to be locked. It can be:
845 * XFS_IOLOCK_SHARED,
846 * XFS_IOLOCK_EXCL,
847 * XFS_ILOCK_SHARED,
848 * XFS_ILOCK_EXCL,
849 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
850 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
851 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
852 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
853 */
854void
855xfs_ilock(xfs_inode_t *ip,
856 uint lock_flags)
857{
858 /*
859 * You can't set both SHARED and EXCL for the same lock,
860 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
861 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
862 */
863 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
864 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
865 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
866 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
867 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
868
869 if (lock_flags & XFS_IOLOCK_EXCL) {
870 mrupdate(&ip->i_iolock);
871 } else if (lock_flags & XFS_IOLOCK_SHARED) {
872 mraccess(&ip->i_iolock);
873 }
874 if (lock_flags & XFS_ILOCK_EXCL) {
875 mrupdate(&ip->i_lock);
876 } else if (lock_flags & XFS_ILOCK_SHARED) {
877 mraccess(&ip->i_lock);
878 }
879 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
880}
881
882/*
883 * This is just like xfs_ilock(), except that the caller
884 * is guaranteed not to sleep. It returns 1 if it gets
885 * the requested locks and 0 otherwise. If the IO lock is
886 * obtained but the inode lock cannot be, then the IO lock
887 * is dropped before returning.
888 *
889 * ip -- the inode being locked
890 * lock_flags -- this parameter indicates the inode's locks to be
891 * to be locked. See the comment for xfs_ilock() for a list
892 * of valid values.
893 *
894 */
895int
896xfs_ilock_nowait(xfs_inode_t *ip,
897 uint lock_flags)
898{
899 int iolocked;
900 int ilocked;
901
902 /*
903 * You can't set both SHARED and EXCL for the same lock,
904 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
905 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
906 */
907 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
908 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
909 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
910 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
911 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
912
913 iolocked = 0;
914 if (lock_flags & XFS_IOLOCK_EXCL) {
915 iolocked = mrtryupdate(&ip->i_iolock);
916 if (!iolocked) {
917 return 0;
918 }
919 } else if (lock_flags & XFS_IOLOCK_SHARED) {
920 iolocked = mrtryaccess(&ip->i_iolock);
921 if (!iolocked) {
922 return 0;
923 }
924 }
925 if (lock_flags & XFS_ILOCK_EXCL) {
926 ilocked = mrtryupdate(&ip->i_lock);
927 if (!ilocked) {
928 if (iolocked) {
929 mrunlock(&ip->i_iolock);
930 }
931 return 0;
932 }
933 } else if (lock_flags & XFS_ILOCK_SHARED) {
934 ilocked = mrtryaccess(&ip->i_lock);
935 if (!ilocked) {
936 if (iolocked) {
937 mrunlock(&ip->i_iolock);
938 }
939 return 0;
940 }
941 }
942 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
943 return 1;
944}
945
946/*
947 * xfs_iunlock() is used to drop the inode locks acquired with
948 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
949 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
950 * that we know which locks to drop.
951 *
952 * ip -- the inode being unlocked
953 * lock_flags -- this parameter indicates the inode's locks to be
954 * to be unlocked. See the comment for xfs_ilock() for a list
955 * of valid values for this parameter.
956 *
957 */
958void
959xfs_iunlock(xfs_inode_t *ip,
960 uint lock_flags)
961{
962 /*
963 * You can't set both SHARED and EXCL for the same lock,
964 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
965 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
966 */
967 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
968 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
969 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
970 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
971 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0);
972 ASSERT(lock_flags != 0);
973
974 if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
975 ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
976 (ismrlocked(&ip->i_iolock, MR_ACCESS)));
977 ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
978 (ismrlocked(&ip->i_iolock, MR_UPDATE)));
979 mrunlock(&ip->i_iolock);
980 }
981
982 if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
983 ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
984 (ismrlocked(&ip->i_lock, MR_ACCESS)));
985 ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
986 (ismrlocked(&ip->i_lock, MR_UPDATE)));
987 mrunlock(&ip->i_lock);
988
989 /*
990 * Let the AIL know that this item has been unlocked in case
991 * it is in the AIL and anyone is waiting on it. Don't do
992 * this if the caller has asked us not to.
993 */
994 if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
995 ip->i_itemp != NULL) {
996 xfs_trans_unlocked_item(ip->i_mount,
997 (xfs_log_item_t*)(ip->i_itemp));
998 }
999 }
1000 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
1001}
1002
1003/*
1004 * give up write locks. the i/o lock cannot be held nested
1005 * if it is being demoted.
1006 */
1007void
1008xfs_ilock_demote(xfs_inode_t *ip,
1009 uint lock_flags)
1010{
1011 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
1012 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
1013
1014 if (lock_flags & XFS_ILOCK_EXCL) {
1015 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
1016 mrdemote(&ip->i_lock);
1017 }
1018 if (lock_flags & XFS_IOLOCK_EXCL) {
1019 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1020 mrdemote(&ip->i_iolock);
1021 }
1022}
1023
1024/*
1025 * The following three routines simply manage the i_flock
1026 * semaphore embedded in the inode. This semaphore synchronizes
1027 * processes attempting to flush the in-core inode back to disk.
1028 */
1029void
1030xfs_iflock(xfs_inode_t *ip)
1031{
1032 psema(&(ip->i_flock), PINOD|PLTWAIT);
1033}
1034
1035int
1036xfs_iflock_nowait(xfs_inode_t *ip)
1037{
1038 return (cpsema(&(ip->i_flock)));
1039}
1040
1041void
1042xfs_ifunlock(xfs_inode_t *ip)
1043{
1044 ASSERT(valusema(&(ip->i_flock)) <= 0);
1045 vsema(&(ip->i_flock));
1046}