xfs_dir2.c 16 KB

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