xfs_inode_fork.c 23 KB

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