xfs_dir2.c 16 KB

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