Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
| 3 | * Copyright (c) 2012-2013 Red Hat, Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it would be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | #include "xfs.h" |
| 20 | #include "xfs_fs.h" |
| 21 | #include "xfs_types.h" |
| 22 | #include "xfs_bit.h" |
| 23 | #include "xfs_log.h" |
| 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
| 27 | #include "xfs_dir2.h" |
| 28 | #include "xfs_mount.h" |
| 29 | #include "xfs_da_btree.h" |
| 30 | #include "xfs_bmap_btree.h" |
| 31 | #include "xfs_ialloc_btree.h" |
| 32 | #include "xfs_dinode.h" |
| 33 | #include "xfs_inode.h" |
| 34 | #include "xfs_inode_item.h" |
| 35 | #include "xfs_itable.h" |
| 36 | #include "xfs_ialloc.h" |
| 37 | #include "xfs_alloc.h" |
| 38 | #include "xfs_bmap.h" |
| 39 | #include "xfs_error.h" |
| 40 | #include "xfs_quota.h" |
| 41 | #include "xfs_utils.h" |
| 42 | #include "xfs_trans_space.h" |
| 43 | #include "xfs_log_priv.h" |
| 44 | #include "xfs_trace.h" |
| 45 | #include "xfs_symlink.h" |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 46 | #include "xfs_cksum.h" |
| 47 | #include "xfs_buf_item.h" |
| 48 | |
| 49 | |
| 50 | /* |
| 51 | * Each contiguous block has a header, so it is not just a simple pathlen |
| 52 | * to FSB conversion. |
| 53 | */ |
| 54 | int |
| 55 | xfs_symlink_blocks( |
| 56 | struct xfs_mount *mp, |
| 57 | int pathlen) |
| 58 | { |
Dave Chinner | 1de09d1 | 2013-05-27 16:38:20 +1000 | [diff] [blame] | 59 | int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize); |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 60 | |
Dave Chinner | 1de09d1 | 2013-05-27 16:38:20 +1000 | [diff] [blame] | 61 | return (pathlen + buflen - 1) / buflen; |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int |
| 65 | xfs_symlink_hdr_set( |
| 66 | struct xfs_mount *mp, |
| 67 | xfs_ino_t ino, |
| 68 | uint32_t offset, |
| 69 | uint32_t size, |
| 70 | struct xfs_buf *bp) |
| 71 | { |
| 72 | struct xfs_dsymlink_hdr *dsl = bp->b_addr; |
| 73 | |
| 74 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 75 | return 0; |
| 76 | |
| 77 | dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC); |
| 78 | dsl->sl_offset = cpu_to_be32(offset); |
| 79 | dsl->sl_bytes = cpu_to_be32(size); |
| 80 | uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_uuid); |
| 81 | dsl->sl_owner = cpu_to_be64(ino); |
| 82 | dsl->sl_blkno = cpu_to_be64(bp->b_bn); |
| 83 | bp->b_ops = &xfs_symlink_buf_ops; |
| 84 | |
| 85 | return sizeof(struct xfs_dsymlink_hdr); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Checking of the symlink header is split into two parts. the verifier does |
| 90 | * CRC, location and bounds checking, the unpacking function checks the path |
| 91 | * parameters and owner. |
| 92 | */ |
| 93 | bool |
| 94 | xfs_symlink_hdr_ok( |
| 95 | struct xfs_mount *mp, |
| 96 | xfs_ino_t ino, |
| 97 | uint32_t offset, |
| 98 | uint32_t size, |
| 99 | struct xfs_buf *bp) |
| 100 | { |
| 101 | struct xfs_dsymlink_hdr *dsl = bp->b_addr; |
| 102 | |
| 103 | if (offset != be32_to_cpu(dsl->sl_offset)) |
| 104 | return false; |
| 105 | if (size != be32_to_cpu(dsl->sl_bytes)) |
| 106 | return false; |
| 107 | if (ino != be64_to_cpu(dsl->sl_owner)) |
| 108 | return false; |
| 109 | |
| 110 | /* ok */ |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | static bool |
| 115 | xfs_symlink_verify( |
| 116 | struct xfs_buf *bp) |
| 117 | { |
| 118 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 119 | struct xfs_dsymlink_hdr *dsl = bp->b_addr; |
| 120 | |
| 121 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 122 | return false; |
| 123 | if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC)) |
| 124 | return false; |
| 125 | if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_uuid)) |
| 126 | return false; |
| 127 | if (bp->b_bn != be64_to_cpu(dsl->sl_blkno)) |
| 128 | return false; |
| 129 | if (be32_to_cpu(dsl->sl_offset) + |
| 130 | be32_to_cpu(dsl->sl_bytes) >= MAXPATHLEN) |
| 131 | return false; |
| 132 | if (dsl->sl_owner == 0) |
| 133 | return false; |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | xfs_symlink_read_verify( |
| 140 | struct xfs_buf *bp) |
| 141 | { |
| 142 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 143 | |
| 144 | /* no verification of non-crc buffers */ |
| 145 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 146 | return; |
| 147 | |
| 148 | if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length), |
| 149 | offsetof(struct xfs_dsymlink_hdr, sl_crc)) || |
| 150 | !xfs_symlink_verify(bp)) { |
| 151 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); |
| 152 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void |
| 157 | xfs_symlink_write_verify( |
| 158 | struct xfs_buf *bp) |
| 159 | { |
| 160 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 161 | struct xfs_buf_log_item *bip = bp->b_fspriv; |
| 162 | |
| 163 | /* no verification of non-crc buffers */ |
| 164 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 165 | return; |
| 166 | |
| 167 | if (!xfs_symlink_verify(bp)) { |
| 168 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); |
| 169 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if (bip) { |
| 174 | struct xfs_dsymlink_hdr *dsl = bp->b_addr; |
| 175 | dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 176 | } |
| 177 | xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), |
| 178 | offsetof(struct xfs_dsymlink_hdr, sl_crc)); |
| 179 | } |
| 180 | |
| 181 | const struct xfs_buf_ops xfs_symlink_buf_ops = { |
| 182 | .verify_read = xfs_symlink_read_verify, |
| 183 | .verify_write = xfs_symlink_write_verify, |
| 184 | }; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 185 | |
| 186 | void |
| 187 | xfs_symlink_local_to_remote( |
| 188 | struct xfs_trans *tp, |
| 189 | struct xfs_buf *bp, |
| 190 | struct xfs_inode *ip, |
| 191 | struct xfs_ifork *ifp) |
| 192 | { |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 193 | struct xfs_mount *mp = ip->i_mount; |
| 194 | char *buf; |
| 195 | |
| 196 | if (!xfs_sb_version_hascrc(&mp->m_sb)) { |
| 197 | bp->b_ops = NULL; |
| 198 | memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * As this symlink fits in an inode literal area, it must also fit in |
| 204 | * the smallest buffer the filesystem supports. |
| 205 | */ |
| 206 | ASSERT(BBTOB(bp->b_length) >= |
| 207 | ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr)); |
| 208 | |
| 209 | bp->b_ops = &xfs_symlink_buf_ops; |
| 210 | |
| 211 | buf = bp->b_addr; |
| 212 | buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp); |
| 213 | memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes); |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* ----- Kernel only functions below ----- */ |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 217 | STATIC int |
| 218 | xfs_readlink_bmap( |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 219 | struct xfs_inode *ip, |
| 220 | char *link) |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 221 | { |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 222 | struct xfs_mount *mp = ip->i_mount; |
| 223 | struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS]; |
| 224 | struct xfs_buf *bp; |
| 225 | xfs_daddr_t d; |
| 226 | char *cur_chunk; |
| 227 | int pathlen = ip->i_d.di_size; |
| 228 | int nmaps = XFS_SYMLINK_MAPS; |
| 229 | int byte_cnt; |
| 230 | int n; |
| 231 | int error = 0; |
| 232 | int fsblocks = 0; |
| 233 | int offset; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 234 | |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 235 | fsblocks = xfs_symlink_blocks(mp, pathlen); |
| 236 | error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0); |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 237 | if (error) |
| 238 | goto out; |
| 239 | |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 240 | offset = 0; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 241 | for (n = 0; n < nmaps; n++) { |
| 242 | d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); |
| 243 | byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); |
| 244 | |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 245 | bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0, |
| 246 | &xfs_symlink_buf_ops); |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 247 | if (!bp) |
| 248 | return XFS_ERROR(ENOMEM); |
| 249 | error = bp->b_error; |
| 250 | if (error) { |
| 251 | xfs_buf_ioerror_alert(bp, __func__); |
| 252 | xfs_buf_relse(bp); |
| 253 | goto out; |
| 254 | } |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 255 | byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt); |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 256 | if (pathlen < byte_cnt) |
| 257 | byte_cnt = pathlen; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 258 | |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 259 | cur_chunk = bp->b_addr; |
| 260 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 261 | if (!xfs_symlink_hdr_ok(mp, ip->i_ino, offset, |
| 262 | byte_cnt, bp)) { |
| 263 | error = EFSCORRUPTED; |
| 264 | xfs_alert(mp, |
| 265 | "symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)", |
| 266 | offset, byte_cnt, ip->i_ino); |
| 267 | xfs_buf_relse(bp); |
| 268 | goto out; |
| 269 | |
| 270 | } |
| 271 | |
| 272 | cur_chunk += sizeof(struct xfs_dsymlink_hdr); |
| 273 | } |
| 274 | |
| 275 | memcpy(link + offset, bp->b_addr, byte_cnt); |
| 276 | |
| 277 | pathlen -= byte_cnt; |
| 278 | offset += byte_cnt; |
| 279 | |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 280 | xfs_buf_relse(bp); |
| 281 | } |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 282 | ASSERT(pathlen == 0); |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 283 | |
| 284 | link[ip->i_d.di_size] = '\0'; |
| 285 | error = 0; |
| 286 | |
| 287 | out: |
| 288 | return error; |
| 289 | } |
| 290 | |
| 291 | int |
| 292 | xfs_readlink( |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 293 | struct xfs_inode *ip, |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 294 | char *link) |
| 295 | { |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 296 | struct xfs_mount *mp = ip->i_mount; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 297 | xfs_fsize_t pathlen; |
| 298 | int error = 0; |
| 299 | |
| 300 | trace_xfs_readlink(ip); |
| 301 | |
| 302 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 303 | return XFS_ERROR(EIO); |
| 304 | |
| 305 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 306 | |
| 307 | pathlen = ip->i_d.di_size; |
| 308 | if (!pathlen) |
| 309 | goto out; |
| 310 | |
| 311 | if (pathlen < 0 || pathlen > MAXPATHLEN) { |
| 312 | xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)", |
| 313 | __func__, (unsigned long long) ip->i_ino, |
| 314 | (long long) pathlen); |
| 315 | ASSERT(0); |
| 316 | error = XFS_ERROR(EFSCORRUPTED); |
| 317 | goto out; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | if (ip->i_df.if_flags & XFS_IFINLINE) { |
| 322 | memcpy(link, ip->i_df.if_u1.if_data, pathlen); |
| 323 | link[pathlen] = '\0'; |
| 324 | } else { |
| 325 | error = xfs_readlink_bmap(ip, link); |
| 326 | } |
| 327 | |
| 328 | out: |
| 329 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 330 | return error; |
| 331 | } |
| 332 | |
| 333 | int |
| 334 | xfs_symlink( |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 335 | struct xfs_inode *dp, |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 336 | struct xfs_name *link_name, |
| 337 | const char *target_path, |
| 338 | umode_t mode, |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 339 | struct xfs_inode **ipp) |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 340 | { |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 341 | struct xfs_mount *mp = dp->i_mount; |
| 342 | struct xfs_trans *tp = NULL; |
| 343 | struct xfs_inode *ip = NULL; |
| 344 | int error = 0; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 345 | int pathlen; |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 346 | struct xfs_bmap_free free_list; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 347 | xfs_fsblock_t first_block; |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 348 | bool unlock_dp_on_error = false; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 349 | uint cancel_flags; |
| 350 | int committed; |
| 351 | xfs_fileoff_t first_fsb; |
| 352 | xfs_filblks_t fs_blocks; |
| 353 | int nmaps; |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 354 | struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS]; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 355 | xfs_daddr_t d; |
| 356 | const char *cur_chunk; |
| 357 | int byte_cnt; |
| 358 | int n; |
| 359 | xfs_buf_t *bp; |
| 360 | prid_t prid; |
| 361 | struct xfs_dquot *udqp, *gdqp; |
| 362 | uint resblks; |
| 363 | |
| 364 | *ipp = NULL; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 365 | |
| 366 | trace_xfs_symlink(dp, link_name); |
| 367 | |
| 368 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 369 | return XFS_ERROR(EIO); |
| 370 | |
| 371 | /* |
| 372 | * Check component lengths of the target path name. |
| 373 | */ |
| 374 | pathlen = strlen(target_path); |
| 375 | if (pathlen >= MAXPATHLEN) /* total string too long */ |
| 376 | return XFS_ERROR(ENAMETOOLONG); |
| 377 | |
| 378 | udqp = gdqp = NULL; |
| 379 | if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 380 | prid = xfs_get_projid(dp); |
| 381 | else |
| 382 | prid = XFS_PROJID_DEFAULT; |
| 383 | |
| 384 | /* |
| 385 | * Make sure that we have allocated dquot(s) on disk. |
| 386 | */ |
| 387 | error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid, |
| 388 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp); |
| 389 | if (error) |
| 390 | goto std_return; |
| 391 | |
| 392 | tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK); |
| 393 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 394 | /* |
| 395 | * The symlink will fit into the inode data fork? |
| 396 | * There can't be any attributes so we get the whole variable part. |
| 397 | */ |
| 398 | if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version)) |
| 399 | fs_blocks = 0; |
| 400 | else |
Dave Chinner | 1de09d1 | 2013-05-27 16:38:20 +1000 | [diff] [blame] | 401 | fs_blocks = xfs_symlink_blocks(mp, pathlen); |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 402 | resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks); |
| 403 | error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0, |
| 404 | XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); |
| 405 | if (error == ENOSPC && fs_blocks == 0) { |
| 406 | resblks = 0; |
| 407 | error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0, |
| 408 | XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); |
| 409 | } |
| 410 | if (error) { |
| 411 | cancel_flags = 0; |
| 412 | goto error_return; |
| 413 | } |
| 414 | |
| 415 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
| 416 | unlock_dp_on_error = true; |
| 417 | |
| 418 | /* |
| 419 | * Check whether the directory allows new symlinks or not. |
| 420 | */ |
| 421 | if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) { |
| 422 | error = XFS_ERROR(EPERM); |
| 423 | goto error_return; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Reserve disk quota : blocks and inode. |
| 428 | */ |
| 429 | error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0); |
| 430 | if (error) |
| 431 | goto error_return; |
| 432 | |
| 433 | /* |
| 434 | * Check for ability to enter directory entry, if no space reserved. |
| 435 | */ |
| 436 | error = xfs_dir_canenter(tp, dp, link_name, resblks); |
| 437 | if (error) |
| 438 | goto error_return; |
| 439 | /* |
| 440 | * Initialize the bmap freelist prior to calling either |
| 441 | * bmapi or the directory create code. |
| 442 | */ |
| 443 | xfs_bmap_init(&free_list, &first_block); |
| 444 | |
| 445 | /* |
| 446 | * Allocate an inode for the symlink. |
| 447 | */ |
| 448 | error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0, |
| 449 | prid, resblks > 0, &ip, NULL); |
| 450 | if (error) { |
| 451 | if (error == ENOSPC) |
| 452 | goto error_return; |
| 453 | goto error1; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * An error after we've joined dp to the transaction will result in the |
| 458 | * transaction cancel unlocking dp so don't do it explicitly in the |
| 459 | * error path. |
| 460 | */ |
| 461 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
| 462 | unlock_dp_on_error = false; |
| 463 | |
| 464 | /* |
| 465 | * Also attach the dquot(s) to it, if applicable. |
| 466 | */ |
| 467 | xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp); |
| 468 | |
| 469 | if (resblks) |
| 470 | resblks -= XFS_IALLOC_SPACE_RES(mp); |
| 471 | /* |
| 472 | * If the symlink will fit into the inode, write it inline. |
| 473 | */ |
| 474 | if (pathlen <= XFS_IFORK_DSIZE(ip)) { |
| 475 | xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK); |
| 476 | memcpy(ip->i_df.if_u1.if_data, target_path, pathlen); |
| 477 | ip->i_d.di_size = pathlen; |
| 478 | |
| 479 | /* |
| 480 | * The inode was initially created in extent format. |
| 481 | */ |
| 482 | ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT); |
| 483 | ip->i_df.if_flags |= XFS_IFINLINE; |
| 484 | |
| 485 | ip->i_d.di_format = XFS_DINODE_FMT_LOCAL; |
| 486 | xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE); |
| 487 | |
| 488 | } else { |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 489 | int offset; |
| 490 | |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 491 | first_fsb = 0; |
| 492 | nmaps = XFS_SYMLINK_MAPS; |
| 493 | |
| 494 | error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks, |
| 495 | XFS_BMAPI_METADATA, &first_block, resblks, |
| 496 | mval, &nmaps, &free_list); |
| 497 | if (error) |
| 498 | goto error2; |
| 499 | |
| 500 | if (resblks) |
| 501 | resblks -= fs_blocks; |
| 502 | ip->i_d.di_size = pathlen; |
| 503 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 504 | |
| 505 | cur_chunk = target_path; |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 506 | offset = 0; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 507 | for (n = 0; n < nmaps; n++) { |
Dave Chinner | 1de09d1 | 2013-05-27 16:38:20 +1000 | [diff] [blame] | 508 | char *buf; |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 509 | |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 510 | d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); |
| 511 | byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); |
| 512 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, |
| 513 | BTOBB(byte_cnt), 0); |
| 514 | if (!bp) { |
| 515 | error = ENOMEM; |
| 516 | goto error2; |
| 517 | } |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 518 | bp->b_ops = &xfs_symlink_buf_ops; |
| 519 | |
| 520 | byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt); |
Dave Chinner | 1de09d1 | 2013-05-27 16:38:20 +1000 | [diff] [blame] | 521 | byte_cnt = min(byte_cnt, pathlen); |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 522 | |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 523 | buf = bp->b_addr; |
| 524 | buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset, |
| 525 | byte_cnt, bp); |
| 526 | |
| 527 | memcpy(buf, cur_chunk, byte_cnt); |
| 528 | |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 529 | cur_chunk += byte_cnt; |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 530 | pathlen -= byte_cnt; |
| 531 | offset += byte_cnt; |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 532 | |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 533 | xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) - |
| 534 | (char *)bp->b_addr); |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 535 | } |
Dave Chinner | 1de09d1 | 2013-05-27 16:38:20 +1000 | [diff] [blame] | 536 | ASSERT(pathlen == 0); |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Create the directory entry for the symlink. |
| 541 | */ |
| 542 | error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, |
| 543 | &first_block, &free_list, resblks); |
| 544 | if (error) |
| 545 | goto error2; |
| 546 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 547 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 548 | |
| 549 | /* |
| 550 | * If this is a synchronous mount, make sure that the |
| 551 | * symlink transaction goes to disk before returning to |
| 552 | * the user. |
| 553 | */ |
| 554 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 555 | xfs_trans_set_sync(tp); |
| 556 | } |
| 557 | |
| 558 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
| 559 | if (error) { |
| 560 | goto error2; |
| 561 | } |
| 562 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 563 | xfs_qm_dqrele(udqp); |
| 564 | xfs_qm_dqrele(gdqp); |
| 565 | |
| 566 | *ipp = ip; |
| 567 | return 0; |
| 568 | |
| 569 | error2: |
| 570 | IRELE(ip); |
| 571 | error1: |
| 572 | xfs_bmap_cancel(&free_list); |
| 573 | cancel_flags |= XFS_TRANS_ABORT; |
| 574 | error_return: |
| 575 | xfs_trans_cancel(tp, cancel_flags); |
| 576 | xfs_qm_dqrele(udqp); |
| 577 | xfs_qm_dqrele(gdqp); |
| 578 | |
| 579 | if (unlock_dp_on_error) |
| 580 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 581 | std_return: |
| 582 | return error; |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * Free a symlink that has blocks associated with it. |
| 587 | */ |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 588 | int |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 589 | xfs_inactive_symlink_rmt( |
| 590 | xfs_inode_t *ip, |
| 591 | xfs_trans_t **tpp) |
| 592 | { |
| 593 | xfs_buf_t *bp; |
| 594 | int committed; |
| 595 | int done; |
| 596 | int error; |
| 597 | xfs_fsblock_t first_block; |
| 598 | xfs_bmap_free_t free_list; |
| 599 | int i; |
| 600 | xfs_mount_t *mp; |
| 601 | xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS]; |
| 602 | int nmaps; |
| 603 | xfs_trans_t *ntp; |
| 604 | int size; |
| 605 | xfs_trans_t *tp; |
| 606 | |
| 607 | tp = *tpp; |
| 608 | mp = ip->i_mount; |
| 609 | ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip)); |
| 610 | /* |
| 611 | * We're freeing a symlink that has some |
| 612 | * blocks allocated to it. Free the |
| 613 | * blocks here. We know that we've got |
| 614 | * either 1 or 2 extents and that we can |
| 615 | * free them all in one bunmapi call. |
| 616 | */ |
| 617 | ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2); |
| 618 | |
| 619 | /* |
| 620 | * Lock the inode, fix the size, and join it to the transaction. |
| 621 | * Hold it so in the normal path, we still have it locked for |
| 622 | * the second transaction. In the error paths we need it |
| 623 | * held so the cancel won't rele it, see below. |
| 624 | */ |
| 625 | size = (int)ip->i_d.di_size; |
| 626 | ip->i_d.di_size = 0; |
| 627 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 628 | /* |
| 629 | * Find the block(s) so we can inval and unmap them. |
| 630 | */ |
| 631 | done = 0; |
| 632 | xfs_bmap_init(&free_list, &first_block); |
| 633 | nmaps = ARRAY_SIZE(mval); |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 634 | error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size), |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 635 | mval, &nmaps, 0); |
| 636 | if (error) |
| 637 | goto error0; |
| 638 | /* |
Dave Chinner | f948dd7 | 2013-04-03 16:11:19 +1100 | [diff] [blame] | 639 | * Invalidate the block(s). No validation is done. |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 640 | */ |
| 641 | for (i = 0; i < nmaps; i++) { |
| 642 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, |
| 643 | XFS_FSB_TO_DADDR(mp, mval[i].br_startblock), |
| 644 | XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0); |
| 645 | if (!bp) { |
| 646 | error = ENOMEM; |
| 647 | goto error1; |
| 648 | } |
| 649 | xfs_trans_binval(tp, bp); |
| 650 | } |
| 651 | /* |
| 652 | * Unmap the dead block(s) to the free_list. |
| 653 | */ |
| 654 | if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps, |
| 655 | &first_block, &free_list, &done))) |
| 656 | goto error1; |
| 657 | ASSERT(done); |
| 658 | /* |
| 659 | * Commit the first transaction. This logs the EFI and the inode. |
| 660 | */ |
| 661 | if ((error = xfs_bmap_finish(&tp, &free_list, &committed))) |
| 662 | goto error1; |
| 663 | /* |
| 664 | * The transaction must have been committed, since there were |
| 665 | * actually extents freed by xfs_bunmapi. See xfs_bmap_finish. |
| 666 | * The new tp has the extent freeing and EFDs. |
| 667 | */ |
| 668 | ASSERT(committed); |
| 669 | /* |
| 670 | * The first xact was committed, so add the inode to the new one. |
| 671 | * Mark it dirty so it will be logged and moved forward in the log as |
| 672 | * part of every commit. |
| 673 | */ |
| 674 | xfs_trans_ijoin(tp, ip, 0); |
| 675 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 676 | /* |
| 677 | * Get a new, empty transaction to return to our caller. |
| 678 | */ |
| 679 | ntp = xfs_trans_dup(tp); |
| 680 | /* |
| 681 | * Commit the transaction containing extent freeing and EFDs. |
| 682 | * If we get an error on the commit here or on the reserve below, |
| 683 | * we need to unlock the inode since the new transaction doesn't |
| 684 | * have the inode attached. |
| 685 | */ |
| 686 | error = xfs_trans_commit(tp, 0); |
| 687 | tp = ntp; |
| 688 | if (error) { |
| 689 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 690 | goto error0; |
| 691 | } |
| 692 | /* |
| 693 | * transaction commit worked ok so we can drop the extra ticket |
| 694 | * reference that we gained in xfs_trans_dup() |
| 695 | */ |
| 696 | xfs_log_ticket_put(tp->t_ticket); |
| 697 | |
| 698 | /* |
| 699 | * Remove the memory for extent descriptions (just bookkeeping). |
| 700 | */ |
| 701 | if (ip->i_df.if_bytes) |
| 702 | xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK); |
| 703 | ASSERT(ip->i_df.if_bytes == 0); |
| 704 | /* |
| 705 | * Put an itruncate log reservation in the new transaction |
| 706 | * for our caller. |
| 707 | */ |
| 708 | if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 709 | XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) { |
| 710 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 711 | goto error0; |
| 712 | } |
| 713 | |
| 714 | xfs_trans_ijoin(tp, ip, 0); |
| 715 | *tpp = tp; |
| 716 | return 0; |
| 717 | |
| 718 | error1: |
| 719 | xfs_bmap_cancel(&free_list); |
| 720 | error0: |
| 721 | return error; |
| 722 | } |