xfs_dir2.c 18 KB

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