xfs_dir2.c 17 KB

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