hwmon: (via-cputemp) sync hotplug handling with coretemp/pkgtemp
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_vnodeops.c
blob8e4a63c4151a48e05a11d9c791f7c5a9092bf197
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
7 * published by the Free Software Foundation.
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.
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
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_types.h"
22 #include "xfs_bit.h"
23 #include "xfs_log.h"
24 #include "xfs_inum.h"
25 #include "xfs_trans.h"
26 #include "xfs_sb.h"
27 #include "xfs_ag.h"
28 #include "xfs_dir2.h"
29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_inode_item.h"
36 #include "xfs_itable.h"
37 #include "xfs_ialloc.h"
38 #include "xfs_alloc.h"
39 #include "xfs_bmap.h"
40 #include "xfs_acl.h"
41 #include "xfs_attr.h"
42 #include "xfs_rw.h"
43 #include "xfs_error.h"
44 #include "xfs_quota.h"
45 #include "xfs_utils.h"
46 #include "xfs_rtalloc.h"
47 #include "xfs_trans_space.h"
48 #include "xfs_log_priv.h"
49 #include "xfs_filestream.h"
50 #include "xfs_vnodeops.h"
51 #include "xfs_trace.h"
53 int
54 xfs_setattr(
55 struct xfs_inode *ip,
56 struct iattr *iattr,
57 int flags)
59 xfs_mount_t *mp = ip->i_mount;
60 struct inode *inode = VFS_I(ip);
61 int mask = iattr->ia_valid;
62 xfs_trans_t *tp;
63 int code;
64 uint lock_flags;
65 uint commit_flags=0;
66 uid_t uid=0, iuid=0;
67 gid_t gid=0, igid=0;
68 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
69 int need_iolock = 1;
71 trace_xfs_setattr(ip);
73 if (mp->m_flags & XFS_MOUNT_RDONLY)
74 return XFS_ERROR(EROFS);
76 if (XFS_FORCED_SHUTDOWN(mp))
77 return XFS_ERROR(EIO);
79 code = -inode_change_ok(inode, iattr);
80 if (code)
81 return code;
83 olddquot1 = olddquot2 = NULL;
84 udqp = gdqp = NULL;
87 * If disk quotas is on, we make sure that the dquots do exist on disk,
88 * before we start any other transactions. Trying to do this later
89 * is messy. We don't care to take a readlock to look at the ids
90 * in inode here, because we can't hold it across the trans_reserve.
91 * If the IDs do change before we take the ilock, we're covered
92 * because the i_*dquot fields will get updated anyway.
94 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
95 uint qflags = 0;
97 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
98 uid = iattr->ia_uid;
99 qflags |= XFS_QMOPT_UQUOTA;
100 } else {
101 uid = ip->i_d.di_uid;
103 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
104 gid = iattr->ia_gid;
105 qflags |= XFS_QMOPT_GQUOTA;
106 } else {
107 gid = ip->i_d.di_gid;
111 * We take a reference when we initialize udqp and gdqp,
112 * so it is important that we never blindly double trip on
113 * the same variable. See xfs_create() for an example.
115 ASSERT(udqp == NULL);
116 ASSERT(gdqp == NULL);
117 code = xfs_qm_vop_dqalloc(ip, uid, gid, xfs_get_projid(ip),
118 qflags, &udqp, &gdqp);
119 if (code)
120 return code;
124 * For the other attributes, we acquire the inode lock and
125 * first do an error checking pass.
127 tp = NULL;
128 lock_flags = XFS_ILOCK_EXCL;
129 if (flags & XFS_ATTR_NOLOCK)
130 need_iolock = 0;
131 if (!(mask & ATTR_SIZE)) {
132 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
133 commit_flags = 0;
134 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp),
135 0, 0, 0);
136 if (code) {
137 lock_flags = 0;
138 goto error_return;
140 } else {
141 if (need_iolock)
142 lock_flags |= XFS_IOLOCK_EXCL;
145 xfs_ilock(ip, lock_flags);
148 * Change file ownership. Must be the owner or privileged.
150 if (mask & (ATTR_UID|ATTR_GID)) {
152 * These IDs could have changed since we last looked at them.
153 * But, we're assured that if the ownership did change
154 * while we didn't have the inode locked, inode's dquot(s)
155 * would have changed also.
157 iuid = ip->i_d.di_uid;
158 igid = ip->i_d.di_gid;
159 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
160 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
163 * Do a quota reservation only if uid/gid is actually
164 * going to change.
166 if (XFS_IS_QUOTA_RUNNING(mp) &&
167 ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
168 (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
169 ASSERT(tp);
170 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
171 capable(CAP_FOWNER) ?
172 XFS_QMOPT_FORCE_RES : 0);
173 if (code) /* out of quota */
174 goto error_return;
179 * Truncate file. Must have write permission and not be a directory.
181 if (mask & ATTR_SIZE) {
182 /* Short circuit the truncate case for zero length files */
183 if (iattr->ia_size == 0 &&
184 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
185 xfs_iunlock(ip, XFS_ILOCK_EXCL);
186 lock_flags &= ~XFS_ILOCK_EXCL;
187 if (mask & ATTR_CTIME) {
188 inode->i_mtime = inode->i_ctime =
189 current_fs_time(inode->i_sb);
190 xfs_mark_inode_dirty_sync(ip);
192 code = 0;
193 goto error_return;
196 if (S_ISDIR(ip->i_d.di_mode)) {
197 code = XFS_ERROR(EISDIR);
198 goto error_return;
199 } else if (!S_ISREG(ip->i_d.di_mode)) {
200 code = XFS_ERROR(EINVAL);
201 goto error_return;
205 * Make sure that the dquots are attached to the inode.
207 code = xfs_qm_dqattach_locked(ip, 0);
208 if (code)
209 goto error_return;
212 * Now we can make the changes. Before we join the inode
213 * to the transaction, if ATTR_SIZE is set then take care of
214 * the part of the truncation that must be done without the
215 * inode lock. This needs to be done before joining the inode
216 * to the transaction, because the inode cannot be unlocked
217 * once it is a part of the transaction.
219 if (iattr->ia_size > ip->i_size) {
221 * Do the first part of growing a file: zero any data
222 * in the last block that is beyond the old EOF. We
223 * need to do this before the inode is joined to the
224 * transaction to modify the i_size.
226 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
227 if (code)
228 goto error_return;
230 xfs_iunlock(ip, XFS_ILOCK_EXCL);
231 lock_flags &= ~XFS_ILOCK_EXCL;
234 * We are going to log the inode size change in this
235 * transaction so any previous writes that are beyond the on
236 * disk EOF and the new EOF that have not been written out need
237 * to be written here. If we do not write the data out, we
238 * expose ourselves to the null files problem.
240 * Only flush from the on disk size to the smaller of the in
241 * memory file size or the new size as that's the range we
242 * really care about here and prevents waiting for other data
243 * not within the range we care about here.
245 if (ip->i_size != ip->i_d.di_size &&
246 iattr->ia_size > ip->i_d.di_size) {
247 code = xfs_flush_pages(ip,
248 ip->i_d.di_size, iattr->ia_size,
249 XBF_ASYNC, FI_NONE);
250 if (code)
251 goto error_return;
254 /* wait for all I/O to complete */
255 xfs_ioend_wait(ip);
257 code = -block_truncate_page(inode->i_mapping, iattr->ia_size,
258 xfs_get_blocks);
259 if (code)
260 goto error_return;
262 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
263 code = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
264 XFS_TRANS_PERM_LOG_RES,
265 XFS_ITRUNCATE_LOG_COUNT);
266 if (code)
267 goto error_return;
269 truncate_setsize(inode, iattr->ia_size);
271 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
272 lock_flags |= XFS_ILOCK_EXCL;
274 xfs_ilock(ip, XFS_ILOCK_EXCL);
276 xfs_trans_ijoin(tp, ip);
279 * Only change the c/mtime if we are changing the size
280 * or we are explicitly asked to change it. This handles
281 * the semantic difference between truncate() and ftruncate()
282 * as implemented in the VFS.
284 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME
285 * is a special case where we need to update the times despite
286 * not having these flags set. For all other operations the
287 * VFS set these flags explicitly if it wants a timestamp
288 * update.
290 if (iattr->ia_size != ip->i_size &&
291 (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
292 iattr->ia_ctime = iattr->ia_mtime =
293 current_fs_time(inode->i_sb);
294 mask |= ATTR_CTIME | ATTR_MTIME;
297 if (iattr->ia_size > ip->i_size) {
298 ip->i_d.di_size = iattr->ia_size;
299 ip->i_size = iattr->ia_size;
300 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
301 } else if (iattr->ia_size <= ip->i_size ||
302 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
304 * signal a sync transaction unless
305 * we're truncating an already unlinked
306 * file on a wsync filesystem
308 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
309 XFS_DATA_FORK,
310 ((ip->i_d.di_nlink != 0 ||
311 !(mp->m_flags & XFS_MOUNT_WSYNC))
312 ? 1 : 0));
313 if (code)
314 goto abort_return;
316 * Truncated "down", so we're removing references
317 * to old data here - if we now delay flushing for
318 * a long time, we expose ourselves unduly to the
319 * notorious NULL files problem. So, we mark this
320 * vnode and flush it when the file is closed, and
321 * do not wait the usual (long) time for writeout.
323 xfs_iflags_set(ip, XFS_ITRUNCATED);
325 } else if (tp) {
326 xfs_trans_ijoin(tp, ip);
330 * Change file ownership. Must be the owner or privileged.
332 if (mask & (ATTR_UID|ATTR_GID)) {
334 * CAP_FSETID overrides the following restrictions:
336 * The set-user-ID and set-group-ID bits of a file will be
337 * cleared upon successful return from chown()
339 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
340 !capable(CAP_FSETID)) {
341 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
345 * Change the ownerships and register quota modifications
346 * in the transaction.
348 if (iuid != uid) {
349 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
350 ASSERT(mask & ATTR_UID);
351 ASSERT(udqp);
352 olddquot1 = xfs_qm_vop_chown(tp, ip,
353 &ip->i_udquot, udqp);
355 ip->i_d.di_uid = uid;
356 inode->i_uid = uid;
358 if (igid != gid) {
359 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
360 ASSERT(!XFS_IS_PQUOTA_ON(mp));
361 ASSERT(mask & ATTR_GID);
362 ASSERT(gdqp);
363 olddquot2 = xfs_qm_vop_chown(tp, ip,
364 &ip->i_gdquot, gdqp);
366 ip->i_d.di_gid = gid;
367 inode->i_gid = gid;
372 * Change file access modes.
374 if (mask & ATTR_MODE) {
375 umode_t mode = iattr->ia_mode;
377 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
378 mode &= ~S_ISGID;
380 ip->i_d.di_mode &= S_IFMT;
381 ip->i_d.di_mode |= mode & ~S_IFMT;
383 inode->i_mode &= S_IFMT;
384 inode->i_mode |= mode & ~S_IFMT;
388 * Change file access or modified times.
390 if (mask & ATTR_ATIME) {
391 inode->i_atime = iattr->ia_atime;
392 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
393 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
394 ip->i_update_core = 1;
396 if (mask & ATTR_CTIME) {
397 inode->i_ctime = iattr->ia_ctime;
398 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
399 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
400 ip->i_update_core = 1;
402 if (mask & ATTR_MTIME) {
403 inode->i_mtime = iattr->ia_mtime;
404 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
405 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
406 ip->i_update_core = 1;
410 * And finally, log the inode core if any attribute in it
411 * has been changed.
413 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE|
414 ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
415 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
417 XFS_STATS_INC(xs_ig_attrchg);
420 * If this is a synchronous mount, make sure that the
421 * transaction goes to disk before returning to the user.
422 * This is slightly sub-optimal in that truncates require
423 * two sync transactions instead of one for wsync filesystems.
424 * One for the truncate and one for the timestamps since we
425 * don't want to change the timestamps unless we're sure the
426 * truncate worked. Truncates are less than 1% of the laddis
427 * mix so this probably isn't worth the trouble to optimize.
429 code = 0;
430 if (mp->m_flags & XFS_MOUNT_WSYNC)
431 xfs_trans_set_sync(tp);
433 code = xfs_trans_commit(tp, commit_flags);
435 xfs_iunlock(ip, lock_flags);
438 * Release any dquot(s) the inode had kept before chown.
440 xfs_qm_dqrele(olddquot1);
441 xfs_qm_dqrele(olddquot2);
442 xfs_qm_dqrele(udqp);
443 xfs_qm_dqrele(gdqp);
445 if (code)
446 return code;
449 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
450 * update. We could avoid this with linked transactions
451 * and passing down the transaction pointer all the way
452 * to attr_set. No previous user of the generic
453 * Posix ACL code seems to care about this issue either.
455 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
456 code = -xfs_acl_chmod(inode);
457 if (code)
458 return XFS_ERROR(code);
461 return 0;
463 abort_return:
464 commit_flags |= XFS_TRANS_ABORT;
465 error_return:
466 xfs_qm_dqrele(udqp);
467 xfs_qm_dqrele(gdqp);
468 if (tp) {
469 xfs_trans_cancel(tp, commit_flags);
471 if (lock_flags != 0) {
472 xfs_iunlock(ip, lock_flags);
474 return code;
478 * The maximum pathlen is 1024 bytes. Since the minimum file system
479 * blocksize is 512 bytes, we can get a max of 2 extents back from
480 * bmapi.
482 #define SYMLINK_MAPS 2
484 STATIC int
485 xfs_readlink_bmap(
486 xfs_inode_t *ip,
487 char *link)
489 xfs_mount_t *mp = ip->i_mount;
490 int pathlen = ip->i_d.di_size;
491 int nmaps = SYMLINK_MAPS;
492 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
493 xfs_daddr_t d;
494 int byte_cnt;
495 int n;
496 xfs_buf_t *bp;
497 int error = 0;
499 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
500 mval, &nmaps, NULL);
501 if (error)
502 goto out;
504 for (n = 0; n < nmaps; n++) {
505 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
506 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
508 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
509 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
510 error = XFS_BUF_GETERROR(bp);
511 if (error) {
512 xfs_ioerror_alert("xfs_readlink",
513 ip->i_mount, bp, XFS_BUF_ADDR(bp));
514 xfs_buf_relse(bp);
515 goto out;
517 if (pathlen < byte_cnt)
518 byte_cnt = pathlen;
519 pathlen -= byte_cnt;
521 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
522 xfs_buf_relse(bp);
525 link[ip->i_d.di_size] = '\0';
526 error = 0;
528 out:
529 return error;
533 xfs_readlink(
534 xfs_inode_t *ip,
535 char *link)
537 xfs_mount_t *mp = ip->i_mount;
538 int pathlen;
539 int error = 0;
541 trace_xfs_readlink(ip);
543 if (XFS_FORCED_SHUTDOWN(mp))
544 return XFS_ERROR(EIO);
546 xfs_ilock(ip, XFS_ILOCK_SHARED);
548 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
549 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
551 pathlen = ip->i_d.di_size;
552 if (!pathlen)
553 goto out;
555 if (ip->i_df.if_flags & XFS_IFINLINE) {
556 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
557 link[pathlen] = '\0';
558 } else {
559 error = xfs_readlink_bmap(ip, link);
562 out:
563 xfs_iunlock(ip, XFS_ILOCK_SHARED);
564 return error;
568 * Flags for xfs_free_eofblocks
570 #define XFS_FREE_EOF_TRYLOCK (1<<0)
573 * This is called by xfs_inactive to free any blocks beyond eof
574 * when the link count isn't zero and by xfs_dm_punch_hole() when
575 * punching a hole to EOF.
577 STATIC int
578 xfs_free_eofblocks(
579 xfs_mount_t *mp,
580 xfs_inode_t *ip,
581 int flags)
583 xfs_trans_t *tp;
584 int error;
585 xfs_fileoff_t end_fsb;
586 xfs_fileoff_t last_fsb;
587 xfs_filblks_t map_len;
588 int nimaps;
589 xfs_bmbt_irec_t imap;
592 * Figure out if there are any blocks beyond the end
593 * of the file. If not, then there is nothing to do.
595 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
596 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
597 if (last_fsb <= end_fsb)
598 return 0;
599 map_len = last_fsb - end_fsb;
601 nimaps = 1;
602 xfs_ilock(ip, XFS_ILOCK_SHARED);
603 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
604 NULL, 0, &imap, &nimaps, NULL);
605 xfs_iunlock(ip, XFS_ILOCK_SHARED);
607 if (!error && (nimaps != 0) &&
608 (imap.br_startblock != HOLESTARTBLOCK ||
609 ip->i_delayed_blks)) {
611 * Attach the dquots to the inode up front.
613 error = xfs_qm_dqattach(ip, 0);
614 if (error)
615 return error;
618 * There are blocks after the end of file.
619 * Free them up now by truncating the file to
620 * its current size.
622 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
625 * Do the xfs_itruncate_start() call before
626 * reserving any log space because
627 * itruncate_start will call into the buffer
628 * cache and we can't
629 * do that within a transaction.
631 if (flags & XFS_FREE_EOF_TRYLOCK) {
632 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
633 xfs_trans_cancel(tp, 0);
634 return 0;
636 } else {
637 xfs_ilock(ip, XFS_IOLOCK_EXCL);
639 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
640 ip->i_size);
641 if (error) {
642 xfs_trans_cancel(tp, 0);
643 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
644 return error;
647 error = xfs_trans_reserve(tp, 0,
648 XFS_ITRUNCATE_LOG_RES(mp),
649 0, XFS_TRANS_PERM_LOG_RES,
650 XFS_ITRUNCATE_LOG_COUNT);
651 if (error) {
652 ASSERT(XFS_FORCED_SHUTDOWN(mp));
653 xfs_trans_cancel(tp, 0);
654 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
655 return error;
658 xfs_ilock(ip, XFS_ILOCK_EXCL);
659 xfs_trans_ijoin(tp, ip);
661 error = xfs_itruncate_finish(&tp, ip,
662 ip->i_size,
663 XFS_DATA_FORK,
666 * If we get an error at this point we
667 * simply don't bother truncating the file.
669 if (error) {
670 xfs_trans_cancel(tp,
671 (XFS_TRANS_RELEASE_LOG_RES |
672 XFS_TRANS_ABORT));
673 } else {
674 error = xfs_trans_commit(tp,
675 XFS_TRANS_RELEASE_LOG_RES);
677 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
679 return error;
683 * Free a symlink that has blocks associated with it.
685 STATIC int
686 xfs_inactive_symlink_rmt(
687 xfs_inode_t *ip,
688 xfs_trans_t **tpp)
690 xfs_buf_t *bp;
691 int committed;
692 int done;
693 int error;
694 xfs_fsblock_t first_block;
695 xfs_bmap_free_t free_list;
696 int i;
697 xfs_mount_t *mp;
698 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
699 int nmaps;
700 xfs_trans_t *ntp;
701 int size;
702 xfs_trans_t *tp;
704 tp = *tpp;
705 mp = ip->i_mount;
706 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
708 * We're freeing a symlink that has some
709 * blocks allocated to it. Free the
710 * blocks here. We know that we've got
711 * either 1 or 2 extents and that we can
712 * free them all in one bunmapi call.
714 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
715 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
716 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
717 ASSERT(XFS_FORCED_SHUTDOWN(mp));
718 xfs_trans_cancel(tp, 0);
719 *tpp = NULL;
720 return error;
723 * Lock the inode, fix the size, and join it to the transaction.
724 * Hold it so in the normal path, we still have it locked for
725 * the second transaction. In the error paths we need it
726 * held so the cancel won't rele it, see below.
728 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
729 size = (int)ip->i_d.di_size;
730 ip->i_d.di_size = 0;
731 xfs_trans_ijoin(tp, ip);
732 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
734 * Find the block(s) so we can inval and unmap them.
736 done = 0;
737 xfs_bmap_init(&free_list, &first_block);
738 nmaps = ARRAY_SIZE(mval);
739 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
740 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
741 &free_list)))
742 goto error0;
744 * Invalidate the block(s).
746 for (i = 0; i < nmaps; i++) {
747 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
748 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
749 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
750 xfs_trans_binval(tp, bp);
753 * Unmap the dead block(s) to the free_list.
755 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
756 &first_block, &free_list, &done)))
757 goto error1;
758 ASSERT(done);
760 * Commit the first transaction. This logs the EFI and the inode.
762 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
763 goto error1;
765 * The transaction must have been committed, since there were
766 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
767 * The new tp has the extent freeing and EFDs.
769 ASSERT(committed);
771 * The first xact was committed, so add the inode to the new one.
772 * Mark it dirty so it will be logged and moved forward in the log as
773 * part of every commit.
775 xfs_trans_ijoin(tp, ip);
776 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
778 * Get a new, empty transaction to return to our caller.
780 ntp = xfs_trans_dup(tp);
782 * Commit the transaction containing extent freeing and EFDs.
783 * If we get an error on the commit here or on the reserve below,
784 * we need to unlock the inode since the new transaction doesn't
785 * have the inode attached.
787 error = xfs_trans_commit(tp, 0);
788 tp = ntp;
789 if (error) {
790 ASSERT(XFS_FORCED_SHUTDOWN(mp));
791 goto error0;
794 * transaction commit worked ok so we can drop the extra ticket
795 * reference that we gained in xfs_trans_dup()
797 xfs_log_ticket_put(tp->t_ticket);
800 * Remove the memory for extent descriptions (just bookkeeping).
802 if (ip->i_df.if_bytes)
803 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
804 ASSERT(ip->i_df.if_bytes == 0);
806 * Put an itruncate log reservation in the new transaction
807 * for our caller.
809 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
810 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
811 ASSERT(XFS_FORCED_SHUTDOWN(mp));
812 goto error0;
815 * Return with the inode locked but not joined to the transaction.
817 *tpp = tp;
818 return 0;
820 error1:
821 xfs_bmap_cancel(&free_list);
822 error0:
824 * Have to come here with the inode locked and either
825 * (held and in the transaction) or (not in the transaction).
826 * If the inode isn't held then cancel would iput it, but
827 * that's wrong since this is inactive and the vnode ref
828 * count is 0 already.
829 * Cancel won't do anything to the inode if held, but it still
830 * needs to be locked until the cancel is done, if it was
831 * joined to the transaction.
833 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
834 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
835 *tpp = NULL;
836 return error;
840 STATIC int
841 xfs_inactive_symlink_local(
842 xfs_inode_t *ip,
843 xfs_trans_t **tpp)
845 int error;
847 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
849 * We're freeing a symlink which fit into
850 * the inode. Just free the memory used
851 * to hold the old symlink.
853 error = xfs_trans_reserve(*tpp, 0,
854 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
855 0, XFS_TRANS_PERM_LOG_RES,
856 XFS_ITRUNCATE_LOG_COUNT);
858 if (error) {
859 xfs_trans_cancel(*tpp, 0);
860 *tpp = NULL;
861 return error;
863 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
866 * Zero length symlinks _can_ exist.
868 if (ip->i_df.if_bytes > 0) {
869 xfs_idata_realloc(ip,
870 -(ip->i_df.if_bytes),
871 XFS_DATA_FORK);
872 ASSERT(ip->i_df.if_bytes == 0);
874 return 0;
877 STATIC int
878 xfs_inactive_attrs(
879 xfs_inode_t *ip,
880 xfs_trans_t **tpp)
882 xfs_trans_t *tp;
883 int error;
884 xfs_mount_t *mp;
886 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
887 tp = *tpp;
888 mp = ip->i_mount;
889 ASSERT(ip->i_d.di_forkoff != 0);
890 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
891 xfs_iunlock(ip, XFS_ILOCK_EXCL);
892 if (error)
893 goto error_unlock;
895 error = xfs_attr_inactive(ip);
896 if (error)
897 goto error_unlock;
899 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
900 error = xfs_trans_reserve(tp, 0,
901 XFS_IFREE_LOG_RES(mp),
902 0, XFS_TRANS_PERM_LOG_RES,
903 XFS_INACTIVE_LOG_COUNT);
904 if (error)
905 goto error_cancel;
907 xfs_ilock(ip, XFS_ILOCK_EXCL);
908 xfs_trans_ijoin(tp, ip);
909 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
911 ASSERT(ip->i_d.di_anextents == 0);
913 *tpp = tp;
914 return 0;
916 error_cancel:
917 ASSERT(XFS_FORCED_SHUTDOWN(mp));
918 xfs_trans_cancel(tp, 0);
919 error_unlock:
920 *tpp = NULL;
921 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
922 return error;
926 xfs_release(
927 xfs_inode_t *ip)
929 xfs_mount_t *mp = ip->i_mount;
930 int error;
932 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
933 return 0;
935 /* If this is a read-only mount, don't do this (would generate I/O) */
936 if (mp->m_flags & XFS_MOUNT_RDONLY)
937 return 0;
939 if (!XFS_FORCED_SHUTDOWN(mp)) {
940 int truncated;
943 * If we are using filestreams, and we have an unlinked
944 * file that we are processing the last close on, then nothing
945 * will be able to reopen and write to this file. Purge this
946 * inode from the filestreams cache so that it doesn't delay
947 * teardown of the inode.
949 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
950 xfs_filestream_deassociate(ip);
953 * If we previously truncated this file and removed old data
954 * in the process, we want to initiate "early" writeout on
955 * the last close. This is an attempt to combat the notorious
956 * NULL files problem which is particularly noticable from a
957 * truncate down, buffered (re-)write (delalloc), followed by
958 * a crash. What we are effectively doing here is
959 * significantly reducing the time window where we'd otherwise
960 * be exposed to that problem.
962 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
963 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
964 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
967 if (ip->i_d.di_nlink != 0) {
968 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
969 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
970 ip->i_delayed_blks > 0)) &&
971 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
972 (!(ip->i_d.di_flags &
973 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
976 * If we can't get the iolock just skip truncating
977 * the blocks past EOF because we could deadlock
978 * with the mmap_sem otherwise. We'll get another
979 * chance to drop them once the last reference to
980 * the inode is dropped, so we'll never leak blocks
981 * permanently.
983 error = xfs_free_eofblocks(mp, ip,
984 XFS_FREE_EOF_TRYLOCK);
985 if (error)
986 return error;
990 return 0;
994 * xfs_inactive
996 * This is called when the vnode reference count for the vnode
997 * goes to zero. If the file has been unlinked, then it must
998 * now be truncated. Also, we clear all of the read-ahead state
999 * kept for the inode here since the file is now closed.
1002 xfs_inactive(
1003 xfs_inode_t *ip)
1005 xfs_bmap_free_t free_list;
1006 xfs_fsblock_t first_block;
1007 int committed;
1008 xfs_trans_t *tp;
1009 xfs_mount_t *mp;
1010 int error;
1011 int truncate;
1014 * If the inode is already free, then there can be nothing
1015 * to clean up here.
1017 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
1018 ASSERT(ip->i_df.if_real_bytes == 0);
1019 ASSERT(ip->i_df.if_broot_bytes == 0);
1020 return VN_INACTIVE_CACHE;
1024 * Only do a truncate if it's a regular file with
1025 * some actual space in it. It's OK to look at the
1026 * inode's fields without the lock because we're the
1027 * only one with a reference to the inode.
1029 truncate = ((ip->i_d.di_nlink == 0) &&
1030 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1031 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1032 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1034 mp = ip->i_mount;
1036 error = 0;
1038 /* If this is a read-only mount, don't do this (would generate I/O) */
1039 if (mp->m_flags & XFS_MOUNT_RDONLY)
1040 goto out;
1042 if (ip->i_d.di_nlink != 0) {
1043 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1044 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1045 ip->i_delayed_blks > 0)) &&
1046 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1047 (!(ip->i_d.di_flags &
1048 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1049 (ip->i_delayed_blks != 0)))) {
1050 error = xfs_free_eofblocks(mp, ip, 0);
1051 if (error)
1052 return VN_INACTIVE_CACHE;
1054 goto out;
1057 ASSERT(ip->i_d.di_nlink == 0);
1059 error = xfs_qm_dqattach(ip, 0);
1060 if (error)
1061 return VN_INACTIVE_CACHE;
1063 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1064 if (truncate) {
1066 * Do the xfs_itruncate_start() call before
1067 * reserving any log space because itruncate_start
1068 * will call into the buffer cache and we can't
1069 * do that within a transaction.
1071 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1073 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1074 if (error) {
1075 xfs_trans_cancel(tp, 0);
1076 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1077 return VN_INACTIVE_CACHE;
1080 error = xfs_trans_reserve(tp, 0,
1081 XFS_ITRUNCATE_LOG_RES(mp),
1082 0, XFS_TRANS_PERM_LOG_RES,
1083 XFS_ITRUNCATE_LOG_COUNT);
1084 if (error) {
1085 /* Don't call itruncate_cleanup */
1086 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1087 xfs_trans_cancel(tp, 0);
1088 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1089 return VN_INACTIVE_CACHE;
1092 xfs_ilock(ip, XFS_ILOCK_EXCL);
1093 xfs_trans_ijoin(tp, ip);
1096 * normally, we have to run xfs_itruncate_finish sync.
1097 * But if filesystem is wsync and we're in the inactive
1098 * path, then we know that nlink == 0, and that the
1099 * xaction that made nlink == 0 is permanently committed
1100 * since xfs_remove runs as a synchronous transaction.
1102 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1103 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1105 if (error) {
1106 xfs_trans_cancel(tp,
1107 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1108 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1109 return VN_INACTIVE_CACHE;
1111 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1114 * If we get an error while cleaning up a
1115 * symlink we bail out.
1117 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1118 xfs_inactive_symlink_rmt(ip, &tp) :
1119 xfs_inactive_symlink_local(ip, &tp);
1121 if (error) {
1122 ASSERT(tp == NULL);
1123 return VN_INACTIVE_CACHE;
1126 xfs_trans_ijoin(tp, ip);
1127 } else {
1128 error = xfs_trans_reserve(tp, 0,
1129 XFS_IFREE_LOG_RES(mp),
1130 0, XFS_TRANS_PERM_LOG_RES,
1131 XFS_INACTIVE_LOG_COUNT);
1132 if (error) {
1133 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1134 xfs_trans_cancel(tp, 0);
1135 return VN_INACTIVE_CACHE;
1138 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1139 xfs_trans_ijoin(tp, ip);
1143 * If there are attributes associated with the file
1144 * then blow them away now. The code calls a routine
1145 * that recursively deconstructs the attribute fork.
1146 * We need to just commit the current transaction
1147 * because we can't use it for xfs_attr_inactive().
1149 if (ip->i_d.di_anextents > 0) {
1150 error = xfs_inactive_attrs(ip, &tp);
1152 * If we got an error, the transaction is already
1153 * cancelled, and the inode is unlocked. Just get out.
1155 if (error)
1156 return VN_INACTIVE_CACHE;
1157 } else if (ip->i_afp) {
1158 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1162 * Free the inode.
1164 xfs_bmap_init(&free_list, &first_block);
1165 error = xfs_ifree(tp, ip, &free_list);
1166 if (error) {
1168 * If we fail to free the inode, shut down. The cancel
1169 * might do that, we need to make sure. Otherwise the
1170 * inode might be lost for a long time or forever.
1172 if (!XFS_FORCED_SHUTDOWN(mp)) {
1173 cmn_err(CE_NOTE,
1174 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1175 error, mp->m_fsname);
1176 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1178 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1179 } else {
1181 * Credit the quota account(s). The inode is gone.
1183 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1186 * Just ignore errors at this point. There is nothing we can
1187 * do except to try to keep going. Make sure it's not a silent
1188 * error.
1190 error = xfs_bmap_finish(&tp, &free_list, &committed);
1191 if (error)
1192 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1193 "xfs_bmap_finish() returned error %d", error);
1194 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1195 if (error)
1196 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1197 "xfs_trans_commit() returned error %d", error);
1201 * Release the dquots held by inode, if any.
1203 xfs_qm_dqdetach(ip);
1204 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1206 out:
1207 return VN_INACTIVE_CACHE;
1211 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1212 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1213 * ci_name->name will point to a the actual name (caller must free) or
1214 * will be set to NULL if an exact match is found.
1217 xfs_lookup(
1218 xfs_inode_t *dp,
1219 struct xfs_name *name,
1220 xfs_inode_t **ipp,
1221 struct xfs_name *ci_name)
1223 xfs_ino_t inum;
1224 int error;
1225 uint lock_mode;
1227 trace_xfs_lookup(dp, name);
1229 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1230 return XFS_ERROR(EIO);
1232 lock_mode = xfs_ilock_map_shared(dp);
1233 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1234 xfs_iunlock_map_shared(dp, lock_mode);
1236 if (error)
1237 goto out;
1239 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
1240 if (error)
1241 goto out_free_name;
1243 return 0;
1245 out_free_name:
1246 if (ci_name)
1247 kmem_free(ci_name->name);
1248 out:
1249 *ipp = NULL;
1250 return error;
1254 xfs_create(
1255 xfs_inode_t *dp,
1256 struct xfs_name *name,
1257 mode_t mode,
1258 xfs_dev_t rdev,
1259 xfs_inode_t **ipp)
1261 int is_dir = S_ISDIR(mode);
1262 struct xfs_mount *mp = dp->i_mount;
1263 struct xfs_inode *ip = NULL;
1264 struct xfs_trans *tp = NULL;
1265 int error;
1266 xfs_bmap_free_t free_list;
1267 xfs_fsblock_t first_block;
1268 boolean_t unlock_dp_on_error = B_FALSE;
1269 uint cancel_flags;
1270 int committed;
1271 prid_t prid;
1272 struct xfs_dquot *udqp = NULL;
1273 struct xfs_dquot *gdqp = NULL;
1274 uint resblks;
1275 uint log_res;
1276 uint log_count;
1278 trace_xfs_create(dp, name);
1280 if (XFS_FORCED_SHUTDOWN(mp))
1281 return XFS_ERROR(EIO);
1283 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1284 prid = xfs_get_projid(dp);
1285 else
1286 prid = XFS_PROJID_DEFAULT;
1289 * Make sure that we have allocated dquot(s) on disk.
1291 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1292 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1293 if (error)
1294 goto std_return;
1296 if (is_dir) {
1297 rdev = 0;
1298 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1299 log_res = XFS_MKDIR_LOG_RES(mp);
1300 log_count = XFS_MKDIR_LOG_COUNT;
1301 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1302 } else {
1303 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1304 log_res = XFS_CREATE_LOG_RES(mp);
1305 log_count = XFS_CREATE_LOG_COUNT;
1306 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1309 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1312 * Initially assume that the file does not exist and
1313 * reserve the resources for that case. If that is not
1314 * the case we'll drop the one we have and get a more
1315 * appropriate transaction later.
1317 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1318 XFS_TRANS_PERM_LOG_RES, log_count);
1319 if (error == ENOSPC) {
1320 /* flush outstanding delalloc blocks and retry */
1321 xfs_flush_inodes(dp);
1322 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1323 XFS_TRANS_PERM_LOG_RES, log_count);
1325 if (error == ENOSPC) {
1326 /* No space at all so try a "no-allocation" reservation */
1327 resblks = 0;
1328 error = xfs_trans_reserve(tp, 0, log_res, 0,
1329 XFS_TRANS_PERM_LOG_RES, log_count);
1331 if (error) {
1332 cancel_flags = 0;
1333 goto out_trans_cancel;
1336 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1337 unlock_dp_on_error = B_TRUE;
1340 * Check for directory link count overflow.
1342 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1343 error = XFS_ERROR(EMLINK);
1344 goto out_trans_cancel;
1347 xfs_bmap_init(&free_list, &first_block);
1350 * Reserve disk quota and the inode.
1352 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
1353 if (error)
1354 goto out_trans_cancel;
1356 error = xfs_dir_canenter(tp, dp, name, resblks);
1357 if (error)
1358 goto out_trans_cancel;
1361 * A newly created regular or special file just has one directory
1362 * entry pointing to them, but a directory also the "." entry
1363 * pointing to itself.
1365 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
1366 prid, resblks > 0, &ip, &committed);
1367 if (error) {
1368 if (error == ENOSPC)
1369 goto out_trans_cancel;
1370 goto out_trans_abort;
1374 * At this point, we've gotten a newly allocated inode.
1375 * It is locked (and joined to the transaction).
1377 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1380 * Now we join the directory inode to the transaction. We do not do it
1381 * earlier because xfs_dir_ialloc might commit the previous transaction
1382 * (and release all the locks). An error from here on will result in
1383 * the transaction cancel unlocking dp so don't do it explicitly in the
1384 * error path.
1386 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
1387 unlock_dp_on_error = B_FALSE;
1389 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1390 &first_block, &free_list, resblks ?
1391 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1392 if (error) {
1393 ASSERT(error != ENOSPC);
1394 goto out_trans_abort;
1396 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1397 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1399 if (is_dir) {
1400 error = xfs_dir_init(tp, ip, dp);
1401 if (error)
1402 goto out_bmap_cancel;
1404 error = xfs_bumplink(tp, dp);
1405 if (error)
1406 goto out_bmap_cancel;
1410 * If this is a synchronous mount, make sure that the
1411 * create transaction goes to disk before returning to
1412 * the user.
1414 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1415 xfs_trans_set_sync(tp);
1418 * Attach the dquot(s) to the inodes and modify them incore.
1419 * These ids of the inode couldn't have changed since the new
1420 * inode has been locked ever since it was created.
1422 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
1425 * xfs_trans_commit normally decrements the vnode ref count
1426 * when it unlocks the inode. Since we want to return the
1427 * vnode to the caller, we bump the vnode ref count now.
1429 IHOLD(ip);
1431 error = xfs_bmap_finish(&tp, &free_list, &committed);
1432 if (error)
1433 goto out_abort_rele;
1435 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1436 if (error) {
1437 IRELE(ip);
1438 goto out_dqrele;
1441 xfs_qm_dqrele(udqp);
1442 xfs_qm_dqrele(gdqp);
1444 *ipp = ip;
1445 return 0;
1447 out_bmap_cancel:
1448 xfs_bmap_cancel(&free_list);
1449 out_trans_abort:
1450 cancel_flags |= XFS_TRANS_ABORT;
1451 out_trans_cancel:
1452 xfs_trans_cancel(tp, cancel_flags);
1453 out_dqrele:
1454 xfs_qm_dqrele(udqp);
1455 xfs_qm_dqrele(gdqp);
1457 if (unlock_dp_on_error)
1458 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1459 std_return:
1460 return error;
1462 out_abort_rele:
1464 * Wait until after the current transaction is aborted to
1465 * release the inode. This prevents recursive transactions
1466 * and deadlocks from xfs_inactive.
1468 xfs_bmap_cancel(&free_list);
1469 cancel_flags |= XFS_TRANS_ABORT;
1470 xfs_trans_cancel(tp, cancel_flags);
1471 IRELE(ip);
1472 unlock_dp_on_error = B_FALSE;
1473 goto out_dqrele;
1476 #ifdef DEBUG
1477 int xfs_locked_n;
1478 int xfs_small_retries;
1479 int xfs_middle_retries;
1480 int xfs_lots_retries;
1481 int xfs_lock_delays;
1482 #endif
1485 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1486 * a different value
1488 static inline int
1489 xfs_lock_inumorder(int lock_mode, int subclass)
1491 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1492 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1493 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1494 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1496 return lock_mode;
1500 * The following routine will lock n inodes in exclusive mode.
1501 * We assume the caller calls us with the inodes in i_ino order.
1503 * We need to detect deadlock where an inode that we lock
1504 * is in the AIL and we start waiting for another inode that is locked
1505 * by a thread in a long running transaction (such as truncate). This can
1506 * result in deadlock since the long running trans might need to wait
1507 * for the inode we just locked in order to push the tail and free space
1508 * in the log.
1510 void
1511 xfs_lock_inodes(
1512 xfs_inode_t **ips,
1513 int inodes,
1514 uint lock_mode)
1516 int attempts = 0, i, j, try_lock;
1517 xfs_log_item_t *lp;
1519 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1521 try_lock = 0;
1522 i = 0;
1524 again:
1525 for (; i < inodes; i++) {
1526 ASSERT(ips[i]);
1528 if (i && (ips[i] == ips[i-1])) /* Already locked */
1529 continue;
1532 * If try_lock is not set yet, make sure all locked inodes
1533 * are not in the AIL.
1534 * If any are, set try_lock to be used later.
1537 if (!try_lock) {
1538 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1539 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1540 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1541 try_lock++;
1547 * If any of the previous locks we have locked is in the AIL,
1548 * we must TRY to get the second and subsequent locks. If
1549 * we can't get any, we must release all we have
1550 * and try again.
1553 if (try_lock) {
1554 /* try_lock must be 0 if i is 0. */
1556 * try_lock means we have an inode locked
1557 * that is in the AIL.
1559 ASSERT(i != 0);
1560 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1561 attempts++;
1564 * Unlock all previous guys and try again.
1565 * xfs_iunlock will try to push the tail
1566 * if the inode is in the AIL.
1569 for(j = i - 1; j >= 0; j--) {
1572 * Check to see if we've already
1573 * unlocked this one.
1574 * Not the first one going back,
1575 * and the inode ptr is the same.
1577 if ((j != (i - 1)) && ips[j] ==
1578 ips[j+1])
1579 continue;
1581 xfs_iunlock(ips[j], lock_mode);
1584 if ((attempts % 5) == 0) {
1585 delay(1); /* Don't just spin the CPU */
1586 #ifdef DEBUG
1587 xfs_lock_delays++;
1588 #endif
1590 i = 0;
1591 try_lock = 0;
1592 goto again;
1594 } else {
1595 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1599 #ifdef DEBUG
1600 if (attempts) {
1601 if (attempts < 5) xfs_small_retries++;
1602 else if (attempts < 100) xfs_middle_retries++;
1603 else xfs_lots_retries++;
1604 } else {
1605 xfs_locked_n++;
1607 #endif
1611 * xfs_lock_two_inodes() can only be used to lock one type of lock
1612 * at a time - the iolock or the ilock, but not both at once. If
1613 * we lock both at once, lockdep will report false positives saying
1614 * we have violated locking orders.
1616 void
1617 xfs_lock_two_inodes(
1618 xfs_inode_t *ip0,
1619 xfs_inode_t *ip1,
1620 uint lock_mode)
1622 xfs_inode_t *temp;
1623 int attempts = 0;
1624 xfs_log_item_t *lp;
1626 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1627 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1628 ASSERT(ip0->i_ino != ip1->i_ino);
1630 if (ip0->i_ino > ip1->i_ino) {
1631 temp = ip0;
1632 ip0 = ip1;
1633 ip1 = temp;
1636 again:
1637 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1640 * If the first lock we have locked is in the AIL, we must TRY to get
1641 * the second lock. If we can't get it, we must release the first one
1642 * and try again.
1644 lp = (xfs_log_item_t *)ip0->i_itemp;
1645 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1646 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1647 xfs_iunlock(ip0, lock_mode);
1648 if ((++attempts % 5) == 0)
1649 delay(1); /* Don't just spin the CPU */
1650 goto again;
1652 } else {
1653 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1658 xfs_remove(
1659 xfs_inode_t *dp,
1660 struct xfs_name *name,
1661 xfs_inode_t *ip)
1663 xfs_mount_t *mp = dp->i_mount;
1664 xfs_trans_t *tp = NULL;
1665 int is_dir = S_ISDIR(ip->i_d.di_mode);
1666 int error = 0;
1667 xfs_bmap_free_t free_list;
1668 xfs_fsblock_t first_block;
1669 int cancel_flags;
1670 int committed;
1671 int link_zero;
1672 uint resblks;
1673 uint log_count;
1675 trace_xfs_remove(dp, name);
1677 if (XFS_FORCED_SHUTDOWN(mp))
1678 return XFS_ERROR(EIO);
1680 error = xfs_qm_dqattach(dp, 0);
1681 if (error)
1682 goto std_return;
1684 error = xfs_qm_dqattach(ip, 0);
1685 if (error)
1686 goto std_return;
1688 if (is_dir) {
1689 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1690 log_count = XFS_DEFAULT_LOG_COUNT;
1691 } else {
1692 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1693 log_count = XFS_REMOVE_LOG_COUNT;
1695 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1698 * We try to get the real space reservation first,
1699 * allowing for directory btree deletion(s) implying
1700 * possible bmap insert(s). If we can't get the space
1701 * reservation then we use 0 instead, and avoid the bmap
1702 * btree insert(s) in the directory code by, if the bmap
1703 * insert tries to happen, instead trimming the LAST
1704 * block from the directory.
1706 resblks = XFS_REMOVE_SPACE_RES(mp);
1707 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1708 XFS_TRANS_PERM_LOG_RES, log_count);
1709 if (error == ENOSPC) {
1710 resblks = 0;
1711 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1712 XFS_TRANS_PERM_LOG_RES, log_count);
1714 if (error) {
1715 ASSERT(error != ENOSPC);
1716 cancel_flags = 0;
1717 goto out_trans_cancel;
1720 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1722 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
1723 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
1726 * If we're removing a directory perform some additional validation.
1728 if (is_dir) {
1729 ASSERT(ip->i_d.di_nlink >= 2);
1730 if (ip->i_d.di_nlink != 2) {
1731 error = XFS_ERROR(ENOTEMPTY);
1732 goto out_trans_cancel;
1734 if (!xfs_dir_isempty(ip)) {
1735 error = XFS_ERROR(ENOTEMPTY);
1736 goto out_trans_cancel;
1740 xfs_bmap_init(&free_list, &first_block);
1741 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1742 &first_block, &free_list, resblks);
1743 if (error) {
1744 ASSERT(error != ENOENT);
1745 goto out_bmap_cancel;
1747 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1749 if (is_dir) {
1751 * Drop the link from ip's "..".
1753 error = xfs_droplink(tp, dp);
1754 if (error)
1755 goto out_bmap_cancel;
1758 * Drop the "." link from ip to self.
1760 error = xfs_droplink(tp, ip);
1761 if (error)
1762 goto out_bmap_cancel;
1763 } else {
1765 * When removing a non-directory we need to log the parent
1766 * inode here. For a directory this is done implicitly
1767 * by the xfs_droplink call for the ".." entry.
1769 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1773 * Drop the link from dp to ip.
1775 error = xfs_droplink(tp, ip);
1776 if (error)
1777 goto out_bmap_cancel;
1780 * Determine if this is the last link while
1781 * we are in the transaction.
1783 link_zero = (ip->i_d.di_nlink == 0);
1786 * If this is a synchronous mount, make sure that the
1787 * remove transaction goes to disk before returning to
1788 * the user.
1790 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1791 xfs_trans_set_sync(tp);
1793 error = xfs_bmap_finish(&tp, &free_list, &committed);
1794 if (error)
1795 goto out_bmap_cancel;
1797 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1798 if (error)
1799 goto std_return;
1802 * If we are using filestreams, kill the stream association.
1803 * If the file is still open it may get a new one but that
1804 * will get killed on last close in xfs_close() so we don't
1805 * have to worry about that.
1807 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
1808 xfs_filestream_deassociate(ip);
1810 return 0;
1812 out_bmap_cancel:
1813 xfs_bmap_cancel(&free_list);
1814 cancel_flags |= XFS_TRANS_ABORT;
1815 out_trans_cancel:
1816 xfs_trans_cancel(tp, cancel_flags);
1817 std_return:
1818 return error;
1822 xfs_link(
1823 xfs_inode_t *tdp,
1824 xfs_inode_t *sip,
1825 struct xfs_name *target_name)
1827 xfs_mount_t *mp = tdp->i_mount;
1828 xfs_trans_t *tp;
1829 int error;
1830 xfs_bmap_free_t free_list;
1831 xfs_fsblock_t first_block;
1832 int cancel_flags;
1833 int committed;
1834 int resblks;
1836 trace_xfs_link(tdp, target_name);
1838 ASSERT(!S_ISDIR(sip->i_d.di_mode));
1840 if (XFS_FORCED_SHUTDOWN(mp))
1841 return XFS_ERROR(EIO);
1843 error = xfs_qm_dqattach(sip, 0);
1844 if (error)
1845 goto std_return;
1847 error = xfs_qm_dqattach(tdp, 0);
1848 if (error)
1849 goto std_return;
1851 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1852 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1853 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1854 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1855 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1856 if (error == ENOSPC) {
1857 resblks = 0;
1858 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1859 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1861 if (error) {
1862 cancel_flags = 0;
1863 goto error_return;
1866 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1868 xfs_trans_ijoin_ref(tp, sip, XFS_ILOCK_EXCL);
1869 xfs_trans_ijoin_ref(tp, tdp, XFS_ILOCK_EXCL);
1872 * If the source has too many links, we can't make any more to it.
1874 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
1875 error = XFS_ERROR(EMLINK);
1876 goto error_return;
1880 * If we are using project inheritance, we only allow hard link
1881 * creation in our tree when the project IDs are the same; else
1882 * the tree quota mechanism could be circumvented.
1884 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1885 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
1886 error = XFS_ERROR(EXDEV);
1887 goto error_return;
1890 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1891 if (error)
1892 goto error_return;
1894 xfs_bmap_init(&free_list, &first_block);
1896 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1897 &first_block, &free_list, resblks);
1898 if (error)
1899 goto abort_return;
1900 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1901 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1903 error = xfs_bumplink(tp, sip);
1904 if (error)
1905 goto abort_return;
1908 * If this is a synchronous mount, make sure that the
1909 * link transaction goes to disk before returning to
1910 * the user.
1912 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1913 xfs_trans_set_sync(tp);
1916 error = xfs_bmap_finish (&tp, &free_list, &committed);
1917 if (error) {
1918 xfs_bmap_cancel(&free_list);
1919 goto abort_return;
1922 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1924 abort_return:
1925 cancel_flags |= XFS_TRANS_ABORT;
1926 error_return:
1927 xfs_trans_cancel(tp, cancel_flags);
1928 std_return:
1929 return error;
1933 xfs_symlink(
1934 xfs_inode_t *dp,
1935 struct xfs_name *link_name,
1936 const char *target_path,
1937 mode_t mode,
1938 xfs_inode_t **ipp)
1940 xfs_mount_t *mp = dp->i_mount;
1941 xfs_trans_t *tp;
1942 xfs_inode_t *ip;
1943 int error;
1944 int pathlen;
1945 xfs_bmap_free_t free_list;
1946 xfs_fsblock_t first_block;
1947 boolean_t unlock_dp_on_error = B_FALSE;
1948 uint cancel_flags;
1949 int committed;
1950 xfs_fileoff_t first_fsb;
1951 xfs_filblks_t fs_blocks;
1952 int nmaps;
1953 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1954 xfs_daddr_t d;
1955 const char *cur_chunk;
1956 int byte_cnt;
1957 int n;
1958 xfs_buf_t *bp;
1959 prid_t prid;
1960 struct xfs_dquot *udqp, *gdqp;
1961 uint resblks;
1963 *ipp = NULL;
1964 error = 0;
1965 ip = NULL;
1966 tp = NULL;
1968 trace_xfs_symlink(dp, link_name);
1970 if (XFS_FORCED_SHUTDOWN(mp))
1971 return XFS_ERROR(EIO);
1974 * Check component lengths of the target path name.
1976 pathlen = strlen(target_path);
1977 if (pathlen >= MAXPATHLEN) /* total string too long */
1978 return XFS_ERROR(ENAMETOOLONG);
1980 udqp = gdqp = NULL;
1981 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1982 prid = xfs_get_projid(dp);
1983 else
1984 prid = XFS_PROJID_DEFAULT;
1987 * Make sure that we have allocated dquot(s) on disk.
1989 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1990 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1991 if (error)
1992 goto std_return;
1994 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
1995 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1997 * The symlink will fit into the inode data fork?
1998 * There can't be any attributes so we get the whole variable part.
2000 if (pathlen <= XFS_LITINO(mp))
2001 fs_blocks = 0;
2002 else
2003 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2004 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2005 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2006 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2007 if (error == ENOSPC && fs_blocks == 0) {
2008 resblks = 0;
2009 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2010 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2012 if (error) {
2013 cancel_flags = 0;
2014 goto error_return;
2017 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2018 unlock_dp_on_error = B_TRUE;
2021 * Check whether the directory allows new symlinks or not.
2023 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2024 error = XFS_ERROR(EPERM);
2025 goto error_return;
2029 * Reserve disk quota : blocks and inode.
2031 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
2032 if (error)
2033 goto error_return;
2036 * Check for ability to enter directory entry, if no space reserved.
2038 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2039 if (error)
2040 goto error_return;
2042 * Initialize the bmap freelist prior to calling either
2043 * bmapi or the directory create code.
2045 xfs_bmap_init(&free_list, &first_block);
2048 * Allocate an inode for the symlink.
2050 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
2051 prid, resblks > 0, &ip, NULL);
2052 if (error) {
2053 if (error == ENOSPC)
2054 goto error_return;
2055 goto error1;
2059 * An error after we've joined dp to the transaction will result in the
2060 * transaction cancel unlocking dp so don't do it explicitly in the
2061 * error path.
2063 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
2064 unlock_dp_on_error = B_FALSE;
2067 * Also attach the dquot(s) to it, if applicable.
2069 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
2071 if (resblks)
2072 resblks -= XFS_IALLOC_SPACE_RES(mp);
2074 * If the symlink will fit into the inode, write it inline.
2076 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2077 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2078 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2079 ip->i_d.di_size = pathlen;
2082 * The inode was initially created in extent format.
2084 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2085 ip->i_df.if_flags |= XFS_IFINLINE;
2087 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2088 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2090 } else {
2091 first_fsb = 0;
2092 nmaps = SYMLINK_MAPS;
2094 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2095 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2096 &first_block, resblks, mval, &nmaps,
2097 &free_list);
2098 if (error) {
2099 goto error1;
2102 if (resblks)
2103 resblks -= fs_blocks;
2104 ip->i_d.di_size = pathlen;
2105 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2107 cur_chunk = target_path;
2108 for (n = 0; n < nmaps; n++) {
2109 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2110 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2111 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2112 BTOBB(byte_cnt), 0);
2113 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2114 if (pathlen < byte_cnt) {
2115 byte_cnt = pathlen;
2117 pathlen -= byte_cnt;
2119 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2120 cur_chunk += byte_cnt;
2122 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2127 * Create the directory entry for the symlink.
2129 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2130 &first_block, &free_list, resblks);
2131 if (error)
2132 goto error1;
2133 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2134 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2137 * If this is a synchronous mount, make sure that the
2138 * symlink transaction goes to disk before returning to
2139 * the user.
2141 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2142 xfs_trans_set_sync(tp);
2146 * xfs_trans_commit normally decrements the vnode ref count
2147 * when it unlocks the inode. Since we want to return the
2148 * vnode to the caller, we bump the vnode ref count now.
2150 IHOLD(ip);
2152 error = xfs_bmap_finish(&tp, &free_list, &committed);
2153 if (error) {
2154 goto error2;
2156 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2157 xfs_qm_dqrele(udqp);
2158 xfs_qm_dqrele(gdqp);
2160 *ipp = ip;
2161 return 0;
2163 error2:
2164 IRELE(ip);
2165 error1:
2166 xfs_bmap_cancel(&free_list);
2167 cancel_flags |= XFS_TRANS_ABORT;
2168 error_return:
2169 xfs_trans_cancel(tp, cancel_flags);
2170 xfs_qm_dqrele(udqp);
2171 xfs_qm_dqrele(gdqp);
2173 if (unlock_dp_on_error)
2174 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2175 std_return:
2176 return error;
2180 xfs_set_dmattrs(
2181 xfs_inode_t *ip,
2182 u_int evmask,
2183 u_int16_t state)
2185 xfs_mount_t *mp = ip->i_mount;
2186 xfs_trans_t *tp;
2187 int error;
2189 if (!capable(CAP_SYS_ADMIN))
2190 return XFS_ERROR(EPERM);
2192 if (XFS_FORCED_SHUTDOWN(mp))
2193 return XFS_ERROR(EIO);
2195 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2196 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2197 if (error) {
2198 xfs_trans_cancel(tp, 0);
2199 return error;
2201 xfs_ilock(ip, XFS_ILOCK_EXCL);
2202 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
2204 ip->i_d.di_dmevmask = evmask;
2205 ip->i_d.di_dmstate = state;
2207 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2208 error = xfs_trans_commit(tp, 0);
2210 return error;
2214 * xfs_alloc_file_space()
2215 * This routine allocates disk space for the given file.
2217 * If alloc_type == 0, this request is for an ALLOCSP type
2218 * request which will change the file size. In this case, no
2219 * DMAPI event will be generated by the call. A TRUNCATE event
2220 * will be generated later by xfs_setattr.
2222 * If alloc_type != 0, this request is for a RESVSP type
2223 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2224 * lower block boundary byte address is less than the file's
2225 * length.
2227 * RETURNS:
2228 * 0 on success
2229 * errno on error
2232 STATIC int
2233 xfs_alloc_file_space(
2234 xfs_inode_t *ip,
2235 xfs_off_t offset,
2236 xfs_off_t len,
2237 int alloc_type,
2238 int attr_flags)
2240 xfs_mount_t *mp = ip->i_mount;
2241 xfs_off_t count;
2242 xfs_filblks_t allocated_fsb;
2243 xfs_filblks_t allocatesize_fsb;
2244 xfs_extlen_t extsz, temp;
2245 xfs_fileoff_t startoffset_fsb;
2246 xfs_fsblock_t firstfsb;
2247 int nimaps;
2248 int bmapi_flag;
2249 int quota_flag;
2250 int rt;
2251 xfs_trans_t *tp;
2252 xfs_bmbt_irec_t imaps[1], *imapp;
2253 xfs_bmap_free_t free_list;
2254 uint qblocks, resblks, resrtextents;
2255 int committed;
2256 int error;
2258 trace_xfs_alloc_file_space(ip);
2260 if (XFS_FORCED_SHUTDOWN(mp))
2261 return XFS_ERROR(EIO);
2263 error = xfs_qm_dqattach(ip, 0);
2264 if (error)
2265 return error;
2267 if (len <= 0)
2268 return XFS_ERROR(EINVAL);
2270 rt = XFS_IS_REALTIME_INODE(ip);
2271 extsz = xfs_get_extsz_hint(ip);
2273 count = len;
2274 imapp = &imaps[0];
2275 nimaps = 1;
2276 bmapi_flag = XFS_BMAPI_WRITE | alloc_type;
2277 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2278 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2281 * Allocate file space until done or until there is an error
2283 while (allocatesize_fsb && !error) {
2284 xfs_fileoff_t s, e;
2287 * Determine space reservations for data/realtime.
2289 if (unlikely(extsz)) {
2290 s = startoffset_fsb;
2291 do_div(s, extsz);
2292 s *= extsz;
2293 e = startoffset_fsb + allocatesize_fsb;
2294 if ((temp = do_mod(startoffset_fsb, extsz)))
2295 e += temp;
2296 if ((temp = do_mod(e, extsz)))
2297 e += extsz - temp;
2298 } else {
2299 s = 0;
2300 e = allocatesize_fsb;
2304 * The transaction reservation is limited to a 32-bit block
2305 * count, hence we need to limit the number of blocks we are
2306 * trying to reserve to avoid an overflow. We can't allocate
2307 * more than @nimaps extents, and an extent is limited on disk
2308 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
2310 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
2311 if (unlikely(rt)) {
2312 resrtextents = qblocks = resblks;
2313 resrtextents /= mp->m_sb.sb_rextsize;
2314 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2315 quota_flag = XFS_QMOPT_RES_RTBLKS;
2316 } else {
2317 resrtextents = 0;
2318 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
2319 quota_flag = XFS_QMOPT_RES_REGBLKS;
2323 * Allocate and setup the transaction.
2325 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2326 error = xfs_trans_reserve(tp, resblks,
2327 XFS_WRITE_LOG_RES(mp), resrtextents,
2328 XFS_TRANS_PERM_LOG_RES,
2329 XFS_WRITE_LOG_COUNT);
2331 * Check for running out of space
2333 if (error) {
2335 * Free the transaction structure.
2337 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2338 xfs_trans_cancel(tp, 0);
2339 break;
2341 xfs_ilock(ip, XFS_ILOCK_EXCL);
2342 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2343 0, quota_flag);
2344 if (error)
2345 goto error1;
2347 xfs_trans_ijoin(tp, ip);
2350 * Issue the xfs_bmapi() call to allocate the blocks
2352 xfs_bmap_init(&free_list, &firstfsb);
2353 error = xfs_bmapi(tp, ip, startoffset_fsb,
2354 allocatesize_fsb, bmapi_flag,
2355 &firstfsb, 0, imapp, &nimaps,
2356 &free_list);
2357 if (error) {
2358 goto error0;
2362 * Complete the transaction
2364 error = xfs_bmap_finish(&tp, &free_list, &committed);
2365 if (error) {
2366 goto error0;
2369 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2370 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2371 if (error) {
2372 break;
2375 allocated_fsb = imapp->br_blockcount;
2377 if (nimaps == 0) {
2378 error = XFS_ERROR(ENOSPC);
2379 break;
2382 startoffset_fsb += allocated_fsb;
2383 allocatesize_fsb -= allocated_fsb;
2386 return error;
2388 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
2389 xfs_bmap_cancel(&free_list);
2390 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
2392 error1: /* Just cancel transaction */
2393 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2394 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2395 return error;
2399 * Zero file bytes between startoff and endoff inclusive.
2400 * The iolock is held exclusive and no blocks are buffered.
2402 * This function is used by xfs_free_file_space() to zero
2403 * partial blocks when the range to free is not block aligned.
2404 * When unreserving space with boundaries that are not block
2405 * aligned we round up the start and round down the end
2406 * boundaries and then use this function to zero the parts of
2407 * the blocks that got dropped during the rounding.
2409 STATIC int
2410 xfs_zero_remaining_bytes(
2411 xfs_inode_t *ip,
2412 xfs_off_t startoff,
2413 xfs_off_t endoff)
2415 xfs_bmbt_irec_t imap;
2416 xfs_fileoff_t offset_fsb;
2417 xfs_off_t lastoffset;
2418 xfs_off_t offset;
2419 xfs_buf_t *bp;
2420 xfs_mount_t *mp = ip->i_mount;
2421 int nimap;
2422 int error = 0;
2425 * Avoid doing I/O beyond eof - it's not necessary
2426 * since nothing can read beyond eof. The space will
2427 * be zeroed when the file is extended anyway.
2429 if (startoff >= ip->i_size)
2430 return 0;
2432 if (endoff > ip->i_size)
2433 endoff = ip->i_size;
2435 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
2436 mp->m_rtdev_targp : mp->m_ddev_targp,
2437 mp->m_sb.sb_blocksize, XBF_DONT_BLOCK);
2438 if (!bp)
2439 return XFS_ERROR(ENOMEM);
2441 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2442 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2443 nimap = 1;
2444 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
2445 NULL, 0, &imap, &nimap, NULL);
2446 if (error || nimap < 1)
2447 break;
2448 ASSERT(imap.br_blockcount >= 1);
2449 ASSERT(imap.br_startoff == offset_fsb);
2450 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2451 if (lastoffset > endoff)
2452 lastoffset = endoff;
2453 if (imap.br_startblock == HOLESTARTBLOCK)
2454 continue;
2455 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2456 if (imap.br_state == XFS_EXT_UNWRITTEN)
2457 continue;
2458 XFS_BUF_UNDONE(bp);
2459 XFS_BUF_UNWRITE(bp);
2460 XFS_BUF_READ(bp);
2461 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
2462 xfsbdstrat(mp, bp);
2463 error = xfs_buf_iowait(bp);
2464 if (error) {
2465 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2466 mp, bp, XFS_BUF_ADDR(bp));
2467 break;
2469 memset(XFS_BUF_PTR(bp) +
2470 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2471 0, lastoffset - offset + 1);
2472 XFS_BUF_UNDONE(bp);
2473 XFS_BUF_UNREAD(bp);
2474 XFS_BUF_WRITE(bp);
2475 xfsbdstrat(mp, bp);
2476 error = xfs_buf_iowait(bp);
2477 if (error) {
2478 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2479 mp, bp, XFS_BUF_ADDR(bp));
2480 break;
2483 xfs_buf_free(bp);
2484 return error;
2488 * xfs_free_file_space()
2489 * This routine frees disk space for the given file.
2491 * This routine is only called by xfs_change_file_space
2492 * for an UNRESVSP type call.
2494 * RETURNS:
2495 * 0 on success
2496 * errno on error
2499 STATIC int
2500 xfs_free_file_space(
2501 xfs_inode_t *ip,
2502 xfs_off_t offset,
2503 xfs_off_t len,
2504 int attr_flags)
2506 int committed;
2507 int done;
2508 xfs_fileoff_t endoffset_fsb;
2509 int error;
2510 xfs_fsblock_t firstfsb;
2511 xfs_bmap_free_t free_list;
2512 xfs_bmbt_irec_t imap;
2513 xfs_off_t ioffset;
2514 xfs_extlen_t mod=0;
2515 xfs_mount_t *mp;
2516 int nimap;
2517 uint resblks;
2518 uint rounding;
2519 int rt;
2520 xfs_fileoff_t startoffset_fsb;
2521 xfs_trans_t *tp;
2522 int need_iolock = 1;
2524 mp = ip->i_mount;
2526 trace_xfs_free_file_space(ip);
2528 error = xfs_qm_dqattach(ip, 0);
2529 if (error)
2530 return error;
2532 error = 0;
2533 if (len <= 0) /* if nothing being freed */
2534 return error;
2535 rt = XFS_IS_REALTIME_INODE(ip);
2536 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2537 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
2539 if (attr_flags & XFS_ATTR_NOLOCK)
2540 need_iolock = 0;
2541 if (need_iolock) {
2542 xfs_ilock(ip, XFS_IOLOCK_EXCL);
2543 /* wait for the completion of any pending DIOs */
2544 xfs_ioend_wait(ip);
2547 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
2548 ioffset = offset & ~(rounding - 1);
2550 if (VN_CACHED(VFS_I(ip)) != 0) {
2551 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
2552 if (error)
2553 goto out_unlock_iolock;
2557 * Need to zero the stuff we're not freeing, on disk.
2558 * If it's a realtime file & can't use unwritten extents then we
2559 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2560 * will take care of it for us.
2562 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
2563 nimap = 1;
2564 error = xfs_bmapi(NULL, ip, startoffset_fsb,
2565 1, 0, NULL, 0, &imap, &nimap, NULL);
2566 if (error)
2567 goto out_unlock_iolock;
2568 ASSERT(nimap == 0 || nimap == 1);
2569 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2570 xfs_daddr_t block;
2572 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2573 block = imap.br_startblock;
2574 mod = do_div(block, mp->m_sb.sb_rextsize);
2575 if (mod)
2576 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2578 nimap = 1;
2579 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
2580 1, 0, NULL, 0, &imap, &nimap, NULL);
2581 if (error)
2582 goto out_unlock_iolock;
2583 ASSERT(nimap == 0 || nimap == 1);
2584 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2585 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2586 mod++;
2587 if (mod && (mod != mp->m_sb.sb_rextsize))
2588 endoffset_fsb -= mod;
2591 if ((done = (endoffset_fsb <= startoffset_fsb)))
2593 * One contiguous piece to clear
2595 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2596 else {
2598 * Some full blocks, possibly two pieces to clear
2600 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2601 error = xfs_zero_remaining_bytes(ip, offset,
2602 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2603 if (!error &&
2604 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2605 error = xfs_zero_remaining_bytes(ip,
2606 XFS_FSB_TO_B(mp, endoffset_fsb),
2607 offset + len - 1);
2611 * free file space until done or until there is an error
2613 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2614 while (!error && !done) {
2617 * allocate and setup the transaction. Allow this
2618 * transaction to dip into the reserve blocks to ensure
2619 * the freeing of the space succeeds at ENOSPC.
2621 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2622 tp->t_flags |= XFS_TRANS_RESERVE;
2623 error = xfs_trans_reserve(tp,
2624 resblks,
2625 XFS_WRITE_LOG_RES(mp),
2627 XFS_TRANS_PERM_LOG_RES,
2628 XFS_WRITE_LOG_COUNT);
2631 * check for running out of space
2633 if (error) {
2635 * Free the transaction structure.
2637 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2638 xfs_trans_cancel(tp, 0);
2639 break;
2641 xfs_ilock(ip, XFS_ILOCK_EXCL);
2642 error = xfs_trans_reserve_quota(tp, mp,
2643 ip->i_udquot, ip->i_gdquot,
2644 resblks, 0, XFS_QMOPT_RES_REGBLKS);
2645 if (error)
2646 goto error1;
2648 xfs_trans_ijoin(tp, ip);
2651 * issue the bunmapi() call to free the blocks
2653 xfs_bmap_init(&free_list, &firstfsb);
2654 error = xfs_bunmapi(tp, ip, startoffset_fsb,
2655 endoffset_fsb - startoffset_fsb,
2656 0, 2, &firstfsb, &free_list, &done);
2657 if (error) {
2658 goto error0;
2662 * complete the transaction
2664 error = xfs_bmap_finish(&tp, &free_list, &committed);
2665 if (error) {
2666 goto error0;
2669 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2670 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2673 out_unlock_iolock:
2674 if (need_iolock)
2675 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2676 return error;
2678 error0:
2679 xfs_bmap_cancel(&free_list);
2680 error1:
2681 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2682 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2683 XFS_ILOCK_EXCL);
2684 return error;
2688 * xfs_change_file_space()
2689 * This routine allocates or frees disk space for the given file.
2690 * The user specified parameters are checked for alignment and size
2691 * limitations.
2693 * RETURNS:
2694 * 0 on success
2695 * errno on error
2699 xfs_change_file_space(
2700 xfs_inode_t *ip,
2701 int cmd,
2702 xfs_flock64_t *bf,
2703 xfs_off_t offset,
2704 int attr_flags)
2706 xfs_mount_t *mp = ip->i_mount;
2707 int clrprealloc;
2708 int error;
2709 xfs_fsize_t fsize;
2710 int setprealloc;
2711 xfs_off_t startoffset;
2712 xfs_off_t llen;
2713 xfs_trans_t *tp;
2714 struct iattr iattr;
2715 int prealloc_type;
2717 if (!S_ISREG(ip->i_d.di_mode))
2718 return XFS_ERROR(EINVAL);
2720 switch (bf->l_whence) {
2721 case 0: /*SEEK_SET*/
2722 break;
2723 case 1: /*SEEK_CUR*/
2724 bf->l_start += offset;
2725 break;
2726 case 2: /*SEEK_END*/
2727 bf->l_start += ip->i_size;
2728 break;
2729 default:
2730 return XFS_ERROR(EINVAL);
2733 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2735 if ( (bf->l_start < 0)
2736 || (bf->l_start > XFS_MAXIOFFSET(mp))
2737 || (bf->l_start + llen < 0)
2738 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
2739 return XFS_ERROR(EINVAL);
2741 bf->l_whence = 0;
2743 startoffset = bf->l_start;
2744 fsize = ip->i_size;
2747 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2748 * file space.
2749 * These calls do NOT zero the data space allocated to the file,
2750 * nor do they change the file size.
2752 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2753 * space.
2754 * These calls cause the new file data to be zeroed and the file
2755 * size to be changed.
2757 setprealloc = clrprealloc = 0;
2758 prealloc_type = XFS_BMAPI_PREALLOC;
2760 switch (cmd) {
2761 case XFS_IOC_ZERO_RANGE:
2762 prealloc_type |= XFS_BMAPI_CONVERT;
2763 xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
2764 /* FALLTHRU */
2765 case XFS_IOC_RESVSP:
2766 case XFS_IOC_RESVSP64:
2767 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
2768 prealloc_type, attr_flags);
2769 if (error)
2770 return error;
2771 setprealloc = 1;
2772 break;
2774 case XFS_IOC_UNRESVSP:
2775 case XFS_IOC_UNRESVSP64:
2776 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2777 attr_flags)))
2778 return error;
2779 break;
2781 case XFS_IOC_ALLOCSP:
2782 case XFS_IOC_ALLOCSP64:
2783 case XFS_IOC_FREESP:
2784 case XFS_IOC_FREESP64:
2785 if (startoffset > fsize) {
2786 error = xfs_alloc_file_space(ip, fsize,
2787 startoffset - fsize, 0, attr_flags);
2788 if (error)
2789 break;
2792 iattr.ia_valid = ATTR_SIZE;
2793 iattr.ia_size = startoffset;
2795 error = xfs_setattr(ip, &iattr, attr_flags);
2797 if (error)
2798 return error;
2800 clrprealloc = 1;
2801 break;
2803 default:
2804 ASSERT(0);
2805 return XFS_ERROR(EINVAL);
2809 * update the inode timestamp, mode, and prealloc flag bits
2811 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2813 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2814 0, 0, 0))) {
2815 /* ASSERT(0); */
2816 xfs_trans_cancel(tp, 0);
2817 return error;
2820 xfs_ilock(ip, XFS_ILOCK_EXCL);
2822 xfs_trans_ijoin(tp, ip);
2824 if ((attr_flags & XFS_ATTR_DMI) == 0) {
2825 ip->i_d.di_mode &= ~S_ISUID;
2828 * Note that we don't have to worry about mandatory
2829 * file locking being disabled here because we only
2830 * clear the S_ISGID bit if the Group execute bit is
2831 * on, but if it was on then mandatory locking wouldn't
2832 * have been enabled.
2834 if (ip->i_d.di_mode & S_IXGRP)
2835 ip->i_d.di_mode &= ~S_ISGID;
2837 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2839 if (setprealloc)
2840 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2841 else if (clrprealloc)
2842 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2844 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2845 xfs_trans_set_sync(tp);
2847 error = xfs_trans_commit(tp, 0);
2849 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2851 return error;