xfs_dir2_sf.c 33 KB

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