xfs_inode_fork.c 58 KB

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