xfs_inode_fork.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  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. #include <linux/log2.h>
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_inum.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_inode_item.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_error.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. kmem_zone_t *xfs_ifork_zone;
  38. STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
  39. STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
  40. STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
  41. #ifdef DEBUG
  42. /*
  43. * Make sure that the extents in the given memory buffer
  44. * are valid.
  45. */
  46. void
  47. xfs_validate_extents(
  48. xfs_ifork_t *ifp,
  49. int nrecs,
  50. xfs_exntfmt_t fmt)
  51. {
  52. xfs_bmbt_irec_t irec;
  53. xfs_bmbt_rec_host_t rec;
  54. int i;
  55. for (i = 0; i < nrecs; i++) {
  56. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  57. rec.l0 = get_unaligned(&ep->l0);
  58. rec.l1 = get_unaligned(&ep->l1);
  59. xfs_bmbt_get_all(&rec, &irec);
  60. if (fmt == XFS_EXTFMT_NOSTATE)
  61. ASSERT(irec.br_state == XFS_EXT_NORM);
  62. }
  63. }
  64. #else /* DEBUG */
  65. #define xfs_validate_extents(ifp, nrecs, fmt)
  66. #endif /* DEBUG */
  67. /*
  68. * Move inode type and inode format specific information from the
  69. * on-disk inode to the in-core inode. For fifos, devs, and sockets
  70. * this means set if_rdev to the proper value. For files, directories,
  71. * and symlinks this means to bring in the in-line data or extent
  72. * pointers. For a file in B-tree format, only the root is immediately
  73. * brought in-core. The rest will be in-lined in if_extents when it
  74. * is first referenced (see xfs_iread_extents()).
  75. */
  76. int
  77. xfs_iformat_fork(
  78. xfs_inode_t *ip,
  79. xfs_dinode_t *dip)
  80. {
  81. xfs_attr_shortform_t *atp;
  82. int size;
  83. int error = 0;
  84. xfs_fsize_t di_size;
  85. if (unlikely(be32_to_cpu(dip->di_nextents) +
  86. be16_to_cpu(dip->di_anextents) >
  87. be64_to_cpu(dip->di_nblocks))) {
  88. xfs_warn(ip->i_mount,
  89. "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
  90. (unsigned long long)ip->i_ino,
  91. (int)(be32_to_cpu(dip->di_nextents) +
  92. be16_to_cpu(dip->di_anextents)),
  93. (unsigned long long)
  94. be64_to_cpu(dip->di_nblocks));
  95. XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
  96. ip->i_mount, dip);
  97. return XFS_ERROR(EFSCORRUPTED);
  98. }
  99. if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
  100. xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
  101. (unsigned long long)ip->i_ino,
  102. dip->di_forkoff);
  103. XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
  104. ip->i_mount, dip);
  105. return XFS_ERROR(EFSCORRUPTED);
  106. }
  107. if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
  108. !ip->i_mount->m_rtdev_targp)) {
  109. xfs_warn(ip->i_mount,
  110. "corrupt dinode %Lu, has realtime flag set.",
  111. ip->i_ino);
  112. XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
  113. XFS_ERRLEVEL_LOW, ip->i_mount, dip);
  114. return XFS_ERROR(EFSCORRUPTED);
  115. }
  116. switch (ip->i_d.di_mode & S_IFMT) {
  117. case S_IFIFO:
  118. case S_IFCHR:
  119. case S_IFBLK:
  120. case S_IFSOCK:
  121. if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
  122. XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
  123. ip->i_mount, dip);
  124. return XFS_ERROR(EFSCORRUPTED);
  125. }
  126. ip->i_d.di_size = 0;
  127. ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
  128. break;
  129. case S_IFREG:
  130. case S_IFLNK:
  131. case S_IFDIR:
  132. switch (dip->di_format) {
  133. case XFS_DINODE_FMT_LOCAL:
  134. /*
  135. * no local regular files yet
  136. */
  137. if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
  138. xfs_warn(ip->i_mount,
  139. "corrupt inode %Lu (local format for regular file).",
  140. (unsigned long long) ip->i_ino);
  141. XFS_CORRUPTION_ERROR("xfs_iformat(4)",
  142. XFS_ERRLEVEL_LOW,
  143. ip->i_mount, dip);
  144. return XFS_ERROR(EFSCORRUPTED);
  145. }
  146. di_size = be64_to_cpu(dip->di_size);
  147. if (unlikely(di_size < 0 ||
  148. di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
  149. xfs_warn(ip->i_mount,
  150. "corrupt inode %Lu (bad size %Ld for local inode).",
  151. (unsigned long long) ip->i_ino,
  152. (long long) di_size);
  153. XFS_CORRUPTION_ERROR("xfs_iformat(5)",
  154. XFS_ERRLEVEL_LOW,
  155. ip->i_mount, dip);
  156. return XFS_ERROR(EFSCORRUPTED);
  157. }
  158. size = (int)di_size;
  159. error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
  160. break;
  161. case XFS_DINODE_FMT_EXTENTS:
  162. error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
  163. break;
  164. case XFS_DINODE_FMT_BTREE:
  165. error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
  166. break;
  167. default:
  168. XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
  169. ip->i_mount);
  170. return XFS_ERROR(EFSCORRUPTED);
  171. }
  172. break;
  173. default:
  174. XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
  175. return XFS_ERROR(EFSCORRUPTED);
  176. }
  177. if (error) {
  178. return error;
  179. }
  180. if (!XFS_DFORK_Q(dip))
  181. return 0;
  182. ASSERT(ip->i_afp == NULL);
  183. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
  184. switch (dip->di_aformat) {
  185. case XFS_DINODE_FMT_LOCAL:
  186. atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
  187. size = be16_to_cpu(atp->hdr.totsize);
  188. if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
  189. xfs_warn(ip->i_mount,
  190. "corrupt inode %Lu (bad attr fork size %Ld).",
  191. (unsigned long long) ip->i_ino,
  192. (long long) size);
  193. XFS_CORRUPTION_ERROR("xfs_iformat(8)",
  194. XFS_ERRLEVEL_LOW,
  195. ip->i_mount, dip);
  196. return XFS_ERROR(EFSCORRUPTED);
  197. }
  198. error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
  199. break;
  200. case XFS_DINODE_FMT_EXTENTS:
  201. error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
  202. break;
  203. case XFS_DINODE_FMT_BTREE:
  204. error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
  205. break;
  206. default:
  207. error = XFS_ERROR(EFSCORRUPTED);
  208. break;
  209. }
  210. if (error) {
  211. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  212. ip->i_afp = NULL;
  213. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  214. }
  215. return error;
  216. }
  217. /*
  218. * The file is in-lined in the on-disk inode.
  219. * If it fits into if_inline_data, then copy
  220. * it there, otherwise allocate a buffer for it
  221. * and copy the data there. Either way, set
  222. * if_data to point at the data.
  223. * If we allocate a buffer for the data, make
  224. * sure that its size is a multiple of 4 and
  225. * record the real size in i_real_bytes.
  226. */
  227. STATIC int
  228. xfs_iformat_local(
  229. xfs_inode_t *ip,
  230. xfs_dinode_t *dip,
  231. int whichfork,
  232. int size)
  233. {
  234. xfs_ifork_t *ifp;
  235. int real_size;
  236. /*
  237. * If the size is unreasonable, then something
  238. * is wrong and we just bail out rather than crash in
  239. * kmem_alloc() or memcpy() below.
  240. */
  241. if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  242. xfs_warn(ip->i_mount,
  243. "corrupt inode %Lu (bad size %d for local fork, size = %d).",
  244. (unsigned long long) ip->i_ino, size,
  245. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
  246. XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
  247. ip->i_mount, dip);
  248. return XFS_ERROR(EFSCORRUPTED);
  249. }
  250. ifp = XFS_IFORK_PTR(ip, whichfork);
  251. real_size = 0;
  252. if (size == 0)
  253. ifp->if_u1.if_data = NULL;
  254. else if (size <= sizeof(ifp->if_u2.if_inline_data))
  255. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  256. else {
  257. real_size = roundup(size, 4);
  258. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
  259. }
  260. ifp->if_bytes = size;
  261. ifp->if_real_bytes = real_size;
  262. if (size)
  263. memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
  264. ifp->if_flags &= ~XFS_IFEXTENTS;
  265. ifp->if_flags |= XFS_IFINLINE;
  266. return 0;
  267. }
  268. /*
  269. * The file consists of a set of extents all
  270. * of which fit into the on-disk inode.
  271. * If there are few enough extents to fit into
  272. * the if_inline_ext, then copy them there.
  273. * Otherwise allocate a buffer for them and copy
  274. * them into it. Either way, set if_extents
  275. * to point at the extents.
  276. */
  277. STATIC int
  278. xfs_iformat_extents(
  279. xfs_inode_t *ip,
  280. xfs_dinode_t *dip,
  281. int whichfork)
  282. {
  283. xfs_bmbt_rec_t *dp;
  284. xfs_ifork_t *ifp;
  285. int nex;
  286. int size;
  287. int i;
  288. ifp = XFS_IFORK_PTR(ip, whichfork);
  289. nex = XFS_DFORK_NEXTENTS(dip, whichfork);
  290. size = nex * (uint)sizeof(xfs_bmbt_rec_t);
  291. /*
  292. * If the number of extents is unreasonable, then something
  293. * is wrong and we just bail out rather than crash in
  294. * kmem_alloc() or memcpy() below.
  295. */
  296. if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  297. xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
  298. (unsigned long long) ip->i_ino, nex);
  299. XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
  300. ip->i_mount, dip);
  301. return XFS_ERROR(EFSCORRUPTED);
  302. }
  303. ifp->if_real_bytes = 0;
  304. if (nex == 0)
  305. ifp->if_u1.if_extents = NULL;
  306. else if (nex <= XFS_INLINE_EXTS)
  307. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  308. else
  309. xfs_iext_add(ifp, 0, nex);
  310. ifp->if_bytes = size;
  311. if (size) {
  312. dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
  313. xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
  314. for (i = 0; i < nex; i++, dp++) {
  315. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  316. ep->l0 = get_unaligned_be64(&dp->l0);
  317. ep->l1 = get_unaligned_be64(&dp->l1);
  318. }
  319. XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
  320. if (whichfork != XFS_DATA_FORK ||
  321. XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
  322. if (unlikely(xfs_check_nostate_extents(
  323. ifp, 0, nex))) {
  324. XFS_ERROR_REPORT("xfs_iformat_extents(2)",
  325. XFS_ERRLEVEL_LOW,
  326. ip->i_mount);
  327. return XFS_ERROR(EFSCORRUPTED);
  328. }
  329. }
  330. ifp->if_flags |= XFS_IFEXTENTS;
  331. return 0;
  332. }
  333. /*
  334. * The file has too many extents to fit into
  335. * the inode, so they are in B-tree format.
  336. * Allocate a buffer for the root of the B-tree
  337. * and copy the root into it. The i_extents
  338. * field will remain NULL until all of the
  339. * extents are read in (when they are needed).
  340. */
  341. STATIC int
  342. xfs_iformat_btree(
  343. xfs_inode_t *ip,
  344. xfs_dinode_t *dip,
  345. int whichfork)
  346. {
  347. struct xfs_mount *mp = ip->i_mount;
  348. xfs_bmdr_block_t *dfp;
  349. xfs_ifork_t *ifp;
  350. /* REFERENCED */
  351. int nrecs;
  352. int size;
  353. ifp = XFS_IFORK_PTR(ip, whichfork);
  354. dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
  355. size = XFS_BMAP_BROOT_SPACE(mp, dfp);
  356. nrecs = be16_to_cpu(dfp->bb_numrecs);
  357. /*
  358. * blow out if -- fork has less extents than can fit in
  359. * fork (fork shouldn't be a btree format), root btree
  360. * block has more records than can fit into the fork,
  361. * or the number of extents is greater than the number of
  362. * blocks.
  363. */
  364. if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
  365. XFS_IFORK_MAXEXT(ip, whichfork) ||
  366. XFS_BMDR_SPACE_CALC(nrecs) >
  367. XFS_DFORK_SIZE(dip, mp, whichfork) ||
  368. XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
  369. xfs_warn(mp, "corrupt inode %Lu (btree).",
  370. (unsigned long long) ip->i_ino);
  371. XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
  372. mp, dip);
  373. return XFS_ERROR(EFSCORRUPTED);
  374. }
  375. ifp->if_broot_bytes = size;
  376. ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
  377. ASSERT(ifp->if_broot != NULL);
  378. /*
  379. * Copy and convert from the on-disk structure
  380. * to the in-memory structure.
  381. */
  382. xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
  383. ifp->if_broot, size);
  384. ifp->if_flags &= ~XFS_IFEXTENTS;
  385. ifp->if_flags |= XFS_IFBROOT;
  386. return 0;
  387. }
  388. /*
  389. * Read in extents from a btree-format inode.
  390. * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
  391. */
  392. int
  393. xfs_iread_extents(
  394. xfs_trans_t *tp,
  395. xfs_inode_t *ip,
  396. int whichfork)
  397. {
  398. int error;
  399. xfs_ifork_t *ifp;
  400. xfs_extnum_t nextents;
  401. if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  402. XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
  403. ip->i_mount);
  404. return XFS_ERROR(EFSCORRUPTED);
  405. }
  406. nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
  407. ifp = XFS_IFORK_PTR(ip, whichfork);
  408. /*
  409. * We know that the size is valid (it's checked in iformat_btree)
  410. */
  411. ifp->if_bytes = ifp->if_real_bytes = 0;
  412. ifp->if_flags |= XFS_IFEXTENTS;
  413. xfs_iext_add(ifp, 0, nextents);
  414. error = xfs_bmap_read_extents(tp, ip, whichfork);
  415. if (error) {
  416. xfs_iext_destroy(ifp);
  417. ifp->if_flags &= ~XFS_IFEXTENTS;
  418. return error;
  419. }
  420. xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
  421. return 0;
  422. }
  423. /*
  424. * Reallocate the space for if_broot based on the number of records
  425. * being added or deleted as indicated in rec_diff. Move the records
  426. * and pointers in if_broot to fit the new size. When shrinking this
  427. * will eliminate holes between the records and pointers created by
  428. * the caller. When growing this will create holes to be filled in
  429. * by the caller.
  430. *
  431. * The caller must not request to add more records than would fit in
  432. * the on-disk inode root. If the if_broot is currently NULL, then
  433. * if we are adding records, one will be allocated. The caller must also
  434. * not request that the number of records go below zero, although
  435. * it can go to zero.
  436. *
  437. * ip -- the inode whose if_broot area is changing
  438. * ext_diff -- the change in the number of records, positive or negative,
  439. * requested for the if_broot array.
  440. */
  441. void
  442. xfs_iroot_realloc(
  443. xfs_inode_t *ip,
  444. int rec_diff,
  445. int whichfork)
  446. {
  447. struct xfs_mount *mp = ip->i_mount;
  448. int cur_max;
  449. xfs_ifork_t *ifp;
  450. struct xfs_btree_block *new_broot;
  451. int new_max;
  452. size_t new_size;
  453. char *np;
  454. char *op;
  455. /*
  456. * Handle the degenerate case quietly.
  457. */
  458. if (rec_diff == 0) {
  459. return;
  460. }
  461. ifp = XFS_IFORK_PTR(ip, whichfork);
  462. if (rec_diff > 0) {
  463. /*
  464. * If there wasn't any memory allocated before, just
  465. * allocate it now and get out.
  466. */
  467. if (ifp->if_broot_bytes == 0) {
  468. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
  469. ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  470. ifp->if_broot_bytes = (int)new_size;
  471. return;
  472. }
  473. /*
  474. * If there is already an existing if_broot, then we need
  475. * to realloc() it and shift the pointers to their new
  476. * location. The records don't change location because
  477. * they are kept butted up against the btree block header.
  478. */
  479. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  480. new_max = cur_max + rec_diff;
  481. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  482. ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
  483. XFS_BMAP_BROOT_SPACE_CALC(mp, cur_max),
  484. KM_SLEEP | KM_NOFS);
  485. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  486. ifp->if_broot_bytes);
  487. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  488. (int)new_size);
  489. ifp->if_broot_bytes = (int)new_size;
  490. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  491. XFS_IFORK_SIZE(ip, whichfork));
  492. memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
  493. return;
  494. }
  495. /*
  496. * rec_diff is less than 0. In this case, we are shrinking the
  497. * if_broot buffer. It must already exist. If we go to zero
  498. * records, just get rid of the root and clear the status bit.
  499. */
  500. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  501. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  502. new_max = cur_max + rec_diff;
  503. ASSERT(new_max >= 0);
  504. if (new_max > 0)
  505. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  506. else
  507. new_size = 0;
  508. if (new_size > 0) {
  509. new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  510. /*
  511. * First copy over the btree block header.
  512. */
  513. memcpy(new_broot, ifp->if_broot,
  514. XFS_BMBT_BLOCK_LEN(ip->i_mount));
  515. } else {
  516. new_broot = NULL;
  517. ifp->if_flags &= ~XFS_IFBROOT;
  518. }
  519. /*
  520. * Only copy the records and pointers if there are any.
  521. */
  522. if (new_max > 0) {
  523. /*
  524. * First copy the records.
  525. */
  526. op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
  527. np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
  528. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  529. /*
  530. * Then copy the pointers.
  531. */
  532. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  533. ifp->if_broot_bytes);
  534. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
  535. (int)new_size);
  536. memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
  537. }
  538. kmem_free(ifp->if_broot);
  539. ifp->if_broot = new_broot;
  540. ifp->if_broot_bytes = (int)new_size;
  541. if (ifp->if_broot)
  542. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  543. XFS_IFORK_SIZE(ip, whichfork));
  544. return;
  545. }
  546. /*
  547. * This is called when the amount of space needed for if_data
  548. * is increased or decreased. The change in size is indicated by
  549. * the number of bytes that need to be added or deleted in the
  550. * byte_diff parameter.
  551. *
  552. * If the amount of space needed has decreased below the size of the
  553. * inline buffer, then switch to using the inline buffer. Otherwise,
  554. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  555. * to what is needed.
  556. *
  557. * ip -- the inode whose if_data area is changing
  558. * byte_diff -- the change in the number of bytes, positive or negative,
  559. * requested for the if_data array.
  560. */
  561. void
  562. xfs_idata_realloc(
  563. xfs_inode_t *ip,
  564. int byte_diff,
  565. int whichfork)
  566. {
  567. xfs_ifork_t *ifp;
  568. int new_size;
  569. int real_size;
  570. if (byte_diff == 0) {
  571. return;
  572. }
  573. ifp = XFS_IFORK_PTR(ip, whichfork);
  574. new_size = (int)ifp->if_bytes + byte_diff;
  575. ASSERT(new_size >= 0);
  576. if (new_size == 0) {
  577. if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  578. kmem_free(ifp->if_u1.if_data);
  579. }
  580. ifp->if_u1.if_data = NULL;
  581. real_size = 0;
  582. } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
  583. /*
  584. * If the valid extents/data can fit in if_inline_ext/data,
  585. * copy them from the malloc'd vector and free it.
  586. */
  587. if (ifp->if_u1.if_data == NULL) {
  588. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  589. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  590. ASSERT(ifp->if_real_bytes != 0);
  591. memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
  592. new_size);
  593. kmem_free(ifp->if_u1.if_data);
  594. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  595. }
  596. real_size = 0;
  597. } else {
  598. /*
  599. * Stuck with malloc/realloc.
  600. * For inline data, the underlying buffer must be
  601. * a multiple of 4 bytes in size so that it can be
  602. * logged and stay on word boundaries. We enforce
  603. * that here.
  604. */
  605. real_size = roundup(new_size, 4);
  606. if (ifp->if_u1.if_data == NULL) {
  607. ASSERT(ifp->if_real_bytes == 0);
  608. ifp->if_u1.if_data = kmem_alloc(real_size,
  609. KM_SLEEP | KM_NOFS);
  610. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  611. /*
  612. * Only do the realloc if the underlying size
  613. * is really changing.
  614. */
  615. if (ifp->if_real_bytes != real_size) {
  616. ifp->if_u1.if_data =
  617. kmem_realloc(ifp->if_u1.if_data,
  618. real_size,
  619. ifp->if_real_bytes,
  620. KM_SLEEP | KM_NOFS);
  621. }
  622. } else {
  623. ASSERT(ifp->if_real_bytes == 0);
  624. ifp->if_u1.if_data = kmem_alloc(real_size,
  625. KM_SLEEP | KM_NOFS);
  626. memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
  627. ifp->if_bytes);
  628. }
  629. }
  630. ifp->if_real_bytes = real_size;
  631. ifp->if_bytes = new_size;
  632. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  633. }
  634. void
  635. xfs_idestroy_fork(
  636. xfs_inode_t *ip,
  637. int whichfork)
  638. {
  639. xfs_ifork_t *ifp;
  640. ifp = XFS_IFORK_PTR(ip, whichfork);
  641. if (ifp->if_broot != NULL) {
  642. kmem_free(ifp->if_broot);
  643. ifp->if_broot = NULL;
  644. }
  645. /*
  646. * If the format is local, then we can't have an extents
  647. * array so just look for an inline data array. If we're
  648. * not local then we may or may not have an extents list,
  649. * so check and free it up if we do.
  650. */
  651. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  652. if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
  653. (ifp->if_u1.if_data != NULL)) {
  654. ASSERT(ifp->if_real_bytes != 0);
  655. kmem_free(ifp->if_u1.if_data);
  656. ifp->if_u1.if_data = NULL;
  657. ifp->if_real_bytes = 0;
  658. }
  659. } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
  660. ((ifp->if_flags & XFS_IFEXTIREC) ||
  661. ((ifp->if_u1.if_extents != NULL) &&
  662. (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
  663. ASSERT(ifp->if_real_bytes != 0);
  664. xfs_iext_destroy(ifp);
  665. }
  666. ASSERT(ifp->if_u1.if_extents == NULL ||
  667. ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
  668. ASSERT(ifp->if_real_bytes == 0);
  669. if (whichfork == XFS_ATTR_FORK) {
  670. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  671. ip->i_afp = NULL;
  672. }
  673. }
  674. /*
  675. * xfs_iextents_copy()
  676. *
  677. * This is called to copy the REAL extents (as opposed to the delayed
  678. * allocation extents) from the inode into the given buffer. It
  679. * returns the number of bytes copied into the buffer.
  680. *
  681. * If there are no delayed allocation extents, then we can just
  682. * memcpy() the extents into the buffer. Otherwise, we need to
  683. * examine each extent in turn and skip those which are delayed.
  684. */
  685. int
  686. xfs_iextents_copy(
  687. xfs_inode_t *ip,
  688. xfs_bmbt_rec_t *dp,
  689. int whichfork)
  690. {
  691. int copied;
  692. int i;
  693. xfs_ifork_t *ifp;
  694. int nrecs;
  695. xfs_fsblock_t start_block;
  696. ifp = XFS_IFORK_PTR(ip, whichfork);
  697. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  698. ASSERT(ifp->if_bytes > 0);
  699. nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  700. XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
  701. ASSERT(nrecs > 0);
  702. /*
  703. * There are some delayed allocation extents in the
  704. * inode, so copy the extents one at a time and skip
  705. * the delayed ones. There must be at least one
  706. * non-delayed extent.
  707. */
  708. copied = 0;
  709. for (i = 0; i < nrecs; i++) {
  710. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  711. start_block = xfs_bmbt_get_startblock(ep);
  712. if (isnullstartblock(start_block)) {
  713. /*
  714. * It's a delayed allocation extent, so skip it.
  715. */
  716. continue;
  717. }
  718. /* Translate to on disk format */
  719. put_unaligned_be64(ep->l0, &dp->l0);
  720. put_unaligned_be64(ep->l1, &dp->l1);
  721. dp++;
  722. copied++;
  723. }
  724. ASSERT(copied != 0);
  725. xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
  726. return (copied * (uint)sizeof(xfs_bmbt_rec_t));
  727. }
  728. /*
  729. * Each of the following cases stores data into the same region
  730. * of the on-disk inode, so only one of them can be valid at
  731. * any given time. While it is possible to have conflicting formats
  732. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  733. * in EXTENTS format, this can only happen when the fork has
  734. * changed formats after being modified but before being flushed.
  735. * In these cases, the format always takes precedence, because the
  736. * format indicates the current state of the fork.
  737. */
  738. void
  739. xfs_iflush_fork(
  740. xfs_inode_t *ip,
  741. xfs_dinode_t *dip,
  742. xfs_inode_log_item_t *iip,
  743. int whichfork,
  744. xfs_buf_t *bp)
  745. {
  746. char *cp;
  747. xfs_ifork_t *ifp;
  748. xfs_mount_t *mp;
  749. static const short brootflag[2] =
  750. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  751. static const short dataflag[2] =
  752. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  753. static const short extflag[2] =
  754. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  755. if (!iip)
  756. return;
  757. ifp = XFS_IFORK_PTR(ip, whichfork);
  758. /*
  759. * This can happen if we gave up in iformat in an error path,
  760. * for the attribute fork.
  761. */
  762. if (!ifp) {
  763. ASSERT(whichfork == XFS_ATTR_FORK);
  764. return;
  765. }
  766. cp = XFS_DFORK_PTR(dip, whichfork);
  767. mp = ip->i_mount;
  768. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  769. case XFS_DINODE_FMT_LOCAL:
  770. if ((iip->ili_fields & dataflag[whichfork]) &&
  771. (ifp->if_bytes > 0)) {
  772. ASSERT(ifp->if_u1.if_data != NULL);
  773. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  774. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  775. }
  776. break;
  777. case XFS_DINODE_FMT_EXTENTS:
  778. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  779. !(iip->ili_fields & extflag[whichfork]));
  780. if ((iip->ili_fields & extflag[whichfork]) &&
  781. (ifp->if_bytes > 0)) {
  782. ASSERT(xfs_iext_get_ext(ifp, 0));
  783. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  784. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  785. whichfork);
  786. }
  787. break;
  788. case XFS_DINODE_FMT_BTREE:
  789. if ((iip->ili_fields & brootflag[whichfork]) &&
  790. (ifp->if_broot_bytes > 0)) {
  791. ASSERT(ifp->if_broot != NULL);
  792. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  793. XFS_IFORK_SIZE(ip, whichfork));
  794. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  795. (xfs_bmdr_block_t *)cp,
  796. XFS_DFORK_SIZE(dip, mp, whichfork));
  797. }
  798. break;
  799. case XFS_DINODE_FMT_DEV:
  800. if (iip->ili_fields & XFS_ILOG_DEV) {
  801. ASSERT(whichfork == XFS_DATA_FORK);
  802. xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
  803. }
  804. break;
  805. case XFS_DINODE_FMT_UUID:
  806. if (iip->ili_fields & XFS_ILOG_UUID) {
  807. ASSERT(whichfork == XFS_DATA_FORK);
  808. memcpy(XFS_DFORK_DPTR(dip),
  809. &ip->i_df.if_u2.if_uuid,
  810. sizeof(uuid_t));
  811. }
  812. break;
  813. default:
  814. ASSERT(0);
  815. break;
  816. }
  817. }
  818. /*
  819. * Return a pointer to the extent record at file index idx.
  820. */
  821. xfs_bmbt_rec_host_t *
  822. xfs_iext_get_ext(
  823. xfs_ifork_t *ifp, /* inode fork pointer */
  824. xfs_extnum_t idx) /* index of target extent */
  825. {
  826. ASSERT(idx >= 0);
  827. ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  828. if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
  829. return ifp->if_u1.if_ext_irec->er_extbuf;
  830. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  831. xfs_ext_irec_t *erp; /* irec pointer */
  832. int erp_idx = 0; /* irec index */
  833. xfs_extnum_t page_idx = idx; /* ext index in target list */
  834. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  835. return &erp->er_extbuf[page_idx];
  836. } else if (ifp->if_bytes) {
  837. return &ifp->if_u1.if_extents[idx];
  838. } else {
  839. return NULL;
  840. }
  841. }
  842. /*
  843. * Insert new item(s) into the extent records for incore inode
  844. * fork 'ifp'. 'count' new items are inserted at index 'idx'.
  845. */
  846. void
  847. xfs_iext_insert(
  848. xfs_inode_t *ip, /* incore inode pointer */
  849. xfs_extnum_t idx, /* starting index of new items */
  850. xfs_extnum_t count, /* number of inserted items */
  851. xfs_bmbt_irec_t *new, /* items to insert */
  852. int state) /* type of extent conversion */
  853. {
  854. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  855. xfs_extnum_t i; /* extent record index */
  856. trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
  857. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  858. xfs_iext_add(ifp, idx, count);
  859. for (i = idx; i < idx + count; i++, new++)
  860. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
  861. }
  862. /*
  863. * This is called when the amount of space required for incore file
  864. * extents needs to be increased. The ext_diff parameter stores the
  865. * number of new extents being added and the idx parameter contains
  866. * the extent index where the new extents will be added. If the new
  867. * extents are being appended, then we just need to (re)allocate and
  868. * initialize the space. Otherwise, if the new extents are being
  869. * inserted into the middle of the existing entries, a bit more work
  870. * is required to make room for the new extents to be inserted. The
  871. * caller is responsible for filling in the new extent entries upon
  872. * return.
  873. */
  874. void
  875. xfs_iext_add(
  876. xfs_ifork_t *ifp, /* inode fork pointer */
  877. xfs_extnum_t idx, /* index to begin adding exts */
  878. int ext_diff) /* number of extents to add */
  879. {
  880. int byte_diff; /* new bytes being added */
  881. int new_size; /* size of extents after adding */
  882. xfs_extnum_t nextents; /* number of extents in file */
  883. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  884. ASSERT((idx >= 0) && (idx <= nextents));
  885. byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
  886. new_size = ifp->if_bytes + byte_diff;
  887. /*
  888. * If the new number of extents (nextents + ext_diff)
  889. * fits inside the inode, then continue to use the inline
  890. * extent buffer.
  891. */
  892. if (nextents + ext_diff <= XFS_INLINE_EXTS) {
  893. if (idx < nextents) {
  894. memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
  895. &ifp->if_u2.if_inline_ext[idx],
  896. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  897. memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
  898. }
  899. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  900. ifp->if_real_bytes = 0;
  901. }
  902. /*
  903. * Otherwise use a linear (direct) extent list.
  904. * If the extents are currently inside the inode,
  905. * xfs_iext_realloc_direct will switch us from
  906. * inline to direct extent allocation mode.
  907. */
  908. else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
  909. xfs_iext_realloc_direct(ifp, new_size);
  910. if (idx < nextents) {
  911. memmove(&ifp->if_u1.if_extents[idx + ext_diff],
  912. &ifp->if_u1.if_extents[idx],
  913. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  914. memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
  915. }
  916. }
  917. /* Indirection array */
  918. else {
  919. xfs_ext_irec_t *erp;
  920. int erp_idx = 0;
  921. int page_idx = idx;
  922. ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
  923. if (ifp->if_flags & XFS_IFEXTIREC) {
  924. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
  925. } else {
  926. xfs_iext_irec_init(ifp);
  927. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  928. erp = ifp->if_u1.if_ext_irec;
  929. }
  930. /* Extents fit in target extent page */
  931. if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
  932. if (page_idx < erp->er_extcount) {
  933. memmove(&erp->er_extbuf[page_idx + ext_diff],
  934. &erp->er_extbuf[page_idx],
  935. (erp->er_extcount - page_idx) *
  936. sizeof(xfs_bmbt_rec_t));
  937. memset(&erp->er_extbuf[page_idx], 0, byte_diff);
  938. }
  939. erp->er_extcount += ext_diff;
  940. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  941. }
  942. /* Insert a new extent page */
  943. else if (erp) {
  944. xfs_iext_add_indirect_multi(ifp,
  945. erp_idx, page_idx, ext_diff);
  946. }
  947. /*
  948. * If extent(s) are being appended to the last page in
  949. * the indirection array and the new extent(s) don't fit
  950. * in the page, then erp is NULL and erp_idx is set to
  951. * the next index needed in the indirection array.
  952. */
  953. else {
  954. uint count = ext_diff;
  955. while (count) {
  956. erp = xfs_iext_irec_new(ifp, erp_idx);
  957. erp->er_extcount = min(count, XFS_LINEAR_EXTS);
  958. count -= erp->er_extcount;
  959. if (count)
  960. erp_idx++;
  961. }
  962. }
  963. }
  964. ifp->if_bytes = new_size;
  965. }
  966. /*
  967. * This is called when incore extents are being added to the indirection
  968. * array and the new extents do not fit in the target extent list. The
  969. * erp_idx parameter contains the irec index for the target extent list
  970. * in the indirection array, and the idx parameter contains the extent
  971. * index within the list. The number of extents being added is stored
  972. * in the count parameter.
  973. *
  974. * |-------| |-------|
  975. * | | | | idx - number of extents before idx
  976. * | idx | | count |
  977. * | | | | count - number of extents being inserted at idx
  978. * |-------| |-------|
  979. * | count | | nex2 | nex2 - number of extents after idx + count
  980. * |-------| |-------|
  981. */
  982. void
  983. xfs_iext_add_indirect_multi(
  984. xfs_ifork_t *ifp, /* inode fork pointer */
  985. int erp_idx, /* target extent irec index */
  986. xfs_extnum_t idx, /* index within target list */
  987. int count) /* new extents being added */
  988. {
  989. int byte_diff; /* new bytes being added */
  990. xfs_ext_irec_t *erp; /* pointer to irec entry */
  991. xfs_extnum_t ext_diff; /* number of extents to add */
  992. xfs_extnum_t ext_cnt; /* new extents still needed */
  993. xfs_extnum_t nex2; /* extents after idx + count */
  994. xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
  995. int nlists; /* number of irec's (lists) */
  996. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  997. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  998. nex2 = erp->er_extcount - idx;
  999. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1000. /*
  1001. * Save second part of target extent list
  1002. * (all extents past */
  1003. if (nex2) {
  1004. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  1005. nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
  1006. memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
  1007. erp->er_extcount -= nex2;
  1008. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
  1009. memset(&erp->er_extbuf[idx], 0, byte_diff);
  1010. }
  1011. /*
  1012. * Add the new extents to the end of the target
  1013. * list, then allocate new irec record(s) and
  1014. * extent buffer(s) as needed to store the rest
  1015. * of the new extents.
  1016. */
  1017. ext_cnt = count;
  1018. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
  1019. if (ext_diff) {
  1020. erp->er_extcount += ext_diff;
  1021. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  1022. ext_cnt -= ext_diff;
  1023. }
  1024. while (ext_cnt) {
  1025. erp_idx++;
  1026. erp = xfs_iext_irec_new(ifp, erp_idx);
  1027. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
  1028. erp->er_extcount = ext_diff;
  1029. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  1030. ext_cnt -= ext_diff;
  1031. }
  1032. /* Add nex2 extents back to indirection array */
  1033. if (nex2) {
  1034. xfs_extnum_t ext_avail;
  1035. int i;
  1036. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  1037. ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
  1038. i = 0;
  1039. /*
  1040. * If nex2 extents fit in the current page, append
  1041. * nex2_ep after the new extents.
  1042. */
  1043. if (nex2 <= ext_avail) {
  1044. i = erp->er_extcount;
  1045. }
  1046. /*
  1047. * Otherwise, check if space is available in the
  1048. * next page.
  1049. */
  1050. else if ((erp_idx < nlists - 1) &&
  1051. (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
  1052. ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
  1053. erp_idx++;
  1054. erp++;
  1055. /* Create a hole for nex2 extents */
  1056. memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
  1057. erp->er_extcount * sizeof(xfs_bmbt_rec_t));
  1058. }
  1059. /*
  1060. * Final choice, create a new extent page for
  1061. * nex2 extents.
  1062. */
  1063. else {
  1064. erp_idx++;
  1065. erp = xfs_iext_irec_new(ifp, erp_idx);
  1066. }
  1067. memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
  1068. kmem_free(nex2_ep);
  1069. erp->er_extcount += nex2;
  1070. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
  1071. }
  1072. }
  1073. /*
  1074. * This is called when the amount of space required for incore file
  1075. * extents needs to be decreased. The ext_diff parameter stores the
  1076. * number of extents to be removed and the idx parameter contains
  1077. * the extent index where the extents will be removed from.
  1078. *
  1079. * If the amount of space needed has decreased below the linear
  1080. * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
  1081. * extent array. Otherwise, use kmem_realloc() to adjust the
  1082. * size to what is needed.
  1083. */
  1084. void
  1085. xfs_iext_remove(
  1086. xfs_inode_t *ip, /* incore inode pointer */
  1087. xfs_extnum_t idx, /* index to begin removing exts */
  1088. int ext_diff, /* number of extents to remove */
  1089. int state) /* type of extent conversion */
  1090. {
  1091. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  1092. xfs_extnum_t nextents; /* number of extents in file */
  1093. int new_size; /* size of extents after removal */
  1094. trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
  1095. ASSERT(ext_diff > 0);
  1096. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1097. new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
  1098. if (new_size == 0) {
  1099. xfs_iext_destroy(ifp);
  1100. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  1101. xfs_iext_remove_indirect(ifp, idx, ext_diff);
  1102. } else if (ifp->if_real_bytes) {
  1103. xfs_iext_remove_direct(ifp, idx, ext_diff);
  1104. } else {
  1105. xfs_iext_remove_inline(ifp, idx, ext_diff);
  1106. }
  1107. ifp->if_bytes = new_size;
  1108. }
  1109. /*
  1110. * This removes ext_diff extents from the inline buffer, beginning
  1111. * at extent index idx.
  1112. */
  1113. void
  1114. xfs_iext_remove_inline(
  1115. xfs_ifork_t *ifp, /* inode fork pointer */
  1116. xfs_extnum_t idx, /* index to begin removing exts */
  1117. int ext_diff) /* number of extents to remove */
  1118. {
  1119. int nextents; /* number of extents in file */
  1120. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  1121. ASSERT(idx < XFS_INLINE_EXTS);
  1122. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1123. ASSERT(((nextents - ext_diff) > 0) &&
  1124. (nextents - ext_diff) < XFS_INLINE_EXTS);
  1125. if (idx + ext_diff < nextents) {
  1126. memmove(&ifp->if_u2.if_inline_ext[idx],
  1127. &ifp->if_u2.if_inline_ext[idx + ext_diff],
  1128. (nextents - (idx + ext_diff)) *
  1129. sizeof(xfs_bmbt_rec_t));
  1130. memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
  1131. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  1132. } else {
  1133. memset(&ifp->if_u2.if_inline_ext[idx], 0,
  1134. ext_diff * sizeof(xfs_bmbt_rec_t));
  1135. }
  1136. }
  1137. /*
  1138. * This removes ext_diff extents from a linear (direct) extent list,
  1139. * beginning at extent index idx. If the extents are being removed
  1140. * from the end of the list (ie. truncate) then we just need to re-
  1141. * allocate the list to remove the extra space. Otherwise, if the
  1142. * extents are being removed from the middle of the existing extent
  1143. * entries, then we first need to move the extent records beginning
  1144. * at idx + ext_diff up in the list to overwrite the records being
  1145. * removed, then remove the extra space via kmem_realloc.
  1146. */
  1147. void
  1148. xfs_iext_remove_direct(
  1149. xfs_ifork_t *ifp, /* inode fork pointer */
  1150. xfs_extnum_t idx, /* index to begin removing exts */
  1151. int ext_diff) /* number of extents to remove */
  1152. {
  1153. xfs_extnum_t nextents; /* number of extents in file */
  1154. int new_size; /* size of extents after removal */
  1155. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  1156. new_size = ifp->if_bytes -
  1157. (ext_diff * sizeof(xfs_bmbt_rec_t));
  1158. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1159. if (new_size == 0) {
  1160. xfs_iext_destroy(ifp);
  1161. return;
  1162. }
  1163. /* Move extents up in the list (if needed) */
  1164. if (idx + ext_diff < nextents) {
  1165. memmove(&ifp->if_u1.if_extents[idx],
  1166. &ifp->if_u1.if_extents[idx + ext_diff],
  1167. (nextents - (idx + ext_diff)) *
  1168. sizeof(xfs_bmbt_rec_t));
  1169. }
  1170. memset(&ifp->if_u1.if_extents[nextents - ext_diff],
  1171. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  1172. /*
  1173. * Reallocate the direct extent list. If the extents
  1174. * will fit inside the inode then xfs_iext_realloc_direct
  1175. * will switch from direct to inline extent allocation
  1176. * mode for us.
  1177. */
  1178. xfs_iext_realloc_direct(ifp, new_size);
  1179. ifp->if_bytes = new_size;
  1180. }
  1181. /*
  1182. * This is called when incore extents are being removed from the
  1183. * indirection array and the extents being removed span multiple extent
  1184. * buffers. The idx parameter contains the file extent index where we
  1185. * want to begin removing extents, and the count parameter contains
  1186. * how many extents need to be removed.
  1187. *
  1188. * |-------| |-------|
  1189. * | nex1 | | | nex1 - number of extents before idx
  1190. * |-------| | count |
  1191. * | | | | count - number of extents being removed at idx
  1192. * | count | |-------|
  1193. * | | | nex2 | nex2 - number of extents after idx + count
  1194. * |-------| |-------|
  1195. */
  1196. void
  1197. xfs_iext_remove_indirect(
  1198. xfs_ifork_t *ifp, /* inode fork pointer */
  1199. xfs_extnum_t idx, /* index to begin removing extents */
  1200. int count) /* number of extents to remove */
  1201. {
  1202. xfs_ext_irec_t *erp; /* indirection array pointer */
  1203. int erp_idx = 0; /* indirection array index */
  1204. xfs_extnum_t ext_cnt; /* extents left to remove */
  1205. xfs_extnum_t ext_diff; /* extents to remove in current list */
  1206. xfs_extnum_t nex1; /* number of extents before idx */
  1207. xfs_extnum_t nex2; /* extents after idx + count */
  1208. int page_idx = idx; /* index in target extent list */
  1209. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1210. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  1211. ASSERT(erp != NULL);
  1212. nex1 = page_idx;
  1213. ext_cnt = count;
  1214. while (ext_cnt) {
  1215. nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
  1216. ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
  1217. /*
  1218. * Check for deletion of entire list;
  1219. * xfs_iext_irec_remove() updates extent offsets.
  1220. */
  1221. if (ext_diff == erp->er_extcount) {
  1222. xfs_iext_irec_remove(ifp, erp_idx);
  1223. ext_cnt -= ext_diff;
  1224. nex1 = 0;
  1225. if (ext_cnt) {
  1226. ASSERT(erp_idx < ifp->if_real_bytes /
  1227. XFS_IEXT_BUFSZ);
  1228. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1229. nex1 = 0;
  1230. continue;
  1231. } else {
  1232. break;
  1233. }
  1234. }
  1235. /* Move extents up (if needed) */
  1236. if (nex2) {
  1237. memmove(&erp->er_extbuf[nex1],
  1238. &erp->er_extbuf[nex1 + ext_diff],
  1239. nex2 * sizeof(xfs_bmbt_rec_t));
  1240. }
  1241. /* Zero out rest of page */
  1242. memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
  1243. ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
  1244. /* Update remaining counters */
  1245. erp->er_extcount -= ext_diff;
  1246. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
  1247. ext_cnt -= ext_diff;
  1248. nex1 = 0;
  1249. erp_idx++;
  1250. erp++;
  1251. }
  1252. ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
  1253. xfs_iext_irec_compact(ifp);
  1254. }
  1255. /*
  1256. * Create, destroy, or resize a linear (direct) block of extents.
  1257. */
  1258. void
  1259. xfs_iext_realloc_direct(
  1260. xfs_ifork_t *ifp, /* inode fork pointer */
  1261. int new_size) /* new size of extents after adding */
  1262. {
  1263. int rnew_size; /* real new size of extents */
  1264. rnew_size = new_size;
  1265. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
  1266. ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
  1267. (new_size != ifp->if_real_bytes)));
  1268. /* Free extent records */
  1269. if (new_size == 0) {
  1270. xfs_iext_destroy(ifp);
  1271. }
  1272. /* Resize direct extent list and zero any new bytes */
  1273. else if (ifp->if_real_bytes) {
  1274. /* Check if extents will fit inside the inode */
  1275. if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
  1276. xfs_iext_direct_to_inline(ifp, new_size /
  1277. (uint)sizeof(xfs_bmbt_rec_t));
  1278. ifp->if_bytes = new_size;
  1279. return;
  1280. }
  1281. if (!is_power_of_2(new_size)){
  1282. rnew_size = roundup_pow_of_two(new_size);
  1283. }
  1284. if (rnew_size != ifp->if_real_bytes) {
  1285. ifp->if_u1.if_extents =
  1286. kmem_realloc(ifp->if_u1.if_extents,
  1287. rnew_size,
  1288. ifp->if_real_bytes, KM_NOFS);
  1289. }
  1290. if (rnew_size > ifp->if_real_bytes) {
  1291. memset(&ifp->if_u1.if_extents[ifp->if_bytes /
  1292. (uint)sizeof(xfs_bmbt_rec_t)], 0,
  1293. rnew_size - ifp->if_real_bytes);
  1294. }
  1295. }
  1296. /* Switch from the inline extent buffer to a direct extent list */
  1297. else {
  1298. if (!is_power_of_2(new_size)) {
  1299. rnew_size = roundup_pow_of_two(new_size);
  1300. }
  1301. xfs_iext_inline_to_direct(ifp, rnew_size);
  1302. }
  1303. ifp->if_real_bytes = rnew_size;
  1304. ifp->if_bytes = new_size;
  1305. }
  1306. /*
  1307. * Switch from linear (direct) extent records to inline buffer.
  1308. */
  1309. void
  1310. xfs_iext_direct_to_inline(
  1311. xfs_ifork_t *ifp, /* inode fork pointer */
  1312. xfs_extnum_t nextents) /* number of extents in file */
  1313. {
  1314. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  1315. ASSERT(nextents <= XFS_INLINE_EXTS);
  1316. /*
  1317. * The inline buffer was zeroed when we switched
  1318. * from inline to direct extent allocation mode,
  1319. * so we don't need to clear it here.
  1320. */
  1321. memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
  1322. nextents * sizeof(xfs_bmbt_rec_t));
  1323. kmem_free(ifp->if_u1.if_extents);
  1324. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  1325. ifp->if_real_bytes = 0;
  1326. }
  1327. /*
  1328. * Switch from inline buffer to linear (direct) extent records.
  1329. * new_size should already be rounded up to the next power of 2
  1330. * by the caller (when appropriate), so use new_size as it is.
  1331. * However, since new_size may be rounded up, we can't update
  1332. * if_bytes here. It is the caller's responsibility to update
  1333. * if_bytes upon return.
  1334. */
  1335. void
  1336. xfs_iext_inline_to_direct(
  1337. xfs_ifork_t *ifp, /* inode fork pointer */
  1338. int new_size) /* number of extents in file */
  1339. {
  1340. ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
  1341. memset(ifp->if_u1.if_extents, 0, new_size);
  1342. if (ifp->if_bytes) {
  1343. memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
  1344. ifp->if_bytes);
  1345. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  1346. sizeof(xfs_bmbt_rec_t));
  1347. }
  1348. ifp->if_real_bytes = new_size;
  1349. }
  1350. /*
  1351. * Resize an extent indirection array to new_size bytes.
  1352. */
  1353. STATIC void
  1354. xfs_iext_realloc_indirect(
  1355. xfs_ifork_t *ifp, /* inode fork pointer */
  1356. int new_size) /* new indirection array size */
  1357. {
  1358. int nlists; /* number of irec's (ex lists) */
  1359. int size; /* current indirection array size */
  1360. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1361. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1362. size = nlists * sizeof(xfs_ext_irec_t);
  1363. ASSERT(ifp->if_real_bytes);
  1364. ASSERT((new_size >= 0) && (new_size != size));
  1365. if (new_size == 0) {
  1366. xfs_iext_destroy(ifp);
  1367. } else {
  1368. ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
  1369. kmem_realloc(ifp->if_u1.if_ext_irec,
  1370. new_size, size, KM_NOFS);
  1371. }
  1372. }
  1373. /*
  1374. * Switch from indirection array to linear (direct) extent allocations.
  1375. */
  1376. STATIC void
  1377. xfs_iext_indirect_to_direct(
  1378. xfs_ifork_t *ifp) /* inode fork pointer */
  1379. {
  1380. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  1381. xfs_extnum_t nextents; /* number of extents in file */
  1382. int size; /* size of file extents */
  1383. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1384. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1385. ASSERT(nextents <= XFS_LINEAR_EXTS);
  1386. size = nextents * sizeof(xfs_bmbt_rec_t);
  1387. xfs_iext_irec_compact_pages(ifp);
  1388. ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
  1389. ep = ifp->if_u1.if_ext_irec->er_extbuf;
  1390. kmem_free(ifp->if_u1.if_ext_irec);
  1391. ifp->if_flags &= ~XFS_IFEXTIREC;
  1392. ifp->if_u1.if_extents = ep;
  1393. ifp->if_bytes = size;
  1394. if (nextents < XFS_LINEAR_EXTS) {
  1395. xfs_iext_realloc_direct(ifp, size);
  1396. }
  1397. }
  1398. /*
  1399. * Free incore file extents.
  1400. */
  1401. void
  1402. xfs_iext_destroy(
  1403. xfs_ifork_t *ifp) /* inode fork pointer */
  1404. {
  1405. if (ifp->if_flags & XFS_IFEXTIREC) {
  1406. int erp_idx;
  1407. int nlists;
  1408. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1409. for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
  1410. xfs_iext_irec_remove(ifp, erp_idx);
  1411. }
  1412. ifp->if_flags &= ~XFS_IFEXTIREC;
  1413. } else if (ifp->if_real_bytes) {
  1414. kmem_free(ifp->if_u1.if_extents);
  1415. } else if (ifp->if_bytes) {
  1416. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  1417. sizeof(xfs_bmbt_rec_t));
  1418. }
  1419. ifp->if_u1.if_extents = NULL;
  1420. ifp->if_real_bytes = 0;
  1421. ifp->if_bytes = 0;
  1422. }
  1423. /*
  1424. * Return a pointer to the extent record for file system block bno.
  1425. */
  1426. xfs_bmbt_rec_host_t * /* pointer to found extent record */
  1427. xfs_iext_bno_to_ext(
  1428. xfs_ifork_t *ifp, /* inode fork pointer */
  1429. xfs_fileoff_t bno, /* block number to search for */
  1430. xfs_extnum_t *idxp) /* index of target extent */
  1431. {
  1432. xfs_bmbt_rec_host_t *base; /* pointer to first extent */
  1433. xfs_filblks_t blockcount = 0; /* number of blocks in extent */
  1434. xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
  1435. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  1436. int high; /* upper boundary in search */
  1437. xfs_extnum_t idx = 0; /* index of target extent */
  1438. int low; /* lower boundary in search */
  1439. xfs_extnum_t nextents; /* number of file extents */
  1440. xfs_fileoff_t startoff = 0; /* start offset of extent */
  1441. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1442. if (nextents == 0) {
  1443. *idxp = 0;
  1444. return NULL;
  1445. }
  1446. low = 0;
  1447. if (ifp->if_flags & XFS_IFEXTIREC) {
  1448. /* Find target extent list */
  1449. int erp_idx = 0;
  1450. erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
  1451. base = erp->er_extbuf;
  1452. high = erp->er_extcount - 1;
  1453. } else {
  1454. base = ifp->if_u1.if_extents;
  1455. high = nextents - 1;
  1456. }
  1457. /* Binary search extent records */
  1458. while (low <= high) {
  1459. idx = (low + high) >> 1;
  1460. ep = base + idx;
  1461. startoff = xfs_bmbt_get_startoff(ep);
  1462. blockcount = xfs_bmbt_get_blockcount(ep);
  1463. if (bno < startoff) {
  1464. high = idx - 1;
  1465. } else if (bno >= startoff + blockcount) {
  1466. low = idx + 1;
  1467. } else {
  1468. /* Convert back to file-based extent index */
  1469. if (ifp->if_flags & XFS_IFEXTIREC) {
  1470. idx += erp->er_extoff;
  1471. }
  1472. *idxp = idx;
  1473. return ep;
  1474. }
  1475. }
  1476. /* Convert back to file-based extent index */
  1477. if (ifp->if_flags & XFS_IFEXTIREC) {
  1478. idx += erp->er_extoff;
  1479. }
  1480. if (bno >= startoff + blockcount) {
  1481. if (++idx == nextents) {
  1482. ep = NULL;
  1483. } else {
  1484. ep = xfs_iext_get_ext(ifp, idx);
  1485. }
  1486. }
  1487. *idxp = idx;
  1488. return ep;
  1489. }
  1490. /*
  1491. * Return a pointer to the indirection array entry containing the
  1492. * extent record for filesystem block bno. Store the index of the
  1493. * target irec in *erp_idxp.
  1494. */
  1495. xfs_ext_irec_t * /* pointer to found extent record */
  1496. xfs_iext_bno_to_irec(
  1497. xfs_ifork_t *ifp, /* inode fork pointer */
  1498. xfs_fileoff_t bno, /* block number to search for */
  1499. int *erp_idxp) /* irec index of target ext list */
  1500. {
  1501. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  1502. xfs_ext_irec_t *erp_next; /* next indirection array entry */
  1503. int erp_idx; /* indirection array index */
  1504. int nlists; /* number of extent irec's (lists) */
  1505. int high; /* binary search upper limit */
  1506. int low; /* binary search lower limit */
  1507. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1508. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1509. erp_idx = 0;
  1510. low = 0;
  1511. high = nlists - 1;
  1512. while (low <= high) {
  1513. erp_idx = (low + high) >> 1;
  1514. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1515. erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
  1516. if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
  1517. high = erp_idx - 1;
  1518. } else if (erp_next && bno >=
  1519. xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
  1520. low = erp_idx + 1;
  1521. } else {
  1522. break;
  1523. }
  1524. }
  1525. *erp_idxp = erp_idx;
  1526. return erp;
  1527. }
  1528. /*
  1529. * Return a pointer to the indirection array entry containing the
  1530. * extent record at file extent index *idxp. Store the index of the
  1531. * target irec in *erp_idxp and store the page index of the target
  1532. * extent record in *idxp.
  1533. */
  1534. xfs_ext_irec_t *
  1535. xfs_iext_idx_to_irec(
  1536. xfs_ifork_t *ifp, /* inode fork pointer */
  1537. xfs_extnum_t *idxp, /* extent index (file -> page) */
  1538. int *erp_idxp, /* pointer to target irec */
  1539. int realloc) /* new bytes were just added */
  1540. {
  1541. xfs_ext_irec_t *prev; /* pointer to previous irec */
  1542. xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
  1543. int erp_idx; /* indirection array index */
  1544. int nlists; /* number of irec's (ex lists) */
  1545. int high; /* binary search upper limit */
  1546. int low; /* binary search lower limit */
  1547. xfs_extnum_t page_idx = *idxp; /* extent index in target list */
  1548. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1549. ASSERT(page_idx >= 0);
  1550. ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  1551. ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
  1552. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1553. erp_idx = 0;
  1554. low = 0;
  1555. high = nlists - 1;
  1556. /* Binary search extent irec's */
  1557. while (low <= high) {
  1558. erp_idx = (low + high) >> 1;
  1559. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1560. prev = erp_idx > 0 ? erp - 1 : NULL;
  1561. if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
  1562. realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
  1563. high = erp_idx - 1;
  1564. } else if (page_idx > erp->er_extoff + erp->er_extcount ||
  1565. (page_idx == erp->er_extoff + erp->er_extcount &&
  1566. !realloc)) {
  1567. low = erp_idx + 1;
  1568. } else if (page_idx == erp->er_extoff + erp->er_extcount &&
  1569. erp->er_extcount == XFS_LINEAR_EXTS) {
  1570. ASSERT(realloc);
  1571. page_idx = 0;
  1572. erp_idx++;
  1573. erp = erp_idx < nlists ? erp + 1 : NULL;
  1574. break;
  1575. } else {
  1576. page_idx -= erp->er_extoff;
  1577. break;
  1578. }
  1579. }
  1580. *idxp = page_idx;
  1581. *erp_idxp = erp_idx;
  1582. return(erp);
  1583. }
  1584. /*
  1585. * Allocate and initialize an indirection array once the space needed
  1586. * for incore extents increases above XFS_IEXT_BUFSZ.
  1587. */
  1588. void
  1589. xfs_iext_irec_init(
  1590. xfs_ifork_t *ifp) /* inode fork pointer */
  1591. {
  1592. xfs_ext_irec_t *erp; /* indirection array pointer */
  1593. xfs_extnum_t nextents; /* number of extents in file */
  1594. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  1595. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1596. ASSERT(nextents <= XFS_LINEAR_EXTS);
  1597. erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
  1598. if (nextents == 0) {
  1599. ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  1600. } else if (!ifp->if_real_bytes) {
  1601. xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
  1602. } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
  1603. xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
  1604. }
  1605. erp->er_extbuf = ifp->if_u1.if_extents;
  1606. erp->er_extcount = nextents;
  1607. erp->er_extoff = 0;
  1608. ifp->if_flags |= XFS_IFEXTIREC;
  1609. ifp->if_real_bytes = XFS_IEXT_BUFSZ;
  1610. ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
  1611. ifp->if_u1.if_ext_irec = erp;
  1612. return;
  1613. }
  1614. /*
  1615. * Allocate and initialize a new entry in the indirection array.
  1616. */
  1617. xfs_ext_irec_t *
  1618. xfs_iext_irec_new(
  1619. xfs_ifork_t *ifp, /* inode fork pointer */
  1620. int erp_idx) /* index for new irec */
  1621. {
  1622. xfs_ext_irec_t *erp; /* indirection array pointer */
  1623. int i; /* loop counter */
  1624. int nlists; /* number of irec's (ex lists) */
  1625. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1626. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1627. /* Resize indirection array */
  1628. xfs_iext_realloc_indirect(ifp, ++nlists *
  1629. sizeof(xfs_ext_irec_t));
  1630. /*
  1631. * Move records down in the array so the
  1632. * new page can use erp_idx.
  1633. */
  1634. erp = ifp->if_u1.if_ext_irec;
  1635. for (i = nlists - 1; i > erp_idx; i--) {
  1636. memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
  1637. }
  1638. ASSERT(i == erp_idx);
  1639. /* Initialize new extent record */
  1640. erp = ifp->if_u1.if_ext_irec;
  1641. erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  1642. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  1643. memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
  1644. erp[erp_idx].er_extcount = 0;
  1645. erp[erp_idx].er_extoff = erp_idx > 0 ?
  1646. erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
  1647. return (&erp[erp_idx]);
  1648. }
  1649. /*
  1650. * Remove a record from the indirection array.
  1651. */
  1652. void
  1653. xfs_iext_irec_remove(
  1654. xfs_ifork_t *ifp, /* inode fork pointer */
  1655. int erp_idx) /* irec index to remove */
  1656. {
  1657. xfs_ext_irec_t *erp; /* indirection array pointer */
  1658. int i; /* loop counter */
  1659. int nlists; /* number of irec's (ex lists) */
  1660. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1661. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1662. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1663. if (erp->er_extbuf) {
  1664. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
  1665. -erp->er_extcount);
  1666. kmem_free(erp->er_extbuf);
  1667. }
  1668. /* Compact extent records */
  1669. erp = ifp->if_u1.if_ext_irec;
  1670. for (i = erp_idx; i < nlists - 1; i++) {
  1671. memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
  1672. }
  1673. /*
  1674. * Manually free the last extent record from the indirection
  1675. * array. A call to xfs_iext_realloc_indirect() with a size
  1676. * of zero would result in a call to xfs_iext_destroy() which
  1677. * would in turn call this function again, creating a nasty
  1678. * infinite loop.
  1679. */
  1680. if (--nlists) {
  1681. xfs_iext_realloc_indirect(ifp,
  1682. nlists * sizeof(xfs_ext_irec_t));
  1683. } else {
  1684. kmem_free(ifp->if_u1.if_ext_irec);
  1685. }
  1686. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  1687. }
  1688. /*
  1689. * This is called to clean up large amounts of unused memory allocated
  1690. * by the indirection array. Before compacting anything though, verify
  1691. * that the indirection array is still needed and switch back to the
  1692. * linear extent list (or even the inline buffer) if possible. The
  1693. * compaction policy is as follows:
  1694. *
  1695. * Full Compaction: Extents fit into a single page (or inline buffer)
  1696. * Partial Compaction: Extents occupy less than 50% of allocated space
  1697. * No Compaction: Extents occupy at least 50% of allocated space
  1698. */
  1699. void
  1700. xfs_iext_irec_compact(
  1701. xfs_ifork_t *ifp) /* inode fork pointer */
  1702. {
  1703. xfs_extnum_t nextents; /* number of extents in file */
  1704. int nlists; /* number of irec's (ex lists) */
  1705. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1706. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1707. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1708. if (nextents == 0) {
  1709. xfs_iext_destroy(ifp);
  1710. } else if (nextents <= XFS_INLINE_EXTS) {
  1711. xfs_iext_indirect_to_direct(ifp);
  1712. xfs_iext_direct_to_inline(ifp, nextents);
  1713. } else if (nextents <= XFS_LINEAR_EXTS) {
  1714. xfs_iext_indirect_to_direct(ifp);
  1715. } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
  1716. xfs_iext_irec_compact_pages(ifp);
  1717. }
  1718. }
  1719. /*
  1720. * Combine extents from neighboring extent pages.
  1721. */
  1722. void
  1723. xfs_iext_irec_compact_pages(
  1724. xfs_ifork_t *ifp) /* inode fork pointer */
  1725. {
  1726. xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
  1727. int erp_idx = 0; /* indirection array index */
  1728. int nlists; /* number of irec's (ex lists) */
  1729. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1730. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1731. while (erp_idx < nlists - 1) {
  1732. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1733. erp_next = erp + 1;
  1734. if (erp_next->er_extcount <=
  1735. (XFS_LINEAR_EXTS - erp->er_extcount)) {
  1736. memcpy(&erp->er_extbuf[erp->er_extcount],
  1737. erp_next->er_extbuf, erp_next->er_extcount *
  1738. sizeof(xfs_bmbt_rec_t));
  1739. erp->er_extcount += erp_next->er_extcount;
  1740. /*
  1741. * Free page before removing extent record
  1742. * so er_extoffs don't get modified in
  1743. * xfs_iext_irec_remove.
  1744. */
  1745. kmem_free(erp_next->er_extbuf);
  1746. erp_next->er_extbuf = NULL;
  1747. xfs_iext_irec_remove(ifp, erp_idx + 1);
  1748. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1749. } else {
  1750. erp_idx++;
  1751. }
  1752. }
  1753. }
  1754. /*
  1755. * This is called to update the er_extoff field in the indirection
  1756. * array when extents have been added or removed from one of the
  1757. * extent lists. erp_idx contains the irec index to begin updating
  1758. * at and ext_diff contains the number of extents that were added
  1759. * or removed.
  1760. */
  1761. void
  1762. xfs_iext_irec_update_extoffs(
  1763. xfs_ifork_t *ifp, /* inode fork pointer */
  1764. int erp_idx, /* irec index to update */
  1765. int ext_diff) /* number of new extents */
  1766. {
  1767. int i; /* loop counter */
  1768. int nlists; /* number of irec's (ex lists */
  1769. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1770. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1771. for (i = erp_idx; i < nlists; i++) {
  1772. ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
  1773. }
  1774. }