xfs_inode_fork.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  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. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  402. if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  403. XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
  404. ip->i_mount);
  405. return XFS_ERROR(EFSCORRUPTED);
  406. }
  407. nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
  408. ifp = XFS_IFORK_PTR(ip, whichfork);
  409. /*
  410. * We know that the size is valid (it's checked in iformat_btree)
  411. */
  412. ifp->if_bytes = ifp->if_real_bytes = 0;
  413. ifp->if_flags |= XFS_IFEXTENTS;
  414. xfs_iext_add(ifp, 0, nextents);
  415. error = xfs_bmap_read_extents(tp, ip, whichfork);
  416. if (error) {
  417. xfs_iext_destroy(ifp);
  418. ifp->if_flags &= ~XFS_IFEXTENTS;
  419. return error;
  420. }
  421. xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
  422. return 0;
  423. }
  424. /*
  425. * Reallocate the space for if_broot based on the number of records
  426. * being added or deleted as indicated in rec_diff. Move the records
  427. * and pointers in if_broot to fit the new size. When shrinking this
  428. * will eliminate holes between the records and pointers created by
  429. * the caller. When growing this will create holes to be filled in
  430. * by the caller.
  431. *
  432. * The caller must not request to add more records than would fit in
  433. * the on-disk inode root. If the if_broot is currently NULL, then
  434. * if we are adding records, one will be allocated. The caller must also
  435. * not request that the number of records go below zero, although
  436. * it can go to zero.
  437. *
  438. * ip -- the inode whose if_broot area is changing
  439. * ext_diff -- the change in the number of records, positive or negative,
  440. * requested for the if_broot array.
  441. */
  442. void
  443. xfs_iroot_realloc(
  444. xfs_inode_t *ip,
  445. int rec_diff,
  446. int whichfork)
  447. {
  448. struct xfs_mount *mp = ip->i_mount;
  449. int cur_max;
  450. xfs_ifork_t *ifp;
  451. struct xfs_btree_block *new_broot;
  452. int new_max;
  453. size_t new_size;
  454. char *np;
  455. char *op;
  456. /*
  457. * Handle the degenerate case quietly.
  458. */
  459. if (rec_diff == 0) {
  460. return;
  461. }
  462. ifp = XFS_IFORK_PTR(ip, whichfork);
  463. if (rec_diff > 0) {
  464. /*
  465. * If there wasn't any memory allocated before, just
  466. * allocate it now and get out.
  467. */
  468. if (ifp->if_broot_bytes == 0) {
  469. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
  470. ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  471. ifp->if_broot_bytes = (int)new_size;
  472. return;
  473. }
  474. /*
  475. * If there is already an existing if_broot, then we need
  476. * to realloc() it and shift the pointers to their new
  477. * location. The records don't change location because
  478. * they are kept butted up against the btree block header.
  479. */
  480. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  481. new_max = cur_max + rec_diff;
  482. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  483. ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
  484. XFS_BMAP_BROOT_SPACE_CALC(mp, cur_max),
  485. KM_SLEEP | KM_NOFS);
  486. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  487. ifp->if_broot_bytes);
  488. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  489. (int)new_size);
  490. ifp->if_broot_bytes = (int)new_size;
  491. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  492. XFS_IFORK_SIZE(ip, whichfork));
  493. memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
  494. return;
  495. }
  496. /*
  497. * rec_diff is less than 0. In this case, we are shrinking the
  498. * if_broot buffer. It must already exist. If we go to zero
  499. * records, just get rid of the root and clear the status bit.
  500. */
  501. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  502. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  503. new_max = cur_max + rec_diff;
  504. ASSERT(new_max >= 0);
  505. if (new_max > 0)
  506. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  507. else
  508. new_size = 0;
  509. if (new_size > 0) {
  510. new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  511. /*
  512. * First copy over the btree block header.
  513. */
  514. memcpy(new_broot, ifp->if_broot,
  515. XFS_BMBT_BLOCK_LEN(ip->i_mount));
  516. } else {
  517. new_broot = NULL;
  518. ifp->if_flags &= ~XFS_IFBROOT;
  519. }
  520. /*
  521. * Only copy the records and pointers if there are any.
  522. */
  523. if (new_max > 0) {
  524. /*
  525. * First copy the records.
  526. */
  527. op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
  528. np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
  529. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  530. /*
  531. * Then copy the pointers.
  532. */
  533. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  534. ifp->if_broot_bytes);
  535. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
  536. (int)new_size);
  537. memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
  538. }
  539. kmem_free(ifp->if_broot);
  540. ifp->if_broot = new_broot;
  541. ifp->if_broot_bytes = (int)new_size;
  542. if (ifp->if_broot)
  543. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  544. XFS_IFORK_SIZE(ip, whichfork));
  545. return;
  546. }
  547. /*
  548. * This is called when the amount of space needed for if_data
  549. * is increased or decreased. The change in size is indicated by
  550. * the number of bytes that need to be added or deleted in the
  551. * byte_diff parameter.
  552. *
  553. * If the amount of space needed has decreased below the size of the
  554. * inline buffer, then switch to using the inline buffer. Otherwise,
  555. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  556. * to what is needed.
  557. *
  558. * ip -- the inode whose if_data area is changing
  559. * byte_diff -- the change in the number of bytes, positive or negative,
  560. * requested for the if_data array.
  561. */
  562. void
  563. xfs_idata_realloc(
  564. xfs_inode_t *ip,
  565. int byte_diff,
  566. int whichfork)
  567. {
  568. xfs_ifork_t *ifp;
  569. int new_size;
  570. int real_size;
  571. if (byte_diff == 0) {
  572. return;
  573. }
  574. ifp = XFS_IFORK_PTR(ip, whichfork);
  575. new_size = (int)ifp->if_bytes + byte_diff;
  576. ASSERT(new_size >= 0);
  577. if (new_size == 0) {
  578. if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  579. kmem_free(ifp->if_u1.if_data);
  580. }
  581. ifp->if_u1.if_data = NULL;
  582. real_size = 0;
  583. } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
  584. /*
  585. * If the valid extents/data can fit in if_inline_ext/data,
  586. * copy them from the malloc'd vector and free it.
  587. */
  588. if (ifp->if_u1.if_data == NULL) {
  589. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  590. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  591. ASSERT(ifp->if_real_bytes != 0);
  592. memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
  593. new_size);
  594. kmem_free(ifp->if_u1.if_data);
  595. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  596. }
  597. real_size = 0;
  598. } else {
  599. /*
  600. * Stuck with malloc/realloc.
  601. * For inline data, the underlying buffer must be
  602. * a multiple of 4 bytes in size so that it can be
  603. * logged and stay on word boundaries. We enforce
  604. * that here.
  605. */
  606. real_size = roundup(new_size, 4);
  607. if (ifp->if_u1.if_data == NULL) {
  608. ASSERT(ifp->if_real_bytes == 0);
  609. ifp->if_u1.if_data = kmem_alloc(real_size,
  610. KM_SLEEP | KM_NOFS);
  611. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  612. /*
  613. * Only do the realloc if the underlying size
  614. * is really changing.
  615. */
  616. if (ifp->if_real_bytes != real_size) {
  617. ifp->if_u1.if_data =
  618. kmem_realloc(ifp->if_u1.if_data,
  619. real_size,
  620. ifp->if_real_bytes,
  621. KM_SLEEP | KM_NOFS);
  622. }
  623. } else {
  624. ASSERT(ifp->if_real_bytes == 0);
  625. ifp->if_u1.if_data = kmem_alloc(real_size,
  626. KM_SLEEP | KM_NOFS);
  627. memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
  628. ifp->if_bytes);
  629. }
  630. }
  631. ifp->if_real_bytes = real_size;
  632. ifp->if_bytes = new_size;
  633. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  634. }
  635. void
  636. xfs_idestroy_fork(
  637. xfs_inode_t *ip,
  638. int whichfork)
  639. {
  640. xfs_ifork_t *ifp;
  641. ifp = XFS_IFORK_PTR(ip, whichfork);
  642. if (ifp->if_broot != NULL) {
  643. kmem_free(ifp->if_broot);
  644. ifp->if_broot = NULL;
  645. }
  646. /*
  647. * If the format is local, then we can't have an extents
  648. * array so just look for an inline data array. If we're
  649. * not local then we may or may not have an extents list,
  650. * so check and free it up if we do.
  651. */
  652. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  653. if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
  654. (ifp->if_u1.if_data != NULL)) {
  655. ASSERT(ifp->if_real_bytes != 0);
  656. kmem_free(ifp->if_u1.if_data);
  657. ifp->if_u1.if_data = NULL;
  658. ifp->if_real_bytes = 0;
  659. }
  660. } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
  661. ((ifp->if_flags & XFS_IFEXTIREC) ||
  662. ((ifp->if_u1.if_extents != NULL) &&
  663. (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
  664. ASSERT(ifp->if_real_bytes != 0);
  665. xfs_iext_destroy(ifp);
  666. }
  667. ASSERT(ifp->if_u1.if_extents == NULL ||
  668. ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
  669. ASSERT(ifp->if_real_bytes == 0);
  670. if (whichfork == XFS_ATTR_FORK) {
  671. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  672. ip->i_afp = NULL;
  673. }
  674. }
  675. /*
  676. * Convert in-core extents to on-disk form
  677. *
  678. * For either the data or attr fork in extent format, we need to endian convert
  679. * the in-core extent as we place them into the on-disk inode.
  680. *
  681. * In the case of the data fork, the in-core and on-disk fork sizes can be
  682. * different due to delayed allocation extents. We only copy on-disk extents
  683. * here, so callers must always use the physical fork size to determine the
  684. * size of the buffer passed to this routine. We will return the size actually
  685. * used.
  686. */
  687. int
  688. xfs_iextents_copy(
  689. xfs_inode_t *ip,
  690. xfs_bmbt_rec_t *dp,
  691. int whichfork)
  692. {
  693. int copied;
  694. int i;
  695. xfs_ifork_t *ifp;
  696. int nrecs;
  697. xfs_fsblock_t start_block;
  698. ifp = XFS_IFORK_PTR(ip, whichfork);
  699. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  700. ASSERT(ifp->if_bytes > 0);
  701. nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  702. XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
  703. ASSERT(nrecs > 0);
  704. /*
  705. * There are some delayed allocation extents in the
  706. * inode, so copy the extents one at a time and skip
  707. * the delayed ones. There must be at least one
  708. * non-delayed extent.
  709. */
  710. copied = 0;
  711. for (i = 0; i < nrecs; i++) {
  712. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  713. start_block = xfs_bmbt_get_startblock(ep);
  714. if (isnullstartblock(start_block)) {
  715. /*
  716. * It's a delayed allocation extent, so skip it.
  717. */
  718. continue;
  719. }
  720. /* Translate to on disk format */
  721. put_unaligned_be64(ep->l0, &dp->l0);
  722. put_unaligned_be64(ep->l1, &dp->l1);
  723. dp++;
  724. copied++;
  725. }
  726. ASSERT(copied != 0);
  727. xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
  728. return (copied * (uint)sizeof(xfs_bmbt_rec_t));
  729. }
  730. /*
  731. * Each of the following cases stores data into the same region
  732. * of the on-disk inode, so only one of them can be valid at
  733. * any given time. While it is possible to have conflicting formats
  734. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  735. * in EXTENTS format, this can only happen when the fork has
  736. * changed formats after being modified but before being flushed.
  737. * In these cases, the format always takes precedence, because the
  738. * format indicates the current state of the fork.
  739. */
  740. void
  741. xfs_iflush_fork(
  742. xfs_inode_t *ip,
  743. xfs_dinode_t *dip,
  744. xfs_inode_log_item_t *iip,
  745. int whichfork,
  746. xfs_buf_t *bp)
  747. {
  748. char *cp;
  749. xfs_ifork_t *ifp;
  750. xfs_mount_t *mp;
  751. static const short brootflag[2] =
  752. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  753. static const short dataflag[2] =
  754. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  755. static const short extflag[2] =
  756. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  757. if (!iip)
  758. return;
  759. ifp = XFS_IFORK_PTR(ip, whichfork);
  760. /*
  761. * This can happen if we gave up in iformat in an error path,
  762. * for the attribute fork.
  763. */
  764. if (!ifp) {
  765. ASSERT(whichfork == XFS_ATTR_FORK);
  766. return;
  767. }
  768. cp = XFS_DFORK_PTR(dip, whichfork);
  769. mp = ip->i_mount;
  770. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  771. case XFS_DINODE_FMT_LOCAL:
  772. if ((iip->ili_fields & dataflag[whichfork]) &&
  773. (ifp->if_bytes > 0)) {
  774. ASSERT(ifp->if_u1.if_data != NULL);
  775. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  776. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  777. }
  778. break;
  779. case XFS_DINODE_FMT_EXTENTS:
  780. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  781. !(iip->ili_fields & extflag[whichfork]));
  782. if ((iip->ili_fields & extflag[whichfork]) &&
  783. (ifp->if_bytes > 0)) {
  784. ASSERT(xfs_iext_get_ext(ifp, 0));
  785. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  786. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  787. whichfork);
  788. }
  789. break;
  790. case XFS_DINODE_FMT_BTREE:
  791. if ((iip->ili_fields & brootflag[whichfork]) &&
  792. (ifp->if_broot_bytes > 0)) {
  793. ASSERT(ifp->if_broot != NULL);
  794. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  795. XFS_IFORK_SIZE(ip, whichfork));
  796. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  797. (xfs_bmdr_block_t *)cp,
  798. XFS_DFORK_SIZE(dip, mp, whichfork));
  799. }
  800. break;
  801. case XFS_DINODE_FMT_DEV:
  802. if (iip->ili_fields & XFS_ILOG_DEV) {
  803. ASSERT(whichfork == XFS_DATA_FORK);
  804. xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
  805. }
  806. break;
  807. case XFS_DINODE_FMT_UUID:
  808. if (iip->ili_fields & XFS_ILOG_UUID) {
  809. ASSERT(whichfork == XFS_DATA_FORK);
  810. memcpy(XFS_DFORK_DPTR(dip),
  811. &ip->i_df.if_u2.if_uuid,
  812. sizeof(uuid_t));
  813. }
  814. break;
  815. default:
  816. ASSERT(0);
  817. break;
  818. }
  819. }
  820. /*
  821. * Return a pointer to the extent record at file index idx.
  822. */
  823. xfs_bmbt_rec_host_t *
  824. xfs_iext_get_ext(
  825. xfs_ifork_t *ifp, /* inode fork pointer */
  826. xfs_extnum_t idx) /* index of target extent */
  827. {
  828. ASSERT(idx >= 0);
  829. ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  830. if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
  831. return ifp->if_u1.if_ext_irec->er_extbuf;
  832. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  833. xfs_ext_irec_t *erp; /* irec pointer */
  834. int erp_idx = 0; /* irec index */
  835. xfs_extnum_t page_idx = idx; /* ext index in target list */
  836. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  837. return &erp->er_extbuf[page_idx];
  838. } else if (ifp->if_bytes) {
  839. return &ifp->if_u1.if_extents[idx];
  840. } else {
  841. return NULL;
  842. }
  843. }
  844. /*
  845. * Insert new item(s) into the extent records for incore inode
  846. * fork 'ifp'. 'count' new items are inserted at index 'idx'.
  847. */
  848. void
  849. xfs_iext_insert(
  850. xfs_inode_t *ip, /* incore inode pointer */
  851. xfs_extnum_t idx, /* starting index of new items */
  852. xfs_extnum_t count, /* number of inserted items */
  853. xfs_bmbt_irec_t *new, /* items to insert */
  854. int state) /* type of extent conversion */
  855. {
  856. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  857. xfs_extnum_t i; /* extent record index */
  858. trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
  859. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  860. xfs_iext_add(ifp, idx, count);
  861. for (i = idx; i < idx + count; i++, new++)
  862. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
  863. }
  864. /*
  865. * This is called when the amount of space required for incore file
  866. * extents needs to be increased. The ext_diff parameter stores the
  867. * number of new extents being added and the idx parameter contains
  868. * the extent index where the new extents will be added. If the new
  869. * extents are being appended, then we just need to (re)allocate and
  870. * initialize the space. Otherwise, if the new extents are being
  871. * inserted into the middle of the existing entries, a bit more work
  872. * is required to make room for the new extents to be inserted. The
  873. * caller is responsible for filling in the new extent entries upon
  874. * return.
  875. */
  876. void
  877. xfs_iext_add(
  878. xfs_ifork_t *ifp, /* inode fork pointer */
  879. xfs_extnum_t idx, /* index to begin adding exts */
  880. int ext_diff) /* number of extents to add */
  881. {
  882. int byte_diff; /* new bytes being added */
  883. int new_size; /* size of extents after adding */
  884. xfs_extnum_t nextents; /* number of extents in file */
  885. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  886. ASSERT((idx >= 0) && (idx <= nextents));
  887. byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
  888. new_size = ifp->if_bytes + byte_diff;
  889. /*
  890. * If the new number of extents (nextents + ext_diff)
  891. * fits inside the inode, then continue to use the inline
  892. * extent buffer.
  893. */
  894. if (nextents + ext_diff <= XFS_INLINE_EXTS) {
  895. if (idx < nextents) {
  896. memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
  897. &ifp->if_u2.if_inline_ext[idx],
  898. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  899. memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
  900. }
  901. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  902. ifp->if_real_bytes = 0;
  903. }
  904. /*
  905. * Otherwise use a linear (direct) extent list.
  906. * If the extents are currently inside the inode,
  907. * xfs_iext_realloc_direct will switch us from
  908. * inline to direct extent allocation mode.
  909. */
  910. else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
  911. xfs_iext_realloc_direct(ifp, new_size);
  912. if (idx < nextents) {
  913. memmove(&ifp->if_u1.if_extents[idx + ext_diff],
  914. &ifp->if_u1.if_extents[idx],
  915. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  916. memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
  917. }
  918. }
  919. /* Indirection array */
  920. else {
  921. xfs_ext_irec_t *erp;
  922. int erp_idx = 0;
  923. int page_idx = idx;
  924. ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
  925. if (ifp->if_flags & XFS_IFEXTIREC) {
  926. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
  927. } else {
  928. xfs_iext_irec_init(ifp);
  929. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  930. erp = ifp->if_u1.if_ext_irec;
  931. }
  932. /* Extents fit in target extent page */
  933. if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
  934. if (page_idx < erp->er_extcount) {
  935. memmove(&erp->er_extbuf[page_idx + ext_diff],
  936. &erp->er_extbuf[page_idx],
  937. (erp->er_extcount - page_idx) *
  938. sizeof(xfs_bmbt_rec_t));
  939. memset(&erp->er_extbuf[page_idx], 0, byte_diff);
  940. }
  941. erp->er_extcount += ext_diff;
  942. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  943. }
  944. /* Insert a new extent page */
  945. else if (erp) {
  946. xfs_iext_add_indirect_multi(ifp,
  947. erp_idx, page_idx, ext_diff);
  948. }
  949. /*
  950. * If extent(s) are being appended to the last page in
  951. * the indirection array and the new extent(s) don't fit
  952. * in the page, then erp is NULL and erp_idx is set to
  953. * the next index needed in the indirection array.
  954. */
  955. else {
  956. uint count = ext_diff;
  957. while (count) {
  958. erp = xfs_iext_irec_new(ifp, erp_idx);
  959. erp->er_extcount = min(count, XFS_LINEAR_EXTS);
  960. count -= erp->er_extcount;
  961. if (count)
  962. erp_idx++;
  963. }
  964. }
  965. }
  966. ifp->if_bytes = new_size;
  967. }
  968. /*
  969. * This is called when incore extents are being added to the indirection
  970. * array and the new extents do not fit in the target extent list. The
  971. * erp_idx parameter contains the irec index for the target extent list
  972. * in the indirection array, and the idx parameter contains the extent
  973. * index within the list. The number of extents being added is stored
  974. * in the count parameter.
  975. *
  976. * |-------| |-------|
  977. * | | | | idx - number of extents before idx
  978. * | idx | | count |
  979. * | | | | count - number of extents being inserted at idx
  980. * |-------| |-------|
  981. * | count | | nex2 | nex2 - number of extents after idx + count
  982. * |-------| |-------|
  983. */
  984. void
  985. xfs_iext_add_indirect_multi(
  986. xfs_ifork_t *ifp, /* inode fork pointer */
  987. int erp_idx, /* target extent irec index */
  988. xfs_extnum_t idx, /* index within target list */
  989. int count) /* new extents being added */
  990. {
  991. int byte_diff; /* new bytes being added */
  992. xfs_ext_irec_t *erp; /* pointer to irec entry */
  993. xfs_extnum_t ext_diff; /* number of extents to add */
  994. xfs_extnum_t ext_cnt; /* new extents still needed */
  995. xfs_extnum_t nex2; /* extents after idx + count */
  996. xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
  997. int nlists; /* number of irec's (lists) */
  998. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  999. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1000. nex2 = erp->er_extcount - idx;
  1001. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1002. /*
  1003. * Save second part of target extent list
  1004. * (all extents past */
  1005. if (nex2) {
  1006. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  1007. nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
  1008. memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
  1009. erp->er_extcount -= nex2;
  1010. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
  1011. memset(&erp->er_extbuf[idx], 0, byte_diff);
  1012. }
  1013. /*
  1014. * Add the new extents to the end of the target
  1015. * list, then allocate new irec record(s) and
  1016. * extent buffer(s) as needed to store the rest
  1017. * of the new extents.
  1018. */
  1019. ext_cnt = count;
  1020. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
  1021. if (ext_diff) {
  1022. erp->er_extcount += ext_diff;
  1023. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  1024. ext_cnt -= ext_diff;
  1025. }
  1026. while (ext_cnt) {
  1027. erp_idx++;
  1028. erp = xfs_iext_irec_new(ifp, erp_idx);
  1029. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
  1030. erp->er_extcount = ext_diff;
  1031. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  1032. ext_cnt -= ext_diff;
  1033. }
  1034. /* Add nex2 extents back to indirection array */
  1035. if (nex2) {
  1036. xfs_extnum_t ext_avail;
  1037. int i;
  1038. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  1039. ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
  1040. i = 0;
  1041. /*
  1042. * If nex2 extents fit in the current page, append
  1043. * nex2_ep after the new extents.
  1044. */
  1045. if (nex2 <= ext_avail) {
  1046. i = erp->er_extcount;
  1047. }
  1048. /*
  1049. * Otherwise, check if space is available in the
  1050. * next page.
  1051. */
  1052. else if ((erp_idx < nlists - 1) &&
  1053. (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
  1054. ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
  1055. erp_idx++;
  1056. erp++;
  1057. /* Create a hole for nex2 extents */
  1058. memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
  1059. erp->er_extcount * sizeof(xfs_bmbt_rec_t));
  1060. }
  1061. /*
  1062. * Final choice, create a new extent page for
  1063. * nex2 extents.
  1064. */
  1065. else {
  1066. erp_idx++;
  1067. erp = xfs_iext_irec_new(ifp, erp_idx);
  1068. }
  1069. memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
  1070. kmem_free(nex2_ep);
  1071. erp->er_extcount += nex2;
  1072. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
  1073. }
  1074. }
  1075. /*
  1076. * This is called when the amount of space required for incore file
  1077. * extents needs to be decreased. The ext_diff parameter stores the
  1078. * number of extents to be removed and the idx parameter contains
  1079. * the extent index where the extents will be removed from.
  1080. *
  1081. * If the amount of space needed has decreased below the linear
  1082. * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
  1083. * extent array. Otherwise, use kmem_realloc() to adjust the
  1084. * size to what is needed.
  1085. */
  1086. void
  1087. xfs_iext_remove(
  1088. xfs_inode_t *ip, /* incore inode pointer */
  1089. xfs_extnum_t idx, /* index to begin removing exts */
  1090. int ext_diff, /* number of extents to remove */
  1091. int state) /* type of extent conversion */
  1092. {
  1093. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  1094. xfs_extnum_t nextents; /* number of extents in file */
  1095. int new_size; /* size of extents after removal */
  1096. trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
  1097. ASSERT(ext_diff > 0);
  1098. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1099. new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
  1100. if (new_size == 0) {
  1101. xfs_iext_destroy(ifp);
  1102. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  1103. xfs_iext_remove_indirect(ifp, idx, ext_diff);
  1104. } else if (ifp->if_real_bytes) {
  1105. xfs_iext_remove_direct(ifp, idx, ext_diff);
  1106. } else {
  1107. xfs_iext_remove_inline(ifp, idx, ext_diff);
  1108. }
  1109. ifp->if_bytes = new_size;
  1110. }
  1111. /*
  1112. * This removes ext_diff extents from the inline buffer, beginning
  1113. * at extent index idx.
  1114. */
  1115. void
  1116. xfs_iext_remove_inline(
  1117. xfs_ifork_t *ifp, /* inode fork pointer */
  1118. xfs_extnum_t idx, /* index to begin removing exts */
  1119. int ext_diff) /* number of extents to remove */
  1120. {
  1121. int nextents; /* number of extents in file */
  1122. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  1123. ASSERT(idx < XFS_INLINE_EXTS);
  1124. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1125. ASSERT(((nextents - ext_diff) > 0) &&
  1126. (nextents - ext_diff) < XFS_INLINE_EXTS);
  1127. if (idx + ext_diff < nextents) {
  1128. memmove(&ifp->if_u2.if_inline_ext[idx],
  1129. &ifp->if_u2.if_inline_ext[idx + ext_diff],
  1130. (nextents - (idx + ext_diff)) *
  1131. sizeof(xfs_bmbt_rec_t));
  1132. memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
  1133. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  1134. } else {
  1135. memset(&ifp->if_u2.if_inline_ext[idx], 0,
  1136. ext_diff * sizeof(xfs_bmbt_rec_t));
  1137. }
  1138. }
  1139. /*
  1140. * This removes ext_diff extents from a linear (direct) extent list,
  1141. * beginning at extent index idx. If the extents are being removed
  1142. * from the end of the list (ie. truncate) then we just need to re-
  1143. * allocate the list to remove the extra space. Otherwise, if the
  1144. * extents are being removed from the middle of the existing extent
  1145. * entries, then we first need to move the extent records beginning
  1146. * at idx + ext_diff up in the list to overwrite the records being
  1147. * removed, then remove the extra space via kmem_realloc.
  1148. */
  1149. void
  1150. xfs_iext_remove_direct(
  1151. xfs_ifork_t *ifp, /* inode fork pointer */
  1152. xfs_extnum_t idx, /* index to begin removing exts */
  1153. int ext_diff) /* number of extents to remove */
  1154. {
  1155. xfs_extnum_t nextents; /* number of extents in file */
  1156. int new_size; /* size of extents after removal */
  1157. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  1158. new_size = ifp->if_bytes -
  1159. (ext_diff * sizeof(xfs_bmbt_rec_t));
  1160. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1161. if (new_size == 0) {
  1162. xfs_iext_destroy(ifp);
  1163. return;
  1164. }
  1165. /* Move extents up in the list (if needed) */
  1166. if (idx + ext_diff < nextents) {
  1167. memmove(&ifp->if_u1.if_extents[idx],
  1168. &ifp->if_u1.if_extents[idx + ext_diff],
  1169. (nextents - (idx + ext_diff)) *
  1170. sizeof(xfs_bmbt_rec_t));
  1171. }
  1172. memset(&ifp->if_u1.if_extents[nextents - ext_diff],
  1173. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  1174. /*
  1175. * Reallocate the direct extent list. If the extents
  1176. * will fit inside the inode then xfs_iext_realloc_direct
  1177. * will switch from direct to inline extent allocation
  1178. * mode for us.
  1179. */
  1180. xfs_iext_realloc_direct(ifp, new_size);
  1181. ifp->if_bytes = new_size;
  1182. }
  1183. /*
  1184. * This is called when incore extents are being removed from the
  1185. * indirection array and the extents being removed span multiple extent
  1186. * buffers. The idx parameter contains the file extent index where we
  1187. * want to begin removing extents, and the count parameter contains
  1188. * how many extents need to be removed.
  1189. *
  1190. * |-------| |-------|
  1191. * | nex1 | | | nex1 - number of extents before idx
  1192. * |-------| | count |
  1193. * | | | | count - number of extents being removed at idx
  1194. * | count | |-------|
  1195. * | | | nex2 | nex2 - number of extents after idx + count
  1196. * |-------| |-------|
  1197. */
  1198. void
  1199. xfs_iext_remove_indirect(
  1200. xfs_ifork_t *ifp, /* inode fork pointer */
  1201. xfs_extnum_t idx, /* index to begin removing extents */
  1202. int count) /* number of extents to remove */
  1203. {
  1204. xfs_ext_irec_t *erp; /* indirection array pointer */
  1205. int erp_idx = 0; /* indirection array index */
  1206. xfs_extnum_t ext_cnt; /* extents left to remove */
  1207. xfs_extnum_t ext_diff; /* extents to remove in current list */
  1208. xfs_extnum_t nex1; /* number of extents before idx */
  1209. xfs_extnum_t nex2; /* extents after idx + count */
  1210. int page_idx = idx; /* index in target extent list */
  1211. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1212. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  1213. ASSERT(erp != NULL);
  1214. nex1 = page_idx;
  1215. ext_cnt = count;
  1216. while (ext_cnt) {
  1217. nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
  1218. ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
  1219. /*
  1220. * Check for deletion of entire list;
  1221. * xfs_iext_irec_remove() updates extent offsets.
  1222. */
  1223. if (ext_diff == erp->er_extcount) {
  1224. xfs_iext_irec_remove(ifp, erp_idx);
  1225. ext_cnt -= ext_diff;
  1226. nex1 = 0;
  1227. if (ext_cnt) {
  1228. ASSERT(erp_idx < ifp->if_real_bytes /
  1229. XFS_IEXT_BUFSZ);
  1230. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1231. nex1 = 0;
  1232. continue;
  1233. } else {
  1234. break;
  1235. }
  1236. }
  1237. /* Move extents up (if needed) */
  1238. if (nex2) {
  1239. memmove(&erp->er_extbuf[nex1],
  1240. &erp->er_extbuf[nex1 + ext_diff],
  1241. nex2 * sizeof(xfs_bmbt_rec_t));
  1242. }
  1243. /* Zero out rest of page */
  1244. memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
  1245. ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
  1246. /* Update remaining counters */
  1247. erp->er_extcount -= ext_diff;
  1248. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
  1249. ext_cnt -= ext_diff;
  1250. nex1 = 0;
  1251. erp_idx++;
  1252. erp++;
  1253. }
  1254. ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
  1255. xfs_iext_irec_compact(ifp);
  1256. }
  1257. /*
  1258. * Create, destroy, or resize a linear (direct) block of extents.
  1259. */
  1260. void
  1261. xfs_iext_realloc_direct(
  1262. xfs_ifork_t *ifp, /* inode fork pointer */
  1263. int new_size) /* new size of extents after adding */
  1264. {
  1265. int rnew_size; /* real new size of extents */
  1266. rnew_size = new_size;
  1267. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
  1268. ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
  1269. (new_size != ifp->if_real_bytes)));
  1270. /* Free extent records */
  1271. if (new_size == 0) {
  1272. xfs_iext_destroy(ifp);
  1273. }
  1274. /* Resize direct extent list and zero any new bytes */
  1275. else if (ifp->if_real_bytes) {
  1276. /* Check if extents will fit inside the inode */
  1277. if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
  1278. xfs_iext_direct_to_inline(ifp, new_size /
  1279. (uint)sizeof(xfs_bmbt_rec_t));
  1280. ifp->if_bytes = new_size;
  1281. return;
  1282. }
  1283. if (!is_power_of_2(new_size)){
  1284. rnew_size = roundup_pow_of_two(new_size);
  1285. }
  1286. if (rnew_size != ifp->if_real_bytes) {
  1287. ifp->if_u1.if_extents =
  1288. kmem_realloc(ifp->if_u1.if_extents,
  1289. rnew_size,
  1290. ifp->if_real_bytes, KM_NOFS);
  1291. }
  1292. if (rnew_size > ifp->if_real_bytes) {
  1293. memset(&ifp->if_u1.if_extents[ifp->if_bytes /
  1294. (uint)sizeof(xfs_bmbt_rec_t)], 0,
  1295. rnew_size - ifp->if_real_bytes);
  1296. }
  1297. }
  1298. /* Switch from the inline extent buffer to a direct extent list */
  1299. else {
  1300. if (!is_power_of_2(new_size)) {
  1301. rnew_size = roundup_pow_of_two(new_size);
  1302. }
  1303. xfs_iext_inline_to_direct(ifp, rnew_size);
  1304. }
  1305. ifp->if_real_bytes = rnew_size;
  1306. ifp->if_bytes = new_size;
  1307. }
  1308. /*
  1309. * Switch from linear (direct) extent records to inline buffer.
  1310. */
  1311. void
  1312. xfs_iext_direct_to_inline(
  1313. xfs_ifork_t *ifp, /* inode fork pointer */
  1314. xfs_extnum_t nextents) /* number of extents in file */
  1315. {
  1316. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  1317. ASSERT(nextents <= XFS_INLINE_EXTS);
  1318. /*
  1319. * The inline buffer was zeroed when we switched
  1320. * from inline to direct extent allocation mode,
  1321. * so we don't need to clear it here.
  1322. */
  1323. memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
  1324. nextents * sizeof(xfs_bmbt_rec_t));
  1325. kmem_free(ifp->if_u1.if_extents);
  1326. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  1327. ifp->if_real_bytes = 0;
  1328. }
  1329. /*
  1330. * Switch from inline buffer to linear (direct) extent records.
  1331. * new_size should already be rounded up to the next power of 2
  1332. * by the caller (when appropriate), so use new_size as it is.
  1333. * However, since new_size may be rounded up, we can't update
  1334. * if_bytes here. It is the caller's responsibility to update
  1335. * if_bytes upon return.
  1336. */
  1337. void
  1338. xfs_iext_inline_to_direct(
  1339. xfs_ifork_t *ifp, /* inode fork pointer */
  1340. int new_size) /* number of extents in file */
  1341. {
  1342. ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
  1343. memset(ifp->if_u1.if_extents, 0, new_size);
  1344. if (ifp->if_bytes) {
  1345. memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
  1346. ifp->if_bytes);
  1347. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  1348. sizeof(xfs_bmbt_rec_t));
  1349. }
  1350. ifp->if_real_bytes = new_size;
  1351. }
  1352. /*
  1353. * Resize an extent indirection array to new_size bytes.
  1354. */
  1355. STATIC void
  1356. xfs_iext_realloc_indirect(
  1357. xfs_ifork_t *ifp, /* inode fork pointer */
  1358. int new_size) /* new indirection array size */
  1359. {
  1360. int nlists; /* number of irec's (ex lists) */
  1361. int size; /* current indirection array size */
  1362. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1363. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1364. size = nlists * sizeof(xfs_ext_irec_t);
  1365. ASSERT(ifp->if_real_bytes);
  1366. ASSERT((new_size >= 0) && (new_size != size));
  1367. if (new_size == 0) {
  1368. xfs_iext_destroy(ifp);
  1369. } else {
  1370. ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
  1371. kmem_realloc(ifp->if_u1.if_ext_irec,
  1372. new_size, size, KM_NOFS);
  1373. }
  1374. }
  1375. /*
  1376. * Switch from indirection array to linear (direct) extent allocations.
  1377. */
  1378. STATIC void
  1379. xfs_iext_indirect_to_direct(
  1380. xfs_ifork_t *ifp) /* inode fork pointer */
  1381. {
  1382. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  1383. xfs_extnum_t nextents; /* number of extents in file */
  1384. int size; /* size of file extents */
  1385. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1386. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1387. ASSERT(nextents <= XFS_LINEAR_EXTS);
  1388. size = nextents * sizeof(xfs_bmbt_rec_t);
  1389. xfs_iext_irec_compact_pages(ifp);
  1390. ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
  1391. ep = ifp->if_u1.if_ext_irec->er_extbuf;
  1392. kmem_free(ifp->if_u1.if_ext_irec);
  1393. ifp->if_flags &= ~XFS_IFEXTIREC;
  1394. ifp->if_u1.if_extents = ep;
  1395. ifp->if_bytes = size;
  1396. if (nextents < XFS_LINEAR_EXTS) {
  1397. xfs_iext_realloc_direct(ifp, size);
  1398. }
  1399. }
  1400. /*
  1401. * Free incore file extents.
  1402. */
  1403. void
  1404. xfs_iext_destroy(
  1405. xfs_ifork_t *ifp) /* inode fork pointer */
  1406. {
  1407. if (ifp->if_flags & XFS_IFEXTIREC) {
  1408. int erp_idx;
  1409. int nlists;
  1410. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1411. for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
  1412. xfs_iext_irec_remove(ifp, erp_idx);
  1413. }
  1414. ifp->if_flags &= ~XFS_IFEXTIREC;
  1415. } else if (ifp->if_real_bytes) {
  1416. kmem_free(ifp->if_u1.if_extents);
  1417. } else if (ifp->if_bytes) {
  1418. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  1419. sizeof(xfs_bmbt_rec_t));
  1420. }
  1421. ifp->if_u1.if_extents = NULL;
  1422. ifp->if_real_bytes = 0;
  1423. ifp->if_bytes = 0;
  1424. }
  1425. /*
  1426. * Return a pointer to the extent record for file system block bno.
  1427. */
  1428. xfs_bmbt_rec_host_t * /* pointer to found extent record */
  1429. xfs_iext_bno_to_ext(
  1430. xfs_ifork_t *ifp, /* inode fork pointer */
  1431. xfs_fileoff_t bno, /* block number to search for */
  1432. xfs_extnum_t *idxp) /* index of target extent */
  1433. {
  1434. xfs_bmbt_rec_host_t *base; /* pointer to first extent */
  1435. xfs_filblks_t blockcount = 0; /* number of blocks in extent */
  1436. xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
  1437. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  1438. int high; /* upper boundary in search */
  1439. xfs_extnum_t idx = 0; /* index of target extent */
  1440. int low; /* lower boundary in search */
  1441. xfs_extnum_t nextents; /* number of file extents */
  1442. xfs_fileoff_t startoff = 0; /* start offset of extent */
  1443. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1444. if (nextents == 0) {
  1445. *idxp = 0;
  1446. return NULL;
  1447. }
  1448. low = 0;
  1449. if (ifp->if_flags & XFS_IFEXTIREC) {
  1450. /* Find target extent list */
  1451. int erp_idx = 0;
  1452. erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
  1453. base = erp->er_extbuf;
  1454. high = erp->er_extcount - 1;
  1455. } else {
  1456. base = ifp->if_u1.if_extents;
  1457. high = nextents - 1;
  1458. }
  1459. /* Binary search extent records */
  1460. while (low <= high) {
  1461. idx = (low + high) >> 1;
  1462. ep = base + idx;
  1463. startoff = xfs_bmbt_get_startoff(ep);
  1464. blockcount = xfs_bmbt_get_blockcount(ep);
  1465. if (bno < startoff) {
  1466. high = idx - 1;
  1467. } else if (bno >= startoff + blockcount) {
  1468. low = idx + 1;
  1469. } else {
  1470. /* Convert back to file-based extent index */
  1471. if (ifp->if_flags & XFS_IFEXTIREC) {
  1472. idx += erp->er_extoff;
  1473. }
  1474. *idxp = idx;
  1475. return ep;
  1476. }
  1477. }
  1478. /* Convert back to file-based extent index */
  1479. if (ifp->if_flags & XFS_IFEXTIREC) {
  1480. idx += erp->er_extoff;
  1481. }
  1482. if (bno >= startoff + blockcount) {
  1483. if (++idx == nextents) {
  1484. ep = NULL;
  1485. } else {
  1486. ep = xfs_iext_get_ext(ifp, idx);
  1487. }
  1488. }
  1489. *idxp = idx;
  1490. return ep;
  1491. }
  1492. /*
  1493. * Return a pointer to the indirection array entry containing the
  1494. * extent record for filesystem block bno. Store the index of the
  1495. * target irec in *erp_idxp.
  1496. */
  1497. xfs_ext_irec_t * /* pointer to found extent record */
  1498. xfs_iext_bno_to_irec(
  1499. xfs_ifork_t *ifp, /* inode fork pointer */
  1500. xfs_fileoff_t bno, /* block number to search for */
  1501. int *erp_idxp) /* irec index of target ext list */
  1502. {
  1503. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  1504. xfs_ext_irec_t *erp_next; /* next indirection array entry */
  1505. int erp_idx; /* indirection array index */
  1506. int nlists; /* number of extent irec's (lists) */
  1507. int high; /* binary search upper limit */
  1508. int low; /* binary search lower limit */
  1509. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1510. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1511. erp_idx = 0;
  1512. low = 0;
  1513. high = nlists - 1;
  1514. while (low <= high) {
  1515. erp_idx = (low + high) >> 1;
  1516. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1517. erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
  1518. if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
  1519. high = erp_idx - 1;
  1520. } else if (erp_next && bno >=
  1521. xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
  1522. low = erp_idx + 1;
  1523. } else {
  1524. break;
  1525. }
  1526. }
  1527. *erp_idxp = erp_idx;
  1528. return erp;
  1529. }
  1530. /*
  1531. * Return a pointer to the indirection array entry containing the
  1532. * extent record at file extent index *idxp. Store the index of the
  1533. * target irec in *erp_idxp and store the page index of the target
  1534. * extent record in *idxp.
  1535. */
  1536. xfs_ext_irec_t *
  1537. xfs_iext_idx_to_irec(
  1538. xfs_ifork_t *ifp, /* inode fork pointer */
  1539. xfs_extnum_t *idxp, /* extent index (file -> page) */
  1540. int *erp_idxp, /* pointer to target irec */
  1541. int realloc) /* new bytes were just added */
  1542. {
  1543. xfs_ext_irec_t *prev; /* pointer to previous irec */
  1544. xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
  1545. int erp_idx; /* indirection array index */
  1546. int nlists; /* number of irec's (ex lists) */
  1547. int high; /* binary search upper limit */
  1548. int low; /* binary search lower limit */
  1549. xfs_extnum_t page_idx = *idxp; /* extent index in target list */
  1550. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1551. ASSERT(page_idx >= 0);
  1552. ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  1553. ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
  1554. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1555. erp_idx = 0;
  1556. low = 0;
  1557. high = nlists - 1;
  1558. /* Binary search extent irec's */
  1559. while (low <= high) {
  1560. erp_idx = (low + high) >> 1;
  1561. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1562. prev = erp_idx > 0 ? erp - 1 : NULL;
  1563. if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
  1564. realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
  1565. high = erp_idx - 1;
  1566. } else if (page_idx > erp->er_extoff + erp->er_extcount ||
  1567. (page_idx == erp->er_extoff + erp->er_extcount &&
  1568. !realloc)) {
  1569. low = erp_idx + 1;
  1570. } else if (page_idx == erp->er_extoff + erp->er_extcount &&
  1571. erp->er_extcount == XFS_LINEAR_EXTS) {
  1572. ASSERT(realloc);
  1573. page_idx = 0;
  1574. erp_idx++;
  1575. erp = erp_idx < nlists ? erp + 1 : NULL;
  1576. break;
  1577. } else {
  1578. page_idx -= erp->er_extoff;
  1579. break;
  1580. }
  1581. }
  1582. *idxp = page_idx;
  1583. *erp_idxp = erp_idx;
  1584. return(erp);
  1585. }
  1586. /*
  1587. * Allocate and initialize an indirection array once the space needed
  1588. * for incore extents increases above XFS_IEXT_BUFSZ.
  1589. */
  1590. void
  1591. xfs_iext_irec_init(
  1592. xfs_ifork_t *ifp) /* inode fork pointer */
  1593. {
  1594. xfs_ext_irec_t *erp; /* indirection array pointer */
  1595. xfs_extnum_t nextents; /* number of extents in file */
  1596. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  1597. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1598. ASSERT(nextents <= XFS_LINEAR_EXTS);
  1599. erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
  1600. if (nextents == 0) {
  1601. ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  1602. } else if (!ifp->if_real_bytes) {
  1603. xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
  1604. } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
  1605. xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
  1606. }
  1607. erp->er_extbuf = ifp->if_u1.if_extents;
  1608. erp->er_extcount = nextents;
  1609. erp->er_extoff = 0;
  1610. ifp->if_flags |= XFS_IFEXTIREC;
  1611. ifp->if_real_bytes = XFS_IEXT_BUFSZ;
  1612. ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
  1613. ifp->if_u1.if_ext_irec = erp;
  1614. return;
  1615. }
  1616. /*
  1617. * Allocate and initialize a new entry in the indirection array.
  1618. */
  1619. xfs_ext_irec_t *
  1620. xfs_iext_irec_new(
  1621. xfs_ifork_t *ifp, /* inode fork pointer */
  1622. int erp_idx) /* index for new irec */
  1623. {
  1624. xfs_ext_irec_t *erp; /* indirection array pointer */
  1625. int i; /* loop counter */
  1626. int nlists; /* number of irec's (ex lists) */
  1627. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1628. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1629. /* Resize indirection array */
  1630. xfs_iext_realloc_indirect(ifp, ++nlists *
  1631. sizeof(xfs_ext_irec_t));
  1632. /*
  1633. * Move records down in the array so the
  1634. * new page can use erp_idx.
  1635. */
  1636. erp = ifp->if_u1.if_ext_irec;
  1637. for (i = nlists - 1; i > erp_idx; i--) {
  1638. memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
  1639. }
  1640. ASSERT(i == erp_idx);
  1641. /* Initialize new extent record */
  1642. erp = ifp->if_u1.if_ext_irec;
  1643. erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  1644. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  1645. memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
  1646. erp[erp_idx].er_extcount = 0;
  1647. erp[erp_idx].er_extoff = erp_idx > 0 ?
  1648. erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
  1649. return (&erp[erp_idx]);
  1650. }
  1651. /*
  1652. * Remove a record from the indirection array.
  1653. */
  1654. void
  1655. xfs_iext_irec_remove(
  1656. xfs_ifork_t *ifp, /* inode fork pointer */
  1657. int erp_idx) /* irec index to remove */
  1658. {
  1659. xfs_ext_irec_t *erp; /* indirection array pointer */
  1660. int i; /* loop counter */
  1661. int nlists; /* number of irec's (ex lists) */
  1662. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1663. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1664. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1665. if (erp->er_extbuf) {
  1666. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
  1667. -erp->er_extcount);
  1668. kmem_free(erp->er_extbuf);
  1669. }
  1670. /* Compact extent records */
  1671. erp = ifp->if_u1.if_ext_irec;
  1672. for (i = erp_idx; i < nlists - 1; i++) {
  1673. memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
  1674. }
  1675. /*
  1676. * Manually free the last extent record from the indirection
  1677. * array. A call to xfs_iext_realloc_indirect() with a size
  1678. * of zero would result in a call to xfs_iext_destroy() which
  1679. * would in turn call this function again, creating a nasty
  1680. * infinite loop.
  1681. */
  1682. if (--nlists) {
  1683. xfs_iext_realloc_indirect(ifp,
  1684. nlists * sizeof(xfs_ext_irec_t));
  1685. } else {
  1686. kmem_free(ifp->if_u1.if_ext_irec);
  1687. }
  1688. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  1689. }
  1690. /*
  1691. * This is called to clean up large amounts of unused memory allocated
  1692. * by the indirection array. Before compacting anything though, verify
  1693. * that the indirection array is still needed and switch back to the
  1694. * linear extent list (or even the inline buffer) if possible. The
  1695. * compaction policy is as follows:
  1696. *
  1697. * Full Compaction: Extents fit into a single page (or inline buffer)
  1698. * Partial Compaction: Extents occupy less than 50% of allocated space
  1699. * No Compaction: Extents occupy at least 50% of allocated space
  1700. */
  1701. void
  1702. xfs_iext_irec_compact(
  1703. xfs_ifork_t *ifp) /* inode fork pointer */
  1704. {
  1705. xfs_extnum_t nextents; /* number of extents in file */
  1706. int nlists; /* number of irec's (ex lists) */
  1707. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1708. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1709. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1710. if (nextents == 0) {
  1711. xfs_iext_destroy(ifp);
  1712. } else if (nextents <= XFS_INLINE_EXTS) {
  1713. xfs_iext_indirect_to_direct(ifp);
  1714. xfs_iext_direct_to_inline(ifp, nextents);
  1715. } else if (nextents <= XFS_LINEAR_EXTS) {
  1716. xfs_iext_indirect_to_direct(ifp);
  1717. } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
  1718. xfs_iext_irec_compact_pages(ifp);
  1719. }
  1720. }
  1721. /*
  1722. * Combine extents from neighboring extent pages.
  1723. */
  1724. void
  1725. xfs_iext_irec_compact_pages(
  1726. xfs_ifork_t *ifp) /* inode fork pointer */
  1727. {
  1728. xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
  1729. int erp_idx = 0; /* indirection array index */
  1730. int nlists; /* number of irec's (ex lists) */
  1731. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1732. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1733. while (erp_idx < nlists - 1) {
  1734. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  1735. erp_next = erp + 1;
  1736. if (erp_next->er_extcount <=
  1737. (XFS_LINEAR_EXTS - erp->er_extcount)) {
  1738. memcpy(&erp->er_extbuf[erp->er_extcount],
  1739. erp_next->er_extbuf, erp_next->er_extcount *
  1740. sizeof(xfs_bmbt_rec_t));
  1741. erp->er_extcount += erp_next->er_extcount;
  1742. /*
  1743. * Free page before removing extent record
  1744. * so er_extoffs don't get modified in
  1745. * xfs_iext_irec_remove.
  1746. */
  1747. kmem_free(erp_next->er_extbuf);
  1748. erp_next->er_extbuf = NULL;
  1749. xfs_iext_irec_remove(ifp, erp_idx + 1);
  1750. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1751. } else {
  1752. erp_idx++;
  1753. }
  1754. }
  1755. }
  1756. /*
  1757. * This is called to update the er_extoff field in the indirection
  1758. * array when extents have been added or removed from one of the
  1759. * extent lists. erp_idx contains the irec index to begin updating
  1760. * at and ext_diff contains the number of extents that were added
  1761. * or removed.
  1762. */
  1763. void
  1764. xfs_iext_irec_update_extoffs(
  1765. xfs_ifork_t *ifp, /* inode fork pointer */
  1766. int erp_idx, /* irec index to update */
  1767. int ext_diff) /* number of new extents */
  1768. {
  1769. int i; /* loop counter */
  1770. int nlists; /* number of irec's (ex lists */
  1771. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  1772. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  1773. for (i = erp_idx; i < nlists; i++) {
  1774. ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
  1775. }
  1776. }