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