xfs_dir2_sf.c 37 KB

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