xfs_dir2.c 23 KB

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