2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
3 * Copyright (c) 2006 Russell Cattelan Digital Elves, Inc. All Rights Reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * Further, this software is distributed without any warranty that it is
14 * free of the rightful claim of any third person regarding infringement
15 * or the like. Any license provided herein, whether implied or
16 * otherwise, applies only to this software file. Patent licenses, if
17 * any, provided herein do not apply to combinations of this program with
18 * other software, or any other product whatsoever.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write the Free Software Foundation, Inc., 59
22 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
24 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25 * Mountain View, CA 94043, or:
29 * For further information regarding this notice, see:
31 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
36 #include "xfs_types.h"
40 #include "xfs_trans.h"
45 #include "xfs_dmapi.h"
46 #include "xfs_mount.h"
47 #include "xfs_alloc_btree.h"
48 #include "xfs_bmap_btree.h"
49 #include "xfs_ialloc_btree.h"
50 #include "xfs_attr_sf.h"
51 #include "xfs_dir_sf.h"
52 #include "xfs_dir2_sf.h"
53 #include "xfs_dinode.h"
54 #include "xfs_inode.h"
55 #include "xfs_btree.h"
56 #include "xfs_ialloc.h"
57 #include "xfs_quota.h"
58 #include "xfs_utils.h"
59 #include "xfs_vnode.h"
61 static int xfs_vn_allocate(xfs_mount_t *, xfs_inode_t *, struct xfs_vnode **);
64 * Look up an inode by number in the given file system.
65 * The inode is looked up in the hash table for the file system
66 * represented by the mount point parameter mp. Each bucket of
67 * the hash table is guarded by an individual semaphore.
69 * If the inode is found in the hash table, its corresponding vnode
70 * is obtained with a call to vn_get(). This call takes care of
71 * coordination with the reclamation of the inode and vnode. Note
72 * that the vmap structure is filled in while holding the hash lock.
73 * This gives us the state of the inode/vnode when we found it and
74 * is used for coordination in vn_get().
76 * If it is not in core, read it in from the file system's device and
77 * add the inode into the hash table.
79 * The inode is locked according to the value of the lock_flags parameter.
80 * This flag parameter indicates how and if the inode's IO lock and inode lock
83 * mp -- the mount point structure for the current file system. It points
84 * to the inode hash table.
85 * tp -- a pointer to the current transaction if there is one. This is
86 * simply passed through to the xfs_iread() call.
87 * ino -- the number of the inode desired. This is the unique identifier
88 * within the file system for the inode being requested.
89 * lock_flags -- flags indicating how to lock the inode. See the comment
90 * for xfs_ilock() for a list of valid values.
91 * bno -- the block number starting the buffer containing the inode,
92 * if known (as by bulkstat), else 0.
113 xfs_chashlist_t *chl, *chlnew;
117 XFS_STATS_INC(xs_ig_attempts);
119 ih = XFS_IHASH(mp, ino);
122 read_lock(&ih->ih_lock);
124 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
125 if (ip->i_ino == ino) {
129 * Inode cache hit: if ip is not at the front of
130 * its hash chain, move it there now.
131 * Do this with the lock held for update, but
132 * do statistics after releasing the lock.
134 if (ip->i_prevp != &ih->ih_next
135 && rwlock_trypromote(&ih->ih_lock)) {
137 if ((iq = ip->i_next)) {
138 iq->i_prevp = ip->i_prevp;
142 iq->i_prevp = &ip->i_next;
144 ip->i_prevp = &ih->ih_next;
146 write_unlock(&ih->ih_lock);
148 read_unlock(&ih->ih_lock);
151 XFS_STATS_INC(xs_ig_found);
154 * Get a reference to the vnode/inode.
155 * vn_get() takes care of coordination with
156 * the file system inode release and reclaim
157 * functions. If it returns NULL, the inode
158 * has been reclaimed so just start the search
159 * over again. We probably won't find it,
160 * but we could be racing with another cpu
161 * looking for the same inode so we have to at
164 if (!(vp = vn_get(vp, &vmap))) {
165 XFS_STATS_INC(xs_ig_frecycle);
169 if (lock_flags != 0) {
170 ip->i_flags &= ~XFS_IRECLAIM;
171 xfs_ilock(ip, lock_flags);
174 newnode = (ip->i_d.di_mode == 0);
176 xfs_iocore_inode_reinit(ip);
178 ip->i_flags &= ~XFS_ISTALE;
180 vn_trace_exit(vp, "xfs_iget.found",
181 (inst_t *)__return_address);
187 * Inode cache miss: save the hash chain version stamp and unlock
188 * the chain, so we don't deadlock in vn_alloc.
190 XFS_STATS_INC(xs_ig_missed);
192 version = ih->ih_version;
194 read_unlock(&ih->ih_lock);
197 * Read the disk inode attributes into a new inode structure and get
198 * a new vnode for it. This should also initialize i_ino and i_mount.
200 error = xfs_iread(mp, tp, ino, &ip, bno);
205 error = xfs_vn_allocate(mp, ip, &vp);
209 vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
211 xfs_inode_lock_init(ip, vp);
212 xfs_iocore_inode_init(ip);
214 if (lock_flags != 0) {
215 xfs_ilock(ip, lock_flags);
219 * Put ip on its hash chain, unless someone else hashed a duplicate
220 * after we released the hash lock.
222 write_lock(&ih->ih_lock);
224 if (ih->ih_version != version) {
225 for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) {
226 if (iq->i_ino == ino) {
227 write_unlock(&ih->ih_lock);
230 XFS_STATS_INC(xs_ig_dup);
237 * These values _must_ be set before releasing ihlock!
240 if ((iq = ih->ih_next)) {
241 iq->i_prevp = &ip->i_next;
244 ip->i_prevp = &ih->ih_next;
246 ip->i_udquot = ip->i_gdquot = NULL;
249 write_unlock(&ih->ih_lock);
252 * put ip on its cluster's hash chain
254 ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
255 ip->i_cnext == NULL);
258 ch = XFS_CHASH(mp, ip->i_blkno);
260 s = mutex_spinlock(&ch->ch_lock);
261 for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
262 if (chl->chl_blkno == ip->i_blkno) {
264 /* insert this inode into the doubly-linked list
265 * where chl points */
266 if ((iq = chl->chl_ip)) {
267 ip->i_cprev = iq->i_cprev;
268 iq->i_cprev->i_cnext = ip;
281 /* no hash list found for this block; add a new hash list */
283 if (chlnew == NULL) {
284 mutex_spinunlock(&ch->ch_lock, s);
285 ASSERT(xfs_chashlist_zone != NULL);
286 chlnew = (xfs_chashlist_t *)
287 kmem_zone_alloc(xfs_chashlist_zone,
289 ASSERT(chlnew != NULL);
294 ip->i_chash = chlnew;
296 chlnew->chl_blkno = ip->i_blkno;
297 chlnew->chl_next = ch->ch_list;
298 ch->ch_list = chlnew;
302 if (chlnew != NULL) {
303 kmem_zone_free(xfs_chashlist_zone, chlnew);
307 mutex_spinunlock(&ch->ch_lock, s);
310 * Link ip to its mount and thread it on the mount's inode list.
313 if ((iq = mp->m_inodes)) {
314 ASSERT(iq->i_mprev->i_mnext == iq);
315 ip->i_mprev = iq->i_mprev;
316 iq->i_mprev->i_mnext = ip;
325 XFS_MOUNT_IUNLOCK(mp);
330 ASSERT(ip->i_df.if_ext_max ==
331 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
333 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
334 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
339 * If we have a real type for an on-disk inode, we can set ops(&unlock)
340 * now. If it's a new inode being created, xfs_ialloc will handle it.
342 XVFS_INIT_VNODE(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
348 * Special iput for brand-new inodes that are still locked
351 xfs_iput_new(xfs_inode_t *ip,
354 xfs_vnode_t *vp = XFS_ITOV(ip);
356 vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address);
358 printf("xfs_iput_new: ip %p\n",ip);
360 if ((ip->i_d.di_mode == 0)) {
361 ASSERT(!(ip->i_flags & XFS_IRECLAIMABLE));
363 printf("xfs_iput_new: ip %p di_mode == 0\n",ip);
364 /* mabe call vgone here? RMC */
367 xfs_iunlock(ip, lock_flags);
369 ASSERT_VOP_LOCKED(vp->v_vnode, "xfs_iput_new");
373 extern struct vop_vector xfs_vnops;
376 xfs_vn_allocate(xfs_mount_t *mp, xfs_inode_t *ip, struct xfs_vnode **vpp)
379 struct xfs_vnode *vdata;
382 /* Use zone allocator here? */
383 vdata = kmem_zalloc(sizeof(*vdata), KM_SLEEP);
385 error = getnewvnode("xfs", XVFSTOMNT(XFS_MTOVFS(mp)),
388 kmem_free(vdata, sizeof(*vdata));
393 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
394 error = insmntque(vp, XVFSTOMNT(XFS_MTOVFS(mp)));
396 kmem_free(vdata, sizeof(*vdata));
400 vp->v_data = (void *)vdata;
403 vdata->v_vfsp = XFS_MTOVFS(mp);
406 vn_bhv_head_init(VN_BHV_HEAD(vdata), "vnode");
409 #ifdef CONFIG_XFS_VNODE_TRACING
410 vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
411 #endif /* CONFIG_XFS_VNODE_TRACING */
413 vn_trace_exit(vp, "vn_initialize", (inst_t *)__return_address);