xfs_dir2_sf.c 33 KB

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