2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
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
21 #include "xfs_types.h"
25 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_itable.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_alloc.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_utils.h"
50 #include "xfs_rtalloc.h"
51 #include "xfs_trans_space.h"
52 #include "xfs_log_priv.h"
53 #include "xfs_filestream.h"
54 #include "xfs_vnodeops.h"
62 xfs_mount_t *mp = ip->i_mount;
63 struct inode *inode = VFS_I(ip);
64 int mask = iattr->ia_valid;
72 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
77 if (mp->m_flags & XFS_MOUNT_RDONLY)
78 return XFS_ERROR(EROFS);
80 if (XFS_FORCED_SHUTDOWN(mp))
81 return XFS_ERROR(EIO);
83 code = -inode_change_ok(inode, iattr);
87 olddquot1 = olddquot2 = NULL;
91 * If disk quotas is on, we make sure that the dquots do exist on disk,
92 * before we start any other transactions. Trying to do this later
93 * is messy. We don't care to take a readlock to look at the ids
94 * in inode here, because we can't hold it across the trans_reserve.
95 * If the IDs do change before we take the ilock, we're covered
96 * because the i_*dquot fields will get updated anyway.
98 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
101 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
103 qflags |= XFS_QMOPT_UQUOTA;
105 uid = ip->i_d.di_uid;
107 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
109 qflags |= XFS_QMOPT_GQUOTA;
111 gid = ip->i_d.di_gid;
115 * We take a reference when we initialize udqp and gdqp,
116 * so it is important that we never blindly double trip on
117 * the same variable. See xfs_create() for an example.
119 ASSERT(udqp == NULL);
120 ASSERT(gdqp == NULL);
121 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, ip->i_d.di_projid,
122 qflags, &udqp, &gdqp);
128 * For the other attributes, we acquire the inode lock and
129 * first do an error checking pass.
132 lock_flags = XFS_ILOCK_EXCL;
133 if (flags & XFS_ATTR_NOLOCK)
135 if (!(mask & ATTR_SIZE)) {
136 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
137 (mp->m_flags & XFS_MOUNT_WSYNC)) {
138 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
140 if ((code = xfs_trans_reserve(tp, 0,
141 XFS_ICHANGE_LOG_RES(mp), 0,
148 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
149 !(flags & XFS_ATTR_DMI)) {
150 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
151 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
152 iattr->ia_size, 0, dmflags, NULL);
159 lock_flags |= XFS_IOLOCK_EXCL;
162 xfs_ilock(ip, lock_flags);
165 * Change file ownership. Must be the owner or privileged.
167 if (mask & (ATTR_UID|ATTR_GID)) {
169 * These IDs could have changed since we last looked at them.
170 * But, we're assured that if the ownership did change
171 * while we didn't have the inode locked, inode's dquot(s)
172 * would have changed also.
174 iuid = ip->i_d.di_uid;
175 igid = ip->i_d.di_gid;
176 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
177 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
180 * Do a quota reservation only if uid/gid is actually
183 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
184 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
186 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
187 capable(CAP_FOWNER) ?
188 XFS_QMOPT_FORCE_RES : 0);
189 if (code) /* out of quota */
195 * Truncate file. Must have write permission and not be a directory.
197 if (mask & ATTR_SIZE) {
198 /* Short circuit the truncate case for zero length files */
199 if (iattr->ia_size == 0 &&
200 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
201 xfs_iunlock(ip, XFS_ILOCK_EXCL);
202 lock_flags &= ~XFS_ILOCK_EXCL;
203 if (mask & ATTR_CTIME)
204 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
209 if (S_ISDIR(ip->i_d.di_mode)) {
210 code = XFS_ERROR(EISDIR);
212 } else if (!S_ISREG(ip->i_d.di_mode)) {
213 code = XFS_ERROR(EINVAL);
218 * Make sure that the dquots are attached to the inode.
220 code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
225 * Now we can make the changes. Before we join the inode
226 * to the transaction, if ATTR_SIZE is set then take care of
227 * the part of the truncation that must be done without the
228 * inode lock. This needs to be done before joining the inode
229 * to the transaction, because the inode cannot be unlocked
230 * once it is a part of the transaction.
232 if (iattr->ia_size > ip->i_size) {
234 * Do the first part of growing a file: zero any data
235 * in the last block that is beyond the old EOF. We
236 * need to do this before the inode is joined to the
237 * transaction to modify the i_size.
239 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
241 xfs_iunlock(ip, XFS_ILOCK_EXCL);
244 * We are going to log the inode size change in this
245 * transaction so any previous writes that are beyond the on
246 * disk EOF and the new EOF that have not been written out need
247 * to be written here. If we do not write the data out, we
248 * expose ourselves to the null files problem.
250 * Only flush from the on disk size to the smaller of the in
251 * memory file size or the new size as that's the range we
252 * really care about here and prevents waiting for other data
253 * not within the range we care about here.
256 ip->i_size != ip->i_d.di_size &&
257 iattr->ia_size > ip->i_d.di_size) {
258 code = xfs_flush_pages(ip,
259 ip->i_d.di_size, iattr->ia_size,
260 XFS_B_ASYNC, FI_NONE);
263 /* wait for all I/O to complete */
267 code = xfs_itruncate_data(ip, iattr->ia_size);
270 lock_flags &= ~XFS_ILOCK_EXCL;
271 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
274 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
275 if ((code = xfs_trans_reserve(tp, 0,
276 XFS_ITRUNCATE_LOG_RES(mp), 0,
277 XFS_TRANS_PERM_LOG_RES,
278 XFS_ITRUNCATE_LOG_COUNT))) {
279 xfs_trans_cancel(tp, 0);
281 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
284 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
285 xfs_ilock(ip, XFS_ILOCK_EXCL);
287 xfs_trans_ijoin(tp, ip, lock_flags);
288 xfs_trans_ihold(tp, ip);
291 * Only change the c/mtime if we are changing the size
292 * or we are explicitly asked to change it. This handles
293 * the semantic difference between truncate() and ftruncate()
294 * as implemented in the VFS.
296 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
297 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
299 if (iattr->ia_size > ip->i_size) {
300 ip->i_d.di_size = iattr->ia_size;
301 ip->i_size = iattr->ia_size;
302 if (!(flags & XFS_ATTR_DMI))
303 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
304 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
305 } else if (iattr->ia_size <= ip->i_size ||
306 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
308 * signal a sync transaction unless
309 * we're truncating an already unlinked
310 * file on a wsync filesystem
312 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
314 ((ip->i_d.di_nlink != 0 ||
315 !(mp->m_flags & XFS_MOUNT_WSYNC))
320 * Truncated "down", so we're removing references
321 * to old data here - if we now delay flushing for
322 * a long time, we expose ourselves unduly to the
323 * notorious NULL files problem. So, we mark this
324 * vnode and flush it when the file is closed, and
325 * do not wait the usual (long) time for writeout.
327 xfs_iflags_set(ip, XFS_ITRUNCATED);
330 xfs_trans_ijoin(tp, ip, lock_flags);
331 xfs_trans_ihold(tp, ip);
335 * Change file ownership. Must be the owner or privileged.
337 if (mask & (ATTR_UID|ATTR_GID)) {
339 * CAP_FSETID overrides the following restrictions:
341 * The set-user-ID and set-group-ID bits of a file will be
342 * cleared upon successful return from chown()
344 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
345 !capable(CAP_FSETID)) {
346 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
350 * Change the ownerships and register quota modifications
351 * in the transaction.
354 if (XFS_IS_UQUOTA_ON(mp)) {
355 ASSERT(mask & ATTR_UID);
357 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
358 &ip->i_udquot, udqp);
360 ip->i_d.di_uid = uid;
364 if (XFS_IS_GQUOTA_ON(mp)) {
365 ASSERT(!XFS_IS_PQUOTA_ON(mp));
366 ASSERT(mask & ATTR_GID);
368 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
369 &ip->i_gdquot, gdqp);
371 ip->i_d.di_gid = gid;
375 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
376 timeflags |= XFS_ICHGTIME_CHG;
380 * Change file access modes.
382 if (mask & ATTR_MODE) {
383 umode_t mode = iattr->ia_mode;
385 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
388 ip->i_d.di_mode &= S_IFMT;
389 ip->i_d.di_mode |= mode & ~S_IFMT;
391 inode->i_mode &= S_IFMT;
392 inode->i_mode |= mode & ~S_IFMT;
394 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
395 timeflags |= XFS_ICHGTIME_CHG;
399 * Change file access or modified times.
401 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
402 if (mask & ATTR_ATIME) {
403 inode->i_atime = iattr->ia_atime;
404 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
405 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
406 ip->i_update_core = 1;
408 if (mask & ATTR_MTIME) {
409 inode->i_mtime = iattr->ia_mtime;
410 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
411 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
412 timeflags &= ~XFS_ICHGTIME_MOD;
413 timeflags |= XFS_ICHGTIME_CHG;
415 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
416 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
420 * Change file inode change time only if ATTR_CTIME set
421 * AND we have been called by a DMI function.
424 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
425 inode->i_ctime = iattr->ia_ctime;
426 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
427 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
428 ip->i_update_core = 1;
429 timeflags &= ~XFS_ICHGTIME_CHG;
433 * Send out timestamp changes that need to be set to the
434 * current time. Not done when called by a DMI function.
436 if (timeflags && !(flags & XFS_ATTR_DMI))
437 xfs_ichgtime(ip, timeflags);
439 XFS_STATS_INC(xs_ig_attrchg);
442 * If this is a synchronous mount, make sure that the
443 * transaction goes to disk before returning to the user.
444 * This is slightly sub-optimal in that truncates require
445 * two sync transactions instead of one for wsync filesystems.
446 * One for the truncate and one for the timestamps since we
447 * don't want to change the timestamps unless we're sure the
448 * truncate worked. Truncates are less than 1% of the laddis
449 * mix so this probably isn't worth the trouble to optimize.
453 if (mp->m_flags & XFS_MOUNT_WSYNC)
454 xfs_trans_set_sync(tp);
456 code = xfs_trans_commit(tp, commit_flags);
459 xfs_iunlock(ip, lock_flags);
462 * Release any dquot(s) the inode had kept before chown.
464 XFS_QM_DQRELE(mp, olddquot1);
465 XFS_QM_DQRELE(mp, olddquot2);
466 XFS_QM_DQRELE(mp, udqp);
467 XFS_QM_DQRELE(mp, gdqp);
473 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
474 !(flags & XFS_ATTR_DMI)) {
475 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
476 NULL, DM_RIGHT_NULL, NULL, NULL,
477 0, 0, AT_DELAY_FLAG(flags));
482 commit_flags |= XFS_TRANS_ABORT;
485 XFS_QM_DQRELE(mp, udqp);
486 XFS_QM_DQRELE(mp, gdqp);
488 xfs_trans_cancel(tp, commit_flags);
490 if (lock_flags != 0) {
491 xfs_iunlock(ip, lock_flags);
497 * The maximum pathlen is 1024 bytes. Since the minimum file system
498 * blocksize is 512 bytes, we can get a max of 2 extents back from
501 #define SYMLINK_MAPS 2
508 xfs_mount_t *mp = ip->i_mount;
509 int pathlen = ip->i_d.di_size;
510 int nmaps = SYMLINK_MAPS;
511 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
518 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
519 mval, &nmaps, NULL, NULL);
523 for (n = 0; n < nmaps; n++) {
524 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
525 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
527 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
528 error = XFS_BUF_GETERROR(bp);
530 xfs_ioerror_alert("xfs_readlink",
531 ip->i_mount, bp, XFS_BUF_ADDR(bp));
535 if (pathlen < byte_cnt)
539 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
543 link[ip->i_d.di_size] = '\0';
555 xfs_mount_t *mp = ip->i_mount;
559 xfs_itrace_entry(ip);
561 if (XFS_FORCED_SHUTDOWN(mp))
562 return XFS_ERROR(EIO);
564 xfs_ilock(ip, XFS_ILOCK_SHARED);
566 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
567 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
569 pathlen = ip->i_d.di_size;
573 if (ip->i_df.if_flags & XFS_IFINLINE) {
574 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
575 link[pathlen] = '\0';
577 error = xfs_readlink_bmap(ip, link);
581 xfs_iunlock(ip, XFS_ILOCK_SHARED);
588 * This is called to sync the inode and its data out to disk. We need to hold
589 * the I/O lock while flushing the data, and the inode lock while flushing the
590 * inode. The inode lock CANNOT be held while flushing the data, so acquire
591 * after we're done with that.
599 int log_flushed = 0, changed = 1;
601 xfs_itrace_entry(ip);
603 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
604 return XFS_ERROR(EIO);
606 /* capture size updates in I/O completion before writing the inode. */
607 error = xfs_wait_on_pages(ip, 0, -1);
609 return XFS_ERROR(error);
612 * We always need to make sure that the required inode state is safe on
613 * disk. The vnode might be clean but we still might need to force the
614 * log because of committed transactions that haven't hit the disk yet.
615 * Likewise, there could be unflushed non-transactional changes to the
616 * inode core that have to go to disk and this requires us to issue
617 * a synchronous transaction to capture these changes correctly.
619 * This code relies on the assumption that if the update_* fields
620 * of the inode are clear and the inode is unpinned then it is clean
621 * and no action is required.
623 xfs_ilock(ip, XFS_ILOCK_SHARED);
625 if (!(ip->i_update_size || ip->i_update_core)) {
627 * Timestamps/size haven't changed since last inode flush or
628 * inode transaction commit. That means either nothing got
629 * written or a transaction committed which caught the updates.
630 * If the latter happened and the transaction hasn't hit the
631 * disk yet, the inode will be still be pinned. If it is,
635 xfs_iunlock(ip, XFS_ILOCK_SHARED);
637 if (xfs_ipincount(ip)) {
638 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
639 XFS_LOG_FORCE | XFS_LOG_SYNC,
643 * If the inode is not pinned and nothing has changed
644 * we don't need to flush the cache.
650 * Kick off a transaction to log the inode core to get the
651 * updates. The sync transaction will also force the log.
653 xfs_iunlock(ip, XFS_ILOCK_SHARED);
654 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
655 error = xfs_trans_reserve(tp, 0,
656 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
658 xfs_trans_cancel(tp, 0);
661 xfs_ilock(ip, XFS_ILOCK_EXCL);
664 * Note - it's possible that we might have pushed ourselves out
665 * of the way during trans_reserve which would flush the inode.
666 * But there's no guarantee that the inode buffer has actually
667 * gone out yet (it's delwri). Plus the buffer could be pinned
668 * anyway if it's part of an inode in another recent
669 * transaction. So we play it safe and fire off the
670 * transaction anyway.
672 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
673 xfs_trans_ihold(tp, ip);
674 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
675 xfs_trans_set_sync(tp);
676 error = _xfs_trans_commit(tp, 0, &log_flushed);
678 xfs_iunlock(ip, XFS_ILOCK_EXCL);
681 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
683 * If the log write didn't issue an ordered tag we need
684 * to flush the disk cache for the data device now.
687 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
690 * If this inode is on the RT dev we need to flush that
693 if (XFS_IS_REALTIME_INODE(ip))
694 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
701 * This is called by xfs_inactive to free any blocks beyond eof
702 * when the link count isn't zero and by xfs_dm_punch_hole() when
703 * punching a hole to EOF.
713 xfs_fileoff_t end_fsb;
714 xfs_fileoff_t last_fsb;
715 xfs_filblks_t map_len;
717 xfs_bmbt_irec_t imap;
718 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
721 * Figure out if there are any blocks beyond the end
722 * of the file. If not, then there is nothing to do.
724 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
725 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
726 map_len = last_fsb - end_fsb;
731 xfs_ilock(ip, XFS_ILOCK_SHARED);
732 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
733 NULL, 0, &imap, &nimaps, NULL, NULL);
734 xfs_iunlock(ip, XFS_ILOCK_SHARED);
736 if (!error && (nimaps != 0) &&
737 (imap.br_startblock != HOLESTARTBLOCK ||
738 ip->i_delayed_blks)) {
740 * Attach the dquots to the inode up front.
742 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
746 * There are blocks after the end of file.
747 * Free them up now by truncating the file to
750 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
753 * Do the xfs_itruncate_start() call before
754 * reserving any log space because
755 * itruncate_start will call into the buffer
757 * do that within a transaction.
760 xfs_ilock(ip, XFS_IOLOCK_EXCL);
761 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
764 xfs_trans_cancel(tp, 0);
766 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
770 error = xfs_trans_reserve(tp, 0,
771 XFS_ITRUNCATE_LOG_RES(mp),
772 0, XFS_TRANS_PERM_LOG_RES,
773 XFS_ITRUNCATE_LOG_COUNT);
775 ASSERT(XFS_FORCED_SHUTDOWN(mp));
776 xfs_trans_cancel(tp, 0);
777 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
781 xfs_ilock(ip, XFS_ILOCK_EXCL);
782 xfs_trans_ijoin(tp, ip,
785 xfs_trans_ihold(tp, ip);
787 error = xfs_itruncate_finish(&tp, ip,
792 * If we get an error at this point we
793 * simply don't bother truncating the file.
797 (XFS_TRANS_RELEASE_LOG_RES |
800 error = xfs_trans_commit(tp,
801 XFS_TRANS_RELEASE_LOG_RES);
803 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
810 * Free a symlink that has blocks associated with it.
813 xfs_inactive_symlink_rmt(
821 xfs_fsblock_t first_block;
822 xfs_bmap_free_t free_list;
825 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
833 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
835 * We're freeing a symlink that has some
836 * blocks allocated to it. Free the
837 * blocks here. We know that we've got
838 * either 1 or 2 extents and that we can
839 * free them all in one bunmapi call.
841 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
842 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
843 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
844 ASSERT(XFS_FORCED_SHUTDOWN(mp));
845 xfs_trans_cancel(tp, 0);
850 * Lock the inode, fix the size, and join it to the transaction.
851 * Hold it so in the normal path, we still have it locked for
852 * the second transaction. In the error paths we need it
853 * held so the cancel won't rele it, see below.
855 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
856 size = (int)ip->i_d.di_size;
858 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
859 xfs_trans_ihold(tp, ip);
860 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
862 * Find the block(s) so we can inval and unmap them.
865 xfs_bmap_init(&free_list, &first_block);
866 nmaps = ARRAY_SIZE(mval);
867 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
868 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
872 * Invalidate the block(s).
874 for (i = 0; i < nmaps; i++) {
875 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
876 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
877 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
878 xfs_trans_binval(tp, bp);
881 * Unmap the dead block(s) to the free_list.
883 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
884 &first_block, &free_list, NULL, &done)))
888 * Commit the first transaction. This logs the EFI and the inode.
890 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
893 * The transaction must have been committed, since there were
894 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
895 * The new tp has the extent freeing and EFDs.
899 * The first xact was committed, so add the inode to the new one.
900 * Mark it dirty so it will be logged and moved forward in the log as
901 * part of every commit.
903 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
904 xfs_trans_ihold(tp, ip);
905 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
907 * Get a new, empty transaction to return to our caller.
909 ntp = xfs_trans_dup(tp);
911 * Commit the transaction containing extent freeing and EFDs.
912 * If we get an error on the commit here or on the reserve below,
913 * we need to unlock the inode since the new transaction doesn't
914 * have the inode attached.
916 error = xfs_trans_commit(tp, 0);
919 ASSERT(XFS_FORCED_SHUTDOWN(mp));
923 * transaction commit worked ok so we can drop the extra ticket
924 * reference that we gained in xfs_trans_dup()
926 xfs_log_ticket_put(tp->t_ticket);
929 * Remove the memory for extent descriptions (just bookkeeping).
931 if (ip->i_df.if_bytes)
932 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
933 ASSERT(ip->i_df.if_bytes == 0);
935 * Put an itruncate log reservation in the new transaction
938 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
939 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
940 ASSERT(XFS_FORCED_SHUTDOWN(mp));
944 * Return with the inode locked but not joined to the transaction.
950 xfs_bmap_cancel(&free_list);
953 * Have to come here with the inode locked and either
954 * (held and in the transaction) or (not in the transaction).
955 * If the inode isn't held then cancel would iput it, but
956 * that's wrong since this is inactive and the vnode ref
957 * count is 0 already.
958 * Cancel won't do anything to the inode if held, but it still
959 * needs to be locked until the cancel is done, if it was
960 * joined to the transaction.
962 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
963 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
970 xfs_inactive_symlink_local(
976 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
978 * We're freeing a symlink which fit into
979 * the inode. Just free the memory used
980 * to hold the old symlink.
982 error = xfs_trans_reserve(*tpp, 0,
983 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
984 0, XFS_TRANS_PERM_LOG_RES,
985 XFS_ITRUNCATE_LOG_COUNT);
988 xfs_trans_cancel(*tpp, 0);
992 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
995 * Zero length symlinks _can_ exist.
997 if (ip->i_df.if_bytes > 0) {
998 xfs_idata_realloc(ip,
999 -(ip->i_df.if_bytes),
1001 ASSERT(ip->i_df.if_bytes == 0);
1015 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1018 ASSERT(ip->i_d.di_forkoff != 0);
1019 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1020 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1024 error = xfs_attr_inactive(ip);
1028 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1029 error = xfs_trans_reserve(tp, 0,
1030 XFS_IFREE_LOG_RES(mp),
1031 0, XFS_TRANS_PERM_LOG_RES,
1032 XFS_INACTIVE_LOG_COUNT);
1036 xfs_ilock(ip, XFS_ILOCK_EXCL);
1037 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1038 xfs_trans_ihold(tp, ip);
1039 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1041 ASSERT(ip->i_d.di_anextents == 0);
1047 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1048 xfs_trans_cancel(tp, 0);
1051 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1059 xfs_mount_t *mp = ip->i_mount;
1062 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1065 /* If this is a read-only mount, don't do this (would generate I/O) */
1066 if (mp->m_flags & XFS_MOUNT_RDONLY)
1069 if (!XFS_FORCED_SHUTDOWN(mp)) {
1073 * If we are using filestreams, and we have an unlinked
1074 * file that we are processing the last close on, then nothing
1075 * will be able to reopen and write to this file. Purge this
1076 * inode from the filestreams cache so that it doesn't delay
1077 * teardown of the inode.
1079 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1080 xfs_filestream_deassociate(ip);
1083 * If we previously truncated this file and removed old data
1084 * in the process, we want to initiate "early" writeout on
1085 * the last close. This is an attempt to combat the notorious
1086 * NULL files problem which is particularly noticable from a
1087 * truncate down, buffered (re-)write (delalloc), followed by
1088 * a crash. What we are effectively doing here is
1089 * significantly reducing the time window where we'd otherwise
1090 * be exposed to that problem.
1092 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1093 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
1094 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1097 if (ip->i_d.di_nlink != 0) {
1098 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1099 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1100 ip->i_delayed_blks > 0)) &&
1101 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1102 (!(ip->i_d.di_flags &
1103 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1104 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1116 * This is called when the vnode reference count for the vnode
1117 * goes to zero. If the file has been unlinked, then it must
1118 * now be truncated. Also, we clear all of the read-ahead state
1119 * kept for the inode here since the file is now closed.
1125 xfs_bmap_free_t free_list;
1126 xfs_fsblock_t first_block;
1133 xfs_itrace_entry(ip);
1136 * If the inode is already free, then there can be nothing
1139 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
1140 ASSERT(ip->i_df.if_real_bytes == 0);
1141 ASSERT(ip->i_df.if_broot_bytes == 0);
1142 return VN_INACTIVE_CACHE;
1146 * Only do a truncate if it's a regular file with
1147 * some actual space in it. It's OK to look at the
1148 * inode's fields without the lock because we're the
1149 * only one with a reference to the inode.
1151 truncate = ((ip->i_d.di_nlink == 0) &&
1152 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1153 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1154 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1158 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1159 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1163 /* If this is a read-only mount, don't do this (would generate I/O) */
1164 if (mp->m_flags & XFS_MOUNT_RDONLY)
1167 if (ip->i_d.di_nlink != 0) {
1168 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1169 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1170 ip->i_delayed_blks > 0)) &&
1171 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1172 (!(ip->i_d.di_flags &
1173 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1174 (ip->i_delayed_blks != 0)))) {
1175 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1177 return VN_INACTIVE_CACHE;
1182 ASSERT(ip->i_d.di_nlink == 0);
1184 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1185 return VN_INACTIVE_CACHE;
1187 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1190 * Do the xfs_itruncate_start() call before
1191 * reserving any log space because itruncate_start
1192 * will call into the buffer cache and we can't
1193 * do that within a transaction.
1195 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1197 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1199 xfs_trans_cancel(tp, 0);
1200 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1201 return VN_INACTIVE_CACHE;
1204 error = xfs_trans_reserve(tp, 0,
1205 XFS_ITRUNCATE_LOG_RES(mp),
1206 0, XFS_TRANS_PERM_LOG_RES,
1207 XFS_ITRUNCATE_LOG_COUNT);
1209 /* Don't call itruncate_cleanup */
1210 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1211 xfs_trans_cancel(tp, 0);
1212 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1213 return VN_INACTIVE_CACHE;
1216 xfs_ilock(ip, XFS_ILOCK_EXCL);
1217 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1218 xfs_trans_ihold(tp, ip);
1221 * normally, we have to run xfs_itruncate_finish sync.
1222 * But if filesystem is wsync and we're in the inactive
1223 * path, then we know that nlink == 0, and that the
1224 * xaction that made nlink == 0 is permanently committed
1225 * since xfs_remove runs as a synchronous transaction.
1227 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1228 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1231 xfs_trans_cancel(tp,
1232 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1233 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1234 return VN_INACTIVE_CACHE;
1236 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1239 * If we get an error while cleaning up a
1240 * symlink we bail out.
1242 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1243 xfs_inactive_symlink_rmt(ip, &tp) :
1244 xfs_inactive_symlink_local(ip, &tp);
1248 return VN_INACTIVE_CACHE;
1251 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1252 xfs_trans_ihold(tp, ip);
1254 error = xfs_trans_reserve(tp, 0,
1255 XFS_IFREE_LOG_RES(mp),
1256 0, XFS_TRANS_PERM_LOG_RES,
1257 XFS_INACTIVE_LOG_COUNT);
1259 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1260 xfs_trans_cancel(tp, 0);
1261 return VN_INACTIVE_CACHE;
1264 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1265 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1266 xfs_trans_ihold(tp, ip);
1270 * If there are attributes associated with the file
1271 * then blow them away now. The code calls a routine
1272 * that recursively deconstructs the attribute fork.
1273 * We need to just commit the current transaction
1274 * because we can't use it for xfs_attr_inactive().
1276 if (ip->i_d.di_anextents > 0) {
1277 error = xfs_inactive_attrs(ip, &tp);
1279 * If we got an error, the transaction is already
1280 * cancelled, and the inode is unlocked. Just get out.
1283 return VN_INACTIVE_CACHE;
1284 } else if (ip->i_afp) {
1285 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1291 xfs_bmap_init(&free_list, &first_block);
1292 error = xfs_ifree(tp, ip, &free_list);
1295 * If we fail to free the inode, shut down. The cancel
1296 * might do that, we need to make sure. Otherwise the
1297 * inode might be lost for a long time or forever.
1299 if (!XFS_FORCED_SHUTDOWN(mp)) {
1301 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1302 error, mp->m_fsname);
1303 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1305 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1308 * Credit the quota account(s). The inode is gone.
1310 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1313 * Just ignore errors at this point. There is nothing we can
1314 * do except to try to keep going. Make sure it's not a silent
1317 error = xfs_bmap_finish(&tp, &free_list, &committed);
1319 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1320 "xfs_bmap_finish() returned error %d", error);
1321 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1323 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1324 "xfs_trans_commit() returned error %d", error);
1327 * Release the dquots held by inode, if any.
1329 XFS_QM_DQDETACH(mp, ip);
1331 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1334 return VN_INACTIVE_CACHE;
1338 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1339 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1340 * ci_name->name will point to a the actual name (caller must free) or
1341 * will be set to NULL if an exact match is found.
1346 struct xfs_name *name,
1348 struct xfs_name *ci_name)
1354 xfs_itrace_entry(dp);
1356 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1357 return XFS_ERROR(EIO);
1359 lock_mode = xfs_ilock_map_shared(dp);
1360 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1361 xfs_iunlock_map_shared(dp, lock_mode);
1366 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1370 xfs_itrace_ref(*ipp);
1375 kmem_free(ci_name->name);
1384 struct xfs_name *name,
1390 int is_dir = S_ISDIR(mode);
1391 struct xfs_mount *mp = dp->i_mount;
1392 struct xfs_inode *ip = NULL;
1393 struct xfs_trans *tp = NULL;
1395 xfs_bmap_free_t free_list;
1396 xfs_fsblock_t first_block;
1397 boolean_t unlock_dp_on_error = B_FALSE;
1401 struct xfs_dquot *udqp = NULL;
1402 struct xfs_dquot *gdqp = NULL;
1407 xfs_itrace_entry(dp);
1409 if (XFS_FORCED_SHUTDOWN(mp))
1410 return XFS_ERROR(EIO);
1412 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1413 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1414 dp, DM_RIGHT_NULL, NULL,
1415 DM_RIGHT_NULL, name->name, NULL,
1422 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1423 prid = dp->i_d.di_projid;
1428 * Make sure that we have allocated dquot(s) on disk.
1430 error = XFS_QM_DQVOPALLOC(mp, dp,
1431 current_fsuid(), current_fsgid(), prid,
1432 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1438 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1439 log_res = XFS_MKDIR_LOG_RES(mp);
1440 log_count = XFS_MKDIR_LOG_COUNT;
1441 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1443 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1444 log_res = XFS_CREATE_LOG_RES(mp);
1445 log_count = XFS_CREATE_LOG_COUNT;
1446 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1449 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1452 * Initially assume that the file does not exist and
1453 * reserve the resources for that case. If that is not
1454 * the case we'll drop the one we have and get a more
1455 * appropriate transaction later.
1457 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1458 XFS_TRANS_PERM_LOG_RES, log_count);
1459 if (error == ENOSPC) {
1460 /* flush outstanding delalloc blocks and retry */
1461 xfs_flush_inodes(dp);
1462 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1463 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1465 if (error == ENOSPC) {
1466 /* No space at all so try a "no-allocation" reservation */
1468 error = xfs_trans_reserve(tp, 0, log_res, 0,
1469 XFS_TRANS_PERM_LOG_RES, log_count);
1473 goto out_trans_cancel;
1476 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1477 unlock_dp_on_error = B_TRUE;
1480 * Check for directory link count overflow.
1482 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1483 error = XFS_ERROR(EMLINK);
1484 goto out_trans_cancel;
1487 xfs_bmap_init(&free_list, &first_block);
1490 * Reserve disk quota and the inode.
1492 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1494 goto out_trans_cancel;
1496 error = xfs_dir_canenter(tp, dp, name, resblks);
1498 goto out_trans_cancel;
1501 * A newly created regular or special file just has one directory
1502 * entry pointing to them, but a directory also the "." entry
1503 * pointing to itself.
1505 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1506 prid, resblks > 0, &ip, &committed);
1508 if (error == ENOSPC)
1509 goto out_trans_cancel;
1510 goto out_trans_abort;
1514 * At this point, we've gotten a newly allocated inode.
1515 * It is locked (and joined to the transaction).
1518 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1521 * Now we join the directory inode to the transaction. We do not do it
1522 * earlier because xfs_dir_ialloc might commit the previous transaction
1523 * (and release all the locks). An error from here on will result in
1524 * the transaction cancel unlocking dp so don't do it explicitly in the
1528 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1529 unlock_dp_on_error = B_FALSE;
1531 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1532 &first_block, &free_list, resblks ?
1533 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1535 ASSERT(error != ENOSPC);
1536 goto out_trans_abort;
1538 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1539 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1542 error = xfs_dir_init(tp, ip, dp);
1544 goto out_bmap_cancel;
1546 error = xfs_bumplink(tp, dp);
1548 goto out_bmap_cancel;
1552 * If this is a synchronous mount, make sure that the
1553 * create transaction goes to disk before returning to
1556 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1557 xfs_trans_set_sync(tp);
1560 * Attach the dquot(s) to the inodes and modify them incore.
1561 * These ids of the inode couldn't have changed since the new
1562 * inode has been locked ever since it was created.
1564 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1567 * xfs_trans_commit normally decrements the vnode ref count
1568 * when it unlocks the inode. Since we want to return the
1569 * vnode to the caller, we bump the vnode ref count now.
1573 error = xfs_bmap_finish(&tp, &free_list, &committed);
1575 goto out_abort_rele;
1577 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1583 XFS_QM_DQRELE(mp, udqp);
1584 XFS_QM_DQRELE(mp, gdqp);
1588 /* Fallthrough to std_return with error = 0 */
1590 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1591 XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
1592 ip, DM_RIGHT_NULL, name->name, NULL, mode,
1599 xfs_bmap_cancel(&free_list);
1601 cancel_flags |= XFS_TRANS_ABORT;
1603 xfs_trans_cancel(tp, cancel_flags);
1605 XFS_QM_DQRELE(mp, udqp);
1606 XFS_QM_DQRELE(mp, gdqp);
1608 if (unlock_dp_on_error)
1609 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1615 * Wait until after the current transaction is aborted to
1616 * release the inode. This prevents recursive transactions
1617 * and deadlocks from xfs_inactive.
1619 xfs_bmap_cancel(&free_list);
1620 cancel_flags |= XFS_TRANS_ABORT;
1621 xfs_trans_cancel(tp, cancel_flags);
1623 unlock_dp_on_error = B_FALSE;
1629 int xfs_small_retries;
1630 int xfs_middle_retries;
1631 int xfs_lots_retries;
1632 int xfs_lock_delays;
1636 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1640 xfs_lock_inumorder(int lock_mode, int subclass)
1642 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1643 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1644 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1645 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1651 * The following routine will lock n inodes in exclusive mode.
1652 * We assume the caller calls us with the inodes in i_ino order.
1654 * We need to detect deadlock where an inode that we lock
1655 * is in the AIL and we start waiting for another inode that is locked
1656 * by a thread in a long running transaction (such as truncate). This can
1657 * result in deadlock since the long running trans might need to wait
1658 * for the inode we just locked in order to push the tail and free space
1667 int attempts = 0, i, j, try_lock;
1670 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1676 for (; i < inodes; i++) {
1679 if (i && (ips[i] == ips[i-1])) /* Already locked */
1683 * If try_lock is not set yet, make sure all locked inodes
1684 * are not in the AIL.
1685 * If any are, set try_lock to be used later.
1689 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1690 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1691 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1698 * If any of the previous locks we have locked is in the AIL,
1699 * we must TRY to get the second and subsequent locks. If
1700 * we can't get any, we must release all we have
1705 /* try_lock must be 0 if i is 0. */
1707 * try_lock means we have an inode locked
1708 * that is in the AIL.
1711 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1715 * Unlock all previous guys and try again.
1716 * xfs_iunlock will try to push the tail
1717 * if the inode is in the AIL.
1720 for(j = i - 1; j >= 0; j--) {
1723 * Check to see if we've already
1724 * unlocked this one.
1725 * Not the first one going back,
1726 * and the inode ptr is the same.
1728 if ((j != (i - 1)) && ips[j] ==
1732 xfs_iunlock(ips[j], lock_mode);
1735 if ((attempts % 5) == 0) {
1736 delay(1); /* Don't just spin the CPU */
1746 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1752 if (attempts < 5) xfs_small_retries++;
1753 else if (attempts < 100) xfs_middle_retries++;
1754 else xfs_lots_retries++;
1762 * xfs_lock_two_inodes() can only be used to lock one type of lock
1763 * at a time - the iolock or the ilock, but not both at once. If
1764 * we lock both at once, lockdep will report false positives saying
1765 * we have violated locking orders.
1768 xfs_lock_two_inodes(
1777 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1778 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1779 ASSERT(ip0->i_ino != ip1->i_ino);
1781 if (ip0->i_ino > ip1->i_ino) {
1788 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1791 * If the first lock we have locked is in the AIL, we must TRY to get
1792 * the second lock. If we can't get it, we must release the first one
1795 lp = (xfs_log_item_t *)ip0->i_itemp;
1796 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1797 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1798 xfs_iunlock(ip0, lock_mode);
1799 if ((++attempts % 5) == 0)
1800 delay(1); /* Don't just spin the CPU */
1804 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1811 struct xfs_name *name,
1814 xfs_mount_t *mp = dp->i_mount;
1815 xfs_trans_t *tp = NULL;
1816 int is_dir = S_ISDIR(ip->i_d.di_mode);
1818 xfs_bmap_free_t free_list;
1819 xfs_fsblock_t first_block;
1826 xfs_itrace_entry(dp);
1827 xfs_itrace_entry(ip);
1829 if (XFS_FORCED_SHUTDOWN(mp))
1830 return XFS_ERROR(EIO);
1832 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
1833 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1834 NULL, DM_RIGHT_NULL, name->name, NULL,
1835 ip->i_d.di_mode, 0, 0);
1840 error = XFS_QM_DQATTACH(mp, dp, 0);
1844 error = XFS_QM_DQATTACH(mp, ip, 0);
1849 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1850 log_count = XFS_DEFAULT_LOG_COUNT;
1852 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1853 log_count = XFS_REMOVE_LOG_COUNT;
1855 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1858 * We try to get the real space reservation first,
1859 * allowing for directory btree deletion(s) implying
1860 * possible bmap insert(s). If we can't get the space
1861 * reservation then we use 0 instead, and avoid the bmap
1862 * btree insert(s) in the directory code by, if the bmap
1863 * insert tries to happen, instead trimming the LAST
1864 * block from the directory.
1866 resblks = XFS_REMOVE_SPACE_RES(mp);
1867 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1868 XFS_TRANS_PERM_LOG_RES, log_count);
1869 if (error == ENOSPC) {
1871 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1872 XFS_TRANS_PERM_LOG_RES, log_count);
1875 ASSERT(error != ENOSPC);
1877 goto out_trans_cancel;
1880 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1883 * At this point, we've gotten both the directory and the entry
1887 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1890 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1893 * If we're removing a directory perform some additional validation.
1896 ASSERT(ip->i_d.di_nlink >= 2);
1897 if (ip->i_d.di_nlink != 2) {
1898 error = XFS_ERROR(ENOTEMPTY);
1899 goto out_trans_cancel;
1901 if (!xfs_dir_isempty(ip)) {
1902 error = XFS_ERROR(ENOTEMPTY);
1903 goto out_trans_cancel;
1907 xfs_bmap_init(&free_list, &first_block);
1908 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1909 &first_block, &free_list, resblks);
1911 ASSERT(error != ENOENT);
1912 goto out_bmap_cancel;
1914 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1918 * Drop the link from ip's "..".
1920 error = xfs_droplink(tp, dp);
1922 goto out_bmap_cancel;
1925 * Drop the "." link from ip to self.
1927 error = xfs_droplink(tp, ip);
1929 goto out_bmap_cancel;
1932 * When removing a non-directory we need to log the parent
1933 * inode here. For a directory this is done implicitly
1934 * by the xfs_droplink call for the ".." entry.
1936 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1940 * Drop the link from dp to ip.
1942 error = xfs_droplink(tp, ip);
1944 goto out_bmap_cancel;
1947 * Determine if this is the last link while
1948 * we are in the transaction.
1950 link_zero = (ip->i_d.di_nlink == 0);
1953 * If this is a synchronous mount, make sure that the
1954 * remove transaction goes to disk before returning to
1957 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1958 xfs_trans_set_sync(tp);
1960 error = xfs_bmap_finish(&tp, &free_list, &committed);
1962 goto out_bmap_cancel;
1964 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1969 * If we are using filestreams, kill the stream association.
1970 * If the file is still open it may get a new one but that
1971 * will get killed on last close in xfs_close() so we don't
1972 * have to worry about that.
1974 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
1975 xfs_filestream_deassociate(ip);
1977 xfs_itrace_exit(ip);
1978 xfs_itrace_exit(dp);
1981 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
1982 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
1983 NULL, DM_RIGHT_NULL, name->name, NULL,
1984 ip->i_d.di_mode, error, 0);
1990 xfs_bmap_cancel(&free_list);
1991 cancel_flags |= XFS_TRANS_ABORT;
1993 xfs_trans_cancel(tp, cancel_flags);
2001 struct xfs_name *target_name)
2003 xfs_mount_t *mp = tdp->i_mount;
2006 xfs_bmap_free_t free_list;
2007 xfs_fsblock_t first_block;
2012 xfs_itrace_entry(tdp);
2013 xfs_itrace_entry(sip);
2015 ASSERT(!S_ISDIR(sip->i_d.di_mode));
2017 if (XFS_FORCED_SHUTDOWN(mp))
2018 return XFS_ERROR(EIO);
2020 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2021 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2024 target_name->name, NULL, 0, 0, 0);
2029 /* Return through std_return after this point. */
2031 error = XFS_QM_DQATTACH(mp, sip, 0);
2035 error = XFS_QM_DQATTACH(mp, tdp, 0);
2039 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2040 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2041 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
2042 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2043 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2044 if (error == ENOSPC) {
2046 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2047 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2054 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
2057 * Increment vnode ref counts since xfs_trans_commit &
2058 * xfs_trans_cancel will both unlock the inodes and
2059 * decrement the associated ref counts.
2063 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2064 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2067 * If the source has too many links, we can't make any more to it.
2069 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2070 error = XFS_ERROR(EMLINK);
2075 * If we are using project inheritance, we only allow hard link
2076 * creation in our tree when the project IDs are the same; else
2077 * the tree quota mechanism could be circumvented.
2079 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2080 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2081 error = XFS_ERROR(EXDEV);
2085 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2089 xfs_bmap_init(&free_list, &first_block);
2091 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2092 &first_block, &free_list, resblks);
2095 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2096 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2098 error = xfs_bumplink(tp, sip);
2103 * If this is a synchronous mount, make sure that the
2104 * link transaction goes to disk before returning to
2107 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2108 xfs_trans_set_sync(tp);
2111 error = xfs_bmap_finish (&tp, &free_list, &committed);
2113 xfs_bmap_cancel(&free_list);
2117 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2121 /* Fall through to std_return with error = 0. */
2123 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2124 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2127 target_name->name, NULL, 0, error, 0);
2132 cancel_flags |= XFS_TRANS_ABORT;
2136 xfs_trans_cancel(tp, cancel_flags);
2143 struct xfs_name *link_name,
2144 const char *target_path,
2149 xfs_mount_t *mp = dp->i_mount;
2154 xfs_bmap_free_t free_list;
2155 xfs_fsblock_t first_block;
2156 boolean_t unlock_dp_on_error = B_FALSE;
2159 xfs_fileoff_t first_fsb;
2160 xfs_filblks_t fs_blocks;
2162 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2164 const char *cur_chunk;
2169 struct xfs_dquot *udqp, *gdqp;
2177 xfs_itrace_entry(dp);
2179 if (XFS_FORCED_SHUTDOWN(mp))
2180 return XFS_ERROR(EIO);
2183 * Check component lengths of the target path name.
2185 pathlen = strlen(target_path);
2186 if (pathlen >= MAXPATHLEN) /* total string too long */
2187 return XFS_ERROR(ENAMETOOLONG);
2189 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
2190 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
2191 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2192 link_name->name, target_path, 0, 0, 0);
2197 /* Return through std_return after this point. */
2200 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2201 prid = dp->i_d.di_projid;
2203 prid = (xfs_prid_t)dfltprid;
2206 * Make sure that we have allocated dquot(s) on disk.
2208 error = XFS_QM_DQVOPALLOC(mp, dp,
2209 current_fsuid(), current_fsgid(), prid,
2210 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2214 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2215 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2217 * The symlink will fit into the inode data fork?
2218 * There can't be any attributes so we get the whole variable part.
2220 if (pathlen <= XFS_LITINO(mp))
2223 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2224 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2225 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2226 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2227 if (error == ENOSPC && fs_blocks == 0) {
2229 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2230 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2237 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2238 unlock_dp_on_error = B_TRUE;
2241 * Check whether the directory allows new symlinks or not.
2243 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2244 error = XFS_ERROR(EPERM);
2249 * Reserve disk quota : blocks and inode.
2251 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2256 * Check for ability to enter directory entry, if no space reserved.
2258 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2262 * Initialize the bmap freelist prior to calling either
2263 * bmapi or the directory create code.
2265 xfs_bmap_init(&free_list, &first_block);
2268 * Allocate an inode for the symlink.
2270 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
2271 1, 0, credp, prid, resblks > 0, &ip, NULL);
2273 if (error == ENOSPC)
2280 * An error after we've joined dp to the transaction will result in the
2281 * transaction cancel unlocking dp so don't do it explicitly in the
2285 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2286 unlock_dp_on_error = B_FALSE;
2289 * Also attach the dquot(s) to it, if applicable.
2291 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2294 resblks -= XFS_IALLOC_SPACE_RES(mp);
2296 * If the symlink will fit into the inode, write it inline.
2298 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2299 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2300 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2301 ip->i_d.di_size = pathlen;
2304 * The inode was initially created in extent format.
2306 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2307 ip->i_df.if_flags |= XFS_IFINLINE;
2309 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2310 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2314 nmaps = SYMLINK_MAPS;
2316 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2317 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2318 &first_block, resblks, mval, &nmaps,
2325 resblks -= fs_blocks;
2326 ip->i_d.di_size = pathlen;
2327 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2329 cur_chunk = target_path;
2330 for (n = 0; n < nmaps; n++) {
2331 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2332 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2333 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2334 BTOBB(byte_cnt), 0);
2335 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2336 if (pathlen < byte_cnt) {
2339 pathlen -= byte_cnt;
2341 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2342 cur_chunk += byte_cnt;
2344 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2349 * Create the directory entry for the symlink.
2351 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2352 &first_block, &free_list, resblks);
2355 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2356 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2359 * If this is a synchronous mount, make sure that the
2360 * symlink transaction goes to disk before returning to
2363 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2364 xfs_trans_set_sync(tp);
2368 * xfs_trans_commit normally decrements the vnode ref count
2369 * when it unlocks the inode. Since we want to return the
2370 * vnode to the caller, we bump the vnode ref count now.
2374 error = xfs_bmap_finish(&tp, &free_list, &committed);
2378 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2379 XFS_QM_DQRELE(mp, udqp);
2380 XFS_QM_DQRELE(mp, gdqp);
2382 /* Fall through to std_return with error = 0 or errno from
2383 * xfs_trans_commit */
2385 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
2386 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
2389 DM_RIGHT_NULL, link_name->name,
2390 target_path, 0, error, 0);
2400 xfs_bmap_cancel(&free_list);
2401 cancel_flags |= XFS_TRANS_ABORT;
2403 xfs_trans_cancel(tp, cancel_flags);
2404 XFS_QM_DQRELE(mp, udqp);
2405 XFS_QM_DQRELE(mp, gdqp);
2407 if (unlock_dp_on_error)
2408 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2419 xfs_mount_t *mp = ip->i_mount;
2423 if (!capable(CAP_SYS_ADMIN))
2424 return XFS_ERROR(EPERM);
2426 if (XFS_FORCED_SHUTDOWN(mp))
2427 return XFS_ERROR(EIO);
2429 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2430 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2432 xfs_trans_cancel(tp, 0);
2435 xfs_ilock(ip, XFS_ILOCK_EXCL);
2436 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2438 ip->i_d.di_dmevmask = evmask;
2439 ip->i_d.di_dmstate = state;
2441 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2443 error = xfs_trans_commit(tp, 0);
2453 xfs_itrace_entry(ip);
2455 ASSERT(!VN_MAPPED(VFS_I(ip)));
2457 /* bad inode, get out here ASAP */
2458 if (is_bad_inode(VFS_I(ip))) {
2465 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
2468 * Make sure the atime in the XFS inode is correct before freeing the
2471 xfs_synchronize_atime(ip);
2474 * If we have nothing to flush with this inode then complete the
2475 * teardown now, otherwise break the link between the xfs inode and the
2476 * linux inode and clean up the xfs inode later. This avoids flushing
2477 * the inode to disk during the delete operation itself.
2479 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2480 * first to ensure that xfs_iunpin() will never see an xfs inode
2481 * that has a linux inode being reclaimed. Synchronisation is provided
2482 * by the i_flags_lock.
2484 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2485 xfs_ilock(ip, XFS_ILOCK_EXCL);
2487 xfs_iflags_set(ip, XFS_IRECLAIMABLE);
2488 return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
2490 xfs_inode_set_reclaim_tag(ip);
2495 * xfs_alloc_file_space()
2496 * This routine allocates disk space for the given file.
2498 * If alloc_type == 0, this request is for an ALLOCSP type
2499 * request which will change the file size. In this case, no
2500 * DMAPI event will be generated by the call. A TRUNCATE event
2501 * will be generated later by xfs_setattr.
2503 * If alloc_type != 0, this request is for a RESVSP type
2504 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2505 * lower block boundary byte address is less than the file's
2514 xfs_alloc_file_space(
2521 xfs_mount_t *mp = ip->i_mount;
2523 xfs_filblks_t allocated_fsb;
2524 xfs_filblks_t allocatesize_fsb;
2525 xfs_extlen_t extsz, temp;
2526 xfs_fileoff_t startoffset_fsb;
2527 xfs_fsblock_t firstfsb;
2533 xfs_bmbt_irec_t imaps[1], *imapp;
2534 xfs_bmap_free_t free_list;
2535 uint qblocks, resblks, resrtextents;
2539 xfs_itrace_entry(ip);
2541 if (XFS_FORCED_SHUTDOWN(mp))
2542 return XFS_ERROR(EIO);
2544 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2548 return XFS_ERROR(EINVAL);
2550 rt = XFS_IS_REALTIME_INODE(ip);
2551 extsz = xfs_get_extsz_hint(ip);
2556 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
2557 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2558 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2560 /* Generate a DMAPI event if needed. */
2561 if (alloc_type != 0 && offset < ip->i_size &&
2562 (attr_flags & XFS_ATTR_DMI) == 0 &&
2563 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2564 xfs_off_t end_dmi_offset;
2566 end_dmi_offset = offset+len;
2567 if (end_dmi_offset > ip->i_size)
2568 end_dmi_offset = ip->i_size;
2569 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2570 end_dmi_offset - offset, 0, NULL);
2576 * Allocate file space until done or until there is an error
2579 while (allocatesize_fsb && !error) {
2583 * Determine space reservations for data/realtime.
2585 if (unlikely(extsz)) {
2586 s = startoffset_fsb;
2589 e = startoffset_fsb + allocatesize_fsb;
2590 if ((temp = do_mod(startoffset_fsb, extsz)))
2592 if ((temp = do_mod(e, extsz)))
2596 e = allocatesize_fsb;
2600 resrtextents = qblocks = (uint)(e - s);
2601 resrtextents /= mp->m_sb.sb_rextsize;
2602 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2603 quota_flag = XFS_QMOPT_RES_RTBLKS;
2606 resblks = qblocks = \
2607 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2608 quota_flag = XFS_QMOPT_RES_REGBLKS;
2612 * Allocate and setup the transaction.
2614 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2615 error = xfs_trans_reserve(tp, resblks,
2616 XFS_WRITE_LOG_RES(mp), resrtextents,
2617 XFS_TRANS_PERM_LOG_RES,
2618 XFS_WRITE_LOG_COUNT);
2620 * Check for running out of space
2624 * Free the transaction structure.
2626 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2627 xfs_trans_cancel(tp, 0);
2630 xfs_ilock(ip, XFS_ILOCK_EXCL);
2631 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
2632 qblocks, 0, quota_flag);
2636 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2637 xfs_trans_ihold(tp, ip);
2640 * Issue the xfs_bmapi() call to allocate the blocks
2642 xfs_bmap_init(&free_list, &firstfsb);
2643 error = xfs_bmapi(tp, ip, startoffset_fsb,
2644 allocatesize_fsb, bmapi_flag,
2645 &firstfsb, 0, imapp, &nimaps,
2652 * Complete the transaction
2654 error = xfs_bmap_finish(&tp, &free_list, &committed);
2659 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2660 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2665 allocated_fsb = imapp->br_blockcount;
2668 error = XFS_ERROR(ENOSPC);
2672 startoffset_fsb += allocated_fsb;
2673 allocatesize_fsb -= allocated_fsb;
2676 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
2677 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
2678 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
2681 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
2683 goto retry; /* Maybe DMAPI app. has made space */
2684 /* else fall through with error from XFS_SEND_DATA */
2689 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
2690 xfs_bmap_cancel(&free_list);
2691 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
2693 error1: /* Just cancel transaction */
2694 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2695 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2696 goto dmapi_enospc_check;
2700 * Zero file bytes between startoff and endoff inclusive.
2701 * The iolock is held exclusive and no blocks are buffered.
2703 * This function is used by xfs_free_file_space() to zero
2704 * partial blocks when the range to free is not block aligned.
2705 * When unreserving space with boundaries that are not block
2706 * aligned we round up the start and round down the end
2707 * boundaries and then use this function to zero the parts of
2708 * the blocks that got dropped during the rounding.
2711 xfs_zero_remaining_bytes(
2716 xfs_bmbt_irec_t imap;
2717 xfs_fileoff_t offset_fsb;
2718 xfs_off_t lastoffset;
2721 xfs_mount_t *mp = ip->i_mount;
2726 * Avoid doing I/O beyond eof - it's not necessary
2727 * since nothing can read beyond eof. The space will
2728 * be zeroed when the file is extended anyway.
2730 if (startoff >= ip->i_size)
2733 if (endoff > ip->i_size)
2734 endoff = ip->i_size;
2736 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
2737 XFS_IS_REALTIME_INODE(ip) ?
2738 mp->m_rtdev_targp : mp->m_ddev_targp);
2740 return XFS_ERROR(ENOMEM);
2742 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2743 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2745 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
2746 NULL, 0, &imap, &nimap, NULL, NULL);
2747 if (error || nimap < 1)
2749 ASSERT(imap.br_blockcount >= 1);
2750 ASSERT(imap.br_startoff == offset_fsb);
2751 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2752 if (lastoffset > endoff)
2753 lastoffset = endoff;
2754 if (imap.br_startblock == HOLESTARTBLOCK)
2756 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2757 if (imap.br_state == XFS_EXT_UNWRITTEN)
2760 XFS_BUF_UNWRITE(bp);
2762 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
2764 error = xfs_iowait(bp);
2766 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2767 mp, bp, XFS_BUF_ADDR(bp));
2770 memset(XFS_BUF_PTR(bp) +
2771 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2772 0, lastoffset - offset + 1);
2777 error = xfs_iowait(bp);
2779 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2780 mp, bp, XFS_BUF_ADDR(bp));
2789 * xfs_free_file_space()
2790 * This routine frees disk space for the given file.
2792 * This routine is only called by xfs_change_file_space
2793 * for an UNRESVSP type call.
2801 xfs_free_file_space(
2809 xfs_off_t end_dmi_offset;
2810 xfs_fileoff_t endoffset_fsb;
2812 xfs_fsblock_t firstfsb;
2813 xfs_bmap_free_t free_list;
2814 xfs_bmbt_irec_t imap;
2822 xfs_fileoff_t startoffset_fsb;
2824 int need_iolock = 1;
2828 xfs_itrace_entry(ip);
2830 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2834 if (len <= 0) /* if nothing being freed */
2836 rt = XFS_IS_REALTIME_INODE(ip);
2837 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2838 end_dmi_offset = offset + len;
2839 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
2841 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
2842 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2843 if (end_dmi_offset > ip->i_size)
2844 end_dmi_offset = ip->i_size;
2845 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
2846 offset, end_dmi_offset - offset,
2847 AT_DELAY_FLAG(attr_flags), NULL);
2852 if (attr_flags & XFS_ATTR_NOLOCK)
2855 xfs_ilock(ip, XFS_IOLOCK_EXCL);
2856 /* wait for the completion of any pending DIOs */
2860 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
2861 ioffset = offset & ~(rounding - 1);
2863 if (VN_CACHED(VFS_I(ip)) != 0) {
2864 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
2865 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
2867 goto out_unlock_iolock;
2871 * Need to zero the stuff we're not freeing, on disk.
2872 * If it's a realtime file & can't use unwritten extents then we
2873 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2874 * will take care of it for us.
2876 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
2878 error = xfs_bmapi(NULL, ip, startoffset_fsb,
2879 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
2881 goto out_unlock_iolock;
2882 ASSERT(nimap == 0 || nimap == 1);
2883 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2886 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2887 block = imap.br_startblock;
2888 mod = do_div(block, mp->m_sb.sb_rextsize);
2890 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2893 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
2894 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
2896 goto out_unlock_iolock;
2897 ASSERT(nimap == 0 || nimap == 1);
2898 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2899 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2901 if (mod && (mod != mp->m_sb.sb_rextsize))
2902 endoffset_fsb -= mod;
2905 if ((done = (endoffset_fsb <= startoffset_fsb)))
2907 * One contiguous piece to clear
2909 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2912 * Some full blocks, possibly two pieces to clear
2914 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2915 error = xfs_zero_remaining_bytes(ip, offset,
2916 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2918 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2919 error = xfs_zero_remaining_bytes(ip,
2920 XFS_FSB_TO_B(mp, endoffset_fsb),
2925 * free file space until done or until there is an error
2927 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2928 while (!error && !done) {
2931 * allocate and setup the transaction. Allow this
2932 * transaction to dip into the reserve blocks to ensure
2933 * the freeing of the space succeeds at ENOSPC.
2935 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2936 tp->t_flags |= XFS_TRANS_RESERVE;
2937 error = xfs_trans_reserve(tp,
2939 XFS_WRITE_LOG_RES(mp),
2941 XFS_TRANS_PERM_LOG_RES,
2942 XFS_WRITE_LOG_COUNT);
2945 * check for running out of space
2949 * Free the transaction structure.
2951 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2952 xfs_trans_cancel(tp, 0);
2955 xfs_ilock(ip, XFS_ILOCK_EXCL);
2956 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
2957 ip->i_udquot, ip->i_gdquot, resblks, 0,
2958 XFS_QMOPT_RES_REGBLKS);
2962 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2963 xfs_trans_ihold(tp, ip);
2966 * issue the bunmapi() call to free the blocks
2968 xfs_bmap_init(&free_list, &firstfsb);
2969 error = xfs_bunmapi(tp, ip, startoffset_fsb,
2970 endoffset_fsb - startoffset_fsb,
2971 0, 2, &firstfsb, &free_list, NULL, &done);
2977 * complete the transaction
2979 error = xfs_bmap_finish(&tp, &free_list, &committed);
2984 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2985 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2990 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2994 xfs_bmap_cancel(&free_list);
2996 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2997 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3003 * xfs_change_file_space()
3004 * This routine allocates or frees disk space for the given file.
3005 * The user specified parameters are checked for alignment and size
3014 xfs_change_file_space(
3021 xfs_mount_t *mp = ip->i_mount;
3026 xfs_off_t startoffset;
3031 xfs_itrace_entry(ip);
3033 if (!S_ISREG(ip->i_d.di_mode))
3034 return XFS_ERROR(EINVAL);
3036 switch (bf->l_whence) {
3037 case 0: /*SEEK_SET*/
3039 case 1: /*SEEK_CUR*/
3040 bf->l_start += offset;
3042 case 2: /*SEEK_END*/
3043 bf->l_start += ip->i_size;
3046 return XFS_ERROR(EINVAL);
3049 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3051 if ( (bf->l_start < 0)
3052 || (bf->l_start > XFS_MAXIOFFSET(mp))
3053 || (bf->l_start + llen < 0)
3054 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3055 return XFS_ERROR(EINVAL);
3059 startoffset = bf->l_start;
3063 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3065 * These calls do NOT zero the data space allocated to the file,
3066 * nor do they change the file size.
3068 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3070 * These calls cause the new file data to be zeroed and the file
3071 * size to be changed.
3073 setprealloc = clrprealloc = 0;
3076 case XFS_IOC_RESVSP:
3077 case XFS_IOC_RESVSP64:
3078 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3085 case XFS_IOC_UNRESVSP:
3086 case XFS_IOC_UNRESVSP64:
3087 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3092 case XFS_IOC_ALLOCSP:
3093 case XFS_IOC_ALLOCSP64:
3094 case XFS_IOC_FREESP:
3095 case XFS_IOC_FREESP64:
3096 if (startoffset > fsize) {
3097 error = xfs_alloc_file_space(ip, fsize,
3098 startoffset - fsize, 0, attr_flags);
3103 iattr.ia_valid = ATTR_SIZE;
3104 iattr.ia_size = startoffset;
3106 error = xfs_setattr(ip, &iattr, attr_flags);
3116 return XFS_ERROR(EINVAL);
3120 * update the inode timestamp, mode, and prealloc flag bits
3122 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3124 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3127 xfs_trans_cancel(tp, 0);
3131 xfs_ilock(ip, XFS_ILOCK_EXCL);
3133 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3134 xfs_trans_ihold(tp, ip);
3136 if ((attr_flags & XFS_ATTR_DMI) == 0) {
3137 ip->i_d.di_mode &= ~S_ISUID;
3140 * Note that we don't have to worry about mandatory
3141 * file locking being disabled here because we only
3142 * clear the S_ISGID bit if the Group execute bit is
3143 * on, but if it was on then mandatory locking wouldn't
3144 * have been enabled.
3146 if (ip->i_d.di_mode & S_IXGRP)
3147 ip->i_d.di_mode &= ~S_ISGID;
3149 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3152 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3153 else if (clrprealloc)
3154 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3156 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3157 xfs_trans_set_sync(tp);
3159 error = xfs_trans_commit(tp, 0);
3161 xfs_iunlock(ip, XFS_ILOCK_EXCL);