xfs_dir2_sf.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. /* Verify the consistency of an inline directory. */
  594. int
  595. xfs_dir2_sf_verify(
  596. struct xfs_mount *mp,
  597. struct xfs_dir2_sf_hdr *sfp,
  598. int size)
  599. {
  600. struct xfs_dir2_sf_entry *sfep;
  601. struct xfs_dir2_sf_entry *next_sfep;
  602. char *endp;
  603. const struct xfs_dir_ops *dops;
  604. xfs_ino_t ino;
  605. int i;
  606. int i8count;
  607. int offset;
  608. __uint8_t filetype;
  609. dops = xfs_dir_get_ops(mp, NULL);
  610. /*
  611. * Give up if the directory is way too short.
  612. */
  613. XFS_WANT_CORRUPTED_RETURN(mp, size >
  614. offsetof(struct xfs_dir2_sf_hdr, parent));
  615. XFS_WANT_CORRUPTED_RETURN(mp, size >=
  616. xfs_dir2_sf_hdr_size(sfp->i8count));
  617. endp = (char *)sfp + size;
  618. /* Check .. entry */
  619. ino = dops->sf_get_parent_ino(sfp);
  620. i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
  621. XFS_WANT_CORRUPTED_RETURN(mp, !xfs_dir_ino_validate(mp, ino));
  622. offset = dops->data_first_offset;
  623. /* Check all reported entries */
  624. sfep = xfs_dir2_sf_firstentry(sfp);
  625. for (i = 0; i < sfp->count; i++) {
  626. /*
  627. * struct xfs_dir2_sf_entry has a variable length.
  628. * Check the fixed-offset parts of the structure are
  629. * within the data buffer.
  630. */
  631. XFS_WANT_CORRUPTED_RETURN(mp,
  632. ((char *)sfep + sizeof(*sfep)) < endp);
  633. /* Don't allow names with known bad length. */
  634. XFS_WANT_CORRUPTED_RETURN(mp, sfep->namelen > 0);
  635. XFS_WANT_CORRUPTED_RETURN(mp, sfep->namelen < MAXNAMELEN);
  636. /*
  637. * Check that the variable-length part of the structure is
  638. * within the data buffer. The next entry starts after the
  639. * name component, so nextentry is an acceptable test.
  640. */
  641. next_sfep = dops->sf_nextentry(sfp, sfep);
  642. XFS_WANT_CORRUPTED_RETURN(mp, endp >= (char *)next_sfep);
  643. /* Check that the offsets always increase. */
  644. XFS_WANT_CORRUPTED_RETURN(mp,
  645. xfs_dir2_sf_get_offset(sfep) >= offset);
  646. /* Check the inode number. */
  647. ino = dops->sf_get_ino(sfp, sfep);
  648. i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
  649. XFS_WANT_CORRUPTED_RETURN(mp, !xfs_dir_ino_validate(mp, ino));
  650. /* Check the file type. */
  651. filetype = dops->sf_get_ftype(sfep);
  652. XFS_WANT_CORRUPTED_RETURN(mp, filetype < XFS_DIR3_FT_MAX);
  653. offset = xfs_dir2_sf_get_offset(sfep) +
  654. dops->data_entsize(sfep->namelen);
  655. sfep = next_sfep;
  656. }
  657. XFS_WANT_CORRUPTED_RETURN(mp, i8count == sfp->i8count);
  658. XFS_WANT_CORRUPTED_RETURN(mp, (void *)sfep == (void *)endp);
  659. /* Make sure this whole thing ought to be in local format. */
  660. XFS_WANT_CORRUPTED_RETURN(mp, offset +
  661. (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
  662. (uint)sizeof(xfs_dir2_block_tail_t) <= mp->m_dir_geo->blksize);
  663. return 0;
  664. }
  665. /*
  666. * Create a new (shortform) directory.
  667. */
  668. int /* error, always 0 */
  669. xfs_dir2_sf_create(
  670. xfs_da_args_t *args, /* operation arguments */
  671. xfs_ino_t pino) /* parent inode number */
  672. {
  673. xfs_inode_t *dp; /* incore directory inode */
  674. int i8count; /* parent inode is an 8-byte number */
  675. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  676. int size; /* directory size */
  677. trace_xfs_dir2_sf_create(args);
  678. dp = args->dp;
  679. ASSERT(dp != NULL);
  680. ASSERT(dp->i_d.di_size == 0);
  681. /*
  682. * If it's currently a zero-length extent file,
  683. * convert it to local format.
  684. */
  685. if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
  686. dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
  687. dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  688. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  689. dp->i_df.if_flags |= XFS_IFINLINE;
  690. }
  691. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  692. ASSERT(dp->i_df.if_bytes == 0);
  693. i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
  694. size = xfs_dir2_sf_hdr_size(i8count);
  695. /*
  696. * Make a buffer for the data.
  697. */
  698. xfs_idata_realloc(dp, size, XFS_DATA_FORK);
  699. /*
  700. * Fill in the header,
  701. */
  702. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  703. sfp->i8count = i8count;
  704. /*
  705. * Now can put in the inode number, since i8count is set.
  706. */
  707. dp->d_ops->sf_put_parent_ino(sfp, pino);
  708. sfp->count = 0;
  709. dp->i_d.di_size = size;
  710. xfs_dir2_sf_check(args);
  711. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  712. return 0;
  713. }
  714. /*
  715. * Lookup an entry in a shortform directory.
  716. * Returns EEXIST if found, ENOENT if not found.
  717. */
  718. int /* error */
  719. xfs_dir2_sf_lookup(
  720. xfs_da_args_t *args) /* operation arguments */
  721. {
  722. xfs_inode_t *dp; /* incore directory inode */
  723. int i; /* entry index */
  724. int error;
  725. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  726. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  727. enum xfs_dacmp cmp; /* comparison result */
  728. xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
  729. trace_xfs_dir2_sf_lookup(args);
  730. xfs_dir2_sf_check(args);
  731. dp = args->dp;
  732. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  733. /*
  734. * Bail out if the directory is way too short.
  735. */
  736. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  737. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  738. return -EIO;
  739. }
  740. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  741. ASSERT(dp->i_df.if_u1.if_data != NULL);
  742. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  743. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
  744. /*
  745. * Special case for .
  746. */
  747. if (args->namelen == 1 && args->name[0] == '.') {
  748. args->inumber = dp->i_ino;
  749. args->cmpresult = XFS_CMP_EXACT;
  750. args->filetype = XFS_DIR3_FT_DIR;
  751. return -EEXIST;
  752. }
  753. /*
  754. * Special case for ..
  755. */
  756. if (args->namelen == 2 &&
  757. args->name[0] == '.' && args->name[1] == '.') {
  758. args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
  759. args->cmpresult = XFS_CMP_EXACT;
  760. args->filetype = XFS_DIR3_FT_DIR;
  761. return -EEXIST;
  762. }
  763. /*
  764. * Loop over all the entries trying to match ours.
  765. */
  766. ci_sfep = NULL;
  767. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
  768. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
  769. /*
  770. * Compare name and if it's an exact match, return the inode
  771. * number. If it's the first case-insensitive match, store the
  772. * inode number and continue looking for an exact match.
  773. */
  774. cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
  775. sfep->namelen);
  776. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  777. args->cmpresult = cmp;
  778. args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
  779. args->filetype = dp->d_ops->sf_get_ftype(sfep);
  780. if (cmp == XFS_CMP_EXACT)
  781. return -EEXIST;
  782. ci_sfep = sfep;
  783. }
  784. }
  785. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  786. /*
  787. * Here, we can only be doing a lookup (not a rename or replace).
  788. * If a case-insensitive match was not found, return -ENOENT.
  789. */
  790. if (!ci_sfep)
  791. return -ENOENT;
  792. /* otherwise process the CI match as required by the caller */
  793. error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
  794. return error;
  795. }
  796. /*
  797. * Remove an entry from a shortform directory.
  798. */
  799. int /* error */
  800. xfs_dir2_sf_removename(
  801. xfs_da_args_t *args)
  802. {
  803. int byteoff; /* offset of removed entry */
  804. xfs_inode_t *dp; /* incore directory inode */
  805. int entsize; /* this entry's size */
  806. int i; /* shortform entry index */
  807. int newsize; /* new inode size */
  808. int oldsize; /* old inode size */
  809. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  810. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  811. trace_xfs_dir2_sf_removename(args);
  812. dp = args->dp;
  813. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  814. oldsize = (int)dp->i_d.di_size;
  815. /*
  816. * Bail out if the directory is way too short.
  817. */
  818. if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  819. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  820. return -EIO;
  821. }
  822. ASSERT(dp->i_df.if_bytes == oldsize);
  823. ASSERT(dp->i_df.if_u1.if_data != NULL);
  824. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  825. ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
  826. /*
  827. * Loop over the old directory entries.
  828. * Find the one we're deleting.
  829. */
  830. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
  831. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
  832. if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
  833. XFS_CMP_EXACT) {
  834. ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
  835. args->inumber);
  836. break;
  837. }
  838. }
  839. /*
  840. * Didn't find it.
  841. */
  842. if (i == sfp->count)
  843. return -ENOENT;
  844. /*
  845. * Calculate sizes.
  846. */
  847. byteoff = (int)((char *)sfep - (char *)sfp);
  848. entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
  849. newsize = oldsize - entsize;
  850. /*
  851. * Copy the part if any after the removed entry, sliding it down.
  852. */
  853. if (byteoff + entsize < oldsize)
  854. memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
  855. oldsize - (byteoff + entsize));
  856. /*
  857. * Fix up the header and file size.
  858. */
  859. sfp->count--;
  860. dp->i_d.di_size = newsize;
  861. /*
  862. * Reallocate, making it smaller.
  863. */
  864. xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
  865. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  866. /*
  867. * Are we changing inode number size?
  868. */
  869. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  870. if (sfp->i8count == 1)
  871. xfs_dir2_sf_toino4(args);
  872. else
  873. sfp->i8count--;
  874. }
  875. xfs_dir2_sf_check(args);
  876. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  877. return 0;
  878. }
  879. /*
  880. * Replace the inode number of an entry in a shortform directory.
  881. */
  882. int /* error */
  883. xfs_dir2_sf_replace(
  884. xfs_da_args_t *args) /* operation arguments */
  885. {
  886. xfs_inode_t *dp; /* incore directory inode */
  887. int i; /* entry index */
  888. xfs_ino_t ino=0; /* entry old inode number */
  889. int i8elevated; /* sf_toino8 set i8count=1 */
  890. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  891. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  892. trace_xfs_dir2_sf_replace(args);
  893. dp = args->dp;
  894. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  895. /*
  896. * Bail out if the shortform directory is way too small.
  897. */
  898. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  899. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  900. return -EIO;
  901. }
  902. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  903. ASSERT(dp->i_df.if_u1.if_data != NULL);
  904. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  905. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
  906. /*
  907. * New inode number is large, and need to convert to 8-byte inodes.
  908. */
  909. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
  910. int error; /* error return value */
  911. int newsize; /* new inode size */
  912. newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
  913. /*
  914. * Won't fit as shortform, convert to block then do replace.
  915. */
  916. if (newsize > XFS_IFORK_DSIZE(dp)) {
  917. error = xfs_dir2_sf_to_block(args);
  918. if (error) {
  919. return error;
  920. }
  921. return xfs_dir2_block_replace(args);
  922. }
  923. /*
  924. * Still fits, convert to 8-byte now.
  925. */
  926. xfs_dir2_sf_toino8(args);
  927. i8elevated = 1;
  928. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  929. } else
  930. i8elevated = 0;
  931. ASSERT(args->namelen != 1 || args->name[0] != '.');
  932. /*
  933. * Replace ..'s entry.
  934. */
  935. if (args->namelen == 2 &&
  936. args->name[0] == '.' && args->name[1] == '.') {
  937. ino = dp->d_ops->sf_get_parent_ino(sfp);
  938. ASSERT(args->inumber != ino);
  939. dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
  940. }
  941. /*
  942. * Normal entry, look for the name.
  943. */
  944. else {
  945. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
  946. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
  947. if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
  948. XFS_CMP_EXACT) {
  949. ino = dp->d_ops->sf_get_ino(sfp, sfep);
  950. ASSERT(args->inumber != ino);
  951. dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
  952. dp->d_ops->sf_put_ftype(sfep, args->filetype);
  953. break;
  954. }
  955. }
  956. /*
  957. * Didn't find it.
  958. */
  959. if (i == sfp->count) {
  960. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  961. if (i8elevated)
  962. xfs_dir2_sf_toino4(args);
  963. return -ENOENT;
  964. }
  965. }
  966. /*
  967. * See if the old number was large, the new number is small.
  968. */
  969. if (ino > XFS_DIR2_MAX_SHORT_INUM &&
  970. args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
  971. /*
  972. * And the old count was one, so need to convert to small.
  973. */
  974. if (sfp->i8count == 1)
  975. xfs_dir2_sf_toino4(args);
  976. else
  977. sfp->i8count--;
  978. }
  979. /*
  980. * See if the old number was small, the new number is large.
  981. */
  982. if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
  983. args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  984. /*
  985. * add to the i8count unless we just converted to 8-byte
  986. * inodes (which does an implied i8count = 1)
  987. */
  988. ASSERT(sfp->i8count != 0);
  989. if (!i8elevated)
  990. sfp->i8count++;
  991. }
  992. xfs_dir2_sf_check(args);
  993. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
  994. return 0;
  995. }
  996. /*
  997. * Convert from 8-byte inode numbers to 4-byte inode numbers.
  998. * The last 8-byte inode number is gone, but the count is still 1.
  999. */
  1000. static void
  1001. xfs_dir2_sf_toino4(
  1002. xfs_da_args_t *args) /* operation arguments */
  1003. {
  1004. char *buf; /* old dir's buffer */
  1005. xfs_inode_t *dp; /* incore directory inode */
  1006. int i; /* entry index */
  1007. int newsize; /* new inode size */
  1008. xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
  1009. xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
  1010. int oldsize; /* old inode size */
  1011. xfs_dir2_sf_entry_t *sfep; /* new sf entry */
  1012. xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
  1013. trace_xfs_dir2_sf_toino4(args);
  1014. dp = args->dp;
  1015. /*
  1016. * Copy the old directory to the buffer.
  1017. * Then nuke it from the inode, and add the new buffer to the inode.
  1018. * Don't want xfs_idata_realloc copying the data here.
  1019. */
  1020. oldsize = dp->i_df.if_bytes;
  1021. buf = kmem_alloc(oldsize, KM_SLEEP);
  1022. oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  1023. ASSERT(oldsfp->i8count == 1);
  1024. memcpy(buf, oldsfp, oldsize);
  1025. /*
  1026. * Compute the new inode size.
  1027. */
  1028. newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
  1029. xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  1030. xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  1031. /*
  1032. * Reset our pointers, the data has moved.
  1033. */
  1034. oldsfp = (xfs_dir2_sf_hdr_t *)buf;
  1035. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  1036. /*
  1037. * Fill in the new header.
  1038. */
  1039. sfp->count = oldsfp->count;
  1040. sfp->i8count = 0;
  1041. dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
  1042. /*
  1043. * Copy the entries field by field.
  1044. */
  1045. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  1046. oldsfep = xfs_dir2_sf_firstentry(oldsfp);
  1047. i < sfp->count;
  1048. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
  1049. oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
  1050. sfep->namelen = oldsfep->namelen;
  1051. memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
  1052. memcpy(sfep->name, oldsfep->name, sfep->namelen);
  1053. dp->d_ops->sf_put_ino(sfp, sfep,
  1054. dp->d_ops->sf_get_ino(oldsfp, oldsfep));
  1055. dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
  1056. }
  1057. /*
  1058. * Clean up the inode.
  1059. */
  1060. kmem_free(buf);
  1061. dp->i_d.di_size = newsize;
  1062. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1063. }
  1064. /*
  1065. * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
  1066. * The new entry w/ an 8-byte inode number is not there yet; we leave with
  1067. * i8count set to 1, but no corresponding 8-byte entry.
  1068. */
  1069. static void
  1070. xfs_dir2_sf_toino8(
  1071. xfs_da_args_t *args) /* operation arguments */
  1072. {
  1073. char *buf; /* old dir's buffer */
  1074. xfs_inode_t *dp; /* incore directory inode */
  1075. int i; /* entry index */
  1076. int newsize; /* new inode size */
  1077. xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
  1078. xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
  1079. int oldsize; /* old inode size */
  1080. xfs_dir2_sf_entry_t *sfep; /* new sf entry */
  1081. xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
  1082. trace_xfs_dir2_sf_toino8(args);
  1083. dp = args->dp;
  1084. /*
  1085. * Copy the old directory to the buffer.
  1086. * Then nuke it from the inode, and add the new buffer to the inode.
  1087. * Don't want xfs_idata_realloc copying the data here.
  1088. */
  1089. oldsize = dp->i_df.if_bytes;
  1090. buf = kmem_alloc(oldsize, KM_SLEEP);
  1091. oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  1092. ASSERT(oldsfp->i8count == 0);
  1093. memcpy(buf, oldsfp, oldsize);
  1094. /*
  1095. * Compute the new inode size (nb: entry count + 1 for parent)
  1096. */
  1097. newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
  1098. xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  1099. xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  1100. /*
  1101. * Reset our pointers, the data has moved.
  1102. */
  1103. oldsfp = (xfs_dir2_sf_hdr_t *)buf;
  1104. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  1105. /*
  1106. * Fill in the new header.
  1107. */
  1108. sfp->count = oldsfp->count;
  1109. sfp->i8count = 1;
  1110. dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
  1111. /*
  1112. * Copy the entries field by field.
  1113. */
  1114. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  1115. oldsfep = xfs_dir2_sf_firstentry(oldsfp);
  1116. i < sfp->count;
  1117. i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
  1118. oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
  1119. sfep->namelen = oldsfep->namelen;
  1120. memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
  1121. memcpy(sfep->name, oldsfep->name, sfep->namelen);
  1122. dp->d_ops->sf_put_ino(sfp, sfep,
  1123. dp->d_ops->sf_get_ino(oldsfp, oldsfep));
  1124. dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
  1125. }
  1126. /*
  1127. * Clean up the inode.
  1128. */
  1129. kmem_free(buf);
  1130. dp->i_d.di_size = newsize;
  1131. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1132. }