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