xfs_inode_fork.c 23 KB

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