xfs_inode_fork.c 59 KB

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