xfs_dir2.c 19 KB

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