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 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
63 return XFS_ERROR(EIO);
66 * If it's a directory with any blocks, read-ahead block 0
67 * as we're almost certain to have the next operation be a read there.
69 if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
70 mode = xfs_ilock_map_shared(ip);
71 if (ip->i_d.di_nextents > 0)
72 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
73 xfs_iunlock(ip, mode);
84 xfs_mount_t *mp = ip->i_mount;
85 struct inode *inode = VFS_I(ip);
86 int mask = iattr->ia_valid;
94 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
100 if (mp->m_flags & XFS_MOUNT_RDONLY)
101 return XFS_ERROR(EROFS);
103 if (XFS_FORCED_SHUTDOWN(mp))
104 return XFS_ERROR(EIO);
106 olddquot1 = olddquot2 = NULL;
110 * If disk quotas is on, we make sure that the dquots do exist on disk,
111 * before we start any other transactions. Trying to do this later
112 * is messy. We don't care to take a readlock to look at the ids
113 * in inode here, because we can't hold it across the trans_reserve.
114 * If the IDs do change before we take the ilock, we're covered
115 * because the i_*dquot fields will get updated anyway.
117 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
120 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
122 qflags |= XFS_QMOPT_UQUOTA;
124 uid = ip->i_d.di_uid;
126 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
128 qflags |= XFS_QMOPT_GQUOTA;
130 gid = ip->i_d.di_gid;
134 * We take a reference when we initialize udqp and gdqp,
135 * so it is important that we never blindly double trip on
136 * the same variable. See xfs_create() for an example.
138 ASSERT(udqp == NULL);
139 ASSERT(gdqp == NULL);
140 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, ip->i_d.di_projid,
141 qflags, &udqp, &gdqp);
147 * For the other attributes, we acquire the inode lock and
148 * first do an error checking pass.
151 lock_flags = XFS_ILOCK_EXCL;
152 if (flags & XFS_ATTR_NOLOCK)
154 if (!(mask & ATTR_SIZE)) {
155 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
156 (mp->m_flags & XFS_MOUNT_WSYNC)) {
157 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
159 if ((code = xfs_trans_reserve(tp, 0,
160 XFS_ICHANGE_LOG_RES(mp), 0,
167 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
168 !(flags & XFS_ATTR_DMI)) {
169 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
170 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
171 iattr->ia_size, 0, dmflags, NULL);
178 lock_flags |= XFS_IOLOCK_EXCL;
181 xfs_ilock(ip, lock_flags);
183 /* boolean: are we the file owner? */
184 file_owner = (current_fsuid() == ip->i_d.di_uid);
187 * Change various properties of a file.
188 * Only the owner or users with CAP_FOWNER
189 * capability may do these things.
191 if (mask & (ATTR_MODE|ATTR_UID|ATTR_GID)) {
193 * CAP_FOWNER overrides the following restrictions:
195 * The user ID of the calling process must be equal
196 * to the file owner ID, except in cases where the
197 * CAP_FSETID capability is applicable.
199 if (!file_owner && !capable(CAP_FOWNER)) {
200 code = XFS_ERROR(EPERM);
205 * CAP_FSETID overrides the following restrictions:
207 * The effective user ID of the calling process shall match
208 * the file owner when setting the set-user-ID and
209 * set-group-ID bits on that file.
211 * The effective group ID or one of the supplementary group
212 * IDs of the calling process shall match the group owner of
213 * the file when setting the set-group-ID bit on that file
215 if (mask & ATTR_MODE) {
218 if ((iattr->ia_mode & S_ISUID) && !file_owner)
220 if ((iattr->ia_mode & S_ISGID) &&
221 !in_group_p((gid_t)ip->i_d.di_gid))
224 /* Linux allows this, Irix doesn't. */
225 if ((iattr->ia_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode))
228 if (m && !capable(CAP_FSETID))
229 iattr->ia_mode &= ~m;
234 * Change file ownership. Must be the owner or privileged.
236 if (mask & (ATTR_UID|ATTR_GID)) {
238 * These IDs could have changed since we last looked at them.
239 * But, we're assured that if the ownership did change
240 * while we didn't have the inode locked, inode's dquot(s)
241 * would have changed also.
243 iuid = ip->i_d.di_uid;
244 igid = ip->i_d.di_gid;
245 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
246 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
249 * CAP_CHOWN overrides the following restrictions:
251 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
252 * shall override the restriction that a process cannot
253 * change the user ID of a file it owns and the restriction
254 * that the group ID supplied to the chown() function
255 * shall be equal to either the group ID or one of the
256 * supplementary group IDs of the calling process.
259 (igid != gid && !in_group_p((gid_t)gid))) &&
260 !capable(CAP_CHOWN)) {
261 code = XFS_ERROR(EPERM);
265 * Do a quota reservation only if uid/gid is actually
268 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
269 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
271 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
272 capable(CAP_FOWNER) ?
273 XFS_QMOPT_FORCE_RES : 0);
274 if (code) /* out of quota */
280 * Truncate file. Must have write permission and not be a directory.
282 if (mask & ATTR_SIZE) {
283 /* Short circuit the truncate case for zero length files */
284 if (iattr->ia_size == 0 &&
285 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
286 xfs_iunlock(ip, XFS_ILOCK_EXCL);
287 lock_flags &= ~XFS_ILOCK_EXCL;
288 if (mask & ATTR_CTIME)
289 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
294 if (S_ISDIR(ip->i_d.di_mode)) {
295 code = XFS_ERROR(EISDIR);
297 } else if (!S_ISREG(ip->i_d.di_mode)) {
298 code = XFS_ERROR(EINVAL);
302 * Make sure that the dquots are attached to the inode.
304 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
309 * Change file access or modified times.
311 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
313 if ((mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)) &&
314 !capable(CAP_FOWNER)) {
315 code = XFS_ERROR(EPERM);
322 * Now we can make the changes. Before we join the inode
323 * to the transaction, if ATTR_SIZE is set then take care of
324 * the part of the truncation that must be done without the
325 * inode lock. This needs to be done before joining the inode
326 * to the transaction, because the inode cannot be unlocked
327 * once it is a part of the transaction.
329 if (mask & ATTR_SIZE) {
331 if (iattr->ia_size > ip->i_size) {
333 * Do the first part of growing a file: zero any data
334 * in the last block that is beyond the old EOF. We
335 * need to do this before the inode is joined to the
336 * transaction to modify the i_size.
338 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
340 xfs_iunlock(ip, XFS_ILOCK_EXCL);
343 * We are going to log the inode size change in this
344 * transaction so any previous writes that are beyond the on
345 * disk EOF and the new EOF that have not been written out need
346 * to be written here. If we do not write the data out, we
347 * expose ourselves to the null files problem.
349 * Only flush from the on disk size to the smaller of the in
350 * memory file size or the new size as that's the range we
351 * really care about here and prevents waiting for other data
352 * not within the range we care about here.
355 ip->i_size != ip->i_d.di_size &&
356 iattr->ia_size > ip->i_d.di_size) {
357 code = xfs_flush_pages(ip,
358 ip->i_d.di_size, iattr->ia_size,
359 XFS_B_ASYNC, FI_NONE);
362 /* wait for all I/O to complete */
366 code = xfs_itruncate_data(ip, iattr->ia_size);
369 lock_flags &= ~XFS_ILOCK_EXCL;
370 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
373 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
374 if ((code = xfs_trans_reserve(tp, 0,
375 XFS_ITRUNCATE_LOG_RES(mp), 0,
376 XFS_TRANS_PERM_LOG_RES,
377 XFS_ITRUNCATE_LOG_COUNT))) {
378 xfs_trans_cancel(tp, 0);
380 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
383 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
384 xfs_ilock(ip, XFS_ILOCK_EXCL);
388 xfs_trans_ijoin(tp, ip, lock_flags);
389 xfs_trans_ihold(tp, ip);
393 * Truncate file. Must have write permission and not be a directory.
395 if (mask & ATTR_SIZE) {
397 * Only change the c/mtime if we are changing the size
398 * or we are explicitly asked to change it. This handles
399 * the semantic difference between truncate() and ftruncate()
400 * as implemented in the VFS.
402 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
403 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
405 if (iattr->ia_size > ip->i_size) {
406 ip->i_d.di_size = iattr->ia_size;
407 ip->i_size = iattr->ia_size;
408 if (!(flags & XFS_ATTR_DMI))
409 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
410 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
411 } else if (iattr->ia_size <= ip->i_size ||
412 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
414 * signal a sync transaction unless
415 * we're truncating an already unlinked
416 * file on a wsync filesystem
418 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
420 ((ip->i_d.di_nlink != 0 ||
421 !(mp->m_flags & XFS_MOUNT_WSYNC))
426 * Truncated "down", so we're removing references
427 * to old data here - if we now delay flushing for
428 * a long time, we expose ourselves unduly to the
429 * notorious NULL files problem. So, we mark this
430 * vnode and flush it when the file is closed, and
431 * do not wait the usual (long) time for writeout.
433 xfs_iflags_set(ip, XFS_ITRUNCATED);
438 * Change file access modes.
440 if (mask & ATTR_MODE) {
441 ip->i_d.di_mode &= S_IFMT;
442 ip->i_d.di_mode |= iattr->ia_mode & ~S_IFMT;
444 inode->i_mode &= S_IFMT;
445 inode->i_mode |= iattr->ia_mode & ~S_IFMT;
447 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
448 timeflags |= XFS_ICHGTIME_CHG;
452 * Change file ownership. Must be the owner or privileged.
454 if (mask & (ATTR_UID|ATTR_GID)) {
456 * CAP_FSETID overrides the following restrictions:
458 * The set-user-ID and set-group-ID bits of a file will be
459 * cleared upon successful return from chown()
461 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
462 !capable(CAP_FSETID)) {
463 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
467 * Change the ownerships and register quota modifications
468 * in the transaction.
471 if (XFS_IS_UQUOTA_ON(mp)) {
472 ASSERT(mask & ATTR_UID);
474 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
475 &ip->i_udquot, udqp);
477 ip->i_d.di_uid = uid;
481 if (XFS_IS_GQUOTA_ON(mp)) {
482 ASSERT(!XFS_IS_PQUOTA_ON(mp));
483 ASSERT(mask & ATTR_GID);
485 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
486 &ip->i_gdquot, gdqp);
488 ip->i_d.di_gid = gid;
492 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
493 timeflags |= XFS_ICHGTIME_CHG;
498 * Change file access or modified times.
500 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
501 if (mask & ATTR_ATIME) {
502 inode->i_atime = iattr->ia_atime;
503 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
504 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
505 ip->i_update_core = 1;
507 if (mask & ATTR_MTIME) {
508 inode->i_mtime = iattr->ia_mtime;
509 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
510 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
511 timeflags &= ~XFS_ICHGTIME_MOD;
512 timeflags |= XFS_ICHGTIME_CHG;
514 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
515 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
519 * Change file inode change time only if ATTR_CTIME set
520 * AND we have been called by a DMI function.
523 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
524 inode->i_ctime = iattr->ia_ctime;
525 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
526 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
527 ip->i_update_core = 1;
528 timeflags &= ~XFS_ICHGTIME_CHG;
532 * Send out timestamp changes that need to be set to the
533 * current time. Not done when called by a DMI function.
535 if (timeflags && !(flags & XFS_ATTR_DMI))
536 xfs_ichgtime(ip, timeflags);
538 XFS_STATS_INC(xs_ig_attrchg);
541 * If this is a synchronous mount, make sure that the
542 * transaction goes to disk before returning to the user.
543 * This is slightly sub-optimal in that truncates require
544 * two sync transactions instead of one for wsync filesystems.
545 * One for the truncate and one for the timestamps since we
546 * don't want to change the timestamps unless we're sure the
547 * truncate worked. Truncates are less than 1% of the laddis
548 * mix so this probably isn't worth the trouble to optimize.
552 if (mp->m_flags & XFS_MOUNT_WSYNC)
553 xfs_trans_set_sync(tp);
555 code = xfs_trans_commit(tp, commit_flags);
558 xfs_iunlock(ip, lock_flags);
561 * Release any dquot(s) the inode had kept before chown.
563 XFS_QM_DQRELE(mp, olddquot1);
564 XFS_QM_DQRELE(mp, olddquot2);
565 XFS_QM_DQRELE(mp, udqp);
566 XFS_QM_DQRELE(mp, gdqp);
572 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
573 !(flags & XFS_ATTR_DMI)) {
574 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
575 NULL, DM_RIGHT_NULL, NULL, NULL,
576 0, 0, AT_DELAY_FLAG(flags));
581 commit_flags |= XFS_TRANS_ABORT;
584 XFS_QM_DQRELE(mp, udqp);
585 XFS_QM_DQRELE(mp, gdqp);
587 xfs_trans_cancel(tp, commit_flags);
589 if (lock_flags != 0) {
590 xfs_iunlock(ip, lock_flags);
596 * The maximum pathlen is 1024 bytes. Since the minimum file system
597 * blocksize is 512 bytes, we can get a max of 2 extents back from
600 #define SYMLINK_MAPS 2
607 xfs_mount_t *mp = ip->i_mount;
608 int pathlen = ip->i_d.di_size;
609 int nmaps = SYMLINK_MAPS;
610 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
617 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
618 mval, &nmaps, NULL, NULL);
622 for (n = 0; n < nmaps; n++) {
623 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
624 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
626 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
627 error = XFS_BUF_GETERROR(bp);
629 xfs_ioerror_alert("xfs_readlink",
630 ip->i_mount, bp, XFS_BUF_ADDR(bp));
634 if (pathlen < byte_cnt)
638 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
642 link[ip->i_d.di_size] = '\0';
654 xfs_mount_t *mp = ip->i_mount;
658 xfs_itrace_entry(ip);
660 if (XFS_FORCED_SHUTDOWN(mp))
661 return XFS_ERROR(EIO);
663 xfs_ilock(ip, XFS_ILOCK_SHARED);
665 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
666 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
668 pathlen = ip->i_d.di_size;
672 if (ip->i_df.if_flags & XFS_IFINLINE) {
673 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
674 link[pathlen] = '\0';
676 error = xfs_readlink_bmap(ip, link);
680 xfs_iunlock(ip, XFS_ILOCK_SHARED);
687 * This is called to sync the inode and its data out to disk. We need to hold
688 * the I/O lock while flushing the data, and the inode lock while flushing the
689 * inode. The inode lock CANNOT be held while flushing the data, so acquire
690 * after we're done with that.
698 int log_flushed = 0, changed = 1;
700 xfs_itrace_entry(ip);
702 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
703 return XFS_ERROR(EIO);
705 /* capture size updates in I/O completion before writing the inode. */
706 error = filemap_fdatawait(VFS_I(ip)->i_mapping);
708 return XFS_ERROR(error);
711 * We always need to make sure that the required inode state is safe on
712 * disk. The vnode might be clean but we still might need to force the
713 * log because of committed transactions that haven't hit the disk yet.
714 * Likewise, there could be unflushed non-transactional changes to the
715 * inode core that have to go to disk and this requires us to issue
716 * a synchronous transaction to capture these changes correctly.
718 * This code relies on the assumption that if the update_* fields
719 * of the inode are clear and the inode is unpinned then it is clean
720 * and no action is required.
722 xfs_ilock(ip, XFS_ILOCK_SHARED);
724 if (!(ip->i_update_size || ip->i_update_core)) {
726 * Timestamps/size haven't changed since last inode flush or
727 * inode transaction commit. That means either nothing got
728 * written or a transaction committed which caught the updates.
729 * If the latter happened and the transaction hasn't hit the
730 * disk yet, the inode will be still be pinned. If it is,
734 xfs_iunlock(ip, XFS_ILOCK_SHARED);
736 if (xfs_ipincount(ip)) {
737 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
738 XFS_LOG_FORCE | XFS_LOG_SYNC,
742 * If the inode is not pinned and nothing has changed
743 * we don't need to flush the cache.
749 * Kick off a transaction to log the inode core to get the
750 * updates. The sync transaction will also force the log.
752 xfs_iunlock(ip, XFS_ILOCK_SHARED);
753 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
754 error = xfs_trans_reserve(tp, 0,
755 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
757 xfs_trans_cancel(tp, 0);
760 xfs_ilock(ip, XFS_ILOCK_EXCL);
763 * Note - it's possible that we might have pushed ourselves out
764 * of the way during trans_reserve which would flush the inode.
765 * But there's no guarantee that the inode buffer has actually
766 * gone out yet (it's delwri). Plus the buffer could be pinned
767 * anyway if it's part of an inode in another recent
768 * transaction. So we play it safe and fire off the
769 * transaction anyway.
771 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
772 xfs_trans_ihold(tp, ip);
773 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
774 xfs_trans_set_sync(tp);
775 error = _xfs_trans_commit(tp, 0, &log_flushed);
777 xfs_iunlock(ip, XFS_ILOCK_EXCL);
780 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
782 * If the log write didn't issue an ordered tag we need
783 * to flush the disk cache for the data device now.
786 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
789 * If this inode is on the RT dev we need to flush that
792 if (XFS_IS_REALTIME_INODE(ip))
793 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
800 * This is called by xfs_inactive to free any blocks beyond eof
801 * when the link count isn't zero and by xfs_dm_punch_hole() when
802 * punching a hole to EOF.
812 xfs_fileoff_t end_fsb;
813 xfs_fileoff_t last_fsb;
814 xfs_filblks_t map_len;
816 xfs_bmbt_irec_t imap;
817 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
820 * Figure out if there are any blocks beyond the end
821 * of the file. If not, then there is nothing to do.
823 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
824 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
825 map_len = last_fsb - end_fsb;
830 xfs_ilock(ip, XFS_ILOCK_SHARED);
831 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
832 NULL, 0, &imap, &nimaps, NULL, NULL);
833 xfs_iunlock(ip, XFS_ILOCK_SHARED);
835 if (!error && (nimaps != 0) &&
836 (imap.br_startblock != HOLESTARTBLOCK ||
837 ip->i_delayed_blks)) {
839 * Attach the dquots to the inode up front.
841 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
845 * There are blocks after the end of file.
846 * Free them up now by truncating the file to
849 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
852 * Do the xfs_itruncate_start() call before
853 * reserving any log space because
854 * itruncate_start will call into the buffer
856 * do that within a transaction.
859 xfs_ilock(ip, XFS_IOLOCK_EXCL);
860 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
863 xfs_trans_cancel(tp, 0);
865 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
869 error = xfs_trans_reserve(tp, 0,
870 XFS_ITRUNCATE_LOG_RES(mp),
871 0, XFS_TRANS_PERM_LOG_RES,
872 XFS_ITRUNCATE_LOG_COUNT);
874 ASSERT(XFS_FORCED_SHUTDOWN(mp));
875 xfs_trans_cancel(tp, 0);
876 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
880 xfs_ilock(ip, XFS_ILOCK_EXCL);
881 xfs_trans_ijoin(tp, ip,
884 xfs_trans_ihold(tp, ip);
886 error = xfs_itruncate_finish(&tp, ip,
891 * If we get an error at this point we
892 * simply don't bother truncating the file.
896 (XFS_TRANS_RELEASE_LOG_RES |
899 error = xfs_trans_commit(tp,
900 XFS_TRANS_RELEASE_LOG_RES);
902 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
909 * Free a symlink that has blocks associated with it.
912 xfs_inactive_symlink_rmt(
920 xfs_fsblock_t first_block;
921 xfs_bmap_free_t free_list;
924 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
932 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
934 * We're freeing a symlink that has some
935 * blocks allocated to it. Free the
936 * blocks here. We know that we've got
937 * either 1 or 2 extents and that we can
938 * free them all in one bunmapi call.
940 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
941 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
942 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
943 ASSERT(XFS_FORCED_SHUTDOWN(mp));
944 xfs_trans_cancel(tp, 0);
949 * Lock the inode, fix the size, and join it to the transaction.
950 * Hold it so in the normal path, we still have it locked for
951 * the second transaction. In the error paths we need it
952 * held so the cancel won't rele it, see below.
954 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
955 size = (int)ip->i_d.di_size;
957 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
958 xfs_trans_ihold(tp, ip);
959 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
961 * Find the block(s) so we can inval and unmap them.
964 XFS_BMAP_INIT(&free_list, &first_block);
965 nmaps = ARRAY_SIZE(mval);
966 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
967 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
971 * Invalidate the block(s).
973 for (i = 0; i < nmaps; i++) {
974 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
975 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
976 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
977 xfs_trans_binval(tp, bp);
980 * Unmap the dead block(s) to the free_list.
982 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
983 &first_block, &free_list, NULL, &done)))
987 * Commit the first transaction. This logs the EFI and the inode.
989 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
992 * The transaction must have been committed, since there were
993 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
994 * The new tp has the extent freeing and EFDs.
998 * The first xact was committed, so add the inode to the new one.
999 * Mark it dirty so it will be logged and moved forward in the log as
1000 * part of every commit.
1002 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1003 xfs_trans_ihold(tp, ip);
1004 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1006 * Get a new, empty transaction to return to our caller.
1008 ntp = xfs_trans_dup(tp);
1010 * Commit the transaction containing extent freeing and EFDs.
1011 * If we get an error on the commit here or on the reserve below,
1012 * we need to unlock the inode since the new transaction doesn't
1013 * have the inode attached.
1015 error = xfs_trans_commit(tp, 0);
1018 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1022 * transaction commit worked ok so we can drop the extra ticket
1023 * reference that we gained in xfs_trans_dup()
1025 xfs_log_ticket_put(tp->t_ticket);
1028 * Remove the memory for extent descriptions (just bookkeeping).
1030 if (ip->i_df.if_bytes)
1031 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1032 ASSERT(ip->i_df.if_bytes == 0);
1034 * Put an itruncate log reservation in the new transaction
1037 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1038 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1039 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1043 * Return with the inode locked but not joined to the transaction.
1049 xfs_bmap_cancel(&free_list);
1052 * Have to come here with the inode locked and either
1053 * (held and in the transaction) or (not in the transaction).
1054 * If the inode isn't held then cancel would iput it, but
1055 * that's wrong since this is inactive and the vnode ref
1056 * count is 0 already.
1057 * Cancel won't do anything to the inode if held, but it still
1058 * needs to be locked until the cancel is done, if it was
1059 * joined to the transaction.
1061 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1062 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1069 xfs_inactive_symlink_local(
1075 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1077 * We're freeing a symlink which fit into
1078 * the inode. Just free the memory used
1079 * to hold the old symlink.
1081 error = xfs_trans_reserve(*tpp, 0,
1082 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1083 0, XFS_TRANS_PERM_LOG_RES,
1084 XFS_ITRUNCATE_LOG_COUNT);
1087 xfs_trans_cancel(*tpp, 0);
1091 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1094 * Zero length symlinks _can_ exist.
1096 if (ip->i_df.if_bytes > 0) {
1097 xfs_idata_realloc(ip,
1098 -(ip->i_df.if_bytes),
1100 ASSERT(ip->i_df.if_bytes == 0);
1114 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1117 ASSERT(ip->i_d.di_forkoff != 0);
1118 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1119 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1123 error = xfs_attr_inactive(ip);
1127 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1128 error = xfs_trans_reserve(tp, 0,
1129 XFS_IFREE_LOG_RES(mp),
1130 0, XFS_TRANS_PERM_LOG_RES,
1131 XFS_INACTIVE_LOG_COUNT);
1135 xfs_ilock(ip, XFS_ILOCK_EXCL);
1136 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1137 xfs_trans_ihold(tp, ip);
1138 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1140 ASSERT(ip->i_d.di_anextents == 0);
1146 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1147 xfs_trans_cancel(tp, 0);
1150 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1158 xfs_mount_t *mp = ip->i_mount;
1161 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1164 /* If this is a read-only mount, don't do this (would generate I/O) */
1165 if (mp->m_flags & XFS_MOUNT_RDONLY)
1168 if (!XFS_FORCED_SHUTDOWN(mp)) {
1172 * If we are using filestreams, and we have an unlinked
1173 * file that we are processing the last close on, then nothing
1174 * will be able to reopen and write to this file. Purge this
1175 * inode from the filestreams cache so that it doesn't delay
1176 * teardown of the inode.
1178 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1179 xfs_filestream_deassociate(ip);
1182 * If we previously truncated this file and removed old data
1183 * in the process, we want to initiate "early" writeout on
1184 * the last close. This is an attempt to combat the notorious
1185 * NULL files problem which is particularly noticable from a
1186 * truncate down, buffered (re-)write (delalloc), followed by
1187 * a crash. What we are effectively doing here is
1188 * significantly reducing the time window where we'd otherwise
1189 * be exposed to that problem.
1191 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1192 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
1193 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1196 if (ip->i_d.di_nlink != 0) {
1197 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1198 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1199 ip->i_delayed_blks > 0)) &&
1200 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1201 (!(ip->i_d.di_flags &
1202 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1203 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1215 * This is called when the vnode reference count for the vnode
1216 * goes to zero. If the file has been unlinked, then it must
1217 * now be truncated. Also, we clear all of the read-ahead state
1218 * kept for the inode here since the file is now closed.
1224 xfs_bmap_free_t free_list;
1225 xfs_fsblock_t first_block;
1232 xfs_itrace_entry(ip);
1235 * If the inode is already free, then there can be nothing
1238 if (ip->i_d.di_mode == 0 || VN_BAD(VFS_I(ip))) {
1239 ASSERT(ip->i_df.if_real_bytes == 0);
1240 ASSERT(ip->i_df.if_broot_bytes == 0);
1241 return VN_INACTIVE_CACHE;
1245 * Only do a truncate if it's a regular file with
1246 * some actual space in it. It's OK to look at the
1247 * inode's fields without the lock because we're the
1248 * only one with a reference to the inode.
1250 truncate = ((ip->i_d.di_nlink == 0) &&
1251 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1252 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1253 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1257 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1258 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1262 /* If this is a read-only mount, don't do this (would generate I/O) */
1263 if (mp->m_flags & XFS_MOUNT_RDONLY)
1266 if (ip->i_d.di_nlink != 0) {
1267 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1268 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1269 ip->i_delayed_blks > 0)) &&
1270 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1271 (!(ip->i_d.di_flags &
1272 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1273 (ip->i_delayed_blks != 0)))) {
1274 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1276 return VN_INACTIVE_CACHE;
1281 ASSERT(ip->i_d.di_nlink == 0);
1283 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1284 return VN_INACTIVE_CACHE;
1286 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1289 * Do the xfs_itruncate_start() call before
1290 * reserving any log space because itruncate_start
1291 * will call into the buffer cache and we can't
1292 * do that within a transaction.
1294 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1296 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1298 xfs_trans_cancel(tp, 0);
1299 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1300 return VN_INACTIVE_CACHE;
1303 error = xfs_trans_reserve(tp, 0,
1304 XFS_ITRUNCATE_LOG_RES(mp),
1305 0, XFS_TRANS_PERM_LOG_RES,
1306 XFS_ITRUNCATE_LOG_COUNT);
1308 /* Don't call itruncate_cleanup */
1309 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1310 xfs_trans_cancel(tp, 0);
1311 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1312 return VN_INACTIVE_CACHE;
1315 xfs_ilock(ip, XFS_ILOCK_EXCL);
1316 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1317 xfs_trans_ihold(tp, ip);
1320 * normally, we have to run xfs_itruncate_finish sync.
1321 * But if filesystem is wsync and we're in the inactive
1322 * path, then we know that nlink == 0, and that the
1323 * xaction that made nlink == 0 is permanently committed
1324 * since xfs_remove runs as a synchronous transaction.
1326 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1327 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1330 xfs_trans_cancel(tp,
1331 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1332 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1333 return VN_INACTIVE_CACHE;
1335 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1338 * If we get an error while cleaning up a
1339 * symlink we bail out.
1341 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1342 xfs_inactive_symlink_rmt(ip, &tp) :
1343 xfs_inactive_symlink_local(ip, &tp);
1347 return VN_INACTIVE_CACHE;
1350 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1351 xfs_trans_ihold(tp, ip);
1353 error = xfs_trans_reserve(tp, 0,
1354 XFS_IFREE_LOG_RES(mp),
1355 0, XFS_TRANS_PERM_LOG_RES,
1356 XFS_INACTIVE_LOG_COUNT);
1358 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1359 xfs_trans_cancel(tp, 0);
1360 return VN_INACTIVE_CACHE;
1363 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1364 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1365 xfs_trans_ihold(tp, ip);
1369 * If there are attributes associated with the file
1370 * then blow them away now. The code calls a routine
1371 * that recursively deconstructs the attribute fork.
1372 * We need to just commit the current transaction
1373 * because we can't use it for xfs_attr_inactive().
1375 if (ip->i_d.di_anextents > 0) {
1376 error = xfs_inactive_attrs(ip, &tp);
1378 * If we got an error, the transaction is already
1379 * cancelled, and the inode is unlocked. Just get out.
1382 return VN_INACTIVE_CACHE;
1383 } else if (ip->i_afp) {
1384 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1390 XFS_BMAP_INIT(&free_list, &first_block);
1391 error = xfs_ifree(tp, ip, &free_list);
1394 * If we fail to free the inode, shut down. The cancel
1395 * might do that, we need to make sure. Otherwise the
1396 * inode might be lost for a long time or forever.
1398 if (!XFS_FORCED_SHUTDOWN(mp)) {
1400 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1401 error, mp->m_fsname);
1402 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1404 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1407 * Credit the quota account(s). The inode is gone.
1409 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1412 * Just ignore errors at this point. There is nothing we can
1413 * do except to try to keep going. Make sure it's not a silent
1416 error = xfs_bmap_finish(&tp, &free_list, &committed);
1418 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1419 "xfs_bmap_finish() returned error %d", error);
1420 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1422 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1423 "xfs_trans_commit() returned error %d", error);
1426 * Release the dquots held by inode, if any.
1428 XFS_QM_DQDETACH(mp, ip);
1430 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1433 return VN_INACTIVE_CACHE;
1437 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1438 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1439 * ci_name->name will point to a the actual name (caller must free) or
1440 * will be set to NULL if an exact match is found.
1445 struct xfs_name *name,
1447 struct xfs_name *ci_name)
1453 xfs_itrace_entry(dp);
1455 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1456 return XFS_ERROR(EIO);
1458 lock_mode = xfs_ilock_map_shared(dp);
1459 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1460 xfs_iunlock_map_shared(dp, lock_mode);
1465 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1469 xfs_itrace_ref(*ipp);
1474 kmem_free(ci_name->name);
1483 struct xfs_name *name,
1489 xfs_mount_t *mp = dp->i_mount;
1493 xfs_bmap_free_t free_list;
1494 xfs_fsblock_t first_block;
1495 boolean_t unlock_dp_on_error = B_FALSE;
1496 int dm_event_sent = 0;
1500 struct xfs_dquot *udqp, *gdqp;
1504 xfs_itrace_entry(dp);
1506 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1507 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1508 dp, DM_RIGHT_NULL, NULL,
1509 DM_RIGHT_NULL, name->name, NULL,
1517 if (XFS_FORCED_SHUTDOWN(mp))
1518 return XFS_ERROR(EIO);
1520 /* Return through std_return after this point. */
1523 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1524 prid = dp->i_d.di_projid;
1526 prid = (xfs_prid_t)dfltprid;
1529 * Make sure that we have allocated dquot(s) on disk.
1531 error = XFS_QM_DQVOPALLOC(mp, dp,
1532 current_fsuid(), current_fsgid(), prid,
1533 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1539 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1540 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1541 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1543 * Initially assume that the file does not exist and
1544 * reserve the resources for that case. If that is not
1545 * the case we'll drop the one we have and get a more
1546 * appropriate transaction later.
1548 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1549 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1550 if (error == ENOSPC) {
1552 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1553 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1560 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1561 unlock_dp_on_error = B_TRUE;
1563 XFS_BMAP_INIT(&free_list, &first_block);
1568 * Reserve disk quota and the inode.
1570 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1574 error = xfs_dir_canenter(tp, dp, name, resblks);
1577 error = xfs_dir_ialloc(&tp, dp, mode, 1,
1578 rdev, credp, prid, resblks > 0,
1581 if (error == ENOSPC)
1588 * At this point, we've gotten a newly allocated inode.
1589 * It is locked (and joined to the transaction).
1592 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1595 * Now we join the directory inode to the transaction. We do not do it
1596 * earlier because xfs_dir_ialloc might commit the previous transaction
1597 * (and release all the locks). An error from here on will result in
1598 * the transaction cancel unlocking dp so don't do it explicitly in the
1602 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1603 unlock_dp_on_error = B_FALSE;
1605 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1606 &first_block, &free_list, resblks ?
1607 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1609 ASSERT(error != ENOSPC);
1612 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1613 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1616 * If this is a synchronous mount, make sure that the
1617 * create transaction goes to disk before returning to
1620 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1621 xfs_trans_set_sync(tp);
1627 * Attach the dquot(s) to the inodes and modify them incore.
1628 * These ids of the inode couldn't have changed since the new
1629 * inode has been locked ever since it was created.
1631 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1634 * xfs_trans_commit normally decrements the vnode ref count
1635 * when it unlocks the inode. Since we want to return the
1636 * vnode to the caller, we bump the vnode ref count now.
1640 error = xfs_bmap_finish(&tp, &free_list, &committed);
1642 xfs_bmap_cancel(&free_list);
1646 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1653 XFS_QM_DQRELE(mp, udqp);
1654 XFS_QM_DQRELE(mp, gdqp);
1658 /* Fallthrough to std_return with error = 0 */
1661 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
1662 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1663 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
1666 DM_RIGHT_NULL, name->name, NULL,
1672 cancel_flags |= XFS_TRANS_ABORT;
1677 xfs_trans_cancel(tp, cancel_flags);
1679 XFS_QM_DQRELE(mp, udqp);
1680 XFS_QM_DQRELE(mp, gdqp);
1682 if (unlock_dp_on_error)
1683 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1689 * Wait until after the current transaction is aborted to
1690 * release the inode. This prevents recursive transactions
1691 * and deadlocks from xfs_inactive.
1693 cancel_flags |= XFS_TRANS_ABORT;
1694 xfs_trans_cancel(tp, cancel_flags);
1697 XFS_QM_DQRELE(mp, udqp);
1698 XFS_QM_DQRELE(mp, gdqp);
1705 int xfs_small_retries;
1706 int xfs_middle_retries;
1707 int xfs_lots_retries;
1708 int xfs_lock_delays;
1712 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1716 xfs_lock_inumorder(int lock_mode, int subclass)
1718 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1719 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1720 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1721 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1727 * The following routine will lock n inodes in exclusive mode.
1728 * We assume the caller calls us with the inodes in i_ino order.
1730 * We need to detect deadlock where an inode that we lock
1731 * is in the AIL and we start waiting for another inode that is locked
1732 * by a thread in a long running transaction (such as truncate). This can
1733 * result in deadlock since the long running trans might need to wait
1734 * for the inode we just locked in order to push the tail and free space
1743 int attempts = 0, i, j, try_lock;
1746 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1752 for (; i < inodes; i++) {
1755 if (i && (ips[i] == ips[i-1])) /* Already locked */
1759 * If try_lock is not set yet, make sure all locked inodes
1760 * are not in the AIL.
1761 * If any are, set try_lock to be used later.
1765 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1766 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1767 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1774 * If any of the previous locks we have locked is in the AIL,
1775 * we must TRY to get the second and subsequent locks. If
1776 * we can't get any, we must release all we have
1781 /* try_lock must be 0 if i is 0. */
1783 * try_lock means we have an inode locked
1784 * that is in the AIL.
1787 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1791 * Unlock all previous guys and try again.
1792 * xfs_iunlock will try to push the tail
1793 * if the inode is in the AIL.
1796 for(j = i - 1; j >= 0; j--) {
1799 * Check to see if we've already
1800 * unlocked this one.
1801 * Not the first one going back,
1802 * and the inode ptr is the same.
1804 if ((j != (i - 1)) && ips[j] ==
1808 xfs_iunlock(ips[j], lock_mode);
1811 if ((attempts % 5) == 0) {
1812 delay(1); /* Don't just spin the CPU */
1822 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1828 if (attempts < 5) xfs_small_retries++;
1829 else if (attempts < 100) xfs_middle_retries++;
1830 else xfs_lots_retries++;
1838 * xfs_lock_two_inodes() can only be used to lock one type of lock
1839 * at a time - the iolock or the ilock, but not both at once. If
1840 * we lock both at once, lockdep will report false positives saying
1841 * we have violated locking orders.
1844 xfs_lock_two_inodes(
1853 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1854 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1855 ASSERT(ip0->i_ino != ip1->i_ino);
1857 if (ip0->i_ino > ip1->i_ino) {
1864 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1867 * If the first lock we have locked is in the AIL, we must TRY to get
1868 * the second lock. If we can't get it, we must release the first one
1871 lp = (xfs_log_item_t *)ip0->i_itemp;
1872 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1873 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1874 xfs_iunlock(ip0, lock_mode);
1875 if ((++attempts % 5) == 0)
1876 delay(1); /* Don't just spin the CPU */
1880 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1887 struct xfs_name *name,
1890 xfs_mount_t *mp = dp->i_mount;
1891 xfs_trans_t *tp = NULL;
1892 int is_dir = S_ISDIR(ip->i_d.di_mode);
1894 xfs_bmap_free_t free_list;
1895 xfs_fsblock_t first_block;
1902 xfs_itrace_entry(dp);
1903 xfs_itrace_entry(ip);
1905 if (XFS_FORCED_SHUTDOWN(mp))
1906 return XFS_ERROR(EIO);
1908 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
1909 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1910 NULL, DM_RIGHT_NULL, name->name, NULL,
1911 ip->i_d.di_mode, 0, 0);
1916 error = XFS_QM_DQATTACH(mp, dp, 0);
1920 error = XFS_QM_DQATTACH(mp, ip, 0);
1925 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1926 log_count = XFS_DEFAULT_LOG_COUNT;
1928 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1929 log_count = XFS_REMOVE_LOG_COUNT;
1931 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1934 * We try to get the real space reservation first,
1935 * allowing for directory btree deletion(s) implying
1936 * possible bmap insert(s). If we can't get the space
1937 * reservation then we use 0 instead, and avoid the bmap
1938 * btree insert(s) in the directory code by, if the bmap
1939 * insert tries to happen, instead trimming the LAST
1940 * block from the directory.
1942 resblks = XFS_REMOVE_SPACE_RES(mp);
1943 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1944 XFS_TRANS_PERM_LOG_RES, log_count);
1945 if (error == ENOSPC) {
1947 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1948 XFS_TRANS_PERM_LOG_RES, log_count);
1951 ASSERT(error != ENOSPC);
1953 goto out_trans_cancel;
1956 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1959 * At this point, we've gotten both the directory and the entry
1963 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1966 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1969 * If we're removing a directory perform some additional validation.
1972 ASSERT(ip->i_d.di_nlink >= 2);
1973 if (ip->i_d.di_nlink != 2) {
1974 error = XFS_ERROR(ENOTEMPTY);
1975 goto out_trans_cancel;
1977 if (!xfs_dir_isempty(ip)) {
1978 error = XFS_ERROR(ENOTEMPTY);
1979 goto out_trans_cancel;
1983 XFS_BMAP_INIT(&free_list, &first_block);
1984 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1985 &first_block, &free_list, resblks);
1987 ASSERT(error != ENOENT);
1988 goto out_bmap_cancel;
1990 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1993 * Bump the in memory generation count on the parent
1994 * directory so that other can know that it has changed.
1997 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2001 * Drop the link from ip's "..".
2003 error = xfs_droplink(tp, dp);
2005 goto out_bmap_cancel;
2008 * Drop the "." link from ip to self.
2010 error = xfs_droplink(tp, ip);
2012 goto out_bmap_cancel;
2015 * When removing a non-directory we need to log the parent
2016 * inode here for the i_gen update. For a directory this is
2017 * done implicitly by the xfs_droplink call for the ".." entry.
2019 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2023 * Drop the link from dp to ip.
2025 error = xfs_droplink(tp, ip);
2027 goto out_bmap_cancel;
2030 * Determine if this is the last link while
2031 * we are in the transaction.
2033 link_zero = (ip->i_d.di_nlink == 0);
2036 * If this is a synchronous mount, make sure that the
2037 * remove transaction goes to disk before returning to
2040 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2041 xfs_trans_set_sync(tp);
2043 error = xfs_bmap_finish(&tp, &free_list, &committed);
2045 goto out_bmap_cancel;
2047 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2052 * If we are using filestreams, kill the stream association.
2053 * If the file is still open it may get a new one but that
2054 * will get killed on last close in xfs_close() so we don't
2055 * have to worry about that.
2057 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2058 xfs_filestream_deassociate(ip);
2060 xfs_itrace_exit(ip);
2061 xfs_itrace_exit(dp);
2064 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2065 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2066 NULL, DM_RIGHT_NULL, name->name, NULL,
2067 ip->i_d.di_mode, error, 0);
2073 xfs_bmap_cancel(&free_list);
2074 cancel_flags |= XFS_TRANS_ABORT;
2076 xfs_trans_cancel(tp, cancel_flags);
2084 struct xfs_name *target_name)
2086 xfs_mount_t *mp = tdp->i_mount;
2089 xfs_bmap_free_t free_list;
2090 xfs_fsblock_t first_block;
2095 xfs_itrace_entry(tdp);
2096 xfs_itrace_entry(sip);
2098 ASSERT(!S_ISDIR(sip->i_d.di_mode));
2100 if (XFS_FORCED_SHUTDOWN(mp))
2101 return XFS_ERROR(EIO);
2103 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2104 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2107 target_name->name, NULL, 0, 0, 0);
2112 /* Return through std_return after this point. */
2114 error = XFS_QM_DQATTACH(mp, sip, 0);
2115 if (!error && sip != tdp)
2116 error = XFS_QM_DQATTACH(mp, tdp, 0);
2120 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2121 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2122 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
2123 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2124 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2125 if (error == ENOSPC) {
2127 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2128 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2135 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
2138 * Increment vnode ref counts since xfs_trans_commit &
2139 * xfs_trans_cancel will both unlock the inodes and
2140 * decrement the associated ref counts.
2144 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2145 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2148 * If the source has too many links, we can't make any more to it.
2150 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2151 error = XFS_ERROR(EMLINK);
2156 * If we are using project inheritance, we only allow hard link
2157 * creation in our tree when the project IDs are the same; else
2158 * the tree quota mechanism could be circumvented.
2160 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2161 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2162 error = XFS_ERROR(EXDEV);
2166 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2170 XFS_BMAP_INIT(&free_list, &first_block);
2172 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2173 &first_block, &free_list, resblks);
2176 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2178 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2180 error = xfs_bumplink(tp, sip);
2185 * If this is a synchronous mount, make sure that the
2186 * link transaction goes to disk before returning to
2189 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2190 xfs_trans_set_sync(tp);
2193 error = xfs_bmap_finish (&tp, &free_list, &committed);
2195 xfs_bmap_cancel(&free_list);
2199 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2203 /* Fall through to std_return with error = 0. */
2205 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2206 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2209 target_name->name, NULL, 0, error, 0);
2214 cancel_flags |= XFS_TRANS_ABORT;
2218 xfs_trans_cancel(tp, cancel_flags);
2226 struct xfs_name *dir_name,
2231 xfs_mount_t *mp = dp->i_mount;
2232 xfs_inode_t *cdp; /* inode of created dir */
2237 xfs_bmap_free_t free_list;
2238 xfs_fsblock_t first_block;
2239 boolean_t unlock_dp_on_error = B_FALSE;
2240 boolean_t created = B_FALSE;
2241 int dm_event_sent = 0;
2243 struct xfs_dquot *udqp, *gdqp;
2246 if (XFS_FORCED_SHUTDOWN(mp))
2247 return XFS_ERROR(EIO);
2251 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
2252 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2253 dp, DM_RIGHT_NULL, NULL,
2254 DM_RIGHT_NULL, dir_name->name, NULL,
2261 /* Return through std_return after this point. */
2263 xfs_itrace_entry(dp);
2267 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2268 prid = dp->i_d.di_projid;
2270 prid = (xfs_prid_t)dfltprid;
2273 * Make sure that we have allocated dquot(s) on disk.
2275 error = XFS_QM_DQVOPALLOC(mp, dp,
2276 current_fsuid(), current_fsgid(), prid,
2277 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2281 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2282 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2283 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
2284 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2285 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2286 if (error == ENOSPC) {
2288 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2289 XFS_TRANS_PERM_LOG_RES,
2290 XFS_MKDIR_LOG_COUNT);
2297 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2298 unlock_dp_on_error = B_TRUE;
2301 * Check for directory link count overflow.
2303 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2304 error = XFS_ERROR(EMLINK);
2309 * Reserve disk quota and the inode.
2311 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2315 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2319 * create the directory inode.
2321 error = xfs_dir_ialloc(&tp, dp, mode, 2,
2322 0, credp, prid, resblks > 0,
2325 if (error == ENOSPC)
2329 xfs_itrace_ref(cdp);
2332 * Now we add the directory inode to the transaction.
2333 * We waited until now since xfs_dir_ialloc might start
2334 * a new transaction. Had we joined the transaction
2335 * earlier, the locks might have gotten released. An error
2336 * from here on will result in the transaction cancel
2337 * unlocking dp so don't do it explicitly in the error path.
2340 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2341 unlock_dp_on_error = B_FALSE;
2343 XFS_BMAP_INIT(&free_list, &first_block);
2345 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2346 &first_block, &free_list, resblks ?
2347 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2349 ASSERT(error != ENOSPC);
2352 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2355 * Bump the in memory version number of the parent directory
2356 * so that other processes accessing it will recognize that
2357 * the directory has changed.
2361 error = xfs_dir_init(tp, cdp, dp);
2366 error = xfs_bumplink(tp, dp);
2376 * Attach the dquots to the new inode and modify the icount incore.
2378 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2381 * If this is a synchronous mount, make sure that the
2382 * mkdir transaction goes to disk before returning to
2385 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2386 xfs_trans_set_sync(tp);
2389 error = xfs_bmap_finish(&tp, &free_list, &committed);
2395 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2396 XFS_QM_DQRELE(mp, udqp);
2397 XFS_QM_DQRELE(mp, gdqp);
2402 /* Fall through to std_return with error = 0 or errno from
2403 * xfs_trans_commit. */
2406 if ((created || (error != 0 && dm_event_sent != 0)) &&
2407 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2408 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2410 created ? cdp : NULL,
2412 dir_name->name, NULL,
2419 xfs_bmap_cancel(&free_list);
2421 cancel_flags |= XFS_TRANS_ABORT;
2423 xfs_trans_cancel(tp, cancel_flags);
2424 XFS_QM_DQRELE(mp, udqp);
2425 XFS_QM_DQRELE(mp, gdqp);
2427 if (unlock_dp_on_error)
2428 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2436 struct xfs_name *link_name,
2437 const char *target_path,
2442 xfs_mount_t *mp = dp->i_mount;
2447 xfs_bmap_free_t free_list;
2448 xfs_fsblock_t first_block;
2449 boolean_t unlock_dp_on_error = B_FALSE;
2452 xfs_fileoff_t first_fsb;
2453 xfs_filblks_t fs_blocks;
2455 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2457 const char *cur_chunk;
2462 struct xfs_dquot *udqp, *gdqp;
2470 xfs_itrace_entry(dp);
2472 if (XFS_FORCED_SHUTDOWN(mp))
2473 return XFS_ERROR(EIO);
2476 * Check component lengths of the target path name.
2478 pathlen = strlen(target_path);
2479 if (pathlen >= MAXPATHLEN) /* total string too long */
2480 return XFS_ERROR(ENAMETOOLONG);
2482 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
2483 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
2484 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2485 link_name->name, target_path, 0, 0, 0);
2490 /* Return through std_return after this point. */
2493 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2494 prid = dp->i_d.di_projid;
2496 prid = (xfs_prid_t)dfltprid;
2499 * Make sure that we have allocated dquot(s) on disk.
2501 error = XFS_QM_DQVOPALLOC(mp, dp,
2502 current_fsuid(), current_fsgid(), prid,
2503 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2507 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2508 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2510 * The symlink will fit into the inode data fork?
2511 * There can't be any attributes so we get the whole variable part.
2513 if (pathlen <= XFS_LITINO(mp))
2516 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2517 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2518 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2519 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2520 if (error == ENOSPC && fs_blocks == 0) {
2522 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2523 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2530 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2531 unlock_dp_on_error = B_TRUE;
2534 * Check whether the directory allows new symlinks or not.
2536 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2537 error = XFS_ERROR(EPERM);
2542 * Reserve disk quota : blocks and inode.
2544 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2549 * Check for ability to enter directory entry, if no space reserved.
2551 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2555 * Initialize the bmap freelist prior to calling either
2556 * bmapi or the directory create code.
2558 XFS_BMAP_INIT(&free_list, &first_block);
2561 * Allocate an inode for the symlink.
2563 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
2564 1, 0, credp, prid, resblks > 0, &ip, NULL);
2566 if (error == ENOSPC)
2573 * An error after we've joined dp to the transaction will result in the
2574 * transaction cancel unlocking dp so don't do it explicitly in the
2578 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2579 unlock_dp_on_error = B_FALSE;
2582 * Also attach the dquot(s) to it, if applicable.
2584 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2587 resblks -= XFS_IALLOC_SPACE_RES(mp);
2589 * If the symlink will fit into the inode, write it inline.
2591 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2592 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2593 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2594 ip->i_d.di_size = pathlen;
2597 * The inode was initially created in extent format.
2599 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2600 ip->i_df.if_flags |= XFS_IFINLINE;
2602 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2603 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2607 nmaps = SYMLINK_MAPS;
2609 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2610 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2611 &first_block, resblks, mval, &nmaps,
2618 resblks -= fs_blocks;
2619 ip->i_d.di_size = pathlen;
2620 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2622 cur_chunk = target_path;
2623 for (n = 0; n < nmaps; n++) {
2624 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2625 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2626 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2627 BTOBB(byte_cnt), 0);
2628 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2629 if (pathlen < byte_cnt) {
2632 pathlen -= byte_cnt;
2634 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2635 cur_chunk += byte_cnt;
2637 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2642 * Create the directory entry for the symlink.
2644 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2645 &first_block, &free_list, resblks);
2648 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2649 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2652 * Bump the in memory version number of the parent directory
2653 * so that other processes accessing it will recognize that
2654 * the directory has changed.
2659 * If this is a synchronous mount, make sure that the
2660 * symlink transaction goes to disk before returning to
2663 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2664 xfs_trans_set_sync(tp);
2668 * xfs_trans_commit normally decrements the vnode ref count
2669 * when it unlocks the inode. Since we want to return the
2670 * vnode to the caller, we bump the vnode ref count now.
2674 error = xfs_bmap_finish(&tp, &free_list, &committed);
2678 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2679 XFS_QM_DQRELE(mp, udqp);
2680 XFS_QM_DQRELE(mp, gdqp);
2682 /* Fall through to std_return with error = 0 or errno from
2683 * xfs_trans_commit */
2685 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
2686 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
2689 DM_RIGHT_NULL, link_name->name,
2690 target_path, 0, error, 0);
2700 xfs_bmap_cancel(&free_list);
2701 cancel_flags |= XFS_TRANS_ABORT;
2703 xfs_trans_cancel(tp, cancel_flags);
2704 XFS_QM_DQRELE(mp, udqp);
2705 XFS_QM_DQRELE(mp, gdqp);
2707 if (unlock_dp_on_error)
2708 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2718 xfs_mount_t *mp = ip->i_mount;
2721 if (XFS_FORCED_SHUTDOWN(mp))
2722 return XFS_ERROR(EIO);
2725 * Bypass inodes which have already been cleaned by
2726 * the inode flush clustering code inside xfs_iflush
2728 if (xfs_inode_clean(ip))
2732 * We make this non-blocking if the inode is contended,
2733 * return EAGAIN to indicate to the caller that they
2734 * did not succeed. This prevents the flush path from
2735 * blocking on inodes inside another operation right
2736 * now, they get caught later by xfs_sync.
2738 if (flags & FLUSH_SYNC) {
2739 xfs_ilock(ip, XFS_ILOCK_SHARED);
2741 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
2742 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
2743 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2750 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
2751 : XFS_IFLUSH_ASYNC_NOBLOCK);
2752 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2764 xfs_mount_t *mp = ip->i_mount;
2768 if (!capable(CAP_SYS_ADMIN))
2769 return XFS_ERROR(EPERM);
2771 if (XFS_FORCED_SHUTDOWN(mp))
2772 return XFS_ERROR(EIO);
2774 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2775 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2777 xfs_trans_cancel(tp, 0);
2780 xfs_ilock(ip, XFS_ILOCK_EXCL);
2781 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2783 ip->i_d.di_dmevmask = evmask;
2784 ip->i_d.di_dmstate = state;
2786 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2788 error = xfs_trans_commit(tp, 0);
2798 xfs_itrace_entry(ip);
2800 ASSERT(!VN_MAPPED(VFS_I(ip)));
2802 /* bad inode, get out here ASAP */
2803 if (VN_BAD(VFS_I(ip))) {
2810 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
2813 * Make sure the atime in the XFS inode is correct before freeing the
2816 xfs_synchronize_atime(ip);
2819 * If we have nothing to flush with this inode then complete the
2820 * teardown now, otherwise break the link between the xfs inode and the
2821 * linux inode and clean up the xfs inode later. This avoids flushing
2822 * the inode to disk during the delete operation itself.
2824 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2825 * first to ensure that xfs_iunpin() will never see an xfs inode
2826 * that has a linux inode being reclaimed. Synchronisation is provided
2827 * by the i_flags_lock.
2829 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2830 xfs_ilock(ip, XFS_ILOCK_EXCL);
2832 xfs_iflags_set(ip, XFS_IRECLAIMABLE);
2833 return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
2835 xfs_inode_set_reclaim_tag(ip);
2840 * xfs_alloc_file_space()
2841 * This routine allocates disk space for the given file.
2843 * If alloc_type == 0, this request is for an ALLOCSP type
2844 * request which will change the file size. In this case, no
2845 * DMAPI event will be generated by the call. A TRUNCATE event
2846 * will be generated later by xfs_setattr.
2848 * If alloc_type != 0, this request is for a RESVSP type
2849 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2850 * lower block boundary byte address is less than the file's
2859 xfs_alloc_file_space(
2866 xfs_mount_t *mp = ip->i_mount;
2868 xfs_filblks_t allocated_fsb;
2869 xfs_filblks_t allocatesize_fsb;
2870 xfs_extlen_t extsz, temp;
2871 xfs_fileoff_t startoffset_fsb;
2872 xfs_fsblock_t firstfsb;
2878 xfs_bmbt_irec_t imaps[1], *imapp;
2879 xfs_bmap_free_t free_list;
2880 uint qblocks, resblks, resrtextents;
2884 xfs_itrace_entry(ip);
2886 if (XFS_FORCED_SHUTDOWN(mp))
2887 return XFS_ERROR(EIO);
2889 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2893 return XFS_ERROR(EINVAL);
2895 rt = XFS_IS_REALTIME_INODE(ip);
2896 extsz = xfs_get_extsz_hint(ip);
2901 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
2902 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2903 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2905 /* Generate a DMAPI event if needed. */
2906 if (alloc_type != 0 && offset < ip->i_size &&
2907 (attr_flags & XFS_ATTR_DMI) == 0 &&
2908 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2909 xfs_off_t end_dmi_offset;
2911 end_dmi_offset = offset+len;
2912 if (end_dmi_offset > ip->i_size)
2913 end_dmi_offset = ip->i_size;
2914 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2915 end_dmi_offset - offset, 0, NULL);
2921 * Allocate file space until done or until there is an error
2924 while (allocatesize_fsb && !error) {
2928 * Determine space reservations for data/realtime.
2930 if (unlikely(extsz)) {
2931 s = startoffset_fsb;
2934 e = startoffset_fsb + allocatesize_fsb;
2935 if ((temp = do_mod(startoffset_fsb, extsz)))
2937 if ((temp = do_mod(e, extsz)))
2941 e = allocatesize_fsb;
2945 resrtextents = qblocks = (uint)(e - s);
2946 resrtextents /= mp->m_sb.sb_rextsize;
2947 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2948 quota_flag = XFS_QMOPT_RES_RTBLKS;
2951 resblks = qblocks = \
2952 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2953 quota_flag = XFS_QMOPT_RES_REGBLKS;
2957 * Allocate and setup the transaction.
2959 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2960 error = xfs_trans_reserve(tp, resblks,
2961 XFS_WRITE_LOG_RES(mp), resrtextents,
2962 XFS_TRANS_PERM_LOG_RES,
2963 XFS_WRITE_LOG_COUNT);
2965 * Check for running out of space
2969 * Free the transaction structure.
2971 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2972 xfs_trans_cancel(tp, 0);
2975 xfs_ilock(ip, XFS_ILOCK_EXCL);
2976 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
2977 qblocks, 0, quota_flag);
2981 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2982 xfs_trans_ihold(tp, ip);
2985 * Issue the xfs_bmapi() call to allocate the blocks
2987 XFS_BMAP_INIT(&free_list, &firstfsb);
2988 error = xfs_bmapi(tp, ip, startoffset_fsb,
2989 allocatesize_fsb, bmapi_flag,
2990 &firstfsb, 0, imapp, &nimaps,
2997 * Complete the transaction
2999 error = xfs_bmap_finish(&tp, &free_list, &committed);
3004 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3005 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3010 allocated_fsb = imapp->br_blockcount;
3013 error = XFS_ERROR(ENOSPC);
3017 startoffset_fsb += allocated_fsb;
3018 allocatesize_fsb -= allocated_fsb;
3021 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
3022 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
3023 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
3026 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3028 goto retry; /* Maybe DMAPI app. has made space */
3029 /* else fall through with error from XFS_SEND_DATA */
3034 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3035 xfs_bmap_cancel(&free_list);
3036 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3038 error1: /* Just cancel transaction */
3039 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3040 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3041 goto dmapi_enospc_check;
3045 * Zero file bytes between startoff and endoff inclusive.
3046 * The iolock is held exclusive and no blocks are buffered.
3048 * This function is used by xfs_free_file_space() to zero
3049 * partial blocks when the range to free is not block aligned.
3050 * When unreserving space with boundaries that are not block
3051 * aligned we round up the start and round down the end
3052 * boundaries and then use this function to zero the parts of
3053 * the blocks that got dropped during the rounding.
3056 xfs_zero_remaining_bytes(
3061 xfs_bmbt_irec_t imap;
3062 xfs_fileoff_t offset_fsb;
3063 xfs_off_t lastoffset;
3066 xfs_mount_t *mp = ip->i_mount;
3071 * Avoid doing I/O beyond eof - it's not necessary
3072 * since nothing can read beyond eof. The space will
3073 * be zeroed when the file is extended anyway.
3075 if (startoff >= ip->i_size)
3078 if (endoff > ip->i_size)
3079 endoff = ip->i_size;
3081 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
3082 XFS_IS_REALTIME_INODE(ip) ?
3083 mp->m_rtdev_targp : mp->m_ddev_targp);
3085 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3086 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3088 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
3089 NULL, 0, &imap, &nimap, NULL, NULL);
3090 if (error || nimap < 1)
3092 ASSERT(imap.br_blockcount >= 1);
3093 ASSERT(imap.br_startoff == offset_fsb);
3094 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3095 if (lastoffset > endoff)
3096 lastoffset = endoff;
3097 if (imap.br_startblock == HOLESTARTBLOCK)
3099 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3100 if (imap.br_state == XFS_EXT_UNWRITTEN)
3103 XFS_BUF_UNWRITE(bp);
3105 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3107 error = xfs_iowait(bp);
3109 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3110 mp, bp, XFS_BUF_ADDR(bp));
3113 memset(XFS_BUF_PTR(bp) +
3114 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3115 0, lastoffset - offset + 1);
3120 error = xfs_iowait(bp);
3122 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3123 mp, bp, XFS_BUF_ADDR(bp));
3132 * xfs_free_file_space()
3133 * This routine frees disk space for the given file.
3135 * This routine is only called by xfs_change_file_space
3136 * for an UNRESVSP type call.
3144 xfs_free_file_space(
3152 xfs_off_t end_dmi_offset;
3153 xfs_fileoff_t endoffset_fsb;
3155 xfs_fsblock_t firstfsb;
3156 xfs_bmap_free_t free_list;
3157 xfs_bmbt_irec_t imap;
3165 xfs_fileoff_t startoffset_fsb;
3167 int need_iolock = 1;
3171 xfs_itrace_entry(ip);
3173 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3177 if (len <= 0) /* if nothing being freed */
3179 rt = XFS_IS_REALTIME_INODE(ip);
3180 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3181 end_dmi_offset = offset + len;
3182 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3184 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
3185 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3186 if (end_dmi_offset > ip->i_size)
3187 end_dmi_offset = ip->i_size;
3188 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
3189 offset, end_dmi_offset - offset,
3190 AT_DELAY_FLAG(attr_flags), NULL);
3195 if (attr_flags & XFS_ATTR_NOLOCK)
3198 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3199 vn_iowait(ip); /* wait for the completion of any pending DIOs */
3202 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
3203 ioffset = offset & ~(rounding - 1);
3205 if (VN_CACHED(VFS_I(ip)) != 0) {
3206 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3207 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
3209 goto out_unlock_iolock;
3213 * Need to zero the stuff we're not freeing, on disk.
3214 * If its a realtime file & can't use unwritten extents then we
3215 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3216 * will take care of it for us.
3218 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
3220 error = xfs_bmapi(NULL, ip, startoffset_fsb,
3221 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3223 goto out_unlock_iolock;
3224 ASSERT(nimap == 0 || nimap == 1);
3225 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3228 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3229 block = imap.br_startblock;
3230 mod = do_div(block, mp->m_sb.sb_rextsize);
3232 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3235 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
3236 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3238 goto out_unlock_iolock;
3239 ASSERT(nimap == 0 || nimap == 1);
3240 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3241 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3243 if (mod && (mod != mp->m_sb.sb_rextsize))
3244 endoffset_fsb -= mod;
3247 if ((done = (endoffset_fsb <= startoffset_fsb)))
3249 * One contiguous piece to clear
3251 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3254 * Some full blocks, possibly two pieces to clear
3256 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3257 error = xfs_zero_remaining_bytes(ip, offset,
3258 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3260 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3261 error = xfs_zero_remaining_bytes(ip,
3262 XFS_FSB_TO_B(mp, endoffset_fsb),
3267 * free file space until done or until there is an error
3269 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3270 while (!error && !done) {
3273 * allocate and setup the transaction. Allow this
3274 * transaction to dip into the reserve blocks to ensure
3275 * the freeing of the space succeeds at ENOSPC.
3277 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3278 tp->t_flags |= XFS_TRANS_RESERVE;
3279 error = xfs_trans_reserve(tp,
3281 XFS_WRITE_LOG_RES(mp),
3283 XFS_TRANS_PERM_LOG_RES,
3284 XFS_WRITE_LOG_COUNT);
3287 * check for running out of space
3291 * Free the transaction structure.
3293 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3294 xfs_trans_cancel(tp, 0);
3297 xfs_ilock(ip, XFS_ILOCK_EXCL);
3298 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
3299 ip->i_udquot, ip->i_gdquot, resblks, 0,
3300 XFS_QMOPT_RES_REGBLKS);
3304 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3305 xfs_trans_ihold(tp, ip);
3308 * issue the bunmapi() call to free the blocks
3310 XFS_BMAP_INIT(&free_list, &firstfsb);
3311 error = xfs_bunmapi(tp, ip, startoffset_fsb,
3312 endoffset_fsb - startoffset_fsb,
3313 0, 2, &firstfsb, &free_list, NULL, &done);
3319 * complete the transaction
3321 error = xfs_bmap_finish(&tp, &free_list, &committed);
3326 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3327 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3332 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3336 xfs_bmap_cancel(&free_list);
3338 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3339 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3345 * xfs_change_file_space()
3346 * This routine allocates or frees disk space for the given file.
3347 * The user specified parameters are checked for alignment and size
3356 xfs_change_file_space(
3363 xfs_mount_t *mp = ip->i_mount;
3368 xfs_off_t startoffset;
3373 xfs_itrace_entry(ip);
3375 if (!S_ISREG(ip->i_d.di_mode))
3376 return XFS_ERROR(EINVAL);
3378 switch (bf->l_whence) {
3379 case 0: /*SEEK_SET*/
3381 case 1: /*SEEK_CUR*/
3382 bf->l_start += offset;
3384 case 2: /*SEEK_END*/
3385 bf->l_start += ip->i_size;
3388 return XFS_ERROR(EINVAL);
3391 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3393 if ( (bf->l_start < 0)
3394 || (bf->l_start > XFS_MAXIOFFSET(mp))
3395 || (bf->l_start + llen < 0)
3396 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3397 return XFS_ERROR(EINVAL);
3401 startoffset = bf->l_start;
3405 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3407 * These calls do NOT zero the data space allocated to the file,
3408 * nor do they change the file size.
3410 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3412 * These calls cause the new file data to be zeroed and the file
3413 * size to be changed.
3415 setprealloc = clrprealloc = 0;
3418 case XFS_IOC_RESVSP:
3419 case XFS_IOC_RESVSP64:
3420 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3427 case XFS_IOC_UNRESVSP:
3428 case XFS_IOC_UNRESVSP64:
3429 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3434 case XFS_IOC_ALLOCSP:
3435 case XFS_IOC_ALLOCSP64:
3436 case XFS_IOC_FREESP:
3437 case XFS_IOC_FREESP64:
3438 if (startoffset > fsize) {
3439 error = xfs_alloc_file_space(ip, fsize,
3440 startoffset - fsize, 0, attr_flags);
3445 iattr.ia_valid = ATTR_SIZE;
3446 iattr.ia_size = startoffset;
3448 error = xfs_setattr(ip, &iattr, attr_flags);
3458 return XFS_ERROR(EINVAL);
3462 * update the inode timestamp, mode, and prealloc flag bits
3464 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3466 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3469 xfs_trans_cancel(tp, 0);
3473 xfs_ilock(ip, XFS_ILOCK_EXCL);
3475 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3476 xfs_trans_ihold(tp, ip);
3478 if ((attr_flags & XFS_ATTR_DMI) == 0) {
3479 ip->i_d.di_mode &= ~S_ISUID;
3482 * Note that we don't have to worry about mandatory
3483 * file locking being disabled here because we only
3484 * clear the S_ISGID bit if the Group execute bit is
3485 * on, but if it was on then mandatory locking wouldn't
3486 * have been enabled.
3488 if (ip->i_d.di_mode & S_IXGRP)
3489 ip->i_d.di_mode &= ~S_ISGID;
3491 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3494 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3495 else if (clrprealloc)
3496 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3498 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3499 xfs_trans_set_sync(tp);
3501 error = xfs_trans_commit(tp, 0);
3503 xfs_iunlock(ip, XFS_ILOCK_EXCL);