xfs_dir2_sf.c 38 KB

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