xfs_dir2_sf.c 37 KB

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