xfs_dir2_sf.c 33 KB

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