xfs_dir2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * Copyright (c) 2000-2001,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_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_inode_item.h"
  38. #include "xfs_bmap.h"
  39. #include "xfs_dir2_data.h"
  40. #include "xfs_dir2_leaf.h"
  41. #include "xfs_dir2_block.h"
  42. #include "xfs_dir2_node.h"
  43. #include "xfs_dir2_trace.h"
  44. #include "xfs_error.h"
  45. void
  46. xfs_dir_mount(
  47. xfs_mount_t *mp)
  48. {
  49. ASSERT(XFS_SB_VERSION_HASDIRV2(&mp->m_sb));
  50. ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <=
  51. XFS_MAX_BLOCKSIZE);
  52. mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog);
  53. mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog;
  54. mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp));
  55. mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp));
  56. mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp));
  57. mp->m_attr_node_ents =
  58. (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) /
  59. (uint)sizeof(xfs_da_node_entry_t);
  60. mp->m_dir_node_ents =
  61. (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) /
  62. (uint)sizeof(xfs_da_node_entry_t);
  63. mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100;
  64. }
  65. /*
  66. * Return 1 if directory contains only "." and "..".
  67. */
  68. int
  69. xfs_dir_isempty(
  70. xfs_inode_t *dp)
  71. {
  72. xfs_dir2_sf_t *sfp;
  73. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  74. if (dp->i_d.di_size == 0) /* might happen during shutdown. */
  75. return 1;
  76. if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
  77. return 0;
  78. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  79. return !sfp->hdr.count;
  80. }
  81. /*
  82. * Validate a given inode number.
  83. */
  84. int
  85. xfs_dir_ino_validate(
  86. xfs_mount_t *mp,
  87. xfs_ino_t ino)
  88. {
  89. xfs_agblock_t agblkno;
  90. xfs_agino_t agino;
  91. xfs_agnumber_t agno;
  92. int ino_ok;
  93. int ioff;
  94. agno = XFS_INO_TO_AGNO(mp, ino);
  95. agblkno = XFS_INO_TO_AGBNO(mp, ino);
  96. ioff = XFS_INO_TO_OFFSET(mp, ino);
  97. agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
  98. ino_ok =
  99. agno < mp->m_sb.sb_agcount &&
  100. agblkno < mp->m_sb.sb_agblocks &&
  101. agblkno != 0 &&
  102. ioff < (1 << mp->m_sb.sb_inopblog) &&
  103. XFS_AGINO_TO_INO(mp, agno, agino) == ino;
  104. if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
  105. XFS_RANDOM_DIR_INO_VALIDATE))) {
  106. xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx",
  107. (unsigned long long) ino);
  108. XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
  109. return XFS_ERROR(EFSCORRUPTED);
  110. }
  111. return 0;
  112. }
  113. /*
  114. * Initialize a directory with its "." and ".." entries.
  115. */
  116. int
  117. xfs_dir_init(
  118. xfs_trans_t *tp,
  119. xfs_inode_t *dp,
  120. xfs_inode_t *pdp)
  121. {
  122. xfs_da_args_t args;
  123. int error;
  124. memset((char *)&args, 0, sizeof(args));
  125. args.dp = dp;
  126. args.trans = tp;
  127. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  128. if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino)))
  129. return error;
  130. return xfs_dir2_sf_create(&args, pdp->i_ino);
  131. }
  132. /*
  133. Enter a name in a directory.
  134. */
  135. int
  136. xfs_dir_createname(
  137. xfs_trans_t *tp,
  138. xfs_inode_t *dp,
  139. char *name,
  140. int namelen,
  141. xfs_ino_t inum, /* new entry inode number */
  142. xfs_fsblock_t *first, /* bmap's firstblock */
  143. xfs_bmap_free_t *flist, /* bmap's freeblock list */
  144. xfs_extlen_t total) /* bmap's total block count */
  145. {
  146. xfs_da_args_t args;
  147. int rval;
  148. int v; /* type-checking value */
  149. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  150. if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
  151. return rval;
  152. XFS_STATS_INC(xs_dir_create);
  153. args.name = name;
  154. args.namelen = namelen;
  155. args.hashval = xfs_da_hashname(name, namelen);
  156. args.inumber = inum;
  157. args.dp = dp;
  158. args.firstblock = first;
  159. args.flist = flist;
  160. args.total = total;
  161. args.whichfork = XFS_DATA_FORK;
  162. args.trans = tp;
  163. args.justcheck = 0;
  164. args.addname = args.oknoent = 1;
  165. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  166. rval = xfs_dir2_sf_addname(&args);
  167. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  168. return rval;
  169. else if (v)
  170. rval = xfs_dir2_block_addname(&args);
  171. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  172. return rval;
  173. else if (v)
  174. rval = xfs_dir2_leaf_addname(&args);
  175. else
  176. rval = xfs_dir2_node_addname(&args);
  177. return rval;
  178. }
  179. /*
  180. * Lookup a name in a directory, give back the inode number.
  181. */
  182. int
  183. xfs_dir_lookup(
  184. xfs_trans_t *tp,
  185. xfs_inode_t *dp,
  186. char *name,
  187. int namelen,
  188. xfs_ino_t *inum) /* out: inode number */
  189. {
  190. xfs_da_args_t args;
  191. int rval;
  192. int v; /* type-checking value */
  193. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  194. XFS_STATS_INC(xs_dir_lookup);
  195. args.name = name;
  196. args.namelen = namelen;
  197. args.hashval = xfs_da_hashname(name, namelen);
  198. args.inumber = 0;
  199. args.dp = dp;
  200. args.firstblock = NULL;
  201. args.flist = NULL;
  202. args.total = 0;
  203. args.whichfork = XFS_DATA_FORK;
  204. args.trans = tp;
  205. args.justcheck = args.addname = 0;
  206. args.oknoent = 1;
  207. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  208. rval = xfs_dir2_sf_lookup(&args);
  209. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  210. return rval;
  211. else if (v)
  212. rval = xfs_dir2_block_lookup(&args);
  213. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  214. return rval;
  215. else if (v)
  216. rval = xfs_dir2_leaf_lookup(&args);
  217. else
  218. rval = xfs_dir2_node_lookup(&args);
  219. if (rval == EEXIST)
  220. rval = 0;
  221. if (rval == 0)
  222. *inum = args.inumber;
  223. return rval;
  224. }
  225. /*
  226. * Remove an entry from a directory.
  227. */
  228. int
  229. xfs_dir_removename(
  230. xfs_trans_t *tp,
  231. xfs_inode_t *dp,
  232. char *name,
  233. int namelen,
  234. xfs_ino_t ino,
  235. xfs_fsblock_t *first, /* bmap's firstblock */
  236. xfs_bmap_free_t *flist, /* bmap's freeblock list */
  237. xfs_extlen_t total) /* bmap's total block count */
  238. {
  239. xfs_da_args_t args;
  240. int rval;
  241. int v; /* type-checking value */
  242. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  243. XFS_STATS_INC(xs_dir_remove);
  244. args.name = name;
  245. args.namelen = namelen;
  246. args.hashval = xfs_da_hashname(name, namelen);
  247. args.inumber = ino;
  248. args.dp = dp;
  249. args.firstblock = first;
  250. args.flist = flist;
  251. args.total = total;
  252. args.whichfork = XFS_DATA_FORK;
  253. args.trans = tp;
  254. args.justcheck = args.addname = args.oknoent = 0;
  255. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  256. rval = xfs_dir2_sf_removename(&args);
  257. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  258. return rval;
  259. else if (v)
  260. rval = xfs_dir2_block_removename(&args);
  261. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  262. return rval;
  263. else if (v)
  264. rval = xfs_dir2_leaf_removename(&args);
  265. else
  266. rval = xfs_dir2_node_removename(&args);
  267. return rval;
  268. }
  269. /*
  270. * Read a directory.
  271. */
  272. int
  273. xfs_readdir(
  274. bhv_desc_t *dir_bdp,
  275. void *dirent,
  276. size_t bufsize,
  277. xfs_off_t *offset,
  278. filldir_t filldir)
  279. {
  280. xfs_inode_t *dp = XFS_BHVTOI(dir_bdp);
  281. int rval; /* return value */
  282. int v; /* type-checking value */
  283. vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
  284. (inst_t *)__return_address);
  285. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  286. return XFS_ERROR(EIO);
  287. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  288. XFS_STATS_INC(xs_dir_getdents);
  289. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  290. rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir);
  291. else if ((rval = xfs_dir2_isblock(NULL, dp, &v)))
  292. ;
  293. else if (v)
  294. rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir);
  295. else
  296. rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset,
  297. filldir);
  298. return rval;
  299. }
  300. /*
  301. * Replace the inode number of a directory entry.
  302. */
  303. int
  304. xfs_dir_replace(
  305. xfs_trans_t *tp,
  306. xfs_inode_t *dp,
  307. char *name, /* name of entry to replace */
  308. int namelen,
  309. xfs_ino_t inum, /* new inode number */
  310. xfs_fsblock_t *first, /* bmap's firstblock */
  311. xfs_bmap_free_t *flist, /* bmap's freeblock list */
  312. xfs_extlen_t total) /* bmap's total block count */
  313. {
  314. xfs_da_args_t args;
  315. int rval;
  316. int v; /* type-checking value */
  317. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  318. if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
  319. return rval;
  320. args.name = name;
  321. args.namelen = namelen;
  322. args.hashval = xfs_da_hashname(name, namelen);
  323. args.inumber = inum;
  324. args.dp = dp;
  325. args.firstblock = first;
  326. args.flist = flist;
  327. args.total = total;
  328. args.whichfork = XFS_DATA_FORK;
  329. args.trans = tp;
  330. args.justcheck = args.addname = args.oknoent = 0;
  331. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  332. rval = xfs_dir2_sf_replace(&args);
  333. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  334. return rval;
  335. else if (v)
  336. rval = xfs_dir2_block_replace(&args);
  337. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  338. return rval;
  339. else if (v)
  340. rval = xfs_dir2_leaf_replace(&args);
  341. else
  342. rval = xfs_dir2_node_replace(&args);
  343. return rval;
  344. }
  345. /*
  346. * See if this entry can be added to the directory without allocating space.
  347. */
  348. int
  349. xfs_dir_canenter(
  350. xfs_trans_t *tp,
  351. xfs_inode_t *dp,
  352. char *name, /* name of entry to add */
  353. int namelen)
  354. {
  355. xfs_da_args_t args;
  356. int rval;
  357. int v; /* type-checking value */
  358. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  359. args.name = name;
  360. args.namelen = namelen;
  361. args.hashval = xfs_da_hashname(name, namelen);
  362. args.inumber = 0;
  363. args.dp = dp;
  364. args.firstblock = NULL;
  365. args.flist = NULL;
  366. args.total = 0;
  367. args.whichfork = XFS_DATA_FORK;
  368. args.trans = tp;
  369. args.justcheck = args.addname = args.oknoent = 1;
  370. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  371. rval = xfs_dir2_sf_addname(&args);
  372. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  373. return rval;
  374. else if (v)
  375. rval = xfs_dir2_block_addname(&args);
  376. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  377. return rval;
  378. else if (v)
  379. rval = xfs_dir2_leaf_addname(&args);
  380. else
  381. rval = xfs_dir2_node_addname(&args);
  382. return rval;
  383. }
  384. /*
  385. * Utility routines.
  386. */
  387. /*
  388. * Add a block to the directory.
  389. * This routine is for data and free blocks, not leaf/node blocks
  390. * which are handled by xfs_da_grow_inode.
  391. */
  392. int
  393. xfs_dir2_grow_inode(
  394. xfs_da_args_t *args,
  395. int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
  396. xfs_dir2_db_t *dbp) /* out: block number added */
  397. {
  398. xfs_fileoff_t bno; /* directory offset of new block */
  399. int count; /* count of filesystem blocks */
  400. xfs_inode_t *dp; /* incore directory inode */
  401. int error;
  402. int got; /* blocks actually mapped */
  403. int i;
  404. xfs_bmbt_irec_t map; /* single structure for bmap */
  405. int mapi; /* mapping index */
  406. xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
  407. xfs_mount_t *mp;
  408. int nmap; /* number of bmap entries */
  409. xfs_trans_t *tp;
  410. xfs_dir2_trace_args_s("grow_inode", args, space);
  411. dp = args->dp;
  412. tp = args->trans;
  413. mp = dp->i_mount;
  414. /*
  415. * Set lowest possible block in the space requested.
  416. */
  417. bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
  418. count = mp->m_dirblkfsbs;
  419. /*
  420. * Find the first hole for our block.
  421. */
  422. if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK)))
  423. return error;
  424. nmap = 1;
  425. ASSERT(args->firstblock != NULL);
  426. /*
  427. * Try mapping the new block contiguously (one extent).
  428. */
  429. if ((error = xfs_bmapi(tp, dp, bno, count,
  430. XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
  431. args->firstblock, args->total, &map, &nmap,
  432. args->flist, NULL)))
  433. return error;
  434. ASSERT(nmap <= 1);
  435. if (nmap == 1) {
  436. mapp = &map;
  437. mapi = 1;
  438. }
  439. /*
  440. * Didn't work and this is a multiple-fsb directory block.
  441. * Try again with contiguous flag turned on.
  442. */
  443. else if (nmap == 0 && count > 1) {
  444. xfs_fileoff_t b; /* current file offset */
  445. /*
  446. * Space for maximum number of mappings.
  447. */
  448. mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
  449. /*
  450. * Iterate until we get to the end of our block.
  451. */
  452. for (b = bno, mapi = 0; b < bno + count; ) {
  453. int c; /* current fsb count */
  454. /*
  455. * Can't map more than MAX_NMAP at once.
  456. */
  457. nmap = MIN(XFS_BMAP_MAX_NMAP, count);
  458. c = (int)(bno + count - b);
  459. if ((error = xfs_bmapi(tp, dp, b, c,
  460. XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
  461. args->firstblock, args->total,
  462. &mapp[mapi], &nmap, args->flist,
  463. NULL))) {
  464. kmem_free(mapp, sizeof(*mapp) * count);
  465. return error;
  466. }
  467. if (nmap < 1)
  468. break;
  469. /*
  470. * Add this bunch into our table, go to the next offset.
  471. */
  472. mapi += nmap;
  473. b = mapp[mapi - 1].br_startoff +
  474. mapp[mapi - 1].br_blockcount;
  475. }
  476. }
  477. /*
  478. * Didn't work.
  479. */
  480. else {
  481. mapi = 0;
  482. mapp = NULL;
  483. }
  484. /*
  485. * See how many fsb's we got.
  486. */
  487. for (i = 0, got = 0; i < mapi; i++)
  488. got += mapp[i].br_blockcount;
  489. /*
  490. * Didn't get enough fsb's, or the first/last block's are wrong.
  491. */
  492. if (got != count || mapp[0].br_startoff != bno ||
  493. mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
  494. bno + count) {
  495. if (mapp != &map)
  496. kmem_free(mapp, sizeof(*mapp) * count);
  497. return XFS_ERROR(ENOSPC);
  498. }
  499. /*
  500. * Done with the temporary mapping table.
  501. */
  502. if (mapp != &map)
  503. kmem_free(mapp, sizeof(*mapp) * count);
  504. *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
  505. /*
  506. * Update file's size if this is the data space and it grew.
  507. */
  508. if (space == XFS_DIR2_DATA_SPACE) {
  509. xfs_fsize_t size; /* directory file (data) size */
  510. size = XFS_FSB_TO_B(mp, bno + count);
  511. if (size > dp->i_d.di_size) {
  512. dp->i_d.di_size = size;
  513. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  514. }
  515. }
  516. return 0;
  517. }
  518. /*
  519. * See if the directory is a single-block form directory.
  520. */
  521. int
  522. xfs_dir2_isblock(
  523. xfs_trans_t *tp,
  524. xfs_inode_t *dp,
  525. int *vp) /* out: 1 is block, 0 is not block */
  526. {
  527. xfs_fileoff_t last; /* last file offset */
  528. xfs_mount_t *mp;
  529. int rval;
  530. mp = dp->i_mount;
  531. if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
  532. return rval;
  533. rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
  534. ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
  535. *vp = rval;
  536. return 0;
  537. }
  538. /*
  539. * See if the directory is a single-leaf form directory.
  540. */
  541. int
  542. xfs_dir2_isleaf(
  543. xfs_trans_t *tp,
  544. xfs_inode_t *dp,
  545. int *vp) /* out: 1 is leaf, 0 is not leaf */
  546. {
  547. xfs_fileoff_t last; /* last file offset */
  548. xfs_mount_t *mp;
  549. int rval;
  550. mp = dp->i_mount;
  551. if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
  552. return rval;
  553. *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
  554. return 0;
  555. }
  556. /*
  557. * Remove the given block from the directory.
  558. * This routine is used for data and free blocks, leaf/node are done
  559. * by xfs_da_shrink_inode.
  560. */
  561. int
  562. xfs_dir2_shrink_inode(
  563. xfs_da_args_t *args,
  564. xfs_dir2_db_t db,
  565. xfs_dabuf_t *bp)
  566. {
  567. xfs_fileoff_t bno; /* directory file offset */
  568. xfs_dablk_t da; /* directory file offset */
  569. int done; /* bunmap is finished */
  570. xfs_inode_t *dp;
  571. int error;
  572. xfs_mount_t *mp;
  573. xfs_trans_t *tp;
  574. xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
  575. dp = args->dp;
  576. mp = dp->i_mount;
  577. tp = args->trans;
  578. da = xfs_dir2_db_to_da(mp, db);
  579. /*
  580. * Unmap the fsblock(s).
  581. */
  582. if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
  583. XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
  584. NULL, &done))) {
  585. /*
  586. * ENOSPC actually can happen if we're in a removename with
  587. * no space reservation, and the resulting block removal
  588. * would cause a bmap btree split or conversion from extents
  589. * to btree. This can only happen for un-fragmented
  590. * directory blocks, since you need to be punching out
  591. * the middle of an extent.
  592. * In this case we need to leave the block in the file,
  593. * and not binval it.
  594. * So the block has to be in a consistent empty state
  595. * and appropriately logged.
  596. * We don't free up the buffer, the caller can tell it
  597. * hasn't happened since it got an error back.
  598. */
  599. return error;
  600. }
  601. ASSERT(done);
  602. /*
  603. * Invalidate the buffer from the transaction.
  604. */
  605. xfs_da_binval(tp, bp);
  606. /*
  607. * If it's not a data block, we're done.
  608. */
  609. if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
  610. return 0;
  611. /*
  612. * If the block isn't the last one in the directory, we're done.
  613. */
  614. if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
  615. return 0;
  616. bno = da;
  617. if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
  618. /*
  619. * This can't really happen unless there's kernel corruption.
  620. */
  621. return error;
  622. }
  623. if (db == mp->m_dirdatablk)
  624. ASSERT(bno == 0);
  625. else
  626. ASSERT(bno > 0);
  627. /*
  628. * Set the size to the new last block.
  629. */
  630. dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
  631. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  632. return 0;
  633. }