xfs_bmap.h 11 KB

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