xfs_inode_fork.c 58 KB

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