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