xfs_inode_fork.c 20 KB

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