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_refcache.h"
52 #include "xfs_trans_space.h"
53 #include "xfs_log_priv.h"
54 #include "xfs_filestream.h"
55 #include "xfs_vnodeops.h"
63 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
64 return XFS_ERROR(EIO);
67 * If it's a directory with any blocks, read-ahead block 0
68 * as we're almost certain to have the next operation be a read there.
70 if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
71 mode = xfs_ilock_map_shared(ip);
72 if (ip->i_d.di_nextents > 0)
73 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
74 xfs_iunlock(ip, mode);
88 bhv_vnode_t *vp = XFS_ITOV(ip);
89 xfs_mount_t *mp = ip->i_mount;
91 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
93 if (XFS_FORCED_SHUTDOWN(mp))
94 return XFS_ERROR(EIO);
96 if (!(flags & ATTR_LAZY))
97 xfs_ilock(ip, XFS_ILOCK_SHARED);
99 vap->va_size = XFS_ISIZE(ip);
100 if (vap->va_mask == XFS_AT_SIZE)
104 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
105 vap->va_nodeid = ip->i_ino;
107 vap->va_nodeid += mp->m_inoadd;
109 vap->va_nlink = ip->i_d.di_nlink;
112 * Quick exit for non-stat callers
115 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
116 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
120 * Copy from in-core inode.
122 vap->va_mode = ip->i_d.di_mode;
123 vap->va_uid = ip->i_d.di_uid;
124 vap->va_gid = ip->i_d.di_gid;
125 vap->va_projid = ip->i_d.di_projid;
128 * Check vnode type block/char vs. everything else.
130 switch (ip->i_d.di_mode & S_IFMT) {
133 vap->va_rdev = ip->i_df.if_u2.if_rdev;
134 vap->va_blocksize = BLKDEV_IOSIZE;
139 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
140 vap->va_blocksize = xfs_preferred_iosize(mp);
144 * If the file blocks are being allocated from a
145 * realtime partition, then return the inode's
146 * realtime extent size or the realtime volume's
150 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
155 vn_atime_to_timespec(vp, &vap->va_atime);
156 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
157 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
158 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
159 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
162 * Exit for stat callers. See if any of the rest of the fields
163 * to be filled in are needed.
166 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
167 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
171 * Convert di_flags to xflags.
173 vap->va_xflags = xfs_ip2xflags(ip);
176 * Exit for inode revalidate. See if any of the rest of
177 * the fields to be filled in are needed.
180 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
181 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
184 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
186 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
187 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
191 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
192 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
193 ip->i_d.di_anextents;
195 vap->va_anextents = 0;
196 vap->va_gen = ip->i_d.di_gen;
199 if (!(flags & ATTR_LAZY))
200 xfs_iunlock(ip, XFS_ILOCK_SHARED);
215 bhv_vnode_t *vp = XFS_ITOV(ip);
216 xfs_mount_t *mp = ip->i_mount;
225 xfs_prid_t projid=0, iprojid=0;
226 int mandlock_before, mandlock_after;
227 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
231 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
233 if (mp->m_flags & XFS_MOUNT_RDONLY)
234 return XFS_ERROR(EROFS);
237 * Cannot set certain attributes.
240 if (mask & XFS_AT_NOSET) {
241 return XFS_ERROR(EINVAL);
244 if (XFS_FORCED_SHUTDOWN(mp))
245 return XFS_ERROR(EIO);
248 * Timestamps do not need to be logged and hence do not
249 * need to be done within a transaction.
251 if (mask & XFS_AT_UPDTIMES) {
252 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
253 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
254 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
255 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
256 xfs_ichgtime(ip, timeflags);
260 olddquot1 = olddquot2 = NULL;
264 * If disk quotas is on, we make sure that the dquots do exist on disk,
265 * before we start any other transactions. Trying to do this later
266 * is messy. We don't care to take a readlock to look at the ids
267 * in inode here, because we can't hold it across the trans_reserve.
268 * If the IDs do change before we take the ilock, we're covered
269 * because the i_*dquot fields will get updated anyway.
271 if (XFS_IS_QUOTA_ON(mp) &&
272 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
275 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
277 qflags |= XFS_QMOPT_UQUOTA;
279 uid = ip->i_d.di_uid;
281 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
283 qflags |= XFS_QMOPT_GQUOTA;
285 gid = ip->i_d.di_gid;
287 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
288 projid = vap->va_projid;
289 qflags |= XFS_QMOPT_PQUOTA;
291 projid = ip->i_d.di_projid;
294 * We take a reference when we initialize udqp and gdqp,
295 * so it is important that we never blindly double trip on
296 * the same variable. See xfs_create() for an example.
298 ASSERT(udqp == NULL);
299 ASSERT(gdqp == NULL);
300 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
307 * For the other attributes, we acquire the inode lock and
308 * first do an error checking pass.
311 lock_flags = XFS_ILOCK_EXCL;
312 if (flags & ATTR_NOLOCK)
314 if (!(mask & XFS_AT_SIZE)) {
315 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
316 (mp->m_flags & XFS_MOUNT_WSYNC)) {
317 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
319 if ((code = xfs_trans_reserve(tp, 0,
320 XFS_ICHANGE_LOG_RES(mp), 0,
327 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
328 !(flags & ATTR_DMI)) {
329 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
330 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
331 vap->va_size, 0, dmflags, NULL);
338 lock_flags |= XFS_IOLOCK_EXCL;
341 xfs_ilock(ip, lock_flags);
343 /* boolean: are we the file owner? */
344 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
347 * Change various properties of a file.
348 * Only the owner or users with CAP_FOWNER
349 * capability may do these things.
352 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
353 XFS_AT_GID|XFS_AT_PROJID)) {
355 * CAP_FOWNER overrides the following restrictions:
357 * The user ID of the calling process must be equal
358 * to the file owner ID, except in cases where the
359 * CAP_FSETID capability is applicable.
361 if (!file_owner && !capable(CAP_FOWNER)) {
362 code = XFS_ERROR(EPERM);
367 * CAP_FSETID overrides the following restrictions:
369 * The effective user ID of the calling process shall match
370 * the file owner when setting the set-user-ID and
371 * set-group-ID bits on that file.
373 * The effective group ID or one of the supplementary group
374 * IDs of the calling process shall match the group owner of
375 * the file when setting the set-group-ID bit on that file
377 if (mask & XFS_AT_MODE) {
380 if ((vap->va_mode & S_ISUID) && !file_owner)
382 if ((vap->va_mode & S_ISGID) &&
383 !in_group_p((gid_t)ip->i_d.di_gid))
386 /* Linux allows this, Irix doesn't. */
387 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
390 if (m && !capable(CAP_FSETID))
396 * Change file ownership. Must be the owner or privileged.
397 * If the system was configured with the "restricted_chown"
398 * option, the owner is not permitted to give away the file,
399 * and can change the group id only to a group of which he
400 * or she is a member.
402 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
404 * These IDs could have changed since we last looked at them.
405 * But, we're assured that if the ownership did change
406 * while we didn't have the inode locked, inode's dquot(s)
407 * would have changed also.
409 iuid = ip->i_d.di_uid;
410 iprojid = ip->i_d.di_projid;
411 igid = ip->i_d.di_gid;
412 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
413 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
414 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
418 * CAP_CHOWN overrides the following restrictions:
420 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
421 * shall override the restriction that a process cannot
422 * change the user ID of a file it owns and the restriction
423 * that the group ID supplied to the chown() function
424 * shall be equal to either the group ID or one of the
425 * supplementary group IDs of the calling process.
427 if (restricted_chown &&
428 (iuid != uid || (igid != gid &&
429 !in_group_p((gid_t)gid))) &&
430 !capable(CAP_CHOWN)) {
431 code = XFS_ERROR(EPERM);
435 * Do a quota reservation only if uid/projid/gid is actually
438 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
439 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
440 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
442 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
443 capable(CAP_FOWNER) ?
444 XFS_QMOPT_FORCE_RES : 0);
445 if (code) /* out of quota */
451 * Truncate file. Must have write permission and not be a directory.
453 if (mask & XFS_AT_SIZE) {
454 /* Short circuit the truncate case for zero length files */
455 if ((vap->va_size == 0) &&
456 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
457 xfs_iunlock(ip, XFS_ILOCK_EXCL);
458 lock_flags &= ~XFS_ILOCK_EXCL;
459 if (mask & XFS_AT_CTIME)
460 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
466 code = XFS_ERROR(EISDIR);
468 } else if (!VN_ISREG(vp)) {
469 code = XFS_ERROR(EINVAL);
473 * Make sure that the dquots are attached to the inode.
475 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
480 * Change file access or modified times.
482 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
484 if ((flags & ATTR_UTIME) &&
485 !capable(CAP_FOWNER)) {
486 code = XFS_ERROR(EPERM);
493 * Change extent size or realtime flag.
495 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
497 * Can't change extent size if any extents are allocated.
499 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
500 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
502 code = XFS_ERROR(EINVAL); /* EFBIG? */
507 * Can't change realtime flag if any extents are allocated.
509 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
510 (mask & XFS_AT_XFLAGS) &&
511 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
512 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
513 code = XFS_ERROR(EINVAL); /* EFBIG? */
517 * Extent size must be a multiple of the appropriate block
518 * size, if set at all.
520 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
523 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
524 ((mask & XFS_AT_XFLAGS) &&
525 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
526 size = mp->m_sb.sb_rextsize <<
527 mp->m_sb.sb_blocklog;
529 size = mp->m_sb.sb_blocksize;
531 if (vap->va_extsize % size) {
532 code = XFS_ERROR(EINVAL);
537 * If realtime flag is set then must have realtime data.
539 if ((mask & XFS_AT_XFLAGS) &&
540 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
541 if ((mp->m_sb.sb_rblocks == 0) ||
542 (mp->m_sb.sb_rextsize == 0) ||
543 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
544 code = XFS_ERROR(EINVAL);
550 * Can't modify an immutable/append-only file unless
551 * we have appropriate permission.
553 if ((mask & XFS_AT_XFLAGS) &&
555 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
557 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
558 !capable(CAP_LINUX_IMMUTABLE)) {
559 code = XFS_ERROR(EPERM);
565 * Now we can make the changes. Before we join the inode
566 * to the transaction, if XFS_AT_SIZE is set then take care of
567 * the part of the truncation that must be done without the
568 * inode lock. This needs to be done before joining the inode
569 * to the transaction, because the inode cannot be unlocked
570 * once it is a part of the transaction.
572 if (mask & XFS_AT_SIZE) {
574 if ((vap->va_size > ip->i_size) &&
575 (flags & ATTR_NOSIZETOK) == 0) {
576 code = xfs_igrow_start(ip, vap->va_size, credp);
578 xfs_iunlock(ip, XFS_ILOCK_EXCL);
581 * We are going to log the inode size change in this
582 * transaction so any previous writes that are beyond the on
583 * disk EOF and the new EOF that have not been written out need
584 * to be written here. If we do not write the data out, we
585 * expose ourselves to the null files problem.
587 * Only flush from the on disk size to the smaller of the in
588 * memory file size or the new size as that's the range we
589 * really care about here and prevents waiting for other data
590 * not within the range we care about here.
593 (ip->i_size != ip->i_d.di_size) &&
594 (vap->va_size > ip->i_d.di_size)) {
595 code = xfs_flush_pages(ip,
596 ip->i_d.di_size, vap->va_size,
597 XFS_B_ASYNC, FI_NONE);
600 /* wait for all I/O to complete */
604 code = xfs_itruncate_data(ip, vap->va_size);
607 lock_flags &= ~XFS_ILOCK_EXCL;
608 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
611 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
612 if ((code = xfs_trans_reserve(tp, 0,
613 XFS_ITRUNCATE_LOG_RES(mp), 0,
614 XFS_TRANS_PERM_LOG_RES,
615 XFS_ITRUNCATE_LOG_COUNT))) {
616 xfs_trans_cancel(tp, 0);
618 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
621 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
622 xfs_ilock(ip, XFS_ILOCK_EXCL);
626 xfs_trans_ijoin(tp, ip, lock_flags);
627 xfs_trans_ihold(tp, ip);
630 /* determine whether mandatory locking mode changes */
631 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
634 * Truncate file. Must have write permission and not be a directory.
636 if (mask & XFS_AT_SIZE) {
637 if (vap->va_size > ip->i_size) {
638 xfs_igrow_finish(tp, ip, vap->va_size,
639 !(flags & ATTR_DMI));
640 } else if ((vap->va_size <= ip->i_size) ||
641 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
643 * signal a sync transaction unless
644 * we're truncating an already unlinked
645 * file on a wsync filesystem
647 code = xfs_itruncate_finish(&tp, ip,
648 (xfs_fsize_t)vap->va_size,
650 ((ip->i_d.di_nlink != 0 ||
651 !(mp->m_flags & XFS_MOUNT_WSYNC))
656 * Truncated "down", so we're removing references
657 * to old data here - if we now delay flushing for
658 * a long time, we expose ourselves unduly to the
659 * notorious NULL files problem. So, we mark this
660 * vnode and flush it when the file is closed, and
661 * do not wait the usual (long) time for writeout.
663 xfs_iflags_set(ip, XFS_ITRUNCATED);
666 * Have to do this even if the file's size doesn't change.
668 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
672 * Change file access modes.
674 if (mask & XFS_AT_MODE) {
675 ip->i_d.di_mode &= S_IFMT;
676 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
678 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
679 timeflags |= XFS_ICHGTIME_CHG;
683 * Change file ownership. Must be the owner or privileged.
684 * If the system was configured with the "restricted_chown"
685 * option, the owner is not permitted to give away the file,
686 * and can change the group id only to a group of which he
687 * or she is a member.
689 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
691 * CAP_FSETID overrides the following restrictions:
693 * The set-user-ID and set-group-ID bits of a file will be
694 * cleared upon successful return from chown()
696 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
697 !capable(CAP_FSETID)) {
698 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
702 * Change the ownerships and register quota modifications
703 * in the transaction.
706 if (XFS_IS_UQUOTA_ON(mp)) {
707 ASSERT(mask & XFS_AT_UID);
709 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
710 &ip->i_udquot, udqp);
712 ip->i_d.di_uid = uid;
715 if (XFS_IS_GQUOTA_ON(mp)) {
716 ASSERT(!XFS_IS_PQUOTA_ON(mp));
717 ASSERT(mask & XFS_AT_GID);
719 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
720 &ip->i_gdquot, gdqp);
722 ip->i_d.di_gid = gid;
724 if (iprojid != projid) {
725 if (XFS_IS_PQUOTA_ON(mp)) {
726 ASSERT(!XFS_IS_GQUOTA_ON(mp));
727 ASSERT(mask & XFS_AT_PROJID);
729 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
730 &ip->i_gdquot, gdqp);
732 ip->i_d.di_projid = projid;
734 * We may have to rev the inode as well as
735 * the superblock version number since projids didn't
736 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
738 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
739 xfs_bump_ino_vers2(tp, ip);
742 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
743 timeflags |= XFS_ICHGTIME_CHG;
748 * Change file access or modified times.
750 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
751 if (mask & XFS_AT_ATIME) {
752 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
753 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
754 ip->i_update_core = 1;
755 timeflags &= ~XFS_ICHGTIME_ACC;
757 if (mask & XFS_AT_MTIME) {
758 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
759 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
760 timeflags &= ~XFS_ICHGTIME_MOD;
761 timeflags |= XFS_ICHGTIME_CHG;
763 if (tp && (flags & ATTR_UTIME))
764 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
768 * Change XFS-added attributes.
770 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
771 if (mask & XFS_AT_EXTSIZE) {
773 * Converting bytes to fs blocks.
775 ip->i_d.di_extsize = vap->va_extsize >>
776 mp->m_sb.sb_blocklog;
778 if (mask & XFS_AT_XFLAGS) {
781 /* can't set PREALLOC this way, just preserve it */
782 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
783 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
784 di_flags |= XFS_DIFLAG_IMMUTABLE;
785 if (vap->va_xflags & XFS_XFLAG_APPEND)
786 di_flags |= XFS_DIFLAG_APPEND;
787 if (vap->va_xflags & XFS_XFLAG_SYNC)
788 di_flags |= XFS_DIFLAG_SYNC;
789 if (vap->va_xflags & XFS_XFLAG_NOATIME)
790 di_flags |= XFS_DIFLAG_NOATIME;
791 if (vap->va_xflags & XFS_XFLAG_NODUMP)
792 di_flags |= XFS_DIFLAG_NODUMP;
793 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
794 di_flags |= XFS_DIFLAG_PROJINHERIT;
795 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
796 di_flags |= XFS_DIFLAG_NODEFRAG;
797 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
798 di_flags |= XFS_DIFLAG_FILESTREAM;
799 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
800 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
801 di_flags |= XFS_DIFLAG_RTINHERIT;
802 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
803 di_flags |= XFS_DIFLAG_NOSYMLINKS;
804 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
805 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
806 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
807 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
808 di_flags |= XFS_DIFLAG_REALTIME;
809 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
811 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
813 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
814 di_flags |= XFS_DIFLAG_EXTSIZE;
816 ip->i_d.di_flags = di_flags;
818 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
819 timeflags |= XFS_ICHGTIME_CHG;
823 * Change file inode change time only if XFS_AT_CTIME set
824 * AND we have been called by a DMI function.
827 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
828 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
829 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
830 ip->i_update_core = 1;
831 timeflags &= ~XFS_ICHGTIME_CHG;
835 * Send out timestamp changes that need to be set to the
836 * current time. Not done when called by a DMI function.
838 if (timeflags && !(flags & ATTR_DMI))
839 xfs_ichgtime(ip, timeflags);
841 XFS_STATS_INC(xs_ig_attrchg);
844 * If this is a synchronous mount, make sure that the
845 * transaction goes to disk before returning to the user.
846 * This is slightly sub-optimal in that truncates require
847 * two sync transactions instead of one for wsync filesystems.
848 * One for the truncate and one for the timestamps since we
849 * don't want to change the timestamps unless we're sure the
850 * truncate worked. Truncates are less than 1% of the laddis
851 * mix so this probably isn't worth the trouble to optimize.
855 if (mp->m_flags & XFS_MOUNT_WSYNC)
856 xfs_trans_set_sync(tp);
858 code = xfs_trans_commit(tp, commit_flags);
862 * If the (regular) file's mandatory locking mode changed, then
863 * notify the vnode. We do this under the inode lock to prevent
864 * racing calls to vop_vnode_change.
866 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
868 xfs_iunlock(ip, lock_flags);
871 * Release any dquot(s) the inode had kept before chown.
873 XFS_QM_DQRELE(mp, olddquot1);
874 XFS_QM_DQRELE(mp, olddquot2);
875 XFS_QM_DQRELE(mp, udqp);
876 XFS_QM_DQRELE(mp, gdqp);
882 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
883 !(flags & ATTR_DMI)) {
884 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
885 NULL, DM_RIGHT_NULL, NULL, NULL,
886 0, 0, AT_DELAY_FLAG(flags));
891 commit_flags |= XFS_TRANS_ABORT;
894 XFS_QM_DQRELE(mp, udqp);
895 XFS_QM_DQRELE(mp, gdqp);
897 xfs_trans_cancel(tp, commit_flags);
899 if (lock_flags != 0) {
900 xfs_iunlock(ip, lock_flags);
908 * Null conversion from vnode mode bits to inode mode bits, as in efs.
918 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
920 xfs_ilock(ip, XFS_ILOCK_SHARED);
921 error = xfs_iaccess(ip, mode, credp);
922 xfs_iunlock(ip, XFS_ILOCK_SHARED);
928 * The maximum pathlen is 1024 bytes. Since the minimum file system
929 * blocksize is 512 bytes, we can get a max of 2 extents back from
932 #define SYMLINK_MAPS 2
939 xfs_mount_t *mp = ip->i_mount;
940 int pathlen = ip->i_d.di_size;
941 int nmaps = SYMLINK_MAPS;
942 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
949 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
950 mval, &nmaps, NULL, NULL);
954 for (n = 0; n < nmaps; n++) {
955 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
956 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
958 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
959 error = XFS_BUF_GETERROR(bp);
961 xfs_ioerror_alert("xfs_readlink",
962 ip->i_mount, bp, XFS_BUF_ADDR(bp));
966 if (pathlen < byte_cnt)
970 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
974 link[ip->i_d.di_size] = '\0';
986 xfs_mount_t *mp = ip->i_mount;
990 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
992 if (XFS_FORCED_SHUTDOWN(mp))
993 return XFS_ERROR(EIO);
995 xfs_ilock(ip, XFS_ILOCK_SHARED);
997 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
998 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
1000 pathlen = ip->i_d.di_size;
1004 if (ip->i_df.if_flags & XFS_IFINLINE) {
1005 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
1006 link[pathlen] = '\0';
1008 error = xfs_readlink_bmap(ip, link);
1012 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1019 * This is called to sync the inode and its data out to disk.
1020 * We need to hold the I/O lock while flushing the data, and
1021 * the inode lock while flushing the inode. The inode lock CANNOT
1022 * be held while flushing the data, so acquire after we're done
1034 int log_flushed = 0, changed = 1;
1036 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
1038 ASSERT(start >= 0 && stop >= -1);
1040 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1041 return XFS_ERROR(EIO);
1043 if (flag & FSYNC_DATA)
1044 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1047 * We always need to make sure that the required inode state
1048 * is safe on disk. The vnode might be clean but because
1049 * of committed transactions that haven't hit the disk yet.
1050 * Likewise, there could be unflushed non-transactional
1051 * changes to the inode core that have to go to disk.
1053 * The following code depends on one assumption: that
1054 * any transaction that changes an inode logs the core
1055 * because it has to change some field in the inode core
1056 * (typically nextents or nblocks). That assumption
1057 * implies that any transactions against an inode will
1058 * catch any non-transactional updates. If inode-altering
1059 * transactions exist that violate this assumption, the
1060 * code breaks. Right now, it figures that if the involved
1061 * update_* field is clear and the inode is unpinned, the
1062 * inode is clean. Either it's been flushed or it's been
1063 * committed and the commit has hit the disk unpinning the inode.
1064 * (Note that xfs_inode_item_format() called at commit clears
1065 * the update_* fields.)
1067 xfs_ilock(ip, XFS_ILOCK_SHARED);
1069 /* If we are flushing data then we care about update_size
1070 * being set, otherwise we care about update_core
1072 if ((flag & FSYNC_DATA) ?
1073 (ip->i_update_size == 0) :
1074 (ip->i_update_core == 0)) {
1076 * Timestamps/size haven't changed since last inode
1077 * flush or inode transaction commit. That means
1078 * either nothing got written or a transaction
1079 * committed which caught the updates. If the
1080 * latter happened and the transaction hasn't
1081 * hit the disk yet, the inode will be still
1082 * be pinned. If it is, force the log.
1085 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1087 if (xfs_ipincount(ip)) {
1088 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1090 ((flag & FSYNC_WAIT)
1091 ? XFS_LOG_SYNC : 0),
1095 * If the inode is not pinned and nothing
1096 * has changed we don't need to flush the
1104 * Kick off a transaction to log the inode
1105 * core to get the updates. Make it
1106 * sync if FSYNC_WAIT is passed in (which
1107 * is done by everybody but specfs). The
1108 * sync transaction will also force the log.
1110 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1111 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1112 if ((error = xfs_trans_reserve(tp, 0,
1113 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1115 xfs_trans_cancel(tp, 0);
1118 xfs_ilock(ip, XFS_ILOCK_EXCL);
1121 * Note - it's possible that we might have pushed
1122 * ourselves out of the way during trans_reserve
1123 * which would flush the inode. But there's no
1124 * guarantee that the inode buffer has actually
1125 * gone out yet (it's delwri). Plus the buffer
1126 * could be pinned anyway if it's part of an
1127 * inode in another recent transaction. So we
1128 * play it safe and fire off the transaction anyway.
1130 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1131 xfs_trans_ihold(tp, ip);
1132 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1133 if (flag & FSYNC_WAIT)
1134 xfs_trans_set_sync(tp);
1135 error = _xfs_trans_commit(tp, 0, &log_flushed);
1137 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1140 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1142 * If the log write didn't issue an ordered tag we need
1143 * to flush the disk cache for the data device now.
1146 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1149 * If this inode is on the RT dev we need to flush that
1152 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1153 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1160 * This is called by xfs_inactive to free any blocks beyond eof
1161 * when the link count isn't zero and by xfs_dm_punch_hole() when
1162 * punching a hole to EOF.
1172 xfs_fileoff_t end_fsb;
1173 xfs_fileoff_t last_fsb;
1174 xfs_filblks_t map_len;
1176 xfs_bmbt_irec_t imap;
1177 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
1180 * Figure out if there are any blocks beyond the end
1181 * of the file. If not, then there is nothing to do.
1183 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
1184 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1185 map_len = last_fsb - end_fsb;
1190 xfs_ilock(ip, XFS_ILOCK_SHARED);
1191 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, end_fsb, map_len, 0,
1192 NULL, 0, &imap, &nimaps, NULL, NULL);
1193 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1195 if (!error && (nimaps != 0) &&
1196 (imap.br_startblock != HOLESTARTBLOCK ||
1197 ip->i_delayed_blks)) {
1199 * Attach the dquots to the inode up front.
1201 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1205 * There are blocks after the end of file.
1206 * Free them up now by truncating the file to
1209 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1212 * Do the xfs_itruncate_start() call before
1213 * reserving any log space because
1214 * itruncate_start will call into the buffer
1215 * cache and we can't
1216 * do that within a transaction.
1219 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1220 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1223 xfs_trans_cancel(tp, 0);
1225 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1229 error = xfs_trans_reserve(tp, 0,
1230 XFS_ITRUNCATE_LOG_RES(mp),
1231 0, XFS_TRANS_PERM_LOG_RES,
1232 XFS_ITRUNCATE_LOG_COUNT);
1234 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1235 xfs_trans_cancel(tp, 0);
1236 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1240 xfs_ilock(ip, XFS_ILOCK_EXCL);
1241 xfs_trans_ijoin(tp, ip,
1244 xfs_trans_ihold(tp, ip);
1246 error = xfs_itruncate_finish(&tp, ip,
1251 * If we get an error at this point we
1252 * simply don't bother truncating the file.
1255 xfs_trans_cancel(tp,
1256 (XFS_TRANS_RELEASE_LOG_RES |
1259 error = xfs_trans_commit(tp,
1260 XFS_TRANS_RELEASE_LOG_RES);
1262 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1269 * Free a symlink that has blocks associated with it.
1272 xfs_inactive_symlink_rmt(
1280 xfs_fsblock_t first_block;
1281 xfs_bmap_free_t free_list;
1284 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1292 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1294 * We're freeing a symlink that has some
1295 * blocks allocated to it. Free the
1296 * blocks here. We know that we've got
1297 * either 1 or 2 extents and that we can
1298 * free them all in one bunmapi call.
1300 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1301 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1302 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1303 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1304 xfs_trans_cancel(tp, 0);
1309 * Lock the inode, fix the size, and join it to the transaction.
1310 * Hold it so in the normal path, we still have it locked for
1311 * the second transaction. In the error paths we need it
1312 * held so the cancel won't rele it, see below.
1314 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1315 size = (int)ip->i_d.di_size;
1316 ip->i_d.di_size = 0;
1317 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1318 xfs_trans_ihold(tp, ip);
1319 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1321 * Find the block(s) so we can inval and unmap them.
1324 XFS_BMAP_INIT(&free_list, &first_block);
1325 nmaps = ARRAY_SIZE(mval);
1326 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1327 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1331 * Invalidate the block(s).
1333 for (i = 0; i < nmaps; i++) {
1334 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1335 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1336 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1337 xfs_trans_binval(tp, bp);
1340 * Unmap the dead block(s) to the free_list.
1342 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1343 &first_block, &free_list, NULL, &done)))
1347 * Commit the first transaction. This logs the EFI and the inode.
1349 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1352 * The transaction must have been committed, since there were
1353 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1354 * The new tp has the extent freeing and EFDs.
1358 * The first xact was committed, so add the inode to the new one.
1359 * Mark it dirty so it will be logged and moved forward in the log as
1360 * part of every commit.
1362 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1363 xfs_trans_ihold(tp, ip);
1364 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1366 * Get a new, empty transaction to return to our caller.
1368 ntp = xfs_trans_dup(tp);
1370 * Commit the transaction containing extent freeing and EFDs.
1371 * If we get an error on the commit here or on the reserve below,
1372 * we need to unlock the inode since the new transaction doesn't
1373 * have the inode attached.
1375 error = xfs_trans_commit(tp, 0);
1378 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1382 * Remove the memory for extent descriptions (just bookkeeping).
1384 if (ip->i_df.if_bytes)
1385 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1386 ASSERT(ip->i_df.if_bytes == 0);
1388 * Put an itruncate log reservation in the new transaction
1391 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1392 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1393 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1397 * Return with the inode locked but not joined to the transaction.
1403 xfs_bmap_cancel(&free_list);
1406 * Have to come here with the inode locked and either
1407 * (held and in the transaction) or (not in the transaction).
1408 * If the inode isn't held then cancel would iput it, but
1409 * that's wrong since this is inactive and the vnode ref
1410 * count is 0 already.
1411 * Cancel won't do anything to the inode if held, but it still
1412 * needs to be locked until the cancel is done, if it was
1413 * joined to the transaction.
1415 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1416 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1423 xfs_inactive_symlink_local(
1429 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1431 * We're freeing a symlink which fit into
1432 * the inode. Just free the memory used
1433 * to hold the old symlink.
1435 error = xfs_trans_reserve(*tpp, 0,
1436 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1437 0, XFS_TRANS_PERM_LOG_RES,
1438 XFS_ITRUNCATE_LOG_COUNT);
1441 xfs_trans_cancel(*tpp, 0);
1445 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1448 * Zero length symlinks _can_ exist.
1450 if (ip->i_df.if_bytes > 0) {
1451 xfs_idata_realloc(ip,
1452 -(ip->i_df.if_bytes),
1454 ASSERT(ip->i_df.if_bytes == 0);
1468 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1471 ASSERT(ip->i_d.di_forkoff != 0);
1472 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1473 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1475 error = xfs_attr_inactive(ip);
1478 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1479 return error; /* goto out */
1482 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1483 error = xfs_trans_reserve(tp, 0,
1484 XFS_IFREE_LOG_RES(mp),
1485 0, XFS_TRANS_PERM_LOG_RES,
1486 XFS_INACTIVE_LOG_COUNT);
1488 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1489 xfs_trans_cancel(tp, 0);
1491 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1495 xfs_ilock(ip, XFS_ILOCK_EXCL);
1496 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1497 xfs_trans_ihold(tp, ip);
1498 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1500 ASSERT(ip->i_d.di_anextents == 0);
1510 bhv_vnode_t *vp = XFS_ITOV(ip);
1511 xfs_mount_t *mp = ip->i_mount;
1514 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1517 /* If this is a read-only mount, don't do this (would generate I/O) */
1518 if (mp->m_flags & XFS_MOUNT_RDONLY)
1521 if (!XFS_FORCED_SHUTDOWN(mp)) {
1525 * If we are using filestreams, and we have an unlinked
1526 * file that we are processing the last close on, then nothing
1527 * will be able to reopen and write to this file. Purge this
1528 * inode from the filestreams cache so that it doesn't delay
1529 * teardown of the inode.
1531 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1532 xfs_filestream_deassociate(ip);
1535 * If we previously truncated this file and removed old data
1536 * in the process, we want to initiate "early" writeout on
1537 * the last close. This is an attempt to combat the notorious
1538 * NULL files problem which is particularly noticable from a
1539 * truncate down, buffered (re-)write (delalloc), followed by
1540 * a crash. What we are effectively doing here is
1541 * significantly reducing the time window where we'd otherwise
1542 * be exposed to that problem.
1544 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1545 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
1546 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1549 #ifdef HAVE_REFCACHE
1550 /* If we are in the NFS reference cache then don't do this now */
1555 if (ip->i_d.di_nlink != 0) {
1556 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1557 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1558 ip->i_delayed_blks > 0)) &&
1559 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1560 (!(ip->i_d.di_flags &
1561 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1562 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1565 /* Update linux inode block count after free above */
1566 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1567 ip->i_d.di_nblocks + ip->i_delayed_blks);
1577 * This is called when the vnode reference count for the vnode
1578 * goes to zero. If the file has been unlinked, then it must
1579 * now be truncated. Also, we clear all of the read-ahead state
1580 * kept for the inode here since the file is now closed.
1586 bhv_vnode_t *vp = XFS_ITOV(ip);
1587 xfs_bmap_free_t free_list;
1588 xfs_fsblock_t first_block;
1595 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
1598 * If the inode is already free, then there can be nothing
1601 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1602 ASSERT(ip->i_df.if_real_bytes == 0);
1603 ASSERT(ip->i_df.if_broot_bytes == 0);
1604 return VN_INACTIVE_CACHE;
1608 * Only do a truncate if it's a regular file with
1609 * some actual space in it. It's OK to look at the
1610 * inode's fields without the lock because we're the
1611 * only one with a reference to the inode.
1613 truncate = ((ip->i_d.di_nlink == 0) &&
1614 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1615 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1616 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1620 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
1621 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1626 /* If this is a read-only mount, don't do this (would generate I/O) */
1627 if (mp->m_flags & XFS_MOUNT_RDONLY)
1630 if (ip->i_d.di_nlink != 0) {
1631 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1632 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1633 ip->i_delayed_blks > 0)) &&
1634 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1635 (!(ip->i_d.di_flags &
1636 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1637 (ip->i_delayed_blks != 0)))) {
1638 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1640 return VN_INACTIVE_CACHE;
1641 /* Update linux inode block count after free above */
1642 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1643 ip->i_d.di_nblocks + ip->i_delayed_blks);
1648 ASSERT(ip->i_d.di_nlink == 0);
1650 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1651 return VN_INACTIVE_CACHE;
1653 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1656 * Do the xfs_itruncate_start() call before
1657 * reserving any log space because itruncate_start
1658 * will call into the buffer cache and we can't
1659 * do that within a transaction.
1661 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1663 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1665 xfs_trans_cancel(tp, 0);
1666 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1667 return VN_INACTIVE_CACHE;
1670 error = xfs_trans_reserve(tp, 0,
1671 XFS_ITRUNCATE_LOG_RES(mp),
1672 0, XFS_TRANS_PERM_LOG_RES,
1673 XFS_ITRUNCATE_LOG_COUNT);
1675 /* Don't call itruncate_cleanup */
1676 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1677 xfs_trans_cancel(tp, 0);
1678 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1679 return VN_INACTIVE_CACHE;
1682 xfs_ilock(ip, XFS_ILOCK_EXCL);
1683 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1684 xfs_trans_ihold(tp, ip);
1687 * normally, we have to run xfs_itruncate_finish sync.
1688 * But if filesystem is wsync and we're in the inactive
1689 * path, then we know that nlink == 0, and that the
1690 * xaction that made nlink == 0 is permanently committed
1691 * since xfs_remove runs as a synchronous transaction.
1693 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1694 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1697 xfs_trans_cancel(tp,
1698 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1699 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1700 return VN_INACTIVE_CACHE;
1702 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1705 * If we get an error while cleaning up a
1706 * symlink we bail out.
1708 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1709 xfs_inactive_symlink_rmt(ip, &tp) :
1710 xfs_inactive_symlink_local(ip, &tp);
1714 return VN_INACTIVE_CACHE;
1717 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1718 xfs_trans_ihold(tp, ip);
1720 error = xfs_trans_reserve(tp, 0,
1721 XFS_IFREE_LOG_RES(mp),
1722 0, XFS_TRANS_PERM_LOG_RES,
1723 XFS_INACTIVE_LOG_COUNT);
1725 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1726 xfs_trans_cancel(tp, 0);
1727 return VN_INACTIVE_CACHE;
1730 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1731 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1732 xfs_trans_ihold(tp, ip);
1736 * If there are attributes associated with the file
1737 * then blow them away now. The code calls a routine
1738 * that recursively deconstructs the attribute fork.
1739 * We need to just commit the current transaction
1740 * because we can't use it for xfs_attr_inactive().
1742 if (ip->i_d.di_anextents > 0) {
1743 error = xfs_inactive_attrs(ip, &tp);
1745 * If we got an error, the transaction is already
1746 * cancelled, and the inode is unlocked. Just get out.
1749 return VN_INACTIVE_CACHE;
1750 } else if (ip->i_afp) {
1751 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1757 XFS_BMAP_INIT(&free_list, &first_block);
1758 error = xfs_ifree(tp, ip, &free_list);
1761 * If we fail to free the inode, shut down. The cancel
1762 * might do that, we need to make sure. Otherwise the
1763 * inode might be lost for a long time or forever.
1765 if (!XFS_FORCED_SHUTDOWN(mp)) {
1767 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1768 error, mp->m_fsname);
1769 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1771 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1774 * Credit the quota account(s). The inode is gone.
1776 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1779 * Just ignore errors at this point. There is
1780 * nothing we can do except to try to keep going.
1782 (void) xfs_bmap_finish(&tp, &free_list, &committed);
1783 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1786 * Release the dquots held by inode, if any.
1788 XFS_QM_DQDETACH(mp, ip);
1790 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1793 return VN_INACTIVE_CACHE;
1800 bhv_vname_t *dentry,
1808 vn_trace_entry(dp, __FUNCTION__, (inst_t *)__return_address);
1810 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1811 return XFS_ERROR(EIO);
1813 lock_mode = xfs_ilock_map_shared(dp);
1814 error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
1816 *vpp = XFS_ITOV(ip);
1819 xfs_iunlock_map_shared(dp, lock_mode);
1826 bhv_vname_t *dentry,
1832 char *name = VNAME(dentry);
1833 xfs_mount_t *mp = dp->i_mount;
1834 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
1836 bhv_vnode_t *vp = NULL;
1839 xfs_bmap_free_t free_list;
1840 xfs_fsblock_t first_block;
1841 boolean_t unlock_dp_on_error = B_FALSE;
1842 int dm_event_sent = 0;
1846 struct xfs_dquot *udqp, *gdqp;
1851 vn_trace_entry(dp, __FUNCTION__, (inst_t *)__return_address);
1853 namelen = VNAMELEN(dentry);
1855 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1856 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1857 dir_vp, DM_RIGHT_NULL, NULL,
1858 DM_RIGHT_NULL, name, NULL,
1866 if (XFS_FORCED_SHUTDOWN(mp))
1867 return XFS_ERROR(EIO);
1869 /* Return through std_return after this point. */
1872 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1873 prid = dp->i_d.di_projid;
1875 prid = (xfs_prid_t)dfltprid;
1878 * Make sure that we have allocated dquot(s) on disk.
1880 error = XFS_QM_DQVOPALLOC(mp, dp,
1881 current_fsuid(credp), current_fsgid(credp), prid,
1882 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1888 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1889 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1890 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1892 * Initially assume that the file does not exist and
1893 * reserve the resources for that case. If that is not
1894 * the case we'll drop the one we have and get a more
1895 * appropriate transaction later.
1897 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1898 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1899 if (error == ENOSPC) {
1901 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1902 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1909 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1910 unlock_dp_on_error = B_TRUE;
1912 XFS_BMAP_INIT(&free_list, &first_block);
1917 * Reserve disk quota and the inode.
1919 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1923 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1925 error = xfs_dir_ialloc(&tp, dp, mode, 1,
1926 rdev, credp, prid, resblks > 0,
1929 if (error == ENOSPC)
1936 * At this point, we've gotten a newly allocated inode.
1937 * It is locked (and joined to the transaction).
1940 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1943 * Now we join the directory inode to the transaction. We do not do it
1944 * earlier because xfs_dir_ialloc might commit the previous transaction
1945 * (and release all the locks). An error from here on will result in
1946 * the transaction cancel unlocking dp so don't do it explicitly in the
1950 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1951 unlock_dp_on_error = B_FALSE;
1953 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
1954 &first_block, &free_list, resblks ?
1955 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1957 ASSERT(error != ENOSPC);
1960 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1961 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1964 * If this is a synchronous mount, make sure that the
1965 * create transaction goes to disk before returning to
1968 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1969 xfs_trans_set_sync(tp);
1975 * Attach the dquot(s) to the inodes and modify them incore.
1976 * These ids of the inode couldn't have changed since the new
1977 * inode has been locked ever since it was created.
1979 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1982 * xfs_trans_commit normally decrements the vnode ref count
1983 * when it unlocks the inode. Since we want to return the
1984 * vnode to the caller, we bump the vnode ref count now.
1989 error = xfs_bmap_finish(&tp, &free_list, &committed);
1991 xfs_bmap_cancel(&free_list);
1995 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2002 XFS_QM_DQRELE(mp, udqp);
2003 XFS_QM_DQRELE(mp, gdqp);
2007 /* Fallthrough to std_return with error = 0 */
2010 if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
2011 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2012 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2013 dir_vp, DM_RIGHT_NULL,
2015 DM_RIGHT_NULL, name, NULL,
2021 cancel_flags |= XFS_TRANS_ABORT;
2026 xfs_trans_cancel(tp, cancel_flags);
2028 XFS_QM_DQRELE(mp, udqp);
2029 XFS_QM_DQRELE(mp, gdqp);
2031 if (unlock_dp_on_error)
2032 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2038 * Wait until after the current transaction is aborted to
2039 * release the inode. This prevents recursive transactions
2040 * and deadlocks from xfs_inactive.
2042 cancel_flags |= XFS_TRANS_ABORT;
2043 xfs_trans_cancel(tp, cancel_flags);
2046 XFS_QM_DQRELE(mp, udqp);
2047 XFS_QM_DQRELE(mp, gdqp);
2054 * Some counters to see if (and how often) we are hitting some deadlock
2055 * prevention code paths.
2059 int xfs_rm_lock_delays;
2060 int xfs_rm_attempts;
2064 * The following routine will lock the inodes associated with the
2065 * directory and the named entry in the directory. The locks are
2066 * acquired in increasing inode number.
2068 * If the entry is "..", then only the directory is locked. The
2069 * vnode ref count will still include that from the .. entry in
2072 * There is a deadlock we need to worry about. If the locked directory is
2073 * in the AIL, it might be blocking up the log. The next inode we lock
2074 * could be already locked by another thread waiting for log space (e.g
2075 * a permanent log reservation with a long running transaction (see
2076 * xfs_itruncate_finish)). To solve this, we must check if the directory
2077 * is in the ail and use lock_nowait. If we can't lock, we need to
2078 * drop the inode lock on the directory and try again. xfs_iunlock will
2079 * potentially push the tail if we were holding up the log.
2082 xfs_lock_dir_and_entry(
2084 xfs_inode_t *ip) /* inode of entry 'name' */
2088 xfs_inode_t *ips[2];
2097 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2104 * We want to lock in increasing inum. Since we've already
2105 * acquired the lock on the directory, we may need to release
2106 * if if the inum of the entry turns out to be less.
2108 if (e_inum > dp->i_ino) {
2110 * We are already in the right order, so just
2111 * lock on the inode of the entry.
2112 * We need to use nowait if dp is in the AIL.
2115 lp = (xfs_log_item_t *)dp->i_itemp;
2116 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2117 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2124 * Unlock dp and try again.
2125 * xfs_iunlock will try to push the tail
2126 * if the inode is in the AIL.
2129 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2131 if ((attempts % 5) == 0) {
2132 delay(1); /* Don't just spin the CPU */
2134 xfs_rm_lock_delays++;
2140 xfs_ilock(ip, XFS_ILOCK_EXCL);
2142 } else if (e_inum < dp->i_ino) {
2143 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2147 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2149 /* else e_inum == dp->i_ino */
2150 /* This can happen if we're asked to lock /x/..
2151 * the entry is "..", which is also the parent directory.
2159 int xfs_small_retries;
2160 int xfs_middle_retries;
2161 int xfs_lots_retries;
2162 int xfs_lock_delays;
2166 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2170 xfs_lock_inumorder(int lock_mode, int subclass)
2172 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
2173 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
2174 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
2175 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
2181 * The following routine will lock n inodes in exclusive mode.
2182 * We assume the caller calls us with the inodes in i_ino order.
2184 * We need to detect deadlock where an inode that we lock
2185 * is in the AIL and we start waiting for another inode that is locked
2186 * by a thread in a long running transaction (such as truncate). This can
2187 * result in deadlock since the long running trans might need to wait
2188 * for the inode we just locked in order to push the tail and free space
2198 int attempts = 0, i, j, try_lock;
2201 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2212 for (; i < inodes; i++) {
2215 if (i && (ips[i] == ips[i-1])) /* Already locked */
2219 * If try_lock is not set yet, make sure all locked inodes
2220 * are not in the AIL.
2221 * If any are, set try_lock to be used later.
2225 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2226 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2227 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2234 * If any of the previous locks we have locked is in the AIL,
2235 * we must TRY to get the second and subsequent locks. If
2236 * we can't get any, we must release all we have
2241 /* try_lock must be 0 if i is 0. */
2243 * try_lock means we have an inode locked
2244 * that is in the AIL.
2247 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
2251 * Unlock all previous guys and try again.
2252 * xfs_iunlock will try to push the tail
2253 * if the inode is in the AIL.
2256 for(j = i - 1; j >= 0; j--) {
2259 * Check to see if we've already
2260 * unlocked this one.
2261 * Not the first one going back,
2262 * and the inode ptr is the same.
2264 if ((j != (i - 1)) && ips[j] ==
2268 xfs_iunlock(ips[j], lock_mode);
2271 if ((attempts % 5) == 0) {
2272 delay(1); /* Don't just spin the CPU */
2282 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
2288 if (attempts < 5) xfs_small_retries++;
2289 else if (attempts < 100) xfs_middle_retries++;
2290 else xfs_lots_retries++;
2298 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2299 int remove_which_error_return = 0;
2301 #define REMOVE_DEBUG_TRACE(x)
2302 #endif /* ! DEBUG */
2307 bhv_vname_t *dentry)
2309 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2310 char *name = VNAME(dentry);
2311 xfs_mount_t *mp = dp->i_mount;
2313 xfs_trans_t *tp = NULL;
2315 xfs_bmap_free_t free_list;
2316 xfs_fsblock_t first_block;
2324 vn_trace_entry(dp, __FUNCTION__, (inst_t *)__return_address);
2326 if (XFS_FORCED_SHUTDOWN(mp))
2327 return XFS_ERROR(EIO);
2329 namelen = VNAMELEN(dentry);
2331 if (!xfs_get_dir_entry(dentry, &ip)) {
2332 dm_di_mode = ip->i_d.di_mode;
2336 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2337 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2338 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2339 name, NULL, dm_di_mode, 0, 0);
2344 /* From this point on, return through std_return */
2348 * We need to get a reference to ip before we get our log
2349 * reservation. The reason for this is that we cannot call
2350 * xfs_iget for an inode for which we do not have a reference
2351 * once we've acquired a log reservation. This is because the
2352 * inode we are trying to get might be in xfs_inactive going
2353 * for a log reservation. Since we'll have to wait for the
2354 * inactive code to complete before returning from xfs_iget,
2355 * we need to make sure that we don't have log space reserved
2356 * when we call xfs_iget. Instead we get an unlocked reference
2357 * to the inode before getting our log reservation.
2359 error = xfs_get_dir_entry(dentry, &ip);
2361 REMOVE_DEBUG_TRACE(__LINE__);
2365 dm_di_mode = ip->i_d.di_mode;
2367 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
2371 error = XFS_QM_DQATTACH(mp, dp, 0);
2372 if (!error && dp != ip)
2373 error = XFS_QM_DQATTACH(mp, ip, 0);
2375 REMOVE_DEBUG_TRACE(__LINE__);
2380 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2381 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2383 * We try to get the real space reservation first,
2384 * allowing for directory btree deletion(s) implying
2385 * possible bmap insert(s). If we can't get the space
2386 * reservation then we use 0 instead, and avoid the bmap
2387 * btree insert(s) in the directory code by, if the bmap
2388 * insert tries to happen, instead trimming the LAST
2389 * block from the directory.
2391 resblks = XFS_REMOVE_SPACE_RES(mp);
2392 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2393 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2394 if (error == ENOSPC) {
2396 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2397 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2400 ASSERT(error != ENOSPC);
2401 REMOVE_DEBUG_TRACE(__LINE__);
2402 xfs_trans_cancel(tp, 0);
2407 error = xfs_lock_dir_and_entry(dp, ip);
2409 REMOVE_DEBUG_TRACE(__LINE__);
2410 xfs_trans_cancel(tp, cancel_flags);
2416 * At this point, we've gotten both the directory and the entry
2419 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2422 * Increment vnode ref count only in this case since
2423 * there's an extra vnode reference in the case where
2427 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2431 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2433 XFS_BMAP_INIT(&free_list, &first_block);
2434 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2435 &first_block, &free_list, 0);
2437 ASSERT(error != ENOENT);
2438 REMOVE_DEBUG_TRACE(__LINE__);
2441 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2444 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2446 error = xfs_droplink(tp, ip);
2448 REMOVE_DEBUG_TRACE(__LINE__);
2452 /* Determine if this is the last link while
2453 * we are in the transaction.
2455 link_zero = (ip)->i_d.di_nlink==0;
2458 * Take an extra ref on the inode so that it doesn't
2459 * go to xfs_inactive() from within the commit.
2464 * If this is a synchronous mount, make sure that the
2465 * remove transaction goes to disk before returning to
2468 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2469 xfs_trans_set_sync(tp);
2472 error = xfs_bmap_finish(&tp, &free_list, &committed);
2474 REMOVE_DEBUG_TRACE(__LINE__);
2478 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2485 * Before we drop our extra reference to the inode, purge it
2486 * from the refcache if it is there. By waiting until afterwards
2487 * to do the IRELE, we ensure that we won't go inactive in the
2488 * xfs_refcache_purge_ip routine (although that would be OK).
2490 xfs_refcache_purge_ip(ip);
2493 * If we are using filestreams, kill the stream association.
2494 * If the file is still open it may get a new one but that
2495 * will get killed on last close in xfs_close() so we don't
2496 * have to worry about that.
2498 if (link_zero && xfs_inode_is_filestream(ip))
2499 xfs_filestream_deassociate(ip);
2501 vn_trace_exit(ip, __FUNCTION__, (inst_t *)__return_address);
2505 /* Fall through to std_return with error = 0 */
2507 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2508 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2509 dir_vp, DM_RIGHT_NULL,
2510 NULL, DM_RIGHT_NULL,
2511 name, NULL, dm_di_mode, error, 0);
2516 xfs_bmap_cancel(&free_list);
2517 cancel_flags |= XFS_TRANS_ABORT;
2518 xfs_trans_cancel(tp, cancel_flags);
2523 * In this case make sure to not release the inode until after
2524 * the current transaction is aborted. Releasing it beforehand
2525 * can cause us to go to xfs_inactive and start a recursive
2526 * transaction which can easily deadlock with the current one.
2528 xfs_bmap_cancel(&free_list);
2529 cancel_flags |= XFS_TRANS_ABORT;
2530 xfs_trans_cancel(tp, cancel_flags);
2533 * Before we drop our extra reference to the inode, purge it
2534 * from the refcache if it is there. By waiting until afterwards
2535 * to do the IRELE, we ensure that we won't go inactive in the
2536 * xfs_refcache_purge_ip routine (although that would be OK).
2538 xfs_refcache_purge_ip(ip);
2548 bhv_vnode_t *src_vp,
2549 bhv_vname_t *dentry)
2551 bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
2552 xfs_mount_t *mp = tdp->i_mount;
2553 xfs_inode_t *sip = xfs_vtoi(src_vp);
2555 xfs_inode_t *ips[2];
2557 xfs_bmap_free_t free_list;
2558 xfs_fsblock_t first_block;
2562 char *target_name = VNAME(dentry);
2565 vn_trace_entry(tdp, __FUNCTION__, (inst_t *)__return_address);
2566 vn_trace_entry(xfs_vtoi(src_vp), __FUNCTION__, (inst_t *)__return_address);
2568 target_namelen = VNAMELEN(dentry);
2569 ASSERT(!VN_ISDIR(src_vp));
2571 if (XFS_FORCED_SHUTDOWN(mp))
2572 return XFS_ERROR(EIO);
2574 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2575 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2576 target_dir_vp, DM_RIGHT_NULL,
2577 src_vp, DM_RIGHT_NULL,
2578 target_name, NULL, 0, 0, 0);
2583 /* Return through std_return after this point. */
2585 error = XFS_QM_DQATTACH(mp, sip, 0);
2586 if (!error && sip != tdp)
2587 error = XFS_QM_DQATTACH(mp, tdp, 0);
2591 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2592 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2593 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2594 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2595 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2596 if (error == ENOSPC) {
2598 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2599 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2606 if (sip->i_ino < tdp->i_ino) {
2614 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2617 * Increment vnode ref counts since xfs_trans_commit &
2618 * xfs_trans_cancel will both unlock the inodes and
2619 * decrement the associated ref counts.
2622 VN_HOLD(target_dir_vp);
2623 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2624 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2627 * If the source has too many links, we can't make any more to it.
2629 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2630 error = XFS_ERROR(EMLINK);
2635 * If we are using project inheritance, we only allow hard link
2636 * creation in our tree when the project IDs are the same; else
2637 * the tree quota mechanism could be circumvented.
2639 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2640 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2641 error = XFS_ERROR(EXDEV);
2646 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2649 XFS_BMAP_INIT(&free_list, &first_block);
2651 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2652 sip->i_ino, &first_block, &free_list,
2656 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2658 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2660 error = xfs_bumplink(tp, sip);
2665 * If this is a synchronous mount, make sure that the
2666 * link transaction goes to disk before returning to
2669 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2670 xfs_trans_set_sync(tp);
2673 error = xfs_bmap_finish (&tp, &free_list, &committed);
2675 xfs_bmap_cancel(&free_list);
2679 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2683 /* Fall through to std_return with error = 0. */
2685 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2686 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2687 target_dir_vp, DM_RIGHT_NULL,
2688 src_vp, DM_RIGHT_NULL,
2689 target_name, NULL, 0, error, 0);
2694 cancel_flags |= XFS_TRANS_ABORT;
2698 xfs_trans_cancel(tp, cancel_flags);
2706 bhv_vname_t *dentry,
2711 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2712 char *dir_name = VNAME(dentry);
2713 int dir_namelen = VNAMELEN(dentry);
2714 xfs_mount_t *mp = dp->i_mount;
2715 xfs_inode_t *cdp; /* inode of created dir */
2716 bhv_vnode_t *cvp; /* vnode of created dir */
2721 xfs_bmap_free_t free_list;
2722 xfs_fsblock_t first_block;
2723 boolean_t unlock_dp_on_error = B_FALSE;
2724 boolean_t created = B_FALSE;
2725 int dm_event_sent = 0;
2727 struct xfs_dquot *udqp, *gdqp;
2730 if (XFS_FORCED_SHUTDOWN(mp))
2731 return XFS_ERROR(EIO);
2735 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
2736 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2737 dir_vp, DM_RIGHT_NULL, NULL,
2738 DM_RIGHT_NULL, dir_name, NULL,
2745 /* Return through std_return after this point. */
2747 vn_trace_entry(dp, __FUNCTION__, (inst_t *)__return_address);
2751 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2752 prid = dp->i_d.di_projid;
2754 prid = (xfs_prid_t)dfltprid;
2757 * Make sure that we have allocated dquot(s) on disk.
2759 error = XFS_QM_DQVOPALLOC(mp, dp,
2760 current_fsuid(credp), current_fsgid(credp), prid,
2761 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2765 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2766 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2767 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2768 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2769 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2770 if (error == ENOSPC) {
2772 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2773 XFS_TRANS_PERM_LOG_RES,
2774 XFS_MKDIR_LOG_COUNT);
2781 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2782 unlock_dp_on_error = B_TRUE;
2785 * Check for directory link count overflow.
2787 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2788 error = XFS_ERROR(EMLINK);
2793 * Reserve disk quota and the inode.
2795 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2800 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2803 * create the directory inode.
2805 error = xfs_dir_ialloc(&tp, dp, mode, 2,
2806 0, credp, prid, resblks > 0,
2809 if (error == ENOSPC)
2816 * Now we add the directory inode to the transaction.
2817 * We waited until now since xfs_dir_ialloc might start
2818 * a new transaction. Had we joined the transaction
2819 * earlier, the locks might have gotten released. An error
2820 * from here on will result in the transaction cancel
2821 * unlocking dp so don't do it explicitly in the error path.
2824 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2825 unlock_dp_on_error = B_FALSE;
2827 XFS_BMAP_INIT(&free_list, &first_block);
2829 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2830 &first_block, &free_list, resblks ?
2831 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2833 ASSERT(error != ENOSPC);
2836 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2839 * Bump the in memory version number of the parent directory
2840 * so that other processes accessing it will recognize that
2841 * the directory has changed.
2845 error = xfs_dir_init(tp, cdp, dp);
2850 error = xfs_bumplink(tp, dp);
2854 cvp = XFS_ITOV(cdp);
2862 * Attach the dquots to the new inode and modify the icount incore.
2864 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2867 * If this is a synchronous mount, make sure that the
2868 * mkdir transaction goes to disk before returning to
2871 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2872 xfs_trans_set_sync(tp);
2875 error = xfs_bmap_finish(&tp, &free_list, &committed);
2881 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2882 XFS_QM_DQRELE(mp, udqp);
2883 XFS_QM_DQRELE(mp, gdqp);
2888 /* Fall through to std_return with error = 0 or errno from
2889 * xfs_trans_commit. */
2892 if ((created || (error != 0 && dm_event_sent != 0)) &&
2893 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2894 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2895 dir_vp, DM_RIGHT_NULL,
2896 created ? XFS_ITOV(cdp):NULL,
2905 xfs_bmap_cancel(&free_list);
2907 cancel_flags |= XFS_TRANS_ABORT;
2909 xfs_trans_cancel(tp, cancel_flags);
2910 XFS_QM_DQRELE(mp, udqp);
2911 XFS_QM_DQRELE(mp, gdqp);
2913 if (unlock_dp_on_error)
2914 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2922 bhv_vname_t *dentry)
2924 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2925 char *name = VNAME(dentry);
2926 int namelen = VNAMELEN(dentry);
2927 xfs_mount_t *mp = dp->i_mount;
2928 xfs_inode_t *cdp; /* child directory */
2931 xfs_bmap_free_t free_list;
2932 xfs_fsblock_t first_block;
2935 int dm_di_mode = S_IFDIR;
2939 vn_trace_entry(dp, __FUNCTION__, (inst_t *)__return_address);
2941 if (XFS_FORCED_SHUTDOWN(mp))
2942 return XFS_ERROR(EIO);
2944 if (!xfs_get_dir_entry(dentry, &cdp)) {
2945 dm_di_mode = cdp->i_d.di_mode;
2949 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2950 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
2951 dir_vp, DM_RIGHT_NULL,
2952 NULL, DM_RIGHT_NULL,
2953 name, NULL, dm_di_mode, 0, 0);
2955 return XFS_ERROR(error);
2958 /* Return through std_return after this point. */
2963 * We need to get a reference to cdp before we get our log
2964 * reservation. The reason for this is that we cannot call
2965 * xfs_iget for an inode for which we do not have a reference
2966 * once we've acquired a log reservation. This is because the
2967 * inode we are trying to get might be in xfs_inactive going
2968 * for a log reservation. Since we'll have to wait for the
2969 * inactive code to complete before returning from xfs_iget,
2970 * we need to make sure that we don't have log space reserved
2971 * when we call xfs_iget. Instead we get an unlocked reference
2972 * to the inode before getting our log reservation.
2974 error = xfs_get_dir_entry(dentry, &cdp);
2976 REMOVE_DEBUG_TRACE(__LINE__);
2980 dm_di_mode = cdp->i_d.di_mode;
2983 * Get the dquots for the inodes.
2985 error = XFS_QM_DQATTACH(mp, dp, 0);
2986 if (!error && dp != cdp)
2987 error = XFS_QM_DQATTACH(mp, cdp, 0);
2990 REMOVE_DEBUG_TRACE(__LINE__);
2994 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2995 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2997 * We try to get the real space reservation first,
2998 * allowing for directory btree deletion(s) implying
2999 * possible bmap insert(s). If we can't get the space
3000 * reservation then we use 0 instead, and avoid the bmap
3001 * btree insert(s) in the directory code by, if the bmap
3002 * insert tries to happen, instead trimming the LAST
3003 * block from the directory.
3005 resblks = XFS_REMOVE_SPACE_RES(mp);
3006 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3007 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3008 if (error == ENOSPC) {
3010 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3011 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3014 ASSERT(error != ENOSPC);
3019 XFS_BMAP_INIT(&free_list, &first_block);
3022 * Now lock the child directory inode and the parent directory
3023 * inode in the proper order. This will take care of validating
3024 * that the directory entry for the child directory inode has
3025 * not changed while we were obtaining a log reservation.
3027 error = xfs_lock_dir_and_entry(dp, cdp);
3029 xfs_trans_cancel(tp, cancel_flags);
3034 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3037 * Only increment the parent directory vnode count if
3038 * we didn't bump it in looking up cdp. The only time
3039 * we don't bump it is when we're looking up ".".
3045 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3047 ASSERT(cdp->i_d.di_nlink >= 2);
3048 if (cdp->i_d.di_nlink != 2) {
3049 error = XFS_ERROR(ENOTEMPTY);
3052 if (!xfs_dir_isempty(cdp)) {
3053 error = XFS_ERROR(ENOTEMPTY);
3057 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3058 &first_block, &free_list, resblks);
3062 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3065 * Bump the in memory generation count on the parent
3066 * directory so that other can know that it has changed.
3071 * Drop the link from cdp's "..".
3073 error = xfs_droplink(tp, dp);
3079 * Drop the link from dp to cdp.
3081 error = xfs_droplink(tp, cdp);
3087 * Drop the "." link from cdp to self.
3089 error = xfs_droplink(tp, cdp);
3094 /* Determine these before committing transaction */
3095 last_cdp_link = (cdp)->i_d.di_nlink==0;
3098 * Take an extra ref on the child vnode so that it
3099 * does not go to xfs_inactive() from within the commit.
3104 * If this is a synchronous mount, make sure that the
3105 * rmdir transaction goes to disk before returning to
3108 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3109 xfs_trans_set_sync(tp);
3112 error = xfs_bmap_finish (&tp, &free_list, &committed);
3114 xfs_bmap_cancel(&free_list);
3115 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3121 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3130 /* Fall through to std_return with error = 0 or the errno
3131 * from xfs_trans_commit. */
3133 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
3134 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3135 dir_vp, DM_RIGHT_NULL,
3136 NULL, DM_RIGHT_NULL,
3137 name, NULL, dm_di_mode,
3143 xfs_bmap_cancel(&free_list);
3144 cancel_flags |= XFS_TRANS_ABORT;
3148 xfs_trans_cancel(tp, cancel_flags);
3155 bhv_vname_t *dentry,
3161 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
3162 xfs_mount_t *mp = dp->i_mount;
3167 xfs_bmap_free_t free_list;
3168 xfs_fsblock_t first_block;
3169 boolean_t unlock_dp_on_error = B_FALSE;
3172 xfs_fileoff_t first_fsb;
3173 xfs_filblks_t fs_blocks;
3175 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3182 struct xfs_dquot *udqp, *gdqp;
3184 char *link_name = VNAME(dentry);
3192 vn_trace_entry(dp, __FUNCTION__, (inst_t *)__return_address);
3195 if (XFS_FORCED_SHUTDOWN(mp))
3196 return XFS_ERROR(EIO);
3198 link_namelen = VNAMELEN(dentry);
3201 * Check component lengths of the target path name.
3203 pathlen = strlen(target_path);
3204 if (pathlen >= MAXPATHLEN) /* total string too long */
3205 return XFS_ERROR(ENAMETOOLONG);
3206 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3210 for (total = 0, path = target_path; total < pathlen;) {
3214 while(*path == '/') {
3220 * Count up to the next slash or end of path.
3221 * Error out if the component is bigger than MAXNAMELEN.
3223 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3224 if (++len >= MAXNAMELEN) {
3225 error = ENAMETOOLONG;
3232 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
3233 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3234 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3235 link_name, target_path, 0, 0, 0);
3240 /* Return through std_return after this point. */
3243 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3244 prid = dp->i_d.di_projid;
3246 prid = (xfs_prid_t)dfltprid;
3249 * Make sure that we have allocated dquot(s) on disk.
3251 error = XFS_QM_DQVOPALLOC(mp, dp,
3252 current_fsuid(credp), current_fsgid(credp), prid,
3253 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3257 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3258 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3260 * The symlink will fit into the inode data fork?
3261 * There can't be any attributes so we get the whole variable part.
3263 if (pathlen <= XFS_LITINO(mp))
3266 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3267 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3268 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3269 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3270 if (error == ENOSPC && fs_blocks == 0) {
3272 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3273 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3280 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
3281 unlock_dp_on_error = B_TRUE;
3284 * Check whether the directory allows new symlinks or not.
3286 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3287 error = XFS_ERROR(EPERM);
3292 * Reserve disk quota : blocks and inode.
3294 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3299 * Check for ability to enter directory entry, if no space reserved.
3302 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
3305 * Initialize the bmap freelist prior to calling either
3306 * bmapi or the directory create code.
3308 XFS_BMAP_INIT(&free_list, &first_block);
3311 * Allocate an inode for the symlink.
3313 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
3314 1, 0, credp, prid, resblks > 0, &ip, NULL);
3316 if (error == ENOSPC)
3323 * An error after we've joined dp to the transaction will result in the
3324 * transaction cancel unlocking dp so don't do it explicitly in the
3328 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3329 unlock_dp_on_error = B_FALSE;
3332 * Also attach the dquot(s) to it, if applicable.
3334 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3337 resblks -= XFS_IALLOC_SPACE_RES(mp);
3339 * If the symlink will fit into the inode, write it inline.
3341 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3342 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3343 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3344 ip->i_d.di_size = pathlen;
3347 * The inode was initially created in extent format.
3349 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3350 ip->i_df.if_flags |= XFS_IFINLINE;
3352 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3353 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3357 nmaps = SYMLINK_MAPS;
3359 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3360 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3361 &first_block, resblks, mval, &nmaps,
3368 resblks -= fs_blocks;
3369 ip->i_d.di_size = pathlen;
3370 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3372 cur_chunk = target_path;
3373 for (n = 0; n < nmaps; n++) {
3374 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3375 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3376 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3377 BTOBB(byte_cnt), 0);
3378 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3379 if (pathlen < byte_cnt) {
3382 pathlen -= byte_cnt;
3384 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3385 cur_chunk += byte_cnt;
3387 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3392 * Create the directory entry for the symlink.
3394 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3395 &first_block, &free_list, resblks);
3398 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3399 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3402 * Bump the in memory version number of the parent directory
3403 * so that other processes accessing it will recognize that
3404 * the directory has changed.
3409 * If this is a synchronous mount, make sure that the
3410 * symlink transaction goes to disk before returning to
3413 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3414 xfs_trans_set_sync(tp);
3418 * xfs_trans_commit normally decrements the vnode ref count
3419 * when it unlocks the inode. Since we want to return the
3420 * vnode to the caller, we bump the vnode ref count now.
3424 error = xfs_bmap_finish(&tp, &free_list, &committed);
3428 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3429 XFS_QM_DQRELE(mp, udqp);
3430 XFS_QM_DQRELE(mp, gdqp);
3432 /* Fall through to std_return with error = 0 or errno from
3433 * xfs_trans_commit */
3435 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
3436 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3437 dir_vp, DM_RIGHT_NULL,
3438 error ? NULL : XFS_ITOV(ip),
3439 DM_RIGHT_NULL, link_name, target_path,
3455 xfs_bmap_cancel(&free_list);
3456 cancel_flags |= XFS_TRANS_ABORT;
3458 xfs_trans_cancel(tp, cancel_flags);
3459 XFS_QM_DQRELE(mp, udqp);
3460 XFS_QM_DQRELE(mp, gdqp);
3462 if (unlock_dp_on_error)
3463 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3472 * A fid routine that takes a pointer to a previously allocated
3473 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3480 xfs_fid2_t *xfid = (xfs_fid2_t *)fidp;
3482 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
3483 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3485 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3488 * use memcpy because the inode is a long long and there's no
3489 * assurance that xfid->fid_ino is properly aligned.
3491 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3492 xfid->fid_gen = ip->i_d.di_gen;
3501 bhv_vrwlock_t locktype)
3503 if (S_ISDIR(ip->i_d.di_mode))
3505 if (locktype == VRWLOCK_WRITE) {
3506 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3507 } else if (locktype == VRWLOCK_TRY_READ) {
3508 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
3509 } else if (locktype == VRWLOCK_TRY_WRITE) {
3510 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
3512 ASSERT((locktype == VRWLOCK_READ) ||
3513 (locktype == VRWLOCK_WRITE_DIRECT));
3514 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3524 bhv_vrwlock_t locktype)
3526 if (S_ISDIR(ip->i_d.di_mode))
3528 if (locktype == VRWLOCK_WRITE) {
3530 * In the write case, we may have added a new entry to
3531 * the reference cache. This might store a pointer to
3532 * an inode to be released in this inode. If it is there,
3533 * clear the pointer and release the inode after unlocking
3536 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3538 ASSERT((locktype == VRWLOCK_READ) ||
3539 (locktype == VRWLOCK_WRITE_DIRECT));
3540 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3551 xfs_mount_t *mp = ip->i_mount;
3552 xfs_inode_log_item_t *iip = ip->i_itemp;
3555 if (XFS_FORCED_SHUTDOWN(mp))
3556 return XFS_ERROR(EIO);
3559 * Bypass inodes which have already been cleaned by
3560 * the inode flush clustering code inside xfs_iflush
3562 if ((ip->i_update_core == 0) &&
3563 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3566 if (flags & FLUSH_LOG) {
3567 if (iip && iip->ili_last_lsn) {
3568 xlog_t *log = mp->m_log;
3570 int s, log_flags = XFS_LOG_FORCE;
3572 s = GRANT_LOCK(log);
3573 sync_lsn = log->l_last_sync_lsn;
3574 GRANT_UNLOCK(log, s);
3576 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) > 0)) {
3577 if (flags & FLUSH_SYNC)
3578 log_flags |= XFS_LOG_SYNC;
3579 error = xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3584 if (ip->i_update_core == 0)
3590 * We make this non-blocking if the inode is contended,
3591 * return EAGAIN to indicate to the caller that they
3592 * did not succeed. This prevents the flush path from
3593 * blocking on inodes inside another operation right
3594 * now, they get caught later by xfs_sync.
3596 if (flags & FLUSH_INODE) {
3599 if (flags & FLUSH_SYNC) {
3600 xfs_ilock(ip, XFS_ILOCK_SHARED);
3602 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3603 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3604 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3611 if (flags & FLUSH_SYNC)
3612 flush_flags = XFS_IFLUSH_SYNC;
3614 flush_flags = XFS_IFLUSH_ASYNC;
3616 error = xfs_iflush(ip, flush_flags);
3617 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3630 xfs_mount_t *mp = ip->i_mount;
3634 if (!capable(CAP_SYS_ADMIN))
3635 return XFS_ERROR(EPERM);
3637 if (XFS_FORCED_SHUTDOWN(mp))
3638 return XFS_ERROR(EIO);
3640 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3641 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3643 xfs_trans_cancel(tp, 0);
3646 xfs_ilock(ip, XFS_ILOCK_EXCL);
3647 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3649 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3650 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3652 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3654 error = xfs_trans_commit(tp, 0);
3663 bhv_vnode_t *vp = XFS_ITOV(ip);
3665 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
3667 ASSERT(!VN_MAPPED(vp));
3669 /* bad inode, get out here ASAP */
3677 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3680 * Make sure the atime in the XFS inode is correct before freeing the
3683 xfs_synchronize_atime(ip);
3686 * If we have nothing to flush with this inode then complete the
3687 * teardown now, otherwise break the link between the xfs inode and the
3688 * linux inode and clean up the xfs inode later. This avoids flushing
3689 * the inode to disk during the delete operation itself.
3691 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3692 * first to ensure that xfs_iunpin() will never see an xfs inode
3693 * that has a linux inode being reclaimed. Synchronisation is provided
3694 * by the i_flags_lock.
3696 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3697 xfs_ilock(ip, XFS_ILOCK_EXCL);
3699 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3701 xfs_mount_t *mp = ip->i_mount;
3703 /* Protect sync and unpin from us */
3704 XFS_MOUNT_ILOCK(mp);
3705 spin_lock(&ip->i_flags_lock);
3706 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
3707 vn_to_inode(vp)->i_private = NULL;
3709 spin_unlock(&ip->i_flags_lock);
3710 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3711 XFS_MOUNT_IUNLOCK(mp);
3722 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
3723 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3726 if (vp && VN_BAD(vp))
3729 /* The hash lock here protects a thread in xfs_iget_core from
3730 * racing with us on linking the inode back with a vnode.
3731 * Once we have the XFS_IRECLAIM flag set it will not touch
3734 write_lock(&pag->pag_ici_lock);
3735 spin_lock(&ip->i_flags_lock);
3736 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3737 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
3738 spin_unlock(&ip->i_flags_lock);
3739 write_unlock(&pag->pag_ici_lock);
3742 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3746 __xfs_iflags_set(ip, XFS_IRECLAIM);
3747 spin_unlock(&ip->i_flags_lock);
3748 write_unlock(&pag->pag_ici_lock);
3749 xfs_put_perag(ip->i_mount, pag);
3752 * If the inode is still dirty, then flush it out. If the inode
3753 * is not in the AIL, then it will be OK to flush it delwri as
3754 * long as xfs_iflush() does not keep any references to the inode.
3755 * We leave that decision up to xfs_iflush() since it has the
3756 * knowledge of whether it's OK to simply do a delwri flush of
3757 * the inode or whether we need to wait until the inode is
3758 * pulled from the AIL.
3759 * We get the flush lock regardless, though, just to make sure
3760 * we don't free it while it is being flushed.
3762 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3764 xfs_ilock(ip, XFS_ILOCK_EXCL);
3768 if (ip->i_update_core ||
3769 ((ip->i_itemp != NULL) &&
3770 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3771 error = xfs_iflush(ip, sync_mode);
3773 * If we hit an error, typically because of filesystem
3774 * shutdown, we don't need to let vn_reclaim to know
3775 * because we're gonna reclaim the inode anyway.
3778 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3781 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3784 ASSERT(ip->i_update_core == 0);
3785 ASSERT(ip->i_itemp == NULL ||
3786 ip->i_itemp->ili_format.ilf_fields == 0);
3787 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3788 } else if (locked) {
3790 * We are not interested in doing an iflush if we're
3791 * in the process of shutting down the filesystem forcibly.
3792 * So, just reclaim the inode.
3795 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3804 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3807 xfs_inode_t *ip, *n;
3812 XFS_MOUNT_ILOCK(mp);
3813 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3815 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3817 if (xfs_ipincount(ip) ||
3818 !xfs_iflock_nowait(ip)) {
3819 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3823 XFS_MOUNT_IUNLOCK(mp);
3824 if (xfs_finish_reclaim(ip, noblock,
3825 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3834 XFS_MOUNT_IUNLOCK(mp);
3839 * xfs_alloc_file_space()
3840 * This routine allocates disk space for the given file.
3842 * If alloc_type == 0, this request is for an ALLOCSP type
3843 * request which will change the file size. In this case, no
3844 * DMAPI event will be generated by the call. A TRUNCATE event
3845 * will be generated later by xfs_setattr.
3847 * If alloc_type != 0, this request is for a RESVSP type
3848 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3849 * lower block boundary byte address is less than the file's
3858 xfs_alloc_file_space(
3865 xfs_mount_t *mp = ip->i_mount;
3867 xfs_filblks_t allocated_fsb;
3868 xfs_filblks_t allocatesize_fsb;
3869 xfs_extlen_t extsz, temp;
3870 xfs_fileoff_t startoffset_fsb;
3871 xfs_fsblock_t firstfsb;
3877 xfs_bmbt_irec_t imaps[1], *imapp;
3878 xfs_bmap_free_t free_list;
3879 uint qblocks, resblks, resrtextents;
3883 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
3885 if (XFS_FORCED_SHUTDOWN(mp))
3886 return XFS_ERROR(EIO);
3888 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3892 return XFS_ERROR(EINVAL);
3894 rt = XFS_IS_REALTIME_INODE(ip);
3895 extsz = xfs_get_extsz_hint(ip);
3900 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
3901 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3902 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3904 /* Generate a DMAPI event if needed. */
3905 if (alloc_type != 0 && offset < ip->i_size &&
3906 (attr_flags&ATTR_DMI) == 0 &&
3907 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3908 xfs_off_t end_dmi_offset;
3910 end_dmi_offset = offset+len;
3911 if (end_dmi_offset > ip->i_size)
3912 end_dmi_offset = ip->i_size;
3913 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
3914 offset, end_dmi_offset - offset,
3921 * Allocate file space until done or until there is an error
3924 while (allocatesize_fsb && !error) {
3928 * Determine space reservations for data/realtime.
3930 if (unlikely(extsz)) {
3931 s = startoffset_fsb;
3934 e = startoffset_fsb + allocatesize_fsb;
3935 if ((temp = do_mod(startoffset_fsb, extsz)))
3937 if ((temp = do_mod(e, extsz)))
3941 e = allocatesize_fsb;
3945 resrtextents = qblocks = (uint)(e - s);
3946 resrtextents /= mp->m_sb.sb_rextsize;
3947 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3948 quota_flag = XFS_QMOPT_RES_RTBLKS;
3951 resblks = qblocks = \
3952 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3953 quota_flag = XFS_QMOPT_RES_REGBLKS;
3957 * Allocate and setup the transaction.
3959 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3960 error = xfs_trans_reserve(tp, resblks,
3961 XFS_WRITE_LOG_RES(mp), resrtextents,
3962 XFS_TRANS_PERM_LOG_RES,
3963 XFS_WRITE_LOG_COUNT);
3965 * Check for running out of space
3969 * Free the transaction structure.
3971 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3972 xfs_trans_cancel(tp, 0);
3975 xfs_ilock(ip, XFS_ILOCK_EXCL);
3976 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3977 qblocks, 0, quota_flag);
3981 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3982 xfs_trans_ihold(tp, ip);
3985 * Issue the xfs_bmapi() call to allocate the blocks
3987 XFS_BMAP_INIT(&free_list, &firstfsb);
3988 error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
3989 allocatesize_fsb, bmapi_flag,
3990 &firstfsb, 0, imapp, &nimaps,
3997 * Complete the transaction
3999 error = xfs_bmap_finish(&tp, &free_list, &committed);
4004 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4005 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4010 allocated_fsb = imapp->br_blockcount;
4013 error = XFS_ERROR(ENOSPC);
4017 startoffset_fsb += allocated_fsb;
4018 allocatesize_fsb -= allocated_fsb;
4021 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
4022 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
4023 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4024 XFS_ITOV(ip), DM_RIGHT_NULL,
4025 XFS_ITOV(ip), DM_RIGHT_NULL,
4026 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4028 goto retry; /* Maybe DMAPI app. has made space */
4029 /* else fall through with error from XFS_SEND_DATA */
4034 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
4035 xfs_bmap_cancel(&free_list);
4036 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4038 error1: /* Just cancel transaction */
4039 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4040 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4041 goto dmapi_enospc_check;
4045 * Zero file bytes between startoff and endoff inclusive.
4046 * The iolock is held exclusive and no blocks are buffered.
4049 xfs_zero_remaining_bytes(
4054 xfs_bmbt_irec_t imap;
4055 xfs_fileoff_t offset_fsb;
4056 xfs_off_t lastoffset;
4059 xfs_mount_t *mp = ip->i_mount;
4063 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4064 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4065 mp->m_rtdev_targp : mp->m_ddev_targp);
4067 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4068 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4070 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
4071 NULL, 0, &imap, &nimap, NULL, NULL);
4072 if (error || nimap < 1)
4074 ASSERT(imap.br_blockcount >= 1);
4075 ASSERT(imap.br_startoff == offset_fsb);
4076 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4077 if (lastoffset > endoff)
4078 lastoffset = endoff;
4079 if (imap.br_startblock == HOLESTARTBLOCK)
4081 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4082 if (imap.br_state == XFS_EXT_UNWRITTEN)
4085 XFS_BUF_UNWRITE(bp);
4087 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4089 if ((error = xfs_iowait(bp))) {
4090 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4091 mp, bp, XFS_BUF_ADDR(bp));
4094 memset(XFS_BUF_PTR(bp) +
4095 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4096 0, lastoffset - offset + 1);
4101 if ((error = xfs_iowait(bp))) {
4102 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4103 mp, bp, XFS_BUF_ADDR(bp));
4112 * xfs_free_file_space()
4113 * This routine frees disk space for the given file.
4115 * This routine is only called by xfs_change_file_space
4116 * for an UNRESVSP type call.
4124 xfs_free_file_space(
4133 xfs_off_t end_dmi_offset;
4134 xfs_fileoff_t endoffset_fsb;
4136 xfs_fsblock_t firstfsb;
4137 xfs_bmap_free_t free_list;
4138 xfs_bmbt_irec_t imap;
4146 xfs_fileoff_t startoffset_fsb;
4148 int need_iolock = 1;
4153 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
4155 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4159 if (len <= 0) /* if nothing being freed */
4161 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4162 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4163 end_dmi_offset = offset + len;
4164 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4166 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
4167 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
4168 if (end_dmi_offset > ip->i_size)
4169 end_dmi_offset = ip->i_size;
4170 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4171 offset, end_dmi_offset - offset,
4172 AT_DELAY_FLAG(attr_flags), NULL);
4177 if (attr_flags & ATTR_NOLOCK)
4180 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4181 vn_iowait(ip); /* wait for the completion of any pending DIOs */
4184 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, NBPP);
4185 ioffset = offset & ~(rounding - 1);
4187 if (VN_CACHED(vp) != 0) {
4188 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4189 ctooff(offtoct(ioffset)), -1);
4190 error = xfs_flushinval_pages(ip,
4191 ctooff(offtoct(ioffset)),
4192 -1, FI_REMAPF_LOCKED);
4194 goto out_unlock_iolock;
4198 * Need to zero the stuff we're not freeing, on disk.
4199 * If its a realtime file & can't use unwritten extents then we
4200 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4201 * will take care of it for us.
4203 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4205 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
4206 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4208 goto out_unlock_iolock;
4209 ASSERT(nimap == 0 || nimap == 1);
4210 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4213 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4214 block = imap.br_startblock;
4215 mod = do_div(block, mp->m_sb.sb_rextsize);
4217 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4220 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
4221 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4223 goto out_unlock_iolock;
4224 ASSERT(nimap == 0 || nimap == 1);
4225 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4226 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4228 if (mod && (mod != mp->m_sb.sb_rextsize))
4229 endoffset_fsb -= mod;
4232 if ((done = (endoffset_fsb <= startoffset_fsb)))
4234 * One contiguous piece to clear
4236 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4239 * Some full blocks, possibly two pieces to clear
4241 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4242 error = xfs_zero_remaining_bytes(ip, offset,
4243 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4245 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4246 error = xfs_zero_remaining_bytes(ip,
4247 XFS_FSB_TO_B(mp, endoffset_fsb),
4252 * free file space until done or until there is an error
4254 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4255 while (!error && !done) {
4258 * allocate and setup the transaction. Allow this
4259 * transaction to dip into the reserve blocks to ensure
4260 * the freeing of the space succeeds at ENOSPC.
4262 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4263 tp->t_flags |= XFS_TRANS_RESERVE;
4264 error = xfs_trans_reserve(tp,
4266 XFS_WRITE_LOG_RES(mp),
4268 XFS_TRANS_PERM_LOG_RES,
4269 XFS_WRITE_LOG_COUNT);
4272 * check for running out of space
4276 * Free the transaction structure.
4278 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4279 xfs_trans_cancel(tp, 0);
4282 xfs_ilock(ip, XFS_ILOCK_EXCL);
4283 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4284 ip->i_udquot, ip->i_gdquot, resblks, 0,
4285 XFS_QMOPT_RES_REGBLKS);
4289 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4290 xfs_trans_ihold(tp, ip);
4293 * issue the bunmapi() call to free the blocks
4295 XFS_BMAP_INIT(&free_list, &firstfsb);
4296 error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4297 endoffset_fsb - startoffset_fsb,
4298 0, 2, &firstfsb, &free_list, NULL, &done);
4304 * complete the transaction
4306 error = xfs_bmap_finish(&tp, &free_list, &committed);
4311 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4312 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4317 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4321 xfs_bmap_cancel(&free_list);
4323 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4324 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4330 * xfs_change_file_space()
4331 * This routine allocates or frees disk space for the given file.
4332 * The user specified parameters are checked for alignment and size
4341 xfs_change_file_space(
4349 xfs_mount_t *mp = ip->i_mount;
4354 xfs_off_t startoffset;
4359 vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
4362 * must be a regular file and have write permission
4364 if (!S_ISREG(ip->i_d.di_mode))
4365 return XFS_ERROR(EINVAL);
4367 xfs_ilock(ip, XFS_ILOCK_SHARED);
4369 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4370 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4374 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4376 switch (bf->l_whence) {
4377 case 0: /*SEEK_SET*/
4379 case 1: /*SEEK_CUR*/
4380 bf->l_start += offset;
4382 case 2: /*SEEK_END*/
4383 bf->l_start += ip->i_size;
4386 return XFS_ERROR(EINVAL);
4389 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4391 if ( (bf->l_start < 0)
4392 || (bf->l_start > XFS_MAXIOFFSET(mp))
4393 || (bf->l_start + llen < 0)
4394 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4395 return XFS_ERROR(EINVAL);
4399 startoffset = bf->l_start;
4403 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4405 * These calls do NOT zero the data space allocated to the file,
4406 * nor do they change the file size.
4408 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4410 * These calls cause the new file data to be zeroed and the file
4411 * size to be changed.
4413 setprealloc = clrprealloc = 0;
4416 case XFS_IOC_RESVSP:
4417 case XFS_IOC_RESVSP64:
4418 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4425 case XFS_IOC_UNRESVSP:
4426 case XFS_IOC_UNRESVSP64:
4427 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4432 case XFS_IOC_ALLOCSP:
4433 case XFS_IOC_ALLOCSP64:
4434 case XFS_IOC_FREESP:
4435 case XFS_IOC_FREESP64:
4436 if (startoffset > fsize) {
4437 error = xfs_alloc_file_space(ip, fsize,
4438 startoffset - fsize, 0, attr_flags);
4443 va.va_mask = XFS_AT_SIZE;
4444 va.va_size = startoffset;
4446 error = xfs_setattr(ip, &va, attr_flags, credp);
4456 return XFS_ERROR(EINVAL);
4460 * update the inode timestamp, mode, and prealloc flag bits
4462 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4464 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4467 xfs_trans_cancel(tp, 0);
4471 xfs_ilock(ip, XFS_ILOCK_EXCL);
4473 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4474 xfs_trans_ihold(tp, ip);
4476 if ((attr_flags & ATTR_DMI) == 0) {
4477 ip->i_d.di_mode &= ~S_ISUID;
4480 * Note that we don't have to worry about mandatory
4481 * file locking being disabled here because we only
4482 * clear the S_ISGID bit if the Group execute bit is
4483 * on, but if it was on then mandatory locking wouldn't
4484 * have been enabled.
4486 if (ip->i_d.di_mode & S_IXGRP)
4487 ip->i_d.di_mode &= ~S_ISGID;
4489 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4492 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4493 else if (clrprealloc)
4494 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4496 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4497 xfs_trans_set_sync(tp);
4499 error = xfs_trans_commit(tp, 0);
4501 xfs_iunlock(ip, XFS_ILOCK_EXCL);