xfs_dir2_sf.c 37 KB

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