xfs_dir2_sf.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /*
  2. * Copyright (c) 2000-2003,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_format.h"
  21. #include "xfs_log_format.h"
  22. #include "xfs_trans_resv.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_da_format.h"
  27. #include "xfs_da_btree.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_inode_item.h"
  31. #include "xfs_error.h"
  32. #include "xfs_dir2.h"
  33. #include "xfs_dir2_priv.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_dinode.h"
  36. /*
  37. * Prototypes for internal functions.
  38. */
  39. static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
  40. xfs_dir2_sf_entry_t *sfep,
  41. xfs_dir2_data_aoff_t offset,
  42. int new_isize);
  43. static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
  44. int new_isize);
  45. static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
  46. xfs_dir2_sf_entry_t **sfepp,
  47. xfs_dir2_data_aoff_t *offsetp);
  48. #ifdef DEBUG
  49. static void xfs_dir2_sf_check(xfs_da_args_t *args);
  50. #else
  51. #define xfs_dir2_sf_check(args)
  52. #endif /* DEBUG */
  53. #if XFS_BIG_INUMS
  54. static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
  55. static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
  56. #endif /* XFS_BIG_INUMS */
  57. /*
  58. * Given a block directory (dp/block), calculate its size as a shortform (sf)
  59. * directory and a header for the sf directory, if it will fit it the
  60. * space currently present in the inode. If it won't fit, the output
  61. * size is too big (but not accurate).
  62. */
  63. int /* size for sf form */
  64. xfs_dir2_block_sfsize(
  65. xfs_inode_t *dp, /* incore inode pointer */
  66. xfs_dir2_data_hdr_t *hdr, /* block directory data */
  67. xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
  68. {
  69. xfs_dir2_dataptr_t addr; /* data entry address */
  70. xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
  71. xfs_dir2_block_tail_t *btp; /* tail area of the block */
  72. int count; /* shortform entry count */
  73. xfs_dir2_data_entry_t *dep; /* data entry in the block */
  74. int i; /* block entry index */
  75. int i8count; /* count of big-inode entries */
  76. int isdot; /* entry is "." */
  77. int isdotdot; /* entry is ".." */
  78. xfs_mount_t *mp; /* mount structure pointer */
  79. int namelen; /* total name bytes */
  80. xfs_ino_t parent = 0; /* parent inode number */
  81. int size=0; /* total computed size */
  82. int has_ftype;
  83. mp = dp->i_mount;
  84. /*
  85. * if there is a filetype field, add the extra byte to the namelen
  86. * for each entry that we see.
  87. */
  88. has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
  89. count = i8count = namelen = 0;
  90. btp = xfs_dir2_block_tail_p(mp, hdr);
  91. blp = xfs_dir2_block_leaf_p(btp);
  92. /*
  93. * Iterate over the block's data entries by using the leaf pointers.
  94. */
  95. for (i = 0; i < be32_to_cpu(btp->count); i++) {
  96. if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
  97. continue;
  98. /*
  99. * Calculate the pointer to the entry at hand.
  100. */
  101. dep = (xfs_dir2_data_entry_t *)((char *)hdr +
  102. xfs_dir2_dataptr_to_off(mp->m_dir_geo, addr));
  103. /*
  104. * Detect . and .., so we can special-case them.
  105. * . is not included in sf directories.
  106. * .. is included by just the parent inode number.
  107. */
  108. isdot = dep->namelen == 1 && dep->name[0] == '.';
  109. isdotdot =
  110. dep->namelen == 2 &&
  111. dep->name[0] == '.' && dep->name[1] == '.';
  112. #if XFS_BIG_INUMS
  113. if (!isdot)
  114. i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
  115. #endif
  116. /* take into account the file type field */
  117. if (!isdot && !isdotdot) {
  118. count++;
  119. namelen += dep->namelen + has_ftype;
  120. } else if (isdotdot)
  121. parent = be64_to_cpu(dep->inumber);
  122. /*
  123. * Calculate the new size, see if we should give up yet.
  124. */
  125. size = xfs_dir2_sf_hdr_size(i8count) + /* header */
  126. count + /* namelen */
  127. count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
  128. namelen + /* name */
  129. (i8count ? /* inumber */
  130. (uint)sizeof(xfs_dir2_ino8_t) * count :
  131. (uint)sizeof(xfs_dir2_ino4_t) * count);
  132. if (size > XFS_IFORK_DSIZE(dp))
  133. return size; /* size value is a failure */
  134. }
  135. /*
  136. * Create the output header, if it worked.
  137. */
  138. sfhp->count = count;
  139. sfhp->i8count = i8count;
  140. dp->d_ops->sf_put_parent_ino(sfhp, parent);
  141. return size;
  142. }
  143. /*
  144. * Convert a block format directory to shortform.
  145. * Caller has already checked that it will fit, and built us a header.
  146. */
  147. int /* error */
  148. xfs_dir2_block_to_sf(
  149. xfs_da_args_t *args, /* operation arguments */
  150. struct xfs_buf *bp,
  151. int size, /* shortform directory size */
  152. xfs_dir2_sf_hdr_t *sfhp) /* shortform directory hdr */
  153. {
  154. xfs_dir2_data_hdr_t *hdr; /* block header */
  155. xfs_dir2_block_tail_t *btp; /* block tail pointer */
  156. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  157. xfs_inode_t *dp; /* incore directory inode */
  158. xfs_dir2_data_unused_t *dup; /* unused data pointer */
  159. char *endptr; /* end of data entries */
  160. int error; /* error return value */
  161. int logflags; /* inode logging flags */
  162. xfs_mount_t *mp; /* filesystem mount point */
  163. char *ptr; /* current data pointer */
  164. xfs_dir2_sf_entry_t *sfep; /* shortform entry */
  165. xfs_dir2_sf_hdr_t *sfp; /* shortform directory header */
  166. xfs_dir2_sf_hdr_t *dst; /* temporary data buffer */
  167. trace_xfs_dir2_block_to_sf(args);
  168. dp = args->dp;
  169. mp = dp->i_mount;
  170. /*
  171. * allocate a temporary destination buffer the size of the inode
  172. * to format the data into. Once we have formatted the data, we
  173. * can free the block and copy the formatted data into the inode literal
  174. * area.
  175. */
  176. dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
  177. hdr = bp->b_addr;
  178. /*
  179. * Copy the header into the newly allocate local space.
  180. */
  181. sfp = (xfs_dir2_sf_hdr_t *)dst;
  182. memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
  183. /*
  184. * Set up to loop over the block's entries.
  185. */
  186. btp = xfs_dir2_block_tail_p(mp, hdr);
  187. ptr = (char *)dp->d_ops->data_entry_p(hdr);
  188. endptr = (char *)xfs_dir2_block_leaf_p(btp);
  189. sfep = xfs_dir2_sf_firstentry(sfp);
  190. /*
  191. * Loop over the active and unused entries.
  192. * Stop when we reach the leaf/tail portion of the block.
  193. */
  194. while (ptr < endptr) {
  195. /*
  196. * If it's unused, just skip over it.
  197. */
  198. dup = (xfs_dir2_data_unused_t *)ptr;
  199. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  200. ptr += be16_to_cpu(dup->length);
  201. continue;
  202. }
  203. dep = (xfs_dir2_data_entry_t *)ptr;
  204. /*
  205. * Skip .
  206. */
  207. if (dep->namelen == 1 && dep->name[0] == '.')
  208. ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
  209. /*
  210. * Skip .., but make sure the inode number is right.
  211. */
  212. else if (dep->namelen == 2 &&
  213. dep->name[0] == '.' && dep->name[1] == '.')
  214. ASSERT(be64_to_cpu(dep->inumber) ==
  215. dp->d_ops->sf_get_parent_ino(sfp));
  216. /*
  217. * Normal entry, copy it into shortform.
  218. */
  219. else {
  220. sfep->namelen = dep->namelen;
  221. xfs_dir2_sf_put_offset(sfep,
  222. (xfs_dir2_data_aoff_t)
  223. ((char *)dep - (char *)hdr));
  224. memcpy(sfep->name, dep->name, dep->namelen);
  225. dp->d_ops->sf_put_ino(sfp, sfep,
  226. be64_to_cpu(dep->inumber));
  227. dp->d_ops->sf_put_ftype(sfep,
  228. dp->d_ops->data_get_ftype(dep));
  229. sfep = dp->d_ops->sf_nextentry(sfp, sfep);
  230. }
  231. ptr += dp->d_ops->data_entsize(dep->namelen);
  232. }
  233. ASSERT((char *)sfep - (char *)sfp == size);
  234. /* now we are done with the block, we can shrink the inode */
  235. logflags = XFS_ILOG_CORE;
  236. error = xfs_dir2_shrink_inode(args, mp->m_dirdatablk, bp);
  237. if (error) {
  238. ASSERT(error != ENOSPC);
  239. goto out;
  240. }
  241. /*
  242. * The buffer is now unconditionally gone, whether
  243. * xfs_dir2_shrink_inode worked or not.
  244. *
  245. * Convert the inode to local format and copy the data in.
  246. */
  247. dp->i_df.if_flags &= ~XFS_IFEXTENTS;
  248. dp->i_df.if_flags |= XFS_IFINLINE;
  249. dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  250. ASSERT(dp->i_df.if_bytes == 0);
  251. xfs_idata_realloc(dp, size, XFS_DATA_FORK);
  252. logflags |= XFS_ILOG_DDATA;
  253. memcpy(dp->i_df.if_u1.if_data, dst, size);
  254. dp->i_d.di_size = size;
  255. xfs_dir2_sf_check(args);
  256. out:
  257. xfs_trans_log_inode(args->trans, dp, logflags);
  258. kmem_free(dst);
  259. return error;
  260. }
  261. /*
  262. * Add a name to a shortform directory.
  263. * There are two algorithms, "easy" and "hard" which we decide on
  264. * before changing anything.
  265. * Convert to block form if necessary, if the new entry won't fit.
  266. */
  267. int /* error */
  268. xfs_dir2_sf_addname(
  269. xfs_da_args_t *args) /* operation arguments */
  270. {
  271. xfs_inode_t *dp; /* incore directory inode */
  272. int error; /* error return value */
  273. int incr_isize; /* total change in size */
  274. int new_isize; /* di_size after adding name */
  275. int objchange; /* changing to 8-byte inodes */
  276. xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
  277. int pick; /* which algorithm to use */
  278. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  279. xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
  280. trace_xfs_dir2_sf_addname(args);
  281. ASSERT(xfs_dir2_sf_lookup(args) == ENOENT);
  282. dp = args->dp;
  283. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  284. /*
  285. * Make sure the shortform value has some of its header.
  286. */
  287. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  288. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  289. return XFS_ERROR(EIO);
  290. }
  291. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  292. ASSERT(dp->i_df.if_u1.if_data != NULL);
  293. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  294. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
  295. /*
  296. * Compute entry (and change in) size.
  297. */
  298. incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
  299. objchange = 0;
  300. #if XFS_BIG_INUMS
  301. /*
  302. * Do we have to change to 8 byte inodes?
  303. */
  304. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
  305. /*
  306. * Yes, adjust the inode size. old count + (parent + new)
  307. */
  308. incr_isize +=
  309. (sfp->count + 2) *
  310. ((uint)sizeof(xfs_dir2_ino8_t) -
  311. (uint)sizeof(xfs_dir2_ino4_t));
  312. objchange = 1;
  313. }
  314. #endif
  315. new_isize = (int)dp->i_d.di_size + incr_isize;
  316. /*
  317. * Won't fit as shortform any more (due to size),
  318. * or the pick routine says it won't (due to offset values).
  319. */
  320. if (new_isize > XFS_IFORK_DSIZE(dp) ||
  321. (pick =
  322. xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
  323. /*
  324. * Just checking or no space reservation, it doesn't fit.
  325. */
  326. if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
  327. return XFS_ERROR(ENOSPC);
  328. /*
  329. * Convert to block form then add the name.
  330. */
  331. error = xfs_dir2_sf_to_block(args);
  332. if (error)
  333. return error;
  334. return xfs_dir2_block_addname(args);
  335. }
  336. /*
  337. * Just checking, it fits.
  338. */
  339. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  340. return 0;
  341. /*
  342. * Do it the easy way - just add it at the end.
  343. */
  344. if (pick == 1)
  345. xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
  346. /*
  347. * Do it the hard way - look for a place to insert the new entry.
  348. * Convert to 8 byte inode numbers first if necessary.
  349. */
  350. else {
  351. ASSERT(pick == 2);
  352. #if XFS_BIG_INUMS
  353. if (objchange)
  354. xfs_dir2_sf_toino8(args);
  355. #endif
  356. xfs_dir2_sf_addname_hard(args, objchange, new_isize);
  357. }
  358. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  359. return 0;
  360. }
  361. /*
  362. * Add the new entry the "easy" way.
  363. * This is copying the old directory and adding the new entry at the end.
  364. * Since it's sorted by "offset" we need room after the last offset
  365. * that's already there, and then room to convert to a block directory.
  366. * This is already checked by the pick routine.
  367. */
  368. static void
  369. xfs_dir2_sf_addname_easy(
  370. xfs_da_args_t *args, /* operation arguments */
  371. xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
  372. xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
  373. int new_isize) /* new directory size */
  374. {
  375. int byteoff; /* byte offset in sf dir */
  376. xfs_inode_t *dp; /* incore directory inode */
  377. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  378. dp = args->dp;
  379. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  380. byteoff = (int)((char *)sfep - (char *)sfp);
  381. /*
  382. * Grow the in-inode space.
  383. */
  384. xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
  385. XFS_DATA_FORK);
  386. /*
  387. * Need to set up again due to realloc of the inode data.
  388. */
  389. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  390. sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
  391. /*
  392. * Fill in the new entry.
  393. */
  394. sfep->namelen = args->namelen;
  395. xfs_dir2_sf_put_offset(sfep, offset);
  396. memcpy(sfep->name, args->name, sfep->namelen);
  397. dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
  398. dp->d_ops->sf_put_ftype(sfep, args->filetype);
  399. /*
  400. * Update the header and inode.
  401. */
  402. sfp->count++;
  403. #if XFS_BIG_INUMS
  404. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
  405. sfp->i8count++;
  406. #endif
  407. dp->i_d.di_size = new_isize;
  408. xfs_dir2_sf_check(args);
  409. }
  410. /*
  411. * Add the new entry the "hard" way.
  412. * The caller has already converted to 8 byte inode numbers if necessary,
  413. * in which case we need to leave the i8count at 1.
  414. * Find a hole that the new entry will fit into, and copy
  415. * the first part of the entries, the new entry, and the last part of
  416. * the entries.
  417. */
  418. /* ARGSUSED */
  419. static void
  420. xfs_dir2_sf_addname_hard(
  421. xfs_da_args_t *args, /* operation arguments */
  422. int objchange, /* changing inode number size */
  423. int new_isize) /* new directory size */
  424. {
  425. int add_datasize; /* data size need for new ent */
  426. char *buf; /* buffer for old */
  427. xfs_inode_t *dp; /* incore directory inode */
  428. int eof; /* reached end of old dir */
  429. int nbytes; /* temp for byte copies */
  430. xfs_dir2_data_aoff_t new_offset; /* next offset value */
  431. xfs_dir2_data_aoff_t offset; /* current offset value */
  432. int old_isize; /* previous di_size */
  433. xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
  434. xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
  435. xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
  436. xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
  437. struct xfs_mount *mp;
  438. /*
  439. * Copy the old directory to the stack buffer.
  440. */
  441. dp = args->dp;
  442. mp = dp->i_mount;
  443. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  444. old_isize = (int)dp->i_d.di_size;
  445. buf = kmem_alloc(old_isize, KM_SLEEP);
  446. oldsfp = (xfs_dir2_sf_hdr_t *)buf;
  447. memcpy(oldsfp, sfp, old_isize);
  448. /*
  449. * Loop over the old directory finding the place we're going
  450. * to insert the new entry.
  451. * If it's going to end up at the end then oldsfep will point there.
  452. */
  453. for (offset = dp->d_ops->data_first_offset,
  454. oldsfep = xfs_dir2_sf_firstentry(oldsfp),
  455. add_datasize = dp->d_ops->data_entsize(args->namelen),
  456. eof = (char *)oldsfep == &buf[old_isize];
  457. !eof;
  458. offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
  459. oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
  460. eof = (char *)oldsfep == &buf[old_isize]) {
  461. new_offset = xfs_dir2_sf_get_offset(oldsfep);
  462. if (offset + add_datasize <= new_offset)
  463. break;
  464. }
  465. /*
  466. * Get rid of the old directory, then allocate space for
  467. * the new one. We do this so xfs_idata_realloc won't copy
  468. * the data.
  469. */
  470. xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
  471. xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
  472. /*
  473. * Reset the pointer since the buffer was reallocated.
  474. */
  475. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  476. /*
  477. * Copy the first part of the directory, including the header.
  478. */
  479. nbytes = (int)((char *)oldsfep - (char *)oldsfp);
  480. memcpy(sfp, oldsfp, nbytes);
  481. sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
  482. /*
  483. * Fill in the new entry, and update the header counts.
  484. */
  485. sfep->namelen = args->namelen;
  486. xfs_dir2_sf_put_offset(sfep, offset);
  487. memcpy(sfep->name, args->name, sfep->namelen);
  488. dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
  489. dp->d_ops->sf_put_ftype(sfep, args->filetype);
  490. sfp->count++;
  491. #if XFS_BIG_INUMS
  492. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
  493. sfp->i8count++;
  494. #endif
  495. /*
  496. * If there's more left to copy, do that.
  497. */
  498. if (!eof) {
  499. sfep = dp->d_ops->sf_nextentry(sfp, sfep);
  500. memcpy(sfep, oldsfep, old_isize - nbytes);
  501. }
  502. kmem_free(buf);
  503. dp->i_d.di_size = new_isize;
  504. xfs_dir2_sf_check(args);
  505. }
  506. /*
  507. * Decide if the new entry will fit at all.
  508. * If it will fit, pick between adding the new entry to the end (easy)
  509. * or somewhere else (hard).
  510. * Return 0 (won't fit), 1 (easy), 2 (hard).
  511. */
  512. /*ARGSUSED*/
  513. static int /* pick result */
  514. xfs_dir2_sf_addname_pick(
  515. xfs_da_args_t *args, /* operation arguments */
  516. int objchange, /* inode # size changes */
  517. xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
  518. xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
  519. {
  520. xfs_inode_t *dp; /* incore directory inode */
  521. int holefit; /* found hole it will fit in */
  522. int i; /* entry number */
  523. xfs_mount_t *mp; /* filesystem mount point */
  524. xfs_dir2_data_aoff_t offset; /* data block offset */
  525. xfs_dir2_sf_entry_t *sfep; /* shortform entry */
  526. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  527. int size; /* entry's data size */
  528. int used; /* data bytes used */
  529. dp = args->dp;
  530. mp = dp->i_mount;
  531. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  532. size = dp->d_ops->data_entsize(args->namelen);
  533. offset = dp->d_ops->data_first_offset;
  534. sfep = xfs_dir2_sf_firstentry(sfp);
  535. holefit = 0;
  536. /*
  537. * Loop over sf entries.
  538. * Keep track of data offset and whether we've seen a place
  539. * to insert the new entry.
  540. */
  541. for (i = 0; i < sfp->count; i++) {
  542. if (!holefit)
  543. holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
  544. offset = xfs_dir2_sf_get_offset(sfep) +
  545. dp->d_ops->data_entsize(sfep->namelen);
  546. sfep = dp->d_ops->sf_nextentry(sfp, sfep);
  547. }
  548. /*
  549. * Calculate data bytes used excluding the new entry, if this
  550. * was a data block (block form directory).
  551. */
  552. used = offset +
  553. (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
  554. (uint)sizeof(xfs_dir2_block_tail_t);
  555. /*
  556. * If it won't fit in a block form then we can't insert it,
  557. * we'll go back, convert to block, then try the insert and convert
  558. * to leaf.
  559. */
  560. if (used + (holefit ? 0 : size) > mp->m_dirblksize)
  561. return 0;
  562. /*
  563. * If changing the inode number size, do it the hard way.
  564. */
  565. #if XFS_BIG_INUMS
  566. if (objchange) {
  567. return 2;
  568. }
  569. #else
  570. ASSERT(objchange == 0);
  571. #endif
  572. /*
  573. * If it won't fit at the end then do it the hard way (use the hole).
  574. */
  575. if (used + size > mp->m_dirblksize)
  576. return 2;
  577. /*
  578. * Do it the easy way.
  579. */
  580. *sfepp = sfep;
  581. *offsetp = offset;
  582. return 1;
  583. }
  584. #ifdef DEBUG
  585. /*
  586. * Check consistency of shortform directory, assert if bad.
  587. */
  588. static void
  589. xfs_dir2_sf_check(
  590. xfs_da_args_t *args) /* operation arguments */
  591. {
  592. xfs_inode_t *dp; /* incore directory inode */
  593. int i; /* entry number */
  594. int i8count; /* number of big inode#s */
  595. xfs_ino_t ino; /* entry inode number */
  596. int offset; /* data offset */
  597. xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
  598. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  599. struct xfs_mount *mp;
  600. dp = args->dp;
  601. mp = dp->i_mount;
  602. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  603. offset = dp->d_ops->data_first_offset;
  604. ino = dp->d_ops->sf_get_parent_ino(sfp);
  605. i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
  606. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
  607. i < sfp->count;
  608. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
  609. ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
  610. ino = dp->d_ops->sf_get_ino(sfp, sfep);
  611. i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
  612. offset =
  613. xfs_dir2_sf_get_offset(sfep) +
  614. dp->d_ops->data_entsize(sfep->namelen);
  615. ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
  616. }
  617. ASSERT(i8count == sfp->i8count);
  618. ASSERT(XFS_BIG_INUMS || i8count == 0);
  619. ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
  620. ASSERT(offset +
  621. (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
  622. (uint)sizeof(xfs_dir2_block_tail_t) <= mp->m_dirblksize);
  623. }
  624. #endif /* DEBUG */
  625. /*
  626. * Create a new (shortform) directory.
  627. */
  628. int /* error, always 0 */
  629. xfs_dir2_sf_create(
  630. xfs_da_args_t *args, /* operation arguments */
  631. xfs_ino_t pino) /* parent inode number */
  632. {
  633. xfs_inode_t *dp; /* incore directory inode */
  634. int i8count; /* parent inode is an 8-byte number */
  635. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  636. int size; /* directory size */
  637. trace_xfs_dir2_sf_create(args);
  638. dp = args->dp;
  639. ASSERT(dp != NULL);
  640. ASSERT(dp->i_d.di_size == 0);
  641. /*
  642. * If it's currently a zero-length extent file,
  643. * convert it to local format.
  644. */
  645. if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
  646. dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
  647. dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  648. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  649. dp->i_df.if_flags |= XFS_IFINLINE;
  650. }
  651. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  652. ASSERT(dp->i_df.if_bytes == 0);
  653. i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
  654. size = xfs_dir2_sf_hdr_size(i8count);
  655. /*
  656. * Make a buffer for the data.
  657. */
  658. xfs_idata_realloc(dp, size, XFS_DATA_FORK);
  659. /*
  660. * Fill in the header,
  661. */
  662. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  663. sfp->i8count = i8count;
  664. /*
  665. * Now can put in the inode number, since i8count is set.
  666. */
  667. dp->d_ops->sf_put_parent_ino(sfp, pino);
  668. sfp->count = 0;
  669. dp->i_d.di_size = size;
  670. xfs_dir2_sf_check(args);
  671. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  672. return 0;
  673. }
  674. /*
  675. * Lookup an entry in a shortform directory.
  676. * Returns EEXIST if found, ENOENT if not found.
  677. */
  678. int /* error */
  679. xfs_dir2_sf_lookup(
  680. xfs_da_args_t *args) /* operation arguments */
  681. {
  682. xfs_inode_t *dp; /* incore directory inode */
  683. int i; /* entry index */
  684. int error;
  685. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  686. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  687. enum xfs_dacmp cmp; /* comparison result */
  688. xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
  689. trace_xfs_dir2_sf_lookup(args);
  690. xfs_dir2_sf_check(args);
  691. dp = args->dp;
  692. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  693. /*
  694. * Bail out if the directory is way too short.
  695. */
  696. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  697. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  698. return XFS_ERROR(EIO);
  699. }
  700. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  701. ASSERT(dp->i_df.if_u1.if_data != NULL);
  702. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  703. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
  704. /*
  705. * Special case for .
  706. */
  707. if (args->namelen == 1 && args->name[0] == '.') {
  708. args->inumber = dp->i_ino;
  709. args->cmpresult = XFS_CMP_EXACT;
  710. args->filetype = XFS_DIR3_FT_DIR;
  711. return XFS_ERROR(EEXIST);
  712. }
  713. /*
  714. * Special case for ..
  715. */
  716. if (args->namelen == 2 &&
  717. args->name[0] == '.' && args->name[1] == '.') {
  718. args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
  719. args->cmpresult = XFS_CMP_EXACT;
  720. args->filetype = XFS_DIR3_FT_DIR;
  721. return XFS_ERROR(EEXIST);
  722. }
  723. /*
  724. * Loop over all the entries trying to match ours.
  725. */
  726. ci_sfep = NULL;
  727. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
  728. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
  729. /*
  730. * Compare name and if it's an exact match, return the inode
  731. * number. If it's the first case-insensitive match, store the
  732. * inode number and continue looking for an exact match.
  733. */
  734. cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
  735. sfep->namelen);
  736. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  737. args->cmpresult = cmp;
  738. args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
  739. args->filetype = dp->d_ops->sf_get_ftype(sfep);
  740. if (cmp == XFS_CMP_EXACT)
  741. return XFS_ERROR(EEXIST);
  742. ci_sfep = sfep;
  743. }
  744. }
  745. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  746. /*
  747. * Here, we can only be doing a lookup (not a rename or replace).
  748. * If a case-insensitive match was not found, return ENOENT.
  749. */
  750. if (!ci_sfep)
  751. return XFS_ERROR(ENOENT);
  752. /* otherwise process the CI match as required by the caller */
  753. error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
  754. return XFS_ERROR(error);
  755. }
  756. /*
  757. * Remove an entry from a shortform directory.
  758. */
  759. int /* error */
  760. xfs_dir2_sf_removename(
  761. xfs_da_args_t *args)
  762. {
  763. int byteoff; /* offset of removed entry */
  764. xfs_inode_t *dp; /* incore directory inode */
  765. int entsize; /* this entry's size */
  766. int i; /* shortform entry index */
  767. int newsize; /* new inode size */
  768. int oldsize; /* old inode size */
  769. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  770. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  771. trace_xfs_dir2_sf_removename(args);
  772. dp = args->dp;
  773. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  774. oldsize = (int)dp->i_d.di_size;
  775. /*
  776. * Bail out if the directory is way too short.
  777. */
  778. if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  779. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  780. return XFS_ERROR(EIO);
  781. }
  782. ASSERT(dp->i_df.if_bytes == oldsize);
  783. ASSERT(dp->i_df.if_u1.if_data != NULL);
  784. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  785. ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
  786. /*
  787. * Loop over the old directory entries.
  788. * Find the one we're deleting.
  789. */
  790. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
  791. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
  792. if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
  793. XFS_CMP_EXACT) {
  794. ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
  795. args->inumber);
  796. break;
  797. }
  798. }
  799. /*
  800. * Didn't find it.
  801. */
  802. if (i == sfp->count)
  803. return XFS_ERROR(ENOENT);
  804. /*
  805. * Calculate sizes.
  806. */
  807. byteoff = (int)((char *)sfep - (char *)sfp);
  808. entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
  809. newsize = oldsize - entsize;
  810. /*
  811. * Copy the part if any after the removed entry, sliding it down.
  812. */
  813. if (byteoff + entsize < oldsize)
  814. memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
  815. oldsize - (byteoff + entsize));
  816. /*
  817. * Fix up the header and file size.
  818. */
  819. sfp->count--;
  820. dp->i_d.di_size = newsize;
  821. /*
  822. * Reallocate, making it smaller.
  823. */
  824. xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
  825. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  826. #if XFS_BIG_INUMS
  827. /*
  828. * Are we changing inode number size?
  829. */
  830. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  831. if (sfp->i8count == 1)
  832. xfs_dir2_sf_toino4(args);
  833. else
  834. sfp->i8count--;
  835. }
  836. #endif
  837. xfs_dir2_sf_check(args);
  838. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  839. return 0;
  840. }
  841. /*
  842. * Replace the inode number of an entry in a shortform directory.
  843. */
  844. int /* error */
  845. xfs_dir2_sf_replace(
  846. xfs_da_args_t *args) /* operation arguments */
  847. {
  848. xfs_inode_t *dp; /* incore directory inode */
  849. int i; /* entry index */
  850. #if XFS_BIG_INUMS || defined(DEBUG)
  851. xfs_ino_t ino=0; /* entry old inode number */
  852. #endif
  853. #if XFS_BIG_INUMS
  854. int i8elevated; /* sf_toino8 set i8count=1 */
  855. #endif
  856. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  857. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  858. trace_xfs_dir2_sf_replace(args);
  859. dp = args->dp;
  860. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  861. /*
  862. * Bail out if the shortform directory is way too small.
  863. */
  864. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  865. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  866. return XFS_ERROR(EIO);
  867. }
  868. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  869. ASSERT(dp->i_df.if_u1.if_data != NULL);
  870. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  871. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
  872. #if XFS_BIG_INUMS
  873. /*
  874. * New inode number is large, and need to convert to 8-byte inodes.
  875. */
  876. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
  877. int error; /* error return value */
  878. int newsize; /* new inode size */
  879. newsize =
  880. dp->i_df.if_bytes +
  881. (sfp->count + 1) *
  882. ((uint)sizeof(xfs_dir2_ino8_t) -
  883. (uint)sizeof(xfs_dir2_ino4_t));
  884. /*
  885. * Won't fit as shortform, convert to block then do replace.
  886. */
  887. if (newsize > XFS_IFORK_DSIZE(dp)) {
  888. error = xfs_dir2_sf_to_block(args);
  889. if (error) {
  890. return error;
  891. }
  892. return xfs_dir2_block_replace(args);
  893. }
  894. /*
  895. * Still fits, convert to 8-byte now.
  896. */
  897. xfs_dir2_sf_toino8(args);
  898. i8elevated = 1;
  899. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  900. } else
  901. i8elevated = 0;
  902. #endif
  903. ASSERT(args->namelen != 1 || args->name[0] != '.');
  904. /*
  905. * Replace ..'s entry.
  906. */
  907. if (args->namelen == 2 &&
  908. args->name[0] == '.' && args->name[1] == '.') {
  909. #if XFS_BIG_INUMS || defined(DEBUG)
  910. ino = dp->d_ops->sf_get_parent_ino(sfp);
  911. ASSERT(args->inumber != ino);
  912. #endif
  913. dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
  914. }
  915. /*
  916. * Normal entry, look for the name.
  917. */
  918. else {
  919. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
  920. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
  921. if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
  922. XFS_CMP_EXACT) {
  923. #if XFS_BIG_INUMS || defined(DEBUG)
  924. ino = dp->d_ops->sf_get_ino(sfp, sfep);
  925. ASSERT(args->inumber != ino);
  926. #endif
  927. dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
  928. dp->d_ops->sf_put_ftype(sfep, args->filetype);
  929. break;
  930. }
  931. }
  932. /*
  933. * Didn't find it.
  934. */
  935. if (i == sfp->count) {
  936. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  937. #if XFS_BIG_INUMS
  938. if (i8elevated)
  939. xfs_dir2_sf_toino4(args);
  940. #endif
  941. return XFS_ERROR(ENOENT);
  942. }
  943. }
  944. #if XFS_BIG_INUMS
  945. /*
  946. * See if the old number was large, the new number is small.
  947. */
  948. if (ino > XFS_DIR2_MAX_SHORT_INUM &&
  949. args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
  950. /*
  951. * And the old count was one, so need to convert to small.
  952. */
  953. if (sfp->i8count == 1)
  954. xfs_dir2_sf_toino4(args);
  955. else
  956. sfp->i8count--;
  957. }
  958. /*
  959. * See if the old number was small, the new number is large.
  960. */
  961. if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
  962. args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  963. /*
  964. * add to the i8count unless we just converted to 8-byte
  965. * inodes (which does an implied i8count = 1)
  966. */
  967. ASSERT(sfp->i8count != 0);
  968. if (!i8elevated)
  969. sfp->i8count++;
  970. }
  971. #endif
  972. xfs_dir2_sf_check(args);
  973. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
  974. return 0;
  975. }
  976. #if XFS_BIG_INUMS
  977. /*
  978. * Convert from 8-byte inode numbers to 4-byte inode numbers.
  979. * The last 8-byte inode number is gone, but the count is still 1.
  980. */
  981. static void
  982. xfs_dir2_sf_toino4(
  983. xfs_da_args_t *args) /* operation arguments */
  984. {
  985. char *buf; /* old dir's buffer */
  986. xfs_inode_t *dp; /* incore directory inode */
  987. int i; /* entry index */
  988. int newsize; /* new inode size */
  989. xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
  990. xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
  991. int oldsize; /* old inode size */
  992. xfs_dir2_sf_entry_t *sfep; /* new sf entry */
  993. xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
  994. struct xfs_mount *mp;
  995. trace_xfs_dir2_sf_toino4(args);
  996. dp = args->dp;
  997. mp = dp->i_mount;
  998. /*
  999. * Copy the old directory to the buffer.
  1000. * Then nuke it from the inode, and add the new buffer to the inode.
  1001. * Don't want xfs_idata_realloc copying the data here.
  1002. */
  1003. oldsize = dp->i_df.if_bytes;
  1004. buf = kmem_alloc(oldsize, KM_SLEEP);
  1005. oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  1006. ASSERT(oldsfp->i8count == 1);
  1007. memcpy(buf, oldsfp, oldsize);
  1008. /*
  1009. * Compute the new inode size.
  1010. */
  1011. newsize =
  1012. oldsize -
  1013. (oldsfp->count + 1) *
  1014. ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
  1015. xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  1016. xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  1017. /*
  1018. * Reset our pointers, the data has moved.
  1019. */
  1020. oldsfp = (xfs_dir2_sf_hdr_t *)buf;
  1021. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  1022. /*
  1023. * Fill in the new header.
  1024. */
  1025. sfp->count = oldsfp->count;
  1026. sfp->i8count = 0;
  1027. dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
  1028. /*
  1029. * Copy the entries field by field.
  1030. */
  1031. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  1032. oldsfep = xfs_dir2_sf_firstentry(oldsfp);
  1033. i < sfp->count;
  1034. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
  1035. oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
  1036. sfep->namelen = oldsfep->namelen;
  1037. sfep->offset = oldsfep->offset;
  1038. memcpy(sfep->name, oldsfep->name, sfep->namelen);
  1039. dp->d_ops->sf_put_ino(sfp, sfep,
  1040. dp->d_ops->sf_get_ino(oldsfp, oldsfep));
  1041. dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
  1042. }
  1043. /*
  1044. * Clean up the inode.
  1045. */
  1046. kmem_free(buf);
  1047. dp->i_d.di_size = newsize;
  1048. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1049. }
  1050. /*
  1051. * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
  1052. * The new entry w/ an 8-byte inode number is not there yet; we leave with
  1053. * i8count set to 1, but no corresponding 8-byte entry.
  1054. */
  1055. static void
  1056. xfs_dir2_sf_toino8(
  1057. xfs_da_args_t *args) /* operation arguments */
  1058. {
  1059. char *buf; /* old dir's buffer */
  1060. xfs_inode_t *dp; /* incore directory inode */
  1061. int i; /* entry index */
  1062. int newsize; /* new inode size */
  1063. xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
  1064. xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
  1065. int oldsize; /* old inode size */
  1066. xfs_dir2_sf_entry_t *sfep; /* new sf entry */
  1067. xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
  1068. struct xfs_mount *mp;
  1069. trace_xfs_dir2_sf_toino8(args);
  1070. dp = args->dp;
  1071. mp = dp->i_mount;
  1072. /*
  1073. * Copy the old directory to the buffer.
  1074. * Then nuke it from the inode, and add the new buffer to the inode.
  1075. * Don't want xfs_idata_realloc copying the data here.
  1076. */
  1077. oldsize = dp->i_df.if_bytes;
  1078. buf = kmem_alloc(oldsize, KM_SLEEP);
  1079. oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  1080. ASSERT(oldsfp->i8count == 0);
  1081. memcpy(buf, oldsfp, oldsize);
  1082. /*
  1083. * Compute the new inode size (nb: entry count + 1 for parent)
  1084. */
  1085. newsize =
  1086. oldsize +
  1087. (oldsfp->count + 1) *
  1088. ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
  1089. xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  1090. xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  1091. /*
  1092. * Reset our pointers, the data has moved.
  1093. */
  1094. oldsfp = (xfs_dir2_sf_hdr_t *)buf;
  1095. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  1096. /*
  1097. * Fill in the new header.
  1098. */
  1099. sfp->count = oldsfp->count;
  1100. sfp->i8count = 1;
  1101. dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
  1102. /*
  1103. * Copy the entries field by field.
  1104. */
  1105. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  1106. oldsfep = xfs_dir2_sf_firstentry(oldsfp);
  1107. i < sfp->count;
  1108. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
  1109. oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
  1110. sfep->namelen = oldsfep->namelen;
  1111. sfep->offset = oldsfep->offset;
  1112. memcpy(sfep->name, oldsfep->name, sfep->namelen);
  1113. dp->d_ops->sf_put_ino(sfp, sfep,
  1114. dp->d_ops->sf_get_ino(oldsfp, oldsfep));
  1115. dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
  1116. }
  1117. /*
  1118. * Clean up the inode.
  1119. */
  1120. kmem_free(buf);
  1121. dp->i_d.di_size = newsize;
  1122. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1123. }
  1124. #endif /* XFS_BIG_INUMS */