xfs_dir2_sf.c 36 KB

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