xfs_inode_fork.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_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_da_format.h"
  22. #include "xfs_da_btree.h"
  23. #include "xfs_dir2_priv.h"
  24. #include "xfs_attr_leaf.h"
  25. #include "xfs_types.h"
  26. #include "xfs_errortag.h"
  27. #include "xfs_health.h"
  28. #include "xfs_symlink_remote.h"
  29. struct kmem_cache *xfs_ifork_cache;
  30. void
  31. xfs_init_local_fork(
  32. struct xfs_inode *ip,
  33. int whichfork,
  34. const void *data,
  35. int64_t size)
  36. {
  37. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  38. int mem_size = size;
  39. bool zero_terminate;
  40. /*
  41. * If we are using the local fork to store a symlink body we need to
  42. * zero-terminate it so that we can pass it back to the VFS directly.
  43. * Overallocate the in-memory fork by one for that and add a zero
  44. * to terminate it below.
  45. */
  46. zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
  47. if (zero_terminate)
  48. mem_size++;
  49. if (size) {
  50. char *new_data = kmalloc(mem_size,
  51. GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
  52. memcpy(new_data, data, size);
  53. if (zero_terminate)
  54. new_data[size] = '\0';
  55. ifp->if_data = new_data;
  56. } else {
  57. ifp->if_data = NULL;
  58. }
  59. ifp->if_bytes = size;
  60. }
  61. /*
  62. * The file is in-lined in the on-disk inode.
  63. */
  64. STATIC int
  65. xfs_iformat_local(
  66. struct xfs_inode *ip,
  67. struct xfs_dinode *dip,
  68. int whichfork,
  69. int size)
  70. {
  71. /*
  72. * If the size is unreasonable, then something
  73. * is wrong and we just bail out rather than crash in
  74. * kmalloc() or memcpy() below.
  75. */
  76. if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  77. xfs_warn(ip->i_mount,
  78. "corrupt inode %llu (bad size %d for local fork, size = %zd).",
  79. (unsigned long long) ip->i_ino, size,
  80. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
  81. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  82. "xfs_iformat_local", dip, sizeof(*dip),
  83. __this_address);
  84. xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
  85. return -EFSCORRUPTED;
  86. }
  87. xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
  88. return 0;
  89. }
  90. /*
  91. * The file consists of a set of extents all of which fit into the on-disk
  92. * inode.
  93. */
  94. STATIC int
  95. xfs_iformat_extents(
  96. struct xfs_inode *ip,
  97. struct xfs_dinode *dip,
  98. int whichfork)
  99. {
  100. struct xfs_mount *mp = ip->i_mount;
  101. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  102. int state = xfs_bmap_fork_to_state(whichfork);
  103. xfs_extnum_t nex = xfs_dfork_nextents(dip, whichfork);
  104. int size = nex * sizeof(xfs_bmbt_rec_t);
  105. struct xfs_iext_cursor icur;
  106. struct xfs_bmbt_rec *dp;
  107. struct xfs_bmbt_irec new;
  108. int i;
  109. /*
  110. * If the number of extents is unreasonable, then something is wrong and
  111. * we just bail out rather than crash in kmalloc() or memcpy() below.
  112. */
  113. if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
  114. xfs_warn(ip->i_mount, "corrupt inode %llu ((a)extents = %llu).",
  115. ip->i_ino, nex);
  116. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  117. "xfs_iformat_extents(1)", dip, sizeof(*dip),
  118. __this_address);
  119. xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
  120. return -EFSCORRUPTED;
  121. }
  122. ifp->if_bytes = 0;
  123. ifp->if_data = NULL;
  124. ifp->if_height = 0;
  125. if (size) {
  126. dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
  127. xfs_iext_first(ifp, &icur);
  128. for (i = 0; i < nex; i++, dp++) {
  129. xfs_failaddr_t fa;
  130. xfs_bmbt_disk_get_all(dp, &new);
  131. fa = xfs_bmap_validate_extent(ip, whichfork, &new);
  132. if (fa) {
  133. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  134. "xfs_iformat_extents(2)",
  135. dp, sizeof(*dp), fa);
  136. xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
  137. return xfs_bmap_complain_bad_rec(ip, whichfork,
  138. fa, &new);
  139. }
  140. xfs_iext_insert(ip, &icur, &new, state);
  141. trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
  142. xfs_iext_next(ifp, &icur);
  143. }
  144. }
  145. return 0;
  146. }
  147. /*
  148. * The file has too many extents to fit into
  149. * the inode, so they are in B-tree format.
  150. * Allocate a buffer for the root of the B-tree
  151. * and copy the root into it. The i_extents
  152. * field will remain NULL until all of the
  153. * extents are read in (when they are needed).
  154. */
  155. STATIC int
  156. xfs_iformat_btree(
  157. struct xfs_inode *ip,
  158. struct xfs_dinode *dip,
  159. int whichfork)
  160. {
  161. struct xfs_mount *mp = ip->i_mount;
  162. xfs_bmdr_block_t *dfp;
  163. struct xfs_ifork *ifp;
  164. /* REFERENCED */
  165. int nrecs;
  166. int size;
  167. int level;
  168. ifp = xfs_ifork_ptr(ip, whichfork);
  169. dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
  170. size = xfs_bmap_broot_space(mp, dfp);
  171. nrecs = be16_to_cpu(dfp->bb_numrecs);
  172. level = be16_to_cpu(dfp->bb_level);
  173. /*
  174. * blow out if -- fork has less extents than can fit in
  175. * fork (fork shouldn't be a btree format), root btree
  176. * block has more records than can fit into the fork,
  177. * or the number of extents is greater than the number of
  178. * blocks.
  179. */
  180. if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
  181. nrecs == 0 ||
  182. xfs_bmdr_space_calc(nrecs) >
  183. XFS_DFORK_SIZE(dip, mp, whichfork) ||
  184. ifp->if_nextents > ip->i_nblocks) ||
  185. level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
  186. xfs_warn(mp, "corrupt inode %llu (btree).",
  187. (unsigned long long) ip->i_ino);
  188. xfs_inode_verifier_error(ip, -EFSCORRUPTED,
  189. "xfs_iformat_btree", dfp, size,
  190. __this_address);
  191. xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
  192. return -EFSCORRUPTED;
  193. }
  194. ifp->if_broot_bytes = size;
  195. ifp->if_broot = kmalloc(size,
  196. GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
  197. ASSERT(ifp->if_broot != NULL);
  198. /*
  199. * Copy and convert from the on-disk structure
  200. * to the in-memory structure.
  201. */
  202. xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
  203. ifp->if_broot, size);
  204. ifp->if_bytes = 0;
  205. ifp->if_data = NULL;
  206. ifp->if_height = 0;
  207. return 0;
  208. }
  209. int
  210. xfs_iformat_data_fork(
  211. struct xfs_inode *ip,
  212. struct xfs_dinode *dip)
  213. {
  214. struct inode *inode = VFS_I(ip);
  215. int error;
  216. /*
  217. * Initialize the extent count early, as the per-format routines may
  218. * depend on it. Use release semantics to set needextents /after/ we
  219. * set the format. This ensures that we can use acquire semantics on
  220. * needextents in xfs_need_iread_extents() and be guaranteed to see a
  221. * valid format value after that load.
  222. */
  223. ip->i_df.if_format = dip->di_format;
  224. ip->i_df.if_nextents = xfs_dfork_data_extents(dip);
  225. smp_store_release(&ip->i_df.if_needextents,
  226. ip->i_df.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
  227. switch (inode->i_mode & S_IFMT) {
  228. case S_IFIFO:
  229. case S_IFCHR:
  230. case S_IFBLK:
  231. case S_IFSOCK:
  232. ip->i_disk_size = 0;
  233. inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
  234. return 0;
  235. case S_IFREG:
  236. case S_IFLNK:
  237. case S_IFDIR:
  238. switch (ip->i_df.if_format) {
  239. case XFS_DINODE_FMT_LOCAL:
  240. error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
  241. be64_to_cpu(dip->di_size));
  242. if (!error)
  243. error = xfs_ifork_verify_local_data(ip);
  244. return error;
  245. case XFS_DINODE_FMT_EXTENTS:
  246. return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
  247. case XFS_DINODE_FMT_BTREE:
  248. return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
  249. default:
  250. xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
  251. dip, sizeof(*dip), __this_address);
  252. xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
  253. return -EFSCORRUPTED;
  254. }
  255. break;
  256. default:
  257. xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
  258. sizeof(*dip), __this_address);
  259. xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
  260. return -EFSCORRUPTED;
  261. }
  262. }
  263. static uint16_t
  264. xfs_dfork_attr_shortform_size(
  265. struct xfs_dinode *dip)
  266. {
  267. struct xfs_attr_sf_hdr *sf = XFS_DFORK_APTR(dip);
  268. return be16_to_cpu(sf->totsize);
  269. }
  270. void
  271. xfs_ifork_init_attr(
  272. struct xfs_inode *ip,
  273. enum xfs_dinode_fmt format,
  274. xfs_extnum_t nextents)
  275. {
  276. /*
  277. * Initialize the extent count early, as the per-format routines may
  278. * depend on it. Use release semantics to set needextents /after/ we
  279. * set the format. This ensures that we can use acquire semantics on
  280. * needextents in xfs_need_iread_extents() and be guaranteed to see a
  281. * valid format value after that load.
  282. */
  283. ip->i_af.if_format = format;
  284. ip->i_af.if_nextents = nextents;
  285. smp_store_release(&ip->i_af.if_needextents,
  286. ip->i_af.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
  287. }
  288. void
  289. xfs_ifork_zap_attr(
  290. struct xfs_inode *ip)
  291. {
  292. xfs_idestroy_fork(&ip->i_af);
  293. memset(&ip->i_af, 0, sizeof(struct xfs_ifork));
  294. ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
  295. }
  296. int
  297. xfs_iformat_attr_fork(
  298. struct xfs_inode *ip,
  299. struct xfs_dinode *dip)
  300. {
  301. xfs_extnum_t naextents = xfs_dfork_attr_extents(dip);
  302. int error = 0;
  303. /*
  304. * Initialize the extent count early, as the per-format routines may
  305. * depend on it.
  306. */
  307. xfs_ifork_init_attr(ip, dip->di_aformat, naextents);
  308. switch (ip->i_af.if_format) {
  309. case XFS_DINODE_FMT_LOCAL:
  310. error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
  311. xfs_dfork_attr_shortform_size(dip));
  312. if (!error)
  313. error = xfs_ifork_verify_local_attr(ip);
  314. break;
  315. case XFS_DINODE_FMT_EXTENTS:
  316. error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
  317. break;
  318. case XFS_DINODE_FMT_BTREE:
  319. error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
  320. break;
  321. default:
  322. xfs_inode_verifier_error(ip, error, __func__, dip,
  323. sizeof(*dip), __this_address);
  324. xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
  325. error = -EFSCORRUPTED;
  326. break;
  327. }
  328. if (error)
  329. xfs_ifork_zap_attr(ip);
  330. return error;
  331. }
  332. /*
  333. * Reallocate the space for if_broot based on the number of records
  334. * being added or deleted as indicated in rec_diff. Move the records
  335. * and pointers in if_broot to fit the new size. When shrinking this
  336. * will eliminate holes between the records and pointers created by
  337. * the caller. When growing this will create holes to be filled in
  338. * by the caller.
  339. *
  340. * The caller must not request to add more records than would fit in
  341. * the on-disk inode root. If the if_broot is currently NULL, then
  342. * if we are adding records, one will be allocated. The caller must also
  343. * not request that the number of records go below zero, although
  344. * it can go to zero.
  345. *
  346. * ip -- the inode whose if_broot area is changing
  347. * ext_diff -- the change in the number of records, positive or negative,
  348. * requested for the if_broot array.
  349. */
  350. void
  351. xfs_iroot_realloc(
  352. xfs_inode_t *ip,
  353. int rec_diff,
  354. int whichfork)
  355. {
  356. struct xfs_mount *mp = ip->i_mount;
  357. int cur_max;
  358. struct xfs_ifork *ifp;
  359. struct xfs_btree_block *new_broot;
  360. int new_max;
  361. size_t new_size;
  362. char *np;
  363. char *op;
  364. /*
  365. * Handle the degenerate case quietly.
  366. */
  367. if (rec_diff == 0) {
  368. return;
  369. }
  370. ifp = xfs_ifork_ptr(ip, whichfork);
  371. if (rec_diff > 0) {
  372. /*
  373. * If there wasn't any memory allocated before, just
  374. * allocate it now and get out.
  375. */
  376. if (ifp->if_broot_bytes == 0) {
  377. new_size = xfs_bmap_broot_space_calc(mp, rec_diff);
  378. ifp->if_broot = kmalloc(new_size,
  379. GFP_KERNEL | __GFP_NOFAIL);
  380. ifp->if_broot_bytes = (int)new_size;
  381. return;
  382. }
  383. /*
  384. * If there is already an existing if_broot, then we need
  385. * to realloc() it and shift the pointers to their new
  386. * location. The records don't change location because
  387. * they are kept butted up against the btree block header.
  388. */
  389. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false);
  390. new_max = cur_max + rec_diff;
  391. new_size = xfs_bmap_broot_space_calc(mp, new_max);
  392. ifp->if_broot = krealloc(ifp->if_broot, new_size,
  393. GFP_KERNEL | __GFP_NOFAIL);
  394. op = (char *)xfs_bmap_broot_ptr_addr(mp, ifp->if_broot, 1,
  395. ifp->if_broot_bytes);
  396. np = (char *)xfs_bmap_broot_ptr_addr(mp, ifp->if_broot, 1,
  397. (int)new_size);
  398. ifp->if_broot_bytes = (int)new_size;
  399. ASSERT(xfs_bmap_bmdr_space(ifp->if_broot) <=
  400. xfs_inode_fork_size(ip, whichfork));
  401. memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
  402. return;
  403. }
  404. /*
  405. * rec_diff is less than 0. In this case, we are shrinking the
  406. * if_broot buffer. It must already exist. If we go to zero
  407. * records, just get rid of the root and clear the status bit.
  408. */
  409. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  410. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false);
  411. new_max = cur_max + rec_diff;
  412. ASSERT(new_max >= 0);
  413. if (new_max > 0)
  414. new_size = xfs_bmap_broot_space_calc(mp, new_max);
  415. else
  416. new_size = 0;
  417. if (new_size > 0) {
  418. new_broot = kmalloc(new_size, GFP_KERNEL | __GFP_NOFAIL);
  419. /*
  420. * First copy over the btree block header.
  421. */
  422. memcpy(new_broot, ifp->if_broot,
  423. xfs_bmbt_block_len(ip->i_mount));
  424. } else {
  425. new_broot = NULL;
  426. }
  427. /*
  428. * Only copy the keys and pointers if there are any.
  429. */
  430. if (new_max > 0) {
  431. /*
  432. * First copy the keys.
  433. */
  434. op = (char *)xfs_bmbt_key_addr(mp, ifp->if_broot, 1);
  435. np = (char *)xfs_bmbt_key_addr(mp, new_broot, 1);
  436. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_key_t));
  437. /*
  438. * Then copy the pointers.
  439. */
  440. op = (char *)xfs_bmap_broot_ptr_addr(mp, ifp->if_broot, 1,
  441. ifp->if_broot_bytes);
  442. np = (char *)xfs_bmap_broot_ptr_addr(mp, new_broot, 1,
  443. (int)new_size);
  444. memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
  445. }
  446. kfree(ifp->if_broot);
  447. ifp->if_broot = new_broot;
  448. ifp->if_broot_bytes = (int)new_size;
  449. if (ifp->if_broot)
  450. ASSERT(xfs_bmap_bmdr_space(ifp->if_broot) <=
  451. xfs_inode_fork_size(ip, whichfork));
  452. return;
  453. }
  454. /*
  455. * This is called when the amount of space needed for if_data
  456. * is increased or decreased. The change in size is indicated by
  457. * the number of bytes that need to be added or deleted in the
  458. * byte_diff parameter.
  459. *
  460. * If the amount of space needed has decreased below the size of the
  461. * inline buffer, then switch to using the inline buffer. Otherwise,
  462. * use krealloc() or kmalloc() to adjust the size of the buffer
  463. * to what is needed.
  464. *
  465. * ip -- the inode whose if_data area is changing
  466. * byte_diff -- the change in the number of bytes, positive or negative,
  467. * requested for the if_data array.
  468. */
  469. void *
  470. xfs_idata_realloc(
  471. struct xfs_inode *ip,
  472. int64_t byte_diff,
  473. int whichfork)
  474. {
  475. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  476. int64_t new_size = ifp->if_bytes + byte_diff;
  477. ASSERT(new_size >= 0);
  478. ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork));
  479. if (byte_diff) {
  480. ifp->if_data = krealloc(ifp->if_data, new_size,
  481. GFP_KERNEL | __GFP_NOFAIL);
  482. if (new_size == 0)
  483. ifp->if_data = NULL;
  484. ifp->if_bytes = new_size;
  485. }
  486. return ifp->if_data;
  487. }
  488. /* Free all memory and reset a fork back to its initial state. */
  489. void
  490. xfs_idestroy_fork(
  491. struct xfs_ifork *ifp)
  492. {
  493. if (ifp->if_broot != NULL) {
  494. kfree(ifp->if_broot);
  495. ifp->if_broot = NULL;
  496. }
  497. switch (ifp->if_format) {
  498. case XFS_DINODE_FMT_LOCAL:
  499. kfree(ifp->if_data);
  500. ifp->if_data = NULL;
  501. break;
  502. case XFS_DINODE_FMT_EXTENTS:
  503. case XFS_DINODE_FMT_BTREE:
  504. if (ifp->if_height)
  505. xfs_iext_destroy(ifp);
  506. break;
  507. }
  508. }
  509. /*
  510. * Convert in-core extents to on-disk form
  511. *
  512. * In the case of the data fork, the in-core and on-disk fork sizes can be
  513. * different due to delayed allocation extents. We only copy on-disk extents
  514. * here, so callers must always use the physical fork size to determine the
  515. * size of the buffer passed to this routine. We will return the size actually
  516. * used.
  517. */
  518. int
  519. xfs_iextents_copy(
  520. struct xfs_inode *ip,
  521. struct xfs_bmbt_rec *dp,
  522. int whichfork)
  523. {
  524. int state = xfs_bmap_fork_to_state(whichfork);
  525. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  526. struct xfs_iext_cursor icur;
  527. struct xfs_bmbt_irec rec;
  528. int64_t copied = 0;
  529. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED);
  530. ASSERT(ifp->if_bytes > 0);
  531. for_each_xfs_iext(ifp, &icur, &rec) {
  532. if (isnullstartblock(rec.br_startblock))
  533. continue;
  534. ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
  535. xfs_bmbt_disk_set_all(dp, &rec);
  536. trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
  537. copied += sizeof(struct xfs_bmbt_rec);
  538. dp++;
  539. }
  540. ASSERT(copied > 0);
  541. ASSERT(copied <= ifp->if_bytes);
  542. return copied;
  543. }
  544. /*
  545. * Each of the following cases stores data into the same region
  546. * of the on-disk inode, so only one of them can be valid at
  547. * any given time. While it is possible to have conflicting formats
  548. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  549. * in EXTENTS format, this can only happen when the fork has
  550. * changed formats after being modified but before being flushed.
  551. * In these cases, the format always takes precedence, because the
  552. * format indicates the current state of the fork.
  553. */
  554. void
  555. xfs_iflush_fork(
  556. struct xfs_inode *ip,
  557. struct xfs_dinode *dip,
  558. struct xfs_inode_log_item *iip,
  559. int whichfork)
  560. {
  561. char *cp;
  562. struct xfs_ifork *ifp;
  563. xfs_mount_t *mp;
  564. static const short brootflag[2] =
  565. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  566. static const short dataflag[2] =
  567. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  568. static const short extflag[2] =
  569. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  570. if (!iip)
  571. return;
  572. ifp = xfs_ifork_ptr(ip, whichfork);
  573. /*
  574. * This can happen if we gave up in iformat in an error path,
  575. * for the attribute fork.
  576. */
  577. if (!ifp) {
  578. ASSERT(whichfork == XFS_ATTR_FORK);
  579. return;
  580. }
  581. cp = XFS_DFORK_PTR(dip, whichfork);
  582. mp = ip->i_mount;
  583. switch (ifp->if_format) {
  584. case XFS_DINODE_FMT_LOCAL:
  585. if ((iip->ili_fields & dataflag[whichfork]) &&
  586. (ifp->if_bytes > 0)) {
  587. ASSERT(ifp->if_data != NULL);
  588. ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork));
  589. memcpy(cp, ifp->if_data, ifp->if_bytes);
  590. }
  591. break;
  592. case XFS_DINODE_FMT_EXTENTS:
  593. if ((iip->ili_fields & extflag[whichfork]) &&
  594. (ifp->if_bytes > 0)) {
  595. ASSERT(ifp->if_nextents > 0);
  596. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  597. whichfork);
  598. }
  599. break;
  600. case XFS_DINODE_FMT_BTREE:
  601. if ((iip->ili_fields & brootflag[whichfork]) &&
  602. (ifp->if_broot_bytes > 0)) {
  603. ASSERT(ifp->if_broot != NULL);
  604. ASSERT(xfs_bmap_bmdr_space(ifp->if_broot) <=
  605. xfs_inode_fork_size(ip, whichfork));
  606. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  607. (xfs_bmdr_block_t *)cp,
  608. XFS_DFORK_SIZE(dip, mp, whichfork));
  609. }
  610. break;
  611. case XFS_DINODE_FMT_DEV:
  612. if (iip->ili_fields & XFS_ILOG_DEV) {
  613. ASSERT(whichfork == XFS_DATA_FORK);
  614. xfs_dinode_put_rdev(dip,
  615. linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
  616. }
  617. break;
  618. default:
  619. ASSERT(0);
  620. break;
  621. }
  622. }
  623. /* Convert bmap state flags to an inode fork. */
  624. struct xfs_ifork *
  625. xfs_iext_state_to_fork(
  626. struct xfs_inode *ip,
  627. int state)
  628. {
  629. if (state & BMAP_COWFORK)
  630. return ip->i_cowfp;
  631. else if (state & BMAP_ATTRFORK)
  632. return &ip->i_af;
  633. return &ip->i_df;
  634. }
  635. /*
  636. * Initialize an inode's copy-on-write fork.
  637. */
  638. void
  639. xfs_ifork_init_cow(
  640. struct xfs_inode *ip)
  641. {
  642. if (ip->i_cowfp)
  643. return;
  644. ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
  645. GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
  646. ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
  647. }
  648. /* Verify the inline contents of the data fork of an inode. */
  649. int
  650. xfs_ifork_verify_local_data(
  651. struct xfs_inode *ip)
  652. {
  653. xfs_failaddr_t fa = NULL;
  654. switch (VFS_I(ip)->i_mode & S_IFMT) {
  655. case S_IFDIR: {
  656. struct xfs_mount *mp = ip->i_mount;
  657. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
  658. struct xfs_dir2_sf_hdr *sfp = ifp->if_data;
  659. fa = xfs_dir2_sf_verify(mp, sfp, ifp->if_bytes);
  660. break;
  661. }
  662. case S_IFLNK: {
  663. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
  664. fa = xfs_symlink_shortform_verify(ifp->if_data, ifp->if_bytes);
  665. break;
  666. }
  667. default:
  668. break;
  669. }
  670. if (fa) {
  671. xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
  672. ip->i_df.if_data, ip->i_df.if_bytes, fa);
  673. return -EFSCORRUPTED;
  674. }
  675. return 0;
  676. }
  677. /* Verify the inline contents of the attr fork of an inode. */
  678. int
  679. xfs_ifork_verify_local_attr(
  680. struct xfs_inode *ip)
  681. {
  682. struct xfs_ifork *ifp = &ip->i_af;
  683. xfs_failaddr_t fa;
  684. if (!xfs_inode_has_attr_fork(ip)) {
  685. fa = __this_address;
  686. } else {
  687. struct xfs_ifork *ifp = &ip->i_af;
  688. ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
  689. fa = xfs_attr_shortform_verify(ifp->if_data, ifp->if_bytes);
  690. }
  691. if (fa) {
  692. xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
  693. ifp->if_data, ifp->if_bytes, fa);
  694. return -EFSCORRUPTED;
  695. }
  696. return 0;
  697. }
  698. /*
  699. * Check if the inode fork supports adding nr_to_add more extents.
  700. *
  701. * If it doesn't but we can upgrade it to large extent counters, do the upgrade.
  702. * If we can't upgrade or are already using big counters but still can't fit the
  703. * additional extents, return -EFBIG.
  704. */
  705. int
  706. xfs_iext_count_extend(
  707. struct xfs_trans *tp,
  708. struct xfs_inode *ip,
  709. int whichfork,
  710. uint nr_to_add)
  711. {
  712. struct xfs_mount *mp = ip->i_mount;
  713. bool has_large =
  714. xfs_inode_has_large_extent_counts(ip);
  715. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  716. uint64_t nr_exts;
  717. ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
  718. if (whichfork == XFS_COW_FORK)
  719. return 0;
  720. /* no point in upgrading if if_nextents overflows */
  721. nr_exts = ifp->if_nextents + nr_to_add;
  722. if (nr_exts < ifp->if_nextents)
  723. return -EFBIG;
  724. if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_REDUCE_MAX_IEXTENTS) &&
  725. nr_exts > 10)
  726. return -EFBIG;
  727. if (nr_exts > xfs_iext_max_nextents(has_large, whichfork)) {
  728. if (has_large || !xfs_has_large_extent_counts(mp))
  729. return -EFBIG;
  730. ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
  731. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  732. }
  733. return 0;
  734. }
  735. /* Decide if a file mapping is on the realtime device or not. */
  736. bool
  737. xfs_ifork_is_realtime(
  738. struct xfs_inode *ip,
  739. int whichfork)
  740. {
  741. return XFS_IS_REALTIME_INODE(ip) && whichfork != XFS_ATTR_FORK;
  742. }