xfs_dir2.c 18 KB

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