xfs_dir2_node.c 62 KB

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