xfs_inode_fork.c 56 KB

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