xfs_dir2_sf.c 37 KB

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