xfs_dir2_node.c 60 KB

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