1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc. 4 * Copyright (c) 2012-2013 Red Hat, Inc. 5 * All rights reserved. 6 */ 7 #include "xfs.h" 8 #include "xfs_shared.h" 9 #include "xfs_fs.h" 10 #include "xfs_format.h" 11 #include "xfs_log_format.h" 12 #include "xfs_trans_resv.h" 13 #include "xfs_bit.h" 14 #include "xfs_mount.h" 15 #include "xfs_dir2.h" 16 #include "xfs_inode.h" 17 #include "xfs_bmap.h" 18 #include "xfs_bmap_btree.h" 19 #include "xfs_quota.h" 20 #include "xfs_symlink.h" 21 #include "xfs_trans_space.h" 22 #include "xfs_trace.h" 23 #include "xfs_trans.h" 24 #include "xfs_ialloc.h" 25 #include "xfs_error.h" 26 #include "xfs_health.h" 27 28 /* ----- Kernel only functions below ----- */ 29 int 30 xfs_readlink_bmap_ilocked( 31 struct xfs_inode *ip, 32 char *link) 33 { 34 struct xfs_mount *mp = ip->i_mount; 35 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS]; 36 struct xfs_buf *bp; 37 xfs_daddr_t d; 38 char *cur_chunk; 39 int pathlen = ip->i_disk_size; 40 int nmaps = XFS_SYMLINK_MAPS; 41 int byte_cnt; 42 int n; 43 int error = 0; 44 int fsblocks = 0; 45 int offset; 46 47 xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL); 48 49 fsblocks = xfs_symlink_blocks(mp, pathlen); 50 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0); 51 if (error) 52 goto out; 53 54 offset = 0; 55 for (n = 0; n < nmaps; n++) { 56 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); 57 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); 58 59 error = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0, 60 &bp, &xfs_symlink_buf_ops); 61 if (xfs_metadata_is_sick(error)) 62 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK); 63 if (error) 64 return error; 65 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt); 66 if (pathlen < byte_cnt) 67 byte_cnt = pathlen; 68 69 cur_chunk = bp->b_addr; 70 if (xfs_has_crc(mp)) { 71 if (!xfs_symlink_hdr_ok(ip->i_ino, offset, 72 byte_cnt, bp)) { 73 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK); 74 error = -EFSCORRUPTED; 75 xfs_alert(mp, 76 "symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)", 77 offset, byte_cnt, ip->i_ino); 78 xfs_buf_relse(bp); 79 goto out; 80 81 } 82 83 cur_chunk += sizeof(struct xfs_dsymlink_hdr); 84 } 85 86 memcpy(link + offset, cur_chunk, byte_cnt); 87 88 pathlen -= byte_cnt; 89 offset += byte_cnt; 90 91 xfs_buf_relse(bp); 92 } 93 ASSERT(pathlen == 0); 94 95 link[ip->i_disk_size] = '\0'; 96 error = 0; 97 98 out: 99 return error; 100 } 101 102 int 103 xfs_readlink( 104 struct xfs_inode *ip, 105 char *link) 106 { 107 struct xfs_mount *mp = ip->i_mount; 108 xfs_fsize_t pathlen; 109 int error; 110 111 trace_xfs_readlink(ip); 112 113 if (xfs_is_shutdown(mp)) 114 return -EIO; 115 if (xfs_ifork_zapped(ip, XFS_DATA_FORK)) 116 return -EIO; 117 118 xfs_ilock(ip, XFS_ILOCK_SHARED); 119 120 pathlen = ip->i_disk_size; 121 if (!pathlen) 122 goto out_corrupt; 123 124 if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) { 125 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)", 126 __func__, (unsigned long long) ip->i_ino, 127 (long long) pathlen); 128 ASSERT(0); 129 goto out_corrupt; 130 } 131 132 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) { 133 /* 134 * The VFS crashes on a NULL pointer, so return -EFSCORRUPTED 135 * if if_data is junk. 136 */ 137 if (XFS_IS_CORRUPT(ip->i_mount, !ip->i_df.if_data)) 138 goto out_corrupt; 139 140 memcpy(link, ip->i_df.if_data, pathlen + 1); 141 error = 0; 142 } else { 143 error = xfs_readlink_bmap_ilocked(ip, link); 144 } 145 146 xfs_iunlock(ip, XFS_ILOCK_SHARED); 147 return error; 148 out_corrupt: 149 xfs_iunlock(ip, XFS_ILOCK_SHARED); 150 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK); 151 return -EFSCORRUPTED; 152 } 153 154 int 155 xfs_symlink( 156 struct mnt_idmap *idmap, 157 struct xfs_inode *dp, 158 struct xfs_name *link_name, 159 const char *target_path, 160 umode_t mode, 161 struct xfs_inode **ipp) 162 { 163 struct xfs_mount *mp = dp->i_mount; 164 struct xfs_trans *tp = NULL; 165 struct xfs_inode *ip = NULL; 166 int error = 0; 167 int pathlen; 168 bool unlock_dp_on_error = false; 169 xfs_fileoff_t first_fsb; 170 xfs_filblks_t fs_blocks; 171 int nmaps; 172 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS]; 173 xfs_daddr_t d; 174 const char *cur_chunk; 175 int byte_cnt; 176 int n; 177 struct xfs_buf *bp; 178 prid_t prid; 179 struct xfs_dquot *udqp = NULL; 180 struct xfs_dquot *gdqp = NULL; 181 struct xfs_dquot *pdqp = NULL; 182 uint resblks; 183 xfs_ino_t ino; 184 185 *ipp = NULL; 186 187 trace_xfs_symlink(dp, link_name); 188 189 if (xfs_is_shutdown(mp)) 190 return -EIO; 191 192 /* 193 * Check component lengths of the target path name. 194 */ 195 pathlen = strlen(target_path); 196 if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */ 197 return -ENAMETOOLONG; 198 ASSERT(pathlen > 0); 199 200 prid = xfs_get_initial_prid(dp); 201 202 /* 203 * Make sure that we have allocated dquot(s) on disk. 204 */ 205 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(idmap, &init_user_ns), 206 mapped_fsgid(idmap, &init_user_ns), prid, 207 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, 208 &udqp, &gdqp, &pdqp); 209 if (error) 210 return error; 211 212 /* 213 * The symlink will fit into the inode data fork? 214 * There can't be any attributes so we get the whole variable part. 215 */ 216 if (pathlen <= XFS_LITINO(mp)) 217 fs_blocks = 0; 218 else 219 fs_blocks = xfs_symlink_blocks(mp, pathlen); 220 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks); 221 222 error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp, 223 pdqp, resblks, &tp); 224 if (error) 225 goto out_release_dquots; 226 227 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); 228 unlock_dp_on_error = true; 229 230 /* 231 * Check whether the directory allows new symlinks or not. 232 */ 233 if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) { 234 error = -EPERM; 235 goto out_trans_cancel; 236 } 237 238 /* 239 * Allocate an inode for the symlink. 240 */ 241 error = xfs_dialloc(&tp, dp->i_ino, S_IFLNK, &ino); 242 if (!error) 243 error = xfs_init_new_inode(idmap, tp, dp, ino, 244 S_IFLNK | (mode & ~S_IFMT), 1, 0, prid, 245 false, &ip); 246 if (error) 247 goto out_trans_cancel; 248 249 /* 250 * Now we join the directory inode to the transaction. We do not do it 251 * earlier because xfs_dir_ialloc might commit the previous transaction 252 * (and release all the locks). An error from here on will result in 253 * the transaction cancel unlocking dp so don't do it explicitly in the 254 * error path. 255 */ 256 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); 257 unlock_dp_on_error = false; 258 259 /* 260 * Also attach the dquot(s) to it, if applicable. 261 */ 262 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); 263 264 resblks -= XFS_IALLOC_SPACE_RES(mp); 265 /* 266 * If the symlink will fit into the inode, write it inline. 267 */ 268 if (pathlen <= xfs_inode_data_fork_size(ip)) { 269 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen); 270 271 ip->i_disk_size = pathlen; 272 ip->i_df.if_format = XFS_DINODE_FMT_LOCAL; 273 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE); 274 } else { 275 int offset; 276 277 first_fsb = 0; 278 nmaps = XFS_SYMLINK_MAPS; 279 280 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks, 281 XFS_BMAPI_METADATA, resblks, mval, &nmaps); 282 if (error) 283 goto out_trans_cancel; 284 285 resblks -= fs_blocks; 286 ip->i_disk_size = pathlen; 287 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 288 289 cur_chunk = target_path; 290 offset = 0; 291 for (n = 0; n < nmaps; n++) { 292 char *buf; 293 294 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); 295 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); 296 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, 297 BTOBB(byte_cnt), 0, &bp); 298 if (error) 299 goto out_trans_cancel; 300 bp->b_ops = &xfs_symlink_buf_ops; 301 302 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt); 303 byte_cnt = min(byte_cnt, pathlen); 304 305 buf = bp->b_addr; 306 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset, 307 byte_cnt, bp); 308 309 memcpy(buf, cur_chunk, byte_cnt); 310 311 cur_chunk += byte_cnt; 312 pathlen -= byte_cnt; 313 offset += byte_cnt; 314 315 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF); 316 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) - 317 (char *)bp->b_addr); 318 } 319 ASSERT(pathlen == 0); 320 } 321 i_size_write(VFS_I(ip), ip->i_disk_size); 322 323 /* 324 * Create the directory entry for the symlink. 325 */ 326 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, resblks); 327 if (error) 328 goto out_trans_cancel; 329 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 330 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); 331 xfs_dir_update_hook(dp, ip, 1, link_name); 332 333 /* 334 * If this is a synchronous mount, make sure that the 335 * symlink transaction goes to disk before returning to 336 * the user. 337 */ 338 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp)) 339 xfs_trans_set_sync(tp); 340 341 error = xfs_trans_commit(tp); 342 if (error) 343 goto out_release_inode; 344 345 xfs_qm_dqrele(udqp); 346 xfs_qm_dqrele(gdqp); 347 xfs_qm_dqrele(pdqp); 348 349 *ipp = ip; 350 return 0; 351 352 out_trans_cancel: 353 xfs_trans_cancel(tp); 354 out_release_inode: 355 /* 356 * Wait until after the current transaction is aborted to finish the 357 * setup of the inode and release the inode. This prevents recursive 358 * transactions and deadlocks from xfs_inactive. 359 */ 360 if (ip) { 361 xfs_finish_inode_setup(ip); 362 xfs_irele(ip); 363 } 364 out_release_dquots: 365 xfs_qm_dqrele(udqp); 366 xfs_qm_dqrele(gdqp); 367 xfs_qm_dqrele(pdqp); 368 369 if (unlock_dp_on_error) 370 xfs_iunlock(dp, XFS_ILOCK_EXCL); 371 return error; 372 } 373 374 /* 375 * Free a symlink that has blocks associated with it. 376 * 377 * Note: zero length symlinks are not allowed to exist. When we set the size to 378 * zero, also change it to a regular file so that it does not get written to 379 * disk as a zero length symlink. The inode is on the unlinked list already, so 380 * userspace cannot find this inode anymore, so this change is not user visible 381 * but allows us to catch corrupt zero-length symlinks in the verifiers. 382 */ 383 STATIC int 384 xfs_inactive_symlink_rmt( 385 struct xfs_inode *ip) 386 { 387 struct xfs_buf *bp; 388 int done; 389 int error; 390 int i; 391 xfs_mount_t *mp; 392 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS]; 393 int nmaps; 394 int size; 395 xfs_trans_t *tp; 396 397 mp = ip->i_mount; 398 ASSERT(!xfs_need_iread_extents(&ip->i_df)); 399 /* 400 * We're freeing a symlink that has some 401 * blocks allocated to it. Free the 402 * blocks here. We know that we've got 403 * either 1 or 2 extents and that we can 404 * free them all in one bunmapi call. 405 */ 406 ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2); 407 408 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); 409 if (error) 410 return error; 411 412 xfs_ilock(ip, XFS_ILOCK_EXCL); 413 xfs_trans_ijoin(tp, ip, 0); 414 415 /* 416 * Lock the inode, fix the size, turn it into a regular file and join it 417 * to the transaction. Hold it so in the normal path, we still have it 418 * locked for the second transaction. In the error paths we need it 419 * held so the cancel won't rele it, see below. 420 */ 421 size = (int)ip->i_disk_size; 422 ip->i_disk_size = 0; 423 VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG; 424 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 425 /* 426 * Find the block(s) so we can inval and unmap them. 427 */ 428 done = 0; 429 nmaps = ARRAY_SIZE(mval); 430 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size), 431 mval, &nmaps, 0); 432 if (error) 433 goto error_trans_cancel; 434 /* 435 * Invalidate the block(s). No validation is done. 436 */ 437 for (i = 0; i < nmaps; i++) { 438 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, 439 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock), 440 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0, 441 &bp); 442 if (error) 443 goto error_trans_cancel; 444 xfs_trans_binval(tp, bp); 445 } 446 /* 447 * Unmap the dead block(s) to the dfops. 448 */ 449 error = xfs_bunmapi(tp, ip, 0, size, 0, nmaps, &done); 450 if (error) 451 goto error_trans_cancel; 452 ASSERT(done); 453 454 /* 455 * Commit the transaction. This first logs the EFI and the inode, then 456 * rolls and commits the transaction that frees the extents. 457 */ 458 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 459 error = xfs_trans_commit(tp); 460 if (error) { 461 ASSERT(xfs_is_shutdown(mp)); 462 goto error_unlock; 463 } 464 465 /* 466 * Remove the memory for extent descriptions (just bookkeeping). 467 */ 468 if (ip->i_df.if_bytes) 469 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK); 470 ASSERT(ip->i_df.if_bytes == 0); 471 472 xfs_iunlock(ip, XFS_ILOCK_EXCL); 473 return 0; 474 475 error_trans_cancel: 476 xfs_trans_cancel(tp); 477 error_unlock: 478 xfs_iunlock(ip, XFS_ILOCK_EXCL); 479 return error; 480 } 481 482 /* 483 * xfs_inactive_symlink - free a symlink 484 */ 485 int 486 xfs_inactive_symlink( 487 struct xfs_inode *ip) 488 { 489 struct xfs_mount *mp = ip->i_mount; 490 int pathlen; 491 492 trace_xfs_inactive_symlink(ip); 493 494 if (xfs_is_shutdown(mp)) 495 return -EIO; 496 497 xfs_ilock(ip, XFS_ILOCK_EXCL); 498 pathlen = (int)ip->i_disk_size; 499 ASSERT(pathlen); 500 501 if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) { 502 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)", 503 __func__, (unsigned long long)ip->i_ino, pathlen); 504 xfs_iunlock(ip, XFS_ILOCK_EXCL); 505 ASSERT(0); 506 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK); 507 return -EFSCORRUPTED; 508 } 509 510 /* 511 * Inline fork state gets removed by xfs_difree() so we have nothing to 512 * do here in that case. 513 */ 514 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) { 515 xfs_iunlock(ip, XFS_ILOCK_EXCL); 516 return 0; 517 } 518 519 xfs_iunlock(ip, XFS_ILOCK_EXCL); 520 521 /* remove the remote symlink */ 522 return xfs_inactive_symlink_rmt(ip); 523 } 524