xfs_dir2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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_dir_inode_ops = xfs_dir_get_ops(mp, NULL);
  86. mp->m_nondir_inode_ops = xfs_nondir_get_ops(mp, NULL);
  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. nodehdr_size = mp->m_dir_inode_ops->node_hdr_size;
  93. mp->m_attr_node_ents = (mp->m_sb.sb_blocksize - nodehdr_size) /
  94. (uint)sizeof(xfs_da_node_entry_t);
  95. mp->m_dir_node_ents = (mp->m_dirblksize - nodehdr_size) /
  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.filetype = name->type;
  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.filetype = name->type;
  261. args.hashval = dp->i_mount->m_dirnameops->hashname(name);
  262. args.dp = dp;
  263. args.whichfork = XFS_DATA_FORK;
  264. args.trans = tp;
  265. args.op_flags = XFS_DA_OP_OKNOENT;
  266. if (ci_name)
  267. args.op_flags |= XFS_DA_OP_CILOOKUP;
  268. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  269. rval = xfs_dir2_sf_lookup(&args);
  270. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  271. return rval;
  272. else if (v)
  273. rval = xfs_dir2_block_lookup(&args);
  274. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  275. return rval;
  276. else if (v)
  277. rval = xfs_dir2_leaf_lookup(&args);
  278. else
  279. rval = xfs_dir2_node_lookup(&args);
  280. if (rval == EEXIST)
  281. rval = 0;
  282. if (!rval) {
  283. *inum = args.inumber;
  284. if (ci_name) {
  285. ci_name->name = args.value;
  286. ci_name->len = args.valuelen;
  287. }
  288. }
  289. return rval;
  290. }
  291. /*
  292. * Remove an entry from a directory.
  293. */
  294. int
  295. xfs_dir_removename(
  296. xfs_trans_t *tp,
  297. xfs_inode_t *dp,
  298. struct xfs_name *name,
  299. xfs_ino_t ino,
  300. xfs_fsblock_t *first, /* bmap's firstblock */
  301. xfs_bmap_free_t *flist, /* bmap's freeblock list */
  302. xfs_extlen_t total) /* bmap's total block count */
  303. {
  304. xfs_da_args_t args;
  305. int rval;
  306. int v; /* type-checking value */
  307. ASSERT(S_ISDIR(dp->i_d.di_mode));
  308. XFS_STATS_INC(xs_dir_remove);
  309. memset(&args, 0, sizeof(xfs_da_args_t));
  310. args.name = name->name;
  311. args.namelen = name->len;
  312. args.filetype = name->type;
  313. args.hashval = dp->i_mount->m_dirnameops->hashname(name);
  314. args.inumber = ino;
  315. args.dp = dp;
  316. args.firstblock = first;
  317. args.flist = flist;
  318. args.total = total;
  319. args.whichfork = XFS_DATA_FORK;
  320. args.trans = tp;
  321. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  322. rval = xfs_dir2_sf_removename(&args);
  323. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  324. return rval;
  325. else if (v)
  326. rval = xfs_dir2_block_removename(&args);
  327. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  328. return rval;
  329. else if (v)
  330. rval = xfs_dir2_leaf_removename(&args);
  331. else
  332. rval = xfs_dir2_node_removename(&args);
  333. return rval;
  334. }
  335. /*
  336. * Replace the inode number of a directory entry.
  337. */
  338. int
  339. xfs_dir_replace(
  340. xfs_trans_t *tp,
  341. xfs_inode_t *dp,
  342. struct xfs_name *name, /* name of entry to replace */
  343. xfs_ino_t inum, /* new inode number */
  344. xfs_fsblock_t *first, /* bmap's firstblock */
  345. xfs_bmap_free_t *flist, /* bmap's freeblock list */
  346. xfs_extlen_t total) /* bmap's total block count */
  347. {
  348. xfs_da_args_t args;
  349. int rval;
  350. int v; /* type-checking value */
  351. ASSERT(S_ISDIR(dp->i_d.di_mode));
  352. if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum)))
  353. return rval;
  354. memset(&args, 0, sizeof(xfs_da_args_t));
  355. args.name = name->name;
  356. args.namelen = name->len;
  357. args.filetype = name->type;
  358. args.hashval = dp->i_mount->m_dirnameops->hashname(name);
  359. args.inumber = inum;
  360. args.dp = dp;
  361. args.firstblock = first;
  362. args.flist = flist;
  363. args.total = total;
  364. args.whichfork = XFS_DATA_FORK;
  365. args.trans = tp;
  366. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  367. rval = xfs_dir2_sf_replace(&args);
  368. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  369. return rval;
  370. else if (v)
  371. rval = xfs_dir2_block_replace(&args);
  372. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  373. return rval;
  374. else if (v)
  375. rval = xfs_dir2_leaf_replace(&args);
  376. else
  377. rval = xfs_dir2_node_replace(&args);
  378. return rval;
  379. }
  380. /*
  381. * See if this entry can be added to the directory without allocating space.
  382. * First checks that the caller couldn't reserve enough space (resblks = 0).
  383. */
  384. int
  385. xfs_dir_canenter(
  386. xfs_trans_t *tp,
  387. xfs_inode_t *dp,
  388. struct xfs_name *name, /* name of entry to add */
  389. uint resblks)
  390. {
  391. xfs_da_args_t args;
  392. int rval;
  393. int v; /* type-checking value */
  394. if (resblks)
  395. return 0;
  396. ASSERT(S_ISDIR(dp->i_d.di_mode));
  397. memset(&args, 0, sizeof(xfs_da_args_t));
  398. args.name = name->name;
  399. args.namelen = name->len;
  400. args.filetype = name->type;
  401. args.hashval = dp->i_mount->m_dirnameops->hashname(name);
  402. args.dp = dp;
  403. args.whichfork = XFS_DATA_FORK;
  404. args.trans = tp;
  405. args.op_flags = XFS_DA_OP_JUSTCHECK | XFS_DA_OP_ADDNAME |
  406. XFS_DA_OP_OKNOENT;
  407. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  408. rval = xfs_dir2_sf_addname(&args);
  409. else if ((rval = xfs_dir2_isblock(tp, dp, &v)))
  410. return rval;
  411. else if (v)
  412. rval = xfs_dir2_block_addname(&args);
  413. else if ((rval = xfs_dir2_isleaf(tp, dp, &v)))
  414. return rval;
  415. else if (v)
  416. rval = xfs_dir2_leaf_addname(&args);
  417. else
  418. rval = xfs_dir2_node_addname(&args);
  419. return rval;
  420. }
  421. /*
  422. * Utility routines.
  423. */
  424. /*
  425. * Add a block to the directory.
  426. *
  427. * This routine is for data and free blocks, not leaf/node blocks which are
  428. * handled by xfs_da_grow_inode.
  429. */
  430. int
  431. xfs_dir2_grow_inode(
  432. struct xfs_da_args *args,
  433. int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
  434. xfs_dir2_db_t *dbp) /* out: block number added */
  435. {
  436. struct xfs_inode *dp = args->dp;
  437. struct xfs_mount *mp = dp->i_mount;
  438. xfs_fileoff_t bno; /* directory offset of new block */
  439. int count; /* count of filesystem blocks */
  440. int error;
  441. trace_xfs_dir2_grow_inode(args, space);
  442. /*
  443. * Set lowest possible block in the space requested.
  444. */
  445. bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
  446. count = mp->m_dirblkfsbs;
  447. error = xfs_da_grow_inode_int(args, &bno, count);
  448. if (error)
  449. return error;
  450. *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno);
  451. /*
  452. * Update file's size if this is the data space and it grew.
  453. */
  454. if (space == XFS_DIR2_DATA_SPACE) {
  455. xfs_fsize_t size; /* directory file (data) size */
  456. size = XFS_FSB_TO_B(mp, bno + count);
  457. if (size > dp->i_d.di_size) {
  458. dp->i_d.di_size = size;
  459. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  460. }
  461. }
  462. return 0;
  463. }
  464. /*
  465. * See if the directory is a single-block form directory.
  466. */
  467. int
  468. xfs_dir2_isblock(
  469. xfs_trans_t *tp,
  470. xfs_inode_t *dp,
  471. int *vp) /* out: 1 is block, 0 is not block */
  472. {
  473. xfs_fileoff_t last; /* last file offset */
  474. xfs_mount_t *mp;
  475. int rval;
  476. mp = dp->i_mount;
  477. if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
  478. return rval;
  479. rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
  480. ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
  481. *vp = rval;
  482. return 0;
  483. }
  484. /*
  485. * See if the directory is a single-leaf form directory.
  486. */
  487. int
  488. xfs_dir2_isleaf(
  489. xfs_trans_t *tp,
  490. xfs_inode_t *dp,
  491. int *vp) /* out: 1 is leaf, 0 is not leaf */
  492. {
  493. xfs_fileoff_t last; /* last file offset */
  494. xfs_mount_t *mp;
  495. int rval;
  496. mp = dp->i_mount;
  497. if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK)))
  498. return rval;
  499. *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
  500. return 0;
  501. }
  502. /*
  503. * Remove the given block from the directory.
  504. * This routine is used for data and free blocks, leaf/node are done
  505. * by xfs_da_shrink_inode.
  506. */
  507. int
  508. xfs_dir2_shrink_inode(
  509. xfs_da_args_t *args,
  510. xfs_dir2_db_t db,
  511. struct xfs_buf *bp)
  512. {
  513. xfs_fileoff_t bno; /* directory file offset */
  514. xfs_dablk_t da; /* directory file offset */
  515. int done; /* bunmap is finished */
  516. xfs_inode_t *dp;
  517. int error;
  518. xfs_mount_t *mp;
  519. xfs_trans_t *tp;
  520. trace_xfs_dir2_shrink_inode(args, db);
  521. dp = args->dp;
  522. mp = dp->i_mount;
  523. tp = args->trans;
  524. da = xfs_dir2_db_to_da(mp, db);
  525. /*
  526. * Unmap the fsblock(s).
  527. */
  528. if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
  529. XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
  530. &done))) {
  531. /*
  532. * ENOSPC actually can happen if we're in a removename with
  533. * no space reservation, and the resulting block removal
  534. * would cause a bmap btree split or conversion from extents
  535. * to btree. This can only happen for un-fragmented
  536. * directory blocks, since you need to be punching out
  537. * the middle of an extent.
  538. * In this case we need to leave the block in the file,
  539. * and not binval it.
  540. * So the block has to be in a consistent empty state
  541. * and appropriately logged.
  542. * We don't free up the buffer, the caller can tell it
  543. * hasn't happened since it got an error back.
  544. */
  545. return error;
  546. }
  547. ASSERT(done);
  548. /*
  549. * Invalidate the buffer from the transaction.
  550. */
  551. xfs_trans_binval(tp, bp);
  552. /*
  553. * If it's not a data block, we're done.
  554. */
  555. if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
  556. return 0;
  557. /*
  558. * If the block isn't the last one in the directory, we're done.
  559. */
  560. if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0))
  561. return 0;
  562. bno = da;
  563. if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
  564. /*
  565. * This can't really happen unless there's kernel corruption.
  566. */
  567. return error;
  568. }
  569. if (db == mp->m_dirdatablk)
  570. ASSERT(bno == 0);
  571. else
  572. ASSERT(bno > 0);
  573. /*
  574. * Set the size to the new last block.
  575. */
  576. dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
  577. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  578. return 0;
  579. }