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