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