xfs_inode_fork.c 57 KB

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