xfs_dir2.c 23 KB

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