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