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