xfs_dir2.c 18 KB

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