xfs_inode_fork.c 55 KB

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