xfs_dir2.c 17 KB

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