xfs_dir2.c 18 KB

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