ext2fs_extents.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*-
  2. * Copyright (c) 2010 Zheng Liu <lz@freebsd.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * $FreeBSD: head/sys/fs/ext2fs/ext2_extents.c 254260 2013-08-12 21:34:48Z pfg $
  27. */
  28. #include <sys/param.h>
  29. #include <sys/systm.h>
  30. #include <sys/types.h>
  31. #include <sys/kernel.h>
  32. #include <sys/malloc.h>
  33. #include <sys/vnode.h>
  34. #include <sys/mount.h>
  35. #include <sys/buf.h>
  36. #include <ufs/ufs/quota.h>
  37. #include <ufs/ufs/ufsmount.h>
  38. #include <ufs/ufs/inode.h>
  39. #include <ufs/ext2fs/ext2fs.h>
  40. #include <ufs/ext2fs/ext2fs_extents.h>
  41. #include <ufs/ext2fs/ext2fs_extern.h>
  42. static void ext4_ext_binsearch_index(struct inode *ip, struct ext4_extent_path
  43. *path, daddr_t lbn)
  44. {
  45. struct ext4_extent_header *ehp = path->ep_header;
  46. struct ext4_extent_index *l, *r, *m;
  47. l = (struct ext4_extent_index *)(char *)(ehp + 1);
  48. r = (struct ext4_extent_index *)(char *)(ehp + 1) + ehp->eh_ecount - 1;
  49. while (l <= r) {
  50. m = l + (r - l) / 2;
  51. if (lbn < m->ei_blk)
  52. r = m - 1;
  53. else
  54. l = m + 1;
  55. }
  56. path->ep_index = l - 1;
  57. }
  58. static void
  59. ext4_ext_binsearch(struct inode *ip, struct ext4_extent_path *path, daddr_t lbn)
  60. {
  61. struct ext4_extent_header *ehp = path->ep_header;
  62. struct ext4_extent *l, *r, *m;
  63. if (ehp->eh_ecount == 0)
  64. return;
  65. l = (struct ext4_extent *)(char *)(ehp + 1);
  66. r = (struct ext4_extent *)(char *)(ehp + 1) + ehp->eh_ecount - 1;
  67. while (l <= r) {
  68. m = l + (r - l) / 2;
  69. if (lbn < m->e_blk)
  70. r = m - 1;
  71. else
  72. l = m + 1;
  73. }
  74. path->ep_ext = l - 1;
  75. }
  76. /*
  77. * Find a block in ext4 extent cache.
  78. */
  79. int
  80. ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
  81. {
  82. struct ext4_extent_cache *ecp;
  83. int ret = EXT4_EXT_CACHE_NO;
  84. ecp = &ip->i_e2fs_ext_cache;
  85. /* cache is invalid */
  86. if (ecp->ec_type == EXT4_EXT_CACHE_NO)
  87. return (ret);
  88. if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
  89. ep->e_blk = ecp->ec_blk;
  90. ep->e_start_lo = ecp->ec_start & 0xffffffff;
  91. ep->e_start_hi = ecp->ec_start >> 32 & 0xffff;
  92. ep->e_len = ecp->ec_len;
  93. ret = ecp->ec_type;
  94. }
  95. return (ret);
  96. }
  97. /*
  98. * Put an ext4_extent structure in ext4 cache.
  99. */
  100. void
  101. ext4_ext_put_cache(struct inode *ip, struct ext4_extent *ep, int type)
  102. {
  103. struct ext4_extent_cache *ecp;
  104. ecp = &ip->i_e2fs_ext_cache;
  105. ecp->ec_type = type;
  106. ecp->ec_blk = ep->e_blk;
  107. ecp->ec_len = ep->e_len;
  108. ecp->ec_start = (daddr_t)ep->e_start_hi << 32 | ep->e_start_lo;
  109. }
  110. /*
  111. * Find an extent.
  112. */
  113. struct ext4_extent_path *
  114. ext4_ext_find_extent(struct m_ext2fs *fs, struct inode *ip,
  115. daddr_t lbn, struct ext4_extent_path *path)
  116. {
  117. struct vnode *vp;
  118. struct ext4_extent_header *ehp;
  119. uint16_t i;
  120. int error;
  121. daddr_t nblk;
  122. vp = ITOV(ip);
  123. ehp = (struct ext4_extent_header *)(char *)ip->i_e2fs_blocks;
  124. if (ehp->eh_magic != EXT4_EXT_MAGIC)
  125. return (NULL);
  126. path->ep_header = ehp;
  127. for (i = ehp->eh_depth; i != 0; --i) {
  128. ext4_ext_binsearch_index(ip, path, lbn);
  129. path->ep_depth = 0;
  130. path->ep_ext = NULL;
  131. nblk = (daddr_t)path->ep_index->ei_leaf_hi << 32 |
  132. path->ep_index->ei_leaf_lo;
  133. if (path->ep_bp != NULL) {
  134. brelse(path->ep_bp);
  135. path->ep_bp = NULL;
  136. }
  137. error = bread(ip->i_devvp, fsbtodb(fs, nblk), fs->e2fs_fsize,
  138. &path->ep_bp);
  139. if (error) {
  140. brelse(path->ep_bp);
  141. path->ep_bp = NULL;
  142. return (NULL);
  143. }
  144. ehp = (struct ext4_extent_header *)path->ep_bp->b_data;
  145. path->ep_header = ehp;
  146. }
  147. path->ep_depth = i;
  148. path->ep_ext = NULL;
  149. path->ep_index = NULL;
  150. ext4_ext_binsearch(ip, path, lbn);
  151. return (path);
  152. }