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