xfs_inode_fork.c 21 KB

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