xfs_inode_fork.c 55 KB

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