xfs_inode_fork.c 59 KB

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