blob: 0e6563c05e05fb20df8ed4a8f769aed2d38ce853 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2001,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"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_dir2_sf.h"
33#include "xfs_attr_sf.h"
34#include "xfs_dinode.h"
35#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_btree.h"
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +110037#include "xfs_btree_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_ialloc.h"
39#include "xfs_alloc.h"
40#include "xfs_error.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Christoph Hellwig91cca5d2008-10-30 16:58:01 +110043STATIC int
44xfs_inobt_get_minrecs(
45 struct xfs_btree_cur *cur,
46 int level)
47{
48 return cur->bc_mp->m_inobt_mnr[level != 0];
49}
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Christoph Hellwig561f7d12008-10-30 16:53:59 +110051STATIC struct xfs_btree_cur *
52xfs_inobt_dup_cursor(
53 struct xfs_btree_cur *cur)
54{
55 return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
56 cur->bc_private.a.agbp, cur->bc_private.a.agno);
57}
58
Christoph Hellwig344207c2008-10-30 16:57:16 +110059STATIC void
60xfs_inobt_set_root(
61 struct xfs_btree_cur *cur,
62 union xfs_btree_ptr *nptr,
63 int inc) /* level change */
64{
65 struct xfs_buf *agbp = cur->bc_private.a.agbp;
66 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
67
68 agi->agi_root = nptr->s;
69 be32_add_cpu(&agi->agi_level, inc);
70 xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
71}
72
Christoph Hellwigce5e42d2008-10-30 16:55:23 +110073STATIC int
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +110074xfs_inobt_alloc_block(
75 struct xfs_btree_cur *cur,
76 union xfs_btree_ptr *start,
77 union xfs_btree_ptr *new,
78 int length,
79 int *stat)
80{
81 xfs_alloc_arg_t args; /* block allocation args */
82 int error; /* error return value */
83 xfs_agblock_t sbno = be32_to_cpu(start->s);
84
85 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
86
87 memset(&args, 0, sizeof(args));
88 args.tp = cur->bc_tp;
89 args.mp = cur->bc_mp;
90 args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
91 args.minlen = 1;
92 args.maxlen = 1;
93 args.prod = 1;
94 args.type = XFS_ALLOCTYPE_NEAR_BNO;
95
96 error = xfs_alloc_vextent(&args);
97 if (error) {
98 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
99 return error;
100 }
101 if (args.fsbno == NULLFSBLOCK) {
102 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
103 *stat = 0;
104 return 0;
105 }
106 ASSERT(args.len == 1);
107 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
108
109 new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
110 *stat = 1;
111 return 0;
112}
113
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100114STATIC int
115xfs_inobt_free_block(
116 struct xfs_btree_cur *cur,
117 struct xfs_buf *bp)
118{
119 xfs_fsblock_t fsbno;
120 int error;
121
122 fsbno = XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(bp));
123 error = xfs_free_extent(cur->bc_tp, fsbno, 1);
124 if (error)
125 return error;
126
127 xfs_trans_binval(cur->bc_tp, bp);
128 return error;
129}
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100130
131STATIC int
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100132xfs_inobt_get_maxrecs(
133 struct xfs_btree_cur *cur,
134 int level)
135{
136 return cur->bc_mp->m_inobt_mxr[level != 0];
137}
138
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100139STATIC void
140xfs_inobt_init_key_from_rec(
141 union xfs_btree_key *key,
142 union xfs_btree_rec *rec)
143{
144 key->inobt.ir_startino = rec->inobt.ir_startino;
145}
146
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100147STATIC void
148xfs_inobt_init_rec_from_key(
149 union xfs_btree_key *key,
150 union xfs_btree_rec *rec)
151{
152 rec->inobt.ir_startino = key->inobt.ir_startino;
153}
154
155STATIC void
156xfs_inobt_init_rec_from_cur(
157 struct xfs_btree_cur *cur,
158 union xfs_btree_rec *rec)
159{
160 rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
161 rec->inobt.ir_freecount = cpu_to_be32(cur->bc_rec.i.ir_freecount);
162 rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
163}
164
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100165/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200166 * initial value of ptr for lookup
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100167 */
168STATIC void
169xfs_inobt_init_ptr_from_cur(
170 struct xfs_btree_cur *cur,
171 union xfs_btree_ptr *ptr)
172{
173 struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
174
175 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
176
177 ptr->s = agi->agi_root;
178}
179
180STATIC __int64_t
181xfs_inobt_key_diff(
182 struct xfs_btree_cur *cur,
183 union xfs_btree_key *key)
184{
185 return (__int64_t)be32_to_cpu(key->inobt.ir_startino) -
186 cur->bc_rec.i.ir_startino;
187}
188
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100189STATIC int
190xfs_inobt_kill_root(
191 struct xfs_btree_cur *cur,
192 struct xfs_buf *bp,
193 int level,
194 union xfs_btree_ptr *newroot)
195{
196 int error;
197
198 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
199 XFS_BTREE_STATS_INC(cur, killroot);
200
201 /*
202 * Update the root pointer, decreasing the level by 1 and then
203 * free the old root.
204 */
205 xfs_inobt_set_root(cur, newroot, -1);
206 error = xfs_inobt_free_block(cur, bp);
207 if (error) {
208 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
209 return error;
210 }
211
212 XFS_BTREE_STATS_INC(cur, free);
213
214 cur->bc_bufs[level] = NULL;
215 cur->bc_nlevels--;
216
217 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
218 return 0;
219}
220
Christoph Hellwig4a26e662008-10-30 16:58:32 +1100221#ifdef DEBUG
222STATIC int
223xfs_inobt_keys_inorder(
224 struct xfs_btree_cur *cur,
225 union xfs_btree_key *k1,
226 union xfs_btree_key *k2)
227{
228 return be32_to_cpu(k1->inobt.ir_startino) <
229 be32_to_cpu(k2->inobt.ir_startino);
230}
231
232STATIC int
233xfs_inobt_recs_inorder(
234 struct xfs_btree_cur *cur,
235 union xfs_btree_rec *r1,
236 union xfs_btree_rec *r2)
237{
238 return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
239 be32_to_cpu(r2->inobt.ir_startino);
240}
241#endif /* DEBUG */
242
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100243#ifdef XFS_BTREE_TRACE
244ktrace_t *xfs_inobt_trace_buf;
245
246STATIC void
247xfs_inobt_trace_enter(
248 struct xfs_btree_cur *cur,
249 const char *func,
250 char *s,
251 int type,
252 int line,
253 __psunsigned_t a0,
254 __psunsigned_t a1,
255 __psunsigned_t a2,
256 __psunsigned_t a3,
257 __psunsigned_t a4,
258 __psunsigned_t a5,
259 __psunsigned_t a6,
260 __psunsigned_t a7,
261 __psunsigned_t a8,
262 __psunsigned_t a9,
263 __psunsigned_t a10)
264{
265 ktrace_enter(xfs_inobt_trace_buf, (void *)(__psint_t)type,
266 (void *)func, (void *)s, NULL, (void *)cur,
267 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
268 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
269 (void *)a8, (void *)a9, (void *)a10);
270}
271
272STATIC void
273xfs_inobt_trace_cursor(
274 struct xfs_btree_cur *cur,
275 __uint32_t *s0,
276 __uint64_t *l0,
277 __uint64_t *l1)
278{
279 *s0 = cur->bc_private.a.agno;
280 *l0 = cur->bc_rec.i.ir_startino;
281 *l1 = cur->bc_rec.i.ir_free;
282}
283
284STATIC void
285xfs_inobt_trace_key(
286 struct xfs_btree_cur *cur,
287 union xfs_btree_key *key,
288 __uint64_t *l0,
289 __uint64_t *l1)
290{
291 *l0 = be32_to_cpu(key->inobt.ir_startino);
292 *l1 = 0;
293}
294
295STATIC void
296xfs_inobt_trace_record(
297 struct xfs_btree_cur *cur,
298 union xfs_btree_rec *rec,
299 __uint64_t *l0,
300 __uint64_t *l1,
301 __uint64_t *l2)
302{
303 *l0 = be32_to_cpu(rec->inobt.ir_startino);
304 *l1 = be32_to_cpu(rec->inobt.ir_freecount);
305 *l2 = be64_to_cpu(rec->inobt.ir_free);
306}
307#endif /* XFS_BTREE_TRACE */
308
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100309static const struct xfs_btree_ops xfs_inobt_ops = {
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100310 .rec_len = sizeof(xfs_inobt_rec_t),
311 .key_len = sizeof(xfs_inobt_key_t),
312
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100313 .dup_cursor = xfs_inobt_dup_cursor,
Christoph Hellwig344207c2008-10-30 16:57:16 +1100314 .set_root = xfs_inobt_set_root,
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100315 .kill_root = xfs_inobt_kill_root,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100316 .alloc_block = xfs_inobt_alloc_block,
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +1100317 .free_block = xfs_inobt_free_block,
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100318 .get_minrecs = xfs_inobt_get_minrecs,
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100319 .get_maxrecs = xfs_inobt_get_maxrecs,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100320 .init_key_from_rec = xfs_inobt_init_key_from_rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100321 .init_rec_from_key = xfs_inobt_init_rec_from_key,
322 .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100323 .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
324 .key_diff = xfs_inobt_key_diff,
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100325
Christoph Hellwig4a26e662008-10-30 16:58:32 +1100326#ifdef DEBUG
327 .keys_inorder = xfs_inobt_keys_inorder,
328 .recs_inorder = xfs_inobt_recs_inorder,
329#endif
330
Christoph Hellwig8c4ed632008-10-30 16:55:13 +1100331#ifdef XFS_BTREE_TRACE
332 .trace_enter = xfs_inobt_trace_enter,
333 .trace_cursor = xfs_inobt_trace_cursor,
334 .trace_key = xfs_inobt_trace_key,
335 .trace_record = xfs_inobt_trace_record,
336#endif
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100337};
338
339/*
340 * Allocate a new inode btree cursor.
341 */
342struct xfs_btree_cur * /* new inode btree cursor */
343xfs_inobt_init_cursor(
344 struct xfs_mount *mp, /* file system mount point */
345 struct xfs_trans *tp, /* transaction pointer */
346 struct xfs_buf *agbp, /* buffer for agi structure */
347 xfs_agnumber_t agno) /* allocation group number */
348{
349 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
350 struct xfs_btree_cur *cur;
351
352 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
353
354 cur->bc_tp = tp;
355 cur->bc_mp = mp;
356 cur->bc_nlevels = be32_to_cpu(agi->agi_level);
357 cur->bc_btnum = XFS_BTNUM_INO;
358 cur->bc_blocklog = mp->m_sb.sb_blocklog;
359
360 cur->bc_ops = &xfs_inobt_ops;
361
362 cur->bc_private.a.agbp = agbp;
363 cur->bc_private.a.agno = agno;
364
365 return cur;
366}
Christoph Hellwig60197e82008-10-30 17:11:19 +1100367
368/*
369 * Calculate number of records in an inobt btree block.
370 */
371int
372xfs_inobt_maxrecs(
373 struct xfs_mount *mp,
374 int blocklen,
375 int leaf)
376{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100377 blocklen -= XFS_INOBT_BLOCK_LEN(mp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100378
379 if (leaf)
380 return blocklen / sizeof(xfs_inobt_rec_t);
381 return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
382}