xfs_inode_fork.c 55 KB

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