xfs_dir2_sf.c 36 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, 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, dotdot_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. off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  730. XFS_DIR2_DATA_FIRST_OFFSET);
  731. ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
  732. #if XFS_BIG_INUMS
  733. ino += mp->m_inoadd;
  734. #endif
  735. if (filldir(dirent, "..", 2, off, ino, DT_DIR)) {
  736. *offset = dotdot_offset;
  737. return 0;
  738. }
  739. }
  740. /*
  741. * Loop while there are more entries and put'ing works.
  742. */
  743. sfep = xfs_dir2_sf_firstentry(sfp);
  744. for (i = 0; i < sfp->hdr.count; i++) {
  745. off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  746. xfs_dir2_sf_get_offset(sfep));
  747. if (*offset > off) {
  748. sfep = xfs_dir2_sf_nextentry(sfp, sfep);
  749. continue;
  750. }
  751. ino = xfs_dir2_sf_get_inumber(sfp, xfs_dir2_sf_inumberp(sfep));
  752. #if XFS_BIG_INUMS
  753. ino += mp->m_inoadd;
  754. #endif
  755. if (filldir(dirent, sfep->name, sfep->namelen,
  756. off + xfs_dir2_data_entsize(sfep->namelen),
  757. ino, DT_UNKNOWN)) {
  758. *offset = off;
  759. return 0;
  760. }
  761. sfep = xfs_dir2_sf_nextentry(sfp, sfep);
  762. }
  763. *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0);
  764. return 0;
  765. }
  766. /*
  767. * Lookup an entry in a shortform directory.
  768. * Returns EEXIST if found, ENOENT if not found.
  769. */
  770. int /* error */
  771. xfs_dir2_sf_lookup(
  772. xfs_da_args_t *args) /* operation arguments */
  773. {
  774. xfs_inode_t *dp; /* incore directory inode */
  775. int i; /* entry index */
  776. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  777. xfs_dir2_sf_t *sfp; /* shortform structure */
  778. xfs_dir2_trace_args("sf_lookup", args);
  779. xfs_dir2_sf_check(args);
  780. dp = args->dp;
  781. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  782. /*
  783. * Bail out if the directory is way too short.
  784. */
  785. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  786. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  787. return XFS_ERROR(EIO);
  788. }
  789. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  790. ASSERT(dp->i_df.if_u1.if_data != NULL);
  791. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  792. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
  793. /*
  794. * Special case for .
  795. */
  796. if (args->namelen == 1 && args->name[0] == '.') {
  797. args->inumber = dp->i_ino;
  798. return XFS_ERROR(EEXIST);
  799. }
  800. /*
  801. * Special case for ..
  802. */
  803. if (args->namelen == 2 &&
  804. args->name[0] == '.' && args->name[1] == '.') {
  805. args->inumber = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
  806. return XFS_ERROR(EEXIST);
  807. }
  808. /*
  809. * Loop over all the entries trying to match ours.
  810. */
  811. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
  812. i < sfp->hdr.count;
  813. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  814. if (sfep->namelen == args->namelen &&
  815. sfep->name[0] == args->name[0] &&
  816. memcmp(args->name, sfep->name, args->namelen) == 0) {
  817. args->inumber =
  818. xfs_dir2_sf_get_inumber(sfp,
  819. xfs_dir2_sf_inumberp(sfep));
  820. return XFS_ERROR(EEXIST);
  821. }
  822. }
  823. /*
  824. * Didn't find it.
  825. */
  826. ASSERT(args->oknoent);
  827. return XFS_ERROR(ENOENT);
  828. }
  829. /*
  830. * Remove an entry from a shortform directory.
  831. */
  832. int /* error */
  833. xfs_dir2_sf_removename(
  834. xfs_da_args_t *args)
  835. {
  836. int byteoff; /* offset of removed entry */
  837. xfs_inode_t *dp; /* incore directory inode */
  838. int entsize; /* this entry's size */
  839. int i; /* shortform entry index */
  840. int newsize; /* new inode size */
  841. int oldsize; /* old inode size */
  842. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  843. xfs_dir2_sf_t *sfp; /* shortform structure */
  844. xfs_dir2_trace_args("sf_removename", args);
  845. dp = args->dp;
  846. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  847. oldsize = (int)dp->i_d.di_size;
  848. /*
  849. * Bail out if the directory is way too short.
  850. */
  851. if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  852. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  853. return XFS_ERROR(EIO);
  854. }
  855. ASSERT(dp->i_df.if_bytes == oldsize);
  856. ASSERT(dp->i_df.if_u1.if_data != NULL);
  857. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  858. ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
  859. /*
  860. * Loop over the old directory entries.
  861. * Find the one we're deleting.
  862. */
  863. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
  864. i < sfp->hdr.count;
  865. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  866. if (sfep->namelen == args->namelen &&
  867. sfep->name[0] == args->name[0] &&
  868. memcmp(sfep->name, args->name, args->namelen) == 0) {
  869. ASSERT(xfs_dir2_sf_get_inumber(sfp,
  870. xfs_dir2_sf_inumberp(sfep)) ==
  871. args->inumber);
  872. break;
  873. }
  874. }
  875. /*
  876. * Didn't find it.
  877. */
  878. if (i == sfp->hdr.count) {
  879. return XFS_ERROR(ENOENT);
  880. }
  881. /*
  882. * Calculate sizes.
  883. */
  884. byteoff = (int)((char *)sfep - (char *)sfp);
  885. entsize = xfs_dir2_sf_entsize_byname(sfp, args->namelen);
  886. newsize = oldsize - entsize;
  887. /*
  888. * Copy the part if any after the removed entry, sliding it down.
  889. */
  890. if (byteoff + entsize < oldsize)
  891. memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
  892. oldsize - (byteoff + entsize));
  893. /*
  894. * Fix up the header and file size.
  895. */
  896. sfp->hdr.count--;
  897. dp->i_d.di_size = newsize;
  898. /*
  899. * Reallocate, making it smaller.
  900. */
  901. xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
  902. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  903. #if XFS_BIG_INUMS
  904. /*
  905. * Are we changing inode number size?
  906. */
  907. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  908. if (sfp->hdr.i8count == 1)
  909. xfs_dir2_sf_toino4(args);
  910. else
  911. sfp->hdr.i8count--;
  912. }
  913. #endif
  914. xfs_dir2_sf_check(args);
  915. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  916. return 0;
  917. }
  918. /*
  919. * Replace the inode number of an entry in a shortform directory.
  920. */
  921. int /* error */
  922. xfs_dir2_sf_replace(
  923. xfs_da_args_t *args) /* operation arguments */
  924. {
  925. xfs_inode_t *dp; /* incore directory inode */
  926. int i; /* entry index */
  927. #if XFS_BIG_INUMS || defined(DEBUG)
  928. xfs_ino_t ino=0; /* entry old inode number */
  929. #endif
  930. #if XFS_BIG_INUMS
  931. int i8elevated; /* sf_toino8 set i8count=1 */
  932. #endif
  933. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  934. xfs_dir2_sf_t *sfp; /* shortform structure */
  935. xfs_dir2_trace_args("sf_replace", args);
  936. dp = args->dp;
  937. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  938. /*
  939. * Bail out if the shortform directory is way too small.
  940. */
  941. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  942. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  943. return XFS_ERROR(EIO);
  944. }
  945. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  946. ASSERT(dp->i_df.if_u1.if_data != NULL);
  947. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  948. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
  949. #if XFS_BIG_INUMS
  950. /*
  951. * New inode number is large, and need to convert to 8-byte inodes.
  952. */
  953. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {
  954. int error; /* error return value */
  955. int newsize; /* new inode size */
  956. newsize =
  957. dp->i_df.if_bytes +
  958. (sfp->hdr.count + 1) *
  959. ((uint)sizeof(xfs_dir2_ino8_t) -
  960. (uint)sizeof(xfs_dir2_ino4_t));
  961. /*
  962. * Won't fit as shortform, convert to block then do replace.
  963. */
  964. if (newsize > XFS_IFORK_DSIZE(dp)) {
  965. error = xfs_dir2_sf_to_block(args);
  966. if (error) {
  967. return error;
  968. }
  969. return xfs_dir2_block_replace(args);
  970. }
  971. /*
  972. * Still fits, convert to 8-byte now.
  973. */
  974. xfs_dir2_sf_toino8(args);
  975. i8elevated = 1;
  976. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  977. } else
  978. i8elevated = 0;
  979. #endif
  980. ASSERT(args->namelen != 1 || args->name[0] != '.');
  981. /*
  982. * Replace ..'s entry.
  983. */
  984. if (args->namelen == 2 &&
  985. args->name[0] == '.' && args->name[1] == '.') {
  986. #if XFS_BIG_INUMS || defined(DEBUG)
  987. ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);
  988. ASSERT(args->inumber != ino);
  989. #endif
  990. xfs_dir2_sf_put_inumber(sfp, &args->inumber, &sfp->hdr.parent);
  991. }
  992. /*
  993. * Normal entry, look for the name.
  994. */
  995. else {
  996. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
  997. i < sfp->hdr.count;
  998. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {
  999. if (sfep->namelen == args->namelen &&
  1000. sfep->name[0] == args->name[0] &&
  1001. memcmp(args->name, sfep->name, args->namelen) == 0) {
  1002. #if XFS_BIG_INUMS || defined(DEBUG)
  1003. ino = xfs_dir2_sf_get_inumber(sfp,
  1004. xfs_dir2_sf_inumberp(sfep));
  1005. ASSERT(args->inumber != ino);
  1006. #endif
  1007. xfs_dir2_sf_put_inumber(sfp, &args->inumber,
  1008. xfs_dir2_sf_inumberp(sfep));
  1009. break;
  1010. }
  1011. }
  1012. /*
  1013. * Didn't find it.
  1014. */
  1015. if (i == sfp->hdr.count) {
  1016. ASSERT(args->oknoent);
  1017. #if XFS_BIG_INUMS
  1018. if (i8elevated)
  1019. xfs_dir2_sf_toino4(args);
  1020. #endif
  1021. return XFS_ERROR(ENOENT);
  1022. }
  1023. }
  1024. #if XFS_BIG_INUMS
  1025. /*
  1026. * See if the old number was large, the new number is small.
  1027. */
  1028. if (ino > XFS_DIR2_MAX_SHORT_INUM &&
  1029. args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
  1030. /*
  1031. * And the old count was one, so need to convert to small.
  1032. */
  1033. if (sfp->hdr.i8count == 1)
  1034. xfs_dir2_sf_toino4(args);
  1035. else
  1036. sfp->hdr.i8count--;
  1037. }
  1038. /*
  1039. * See if the old number was small, the new number is large.
  1040. */
  1041. if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
  1042. args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  1043. /*
  1044. * add to the i8count unless we just converted to 8-byte
  1045. * inodes (which does an implied i8count = 1)
  1046. */
  1047. ASSERT(sfp->hdr.i8count != 0);
  1048. if (!i8elevated)
  1049. sfp->hdr.i8count++;
  1050. }
  1051. #endif
  1052. xfs_dir2_sf_check(args);
  1053. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
  1054. return 0;
  1055. }
  1056. #if XFS_BIG_INUMS
  1057. /*
  1058. * Convert from 8-byte inode numbers to 4-byte inode numbers.
  1059. * The last 8-byte inode number is gone, but the count is still 1.
  1060. */
  1061. static void
  1062. xfs_dir2_sf_toino4(
  1063. xfs_da_args_t *args) /* operation arguments */
  1064. {
  1065. char *buf; /* old dir's buffer */
  1066. xfs_inode_t *dp; /* incore directory inode */
  1067. int i; /* entry index */
  1068. xfs_ino_t ino; /* entry inode number */
  1069. int newsize; /* new inode size */
  1070. xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
  1071. xfs_dir2_sf_t *oldsfp; /* old sf directory */
  1072. int oldsize; /* old inode size */
  1073. xfs_dir2_sf_entry_t *sfep; /* new sf entry */
  1074. xfs_dir2_sf_t *sfp; /* new sf directory */
  1075. xfs_dir2_trace_args("sf_toino4", args);
  1076. dp = args->dp;
  1077. /*
  1078. * Copy the old directory to the buffer.
  1079. * Then nuke it from the inode, and add the new buffer to the inode.
  1080. * Don't want xfs_idata_realloc copying the data here.
  1081. */
  1082. oldsize = dp->i_df.if_bytes;
  1083. buf = kmem_alloc(oldsize, KM_SLEEP);
  1084. oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  1085. ASSERT(oldsfp->hdr.i8count == 1);
  1086. memcpy(buf, oldsfp, oldsize);
  1087. /*
  1088. * Compute the new inode size.
  1089. */
  1090. newsize =
  1091. oldsize -
  1092. (oldsfp->hdr.count + 1) *
  1093. ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
  1094. xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  1095. xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  1096. /*
  1097. * Reset our pointers, the data has moved.
  1098. */
  1099. oldsfp = (xfs_dir2_sf_t *)buf;
  1100. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  1101. /*
  1102. * Fill in the new header.
  1103. */
  1104. sfp->hdr.count = oldsfp->hdr.count;
  1105. sfp->hdr.i8count = 0;
  1106. ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent);
  1107. xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent);
  1108. /*
  1109. * Copy the entries field by field.
  1110. */
  1111. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  1112. oldsfep = xfs_dir2_sf_firstentry(oldsfp);
  1113. i < sfp->hdr.count;
  1114. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
  1115. oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
  1116. sfep->namelen = oldsfep->namelen;
  1117. sfep->offset = oldsfep->offset;
  1118. memcpy(sfep->name, oldsfep->name, sfep->namelen);
  1119. ino = xfs_dir2_sf_get_inumber(oldsfp,
  1120. xfs_dir2_sf_inumberp(oldsfep));
  1121. xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep));
  1122. }
  1123. /*
  1124. * Clean up the inode.
  1125. */
  1126. kmem_free(buf, oldsize);
  1127. dp->i_d.di_size = newsize;
  1128. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1129. }
  1130. /*
  1131. * Convert from 4-byte inode numbers to 8-byte inode numbers.
  1132. * The new 8-byte inode number is not there yet, we leave with the
  1133. * count 1 but no corresponding entry.
  1134. */
  1135. static void
  1136. xfs_dir2_sf_toino8(
  1137. xfs_da_args_t *args) /* operation arguments */
  1138. {
  1139. char *buf; /* old dir's buffer */
  1140. xfs_inode_t *dp; /* incore directory inode */
  1141. int i; /* entry index */
  1142. xfs_ino_t ino; /* entry inode number */
  1143. int newsize; /* new inode size */
  1144. xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
  1145. xfs_dir2_sf_t *oldsfp; /* old sf directory */
  1146. int oldsize; /* old inode size */
  1147. xfs_dir2_sf_entry_t *sfep; /* new sf entry */
  1148. xfs_dir2_sf_t *sfp; /* new sf directory */
  1149. xfs_dir2_trace_args("sf_toino8", args);
  1150. dp = args->dp;
  1151. /*
  1152. * Copy the old directory to the buffer.
  1153. * Then nuke it from the inode, and add the new buffer to the inode.
  1154. * Don't want xfs_idata_realloc copying the data here.
  1155. */
  1156. oldsize = dp->i_df.if_bytes;
  1157. buf = kmem_alloc(oldsize, KM_SLEEP);
  1158. oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  1159. ASSERT(oldsfp->hdr.i8count == 0);
  1160. memcpy(buf, oldsfp, oldsize);
  1161. /*
  1162. * Compute the new inode size.
  1163. */
  1164. newsize =
  1165. oldsize +
  1166. (oldsfp->hdr.count + 1) *
  1167. ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
  1168. xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  1169. xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  1170. /*
  1171. * Reset our pointers, the data has moved.
  1172. */
  1173. oldsfp = (xfs_dir2_sf_t *)buf;
  1174. sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
  1175. /*
  1176. * Fill in the new header.
  1177. */
  1178. sfp->hdr.count = oldsfp->hdr.count;
  1179. sfp->hdr.i8count = 1;
  1180. ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent);
  1181. xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent);
  1182. /*
  1183. * Copy the entries field by field.
  1184. */
  1185. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  1186. oldsfep = xfs_dir2_sf_firstentry(oldsfp);
  1187. i < sfp->hdr.count;
  1188. i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),
  1189. oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {
  1190. sfep->namelen = oldsfep->namelen;
  1191. sfep->offset = oldsfep->offset;
  1192. memcpy(sfep->name, oldsfep->name, sfep->namelen);
  1193. ino = xfs_dir2_sf_get_inumber(oldsfp,
  1194. xfs_dir2_sf_inumberp(oldsfep));
  1195. xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep));
  1196. }
  1197. /*
  1198. * Clean up the inode.
  1199. */
  1200. kmem_free(buf, oldsize);
  1201. dp->i_d.di_size = newsize;
  1202. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1203. }
  1204. #endif /* XFS_BIG_INUMS */