xfs_dir2_sf.c 37 KB

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