xfs_dir2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. xfs_inode_t *dp,
  275. void *dirent,
  276. size_t bufsize,
  277. xfs_off_t *offset,
  278. filldir_t filldir)
  279. {
  280. int rval; /* return value */
  281. int v; /* type-checking value */
  282. vn_trace_entry(dp, __FUNCTION__, (inst_t *)__return_address);
  283. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  284. return XFS_ERROR(EIO);
  285. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  286. XFS_STATS_INC(xs_dir_getdents);
  287. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  288. rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir);
  289. else if ((rval = xfs_dir2_isblock(NULL, dp, &v)))
  290. ;
  291. else if (v)
  292. rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir);
  293. else
  294. rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset,
  295. filldir);
  296. return rval;
  297. }
  298. /*
  299. * Replace the inode number of a directory entry.
  300. */
  301. int
  302. xfs_dir_replace(
  303. xfs_trans_t *tp,
  304. xfs_inode_t *dp,
  305. char *name, /* name of entry to replace */
  306. int namelen,
  307. xfs_ino_t inum, /* new inode number */
  308. xfs_fsblock_t *first, /* bmap's firstblock */
  309. xfs_bmap_free_t *flist, /* bmap's freeblock list */
  310. xfs_extlen_t total) /* bmap's total block count */
  311. {
  312. xfs_da_args_t args;
  313. int rval;
  314. int v; /* type-checking value */
  315. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  316. if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
  317. return rval;
  318. args.name = name;
  319. args.namelen = namelen;
  320. args.hashval = xfs_da_hashname(name, namelen);
  321. args.inumber = inum;
  322. args.dp = dp;
  323. args.firstblock = first;
  324. args.flist = flist;
  325. args.total = total;
  326. args.whichfork = XFS_DATA_FORK;
  327. args.trans = tp;
  328. args.justcheck = args.addname = args.oknoent = 0;
  329. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  330. rval = xfs_dir2_sf_replace(&args);
  331. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  332. return rval;
  333. else if (v)
  334. rval = xfs_dir2_block_replace(&args);
  335. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  336. return rval;
  337. else if (v)
  338. rval = xfs_dir2_leaf_replace(&args);
  339. else
  340. rval = xfs_dir2_node_replace(&args);
  341. return rval;
  342. }
  343. /*
  344. * See if this entry can be added to the directory without allocating space.
  345. */
  346. int
  347. xfs_dir_canenter(
  348. xfs_trans_t *tp,
  349. xfs_inode_t *dp,
  350. char *name, /* name of entry to add */
  351. int namelen)
  352. {
  353. xfs_da_args_t args;
  354. int rval;
  355. int v; /* type-checking value */
  356. ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR);
  357. args.name = name;
  358. args.namelen = namelen;
  359. args.hashval = xfs_da_hashname(name, namelen);
  360. args.inumber = 0;
  361. args.dp = dp;
  362. args.firstblock = NULL;
  363. args.flist = NULL;
  364. args.total = 0;
  365. args.whichfork = XFS_DATA_FORK;
  366. args.trans = tp;
  367. args.justcheck = args.addname = args.oknoent = 1;
  368. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  369. rval = xfs_dir2_sf_addname(&args);
  370. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  371. return rval;
  372. else if (v)
  373. rval = xfs_dir2_block_addname(&args);
  374. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  375. return rval;
  376. else if (v)
  377. rval = xfs_dir2_leaf_addname(&args);
  378. else
  379. rval = xfs_dir2_node_addname(&args);
  380. return rval;
  381. }
  382. /*
  383. * Utility routines.
  384. */
  385. /*
  386. * Add a block to the directory.
  387. * This routine is for data and free blocks, not leaf/node blocks
  388. * which are handled by xfs_da_grow_inode.
  389. */
  390. int
  391. xfs_dir2_grow_inode(
  392. xfs_da_args_t *args,
  393. int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
  394. xfs_dir2_db_t *dbp) /* out: block number added */
  395. {
  396. xfs_fileoff_t bno; /* directory offset of new block */
  397. int count; /* count of filesystem blocks */
  398. xfs_inode_t *dp; /* incore directory inode */
  399. int error;
  400. int got; /* blocks actually mapped */
  401. int i;
  402. xfs_bmbt_irec_t map; /* single structure for bmap */
  403. int mapi; /* mapping index */
  404. xfs_bmbt_irec_t *mapp; /* bmap mapping structure(s) */
  405. xfs_mount_t *mp;
  406. int nmap; /* number of bmap entries */
  407. xfs_trans_t *tp;
  408. xfs_dir2_trace_args_s("grow_inode", args, space);
  409. dp = args->dp;
  410. tp = args->trans;
  411. mp = dp->i_mount;
  412. /*
  413. * Set lowest possible block in the space requested.
  414. */
  415. bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
  416. count = mp->m_dirblkfsbs;
  417. /*
  418. * Find the first hole for our block.
  419. */
  420. if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK)))
  421. return error;
  422. nmap = 1;
  423. ASSERT(args->firstblock != NULL);
  424. /*
  425. * Try mapping the new block contiguously (one extent).
  426. */
  427. if ((error = xfs_bmapi(tp, dp, bno, count,
  428. XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
  429. args->firstblock, args->total, &map, &nmap,
  430. args->flist, NULL)))
  431. return error;
  432. ASSERT(nmap <= 1);
  433. if (nmap == 1) {
  434. mapp = &map;
  435. mapi = 1;
  436. }
  437. /*
  438. * Didn't work and this is a multiple-fsb directory block.
  439. * Try again with contiguous flag turned on.
  440. */
  441. else if (nmap == 0 && count > 1) {
  442. xfs_fileoff_t b; /* current file offset */
  443. /*
  444. * Space for maximum number of mappings.
  445. */
  446. mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
  447. /*
  448. * Iterate until we get to the end of our block.
  449. */
  450. for (b = bno, mapi = 0; b < bno + count; ) {
  451. int c; /* current fsb count */
  452. /*
  453. * Can't map more than MAX_NMAP at once.
  454. */
  455. nmap = MIN(XFS_BMAP_MAX_NMAP, count);
  456. c = (int)(bno + count - b);
  457. if ((error = xfs_bmapi(tp, dp, b, c,
  458. XFS_BMAPI_WRITE|XFS_BMAPI_METADATA,
  459. args->firstblock, args->total,
  460. &mapp[mapi], &nmap, args->flist,
  461. NULL))) {
  462. kmem_free(mapp, sizeof(*mapp) * count);
  463. return error;
  464. }
  465. if (nmap < 1)
  466. break;
  467. /*
  468. * Add this bunch into our table, go to the next offset.
  469. */
  470. mapi += nmap;
  471. b = mapp[mapi - 1].br_startoff +
  472. mapp[mapi - 1].br_blockcount;
  473. }
  474. }
  475. /*
  476. * Didn't work.
  477. */
  478. else {
  479. mapi = 0;
  480. mapp = NULL;
  481. }
  482. /*
  483. * See how many fsb's we got.
  484. */
  485. for (i = 0, got = 0; i < mapi; i++)
  486. got += mapp[i].br_blockcount;
  487. /*
  488. * Didn't get enough fsb's, or the first/last block's are wrong.
  489. */
  490. if (got != count || mapp[0].br_startoff != bno ||
  491. mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
  492. bno + count) {
  493. if (mapp != &map)
  494. kmem_free(mapp, sizeof(*mapp) * count);
  495. return XFS_ERROR(ENOSPC);
  496. }
  497. /*
  498. * Done with the temporary mapping table.
  499. */
  500. if (mapp != &map)
  501. kmem_free(mapp, sizeof(*mapp) * count);
  502. *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
  503. /*
  504. * Update file's size if this is the data space and it grew.
  505. */
  506. if (space == XFS_DIR2_DATA_SPACE) {
  507. xfs_fsize_t size; /* directory file (data) size */
  508. size = XFS_FSB_TO_B(mp, bno + count);
  509. if (size > dp->i_d.di_size) {
  510. dp->i_d.di_size = size;
  511. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  512. }
  513. }
  514. return 0;
  515. }
  516. /*
  517. * See if the directory is a single-block form directory.
  518. */
  519. int
  520. xfs_dir2_isblock(
  521. xfs_trans_t *tp,
  522. xfs_inode_t *dp,
  523. int *vp) /* out: 1 is block, 0 is not block */
  524. {
  525. xfs_fileoff_t last; /* last file offset */
  526. xfs_mount_t *mp;
  527. int rval;
  528. mp = dp->i_mount;
  529. if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
  530. return rval;
  531. rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
  532. ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
  533. *vp = rval;
  534. return 0;
  535. }
  536. /*
  537. * See if the directory is a single-leaf form directory.
  538. */
  539. int
  540. xfs_dir2_isleaf(
  541. xfs_trans_t *tp,
  542. xfs_inode_t *dp,
  543. int *vp) /* out: 1 is leaf, 0 is not leaf */
  544. {
  545. xfs_fileoff_t last; /* last file offset */
  546. xfs_mount_t *mp;
  547. int rval;
  548. mp = dp->i_mount;
  549. if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
  550. return rval;
  551. *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
  552. return 0;
  553. }
  554. /*
  555. * Remove the given block from the directory.
  556. * This routine is used for data and free blocks, leaf/node are done
  557. * by xfs_da_shrink_inode.
  558. */
  559. int
  560. xfs_dir2_shrink_inode(
  561. xfs_da_args_t *args,
  562. xfs_dir2_db_t db,
  563. xfs_dabuf_t *bp)
  564. {
  565. xfs_fileoff_t bno; /* directory file offset */
  566. xfs_dablk_t da; /* directory file offset */
  567. int done; /* bunmap is finished */
  568. xfs_inode_t *dp;
  569. int error;
  570. xfs_mount_t *mp;
  571. xfs_trans_t *tp;
  572. xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
  573. dp = args->dp;
  574. mp = dp->i_mount;
  575. tp = args->trans;
  576. da = xfs_dir2_db_to_da(mp, db);
  577. /*
  578. * Unmap the fsblock(s).
  579. */
  580. if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
  581. XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
  582. NULL, &done))) {
  583. /*
  584. * ENOSPC actually can happen if we're in a removename with
  585. * no space reservation, and the resulting block removal
  586. * would cause a bmap btree split or conversion from extents
  587. * to btree. This can only happen for un-fragmented
  588. * directory blocks, since you need to be punching out
  589. * the middle of an extent.
  590. * In this case we need to leave the block in the file,
  591. * and not binval it.
  592. * So the block has to be in a consistent empty state
  593. * and appropriately logged.
  594. * We don't free up the buffer, the caller can tell it
  595. * hasn't happened since it got an error back.
  596. */
  597. return error;
  598. }
  599. ASSERT(done);
  600. /*
  601. * Invalidate the buffer from the transaction.
  602. */
  603. xfs_da_binval(tp, bp);
  604. /*
  605. * If it's not a data block, we're done.
  606. */
  607. if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
  608. return 0;
  609. /*
  610. * If the block isn't the last one in the directory, we're done.
  611. */
  612. if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
  613. return 0;
  614. bno = da;
  615. if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
  616. /*
  617. * This can't really happen unless there's kernel corruption.
  618. */
  619. return error;
  620. }
  621. if (db == mp->m_dirdatablk)
  622. ASSERT(bno == 0);
  623. else
  624. ASSERT(bno > 0);
  625. /*
  626. * Set the size to the new last block.
  627. */
  628. dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
  629. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  630. return 0;
  631. }