Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_trans.h" |
| 26 | #include "xfs_sb.h" |
| 27 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dir2.h" |
| 29 | #include "xfs_dmapi.h" |
| 30 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_ialloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_dir2_sf.h" |
| 36 | #include "xfs_attr_sf.h" |
| 37 | #include "xfs_dinode.h" |
| 38 | #include "xfs_inode.h" |
| 39 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_itable.h" |
| 41 | #include "xfs_btree.h" |
| 42 | #include "xfs_ialloc.h" |
| 43 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "xfs_attr.h" |
| 46 | #include "xfs_rw.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include "xfs_quota.h" |
| 49 | #include "xfs_utils.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 50 | #include "xfs_rtalloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "xfs_trans_space.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include "xfs_log_priv.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 53 | #include "xfs_filestream.h" |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 54 | #include "xfs_vnodeops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 56 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | xfs_open( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 58 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | int mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 63 | return XFS_ERROR(EIO); |
| 64 | |
| 65 | /* |
| 66 | * If it's a directory with any blocks, read-ahead block 0 |
| 67 | * as we're almost certain to have the next operation be a read there. |
| 68 | */ |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 69 | if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | mode = xfs_ilock_map_shared(ip); |
| 71 | if (ip->i_d.di_nextents > 0) |
| 72 | (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK); |
| 73 | xfs_iunlock(ip, mode); |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* |
| 79 | * xfs_getattr |
| 80 | */ |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 81 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | xfs_getattr( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 83 | xfs_inode_t *ip, |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 84 | bhv_vattr_t *vap, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 85 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 87 | bhv_vnode_t *vp = XFS_ITOV(ip); |
| 88 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 90 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 93 | return XFS_ERROR(EIO); |
| 94 | |
| 95 | if (!(flags & ATTR_LAZY)) |
| 96 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 97 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 98 | vap->va_size = XFS_ISIZE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (vap->va_mask == XFS_AT_SIZE) |
| 100 | goto all_done; |
| 101 | |
| 102 | vap->va_nblocks = |
| 103 | XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks); |
| 104 | vap->va_nodeid = ip->i_ino; |
| 105 | #if XFS_BIG_INUMS |
| 106 | vap->va_nodeid += mp->m_inoadd; |
| 107 | #endif |
| 108 | vap->va_nlink = ip->i_d.di_nlink; |
| 109 | |
| 110 | /* |
| 111 | * Quick exit for non-stat callers |
| 112 | */ |
| 113 | if ((vap->va_mask & |
| 114 | ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID| |
| 115 | XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0) |
| 116 | goto all_done; |
| 117 | |
| 118 | /* |
| 119 | * Copy from in-core inode. |
| 120 | */ |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 121 | vap->va_mode = ip->i_d.di_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | vap->va_uid = ip->i_d.di_uid; |
| 123 | vap->va_gid = ip->i_d.di_gid; |
| 124 | vap->va_projid = ip->i_d.di_projid; |
| 125 | |
| 126 | /* |
| 127 | * Check vnode type block/char vs. everything else. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | */ |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 129 | switch (ip->i_d.di_mode & S_IFMT) { |
| 130 | case S_IFBLK: |
| 131 | case S_IFCHR: |
| 132 | vap->va_rdev = ip->i_df.if_u2.if_rdev; |
| 133 | vap->va_blocksize = BLKDEV_IOSIZE; |
| 134 | break; |
| 135 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | vap->va_rdev = 0; |
| 137 | |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 138 | if (!(XFS_IS_REALTIME_INODE(ip))) { |
David Chinner | e8c8b3a | 2005-11-02 10:33:05 +1100 | [diff] [blame] | 139 | vap->va_blocksize = xfs_preferred_iosize(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } else { |
| 141 | |
| 142 | /* |
| 143 | * If the file blocks are being allocated from a |
| 144 | * realtime partition, then return the inode's |
| 145 | * realtime extent size or the realtime volume's |
| 146 | * extent size. |
| 147 | */ |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 148 | vap->va_blocksize = |
| 149 | xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 151 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Nathan Scott | ca5ccbf | 2006-01-11 21:03:04 +1100 | [diff] [blame] | 154 | vn_atime_to_timespec(vp, &vap->va_atime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec; |
| 156 | vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec; |
| 157 | vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec; |
| 158 | vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec; |
| 159 | |
| 160 | /* |
| 161 | * Exit for stat callers. See if any of the rest of the fields |
| 162 | * to be filled in are needed. |
| 163 | */ |
| 164 | if ((vap->va_mask & |
| 165 | (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS| |
| 166 | XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0) |
| 167 | goto all_done; |
| 168 | |
| 169 | /* |
| 170 | * Convert di_flags to xflags. |
| 171 | */ |
| 172 | vap->va_xflags = xfs_ip2xflags(ip); |
| 173 | |
| 174 | /* |
| 175 | * Exit for inode revalidate. See if any of the rest of |
| 176 | * the fields to be filled in are needed. |
| 177 | */ |
| 178 | if ((vap->va_mask & |
| 179 | (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS| |
| 180 | XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0) |
| 181 | goto all_done; |
| 182 | |
| 183 | vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog; |
| 184 | vap->va_nextents = |
| 185 | (ip->i_df.if_flags & XFS_IFEXTENTS) ? |
| 186 | ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) : |
| 187 | ip->i_d.di_nextents; |
| 188 | if (ip->i_afp) |
| 189 | vap->va_anextents = |
| 190 | (ip->i_afp->if_flags & XFS_IFEXTENTS) ? |
| 191 | ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) : |
| 192 | ip->i_d.di_anextents; |
| 193 | else |
| 194 | vap->va_anextents = 0; |
| 195 | vap->va_gen = ip->i_d.di_gen; |
| 196 | |
| 197 | all_done: |
| 198 | if (!(flags & ATTR_LAZY)) |
| 199 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /* |
| 205 | * xfs_setattr |
| 206 | */ |
| 207 | int |
| 208 | xfs_setattr( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 209 | xfs_inode_t *ip, |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 210 | bhv_vattr_t *vap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | int flags, |
| 212 | cred_t *credp) |
| 213 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 214 | bhv_vnode_t *vp = XFS_ITOV(ip); |
| 215 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | int mask; |
| 218 | int code; |
| 219 | uint lock_flags; |
| 220 | uint commit_flags=0; |
| 221 | uid_t uid=0, iuid=0; |
| 222 | gid_t gid=0, igid=0; |
| 223 | int timeflags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | xfs_prid_t projid=0, iprojid=0; |
| 225 | int mandlock_before, mandlock_after; |
| 226 | struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2; |
| 227 | int file_owner; |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 228 | int need_iolock = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 230 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 232 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | return XFS_ERROR(EROFS); |
| 234 | |
| 235 | /* |
| 236 | * Cannot set certain attributes. |
| 237 | */ |
| 238 | mask = vap->va_mask; |
| 239 | if (mask & XFS_AT_NOSET) { |
| 240 | return XFS_ERROR(EINVAL); |
| 241 | } |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 244 | return XFS_ERROR(EIO); |
| 245 | |
| 246 | /* |
| 247 | * Timestamps do not need to be logged and hence do not |
| 248 | * need to be done within a transaction. |
| 249 | */ |
| 250 | if (mask & XFS_AT_UPDTIMES) { |
| 251 | ASSERT((mask & ~XFS_AT_UPDTIMES) == 0); |
| 252 | timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) | |
| 253 | ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) | |
| 254 | ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0); |
| 255 | xfs_ichgtime(ip, timeflags); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | olddquot1 = olddquot2 = NULL; |
| 260 | udqp = gdqp = NULL; |
| 261 | |
| 262 | /* |
| 263 | * If disk quotas is on, we make sure that the dquots do exist on disk, |
| 264 | * before we start any other transactions. Trying to do this later |
| 265 | * is messy. We don't care to take a readlock to look at the ids |
| 266 | * in inode here, because we can't hold it across the trans_reserve. |
| 267 | * If the IDs do change before we take the ilock, we're covered |
| 268 | * because the i_*dquot fields will get updated anyway. |
| 269 | */ |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 270 | if (XFS_IS_QUOTA_ON(mp) && |
| 271 | (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | uint qflags = 0; |
| 273 | |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 274 | if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | uid = vap->va_uid; |
| 276 | qflags |= XFS_QMOPT_UQUOTA; |
| 277 | } else { |
| 278 | uid = ip->i_d.di_uid; |
| 279 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 280 | if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | gid = vap->va_gid; |
| 282 | qflags |= XFS_QMOPT_GQUOTA; |
| 283 | } else { |
| 284 | gid = ip->i_d.di_gid; |
| 285 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 286 | if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) { |
| 287 | projid = vap->va_projid; |
| 288 | qflags |= XFS_QMOPT_PQUOTA; |
| 289 | } else { |
| 290 | projid = ip->i_d.di_projid; |
| 291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | /* |
| 293 | * We take a reference when we initialize udqp and gdqp, |
| 294 | * so it is important that we never blindly double trip on |
| 295 | * the same variable. See xfs_create() for an example. |
| 296 | */ |
| 297 | ASSERT(udqp == NULL); |
| 298 | ASSERT(gdqp == NULL); |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 299 | code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags, |
| 300 | &udqp, &gdqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | if (code) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 302 | return code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* |
| 306 | * For the other attributes, we acquire the inode lock and |
| 307 | * first do an error checking pass. |
| 308 | */ |
| 309 | tp = NULL; |
| 310 | lock_flags = XFS_ILOCK_EXCL; |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 311 | if (flags & ATTR_NOLOCK) |
| 312 | need_iolock = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | if (!(mask & XFS_AT_SIZE)) { |
| 314 | if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) || |
| 315 | (mp->m_flags & XFS_MOUNT_WSYNC)) { |
| 316 | tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE); |
| 317 | commit_flags = 0; |
| 318 | if ((code = xfs_trans_reserve(tp, 0, |
| 319 | XFS_ICHANGE_LOG_RES(mp), 0, |
| 320 | 0, 0))) { |
| 321 | lock_flags = 0; |
| 322 | goto error_return; |
| 323 | } |
| 324 | } |
| 325 | } else { |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 326 | if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | !(flags & ATTR_DMI)) { |
| 328 | int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR; |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 329 | code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | vap->va_size, 0, dmflags, NULL); |
| 331 | if (code) { |
| 332 | lock_flags = 0; |
| 333 | goto error_return; |
| 334 | } |
| 335 | } |
| 336 | if (need_iolock) |
| 337 | lock_flags |= XFS_IOLOCK_EXCL; |
| 338 | } |
| 339 | |
| 340 | xfs_ilock(ip, lock_flags); |
| 341 | |
| 342 | /* boolean: are we the file owner? */ |
| 343 | file_owner = (current_fsuid(credp) == ip->i_d.di_uid); |
| 344 | |
| 345 | /* |
| 346 | * Change various properties of a file. |
| 347 | * Only the owner or users with CAP_FOWNER |
| 348 | * capability may do these things. |
| 349 | */ |
| 350 | if (mask & |
| 351 | (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID| |
| 352 | XFS_AT_GID|XFS_AT_PROJID)) { |
| 353 | /* |
| 354 | * CAP_FOWNER overrides the following restrictions: |
| 355 | * |
| 356 | * The user ID of the calling process must be equal |
| 357 | * to the file owner ID, except in cases where the |
| 358 | * CAP_FSETID capability is applicable. |
| 359 | */ |
| 360 | if (!file_owner && !capable(CAP_FOWNER)) { |
| 361 | code = XFS_ERROR(EPERM); |
| 362 | goto error_return; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * CAP_FSETID overrides the following restrictions: |
| 367 | * |
| 368 | * The effective user ID of the calling process shall match |
| 369 | * the file owner when setting the set-user-ID and |
| 370 | * set-group-ID bits on that file. |
| 371 | * |
| 372 | * The effective group ID or one of the supplementary group |
| 373 | * IDs of the calling process shall match the group owner of |
| 374 | * the file when setting the set-group-ID bit on that file |
| 375 | */ |
| 376 | if (mask & XFS_AT_MODE) { |
| 377 | mode_t m = 0; |
| 378 | |
| 379 | if ((vap->va_mode & S_ISUID) && !file_owner) |
| 380 | m |= S_ISUID; |
| 381 | if ((vap->va_mode & S_ISGID) && |
| 382 | !in_group_p((gid_t)ip->i_d.di_gid)) |
| 383 | m |= S_ISGID; |
| 384 | #if 0 |
| 385 | /* Linux allows this, Irix doesn't. */ |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 386 | if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | m |= S_ISVTX; |
| 388 | #endif |
| 389 | if (m && !capable(CAP_FSETID)) |
| 390 | vap->va_mode &= ~m; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Change file ownership. Must be the owner or privileged. |
| 396 | * If the system was configured with the "restricted_chown" |
| 397 | * option, the owner is not permitted to give away the file, |
| 398 | * and can change the group id only to a group of which he |
| 399 | * or she is a member. |
| 400 | */ |
| 401 | if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) { |
| 402 | /* |
| 403 | * These IDs could have changed since we last looked at them. |
| 404 | * But, we're assured that if the ownership did change |
| 405 | * while we didn't have the inode locked, inode's dquot(s) |
| 406 | * would have changed also. |
| 407 | */ |
| 408 | iuid = ip->i_d.di_uid; |
| 409 | iprojid = ip->i_d.di_projid; |
| 410 | igid = ip->i_d.di_gid; |
| 411 | gid = (mask & XFS_AT_GID) ? vap->va_gid : igid; |
| 412 | uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid; |
| 413 | projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid : |
| 414 | iprojid; |
| 415 | |
| 416 | /* |
| 417 | * CAP_CHOWN overrides the following restrictions: |
| 418 | * |
| 419 | * If _POSIX_CHOWN_RESTRICTED is defined, this capability |
| 420 | * shall override the restriction that a process cannot |
| 421 | * change the user ID of a file it owns and the restriction |
| 422 | * that the group ID supplied to the chown() function |
| 423 | * shall be equal to either the group ID or one of the |
| 424 | * supplementary group IDs of the calling process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | */ |
| 426 | if (restricted_chown && |
| 427 | (iuid != uid || (igid != gid && |
| 428 | !in_group_p((gid_t)gid))) && |
| 429 | !capable(CAP_CHOWN)) { |
| 430 | code = XFS_ERROR(EPERM); |
| 431 | goto error_return; |
| 432 | } |
| 433 | /* |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 434 | * Do a quota reservation only if uid/projid/gid is actually |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | * going to change. |
| 436 | */ |
| 437 | if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) || |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 438 | (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | (XFS_IS_GQUOTA_ON(mp) && igid != gid)) { |
| 440 | ASSERT(tp); |
| 441 | code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp, |
| 442 | capable(CAP_FOWNER) ? |
| 443 | XFS_QMOPT_FORCE_RES : 0); |
| 444 | if (code) /* out of quota */ |
| 445 | goto error_return; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Truncate file. Must have write permission and not be a directory. |
| 451 | */ |
| 452 | if (mask & XFS_AT_SIZE) { |
| 453 | /* Short circuit the truncate case for zero length files */ |
| 454 | if ((vap->va_size == 0) && |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 455 | (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 457 | lock_flags &= ~XFS_ILOCK_EXCL; |
| 458 | if (mask & XFS_AT_CTIME) |
| 459 | xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 460 | code = 0; |
| 461 | goto error_return; |
| 462 | } |
| 463 | |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 464 | if (VN_ISDIR(vp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | code = XFS_ERROR(EISDIR); |
| 466 | goto error_return; |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 467 | } else if (!VN_ISREG(vp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | code = XFS_ERROR(EINVAL); |
| 469 | goto error_return; |
| 470 | } |
| 471 | /* |
| 472 | * Make sure that the dquots are attached to the inode. |
| 473 | */ |
| 474 | if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED))) |
| 475 | goto error_return; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Change file access or modified times. |
| 480 | */ |
| 481 | if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) { |
| 482 | if (!file_owner) { |
| 483 | if ((flags & ATTR_UTIME) && |
| 484 | !capable(CAP_FOWNER)) { |
| 485 | code = XFS_ERROR(EPERM); |
| 486 | goto error_return; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Change extent size or realtime flag. |
| 493 | */ |
| 494 | if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) { |
| 495 | /* |
| 496 | * Can't change extent size if any extents are allocated. |
| 497 | */ |
Eric Sandeen | e94af02 | 2005-11-02 15:10:41 +1100 | [diff] [blame] | 498 | if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != |
| 500 | vap->va_extsize) ) { |
| 501 | code = XFS_ERROR(EINVAL); /* EFBIG? */ |
| 502 | goto error_return; |
| 503 | } |
| 504 | |
| 505 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | * Can't change realtime flag if any extents are allocated. |
| 507 | */ |
Eric Sandeen | e94af02 | 2005-11-02 15:10:41 +1100 | [diff] [blame] | 508 | if ((ip->i_d.di_nextents || ip->i_delayed_blks) && |
| 509 | (mask & XFS_AT_XFLAGS) && |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 510 | (XFS_IS_REALTIME_INODE(ip)) != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | (vap->va_xflags & XFS_XFLAG_REALTIME)) { |
| 512 | code = XFS_ERROR(EINVAL); /* EFBIG? */ |
| 513 | goto error_return; |
| 514 | } |
| 515 | /* |
| 516 | * Extent size must be a multiple of the appropriate block |
| 517 | * size, if set at all. |
| 518 | */ |
| 519 | if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) { |
| 520 | xfs_extlen_t size; |
| 521 | |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 522 | if (XFS_IS_REALTIME_INODE(ip) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | ((mask & XFS_AT_XFLAGS) && |
| 524 | (vap->va_xflags & XFS_XFLAG_REALTIME))) { |
| 525 | size = mp->m_sb.sb_rextsize << |
| 526 | mp->m_sb.sb_blocklog; |
| 527 | } else { |
| 528 | size = mp->m_sb.sb_blocksize; |
| 529 | } |
| 530 | if (vap->va_extsize % size) { |
| 531 | code = XFS_ERROR(EINVAL); |
| 532 | goto error_return; |
| 533 | } |
| 534 | } |
| 535 | /* |
| 536 | * If realtime flag is set then must have realtime data. |
| 537 | */ |
| 538 | if ((mask & XFS_AT_XFLAGS) && |
| 539 | (vap->va_xflags & XFS_XFLAG_REALTIME)) { |
| 540 | if ((mp->m_sb.sb_rblocks == 0) || |
| 541 | (mp->m_sb.sb_rextsize == 0) || |
| 542 | (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) { |
| 543 | code = XFS_ERROR(EINVAL); |
| 544 | goto error_return; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Can't modify an immutable/append-only file unless |
| 550 | * we have appropriate permission. |
| 551 | */ |
| 552 | if ((mask & XFS_AT_XFLAGS) && |
| 553 | (ip->i_d.di_flags & |
| 554 | (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) || |
| 555 | (vap->va_xflags & |
| 556 | (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) && |
| 557 | !capable(CAP_LINUX_IMMUTABLE)) { |
| 558 | code = XFS_ERROR(EPERM); |
| 559 | goto error_return; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * Now we can make the changes. Before we join the inode |
| 565 | * to the transaction, if XFS_AT_SIZE is set then take care of |
| 566 | * the part of the truncation that must be done without the |
| 567 | * inode lock. This needs to be done before joining the inode |
| 568 | * to the transaction, because the inode cannot be unlocked |
| 569 | * once it is a part of the transaction. |
| 570 | */ |
| 571 | if (mask & XFS_AT_SIZE) { |
| 572 | code = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 573 | if ((vap->va_size > ip->i_size) && |
Eric Sandeen | 374e2ac | 2005-11-02 15:07:34 +1100 | [diff] [blame] | 574 | (flags & ATTR_NOSIZETOK) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | code = xfs_igrow_start(ip, vap->va_size, credp); |
Eric Sandeen | 374e2ac | 2005-11-02 15:07:34 +1100 | [diff] [blame] | 576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
David Chinner | c32676e | 2007-07-19 16:28:58 +1000 | [diff] [blame] | 578 | |
| 579 | /* |
| 580 | * We are going to log the inode size change in this |
| 581 | * transaction so any previous writes that are beyond the on |
| 582 | * disk EOF and the new EOF that have not been written out need |
| 583 | * to be written here. If we do not write the data out, we |
| 584 | * expose ourselves to the null files problem. |
| 585 | * |
| 586 | * Only flush from the on disk size to the smaller of the in |
| 587 | * memory file size or the new size as that's the range we |
| 588 | * really care about here and prevents waiting for other data |
| 589 | * not within the range we care about here. |
| 590 | */ |
| 591 | if (!code && |
| 592 | (ip->i_size != ip->i_d.di_size) && |
| 593 | (vap->va_size > ip->i_d.di_size)) { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 594 | code = xfs_flush_pages(ip, |
David Chinner | c32676e | 2007-07-19 16:28:58 +1000 | [diff] [blame] | 595 | ip->i_d.di_size, vap->va_size, |
| 596 | XFS_B_ASYNC, FI_NONE); |
| 597 | } |
| 598 | |
| 599 | /* wait for all I/O to complete */ |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 600 | vn_iowait(ip); |
David Chinner | c32676e | 2007-07-19 16:28:58 +1000 | [diff] [blame] | 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | if (!code) |
| 603 | code = xfs_itruncate_data(ip, vap->va_size); |
| 604 | if (code) { |
| 605 | ASSERT(tp == NULL); |
| 606 | lock_flags &= ~XFS_ILOCK_EXCL; |
| 607 | ASSERT(lock_flags == XFS_IOLOCK_EXCL); |
| 608 | goto error_return; |
| 609 | } |
| 610 | tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE); |
| 611 | if ((code = xfs_trans_reserve(tp, 0, |
| 612 | XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 613 | XFS_TRANS_PERM_LOG_RES, |
| 614 | XFS_ITRUNCATE_LOG_COUNT))) { |
| 615 | xfs_trans_cancel(tp, 0); |
| 616 | if (need_iolock) |
| 617 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 618 | return code; |
| 619 | } |
| 620 | commit_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 621 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 622 | } |
| 623 | |
| 624 | if (tp) { |
| 625 | xfs_trans_ijoin(tp, ip, lock_flags); |
| 626 | xfs_trans_ihold(tp, ip); |
| 627 | } |
| 628 | |
| 629 | /* determine whether mandatory locking mode changes */ |
| 630 | mandlock_before = MANDLOCK(vp, ip->i_d.di_mode); |
| 631 | |
| 632 | /* |
| 633 | * Truncate file. Must have write permission and not be a directory. |
| 634 | */ |
| 635 | if (mask & XFS_AT_SIZE) { |
David Chinner | 44d814c | 2008-03-06 13:45:29 +1100 | [diff] [blame] | 636 | /* |
| 637 | * Only change the c/mtime if we are changing the size |
| 638 | * or we are explicitly asked to change it. This handles |
| 639 | * the semantic difference between truncate() and ftruncate() |
| 640 | * as implemented in the VFS. |
| 641 | */ |
| 642 | if (vap->va_size != ip->i_size || (mask & XFS_AT_CTIME)) |
| 643 | timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; |
| 644 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 645 | if (vap->va_size > ip->i_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | xfs_igrow_finish(tp, ip, vap->va_size, |
| 647 | !(flags & ATTR_DMI)); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 648 | } else if ((vap->va_size <= ip->i_size) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | ((vap->va_size == 0) && ip->i_d.di_nextents)) { |
| 650 | /* |
| 651 | * signal a sync transaction unless |
| 652 | * we're truncating an already unlinked |
| 653 | * file on a wsync filesystem |
| 654 | */ |
| 655 | code = xfs_itruncate_finish(&tp, ip, |
| 656 | (xfs_fsize_t)vap->va_size, |
| 657 | XFS_DATA_FORK, |
| 658 | ((ip->i_d.di_nlink != 0 || |
| 659 | !(mp->m_flags & XFS_MOUNT_WSYNC)) |
| 660 | ? 1 : 0)); |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 661 | if (code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | goto abort_return; |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 663 | /* |
| 664 | * Truncated "down", so we're removing references |
| 665 | * to old data here - if we now delay flushing for |
| 666 | * a long time, we expose ourselves unduly to the |
| 667 | * notorious NULL files problem. So, we mark this |
| 668 | * vnode and flush it when the file is closed, and |
| 669 | * do not wait the usual (long) time for writeout. |
| 670 | */ |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 671 | xfs_iflags_set(ip, XFS_ITRUNCATED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Change file access modes. |
| 677 | */ |
| 678 | if (mask & XFS_AT_MODE) { |
| 679 | ip->i_d.di_mode &= S_IFMT; |
| 680 | ip->i_d.di_mode |= vap->va_mode & ~S_IFMT; |
| 681 | |
| 682 | xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE); |
| 683 | timeflags |= XFS_ICHGTIME_CHG; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Change file ownership. Must be the owner or privileged. |
| 688 | * If the system was configured with the "restricted_chown" |
| 689 | * option, the owner is not permitted to give away the file, |
| 690 | * and can change the group id only to a group of which he |
| 691 | * or she is a member. |
| 692 | */ |
| 693 | if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) { |
| 694 | /* |
| 695 | * CAP_FSETID overrides the following restrictions: |
| 696 | * |
| 697 | * The set-user-ID and set-group-ID bits of a file will be |
| 698 | * cleared upon successful return from chown() |
| 699 | */ |
| 700 | if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) && |
| 701 | !capable(CAP_FSETID)) { |
| 702 | ip->i_d.di_mode &= ~(S_ISUID|S_ISGID); |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Change the ownerships and register quota modifications |
| 707 | * in the transaction. |
| 708 | */ |
| 709 | if (iuid != uid) { |
| 710 | if (XFS_IS_UQUOTA_ON(mp)) { |
| 711 | ASSERT(mask & XFS_AT_UID); |
| 712 | ASSERT(udqp); |
| 713 | olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip, |
| 714 | &ip->i_udquot, udqp); |
| 715 | } |
| 716 | ip->i_d.di_uid = uid; |
| 717 | } |
| 718 | if (igid != gid) { |
| 719 | if (XFS_IS_GQUOTA_ON(mp)) { |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 720 | ASSERT(!XFS_IS_PQUOTA_ON(mp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | ASSERT(mask & XFS_AT_GID); |
| 722 | ASSERT(gdqp); |
| 723 | olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip, |
| 724 | &ip->i_gdquot, gdqp); |
| 725 | } |
| 726 | ip->i_d.di_gid = gid; |
| 727 | } |
| 728 | if (iprojid != projid) { |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 729 | if (XFS_IS_PQUOTA_ON(mp)) { |
| 730 | ASSERT(!XFS_IS_GQUOTA_ON(mp)); |
| 731 | ASSERT(mask & XFS_AT_PROJID); |
| 732 | ASSERT(gdqp); |
| 733 | olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip, |
| 734 | &ip->i_gdquot, gdqp); |
| 735 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | ip->i_d.di_projid = projid; |
| 737 | /* |
| 738 | * We may have to rev the inode as well as |
| 739 | * the superblock version number since projids didn't |
| 740 | * exist before DINODE_VERSION_2 and SB_VERSION_NLINK. |
| 741 | */ |
| 742 | if (ip->i_d.di_version == XFS_DINODE_VERSION_1) |
| 743 | xfs_bump_ino_vers2(tp, ip); |
| 744 | } |
| 745 | |
| 746 | xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE); |
| 747 | timeflags |= XFS_ICHGTIME_CHG; |
| 748 | } |
| 749 | |
| 750 | |
| 751 | /* |
| 752 | * Change file access or modified times. |
| 753 | */ |
| 754 | if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) { |
| 755 | if (mask & XFS_AT_ATIME) { |
| 756 | ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec; |
| 757 | ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec; |
| 758 | ip->i_update_core = 1; |
| 759 | timeflags &= ~XFS_ICHGTIME_ACC; |
| 760 | } |
| 761 | if (mask & XFS_AT_MTIME) { |
| 762 | ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec; |
| 763 | ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec; |
| 764 | timeflags &= ~XFS_ICHGTIME_MOD; |
| 765 | timeflags |= XFS_ICHGTIME_CHG; |
| 766 | } |
| 767 | if (tp && (flags & ATTR_UTIME)) |
| 768 | xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE); |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * Change XFS-added attributes. |
| 773 | */ |
| 774 | if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) { |
| 775 | if (mask & XFS_AT_EXTSIZE) { |
| 776 | /* |
| 777 | * Converting bytes to fs blocks. |
| 778 | */ |
| 779 | ip->i_d.di_extsize = vap->va_extsize >> |
| 780 | mp->m_sb.sb_blocklog; |
| 781 | } |
| 782 | if (mask & XFS_AT_XFLAGS) { |
| 783 | uint di_flags; |
| 784 | |
| 785 | /* can't set PREALLOC this way, just preserve it */ |
| 786 | di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC); |
| 787 | if (vap->va_xflags & XFS_XFLAG_IMMUTABLE) |
| 788 | di_flags |= XFS_DIFLAG_IMMUTABLE; |
| 789 | if (vap->va_xflags & XFS_XFLAG_APPEND) |
| 790 | di_flags |= XFS_DIFLAG_APPEND; |
| 791 | if (vap->va_xflags & XFS_XFLAG_SYNC) |
| 792 | di_flags |= XFS_DIFLAG_SYNC; |
| 793 | if (vap->va_xflags & XFS_XFLAG_NOATIME) |
| 794 | di_flags |= XFS_DIFLAG_NOATIME; |
| 795 | if (vap->va_xflags & XFS_XFLAG_NODUMP) |
| 796 | di_flags |= XFS_DIFLAG_NODUMP; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 797 | if (vap->va_xflags & XFS_XFLAG_PROJINHERIT) |
| 798 | di_flags |= XFS_DIFLAG_PROJINHERIT; |
Barry Naujok | d3446ea | 2006-06-09 14:54:19 +1000 | [diff] [blame] | 799 | if (vap->va_xflags & XFS_XFLAG_NODEFRAG) |
| 800 | di_flags |= XFS_DIFLAG_NODEFRAG; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 801 | if (vap->va_xflags & XFS_XFLAG_FILESTREAM) |
| 802 | di_flags |= XFS_DIFLAG_FILESTREAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { |
| 804 | if (vap->va_xflags & XFS_XFLAG_RTINHERIT) |
| 805 | di_flags |= XFS_DIFLAG_RTINHERIT; |
| 806 | if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS) |
| 807 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 808 | if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT) |
| 809 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; |
| 810 | } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) { |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 811 | if (vap->va_xflags & XFS_XFLAG_REALTIME) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | di_flags |= XFS_DIFLAG_REALTIME; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 813 | if (vap->va_xflags & XFS_XFLAG_EXTSIZE) |
| 814 | di_flags |= XFS_DIFLAG_EXTSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |
| 816 | ip->i_d.di_flags = di_flags; |
| 817 | } |
| 818 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 819 | timeflags |= XFS_ICHGTIME_CHG; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * Change file inode change time only if XFS_AT_CTIME set |
| 824 | * AND we have been called by a DMI function. |
| 825 | */ |
| 826 | |
| 827 | if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) { |
| 828 | ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec; |
| 829 | ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec; |
| 830 | ip->i_update_core = 1; |
| 831 | timeflags &= ~XFS_ICHGTIME_CHG; |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | * Send out timestamp changes that need to be set to the |
| 836 | * current time. Not done when called by a DMI function. |
| 837 | */ |
| 838 | if (timeflags && !(flags & ATTR_DMI)) |
| 839 | xfs_ichgtime(ip, timeflags); |
| 840 | |
| 841 | XFS_STATS_INC(xs_ig_attrchg); |
| 842 | |
| 843 | /* |
| 844 | * If this is a synchronous mount, make sure that the |
| 845 | * transaction goes to disk before returning to the user. |
| 846 | * This is slightly sub-optimal in that truncates require |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 847 | * two sync transactions instead of one for wsync filesystems. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | * One for the truncate and one for the timestamps since we |
| 849 | * don't want to change the timestamps unless we're sure the |
| 850 | * truncate worked. Truncates are less than 1% of the laddis |
| 851 | * mix so this probably isn't worth the trouble to optimize. |
| 852 | */ |
| 853 | code = 0; |
| 854 | if (tp) { |
| 855 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
| 856 | xfs_trans_set_sync(tp); |
| 857 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 858 | code = xfs_trans_commit(tp, commit_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* |
| 862 | * If the (regular) file's mandatory locking mode changed, then |
| 863 | * notify the vnode. We do this under the inode lock to prevent |
| 864 | * racing calls to vop_vnode_change. |
| 865 | */ |
| 866 | mandlock_after = MANDLOCK(vp, ip->i_d.di_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | xfs_iunlock(ip, lock_flags); |
| 869 | |
| 870 | /* |
| 871 | * Release any dquot(s) the inode had kept before chown. |
| 872 | */ |
| 873 | XFS_QM_DQRELE(mp, olddquot1); |
| 874 | XFS_QM_DQRELE(mp, olddquot2); |
| 875 | XFS_QM_DQRELE(mp, udqp); |
| 876 | XFS_QM_DQRELE(mp, gdqp); |
| 877 | |
| 878 | if (code) { |
| 879 | return code; |
| 880 | } |
| 881 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 882 | if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | !(flags & ATTR_DMI)) { |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 884 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | NULL, DM_RIGHT_NULL, NULL, NULL, |
| 886 | 0, 0, AT_DELAY_FLAG(flags)); |
| 887 | } |
| 888 | return 0; |
| 889 | |
| 890 | abort_return: |
| 891 | commit_flags |= XFS_TRANS_ABORT; |
| 892 | /* FALLTHROUGH */ |
| 893 | error_return: |
| 894 | XFS_QM_DQRELE(mp, udqp); |
| 895 | XFS_QM_DQRELE(mp, gdqp); |
| 896 | if (tp) { |
| 897 | xfs_trans_cancel(tp, commit_flags); |
| 898 | } |
| 899 | if (lock_flags != 0) { |
| 900 | xfs_iunlock(ip, lock_flags); |
| 901 | } |
| 902 | return code; |
| 903 | } |
| 904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | /* |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 906 | * The maximum pathlen is 1024 bytes. Since the minimum file system |
| 907 | * blocksize is 512 bytes, we can get a max of 2 extents back from |
| 908 | * bmapi. |
| 909 | */ |
| 910 | #define SYMLINK_MAPS 2 |
| 911 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 912 | STATIC int |
| 913 | xfs_readlink_bmap( |
| 914 | xfs_inode_t *ip, |
| 915 | char *link) |
| 916 | { |
| 917 | xfs_mount_t *mp = ip->i_mount; |
| 918 | int pathlen = ip->i_d.di_size; |
| 919 | int nmaps = SYMLINK_MAPS; |
| 920 | xfs_bmbt_irec_t mval[SYMLINK_MAPS]; |
| 921 | xfs_daddr_t d; |
| 922 | int byte_cnt; |
| 923 | int n; |
| 924 | xfs_buf_t *bp; |
| 925 | int error = 0; |
| 926 | |
| 927 | error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0, |
| 928 | mval, &nmaps, NULL, NULL); |
| 929 | if (error) |
| 930 | goto out; |
| 931 | |
| 932 | for (n = 0; n < nmaps; n++) { |
| 933 | d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); |
| 934 | byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); |
| 935 | |
| 936 | bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0); |
| 937 | error = XFS_BUF_GETERROR(bp); |
| 938 | if (error) { |
| 939 | xfs_ioerror_alert("xfs_readlink", |
| 940 | ip->i_mount, bp, XFS_BUF_ADDR(bp)); |
| 941 | xfs_buf_relse(bp); |
| 942 | goto out; |
| 943 | } |
| 944 | if (pathlen < byte_cnt) |
| 945 | byte_cnt = pathlen; |
| 946 | pathlen -= byte_cnt; |
| 947 | |
| 948 | memcpy(link, XFS_BUF_PTR(bp), byte_cnt); |
| 949 | xfs_buf_relse(bp); |
| 950 | } |
| 951 | |
| 952 | link[ip->i_d.di_size] = '\0'; |
| 953 | error = 0; |
| 954 | |
| 955 | out: |
| 956 | return error; |
| 957 | } |
| 958 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 959 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | xfs_readlink( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 961 | xfs_inode_t *ip, |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 962 | char *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | { |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 964 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | int pathlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 968 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
| 970 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 971 | return XFS_ERROR(EIO); |
| 972 | |
| 973 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 974 | |
| 975 | ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 976 | ASSERT(ip->i_d.di_size <= MAXPATHLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 978 | pathlen = ip->i_d.di_size; |
| 979 | if (!pathlen) |
| 980 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
| 982 | if (ip->i_df.if_flags & XFS_IFINLINE) { |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 983 | memcpy(link, ip->i_df.if_u1.if_data, pathlen); |
| 984 | link[pathlen] = '\0'; |
| 985 | } else { |
| 986 | error = xfs_readlink_bmap(ip, link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 989 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | return error; |
| 992 | } |
| 993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | /* |
| 995 | * xfs_fsync |
| 996 | * |
| 997 | * This is called to sync the inode and its data out to disk. |
| 998 | * We need to hold the I/O lock while flushing the data, and |
| 999 | * the inode lock while flushing the inode. The inode lock CANNOT |
| 1000 | * be held while flushing the data, so acquire after we're done |
| 1001 | * with that. |
| 1002 | */ |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1003 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | xfs_fsync( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1005 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | int flag, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | xfs_off_t start, |
| 1008 | xfs_off_t stop) |
| 1009 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | xfs_trans_t *tp; |
| 1011 | int error; |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1012 | int log_flushed = 0, changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1014 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
| 1016 | ASSERT(start >= 0 && stop >= -1); |
| 1017 | |
| 1018 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 1019 | return XFS_ERROR(EIO); |
| 1020 | |
Lachlan McIlroy | 776a75fa | 2007-09-14 15:22:50 +1000 | [diff] [blame] | 1021 | if (flag & FSYNC_DATA) |
| 1022 | filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping); |
| 1023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | /* |
| 1025 | * We always need to make sure that the required inode state |
| 1026 | * is safe on disk. The vnode might be clean but because |
| 1027 | * of committed transactions that haven't hit the disk yet. |
| 1028 | * Likewise, there could be unflushed non-transactional |
| 1029 | * changes to the inode core that have to go to disk. |
| 1030 | * |
| 1031 | * The following code depends on one assumption: that |
| 1032 | * any transaction that changes an inode logs the core |
| 1033 | * because it has to change some field in the inode core |
| 1034 | * (typically nextents or nblocks). That assumption |
| 1035 | * implies that any transactions against an inode will |
| 1036 | * catch any non-transactional updates. If inode-altering |
| 1037 | * transactions exist that violate this assumption, the |
| 1038 | * code breaks. Right now, it figures that if the involved |
| 1039 | * update_* field is clear and the inode is unpinned, the |
| 1040 | * inode is clean. Either it's been flushed or it's been |
| 1041 | * committed and the commit has hit the disk unpinning the inode. |
| 1042 | * (Note that xfs_inode_item_format() called at commit clears |
| 1043 | * the update_* fields.) |
| 1044 | */ |
| 1045 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 1046 | |
| 1047 | /* If we are flushing data then we care about update_size |
| 1048 | * being set, otherwise we care about update_core |
| 1049 | */ |
| 1050 | if ((flag & FSYNC_DATA) ? |
| 1051 | (ip->i_update_size == 0) : |
| 1052 | (ip->i_update_core == 0)) { |
| 1053 | /* |
| 1054 | * Timestamps/size haven't changed since last inode |
| 1055 | * flush or inode transaction commit. That means |
| 1056 | * either nothing got written or a transaction |
| 1057 | * committed which caught the updates. If the |
| 1058 | * latter happened and the transaction hasn't |
| 1059 | * hit the disk yet, the inode will be still |
| 1060 | * be pinned. If it is, force the log. |
| 1061 | */ |
| 1062 | |
| 1063 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 1064 | |
| 1065 | if (xfs_ipincount(ip)) { |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1066 | _xfs_log_force(ip->i_mount, (xfs_lsn_t)0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | XFS_LOG_FORCE | |
| 1068 | ((flag & FSYNC_WAIT) |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1069 | ? XFS_LOG_SYNC : 0), |
| 1070 | &log_flushed); |
| 1071 | } else { |
| 1072 | /* |
| 1073 | * If the inode is not pinned and nothing |
| 1074 | * has changed we don't need to flush the |
| 1075 | * cache. |
| 1076 | */ |
| 1077 | changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | } |
| 1079 | error = 0; |
| 1080 | } else { |
| 1081 | /* |
| 1082 | * Kick off a transaction to log the inode |
| 1083 | * core to get the updates. Make it |
| 1084 | * sync if FSYNC_WAIT is passed in (which |
| 1085 | * is done by everybody but specfs). The |
| 1086 | * sync transaction will also force the log. |
| 1087 | */ |
| 1088 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 1089 | tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS); |
| 1090 | if ((error = xfs_trans_reserve(tp, 0, |
| 1091 | XFS_FSYNC_TS_LOG_RES(ip->i_mount), |
| 1092 | 0, 0, 0))) { |
| 1093 | xfs_trans_cancel(tp, 0); |
| 1094 | return error; |
| 1095 | } |
| 1096 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1097 | |
| 1098 | /* |
| 1099 | * Note - it's possible that we might have pushed |
| 1100 | * ourselves out of the way during trans_reserve |
| 1101 | * which would flush the inode. But there's no |
| 1102 | * guarantee that the inode buffer has actually |
| 1103 | * gone out yet (it's delwri). Plus the buffer |
| 1104 | * could be pinned anyway if it's part of an |
| 1105 | * inode in another recent transaction. So we |
| 1106 | * play it safe and fire off the transaction anyway. |
| 1107 | */ |
| 1108 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 1109 | xfs_trans_ihold(tp, ip); |
| 1110 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1111 | if (flag & FSYNC_WAIT) |
| 1112 | xfs_trans_set_sync(tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1113 | error = _xfs_trans_commit(tp, 0, &log_flushed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
| 1115 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1116 | } |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1117 | |
| 1118 | if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) { |
| 1119 | /* |
| 1120 | * If the log write didn't issue an ordered tag we need |
| 1121 | * to flush the disk cache for the data device now. |
| 1122 | */ |
| 1123 | if (!log_flushed) |
| 1124 | xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp); |
| 1125 | |
| 1126 | /* |
| 1127 | * If this inode is on the RT dev we need to flush that |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1128 | * cache as well. |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1129 | */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 1130 | if (XFS_IS_REALTIME_INODE(ip)) |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1131 | xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp); |
| 1132 | } |
| 1133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | return error; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1138 | * This is called by xfs_inactive to free any blocks beyond eof |
| 1139 | * when the link count isn't zero and by xfs_dm_punch_hole() when |
| 1140 | * punching a hole to EOF. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | */ |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1142 | int |
| 1143 | xfs_free_eofblocks( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | xfs_mount_t *mp, |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1145 | xfs_inode_t *ip, |
| 1146 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | { |
| 1148 | xfs_trans_t *tp; |
| 1149 | int error; |
| 1150 | xfs_fileoff_t end_fsb; |
| 1151 | xfs_fileoff_t last_fsb; |
| 1152 | xfs_filblks_t map_len; |
| 1153 | int nimaps; |
| 1154 | xfs_bmbt_irec_t imap; |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1155 | int use_iolock = (flags & XFS_FREE_EOF_LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
| 1157 | /* |
| 1158 | * Figure out if there are any blocks beyond the end |
| 1159 | * of the file. If not, then there is nothing to do. |
| 1160 | */ |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1161 | end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); |
| 1163 | map_len = last_fsb - end_fsb; |
| 1164 | if (map_len <= 0) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1165 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
| 1167 | nimaps = 1; |
| 1168 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 1169 | error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1170 | NULL, 0, &imap, &nimaps, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 1172 | |
| 1173 | if (!error && (nimaps != 0) && |
Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 1174 | (imap.br_startblock != HOLESTARTBLOCK || |
| 1175 | ip->i_delayed_blks)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | /* |
| 1177 | * Attach the dquots to the inode up front. |
| 1178 | */ |
| 1179 | if ((error = XFS_QM_DQATTACH(mp, ip, 0))) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1180 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | |
| 1182 | /* |
| 1183 | * There are blocks after the end of file. |
| 1184 | * Free them up now by truncating the file to |
| 1185 | * its current size. |
| 1186 | */ |
| 1187 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
| 1188 | |
| 1189 | /* |
| 1190 | * Do the xfs_itruncate_start() call before |
| 1191 | * reserving any log space because |
| 1192 | * itruncate_start will call into the buffer |
| 1193 | * cache and we can't |
| 1194 | * do that within a transaction. |
| 1195 | */ |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1196 | if (use_iolock) |
| 1197 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1198 | error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1199 | ip->i_size); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1200 | if (error) { |
Jesper Juhl | 87ae3c2 | 2007-06-28 16:43:14 +1000 | [diff] [blame] | 1201 | xfs_trans_cancel(tp, 0); |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1202 | if (use_iolock) |
| 1203 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1204 | return error; |
| 1205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | |
| 1207 | error = xfs_trans_reserve(tp, 0, |
| 1208 | XFS_ITRUNCATE_LOG_RES(mp), |
| 1209 | 0, XFS_TRANS_PERM_LOG_RES, |
| 1210 | XFS_ITRUNCATE_LOG_COUNT); |
| 1211 | if (error) { |
| 1212 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1213 | xfs_trans_cancel(tp, 0); |
| 1214 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1215 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1219 | xfs_trans_ijoin(tp, ip, |
| 1220 | XFS_IOLOCK_EXCL | |
| 1221 | XFS_ILOCK_EXCL); |
| 1222 | xfs_trans_ihold(tp, ip); |
| 1223 | |
| 1224 | error = xfs_itruncate_finish(&tp, ip, |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1225 | ip->i_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | XFS_DATA_FORK, |
| 1227 | 0); |
| 1228 | /* |
| 1229 | * If we get an error at this point we |
| 1230 | * simply don't bother truncating the file. |
| 1231 | */ |
| 1232 | if (error) { |
| 1233 | xfs_trans_cancel(tp, |
| 1234 | (XFS_TRANS_RELEASE_LOG_RES | |
| 1235 | XFS_TRANS_ABORT)); |
| 1236 | } else { |
| 1237 | error = xfs_trans_commit(tp, |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1238 | XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1240 | xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL) |
| 1241 | : XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1243 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * Free a symlink that has blocks associated with it. |
| 1248 | */ |
| 1249 | STATIC int |
| 1250 | xfs_inactive_symlink_rmt( |
| 1251 | xfs_inode_t *ip, |
| 1252 | xfs_trans_t **tpp) |
| 1253 | { |
| 1254 | xfs_buf_t *bp; |
| 1255 | int committed; |
| 1256 | int done; |
| 1257 | int error; |
| 1258 | xfs_fsblock_t first_block; |
| 1259 | xfs_bmap_free_t free_list; |
| 1260 | int i; |
| 1261 | xfs_mount_t *mp; |
| 1262 | xfs_bmbt_irec_t mval[SYMLINK_MAPS]; |
| 1263 | int nmaps; |
| 1264 | xfs_trans_t *ntp; |
| 1265 | int size; |
| 1266 | xfs_trans_t *tp; |
| 1267 | |
| 1268 | tp = *tpp; |
| 1269 | mp = ip->i_mount; |
| 1270 | ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip)); |
| 1271 | /* |
| 1272 | * We're freeing a symlink that has some |
| 1273 | * blocks allocated to it. Free the |
| 1274 | * blocks here. We know that we've got |
| 1275 | * either 1 or 2 extents and that we can |
| 1276 | * free them all in one bunmapi call. |
| 1277 | */ |
| 1278 | ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2); |
| 1279 | if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 1280 | XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) { |
| 1281 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1282 | xfs_trans_cancel(tp, 0); |
| 1283 | *tpp = NULL; |
| 1284 | return error; |
| 1285 | } |
| 1286 | /* |
| 1287 | * Lock the inode, fix the size, and join it to the transaction. |
| 1288 | * Hold it so in the normal path, we still have it locked for |
| 1289 | * the second transaction. In the error paths we need it |
| 1290 | * held so the cancel won't rele it, see below. |
| 1291 | */ |
| 1292 | xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1293 | size = (int)ip->i_d.di_size; |
| 1294 | ip->i_d.di_size = 0; |
| 1295 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1296 | xfs_trans_ihold(tp, ip); |
| 1297 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1298 | /* |
| 1299 | * Find the block(s) so we can inval and unmap them. |
| 1300 | */ |
| 1301 | done = 0; |
| 1302 | XFS_BMAP_INIT(&free_list, &first_block); |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 1303 | nmaps = ARRAY_SIZE(mval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size), |
| 1305 | XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1306 | &free_list, NULL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | goto error0; |
| 1308 | /* |
| 1309 | * Invalidate the block(s). |
| 1310 | */ |
| 1311 | for (i = 0; i < nmaps; i++) { |
| 1312 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, |
| 1313 | XFS_FSB_TO_DADDR(mp, mval[i].br_startblock), |
| 1314 | XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0); |
| 1315 | xfs_trans_binval(tp, bp); |
| 1316 | } |
| 1317 | /* |
| 1318 | * Unmap the dead block(s) to the free_list. |
| 1319 | */ |
| 1320 | if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1321 | &first_block, &free_list, NULL, &done))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | goto error1; |
| 1323 | ASSERT(done); |
| 1324 | /* |
| 1325 | * Commit the first transaction. This logs the EFI and the inode. |
| 1326 | */ |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1327 | if ((error = xfs_bmap_finish(&tp, &free_list, &committed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | goto error1; |
| 1329 | /* |
| 1330 | * The transaction must have been committed, since there were |
| 1331 | * actually extents freed by xfs_bunmapi. See xfs_bmap_finish. |
| 1332 | * The new tp has the extent freeing and EFDs. |
| 1333 | */ |
| 1334 | ASSERT(committed); |
| 1335 | /* |
| 1336 | * The first xact was committed, so add the inode to the new one. |
| 1337 | * Mark it dirty so it will be logged and moved forward in the log as |
| 1338 | * part of every commit. |
| 1339 | */ |
| 1340 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1341 | xfs_trans_ihold(tp, ip); |
| 1342 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1343 | /* |
| 1344 | * Get a new, empty transaction to return to our caller. |
| 1345 | */ |
| 1346 | ntp = xfs_trans_dup(tp); |
| 1347 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1348 | * Commit the transaction containing extent freeing and EFDs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | * If we get an error on the commit here or on the reserve below, |
| 1350 | * we need to unlock the inode since the new transaction doesn't |
| 1351 | * have the inode attached. |
| 1352 | */ |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1353 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | tp = ntp; |
| 1355 | if (error) { |
| 1356 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1357 | goto error0; |
| 1358 | } |
| 1359 | /* |
| 1360 | * Remove the memory for extent descriptions (just bookkeeping). |
| 1361 | */ |
| 1362 | if (ip->i_df.if_bytes) |
| 1363 | xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK); |
| 1364 | ASSERT(ip->i_df.if_bytes == 0); |
| 1365 | /* |
| 1366 | * Put an itruncate log reservation in the new transaction |
| 1367 | * for our caller. |
| 1368 | */ |
| 1369 | if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, |
| 1370 | XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) { |
| 1371 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1372 | goto error0; |
| 1373 | } |
| 1374 | /* |
| 1375 | * Return with the inode locked but not joined to the transaction. |
| 1376 | */ |
| 1377 | *tpp = tp; |
| 1378 | return 0; |
| 1379 | |
| 1380 | error1: |
| 1381 | xfs_bmap_cancel(&free_list); |
| 1382 | error0: |
| 1383 | /* |
| 1384 | * Have to come here with the inode locked and either |
| 1385 | * (held and in the transaction) or (not in the transaction). |
| 1386 | * If the inode isn't held then cancel would iput it, but |
| 1387 | * that's wrong since this is inactive and the vnode ref |
| 1388 | * count is 0 already. |
| 1389 | * Cancel won't do anything to the inode if held, but it still |
| 1390 | * needs to be locked until the cancel is done, if it was |
| 1391 | * joined to the transaction. |
| 1392 | */ |
| 1393 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 1394 | xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1395 | *tpp = NULL; |
| 1396 | return error; |
| 1397 | |
| 1398 | } |
| 1399 | |
| 1400 | STATIC int |
| 1401 | xfs_inactive_symlink_local( |
| 1402 | xfs_inode_t *ip, |
| 1403 | xfs_trans_t **tpp) |
| 1404 | { |
| 1405 | int error; |
| 1406 | |
| 1407 | ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip)); |
| 1408 | /* |
| 1409 | * We're freeing a symlink which fit into |
| 1410 | * the inode. Just free the memory used |
| 1411 | * to hold the old symlink. |
| 1412 | */ |
| 1413 | error = xfs_trans_reserve(*tpp, 0, |
| 1414 | XFS_ITRUNCATE_LOG_RES(ip->i_mount), |
| 1415 | 0, XFS_TRANS_PERM_LOG_RES, |
| 1416 | XFS_ITRUNCATE_LOG_COUNT); |
| 1417 | |
| 1418 | if (error) { |
| 1419 | xfs_trans_cancel(*tpp, 0); |
| 1420 | *tpp = NULL; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1421 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | } |
| 1423 | xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1424 | |
| 1425 | /* |
| 1426 | * Zero length symlinks _can_ exist. |
| 1427 | */ |
| 1428 | if (ip->i_df.if_bytes > 0) { |
| 1429 | xfs_idata_realloc(ip, |
| 1430 | -(ip->i_df.if_bytes), |
| 1431 | XFS_DATA_FORK); |
| 1432 | ASSERT(ip->i_df.if_bytes == 0); |
| 1433 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1434 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | STATIC int |
| 1438 | xfs_inactive_attrs( |
| 1439 | xfs_inode_t *ip, |
| 1440 | xfs_trans_t **tpp) |
| 1441 | { |
| 1442 | xfs_trans_t *tp; |
| 1443 | int error; |
| 1444 | xfs_mount_t *mp; |
| 1445 | |
| 1446 | ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE)); |
| 1447 | tp = *tpp; |
| 1448 | mp = ip->i_mount; |
| 1449 | ASSERT(ip->i_d.di_forkoff != 0); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1450 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1452 | if (error) |
| 1453 | goto error_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | |
| 1455 | error = xfs_attr_inactive(ip); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1456 | if (error) |
| 1457 | goto error_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
| 1459 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
| 1460 | error = xfs_trans_reserve(tp, 0, |
| 1461 | XFS_IFREE_LOG_RES(mp), |
| 1462 | 0, XFS_TRANS_PERM_LOG_RES, |
| 1463 | XFS_INACTIVE_LOG_COUNT); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1464 | if (error) |
| 1465 | goto error_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | |
| 1467 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1468 | xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1469 | xfs_trans_ihold(tp, ip); |
| 1470 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 1471 | |
| 1472 | ASSERT(ip->i_d.di_anextents == 0); |
| 1473 | |
| 1474 | *tpp = tp; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1475 | return 0; |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1476 | |
| 1477 | error_cancel: |
| 1478 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1479 | xfs_trans_cancel(tp, 0); |
| 1480 | error_unlock: |
| 1481 | *tpp = NULL; |
| 1482 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 1483 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1486 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | xfs_release( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1488 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1490 | bhv_vnode_t *vp = XFS_ITOV(ip); |
| 1491 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | int error; |
| 1493 | |
Nathan Scott | 7d4fb40 | 2006-06-09 15:27:16 +1000 | [diff] [blame] | 1494 | if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
| 1497 | /* If this is a read-only mount, don't do this (would generate I/O) */ |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 1498 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | return 0; |
| 1500 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1501 | if (!XFS_FORCED_SHUTDOWN(mp)) { |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 1502 | int truncated; |
| 1503 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 1504 | /* |
| 1505 | * If we are using filestreams, and we have an unlinked |
| 1506 | * file that we are processing the last close on, then nothing |
| 1507 | * will be able to reopen and write to this file. Purge this |
| 1508 | * inode from the filestreams cache so that it doesn't delay |
| 1509 | * teardown of the inode. |
| 1510 | */ |
| 1511 | if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip)) |
| 1512 | xfs_filestream_deassociate(ip); |
| 1513 | |
Christoph Hellwig | fbf3ce8 | 2007-06-28 16:46:47 +1000 | [diff] [blame] | 1514 | /* |
| 1515 | * If we previously truncated this file and removed old data |
| 1516 | * in the process, we want to initiate "early" writeout on |
| 1517 | * the last close. This is an attempt to combat the notorious |
| 1518 | * NULL files problem which is particularly noticable from a |
| 1519 | * truncate down, buffered (re-)write (delalloc), followed by |
| 1520 | * a crash. What we are effectively doing here is |
| 1521 | * significantly reducing the time window where we'd otherwise |
| 1522 | * be exposed to that problem. |
| 1523 | */ |
Christoph Hellwig | 09262b4 | 2007-08-29 11:44:50 +1000 | [diff] [blame] | 1524 | truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 1525 | if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0) |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1526 | xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE); |
Christoph Hellwig | fbf3ce8 | 2007-06-28 16:46:47 +1000 | [diff] [blame] | 1527 | } |
| 1528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | if (ip->i_d.di_nlink != 0) { |
| 1530 | if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) && |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1531 | ((ip->i_size > 0) || (VN_CACHED(vp) > 0 || |
Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 1532 | ip->i_delayed_blks > 0)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | (ip->i_df.if_flags & XFS_IFEXTENTS)) && |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1534 | (!(ip->i_d.di_flags & |
| 1535 | (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) { |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1536 | error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK); |
| 1537 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1538 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | /* |
| 1546 | * xfs_inactive |
| 1547 | * |
| 1548 | * This is called when the vnode reference count for the vnode |
| 1549 | * goes to zero. If the file has been unlinked, then it must |
| 1550 | * now be truncated. Also, we clear all of the read-ahead state |
| 1551 | * kept for the inode here since the file is now closed. |
| 1552 | */ |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1553 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | xfs_inactive( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1555 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1557 | bhv_vnode_t *vp = XFS_ITOV(ip); |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 1558 | xfs_bmap_free_t free_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | xfs_fsblock_t first_block; |
| 1560 | int committed; |
| 1561 | xfs_trans_t *tp; |
| 1562 | xfs_mount_t *mp; |
| 1563 | int error; |
| 1564 | int truncate; |
| 1565 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1566 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | /* |
| 1569 | * If the inode is already free, then there can be nothing |
| 1570 | * to clean up here. |
| 1571 | */ |
| 1572 | if (ip->i_d.di_mode == 0 || VN_BAD(vp)) { |
| 1573 | ASSERT(ip->i_df.if_real_bytes == 0); |
| 1574 | ASSERT(ip->i_df.if_broot_bytes == 0); |
| 1575 | return VN_INACTIVE_CACHE; |
| 1576 | } |
| 1577 | |
| 1578 | /* |
| 1579 | * Only do a truncate if it's a regular file with |
| 1580 | * some actual space in it. It's OK to look at the |
| 1581 | * inode's fields without the lock because we're the |
| 1582 | * only one with a reference to the inode. |
| 1583 | */ |
| 1584 | truncate = ((ip->i_d.di_nlink == 0) && |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1585 | ((ip->i_d.di_size != 0) || (ip->i_size != 0) || |
| 1586 | (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | ((ip->i_d.di_mode & S_IFMT) == S_IFREG)); |
| 1588 | |
| 1589 | mp = ip->i_mount; |
| 1590 | |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 1591 | if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) |
| 1592 | XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
| 1594 | error = 0; |
| 1595 | |
| 1596 | /* If this is a read-only mount, don't do this (would generate I/O) */ |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 1597 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | goto out; |
| 1599 | |
| 1600 | if (ip->i_d.di_nlink != 0) { |
| 1601 | if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) && |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 1602 | ((ip->i_size > 0) || (VN_CACHED(vp) > 0 || |
Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 1603 | ip->i_delayed_blks > 0)) && |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1604 | (ip->i_df.if_flags & XFS_IFEXTENTS) && |
| 1605 | (!(ip->i_d.di_flags & |
| 1606 | (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) || |
| 1607 | (ip->i_delayed_blks != 0)))) { |
David Chinner | 92dfe8d | 2007-05-24 15:22:19 +1000 | [diff] [blame] | 1608 | error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK); |
| 1609 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1610 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | } |
| 1612 | goto out; |
| 1613 | } |
| 1614 | |
| 1615 | ASSERT(ip->i_d.di_nlink == 0); |
| 1616 | |
| 1617 | if ((error = XFS_QM_DQATTACH(mp, ip, 0))) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1618 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | |
| 1620 | tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); |
| 1621 | if (truncate) { |
| 1622 | /* |
| 1623 | * Do the xfs_itruncate_start() call before |
| 1624 | * reserving any log space because itruncate_start |
| 1625 | * will call into the buffer cache and we can't |
| 1626 | * do that within a transaction. |
| 1627 | */ |
| 1628 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
| 1629 | |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1630 | error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0); |
| 1631 | if (error) { |
Jesper Juhl | 87ae3c2 | 2007-06-28 16:43:14 +1000 | [diff] [blame] | 1632 | xfs_trans_cancel(tp, 0); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 1633 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 1634 | return VN_INACTIVE_CACHE; |
| 1635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | |
| 1637 | error = xfs_trans_reserve(tp, 0, |
| 1638 | XFS_ITRUNCATE_LOG_RES(mp), |
| 1639 | 0, XFS_TRANS_PERM_LOG_RES, |
| 1640 | XFS_ITRUNCATE_LOG_COUNT); |
| 1641 | if (error) { |
| 1642 | /* Don't call itruncate_cleanup */ |
| 1643 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1644 | xfs_trans_cancel(tp, 0); |
| 1645 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1646 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1650 | xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1651 | xfs_trans_ihold(tp, ip); |
| 1652 | |
| 1653 | /* |
| 1654 | * normally, we have to run xfs_itruncate_finish sync. |
| 1655 | * But if filesystem is wsync and we're in the inactive |
| 1656 | * path, then we know that nlink == 0, and that the |
| 1657 | * xaction that made nlink == 0 is permanently committed |
| 1658 | * since xfs_remove runs as a synchronous transaction. |
| 1659 | */ |
| 1660 | error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK, |
| 1661 | (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0)); |
| 1662 | |
| 1663 | if (error) { |
| 1664 | xfs_trans_cancel(tp, |
| 1665 | XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 1666 | xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1667 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
| 1669 | } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) { |
| 1670 | |
| 1671 | /* |
| 1672 | * If we get an error while cleaning up a |
| 1673 | * symlink we bail out. |
| 1674 | */ |
| 1675 | error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ? |
| 1676 | xfs_inactive_symlink_rmt(ip, &tp) : |
| 1677 | xfs_inactive_symlink_local(ip, &tp); |
| 1678 | |
| 1679 | if (error) { |
| 1680 | ASSERT(tp == NULL); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1681 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1685 | xfs_trans_ihold(tp, ip); |
| 1686 | } else { |
| 1687 | error = xfs_trans_reserve(tp, 0, |
| 1688 | XFS_IFREE_LOG_RES(mp), |
| 1689 | 0, XFS_TRANS_PERM_LOG_RES, |
| 1690 | XFS_INACTIVE_LOG_COUNT); |
| 1691 | if (error) { |
| 1692 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1693 | xfs_trans_cancel(tp, 0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1694 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 1698 | xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1699 | xfs_trans_ihold(tp, ip); |
| 1700 | } |
| 1701 | |
| 1702 | /* |
| 1703 | * If there are attributes associated with the file |
| 1704 | * then blow them away now. The code calls a routine |
| 1705 | * that recursively deconstructs the attribute fork. |
| 1706 | * We need to just commit the current transaction |
| 1707 | * because we can't use it for xfs_attr_inactive(). |
| 1708 | */ |
| 1709 | if (ip->i_d.di_anextents > 0) { |
| 1710 | error = xfs_inactive_attrs(ip, &tp); |
| 1711 | /* |
| 1712 | * If we got an error, the transaction is already |
| 1713 | * cancelled, and the inode is unlocked. Just get out. |
| 1714 | */ |
| 1715 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1716 | return VN_INACTIVE_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | } else if (ip->i_afp) { |
| 1718 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 1719 | } |
| 1720 | |
| 1721 | /* |
| 1722 | * Free the inode. |
| 1723 | */ |
| 1724 | XFS_BMAP_INIT(&free_list, &first_block); |
| 1725 | error = xfs_ifree(tp, ip, &free_list); |
| 1726 | if (error) { |
| 1727 | /* |
| 1728 | * If we fail to free the inode, shut down. The cancel |
| 1729 | * might do that, we need to make sure. Otherwise the |
| 1730 | * inode might be lost for a long time or forever. |
| 1731 | */ |
| 1732 | if (!XFS_FORCED_SHUTDOWN(mp)) { |
| 1733 | cmn_err(CE_NOTE, |
| 1734 | "xfs_inactive: xfs_ifree() returned an error = %d on %s", |
| 1735 | error, mp->m_fsname); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 1736 | xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | } |
| 1738 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 1739 | } else { |
| 1740 | /* |
| 1741 | * Credit the quota account(s). The inode is gone. |
| 1742 | */ |
| 1743 | XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1); |
| 1744 | |
| 1745 | /* |
David Chinner | 78e9da7 | 2008-04-10 12:24:17 +1000 | [diff] [blame] | 1746 | * Just ignore errors at this point. There is nothing we can |
| 1747 | * do except to try to keep going. Make sure it's not a silent |
| 1748 | * error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | */ |
David Chinner | 78e9da7 | 2008-04-10 12:24:17 +1000 | [diff] [blame] | 1750 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
| 1751 | if (error) |
| 1752 | xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: " |
| 1753 | "xfs_bmap_finish() returned error %d", error); |
| 1754 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 1755 | if (error) |
| 1756 | xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: " |
| 1757 | "xfs_trans_commit() returned error %d", error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | } |
| 1759 | /* |
| 1760 | * Release the dquots held by inode, if any. |
| 1761 | */ |
| 1762 | XFS_QM_DQDETACH(mp, ip); |
| 1763 | |
| 1764 | xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); |
| 1765 | |
| 1766 | out: |
| 1767 | return VN_INACTIVE_CACHE; |
| 1768 | } |
| 1769 | |
| 1770 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1771 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | xfs_lookup( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1773 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1774 | struct xfs_name *name, |
Christoph Hellwig | ef1f5e7 | 2008-03-06 13:46:25 +1100 | [diff] [blame] | 1775 | xfs_inode_t **ipp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1777 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | xfs_ino_t e_inum; |
| 1779 | int error; |
| 1780 | uint lock_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1782 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | |
| 1784 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
| 1785 | return XFS_ERROR(EIO); |
| 1786 | |
| 1787 | lock_mode = xfs_ilock_map_shared(dp); |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1788 | error = xfs_dir_lookup_int(dp, lock_mode, name, &e_inum, &ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | if (!error) { |
Christoph Hellwig | ef1f5e7 | 2008-03-06 13:46:25 +1100 | [diff] [blame] | 1790 | *ipp = ip; |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1791 | xfs_itrace_ref(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | } |
| 1793 | xfs_iunlock_map_shared(dp, lock_mode); |
| 1794 | return error; |
| 1795 | } |
| 1796 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1797 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | xfs_create( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1799 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1800 | struct xfs_name *name, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 1801 | mode_t mode, |
| 1802 | xfs_dev_t rdev, |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1803 | xfs_inode_t **ipp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | cred_t *credp) |
| 1805 | { |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1806 | xfs_mount_t *mp = dp->i_mount; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1807 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | xfs_trans_t *tp; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1809 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | xfs_bmap_free_t free_list; |
| 1811 | xfs_fsblock_t first_block; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1812 | boolean_t unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | int dm_event_sent = 0; |
| 1814 | uint cancel_flags; |
| 1815 | int committed; |
| 1816 | xfs_prid_t prid; |
| 1817 | struct xfs_dquot *udqp, *gdqp; |
| 1818 | uint resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1820 | ASSERT(!*ipp); |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1821 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 1823 | if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 1825 | dp, DM_RIGHT_NULL, NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1826 | DM_RIGHT_NULL, name->name, NULL, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 1827 | mode, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | |
| 1829 | if (error) |
| 1830 | return error; |
| 1831 | dm_event_sent = 1; |
| 1832 | } |
| 1833 | |
| 1834 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1835 | return XFS_ERROR(EIO); |
| 1836 | |
| 1837 | /* Return through std_return after this point. */ |
| 1838 | |
| 1839 | udqp = gdqp = NULL; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 1840 | if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 1841 | prid = dp->i_d.di_projid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | else |
| 1843 | prid = (xfs_prid_t)dfltprid; |
| 1844 | |
| 1845 | /* |
| 1846 | * Make sure that we have allocated dquot(s) on disk. |
| 1847 | */ |
| 1848 | error = XFS_QM_DQVOPALLOC(mp, dp, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1849 | current_fsuid(credp), current_fsgid(credp), prid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp); |
| 1851 | if (error) |
| 1852 | goto std_return; |
| 1853 | |
| 1854 | ip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | |
| 1856 | tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE); |
| 1857 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1858 | resblks = XFS_CREATE_SPACE_RES(mp, name->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | /* |
| 1860 | * Initially assume that the file does not exist and |
| 1861 | * reserve the resources for that case. If that is not |
| 1862 | * the case we'll drop the one we have and get a more |
| 1863 | * appropriate transaction later. |
| 1864 | */ |
| 1865 | error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0, |
| 1866 | XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT); |
| 1867 | if (error == ENOSPC) { |
| 1868 | resblks = 0; |
| 1869 | error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0, |
| 1870 | XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT); |
| 1871 | } |
| 1872 | if (error) { |
| 1873 | cancel_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | goto error_return; |
| 1875 | } |
| 1876 | |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 1877 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1878 | unlock_dp_on_error = B_TRUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | |
| 1880 | XFS_BMAP_INIT(&free_list, &first_block); |
| 1881 | |
| 1882 | ASSERT(ip == NULL); |
| 1883 | |
| 1884 | /* |
| 1885 | * Reserve disk quota and the inode. |
| 1886 | */ |
| 1887 | error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0); |
| 1888 | if (error) |
| 1889 | goto error_return; |
| 1890 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1891 | error = xfs_dir_canenter(tp, dp, name, resblks); |
| 1892 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | goto error_return; |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 1894 | error = xfs_dir_ialloc(&tp, dp, mode, 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | rdev, credp, prid, resblks > 0, |
| 1896 | &ip, &committed); |
| 1897 | if (error) { |
| 1898 | if (error == ENOSPC) |
| 1899 | goto error_return; |
| 1900 | goto abort_return; |
| 1901 | } |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 1902 | xfs_itrace_ref(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | |
| 1904 | /* |
| 1905 | * At this point, we've gotten a newly allocated inode. |
| 1906 | * It is locked (and joined to the transaction). |
| 1907 | */ |
| 1908 | |
| 1909 | ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE)); |
| 1910 | |
| 1911 | /* |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1912 | * Now we join the directory inode to the transaction. We do not do it |
| 1913 | * earlier because xfs_dir_ialloc might commit the previous transaction |
| 1914 | * (and release all the locks). An error from here on will result in |
| 1915 | * the transaction cancel unlocking dp so don't do it explicitly in the |
| 1916 | * error path. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | */ |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1918 | IHOLD(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1920 | unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1922 | error = xfs_dir_createname(tp, dp, name, ip->i_ino, |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 1923 | &first_block, &free_list, resblks ? |
| 1924 | resblks - XFS_IALLOC_SPACE_RES(mp) : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | if (error) { |
| 1926 | ASSERT(error != ENOSPC); |
| 1927 | goto abort_return; |
| 1928 | } |
| 1929 | xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 1930 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 1931 | |
| 1932 | /* |
| 1933 | * If this is a synchronous mount, make sure that the |
| 1934 | * create transaction goes to disk before returning to |
| 1935 | * the user. |
| 1936 | */ |
| 1937 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 1938 | xfs_trans_set_sync(tp); |
| 1939 | } |
| 1940 | |
| 1941 | dp->i_gen++; |
| 1942 | |
| 1943 | /* |
| 1944 | * Attach the dquot(s) to the inodes and modify them incore. |
| 1945 | * These ids of the inode couldn't have changed since the new |
| 1946 | * inode has been locked ever since it was created. |
| 1947 | */ |
| 1948 | XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp); |
| 1949 | |
| 1950 | /* |
| 1951 | * xfs_trans_commit normally decrements the vnode ref count |
| 1952 | * when it unlocks the inode. Since we want to return the |
| 1953 | * vnode to the caller, we bump the vnode ref count now. |
| 1954 | */ |
| 1955 | IHOLD(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1957 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | if (error) { |
| 1959 | xfs_bmap_cancel(&free_list); |
| 1960 | goto abort_rele; |
| 1961 | } |
| 1962 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 1963 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | if (error) { |
| 1965 | IRELE(ip); |
| 1966 | tp = NULL; |
| 1967 | goto error_return; |
| 1968 | } |
| 1969 | |
| 1970 | XFS_QM_DQRELE(mp, udqp); |
| 1971 | XFS_QM_DQRELE(mp, gdqp); |
| 1972 | |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1973 | *ipp = ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | |
| 1975 | /* Fallthrough to std_return with error = 0 */ |
| 1976 | |
| 1977 | std_return: |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1978 | if ((*ipp || (error != 0 && dm_event_sent != 0)) && |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1979 | DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 1981 | dp, DM_RIGHT_NULL, |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 1982 | *ipp ? ip : NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 1983 | DM_RIGHT_NULL, name->name, NULL, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 1984 | mode, error, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | } |
| 1986 | return error; |
| 1987 | |
| 1988 | abort_return: |
| 1989 | cancel_flags |= XFS_TRANS_ABORT; |
| 1990 | /* FALLTHROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1992 | error_return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | if (tp != NULL) |
| 1994 | xfs_trans_cancel(tp, cancel_flags); |
| 1995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | XFS_QM_DQRELE(mp, udqp); |
| 1997 | XFS_QM_DQRELE(mp, gdqp); |
| 1998 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 1999 | if (unlock_dp_on_error) |
| 2000 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 2001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | goto std_return; |
| 2003 | |
| 2004 | abort_rele: |
| 2005 | /* |
| 2006 | * Wait until after the current transaction is aborted to |
| 2007 | * release the inode. This prevents recursive transactions |
| 2008 | * and deadlocks from xfs_inactive. |
| 2009 | */ |
| 2010 | cancel_flags |= XFS_TRANS_ABORT; |
| 2011 | xfs_trans_cancel(tp, cancel_flags); |
| 2012 | IRELE(ip); |
| 2013 | |
| 2014 | XFS_QM_DQRELE(mp, udqp); |
| 2015 | XFS_QM_DQRELE(mp, gdqp); |
| 2016 | |
| 2017 | goto std_return; |
| 2018 | } |
| 2019 | |
| 2020 | #ifdef DEBUG |
| 2021 | /* |
| 2022 | * Some counters to see if (and how often) we are hitting some deadlock |
| 2023 | * prevention code paths. |
| 2024 | */ |
| 2025 | |
| 2026 | int xfs_rm_locks; |
| 2027 | int xfs_rm_lock_delays; |
| 2028 | int xfs_rm_attempts; |
| 2029 | #endif |
| 2030 | |
| 2031 | /* |
| 2032 | * The following routine will lock the inodes associated with the |
| 2033 | * directory and the named entry in the directory. The locks are |
| 2034 | * acquired in increasing inode number. |
| 2035 | * |
| 2036 | * If the entry is "..", then only the directory is locked. The |
| 2037 | * vnode ref count will still include that from the .. entry in |
| 2038 | * this case. |
| 2039 | * |
| 2040 | * There is a deadlock we need to worry about. If the locked directory is |
| 2041 | * in the AIL, it might be blocking up the log. The next inode we lock |
| 2042 | * could be already locked by another thread waiting for log space (e.g |
| 2043 | * a permanent log reservation with a long running transaction (see |
| 2044 | * xfs_itruncate_finish)). To solve this, we must check if the directory |
| 2045 | * is in the ail and use lock_nowait. If we can't lock, we need to |
| 2046 | * drop the inode lock on the directory and try again. xfs_iunlock will |
| 2047 | * potentially push the tail if we were holding up the log. |
| 2048 | */ |
| 2049 | STATIC int |
| 2050 | xfs_lock_dir_and_entry( |
| 2051 | xfs_inode_t *dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | xfs_inode_t *ip) /* inode of entry 'name' */ |
| 2053 | { |
| 2054 | int attempts; |
| 2055 | xfs_ino_t e_inum; |
| 2056 | xfs_inode_t *ips[2]; |
| 2057 | xfs_log_item_t *lp; |
| 2058 | |
| 2059 | #ifdef DEBUG |
| 2060 | xfs_rm_locks++; |
| 2061 | #endif |
| 2062 | attempts = 0; |
| 2063 | |
| 2064 | again: |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2065 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | |
| 2067 | e_inum = ip->i_ino; |
| 2068 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2069 | xfs_itrace_ref(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
| 2071 | /* |
| 2072 | * We want to lock in increasing inum. Since we've already |
| 2073 | * acquired the lock on the directory, we may need to release |
| 2074 | * if if the inum of the entry turns out to be less. |
| 2075 | */ |
| 2076 | if (e_inum > dp->i_ino) { |
| 2077 | /* |
| 2078 | * We are already in the right order, so just |
| 2079 | * lock on the inode of the entry. |
| 2080 | * We need to use nowait if dp is in the AIL. |
| 2081 | */ |
| 2082 | |
| 2083 | lp = (xfs_log_item_t *)dp->i_itemp; |
| 2084 | if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { |
| 2085 | if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { |
| 2086 | attempts++; |
| 2087 | #ifdef DEBUG |
| 2088 | xfs_rm_attempts++; |
| 2089 | #endif |
| 2090 | |
| 2091 | /* |
| 2092 | * Unlock dp and try again. |
| 2093 | * xfs_iunlock will try to push the tail |
| 2094 | * if the inode is in the AIL. |
| 2095 | */ |
| 2096 | |
| 2097 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 2098 | |
| 2099 | if ((attempts % 5) == 0) { |
| 2100 | delay(1); /* Don't just spin the CPU */ |
| 2101 | #ifdef DEBUG |
| 2102 | xfs_rm_lock_delays++; |
| 2103 | #endif |
| 2104 | } |
| 2105 | goto again; |
| 2106 | } |
| 2107 | } else { |
| 2108 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 2109 | } |
| 2110 | } else if (e_inum < dp->i_ino) { |
| 2111 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 2112 | |
| 2113 | ips[0] = ip; |
| 2114 | ips[1] = dp; |
| 2115 | xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL); |
| 2116 | } |
| 2117 | /* else e_inum == dp->i_ino */ |
| 2118 | /* This can happen if we're asked to lock /x/.. |
| 2119 | * the entry is "..", which is also the parent directory. |
| 2120 | */ |
| 2121 | |
| 2122 | return 0; |
| 2123 | } |
| 2124 | |
| 2125 | #ifdef DEBUG |
| 2126 | int xfs_locked_n; |
| 2127 | int xfs_small_retries; |
| 2128 | int xfs_middle_retries; |
| 2129 | int xfs_lots_retries; |
| 2130 | int xfs_lock_delays; |
| 2131 | #endif |
| 2132 | |
| 2133 | /* |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2134 | * Bump the subclass so xfs_lock_inodes() acquires each lock with |
| 2135 | * a different value |
| 2136 | */ |
| 2137 | static inline int |
| 2138 | xfs_lock_inumorder(int lock_mode, int subclass) |
| 2139 | { |
| 2140 | if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) |
David Chinner | 0f1145c | 2007-06-29 17:26:09 +1000 | [diff] [blame] | 2141 | lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT; |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2142 | if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) |
David Chinner | 0f1145c | 2007-06-29 17:26:09 +1000 | [diff] [blame] | 2143 | lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT; |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2144 | |
| 2145 | return lock_mode; |
| 2146 | } |
| 2147 | |
| 2148 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | * The following routine will lock n inodes in exclusive mode. |
| 2150 | * We assume the caller calls us with the inodes in i_ino order. |
| 2151 | * |
| 2152 | * We need to detect deadlock where an inode that we lock |
| 2153 | * is in the AIL and we start waiting for another inode that is locked |
| 2154 | * by a thread in a long running transaction (such as truncate). This can |
| 2155 | * result in deadlock since the long running trans might need to wait |
| 2156 | * for the inode we just locked in order to push the tail and free space |
| 2157 | * in the log. |
| 2158 | */ |
| 2159 | void |
| 2160 | xfs_lock_inodes( |
| 2161 | xfs_inode_t **ips, |
| 2162 | int inodes, |
| 2163 | int first_locked, |
| 2164 | uint lock_mode) |
| 2165 | { |
| 2166 | int attempts = 0, i, j, try_lock; |
| 2167 | xfs_log_item_t *lp; |
| 2168 | |
| 2169 | ASSERT(ips && (inodes >= 2)); /* we need at least two */ |
| 2170 | |
| 2171 | if (first_locked) { |
| 2172 | try_lock = 1; |
| 2173 | i = 1; |
| 2174 | } else { |
| 2175 | try_lock = 0; |
| 2176 | i = 0; |
| 2177 | } |
| 2178 | |
| 2179 | again: |
| 2180 | for (; i < inodes; i++) { |
| 2181 | ASSERT(ips[i]); |
| 2182 | |
| 2183 | if (i && (ips[i] == ips[i-1])) /* Already locked */ |
| 2184 | continue; |
| 2185 | |
| 2186 | /* |
| 2187 | * If try_lock is not set yet, make sure all locked inodes |
| 2188 | * are not in the AIL. |
| 2189 | * If any are, set try_lock to be used later. |
| 2190 | */ |
| 2191 | |
| 2192 | if (!try_lock) { |
| 2193 | for (j = (i - 1); j >= 0 && !try_lock; j--) { |
| 2194 | lp = (xfs_log_item_t *)ips[j]->i_itemp; |
| 2195 | if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { |
| 2196 | try_lock++; |
| 2197 | } |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | /* |
| 2202 | * If any of the previous locks we have locked is in the AIL, |
| 2203 | * we must TRY to get the second and subsequent locks. If |
| 2204 | * we can't get any, we must release all we have |
| 2205 | * and try again. |
| 2206 | */ |
| 2207 | |
| 2208 | if (try_lock) { |
| 2209 | /* try_lock must be 0 if i is 0. */ |
| 2210 | /* |
| 2211 | * try_lock means we have an inode locked |
| 2212 | * that is in the AIL. |
| 2213 | */ |
| 2214 | ASSERT(i != 0); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2215 | if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | attempts++; |
| 2217 | |
| 2218 | /* |
| 2219 | * Unlock all previous guys and try again. |
| 2220 | * xfs_iunlock will try to push the tail |
| 2221 | * if the inode is in the AIL. |
| 2222 | */ |
| 2223 | |
| 2224 | for(j = i - 1; j >= 0; j--) { |
| 2225 | |
| 2226 | /* |
| 2227 | * Check to see if we've already |
| 2228 | * unlocked this one. |
| 2229 | * Not the first one going back, |
| 2230 | * and the inode ptr is the same. |
| 2231 | */ |
| 2232 | if ((j != (i - 1)) && ips[j] == |
| 2233 | ips[j+1]) |
| 2234 | continue; |
| 2235 | |
| 2236 | xfs_iunlock(ips[j], lock_mode); |
| 2237 | } |
| 2238 | |
| 2239 | if ((attempts % 5) == 0) { |
| 2240 | delay(1); /* Don't just spin the CPU */ |
| 2241 | #ifdef DEBUG |
| 2242 | xfs_lock_delays++; |
| 2243 | #endif |
| 2244 | } |
| 2245 | i = 0; |
| 2246 | try_lock = 0; |
| 2247 | goto again; |
| 2248 | } |
| 2249 | } else { |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2250 | xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | #ifdef DEBUG |
| 2255 | if (attempts) { |
| 2256 | if (attempts < 5) xfs_small_retries++; |
| 2257 | else if (attempts < 100) xfs_middle_retries++; |
| 2258 | else xfs_lots_retries++; |
| 2259 | } else { |
| 2260 | xfs_locked_n++; |
| 2261 | } |
| 2262 | #endif |
| 2263 | } |
| 2264 | |
| 2265 | #ifdef DEBUG |
| 2266 | #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);} |
| 2267 | int remove_which_error_return = 0; |
| 2268 | #else /* ! DEBUG */ |
| 2269 | #define REMOVE_DEBUG_TRACE(x) |
| 2270 | #endif /* ! DEBUG */ |
| 2271 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2272 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | xfs_remove( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2274 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2275 | struct xfs_name *name, |
| 2276 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2278 | xfs_mount_t *mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | xfs_trans_t *tp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | int error = 0; |
| 2281 | xfs_bmap_free_t free_list; |
| 2282 | xfs_fsblock_t first_block; |
| 2283 | int cancel_flags; |
| 2284 | int committed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | int link_zero; |
| 2286 | uint resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2288 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 2291 | return XFS_ERROR(EIO); |
| 2292 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2293 | if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) { |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2294 | error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL, |
| 2295 | NULL, DM_RIGHT_NULL, name->name, NULL, |
| 2296 | ip->i_d.di_mode, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | if (error) |
| 2298 | return error; |
| 2299 | } |
| 2300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | /* |
| 2302 | * We need to get a reference to ip before we get our log |
| 2303 | * reservation. The reason for this is that we cannot call |
| 2304 | * xfs_iget for an inode for which we do not have a reference |
| 2305 | * once we've acquired a log reservation. This is because the |
| 2306 | * inode we are trying to get might be in xfs_inactive going |
| 2307 | * for a log reservation. Since we'll have to wait for the |
| 2308 | * inactive code to complete before returning from xfs_iget, |
| 2309 | * we need to make sure that we don't have log space reserved |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 2310 | * when we call xfs_iget. Instead we get an unlocked reference |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | * to the inode before getting our log reservation. |
| 2312 | */ |
Christoph Hellwig | 4397396 | 2008-03-06 13:44:50 +1100 | [diff] [blame] | 2313 | IHOLD(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2315 | xfs_itrace_entry(ip); |
| 2316 | xfs_itrace_ref(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
| 2318 | error = XFS_QM_DQATTACH(mp, dp, 0); |
| 2319 | if (!error && dp != ip) |
| 2320 | error = XFS_QM_DQATTACH(mp, ip, 0); |
| 2321 | if (error) { |
| 2322 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2323 | IRELE(ip); |
| 2324 | goto std_return; |
| 2325 | } |
| 2326 | |
| 2327 | tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE); |
| 2328 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 2329 | /* |
| 2330 | * We try to get the real space reservation first, |
| 2331 | * allowing for directory btree deletion(s) implying |
| 2332 | * possible bmap insert(s). If we can't get the space |
| 2333 | * reservation then we use 0 instead, and avoid the bmap |
| 2334 | * btree insert(s) in the directory code by, if the bmap |
| 2335 | * insert tries to happen, instead trimming the LAST |
| 2336 | * block from the directory. |
| 2337 | */ |
| 2338 | resblks = XFS_REMOVE_SPACE_RES(mp); |
| 2339 | error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0, |
| 2340 | XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT); |
| 2341 | if (error == ENOSPC) { |
| 2342 | resblks = 0; |
| 2343 | error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0, |
| 2344 | XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT); |
| 2345 | } |
| 2346 | if (error) { |
| 2347 | ASSERT(error != ENOSPC); |
| 2348 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2349 | xfs_trans_cancel(tp, 0); |
| 2350 | IRELE(ip); |
| 2351 | return error; |
| 2352 | } |
| 2353 | |
Eric Sandeen | e9ed9d2 | 2007-05-08 13:48:56 +1000 | [diff] [blame] | 2354 | error = xfs_lock_dir_and_entry(dp, ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | if (error) { |
| 2356 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2357 | xfs_trans_cancel(tp, cancel_flags); |
| 2358 | IRELE(ip); |
| 2359 | goto std_return; |
| 2360 | } |
| 2361 | |
| 2362 | /* |
| 2363 | * At this point, we've gotten both the directory and the entry |
| 2364 | * inodes locked. |
| 2365 | */ |
| 2366 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
| 2367 | if (dp != ip) { |
| 2368 | /* |
| 2369 | * Increment vnode ref count only in this case since |
| 2370 | * there's an extra vnode reference in the case where |
| 2371 | * dp == ip. |
| 2372 | */ |
| 2373 | IHOLD(dp); |
| 2374 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 2375 | } |
| 2376 | |
| 2377 | /* |
| 2378 | * Entry must exist since we did a lookup in xfs_lock_dir_and_entry. |
| 2379 | */ |
| 2380 | XFS_BMAP_INIT(&free_list, &first_block); |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2381 | error = xfs_dir_removename(tp, dp, name, ip->i_ino, |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 2382 | &first_block, &free_list, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | if (error) { |
| 2384 | ASSERT(error != ENOENT); |
| 2385 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2386 | goto error1; |
| 2387 | } |
| 2388 | xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2389 | |
| 2390 | dp->i_gen++; |
| 2391 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 2392 | |
| 2393 | error = xfs_droplink(tp, ip); |
| 2394 | if (error) { |
| 2395 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2396 | goto error1; |
| 2397 | } |
| 2398 | |
| 2399 | /* Determine if this is the last link while |
| 2400 | * we are in the transaction. |
| 2401 | */ |
| 2402 | link_zero = (ip)->i_d.di_nlink==0; |
| 2403 | |
| 2404 | /* |
| 2405 | * Take an extra ref on the inode so that it doesn't |
| 2406 | * go to xfs_inactive() from within the commit. |
| 2407 | */ |
| 2408 | IHOLD(ip); |
| 2409 | |
| 2410 | /* |
| 2411 | * If this is a synchronous mount, make sure that the |
| 2412 | * remove transaction goes to disk before returning to |
| 2413 | * the user. |
| 2414 | */ |
| 2415 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 2416 | xfs_trans_set_sync(tp); |
| 2417 | } |
| 2418 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2419 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | if (error) { |
| 2421 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2422 | goto error_rele; |
| 2423 | } |
| 2424 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2425 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | if (error) { |
| 2427 | IRELE(ip); |
| 2428 | goto std_return; |
| 2429 | } |
| 2430 | |
| 2431 | /* |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2432 | * If we are using filestreams, kill the stream association. |
| 2433 | * If the file is still open it may get a new one but that |
| 2434 | * will get killed on last close in xfs_close() so we don't |
| 2435 | * have to worry about that. |
| 2436 | */ |
| 2437 | if (link_zero && xfs_inode_is_filestream(ip)) |
| 2438 | xfs_filestream_deassociate(ip); |
| 2439 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2440 | xfs_itrace_exit(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | IRELE(ip); |
| 2442 | |
| 2443 | /* Fall through to std_return with error = 0 */ |
| 2444 | std_return: |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2445 | if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2447 | dp, DM_RIGHT_NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | NULL, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2449 | name->name, NULL, ip->i_d.di_mode, error, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | } |
| 2451 | return error; |
| 2452 | |
| 2453 | error1: |
| 2454 | xfs_bmap_cancel(&free_list); |
| 2455 | cancel_flags |= XFS_TRANS_ABORT; |
| 2456 | xfs_trans_cancel(tp, cancel_flags); |
| 2457 | goto std_return; |
| 2458 | |
| 2459 | error_rele: |
| 2460 | /* |
| 2461 | * In this case make sure to not release the inode until after |
| 2462 | * the current transaction is aborted. Releasing it beforehand |
| 2463 | * can cause us to go to xfs_inactive and start a recursive |
| 2464 | * transaction which can easily deadlock with the current one. |
| 2465 | */ |
| 2466 | xfs_bmap_cancel(&free_list); |
| 2467 | cancel_flags |= XFS_TRANS_ABORT; |
| 2468 | xfs_trans_cancel(tp, cancel_flags); |
| 2469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | IRELE(ip); |
| 2471 | |
| 2472 | goto std_return; |
| 2473 | } |
| 2474 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2475 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | xfs_link( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2477 | xfs_inode_t *tdp, |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2478 | xfs_inode_t *sip, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2479 | struct xfs_name *target_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2481 | xfs_mount_t *mp = tdp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | xfs_inode_t *ips[2]; |
| 2484 | int error; |
| 2485 | xfs_bmap_free_t free_list; |
| 2486 | xfs_fsblock_t first_block; |
| 2487 | int cancel_flags; |
| 2488 | int committed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | int resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2491 | xfs_itrace_entry(tdp); |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2492 | xfs_itrace_entry(sip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2494 | ASSERT(!S_ISDIR(sip->i_d.di_mode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 2497 | return XFS_ERROR(EIO); |
| 2498 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2499 | if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2501 | tdp, DM_RIGHT_NULL, |
| 2502 | sip, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2503 | target_name->name, NULL, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | if (error) |
| 2505 | return error; |
| 2506 | } |
| 2507 | |
| 2508 | /* Return through std_return after this point. */ |
| 2509 | |
| 2510 | error = XFS_QM_DQATTACH(mp, sip, 0); |
| 2511 | if (!error && sip != tdp) |
| 2512 | error = XFS_QM_DQATTACH(mp, tdp, 0); |
| 2513 | if (error) |
| 2514 | goto std_return; |
| 2515 | |
| 2516 | tp = xfs_trans_alloc(mp, XFS_TRANS_LINK); |
| 2517 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2518 | resblks = XFS_LINK_SPACE_RES(mp, target_name->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0, |
| 2520 | XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT); |
| 2521 | if (error == ENOSPC) { |
| 2522 | resblks = 0; |
| 2523 | error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0, |
| 2524 | XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT); |
| 2525 | } |
| 2526 | if (error) { |
| 2527 | cancel_flags = 0; |
| 2528 | goto error_return; |
| 2529 | } |
| 2530 | |
| 2531 | if (sip->i_ino < tdp->i_ino) { |
| 2532 | ips[0] = sip; |
| 2533 | ips[1] = tdp; |
| 2534 | } else { |
| 2535 | ips[0] = tdp; |
| 2536 | ips[1] = sip; |
| 2537 | } |
| 2538 | |
| 2539 | xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL); |
| 2540 | |
| 2541 | /* |
| 2542 | * Increment vnode ref counts since xfs_trans_commit & |
| 2543 | * xfs_trans_cancel will both unlock the inodes and |
| 2544 | * decrement the associated ref counts. |
| 2545 | */ |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 2546 | IHOLD(sip); |
| 2547 | IHOLD(tdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL); |
| 2549 | xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL); |
| 2550 | |
| 2551 | /* |
| 2552 | * If the source has too many links, we can't make any more to it. |
| 2553 | */ |
| 2554 | if (sip->i_d.di_nlink >= XFS_MAXLINK) { |
| 2555 | error = XFS_ERROR(EMLINK); |
| 2556 | goto error_return; |
| 2557 | } |
| 2558 | |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 2559 | /* |
| 2560 | * If we are using project inheritance, we only allow hard link |
| 2561 | * creation in our tree when the project IDs are the same; else |
| 2562 | * the tree quota mechanism could be circumvented. |
| 2563 | */ |
| 2564 | if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && |
| 2565 | (tdp->i_d.di_projid != sip->i_d.di_projid))) { |
Nathan Scott | b1ecdda | 2006-05-08 19:51:42 +1000 | [diff] [blame] | 2566 | error = XFS_ERROR(EXDEV); |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 2567 | goto error_return; |
| 2568 | } |
| 2569 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2570 | error = xfs_dir_canenter(tp, tdp, target_name, resblks); |
| 2571 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | goto error_return; |
| 2573 | |
| 2574 | XFS_BMAP_INIT(&free_list, &first_block); |
| 2575 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2576 | error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino, |
| 2577 | &first_block, &free_list, resblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | if (error) |
| 2579 | goto abort_return; |
| 2580 | xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2581 | tdp->i_gen++; |
| 2582 | xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE); |
| 2583 | |
| 2584 | error = xfs_bumplink(tp, sip); |
Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 2585 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | goto abort_return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | |
| 2588 | /* |
| 2589 | * If this is a synchronous mount, make sure that the |
| 2590 | * link transaction goes to disk before returning to |
| 2591 | * the user. |
| 2592 | */ |
| 2593 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 2594 | xfs_trans_set_sync(tp); |
| 2595 | } |
| 2596 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2597 | error = xfs_bmap_finish (&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | if (error) { |
| 2599 | xfs_bmap_cancel(&free_list); |
| 2600 | goto abort_return; |
| 2601 | } |
| 2602 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2603 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 2604 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | goto std_return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | |
| 2607 | /* Fall through to std_return with error = 0. */ |
| 2608 | std_return: |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2609 | if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2611 | tdp, DM_RIGHT_NULL, |
| 2612 | sip, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2613 | target_name->name, NULL, 0, error, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | } |
| 2615 | return error; |
| 2616 | |
| 2617 | abort_return: |
| 2618 | cancel_flags |= XFS_TRANS_ABORT; |
| 2619 | /* FALLTHROUGH */ |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | error_return: |
| 2622 | xfs_trans_cancel(tp, cancel_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | goto std_return; |
| 2624 | } |
Alexey Dobriyan | b71d300 | 2006-06-27 12:45:17 +1000 | [diff] [blame] | 2625 | |
| 2626 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2627 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | xfs_mkdir( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2629 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2630 | struct xfs_name *dir_name, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2631 | mode_t mode, |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 2632 | xfs_inode_t **ipp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | cred_t *credp) |
| 2634 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2635 | xfs_mount_t *mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | xfs_inode_t *cdp; /* inode of created dir */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | int cancel_flags; |
| 2639 | int error; |
| 2640 | int committed; |
| 2641 | xfs_bmap_free_t free_list; |
| 2642 | xfs_fsblock_t first_block; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2643 | boolean_t unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | boolean_t created = B_FALSE; |
| 2645 | int dm_event_sent = 0; |
| 2646 | xfs_prid_t prid; |
| 2647 | struct xfs_dquot *udqp, *gdqp; |
| 2648 | uint resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | |
| 2650 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 2651 | return XFS_ERROR(EIO); |
| 2652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | tp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2655 | if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2657 | dp, DM_RIGHT_NULL, NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2658 | DM_RIGHT_NULL, dir_name->name, NULL, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2659 | mode, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | if (error) |
| 2661 | return error; |
| 2662 | dm_event_sent = 1; |
| 2663 | } |
| 2664 | |
| 2665 | /* Return through std_return after this point. */ |
| 2666 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2667 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | |
| 2669 | mp = dp->i_mount; |
| 2670 | udqp = gdqp = NULL; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 2671 | if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 2672 | prid = dp->i_d.di_projid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | else |
| 2674 | prid = (xfs_prid_t)dfltprid; |
| 2675 | |
| 2676 | /* |
| 2677 | * Make sure that we have allocated dquot(s) on disk. |
| 2678 | */ |
| 2679 | error = XFS_QM_DQVOPALLOC(mp, dp, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 2680 | current_fsuid(credp), current_fsgid(credp), prid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp); |
| 2682 | if (error) |
| 2683 | goto std_return; |
| 2684 | |
| 2685 | tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR); |
| 2686 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2687 | resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0, |
| 2689 | XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT); |
| 2690 | if (error == ENOSPC) { |
| 2691 | resblks = 0; |
| 2692 | error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0, |
| 2693 | XFS_TRANS_PERM_LOG_RES, |
| 2694 | XFS_MKDIR_LOG_COUNT); |
| 2695 | } |
| 2696 | if (error) { |
| 2697 | cancel_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | goto error_return; |
| 2699 | } |
| 2700 | |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 2701 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2702 | unlock_dp_on_error = B_TRUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | |
| 2704 | /* |
| 2705 | * Check for directory link count overflow. |
| 2706 | */ |
| 2707 | if (dp->i_d.di_nlink >= XFS_MAXLINK) { |
| 2708 | error = XFS_ERROR(EMLINK); |
| 2709 | goto error_return; |
| 2710 | } |
| 2711 | |
| 2712 | /* |
| 2713 | * Reserve disk quota and the inode. |
| 2714 | */ |
| 2715 | error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0); |
| 2716 | if (error) |
| 2717 | goto error_return; |
| 2718 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2719 | error = xfs_dir_canenter(tp, dp, dir_name, resblks); |
| 2720 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | goto error_return; |
| 2722 | /* |
| 2723 | * create the directory inode. |
| 2724 | */ |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2725 | error = xfs_dir_ialloc(&tp, dp, mode, 2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | 0, credp, prid, resblks > 0, |
| 2727 | &cdp, NULL); |
| 2728 | if (error) { |
| 2729 | if (error == ENOSPC) |
| 2730 | goto error_return; |
| 2731 | goto abort_return; |
| 2732 | } |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2733 | xfs_itrace_ref(cdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | |
| 2735 | /* |
| 2736 | * Now we add the directory inode to the transaction. |
| 2737 | * We waited until now since xfs_dir_ialloc might start |
| 2738 | * a new transaction. Had we joined the transaction |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2739 | * earlier, the locks might have gotten released. An error |
| 2740 | * from here on will result in the transaction cancel |
| 2741 | * unlocking dp so don't do it explicitly in the error path. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | */ |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 2743 | IHOLD(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2745 | unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | |
| 2747 | XFS_BMAP_INIT(&free_list, &first_block); |
| 2748 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2749 | error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino, |
| 2750 | &first_block, &free_list, resblks ? |
| 2751 | resblks - XFS_IALLOC_SPACE_RES(mp) : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | if (error) { |
| 2753 | ASSERT(error != ENOSPC); |
| 2754 | goto error1; |
| 2755 | } |
| 2756 | xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2757 | |
| 2758 | /* |
| 2759 | * Bump the in memory version number of the parent directory |
| 2760 | * so that other processes accessing it will recognize that |
| 2761 | * the directory has changed. |
| 2762 | */ |
| 2763 | dp->i_gen++; |
| 2764 | |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 2765 | error = xfs_dir_init(tp, cdp, dp); |
| 2766 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | goto error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | |
| 2769 | cdp->i_gen = 1; |
| 2770 | error = xfs_bumplink(tp, dp); |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 2771 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | goto error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | created = B_TRUE; |
| 2775 | |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 2776 | *ipp = cdp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | IHOLD(cdp); |
| 2778 | |
| 2779 | /* |
| 2780 | * Attach the dquots to the new inode and modify the icount incore. |
| 2781 | */ |
| 2782 | XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp); |
| 2783 | |
| 2784 | /* |
| 2785 | * If this is a synchronous mount, make sure that the |
| 2786 | * mkdir transaction goes to disk before returning to |
| 2787 | * the user. |
| 2788 | */ |
| 2789 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 2790 | xfs_trans_set_sync(tp); |
| 2791 | } |
| 2792 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 2793 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | if (error) { |
| 2795 | IRELE(cdp); |
| 2796 | goto error2; |
| 2797 | } |
| 2798 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 2799 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | XFS_QM_DQRELE(mp, udqp); |
| 2801 | XFS_QM_DQRELE(mp, gdqp); |
| 2802 | if (error) { |
| 2803 | IRELE(cdp); |
| 2804 | } |
| 2805 | |
| 2806 | /* Fall through to std_return with error = 0 or errno from |
| 2807 | * xfs_trans_commit. */ |
| 2808 | |
| 2809 | std_return: |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2810 | if ((created || (error != 0 && dm_event_sent != 0)) && |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2811 | DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2813 | dp, DM_RIGHT_NULL, |
| 2814 | created ? cdp : NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2816 | dir_name->name, NULL, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 2817 | mode, error, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | } |
| 2819 | return error; |
| 2820 | |
| 2821 | error2: |
| 2822 | error1: |
| 2823 | xfs_bmap_cancel(&free_list); |
| 2824 | abort_return: |
| 2825 | cancel_flags |= XFS_TRANS_ABORT; |
| 2826 | error_return: |
| 2827 | xfs_trans_cancel(tp, cancel_flags); |
| 2828 | XFS_QM_DQRELE(mp, udqp); |
| 2829 | XFS_QM_DQRELE(mp, gdqp); |
| 2830 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2831 | if (unlock_dp_on_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | |
| 2834 | goto std_return; |
| 2835 | } |
| 2836 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2837 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | xfs_rmdir( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2839 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2840 | struct xfs_name *name, |
| 2841 | xfs_inode_t *cdp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2843 | bhv_vnode_t *dir_vp = XFS_ITOV(dp); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2844 | xfs_mount_t *mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | int error; |
| 2847 | xfs_bmap_free_t free_list; |
| 2848 | xfs_fsblock_t first_block; |
| 2849 | int cancel_flags; |
| 2850 | int committed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | int last_cdp_link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | uint resblks; |
| 2853 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2854 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 2856 | if (XFS_FORCED_SHUTDOWN(mp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | return XFS_ERROR(EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 2859 | if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 2861 | dp, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2862 | NULL, DM_RIGHT_NULL, name->name, |
| 2863 | NULL, cdp->i_d.di_mode, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | if (error) |
| 2865 | return XFS_ERROR(error); |
| 2866 | } |
| 2867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | /* |
| 2869 | * We need to get a reference to cdp before we get our log |
| 2870 | * reservation. The reason for this is that we cannot call |
| 2871 | * xfs_iget for an inode for which we do not have a reference |
| 2872 | * once we've acquired a log reservation. This is because the |
| 2873 | * inode we are trying to get might be in xfs_inactive going |
| 2874 | * for a log reservation. Since we'll have to wait for the |
| 2875 | * inactive code to complete before returning from xfs_iget, |
| 2876 | * we need to make sure that we don't have log space reserved |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 2877 | * when we call xfs_iget. Instead we get an unlocked reference |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | * to the inode before getting our log reservation. |
| 2879 | */ |
Christoph Hellwig | 4397396 | 2008-03-06 13:44:50 +1100 | [diff] [blame] | 2880 | IHOLD(cdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | |
| 2882 | /* |
| 2883 | * Get the dquots for the inodes. |
| 2884 | */ |
| 2885 | error = XFS_QM_DQATTACH(mp, dp, 0); |
| 2886 | if (!error && dp != cdp) |
| 2887 | error = XFS_QM_DQATTACH(mp, cdp, 0); |
| 2888 | if (error) { |
| 2889 | IRELE(cdp); |
| 2890 | REMOVE_DEBUG_TRACE(__LINE__); |
| 2891 | goto std_return; |
| 2892 | } |
| 2893 | |
| 2894 | tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR); |
| 2895 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 2896 | /* |
| 2897 | * We try to get the real space reservation first, |
| 2898 | * allowing for directory btree deletion(s) implying |
| 2899 | * possible bmap insert(s). If we can't get the space |
| 2900 | * reservation then we use 0 instead, and avoid the bmap |
| 2901 | * btree insert(s) in the directory code by, if the bmap |
| 2902 | * insert tries to happen, instead trimming the LAST |
| 2903 | * block from the directory. |
| 2904 | */ |
| 2905 | resblks = XFS_REMOVE_SPACE_RES(mp); |
| 2906 | error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0, |
| 2907 | XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT); |
| 2908 | if (error == ENOSPC) { |
| 2909 | resblks = 0; |
| 2910 | error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0, |
| 2911 | XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT); |
| 2912 | } |
| 2913 | if (error) { |
| 2914 | ASSERT(error != ENOSPC); |
| 2915 | cancel_flags = 0; |
| 2916 | IRELE(cdp); |
| 2917 | goto error_return; |
| 2918 | } |
| 2919 | XFS_BMAP_INIT(&free_list, &first_block); |
| 2920 | |
| 2921 | /* |
| 2922 | * Now lock the child directory inode and the parent directory |
| 2923 | * inode in the proper order. This will take care of validating |
| 2924 | * that the directory entry for the child directory inode has |
| 2925 | * not changed while we were obtaining a log reservation. |
| 2926 | */ |
Eric Sandeen | e9ed9d2 | 2007-05-08 13:48:56 +1000 | [diff] [blame] | 2927 | error = xfs_lock_dir_and_entry(dp, cdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | if (error) { |
| 2929 | xfs_trans_cancel(tp, cancel_flags); |
| 2930 | IRELE(cdp); |
| 2931 | goto std_return; |
| 2932 | } |
| 2933 | |
| 2934 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
| 2935 | if (dp != cdp) { |
| 2936 | /* |
| 2937 | * Only increment the parent directory vnode count if |
| 2938 | * we didn't bump it in looking up cdp. The only time |
| 2939 | * we don't bump it is when we're looking up ".". |
| 2940 | */ |
| 2941 | VN_HOLD(dir_vp); |
| 2942 | } |
| 2943 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 2944 | xfs_itrace_ref(cdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL); |
| 2946 | |
| 2947 | ASSERT(cdp->i_d.di_nlink >= 2); |
| 2948 | if (cdp->i_d.di_nlink != 2) { |
| 2949 | error = XFS_ERROR(ENOTEMPTY); |
| 2950 | goto error_return; |
| 2951 | } |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 2952 | if (!xfs_dir_isempty(cdp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | error = XFS_ERROR(ENOTEMPTY); |
| 2954 | goto error_return; |
| 2955 | } |
| 2956 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 2957 | error = xfs_dir_removename(tp, dp, name, cdp->i_ino, |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 2958 | &first_block, &free_list, resblks); |
| 2959 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | goto error1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | |
| 2962 | xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 2963 | |
| 2964 | /* |
| 2965 | * Bump the in memory generation count on the parent |
| 2966 | * directory so that other can know that it has changed. |
| 2967 | */ |
| 2968 | dp->i_gen++; |
| 2969 | |
| 2970 | /* |
| 2971 | * Drop the link from cdp's "..". |
| 2972 | */ |
| 2973 | error = xfs_droplink(tp, dp); |
| 2974 | if (error) { |
| 2975 | goto error1; |
| 2976 | } |
| 2977 | |
| 2978 | /* |
| 2979 | * Drop the link from dp to cdp. |
| 2980 | */ |
| 2981 | error = xfs_droplink(tp, cdp); |
| 2982 | if (error) { |
| 2983 | goto error1; |
| 2984 | } |
| 2985 | |
| 2986 | /* |
| 2987 | * Drop the "." link from cdp to self. |
| 2988 | */ |
| 2989 | error = xfs_droplink(tp, cdp); |
| 2990 | if (error) { |
| 2991 | goto error1; |
| 2992 | } |
| 2993 | |
| 2994 | /* Determine these before committing transaction */ |
| 2995 | last_cdp_link = (cdp)->i_d.di_nlink==0; |
| 2996 | |
| 2997 | /* |
| 2998 | * Take an extra ref on the child vnode so that it |
| 2999 | * does not go to xfs_inactive() from within the commit. |
| 3000 | */ |
| 3001 | IHOLD(cdp); |
| 3002 | |
| 3003 | /* |
| 3004 | * If this is a synchronous mount, make sure that the |
| 3005 | * rmdir transaction goes to disk before returning to |
| 3006 | * the user. |
| 3007 | */ |
| 3008 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 3009 | xfs_trans_set_sync(tp); |
| 3010 | } |
| 3011 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3012 | error = xfs_bmap_finish (&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | if (error) { |
| 3014 | xfs_bmap_cancel(&free_list); |
| 3015 | xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | |
| 3016 | XFS_TRANS_ABORT)); |
| 3017 | IRELE(cdp); |
| 3018 | goto std_return; |
| 3019 | } |
| 3020 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3021 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | if (error) { |
| 3023 | IRELE(cdp); |
| 3024 | goto std_return; |
| 3025 | } |
| 3026 | |
| 3027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | IRELE(cdp); |
| 3029 | |
| 3030 | /* Fall through to std_return with error = 0 or the errno |
| 3031 | * from xfs_trans_commit. */ |
Nathan Scott | bb19fba | 2006-03-22 14:12:12 +1100 | [diff] [blame] | 3032 | std_return: |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 3033 | if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 3035 | dp, DM_RIGHT_NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | NULL, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 3037 | name->name, NULL, cdp->i_d.di_mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | error, 0); |
| 3039 | } |
| 3040 | return error; |
| 3041 | |
Nathan Scott | bb19fba | 2006-03-22 14:12:12 +1100 | [diff] [blame] | 3042 | error1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | xfs_bmap_cancel(&free_list); |
| 3044 | cancel_flags |= XFS_TRANS_ABORT; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3045 | /* FALLTHROUGH */ |
| 3046 | |
Nathan Scott | bb19fba | 2006-03-22 14:12:12 +1100 | [diff] [blame] | 3047 | error_return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | xfs_trans_cancel(tp, cancel_flags); |
| 3049 | goto std_return; |
| 3050 | } |
| 3051 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3052 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | xfs_symlink( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3054 | xfs_inode_t *dp, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 3055 | struct xfs_name *link_name, |
| 3056 | const char *target_path, |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 3057 | mode_t mode, |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 3058 | xfs_inode_t **ipp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | cred_t *credp) |
| 3060 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3061 | xfs_mount_t *mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | xfs_inode_t *ip; |
| 3064 | int error; |
| 3065 | int pathlen; |
| 3066 | xfs_bmap_free_t free_list; |
| 3067 | xfs_fsblock_t first_block; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3068 | boolean_t unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | uint cancel_flags; |
| 3070 | int committed; |
| 3071 | xfs_fileoff_t first_fsb; |
| 3072 | xfs_filblks_t fs_blocks; |
| 3073 | int nmaps; |
| 3074 | xfs_bmbt_irec_t mval[SYMLINK_MAPS]; |
| 3075 | xfs_daddr_t d; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 3076 | const char *cur_chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | int byte_cnt; |
| 3078 | int n; |
| 3079 | xfs_buf_t *bp; |
| 3080 | xfs_prid_t prid; |
| 3081 | struct xfs_dquot *udqp, *gdqp; |
| 3082 | uint resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 3084 | *ipp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | error = 0; |
| 3086 | ip = NULL; |
| 3087 | tp = NULL; |
| 3088 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 3089 | xfs_itrace_entry(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | |
| 3091 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 3092 | return XFS_ERROR(EIO); |
| 3093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | /* |
| 3095 | * Check component lengths of the target path name. |
| 3096 | */ |
| 3097 | pathlen = strlen(target_path); |
| 3098 | if (pathlen >= MAXPATHLEN) /* total string too long */ |
| 3099 | return XFS_ERROR(ENAMETOOLONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 3101 | if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) { |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 3102 | error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | DM_RIGHT_NULL, NULL, DM_RIGHT_NULL, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 3104 | link_name->name, target_path, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | if (error) |
| 3106 | return error; |
| 3107 | } |
| 3108 | |
| 3109 | /* Return through std_return after this point. */ |
| 3110 | |
| 3111 | udqp = gdqp = NULL; |
Nathan Scott | 365ca83 | 2005-06-21 15:39:12 +1000 | [diff] [blame] | 3112 | if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) |
| 3113 | prid = dp->i_d.di_projid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | else |
| 3115 | prid = (xfs_prid_t)dfltprid; |
| 3116 | |
| 3117 | /* |
| 3118 | * Make sure that we have allocated dquot(s) on disk. |
| 3119 | */ |
| 3120 | error = XFS_QM_DQVOPALLOC(mp, dp, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 3121 | current_fsuid(credp), current_fsgid(credp), prid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp); |
| 3123 | if (error) |
| 3124 | goto std_return; |
| 3125 | |
| 3126 | tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK); |
| 3127 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
| 3128 | /* |
| 3129 | * The symlink will fit into the inode data fork? |
| 3130 | * There can't be any attributes so we get the whole variable part. |
| 3131 | */ |
| 3132 | if (pathlen <= XFS_LITINO(mp)) |
| 3133 | fs_blocks = 0; |
| 3134 | else |
| 3135 | fs_blocks = XFS_B_TO_FSB(mp, pathlen); |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 3136 | resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0, |
| 3138 | XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); |
| 3139 | if (error == ENOSPC && fs_blocks == 0) { |
| 3140 | resblks = 0; |
| 3141 | error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0, |
| 3142 | XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); |
| 3143 | } |
| 3144 | if (error) { |
| 3145 | cancel_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | goto error_return; |
| 3147 | } |
| 3148 | |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 3149 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3150 | unlock_dp_on_error = B_TRUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | |
| 3152 | /* |
| 3153 | * Check whether the directory allows new symlinks or not. |
| 3154 | */ |
| 3155 | if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) { |
| 3156 | error = XFS_ERROR(EPERM); |
| 3157 | goto error_return; |
| 3158 | } |
| 3159 | |
| 3160 | /* |
| 3161 | * Reserve disk quota : blocks and inode. |
| 3162 | */ |
| 3163 | error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0); |
| 3164 | if (error) |
| 3165 | goto error_return; |
| 3166 | |
| 3167 | /* |
| 3168 | * Check for ability to enter directory entry, if no space reserved. |
| 3169 | */ |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 3170 | error = xfs_dir_canenter(tp, dp, link_name, resblks); |
| 3171 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | goto error_return; |
| 3173 | /* |
| 3174 | * Initialize the bmap freelist prior to calling either |
| 3175 | * bmapi or the directory create code. |
| 3176 | */ |
| 3177 | XFS_BMAP_INIT(&free_list, &first_block); |
| 3178 | |
| 3179 | /* |
| 3180 | * Allocate an inode for the symlink. |
| 3181 | */ |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 3182 | error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | 1, 0, credp, prid, resblks > 0, &ip, NULL); |
| 3184 | if (error) { |
| 3185 | if (error == ENOSPC) |
| 3186 | goto error_return; |
| 3187 | goto error1; |
| 3188 | } |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 3189 | xfs_itrace_ref(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3191 | /* |
| 3192 | * An error after we've joined dp to the transaction will result in the |
| 3193 | * transaction cancel unlocking dp so don't do it explicitly in the |
| 3194 | * error path. |
| 3195 | */ |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 3196 | IHOLD(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3198 | unlock_dp_on_error = B_FALSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | |
| 3200 | /* |
| 3201 | * Also attach the dquot(s) to it, if applicable. |
| 3202 | */ |
| 3203 | XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp); |
| 3204 | |
| 3205 | if (resblks) |
| 3206 | resblks -= XFS_IALLOC_SPACE_RES(mp); |
| 3207 | /* |
| 3208 | * If the symlink will fit into the inode, write it inline. |
| 3209 | */ |
| 3210 | if (pathlen <= XFS_IFORK_DSIZE(ip)) { |
| 3211 | xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK); |
| 3212 | memcpy(ip->i_df.if_u1.if_data, target_path, pathlen); |
| 3213 | ip->i_d.di_size = pathlen; |
| 3214 | |
| 3215 | /* |
| 3216 | * The inode was initially created in extent format. |
| 3217 | */ |
| 3218 | ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT); |
| 3219 | ip->i_df.if_flags |= XFS_IFINLINE; |
| 3220 | |
| 3221 | ip->i_d.di_format = XFS_DINODE_FMT_LOCAL; |
| 3222 | xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE); |
| 3223 | |
| 3224 | } else { |
| 3225 | first_fsb = 0; |
| 3226 | nmaps = SYMLINK_MAPS; |
| 3227 | |
| 3228 | error = xfs_bmapi(tp, ip, first_fsb, fs_blocks, |
| 3229 | XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, |
| 3230 | &first_block, resblks, mval, &nmaps, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3231 | &free_list, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | if (error) { |
| 3233 | goto error1; |
| 3234 | } |
| 3235 | |
| 3236 | if (resblks) |
| 3237 | resblks -= fs_blocks; |
| 3238 | ip->i_d.di_size = pathlen; |
| 3239 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 3240 | |
| 3241 | cur_chunk = target_path; |
| 3242 | for (n = 0; n < nmaps; n++) { |
| 3243 | d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); |
| 3244 | byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); |
| 3245 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, |
| 3246 | BTOBB(byte_cnt), 0); |
| 3247 | ASSERT(bp && !XFS_BUF_GETERROR(bp)); |
| 3248 | if (pathlen < byte_cnt) { |
| 3249 | byte_cnt = pathlen; |
| 3250 | } |
| 3251 | pathlen -= byte_cnt; |
| 3252 | |
| 3253 | memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt); |
| 3254 | cur_chunk += byte_cnt; |
| 3255 | |
| 3256 | xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1); |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | /* |
| 3261 | * Create the directory entry for the symlink. |
| 3262 | */ |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 3263 | error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, |
| 3264 | &first_block, &free_list, resblks); |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 3265 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | goto error1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 3268 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
| 3269 | |
| 3270 | /* |
| 3271 | * Bump the in memory version number of the parent directory |
| 3272 | * so that other processes accessing it will recognize that |
| 3273 | * the directory has changed. |
| 3274 | */ |
| 3275 | dp->i_gen++; |
| 3276 | |
| 3277 | /* |
| 3278 | * If this is a synchronous mount, make sure that the |
| 3279 | * symlink transaction goes to disk before returning to |
| 3280 | * the user. |
| 3281 | */ |
| 3282 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { |
| 3283 | xfs_trans_set_sync(tp); |
| 3284 | } |
| 3285 | |
| 3286 | /* |
| 3287 | * xfs_trans_commit normally decrements the vnode ref count |
| 3288 | * when it unlocks the inode. Since we want to return the |
| 3289 | * vnode to the caller, we bump the vnode ref count now. |
| 3290 | */ |
| 3291 | IHOLD(ip); |
| 3292 | |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3293 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | if (error) { |
| 3295 | goto error2; |
| 3296 | } |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3297 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | XFS_QM_DQRELE(mp, udqp); |
| 3299 | XFS_QM_DQRELE(mp, gdqp); |
| 3300 | |
| 3301 | /* Fall through to std_return with error = 0 or errno from |
| 3302 | * xfs_trans_commit */ |
| 3303 | std_return: |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3304 | if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 3306 | dp, DM_RIGHT_NULL, |
| 3307 | error ? NULL : ip, |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 3308 | DM_RIGHT_NULL, link_name->name, |
| 3309 | target_path, 0, error, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | } |
| 3311 | |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 3312 | if (!error) |
| 3313 | *ipp = ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | return error; |
| 3315 | |
| 3316 | error2: |
| 3317 | IRELE(ip); |
| 3318 | error1: |
| 3319 | xfs_bmap_cancel(&free_list); |
| 3320 | cancel_flags |= XFS_TRANS_ABORT; |
| 3321 | error_return: |
| 3322 | xfs_trans_cancel(tp, cancel_flags); |
| 3323 | XFS_QM_DQRELE(mp, udqp); |
| 3324 | XFS_QM_DQRELE(mp, gdqp); |
| 3325 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3326 | if (unlock_dp_on_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3327 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3328 | |
| 3329 | goto std_return; |
| 3330 | } |
| 3331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | xfs_inode_flush( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3334 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | int flags) |
| 3336 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3337 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | int error = 0; |
| 3339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 3341 | return XFS_ERROR(EIO); |
| 3342 | |
| 3343 | /* |
| 3344 | * Bypass inodes which have already been cleaned by |
| 3345 | * the inode flush clustering code inside xfs_iflush |
| 3346 | */ |
David Chinner | 3354040 | 2008-03-06 13:43:59 +1100 | [diff] [blame] | 3347 | if (xfs_inode_clean(ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | return 0; |
| 3349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | /* |
| 3351 | * We make this non-blocking if the inode is contended, |
| 3352 | * return EAGAIN to indicate to the caller that they |
| 3353 | * did not succeed. This prevents the flush path from |
| 3354 | * blocking on inodes inside another operation right |
| 3355 | * now, they get caught later by xfs_sync. |
| 3356 | */ |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3357 | if (flags & FLUSH_SYNC) { |
| 3358 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 3359 | xfs_iflock(ip); |
| 3360 | } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) { |
| 3361 | if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) { |
| 3362 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | return EAGAIN; |
| 3364 | } |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3365 | } else { |
| 3366 | return EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | } |
| 3368 | |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 3369 | error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC |
| 3370 | : XFS_IFLUSH_ASYNC_NOBLOCK); |
| 3371 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 3372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | return error; |
| 3374 | } |
| 3375 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | int |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3378 | xfs_set_dmattrs( |
| 3379 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | u_int evmask, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3381 | u_int16_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3382 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3383 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | int error; |
| 3386 | |
| 3387 | if (!capable(CAP_SYS_ADMIN)) |
| 3388 | return XFS_ERROR(EPERM); |
| 3389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 3391 | return XFS_ERROR(EIO); |
| 3392 | |
| 3393 | tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS); |
| 3394 | error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0); |
| 3395 | if (error) { |
| 3396 | xfs_trans_cancel(tp, 0); |
| 3397 | return error; |
| 3398 | } |
| 3399 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 3400 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 3401 | |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 3402 | ip->i_d.di_dmevmask = evmask; |
| 3403 | ip->i_d.di_dmstate = state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | |
| 3405 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 3406 | IHOLD(ip); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3407 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | |
| 3409 | return error; |
| 3410 | } |
| 3411 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3412 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | xfs_reclaim( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3414 | xfs_inode_t *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 3416 | bhv_vnode_t *vp = XFS_ITOV(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 3418 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | |
| 3420 | ASSERT(!VN_MAPPED(vp)); |
| 3421 | |
| 3422 | /* bad inode, get out here ASAP */ |
| 3423 | if (VN_BAD(vp)) { |
| 3424 | xfs_ireclaim(ip); |
| 3425 | return 0; |
| 3426 | } |
| 3427 | |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 3428 | vn_iowait(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | |
Christoph Hellwig | 51c91ed | 2005-09-02 16:58:38 +1000 | [diff] [blame] | 3430 | ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 3432 | /* |
| 3433 | * Make sure the atime in the XFS inode is correct before freeing the |
| 3434 | * Linux inode. |
| 3435 | */ |
| 3436 | xfs_synchronize_atime(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3437 | |
David Chinner | 4c60658 | 2006-11-11 18:05:00 +1100 | [diff] [blame] | 3438 | /* |
| 3439 | * If we have nothing to flush with this inode then complete the |
| 3440 | * teardown now, otherwise break the link between the xfs inode and the |
| 3441 | * linux inode and clean up the xfs inode later. This avoids flushing |
| 3442 | * the inode to disk during the delete operation itself. |
| 3443 | * |
| 3444 | * When breaking the link, we need to set the XFS_IRECLAIMABLE flag |
| 3445 | * first to ensure that xfs_iunpin() will never see an xfs inode |
| 3446 | * that has a linux inode being reclaimed. Synchronisation is provided |
| 3447 | * by the i_flags_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | */ |
| 3449 | if (!ip->i_update_core && (ip->i_itemp == NULL)) { |
| 3450 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 3451 | xfs_iflock(ip); |
| 3452 | return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC); |
| 3453 | } else { |
| 3454 | xfs_mount_t *mp = ip->i_mount; |
| 3455 | |
David Chinner | 4c60658 | 2006-11-11 18:05:00 +1100 | [diff] [blame] | 3456 | /* Protect sync and unpin from us */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | XFS_MOUNT_ILOCK(mp); |
David Chinner | 4c60658 | 2006-11-11 18:05:00 +1100 | [diff] [blame] | 3458 | spin_lock(&ip->i_flags_lock); |
| 3459 | __xfs_iflags_set(ip, XFS_IRECLAIMABLE); |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 3460 | vn_to_inode(vp)->i_private = NULL; |
| 3461 | ip->i_vnode = NULL; |
David Chinner | 4c60658 | 2006-11-11 18:05:00 +1100 | [diff] [blame] | 3462 | spin_unlock(&ip->i_flags_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | list_add_tail(&ip->i_reclaim, &mp->m_del_inodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | XFS_MOUNT_IUNLOCK(mp); |
| 3465 | } |
| 3466 | return 0; |
| 3467 | } |
| 3468 | |
| 3469 | int |
| 3470 | xfs_finish_reclaim( |
| 3471 | xfs_inode_t *ip, |
| 3472 | int locked, |
| 3473 | int sync_mode) |
| 3474 | { |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 3475 | xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino); |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 3476 | bhv_vnode_t *vp = XFS_ITOV_NULL(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | int error; |
| 3478 | |
| 3479 | if (vp && VN_BAD(vp)) |
| 3480 | goto reclaim; |
| 3481 | |
| 3482 | /* The hash lock here protects a thread in xfs_iget_core from |
| 3483 | * racing with us on linking the inode back with a vnode. |
| 3484 | * Once we have the XFS_IRECLAIM flag set it will not touch |
| 3485 | * us. |
| 3486 | */ |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 3487 | write_lock(&pag->pag_ici_lock); |
David Chinner | f273ab8 | 2006-09-28 11:06:03 +1000 | [diff] [blame] | 3488 | spin_lock(&ip->i_flags_lock); |
David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 3489 | if (__xfs_iflags_test(ip, XFS_IRECLAIM) || |
| 3490 | (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) { |
David Chinner | f273ab8 | 2006-09-28 11:06:03 +1000 | [diff] [blame] | 3491 | spin_unlock(&ip->i_flags_lock); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 3492 | write_unlock(&pag->pag_ici_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | if (locked) { |
| 3494 | xfs_ifunlock(ip); |
| 3495 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3496 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3497 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | } |
David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 3499 | __xfs_iflags_set(ip, XFS_IRECLAIM); |
David Chinner | f273ab8 | 2006-09-28 11:06:03 +1000 | [diff] [blame] | 3500 | spin_unlock(&ip->i_flags_lock); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 3501 | write_unlock(&pag->pag_ici_lock); |
| 3502 | xfs_put_perag(ip->i_mount, pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | |
| 3504 | /* |
| 3505 | * If the inode is still dirty, then flush it out. If the inode |
| 3506 | * is not in the AIL, then it will be OK to flush it delwri as |
| 3507 | * long as xfs_iflush() does not keep any references to the inode. |
| 3508 | * We leave that decision up to xfs_iflush() since it has the |
| 3509 | * knowledge of whether it's OK to simply do a delwri flush of |
| 3510 | * the inode or whether we need to wait until the inode is |
| 3511 | * pulled from the AIL. |
| 3512 | * We get the flush lock regardless, though, just to make sure |
| 3513 | * we don't free it while it is being flushed. |
| 3514 | */ |
Lachlan McIlroy | 461aa8a | 2008-03-06 13:43:11 +1100 | [diff] [blame] | 3515 | if (!locked) { |
| 3516 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 3517 | xfs_iflock(ip); |
| 3518 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3519 | |
Lachlan McIlroy | 461aa8a | 2008-03-06 13:43:11 +1100 | [diff] [blame] | 3520 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | if (ip->i_update_core || |
| 3522 | ((ip->i_itemp != NULL) && |
| 3523 | (ip->i_itemp->ili_format.ilf_fields != 0))) { |
| 3524 | error = xfs_iflush(ip, sync_mode); |
| 3525 | /* |
| 3526 | * If we hit an error, typically because of filesystem |
| 3527 | * shutdown, we don't need to let vn_reclaim to know |
| 3528 | * because we're gonna reclaim the inode anyway. |
| 3529 | */ |
| 3530 | if (error) { |
| 3531 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3532 | goto reclaim; |
| 3533 | } |
| 3534 | xfs_iflock(ip); /* synchronize with xfs_iflush_done */ |
| 3535 | } |
| 3536 | |
| 3537 | ASSERT(ip->i_update_core == 0); |
| 3538 | ASSERT(ip->i_itemp == NULL || |
| 3539 | ip->i_itemp->ili_format.ilf_fields == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | } |
| 3541 | |
Lachlan McIlroy | 461aa8a | 2008-03-06 13:43:11 +1100 | [diff] [blame] | 3542 | xfs_ifunlock(ip); |
| 3543 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | reclaim: |
| 3546 | xfs_ireclaim(ip); |
| 3547 | return 0; |
| 3548 | } |
| 3549 | |
| 3550 | int |
| 3551 | xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock) |
| 3552 | { |
| 3553 | int purged; |
| 3554 | xfs_inode_t *ip, *n; |
| 3555 | int done = 0; |
| 3556 | |
| 3557 | while (!done) { |
| 3558 | purged = 0; |
| 3559 | XFS_MOUNT_ILOCK(mp); |
| 3560 | list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) { |
| 3561 | if (noblock) { |
| 3562 | if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) |
| 3563 | continue; |
| 3564 | if (xfs_ipincount(ip) || |
| 3565 | !xfs_iflock_nowait(ip)) { |
| 3566 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3567 | continue; |
| 3568 | } |
| 3569 | } |
| 3570 | XFS_MOUNT_IUNLOCK(mp); |
Felix Blyakher | 6b2cf61 | 2005-11-25 16:42:13 +1100 | [diff] [blame] | 3571 | if (xfs_finish_reclaim(ip, noblock, |
| 3572 | XFS_IFLUSH_DELWRI_ELSE_ASYNC)) |
| 3573 | delay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3574 | purged = 1; |
| 3575 | break; |
| 3576 | } |
| 3577 | |
| 3578 | done = !purged; |
| 3579 | } |
| 3580 | |
| 3581 | XFS_MOUNT_IUNLOCK(mp); |
| 3582 | return 0; |
| 3583 | } |
| 3584 | |
| 3585 | /* |
| 3586 | * xfs_alloc_file_space() |
| 3587 | * This routine allocates disk space for the given file. |
| 3588 | * |
| 3589 | * If alloc_type == 0, this request is for an ALLOCSP type |
| 3590 | * request which will change the file size. In this case, no |
| 3591 | * DMAPI event will be generated by the call. A TRUNCATE event |
| 3592 | * will be generated later by xfs_setattr. |
| 3593 | * |
| 3594 | * If alloc_type != 0, this request is for a RESVSP type |
| 3595 | * request, and a DMAPI DM_EVENT_WRITE will be generated if the |
| 3596 | * lower block boundary byte address is less than the file's |
| 3597 | * length. |
| 3598 | * |
| 3599 | * RETURNS: |
| 3600 | * 0 on success |
| 3601 | * errno on error |
| 3602 | * |
| 3603 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 3604 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | xfs_alloc_file_space( |
| 3606 | xfs_inode_t *ip, |
| 3607 | xfs_off_t offset, |
| 3608 | xfs_off_t len, |
| 3609 | int alloc_type, |
| 3610 | int attr_flags) |
| 3611 | { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3612 | xfs_mount_t *mp = ip->i_mount; |
| 3613 | xfs_off_t count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | xfs_filblks_t allocated_fsb; |
| 3615 | xfs_filblks_t allocatesize_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3616 | xfs_extlen_t extsz, temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | xfs_fileoff_t startoffset_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3618 | xfs_fsblock_t firstfsb; |
| 3619 | int nimaps; |
| 3620 | int bmapi_flag; |
| 3621 | int quota_flag; |
| 3622 | int rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | xfs_trans_t *tp; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3624 | xfs_bmbt_irec_t imaps[1], *imapp; |
| 3625 | xfs_bmap_free_t free_list; |
| 3626 | uint qblocks, resblks, resrtextents; |
| 3627 | int committed; |
| 3628 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3629 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 3630 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | |
| 3632 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 3633 | return XFS_ERROR(EIO); |
| 3634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | if ((error = XFS_QM_DQATTACH(mp, ip, 0))) |
| 3636 | return error; |
| 3637 | |
| 3638 | if (len <= 0) |
| 3639 | return XFS_ERROR(EINVAL); |
| 3640 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3641 | rt = XFS_IS_REALTIME_INODE(ip); |
| 3642 | extsz = xfs_get_extsz_hint(ip); |
| 3643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3644 | count = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | imapp = &imaps[0]; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3646 | nimaps = 1; |
| 3647 | bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | startoffset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 3649 | allocatesize_fsb = XFS_B_TO_FSB(mp, count); |
| 3650 | |
| 3651 | /* Generate a DMAPI event if needed. */ |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3652 | if (alloc_type != 0 && offset < ip->i_size && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | (attr_flags&ATTR_DMI) == 0 && |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 3654 | DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3655 | xfs_off_t end_dmi_offset; |
| 3656 | |
| 3657 | end_dmi_offset = offset+len; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3658 | if (end_dmi_offset > ip->i_size) |
| 3659 | end_dmi_offset = ip->i_size; |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 3660 | error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset, |
| 3661 | end_dmi_offset - offset, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3663 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3667 | * Allocate file space until done or until there is an error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3668 | */ |
| 3669 | retry: |
| 3670 | while (allocatesize_fsb && !error) { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3671 | xfs_fileoff_t s, e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3673 | /* |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3674 | * Determine space reservations for data/realtime. |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3675 | */ |
| 3676 | if (unlikely(extsz)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | s = startoffset_fsb; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3678 | do_div(s, extsz); |
| 3679 | s *= extsz; |
| 3680 | e = startoffset_fsb + allocatesize_fsb; |
| 3681 | if ((temp = do_mod(startoffset_fsb, extsz))) |
| 3682 | e += temp; |
| 3683 | if ((temp = do_mod(e, extsz))) |
| 3684 | e += extsz - temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | } else { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3686 | s = 0; |
| 3687 | e = allocatesize_fsb; |
| 3688 | } |
| 3689 | |
| 3690 | if (unlikely(rt)) { |
| 3691 | resrtextents = qblocks = (uint)(e - s); |
| 3692 | resrtextents /= mp->m_sb.sb_rextsize; |
| 3693 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 3694 | quota_flag = XFS_QMOPT_RES_RTBLKS; |
| 3695 | } else { |
| 3696 | resrtextents = 0; |
| 3697 | resblks = qblocks = \ |
| 3698 | XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s)); |
| 3699 | quota_flag = XFS_QMOPT_RES_REGBLKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3703 | * Allocate and setup the transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | */ |
| 3705 | tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3706 | error = xfs_trans_reserve(tp, resblks, |
| 3707 | XFS_WRITE_LOG_RES(mp), resrtextents, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | XFS_TRANS_PERM_LOG_RES, |
| 3709 | XFS_WRITE_LOG_COUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3711 | * Check for running out of space |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | */ |
| 3713 | if (error) { |
| 3714 | /* |
| 3715 | * Free the transaction structure. |
| 3716 | */ |
| 3717 | ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp)); |
| 3718 | xfs_trans_cancel(tp, 0); |
| 3719 | break; |
| 3720 | } |
| 3721 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3722 | error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, |
| 3723 | qblocks, 0, quota_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | if (error) |
| 3725 | goto error1; |
| 3726 | |
| 3727 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 3728 | xfs_trans_ihold(tp, ip); |
| 3729 | |
| 3730 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3731 | * Issue the xfs_bmapi() call to allocate the blocks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | */ |
| 3733 | XFS_BMAP_INIT(&free_list, &firstfsb); |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 3734 | error = xfs_bmapi(tp, ip, startoffset_fsb, |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3735 | allocatesize_fsb, bmapi_flag, |
| 3736 | &firstfsb, 0, imapp, &nimaps, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3737 | &free_list, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | if (error) { |
| 3739 | goto error0; |
| 3740 | } |
| 3741 | |
| 3742 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3743 | * Complete the transaction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3744 | */ |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3745 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | if (error) { |
| 3747 | goto error0; |
| 3748 | } |
| 3749 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3750 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3752 | if (error) { |
| 3753 | break; |
| 3754 | } |
| 3755 | |
| 3756 | allocated_fsb = imapp->br_blockcount; |
| 3757 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3758 | if (nimaps == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | error = XFS_ERROR(ENOSPC); |
| 3760 | break; |
| 3761 | } |
| 3762 | |
| 3763 | startoffset_fsb += allocated_fsb; |
| 3764 | allocatesize_fsb -= allocated_fsb; |
| 3765 | } |
| 3766 | dmapi_enospc_check: |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 3767 | if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 && |
| 3768 | DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE, |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 3770 | ip, DM_RIGHT_NULL, |
| 3771 | ip, DM_RIGHT_NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */ |
| 3773 | if (error == 0) |
| 3774 | goto retry; /* Maybe DMAPI app. has made space */ |
| 3775 | /* else fall through with error from XFS_SEND_DATA */ |
| 3776 | } |
| 3777 | |
| 3778 | return error; |
| 3779 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3780 | error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | xfs_bmap_cancel(&free_list); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3782 | XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag); |
| 3783 | |
| 3784 | error1: /* Just cancel transaction */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3785 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 3786 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3787 | goto dmapi_enospc_check; |
| 3788 | } |
| 3789 | |
| 3790 | /* |
| 3791 | * Zero file bytes between startoff and endoff inclusive. |
| 3792 | * The iolock is held exclusive and no blocks are buffered. |
| 3793 | */ |
| 3794 | STATIC int |
| 3795 | xfs_zero_remaining_bytes( |
| 3796 | xfs_inode_t *ip, |
| 3797 | xfs_off_t startoff, |
| 3798 | xfs_off_t endoff) |
| 3799 | { |
| 3800 | xfs_bmbt_irec_t imap; |
| 3801 | xfs_fileoff_t offset_fsb; |
| 3802 | xfs_off_t lastoffset; |
| 3803 | xfs_off_t offset; |
| 3804 | xfs_buf_t *bp; |
| 3805 | xfs_mount_t *mp = ip->i_mount; |
| 3806 | int nimap; |
| 3807 | int error = 0; |
| 3808 | |
| 3809 | bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize, |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3810 | XFS_IS_REALTIME_INODE(ip) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | mp->m_rtdev_targp : mp->m_ddev_targp); |
| 3812 | |
| 3813 | for (offset = startoff; offset <= endoff; offset = lastoffset + 1) { |
| 3814 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 3815 | nimap = 1; |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 3816 | error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3817 | NULL, 0, &imap, &nimap, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | if (error || nimap < 1) |
| 3819 | break; |
| 3820 | ASSERT(imap.br_blockcount >= 1); |
| 3821 | ASSERT(imap.br_startoff == offset_fsb); |
| 3822 | lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1; |
| 3823 | if (lastoffset > endoff) |
| 3824 | lastoffset = endoff; |
| 3825 | if (imap.br_startblock == HOLESTARTBLOCK) |
| 3826 | continue; |
| 3827 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
| 3828 | if (imap.br_state == XFS_EXT_UNWRITTEN) |
| 3829 | continue; |
| 3830 | XFS_BUF_UNDONE(bp); |
| 3831 | XFS_BUF_UNWRITE(bp); |
| 3832 | XFS_BUF_READ(bp); |
| 3833 | XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock)); |
| 3834 | xfsbdstrat(mp, bp); |
David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 3835 | error = xfs_iowait(bp); |
| 3836 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | xfs_ioerror_alert("xfs_zero_remaining_bytes(read)", |
| 3838 | mp, bp, XFS_BUF_ADDR(bp)); |
| 3839 | break; |
| 3840 | } |
| 3841 | memset(XFS_BUF_PTR(bp) + |
| 3842 | (offset - XFS_FSB_TO_B(mp, imap.br_startoff)), |
| 3843 | 0, lastoffset - offset + 1); |
| 3844 | XFS_BUF_UNDONE(bp); |
| 3845 | XFS_BUF_UNREAD(bp); |
| 3846 | XFS_BUF_WRITE(bp); |
| 3847 | xfsbdstrat(mp, bp); |
David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 3848 | error = xfs_iowait(bp); |
| 3849 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3850 | xfs_ioerror_alert("xfs_zero_remaining_bytes(write)", |
| 3851 | mp, bp, XFS_BUF_ADDR(bp)); |
| 3852 | break; |
| 3853 | } |
| 3854 | } |
| 3855 | xfs_buf_free(bp); |
| 3856 | return error; |
| 3857 | } |
| 3858 | |
| 3859 | /* |
| 3860 | * xfs_free_file_space() |
| 3861 | * This routine frees disk space for the given file. |
| 3862 | * |
| 3863 | * This routine is only called by xfs_change_file_space |
| 3864 | * for an UNRESVSP type call. |
| 3865 | * |
| 3866 | * RETURNS: |
| 3867 | * 0 on success |
| 3868 | * errno on error |
| 3869 | * |
| 3870 | */ |
| 3871 | STATIC int |
| 3872 | xfs_free_file_space( |
| 3873 | xfs_inode_t *ip, |
| 3874 | xfs_off_t offset, |
| 3875 | xfs_off_t len, |
| 3876 | int attr_flags) |
| 3877 | { |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 3878 | bhv_vnode_t *vp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | int committed; |
| 3880 | int done; |
| 3881 | xfs_off_t end_dmi_offset; |
| 3882 | xfs_fileoff_t endoffset_fsb; |
| 3883 | int error; |
| 3884 | xfs_fsblock_t firstfsb; |
| 3885 | xfs_bmap_free_t free_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | xfs_bmbt_irec_t imap; |
| 3887 | xfs_off_t ioffset; |
| 3888 | xfs_extlen_t mod=0; |
| 3889 | xfs_mount_t *mp; |
| 3890 | int nimap; |
| 3891 | uint resblks; |
Nathan Scott | 673cdf5 | 2006-09-28 10:56:26 +1000 | [diff] [blame] | 3892 | uint rounding; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | int rt; |
| 3894 | xfs_fileoff_t startoffset_fsb; |
| 3895 | xfs_trans_t *tp; |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 3896 | int need_iolock = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3897 | |
Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 3898 | vp = XFS_ITOV(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3899 | mp = ip->i_mount; |
| 3900 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 3901 | xfs_itrace_entry(ip); |
Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 3902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3903 | if ((error = XFS_QM_DQATTACH(mp, ip, 0))) |
| 3904 | return error; |
| 3905 | |
| 3906 | error = 0; |
| 3907 | if (len <= 0) /* if nothing being freed */ |
| 3908 | return error; |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3909 | rt = XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3910 | startoffset_fsb = XFS_B_TO_FSB(mp, offset); |
| 3911 | end_dmi_offset = offset + len; |
| 3912 | endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset); |
| 3913 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 3914 | if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 && |
| 3915 | DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3916 | if (end_dmi_offset > ip->i_size) |
| 3917 | end_dmi_offset = ip->i_size; |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 3918 | error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | offset, end_dmi_offset - offset, |
| 3920 | AT_DELAY_FLAG(attr_flags), NULL); |
| 3921 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3922 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | } |
| 3924 | |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 3925 | if (attr_flags & ATTR_NOLOCK) |
| 3926 | need_iolock = 0; |
Yingping Lu | 9fa8046 | 2006-03-22 12:44:35 +1100 | [diff] [blame] | 3927 | if (need_iolock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 3929 | vn_iowait(ip); /* wait for the completion of any pending DIOs */ |
Yingping Lu | 9fa8046 | 2006-03-22 12:44:35 +1100 | [diff] [blame] | 3930 | } |
Dean Roehrich | 5fcbab3 | 2005-05-05 13:27:19 -0700 | [diff] [blame] | 3931 | |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 3932 | rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | ioffset = offset & ~(rounding - 1); |
Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 3934 | |
| 3935 | if (VN_CACHED(vp) != 0) { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 3936 | xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1); |
| 3937 | error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 3938 | if (error) |
| 3939 | goto out_unlock_iolock; |
Christoph Hellwig | bd5a876 | 2005-06-21 15:47:39 +1000 | [diff] [blame] | 3940 | } |
| 3941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | /* |
| 3943 | * Need to zero the stuff we're not freeing, on disk. |
| 3944 | * If its a realtime file & can't use unwritten extents then we |
| 3945 | * actually need to zero the extent edges. Otherwise xfs_bunmapi |
| 3946 | * will take care of it for us. |
| 3947 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3948 | if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | nimap = 1; |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 3950 | error = xfs_bmapi(NULL, ip, startoffset_fsb, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3951 | 1, 0, NULL, 0, &imap, &nimap, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | if (error) |
| 3953 | goto out_unlock_iolock; |
| 3954 | ASSERT(nimap == 0 || nimap == 1); |
| 3955 | if (nimap && imap.br_startblock != HOLESTARTBLOCK) { |
| 3956 | xfs_daddr_t block; |
| 3957 | |
| 3958 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
| 3959 | block = imap.br_startblock; |
| 3960 | mod = do_div(block, mp->m_sb.sb_rextsize); |
| 3961 | if (mod) |
| 3962 | startoffset_fsb += mp->m_sb.sb_rextsize - mod; |
| 3963 | } |
| 3964 | nimap = 1; |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 3965 | error = xfs_bmapi(NULL, ip, endoffset_fsb - 1, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3966 | 1, 0, NULL, 0, &imap, &nimap, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | if (error) |
| 3968 | goto out_unlock_iolock; |
| 3969 | ASSERT(nimap == 0 || nimap == 1); |
| 3970 | if (nimap && imap.br_startblock != HOLESTARTBLOCK) { |
| 3971 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
| 3972 | mod++; |
| 3973 | if (mod && (mod != mp->m_sb.sb_rextsize)) |
| 3974 | endoffset_fsb -= mod; |
| 3975 | } |
| 3976 | } |
| 3977 | if ((done = (endoffset_fsb <= startoffset_fsb))) |
| 3978 | /* |
| 3979 | * One contiguous piece to clear |
| 3980 | */ |
| 3981 | error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1); |
| 3982 | else { |
| 3983 | /* |
| 3984 | * Some full blocks, possibly two pieces to clear |
| 3985 | */ |
| 3986 | if (offset < XFS_FSB_TO_B(mp, startoffset_fsb)) |
| 3987 | error = xfs_zero_remaining_bytes(ip, offset, |
| 3988 | XFS_FSB_TO_B(mp, startoffset_fsb) - 1); |
| 3989 | if (!error && |
| 3990 | XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len) |
| 3991 | error = xfs_zero_remaining_bytes(ip, |
| 3992 | XFS_FSB_TO_B(mp, endoffset_fsb), |
| 3993 | offset + len - 1); |
| 3994 | } |
| 3995 | |
| 3996 | /* |
| 3997 | * free file space until done or until there is an error |
| 3998 | */ |
| 3999 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 4000 | while (!error && !done) { |
| 4001 | |
| 4002 | /* |
David Chinner | 91ebecc | 2007-07-19 16:28:30 +1000 | [diff] [blame] | 4003 | * allocate and setup the transaction. Allow this |
| 4004 | * transaction to dip into the reserve blocks to ensure |
| 4005 | * the freeing of the space succeeds at ENOSPC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | */ |
| 4007 | tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); |
David Chinner | 91ebecc | 2007-07-19 16:28:30 +1000 | [diff] [blame] | 4008 | tp->t_flags |= XFS_TRANS_RESERVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | error = xfs_trans_reserve(tp, |
| 4010 | resblks, |
| 4011 | XFS_WRITE_LOG_RES(mp), |
| 4012 | 0, |
| 4013 | XFS_TRANS_PERM_LOG_RES, |
| 4014 | XFS_WRITE_LOG_COUNT); |
| 4015 | |
| 4016 | /* |
| 4017 | * check for running out of space |
| 4018 | */ |
| 4019 | if (error) { |
| 4020 | /* |
| 4021 | * Free the transaction structure. |
| 4022 | */ |
| 4023 | ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp)); |
| 4024 | xfs_trans_cancel(tp, 0); |
| 4025 | break; |
| 4026 | } |
| 4027 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 4028 | error = XFS_TRANS_RESERVE_QUOTA(mp, tp, |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4029 | ip->i_udquot, ip->i_gdquot, resblks, 0, |
| 4030 | XFS_QMOPT_RES_REGBLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | if (error) |
| 4032 | goto error1; |
| 4033 | |
| 4034 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 4035 | xfs_trans_ihold(tp, ip); |
| 4036 | |
| 4037 | /* |
| 4038 | * issue the bunmapi() call to free the blocks |
| 4039 | */ |
| 4040 | XFS_BMAP_INIT(&free_list, &firstfsb); |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 4041 | error = xfs_bunmapi(tp, ip, startoffset_fsb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | endoffset_fsb - startoffset_fsb, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4043 | 0, 2, &firstfsb, &free_list, NULL, &done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | if (error) { |
| 4045 | goto error0; |
| 4046 | } |
| 4047 | |
| 4048 | /* |
| 4049 | * complete the transaction |
| 4050 | */ |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 4051 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | if (error) { |
| 4053 | goto error0; |
| 4054 | } |
| 4055 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 4056 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 4058 | } |
| 4059 | |
| 4060 | out_unlock_iolock: |
| 4061 | if (need_iolock) |
| 4062 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 4063 | return error; |
| 4064 | |
| 4065 | error0: |
| 4066 | xfs_bmap_cancel(&free_list); |
| 4067 | error1: |
| 4068 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 4069 | xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) : |
| 4070 | XFS_ILOCK_EXCL); |
| 4071 | return error; |
| 4072 | } |
| 4073 | |
| 4074 | /* |
| 4075 | * xfs_change_file_space() |
| 4076 | * This routine allocates or frees disk space for the given file. |
| 4077 | * The user specified parameters are checked for alignment and size |
| 4078 | * limitations. |
| 4079 | * |
| 4080 | * RETURNS: |
| 4081 | * 0 on success |
| 4082 | * errno on error |
| 4083 | * |
| 4084 | */ |
| 4085 | int |
| 4086 | xfs_change_file_space( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 4087 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | int cmd, |
| 4089 | xfs_flock64_t *bf, |
| 4090 | xfs_off_t offset, |
| 4091 | cred_t *credp, |
| 4092 | int attr_flags) |
| 4093 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 4094 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | int clrprealloc; |
| 4096 | int error; |
| 4097 | xfs_fsize_t fsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | int setprealloc; |
| 4099 | xfs_off_t startoffset; |
| 4100 | xfs_off_t llen; |
| 4101 | xfs_trans_t *tp; |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 4102 | bhv_vattr_t va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 4104 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 4106 | if (!S_ISREG(ip->i_d.di_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | return XFS_ERROR(EINVAL); |
| 4108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4109 | switch (bf->l_whence) { |
| 4110 | case 0: /*SEEK_SET*/ |
| 4111 | break; |
| 4112 | case 1: /*SEEK_CUR*/ |
| 4113 | bf->l_start += offset; |
| 4114 | break; |
| 4115 | case 2: /*SEEK_END*/ |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4116 | bf->l_start += ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | break; |
| 4118 | default: |
| 4119 | return XFS_ERROR(EINVAL); |
| 4120 | } |
| 4121 | |
| 4122 | llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len; |
| 4123 | |
| 4124 | if ( (bf->l_start < 0) |
| 4125 | || (bf->l_start > XFS_MAXIOFFSET(mp)) |
| 4126 | || (bf->l_start + llen < 0) |
| 4127 | || (bf->l_start + llen > XFS_MAXIOFFSET(mp))) |
| 4128 | return XFS_ERROR(EINVAL); |
| 4129 | |
| 4130 | bf->l_whence = 0; |
| 4131 | |
| 4132 | startoffset = bf->l_start; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4133 | fsize = ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | |
| 4135 | /* |
| 4136 | * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve |
| 4137 | * file space. |
| 4138 | * These calls do NOT zero the data space allocated to the file, |
| 4139 | * nor do they change the file size. |
| 4140 | * |
| 4141 | * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file |
| 4142 | * space. |
| 4143 | * These calls cause the new file data to be zeroed and the file |
| 4144 | * size to be changed. |
| 4145 | */ |
| 4146 | setprealloc = clrprealloc = 0; |
| 4147 | |
| 4148 | switch (cmd) { |
| 4149 | case XFS_IOC_RESVSP: |
| 4150 | case XFS_IOC_RESVSP64: |
| 4151 | error = xfs_alloc_file_space(ip, startoffset, bf->l_len, |
| 4152 | 1, attr_flags); |
| 4153 | if (error) |
| 4154 | return error; |
| 4155 | setprealloc = 1; |
| 4156 | break; |
| 4157 | |
| 4158 | case XFS_IOC_UNRESVSP: |
| 4159 | case XFS_IOC_UNRESVSP64: |
| 4160 | if ((error = xfs_free_file_space(ip, startoffset, bf->l_len, |
| 4161 | attr_flags))) |
| 4162 | return error; |
| 4163 | break; |
| 4164 | |
| 4165 | case XFS_IOC_ALLOCSP: |
| 4166 | case XFS_IOC_ALLOCSP64: |
| 4167 | case XFS_IOC_FREESP: |
| 4168 | case XFS_IOC_FREESP64: |
| 4169 | if (startoffset > fsize) { |
| 4170 | error = xfs_alloc_file_space(ip, fsize, |
| 4171 | startoffset - fsize, 0, attr_flags); |
| 4172 | if (error) |
| 4173 | break; |
| 4174 | } |
| 4175 | |
| 4176 | va.va_mask = XFS_AT_SIZE; |
| 4177 | va.va_size = startoffset; |
| 4178 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 4179 | error = xfs_setattr(ip, &va, attr_flags, credp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | |
| 4181 | if (error) |
| 4182 | return error; |
| 4183 | |
| 4184 | clrprealloc = 1; |
| 4185 | break; |
| 4186 | |
| 4187 | default: |
| 4188 | ASSERT(0); |
| 4189 | return XFS_ERROR(EINVAL); |
| 4190 | } |
| 4191 | |
| 4192 | /* |
| 4193 | * update the inode timestamp, mode, and prealloc flag bits |
| 4194 | */ |
| 4195 | tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID); |
| 4196 | |
| 4197 | if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp), |
| 4198 | 0, 0, 0))) { |
| 4199 | /* ASSERT(0); */ |
| 4200 | xfs_trans_cancel(tp, 0); |
| 4201 | return error; |
| 4202 | } |
| 4203 | |
| 4204 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 4205 | |
| 4206 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 4207 | xfs_trans_ihold(tp, ip); |
| 4208 | |
| 4209 | if ((attr_flags & ATTR_DMI) == 0) { |
| 4210 | ip->i_d.di_mode &= ~S_ISUID; |
| 4211 | |
| 4212 | /* |
| 4213 | * Note that we don't have to worry about mandatory |
| 4214 | * file locking being disabled here because we only |
| 4215 | * clear the S_ISGID bit if the Group execute bit is |
| 4216 | * on, but if it was on then mandatory locking wouldn't |
| 4217 | * have been enabled. |
| 4218 | */ |
| 4219 | if (ip->i_d.di_mode & S_IXGRP) |
| 4220 | ip->i_d.di_mode &= ~S_ISGID; |
| 4221 | |
| 4222 | xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 4223 | } |
| 4224 | if (setprealloc) |
| 4225 | ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC; |
| 4226 | else if (clrprealloc) |
| 4227 | ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC; |
| 4228 | |
| 4229 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 4230 | xfs_trans_set_sync(tp); |
| 4231 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 4232 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | |
| 4234 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 4235 | |
| 4236 | return error; |
| 4237 | } |