Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * 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. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * 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 | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
1da177e4 | 21 | #include "xfs_log.h" |
a844f451 | 22 | #include "xfs_inum.h" |
1da177e4 LT |
23 | #include "xfs_trans.h" |
24 | #include "xfs_sb.h" | |
da353b0d | 25 | #include "xfs_ag.h" |
1da177e4 | 26 | #include "xfs_dir2.h" |
1da177e4 | 27 | #include "xfs_mount.h" |
a844f451 | 28 | #include "xfs_da_btree.h" |
1da177e4 | 29 | #include "xfs_bmap_btree.h" |
1da177e4 | 30 | #include "xfs_dir2_sf.h" |
a844f451 | 31 | #include "xfs_attr_sf.h" |
1da177e4 LT |
32 | #include "xfs_dinode.h" |
33 | #include "xfs_inode.h" | |
34 | #include "xfs_bmap.h" | |
1da177e4 LT |
35 | #include "xfs_dir2_data.h" |
36 | #include "xfs_dir2_leaf.h" | |
37 | #include "xfs_dir2_block.h" | |
38 | #include "xfs_dir2_node.h" | |
1da177e4 | 39 | #include "xfs_error.h" |
0b1b213f | 40 | #include "xfs_trace.h" |
1da177e4 LT |
41 | |
42 | /* | |
43 | * Function declarations. | |
44 | */ | |
45 | static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp); | |
46 | static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index); | |
47 | #ifdef DEBUG | |
48 | static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp); | |
49 | #else | |
50 | #define xfs_dir2_leafn_check(dp, bp) | |
51 | #endif | |
52 | static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s, | |
53 | int start_s, xfs_dabuf_t *bp_d, int start_d, | |
54 | int count); | |
55 | static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state, | |
56 | xfs_da_state_blk_t *blk1, | |
57 | xfs_da_state_blk_t *blk2); | |
58 | static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp, | |
59 | int index, xfs_da_state_blk_t *dblk, | |
60 | int *rval); | |
61 | static int xfs_dir2_node_addname_int(xfs_da_args_t *args, | |
62 | xfs_da_state_blk_t *fblk); | |
63 | ||
64 | /* | |
65 | * Log entries from a freespace block. | |
66 | */ | |
5d77c0dc | 67 | STATIC void |
1da177e4 LT |
68 | xfs_dir2_free_log_bests( |
69 | xfs_trans_t *tp, /* transaction pointer */ | |
70 | xfs_dabuf_t *bp, /* freespace buffer */ | |
71 | int first, /* first entry to log */ | |
72 | int last) /* last entry to log */ | |
73 | { | |
74 | xfs_dir2_free_t *free; /* freespace structure */ | |
75 | ||
76 | free = bp->data; | |
0ba962ef | 77 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
1da177e4 LT |
78 | xfs_da_log_buf(tp, bp, |
79 | (uint)((char *)&free->bests[first] - (char *)free), | |
80 | (uint)((char *)&free->bests[last] - (char *)free + | |
81 | sizeof(free->bests[0]) - 1)); | |
82 | } | |
83 | ||
84 | /* | |
85 | * Log header from a freespace block. | |
86 | */ | |
87 | static void | |
88 | xfs_dir2_free_log_header( | |
89 | xfs_trans_t *tp, /* transaction pointer */ | |
90 | xfs_dabuf_t *bp) /* freespace buffer */ | |
91 | { | |
92 | xfs_dir2_free_t *free; /* freespace structure */ | |
93 | ||
94 | free = bp->data; | |
0ba962ef | 95 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
1da177e4 LT |
96 | xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free), |
97 | (uint)(sizeof(xfs_dir2_free_hdr_t) - 1)); | |
98 | } | |
99 | ||
100 | /* | |
101 | * Convert a leaf-format directory to a node-format directory. | |
102 | * We need to change the magic number of the leaf block, and copy | |
103 | * the freespace table out of the leaf block into its own block. | |
104 | */ | |
105 | int /* error */ | |
106 | xfs_dir2_leaf_to_node( | |
107 | xfs_da_args_t *args, /* operation arguments */ | |
108 | xfs_dabuf_t *lbp) /* leaf buffer */ | |
109 | { | |
110 | xfs_inode_t *dp; /* incore directory inode */ | |
111 | int error; /* error return value */ | |
112 | xfs_dabuf_t *fbp; /* freespace buffer */ | |
113 | xfs_dir2_db_t fdb; /* freespace block number */ | |
114 | xfs_dir2_free_t *free; /* freespace structure */ | |
68b3a102 | 115 | __be16 *from; /* pointer to freespace entry */ |
1da177e4 LT |
116 | int i; /* leaf freespace index */ |
117 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
118 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
119 | xfs_mount_t *mp; /* filesystem mount point */ | |
120 | int n; /* count of live freespc ents */ | |
121 | xfs_dir2_data_off_t off; /* freespace entry value */ | |
0ba962ef | 122 | __be16 *to; /* pointer to freespace entry */ |
1da177e4 LT |
123 | xfs_trans_t *tp; /* transaction pointer */ |
124 | ||
0b1b213f CH |
125 | trace_xfs_dir2_leaf_to_node(args); |
126 | ||
1da177e4 LT |
127 | dp = args->dp; |
128 | mp = dp->i_mount; | |
129 | tp = args->trans; | |
130 | /* | |
131 | * Add a freespace block to the directory. | |
132 | */ | |
133 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) { | |
134 | return error; | |
135 | } | |
136 | ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp)); | |
137 | /* | |
138 | * Get the buffer for the new freespace block. | |
139 | */ | |
bbaaf538 | 140 | if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp, |
1da177e4 LT |
141 | XFS_DATA_FORK))) { |
142 | return error; | |
143 | } | |
144 | ASSERT(fbp != NULL); | |
145 | free = fbp->data; | |
146 | leaf = lbp->data; | |
bbaaf538 | 147 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
1da177e4 LT |
148 | /* |
149 | * Initialize the freespace block header. | |
150 | */ | |
0ba962ef | 151 | free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC); |
1da177e4 | 152 | free->hdr.firstdb = 0; |
afbcb3f9 | 153 | ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize); |
0ba962ef | 154 | free->hdr.nvalid = ltp->bestcount; |
1da177e4 LT |
155 | /* |
156 | * Copy freespace entries from the leaf block to the new block. | |
157 | * Count active entries. | |
158 | */ | |
bbaaf538 | 159 | for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests; |
afbcb3f9 | 160 | i < be32_to_cpu(ltp->bestcount); i++, from++, to++) { |
68b3a102 | 161 | if ((off = be16_to_cpu(*from)) != NULLDATAOFF) |
1da177e4 | 162 | n++; |
0ba962ef | 163 | *to = cpu_to_be16(off); |
1da177e4 | 164 | } |
0ba962ef | 165 | free->hdr.nused = cpu_to_be32(n); |
89da0544 | 166 | leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC); |
1da177e4 LT |
167 | /* |
168 | * Log everything. | |
169 | */ | |
170 | xfs_dir2_leaf_log_header(tp, lbp); | |
171 | xfs_dir2_free_log_header(tp, fbp); | |
0ba962ef | 172 | xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1); |
1da177e4 LT |
173 | xfs_da_buf_done(fbp); |
174 | xfs_dir2_leafn_check(dp, lbp); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | /* | |
179 | * Add a leaf entry to a leaf block in a node-form directory. | |
180 | * The other work necessary is done from the caller. | |
181 | */ | |
182 | static int /* error */ | |
183 | xfs_dir2_leafn_add( | |
184 | xfs_dabuf_t *bp, /* leaf buffer */ | |
185 | xfs_da_args_t *args, /* operation arguments */ | |
186 | int index) /* insertion pt for new entry */ | |
187 | { | |
188 | int compact; /* compacting stale leaves */ | |
189 | xfs_inode_t *dp; /* incore directory inode */ | |
190 | int highstale; /* next stale entry */ | |
191 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
192 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
193 | int lfloghigh; /* high leaf entry logging */ | |
194 | int lfloglow; /* low leaf entry logging */ | |
195 | int lowstale; /* previous stale entry */ | |
196 | xfs_mount_t *mp; /* filesystem mount point */ | |
197 | xfs_trans_t *tp; /* transaction pointer */ | |
198 | ||
0b1b213f CH |
199 | trace_xfs_dir2_leafn_add(args, index); |
200 | ||
1da177e4 LT |
201 | dp = args->dp; |
202 | mp = dp->i_mount; | |
203 | tp = args->trans; | |
204 | leaf = bp->data; | |
205 | ||
206 | /* | |
207 | * Quick check just to make sure we are not going to index | |
208 | * into other peoples memory | |
209 | */ | |
210 | if (index < 0) | |
211 | return XFS_ERROR(EFSCORRUPTED); | |
212 | ||
213 | /* | |
214 | * If there are already the maximum number of leaf entries in | |
215 | * the block, if there are no stale entries it won't fit. | |
216 | * Caller will do a split. If there are stale entries we'll do | |
217 | * a compact. | |
218 | */ | |
219 | ||
bbaaf538 | 220 | if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) { |
1da177e4 LT |
221 | if (!leaf->hdr.stale) |
222 | return XFS_ERROR(ENOSPC); | |
a818e5de | 223 | compact = be16_to_cpu(leaf->hdr.stale) > 1; |
1da177e4 LT |
224 | } else |
225 | compact = 0; | |
3c1f9c15 | 226 | ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval); |
a818e5de | 227 | ASSERT(index == be16_to_cpu(leaf->hdr.count) || |
3c1f9c15 | 228 | be32_to_cpu(leaf->ents[index].hashval) >= args->hashval); |
1da177e4 | 229 | |
6a178100 | 230 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) |
1da177e4 LT |
231 | return 0; |
232 | ||
233 | /* | |
234 | * Compact out all but one stale leaf entry. Leaves behind | |
235 | * the entry closest to index. | |
236 | */ | |
237 | if (compact) { | |
238 | xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale, | |
239 | &lfloglow, &lfloghigh); | |
240 | } | |
241 | /* | |
242 | * Set impossible logging indices for this case. | |
243 | */ | |
244 | else if (leaf->hdr.stale) { | |
a818e5de | 245 | lfloglow = be16_to_cpu(leaf->hdr.count); |
1da177e4 LT |
246 | lfloghigh = -1; |
247 | } | |
248 | /* | |
249 | * No stale entries, just insert a space for the new entry. | |
250 | */ | |
251 | if (!leaf->hdr.stale) { | |
252 | lep = &leaf->ents[index]; | |
a818e5de | 253 | if (index < be16_to_cpu(leaf->hdr.count)) |
1da177e4 | 254 | memmove(lep + 1, lep, |
a818e5de | 255 | (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep)); |
1da177e4 | 256 | lfloglow = index; |
a818e5de | 257 | lfloghigh = be16_to_cpu(leaf->hdr.count); |
413d57c9 | 258 | be16_add_cpu(&leaf->hdr.count, 1); |
1da177e4 LT |
259 | } |
260 | /* | |
261 | * There are stale entries. We'll use one for the new entry. | |
262 | */ | |
263 | else { | |
264 | /* | |
265 | * If we didn't do a compact then we need to figure out | |
266 | * which stale entry will be used. | |
267 | */ | |
268 | if (compact == 0) { | |
269 | /* | |
270 | * Find first stale entry before our insertion point. | |
271 | */ | |
272 | for (lowstale = index - 1; | |
273 | lowstale >= 0 && | |
3c1f9c15 | 274 | be32_to_cpu(leaf->ents[lowstale].address) != |
1da177e4 LT |
275 | XFS_DIR2_NULL_DATAPTR; |
276 | lowstale--) | |
277 | continue; | |
278 | /* | |
279 | * Find next stale entry after insertion point. | |
280 | * Stop looking if the answer would be worse than | |
281 | * lowstale already found. | |
282 | */ | |
283 | for (highstale = index; | |
a818e5de | 284 | highstale < be16_to_cpu(leaf->hdr.count) && |
3c1f9c15 | 285 | be32_to_cpu(leaf->ents[highstale].address) != |
1da177e4 LT |
286 | XFS_DIR2_NULL_DATAPTR && |
287 | (lowstale < 0 || | |
288 | index - lowstale - 1 >= highstale - index); | |
289 | highstale++) | |
290 | continue; | |
291 | } | |
292 | /* | |
293 | * Using the low stale entry. | |
294 | * Shift entries up toward the stale slot. | |
295 | */ | |
296 | if (lowstale >= 0 && | |
a818e5de | 297 | (highstale == be16_to_cpu(leaf->hdr.count) || |
1da177e4 | 298 | index - lowstale - 1 < highstale - index)) { |
3c1f9c15 | 299 | ASSERT(be32_to_cpu(leaf->ents[lowstale].address) == |
1da177e4 LT |
300 | XFS_DIR2_NULL_DATAPTR); |
301 | ASSERT(index - lowstale - 1 >= 0); | |
302 | if (index - lowstale - 1 > 0) | |
303 | memmove(&leaf->ents[lowstale], | |
304 | &leaf->ents[lowstale + 1], | |
305 | (index - lowstale - 1) * sizeof(*lep)); | |
306 | lep = &leaf->ents[index - 1]; | |
307 | lfloglow = MIN(lowstale, lfloglow); | |
308 | lfloghigh = MAX(index - 1, lfloghigh); | |
309 | } | |
310 | /* | |
311 | * Using the high stale entry. | |
312 | * Shift entries down toward the stale slot. | |
313 | */ | |
314 | else { | |
3c1f9c15 | 315 | ASSERT(be32_to_cpu(leaf->ents[highstale].address) == |
1da177e4 LT |
316 | XFS_DIR2_NULL_DATAPTR); |
317 | ASSERT(highstale - index >= 0); | |
318 | if (highstale - index > 0) | |
319 | memmove(&leaf->ents[index + 1], | |
320 | &leaf->ents[index], | |
321 | (highstale - index) * sizeof(*lep)); | |
322 | lep = &leaf->ents[index]; | |
323 | lfloglow = MIN(index, lfloglow); | |
324 | lfloghigh = MAX(highstale, lfloghigh); | |
325 | } | |
413d57c9 | 326 | be16_add_cpu(&leaf->hdr.stale, -1); |
1da177e4 LT |
327 | } |
328 | /* | |
329 | * Insert the new entry, log everything. | |
330 | */ | |
3c1f9c15 | 331 | lep->hashval = cpu_to_be32(args->hashval); |
bbaaf538 | 332 | lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, |
3c1f9c15 | 333 | args->blkno, args->index)); |
1da177e4 LT |
334 | xfs_dir2_leaf_log_header(tp, bp); |
335 | xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh); | |
336 | xfs_dir2_leafn_check(dp, bp); | |
337 | return 0; | |
338 | } | |
339 | ||
340 | #ifdef DEBUG | |
341 | /* | |
342 | * Check internal consistency of a leafn block. | |
343 | */ | |
344 | void | |
345 | xfs_dir2_leafn_check( | |
346 | xfs_inode_t *dp, /* incore directory inode */ | |
347 | xfs_dabuf_t *bp) /* leaf buffer */ | |
348 | { | |
349 | int i; /* leaf index */ | |
350 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
351 | xfs_mount_t *mp; /* filesystem mount point */ | |
352 | int stale; /* count of stale leaves */ | |
353 | ||
354 | leaf = bp->data; | |
355 | mp = dp->i_mount; | |
89da0544 | 356 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
bbaaf538 | 357 | ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp)); |
a818e5de NS |
358 | for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) { |
359 | if (i + 1 < be16_to_cpu(leaf->hdr.count)) { | |
3c1f9c15 NS |
360 | ASSERT(be32_to_cpu(leaf->ents[i].hashval) <= |
361 | be32_to_cpu(leaf->ents[i + 1].hashval)); | |
1da177e4 | 362 | } |
3c1f9c15 | 363 | if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR) |
1da177e4 LT |
364 | stale++; |
365 | } | |
a818e5de | 366 | ASSERT(be16_to_cpu(leaf->hdr.stale) == stale); |
1da177e4 LT |
367 | } |
368 | #endif /* DEBUG */ | |
369 | ||
370 | /* | |
371 | * Return the last hash value in the leaf. | |
372 | * Stale entries are ok. | |
373 | */ | |
374 | xfs_dahash_t /* hash value */ | |
375 | xfs_dir2_leafn_lasthash( | |
376 | xfs_dabuf_t *bp, /* leaf buffer */ | |
377 | int *count) /* count of entries in leaf */ | |
378 | { | |
379 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
380 | ||
381 | leaf = bp->data; | |
89da0544 | 382 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
1da177e4 | 383 | if (count) |
a818e5de | 384 | *count = be16_to_cpu(leaf->hdr.count); |
1da177e4 LT |
385 | if (!leaf->hdr.count) |
386 | return 0; | |
3c1f9c15 | 387 | return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval); |
1da177e4 LT |
388 | } |
389 | ||
390 | /* | |
f9f6dce0 BN |
391 | * Look up a leaf entry for space to add a name in a node-format leaf block. |
392 | * The extrablk in state is a freespace block. | |
1da177e4 | 393 | */ |
f9f6dce0 BN |
394 | STATIC int |
395 | xfs_dir2_leafn_lookup_for_addname( | |
1da177e4 LT |
396 | xfs_dabuf_t *bp, /* leaf buffer */ |
397 | xfs_da_args_t *args, /* operation arguments */ | |
398 | int *indexp, /* out: leaf entry index */ | |
399 | xfs_da_state_t *state) /* state to fill in */ | |
400 | { | |
f9f6dce0 BN |
401 | xfs_dabuf_t *curbp = NULL; /* current data/free buffer */ |
402 | xfs_dir2_db_t curdb = -1; /* current data block number */ | |
403 | xfs_dir2_db_t curfdb = -1; /* current free block number */ | |
1da177e4 LT |
404 | xfs_inode_t *dp; /* incore directory inode */ |
405 | int error; /* error return value */ | |
406 | int fi; /* free entry index */ | |
f9f6dce0 | 407 | xfs_dir2_free_t *free = NULL; /* free block structure */ |
1da177e4 LT |
408 | int index; /* leaf entry index */ |
409 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
f9f6dce0 | 410 | int length; /* length of new data entry */ |
1da177e4 LT |
411 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
412 | xfs_mount_t *mp; /* filesystem mount point */ | |
413 | xfs_dir2_db_t newdb; /* new data block number */ | |
414 | xfs_dir2_db_t newfdb; /* new free block number */ | |
415 | xfs_trans_t *tp; /* transaction pointer */ | |
416 | ||
417 | dp = args->dp; | |
418 | tp = args->trans; | |
419 | mp = dp->i_mount; | |
420 | leaf = bp->data; | |
89da0544 | 421 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
1da177e4 | 422 | #ifdef __KERNEL__ |
a818e5de | 423 | ASSERT(be16_to_cpu(leaf->hdr.count) > 0); |
1da177e4 LT |
424 | #endif |
425 | xfs_dir2_leafn_check(dp, bp); | |
426 | /* | |
427 | * Look up the hash value in the leaf entries. | |
428 | */ | |
429 | index = xfs_dir2_leaf_search_hash(args, bp); | |
430 | /* | |
431 | * Do we have a buffer coming in? | |
432 | */ | |
f9f6dce0 BN |
433 | if (state->extravalid) { |
434 | /* If so, it's a free block buffer, get the block number. */ | |
1da177e4 | 435 | curbp = state->extrablk.bp; |
f9f6dce0 BN |
436 | curfdb = state->extrablk.blkno; |
437 | free = curbp->data; | |
438 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); | |
1da177e4 | 439 | } |
f9f6dce0 | 440 | length = xfs_dir2_data_entsize(args->namelen); |
1da177e4 LT |
441 | /* |
442 | * Loop over leaf entries with the right hash value. | |
443 | */ | |
f9f6dce0 BN |
444 | for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) && |
445 | be32_to_cpu(lep->hashval) == args->hashval; | |
446 | lep++, index++) { | |
1da177e4 LT |
447 | /* |
448 | * Skip stale leaf entries. | |
449 | */ | |
3c1f9c15 | 450 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
1da177e4 LT |
451 | continue; |
452 | /* | |
453 | * Pull the data block number from the entry. | |
454 | */ | |
bbaaf538 | 455 | newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
1da177e4 LT |
456 | /* |
457 | * For addname, we're looking for a place to put the new entry. | |
458 | * We want to use a data block with an entry of equal | |
459 | * hash value to ours if there is one with room. | |
f9f6dce0 BN |
460 | * |
461 | * If this block isn't the data block we already have | |
462 | * in hand, take a look at it. | |
1da177e4 | 463 | */ |
f9f6dce0 BN |
464 | if (newdb != curdb) { |
465 | curdb = newdb; | |
1da177e4 | 466 | /* |
f9f6dce0 BN |
467 | * Convert the data block to the free block |
468 | * holding its freespace information. | |
1da177e4 | 469 | */ |
f9f6dce0 | 470 | newfdb = xfs_dir2_db_to_fdb(mp, newdb); |
1da177e4 | 471 | /* |
f9f6dce0 | 472 | * If it's not the one we have in hand, read it in. |
1da177e4 | 473 | */ |
f9f6dce0 | 474 | if (newfdb != curfdb) { |
1da177e4 | 475 | /* |
f9f6dce0 | 476 | * If we had one before, drop it. |
1da177e4 LT |
477 | */ |
478 | if (curbp) | |
479 | xfs_da_brelse(tp, curbp); | |
480 | /* | |
f9f6dce0 | 481 | * Read the free block. |
1da177e4 | 482 | */ |
f9f6dce0 BN |
483 | error = xfs_da_read_buf(tp, dp, |
484 | xfs_dir2_db_to_da(mp, newfdb), | |
485 | -1, &curbp, XFS_DATA_FORK); | |
486 | if (error) | |
1da177e4 | 487 | return error; |
f9f6dce0 BN |
488 | free = curbp->data; |
489 | ASSERT(be32_to_cpu(free->hdr.magic) == | |
490 | XFS_DIR2_FREE_MAGIC); | |
491 | ASSERT((be32_to_cpu(free->hdr.firstdb) % | |
492 | XFS_DIR2_MAX_FREE_BESTS(mp)) == 0); | |
493 | ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb); | |
494 | ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) + | |
495 | be32_to_cpu(free->hdr.nvalid)); | |
1da177e4 LT |
496 | } |
497 | /* | |
f9f6dce0 | 498 | * Get the index for our entry. |
1da177e4 | 499 | */ |
f9f6dce0 | 500 | fi = xfs_dir2_db_to_fdindex(mp, curdb); |
1da177e4 | 501 | /* |
f9f6dce0 | 502 | * If it has room, return it. |
1da177e4 | 503 | */ |
f9f6dce0 BN |
504 | if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) { |
505 | XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int", | |
506 | XFS_ERRLEVEL_LOW, mp); | |
507 | if (curfdb != newfdb) | |
508 | xfs_da_brelse(tp, curbp); | |
509 | return XFS_ERROR(EFSCORRUPTED); | |
1da177e4 | 510 | } |
f9f6dce0 BN |
511 | curfdb = newfdb; |
512 | if (be16_to_cpu(free->bests[fi]) >= length) | |
513 | goto out; | |
1da177e4 LT |
514 | } |
515 | } | |
f9f6dce0 BN |
516 | /* Didn't find any space */ |
517 | fi = -1; | |
518 | out: | |
6a178100 | 519 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
f9f6dce0 BN |
520 | if (curbp) { |
521 | /* Giving back a free block. */ | |
522 | state->extravalid = 1; | |
523 | state->extrablk.bp = curbp; | |
524 | state->extrablk.index = fi; | |
525 | state->extrablk.blkno = curfdb; | |
526 | state->extrablk.magic = XFS_DIR2_FREE_MAGIC; | |
527 | } else { | |
528 | state->extravalid = 0; | |
529 | } | |
1da177e4 | 530 | /* |
f9f6dce0 | 531 | * Return the index, that will be the insertion point. |
1da177e4 | 532 | */ |
f9f6dce0 BN |
533 | *indexp = index; |
534 | return XFS_ERROR(ENOENT); | |
535 | } | |
536 | ||
537 | /* | |
538 | * Look up a leaf entry in a node-format leaf block. | |
539 | * The extrablk in state a data block. | |
540 | */ | |
541 | STATIC int | |
542 | xfs_dir2_leafn_lookup_for_entry( | |
543 | xfs_dabuf_t *bp, /* leaf buffer */ | |
544 | xfs_da_args_t *args, /* operation arguments */ | |
545 | int *indexp, /* out: leaf entry index */ | |
546 | xfs_da_state_t *state) /* state to fill in */ | |
547 | { | |
548 | xfs_dabuf_t *curbp = NULL; /* current data/free buffer */ | |
549 | xfs_dir2_db_t curdb = -1; /* current data block number */ | |
550 | xfs_dir2_data_entry_t *dep; /* data block entry */ | |
551 | xfs_inode_t *dp; /* incore directory inode */ | |
552 | int error; /* error return value */ | |
f9f6dce0 BN |
553 | int index; /* leaf entry index */ |
554 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
555 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
556 | xfs_mount_t *mp; /* filesystem mount point */ | |
557 | xfs_dir2_db_t newdb; /* new data block number */ | |
558 | xfs_trans_t *tp; /* transaction pointer */ | |
5163f95a | 559 | enum xfs_dacmp cmp; /* comparison result */ |
f9f6dce0 BN |
560 | |
561 | dp = args->dp; | |
562 | tp = args->trans; | |
563 | mp = dp->i_mount; | |
564 | leaf = bp->data; | |
565 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); | |
566 | #ifdef __KERNEL__ | |
567 | ASSERT(be16_to_cpu(leaf->hdr.count) > 0); | |
568 | #endif | |
569 | xfs_dir2_leafn_check(dp, bp); | |
570 | /* | |
571 | * Look up the hash value in the leaf entries. | |
572 | */ | |
573 | index = xfs_dir2_leaf_search_hash(args, bp); | |
574 | /* | |
575 | * Do we have a buffer coming in? | |
576 | */ | |
577 | if (state->extravalid) { | |
578 | curbp = state->extrablk.bp; | |
579 | curdb = state->extrablk.blkno; | |
580 | } | |
581 | /* | |
582 | * Loop over leaf entries with the right hash value. | |
583 | */ | |
584 | for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) && | |
585 | be32_to_cpu(lep->hashval) == args->hashval; | |
586 | lep++, index++) { | |
587 | /* | |
588 | * Skip stale leaf entries. | |
589 | */ | |
590 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) | |
591 | continue; | |
1da177e4 | 592 | /* |
f9f6dce0 BN |
593 | * Pull the data block number from the entry. |
594 | */ | |
595 | newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); | |
596 | /* | |
597 | * Not adding a new entry, so we really want to find | |
598 | * the name given to us. | |
599 | * | |
600 | * If it's a different data block, go get it. | |
1da177e4 | 601 | */ |
f9f6dce0 BN |
602 | if (newdb != curdb) { |
603 | /* | |
90bb7ab0 BN |
604 | * If we had a block before that we aren't saving |
605 | * for a CI name, drop it | |
f9f6dce0 | 606 | */ |
90bb7ab0 BN |
607 | if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT || |
608 | curdb != state->extrablk.blkno)) | |
f9f6dce0 BN |
609 | xfs_da_brelse(tp, curbp); |
610 | /* | |
90bb7ab0 BN |
611 | * If needing the block that is saved with a CI match, |
612 | * use it otherwise read in the new data block. | |
f9f6dce0 | 613 | */ |
90bb7ab0 BN |
614 | if (args->cmpresult != XFS_CMP_DIFFERENT && |
615 | newdb == state->extrablk.blkno) { | |
616 | ASSERT(state->extravalid); | |
617 | curbp = state->extrablk.bp; | |
618 | } else { | |
619 | error = xfs_da_read_buf(tp, dp, | |
620 | xfs_dir2_db_to_da(mp, newdb), | |
621 | -1, &curbp, XFS_DATA_FORK); | |
622 | if (error) | |
623 | return error; | |
624 | } | |
f9f6dce0 BN |
625 | xfs_dir2_data_check(dp, curbp); |
626 | curdb = newdb; | |
1da177e4 LT |
627 | } |
628 | /* | |
f9f6dce0 | 629 | * Point to the data entry. |
1da177e4 | 630 | */ |
f9f6dce0 BN |
631 | dep = (xfs_dir2_data_entry_t *)((char *)curbp->data + |
632 | xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address))); | |
633 | /* | |
5163f95a BN |
634 | * Compare the entry and if it's an exact match, return |
635 | * EEXIST immediately. If it's the first case-insensitive | |
90bb7ab0 | 636 | * match, store the block & inode number and continue looking. |
f9f6dce0 | 637 | */ |
5163f95a BN |
638 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
639 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { | |
90bb7ab0 BN |
640 | /* If there is a CI match block, drop it */ |
641 | if (args->cmpresult != XFS_CMP_DIFFERENT && | |
642 | curdb != state->extrablk.blkno) | |
643 | xfs_da_brelse(tp, state->extrablk.bp); | |
5163f95a | 644 | args->cmpresult = cmp; |
f9f6dce0 | 645 | args->inumber = be64_to_cpu(dep->inumber); |
90bb7ab0 BN |
646 | *indexp = index; |
647 | state->extravalid = 1; | |
648 | state->extrablk.bp = curbp; | |
649 | state->extrablk.blkno = curdb; | |
650 | state->extrablk.index = (int)((char *)dep - | |
651 | (char *)curbp->data); | |
652 | state->extrablk.magic = XFS_DIR2_DATA_MAGIC; | |
5163f95a | 653 | if (cmp == XFS_CMP_EXACT) |
90bb7ab0 | 654 | return XFS_ERROR(EEXIST); |
1da177e4 LT |
655 | } |
656 | } | |
6a178100 BN |
657 | ASSERT(index == be16_to_cpu(leaf->hdr.count) || |
658 | (args->op_flags & XFS_DA_OP_OKNOENT)); | |
f9f6dce0 | 659 | if (curbp) { |
90bb7ab0 BN |
660 | if (args->cmpresult == XFS_CMP_DIFFERENT) { |
661 | /* Giving back last used data block. */ | |
662 | state->extravalid = 1; | |
663 | state->extrablk.bp = curbp; | |
664 | state->extrablk.index = -1; | |
665 | state->extrablk.blkno = curdb; | |
666 | state->extrablk.magic = XFS_DIR2_DATA_MAGIC; | |
667 | } else { | |
668 | /* If the curbp is not the CI match block, drop it */ | |
669 | if (state->extrablk.bp != curbp) | |
670 | xfs_da_brelse(tp, curbp); | |
671 | } | |
f9f6dce0 BN |
672 | } else { |
673 | state->extravalid = 0; | |
674 | } | |
1da177e4 | 675 | *indexp = index; |
90bb7ab0 | 676 | return XFS_ERROR(ENOENT); |
f9f6dce0 BN |
677 | } |
678 | ||
679 | /* | |
680 | * Look up a leaf entry in a node-format leaf block. | |
681 | * If this is an addname then the extrablk in state is a freespace block, | |
682 | * otherwise it's a data block. | |
683 | */ | |
684 | int | |
685 | xfs_dir2_leafn_lookup_int( | |
686 | xfs_dabuf_t *bp, /* leaf buffer */ | |
687 | xfs_da_args_t *args, /* operation arguments */ | |
688 | int *indexp, /* out: leaf entry index */ | |
689 | xfs_da_state_t *state) /* state to fill in */ | |
690 | { | |
6a178100 | 691 | if (args->op_flags & XFS_DA_OP_ADDNAME) |
f9f6dce0 BN |
692 | return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp, |
693 | state); | |
694 | return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state); | |
1da177e4 LT |
695 | } |
696 | ||
697 | /* | |
698 | * Move count leaf entries from source to destination leaf. | |
699 | * Log entries and headers. Stale entries are preserved. | |
700 | */ | |
701 | static void | |
702 | xfs_dir2_leafn_moveents( | |
703 | xfs_da_args_t *args, /* operation arguments */ | |
704 | xfs_dabuf_t *bp_s, /* source leaf buffer */ | |
705 | int start_s, /* source leaf index */ | |
706 | xfs_dabuf_t *bp_d, /* destination leaf buffer */ | |
707 | int start_d, /* destination leaf index */ | |
708 | int count) /* count of leaves to copy */ | |
709 | { | |
710 | xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */ | |
711 | xfs_dir2_leaf_t *leaf_s; /* source leaf structure */ | |
712 | int stale; /* count stale leaves copied */ | |
713 | xfs_trans_t *tp; /* transaction pointer */ | |
714 | ||
0b1b213f CH |
715 | trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count); |
716 | ||
1da177e4 LT |
717 | /* |
718 | * Silently return if nothing to do. | |
719 | */ | |
720 | if (count == 0) { | |
721 | return; | |
722 | } | |
723 | tp = args->trans; | |
724 | leaf_s = bp_s->data; | |
725 | leaf_d = bp_d->data; | |
726 | /* | |
727 | * If the destination index is not the end of the current | |
728 | * destination leaf entries, open up a hole in the destination | |
729 | * to hold the new entries. | |
730 | */ | |
a818e5de | 731 | if (start_d < be16_to_cpu(leaf_d->hdr.count)) { |
1da177e4 | 732 | memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d], |
a818e5de | 733 | (be16_to_cpu(leaf_d->hdr.count) - start_d) * |
1da177e4 LT |
734 | sizeof(xfs_dir2_leaf_entry_t)); |
735 | xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count, | |
a818e5de | 736 | count + be16_to_cpu(leaf_d->hdr.count) - 1); |
1da177e4 LT |
737 | } |
738 | /* | |
739 | * If the source has stale leaves, count the ones in the copy range | |
740 | * so we can update the header correctly. | |
741 | */ | |
742 | if (leaf_s->hdr.stale) { | |
743 | int i; /* temp leaf index */ | |
744 | ||
745 | for (i = start_s, stale = 0; i < start_s + count; i++) { | |
3c1f9c15 | 746 | if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR) |
1da177e4 LT |
747 | stale++; |
748 | } | |
749 | } else | |
750 | stale = 0; | |
751 | /* | |
752 | * Copy the leaf entries from source to destination. | |
753 | */ | |
754 | memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s], | |
755 | count * sizeof(xfs_dir2_leaf_entry_t)); | |
756 | xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1); | |
757 | /* | |
758 | * If there are source entries after the ones we copied, | |
759 | * delete the ones we copied by sliding the next ones down. | |
760 | */ | |
a818e5de | 761 | if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) { |
1da177e4 LT |
762 | memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count], |
763 | count * sizeof(xfs_dir2_leaf_entry_t)); | |
764 | xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1); | |
765 | } | |
766 | /* | |
767 | * Update the headers and log them. | |
768 | */ | |
413d57c9 MS |
769 | be16_add_cpu(&leaf_s->hdr.count, -(count)); |
770 | be16_add_cpu(&leaf_s->hdr.stale, -(stale)); | |
771 | be16_add_cpu(&leaf_d->hdr.count, count); | |
772 | be16_add_cpu(&leaf_d->hdr.stale, stale); | |
1da177e4 LT |
773 | xfs_dir2_leaf_log_header(tp, bp_s); |
774 | xfs_dir2_leaf_log_header(tp, bp_d); | |
775 | xfs_dir2_leafn_check(args->dp, bp_s); | |
776 | xfs_dir2_leafn_check(args->dp, bp_d); | |
777 | } | |
778 | ||
779 | /* | |
780 | * Determine the sort order of two leaf blocks. | |
781 | * Returns 1 if both are valid and leaf2 should be before leaf1, else 0. | |
782 | */ | |
783 | int /* sort order */ | |
784 | xfs_dir2_leafn_order( | |
785 | xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */ | |
786 | xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */ | |
787 | { | |
788 | xfs_dir2_leaf_t *leaf1; /* leaf1 structure */ | |
789 | xfs_dir2_leaf_t *leaf2; /* leaf2 structure */ | |
790 | ||
791 | leaf1 = leaf1_bp->data; | |
792 | leaf2 = leaf2_bp->data; | |
89da0544 NS |
793 | ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
794 | ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); | |
a818e5de NS |
795 | if (be16_to_cpu(leaf1->hdr.count) > 0 && |
796 | be16_to_cpu(leaf2->hdr.count) > 0 && | |
3c1f9c15 NS |
797 | (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) || |
798 | be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) < | |
799 | be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval))) | |
1da177e4 LT |
800 | return 1; |
801 | return 0; | |
802 | } | |
803 | ||
804 | /* | |
805 | * Rebalance leaf entries between two leaf blocks. | |
806 | * This is actually only called when the second block is new, | |
807 | * though the code deals with the general case. | |
808 | * A new entry will be inserted in one of the blocks, and that | |
809 | * entry is taken into account when balancing. | |
810 | */ | |
811 | static void | |
812 | xfs_dir2_leafn_rebalance( | |
813 | xfs_da_state_t *state, /* btree cursor */ | |
814 | xfs_da_state_blk_t *blk1, /* first btree block */ | |
815 | xfs_da_state_blk_t *blk2) /* second btree block */ | |
816 | { | |
817 | xfs_da_args_t *args; /* operation arguments */ | |
818 | int count; /* count (& direction) leaves */ | |
819 | int isleft; /* new goes in left leaf */ | |
820 | xfs_dir2_leaf_t *leaf1; /* first leaf structure */ | |
821 | xfs_dir2_leaf_t *leaf2; /* second leaf structure */ | |
822 | int mid; /* midpoint leaf index */ | |
823 | #ifdef DEBUG | |
824 | int oldstale; /* old count of stale leaves */ | |
825 | #endif | |
826 | int oldsum; /* old total leaf count */ | |
827 | int swap; /* swapped leaf blocks */ | |
828 | ||
829 | args = state->args; | |
830 | /* | |
831 | * If the block order is wrong, swap the arguments. | |
832 | */ | |
833 | if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) { | |
834 | xfs_da_state_blk_t *tmp; /* temp for block swap */ | |
835 | ||
836 | tmp = blk1; | |
837 | blk1 = blk2; | |
838 | blk2 = tmp; | |
839 | } | |
840 | leaf1 = blk1->bp->data; | |
841 | leaf2 = blk2->bp->data; | |
a818e5de | 842 | oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count); |
1da177e4 | 843 | #ifdef DEBUG |
a818e5de | 844 | oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale); |
1da177e4 LT |
845 | #endif |
846 | mid = oldsum >> 1; | |
847 | /* | |
848 | * If the old leaf count was odd then the new one will be even, | |
849 | * so we need to divide the new count evenly. | |
850 | */ | |
851 | if (oldsum & 1) { | |
852 | xfs_dahash_t midhash; /* middle entry hash value */ | |
853 | ||
a818e5de | 854 | if (mid >= be16_to_cpu(leaf1->hdr.count)) |
3c1f9c15 | 855 | midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval); |
1da177e4 | 856 | else |
3c1f9c15 | 857 | midhash = be32_to_cpu(leaf1->ents[mid].hashval); |
1da177e4 LT |
858 | isleft = args->hashval <= midhash; |
859 | } | |
860 | /* | |
861 | * If the old count is even then the new count is odd, so there's | |
862 | * no preferred side for the new entry. | |
863 | * Pick the left one. | |
864 | */ | |
865 | else | |
866 | isleft = 1; | |
867 | /* | |
868 | * Calculate moved entry count. Positive means left-to-right, | |
869 | * negative means right-to-left. Then move the entries. | |
870 | */ | |
a818e5de | 871 | count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0); |
1da177e4 LT |
872 | if (count > 0) |
873 | xfs_dir2_leafn_moveents(args, blk1->bp, | |
a818e5de | 874 | be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count); |
1da177e4 LT |
875 | else if (count < 0) |
876 | xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp, | |
a818e5de NS |
877 | be16_to_cpu(leaf1->hdr.count), count); |
878 | ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum); | |
879 | ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale); | |
1da177e4 LT |
880 | /* |
881 | * Mark whether we're inserting into the old or new leaf. | |
882 | */ | |
a818e5de | 883 | if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count)) |
1da177e4 | 884 | state->inleaf = swap; |
a818e5de | 885 | else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count)) |
1da177e4 LT |
886 | state->inleaf = !swap; |
887 | else | |
888 | state->inleaf = | |
a818e5de | 889 | swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count)); |
1da177e4 LT |
890 | /* |
891 | * Adjust the expected index for insertion. | |
892 | */ | |
893 | if (!state->inleaf) | |
a818e5de | 894 | blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count); |
f9f6dce0 BN |
895 | |
896 | /* | |
897 | * Finally sanity check just to make sure we are not returning a | |
898 | * negative index | |
1da177e4 LT |
899 | */ |
900 | if(blk2->index < 0) { | |
901 | state->inleaf = 1; | |
902 | blk2->index = 0; | |
903 | cmn_err(CE_ALERT, | |
c41564b5 | 904 | "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: " |
1da177e4 LT |
905 | "blk1->index %d\n", |
906 | blk1->index); | |
907 | } | |
908 | } | |
909 | ||
910 | /* | |
911 | * Remove an entry from a node directory. | |
912 | * This removes the leaf entry and the data entry, | |
913 | * and updates the free block if necessary. | |
914 | */ | |
915 | static int /* error */ | |
916 | xfs_dir2_leafn_remove( | |
917 | xfs_da_args_t *args, /* operation arguments */ | |
918 | xfs_dabuf_t *bp, /* leaf buffer */ | |
919 | int index, /* leaf entry index */ | |
920 | xfs_da_state_blk_t *dblk, /* data block */ | |
921 | int *rval) /* resulting block needs join */ | |
922 | { | |
923 | xfs_dir2_data_t *data; /* data block structure */ | |
924 | xfs_dir2_db_t db; /* data block number */ | |
925 | xfs_dabuf_t *dbp; /* data block buffer */ | |
926 | xfs_dir2_data_entry_t *dep; /* data block entry */ | |
927 | xfs_inode_t *dp; /* incore directory inode */ | |
928 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
929 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
930 | int longest; /* longest data free entry */ | |
931 | int off; /* data block entry offset */ | |
932 | xfs_mount_t *mp; /* filesystem mount point */ | |
933 | int needlog; /* need to log data header */ | |
934 | int needscan; /* need to rescan data frees */ | |
935 | xfs_trans_t *tp; /* transaction pointer */ | |
936 | ||
0b1b213f CH |
937 | trace_xfs_dir2_leafn_remove(args, index); |
938 | ||
1da177e4 LT |
939 | dp = args->dp; |
940 | tp = args->trans; | |
941 | mp = dp->i_mount; | |
942 | leaf = bp->data; | |
89da0544 | 943 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
1da177e4 LT |
944 | /* |
945 | * Point to the entry we're removing. | |
946 | */ | |
947 | lep = &leaf->ents[index]; | |
948 | /* | |
949 | * Extract the data block and offset from the entry. | |
950 | */ | |
bbaaf538 | 951 | db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
1da177e4 | 952 | ASSERT(dblk->blkno == db); |
bbaaf538 | 953 | off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)); |
1da177e4 LT |
954 | ASSERT(dblk->index == off); |
955 | /* | |
956 | * Kill the leaf entry by marking it stale. | |
957 | * Log the leaf block changes. | |
958 | */ | |
413d57c9 | 959 | be16_add_cpu(&leaf->hdr.stale, 1); |
1da177e4 | 960 | xfs_dir2_leaf_log_header(tp, bp); |
3c1f9c15 | 961 | lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
1da177e4 LT |
962 | xfs_dir2_leaf_log_ents(tp, bp, index, index); |
963 | /* | |
964 | * Make the data entry free. Keep track of the longest freespace | |
965 | * in the data block in case it changes. | |
966 | */ | |
967 | dbp = dblk->bp; | |
968 | data = dbp->data; | |
969 | dep = (xfs_dir2_data_entry_t *)((char *)data + off); | |
70e73f59 | 970 | longest = be16_to_cpu(data->hdr.bestfree[0].length); |
1da177e4 LT |
971 | needlog = needscan = 0; |
972 | xfs_dir2_data_make_free(tp, dbp, off, | |
bbaaf538 | 973 | xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan); |
1da177e4 LT |
974 | /* |
975 | * Rescan the data block freespaces for bestfree. | |
976 | * Log the data block header if needed. | |
977 | */ | |
978 | if (needscan) | |
ef497f8a | 979 | xfs_dir2_data_freescan(mp, data, &needlog); |
1da177e4 LT |
980 | if (needlog) |
981 | xfs_dir2_data_log_header(tp, dbp); | |
982 | xfs_dir2_data_check(dp, dbp); | |
983 | /* | |
984 | * If the longest data block freespace changes, need to update | |
985 | * the corresponding freeblock entry. | |
986 | */ | |
70e73f59 | 987 | if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) { |
1da177e4 LT |
988 | int error; /* error return value */ |
989 | xfs_dabuf_t *fbp; /* freeblock buffer */ | |
990 | xfs_dir2_db_t fdb; /* freeblock block number */ | |
991 | int findex; /* index in freeblock entries */ | |
992 | xfs_dir2_free_t *free; /* freeblock structure */ | |
993 | int logfree; /* need to log free entry */ | |
994 | ||
995 | /* | |
996 | * Convert the data block number to a free block, | |
997 | * read in the free block. | |
998 | */ | |
bbaaf538 CH |
999 | fdb = xfs_dir2_db_to_fdb(mp, db); |
1000 | if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), | |
1da177e4 LT |
1001 | -1, &fbp, XFS_DATA_FORK))) { |
1002 | return error; | |
1003 | } | |
1004 | free = fbp->data; | |
0ba962ef NS |
1005 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
1006 | ASSERT(be32_to_cpu(free->hdr.firstdb) == | |
1da177e4 LT |
1007 | XFS_DIR2_MAX_FREE_BESTS(mp) * |
1008 | (fdb - XFS_DIR2_FREE_FIRSTDB(mp))); | |
1009 | /* | |
1010 | * Calculate which entry we need to fix. | |
1011 | */ | |
bbaaf538 | 1012 | findex = xfs_dir2_db_to_fdindex(mp, db); |
70e73f59 | 1013 | longest = be16_to_cpu(data->hdr.bestfree[0].length); |
1da177e4 LT |
1014 | /* |
1015 | * If the data block is now empty we can get rid of it | |
1016 | * (usually). | |
1017 | */ | |
1018 | if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) { | |
1019 | /* | |
1020 | * Try to punch out the data block. | |
1021 | */ | |
1022 | error = xfs_dir2_shrink_inode(args, db, dbp); | |
1023 | if (error == 0) { | |
1024 | dblk->bp = NULL; | |
1025 | data = NULL; | |
1026 | } | |
1027 | /* | |
1028 | * We can get ENOSPC if there's no space reservation. | |
1029 | * In this case just drop the buffer and some one else | |
1030 | * will eventually get rid of the empty block. | |
1031 | */ | |
1032 | else if (error == ENOSPC && args->total == 0) | |
1033 | xfs_da_buf_done(dbp); | |
1034 | else | |
1035 | return error; | |
1036 | } | |
1037 | /* | |
1038 | * If we got rid of the data block, we can eliminate that entry | |
1039 | * in the free block. | |
1040 | */ | |
1041 | if (data == NULL) { | |
1042 | /* | |
1043 | * One less used entry in the free table. | |
1044 | */ | |
413d57c9 | 1045 | be32_add_cpu(&free->hdr.nused, -1); |
1da177e4 LT |
1046 | xfs_dir2_free_log_header(tp, fbp); |
1047 | /* | |
1048 | * If this was the last entry in the table, we can | |
1049 | * trim the table size back. There might be other | |
1050 | * entries at the end referring to non-existent | |
1051 | * data blocks, get those too. | |
1052 | */ | |
0ba962ef | 1053 | if (findex == be32_to_cpu(free->hdr.nvalid) - 1) { |
1da177e4 LT |
1054 | int i; /* free entry index */ |
1055 | ||
1056 | for (i = findex - 1; | |
0ba962ef | 1057 | i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF; |
1da177e4 LT |
1058 | i--) |
1059 | continue; | |
0ba962ef | 1060 | free->hdr.nvalid = cpu_to_be32(i + 1); |
1da177e4 LT |
1061 | logfree = 0; |
1062 | } | |
1063 | /* | |
1064 | * Not the last entry, just punch it out. | |
1065 | */ | |
1066 | else { | |
0ba962ef | 1067 | free->bests[findex] = cpu_to_be16(NULLDATAOFF); |
1da177e4 LT |
1068 | logfree = 1; |
1069 | } | |
1070 | /* | |
1071 | * If there are no useful entries left in the block, | |
1072 | * get rid of the block if we can. | |
1073 | */ | |
1074 | if (!free->hdr.nused) { | |
1075 | error = xfs_dir2_shrink_inode(args, fdb, fbp); | |
1076 | if (error == 0) { | |
1077 | fbp = NULL; | |
1078 | logfree = 0; | |
1079 | } else if (error != ENOSPC || args->total != 0) | |
1080 | return error; | |
1081 | /* | |
1082 | * It's possible to get ENOSPC if there is no | |
1083 | * space reservation. In this case some one | |
1084 | * else will eventually get rid of this block. | |
1085 | */ | |
1086 | } | |
1087 | } | |
1088 | /* | |
1089 | * Data block is not empty, just set the free entry to | |
1090 | * the new value. | |
1091 | */ | |
1092 | else { | |
0ba962ef | 1093 | free->bests[findex] = cpu_to_be16(longest); |
1da177e4 LT |
1094 | logfree = 1; |
1095 | } | |
1096 | /* | |
1097 | * Log the free entry that changed, unless we got rid of it. | |
1098 | */ | |
1099 | if (logfree) | |
1100 | xfs_dir2_free_log_bests(tp, fbp, findex, findex); | |
1101 | /* | |
1102 | * Drop the buffer if we still have it. | |
1103 | */ | |
1104 | if (fbp) | |
1105 | xfs_da_buf_done(fbp); | |
1106 | } | |
1107 | xfs_dir2_leafn_check(dp, bp); | |
1108 | /* | |
9da096fd | 1109 | * Return indication of whether this leaf block is empty enough |
1da177e4 LT |
1110 | * to justify trying to join it with a neighbor. |
1111 | */ | |
1112 | *rval = | |
1113 | ((uint)sizeof(leaf->hdr) + | |
1114 | (uint)sizeof(leaf->ents[0]) * | |
a818e5de | 1115 | (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) < |
1da177e4 LT |
1116 | mp->m_dir_magicpct; |
1117 | return 0; | |
1118 | } | |
1119 | ||
1120 | /* | |
1121 | * Split the leaf entries in the old block into old and new blocks. | |
1122 | */ | |
1123 | int /* error */ | |
1124 | xfs_dir2_leafn_split( | |
1125 | xfs_da_state_t *state, /* btree cursor */ | |
1126 | xfs_da_state_blk_t *oldblk, /* original block */ | |
1127 | xfs_da_state_blk_t *newblk) /* newly created block */ | |
1128 | { | |
1129 | xfs_da_args_t *args; /* operation arguments */ | |
1130 | xfs_dablk_t blkno; /* new leaf block number */ | |
1131 | int error; /* error return value */ | |
1132 | xfs_mount_t *mp; /* filesystem mount point */ | |
1133 | ||
1134 | /* | |
1135 | * Allocate space for a new leaf node. | |
1136 | */ | |
1137 | args = state->args; | |
1138 | mp = args->dp->i_mount; | |
1139 | ASSERT(args != NULL); | |
1140 | ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC); | |
1141 | error = xfs_da_grow_inode(args, &blkno); | |
1142 | if (error) { | |
1143 | return error; | |
1144 | } | |
1145 | /* | |
1146 | * Initialize the new leaf block. | |
1147 | */ | |
bbaaf538 | 1148 | error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno), |
1da177e4 LT |
1149 | &newblk->bp, XFS_DIR2_LEAFN_MAGIC); |
1150 | if (error) { | |
1151 | return error; | |
1152 | } | |
1153 | newblk->blkno = blkno; | |
1154 | newblk->magic = XFS_DIR2_LEAFN_MAGIC; | |
1155 | /* | |
1156 | * Rebalance the entries across the two leaves, link the new | |
1157 | * block into the leaves. | |
1158 | */ | |
1159 | xfs_dir2_leafn_rebalance(state, oldblk, newblk); | |
1160 | error = xfs_da_blk_link(state, oldblk, newblk); | |
1161 | if (error) { | |
1162 | return error; | |
1163 | } | |
1164 | /* | |
1165 | * Insert the new entry in the correct block. | |
1166 | */ | |
1167 | if (state->inleaf) | |
1168 | error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index); | |
1169 | else | |
1170 | error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index); | |
1171 | /* | |
1172 | * Update last hashval in each block since we added the name. | |
1173 | */ | |
1174 | oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL); | |
1175 | newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL); | |
1176 | xfs_dir2_leafn_check(args->dp, oldblk->bp); | |
1177 | xfs_dir2_leafn_check(args->dp, newblk->bp); | |
1178 | return error; | |
1179 | } | |
1180 | ||
1181 | /* | |
1182 | * Check a leaf block and its neighbors to see if the block should be | |
1183 | * collapsed into one or the other neighbor. Always keep the block | |
1184 | * with the smaller block number. | |
1185 | * If the current block is over 50% full, don't try to join it, return 0. | |
1186 | * If the block is empty, fill in the state structure and return 2. | |
1187 | * If it can be collapsed, fill in the state structure and return 1. | |
1188 | * If nothing can be done, return 0. | |
1189 | */ | |
1190 | int /* error */ | |
1191 | xfs_dir2_leafn_toosmall( | |
1192 | xfs_da_state_t *state, /* btree cursor */ | |
1193 | int *action) /* resulting action to take */ | |
1194 | { | |
1195 | xfs_da_state_blk_t *blk; /* leaf block */ | |
1196 | xfs_dablk_t blkno; /* leaf block number */ | |
1197 | xfs_dabuf_t *bp; /* leaf buffer */ | |
1198 | int bytes; /* bytes in use */ | |
1199 | int count; /* leaf live entry count */ | |
1200 | int error; /* error return value */ | |
1201 | int forward; /* sibling block direction */ | |
1202 | int i; /* sibling counter */ | |
1203 | xfs_da_blkinfo_t *info; /* leaf block header */ | |
1204 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1205 | int rval; /* result from path_shift */ | |
1206 | ||
1207 | /* | |
1208 | * Check for the degenerate case of the block being over 50% full. | |
1209 | * If so, it's not worth even looking to see if we might be able | |
1210 | * to coalesce with a sibling. | |
1211 | */ | |
1212 | blk = &state->path.blk[state->path.active - 1]; | |
1213 | info = blk->bp->data; | |
89da0544 | 1214 | ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC); |
1da177e4 | 1215 | leaf = (xfs_dir2_leaf_t *)info; |
a818e5de | 1216 | count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale); |
1da177e4 LT |
1217 | bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]); |
1218 | if (bytes > (state->blocksize >> 1)) { | |
1219 | /* | |
1220 | * Blk over 50%, don't try to join. | |
1221 | */ | |
1222 | *action = 0; | |
1223 | return 0; | |
1224 | } | |
1225 | /* | |
1226 | * Check for the degenerate case of the block being empty. | |
1227 | * If the block is empty, we'll simply delete it, no need to | |
1228 | * coalesce it with a sibling block. We choose (arbitrarily) | |
1229 | * to merge with the forward block unless it is NULL. | |
1230 | */ | |
1231 | if (count == 0) { | |
1232 | /* | |
1233 | * Make altpath point to the block we want to keep and | |
1234 | * path point to the block we want to drop (this one). | |
1235 | */ | |
89da0544 | 1236 | forward = (info->forw != 0); |
1da177e4 LT |
1237 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
1238 | error = xfs_da_path_shift(state, &state->altpath, forward, 0, | |
1239 | &rval); | |
1240 | if (error) | |
1241 | return error; | |
1242 | *action = rval ? 2 : 0; | |
1243 | return 0; | |
1244 | } | |
1245 | /* | |
1246 | * Examine each sibling block to see if we can coalesce with | |
1247 | * at least 25% free space to spare. We need to figure out | |
1248 | * whether to merge with the forward or the backward block. | |
1249 | * We prefer coalescing with the lower numbered sibling so as | |
1250 | * to shrink a directory over time. | |
1251 | */ | |
89da0544 | 1252 | forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back); |
1da177e4 | 1253 | for (i = 0, bp = NULL; i < 2; forward = !forward, i++) { |
89da0544 | 1254 | blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back); |
1da177e4 LT |
1255 | if (blkno == 0) |
1256 | continue; | |
1257 | /* | |
1258 | * Read the sibling leaf block. | |
1259 | */ | |
1260 | if ((error = | |
1261 | xfs_da_read_buf(state->args->trans, state->args->dp, blkno, | |
1262 | -1, &bp, XFS_DATA_FORK))) { | |
1263 | return error; | |
1264 | } | |
1265 | ASSERT(bp != NULL); | |
1266 | /* | |
1267 | * Count bytes in the two blocks combined. | |
1268 | */ | |
1269 | leaf = (xfs_dir2_leaf_t *)info; | |
a818e5de | 1270 | count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale); |
1da177e4 LT |
1271 | bytes = state->blocksize - (state->blocksize >> 2); |
1272 | leaf = bp->data; | |
89da0544 | 1273 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
a818e5de | 1274 | count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale); |
1da177e4 LT |
1275 | bytes -= count * (uint)sizeof(leaf->ents[0]); |
1276 | /* | |
1277 | * Fits with at least 25% to spare. | |
1278 | */ | |
1279 | if (bytes >= 0) | |
1280 | break; | |
1281 | xfs_da_brelse(state->args->trans, bp); | |
1282 | } | |
1283 | /* | |
1284 | * Didn't like either block, give up. | |
1285 | */ | |
1286 | if (i >= 2) { | |
1287 | *action = 0; | |
1288 | return 0; | |
1289 | } | |
1290 | /* | |
1291 | * Done with the sibling leaf block here, drop the dabuf | |
1292 | * so path_shift can get it. | |
1293 | */ | |
1294 | xfs_da_buf_done(bp); | |
1295 | /* | |
1296 | * Make altpath point to the block we want to keep (the lower | |
1297 | * numbered block) and path point to the block we want to drop. | |
1298 | */ | |
1299 | memcpy(&state->altpath, &state->path, sizeof(state->path)); | |
1300 | if (blkno < blk->blkno) | |
1301 | error = xfs_da_path_shift(state, &state->altpath, forward, 0, | |
1302 | &rval); | |
1303 | else | |
1304 | error = xfs_da_path_shift(state, &state->path, forward, 0, | |
1305 | &rval); | |
1306 | if (error) { | |
1307 | return error; | |
1308 | } | |
1309 | *action = rval ? 0 : 1; | |
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | /* | |
1314 | * Move all the leaf entries from drop_blk to save_blk. | |
1315 | * This is done as part of a join operation. | |
1316 | */ | |
1317 | void | |
1318 | xfs_dir2_leafn_unbalance( | |
1319 | xfs_da_state_t *state, /* cursor */ | |
1320 | xfs_da_state_blk_t *drop_blk, /* dead block */ | |
1321 | xfs_da_state_blk_t *save_blk) /* surviving block */ | |
1322 | { | |
1323 | xfs_da_args_t *args; /* operation arguments */ | |
1324 | xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */ | |
1325 | xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */ | |
1326 | ||
1327 | args = state->args; | |
1328 | ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC); | |
1329 | ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC); | |
1330 | drop_leaf = drop_blk->bp->data; | |
1331 | save_leaf = save_blk->bp->data; | |
89da0544 NS |
1332 | ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
1333 | ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); | |
1da177e4 LT |
1334 | /* |
1335 | * If there are any stale leaf entries, take this opportunity | |
1336 | * to purge them. | |
1337 | */ | |
a818e5de | 1338 | if (drop_leaf->hdr.stale) |
1da177e4 | 1339 | xfs_dir2_leaf_compact(args, drop_blk->bp); |
a818e5de | 1340 | if (save_leaf->hdr.stale) |
1da177e4 LT |
1341 | xfs_dir2_leaf_compact(args, save_blk->bp); |
1342 | /* | |
1343 | * Move the entries from drop to the appropriate end of save. | |
1344 | */ | |
3c1f9c15 | 1345 | drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval); |
1da177e4 LT |
1346 | if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp)) |
1347 | xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0, | |
a818e5de | 1348 | be16_to_cpu(drop_leaf->hdr.count)); |
1da177e4 LT |
1349 | else |
1350 | xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, | |
a818e5de | 1351 | be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count)); |
3c1f9c15 | 1352 | save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval); |
1da177e4 LT |
1353 | xfs_dir2_leafn_check(args->dp, save_blk->bp); |
1354 | } | |
1355 | ||
1356 | /* | |
1357 | * Top-level node form directory addname routine. | |
1358 | */ | |
1359 | int /* error */ | |
1360 | xfs_dir2_node_addname( | |
1361 | xfs_da_args_t *args) /* operation arguments */ | |
1362 | { | |
1363 | xfs_da_state_blk_t *blk; /* leaf block for insert */ | |
1364 | int error; /* error return value */ | |
1365 | int rval; /* sub-return value */ | |
1366 | xfs_da_state_t *state; /* btree cursor */ | |
1367 | ||
0b1b213f CH |
1368 | trace_xfs_dir2_node_addname(args); |
1369 | ||
1da177e4 LT |
1370 | /* |
1371 | * Allocate and initialize the state (btree cursor). | |
1372 | */ | |
1373 | state = xfs_da_state_alloc(); | |
1374 | state->args = args; | |
1375 | state->mp = args->dp->i_mount; | |
1376 | state->blocksize = state->mp->m_dirblksize; | |
1377 | state->node_ents = state->mp->m_dir_node_ents; | |
1378 | /* | |
1379 | * Look up the name. We're not supposed to find it, but | |
1380 | * this gives us the insertion point. | |
1381 | */ | |
1382 | error = xfs_da_node_lookup_int(state, &rval); | |
1383 | if (error) | |
1384 | rval = error; | |
1385 | if (rval != ENOENT) { | |
1386 | goto done; | |
1387 | } | |
1388 | /* | |
1389 | * Add the data entry to a data block. | |
1390 | * Extravalid is set to a freeblock found by lookup. | |
1391 | */ | |
1392 | rval = xfs_dir2_node_addname_int(args, | |
1393 | state->extravalid ? &state->extrablk : NULL); | |
1394 | if (rval) { | |
1395 | goto done; | |
1396 | } | |
1397 | blk = &state->path.blk[state->path.active - 1]; | |
1398 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); | |
1399 | /* | |
1400 | * Add the new leaf entry. | |
1401 | */ | |
1402 | rval = xfs_dir2_leafn_add(blk->bp, args, blk->index); | |
1403 | if (rval == 0) { | |
1404 | /* | |
1405 | * It worked, fix the hash values up the btree. | |
1406 | */ | |
6a178100 | 1407 | if (!(args->op_flags & XFS_DA_OP_JUSTCHECK)) |
1da177e4 LT |
1408 | xfs_da_fixhashpath(state, &state->path); |
1409 | } else { | |
1410 | /* | |
1411 | * It didn't work, we need to split the leaf block. | |
1412 | */ | |
1413 | if (args->total == 0) { | |
1414 | ASSERT(rval == ENOSPC); | |
1415 | goto done; | |
1416 | } | |
1417 | /* | |
1418 | * Split the leaf block and insert the new entry. | |
1419 | */ | |
1420 | rval = xfs_da_split(state); | |
1421 | } | |
1422 | done: | |
1423 | xfs_da_state_free(state); | |
1424 | return rval; | |
1425 | } | |
1426 | ||
1427 | /* | |
1428 | * Add the data entry for a node-format directory name addition. | |
1429 | * The leaf entry is added in xfs_dir2_leafn_add. | |
1430 | * We may enter with a freespace block that the lookup found. | |
1431 | */ | |
1432 | static int /* error */ | |
1433 | xfs_dir2_node_addname_int( | |
1434 | xfs_da_args_t *args, /* operation arguments */ | |
1435 | xfs_da_state_blk_t *fblk) /* optional freespace block */ | |
1436 | { | |
1437 | xfs_dir2_data_t *data; /* data block structure */ | |
1438 | xfs_dir2_db_t dbno; /* data block number */ | |
1439 | xfs_dabuf_t *dbp; /* data block buffer */ | |
1440 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ | |
1441 | xfs_inode_t *dp; /* incore directory inode */ | |
1442 | xfs_dir2_data_unused_t *dup; /* data unused entry pointer */ | |
1443 | int error; /* error return value */ | |
1444 | xfs_dir2_db_t fbno; /* freespace block number */ | |
1445 | xfs_dabuf_t *fbp; /* freespace buffer */ | |
1446 | int findex; /* freespace entry index */ | |
1447 | xfs_dir2_free_t *free=NULL; /* freespace block structure */ | |
1448 | xfs_dir2_db_t ifbno; /* initial freespace block no */ | |
1449 | xfs_dir2_db_t lastfbno=0; /* highest freespace block no */ | |
1450 | int length; /* length of the new entry */ | |
1451 | int logfree; /* need to log free entry */ | |
1452 | xfs_mount_t *mp; /* filesystem mount point */ | |
1453 | int needlog; /* need to log data header */ | |
1454 | int needscan; /* need to rescan data frees */ | |
3d693c6e | 1455 | __be16 *tagp; /* data entry tag pointer */ |
1da177e4 LT |
1456 | xfs_trans_t *tp; /* transaction pointer */ |
1457 | ||
1458 | dp = args->dp; | |
1459 | mp = dp->i_mount; | |
1460 | tp = args->trans; | |
bbaaf538 | 1461 | length = xfs_dir2_data_entsize(args->namelen); |
1da177e4 LT |
1462 | /* |
1463 | * If we came in with a freespace block that means that lookup | |
1464 | * found an entry with our hash value. This is the freespace | |
1465 | * block for that data entry. | |
1466 | */ | |
1467 | if (fblk) { | |
1468 | fbp = fblk->bp; | |
1469 | /* | |
1470 | * Remember initial freespace block number. | |
1471 | */ | |
1472 | ifbno = fblk->blkno; | |
1473 | free = fbp->data; | |
0ba962ef | 1474 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
1da177e4 LT |
1475 | findex = fblk->index; |
1476 | /* | |
1477 | * This means the free entry showed that the data block had | |
1478 | * space for our entry, so we remembered it. | |
1479 | * Use that data block. | |
1480 | */ | |
1481 | if (findex >= 0) { | |
0ba962ef NS |
1482 | ASSERT(findex < be32_to_cpu(free->hdr.nvalid)); |
1483 | ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF); | |
1484 | ASSERT(be16_to_cpu(free->bests[findex]) >= length); | |
1485 | dbno = be32_to_cpu(free->hdr.firstdb) + findex; | |
1da177e4 LT |
1486 | } |
1487 | /* | |
1488 | * The data block looked at didn't have enough room. | |
1489 | * We'll start at the beginning of the freespace entries. | |
1490 | */ | |
1491 | else { | |
1492 | dbno = -1; | |
1493 | findex = 0; | |
1494 | } | |
1495 | } | |
1496 | /* | |
1497 | * Didn't come in with a freespace block, so don't have a data block. | |
1498 | */ | |
1499 | else { | |
1500 | ifbno = dbno = -1; | |
1501 | fbp = NULL; | |
1502 | findex = 0; | |
1503 | } | |
1504 | /* | |
1505 | * If we don't have a data block yet, we're going to scan the | |
1506 | * freespace blocks looking for one. Figure out what the | |
1507 | * highest freespace block number is. | |
1508 | */ | |
1509 | if (dbno == -1) { | |
1510 | xfs_fileoff_t fo; /* freespace block number */ | |
1511 | ||
1512 | if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) | |
1513 | return error; | |
bbaaf538 | 1514 | lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo); |
1da177e4 LT |
1515 | fbno = ifbno; |
1516 | } | |
1517 | /* | |
1518 | * While we haven't identified a data block, search the freeblock | |
1519 | * data for a good data block. If we find a null freeblock entry, | |
1520 | * indicating a hole in the data blocks, remember that. | |
1521 | */ | |
1522 | while (dbno == -1) { | |
1523 | /* | |
1524 | * If we don't have a freeblock in hand, get the next one. | |
1525 | */ | |
1526 | if (fbp == NULL) { | |
1527 | /* | |
1528 | * Happens the first time through unless lookup gave | |
1529 | * us a freespace block to start with. | |
1530 | */ | |
1531 | if (++fbno == 0) | |
1532 | fbno = XFS_DIR2_FREE_FIRSTDB(mp); | |
1533 | /* | |
1534 | * If it's ifbno we already looked at it. | |
1535 | */ | |
1536 | if (fbno == ifbno) | |
1537 | fbno++; | |
1538 | /* | |
1539 | * If it's off the end we're done. | |
1540 | */ | |
1541 | if (fbno >= lastfbno) | |
1542 | break; | |
1543 | /* | |
1544 | * Read the block. There can be holes in the | |
1545 | * freespace blocks, so this might not succeed. | |
1546 | * This should be really rare, so there's no reason | |
1547 | * to avoid it. | |
1548 | */ | |
1549 | if ((error = xfs_da_read_buf(tp, dp, | |
bbaaf538 | 1550 | xfs_dir2_db_to_da(mp, fbno), -2, &fbp, |
1da177e4 LT |
1551 | XFS_DATA_FORK))) { |
1552 | return error; | |
1553 | } | |
1554 | if (unlikely(fbp == NULL)) { | |
1555 | continue; | |
1556 | } | |
1557 | free = fbp->data; | |
0ba962ef | 1558 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
1da177e4 LT |
1559 | findex = 0; |
1560 | } | |
1561 | /* | |
1562 | * Look at the current free entry. Is it good enough? | |
1563 | */ | |
0ba962ef NS |
1564 | if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF && |
1565 | be16_to_cpu(free->bests[findex]) >= length) | |
1566 | dbno = be32_to_cpu(free->hdr.firstdb) + findex; | |
1da177e4 LT |
1567 | else { |
1568 | /* | |
1569 | * Are we done with the freeblock? | |
1570 | */ | |
0ba962ef | 1571 | if (++findex == be32_to_cpu(free->hdr.nvalid)) { |
1da177e4 LT |
1572 | /* |
1573 | * Drop the block. | |
1574 | */ | |
1575 | xfs_da_brelse(tp, fbp); | |
1576 | fbp = NULL; | |
1577 | if (fblk && fblk->bp) | |
1578 | fblk->bp = NULL; | |
1579 | } | |
1580 | } | |
1581 | } | |
1582 | /* | |
1583 | * If we don't have a data block, we need to allocate one and make | |
1584 | * the freespace entries refer to it. | |
1585 | */ | |
1586 | if (unlikely(dbno == -1)) { | |
1587 | /* | |
1588 | * Not allowed to allocate, return failure. | |
1589 | */ | |
6a178100 BN |
1590 | if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || |
1591 | args->total == 0) { | |
1da177e4 LT |
1592 | /* |
1593 | * Drop the freespace buffer unless it came from our | |
1594 | * caller. | |
1595 | */ | |
1596 | if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL) | |
1597 | xfs_da_buf_done(fbp); | |
1598 | return XFS_ERROR(ENOSPC); | |
1599 | } | |
1600 | /* | |
1601 | * Allocate and initialize the new data block. | |
1602 | */ | |
1603 | if (unlikely((error = xfs_dir2_grow_inode(args, | |
1604 | XFS_DIR2_DATA_SPACE, | |
1605 | &dbno)) || | |
1606 | (error = xfs_dir2_data_init(args, dbno, &dbp)))) { | |
1607 | /* | |
1608 | * Drop the freespace buffer unless it came from our | |
1609 | * caller. | |
1610 | */ | |
1611 | if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL) | |
1612 | xfs_da_buf_done(fbp); | |
1613 | return error; | |
1614 | } | |
1615 | /* | |
1616 | * If (somehow) we have a freespace block, get rid of it. | |
1617 | */ | |
1618 | if (fbp) | |
1619 | xfs_da_brelse(tp, fbp); | |
1620 | if (fblk && fblk->bp) | |
1621 | fblk->bp = NULL; | |
1622 | ||
1623 | /* | |
1624 | * Get the freespace block corresponding to the data block | |
1625 | * that was just allocated. | |
1626 | */ | |
bbaaf538 | 1627 | fbno = xfs_dir2_db_to_fdb(mp, dbno); |
1da177e4 | 1628 | if (unlikely(error = xfs_da_read_buf(tp, dp, |
bbaaf538 | 1629 | xfs_dir2_db_to_da(mp, fbno), -2, &fbp, |
1da177e4 LT |
1630 | XFS_DATA_FORK))) { |
1631 | xfs_da_buf_done(dbp); | |
1632 | return error; | |
1633 | } | |
1634 | /* | |
1635 | * If there wasn't a freespace block, the read will | |
1636 | * return a NULL fbp. Allocate and initialize a new one. | |
1637 | */ | |
1638 | if( fbp == NULL ) { | |
1639 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, | |
1640 | &fbno))) { | |
1641 | return error; | |
1642 | } | |
1643 | ||
bbaaf538 | 1644 | if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) { |
1da177e4 LT |
1645 | cmn_err(CE_ALERT, |
1646 | "xfs_dir2_node_addname_int: dir ino " | |
1647 | "%llu needed freesp block %lld for\n" | |
1648 | " data block %lld, got %lld\n" | |
1649 | " ifbno %llu lastfbno %d\n", | |
1650 | (unsigned long long)dp->i_ino, | |
bbaaf538 | 1651 | (long long)xfs_dir2_db_to_fdb(mp, dbno), |
1da177e4 LT |
1652 | (long long)dbno, (long long)fbno, |
1653 | (unsigned long long)ifbno, lastfbno); | |
1654 | if (fblk) { | |
1655 | cmn_err(CE_ALERT, | |
1656 | " fblk 0x%p blkno %llu " | |
1657 | "index %d magic 0x%x\n", | |
1658 | fblk, | |
1659 | (unsigned long long)fblk->blkno, | |
1660 | fblk->index, | |
1661 | fblk->magic); | |
1662 | } else { | |
1663 | cmn_err(CE_ALERT, | |
1664 | " ... fblk is NULL\n"); | |
1665 | } | |
1666 | XFS_ERROR_REPORT("xfs_dir2_node_addname_int", | |
1667 | XFS_ERRLEVEL_LOW, mp); | |
1668 | return XFS_ERROR(EFSCORRUPTED); | |
1669 | } | |
1670 | ||
1671 | /* | |
1672 | * Get a buffer for the new block. | |
1673 | */ | |
1674 | if ((error = xfs_da_get_buf(tp, dp, | |
bbaaf538 | 1675 | xfs_dir2_db_to_da(mp, fbno), |
1da177e4 LT |
1676 | -1, &fbp, XFS_DATA_FORK))) { |
1677 | return error; | |
1678 | } | |
1679 | ASSERT(fbp != NULL); | |
1680 | ||
1681 | /* | |
1682 | * Initialize the new block to be empty, and remember | |
1683 | * its first slot as our empty slot. | |
1684 | */ | |
1685 | free = fbp->data; | |
0ba962ef NS |
1686 | free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC); |
1687 | free->hdr.firstdb = cpu_to_be32( | |
1da177e4 LT |
1688 | (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) * |
1689 | XFS_DIR2_MAX_FREE_BESTS(mp)); | |
1690 | free->hdr.nvalid = 0; | |
1691 | free->hdr.nused = 0; | |
1692 | } else { | |
1693 | free = fbp->data; | |
0ba962ef | 1694 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
1da177e4 LT |
1695 | } |
1696 | ||
1697 | /* | |
1698 | * Set the freespace block index from the data block number. | |
1699 | */ | |
bbaaf538 | 1700 | findex = xfs_dir2_db_to_fdindex(mp, dbno); |
1da177e4 LT |
1701 | /* |
1702 | * If it's after the end of the current entries in the | |
1703 | * freespace block, extend that table. | |
1704 | */ | |
0ba962ef | 1705 | if (findex >= be32_to_cpu(free->hdr.nvalid)) { |
1da177e4 | 1706 | ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp)); |
0ba962ef | 1707 | free->hdr.nvalid = cpu_to_be32(findex + 1); |
1da177e4 LT |
1708 | /* |
1709 | * Tag new entry so nused will go up. | |
1710 | */ | |
0ba962ef | 1711 | free->bests[findex] = cpu_to_be16(NULLDATAOFF); |
1da177e4 LT |
1712 | } |
1713 | /* | |
1714 | * If this entry was for an empty data block | |
1715 | * (this should always be true) then update the header. | |
1716 | */ | |
0ba962ef | 1717 | if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) { |
413d57c9 | 1718 | be32_add_cpu(&free->hdr.nused, 1); |
1da177e4 LT |
1719 | xfs_dir2_free_log_header(tp, fbp); |
1720 | } | |
1721 | /* | |
1722 | * Update the real value in the table. | |
1723 | * We haven't allocated the data entry yet so this will | |
1724 | * change again. | |
1725 | */ | |
1726 | data = dbp->data; | |
70e73f59 | 1727 | free->bests[findex] = data->hdr.bestfree[0].length; |
1da177e4 LT |
1728 | logfree = 1; |
1729 | } | |
1730 | /* | |
1731 | * We had a data block so we don't have to make a new one. | |
1732 | */ | |
1733 | else { | |
1734 | /* | |
1735 | * If just checking, we succeeded. | |
1736 | */ | |
6a178100 | 1737 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) { |
1da177e4 LT |
1738 | if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL) |
1739 | xfs_da_buf_done(fbp); | |
1740 | return 0; | |
1741 | } | |
1742 | /* | |
1743 | * Read the data block in. | |
1744 | */ | |
1745 | if (unlikely( | |
bbaaf538 | 1746 | error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno), |
1da177e4 LT |
1747 | -1, &dbp, XFS_DATA_FORK))) { |
1748 | if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL) | |
1749 | xfs_da_buf_done(fbp); | |
1750 | return error; | |
1751 | } | |
1752 | data = dbp->data; | |
1753 | logfree = 0; | |
1754 | } | |
70e73f59 | 1755 | ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length); |
1da177e4 LT |
1756 | /* |
1757 | * Point to the existing unused space. | |
1758 | */ | |
1759 | dup = (xfs_dir2_data_unused_t *) | |
70e73f59 | 1760 | ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset)); |
1da177e4 LT |
1761 | needscan = needlog = 0; |
1762 | /* | |
1763 | * Mark the first part of the unused space, inuse for us. | |
1764 | */ | |
1765 | xfs_dir2_data_use_free(tp, dbp, dup, | |
1766 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length, | |
1767 | &needlog, &needscan); | |
1768 | /* | |
1769 | * Fill in the new entry and log it. | |
1770 | */ | |
1771 | dep = (xfs_dir2_data_entry_t *)dup; | |
ff9901c1 | 1772 | dep->inumber = cpu_to_be64(args->inumber); |
1da177e4 LT |
1773 | dep->namelen = args->namelen; |
1774 | memcpy(dep->name, args->name, dep->namelen); | |
bbaaf538 | 1775 | tagp = xfs_dir2_data_entry_tag_p(dep); |
3d693c6e | 1776 | *tagp = cpu_to_be16((char *)dep - (char *)data); |
1da177e4 LT |
1777 | xfs_dir2_data_log_entry(tp, dbp, dep); |
1778 | /* | |
1779 | * Rescan the block for bestfree if needed. | |
1780 | */ | |
1781 | if (needscan) | |
ef497f8a | 1782 | xfs_dir2_data_freescan(mp, data, &needlog); |
1da177e4 LT |
1783 | /* |
1784 | * Log the data block header if needed. | |
1785 | */ | |
1786 | if (needlog) | |
1787 | xfs_dir2_data_log_header(tp, dbp); | |
1788 | /* | |
1789 | * If the freespace entry is now wrong, update it. | |
1790 | */ | |
0ba962ef | 1791 | if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) { |
70e73f59 | 1792 | free->bests[findex] = data->hdr.bestfree[0].length; |
1da177e4 LT |
1793 | logfree = 1; |
1794 | } | |
1795 | /* | |
1796 | * Log the freespace entry if needed. | |
1797 | */ | |
1798 | if (logfree) | |
1799 | xfs_dir2_free_log_bests(tp, fbp, findex, findex); | |
1800 | /* | |
1801 | * If the caller didn't hand us the freespace block, drop it. | |
1802 | */ | |
1803 | if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL) | |
1804 | xfs_da_buf_done(fbp); | |
1805 | /* | |
1806 | * Return the data block and offset in args, then drop the data block. | |
1807 | */ | |
1808 | args->blkno = (xfs_dablk_t)dbno; | |
3d693c6e | 1809 | args->index = be16_to_cpu(*tagp); |
1da177e4 LT |
1810 | xfs_da_buf_done(dbp); |
1811 | return 0; | |
1812 | } | |
1813 | ||
1814 | /* | |
1815 | * Lookup an entry in a node-format directory. | |
1816 | * All the real work happens in xfs_da_node_lookup_int. | |
1817 | * The only real output is the inode number of the entry. | |
1818 | */ | |
1819 | int /* error */ | |
1820 | xfs_dir2_node_lookup( | |
1821 | xfs_da_args_t *args) /* operation arguments */ | |
1822 | { | |
1823 | int error; /* error return value */ | |
1824 | int i; /* btree level */ | |
1825 | int rval; /* operation return value */ | |
1826 | xfs_da_state_t *state; /* btree cursor */ | |
1827 | ||
0b1b213f CH |
1828 | trace_xfs_dir2_node_lookup(args); |
1829 | ||
1da177e4 LT |
1830 | /* |
1831 | * Allocate and initialize the btree cursor. | |
1832 | */ | |
1833 | state = xfs_da_state_alloc(); | |
1834 | state->args = args; | |
1835 | state->mp = args->dp->i_mount; | |
1836 | state->blocksize = state->mp->m_dirblksize; | |
1837 | state->node_ents = state->mp->m_dir_node_ents; | |
1838 | /* | |
1839 | * Fill in the path to the entry in the cursor. | |
1840 | */ | |
1841 | error = xfs_da_node_lookup_int(state, &rval); | |
1842 | if (error) | |
1843 | rval = error; | |
384f3ced BN |
1844 | else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) { |
1845 | /* If a CI match, dup the actual name and return EEXIST */ | |
1846 | xfs_dir2_data_entry_t *dep; | |
1847 | ||
1848 | dep = (xfs_dir2_data_entry_t *)((char *)state->extrablk.bp-> | |
1849 | data + state->extrablk.index); | |
1850 | rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen); | |
1851 | } | |
1da177e4 LT |
1852 | /* |
1853 | * Release the btree blocks and leaf block. | |
1854 | */ | |
1855 | for (i = 0; i < state->path.active; i++) { | |
1856 | xfs_da_brelse(args->trans, state->path.blk[i].bp); | |
1857 | state->path.blk[i].bp = NULL; | |
1858 | } | |
1859 | /* | |
1860 | * Release the data block if we have it. | |
1861 | */ | |
1862 | if (state->extravalid && state->extrablk.bp) { | |
1863 | xfs_da_brelse(args->trans, state->extrablk.bp); | |
1864 | state->extrablk.bp = NULL; | |
1865 | } | |
1866 | xfs_da_state_free(state); | |
1867 | return rval; | |
1868 | } | |
1869 | ||
1870 | /* | |
1871 | * Remove an entry from a node-format directory. | |
1872 | */ | |
1873 | int /* error */ | |
1874 | xfs_dir2_node_removename( | |
1875 | xfs_da_args_t *args) /* operation arguments */ | |
1876 | { | |
1877 | xfs_da_state_blk_t *blk; /* leaf block */ | |
1878 | int error; /* error return value */ | |
1879 | int rval; /* operation return value */ | |
1880 | xfs_da_state_t *state; /* btree cursor */ | |
1881 | ||
0b1b213f CH |
1882 | trace_xfs_dir2_node_removename(args); |
1883 | ||
1da177e4 LT |
1884 | /* |
1885 | * Allocate and initialize the btree cursor. | |
1886 | */ | |
1887 | state = xfs_da_state_alloc(); | |
1888 | state->args = args; | |
1889 | state->mp = args->dp->i_mount; | |
1890 | state->blocksize = state->mp->m_dirblksize; | |
1891 | state->node_ents = state->mp->m_dir_node_ents; | |
1892 | /* | |
1893 | * Look up the entry we're deleting, set up the cursor. | |
1894 | */ | |
1895 | error = xfs_da_node_lookup_int(state, &rval); | |
5163f95a | 1896 | if (error) |
1da177e4 | 1897 | rval = error; |
1da177e4 LT |
1898 | /* |
1899 | * Didn't find it, upper layer screwed up. | |
1900 | */ | |
1901 | if (rval != EEXIST) { | |
1902 | xfs_da_state_free(state); | |
1903 | return rval; | |
1904 | } | |
1905 | blk = &state->path.blk[state->path.active - 1]; | |
1906 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); | |
1907 | ASSERT(state->extravalid); | |
1908 | /* | |
1909 | * Remove the leaf and data entries. | |
1910 | * Extrablk refers to the data block. | |
1911 | */ | |
1912 | error = xfs_dir2_leafn_remove(args, blk->bp, blk->index, | |
1913 | &state->extrablk, &rval); | |
5163f95a | 1914 | if (error) |
1da177e4 | 1915 | return error; |
1da177e4 LT |
1916 | /* |
1917 | * Fix the hash values up the btree. | |
1918 | */ | |
1919 | xfs_da_fixhashpath(state, &state->path); | |
1920 | /* | |
1921 | * If we need to join leaf blocks, do it. | |
1922 | */ | |
1923 | if (rval && state->path.active > 1) | |
1924 | error = xfs_da_join(state); | |
1925 | /* | |
1926 | * If no errors so far, try conversion to leaf format. | |
1927 | */ | |
1928 | if (!error) | |
1929 | error = xfs_dir2_node_to_leaf(state); | |
1930 | xfs_da_state_free(state); | |
1931 | return error; | |
1932 | } | |
1933 | ||
1934 | /* | |
1935 | * Replace an entry's inode number in a node-format directory. | |
1936 | */ | |
1937 | int /* error */ | |
1938 | xfs_dir2_node_replace( | |
1939 | xfs_da_args_t *args) /* operation arguments */ | |
1940 | { | |
1941 | xfs_da_state_blk_t *blk; /* leaf block */ | |
1942 | xfs_dir2_data_t *data; /* data block structure */ | |
1943 | xfs_dir2_data_entry_t *dep; /* data entry changed */ | |
1944 | int error; /* error return value */ | |
1945 | int i; /* btree level */ | |
1946 | xfs_ino_t inum; /* new inode number */ | |
1947 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1948 | xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */ | |
1949 | int rval; /* internal return value */ | |
1950 | xfs_da_state_t *state; /* btree cursor */ | |
1951 | ||
0b1b213f CH |
1952 | trace_xfs_dir2_node_replace(args); |
1953 | ||
1da177e4 LT |
1954 | /* |
1955 | * Allocate and initialize the btree cursor. | |
1956 | */ | |
1957 | state = xfs_da_state_alloc(); | |
1958 | state->args = args; | |
1959 | state->mp = args->dp->i_mount; | |
1960 | state->blocksize = state->mp->m_dirblksize; | |
1961 | state->node_ents = state->mp->m_dir_node_ents; | |
1962 | inum = args->inumber; | |
1963 | /* | |
1964 | * Lookup the entry to change in the btree. | |
1965 | */ | |
1966 | error = xfs_da_node_lookup_int(state, &rval); | |
1967 | if (error) { | |
1968 | rval = error; | |
1969 | } | |
1970 | /* | |
1971 | * It should be found, since the vnodeops layer has looked it up | |
1972 | * and locked it. But paranoia is good. | |
1973 | */ | |
1974 | if (rval == EEXIST) { | |
1975 | /* | |
1976 | * Find the leaf entry. | |
1977 | */ | |
1978 | blk = &state->path.blk[state->path.active - 1]; | |
1979 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); | |
1980 | leaf = blk->bp->data; | |
1981 | lep = &leaf->ents[blk->index]; | |
1982 | ASSERT(state->extravalid); | |
1983 | /* | |
1984 | * Point to the data entry. | |
1985 | */ | |
1986 | data = state->extrablk.bp->data; | |
70e73f59 | 1987 | ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC); |
1da177e4 LT |
1988 | dep = (xfs_dir2_data_entry_t *) |
1989 | ((char *)data + | |
bbaaf538 | 1990 | xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address))); |
ff9901c1 | 1991 | ASSERT(inum != be64_to_cpu(dep->inumber)); |
1da177e4 LT |
1992 | /* |
1993 | * Fill in the new inode number and log the entry. | |
1994 | */ | |
ff9901c1 | 1995 | dep->inumber = cpu_to_be64(inum); |
1da177e4 LT |
1996 | xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep); |
1997 | rval = 0; | |
1998 | } | |
1999 | /* | |
2000 | * Didn't find it, and we're holding a data block. Drop it. | |
2001 | */ | |
2002 | else if (state->extravalid) { | |
2003 | xfs_da_brelse(args->trans, state->extrablk.bp); | |
2004 | state->extrablk.bp = NULL; | |
2005 | } | |
2006 | /* | |
2007 | * Release all the buffers in the cursor. | |
2008 | */ | |
2009 | for (i = 0; i < state->path.active; i++) { | |
2010 | xfs_da_brelse(args->trans, state->path.blk[i].bp); | |
2011 | state->path.blk[i].bp = NULL; | |
2012 | } | |
2013 | xfs_da_state_free(state); | |
2014 | return rval; | |
2015 | } | |
2016 | ||
2017 | /* | |
2018 | * Trim off a trailing empty freespace block. | |
2019 | * Return (in rvalp) 1 if we did it, 0 if not. | |
2020 | */ | |
2021 | int /* error */ | |
2022 | xfs_dir2_node_trim_free( | |
2023 | xfs_da_args_t *args, /* operation arguments */ | |
2024 | xfs_fileoff_t fo, /* free block number */ | |
2025 | int *rvalp) /* out: did something */ | |
2026 | { | |
2027 | xfs_dabuf_t *bp; /* freespace buffer */ | |
2028 | xfs_inode_t *dp; /* incore directory inode */ | |
2029 | int error; /* error return code */ | |
2030 | xfs_dir2_free_t *free; /* freespace structure */ | |
2031 | xfs_mount_t *mp; /* filesystem mount point */ | |
2032 | xfs_trans_t *tp; /* transaction pointer */ | |
2033 | ||
2034 | dp = args->dp; | |
2035 | mp = dp->i_mount; | |
2036 | tp = args->trans; | |
2037 | /* | |
2038 | * Read the freespace block. | |
2039 | */ | |
2040 | if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp, | |
2041 | XFS_DATA_FORK))) { | |
2042 | return error; | |
2043 | } | |
2044 | ||
2045 | /* | |
2046 | * There can be holes in freespace. If fo is a hole, there's | |
2047 | * nothing to do. | |
2048 | */ | |
2049 | if (bp == NULL) { | |
2050 | return 0; | |
2051 | } | |
2052 | free = bp->data; | |
0ba962ef | 2053 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
1da177e4 LT |
2054 | /* |
2055 | * If there are used entries, there's nothing to do. | |
2056 | */ | |
0ba962ef | 2057 | if (be32_to_cpu(free->hdr.nused) > 0) { |
1da177e4 LT |
2058 | xfs_da_brelse(tp, bp); |
2059 | *rvalp = 0; | |
2060 | return 0; | |
2061 | } | |
2062 | /* | |
2063 | * Blow the block away. | |
2064 | */ | |
2065 | if ((error = | |
bbaaf538 | 2066 | xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo), |
1da177e4 LT |
2067 | bp))) { |
2068 | /* | |
2069 | * Can't fail with ENOSPC since that only happens with no | |
2070 | * space reservation, when breaking up an extent into two | |
2071 | * pieces. This is the last block of an extent. | |
2072 | */ | |
2073 | ASSERT(error != ENOSPC); | |
2074 | xfs_da_brelse(tp, bp); | |
2075 | return error; | |
2076 | } | |
2077 | /* | |
2078 | * Return that we succeeded. | |
2079 | */ | |
2080 | *rvalp = 1; | |
2081 | return 0; | |
2082 | } |