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