xfs_dir2.c 23 KB

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