xfs_inode_fork.c 55 KB

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