xfs_dir2.c 17 KB

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