[XFS] call common xfs vnode-level helpers directly and remove vnode operations
[linux-2.6/kvm.git] / fs / xfs / xfs_vnodeops.c
blob5eb392d062983555ddb819aa7df8290cbd467e30
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_types.h"
22 #include "xfs_bit.h"
23 #include "xfs_log.h"
24 #include "xfs_inum.h"
25 #include "xfs_trans.h"
26 #include "xfs_sb.h"
27 #include "xfs_ag.h"
28 #include "xfs_dir2.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"
44 #include "xfs_bmap.h"
45 #include "xfs_attr.h"
46 #include "xfs_rw.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"
57 int
58 xfs_open(
59 xfs_inode_t *ip)
61 int mode;
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);
76 return 0;
80 * xfs_getattr
82 int
83 xfs_getattr(
84 xfs_inode_t *ip,
85 bhv_vattr_t *vap,
86 int flags)
88 bhv_vnode_t *vp = XFS_ITOV(ip);
89 xfs_mount_t *mp = ip->i_mount;
91 vn_trace_entry(vp, __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)
101 goto all_done;
103 vap->va_nblocks =
104 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
105 vap->va_nodeid = ip->i_ino;
106 #if XFS_BIG_INUMS
107 vap->va_nodeid += mp->m_inoadd;
108 #endif
109 vap->va_nlink = ip->i_d.di_nlink;
112 * Quick exit for non-stat callers
114 if ((vap->va_mask &
115 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
116 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
117 goto all_done;
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) {
131 case S_IFBLK:
132 case S_IFCHR:
133 vap->va_rdev = ip->i_df.if_u2.if_rdev;
134 vap->va_blocksize = BLKDEV_IOSIZE;
135 break;
136 default:
137 vap->va_rdev = 0;
139 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
140 vap->va_blocksize = xfs_preferred_iosize(mp);
141 } else {
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
147 * extent size.
149 vap->va_blocksize =
150 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
152 break;
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.
165 if ((vap->va_mask &
166 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
167 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
168 goto all_done;
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.
179 if ((vap->va_mask &
180 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
181 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
182 goto all_done;
184 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
185 vap->va_nextents =
186 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
187 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
188 ip->i_d.di_nextents;
189 if (ip->i_afp)
190 vap->va_anextents =
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;
194 else
195 vap->va_anextents = 0;
196 vap->va_gen = ip->i_d.di_gen;
198 all_done:
199 if (!(flags & ATTR_LAZY))
200 xfs_iunlock(ip, XFS_ILOCK_SHARED);
201 return 0;
206 * xfs_setattr
209 xfs_setattr(
210 xfs_inode_t *ip,
211 bhv_vattr_t *vap,
212 int flags,
213 cred_t *credp)
215 bhv_vnode_t *vp = XFS_ITOV(ip);
216 xfs_mount_t *mp = ip->i_mount;
217 xfs_trans_t *tp;
218 int mask;
219 int code;
220 uint lock_flags;
221 uint commit_flags=0;
222 uid_t uid=0, iuid=0;
223 gid_t gid=0, igid=0;
224 int timeflags = 0;
225 xfs_prid_t projid=0, iprojid=0;
226 int mandlock_before, mandlock_after;
227 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
228 int file_owner;
229 int need_iolock = 1;
231 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
233 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
234 return XFS_ERROR(EROFS);
237 * Cannot set certain attributes.
239 mask = vap->va_mask;
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);
257 return 0;
260 olddquot1 = olddquot2 = NULL;
261 udqp = gdqp = 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))) {
273 uint qflags = 0;
275 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
276 uid = vap->va_uid;
277 qflags |= XFS_QMOPT_UQUOTA;
278 } else {
279 uid = ip->i_d.di_uid;
281 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
282 gid = vap->va_gid;
283 qflags |= XFS_QMOPT_GQUOTA;
284 } else {
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;
290 } else {
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,
301 &udqp, &gdqp);
302 if (code)
303 return code;
307 * For the other attributes, we acquire the inode lock and
308 * first do an error checking pass.
310 tp = NULL;
311 lock_flags = XFS_ILOCK_EXCL;
312 if (flags & ATTR_NOLOCK)
313 need_iolock = 0;
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);
318 commit_flags = 0;
319 if ((code = xfs_trans_reserve(tp, 0,
320 XFS_ICHANGE_LOG_RES(mp), 0,
321 0, 0))) {
322 lock_flags = 0;
323 goto error_return;
326 } else {
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);
332 if (code) {
333 lock_flags = 0;
334 goto error_return;
337 if (need_iolock)
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.
351 if (mask &
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);
363 goto error_return;
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) {
378 mode_t m = 0;
380 if ((vap->va_mode & S_ISUID) && !file_owner)
381 m |= S_ISUID;
382 if ((vap->va_mode & S_ISGID) &&
383 !in_group_p((gid_t)ip->i_d.di_gid))
384 m |= S_ISGID;
385 #if 0
386 /* Linux allows this, Irix doesn't. */
387 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
388 m |= S_ISVTX;
389 #endif
390 if (m && !capable(CAP_FSETID))
391 vap->va_mode &= ~m;
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 :
415 iprojid;
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);
432 goto error_return;
435 * Do a quota reservation only if uid/projid/gid is actually
436 * going to change.
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)) {
441 ASSERT(tp);
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 */
446 goto error_return;
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);
461 code = 0;
462 goto error_return;
465 if (VN_ISDIR(vp)) {
466 code = XFS_ERROR(EISDIR);
467 goto error_return;
468 } else if (!VN_ISREG(vp)) {
469 code = XFS_ERROR(EINVAL);
470 goto error_return;
473 * Make sure that the dquots are attached to the inode.
475 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
476 goto error_return;
480 * Change file access or modified times.
482 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
483 if (!file_owner) {
484 if ((flags & ATTR_UTIME) &&
485 !capable(CAP_FOWNER)) {
486 code = XFS_ERROR(EPERM);
487 goto error_return;
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) !=
501 vap->va_extsize) ) {
502 code = XFS_ERROR(EINVAL); /* EFBIG? */
503 goto error_return;
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? */
514 goto error_return;
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) {
521 xfs_extlen_t size;
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;
528 } else {
529 size = mp->m_sb.sb_blocksize;
531 if (vap->va_extsize % size) {
532 code = XFS_ERROR(EINVAL);
533 goto error_return;
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);
545 goto error_return;
550 * Can't modify an immutable/append-only file unless
551 * we have appropriate permission.
553 if ((mask & XFS_AT_XFLAGS) &&
554 (ip->i_d.di_flags &
555 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
556 (vap->va_xflags &
557 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
558 !capable(CAP_LINUX_IMMUTABLE)) {
559 code = XFS_ERROR(EPERM);
560 goto error_return;
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) {
573 code = 0;
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.
592 if (!code &&
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 */
601 vn_iowait(vp);
603 if (!code)
604 code = xfs_itruncate_data(ip, vap->va_size);
605 if (code) {
606 ASSERT(tp == NULL);
607 lock_flags &= ~XFS_ILOCK_EXCL;
608 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
609 goto error_return;
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);
617 if (need_iolock)
618 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
619 return code;
621 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
622 xfs_ilock(ip, XFS_ILOCK_EXCL);
625 if (tp) {
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,
649 XFS_DATA_FORK,
650 ((ip->i_d.di_nlink != 0 ||
651 !(mp->m_flags & XFS_MOUNT_WSYNC))
652 ? 1 : 0));
653 if (code)
654 goto abort_return;
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 VTRUNCATE(vp);
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.
705 if (iuid != uid) {
706 if (XFS_IS_UQUOTA_ON(mp)) {
707 ASSERT(mask & XFS_AT_UID);
708 ASSERT(udqp);
709 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
710 &ip->i_udquot, udqp);
712 ip->i_d.di_uid = uid;
714 if (igid != gid) {
715 if (XFS_IS_GQUOTA_ON(mp)) {
716 ASSERT(!XFS_IS_PQUOTA_ON(mp));
717 ASSERT(mask & XFS_AT_GID);
718 ASSERT(gdqp);
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);
728 ASSERT(gdqp);
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) {
779 uint di_flags;
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;
810 } else {
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.
853 code = 0;
854 if (tp) {
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);
878 if (code) {
879 return code;
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));
888 return 0;
890 abort_return:
891 commit_flags |= XFS_TRANS_ABORT;
892 /* FALLTHROUGH */
893 error_return:
894 XFS_QM_DQRELE(mp, udqp);
895 XFS_QM_DQRELE(mp, gdqp);
896 if (tp) {
897 xfs_trans_cancel(tp, commit_flags);
899 if (lock_flags != 0) {
900 xfs_iunlock(ip, lock_flags);
902 return code;
907 * xfs_access
908 * Null conversion from vnode mode bits to inode mode bits, as in efs.
911 xfs_access(
912 xfs_inode_t *ip,
913 int mode,
914 cred_t *credp)
916 int error;
918 vn_trace_entry(XFS_ITOV(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);
923 return error;
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
930 * bmapi.
932 #define SYMLINK_MAPS 2
934 STATIC int
935 xfs_readlink_bmap(
936 xfs_inode_t *ip,
937 char *link)
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];
943 xfs_daddr_t d;
944 int byte_cnt;
945 int n;
946 xfs_buf_t *bp;
947 int error = 0;
949 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
950 mval, &nmaps, NULL, NULL);
951 if (error)
952 goto out;
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);
960 if (error) {
961 xfs_ioerror_alert("xfs_readlink",
962 ip->i_mount, bp, XFS_BUF_ADDR(bp));
963 xfs_buf_relse(bp);
964 goto out;
966 if (pathlen < byte_cnt)
967 byte_cnt = pathlen;
968 pathlen -= byte_cnt;
970 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
971 xfs_buf_relse(bp);
974 link[ip->i_d.di_size] = '\0';
975 error = 0;
977 out:
978 return error;
982 xfs_readlink(
983 xfs_inode_t *ip,
984 char *link)
986 xfs_mount_t *mp = ip->i_mount;
987 int pathlen;
988 int error = 0;
990 vn_trace_entry(XFS_ITOV(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;
1001 if (!pathlen)
1002 goto out;
1004 if (ip->i_df.if_flags & XFS_IFINLINE) {
1005 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
1006 link[pathlen] = '\0';
1007 } else {
1008 error = xfs_readlink_bmap(ip, link);
1011 out:
1012 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1013 return error;
1017 * xfs_fsync
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
1023 * with that.
1026 xfs_fsync(
1027 xfs_inode_t *ip,
1028 int flag,
1029 xfs_off_t start,
1030 xfs_off_t stop)
1032 xfs_trans_t *tp;
1033 int error;
1034 int log_flushed = 0, changed = 1;
1036 vn_trace_entry(XFS_ITOV(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,
1089 XFS_LOG_FORCE |
1090 ((flag & FSYNC_WAIT)
1091 ? XFS_LOG_SYNC : 0),
1092 &log_flushed);
1093 } else {
1095 * If the inode is not pinned and nothing
1096 * has changed we don't need to flush the
1097 * cache.
1099 changed = 0;
1101 error = 0;
1102 } else {
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),
1114 0, 0, 0))) {
1115 xfs_trans_cancel(tp, 0);
1116 return error;
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.
1145 if (!log_flushed)
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
1150 * cache as well.
1152 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1153 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1156 return error;
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.
1165 xfs_free_eofblocks(
1166 xfs_mount_t *mp,
1167 xfs_inode_t *ip,
1168 int flags)
1170 xfs_trans_t *tp;
1171 int error;
1172 xfs_fileoff_t end_fsb;
1173 xfs_fileoff_t last_fsb;
1174 xfs_filblks_t map_len;
1175 int nimaps;
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;
1186 if (map_len <= 0)
1187 return 0;
1189 nimaps = 1;
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)))
1202 return error;
1205 * There are blocks after the end of file.
1206 * Free them up now by truncating the file to
1207 * its current size.
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.
1218 if (use_iolock)
1219 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1220 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1221 ip->i_size);
1222 if (error) {
1223 xfs_trans_cancel(tp, 0);
1224 if (use_iolock)
1225 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1226 return error;
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);
1233 if (error) {
1234 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1235 xfs_trans_cancel(tp, 0);
1236 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1237 return error;
1240 xfs_ilock(ip, XFS_ILOCK_EXCL);
1241 xfs_trans_ijoin(tp, ip,
1242 XFS_IOLOCK_EXCL |
1243 XFS_ILOCK_EXCL);
1244 xfs_trans_ihold(tp, ip);
1246 error = xfs_itruncate_finish(&tp, ip,
1247 ip->i_size,
1248 XFS_DATA_FORK,
1251 * If we get an error at this point we
1252 * simply don't bother truncating the file.
1254 if (error) {
1255 xfs_trans_cancel(tp,
1256 (XFS_TRANS_RELEASE_LOG_RES |
1257 XFS_TRANS_ABORT));
1258 } else {
1259 error = xfs_trans_commit(tp,
1260 XFS_TRANS_RELEASE_LOG_RES);
1262 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1263 : XFS_ILOCK_EXCL));
1265 return error;
1269 * Free a symlink that has blocks associated with it.
1271 STATIC int
1272 xfs_inactive_symlink_rmt(
1273 xfs_inode_t *ip,
1274 xfs_trans_t **tpp)
1276 xfs_buf_t *bp;
1277 int committed;
1278 int done;
1279 int error;
1280 xfs_fsblock_t first_block;
1281 xfs_bmap_free_t free_list;
1282 int i;
1283 xfs_mount_t *mp;
1284 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1285 int nmaps;
1286 xfs_trans_t *ntp;
1287 int size;
1288 xfs_trans_t *tp;
1290 tp = *tpp;
1291 mp = ip->i_mount;
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);
1305 *tpp = NULL;
1306 return error;
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.
1323 done = 0;
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,
1328 &free_list, NULL)))
1329 goto error0;
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)))
1344 goto error1;
1345 ASSERT(done);
1347 * Commit the first transaction. This logs the EFI and the inode.
1349 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1350 goto error1;
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.
1356 ASSERT(committed);
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);
1376 tp = ntp;
1377 if (error) {
1378 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1379 goto error0;
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
1389 * for our caller.
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));
1394 goto error0;
1397 * Return with the inode locked but not joined to the transaction.
1399 *tpp = tp;
1400 return 0;
1402 error1:
1403 xfs_bmap_cancel(&free_list);
1404 error0:
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);
1417 *tpp = NULL;
1418 return error;
1422 STATIC int
1423 xfs_inactive_symlink_local(
1424 xfs_inode_t *ip,
1425 xfs_trans_t **tpp)
1427 int error;
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);
1440 if (error) {
1441 xfs_trans_cancel(*tpp, 0);
1442 *tpp = NULL;
1443 return error;
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),
1453 XFS_DATA_FORK);
1454 ASSERT(ip->i_df.if_bytes == 0);
1456 return 0;
1459 STATIC int
1460 xfs_inactive_attrs(
1461 xfs_inode_t *ip,
1462 xfs_trans_t **tpp)
1464 xfs_trans_t *tp;
1465 int error;
1466 xfs_mount_t *mp;
1468 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1469 tp = *tpp;
1470 mp = ip->i_mount;
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);
1476 if (error) {
1477 *tpp = NULL;
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);
1487 if (error) {
1488 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1489 xfs_trans_cancel(tp, 0);
1490 *tpp = NULL;
1491 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1492 return error;
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);
1502 *tpp = tp;
1503 return 0;
1507 xfs_release(
1508 xfs_inode_t *ip)
1510 bhv_vnode_t *vp = XFS_ITOV(ip);
1511 xfs_mount_t *mp = ip->i_mount;
1512 int error;
1514 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1515 return 0;
1517 /* If this is a read-only mount, don't do this (would generate I/O) */
1518 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1519 return 0;
1521 if (!XFS_FORCED_SHUTDOWN(mp)) {
1523 * If we are using filestreams, and we have an unlinked
1524 * file that we are processing the last close on, then nothing
1525 * will be able to reopen and write to this file. Purge this
1526 * inode from the filestreams cache so that it doesn't delay
1527 * teardown of the inode.
1529 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1530 xfs_filestream_deassociate(ip);
1533 * If we previously truncated this file and removed old data
1534 * in the process, we want to initiate "early" writeout on
1535 * the last close. This is an attempt to combat the notorious
1536 * NULL files problem which is particularly noticable from a
1537 * truncate down, buffered (re-)write (delalloc), followed by
1538 * a crash. What we are effectively doing here is
1539 * significantly reducing the time window where we'd otherwise
1540 * be exposed to that problem.
1542 if (VUNTRUNCATE(vp) && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
1543 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1546 #ifdef HAVE_REFCACHE
1547 /* If we are in the NFS reference cache then don't do this now */
1548 if (ip->i_refcache)
1549 return 0;
1550 #endif
1552 if (ip->i_d.di_nlink != 0) {
1553 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1554 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1555 ip->i_delayed_blks > 0)) &&
1556 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1557 (!(ip->i_d.di_flags &
1558 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1559 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1560 if (error)
1561 return error;
1562 /* Update linux inode block count after free above */
1563 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1564 ip->i_d.di_nblocks + ip->i_delayed_blks);
1568 return 0;
1572 * xfs_inactive
1574 * This is called when the vnode reference count for the vnode
1575 * goes to zero. If the file has been unlinked, then it must
1576 * now be truncated. Also, we clear all of the read-ahead state
1577 * kept for the inode here since the file is now closed.
1580 xfs_inactive(
1581 xfs_inode_t *ip)
1583 bhv_vnode_t *vp = XFS_ITOV(ip);
1584 xfs_bmap_free_t free_list;
1585 xfs_fsblock_t first_block;
1586 int committed;
1587 xfs_trans_t *tp;
1588 xfs_mount_t *mp;
1589 int error;
1590 int truncate;
1592 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1595 * If the inode is already free, then there can be nothing
1596 * to clean up here.
1598 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1599 ASSERT(ip->i_df.if_real_bytes == 0);
1600 ASSERT(ip->i_df.if_broot_bytes == 0);
1601 return VN_INACTIVE_CACHE;
1605 * Only do a truncate if it's a regular file with
1606 * some actual space in it. It's OK to look at the
1607 * inode's fields without the lock because we're the
1608 * only one with a reference to the inode.
1610 truncate = ((ip->i_d.di_nlink == 0) &&
1611 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1612 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1613 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1615 mp = ip->i_mount;
1617 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
1618 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1621 error = 0;
1623 /* If this is a read-only mount, don't do this (would generate I/O) */
1624 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1625 goto out;
1627 if (ip->i_d.di_nlink != 0) {
1628 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1629 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1630 ip->i_delayed_blks > 0)) &&
1631 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1632 (!(ip->i_d.di_flags &
1633 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1634 (ip->i_delayed_blks != 0)))) {
1635 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1636 if (error)
1637 return VN_INACTIVE_CACHE;
1638 /* Update linux inode block count after free above */
1639 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1640 ip->i_d.di_nblocks + ip->i_delayed_blks);
1642 goto out;
1645 ASSERT(ip->i_d.di_nlink == 0);
1647 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1648 return VN_INACTIVE_CACHE;
1650 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1651 if (truncate) {
1653 * Do the xfs_itruncate_start() call before
1654 * reserving any log space because itruncate_start
1655 * will call into the buffer cache and we can't
1656 * do that within a transaction.
1658 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1660 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1661 if (error) {
1662 xfs_trans_cancel(tp, 0);
1663 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1664 return VN_INACTIVE_CACHE;
1667 error = xfs_trans_reserve(tp, 0,
1668 XFS_ITRUNCATE_LOG_RES(mp),
1669 0, XFS_TRANS_PERM_LOG_RES,
1670 XFS_ITRUNCATE_LOG_COUNT);
1671 if (error) {
1672 /* Don't call itruncate_cleanup */
1673 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1674 xfs_trans_cancel(tp, 0);
1675 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1676 return VN_INACTIVE_CACHE;
1679 xfs_ilock(ip, XFS_ILOCK_EXCL);
1680 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1681 xfs_trans_ihold(tp, ip);
1684 * normally, we have to run xfs_itruncate_finish sync.
1685 * But if filesystem is wsync and we're in the inactive
1686 * path, then we know that nlink == 0, and that the
1687 * xaction that made nlink == 0 is permanently committed
1688 * since xfs_remove runs as a synchronous transaction.
1690 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1691 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1693 if (error) {
1694 xfs_trans_cancel(tp,
1695 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1696 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1697 return VN_INACTIVE_CACHE;
1699 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1702 * If we get an error while cleaning up a
1703 * symlink we bail out.
1705 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1706 xfs_inactive_symlink_rmt(ip, &tp) :
1707 xfs_inactive_symlink_local(ip, &tp);
1709 if (error) {
1710 ASSERT(tp == NULL);
1711 return VN_INACTIVE_CACHE;
1714 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1715 xfs_trans_ihold(tp, ip);
1716 } else {
1717 error = xfs_trans_reserve(tp, 0,
1718 XFS_IFREE_LOG_RES(mp),
1719 0, XFS_TRANS_PERM_LOG_RES,
1720 XFS_INACTIVE_LOG_COUNT);
1721 if (error) {
1722 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1723 xfs_trans_cancel(tp, 0);
1724 return VN_INACTIVE_CACHE;
1727 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1728 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1729 xfs_trans_ihold(tp, ip);
1733 * If there are attributes associated with the file
1734 * then blow them away now. The code calls a routine
1735 * that recursively deconstructs the attribute fork.
1736 * We need to just commit the current transaction
1737 * because we can't use it for xfs_attr_inactive().
1739 if (ip->i_d.di_anextents > 0) {
1740 error = xfs_inactive_attrs(ip, &tp);
1742 * If we got an error, the transaction is already
1743 * cancelled, and the inode is unlocked. Just get out.
1745 if (error)
1746 return VN_INACTIVE_CACHE;
1747 } else if (ip->i_afp) {
1748 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1752 * Free the inode.
1754 XFS_BMAP_INIT(&free_list, &first_block);
1755 error = xfs_ifree(tp, ip, &free_list);
1756 if (error) {
1758 * If we fail to free the inode, shut down. The cancel
1759 * might do that, we need to make sure. Otherwise the
1760 * inode might be lost for a long time or forever.
1762 if (!XFS_FORCED_SHUTDOWN(mp)) {
1763 cmn_err(CE_NOTE,
1764 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1765 error, mp->m_fsname);
1766 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1768 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1769 } else {
1771 * Credit the quota account(s). The inode is gone.
1773 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1776 * Just ignore errors at this point. There is
1777 * nothing we can do except to try to keep going.
1779 (void) xfs_bmap_finish(&tp, &free_list, &committed);
1780 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1783 * Release the dquots held by inode, if any.
1785 XFS_QM_DQDETACH(mp, ip);
1787 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1789 out:
1790 return VN_INACTIVE_CACHE;
1795 xfs_lookup(
1796 xfs_inode_t *dp,
1797 bhv_vname_t *dentry,
1798 bhv_vnode_t **vpp)
1800 xfs_inode_t *ip;
1801 xfs_ino_t e_inum;
1802 int error;
1803 uint lock_mode;
1805 vn_trace_entry(XFS_ITOV(dp), __FUNCTION__, (inst_t *)__return_address);
1807 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1808 return XFS_ERROR(EIO);
1810 lock_mode = xfs_ilock_map_shared(dp);
1811 error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
1812 if (!error) {
1813 *vpp = XFS_ITOV(ip);
1814 ITRACE(ip);
1816 xfs_iunlock_map_shared(dp, lock_mode);
1817 return error;
1821 xfs_create(
1822 xfs_inode_t *dp,
1823 bhv_vname_t *dentry,
1824 bhv_vattr_t *vap,
1825 bhv_vnode_t **vpp,
1826 cred_t *credp)
1828 char *name = VNAME(dentry);
1829 xfs_mount_t *mp = dp->i_mount;
1830 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
1831 xfs_inode_t *ip;
1832 bhv_vnode_t *vp = NULL;
1833 xfs_trans_t *tp;
1834 xfs_dev_t rdev;
1835 int error;
1836 xfs_bmap_free_t free_list;
1837 xfs_fsblock_t first_block;
1838 boolean_t unlock_dp_on_error = B_FALSE;
1839 int dm_event_sent = 0;
1840 uint cancel_flags;
1841 int committed;
1842 xfs_prid_t prid;
1843 struct xfs_dquot *udqp, *gdqp;
1844 uint resblks;
1845 int dm_di_mode;
1846 int namelen;
1848 ASSERT(!*vpp);
1849 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1851 dm_di_mode = vap->va_mode;
1852 namelen = VNAMELEN(dentry);
1854 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1855 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1856 dir_vp, DM_RIGHT_NULL, NULL,
1857 DM_RIGHT_NULL, name, NULL,
1858 dm_di_mode, 0, 0);
1860 if (error)
1861 return error;
1862 dm_event_sent = 1;
1865 if (XFS_FORCED_SHUTDOWN(mp))
1866 return XFS_ERROR(EIO);
1868 /* Return through std_return after this point. */
1870 udqp = gdqp = NULL;
1871 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1872 prid = dp->i_d.di_projid;
1873 else if (vap->va_mask & XFS_AT_PROJID)
1874 prid = (xfs_prid_t)vap->va_projid;
1875 else
1876 prid = (xfs_prid_t)dfltprid;
1879 * Make sure that we have allocated dquot(s) on disk.
1881 error = XFS_QM_DQVOPALLOC(mp, dp,
1882 current_fsuid(credp), current_fsgid(credp), prid,
1883 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1884 if (error)
1885 goto std_return;
1887 ip = NULL;
1889 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1890 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1891 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1893 * Initially assume that the file does not exist and
1894 * reserve the resources for that case. If that is not
1895 * the case we'll drop the one we have and get a more
1896 * appropriate transaction later.
1898 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1899 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1900 if (error == ENOSPC) {
1901 resblks = 0;
1902 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1903 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1905 if (error) {
1906 cancel_flags = 0;
1907 goto error_return;
1910 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1911 unlock_dp_on_error = B_TRUE;
1913 XFS_BMAP_INIT(&free_list, &first_block);
1915 ASSERT(ip == NULL);
1918 * Reserve disk quota and the inode.
1920 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1921 if (error)
1922 goto error_return;
1924 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1925 goto error_return;
1926 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1927 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
1928 rdev, credp, prid, resblks > 0,
1929 &ip, &committed);
1930 if (error) {
1931 if (error == ENOSPC)
1932 goto error_return;
1933 goto abort_return;
1935 ITRACE(ip);
1938 * At this point, we've gotten a newly allocated inode.
1939 * It is locked (and joined to the transaction).
1942 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1945 * Now we join the directory inode to the transaction. We do not do it
1946 * earlier because xfs_dir_ialloc might commit the previous transaction
1947 * (and release all the locks). An error from here on will result in
1948 * the transaction cancel unlocking dp so don't do it explicitly in the
1949 * error path.
1951 VN_HOLD(dir_vp);
1952 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1953 unlock_dp_on_error = B_FALSE;
1955 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
1956 &first_block, &free_list, resblks ?
1957 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1958 if (error) {
1959 ASSERT(error != ENOSPC);
1960 goto abort_return;
1962 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1963 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1966 * If this is a synchronous mount, make sure that the
1967 * create transaction goes to disk before returning to
1968 * the user.
1970 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1971 xfs_trans_set_sync(tp);
1974 dp->i_gen++;
1977 * Attach the dquot(s) to the inodes and modify them incore.
1978 * These ids of the inode couldn't have changed since the new
1979 * inode has been locked ever since it was created.
1981 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1984 * xfs_trans_commit normally decrements the vnode ref count
1985 * when it unlocks the inode. Since we want to return the
1986 * vnode to the caller, we bump the vnode ref count now.
1988 IHOLD(ip);
1989 vp = XFS_ITOV(ip);
1991 error = xfs_bmap_finish(&tp, &free_list, &committed);
1992 if (error) {
1993 xfs_bmap_cancel(&free_list);
1994 goto abort_rele;
1997 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1998 if (error) {
1999 IRELE(ip);
2000 tp = NULL;
2001 goto error_return;
2004 XFS_QM_DQRELE(mp, udqp);
2005 XFS_QM_DQRELE(mp, gdqp);
2007 *vpp = vp;
2009 /* Fallthrough to std_return with error = 0 */
2011 std_return:
2012 if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
2013 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2014 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2015 dir_vp, DM_RIGHT_NULL,
2016 *vpp ? vp:NULL,
2017 DM_RIGHT_NULL, name, NULL,
2018 dm_di_mode, error, 0);
2020 return error;
2022 abort_return:
2023 cancel_flags |= XFS_TRANS_ABORT;
2024 /* FALLTHROUGH */
2026 error_return:
2027 if (tp != NULL)
2028 xfs_trans_cancel(tp, cancel_flags);
2030 XFS_QM_DQRELE(mp, udqp);
2031 XFS_QM_DQRELE(mp, gdqp);
2033 if (unlock_dp_on_error)
2034 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2036 goto std_return;
2038 abort_rele:
2040 * Wait until after the current transaction is aborted to
2041 * release the inode. This prevents recursive transactions
2042 * and deadlocks from xfs_inactive.
2044 cancel_flags |= XFS_TRANS_ABORT;
2045 xfs_trans_cancel(tp, cancel_flags);
2046 IRELE(ip);
2048 XFS_QM_DQRELE(mp, udqp);
2049 XFS_QM_DQRELE(mp, gdqp);
2051 goto std_return;
2054 #ifdef DEBUG
2056 * Some counters to see if (and how often) we are hitting some deadlock
2057 * prevention code paths.
2060 int xfs_rm_locks;
2061 int xfs_rm_lock_delays;
2062 int xfs_rm_attempts;
2063 #endif
2066 * The following routine will lock the inodes associated with the
2067 * directory and the named entry in the directory. The locks are
2068 * acquired in increasing inode number.
2070 * If the entry is "..", then only the directory is locked. The
2071 * vnode ref count will still include that from the .. entry in
2072 * this case.
2074 * There is a deadlock we need to worry about. If the locked directory is
2075 * in the AIL, it might be blocking up the log. The next inode we lock
2076 * could be already locked by another thread waiting for log space (e.g
2077 * a permanent log reservation with a long running transaction (see
2078 * xfs_itruncate_finish)). To solve this, we must check if the directory
2079 * is in the ail and use lock_nowait. If we can't lock, we need to
2080 * drop the inode lock on the directory and try again. xfs_iunlock will
2081 * potentially push the tail if we were holding up the log.
2083 STATIC int
2084 xfs_lock_dir_and_entry(
2085 xfs_inode_t *dp,
2086 xfs_inode_t *ip) /* inode of entry 'name' */
2088 int attempts;
2089 xfs_ino_t e_inum;
2090 xfs_inode_t *ips[2];
2091 xfs_log_item_t *lp;
2093 #ifdef DEBUG
2094 xfs_rm_locks++;
2095 #endif
2096 attempts = 0;
2098 again:
2099 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2101 e_inum = ip->i_ino;
2103 ITRACE(ip);
2106 * We want to lock in increasing inum. Since we've already
2107 * acquired the lock on the directory, we may need to release
2108 * if if the inum of the entry turns out to be less.
2110 if (e_inum > dp->i_ino) {
2112 * We are already in the right order, so just
2113 * lock on the inode of the entry.
2114 * We need to use nowait if dp is in the AIL.
2117 lp = (xfs_log_item_t *)dp->i_itemp;
2118 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2119 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2120 attempts++;
2121 #ifdef DEBUG
2122 xfs_rm_attempts++;
2123 #endif
2126 * Unlock dp and try again.
2127 * xfs_iunlock will try to push the tail
2128 * if the inode is in the AIL.
2131 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2133 if ((attempts % 5) == 0) {
2134 delay(1); /* Don't just spin the CPU */
2135 #ifdef DEBUG
2136 xfs_rm_lock_delays++;
2137 #endif
2139 goto again;
2141 } else {
2142 xfs_ilock(ip, XFS_ILOCK_EXCL);
2144 } else if (e_inum < dp->i_ino) {
2145 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2147 ips[0] = ip;
2148 ips[1] = dp;
2149 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2151 /* else e_inum == dp->i_ino */
2152 /* This can happen if we're asked to lock /x/..
2153 * the entry is "..", which is also the parent directory.
2156 return 0;
2159 #ifdef DEBUG
2160 int xfs_locked_n;
2161 int xfs_small_retries;
2162 int xfs_middle_retries;
2163 int xfs_lots_retries;
2164 int xfs_lock_delays;
2165 #endif
2168 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2169 * a different value
2171 static inline int
2172 xfs_lock_inumorder(int lock_mode, int subclass)
2174 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
2175 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
2176 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
2177 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
2179 return lock_mode;
2183 * The following routine will lock n inodes in exclusive mode.
2184 * We assume the caller calls us with the inodes in i_ino order.
2186 * We need to detect deadlock where an inode that we lock
2187 * is in the AIL and we start waiting for another inode that is locked
2188 * by a thread in a long running transaction (such as truncate). This can
2189 * result in deadlock since the long running trans might need to wait
2190 * for the inode we just locked in order to push the tail and free space
2191 * in the log.
2193 void
2194 xfs_lock_inodes(
2195 xfs_inode_t **ips,
2196 int inodes,
2197 int first_locked,
2198 uint lock_mode)
2200 int attempts = 0, i, j, try_lock;
2201 xfs_log_item_t *lp;
2203 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2205 if (first_locked) {
2206 try_lock = 1;
2207 i = 1;
2208 } else {
2209 try_lock = 0;
2210 i = 0;
2213 again:
2214 for (; i < inodes; i++) {
2215 ASSERT(ips[i]);
2217 if (i && (ips[i] == ips[i-1])) /* Already locked */
2218 continue;
2221 * If try_lock is not set yet, make sure all locked inodes
2222 * are not in the AIL.
2223 * If any are, set try_lock to be used later.
2226 if (!try_lock) {
2227 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2228 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2229 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2230 try_lock++;
2236 * If any of the previous locks we have locked is in the AIL,
2237 * we must TRY to get the second and subsequent locks. If
2238 * we can't get any, we must release all we have
2239 * and try again.
2242 if (try_lock) {
2243 /* try_lock must be 0 if i is 0. */
2245 * try_lock means we have an inode locked
2246 * that is in the AIL.
2248 ASSERT(i != 0);
2249 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
2250 attempts++;
2253 * Unlock all previous guys and try again.
2254 * xfs_iunlock will try to push the tail
2255 * if the inode is in the AIL.
2258 for(j = i - 1; j >= 0; j--) {
2261 * Check to see if we've already
2262 * unlocked this one.
2263 * Not the first one going back,
2264 * and the inode ptr is the same.
2266 if ((j != (i - 1)) && ips[j] ==
2267 ips[j+1])
2268 continue;
2270 xfs_iunlock(ips[j], lock_mode);
2273 if ((attempts % 5) == 0) {
2274 delay(1); /* Don't just spin the CPU */
2275 #ifdef DEBUG
2276 xfs_lock_delays++;
2277 #endif
2279 i = 0;
2280 try_lock = 0;
2281 goto again;
2283 } else {
2284 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
2288 #ifdef DEBUG
2289 if (attempts) {
2290 if (attempts < 5) xfs_small_retries++;
2291 else if (attempts < 100) xfs_middle_retries++;
2292 else xfs_lots_retries++;
2293 } else {
2294 xfs_locked_n++;
2296 #endif
2299 #ifdef DEBUG
2300 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2301 int remove_which_error_return = 0;
2302 #else /* ! DEBUG */
2303 #define REMOVE_DEBUG_TRACE(x)
2304 #endif /* ! DEBUG */
2307 xfs_remove(
2308 xfs_inode_t *dp,
2309 bhv_vname_t *dentry)
2311 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2312 char *name = VNAME(dentry);
2313 xfs_mount_t *mp = dp->i_mount;
2314 xfs_inode_t *ip;
2315 xfs_trans_t *tp = NULL;
2316 int error = 0;
2317 xfs_bmap_free_t free_list;
2318 xfs_fsblock_t first_block;
2319 int cancel_flags;
2320 int committed;
2321 int dm_di_mode = 0;
2322 int link_zero;
2323 uint resblks;
2324 int namelen;
2326 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2328 if (XFS_FORCED_SHUTDOWN(mp))
2329 return XFS_ERROR(EIO);
2331 namelen = VNAMELEN(dentry);
2333 if (!xfs_get_dir_entry(dentry, &ip)) {
2334 dm_di_mode = ip->i_d.di_mode;
2335 IRELE(ip);
2338 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2339 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2340 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2341 name, NULL, dm_di_mode, 0, 0);
2342 if (error)
2343 return error;
2346 /* From this point on, return through std_return */
2347 ip = NULL;
2350 * We need to get a reference to ip before we get our log
2351 * reservation. The reason for this is that we cannot call
2352 * xfs_iget for an inode for which we do not have a reference
2353 * once we've acquired a log reservation. This is because the
2354 * inode we are trying to get might be in xfs_inactive going
2355 * for a log reservation. Since we'll have to wait for the
2356 * inactive code to complete before returning from xfs_iget,
2357 * we need to make sure that we don't have log space reserved
2358 * when we call xfs_iget. Instead we get an unlocked reference
2359 * to the inode before getting our log reservation.
2361 error = xfs_get_dir_entry(dentry, &ip);
2362 if (error) {
2363 REMOVE_DEBUG_TRACE(__LINE__);
2364 goto std_return;
2367 dm_di_mode = ip->i_d.di_mode;
2369 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2371 ITRACE(ip);
2373 error = XFS_QM_DQATTACH(mp, dp, 0);
2374 if (!error && dp != ip)
2375 error = XFS_QM_DQATTACH(mp, ip, 0);
2376 if (error) {
2377 REMOVE_DEBUG_TRACE(__LINE__);
2378 IRELE(ip);
2379 goto std_return;
2382 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2383 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2385 * We try to get the real space reservation first,
2386 * allowing for directory btree deletion(s) implying
2387 * possible bmap insert(s). If we can't get the space
2388 * reservation then we use 0 instead, and avoid the bmap
2389 * btree insert(s) in the directory code by, if the bmap
2390 * insert tries to happen, instead trimming the LAST
2391 * block from the directory.
2393 resblks = XFS_REMOVE_SPACE_RES(mp);
2394 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2395 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2396 if (error == ENOSPC) {
2397 resblks = 0;
2398 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2399 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2401 if (error) {
2402 ASSERT(error != ENOSPC);
2403 REMOVE_DEBUG_TRACE(__LINE__);
2404 xfs_trans_cancel(tp, 0);
2405 IRELE(ip);
2406 return error;
2409 error = xfs_lock_dir_and_entry(dp, ip);
2410 if (error) {
2411 REMOVE_DEBUG_TRACE(__LINE__);
2412 xfs_trans_cancel(tp, cancel_flags);
2413 IRELE(ip);
2414 goto std_return;
2418 * At this point, we've gotten both the directory and the entry
2419 * inodes locked.
2421 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2422 if (dp != ip) {
2424 * Increment vnode ref count only in this case since
2425 * there's an extra vnode reference in the case where
2426 * dp == ip.
2428 IHOLD(dp);
2429 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2433 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2435 XFS_BMAP_INIT(&free_list, &first_block);
2436 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2437 &first_block, &free_list, 0);
2438 if (error) {
2439 ASSERT(error != ENOENT);
2440 REMOVE_DEBUG_TRACE(__LINE__);
2441 goto error1;
2443 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2445 dp->i_gen++;
2446 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2448 error = xfs_droplink(tp, ip);
2449 if (error) {
2450 REMOVE_DEBUG_TRACE(__LINE__);
2451 goto error1;
2454 /* Determine if this is the last link while
2455 * we are in the transaction.
2457 link_zero = (ip)->i_d.di_nlink==0;
2460 * Take an extra ref on the inode so that it doesn't
2461 * go to xfs_inactive() from within the commit.
2463 IHOLD(ip);
2466 * If this is a synchronous mount, make sure that the
2467 * remove transaction goes to disk before returning to
2468 * the user.
2470 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2471 xfs_trans_set_sync(tp);
2474 error = xfs_bmap_finish(&tp, &free_list, &committed);
2475 if (error) {
2476 REMOVE_DEBUG_TRACE(__LINE__);
2477 goto error_rele;
2480 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2481 if (error) {
2482 IRELE(ip);
2483 goto std_return;
2487 * Before we drop our extra reference to the inode, purge it
2488 * from the refcache if it is there. By waiting until afterwards
2489 * to do the IRELE, we ensure that we won't go inactive in the
2490 * xfs_refcache_purge_ip routine (although that would be OK).
2492 xfs_refcache_purge_ip(ip);
2495 * If we are using filestreams, kill the stream association.
2496 * If the file is still open it may get a new one but that
2497 * will get killed on last close in xfs_close() so we don't
2498 * have to worry about that.
2500 if (link_zero && xfs_inode_is_filestream(ip))
2501 xfs_filestream_deassociate(ip);
2503 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2505 IRELE(ip);
2507 /* Fall through to std_return with error = 0 */
2508 std_return:
2509 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2510 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2511 dir_vp, DM_RIGHT_NULL,
2512 NULL, DM_RIGHT_NULL,
2513 name, NULL, dm_di_mode, error, 0);
2515 return error;
2517 error1:
2518 xfs_bmap_cancel(&free_list);
2519 cancel_flags |= XFS_TRANS_ABORT;
2520 xfs_trans_cancel(tp, cancel_flags);
2521 goto std_return;
2523 error_rele:
2525 * In this case make sure to not release the inode until after
2526 * the current transaction is aborted. Releasing it beforehand
2527 * can cause us to go to xfs_inactive and start a recursive
2528 * transaction which can easily deadlock with the current one.
2530 xfs_bmap_cancel(&free_list);
2531 cancel_flags |= XFS_TRANS_ABORT;
2532 xfs_trans_cancel(tp, cancel_flags);
2535 * Before we drop our extra reference to the inode, purge it
2536 * from the refcache if it is there. By waiting until afterwards
2537 * to do the IRELE, we ensure that we won't go inactive in the
2538 * xfs_refcache_purge_ip routine (although that would be OK).
2540 xfs_refcache_purge_ip(ip);
2542 IRELE(ip);
2544 goto std_return;
2548 xfs_link(
2549 xfs_inode_t *tdp,
2550 bhv_vnode_t *src_vp,
2551 bhv_vname_t *dentry)
2553 bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
2554 xfs_mount_t *mp = tdp->i_mount;
2555 xfs_inode_t *sip = xfs_vtoi(src_vp);
2556 xfs_trans_t *tp;
2557 xfs_inode_t *ips[2];
2558 int error;
2559 xfs_bmap_free_t free_list;
2560 xfs_fsblock_t first_block;
2561 int cancel_flags;
2562 int committed;
2563 int resblks;
2564 char *target_name = VNAME(dentry);
2565 int target_namelen;
2567 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2568 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2570 target_namelen = VNAMELEN(dentry);
2571 ASSERT(!VN_ISDIR(src_vp));
2573 if (XFS_FORCED_SHUTDOWN(mp))
2574 return XFS_ERROR(EIO);
2576 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2577 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2578 target_dir_vp, DM_RIGHT_NULL,
2579 src_vp, DM_RIGHT_NULL,
2580 target_name, NULL, 0, 0, 0);
2581 if (error)
2582 return error;
2585 /* Return through std_return after this point. */
2587 error = XFS_QM_DQATTACH(mp, sip, 0);
2588 if (!error && sip != tdp)
2589 error = XFS_QM_DQATTACH(mp, tdp, 0);
2590 if (error)
2591 goto std_return;
2593 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2594 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2595 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2596 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2597 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2598 if (error == ENOSPC) {
2599 resblks = 0;
2600 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2601 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2603 if (error) {
2604 cancel_flags = 0;
2605 goto error_return;
2608 if (sip->i_ino < tdp->i_ino) {
2609 ips[0] = sip;
2610 ips[1] = tdp;
2611 } else {
2612 ips[0] = tdp;
2613 ips[1] = sip;
2616 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2619 * Increment vnode ref counts since xfs_trans_commit &
2620 * xfs_trans_cancel will both unlock the inodes and
2621 * decrement the associated ref counts.
2623 VN_HOLD(src_vp);
2624 VN_HOLD(target_dir_vp);
2625 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2626 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2629 * If the source has too many links, we can't make any more to it.
2631 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2632 error = XFS_ERROR(EMLINK);
2633 goto error_return;
2637 * If we are using project inheritance, we only allow hard link
2638 * creation in our tree when the project IDs are the same; else
2639 * the tree quota mechanism could be circumvented.
2641 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2642 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2643 error = XFS_ERROR(EXDEV);
2644 goto error_return;
2647 if (resblks == 0 &&
2648 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2649 goto error_return;
2651 XFS_BMAP_INIT(&free_list, &first_block);
2653 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2654 sip->i_ino, &first_block, &free_list,
2655 resblks);
2656 if (error)
2657 goto abort_return;
2658 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2659 tdp->i_gen++;
2660 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2662 error = xfs_bumplink(tp, sip);
2663 if (error)
2664 goto abort_return;
2667 * If this is a synchronous mount, make sure that the
2668 * link transaction goes to disk before returning to
2669 * the user.
2671 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2672 xfs_trans_set_sync(tp);
2675 error = xfs_bmap_finish (&tp, &free_list, &committed);
2676 if (error) {
2677 xfs_bmap_cancel(&free_list);
2678 goto abort_return;
2681 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2682 if (error)
2683 goto std_return;
2685 /* Fall through to std_return with error = 0. */
2686 std_return:
2687 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2688 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2689 target_dir_vp, DM_RIGHT_NULL,
2690 src_vp, DM_RIGHT_NULL,
2691 target_name, NULL, 0, error, 0);
2693 return error;
2695 abort_return:
2696 cancel_flags |= XFS_TRANS_ABORT;
2697 /* FALLTHROUGH */
2699 error_return:
2700 xfs_trans_cancel(tp, cancel_flags);
2701 goto std_return;
2706 xfs_mkdir(
2707 xfs_inode_t *dp,
2708 bhv_vname_t *dentry,
2709 bhv_vattr_t *vap,
2710 bhv_vnode_t **vpp,
2711 cred_t *credp)
2713 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2714 char *dir_name = VNAME(dentry);
2715 int dir_namelen = VNAMELEN(dentry);
2716 xfs_mount_t *mp = dp->i_mount;
2717 xfs_inode_t *cdp; /* inode of created dir */
2718 bhv_vnode_t *cvp; /* vnode of created dir */
2719 xfs_trans_t *tp;
2720 int cancel_flags;
2721 int error;
2722 int committed;
2723 xfs_bmap_free_t free_list;
2724 xfs_fsblock_t first_block;
2725 boolean_t unlock_dp_on_error = B_FALSE;
2726 boolean_t created = B_FALSE;
2727 int dm_event_sent = 0;
2728 xfs_prid_t prid;
2729 struct xfs_dquot *udqp, *gdqp;
2730 uint resblks;
2731 int dm_di_mode;
2733 if (XFS_FORCED_SHUTDOWN(mp))
2734 return XFS_ERROR(EIO);
2736 tp = NULL;
2737 dm_di_mode = vap->va_mode;
2739 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
2740 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2741 dir_vp, DM_RIGHT_NULL, NULL,
2742 DM_RIGHT_NULL, dir_name, NULL,
2743 dm_di_mode, 0, 0);
2744 if (error)
2745 return error;
2746 dm_event_sent = 1;
2749 /* Return through std_return after this point. */
2751 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2753 mp = dp->i_mount;
2754 udqp = gdqp = NULL;
2755 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2756 prid = dp->i_d.di_projid;
2757 else if (vap->va_mask & XFS_AT_PROJID)
2758 prid = (xfs_prid_t)vap->va_projid;
2759 else
2760 prid = (xfs_prid_t)dfltprid;
2763 * Make sure that we have allocated dquot(s) on disk.
2765 error = XFS_QM_DQVOPALLOC(mp, dp,
2766 current_fsuid(credp), current_fsgid(credp), prid,
2767 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2768 if (error)
2769 goto std_return;
2771 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2772 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2773 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2774 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2775 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2776 if (error == ENOSPC) {
2777 resblks = 0;
2778 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2779 XFS_TRANS_PERM_LOG_RES,
2780 XFS_MKDIR_LOG_COUNT);
2782 if (error) {
2783 cancel_flags = 0;
2784 goto error_return;
2787 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2788 unlock_dp_on_error = B_TRUE;
2791 * Check for directory link count overflow.
2793 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2794 error = XFS_ERROR(EMLINK);
2795 goto error_return;
2799 * Reserve disk quota and the inode.
2801 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2802 if (error)
2803 goto error_return;
2805 if (resblks == 0 &&
2806 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2807 goto error_return;
2809 * create the directory inode.
2811 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
2812 0, credp, prid, resblks > 0,
2813 &cdp, NULL);
2814 if (error) {
2815 if (error == ENOSPC)
2816 goto error_return;
2817 goto abort_return;
2819 ITRACE(cdp);
2822 * Now we add the directory inode to the transaction.
2823 * We waited until now since xfs_dir_ialloc might start
2824 * a new transaction. Had we joined the transaction
2825 * earlier, the locks might have gotten released. An error
2826 * from here on will result in the transaction cancel
2827 * unlocking dp so don't do it explicitly in the error path.
2829 VN_HOLD(dir_vp);
2830 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2831 unlock_dp_on_error = B_FALSE;
2833 XFS_BMAP_INIT(&free_list, &first_block);
2835 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2836 &first_block, &free_list, resblks ?
2837 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2838 if (error) {
2839 ASSERT(error != ENOSPC);
2840 goto error1;
2842 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2845 * Bump the in memory version number of the parent directory
2846 * so that other processes accessing it will recognize that
2847 * the directory has changed.
2849 dp->i_gen++;
2851 error = xfs_dir_init(tp, cdp, dp);
2852 if (error)
2853 goto error2;
2855 cdp->i_gen = 1;
2856 error = xfs_bumplink(tp, dp);
2857 if (error)
2858 goto error2;
2860 cvp = XFS_ITOV(cdp);
2862 created = B_TRUE;
2864 *vpp = cvp;
2865 IHOLD(cdp);
2868 * Attach the dquots to the new inode and modify the icount incore.
2870 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2873 * If this is a synchronous mount, make sure that the
2874 * mkdir transaction goes to disk before returning to
2875 * the user.
2877 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2878 xfs_trans_set_sync(tp);
2881 error = xfs_bmap_finish(&tp, &free_list, &committed);
2882 if (error) {
2883 IRELE(cdp);
2884 goto error2;
2887 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2888 XFS_QM_DQRELE(mp, udqp);
2889 XFS_QM_DQRELE(mp, gdqp);
2890 if (error) {
2891 IRELE(cdp);
2894 /* Fall through to std_return with error = 0 or errno from
2895 * xfs_trans_commit. */
2897 std_return:
2898 if ((created || (error != 0 && dm_event_sent != 0)) &&
2899 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2900 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2901 dir_vp, DM_RIGHT_NULL,
2902 created ? XFS_ITOV(cdp):NULL,
2903 DM_RIGHT_NULL,
2904 dir_name, NULL,
2905 dm_di_mode, error, 0);
2907 return error;
2909 error2:
2910 error1:
2911 xfs_bmap_cancel(&free_list);
2912 abort_return:
2913 cancel_flags |= XFS_TRANS_ABORT;
2914 error_return:
2915 xfs_trans_cancel(tp, cancel_flags);
2916 XFS_QM_DQRELE(mp, udqp);
2917 XFS_QM_DQRELE(mp, gdqp);
2919 if (unlock_dp_on_error)
2920 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2922 goto std_return;
2926 xfs_rmdir(
2927 xfs_inode_t *dp,
2928 bhv_vname_t *dentry)
2930 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2931 char *name = VNAME(dentry);
2932 int namelen = VNAMELEN(dentry);
2933 xfs_mount_t *mp = dp->i_mount;
2934 xfs_inode_t *cdp; /* child directory */
2935 xfs_trans_t *tp;
2936 int error;
2937 xfs_bmap_free_t free_list;
2938 xfs_fsblock_t first_block;
2939 int cancel_flags;
2940 int committed;
2941 int dm_di_mode = S_IFDIR;
2942 int last_cdp_link;
2943 uint resblks;
2945 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2947 if (XFS_FORCED_SHUTDOWN(mp))
2948 return XFS_ERROR(EIO);
2950 if (!xfs_get_dir_entry(dentry, &cdp)) {
2951 dm_di_mode = cdp->i_d.di_mode;
2952 IRELE(cdp);
2955 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2956 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
2957 dir_vp, DM_RIGHT_NULL,
2958 NULL, DM_RIGHT_NULL,
2959 name, NULL, dm_di_mode, 0, 0);
2960 if (error)
2961 return XFS_ERROR(error);
2964 /* Return through std_return after this point. */
2966 cdp = NULL;
2969 * We need to get a reference to cdp before we get our log
2970 * reservation. The reason for this is that we cannot call
2971 * xfs_iget for an inode for which we do not have a reference
2972 * once we've acquired a log reservation. This is because the
2973 * inode we are trying to get might be in xfs_inactive going
2974 * for a log reservation. Since we'll have to wait for the
2975 * inactive code to complete before returning from xfs_iget,
2976 * we need to make sure that we don't have log space reserved
2977 * when we call xfs_iget. Instead we get an unlocked reference
2978 * to the inode before getting our log reservation.
2980 error = xfs_get_dir_entry(dentry, &cdp);
2981 if (error) {
2982 REMOVE_DEBUG_TRACE(__LINE__);
2983 goto std_return;
2985 mp = dp->i_mount;
2986 dm_di_mode = cdp->i_d.di_mode;
2989 * Get the dquots for the inodes.
2991 error = XFS_QM_DQATTACH(mp, dp, 0);
2992 if (!error && dp != cdp)
2993 error = XFS_QM_DQATTACH(mp, cdp, 0);
2994 if (error) {
2995 IRELE(cdp);
2996 REMOVE_DEBUG_TRACE(__LINE__);
2997 goto std_return;
3000 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3001 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3003 * We try to get the real space reservation first,
3004 * allowing for directory btree deletion(s) implying
3005 * possible bmap insert(s). If we can't get the space
3006 * reservation then we use 0 instead, and avoid the bmap
3007 * btree insert(s) in the directory code by, if the bmap
3008 * insert tries to happen, instead trimming the LAST
3009 * block from the directory.
3011 resblks = XFS_REMOVE_SPACE_RES(mp);
3012 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3013 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3014 if (error == ENOSPC) {
3015 resblks = 0;
3016 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3017 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3019 if (error) {
3020 ASSERT(error != ENOSPC);
3021 cancel_flags = 0;
3022 IRELE(cdp);
3023 goto error_return;
3025 XFS_BMAP_INIT(&free_list, &first_block);
3028 * Now lock the child directory inode and the parent directory
3029 * inode in the proper order. This will take care of validating
3030 * that the directory entry for the child directory inode has
3031 * not changed while we were obtaining a log reservation.
3033 error = xfs_lock_dir_and_entry(dp, cdp);
3034 if (error) {
3035 xfs_trans_cancel(tp, cancel_flags);
3036 IRELE(cdp);
3037 goto std_return;
3040 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3041 if (dp != cdp) {
3043 * Only increment the parent directory vnode count if
3044 * we didn't bump it in looking up cdp. The only time
3045 * we don't bump it is when we're looking up ".".
3047 VN_HOLD(dir_vp);
3050 ITRACE(cdp);
3051 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3053 ASSERT(cdp->i_d.di_nlink >= 2);
3054 if (cdp->i_d.di_nlink != 2) {
3055 error = XFS_ERROR(ENOTEMPTY);
3056 goto error_return;
3058 if (!xfs_dir_isempty(cdp)) {
3059 error = XFS_ERROR(ENOTEMPTY);
3060 goto error_return;
3063 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3064 &first_block, &free_list, resblks);
3065 if (error)
3066 goto error1;
3068 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3071 * Bump the in memory generation count on the parent
3072 * directory so that other can know that it has changed.
3074 dp->i_gen++;
3077 * Drop the link from cdp's "..".
3079 error = xfs_droplink(tp, dp);
3080 if (error) {
3081 goto error1;
3085 * Drop the link from dp to cdp.
3087 error = xfs_droplink(tp, cdp);
3088 if (error) {
3089 goto error1;
3093 * Drop the "." link from cdp to self.
3095 error = xfs_droplink(tp, cdp);
3096 if (error) {
3097 goto error1;
3100 /* Determine these before committing transaction */
3101 last_cdp_link = (cdp)->i_d.di_nlink==0;
3104 * Take an extra ref on the child vnode so that it
3105 * does not go to xfs_inactive() from within the commit.
3107 IHOLD(cdp);
3110 * If this is a synchronous mount, make sure that the
3111 * rmdir transaction goes to disk before returning to
3112 * the user.
3114 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3115 xfs_trans_set_sync(tp);
3118 error = xfs_bmap_finish (&tp, &free_list, &committed);
3119 if (error) {
3120 xfs_bmap_cancel(&free_list);
3121 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3122 XFS_TRANS_ABORT));
3123 IRELE(cdp);
3124 goto std_return;
3127 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3128 if (error) {
3129 IRELE(cdp);
3130 goto std_return;
3134 IRELE(cdp);
3136 /* Fall through to std_return with error = 0 or the errno
3137 * from xfs_trans_commit. */
3138 std_return:
3139 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
3140 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3141 dir_vp, DM_RIGHT_NULL,
3142 NULL, DM_RIGHT_NULL,
3143 name, NULL, dm_di_mode,
3144 error, 0);
3146 return error;
3148 error1:
3149 xfs_bmap_cancel(&free_list);
3150 cancel_flags |= XFS_TRANS_ABORT;
3151 /* FALLTHROUGH */
3153 error_return:
3154 xfs_trans_cancel(tp, cancel_flags);
3155 goto std_return;
3159 xfs_symlink(
3160 xfs_inode_t *dp,
3161 bhv_vname_t *dentry,
3162 bhv_vattr_t *vap,
3163 char *target_path,
3164 bhv_vnode_t **vpp,
3165 cred_t *credp)
3167 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
3168 xfs_mount_t *mp = dp->i_mount;
3169 xfs_trans_t *tp;
3170 xfs_inode_t *ip;
3171 int error;
3172 int pathlen;
3173 xfs_bmap_free_t free_list;
3174 xfs_fsblock_t first_block;
3175 boolean_t unlock_dp_on_error = B_FALSE;
3176 uint cancel_flags;
3177 int committed;
3178 xfs_fileoff_t first_fsb;
3179 xfs_filblks_t fs_blocks;
3180 int nmaps;
3181 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3182 xfs_daddr_t d;
3183 char *cur_chunk;
3184 int byte_cnt;
3185 int n;
3186 xfs_buf_t *bp;
3187 xfs_prid_t prid;
3188 struct xfs_dquot *udqp, *gdqp;
3189 uint resblks;
3190 char *link_name = VNAME(dentry);
3191 int link_namelen;
3193 *vpp = NULL;
3194 error = 0;
3195 ip = NULL;
3196 tp = NULL;
3198 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3201 if (XFS_FORCED_SHUTDOWN(mp))
3202 return XFS_ERROR(EIO);
3204 link_namelen = VNAMELEN(dentry);
3207 * Check component lengths of the target path name.
3209 pathlen = strlen(target_path);
3210 if (pathlen >= MAXPATHLEN) /* total string too long */
3211 return XFS_ERROR(ENAMETOOLONG);
3212 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3213 int len, total;
3214 char *path;
3216 for (total = 0, path = target_path; total < pathlen;) {
3218 * Skip any slashes.
3220 while(*path == '/') {
3221 total++;
3222 path++;
3226 * Count up to the next slash or end of path.
3227 * Error out if the component is bigger than MAXNAMELEN.
3229 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3230 if (++len >= MAXNAMELEN) {
3231 error = ENAMETOOLONG;
3232 return error;
3238 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
3239 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3240 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3241 link_name, target_path, 0, 0, 0);
3242 if (error)
3243 return error;
3246 /* Return through std_return after this point. */
3248 udqp = gdqp = NULL;
3249 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3250 prid = dp->i_d.di_projid;
3251 else if (vap->va_mask & XFS_AT_PROJID)
3252 prid = (xfs_prid_t)vap->va_projid;
3253 else
3254 prid = (xfs_prid_t)dfltprid;
3257 * Make sure that we have allocated dquot(s) on disk.
3259 error = XFS_QM_DQVOPALLOC(mp, dp,
3260 current_fsuid(credp), current_fsgid(credp), prid,
3261 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3262 if (error)
3263 goto std_return;
3265 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3266 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3268 * The symlink will fit into the inode data fork?
3269 * There can't be any attributes so we get the whole variable part.
3271 if (pathlen <= XFS_LITINO(mp))
3272 fs_blocks = 0;
3273 else
3274 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3275 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3276 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3277 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3278 if (error == ENOSPC && fs_blocks == 0) {
3279 resblks = 0;
3280 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3281 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3283 if (error) {
3284 cancel_flags = 0;
3285 goto error_return;
3288 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
3289 unlock_dp_on_error = B_TRUE;
3292 * Check whether the directory allows new symlinks or not.
3294 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3295 error = XFS_ERROR(EPERM);
3296 goto error_return;
3300 * Reserve disk quota : blocks and inode.
3302 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3303 if (error)
3304 goto error_return;
3307 * Check for ability to enter directory entry, if no space reserved.
3309 if (resblks == 0 &&
3310 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
3311 goto error_return;
3313 * Initialize the bmap freelist prior to calling either
3314 * bmapi or the directory create code.
3316 XFS_BMAP_INIT(&free_list, &first_block);
3319 * Allocate an inode for the symlink.
3321 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3322 1, 0, credp, prid, resblks > 0, &ip, NULL);
3323 if (error) {
3324 if (error == ENOSPC)
3325 goto error_return;
3326 goto error1;
3328 ITRACE(ip);
3331 * An error after we've joined dp to the transaction will result in the
3332 * transaction cancel unlocking dp so don't do it explicitly in the
3333 * error path.
3335 VN_HOLD(dir_vp);
3336 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3337 unlock_dp_on_error = B_FALSE;
3340 * Also attach the dquot(s) to it, if applicable.
3342 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3344 if (resblks)
3345 resblks -= XFS_IALLOC_SPACE_RES(mp);
3347 * If the symlink will fit into the inode, write it inline.
3349 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3350 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3351 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3352 ip->i_d.di_size = pathlen;
3355 * The inode was initially created in extent format.
3357 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3358 ip->i_df.if_flags |= XFS_IFINLINE;
3360 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3361 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3363 } else {
3364 first_fsb = 0;
3365 nmaps = SYMLINK_MAPS;
3367 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3368 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3369 &first_block, resblks, mval, &nmaps,
3370 &free_list, NULL);
3371 if (error) {
3372 goto error1;
3375 if (resblks)
3376 resblks -= fs_blocks;
3377 ip->i_d.di_size = pathlen;
3378 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3380 cur_chunk = target_path;
3381 for (n = 0; n < nmaps; n++) {
3382 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3383 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3384 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3385 BTOBB(byte_cnt), 0);
3386 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3387 if (pathlen < byte_cnt) {
3388 byte_cnt = pathlen;
3390 pathlen -= byte_cnt;
3392 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3393 cur_chunk += byte_cnt;
3395 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3400 * Create the directory entry for the symlink.
3402 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3403 &first_block, &free_list, resblks);
3404 if (error)
3405 goto error1;
3406 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3407 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3410 * Bump the in memory version number of the parent directory
3411 * so that other processes accessing it will recognize that
3412 * the directory has changed.
3414 dp->i_gen++;
3417 * If this is a synchronous mount, make sure that the
3418 * symlink transaction goes to disk before returning to
3419 * the user.
3421 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3422 xfs_trans_set_sync(tp);
3426 * xfs_trans_commit normally decrements the vnode ref count
3427 * when it unlocks the inode. Since we want to return the
3428 * vnode to the caller, we bump the vnode ref count now.
3430 IHOLD(ip);
3432 error = xfs_bmap_finish(&tp, &free_list, &committed);
3433 if (error) {
3434 goto error2;
3436 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3437 XFS_QM_DQRELE(mp, udqp);
3438 XFS_QM_DQRELE(mp, gdqp);
3440 /* Fall through to std_return with error = 0 or errno from
3441 * xfs_trans_commit */
3442 std_return:
3443 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
3444 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3445 dir_vp, DM_RIGHT_NULL,
3446 error ? NULL : XFS_ITOV(ip),
3447 DM_RIGHT_NULL, link_name, target_path,
3448 0, error, 0);
3451 if (!error) {
3452 bhv_vnode_t *vp;
3454 ASSERT(ip);
3455 vp = XFS_ITOV(ip);
3456 *vpp = vp;
3458 return error;
3460 error2:
3461 IRELE(ip);
3462 error1:
3463 xfs_bmap_cancel(&free_list);
3464 cancel_flags |= XFS_TRANS_ABORT;
3465 error_return:
3466 xfs_trans_cancel(tp, cancel_flags);
3467 XFS_QM_DQRELE(mp, udqp);
3468 XFS_QM_DQRELE(mp, gdqp);
3470 if (unlock_dp_on_error)
3471 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3473 goto std_return;
3478 * xfs_fid2
3480 * A fid routine that takes a pointer to a previously allocated
3481 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3484 xfs_fid2(
3485 xfs_inode_t *ip,
3486 fid_t *fidp)
3488 xfs_fid2_t *xfid = (xfs_fid2_t *)fidp;
3490 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
3491 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3493 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3494 xfid->fid_pad = 0;
3496 * use memcpy because the inode is a long long and there's no
3497 * assurance that xfid->fid_ino is properly aligned.
3499 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3500 xfid->fid_gen = ip->i_d.di_gen;
3502 return 0;
3507 xfs_rwlock(
3508 xfs_inode_t *ip,
3509 bhv_vrwlock_t locktype)
3511 if (S_ISDIR(ip->i_d.di_mode))
3512 return 1;
3513 if (locktype == VRWLOCK_WRITE) {
3514 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3515 } else if (locktype == VRWLOCK_TRY_READ) {
3516 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
3517 } else if (locktype == VRWLOCK_TRY_WRITE) {
3518 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
3519 } else {
3520 ASSERT((locktype == VRWLOCK_READ) ||
3521 (locktype == VRWLOCK_WRITE_DIRECT));
3522 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3525 return 1;
3529 void
3530 xfs_rwunlock(
3531 xfs_inode_t *ip,
3532 bhv_vrwlock_t locktype)
3534 if (S_ISDIR(ip->i_d.di_mode))
3535 return;
3536 if (locktype == VRWLOCK_WRITE) {
3538 * In the write case, we may have added a new entry to
3539 * the reference cache. This might store a pointer to
3540 * an inode to be released in this inode. If it is there,
3541 * clear the pointer and release the inode after unlocking
3542 * this one.
3544 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3545 } else {
3546 ASSERT((locktype == VRWLOCK_READ) ||
3547 (locktype == VRWLOCK_WRITE_DIRECT));
3548 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3550 return;
3555 xfs_inode_flush(
3556 xfs_inode_t *ip,
3557 int flags)
3559 xfs_mount_t *mp = ip->i_mount;
3560 xfs_inode_log_item_t *iip = ip->i_itemp;
3561 int error = 0;
3563 if (XFS_FORCED_SHUTDOWN(mp))
3564 return XFS_ERROR(EIO);
3567 * Bypass inodes which have already been cleaned by
3568 * the inode flush clustering code inside xfs_iflush
3570 if ((ip->i_update_core == 0) &&
3571 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3572 return 0;
3574 if (flags & FLUSH_LOG) {
3575 if (iip && iip->ili_last_lsn) {
3576 xlog_t *log = mp->m_log;
3577 xfs_lsn_t sync_lsn;
3578 int s, log_flags = XFS_LOG_FORCE;
3580 s = GRANT_LOCK(log);
3581 sync_lsn = log->l_last_sync_lsn;
3582 GRANT_UNLOCK(log, s);
3584 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) > 0)) {
3585 if (flags & FLUSH_SYNC)
3586 log_flags |= XFS_LOG_SYNC;
3587 error = xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3588 if (error)
3589 return error;
3592 if (ip->i_update_core == 0)
3593 return 0;
3598 * We make this non-blocking if the inode is contended,
3599 * return EAGAIN to indicate to the caller that they
3600 * did not succeed. This prevents the flush path from
3601 * blocking on inodes inside another operation right
3602 * now, they get caught later by xfs_sync.
3604 if (flags & FLUSH_INODE) {
3605 int flush_flags;
3607 if (flags & FLUSH_SYNC) {
3608 xfs_ilock(ip, XFS_ILOCK_SHARED);
3609 xfs_iflock(ip);
3610 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3611 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3612 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3613 return EAGAIN;
3615 } else {
3616 return EAGAIN;
3619 if (flags & FLUSH_SYNC)
3620 flush_flags = XFS_IFLUSH_SYNC;
3621 else
3622 flush_flags = XFS_IFLUSH_ASYNC;
3624 error = xfs_iflush(ip, flush_flags);
3625 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3628 return error;
3633 xfs_set_dmattrs(
3634 xfs_inode_t *ip,
3635 u_int evmask,
3636 u_int16_t state)
3638 xfs_mount_t *mp = ip->i_mount;
3639 xfs_trans_t *tp;
3640 int error;
3642 if (!capable(CAP_SYS_ADMIN))
3643 return XFS_ERROR(EPERM);
3645 if (XFS_FORCED_SHUTDOWN(mp))
3646 return XFS_ERROR(EIO);
3648 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3649 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3650 if (error) {
3651 xfs_trans_cancel(tp, 0);
3652 return error;
3654 xfs_ilock(ip, XFS_ILOCK_EXCL);
3655 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3657 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3658 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3660 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3661 IHOLD(ip);
3662 error = xfs_trans_commit(tp, 0);
3664 return error;
3668 xfs_reclaim(
3669 xfs_inode_t *ip)
3671 bhv_vnode_t *vp = XFS_ITOV(ip);
3673 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3675 ASSERT(!VN_MAPPED(vp));
3677 /* bad inode, get out here ASAP */
3678 if (VN_BAD(vp)) {
3679 xfs_ireclaim(ip);
3680 return 0;
3683 vn_iowait(vp);
3685 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3688 * Make sure the atime in the XFS inode is correct before freeing the
3689 * Linux inode.
3691 xfs_synchronize_atime(ip);
3694 * If we have nothing to flush with this inode then complete the
3695 * teardown now, otherwise break the link between the xfs inode and the
3696 * linux inode and clean up the xfs inode later. This avoids flushing
3697 * the inode to disk during the delete operation itself.
3699 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3700 * first to ensure that xfs_iunpin() will never see an xfs inode
3701 * that has a linux inode being reclaimed. Synchronisation is provided
3702 * by the i_flags_lock.
3704 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3705 xfs_ilock(ip, XFS_ILOCK_EXCL);
3706 xfs_iflock(ip);
3707 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3708 } else {
3709 xfs_mount_t *mp = ip->i_mount;
3711 /* Protect sync and unpin from us */
3712 XFS_MOUNT_ILOCK(mp);
3713 spin_lock(&ip->i_flags_lock);
3714 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
3715 vn_to_inode(vp)->i_private = NULL;
3716 ip->i_vnode = NULL;
3717 spin_unlock(&ip->i_flags_lock);
3718 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3719 XFS_MOUNT_IUNLOCK(mp);
3721 return 0;
3725 xfs_finish_reclaim(
3726 xfs_inode_t *ip,
3727 int locked,
3728 int sync_mode)
3730 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
3731 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3732 int error;
3734 if (vp && VN_BAD(vp))
3735 goto reclaim;
3737 /* The hash lock here protects a thread in xfs_iget_core from
3738 * racing with us on linking the inode back with a vnode.
3739 * Once we have the XFS_IRECLAIM flag set it will not touch
3740 * us.
3742 write_lock(&pag->pag_ici_lock);
3743 spin_lock(&ip->i_flags_lock);
3744 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3745 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
3746 spin_unlock(&ip->i_flags_lock);
3747 write_unlock(&pag->pag_ici_lock);
3748 if (locked) {
3749 xfs_ifunlock(ip);
3750 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3752 return 1;
3754 __xfs_iflags_set(ip, XFS_IRECLAIM);
3755 spin_unlock(&ip->i_flags_lock);
3756 write_unlock(&pag->pag_ici_lock);
3757 xfs_put_perag(ip->i_mount, pag);
3760 * If the inode is still dirty, then flush it out. If the inode
3761 * is not in the AIL, then it will be OK to flush it delwri as
3762 * long as xfs_iflush() does not keep any references to the inode.
3763 * We leave that decision up to xfs_iflush() since it has the
3764 * knowledge of whether it's OK to simply do a delwri flush of
3765 * the inode or whether we need to wait until the inode is
3766 * pulled from the AIL.
3767 * We get the flush lock regardless, though, just to make sure
3768 * we don't free it while it is being flushed.
3770 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3771 if (!locked) {
3772 xfs_ilock(ip, XFS_ILOCK_EXCL);
3773 xfs_iflock(ip);
3776 if (ip->i_update_core ||
3777 ((ip->i_itemp != NULL) &&
3778 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3779 error = xfs_iflush(ip, sync_mode);
3781 * If we hit an error, typically because of filesystem
3782 * shutdown, we don't need to let vn_reclaim to know
3783 * because we're gonna reclaim the inode anyway.
3785 if (error) {
3786 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3787 goto reclaim;
3789 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3792 ASSERT(ip->i_update_core == 0);
3793 ASSERT(ip->i_itemp == NULL ||
3794 ip->i_itemp->ili_format.ilf_fields == 0);
3795 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3796 } else if (locked) {
3798 * We are not interested in doing an iflush if we're
3799 * in the process of shutting down the filesystem forcibly.
3800 * So, just reclaim the inode.
3802 xfs_ifunlock(ip);
3803 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3806 reclaim:
3807 xfs_ireclaim(ip);
3808 return 0;
3812 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3814 int purged;
3815 xfs_inode_t *ip, *n;
3816 int done = 0;
3818 while (!done) {
3819 purged = 0;
3820 XFS_MOUNT_ILOCK(mp);
3821 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3822 if (noblock) {
3823 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3824 continue;
3825 if (xfs_ipincount(ip) ||
3826 !xfs_iflock_nowait(ip)) {
3827 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3828 continue;
3831 XFS_MOUNT_IUNLOCK(mp);
3832 if (xfs_finish_reclaim(ip, noblock,
3833 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3834 delay(1);
3835 purged = 1;
3836 break;
3839 done = !purged;
3842 XFS_MOUNT_IUNLOCK(mp);
3843 return 0;
3847 * xfs_alloc_file_space()
3848 * This routine allocates disk space for the given file.
3850 * If alloc_type == 0, this request is for an ALLOCSP type
3851 * request which will change the file size. In this case, no
3852 * DMAPI event will be generated by the call. A TRUNCATE event
3853 * will be generated later by xfs_setattr.
3855 * If alloc_type != 0, this request is for a RESVSP type
3856 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3857 * lower block boundary byte address is less than the file's
3858 * length.
3860 * RETURNS:
3861 * 0 on success
3862 * errno on error
3865 STATIC int
3866 xfs_alloc_file_space(
3867 xfs_inode_t *ip,
3868 xfs_off_t offset,
3869 xfs_off_t len,
3870 int alloc_type,
3871 int attr_flags)
3873 xfs_mount_t *mp = ip->i_mount;
3874 xfs_off_t count;
3875 xfs_filblks_t allocated_fsb;
3876 xfs_filblks_t allocatesize_fsb;
3877 xfs_extlen_t extsz, temp;
3878 xfs_fileoff_t startoffset_fsb;
3879 xfs_fsblock_t firstfsb;
3880 int nimaps;
3881 int bmapi_flag;
3882 int quota_flag;
3883 int rt;
3884 xfs_trans_t *tp;
3885 xfs_bmbt_irec_t imaps[1], *imapp;
3886 xfs_bmap_free_t free_list;
3887 uint qblocks, resblks, resrtextents;
3888 int committed;
3889 int error;
3891 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
3893 if (XFS_FORCED_SHUTDOWN(mp))
3894 return XFS_ERROR(EIO);
3896 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3897 return error;
3899 if (len <= 0)
3900 return XFS_ERROR(EINVAL);
3902 rt = XFS_IS_REALTIME_INODE(ip);
3903 extsz = xfs_get_extsz_hint(ip);
3905 count = len;
3906 imapp = &imaps[0];
3907 nimaps = 1;
3908 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
3909 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3910 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3912 /* Generate a DMAPI event if needed. */
3913 if (alloc_type != 0 && offset < ip->i_size &&
3914 (attr_flags&ATTR_DMI) == 0 &&
3915 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3916 xfs_off_t end_dmi_offset;
3918 end_dmi_offset = offset+len;
3919 if (end_dmi_offset > ip->i_size)
3920 end_dmi_offset = ip->i_size;
3921 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
3922 offset, end_dmi_offset - offset,
3923 0, NULL);
3924 if (error)
3925 return error;
3929 * Allocate file space until done or until there is an error
3931 retry:
3932 while (allocatesize_fsb && !error) {
3933 xfs_fileoff_t s, e;
3936 * Determine space reservations for data/realtime.
3938 if (unlikely(extsz)) {
3939 s = startoffset_fsb;
3940 do_div(s, extsz);
3941 s *= extsz;
3942 e = startoffset_fsb + allocatesize_fsb;
3943 if ((temp = do_mod(startoffset_fsb, extsz)))
3944 e += temp;
3945 if ((temp = do_mod(e, extsz)))
3946 e += extsz - temp;
3947 } else {
3948 s = 0;
3949 e = allocatesize_fsb;
3952 if (unlikely(rt)) {
3953 resrtextents = qblocks = (uint)(e - s);
3954 resrtextents /= mp->m_sb.sb_rextsize;
3955 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3956 quota_flag = XFS_QMOPT_RES_RTBLKS;
3957 } else {
3958 resrtextents = 0;
3959 resblks = qblocks = \
3960 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3961 quota_flag = XFS_QMOPT_RES_REGBLKS;
3965 * Allocate and setup the transaction.
3967 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3968 error = xfs_trans_reserve(tp, resblks,
3969 XFS_WRITE_LOG_RES(mp), resrtextents,
3970 XFS_TRANS_PERM_LOG_RES,
3971 XFS_WRITE_LOG_COUNT);
3973 * Check for running out of space
3975 if (error) {
3977 * Free the transaction structure.
3979 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3980 xfs_trans_cancel(tp, 0);
3981 break;
3983 xfs_ilock(ip, XFS_ILOCK_EXCL);
3984 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3985 qblocks, 0, quota_flag);
3986 if (error)
3987 goto error1;
3989 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3990 xfs_trans_ihold(tp, ip);
3993 * Issue the xfs_bmapi() call to allocate the blocks
3995 XFS_BMAP_INIT(&free_list, &firstfsb);
3996 error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
3997 allocatesize_fsb, bmapi_flag,
3998 &firstfsb, 0, imapp, &nimaps,
3999 &free_list, NULL);
4000 if (error) {
4001 goto error0;
4005 * Complete the transaction
4007 error = xfs_bmap_finish(&tp, &free_list, &committed);
4008 if (error) {
4009 goto error0;
4012 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4013 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4014 if (error) {
4015 break;
4018 allocated_fsb = imapp->br_blockcount;
4020 if (nimaps == 0) {
4021 error = XFS_ERROR(ENOSPC);
4022 break;
4025 startoffset_fsb += allocated_fsb;
4026 allocatesize_fsb -= allocated_fsb;
4028 dmapi_enospc_check:
4029 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
4030 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
4031 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4032 XFS_ITOV(ip), DM_RIGHT_NULL,
4033 XFS_ITOV(ip), DM_RIGHT_NULL,
4034 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4035 if (error == 0)
4036 goto retry; /* Maybe DMAPI app. has made space */
4037 /* else fall through with error from XFS_SEND_DATA */
4040 return error;
4042 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
4043 xfs_bmap_cancel(&free_list);
4044 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4046 error1: /* Just cancel transaction */
4047 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4048 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4049 goto dmapi_enospc_check;
4053 * Zero file bytes between startoff and endoff inclusive.
4054 * The iolock is held exclusive and no blocks are buffered.
4056 STATIC int
4057 xfs_zero_remaining_bytes(
4058 xfs_inode_t *ip,
4059 xfs_off_t startoff,
4060 xfs_off_t endoff)
4062 xfs_bmbt_irec_t imap;
4063 xfs_fileoff_t offset_fsb;
4064 xfs_off_t lastoffset;
4065 xfs_off_t offset;
4066 xfs_buf_t *bp;
4067 xfs_mount_t *mp = ip->i_mount;
4068 int nimap;
4069 int error = 0;
4071 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4072 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4073 mp->m_rtdev_targp : mp->m_ddev_targp);
4075 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4076 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4077 nimap = 1;
4078 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
4079 NULL, 0, &imap, &nimap, NULL, NULL);
4080 if (error || nimap < 1)
4081 break;
4082 ASSERT(imap.br_blockcount >= 1);
4083 ASSERT(imap.br_startoff == offset_fsb);
4084 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4085 if (lastoffset > endoff)
4086 lastoffset = endoff;
4087 if (imap.br_startblock == HOLESTARTBLOCK)
4088 continue;
4089 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4090 if (imap.br_state == XFS_EXT_UNWRITTEN)
4091 continue;
4092 XFS_BUF_UNDONE(bp);
4093 XFS_BUF_UNWRITE(bp);
4094 XFS_BUF_READ(bp);
4095 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4096 xfsbdstrat(mp, bp);
4097 if ((error = xfs_iowait(bp))) {
4098 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4099 mp, bp, XFS_BUF_ADDR(bp));
4100 break;
4102 memset(XFS_BUF_PTR(bp) +
4103 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4104 0, lastoffset - offset + 1);
4105 XFS_BUF_UNDONE(bp);
4106 XFS_BUF_UNREAD(bp);
4107 XFS_BUF_WRITE(bp);
4108 xfsbdstrat(mp, bp);
4109 if ((error = xfs_iowait(bp))) {
4110 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4111 mp, bp, XFS_BUF_ADDR(bp));
4112 break;
4115 xfs_buf_free(bp);
4116 return error;
4120 * xfs_free_file_space()
4121 * This routine frees disk space for the given file.
4123 * This routine is only called by xfs_change_file_space
4124 * for an UNRESVSP type call.
4126 * RETURNS:
4127 * 0 on success
4128 * errno on error
4131 STATIC int
4132 xfs_free_file_space(
4133 xfs_inode_t *ip,
4134 xfs_off_t offset,
4135 xfs_off_t len,
4136 int attr_flags)
4138 bhv_vnode_t *vp;
4139 int committed;
4140 int done;
4141 xfs_off_t end_dmi_offset;
4142 xfs_fileoff_t endoffset_fsb;
4143 int error;
4144 xfs_fsblock_t firstfsb;
4145 xfs_bmap_free_t free_list;
4146 xfs_bmbt_irec_t imap;
4147 xfs_off_t ioffset;
4148 xfs_extlen_t mod=0;
4149 xfs_mount_t *mp;
4150 int nimap;
4151 uint resblks;
4152 uint rounding;
4153 int rt;
4154 xfs_fileoff_t startoffset_fsb;
4155 xfs_trans_t *tp;
4156 int need_iolock = 1;
4158 vp = XFS_ITOV(ip);
4159 mp = ip->i_mount;
4161 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4163 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4164 return error;
4166 error = 0;
4167 if (len <= 0) /* if nothing being freed */
4168 return error;
4169 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4170 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4171 end_dmi_offset = offset + len;
4172 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4174 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
4175 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
4176 if (end_dmi_offset > ip->i_size)
4177 end_dmi_offset = ip->i_size;
4178 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4179 offset, end_dmi_offset - offset,
4180 AT_DELAY_FLAG(attr_flags), NULL);
4181 if (error)
4182 return error;
4185 if (attr_flags & ATTR_NOLOCK)
4186 need_iolock = 0;
4187 if (need_iolock) {
4188 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4189 vn_iowait(vp); /* wait for the completion of any pending DIOs */
4192 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, NBPP);
4193 ioffset = offset & ~(rounding - 1);
4195 if (VN_CACHED(vp) != 0) {
4196 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4197 ctooff(offtoct(ioffset)), -1);
4198 error = xfs_flushinval_pages(ip,
4199 ctooff(offtoct(ioffset)),
4200 -1, FI_REMAPF_LOCKED);
4201 if (error)
4202 goto out_unlock_iolock;
4206 * Need to zero the stuff we're not freeing, on disk.
4207 * If its a realtime file & can't use unwritten extents then we
4208 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4209 * will take care of it for us.
4211 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4212 nimap = 1;
4213 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
4214 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4215 if (error)
4216 goto out_unlock_iolock;
4217 ASSERT(nimap == 0 || nimap == 1);
4218 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4219 xfs_daddr_t block;
4221 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4222 block = imap.br_startblock;
4223 mod = do_div(block, mp->m_sb.sb_rextsize);
4224 if (mod)
4225 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4227 nimap = 1;
4228 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
4229 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4230 if (error)
4231 goto out_unlock_iolock;
4232 ASSERT(nimap == 0 || nimap == 1);
4233 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4234 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4235 mod++;
4236 if (mod && (mod != mp->m_sb.sb_rextsize))
4237 endoffset_fsb -= mod;
4240 if ((done = (endoffset_fsb <= startoffset_fsb)))
4242 * One contiguous piece to clear
4244 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4245 else {
4247 * Some full blocks, possibly two pieces to clear
4249 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4250 error = xfs_zero_remaining_bytes(ip, offset,
4251 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4252 if (!error &&
4253 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4254 error = xfs_zero_remaining_bytes(ip,
4255 XFS_FSB_TO_B(mp, endoffset_fsb),
4256 offset + len - 1);
4260 * free file space until done or until there is an error
4262 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4263 while (!error && !done) {
4266 * allocate and setup the transaction. Allow this
4267 * transaction to dip into the reserve blocks to ensure
4268 * the freeing of the space succeeds at ENOSPC.
4270 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4271 tp->t_flags |= XFS_TRANS_RESERVE;
4272 error = xfs_trans_reserve(tp,
4273 resblks,
4274 XFS_WRITE_LOG_RES(mp),
4276 XFS_TRANS_PERM_LOG_RES,
4277 XFS_WRITE_LOG_COUNT);
4280 * check for running out of space
4282 if (error) {
4284 * Free the transaction structure.
4286 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4287 xfs_trans_cancel(tp, 0);
4288 break;
4290 xfs_ilock(ip, XFS_ILOCK_EXCL);
4291 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4292 ip->i_udquot, ip->i_gdquot, resblks, 0,
4293 XFS_QMOPT_RES_REGBLKS);
4294 if (error)
4295 goto error1;
4297 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4298 xfs_trans_ihold(tp, ip);
4301 * issue the bunmapi() call to free the blocks
4303 XFS_BMAP_INIT(&free_list, &firstfsb);
4304 error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4305 endoffset_fsb - startoffset_fsb,
4306 0, 2, &firstfsb, &free_list, NULL, &done);
4307 if (error) {
4308 goto error0;
4312 * complete the transaction
4314 error = xfs_bmap_finish(&tp, &free_list, &committed);
4315 if (error) {
4316 goto error0;
4319 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4320 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4323 out_unlock_iolock:
4324 if (need_iolock)
4325 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4326 return error;
4328 error0:
4329 xfs_bmap_cancel(&free_list);
4330 error1:
4331 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4332 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4333 XFS_ILOCK_EXCL);
4334 return error;
4338 * xfs_change_file_space()
4339 * This routine allocates or frees disk space for the given file.
4340 * The user specified parameters are checked for alignment and size
4341 * limitations.
4343 * RETURNS:
4344 * 0 on success
4345 * errno on error
4349 xfs_change_file_space(
4350 xfs_inode_t *ip,
4351 int cmd,
4352 xfs_flock64_t *bf,
4353 xfs_off_t offset,
4354 cred_t *credp,
4355 int attr_flags)
4357 xfs_mount_t *mp = ip->i_mount;
4358 int clrprealloc;
4359 int error;
4360 xfs_fsize_t fsize;
4361 int setprealloc;
4362 xfs_off_t startoffset;
4363 xfs_off_t llen;
4364 xfs_trans_t *tp;
4365 bhv_vattr_t va;
4367 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4370 * must be a regular file and have write permission
4372 if (!S_ISREG(ip->i_d.di_mode))
4373 return XFS_ERROR(EINVAL);
4375 xfs_ilock(ip, XFS_ILOCK_SHARED);
4377 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4378 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4379 return error;
4382 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4384 switch (bf->l_whence) {
4385 case 0: /*SEEK_SET*/
4386 break;
4387 case 1: /*SEEK_CUR*/
4388 bf->l_start += offset;
4389 break;
4390 case 2: /*SEEK_END*/
4391 bf->l_start += ip->i_size;
4392 break;
4393 default:
4394 return XFS_ERROR(EINVAL);
4397 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4399 if ( (bf->l_start < 0)
4400 || (bf->l_start > XFS_MAXIOFFSET(mp))
4401 || (bf->l_start + llen < 0)
4402 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4403 return XFS_ERROR(EINVAL);
4405 bf->l_whence = 0;
4407 startoffset = bf->l_start;
4408 fsize = ip->i_size;
4411 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4412 * file space.
4413 * These calls do NOT zero the data space allocated to the file,
4414 * nor do they change the file size.
4416 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4417 * space.
4418 * These calls cause the new file data to be zeroed and the file
4419 * size to be changed.
4421 setprealloc = clrprealloc = 0;
4423 switch (cmd) {
4424 case XFS_IOC_RESVSP:
4425 case XFS_IOC_RESVSP64:
4426 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4427 1, attr_flags);
4428 if (error)
4429 return error;
4430 setprealloc = 1;
4431 break;
4433 case XFS_IOC_UNRESVSP:
4434 case XFS_IOC_UNRESVSP64:
4435 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4436 attr_flags)))
4437 return error;
4438 break;
4440 case XFS_IOC_ALLOCSP:
4441 case XFS_IOC_ALLOCSP64:
4442 case XFS_IOC_FREESP:
4443 case XFS_IOC_FREESP64:
4444 if (startoffset > fsize) {
4445 error = xfs_alloc_file_space(ip, fsize,
4446 startoffset - fsize, 0, attr_flags);
4447 if (error)
4448 break;
4451 va.va_mask = XFS_AT_SIZE;
4452 va.va_size = startoffset;
4454 error = xfs_setattr(ip, &va, attr_flags, credp);
4456 if (error)
4457 return error;
4459 clrprealloc = 1;
4460 break;
4462 default:
4463 ASSERT(0);
4464 return XFS_ERROR(EINVAL);
4468 * update the inode timestamp, mode, and prealloc flag bits
4470 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4472 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4473 0, 0, 0))) {
4474 /* ASSERT(0); */
4475 xfs_trans_cancel(tp, 0);
4476 return error;
4479 xfs_ilock(ip, XFS_ILOCK_EXCL);
4481 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4482 xfs_trans_ihold(tp, ip);
4484 if ((attr_flags & ATTR_DMI) == 0) {
4485 ip->i_d.di_mode &= ~S_ISUID;
4488 * Note that we don't have to worry about mandatory
4489 * file locking being disabled here because we only
4490 * clear the S_ISGID bit if the Group execute bit is
4491 * on, but if it was on then mandatory locking wouldn't
4492 * have been enabled.
4494 if (ip->i_d.di_mode & S_IXGRP)
4495 ip->i_d.di_mode &= ~S_ISGID;
4497 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4499 if (setprealloc)
4500 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4501 else if (clrprealloc)
4502 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4504 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4505 xfs_trans_set_sync(tp);
4507 error = xfs_trans_commit(tp, 0);
4509 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4511 return error;