xfs_dir2_node.c 62 KB

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