xfs_inode_fork.c 56 KB

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