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