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