xfs_dir2_node.c 62 KB

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