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