xfs_dir2.c 17 KB

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