[XFS] Remove xfs_macros.c, xfs_macros.h, rework headers a whole lot.
[pandora-kernel.git] / fs / xfs / xfs_dir2_data.c
1 /*
2  * Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 #include "xfs.h"
33 #include "xfs_fs.h"
34 #include "xfs_types.h"
35 #include "xfs_log.h"
36 #include "xfs_inum.h"
37 #include "xfs_trans.h"
38 #include "xfs_sb.h"
39 #include "xfs_dir.h"
40 #include "xfs_dir2.h"
41 #include "xfs_dmapi.h"
42 #include "xfs_mount.h"
43 #include "xfs_da_btree.h"
44 #include "xfs_bmap_btree.h"
45 #include "xfs_dir_sf.h"
46 #include "xfs_dir2_sf.h"
47 #include "xfs_attr_sf.h"
48 #include "xfs_dinode.h"
49 #include "xfs_inode.h"
50 #include "xfs_dir_leaf.h"
51 #include "xfs_dir2_data.h"
52 #include "xfs_dir2_leaf.h"
53 #include "xfs_dir2_block.h"
54 #include "xfs_error.h"
55
56 #ifdef DEBUG
57 /*
58  * Check the consistency of the data block.
59  * The input can also be a block-format directory.
60  * Pop an assert if we find anything bad.
61  */
62 void
63 xfs_dir2_data_check(
64         xfs_inode_t             *dp,            /* incore inode pointer */
65         xfs_dabuf_t             *bp)            /* data block's buffer */
66 {
67         xfs_dir2_dataptr_t      addr;           /* addr for leaf lookup */
68         xfs_dir2_data_free_t    *bf;            /* bestfree table */
69         xfs_dir2_block_tail_t   *btp=NULL;      /* block tail */
70         int                     count;          /* count of entries found */
71         xfs_dir2_data_t         *d;             /* data block pointer */
72         xfs_dir2_data_entry_t   *dep;           /* data entry */
73         xfs_dir2_data_free_t    *dfp;           /* bestfree entry */
74         xfs_dir2_data_unused_t  *dup;           /* unused entry */
75         char                    *endp;          /* end of useful data */
76         int                     freeseen;       /* mask of bestfrees seen */
77         xfs_dahash_t            hash;           /* hash of current name */
78         int                     i;              /* leaf index */
79         int                     lastfree;       /* last entry was unused */
80         xfs_dir2_leaf_entry_t   *lep=NULL;      /* block leaf entries */
81         xfs_mount_t             *mp;            /* filesystem mount point */
82         char                    *p;             /* current data position */
83         int                     stale;          /* count of stale leaves */
84
85         mp = dp->i_mount;
86         d = bp->data;
87         ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
88                INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
89         bf = d->hdr.bestfree;
90         p = (char *)d->u;
91         if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
92                 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
93                 lep = XFS_DIR2_BLOCK_LEAF_P(btp);
94                 endp = (char *)lep;
95         } else
96                 endp = (char *)d + mp->m_dirblksize;
97         count = lastfree = freeseen = 0;
98         /*
99          * Account for zero bestfree entries.
100          */
101         if (!bf[0].length) {
102                 ASSERT(!bf[0].offset);
103                 freeseen |= 1 << 0;
104         }
105         if (!bf[1].length) {
106                 ASSERT(!bf[1].offset);
107                 freeseen |= 1 << 1;
108         }
109         if (!bf[2].length) {
110                 ASSERT(!bf[2].offset);
111                 freeseen |= 1 << 2;
112         }
113         ASSERT(INT_GET(bf[0].length, ARCH_CONVERT) >= INT_GET(bf[1].length, ARCH_CONVERT));
114         ASSERT(INT_GET(bf[1].length, ARCH_CONVERT) >= INT_GET(bf[2].length, ARCH_CONVERT));
115         /*
116          * Loop over the data/unused entries.
117          */
118         while (p < endp) {
119                 dup = (xfs_dir2_data_unused_t *)p;
120                 /*
121                  * If it's unused, look for the space in the bestfree table.
122                  * If we find it, account for that, else make sure it
123                  * doesn't need to be there.
124                  */
125                 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
126                         ASSERT(lastfree == 0);
127                         ASSERT(INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT) ==
128                                (char *)dup - (char *)d);
129                         dfp = xfs_dir2_data_freefind(d, dup);
130                         if (dfp) {
131                                 i = (int)(dfp - bf);
132                                 ASSERT((freeseen & (1 << i)) == 0);
133                                 freeseen |= 1 << i;
134                         } else
135                                 ASSERT(INT_GET(dup->length, ARCH_CONVERT) <= INT_GET(bf[2].length, ARCH_CONVERT));
136                         p += INT_GET(dup->length, ARCH_CONVERT);
137                         lastfree = 1;
138                         continue;
139                 }
140                 /*
141                  * It's a real entry.  Validate the fields.
142                  * If this is a block directory then make sure it's
143                  * in the leaf section of the block.
144                  * The linear search is crude but this is DEBUG code.
145                  */
146                 dep = (xfs_dir2_data_entry_t *)p;
147                 ASSERT(dep->namelen != 0);
148                 ASSERT(xfs_dir_ino_validate(mp, INT_GET(dep->inumber, ARCH_CONVERT)) == 0);
149                 ASSERT(INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep), ARCH_CONVERT) ==
150                        (char *)dep - (char *)d);
151                 count++;
152                 lastfree = 0;
153                 if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
154                         addr = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
155                                 (xfs_dir2_data_aoff_t)
156                                 ((char *)dep - (char *)d));
157                         hash = xfs_da_hashname((char *)dep->name, dep->namelen);
158                         for (i = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
159                                 if (INT_GET(lep[i].address, ARCH_CONVERT) == addr &&
160                                     INT_GET(lep[i].hashval, ARCH_CONVERT) == hash)
161                                         break;
162                         }
163                         ASSERT(i < INT_GET(btp->count, ARCH_CONVERT));
164                 }
165                 p += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
166         }
167         /*
168          * Need to have seen all the entries and all the bestfree slots.
169          */
170         ASSERT(freeseen == 7);
171         if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
172                 for (i = stale = 0; i < INT_GET(btp->count, ARCH_CONVERT); i++) {
173                         if (INT_GET(lep[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
174                                 stale++;
175                         if (i > 0)
176                                 ASSERT(INT_GET(lep[i].hashval, ARCH_CONVERT) >= INT_GET(lep[i - 1].hashval, ARCH_CONVERT));
177                 }
178                 ASSERT(count == INT_GET(btp->count, ARCH_CONVERT) - INT_GET(btp->stale, ARCH_CONVERT));
179                 ASSERT(stale == INT_GET(btp->stale, ARCH_CONVERT));
180         }
181 }
182 #endif
183
184 /*
185  * Given a data block and an unused entry from that block,
186  * return the bestfree entry if any that corresponds to it.
187  */
188 xfs_dir2_data_free_t *
189 xfs_dir2_data_freefind(
190         xfs_dir2_data_t         *d,             /* data block */
191         xfs_dir2_data_unused_t  *dup)           /* data unused entry */
192 {
193         xfs_dir2_data_free_t    *dfp;           /* bestfree entry */
194         xfs_dir2_data_aoff_t    off;            /* offset value needed */
195 #if defined(DEBUG) && defined(__KERNEL__)
196         int                     matched;        /* matched the value */
197         int                     seenzero;       /* saw a 0 bestfree entry */
198 #endif
199
200         off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)d);
201 #if defined(DEBUG) && defined(__KERNEL__)
202         /*
203          * Validate some consistency in the bestfree table.
204          * Check order, non-overlapping entries, and if we find the
205          * one we're looking for it has to be exact.
206          */
207         ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
208                INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
209         for (dfp = &d->hdr.bestfree[0], seenzero = matched = 0;
210              dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
211              dfp++) {
212                 if (!dfp->offset) {
213                         ASSERT(!dfp->length);
214                         seenzero = 1;
215                         continue;
216                 }
217                 ASSERT(seenzero == 0);
218                 if (INT_GET(dfp->offset, ARCH_CONVERT) == off) {
219                         matched = 1;
220                         ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(dup->length, ARCH_CONVERT));
221                 } else if (off < INT_GET(dfp->offset, ARCH_CONVERT))
222                         ASSERT(off + INT_GET(dup->length, ARCH_CONVERT) <= INT_GET(dfp->offset, ARCH_CONVERT));
223                 else
224                         ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) + INT_GET(dfp->length, ARCH_CONVERT) <= off);
225                 ASSERT(matched || INT_GET(dfp->length, ARCH_CONVERT) >= INT_GET(dup->length, ARCH_CONVERT));
226                 if (dfp > &d->hdr.bestfree[0])
227                         ASSERT(INT_GET(dfp[-1].length, ARCH_CONVERT) >= INT_GET(dfp[0].length, ARCH_CONVERT));
228         }
229 #endif
230         /*
231          * If this is smaller than the smallest bestfree entry,
232          * it can't be there since they're sorted.
233          */
234         if (INT_GET(dup->length, ARCH_CONVERT) < INT_GET(d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length, ARCH_CONVERT))
235                 return NULL;
236         /*
237          * Look at the three bestfree entries for our guy.
238          */
239         for (dfp = &d->hdr.bestfree[0];
240              dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT];
241              dfp++) {
242                 if (!dfp->offset)
243                         return NULL;
244                 if (INT_GET(dfp->offset, ARCH_CONVERT) == off)
245                         return dfp;
246         }
247         /*
248          * Didn't find it.  This only happens if there are duplicate lengths.
249          */
250         return NULL;
251 }
252
253 /*
254  * Insert an unused-space entry into the bestfree table.
255  */
256 xfs_dir2_data_free_t *                          /* entry inserted */
257 xfs_dir2_data_freeinsert(
258         xfs_dir2_data_t         *d,             /* data block pointer */
259         xfs_dir2_data_unused_t  *dup,           /* unused space */
260         int                     *loghead)       /* log the data header (out) */
261 {
262         xfs_dir2_data_free_t    *dfp;           /* bestfree table pointer */
263         xfs_dir2_data_free_t    new;            /* new bestfree entry */
264
265 #ifdef __KERNEL__
266         ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
267                INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
268 #endif
269         dfp = d->hdr.bestfree;
270         INT_COPY(new.length, dup->length, ARCH_CONVERT);
271         INT_SET(new.offset, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dup - (char *)d));
272         /*
273          * Insert at position 0, 1, or 2; or not at all.
274          */
275         if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[0].length, ARCH_CONVERT)) {
276                 dfp[2] = dfp[1];
277                 dfp[1] = dfp[0];
278                 dfp[0] = new;
279                 *loghead = 1;
280                 return &dfp[0];
281         }
282         if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[1].length, ARCH_CONVERT)) {
283                 dfp[2] = dfp[1];
284                 dfp[1] = new;
285                 *loghead = 1;
286                 return &dfp[1];
287         }
288         if (INT_GET(new.length, ARCH_CONVERT) > INT_GET(dfp[2].length, ARCH_CONVERT)) {
289                 dfp[2] = new;
290                 *loghead = 1;
291                 return &dfp[2];
292         }
293         return NULL;
294 }
295
296 /*
297  * Remove a bestfree entry from the table.
298  */
299 STATIC void
300 xfs_dir2_data_freeremove(
301         xfs_dir2_data_t         *d,             /* data block pointer */
302         xfs_dir2_data_free_t    *dfp,           /* bestfree entry pointer */
303         int                     *loghead)       /* out: log data header */
304 {
305 #ifdef __KERNEL__
306         ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
307                INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
308 #endif
309         /*
310          * It's the first entry, slide the next 2 up.
311          */
312         if (dfp == &d->hdr.bestfree[0]) {
313                 d->hdr.bestfree[0] = d->hdr.bestfree[1];
314                 d->hdr.bestfree[1] = d->hdr.bestfree[2];
315         }
316         /*
317          * It's the second entry, slide the 3rd entry up.
318          */
319         else if (dfp == &d->hdr.bestfree[1])
320                 d->hdr.bestfree[1] = d->hdr.bestfree[2];
321         /*
322          * Must be the last entry.
323          */
324         else
325                 ASSERT(dfp == &d->hdr.bestfree[2]);
326         /*
327          * Clear the 3rd entry, must be zero now.
328          */
329         d->hdr.bestfree[2].length = 0;
330         d->hdr.bestfree[2].offset = 0;
331         *loghead = 1;
332 }
333
334 /*
335  * Given a data block, reconstruct its bestfree map.
336  */
337 void
338 xfs_dir2_data_freescan(
339         xfs_mount_t             *mp,            /* filesystem mount point */
340         xfs_dir2_data_t         *d,             /* data block pointer */
341         int                     *loghead,       /* out: log data header */
342         char                    *aendp)         /* in: caller's endp */
343 {
344         xfs_dir2_block_tail_t   *btp;           /* block tail */
345         xfs_dir2_data_entry_t   *dep;           /* active data entry */
346         xfs_dir2_data_unused_t  *dup;           /* unused data entry */
347         char                    *endp;          /* end of block's data */
348         char                    *p;             /* current entry pointer */
349
350 #ifdef __KERNEL__
351         ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
352                INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
353 #endif
354         /*
355          * Start by clearing the table.
356          */
357         memset(d->hdr.bestfree, 0, sizeof(d->hdr.bestfree));
358         *loghead = 1;
359         /*
360          * Set up pointers.
361          */
362         p = (char *)d->u;
363         if (aendp)
364                 endp = aendp;
365         else if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC) {
366                 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
367                 endp = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
368         } else
369                 endp = (char *)d + mp->m_dirblksize;
370         /*
371          * Loop over the block's entries.
372          */
373         while (p < endp) {
374                 dup = (xfs_dir2_data_unused_t *)p;
375                 /*
376                  * If it's a free entry, insert it.
377                  */
378                 if (INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG) {
379                         ASSERT((char *)dup - (char *)d ==
380                                INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT));
381                         xfs_dir2_data_freeinsert(d, dup, loghead);
382                         p += INT_GET(dup->length, ARCH_CONVERT);
383                 }
384                 /*
385                  * For active entries, check their tags and skip them.
386                  */
387                 else {
388                         dep = (xfs_dir2_data_entry_t *)p;
389                         ASSERT((char *)dep - (char *)d ==
390                                INT_GET(*XFS_DIR2_DATA_ENTRY_TAG_P(dep), ARCH_CONVERT));
391                         p += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
392                 }
393         }
394 }
395
396 /*
397  * Initialize a data block at the given block number in the directory.
398  * Give back the buffer for the created block.
399  */
400 int                                             /* error */
401 xfs_dir2_data_init(
402         xfs_da_args_t           *args,          /* directory operation args */
403         xfs_dir2_db_t           blkno,          /* logical dir block number */
404         xfs_dabuf_t             **bpp)          /* output block buffer */
405 {
406         xfs_dabuf_t             *bp;            /* block buffer */
407         xfs_dir2_data_t         *d;             /* pointer to block */
408         xfs_inode_t             *dp;            /* incore directory inode */
409         xfs_dir2_data_unused_t  *dup;           /* unused entry pointer */
410         int                     error;          /* error return value */
411         int                     i;              /* bestfree index */
412         xfs_mount_t             *mp;            /* filesystem mount point */
413         xfs_trans_t             *tp;            /* transaction pointer */
414         int                     t;              /* temp */
415
416         dp = args->dp;
417         mp = dp->i_mount;
418         tp = args->trans;
419         /*
420          * Get the buffer set up for the block.
421          */
422         error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, blkno), -1, &bp,
423                 XFS_DATA_FORK);
424         if (error) {
425                 return error;
426         }
427         ASSERT(bp != NULL);
428         /*
429          * Initialize the header.
430          */
431         d = bp->data;
432         INT_SET(d->hdr.magic, ARCH_CONVERT, XFS_DIR2_DATA_MAGIC);
433         INT_SET(d->hdr.bestfree[0].offset, ARCH_CONVERT, (xfs_dir2_data_off_t)sizeof(d->hdr));
434         for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
435                 d->hdr.bestfree[i].length = 0;
436                 d->hdr.bestfree[i].offset = 0;
437         }
438         /*
439          * Set up an unused entry for the block's body.
440          */
441         dup = &d->u[0].unused;
442         INT_SET(dup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
443
444         t=mp->m_dirblksize - (uint)sizeof(d->hdr);
445         INT_SET(d->hdr.bestfree[0].length, ARCH_CONVERT, t);
446         INT_SET(dup->length, ARCH_CONVERT, t);
447         INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT,
448                 (xfs_dir2_data_off_t)((char *)dup - (char *)d));
449         /*
450          * Log it and return it.
451          */
452         xfs_dir2_data_log_header(tp, bp);
453         xfs_dir2_data_log_unused(tp, bp, dup);
454         *bpp = bp;
455         return 0;
456 }
457
458 /*
459  * Log an active data entry from the block.
460  */
461 void
462 xfs_dir2_data_log_entry(
463         xfs_trans_t             *tp,            /* transaction pointer */
464         xfs_dabuf_t             *bp,            /* block buffer */
465         xfs_dir2_data_entry_t   *dep)           /* data entry pointer */
466 {
467         xfs_dir2_data_t         *d;             /* data block pointer */
468
469         d = bp->data;
470         ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
471                INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
472         xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)d),
473                 (uint)((char *)(XFS_DIR2_DATA_ENTRY_TAG_P(dep) + 1) -
474                        (char *)d - 1));
475 }
476
477 /*
478  * Log a data block header.
479  */
480 void
481 xfs_dir2_data_log_header(
482         xfs_trans_t             *tp,            /* transaction pointer */
483         xfs_dabuf_t             *bp)            /* block buffer */
484 {
485         xfs_dir2_data_t         *d;             /* data block pointer */
486
487         d = bp->data;
488         ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
489                INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
490         xfs_da_log_buf(tp, bp, (uint)((char *)&d->hdr - (char *)d),
491                 (uint)(sizeof(d->hdr) - 1));
492 }
493
494 /*
495  * Log a data unused entry.
496  */
497 void
498 xfs_dir2_data_log_unused(
499         xfs_trans_t             *tp,            /* transaction pointer */
500         xfs_dabuf_t             *bp,            /* block buffer */
501         xfs_dir2_data_unused_t  *dup)           /* data unused pointer */
502 {
503         xfs_dir2_data_t         *d;             /* data block pointer */
504
505         d = bp->data;
506         ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
507                INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
508         /*
509          * Log the first part of the unused entry.
510          */
511         xfs_da_log_buf(tp, bp, (uint)((char *)dup - (char *)d),
512                 (uint)((char *)&dup->length + sizeof(dup->length) -
513                        1 - (char *)d));
514         /*
515          * Log the end (tag) of the unused entry.
516          */
517         xfs_da_log_buf(tp, bp,
518                 (uint)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup) - (char *)d),
519                 (uint)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup) - (char *)d +
520                        sizeof(xfs_dir2_data_off_t) - 1));
521 }
522
523 /*
524  * Make a byte range in the data block unused.
525  * Its current contents are unimportant.
526  */
527 void
528 xfs_dir2_data_make_free(
529         xfs_trans_t             *tp,            /* transaction pointer */
530         xfs_dabuf_t             *bp,            /* block buffer */
531         xfs_dir2_data_aoff_t    offset,         /* starting byte offset */
532         xfs_dir2_data_aoff_t    len,            /* length in bytes */
533         int                     *needlogp,      /* out: log header */
534         int                     *needscanp)     /* out: regen bestfree */
535 {
536         xfs_dir2_data_t         *d;             /* data block pointer */
537         xfs_dir2_data_free_t    *dfp;           /* bestfree pointer */
538         char                    *endptr;        /* end of data area */
539         xfs_mount_t             *mp;            /* filesystem mount point */
540         int                     needscan;       /* need to regen bestfree */
541         xfs_dir2_data_unused_t  *newdup;        /* new unused entry */
542         xfs_dir2_data_unused_t  *postdup;       /* unused entry after us */
543         xfs_dir2_data_unused_t  *prevdup;       /* unused entry before us */
544
545         mp = tp->t_mountp;
546         d = bp->data;
547         /*
548          * Figure out where the end of the data area is.
549          */
550         if (INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC)
551                 endptr = (char *)d + mp->m_dirblksize;
552         else {
553                 xfs_dir2_block_tail_t   *btp;   /* block tail */
554
555                 ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
556                 btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d);
557                 endptr = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
558         }
559         /*
560          * If this isn't the start of the block, then back up to
561          * the previous entry and see if it's free.
562          */
563         if (offset > sizeof(d->hdr)) {
564                 xfs_dir2_data_off_t     *tagp;  /* tag just before us */
565
566                 tagp = (xfs_dir2_data_off_t *)((char *)d + offset) - 1;
567                 prevdup = (xfs_dir2_data_unused_t *)((char *)d + INT_GET(*tagp, ARCH_CONVERT));
568                 if (INT_GET(prevdup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG)
569                         prevdup = NULL;
570         } else
571                 prevdup = NULL;
572         /*
573          * If this isn't the end of the block, see if the entry after
574          * us is free.
575          */
576         if ((char *)d + offset + len < endptr) {
577                 postdup =
578                         (xfs_dir2_data_unused_t *)((char *)d + offset + len);
579                 if (INT_GET(postdup->freetag, ARCH_CONVERT) != XFS_DIR2_DATA_FREE_TAG)
580                         postdup = NULL;
581         } else
582                 postdup = NULL;
583         ASSERT(*needscanp == 0);
584         needscan = 0;
585         /*
586          * Previous and following entries are both free,
587          * merge everything into a single free entry.
588          */
589         if (prevdup && postdup) {
590                 xfs_dir2_data_free_t    *dfp2;  /* another bestfree pointer */
591
592                 /*
593                  * See if prevdup and/or postdup are in bestfree table.
594                  */
595                 dfp = xfs_dir2_data_freefind(d, prevdup);
596                 dfp2 = xfs_dir2_data_freefind(d, postdup);
597                 /*
598                  * We need a rescan unless there are exactly 2 free entries
599                  * namely our two.  Then we know what's happening, otherwise
600                  * since the third bestfree is there, there might be more
601                  * entries.
602                  */
603                 needscan = d->hdr.bestfree[2].length;
604                 /*
605                  * Fix up the new big freespace.
606                  */
607                 INT_MOD(prevdup->length, ARCH_CONVERT, len + INT_GET(postdup->length, ARCH_CONVERT));
608                 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup), ARCH_CONVERT,
609                         (xfs_dir2_data_off_t)((char *)prevdup - (char *)d));
610                 xfs_dir2_data_log_unused(tp, bp, prevdup);
611                 if (!needscan) {
612                         /*
613                          * Has to be the case that entries 0 and 1 are
614                          * dfp and dfp2 (don't know which is which), and
615                          * entry 2 is empty.
616                          * Remove entry 1 first then entry 0.
617                          */
618                         ASSERT(dfp && dfp2);
619                         if (dfp == &d->hdr.bestfree[1]) {
620                                 dfp = &d->hdr.bestfree[0];
621                                 ASSERT(dfp2 == dfp);
622                                 dfp2 = &d->hdr.bestfree[1];
623                         }
624                         xfs_dir2_data_freeremove(d, dfp2, needlogp);
625                         xfs_dir2_data_freeremove(d, dfp, needlogp);
626                         /*
627                          * Now insert the new entry.
628                          */
629                         dfp = xfs_dir2_data_freeinsert(d, prevdup, needlogp);
630                         ASSERT(dfp == &d->hdr.bestfree[0]);
631                         ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(prevdup->length, ARCH_CONVERT));
632                         ASSERT(!dfp[1].length);
633                         ASSERT(!dfp[2].length);
634                 }
635         }
636         /*
637          * The entry before us is free, merge with it.
638          */
639         else if (prevdup) {
640                 dfp = xfs_dir2_data_freefind(d, prevdup);
641                 INT_MOD(prevdup->length, ARCH_CONVERT, len);
642                 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(prevdup), ARCH_CONVERT,
643                         (xfs_dir2_data_off_t)((char *)prevdup - (char *)d));
644                 xfs_dir2_data_log_unused(tp, bp, prevdup);
645                 /*
646                  * If the previous entry was in the table, the new entry
647                  * is longer, so it will be in the table too.  Remove
648                  * the old one and add the new one.
649                  */
650                 if (dfp) {
651                         xfs_dir2_data_freeremove(d, dfp, needlogp);
652                         (void)xfs_dir2_data_freeinsert(d, prevdup, needlogp);
653                 }
654                 /*
655                  * Otherwise we need a scan if the new entry is big enough.
656                  */
657                 else
658                         needscan = INT_GET(prevdup->length, ARCH_CONVERT) > INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT);
659         }
660         /*
661          * The following entry is free, merge with it.
662          */
663         else if (postdup) {
664                 dfp = xfs_dir2_data_freefind(d, postdup);
665                 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
666                 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
667                 INT_SET(newdup->length, ARCH_CONVERT, len + INT_GET(postdup->length, ARCH_CONVERT));
668                 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
669                         (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
670                 xfs_dir2_data_log_unused(tp, bp, newdup);
671                 /*
672                  * If the following entry was in the table, the new entry
673                  * is longer, so it will be in the table too.  Remove
674                  * the old one and add the new one.
675                  */
676                 if (dfp) {
677                         xfs_dir2_data_freeremove(d, dfp, needlogp);
678                         (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
679                 }
680                 /*
681                  * Otherwise we need a scan if the new entry is big enough.
682                  */
683                 else
684                         needscan = INT_GET(newdup->length, ARCH_CONVERT) > INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT);
685         }
686         /*
687          * Neither neighbor is free.  Make a new entry.
688          */
689         else {
690                 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset);
691                 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
692                 INT_SET(newdup->length, ARCH_CONVERT, len);
693                 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
694                         (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
695                 xfs_dir2_data_log_unused(tp, bp, newdup);
696                 (void)xfs_dir2_data_freeinsert(d, newdup, needlogp);
697         }
698         *needscanp = needscan;
699 }
700
701 /*
702  * Take a byte range out of an existing unused space and make it un-free.
703  */
704 void
705 xfs_dir2_data_use_free(
706         xfs_trans_t             *tp,            /* transaction pointer */
707         xfs_dabuf_t             *bp,            /* data block buffer */
708         xfs_dir2_data_unused_t  *dup,           /* unused entry */
709         xfs_dir2_data_aoff_t    offset,         /* starting offset to use */
710         xfs_dir2_data_aoff_t    len,            /* length to use */
711         int                     *needlogp,      /* out: need to log header */
712         int                     *needscanp)     /* out: need regen bestfree */
713 {
714         xfs_dir2_data_t         *d;             /* data block */
715         xfs_dir2_data_free_t    *dfp;           /* bestfree pointer */
716         int                     matchback;      /* matches end of freespace */
717         int                     matchfront;     /* matches start of freespace */
718         int                     needscan;       /* need to regen bestfree */
719         xfs_dir2_data_unused_t  *newdup;        /* new unused entry */
720         xfs_dir2_data_unused_t  *newdup2;       /* another new unused entry */
721         int                     oldlen;         /* old unused entry's length */
722
723         d = bp->data;
724         ASSERT(INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC ||
725                INT_GET(d->hdr.magic, ARCH_CONVERT) == XFS_DIR2_BLOCK_MAGIC);
726         ASSERT(INT_GET(dup->freetag, ARCH_CONVERT) == XFS_DIR2_DATA_FREE_TAG);
727         ASSERT(offset >= (char *)dup - (char *)d);
728         ASSERT(offset + len <= (char *)dup + INT_GET(dup->length, ARCH_CONVERT) - (char *)d);
729         ASSERT((char *)dup - (char *)d == INT_GET(*XFS_DIR2_DATA_UNUSED_TAG_P(dup), ARCH_CONVERT));
730         /*
731          * Look up the entry in the bestfree table.
732          */
733         dfp = xfs_dir2_data_freefind(d, dup);
734         oldlen = INT_GET(dup->length, ARCH_CONVERT);
735         ASSERT(dfp || oldlen <= INT_GET(d->hdr.bestfree[2].length, ARCH_CONVERT));
736         /*
737          * Check for alignment with front and back of the entry.
738          */
739         matchfront = (char *)dup - (char *)d == offset;
740         matchback = (char *)dup + oldlen - (char *)d == offset + len;
741         ASSERT(*needscanp == 0);
742         needscan = 0;
743         /*
744          * If we matched it exactly we just need to get rid of it from
745          * the bestfree table.
746          */
747         if (matchfront && matchback) {
748                 if (dfp) {
749                         needscan = d->hdr.bestfree[2].offset;
750                         if (!needscan)
751                                 xfs_dir2_data_freeremove(d, dfp, needlogp);
752                 }
753         }
754         /*
755          * We match the first part of the entry.
756          * Make a new entry with the remaining freespace.
757          */
758         else if (matchfront) {
759                 newdup = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
760                 INT_SET(newdup->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
761                 INT_SET(newdup->length, ARCH_CONVERT, oldlen - len);
762                 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
763                         (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
764                 xfs_dir2_data_log_unused(tp, bp, newdup);
765                 /*
766                  * If it was in the table, remove it and add the new one.
767                  */
768                 if (dfp) {
769                         xfs_dir2_data_freeremove(d, dfp, needlogp);
770                         dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
771                         ASSERT(dfp != NULL);
772                         ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(newdup->length, ARCH_CONVERT));
773                         ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) == (char *)newdup - (char *)d);
774                         /*
775                          * If we got inserted at the last slot,
776                          * that means we don't know if there was a better
777                          * choice for the last slot, or not.  Rescan.
778                          */
779                         needscan = dfp == &d->hdr.bestfree[2];
780                 }
781         }
782         /*
783          * We match the last part of the entry.
784          * Trim the allocated space off the tail of the entry.
785          */
786         else if (matchback) {
787                 newdup = dup;
788                 INT_SET(newdup->length, ARCH_CONVERT, (xfs_dir2_data_off_t)
789                         (((char *)d + offset) - (char *)newdup));
790                 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
791                         (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
792                 xfs_dir2_data_log_unused(tp, bp, newdup);
793                 /*
794                  * If it was in the table, remove it and add the new one.
795                  */
796                 if (dfp) {
797                         xfs_dir2_data_freeremove(d, dfp, needlogp);
798                         dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp);
799                         ASSERT(dfp != NULL);
800                         ASSERT(INT_GET(dfp->length, ARCH_CONVERT) == INT_GET(newdup->length, ARCH_CONVERT));
801                         ASSERT(INT_GET(dfp->offset, ARCH_CONVERT) == (char *)newdup - (char *)d);
802                         /*
803                          * If we got inserted at the last slot,
804                          * that means we don't know if there was a better
805                          * choice for the last slot, or not.  Rescan.
806                          */
807                         needscan = dfp == &d->hdr.bestfree[2];
808                 }
809         }
810         /*
811          * Poking out the middle of an entry.
812          * Make two new entries.
813          */
814         else {
815                 newdup = dup;
816                 INT_SET(newdup->length, ARCH_CONVERT, (xfs_dir2_data_off_t)
817                         (((char *)d + offset) - (char *)newdup));
818                 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup), ARCH_CONVERT,
819                         (xfs_dir2_data_off_t)((char *)newdup - (char *)d));
820                 xfs_dir2_data_log_unused(tp, bp, newdup);
821                 newdup2 = (xfs_dir2_data_unused_t *)((char *)d + offset + len);
822                 INT_SET(newdup2->freetag, ARCH_CONVERT, XFS_DIR2_DATA_FREE_TAG);
823                 INT_SET(newdup2->length, ARCH_CONVERT, oldlen - len - INT_GET(newdup->length, ARCH_CONVERT));
824                 INT_SET(*XFS_DIR2_DATA_UNUSED_TAG_P(newdup2), ARCH_CONVERT,
825                         (xfs_dir2_data_off_t)((char *)newdup2 - (char *)d));
826                 xfs_dir2_data_log_unused(tp, bp, newdup2);
827                 /*
828                  * If the old entry was in the table, we need to scan
829                  * if the 3rd entry was valid, since these entries
830                  * are smaller than the old one.
831                  * If we don't need to scan that means there were 1 or 2
832                  * entries in the table, and removing the old and adding
833                  * the 2 new will work.
834                  */
835                 if (dfp) {
836                         needscan = d->hdr.bestfree[2].length;
837                         if (!needscan) {
838                                 xfs_dir2_data_freeremove(d, dfp, needlogp);
839                                 (void)xfs_dir2_data_freeinsert(d, newdup,
840                                         needlogp);
841                                 (void)xfs_dir2_data_freeinsert(d, newdup2,
842                                         needlogp);
843                         }
844                 }
845         }
846         *needscanp = needscan;
847 }