Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | #ifndef __XFS_BMAP_H__ |
| 19 | #define __XFS_BMAP_H__ |
| 20 | |
| 21 | struct getbmap; |
| 22 | struct xfs_bmbt_irec; |
| 23 | struct xfs_inode; |
| 24 | struct xfs_mount; |
| 25 | struct xfs_trans; |
| 26 | |
| 27 | /* |
| 28 | * List of extents to be free "later". |
| 29 | * The list is kept sorted on xbf_startblock. |
| 30 | */ |
| 31 | typedef struct xfs_bmap_free_item |
| 32 | { |
| 33 | xfs_fsblock_t xbfi_startblock;/* starting fs block number */ |
| 34 | xfs_extlen_t xbfi_blockcount;/* number of blocks in extent */ |
| 35 | struct xfs_bmap_free_item *xbfi_next; /* link to next entry */ |
| 36 | } xfs_bmap_free_item_t; |
| 37 | |
| 38 | /* |
| 39 | * Header for free extent list. |
| 40 | */ |
| 41 | typedef struct xfs_bmap_free |
| 42 | { |
| 43 | xfs_bmap_free_item_t *xbf_first; /* list of to-be-free extents */ |
| 44 | int xbf_count; /* count of items on list */ |
| 45 | int xbf_low; /* kludge: alloc in low mode */ |
| 46 | } xfs_bmap_free_t; |
| 47 | |
| 48 | #define XFS_BMAP_MAX_NMAP 4 |
| 49 | |
| 50 | /* |
| 51 | * Flags for xfs_bmapi |
| 52 | */ |
| 53 | #define XFS_BMAPI_WRITE 0x001 /* write operation: allocate space */ |
| 54 | #define XFS_BMAPI_DELAY 0x002 /* delayed write operation */ |
| 55 | #define XFS_BMAPI_ENTIRE 0x004 /* return entire extent, not trimmed */ |
| 56 | #define XFS_BMAPI_METADATA 0x008 /* mapping metadata not user data */ |
| 57 | #define XFS_BMAPI_EXACT 0x010 /* allocate only to spec'd bounds */ |
| 58 | #define XFS_BMAPI_ATTRFORK 0x020 /* use attribute fork not data */ |
| 59 | #define XFS_BMAPI_ASYNC 0x040 /* bunmapi xactions can be async */ |
| 60 | #define XFS_BMAPI_RSVBLOCKS 0x080 /* OK to alloc. reserved data blocks */ |
| 61 | #define XFS_BMAPI_PREALLOC 0x100 /* preallocation op: unwritten space */ |
| 62 | #define XFS_BMAPI_IGSTATE 0x200 /* Ignore state - */ |
| 63 | /* combine contig. space */ |
| 64 | #define XFS_BMAPI_CONTIG 0x400 /* must allocate only one extent */ |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #define XFS_BMAPI_AFLAG(w) xfs_bmapi_aflag(w) |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 67 | static inline int xfs_bmapi_aflag(int w) |
| 68 | { |
| 69 | return (w == XFS_ATTR_FORK ? XFS_BMAPI_ATTRFORK : 0); |
| 70 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * Special values for xfs_bmbt_irec_t br_startblock field. |
| 74 | */ |
| 75 | #define DELAYSTARTBLOCK ((xfs_fsblock_t)-1LL) |
| 76 | #define HOLESTARTBLOCK ((xfs_fsblock_t)-2LL) |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #define XFS_BMAP_INIT(flp,fbp) xfs_bmap_init(flp,fbp) |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 79 | static inline void xfs_bmap_init(xfs_bmap_free_t *flp, xfs_fsblock_t *fbp) |
| 80 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | ((flp)->xbf_first = NULL, (flp)->xbf_count = 0, \ |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 82 | (flp)->xbf_low = 0, *(fbp) = NULLFSBLOCK); |
| 83 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * Argument structure for xfs_bmap_alloc. |
| 87 | */ |
| 88 | typedef struct xfs_bmalloca { |
| 89 | xfs_fsblock_t firstblock; /* i/o first block allocated */ |
| 90 | xfs_fsblock_t rval; /* starting block of new extent */ |
| 91 | xfs_fileoff_t off; /* offset in file filling in */ |
| 92 | struct xfs_trans *tp; /* transaction pointer */ |
| 93 | struct xfs_inode *ip; /* incore inode pointer */ |
| 94 | struct xfs_bmbt_irec *prevp; /* extent before the new one */ |
| 95 | struct xfs_bmbt_irec *gotp; /* extent after, or delayed */ |
| 96 | xfs_extlen_t alen; /* i/o length asked/allocated */ |
| 97 | xfs_extlen_t total; /* total blocks needed for xaction */ |
| 98 | xfs_extlen_t minlen; /* mininum allocation size (blocks) */ |
| 99 | xfs_extlen_t minleft; /* amount must be left after alloc */ |
| 100 | char eof; /* set if allocating past last extent */ |
| 101 | char wasdel; /* replacing a delayed allocation */ |
| 102 | char userdata;/* set if is user data */ |
| 103 | char low; /* low on space, using seq'l ags */ |
| 104 | char aeof; /* allocated space at eof */ |
| 105 | } xfs_bmalloca_t; |
| 106 | |
| 107 | #ifdef __KERNEL__ |
| 108 | |
| 109 | #if defined(XFS_BMAP_TRACE) |
| 110 | /* |
| 111 | * Trace operations for bmap extent tracing |
| 112 | */ |
| 113 | #define XFS_BMAP_KTRACE_DELETE 1 |
| 114 | #define XFS_BMAP_KTRACE_INSERT 2 |
| 115 | #define XFS_BMAP_KTRACE_PRE_UP 3 |
| 116 | #define XFS_BMAP_KTRACE_POST_UP 4 |
| 117 | |
| 118 | #define XFS_BMAP_TRACE_SIZE 4096 /* size of global trace buffer */ |
| 119 | #define XFS_BMAP_KTRACE_SIZE 32 /* size of per-inode trace buffer */ |
| 120 | extern ktrace_t *xfs_bmap_trace_buf; |
| 121 | |
| 122 | /* |
| 123 | * Add bmap trace insert entries for all the contents of the extent list. |
| 124 | */ |
| 125 | void |
| 126 | xfs_bmap_trace_exlist( |
| 127 | char *fname, /* function name */ |
| 128 | struct xfs_inode *ip, /* incore inode pointer */ |
| 129 | xfs_extnum_t cnt, /* count of entries in list */ |
| 130 | int whichfork); /* data or attr fork */ |
| 131 | #else |
| 132 | #define xfs_bmap_trace_exlist(f,ip,c,w) |
| 133 | #endif |
| 134 | |
| 135 | /* |
| 136 | * Convert inode from non-attributed to attributed. |
| 137 | * Must not be in a transaction, ip must not be locked. |
| 138 | */ |
| 139 | int /* error code */ |
| 140 | xfs_bmap_add_attrfork( |
| 141 | struct xfs_inode *ip, /* incore inode pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 142 | int size, /* space needed for new attribute */ |
| 143 | int rsvd); /* flag for reserved block allocation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Add the extent to the list of extents to be free at transaction end. |
| 147 | * The list is maintained sorted (by block number). |
| 148 | */ |
| 149 | void |
| 150 | xfs_bmap_add_free( |
| 151 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 152 | xfs_filblks_t len, /* length of extent */ |
| 153 | xfs_bmap_free_t *flist, /* list of extents */ |
| 154 | struct xfs_mount *mp); /* mount point structure */ |
| 155 | |
| 156 | /* |
| 157 | * Routine to clean up the free list data structure when |
| 158 | * an error occurs during a transaction. |
| 159 | */ |
| 160 | void |
| 161 | xfs_bmap_cancel( |
| 162 | xfs_bmap_free_t *flist); /* free list to clean up */ |
| 163 | |
| 164 | /* |
| 165 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 166 | * in this filesystem. Done once, during mount. |
| 167 | */ |
| 168 | void |
| 169 | xfs_bmap_compute_maxlevels( |
| 170 | struct xfs_mount *mp, /* file system mount structure */ |
| 171 | int whichfork); /* data or attr fork */ |
| 172 | |
| 173 | /* |
| 174 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 175 | * caller. Frees all the extents that need freeing, which must be done |
| 176 | * last due to locking considerations. |
| 177 | * |
| 178 | * Return 1 if the given transaction was committed and a new one allocated, |
| 179 | * and 0 otherwise. |
| 180 | */ |
| 181 | int /* error */ |
| 182 | xfs_bmap_finish( |
| 183 | struct xfs_trans **tp, /* transaction pointer addr */ |
| 184 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 185 | xfs_fsblock_t firstblock, /* controlled a.g. for allocs */ |
| 186 | int *committed); /* xact committed or not */ |
| 187 | |
| 188 | /* |
| 189 | * Returns the file-relative block number of the first unused block in the file. |
| 190 | * This is the lowest-address hole if the file has holes, else the first block |
| 191 | * past the end of file. |
| 192 | */ |
| 193 | int /* error */ |
| 194 | xfs_bmap_first_unused( |
| 195 | struct xfs_trans *tp, /* transaction pointer */ |
| 196 | struct xfs_inode *ip, /* incore inode */ |
| 197 | xfs_extlen_t len, /* size of hole to find */ |
| 198 | xfs_fileoff_t *unused, /* unused block num */ |
| 199 | int whichfork); /* data or attr fork */ |
| 200 | |
| 201 | /* |
| 202 | * Returns the file-relative block number of the last block + 1 before |
| 203 | * last_block (input value) in the file. |
| 204 | * This is not based on i_size, it is based on the extent list. |
| 205 | * Returns 0 for local files, as they do not have an extent list. |
| 206 | */ |
| 207 | int /* error */ |
| 208 | xfs_bmap_last_before( |
| 209 | struct xfs_trans *tp, /* transaction pointer */ |
| 210 | struct xfs_inode *ip, /* incore inode */ |
| 211 | xfs_fileoff_t *last_block, /* last block */ |
| 212 | int whichfork); /* data or attr fork */ |
| 213 | |
| 214 | /* |
| 215 | * Returns the file-relative block number of the first block past eof in |
| 216 | * the file. This is not based on i_size, it is based on the extent list. |
| 217 | * Returns 0 for local files, as they do not have an extent list. |
| 218 | */ |
| 219 | int /* error */ |
| 220 | xfs_bmap_last_offset( |
| 221 | struct xfs_trans *tp, /* transaction pointer */ |
| 222 | struct xfs_inode *ip, /* incore inode */ |
| 223 | xfs_fileoff_t *unused, /* last block num */ |
| 224 | int whichfork); /* data or attr fork */ |
| 225 | |
| 226 | /* |
| 227 | * Returns whether the selected fork of the inode has exactly one |
| 228 | * block or not. For the data fork we check this matches di_size, |
| 229 | * implying the file's range is 0..bsize-1. |
| 230 | */ |
| 231 | int |
| 232 | xfs_bmap_one_block( |
| 233 | struct xfs_inode *ip, /* incore inode */ |
| 234 | int whichfork); /* data or attr fork */ |
| 235 | |
| 236 | /* |
| 237 | * Read in the extents to iu_extents. |
| 238 | * All inode fields are set up by caller, we just traverse the btree |
| 239 | * and copy the records in. |
| 240 | */ |
| 241 | int /* error */ |
| 242 | xfs_bmap_read_extents( |
| 243 | struct xfs_trans *tp, /* transaction pointer */ |
| 244 | struct xfs_inode *ip, /* incore inode */ |
| 245 | int whichfork); /* data or attr fork */ |
| 246 | |
| 247 | /* |
| 248 | * Map file blocks to filesystem blocks. |
| 249 | * File range is given by the bno/len pair. |
| 250 | * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set) |
| 251 | * into a hole or past eof. |
| 252 | * Only allocates blocks from a single allocation group, |
| 253 | * to avoid locking problems. |
| 254 | * The returned value in "firstblock" from the first call in a transaction |
| 255 | * must be remembered and presented to subsequent calls in "firstblock". |
| 256 | * An upper bound for the number of blocks to be allocated is supplied to |
| 257 | * the first call in "total"; if no allocation group has that many free |
| 258 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 259 | */ |
| 260 | int /* error */ |
| 261 | xfs_bmapi( |
| 262 | struct xfs_trans *tp, /* transaction pointer */ |
| 263 | struct xfs_inode *ip, /* incore inode */ |
| 264 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 265 | xfs_filblks_t len, /* length to map in file */ |
| 266 | int flags, /* XFS_BMAPI_... */ |
| 267 | xfs_fsblock_t *firstblock, /* first allocated block |
| 268 | controls a.g. for allocs */ |
| 269 | xfs_extlen_t total, /* total blocks needed */ |
| 270 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 271 | int *nmap, /* i/o: mval size/count */ |
| 272 | xfs_bmap_free_t *flist); /* i/o: list extents to free */ |
| 273 | |
| 274 | /* |
| 275 | * Map file blocks to filesystem blocks, simple version. |
| 276 | * One block only, read-only. |
| 277 | * For flags, only the XFS_BMAPI_ATTRFORK flag is examined. |
| 278 | * For the other flag values, the effect is as if XFS_BMAPI_METADATA |
| 279 | * was set and all the others were clear. |
| 280 | */ |
| 281 | int /* error */ |
| 282 | xfs_bmapi_single( |
| 283 | struct xfs_trans *tp, /* transaction pointer */ |
| 284 | struct xfs_inode *ip, /* incore inode */ |
| 285 | int whichfork, /* data or attr fork */ |
| 286 | xfs_fsblock_t *fsb, /* output: mapped block */ |
| 287 | xfs_fileoff_t bno); /* starting file offs. mapped */ |
| 288 | |
| 289 | /* |
| 290 | * Unmap (remove) blocks from a file. |
| 291 | * If nexts is nonzero then the number of extents to remove is limited to |
| 292 | * that value. If not all extents in the block range can be removed then |
| 293 | * *done is set. |
| 294 | */ |
| 295 | int /* error */ |
| 296 | xfs_bunmapi( |
| 297 | struct xfs_trans *tp, /* transaction pointer */ |
| 298 | struct xfs_inode *ip, /* incore inode */ |
| 299 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 300 | xfs_filblks_t len, /* length to unmap in file */ |
| 301 | int flags, /* XFS_BMAPI_... */ |
| 302 | xfs_extnum_t nexts, /* number of extents max */ |
| 303 | xfs_fsblock_t *firstblock, /* first allocated block |
| 304 | controls a.g. for allocs */ |
| 305 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 306 | int *done); /* set if not done yet */ |
| 307 | |
| 308 | /* |
| 309 | * Fcntl interface to xfs_bmapi. |
| 310 | */ |
| 311 | int /* error code */ |
| 312 | xfs_getbmap( |
| 313 | bhv_desc_t *bdp, /* XFS behavior descriptor*/ |
| 314 | struct getbmap *bmv, /* user bmap structure */ |
| 315 | void __user *ap, /* pointer to user's array */ |
| 316 | int iflags); /* interface flags */ |
| 317 | |
| 318 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 320 | * the allocation to a stripe unit boundary |
| 321 | */ |
| 322 | int |
| 323 | xfs_bmap_eof( |
| 324 | struct xfs_inode *ip, |
| 325 | xfs_fileoff_t endoff, |
| 326 | int whichfork, |
| 327 | int *eof); |
| 328 | |
| 329 | /* |
| 330 | * Count fsblocks of the given fork. |
| 331 | */ |
| 332 | int |
| 333 | xfs_bmap_count_blocks( |
| 334 | xfs_trans_t *tp, |
| 335 | struct xfs_inode *ip, |
| 336 | int whichfork, |
| 337 | int *count); |
| 338 | |
| 339 | /* |
| 340 | * Check an extent list, which has just been read, for |
| 341 | * any bit in the extent flag field. |
| 342 | */ |
| 343 | int |
| 344 | xfs_check_nostate_extents( |
| 345 | xfs_bmbt_rec_t *ep, |
| 346 | xfs_extnum_t num); |
| 347 | |
| 348 | #endif /* __KERNEL__ */ |
| 349 | |
| 350 | #endif /* __XFS_BMAP_H__ */ |