xfs_dir2_node.c 58 KB

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