xfs_dir2.c 16 KB

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