xfs_dir2.c 18 KB

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