xfs_inode_fork.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include <linux/log2.h>
  7. #include "xfs.h"
  8. #include "xfs_fs.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_inode_item.h"
  16. #include "xfs_btree.h"
  17. #include "xfs_bmap_btree.h"
  18. #include "xfs_bmap.h"
  19. #include "xfs_error.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_attr_sf.h"
  22. #include "xfs_da_format.h"
  23. #include "xfs_da_btree.h"
  24. #include "xfs_dir2_priv.h"
  25. #include "xfs_attr_leaf.h"
  26. #include "xfs_shared.h"
  27. kmem_zone_t *xfs_ifork_zone;
  28. STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
  29. STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
  30. STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
  31. /*
  32. * Copy inode type and data and attr format specific information from the
  33. * on-disk inode to the in-core inode and fork structures. For fifos, devices,
  34. * and sockets this means set i_rdev to the proper value. For files,
  35. * directories, and symlinks this means to bring in the in-line data or extent
  36. * pointers as well as the attribute fork. For a fork in B-tree format, only
  37. * the root is immediately brought in-core. The rest will be read in later when
  38. * first referenced (see xfs_iread_extents()).
  39. */
  40. int
  41. xfs_iformat_fork(
  42. struct xfs_inode *ip,
  43. struct xfs_dinode *dip)
  44. {
  45. struct inode *inode = VFS_I(ip);
  46. struct xfs_attr_shortform *atp;
  47. int size;
  48. int error = 0;
  49. xfs_fsize_t di_size;
  50. switch (inode->i_mode & S_IFMT) {
  51. case S_IFIFO:
  52. case S_IFCHR:
  53. case S_IFBLK:
  54. case S_IFSOCK:
  55. ip->i_d.di_size = 0;
  56. inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
  57. break;
  58. case S_IFREG:
  59. case S_IFLNK:
  60. case S_IFDIR:
  61. switch (dip->di_format) {
  62. case XFS_DINODE_FMT_LOCAL:
  63. di_size = be64_to_cpu(dip->di_size);
  64. size = (int)di_size;
  65. error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
  66. break;
  67. case XFS_DINODE_FMT_EXTENTS:
  68. error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
  69. break;
  70. case XFS_DINODE_FMT_BTREE:
  71. error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
  72. break;
  73. default:
  74. return -EFSCORRUPTED;
  75. }
  76. break;
  77. default:
  78. return -EFSCORRUPTED;
  79. }
  80. if (error)
  81. return error;
  82. if (xfs_is_reflink_inode(ip)) {
  83. ASSERT(ip->i_cowfp == NULL);
  84. xfs_ifork_init_cow(ip);
  85. }
  86. if (!XFS_DFORK_Q(dip))
  87. return 0;
  88. ASSERT(ip->i_afp == NULL);
  89. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
  90. switch (dip->di_aformat) {
  91. case XFS_DINODE_FMT_LOCAL:
  92. atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
  93. size = be16_to_cpu(atp->hdr.totsize);
  94. error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
  95. break;
  96. case XFS_DINODE_FMT_EXTENTS:
  97. error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
  98. break;
  99. case XFS_DINODE_FMT_BTREE:
  100. error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
  101. break;
  102. default:
  103. error = -EFSCORRUPTED;
  104. break;
  105. }
  106. if (error) {
  107. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  108. ip->i_afp = NULL;
  109. if (ip->i_cowfp)
  110. kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
  111. ip->i_cowfp = NULL;
  112. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  113. }
  114. return error;
  115. }
  116. void
  117. xfs_init_local_fork(
  118. struct xfs_inode *ip,
  119. int whichfork,
  120. const void *data,
  121. int size)
  122. {
  123. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  124. int mem_size = size, real_size = 0;
  125. bool zero_terminate;
  126. /*
  127. * If we are using the local fork to store a symlink body we need to
  128. * zero-terminate it so that we can pass it back to the VFS directly.
  129. * Overallocate the in-memory fork by one for that and add a zero
  130. * to terminate it below.
  131. */
  132. zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
  133. if (zero_terminate)
  134. mem_size++;
  135. if (size) {
  136. real_size = roundup(mem_size, 4);
  137. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
  138. memcpy(ifp->if_u1.if_data, data, size);
  139. if (zero_terminate)
  140. ifp->if_u1.if_data[size] = '\0';
  141. } else {
  142. ifp->if_u1.if_data = NULL;
  143. }
  144. ifp->if_bytes = size;
  145. ifp->if_real_bytes = real_size;
  146. ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  147. ifp->if_flags |= XFS_IFINLINE;
  148. }
  149. /*
  150. * The file is in-lined in the on-disk inode.
  151. */
  152. STATIC int
  153. xfs_iformat_local(
  154. xfs_inode_t *ip,
  155. xfs_dinode_t *dip,
  156. int whichfork,
  157. int size)
  158. {
  159. /*
  160. * If the size is unreasonable, then something
  161. * is wrong and we just bail out rather than crash in
  162. * kmem_alloc() or memcpy() below.
  163. */
  164. if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  165. xfs_warn(ip->i_mount,
  166. "corrupt inode %Lu (bad size %d for local fork, size = %d).",
  167. (unsigned long long) ip->i_ino, size,
  168. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
  169. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  170. "xfs_iformat_local", dip, sizeof(*dip),
  171. __this_address);
  172. return -EFSCORRUPTED;
  173. }
  174. xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
  175. return 0;
  176. }
  177. /*
  178. * The file consists of a set of extents all of which fit into the on-disk
  179. * inode.
  180. */
  181. STATIC int
  182. xfs_iformat_extents(
  183. struct xfs_inode *ip,
  184. struct xfs_dinode *dip,
  185. int whichfork)
  186. {
  187. struct xfs_mount *mp = ip->i_mount;
  188. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  189. int state = xfs_bmap_fork_to_state(whichfork);
  190. int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
  191. int size = nex * sizeof(xfs_bmbt_rec_t);
  192. struct xfs_iext_cursor icur;
  193. struct xfs_bmbt_rec *dp;
  194. struct xfs_bmbt_irec new;
  195. int i;
  196. /*
  197. * If the number of extents is unreasonable, then something is wrong and
  198. * we just bail out rather than crash in kmem_alloc() or memcpy() below.
  199. */
  200. if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
  201. xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
  202. (unsigned long long) ip->i_ino, nex);
  203. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  204. "xfs_iformat_extents(1)", dip, sizeof(*dip),
  205. __this_address);
  206. return -EFSCORRUPTED;
  207. }
  208. ifp->if_real_bytes = 0;
  209. ifp->if_bytes = 0;
  210. ifp->if_u1.if_root = NULL;
  211. ifp->if_height = 0;
  212. if (size) {
  213. dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
  214. xfs_iext_first(ifp, &icur);
  215. for (i = 0; i < nex; i++, dp++) {
  216. xfs_failaddr_t fa;
  217. xfs_bmbt_disk_get_all(dp, &new);
  218. fa = xfs_bmap_validate_extent(ip, whichfork, &new);
  219. if (fa) {
  220. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  221. "xfs_iformat_extents(2)",
  222. dp, sizeof(*dp), fa);
  223. return -EFSCORRUPTED;
  224. }
  225. xfs_iext_insert(ip, &icur, &new, state);
  226. trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
  227. xfs_iext_next(ifp, &icur);
  228. }
  229. }
  230. ifp->if_flags |= XFS_IFEXTENTS;
  231. return 0;
  232. }
  233. /*
  234. * The file has too many extents to fit into
  235. * the inode, so they are in B-tree format.
  236. * Allocate a buffer for the root of the B-tree
  237. * and copy the root into it. The i_extents
  238. * field will remain NULL until all of the
  239. * extents are read in (when they are needed).
  240. */
  241. STATIC int
  242. xfs_iformat_btree(
  243. xfs_inode_t *ip,
  244. xfs_dinode_t *dip,
  245. int whichfork)
  246. {
  247. struct xfs_mount *mp = ip->i_mount;
  248. xfs_bmdr_block_t *dfp;
  249. xfs_ifork_t *ifp;
  250. /* REFERENCED */
  251. int nrecs;
  252. int size;
  253. int level;
  254. ifp = XFS_IFORK_PTR(ip, whichfork);
  255. dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
  256. size = XFS_BMAP_BROOT_SPACE(mp, dfp);
  257. nrecs = be16_to_cpu(dfp->bb_numrecs);
  258. level = be16_to_cpu(dfp->bb_level);
  259. /*
  260. * blow out if -- fork has less extents than can fit in
  261. * fork (fork shouldn't be a btree format), root btree
  262. * block has more records than can fit into the fork,
  263. * or the number of extents is greater than the number of
  264. * blocks.
  265. */
  266. if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
  267. XFS_IFORK_MAXEXT(ip, whichfork) ||
  268. nrecs == 0 ||
  269. XFS_BMDR_SPACE_CALC(nrecs) >
  270. XFS_DFORK_SIZE(dip, mp, whichfork) ||
  271. XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
  272. level == 0 || level > XFS_BTREE_MAXLEVELS) {
  273. xfs_warn(mp, "corrupt inode %Lu (btree).",
  274. (unsigned long long) ip->i_ino);
  275. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  276. "xfs_iformat_btree", dfp, size,
  277. __this_address);
  278. return -EFSCORRUPTED;
  279. }
  280. ifp->if_broot_bytes = size;
  281. ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
  282. ASSERT(ifp->if_broot != NULL);
  283. /*
  284. * Copy and convert from the on-disk structure
  285. * to the in-memory structure.
  286. */
  287. xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
  288. ifp->if_broot, size);
  289. ifp->if_flags &= ~XFS_IFEXTENTS;
  290. ifp->if_flags |= XFS_IFBROOT;
  291. ifp->if_real_bytes = 0;
  292. ifp->if_bytes = 0;
  293. ifp->if_u1.if_root = NULL;
  294. ifp->if_height = 0;
  295. return 0;
  296. }
  297. /*
  298. * Reallocate the space for if_broot based on the number of records
  299. * being added or deleted as indicated in rec_diff. Move the records
  300. * and pointers in if_broot to fit the new size. When shrinking this
  301. * will eliminate holes between the records and pointers created by
  302. * the caller. When growing this will create holes to be filled in
  303. * by the caller.
  304. *
  305. * The caller must not request to add more records than would fit in
  306. * the on-disk inode root. If the if_broot is currently NULL, then
  307. * if we are adding records, one will be allocated. The caller must also
  308. * not request that the number of records go below zero, although
  309. * it can go to zero.
  310. *
  311. * ip -- the inode whose if_broot area is changing
  312. * ext_diff -- the change in the number of records, positive or negative,
  313. * requested for the if_broot array.
  314. */
  315. void
  316. xfs_iroot_realloc(
  317. xfs_inode_t *ip,
  318. int rec_diff,
  319. int whichfork)
  320. {
  321. struct xfs_mount *mp = ip->i_mount;
  322. int cur_max;
  323. xfs_ifork_t *ifp;
  324. struct xfs_btree_block *new_broot;
  325. int new_max;
  326. size_t new_size;
  327. char *np;
  328. char *op;
  329. /*
  330. * Handle the degenerate case quietly.
  331. */
  332. if (rec_diff == 0) {
  333. return;
  334. }
  335. ifp = XFS_IFORK_PTR(ip, whichfork);
  336. if (rec_diff > 0) {
  337. /*
  338. * If there wasn't any memory allocated before, just
  339. * allocate it now and get out.
  340. */
  341. if (ifp->if_broot_bytes == 0) {
  342. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
  343. ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  344. ifp->if_broot_bytes = (int)new_size;
  345. return;
  346. }
  347. /*
  348. * If there is already an existing if_broot, then we need
  349. * to realloc() it and shift the pointers to their new
  350. * location. The records don't change location because
  351. * they are kept butted up against the btree block header.
  352. */
  353. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  354. new_max = cur_max + rec_diff;
  355. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  356. ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
  357. KM_SLEEP | KM_NOFS);
  358. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  359. ifp->if_broot_bytes);
  360. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  361. (int)new_size);
  362. ifp->if_broot_bytes = (int)new_size;
  363. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  364. XFS_IFORK_SIZE(ip, whichfork));
  365. memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
  366. return;
  367. }
  368. /*
  369. * rec_diff is less than 0. In this case, we are shrinking the
  370. * if_broot buffer. It must already exist. If we go to zero
  371. * records, just get rid of the root and clear the status bit.
  372. */
  373. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  374. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  375. new_max = cur_max + rec_diff;
  376. ASSERT(new_max >= 0);
  377. if (new_max > 0)
  378. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  379. else
  380. new_size = 0;
  381. if (new_size > 0) {
  382. new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  383. /*
  384. * First copy over the btree block header.
  385. */
  386. memcpy(new_broot, ifp->if_broot,
  387. XFS_BMBT_BLOCK_LEN(ip->i_mount));
  388. } else {
  389. new_broot = NULL;
  390. ifp->if_flags &= ~XFS_IFBROOT;
  391. }
  392. /*
  393. * Only copy the records and pointers if there are any.
  394. */
  395. if (new_max > 0) {
  396. /*
  397. * First copy the records.
  398. */
  399. op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
  400. np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
  401. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  402. /*
  403. * Then copy the pointers.
  404. */
  405. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  406. ifp->if_broot_bytes);
  407. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
  408. (int)new_size);
  409. memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
  410. }
  411. kmem_free(ifp->if_broot);
  412. ifp->if_broot = new_broot;
  413. ifp->if_broot_bytes = (int)new_size;
  414. if (ifp->if_broot)
  415. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  416. XFS_IFORK_SIZE(ip, whichfork));
  417. return;
  418. }
  419. /*
  420. * This is called when the amount of space needed for if_data
  421. * is increased or decreased. The change in size is indicated by
  422. * the number of bytes that need to be added or deleted in the
  423. * byte_diff parameter.
  424. *
  425. * If the amount of space needed has decreased below the size of the
  426. * inline buffer, then switch to using the inline buffer. Otherwise,
  427. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  428. * to what is needed.
  429. *
  430. * ip -- the inode whose if_data area is changing
  431. * byte_diff -- the change in the number of bytes, positive or negative,
  432. * requested for the if_data array.
  433. */
  434. void
  435. xfs_idata_realloc(
  436. xfs_inode_t *ip,
  437. int byte_diff,
  438. int whichfork)
  439. {
  440. xfs_ifork_t *ifp;
  441. int new_size;
  442. int real_size;
  443. if (byte_diff == 0) {
  444. return;
  445. }
  446. ifp = XFS_IFORK_PTR(ip, whichfork);
  447. new_size = (int)ifp->if_bytes + byte_diff;
  448. ASSERT(new_size >= 0);
  449. if (new_size == 0) {
  450. kmem_free(ifp->if_u1.if_data);
  451. ifp->if_u1.if_data = NULL;
  452. real_size = 0;
  453. } else {
  454. /*
  455. * Stuck with malloc/realloc.
  456. * For inline data, the underlying buffer must be
  457. * a multiple of 4 bytes in size so that it can be
  458. * logged and stay on word boundaries. We enforce
  459. * that here.
  460. */
  461. real_size = roundup(new_size, 4);
  462. if (ifp->if_u1.if_data == NULL) {
  463. ASSERT(ifp->if_real_bytes == 0);
  464. ifp->if_u1.if_data = kmem_alloc(real_size,
  465. KM_SLEEP | KM_NOFS);
  466. } else {
  467. /*
  468. * Only do the realloc if the underlying size
  469. * is really changing.
  470. */
  471. if (ifp->if_real_bytes != real_size) {
  472. ifp->if_u1.if_data =
  473. kmem_realloc(ifp->if_u1.if_data,
  474. real_size,
  475. KM_SLEEP | KM_NOFS);
  476. }
  477. }
  478. }
  479. ifp->if_real_bytes = real_size;
  480. ifp->if_bytes = new_size;
  481. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  482. }
  483. void
  484. xfs_idestroy_fork(
  485. xfs_inode_t *ip,
  486. int whichfork)
  487. {
  488. xfs_ifork_t *ifp;
  489. ifp = XFS_IFORK_PTR(ip, whichfork);
  490. if (ifp->if_broot != NULL) {
  491. kmem_free(ifp->if_broot);
  492. ifp->if_broot = NULL;
  493. }
  494. /*
  495. * If the format is local, then we can't have an extents
  496. * array so just look for an inline data array. If we're
  497. * not local then we may or may not have an extents list,
  498. * so check and free it up if we do.
  499. */
  500. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  501. if (ifp->if_u1.if_data != NULL) {
  502. ASSERT(ifp->if_real_bytes != 0);
  503. kmem_free(ifp->if_u1.if_data);
  504. ifp->if_u1.if_data = NULL;
  505. ifp->if_real_bytes = 0;
  506. }
  507. } else if ((ifp->if_flags & XFS_IFEXTENTS) && ifp->if_height) {
  508. xfs_iext_destroy(ifp);
  509. }
  510. ASSERT(ifp->if_real_bytes == 0);
  511. if (whichfork == XFS_ATTR_FORK) {
  512. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  513. ip->i_afp = NULL;
  514. } else if (whichfork == XFS_COW_FORK) {
  515. kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
  516. ip->i_cowfp = NULL;
  517. }
  518. }
  519. /*
  520. * Convert in-core extents to on-disk form
  521. *
  522. * In the case of the data fork, the in-core and on-disk fork sizes can be
  523. * different due to delayed allocation extents. We only copy on-disk extents
  524. * here, so callers must always use the physical fork size to determine the
  525. * size of the buffer passed to this routine. We will return the size actually
  526. * used.
  527. */
  528. int
  529. xfs_iextents_copy(
  530. struct xfs_inode *ip,
  531. struct xfs_bmbt_rec *dp,
  532. int whichfork)
  533. {
  534. int state = xfs_bmap_fork_to_state(whichfork);
  535. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  536. struct xfs_iext_cursor icur;
  537. struct xfs_bmbt_irec rec;
  538. int copied = 0;
  539. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
  540. ASSERT(ifp->if_bytes > 0);
  541. for_each_xfs_iext(ifp, &icur, &rec) {
  542. if (isnullstartblock(rec.br_startblock))
  543. continue;
  544. ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
  545. xfs_bmbt_disk_set_all(dp, &rec);
  546. trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
  547. copied += sizeof(struct xfs_bmbt_rec);
  548. dp++;
  549. }
  550. ASSERT(copied > 0);
  551. ASSERT(copied <= ifp->if_bytes);
  552. return copied;
  553. }
  554. /*
  555. * Each of the following cases stores data into the same region
  556. * of the on-disk inode, so only one of them can be valid at
  557. * any given time. While it is possible to have conflicting formats
  558. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  559. * in EXTENTS format, this can only happen when the fork has
  560. * changed formats after being modified but before being flushed.
  561. * In these cases, the format always takes precedence, because the
  562. * format indicates the current state of the fork.
  563. */
  564. void
  565. xfs_iflush_fork(
  566. xfs_inode_t *ip,
  567. xfs_dinode_t *dip,
  568. xfs_inode_log_item_t *iip,
  569. int whichfork)
  570. {
  571. char *cp;
  572. xfs_ifork_t *ifp;
  573. xfs_mount_t *mp;
  574. static const short brootflag[2] =
  575. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  576. static const short dataflag[2] =
  577. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  578. static const short extflag[2] =
  579. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  580. if (!iip)
  581. return;
  582. ifp = XFS_IFORK_PTR(ip, whichfork);
  583. /*
  584. * This can happen if we gave up in iformat in an error path,
  585. * for the attribute fork.
  586. */
  587. if (!ifp) {
  588. ASSERT(whichfork == XFS_ATTR_FORK);
  589. return;
  590. }
  591. cp = XFS_DFORK_PTR(dip, whichfork);
  592. mp = ip->i_mount;
  593. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  594. case XFS_DINODE_FMT_LOCAL:
  595. if ((iip->ili_fields & dataflag[whichfork]) &&
  596. (ifp->if_bytes > 0)) {
  597. ASSERT(ifp->if_u1.if_data != NULL);
  598. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  599. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  600. }
  601. break;
  602. case XFS_DINODE_FMT_EXTENTS:
  603. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  604. !(iip->ili_fields & extflag[whichfork]));
  605. if ((iip->ili_fields & extflag[whichfork]) &&
  606. (ifp->if_bytes > 0)) {
  607. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  608. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  609. whichfork);
  610. }
  611. break;
  612. case XFS_DINODE_FMT_BTREE:
  613. if ((iip->ili_fields & brootflag[whichfork]) &&
  614. (ifp->if_broot_bytes > 0)) {
  615. ASSERT(ifp->if_broot != NULL);
  616. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  617. XFS_IFORK_SIZE(ip, whichfork));
  618. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  619. (xfs_bmdr_block_t *)cp,
  620. XFS_DFORK_SIZE(dip, mp, whichfork));
  621. }
  622. break;
  623. case XFS_DINODE_FMT_DEV:
  624. if (iip->ili_fields & XFS_ILOG_DEV) {
  625. ASSERT(whichfork == XFS_DATA_FORK);
  626. xfs_dinode_put_rdev(dip,
  627. linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
  628. }
  629. break;
  630. default:
  631. ASSERT(0);
  632. break;
  633. }
  634. }
  635. /* Convert bmap state flags to an inode fork. */
  636. struct xfs_ifork *
  637. xfs_iext_state_to_fork(
  638. struct xfs_inode *ip,
  639. int state)
  640. {
  641. if (state & BMAP_COWFORK)
  642. return ip->i_cowfp;
  643. else if (state & BMAP_ATTRFORK)
  644. return ip->i_afp;
  645. return &ip->i_df;
  646. }
  647. /*
  648. * Initialize an inode's copy-on-write fork.
  649. */
  650. void
  651. xfs_ifork_init_cow(
  652. struct xfs_inode *ip)
  653. {
  654. if (ip->i_cowfp)
  655. return;
  656. ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
  657. KM_SLEEP | KM_NOFS);
  658. ip->i_cowfp->if_flags = XFS_IFEXTENTS;
  659. ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
  660. ip->i_cnextents = 0;
  661. }
  662. /* Default fork content verifiers. */
  663. struct xfs_ifork_ops xfs_default_ifork_ops = {
  664. .verify_attr = xfs_attr_shortform_verify,
  665. .verify_dir = xfs_dir2_sf_verify,
  666. .verify_symlink = xfs_symlink_shortform_verify,
  667. };
  668. /* Verify the inline contents of the data fork of an inode. */
  669. xfs_failaddr_t
  670. xfs_ifork_verify_data(
  671. struct xfs_inode *ip,
  672. struct xfs_ifork_ops *ops)
  673. {
  674. /* Non-local data fork, we're done. */
  675. if (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
  676. return NULL;
  677. /* Check the inline data fork if there is one. */
  678. switch (VFS_I(ip)->i_mode & S_IFMT) {
  679. case S_IFDIR:
  680. return ops->verify_dir(ip);
  681. case S_IFLNK:
  682. return ops->verify_symlink(ip);
  683. default:
  684. return NULL;
  685. }
  686. }
  687. /* Verify the inline contents of the attr fork of an inode. */
  688. xfs_failaddr_t
  689. xfs_ifork_verify_attr(
  690. struct xfs_inode *ip,
  691. struct xfs_ifork_ops *ops)
  692. {
  693. /* There has to be an attr fork allocated if aformat is local. */
  694. if (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
  695. return NULL;
  696. if (!XFS_IFORK_PTR(ip, XFS_ATTR_FORK))
  697. return __this_address;
  698. return ops->verify_attr(ip);
  699. }