xfs_dir2_sf.c 37 KB

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