xfs_dir2_sf.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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 = kmalloc(mp->m_sb.sb_inodesize, GFP_KERNEL | __GFP_NOFAIL);
  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. kfree(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. struct xfs_inode *dp = args->dp;
  329. struct xfs_dir2_sf_hdr *sfp = dp->i_df.if_data;
  330. int error; /* error return value */
  331. int incr_isize; /* total change in size */
  332. int new_isize; /* size after adding name */
  333. int objchange; /* changing to 8-byte inodes */
  334. xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
  335. int pick; /* which algorithm to use */
  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. ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
  340. ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
  341. ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
  342. ASSERT(sfp != NULL);
  343. ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
  344. /*
  345. * Compute entry (and change in) size.
  346. */
  347. incr_isize = xfs_dir2_sf_entsize(dp->i_mount, sfp, args->namelen);
  348. objchange = 0;
  349. /*
  350. * Do we have to change to 8 byte inodes?
  351. */
  352. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
  353. /*
  354. * Yes, adjust the inode size. old count + (parent + new)
  355. */
  356. incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
  357. objchange = 1;
  358. }
  359. new_isize = (int)dp->i_disk_size + incr_isize;
  360. /*
  361. * Won't fit as shortform any more (due to size),
  362. * or the pick routine says it won't (due to offset values).
  363. */
  364. if (new_isize > xfs_inode_data_fork_size(dp) ||
  365. (pick =
  366. xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
  367. /*
  368. * Just checking or no space reservation, it doesn't fit.
  369. */
  370. if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
  371. return -ENOSPC;
  372. /*
  373. * Convert to block form then add the name.
  374. */
  375. error = xfs_dir2_sf_to_block(args);
  376. if (error)
  377. return error;
  378. return xfs_dir2_block_addname(args);
  379. }
  380. /*
  381. * Just checking, it fits.
  382. */
  383. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  384. return 0;
  385. /*
  386. * Do it the easy way - just add it at the end.
  387. */
  388. if (pick == 1)
  389. xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
  390. /*
  391. * Do it the hard way - look for a place to insert the new entry.
  392. * Convert to 8 byte inode numbers first if necessary.
  393. */
  394. else {
  395. ASSERT(pick == 2);
  396. if (objchange)
  397. xfs_dir2_sf_toino8(args);
  398. xfs_dir2_sf_addname_hard(args, objchange, new_isize);
  399. }
  400. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  401. return 0;
  402. }
  403. /*
  404. * Add the new entry the "easy" way.
  405. * This is copying the old directory and adding the new entry at the end.
  406. * Since it's sorted by "offset" we need room after the last offset
  407. * that's already there, and then room to convert to a block directory.
  408. * This is already checked by the pick routine.
  409. */
  410. static void
  411. xfs_dir2_sf_addname_easy(
  412. xfs_da_args_t *args, /* operation arguments */
  413. xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
  414. xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
  415. int new_isize) /* new directory size */
  416. {
  417. struct xfs_inode *dp = args->dp;
  418. struct xfs_mount *mp = dp->i_mount;
  419. struct xfs_dir2_sf_hdr *sfp = dp->i_df.if_data;
  420. int byteoff = (int)((char *)sfep - (char *)sfp);
  421. /*
  422. * Grow the in-inode space.
  423. */
  424. sfp = xfs_idata_realloc(dp, xfs_dir2_sf_entsize(mp, sfp, args->namelen),
  425. XFS_DATA_FORK);
  426. /*
  427. * Need to set up again due to realloc of the inode data.
  428. */
  429. sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
  430. /*
  431. * Fill in the new entry.
  432. */
  433. sfep->namelen = args->namelen;
  434. xfs_dir2_sf_put_offset(sfep, offset);
  435. memcpy(sfep->name, args->name, sfep->namelen);
  436. xfs_dir2_sf_put_ino(mp, sfp, sfep, args->inumber);
  437. xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
  438. /*
  439. * Update the header and inode.
  440. */
  441. sfp->count++;
  442. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
  443. sfp->i8count++;
  444. dp->i_disk_size = new_isize;
  445. xfs_dir2_sf_check(args);
  446. }
  447. /*
  448. * Add the new entry the "hard" way.
  449. * The caller has already converted to 8 byte inode numbers if necessary,
  450. * in which case we need to leave the i8count at 1.
  451. * Find a hole that the new entry will fit into, and copy
  452. * the first part of the entries, the new entry, and the last part of
  453. * the entries.
  454. */
  455. /* ARGSUSED */
  456. static void
  457. xfs_dir2_sf_addname_hard(
  458. xfs_da_args_t *args, /* operation arguments */
  459. int objchange, /* changing inode number size */
  460. int new_isize) /* new directory size */
  461. {
  462. struct xfs_inode *dp = args->dp;
  463. struct xfs_mount *mp = dp->i_mount;
  464. int add_datasize; /* data size need for new ent */
  465. char *buf; /* buffer for old */
  466. int eof; /* reached end of old dir */
  467. int nbytes; /* temp for byte copies */
  468. xfs_dir2_data_aoff_t new_offset; /* next offset value */
  469. xfs_dir2_data_aoff_t offset; /* current offset value */
  470. int old_isize; /* previous size */
  471. xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
  472. xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
  473. xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
  474. xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
  475. /*
  476. * Copy the old directory to the stack buffer.
  477. */
  478. old_isize = (int)dp->i_disk_size;
  479. buf = kmalloc(old_isize, GFP_KERNEL | __GFP_NOFAIL);
  480. oldsfp = (xfs_dir2_sf_hdr_t *)buf;
  481. memcpy(oldsfp, dp->i_df.if_data, old_isize);
  482. /*
  483. * Loop over the old directory finding the place we're going
  484. * to insert the new entry.
  485. * If it's going to end up at the end then oldsfep will point there.
  486. */
  487. for (offset = args->geo->data_first_offset,
  488. oldsfep = xfs_dir2_sf_firstentry(oldsfp),
  489. add_datasize = xfs_dir2_data_entsize(mp, args->namelen),
  490. eof = (char *)oldsfep == &buf[old_isize];
  491. !eof;
  492. offset = new_offset + xfs_dir2_data_entsize(mp, oldsfep->namelen),
  493. oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep),
  494. eof = (char *)oldsfep == &buf[old_isize]) {
  495. new_offset = xfs_dir2_sf_get_offset(oldsfep);
  496. if (offset + add_datasize <= new_offset)
  497. break;
  498. }
  499. /*
  500. * Get rid of the old directory, then allocate space for
  501. * the new one. We do this so xfs_idata_realloc won't copy
  502. * the data.
  503. */
  504. xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
  505. sfp = xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
  506. /*
  507. * Copy the first part of the directory, including the header.
  508. */
  509. nbytes = (int)((char *)oldsfep - (char *)oldsfp);
  510. memcpy(sfp, oldsfp, nbytes);
  511. sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
  512. /*
  513. * Fill in the new entry, and update the header counts.
  514. */
  515. sfep->namelen = args->namelen;
  516. xfs_dir2_sf_put_offset(sfep, offset);
  517. memcpy(sfep->name, args->name, sfep->namelen);
  518. xfs_dir2_sf_put_ino(mp, sfp, sfep, args->inumber);
  519. xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
  520. sfp->count++;
  521. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
  522. sfp->i8count++;
  523. /*
  524. * If there's more left to copy, do that.
  525. */
  526. if (!eof) {
  527. sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
  528. memcpy(sfep, oldsfep, old_isize - nbytes);
  529. }
  530. kfree(buf);
  531. dp->i_disk_size = new_isize;
  532. xfs_dir2_sf_check(args);
  533. }
  534. /*
  535. * Decide if the new entry will fit at all.
  536. * If it will fit, pick between adding the new entry to the end (easy)
  537. * or somewhere else (hard).
  538. * Return 0 (won't fit), 1 (easy), 2 (hard).
  539. */
  540. /*ARGSUSED*/
  541. static int /* pick result */
  542. xfs_dir2_sf_addname_pick(
  543. xfs_da_args_t *args, /* operation arguments */
  544. int objchange, /* inode # size changes */
  545. xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
  546. xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
  547. {
  548. struct xfs_inode *dp = args->dp;
  549. struct xfs_mount *mp = dp->i_mount;
  550. int holefit; /* found hole it will fit in */
  551. int i; /* entry number */
  552. xfs_dir2_data_aoff_t offset; /* data block offset */
  553. xfs_dir2_sf_entry_t *sfep; /* shortform entry */
  554. struct xfs_dir2_sf_hdr *sfp = dp->i_df.if_data;
  555. int size; /* entry's data size */
  556. int used; /* data bytes used */
  557. size = xfs_dir2_data_entsize(mp, args->namelen);
  558. offset = args->geo->data_first_offset;
  559. sfep = xfs_dir2_sf_firstentry(sfp);
  560. holefit = 0;
  561. /*
  562. * Loop over sf entries.
  563. * Keep track of data offset and whether we've seen a place
  564. * to insert the new entry.
  565. */
  566. for (i = 0; i < sfp->count; i++) {
  567. if (!holefit)
  568. holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
  569. offset = xfs_dir2_sf_get_offset(sfep) +
  570. xfs_dir2_data_entsize(mp, sfep->namelen);
  571. sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
  572. }
  573. /*
  574. * Calculate data bytes used excluding the new entry, if this
  575. * was a data block (block form directory).
  576. */
  577. used = offset +
  578. (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
  579. (uint)sizeof(xfs_dir2_block_tail_t);
  580. /*
  581. * If it won't fit in a block form then we can't insert it,
  582. * we'll go back, convert to block, then try the insert and convert
  583. * to leaf.
  584. */
  585. if (used + (holefit ? 0 : size) > args->geo->blksize)
  586. return 0;
  587. /*
  588. * If changing the inode number size, do it the hard way.
  589. */
  590. if (objchange)
  591. return 2;
  592. /*
  593. * If it won't fit at the end then do it the hard way (use the hole).
  594. */
  595. if (used + size > args->geo->blksize)
  596. return 2;
  597. /*
  598. * Do it the easy way.
  599. */
  600. *sfepp = sfep;
  601. *offsetp = offset;
  602. return 1;
  603. }
  604. #ifdef DEBUG
  605. /*
  606. * Check consistency of shortform directory, assert if bad.
  607. */
  608. static void
  609. xfs_dir2_sf_check(
  610. xfs_da_args_t *args) /* operation arguments */
  611. {
  612. struct xfs_inode *dp = args->dp;
  613. struct xfs_mount *mp = dp->i_mount;
  614. struct xfs_dir2_sf_hdr *sfp = dp->i_df.if_data;
  615. int i; /* entry number */
  616. int i8count; /* number of big inode#s */
  617. xfs_ino_t ino; /* entry inode number */
  618. int offset; /* data offset */
  619. xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
  620. offset = args->geo->data_first_offset;
  621. ino = xfs_dir2_sf_get_parent_ino(sfp);
  622. i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
  623. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
  624. i < sfp->count;
  625. i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
  626. ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
  627. ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
  628. i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
  629. offset =
  630. xfs_dir2_sf_get_offset(sfep) +
  631. xfs_dir2_data_entsize(mp, sfep->namelen);
  632. ASSERT(xfs_dir2_sf_get_ftype(mp, sfep) < XFS_DIR3_FT_MAX);
  633. }
  634. ASSERT(i8count == sfp->i8count);
  635. ASSERT((char *)sfep - (char *)sfp == dp->i_disk_size);
  636. ASSERT(offset +
  637. (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
  638. (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
  639. }
  640. #endif /* DEBUG */
  641. /* Verify the consistency of an inline directory. */
  642. xfs_failaddr_t
  643. xfs_dir2_sf_verify(
  644. struct xfs_mount *mp,
  645. struct xfs_dir2_sf_hdr *sfp,
  646. int64_t size)
  647. {
  648. struct xfs_dir2_sf_entry *sfep;
  649. struct xfs_dir2_sf_entry *next_sfep;
  650. char *endp;
  651. xfs_ino_t ino;
  652. int i;
  653. int i8count;
  654. int offset;
  655. int error;
  656. uint8_t filetype;
  657. /*
  658. * Give up if the directory is way too short.
  659. */
  660. if (size <= offsetof(struct xfs_dir2_sf_hdr, parent) ||
  661. size < xfs_dir2_sf_hdr_size(sfp->i8count))
  662. return __this_address;
  663. endp = (char *)sfp + size;
  664. /* Check .. entry */
  665. ino = xfs_dir2_sf_get_parent_ino(sfp);
  666. i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
  667. error = xfs_dir_ino_validate(mp, ino);
  668. if (error)
  669. return __this_address;
  670. offset = mp->m_dir_geo->data_first_offset;
  671. /* Check all reported entries */
  672. sfep = xfs_dir2_sf_firstentry(sfp);
  673. for (i = 0; i < sfp->count; i++) {
  674. /*
  675. * struct xfs_dir2_sf_entry has a variable length.
  676. * Check the fixed-offset parts of the structure are
  677. * within the data buffer.
  678. */
  679. if (((char *)sfep + sizeof(*sfep)) >= endp)
  680. return __this_address;
  681. /* Don't allow names with known bad length. */
  682. if (sfep->namelen == 0)
  683. return __this_address;
  684. /*
  685. * Check that the variable-length part of the structure is
  686. * within the data buffer. The next entry starts after the
  687. * name component, so nextentry is an acceptable test.
  688. */
  689. next_sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
  690. if (endp < (char *)next_sfep)
  691. return __this_address;
  692. /* Check that the offsets always increase. */
  693. if (xfs_dir2_sf_get_offset(sfep) < offset)
  694. return __this_address;
  695. /* Check the inode number. */
  696. ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
  697. i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
  698. error = xfs_dir_ino_validate(mp, ino);
  699. if (error)
  700. return __this_address;
  701. /* Check the file type. */
  702. filetype = xfs_dir2_sf_get_ftype(mp, sfep);
  703. if (filetype >= XFS_DIR3_FT_MAX)
  704. return __this_address;
  705. offset = xfs_dir2_sf_get_offset(sfep) +
  706. xfs_dir2_data_entsize(mp, sfep->namelen);
  707. sfep = next_sfep;
  708. }
  709. if (i8count != sfp->i8count)
  710. return __this_address;
  711. if ((void *)sfep != (void *)endp)
  712. return __this_address;
  713. /* Make sure this whole thing ought to be in local format. */
  714. if (offset + (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
  715. (uint)sizeof(xfs_dir2_block_tail_t) > mp->m_dir_geo->blksize)
  716. return __this_address;
  717. return NULL;
  718. }
  719. /*
  720. * Create a new (shortform) directory.
  721. */
  722. int /* error, always 0 */
  723. xfs_dir2_sf_create(
  724. xfs_da_args_t *args, /* operation arguments */
  725. xfs_ino_t pino) /* parent inode number */
  726. {
  727. xfs_inode_t *dp; /* incore directory inode */
  728. int i8count; /* parent inode is an 8-byte number */
  729. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  730. int size; /* directory size */
  731. trace_xfs_dir2_sf_create(args);
  732. dp = args->dp;
  733. ASSERT(dp != NULL);
  734. ASSERT(dp->i_disk_size == 0);
  735. /*
  736. * If it's currently a zero-length extent file,
  737. * convert it to local format.
  738. */
  739. if (dp->i_df.if_format == XFS_DINODE_FMT_EXTENTS) {
  740. dp->i_df.if_format = XFS_DINODE_FMT_LOCAL;
  741. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  742. }
  743. ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
  744. ASSERT(dp->i_df.if_bytes == 0);
  745. i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
  746. size = xfs_dir2_sf_hdr_size(i8count);
  747. /*
  748. * Make a buffer for the data and fill in the header.
  749. */
  750. sfp = xfs_idata_realloc(dp, size, XFS_DATA_FORK);
  751. sfp->i8count = i8count;
  752. /*
  753. * Now can put in the inode number, since i8count is set.
  754. */
  755. xfs_dir2_sf_put_parent_ino(sfp, pino);
  756. sfp->count = 0;
  757. dp->i_disk_size = size;
  758. xfs_dir2_sf_check(args);
  759. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  760. return 0;
  761. }
  762. /*
  763. * Lookup an entry in a shortform directory.
  764. * Returns EEXIST if found, ENOENT if not found.
  765. */
  766. int /* error */
  767. xfs_dir2_sf_lookup(
  768. xfs_da_args_t *args) /* operation arguments */
  769. {
  770. struct xfs_inode *dp = args->dp;
  771. struct xfs_mount *mp = dp->i_mount;
  772. struct xfs_dir2_sf_hdr *sfp = dp->i_df.if_data;
  773. int i; /* entry index */
  774. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  775. enum xfs_dacmp cmp; /* comparison result */
  776. xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
  777. trace_xfs_dir2_sf_lookup(args);
  778. xfs_dir2_sf_check(args);
  779. ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
  780. ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
  781. ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
  782. ASSERT(sfp != NULL);
  783. ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
  784. /*
  785. * Special case for .
  786. */
  787. if (args->namelen == 1 && args->name[0] == '.') {
  788. args->inumber = dp->i_ino;
  789. args->cmpresult = XFS_CMP_EXACT;
  790. args->filetype = XFS_DIR3_FT_DIR;
  791. return -EEXIST;
  792. }
  793. /*
  794. * Special case for ..
  795. */
  796. if (args->namelen == 2 &&
  797. args->name[0] == '.' && args->name[1] == '.') {
  798. args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
  799. args->cmpresult = XFS_CMP_EXACT;
  800. args->filetype = XFS_DIR3_FT_DIR;
  801. return -EEXIST;
  802. }
  803. /*
  804. * Loop over all the entries trying to match ours.
  805. */
  806. ci_sfep = NULL;
  807. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
  808. i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
  809. /*
  810. * Compare name and if it's an exact match, return the inode
  811. * number. If it's the first case-insensitive match, store the
  812. * inode number and continue looking for an exact match.
  813. */
  814. cmp = xfs_dir2_compname(args, sfep->name, sfep->namelen);
  815. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  816. args->cmpresult = cmp;
  817. args->inumber = xfs_dir2_sf_get_ino(mp, sfp, sfep);
  818. args->filetype = xfs_dir2_sf_get_ftype(mp, sfep);
  819. if (cmp == XFS_CMP_EXACT)
  820. return -EEXIST;
  821. ci_sfep = sfep;
  822. }
  823. }
  824. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  825. /*
  826. * Here, we can only be doing a lookup (not a rename or replace).
  827. * If a case-insensitive match was not found, return -ENOENT.
  828. */
  829. if (!ci_sfep)
  830. return -ENOENT;
  831. /* otherwise process the CI match as required by the caller */
  832. return xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
  833. }
  834. /*
  835. * Remove an entry from a shortform directory.
  836. */
  837. int /* error */
  838. xfs_dir2_sf_removename(
  839. xfs_da_args_t *args)
  840. {
  841. struct xfs_inode *dp = args->dp;
  842. struct xfs_mount *mp = dp->i_mount;
  843. struct xfs_dir2_sf_hdr *sfp = dp->i_df.if_data;
  844. int byteoff; /* offset of removed entry */
  845. int entsize; /* this entry's size */
  846. int i; /* shortform entry index */
  847. int newsize; /* new inode size */
  848. int oldsize; /* old inode size */
  849. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  850. trace_xfs_dir2_sf_removename(args);
  851. ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
  852. oldsize = (int)dp->i_disk_size;
  853. ASSERT(oldsize >= offsetof(struct xfs_dir2_sf_hdr, parent));
  854. ASSERT(dp->i_df.if_bytes == oldsize);
  855. ASSERT(sfp != NULL);
  856. ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
  857. /*
  858. * Loop over the old directory entries.
  859. * Find the one we're deleting.
  860. */
  861. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
  862. i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
  863. if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
  864. XFS_CMP_EXACT) {
  865. ASSERT(xfs_dir2_sf_get_ino(mp, sfp, sfep) ==
  866. args->inumber);
  867. break;
  868. }
  869. }
  870. /*
  871. * Didn't find it.
  872. */
  873. if (i == sfp->count)
  874. return -ENOENT;
  875. /*
  876. * Calculate sizes.
  877. */
  878. byteoff = (int)((char *)sfep - (char *)sfp);
  879. entsize = xfs_dir2_sf_entsize(mp, sfp, args->namelen);
  880. newsize = oldsize - entsize;
  881. /*
  882. * Copy the part if any after the removed entry, sliding it down.
  883. */
  884. if (byteoff + entsize < oldsize)
  885. memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
  886. oldsize - (byteoff + entsize));
  887. /*
  888. * Fix up the header and file size.
  889. */
  890. sfp->count--;
  891. dp->i_disk_size = newsize;
  892. /*
  893. * Reallocate, making it smaller.
  894. */
  895. sfp = xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
  896. /*
  897. * Are we changing inode number size?
  898. */
  899. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  900. if (sfp->i8count == 1)
  901. xfs_dir2_sf_toino4(args);
  902. else
  903. sfp->i8count--;
  904. }
  905. xfs_dir2_sf_check(args);
  906. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  907. return 0;
  908. }
  909. /*
  910. * Check whether the sf dir replace operation need more blocks.
  911. */
  912. static bool
  913. xfs_dir2_sf_replace_needblock(
  914. struct xfs_inode *dp,
  915. xfs_ino_t inum)
  916. {
  917. struct xfs_dir2_sf_hdr *sfp = dp->i_df.if_data;
  918. int newsize;
  919. if (dp->i_df.if_format != XFS_DINODE_FMT_LOCAL)
  920. return false;
  921. newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
  922. return inum > XFS_DIR2_MAX_SHORT_INUM &&
  923. sfp->i8count == 0 && newsize > xfs_inode_data_fork_size(dp);
  924. }
  925. /*
  926. * Replace the inode number of an entry in a shortform directory.
  927. */
  928. int /* error */
  929. xfs_dir2_sf_replace(
  930. xfs_da_args_t *args) /* operation arguments */
  931. {
  932. struct xfs_inode *dp = args->dp;
  933. struct xfs_mount *mp = dp->i_mount;
  934. struct xfs_dir2_sf_hdr *sfp = dp->i_df.if_data;
  935. int i; /* entry index */
  936. xfs_ino_t ino=0; /* entry old inode number */
  937. int i8elevated; /* sf_toino8 set i8count=1 */
  938. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  939. trace_xfs_dir2_sf_replace(args);
  940. ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
  941. ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
  942. ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
  943. ASSERT(sfp != NULL);
  944. ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
  945. /*
  946. * New inode number is large, and need to convert to 8-byte inodes.
  947. */
  948. if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
  949. int error; /* error return value */
  950. /*
  951. * Won't fit as shortform, convert to block then do replace.
  952. */
  953. if (xfs_dir2_sf_replace_needblock(dp, args->inumber)) {
  954. error = xfs_dir2_sf_to_block(args);
  955. if (error)
  956. return error;
  957. return xfs_dir2_block_replace(args);
  958. }
  959. /*
  960. * Still fits, convert to 8-byte now.
  961. */
  962. xfs_dir2_sf_toino8(args);
  963. i8elevated = 1;
  964. sfp = dp->i_df.if_data;
  965. } else
  966. i8elevated = 0;
  967. ASSERT(args->namelen != 1 || args->name[0] != '.');
  968. /*
  969. * Replace ..'s entry.
  970. */
  971. if (args->namelen == 2 &&
  972. args->name[0] == '.' && args->name[1] == '.') {
  973. ino = xfs_dir2_sf_get_parent_ino(sfp);
  974. ASSERT(args->inumber != ino);
  975. xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
  976. }
  977. /*
  978. * Normal entry, look for the name.
  979. */
  980. else {
  981. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
  982. i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
  983. if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
  984. XFS_CMP_EXACT) {
  985. ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
  986. ASSERT(args->inumber != ino);
  987. xfs_dir2_sf_put_ino(mp, sfp, sfep,
  988. args->inumber);
  989. xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
  990. break;
  991. }
  992. }
  993. /*
  994. * Didn't find it.
  995. */
  996. if (i == sfp->count) {
  997. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  998. if (i8elevated)
  999. xfs_dir2_sf_toino4(args);
  1000. return -ENOENT;
  1001. }
  1002. }
  1003. /*
  1004. * See if the old number was large, the new number is small.
  1005. */
  1006. if (ino > XFS_DIR2_MAX_SHORT_INUM &&
  1007. args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
  1008. /*
  1009. * And the old count was one, so need to convert to small.
  1010. */
  1011. if (sfp->i8count == 1)
  1012. xfs_dir2_sf_toino4(args);
  1013. else
  1014. sfp->i8count--;
  1015. }
  1016. /*
  1017. * See if the old number was small, the new number is large.
  1018. */
  1019. if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
  1020. args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
  1021. /*
  1022. * add to the i8count unless we just converted to 8-byte
  1023. * inodes (which does an implied i8count = 1)
  1024. */
  1025. ASSERT(sfp->i8count != 0);
  1026. if (!i8elevated)
  1027. sfp->i8count++;
  1028. }
  1029. xfs_dir2_sf_check(args);
  1030. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
  1031. return 0;
  1032. }
  1033. /*
  1034. * Convert from 8-byte inode numbers to 4-byte inode numbers.
  1035. * The last 8-byte inode number is gone, but the count is still 1.
  1036. */
  1037. static void
  1038. xfs_dir2_sf_toino4(
  1039. xfs_da_args_t *args) /* operation arguments */
  1040. {
  1041. struct xfs_inode *dp = args->dp;
  1042. struct xfs_mount *mp = dp->i_mount;
  1043. struct xfs_dir2_sf_hdr *oldsfp = dp->i_df.if_data;
  1044. char *buf; /* old dir's buffer */
  1045. int i; /* entry index */
  1046. int newsize; /* new inode size */
  1047. xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
  1048. int oldsize; /* old inode size */
  1049. xfs_dir2_sf_entry_t *sfep; /* new sf entry */
  1050. xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
  1051. trace_xfs_dir2_sf_toino4(args);
  1052. /*
  1053. * Copy the old directory to the buffer.
  1054. * Then nuke it from the inode, and add the new buffer to the inode.
  1055. * Don't want xfs_idata_realloc copying the data here.
  1056. */
  1057. oldsize = dp->i_df.if_bytes;
  1058. buf = kmalloc(oldsize, GFP_KERNEL | __GFP_NOFAIL);
  1059. ASSERT(oldsfp->i8count == 1);
  1060. memcpy(buf, oldsfp, oldsize);
  1061. /*
  1062. * Compute the new inode size.
  1063. */
  1064. newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
  1065. xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  1066. xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  1067. /*
  1068. * Reset our pointers, the data has moved.
  1069. */
  1070. oldsfp = (xfs_dir2_sf_hdr_t *)buf;
  1071. sfp = dp->i_df.if_data;
  1072. /*
  1073. * Fill in the new header.
  1074. */
  1075. sfp->count = oldsfp->count;
  1076. sfp->i8count = 0;
  1077. xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
  1078. /*
  1079. * Copy the entries field by field.
  1080. */
  1081. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  1082. oldsfep = xfs_dir2_sf_firstentry(oldsfp);
  1083. i < sfp->count;
  1084. i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep),
  1085. oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep)) {
  1086. sfep->namelen = oldsfep->namelen;
  1087. memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
  1088. memcpy(sfep->name, oldsfep->name, sfep->namelen);
  1089. xfs_dir2_sf_put_ino(mp, sfp, sfep,
  1090. xfs_dir2_sf_get_ino(mp, oldsfp, oldsfep));
  1091. xfs_dir2_sf_put_ftype(mp, sfep,
  1092. xfs_dir2_sf_get_ftype(mp, oldsfep));
  1093. }
  1094. /*
  1095. * Clean up the inode.
  1096. */
  1097. kfree(buf);
  1098. dp->i_disk_size = newsize;
  1099. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1100. }
  1101. /*
  1102. * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
  1103. * The new entry w/ an 8-byte inode number is not there yet; we leave with
  1104. * i8count set to 1, but no corresponding 8-byte entry.
  1105. */
  1106. static void
  1107. xfs_dir2_sf_toino8(
  1108. xfs_da_args_t *args) /* operation arguments */
  1109. {
  1110. struct xfs_inode *dp = args->dp;
  1111. struct xfs_mount *mp = dp->i_mount;
  1112. struct xfs_dir2_sf_hdr *oldsfp = dp->i_df.if_data;
  1113. char *buf; /* old dir's buffer */
  1114. int i; /* entry index */
  1115. int newsize; /* new inode size */
  1116. xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
  1117. int oldsize; /* old inode size */
  1118. xfs_dir2_sf_entry_t *sfep; /* new sf entry */
  1119. xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
  1120. trace_xfs_dir2_sf_toino8(args);
  1121. /*
  1122. * Copy the old directory to the buffer.
  1123. * Then nuke it from the inode, and add the new buffer to the inode.
  1124. * Don't want xfs_idata_realloc copying the data here.
  1125. */
  1126. oldsize = dp->i_df.if_bytes;
  1127. buf = kmalloc(oldsize, GFP_KERNEL | __GFP_NOFAIL);
  1128. ASSERT(oldsfp->i8count == 0);
  1129. memcpy(buf, oldsfp, oldsize);
  1130. /*
  1131. * Compute the new inode size (nb: entry count + 1 for parent)
  1132. */
  1133. newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
  1134. xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
  1135. xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
  1136. /*
  1137. * Reset our pointers, the data has moved.
  1138. */
  1139. oldsfp = (xfs_dir2_sf_hdr_t *)buf;
  1140. sfp = dp->i_df.if_data;
  1141. /*
  1142. * Fill in the new header.
  1143. */
  1144. sfp->count = oldsfp->count;
  1145. sfp->i8count = 1;
  1146. xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
  1147. /*
  1148. * Copy the entries field by field.
  1149. */
  1150. for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
  1151. oldsfep = xfs_dir2_sf_firstentry(oldsfp);
  1152. i < sfp->count;
  1153. i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep),
  1154. oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep)) {
  1155. sfep->namelen = oldsfep->namelen;
  1156. memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
  1157. memcpy(sfep->name, oldsfep->name, sfep->namelen);
  1158. xfs_dir2_sf_put_ino(mp, sfp, sfep,
  1159. xfs_dir2_sf_get_ino(mp, oldsfp, oldsfep));
  1160. xfs_dir2_sf_put_ftype(mp, sfep,
  1161. xfs_dir2_sf_get_ftype(mp, oldsfep));
  1162. }
  1163. /*
  1164. * Clean up the inode.
  1165. */
  1166. kfree(buf);
  1167. dp->i_disk_size = newsize;
  1168. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  1169. }