xfs_dir2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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_ialloc.h"
  34. #include "xfs_errortag.h"
  35. #include "xfs_error.h"
  36. #include "xfs_trace.h"
  37. struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR };
  38. /*
  39. * Convert inode mode to directory entry filetype
  40. */
  41. unsigned char
  42. xfs_mode_to_ftype(
  43. int mode)
  44. {
  45. switch (mode & S_IFMT) {
  46. case S_IFREG:
  47. return XFS_DIR3_FT_REG_FILE;
  48. case S_IFDIR:
  49. return XFS_DIR3_FT_DIR;
  50. case S_IFCHR:
  51. return XFS_DIR3_FT_CHRDEV;
  52. case S_IFBLK:
  53. return XFS_DIR3_FT_BLKDEV;
  54. case S_IFIFO:
  55. return XFS_DIR3_FT_FIFO;
  56. case S_IFSOCK:
  57. return XFS_DIR3_FT_SOCK;
  58. case S_IFLNK:
  59. return XFS_DIR3_FT_SYMLINK;
  60. default:
  61. return XFS_DIR3_FT_UNKNOWN;
  62. }
  63. }
  64. /*
  65. * ASCII case-insensitive (ie. A-Z) support for directories that was
  66. * used in IRIX.
  67. */
  68. STATIC xfs_dahash_t
  69. xfs_ascii_ci_hashname(
  70. struct xfs_name *name)
  71. {
  72. xfs_dahash_t hash;
  73. int i;
  74. for (i = 0, hash = 0; i < name->len; i++)
  75. hash = tolower(name->name[i]) ^ rol32(hash, 7);
  76. return hash;
  77. }
  78. STATIC enum xfs_dacmp
  79. xfs_ascii_ci_compname(
  80. struct xfs_da_args *args,
  81. const unsigned char *name,
  82. int len)
  83. {
  84. enum xfs_dacmp result;
  85. int i;
  86. if (args->namelen != len)
  87. return XFS_CMP_DIFFERENT;
  88. result = XFS_CMP_EXACT;
  89. for (i = 0; i < len; i++) {
  90. if (args->name[i] == name[i])
  91. continue;
  92. if (tolower(args->name[i]) != tolower(name[i]))
  93. return XFS_CMP_DIFFERENT;
  94. result = XFS_CMP_CASE;
  95. }
  96. return result;
  97. }
  98. static const struct xfs_nameops xfs_ascii_ci_nameops = {
  99. .hashname = xfs_ascii_ci_hashname,
  100. .compname = xfs_ascii_ci_compname,
  101. };
  102. int
  103. xfs_da_mount(
  104. struct xfs_mount *mp)
  105. {
  106. struct xfs_da_geometry *dageo;
  107. int nodehdr_size;
  108. ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
  109. ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
  110. mp->m_dir_inode_ops = xfs_dir_get_ops(mp, NULL);
  111. mp->m_nondir_inode_ops = xfs_nondir_get_ops(mp, NULL);
  112. nodehdr_size = mp->m_dir_inode_ops->node_hdr_size;
  113. mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
  114. KM_SLEEP | KM_MAYFAIL);
  115. mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
  116. KM_SLEEP | KM_MAYFAIL);
  117. if (!mp->m_dir_geo || !mp->m_attr_geo) {
  118. kmem_free(mp->m_dir_geo);
  119. kmem_free(mp->m_attr_geo);
  120. return -ENOMEM;
  121. }
  122. /* set up directory geometry */
  123. dageo = mp->m_dir_geo;
  124. dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
  125. dageo->fsblog = mp->m_sb.sb_blocklog;
  126. dageo->blksize = xfs_dir2_dirblock_bytes(&mp->m_sb);
  127. dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
  128. /*
  129. * Now we've set up the block conversion variables, we can calculate the
  130. * segment block constants using the geometry structure.
  131. */
  132. dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
  133. dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
  134. dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
  135. dageo->node_ents = (dageo->blksize - nodehdr_size) /
  136. (uint)sizeof(xfs_da_node_entry_t);
  137. dageo->magicpct = (dageo->blksize * 37) / 100;
  138. /* set up attribute geometry - single fsb only */
  139. dageo = mp->m_attr_geo;
  140. dageo->blklog = mp->m_sb.sb_blocklog;
  141. dageo->fsblog = mp->m_sb.sb_blocklog;
  142. dageo->blksize = 1 << dageo->blklog;
  143. dageo->fsbcount = 1;
  144. dageo->node_ents = (dageo->blksize - nodehdr_size) /
  145. (uint)sizeof(xfs_da_node_entry_t);
  146. dageo->magicpct = (dageo->blksize * 37) / 100;
  147. if (xfs_sb_version_hasasciici(&mp->m_sb))
  148. mp->m_dirnameops = &xfs_ascii_ci_nameops;
  149. else
  150. mp->m_dirnameops = &xfs_default_nameops;
  151. return 0;
  152. }
  153. void
  154. xfs_da_unmount(
  155. struct xfs_mount *mp)
  156. {
  157. kmem_free(mp->m_dir_geo);
  158. kmem_free(mp->m_attr_geo);
  159. }
  160. /*
  161. * Return 1 if directory contains only "." and "..".
  162. */
  163. int
  164. xfs_dir_isempty(
  165. xfs_inode_t *dp)
  166. {
  167. xfs_dir2_sf_hdr_t *sfp;
  168. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  169. if (dp->i_d.di_size == 0) /* might happen during shutdown. */
  170. return 1;
  171. if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
  172. return 0;
  173. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  174. return !sfp->count;
  175. }
  176. /*
  177. * Validate a given inode number.
  178. */
  179. int
  180. xfs_dir_ino_validate(
  181. xfs_mount_t *mp,
  182. xfs_ino_t ino)
  183. {
  184. bool ino_ok = xfs_verify_dir_ino(mp, ino);
  185. if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE))) {
  186. xfs_warn(mp, "Invalid inode number 0x%Lx",
  187. (unsigned long long) ino);
  188. XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
  189. return -EFSCORRUPTED;
  190. }
  191. return 0;
  192. }
  193. /*
  194. * Initialize a directory with its "." and ".." entries.
  195. */
  196. int
  197. xfs_dir_init(
  198. xfs_trans_t *tp,
  199. xfs_inode_t *dp,
  200. xfs_inode_t *pdp)
  201. {
  202. struct xfs_da_args *args;
  203. int error;
  204. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  205. error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
  206. if (error)
  207. return error;
  208. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  209. if (!args)
  210. return -ENOMEM;
  211. args->geo = dp->i_mount->m_dir_geo;
  212. args->dp = dp;
  213. args->trans = tp;
  214. error = xfs_dir2_sf_create(args, pdp->i_ino);
  215. kmem_free(args);
  216. return error;
  217. }
  218. /*
  219. * Enter a name in a directory, or check for available space.
  220. * If inum is 0, only the available space test is performed.
  221. */
  222. int
  223. xfs_dir_createname(
  224. xfs_trans_t *tp,
  225. xfs_inode_t *dp,
  226. struct xfs_name *name,
  227. xfs_ino_t inum, /* new entry inode number */
  228. xfs_fsblock_t *first, /* bmap's firstblock */
  229. struct xfs_defer_ops *dfops, /* bmap's freeblock list */
  230. xfs_extlen_t total) /* bmap's total block count */
  231. {
  232. struct xfs_da_args *args;
  233. int rval;
  234. int v; /* type-checking value */
  235. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  236. if (inum) {
  237. rval = xfs_dir_ino_validate(tp->t_mountp, inum);
  238. if (rval)
  239. return rval;
  240. XFS_STATS_INC(dp->i_mount, xs_dir_create);
  241. }
  242. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  243. if (!args)
  244. return -ENOMEM;
  245. args->geo = dp->i_mount->m_dir_geo;
  246. args->name = name->name;
  247. args->namelen = name->len;
  248. args->filetype = name->type;
  249. args->hashval = dp->i_mount->m_dirnameops->hashname(name);
  250. args->inumber = inum;
  251. args->dp = dp;
  252. args->firstblock = first;
  253. args->dfops = dfops;
  254. args->total = total;
  255. args->whichfork = XFS_DATA_FORK;
  256. args->trans = tp;
  257. args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  258. if (!inum)
  259. args->op_flags |= XFS_DA_OP_JUSTCHECK;
  260. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  261. rval = xfs_dir2_sf_addname(args);
  262. goto out_free;
  263. }
  264. rval = xfs_dir2_isblock(args, &v);
  265. if (rval)
  266. goto out_free;
  267. if (v) {
  268. rval = xfs_dir2_block_addname(args);
  269. goto out_free;
  270. }
  271. rval = xfs_dir2_isleaf(args, &v);
  272. if (rval)
  273. goto out_free;
  274. if (v)
  275. rval = xfs_dir2_leaf_addname(args);
  276. else
  277. rval = xfs_dir2_node_addname(args);
  278. out_free:
  279. kmem_free(args);
  280. return rval;
  281. }
  282. /*
  283. * If doing a CI lookup and case-insensitive match, dup actual name into
  284. * args.value. Return EEXIST for success (ie. name found) or an error.
  285. */
  286. int
  287. xfs_dir_cilookup_result(
  288. struct xfs_da_args *args,
  289. const unsigned char *name,
  290. int len)
  291. {
  292. if (args->cmpresult == XFS_CMP_DIFFERENT)
  293. return -ENOENT;
  294. if (args->cmpresult != XFS_CMP_CASE ||
  295. !(args->op_flags & XFS_DA_OP_CILOOKUP))
  296. return -EEXIST;
  297. args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
  298. if (!args->value)
  299. return -ENOMEM;
  300. memcpy(args->value, name, len);
  301. args->valuelen = len;
  302. return -EEXIST;
  303. }
  304. /*
  305. * Lookup a name in a directory, give back the inode number.
  306. * If ci_name is not NULL, returns the actual name in ci_name if it differs
  307. * to name, or ci_name->name is set to NULL for an exact match.
  308. */
  309. int
  310. xfs_dir_lookup(
  311. xfs_trans_t *tp,
  312. xfs_inode_t *dp,
  313. struct xfs_name *name,
  314. xfs_ino_t *inum, /* out: inode number */
  315. struct xfs_name *ci_name) /* out: actual name if CI match */
  316. {
  317. struct xfs_da_args *args;
  318. int rval;
  319. int v; /* type-checking value */
  320. int lock_mode;
  321. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  322. XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
  323. /*
  324. * We need to use KM_NOFS here so that lockdep will not throw false
  325. * positive deadlock warnings on a non-transactional lookup path. It is
  326. * safe to recurse into inode recalim in that case, but lockdep can't
  327. * easily be taught about it. Hence KM_NOFS avoids having to add more
  328. * lockdep Doing this avoids having to add a bunch of lockdep class
  329. * annotations into the reclaim path for the ilock.
  330. */
  331. args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
  332. args->geo = dp->i_mount->m_dir_geo;
  333. args->name = name->name;
  334. args->namelen = name->len;
  335. args->filetype = name->type;
  336. args->hashval = dp->i_mount->m_dirnameops->hashname(name);
  337. args->dp = dp;
  338. args->whichfork = XFS_DATA_FORK;
  339. args->trans = tp;
  340. args->op_flags = XFS_DA_OP_OKNOENT;
  341. if (ci_name)
  342. args->op_flags |= XFS_DA_OP_CILOOKUP;
  343. lock_mode = xfs_ilock_data_map_shared(dp);
  344. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  345. rval = xfs_dir2_sf_lookup(args);
  346. goto out_check_rval;
  347. }
  348. rval = xfs_dir2_isblock(args, &v);
  349. if (rval)
  350. goto out_free;
  351. if (v) {
  352. rval = xfs_dir2_block_lookup(args);
  353. goto out_check_rval;
  354. }
  355. rval = xfs_dir2_isleaf(args, &v);
  356. if (rval)
  357. goto out_free;
  358. if (v)
  359. rval = xfs_dir2_leaf_lookup(args);
  360. else
  361. rval = xfs_dir2_node_lookup(args);
  362. out_check_rval:
  363. if (rval == -EEXIST)
  364. rval = 0;
  365. if (!rval) {
  366. *inum = args->inumber;
  367. if (ci_name) {
  368. ci_name->name = args->value;
  369. ci_name->len = args->valuelen;
  370. }
  371. }
  372. out_free:
  373. xfs_iunlock(dp, lock_mode);
  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. struct xfs_defer_ops *dfops, /* 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(VFS_I(dp)->i_mode));
  394. XFS_STATS_INC(dp->i_mount, 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->dfops = dfops;
  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. struct xfs_defer_ops *dfops, /* 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(VFS_I(dp)->i_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->dfops = dfops;
  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. if (rval != 0 && args->dp->i_d.di_size != args->geo->blksize)
  557. return -EFSCORRUPTED;
  558. *vp = rval;
  559. return 0;
  560. }
  561. /*
  562. * See if the directory is a single-leaf form directory.
  563. */
  564. int
  565. xfs_dir2_isleaf(
  566. struct xfs_da_args *args,
  567. int *vp) /* out: 1 is block, 0 is not block */
  568. {
  569. xfs_fileoff_t last; /* last file offset */
  570. int rval;
  571. if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
  572. return rval;
  573. *vp = last == args->geo->leafblk + args->geo->fsbcount;
  574. return 0;
  575. }
  576. /*
  577. * Remove the given block from the directory.
  578. * This routine is used for data and free blocks, leaf/node are done
  579. * by xfs_da_shrink_inode.
  580. */
  581. int
  582. xfs_dir2_shrink_inode(
  583. xfs_da_args_t *args,
  584. xfs_dir2_db_t db,
  585. struct xfs_buf *bp)
  586. {
  587. xfs_fileoff_t bno; /* directory file offset */
  588. xfs_dablk_t da; /* directory file offset */
  589. int done; /* bunmap is finished */
  590. xfs_inode_t *dp;
  591. int error;
  592. xfs_mount_t *mp;
  593. xfs_trans_t *tp;
  594. trace_xfs_dir2_shrink_inode(args, db);
  595. dp = args->dp;
  596. mp = dp->i_mount;
  597. tp = args->trans;
  598. da = xfs_dir2_db_to_da(args->geo, db);
  599. /* Unmap the fsblock(s). */
  600. error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0,
  601. args->firstblock, args->dfops, &done);
  602. if (error) {
  603. /*
  604. * ENOSPC actually can happen if we're in a removename with no
  605. * space reservation, and the resulting block removal would
  606. * cause a bmap btree split or conversion from extents to btree.
  607. * This can only happen for un-fragmented directory blocks,
  608. * since you need to be punching out the middle of an extent.
  609. * In this case we need to leave the block in the file, and not
  610. * binval it. So the block has to be in a consistent empty
  611. * state and appropriately logged. We don't free up the buffer,
  612. * the caller can tell it hasn't happened since it got an error
  613. * back.
  614. */
  615. return error;
  616. }
  617. ASSERT(done);
  618. /*
  619. * Invalidate the buffer from the transaction.
  620. */
  621. xfs_trans_binval(tp, bp);
  622. /*
  623. * If it's not a data block, we're done.
  624. */
  625. if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
  626. return 0;
  627. /*
  628. * If the block isn't the last one in the directory, we're done.
  629. */
  630. if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
  631. return 0;
  632. bno = da;
  633. if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
  634. /*
  635. * This can't really happen unless there's kernel corruption.
  636. */
  637. return error;
  638. }
  639. if (db == args->geo->datablk)
  640. ASSERT(bno == 0);
  641. else
  642. ASSERT(bno > 0);
  643. /*
  644. * Set the size to the new last block.
  645. */
  646. dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
  647. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  648. return 0;
  649. }