]>
Commit | Line | Data |
---|---|---|
37b3b4d6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2bd0ea18 | 2 | /* |
5e656dbb | 3 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
693fc8f6 | 4 | * Copyright (c) 2013 Red Hat, Inc. |
da23017d | 5 | * All Rights Reserved. |
2bd0ea18 | 6 | */ |
9c799827 | 7 | #include "libxfs_priv.h" |
b626fb59 | 8 | #include "xfs_fs.h" |
27846843 | 9 | #include "xfs_shared.h" |
b626fb59 DC |
10 | #include "xfs_format.h" |
11 | #include "xfs_log_format.h" | |
12 | #include "xfs_trans_resv.h" | |
13 | #include "xfs_mount.h" | |
b626fb59 DC |
14 | #include "xfs_inode.h" |
15 | #include "xfs_trans.h" | |
16 | #include "xfs_bmap.h" | |
17 | #include "xfs_dir2.h" | |
18 | #include "xfs_dir2_priv.h" | |
19 | #include "xfs_trace.h" | |
5e656dbb | 20 | |
2bd0ea18 | 21 | /* |
5e656dbb | 22 | * Local function prototypes. |
2bd0ea18 | 23 | */ |
a2ceac1f DC |
24 | static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp, |
25 | int first, int last); | |
26 | static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp); | |
27 | static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp, | |
5e656dbb BN |
28 | int *entno); |
29 | static int xfs_dir2_block_sort(const void *a, const void *b); | |
2bd0ea18 | 30 | |
5e656dbb BN |
31 | static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot; |
32 | ||
33 | /* | |
34 | * One-time startup routine called from xfs_init(). | |
35 | */ | |
36 | void | |
37 | xfs_dir_startup(void) | |
38 | { | |
56b2de80 DC |
39 | xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1); |
40 | xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2); | |
5e656dbb | 41 | } |
2bd0ea18 | 42 | |
bc01119d | 43 | static xfs_failaddr_t |
693fc8f6 | 44 | xfs_dir3_block_verify( |
a2ceac1f DC |
45 | struct xfs_buf *bp) |
46 | { | |
7861ef77 | 47 | struct xfs_mount *mp = bp->b_mount; |
693fc8f6 DC |
48 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
49 | ||
68dbe77f BF |
50 | if (!xfs_verify_magic(bp, hdr3->magic)) |
51 | return __this_address; | |
52 | ||
94541a16 | 53 | if (xfs_has_crc(mp)) { |
9c4e12fb | 54 | if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid)) |
bc01119d | 55 | return __this_address; |
f1208396 | 56 | if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp)) |
bc01119d | 57 | return __this_address; |
a65d8d29 | 58 | if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn))) |
bc01119d | 59 | return __this_address; |
a2ceac1f | 60 | } |
a7e32f0d | 61 | return __xfs_dir3_data_check(NULL, bp); |
a2ceac1f DC |
62 | } |
63 | ||
64 | static void | |
693fc8f6 | 65 | xfs_dir3_block_read_verify( |
a2ceac1f DC |
66 | struct xfs_buf *bp) |
67 | { | |
7861ef77 | 68 | struct xfs_mount *mp = bp->b_mount; |
1e697959 | 69 | xfs_failaddr_t fa; |
693fc8f6 | 70 | |
b16a427a | 71 | if (xfs_has_crc(mp) && |
45922933 | 72 | !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF)) |
1e697959 DW |
73 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
74 | else { | |
75 | fa = xfs_dir3_block_verify(bp); | |
76 | if (fa) | |
77 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
78 | } | |
a2ceac1f DC |
79 | } |
80 | ||
81 | static void | |
693fc8f6 | 82 | xfs_dir3_block_write_verify( |
a2ceac1f DC |
83 | struct xfs_buf *bp) |
84 | { | |
7861ef77 | 85 | struct xfs_mount *mp = bp->b_mount; |
37d086ca | 86 | struct xfs_buf_log_item *bip = bp->b_log_item; |
693fc8f6 | 87 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
1e697959 | 88 | xfs_failaddr_t fa; |
693fc8f6 | 89 | |
1e697959 DW |
90 | fa = xfs_dir3_block_verify(bp); |
91 | if (fa) { | |
92 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
693fc8f6 DC |
93 | return; |
94 | } | |
95 | ||
b16a427a | 96 | if (!xfs_has_crc(mp)) |
693fc8f6 DC |
97 | return; |
98 | ||
99 | if (bip) | |
100 | hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
101 | ||
43b5aeed | 102 | xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF); |
a2ceac1f DC |
103 | } |
104 | ||
693fc8f6 | 105 | const struct xfs_buf_ops xfs_dir3_block_buf_ops = { |
a3fac935 | 106 | .name = "xfs_dir3_block", |
68dbe77f BF |
107 | .magic = { cpu_to_be32(XFS_DIR2_BLOCK_MAGIC), |
108 | cpu_to_be32(XFS_DIR3_BLOCK_MAGIC) }, | |
693fc8f6 DC |
109 | .verify_read = xfs_dir3_block_read_verify, |
110 | .verify_write = xfs_dir3_block_write_verify, | |
95d9582b | 111 | .verify_struct = xfs_dir3_block_verify, |
a2ceac1f DC |
112 | }; |
113 | ||
f42e1ded DW |
114 | static xfs_failaddr_t |
115 | xfs_dir3_block_header_check( | |
116 | struct xfs_inode *dp, | |
117 | struct xfs_buf *bp) | |
118 | { | |
119 | struct xfs_mount *mp = dp->i_mount; | |
120 | ||
94541a16 | 121 | if (xfs_has_crc(mp)) { |
f42e1ded DW |
122 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
123 | ||
124 | if (be64_to_cpu(hdr3->owner) != dp->i_ino) | |
125 | return __this_address; | |
126 | } | |
127 | ||
128 | return NULL; | |
129 | } | |
130 | ||
494434d7 | 131 | int |
693fc8f6 | 132 | xfs_dir3_block_read( |
a2ceac1f DC |
133 | struct xfs_trans *tp, |
134 | struct xfs_inode *dp, | |
135 | struct xfs_buf **bpp) | |
136 | { | |
137 | struct xfs_mount *mp = dp->i_mount; | |
f42e1ded | 138 | xfs_failaddr_t fa; |
8b4dc4a9 | 139 | int err; |
a2ceac1f | 140 | |
5f356ae6 | 141 | err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, 0, bpp, |
693fc8f6 | 142 | XFS_DATA_FORK, &xfs_dir3_block_buf_ops); |
f42e1ded DW |
143 | if (err || !*bpp) |
144 | return err; | |
145 | ||
146 | /* Check things that we can't do in the verifier. */ | |
147 | fa = xfs_dir3_block_header_check(dp, *bpp); | |
148 | if (fa) { | |
149 | __xfs_buf_mark_corrupt(*bpp, fa); | |
150 | xfs_trans_brelse(tp, *bpp); | |
151 | *bpp = NULL; | |
152 | return -EFSCORRUPTED; | |
153 | } | |
154 | ||
155 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF); | |
8b4dc4a9 | 156 | return err; |
693fc8f6 DC |
157 | } |
158 | ||
159 | static void | |
160 | xfs_dir3_block_init( | |
161 | struct xfs_mount *mp, | |
8b4dc4a9 | 162 | struct xfs_trans *tp, |
693fc8f6 DC |
163 | struct xfs_buf *bp, |
164 | struct xfs_inode *dp) | |
165 | { | |
166 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; | |
167 | ||
168 | bp->b_ops = &xfs_dir3_block_buf_ops; | |
bdc16ee5 | 169 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF); |
693fc8f6 | 170 | |
b16a427a | 171 | if (xfs_has_crc(mp)) { |
693fc8f6 DC |
172 | memset(hdr3, 0, sizeof(*hdr3)); |
173 | hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC); | |
f1208396 | 174 | hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp)); |
693fc8f6 | 175 | hdr3->owner = cpu_to_be64(dp->i_ino); |
9c4e12fb | 176 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid); |
693fc8f6 DC |
177 | return; |
178 | ||
179 | } | |
180 | hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC); | |
a2ceac1f DC |
181 | } |
182 | ||
183 | static void | |
184 | xfs_dir2_block_need_space( | |
ff105f75 | 185 | struct xfs_inode *dp, |
a2ceac1f DC |
186 | struct xfs_dir2_data_hdr *hdr, |
187 | struct xfs_dir2_block_tail *btp, | |
188 | struct xfs_dir2_leaf_entry *blp, | |
189 | __be16 **tagpp, | |
190 | struct xfs_dir2_data_unused **dupp, | |
191 | struct xfs_dir2_data_unused **enddupp, | |
192 | int *compact, | |
193 | int len) | |
194 | { | |
195 | struct xfs_dir2_data_free *bf; | |
196 | __be16 *tagp = NULL; | |
197 | struct xfs_dir2_data_unused *dup = NULL; | |
198 | struct xfs_dir2_data_unused *enddup = NULL; | |
199 | ||
200 | *compact = 0; | |
04f6f354 | 201 | bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); |
a2ceac1f DC |
202 | |
203 | /* | |
204 | * If there are stale entries we'll use one for the leaf. | |
205 | */ | |
206 | if (btp->stale) { | |
207 | if (be16_to_cpu(bf[0].length) >= len) { | |
208 | /* | |
209 | * The biggest entry enough to avoid compaction. | |
210 | */ | |
211 | dup = (xfs_dir2_data_unused_t *) | |
212 | ((char *)hdr + be16_to_cpu(bf[0].offset)); | |
213 | goto out; | |
214 | } | |
215 | ||
216 | /* | |
217 | * Will need to compact to make this work. | |
218 | * Tag just before the first leaf entry. | |
219 | */ | |
220 | *compact = 1; | |
221 | tagp = (__be16 *)blp - 1; | |
222 | ||
223 | /* Data object just before the first leaf entry. */ | |
224 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | |
225 | ||
226 | /* | |
227 | * If it's not free then the data will go where the | |
228 | * leaf data starts now, if it works at all. | |
229 | */ | |
230 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { | |
231 | if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) * | |
232 | (uint)sizeof(*blp) < len) | |
233 | dup = NULL; | |
234 | } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len) | |
235 | dup = NULL; | |
236 | else | |
237 | dup = (xfs_dir2_data_unused_t *)blp; | |
238 | goto out; | |
239 | } | |
240 | ||
241 | /* | |
242 | * no stale entries, so just use free space. | |
243 | * Tag just before the first leaf entry. | |
244 | */ | |
245 | tagp = (__be16 *)blp - 1; | |
246 | ||
247 | /* Data object just before the first leaf entry. */ | |
248 | enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | |
249 | ||
250 | /* | |
251 | * If it's not free then can't do this add without cleaning up: | |
252 | * the space before the first leaf entry needs to be free so it | |
253 | * can be expanded to hold the pointer to the new entry. | |
254 | */ | |
255 | if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) { | |
256 | /* | |
257 | * Check out the biggest freespace and see if it's the same one. | |
258 | */ | |
259 | dup = (xfs_dir2_data_unused_t *) | |
260 | ((char *)hdr + be16_to_cpu(bf[0].offset)); | |
261 | if (dup != enddup) { | |
262 | /* | |
263 | * Not the same free entry, just check its length. | |
264 | */ | |
265 | if (be16_to_cpu(dup->length) < len) | |
266 | dup = NULL; | |
267 | goto out; | |
268 | } | |
269 | ||
270 | /* | |
271 | * It is the biggest freespace, can it hold the leaf too? | |
272 | */ | |
273 | if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) { | |
274 | /* | |
275 | * Yes, use the second-largest entry instead if it works. | |
276 | */ | |
277 | if (be16_to_cpu(bf[1].length) >= len) | |
278 | dup = (xfs_dir2_data_unused_t *) | |
279 | ((char *)hdr + be16_to_cpu(bf[1].offset)); | |
280 | else | |
281 | dup = NULL; | |
282 | } | |
283 | } | |
284 | out: | |
285 | *tagpp = tagp; | |
286 | *dupp = dup; | |
287 | *enddupp = enddup; | |
288 | } | |
289 | ||
290 | /* | |
291 | * compact the leaf entries. | |
292 | * Leave the highest-numbered stale entry stale. | |
293 | * XXX should be the one closest to mid but mid is not yet computed. | |
294 | */ | |
295 | static void | |
296 | xfs_dir2_block_compact( | |
ff105f75 | 297 | struct xfs_da_args *args, |
a2ceac1f DC |
298 | struct xfs_buf *bp, |
299 | struct xfs_dir2_data_hdr *hdr, | |
300 | struct xfs_dir2_block_tail *btp, | |
301 | struct xfs_dir2_leaf_entry *blp, | |
302 | int *needlog, | |
303 | int *lfloghigh, | |
304 | int *lfloglow) | |
305 | { | |
306 | int fromidx; /* source leaf index */ | |
307 | int toidx; /* target leaf index */ | |
308 | int needscan = 0; | |
309 | int highstale; /* high stale index */ | |
310 | ||
311 | fromidx = toidx = be32_to_cpu(btp->count) - 1; | |
312 | highstale = *lfloghigh = -1; | |
313 | for (; fromidx >= 0; fromidx--) { | |
314 | if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) { | |
315 | if (highstale == -1) | |
316 | highstale = toidx; | |
317 | else { | |
318 | if (*lfloghigh == -1) | |
319 | *lfloghigh = toidx; | |
320 | continue; | |
321 | } | |
322 | } | |
323 | if (fromidx < toidx) | |
324 | blp[toidx] = blp[fromidx]; | |
325 | toidx--; | |
326 | } | |
327 | *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1); | |
328 | *lfloghigh -= be32_to_cpu(btp->stale) - 1; | |
329 | be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1)); | |
ff105f75 | 330 | xfs_dir2_data_make_free(args, bp, |
a2ceac1f DC |
331 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr), |
332 | (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)), | |
333 | needlog, &needscan); | |
a2ceac1f DC |
334 | btp->stale = cpu_to_be32(1); |
335 | /* | |
336 | * If we now need to rebuild the bestfree map, do so. | |
337 | * This needs to happen before the next call to use_free. | |
338 | */ | |
339 | if (needscan) | |
d85595d0 | 340 | xfs_dir2_data_freescan(args->dp->i_mount, hdr, needlog); |
a2ceac1f DC |
341 | } |
342 | ||
2bd0ea18 NS |
343 | /* |
344 | * Add an entry to a block directory. | |
345 | */ | |
346 | int /* error */ | |
347 | xfs_dir2_block_addname( | |
348 | xfs_da_args_t *args) /* directory op arguments */ | |
349 | { | |
a2ceac1f | 350 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 351 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 352 | struct xfs_buf *bp; /* buffer for block */ |
2bd0ea18 NS |
353 | xfs_dir2_block_tail_t *btp; /* block tail */ |
354 | int compact; /* need to compact leaf ents */ | |
355 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
356 | xfs_inode_t *dp; /* directory inode */ | |
357 | xfs_dir2_data_unused_t *dup; /* block unused entry */ | |
358 | int error; /* error return value */ | |
0e266570 | 359 | xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */ |
2bd0ea18 NS |
360 | xfs_dahash_t hash; /* hash value of found entry */ |
361 | int high; /* high index for binary srch */ | |
362 | int highstale; /* high stale index */ | |
0e266570 NS |
363 | int lfloghigh=0; /* last final leaf to log */ |
364 | int lfloglow=0; /* first final leaf to log */ | |
2bd0ea18 NS |
365 | int len; /* length of the new entry */ |
366 | int low; /* low index for binary srch */ | |
367 | int lowstale; /* low stale index */ | |
0e266570 | 368 | int mid=0; /* midpoint for binary srch */ |
2bd0ea18 NS |
369 | int needlog; /* need to log header */ |
370 | int needscan; /* need to rescan freespace */ | |
5e656dbb | 371 | __be16 *tagp; /* pointer to tag value */ |
2bd0ea18 NS |
372 | xfs_trans_t *tp; /* transaction structure */ |
373 | ||
56b2de80 DC |
374 | trace_xfs_dir2_block_addname(args); |
375 | ||
2bd0ea18 NS |
376 | dp = args->dp; |
377 | tp = args->trans; | |
a2ceac1f DC |
378 | |
379 | /* Read the (one and only) directory block into bp. */ | |
693fc8f6 | 380 | error = xfs_dir3_block_read(tp, dp, &bp); |
a2ceac1f | 381 | if (error) |
2bd0ea18 | 382 | return error; |
a2ceac1f | 383 | |
271a654f | 384 | len = xfs_dir2_data_entsize(dp->i_mount, args->namelen); |
a2ceac1f | 385 | |
2bd0ea18 NS |
386 | /* |
387 | * Set up pointers to parts of the block. | |
388 | */ | |
a2ceac1f | 389 | hdr = bp->b_addr; |
ff105f75 | 390 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 391 | blp = xfs_dir2_block_leaf_p(btp); |
a2ceac1f | 392 | |
2bd0ea18 | 393 | /* |
a2ceac1f DC |
394 | * Find out if we can reuse stale entries or whether we need extra |
395 | * space for entry and new leaf. | |
2bd0ea18 | 396 | */ |
ff105f75 | 397 | xfs_dir2_block_need_space(dp, hdr, btp, blp, &tagp, &dup, |
a2ceac1f DC |
398 | &enddup, &compact, len); |
399 | ||
2bd0ea18 | 400 | /* |
a2ceac1f | 401 | * Done everything we need for a space check now. |
2bd0ea18 | 402 | */ |
a2ceac1f DC |
403 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) { |
404 | xfs_trans_brelse(tp, bp); | |
405 | if (!dup) | |
12b53197 | 406 | return -ENOSPC; |
a2ceac1f | 407 | return 0; |
2bd0ea18 | 408 | } |
a2ceac1f | 409 | |
2bd0ea18 NS |
410 | /* |
411 | * If we don't have space for the new entry & leaf ... | |
412 | */ | |
413 | if (!dup) { | |
a2ceac1f DC |
414 | /* Don't have a space reservation: return no-space. */ |
415 | if (args->total == 0) | |
12b53197 | 416 | return -ENOSPC; |
2bd0ea18 NS |
417 | /* |
418 | * Convert to the next larger format. | |
419 | * Then add the new entry in that format. | |
420 | */ | |
421 | error = xfs_dir2_block_to_leaf(args, bp); | |
2bd0ea18 NS |
422 | if (error) |
423 | return error; | |
424 | return xfs_dir2_leaf_addname(args); | |
425 | } | |
a2ceac1f | 426 | |
2bd0ea18 | 427 | needlog = needscan = 0; |
a2ceac1f | 428 | |
2bd0ea18 NS |
429 | /* |
430 | * If need to compact the leaf entries, do it now. | |
2bd0ea18 | 431 | */ |
49f693fa | 432 | if (compact) { |
ff105f75 | 433 | xfs_dir2_block_compact(args, bp, hdr, btp, blp, &needlog, |
a2ceac1f | 434 | &lfloghigh, &lfloglow); |
49f693fa DC |
435 | /* recalculate blp post-compaction */ |
436 | blp = xfs_dir2_block_leaf_p(btp); | |
437 | } else if (btp->stale) { | |
2bd0ea18 | 438 | /* |
a2ceac1f DC |
439 | * Set leaf logging boundaries to impossible state. |
440 | * For the no-stale case they're set explicitly. | |
2bd0ea18 | 441 | */ |
5e656dbb | 442 | lfloglow = be32_to_cpu(btp->count); |
2bd0ea18 NS |
443 | lfloghigh = -1; |
444 | } | |
a2ceac1f | 445 | |
2bd0ea18 NS |
446 | /* |
447 | * Find the slot that's first lower than our hash value, -1 if none. | |
448 | */ | |
5e656dbb | 449 | for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) { |
2bd0ea18 | 450 | mid = (low + high) >> 1; |
5e656dbb | 451 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
2bd0ea18 NS |
452 | break; |
453 | if (hash < args->hashval) | |
454 | low = mid + 1; | |
455 | else | |
456 | high = mid - 1; | |
457 | } | |
5e656dbb | 458 | while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) { |
2bd0ea18 NS |
459 | mid--; |
460 | } | |
461 | /* | |
462 | * No stale entries, will use enddup space to hold new leaf. | |
463 | */ | |
46eca962 | 464 | if (!btp->stale) { |
15348e04 DW |
465 | xfs_dir2_data_aoff_t aoff; |
466 | ||
2bd0ea18 NS |
467 | /* |
468 | * Mark the space needed for the new leaf entry, now in use. | |
469 | */ | |
15348e04 DW |
470 | aoff = (xfs_dir2_data_aoff_t)((char *)enddup - (char *)hdr + |
471 | be16_to_cpu(enddup->length) - sizeof(*blp)); | |
472 | error = xfs_dir2_data_use_free(args, bp, enddup, aoff, | |
473 | (xfs_dir2_data_aoff_t)sizeof(*blp), &needlog, | |
474 | &needscan); | |
475 | if (error) | |
476 | return error; | |
477 | ||
2bd0ea18 NS |
478 | /* |
479 | * Update the tail (entry count). | |
480 | */ | |
5e656dbb | 481 | be32_add_cpu(&btp->count, 1); |
2bd0ea18 NS |
482 | /* |
483 | * If we now need to rebuild the bestfree map, do so. | |
484 | * This needs to happen before the next call to use_free. | |
485 | */ | |
486 | if (needscan) { | |
d85595d0 | 487 | xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog); |
2bd0ea18 NS |
488 | needscan = 0; |
489 | } | |
490 | /* | |
491 | * Adjust pointer to the first leaf entry, we're about to move | |
492 | * the table up one to open up space for the new leaf entry. | |
493 | * Then adjust our index to match. | |
494 | */ | |
495 | blp--; | |
496 | mid++; | |
497 | if (mid) | |
32181a02 | 498 | memmove(blp, &blp[1], mid * sizeof(*blp)); |
2bd0ea18 NS |
499 | lfloglow = 0; |
500 | lfloghigh = mid; | |
501 | } | |
502 | /* | |
503 | * Use a stale leaf for our new entry. | |
504 | */ | |
505 | else { | |
506 | for (lowstale = mid; | |
507 | lowstale >= 0 && | |
a2ceac1f DC |
508 | blp[lowstale].address != |
509 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR); | |
2bd0ea18 NS |
510 | lowstale--) |
511 | continue; | |
512 | for (highstale = mid + 1; | |
5e656dbb | 513 | highstale < be32_to_cpu(btp->count) && |
a2ceac1f DC |
514 | blp[highstale].address != |
515 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR) && | |
2bd0ea18 NS |
516 | (lowstale < 0 || mid - lowstale > highstale - mid); |
517 | highstale++) | |
518 | continue; | |
519 | /* | |
520 | * Move entries toward the low-numbered stale entry. | |
521 | */ | |
522 | if (lowstale >= 0 && | |
5e656dbb | 523 | (highstale == be32_to_cpu(btp->count) || |
2bd0ea18 NS |
524 | mid - lowstale <= highstale - mid)) { |
525 | if (mid - lowstale) | |
32181a02 | 526 | memmove(&blp[lowstale], &blp[lowstale + 1], |
2bd0ea18 | 527 | (mid - lowstale) * sizeof(*blp)); |
d7ff12b8 DC |
528 | lfloglow = min(lowstale, lfloglow); |
529 | lfloghigh = max(mid, lfloghigh); | |
2bd0ea18 NS |
530 | } |
531 | /* | |
532 | * Move entries toward the high-numbered stale entry. | |
533 | */ | |
534 | else { | |
5e656dbb | 535 | ASSERT(highstale < be32_to_cpu(btp->count)); |
2bd0ea18 NS |
536 | mid++; |
537 | if (highstale - mid) | |
32181a02 | 538 | memmove(&blp[mid + 1], &blp[mid], |
2bd0ea18 | 539 | (highstale - mid) * sizeof(*blp)); |
d7ff12b8 DC |
540 | lfloglow = min(mid, lfloglow); |
541 | lfloghigh = max(highstale, lfloghigh); | |
2bd0ea18 | 542 | } |
5e656dbb | 543 | be32_add_cpu(&btp->stale, -1); |
2bd0ea18 NS |
544 | } |
545 | /* | |
546 | * Point to the new data entry. | |
547 | */ | |
548 | dep = (xfs_dir2_data_entry_t *)dup; | |
549 | /* | |
550 | * Fill in the leaf entry. | |
551 | */ | |
5e656dbb | 552 | blp[mid].hashval = cpu_to_be32(args->hashval); |
ff105f75 | 553 | blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr( |
a2ceac1f | 554 | (char *)dep - (char *)hdr)); |
2bd0ea18 NS |
555 | xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh); |
556 | /* | |
557 | * Mark space for the data entry used. | |
558 | */ | |
15348e04 DW |
559 | error = xfs_dir2_data_use_free(args, bp, dup, |
560 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), | |
561 | (xfs_dir2_data_aoff_t)len, &needlog, &needscan); | |
562 | if (error) | |
563 | return error; | |
2bd0ea18 NS |
564 | /* |
565 | * Create the new data entry. | |
566 | */ | |
5e656dbb | 567 | dep->inumber = cpu_to_be64(args->inumber); |
2bd0ea18 | 568 | dep->namelen = args->namelen; |
32181a02 | 569 | memcpy(dep->name, args->name, args->namelen); |
28e6e614 | 570 | xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype); |
823711f2 | 571 | tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep); |
a2ceac1f | 572 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
2bd0ea18 NS |
573 | /* |
574 | * Clean up the bestfree array and log the header, tail, and entry. | |
575 | */ | |
576 | if (needscan) | |
d85595d0 | 577 | xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog); |
2bd0ea18 | 578 | if (needlog) |
ff105f75 | 579 | xfs_dir2_data_log_header(args, bp); |
2bd0ea18 | 580 | xfs_dir2_block_log_tail(tp, bp); |
ff105f75 | 581 | xfs_dir2_data_log_entry(args, bp, dep); |
90ea28c3 | 582 | xfs_dir3_data_check(dp, bp); |
2bd0ea18 NS |
583 | return 0; |
584 | } | |
585 | ||
586 | /* | |
587 | * Log leaf entries from the block. | |
588 | */ | |
5e656dbb | 589 | static void |
2bd0ea18 NS |
590 | xfs_dir2_block_log_leaf( |
591 | xfs_trans_t *tp, /* transaction structure */ | |
a2ceac1f | 592 | struct xfs_buf *bp, /* block buffer */ |
2bd0ea18 NS |
593 | int first, /* index of first logged leaf */ |
594 | int last) /* index of last logged leaf */ | |
595 | { | |
a2ceac1f DC |
596 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
597 | xfs_dir2_leaf_entry_t *blp; | |
598 | xfs_dir2_block_tail_t *btp; | |
2bd0ea18 | 599 | |
ff105f75 | 600 | btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr); |
5e656dbb | 601 | blp = xfs_dir2_block_leaf_p(btp); |
a2ceac1f DC |
602 | xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr), |
603 | (uint)((char *)&blp[last + 1] - (char *)hdr - 1)); | |
2bd0ea18 NS |
604 | } |
605 | ||
606 | /* | |
607 | * Log the block tail. | |
608 | */ | |
5e656dbb | 609 | static void |
2bd0ea18 NS |
610 | xfs_dir2_block_log_tail( |
611 | xfs_trans_t *tp, /* transaction structure */ | |
a2ceac1f | 612 | struct xfs_buf *bp) /* block buffer */ |
2bd0ea18 | 613 | { |
a2ceac1f DC |
614 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
615 | xfs_dir2_block_tail_t *btp; | |
2bd0ea18 | 616 | |
ff105f75 | 617 | btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr); |
a2ceac1f DC |
618 | xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr), |
619 | (uint)((char *)(btp + 1) - (char *)hdr - 1)); | |
2bd0ea18 NS |
620 | } |
621 | ||
622 | /* | |
623 | * Look up an entry in the block. This is the external routine, | |
624 | * xfs_dir2_block_lookup_int does the real work. | |
625 | */ | |
626 | int /* error */ | |
627 | xfs_dir2_block_lookup( | |
628 | xfs_da_args_t *args) /* dir lookup arguments */ | |
629 | { | |
a2ceac1f | 630 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 631 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 632 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
633 | xfs_dir2_block_tail_t *btp; /* block tail */ |
634 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
635 | xfs_inode_t *dp; /* incore inode */ | |
636 | int ent; /* entry index */ | |
637 | int error; /* error return value */ | |
2bd0ea18 | 638 | |
56b2de80 DC |
639 | trace_xfs_dir2_block_lookup(args); |
640 | ||
2bd0ea18 NS |
641 | /* |
642 | * Get the buffer, look up the entry. | |
643 | * If not found (ENOENT) then return, have no buffer. | |
644 | */ | |
0e266570 | 645 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) |
2bd0ea18 NS |
646 | return error; |
647 | dp = args->dp; | |
a2ceac1f | 648 | hdr = bp->b_addr; |
90ea28c3 | 649 | xfs_dir3_data_check(dp, bp); |
ff105f75 | 650 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 651 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
652 | /* |
653 | * Get the offset from the leaf entry, to point to the data. | |
654 | */ | |
a2ceac1f | 655 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
ff105f75 DC |
656 | xfs_dir2_dataptr_to_off(args->geo, |
657 | be32_to_cpu(blp[ent].address))); | |
2bd0ea18 | 658 | /* |
5e656dbb | 659 | * Fill in inode number, CI name if appropriate, release the block. |
2bd0ea18 | 660 | */ |
5e656dbb | 661 | args->inumber = be64_to_cpu(dep->inumber); |
28e6e614 | 662 | args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep); |
5e656dbb | 663 | error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
a2ceac1f | 664 | xfs_trans_brelse(args->trans, bp); |
1e68581b | 665 | return error; |
2bd0ea18 NS |
666 | } |
667 | ||
668 | /* | |
669 | * Internal block lookup routine. | |
670 | */ | |
5e656dbb | 671 | static int /* error */ |
2bd0ea18 NS |
672 | xfs_dir2_block_lookup_int( |
673 | xfs_da_args_t *args, /* dir lookup arguments */ | |
a2ceac1f | 674 | struct xfs_buf **bpp, /* returned block buffer */ |
2bd0ea18 NS |
675 | int *entno) /* returned entry number */ |
676 | { | |
677 | xfs_dir2_dataptr_t addr; /* data entry address */ | |
a2ceac1f | 678 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 679 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 680 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
681 | xfs_dir2_block_tail_t *btp; /* block tail */ |
682 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
683 | xfs_inode_t *dp; /* incore inode */ | |
684 | int error; /* error return value */ | |
685 | xfs_dahash_t hash; /* found hash value */ | |
686 | int high; /* binary search high index */ | |
687 | int low; /* binary search low index */ | |
688 | int mid; /* binary search current idx */ | |
2bd0ea18 | 689 | xfs_trans_t *tp; /* transaction pointer */ |
5e656dbb | 690 | enum xfs_dacmp cmp; /* comparison result */ |
2bd0ea18 NS |
691 | |
692 | dp = args->dp; | |
693 | tp = args->trans; | |
a2ceac1f | 694 | |
693fc8f6 | 695 | error = xfs_dir3_block_read(tp, dp, &bp); |
a2ceac1f | 696 | if (error) |
2bd0ea18 | 697 | return error; |
a2ceac1f DC |
698 | |
699 | hdr = bp->b_addr; | |
90ea28c3 | 700 | xfs_dir3_data_check(dp, bp); |
ff105f75 | 701 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 702 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
703 | /* |
704 | * Loop doing a binary search for our hash value. | |
705 | * Find our entry, ENOENT if it's not there. | |
706 | */ | |
5e656dbb | 707 | for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) { |
2bd0ea18 NS |
708 | ASSERT(low <= high); |
709 | mid = (low + high) >> 1; | |
5e656dbb | 710 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
2bd0ea18 NS |
711 | break; |
712 | if (hash < args->hashval) | |
713 | low = mid + 1; | |
714 | else | |
715 | high = mid - 1; | |
716 | if (low > high) { | |
5e656dbb | 717 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
a2ceac1f | 718 | xfs_trans_brelse(tp, bp); |
12b53197 | 719 | return -ENOENT; |
2bd0ea18 NS |
720 | } |
721 | } | |
722 | /* | |
723 | * Back up to the first one with the right hash value. | |
724 | */ | |
5e656dbb | 725 | while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) { |
2bd0ea18 NS |
726 | mid--; |
727 | } | |
728 | /* | |
729 | * Now loop forward through all the entries with the | |
730 | * right hash value looking for our name. | |
731 | */ | |
732 | do { | |
5e656dbb | 733 | if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR) |
2bd0ea18 NS |
734 | continue; |
735 | /* | |
736 | * Get pointer to the entry from the leaf. | |
737 | */ | |
738 | dep = (xfs_dir2_data_entry_t *) | |
ff105f75 | 739 | ((char *)hdr + xfs_dir2_dataptr_to_off(args->geo, addr)); |
2bd0ea18 | 740 | /* |
5e656dbb BN |
741 | * Compare name and if it's an exact match, return the index |
742 | * and buffer. If it's the first case-insensitive match, store | |
743 | * the index and buffer and continue looking for an exact match. | |
2bd0ea18 | 744 | */ |
e169cc9b | 745 | cmp = xfs_dir2_compname(args, dep->name, dep->namelen); |
5e656dbb BN |
746 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { |
747 | args->cmpresult = cmp; | |
2bd0ea18 NS |
748 | *bpp = bp; |
749 | *entno = mid; | |
5e656dbb BN |
750 | if (cmp == XFS_CMP_EXACT) |
751 | return 0; | |
2bd0ea18 | 752 | } |
5e656dbb BN |
753 | } while (++mid < be32_to_cpu(btp->count) && |
754 | be32_to_cpu(blp[mid].hashval) == hash); | |
755 | ||
756 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); | |
757 | /* | |
758 | * Here, we can only be doing a lookup (not a rename or replace). | |
759 | * If a case-insensitive match was found earlier, return success. | |
760 | */ | |
761 | if (args->cmpresult == XFS_CMP_CASE) | |
762 | return 0; | |
2bd0ea18 NS |
763 | /* |
764 | * No match, release the buffer and return ENOENT. | |
765 | */ | |
a2ceac1f | 766 | xfs_trans_brelse(tp, bp); |
12b53197 | 767 | return -ENOENT; |
2bd0ea18 NS |
768 | } |
769 | ||
770 | /* | |
771 | * Remove an entry from a block format directory. | |
772 | * If that makes the block small enough to fit in shortform, transform it. | |
773 | */ | |
774 | int /* error */ | |
775 | xfs_dir2_block_removename( | |
776 | xfs_da_args_t *args) /* directory operation args */ | |
777 | { | |
a2ceac1f | 778 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 779 | xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */ |
a2ceac1f | 780 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
781 | xfs_dir2_block_tail_t *btp; /* block tail */ |
782 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
783 | xfs_inode_t *dp; /* incore inode */ | |
784 | int ent; /* block leaf entry index */ | |
785 | int error; /* error return value */ | |
2bd0ea18 NS |
786 | int needlog; /* need to log block header */ |
787 | int needscan; /* need to fixup bestfree */ | |
788 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ | |
789 | int size; /* shortform size */ | |
790 | xfs_trans_t *tp; /* transaction pointer */ | |
791 | ||
56b2de80 DC |
792 | trace_xfs_dir2_block_removename(args); |
793 | ||
2bd0ea18 NS |
794 | /* |
795 | * Look up the entry in the block. Gets the buffer and entry index. | |
796 | * It will always be there, the vnodeops level does a lookup first. | |
797 | */ | |
0e266570 | 798 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
2bd0ea18 NS |
799 | return error; |
800 | } | |
801 | dp = args->dp; | |
802 | tp = args->trans; | |
a2ceac1f | 803 | hdr = bp->b_addr; |
ff105f75 | 804 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 805 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
806 | /* |
807 | * Point to the data entry using the leaf entry. | |
808 | */ | |
ff105f75 DC |
809 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
810 | xfs_dir2_dataptr_to_off(args->geo, | |
811 | be32_to_cpu(blp[ent].address))); | |
2bd0ea18 NS |
812 | /* |
813 | * Mark the data entry's space free. | |
814 | */ | |
815 | needlog = needscan = 0; | |
ff105f75 | 816 | xfs_dir2_data_make_free(args, bp, |
a2ceac1f | 817 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr), |
271a654f CH |
818 | xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog, |
819 | &needscan); | |
2bd0ea18 NS |
820 | /* |
821 | * Fix up the block tail. | |
822 | */ | |
5e656dbb | 823 | be32_add_cpu(&btp->stale, 1); |
2bd0ea18 NS |
824 | xfs_dir2_block_log_tail(tp, bp); |
825 | /* | |
826 | * Remove the leaf entry by marking it stale. | |
827 | */ | |
5e656dbb | 828 | blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
2bd0ea18 NS |
829 | xfs_dir2_block_log_leaf(tp, bp, ent, ent); |
830 | /* | |
831 | * Fix up bestfree, log the header if necessary. | |
832 | */ | |
833 | if (needscan) | |
d85595d0 | 834 | xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog); |
2bd0ea18 | 835 | if (needlog) |
ff105f75 | 836 | xfs_dir2_data_log_header(args, bp); |
90ea28c3 | 837 | xfs_dir3_data_check(dp, bp); |
2bd0ea18 NS |
838 | /* |
839 | * See if the size as a shortform is good enough. | |
840 | */ | |
a2ceac1f | 841 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); |
eae3e30d | 842 | if (size > xfs_inode_data_fork_size(dp)) |
2bd0ea18 | 843 | return 0; |
a2ceac1f | 844 | |
2bd0ea18 NS |
845 | /* |
846 | * If it works, do the conversion. | |
847 | */ | |
848 | return xfs_dir2_block_to_sf(args, bp, size, &sfh); | |
849 | } | |
850 | ||
851 | /* | |
852 | * Replace an entry in a V2 block directory. | |
853 | * Change the inode number to the new value. | |
854 | */ | |
855 | int /* error */ | |
856 | xfs_dir2_block_replace( | |
857 | xfs_da_args_t *args) /* directory operation args */ | |
858 | { | |
a2ceac1f | 859 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 860 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 861 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
862 | xfs_dir2_block_tail_t *btp; /* block tail */ |
863 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
864 | xfs_inode_t *dp; /* incore inode */ | |
865 | int ent; /* leaf entry index */ | |
866 | int error; /* error return value */ | |
2bd0ea18 | 867 | |
56b2de80 DC |
868 | trace_xfs_dir2_block_replace(args); |
869 | ||
2bd0ea18 NS |
870 | /* |
871 | * Lookup the entry in the directory. Get buffer and entry index. | |
872 | * This will always succeed since the caller has already done a lookup. | |
873 | */ | |
0e266570 | 874 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
2bd0ea18 NS |
875 | return error; |
876 | } | |
877 | dp = args->dp; | |
a2ceac1f | 878 | hdr = bp->b_addr; |
ff105f75 | 879 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 880 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
881 | /* |
882 | * Point to the data entry we need to change. | |
883 | */ | |
ff105f75 DC |
884 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
885 | xfs_dir2_dataptr_to_off(args->geo, | |
886 | be32_to_cpu(blp[ent].address))); | |
5e656dbb | 887 | ASSERT(be64_to_cpu(dep->inumber) != args->inumber); |
2bd0ea18 NS |
888 | /* |
889 | * Change the inode number to the new value. | |
890 | */ | |
5e656dbb | 891 | dep->inumber = cpu_to_be64(args->inumber); |
28e6e614 | 892 | xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype); |
ff105f75 | 893 | xfs_dir2_data_log_entry(args, bp, dep); |
90ea28c3 | 894 | xfs_dir3_data_check(dp, bp); |
2bd0ea18 NS |
895 | return 0; |
896 | } | |
897 | ||
898 | /* | |
899 | * Qsort comparison routine for the block leaf entries. | |
900 | */ | |
901 | static int /* sort order */ | |
902 | xfs_dir2_block_sort( | |
903 | const void *a, /* first leaf entry */ | |
904 | const void *b) /* second leaf entry */ | |
905 | { | |
906 | const xfs_dir2_leaf_entry_t *la; /* first leaf entry */ | |
907 | const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */ | |
908 | ||
909 | la = a; | |
910 | lb = b; | |
5e656dbb BN |
911 | return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 : |
912 | (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0); | |
2bd0ea18 NS |
913 | } |
914 | ||
915 | /* | |
916 | * Convert a V2 leaf directory to a V2 block directory if possible. | |
917 | */ | |
918 | int /* error */ | |
919 | xfs_dir2_leaf_to_block( | |
920 | xfs_da_args_t *args, /* operation arguments */ | |
a2ceac1f DC |
921 | struct xfs_buf *lbp, /* leaf buffer */ |
922 | struct xfs_buf *dbp) /* data buffer */ | |
2bd0ea18 | 923 | { |
5e656dbb | 924 | __be16 *bestsp; /* leaf bests table */ |
a2ceac1f | 925 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 NS |
926 | xfs_dir2_block_tail_t *btp; /* block tail */ |
927 | xfs_inode_t *dp; /* incore directory inode */ | |
928 | xfs_dir2_data_unused_t *dup; /* unused data entry */ | |
929 | int error; /* error return value */ | |
930 | int from; /* leaf from index */ | |
931 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
932 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
933 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
934 | xfs_mount_t *mp; /* file system mount point */ | |
935 | int needlog; /* need to log data header */ | |
936 | int needscan; /* need to scan for bestfree */ | |
937 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ | |
938 | int size; /* bytes used */ | |
5e656dbb | 939 | __be16 *tagp; /* end of entry (tag) */ |
2bd0ea18 NS |
940 | int to; /* block/leaf to index */ |
941 | xfs_trans_t *tp; /* transaction pointer */ | |
65b80c98 | 942 | struct xfs_dir3_icleaf_hdr leafhdr; |
2bd0ea18 | 943 | |
56b2de80 DC |
944 | trace_xfs_dir2_leaf_to_block(args); |
945 | ||
2bd0ea18 NS |
946 | dp = args->dp; |
947 | tp = args->trans; | |
948 | mp = dp->i_mount; | |
a2ceac1f | 949 | leaf = lbp->b_addr; |
9db68faf | 950 | xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf); |
ff105f75 | 951 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
65b80c98 DC |
952 | |
953 | ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC || | |
954 | leafhdr.magic == XFS_DIR3_LEAF1_MAGIC); | |
2bd0ea18 NS |
955 | /* |
956 | * If there are data blocks other than the first one, take this | |
957 | * opportunity to remove trailing empty data blocks that may have | |
958 | * been left behind during no-space-reservation operations. | |
959 | * These will show up in the leaf bests table. | |
960 | */ | |
509dcb4b | 961 | while (dp->i_disk_size > args->geo->blksize) { |
693fc8f6 DC |
962 | int hdrsz; |
963 | ||
58a1d356 | 964 | hdrsz = args->geo->data_entry_offset; |
5e656dbb BN |
965 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
966 | if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) == | |
ff105f75 | 967 | args->geo->blksize - hdrsz) { |
0e266570 | 968 | if ((error = |
2bd0ea18 | 969 | xfs_dir2_leaf_trim_data(args, lbp, |
5e656dbb | 970 | (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1)))) |
a2ceac1f DC |
971 | return error; |
972 | } else | |
973 | return 0; | |
2bd0ea18 NS |
974 | } |
975 | /* | |
976 | * Read the data block if we don't already have it, give up if it fails. | |
977 | */ | |
a2ceac1f | 978 | if (!dbp) { |
5f356ae6 | 979 | error = xfs_dir3_data_read(tp, dp, args->geo->datablk, 0, &dbp); |
a2ceac1f DC |
980 | if (error) |
981 | return error; | |
2bd0ea18 | 982 | } |
a2ceac1f | 983 | hdr = dbp->b_addr; |
90ea28c3 DC |
984 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
985 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); | |
986 | ||
2bd0ea18 NS |
987 | /* |
988 | * Size of the "leaf" area in the block. | |
989 | */ | |
a2ceac1f | 990 | size = (uint)sizeof(xfs_dir2_block_tail_t) + |
65b80c98 | 991 | (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale); |
2bd0ea18 NS |
992 | /* |
993 | * Look at the last data entry. | |
994 | */ | |
ff105f75 | 995 | tagp = (__be16 *)((char *)hdr + args->geo->blksize) - 1; |
a2ceac1f | 996 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); |
2bd0ea18 NS |
997 | /* |
998 | * If it's not free or is too short we can't do it. | |
999 | */ | |
5e656dbb | 1000 | if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG || |
a2ceac1f DC |
1001 | be16_to_cpu(dup->length) < size) |
1002 | return 0; | |
1003 | ||
2bd0ea18 NS |
1004 | /* |
1005 | * Start converting it to block form. | |
1006 | */ | |
8b4dc4a9 | 1007 | xfs_dir3_block_init(mp, tp, dbp, dp); |
693fc8f6 | 1008 | |
2bd0ea18 NS |
1009 | needlog = 1; |
1010 | needscan = 0; | |
1011 | /* | |
1012 | * Use up the space at the end of the block (blp/btp). | |
1013 | */ | |
15348e04 DW |
1014 | error = xfs_dir2_data_use_free(args, dbp, dup, |
1015 | args->geo->blksize - size, size, &needlog, &needscan); | |
1016 | if (error) | |
1017 | return error; | |
2bd0ea18 NS |
1018 | /* |
1019 | * Initialize the block tail. | |
1020 | */ | |
ff105f75 | 1021 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
65b80c98 | 1022 | btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale); |
46eca962 | 1023 | btp->stale = 0; |
2bd0ea18 NS |
1024 | xfs_dir2_block_log_tail(tp, dbp); |
1025 | /* | |
1026 | * Initialize the block leaf area. We compact out stale entries. | |
1027 | */ | |
5e656dbb | 1028 | lep = xfs_dir2_block_leaf_p(btp); |
65b80c98 | 1029 | for (from = to = 0; from < leafhdr.count; from++) { |
a2279497 CH |
1030 | if (leafhdr.ents[from].address == |
1031 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) | |
2bd0ea18 | 1032 | continue; |
a2279497 | 1033 | lep[to++] = leafhdr.ents[from]; |
2bd0ea18 | 1034 | } |
5e656dbb BN |
1035 | ASSERT(to == be32_to_cpu(btp->count)); |
1036 | xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1); | |
2bd0ea18 NS |
1037 | /* |
1038 | * Scan the bestfree if we need it and log the data block header. | |
1039 | */ | |
1040 | if (needscan) | |
d85595d0 | 1041 | xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog); |
2bd0ea18 | 1042 | if (needlog) |
ff105f75 | 1043 | xfs_dir2_data_log_header(args, dbp); |
2bd0ea18 NS |
1044 | /* |
1045 | * Pitch the old leaf block. | |
1046 | */ | |
ff105f75 | 1047 | error = xfs_da_shrink_inode(args, args->geo->leafblk, lbp); |
a2ceac1f DC |
1048 | if (error) |
1049 | return error; | |
1050 | ||
2bd0ea18 NS |
1051 | /* |
1052 | * Now see if the resulting block can be shrunken to shortform. | |
1053 | */ | |
a2ceac1f | 1054 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); |
eae3e30d | 1055 | if (size > xfs_inode_data_fork_size(dp)) |
a2ceac1f DC |
1056 | return 0; |
1057 | ||
2bd0ea18 | 1058 | return xfs_dir2_block_to_sf(args, dbp, size, &sfh); |
2bd0ea18 NS |
1059 | } |
1060 | ||
1061 | /* | |
1062 | * Convert the shortform directory to block form. | |
1063 | */ | |
1064 | int /* error */ | |
1065 | xfs_dir2_sf_to_block( | |
26df2433 | 1066 | struct xfs_da_args *args) |
2bd0ea18 | 1067 | { |
26df2433 CH |
1068 | struct xfs_trans *tp = args->trans; |
1069 | struct xfs_inode *dp = args->dp; | |
1070 | struct xfs_mount *mp = dp->i_mount; | |
722e81c1 | 1071 | struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK); |
58a1d356 | 1072 | struct xfs_da_geometry *geo = args->geo; |
2bd0ea18 | 1073 | xfs_dir2_db_t blkno; /* dir-relative block # (0) */ |
a2ceac1f | 1074 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 1075 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 1076 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 | 1077 | xfs_dir2_block_tail_t *btp; /* block tail pointer */ |
2bd0ea18 | 1078 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ |
2bd0ea18 NS |
1079 | int dummy; /* trash */ |
1080 | xfs_dir2_data_unused_t *dup; /* unused entry pointer */ | |
1081 | int endoffset; /* end of data objects */ | |
1082 | int error; /* error return value */ | |
1083 | int i; /* index */ | |
2bd0ea18 NS |
1084 | int needlog; /* need to log block header */ |
1085 | int needscan; /* need to scan block freespc */ | |
1086 | int newoffset; /* offset from current entry */ | |
58a1d356 | 1087 | unsigned int offset = geo->data_entry_offset; |
2bd0ea18 | 1088 | xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */ |
f8146197 | 1089 | struct xfs_dir2_sf_hdr *oldsfp = ifp->if_data; |
a2ceac1f | 1090 | xfs_dir2_sf_hdr_t *sfp; /* shortform header */ |
5e656dbb | 1091 | __be16 *tagp; /* end of data entry */ |
5e656dbb | 1092 | struct xfs_name name; |
2bd0ea18 | 1093 | |
56b2de80 DC |
1094 | trace_xfs_dir2_sf_to_block(args); |
1095 | ||
9b014ae4 | 1096 | ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL); |
509dcb4b | 1097 | ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent)); |
a2ceac1f | 1098 | |
509dcb4b | 1099 | ASSERT(ifp->if_bytes == dp->i_disk_size); |
f8146197 | 1100 | ASSERT(oldsfp != NULL); |
509dcb4b | 1101 | ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count)); |
87c472b7 | 1102 | ASSERT(dp->i_df.if_nextents == 0); |
a2ceac1f | 1103 | |
2bd0ea18 | 1104 | /* |
a2ceac1f | 1105 | * Copy the directory into a temporary buffer. |
2bd0ea18 NS |
1106 | * Then pitch the incore inode data so we can make extents. |
1107 | */ | |
6cd1e6db | 1108 | sfp = kmem_alloc(ifp->if_bytes, 0); |
3f17ed4b | 1109 | memcpy(sfp, oldsfp, ifp->if_bytes); |
d663096d | 1110 | |
3f17ed4b | 1111 | xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK); |
feee8e52 | 1112 | xfs_bmap_local_to_extents_empty(tp, dp, XFS_DATA_FORK); |
509dcb4b | 1113 | dp->i_disk_size = 0; |
a2ceac1f | 1114 | |
2bd0ea18 NS |
1115 | /* |
1116 | * Add block 0 to the inode. | |
1117 | */ | |
1118 | error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno); | |
15348e04 DW |
1119 | if (error) |
1120 | goto out_free; | |
2bd0ea18 | 1121 | /* |
693fc8f6 | 1122 | * Initialize the data block, then convert it to block format. |
2bd0ea18 | 1123 | */ |
693fc8f6 | 1124 | error = xfs_dir3_data_init(args, blkno, &bp); |
15348e04 DW |
1125 | if (error) |
1126 | goto out_free; | |
8b4dc4a9 | 1127 | xfs_dir3_block_init(mp, tp, bp, dp); |
a2ceac1f | 1128 | hdr = bp->b_addr; |
693fc8f6 | 1129 | |
2bd0ea18 NS |
1130 | /* |
1131 | * Compute size of block "tail" area. | |
1132 | */ | |
1133 | i = (uint)sizeof(*btp) + | |
a2ceac1f | 1134 | (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t); |
2bd0ea18 NS |
1135 | /* |
1136 | * The whole thing is initialized to free by the init routine. | |
1137 | * Say we're using the leaf and tail area. | |
1138 | */ | |
9c036d76 | 1139 | dup = bp->b_addr + offset; |
2bd0ea18 | 1140 | needlog = needscan = 0; |
15348e04 DW |
1141 | error = xfs_dir2_data_use_free(args, bp, dup, args->geo->blksize - i, |
1142 | i, &needlog, &needscan); | |
1143 | if (error) | |
1144 | goto out_free; | |
2bd0ea18 NS |
1145 | ASSERT(needscan == 0); |
1146 | /* | |
1147 | * Fill in the tail. | |
1148 | */ | |
ff105f75 | 1149 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
a2ceac1f | 1150 | btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */ |
46eca962 | 1151 | btp->stale = 0; |
5e656dbb | 1152 | blp = xfs_dir2_block_leaf_p(btp); |
a2ceac1f | 1153 | endoffset = (uint)((char *)blp - (char *)hdr); |
2bd0ea18 NS |
1154 | /* |
1155 | * Remove the freespace, we'll manage it. | |
1156 | */ | |
15348e04 DW |
1157 | error = xfs_dir2_data_use_free(args, bp, dup, |
1158 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), | |
1159 | be16_to_cpu(dup->length), &needlog, &needscan); | |
1160 | if (error) | |
1161 | goto out_free; | |
26df2433 | 1162 | |
2bd0ea18 NS |
1163 | /* |
1164 | * Create entry for . | |
1165 | */ | |
26df2433 | 1166 | dep = bp->b_addr + offset; |
5e656dbb | 1167 | dep->inumber = cpu_to_be64(dp->i_ino); |
2bd0ea18 NS |
1168 | dep->namelen = 1; |
1169 | dep->name[0] = '.'; | |
28e6e614 | 1170 | xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR); |
823711f2 | 1171 | tagp = xfs_dir2_data_entry_tag_p(mp, dep); |
26df2433 | 1172 | *tagp = cpu_to_be16(offset); |
ff105f75 | 1173 | xfs_dir2_data_log_entry(args, bp, dep); |
5e656dbb | 1174 | blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot); |
26df2433 | 1175 | blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset)); |
271a654f | 1176 | offset += xfs_dir2_data_entsize(mp, dep->namelen); |
26df2433 | 1177 | |
2bd0ea18 NS |
1178 | /* |
1179 | * Create entry for .. | |
1180 | */ | |
26df2433 | 1181 | dep = bp->b_addr + offset; |
8a7190bd | 1182 | dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp)); |
2bd0ea18 NS |
1183 | dep->namelen = 2; |
1184 | dep->name[0] = dep->name[1] = '.'; | |
28e6e614 | 1185 | xfs_dir2_data_put_ftype(mp, dep, XFS_DIR3_FT_DIR); |
823711f2 | 1186 | tagp = xfs_dir2_data_entry_tag_p(mp, dep); |
26df2433 | 1187 | *tagp = cpu_to_be16(offset); |
ff105f75 | 1188 | xfs_dir2_data_log_entry(args, bp, dep); |
5e656dbb | 1189 | blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot); |
26df2433 | 1190 | blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(offset)); |
271a654f | 1191 | offset += xfs_dir2_data_entsize(mp, dep->namelen); |
26df2433 | 1192 | |
2bd0ea18 NS |
1193 | /* |
1194 | * Loop over existing entries, stuff them in. | |
1195 | */ | |
a2ceac1f DC |
1196 | i = 0; |
1197 | if (!sfp->count) | |
2bd0ea18 NS |
1198 | sfep = NULL; |
1199 | else | |
5e656dbb | 1200 | sfep = xfs_dir2_sf_firstentry(sfp); |
26df2433 | 1201 | |
2bd0ea18 NS |
1202 | /* |
1203 | * Need to preserve the existing offset values in the sf directory. | |
1204 | * Insert holes (unused entries) where necessary. | |
1205 | */ | |
1206 | while (offset < endoffset) { | |
1207 | /* | |
1208 | * sfep is null when we reach the end of the list. | |
1209 | */ | |
1210 | if (sfep == NULL) | |
1211 | newoffset = endoffset; | |
1212 | else | |
5e656dbb | 1213 | newoffset = xfs_dir2_sf_get_offset(sfep); |
2bd0ea18 NS |
1214 | /* |
1215 | * There should be a hole here, make one. | |
1216 | */ | |
1217 | if (offset < newoffset) { | |
26df2433 | 1218 | dup = bp->b_addr + offset; |
5e656dbb BN |
1219 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
1220 | dup->length = cpu_to_be16(newoffset - offset); | |
26df2433 | 1221 | *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(offset); |
ff105f75 DC |
1222 | xfs_dir2_data_log_unused(args, bp, dup); |
1223 | xfs_dir2_data_freeinsert(hdr, | |
04f6f354 CH |
1224 | xfs_dir2_data_bestfree_p(mp, hdr), |
1225 | dup, &dummy); | |
5e656dbb | 1226 | offset += be16_to_cpu(dup->length); |
2bd0ea18 NS |
1227 | continue; |
1228 | } | |
1229 | /* | |
1230 | * Copy a real entry. | |
1231 | */ | |
26df2433 | 1232 | dep = bp->b_addr + newoffset; |
e96bd2d3 | 1233 | dep->inumber = cpu_to_be64(xfs_dir2_sf_get_ino(mp, sfp, sfep)); |
2bd0ea18 | 1234 | dep->namelen = sfep->namelen; |
28e6e614 CH |
1235 | xfs_dir2_data_put_ftype(mp, dep, |
1236 | xfs_dir2_sf_get_ftype(mp, sfep)); | |
32181a02 | 1237 | memcpy(dep->name, sfep->name, dep->namelen); |
823711f2 | 1238 | tagp = xfs_dir2_data_entry_tag_p(mp, dep); |
26df2433 | 1239 | *tagp = cpu_to_be16(newoffset); |
ff105f75 | 1240 | xfs_dir2_data_log_entry(args, bp, dep); |
5e656dbb BN |
1241 | name.name = sfep->name; |
1242 | name.len = sfep->namelen; | |
e169cc9b | 1243 | blp[2 + i].hashval = cpu_to_be32(xfs_dir2_hashname(mp, &name)); |
26df2433 CH |
1244 | blp[2 + i].address = |
1245 | cpu_to_be32(xfs_dir2_byte_to_dataptr(newoffset)); | |
a2ceac1f DC |
1246 | offset = (int)((char *)(tagp + 1) - (char *)hdr); |
1247 | if (++i == sfp->count) | |
2bd0ea18 NS |
1248 | sfep = NULL; |
1249 | else | |
660836c9 | 1250 | sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep); |
2bd0ea18 | 1251 | } |
d663096d | 1252 | /* Done with the temporary buffer */ |
a2ceac1f | 1253 | kmem_free(sfp); |
2bd0ea18 NS |
1254 | /* |
1255 | * Sort the leaf entries by hash value. | |
1256 | */ | |
5e656dbb | 1257 | xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort); |
5000d01d | 1258 | /* |
2bd0ea18 NS |
1259 | * Log the leaf entry area and tail. |
1260 | * Already logged the header in data_init, ignore needlog. | |
1261 | */ | |
1262 | ASSERT(needscan == 0); | |
5e656dbb | 1263 | xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1); |
2bd0ea18 | 1264 | xfs_dir2_block_log_tail(tp, bp); |
90ea28c3 | 1265 | xfs_dir3_data_check(dp, bp); |
2bd0ea18 | 1266 | return 0; |
15348e04 DW |
1267 | out_free: |
1268 | kmem_free(sfp); | |
1269 | return error; | |
2bd0ea18 | 1270 | } |