xfs_bmap.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  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
  7. * published by the Free Software Foundation.
  8. *
  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.
  13. *
  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
  17. */
  18. #ifndef __XFS_BMAP_H__
  19. #define __XFS_BMAP_H__
  20. struct getbmap;
  21. struct xfs_bmbt_irec;
  22. struct xfs_ifork;
  23. struct xfs_inode;
  24. struct xfs_mount;
  25. struct xfs_trans;
  26. extern kmem_zone_t *xfs_bmap_free_item_zone;
  27. /*
  28. * DELTA: describe a change to the in-core extent list.
  29. *
  30. * Internally the use of xed_blockount is somewhat funky.
  31. * xed_blockcount contains an offset much of the time because this
  32. * makes merging changes easier. (xfs_fileoff_t and xfs_filblks_t are
  33. * the same underlying type).
  34. */
  35. typedef struct xfs_extdelta
  36. {
  37. xfs_fileoff_t xed_startoff; /* offset of range */
  38. xfs_filblks_t xed_blockcount; /* blocks in range */
  39. } xfs_extdelta_t;
  40. /*
  41. * List of extents to be free "later".
  42. * The list is kept sorted on xbf_startblock.
  43. */
  44. typedef struct xfs_bmap_free_item
  45. {
  46. xfs_fsblock_t xbfi_startblock;/* starting fs block number */
  47. xfs_extlen_t xbfi_blockcount;/* number of blocks in extent */
  48. struct xfs_bmap_free_item *xbfi_next; /* link to next entry */
  49. } xfs_bmap_free_item_t;
  50. /*
  51. * Header for free extent list.
  52. *
  53. * xbf_low is used by the allocator to activate the lowspace algorithm -
  54. * when free space is running low the extent allocator may choose to
  55. * allocate an extent from an AG without leaving sufficient space for
  56. * a btree split when inserting the new extent. In this case the allocator
  57. * will enable the lowspace algorithm which is supposed to allow further
  58. * allocations (such as btree splits and newroots) to allocate from
  59. * sequential AGs. In order to avoid locking AGs out of order the lowspace
  60. * algorithm will start searching for free space from AG 0. If the correct
  61. * transaction reservations have been made then this algorithm will eventually
  62. * find all the space it needs.
  63. */
  64. typedef struct xfs_bmap_free
  65. {
  66. xfs_bmap_free_item_t *xbf_first; /* list of to-be-free extents */
  67. int xbf_count; /* count of items on list */
  68. int xbf_low; /* alloc in low mode */
  69. } xfs_bmap_free_t;
  70. #define XFS_BMAP_MAX_NMAP 4
  71. /*
  72. * Flags for xfs_bmapi
  73. */
  74. #define XFS_BMAPI_WRITE 0x001 /* write operation: allocate space */
  75. #define XFS_BMAPI_DELAY 0x002 /* delayed write operation */
  76. #define XFS_BMAPI_ENTIRE 0x004 /* return entire extent, not trimmed */
  77. #define XFS_BMAPI_METADATA 0x008 /* mapping metadata not user data */
  78. #define XFS_BMAPI_ATTRFORK 0x010 /* use attribute fork not data */
  79. #define XFS_BMAPI_RSVBLOCKS 0x020 /* OK to alloc. reserved data blocks */
  80. #define XFS_BMAPI_PREALLOC 0x040 /* preallocation op: unwritten space */
  81. #define XFS_BMAPI_IGSTATE 0x080 /* Ignore state - */
  82. /* combine contig. space */
  83. #define XFS_BMAPI_CONTIG 0x100 /* must allocate only one extent */
  84. #define XFS_BMAPI_CONVERT 0x200 /* unwritten extent conversion - */
  85. /* need write cache flushing and no */
  86. /* additional allocation alignments */
  87. #define XFS_BMAPI_FLAGS \
  88. { XFS_BMAPI_WRITE, "WRITE" }, \
  89. { XFS_BMAPI_DELAY, "DELAY" }, \
  90. { XFS_BMAPI_ENTIRE, "ENTIRE" }, \
  91. { XFS_BMAPI_METADATA, "METADATA" }, \
  92. { XFS_BMAPI_ATTRFORK, "ATTRFORK" }, \
  93. { XFS_BMAPI_RSVBLOCKS, "RSVBLOCKS" }, \
  94. { XFS_BMAPI_PREALLOC, "PREALLOC" }, \
  95. { XFS_BMAPI_IGSTATE, "IGSTATE" }, \
  96. { XFS_BMAPI_CONTIG, "CONTIG" }, \
  97. { XFS_BMAPI_CONVERT, "CONVERT" }
  98. static inline int xfs_bmapi_aflag(int w)
  99. {
  100. return (w == XFS_ATTR_FORK ? XFS_BMAPI_ATTRFORK : 0);
  101. }
  102. /*
  103. * Special values for xfs_bmbt_irec_t br_startblock field.
  104. */
  105. #define DELAYSTARTBLOCK ((xfs_fsblock_t)-1LL)
  106. #define HOLESTARTBLOCK ((xfs_fsblock_t)-2LL)
  107. static inline void xfs_bmap_init(xfs_bmap_free_t *flp, xfs_fsblock_t *fbp)
  108. {
  109. ((flp)->xbf_first = NULL, (flp)->xbf_count = 0, \
  110. (flp)->xbf_low = 0, *(fbp) = NULLFSBLOCK);
  111. }
  112. /*
  113. * Argument structure for xfs_bmap_alloc.
  114. */
  115. typedef struct xfs_bmalloca {
  116. xfs_fsblock_t firstblock; /* i/o first block allocated */
  117. xfs_fsblock_t rval; /* starting block of new extent */
  118. xfs_fileoff_t off; /* offset in file filling in */
  119. struct xfs_trans *tp; /* transaction pointer */
  120. struct xfs_inode *ip; /* incore inode pointer */
  121. struct xfs_bmbt_irec *prevp; /* extent before the new one */
  122. struct xfs_bmbt_irec *gotp; /* extent after, or delayed */
  123. xfs_extlen_t alen; /* i/o length asked/allocated */
  124. xfs_extlen_t total; /* total blocks needed for xaction */
  125. xfs_extlen_t minlen; /* minimum allocation size (blocks) */
  126. xfs_extlen_t minleft; /* amount must be left after alloc */
  127. char eof; /* set if allocating past last extent */
  128. char wasdel; /* replacing a delayed allocation */
  129. char userdata;/* set if is user data */
  130. char low; /* low on space, using seq'l ags */
  131. char aeof; /* allocated space at eof */
  132. char conv; /* overwriting unwritten extents */
  133. } xfs_bmalloca_t;
  134. /*
  135. * Flags for xfs_bmap_add_extent*.
  136. */
  137. #define BMAP_LEFT_CONTIG (1 << 0)
  138. #define BMAP_RIGHT_CONTIG (1 << 1)
  139. #define BMAP_LEFT_FILLING (1 << 2)
  140. #define BMAP_RIGHT_FILLING (1 << 3)
  141. #define BMAP_LEFT_DELAY (1 << 4)
  142. #define BMAP_RIGHT_DELAY (1 << 5)
  143. #define BMAP_LEFT_VALID (1 << 6)
  144. #define BMAP_RIGHT_VALID (1 << 7)
  145. #define BMAP_ATTRFORK (1 << 8)
  146. #define XFS_BMAP_EXT_FLAGS \
  147. { BMAP_LEFT_CONTIG, "LC" }, \
  148. { BMAP_RIGHT_CONTIG, "RC" }, \
  149. { BMAP_LEFT_FILLING, "LF" }, \
  150. { BMAP_RIGHT_FILLING, "RF" }, \
  151. { BMAP_ATTRFORK, "ATTR" }
  152. /*
  153. * Add bmap trace insert entries for all the contents of the extent list.
  154. *
  155. * Quite excessive tracing. Only do this for debug builds.
  156. */
  157. #if defined(__KERNEL) && defined(DEBUG)
  158. void
  159. xfs_bmap_trace_exlist(
  160. struct xfs_inode *ip, /* incore inode pointer */
  161. xfs_extnum_t cnt, /* count of entries in list */
  162. int whichfork,
  163. unsigned long caller_ip); /* data or attr fork */
  164. #define XFS_BMAP_TRACE_EXLIST(ip,c,w) \
  165. xfs_bmap_trace_exlist(ip,c,w, _THIS_IP_)
  166. #else
  167. #define XFS_BMAP_TRACE_EXLIST(ip,c,w)
  168. #endif
  169. /*
  170. * Convert inode from non-attributed to attributed.
  171. * Must not be in a transaction, ip must not be locked.
  172. */
  173. int /* error code */
  174. xfs_bmap_add_attrfork(
  175. struct xfs_inode *ip, /* incore inode pointer */
  176. int size, /* space needed for new attribute */
  177. int rsvd); /* flag for reserved block allocation */
  178. /*
  179. * Add the extent to the list of extents to be free at transaction end.
  180. * The list is maintained sorted (by block number).
  181. */
  182. void
  183. xfs_bmap_add_free(
  184. xfs_fsblock_t bno, /* fs block number of extent */
  185. xfs_filblks_t len, /* length of extent */
  186. xfs_bmap_free_t *flist, /* list of extents */
  187. struct xfs_mount *mp); /* mount point structure */
  188. /*
  189. * Routine to clean up the free list data structure when
  190. * an error occurs during a transaction.
  191. */
  192. void
  193. xfs_bmap_cancel(
  194. xfs_bmap_free_t *flist); /* free list to clean up */
  195. /*
  196. * Compute and fill in the value of the maximum depth of a bmap btree
  197. * in this filesystem. Done once, during mount.
  198. */
  199. void
  200. xfs_bmap_compute_maxlevels(
  201. struct xfs_mount *mp, /* file system mount structure */
  202. int whichfork); /* data or attr fork */
  203. /*
  204. * Returns the file-relative block number of the first unused block in the file.
  205. * This is the lowest-address hole if the file has holes, else the first block
  206. * past the end of file.
  207. */
  208. int /* error */
  209. xfs_bmap_first_unused(
  210. struct xfs_trans *tp, /* transaction pointer */
  211. struct xfs_inode *ip, /* incore inode */
  212. xfs_extlen_t len, /* size of hole to find */
  213. xfs_fileoff_t *unused, /* unused block num */
  214. int whichfork); /* data or attr fork */
  215. /*
  216. * Returns the file-relative block number of the last block + 1 before
  217. * last_block (input value) in the file.
  218. * This is not based on i_size, it is based on the extent list.
  219. * Returns 0 for local files, as they do not have an extent list.
  220. */
  221. int /* error */
  222. xfs_bmap_last_before(
  223. struct xfs_trans *tp, /* transaction pointer */
  224. struct xfs_inode *ip, /* incore inode */
  225. xfs_fileoff_t *last_block, /* last block */
  226. int whichfork); /* data or attr fork */
  227. /*
  228. * Returns the file-relative block number of the first block past eof in
  229. * the file. This is not based on i_size, it is based on the extent list.
  230. * Returns 0 for local files, as they do not have an extent list.
  231. */
  232. int /* error */
  233. xfs_bmap_last_offset(
  234. struct xfs_trans *tp, /* transaction pointer */
  235. struct xfs_inode *ip, /* incore inode */
  236. xfs_fileoff_t *unused, /* last block num */
  237. int whichfork); /* data or attr fork */
  238. /*
  239. * Returns whether the selected fork of the inode has exactly one
  240. * block or not. For the data fork we check this matches di_size,
  241. * implying the file's range is 0..bsize-1.
  242. */
  243. int
  244. xfs_bmap_one_block(
  245. struct xfs_inode *ip, /* incore inode */
  246. int whichfork); /* data or attr fork */
  247. /*
  248. * Read in the extents to iu_extents.
  249. * All inode fields are set up by caller, we just traverse the btree
  250. * and copy the records in.
  251. */
  252. int /* error */
  253. xfs_bmap_read_extents(
  254. struct xfs_trans *tp, /* transaction pointer */
  255. struct xfs_inode *ip, /* incore inode */
  256. int whichfork); /* data or attr fork */
  257. /*
  258. * Map file blocks to filesystem blocks.
  259. * File range is given by the bno/len pair.
  260. * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
  261. * into a hole or past eof.
  262. * Only allocates blocks from a single allocation group,
  263. * to avoid locking problems.
  264. * The returned value in "firstblock" from the first call in a transaction
  265. * must be remembered and presented to subsequent calls in "firstblock".
  266. * An upper bound for the number of blocks to be allocated is supplied to
  267. * the first call in "total"; if no allocation group has that many free
  268. * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
  269. */
  270. int /* error */
  271. xfs_bmapi(
  272. struct xfs_trans *tp, /* transaction pointer */
  273. struct xfs_inode *ip, /* incore inode */
  274. xfs_fileoff_t bno, /* starting file offs. mapped */
  275. xfs_filblks_t len, /* length to map in file */
  276. int flags, /* XFS_BMAPI_... */
  277. xfs_fsblock_t *firstblock, /* first allocated block
  278. controls a.g. for allocs */
  279. xfs_extlen_t total, /* total blocks needed */
  280. struct xfs_bmbt_irec *mval, /* output: map values */
  281. int *nmap, /* i/o: mval size/count */
  282. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  283. xfs_extdelta_t *delta); /* o: change made to incore
  284. extents */
  285. /*
  286. * Map file blocks to filesystem blocks, simple version.
  287. * One block only, read-only.
  288. * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
  289. * For the other flag values, the effect is as if XFS_BMAPI_METADATA
  290. * was set and all the others were clear.
  291. */
  292. int /* error */
  293. xfs_bmapi_single(
  294. struct xfs_trans *tp, /* transaction pointer */
  295. struct xfs_inode *ip, /* incore inode */
  296. int whichfork, /* data or attr fork */
  297. xfs_fsblock_t *fsb, /* output: mapped block */
  298. xfs_fileoff_t bno); /* starting file offs. mapped */
  299. /*
  300. * Unmap (remove) blocks from a file.
  301. * If nexts is nonzero then the number of extents to remove is limited to
  302. * that value. If not all extents in the block range can be removed then
  303. * *done is set.
  304. */
  305. int /* error */
  306. xfs_bunmapi(
  307. struct xfs_trans *tp, /* transaction pointer */
  308. struct xfs_inode *ip, /* incore inode */
  309. xfs_fileoff_t bno, /* starting offset to unmap */
  310. xfs_filblks_t len, /* length to unmap in file */
  311. int flags, /* XFS_BMAPI_... */
  312. xfs_extnum_t nexts, /* number of extents max */
  313. xfs_fsblock_t *firstblock, /* first allocated block
  314. controls a.g. for allocs */
  315. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  316. xfs_extdelta_t *delta, /* o: change made to incore
  317. extents */
  318. int *done); /* set if not done yet */
  319. /*
  320. * Check an extent list, which has just been read, for
  321. * any bit in the extent flag field.
  322. */
  323. int
  324. xfs_check_nostate_extents(
  325. struct xfs_ifork *ifp,
  326. xfs_extnum_t idx,
  327. xfs_extnum_t num);
  328. uint
  329. xfs_default_attroffset(
  330. struct xfs_inode *ip);
  331. #ifdef __KERNEL__
  332. /*
  333. * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
  334. * caller. Frees all the extents that need freeing, which must be done
  335. * last due to locking considerations.
  336. *
  337. * Return 1 if the given transaction was committed and a new one allocated,
  338. * and 0 otherwise.
  339. */
  340. int /* error */
  341. xfs_bmap_finish(
  342. struct xfs_trans **tp, /* transaction pointer addr */
  343. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  344. int *committed); /* xact committed or not */
  345. /* bmap to userspace formatter - copy to user & advance pointer */
  346. typedef int (*xfs_bmap_format_t)(void **, struct getbmapx *, int *);
  347. /*
  348. * Get inode's extents as described in bmv, and format for output.
  349. */
  350. int /* error code */
  351. xfs_getbmap(
  352. xfs_inode_t *ip,
  353. struct getbmapx *bmv, /* user bmap structure */
  354. xfs_bmap_format_t formatter, /* format to user */
  355. void *arg); /* formatter arg */
  356. /*
  357. * Check if the endoff is outside the last extent. If so the caller will grow
  358. * the allocation to a stripe unit boundary
  359. */
  360. int
  361. xfs_bmap_eof(
  362. struct xfs_inode *ip,
  363. xfs_fileoff_t endoff,
  364. int whichfork,
  365. int *eof);
  366. /*
  367. * Count fsblocks of the given fork.
  368. */
  369. int
  370. xfs_bmap_count_blocks(
  371. xfs_trans_t *tp,
  372. struct xfs_inode *ip,
  373. int whichfork,
  374. int *count);
  375. #endif /* __KERNEL__ */
  376. #endif /* __XFS_BMAP_H__ */