xfs_dir2.c 16 KB

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