xfs_dir2.c 15 KB

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