xfs_inode_fork.c 58 KB

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