xfs_dir2.c 17 KB

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