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