xfs_dir2.c 19 KB

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