xfs_dir2.c 17 KB

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