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