xfs_dir2.c 19 KB

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