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