xfs_dir2_sf.c 34 KB

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