xfs_dir2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_defer.h"
  13. #include "xfs_da_format.h"
  14. #include "xfs_da_btree.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_inode_item.h"
  18. #include "xfs_bmap.h"
  19. #include "xfs_dir2.h"
  20. #include "xfs_dir2_priv.h"
  21. #include "xfs_ialloc.h"
  22. #include "xfs_errortag.h"
  23. #include "xfs_error.h"
  24. #include "xfs_trace.h"
  25. struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR };
  26. /*
  27. * Convert inode mode to directory entry filetype
  28. */
  29. unsigned char
  30. xfs_mode_to_ftype(
  31. int mode)
  32. {
  33. switch (mode & S_IFMT) {
  34. case S_IFREG:
  35. return XFS_DIR3_FT_REG_FILE;
  36. case S_IFDIR:
  37. return XFS_DIR3_FT_DIR;
  38. case S_IFCHR:
  39. return XFS_DIR3_FT_CHRDEV;
  40. case S_IFBLK:
  41. return XFS_DIR3_FT_BLKDEV;
  42. case S_IFIFO:
  43. return XFS_DIR3_FT_FIFO;
  44. case S_IFSOCK:
  45. return XFS_DIR3_FT_SOCK;
  46. case S_IFLNK:
  47. return XFS_DIR3_FT_SYMLINK;
  48. default:
  49. return XFS_DIR3_FT_UNKNOWN;
  50. }
  51. }
  52. /*
  53. * ASCII case-insensitive (ie. A-Z) support for directories that was
  54. * used in IRIX.
  55. */
  56. STATIC xfs_dahash_t
  57. xfs_ascii_ci_hashname(
  58. struct xfs_name *name)
  59. {
  60. xfs_dahash_t hash;
  61. int i;
  62. for (i = 0, hash = 0; i < name->len; i++)
  63. hash = tolower(name->name[i]) ^ rol32(hash, 7);
  64. return hash;
  65. }
  66. STATIC enum xfs_dacmp
  67. xfs_ascii_ci_compname(
  68. struct xfs_da_args *args,
  69. const unsigned char *name,
  70. int len)
  71. {
  72. enum xfs_dacmp result;
  73. int i;
  74. if (args->namelen != len)
  75. return XFS_CMP_DIFFERENT;
  76. result = XFS_CMP_EXACT;
  77. for (i = 0; i < len; i++) {
  78. if (args->name[i] == name[i])
  79. continue;
  80. if (tolower(args->name[i]) != tolower(name[i]))
  81. return XFS_CMP_DIFFERENT;
  82. result = XFS_CMP_CASE;
  83. }
  84. return result;
  85. }
  86. static const struct xfs_nameops xfs_ascii_ci_nameops = {
  87. .hashname = xfs_ascii_ci_hashname,
  88. .compname = xfs_ascii_ci_compname,
  89. };
  90. int
  91. xfs_da_mount(
  92. struct xfs_mount *mp)
  93. {
  94. struct xfs_da_geometry *dageo;
  95. int nodehdr_size;
  96. ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
  97. ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
  98. mp->m_dir_inode_ops = xfs_dir_get_ops(mp, NULL);
  99. mp->m_nondir_inode_ops = xfs_nondir_get_ops(mp, NULL);
  100. nodehdr_size = mp->m_dir_inode_ops->node_hdr_size;
  101. mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
  102. KM_SLEEP | KM_MAYFAIL);
  103. mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
  104. KM_SLEEP | KM_MAYFAIL);
  105. if (!mp->m_dir_geo || !mp->m_attr_geo) {
  106. kmem_free(mp->m_dir_geo);
  107. kmem_free(mp->m_attr_geo);
  108. return -ENOMEM;
  109. }
  110. /* set up directory geometry */
  111. dageo = mp->m_dir_geo;
  112. dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
  113. dageo->fsblog = mp->m_sb.sb_blocklog;
  114. dageo->blksize = xfs_dir2_dirblock_bytes(&mp->m_sb);
  115. dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
  116. /*
  117. * Now we've set up the block conversion variables, we can calculate the
  118. * segment block constants using the geometry structure.
  119. */
  120. dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
  121. dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
  122. dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
  123. dageo->node_ents = (dageo->blksize - nodehdr_size) /
  124. (uint)sizeof(xfs_da_node_entry_t);
  125. dageo->magicpct = (dageo->blksize * 37) / 100;
  126. /* set up attribute geometry - single fsb only */
  127. dageo = mp->m_attr_geo;
  128. dageo->blklog = mp->m_sb.sb_blocklog;
  129. dageo->fsblog = mp->m_sb.sb_blocklog;
  130. dageo->blksize = 1 << dageo->blklog;
  131. dageo->fsbcount = 1;
  132. dageo->node_ents = (dageo->blksize - nodehdr_size) /
  133. (uint)sizeof(xfs_da_node_entry_t);
  134. dageo->magicpct = (dageo->blksize * 37) / 100;
  135. if (xfs_sb_version_hasasciici(&mp->m_sb))
  136. mp->m_dirnameops = &xfs_ascii_ci_nameops;
  137. else
  138. mp->m_dirnameops = &xfs_default_nameops;
  139. return 0;
  140. }
  141. void
  142. xfs_da_unmount(
  143. struct xfs_mount *mp)
  144. {
  145. kmem_free(mp->m_dir_geo);
  146. kmem_free(mp->m_attr_geo);
  147. }
  148. /*
  149. * Return 1 if directory contains only "." and "..".
  150. */
  151. int
  152. xfs_dir_isempty(
  153. xfs_inode_t *dp)
  154. {
  155. xfs_dir2_sf_hdr_t *sfp;
  156. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  157. if (dp->i_d.di_size == 0) /* might happen during shutdown. */
  158. return 1;
  159. if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
  160. return 0;
  161. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  162. return !sfp->count;
  163. }
  164. /*
  165. * Validate a given inode number.
  166. */
  167. int
  168. xfs_dir_ino_validate(
  169. xfs_mount_t *mp,
  170. xfs_ino_t ino)
  171. {
  172. bool ino_ok = xfs_verify_dir_ino(mp, ino);
  173. if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_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(VFS_I(dp)->i_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. struct xfs_defer_ops *dfops, /* 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(VFS_I(dp)->i_mode));
  224. if (inum) {
  225. rval = xfs_dir_ino_validate(tp->t_mountp, inum);
  226. if (rval)
  227. return rval;
  228. XFS_STATS_INC(dp->i_mount, 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->dfops = dfops;
  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. int lock_mode;
  309. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  310. XFS_STATS_INC(dp->i_mount, 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. lock_mode = xfs_ilock_data_map_shared(dp);
  332. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  333. rval = xfs_dir2_sf_lookup(args);
  334. goto out_check_rval;
  335. }
  336. rval = xfs_dir2_isblock(args, &v);
  337. if (rval)
  338. goto out_free;
  339. if (v) {
  340. rval = xfs_dir2_block_lookup(args);
  341. goto out_check_rval;
  342. }
  343. rval = xfs_dir2_isleaf(args, &v);
  344. if (rval)
  345. goto out_free;
  346. if (v)
  347. rval = xfs_dir2_leaf_lookup(args);
  348. else
  349. rval = xfs_dir2_node_lookup(args);
  350. out_check_rval:
  351. if (rval == -EEXIST)
  352. rval = 0;
  353. if (!rval) {
  354. *inum = args->inumber;
  355. if (ci_name) {
  356. ci_name->name = args->value;
  357. ci_name->len = args->valuelen;
  358. }
  359. }
  360. out_free:
  361. xfs_iunlock(dp, lock_mode);
  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. struct xfs_defer_ops *dfops, /* 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(VFS_I(dp)->i_mode));
  382. XFS_STATS_INC(dp->i_mount, 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->dfops = dfops;
  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. struct xfs_defer_ops *dfops, /* 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(VFS_I(dp)->i_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->dfops = dfops;
  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. if (rval != 0 && args->dp->i_d.di_size != args->geo->blksize)
  545. return -EFSCORRUPTED;
  546. *vp = rval;
  547. return 0;
  548. }
  549. /*
  550. * See if the directory is a single-leaf form directory.
  551. */
  552. int
  553. xfs_dir2_isleaf(
  554. struct xfs_da_args *args,
  555. int *vp) /* out: 1 is block, 0 is not block */
  556. {
  557. xfs_fileoff_t last; /* last file offset */
  558. int rval;
  559. if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
  560. return rval;
  561. *vp = last == args->geo->leafblk + args->geo->fsbcount;
  562. return 0;
  563. }
  564. /*
  565. * Remove the given block from the directory.
  566. * This routine is used for data and free blocks, leaf/node are done
  567. * by xfs_da_shrink_inode.
  568. */
  569. int
  570. xfs_dir2_shrink_inode(
  571. xfs_da_args_t *args,
  572. xfs_dir2_db_t db,
  573. struct xfs_buf *bp)
  574. {
  575. xfs_fileoff_t bno; /* directory file offset */
  576. xfs_dablk_t da; /* directory file offset */
  577. int done; /* bunmap is finished */
  578. xfs_inode_t *dp;
  579. int error;
  580. xfs_mount_t *mp;
  581. xfs_trans_t *tp;
  582. trace_xfs_dir2_shrink_inode(args, db);
  583. dp = args->dp;
  584. mp = dp->i_mount;
  585. tp = args->trans;
  586. da = xfs_dir2_db_to_da(args->geo, db);
  587. /* Unmap the fsblock(s). */
  588. error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0,
  589. args->firstblock, args->dfops, &done);
  590. if (error) {
  591. /*
  592. * ENOSPC actually can happen if we're in a removename with no
  593. * space reservation, and the resulting block removal would
  594. * cause a bmap btree split or conversion from extents to btree.
  595. * This can only happen for un-fragmented directory blocks,
  596. * since you need to be punching out the middle of an extent.
  597. * In this case we need to leave the block in the file, and not
  598. * binval it. So the block has to be in a consistent empty
  599. * state and appropriately logged. We don't free up the buffer,
  600. * the caller can tell it hasn't happened since it got an error
  601. * 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. }