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. ASSERT(S_ISDIR(dp->i_d.di_mode));
  324. XFS_STATS_INC(xs_dir_lookup);
  325. /*
  326. * We need to use KM_NOFS here so that lockdep will not throw false
  327. * positive deadlock warnings on a non-transactional lookup path. It is
  328. * safe to recurse into inode recalim in that case, but lockdep can't
  329. * easily be taught about it. Hence KM_NOFS avoids having to add more
  330. * lockdep Doing this avoids having to add a bunch of lockdep class
  331. * annotations into the reclaim path for the ilock.
  332. */
  333. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  334. args->geo = dp->i_mount->m_dir_geo;
  335. args->name = name->name;
  336. args->namelen = name->len;
  337. args->filetype = name->type;
  338. args->hashval = dp->i_mount->m_dirnameops->hashname(name);
  339. args->dp = dp;
  340. args->whichfork = XFS_DATA_FORK;
  341. args->trans = tp;
  342. args->op_flags = XFS_DA_OP_OKNOENT;
  343. if (ci_name)
  344. args->op_flags |= XFS_DA_OP_CILOOKUP;
  345. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  346. rval = xfs_dir2_sf_lookup(args);
  347. goto out_check_rval;
  348. }
  349. rval = xfs_dir2_isblock(args, &v);
  350. if (rval)
  351. goto out_free;
  352. if (v) {
  353. rval = xfs_dir2_block_lookup(args);
  354. goto out_check_rval;
  355. }
  356. rval = xfs_dir2_isleaf(args, &v);
  357. if (rval)
  358. goto out_free;
  359. if (v)
  360. rval = xfs_dir2_leaf_lookup(args);
  361. else
  362. rval = xfs_dir2_node_lookup(args);
  363. out_check_rval:
  364. if (rval == -EEXIST)
  365. rval = 0;
  366. if (!rval) {
  367. *inum = args->inumber;
  368. if (ci_name) {
  369. ci_name->name = args->value;
  370. ci_name->len = args->valuelen;
  371. }
  372. }
  373. out_free:
  374. kmem_free(args);
  375. return rval;
  376. }
  377. /*
  378. * Remove an entry from a directory.
  379. */
  380. int
  381. xfs_dir_removename(
  382. xfs_trans_t *tp,
  383. xfs_inode_t *dp,
  384. struct xfs_name *name,
  385. xfs_ino_t ino,
  386. xfs_fsblock_t *first, /* bmap's firstblock */
  387. xfs_bmap_free_t *flist, /* bmap's freeblock list */
  388. xfs_extlen_t total) /* bmap's total block count */
  389. {
  390. struct xfs_da_args *args;
  391. int rval;
  392. int v; /* type-checking value */
  393. ASSERT(S_ISDIR(dp->i_d.di_mode));
  394. XFS_STATS_INC(xs_dir_remove);
  395. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  396. if (!args)
  397. return -ENOMEM;
  398. args->geo = dp->i_mount->m_dir_geo;
  399. args->name = name->name;
  400. args->namelen = name->len;
  401. args->filetype = name->type;
  402. args->hashval = dp->i_mount->m_dirnameops->hashname(name);
  403. args->inumber = ino;
  404. args->dp = dp;
  405. args->firstblock = first;
  406. args->flist = flist;
  407. args->total = total;
  408. args->whichfork = XFS_DATA_FORK;
  409. args->trans = tp;
  410. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  411. rval = xfs_dir2_sf_removename(args);
  412. goto out_free;
  413. }
  414. rval = xfs_dir2_isblock(args, &v);
  415. if (rval)
  416. goto out_free;
  417. if (v) {
  418. rval = xfs_dir2_block_removename(args);
  419. goto out_free;
  420. }
  421. rval = xfs_dir2_isleaf(args, &v);
  422. if (rval)
  423. goto out_free;
  424. if (v)
  425. rval = xfs_dir2_leaf_removename(args);
  426. else
  427. rval = xfs_dir2_node_removename(args);
  428. out_free:
  429. kmem_free(args);
  430. return rval;
  431. }
  432. /*
  433. * Replace the inode number of a directory entry.
  434. */
  435. int
  436. xfs_dir_replace(
  437. xfs_trans_t *tp,
  438. xfs_inode_t *dp,
  439. struct xfs_name *name, /* name of entry to replace */
  440. xfs_ino_t inum, /* new inode number */
  441. xfs_fsblock_t *first, /* bmap's firstblock */
  442. xfs_bmap_free_t *flist, /* bmap's freeblock list */
  443. xfs_extlen_t total) /* bmap's total block count */
  444. {
  445. struct xfs_da_args *args;
  446. int rval;
  447. int v; /* type-checking value */
  448. ASSERT(S_ISDIR(dp->i_d.di_mode));
  449. rval = xfs_dir_ino_validate(tp->t_mountp, inum);
  450. if (rval)
  451. return rval;
  452. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  453. if (!args)
  454. return -ENOMEM;
  455. args->geo = dp->i_mount->m_dir_geo;
  456. args->name = name->name;
  457. args->namelen = name->len;
  458. args->filetype = name->type;
  459. args->hashval = dp->i_mount->m_dirnameops->hashname(name);
  460. args->inumber = inum;
  461. args->dp = dp;
  462. args->firstblock = first;
  463. args->flist = flist;
  464. args->total = total;
  465. args->whichfork = XFS_DATA_FORK;
  466. args->trans = tp;
  467. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  468. rval = xfs_dir2_sf_replace(args);
  469. goto out_free;
  470. }
  471. rval = xfs_dir2_isblock(args, &v);
  472. if (rval)
  473. goto out_free;
  474. if (v) {
  475. rval = xfs_dir2_block_replace(args);
  476. goto out_free;
  477. }
  478. rval = xfs_dir2_isleaf(args, &v);
  479. if (rval)
  480. goto out_free;
  481. if (v)
  482. rval = xfs_dir2_leaf_replace(args);
  483. else
  484. rval = xfs_dir2_node_replace(args);
  485. out_free:
  486. kmem_free(args);
  487. return rval;
  488. }
  489. /*
  490. * See if this entry can be added to the directory without allocating space.
  491. */
  492. int
  493. xfs_dir_canenter(
  494. xfs_trans_t *tp,
  495. xfs_inode_t *dp,
  496. struct xfs_name *name) /* name of entry to add */
  497. {
  498. return xfs_dir_createname(tp, dp, name, 0, NULL, NULL, 0);
  499. }
  500. /*
  501. * Utility routines.
  502. */
  503. /*
  504. * Add a block to the directory.
  505. *
  506. * This routine is for data and free blocks, not leaf/node blocks which are
  507. * handled by xfs_da_grow_inode.
  508. */
  509. int
  510. xfs_dir2_grow_inode(
  511. struct xfs_da_args *args,
  512. int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
  513. xfs_dir2_db_t *dbp) /* out: block number added */
  514. {
  515. struct xfs_inode *dp = args->dp;
  516. struct xfs_mount *mp = dp->i_mount;
  517. xfs_fileoff_t bno; /* directory offset of new block */
  518. int count; /* count of filesystem blocks */
  519. int error;
  520. trace_xfs_dir2_grow_inode(args, space);
  521. /*
  522. * Set lowest possible block in the space requested.
  523. */
  524. bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
  525. count = args->geo->fsbcount;
  526. error = xfs_da_grow_inode_int(args, &bno, count);
  527. if (error)
  528. return error;
  529. *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
  530. /*
  531. * Update file's size if this is the data space and it grew.
  532. */
  533. if (space == XFS_DIR2_DATA_SPACE) {
  534. xfs_fsize_t size; /* directory file (data) size */
  535. size = XFS_FSB_TO_B(mp, bno + count);
  536. if (size > dp->i_d.di_size) {
  537. dp->i_d.di_size = size;
  538. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  539. }
  540. }
  541. return 0;
  542. }
  543. /*
  544. * See if the directory is a single-block form directory.
  545. */
  546. int
  547. xfs_dir2_isblock(
  548. struct xfs_da_args *args,
  549. int *vp) /* out: 1 is block, 0 is not block */
  550. {
  551. xfs_fileoff_t last; /* last file offset */
  552. int rval;
  553. if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
  554. return rval;
  555. rval = XFS_FSB_TO_B(args->dp->i_mount, last) == args->geo->blksize;
  556. ASSERT(rval == 0 || args->dp->i_d.di_size == args->geo->blksize);
  557. *vp = rval;
  558. return 0;
  559. }
  560. /*
  561. * See if the directory is a single-leaf form directory.
  562. */
  563. int
  564. xfs_dir2_isleaf(
  565. struct xfs_da_args *args,
  566. int *vp) /* out: 1 is block, 0 is not block */
  567. {
  568. xfs_fileoff_t last; /* last file offset */
  569. int rval;
  570. if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
  571. return rval;
  572. *vp = last == args->geo->leafblk + args->geo->fsbcount;
  573. return 0;
  574. }
  575. /*
  576. * Remove the given block from the directory.
  577. * This routine is used for data and free blocks, leaf/node are done
  578. * by xfs_da_shrink_inode.
  579. */
  580. int
  581. xfs_dir2_shrink_inode(
  582. xfs_da_args_t *args,
  583. xfs_dir2_db_t db,
  584. struct xfs_buf *bp)
  585. {
  586. xfs_fileoff_t bno; /* directory file offset */
  587. xfs_dablk_t da; /* directory file offset */
  588. int done; /* bunmap is finished */
  589. xfs_inode_t *dp;
  590. int error;
  591. xfs_mount_t *mp;
  592. xfs_trans_t *tp;
  593. trace_xfs_dir2_shrink_inode(args, db);
  594. dp = args->dp;
  595. mp = dp->i_mount;
  596. tp = args->trans;
  597. da = xfs_dir2_db_to_da(args->geo, db);
  598. /*
  599. * Unmap the fsblock(s).
  600. */
  601. if ((error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount,
  602. XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
  603. &done))) {
  604. /*
  605. * ENOSPC actually can happen if we're in a removename with
  606. * no space reservation, and the resulting block removal
  607. * would cause a bmap btree split or conversion from extents
  608. * to btree. This can only happen for un-fragmented
  609. * directory blocks, since you need to be punching out
  610. * the middle of an extent.
  611. * In this case we need to leave the block in the file,
  612. * and not binval it.
  613. * So the block has to be in a consistent empty state
  614. * and appropriately logged.
  615. * We don't free up the buffer, the caller can tell it
  616. * hasn't happened since it got an error back.
  617. */
  618. return error;
  619. }
  620. ASSERT(done);
  621. /*
  622. * Invalidate the buffer from the transaction.
  623. */
  624. xfs_trans_binval(tp, bp);
  625. /*
  626. * If it's not a data block, we're done.
  627. */
  628. if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
  629. return 0;
  630. /*
  631. * If the block isn't the last one in the directory, we're done.
  632. */
  633. if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
  634. return 0;
  635. bno = da;
  636. if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
  637. /*
  638. * This can't really happen unless there's kernel corruption.
  639. */
  640. return error;
  641. }
  642. if (db == args->geo->datablk)
  643. ASSERT(bno == 0);
  644. else
  645. ASSERT(bno > 0);
  646. /*
  647. * Set the size to the new last block.
  648. */
  649. dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
  650. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  651. return 0;
  652. }