xfs_dir2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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))) {
  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. kmem_free(mapp, sizeof(*mapp) * count);
  560. return error;
  561. }
  562. if (nmap < 1)
  563. break;
  564. /*
  565. * Add this bunch into our table, go to the next offset.
  566. */
  567. mapi += nmap;
  568. b = mapp[mapi - 1].br_startoff +
  569. mapp[mapi - 1].br_blockcount;
  570. }
  571. }
  572. /*
  573. * Didn't work.
  574. */
  575. else {
  576. mapi = 0;
  577. mapp = NULL;
  578. }
  579. /*
  580. * See how many fsb's we got.
  581. */
  582. for (i = 0, got = 0; i < mapi; i++)
  583. got += mapp[i].br_blockcount;
  584. /*
  585. * Didn't get enough fsb's, or the first/last block's are wrong.
  586. */
  587. if (got != count || mapp[0].br_startoff != bno ||
  588. mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
  589. bno + count) {
  590. if (mapp != &map)
  591. kmem_free(mapp, sizeof(*mapp) * count);
  592. return XFS_ERROR(ENOSPC);
  593. }
  594. /*
  595. * Done with the temporary mapping table.
  596. */
  597. if (mapp != &map)
  598. kmem_free(mapp, sizeof(*mapp) * count);
  599. *dbp = XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)bno);
  600. /*
  601. * Update file's size if this is the data space and it grew.
  602. */
  603. if (space == XFS_DIR2_DATA_SPACE) {
  604. xfs_fsize_t size; /* directory file (data) size */
  605. size = XFS_FSB_TO_B(mp, bno + count);
  606. if (size > dp->i_d.di_size) {
  607. dp->i_d.di_size = size;
  608. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  609. }
  610. }
  611. return 0;
  612. }
  613. /*
  614. * See if the directory is a single-block form directory.
  615. */
  616. int /* error */
  617. xfs_dir2_isblock(
  618. xfs_trans_t *tp, /* transaction pointer */
  619. xfs_inode_t *dp, /* incore directory inode */
  620. int *vp) /* out: 1 is block, 0 is not block */
  621. {
  622. xfs_fileoff_t last; /* last file offset */
  623. xfs_mount_t *mp; /* filesystem mount point */
  624. int rval; /* return value */
  625. mp = dp->i_mount;
  626. if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) {
  627. return rval;
  628. }
  629. rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize;
  630. ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize);
  631. *vp = rval;
  632. return 0;
  633. }
  634. /*
  635. * See if the directory is a single-leaf form directory.
  636. */
  637. int /* error */
  638. xfs_dir2_isleaf(
  639. xfs_trans_t *tp, /* transaction pointer */
  640. xfs_inode_t *dp, /* incore directory inode */
  641. int *vp) /* out: 1 is leaf, 0 is not leaf */
  642. {
  643. xfs_fileoff_t last; /* last file offset */
  644. xfs_mount_t *mp; /* filesystem mount point */
  645. int rval; /* return value */
  646. mp = dp->i_mount;
  647. if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) {
  648. return rval;
  649. }
  650. *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog);
  651. return 0;
  652. }
  653. /*
  654. * Getdents put routine for 64-bit ABI, direct form.
  655. */
  656. static int /* error */
  657. xfs_dir2_put_dirent64_direct(
  658. xfs_dir2_put_args_t *pa) /* argument bundle */
  659. {
  660. xfs_dirent_t *idbp; /* dirent pointer */
  661. iovec_t *iovp; /* io vector */
  662. int namelen; /* entry name length */
  663. int reclen; /* entry total length */
  664. uio_t *uio; /* I/O control */
  665. namelen = pa->namelen;
  666. reclen = DIRENTSIZE(namelen);
  667. uio = pa->uio;
  668. /*
  669. * Won't fit in the remaining space.
  670. */
  671. if (reclen > uio->uio_resid) {
  672. pa->done = 0;
  673. return 0;
  674. }
  675. iovp = uio->uio_iov;
  676. idbp = (xfs_dirent_t *)iovp->iov_base;
  677. iovp->iov_base = (char *)idbp + reclen;
  678. iovp->iov_len -= reclen;
  679. uio->uio_resid -= reclen;
  680. idbp->d_reclen = reclen;
  681. idbp->d_ino = pa->ino;
  682. idbp->d_off = pa->cook;
  683. idbp->d_name[namelen] = '\0';
  684. pa->done = 1;
  685. memcpy(idbp->d_name, pa->name, namelen);
  686. return 0;
  687. }
  688. /*
  689. * Getdents put routine for 64-bit ABI, uio form.
  690. */
  691. static int /* error */
  692. xfs_dir2_put_dirent64_uio(
  693. xfs_dir2_put_args_t *pa) /* argument bundle */
  694. {
  695. xfs_dirent_t *idbp; /* dirent pointer */
  696. int namelen; /* entry name length */
  697. int reclen; /* entry total length */
  698. int rval; /* return value */
  699. uio_t *uio; /* I/O control */
  700. namelen = pa->namelen;
  701. reclen = DIRENTSIZE(namelen);
  702. uio = pa->uio;
  703. /*
  704. * Won't fit in the remaining space.
  705. */
  706. if (reclen > uio->uio_resid) {
  707. pa->done = 0;
  708. return 0;
  709. }
  710. idbp = pa->dbp;
  711. idbp->d_reclen = reclen;
  712. idbp->d_ino = pa->ino;
  713. idbp->d_off = pa->cook;
  714. idbp->d_name[namelen] = '\0';
  715. memcpy(idbp->d_name, pa->name, namelen);
  716. rval = uio_read((caddr_t)idbp, reclen, uio);
  717. pa->done = (rval == 0);
  718. return rval;
  719. }
  720. /*
  721. * Remove the given block from the directory.
  722. * This routine is used for data and free blocks, leaf/node are done
  723. * by xfs_da_shrink_inode.
  724. */
  725. int
  726. xfs_dir2_shrink_inode(
  727. xfs_da_args_t *args, /* operation arguments */
  728. xfs_dir2_db_t db, /* directory block number */
  729. xfs_dabuf_t *bp) /* block's buffer */
  730. {
  731. xfs_fileoff_t bno; /* directory file offset */
  732. xfs_dablk_t da; /* directory file offset */
  733. int done; /* bunmap is finished */
  734. xfs_inode_t *dp; /* incore directory inode */
  735. int error; /* error return value */
  736. xfs_mount_t *mp; /* filesystem mount point */
  737. xfs_trans_t *tp; /* transaction pointer */
  738. xfs_dir2_trace_args_db("shrink_inode", args, db, bp);
  739. dp = args->dp;
  740. mp = dp->i_mount;
  741. tp = args->trans;
  742. da = XFS_DIR2_DB_TO_DA(mp, db);
  743. /*
  744. * Unmap the fsblock(s).
  745. */
  746. if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs,
  747. XFS_BMAPI_METADATA, 0, args->firstblock, args->flist,
  748. &done))) {
  749. /*
  750. * ENOSPC actually can happen if we're in a removename with
  751. * no space reservation, and the resulting block removal
  752. * would cause a bmap btree split or conversion from extents
  753. * to btree. This can only happen for un-fragmented
  754. * directory blocks, since you need to be punching out
  755. * the middle of an extent.
  756. * In this case we need to leave the block in the file,
  757. * and not binval it.
  758. * So the block has to be in a consistent empty state
  759. * and appropriately logged.
  760. * We don't free up the buffer, the caller can tell it
  761. * hasn't happened since it got an error back.
  762. */
  763. return error;
  764. }
  765. ASSERT(done);
  766. /*
  767. * Invalidate the buffer from the transaction.
  768. */
  769. xfs_da_binval(tp, bp);
  770. /*
  771. * If it's not a data block, we're done.
  772. */
  773. if (db >= XFS_DIR2_LEAF_FIRSTDB(mp))
  774. return 0;
  775. /*
  776. * If the block isn't the last one in the directory, we're done.
  777. */
  778. if (dp->i_d.di_size > XFS_DIR2_DB_OFF_TO_BYTE(mp, db + 1, 0))
  779. return 0;
  780. bno = da;
  781. if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
  782. /*
  783. * This can't really happen unless there's kernel corruption.
  784. */
  785. return error;
  786. }
  787. if (db == mp->m_dirdatablk)
  788. ASSERT(bno == 0);
  789. else
  790. ASSERT(bno > 0);
  791. /*
  792. * Set the size to the new last block.
  793. */
  794. dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
  795. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  796. return 0;
  797. }