xfs_inode_fork.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  146. ifp->if_flags |= XFS_IFINLINE;
  147. }
  148. /*
  149. * The file is in-lined in the on-disk inode.
  150. */
  151. STATIC int
  152. xfs_iformat_local(
  153. xfs_inode_t *ip,
  154. xfs_dinode_t *dip,
  155. int whichfork,
  156. int size)
  157. {
  158. /*
  159. * If the size is unreasonable, then something
  160. * is wrong and we just bail out rather than crash in
  161. * kmem_alloc() or memcpy() below.
  162. */
  163. if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  164. xfs_warn(ip->i_mount,
  165. "corrupt inode %Lu (bad size %d for local fork, size = %d).",
  166. (unsigned long long) ip->i_ino, size,
  167. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
  168. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  169. "xfs_iformat_local", dip, sizeof(*dip),
  170. __this_address);
  171. return -EFSCORRUPTED;
  172. }
  173. xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
  174. return 0;
  175. }
  176. /*
  177. * The file consists of a set of extents all of which fit into the on-disk
  178. * inode.
  179. */
  180. STATIC int
  181. xfs_iformat_extents(
  182. struct xfs_inode *ip,
  183. struct xfs_dinode *dip,
  184. int whichfork)
  185. {
  186. struct xfs_mount *mp = ip->i_mount;
  187. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  188. int state = xfs_bmap_fork_to_state(whichfork);
  189. int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
  190. int size = nex * sizeof(xfs_bmbt_rec_t);
  191. struct xfs_iext_cursor icur;
  192. struct xfs_bmbt_rec *dp;
  193. struct xfs_bmbt_irec new;
  194. int i;
  195. /*
  196. * If the number of extents is unreasonable, then something is wrong and
  197. * we just bail out rather than crash in kmem_alloc() or memcpy() below.
  198. */
  199. if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
  200. xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
  201. (unsigned long long) ip->i_ino, nex);
  202. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  203. "xfs_iformat_extents(1)", dip, sizeof(*dip),
  204. __this_address);
  205. return -EFSCORRUPTED;
  206. }
  207. ifp->if_bytes = 0;
  208. ifp->if_u1.if_root = NULL;
  209. ifp->if_height = 0;
  210. if (size) {
  211. dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
  212. xfs_iext_first(ifp, &icur);
  213. for (i = 0; i < nex; i++, dp++) {
  214. xfs_failaddr_t fa;
  215. xfs_bmbt_disk_get_all(dp, &new);
  216. fa = xfs_bmap_validate_extent(ip, whichfork, &new);
  217. if (fa) {
  218. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  219. "xfs_iformat_extents(2)",
  220. dp, sizeof(*dp), fa);
  221. return -EFSCORRUPTED;
  222. }
  223. xfs_iext_insert(ip, &icur, &new, state);
  224. trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
  225. xfs_iext_next(ifp, &icur);
  226. }
  227. }
  228. ifp->if_flags |= XFS_IFEXTENTS;
  229. return 0;
  230. }
  231. /*
  232. * The file has too many extents to fit into
  233. * the inode, so they are in B-tree format.
  234. * Allocate a buffer for the root of the B-tree
  235. * and copy the root into it. The i_extents
  236. * field will remain NULL until all of the
  237. * extents are read in (when they are needed).
  238. */
  239. STATIC int
  240. xfs_iformat_btree(
  241. xfs_inode_t *ip,
  242. xfs_dinode_t *dip,
  243. int whichfork)
  244. {
  245. struct xfs_mount *mp = ip->i_mount;
  246. xfs_bmdr_block_t *dfp;
  247. struct xfs_ifork *ifp;
  248. /* REFERENCED */
  249. int nrecs;
  250. int size;
  251. int level;
  252. ifp = XFS_IFORK_PTR(ip, whichfork);
  253. dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
  254. size = XFS_BMAP_BROOT_SPACE(mp, dfp);
  255. nrecs = be16_to_cpu(dfp->bb_numrecs);
  256. level = be16_to_cpu(dfp->bb_level);
  257. /*
  258. * blow out if -- fork has less extents than can fit in
  259. * fork (fork shouldn't be a btree format), root btree
  260. * block has more records than can fit into the fork,
  261. * or the number of extents is greater than the number of
  262. * blocks.
  263. */
  264. if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
  265. XFS_IFORK_MAXEXT(ip, whichfork) ||
  266. nrecs == 0 ||
  267. XFS_BMDR_SPACE_CALC(nrecs) >
  268. XFS_DFORK_SIZE(dip, mp, whichfork) ||
  269. XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
  270. level == 0 || level > XFS_BTREE_MAXLEVELS) {
  271. xfs_warn(mp, "corrupt inode %Lu (btree).",
  272. (unsigned long long) ip->i_ino);
  273. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  274. "xfs_iformat_btree", dfp, size,
  275. __this_address);
  276. return -EFSCORRUPTED;
  277. }
  278. ifp->if_broot_bytes = size;
  279. ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
  280. ASSERT(ifp->if_broot != NULL);
  281. /*
  282. * Copy and convert from the on-disk structure
  283. * to the in-memory structure.
  284. */
  285. xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
  286. ifp->if_broot, size);
  287. ifp->if_flags &= ~XFS_IFEXTENTS;
  288. ifp->if_flags |= XFS_IFBROOT;
  289. ifp->if_bytes = 0;
  290. ifp->if_u1.if_root = NULL;
  291. ifp->if_height = 0;
  292. return 0;
  293. }
  294. /*
  295. * Reallocate the space for if_broot based on the number of records
  296. * being added or deleted as indicated in rec_diff. Move the records
  297. * and pointers in if_broot to fit the new size. When shrinking this
  298. * will eliminate holes between the records and pointers created by
  299. * the caller. When growing this will create holes to be filled in
  300. * by the caller.
  301. *
  302. * The caller must not request to add more records than would fit in
  303. * the on-disk inode root. If the if_broot is currently NULL, then
  304. * if we are adding records, one will be allocated. The caller must also
  305. * not request that the number of records go below zero, although
  306. * it can go to zero.
  307. *
  308. * ip -- the inode whose if_broot area is changing
  309. * ext_diff -- the change in the number of records, positive or negative,
  310. * requested for the if_broot array.
  311. */
  312. void
  313. xfs_iroot_realloc(
  314. xfs_inode_t *ip,
  315. int rec_diff,
  316. int whichfork)
  317. {
  318. struct xfs_mount *mp = ip->i_mount;
  319. int cur_max;
  320. struct xfs_ifork *ifp;
  321. struct xfs_btree_block *new_broot;
  322. int new_max;
  323. size_t new_size;
  324. char *np;
  325. char *op;
  326. /*
  327. * Handle the degenerate case quietly.
  328. */
  329. if (rec_diff == 0) {
  330. return;
  331. }
  332. ifp = XFS_IFORK_PTR(ip, whichfork);
  333. if (rec_diff > 0) {
  334. /*
  335. * If there wasn't any memory allocated before, just
  336. * allocate it now and get out.
  337. */
  338. if (ifp->if_broot_bytes == 0) {
  339. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
  340. ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  341. ifp->if_broot_bytes = (int)new_size;
  342. return;
  343. }
  344. /*
  345. * If there is already an existing if_broot, then we need
  346. * to realloc() it and shift the pointers to their new
  347. * location. The records don't change location because
  348. * they are kept butted up against the btree block header.
  349. */
  350. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  351. new_max = cur_max + rec_diff;
  352. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  353. ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
  354. KM_SLEEP | KM_NOFS);
  355. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  356. ifp->if_broot_bytes);
  357. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  358. (int)new_size);
  359. ifp->if_broot_bytes = (int)new_size;
  360. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  361. XFS_IFORK_SIZE(ip, whichfork));
  362. memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
  363. return;
  364. }
  365. /*
  366. * rec_diff is less than 0. In this case, we are shrinking the
  367. * if_broot buffer. It must already exist. If we go to zero
  368. * records, just get rid of the root and clear the status bit.
  369. */
  370. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  371. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  372. new_max = cur_max + rec_diff;
  373. ASSERT(new_max >= 0);
  374. if (new_max > 0)
  375. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  376. else
  377. new_size = 0;
  378. if (new_size > 0) {
  379. new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  380. /*
  381. * First copy over the btree block header.
  382. */
  383. memcpy(new_broot, ifp->if_broot,
  384. XFS_BMBT_BLOCK_LEN(ip->i_mount));
  385. } else {
  386. new_broot = NULL;
  387. ifp->if_flags &= ~XFS_IFBROOT;
  388. }
  389. /*
  390. * Only copy the records and pointers if there are any.
  391. */
  392. if (new_max > 0) {
  393. /*
  394. * First copy the records.
  395. */
  396. op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
  397. np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
  398. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  399. /*
  400. * Then copy the pointers.
  401. */
  402. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  403. ifp->if_broot_bytes);
  404. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
  405. (int)new_size);
  406. memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
  407. }
  408. kmem_free(ifp->if_broot);
  409. ifp->if_broot = new_broot;
  410. ifp->if_broot_bytes = (int)new_size;
  411. if (ifp->if_broot)
  412. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  413. XFS_IFORK_SIZE(ip, whichfork));
  414. return;
  415. }
  416. /*
  417. * This is called when the amount of space needed for if_data
  418. * is increased or decreased. The change in size is indicated by
  419. * the number of bytes that need to be added or deleted in the
  420. * byte_diff parameter.
  421. *
  422. * If the amount of space needed has decreased below the size of the
  423. * inline buffer, then switch to using the inline buffer. Otherwise,
  424. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  425. * to what is needed.
  426. *
  427. * ip -- the inode whose if_data area is changing
  428. * byte_diff -- the change in the number of bytes, positive or negative,
  429. * requested for the if_data array.
  430. */
  431. void
  432. xfs_idata_realloc(
  433. struct xfs_inode *ip,
  434. int byte_diff,
  435. int whichfork)
  436. {
  437. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  438. int new_size = (int)ifp->if_bytes + byte_diff;
  439. ASSERT(new_size >= 0);
  440. ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));
  441. if (byte_diff == 0)
  442. return;
  443. if (new_size == 0) {
  444. kmem_free(ifp->if_u1.if_data);
  445. ifp->if_u1.if_data = NULL;
  446. ifp->if_bytes = 0;
  447. return;
  448. }
  449. /*
  450. * For inline data, the underlying buffer must be a multiple of 4 bytes
  451. * in size so that it can be logged and stay on word boundaries.
  452. * We enforce that here.
  453. */
  454. ifp->if_u1.if_data = kmem_realloc(ifp->if_u1.if_data,
  455. roundup(new_size, 4), KM_SLEEP | KM_NOFS);
  456. ifp->if_bytes = new_size;
  457. }
  458. void
  459. xfs_idestroy_fork(
  460. xfs_inode_t *ip,
  461. int whichfork)
  462. {
  463. struct xfs_ifork *ifp;
  464. ifp = XFS_IFORK_PTR(ip, whichfork);
  465. if (ifp->if_broot != NULL) {
  466. kmem_free(ifp->if_broot);
  467. ifp->if_broot = NULL;
  468. }
  469. /*
  470. * If the format is local, then we can't have an extents
  471. * array so just look for an inline data array. If we're
  472. * not local then we may or may not have an extents list,
  473. * so check and free it up if we do.
  474. */
  475. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  476. if (ifp->if_u1.if_data != NULL) {
  477. kmem_free(ifp->if_u1.if_data);
  478. ifp->if_u1.if_data = NULL;
  479. }
  480. } else if ((ifp->if_flags & XFS_IFEXTENTS) && ifp->if_height) {
  481. xfs_iext_destroy(ifp);
  482. }
  483. if (whichfork == XFS_ATTR_FORK) {
  484. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  485. ip->i_afp = NULL;
  486. } else if (whichfork == XFS_COW_FORK) {
  487. kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
  488. ip->i_cowfp = NULL;
  489. }
  490. }
  491. /*
  492. * Convert in-core extents to on-disk form
  493. *
  494. * In the case of the data fork, the in-core and on-disk fork sizes can be
  495. * different due to delayed allocation extents. We only copy on-disk extents
  496. * here, so callers must always use the physical fork size to determine the
  497. * size of the buffer passed to this routine. We will return the size actually
  498. * used.
  499. */
  500. int
  501. xfs_iextents_copy(
  502. struct xfs_inode *ip,
  503. struct xfs_bmbt_rec *dp,
  504. int whichfork)
  505. {
  506. int state = xfs_bmap_fork_to_state(whichfork);
  507. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  508. struct xfs_iext_cursor icur;
  509. struct xfs_bmbt_irec rec;
  510. int copied = 0;
  511. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
  512. ASSERT(ifp->if_bytes > 0);
  513. for_each_xfs_iext(ifp, &icur, &rec) {
  514. if (isnullstartblock(rec.br_startblock))
  515. continue;
  516. ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
  517. xfs_bmbt_disk_set_all(dp, &rec);
  518. trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
  519. copied += sizeof(struct xfs_bmbt_rec);
  520. dp++;
  521. }
  522. ASSERT(copied > 0);
  523. ASSERT(copied <= ifp->if_bytes);
  524. return copied;
  525. }
  526. /*
  527. * Each of the following cases stores data into the same region
  528. * of the on-disk inode, so only one of them can be valid at
  529. * any given time. While it is possible to have conflicting formats
  530. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  531. * in EXTENTS format, this can only happen when the fork has
  532. * changed formats after being modified but before being flushed.
  533. * In these cases, the format always takes precedence, because the
  534. * format indicates the current state of the fork.
  535. */
  536. void
  537. xfs_iflush_fork(
  538. xfs_inode_t *ip,
  539. xfs_dinode_t *dip,
  540. xfs_inode_log_item_t *iip,
  541. int whichfork)
  542. {
  543. char *cp;
  544. struct xfs_ifork *ifp;
  545. xfs_mount_t *mp;
  546. static const short brootflag[2] =
  547. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  548. static const short dataflag[2] =
  549. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  550. static const short extflag[2] =
  551. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  552. if (!iip)
  553. return;
  554. ifp = XFS_IFORK_PTR(ip, whichfork);
  555. /*
  556. * This can happen if we gave up in iformat in an error path,
  557. * for the attribute fork.
  558. */
  559. if (!ifp) {
  560. ASSERT(whichfork == XFS_ATTR_FORK);
  561. return;
  562. }
  563. cp = XFS_DFORK_PTR(dip, whichfork);
  564. mp = ip->i_mount;
  565. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  566. case XFS_DINODE_FMT_LOCAL:
  567. if ((iip->ili_fields & dataflag[whichfork]) &&
  568. (ifp->if_bytes > 0)) {
  569. ASSERT(ifp->if_u1.if_data != NULL);
  570. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  571. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  572. }
  573. break;
  574. case XFS_DINODE_FMT_EXTENTS:
  575. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  576. !(iip->ili_fields & extflag[whichfork]));
  577. if ((iip->ili_fields & extflag[whichfork]) &&
  578. (ifp->if_bytes > 0)) {
  579. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  580. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  581. whichfork);
  582. }
  583. break;
  584. case XFS_DINODE_FMT_BTREE:
  585. if ((iip->ili_fields & brootflag[whichfork]) &&
  586. (ifp->if_broot_bytes > 0)) {
  587. ASSERT(ifp->if_broot != NULL);
  588. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  589. XFS_IFORK_SIZE(ip, whichfork));
  590. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  591. (xfs_bmdr_block_t *)cp,
  592. XFS_DFORK_SIZE(dip, mp, whichfork));
  593. }
  594. break;
  595. case XFS_DINODE_FMT_DEV:
  596. if (iip->ili_fields & XFS_ILOG_DEV) {
  597. ASSERT(whichfork == XFS_DATA_FORK);
  598. xfs_dinode_put_rdev(dip,
  599. linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
  600. }
  601. break;
  602. default:
  603. ASSERT(0);
  604. break;
  605. }
  606. }
  607. /* Convert bmap state flags to an inode fork. */
  608. struct xfs_ifork *
  609. xfs_iext_state_to_fork(
  610. struct xfs_inode *ip,
  611. int state)
  612. {
  613. if (state & BMAP_COWFORK)
  614. return ip->i_cowfp;
  615. else if (state & BMAP_ATTRFORK)
  616. return ip->i_afp;
  617. return &ip->i_df;
  618. }
  619. /*
  620. * Initialize an inode's copy-on-write fork.
  621. */
  622. void
  623. xfs_ifork_init_cow(
  624. struct xfs_inode *ip)
  625. {
  626. if (ip->i_cowfp)
  627. return;
  628. ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
  629. KM_SLEEP | KM_NOFS);
  630. ip->i_cowfp->if_flags = XFS_IFEXTENTS;
  631. ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
  632. ip->i_cnextents = 0;
  633. }
  634. /* Default fork content verifiers. */
  635. struct xfs_ifork_ops xfs_default_ifork_ops = {
  636. .verify_attr = xfs_attr_shortform_verify,
  637. .verify_dir = xfs_dir2_sf_verify,
  638. .verify_symlink = xfs_symlink_shortform_verify,
  639. };
  640. /* Verify the inline contents of the data fork of an inode. */
  641. xfs_failaddr_t
  642. xfs_ifork_verify_data(
  643. struct xfs_inode *ip,
  644. struct xfs_ifork_ops *ops)
  645. {
  646. /* Non-local data fork, we're done. */
  647. if (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
  648. return NULL;
  649. /* Check the inline data fork if there is one. */
  650. switch (VFS_I(ip)->i_mode & S_IFMT) {
  651. case S_IFDIR:
  652. return ops->verify_dir(ip);
  653. case S_IFLNK:
  654. return ops->verify_symlink(ip);
  655. default:
  656. return NULL;
  657. }
  658. }
  659. /* Verify the inline contents of the attr fork of an inode. */
  660. xfs_failaddr_t
  661. xfs_ifork_verify_attr(
  662. struct xfs_inode *ip,
  663. struct xfs_ifork_ops *ops)
  664. {
  665. /* There has to be an attr fork allocated if aformat is local. */
  666. if (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
  667. return NULL;
  668. if (!XFS_IFORK_PTR(ip, XFS_ATTR_FORK))
  669. return __this_address;
  670. return ops->verify_attr(ip);
  671. }