xfs_dir2_sf.c 37 KB

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