xfs_dir2_node.c 59 KB

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