xfs_dir2_sf.c 37 KB

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