xfs_dir2.c 18 KB

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