xfs_dir2_sf.c 36 KB

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