xfs_dir2_node.c 57 KB

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