xfs_dir2.c 17 KB

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