xfs_dir2_sf.c 37 KB

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