]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/gnu/fs/xfs/FreeBSD/xfs_freebsd_iget.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / gnu / fs / xfs / FreeBSD / xfs_freebsd_iget.c
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
3  * Copyright (c) 2006 Russell Cattelan Digital Elves, Inc. All Rights Reserved.
4  *
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.
8  *
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.
12  *
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.
19  *
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.
23  *
24  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25  * Mountain View, CA  94043, or:
26  *
27  * http://www.sgi.com
28  *
29  * For further information regarding this notice, see:
30  *
31  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
32  */
33
34 #include "xfs.h"
35
36 #include "xfs_types.h"
37 #include "xfs_bit.h"
38 #include "xfs_inum.h"
39 #include "xfs_log.h"
40 #include "xfs_trans.h"
41 #include "xfs_sb.h"
42 #include "xfs_ag.h"
43 #include "xfs_dir.h"
44 #include "xfs_dir2.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"
60
61 static int xfs_vn_allocate(xfs_mount_t *, xfs_inode_t *, struct xfs_vnode **);
62
63 /*
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.
68  *
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().
75  *
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.
78  *
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
81  * should be taken.
82  *
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.
93  */
94 int
95 xfs_iget(
96         xfs_mount_t     *mp,
97         xfs_trans_t     *tp,
98         xfs_ino_t       ino,
99         uint            flags,
100         uint            lock_flags,
101         xfs_inode_t     **ipp,
102         xfs_daddr_t     bno)
103 {
104         xfs_ihash_t     *ih;
105         xfs_inode_t     *ip;
106         xfs_inode_t     *iq;
107         xfs_vnode_t     *vp;
108         ulong           version;
109         int             error;
110         /* REFERENCED */
111         int             newnode;
112         xfs_chash_t     *ch;
113         xfs_chashlist_t *chl, *chlnew;
114         vmap_t          vmap;
115         SPLDECL(s);
116
117         XFS_STATS_INC(xs_ig_attempts);
118
119         ih = XFS_IHASH(mp, ino);
120
121 again:
122         read_lock(&ih->ih_lock);
123
124         for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
125                 if (ip->i_ino == ino) {
126                         vp = XFS_ITOV(ip);
127                         VMAP(vp, vmap);
128                         /*
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.
133                          */
134                         if (ip->i_prevp != &ih->ih_next
135                             && rwlock_trypromote(&ih->ih_lock)) {
136
137                                 if ((iq = ip->i_next)) {
138                                         iq->i_prevp = ip->i_prevp;
139                                 }
140                                 *ip->i_prevp = iq;
141                                 iq = ih->ih_next;
142                                 iq->i_prevp = &ip->i_next;
143                                 ip->i_next = iq;
144                                 ip->i_prevp = &ih->ih_next;
145                                 ih->ih_next = ip;
146                                 write_unlock(&ih->ih_lock);
147                         } else {
148                                 read_unlock(&ih->ih_lock);
149                         }
150
151                         XFS_STATS_INC(xs_ig_found);
152
153                         /*
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
162                          * least look.
163                          */
164                         if (!(vp = vn_get(vp, &vmap))) {
165                                 XFS_STATS_INC(xs_ig_frecycle);
166                                 goto again;
167                         }
168
169                         if (lock_flags != 0) {
170                                 ip->i_flags &= ~XFS_IRECLAIM;
171                                 xfs_ilock(ip, lock_flags);
172                         }
173
174                         newnode = (ip->i_d.di_mode == 0);
175                         if (newnode) {
176                                 xfs_iocore_inode_reinit(ip);
177                         }
178                         ip->i_flags &= ~XFS_ISTALE;
179
180                         vn_trace_exit(vp, "xfs_iget.found",
181                                                 (inst_t *)__return_address);
182                         goto return_ip;
183                 }
184         }
185
186         /*
187          * Inode cache miss: save the hash chain version stamp and unlock
188          * the chain, so we don't deadlock in vn_alloc.
189          */
190         XFS_STATS_INC(xs_ig_missed);
191
192         version = ih->ih_version;
193
194         read_unlock(&ih->ih_lock);
195
196         /*
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.
199          */
200         error = xfs_iread(mp, tp, ino, &ip, bno);
201         if (error) {
202                 return error;
203         }
204
205         error = xfs_vn_allocate(mp, ip, &vp);
206         if (error) {
207                 return error;
208         }
209         vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
210
211         xfs_inode_lock_init(ip, vp);
212         xfs_iocore_inode_init(ip);
213
214         if (lock_flags != 0) {
215                 xfs_ilock(ip, lock_flags);
216         }
217
218         /*
219          * Put ip on its hash chain, unless someone else hashed a duplicate
220          * after we released the hash lock.
221          */
222         write_lock(&ih->ih_lock);
223
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);
228                                 xfs_idestroy(ip);
229
230                                 XFS_STATS_INC(xs_ig_dup);
231                                 goto again;
232                         }
233                 }
234         }
235
236         /*
237          * These values _must_ be set before releasing ihlock!
238          */
239         ip->i_hash = ih;
240         if ((iq = ih->ih_next)) {
241                 iq->i_prevp = &ip->i_next;
242         }
243         ip->i_next = iq;
244         ip->i_prevp = &ih->ih_next;
245         ih->ih_next = ip;
246         ip->i_udquot = ip->i_gdquot = NULL;
247         ih->ih_version++;
248
249         write_unlock(&ih->ih_lock);
250
251         /*
252          * put ip on its cluster's hash chain
253          */
254         ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
255                ip->i_cnext == NULL);
256
257         chlnew = NULL;
258         ch = XFS_CHASH(mp, ip->i_blkno);
259  chlredo:
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) {
263
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;
269                                 iq->i_cprev = ip;
270                                 ip->i_cnext = iq;
271                         } else {
272                                 ip->i_cnext = ip;
273                                 ip->i_cprev = ip;
274                         }
275                         chl->chl_ip = ip;
276                         ip->i_chash = chl;
277                         break;
278                 }
279         }
280
281         /* no hash list found for this block; add a new hash list */
282         if (chl == NULL)  {
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,
288                                                 KM_SLEEP);
289                         ASSERT(chlnew != NULL);
290                         goto chlredo;
291                 } else {
292                         ip->i_cnext = ip;
293                         ip->i_cprev = ip;
294                         ip->i_chash = chlnew;
295                         chlnew->chl_ip = ip;
296                         chlnew->chl_blkno = ip->i_blkno;
297                         chlnew->chl_next = ch->ch_list;
298                         ch->ch_list = chlnew;
299                         chlnew = NULL;
300                 }
301         } else {
302                 if (chlnew != NULL) {
303                         kmem_zone_free(xfs_chashlist_zone, chlnew);
304                 }
305         }
306
307         mutex_spinunlock(&ch->ch_lock, s);
308
309         /*
310          * Link ip to its mount and thread it on the mount's inode list.
311          */
312         XFS_MOUNT_ILOCK(mp);
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;
317                 iq->i_mprev = ip;
318                 ip->i_mnext = iq;
319         } else {
320                 ip->i_mnext = ip;
321                 ip->i_mprev = ip;
322         }
323         mp->m_inodes = ip;
324
325         XFS_MOUNT_IUNLOCK(mp);
326
327         newnode = 1;
328
329  return_ip:
330         ASSERT(ip->i_df.if_ext_max ==
331                XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
332
333         ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
334                ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
335
336         *ipp = ip;
337
338         /*
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.
341          */
342         XVFS_INIT_VNODE(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
343
344         return 0;
345 }
346
347 /*
348  * Special iput for brand-new inodes that are still locked
349  */
350 void
351 xfs_iput_new(xfs_inode_t        *ip,
352              uint               lock_flags)
353 {
354         xfs_vnode_t             *vp = XFS_ITOV(ip);
355
356         vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address);
357
358         printf("xfs_iput_new: ip %p\n",ip);
359         
360         if ((ip->i_d.di_mode == 0)) {
361                 ASSERT(!(ip->i_flags & XFS_IRECLAIMABLE));
362                 //vn_mark_bad(vp);
363                 printf("xfs_iput_new: ip %p di_mode == 0\n",ip);
364                 /* mabe call vgone here? RMC */
365         }
366         if (lock_flags)
367                 xfs_iunlock(ip, lock_flags);
368
369         ASSERT_VOP_LOCKED(vp->v_vnode, "xfs_iput_new");
370         vput(vp->v_vnode);
371 }
372
373 extern struct vop_vector xfs_vnops;
374
375 static int
376 xfs_vn_allocate(xfs_mount_t *mp, xfs_inode_t *ip, struct xfs_vnode **vpp)
377 {
378         struct vnode *vp;
379         struct xfs_vnode *vdata;
380         int error;
381
382         /* Use zone allocator here? */
383         vdata = kmem_zalloc(sizeof(*vdata), KM_SLEEP);
384
385         error = getnewvnode("xfs", XVFSTOMNT(XFS_MTOVFS(mp)),
386                             &xfs_vnops, &vp);
387         if (error) {
388                 kmem_free(vdata, sizeof(*vdata));
389                 return (error);
390         }
391
392         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
393         VN_LOCK_AREC(vp);
394         error = insmntque(vp, XVFSTOMNT(XFS_MTOVFS(mp)));
395         if (error != 0) {
396                 kmem_free(vdata, sizeof(*vdata));
397                 return (error);
398         }
399
400         vp->v_data = (void *)vdata;
401         vdata->v_number= 0;
402         vdata->v_inode = ip;
403         vdata->v_vfsp  = XFS_MTOVFS(mp);
404         vdata->v_vnode = vp;
405
406         vn_bhv_head_init(VN_BHV_HEAD(vdata), "vnode");
407
408
409 #ifdef  CONFIG_XFS_VNODE_TRACING
410         vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
411 #endif  /* CONFIG_XFS_VNODE_TRACING */
412
413         vn_trace_exit(vp, "vn_initialize", (inst_t *)__return_address);
414
415         if (error == 0)
416                 *vpp = vdata;
417
418         return (error);
419 }