xfs_dir2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_defer.h"
  13. #include "xfs_da_format.h"
  14. #include "xfs_da_btree.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_inode_item.h"
  18. #include "xfs_bmap.h"
  19. #include "xfs_dir2.h"
  20. #include "xfs_dir2_priv.h"
  21. #include "xfs_ialloc.h"
  22. #include "xfs_errortag.h"
  23. #include "xfs_error.h"
  24. #include "xfs_trace.h"
  25. struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR };
  26. /*
  27. * Convert inode mode to directory entry filetype
  28. */
  29. unsigned char
  30. xfs_mode_to_ftype(
  31. int mode)
  32. {
  33. switch (mode & S_IFMT) {
  34. case S_IFREG:
  35. return XFS_DIR3_FT_REG_FILE;
  36. case S_IFDIR:
  37. return XFS_DIR3_FT_DIR;
  38. case S_IFCHR:
  39. return XFS_DIR3_FT_CHRDEV;
  40. case S_IFBLK:
  41. return XFS_DIR3_FT_BLKDEV;
  42. case S_IFIFO:
  43. return XFS_DIR3_FT_FIFO;
  44. case S_IFSOCK:
  45. return XFS_DIR3_FT_SOCK;
  46. case S_IFLNK:
  47. return XFS_DIR3_FT_SYMLINK;
  48. default:
  49. return XFS_DIR3_FT_UNKNOWN;
  50. }
  51. }
  52. /*
  53. * ASCII case-insensitive (ie. A-Z) support for directories that was
  54. * used in IRIX.
  55. */
  56. STATIC xfs_dahash_t
  57. xfs_ascii_ci_hashname(
  58. struct xfs_name *name)
  59. {
  60. xfs_dahash_t hash;
  61. int i;
  62. for (i = 0, hash = 0; i < name->len; i++)
  63. hash = tolower(name->name[i]) ^ rol32(hash, 7);
  64. return hash;
  65. }
  66. STATIC enum xfs_dacmp
  67. xfs_ascii_ci_compname(
  68. struct xfs_da_args *args,
  69. const unsigned char *name,
  70. int len)
  71. {
  72. enum xfs_dacmp result;
  73. int i;
  74. if (args->namelen != len)
  75. return XFS_CMP_DIFFERENT;
  76. result = XFS_CMP_EXACT;
  77. for (i = 0; i < len; i++) {
  78. if (args->name[i] == name[i])
  79. continue;
  80. if (tolower(args->name[i]) != tolower(name[i]))
  81. return XFS_CMP_DIFFERENT;
  82. result = XFS_CMP_CASE;
  83. }
  84. return result;
  85. }
  86. static const struct xfs_nameops xfs_ascii_ci_nameops = {
  87. .hashname = xfs_ascii_ci_hashname,
  88. .compname = xfs_ascii_ci_compname,
  89. };
  90. int
  91. xfs_da_mount(
  92. struct xfs_mount *mp)
  93. {
  94. struct xfs_da_geometry *dageo;
  95. int nodehdr_size;
  96. ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
  97. ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
  98. mp->m_dir_inode_ops = xfs_dir_get_ops(mp, NULL);
  99. mp->m_nondir_inode_ops = xfs_nondir_get_ops(mp, NULL);
  100. nodehdr_size = mp->m_dir_inode_ops->node_hdr_size;
  101. mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
  102. KM_SLEEP | KM_MAYFAIL);
  103. mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
  104. KM_SLEEP | KM_MAYFAIL);
  105. if (!mp->m_dir_geo || !mp->m_attr_geo) {
  106. kmem_free(mp->m_dir_geo);
  107. kmem_free(mp->m_attr_geo);
  108. return -ENOMEM;
  109. }
  110. /* set up directory geometry */
  111. dageo = mp->m_dir_geo;
  112. dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
  113. dageo->fsblog = mp->m_sb.sb_blocklog;
  114. dageo->blksize = xfs_dir2_dirblock_bytes(&mp->m_sb);
  115. dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
  116. /*
  117. * Now we've set up the block conversion variables, we can calculate the
  118. * segment block constants using the geometry structure.
  119. */
  120. dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
  121. dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
  122. dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
  123. dageo->node_ents = (dageo->blksize - nodehdr_size) /
  124. (uint)sizeof(xfs_da_node_entry_t);
  125. dageo->magicpct = (dageo->blksize * 37) / 100;
  126. /* set up attribute geometry - single fsb only */
  127. dageo = mp->m_attr_geo;
  128. dageo->blklog = mp->m_sb.sb_blocklog;
  129. dageo->fsblog = mp->m_sb.sb_blocklog;
  130. dageo->blksize = 1 << dageo->blklog;
  131. dageo->fsbcount = 1;
  132. dageo->node_ents = (dageo->blksize - nodehdr_size) /
  133. (uint)sizeof(xfs_da_node_entry_t);
  134. dageo->magicpct = (dageo->blksize * 37) / 100;
  135. if (xfs_sb_version_hasasciici(&mp->m_sb))
  136. mp->m_dirnameops = &xfs_ascii_ci_nameops;
  137. else
  138. mp->m_dirnameops = &xfs_default_nameops;
  139. return 0;
  140. }
  141. void
  142. xfs_da_unmount(
  143. struct xfs_mount *mp)
  144. {
  145. kmem_free(mp->m_dir_geo);
  146. kmem_free(mp->m_attr_geo);
  147. }
  148. /*
  149. * Return 1 if directory contains only "." and "..".
  150. */
  151. int
  152. xfs_dir_isempty(
  153. xfs_inode_t *dp)
  154. {
  155. xfs_dir2_sf_hdr_t *sfp;
  156. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  157. if (dp->i_d.di_size == 0) /* might happen during shutdown. */
  158. return 1;
  159. if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
  160. return 0;
  161. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  162. return !sfp->count;
  163. }
  164. /*
  165. * Validate a given inode number.
  166. */
  167. int
  168. xfs_dir_ino_validate(
  169. xfs_mount_t *mp,
  170. xfs_ino_t ino)
  171. {
  172. bool ino_ok = xfs_verify_dir_ino(mp, ino);
  173. if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE))) {
  174. xfs_warn(mp, "Invalid inode number 0x%Lx",
  175. (unsigned long long) ino);
  176. XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
  177. return -EFSCORRUPTED;
  178. }
  179. return 0;
  180. }
  181. /*
  182. * Initialize a directory with its "." and ".." entries.
  183. */
  184. int
  185. xfs_dir_init(
  186. xfs_trans_t *tp,
  187. xfs_inode_t *dp,
  188. xfs_inode_t *pdp)
  189. {
  190. struct xfs_da_args *args;
  191. int error;
  192. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  193. error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
  194. if (error)
  195. return error;
  196. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  197. if (!args)
  198. return -ENOMEM;
  199. args->geo = dp->i_mount->m_dir_geo;
  200. args->dp = dp;
  201. args->trans = tp;
  202. error = xfs_dir2_sf_create(args, pdp->i_ino);
  203. kmem_free(args);
  204. return error;
  205. }
  206. /*
  207. * Enter a name in a directory, or check for available space.
  208. * If inum is 0, only the available space test is performed.
  209. */
  210. int
  211. xfs_dir_createname(
  212. struct xfs_trans *tp,
  213. struct xfs_inode *dp,
  214. struct xfs_name *name,
  215. xfs_ino_t inum, /* new entry inode number */
  216. xfs_extlen_t total) /* bmap's total block count */
  217. {
  218. struct xfs_da_args *args;
  219. int rval;
  220. int v; /* type-checking value */
  221. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  222. if (inum) {
  223. rval = xfs_dir_ino_validate(tp->t_mountp, inum);
  224. if (rval)
  225. return rval;
  226. XFS_STATS_INC(dp->i_mount, xs_dir_create);
  227. }
  228. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  229. if (!args)
  230. return -ENOMEM;
  231. args->geo = dp->i_mount->m_dir_geo;
  232. args->name = name->name;
  233. args->namelen = name->len;
  234. args->filetype = name->type;
  235. args->hashval = dp->i_mount->m_dirnameops->hashname(name);
  236. args->inumber = inum;
  237. args->dp = dp;
  238. args->total = total;
  239. args->whichfork = XFS_DATA_FORK;
  240. args->trans = tp;
  241. args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  242. if (!inum)
  243. args->op_flags |= XFS_DA_OP_JUSTCHECK;
  244. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  245. rval = xfs_dir2_sf_addname(args);
  246. goto out_free;
  247. }
  248. rval = xfs_dir2_isblock(args, &v);
  249. if (rval)
  250. goto out_free;
  251. if (v) {
  252. rval = xfs_dir2_block_addname(args);
  253. goto out_free;
  254. }
  255. rval = xfs_dir2_isleaf(args, &v);
  256. if (rval)
  257. goto out_free;
  258. if (v)
  259. rval = xfs_dir2_leaf_addname(args);
  260. else
  261. rval = xfs_dir2_node_addname(args);
  262. out_free:
  263. kmem_free(args);
  264. return rval;
  265. }
  266. /*
  267. * If doing a CI lookup and case-insensitive match, dup actual name into
  268. * args.value. Return EEXIST for success (ie. name found) or an error.
  269. */
  270. int
  271. xfs_dir_cilookup_result(
  272. struct xfs_da_args *args,
  273. const unsigned char *name,
  274. int len)
  275. {
  276. if (args->cmpresult == XFS_CMP_DIFFERENT)
  277. return -ENOENT;
  278. if (args->cmpresult != XFS_CMP_CASE ||
  279. !(args->op_flags & XFS_DA_OP_CILOOKUP))
  280. return -EEXIST;
  281. args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
  282. if (!args->value)
  283. return -ENOMEM;
  284. memcpy(args->value, name, len);
  285. args->valuelen = len;
  286. return -EEXIST;
  287. }
  288. /*
  289. * Lookup a name in a directory, give back the inode number.
  290. * If ci_name is not NULL, returns the actual name in ci_name if it differs
  291. * to name, or ci_name->name is set to NULL for an exact match.
  292. */
  293. int
  294. xfs_dir_lookup(
  295. xfs_trans_t *tp,
  296. xfs_inode_t *dp,
  297. struct xfs_name *name,
  298. xfs_ino_t *inum, /* out: inode number */
  299. struct xfs_name *ci_name) /* out: actual name if CI match */
  300. {
  301. struct xfs_da_args *args;
  302. int rval;
  303. int v; /* type-checking value */
  304. int lock_mode;
  305. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  306. XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
  307. /*
  308. * We need to use KM_NOFS here so that lockdep will not throw false
  309. * positive deadlock warnings on a non-transactional lookup path. It is
  310. * safe to recurse into inode recalim in that case, but lockdep can't
  311. * easily be taught about it. Hence KM_NOFS avoids having to add more
  312. * lockdep Doing this avoids having to add a bunch of lockdep class
  313. * annotations into the reclaim path for the ilock.
  314. */
  315. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  316. args->geo = dp->i_mount->m_dir_geo;
  317. args->name = name->name;
  318. args->namelen = name->len;
  319. args->filetype = name->type;
  320. args->hashval = dp->i_mount->m_dirnameops->hashname(name);
  321. args->dp = dp;
  322. args->whichfork = XFS_DATA_FORK;
  323. args->trans = tp;
  324. args->op_flags = XFS_DA_OP_OKNOENT;
  325. if (ci_name)
  326. args->op_flags |= XFS_DA_OP_CILOOKUP;
  327. lock_mode = xfs_ilock_data_map_shared(dp);
  328. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  329. rval = xfs_dir2_sf_lookup(args);
  330. goto out_check_rval;
  331. }
  332. rval = xfs_dir2_isblock(args, &v);
  333. if (rval)
  334. goto out_free;
  335. if (v) {
  336. rval = xfs_dir2_block_lookup(args);
  337. goto out_check_rval;
  338. }
  339. rval = xfs_dir2_isleaf(args, &v);
  340. if (rval)
  341. goto out_free;
  342. if (v)
  343. rval = xfs_dir2_leaf_lookup(args);
  344. else
  345. rval = xfs_dir2_node_lookup(args);
  346. out_check_rval:
  347. if (rval == -EEXIST)
  348. rval = 0;
  349. if (!rval) {
  350. *inum = args->inumber;
  351. if (ci_name) {
  352. ci_name->name = args->value;
  353. ci_name->len = args->valuelen;
  354. }
  355. }
  356. out_free:
  357. xfs_iunlock(dp, lock_mode);
  358. kmem_free(args);
  359. return rval;
  360. }
  361. /*
  362. * Remove an entry from a directory.
  363. */
  364. int
  365. xfs_dir_removename(
  366. struct xfs_trans *tp,
  367. struct xfs_inode *dp,
  368. struct xfs_name *name,
  369. xfs_ino_t ino,
  370. xfs_extlen_t total) /* bmap's total block count */
  371. {
  372. struct xfs_da_args *args;
  373. int rval;
  374. int v; /* type-checking value */
  375. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  376. XFS_STATS_INC(dp->i_mount, xs_dir_remove);
  377. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  378. if (!args)
  379. return -ENOMEM;
  380. args->geo = dp->i_mount->m_dir_geo;
  381. args->name = name->name;
  382. args->namelen = name->len;
  383. args->filetype = name->type;
  384. args->hashval = dp->i_mount->m_dirnameops->hashname(name);
  385. args->inumber = ino;
  386. args->dp = dp;
  387. args->total = total;
  388. args->whichfork = XFS_DATA_FORK;
  389. args->trans = tp;
  390. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  391. rval = xfs_dir2_sf_removename(args);
  392. goto out_free;
  393. }
  394. rval = xfs_dir2_isblock(args, &v);
  395. if (rval)
  396. goto out_free;
  397. if (v) {
  398. rval = xfs_dir2_block_removename(args);
  399. goto out_free;
  400. }
  401. rval = xfs_dir2_isleaf(args, &v);
  402. if (rval)
  403. goto out_free;
  404. if (v)
  405. rval = xfs_dir2_leaf_removename(args);
  406. else
  407. rval = xfs_dir2_node_removename(args);
  408. out_free:
  409. kmem_free(args);
  410. return rval;
  411. }
  412. /*
  413. * Replace the inode number of a directory entry.
  414. */
  415. int
  416. xfs_dir_replace(
  417. struct xfs_trans *tp,
  418. struct xfs_inode *dp,
  419. struct xfs_name *name, /* name of entry to replace */
  420. xfs_ino_t inum, /* new inode number */
  421. xfs_extlen_t total) /* bmap's total block count */
  422. {
  423. struct xfs_da_args *args;
  424. int rval;
  425. int v; /* type-checking value */
  426. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  427. rval = xfs_dir_ino_validate(tp->t_mountp, inum);
  428. if (rval)
  429. return rval;
  430. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  431. if (!args)
  432. return -ENOMEM;
  433. args->geo = dp->i_mount->m_dir_geo;
  434. args->name = name->name;
  435. args->namelen = name->len;
  436. args->filetype = name->type;
  437. args->hashval = dp->i_mount->m_dirnameops->hashname(name);
  438. args->inumber = inum;
  439. args->dp = dp;
  440. args->total = total;
  441. args->whichfork = XFS_DATA_FORK;
  442. args->trans = tp;
  443. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  444. rval = xfs_dir2_sf_replace(args);
  445. goto out_free;
  446. }
  447. rval = xfs_dir2_isblock(args, &v);
  448. if (rval)
  449. goto out_free;
  450. if (v) {
  451. rval = xfs_dir2_block_replace(args);
  452. goto out_free;
  453. }
  454. rval = xfs_dir2_isleaf(args, &v);
  455. if (rval)
  456. goto out_free;
  457. if (v)
  458. rval = xfs_dir2_leaf_replace(args);
  459. else
  460. rval = xfs_dir2_node_replace(args);
  461. out_free:
  462. kmem_free(args);
  463. return rval;
  464. }
  465. /*
  466. * See if this entry can be added to the directory without allocating space.
  467. */
  468. int
  469. xfs_dir_canenter(
  470. xfs_trans_t *tp,
  471. xfs_inode_t *dp,
  472. struct xfs_name *name) /* name of entry to add */
  473. {
  474. return xfs_dir_createname(tp, dp, name, 0, 0);
  475. }
  476. /*
  477. * Utility routines.
  478. */
  479. /*
  480. * Add a block to the directory.
  481. *
  482. * This routine is for data and free blocks, not leaf/node blocks which are
  483. * handled by xfs_da_grow_inode.
  484. */
  485. int
  486. xfs_dir2_grow_inode(
  487. struct xfs_da_args *args,
  488. int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
  489. xfs_dir2_db_t *dbp) /* out: block number added */
  490. {
  491. struct xfs_inode *dp = args->dp;
  492. struct xfs_mount *mp = dp->i_mount;
  493. xfs_fileoff_t bno; /* directory offset of new block */
  494. int count; /* count of filesystem blocks */
  495. int error;
  496. trace_xfs_dir2_grow_inode(args, space);
  497. /*
  498. * Set lowest possible block in the space requested.
  499. */
  500. bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
  501. count = args->geo->fsbcount;
  502. error = xfs_da_grow_inode_int(args, &bno, count);
  503. if (error)
  504. return error;
  505. *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
  506. /*
  507. * Update file's size if this is the data space and it grew.
  508. */
  509. if (space == XFS_DIR2_DATA_SPACE) {
  510. xfs_fsize_t size; /* directory file (data) size */
  511. size = XFS_FSB_TO_B(mp, bno + count);
  512. if (size > dp->i_d.di_size) {
  513. dp->i_d.di_size = size;
  514. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  515. }
  516. }
  517. return 0;
  518. }
  519. /*
  520. * See if the directory is a single-block form directory.
  521. */
  522. int
  523. xfs_dir2_isblock(
  524. struct xfs_da_args *args,
  525. int *vp) /* out: 1 is block, 0 is not block */
  526. {
  527. xfs_fileoff_t last; /* last file offset */
  528. int rval;
  529. if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
  530. return rval;
  531. rval = XFS_FSB_TO_B(args->dp->i_mount, last) == args->geo->blksize;
  532. if (rval != 0 && args->dp->i_d.di_size != args->geo->blksize)
  533. return -EFSCORRUPTED;
  534. *vp = rval;
  535. return 0;
  536. }
  537. /*
  538. * See if the directory is a single-leaf form directory.
  539. */
  540. int
  541. xfs_dir2_isleaf(
  542. struct xfs_da_args *args,
  543. int *vp) /* out: 1 is block, 0 is not block */
  544. {
  545. xfs_fileoff_t last; /* last file offset */
  546. int rval;
  547. if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
  548. return rval;
  549. *vp = last == args->geo->leafblk + args->geo->fsbcount;
  550. return 0;
  551. }
  552. /*
  553. * Remove the given block from the directory.
  554. * This routine is used for data and free blocks, leaf/node are done
  555. * by xfs_da_shrink_inode.
  556. */
  557. int
  558. xfs_dir2_shrink_inode(
  559. struct xfs_da_args *args,
  560. xfs_dir2_db_t db,
  561. struct xfs_buf *bp)
  562. {
  563. xfs_fileoff_t bno; /* directory file offset */
  564. xfs_dablk_t da; /* directory file offset */
  565. int done; /* bunmap is finished */
  566. struct xfs_inode *dp;
  567. int error;
  568. struct xfs_mount *mp;
  569. struct xfs_trans *tp;
  570. trace_xfs_dir2_shrink_inode(args, db);
  571. dp = args->dp;
  572. mp = dp->i_mount;
  573. tp = args->trans;
  574. da = xfs_dir2_db_to_da(args->geo, db);
  575. /* Unmap the fsblock(s). */
  576. error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0, &done);
  577. if (error) {
  578. /*
  579. * ENOSPC actually can happen if we're in a removename with no
  580. * space reservation, and the resulting block removal would
  581. * cause a bmap btree split or conversion from extents to btree.
  582. * This can only happen for un-fragmented directory blocks,
  583. * since you need to be punching out the middle of an extent.
  584. * In this case we need to leave the block in the file, and not
  585. * binval it. So the block has to be in a consistent empty
  586. * state and appropriately logged. We don't free up the buffer,
  587. * the caller can tell it hasn't happened since it got an error
  588. * back.
  589. */
  590. return error;
  591. }
  592. ASSERT(done);
  593. /*
  594. * Invalidate the buffer from the transaction.
  595. */
  596. xfs_trans_binval(tp, bp);
  597. /*
  598. * If it's not a data block, we're done.
  599. */
  600. if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
  601. return 0;
  602. /*
  603. * If the block isn't the last one in the directory, we're done.
  604. */
  605. if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
  606. return 0;
  607. bno = da;
  608. if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
  609. /*
  610. * This can't really happen unless there's kernel corruption.
  611. */
  612. return error;
  613. }
  614. if (db == args->geo->datablk)
  615. ASSERT(bno == 0);
  616. else
  617. ASSERT(bno > 0);
  618. /*
  619. * Set the size to the new last block.
  620. */
  621. dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
  622. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  623. return 0;
  624. }