xfs_dir2_sf.c 34 KB

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