xfs_inode_fork.c 55 KB

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