xfs_inode_fork.c 55 KB

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