mfd: Fix twl-core oops while calling twl_i2c_* for unbound driver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_vnodeops.c
blob6cc4d41fb0db2752af1ee0c8615752f60be4b105
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 xfs_fsize_t 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 pathlen = ip->i_d.di_size;
549 if (!pathlen)
550 goto out;
552 if (pathlen < 0 || pathlen > MAXPATHLEN) {
553 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
554 __func__, (unsigned long long) ip->i_ino,
555 (long long) pathlen);
556 ASSERT(0);
557 return XFS_ERROR(EFSCORRUPTED);
561 if (ip->i_df.if_flags & XFS_IFINLINE) {
562 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
563 link[pathlen] = '\0';
564 } else {
565 error = xfs_readlink_bmap(ip, link);
568 out:
569 xfs_iunlock(ip, XFS_ILOCK_SHARED);
570 return error;
574 * Flags for xfs_free_eofblocks
576 #define XFS_FREE_EOF_TRYLOCK (1<<0)
579 * This is called by xfs_inactive to free any blocks beyond eof
580 * when the link count isn't zero and by xfs_dm_punch_hole() when
581 * punching a hole to EOF.
583 STATIC int
584 xfs_free_eofblocks(
585 xfs_mount_t *mp,
586 xfs_inode_t *ip,
587 int flags)
589 xfs_trans_t *tp;
590 int error;
591 xfs_fileoff_t end_fsb;
592 xfs_fileoff_t last_fsb;
593 xfs_filblks_t map_len;
594 int nimaps;
595 xfs_bmbt_irec_t imap;
598 * Figure out if there are any blocks beyond the end
599 * of the file. If not, then there is nothing to do.
601 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
602 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
603 if (last_fsb <= end_fsb)
604 return 0;
605 map_len = last_fsb - end_fsb;
607 nimaps = 1;
608 xfs_ilock(ip, XFS_ILOCK_SHARED);
609 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
610 NULL, 0, &imap, &nimaps, NULL);
611 xfs_iunlock(ip, XFS_ILOCK_SHARED);
613 if (!error && (nimaps != 0) &&
614 (imap.br_startblock != HOLESTARTBLOCK ||
615 ip->i_delayed_blks)) {
617 * Attach the dquots to the inode up front.
619 error = xfs_qm_dqattach(ip, 0);
620 if (error)
621 return error;
624 * There are blocks after the end of file.
625 * Free them up now by truncating the file to
626 * its current size.
628 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
631 * Do the xfs_itruncate_start() call before
632 * reserving any log space because
633 * itruncate_start will call into the buffer
634 * cache and we can't
635 * do that within a transaction.
637 if (flags & XFS_FREE_EOF_TRYLOCK) {
638 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
639 xfs_trans_cancel(tp, 0);
640 return 0;
642 } else {
643 xfs_ilock(ip, XFS_IOLOCK_EXCL);
645 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
646 ip->i_size);
647 if (error) {
648 xfs_trans_cancel(tp, 0);
649 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
650 return error;
653 error = xfs_trans_reserve(tp, 0,
654 XFS_ITRUNCATE_LOG_RES(mp),
655 0, XFS_TRANS_PERM_LOG_RES,
656 XFS_ITRUNCATE_LOG_COUNT);
657 if (error) {
658 ASSERT(XFS_FORCED_SHUTDOWN(mp));
659 xfs_trans_cancel(tp, 0);
660 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
661 return error;
664 xfs_ilock(ip, XFS_ILOCK_EXCL);
665 xfs_trans_ijoin(tp, ip);
667 error = xfs_itruncate_finish(&tp, ip,
668 ip->i_size,
669 XFS_DATA_FORK,
672 * If we get an error at this point we
673 * simply don't bother truncating the file.
675 if (error) {
676 xfs_trans_cancel(tp,
677 (XFS_TRANS_RELEASE_LOG_RES |
678 XFS_TRANS_ABORT));
679 } else {
680 error = xfs_trans_commit(tp,
681 XFS_TRANS_RELEASE_LOG_RES);
683 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
685 return error;
689 * Free a symlink that has blocks associated with it.
691 STATIC int
692 xfs_inactive_symlink_rmt(
693 xfs_inode_t *ip,
694 xfs_trans_t **tpp)
696 xfs_buf_t *bp;
697 int committed;
698 int done;
699 int error;
700 xfs_fsblock_t first_block;
701 xfs_bmap_free_t free_list;
702 int i;
703 xfs_mount_t *mp;
704 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
705 int nmaps;
706 xfs_trans_t *ntp;
707 int size;
708 xfs_trans_t *tp;
710 tp = *tpp;
711 mp = ip->i_mount;
712 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
714 * We're freeing a symlink that has some
715 * blocks allocated to it. Free the
716 * blocks here. We know that we've got
717 * either 1 or 2 extents and that we can
718 * free them all in one bunmapi call.
720 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
721 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
722 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
723 ASSERT(XFS_FORCED_SHUTDOWN(mp));
724 xfs_trans_cancel(tp, 0);
725 *tpp = NULL;
726 return error;
729 * Lock the inode, fix the size, and join it to the transaction.
730 * Hold it so in the normal path, we still have it locked for
731 * the second transaction. In the error paths we need it
732 * held so the cancel won't rele it, see below.
734 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
735 size = (int)ip->i_d.di_size;
736 ip->i_d.di_size = 0;
737 xfs_trans_ijoin(tp, ip);
738 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
740 * Find the block(s) so we can inval and unmap them.
742 done = 0;
743 xfs_bmap_init(&free_list, &first_block);
744 nmaps = ARRAY_SIZE(mval);
745 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
746 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
747 &free_list)))
748 goto error0;
750 * Invalidate the block(s).
752 for (i = 0; i < nmaps; i++) {
753 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
754 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
755 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
756 xfs_trans_binval(tp, bp);
759 * Unmap the dead block(s) to the free_list.
761 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
762 &first_block, &free_list, &done)))
763 goto error1;
764 ASSERT(done);
766 * Commit the first transaction. This logs the EFI and the inode.
768 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
769 goto error1;
771 * The transaction must have been committed, since there were
772 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
773 * The new tp has the extent freeing and EFDs.
775 ASSERT(committed);
777 * The first xact was committed, so add the inode to the new one.
778 * Mark it dirty so it will be logged and moved forward in the log as
779 * part of every commit.
781 xfs_trans_ijoin(tp, ip);
782 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
784 * Get a new, empty transaction to return to our caller.
786 ntp = xfs_trans_dup(tp);
788 * Commit the transaction containing extent freeing and EFDs.
789 * If we get an error on the commit here or on the reserve below,
790 * we need to unlock the inode since the new transaction doesn't
791 * have the inode attached.
793 error = xfs_trans_commit(tp, 0);
794 tp = ntp;
795 if (error) {
796 ASSERT(XFS_FORCED_SHUTDOWN(mp));
797 goto error0;
800 * transaction commit worked ok so we can drop the extra ticket
801 * reference that we gained in xfs_trans_dup()
803 xfs_log_ticket_put(tp->t_ticket);
806 * Remove the memory for extent descriptions (just bookkeeping).
808 if (ip->i_df.if_bytes)
809 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
810 ASSERT(ip->i_df.if_bytes == 0);
812 * Put an itruncate log reservation in the new transaction
813 * for our caller.
815 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
816 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
817 ASSERT(XFS_FORCED_SHUTDOWN(mp));
818 goto error0;
821 * Return with the inode locked but not joined to the transaction.
823 *tpp = tp;
824 return 0;
826 error1:
827 xfs_bmap_cancel(&free_list);
828 error0:
830 * Have to come here with the inode locked and either
831 * (held and in the transaction) or (not in the transaction).
832 * If the inode isn't held then cancel would iput it, but
833 * that's wrong since this is inactive and the vnode ref
834 * count is 0 already.
835 * Cancel won't do anything to the inode if held, but it still
836 * needs to be locked until the cancel is done, if it was
837 * joined to the transaction.
839 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
840 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
841 *tpp = NULL;
842 return error;
846 STATIC int
847 xfs_inactive_symlink_local(
848 xfs_inode_t *ip,
849 xfs_trans_t **tpp)
851 int error;
853 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
855 * We're freeing a symlink which fit into
856 * the inode. Just free the memory used
857 * to hold the old symlink.
859 error = xfs_trans_reserve(*tpp, 0,
860 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
861 0, XFS_TRANS_PERM_LOG_RES,
862 XFS_ITRUNCATE_LOG_COUNT);
864 if (error) {
865 xfs_trans_cancel(*tpp, 0);
866 *tpp = NULL;
867 return error;
869 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
872 * Zero length symlinks _can_ exist.
874 if (ip->i_df.if_bytes > 0) {
875 xfs_idata_realloc(ip,
876 -(ip->i_df.if_bytes),
877 XFS_DATA_FORK);
878 ASSERT(ip->i_df.if_bytes == 0);
880 return 0;
883 STATIC int
884 xfs_inactive_attrs(
885 xfs_inode_t *ip,
886 xfs_trans_t **tpp)
888 xfs_trans_t *tp;
889 int error;
890 xfs_mount_t *mp;
892 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
893 tp = *tpp;
894 mp = ip->i_mount;
895 ASSERT(ip->i_d.di_forkoff != 0);
896 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
897 xfs_iunlock(ip, XFS_ILOCK_EXCL);
898 if (error)
899 goto error_unlock;
901 error = xfs_attr_inactive(ip);
902 if (error)
903 goto error_unlock;
905 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
906 error = xfs_trans_reserve(tp, 0,
907 XFS_IFREE_LOG_RES(mp),
908 0, XFS_TRANS_PERM_LOG_RES,
909 XFS_INACTIVE_LOG_COUNT);
910 if (error)
911 goto error_cancel;
913 xfs_ilock(ip, XFS_ILOCK_EXCL);
914 xfs_trans_ijoin(tp, ip);
915 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
917 ASSERT(ip->i_d.di_anextents == 0);
919 *tpp = tp;
920 return 0;
922 error_cancel:
923 ASSERT(XFS_FORCED_SHUTDOWN(mp));
924 xfs_trans_cancel(tp, 0);
925 error_unlock:
926 *tpp = NULL;
927 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
928 return error;
932 xfs_release(
933 xfs_inode_t *ip)
935 xfs_mount_t *mp = ip->i_mount;
936 int error;
938 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
939 return 0;
941 /* If this is a read-only mount, don't do this (would generate I/O) */
942 if (mp->m_flags & XFS_MOUNT_RDONLY)
943 return 0;
945 if (!XFS_FORCED_SHUTDOWN(mp)) {
946 int truncated;
949 * If we are using filestreams, and we have an unlinked
950 * file that we are processing the last close on, then nothing
951 * will be able to reopen and write to this file. Purge this
952 * inode from the filestreams cache so that it doesn't delay
953 * teardown of the inode.
955 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
956 xfs_filestream_deassociate(ip);
959 * If we previously truncated this file and removed old data
960 * in the process, we want to initiate "early" writeout on
961 * the last close. This is an attempt to combat the notorious
962 * NULL files problem which is particularly noticeable from a
963 * truncate down, buffered (re-)write (delalloc), followed by
964 * a crash. What we are effectively doing here is
965 * significantly reducing the time window where we'd otherwise
966 * be exposed to that problem.
968 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
969 if (truncated) {
970 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
971 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
972 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
976 if (ip->i_d.di_nlink == 0)
977 return 0;
979 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
980 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
981 ip->i_delayed_blks > 0)) &&
982 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
983 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
986 * If we can't get the iolock just skip truncating the blocks
987 * past EOF because we could deadlock with the mmap_sem
988 * otherwise. We'll get another chance to drop them once the
989 * last reference to the inode is dropped, so we'll never leak
990 * blocks permanently.
992 * Further, check if the inode is being opened, written and
993 * closed frequently and we have delayed allocation blocks
994 * outstanding (e.g. streaming writes from the NFS server),
995 * truncating the blocks past EOF will cause fragmentation to
996 * occur.
998 * In this case don't do the truncation, either, but we have to
999 * be careful how we detect this case. Blocks beyond EOF show
1000 * up as i_delayed_blks even when the inode is clean, so we
1001 * need to truncate them away first before checking for a dirty
1002 * release. Hence on the first dirty close we will still remove
1003 * the speculative allocation, but after that we will leave it
1004 * in place.
1006 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1007 return 0;
1009 error = xfs_free_eofblocks(mp, ip,
1010 XFS_FREE_EOF_TRYLOCK);
1011 if (error)
1012 return error;
1014 /* delalloc blocks after truncation means it really is dirty */
1015 if (ip->i_delayed_blks)
1016 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1018 return 0;
1022 * xfs_inactive
1024 * This is called when the vnode reference count for the vnode
1025 * goes to zero. If the file has been unlinked, then it must
1026 * now be truncated. Also, we clear all of the read-ahead state
1027 * kept for the inode here since the file is now closed.
1030 xfs_inactive(
1031 xfs_inode_t *ip)
1033 xfs_bmap_free_t free_list;
1034 xfs_fsblock_t first_block;
1035 int committed;
1036 xfs_trans_t *tp;
1037 xfs_mount_t *mp;
1038 int error;
1039 int truncate;
1042 * If the inode is already free, then there can be nothing
1043 * to clean up here.
1045 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
1046 ASSERT(ip->i_df.if_real_bytes == 0);
1047 ASSERT(ip->i_df.if_broot_bytes == 0);
1048 return VN_INACTIVE_CACHE;
1052 * Only do a truncate if it's a regular file with
1053 * some actual space in it. It's OK to look at the
1054 * inode's fields without the lock because we're the
1055 * only one with a reference to the inode.
1057 truncate = ((ip->i_d.di_nlink == 0) &&
1058 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1059 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1060 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1062 mp = ip->i_mount;
1064 error = 0;
1066 /* If this is a read-only mount, don't do this (would generate I/O) */
1067 if (mp->m_flags & XFS_MOUNT_RDONLY)
1068 goto out;
1070 if (ip->i_d.di_nlink != 0) {
1071 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1072 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1073 ip->i_delayed_blks > 0)) &&
1074 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1075 (!(ip->i_d.di_flags &
1076 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1077 (ip->i_delayed_blks != 0)))) {
1078 error = xfs_free_eofblocks(mp, ip, 0);
1079 if (error)
1080 return VN_INACTIVE_CACHE;
1082 goto out;
1085 ASSERT(ip->i_d.di_nlink == 0);
1087 error = xfs_qm_dqattach(ip, 0);
1088 if (error)
1089 return VN_INACTIVE_CACHE;
1091 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1092 if (truncate) {
1094 * Do the xfs_itruncate_start() call before
1095 * reserving any log space because itruncate_start
1096 * will call into the buffer cache and we can't
1097 * do that within a transaction.
1099 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1101 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1102 if (error) {
1103 xfs_trans_cancel(tp, 0);
1104 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1105 return VN_INACTIVE_CACHE;
1108 error = xfs_trans_reserve(tp, 0,
1109 XFS_ITRUNCATE_LOG_RES(mp),
1110 0, XFS_TRANS_PERM_LOG_RES,
1111 XFS_ITRUNCATE_LOG_COUNT);
1112 if (error) {
1113 /* Don't call itruncate_cleanup */
1114 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1115 xfs_trans_cancel(tp, 0);
1116 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1117 return VN_INACTIVE_CACHE;
1120 xfs_ilock(ip, XFS_ILOCK_EXCL);
1121 xfs_trans_ijoin(tp, ip);
1124 * normally, we have to run xfs_itruncate_finish sync.
1125 * But if filesystem is wsync and we're in the inactive
1126 * path, then we know that nlink == 0, and that the
1127 * xaction that made nlink == 0 is permanently committed
1128 * since xfs_remove runs as a synchronous transaction.
1130 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1131 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1133 if (error) {
1134 xfs_trans_cancel(tp,
1135 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1136 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1137 return VN_INACTIVE_CACHE;
1139 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1142 * If we get an error while cleaning up a
1143 * symlink we bail out.
1145 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1146 xfs_inactive_symlink_rmt(ip, &tp) :
1147 xfs_inactive_symlink_local(ip, &tp);
1149 if (error) {
1150 ASSERT(tp == NULL);
1151 return VN_INACTIVE_CACHE;
1154 xfs_trans_ijoin(tp, ip);
1155 } else {
1156 error = xfs_trans_reserve(tp, 0,
1157 XFS_IFREE_LOG_RES(mp),
1158 0, XFS_TRANS_PERM_LOG_RES,
1159 XFS_INACTIVE_LOG_COUNT);
1160 if (error) {
1161 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1162 xfs_trans_cancel(tp, 0);
1163 return VN_INACTIVE_CACHE;
1166 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1167 xfs_trans_ijoin(tp, ip);
1171 * If there are attributes associated with the file
1172 * then blow them away now. The code calls a routine
1173 * that recursively deconstructs the attribute fork.
1174 * We need to just commit the current transaction
1175 * because we can't use it for xfs_attr_inactive().
1177 if (ip->i_d.di_anextents > 0) {
1178 error = xfs_inactive_attrs(ip, &tp);
1180 * If we got an error, the transaction is already
1181 * cancelled, and the inode is unlocked. Just get out.
1183 if (error)
1184 return VN_INACTIVE_CACHE;
1185 } else if (ip->i_afp) {
1186 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1190 * Free the inode.
1192 xfs_bmap_init(&free_list, &first_block);
1193 error = xfs_ifree(tp, ip, &free_list);
1194 if (error) {
1196 * If we fail to free the inode, shut down. The cancel
1197 * might do that, we need to make sure. Otherwise the
1198 * inode might be lost for a long time or forever.
1200 if (!XFS_FORCED_SHUTDOWN(mp)) {
1201 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1202 __func__, error);
1203 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1205 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1206 } else {
1208 * Credit the quota account(s). The inode is gone.
1210 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1213 * Just ignore errors at this point. There is nothing we can
1214 * do except to try to keep going. Make sure it's not a silent
1215 * error.
1217 error = xfs_bmap_finish(&tp, &free_list, &committed);
1218 if (error)
1219 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
1220 __func__, error);
1221 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1222 if (error)
1223 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1224 __func__, error);
1228 * Release the dquots held by inode, if any.
1230 xfs_qm_dqdetach(ip);
1231 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1233 out:
1234 return VN_INACTIVE_CACHE;
1238 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1239 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1240 * ci_name->name will point to a the actual name (caller must free) or
1241 * will be set to NULL if an exact match is found.
1244 xfs_lookup(
1245 xfs_inode_t *dp,
1246 struct xfs_name *name,
1247 xfs_inode_t **ipp,
1248 struct xfs_name *ci_name)
1250 xfs_ino_t inum;
1251 int error;
1252 uint lock_mode;
1254 trace_xfs_lookup(dp, name);
1256 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1257 return XFS_ERROR(EIO);
1259 lock_mode = xfs_ilock_map_shared(dp);
1260 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1261 xfs_iunlock_map_shared(dp, lock_mode);
1263 if (error)
1264 goto out;
1266 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
1267 if (error)
1268 goto out_free_name;
1270 return 0;
1272 out_free_name:
1273 if (ci_name)
1274 kmem_free(ci_name->name);
1275 out:
1276 *ipp = NULL;
1277 return error;
1281 xfs_create(
1282 xfs_inode_t *dp,
1283 struct xfs_name *name,
1284 mode_t mode,
1285 xfs_dev_t rdev,
1286 xfs_inode_t **ipp)
1288 int is_dir = S_ISDIR(mode);
1289 struct xfs_mount *mp = dp->i_mount;
1290 struct xfs_inode *ip = NULL;
1291 struct xfs_trans *tp = NULL;
1292 int error;
1293 xfs_bmap_free_t free_list;
1294 xfs_fsblock_t first_block;
1295 boolean_t unlock_dp_on_error = B_FALSE;
1296 uint cancel_flags;
1297 int committed;
1298 prid_t prid;
1299 struct xfs_dquot *udqp = NULL;
1300 struct xfs_dquot *gdqp = NULL;
1301 uint resblks;
1302 uint log_res;
1303 uint log_count;
1305 trace_xfs_create(dp, name);
1307 if (XFS_FORCED_SHUTDOWN(mp))
1308 return XFS_ERROR(EIO);
1310 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1311 prid = xfs_get_projid(dp);
1312 else
1313 prid = XFS_PROJID_DEFAULT;
1316 * Make sure that we have allocated dquot(s) on disk.
1318 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1319 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1320 if (error)
1321 return error;
1323 if (is_dir) {
1324 rdev = 0;
1325 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1326 log_res = XFS_MKDIR_LOG_RES(mp);
1327 log_count = XFS_MKDIR_LOG_COUNT;
1328 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1329 } else {
1330 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1331 log_res = XFS_CREATE_LOG_RES(mp);
1332 log_count = XFS_CREATE_LOG_COUNT;
1333 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1336 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1339 * Initially assume that the file does not exist and
1340 * reserve the resources for that case. If that is not
1341 * the case we'll drop the one we have and get a more
1342 * appropriate transaction later.
1344 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1345 XFS_TRANS_PERM_LOG_RES, log_count);
1346 if (error == ENOSPC) {
1347 /* flush outstanding delalloc blocks and retry */
1348 xfs_flush_inodes(dp);
1349 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1350 XFS_TRANS_PERM_LOG_RES, log_count);
1352 if (error == ENOSPC) {
1353 /* No space at all so try a "no-allocation" reservation */
1354 resblks = 0;
1355 error = xfs_trans_reserve(tp, 0, log_res, 0,
1356 XFS_TRANS_PERM_LOG_RES, log_count);
1358 if (error) {
1359 cancel_flags = 0;
1360 goto out_trans_cancel;
1363 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1364 unlock_dp_on_error = B_TRUE;
1367 * Check for directory link count overflow.
1369 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1370 error = XFS_ERROR(EMLINK);
1371 goto out_trans_cancel;
1374 xfs_bmap_init(&free_list, &first_block);
1377 * Reserve disk quota and the inode.
1379 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
1380 if (error)
1381 goto out_trans_cancel;
1383 error = xfs_dir_canenter(tp, dp, name, resblks);
1384 if (error)
1385 goto out_trans_cancel;
1388 * A newly created regular or special file just has one directory
1389 * entry pointing to them, but a directory also the "." entry
1390 * pointing to itself.
1392 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
1393 prid, resblks > 0, &ip, &committed);
1394 if (error) {
1395 if (error == ENOSPC)
1396 goto out_trans_cancel;
1397 goto out_trans_abort;
1401 * Now we join the directory inode to the transaction. We do not do it
1402 * earlier because xfs_dir_ialloc might commit the previous transaction
1403 * (and release all the locks). An error from here on will result in
1404 * the transaction cancel unlocking dp so don't do it explicitly in the
1405 * error path.
1407 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
1408 unlock_dp_on_error = B_FALSE;
1410 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1411 &first_block, &free_list, resblks ?
1412 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1413 if (error) {
1414 ASSERT(error != ENOSPC);
1415 goto out_trans_abort;
1417 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1418 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1420 if (is_dir) {
1421 error = xfs_dir_init(tp, ip, dp);
1422 if (error)
1423 goto out_bmap_cancel;
1425 error = xfs_bumplink(tp, dp);
1426 if (error)
1427 goto out_bmap_cancel;
1431 * If this is a synchronous mount, make sure that the
1432 * create transaction goes to disk before returning to
1433 * the user.
1435 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1436 xfs_trans_set_sync(tp);
1439 * Attach the dquot(s) to the inodes and modify them incore.
1440 * These ids of the inode couldn't have changed since the new
1441 * inode has been locked ever since it was created.
1443 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
1445 error = xfs_bmap_finish(&tp, &free_list, &committed);
1446 if (error)
1447 goto out_bmap_cancel;
1449 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1450 if (error)
1451 goto out_release_inode;
1453 xfs_qm_dqrele(udqp);
1454 xfs_qm_dqrele(gdqp);
1456 *ipp = ip;
1457 return 0;
1459 out_bmap_cancel:
1460 xfs_bmap_cancel(&free_list);
1461 out_trans_abort:
1462 cancel_flags |= XFS_TRANS_ABORT;
1463 out_trans_cancel:
1464 xfs_trans_cancel(tp, cancel_flags);
1465 out_release_inode:
1467 * Wait until after the current transaction is aborted to
1468 * release the inode. This prevents recursive transactions
1469 * and deadlocks from xfs_inactive.
1471 if (ip)
1472 IRELE(ip);
1474 xfs_qm_dqrele(udqp);
1475 xfs_qm_dqrele(gdqp);
1477 if (unlock_dp_on_error)
1478 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1479 return error;
1482 #ifdef DEBUG
1483 int xfs_locked_n;
1484 int xfs_small_retries;
1485 int xfs_middle_retries;
1486 int xfs_lots_retries;
1487 int xfs_lock_delays;
1488 #endif
1491 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1492 * a different value
1494 static inline int
1495 xfs_lock_inumorder(int lock_mode, int subclass)
1497 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1498 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1499 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1500 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1502 return lock_mode;
1506 * The following routine will lock n inodes in exclusive mode.
1507 * We assume the caller calls us with the inodes in i_ino order.
1509 * We need to detect deadlock where an inode that we lock
1510 * is in the AIL and we start waiting for another inode that is locked
1511 * by a thread in a long running transaction (such as truncate). This can
1512 * result in deadlock since the long running trans might need to wait
1513 * for the inode we just locked in order to push the tail and free space
1514 * in the log.
1516 void
1517 xfs_lock_inodes(
1518 xfs_inode_t **ips,
1519 int inodes,
1520 uint lock_mode)
1522 int attempts = 0, i, j, try_lock;
1523 xfs_log_item_t *lp;
1525 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1527 try_lock = 0;
1528 i = 0;
1530 again:
1531 for (; i < inodes; i++) {
1532 ASSERT(ips[i]);
1534 if (i && (ips[i] == ips[i-1])) /* Already locked */
1535 continue;
1538 * If try_lock is not set yet, make sure all locked inodes
1539 * are not in the AIL.
1540 * If any are, set try_lock to be used later.
1543 if (!try_lock) {
1544 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1545 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1546 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1547 try_lock++;
1553 * If any of the previous locks we have locked is in the AIL,
1554 * we must TRY to get the second and subsequent locks. If
1555 * we can't get any, we must release all we have
1556 * and try again.
1559 if (try_lock) {
1560 /* try_lock must be 0 if i is 0. */
1562 * try_lock means we have an inode locked
1563 * that is in the AIL.
1565 ASSERT(i != 0);
1566 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1567 attempts++;
1570 * Unlock all previous guys and try again.
1571 * xfs_iunlock will try to push the tail
1572 * if the inode is in the AIL.
1575 for(j = i - 1; j >= 0; j--) {
1578 * Check to see if we've already
1579 * unlocked this one.
1580 * Not the first one going back,
1581 * and the inode ptr is the same.
1583 if ((j != (i - 1)) && ips[j] ==
1584 ips[j+1])
1585 continue;
1587 xfs_iunlock(ips[j], lock_mode);
1590 if ((attempts % 5) == 0) {
1591 delay(1); /* Don't just spin the CPU */
1592 #ifdef DEBUG
1593 xfs_lock_delays++;
1594 #endif
1596 i = 0;
1597 try_lock = 0;
1598 goto again;
1600 } else {
1601 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1605 #ifdef DEBUG
1606 if (attempts) {
1607 if (attempts < 5) xfs_small_retries++;
1608 else if (attempts < 100) xfs_middle_retries++;
1609 else xfs_lots_retries++;
1610 } else {
1611 xfs_locked_n++;
1613 #endif
1617 * xfs_lock_two_inodes() can only be used to lock one type of lock
1618 * at a time - the iolock or the ilock, but not both at once. If
1619 * we lock both at once, lockdep will report false positives saying
1620 * we have violated locking orders.
1622 void
1623 xfs_lock_two_inodes(
1624 xfs_inode_t *ip0,
1625 xfs_inode_t *ip1,
1626 uint lock_mode)
1628 xfs_inode_t *temp;
1629 int attempts = 0;
1630 xfs_log_item_t *lp;
1632 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1633 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1634 ASSERT(ip0->i_ino != ip1->i_ino);
1636 if (ip0->i_ino > ip1->i_ino) {
1637 temp = ip0;
1638 ip0 = ip1;
1639 ip1 = temp;
1642 again:
1643 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1646 * If the first lock we have locked is in the AIL, we must TRY to get
1647 * the second lock. If we can't get it, we must release the first one
1648 * and try again.
1650 lp = (xfs_log_item_t *)ip0->i_itemp;
1651 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1652 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1653 xfs_iunlock(ip0, lock_mode);
1654 if ((++attempts % 5) == 0)
1655 delay(1); /* Don't just spin the CPU */
1656 goto again;
1658 } else {
1659 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1664 xfs_remove(
1665 xfs_inode_t *dp,
1666 struct xfs_name *name,
1667 xfs_inode_t *ip)
1669 xfs_mount_t *mp = dp->i_mount;
1670 xfs_trans_t *tp = NULL;
1671 int is_dir = S_ISDIR(ip->i_d.di_mode);
1672 int error = 0;
1673 xfs_bmap_free_t free_list;
1674 xfs_fsblock_t first_block;
1675 int cancel_flags;
1676 int committed;
1677 int link_zero;
1678 uint resblks;
1679 uint log_count;
1681 trace_xfs_remove(dp, name);
1683 if (XFS_FORCED_SHUTDOWN(mp))
1684 return XFS_ERROR(EIO);
1686 error = xfs_qm_dqattach(dp, 0);
1687 if (error)
1688 goto std_return;
1690 error = xfs_qm_dqattach(ip, 0);
1691 if (error)
1692 goto std_return;
1694 if (is_dir) {
1695 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1696 log_count = XFS_DEFAULT_LOG_COUNT;
1697 } else {
1698 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1699 log_count = XFS_REMOVE_LOG_COUNT;
1701 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1704 * We try to get the real space reservation first,
1705 * allowing for directory btree deletion(s) implying
1706 * possible bmap insert(s). If we can't get the space
1707 * reservation then we use 0 instead, and avoid the bmap
1708 * btree insert(s) in the directory code by, if the bmap
1709 * insert tries to happen, instead trimming the LAST
1710 * block from the directory.
1712 resblks = XFS_REMOVE_SPACE_RES(mp);
1713 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1714 XFS_TRANS_PERM_LOG_RES, log_count);
1715 if (error == ENOSPC) {
1716 resblks = 0;
1717 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1718 XFS_TRANS_PERM_LOG_RES, log_count);
1720 if (error) {
1721 ASSERT(error != ENOSPC);
1722 cancel_flags = 0;
1723 goto out_trans_cancel;
1726 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1728 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
1729 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
1732 * If we're removing a directory perform some additional validation.
1734 if (is_dir) {
1735 ASSERT(ip->i_d.di_nlink >= 2);
1736 if (ip->i_d.di_nlink != 2) {
1737 error = XFS_ERROR(ENOTEMPTY);
1738 goto out_trans_cancel;
1740 if (!xfs_dir_isempty(ip)) {
1741 error = XFS_ERROR(ENOTEMPTY);
1742 goto out_trans_cancel;
1746 xfs_bmap_init(&free_list, &first_block);
1747 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1748 &first_block, &free_list, resblks);
1749 if (error) {
1750 ASSERT(error != ENOENT);
1751 goto out_bmap_cancel;
1753 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1755 if (is_dir) {
1757 * Drop the link from ip's "..".
1759 error = xfs_droplink(tp, dp);
1760 if (error)
1761 goto out_bmap_cancel;
1764 * Drop the "." link from ip to self.
1766 error = xfs_droplink(tp, ip);
1767 if (error)
1768 goto out_bmap_cancel;
1769 } else {
1771 * When removing a non-directory we need to log the parent
1772 * inode here. For a directory this is done implicitly
1773 * by the xfs_droplink call for the ".." entry.
1775 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1779 * Drop the link from dp to ip.
1781 error = xfs_droplink(tp, ip);
1782 if (error)
1783 goto out_bmap_cancel;
1786 * Determine if this is the last link while
1787 * we are in the transaction.
1789 link_zero = (ip->i_d.di_nlink == 0);
1792 * If this is a synchronous mount, make sure that the
1793 * remove transaction goes to disk before returning to
1794 * the user.
1796 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1797 xfs_trans_set_sync(tp);
1799 error = xfs_bmap_finish(&tp, &free_list, &committed);
1800 if (error)
1801 goto out_bmap_cancel;
1803 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1804 if (error)
1805 goto std_return;
1808 * If we are using filestreams, kill the stream association.
1809 * If the file is still open it may get a new one but that
1810 * will get killed on last close in xfs_close() so we don't
1811 * have to worry about that.
1813 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
1814 xfs_filestream_deassociate(ip);
1816 return 0;
1818 out_bmap_cancel:
1819 xfs_bmap_cancel(&free_list);
1820 cancel_flags |= XFS_TRANS_ABORT;
1821 out_trans_cancel:
1822 xfs_trans_cancel(tp, cancel_flags);
1823 std_return:
1824 return error;
1828 xfs_link(
1829 xfs_inode_t *tdp,
1830 xfs_inode_t *sip,
1831 struct xfs_name *target_name)
1833 xfs_mount_t *mp = tdp->i_mount;
1834 xfs_trans_t *tp;
1835 int error;
1836 xfs_bmap_free_t free_list;
1837 xfs_fsblock_t first_block;
1838 int cancel_flags;
1839 int committed;
1840 int resblks;
1842 trace_xfs_link(tdp, target_name);
1844 ASSERT(!S_ISDIR(sip->i_d.di_mode));
1846 if (XFS_FORCED_SHUTDOWN(mp))
1847 return XFS_ERROR(EIO);
1849 error = xfs_qm_dqattach(sip, 0);
1850 if (error)
1851 goto std_return;
1853 error = xfs_qm_dqattach(tdp, 0);
1854 if (error)
1855 goto std_return;
1857 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1858 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1859 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1860 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1861 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1862 if (error == ENOSPC) {
1863 resblks = 0;
1864 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1865 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1867 if (error) {
1868 cancel_flags = 0;
1869 goto error_return;
1872 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1874 xfs_trans_ijoin_ref(tp, sip, XFS_ILOCK_EXCL);
1875 xfs_trans_ijoin_ref(tp, tdp, XFS_ILOCK_EXCL);
1878 * If the source has too many links, we can't make any more to it.
1880 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
1881 error = XFS_ERROR(EMLINK);
1882 goto error_return;
1886 * If we are using project inheritance, we only allow hard link
1887 * creation in our tree when the project IDs are the same; else
1888 * the tree quota mechanism could be circumvented.
1890 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1891 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
1892 error = XFS_ERROR(EXDEV);
1893 goto error_return;
1896 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1897 if (error)
1898 goto error_return;
1900 xfs_bmap_init(&free_list, &first_block);
1902 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1903 &first_block, &free_list, resblks);
1904 if (error)
1905 goto abort_return;
1906 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1907 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1909 error = xfs_bumplink(tp, sip);
1910 if (error)
1911 goto abort_return;
1914 * If this is a synchronous mount, make sure that the
1915 * link transaction goes to disk before returning to
1916 * the user.
1918 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1919 xfs_trans_set_sync(tp);
1922 error = xfs_bmap_finish (&tp, &free_list, &committed);
1923 if (error) {
1924 xfs_bmap_cancel(&free_list);
1925 goto abort_return;
1928 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1930 abort_return:
1931 cancel_flags |= XFS_TRANS_ABORT;
1932 error_return:
1933 xfs_trans_cancel(tp, cancel_flags);
1934 std_return:
1935 return error;
1939 xfs_symlink(
1940 xfs_inode_t *dp,
1941 struct xfs_name *link_name,
1942 const char *target_path,
1943 mode_t mode,
1944 xfs_inode_t **ipp)
1946 xfs_mount_t *mp = dp->i_mount;
1947 xfs_trans_t *tp;
1948 xfs_inode_t *ip;
1949 int error;
1950 int pathlen;
1951 xfs_bmap_free_t free_list;
1952 xfs_fsblock_t first_block;
1953 boolean_t unlock_dp_on_error = B_FALSE;
1954 uint cancel_flags;
1955 int committed;
1956 xfs_fileoff_t first_fsb;
1957 xfs_filblks_t fs_blocks;
1958 int nmaps;
1959 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1960 xfs_daddr_t d;
1961 const char *cur_chunk;
1962 int byte_cnt;
1963 int n;
1964 xfs_buf_t *bp;
1965 prid_t prid;
1966 struct xfs_dquot *udqp, *gdqp;
1967 uint resblks;
1969 *ipp = NULL;
1970 error = 0;
1971 ip = NULL;
1972 tp = NULL;
1974 trace_xfs_symlink(dp, link_name);
1976 if (XFS_FORCED_SHUTDOWN(mp))
1977 return XFS_ERROR(EIO);
1980 * Check component lengths of the target path name.
1982 pathlen = strlen(target_path);
1983 if (pathlen >= MAXPATHLEN) /* total string too long */
1984 return XFS_ERROR(ENAMETOOLONG);
1986 udqp = gdqp = NULL;
1987 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1988 prid = xfs_get_projid(dp);
1989 else
1990 prid = XFS_PROJID_DEFAULT;
1993 * Make sure that we have allocated dquot(s) on disk.
1995 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1996 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1997 if (error)
1998 goto std_return;
2000 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2001 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2003 * The symlink will fit into the inode data fork?
2004 * There can't be any attributes so we get the whole variable part.
2006 if (pathlen <= XFS_LITINO(mp))
2007 fs_blocks = 0;
2008 else
2009 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2010 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2011 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2012 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2013 if (error == ENOSPC && fs_blocks == 0) {
2014 resblks = 0;
2015 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2016 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2018 if (error) {
2019 cancel_flags = 0;
2020 goto error_return;
2023 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2024 unlock_dp_on_error = B_TRUE;
2027 * Check whether the directory allows new symlinks or not.
2029 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2030 error = XFS_ERROR(EPERM);
2031 goto error_return;
2035 * Reserve disk quota : blocks and inode.
2037 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
2038 if (error)
2039 goto error_return;
2042 * Check for ability to enter directory entry, if no space reserved.
2044 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2045 if (error)
2046 goto error_return;
2048 * Initialize the bmap freelist prior to calling either
2049 * bmapi or the directory create code.
2051 xfs_bmap_init(&free_list, &first_block);
2054 * Allocate an inode for the symlink.
2056 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
2057 prid, resblks > 0, &ip, NULL);
2058 if (error) {
2059 if (error == ENOSPC)
2060 goto error_return;
2061 goto error1;
2065 * An error after we've joined dp to the transaction will result in the
2066 * transaction cancel unlocking dp so don't do it explicitly in the
2067 * error path.
2069 xfs_trans_ijoin_ref(tp, dp, XFS_ILOCK_EXCL);
2070 unlock_dp_on_error = B_FALSE;
2073 * Also attach the dquot(s) to it, if applicable.
2075 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
2077 if (resblks)
2078 resblks -= XFS_IALLOC_SPACE_RES(mp);
2080 * If the symlink will fit into the inode, write it inline.
2082 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2083 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2084 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2085 ip->i_d.di_size = pathlen;
2088 * The inode was initially created in extent format.
2090 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2091 ip->i_df.if_flags |= XFS_IFINLINE;
2093 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2094 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2096 } else {
2097 first_fsb = 0;
2098 nmaps = SYMLINK_MAPS;
2100 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2101 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2102 &first_block, resblks, mval, &nmaps,
2103 &free_list);
2104 if (error)
2105 goto error2;
2107 if (resblks)
2108 resblks -= fs_blocks;
2109 ip->i_d.di_size = pathlen;
2110 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2112 cur_chunk = target_path;
2113 for (n = 0; n < nmaps; n++) {
2114 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2115 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2116 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2117 BTOBB(byte_cnt), 0);
2118 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2119 if (pathlen < byte_cnt) {
2120 byte_cnt = pathlen;
2122 pathlen -= byte_cnt;
2124 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2125 cur_chunk += byte_cnt;
2127 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2132 * Create the directory entry for the symlink.
2134 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2135 &first_block, &free_list, resblks);
2136 if (error)
2137 goto error2;
2138 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2139 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2142 * If this is a synchronous mount, make sure that the
2143 * symlink transaction goes to disk before returning to
2144 * the user.
2146 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2147 xfs_trans_set_sync(tp);
2150 error = xfs_bmap_finish(&tp, &free_list, &committed);
2151 if (error) {
2152 goto error2;
2154 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2155 xfs_qm_dqrele(udqp);
2156 xfs_qm_dqrele(gdqp);
2158 *ipp = ip;
2159 return 0;
2161 error2:
2162 IRELE(ip);
2163 error1:
2164 xfs_bmap_cancel(&free_list);
2165 cancel_flags |= XFS_TRANS_ABORT;
2166 error_return:
2167 xfs_trans_cancel(tp, cancel_flags);
2168 xfs_qm_dqrele(udqp);
2169 xfs_qm_dqrele(gdqp);
2171 if (unlock_dp_on_error)
2172 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2173 std_return:
2174 return error;
2178 xfs_set_dmattrs(
2179 xfs_inode_t *ip,
2180 u_int evmask,
2181 u_int16_t state)
2183 xfs_mount_t *mp = ip->i_mount;
2184 xfs_trans_t *tp;
2185 int error;
2187 if (!capable(CAP_SYS_ADMIN))
2188 return XFS_ERROR(EPERM);
2190 if (XFS_FORCED_SHUTDOWN(mp))
2191 return XFS_ERROR(EIO);
2193 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2194 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2195 if (error) {
2196 xfs_trans_cancel(tp, 0);
2197 return error;
2199 xfs_ilock(ip, XFS_ILOCK_EXCL);
2200 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
2202 ip->i_d.di_dmevmask = evmask;
2203 ip->i_d.di_dmstate = state;
2205 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2206 error = xfs_trans_commit(tp, 0);
2208 return error;
2212 * xfs_alloc_file_space()
2213 * This routine allocates disk space for the given file.
2215 * If alloc_type == 0, this request is for an ALLOCSP type
2216 * request which will change the file size. In this case, no
2217 * DMAPI event will be generated by the call. A TRUNCATE event
2218 * will be generated later by xfs_setattr.
2220 * If alloc_type != 0, this request is for a RESVSP type
2221 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2222 * lower block boundary byte address is less than the file's
2223 * length.
2225 * RETURNS:
2226 * 0 on success
2227 * errno on error
2230 STATIC int
2231 xfs_alloc_file_space(
2232 xfs_inode_t *ip,
2233 xfs_off_t offset,
2234 xfs_off_t len,
2235 int alloc_type,
2236 int attr_flags)
2238 xfs_mount_t *mp = ip->i_mount;
2239 xfs_off_t count;
2240 xfs_filblks_t allocated_fsb;
2241 xfs_filblks_t allocatesize_fsb;
2242 xfs_extlen_t extsz, temp;
2243 xfs_fileoff_t startoffset_fsb;
2244 xfs_fsblock_t firstfsb;
2245 int nimaps;
2246 int bmapi_flag;
2247 int quota_flag;
2248 int rt;
2249 xfs_trans_t *tp;
2250 xfs_bmbt_irec_t imaps[1], *imapp;
2251 xfs_bmap_free_t free_list;
2252 uint qblocks, resblks, resrtextents;
2253 int committed;
2254 int error;
2256 trace_xfs_alloc_file_space(ip);
2258 if (XFS_FORCED_SHUTDOWN(mp))
2259 return XFS_ERROR(EIO);
2261 error = xfs_qm_dqattach(ip, 0);
2262 if (error)
2263 return error;
2265 if (len <= 0)
2266 return XFS_ERROR(EINVAL);
2268 rt = XFS_IS_REALTIME_INODE(ip);
2269 extsz = xfs_get_extsz_hint(ip);
2271 count = len;
2272 imapp = &imaps[0];
2273 nimaps = 1;
2274 bmapi_flag = XFS_BMAPI_WRITE | alloc_type;
2275 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2276 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2279 * Allocate file space until done or until there is an error
2281 while (allocatesize_fsb && !error) {
2282 xfs_fileoff_t s, e;
2285 * Determine space reservations for data/realtime.
2287 if (unlikely(extsz)) {
2288 s = startoffset_fsb;
2289 do_div(s, extsz);
2290 s *= extsz;
2291 e = startoffset_fsb + allocatesize_fsb;
2292 if ((temp = do_mod(startoffset_fsb, extsz)))
2293 e += temp;
2294 if ((temp = do_mod(e, extsz)))
2295 e += extsz - temp;
2296 } else {
2297 s = 0;
2298 e = allocatesize_fsb;
2302 * The transaction reservation is limited to a 32-bit block
2303 * count, hence we need to limit the number of blocks we are
2304 * trying to reserve to avoid an overflow. We can't allocate
2305 * more than @nimaps extents, and an extent is limited on disk
2306 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
2308 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
2309 if (unlikely(rt)) {
2310 resrtextents = qblocks = resblks;
2311 resrtextents /= mp->m_sb.sb_rextsize;
2312 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2313 quota_flag = XFS_QMOPT_RES_RTBLKS;
2314 } else {
2315 resrtextents = 0;
2316 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
2317 quota_flag = XFS_QMOPT_RES_REGBLKS;
2321 * Allocate and setup the transaction.
2323 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2324 error = xfs_trans_reserve(tp, resblks,
2325 XFS_WRITE_LOG_RES(mp), resrtextents,
2326 XFS_TRANS_PERM_LOG_RES,
2327 XFS_WRITE_LOG_COUNT);
2329 * Check for running out of space
2331 if (error) {
2333 * Free the transaction structure.
2335 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2336 xfs_trans_cancel(tp, 0);
2337 break;
2339 xfs_ilock(ip, XFS_ILOCK_EXCL);
2340 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2341 0, quota_flag);
2342 if (error)
2343 goto error1;
2345 xfs_trans_ijoin(tp, ip);
2348 * Issue the xfs_bmapi() call to allocate the blocks
2350 xfs_bmap_init(&free_list, &firstfsb);
2351 error = xfs_bmapi(tp, ip, startoffset_fsb,
2352 allocatesize_fsb, bmapi_flag,
2353 &firstfsb, 0, imapp, &nimaps,
2354 &free_list);
2355 if (error) {
2356 goto error0;
2360 * Complete the transaction
2362 error = xfs_bmap_finish(&tp, &free_list, &committed);
2363 if (error) {
2364 goto error0;
2367 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2368 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2369 if (error) {
2370 break;
2373 allocated_fsb = imapp->br_blockcount;
2375 if (nimaps == 0) {
2376 error = XFS_ERROR(ENOSPC);
2377 break;
2380 startoffset_fsb += allocated_fsb;
2381 allocatesize_fsb -= allocated_fsb;
2384 return error;
2386 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
2387 xfs_bmap_cancel(&free_list);
2388 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
2390 error1: /* Just cancel transaction */
2391 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2392 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2393 return error;
2397 * Zero file bytes between startoff and endoff inclusive.
2398 * The iolock is held exclusive and no blocks are buffered.
2400 * This function is used by xfs_free_file_space() to zero
2401 * partial blocks when the range to free is not block aligned.
2402 * When unreserving space with boundaries that are not block
2403 * aligned we round up the start and round down the end
2404 * boundaries and then use this function to zero the parts of
2405 * the blocks that got dropped during the rounding.
2407 STATIC int
2408 xfs_zero_remaining_bytes(
2409 xfs_inode_t *ip,
2410 xfs_off_t startoff,
2411 xfs_off_t endoff)
2413 xfs_bmbt_irec_t imap;
2414 xfs_fileoff_t offset_fsb;
2415 xfs_off_t lastoffset;
2416 xfs_off_t offset;
2417 xfs_buf_t *bp;
2418 xfs_mount_t *mp = ip->i_mount;
2419 int nimap;
2420 int error = 0;
2423 * Avoid doing I/O beyond eof - it's not necessary
2424 * since nothing can read beyond eof. The space will
2425 * be zeroed when the file is extended anyway.
2427 if (startoff >= ip->i_size)
2428 return 0;
2430 if (endoff > ip->i_size)
2431 endoff = ip->i_size;
2433 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
2434 mp->m_rtdev_targp : mp->m_ddev_targp,
2435 mp->m_sb.sb_blocksize, XBF_DONT_BLOCK);
2436 if (!bp)
2437 return XFS_ERROR(ENOMEM);
2439 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2440 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2441 nimap = 1;
2442 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
2443 NULL, 0, &imap, &nimap, NULL);
2444 if (error || nimap < 1)
2445 break;
2446 ASSERT(imap.br_blockcount >= 1);
2447 ASSERT(imap.br_startoff == offset_fsb);
2448 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2449 if (lastoffset > endoff)
2450 lastoffset = endoff;
2451 if (imap.br_startblock == HOLESTARTBLOCK)
2452 continue;
2453 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2454 if (imap.br_state == XFS_EXT_UNWRITTEN)
2455 continue;
2456 XFS_BUF_UNDONE(bp);
2457 XFS_BUF_UNWRITE(bp);
2458 XFS_BUF_READ(bp);
2459 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
2460 xfsbdstrat(mp, bp);
2461 error = xfs_buf_iowait(bp);
2462 if (error) {
2463 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2464 mp, bp, XFS_BUF_ADDR(bp));
2465 break;
2467 memset(XFS_BUF_PTR(bp) +
2468 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2469 0, lastoffset - offset + 1);
2470 XFS_BUF_UNDONE(bp);
2471 XFS_BUF_UNREAD(bp);
2472 XFS_BUF_WRITE(bp);
2473 xfsbdstrat(mp, bp);
2474 error = xfs_buf_iowait(bp);
2475 if (error) {
2476 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2477 mp, bp, XFS_BUF_ADDR(bp));
2478 break;
2481 xfs_buf_free(bp);
2482 return error;
2486 * xfs_free_file_space()
2487 * This routine frees disk space for the given file.
2489 * This routine is only called by xfs_change_file_space
2490 * for an UNRESVSP type call.
2492 * RETURNS:
2493 * 0 on success
2494 * errno on error
2497 STATIC int
2498 xfs_free_file_space(
2499 xfs_inode_t *ip,
2500 xfs_off_t offset,
2501 xfs_off_t len,
2502 int attr_flags)
2504 int committed;
2505 int done;
2506 xfs_fileoff_t endoffset_fsb;
2507 int error;
2508 xfs_fsblock_t firstfsb;
2509 xfs_bmap_free_t free_list;
2510 xfs_bmbt_irec_t imap;
2511 xfs_off_t ioffset;
2512 xfs_extlen_t mod=0;
2513 xfs_mount_t *mp;
2514 int nimap;
2515 uint resblks;
2516 uint rounding;
2517 int rt;
2518 xfs_fileoff_t startoffset_fsb;
2519 xfs_trans_t *tp;
2520 int need_iolock = 1;
2522 mp = ip->i_mount;
2524 trace_xfs_free_file_space(ip);
2526 error = xfs_qm_dqattach(ip, 0);
2527 if (error)
2528 return error;
2530 error = 0;
2531 if (len <= 0) /* if nothing being freed */
2532 return error;
2533 rt = XFS_IS_REALTIME_INODE(ip);
2534 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2535 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
2537 if (attr_flags & XFS_ATTR_NOLOCK)
2538 need_iolock = 0;
2539 if (need_iolock) {
2540 xfs_ilock(ip, XFS_IOLOCK_EXCL);
2541 /* wait for the completion of any pending DIOs */
2542 xfs_ioend_wait(ip);
2545 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
2546 ioffset = offset & ~(rounding - 1);
2548 if (VN_CACHED(VFS_I(ip)) != 0) {
2549 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
2550 if (error)
2551 goto out_unlock_iolock;
2555 * Need to zero the stuff we're not freeing, on disk.
2556 * If it's a realtime file & can't use unwritten extents then we
2557 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2558 * will take care of it for us.
2560 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
2561 nimap = 1;
2562 error = xfs_bmapi(NULL, ip, startoffset_fsb,
2563 1, 0, NULL, 0, &imap, &nimap, NULL);
2564 if (error)
2565 goto out_unlock_iolock;
2566 ASSERT(nimap == 0 || nimap == 1);
2567 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2568 xfs_daddr_t block;
2570 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2571 block = imap.br_startblock;
2572 mod = do_div(block, mp->m_sb.sb_rextsize);
2573 if (mod)
2574 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2576 nimap = 1;
2577 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
2578 1, 0, NULL, 0, &imap, &nimap, NULL);
2579 if (error)
2580 goto out_unlock_iolock;
2581 ASSERT(nimap == 0 || nimap == 1);
2582 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2583 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2584 mod++;
2585 if (mod && (mod != mp->m_sb.sb_rextsize))
2586 endoffset_fsb -= mod;
2589 if ((done = (endoffset_fsb <= startoffset_fsb)))
2591 * One contiguous piece to clear
2593 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2594 else {
2596 * Some full blocks, possibly two pieces to clear
2598 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2599 error = xfs_zero_remaining_bytes(ip, offset,
2600 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2601 if (!error &&
2602 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2603 error = xfs_zero_remaining_bytes(ip,
2604 XFS_FSB_TO_B(mp, endoffset_fsb),
2605 offset + len - 1);
2609 * free file space until done or until there is an error
2611 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2612 while (!error && !done) {
2615 * allocate and setup the transaction. Allow this
2616 * transaction to dip into the reserve blocks to ensure
2617 * the freeing of the space succeeds at ENOSPC.
2619 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2620 tp->t_flags |= XFS_TRANS_RESERVE;
2621 error = xfs_trans_reserve(tp,
2622 resblks,
2623 XFS_WRITE_LOG_RES(mp),
2625 XFS_TRANS_PERM_LOG_RES,
2626 XFS_WRITE_LOG_COUNT);
2629 * check for running out of space
2631 if (error) {
2633 * Free the transaction structure.
2635 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2636 xfs_trans_cancel(tp, 0);
2637 break;
2639 xfs_ilock(ip, XFS_ILOCK_EXCL);
2640 error = xfs_trans_reserve_quota(tp, mp,
2641 ip->i_udquot, ip->i_gdquot,
2642 resblks, 0, XFS_QMOPT_RES_REGBLKS);
2643 if (error)
2644 goto error1;
2646 xfs_trans_ijoin(tp, ip);
2649 * issue the bunmapi() call to free the blocks
2651 xfs_bmap_init(&free_list, &firstfsb);
2652 error = xfs_bunmapi(tp, ip, startoffset_fsb,
2653 endoffset_fsb - startoffset_fsb,
2654 0, 2, &firstfsb, &free_list, &done);
2655 if (error) {
2656 goto error0;
2660 * complete the transaction
2662 error = xfs_bmap_finish(&tp, &free_list, &committed);
2663 if (error) {
2664 goto error0;
2667 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2668 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2671 out_unlock_iolock:
2672 if (need_iolock)
2673 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2674 return error;
2676 error0:
2677 xfs_bmap_cancel(&free_list);
2678 error1:
2679 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2680 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2681 XFS_ILOCK_EXCL);
2682 return error;
2686 * xfs_change_file_space()
2687 * This routine allocates or frees disk space for the given file.
2688 * The user specified parameters are checked for alignment and size
2689 * limitations.
2691 * RETURNS:
2692 * 0 on success
2693 * errno on error
2697 xfs_change_file_space(
2698 xfs_inode_t *ip,
2699 int cmd,
2700 xfs_flock64_t *bf,
2701 xfs_off_t offset,
2702 int attr_flags)
2704 xfs_mount_t *mp = ip->i_mount;
2705 int clrprealloc;
2706 int error;
2707 xfs_fsize_t fsize;
2708 int setprealloc;
2709 xfs_off_t startoffset;
2710 xfs_off_t llen;
2711 xfs_trans_t *tp;
2712 struct iattr iattr;
2713 int prealloc_type;
2715 if (!S_ISREG(ip->i_d.di_mode))
2716 return XFS_ERROR(EINVAL);
2718 switch (bf->l_whence) {
2719 case 0: /*SEEK_SET*/
2720 break;
2721 case 1: /*SEEK_CUR*/
2722 bf->l_start += offset;
2723 break;
2724 case 2: /*SEEK_END*/
2725 bf->l_start += ip->i_size;
2726 break;
2727 default:
2728 return XFS_ERROR(EINVAL);
2731 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2733 if ( (bf->l_start < 0)
2734 || (bf->l_start > XFS_MAXIOFFSET(mp))
2735 || (bf->l_start + llen < 0)
2736 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
2737 return XFS_ERROR(EINVAL);
2739 bf->l_whence = 0;
2741 startoffset = bf->l_start;
2742 fsize = ip->i_size;
2745 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2746 * file space.
2747 * These calls do NOT zero the data space allocated to the file,
2748 * nor do they change the file size.
2750 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2751 * space.
2752 * These calls cause the new file data to be zeroed and the file
2753 * size to be changed.
2755 setprealloc = clrprealloc = 0;
2756 prealloc_type = XFS_BMAPI_PREALLOC;
2758 switch (cmd) {
2759 case XFS_IOC_ZERO_RANGE:
2760 prealloc_type |= XFS_BMAPI_CONVERT;
2761 xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
2762 /* FALLTHRU */
2763 case XFS_IOC_RESVSP:
2764 case XFS_IOC_RESVSP64:
2765 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
2766 prealloc_type, attr_flags);
2767 if (error)
2768 return error;
2769 setprealloc = 1;
2770 break;
2772 case XFS_IOC_UNRESVSP:
2773 case XFS_IOC_UNRESVSP64:
2774 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2775 attr_flags)))
2776 return error;
2777 break;
2779 case XFS_IOC_ALLOCSP:
2780 case XFS_IOC_ALLOCSP64:
2781 case XFS_IOC_FREESP:
2782 case XFS_IOC_FREESP64:
2783 if (startoffset > fsize) {
2784 error = xfs_alloc_file_space(ip, fsize,
2785 startoffset - fsize, 0, attr_flags);
2786 if (error)
2787 break;
2790 iattr.ia_valid = ATTR_SIZE;
2791 iattr.ia_size = startoffset;
2793 error = xfs_setattr(ip, &iattr, attr_flags);
2795 if (error)
2796 return error;
2798 clrprealloc = 1;
2799 break;
2801 default:
2802 ASSERT(0);
2803 return XFS_ERROR(EINVAL);
2807 * update the inode timestamp, mode, and prealloc flag bits
2809 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2811 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2812 0, 0, 0))) {
2813 /* ASSERT(0); */
2814 xfs_trans_cancel(tp, 0);
2815 return error;
2818 xfs_ilock(ip, XFS_ILOCK_EXCL);
2820 xfs_trans_ijoin(tp, ip);
2822 if ((attr_flags & XFS_ATTR_DMI) == 0) {
2823 ip->i_d.di_mode &= ~S_ISUID;
2826 * Note that we don't have to worry about mandatory
2827 * file locking being disabled here because we only
2828 * clear the S_ISGID bit if the Group execute bit is
2829 * on, but if it was on then mandatory locking wouldn't
2830 * have been enabled.
2832 if (ip->i_d.di_mode & S_IXGRP)
2833 ip->i_d.di_mode &= ~S_ISGID;
2835 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2837 if (setprealloc)
2838 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2839 else if (clrprealloc)
2840 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2842 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2843 if (attr_flags & XFS_ATTR_SYNC)
2844 xfs_trans_set_sync(tp);
2846 error = xfs_trans_commit(tp, 0);
2848 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2850 return error;