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