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);
85 xfs_mount_t *mp = ip->i_mount;
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(credp) == 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.
235 * If the system was configured with the "restricted_chown"
236 * option, the owner is not permitted to give away the file,
237 * and can change the group id only to a group of which he
238 * or she is a member.
240 if (mask & (ATTR_UID|ATTR_GID)) {
242 * These IDs could have changed since we last looked at them.
243 * But, we're assured that if the ownership did change
244 * while we didn't have the inode locked, inode's dquot(s)
245 * would have changed also.
247 iuid = ip->i_d.di_uid;
248 igid = ip->i_d.di_gid;
249 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
250 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
253 * CAP_CHOWN overrides the following restrictions:
255 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
256 * shall override the restriction that a process cannot
257 * change the user ID of a file it owns and the restriction
258 * that the group ID supplied to the chown() function
259 * shall be equal to either the group ID or one of the
260 * supplementary group IDs of the calling process.
262 if (restricted_chown &&
263 (iuid != uid || (igid != gid &&
264 !in_group_p((gid_t)gid))) &&
265 !capable(CAP_CHOWN)) {
266 code = XFS_ERROR(EPERM);
270 * Do a quota reservation only if uid/gid is actually
273 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
274 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
276 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
277 capable(CAP_FOWNER) ?
278 XFS_QMOPT_FORCE_RES : 0);
279 if (code) /* out of quota */
285 * Truncate file. Must have write permission and not be a directory.
287 if (mask & ATTR_SIZE) {
288 /* Short circuit the truncate case for zero length files */
289 if (iattr->ia_size == 0 &&
290 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
291 xfs_iunlock(ip, XFS_ILOCK_EXCL);
292 lock_flags &= ~XFS_ILOCK_EXCL;
293 if (mask & ATTR_CTIME)
294 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
299 if (S_ISDIR(ip->i_d.di_mode)) {
300 code = XFS_ERROR(EISDIR);
302 } else if (!S_ISREG(ip->i_d.di_mode)) {
303 code = XFS_ERROR(EINVAL);
307 * Make sure that the dquots are attached to the inode.
309 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
314 * Change file access or modified times.
316 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
318 if ((mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)) &&
319 !capable(CAP_FOWNER)) {
320 code = XFS_ERROR(EPERM);
327 * Now we can make the changes. Before we join the inode
328 * to the transaction, if ATTR_SIZE is set then take care of
329 * the part of the truncation that must be done without the
330 * inode lock. This needs to be done before joining the inode
331 * to the transaction, because the inode cannot be unlocked
332 * once it is a part of the transaction.
334 if (mask & ATTR_SIZE) {
336 if (iattr->ia_size > ip->i_size) {
338 * Do the first part of growing a file: zero any data
339 * in the last block that is beyond the old EOF. We
340 * need to do this before the inode is joined to the
341 * transaction to modify the i_size.
343 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
345 xfs_iunlock(ip, XFS_ILOCK_EXCL);
348 * We are going to log the inode size change in this
349 * transaction so any previous writes that are beyond the on
350 * disk EOF and the new EOF that have not been written out need
351 * to be written here. If we do not write the data out, we
352 * expose ourselves to the null files problem.
354 * Only flush from the on disk size to the smaller of the in
355 * memory file size or the new size as that's the range we
356 * really care about here and prevents waiting for other data
357 * not within the range we care about here.
360 ip->i_size != ip->i_d.di_size &&
361 iattr->ia_size > ip->i_d.di_size) {
362 code = xfs_flush_pages(ip,
363 ip->i_d.di_size, iattr->ia_size,
364 XFS_B_ASYNC, FI_NONE);
367 /* wait for all I/O to complete */
371 code = xfs_itruncate_data(ip, iattr->ia_size);
374 lock_flags &= ~XFS_ILOCK_EXCL;
375 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
378 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
379 if ((code = xfs_trans_reserve(tp, 0,
380 XFS_ITRUNCATE_LOG_RES(mp), 0,
381 XFS_TRANS_PERM_LOG_RES,
382 XFS_ITRUNCATE_LOG_COUNT))) {
383 xfs_trans_cancel(tp, 0);
385 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
388 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
389 xfs_ilock(ip, XFS_ILOCK_EXCL);
393 xfs_trans_ijoin(tp, ip, lock_flags);
394 xfs_trans_ihold(tp, ip);
398 * Truncate file. Must have write permission and not be a directory.
400 if (mask & ATTR_SIZE) {
402 * Only change the c/mtime if we are changing the size
403 * or we are explicitly asked to change it. This handles
404 * the semantic difference between truncate() and ftruncate()
405 * as implemented in the VFS.
407 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
408 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
410 if (iattr->ia_size > ip->i_size) {
411 ip->i_d.di_size = iattr->ia_size;
412 ip->i_size = iattr->ia_size;
413 if (!(flags & XFS_ATTR_DMI))
414 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
415 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
416 } else if (iattr->ia_size <= ip->i_size ||
417 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
419 * signal a sync transaction unless
420 * we're truncating an already unlinked
421 * file on a wsync filesystem
423 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
425 ((ip->i_d.di_nlink != 0 ||
426 !(mp->m_flags & XFS_MOUNT_WSYNC))
431 * Truncated "down", so we're removing references
432 * to old data here - if we now delay flushing for
433 * a long time, we expose ourselves unduly to the
434 * notorious NULL files problem. So, we mark this
435 * vnode and flush it when the file is closed, and
436 * do not wait the usual (long) time for writeout.
438 xfs_iflags_set(ip, XFS_ITRUNCATED);
443 * Change file access modes.
445 if (mask & ATTR_MODE) {
446 ip->i_d.di_mode &= S_IFMT;
447 ip->i_d.di_mode |= iattr->ia_mode & ~S_IFMT;
449 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
450 timeflags |= XFS_ICHGTIME_CHG;
454 * Change file ownership. Must be the owner or privileged.
455 * If the system was configured with the "restricted_chown"
456 * option, the owner is not permitted to give away the file,
457 * and can change the group id only to a group of which he
458 * or she is a member.
460 if (mask & (ATTR_UID|ATTR_GID)) {
462 * CAP_FSETID overrides the following restrictions:
464 * The set-user-ID and set-group-ID bits of a file will be
465 * cleared upon successful return from chown()
467 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
468 !capable(CAP_FSETID)) {
469 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
473 * Change the ownerships and register quota modifications
474 * in the transaction.
477 if (XFS_IS_UQUOTA_ON(mp)) {
478 ASSERT(mask & ATTR_UID);
480 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
481 &ip->i_udquot, udqp);
483 ip->i_d.di_uid = uid;
486 if (XFS_IS_GQUOTA_ON(mp)) {
487 ASSERT(!XFS_IS_PQUOTA_ON(mp));
488 ASSERT(mask & ATTR_GID);
490 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
491 &ip->i_gdquot, gdqp);
493 ip->i_d.di_gid = gid;
496 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
497 timeflags |= XFS_ICHGTIME_CHG;
502 * Change file access or modified times.
504 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
505 if (mask & ATTR_ATIME) {
506 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
507 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
508 ip->i_update_core = 1;
509 timeflags &= ~XFS_ICHGTIME_ACC;
511 if (mask & ATTR_MTIME) {
512 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
513 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
514 timeflags &= ~XFS_ICHGTIME_MOD;
515 timeflags |= XFS_ICHGTIME_CHG;
517 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
518 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
522 * Change file inode change time only if ATTR_CTIME set
523 * AND we have been called by a DMI function.
526 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
527 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
528 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
529 ip->i_update_core = 1;
530 timeflags &= ~XFS_ICHGTIME_CHG;
534 * Send out timestamp changes that need to be set to the
535 * current time. Not done when called by a DMI function.
537 if (timeflags && !(flags & XFS_ATTR_DMI))
538 xfs_ichgtime(ip, timeflags);
540 XFS_STATS_INC(xs_ig_attrchg);
543 * If this is a synchronous mount, make sure that the
544 * transaction goes to disk before returning to the user.
545 * This is slightly sub-optimal in that truncates require
546 * two sync transactions instead of one for wsync filesystems.
547 * One for the truncate and one for the timestamps since we
548 * don't want to change the timestamps unless we're sure the
549 * truncate worked. Truncates are less than 1% of the laddis
550 * mix so this probably isn't worth the trouble to optimize.
554 if (mp->m_flags & XFS_MOUNT_WSYNC)
555 xfs_trans_set_sync(tp);
557 code = xfs_trans_commit(tp, commit_flags);
560 xfs_iunlock(ip, lock_flags);
563 * Release any dquot(s) the inode had kept before chown.
565 XFS_QM_DQRELE(mp, olddquot1);
566 XFS_QM_DQRELE(mp, olddquot2);
567 XFS_QM_DQRELE(mp, udqp);
568 XFS_QM_DQRELE(mp, gdqp);
574 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
575 !(flags & XFS_ATTR_DMI)) {
576 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
577 NULL, DM_RIGHT_NULL, NULL, NULL,
578 0, 0, AT_DELAY_FLAG(flags));
583 commit_flags |= XFS_TRANS_ABORT;
586 XFS_QM_DQRELE(mp, udqp);
587 XFS_QM_DQRELE(mp, gdqp);
589 xfs_trans_cancel(tp, commit_flags);
591 if (lock_flags != 0) {
592 xfs_iunlock(ip, lock_flags);
598 * The maximum pathlen is 1024 bytes. Since the minimum file system
599 * blocksize is 512 bytes, we can get a max of 2 extents back from
602 #define SYMLINK_MAPS 2
609 xfs_mount_t *mp = ip->i_mount;
610 int pathlen = ip->i_d.di_size;
611 int nmaps = SYMLINK_MAPS;
612 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
619 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
620 mval, &nmaps, NULL, NULL);
624 for (n = 0; n < nmaps; n++) {
625 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
626 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
628 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
629 error = XFS_BUF_GETERROR(bp);
631 xfs_ioerror_alert("xfs_readlink",
632 ip->i_mount, bp, XFS_BUF_ADDR(bp));
636 if (pathlen < byte_cnt)
640 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
644 link[ip->i_d.di_size] = '\0';
656 xfs_mount_t *mp = ip->i_mount;
660 xfs_itrace_entry(ip);
662 if (XFS_FORCED_SHUTDOWN(mp))
663 return XFS_ERROR(EIO);
665 xfs_ilock(ip, XFS_ILOCK_SHARED);
667 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
668 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
670 pathlen = ip->i_d.di_size;
674 if (ip->i_df.if_flags & XFS_IFINLINE) {
675 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
676 link[pathlen] = '\0';
678 error = xfs_readlink_bmap(ip, link);
682 xfs_iunlock(ip, XFS_ILOCK_SHARED);
689 * This is called to sync the inode and its data out to disk. We need to hold
690 * the I/O lock while flushing the data, and the inode lock while flushing the
691 * inode. The inode lock CANNOT be held while flushing the data, so acquire
692 * after we're done with that.
700 int log_flushed = 0, changed = 1;
702 xfs_itrace_entry(ip);
704 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
705 return XFS_ERROR(EIO);
707 /* capture size updates in I/O completion before writing the inode. */
708 error = filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
710 return XFS_ERROR(error);
713 * We always need to make sure that the required inode state is safe on
714 * disk. The vnode might be clean but we still might need to force the
715 * log because of committed transactions that haven't hit the disk yet.
716 * Likewise, there could be unflushed non-transactional changes to the
717 * inode core that have to go to disk and this requires us to issue
718 * a synchronous transaction to capture these changes correctly.
720 * This code relies on the assumption that if the update_* fields
721 * of the inode are clear and the inode is unpinned then it is clean
722 * and no action is required.
724 xfs_ilock(ip, XFS_ILOCK_SHARED);
726 if (!(ip->i_update_size || ip->i_update_core)) {
728 * Timestamps/size haven't changed since last inode flush or
729 * inode transaction commit. That means either nothing got
730 * written or a transaction committed which caught the updates.
731 * If the latter happened and the transaction hasn't hit the
732 * disk yet, the inode will be still be pinned. If it is,
736 xfs_iunlock(ip, XFS_ILOCK_SHARED);
738 if (xfs_ipincount(ip)) {
739 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
740 XFS_LOG_FORCE | XFS_LOG_SYNC,
744 * If the inode is not pinned and nothing has changed
745 * we don't need to flush the cache.
751 * Kick off a transaction to log the inode core to get the
752 * updates. The sync transaction will also force the log.
754 xfs_iunlock(ip, XFS_ILOCK_SHARED);
755 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
756 error = xfs_trans_reserve(tp, 0,
757 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
759 xfs_trans_cancel(tp, 0);
762 xfs_ilock(ip, XFS_ILOCK_EXCL);
765 * Note - it's possible that we might have pushed ourselves out
766 * of the way during trans_reserve which would flush the inode.
767 * But there's no guarantee that the inode buffer has actually
768 * gone out yet (it's delwri). Plus the buffer could be pinned
769 * anyway if it's part of an inode in another recent
770 * transaction. So we play it safe and fire off the
771 * transaction anyway.
773 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
774 xfs_trans_ihold(tp, ip);
775 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
776 xfs_trans_set_sync(tp);
777 error = _xfs_trans_commit(tp, 0, &log_flushed);
779 xfs_iunlock(ip, XFS_ILOCK_EXCL);
782 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
784 * If the log write didn't issue an ordered tag we need
785 * to flush the disk cache for the data device now.
788 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
791 * If this inode is on the RT dev we need to flush that
794 if (XFS_IS_REALTIME_INODE(ip))
795 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
802 * This is called by xfs_inactive to free any blocks beyond eof
803 * when the link count isn't zero and by xfs_dm_punch_hole() when
804 * punching a hole to EOF.
814 xfs_fileoff_t end_fsb;
815 xfs_fileoff_t last_fsb;
816 xfs_filblks_t map_len;
818 xfs_bmbt_irec_t imap;
819 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
822 * Figure out if there are any blocks beyond the end
823 * of the file. If not, then there is nothing to do.
825 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
826 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
827 map_len = last_fsb - end_fsb;
832 xfs_ilock(ip, XFS_ILOCK_SHARED);
833 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
834 NULL, 0, &imap, &nimaps, NULL, NULL);
835 xfs_iunlock(ip, XFS_ILOCK_SHARED);
837 if (!error && (nimaps != 0) &&
838 (imap.br_startblock != HOLESTARTBLOCK ||
839 ip->i_delayed_blks)) {
841 * Attach the dquots to the inode up front.
843 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
847 * There are blocks after the end of file.
848 * Free them up now by truncating the file to
851 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
854 * Do the xfs_itruncate_start() call before
855 * reserving any log space because
856 * itruncate_start will call into the buffer
858 * do that within a transaction.
861 xfs_ilock(ip, XFS_IOLOCK_EXCL);
862 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
865 xfs_trans_cancel(tp, 0);
867 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
871 error = xfs_trans_reserve(tp, 0,
872 XFS_ITRUNCATE_LOG_RES(mp),
873 0, XFS_TRANS_PERM_LOG_RES,
874 XFS_ITRUNCATE_LOG_COUNT);
876 ASSERT(XFS_FORCED_SHUTDOWN(mp));
877 xfs_trans_cancel(tp, 0);
878 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
882 xfs_ilock(ip, XFS_ILOCK_EXCL);
883 xfs_trans_ijoin(tp, ip,
886 xfs_trans_ihold(tp, ip);
888 error = xfs_itruncate_finish(&tp, ip,
893 * If we get an error at this point we
894 * simply don't bother truncating the file.
898 (XFS_TRANS_RELEASE_LOG_RES |
901 error = xfs_trans_commit(tp,
902 XFS_TRANS_RELEASE_LOG_RES);
904 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
911 * Free a symlink that has blocks associated with it.
914 xfs_inactive_symlink_rmt(
922 xfs_fsblock_t first_block;
923 xfs_bmap_free_t free_list;
926 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
934 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
936 * We're freeing a symlink that has some
937 * blocks allocated to it. Free the
938 * blocks here. We know that we've got
939 * either 1 or 2 extents and that we can
940 * free them all in one bunmapi call.
942 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
943 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
944 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
945 ASSERT(XFS_FORCED_SHUTDOWN(mp));
946 xfs_trans_cancel(tp, 0);
951 * Lock the inode, fix the size, and join it to the transaction.
952 * Hold it so in the normal path, we still have it locked for
953 * the second transaction. In the error paths we need it
954 * held so the cancel won't rele it, see below.
956 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
957 size = (int)ip->i_d.di_size;
959 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
960 xfs_trans_ihold(tp, ip);
961 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
963 * Find the block(s) so we can inval and unmap them.
966 XFS_BMAP_INIT(&free_list, &first_block);
967 nmaps = ARRAY_SIZE(mval);
968 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
969 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
973 * Invalidate the block(s).
975 for (i = 0; i < nmaps; i++) {
976 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
977 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
978 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
979 xfs_trans_binval(tp, bp);
982 * Unmap the dead block(s) to the free_list.
984 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
985 &first_block, &free_list, NULL, &done)))
989 * Commit the first transaction. This logs the EFI and the inode.
991 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
994 * The transaction must have been committed, since there were
995 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
996 * The new tp has the extent freeing and EFDs.
1000 * The first xact was committed, so add the inode to the new one.
1001 * Mark it dirty so it will be logged and moved forward in the log as
1002 * part of every commit.
1004 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1005 xfs_trans_ihold(tp, ip);
1006 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1008 * Get a new, empty transaction to return to our caller.
1010 ntp = xfs_trans_dup(tp);
1012 * Commit the transaction containing extent freeing and EFDs.
1013 * If we get an error on the commit here or on the reserve below,
1014 * we need to unlock the inode since the new transaction doesn't
1015 * have the inode attached.
1017 error = xfs_trans_commit(tp, 0);
1020 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1024 * Remove the memory for extent descriptions (just bookkeeping).
1026 if (ip->i_df.if_bytes)
1027 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1028 ASSERT(ip->i_df.if_bytes == 0);
1030 * Put an itruncate log reservation in the new transaction
1033 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1034 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1035 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1039 * Return with the inode locked but not joined to the transaction.
1045 xfs_bmap_cancel(&free_list);
1048 * Have to come here with the inode locked and either
1049 * (held and in the transaction) or (not in the transaction).
1050 * If the inode isn't held then cancel would iput it, but
1051 * that's wrong since this is inactive and the vnode ref
1052 * count is 0 already.
1053 * Cancel won't do anything to the inode if held, but it still
1054 * needs to be locked until the cancel is done, if it was
1055 * joined to the transaction.
1057 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1058 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1065 xfs_inactive_symlink_local(
1071 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1073 * We're freeing a symlink which fit into
1074 * the inode. Just free the memory used
1075 * to hold the old symlink.
1077 error = xfs_trans_reserve(*tpp, 0,
1078 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1079 0, XFS_TRANS_PERM_LOG_RES,
1080 XFS_ITRUNCATE_LOG_COUNT);
1083 xfs_trans_cancel(*tpp, 0);
1087 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1090 * Zero length symlinks _can_ exist.
1092 if (ip->i_df.if_bytes > 0) {
1093 xfs_idata_realloc(ip,
1094 -(ip->i_df.if_bytes),
1096 ASSERT(ip->i_df.if_bytes == 0);
1110 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1113 ASSERT(ip->i_d.di_forkoff != 0);
1114 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1115 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1119 error = xfs_attr_inactive(ip);
1123 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1124 error = xfs_trans_reserve(tp, 0,
1125 XFS_IFREE_LOG_RES(mp),
1126 0, XFS_TRANS_PERM_LOG_RES,
1127 XFS_INACTIVE_LOG_COUNT);
1131 xfs_ilock(ip, XFS_ILOCK_EXCL);
1132 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1133 xfs_trans_ihold(tp, ip);
1134 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1136 ASSERT(ip->i_d.di_anextents == 0);
1142 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1143 xfs_trans_cancel(tp, 0);
1146 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1154 bhv_vnode_t *vp = XFS_ITOV(ip);
1155 xfs_mount_t *mp = ip->i_mount;
1158 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1161 /* If this is a read-only mount, don't do this (would generate I/O) */
1162 if (mp->m_flags & XFS_MOUNT_RDONLY)
1165 if (!XFS_FORCED_SHUTDOWN(mp)) {
1169 * If we are using filestreams, and we have an unlinked
1170 * file that we are processing the last close on, then nothing
1171 * will be able to reopen and write to this file. Purge this
1172 * inode from the filestreams cache so that it doesn't delay
1173 * teardown of the inode.
1175 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1176 xfs_filestream_deassociate(ip);
1179 * If we previously truncated this file and removed old data
1180 * in the process, we want to initiate "early" writeout on
1181 * the last close. This is an attempt to combat the notorious
1182 * NULL files problem which is particularly noticable from a
1183 * truncate down, buffered (re-)write (delalloc), followed by
1184 * a crash. What we are effectively doing here is
1185 * significantly reducing the time window where we'd otherwise
1186 * be exposed to that problem.
1188 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1189 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
1190 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1193 if (ip->i_d.di_nlink != 0) {
1194 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1195 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1196 ip->i_delayed_blks > 0)) &&
1197 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1198 (!(ip->i_d.di_flags &
1199 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1200 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1212 * This is called when the vnode reference count for the vnode
1213 * goes to zero. If the file has been unlinked, then it must
1214 * now be truncated. Also, we clear all of the read-ahead state
1215 * kept for the inode here since the file is now closed.
1221 bhv_vnode_t *vp = XFS_ITOV(ip);
1222 xfs_bmap_free_t free_list;
1223 xfs_fsblock_t first_block;
1230 xfs_itrace_entry(ip);
1233 * If the inode is already free, then there can be nothing
1236 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1237 ASSERT(ip->i_df.if_real_bytes == 0);
1238 ASSERT(ip->i_df.if_broot_bytes == 0);
1239 return VN_INACTIVE_CACHE;
1243 * Only do a truncate if it's a regular file with
1244 * some actual space in it. It's OK to look at the
1245 * inode's fields without the lock because we're the
1246 * only one with a reference to the inode.
1248 truncate = ((ip->i_d.di_nlink == 0) &&
1249 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1250 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1251 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1255 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1256 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1260 /* If this is a read-only mount, don't do this (would generate I/O) */
1261 if (mp->m_flags & XFS_MOUNT_RDONLY)
1264 if (ip->i_d.di_nlink != 0) {
1265 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1266 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1267 ip->i_delayed_blks > 0)) &&
1268 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1269 (!(ip->i_d.di_flags &
1270 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1271 (ip->i_delayed_blks != 0)))) {
1272 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1274 return VN_INACTIVE_CACHE;
1279 ASSERT(ip->i_d.di_nlink == 0);
1281 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1282 return VN_INACTIVE_CACHE;
1284 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1287 * Do the xfs_itruncate_start() call before
1288 * reserving any log space because itruncate_start
1289 * will call into the buffer cache and we can't
1290 * do that within a transaction.
1292 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1294 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1296 xfs_trans_cancel(tp, 0);
1297 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1298 return VN_INACTIVE_CACHE;
1301 error = xfs_trans_reserve(tp, 0,
1302 XFS_ITRUNCATE_LOG_RES(mp),
1303 0, XFS_TRANS_PERM_LOG_RES,
1304 XFS_ITRUNCATE_LOG_COUNT);
1306 /* Don't call itruncate_cleanup */
1307 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1308 xfs_trans_cancel(tp, 0);
1309 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1310 return VN_INACTIVE_CACHE;
1313 xfs_ilock(ip, XFS_ILOCK_EXCL);
1314 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1315 xfs_trans_ihold(tp, ip);
1318 * normally, we have to run xfs_itruncate_finish sync.
1319 * But if filesystem is wsync and we're in the inactive
1320 * path, then we know that nlink == 0, and that the
1321 * xaction that made nlink == 0 is permanently committed
1322 * since xfs_remove runs as a synchronous transaction.
1324 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1325 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1328 xfs_trans_cancel(tp,
1329 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1330 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1331 return VN_INACTIVE_CACHE;
1333 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1336 * If we get an error while cleaning up a
1337 * symlink we bail out.
1339 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1340 xfs_inactive_symlink_rmt(ip, &tp) :
1341 xfs_inactive_symlink_local(ip, &tp);
1345 return VN_INACTIVE_CACHE;
1348 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1349 xfs_trans_ihold(tp, ip);
1351 error = xfs_trans_reserve(tp, 0,
1352 XFS_IFREE_LOG_RES(mp),
1353 0, XFS_TRANS_PERM_LOG_RES,
1354 XFS_INACTIVE_LOG_COUNT);
1356 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1357 xfs_trans_cancel(tp, 0);
1358 return VN_INACTIVE_CACHE;
1361 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1362 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1363 xfs_trans_ihold(tp, ip);
1367 * If there are attributes associated with the file
1368 * then blow them away now. The code calls a routine
1369 * that recursively deconstructs the attribute fork.
1370 * We need to just commit the current transaction
1371 * because we can't use it for xfs_attr_inactive().
1373 if (ip->i_d.di_anextents > 0) {
1374 error = xfs_inactive_attrs(ip, &tp);
1376 * If we got an error, the transaction is already
1377 * cancelled, and the inode is unlocked. Just get out.
1380 return VN_INACTIVE_CACHE;
1381 } else if (ip->i_afp) {
1382 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1388 XFS_BMAP_INIT(&free_list, &first_block);
1389 error = xfs_ifree(tp, ip, &free_list);
1392 * If we fail to free the inode, shut down. The cancel
1393 * might do that, we need to make sure. Otherwise the
1394 * inode might be lost for a long time or forever.
1396 if (!XFS_FORCED_SHUTDOWN(mp)) {
1398 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1399 error, mp->m_fsname);
1400 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1402 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1405 * Credit the quota account(s). The inode is gone.
1407 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1410 * Just ignore errors at this point. There is nothing we can
1411 * do except to try to keep going. Make sure it's not a silent
1414 error = xfs_bmap_finish(&tp, &free_list, &committed);
1416 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1417 "xfs_bmap_finish() returned error %d", error);
1418 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1420 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1421 "xfs_trans_commit() returned error %d", error);
1424 * Release the dquots held by inode, if any.
1426 XFS_QM_DQDETACH(mp, ip);
1428 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1431 return VN_INACTIVE_CACHE;
1435 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1436 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1437 * ci_name->name will point to a the actual name (caller must free) or
1438 * will be set to NULL if an exact match is found.
1443 struct xfs_name *name,
1445 struct xfs_name *ci_name)
1451 xfs_itrace_entry(dp);
1453 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1454 return XFS_ERROR(EIO);
1456 lock_mode = xfs_ilock_map_shared(dp);
1457 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1458 xfs_iunlock_map_shared(dp, lock_mode);
1463 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1467 xfs_itrace_ref(*ipp);
1472 kmem_free(ci_name->name);
1481 struct xfs_name *name,
1487 xfs_mount_t *mp = dp->i_mount;
1491 xfs_bmap_free_t free_list;
1492 xfs_fsblock_t first_block;
1493 boolean_t unlock_dp_on_error = B_FALSE;
1494 int dm_event_sent = 0;
1498 struct xfs_dquot *udqp, *gdqp;
1502 xfs_itrace_entry(dp);
1504 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1505 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1506 dp, DM_RIGHT_NULL, NULL,
1507 DM_RIGHT_NULL, name->name, NULL,
1515 if (XFS_FORCED_SHUTDOWN(mp))
1516 return XFS_ERROR(EIO);
1518 /* Return through std_return after this point. */
1521 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1522 prid = dp->i_d.di_projid;
1524 prid = (xfs_prid_t)dfltprid;
1527 * Make sure that we have allocated dquot(s) on disk.
1529 error = XFS_QM_DQVOPALLOC(mp, dp,
1530 current_fsuid(credp), current_fsgid(credp), prid,
1531 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1537 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1538 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1539 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1541 * Initially assume that the file does not exist and
1542 * reserve the resources for that case. If that is not
1543 * the case we'll drop the one we have and get a more
1544 * appropriate transaction later.
1546 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1547 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1548 if (error == ENOSPC) {
1550 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1551 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1558 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1559 unlock_dp_on_error = B_TRUE;
1561 XFS_BMAP_INIT(&free_list, &first_block);
1566 * Reserve disk quota and the inode.
1568 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1572 error = xfs_dir_canenter(tp, dp, name, resblks);
1575 error = xfs_dir_ialloc(&tp, dp, mode, 1,
1576 rdev, credp, prid, resblks > 0,
1579 if (error == ENOSPC)
1586 * At this point, we've gotten a newly allocated inode.
1587 * It is locked (and joined to the transaction).
1590 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1593 * Now we join the directory inode to the transaction. We do not do it
1594 * earlier because xfs_dir_ialloc might commit the previous transaction
1595 * (and release all the locks). An error from here on will result in
1596 * the transaction cancel unlocking dp so don't do it explicitly in the
1600 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1601 unlock_dp_on_error = B_FALSE;
1603 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1604 &first_block, &free_list, resblks ?
1605 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1607 ASSERT(error != ENOSPC);
1610 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1611 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1614 * If this is a synchronous mount, make sure that the
1615 * create transaction goes to disk before returning to
1618 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1619 xfs_trans_set_sync(tp);
1625 * Attach the dquot(s) to the inodes and modify them incore.
1626 * These ids of the inode couldn't have changed since the new
1627 * inode has been locked ever since it was created.
1629 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1632 * xfs_trans_commit normally decrements the vnode ref count
1633 * when it unlocks the inode. Since we want to return the
1634 * vnode to the caller, we bump the vnode ref count now.
1638 error = xfs_bmap_finish(&tp, &free_list, &committed);
1640 xfs_bmap_cancel(&free_list);
1644 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1651 XFS_QM_DQRELE(mp, udqp);
1652 XFS_QM_DQRELE(mp, gdqp);
1656 /* Fallthrough to std_return with error = 0 */
1659 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
1660 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1661 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
1664 DM_RIGHT_NULL, name->name, NULL,
1670 cancel_flags |= XFS_TRANS_ABORT;
1675 xfs_trans_cancel(tp, cancel_flags);
1677 XFS_QM_DQRELE(mp, udqp);
1678 XFS_QM_DQRELE(mp, gdqp);
1680 if (unlock_dp_on_error)
1681 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1687 * Wait until after the current transaction is aborted to
1688 * release the inode. This prevents recursive transactions
1689 * and deadlocks from xfs_inactive.
1691 cancel_flags |= XFS_TRANS_ABORT;
1692 xfs_trans_cancel(tp, cancel_flags);
1695 XFS_QM_DQRELE(mp, udqp);
1696 XFS_QM_DQRELE(mp, gdqp);
1703 * Some counters to see if (and how often) we are hitting some deadlock
1704 * prevention code paths.
1708 int xfs_rm_lock_delays;
1709 int xfs_rm_attempts;
1713 * The following routine will lock the inodes associated with the
1714 * directory and the named entry in the directory. The locks are
1715 * acquired in increasing inode number.
1717 * If the entry is "..", then only the directory is locked. The
1718 * vnode ref count will still include that from the .. entry in
1721 * There is a deadlock we need to worry about. If the locked directory is
1722 * in the AIL, it might be blocking up the log. The next inode we lock
1723 * could be already locked by another thread waiting for log space (e.g
1724 * a permanent log reservation with a long running transaction (see
1725 * xfs_itruncate_finish)). To solve this, we must check if the directory
1726 * is in the ail and use lock_nowait. If we can't lock, we need to
1727 * drop the inode lock on the directory and try again. xfs_iunlock will
1728 * potentially push the tail if we were holding up the log.
1731 xfs_lock_dir_and_entry(
1733 xfs_inode_t *ip) /* inode of entry 'name' */
1737 xfs_inode_t *ips[2];
1746 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1753 * We want to lock in increasing inum. Since we've already
1754 * acquired the lock on the directory, we may need to release
1755 * if if the inum of the entry turns out to be less.
1757 if (e_inum > dp->i_ino) {
1759 * We are already in the right order, so just
1760 * lock on the inode of the entry.
1761 * We need to use nowait if dp is in the AIL.
1764 lp = (xfs_log_item_t *)dp->i_itemp;
1765 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1766 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1773 * Unlock dp and try again.
1774 * xfs_iunlock will try to push the tail
1775 * if the inode is in the AIL.
1778 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1780 if ((attempts % 5) == 0) {
1781 delay(1); /* Don't just spin the CPU */
1783 xfs_rm_lock_delays++;
1789 xfs_ilock(ip, XFS_ILOCK_EXCL);
1791 } else if (e_inum < dp->i_ino) {
1792 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1796 xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL);
1798 /* else e_inum == dp->i_ino */
1799 /* This can happen if we're asked to lock /x/..
1800 * the entry is "..", which is also the parent directory.
1808 int xfs_small_retries;
1809 int xfs_middle_retries;
1810 int xfs_lots_retries;
1811 int xfs_lock_delays;
1815 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1819 xfs_lock_inumorder(int lock_mode, int subclass)
1821 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1822 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1823 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1824 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1830 * The following routine will lock n inodes in exclusive mode.
1831 * We assume the caller calls us with the inodes in i_ino order.
1833 * We need to detect deadlock where an inode that we lock
1834 * is in the AIL and we start waiting for another inode that is locked
1835 * by a thread in a long running transaction (such as truncate). This can
1836 * result in deadlock since the long running trans might need to wait
1837 * for the inode we just locked in order to push the tail and free space
1846 int attempts = 0, i, j, try_lock;
1849 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1855 for (; i < inodes; i++) {
1858 if (i && (ips[i] == ips[i-1])) /* Already locked */
1862 * If try_lock is not set yet, make sure all locked inodes
1863 * are not in the AIL.
1864 * If any are, set try_lock to be used later.
1868 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1869 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1870 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1877 * If any of the previous locks we have locked is in the AIL,
1878 * we must TRY to get the second and subsequent locks. If
1879 * we can't get any, we must release all we have
1884 /* try_lock must be 0 if i is 0. */
1886 * try_lock means we have an inode locked
1887 * that is in the AIL.
1890 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1894 * Unlock all previous guys and try again.
1895 * xfs_iunlock will try to push the tail
1896 * if the inode is in the AIL.
1899 for(j = i - 1; j >= 0; j--) {
1902 * Check to see if we've already
1903 * unlocked this one.
1904 * Not the first one going back,
1905 * and the inode ptr is the same.
1907 if ((j != (i - 1)) && ips[j] ==
1911 xfs_iunlock(ips[j], lock_mode);
1914 if ((attempts % 5) == 0) {
1915 delay(1); /* Don't just spin the CPU */
1925 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1931 if (attempts < 5) xfs_small_retries++;
1932 else if (attempts < 100) xfs_middle_retries++;
1933 else xfs_lots_retries++;
1943 struct xfs_name *name,
1946 xfs_mount_t *mp = dp->i_mount;
1947 xfs_trans_t *tp = NULL;
1948 int is_dir = S_ISDIR(ip->i_d.di_mode);
1950 xfs_bmap_free_t free_list;
1951 xfs_fsblock_t first_block;
1958 xfs_itrace_entry(dp);
1959 xfs_itrace_entry(ip);
1961 if (XFS_FORCED_SHUTDOWN(mp))
1962 return XFS_ERROR(EIO);
1964 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
1965 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1966 NULL, DM_RIGHT_NULL, name->name, NULL,
1967 ip->i_d.di_mode, 0, 0);
1972 error = XFS_QM_DQATTACH(mp, dp, 0);
1976 error = XFS_QM_DQATTACH(mp, ip, 0);
1981 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1982 log_count = XFS_DEFAULT_LOG_COUNT;
1984 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1985 log_count = XFS_REMOVE_LOG_COUNT;
1987 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1990 * We try to get the real space reservation first,
1991 * allowing for directory btree deletion(s) implying
1992 * possible bmap insert(s). If we can't get the space
1993 * reservation then we use 0 instead, and avoid the bmap
1994 * btree insert(s) in the directory code by, if the bmap
1995 * insert tries to happen, instead trimming the LAST
1996 * block from the directory.
1998 resblks = XFS_REMOVE_SPACE_RES(mp);
1999 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2000 XFS_TRANS_PERM_LOG_RES, log_count);
2001 if (error == ENOSPC) {
2003 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2004 XFS_TRANS_PERM_LOG_RES, log_count);
2007 ASSERT(error != ENOSPC);
2009 goto out_trans_cancel;
2012 error = xfs_lock_dir_and_entry(dp, ip);
2014 goto out_trans_cancel;
2017 * At this point, we've gotten both the directory and the entry
2021 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2024 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2027 * If we're removing a directory perform some additional validation.
2030 ASSERT(ip->i_d.di_nlink >= 2);
2031 if (ip->i_d.di_nlink != 2) {
2032 error = XFS_ERROR(ENOTEMPTY);
2033 goto out_trans_cancel;
2035 if (!xfs_dir_isempty(ip)) {
2036 error = XFS_ERROR(ENOTEMPTY);
2037 goto out_trans_cancel;
2042 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2044 XFS_BMAP_INIT(&free_list, &first_block);
2045 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
2046 &first_block, &free_list, resblks);
2048 ASSERT(error != ENOENT);
2049 goto out_bmap_cancel;
2051 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2054 * Bump the in memory generation count on the parent
2055 * directory so that other can know that it has changed.
2058 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2062 * Drop the link from ip's "..".
2064 error = xfs_droplink(tp, dp);
2066 goto out_bmap_cancel;
2069 * Drop the link from dp to ip.
2071 error = xfs_droplink(tp, ip);
2073 goto out_bmap_cancel;
2076 * When removing a non-directory we need to log the parent
2077 * inode here for the i_gen update. For a directory this is
2078 * done implicitly by the xfs_droplink call for the ".." entry.
2080 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2084 * Drop the "." link from ip to self.
2086 error = xfs_droplink(tp, ip);
2088 goto out_bmap_cancel;
2091 * Determine if this is the last link while
2092 * we are in the transaction.
2094 link_zero = (ip->i_d.di_nlink == 0);
2097 * If this is a synchronous mount, make sure that the
2098 * remove transaction goes to disk before returning to
2101 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2102 xfs_trans_set_sync(tp);
2104 error = xfs_bmap_finish(&tp, &free_list, &committed);
2106 goto out_bmap_cancel;
2108 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2113 * If we are using filestreams, kill the stream association.
2114 * If the file is still open it may get a new one but that
2115 * will get killed on last close in xfs_close() so we don't
2116 * have to worry about that.
2118 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2119 xfs_filestream_deassociate(ip);
2121 xfs_itrace_exit(ip);
2122 xfs_itrace_exit(dp);
2125 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2126 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2127 NULL, DM_RIGHT_NULL, name->name, NULL,
2128 ip->i_d.di_mode, error, 0);
2134 xfs_bmap_cancel(&free_list);
2135 cancel_flags |= XFS_TRANS_ABORT;
2137 xfs_trans_cancel(tp, cancel_flags);
2145 struct xfs_name *target_name)
2147 xfs_mount_t *mp = tdp->i_mount;
2149 xfs_inode_t *ips[2];
2151 xfs_bmap_free_t free_list;
2152 xfs_fsblock_t first_block;
2157 xfs_itrace_entry(tdp);
2158 xfs_itrace_entry(sip);
2160 ASSERT(!S_ISDIR(sip->i_d.di_mode));
2162 if (XFS_FORCED_SHUTDOWN(mp))
2163 return XFS_ERROR(EIO);
2165 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2166 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2169 target_name->name, NULL, 0, 0, 0);
2174 /* Return through std_return after this point. */
2176 error = XFS_QM_DQATTACH(mp, sip, 0);
2177 if (!error && sip != tdp)
2178 error = XFS_QM_DQATTACH(mp, tdp, 0);
2182 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2183 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2184 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
2185 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2186 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2187 if (error == ENOSPC) {
2189 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2190 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2197 if (sip->i_ino < tdp->i_ino) {
2205 xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL);
2208 * Increment vnode ref counts since xfs_trans_commit &
2209 * xfs_trans_cancel will both unlock the inodes and
2210 * decrement the associated ref counts.
2214 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2215 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2218 * If the source has too many links, we can't make any more to it.
2220 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2221 error = XFS_ERROR(EMLINK);
2226 * If we are using project inheritance, we only allow hard link
2227 * creation in our tree when the project IDs are the same; else
2228 * the tree quota mechanism could be circumvented.
2230 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2231 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2232 error = XFS_ERROR(EXDEV);
2236 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2240 XFS_BMAP_INIT(&free_list, &first_block);
2242 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2243 &first_block, &free_list, resblks);
2246 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2248 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2250 error = xfs_bumplink(tp, sip);
2255 * If this is a synchronous mount, make sure that the
2256 * link transaction goes to disk before returning to
2259 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2260 xfs_trans_set_sync(tp);
2263 error = xfs_bmap_finish (&tp, &free_list, &committed);
2265 xfs_bmap_cancel(&free_list);
2269 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2273 /* Fall through to std_return with error = 0. */
2275 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2276 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2279 target_name->name, NULL, 0, error, 0);
2284 cancel_flags |= XFS_TRANS_ABORT;
2288 xfs_trans_cancel(tp, cancel_flags);
2296 struct xfs_name *dir_name,
2301 xfs_mount_t *mp = dp->i_mount;
2302 xfs_inode_t *cdp; /* inode of created dir */
2307 xfs_bmap_free_t free_list;
2308 xfs_fsblock_t first_block;
2309 boolean_t unlock_dp_on_error = B_FALSE;
2310 boolean_t created = B_FALSE;
2311 int dm_event_sent = 0;
2313 struct xfs_dquot *udqp, *gdqp;
2316 if (XFS_FORCED_SHUTDOWN(mp))
2317 return XFS_ERROR(EIO);
2321 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
2322 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2323 dp, DM_RIGHT_NULL, NULL,
2324 DM_RIGHT_NULL, dir_name->name, NULL,
2331 /* Return through std_return after this point. */
2333 xfs_itrace_entry(dp);
2337 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2338 prid = dp->i_d.di_projid;
2340 prid = (xfs_prid_t)dfltprid;
2343 * Make sure that we have allocated dquot(s) on disk.
2345 error = XFS_QM_DQVOPALLOC(mp, dp,
2346 current_fsuid(credp), current_fsgid(credp), prid,
2347 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2351 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2352 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2353 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
2354 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2355 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2356 if (error == ENOSPC) {
2358 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2359 XFS_TRANS_PERM_LOG_RES,
2360 XFS_MKDIR_LOG_COUNT);
2367 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2368 unlock_dp_on_error = B_TRUE;
2371 * Check for directory link count overflow.
2373 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2374 error = XFS_ERROR(EMLINK);
2379 * Reserve disk quota and the inode.
2381 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2385 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2389 * create the directory inode.
2391 error = xfs_dir_ialloc(&tp, dp, mode, 2,
2392 0, credp, prid, resblks > 0,
2395 if (error == ENOSPC)
2399 xfs_itrace_ref(cdp);
2402 * Now we add the directory inode to the transaction.
2403 * We waited until now since xfs_dir_ialloc might start
2404 * a new transaction. Had we joined the transaction
2405 * earlier, the locks might have gotten released. An error
2406 * from here on will result in the transaction cancel
2407 * unlocking dp so don't do it explicitly in the error path.
2410 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2411 unlock_dp_on_error = B_FALSE;
2413 XFS_BMAP_INIT(&free_list, &first_block);
2415 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2416 &first_block, &free_list, resblks ?
2417 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2419 ASSERT(error != ENOSPC);
2422 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2425 * Bump the in memory version number of the parent directory
2426 * so that other processes accessing it will recognize that
2427 * the directory has changed.
2431 error = xfs_dir_init(tp, cdp, dp);
2436 error = xfs_bumplink(tp, dp);
2446 * Attach the dquots to the new inode and modify the icount incore.
2448 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2451 * If this is a synchronous mount, make sure that the
2452 * mkdir transaction goes to disk before returning to
2455 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2456 xfs_trans_set_sync(tp);
2459 error = xfs_bmap_finish(&tp, &free_list, &committed);
2465 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2466 XFS_QM_DQRELE(mp, udqp);
2467 XFS_QM_DQRELE(mp, gdqp);
2472 /* Fall through to std_return with error = 0 or errno from
2473 * xfs_trans_commit. */
2476 if ((created || (error != 0 && dm_event_sent != 0)) &&
2477 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2478 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2480 created ? cdp : NULL,
2482 dir_name->name, NULL,
2489 xfs_bmap_cancel(&free_list);
2491 cancel_flags |= XFS_TRANS_ABORT;
2493 xfs_trans_cancel(tp, cancel_flags);
2494 XFS_QM_DQRELE(mp, udqp);
2495 XFS_QM_DQRELE(mp, gdqp);
2497 if (unlock_dp_on_error)
2498 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2506 struct xfs_name *link_name,
2507 const char *target_path,
2512 xfs_mount_t *mp = dp->i_mount;
2517 xfs_bmap_free_t free_list;
2518 xfs_fsblock_t first_block;
2519 boolean_t unlock_dp_on_error = B_FALSE;
2522 xfs_fileoff_t first_fsb;
2523 xfs_filblks_t fs_blocks;
2525 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2527 const char *cur_chunk;
2532 struct xfs_dquot *udqp, *gdqp;
2540 xfs_itrace_entry(dp);
2542 if (XFS_FORCED_SHUTDOWN(mp))
2543 return XFS_ERROR(EIO);
2546 * Check component lengths of the target path name.
2548 pathlen = strlen(target_path);
2549 if (pathlen >= MAXPATHLEN) /* total string too long */
2550 return XFS_ERROR(ENAMETOOLONG);
2552 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
2553 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
2554 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2555 link_name->name, target_path, 0, 0, 0);
2560 /* Return through std_return after this point. */
2563 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2564 prid = dp->i_d.di_projid;
2566 prid = (xfs_prid_t)dfltprid;
2569 * Make sure that we have allocated dquot(s) on disk.
2571 error = XFS_QM_DQVOPALLOC(mp, dp,
2572 current_fsuid(credp), current_fsgid(credp), prid,
2573 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2577 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2578 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2580 * The symlink will fit into the inode data fork?
2581 * There can't be any attributes so we get the whole variable part.
2583 if (pathlen <= XFS_LITINO(mp))
2586 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2587 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2588 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2589 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2590 if (error == ENOSPC && fs_blocks == 0) {
2592 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2593 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2600 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2601 unlock_dp_on_error = B_TRUE;
2604 * Check whether the directory allows new symlinks or not.
2606 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2607 error = XFS_ERROR(EPERM);
2612 * Reserve disk quota : blocks and inode.
2614 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2619 * Check for ability to enter directory entry, if no space reserved.
2621 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2625 * Initialize the bmap freelist prior to calling either
2626 * bmapi or the directory create code.
2628 XFS_BMAP_INIT(&free_list, &first_block);
2631 * Allocate an inode for the symlink.
2633 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
2634 1, 0, credp, prid, resblks > 0, &ip, NULL);
2636 if (error == ENOSPC)
2643 * An error after we've joined dp to the transaction will result in the
2644 * transaction cancel unlocking dp so don't do it explicitly in the
2648 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2649 unlock_dp_on_error = B_FALSE;
2652 * Also attach the dquot(s) to it, if applicable.
2654 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2657 resblks -= XFS_IALLOC_SPACE_RES(mp);
2659 * If the symlink will fit into the inode, write it inline.
2661 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2662 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2663 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2664 ip->i_d.di_size = pathlen;
2667 * The inode was initially created in extent format.
2669 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2670 ip->i_df.if_flags |= XFS_IFINLINE;
2672 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2673 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2677 nmaps = SYMLINK_MAPS;
2679 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2680 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2681 &first_block, resblks, mval, &nmaps,
2688 resblks -= fs_blocks;
2689 ip->i_d.di_size = pathlen;
2690 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2692 cur_chunk = target_path;
2693 for (n = 0; n < nmaps; n++) {
2694 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2695 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2696 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2697 BTOBB(byte_cnt), 0);
2698 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2699 if (pathlen < byte_cnt) {
2702 pathlen -= byte_cnt;
2704 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2705 cur_chunk += byte_cnt;
2707 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2712 * Create the directory entry for the symlink.
2714 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2715 &first_block, &free_list, resblks);
2718 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2719 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2722 * Bump the in memory version number of the parent directory
2723 * so that other processes accessing it will recognize that
2724 * the directory has changed.
2729 * If this is a synchronous mount, make sure that the
2730 * symlink transaction goes to disk before returning to
2733 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2734 xfs_trans_set_sync(tp);
2738 * xfs_trans_commit normally decrements the vnode ref count
2739 * when it unlocks the inode. Since we want to return the
2740 * vnode to the caller, we bump the vnode ref count now.
2744 error = xfs_bmap_finish(&tp, &free_list, &committed);
2748 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2749 XFS_QM_DQRELE(mp, udqp);
2750 XFS_QM_DQRELE(mp, gdqp);
2752 /* Fall through to std_return with error = 0 or errno from
2753 * xfs_trans_commit */
2755 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
2756 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
2759 DM_RIGHT_NULL, link_name->name,
2760 target_path, 0, error, 0);
2770 xfs_bmap_cancel(&free_list);
2771 cancel_flags |= XFS_TRANS_ABORT;
2773 xfs_trans_cancel(tp, cancel_flags);
2774 XFS_QM_DQRELE(mp, udqp);
2775 XFS_QM_DQRELE(mp, gdqp);
2777 if (unlock_dp_on_error)
2778 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2788 xfs_mount_t *mp = ip->i_mount;
2791 if (XFS_FORCED_SHUTDOWN(mp))
2792 return XFS_ERROR(EIO);
2795 * Bypass inodes which have already been cleaned by
2796 * the inode flush clustering code inside xfs_iflush
2798 if (xfs_inode_clean(ip))
2802 * We make this non-blocking if the inode is contended,
2803 * return EAGAIN to indicate to the caller that they
2804 * did not succeed. This prevents the flush path from
2805 * blocking on inodes inside another operation right
2806 * now, they get caught later by xfs_sync.
2808 if (flags & FLUSH_SYNC) {
2809 xfs_ilock(ip, XFS_ILOCK_SHARED);
2811 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
2812 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
2813 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2820 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
2821 : XFS_IFLUSH_ASYNC_NOBLOCK);
2822 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2834 xfs_mount_t *mp = ip->i_mount;
2838 if (!capable(CAP_SYS_ADMIN))
2839 return XFS_ERROR(EPERM);
2841 if (XFS_FORCED_SHUTDOWN(mp))
2842 return XFS_ERROR(EIO);
2844 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2845 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2847 xfs_trans_cancel(tp, 0);
2850 xfs_ilock(ip, XFS_ILOCK_EXCL);
2851 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2853 ip->i_d.di_dmevmask = evmask;
2854 ip->i_d.di_dmstate = state;
2856 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2858 error = xfs_trans_commit(tp, 0);
2867 bhv_vnode_t *vp = XFS_ITOV(ip);
2869 xfs_itrace_entry(ip);
2871 ASSERT(!VN_MAPPED(vp));
2873 /* bad inode, get out here ASAP */
2881 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
2884 * Make sure the atime in the XFS inode is correct before freeing the
2887 xfs_synchronize_atime(ip);
2890 * If we have nothing to flush with this inode then complete the
2891 * teardown now, otherwise break the link between the xfs inode and the
2892 * linux inode and clean up the xfs inode later. This avoids flushing
2893 * the inode to disk during the delete operation itself.
2895 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2896 * first to ensure that xfs_iunpin() will never see an xfs inode
2897 * that has a linux inode being reclaimed. Synchronisation is provided
2898 * by the i_flags_lock.
2900 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2901 xfs_ilock(ip, XFS_ILOCK_EXCL);
2903 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
2905 xfs_mount_t *mp = ip->i_mount;
2907 /* Protect sync and unpin from us */
2908 XFS_MOUNT_ILOCK(mp);
2909 spin_lock(&ip->i_flags_lock);
2910 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
2911 vn_to_inode(vp)->i_private = NULL;
2913 spin_unlock(&ip->i_flags_lock);
2914 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
2915 XFS_MOUNT_IUNLOCK(mp);
2926 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
2927 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
2929 if (vp && VN_BAD(vp))
2932 /* The hash lock here protects a thread in xfs_iget_core from
2933 * racing with us on linking the inode back with a vnode.
2934 * Once we have the XFS_IRECLAIM flag set it will not touch
2937 write_lock(&pag->pag_ici_lock);
2938 spin_lock(&ip->i_flags_lock);
2939 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
2940 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
2941 spin_unlock(&ip->i_flags_lock);
2942 write_unlock(&pag->pag_ici_lock);
2945 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2949 __xfs_iflags_set(ip, XFS_IRECLAIM);
2950 spin_unlock(&ip->i_flags_lock);
2951 write_unlock(&pag->pag_ici_lock);
2952 xfs_put_perag(ip->i_mount, pag);
2955 * If the inode is still dirty, then flush it out. If the inode
2956 * is not in the AIL, then it will be OK to flush it delwri as
2957 * long as xfs_iflush() does not keep any references to the inode.
2958 * We leave that decision up to xfs_iflush() since it has the
2959 * knowledge of whether it's OK to simply do a delwri flush of
2960 * the inode or whether we need to wait until the inode is
2961 * pulled from the AIL.
2962 * We get the flush lock regardless, though, just to make sure
2963 * we don't free it while it is being flushed.
2966 xfs_ilock(ip, XFS_ILOCK_EXCL);
2971 * In the case of a forced shutdown we rely on xfs_iflush() to
2972 * wait for the inode to be unpinned before returning an error.
2974 if (xfs_iflush(ip, sync_mode) == 0) {
2975 /* synchronize with xfs_iflush_done */
2980 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2988 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
2991 xfs_inode_t *ip, *n;
2996 XFS_MOUNT_ILOCK(mp);
2997 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
2999 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3001 if (xfs_ipincount(ip) ||
3002 !xfs_iflock_nowait(ip)) {
3003 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3007 XFS_MOUNT_IUNLOCK(mp);
3008 if (xfs_finish_reclaim(ip, noblock,
3009 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3018 XFS_MOUNT_IUNLOCK(mp);
3023 * xfs_alloc_file_space()
3024 * This routine allocates disk space for the given file.
3026 * If alloc_type == 0, this request is for an ALLOCSP type
3027 * request which will change the file size. In this case, no
3028 * DMAPI event will be generated by the call. A TRUNCATE event
3029 * will be generated later by xfs_setattr.
3031 * If alloc_type != 0, this request is for a RESVSP type
3032 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3033 * lower block boundary byte address is less than the file's
3042 xfs_alloc_file_space(
3049 xfs_mount_t *mp = ip->i_mount;
3051 xfs_filblks_t allocated_fsb;
3052 xfs_filblks_t allocatesize_fsb;
3053 xfs_extlen_t extsz, temp;
3054 xfs_fileoff_t startoffset_fsb;
3055 xfs_fsblock_t firstfsb;
3061 xfs_bmbt_irec_t imaps[1], *imapp;
3062 xfs_bmap_free_t free_list;
3063 uint qblocks, resblks, resrtextents;
3067 xfs_itrace_entry(ip);
3069 if (XFS_FORCED_SHUTDOWN(mp))
3070 return XFS_ERROR(EIO);
3072 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3076 return XFS_ERROR(EINVAL);
3078 rt = XFS_IS_REALTIME_INODE(ip);
3079 extsz = xfs_get_extsz_hint(ip);
3084 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
3085 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3086 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3088 /* Generate a DMAPI event if needed. */
3089 if (alloc_type != 0 && offset < ip->i_size &&
3090 (attr_flags & XFS_ATTR_DMI) == 0 &&
3091 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3092 xfs_off_t end_dmi_offset;
3094 end_dmi_offset = offset+len;
3095 if (end_dmi_offset > ip->i_size)
3096 end_dmi_offset = ip->i_size;
3097 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
3098 end_dmi_offset - offset, 0, NULL);
3104 * Allocate file space until done or until there is an error
3107 while (allocatesize_fsb && !error) {
3111 * Determine space reservations for data/realtime.
3113 if (unlikely(extsz)) {
3114 s = startoffset_fsb;
3117 e = startoffset_fsb + allocatesize_fsb;
3118 if ((temp = do_mod(startoffset_fsb, extsz)))
3120 if ((temp = do_mod(e, extsz)))
3124 e = allocatesize_fsb;
3128 resrtextents = qblocks = (uint)(e - s);
3129 resrtextents /= mp->m_sb.sb_rextsize;
3130 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3131 quota_flag = XFS_QMOPT_RES_RTBLKS;
3134 resblks = qblocks = \
3135 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3136 quota_flag = XFS_QMOPT_RES_REGBLKS;
3140 * Allocate and setup the transaction.
3142 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3143 error = xfs_trans_reserve(tp, resblks,
3144 XFS_WRITE_LOG_RES(mp), resrtextents,
3145 XFS_TRANS_PERM_LOG_RES,
3146 XFS_WRITE_LOG_COUNT);
3148 * Check for running out of space
3152 * Free the transaction structure.
3154 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3155 xfs_trans_cancel(tp, 0);
3158 xfs_ilock(ip, XFS_ILOCK_EXCL);
3159 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3160 qblocks, 0, quota_flag);
3164 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3165 xfs_trans_ihold(tp, ip);
3168 * Issue the xfs_bmapi() call to allocate the blocks
3170 XFS_BMAP_INIT(&free_list, &firstfsb);
3171 error = xfs_bmapi(tp, ip, startoffset_fsb,
3172 allocatesize_fsb, bmapi_flag,
3173 &firstfsb, 0, imapp, &nimaps,
3180 * Complete the transaction
3182 error = xfs_bmap_finish(&tp, &free_list, &committed);
3187 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3188 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3193 allocated_fsb = imapp->br_blockcount;
3196 error = XFS_ERROR(ENOSPC);
3200 startoffset_fsb += allocated_fsb;
3201 allocatesize_fsb -= allocated_fsb;
3204 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
3205 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
3206 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
3209 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3211 goto retry; /* Maybe DMAPI app. has made space */
3212 /* else fall through with error from XFS_SEND_DATA */
3217 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3218 xfs_bmap_cancel(&free_list);
3219 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3221 error1: /* Just cancel transaction */
3222 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3223 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3224 goto dmapi_enospc_check;
3228 * Zero file bytes between startoff and endoff inclusive.
3229 * The iolock is held exclusive and no blocks are buffered.
3232 xfs_zero_remaining_bytes(
3237 xfs_bmbt_irec_t imap;
3238 xfs_fileoff_t offset_fsb;
3239 xfs_off_t lastoffset;
3242 xfs_mount_t *mp = ip->i_mount;
3246 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
3247 XFS_IS_REALTIME_INODE(ip) ?
3248 mp->m_rtdev_targp : mp->m_ddev_targp);
3250 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3251 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3253 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
3254 NULL, 0, &imap, &nimap, NULL, NULL);
3255 if (error || nimap < 1)
3257 ASSERT(imap.br_blockcount >= 1);
3258 ASSERT(imap.br_startoff == offset_fsb);
3259 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3260 if (lastoffset > endoff)
3261 lastoffset = endoff;
3262 if (imap.br_startblock == HOLESTARTBLOCK)
3264 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3265 if (imap.br_state == XFS_EXT_UNWRITTEN)
3268 XFS_BUF_UNWRITE(bp);
3270 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3272 error = xfs_iowait(bp);
3274 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3275 mp, bp, XFS_BUF_ADDR(bp));
3278 memset(XFS_BUF_PTR(bp) +
3279 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3280 0, lastoffset - offset + 1);
3285 error = xfs_iowait(bp);
3287 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3288 mp, bp, XFS_BUF_ADDR(bp));
3297 * xfs_free_file_space()
3298 * This routine frees disk space for the given file.
3300 * This routine is only called by xfs_change_file_space
3301 * for an UNRESVSP type call.
3309 xfs_free_file_space(
3318 xfs_off_t end_dmi_offset;
3319 xfs_fileoff_t endoffset_fsb;
3321 xfs_fsblock_t firstfsb;
3322 xfs_bmap_free_t free_list;
3323 xfs_bmbt_irec_t imap;
3331 xfs_fileoff_t startoffset_fsb;
3333 int need_iolock = 1;
3338 xfs_itrace_entry(ip);
3340 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3344 if (len <= 0) /* if nothing being freed */
3346 rt = XFS_IS_REALTIME_INODE(ip);
3347 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3348 end_dmi_offset = offset + len;
3349 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3351 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
3352 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3353 if (end_dmi_offset > ip->i_size)
3354 end_dmi_offset = ip->i_size;
3355 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
3356 offset, end_dmi_offset - offset,
3357 AT_DELAY_FLAG(attr_flags), NULL);
3362 if (attr_flags & XFS_ATTR_NOLOCK)
3365 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3366 vn_iowait(ip); /* wait for the completion of any pending DIOs */
3369 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
3370 ioffset = offset & ~(rounding - 1);
3372 if (VN_CACHED(vp) != 0) {
3373 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3374 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
3376 goto out_unlock_iolock;
3380 * Need to zero the stuff we're not freeing, on disk.
3381 * If its a realtime file & can't use unwritten extents then we
3382 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3383 * will take care of it for us.
3385 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
3387 error = xfs_bmapi(NULL, ip, startoffset_fsb,
3388 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3390 goto out_unlock_iolock;
3391 ASSERT(nimap == 0 || nimap == 1);
3392 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3395 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3396 block = imap.br_startblock;
3397 mod = do_div(block, mp->m_sb.sb_rextsize);
3399 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3402 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
3403 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3405 goto out_unlock_iolock;
3406 ASSERT(nimap == 0 || nimap == 1);
3407 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3408 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3410 if (mod && (mod != mp->m_sb.sb_rextsize))
3411 endoffset_fsb -= mod;
3414 if ((done = (endoffset_fsb <= startoffset_fsb)))
3416 * One contiguous piece to clear
3418 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3421 * Some full blocks, possibly two pieces to clear
3423 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3424 error = xfs_zero_remaining_bytes(ip, offset,
3425 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3427 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3428 error = xfs_zero_remaining_bytes(ip,
3429 XFS_FSB_TO_B(mp, endoffset_fsb),
3434 * free file space until done or until there is an error
3436 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3437 while (!error && !done) {
3440 * allocate and setup the transaction. Allow this
3441 * transaction to dip into the reserve blocks to ensure
3442 * the freeing of the space succeeds at ENOSPC.
3444 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3445 tp->t_flags |= XFS_TRANS_RESERVE;
3446 error = xfs_trans_reserve(tp,
3448 XFS_WRITE_LOG_RES(mp),
3450 XFS_TRANS_PERM_LOG_RES,
3451 XFS_WRITE_LOG_COUNT);
3454 * check for running out of space
3458 * Free the transaction structure.
3460 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3461 xfs_trans_cancel(tp, 0);
3464 xfs_ilock(ip, XFS_ILOCK_EXCL);
3465 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
3466 ip->i_udquot, ip->i_gdquot, resblks, 0,
3467 XFS_QMOPT_RES_REGBLKS);
3471 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3472 xfs_trans_ihold(tp, ip);
3475 * issue the bunmapi() call to free the blocks
3477 XFS_BMAP_INIT(&free_list, &firstfsb);
3478 error = xfs_bunmapi(tp, ip, startoffset_fsb,
3479 endoffset_fsb - startoffset_fsb,
3480 0, 2, &firstfsb, &free_list, NULL, &done);
3486 * complete the transaction
3488 error = xfs_bmap_finish(&tp, &free_list, &committed);
3493 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3494 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3499 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3503 xfs_bmap_cancel(&free_list);
3505 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3506 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3512 * xfs_change_file_space()
3513 * This routine allocates or frees disk space for the given file.
3514 * The user specified parameters are checked for alignment and size
3523 xfs_change_file_space(
3531 xfs_mount_t *mp = ip->i_mount;
3536 xfs_off_t startoffset;
3541 xfs_itrace_entry(ip);
3543 if (!S_ISREG(ip->i_d.di_mode))
3544 return XFS_ERROR(EINVAL);
3546 switch (bf->l_whence) {
3547 case 0: /*SEEK_SET*/
3549 case 1: /*SEEK_CUR*/
3550 bf->l_start += offset;
3552 case 2: /*SEEK_END*/
3553 bf->l_start += ip->i_size;
3556 return XFS_ERROR(EINVAL);
3559 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3561 if ( (bf->l_start < 0)
3562 || (bf->l_start > XFS_MAXIOFFSET(mp))
3563 || (bf->l_start + llen < 0)
3564 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3565 return XFS_ERROR(EINVAL);
3569 startoffset = bf->l_start;
3573 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3575 * These calls do NOT zero the data space allocated to the file,
3576 * nor do they change the file size.
3578 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3580 * These calls cause the new file data to be zeroed and the file
3581 * size to be changed.
3583 setprealloc = clrprealloc = 0;
3586 case XFS_IOC_RESVSP:
3587 case XFS_IOC_RESVSP64:
3588 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3595 case XFS_IOC_UNRESVSP:
3596 case XFS_IOC_UNRESVSP64:
3597 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3602 case XFS_IOC_ALLOCSP:
3603 case XFS_IOC_ALLOCSP64:
3604 case XFS_IOC_FREESP:
3605 case XFS_IOC_FREESP64:
3606 if (startoffset > fsize) {
3607 error = xfs_alloc_file_space(ip, fsize,
3608 startoffset - fsize, 0, attr_flags);
3613 iattr.ia_valid = ATTR_SIZE;
3614 iattr.ia_size = startoffset;
3616 error = xfs_setattr(ip, &iattr, attr_flags, credp);
3626 return XFS_ERROR(EINVAL);
3630 * update the inode timestamp, mode, and prealloc flag bits
3632 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3634 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3637 xfs_trans_cancel(tp, 0);
3641 xfs_ilock(ip, XFS_ILOCK_EXCL);
3643 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3644 xfs_trans_ihold(tp, ip);
3646 if ((attr_flags & XFS_ATTR_DMI) == 0) {
3647 ip->i_d.di_mode &= ~S_ISUID;
3650 * Note that we don't have to worry about mandatory
3651 * file locking being disabled here because we only
3652 * clear the S_ISGID bit if the Group execute bit is
3653 * on, but if it was on then mandatory locking wouldn't
3654 * have been enabled.
3656 if (ip->i_d.di_mode & S_IXGRP)
3657 ip->i_d.di_mode &= ~S_ISGID;
3659 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3662 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3663 else if (clrprealloc)
3664 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3666 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3667 xfs_trans_set_sync(tp);
3669 error = xfs_trans_commit(tp, 0);
3671 xfs_iunlock(ip, XFS_ILOCK_EXCL);