xfs_dir2_node.c 64 KB

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