xfs_dir2_sf.c 35 KB

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