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