xfs_dir2_sf.c 34 KB

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