xfs_dir2_node.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_bmap.h"
  16. #include "xfs_dir2.h"
  17. #include "xfs_dir2_priv.h"
  18. #include "xfs_error.h"
  19. #include "xfs_trace.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_buf_item.h"
  22. #include "xfs_log.h"
  23. /*
  24. * Function declarations.
  25. */
  26. static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
  27. int index);
  28. static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
  29. xfs_da_state_blk_t *blk1,
  30. xfs_da_state_blk_t *blk2);
  31. static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
  32. int index, xfs_da_state_blk_t *dblk,
  33. int *rval);
  34. /*
  35. * Convert data space db to the corresponding free db.
  36. */
  37. static xfs_dir2_db_t
  38. xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  39. {
  40. return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
  41. (db / geo->free_max_bests);
  42. }
  43. /*
  44. * Convert data space db to the corresponding index in a free db.
  45. */
  46. static int
  47. xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
  48. {
  49. return db % geo->free_max_bests;
  50. }
  51. /*
  52. * Check internal consistency of a leafn block.
  53. */
  54. #ifdef DEBUG
  55. static xfs_failaddr_t
  56. xfs_dir3_leafn_check(
  57. struct xfs_inode *dp,
  58. struct xfs_buf *bp)
  59. {
  60. struct xfs_dir2_leaf *leaf = bp->b_addr;
  61. struct xfs_dir3_icleaf_hdr leafhdr;
  62. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
  63. if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
  64. struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
  65. if (be64_to_cpu(leaf3->info.blkno) != xfs_buf_daddr(bp))
  66. return __this_address;
  67. } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
  68. return __this_address;
  69. return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
  70. }
  71. static inline void
  72. xfs_dir3_leaf_check(
  73. struct xfs_inode *dp,
  74. struct xfs_buf *bp)
  75. {
  76. xfs_failaddr_t fa;
  77. fa = xfs_dir3_leafn_check(dp, bp);
  78. if (!fa)
  79. return;
  80. xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
  81. bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
  82. fa);
  83. ASSERT(0);
  84. }
  85. #else
  86. #define xfs_dir3_leaf_check(dp, bp)
  87. #endif
  88. static xfs_failaddr_t
  89. xfs_dir3_free_verify(
  90. struct xfs_buf *bp)
  91. {
  92. struct xfs_mount *mp = bp->b_mount;
  93. struct xfs_dir2_free_hdr *hdr = bp->b_addr;
  94. if (!xfs_verify_magic(bp, hdr->magic))
  95. return __this_address;
  96. if (xfs_has_crc(mp)) {
  97. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  98. if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
  99. return __this_address;
  100. if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
  101. return __this_address;
  102. if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
  103. return __this_address;
  104. }
  105. /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
  106. return NULL;
  107. }
  108. static void
  109. xfs_dir3_free_read_verify(
  110. struct xfs_buf *bp)
  111. {
  112. struct xfs_mount *mp = bp->b_mount;
  113. xfs_failaddr_t fa;
  114. if (xfs_has_crc(mp) &&
  115. !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
  116. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  117. else {
  118. fa = xfs_dir3_free_verify(bp);
  119. if (fa)
  120. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  121. }
  122. }
  123. static void
  124. xfs_dir3_free_write_verify(
  125. struct xfs_buf *bp)
  126. {
  127. struct xfs_mount *mp = bp->b_mount;
  128. struct xfs_buf_log_item *bip = bp->b_log_item;
  129. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  130. xfs_failaddr_t fa;
  131. fa = xfs_dir3_free_verify(bp);
  132. if (fa) {
  133. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  134. return;
  135. }
  136. if (!xfs_has_crc(mp))
  137. return;
  138. if (bip)
  139. hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
  140. xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
  141. }
  142. const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
  143. .name = "xfs_dir3_free",
  144. .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
  145. cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
  146. .verify_read = xfs_dir3_free_read_verify,
  147. .verify_write = xfs_dir3_free_write_verify,
  148. .verify_struct = xfs_dir3_free_verify,
  149. };
  150. /* Everything ok in the free block header? */
  151. static xfs_failaddr_t
  152. xfs_dir3_free_header_check(
  153. struct xfs_inode *dp,
  154. xfs_dablk_t fbno,
  155. struct xfs_buf *bp)
  156. {
  157. struct xfs_mount *mp = dp->i_mount;
  158. int maxbests = mp->m_dir_geo->free_max_bests;
  159. unsigned int firstdb;
  160. firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
  161. xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
  162. maxbests;
  163. if (xfs_has_crc(mp)) {
  164. struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
  165. if (be32_to_cpu(hdr3->firstdb) != firstdb)
  166. return __this_address;
  167. if (be32_to_cpu(hdr3->nvalid) > maxbests)
  168. return __this_address;
  169. if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
  170. return __this_address;
  171. if (be64_to_cpu(hdr3->hdr.owner) != dp->i_ino)
  172. return __this_address;
  173. } else {
  174. struct xfs_dir2_free_hdr *hdr = bp->b_addr;
  175. if (be32_to_cpu(hdr->firstdb) != firstdb)
  176. return __this_address;
  177. if (be32_to_cpu(hdr->nvalid) > maxbests)
  178. return __this_address;
  179. if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
  180. return __this_address;
  181. }
  182. return NULL;
  183. }
  184. static int
  185. __xfs_dir3_free_read(
  186. struct xfs_trans *tp,
  187. struct xfs_inode *dp,
  188. xfs_dablk_t fbno,
  189. unsigned int flags,
  190. struct xfs_buf **bpp)
  191. {
  192. xfs_failaddr_t fa;
  193. int err;
  194. err = xfs_da_read_buf(tp, dp, fbno, flags, bpp, XFS_DATA_FORK,
  195. &xfs_dir3_free_buf_ops);
  196. if (err || !*bpp)
  197. return err;
  198. /* Check things that we can't do in the verifier. */
  199. fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
  200. if (fa) {
  201. __xfs_buf_mark_corrupt(*bpp, fa);
  202. xfs_trans_brelse(tp, *bpp);
  203. *bpp = NULL;
  204. return -EFSCORRUPTED;
  205. }
  206. /* try read returns without an error or *bpp if it lands in a hole */
  207. if (tp)
  208. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
  209. return 0;
  210. }
  211. void
  212. xfs_dir2_free_hdr_from_disk(
  213. struct xfs_mount *mp,
  214. struct xfs_dir3_icfree_hdr *to,
  215. struct xfs_dir2_free *from)
  216. {
  217. if (xfs_has_crc(mp)) {
  218. struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from;
  219. to->magic = be32_to_cpu(from3->hdr.hdr.magic);
  220. to->firstdb = be32_to_cpu(from3->hdr.firstdb);
  221. to->nvalid = be32_to_cpu(from3->hdr.nvalid);
  222. to->nused = be32_to_cpu(from3->hdr.nused);
  223. to->bests = from3->bests;
  224. ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
  225. } else {
  226. to->magic = be32_to_cpu(from->hdr.magic);
  227. to->firstdb = be32_to_cpu(from->hdr.firstdb);
  228. to->nvalid = be32_to_cpu(from->hdr.nvalid);
  229. to->nused = be32_to_cpu(from->hdr.nused);
  230. to->bests = from->bests;
  231. ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
  232. }
  233. }
  234. static void
  235. xfs_dir2_free_hdr_to_disk(
  236. struct xfs_mount *mp,
  237. struct xfs_dir2_free *to,
  238. struct xfs_dir3_icfree_hdr *from)
  239. {
  240. if (xfs_has_crc(mp)) {
  241. struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to;
  242. ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
  243. to3->hdr.hdr.magic = cpu_to_be32(from->magic);
  244. to3->hdr.firstdb = cpu_to_be32(from->firstdb);
  245. to3->hdr.nvalid = cpu_to_be32(from->nvalid);
  246. to3->hdr.nused = cpu_to_be32(from->nused);
  247. } else {
  248. ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
  249. to->hdr.magic = cpu_to_be32(from->magic);
  250. to->hdr.firstdb = cpu_to_be32(from->firstdb);
  251. to->hdr.nvalid = cpu_to_be32(from->nvalid);
  252. to->hdr.nused = cpu_to_be32(from->nused);
  253. }
  254. }
  255. int
  256. xfs_dir2_free_read(
  257. struct xfs_trans *tp,
  258. struct xfs_inode *dp,
  259. xfs_dablk_t fbno,
  260. struct xfs_buf **bpp)
  261. {
  262. return __xfs_dir3_free_read(tp, dp, fbno, 0, bpp);
  263. }
  264. static int
  265. xfs_dir2_free_try_read(
  266. struct xfs_trans *tp,
  267. struct xfs_inode *dp,
  268. xfs_dablk_t fbno,
  269. struct xfs_buf **bpp)
  270. {
  271. return __xfs_dir3_free_read(tp, dp, fbno, XFS_DABUF_MAP_HOLE_OK, bpp);
  272. }
  273. static int
  274. xfs_dir3_free_get_buf(
  275. xfs_da_args_t *args,
  276. xfs_dir2_db_t fbno,
  277. struct xfs_buf **bpp)
  278. {
  279. struct xfs_trans *tp = args->trans;
  280. struct xfs_inode *dp = args->dp;
  281. struct xfs_mount *mp = dp->i_mount;
  282. struct xfs_buf *bp;
  283. int error;
  284. struct xfs_dir3_icfree_hdr hdr;
  285. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
  286. &bp, XFS_DATA_FORK);
  287. if (error)
  288. return error;
  289. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
  290. bp->b_ops = &xfs_dir3_free_buf_ops;
  291. /*
  292. * Initialize the new block to be empty, and remember
  293. * its first slot as our empty slot.
  294. */
  295. memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
  296. memset(&hdr, 0, sizeof(hdr));
  297. if (xfs_has_crc(mp)) {
  298. struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
  299. hdr.magic = XFS_DIR3_FREE_MAGIC;
  300. hdr3->hdr.blkno = cpu_to_be64(xfs_buf_daddr(bp));
  301. hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
  302. uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
  303. } else
  304. hdr.magic = XFS_DIR2_FREE_MAGIC;
  305. xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr);
  306. *bpp = bp;
  307. return 0;
  308. }
  309. /*
  310. * Log entries from a freespace block.
  311. */
  312. STATIC void
  313. xfs_dir2_free_log_bests(
  314. struct xfs_da_args *args,
  315. struct xfs_dir3_icfree_hdr *hdr,
  316. struct xfs_buf *bp,
  317. int first, /* first entry to log */
  318. int last) /* last entry to log */
  319. {
  320. struct xfs_dir2_free *free = bp->b_addr;
  321. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  322. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  323. xfs_trans_log_buf(args->trans, bp,
  324. (char *)&hdr->bests[first] - (char *)free,
  325. (char *)&hdr->bests[last] - (char *)free +
  326. sizeof(hdr->bests[0]) - 1);
  327. }
  328. /*
  329. * Log header from a freespace block.
  330. */
  331. static void
  332. xfs_dir2_free_log_header(
  333. struct xfs_da_args *args,
  334. struct xfs_buf *bp)
  335. {
  336. #ifdef DEBUG
  337. xfs_dir2_free_t *free; /* freespace structure */
  338. free = bp->b_addr;
  339. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  340. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  341. #endif
  342. xfs_trans_log_buf(args->trans, bp, 0,
  343. args->geo->free_hdr_size - 1);
  344. }
  345. /*
  346. * Convert a leaf-format directory to a node-format directory.
  347. * We need to change the magic number of the leaf block, and copy
  348. * the freespace table out of the leaf block into its own block.
  349. */
  350. int /* error */
  351. xfs_dir2_leaf_to_node(
  352. xfs_da_args_t *args, /* operation arguments */
  353. struct xfs_buf *lbp) /* leaf buffer */
  354. {
  355. xfs_inode_t *dp; /* incore directory inode */
  356. int error; /* error return value */
  357. struct xfs_buf *fbp; /* freespace buffer */
  358. xfs_dir2_db_t fdb; /* freespace block number */
  359. __be16 *from; /* pointer to freespace entry */
  360. int i; /* leaf freespace index */
  361. xfs_dir2_leaf_t *leaf; /* leaf structure */
  362. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  363. int n; /* count of live freespc ents */
  364. xfs_dir2_data_off_t off; /* freespace entry value */
  365. xfs_trans_t *tp; /* transaction pointer */
  366. struct xfs_dir3_icfree_hdr freehdr;
  367. trace_xfs_dir2_leaf_to_node(args);
  368. dp = args->dp;
  369. tp = args->trans;
  370. /*
  371. * Add a freespace block to the directory.
  372. */
  373. if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
  374. return error;
  375. }
  376. ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
  377. /*
  378. * Get the buffer for the new freespace block.
  379. */
  380. error = xfs_dir3_free_get_buf(args, fdb, &fbp);
  381. if (error)
  382. return error;
  383. xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr);
  384. leaf = lbp->b_addr;
  385. ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
  386. if (be32_to_cpu(ltp->bestcount) >
  387. (uint)dp->i_disk_size / args->geo->blksize) {
  388. xfs_buf_mark_corrupt(lbp);
  389. return -EFSCORRUPTED;
  390. }
  391. /*
  392. * Copy freespace entries from the leaf block to the new block.
  393. * Count active entries.
  394. */
  395. from = xfs_dir2_leaf_bests_p(ltp);
  396. for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) {
  397. off = be16_to_cpu(*from);
  398. if (off != NULLDATAOFF)
  399. n++;
  400. freehdr.bests[i] = cpu_to_be16(off);
  401. }
  402. /*
  403. * Now initialize the freespace block header.
  404. */
  405. freehdr.nused = n;
  406. freehdr.nvalid = be32_to_cpu(ltp->bestcount);
  407. xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr);
  408. xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1);
  409. xfs_dir2_free_log_header(args, fbp);
  410. /*
  411. * Converting the leaf to a leafnode is just a matter of changing the
  412. * magic number and the ops. Do the change directly to the buffer as
  413. * it's less work (and less code) than decoding the header to host
  414. * format and back again.
  415. */
  416. if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
  417. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
  418. else
  419. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
  420. lbp->b_ops = &xfs_dir3_leafn_buf_ops;
  421. xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
  422. xfs_dir3_leaf_log_header(args, lbp);
  423. xfs_dir3_leaf_check(dp, lbp);
  424. return 0;
  425. }
  426. /*
  427. * Add a leaf entry to a leaf block in a node-form directory.
  428. * The other work necessary is done from the caller.
  429. */
  430. static int /* error */
  431. xfs_dir2_leafn_add(
  432. struct xfs_buf *bp, /* leaf buffer */
  433. struct xfs_da_args *args, /* operation arguments */
  434. int index) /* insertion pt for new entry */
  435. {
  436. struct xfs_dir3_icleaf_hdr leafhdr;
  437. struct xfs_inode *dp = args->dp;
  438. struct xfs_dir2_leaf *leaf = bp->b_addr;
  439. struct xfs_dir2_leaf_entry *lep;
  440. struct xfs_dir2_leaf_entry *ents;
  441. int compact; /* compacting stale leaves */
  442. int highstale = 0; /* next stale entry */
  443. int lfloghigh; /* high leaf entry logging */
  444. int lfloglow; /* low leaf entry logging */
  445. int lowstale = 0; /* previous stale entry */
  446. trace_xfs_dir2_leafn_add(args, index);
  447. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
  448. ents = leafhdr.ents;
  449. /*
  450. * Quick check just to make sure we are not going to index
  451. * into other peoples memory
  452. */
  453. if (index < 0) {
  454. xfs_buf_mark_corrupt(bp);
  455. return -EFSCORRUPTED;
  456. }
  457. /*
  458. * If there are already the maximum number of leaf entries in
  459. * the block, if there are no stale entries it won't fit.
  460. * Caller will do a split. If there are stale entries we'll do
  461. * a compact.
  462. */
  463. if (leafhdr.count == args->geo->leaf_max_ents) {
  464. if (!leafhdr.stale)
  465. return -ENOSPC;
  466. compact = leafhdr.stale > 1;
  467. } else
  468. compact = 0;
  469. ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
  470. ASSERT(index == leafhdr.count ||
  471. be32_to_cpu(ents[index].hashval) >= args->hashval);
  472. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  473. return 0;
  474. /*
  475. * Compact out all but one stale leaf entry. Leaves behind
  476. * the entry closest to index.
  477. */
  478. if (compact)
  479. xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
  480. &highstale, &lfloglow, &lfloghigh);
  481. else if (leafhdr.stale) {
  482. /*
  483. * Set impossible logging indices for this case.
  484. */
  485. lfloglow = leafhdr.count;
  486. lfloghigh = -1;
  487. }
  488. /*
  489. * Insert the new entry, log everything.
  490. */
  491. lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
  492. highstale, &lfloglow, &lfloghigh);
  493. lep->hashval = cpu_to_be32(args->hashval);
  494. lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
  495. args->blkno, args->index));
  496. xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
  497. xfs_dir3_leaf_log_header(args, bp);
  498. xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
  499. xfs_dir3_leaf_check(dp, bp);
  500. return 0;
  501. }
  502. #ifdef DEBUG
  503. static void
  504. xfs_dir2_free_hdr_check(
  505. struct xfs_inode *dp,
  506. struct xfs_buf *bp,
  507. xfs_dir2_db_t db)
  508. {
  509. struct xfs_dir3_icfree_hdr hdr;
  510. xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr);
  511. ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0);
  512. ASSERT(hdr.firstdb <= db);
  513. ASSERT(db < hdr.firstdb + hdr.nvalid);
  514. }
  515. #else
  516. #define xfs_dir2_free_hdr_check(dp, bp, db)
  517. #endif /* DEBUG */
  518. /*
  519. * Return the last hash value in the leaf.
  520. * Stale entries are ok.
  521. */
  522. xfs_dahash_t /* hash value */
  523. xfs_dir2_leaf_lasthash(
  524. struct xfs_inode *dp,
  525. struct xfs_buf *bp, /* leaf buffer */
  526. int *count) /* count of entries in leaf */
  527. {
  528. struct xfs_dir3_icleaf_hdr leafhdr;
  529. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
  530. ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
  531. leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
  532. leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
  533. leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
  534. if (count)
  535. *count = leafhdr.count;
  536. if (!leafhdr.count)
  537. return 0;
  538. return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval);
  539. }
  540. /*
  541. * Look up a leaf entry for space to add a name in a node-format leaf block.
  542. * The extrablk in state is a freespace block.
  543. */
  544. STATIC int
  545. xfs_dir2_leafn_lookup_for_addname(
  546. struct xfs_buf *bp, /* leaf buffer */
  547. xfs_da_args_t *args, /* operation arguments */
  548. int *indexp, /* out: leaf entry index */
  549. xfs_da_state_t *state) /* state to fill in */
  550. {
  551. struct xfs_buf *curbp = NULL; /* current data/free buffer */
  552. xfs_dir2_db_t curdb = -1; /* current data block number */
  553. xfs_dir2_db_t curfdb = -1; /* current free block number */
  554. xfs_inode_t *dp; /* incore directory inode */
  555. int error; /* error return value */
  556. int fi; /* free entry index */
  557. xfs_dir2_free_t *free = NULL; /* free block structure */
  558. int index; /* leaf entry index */
  559. xfs_dir2_leaf_t *leaf; /* leaf structure */
  560. int length; /* length of new data entry */
  561. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  562. xfs_mount_t *mp; /* filesystem mount point */
  563. xfs_dir2_db_t newdb; /* new data block number */
  564. xfs_dir2_db_t newfdb; /* new free block number */
  565. xfs_trans_t *tp; /* transaction pointer */
  566. struct xfs_dir3_icleaf_hdr leafhdr;
  567. dp = args->dp;
  568. tp = args->trans;
  569. mp = dp->i_mount;
  570. leaf = bp->b_addr;
  571. xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
  572. xfs_dir3_leaf_check(dp, bp);
  573. ASSERT(leafhdr.count > 0);
  574. /*
  575. * Look up the hash value in the leaf entries.
  576. */
  577. index = xfs_dir2_leaf_search_hash(args, bp);
  578. /*
  579. * Do we have a buffer coming in?
  580. */
  581. if (state->extravalid) {
  582. /* If so, it's a free block buffer, get the block number. */
  583. curbp = state->extrablk.bp;
  584. curfdb = state->extrablk.blkno;
  585. free = curbp->b_addr;
  586. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  587. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  588. }
  589. length = xfs_dir2_data_entsize(mp, args->namelen);
  590. /*
  591. * Loop over leaf entries with the right hash value.
  592. */
  593. for (lep = &leafhdr.ents[index];
  594. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  595. lep++, index++) {
  596. /*
  597. * Skip stale leaf entries.
  598. */
  599. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  600. continue;
  601. /*
  602. * Pull the data block number from the entry.
  603. */
  604. newdb = xfs_dir2_dataptr_to_db(args->geo,
  605. be32_to_cpu(lep->address));
  606. /*
  607. * For addname, we're looking for a place to put the new entry.
  608. * We want to use a data block with an entry of equal
  609. * hash value to ours if there is one with room.
  610. *
  611. * If this block isn't the data block we already have
  612. * in hand, take a look at it.
  613. */
  614. if (newdb != curdb) {
  615. struct xfs_dir3_icfree_hdr freehdr;
  616. curdb = newdb;
  617. /*
  618. * Convert the data block to the free block
  619. * holding its freespace information.
  620. */
  621. newfdb = xfs_dir2_db_to_fdb(args->geo, newdb);
  622. /*
  623. * If it's not the one we have in hand, read it in.
  624. */
  625. if (newfdb != curfdb) {
  626. /*
  627. * If we had one before, drop it.
  628. */
  629. if (curbp)
  630. xfs_trans_brelse(tp, curbp);
  631. error = xfs_dir2_free_read(tp, dp,
  632. xfs_dir2_db_to_da(args->geo,
  633. newfdb),
  634. &curbp);
  635. if (error)
  636. return error;
  637. free = curbp->b_addr;
  638. xfs_dir2_free_hdr_check(dp, curbp, curdb);
  639. }
  640. /*
  641. * Get the index for our entry.
  642. */
  643. fi = xfs_dir2_db_to_fdindex(args->geo, curdb);
  644. /*
  645. * If it has room, return it.
  646. */
  647. xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
  648. if (XFS_IS_CORRUPT(mp,
  649. freehdr.bests[fi] ==
  650. cpu_to_be16(NULLDATAOFF))) {
  651. if (curfdb != newfdb)
  652. xfs_trans_brelse(tp, curbp);
  653. return -EFSCORRUPTED;
  654. }
  655. curfdb = newfdb;
  656. if (be16_to_cpu(freehdr.bests[fi]) >= length)
  657. goto out;
  658. }
  659. }
  660. /* Didn't find any space */
  661. fi = -1;
  662. out:
  663. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  664. if (curbp) {
  665. /* Giving back a free block. */
  666. state->extravalid = 1;
  667. state->extrablk.bp = curbp;
  668. state->extrablk.index = fi;
  669. state->extrablk.blkno = curfdb;
  670. /*
  671. * Important: this magic number is not in the buffer - it's for
  672. * buffer type information and therefore only the free/data type
  673. * matters here, not whether CRCs are enabled or not.
  674. */
  675. state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
  676. } else {
  677. state->extravalid = 0;
  678. }
  679. /*
  680. * Return the index, that will be the insertion point.
  681. */
  682. *indexp = index;
  683. return -ENOENT;
  684. }
  685. /*
  686. * Look up a leaf entry in a node-format leaf block.
  687. * The extrablk in state a data block.
  688. */
  689. STATIC int
  690. xfs_dir2_leafn_lookup_for_entry(
  691. struct xfs_buf *bp, /* leaf buffer */
  692. xfs_da_args_t *args, /* operation arguments */
  693. int *indexp, /* out: leaf entry index */
  694. xfs_da_state_t *state) /* state to fill in */
  695. {
  696. struct xfs_buf *curbp = NULL; /* current data/free buffer */
  697. xfs_dir2_db_t curdb = -1; /* current data block number */
  698. xfs_dir2_data_entry_t *dep; /* data block entry */
  699. xfs_inode_t *dp; /* incore directory inode */
  700. int error; /* error return value */
  701. int index; /* leaf entry index */
  702. xfs_dir2_leaf_t *leaf; /* leaf structure */
  703. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  704. xfs_mount_t *mp; /* filesystem mount point */
  705. xfs_dir2_db_t newdb; /* new data block number */
  706. xfs_trans_t *tp; /* transaction pointer */
  707. enum xfs_dacmp cmp; /* comparison result */
  708. struct xfs_dir3_icleaf_hdr leafhdr;
  709. dp = args->dp;
  710. tp = args->trans;
  711. mp = dp->i_mount;
  712. leaf = bp->b_addr;
  713. xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
  714. xfs_dir3_leaf_check(dp, bp);
  715. if (leafhdr.count <= 0) {
  716. xfs_buf_mark_corrupt(bp);
  717. return -EFSCORRUPTED;
  718. }
  719. /*
  720. * Look up the hash value in the leaf entries.
  721. */
  722. index = xfs_dir2_leaf_search_hash(args, bp);
  723. /*
  724. * Do we have a buffer coming in?
  725. */
  726. if (state->extravalid) {
  727. curbp = state->extrablk.bp;
  728. curdb = state->extrablk.blkno;
  729. }
  730. /*
  731. * Loop over leaf entries with the right hash value.
  732. */
  733. for (lep = &leafhdr.ents[index];
  734. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  735. lep++, index++) {
  736. /*
  737. * Skip stale leaf entries.
  738. */
  739. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  740. continue;
  741. /*
  742. * Pull the data block number from the entry.
  743. */
  744. newdb = xfs_dir2_dataptr_to_db(args->geo,
  745. be32_to_cpu(lep->address));
  746. /*
  747. * Not adding a new entry, so we really want to find
  748. * the name given to us.
  749. *
  750. * If it's a different data block, go get it.
  751. */
  752. if (newdb != curdb) {
  753. /*
  754. * If we had a block before that we aren't saving
  755. * for a CI name, drop it
  756. */
  757. if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
  758. curdb != state->extrablk.blkno))
  759. xfs_trans_brelse(tp, curbp);
  760. /*
  761. * If needing the block that is saved with a CI match,
  762. * use it otherwise read in the new data block.
  763. */
  764. if (args->cmpresult != XFS_CMP_DIFFERENT &&
  765. newdb == state->extrablk.blkno) {
  766. ASSERT(state->extravalid);
  767. curbp = state->extrablk.bp;
  768. } else {
  769. error = xfs_dir3_data_read(tp, dp,
  770. xfs_dir2_db_to_da(args->geo,
  771. newdb),
  772. 0, &curbp);
  773. if (error)
  774. return error;
  775. }
  776. xfs_dir3_data_check(dp, curbp);
  777. curdb = newdb;
  778. }
  779. /*
  780. * Point to the data entry.
  781. */
  782. dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
  783. xfs_dir2_dataptr_to_off(args->geo,
  784. be32_to_cpu(lep->address)));
  785. /*
  786. * Compare the entry and if it's an exact match, return
  787. * EEXIST immediately. If it's the first case-insensitive
  788. * match, store the block & inode number and continue looking.
  789. */
  790. cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
  791. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  792. /* If there is a CI match block, drop it */
  793. if (args->cmpresult != XFS_CMP_DIFFERENT &&
  794. curdb != state->extrablk.blkno)
  795. xfs_trans_brelse(tp, state->extrablk.bp);
  796. args->cmpresult = cmp;
  797. args->inumber = be64_to_cpu(dep->inumber);
  798. args->filetype = xfs_dir2_data_get_ftype(mp, dep);
  799. *indexp = index;
  800. state->extravalid = 1;
  801. state->extrablk.bp = curbp;
  802. state->extrablk.blkno = curdb;
  803. state->extrablk.index = (int)((char *)dep -
  804. (char *)curbp->b_addr);
  805. state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
  806. curbp->b_ops = &xfs_dir3_data_buf_ops;
  807. xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
  808. if (cmp == XFS_CMP_EXACT)
  809. return -EEXIST;
  810. }
  811. }
  812. ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
  813. if (curbp) {
  814. if (args->cmpresult == XFS_CMP_DIFFERENT) {
  815. /* Giving back last used data block. */
  816. state->extravalid = 1;
  817. state->extrablk.bp = curbp;
  818. state->extrablk.index = -1;
  819. state->extrablk.blkno = curdb;
  820. state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
  821. curbp->b_ops = &xfs_dir3_data_buf_ops;
  822. xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
  823. } else {
  824. /* If the curbp is not the CI match block, drop it */
  825. if (state->extrablk.bp != curbp)
  826. xfs_trans_brelse(tp, curbp);
  827. }
  828. } else {
  829. state->extravalid = 0;
  830. }
  831. *indexp = index;
  832. return -ENOENT;
  833. }
  834. /*
  835. * Look up a leaf entry in a node-format leaf block.
  836. * If this is an addname then the extrablk in state is a freespace block,
  837. * otherwise it's a data block.
  838. */
  839. int
  840. xfs_dir2_leafn_lookup_int(
  841. struct xfs_buf *bp, /* leaf buffer */
  842. xfs_da_args_t *args, /* operation arguments */
  843. int *indexp, /* out: leaf entry index */
  844. xfs_da_state_t *state) /* state to fill in */
  845. {
  846. if (args->op_flags & XFS_DA_OP_ADDNAME)
  847. return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
  848. state);
  849. return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
  850. }
  851. /*
  852. * Move count leaf entries from source to destination leaf.
  853. * Log entries and headers. Stale entries are preserved.
  854. */
  855. static void
  856. xfs_dir3_leafn_moveents(
  857. xfs_da_args_t *args, /* operation arguments */
  858. struct xfs_buf *bp_s, /* source */
  859. struct xfs_dir3_icleaf_hdr *shdr,
  860. struct xfs_dir2_leaf_entry *sents,
  861. int start_s,/* source leaf index */
  862. struct xfs_buf *bp_d, /* destination */
  863. struct xfs_dir3_icleaf_hdr *dhdr,
  864. struct xfs_dir2_leaf_entry *dents,
  865. int start_d,/* destination leaf index */
  866. int count) /* count of leaves to copy */
  867. {
  868. int stale; /* count stale leaves copied */
  869. trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
  870. /*
  871. * Silently return if nothing to do.
  872. */
  873. if (count == 0)
  874. return;
  875. /*
  876. * If the destination index is not the end of the current
  877. * destination leaf entries, open up a hole in the destination
  878. * to hold the new entries.
  879. */
  880. if (start_d < dhdr->count) {
  881. memmove(&dents[start_d + count], &dents[start_d],
  882. (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
  883. xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
  884. count + dhdr->count - 1);
  885. }
  886. /*
  887. * If the source has stale leaves, count the ones in the copy range
  888. * so we can update the header correctly.
  889. */
  890. if (shdr->stale) {
  891. int i; /* temp leaf index */
  892. for (i = start_s, stale = 0; i < start_s + count; i++) {
  893. if (sents[i].address ==
  894. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  895. stale++;
  896. }
  897. } else
  898. stale = 0;
  899. /*
  900. * Copy the leaf entries from source to destination.
  901. */
  902. memcpy(&dents[start_d], &sents[start_s],
  903. count * sizeof(xfs_dir2_leaf_entry_t));
  904. xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1);
  905. /*
  906. * If there are source entries after the ones we copied,
  907. * delete the ones we copied by sliding the next ones down.
  908. */
  909. if (start_s + count < shdr->count) {
  910. memmove(&sents[start_s], &sents[start_s + count],
  911. count * sizeof(xfs_dir2_leaf_entry_t));
  912. xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s,
  913. start_s + count - 1);
  914. }
  915. /*
  916. * Update the headers and log them.
  917. */
  918. shdr->count -= count;
  919. shdr->stale -= stale;
  920. dhdr->count += count;
  921. dhdr->stale += stale;
  922. }
  923. /*
  924. * Determine the sort order of two leaf blocks.
  925. * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
  926. */
  927. int /* sort order */
  928. xfs_dir2_leafn_order(
  929. struct xfs_inode *dp,
  930. struct xfs_buf *leaf1_bp, /* leaf1 buffer */
  931. struct xfs_buf *leaf2_bp) /* leaf2 buffer */
  932. {
  933. struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
  934. struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
  935. struct xfs_dir2_leaf_entry *ents1;
  936. struct xfs_dir2_leaf_entry *ents2;
  937. struct xfs_dir3_icleaf_hdr hdr1;
  938. struct xfs_dir3_icleaf_hdr hdr2;
  939. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
  940. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
  941. ents1 = hdr1.ents;
  942. ents2 = hdr2.ents;
  943. if (hdr1.count > 0 && hdr2.count > 0 &&
  944. (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
  945. be32_to_cpu(ents2[hdr2.count - 1].hashval) <
  946. be32_to_cpu(ents1[hdr1.count - 1].hashval)))
  947. return 1;
  948. return 0;
  949. }
  950. /*
  951. * Rebalance leaf entries between two leaf blocks.
  952. * This is actually only called when the second block is new,
  953. * though the code deals with the general case.
  954. * A new entry will be inserted in one of the blocks, and that
  955. * entry is taken into account when balancing.
  956. */
  957. static void
  958. xfs_dir2_leafn_rebalance(
  959. xfs_da_state_t *state, /* btree cursor */
  960. xfs_da_state_blk_t *blk1, /* first btree block */
  961. xfs_da_state_blk_t *blk2) /* second btree block */
  962. {
  963. xfs_da_args_t *args; /* operation arguments */
  964. int count; /* count (& direction) leaves */
  965. int isleft; /* new goes in left leaf */
  966. xfs_dir2_leaf_t *leaf1; /* first leaf structure */
  967. xfs_dir2_leaf_t *leaf2; /* second leaf structure */
  968. int mid; /* midpoint leaf index */
  969. #if defined(DEBUG) || defined(XFS_WARN)
  970. int oldstale; /* old count of stale leaves */
  971. #endif
  972. int oldsum; /* old total leaf count */
  973. int swap_blocks; /* swapped leaf blocks */
  974. struct xfs_dir2_leaf_entry *ents1;
  975. struct xfs_dir2_leaf_entry *ents2;
  976. struct xfs_dir3_icleaf_hdr hdr1;
  977. struct xfs_dir3_icleaf_hdr hdr2;
  978. struct xfs_inode *dp = state->args->dp;
  979. args = state->args;
  980. /*
  981. * If the block order is wrong, swap the arguments.
  982. */
  983. swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
  984. if (swap_blocks)
  985. swap(blk1, blk2);
  986. leaf1 = blk1->bp->b_addr;
  987. leaf2 = blk2->bp->b_addr;
  988. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
  989. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
  990. ents1 = hdr1.ents;
  991. ents2 = hdr2.ents;
  992. oldsum = hdr1.count + hdr2.count;
  993. #if defined(DEBUG) || defined(XFS_WARN)
  994. oldstale = hdr1.stale + hdr2.stale;
  995. #endif
  996. mid = oldsum >> 1;
  997. /*
  998. * If the old leaf count was odd then the new one will be even,
  999. * so we need to divide the new count evenly.
  1000. */
  1001. if (oldsum & 1) {
  1002. xfs_dahash_t midhash; /* middle entry hash value */
  1003. if (mid >= hdr1.count)
  1004. midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
  1005. else
  1006. midhash = be32_to_cpu(ents1[mid].hashval);
  1007. isleft = args->hashval <= midhash;
  1008. }
  1009. /*
  1010. * If the old count is even then the new count is odd, so there's
  1011. * no preferred side for the new entry.
  1012. * Pick the left one.
  1013. */
  1014. else
  1015. isleft = 1;
  1016. /*
  1017. * Calculate moved entry count. Positive means left-to-right,
  1018. * negative means right-to-left. Then move the entries.
  1019. */
  1020. count = hdr1.count - mid + (isleft == 0);
  1021. if (count > 0)
  1022. xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
  1023. hdr1.count - count, blk2->bp,
  1024. &hdr2, ents2, 0, count);
  1025. else if (count < 0)
  1026. xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
  1027. blk1->bp, &hdr1, ents1,
  1028. hdr1.count, count);
  1029. ASSERT(hdr1.count + hdr2.count == oldsum);
  1030. ASSERT(hdr1.stale + hdr2.stale == oldstale);
  1031. /* log the changes made when moving the entries */
  1032. xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
  1033. xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
  1034. xfs_dir3_leaf_log_header(args, blk1->bp);
  1035. xfs_dir3_leaf_log_header(args, blk2->bp);
  1036. xfs_dir3_leaf_check(dp, blk1->bp);
  1037. xfs_dir3_leaf_check(dp, blk2->bp);
  1038. /*
  1039. * Mark whether we're inserting into the old or new leaf.
  1040. */
  1041. if (hdr1.count < hdr2.count)
  1042. state->inleaf = swap_blocks;
  1043. else if (hdr1.count > hdr2.count)
  1044. state->inleaf = !swap_blocks;
  1045. else
  1046. state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
  1047. /*
  1048. * Adjust the expected index for insertion.
  1049. */
  1050. if (!state->inleaf)
  1051. blk2->index = blk1->index - hdr1.count;
  1052. /*
  1053. * Finally sanity check just to make sure we are not returning a
  1054. * negative index
  1055. */
  1056. if (blk2->index < 0) {
  1057. state->inleaf = 1;
  1058. blk2->index = 0;
  1059. xfs_alert(dp->i_mount,
  1060. "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
  1061. __func__, blk1->index);
  1062. }
  1063. }
  1064. static int
  1065. xfs_dir3_data_block_free(
  1066. xfs_da_args_t *args,
  1067. struct xfs_dir2_data_hdr *hdr,
  1068. struct xfs_dir2_free *free,
  1069. xfs_dir2_db_t fdb,
  1070. int findex,
  1071. struct xfs_buf *fbp,
  1072. int longest)
  1073. {
  1074. int logfree = 0;
  1075. struct xfs_dir3_icfree_hdr freehdr;
  1076. struct xfs_inode *dp = args->dp;
  1077. xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
  1078. if (hdr) {
  1079. /*
  1080. * Data block is not empty, just set the free entry to the new
  1081. * value.
  1082. */
  1083. freehdr.bests[findex] = cpu_to_be16(longest);
  1084. xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
  1085. return 0;
  1086. }
  1087. /* One less used entry in the free table. */
  1088. freehdr.nused--;
  1089. /*
  1090. * If this was the last entry in the table, we can trim the table size
  1091. * back. There might be other entries at the end referring to
  1092. * non-existent data blocks, get those too.
  1093. */
  1094. if (findex == freehdr.nvalid - 1) {
  1095. int i; /* free entry index */
  1096. for (i = findex - 1; i >= 0; i--) {
  1097. if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF))
  1098. break;
  1099. }
  1100. freehdr.nvalid = i + 1;
  1101. logfree = 0;
  1102. } else {
  1103. /* Not the last entry, just punch it out. */
  1104. freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF);
  1105. logfree = 1;
  1106. }
  1107. xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr);
  1108. xfs_dir2_free_log_header(args, fbp);
  1109. /*
  1110. * If there are no useful entries left in the block, get rid of the
  1111. * block if we can.
  1112. */
  1113. if (!freehdr.nused) {
  1114. int error;
  1115. error = xfs_dir2_shrink_inode(args, fdb, fbp);
  1116. if (error == 0) {
  1117. fbp = NULL;
  1118. logfree = 0;
  1119. } else if (error != -ENOSPC || args->total != 0)
  1120. return error;
  1121. /*
  1122. * It's possible to get ENOSPC if there is no
  1123. * space reservation. In this case some one
  1124. * else will eventually get rid of this block.
  1125. */
  1126. }
  1127. /* Log the free entry that changed, unless we got rid of it. */
  1128. if (logfree)
  1129. xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
  1130. return 0;
  1131. }
  1132. /*
  1133. * Remove an entry from a node directory.
  1134. * This removes the leaf entry and the data entry,
  1135. * and updates the free block if necessary.
  1136. */
  1137. static int /* error */
  1138. xfs_dir2_leafn_remove(
  1139. xfs_da_args_t *args, /* operation arguments */
  1140. struct xfs_buf *bp, /* leaf buffer */
  1141. int index, /* leaf entry index */
  1142. xfs_da_state_blk_t *dblk, /* data block */
  1143. int *rval) /* resulting block needs join */
  1144. {
  1145. struct xfs_da_geometry *geo = args->geo;
  1146. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1147. xfs_dir2_db_t db; /* data block number */
  1148. struct xfs_buf *dbp; /* data block buffer */
  1149. xfs_dir2_data_entry_t *dep; /* data block entry */
  1150. xfs_inode_t *dp; /* incore directory inode */
  1151. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1152. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1153. int longest; /* longest data free entry */
  1154. int off; /* data block entry offset */
  1155. int needlog; /* need to log data header */
  1156. int needscan; /* need to rescan data frees */
  1157. xfs_trans_t *tp; /* transaction pointer */
  1158. struct xfs_dir2_data_free *bf; /* bestfree table */
  1159. struct xfs_dir3_icleaf_hdr leafhdr;
  1160. trace_xfs_dir2_leafn_remove(args, index);
  1161. dp = args->dp;
  1162. tp = args->trans;
  1163. leaf = bp->b_addr;
  1164. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
  1165. /*
  1166. * Point to the entry we're removing.
  1167. */
  1168. lep = &leafhdr.ents[index];
  1169. /*
  1170. * Extract the data block and offset from the entry.
  1171. */
  1172. db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
  1173. ASSERT(dblk->blkno == db);
  1174. off = xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address));
  1175. ASSERT(dblk->index == off);
  1176. /*
  1177. * Kill the leaf entry by marking it stale.
  1178. * Log the leaf block changes.
  1179. */
  1180. leafhdr.stale++;
  1181. xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
  1182. xfs_dir3_leaf_log_header(args, bp);
  1183. lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  1184. xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index);
  1185. /*
  1186. * Make the data entry free. Keep track of the longest freespace
  1187. * in the data block in case it changes.
  1188. */
  1189. dbp = dblk->bp;
  1190. hdr = dbp->b_addr;
  1191. dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
  1192. bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
  1193. longest = be16_to_cpu(bf[0].length);
  1194. needlog = needscan = 0;
  1195. xfs_dir2_data_make_free(args, dbp, off,
  1196. xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
  1197. &needscan);
  1198. /*
  1199. * Rescan the data block freespaces for bestfree.
  1200. * Log the data block header if needed.
  1201. */
  1202. if (needscan)
  1203. xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
  1204. if (needlog)
  1205. xfs_dir2_data_log_header(args, dbp);
  1206. xfs_dir3_data_check(dp, dbp);
  1207. /*
  1208. * If the longest data block freespace changes, need to update
  1209. * the corresponding freeblock entry.
  1210. */
  1211. if (longest < be16_to_cpu(bf[0].length)) {
  1212. int error; /* error return value */
  1213. struct xfs_buf *fbp; /* freeblock buffer */
  1214. xfs_dir2_db_t fdb; /* freeblock block number */
  1215. int findex; /* index in freeblock entries */
  1216. xfs_dir2_free_t *free; /* freeblock structure */
  1217. /*
  1218. * Convert the data block number to a free block,
  1219. * read in the free block.
  1220. */
  1221. fdb = xfs_dir2_db_to_fdb(geo, db);
  1222. error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(geo, fdb),
  1223. &fbp);
  1224. if (error)
  1225. return error;
  1226. free = fbp->b_addr;
  1227. #ifdef DEBUG
  1228. {
  1229. struct xfs_dir3_icfree_hdr freehdr;
  1230. xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
  1231. ASSERT(freehdr.firstdb == geo->free_max_bests *
  1232. (fdb - xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET)));
  1233. }
  1234. #endif
  1235. /*
  1236. * Calculate which entry we need to fix.
  1237. */
  1238. findex = xfs_dir2_db_to_fdindex(geo, db);
  1239. longest = be16_to_cpu(bf[0].length);
  1240. /*
  1241. * If the data block is now empty we can get rid of it
  1242. * (usually).
  1243. */
  1244. if (longest == geo->blksize - geo->data_entry_offset) {
  1245. /*
  1246. * Try to punch out the data block.
  1247. */
  1248. error = xfs_dir2_shrink_inode(args, db, dbp);
  1249. if (error == 0) {
  1250. dblk->bp = NULL;
  1251. hdr = NULL;
  1252. }
  1253. /*
  1254. * We can get ENOSPC if there's no space reservation.
  1255. * In this case just drop the buffer and some one else
  1256. * will eventually get rid of the empty block.
  1257. */
  1258. else if (!(error == -ENOSPC && args->total == 0))
  1259. return error;
  1260. }
  1261. /*
  1262. * If we got rid of the data block, we can eliminate that entry
  1263. * in the free block.
  1264. */
  1265. error = xfs_dir3_data_block_free(args, hdr, free,
  1266. fdb, findex, fbp, longest);
  1267. if (error)
  1268. return error;
  1269. }
  1270. xfs_dir3_leaf_check(dp, bp);
  1271. /*
  1272. * Return indication of whether this leaf block is empty enough
  1273. * to justify trying to join it with a neighbor.
  1274. */
  1275. *rval = (geo->leaf_hdr_size +
  1276. (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
  1277. geo->magicpct;
  1278. return 0;
  1279. }
  1280. /*
  1281. * Split the leaf entries in the old block into old and new blocks.
  1282. */
  1283. int /* error */
  1284. xfs_dir2_leafn_split(
  1285. xfs_da_state_t *state, /* btree cursor */
  1286. xfs_da_state_blk_t *oldblk, /* original block */
  1287. xfs_da_state_blk_t *newblk) /* newly created block */
  1288. {
  1289. xfs_da_args_t *args; /* operation arguments */
  1290. xfs_dablk_t blkno; /* new leaf block number */
  1291. int error; /* error return value */
  1292. struct xfs_inode *dp;
  1293. /*
  1294. * Allocate space for a new leaf node.
  1295. */
  1296. args = state->args;
  1297. dp = args->dp;
  1298. ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
  1299. error = xfs_da_grow_inode(args, &blkno);
  1300. if (error) {
  1301. return error;
  1302. }
  1303. /*
  1304. * Initialize the new leaf block.
  1305. */
  1306. error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
  1307. &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
  1308. if (error)
  1309. return error;
  1310. newblk->blkno = blkno;
  1311. newblk->magic = XFS_DIR2_LEAFN_MAGIC;
  1312. /*
  1313. * Rebalance the entries across the two leaves, link the new
  1314. * block into the leaves.
  1315. */
  1316. xfs_dir2_leafn_rebalance(state, oldblk, newblk);
  1317. error = xfs_da3_blk_link(state, oldblk, newblk);
  1318. if (error) {
  1319. return error;
  1320. }
  1321. /*
  1322. * Insert the new entry in the correct block.
  1323. */
  1324. if (state->inleaf)
  1325. error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
  1326. else
  1327. error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
  1328. /*
  1329. * Update last hashval in each block since we added the name.
  1330. */
  1331. oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
  1332. newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
  1333. xfs_dir3_leaf_check(dp, oldblk->bp);
  1334. xfs_dir3_leaf_check(dp, newblk->bp);
  1335. return error;
  1336. }
  1337. /*
  1338. * Check a leaf block and its neighbors to see if the block should be
  1339. * collapsed into one or the other neighbor. Always keep the block
  1340. * with the smaller block number.
  1341. * If the current block is over 50% full, don't try to join it, return 0.
  1342. * If the block is empty, fill in the state structure and return 2.
  1343. * If it can be collapsed, fill in the state structure and return 1.
  1344. * If nothing can be done, return 0.
  1345. */
  1346. int /* error */
  1347. xfs_dir2_leafn_toosmall(
  1348. xfs_da_state_t *state, /* btree cursor */
  1349. int *action) /* resulting action to take */
  1350. {
  1351. xfs_da_state_blk_t *blk; /* leaf block */
  1352. xfs_dablk_t blkno; /* leaf block number */
  1353. struct xfs_buf *bp; /* leaf buffer */
  1354. int bytes; /* bytes in use */
  1355. int count; /* leaf live entry count */
  1356. int error; /* error return value */
  1357. int forward; /* sibling block direction */
  1358. int i; /* sibling counter */
  1359. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1360. int rval; /* result from path_shift */
  1361. struct xfs_dir3_icleaf_hdr leafhdr;
  1362. struct xfs_dir2_leaf_entry *ents;
  1363. struct xfs_inode *dp = state->args->dp;
  1364. /*
  1365. * Check for the degenerate case of the block being over 50% full.
  1366. * If so, it's not worth even looking to see if we might be able
  1367. * to coalesce with a sibling.
  1368. */
  1369. blk = &state->path.blk[state->path.active - 1];
  1370. leaf = blk->bp->b_addr;
  1371. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
  1372. ents = leafhdr.ents;
  1373. xfs_dir3_leaf_check(dp, blk->bp);
  1374. count = leafhdr.count - leafhdr.stale;
  1375. bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
  1376. if (bytes > (state->args->geo->blksize >> 1)) {
  1377. /*
  1378. * Blk over 50%, don't try to join.
  1379. */
  1380. *action = 0;
  1381. return 0;
  1382. }
  1383. /*
  1384. * Check for the degenerate case of the block being empty.
  1385. * If the block is empty, we'll simply delete it, no need to
  1386. * coalesce it with a sibling block. We choose (arbitrarily)
  1387. * to merge with the forward block unless it is NULL.
  1388. */
  1389. if (count == 0) {
  1390. /*
  1391. * Make altpath point to the block we want to keep and
  1392. * path point to the block we want to drop (this one).
  1393. */
  1394. forward = (leafhdr.forw != 0);
  1395. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1396. error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
  1397. &rval);
  1398. if (error)
  1399. return error;
  1400. *action = rval ? 2 : 0;
  1401. return 0;
  1402. }
  1403. /*
  1404. * Examine each sibling block to see if we can coalesce with
  1405. * at least 25% free space to spare. We need to figure out
  1406. * whether to merge with the forward or the backward block.
  1407. * We prefer coalescing with the lower numbered sibling so as
  1408. * to shrink a directory over time.
  1409. */
  1410. forward = leafhdr.forw < leafhdr.back;
  1411. for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
  1412. struct xfs_dir3_icleaf_hdr hdr2;
  1413. blkno = forward ? leafhdr.forw : leafhdr.back;
  1414. if (blkno == 0)
  1415. continue;
  1416. /*
  1417. * Read the sibling leaf block.
  1418. */
  1419. error = xfs_dir3_leafn_read(state->args->trans, dp, blkno, &bp);
  1420. if (error)
  1421. return error;
  1422. /*
  1423. * Count bytes in the two blocks combined.
  1424. */
  1425. count = leafhdr.count - leafhdr.stale;
  1426. bytes = state->args->geo->blksize -
  1427. (state->args->geo->blksize >> 2);
  1428. leaf = bp->b_addr;
  1429. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
  1430. ents = hdr2.ents;
  1431. count += hdr2.count - hdr2.stale;
  1432. bytes -= count * sizeof(ents[0]);
  1433. /*
  1434. * Fits with at least 25% to spare.
  1435. */
  1436. if (bytes >= 0)
  1437. break;
  1438. xfs_trans_brelse(state->args->trans, bp);
  1439. }
  1440. /*
  1441. * Didn't like either block, give up.
  1442. */
  1443. if (i >= 2) {
  1444. *action = 0;
  1445. return 0;
  1446. }
  1447. /*
  1448. * Make altpath point to the block we want to keep (the lower
  1449. * numbered block) and path point to the block we want to drop.
  1450. */
  1451. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1452. if (blkno < blk->blkno)
  1453. error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
  1454. &rval);
  1455. else
  1456. error = xfs_da3_path_shift(state, &state->path, forward, 0,
  1457. &rval);
  1458. if (error) {
  1459. return error;
  1460. }
  1461. *action = rval ? 0 : 1;
  1462. return 0;
  1463. }
  1464. /*
  1465. * Move all the leaf entries from drop_blk to save_blk.
  1466. * This is done as part of a join operation.
  1467. */
  1468. void
  1469. xfs_dir2_leafn_unbalance(
  1470. xfs_da_state_t *state, /* cursor */
  1471. xfs_da_state_blk_t *drop_blk, /* dead block */
  1472. xfs_da_state_blk_t *save_blk) /* surviving block */
  1473. {
  1474. xfs_da_args_t *args; /* operation arguments */
  1475. xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
  1476. xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
  1477. struct xfs_dir3_icleaf_hdr savehdr;
  1478. struct xfs_dir3_icleaf_hdr drophdr;
  1479. struct xfs_dir2_leaf_entry *sents;
  1480. struct xfs_dir2_leaf_entry *dents;
  1481. struct xfs_inode *dp = state->args->dp;
  1482. args = state->args;
  1483. ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1484. ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1485. drop_leaf = drop_blk->bp->b_addr;
  1486. save_leaf = save_blk->bp->b_addr;
  1487. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
  1488. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
  1489. sents = savehdr.ents;
  1490. dents = drophdr.ents;
  1491. /*
  1492. * If there are any stale leaf entries, take this opportunity
  1493. * to purge them.
  1494. */
  1495. if (drophdr.stale)
  1496. xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
  1497. if (savehdr.stale)
  1498. xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
  1499. /*
  1500. * Move the entries from drop to the appropriate end of save.
  1501. */
  1502. drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
  1503. if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
  1504. xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
  1505. save_blk->bp, &savehdr, sents, 0,
  1506. drophdr.count);
  1507. else
  1508. xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
  1509. save_blk->bp, &savehdr, sents,
  1510. savehdr.count, drophdr.count);
  1511. save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
  1512. /* log the changes made when moving the entries */
  1513. xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
  1514. xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
  1515. xfs_dir3_leaf_log_header(args, save_blk->bp);
  1516. xfs_dir3_leaf_log_header(args, drop_blk->bp);
  1517. xfs_dir3_leaf_check(dp, save_blk->bp);
  1518. xfs_dir3_leaf_check(dp, drop_blk->bp);
  1519. }
  1520. /*
  1521. * Add a new data block to the directory at the free space index that the caller
  1522. * has specified.
  1523. */
  1524. static int
  1525. xfs_dir2_node_add_datablk(
  1526. struct xfs_da_args *args,
  1527. struct xfs_da_state_blk *fblk,
  1528. xfs_dir2_db_t *dbno,
  1529. struct xfs_buf **dbpp,
  1530. struct xfs_buf **fbpp,
  1531. struct xfs_dir3_icfree_hdr *hdr,
  1532. int *findex)
  1533. {
  1534. struct xfs_inode *dp = args->dp;
  1535. struct xfs_trans *tp = args->trans;
  1536. struct xfs_mount *mp = dp->i_mount;
  1537. struct xfs_dir2_data_free *bf;
  1538. xfs_dir2_db_t fbno;
  1539. struct xfs_buf *fbp;
  1540. struct xfs_buf *dbp;
  1541. int error;
  1542. /* Not allowed to allocate, return failure. */
  1543. if (args->total == 0)
  1544. return -ENOSPC;
  1545. /* Allocate and initialize the new data block. */
  1546. error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
  1547. if (error)
  1548. return error;
  1549. error = xfs_dir3_data_init(args, *dbno, &dbp);
  1550. if (error)
  1551. return error;
  1552. /*
  1553. * Get the freespace block corresponding to the data block
  1554. * that was just allocated.
  1555. */
  1556. fbno = xfs_dir2_db_to_fdb(args->geo, *dbno);
  1557. error = xfs_dir2_free_try_read(tp, dp,
  1558. xfs_dir2_db_to_da(args->geo, fbno), &fbp);
  1559. if (error)
  1560. return error;
  1561. /*
  1562. * If there wasn't a freespace block, the read will
  1563. * return a NULL fbp. Allocate and initialize a new one.
  1564. */
  1565. if (!fbp) {
  1566. error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
  1567. if (error)
  1568. return error;
  1569. if (XFS_IS_CORRUPT(mp,
  1570. xfs_dir2_db_to_fdb(args->geo, *dbno) !=
  1571. fbno)) {
  1572. xfs_alert(mp,
  1573. "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
  1574. __func__, (unsigned long long)dp->i_ino,
  1575. (long long)xfs_dir2_db_to_fdb(args->geo, *dbno),
  1576. (long long)*dbno, (long long)fbno);
  1577. if (fblk) {
  1578. xfs_alert(mp,
  1579. " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
  1580. fblk, (unsigned long long)fblk->blkno,
  1581. fblk->index, fblk->magic);
  1582. } else {
  1583. xfs_alert(mp, " ... fblk is NULL");
  1584. }
  1585. return -EFSCORRUPTED;
  1586. }
  1587. /* Get a buffer for the new block. */
  1588. error = xfs_dir3_free_get_buf(args, fbno, &fbp);
  1589. if (error)
  1590. return error;
  1591. xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
  1592. /* Remember the first slot as our empty slot. */
  1593. hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
  1594. XFS_DIR2_FREE_OFFSET)) *
  1595. args->geo->free_max_bests;
  1596. } else {
  1597. xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
  1598. }
  1599. /* Set the freespace block index from the data block number. */
  1600. *findex = xfs_dir2_db_to_fdindex(args->geo, *dbno);
  1601. /* Extend the freespace table if the new data block is off the end. */
  1602. if (*findex >= hdr->nvalid) {
  1603. ASSERT(*findex < args->geo->free_max_bests);
  1604. hdr->nvalid = *findex + 1;
  1605. hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF);
  1606. }
  1607. /*
  1608. * If this entry was for an empty data block (this should always be
  1609. * true) then update the header.
  1610. */
  1611. if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
  1612. hdr->nused++;
  1613. xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr);
  1614. xfs_dir2_free_log_header(args, fbp);
  1615. }
  1616. /* Update the freespace value for the new block in the table. */
  1617. bf = xfs_dir2_data_bestfree_p(mp, dbp->b_addr);
  1618. hdr->bests[*findex] = bf[0].length;
  1619. *dbpp = dbp;
  1620. *fbpp = fbp;
  1621. return 0;
  1622. }
  1623. static int
  1624. xfs_dir2_node_find_freeblk(
  1625. struct xfs_da_args *args,
  1626. struct xfs_da_state_blk *fblk,
  1627. xfs_dir2_db_t *dbnop,
  1628. struct xfs_buf **fbpp,
  1629. struct xfs_dir3_icfree_hdr *hdr,
  1630. int *findexp,
  1631. int length)
  1632. {
  1633. struct xfs_inode *dp = args->dp;
  1634. struct xfs_trans *tp = args->trans;
  1635. struct xfs_buf *fbp = NULL;
  1636. xfs_dir2_db_t firstfbno;
  1637. xfs_dir2_db_t lastfbno;
  1638. xfs_dir2_db_t ifbno = -1;
  1639. xfs_dir2_db_t dbno = -1;
  1640. xfs_dir2_db_t fbno;
  1641. xfs_fileoff_t fo;
  1642. int findex = 0;
  1643. int error;
  1644. /*
  1645. * If we came in with a freespace block that means that lookup
  1646. * found an entry with our hash value. This is the freespace
  1647. * block for that data entry.
  1648. */
  1649. if (fblk) {
  1650. fbp = fblk->bp;
  1651. findex = fblk->index;
  1652. xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
  1653. if (findex >= 0) {
  1654. /* caller already found the freespace for us. */
  1655. ASSERT(findex < hdr->nvalid);
  1656. ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF);
  1657. ASSERT(be16_to_cpu(hdr->bests[findex]) >= length);
  1658. dbno = hdr->firstdb + findex;
  1659. goto found_block;
  1660. }
  1661. /*
  1662. * The data block looked at didn't have enough room.
  1663. * We'll start at the beginning of the freespace entries.
  1664. */
  1665. ifbno = fblk->blkno;
  1666. xfs_trans_brelse(tp, fbp);
  1667. fbp = NULL;
  1668. fblk->bp = NULL;
  1669. }
  1670. /*
  1671. * If we don't have a data block yet, we're going to scan the freespace
  1672. * data for a data block with enough free space in it.
  1673. */
  1674. error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
  1675. if (error)
  1676. return error;
  1677. lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
  1678. firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
  1679. for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
  1680. /* If it's ifbno we already looked at it. */
  1681. if (fbno == ifbno)
  1682. continue;
  1683. /*
  1684. * Read the block. There can be holes in the freespace blocks,
  1685. * so this might not succeed. This should be really rare, so
  1686. * there's no reason to avoid it.
  1687. */
  1688. error = xfs_dir2_free_try_read(tp, dp,
  1689. xfs_dir2_db_to_da(args->geo, fbno),
  1690. &fbp);
  1691. if (error)
  1692. return error;
  1693. if (!fbp)
  1694. continue;
  1695. xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
  1696. /* Scan the free entry array for a large enough free space. */
  1697. for (findex = hdr->nvalid - 1; findex >= 0; findex--) {
  1698. if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF &&
  1699. be16_to_cpu(hdr->bests[findex]) >= length) {
  1700. dbno = hdr->firstdb + findex;
  1701. goto found_block;
  1702. }
  1703. }
  1704. /* Didn't find free space, go on to next free block */
  1705. xfs_trans_brelse(tp, fbp);
  1706. }
  1707. found_block:
  1708. *dbnop = dbno;
  1709. *fbpp = fbp;
  1710. *findexp = findex;
  1711. return 0;
  1712. }
  1713. /*
  1714. * Add the data entry for a node-format directory name addition.
  1715. * The leaf entry is added in xfs_dir2_leafn_add.
  1716. * We may enter with a freespace block that the lookup found.
  1717. */
  1718. static int
  1719. xfs_dir2_node_addname_int(
  1720. struct xfs_da_args *args, /* operation arguments */
  1721. struct xfs_da_state_blk *fblk) /* optional freespace block */
  1722. {
  1723. struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
  1724. struct xfs_dir2_data_entry *dep; /* data entry pointer */
  1725. struct xfs_dir2_data_hdr *hdr; /* data block header */
  1726. struct xfs_dir2_data_free *bf;
  1727. struct xfs_trans *tp = args->trans;
  1728. struct xfs_inode *dp = args->dp;
  1729. struct xfs_dir3_icfree_hdr freehdr;
  1730. struct xfs_buf *dbp; /* data block buffer */
  1731. struct xfs_buf *fbp; /* freespace buffer */
  1732. xfs_dir2_data_aoff_t aoff;
  1733. xfs_dir2_db_t dbno; /* data block number */
  1734. int error; /* error return value */
  1735. int findex; /* freespace entry index */
  1736. int length; /* length of the new entry */
  1737. int logfree = 0; /* need to log free entry */
  1738. int needlog = 0; /* need to log data header */
  1739. int needscan = 0; /* need to rescan data frees */
  1740. __be16 *tagp; /* data entry tag pointer */
  1741. length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
  1742. error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr,
  1743. &findex, length);
  1744. if (error)
  1745. return error;
  1746. /*
  1747. * Now we know if we must allocate blocks, so if we are checking whether
  1748. * we can insert without allocation then we can return now.
  1749. */
  1750. if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
  1751. if (dbno == -1)
  1752. return -ENOSPC;
  1753. return 0;
  1754. }
  1755. /*
  1756. * If we don't have a data block, we need to allocate one and make
  1757. * the freespace entries refer to it.
  1758. */
  1759. if (dbno == -1) {
  1760. /* we're going to have to log the free block index later */
  1761. logfree = 1;
  1762. error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
  1763. &freehdr, &findex);
  1764. } else {
  1765. /* Read the data block in. */
  1766. error = xfs_dir3_data_read(tp, dp,
  1767. xfs_dir2_db_to_da(args->geo, dbno),
  1768. 0, &dbp);
  1769. }
  1770. if (error)
  1771. return error;
  1772. /* setup for data block up now */
  1773. hdr = dbp->b_addr;
  1774. bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
  1775. ASSERT(be16_to_cpu(bf[0].length) >= length);
  1776. /* Point to the existing unused space. */
  1777. dup = (xfs_dir2_data_unused_t *)
  1778. ((char *)hdr + be16_to_cpu(bf[0].offset));
  1779. /* Mark the first part of the unused space, inuse for us. */
  1780. aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
  1781. error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
  1782. &needlog, &needscan);
  1783. if (error) {
  1784. xfs_trans_brelse(tp, dbp);
  1785. return error;
  1786. }
  1787. /* Fill in the new entry and log it. */
  1788. dep = (xfs_dir2_data_entry_t *)dup;
  1789. dep->inumber = cpu_to_be64(args->inumber);
  1790. dep->namelen = args->namelen;
  1791. memcpy(dep->name, args->name, dep->namelen);
  1792. xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
  1793. tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
  1794. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1795. xfs_dir2_data_log_entry(args, dbp, dep);
  1796. /* Rescan the freespace and log the data block if needed. */
  1797. if (needscan)
  1798. xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
  1799. if (needlog)
  1800. xfs_dir2_data_log_header(args, dbp);
  1801. /* If the freespace block entry is now wrong, update it. */
  1802. if (freehdr.bests[findex] != bf[0].length) {
  1803. freehdr.bests[findex] = bf[0].length;
  1804. logfree = 1;
  1805. }
  1806. /* Log the freespace entry if needed. */
  1807. if (logfree)
  1808. xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
  1809. /* Return the data block and offset in args. */
  1810. args->blkno = (xfs_dablk_t)dbno;
  1811. args->index = be16_to_cpu(*tagp);
  1812. return 0;
  1813. }
  1814. /*
  1815. * Top-level node form directory addname routine.
  1816. */
  1817. int /* error */
  1818. xfs_dir2_node_addname(
  1819. xfs_da_args_t *args) /* operation arguments */
  1820. {
  1821. xfs_da_state_blk_t *blk; /* leaf block for insert */
  1822. int error; /* error return value */
  1823. int rval; /* sub-return value */
  1824. xfs_da_state_t *state; /* btree cursor */
  1825. trace_xfs_dir2_node_addname(args);
  1826. /*
  1827. * Allocate and initialize the state (btree cursor).
  1828. */
  1829. state = xfs_da_state_alloc(args);
  1830. /*
  1831. * Look up the name. We're not supposed to find it, but
  1832. * this gives us the insertion point.
  1833. */
  1834. error = xfs_da3_node_lookup_int(state, &rval);
  1835. if (error)
  1836. rval = error;
  1837. if (rval != -ENOENT) {
  1838. goto done;
  1839. }
  1840. /*
  1841. * Add the data entry to a data block.
  1842. * Extravalid is set to a freeblock found by lookup.
  1843. */
  1844. rval = xfs_dir2_node_addname_int(args,
  1845. state->extravalid ? &state->extrablk : NULL);
  1846. if (rval) {
  1847. goto done;
  1848. }
  1849. blk = &state->path.blk[state->path.active - 1];
  1850. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1851. /*
  1852. * Add the new leaf entry.
  1853. */
  1854. rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
  1855. if (rval == 0) {
  1856. /*
  1857. * It worked, fix the hash values up the btree.
  1858. */
  1859. if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
  1860. xfs_da3_fixhashpath(state, &state->path);
  1861. } else {
  1862. /*
  1863. * It didn't work, we need to split the leaf block.
  1864. */
  1865. if (args->total == 0) {
  1866. ASSERT(rval == -ENOSPC);
  1867. goto done;
  1868. }
  1869. /*
  1870. * Split the leaf block and insert the new entry.
  1871. */
  1872. rval = xfs_da3_split(state);
  1873. }
  1874. done:
  1875. xfs_da_state_free(state);
  1876. return rval;
  1877. }
  1878. /*
  1879. * Lookup an entry in a node-format directory.
  1880. * All the real work happens in xfs_da3_node_lookup_int.
  1881. * The only real output is the inode number of the entry.
  1882. */
  1883. int /* error */
  1884. xfs_dir2_node_lookup(
  1885. xfs_da_args_t *args) /* operation arguments */
  1886. {
  1887. int error; /* error return value */
  1888. int i; /* btree level */
  1889. int rval; /* operation return value */
  1890. xfs_da_state_t *state; /* btree cursor */
  1891. trace_xfs_dir2_node_lookup(args);
  1892. /*
  1893. * Allocate and initialize the btree cursor.
  1894. */
  1895. state = xfs_da_state_alloc(args);
  1896. /*
  1897. * Fill in the path to the entry in the cursor.
  1898. */
  1899. error = xfs_da3_node_lookup_int(state, &rval);
  1900. if (error)
  1901. rval = error;
  1902. else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
  1903. /* If a CI match, dup the actual name and return -EEXIST */
  1904. xfs_dir2_data_entry_t *dep;
  1905. dep = (xfs_dir2_data_entry_t *)
  1906. ((char *)state->extrablk.bp->b_addr +
  1907. state->extrablk.index);
  1908. rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  1909. }
  1910. /*
  1911. * Release the btree blocks and leaf block.
  1912. */
  1913. for (i = 0; i < state->path.active; i++) {
  1914. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1915. state->path.blk[i].bp = NULL;
  1916. }
  1917. /*
  1918. * Release the data block if we have it.
  1919. */
  1920. if (state->extravalid && state->extrablk.bp) {
  1921. xfs_trans_brelse(args->trans, state->extrablk.bp);
  1922. state->extrablk.bp = NULL;
  1923. }
  1924. xfs_da_state_free(state);
  1925. return rval;
  1926. }
  1927. /*
  1928. * Remove an entry from a node-format directory.
  1929. */
  1930. int /* error */
  1931. xfs_dir2_node_removename(
  1932. struct xfs_da_args *args) /* operation arguments */
  1933. {
  1934. struct xfs_da_state_blk *blk; /* leaf block */
  1935. int error; /* error return value */
  1936. int rval; /* operation return value */
  1937. struct xfs_da_state *state; /* btree cursor */
  1938. trace_xfs_dir2_node_removename(args);
  1939. /*
  1940. * Allocate and initialize the btree cursor.
  1941. */
  1942. state = xfs_da_state_alloc(args);
  1943. /* Look up the entry we're deleting, set up the cursor. */
  1944. error = xfs_da3_node_lookup_int(state, &rval);
  1945. if (error)
  1946. goto out_free;
  1947. /* Didn't find it, upper layer screwed up. */
  1948. if (rval != -EEXIST) {
  1949. error = rval;
  1950. goto out_free;
  1951. }
  1952. blk = &state->path.blk[state->path.active - 1];
  1953. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1954. ASSERT(state->extravalid);
  1955. /*
  1956. * Remove the leaf and data entries.
  1957. * Extrablk refers to the data block.
  1958. */
  1959. error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
  1960. &state->extrablk, &rval);
  1961. if (error)
  1962. goto out_free;
  1963. /*
  1964. * Fix the hash values up the btree.
  1965. */
  1966. xfs_da3_fixhashpath(state, &state->path);
  1967. /*
  1968. * If we need to join leaf blocks, do it.
  1969. */
  1970. if (rval && state->path.active > 1)
  1971. error = xfs_da3_join(state);
  1972. /*
  1973. * If no errors so far, try conversion to leaf format.
  1974. */
  1975. if (!error)
  1976. error = xfs_dir2_node_to_leaf(state);
  1977. out_free:
  1978. xfs_da_state_free(state);
  1979. return error;
  1980. }
  1981. /*
  1982. * Replace an entry's inode number in a node-format directory.
  1983. */
  1984. int /* error */
  1985. xfs_dir2_node_replace(
  1986. xfs_da_args_t *args) /* operation arguments */
  1987. {
  1988. xfs_da_state_blk_t *blk; /* leaf block */
  1989. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1990. xfs_dir2_data_entry_t *dep; /* data entry changed */
  1991. int error; /* error return value */
  1992. int i; /* btree level */
  1993. xfs_ino_t inum; /* new inode number */
  1994. int ftype; /* new file type */
  1995. int rval; /* internal return value */
  1996. xfs_da_state_t *state; /* btree cursor */
  1997. trace_xfs_dir2_node_replace(args);
  1998. /*
  1999. * Allocate and initialize the btree cursor.
  2000. */
  2001. state = xfs_da_state_alloc(args);
  2002. /*
  2003. * We have to save new inode number and ftype since
  2004. * xfs_da3_node_lookup_int() is going to overwrite them
  2005. */
  2006. inum = args->inumber;
  2007. ftype = args->filetype;
  2008. /*
  2009. * Lookup the entry to change in the btree.
  2010. */
  2011. error = xfs_da3_node_lookup_int(state, &rval);
  2012. if (error) {
  2013. rval = error;
  2014. }
  2015. /*
  2016. * It should be found, since the vnodeops layer has looked it up
  2017. * and locked it. But paranoia is good.
  2018. */
  2019. if (rval == -EEXIST) {
  2020. struct xfs_dir3_icleaf_hdr leafhdr;
  2021. /*
  2022. * Find the leaf entry.
  2023. */
  2024. blk = &state->path.blk[state->path.active - 1];
  2025. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  2026. ASSERT(state->extravalid);
  2027. xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
  2028. blk->bp->b_addr);
  2029. /*
  2030. * Point to the data entry.
  2031. */
  2032. hdr = state->extrablk.bp->b_addr;
  2033. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  2034. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
  2035. dep = (xfs_dir2_data_entry_t *)
  2036. ((char *)hdr +
  2037. xfs_dir2_dataptr_to_off(args->geo,
  2038. be32_to_cpu(leafhdr.ents[blk->index].address)));
  2039. ASSERT(inum != be64_to_cpu(dep->inumber));
  2040. /*
  2041. * Fill in the new inode number and log the entry.
  2042. */
  2043. dep->inumber = cpu_to_be64(inum);
  2044. xfs_dir2_data_put_ftype(state->mp, dep, ftype);
  2045. xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
  2046. rval = 0;
  2047. }
  2048. /*
  2049. * Didn't find it, and we're holding a data block. Drop it.
  2050. */
  2051. else if (state->extravalid) {
  2052. xfs_trans_brelse(args->trans, state->extrablk.bp);
  2053. state->extrablk.bp = NULL;
  2054. }
  2055. /*
  2056. * Release all the buffers in the cursor.
  2057. */
  2058. for (i = 0; i < state->path.active; i++) {
  2059. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  2060. state->path.blk[i].bp = NULL;
  2061. }
  2062. xfs_da_state_free(state);
  2063. return rval;
  2064. }
  2065. /*
  2066. * Trim off a trailing empty freespace block.
  2067. * Return (in rvalp) 1 if we did it, 0 if not.
  2068. */
  2069. int /* error */
  2070. xfs_dir2_node_trim_free(
  2071. xfs_da_args_t *args, /* operation arguments */
  2072. xfs_fileoff_t fo, /* free block number */
  2073. int *rvalp) /* out: did something */
  2074. {
  2075. struct xfs_buf *bp; /* freespace buffer */
  2076. xfs_inode_t *dp; /* incore directory inode */
  2077. int error; /* error return code */
  2078. xfs_dir2_free_t *free; /* freespace structure */
  2079. xfs_trans_t *tp; /* transaction pointer */
  2080. struct xfs_dir3_icfree_hdr freehdr;
  2081. dp = args->dp;
  2082. tp = args->trans;
  2083. *rvalp = 0;
  2084. /*
  2085. * Read the freespace block.
  2086. */
  2087. error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
  2088. if (error)
  2089. return error;
  2090. /*
  2091. * There can be holes in freespace. If fo is a hole, there's
  2092. * nothing to do.
  2093. */
  2094. if (!bp)
  2095. return 0;
  2096. free = bp->b_addr;
  2097. xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
  2098. /*
  2099. * If there are used entries, there's nothing to do.
  2100. */
  2101. if (freehdr.nused > 0) {
  2102. xfs_trans_brelse(tp, bp);
  2103. return 0;
  2104. }
  2105. /*
  2106. * Blow the block away.
  2107. */
  2108. error = xfs_dir2_shrink_inode(args,
  2109. xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
  2110. if (error) {
  2111. /*
  2112. * Can't fail with ENOSPC since that only happens with no
  2113. * space reservation, when breaking up an extent into two
  2114. * pieces. This is the last block of an extent.
  2115. */
  2116. ASSERT(error != -ENOSPC);
  2117. xfs_trans_brelse(tp, bp);
  2118. return error;
  2119. }
  2120. /*
  2121. * Return that we succeeded.
  2122. */
  2123. *rvalp = 1;
  2124. return 0;
  2125. }