xfs_inode_fork.c 56 KB

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