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 * Remove the memory for extent descriptions (just bookkeeping).
1024 if (ip->i_df.if_bytes)
1025 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1026 ASSERT(ip->i_df.if_bytes == 0);
1028 * Put an itruncate log reservation in the new transaction
1031 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1032 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1033 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1037 * Return with the inode locked but not joined to the transaction.
1043 xfs_bmap_cancel(&free_list);
1046 * Have to come here with the inode locked and either
1047 * (held and in the transaction) or (not in the transaction).
1048 * If the inode isn't held then cancel would iput it, but
1049 * that's wrong since this is inactive and the vnode ref
1050 * count is 0 already.
1051 * Cancel won't do anything to the inode if held, but it still
1052 * needs to be locked until the cancel is done, if it was
1053 * joined to the transaction.
1055 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1056 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1063 xfs_inactive_symlink_local(
1069 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1071 * We're freeing a symlink which fit into
1072 * the inode. Just free the memory used
1073 * to hold the old symlink.
1075 error = xfs_trans_reserve(*tpp, 0,
1076 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1077 0, XFS_TRANS_PERM_LOG_RES,
1078 XFS_ITRUNCATE_LOG_COUNT);
1081 xfs_trans_cancel(*tpp, 0);
1085 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1088 * Zero length symlinks _can_ exist.
1090 if (ip->i_df.if_bytes > 0) {
1091 xfs_idata_realloc(ip,
1092 -(ip->i_df.if_bytes),
1094 ASSERT(ip->i_df.if_bytes == 0);
1108 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1111 ASSERT(ip->i_d.di_forkoff != 0);
1112 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1113 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1117 error = xfs_attr_inactive(ip);
1121 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1122 error = xfs_trans_reserve(tp, 0,
1123 XFS_IFREE_LOG_RES(mp),
1124 0, XFS_TRANS_PERM_LOG_RES,
1125 XFS_INACTIVE_LOG_COUNT);
1129 xfs_ilock(ip, XFS_ILOCK_EXCL);
1130 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1131 xfs_trans_ihold(tp, ip);
1132 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1134 ASSERT(ip->i_d.di_anextents == 0);
1140 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1141 xfs_trans_cancel(tp, 0);
1144 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1152 xfs_mount_t *mp = ip->i_mount;
1155 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1158 /* If this is a read-only mount, don't do this (would generate I/O) */
1159 if (mp->m_flags & XFS_MOUNT_RDONLY)
1162 if (!XFS_FORCED_SHUTDOWN(mp)) {
1166 * If we are using filestreams, and we have an unlinked
1167 * file that we are processing the last close on, then nothing
1168 * will be able to reopen and write to this file. Purge this
1169 * inode from the filestreams cache so that it doesn't delay
1170 * teardown of the inode.
1172 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1173 xfs_filestream_deassociate(ip);
1176 * If we previously truncated this file and removed old data
1177 * in the process, we want to initiate "early" writeout on
1178 * the last close. This is an attempt to combat the notorious
1179 * NULL files problem which is particularly noticable from a
1180 * truncate down, buffered (re-)write (delalloc), followed by
1181 * a crash. What we are effectively doing here is
1182 * significantly reducing the time window where we'd otherwise
1183 * be exposed to that problem.
1185 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1186 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
1187 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1190 if (ip->i_d.di_nlink != 0) {
1191 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1192 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1193 ip->i_delayed_blks > 0)) &&
1194 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1195 (!(ip->i_d.di_flags &
1196 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1197 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1209 * This is called when the vnode reference count for the vnode
1210 * goes to zero. If the file has been unlinked, then it must
1211 * now be truncated. Also, we clear all of the read-ahead state
1212 * kept for the inode here since the file is now closed.
1218 xfs_bmap_free_t free_list;
1219 xfs_fsblock_t first_block;
1226 xfs_itrace_entry(ip);
1229 * If the inode is already free, then there can be nothing
1232 if (ip->i_d.di_mode == 0 || VN_BAD(VFS_I(ip))) {
1233 ASSERT(ip->i_df.if_real_bytes == 0);
1234 ASSERT(ip->i_df.if_broot_bytes == 0);
1235 return VN_INACTIVE_CACHE;
1239 * Only do a truncate if it's a regular file with
1240 * some actual space in it. It's OK to look at the
1241 * inode's fields without the lock because we're the
1242 * only one with a reference to the inode.
1244 truncate = ((ip->i_d.di_nlink == 0) &&
1245 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1246 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1247 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1251 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1252 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1256 /* If this is a read-only mount, don't do this (would generate I/O) */
1257 if (mp->m_flags & XFS_MOUNT_RDONLY)
1260 if (ip->i_d.di_nlink != 0) {
1261 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1262 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1263 ip->i_delayed_blks > 0)) &&
1264 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1265 (!(ip->i_d.di_flags &
1266 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1267 (ip->i_delayed_blks != 0)))) {
1268 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1270 return VN_INACTIVE_CACHE;
1275 ASSERT(ip->i_d.di_nlink == 0);
1277 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1278 return VN_INACTIVE_CACHE;
1280 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1283 * Do the xfs_itruncate_start() call before
1284 * reserving any log space because itruncate_start
1285 * will call into the buffer cache and we can't
1286 * do that within a transaction.
1288 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1290 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1292 xfs_trans_cancel(tp, 0);
1293 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1294 return VN_INACTIVE_CACHE;
1297 error = xfs_trans_reserve(tp, 0,
1298 XFS_ITRUNCATE_LOG_RES(mp),
1299 0, XFS_TRANS_PERM_LOG_RES,
1300 XFS_ITRUNCATE_LOG_COUNT);
1302 /* Don't call itruncate_cleanup */
1303 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1304 xfs_trans_cancel(tp, 0);
1305 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1306 return VN_INACTIVE_CACHE;
1309 xfs_ilock(ip, XFS_ILOCK_EXCL);
1310 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1311 xfs_trans_ihold(tp, ip);
1314 * normally, we have to run xfs_itruncate_finish sync.
1315 * But if filesystem is wsync and we're in the inactive
1316 * path, then we know that nlink == 0, and that the
1317 * xaction that made nlink == 0 is permanently committed
1318 * since xfs_remove runs as a synchronous transaction.
1320 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1321 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1324 xfs_trans_cancel(tp,
1325 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1326 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1327 return VN_INACTIVE_CACHE;
1329 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1332 * If we get an error while cleaning up a
1333 * symlink we bail out.
1335 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1336 xfs_inactive_symlink_rmt(ip, &tp) :
1337 xfs_inactive_symlink_local(ip, &tp);
1341 return VN_INACTIVE_CACHE;
1344 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1345 xfs_trans_ihold(tp, ip);
1347 error = xfs_trans_reserve(tp, 0,
1348 XFS_IFREE_LOG_RES(mp),
1349 0, XFS_TRANS_PERM_LOG_RES,
1350 XFS_INACTIVE_LOG_COUNT);
1352 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1353 xfs_trans_cancel(tp, 0);
1354 return VN_INACTIVE_CACHE;
1357 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1358 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1359 xfs_trans_ihold(tp, ip);
1363 * If there are attributes associated with the file
1364 * then blow them away now. The code calls a routine
1365 * that recursively deconstructs the attribute fork.
1366 * We need to just commit the current transaction
1367 * because we can't use it for xfs_attr_inactive().
1369 if (ip->i_d.di_anextents > 0) {
1370 error = xfs_inactive_attrs(ip, &tp);
1372 * If we got an error, the transaction is already
1373 * cancelled, and the inode is unlocked. Just get out.
1376 return VN_INACTIVE_CACHE;
1377 } else if (ip->i_afp) {
1378 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1384 XFS_BMAP_INIT(&free_list, &first_block);
1385 error = xfs_ifree(tp, ip, &free_list);
1388 * If we fail to free the inode, shut down. The cancel
1389 * might do that, we need to make sure. Otherwise the
1390 * inode might be lost for a long time or forever.
1392 if (!XFS_FORCED_SHUTDOWN(mp)) {
1394 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1395 error, mp->m_fsname);
1396 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1398 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1401 * Credit the quota account(s). The inode is gone.
1403 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1406 * Just ignore errors at this point. There is nothing we can
1407 * do except to try to keep going. Make sure it's not a silent
1410 error = xfs_bmap_finish(&tp, &free_list, &committed);
1412 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1413 "xfs_bmap_finish() returned error %d", error);
1414 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1416 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1417 "xfs_trans_commit() returned error %d", error);
1420 * Release the dquots held by inode, if any.
1422 XFS_QM_DQDETACH(mp, ip);
1424 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1427 return VN_INACTIVE_CACHE;
1431 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1432 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1433 * ci_name->name will point to a the actual name (caller must free) or
1434 * will be set to NULL if an exact match is found.
1439 struct xfs_name *name,
1441 struct xfs_name *ci_name)
1447 xfs_itrace_entry(dp);
1449 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1450 return XFS_ERROR(EIO);
1452 lock_mode = xfs_ilock_map_shared(dp);
1453 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1454 xfs_iunlock_map_shared(dp, lock_mode);
1459 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1463 xfs_itrace_ref(*ipp);
1468 kmem_free(ci_name->name);
1477 struct xfs_name *name,
1483 xfs_mount_t *mp = dp->i_mount;
1487 xfs_bmap_free_t free_list;
1488 xfs_fsblock_t first_block;
1489 boolean_t unlock_dp_on_error = B_FALSE;
1490 int dm_event_sent = 0;
1494 struct xfs_dquot *udqp, *gdqp;
1498 xfs_itrace_entry(dp);
1500 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1501 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1502 dp, DM_RIGHT_NULL, NULL,
1503 DM_RIGHT_NULL, name->name, NULL,
1511 if (XFS_FORCED_SHUTDOWN(mp))
1512 return XFS_ERROR(EIO);
1514 /* Return through std_return after this point. */
1517 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1518 prid = dp->i_d.di_projid;
1520 prid = (xfs_prid_t)dfltprid;
1523 * Make sure that we have allocated dquot(s) on disk.
1525 error = XFS_QM_DQVOPALLOC(mp, dp,
1526 current_fsuid(), current_fsgid(), prid,
1527 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1533 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1534 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1535 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1537 * Initially assume that the file does not exist and
1538 * reserve the resources for that case. If that is not
1539 * the case we'll drop the one we have and get a more
1540 * appropriate transaction later.
1542 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1543 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1544 if (error == ENOSPC) {
1546 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1547 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1554 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1555 unlock_dp_on_error = B_TRUE;
1557 XFS_BMAP_INIT(&free_list, &first_block);
1562 * Reserve disk quota and the inode.
1564 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1568 error = xfs_dir_canenter(tp, dp, name, resblks);
1571 error = xfs_dir_ialloc(&tp, dp, mode, 1,
1572 rdev, credp, prid, resblks > 0,
1575 if (error == ENOSPC)
1582 * At this point, we've gotten a newly allocated inode.
1583 * It is locked (and joined to the transaction).
1586 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1589 * Now we join the directory inode to the transaction. We do not do it
1590 * earlier because xfs_dir_ialloc might commit the previous transaction
1591 * (and release all the locks). An error from here on will result in
1592 * the transaction cancel unlocking dp so don't do it explicitly in the
1596 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1597 unlock_dp_on_error = B_FALSE;
1599 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1600 &first_block, &free_list, resblks ?
1601 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1603 ASSERT(error != ENOSPC);
1606 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1607 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1610 * If this is a synchronous mount, make sure that the
1611 * create transaction goes to disk before returning to
1614 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1615 xfs_trans_set_sync(tp);
1621 * Attach the dquot(s) to the inodes and modify them incore.
1622 * These ids of the inode couldn't have changed since the new
1623 * inode has been locked ever since it was created.
1625 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1628 * xfs_trans_commit normally decrements the vnode ref count
1629 * when it unlocks the inode. Since we want to return the
1630 * vnode to the caller, we bump the vnode ref count now.
1634 error = xfs_bmap_finish(&tp, &free_list, &committed);
1636 xfs_bmap_cancel(&free_list);
1640 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1647 XFS_QM_DQRELE(mp, udqp);
1648 XFS_QM_DQRELE(mp, gdqp);
1652 /* Fallthrough to std_return with error = 0 */
1655 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
1656 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1657 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
1660 DM_RIGHT_NULL, name->name, NULL,
1666 cancel_flags |= XFS_TRANS_ABORT;
1671 xfs_trans_cancel(tp, cancel_flags);
1673 XFS_QM_DQRELE(mp, udqp);
1674 XFS_QM_DQRELE(mp, gdqp);
1676 if (unlock_dp_on_error)
1677 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1683 * Wait until after the current transaction is aborted to
1684 * release the inode. This prevents recursive transactions
1685 * and deadlocks from xfs_inactive.
1687 cancel_flags |= XFS_TRANS_ABORT;
1688 xfs_trans_cancel(tp, cancel_flags);
1691 XFS_QM_DQRELE(mp, udqp);
1692 XFS_QM_DQRELE(mp, gdqp);
1699 int xfs_small_retries;
1700 int xfs_middle_retries;
1701 int xfs_lots_retries;
1702 int xfs_lock_delays;
1706 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1710 xfs_lock_inumorder(int lock_mode, int subclass)
1712 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1713 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1714 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1715 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1721 * The following routine will lock n inodes in exclusive mode.
1722 * We assume the caller calls us with the inodes in i_ino order.
1724 * We need to detect deadlock where an inode that we lock
1725 * is in the AIL and we start waiting for another inode that is locked
1726 * by a thread in a long running transaction (such as truncate). This can
1727 * result in deadlock since the long running trans might need to wait
1728 * for the inode we just locked in order to push the tail and free space
1737 int attempts = 0, i, j, try_lock;
1740 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1746 for (; i < inodes; i++) {
1749 if (i && (ips[i] == ips[i-1])) /* Already locked */
1753 * If try_lock is not set yet, make sure all locked inodes
1754 * are not in the AIL.
1755 * If any are, set try_lock to be used later.
1759 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1760 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1761 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1768 * If any of the previous locks we have locked is in the AIL,
1769 * we must TRY to get the second and subsequent locks. If
1770 * we can't get any, we must release all we have
1775 /* try_lock must be 0 if i is 0. */
1777 * try_lock means we have an inode locked
1778 * that is in the AIL.
1781 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1785 * Unlock all previous guys and try again.
1786 * xfs_iunlock will try to push the tail
1787 * if the inode is in the AIL.
1790 for(j = i - 1; j >= 0; j--) {
1793 * Check to see if we've already
1794 * unlocked this one.
1795 * Not the first one going back,
1796 * and the inode ptr is the same.
1798 if ((j != (i - 1)) && ips[j] ==
1802 xfs_iunlock(ips[j], lock_mode);
1805 if ((attempts % 5) == 0) {
1806 delay(1); /* Don't just spin the CPU */
1816 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1822 if (attempts < 5) xfs_small_retries++;
1823 else if (attempts < 100) xfs_middle_retries++;
1824 else xfs_lots_retries++;
1832 * xfs_lock_two_inodes() can only be used to lock one type of lock
1833 * at a time - the iolock or the ilock, but not both at once. If
1834 * we lock both at once, lockdep will report false positives saying
1835 * we have violated locking orders.
1838 xfs_lock_two_inodes(
1847 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1848 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1849 ASSERT(ip0->i_ino != ip1->i_ino);
1851 if (ip0->i_ino > ip1->i_ino) {
1858 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1861 * If the first lock we have locked is in the AIL, we must TRY to get
1862 * the second lock. If we can't get it, we must release the first one
1865 lp = (xfs_log_item_t *)ip0->i_itemp;
1866 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1867 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1868 xfs_iunlock(ip0, lock_mode);
1869 if ((++attempts % 5) == 0)
1870 delay(1); /* Don't just spin the CPU */
1874 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1881 struct xfs_name *name,
1884 xfs_mount_t *mp = dp->i_mount;
1885 xfs_trans_t *tp = NULL;
1886 int is_dir = S_ISDIR(ip->i_d.di_mode);
1888 xfs_bmap_free_t free_list;
1889 xfs_fsblock_t first_block;
1896 xfs_itrace_entry(dp);
1897 xfs_itrace_entry(ip);
1899 if (XFS_FORCED_SHUTDOWN(mp))
1900 return XFS_ERROR(EIO);
1902 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
1903 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1904 NULL, DM_RIGHT_NULL, name->name, NULL,
1905 ip->i_d.di_mode, 0, 0);
1910 error = XFS_QM_DQATTACH(mp, dp, 0);
1914 error = XFS_QM_DQATTACH(mp, ip, 0);
1919 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1920 log_count = XFS_DEFAULT_LOG_COUNT;
1922 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1923 log_count = XFS_REMOVE_LOG_COUNT;
1925 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1928 * We try to get the real space reservation first,
1929 * allowing for directory btree deletion(s) implying
1930 * possible bmap insert(s). If we can't get the space
1931 * reservation then we use 0 instead, and avoid the bmap
1932 * btree insert(s) in the directory code by, if the bmap
1933 * insert tries to happen, instead trimming the LAST
1934 * block from the directory.
1936 resblks = XFS_REMOVE_SPACE_RES(mp);
1937 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1938 XFS_TRANS_PERM_LOG_RES, log_count);
1939 if (error == ENOSPC) {
1941 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1942 XFS_TRANS_PERM_LOG_RES, log_count);
1945 ASSERT(error != ENOSPC);
1947 goto out_trans_cancel;
1950 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1953 * At this point, we've gotten both the directory and the entry
1957 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1960 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1963 * If we're removing a directory perform some additional validation.
1966 ASSERT(ip->i_d.di_nlink >= 2);
1967 if (ip->i_d.di_nlink != 2) {
1968 error = XFS_ERROR(ENOTEMPTY);
1969 goto out_trans_cancel;
1971 if (!xfs_dir_isempty(ip)) {
1972 error = XFS_ERROR(ENOTEMPTY);
1973 goto out_trans_cancel;
1977 XFS_BMAP_INIT(&free_list, &first_block);
1978 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1979 &first_block, &free_list, resblks);
1981 ASSERT(error != ENOENT);
1982 goto out_bmap_cancel;
1984 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1987 * Bump the in memory generation count on the parent
1988 * directory so that other can know that it has changed.
1991 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1995 * Drop the link from ip's "..".
1997 error = xfs_droplink(tp, dp);
1999 goto out_bmap_cancel;
2002 * Drop the "." link from ip to self.
2004 error = xfs_droplink(tp, ip);
2006 goto out_bmap_cancel;
2009 * When removing a non-directory we need to log the parent
2010 * inode here for the i_gen update. For a directory this is
2011 * done implicitly by the xfs_droplink call for the ".." entry.
2013 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2017 * Drop the link from dp to ip.
2019 error = xfs_droplink(tp, ip);
2021 goto out_bmap_cancel;
2024 * Determine if this is the last link while
2025 * we are in the transaction.
2027 link_zero = (ip->i_d.di_nlink == 0);
2030 * If this is a synchronous mount, make sure that the
2031 * remove transaction goes to disk before returning to
2034 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2035 xfs_trans_set_sync(tp);
2037 error = xfs_bmap_finish(&tp, &free_list, &committed);
2039 goto out_bmap_cancel;
2041 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2046 * If we are using filestreams, kill the stream association.
2047 * If the file is still open it may get a new one but that
2048 * will get killed on last close in xfs_close() so we don't
2049 * have to worry about that.
2051 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2052 xfs_filestream_deassociate(ip);
2054 xfs_itrace_exit(ip);
2055 xfs_itrace_exit(dp);
2058 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2059 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2060 NULL, DM_RIGHT_NULL, name->name, NULL,
2061 ip->i_d.di_mode, error, 0);
2067 xfs_bmap_cancel(&free_list);
2068 cancel_flags |= XFS_TRANS_ABORT;
2070 xfs_trans_cancel(tp, cancel_flags);
2078 struct xfs_name *target_name)
2080 xfs_mount_t *mp = tdp->i_mount;
2083 xfs_bmap_free_t free_list;
2084 xfs_fsblock_t first_block;
2089 xfs_itrace_entry(tdp);
2090 xfs_itrace_entry(sip);
2092 ASSERT(!S_ISDIR(sip->i_d.di_mode));
2094 if (XFS_FORCED_SHUTDOWN(mp))
2095 return XFS_ERROR(EIO);
2097 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2098 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2101 target_name->name, NULL, 0, 0, 0);
2106 /* Return through std_return after this point. */
2108 error = XFS_QM_DQATTACH(mp, sip, 0);
2109 if (!error && sip != tdp)
2110 error = XFS_QM_DQATTACH(mp, tdp, 0);
2114 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2115 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2116 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
2117 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2118 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2119 if (error == ENOSPC) {
2121 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2122 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2129 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
2132 * Increment vnode ref counts since xfs_trans_commit &
2133 * xfs_trans_cancel will both unlock the inodes and
2134 * decrement the associated ref counts.
2138 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2139 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2142 * If the source has too many links, we can't make any more to it.
2144 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2145 error = XFS_ERROR(EMLINK);
2150 * If we are using project inheritance, we only allow hard link
2151 * creation in our tree when the project IDs are the same; else
2152 * the tree quota mechanism could be circumvented.
2154 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2155 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2156 error = XFS_ERROR(EXDEV);
2160 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2164 XFS_BMAP_INIT(&free_list, &first_block);
2166 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2167 &first_block, &free_list, resblks);
2170 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2172 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2174 error = xfs_bumplink(tp, sip);
2179 * If this is a synchronous mount, make sure that the
2180 * link transaction goes to disk before returning to
2183 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2184 xfs_trans_set_sync(tp);
2187 error = xfs_bmap_finish (&tp, &free_list, &committed);
2189 xfs_bmap_cancel(&free_list);
2193 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2197 /* Fall through to std_return with error = 0. */
2199 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2200 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2203 target_name->name, NULL, 0, error, 0);
2208 cancel_flags |= XFS_TRANS_ABORT;
2212 xfs_trans_cancel(tp, cancel_flags);
2220 struct xfs_name *dir_name,
2225 xfs_mount_t *mp = dp->i_mount;
2226 xfs_inode_t *cdp; /* inode of created dir */
2231 xfs_bmap_free_t free_list;
2232 xfs_fsblock_t first_block;
2233 boolean_t unlock_dp_on_error = B_FALSE;
2234 boolean_t created = B_FALSE;
2235 int dm_event_sent = 0;
2237 struct xfs_dquot *udqp, *gdqp;
2240 if (XFS_FORCED_SHUTDOWN(mp))
2241 return XFS_ERROR(EIO);
2245 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
2246 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2247 dp, DM_RIGHT_NULL, NULL,
2248 DM_RIGHT_NULL, dir_name->name, NULL,
2255 /* Return through std_return after this point. */
2257 xfs_itrace_entry(dp);
2261 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2262 prid = dp->i_d.di_projid;
2264 prid = (xfs_prid_t)dfltprid;
2267 * Make sure that we have allocated dquot(s) on disk.
2269 error = XFS_QM_DQVOPALLOC(mp, dp,
2270 current_fsuid(), current_fsgid(), prid,
2271 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2275 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2276 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2277 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
2278 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2279 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2280 if (error == ENOSPC) {
2282 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2283 XFS_TRANS_PERM_LOG_RES,
2284 XFS_MKDIR_LOG_COUNT);
2291 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2292 unlock_dp_on_error = B_TRUE;
2295 * Check for directory link count overflow.
2297 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2298 error = XFS_ERROR(EMLINK);
2303 * Reserve disk quota and the inode.
2305 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2309 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2313 * create the directory inode.
2315 error = xfs_dir_ialloc(&tp, dp, mode, 2,
2316 0, credp, prid, resblks > 0,
2319 if (error == ENOSPC)
2323 xfs_itrace_ref(cdp);
2326 * Now we add the directory inode to the transaction.
2327 * We waited until now since xfs_dir_ialloc might start
2328 * a new transaction. Had we joined the transaction
2329 * earlier, the locks might have gotten released. An error
2330 * from here on will result in the transaction cancel
2331 * unlocking dp so don't do it explicitly in the error path.
2334 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2335 unlock_dp_on_error = B_FALSE;
2337 XFS_BMAP_INIT(&free_list, &first_block);
2339 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2340 &first_block, &free_list, resblks ?
2341 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2343 ASSERT(error != ENOSPC);
2346 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2349 * Bump the in memory version number of the parent directory
2350 * so that other processes accessing it will recognize that
2351 * the directory has changed.
2355 error = xfs_dir_init(tp, cdp, dp);
2360 error = xfs_bumplink(tp, dp);
2370 * Attach the dquots to the new inode and modify the icount incore.
2372 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2375 * If this is a synchronous mount, make sure that the
2376 * mkdir transaction goes to disk before returning to
2379 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2380 xfs_trans_set_sync(tp);
2383 error = xfs_bmap_finish(&tp, &free_list, &committed);
2389 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2390 XFS_QM_DQRELE(mp, udqp);
2391 XFS_QM_DQRELE(mp, gdqp);
2396 /* Fall through to std_return with error = 0 or errno from
2397 * xfs_trans_commit. */
2400 if ((created || (error != 0 && dm_event_sent != 0)) &&
2401 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2402 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2404 created ? cdp : NULL,
2406 dir_name->name, NULL,
2413 xfs_bmap_cancel(&free_list);
2415 cancel_flags |= XFS_TRANS_ABORT;
2417 xfs_trans_cancel(tp, cancel_flags);
2418 XFS_QM_DQRELE(mp, udqp);
2419 XFS_QM_DQRELE(mp, gdqp);
2421 if (unlock_dp_on_error)
2422 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2430 struct xfs_name *link_name,
2431 const char *target_path,
2436 xfs_mount_t *mp = dp->i_mount;
2441 xfs_bmap_free_t free_list;
2442 xfs_fsblock_t first_block;
2443 boolean_t unlock_dp_on_error = B_FALSE;
2446 xfs_fileoff_t first_fsb;
2447 xfs_filblks_t fs_blocks;
2449 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2451 const char *cur_chunk;
2456 struct xfs_dquot *udqp, *gdqp;
2464 xfs_itrace_entry(dp);
2466 if (XFS_FORCED_SHUTDOWN(mp))
2467 return XFS_ERROR(EIO);
2470 * Check component lengths of the target path name.
2472 pathlen = strlen(target_path);
2473 if (pathlen >= MAXPATHLEN) /* total string too long */
2474 return XFS_ERROR(ENAMETOOLONG);
2476 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
2477 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
2478 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2479 link_name->name, target_path, 0, 0, 0);
2484 /* Return through std_return after this point. */
2487 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2488 prid = dp->i_d.di_projid;
2490 prid = (xfs_prid_t)dfltprid;
2493 * Make sure that we have allocated dquot(s) on disk.
2495 error = XFS_QM_DQVOPALLOC(mp, dp,
2496 current_fsuid(), current_fsgid(), prid,
2497 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2501 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2502 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2504 * The symlink will fit into the inode data fork?
2505 * There can't be any attributes so we get the whole variable part.
2507 if (pathlen <= XFS_LITINO(mp))
2510 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2511 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2512 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2513 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2514 if (error == ENOSPC && fs_blocks == 0) {
2516 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2517 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2524 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2525 unlock_dp_on_error = B_TRUE;
2528 * Check whether the directory allows new symlinks or not.
2530 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2531 error = XFS_ERROR(EPERM);
2536 * Reserve disk quota : blocks and inode.
2538 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2543 * Check for ability to enter directory entry, if no space reserved.
2545 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2549 * Initialize the bmap freelist prior to calling either
2550 * bmapi or the directory create code.
2552 XFS_BMAP_INIT(&free_list, &first_block);
2555 * Allocate an inode for the symlink.
2557 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
2558 1, 0, credp, prid, resblks > 0, &ip, NULL);
2560 if (error == ENOSPC)
2567 * An error after we've joined dp to the transaction will result in the
2568 * transaction cancel unlocking dp so don't do it explicitly in the
2572 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2573 unlock_dp_on_error = B_FALSE;
2576 * Also attach the dquot(s) to it, if applicable.
2578 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2581 resblks -= XFS_IALLOC_SPACE_RES(mp);
2583 * If the symlink will fit into the inode, write it inline.
2585 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2586 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2587 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2588 ip->i_d.di_size = pathlen;
2591 * The inode was initially created in extent format.
2593 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2594 ip->i_df.if_flags |= XFS_IFINLINE;
2596 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2597 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2601 nmaps = SYMLINK_MAPS;
2603 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2604 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2605 &first_block, resblks, mval, &nmaps,
2612 resblks -= fs_blocks;
2613 ip->i_d.di_size = pathlen;
2614 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2616 cur_chunk = target_path;
2617 for (n = 0; n < nmaps; n++) {
2618 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2619 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2620 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2621 BTOBB(byte_cnt), 0);
2622 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2623 if (pathlen < byte_cnt) {
2626 pathlen -= byte_cnt;
2628 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2629 cur_chunk += byte_cnt;
2631 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2636 * Create the directory entry for the symlink.
2638 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2639 &first_block, &free_list, resblks);
2642 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2643 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2646 * Bump the in memory version number of the parent directory
2647 * so that other processes accessing it will recognize that
2648 * the directory has changed.
2653 * If this is a synchronous mount, make sure that the
2654 * symlink transaction goes to disk before returning to
2657 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2658 xfs_trans_set_sync(tp);
2662 * xfs_trans_commit normally decrements the vnode ref count
2663 * when it unlocks the inode. Since we want to return the
2664 * vnode to the caller, we bump the vnode ref count now.
2668 error = xfs_bmap_finish(&tp, &free_list, &committed);
2672 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2673 XFS_QM_DQRELE(mp, udqp);
2674 XFS_QM_DQRELE(mp, gdqp);
2676 /* Fall through to std_return with error = 0 or errno from
2677 * xfs_trans_commit */
2679 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
2680 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
2683 DM_RIGHT_NULL, link_name->name,
2684 target_path, 0, error, 0);
2694 xfs_bmap_cancel(&free_list);
2695 cancel_flags |= XFS_TRANS_ABORT;
2697 xfs_trans_cancel(tp, cancel_flags);
2698 XFS_QM_DQRELE(mp, udqp);
2699 XFS_QM_DQRELE(mp, gdqp);
2701 if (unlock_dp_on_error)
2702 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2712 xfs_mount_t *mp = ip->i_mount;
2715 if (XFS_FORCED_SHUTDOWN(mp))
2716 return XFS_ERROR(EIO);
2719 * Bypass inodes which have already been cleaned by
2720 * the inode flush clustering code inside xfs_iflush
2722 if (xfs_inode_clean(ip))
2726 * We make this non-blocking if the inode is contended,
2727 * return EAGAIN to indicate to the caller that they
2728 * did not succeed. This prevents the flush path from
2729 * blocking on inodes inside another operation right
2730 * now, they get caught later by xfs_sync.
2732 if (flags & FLUSH_SYNC) {
2733 xfs_ilock(ip, XFS_ILOCK_SHARED);
2735 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
2736 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
2737 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2744 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
2745 : XFS_IFLUSH_ASYNC_NOBLOCK);
2746 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2758 xfs_mount_t *mp = ip->i_mount;
2762 if (!capable(CAP_SYS_ADMIN))
2763 return XFS_ERROR(EPERM);
2765 if (XFS_FORCED_SHUTDOWN(mp))
2766 return XFS_ERROR(EIO);
2768 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2769 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2771 xfs_trans_cancel(tp, 0);
2774 xfs_ilock(ip, XFS_ILOCK_EXCL);
2775 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2777 ip->i_d.di_dmevmask = evmask;
2778 ip->i_d.di_dmstate = state;
2780 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2782 error = xfs_trans_commit(tp, 0);
2792 xfs_itrace_entry(ip);
2794 ASSERT(!VN_MAPPED(VFS_I(ip)));
2796 /* bad inode, get out here ASAP */
2797 if (VN_BAD(VFS_I(ip))) {
2804 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
2807 * Make sure the atime in the XFS inode is correct before freeing the
2810 xfs_synchronize_atime(ip);
2813 * If we have nothing to flush with this inode then complete the
2814 * teardown now, otherwise break the link between the xfs inode and the
2815 * linux inode and clean up the xfs inode later. This avoids flushing
2816 * the inode to disk during the delete operation itself.
2818 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2819 * first to ensure that xfs_iunpin() will never see an xfs inode
2820 * that has a linux inode being reclaimed. Synchronisation is provided
2821 * by the i_flags_lock.
2823 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2824 xfs_ilock(ip, XFS_ILOCK_EXCL);
2826 xfs_iflags_set(ip, XFS_IRECLAIMABLE);
2827 return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
2829 xfs_inode_set_reclaim_tag(ip);
2834 * xfs_alloc_file_space()
2835 * This routine allocates disk space for the given file.
2837 * If alloc_type == 0, this request is for an ALLOCSP type
2838 * request which will change the file size. In this case, no
2839 * DMAPI event will be generated by the call. A TRUNCATE event
2840 * will be generated later by xfs_setattr.
2842 * If alloc_type != 0, this request is for a RESVSP type
2843 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2844 * lower block boundary byte address is less than the file's
2853 xfs_alloc_file_space(
2860 xfs_mount_t *mp = ip->i_mount;
2862 xfs_filblks_t allocated_fsb;
2863 xfs_filblks_t allocatesize_fsb;
2864 xfs_extlen_t extsz, temp;
2865 xfs_fileoff_t startoffset_fsb;
2866 xfs_fsblock_t firstfsb;
2872 xfs_bmbt_irec_t imaps[1], *imapp;
2873 xfs_bmap_free_t free_list;
2874 uint qblocks, resblks, resrtextents;
2878 xfs_itrace_entry(ip);
2880 if (XFS_FORCED_SHUTDOWN(mp))
2881 return XFS_ERROR(EIO);
2883 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2887 return XFS_ERROR(EINVAL);
2889 rt = XFS_IS_REALTIME_INODE(ip);
2890 extsz = xfs_get_extsz_hint(ip);
2895 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
2896 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2897 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2899 /* Generate a DMAPI event if needed. */
2900 if (alloc_type != 0 && offset < ip->i_size &&
2901 (attr_flags & XFS_ATTR_DMI) == 0 &&
2902 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2903 xfs_off_t end_dmi_offset;
2905 end_dmi_offset = offset+len;
2906 if (end_dmi_offset > ip->i_size)
2907 end_dmi_offset = ip->i_size;
2908 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2909 end_dmi_offset - offset, 0, NULL);
2915 * Allocate file space until done or until there is an error
2918 while (allocatesize_fsb && !error) {
2922 * Determine space reservations for data/realtime.
2924 if (unlikely(extsz)) {
2925 s = startoffset_fsb;
2928 e = startoffset_fsb + allocatesize_fsb;
2929 if ((temp = do_mod(startoffset_fsb, extsz)))
2931 if ((temp = do_mod(e, extsz)))
2935 e = allocatesize_fsb;
2939 resrtextents = qblocks = (uint)(e - s);
2940 resrtextents /= mp->m_sb.sb_rextsize;
2941 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2942 quota_flag = XFS_QMOPT_RES_RTBLKS;
2945 resblks = qblocks = \
2946 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2947 quota_flag = XFS_QMOPT_RES_REGBLKS;
2951 * Allocate and setup the transaction.
2953 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2954 error = xfs_trans_reserve(tp, resblks,
2955 XFS_WRITE_LOG_RES(mp), resrtextents,
2956 XFS_TRANS_PERM_LOG_RES,
2957 XFS_WRITE_LOG_COUNT);
2959 * Check for running out of space
2963 * Free the transaction structure.
2965 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2966 xfs_trans_cancel(tp, 0);
2969 xfs_ilock(ip, XFS_ILOCK_EXCL);
2970 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
2971 qblocks, 0, quota_flag);
2975 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2976 xfs_trans_ihold(tp, ip);
2979 * Issue the xfs_bmapi() call to allocate the blocks
2981 XFS_BMAP_INIT(&free_list, &firstfsb);
2982 error = xfs_bmapi(tp, ip, startoffset_fsb,
2983 allocatesize_fsb, bmapi_flag,
2984 &firstfsb, 0, imapp, &nimaps,
2991 * Complete the transaction
2993 error = xfs_bmap_finish(&tp, &free_list, &committed);
2998 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2999 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3004 allocated_fsb = imapp->br_blockcount;
3007 error = XFS_ERROR(ENOSPC);
3011 startoffset_fsb += allocated_fsb;
3012 allocatesize_fsb -= allocated_fsb;
3015 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
3016 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
3017 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
3020 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3022 goto retry; /* Maybe DMAPI app. has made space */
3023 /* else fall through with error from XFS_SEND_DATA */
3028 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3029 xfs_bmap_cancel(&free_list);
3030 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3032 error1: /* Just cancel transaction */
3033 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3034 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3035 goto dmapi_enospc_check;
3039 * Zero file bytes between startoff and endoff inclusive.
3040 * The iolock is held exclusive and no blocks are buffered.
3042 * This function is used by xfs_free_file_space() to zero
3043 * partial blocks when the range to free is not block aligned.
3044 * When unreserving space with boundaries that are not block
3045 * aligned we round up the start and round down the end
3046 * boundaries and then use this function to zero the parts of
3047 * the blocks that got dropped during the rounding.
3050 xfs_zero_remaining_bytes(
3055 xfs_bmbt_irec_t imap;
3056 xfs_fileoff_t offset_fsb;
3057 xfs_off_t lastoffset;
3060 xfs_mount_t *mp = ip->i_mount;
3065 * Avoid doing I/O beyond eof - it's not necessary
3066 * since nothing can read beyond eof. The space will
3067 * be zeroed when the file is extended anyway.
3069 if (startoff >= ip->i_size)
3072 if (endoff > ip->i_size)
3073 endoff = ip->i_size;
3075 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
3076 XFS_IS_REALTIME_INODE(ip) ?
3077 mp->m_rtdev_targp : mp->m_ddev_targp);
3079 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3080 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3082 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
3083 NULL, 0, &imap, &nimap, NULL, NULL);
3084 if (error || nimap < 1)
3086 ASSERT(imap.br_blockcount >= 1);
3087 ASSERT(imap.br_startoff == offset_fsb);
3088 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3089 if (lastoffset > endoff)
3090 lastoffset = endoff;
3091 if (imap.br_startblock == HOLESTARTBLOCK)
3093 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3094 if (imap.br_state == XFS_EXT_UNWRITTEN)
3097 XFS_BUF_UNWRITE(bp);
3099 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3101 error = xfs_iowait(bp);
3103 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3104 mp, bp, XFS_BUF_ADDR(bp));
3107 memset(XFS_BUF_PTR(bp) +
3108 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3109 0, lastoffset - offset + 1);
3114 error = xfs_iowait(bp);
3116 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3117 mp, bp, XFS_BUF_ADDR(bp));
3126 * xfs_free_file_space()
3127 * This routine frees disk space for the given file.
3129 * This routine is only called by xfs_change_file_space
3130 * for an UNRESVSP type call.
3138 xfs_free_file_space(
3146 xfs_off_t end_dmi_offset;
3147 xfs_fileoff_t endoffset_fsb;
3149 xfs_fsblock_t firstfsb;
3150 xfs_bmap_free_t free_list;
3151 xfs_bmbt_irec_t imap;
3159 xfs_fileoff_t startoffset_fsb;
3161 int need_iolock = 1;
3165 xfs_itrace_entry(ip);
3167 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3171 if (len <= 0) /* if nothing being freed */
3173 rt = XFS_IS_REALTIME_INODE(ip);
3174 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3175 end_dmi_offset = offset + len;
3176 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3178 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
3179 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3180 if (end_dmi_offset > ip->i_size)
3181 end_dmi_offset = ip->i_size;
3182 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
3183 offset, end_dmi_offset - offset,
3184 AT_DELAY_FLAG(attr_flags), NULL);
3189 if (attr_flags & XFS_ATTR_NOLOCK)
3192 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3193 vn_iowait(ip); /* wait for the completion of any pending DIOs */
3196 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
3197 ioffset = offset & ~(rounding - 1);
3199 if (VN_CACHED(VFS_I(ip)) != 0) {
3200 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3201 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
3203 goto out_unlock_iolock;
3207 * Need to zero the stuff we're not freeing, on disk.
3208 * If its a realtime file & can't use unwritten extents then we
3209 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3210 * will take care of it for us.
3212 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
3214 error = xfs_bmapi(NULL, ip, startoffset_fsb,
3215 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3217 goto out_unlock_iolock;
3218 ASSERT(nimap == 0 || nimap == 1);
3219 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3222 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3223 block = imap.br_startblock;
3224 mod = do_div(block, mp->m_sb.sb_rextsize);
3226 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3229 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
3230 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3232 goto out_unlock_iolock;
3233 ASSERT(nimap == 0 || nimap == 1);
3234 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3235 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3237 if (mod && (mod != mp->m_sb.sb_rextsize))
3238 endoffset_fsb -= mod;
3241 if ((done = (endoffset_fsb <= startoffset_fsb)))
3243 * One contiguous piece to clear
3245 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3248 * Some full blocks, possibly two pieces to clear
3250 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3251 error = xfs_zero_remaining_bytes(ip, offset,
3252 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3254 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3255 error = xfs_zero_remaining_bytes(ip,
3256 XFS_FSB_TO_B(mp, endoffset_fsb),
3261 * free file space until done or until there is an error
3263 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3264 while (!error && !done) {
3267 * allocate and setup the transaction. Allow this
3268 * transaction to dip into the reserve blocks to ensure
3269 * the freeing of the space succeeds at ENOSPC.
3271 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3272 tp->t_flags |= XFS_TRANS_RESERVE;
3273 error = xfs_trans_reserve(tp,
3275 XFS_WRITE_LOG_RES(mp),
3277 XFS_TRANS_PERM_LOG_RES,
3278 XFS_WRITE_LOG_COUNT);
3281 * check for running out of space
3285 * Free the transaction structure.
3287 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3288 xfs_trans_cancel(tp, 0);
3291 xfs_ilock(ip, XFS_ILOCK_EXCL);
3292 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
3293 ip->i_udquot, ip->i_gdquot, resblks, 0,
3294 XFS_QMOPT_RES_REGBLKS);
3298 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3299 xfs_trans_ihold(tp, ip);
3302 * issue the bunmapi() call to free the blocks
3304 XFS_BMAP_INIT(&free_list, &firstfsb);
3305 error = xfs_bunmapi(tp, ip, startoffset_fsb,
3306 endoffset_fsb - startoffset_fsb,
3307 0, 2, &firstfsb, &free_list, NULL, &done);
3313 * complete the transaction
3315 error = xfs_bmap_finish(&tp, &free_list, &committed);
3320 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3321 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3326 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3330 xfs_bmap_cancel(&free_list);
3332 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3333 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3339 * xfs_change_file_space()
3340 * This routine allocates or frees disk space for the given file.
3341 * The user specified parameters are checked for alignment and size
3350 xfs_change_file_space(
3357 xfs_mount_t *mp = ip->i_mount;
3362 xfs_off_t startoffset;
3367 xfs_itrace_entry(ip);
3369 if (!S_ISREG(ip->i_d.di_mode))
3370 return XFS_ERROR(EINVAL);
3372 switch (bf->l_whence) {
3373 case 0: /*SEEK_SET*/
3375 case 1: /*SEEK_CUR*/
3376 bf->l_start += offset;
3378 case 2: /*SEEK_END*/
3379 bf->l_start += ip->i_size;
3382 return XFS_ERROR(EINVAL);
3385 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3387 if ( (bf->l_start < 0)
3388 || (bf->l_start > XFS_MAXIOFFSET(mp))
3389 || (bf->l_start + llen < 0)
3390 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3391 return XFS_ERROR(EINVAL);
3395 startoffset = bf->l_start;
3399 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3401 * These calls do NOT zero the data space allocated to the file,
3402 * nor do they change the file size.
3404 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3406 * These calls cause the new file data to be zeroed and the file
3407 * size to be changed.
3409 setprealloc = clrprealloc = 0;
3412 case XFS_IOC_RESVSP:
3413 case XFS_IOC_RESVSP64:
3414 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3421 case XFS_IOC_UNRESVSP:
3422 case XFS_IOC_UNRESVSP64:
3423 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3428 case XFS_IOC_ALLOCSP:
3429 case XFS_IOC_ALLOCSP64:
3430 case XFS_IOC_FREESP:
3431 case XFS_IOC_FREESP64:
3432 if (startoffset > fsize) {
3433 error = xfs_alloc_file_space(ip, fsize,
3434 startoffset - fsize, 0, attr_flags);
3439 iattr.ia_valid = ATTR_SIZE;
3440 iattr.ia_size = startoffset;
3442 error = xfs_setattr(ip, &iattr, attr_flags);
3452 return XFS_ERROR(EINVAL);
3456 * update the inode timestamp, mode, and prealloc flag bits
3458 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3460 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3463 xfs_trans_cancel(tp, 0);
3467 xfs_ilock(ip, XFS_ILOCK_EXCL);
3469 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3470 xfs_trans_ihold(tp, ip);
3472 if ((attr_flags & XFS_ATTR_DMI) == 0) {
3473 ip->i_d.di_mode &= ~S_ISUID;
3476 * Note that we don't have to worry about mandatory
3477 * file locking being disabled here because we only
3478 * clear the S_ISGID bit if the Group execute bit is
3479 * on, but if it was on then mandatory locking wouldn't
3480 * have been enabled.
3482 if (ip->i_d.di_mode & S_IXGRP)
3483 ip->i_d.di_mode &= ~S_ISGID;
3485 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3488 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3489 else if (clrprealloc)
3490 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3492 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3493 xfs_trans_set_sync(tp);
3495 error = xfs_trans_commit(tp, 0);
3497 xfs_iunlock(ip, XFS_ILOCK_EXCL);