xfs_dir2_sf.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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, mp->m_dirblksize);
  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->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->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, old_isize);
  500. dp->i_d.di_size = new_isize;
  501. xfs_dir2_sf_check(args);
  502. }
  503. /*
  504. * Decide if the new entry will fit at all.
  505. * If it will fit, pick between adding the new entry to the end (easy)
  506. * or somewhere else (hard).
  507. * Return 0 (won't fit), 1 (easy), 2 (hard).
  508. */
  509. /*ARGSUSED*/
  510. static int /* pick result */
  511. xfs_dir2_sf_addname_pick(
  512. xfs_da_args_t *args, /* operation arguments */
  513. int objchange, /* inode # size changes */
  514. xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
  515. xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
  516. {
  517. xfs_inode_t *dp; /* incore directory inode */
  518. int holefit; /* found hole it will fit in */
  519. int i; /* entry number */
  520. xfs_mount_t *mp; /* filesystem mount point */
  521. xfs_dir2_data_aoff_t offset; /* data block offset */
  522. xfs_dir2_sf_entry_t *sfep; /* shortform entry */
  523. xfs_dir2_sf_t *sfp; /* shortform structure */
  524. int size; /* entry's data size */
  525. int used; /* data bytes used */
  526. dp = args->dp;
  527. mp = dp->i_mount;
  528. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  529. size = xfs_dir2_data_entsize(args->namelen);
  530. offset = XFS_DIR2_DATA_FIRST_OFFSET;
  531. sfep = xfs_dir2_sf_firstentry(sfp);
  532. holefit = 0;
  533. /*
  534. * Loop over sf entries.
  535. * Keep track of data offset and whether we've seen a place
  536. * to insert the new entry.
  537. */
  538. for (i = 0; i < sfp->hdr.count; i++) {
  539. if (!holefit)
  540. holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
  541. offset = xfs_dir2_sf_get_offset(sfep) +
  542. xfs_dir2_data_entsize(sfep->namelen);
  543. sfep = xfs_dir2_sf_nextentry(sfp, sfep);
  544. }
  545. /*
  546. * Calculate data bytes used excluding the new entry, if this
  547. * was a data block (block form directory).
  548. */
  549. used = offset +
  550. (sfp->hdr.count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
  551. (uint)sizeof(xfs_dir2_block_tail_t);
  552. /*
  553. * If it won't fit in a block form then we can't insert it,
  554. * we'll go back, convert to block, then try the insert and convert
  555. * to leaf.
  556. */
  557. if (used + (holefit ? 0 : size) > mp->m_dirblksize)
  558. return 0;
  559. /*
  560. * If changing the inode number size, do it the hard way.
  561. */
  562. #if XFS_BIG_INUMS
  563. if (objchange) {
  564. return 2;
  565. }
  566. #else
  567. ASSERT(objchange == 0);
  568. #endif
  569. /*
  570. * If it won't fit at the end then do it the hard way (use the hole).
  571. */
  572. if (used + size > mp->m_dirblksize)
  573. return 2;
  574. /*
  575. * Do it the easy way.
  576. */
  577. *sfepp = sfep;
  578. *offsetp = offset;
  579. return 1;
  580. }
  581. #ifdef DEBUG
  582. /*
  583. * Check consistency of shortform directory, assert if bad.
  584. */
  585. static void
  586. xfs_dir2_sf_check(
  587. xfs_da_args_t *args) /* operation arguments */
  588. {
  589. xfs_inode_t *dp; /* incore directory inode */
  590. int i; /* entry number */
  591. int i8count; /* number of big inode#s */
  592. xfs_ino_t ino; /* entry inode number */
  593. int offset; /* data offset */
  594. xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
  595. xfs_dir2_sf_t *sfp; /* shortform structure */
  596. dp = args->dp;
  597. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  598. offset = XFS_DIR2_DATA_FIRST_OFFSET;
  599. ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
  600. i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
  601. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
  602. i < sfp->hdr.count;
  603. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  604. ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
  605. ino = xfs_dir2_sf_get_inumber(sfp, xfs_dir2_sf_inumberp(sfep));
  606. i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
  607. offset =
  608. xfs_dir2_sf_get_offset(sfep) +
  609. xfs_dir2_data_entsize(sfep->namelen);
  610. }
  611. ASSERT(i8count == sfp->hdr.i8count);
  612. ASSERT(XFS_BIG_INUMS || i8count == 0);
  613. ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
  614. ASSERT(offset +
  615. (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
  616. (uint)sizeof(xfs_dir2_block_tail_t) <=
  617. dp->i_mount->m_dirblksize);
  618. }
  619. #endif /* DEBUG */
  620. /*
  621. * Create a new (shortform) directory.
  622. */
  623. int /* error, always 0 */
  624. xfs_dir2_sf_create(
  625. xfs_da_args_t *args, /* operation arguments */
  626. xfs_ino_t pino) /* parent inode number */
  627. {
  628. xfs_inode_t *dp; /* incore directory inode */
  629. int i8count; /* parent inode is an 8-byte number */
  630. xfs_dir2_sf_t *sfp; /* shortform structure */
  631. int size; /* directory size */
  632. xfs_dir2_trace_args_i("sf_create", args, pino);
  633. dp = args->dp;
  634. ASSERT(dp != NULL);
  635. ASSERT(dp->i_d.di_size == 0);
  636. /*
  637. * If it's currently a zero-length extent file,
  638. * convert it to local format.
  639. */
  640. if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
  641. dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
  642. dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  643. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  644. dp->i_df.if_flags |= XFS_IFINLINE;
  645. }
  646. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  647. ASSERT(dp->i_df.if_bytes == 0);
  648. i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
  649. size = xfs_dir2_sf_hdr_size(i8count);
  650. /*
  651. * Make a buffer for the data.
  652. */
  653. xfs_idata_realloc(dp, size, XFS_DATA_FORK);
  654. /*
  655. * Fill in the header,
  656. */
  657. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  658. sfp->hdr.i8count = i8count;
  659. /*
  660. * Now can put in the inode number, since i8count is set.
  661. */
  662. xfs_dir2_sf_put_inumber(sfp, &pino, &sfp->hdr.parent);
  663. sfp->hdr.count = 0;
  664. dp->i_d.di_size = size;
  665. xfs_dir2_sf_check(args);
  666. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  667. return 0;
  668. }
  669. int /* error */
  670. xfs_dir2_sf_getdents(
  671. xfs_inode_t *dp, /* incore directory inode */
  672. void *dirent,
  673. xfs_off_t *offset,
  674. filldir_t filldir)
  675. {
  676. int i; /* shortform entry number */
  677. xfs_mount_t *mp; /* filesystem mount point */
  678. xfs_dir2_dataptr_t off; /* current entry's offset */
  679. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  680. xfs_dir2_sf_t *sfp; /* shortform structure */
  681. xfs_dir2_dataptr_t dot_offset;
  682. xfs_dir2_dataptr_t dotdot_offset;
  683. xfs_ino_t ino;
  684. mp = dp->i_mount;
  685. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  686. /*
  687. * Give up if the directory is way too short.
  688. */
  689. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  690. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  691. return XFS_ERROR(EIO);
  692. }
  693. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  694. ASSERT(dp->i_df.if_u1.if_data != NULL);
  695. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  696. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
  697. /*
  698. * If the block number in the offset is out of range, we're done.
  699. */
  700. if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
  701. return 0;
  702. /*
  703. * Precalculate offsets for . and .. as we will always need them.
  704. *
  705. * XXX(hch): the second argument is sometimes 0 and sometimes
  706. * mp->m_dirdatablk.
  707. */
  708. dot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  709. XFS_DIR2_DATA_DOT_OFFSET);
  710. dotdot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  711. XFS_DIR2_DATA_DOTDOT_OFFSET);
  712. /*
  713. * Put . entry unless we're starting past it.
  714. */
  715. if (*offset <= dot_offset) {
  716. ino = dp->i_ino;
  717. #if XFS_BIG_INUMS
  718. ino += mp->m_inoadd;
  719. #endif
  720. if (filldir(dirent, ".", 1, dot_offset, ino, DT_DIR)) {
  721. *offset = dot_offset;
  722. return 0;
  723. }
  724. }
  725. /*
  726. * Put .. entry unless we're starting past it.
  727. */
  728. if (*offset <= dotdot_offset) {
  729. ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
  730. #if XFS_BIG_INUMS
  731. ino += mp->m_inoadd;
  732. #endif
  733. if (filldir(dirent, "..", 2, dotdot_offset, ino, DT_DIR)) {
  734. *offset = dotdot_offset;
  735. return 0;
  736. }
  737. }
  738. /*
  739. * Loop while there are more entries and put'ing works.
  740. */
  741. sfep = xfs_dir2_sf_firstentry(sfp);
  742. for (i = 0; i < sfp->hdr.count; i++) {
  743. off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  744. xfs_dir2_sf_get_offset(sfep));
  745. if (*offset > off) {
  746. sfep = xfs_dir2_sf_nextentry(sfp, sfep);
  747. continue;
  748. }
  749. ino = xfs_dir2_sf_get_inumber(sfp, xfs_dir2_sf_inumberp(sfep));
  750. #if XFS_BIG_INUMS
  751. ino += mp->m_inoadd;
  752. #endif
  753. if (filldir(dirent, sfep->name, sfep->namelen,
  754. off, ino, DT_UNKNOWN)) {
  755. *offset = off;
  756. return 0;
  757. }
  758. sfep = xfs_dir2_sf_nextentry(sfp, sfep);
  759. }
  760. *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0);
  761. return 0;
  762. }
  763. /*
  764. * Lookup an entry in a shortform directory.
  765. * Returns EEXIST if found, ENOENT if not found.
  766. */
  767. int /* error */
  768. xfs_dir2_sf_lookup(
  769. xfs_da_args_t *args) /* operation arguments */
  770. {
  771. xfs_inode_t *dp; /* incore directory inode */
  772. int i; /* entry index */
  773. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  774. xfs_dir2_sf_t *sfp; /* shortform structure */
  775. xfs_dir2_trace_args("sf_lookup", args);
  776. xfs_dir2_sf_check(args);
  777. dp = args->dp;
  778. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  779. /*
  780. * Bail out if the directory is way too short.
  781. */
  782. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  783. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  784. return XFS_ERROR(EIO);
  785. }
  786. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  787. ASSERT(dp->i_df.if_u1.if_data != NULL);
  788. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  789. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
  790. /*
  791. * Special case for .
  792. */
  793. if (args->namelen == 1 && args->name[0] == '.') {
  794. args->inumber = dp->i_ino;
  795. return XFS_ERROR(EEXIST);
  796. }
  797. /*
  798. * Special case for ..
  799. */
  800. if (args->namelen == 2 &&
  801. args->name[0] == '.' && args->name[1] == '.') {
  802. args->inumber = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
  803. return XFS_ERROR(EEXIST);
  804. }
  805. /*
  806. * Loop over all the entries trying to match ours.
  807. */
  808. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
  809. i < sfp->hdr.count;
  810. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  811. if (sfep->namelen == args->namelen &&
  812. sfep->name[0] == args->name[0] &&
  813. memcmp(args->name, sfep->name, args->namelen) == 0) {
  814. args->inumber =
  815. xfs_dir2_sf_get_inumber(sfp,
  816. xfs_dir2_sf_inumberp(sfep));
  817. return XFS_ERROR(EEXIST);
  818. }
  819. }
  820. /*
  821. * Didn't find it.
  822. */
  823. ASSERT(args->oknoent);
  824. return XFS_ERROR(ENOENT);
  825. }
  826. /*
  827. * Remove an entry from a shortform directory.
  828. */
  829. int /* error */
  830. xfs_dir2_sf_removename(
  831. xfs_da_args_t *args)
  832. {
  833. int byteoff; /* offset of removed entry */
  834. xfs_inode_t *dp; /* incore directory inode */
  835. int entsize; /* this entry's size */
  836. int i; /* shortform entry index */
  837. int newsize; /* new inode size */
  838. int oldsize; /* old inode size */
  839. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  840. xfs_dir2_sf_t *sfp; /* shortform structure */
  841. xfs_dir2_trace_args("sf_removename", args);
  842. dp = args->dp;
  843. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  844. oldsize = (int)dp->i_d.di_size;
  845. /*
  846. * Bail out if the directory is way too short.
  847. */
  848. if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  849. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  850. return XFS_ERROR(EIO);
  851. }
  852. ASSERT(dp->i_df.if_bytes == oldsize);
  853. ASSERT(dp->i_df.if_u1.if_data != NULL);
  854. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  855. ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
  856. /*
  857. * Loop over the old directory entries.
  858. * Find the one we're deleting.
  859. */
  860. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
  861. i < sfp->hdr.count;
  862. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  863. if (sfep->namelen == args->namelen &&
  864. sfep->name[0] == args->name[0] &&
  865. memcmp(sfep->name, args->name, args->namelen) == 0) {
  866. ASSERT(xfs_dir2_sf_get_inumber(sfp,
  867. xfs_dir2_sf_inumberp(sfep)) ==
  868. args->inumber);
  869. break;
  870. }
  871. }
  872. /*
  873. * Didn't find it.
  874. */
  875. if (i == sfp->hdr.count) {
  876. return XFS_ERROR(ENOENT);
  877. }
  878. /*
  879. * Calculate sizes.
  880. */
  881. byteoff = (int)((char *)sfep - (char *)sfp);
  882. entsize = xfs_dir2_sf_entsize_byname(sfp, args->namelen);
  883. newsize = oldsize - entsize;
  884. /*
  885. * Copy the part if any after the removed entry, sliding it down.
  886. */
  887. if (byteoff + entsize < oldsize)
  888. memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
  889. oldsize - (byteoff + entsize));
  890. /*
  891. * Fix up the header and file size.
  892. */
  893. sfp->hdr.count--;
  894. dp->i_d.di_size = newsize;
  895. /*
  896. * Reallocate, making it smaller.
  897. */
  898. xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
  899. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  900. #if XFS_BIG_INUMS
  901. /*
  902. * Are we changing inode number size?
  903. */
  904. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  905. if (sfp->hdr.i8count == 1)
  906. xfs_dir2_sf_toino4(args);
  907. else
  908. sfp->hdr.i8count--;
  909. }
  910. #endif
  911. xfs_dir2_sf_check(args);
  912. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  913. return 0;
  914. }
  915. /*
  916. * Replace the inode number of an entry in a shortform directory.
  917. */
  918. int /* error */
  919. xfs_dir2_sf_replace(
  920. xfs_da_args_t *args) /* operation arguments */
  921. {
  922. xfs_inode_t *dp; /* incore directory inode */
  923. int i; /* entry index */
  924. #if XFS_BIG_INUMS || defined(DEBUG)
  925. xfs_ino_t ino=0; /* entry old inode number */
  926. #endif
  927. #if XFS_BIG_INUMS
  928. int i8elevated; /* sf_toino8 set i8count=1 */
  929. #endif
  930. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  931. xfs_dir2_sf_t *sfp; /* shortform structure */
  932. xfs_dir2_trace_args("sf_replace", args);
  933. dp = args->dp;
  934. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  935. /*
  936. * Bail out if the shortform directory is way too small.
  937. */
  938. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  939. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  940. return XFS_ERROR(EIO);
  941. }
  942. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  943. ASSERT(dp->i_df.if_u1.if_data != NULL);
  944. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  945. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
  946. #if XFS_BIG_INUMS
  947. /*
  948. * New inode number is large, and need to convert to 8-byte inodes.
  949. */
  950. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
  951. int error; /* error return value */
  952. int newsize; /* new inode size */
  953. newsize =
  954. dp->i_df.if_bytes +
  955. (sfp->hdr.count + 1) *
  956. ((uint)sizeof(xfs_dir2_ino8_t) -
  957. (uint)sizeof(xfs_dir2_ino4_t));
  958. /*
  959. * Won't fit as shortform, convert to block then do replace.
  960. */
  961. if (newsize > XFS_IFORK_DSIZE(dp)) {
  962. error = xfs_dir2_sf_to_block(args);
  963. if (error) {
  964. return error;
  965. }
  966. return xfs_dir2_block_replace(args);
  967. }
  968. /*
  969. * Still fits, convert to 8-byte now.
  970. */
  971. xfs_dir2_sf_toino8(args);
  972. i8elevated = 1;
  973. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  974. } else
  975. i8elevated = 0;
  976. #endif
  977. ASSERT(args->namelen != 1 || args->name[0] != '.');
  978. /*
  979. * Replace ..'s entry.
  980. */
  981. if (args->namelen == 2 &&
  982. args->name[0] == '.' && args->name[1] == '.') {
  983. #if XFS_BIG_INUMS || defined(DEBUG)
  984. ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
  985. ASSERT(args->inumber != ino);
  986. #endif
  987. xfs_dir2_sf_put_inumber(sfp, &args->inumber, &sfp->hdr.parent);
  988. }
  989. /*
  990. * Normal entry, look for the name.
  991. */
  992. else {
  993. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
  994. i < sfp->hdr.count;
  995. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  996. if (sfep->namelen == args->namelen &&
  997. sfep->name[0] == args->name[0] &&
  998. memcmp(args->name, sfep->name, args->namelen) == 0) {
  999. #if XFS_BIG_INUMS || defined(DEBUG)
  1000. ino = xfs_dir2_sf_get_inumber(sfp,
  1001. xfs_dir2_sf_inumberp(sfep));
  1002. ASSERT(args->inumber != ino);
  1003. #endif
  1004. xfs_dir2_sf_put_inumber(sfp, &args->inumber,
  1005. xfs_dir2_sf_inumberp(sfep));
  1006. break;
  1007. }
  1008. }
  1009. /*
  1010. * Didn't find it.
  1011. */
  1012. if (i == sfp->hdr.count) {
  1013. ASSERT(args->oknoent);
  1014. #if XFS_BIG_INUMS
  1015. if (i8elevated)
  1016. xfs_dir2_sf_toino4(args);
  1017. #endif
  1018. return XFS_ERROR(ENOENT);
  1019. }
  1020. }
  1021. #if XFS_BIG_INUMS
  1022. /*
  1023. * See if the old number was large, the new number is small.
  1024. */
  1025. if (ino > XFS_DIR2_MAX_SHORT_INUM &&
  1026. args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
  1027. /*
  1028. * And the old count was one, so need to convert to small.
  1029. */
  1030. if (sfp->hdr.i8count == 1)
  1031. xfs_dir2_sf_toino4(args);
  1032. else
  1033. sfp->hdr.i8count--;
  1034. }
  1035. /*
  1036. * See if the old number was small, the new number is large.
  1037. */
  1038. if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
  1039. args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  1040. /*
  1041. * add to the i8count unless we just converted to 8-byte
  1042. * inodes (which does an implied i8count = 1)
  1043. */
  1044. ASSERT(sfp->hdr.i8count != 0);
  1045. if (!i8elevated)
  1046. sfp->hdr.i8count++;
  1047. }
  1048. #endif
  1049. xfs_dir2_sf_check(args);
  1050. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
  1051. return 0;
  1052. }
  1053. #if XFS_BIG_INUMS
  1054. /*
  1055. * Convert from 8-byte inode numbers to 4-byte inode numbers.
  1056. * The last 8-byte inode number is gone, but the count is still 1.
  1057. */
  1058. static void
  1059. xfs_dir2_sf_toino4(
  1060. xfs_da_args_t *args) /* operation arguments */
  1061. {
  1062. char *buf; /* old dir's buffer */
  1063. xfs_inode_t *dp; /* incore directory inode */
  1064. int i; /* entry index */
  1065. xfs_ino_t ino; /* entry inode number */
  1066. int newsize; /* new inode size */
  1067. xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
  1068. xfs_dir2_sf_t *oldsfp; /* old sf directory */
  1069. int oldsize; /* old inode size */
  1070. xfs_dir2_sf_entry_t *sfep; /* new sf entry */
  1071. xfs_dir2_sf_t *sfp; /* new sf directory */
  1072. xfs_dir2_trace_args("sf_toino4", args);
  1073. dp = args->dp;
  1074. /*
  1075. * Copy the old directory to the buffer.
  1076. * Then nuke it from the inode, and add the new buffer to the inode.
  1077. * Don't want xfs_idata_realloc copying the data here.
  1078. */
  1079. oldsize = dp->i_df.if_bytes;
  1080. buf = kmem_alloc(oldsize, KM_SLEEP);
  1081. oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  1082. ASSERT(oldsfp->hdr.i8count == 1);
  1083. memcpy(buf, oldsfp, oldsize);
  1084. /*
  1085. * Compute the new inode size.
  1086. */
  1087. newsize =
  1088. oldsize -
  1089. (oldsfp->hdr.count + 1) *
  1090. ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
  1091. xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  1092. xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  1093. /*
  1094. * Reset our pointers, the data has moved.
  1095. */
  1096. oldsfp = (xfs_dir2_sf_t *)buf;
  1097. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  1098. /*
  1099. * Fill in the new header.
  1100. */
  1101. sfp->hdr.count = oldsfp->hdr.count;
  1102. sfp->hdr.i8count = 0;
  1103. ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent);
  1104. xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent);
  1105. /*
  1106. * Copy the entries field by field.
  1107. */
  1108. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  1109. oldsfep = xfs_dir2_sf_firstentry(oldsfp);
  1110. i < sfp->hdr.count;
  1111. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
  1112. oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
  1113. sfep->namelen = oldsfep->namelen;
  1114. sfep->offset = oldsfep->offset;
  1115. memcpy(sfep->name, oldsfep->name, sfep->namelen);
  1116. ino = xfs_dir2_sf_get_inumber(oldsfp,
  1117. xfs_dir2_sf_inumberp(oldsfep));
  1118. xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep));
  1119. }
  1120. /*
  1121. * Clean up the inode.
  1122. */
  1123. kmem_free(buf, oldsize);
  1124. dp->i_d.di_size = newsize;
  1125. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1126. }
  1127. /*
  1128. * Convert from 4-byte inode numbers to 8-byte inode numbers.
  1129. * The new 8-byte inode number is not there yet, we leave with the
  1130. * count 1 but no corresponding entry.
  1131. */
  1132. static void
  1133. xfs_dir2_sf_toino8(
  1134. xfs_da_args_t *args) /* operation arguments */
  1135. {
  1136. char *buf; /* old dir's buffer */
  1137. xfs_inode_t *dp; /* incore directory inode */
  1138. int i; /* entry index */
  1139. xfs_ino_t ino; /* entry inode number */
  1140. int newsize; /* new inode size */
  1141. xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
  1142. xfs_dir2_sf_t *oldsfp; /* old sf directory */
  1143. int oldsize; /* old inode size */
  1144. xfs_dir2_sf_entry_t *sfep; /* new sf entry */
  1145. xfs_dir2_sf_t *sfp; /* new sf directory */
  1146. xfs_dir2_trace_args("sf_toino8", args);
  1147. dp = args->dp;
  1148. /*
  1149. * Copy the old directory to the buffer.
  1150. * Then nuke it from the inode, and add the new buffer to the inode.
  1151. * Don't want xfs_idata_realloc copying the data here.
  1152. */
  1153. oldsize = dp->i_df.if_bytes;
  1154. buf = kmem_alloc(oldsize, KM_SLEEP);
  1155. oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  1156. ASSERT(oldsfp->hdr.i8count == 0);
  1157. memcpy(buf, oldsfp, oldsize);
  1158. /*
  1159. * Compute the new inode size.
  1160. */
  1161. newsize =
  1162. oldsize +
  1163. (oldsfp->hdr.count + 1) *
  1164. ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
  1165. xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  1166. xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  1167. /*
  1168. * Reset our pointers, the data has moved.
  1169. */
  1170. oldsfp = (xfs_dir2_sf_t *)buf;
  1171. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  1172. /*
  1173. * Fill in the new header.
  1174. */
  1175. sfp->hdr.count = oldsfp->hdr.count;
  1176. sfp->hdr.i8count = 1;
  1177. ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent);
  1178. xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent);
  1179. /*
  1180. * Copy the entries field by field.
  1181. */
  1182. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  1183. oldsfep = xfs_dir2_sf_firstentry(oldsfp);
  1184. i < sfp->hdr.count;
  1185. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
  1186. oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
  1187. sfep->namelen = oldsfep->namelen;
  1188. sfep->offset = oldsfep->offset;
  1189. memcpy(sfep->name, oldsfep->name, sfep->namelen);
  1190. ino = xfs_dir2_sf_get_inumber(oldsfp,
  1191. xfs_dir2_sf_inumberp(oldsfep));
  1192. xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep));
  1193. }
  1194. /*
  1195. * Clean up the inode.
  1196. */
  1197. kmem_free(buf, oldsize);
  1198. dp->i_d.di_size = newsize;
  1199. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1200. }
  1201. #endif /* XFS_BIG_INUMS */