Commit 72656c46f50b8dfe50e15793692982e636e3df20

Authored by Dave Chinner
Committed by Dave Chinner
1 parent 9bc08a45fb

xfs: prevent 32bit overflow in space reservation

If we attempt to preallocate more than 2^32 blocks of space in a
single syscall, the transaction block reservation will overflow
leading to a hangs in the superblock block accounting code. This
is trivially reproduced with xfs_io. Fix the problem by capping the
allocation reservation to the maximum number of blocks a single
xfs_bmapi() call can allocate (2^21 blocks).

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>

Showing 1 changed file with 10 additions and 3 deletions Inline Diff

fs/xfs/xfs_vnodeops.c
1 /* 1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc. 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved. 3 * All Rights Reserved.
4 * 4 *
5 * This program is free software; you can redistribute it and/or 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 6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * This program is distributed in the hope that it would be useful, 9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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, 15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */ 17 */
18 18
19 #include "xfs.h" 19 #include "xfs.h"
20 #include "xfs_fs.h" 20 #include "xfs_fs.h"
21 #include "xfs_types.h" 21 #include "xfs_types.h"
22 #include "xfs_bit.h" 22 #include "xfs_bit.h"
23 #include "xfs_log.h" 23 #include "xfs_log.h"
24 #include "xfs_inum.h" 24 #include "xfs_inum.h"
25 #include "xfs_trans.h" 25 #include "xfs_trans.h"
26 #include "xfs_sb.h" 26 #include "xfs_sb.h"
27 #include "xfs_ag.h" 27 #include "xfs_ag.h"
28 #include "xfs_dir2.h" 28 #include "xfs_dir2.h"
29 #include "xfs_mount.h" 29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h" 30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h" 31 #include "xfs_bmap_btree.h"
32 #include "xfs_ialloc_btree.h" 32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dinode.h" 33 #include "xfs_dinode.h"
34 #include "xfs_inode.h" 34 #include "xfs_inode.h"
35 #include "xfs_inode_item.h" 35 #include "xfs_inode_item.h"
36 #include "xfs_itable.h" 36 #include "xfs_itable.h"
37 #include "xfs_ialloc.h" 37 #include "xfs_ialloc.h"
38 #include "xfs_alloc.h" 38 #include "xfs_alloc.h"
39 #include "xfs_bmap.h" 39 #include "xfs_bmap.h"
40 #include "xfs_acl.h" 40 #include "xfs_acl.h"
41 #include "xfs_attr.h" 41 #include "xfs_attr.h"
42 #include "xfs_rw.h" 42 #include "xfs_rw.h"
43 #include "xfs_error.h" 43 #include "xfs_error.h"
44 #include "xfs_quota.h" 44 #include "xfs_quota.h"
45 #include "xfs_utils.h" 45 #include "xfs_utils.h"
46 #include "xfs_rtalloc.h" 46 #include "xfs_rtalloc.h"
47 #include "xfs_trans_space.h" 47 #include "xfs_trans_space.h"
48 #include "xfs_log_priv.h" 48 #include "xfs_log_priv.h"
49 #include "xfs_filestream.h" 49 #include "xfs_filestream.h"
50 #include "xfs_vnodeops.h" 50 #include "xfs_vnodeops.h"
51 #include "xfs_trace.h" 51 #include "xfs_trace.h"
52 52
53 int 53 int
54 xfs_setattr( 54 xfs_setattr(
55 struct xfs_inode *ip, 55 struct xfs_inode *ip,
56 struct iattr *iattr, 56 struct iattr *iattr,
57 int flags) 57 int flags)
58 { 58 {
59 xfs_mount_t *mp = ip->i_mount; 59 xfs_mount_t *mp = ip->i_mount;
60 struct inode *inode = VFS_I(ip); 60 struct inode *inode = VFS_I(ip);
61 int mask = iattr->ia_valid; 61 int mask = iattr->ia_valid;
62 xfs_trans_t *tp; 62 xfs_trans_t *tp;
63 int code; 63 int code;
64 uint lock_flags; 64 uint lock_flags;
65 uint commit_flags=0; 65 uint commit_flags=0;
66 uid_t uid=0, iuid=0; 66 uid_t uid=0, iuid=0;
67 gid_t gid=0, igid=0; 67 gid_t gid=0, igid=0;
68 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2; 68 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
69 int need_iolock = 1; 69 int need_iolock = 1;
70 70
71 trace_xfs_setattr(ip); 71 trace_xfs_setattr(ip);
72 72
73 if (mp->m_flags & XFS_MOUNT_RDONLY) 73 if (mp->m_flags & XFS_MOUNT_RDONLY)
74 return XFS_ERROR(EROFS); 74 return XFS_ERROR(EROFS);
75 75
76 if (XFS_FORCED_SHUTDOWN(mp)) 76 if (XFS_FORCED_SHUTDOWN(mp))
77 return XFS_ERROR(EIO); 77 return XFS_ERROR(EIO);
78 78
79 code = -inode_change_ok(inode, iattr); 79 code = -inode_change_ok(inode, iattr);
80 if (code) 80 if (code)
81 return code; 81 return code;
82 82
83 olddquot1 = olddquot2 = NULL; 83 olddquot1 = olddquot2 = NULL;
84 udqp = gdqp = NULL; 84 udqp = gdqp = NULL;
85 85
86 /* 86 /*
87 * If disk quotas is on, we make sure that the dquots do exist on disk, 87 * If disk quotas is on, we make sure that the dquots do exist on disk,
88 * before we start any other transactions. Trying to do this later 88 * before we start any other transactions. Trying to do this later
89 * is messy. We don't care to take a readlock to look at the ids 89 * is messy. We don't care to take a readlock to look at the ids
90 * in inode here, because we can't hold it across the trans_reserve. 90 * in inode here, because we can't hold it across the trans_reserve.
91 * If the IDs do change before we take the ilock, we're covered 91 * If the IDs do change before we take the ilock, we're covered
92 * because the i_*dquot fields will get updated anyway. 92 * because the i_*dquot fields will get updated anyway.
93 */ 93 */
94 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) { 94 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
95 uint qflags = 0; 95 uint qflags = 0;
96 96
97 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) { 97 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
98 uid = iattr->ia_uid; 98 uid = iattr->ia_uid;
99 qflags |= XFS_QMOPT_UQUOTA; 99 qflags |= XFS_QMOPT_UQUOTA;
100 } else { 100 } else {
101 uid = ip->i_d.di_uid; 101 uid = ip->i_d.di_uid;
102 } 102 }
103 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) { 103 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
104 gid = iattr->ia_gid; 104 gid = iattr->ia_gid;
105 qflags |= XFS_QMOPT_GQUOTA; 105 qflags |= XFS_QMOPT_GQUOTA;
106 } else { 106 } else {
107 gid = ip->i_d.di_gid; 107 gid = ip->i_d.di_gid;
108 } 108 }
109 109
110 /* 110 /*
111 * We take a reference when we initialize udqp and gdqp, 111 * We take a reference when we initialize udqp and gdqp,
112 * so it is important that we never blindly double trip on 112 * so it is important that we never blindly double trip on
113 * the same variable. See xfs_create() for an example. 113 * the same variable. See xfs_create() for an example.
114 */ 114 */
115 ASSERT(udqp == NULL); 115 ASSERT(udqp == NULL);
116 ASSERT(gdqp == NULL); 116 ASSERT(gdqp == NULL);
117 code = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid, 117 code = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid,
118 qflags, &udqp, &gdqp); 118 qflags, &udqp, &gdqp);
119 if (code) 119 if (code)
120 return code; 120 return code;
121 } 121 }
122 122
123 /* 123 /*
124 * For the other attributes, we acquire the inode lock and 124 * For the other attributes, we acquire the inode lock and
125 * first do an error checking pass. 125 * first do an error checking pass.
126 */ 126 */
127 tp = NULL; 127 tp = NULL;
128 lock_flags = XFS_ILOCK_EXCL; 128 lock_flags = XFS_ILOCK_EXCL;
129 if (flags & XFS_ATTR_NOLOCK) 129 if (flags & XFS_ATTR_NOLOCK)
130 need_iolock = 0; 130 need_iolock = 0;
131 if (!(mask & ATTR_SIZE)) { 131 if (!(mask & ATTR_SIZE)) {
132 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE); 132 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
133 commit_flags = 0; 133 commit_flags = 0;
134 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 134 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp),
135 0, 0, 0); 135 0, 0, 0);
136 if (code) { 136 if (code) {
137 lock_flags = 0; 137 lock_flags = 0;
138 goto error_return; 138 goto error_return;
139 } 139 }
140 } else { 140 } else {
141 if (need_iolock) 141 if (need_iolock)
142 lock_flags |= XFS_IOLOCK_EXCL; 142 lock_flags |= XFS_IOLOCK_EXCL;
143 } 143 }
144 144
145 xfs_ilock(ip, lock_flags); 145 xfs_ilock(ip, lock_flags);
146 146
147 /* 147 /*
148 * Change file ownership. Must be the owner or privileged. 148 * Change file ownership. Must be the owner or privileged.
149 */ 149 */
150 if (mask & (ATTR_UID|ATTR_GID)) { 150 if (mask & (ATTR_UID|ATTR_GID)) {
151 /* 151 /*
152 * These IDs could have changed since we last looked at them. 152 * These IDs could have changed since we last looked at them.
153 * But, we're assured that if the ownership did change 153 * But, we're assured that if the ownership did change
154 * while we didn't have the inode locked, inode's dquot(s) 154 * while we didn't have the inode locked, inode's dquot(s)
155 * would have changed also. 155 * would have changed also.
156 */ 156 */
157 iuid = ip->i_d.di_uid; 157 iuid = ip->i_d.di_uid;
158 igid = ip->i_d.di_gid; 158 igid = ip->i_d.di_gid;
159 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid; 159 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
160 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid; 160 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
161 161
162 /* 162 /*
163 * Do a quota reservation only if uid/gid is actually 163 * Do a quota reservation only if uid/gid is actually
164 * going to change. 164 * going to change.
165 */ 165 */
166 if (XFS_IS_QUOTA_RUNNING(mp) && 166 if (XFS_IS_QUOTA_RUNNING(mp) &&
167 ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) || 167 ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
168 (XFS_IS_GQUOTA_ON(mp) && igid != gid))) { 168 (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
169 ASSERT(tp); 169 ASSERT(tp);
170 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp, 170 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
171 capable(CAP_FOWNER) ? 171 capable(CAP_FOWNER) ?
172 XFS_QMOPT_FORCE_RES : 0); 172 XFS_QMOPT_FORCE_RES : 0);
173 if (code) /* out of quota */ 173 if (code) /* out of quota */
174 goto error_return; 174 goto error_return;
175 } 175 }
176 } 176 }
177 177
178 /* 178 /*
179 * Truncate file. Must have write permission and not be a directory. 179 * Truncate file. Must have write permission and not be a directory.
180 */ 180 */
181 if (mask & ATTR_SIZE) { 181 if (mask & ATTR_SIZE) {
182 /* Short circuit the truncate case for zero length files */ 182 /* Short circuit the truncate case for zero length files */
183 if (iattr->ia_size == 0 && 183 if (iattr->ia_size == 0 &&
184 ip->i_size == 0 && ip->i_d.di_nextents == 0) { 184 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
185 xfs_iunlock(ip, XFS_ILOCK_EXCL); 185 xfs_iunlock(ip, XFS_ILOCK_EXCL);
186 lock_flags &= ~XFS_ILOCK_EXCL; 186 lock_flags &= ~XFS_ILOCK_EXCL;
187 if (mask & ATTR_CTIME) 187 if (mask & ATTR_CTIME)
188 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 188 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
189 code = 0; 189 code = 0;
190 goto error_return; 190 goto error_return;
191 } 191 }
192 192
193 if (S_ISDIR(ip->i_d.di_mode)) { 193 if (S_ISDIR(ip->i_d.di_mode)) {
194 code = XFS_ERROR(EISDIR); 194 code = XFS_ERROR(EISDIR);
195 goto error_return; 195 goto error_return;
196 } else if (!S_ISREG(ip->i_d.di_mode)) { 196 } else if (!S_ISREG(ip->i_d.di_mode)) {
197 code = XFS_ERROR(EINVAL); 197 code = XFS_ERROR(EINVAL);
198 goto error_return; 198 goto error_return;
199 } 199 }
200 200
201 /* 201 /*
202 * Make sure that the dquots are attached to the inode. 202 * Make sure that the dquots are attached to the inode.
203 */ 203 */
204 code = xfs_qm_dqattach_locked(ip, 0); 204 code = xfs_qm_dqattach_locked(ip, 0);
205 if (code) 205 if (code)
206 goto error_return; 206 goto error_return;
207 207
208 /* 208 /*
209 * Now we can make the changes. Before we join the inode 209 * Now we can make the changes. Before we join the inode
210 * to the transaction, if ATTR_SIZE is set then take care of 210 * to the transaction, if ATTR_SIZE is set then take care of
211 * the part of the truncation that must be done without the 211 * the part of the truncation that must be done without the
212 * inode lock. This needs to be done before joining the inode 212 * inode lock. This needs to be done before joining the inode
213 * to the transaction, because the inode cannot be unlocked 213 * to the transaction, because the inode cannot be unlocked
214 * once it is a part of the transaction. 214 * once it is a part of the transaction.
215 */ 215 */
216 if (iattr->ia_size > ip->i_size) { 216 if (iattr->ia_size > ip->i_size) {
217 /* 217 /*
218 * Do the first part of growing a file: zero any data 218 * Do the first part of growing a file: zero any data
219 * in the last block that is beyond the old EOF. We 219 * in the last block that is beyond the old EOF. We
220 * need to do this before the inode is joined to the 220 * need to do this before the inode is joined to the
221 * transaction to modify the i_size. 221 * transaction to modify the i_size.
222 */ 222 */
223 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size); 223 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
224 if (code) 224 if (code)
225 goto error_return; 225 goto error_return;
226 } 226 }
227 xfs_iunlock(ip, XFS_ILOCK_EXCL); 227 xfs_iunlock(ip, XFS_ILOCK_EXCL);
228 lock_flags &= ~XFS_ILOCK_EXCL; 228 lock_flags &= ~XFS_ILOCK_EXCL;
229 229
230 /* 230 /*
231 * We are going to log the inode size change in this 231 * We are going to log the inode size change in this
232 * transaction so any previous writes that are beyond the on 232 * transaction so any previous writes that are beyond the on
233 * disk EOF and the new EOF that have not been written out need 233 * disk EOF and the new EOF that have not been written out need
234 * to be written here. If we do not write the data out, we 234 * to be written here. If we do not write the data out, we
235 * expose ourselves to the null files problem. 235 * expose ourselves to the null files problem.
236 * 236 *
237 * Only flush from the on disk size to the smaller of the in 237 * Only flush from the on disk size to the smaller of the in
238 * memory file size or the new size as that's the range we 238 * memory file size or the new size as that's the range we
239 * really care about here and prevents waiting for other data 239 * really care about here and prevents waiting for other data
240 * not within the range we care about here. 240 * not within the range we care about here.
241 */ 241 */
242 if (ip->i_size != ip->i_d.di_size && 242 if (ip->i_size != ip->i_d.di_size &&
243 iattr->ia_size > ip->i_d.di_size) { 243 iattr->ia_size > ip->i_d.di_size) {
244 code = xfs_flush_pages(ip, 244 code = xfs_flush_pages(ip,
245 ip->i_d.di_size, iattr->ia_size, 245 ip->i_d.di_size, iattr->ia_size,
246 XBF_ASYNC, FI_NONE); 246 XBF_ASYNC, FI_NONE);
247 if (code) 247 if (code)
248 goto error_return; 248 goto error_return;
249 } 249 }
250 250
251 /* wait for all I/O to complete */ 251 /* wait for all I/O to complete */
252 xfs_ioend_wait(ip); 252 xfs_ioend_wait(ip);
253 253
254 code = -block_truncate_page(inode->i_mapping, iattr->ia_size, 254 code = -block_truncate_page(inode->i_mapping, iattr->ia_size,
255 xfs_get_blocks); 255 xfs_get_blocks);
256 if (code) 256 if (code)
257 goto error_return; 257 goto error_return;
258 258
259 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE); 259 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
260 code = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, 260 code = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
261 XFS_TRANS_PERM_LOG_RES, 261 XFS_TRANS_PERM_LOG_RES,
262 XFS_ITRUNCATE_LOG_COUNT); 262 XFS_ITRUNCATE_LOG_COUNT);
263 if (code) 263 if (code)
264 goto error_return; 264 goto error_return;
265 265
266 truncate_setsize(inode, iattr->ia_size); 266 truncate_setsize(inode, iattr->ia_size);
267 267
268 commit_flags = XFS_TRANS_RELEASE_LOG_RES; 268 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
269 lock_flags |= XFS_ILOCK_EXCL; 269 lock_flags |= XFS_ILOCK_EXCL;
270 270
271 xfs_ilock(ip, XFS_ILOCK_EXCL); 271 xfs_ilock(ip, XFS_ILOCK_EXCL);
272 272
273 xfs_trans_ijoin(tp, ip); 273 xfs_trans_ijoin(tp, ip);
274 274
275 /* 275 /*
276 * Only change the c/mtime if we are changing the size 276 * Only change the c/mtime if we are changing the size
277 * or we are explicitly asked to change it. This handles 277 * or we are explicitly asked to change it. This handles
278 * the semantic difference between truncate() and ftruncate() 278 * the semantic difference between truncate() and ftruncate()
279 * as implemented in the VFS. 279 * as implemented in the VFS.
280 * 280 *
281 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME 281 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME
282 * is a special case where we need to update the times despite 282 * is a special case where we need to update the times despite
283 * not having these flags set. For all other operations the 283 * not having these flags set. For all other operations the
284 * VFS set these flags explicitly if it wants a timestamp 284 * VFS set these flags explicitly if it wants a timestamp
285 * update. 285 * update.
286 */ 286 */
287 if (iattr->ia_size != ip->i_size && 287 if (iattr->ia_size != ip->i_size &&
288 (!(mask & (ATTR_CTIME | ATTR_MTIME)))) { 288 (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
289 iattr->ia_ctime = iattr->ia_mtime = 289 iattr->ia_ctime = iattr->ia_mtime =
290 current_fs_time(inode->i_sb); 290 current_fs_time(inode->i_sb);
291 mask |= ATTR_CTIME | ATTR_MTIME; 291 mask |= ATTR_CTIME | ATTR_MTIME;
292 } 292 }
293 293
294 if (iattr->ia_size > ip->i_size) { 294 if (iattr->ia_size > ip->i_size) {
295 ip->i_d.di_size = iattr->ia_size; 295 ip->i_d.di_size = iattr->ia_size;
296 ip->i_size = iattr->ia_size; 296 ip->i_size = iattr->ia_size;
297 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 297 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
298 } else if (iattr->ia_size <= ip->i_size || 298 } else if (iattr->ia_size <= ip->i_size ||
299 (iattr->ia_size == 0 && ip->i_d.di_nextents)) { 299 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
300 /* 300 /*
301 * signal a sync transaction unless 301 * signal a sync transaction unless
302 * we're truncating an already unlinked 302 * we're truncating an already unlinked
303 * file on a wsync filesystem 303 * file on a wsync filesystem
304 */ 304 */
305 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size, 305 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
306 XFS_DATA_FORK, 306 XFS_DATA_FORK,
307 ((ip->i_d.di_nlink != 0 || 307 ((ip->i_d.di_nlink != 0 ||
308 !(mp->m_flags & XFS_MOUNT_WSYNC)) 308 !(mp->m_flags & XFS_MOUNT_WSYNC))
309 ? 1 : 0)); 309 ? 1 : 0));
310 if (code) 310 if (code)
311 goto abort_return; 311 goto abort_return;
312 /* 312 /*
313 * Truncated "down", so we're removing references 313 * Truncated "down", so we're removing references
314 * to old data here - if we now delay flushing for 314 * to old data here - if we now delay flushing for
315 * a long time, we expose ourselves unduly to the 315 * a long time, we expose ourselves unduly to the
316 * notorious NULL files problem. So, we mark this 316 * notorious NULL files problem. So, we mark this
317 * vnode and flush it when the file is closed, and 317 * vnode and flush it when the file is closed, and
318 * do not wait the usual (long) time for writeout. 318 * do not wait the usual (long) time for writeout.
319 */ 319 */
320 xfs_iflags_set(ip, XFS_ITRUNCATED); 320 xfs_iflags_set(ip, XFS_ITRUNCATED);
321 } 321 }
322 } else if (tp) { 322 } else if (tp) {
323 xfs_trans_ijoin(tp, ip); 323 xfs_trans_ijoin(tp, ip);
324 } 324 }
325 325
326 /* 326 /*
327 * Change file ownership. Must be the owner or privileged. 327 * Change file ownership. Must be the owner or privileged.
328 */ 328 */
329 if (mask & (ATTR_UID|ATTR_GID)) { 329 if (mask & (ATTR_UID|ATTR_GID)) {
330 /* 330 /*
331 * CAP_FSETID overrides the following restrictions: 331 * CAP_FSETID overrides the following restrictions:
332 * 332 *
333 * The set-user-ID and set-group-ID bits of a file will be 333 * The set-user-ID and set-group-ID bits of a file will be
334 * cleared upon successful return from chown() 334 * cleared upon successful return from chown()
335 */ 335 */
336 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) && 336 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
337 !capable(CAP_FSETID)) { 337 !capable(CAP_FSETID)) {
338 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID); 338 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
339 } 339 }
340 340
341 /* 341 /*
342 * Change the ownerships and register quota modifications 342 * Change the ownerships and register quota modifications
343 * in the transaction. 343 * in the transaction.
344 */ 344 */
345 if (iuid != uid) { 345 if (iuid != uid) {
346 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) { 346 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
347 ASSERT(mask & ATTR_UID); 347 ASSERT(mask & ATTR_UID);
348 ASSERT(udqp); 348 ASSERT(udqp);
349 olddquot1 = xfs_qm_vop_chown(tp, ip, 349 olddquot1 = xfs_qm_vop_chown(tp, ip,
350 &ip->i_udquot, udqp); 350 &ip->i_udquot, udqp);
351 } 351 }
352 ip->i_d.di_uid = uid; 352 ip->i_d.di_uid = uid;
353 inode->i_uid = uid; 353 inode->i_uid = uid;
354 } 354 }
355 if (igid != gid) { 355 if (igid != gid) {
356 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) { 356 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
357 ASSERT(!XFS_IS_PQUOTA_ON(mp)); 357 ASSERT(!XFS_IS_PQUOTA_ON(mp));
358 ASSERT(mask & ATTR_GID); 358 ASSERT(mask & ATTR_GID);
359 ASSERT(gdqp); 359 ASSERT(gdqp);
360 olddquot2 = xfs_qm_vop_chown(tp, ip, 360 olddquot2 = xfs_qm_vop_chown(tp, ip,
361 &ip->i_gdquot, gdqp); 361 &ip->i_gdquot, gdqp);
362 } 362 }
363 ip->i_d.di_gid = gid; 363 ip->i_d.di_gid = gid;
364 inode->i_gid = gid; 364 inode->i_gid = gid;
365 } 365 }
366 } 366 }
367 367
368 /* 368 /*
369 * Change file access modes. 369 * Change file access modes.
370 */ 370 */
371 if (mask & ATTR_MODE) { 371 if (mask & ATTR_MODE) {
372 umode_t mode = iattr->ia_mode; 372 umode_t mode = iattr->ia_mode;
373 373
374 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) 374 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
375 mode &= ~S_ISGID; 375 mode &= ~S_ISGID;
376 376
377 ip->i_d.di_mode &= S_IFMT; 377 ip->i_d.di_mode &= S_IFMT;
378 ip->i_d.di_mode |= mode & ~S_IFMT; 378 ip->i_d.di_mode |= mode & ~S_IFMT;
379 379
380 inode->i_mode &= S_IFMT; 380 inode->i_mode &= S_IFMT;
381 inode->i_mode |= mode & ~S_IFMT; 381 inode->i_mode |= mode & ~S_IFMT;
382 } 382 }
383 383
384 /* 384 /*
385 * Change file access or modified times. 385 * Change file access or modified times.
386 */ 386 */
387 if (mask & ATTR_ATIME) { 387 if (mask & ATTR_ATIME) {
388 inode->i_atime = iattr->ia_atime; 388 inode->i_atime = iattr->ia_atime;
389 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec; 389 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
390 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec; 390 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
391 ip->i_update_core = 1; 391 ip->i_update_core = 1;
392 } 392 }
393 if (mask & ATTR_CTIME) { 393 if (mask & ATTR_CTIME) {
394 inode->i_ctime = iattr->ia_ctime; 394 inode->i_ctime = iattr->ia_ctime;
395 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec; 395 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
396 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec; 396 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
397 ip->i_update_core = 1; 397 ip->i_update_core = 1;
398 } 398 }
399 if (mask & ATTR_MTIME) { 399 if (mask & ATTR_MTIME) {
400 inode->i_mtime = iattr->ia_mtime; 400 inode->i_mtime = iattr->ia_mtime;
401 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec; 401 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
402 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec; 402 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
403 ip->i_update_core = 1; 403 ip->i_update_core = 1;
404 } 404 }
405 405
406 /* 406 /*
407 * And finally, log the inode core if any attribute in it 407 * And finally, log the inode core if any attribute in it
408 * has been changed. 408 * has been changed.
409 */ 409 */
410 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE| 410 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE|
411 ATTR_ATIME|ATTR_CTIME|ATTR_MTIME)) 411 ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
412 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 412 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
413 413
414 XFS_STATS_INC(xs_ig_attrchg); 414 XFS_STATS_INC(xs_ig_attrchg);
415 415
416 /* 416 /*
417 * If this is a synchronous mount, make sure that the 417 * If this is a synchronous mount, make sure that the
418 * transaction goes to disk before returning to the user. 418 * transaction goes to disk before returning to the user.
419 * This is slightly sub-optimal in that truncates require 419 * This is slightly sub-optimal in that truncates require
420 * two sync transactions instead of one for wsync filesystems. 420 * two sync transactions instead of one for wsync filesystems.
421 * One for the truncate and one for the timestamps since we 421 * One for the truncate and one for the timestamps since we
422 * don't want to change the timestamps unless we're sure the 422 * don't want to change the timestamps unless we're sure the
423 * truncate worked. Truncates are less than 1% of the laddis 423 * truncate worked. Truncates are less than 1% of the laddis
424 * mix so this probably isn't worth the trouble to optimize. 424 * mix so this probably isn't worth the trouble to optimize.
425 */ 425 */
426 code = 0; 426 code = 0;
427 if (mp->m_flags & XFS_MOUNT_WSYNC) 427 if (mp->m_flags & XFS_MOUNT_WSYNC)
428 xfs_trans_set_sync(tp); 428 xfs_trans_set_sync(tp);
429 429
430 code = xfs_trans_commit(tp, commit_flags); 430 code = xfs_trans_commit(tp, commit_flags);
431 431
432 xfs_iunlock(ip, lock_flags); 432 xfs_iunlock(ip, lock_flags);
433 433
434 /* 434 /*
435 * Release any dquot(s) the inode had kept before chown. 435 * Release any dquot(s) the inode had kept before chown.
436 */ 436 */
437 xfs_qm_dqrele(olddquot1); 437 xfs_qm_dqrele(olddquot1);
438 xfs_qm_dqrele(olddquot2); 438 xfs_qm_dqrele(olddquot2);
439 xfs_qm_dqrele(udqp); 439 xfs_qm_dqrele(udqp);
440 xfs_qm_dqrele(gdqp); 440 xfs_qm_dqrele(gdqp);
441 441
442 if (code) 442 if (code)
443 return code; 443 return code;
444 444
445 /* 445 /*
446 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode 446 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
447 * update. We could avoid this with linked transactions 447 * update. We could avoid this with linked transactions
448 * and passing down the transaction pointer all the way 448 * and passing down the transaction pointer all the way
449 * to attr_set. No previous user of the generic 449 * to attr_set. No previous user of the generic
450 * Posix ACL code seems to care about this issue either. 450 * Posix ACL code seems to care about this issue either.
451 */ 451 */
452 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) { 452 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
453 code = -xfs_acl_chmod(inode); 453 code = -xfs_acl_chmod(inode);
454 if (code) 454 if (code)
455 return XFS_ERROR(code); 455 return XFS_ERROR(code);
456 } 456 }
457 457
458 return 0; 458 return 0;
459 459
460 abort_return: 460 abort_return:
461 commit_flags |= XFS_TRANS_ABORT; 461 commit_flags |= XFS_TRANS_ABORT;
462 error_return: 462 error_return:
463 xfs_qm_dqrele(udqp); 463 xfs_qm_dqrele(udqp);
464 xfs_qm_dqrele(gdqp); 464 xfs_qm_dqrele(gdqp);
465 if (tp) { 465 if (tp) {
466 xfs_trans_cancel(tp, commit_flags); 466 xfs_trans_cancel(tp, commit_flags);
467 } 467 }
468 if (lock_flags != 0) { 468 if (lock_flags != 0) {
469 xfs_iunlock(ip, lock_flags); 469 xfs_iunlock(ip, lock_flags);
470 } 470 }
471 return code; 471 return code;
472 } 472 }
473 473
474 /* 474 /*
475 * The maximum pathlen is 1024 bytes. Since the minimum file system 475 * The maximum pathlen is 1024 bytes. Since the minimum file system
476 * blocksize is 512 bytes, we can get a max of 2 extents back from 476 * blocksize is 512 bytes, we can get a max of 2 extents back from
477 * bmapi. 477 * bmapi.
478 */ 478 */
479 #define SYMLINK_MAPS 2 479 #define SYMLINK_MAPS 2
480 480
481 STATIC int 481 STATIC int
482 xfs_readlink_bmap( 482 xfs_readlink_bmap(
483 xfs_inode_t *ip, 483 xfs_inode_t *ip,
484 char *link) 484 char *link)
485 { 485 {
486 xfs_mount_t *mp = ip->i_mount; 486 xfs_mount_t *mp = ip->i_mount;
487 int pathlen = ip->i_d.di_size; 487 int pathlen = ip->i_d.di_size;
488 int nmaps = SYMLINK_MAPS; 488 int nmaps = SYMLINK_MAPS;
489 xfs_bmbt_irec_t mval[SYMLINK_MAPS]; 489 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
490 xfs_daddr_t d; 490 xfs_daddr_t d;
491 int byte_cnt; 491 int byte_cnt;
492 int n; 492 int n;
493 xfs_buf_t *bp; 493 xfs_buf_t *bp;
494 int error = 0; 494 int error = 0;
495 495
496 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0, 496 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
497 mval, &nmaps, NULL); 497 mval, &nmaps, NULL);
498 if (error) 498 if (error)
499 goto out; 499 goto out;
500 500
501 for (n = 0; n < nmaps; n++) { 501 for (n = 0; n < nmaps; n++) {
502 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); 502 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
503 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); 503 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
504 504
505 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 505 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
506 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK); 506 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
507 error = XFS_BUF_GETERROR(bp); 507 error = XFS_BUF_GETERROR(bp);
508 if (error) { 508 if (error) {
509 xfs_ioerror_alert("xfs_readlink", 509 xfs_ioerror_alert("xfs_readlink",
510 ip->i_mount, bp, XFS_BUF_ADDR(bp)); 510 ip->i_mount, bp, XFS_BUF_ADDR(bp));
511 xfs_buf_relse(bp); 511 xfs_buf_relse(bp);
512 goto out; 512 goto out;
513 } 513 }
514 if (pathlen < byte_cnt) 514 if (pathlen < byte_cnt)
515 byte_cnt = pathlen; 515 byte_cnt = pathlen;
516 pathlen -= byte_cnt; 516 pathlen -= byte_cnt;
517 517
518 memcpy(link, XFS_BUF_PTR(bp), byte_cnt); 518 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
519 xfs_buf_relse(bp); 519 xfs_buf_relse(bp);
520 } 520 }
521 521
522 link[ip->i_d.di_size] = '\0'; 522 link[ip->i_d.di_size] = '\0';
523 error = 0; 523 error = 0;
524 524
525 out: 525 out:
526 return error; 526 return error;
527 } 527 }
528 528
529 int 529 int
530 xfs_readlink( 530 xfs_readlink(
531 xfs_inode_t *ip, 531 xfs_inode_t *ip,
532 char *link) 532 char *link)
533 { 533 {
534 xfs_mount_t *mp = ip->i_mount; 534 xfs_mount_t *mp = ip->i_mount;
535 int pathlen; 535 int pathlen;
536 int error = 0; 536 int error = 0;
537 537
538 trace_xfs_readlink(ip); 538 trace_xfs_readlink(ip);
539 539
540 if (XFS_FORCED_SHUTDOWN(mp)) 540 if (XFS_FORCED_SHUTDOWN(mp))
541 return XFS_ERROR(EIO); 541 return XFS_ERROR(EIO);
542 542
543 xfs_ilock(ip, XFS_ILOCK_SHARED); 543 xfs_ilock(ip, XFS_ILOCK_SHARED);
544 544
545 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK); 545 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
546 ASSERT(ip->i_d.di_size <= MAXPATHLEN); 546 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
547 547
548 pathlen = ip->i_d.di_size; 548 pathlen = ip->i_d.di_size;
549 if (!pathlen) 549 if (!pathlen)
550 goto out; 550 goto out;
551 551
552 if (ip->i_df.if_flags & XFS_IFINLINE) { 552 if (ip->i_df.if_flags & XFS_IFINLINE) {
553 memcpy(link, ip->i_df.if_u1.if_data, pathlen); 553 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
554 link[pathlen] = '\0'; 554 link[pathlen] = '\0';
555 } else { 555 } else {
556 error = xfs_readlink_bmap(ip, link); 556 error = xfs_readlink_bmap(ip, link);
557 } 557 }
558 558
559 out: 559 out:
560 xfs_iunlock(ip, XFS_ILOCK_SHARED); 560 xfs_iunlock(ip, XFS_ILOCK_SHARED);
561 return error; 561 return error;
562 } 562 }
563 563
564 /* 564 /*
565 * Flags for xfs_free_eofblocks 565 * Flags for xfs_free_eofblocks
566 */ 566 */
567 #define XFS_FREE_EOF_TRYLOCK (1<<0) 567 #define XFS_FREE_EOF_TRYLOCK (1<<0)
568 568
569 /* 569 /*
570 * This is called by xfs_inactive to free any blocks beyond eof 570 * This is called by xfs_inactive to free any blocks beyond eof
571 * when the link count isn't zero and by xfs_dm_punch_hole() when 571 * when the link count isn't zero and by xfs_dm_punch_hole() when
572 * punching a hole to EOF. 572 * punching a hole to EOF.
573 */ 573 */
574 STATIC int 574 STATIC int
575 xfs_free_eofblocks( 575 xfs_free_eofblocks(
576 xfs_mount_t *mp, 576 xfs_mount_t *mp,
577 xfs_inode_t *ip, 577 xfs_inode_t *ip,
578 int flags) 578 int flags)
579 { 579 {
580 xfs_trans_t *tp; 580 xfs_trans_t *tp;
581 int error; 581 int error;
582 xfs_fileoff_t end_fsb; 582 xfs_fileoff_t end_fsb;
583 xfs_fileoff_t last_fsb; 583 xfs_fileoff_t last_fsb;
584 xfs_filblks_t map_len; 584 xfs_filblks_t map_len;
585 int nimaps; 585 int nimaps;
586 xfs_bmbt_irec_t imap; 586 xfs_bmbt_irec_t imap;
587 587
588 /* 588 /*
589 * Figure out if there are any blocks beyond the end 589 * Figure out if there are any blocks beyond the end
590 * of the file. If not, then there is nothing to do. 590 * of the file. If not, then there is nothing to do.
591 */ 591 */
592 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size)); 592 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
593 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); 593 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
594 if (last_fsb <= end_fsb) 594 if (last_fsb <= end_fsb)
595 return 0; 595 return 0;
596 map_len = last_fsb - end_fsb; 596 map_len = last_fsb - end_fsb;
597 597
598 nimaps = 1; 598 nimaps = 1;
599 xfs_ilock(ip, XFS_ILOCK_SHARED); 599 xfs_ilock(ip, XFS_ILOCK_SHARED);
600 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0, 600 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
601 NULL, 0, &imap, &nimaps, NULL); 601 NULL, 0, &imap, &nimaps, NULL);
602 xfs_iunlock(ip, XFS_ILOCK_SHARED); 602 xfs_iunlock(ip, XFS_ILOCK_SHARED);
603 603
604 if (!error && (nimaps != 0) && 604 if (!error && (nimaps != 0) &&
605 (imap.br_startblock != HOLESTARTBLOCK || 605 (imap.br_startblock != HOLESTARTBLOCK ||
606 ip->i_delayed_blks)) { 606 ip->i_delayed_blks)) {
607 /* 607 /*
608 * Attach the dquots to the inode up front. 608 * Attach the dquots to the inode up front.
609 */ 609 */
610 error = xfs_qm_dqattach(ip, 0); 610 error = xfs_qm_dqattach(ip, 0);
611 if (error) 611 if (error)
612 return error; 612 return error;
613 613
614 /* 614 /*
615 * There are blocks after the end of file. 615 * There are blocks after the end of file.
616 * Free them up now by truncating the file to 616 * Free them up now by truncating the file to
617 * its current size. 617 * its current size.
618 */ 618 */
619 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); 619 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
620 620
621 /* 621 /*
622 * Do the xfs_itruncate_start() call before 622 * Do the xfs_itruncate_start() call before
623 * reserving any log space because 623 * reserving any log space because
624 * itruncate_start will call into the buffer 624 * itruncate_start will call into the buffer
625 * cache and we can't 625 * cache and we can't
626 * do that within a transaction. 626 * do that within a transaction.
627 */ 627 */
628 if (flags & XFS_FREE_EOF_TRYLOCK) { 628 if (flags & XFS_FREE_EOF_TRYLOCK) {
629 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { 629 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
630 xfs_trans_cancel(tp, 0); 630 xfs_trans_cancel(tp, 0);
631 return 0; 631 return 0;
632 } 632 }
633 } else { 633 } else {
634 xfs_ilock(ip, XFS_IOLOCK_EXCL); 634 xfs_ilock(ip, XFS_IOLOCK_EXCL);
635 } 635 }
636 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 636 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
637 ip->i_size); 637 ip->i_size);
638 if (error) { 638 if (error) {
639 xfs_trans_cancel(tp, 0); 639 xfs_trans_cancel(tp, 0);
640 xfs_iunlock(ip, XFS_IOLOCK_EXCL); 640 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
641 return error; 641 return error;
642 } 642 }
643 643
644 error = xfs_trans_reserve(tp, 0, 644 error = xfs_trans_reserve(tp, 0,
645 XFS_ITRUNCATE_LOG_RES(mp), 645 XFS_ITRUNCATE_LOG_RES(mp),
646 0, XFS_TRANS_PERM_LOG_RES, 646 0, XFS_TRANS_PERM_LOG_RES,
647 XFS_ITRUNCATE_LOG_COUNT); 647 XFS_ITRUNCATE_LOG_COUNT);
648 if (error) { 648 if (error) {
649 ASSERT(XFS_FORCED_SHUTDOWN(mp)); 649 ASSERT(XFS_FORCED_SHUTDOWN(mp));
650 xfs_trans_cancel(tp, 0); 650 xfs_trans_cancel(tp, 0);
651 xfs_iunlock(ip, XFS_IOLOCK_EXCL); 651 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
652 return error; 652 return error;
653 } 653 }
654 654
655 xfs_ilock(ip, XFS_ILOCK_EXCL); 655 xfs_ilock(ip, XFS_ILOCK_EXCL);
656 xfs_trans_ijoin(tp, ip); 656 xfs_trans_ijoin(tp, ip);
657 657
658 error = xfs_itruncate_finish(&tp, ip, 658 error = xfs_itruncate_finish(&tp, ip,
659 ip->i_size, 659 ip->i_size,
660 XFS_DATA_FORK, 660 XFS_DATA_FORK,
661 0); 661 0);
662 /* 662 /*
663 * If we get an error at this point we 663 * If we get an error at this point we
664 * simply don't bother truncating the file. 664 * simply don't bother truncating the file.
665 */ 665 */
666 if (error) { 666 if (error) {
667 xfs_trans_cancel(tp, 667 xfs_trans_cancel(tp,
668 (XFS_TRANS_RELEASE_LOG_RES | 668 (XFS_TRANS_RELEASE_LOG_RES |
669 XFS_TRANS_ABORT)); 669 XFS_TRANS_ABORT));
670 } else { 670 } else {
671 error = xfs_trans_commit(tp, 671 error = xfs_trans_commit(tp,
672 XFS_TRANS_RELEASE_LOG_RES); 672 XFS_TRANS_RELEASE_LOG_RES);
673 } 673 }
674 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL); 674 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
675 } 675 }
676 return error; 676 return error;
677 } 677 }
678 678
679 /* 679 /*
680 * Free a symlink that has blocks associated with it. 680 * Free a symlink that has blocks associated with it.
681 */ 681 */
682 STATIC int 682 STATIC int
683 xfs_inactive_symlink_rmt( 683 xfs_inactive_symlink_rmt(
684 xfs_inode_t *ip, 684 xfs_inode_t *ip,
685 xfs_trans_t **tpp) 685 xfs_trans_t **tpp)
686 { 686 {
687 xfs_buf_t *bp; 687 xfs_buf_t *bp;
688 int committed; 688 int committed;
689 int done; 689 int done;
690 int error; 690 int error;
691 xfs_fsblock_t first_block; 691 xfs_fsblock_t first_block;
692 xfs_bmap_free_t free_list; 692 xfs_bmap_free_t free_list;
693 int i; 693 int i;
694 xfs_mount_t *mp; 694 xfs_mount_t *mp;
695 xfs_bmbt_irec_t mval[SYMLINK_MAPS]; 695 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
696 int nmaps; 696 int nmaps;
697 xfs_trans_t *ntp; 697 xfs_trans_t *ntp;
698 int size; 698 int size;
699 xfs_trans_t *tp; 699 xfs_trans_t *tp;
700 700
701 tp = *tpp; 701 tp = *tpp;
702 mp = ip->i_mount; 702 mp = ip->i_mount;
703 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip)); 703 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
704 /* 704 /*
705 * We're freeing a symlink that has some 705 * We're freeing a symlink that has some
706 * blocks allocated to it. Free the 706 * blocks allocated to it. Free the
707 * blocks here. We know that we've got 707 * blocks here. We know that we've got
708 * either 1 or 2 extents and that we can 708 * either 1 or 2 extents and that we can
709 * free them all in one bunmapi call. 709 * free them all in one bunmapi call.
710 */ 710 */
711 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2); 711 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
712 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, 712 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
713 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) { 713 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
714 ASSERT(XFS_FORCED_SHUTDOWN(mp)); 714 ASSERT(XFS_FORCED_SHUTDOWN(mp));
715 xfs_trans_cancel(tp, 0); 715 xfs_trans_cancel(tp, 0);
716 *tpp = NULL; 716 *tpp = NULL;
717 return error; 717 return error;
718 } 718 }
719 /* 719 /*
720 * Lock the inode, fix the size, and join it to the transaction. 720 * Lock the inode, fix the size, and join it to the transaction.
721 * Hold it so in the normal path, we still have it locked for 721 * Hold it so in the normal path, we still have it locked for
722 * the second transaction. In the error paths we need it 722 * the second transaction. In the error paths we need it
723 * held so the cancel won't rele it, see below. 723 * held so the cancel won't rele it, see below.
724 */ 724 */
725 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); 725 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
726 size = (int)ip->i_d.di_size; 726 size = (int)ip->i_d.di_size;
727 ip->i_d.di_size = 0; 727 ip->i_d.di_size = 0;
728 xfs_trans_ijoin(tp, ip); 728 xfs_trans_ijoin(tp, ip);
729 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 729 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
730 /* 730 /*
731 * Find the block(s) so we can inval and unmap them. 731 * Find the block(s) so we can inval and unmap them.
732 */ 732 */
733 done = 0; 733 done = 0;
734 xfs_bmap_init(&free_list, &first_block); 734 xfs_bmap_init(&free_list, &first_block);
735 nmaps = ARRAY_SIZE(mval); 735 nmaps = ARRAY_SIZE(mval);
736 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size), 736 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
737 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps, 737 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
738 &free_list))) 738 &free_list)))
739 goto error0; 739 goto error0;
740 /* 740 /*
741 * Invalidate the block(s). 741 * Invalidate the block(s).
742 */ 742 */
743 for (i = 0; i < nmaps; i++) { 743 for (i = 0; i < nmaps; i++) {
744 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, 744 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
745 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock), 745 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
746 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0); 746 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
747 xfs_trans_binval(tp, bp); 747 xfs_trans_binval(tp, bp);
748 } 748 }
749 /* 749 /*
750 * Unmap the dead block(s) to the free_list. 750 * Unmap the dead block(s) to the free_list.
751 */ 751 */
752 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps, 752 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
753 &first_block, &free_list, &done))) 753 &first_block, &free_list, &done)))
754 goto error1; 754 goto error1;
755 ASSERT(done); 755 ASSERT(done);
756 /* 756 /*
757 * Commit the first transaction. This logs the EFI and the inode. 757 * Commit the first transaction. This logs the EFI and the inode.
758 */ 758 */
759 if ((error = xfs_bmap_finish(&tp, &free_list, &committed))) 759 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
760 goto error1; 760 goto error1;
761 /* 761 /*
762 * The transaction must have been committed, since there were 762 * The transaction must have been committed, since there were
763 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish. 763 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
764 * The new tp has the extent freeing and EFDs. 764 * The new tp has the extent freeing and EFDs.
765 */ 765 */
766 ASSERT(committed); 766 ASSERT(committed);
767 /* 767 /*
768 * The first xact was committed, so add the inode to the new one. 768 * The first xact was committed, so add the inode to the new one.
769 * Mark it dirty so it will be logged and moved forward in the log as 769 * Mark it dirty so it will be logged and moved forward in the log as
770 * part of every commit. 770 * part of every commit.
771 */ 771 */
772 xfs_trans_ijoin(tp, ip); 772 xfs_trans_ijoin(tp, ip);
773 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 773 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
774 /* 774 /*
775 * Get a new, empty transaction to return to our caller. 775 * Get a new, empty transaction to return to our caller.
776 */ 776 */
777 ntp = xfs_trans_dup(tp); 777 ntp = xfs_trans_dup(tp);
778 /* 778 /*
779 * Commit the transaction containing extent freeing and EFDs. 779 * Commit the transaction containing extent freeing and EFDs.
780 * If we get an error on the commit here or on the reserve below, 780 * If we get an error on the commit here or on the reserve below,
781 * we need to unlock the inode since the new transaction doesn't 781 * we need to unlock the inode since the new transaction doesn't
782 * have the inode attached. 782 * have the inode attached.
783 */ 783 */
784 error = xfs_trans_commit(tp, 0); 784 error = xfs_trans_commit(tp, 0);
785 tp = ntp; 785 tp = ntp;
786 if (error) { 786 if (error) {
787 ASSERT(XFS_FORCED_SHUTDOWN(mp)); 787 ASSERT(XFS_FORCED_SHUTDOWN(mp));
788 goto error0; 788 goto error0;
789 } 789 }
790 /* 790 /*
791 * transaction commit worked ok so we can drop the extra ticket 791 * transaction commit worked ok so we can drop the extra ticket
792 * reference that we gained in xfs_trans_dup() 792 * reference that we gained in xfs_trans_dup()
793 */ 793 */
794 xfs_log_ticket_put(tp->t_ticket); 794 xfs_log_ticket_put(tp->t_ticket);
795 795
796 /* 796 /*
797 * Remove the memory for extent descriptions (just bookkeeping). 797 * Remove the memory for extent descriptions (just bookkeeping).
798 */ 798 */
799 if (ip->i_df.if_bytes) 799 if (ip->i_df.if_bytes)
800 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK); 800 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
801 ASSERT(ip->i_df.if_bytes == 0); 801 ASSERT(ip->i_df.if_bytes == 0);
802 /* 802 /*
803 * Put an itruncate log reservation in the new transaction 803 * Put an itruncate log reservation in the new transaction
804 * for our caller. 804 * for our caller.
805 */ 805 */
806 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, 806 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
807 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) { 807 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
808 ASSERT(XFS_FORCED_SHUTDOWN(mp)); 808 ASSERT(XFS_FORCED_SHUTDOWN(mp));
809 goto error0; 809 goto error0;
810 } 810 }
811 /* 811 /*
812 * Return with the inode locked but not joined to the transaction. 812 * Return with the inode locked but not joined to the transaction.
813 */ 813 */
814 *tpp = tp; 814 *tpp = tp;
815 return 0; 815 return 0;
816 816
817 error1: 817 error1:
818 xfs_bmap_cancel(&free_list); 818 xfs_bmap_cancel(&free_list);
819 error0: 819 error0:
820 /* 820 /*
821 * Have to come here with the inode locked and either 821 * Have to come here with the inode locked and either
822 * (held and in the transaction) or (not in the transaction). 822 * (held and in the transaction) or (not in the transaction).
823 * If the inode isn't held then cancel would iput it, but 823 * If the inode isn't held then cancel would iput it, but
824 * that's wrong since this is inactive and the vnode ref 824 * that's wrong since this is inactive and the vnode ref
825 * count is 0 already. 825 * count is 0 already.
826 * Cancel won't do anything to the inode if held, but it still 826 * Cancel won't do anything to the inode if held, but it still
827 * needs to be locked until the cancel is done, if it was 827 * needs to be locked until the cancel is done, if it was
828 * joined to the transaction. 828 * joined to the transaction.
829 */ 829 */
830 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); 830 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
831 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); 831 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
832 *tpp = NULL; 832 *tpp = NULL;
833 return error; 833 return error;
834 834
835 } 835 }
836 836
837 STATIC int 837 STATIC int
838 xfs_inactive_symlink_local( 838 xfs_inactive_symlink_local(
839 xfs_inode_t *ip, 839 xfs_inode_t *ip,
840 xfs_trans_t **tpp) 840 xfs_trans_t **tpp)
841 { 841 {
842 int error; 842 int error;
843 843
844 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip)); 844 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
845 /* 845 /*
846 * We're freeing a symlink which fit into 846 * We're freeing a symlink which fit into
847 * the inode. Just free the memory used 847 * the inode. Just free the memory used
848 * to hold the old symlink. 848 * to hold the old symlink.
849 */ 849 */
850 error = xfs_trans_reserve(*tpp, 0, 850 error = xfs_trans_reserve(*tpp, 0,
851 XFS_ITRUNCATE_LOG_RES(ip->i_mount), 851 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
852 0, XFS_TRANS_PERM_LOG_RES, 852 0, XFS_TRANS_PERM_LOG_RES,
853 XFS_ITRUNCATE_LOG_COUNT); 853 XFS_ITRUNCATE_LOG_COUNT);
854 854
855 if (error) { 855 if (error) {
856 xfs_trans_cancel(*tpp, 0); 856 xfs_trans_cancel(*tpp, 0);
857 *tpp = NULL; 857 *tpp = NULL;
858 return error; 858 return error;
859 } 859 }
860 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); 860 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
861 861
862 /* 862 /*
863 * Zero length symlinks _can_ exist. 863 * Zero length symlinks _can_ exist.
864 */ 864 */
865 if (ip->i_df.if_bytes > 0) { 865 if (ip->i_df.if_bytes > 0) {
866 xfs_idata_realloc(ip, 866 xfs_idata_realloc(ip,
867 -(ip->i_df.if_bytes), 867 -(ip->i_df.if_bytes),
868 XFS_DATA_FORK); 868 XFS_DATA_FORK);
869 ASSERT(ip->i_df.if_bytes == 0); 869 ASSERT(ip->i_df.if_bytes == 0);
870 } 870 }
871 return 0; 871 return 0;
872 } 872 }
873 873
874 STATIC int 874 STATIC int
875 xfs_inactive_attrs( 875 xfs_inactive_attrs(
876 xfs_inode_t *ip, 876 xfs_inode_t *ip,
877 xfs_trans_t **tpp) 877 xfs_trans_t **tpp)
878 { 878 {
879 xfs_trans_t *tp; 879 xfs_trans_t *tp;
880 int error; 880 int error;
881 xfs_mount_t *mp; 881 xfs_mount_t *mp;
882 882
883 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); 883 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
884 tp = *tpp; 884 tp = *tpp;
885 mp = ip->i_mount; 885 mp = ip->i_mount;
886 ASSERT(ip->i_d.di_forkoff != 0); 886 ASSERT(ip->i_d.di_forkoff != 0);
887 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 887 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
888 xfs_iunlock(ip, XFS_ILOCK_EXCL); 888 xfs_iunlock(ip, XFS_ILOCK_EXCL);
889 if (error) 889 if (error)
890 goto error_unlock; 890 goto error_unlock;
891 891
892 error = xfs_attr_inactive(ip); 892 error = xfs_attr_inactive(ip);
893 if (error) 893 if (error)
894 goto error_unlock; 894 goto error_unlock;
895 895
896 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); 896 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
897 error = xfs_trans_reserve(tp, 0, 897 error = xfs_trans_reserve(tp, 0,
898 XFS_IFREE_LOG_RES(mp), 898 XFS_IFREE_LOG_RES(mp),
899 0, XFS_TRANS_PERM_LOG_RES, 899 0, XFS_TRANS_PERM_LOG_RES,
900 XFS_INACTIVE_LOG_COUNT); 900 XFS_INACTIVE_LOG_COUNT);
901 if (error) 901 if (error)
902 goto error_cancel; 902 goto error_cancel;
903 903
904 xfs_ilock(ip, XFS_ILOCK_EXCL); 904 xfs_ilock(ip, XFS_ILOCK_EXCL);
905 xfs_trans_ijoin(tp, ip); 905 xfs_trans_ijoin(tp, ip);
906 xfs_idestroy_fork(ip, XFS_ATTR_FORK); 906 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
907 907
908 ASSERT(ip->i_d.di_anextents == 0); 908 ASSERT(ip->i_d.di_anextents == 0);
909 909
910 *tpp = tp; 910 *tpp = tp;
911 return 0; 911 return 0;
912 912
913 error_cancel: 913 error_cancel:
914 ASSERT(XFS_FORCED_SHUTDOWN(mp)); 914 ASSERT(XFS_FORCED_SHUTDOWN(mp));
915 xfs_trans_cancel(tp, 0); 915 xfs_trans_cancel(tp, 0);
916 error_unlock: 916 error_unlock:
917 *tpp = NULL; 917 *tpp = NULL;
918 xfs_iunlock(ip, XFS_IOLOCK_EXCL); 918 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
919 return error; 919 return error;
920 } 920 }
921 921
922 int 922 int
923 xfs_release( 923 xfs_release(
924 xfs_inode_t *ip) 924 xfs_inode_t *ip)
925 { 925 {
926 xfs_mount_t *mp = ip->i_mount; 926 xfs_mount_t *mp = ip->i_mount;
927 int error; 927 int error;
928 928
929 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0)) 929 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
930 return 0; 930 return 0;
931 931
932 /* If this is a read-only mount, don't do this (would generate I/O) */ 932 /* If this is a read-only mount, don't do this (would generate I/O) */
933 if (mp->m_flags & XFS_MOUNT_RDONLY) 933 if (mp->m_flags & XFS_MOUNT_RDONLY)
934 return 0; 934 return 0;
935 935
936 if (!XFS_FORCED_SHUTDOWN(mp)) { 936 if (!XFS_FORCED_SHUTDOWN(mp)) {
937 int truncated; 937 int truncated;
938 938
939 /* 939 /*
940 * If we are using filestreams, and we have an unlinked 940 * If we are using filestreams, and we have an unlinked
941 * file that we are processing the last close on, then nothing 941 * file that we are processing the last close on, then nothing
942 * will be able to reopen and write to this file. Purge this 942 * will be able to reopen and write to this file. Purge this
943 * inode from the filestreams cache so that it doesn't delay 943 * inode from the filestreams cache so that it doesn't delay
944 * teardown of the inode. 944 * teardown of the inode.
945 */ 945 */
946 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip)) 946 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
947 xfs_filestream_deassociate(ip); 947 xfs_filestream_deassociate(ip);
948 948
949 /* 949 /*
950 * If we previously truncated this file and removed old data 950 * If we previously truncated this file and removed old data
951 * in the process, we want to initiate "early" writeout on 951 * in the process, we want to initiate "early" writeout on
952 * the last close. This is an attempt to combat the notorious 952 * the last close. This is an attempt to combat the notorious
953 * NULL files problem which is particularly noticable from a 953 * NULL files problem which is particularly noticable from a
954 * truncate down, buffered (re-)write (delalloc), followed by 954 * truncate down, buffered (re-)write (delalloc), followed by
955 * a crash. What we are effectively doing here is 955 * a crash. What we are effectively doing here is
956 * significantly reducing the time window where we'd otherwise 956 * significantly reducing the time window where we'd otherwise
957 * be exposed to that problem. 957 * be exposed to that problem.
958 */ 958 */
959 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); 959 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
960 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) 960 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
961 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE); 961 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
962 } 962 }
963 963
964 if (ip->i_d.di_nlink != 0) { 964 if (ip->i_d.di_nlink != 0) {
965 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) && 965 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
966 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 || 966 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
967 ip->i_delayed_blks > 0)) && 967 ip->i_delayed_blks > 0)) &&
968 (ip->i_df.if_flags & XFS_IFEXTENTS)) && 968 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
969 (!(ip->i_d.di_flags & 969 (!(ip->i_d.di_flags &
970 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) { 970 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
971 971
972 /* 972 /*
973 * If we can't get the iolock just skip truncating 973 * If we can't get the iolock just skip truncating
974 * the blocks past EOF because we could deadlock 974 * the blocks past EOF because we could deadlock
975 * with the mmap_sem otherwise. We'll get another 975 * with the mmap_sem otherwise. We'll get another
976 * chance to drop them once the last reference to 976 * chance to drop them once the last reference to
977 * the inode is dropped, so we'll never leak blocks 977 * the inode is dropped, so we'll never leak blocks
978 * permanently. 978 * permanently.
979 */ 979 */
980 error = xfs_free_eofblocks(mp, ip, 980 error = xfs_free_eofblocks(mp, ip,
981 XFS_FREE_EOF_TRYLOCK); 981 XFS_FREE_EOF_TRYLOCK);
982 if (error) 982 if (error)
983 return error; 983 return error;
984 } 984 }
985 } 985 }
986 986
987 return 0; 987 return 0;
988 } 988 }
989 989
990 /* 990 /*
991 * xfs_inactive 991 * xfs_inactive
992 * 992 *
993 * This is called when the vnode reference count for the vnode 993 * This is called when the vnode reference count for the vnode
994 * goes to zero. If the file has been unlinked, then it must 994 * goes to zero. If the file has been unlinked, then it must
995 * now be truncated. Also, we clear all of the read-ahead state 995 * now be truncated. Also, we clear all of the read-ahead state
996 * kept for the inode here since the file is now closed. 996 * kept for the inode here since the file is now closed.
997 */ 997 */
998 int 998 int
999 xfs_inactive( 999 xfs_inactive(
1000 xfs_inode_t *ip) 1000 xfs_inode_t *ip)
1001 { 1001 {
1002 xfs_bmap_free_t free_list; 1002 xfs_bmap_free_t free_list;
1003 xfs_fsblock_t first_block; 1003 xfs_fsblock_t first_block;
1004 int committed; 1004 int committed;
1005 xfs_trans_t *tp; 1005 xfs_trans_t *tp;
1006 xfs_mount_t *mp; 1006 xfs_mount_t *mp;
1007 int error; 1007 int error;
1008 int truncate; 1008 int truncate;
1009 1009
1010 /* 1010 /*
1011 * If the inode is already free, then there can be nothing 1011 * If the inode is already free, then there can be nothing
1012 * to clean up here. 1012 * to clean up here.
1013 */ 1013 */
1014 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) { 1014 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
1015 ASSERT(ip->i_df.if_real_bytes == 0); 1015 ASSERT(ip->i_df.if_real_bytes == 0);
1016 ASSERT(ip->i_df.if_broot_bytes == 0); 1016 ASSERT(ip->i_df.if_broot_bytes == 0);
1017 return VN_INACTIVE_CACHE; 1017 return VN_INACTIVE_CACHE;
1018 } 1018 }
1019 1019
1020 /* 1020 /*
1021 * Only do a truncate if it's a regular file with 1021 * Only do a truncate if it's a regular file with
1022 * some actual space in it. It's OK to look at the 1022 * some actual space in it. It's OK to look at the
1023 * inode's fields without the lock because we're the 1023 * inode's fields without the lock because we're the
1024 * only one with a reference to the inode. 1024 * only one with a reference to the inode.
1025 */ 1025 */
1026 truncate = ((ip->i_d.di_nlink == 0) && 1026 truncate = ((ip->i_d.di_nlink == 0) &&
1027 ((ip->i_d.di_size != 0) || (ip->i_size != 0) || 1027 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1028 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) && 1028 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1029 ((ip->i_d.di_mode & S_IFMT) == S_IFREG)); 1029 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1030 1030
1031 mp = ip->i_mount; 1031 mp = ip->i_mount;
1032 1032
1033 error = 0; 1033 error = 0;
1034 1034
1035 /* If this is a read-only mount, don't do this (would generate I/O) */ 1035 /* If this is a read-only mount, don't do this (would generate I/O) */
1036 if (mp->m_flags & XFS_MOUNT_RDONLY) 1036 if (mp->m_flags & XFS_MOUNT_RDONLY)
1037 goto out; 1037 goto out;
1038 1038
1039 if (ip->i_d.di_nlink != 0) { 1039 if (ip->i_d.di_nlink != 0) {
1040 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) && 1040 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1041 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 || 1041 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1042 ip->i_delayed_blks > 0)) && 1042 ip->i_delayed_blks > 0)) &&
1043 (ip->i_df.if_flags & XFS_IFEXTENTS) && 1043 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1044 (!(ip->i_d.di_flags & 1044 (!(ip->i_d.di_flags &
1045 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) || 1045 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1046 (ip->i_delayed_blks != 0)))) { 1046 (ip->i_delayed_blks != 0)))) {
1047 error = xfs_free_eofblocks(mp, ip, 0); 1047 error = xfs_free_eofblocks(mp, ip, 0);
1048 if (error) 1048 if (error)
1049 return VN_INACTIVE_CACHE; 1049 return VN_INACTIVE_CACHE;
1050 } 1050 }
1051 goto out; 1051 goto out;
1052 } 1052 }
1053 1053
1054 ASSERT(ip->i_d.di_nlink == 0); 1054 ASSERT(ip->i_d.di_nlink == 0);
1055 1055
1056 error = xfs_qm_dqattach(ip, 0); 1056 error = xfs_qm_dqattach(ip, 0);
1057 if (error) 1057 if (error)
1058 return VN_INACTIVE_CACHE; 1058 return VN_INACTIVE_CACHE;
1059 1059
1060 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); 1060 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1061 if (truncate) { 1061 if (truncate) {
1062 /* 1062 /*
1063 * Do the xfs_itruncate_start() call before 1063 * Do the xfs_itruncate_start() call before
1064 * reserving any log space because itruncate_start 1064 * reserving any log space because itruncate_start
1065 * will call into the buffer cache and we can't 1065 * will call into the buffer cache and we can't
1066 * do that within a transaction. 1066 * do that within a transaction.
1067 */ 1067 */
1068 xfs_ilock(ip, XFS_IOLOCK_EXCL); 1068 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1069 1069
1070 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0); 1070 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1071 if (error) { 1071 if (error) {
1072 xfs_trans_cancel(tp, 0); 1072 xfs_trans_cancel(tp, 0);
1073 xfs_iunlock(ip, XFS_IOLOCK_EXCL); 1073 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1074 return VN_INACTIVE_CACHE; 1074 return VN_INACTIVE_CACHE;
1075 } 1075 }
1076 1076
1077 error = xfs_trans_reserve(tp, 0, 1077 error = xfs_trans_reserve(tp, 0,
1078 XFS_ITRUNCATE_LOG_RES(mp), 1078 XFS_ITRUNCATE_LOG_RES(mp),
1079 0, XFS_TRANS_PERM_LOG_RES, 1079 0, XFS_TRANS_PERM_LOG_RES,
1080 XFS_ITRUNCATE_LOG_COUNT); 1080 XFS_ITRUNCATE_LOG_COUNT);
1081 if (error) { 1081 if (error) {
1082 /* Don't call itruncate_cleanup */ 1082 /* Don't call itruncate_cleanup */
1083 ASSERT(XFS_FORCED_SHUTDOWN(mp)); 1083 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1084 xfs_trans_cancel(tp, 0); 1084 xfs_trans_cancel(tp, 0);
1085 xfs_iunlock(ip, XFS_IOLOCK_EXCL); 1085 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1086 return VN_INACTIVE_CACHE; 1086 return VN_INACTIVE_CACHE;
1087 } 1087 }
1088 1088
1089 xfs_ilock(ip, XFS_ILOCK_EXCL); 1089 xfs_ilock(ip, XFS_ILOCK_EXCL);
1090 xfs_trans_ijoin(tp, ip); 1090 xfs_trans_ijoin(tp, ip);
1091 1091
1092 /* 1092 /*
1093 * normally, we have to run xfs_itruncate_finish sync. 1093 * normally, we have to run xfs_itruncate_finish sync.
1094 * But if filesystem is wsync and we're in the inactive 1094 * But if filesystem is wsync and we're in the inactive
1095 * path, then we know that nlink == 0, and that the 1095 * path, then we know that nlink == 0, and that the
1096 * xaction that made nlink == 0 is permanently committed 1096 * xaction that made nlink == 0 is permanently committed
1097 * since xfs_remove runs as a synchronous transaction. 1097 * since xfs_remove runs as a synchronous transaction.
1098 */ 1098 */
1099 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK, 1099 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1100 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0)); 1100 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1101 1101
1102 if (error) { 1102 if (error) {
1103 xfs_trans_cancel(tp, 1103 xfs_trans_cancel(tp,
1104 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); 1104 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1105 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); 1105 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1106 return VN_INACTIVE_CACHE; 1106 return VN_INACTIVE_CACHE;
1107 } 1107 }
1108 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) { 1108 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1109 1109
1110 /* 1110 /*
1111 * If we get an error while cleaning up a 1111 * If we get an error while cleaning up a
1112 * symlink we bail out. 1112 * symlink we bail out.
1113 */ 1113 */
1114 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ? 1114 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1115 xfs_inactive_symlink_rmt(ip, &tp) : 1115 xfs_inactive_symlink_rmt(ip, &tp) :
1116 xfs_inactive_symlink_local(ip, &tp); 1116 xfs_inactive_symlink_local(ip, &tp);
1117 1117
1118 if (error) { 1118 if (error) {
1119 ASSERT(tp == NULL); 1119 ASSERT(tp == NULL);
1120 return VN_INACTIVE_CACHE; 1120 return VN_INACTIVE_CACHE;
1121 } 1121 }
1122 1122
1123 xfs_trans_ijoin(tp, ip); 1123 xfs_trans_ijoin(tp, ip);
1124 } else { 1124 } else {
1125 error = xfs_trans_reserve(tp, 0, 1125 error = xfs_trans_reserve(tp, 0,
1126 XFS_IFREE_LOG_RES(mp), 1126 XFS_IFREE_LOG_RES(mp),
1127 0, XFS_TRANS_PERM_LOG_RES, 1127 0, XFS_TRANS_PERM_LOG_RES,
1128 XFS_INACTIVE_LOG_COUNT); 1128 XFS_INACTIVE_LOG_COUNT);
1129 if (error) { 1129 if (error) {
1130 ASSERT(XFS_FORCED_SHUTDOWN(mp)); 1130 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1131 xfs_trans_cancel(tp, 0); 1131 xfs_trans_cancel(tp, 0);
1132 return VN_INACTIVE_CACHE; 1132 return VN_INACTIVE_CACHE;
1133 } 1133 }
1134 1134
1135 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); 1135 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1136 xfs_trans_ijoin(tp, ip); 1136 xfs_trans_ijoin(tp, ip);
1137 } 1137 }
1138 1138
1139 /* 1139 /*
1140 * If there are attributes associated with the file 1140 * If there are attributes associated with the file
1141 * then blow them away now. The code calls a routine 1141 * then blow them away now. The code calls a routine
1142 * that recursively deconstructs the attribute fork. 1142 * that recursively deconstructs the attribute fork.
1143 * We need to just commit the current transaction 1143 * We need to just commit the current transaction
1144 * because we can't use it for xfs_attr_inactive(). 1144 * because we can't use it for xfs_attr_inactive().
1145 */ 1145 */
1146 if (ip->i_d.di_anextents > 0) { 1146 if (ip->i_d.di_anextents > 0) {
1147 error = xfs_inactive_attrs(ip, &tp); 1147 error = xfs_inactive_attrs(ip, &tp);
1148 /* 1148 /*
1149 * If we got an error, the transaction is already 1149 * If we got an error, the transaction is already
1150 * cancelled, and the inode is unlocked. Just get out. 1150 * cancelled, and the inode is unlocked. Just get out.
1151 */ 1151 */
1152 if (error) 1152 if (error)
1153 return VN_INACTIVE_CACHE; 1153 return VN_INACTIVE_CACHE;
1154 } else if (ip->i_afp) { 1154 } else if (ip->i_afp) {
1155 xfs_idestroy_fork(ip, XFS_ATTR_FORK); 1155 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1156 } 1156 }
1157 1157
1158 /* 1158 /*
1159 * Free the inode. 1159 * Free the inode.
1160 */ 1160 */
1161 xfs_bmap_init(&free_list, &first_block); 1161 xfs_bmap_init(&free_list, &first_block);
1162 error = xfs_ifree(tp, ip, &free_list); 1162 error = xfs_ifree(tp, ip, &free_list);
1163 if (error) { 1163 if (error) {
1164 /* 1164 /*
1165 * If we fail to free the inode, shut down. The cancel 1165 * If we fail to free the inode, shut down. The cancel
1166 * might do that, we need to make sure. Otherwise the 1166 * might do that, we need to make sure. Otherwise the
1167 * inode might be lost for a long time or forever. 1167 * inode might be lost for a long time or forever.
1168 */ 1168 */
1169 if (!XFS_FORCED_SHUTDOWN(mp)) { 1169 if (!XFS_FORCED_SHUTDOWN(mp)) {
1170 cmn_err(CE_NOTE, 1170 cmn_err(CE_NOTE,
1171 "xfs_inactive: xfs_ifree() returned an error = %d on %s", 1171 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1172 error, mp->m_fsname); 1172 error, mp->m_fsname);
1173 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); 1173 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1174 } 1174 }
1175 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); 1175 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1176 } else { 1176 } else {
1177 /* 1177 /*
1178 * Credit the quota account(s). The inode is gone. 1178 * Credit the quota account(s). The inode is gone.
1179 */ 1179 */
1180 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1); 1180 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1181 1181
1182 /* 1182 /*
1183 * Just ignore errors at this point. There is nothing we can 1183 * Just ignore errors at this point. There is nothing we can
1184 * do except to try to keep going. Make sure it's not a silent 1184 * do except to try to keep going. Make sure it's not a silent
1185 * error. 1185 * error.
1186 */ 1186 */
1187 error = xfs_bmap_finish(&tp, &free_list, &committed); 1187 error = xfs_bmap_finish(&tp, &free_list, &committed);
1188 if (error) 1188 if (error)
1189 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: " 1189 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1190 "xfs_bmap_finish() returned error %d", error); 1190 "xfs_bmap_finish() returned error %d", error);
1191 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 1191 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1192 if (error) 1192 if (error)
1193 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: " 1193 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1194 "xfs_trans_commit() returned error %d", error); 1194 "xfs_trans_commit() returned error %d", error);
1195 } 1195 }
1196 1196
1197 /* 1197 /*
1198 * Release the dquots held by inode, if any. 1198 * Release the dquots held by inode, if any.
1199 */ 1199 */
1200 xfs_qm_dqdetach(ip); 1200 xfs_qm_dqdetach(ip);
1201 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); 1201 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1202 1202
1203 out: 1203 out:
1204 return VN_INACTIVE_CACHE; 1204 return VN_INACTIVE_CACHE;
1205 } 1205 }
1206 1206
1207 /* 1207 /*
1208 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match 1208 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1209 * is allowed, otherwise it has to be an exact match. If a CI match is found, 1209 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1210 * ci_name->name will point to a the actual name (caller must free) or 1210 * ci_name->name will point to a the actual name (caller must free) or
1211 * will be set to NULL if an exact match is found. 1211 * will be set to NULL if an exact match is found.
1212 */ 1212 */
1213 int 1213 int
1214 xfs_lookup( 1214 xfs_lookup(
1215 xfs_inode_t *dp, 1215 xfs_inode_t *dp,
1216 struct xfs_name *name, 1216 struct xfs_name *name,
1217 xfs_inode_t **ipp, 1217 xfs_inode_t **ipp,
1218 struct xfs_name *ci_name) 1218 struct xfs_name *ci_name)
1219 { 1219 {
1220 xfs_ino_t inum; 1220 xfs_ino_t inum;
1221 int error; 1221 int error;
1222 uint lock_mode; 1222 uint lock_mode;
1223 1223
1224 trace_xfs_lookup(dp, name); 1224 trace_xfs_lookup(dp, name);
1225 1225
1226 if (XFS_FORCED_SHUTDOWN(dp->i_mount)) 1226 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1227 return XFS_ERROR(EIO); 1227 return XFS_ERROR(EIO);
1228 1228
1229 lock_mode = xfs_ilock_map_shared(dp); 1229 lock_mode = xfs_ilock_map_shared(dp);
1230 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name); 1230 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1231 xfs_iunlock_map_shared(dp, lock_mode); 1231 xfs_iunlock_map_shared(dp, lock_mode);
1232 1232
1233 if (error) 1233 if (error)
1234 goto out; 1234 goto out;
1235 1235
1236 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp); 1236 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
1237 if (error) 1237 if (error)
1238 goto out_free_name; 1238 goto out_free_name;
1239 1239
1240 return 0; 1240 return 0;
1241 1241
1242 out_free_name: 1242 out_free_name:
1243 if (ci_name) 1243 if (ci_name)
1244 kmem_free(ci_name->name); 1244 kmem_free(ci_name->name);
1245 out: 1245 out:
1246 *ipp = NULL; 1246 *ipp = NULL;
1247 return error; 1247 return error;
1248 } 1248 }
1249 1249
1250 int 1250 int
1251 xfs_create( 1251 xfs_create(
1252 xfs_inode_t *dp, 1252 xfs_inode_t *dp,
1253 struct xfs_name *name, 1253 struct xfs_name *name,
1254 mode_t mode, 1254 mode_t mode,
1255 xfs_dev_t rdev, 1255 xfs_dev_t rdev,
1256 xfs_inode_t **ipp, 1256 xfs_inode_t **ipp,
1257 cred_t *credp) 1257 cred_t *credp)
1258 { 1258 {
1259 int is_dir = S_ISDIR(mode); 1259 int is_dir = S_ISDIR(mode);
1260 struct xfs_mount *mp = dp->i_mount; 1260 struct xfs_mount *mp = dp->i_mount;
1261 struct xfs_inode *ip = NULL; 1261 struct xfs_inode *ip = NULL;
1262 struct xfs_trans *tp = NULL; 1262 struct xfs_trans *tp = NULL;
1263 int error; 1263 int error;
1264 xfs_bmap_free_t free_list; 1264 xfs_bmap_free_t free_list;
1265 xfs_fsblock_t first_block; 1265 xfs_fsblock_t first_block;
1266 boolean_t unlock_dp_on_error = B_FALSE; 1266 boolean_t unlock_dp_on_error = B_FALSE;
1267 uint cancel_flags; 1267 uint cancel_flags;
1268 int committed; 1268 int committed;
1269 xfs_prid_t prid; 1269 xfs_prid_t prid;
1270 struct xfs_dquot *udqp = NULL; 1270 struct xfs_dquot *udqp = NULL;
1271 struct xfs_dquot *gdqp = NULL; 1271 struct xfs_dquot *gdqp = NULL;
1272 uint resblks; 1272 uint resblks;
1273 uint log_res; 1273 uint log_res;
1274 uint log_count; 1274 uint log_count;
1275 1275
1276 trace_xfs_create(dp, name); 1276 trace_xfs_create(dp, name);
1277 1277
1278 if (XFS_FORCED_SHUTDOWN(mp)) 1278 if (XFS_FORCED_SHUTDOWN(mp))
1279 return XFS_ERROR(EIO); 1279 return XFS_ERROR(EIO);
1280 1280
1281 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) 1281 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1282 prid = dp->i_d.di_projid; 1282 prid = dp->i_d.di_projid;
1283 else 1283 else
1284 prid = dfltprid; 1284 prid = dfltprid;
1285 1285
1286 /* 1286 /*
1287 * Make sure that we have allocated dquot(s) on disk. 1287 * Make sure that we have allocated dquot(s) on disk.
1288 */ 1288 */
1289 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid, 1289 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1290 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp); 1290 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1291 if (error) 1291 if (error)
1292 goto std_return; 1292 goto std_return;
1293 1293
1294 if (is_dir) { 1294 if (is_dir) {
1295 rdev = 0; 1295 rdev = 0;
1296 resblks = XFS_MKDIR_SPACE_RES(mp, name->len); 1296 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1297 log_res = XFS_MKDIR_LOG_RES(mp); 1297 log_res = XFS_MKDIR_LOG_RES(mp);
1298 log_count = XFS_MKDIR_LOG_COUNT; 1298 log_count = XFS_MKDIR_LOG_COUNT;
1299 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR); 1299 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1300 } else { 1300 } else {
1301 resblks = XFS_CREATE_SPACE_RES(mp, name->len); 1301 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1302 log_res = XFS_CREATE_LOG_RES(mp); 1302 log_res = XFS_CREATE_LOG_RES(mp);
1303 log_count = XFS_CREATE_LOG_COUNT; 1303 log_count = XFS_CREATE_LOG_COUNT;
1304 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE); 1304 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1305 } 1305 }
1306 1306
1307 cancel_flags = XFS_TRANS_RELEASE_LOG_RES; 1307 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1308 1308
1309 /* 1309 /*
1310 * Initially assume that the file does not exist and 1310 * Initially assume that the file does not exist and
1311 * reserve the resources for that case. If that is not 1311 * reserve the resources for that case. If that is not
1312 * the case we'll drop the one we have and get a more 1312 * the case we'll drop the one we have and get a more
1313 * appropriate transaction later. 1313 * appropriate transaction later.
1314 */ 1314 */
1315 error = xfs_trans_reserve(tp, resblks, log_res, 0, 1315 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1316 XFS_TRANS_PERM_LOG_RES, log_count); 1316 XFS_TRANS_PERM_LOG_RES, log_count);
1317 if (error == ENOSPC) { 1317 if (error == ENOSPC) {
1318 /* flush outstanding delalloc blocks and retry */ 1318 /* flush outstanding delalloc blocks and retry */
1319 xfs_flush_inodes(dp); 1319 xfs_flush_inodes(dp);
1320 error = xfs_trans_reserve(tp, resblks, log_res, 0, 1320 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1321 XFS_TRANS_PERM_LOG_RES, log_count); 1321 XFS_TRANS_PERM_LOG_RES, log_count);
1322 } 1322 }
1323 if (error == ENOSPC) { 1323 if (error == ENOSPC) {
1324 /* No space at all so try a "no-allocation" reservation */ 1324 /* No space at all so try a "no-allocation" reservation */
1325 resblks = 0; 1325 resblks = 0;
1326 error = xfs_trans_reserve(tp, 0, log_res, 0, 1326 error = xfs_trans_reserve(tp, 0, log_res, 0,
1327 XFS_TRANS_PERM_LOG_RES, log_count); 1327 XFS_TRANS_PERM_LOG_RES, log_count);
1328 } 1328 }
1329 if (error) { 1329 if (error) {
1330 cancel_flags = 0; 1330 cancel_flags = 0;
1331 goto out_trans_cancel; 1331 goto out_trans_cancel;
1332 } 1332 }
1333 1333
1334 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); 1334 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1335 unlock_dp_on_error = B_TRUE; 1335 unlock_dp_on_error = B_TRUE;
1336 1336
1337 /* 1337 /*
1338 * Check for directory link count overflow. 1338 * Check for directory link count overflow.
1339 */ 1339 */
1340 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) { 1340 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1341 error = XFS_ERROR(EMLINK); 1341 error = XFS_ERROR(EMLINK);
1342 goto out_trans_cancel; 1342 goto out_trans_cancel;
1343 } 1343 }
1344 1344
1345 xfs_bmap_init(&free_list, &first_block); 1345 xfs_bmap_init(&free_list, &first_block);
1346 1346
1347 /* 1347 /*
1348 * Reserve disk quota and the inode. 1348 * Reserve disk quota and the inode.
1349 */ 1349 */
1350 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0); 1350 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
1351 if (error) 1351 if (error)
1352 goto out_trans_cancel; 1352 goto out_trans_cancel;
1353 1353
1354 error = xfs_dir_canenter(tp, dp, name, resblks); 1354 error = xfs_dir_canenter(tp, dp, name, resblks);
1355 if (error) 1355 if (error)
1356 goto out_trans_cancel; 1356 goto out_trans_cancel;
1357 1357
1358 /* 1358 /*
1359 * A newly created regular or special file just has one directory 1359 * A newly created regular or special file just has one directory
1360 * entry pointing to them, but a directory also the "." entry 1360 * entry pointing to them, but a directory also the "." entry
1361 * pointing to itself. 1361 * pointing to itself.
1362 */ 1362 */
1363 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp, 1363 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1364 prid, resblks > 0, &ip, &committed); 1364 prid, resblks > 0, &ip, &committed);
1365 if (error) { 1365 if (error) {
1366 if (error == ENOSPC) 1366 if (error == ENOSPC)
1367 goto out_trans_cancel; 1367 goto out_trans_cancel;
1368 goto out_trans_abort; 1368 goto out_trans_abort;
1369 } 1369 }
1370 1370
1371 /* 1371 /*
1372 * At this point, we've gotten a newly allocated inode. 1372 * At this point, we've gotten a newly allocated inode.
1373 * It is locked (and joined to the transaction). 1373 * It is locked (and joined to the transaction).
1374 */ 1374 */
1375 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 1375 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1376 1376
1377 /* 1377 /*
1378 * Now we join the directory inode to the transaction. We do not do it 1378 * Now we join the directory inode to the transaction. We do not do it
1379 * earlier because xfs_dir_ialloc might commit the previous transaction 1379 * earlier because xfs_dir_ialloc might commit the previous transaction
1380 * (and release all the locks). An error from here on will result in 1380 * (and release all the locks). An error from here on will result in
1381 * the transaction cancel unlocking dp so don't do it explicitly in the 1381 * the transaction cancel unlocking dp so don't do it explicitly in the
1382 * error path. 1382 * error path.
1383 */ 1383 */
1384 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL); 1384 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
1385 unlock_dp_on_error = B_FALSE; 1385 unlock_dp_on_error = B_FALSE;
1386 1386
1387 error = xfs_dir_createname(tp, dp, name, ip->i_ino, 1387 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1388 &first_block, &free_list, resblks ? 1388 &first_block, &free_list, resblks ?
1389 resblks - XFS_IALLOC_SPACE_RES(mp) : 0); 1389 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1390 if (error) { 1390 if (error) {
1391 ASSERT(error != ENOSPC); 1391 ASSERT(error != ENOSPC);
1392 goto out_trans_abort; 1392 goto out_trans_abort;
1393 } 1393 }
1394 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 1394 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1395 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); 1395 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1396 1396
1397 if (is_dir) { 1397 if (is_dir) {
1398 error = xfs_dir_init(tp, ip, dp); 1398 error = xfs_dir_init(tp, ip, dp);
1399 if (error) 1399 if (error)
1400 goto out_bmap_cancel; 1400 goto out_bmap_cancel;
1401 1401
1402 error = xfs_bumplink(tp, dp); 1402 error = xfs_bumplink(tp, dp);
1403 if (error) 1403 if (error)
1404 goto out_bmap_cancel; 1404 goto out_bmap_cancel;
1405 } 1405 }
1406 1406
1407 /* 1407 /*
1408 * If this is a synchronous mount, make sure that the 1408 * If this is a synchronous mount, make sure that the
1409 * create transaction goes to disk before returning to 1409 * create transaction goes to disk before returning to
1410 * the user. 1410 * the user.
1411 */ 1411 */
1412 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) 1412 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1413 xfs_trans_set_sync(tp); 1413 xfs_trans_set_sync(tp);
1414 1414
1415 /* 1415 /*
1416 * Attach the dquot(s) to the inodes and modify them incore. 1416 * Attach the dquot(s) to the inodes and modify them incore.
1417 * These ids of the inode couldn't have changed since the new 1417 * These ids of the inode couldn't have changed since the new
1418 * inode has been locked ever since it was created. 1418 * inode has been locked ever since it was created.
1419 */ 1419 */
1420 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp); 1420 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
1421 1421
1422 /* 1422 /*
1423 * xfs_trans_commit normally decrements the vnode ref count 1423 * xfs_trans_commit normally decrements the vnode ref count
1424 * when it unlocks the inode. Since we want to return the 1424 * when it unlocks the inode. Since we want to return the
1425 * vnode to the caller, we bump the vnode ref count now. 1425 * vnode to the caller, we bump the vnode ref count now.
1426 */ 1426 */
1427 IHOLD(ip); 1427 IHOLD(ip);
1428 1428
1429 error = xfs_bmap_finish(&tp, &free_list, &committed); 1429 error = xfs_bmap_finish(&tp, &free_list, &committed);
1430 if (error) 1430 if (error)
1431 goto out_abort_rele; 1431 goto out_abort_rele;
1432 1432
1433 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 1433 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1434 if (error) { 1434 if (error) {
1435 IRELE(ip); 1435 IRELE(ip);
1436 goto out_dqrele; 1436 goto out_dqrele;
1437 } 1437 }
1438 1438
1439 xfs_qm_dqrele(udqp); 1439 xfs_qm_dqrele(udqp);
1440 xfs_qm_dqrele(gdqp); 1440 xfs_qm_dqrele(gdqp);
1441 1441
1442 *ipp = ip; 1442 *ipp = ip;
1443 return 0; 1443 return 0;
1444 1444
1445 out_bmap_cancel: 1445 out_bmap_cancel:
1446 xfs_bmap_cancel(&free_list); 1446 xfs_bmap_cancel(&free_list);
1447 out_trans_abort: 1447 out_trans_abort:
1448 cancel_flags |= XFS_TRANS_ABORT; 1448 cancel_flags |= XFS_TRANS_ABORT;
1449 out_trans_cancel: 1449 out_trans_cancel:
1450 xfs_trans_cancel(tp, cancel_flags); 1450 xfs_trans_cancel(tp, cancel_flags);
1451 out_dqrele: 1451 out_dqrele:
1452 xfs_qm_dqrele(udqp); 1452 xfs_qm_dqrele(udqp);
1453 xfs_qm_dqrele(gdqp); 1453 xfs_qm_dqrele(gdqp);
1454 1454
1455 if (unlock_dp_on_error) 1455 if (unlock_dp_on_error)
1456 xfs_iunlock(dp, XFS_ILOCK_EXCL); 1456 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1457 std_return: 1457 std_return:
1458 return error; 1458 return error;
1459 1459
1460 out_abort_rele: 1460 out_abort_rele:
1461 /* 1461 /*
1462 * Wait until after the current transaction is aborted to 1462 * Wait until after the current transaction is aborted to
1463 * release the inode. This prevents recursive transactions 1463 * release the inode. This prevents recursive transactions
1464 * and deadlocks from xfs_inactive. 1464 * and deadlocks from xfs_inactive.
1465 */ 1465 */
1466 xfs_bmap_cancel(&free_list); 1466 xfs_bmap_cancel(&free_list);
1467 cancel_flags |= XFS_TRANS_ABORT; 1467 cancel_flags |= XFS_TRANS_ABORT;
1468 xfs_trans_cancel(tp, cancel_flags); 1468 xfs_trans_cancel(tp, cancel_flags);
1469 IRELE(ip); 1469 IRELE(ip);
1470 unlock_dp_on_error = B_FALSE; 1470 unlock_dp_on_error = B_FALSE;
1471 goto out_dqrele; 1471 goto out_dqrele;
1472 } 1472 }
1473 1473
1474 #ifdef DEBUG 1474 #ifdef DEBUG
1475 int xfs_locked_n; 1475 int xfs_locked_n;
1476 int xfs_small_retries; 1476 int xfs_small_retries;
1477 int xfs_middle_retries; 1477 int xfs_middle_retries;
1478 int xfs_lots_retries; 1478 int xfs_lots_retries;
1479 int xfs_lock_delays; 1479 int xfs_lock_delays;
1480 #endif 1480 #endif
1481 1481
1482 /* 1482 /*
1483 * Bump the subclass so xfs_lock_inodes() acquires each lock with 1483 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1484 * a different value 1484 * a different value
1485 */ 1485 */
1486 static inline int 1486 static inline int
1487 xfs_lock_inumorder(int lock_mode, int subclass) 1487 xfs_lock_inumorder(int lock_mode, int subclass)
1488 { 1488 {
1489 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) 1489 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1490 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT; 1490 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1491 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) 1491 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1492 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT; 1492 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1493 1493
1494 return lock_mode; 1494 return lock_mode;
1495 } 1495 }
1496 1496
1497 /* 1497 /*
1498 * The following routine will lock n inodes in exclusive mode. 1498 * The following routine will lock n inodes in exclusive mode.
1499 * We assume the caller calls us with the inodes in i_ino order. 1499 * We assume the caller calls us with the inodes in i_ino order.
1500 * 1500 *
1501 * We need to detect deadlock where an inode that we lock 1501 * We need to detect deadlock where an inode that we lock
1502 * is in the AIL and we start waiting for another inode that is locked 1502 * is in the AIL and we start waiting for another inode that is locked
1503 * by a thread in a long running transaction (such as truncate). This can 1503 * by a thread in a long running transaction (such as truncate). This can
1504 * result in deadlock since the long running trans might need to wait 1504 * result in deadlock since the long running trans might need to wait
1505 * for the inode we just locked in order to push the tail and free space 1505 * for the inode we just locked in order to push the tail and free space
1506 * in the log. 1506 * in the log.
1507 */ 1507 */
1508 void 1508 void
1509 xfs_lock_inodes( 1509 xfs_lock_inodes(
1510 xfs_inode_t **ips, 1510 xfs_inode_t **ips,
1511 int inodes, 1511 int inodes,
1512 uint lock_mode) 1512 uint lock_mode)
1513 { 1513 {
1514 int attempts = 0, i, j, try_lock; 1514 int attempts = 0, i, j, try_lock;
1515 xfs_log_item_t *lp; 1515 xfs_log_item_t *lp;
1516 1516
1517 ASSERT(ips && (inodes >= 2)); /* we need at least two */ 1517 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1518 1518
1519 try_lock = 0; 1519 try_lock = 0;
1520 i = 0; 1520 i = 0;
1521 1521
1522 again: 1522 again:
1523 for (; i < inodes; i++) { 1523 for (; i < inodes; i++) {
1524 ASSERT(ips[i]); 1524 ASSERT(ips[i]);
1525 1525
1526 if (i && (ips[i] == ips[i-1])) /* Already locked */ 1526 if (i && (ips[i] == ips[i-1])) /* Already locked */
1527 continue; 1527 continue;
1528 1528
1529 /* 1529 /*
1530 * If try_lock is not set yet, make sure all locked inodes 1530 * If try_lock is not set yet, make sure all locked inodes
1531 * are not in the AIL. 1531 * are not in the AIL.
1532 * If any are, set try_lock to be used later. 1532 * If any are, set try_lock to be used later.
1533 */ 1533 */
1534 1534
1535 if (!try_lock) { 1535 if (!try_lock) {
1536 for (j = (i - 1); j >= 0 && !try_lock; j--) { 1536 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1537 lp = (xfs_log_item_t *)ips[j]->i_itemp; 1537 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1538 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { 1538 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1539 try_lock++; 1539 try_lock++;
1540 } 1540 }
1541 } 1541 }
1542 } 1542 }
1543 1543
1544 /* 1544 /*
1545 * If any of the previous locks we have locked is in the AIL, 1545 * If any of the previous locks we have locked is in the AIL,
1546 * we must TRY to get the second and subsequent locks. If 1546 * we must TRY to get the second and subsequent locks. If
1547 * we can't get any, we must release all we have 1547 * we can't get any, we must release all we have
1548 * and try again. 1548 * and try again.
1549 */ 1549 */
1550 1550
1551 if (try_lock) { 1551 if (try_lock) {
1552 /* try_lock must be 0 if i is 0. */ 1552 /* try_lock must be 0 if i is 0. */
1553 /* 1553 /*
1554 * try_lock means we have an inode locked 1554 * try_lock means we have an inode locked
1555 * that is in the AIL. 1555 * that is in the AIL.
1556 */ 1556 */
1557 ASSERT(i != 0); 1557 ASSERT(i != 0);
1558 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) { 1558 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1559 attempts++; 1559 attempts++;
1560 1560
1561 /* 1561 /*
1562 * Unlock all previous guys and try again. 1562 * Unlock all previous guys and try again.
1563 * xfs_iunlock will try to push the tail 1563 * xfs_iunlock will try to push the tail
1564 * if the inode is in the AIL. 1564 * if the inode is in the AIL.
1565 */ 1565 */
1566 1566
1567 for(j = i - 1; j >= 0; j--) { 1567 for(j = i - 1; j >= 0; j--) {
1568 1568
1569 /* 1569 /*
1570 * Check to see if we've already 1570 * Check to see if we've already
1571 * unlocked this one. 1571 * unlocked this one.
1572 * Not the first one going back, 1572 * Not the first one going back,
1573 * and the inode ptr is the same. 1573 * and the inode ptr is the same.
1574 */ 1574 */
1575 if ((j != (i - 1)) && ips[j] == 1575 if ((j != (i - 1)) && ips[j] ==
1576 ips[j+1]) 1576 ips[j+1])
1577 continue; 1577 continue;
1578 1578
1579 xfs_iunlock(ips[j], lock_mode); 1579 xfs_iunlock(ips[j], lock_mode);
1580 } 1580 }
1581 1581
1582 if ((attempts % 5) == 0) { 1582 if ((attempts % 5) == 0) {
1583 delay(1); /* Don't just spin the CPU */ 1583 delay(1); /* Don't just spin the CPU */
1584 #ifdef DEBUG 1584 #ifdef DEBUG
1585 xfs_lock_delays++; 1585 xfs_lock_delays++;
1586 #endif 1586 #endif
1587 } 1587 }
1588 i = 0; 1588 i = 0;
1589 try_lock = 0; 1589 try_lock = 0;
1590 goto again; 1590 goto again;
1591 } 1591 }
1592 } else { 1592 } else {
1593 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i)); 1593 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1594 } 1594 }
1595 } 1595 }
1596 1596
1597 #ifdef DEBUG 1597 #ifdef DEBUG
1598 if (attempts) { 1598 if (attempts) {
1599 if (attempts < 5) xfs_small_retries++; 1599 if (attempts < 5) xfs_small_retries++;
1600 else if (attempts < 100) xfs_middle_retries++; 1600 else if (attempts < 100) xfs_middle_retries++;
1601 else xfs_lots_retries++; 1601 else xfs_lots_retries++;
1602 } else { 1602 } else {
1603 xfs_locked_n++; 1603 xfs_locked_n++;
1604 } 1604 }
1605 #endif 1605 #endif
1606 } 1606 }
1607 1607
1608 /* 1608 /*
1609 * xfs_lock_two_inodes() can only be used to lock one type of lock 1609 * xfs_lock_two_inodes() can only be used to lock one type of lock
1610 * at a time - the iolock or the ilock, but not both at once. If 1610 * at a time - the iolock or the ilock, but not both at once. If
1611 * we lock both at once, lockdep will report false positives saying 1611 * we lock both at once, lockdep will report false positives saying
1612 * we have violated locking orders. 1612 * we have violated locking orders.
1613 */ 1613 */
1614 void 1614 void
1615 xfs_lock_two_inodes( 1615 xfs_lock_two_inodes(
1616 xfs_inode_t *ip0, 1616 xfs_inode_t *ip0,
1617 xfs_inode_t *ip1, 1617 xfs_inode_t *ip1,
1618 uint lock_mode) 1618 uint lock_mode)
1619 { 1619 {
1620 xfs_inode_t *temp; 1620 xfs_inode_t *temp;
1621 int attempts = 0; 1621 int attempts = 0;
1622 xfs_log_item_t *lp; 1622 xfs_log_item_t *lp;
1623 1623
1624 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) 1624 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1625 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0); 1625 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1626 ASSERT(ip0->i_ino != ip1->i_ino); 1626 ASSERT(ip0->i_ino != ip1->i_ino);
1627 1627
1628 if (ip0->i_ino > ip1->i_ino) { 1628 if (ip0->i_ino > ip1->i_ino) {
1629 temp = ip0; 1629 temp = ip0;
1630 ip0 = ip1; 1630 ip0 = ip1;
1631 ip1 = temp; 1631 ip1 = temp;
1632 } 1632 }
1633 1633
1634 again: 1634 again:
1635 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0)); 1635 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1636 1636
1637 /* 1637 /*
1638 * If the first lock we have locked is in the AIL, we must TRY to get 1638 * If the first lock we have locked is in the AIL, we must TRY to get
1639 * the second lock. If we can't get it, we must release the first one 1639 * the second lock. If we can't get it, we must release the first one
1640 * and try again. 1640 * and try again.
1641 */ 1641 */
1642 lp = (xfs_log_item_t *)ip0->i_itemp; 1642 lp = (xfs_log_item_t *)ip0->i_itemp;
1643 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { 1643 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1644 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) { 1644 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1645 xfs_iunlock(ip0, lock_mode); 1645 xfs_iunlock(ip0, lock_mode);
1646 if ((++attempts % 5) == 0) 1646 if ((++attempts % 5) == 0)
1647 delay(1); /* Don't just spin the CPU */ 1647 delay(1); /* Don't just spin the CPU */
1648 goto again; 1648 goto again;
1649 } 1649 }
1650 } else { 1650 } else {
1651 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1)); 1651 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1652 } 1652 }
1653 } 1653 }
1654 1654
1655 int 1655 int
1656 xfs_remove( 1656 xfs_remove(
1657 xfs_inode_t *dp, 1657 xfs_inode_t *dp,
1658 struct xfs_name *name, 1658 struct xfs_name *name,
1659 xfs_inode_t *ip) 1659 xfs_inode_t *ip)
1660 { 1660 {
1661 xfs_mount_t *mp = dp->i_mount; 1661 xfs_mount_t *mp = dp->i_mount;
1662 xfs_trans_t *tp = NULL; 1662 xfs_trans_t *tp = NULL;
1663 int is_dir = S_ISDIR(ip->i_d.di_mode); 1663 int is_dir = S_ISDIR(ip->i_d.di_mode);
1664 int error = 0; 1664 int error = 0;
1665 xfs_bmap_free_t free_list; 1665 xfs_bmap_free_t free_list;
1666 xfs_fsblock_t first_block; 1666 xfs_fsblock_t first_block;
1667 int cancel_flags; 1667 int cancel_flags;
1668 int committed; 1668 int committed;
1669 int link_zero; 1669 int link_zero;
1670 uint resblks; 1670 uint resblks;
1671 uint log_count; 1671 uint log_count;
1672 1672
1673 trace_xfs_remove(dp, name); 1673 trace_xfs_remove(dp, name);
1674 1674
1675 if (XFS_FORCED_SHUTDOWN(mp)) 1675 if (XFS_FORCED_SHUTDOWN(mp))
1676 return XFS_ERROR(EIO); 1676 return XFS_ERROR(EIO);
1677 1677
1678 error = xfs_qm_dqattach(dp, 0); 1678 error = xfs_qm_dqattach(dp, 0);
1679 if (error) 1679 if (error)
1680 goto std_return; 1680 goto std_return;
1681 1681
1682 error = xfs_qm_dqattach(ip, 0); 1682 error = xfs_qm_dqattach(ip, 0);
1683 if (error) 1683 if (error)
1684 goto std_return; 1684 goto std_return;
1685 1685
1686 if (is_dir) { 1686 if (is_dir) {
1687 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR); 1687 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1688 log_count = XFS_DEFAULT_LOG_COUNT; 1688 log_count = XFS_DEFAULT_LOG_COUNT;
1689 } else { 1689 } else {
1690 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE); 1690 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1691 log_count = XFS_REMOVE_LOG_COUNT; 1691 log_count = XFS_REMOVE_LOG_COUNT;
1692 } 1692 }
1693 cancel_flags = XFS_TRANS_RELEASE_LOG_RES; 1693 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1694 1694
1695 /* 1695 /*
1696 * We try to get the real space reservation first, 1696 * We try to get the real space reservation first,
1697 * allowing for directory btree deletion(s) implying 1697 * allowing for directory btree deletion(s) implying
1698 * possible bmap insert(s). If we can't get the space 1698 * possible bmap insert(s). If we can't get the space
1699 * reservation then we use 0 instead, and avoid the bmap 1699 * reservation then we use 0 instead, and avoid the bmap
1700 * btree insert(s) in the directory code by, if the bmap 1700 * btree insert(s) in the directory code by, if the bmap
1701 * insert tries to happen, instead trimming the LAST 1701 * insert tries to happen, instead trimming the LAST
1702 * block from the directory. 1702 * block from the directory.
1703 */ 1703 */
1704 resblks = XFS_REMOVE_SPACE_RES(mp); 1704 resblks = XFS_REMOVE_SPACE_RES(mp);
1705 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0, 1705 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1706 XFS_TRANS_PERM_LOG_RES, log_count); 1706 XFS_TRANS_PERM_LOG_RES, log_count);
1707 if (error == ENOSPC) { 1707 if (error == ENOSPC) {
1708 resblks = 0; 1708 resblks = 0;
1709 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0, 1709 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1710 XFS_TRANS_PERM_LOG_RES, log_count); 1710 XFS_TRANS_PERM_LOG_RES, log_count);
1711 } 1711 }
1712 if (error) { 1712 if (error) {
1713 ASSERT(error != ENOSPC); 1713 ASSERT(error != ENOSPC);
1714 cancel_flags = 0; 1714 cancel_flags = 0;
1715 goto out_trans_cancel; 1715 goto out_trans_cancel;
1716 } 1716 }
1717 1717
1718 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL); 1718 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1719 1719
1720 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL); 1720 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
1721 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL); 1721 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
1722 1722
1723 /* 1723 /*
1724 * If we're removing a directory perform some additional validation. 1724 * If we're removing a directory perform some additional validation.
1725 */ 1725 */
1726 if (is_dir) { 1726 if (is_dir) {
1727 ASSERT(ip->i_d.di_nlink >= 2); 1727 ASSERT(ip->i_d.di_nlink >= 2);
1728 if (ip->i_d.di_nlink != 2) { 1728 if (ip->i_d.di_nlink != 2) {
1729 error = XFS_ERROR(ENOTEMPTY); 1729 error = XFS_ERROR(ENOTEMPTY);
1730 goto out_trans_cancel; 1730 goto out_trans_cancel;
1731 } 1731 }
1732 if (!xfs_dir_isempty(ip)) { 1732 if (!xfs_dir_isempty(ip)) {
1733 error = XFS_ERROR(ENOTEMPTY); 1733 error = XFS_ERROR(ENOTEMPTY);
1734 goto out_trans_cancel; 1734 goto out_trans_cancel;
1735 } 1735 }
1736 } 1736 }
1737 1737
1738 xfs_bmap_init(&free_list, &first_block); 1738 xfs_bmap_init(&free_list, &first_block);
1739 error = xfs_dir_removename(tp, dp, name, ip->i_ino, 1739 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1740 &first_block, &free_list, resblks); 1740 &first_block, &free_list, resblks);
1741 if (error) { 1741 if (error) {
1742 ASSERT(error != ENOENT); 1742 ASSERT(error != ENOENT);
1743 goto out_bmap_cancel; 1743 goto out_bmap_cancel;
1744 } 1744 }
1745 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 1745 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1746 1746
1747 if (is_dir) { 1747 if (is_dir) {
1748 /* 1748 /*
1749 * Drop the link from ip's "..". 1749 * Drop the link from ip's "..".
1750 */ 1750 */
1751 error = xfs_droplink(tp, dp); 1751 error = xfs_droplink(tp, dp);
1752 if (error) 1752 if (error)
1753 goto out_bmap_cancel; 1753 goto out_bmap_cancel;
1754 1754
1755 /* 1755 /*
1756 * Drop the "." link from ip to self. 1756 * Drop the "." link from ip to self.
1757 */ 1757 */
1758 error = xfs_droplink(tp, ip); 1758 error = xfs_droplink(tp, ip);
1759 if (error) 1759 if (error)
1760 goto out_bmap_cancel; 1760 goto out_bmap_cancel;
1761 } else { 1761 } else {
1762 /* 1762 /*
1763 * When removing a non-directory we need to log the parent 1763 * When removing a non-directory we need to log the parent
1764 * inode here. For a directory this is done implicitly 1764 * inode here. For a directory this is done implicitly
1765 * by the xfs_droplink call for the ".." entry. 1765 * by the xfs_droplink call for the ".." entry.
1766 */ 1766 */
1767 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); 1767 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1768 } 1768 }
1769 1769
1770 /* 1770 /*
1771 * Drop the link from dp to ip. 1771 * Drop the link from dp to ip.
1772 */ 1772 */
1773 error = xfs_droplink(tp, ip); 1773 error = xfs_droplink(tp, ip);
1774 if (error) 1774 if (error)
1775 goto out_bmap_cancel; 1775 goto out_bmap_cancel;
1776 1776
1777 /* 1777 /*
1778 * Determine if this is the last link while 1778 * Determine if this is the last link while
1779 * we are in the transaction. 1779 * we are in the transaction.
1780 */ 1780 */
1781 link_zero = (ip->i_d.di_nlink == 0); 1781 link_zero = (ip->i_d.di_nlink == 0);
1782 1782
1783 /* 1783 /*
1784 * If this is a synchronous mount, make sure that the 1784 * If this is a synchronous mount, make sure that the
1785 * remove transaction goes to disk before returning to 1785 * remove transaction goes to disk before returning to
1786 * the user. 1786 * the user.
1787 */ 1787 */
1788 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) 1788 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1789 xfs_trans_set_sync(tp); 1789 xfs_trans_set_sync(tp);
1790 1790
1791 error = xfs_bmap_finish(&tp, &free_list, &committed); 1791 error = xfs_bmap_finish(&tp, &free_list, &committed);
1792 if (error) 1792 if (error)
1793 goto out_bmap_cancel; 1793 goto out_bmap_cancel;
1794 1794
1795 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 1795 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1796 if (error) 1796 if (error)
1797 goto std_return; 1797 goto std_return;
1798 1798
1799 /* 1799 /*
1800 * If we are using filestreams, kill the stream association. 1800 * If we are using filestreams, kill the stream association.
1801 * If the file is still open it may get a new one but that 1801 * If the file is still open it may get a new one but that
1802 * will get killed on last close in xfs_close() so we don't 1802 * will get killed on last close in xfs_close() so we don't
1803 * have to worry about that. 1803 * have to worry about that.
1804 */ 1804 */
1805 if (!is_dir && link_zero && xfs_inode_is_filestream(ip)) 1805 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
1806 xfs_filestream_deassociate(ip); 1806 xfs_filestream_deassociate(ip);
1807 1807
1808 return 0; 1808 return 0;
1809 1809
1810 out_bmap_cancel: 1810 out_bmap_cancel:
1811 xfs_bmap_cancel(&free_list); 1811 xfs_bmap_cancel(&free_list);
1812 cancel_flags |= XFS_TRANS_ABORT; 1812 cancel_flags |= XFS_TRANS_ABORT;
1813 out_trans_cancel: 1813 out_trans_cancel:
1814 xfs_trans_cancel(tp, cancel_flags); 1814 xfs_trans_cancel(tp, cancel_flags);
1815 std_return: 1815 std_return:
1816 return error; 1816 return error;
1817 } 1817 }
1818 1818
1819 int 1819 int
1820 xfs_link( 1820 xfs_link(
1821 xfs_inode_t *tdp, 1821 xfs_inode_t *tdp,
1822 xfs_inode_t *sip, 1822 xfs_inode_t *sip,
1823 struct xfs_name *target_name) 1823 struct xfs_name *target_name)
1824 { 1824 {
1825 xfs_mount_t *mp = tdp->i_mount; 1825 xfs_mount_t *mp = tdp->i_mount;
1826 xfs_trans_t *tp; 1826 xfs_trans_t *tp;
1827 int error; 1827 int error;
1828 xfs_bmap_free_t free_list; 1828 xfs_bmap_free_t free_list;
1829 xfs_fsblock_t first_block; 1829 xfs_fsblock_t first_block;
1830 int cancel_flags; 1830 int cancel_flags;
1831 int committed; 1831 int committed;
1832 int resblks; 1832 int resblks;
1833 1833
1834 trace_xfs_link(tdp, target_name); 1834 trace_xfs_link(tdp, target_name);
1835 1835
1836 ASSERT(!S_ISDIR(sip->i_d.di_mode)); 1836 ASSERT(!S_ISDIR(sip->i_d.di_mode));
1837 1837
1838 if (XFS_FORCED_SHUTDOWN(mp)) 1838 if (XFS_FORCED_SHUTDOWN(mp))
1839 return XFS_ERROR(EIO); 1839 return XFS_ERROR(EIO);
1840 1840
1841 error = xfs_qm_dqattach(sip, 0); 1841 error = xfs_qm_dqattach(sip, 0);
1842 if (error) 1842 if (error)
1843 goto std_return; 1843 goto std_return;
1844 1844
1845 error = xfs_qm_dqattach(tdp, 0); 1845 error = xfs_qm_dqattach(tdp, 0);
1846 if (error) 1846 if (error)
1847 goto std_return; 1847 goto std_return;
1848 1848
1849 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK); 1849 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1850 cancel_flags = XFS_TRANS_RELEASE_LOG_RES; 1850 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1851 resblks = XFS_LINK_SPACE_RES(mp, target_name->len); 1851 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1852 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0, 1852 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1853 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT); 1853 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1854 if (error == ENOSPC) { 1854 if (error == ENOSPC) {
1855 resblks = 0; 1855 resblks = 0;
1856 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0, 1856 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1857 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT); 1857 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1858 } 1858 }
1859 if (error) { 1859 if (error) {
1860 cancel_flags = 0; 1860 cancel_flags = 0;
1861 goto error_return; 1861 goto error_return;
1862 } 1862 }
1863 1863
1864 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL); 1864 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1865 1865
1866 xfs_trans_ijoin_ref(tp, sip, XFS_ILOCK_EXCL); 1866 xfs_trans_ijoin_ref(tp, sip, XFS_ILOCK_EXCL);
1867 xfs_trans_ijoin_ref(tp, tdp, XFS_ILOCK_EXCL); 1867 xfs_trans_ijoin_ref(tp, tdp, XFS_ILOCK_EXCL);
1868 1868
1869 /* 1869 /*
1870 * If the source has too many links, we can't make any more to it. 1870 * If the source has too many links, we can't make any more to it.
1871 */ 1871 */
1872 if (sip->i_d.di_nlink >= XFS_MAXLINK) { 1872 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
1873 error = XFS_ERROR(EMLINK); 1873 error = XFS_ERROR(EMLINK);
1874 goto error_return; 1874 goto error_return;
1875 } 1875 }
1876 1876
1877 /* 1877 /*
1878 * If we are using project inheritance, we only allow hard link 1878 * If we are using project inheritance, we only allow hard link
1879 * creation in our tree when the project IDs are the same; else 1879 * creation in our tree when the project IDs are the same; else
1880 * the tree quota mechanism could be circumvented. 1880 * the tree quota mechanism could be circumvented.
1881 */ 1881 */
1882 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && 1882 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1883 (tdp->i_d.di_projid != sip->i_d.di_projid))) { 1883 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
1884 error = XFS_ERROR(EXDEV); 1884 error = XFS_ERROR(EXDEV);
1885 goto error_return; 1885 goto error_return;
1886 } 1886 }
1887 1887
1888 error = xfs_dir_canenter(tp, tdp, target_name, resblks); 1888 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1889 if (error) 1889 if (error)
1890 goto error_return; 1890 goto error_return;
1891 1891
1892 xfs_bmap_init(&free_list, &first_block); 1892 xfs_bmap_init(&free_list, &first_block);
1893 1893
1894 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino, 1894 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1895 &first_block, &free_list, resblks); 1895 &first_block, &free_list, resblks);
1896 if (error) 1896 if (error)
1897 goto abort_return; 1897 goto abort_return;
1898 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 1898 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1899 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE); 1899 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1900 1900
1901 error = xfs_bumplink(tp, sip); 1901 error = xfs_bumplink(tp, sip);
1902 if (error) 1902 if (error)
1903 goto abort_return; 1903 goto abort_return;
1904 1904
1905 /* 1905 /*
1906 * If this is a synchronous mount, make sure that the 1906 * If this is a synchronous mount, make sure that the
1907 * link transaction goes to disk before returning to 1907 * link transaction goes to disk before returning to
1908 * the user. 1908 * the user.
1909 */ 1909 */
1910 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { 1910 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1911 xfs_trans_set_sync(tp); 1911 xfs_trans_set_sync(tp);
1912 } 1912 }
1913 1913
1914 error = xfs_bmap_finish (&tp, &free_list, &committed); 1914 error = xfs_bmap_finish (&tp, &free_list, &committed);
1915 if (error) { 1915 if (error) {
1916 xfs_bmap_cancel(&free_list); 1916 xfs_bmap_cancel(&free_list);
1917 goto abort_return; 1917 goto abort_return;
1918 } 1918 }
1919 1919
1920 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 1920 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1921 1921
1922 abort_return: 1922 abort_return:
1923 cancel_flags |= XFS_TRANS_ABORT; 1923 cancel_flags |= XFS_TRANS_ABORT;
1924 error_return: 1924 error_return:
1925 xfs_trans_cancel(tp, cancel_flags); 1925 xfs_trans_cancel(tp, cancel_flags);
1926 std_return: 1926 std_return:
1927 return error; 1927 return error;
1928 } 1928 }
1929 1929
1930 int 1930 int
1931 xfs_symlink( 1931 xfs_symlink(
1932 xfs_inode_t *dp, 1932 xfs_inode_t *dp,
1933 struct xfs_name *link_name, 1933 struct xfs_name *link_name,
1934 const char *target_path, 1934 const char *target_path,
1935 mode_t mode, 1935 mode_t mode,
1936 xfs_inode_t **ipp, 1936 xfs_inode_t **ipp,
1937 cred_t *credp) 1937 cred_t *credp)
1938 { 1938 {
1939 xfs_mount_t *mp = dp->i_mount; 1939 xfs_mount_t *mp = dp->i_mount;
1940 xfs_trans_t *tp; 1940 xfs_trans_t *tp;
1941 xfs_inode_t *ip; 1941 xfs_inode_t *ip;
1942 int error; 1942 int error;
1943 int pathlen; 1943 int pathlen;
1944 xfs_bmap_free_t free_list; 1944 xfs_bmap_free_t free_list;
1945 xfs_fsblock_t first_block; 1945 xfs_fsblock_t first_block;
1946 boolean_t unlock_dp_on_error = B_FALSE; 1946 boolean_t unlock_dp_on_error = B_FALSE;
1947 uint cancel_flags; 1947 uint cancel_flags;
1948 int committed; 1948 int committed;
1949 xfs_fileoff_t first_fsb; 1949 xfs_fileoff_t first_fsb;
1950 xfs_filblks_t fs_blocks; 1950 xfs_filblks_t fs_blocks;
1951 int nmaps; 1951 int nmaps;
1952 xfs_bmbt_irec_t mval[SYMLINK_MAPS]; 1952 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1953 xfs_daddr_t d; 1953 xfs_daddr_t d;
1954 const char *cur_chunk; 1954 const char *cur_chunk;
1955 int byte_cnt; 1955 int byte_cnt;
1956 int n; 1956 int n;
1957 xfs_buf_t *bp; 1957 xfs_buf_t *bp;
1958 xfs_prid_t prid; 1958 xfs_prid_t prid;
1959 struct xfs_dquot *udqp, *gdqp; 1959 struct xfs_dquot *udqp, *gdqp;
1960 uint resblks; 1960 uint resblks;
1961 1961
1962 *ipp = NULL; 1962 *ipp = NULL;
1963 error = 0; 1963 error = 0;
1964 ip = NULL; 1964 ip = NULL;
1965 tp = NULL; 1965 tp = NULL;
1966 1966
1967 trace_xfs_symlink(dp, link_name); 1967 trace_xfs_symlink(dp, link_name);
1968 1968
1969 if (XFS_FORCED_SHUTDOWN(mp)) 1969 if (XFS_FORCED_SHUTDOWN(mp))
1970 return XFS_ERROR(EIO); 1970 return XFS_ERROR(EIO);
1971 1971
1972 /* 1972 /*
1973 * Check component lengths of the target path name. 1973 * Check component lengths of the target path name.
1974 */ 1974 */
1975 pathlen = strlen(target_path); 1975 pathlen = strlen(target_path);
1976 if (pathlen >= MAXPATHLEN) /* total string too long */ 1976 if (pathlen >= MAXPATHLEN) /* total string too long */
1977 return XFS_ERROR(ENAMETOOLONG); 1977 return XFS_ERROR(ENAMETOOLONG);
1978 1978
1979 udqp = gdqp = NULL; 1979 udqp = gdqp = NULL;
1980 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) 1980 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1981 prid = dp->i_d.di_projid; 1981 prid = dp->i_d.di_projid;
1982 else 1982 else
1983 prid = (xfs_prid_t)dfltprid; 1983 prid = (xfs_prid_t)dfltprid;
1984 1984
1985 /* 1985 /*
1986 * Make sure that we have allocated dquot(s) on disk. 1986 * Make sure that we have allocated dquot(s) on disk.
1987 */ 1987 */
1988 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid, 1988 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1989 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp); 1989 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1990 if (error) 1990 if (error)
1991 goto std_return; 1991 goto std_return;
1992 1992
1993 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK); 1993 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
1994 cancel_flags = XFS_TRANS_RELEASE_LOG_RES; 1994 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1995 /* 1995 /*
1996 * The symlink will fit into the inode data fork? 1996 * The symlink will fit into the inode data fork?
1997 * There can't be any attributes so we get the whole variable part. 1997 * There can't be any attributes so we get the whole variable part.
1998 */ 1998 */
1999 if (pathlen <= XFS_LITINO(mp)) 1999 if (pathlen <= XFS_LITINO(mp))
2000 fs_blocks = 0; 2000 fs_blocks = 0;
2001 else 2001 else
2002 fs_blocks = XFS_B_TO_FSB(mp, pathlen); 2002 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2003 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks); 2003 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2004 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0, 2004 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2005 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); 2005 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2006 if (error == ENOSPC && fs_blocks == 0) { 2006 if (error == ENOSPC && fs_blocks == 0) {
2007 resblks = 0; 2007 resblks = 0;
2008 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0, 2008 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2009 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); 2009 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2010 } 2010 }
2011 if (error) { 2011 if (error) {
2012 cancel_flags = 0; 2012 cancel_flags = 0;
2013 goto error_return; 2013 goto error_return;
2014 } 2014 }
2015 2015
2016 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); 2016 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2017 unlock_dp_on_error = B_TRUE; 2017 unlock_dp_on_error = B_TRUE;
2018 2018
2019 /* 2019 /*
2020 * Check whether the directory allows new symlinks or not. 2020 * Check whether the directory allows new symlinks or not.
2021 */ 2021 */
2022 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) { 2022 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2023 error = XFS_ERROR(EPERM); 2023 error = XFS_ERROR(EPERM);
2024 goto error_return; 2024 goto error_return;
2025 } 2025 }
2026 2026
2027 /* 2027 /*
2028 * Reserve disk quota : blocks and inode. 2028 * Reserve disk quota : blocks and inode.
2029 */ 2029 */
2030 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0); 2030 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
2031 if (error) 2031 if (error)
2032 goto error_return; 2032 goto error_return;
2033 2033
2034 /* 2034 /*
2035 * Check for ability to enter directory entry, if no space reserved. 2035 * Check for ability to enter directory entry, if no space reserved.
2036 */ 2036 */
2037 error = xfs_dir_canenter(tp, dp, link_name, resblks); 2037 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2038 if (error) 2038 if (error)
2039 goto error_return; 2039 goto error_return;
2040 /* 2040 /*
2041 * Initialize the bmap freelist prior to calling either 2041 * Initialize the bmap freelist prior to calling either
2042 * bmapi or the directory create code. 2042 * bmapi or the directory create code.
2043 */ 2043 */
2044 xfs_bmap_init(&free_list, &first_block); 2044 xfs_bmap_init(&free_list, &first_block);
2045 2045
2046 /* 2046 /*
2047 * Allocate an inode for the symlink. 2047 * Allocate an inode for the symlink.
2048 */ 2048 */
2049 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 2049 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
2050 1, 0, credp, prid, resblks > 0, &ip, NULL); 2050 1, 0, credp, prid, resblks > 0, &ip, NULL);
2051 if (error) { 2051 if (error) {
2052 if (error == ENOSPC) 2052 if (error == ENOSPC)
2053 goto error_return; 2053 goto error_return;
2054 goto error1; 2054 goto error1;
2055 } 2055 }
2056 2056
2057 /* 2057 /*
2058 * An error after we've joined dp to the transaction will result in the 2058 * An error after we've joined dp to the transaction will result in the
2059 * transaction cancel unlocking dp so don't do it explicitly in the 2059 * transaction cancel unlocking dp so don't do it explicitly in the
2060 * error path. 2060 * error path.
2061 */ 2061 */
2062 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL); 2062 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
2063 unlock_dp_on_error = B_FALSE; 2063 unlock_dp_on_error = B_FALSE;
2064 2064
2065 /* 2065 /*
2066 * Also attach the dquot(s) to it, if applicable. 2066 * Also attach the dquot(s) to it, if applicable.
2067 */ 2067 */
2068 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp); 2068 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
2069 2069
2070 if (resblks) 2070 if (resblks)
2071 resblks -= XFS_IALLOC_SPACE_RES(mp); 2071 resblks -= XFS_IALLOC_SPACE_RES(mp);
2072 /* 2072 /*
2073 * If the symlink will fit into the inode, write it inline. 2073 * If the symlink will fit into the inode, write it inline.
2074 */ 2074 */
2075 if (pathlen <= XFS_IFORK_DSIZE(ip)) { 2075 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2076 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK); 2076 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2077 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen); 2077 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2078 ip->i_d.di_size = pathlen; 2078 ip->i_d.di_size = pathlen;
2079 2079
2080 /* 2080 /*
2081 * The inode was initially created in extent format. 2081 * The inode was initially created in extent format.
2082 */ 2082 */
2083 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT); 2083 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2084 ip->i_df.if_flags |= XFS_IFINLINE; 2084 ip->i_df.if_flags |= XFS_IFINLINE;
2085 2085
2086 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL; 2086 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2087 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE); 2087 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2088 2088
2089 } else { 2089 } else {
2090 first_fsb = 0; 2090 first_fsb = 0;
2091 nmaps = SYMLINK_MAPS; 2091 nmaps = SYMLINK_MAPS;
2092 2092
2093 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks, 2093 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2094 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, 2094 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2095 &first_block, resblks, mval, &nmaps, 2095 &first_block, resblks, mval, &nmaps,
2096 &free_list); 2096 &free_list);
2097 if (error) { 2097 if (error) {
2098 goto error1; 2098 goto error1;
2099 } 2099 }
2100 2100
2101 if (resblks) 2101 if (resblks)
2102 resblks -= fs_blocks; 2102 resblks -= fs_blocks;
2103 ip->i_d.di_size = pathlen; 2103 ip->i_d.di_size = pathlen;
2104 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 2104 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2105 2105
2106 cur_chunk = target_path; 2106 cur_chunk = target_path;
2107 for (n = 0; n < nmaps; n++) { 2107 for (n = 0; n < nmaps; n++) {
2108 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock); 2108 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2109 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount); 2109 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2110 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, 2110 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2111 BTOBB(byte_cnt), 0); 2111 BTOBB(byte_cnt), 0);
2112 ASSERT(bp && !XFS_BUF_GETERROR(bp)); 2112 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2113 if (pathlen < byte_cnt) { 2113 if (pathlen < byte_cnt) {
2114 byte_cnt = pathlen; 2114 byte_cnt = pathlen;
2115 } 2115 }
2116 pathlen -= byte_cnt; 2116 pathlen -= byte_cnt;
2117 2117
2118 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt); 2118 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2119 cur_chunk += byte_cnt; 2119 cur_chunk += byte_cnt;
2120 2120
2121 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1); 2121 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2122 } 2122 }
2123 } 2123 }
2124 2124
2125 /* 2125 /*
2126 * Create the directory entry for the symlink. 2126 * Create the directory entry for the symlink.
2127 */ 2127 */
2128 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, 2128 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2129 &first_block, &free_list, resblks); 2129 &first_block, &free_list, resblks);
2130 if (error) 2130 if (error)
2131 goto error1; 2131 goto error1;
2132 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 2132 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2133 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); 2133 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2134 2134
2135 /* 2135 /*
2136 * If this is a synchronous mount, make sure that the 2136 * If this is a synchronous mount, make sure that the
2137 * symlink transaction goes to disk before returning to 2137 * symlink transaction goes to disk before returning to
2138 * the user. 2138 * the user.
2139 */ 2139 */
2140 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { 2140 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2141 xfs_trans_set_sync(tp); 2141 xfs_trans_set_sync(tp);
2142 } 2142 }
2143 2143
2144 /* 2144 /*
2145 * xfs_trans_commit normally decrements the vnode ref count 2145 * xfs_trans_commit normally decrements the vnode ref count
2146 * when it unlocks the inode. Since we want to return the 2146 * when it unlocks the inode. Since we want to return the
2147 * vnode to the caller, we bump the vnode ref count now. 2147 * vnode to the caller, we bump the vnode ref count now.
2148 */ 2148 */
2149 IHOLD(ip); 2149 IHOLD(ip);
2150 2150
2151 error = xfs_bmap_finish(&tp, &free_list, &committed); 2151 error = xfs_bmap_finish(&tp, &free_list, &committed);
2152 if (error) { 2152 if (error) {
2153 goto error2; 2153 goto error2;
2154 } 2154 }
2155 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 2155 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2156 xfs_qm_dqrele(udqp); 2156 xfs_qm_dqrele(udqp);
2157 xfs_qm_dqrele(gdqp); 2157 xfs_qm_dqrele(gdqp);
2158 2158
2159 *ipp = ip; 2159 *ipp = ip;
2160 return 0; 2160 return 0;
2161 2161
2162 error2: 2162 error2:
2163 IRELE(ip); 2163 IRELE(ip);
2164 error1: 2164 error1:
2165 xfs_bmap_cancel(&free_list); 2165 xfs_bmap_cancel(&free_list);
2166 cancel_flags |= XFS_TRANS_ABORT; 2166 cancel_flags |= XFS_TRANS_ABORT;
2167 error_return: 2167 error_return:
2168 xfs_trans_cancel(tp, cancel_flags); 2168 xfs_trans_cancel(tp, cancel_flags);
2169 xfs_qm_dqrele(udqp); 2169 xfs_qm_dqrele(udqp);
2170 xfs_qm_dqrele(gdqp); 2170 xfs_qm_dqrele(gdqp);
2171 2171
2172 if (unlock_dp_on_error) 2172 if (unlock_dp_on_error)
2173 xfs_iunlock(dp, XFS_ILOCK_EXCL); 2173 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2174 std_return: 2174 std_return:
2175 return error; 2175 return error;
2176 } 2176 }
2177 2177
2178 int 2178 int
2179 xfs_set_dmattrs( 2179 xfs_set_dmattrs(
2180 xfs_inode_t *ip, 2180 xfs_inode_t *ip,
2181 u_int evmask, 2181 u_int evmask,
2182 u_int16_t state) 2182 u_int16_t state)
2183 { 2183 {
2184 xfs_mount_t *mp = ip->i_mount; 2184 xfs_mount_t *mp = ip->i_mount;
2185 xfs_trans_t *tp; 2185 xfs_trans_t *tp;
2186 int error; 2186 int error;
2187 2187
2188 if (!capable(CAP_SYS_ADMIN)) 2188 if (!capable(CAP_SYS_ADMIN))
2189 return XFS_ERROR(EPERM); 2189 return XFS_ERROR(EPERM);
2190 2190
2191 if (XFS_FORCED_SHUTDOWN(mp)) 2191 if (XFS_FORCED_SHUTDOWN(mp))
2192 return XFS_ERROR(EIO); 2192 return XFS_ERROR(EIO);
2193 2193
2194 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS); 2194 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2195 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0); 2195 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2196 if (error) { 2196 if (error) {
2197 xfs_trans_cancel(tp, 0); 2197 xfs_trans_cancel(tp, 0);
2198 return error; 2198 return error;
2199 } 2199 }
2200 xfs_ilock(ip, XFS_ILOCK_EXCL); 2200 xfs_ilock(ip, XFS_ILOCK_EXCL);
2201 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL); 2201 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
2202 2202
2203 ip->i_d.di_dmevmask = evmask; 2203 ip->i_d.di_dmevmask = evmask;
2204 ip->i_d.di_dmstate = state; 2204 ip->i_d.di_dmstate = state;
2205 2205
2206 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 2206 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2207 error = xfs_trans_commit(tp, 0); 2207 error = xfs_trans_commit(tp, 0);
2208 2208
2209 return error; 2209 return error;
2210 } 2210 }
2211 2211
2212 /* 2212 /*
2213 * xfs_alloc_file_space() 2213 * xfs_alloc_file_space()
2214 * This routine allocates disk space for the given file. 2214 * This routine allocates disk space for the given file.
2215 * 2215 *
2216 * If alloc_type == 0, this request is for an ALLOCSP type 2216 * If alloc_type == 0, this request is for an ALLOCSP type
2217 * request which will change the file size. In this case, no 2217 * request which will change the file size. In this case, no
2218 * DMAPI event will be generated by the call. A TRUNCATE event 2218 * DMAPI event will be generated by the call. A TRUNCATE event
2219 * will be generated later by xfs_setattr. 2219 * will be generated later by xfs_setattr.
2220 * 2220 *
2221 * If alloc_type != 0, this request is for a RESVSP type 2221 * If alloc_type != 0, this request is for a RESVSP type
2222 * request, and a DMAPI DM_EVENT_WRITE will be generated if the 2222 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2223 * lower block boundary byte address is less than the file's 2223 * lower block boundary byte address is less than the file's
2224 * length. 2224 * length.
2225 * 2225 *
2226 * RETURNS: 2226 * RETURNS:
2227 * 0 on success 2227 * 0 on success
2228 * errno on error 2228 * errno on error
2229 * 2229 *
2230 */ 2230 */
2231 STATIC int 2231 STATIC int
2232 xfs_alloc_file_space( 2232 xfs_alloc_file_space(
2233 xfs_inode_t *ip, 2233 xfs_inode_t *ip,
2234 xfs_off_t offset, 2234 xfs_off_t offset,
2235 xfs_off_t len, 2235 xfs_off_t len,
2236 int alloc_type, 2236 int alloc_type,
2237 int attr_flags) 2237 int attr_flags)
2238 { 2238 {
2239 xfs_mount_t *mp = ip->i_mount; 2239 xfs_mount_t *mp = ip->i_mount;
2240 xfs_off_t count; 2240 xfs_off_t count;
2241 xfs_filblks_t allocated_fsb; 2241 xfs_filblks_t allocated_fsb;
2242 xfs_filblks_t allocatesize_fsb; 2242 xfs_filblks_t allocatesize_fsb;
2243 xfs_extlen_t extsz, temp; 2243 xfs_extlen_t extsz, temp;
2244 xfs_fileoff_t startoffset_fsb; 2244 xfs_fileoff_t startoffset_fsb;
2245 xfs_fsblock_t firstfsb; 2245 xfs_fsblock_t firstfsb;
2246 int nimaps; 2246 int nimaps;
2247 int bmapi_flag; 2247 int bmapi_flag;
2248 int quota_flag; 2248 int quota_flag;
2249 int rt; 2249 int rt;
2250 xfs_trans_t *tp; 2250 xfs_trans_t *tp;
2251 xfs_bmbt_irec_t imaps[1], *imapp; 2251 xfs_bmbt_irec_t imaps[1], *imapp;
2252 xfs_bmap_free_t free_list; 2252 xfs_bmap_free_t free_list;
2253 uint qblocks, resblks, resrtextents; 2253 uint qblocks, resblks, resrtextents;
2254 int committed; 2254 int committed;
2255 int error; 2255 int error;
2256 2256
2257 trace_xfs_alloc_file_space(ip); 2257 trace_xfs_alloc_file_space(ip);
2258 2258
2259 if (XFS_FORCED_SHUTDOWN(mp)) 2259 if (XFS_FORCED_SHUTDOWN(mp))
2260 return XFS_ERROR(EIO); 2260 return XFS_ERROR(EIO);
2261 2261
2262 error = xfs_qm_dqattach(ip, 0); 2262 error = xfs_qm_dqattach(ip, 0);
2263 if (error) 2263 if (error)
2264 return error; 2264 return error;
2265 2265
2266 if (len <= 0) 2266 if (len <= 0)
2267 return XFS_ERROR(EINVAL); 2267 return XFS_ERROR(EINVAL);
2268 2268
2269 rt = XFS_IS_REALTIME_INODE(ip); 2269 rt = XFS_IS_REALTIME_INODE(ip);
2270 extsz = xfs_get_extsz_hint(ip); 2270 extsz = xfs_get_extsz_hint(ip);
2271 2271
2272 count = len; 2272 count = len;
2273 imapp = &imaps[0]; 2273 imapp = &imaps[0];
2274 nimaps = 1; 2274 nimaps = 1;
2275 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0); 2275 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
2276 startoffset_fsb = XFS_B_TO_FSBT(mp, offset); 2276 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2277 allocatesize_fsb = XFS_B_TO_FSB(mp, count); 2277 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2278 2278
2279 /* 2279 /*
2280 * Allocate file space until done or until there is an error 2280 * Allocate file space until done or until there is an error
2281 */ 2281 */
2282 while (allocatesize_fsb && !error) { 2282 while (allocatesize_fsb && !error) {
2283 xfs_fileoff_t s, e; 2283 xfs_fileoff_t s, e;
2284 2284
2285 /* 2285 /*
2286 * Determine space reservations for data/realtime. 2286 * Determine space reservations for data/realtime.
2287 */ 2287 */
2288 if (unlikely(extsz)) { 2288 if (unlikely(extsz)) {
2289 s = startoffset_fsb; 2289 s = startoffset_fsb;
2290 do_div(s, extsz); 2290 do_div(s, extsz);
2291 s *= extsz; 2291 s *= extsz;
2292 e = startoffset_fsb + allocatesize_fsb; 2292 e = startoffset_fsb + allocatesize_fsb;
2293 if ((temp = do_mod(startoffset_fsb, extsz))) 2293 if ((temp = do_mod(startoffset_fsb, extsz)))
2294 e += temp; 2294 e += temp;
2295 if ((temp = do_mod(e, extsz))) 2295 if ((temp = do_mod(e, extsz)))
2296 e += extsz - temp; 2296 e += extsz - temp;
2297 } else { 2297 } else {
2298 s = 0; 2298 s = 0;
2299 e = allocatesize_fsb; 2299 e = allocatesize_fsb;
2300 } 2300 }
2301 2301
2302 /*
2303 * The transaction reservation is limited to a 32-bit block
2304 * count, hence we need to limit the number of blocks we are
2305 * trying to reserve to avoid an overflow. We can't allocate
2306 * more than @nimaps extents, and an extent is limited on disk
2307 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
2308 */
2309 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
2302 if (unlikely(rt)) { 2310 if (unlikely(rt)) {
2303 resrtextents = qblocks = (uint)(e - s); 2311 resrtextents = qblocks = resblks;
2304 resrtextents /= mp->m_sb.sb_rextsize; 2312 resrtextents /= mp->m_sb.sb_rextsize;
2305 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); 2313 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2306 quota_flag = XFS_QMOPT_RES_RTBLKS; 2314 quota_flag = XFS_QMOPT_RES_RTBLKS;
2307 } else { 2315 } else {
2308 resrtextents = 0; 2316 resrtextents = 0;
2309 resblks = qblocks = \ 2317 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
2310 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2311 quota_flag = XFS_QMOPT_RES_REGBLKS; 2318 quota_flag = XFS_QMOPT_RES_REGBLKS;
2312 } 2319 }
2313 2320
2314 /* 2321 /*
2315 * Allocate and setup the transaction. 2322 * Allocate and setup the transaction.
2316 */ 2323 */
2317 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); 2324 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2318 error = xfs_trans_reserve(tp, resblks, 2325 error = xfs_trans_reserve(tp, resblks,
2319 XFS_WRITE_LOG_RES(mp), resrtextents, 2326 XFS_WRITE_LOG_RES(mp), resrtextents,
2320 XFS_TRANS_PERM_LOG_RES, 2327 XFS_TRANS_PERM_LOG_RES,
2321 XFS_WRITE_LOG_COUNT); 2328 XFS_WRITE_LOG_COUNT);
2322 /* 2329 /*
2323 * Check for running out of space 2330 * Check for running out of space
2324 */ 2331 */
2325 if (error) { 2332 if (error) {
2326 /* 2333 /*
2327 * Free the transaction structure. 2334 * Free the transaction structure.
2328 */ 2335 */
2329 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp)); 2336 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2330 xfs_trans_cancel(tp, 0); 2337 xfs_trans_cancel(tp, 0);
2331 break; 2338 break;
2332 } 2339 }
2333 xfs_ilock(ip, XFS_ILOCK_EXCL); 2340 xfs_ilock(ip, XFS_ILOCK_EXCL);
2334 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 2341 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2335 0, quota_flag); 2342 0, quota_flag);
2336 if (error) 2343 if (error)
2337 goto error1; 2344 goto error1;
2338 2345
2339 xfs_trans_ijoin(tp, ip); 2346 xfs_trans_ijoin(tp, ip);
2340 2347
2341 /* 2348 /*
2342 * Issue the xfs_bmapi() call to allocate the blocks 2349 * Issue the xfs_bmapi() call to allocate the blocks
2343 */ 2350 */
2344 xfs_bmap_init(&free_list, &firstfsb); 2351 xfs_bmap_init(&free_list, &firstfsb);
2345 error = xfs_bmapi(tp, ip, startoffset_fsb, 2352 error = xfs_bmapi(tp, ip, startoffset_fsb,
2346 allocatesize_fsb, bmapi_flag, 2353 allocatesize_fsb, bmapi_flag,
2347 &firstfsb, 0, imapp, &nimaps, 2354 &firstfsb, 0, imapp, &nimaps,
2348 &free_list); 2355 &free_list);
2349 if (error) { 2356 if (error) {
2350 goto error0; 2357 goto error0;
2351 } 2358 }
2352 2359
2353 /* 2360 /*
2354 * Complete the transaction 2361 * Complete the transaction
2355 */ 2362 */
2356 error = xfs_bmap_finish(&tp, &free_list, &committed); 2363 error = xfs_bmap_finish(&tp, &free_list, &committed);
2357 if (error) { 2364 if (error) {
2358 goto error0; 2365 goto error0;
2359 } 2366 }
2360 2367
2361 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 2368 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2362 xfs_iunlock(ip, XFS_ILOCK_EXCL); 2369 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2363 if (error) { 2370 if (error) {
2364 break; 2371 break;
2365 } 2372 }
2366 2373
2367 allocated_fsb = imapp->br_blockcount; 2374 allocated_fsb = imapp->br_blockcount;
2368 2375
2369 if (nimaps == 0) { 2376 if (nimaps == 0) {
2370 error = XFS_ERROR(ENOSPC); 2377 error = XFS_ERROR(ENOSPC);
2371 break; 2378 break;
2372 } 2379 }
2373 2380
2374 startoffset_fsb += allocated_fsb; 2381 startoffset_fsb += allocated_fsb;
2375 allocatesize_fsb -= allocated_fsb; 2382 allocatesize_fsb -= allocated_fsb;
2376 } 2383 }
2377 2384
2378 return error; 2385 return error;
2379 2386
2380 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */ 2387 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
2381 xfs_bmap_cancel(&free_list); 2388 xfs_bmap_cancel(&free_list);
2382 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag); 2389 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
2383 2390
2384 error1: /* Just cancel transaction */ 2391 error1: /* Just cancel transaction */
2385 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); 2392 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2386 xfs_iunlock(ip, XFS_ILOCK_EXCL); 2393 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2387 return error; 2394 return error;
2388 } 2395 }
2389 2396
2390 /* 2397 /*
2391 * Zero file bytes between startoff and endoff inclusive. 2398 * Zero file bytes between startoff and endoff inclusive.
2392 * The iolock is held exclusive and no blocks are buffered. 2399 * The iolock is held exclusive and no blocks are buffered.
2393 * 2400 *
2394 * This function is used by xfs_free_file_space() to zero 2401 * This function is used by xfs_free_file_space() to zero
2395 * partial blocks when the range to free is not block aligned. 2402 * partial blocks when the range to free is not block aligned.
2396 * When unreserving space with boundaries that are not block 2403 * When unreserving space with boundaries that are not block
2397 * aligned we round up the start and round down the end 2404 * aligned we round up the start and round down the end
2398 * boundaries and then use this function to zero the parts of 2405 * boundaries and then use this function to zero the parts of
2399 * the blocks that got dropped during the rounding. 2406 * the blocks that got dropped during the rounding.
2400 */ 2407 */
2401 STATIC int 2408 STATIC int
2402 xfs_zero_remaining_bytes( 2409 xfs_zero_remaining_bytes(
2403 xfs_inode_t *ip, 2410 xfs_inode_t *ip,
2404 xfs_off_t startoff, 2411 xfs_off_t startoff,
2405 xfs_off_t endoff) 2412 xfs_off_t endoff)
2406 { 2413 {
2407 xfs_bmbt_irec_t imap; 2414 xfs_bmbt_irec_t imap;
2408 xfs_fileoff_t offset_fsb; 2415 xfs_fileoff_t offset_fsb;
2409 xfs_off_t lastoffset; 2416 xfs_off_t lastoffset;
2410 xfs_off_t offset; 2417 xfs_off_t offset;
2411 xfs_buf_t *bp; 2418 xfs_buf_t *bp;
2412 xfs_mount_t *mp = ip->i_mount; 2419 xfs_mount_t *mp = ip->i_mount;
2413 int nimap; 2420 int nimap;
2414 int error = 0; 2421 int error = 0;
2415 2422
2416 /* 2423 /*
2417 * Avoid doing I/O beyond eof - it's not necessary 2424 * Avoid doing I/O beyond eof - it's not necessary
2418 * since nothing can read beyond eof. The space will 2425 * since nothing can read beyond eof. The space will
2419 * be zeroed when the file is extended anyway. 2426 * be zeroed when the file is extended anyway.
2420 */ 2427 */
2421 if (startoff >= ip->i_size) 2428 if (startoff >= ip->i_size)
2422 return 0; 2429 return 0;
2423 2430
2424 if (endoff > ip->i_size) 2431 if (endoff > ip->i_size)
2425 endoff = ip->i_size; 2432 endoff = ip->i_size;
2426 2433
2427 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize, 2434 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
2428 XFS_IS_REALTIME_INODE(ip) ? 2435 XFS_IS_REALTIME_INODE(ip) ?
2429 mp->m_rtdev_targp : mp->m_ddev_targp); 2436 mp->m_rtdev_targp : mp->m_ddev_targp);
2430 if (!bp) 2437 if (!bp)
2431 return XFS_ERROR(ENOMEM); 2438 return XFS_ERROR(ENOMEM);
2432 2439
2433 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) { 2440 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2434 offset_fsb = XFS_B_TO_FSBT(mp, offset); 2441 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2435 nimap = 1; 2442 nimap = 1;
2436 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0, 2443 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
2437 NULL, 0, &imap, &nimap, NULL); 2444 NULL, 0, &imap, &nimap, NULL);
2438 if (error || nimap < 1) 2445 if (error || nimap < 1)
2439 break; 2446 break;
2440 ASSERT(imap.br_blockcount >= 1); 2447 ASSERT(imap.br_blockcount >= 1);
2441 ASSERT(imap.br_startoff == offset_fsb); 2448 ASSERT(imap.br_startoff == offset_fsb);
2442 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1; 2449 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2443 if (lastoffset > endoff) 2450 if (lastoffset > endoff)
2444 lastoffset = endoff; 2451 lastoffset = endoff;
2445 if (imap.br_startblock == HOLESTARTBLOCK) 2452 if (imap.br_startblock == HOLESTARTBLOCK)
2446 continue; 2453 continue;
2447 ASSERT(imap.br_startblock != DELAYSTARTBLOCK); 2454 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2448 if (imap.br_state == XFS_EXT_UNWRITTEN) 2455 if (imap.br_state == XFS_EXT_UNWRITTEN)
2449 continue; 2456 continue;
2450 XFS_BUF_UNDONE(bp); 2457 XFS_BUF_UNDONE(bp);
2451 XFS_BUF_UNWRITE(bp); 2458 XFS_BUF_UNWRITE(bp);
2452 XFS_BUF_READ(bp); 2459 XFS_BUF_READ(bp);
2453 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock)); 2460 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
2454 xfsbdstrat(mp, bp); 2461 xfsbdstrat(mp, bp);
2455 error = xfs_iowait(bp); 2462 error = xfs_iowait(bp);
2456 if (error) { 2463 if (error) {
2457 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)", 2464 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2458 mp, bp, XFS_BUF_ADDR(bp)); 2465 mp, bp, XFS_BUF_ADDR(bp));
2459 break; 2466 break;
2460 } 2467 }
2461 memset(XFS_BUF_PTR(bp) + 2468 memset(XFS_BUF_PTR(bp) +
2462 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)), 2469 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2463 0, lastoffset - offset + 1); 2470 0, lastoffset - offset + 1);
2464 XFS_BUF_UNDONE(bp); 2471 XFS_BUF_UNDONE(bp);
2465 XFS_BUF_UNREAD(bp); 2472 XFS_BUF_UNREAD(bp);
2466 XFS_BUF_WRITE(bp); 2473 XFS_BUF_WRITE(bp);
2467 xfsbdstrat(mp, bp); 2474 xfsbdstrat(mp, bp);
2468 error = xfs_iowait(bp); 2475 error = xfs_iowait(bp);
2469 if (error) { 2476 if (error) {
2470 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)", 2477 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2471 mp, bp, XFS_BUF_ADDR(bp)); 2478 mp, bp, XFS_BUF_ADDR(bp));
2472 break; 2479 break;
2473 } 2480 }
2474 } 2481 }
2475 xfs_buf_free(bp); 2482 xfs_buf_free(bp);
2476 return error; 2483 return error;
2477 } 2484 }
2478 2485
2479 /* 2486 /*
2480 * xfs_free_file_space() 2487 * xfs_free_file_space()
2481 * This routine frees disk space for the given file. 2488 * This routine frees disk space for the given file.
2482 * 2489 *
2483 * This routine is only called by xfs_change_file_space 2490 * This routine is only called by xfs_change_file_space
2484 * for an UNRESVSP type call. 2491 * for an UNRESVSP type call.
2485 * 2492 *
2486 * RETURNS: 2493 * RETURNS:
2487 * 0 on success 2494 * 0 on success
2488 * errno on error 2495 * errno on error
2489 * 2496 *
2490 */ 2497 */
2491 STATIC int 2498 STATIC int
2492 xfs_free_file_space( 2499 xfs_free_file_space(
2493 xfs_inode_t *ip, 2500 xfs_inode_t *ip,
2494 xfs_off_t offset, 2501 xfs_off_t offset,
2495 xfs_off_t len, 2502 xfs_off_t len,
2496 int attr_flags) 2503 int attr_flags)
2497 { 2504 {
2498 int committed; 2505 int committed;
2499 int done; 2506 int done;
2500 xfs_fileoff_t endoffset_fsb; 2507 xfs_fileoff_t endoffset_fsb;
2501 int error; 2508 int error;
2502 xfs_fsblock_t firstfsb; 2509 xfs_fsblock_t firstfsb;
2503 xfs_bmap_free_t free_list; 2510 xfs_bmap_free_t free_list;
2504 xfs_bmbt_irec_t imap; 2511 xfs_bmbt_irec_t imap;
2505 xfs_off_t ioffset; 2512 xfs_off_t ioffset;
2506 xfs_extlen_t mod=0; 2513 xfs_extlen_t mod=0;
2507 xfs_mount_t *mp; 2514 xfs_mount_t *mp;
2508 int nimap; 2515 int nimap;
2509 uint resblks; 2516 uint resblks;
2510 uint rounding; 2517 uint rounding;
2511 int rt; 2518 int rt;
2512 xfs_fileoff_t startoffset_fsb; 2519 xfs_fileoff_t startoffset_fsb;
2513 xfs_trans_t *tp; 2520 xfs_trans_t *tp;
2514 int need_iolock = 1; 2521 int need_iolock = 1;
2515 2522
2516 mp = ip->i_mount; 2523 mp = ip->i_mount;
2517 2524
2518 trace_xfs_free_file_space(ip); 2525 trace_xfs_free_file_space(ip);
2519 2526
2520 error = xfs_qm_dqattach(ip, 0); 2527 error = xfs_qm_dqattach(ip, 0);
2521 if (error) 2528 if (error)
2522 return error; 2529 return error;
2523 2530
2524 error = 0; 2531 error = 0;
2525 if (len <= 0) /* if nothing being freed */ 2532 if (len <= 0) /* if nothing being freed */
2526 return error; 2533 return error;
2527 rt = XFS_IS_REALTIME_INODE(ip); 2534 rt = XFS_IS_REALTIME_INODE(ip);
2528 startoffset_fsb = XFS_B_TO_FSB(mp, offset); 2535 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2529 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len); 2536 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
2530 2537
2531 if (attr_flags & XFS_ATTR_NOLOCK) 2538 if (attr_flags & XFS_ATTR_NOLOCK)
2532 need_iolock = 0; 2539 need_iolock = 0;
2533 if (need_iolock) { 2540 if (need_iolock) {
2534 xfs_ilock(ip, XFS_IOLOCK_EXCL); 2541 xfs_ilock(ip, XFS_IOLOCK_EXCL);
2535 /* wait for the completion of any pending DIOs */ 2542 /* wait for the completion of any pending DIOs */
2536 xfs_ioend_wait(ip); 2543 xfs_ioend_wait(ip);
2537 } 2544 }
2538 2545
2539 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE); 2546 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
2540 ioffset = offset & ~(rounding - 1); 2547 ioffset = offset & ~(rounding - 1);
2541 2548
2542 if (VN_CACHED(VFS_I(ip)) != 0) { 2549 if (VN_CACHED(VFS_I(ip)) != 0) {
2543 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED); 2550 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
2544 if (error) 2551 if (error)
2545 goto out_unlock_iolock; 2552 goto out_unlock_iolock;
2546 } 2553 }
2547 2554
2548 /* 2555 /*
2549 * Need to zero the stuff we're not freeing, on disk. 2556 * Need to zero the stuff we're not freeing, on disk.
2550 * If it's a realtime file & can't use unwritten extents then we 2557 * If it's a realtime file & can't use unwritten extents then we
2551 * actually need to zero the extent edges. Otherwise xfs_bunmapi 2558 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2552 * will take care of it for us. 2559 * will take care of it for us.
2553 */ 2560 */
2554 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) { 2561 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
2555 nimap = 1; 2562 nimap = 1;
2556 error = xfs_bmapi(NULL, ip, startoffset_fsb, 2563 error = xfs_bmapi(NULL, ip, startoffset_fsb,
2557 1, 0, NULL, 0, &imap, &nimap, NULL); 2564 1, 0, NULL, 0, &imap, &nimap, NULL);
2558 if (error) 2565 if (error)
2559 goto out_unlock_iolock; 2566 goto out_unlock_iolock;
2560 ASSERT(nimap == 0 || nimap == 1); 2567 ASSERT(nimap == 0 || nimap == 1);
2561 if (nimap && imap.br_startblock != HOLESTARTBLOCK) { 2568 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2562 xfs_daddr_t block; 2569 xfs_daddr_t block;
2563 2570
2564 ASSERT(imap.br_startblock != DELAYSTARTBLOCK); 2571 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2565 block = imap.br_startblock; 2572 block = imap.br_startblock;
2566 mod = do_div(block, mp->m_sb.sb_rextsize); 2573 mod = do_div(block, mp->m_sb.sb_rextsize);
2567 if (mod) 2574 if (mod)
2568 startoffset_fsb += mp->m_sb.sb_rextsize - mod; 2575 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2569 } 2576 }
2570 nimap = 1; 2577 nimap = 1;
2571 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1, 2578 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
2572 1, 0, NULL, 0, &imap, &nimap, NULL); 2579 1, 0, NULL, 0, &imap, &nimap, NULL);
2573 if (error) 2580 if (error)
2574 goto out_unlock_iolock; 2581 goto out_unlock_iolock;
2575 ASSERT(nimap == 0 || nimap == 1); 2582 ASSERT(nimap == 0 || nimap == 1);
2576 if (nimap && imap.br_startblock != HOLESTARTBLOCK) { 2583 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2577 ASSERT(imap.br_startblock != DELAYSTARTBLOCK); 2584 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2578 mod++; 2585 mod++;
2579 if (mod && (mod != mp->m_sb.sb_rextsize)) 2586 if (mod && (mod != mp->m_sb.sb_rextsize))
2580 endoffset_fsb -= mod; 2587 endoffset_fsb -= mod;
2581 } 2588 }
2582 } 2589 }
2583 if ((done = (endoffset_fsb <= startoffset_fsb))) 2590 if ((done = (endoffset_fsb <= startoffset_fsb)))
2584 /* 2591 /*
2585 * One contiguous piece to clear 2592 * One contiguous piece to clear
2586 */ 2593 */
2587 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1); 2594 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2588 else { 2595 else {
2589 /* 2596 /*
2590 * Some full blocks, possibly two pieces to clear 2597 * Some full blocks, possibly two pieces to clear
2591 */ 2598 */
2592 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb)) 2599 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2593 error = xfs_zero_remaining_bytes(ip, offset, 2600 error = xfs_zero_remaining_bytes(ip, offset,
2594 XFS_FSB_TO_B(mp, startoffset_fsb) - 1); 2601 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2595 if (!error && 2602 if (!error &&
2596 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len) 2603 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2597 error = xfs_zero_remaining_bytes(ip, 2604 error = xfs_zero_remaining_bytes(ip,
2598 XFS_FSB_TO_B(mp, endoffset_fsb), 2605 XFS_FSB_TO_B(mp, endoffset_fsb),
2599 offset + len - 1); 2606 offset + len - 1);
2600 } 2607 }
2601 2608
2602 /* 2609 /*
2603 * free file space until done or until there is an error 2610 * free file space until done or until there is an error
2604 */ 2611 */
2605 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); 2612 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2606 while (!error && !done) { 2613 while (!error && !done) {
2607 2614
2608 /* 2615 /*
2609 * allocate and setup the transaction. Allow this 2616 * allocate and setup the transaction. Allow this
2610 * transaction to dip into the reserve blocks to ensure 2617 * transaction to dip into the reserve blocks to ensure
2611 * the freeing of the space succeeds at ENOSPC. 2618 * the freeing of the space succeeds at ENOSPC.
2612 */ 2619 */
2613 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); 2620 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2614 tp->t_flags |= XFS_TRANS_RESERVE; 2621 tp->t_flags |= XFS_TRANS_RESERVE;
2615 error = xfs_trans_reserve(tp, 2622 error = xfs_trans_reserve(tp,
2616 resblks, 2623 resblks,
2617 XFS_WRITE_LOG_RES(mp), 2624 XFS_WRITE_LOG_RES(mp),
2618 0, 2625 0,
2619 XFS_TRANS_PERM_LOG_RES, 2626 XFS_TRANS_PERM_LOG_RES,
2620 XFS_WRITE_LOG_COUNT); 2627 XFS_WRITE_LOG_COUNT);
2621 2628
2622 /* 2629 /*
2623 * check for running out of space 2630 * check for running out of space
2624 */ 2631 */
2625 if (error) { 2632 if (error) {
2626 /* 2633 /*
2627 * Free the transaction structure. 2634 * Free the transaction structure.
2628 */ 2635 */
2629 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp)); 2636 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2630 xfs_trans_cancel(tp, 0); 2637 xfs_trans_cancel(tp, 0);
2631 break; 2638 break;
2632 } 2639 }
2633 xfs_ilock(ip, XFS_ILOCK_EXCL); 2640 xfs_ilock(ip, XFS_ILOCK_EXCL);
2634 error = xfs_trans_reserve_quota(tp, mp, 2641 error = xfs_trans_reserve_quota(tp, mp,
2635 ip->i_udquot, ip->i_gdquot, 2642 ip->i_udquot, ip->i_gdquot,
2636 resblks, 0, XFS_QMOPT_RES_REGBLKS); 2643 resblks, 0, XFS_QMOPT_RES_REGBLKS);
2637 if (error) 2644 if (error)
2638 goto error1; 2645 goto error1;
2639 2646
2640 xfs_trans_ijoin(tp, ip); 2647 xfs_trans_ijoin(tp, ip);
2641 2648
2642 /* 2649 /*
2643 * issue the bunmapi() call to free the blocks 2650 * issue the bunmapi() call to free the blocks
2644 */ 2651 */
2645 xfs_bmap_init(&free_list, &firstfsb); 2652 xfs_bmap_init(&free_list, &firstfsb);
2646 error = xfs_bunmapi(tp, ip, startoffset_fsb, 2653 error = xfs_bunmapi(tp, ip, startoffset_fsb,
2647 endoffset_fsb - startoffset_fsb, 2654 endoffset_fsb - startoffset_fsb,
2648 0, 2, &firstfsb, &free_list, &done); 2655 0, 2, &firstfsb, &free_list, &done);
2649 if (error) { 2656 if (error) {
2650 goto error0; 2657 goto error0;
2651 } 2658 }
2652 2659
2653 /* 2660 /*
2654 * complete the transaction 2661 * complete the transaction
2655 */ 2662 */
2656 error = xfs_bmap_finish(&tp, &free_list, &committed); 2663 error = xfs_bmap_finish(&tp, &free_list, &committed);
2657 if (error) { 2664 if (error) {
2658 goto error0; 2665 goto error0;
2659 } 2666 }
2660 2667
2661 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 2668 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2662 xfs_iunlock(ip, XFS_ILOCK_EXCL); 2669 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2663 } 2670 }
2664 2671
2665 out_unlock_iolock: 2672 out_unlock_iolock:
2666 if (need_iolock) 2673 if (need_iolock)
2667 xfs_iunlock(ip, XFS_IOLOCK_EXCL); 2674 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2668 return error; 2675 return error;
2669 2676
2670 error0: 2677 error0:
2671 xfs_bmap_cancel(&free_list); 2678 xfs_bmap_cancel(&free_list);
2672 error1: 2679 error1:
2673 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); 2680 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2674 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) : 2681 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2675 XFS_ILOCK_EXCL); 2682 XFS_ILOCK_EXCL);
2676 return error; 2683 return error;
2677 } 2684 }
2678 2685
2679 /* 2686 /*
2680 * xfs_change_file_space() 2687 * xfs_change_file_space()
2681 * This routine allocates or frees disk space for the given file. 2688 * This routine allocates or frees disk space for the given file.
2682 * The user specified parameters are checked for alignment and size 2689 * The user specified parameters are checked for alignment and size
2683 * limitations. 2690 * limitations.
2684 * 2691 *
2685 * RETURNS: 2692 * RETURNS:
2686 * 0 on success 2693 * 0 on success
2687 * errno on error 2694 * errno on error
2688 * 2695 *
2689 */ 2696 */
2690 int 2697 int
2691 xfs_change_file_space( 2698 xfs_change_file_space(
2692 xfs_inode_t *ip, 2699 xfs_inode_t *ip,
2693 int cmd, 2700 int cmd,
2694 xfs_flock64_t *bf, 2701 xfs_flock64_t *bf,
2695 xfs_off_t offset, 2702 xfs_off_t offset,
2696 int attr_flags) 2703 int attr_flags)
2697 { 2704 {
2698 xfs_mount_t *mp = ip->i_mount; 2705 xfs_mount_t *mp = ip->i_mount;
2699 int clrprealloc; 2706 int clrprealloc;
2700 int error; 2707 int error;
2701 xfs_fsize_t fsize; 2708 xfs_fsize_t fsize;
2702 int setprealloc; 2709 int setprealloc;
2703 xfs_off_t startoffset; 2710 xfs_off_t startoffset;
2704 xfs_off_t llen; 2711 xfs_off_t llen;
2705 xfs_trans_t *tp; 2712 xfs_trans_t *tp;
2706 struct iattr iattr; 2713 struct iattr iattr;
2707 2714
2708 if (!S_ISREG(ip->i_d.di_mode)) 2715 if (!S_ISREG(ip->i_d.di_mode))
2709 return XFS_ERROR(EINVAL); 2716 return XFS_ERROR(EINVAL);
2710 2717
2711 switch (bf->l_whence) { 2718 switch (bf->l_whence) {
2712 case 0: /*SEEK_SET*/ 2719 case 0: /*SEEK_SET*/
2713 break; 2720 break;
2714 case 1: /*SEEK_CUR*/ 2721 case 1: /*SEEK_CUR*/
2715 bf->l_start += offset; 2722 bf->l_start += offset;
2716 break; 2723 break;
2717 case 2: /*SEEK_END*/ 2724 case 2: /*SEEK_END*/
2718 bf->l_start += ip->i_size; 2725 bf->l_start += ip->i_size;
2719 break; 2726 break;
2720 default: 2727 default:
2721 return XFS_ERROR(EINVAL); 2728 return XFS_ERROR(EINVAL);
2722 } 2729 }
2723 2730
2724 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len; 2731 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2725 2732
2726 if ( (bf->l_start < 0) 2733 if ( (bf->l_start < 0)
2727 || (bf->l_start > XFS_MAXIOFFSET(mp)) 2734 || (bf->l_start > XFS_MAXIOFFSET(mp))
2728 || (bf->l_start + llen < 0) 2735 || (bf->l_start + llen < 0)
2729 || (bf->l_start + llen > XFS_MAXIOFFSET(mp))) 2736 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
2730 return XFS_ERROR(EINVAL); 2737 return XFS_ERROR(EINVAL);
2731 2738
2732 bf->l_whence = 0; 2739 bf->l_whence = 0;
2733 2740
2734 startoffset = bf->l_start; 2741 startoffset = bf->l_start;
2735 fsize = ip->i_size; 2742 fsize = ip->i_size;
2736 2743
2737 /* 2744 /*
2738 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve 2745 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2739 * file space. 2746 * file space.
2740 * These calls do NOT zero the data space allocated to the file, 2747 * These calls do NOT zero the data space allocated to the file,
2741 * nor do they change the file size. 2748 * nor do they change the file size.
2742 * 2749 *
2743 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file 2750 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2744 * space. 2751 * space.
2745 * These calls cause the new file data to be zeroed and the file 2752 * These calls cause the new file data to be zeroed and the file
2746 * size to be changed. 2753 * size to be changed.
2747 */ 2754 */
2748 setprealloc = clrprealloc = 0; 2755 setprealloc = clrprealloc = 0;
2749 2756
2750 switch (cmd) { 2757 switch (cmd) {
2751 case XFS_IOC_RESVSP: 2758 case XFS_IOC_RESVSP:
2752 case XFS_IOC_RESVSP64: 2759 case XFS_IOC_RESVSP64:
2753 error = xfs_alloc_file_space(ip, startoffset, bf->l_len, 2760 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
2754 1, attr_flags); 2761 1, attr_flags);
2755 if (error) 2762 if (error)
2756 return error; 2763 return error;
2757 setprealloc = 1; 2764 setprealloc = 1;
2758 break; 2765 break;
2759 2766
2760 case XFS_IOC_UNRESVSP: 2767 case XFS_IOC_UNRESVSP:
2761 case XFS_IOC_UNRESVSP64: 2768 case XFS_IOC_UNRESVSP64:
2762 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len, 2769 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2763 attr_flags))) 2770 attr_flags)))
2764 return error; 2771 return error;
2765 break; 2772 break;
2766 2773
2767 case XFS_IOC_ALLOCSP: 2774 case XFS_IOC_ALLOCSP:
2768 case XFS_IOC_ALLOCSP64: 2775 case XFS_IOC_ALLOCSP64:
2769 case XFS_IOC_FREESP: 2776 case XFS_IOC_FREESP:
2770 case XFS_IOC_FREESP64: 2777 case XFS_IOC_FREESP64:
2771 if (startoffset > fsize) { 2778 if (startoffset > fsize) {
2772 error = xfs_alloc_file_space(ip, fsize, 2779 error = xfs_alloc_file_space(ip, fsize,
2773 startoffset - fsize, 0, attr_flags); 2780 startoffset - fsize, 0, attr_flags);
2774 if (error) 2781 if (error)
2775 break; 2782 break;
2776 } 2783 }
2777 2784
2778 iattr.ia_valid = ATTR_SIZE; 2785 iattr.ia_valid = ATTR_SIZE;
2779 iattr.ia_size = startoffset; 2786 iattr.ia_size = startoffset;
2780 2787
2781 error = xfs_setattr(ip, &iattr, attr_flags); 2788 error = xfs_setattr(ip, &iattr, attr_flags);
2782 2789
2783 if (error) 2790 if (error)
2784 return error; 2791 return error;
2785 2792
2786 clrprealloc = 1; 2793 clrprealloc = 1;
2787 break; 2794 break;
2788 2795
2789 default: 2796 default:
2790 ASSERT(0); 2797 ASSERT(0);
2791 return XFS_ERROR(EINVAL); 2798 return XFS_ERROR(EINVAL);
2792 } 2799 }
2793 2800
2794 /* 2801 /*
2795 * update the inode timestamp, mode, and prealloc flag bits 2802 * update the inode timestamp, mode, and prealloc flag bits
2796 */ 2803 */
2797 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID); 2804 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2798 2805
2799 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp), 2806 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2800 0, 0, 0))) { 2807 0, 0, 0))) {
2801 /* ASSERT(0); */ 2808 /* ASSERT(0); */
2802 xfs_trans_cancel(tp, 0); 2809 xfs_trans_cancel(tp, 0);
2803 return error; 2810 return error;
2804 } 2811 }
2805 2812
2806 xfs_ilock(ip, XFS_ILOCK_EXCL); 2813 xfs_ilock(ip, XFS_ILOCK_EXCL);
2807 2814
2808 xfs_trans_ijoin(tp, ip); 2815 xfs_trans_ijoin(tp, ip);
2809 2816
2810 if ((attr_flags & XFS_ATTR_DMI) == 0) { 2817 if ((attr_flags & XFS_ATTR_DMI) == 0) {
2811 ip->i_d.di_mode &= ~S_ISUID; 2818 ip->i_d.di_mode &= ~S_ISUID;
2812 2819
2813 /* 2820 /*
2814 * Note that we don't have to worry about mandatory 2821 * Note that we don't have to worry about mandatory
2815 * file locking being disabled here because we only 2822 * file locking being disabled here because we only
2816 * clear the S_ISGID bit if the Group execute bit is 2823 * clear the S_ISGID bit if the Group execute bit is
2817 * on, but if it was on then mandatory locking wouldn't 2824 * on, but if it was on then mandatory locking wouldn't
2818 * have been enabled. 2825 * have been enabled.
2819 */ 2826 */
2820 if (ip->i_d.di_mode & S_IXGRP) 2827 if (ip->i_d.di_mode & S_IXGRP)
2821 ip->i_d.di_mode &= ~S_ISGID; 2828 ip->i_d.di_mode &= ~S_ISGID;
2822 2829
2823 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 2830 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2824 } 2831 }
2825 if (setprealloc) 2832 if (setprealloc)
2826 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC; 2833 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2827 else if (clrprealloc) 2834 else if (clrprealloc)
2828 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC; 2835 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2829 2836
2830 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 2837 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2831 xfs_trans_set_sync(tp); 2838 xfs_trans_set_sync(tp);
2832 2839
2833 error = xfs_trans_commit(tp, 0); 2840 error = xfs_trans_commit(tp, 0);
2834 2841
2835 xfs_iunlock(ip, XFS_ILOCK_EXCL); 2842 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2836 2843
2837 return error; 2844 return error;
2838 } 2845 }