xfs_dir2.c 17 KB

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