xfs_inode_fork.c 56 KB

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