xfs_bmap.h 12 KB

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