xfs_dir2_sf.c 35 KB

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