xfs_dir2_sf.c 37 KB

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