xfs_bmap.h 13 KB

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