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