xfs_inode_fork.c 55 KB

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