xfs_inode_fork.c 58 KB

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