[XFS] Cleanup various fid related bits:
[linux-2.6/mini2440.git] / fs / xfs / xfs_vnodeops.c
blob6b71d9f763c79014acb3956269be095cc14a2e62
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 xfs_itrace_entry(ip);
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 (!(XFS_IS_REALTIME_INODE(ip))) {
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 xfs_itrace_entry(ip);
233 if (mp->m_flags & XFS_MOUNT_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 (XFS_IS_REALTIME_INODE(ip)) !=
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 (XFS_IS_REALTIME_INODE(ip) ||
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(ip);
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 xfs_iflags_set(ip, XFS_ITRUNCATED);
666 * Have to do this even if the file's size doesn't change.
668 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
672 * Change file access modes.
674 if (mask & XFS_AT_MODE) {
675 ip->i_d.di_mode &= S_IFMT;
676 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
678 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
679 timeflags |= XFS_ICHGTIME_CHG;
683 * Change file ownership. Must be the owner or privileged.
684 * If the system was configured with the "restricted_chown"
685 * option, the owner is not permitted to give away the file,
686 * and can change the group id only to a group of which he
687 * or she is a member.
689 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
691 * CAP_FSETID overrides the following restrictions:
693 * The set-user-ID and set-group-ID bits of a file will be
694 * cleared upon successful return from chown()
696 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
697 !capable(CAP_FSETID)) {
698 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
702 * Change the ownerships and register quota modifications
703 * in the transaction.
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 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
810 di_flags |= XFS_DIFLAG_EXTSIZE;
812 ip->i_d.di_flags = di_flags;
814 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
815 timeflags |= XFS_ICHGTIME_CHG;
819 * Change file inode change time only if XFS_AT_CTIME set
820 * AND we have been called by a DMI function.
823 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
824 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
825 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
826 ip->i_update_core = 1;
827 timeflags &= ~XFS_ICHGTIME_CHG;
831 * Send out timestamp changes that need to be set to the
832 * current time. Not done when called by a DMI function.
834 if (timeflags && !(flags & ATTR_DMI))
835 xfs_ichgtime(ip, timeflags);
837 XFS_STATS_INC(xs_ig_attrchg);
840 * If this is a synchronous mount, make sure that the
841 * transaction goes to disk before returning to the user.
842 * This is slightly sub-optimal in that truncates require
843 * two sync transactions instead of one for wsync filesystems.
844 * One for the truncate and one for the timestamps since we
845 * don't want to change the timestamps unless we're sure the
846 * truncate worked. Truncates are less than 1% of the laddis
847 * mix so this probably isn't worth the trouble to optimize.
849 code = 0;
850 if (tp) {
851 if (mp->m_flags & XFS_MOUNT_WSYNC)
852 xfs_trans_set_sync(tp);
854 code = xfs_trans_commit(tp, commit_flags);
858 * If the (regular) file's mandatory locking mode changed, then
859 * notify the vnode. We do this under the inode lock to prevent
860 * racing calls to vop_vnode_change.
862 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
864 xfs_iunlock(ip, lock_flags);
867 * Release any dquot(s) the inode had kept before chown.
869 XFS_QM_DQRELE(mp, olddquot1);
870 XFS_QM_DQRELE(mp, olddquot2);
871 XFS_QM_DQRELE(mp, udqp);
872 XFS_QM_DQRELE(mp, gdqp);
874 if (code) {
875 return code;
878 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
879 !(flags & ATTR_DMI)) {
880 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
881 NULL, DM_RIGHT_NULL, NULL, NULL,
882 0, 0, AT_DELAY_FLAG(flags));
884 return 0;
886 abort_return:
887 commit_flags |= XFS_TRANS_ABORT;
888 /* FALLTHROUGH */
889 error_return:
890 XFS_QM_DQRELE(mp, udqp);
891 XFS_QM_DQRELE(mp, gdqp);
892 if (tp) {
893 xfs_trans_cancel(tp, commit_flags);
895 if (lock_flags != 0) {
896 xfs_iunlock(ip, lock_flags);
898 return code;
903 * xfs_access
904 * Null conversion from vnode mode bits to inode mode bits, as in efs.
907 xfs_access(
908 xfs_inode_t *ip,
909 int mode,
910 cred_t *credp)
912 int error;
914 xfs_itrace_entry(ip);
915 xfs_ilock(ip, XFS_ILOCK_SHARED);
916 error = xfs_iaccess(ip, mode, credp);
917 xfs_iunlock(ip, XFS_ILOCK_SHARED);
918 return error;
923 * The maximum pathlen is 1024 bytes. Since the minimum file system
924 * blocksize is 512 bytes, we can get a max of 2 extents back from
925 * bmapi.
927 #define SYMLINK_MAPS 2
929 STATIC int
930 xfs_readlink_bmap(
931 xfs_inode_t *ip,
932 char *link)
934 xfs_mount_t *mp = ip->i_mount;
935 int pathlen = ip->i_d.di_size;
936 int nmaps = SYMLINK_MAPS;
937 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
938 xfs_daddr_t d;
939 int byte_cnt;
940 int n;
941 xfs_buf_t *bp;
942 int error = 0;
944 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
945 mval, &nmaps, NULL, NULL);
946 if (error)
947 goto out;
949 for (n = 0; n < nmaps; n++) {
950 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
951 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
953 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
954 error = XFS_BUF_GETERROR(bp);
955 if (error) {
956 xfs_ioerror_alert("xfs_readlink",
957 ip->i_mount, bp, XFS_BUF_ADDR(bp));
958 xfs_buf_relse(bp);
959 goto out;
961 if (pathlen < byte_cnt)
962 byte_cnt = pathlen;
963 pathlen -= byte_cnt;
965 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
966 xfs_buf_relse(bp);
969 link[ip->i_d.di_size] = '\0';
970 error = 0;
972 out:
973 return error;
977 xfs_readlink(
978 xfs_inode_t *ip,
979 char *link)
981 xfs_mount_t *mp = ip->i_mount;
982 int pathlen;
983 int error = 0;
985 xfs_itrace_entry(ip);
987 if (XFS_FORCED_SHUTDOWN(mp))
988 return XFS_ERROR(EIO);
990 xfs_ilock(ip, XFS_ILOCK_SHARED);
992 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
993 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
995 pathlen = ip->i_d.di_size;
996 if (!pathlen)
997 goto out;
999 if (ip->i_df.if_flags & XFS_IFINLINE) {
1000 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
1001 link[pathlen] = '\0';
1002 } else {
1003 error = xfs_readlink_bmap(ip, link);
1006 out:
1007 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1008 return error;
1012 * xfs_fsync
1014 * This is called to sync the inode and its data out to disk.
1015 * We need to hold the I/O lock while flushing the data, and
1016 * the inode lock while flushing the inode. The inode lock CANNOT
1017 * be held while flushing the data, so acquire after we're done
1018 * with that.
1021 xfs_fsync(
1022 xfs_inode_t *ip,
1023 int flag,
1024 xfs_off_t start,
1025 xfs_off_t stop)
1027 xfs_trans_t *tp;
1028 int error;
1029 int log_flushed = 0, changed = 1;
1031 xfs_itrace_entry(ip);
1033 ASSERT(start >= 0 && stop >= -1);
1035 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1036 return XFS_ERROR(EIO);
1038 if (flag & FSYNC_DATA)
1039 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1042 * We always need to make sure that the required inode state
1043 * is safe on disk. The vnode might be clean but because
1044 * of committed transactions that haven't hit the disk yet.
1045 * Likewise, there could be unflushed non-transactional
1046 * changes to the inode core that have to go to disk.
1048 * The following code depends on one assumption: that
1049 * any transaction that changes an inode logs the core
1050 * because it has to change some field in the inode core
1051 * (typically nextents or nblocks). That assumption
1052 * implies that any transactions against an inode will
1053 * catch any non-transactional updates. If inode-altering
1054 * transactions exist that violate this assumption, the
1055 * code breaks. Right now, it figures that if the involved
1056 * update_* field is clear and the inode is unpinned, the
1057 * inode is clean. Either it's been flushed or it's been
1058 * committed and the commit has hit the disk unpinning the inode.
1059 * (Note that xfs_inode_item_format() called at commit clears
1060 * the update_* fields.)
1062 xfs_ilock(ip, XFS_ILOCK_SHARED);
1064 /* If we are flushing data then we care about update_size
1065 * being set, otherwise we care about update_core
1067 if ((flag & FSYNC_DATA) ?
1068 (ip->i_update_size == 0) :
1069 (ip->i_update_core == 0)) {
1071 * Timestamps/size haven't changed since last inode
1072 * flush or inode transaction commit. That means
1073 * either nothing got written or a transaction
1074 * committed which caught the updates. If the
1075 * latter happened and the transaction hasn't
1076 * hit the disk yet, the inode will be still
1077 * be pinned. If it is, force the log.
1080 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1082 if (xfs_ipincount(ip)) {
1083 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1084 XFS_LOG_FORCE |
1085 ((flag & FSYNC_WAIT)
1086 ? XFS_LOG_SYNC : 0),
1087 &log_flushed);
1088 } else {
1090 * If the inode is not pinned and nothing
1091 * has changed we don't need to flush the
1092 * cache.
1094 changed = 0;
1096 error = 0;
1097 } else {
1099 * Kick off a transaction to log the inode
1100 * core to get the updates. Make it
1101 * sync if FSYNC_WAIT is passed in (which
1102 * is done by everybody but specfs). The
1103 * sync transaction will also force the log.
1105 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1106 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1107 if ((error = xfs_trans_reserve(tp, 0,
1108 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1109 0, 0, 0))) {
1110 xfs_trans_cancel(tp, 0);
1111 return error;
1113 xfs_ilock(ip, XFS_ILOCK_EXCL);
1116 * Note - it's possible that we might have pushed
1117 * ourselves out of the way during trans_reserve
1118 * which would flush the inode. But there's no
1119 * guarantee that the inode buffer has actually
1120 * gone out yet (it's delwri). Plus the buffer
1121 * could be pinned anyway if it's part of an
1122 * inode in another recent transaction. So we
1123 * play it safe and fire off the transaction anyway.
1125 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1126 xfs_trans_ihold(tp, ip);
1127 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1128 if (flag & FSYNC_WAIT)
1129 xfs_trans_set_sync(tp);
1130 error = _xfs_trans_commit(tp, 0, &log_flushed);
1132 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1135 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1137 * If the log write didn't issue an ordered tag we need
1138 * to flush the disk cache for the data device now.
1140 if (!log_flushed)
1141 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1144 * If this inode is on the RT dev we need to flush that
1145 * cache as well.
1147 if (XFS_IS_REALTIME_INODE(ip))
1148 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1151 return error;
1155 * This is called by xfs_inactive to free any blocks beyond eof
1156 * when the link count isn't zero and by xfs_dm_punch_hole() when
1157 * punching a hole to EOF.
1160 xfs_free_eofblocks(
1161 xfs_mount_t *mp,
1162 xfs_inode_t *ip,
1163 int flags)
1165 xfs_trans_t *tp;
1166 int error;
1167 xfs_fileoff_t end_fsb;
1168 xfs_fileoff_t last_fsb;
1169 xfs_filblks_t map_len;
1170 int nimaps;
1171 xfs_bmbt_irec_t imap;
1172 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
1175 * Figure out if there are any blocks beyond the end
1176 * of the file. If not, then there is nothing to do.
1178 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
1179 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1180 map_len = last_fsb - end_fsb;
1181 if (map_len <= 0)
1182 return 0;
1184 nimaps = 1;
1185 xfs_ilock(ip, XFS_ILOCK_SHARED);
1186 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
1187 NULL, 0, &imap, &nimaps, NULL, NULL);
1188 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1190 if (!error && (nimaps != 0) &&
1191 (imap.br_startblock != HOLESTARTBLOCK ||
1192 ip->i_delayed_blks)) {
1194 * Attach the dquots to the inode up front.
1196 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1197 return error;
1200 * There are blocks after the end of file.
1201 * Free them up now by truncating the file to
1202 * its current size.
1204 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1207 * Do the xfs_itruncate_start() call before
1208 * reserving any log space because
1209 * itruncate_start will call into the buffer
1210 * cache and we can't
1211 * do that within a transaction.
1213 if (use_iolock)
1214 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1215 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1216 ip->i_size);
1217 if (error) {
1218 xfs_trans_cancel(tp, 0);
1219 if (use_iolock)
1220 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1221 return error;
1224 error = xfs_trans_reserve(tp, 0,
1225 XFS_ITRUNCATE_LOG_RES(mp),
1226 0, XFS_TRANS_PERM_LOG_RES,
1227 XFS_ITRUNCATE_LOG_COUNT);
1228 if (error) {
1229 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1230 xfs_trans_cancel(tp, 0);
1231 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1232 return error;
1235 xfs_ilock(ip, XFS_ILOCK_EXCL);
1236 xfs_trans_ijoin(tp, ip,
1237 XFS_IOLOCK_EXCL |
1238 XFS_ILOCK_EXCL);
1239 xfs_trans_ihold(tp, ip);
1241 error = xfs_itruncate_finish(&tp, ip,
1242 ip->i_size,
1243 XFS_DATA_FORK,
1246 * If we get an error at this point we
1247 * simply don't bother truncating the file.
1249 if (error) {
1250 xfs_trans_cancel(tp,
1251 (XFS_TRANS_RELEASE_LOG_RES |
1252 XFS_TRANS_ABORT));
1253 } else {
1254 error = xfs_trans_commit(tp,
1255 XFS_TRANS_RELEASE_LOG_RES);
1257 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1258 : XFS_ILOCK_EXCL));
1260 return error;
1264 * Free a symlink that has blocks associated with it.
1266 STATIC int
1267 xfs_inactive_symlink_rmt(
1268 xfs_inode_t *ip,
1269 xfs_trans_t **tpp)
1271 xfs_buf_t *bp;
1272 int committed;
1273 int done;
1274 int error;
1275 xfs_fsblock_t first_block;
1276 xfs_bmap_free_t free_list;
1277 int i;
1278 xfs_mount_t *mp;
1279 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1280 int nmaps;
1281 xfs_trans_t *ntp;
1282 int size;
1283 xfs_trans_t *tp;
1285 tp = *tpp;
1286 mp = ip->i_mount;
1287 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1289 * We're freeing a symlink that has some
1290 * blocks allocated to it. Free the
1291 * blocks here. We know that we've got
1292 * either 1 or 2 extents and that we can
1293 * free them all in one bunmapi call.
1295 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1296 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1297 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1298 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1299 xfs_trans_cancel(tp, 0);
1300 *tpp = NULL;
1301 return error;
1304 * Lock the inode, fix the size, and join it to the transaction.
1305 * Hold it so in the normal path, we still have it locked for
1306 * the second transaction. In the error paths we need it
1307 * held so the cancel won't rele it, see below.
1309 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1310 size = (int)ip->i_d.di_size;
1311 ip->i_d.di_size = 0;
1312 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1313 xfs_trans_ihold(tp, ip);
1314 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1316 * Find the block(s) so we can inval and unmap them.
1318 done = 0;
1319 XFS_BMAP_INIT(&free_list, &first_block);
1320 nmaps = ARRAY_SIZE(mval);
1321 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1322 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1323 &free_list, NULL)))
1324 goto error0;
1326 * Invalidate the block(s).
1328 for (i = 0; i < nmaps; i++) {
1329 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1330 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1331 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1332 xfs_trans_binval(tp, bp);
1335 * Unmap the dead block(s) to the free_list.
1337 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1338 &first_block, &free_list, NULL, &done)))
1339 goto error1;
1340 ASSERT(done);
1342 * Commit the first transaction. This logs the EFI and the inode.
1344 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1345 goto error1;
1347 * The transaction must have been committed, since there were
1348 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1349 * The new tp has the extent freeing and EFDs.
1351 ASSERT(committed);
1353 * The first xact was committed, so add the inode to the new one.
1354 * Mark it dirty so it will be logged and moved forward in the log as
1355 * part of every commit.
1357 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1358 xfs_trans_ihold(tp, ip);
1359 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1361 * Get a new, empty transaction to return to our caller.
1363 ntp = xfs_trans_dup(tp);
1365 * Commit the transaction containing extent freeing and EFDs.
1366 * If we get an error on the commit here or on the reserve below,
1367 * we need to unlock the inode since the new transaction doesn't
1368 * have the inode attached.
1370 error = xfs_trans_commit(tp, 0);
1371 tp = ntp;
1372 if (error) {
1373 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1374 goto error0;
1377 * Remove the memory for extent descriptions (just bookkeeping).
1379 if (ip->i_df.if_bytes)
1380 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1381 ASSERT(ip->i_df.if_bytes == 0);
1383 * Put an itruncate log reservation in the new transaction
1384 * for our caller.
1386 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1387 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1388 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1389 goto error0;
1392 * Return with the inode locked but not joined to the transaction.
1394 *tpp = tp;
1395 return 0;
1397 error1:
1398 xfs_bmap_cancel(&free_list);
1399 error0:
1401 * Have to come here with the inode locked and either
1402 * (held and in the transaction) or (not in the transaction).
1403 * If the inode isn't held then cancel would iput it, but
1404 * that's wrong since this is inactive and the vnode ref
1405 * count is 0 already.
1406 * Cancel won't do anything to the inode if held, but it still
1407 * needs to be locked until the cancel is done, if it was
1408 * joined to the transaction.
1410 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1411 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1412 *tpp = NULL;
1413 return error;
1417 STATIC int
1418 xfs_inactive_symlink_local(
1419 xfs_inode_t *ip,
1420 xfs_trans_t **tpp)
1422 int error;
1424 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1426 * We're freeing a symlink which fit into
1427 * the inode. Just free the memory used
1428 * to hold the old symlink.
1430 error = xfs_trans_reserve(*tpp, 0,
1431 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1432 0, XFS_TRANS_PERM_LOG_RES,
1433 XFS_ITRUNCATE_LOG_COUNT);
1435 if (error) {
1436 xfs_trans_cancel(*tpp, 0);
1437 *tpp = NULL;
1438 return error;
1440 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1443 * Zero length symlinks _can_ exist.
1445 if (ip->i_df.if_bytes > 0) {
1446 xfs_idata_realloc(ip,
1447 -(ip->i_df.if_bytes),
1448 XFS_DATA_FORK);
1449 ASSERT(ip->i_df.if_bytes == 0);
1451 return 0;
1454 STATIC int
1455 xfs_inactive_attrs(
1456 xfs_inode_t *ip,
1457 xfs_trans_t **tpp)
1459 xfs_trans_t *tp;
1460 int error;
1461 xfs_mount_t *mp;
1463 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1464 tp = *tpp;
1465 mp = ip->i_mount;
1466 ASSERT(ip->i_d.di_forkoff != 0);
1467 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1468 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1470 error = xfs_attr_inactive(ip);
1471 if (error) {
1472 *tpp = NULL;
1473 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1474 return error; /* goto out */
1477 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1478 error = xfs_trans_reserve(tp, 0,
1479 XFS_IFREE_LOG_RES(mp),
1480 0, XFS_TRANS_PERM_LOG_RES,
1481 XFS_INACTIVE_LOG_COUNT);
1482 if (error) {
1483 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1484 xfs_trans_cancel(tp, 0);
1485 *tpp = NULL;
1486 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1487 return error;
1490 xfs_ilock(ip, XFS_ILOCK_EXCL);
1491 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1492 xfs_trans_ihold(tp, ip);
1493 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1495 ASSERT(ip->i_d.di_anextents == 0);
1497 *tpp = tp;
1498 return 0;
1502 xfs_release(
1503 xfs_inode_t *ip)
1505 bhv_vnode_t *vp = XFS_ITOV(ip);
1506 xfs_mount_t *mp = ip->i_mount;
1507 int error;
1509 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1510 return 0;
1512 /* If this is a read-only mount, don't do this (would generate I/O) */
1513 if (mp->m_flags & XFS_MOUNT_RDONLY)
1514 return 0;
1516 if (!XFS_FORCED_SHUTDOWN(mp)) {
1517 int truncated;
1520 * If we are using filestreams, and we have an unlinked
1521 * file that we are processing the last close on, then nothing
1522 * will be able to reopen and write to this file. Purge this
1523 * inode from the filestreams cache so that it doesn't delay
1524 * teardown of the inode.
1526 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1527 xfs_filestream_deassociate(ip);
1530 * If we previously truncated this file and removed old data
1531 * in the process, we want to initiate "early" writeout on
1532 * the last close. This is an attempt to combat the notorious
1533 * NULL files problem which is particularly noticable from a
1534 * truncate down, buffered (re-)write (delalloc), followed by
1535 * a crash. What we are effectively doing here is
1536 * significantly reducing the time window where we'd otherwise
1537 * be exposed to that problem.
1539 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1540 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
1541 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1544 #ifdef HAVE_REFCACHE
1545 /* If we are in the NFS reference cache then don't do this now */
1546 if (ip->i_refcache)
1547 return 0;
1548 #endif
1550 if (ip->i_d.di_nlink != 0) {
1551 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1552 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1553 ip->i_delayed_blks > 0)) &&
1554 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1555 (!(ip->i_d.di_flags &
1556 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1557 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1558 if (error)
1559 return error;
1560 /* Update linux inode block count after free above */
1561 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1562 ip->i_d.di_nblocks + ip->i_delayed_blks);
1566 return 0;
1570 * xfs_inactive
1572 * This is called when the vnode reference count for the vnode
1573 * goes to zero. If the file has been unlinked, then it must
1574 * now be truncated. Also, we clear all of the read-ahead state
1575 * kept for the inode here since the file is now closed.
1578 xfs_inactive(
1579 xfs_inode_t *ip)
1581 bhv_vnode_t *vp = XFS_ITOV(ip);
1582 xfs_bmap_free_t free_list;
1583 xfs_fsblock_t first_block;
1584 int committed;
1585 xfs_trans_t *tp;
1586 xfs_mount_t *mp;
1587 int error;
1588 int truncate;
1590 xfs_itrace_entry(ip);
1593 * If the inode is already free, then there can be nothing
1594 * to clean up here.
1596 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1597 ASSERT(ip->i_df.if_real_bytes == 0);
1598 ASSERT(ip->i_df.if_broot_bytes == 0);
1599 return VN_INACTIVE_CACHE;
1603 * Only do a truncate if it's a regular file with
1604 * some actual space in it. It's OK to look at the
1605 * inode's fields without the lock because we're the
1606 * only one with a reference to the inode.
1608 truncate = ((ip->i_d.di_nlink == 0) &&
1609 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1610 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1611 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1613 mp = ip->i_mount;
1615 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
1616 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1619 error = 0;
1621 /* If this is a read-only mount, don't do this (would generate I/O) */
1622 if (mp->m_flags & XFS_MOUNT_RDONLY)
1623 goto out;
1625 if (ip->i_d.di_nlink != 0) {
1626 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1627 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1628 ip->i_delayed_blks > 0)) &&
1629 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1630 (!(ip->i_d.di_flags &
1631 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1632 (ip->i_delayed_blks != 0)))) {
1633 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1634 if (error)
1635 return VN_INACTIVE_CACHE;
1636 /* Update linux inode block count after free above */
1637 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1638 ip->i_d.di_nblocks + ip->i_delayed_blks);
1640 goto out;
1643 ASSERT(ip->i_d.di_nlink == 0);
1645 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1646 return VN_INACTIVE_CACHE;
1648 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1649 if (truncate) {
1651 * Do the xfs_itruncate_start() call before
1652 * reserving any log space because itruncate_start
1653 * will call into the buffer cache and we can't
1654 * do that within a transaction.
1656 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1658 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1659 if (error) {
1660 xfs_trans_cancel(tp, 0);
1661 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1662 return VN_INACTIVE_CACHE;
1665 error = xfs_trans_reserve(tp, 0,
1666 XFS_ITRUNCATE_LOG_RES(mp),
1667 0, XFS_TRANS_PERM_LOG_RES,
1668 XFS_ITRUNCATE_LOG_COUNT);
1669 if (error) {
1670 /* Don't call itruncate_cleanup */
1671 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1672 xfs_trans_cancel(tp, 0);
1673 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1674 return VN_INACTIVE_CACHE;
1677 xfs_ilock(ip, XFS_ILOCK_EXCL);
1678 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1679 xfs_trans_ihold(tp, ip);
1682 * normally, we have to run xfs_itruncate_finish sync.
1683 * But if filesystem is wsync and we're in the inactive
1684 * path, then we know that nlink == 0, and that the
1685 * xaction that made nlink == 0 is permanently committed
1686 * since xfs_remove runs as a synchronous transaction.
1688 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1689 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1691 if (error) {
1692 xfs_trans_cancel(tp,
1693 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1694 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1695 return VN_INACTIVE_CACHE;
1697 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1700 * If we get an error while cleaning up a
1701 * symlink we bail out.
1703 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1704 xfs_inactive_symlink_rmt(ip, &tp) :
1705 xfs_inactive_symlink_local(ip, &tp);
1707 if (error) {
1708 ASSERT(tp == NULL);
1709 return VN_INACTIVE_CACHE;
1712 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1713 xfs_trans_ihold(tp, ip);
1714 } else {
1715 error = xfs_trans_reserve(tp, 0,
1716 XFS_IFREE_LOG_RES(mp),
1717 0, XFS_TRANS_PERM_LOG_RES,
1718 XFS_INACTIVE_LOG_COUNT);
1719 if (error) {
1720 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1721 xfs_trans_cancel(tp, 0);
1722 return VN_INACTIVE_CACHE;
1725 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1726 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1727 xfs_trans_ihold(tp, ip);
1731 * If there are attributes associated with the file
1732 * then blow them away now. The code calls a routine
1733 * that recursively deconstructs the attribute fork.
1734 * We need to just commit the current transaction
1735 * because we can't use it for xfs_attr_inactive().
1737 if (ip->i_d.di_anextents > 0) {
1738 error = xfs_inactive_attrs(ip, &tp);
1740 * If we got an error, the transaction is already
1741 * cancelled, and the inode is unlocked. Just get out.
1743 if (error)
1744 return VN_INACTIVE_CACHE;
1745 } else if (ip->i_afp) {
1746 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1750 * Free the inode.
1752 XFS_BMAP_INIT(&free_list, &first_block);
1753 error = xfs_ifree(tp, ip, &free_list);
1754 if (error) {
1756 * If we fail to free the inode, shut down. The cancel
1757 * might do that, we need to make sure. Otherwise the
1758 * inode might be lost for a long time or forever.
1760 if (!XFS_FORCED_SHUTDOWN(mp)) {
1761 cmn_err(CE_NOTE,
1762 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1763 error, mp->m_fsname);
1764 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1766 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1767 } else {
1769 * Credit the quota account(s). The inode is gone.
1771 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1774 * Just ignore errors at this point. There is
1775 * nothing we can do except to try to keep going.
1777 (void) xfs_bmap_finish(&tp, &free_list, &committed);
1778 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1781 * Release the dquots held by inode, if any.
1783 XFS_QM_DQDETACH(mp, ip);
1785 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1787 out:
1788 return VN_INACTIVE_CACHE;
1793 xfs_lookup(
1794 xfs_inode_t *dp,
1795 bhv_vname_t *dentry,
1796 bhv_vnode_t **vpp)
1798 xfs_inode_t *ip;
1799 xfs_ino_t e_inum;
1800 int error;
1801 uint lock_mode;
1803 xfs_itrace_entry(dp);
1805 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1806 return XFS_ERROR(EIO);
1808 lock_mode = xfs_ilock_map_shared(dp);
1809 error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
1810 if (!error) {
1811 *vpp = XFS_ITOV(ip);
1812 xfs_itrace_ref(ip);
1814 xfs_iunlock_map_shared(dp, lock_mode);
1815 return error;
1819 xfs_create(
1820 xfs_inode_t *dp,
1821 bhv_vname_t *dentry,
1822 mode_t mode,
1823 xfs_dev_t rdev,
1824 bhv_vnode_t **vpp,
1825 cred_t *credp)
1827 char *name = VNAME(dentry);
1828 xfs_mount_t *mp = dp->i_mount;
1829 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
1830 xfs_inode_t *ip;
1831 bhv_vnode_t *vp = NULL;
1832 xfs_trans_t *tp;
1833 int error;
1834 xfs_bmap_free_t free_list;
1835 xfs_fsblock_t first_block;
1836 boolean_t unlock_dp_on_error = B_FALSE;
1837 int dm_event_sent = 0;
1838 uint cancel_flags;
1839 int committed;
1840 xfs_prid_t prid;
1841 struct xfs_dquot *udqp, *gdqp;
1842 uint resblks;
1843 int namelen;
1845 ASSERT(!*vpp);
1846 xfs_itrace_entry(dp);
1848 namelen = VNAMELEN(dentry);
1850 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1851 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1852 dir_vp, DM_RIGHT_NULL, NULL,
1853 DM_RIGHT_NULL, name, NULL,
1854 mode, 0, 0);
1856 if (error)
1857 return error;
1858 dm_event_sent = 1;
1861 if (XFS_FORCED_SHUTDOWN(mp))
1862 return XFS_ERROR(EIO);
1864 /* Return through std_return after this point. */
1866 udqp = gdqp = NULL;
1867 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1868 prid = dp->i_d.di_projid;
1869 else
1870 prid = (xfs_prid_t)dfltprid;
1873 * Make sure that we have allocated dquot(s) on disk.
1875 error = XFS_QM_DQVOPALLOC(mp, dp,
1876 current_fsuid(credp), current_fsgid(credp), prid,
1877 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1878 if (error)
1879 goto std_return;
1881 ip = NULL;
1883 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1884 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1885 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1887 * Initially assume that the file does not exist and
1888 * reserve the resources for that case. If that is not
1889 * the case we'll drop the one we have and get a more
1890 * appropriate transaction later.
1892 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1893 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1894 if (error == ENOSPC) {
1895 resblks = 0;
1896 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1897 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1899 if (error) {
1900 cancel_flags = 0;
1901 goto error_return;
1904 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1905 unlock_dp_on_error = B_TRUE;
1907 XFS_BMAP_INIT(&free_list, &first_block);
1909 ASSERT(ip == NULL);
1912 * Reserve disk quota and the inode.
1914 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1915 if (error)
1916 goto error_return;
1918 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1919 goto error_return;
1920 error = xfs_dir_ialloc(&tp, dp, mode, 1,
1921 rdev, credp, prid, resblks > 0,
1922 &ip, &committed);
1923 if (error) {
1924 if (error == ENOSPC)
1925 goto error_return;
1926 goto abort_return;
1928 xfs_itrace_ref(ip);
1931 * At this point, we've gotten a newly allocated inode.
1932 * It is locked (and joined to the transaction).
1935 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1938 * Now we join the directory inode to the transaction. We do not do it
1939 * earlier because xfs_dir_ialloc might commit the previous transaction
1940 * (and release all the locks). An error from here on will result in
1941 * the transaction cancel unlocking dp so don't do it explicitly in the
1942 * error path.
1944 VN_HOLD(dir_vp);
1945 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1946 unlock_dp_on_error = B_FALSE;
1948 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
1949 &first_block, &free_list, resblks ?
1950 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1951 if (error) {
1952 ASSERT(error != ENOSPC);
1953 goto abort_return;
1955 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1956 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1959 * If this is a synchronous mount, make sure that the
1960 * create transaction goes to disk before returning to
1961 * the user.
1963 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1964 xfs_trans_set_sync(tp);
1967 dp->i_gen++;
1970 * Attach the dquot(s) to the inodes and modify them incore.
1971 * These ids of the inode couldn't have changed since the new
1972 * inode has been locked ever since it was created.
1974 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1977 * xfs_trans_commit normally decrements the vnode ref count
1978 * when it unlocks the inode. Since we want to return the
1979 * vnode to the caller, we bump the vnode ref count now.
1981 IHOLD(ip);
1982 vp = XFS_ITOV(ip);
1984 error = xfs_bmap_finish(&tp, &free_list, &committed);
1985 if (error) {
1986 xfs_bmap_cancel(&free_list);
1987 goto abort_rele;
1990 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1991 if (error) {
1992 IRELE(ip);
1993 tp = NULL;
1994 goto error_return;
1997 XFS_QM_DQRELE(mp, udqp);
1998 XFS_QM_DQRELE(mp, gdqp);
2000 *vpp = vp;
2002 /* Fallthrough to std_return with error = 0 */
2004 std_return:
2005 if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
2006 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2007 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2008 dir_vp, DM_RIGHT_NULL,
2009 *vpp ? vp:NULL,
2010 DM_RIGHT_NULL, name, NULL,
2011 mode, error, 0);
2013 return error;
2015 abort_return:
2016 cancel_flags |= XFS_TRANS_ABORT;
2017 /* FALLTHROUGH */
2019 error_return:
2020 if (tp != NULL)
2021 xfs_trans_cancel(tp, cancel_flags);
2023 XFS_QM_DQRELE(mp, udqp);
2024 XFS_QM_DQRELE(mp, gdqp);
2026 if (unlock_dp_on_error)
2027 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2029 goto std_return;
2031 abort_rele:
2033 * Wait until after the current transaction is aborted to
2034 * release the inode. This prevents recursive transactions
2035 * and deadlocks from xfs_inactive.
2037 cancel_flags |= XFS_TRANS_ABORT;
2038 xfs_trans_cancel(tp, cancel_flags);
2039 IRELE(ip);
2041 XFS_QM_DQRELE(mp, udqp);
2042 XFS_QM_DQRELE(mp, gdqp);
2044 goto std_return;
2047 #ifdef DEBUG
2049 * Some counters to see if (and how often) we are hitting some deadlock
2050 * prevention code paths.
2053 int xfs_rm_locks;
2054 int xfs_rm_lock_delays;
2055 int xfs_rm_attempts;
2056 #endif
2059 * The following routine will lock the inodes associated with the
2060 * directory and the named entry in the directory. The locks are
2061 * acquired in increasing inode number.
2063 * If the entry is "..", then only the directory is locked. The
2064 * vnode ref count will still include that from the .. entry in
2065 * this case.
2067 * There is a deadlock we need to worry about. If the locked directory is
2068 * in the AIL, it might be blocking up the log. The next inode we lock
2069 * could be already locked by another thread waiting for log space (e.g
2070 * a permanent log reservation with a long running transaction (see
2071 * xfs_itruncate_finish)). To solve this, we must check if the directory
2072 * is in the ail and use lock_nowait. If we can't lock, we need to
2073 * drop the inode lock on the directory and try again. xfs_iunlock will
2074 * potentially push the tail if we were holding up the log.
2076 STATIC int
2077 xfs_lock_dir_and_entry(
2078 xfs_inode_t *dp,
2079 xfs_inode_t *ip) /* inode of entry 'name' */
2081 int attempts;
2082 xfs_ino_t e_inum;
2083 xfs_inode_t *ips[2];
2084 xfs_log_item_t *lp;
2086 #ifdef DEBUG
2087 xfs_rm_locks++;
2088 #endif
2089 attempts = 0;
2091 again:
2092 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2094 e_inum = ip->i_ino;
2096 xfs_itrace_ref(ip);
2099 * We want to lock in increasing inum. Since we've already
2100 * acquired the lock on the directory, we may need to release
2101 * if if the inum of the entry turns out to be less.
2103 if (e_inum > dp->i_ino) {
2105 * We are already in the right order, so just
2106 * lock on the inode of the entry.
2107 * We need to use nowait if dp is in the AIL.
2110 lp = (xfs_log_item_t *)dp->i_itemp;
2111 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2112 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2113 attempts++;
2114 #ifdef DEBUG
2115 xfs_rm_attempts++;
2116 #endif
2119 * Unlock dp and try again.
2120 * xfs_iunlock will try to push the tail
2121 * if the inode is in the AIL.
2124 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2126 if ((attempts % 5) == 0) {
2127 delay(1); /* Don't just spin the CPU */
2128 #ifdef DEBUG
2129 xfs_rm_lock_delays++;
2130 #endif
2132 goto again;
2134 } else {
2135 xfs_ilock(ip, XFS_ILOCK_EXCL);
2137 } else if (e_inum < dp->i_ino) {
2138 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2140 ips[0] = ip;
2141 ips[1] = dp;
2142 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2144 /* else e_inum == dp->i_ino */
2145 /* This can happen if we're asked to lock /x/..
2146 * the entry is "..", which is also the parent directory.
2149 return 0;
2152 #ifdef DEBUG
2153 int xfs_locked_n;
2154 int xfs_small_retries;
2155 int xfs_middle_retries;
2156 int xfs_lots_retries;
2157 int xfs_lock_delays;
2158 #endif
2161 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2162 * a different value
2164 static inline int
2165 xfs_lock_inumorder(int lock_mode, int subclass)
2167 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
2168 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
2169 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
2170 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
2172 return lock_mode;
2176 * The following routine will lock n inodes in exclusive mode.
2177 * We assume the caller calls us with the inodes in i_ino order.
2179 * We need to detect deadlock where an inode that we lock
2180 * is in the AIL and we start waiting for another inode that is locked
2181 * by a thread in a long running transaction (such as truncate). This can
2182 * result in deadlock since the long running trans might need to wait
2183 * for the inode we just locked in order to push the tail and free space
2184 * in the log.
2186 void
2187 xfs_lock_inodes(
2188 xfs_inode_t **ips,
2189 int inodes,
2190 int first_locked,
2191 uint lock_mode)
2193 int attempts = 0, i, j, try_lock;
2194 xfs_log_item_t *lp;
2196 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2198 if (first_locked) {
2199 try_lock = 1;
2200 i = 1;
2201 } else {
2202 try_lock = 0;
2203 i = 0;
2206 again:
2207 for (; i < inodes; i++) {
2208 ASSERT(ips[i]);
2210 if (i && (ips[i] == ips[i-1])) /* Already locked */
2211 continue;
2214 * If try_lock is not set yet, make sure all locked inodes
2215 * are not in the AIL.
2216 * If any are, set try_lock to be used later.
2219 if (!try_lock) {
2220 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2221 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2222 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2223 try_lock++;
2229 * If any of the previous locks we have locked is in the AIL,
2230 * we must TRY to get the second and subsequent locks. If
2231 * we can't get any, we must release all we have
2232 * and try again.
2235 if (try_lock) {
2236 /* try_lock must be 0 if i is 0. */
2238 * try_lock means we have an inode locked
2239 * that is in the AIL.
2241 ASSERT(i != 0);
2242 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
2243 attempts++;
2246 * Unlock all previous guys and try again.
2247 * xfs_iunlock will try to push the tail
2248 * if the inode is in the AIL.
2251 for(j = i - 1; j >= 0; j--) {
2254 * Check to see if we've already
2255 * unlocked this one.
2256 * Not the first one going back,
2257 * and the inode ptr is the same.
2259 if ((j != (i - 1)) && ips[j] ==
2260 ips[j+1])
2261 continue;
2263 xfs_iunlock(ips[j], lock_mode);
2266 if ((attempts % 5) == 0) {
2267 delay(1); /* Don't just spin the CPU */
2268 #ifdef DEBUG
2269 xfs_lock_delays++;
2270 #endif
2272 i = 0;
2273 try_lock = 0;
2274 goto again;
2276 } else {
2277 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
2281 #ifdef DEBUG
2282 if (attempts) {
2283 if (attempts < 5) xfs_small_retries++;
2284 else if (attempts < 100) xfs_middle_retries++;
2285 else xfs_lots_retries++;
2286 } else {
2287 xfs_locked_n++;
2289 #endif
2292 #ifdef DEBUG
2293 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2294 int remove_which_error_return = 0;
2295 #else /* ! DEBUG */
2296 #define REMOVE_DEBUG_TRACE(x)
2297 #endif /* ! DEBUG */
2300 xfs_remove(
2301 xfs_inode_t *dp,
2302 bhv_vname_t *dentry)
2304 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2305 char *name = VNAME(dentry);
2306 xfs_mount_t *mp = dp->i_mount;
2307 xfs_inode_t *ip;
2308 xfs_trans_t *tp = NULL;
2309 int error = 0;
2310 xfs_bmap_free_t free_list;
2311 xfs_fsblock_t first_block;
2312 int cancel_flags;
2313 int committed;
2314 int dm_di_mode = 0;
2315 int link_zero;
2316 uint resblks;
2317 int namelen;
2319 xfs_itrace_entry(dp);
2321 if (XFS_FORCED_SHUTDOWN(mp))
2322 return XFS_ERROR(EIO);
2324 namelen = VNAMELEN(dentry);
2326 if (!xfs_get_dir_entry(dentry, &ip)) {
2327 dm_di_mode = ip->i_d.di_mode;
2328 IRELE(ip);
2331 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2332 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2333 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2334 name, NULL, dm_di_mode, 0, 0);
2335 if (error)
2336 return error;
2339 /* From this point on, return through std_return */
2340 ip = NULL;
2343 * We need to get a reference to ip before we get our log
2344 * reservation. The reason for this is that we cannot call
2345 * xfs_iget for an inode for which we do not have a reference
2346 * once we've acquired a log reservation. This is because the
2347 * inode we are trying to get might be in xfs_inactive going
2348 * for a log reservation. Since we'll have to wait for the
2349 * inactive code to complete before returning from xfs_iget,
2350 * we need to make sure that we don't have log space reserved
2351 * when we call xfs_iget. Instead we get an unlocked reference
2352 * to the inode before getting our log reservation.
2354 error = xfs_get_dir_entry(dentry, &ip);
2355 if (error) {
2356 REMOVE_DEBUG_TRACE(__LINE__);
2357 goto std_return;
2360 dm_di_mode = ip->i_d.di_mode;
2362 xfs_itrace_entry(ip);
2363 xfs_itrace_ref(ip);
2365 error = XFS_QM_DQATTACH(mp, dp, 0);
2366 if (!error && dp != ip)
2367 error = XFS_QM_DQATTACH(mp, ip, 0);
2368 if (error) {
2369 REMOVE_DEBUG_TRACE(__LINE__);
2370 IRELE(ip);
2371 goto std_return;
2374 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2375 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2377 * We try to get the real space reservation first,
2378 * allowing for directory btree deletion(s) implying
2379 * possible bmap insert(s). If we can't get the space
2380 * reservation then we use 0 instead, and avoid the bmap
2381 * btree insert(s) in the directory code by, if the bmap
2382 * insert tries to happen, instead trimming the LAST
2383 * block from the directory.
2385 resblks = XFS_REMOVE_SPACE_RES(mp);
2386 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2387 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2388 if (error == ENOSPC) {
2389 resblks = 0;
2390 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2391 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2393 if (error) {
2394 ASSERT(error != ENOSPC);
2395 REMOVE_DEBUG_TRACE(__LINE__);
2396 xfs_trans_cancel(tp, 0);
2397 IRELE(ip);
2398 return error;
2401 error = xfs_lock_dir_and_entry(dp, ip);
2402 if (error) {
2403 REMOVE_DEBUG_TRACE(__LINE__);
2404 xfs_trans_cancel(tp, cancel_flags);
2405 IRELE(ip);
2406 goto std_return;
2410 * At this point, we've gotten both the directory and the entry
2411 * inodes locked.
2413 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2414 if (dp != ip) {
2416 * Increment vnode ref count only in this case since
2417 * there's an extra vnode reference in the case where
2418 * dp == ip.
2420 IHOLD(dp);
2421 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2425 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2427 XFS_BMAP_INIT(&free_list, &first_block);
2428 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2429 &first_block, &free_list, 0);
2430 if (error) {
2431 ASSERT(error != ENOENT);
2432 REMOVE_DEBUG_TRACE(__LINE__);
2433 goto error1;
2435 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2437 dp->i_gen++;
2438 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2440 error = xfs_droplink(tp, ip);
2441 if (error) {
2442 REMOVE_DEBUG_TRACE(__LINE__);
2443 goto error1;
2446 /* Determine if this is the last link while
2447 * we are in the transaction.
2449 link_zero = (ip)->i_d.di_nlink==0;
2452 * Take an extra ref on the inode so that it doesn't
2453 * go to xfs_inactive() from within the commit.
2455 IHOLD(ip);
2458 * If this is a synchronous mount, make sure that the
2459 * remove transaction goes to disk before returning to
2460 * the user.
2462 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2463 xfs_trans_set_sync(tp);
2466 error = xfs_bmap_finish(&tp, &free_list, &committed);
2467 if (error) {
2468 REMOVE_DEBUG_TRACE(__LINE__);
2469 goto error_rele;
2472 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2473 if (error) {
2474 IRELE(ip);
2475 goto std_return;
2479 * Before we drop our extra reference to the inode, purge it
2480 * from the refcache if it is there. By waiting until afterwards
2481 * to do the IRELE, we ensure that we won't go inactive in the
2482 * xfs_refcache_purge_ip routine (although that would be OK).
2484 xfs_refcache_purge_ip(ip);
2487 * If we are using filestreams, kill the stream association.
2488 * If the file is still open it may get a new one but that
2489 * will get killed on last close in xfs_close() so we don't
2490 * have to worry about that.
2492 if (link_zero && xfs_inode_is_filestream(ip))
2493 xfs_filestream_deassociate(ip);
2495 xfs_itrace_exit(ip);
2496 IRELE(ip);
2498 /* Fall through to std_return with error = 0 */
2499 std_return:
2500 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2501 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2502 dir_vp, DM_RIGHT_NULL,
2503 NULL, DM_RIGHT_NULL,
2504 name, NULL, dm_di_mode, error, 0);
2506 return error;
2508 error1:
2509 xfs_bmap_cancel(&free_list);
2510 cancel_flags |= XFS_TRANS_ABORT;
2511 xfs_trans_cancel(tp, cancel_flags);
2512 goto std_return;
2514 error_rele:
2516 * In this case make sure to not release the inode until after
2517 * the current transaction is aborted. Releasing it beforehand
2518 * can cause us to go to xfs_inactive and start a recursive
2519 * transaction which can easily deadlock with the current one.
2521 xfs_bmap_cancel(&free_list);
2522 cancel_flags |= XFS_TRANS_ABORT;
2523 xfs_trans_cancel(tp, cancel_flags);
2526 * Before we drop our extra reference to the inode, purge it
2527 * from the refcache if it is there. By waiting until afterwards
2528 * to do the IRELE, we ensure that we won't go inactive in the
2529 * xfs_refcache_purge_ip routine (although that would be OK).
2531 xfs_refcache_purge_ip(ip);
2533 IRELE(ip);
2535 goto std_return;
2539 xfs_link(
2540 xfs_inode_t *tdp,
2541 bhv_vnode_t *src_vp,
2542 bhv_vname_t *dentry)
2544 bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
2545 xfs_mount_t *mp = tdp->i_mount;
2546 xfs_inode_t *sip = xfs_vtoi(src_vp);
2547 xfs_trans_t *tp;
2548 xfs_inode_t *ips[2];
2549 int error;
2550 xfs_bmap_free_t free_list;
2551 xfs_fsblock_t first_block;
2552 int cancel_flags;
2553 int committed;
2554 int resblks;
2555 char *target_name = VNAME(dentry);
2556 int target_namelen;
2558 xfs_itrace_entry(tdp);
2559 xfs_itrace_entry(xfs_vtoi(src_vp));
2561 target_namelen = VNAMELEN(dentry);
2562 ASSERT(!VN_ISDIR(src_vp));
2564 if (XFS_FORCED_SHUTDOWN(mp))
2565 return XFS_ERROR(EIO);
2567 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2568 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2569 target_dir_vp, DM_RIGHT_NULL,
2570 src_vp, DM_RIGHT_NULL,
2571 target_name, NULL, 0, 0, 0);
2572 if (error)
2573 return error;
2576 /* Return through std_return after this point. */
2578 error = XFS_QM_DQATTACH(mp, sip, 0);
2579 if (!error && sip != tdp)
2580 error = XFS_QM_DQATTACH(mp, tdp, 0);
2581 if (error)
2582 goto std_return;
2584 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2585 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2586 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2587 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2588 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2589 if (error == ENOSPC) {
2590 resblks = 0;
2591 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2592 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2594 if (error) {
2595 cancel_flags = 0;
2596 goto error_return;
2599 if (sip->i_ino < tdp->i_ino) {
2600 ips[0] = sip;
2601 ips[1] = tdp;
2602 } else {
2603 ips[0] = tdp;
2604 ips[1] = sip;
2607 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2610 * Increment vnode ref counts since xfs_trans_commit &
2611 * xfs_trans_cancel will both unlock the inodes and
2612 * decrement the associated ref counts.
2614 VN_HOLD(src_vp);
2615 VN_HOLD(target_dir_vp);
2616 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2617 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2620 * If the source has too many links, we can't make any more to it.
2622 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2623 error = XFS_ERROR(EMLINK);
2624 goto error_return;
2628 * If we are using project inheritance, we only allow hard link
2629 * creation in our tree when the project IDs are the same; else
2630 * the tree quota mechanism could be circumvented.
2632 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2633 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2634 error = XFS_ERROR(EXDEV);
2635 goto error_return;
2638 if (resblks == 0 &&
2639 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2640 goto error_return;
2642 XFS_BMAP_INIT(&free_list, &first_block);
2644 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2645 sip->i_ino, &first_block, &free_list,
2646 resblks);
2647 if (error)
2648 goto abort_return;
2649 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2650 tdp->i_gen++;
2651 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2653 error = xfs_bumplink(tp, sip);
2654 if (error)
2655 goto abort_return;
2658 * If this is a synchronous mount, make sure that the
2659 * link transaction goes to disk before returning to
2660 * the user.
2662 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2663 xfs_trans_set_sync(tp);
2666 error = xfs_bmap_finish (&tp, &free_list, &committed);
2667 if (error) {
2668 xfs_bmap_cancel(&free_list);
2669 goto abort_return;
2672 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2673 if (error)
2674 goto std_return;
2676 /* Fall through to std_return with error = 0. */
2677 std_return:
2678 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2679 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2680 target_dir_vp, DM_RIGHT_NULL,
2681 src_vp, DM_RIGHT_NULL,
2682 target_name, NULL, 0, error, 0);
2684 return error;
2686 abort_return:
2687 cancel_flags |= XFS_TRANS_ABORT;
2688 /* FALLTHROUGH */
2690 error_return:
2691 xfs_trans_cancel(tp, cancel_flags);
2692 goto std_return;
2697 xfs_mkdir(
2698 xfs_inode_t *dp,
2699 bhv_vname_t *dentry,
2700 mode_t mode,
2701 bhv_vnode_t **vpp,
2702 cred_t *credp)
2704 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2705 char *dir_name = VNAME(dentry);
2706 int dir_namelen = VNAMELEN(dentry);
2707 xfs_mount_t *mp = dp->i_mount;
2708 xfs_inode_t *cdp; /* inode of created dir */
2709 bhv_vnode_t *cvp; /* vnode of created dir */
2710 xfs_trans_t *tp;
2711 int cancel_flags;
2712 int error;
2713 int committed;
2714 xfs_bmap_free_t free_list;
2715 xfs_fsblock_t first_block;
2716 boolean_t unlock_dp_on_error = B_FALSE;
2717 boolean_t created = B_FALSE;
2718 int dm_event_sent = 0;
2719 xfs_prid_t prid;
2720 struct xfs_dquot *udqp, *gdqp;
2721 uint resblks;
2723 if (XFS_FORCED_SHUTDOWN(mp))
2724 return XFS_ERROR(EIO);
2726 tp = NULL;
2728 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
2729 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2730 dir_vp, DM_RIGHT_NULL, NULL,
2731 DM_RIGHT_NULL, dir_name, NULL,
2732 mode, 0, 0);
2733 if (error)
2734 return error;
2735 dm_event_sent = 1;
2738 /* Return through std_return after this point. */
2740 xfs_itrace_entry(dp);
2742 mp = dp->i_mount;
2743 udqp = gdqp = NULL;
2744 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2745 prid = dp->i_d.di_projid;
2746 else
2747 prid = (xfs_prid_t)dfltprid;
2750 * Make sure that we have allocated dquot(s) on disk.
2752 error = XFS_QM_DQVOPALLOC(mp, dp,
2753 current_fsuid(credp), current_fsgid(credp), prid,
2754 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2755 if (error)
2756 goto std_return;
2758 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2759 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2760 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2761 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2762 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2763 if (error == ENOSPC) {
2764 resblks = 0;
2765 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2766 XFS_TRANS_PERM_LOG_RES,
2767 XFS_MKDIR_LOG_COUNT);
2769 if (error) {
2770 cancel_flags = 0;
2771 goto error_return;
2774 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2775 unlock_dp_on_error = B_TRUE;
2778 * Check for directory link count overflow.
2780 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2781 error = XFS_ERROR(EMLINK);
2782 goto error_return;
2786 * Reserve disk quota and the inode.
2788 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2789 if (error)
2790 goto error_return;
2792 if (resblks == 0 &&
2793 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2794 goto error_return;
2796 * create the directory inode.
2798 error = xfs_dir_ialloc(&tp, dp, mode, 2,
2799 0, credp, prid, resblks > 0,
2800 &cdp, NULL);
2801 if (error) {
2802 if (error == ENOSPC)
2803 goto error_return;
2804 goto abort_return;
2806 xfs_itrace_ref(cdp);
2809 * Now we add the directory inode to the transaction.
2810 * We waited until now since xfs_dir_ialloc might start
2811 * a new transaction. Had we joined the transaction
2812 * earlier, the locks might have gotten released. An error
2813 * from here on will result in the transaction cancel
2814 * unlocking dp so don't do it explicitly in the error path.
2816 VN_HOLD(dir_vp);
2817 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2818 unlock_dp_on_error = B_FALSE;
2820 XFS_BMAP_INIT(&free_list, &first_block);
2822 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2823 &first_block, &free_list, resblks ?
2824 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2825 if (error) {
2826 ASSERT(error != ENOSPC);
2827 goto error1;
2829 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2832 * Bump the in memory version number of the parent directory
2833 * so that other processes accessing it will recognize that
2834 * the directory has changed.
2836 dp->i_gen++;
2838 error = xfs_dir_init(tp, cdp, dp);
2839 if (error)
2840 goto error2;
2842 cdp->i_gen = 1;
2843 error = xfs_bumplink(tp, dp);
2844 if (error)
2845 goto error2;
2847 cvp = XFS_ITOV(cdp);
2849 created = B_TRUE;
2851 *vpp = cvp;
2852 IHOLD(cdp);
2855 * Attach the dquots to the new inode and modify the icount incore.
2857 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2860 * If this is a synchronous mount, make sure that the
2861 * mkdir transaction goes to disk before returning to
2862 * the user.
2864 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2865 xfs_trans_set_sync(tp);
2868 error = xfs_bmap_finish(&tp, &free_list, &committed);
2869 if (error) {
2870 IRELE(cdp);
2871 goto error2;
2874 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2875 XFS_QM_DQRELE(mp, udqp);
2876 XFS_QM_DQRELE(mp, gdqp);
2877 if (error) {
2878 IRELE(cdp);
2881 /* Fall through to std_return with error = 0 or errno from
2882 * xfs_trans_commit. */
2884 std_return:
2885 if ((created || (error != 0 && dm_event_sent != 0)) &&
2886 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2887 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2888 dir_vp, DM_RIGHT_NULL,
2889 created ? XFS_ITOV(cdp):NULL,
2890 DM_RIGHT_NULL,
2891 dir_name, NULL,
2892 mode, error, 0);
2894 return error;
2896 error2:
2897 error1:
2898 xfs_bmap_cancel(&free_list);
2899 abort_return:
2900 cancel_flags |= XFS_TRANS_ABORT;
2901 error_return:
2902 xfs_trans_cancel(tp, cancel_flags);
2903 XFS_QM_DQRELE(mp, udqp);
2904 XFS_QM_DQRELE(mp, gdqp);
2906 if (unlock_dp_on_error)
2907 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2909 goto std_return;
2913 xfs_rmdir(
2914 xfs_inode_t *dp,
2915 bhv_vname_t *dentry)
2917 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2918 char *name = VNAME(dentry);
2919 int namelen = VNAMELEN(dentry);
2920 xfs_mount_t *mp = dp->i_mount;
2921 xfs_inode_t *cdp; /* child directory */
2922 xfs_trans_t *tp;
2923 int error;
2924 xfs_bmap_free_t free_list;
2925 xfs_fsblock_t first_block;
2926 int cancel_flags;
2927 int committed;
2928 int dm_di_mode = S_IFDIR;
2929 int last_cdp_link;
2930 uint resblks;
2932 xfs_itrace_entry(dp);
2934 if (XFS_FORCED_SHUTDOWN(mp))
2935 return XFS_ERROR(EIO);
2937 if (!xfs_get_dir_entry(dentry, &cdp)) {
2938 dm_di_mode = cdp->i_d.di_mode;
2939 IRELE(cdp);
2942 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2943 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
2944 dir_vp, DM_RIGHT_NULL,
2945 NULL, DM_RIGHT_NULL,
2946 name, NULL, dm_di_mode, 0, 0);
2947 if (error)
2948 return XFS_ERROR(error);
2951 /* Return through std_return after this point. */
2953 cdp = NULL;
2956 * We need to get a reference to cdp before we get our log
2957 * reservation. The reason for this is that we cannot call
2958 * xfs_iget for an inode for which we do not have a reference
2959 * once we've acquired a log reservation. This is because the
2960 * inode we are trying to get might be in xfs_inactive going
2961 * for a log reservation. Since we'll have to wait for the
2962 * inactive code to complete before returning from xfs_iget,
2963 * we need to make sure that we don't have log space reserved
2964 * when we call xfs_iget. Instead we get an unlocked reference
2965 * to the inode before getting our log reservation.
2967 error = xfs_get_dir_entry(dentry, &cdp);
2968 if (error) {
2969 REMOVE_DEBUG_TRACE(__LINE__);
2970 goto std_return;
2972 mp = dp->i_mount;
2973 dm_di_mode = cdp->i_d.di_mode;
2976 * Get the dquots for the inodes.
2978 error = XFS_QM_DQATTACH(mp, dp, 0);
2979 if (!error && dp != cdp)
2980 error = XFS_QM_DQATTACH(mp, cdp, 0);
2981 if (error) {
2982 IRELE(cdp);
2983 REMOVE_DEBUG_TRACE(__LINE__);
2984 goto std_return;
2987 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2988 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2990 * We try to get the real space reservation first,
2991 * allowing for directory btree deletion(s) implying
2992 * possible bmap insert(s). If we can't get the space
2993 * reservation then we use 0 instead, and avoid the bmap
2994 * btree insert(s) in the directory code by, if the bmap
2995 * insert tries to happen, instead trimming the LAST
2996 * block from the directory.
2998 resblks = XFS_REMOVE_SPACE_RES(mp);
2999 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3000 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3001 if (error == ENOSPC) {
3002 resblks = 0;
3003 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3004 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3006 if (error) {
3007 ASSERT(error != ENOSPC);
3008 cancel_flags = 0;
3009 IRELE(cdp);
3010 goto error_return;
3012 XFS_BMAP_INIT(&free_list, &first_block);
3015 * Now lock the child directory inode and the parent directory
3016 * inode in the proper order. This will take care of validating
3017 * that the directory entry for the child directory inode has
3018 * not changed while we were obtaining a log reservation.
3020 error = xfs_lock_dir_and_entry(dp, cdp);
3021 if (error) {
3022 xfs_trans_cancel(tp, cancel_flags);
3023 IRELE(cdp);
3024 goto std_return;
3027 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3028 if (dp != cdp) {
3030 * Only increment the parent directory vnode count if
3031 * we didn't bump it in looking up cdp. The only time
3032 * we don't bump it is when we're looking up ".".
3034 VN_HOLD(dir_vp);
3037 xfs_itrace_ref(cdp);
3038 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3040 ASSERT(cdp->i_d.di_nlink >= 2);
3041 if (cdp->i_d.di_nlink != 2) {
3042 error = XFS_ERROR(ENOTEMPTY);
3043 goto error_return;
3045 if (!xfs_dir_isempty(cdp)) {
3046 error = XFS_ERROR(ENOTEMPTY);
3047 goto error_return;
3050 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3051 &first_block, &free_list, resblks);
3052 if (error)
3053 goto error1;
3055 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3058 * Bump the in memory generation count on the parent
3059 * directory so that other can know that it has changed.
3061 dp->i_gen++;
3064 * Drop the link from cdp's "..".
3066 error = xfs_droplink(tp, dp);
3067 if (error) {
3068 goto error1;
3072 * Drop the link from dp to cdp.
3074 error = xfs_droplink(tp, cdp);
3075 if (error) {
3076 goto error1;
3080 * Drop the "." link from cdp to self.
3082 error = xfs_droplink(tp, cdp);
3083 if (error) {
3084 goto error1;
3087 /* Determine these before committing transaction */
3088 last_cdp_link = (cdp)->i_d.di_nlink==0;
3091 * Take an extra ref on the child vnode so that it
3092 * does not go to xfs_inactive() from within the commit.
3094 IHOLD(cdp);
3097 * If this is a synchronous mount, make sure that the
3098 * rmdir transaction goes to disk before returning to
3099 * the user.
3101 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3102 xfs_trans_set_sync(tp);
3105 error = xfs_bmap_finish (&tp, &free_list, &committed);
3106 if (error) {
3107 xfs_bmap_cancel(&free_list);
3108 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3109 XFS_TRANS_ABORT));
3110 IRELE(cdp);
3111 goto std_return;
3114 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3115 if (error) {
3116 IRELE(cdp);
3117 goto std_return;
3121 IRELE(cdp);
3123 /* Fall through to std_return with error = 0 or the errno
3124 * from xfs_trans_commit. */
3125 std_return:
3126 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
3127 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3128 dir_vp, DM_RIGHT_NULL,
3129 NULL, DM_RIGHT_NULL,
3130 name, NULL, dm_di_mode,
3131 error, 0);
3133 return error;
3135 error1:
3136 xfs_bmap_cancel(&free_list);
3137 cancel_flags |= XFS_TRANS_ABORT;
3138 /* FALLTHROUGH */
3140 error_return:
3141 xfs_trans_cancel(tp, cancel_flags);
3142 goto std_return;
3146 xfs_symlink(
3147 xfs_inode_t *dp,
3148 bhv_vname_t *dentry,
3149 char *target_path,
3150 mode_t mode,
3151 bhv_vnode_t **vpp,
3152 cred_t *credp)
3154 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
3155 xfs_mount_t *mp = dp->i_mount;
3156 xfs_trans_t *tp;
3157 xfs_inode_t *ip;
3158 int error;
3159 int pathlen;
3160 xfs_bmap_free_t free_list;
3161 xfs_fsblock_t first_block;
3162 boolean_t unlock_dp_on_error = B_FALSE;
3163 uint cancel_flags;
3164 int committed;
3165 xfs_fileoff_t first_fsb;
3166 xfs_filblks_t fs_blocks;
3167 int nmaps;
3168 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3169 xfs_daddr_t d;
3170 char *cur_chunk;
3171 int byte_cnt;
3172 int n;
3173 xfs_buf_t *bp;
3174 xfs_prid_t prid;
3175 struct xfs_dquot *udqp, *gdqp;
3176 uint resblks;
3177 char *link_name = VNAME(dentry);
3178 int link_namelen;
3180 *vpp = NULL;
3181 error = 0;
3182 ip = NULL;
3183 tp = NULL;
3185 xfs_itrace_entry(dp);
3187 if (XFS_FORCED_SHUTDOWN(mp))
3188 return XFS_ERROR(EIO);
3190 link_namelen = VNAMELEN(dentry);
3193 * Check component lengths of the target path name.
3195 pathlen = strlen(target_path);
3196 if (pathlen >= MAXPATHLEN) /* total string too long */
3197 return XFS_ERROR(ENAMETOOLONG);
3198 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3199 int len, total;
3200 char *path;
3202 for (total = 0, path = target_path; total < pathlen;) {
3204 * Skip any slashes.
3206 while(*path == '/') {
3207 total++;
3208 path++;
3212 * Count up to the next slash or end of path.
3213 * Error out if the component is bigger than MAXNAMELEN.
3215 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3216 if (++len >= MAXNAMELEN) {
3217 error = ENAMETOOLONG;
3218 return error;
3224 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
3225 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3226 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3227 link_name, target_path, 0, 0, 0);
3228 if (error)
3229 return error;
3232 /* Return through std_return after this point. */
3234 udqp = gdqp = NULL;
3235 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3236 prid = dp->i_d.di_projid;
3237 else
3238 prid = (xfs_prid_t)dfltprid;
3241 * Make sure that we have allocated dquot(s) on disk.
3243 error = XFS_QM_DQVOPALLOC(mp, dp,
3244 current_fsuid(credp), current_fsgid(credp), prid,
3245 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3246 if (error)
3247 goto std_return;
3249 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3250 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3252 * The symlink will fit into the inode data fork?
3253 * There can't be any attributes so we get the whole variable part.
3255 if (pathlen <= XFS_LITINO(mp))
3256 fs_blocks = 0;
3257 else
3258 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3259 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3260 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3261 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3262 if (error == ENOSPC && fs_blocks == 0) {
3263 resblks = 0;
3264 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3265 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3267 if (error) {
3268 cancel_flags = 0;
3269 goto error_return;
3272 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
3273 unlock_dp_on_error = B_TRUE;
3276 * Check whether the directory allows new symlinks or not.
3278 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3279 error = XFS_ERROR(EPERM);
3280 goto error_return;
3284 * Reserve disk quota : blocks and inode.
3286 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3287 if (error)
3288 goto error_return;
3291 * Check for ability to enter directory entry, if no space reserved.
3293 if (resblks == 0 &&
3294 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
3295 goto error_return;
3297 * Initialize the bmap freelist prior to calling either
3298 * bmapi or the directory create code.
3300 XFS_BMAP_INIT(&free_list, &first_block);
3303 * Allocate an inode for the symlink.
3305 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
3306 1, 0, credp, prid, resblks > 0, &ip, NULL);
3307 if (error) {
3308 if (error == ENOSPC)
3309 goto error_return;
3310 goto error1;
3312 xfs_itrace_ref(ip);
3315 * An error after we've joined dp to the transaction will result in the
3316 * transaction cancel unlocking dp so don't do it explicitly in the
3317 * error path.
3319 VN_HOLD(dir_vp);
3320 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3321 unlock_dp_on_error = B_FALSE;
3324 * Also attach the dquot(s) to it, if applicable.
3326 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3328 if (resblks)
3329 resblks -= XFS_IALLOC_SPACE_RES(mp);
3331 * If the symlink will fit into the inode, write it inline.
3333 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3334 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3335 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3336 ip->i_d.di_size = pathlen;
3339 * The inode was initially created in extent format.
3341 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3342 ip->i_df.if_flags |= XFS_IFINLINE;
3344 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3345 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3347 } else {
3348 first_fsb = 0;
3349 nmaps = SYMLINK_MAPS;
3351 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3352 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3353 &first_block, resblks, mval, &nmaps,
3354 &free_list, NULL);
3355 if (error) {
3356 goto error1;
3359 if (resblks)
3360 resblks -= fs_blocks;
3361 ip->i_d.di_size = pathlen;
3362 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3364 cur_chunk = target_path;
3365 for (n = 0; n < nmaps; n++) {
3366 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3367 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3368 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3369 BTOBB(byte_cnt), 0);
3370 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3371 if (pathlen < byte_cnt) {
3372 byte_cnt = pathlen;
3374 pathlen -= byte_cnt;
3376 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3377 cur_chunk += byte_cnt;
3379 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3384 * Create the directory entry for the symlink.
3386 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3387 &first_block, &free_list, resblks);
3388 if (error)
3389 goto error1;
3390 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3391 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3394 * Bump the in memory version number of the parent directory
3395 * so that other processes accessing it will recognize that
3396 * the directory has changed.
3398 dp->i_gen++;
3401 * If this is a synchronous mount, make sure that the
3402 * symlink transaction goes to disk before returning to
3403 * the user.
3405 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3406 xfs_trans_set_sync(tp);
3410 * xfs_trans_commit normally decrements the vnode ref count
3411 * when it unlocks the inode. Since we want to return the
3412 * vnode to the caller, we bump the vnode ref count now.
3414 IHOLD(ip);
3416 error = xfs_bmap_finish(&tp, &free_list, &committed);
3417 if (error) {
3418 goto error2;
3420 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3421 XFS_QM_DQRELE(mp, udqp);
3422 XFS_QM_DQRELE(mp, gdqp);
3424 /* Fall through to std_return with error = 0 or errno from
3425 * xfs_trans_commit */
3426 std_return:
3427 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
3428 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3429 dir_vp, DM_RIGHT_NULL,
3430 error ? NULL : XFS_ITOV(ip),
3431 DM_RIGHT_NULL, link_name, target_path,
3432 0, error, 0);
3435 if (!error) {
3436 bhv_vnode_t *vp;
3438 ASSERT(ip);
3439 vp = XFS_ITOV(ip);
3440 *vpp = vp;
3442 return error;
3444 error2:
3445 IRELE(ip);
3446 error1:
3447 xfs_bmap_cancel(&free_list);
3448 cancel_flags |= XFS_TRANS_ABORT;
3449 error_return:
3450 xfs_trans_cancel(tp, cancel_flags);
3451 XFS_QM_DQRELE(mp, udqp);
3452 XFS_QM_DQRELE(mp, gdqp);
3454 if (unlock_dp_on_error)
3455 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3457 goto std_return;
3461 xfs_rwlock(
3462 xfs_inode_t *ip,
3463 bhv_vrwlock_t locktype)
3465 if (S_ISDIR(ip->i_d.di_mode))
3466 return 1;
3467 if (locktype == VRWLOCK_WRITE) {
3468 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3469 } else if (locktype == VRWLOCK_TRY_READ) {
3470 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
3471 } else if (locktype == VRWLOCK_TRY_WRITE) {
3472 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
3473 } else {
3474 ASSERT((locktype == VRWLOCK_READ) ||
3475 (locktype == VRWLOCK_WRITE_DIRECT));
3476 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3479 return 1;
3483 void
3484 xfs_rwunlock(
3485 xfs_inode_t *ip,
3486 bhv_vrwlock_t locktype)
3488 if (S_ISDIR(ip->i_d.di_mode))
3489 return;
3490 if (locktype == VRWLOCK_WRITE) {
3492 * In the write case, we may have added a new entry to
3493 * the reference cache. This might store a pointer to
3494 * an inode to be released in this inode. If it is there,
3495 * clear the pointer and release the inode after unlocking
3496 * this one.
3498 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3499 } else {
3500 ASSERT((locktype == VRWLOCK_READ) ||
3501 (locktype == VRWLOCK_WRITE_DIRECT));
3502 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3504 return;
3509 xfs_inode_flush(
3510 xfs_inode_t *ip,
3511 int flags)
3513 xfs_mount_t *mp = ip->i_mount;
3514 xfs_inode_log_item_t *iip = ip->i_itemp;
3515 int error = 0;
3517 if (XFS_FORCED_SHUTDOWN(mp))
3518 return XFS_ERROR(EIO);
3521 * Bypass inodes which have already been cleaned by
3522 * the inode flush clustering code inside xfs_iflush
3524 if ((ip->i_update_core == 0) &&
3525 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3526 return 0;
3528 if (flags & FLUSH_LOG) {
3529 if (iip && iip->ili_last_lsn) {
3530 xlog_t *log = mp->m_log;
3531 xfs_lsn_t sync_lsn;
3532 int log_flags = XFS_LOG_FORCE;
3534 spin_lock(&log->l_grant_lock);
3535 sync_lsn = log->l_last_sync_lsn;
3536 spin_unlock(&log->l_grant_lock);
3538 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) > 0)) {
3539 if (flags & FLUSH_SYNC)
3540 log_flags |= XFS_LOG_SYNC;
3541 error = xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3542 if (error)
3543 return error;
3546 if (ip->i_update_core == 0)
3547 return 0;
3552 * We make this non-blocking if the inode is contended,
3553 * return EAGAIN to indicate to the caller that they
3554 * did not succeed. This prevents the flush path from
3555 * blocking on inodes inside another operation right
3556 * now, they get caught later by xfs_sync.
3558 if (flags & FLUSH_INODE) {
3559 int flush_flags;
3561 if (flags & FLUSH_SYNC) {
3562 xfs_ilock(ip, XFS_ILOCK_SHARED);
3563 xfs_iflock(ip);
3564 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3565 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3566 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3567 return EAGAIN;
3569 } else {
3570 return EAGAIN;
3573 if (flags & FLUSH_SYNC)
3574 flush_flags = XFS_IFLUSH_SYNC;
3575 else
3576 flush_flags = XFS_IFLUSH_ASYNC;
3578 error = xfs_iflush(ip, flush_flags);
3579 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3582 return error;
3587 xfs_set_dmattrs(
3588 xfs_inode_t *ip,
3589 u_int evmask,
3590 u_int16_t state)
3592 xfs_mount_t *mp = ip->i_mount;
3593 xfs_trans_t *tp;
3594 int error;
3596 if (!capable(CAP_SYS_ADMIN))
3597 return XFS_ERROR(EPERM);
3599 if (XFS_FORCED_SHUTDOWN(mp))
3600 return XFS_ERROR(EIO);
3602 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3603 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3604 if (error) {
3605 xfs_trans_cancel(tp, 0);
3606 return error;
3608 xfs_ilock(ip, XFS_ILOCK_EXCL);
3609 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3611 ip->i_d.di_dmevmask = evmask;
3612 ip->i_d.di_dmstate = state;
3614 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3615 IHOLD(ip);
3616 error = xfs_trans_commit(tp, 0);
3618 return error;
3622 xfs_reclaim(
3623 xfs_inode_t *ip)
3625 bhv_vnode_t *vp = XFS_ITOV(ip);
3627 xfs_itrace_entry(ip);
3629 ASSERT(!VN_MAPPED(vp));
3631 /* bad inode, get out here ASAP */
3632 if (VN_BAD(vp)) {
3633 xfs_ireclaim(ip);
3634 return 0;
3637 vn_iowait(ip);
3639 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3642 * Make sure the atime in the XFS inode is correct before freeing the
3643 * Linux inode.
3645 xfs_synchronize_atime(ip);
3648 * If we have nothing to flush with this inode then complete the
3649 * teardown now, otherwise break the link between the xfs inode and the
3650 * linux inode and clean up the xfs inode later. This avoids flushing
3651 * the inode to disk during the delete operation itself.
3653 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3654 * first to ensure that xfs_iunpin() will never see an xfs inode
3655 * that has a linux inode being reclaimed. Synchronisation is provided
3656 * by the i_flags_lock.
3658 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3659 xfs_ilock(ip, XFS_ILOCK_EXCL);
3660 xfs_iflock(ip);
3661 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3662 } else {
3663 xfs_mount_t *mp = ip->i_mount;
3665 /* Protect sync and unpin from us */
3666 XFS_MOUNT_ILOCK(mp);
3667 spin_lock(&ip->i_flags_lock);
3668 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
3669 vn_to_inode(vp)->i_private = NULL;
3670 ip->i_vnode = NULL;
3671 spin_unlock(&ip->i_flags_lock);
3672 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3673 XFS_MOUNT_IUNLOCK(mp);
3675 return 0;
3679 xfs_finish_reclaim(
3680 xfs_inode_t *ip,
3681 int locked,
3682 int sync_mode)
3684 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
3685 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3686 int error;
3688 if (vp && VN_BAD(vp))
3689 goto reclaim;
3691 /* The hash lock here protects a thread in xfs_iget_core from
3692 * racing with us on linking the inode back with a vnode.
3693 * Once we have the XFS_IRECLAIM flag set it will not touch
3694 * us.
3696 write_lock(&pag->pag_ici_lock);
3697 spin_lock(&ip->i_flags_lock);
3698 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3699 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
3700 spin_unlock(&ip->i_flags_lock);
3701 write_unlock(&pag->pag_ici_lock);
3702 if (locked) {
3703 xfs_ifunlock(ip);
3704 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3706 return 1;
3708 __xfs_iflags_set(ip, XFS_IRECLAIM);
3709 spin_unlock(&ip->i_flags_lock);
3710 write_unlock(&pag->pag_ici_lock);
3711 xfs_put_perag(ip->i_mount, pag);
3714 * If the inode is still dirty, then flush it out. If the inode
3715 * is not in the AIL, then it will be OK to flush it delwri as
3716 * long as xfs_iflush() does not keep any references to the inode.
3717 * We leave that decision up to xfs_iflush() since it has the
3718 * knowledge of whether it's OK to simply do a delwri flush of
3719 * the inode or whether we need to wait until the inode is
3720 * pulled from the AIL.
3721 * We get the flush lock regardless, though, just to make sure
3722 * we don't free it while it is being flushed.
3724 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3725 if (!locked) {
3726 xfs_ilock(ip, XFS_ILOCK_EXCL);
3727 xfs_iflock(ip);
3730 if (ip->i_update_core ||
3731 ((ip->i_itemp != NULL) &&
3732 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3733 error = xfs_iflush(ip, sync_mode);
3735 * If we hit an error, typically because of filesystem
3736 * shutdown, we don't need to let vn_reclaim to know
3737 * because we're gonna reclaim the inode anyway.
3739 if (error) {
3740 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3741 goto reclaim;
3743 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3746 ASSERT(ip->i_update_core == 0);
3747 ASSERT(ip->i_itemp == NULL ||
3748 ip->i_itemp->ili_format.ilf_fields == 0);
3749 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3750 } else if (locked) {
3752 * We are not interested in doing an iflush if we're
3753 * in the process of shutting down the filesystem forcibly.
3754 * So, just reclaim the inode.
3756 xfs_ifunlock(ip);
3757 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3760 reclaim:
3761 xfs_ireclaim(ip);
3762 return 0;
3766 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3768 int purged;
3769 xfs_inode_t *ip, *n;
3770 int done = 0;
3772 while (!done) {
3773 purged = 0;
3774 XFS_MOUNT_ILOCK(mp);
3775 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3776 if (noblock) {
3777 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3778 continue;
3779 if (xfs_ipincount(ip) ||
3780 !xfs_iflock_nowait(ip)) {
3781 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3782 continue;
3785 XFS_MOUNT_IUNLOCK(mp);
3786 if (xfs_finish_reclaim(ip, noblock,
3787 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3788 delay(1);
3789 purged = 1;
3790 break;
3793 done = !purged;
3796 XFS_MOUNT_IUNLOCK(mp);
3797 return 0;
3801 * xfs_alloc_file_space()
3802 * This routine allocates disk space for the given file.
3804 * If alloc_type == 0, this request is for an ALLOCSP type
3805 * request which will change the file size. In this case, no
3806 * DMAPI event will be generated by the call. A TRUNCATE event
3807 * will be generated later by xfs_setattr.
3809 * If alloc_type != 0, this request is for a RESVSP type
3810 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3811 * lower block boundary byte address is less than the file's
3812 * length.
3814 * RETURNS:
3815 * 0 on success
3816 * errno on error
3819 STATIC int
3820 xfs_alloc_file_space(
3821 xfs_inode_t *ip,
3822 xfs_off_t offset,
3823 xfs_off_t len,
3824 int alloc_type,
3825 int attr_flags)
3827 xfs_mount_t *mp = ip->i_mount;
3828 xfs_off_t count;
3829 xfs_filblks_t allocated_fsb;
3830 xfs_filblks_t allocatesize_fsb;
3831 xfs_extlen_t extsz, temp;
3832 xfs_fileoff_t startoffset_fsb;
3833 xfs_fsblock_t firstfsb;
3834 int nimaps;
3835 int bmapi_flag;
3836 int quota_flag;
3837 int rt;
3838 xfs_trans_t *tp;
3839 xfs_bmbt_irec_t imaps[1], *imapp;
3840 xfs_bmap_free_t free_list;
3841 uint qblocks, resblks, resrtextents;
3842 int committed;
3843 int error;
3845 xfs_itrace_entry(ip);
3847 if (XFS_FORCED_SHUTDOWN(mp))
3848 return XFS_ERROR(EIO);
3850 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3851 return error;
3853 if (len <= 0)
3854 return XFS_ERROR(EINVAL);
3856 rt = XFS_IS_REALTIME_INODE(ip);
3857 extsz = xfs_get_extsz_hint(ip);
3859 count = len;
3860 imapp = &imaps[0];
3861 nimaps = 1;
3862 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
3863 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3864 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3866 /* Generate a DMAPI event if needed. */
3867 if (alloc_type != 0 && offset < ip->i_size &&
3868 (attr_flags&ATTR_DMI) == 0 &&
3869 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3870 xfs_off_t end_dmi_offset;
3872 end_dmi_offset = offset+len;
3873 if (end_dmi_offset > ip->i_size)
3874 end_dmi_offset = ip->i_size;
3875 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
3876 offset, end_dmi_offset - offset,
3877 0, NULL);
3878 if (error)
3879 return error;
3883 * Allocate file space until done or until there is an error
3885 retry:
3886 while (allocatesize_fsb && !error) {
3887 xfs_fileoff_t s, e;
3890 * Determine space reservations for data/realtime.
3892 if (unlikely(extsz)) {
3893 s = startoffset_fsb;
3894 do_div(s, extsz);
3895 s *= extsz;
3896 e = startoffset_fsb + allocatesize_fsb;
3897 if ((temp = do_mod(startoffset_fsb, extsz)))
3898 e += temp;
3899 if ((temp = do_mod(e, extsz)))
3900 e += extsz - temp;
3901 } else {
3902 s = 0;
3903 e = allocatesize_fsb;
3906 if (unlikely(rt)) {
3907 resrtextents = qblocks = (uint)(e - s);
3908 resrtextents /= mp->m_sb.sb_rextsize;
3909 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3910 quota_flag = XFS_QMOPT_RES_RTBLKS;
3911 } else {
3912 resrtextents = 0;
3913 resblks = qblocks = \
3914 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3915 quota_flag = XFS_QMOPT_RES_REGBLKS;
3919 * Allocate and setup the transaction.
3921 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3922 error = xfs_trans_reserve(tp, resblks,
3923 XFS_WRITE_LOG_RES(mp), resrtextents,
3924 XFS_TRANS_PERM_LOG_RES,
3925 XFS_WRITE_LOG_COUNT);
3927 * Check for running out of space
3929 if (error) {
3931 * Free the transaction structure.
3933 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3934 xfs_trans_cancel(tp, 0);
3935 break;
3937 xfs_ilock(ip, XFS_ILOCK_EXCL);
3938 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3939 qblocks, 0, quota_flag);
3940 if (error)
3941 goto error1;
3943 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3944 xfs_trans_ihold(tp, ip);
3947 * Issue the xfs_bmapi() call to allocate the blocks
3949 XFS_BMAP_INIT(&free_list, &firstfsb);
3950 error = xfs_bmapi(tp, ip, startoffset_fsb,
3951 allocatesize_fsb, bmapi_flag,
3952 &firstfsb, 0, imapp, &nimaps,
3953 &free_list, NULL);
3954 if (error) {
3955 goto error0;
3959 * Complete the transaction
3961 error = xfs_bmap_finish(&tp, &free_list, &committed);
3962 if (error) {
3963 goto error0;
3966 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3967 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3968 if (error) {
3969 break;
3972 allocated_fsb = imapp->br_blockcount;
3974 if (nimaps == 0) {
3975 error = XFS_ERROR(ENOSPC);
3976 break;
3979 startoffset_fsb += allocated_fsb;
3980 allocatesize_fsb -= allocated_fsb;
3982 dmapi_enospc_check:
3983 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
3984 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
3985 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
3986 XFS_ITOV(ip), DM_RIGHT_NULL,
3987 XFS_ITOV(ip), DM_RIGHT_NULL,
3988 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3989 if (error == 0)
3990 goto retry; /* Maybe DMAPI app. has made space */
3991 /* else fall through with error from XFS_SEND_DATA */
3994 return error;
3996 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3997 xfs_bmap_cancel(&free_list);
3998 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4000 error1: /* Just cancel transaction */
4001 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4002 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4003 goto dmapi_enospc_check;
4007 * Zero file bytes between startoff and endoff inclusive.
4008 * The iolock is held exclusive and no blocks are buffered.
4010 STATIC int
4011 xfs_zero_remaining_bytes(
4012 xfs_inode_t *ip,
4013 xfs_off_t startoff,
4014 xfs_off_t endoff)
4016 xfs_bmbt_irec_t imap;
4017 xfs_fileoff_t offset_fsb;
4018 xfs_off_t lastoffset;
4019 xfs_off_t offset;
4020 xfs_buf_t *bp;
4021 xfs_mount_t *mp = ip->i_mount;
4022 int nimap;
4023 int error = 0;
4025 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4026 XFS_IS_REALTIME_INODE(ip) ?
4027 mp->m_rtdev_targp : mp->m_ddev_targp);
4029 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4030 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4031 nimap = 1;
4032 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
4033 NULL, 0, &imap, &nimap, NULL, NULL);
4034 if (error || nimap < 1)
4035 break;
4036 ASSERT(imap.br_blockcount >= 1);
4037 ASSERT(imap.br_startoff == offset_fsb);
4038 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4039 if (lastoffset > endoff)
4040 lastoffset = endoff;
4041 if (imap.br_startblock == HOLESTARTBLOCK)
4042 continue;
4043 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4044 if (imap.br_state == XFS_EXT_UNWRITTEN)
4045 continue;
4046 XFS_BUF_UNDONE(bp);
4047 XFS_BUF_UNWRITE(bp);
4048 XFS_BUF_READ(bp);
4049 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4050 xfsbdstrat(mp, bp);
4051 if ((error = xfs_iowait(bp))) {
4052 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4053 mp, bp, XFS_BUF_ADDR(bp));
4054 break;
4056 memset(XFS_BUF_PTR(bp) +
4057 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4058 0, lastoffset - offset + 1);
4059 XFS_BUF_UNDONE(bp);
4060 XFS_BUF_UNREAD(bp);
4061 XFS_BUF_WRITE(bp);
4062 xfsbdstrat(mp, bp);
4063 if ((error = xfs_iowait(bp))) {
4064 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4065 mp, bp, XFS_BUF_ADDR(bp));
4066 break;
4069 xfs_buf_free(bp);
4070 return error;
4074 * xfs_free_file_space()
4075 * This routine frees disk space for the given file.
4077 * This routine is only called by xfs_change_file_space
4078 * for an UNRESVSP type call.
4080 * RETURNS:
4081 * 0 on success
4082 * errno on error
4085 STATIC int
4086 xfs_free_file_space(
4087 xfs_inode_t *ip,
4088 xfs_off_t offset,
4089 xfs_off_t len,
4090 int attr_flags)
4092 bhv_vnode_t *vp;
4093 int committed;
4094 int done;
4095 xfs_off_t end_dmi_offset;
4096 xfs_fileoff_t endoffset_fsb;
4097 int error;
4098 xfs_fsblock_t firstfsb;
4099 xfs_bmap_free_t free_list;
4100 xfs_bmbt_irec_t imap;
4101 xfs_off_t ioffset;
4102 xfs_extlen_t mod=0;
4103 xfs_mount_t *mp;
4104 int nimap;
4105 uint resblks;
4106 uint rounding;
4107 int rt;
4108 xfs_fileoff_t startoffset_fsb;
4109 xfs_trans_t *tp;
4110 int need_iolock = 1;
4112 vp = XFS_ITOV(ip);
4113 mp = ip->i_mount;
4115 xfs_itrace_entry(ip);
4117 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4118 return error;
4120 error = 0;
4121 if (len <= 0) /* if nothing being freed */
4122 return error;
4123 rt = XFS_IS_REALTIME_INODE(ip);
4124 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4125 end_dmi_offset = offset + len;
4126 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4128 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
4129 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
4130 if (end_dmi_offset > ip->i_size)
4131 end_dmi_offset = ip->i_size;
4132 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4133 offset, end_dmi_offset - offset,
4134 AT_DELAY_FLAG(attr_flags), NULL);
4135 if (error)
4136 return error;
4139 if (attr_flags & ATTR_NOLOCK)
4140 need_iolock = 0;
4141 if (need_iolock) {
4142 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4143 vn_iowait(ip); /* wait for the completion of any pending DIOs */
4146 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
4147 ioffset = offset & ~(rounding - 1);
4149 if (VN_CACHED(vp) != 0) {
4150 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
4151 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
4152 if (error)
4153 goto out_unlock_iolock;
4157 * Need to zero the stuff we're not freeing, on disk.
4158 * If its a realtime file & can't use unwritten extents then we
4159 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4160 * will take care of it for us.
4162 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4163 nimap = 1;
4164 error = xfs_bmapi(NULL, ip, startoffset_fsb,
4165 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4166 if (error)
4167 goto out_unlock_iolock;
4168 ASSERT(nimap == 0 || nimap == 1);
4169 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4170 xfs_daddr_t block;
4172 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4173 block = imap.br_startblock;
4174 mod = do_div(block, mp->m_sb.sb_rextsize);
4175 if (mod)
4176 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4178 nimap = 1;
4179 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
4180 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4181 if (error)
4182 goto out_unlock_iolock;
4183 ASSERT(nimap == 0 || nimap == 1);
4184 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4185 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4186 mod++;
4187 if (mod && (mod != mp->m_sb.sb_rextsize))
4188 endoffset_fsb -= mod;
4191 if ((done = (endoffset_fsb <= startoffset_fsb)))
4193 * One contiguous piece to clear
4195 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4196 else {
4198 * Some full blocks, possibly two pieces to clear
4200 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4201 error = xfs_zero_remaining_bytes(ip, offset,
4202 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4203 if (!error &&
4204 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4205 error = xfs_zero_remaining_bytes(ip,
4206 XFS_FSB_TO_B(mp, endoffset_fsb),
4207 offset + len - 1);
4211 * free file space until done or until there is an error
4213 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4214 while (!error && !done) {
4217 * allocate and setup the transaction. Allow this
4218 * transaction to dip into the reserve blocks to ensure
4219 * the freeing of the space succeeds at ENOSPC.
4221 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4222 tp->t_flags |= XFS_TRANS_RESERVE;
4223 error = xfs_trans_reserve(tp,
4224 resblks,
4225 XFS_WRITE_LOG_RES(mp),
4227 XFS_TRANS_PERM_LOG_RES,
4228 XFS_WRITE_LOG_COUNT);
4231 * check for running out of space
4233 if (error) {
4235 * Free the transaction structure.
4237 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4238 xfs_trans_cancel(tp, 0);
4239 break;
4241 xfs_ilock(ip, XFS_ILOCK_EXCL);
4242 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4243 ip->i_udquot, ip->i_gdquot, resblks, 0,
4244 XFS_QMOPT_RES_REGBLKS);
4245 if (error)
4246 goto error1;
4248 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4249 xfs_trans_ihold(tp, ip);
4252 * issue the bunmapi() call to free the blocks
4254 XFS_BMAP_INIT(&free_list, &firstfsb);
4255 error = xfs_bunmapi(tp, ip, startoffset_fsb,
4256 endoffset_fsb - startoffset_fsb,
4257 0, 2, &firstfsb, &free_list, NULL, &done);
4258 if (error) {
4259 goto error0;
4263 * complete the transaction
4265 error = xfs_bmap_finish(&tp, &free_list, &committed);
4266 if (error) {
4267 goto error0;
4270 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4271 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4274 out_unlock_iolock:
4275 if (need_iolock)
4276 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4277 return error;
4279 error0:
4280 xfs_bmap_cancel(&free_list);
4281 error1:
4282 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4283 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4284 XFS_ILOCK_EXCL);
4285 return error;
4289 * xfs_change_file_space()
4290 * This routine allocates or frees disk space for the given file.
4291 * The user specified parameters are checked for alignment and size
4292 * limitations.
4294 * RETURNS:
4295 * 0 on success
4296 * errno on error
4300 xfs_change_file_space(
4301 xfs_inode_t *ip,
4302 int cmd,
4303 xfs_flock64_t *bf,
4304 xfs_off_t offset,
4305 cred_t *credp,
4306 int attr_flags)
4308 xfs_mount_t *mp = ip->i_mount;
4309 int clrprealloc;
4310 int error;
4311 xfs_fsize_t fsize;
4312 int setprealloc;
4313 xfs_off_t startoffset;
4314 xfs_off_t llen;
4315 xfs_trans_t *tp;
4316 bhv_vattr_t va;
4318 xfs_itrace_entry(ip);
4321 * must be a regular file and have write permission
4323 if (!S_ISREG(ip->i_d.di_mode))
4324 return XFS_ERROR(EINVAL);
4326 xfs_ilock(ip, XFS_ILOCK_SHARED);
4328 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4329 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4330 return error;
4333 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4335 switch (bf->l_whence) {
4336 case 0: /*SEEK_SET*/
4337 break;
4338 case 1: /*SEEK_CUR*/
4339 bf->l_start += offset;
4340 break;
4341 case 2: /*SEEK_END*/
4342 bf->l_start += ip->i_size;
4343 break;
4344 default:
4345 return XFS_ERROR(EINVAL);
4348 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4350 if ( (bf->l_start < 0)
4351 || (bf->l_start > XFS_MAXIOFFSET(mp))
4352 || (bf->l_start + llen < 0)
4353 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4354 return XFS_ERROR(EINVAL);
4356 bf->l_whence = 0;
4358 startoffset = bf->l_start;
4359 fsize = ip->i_size;
4362 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4363 * file space.
4364 * These calls do NOT zero the data space allocated to the file,
4365 * nor do they change the file size.
4367 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4368 * space.
4369 * These calls cause the new file data to be zeroed and the file
4370 * size to be changed.
4372 setprealloc = clrprealloc = 0;
4374 switch (cmd) {
4375 case XFS_IOC_RESVSP:
4376 case XFS_IOC_RESVSP64:
4377 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4378 1, attr_flags);
4379 if (error)
4380 return error;
4381 setprealloc = 1;
4382 break;
4384 case XFS_IOC_UNRESVSP:
4385 case XFS_IOC_UNRESVSP64:
4386 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4387 attr_flags)))
4388 return error;
4389 break;
4391 case XFS_IOC_ALLOCSP:
4392 case XFS_IOC_ALLOCSP64:
4393 case XFS_IOC_FREESP:
4394 case XFS_IOC_FREESP64:
4395 if (startoffset > fsize) {
4396 error = xfs_alloc_file_space(ip, fsize,
4397 startoffset - fsize, 0, attr_flags);
4398 if (error)
4399 break;
4402 va.va_mask = XFS_AT_SIZE;
4403 va.va_size = startoffset;
4405 error = xfs_setattr(ip, &va, attr_flags, credp);
4407 if (error)
4408 return error;
4410 clrprealloc = 1;
4411 break;
4413 default:
4414 ASSERT(0);
4415 return XFS_ERROR(EINVAL);
4419 * update the inode timestamp, mode, and prealloc flag bits
4421 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4423 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4424 0, 0, 0))) {
4425 /* ASSERT(0); */
4426 xfs_trans_cancel(tp, 0);
4427 return error;
4430 xfs_ilock(ip, XFS_ILOCK_EXCL);
4432 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4433 xfs_trans_ihold(tp, ip);
4435 if ((attr_flags & ATTR_DMI) == 0) {
4436 ip->i_d.di_mode &= ~S_ISUID;
4439 * Note that we don't have to worry about mandatory
4440 * file locking being disabled here because we only
4441 * clear the S_ISGID bit if the Group execute bit is
4442 * on, but if it was on then mandatory locking wouldn't
4443 * have been enabled.
4445 if (ip->i_d.di_mode & S_IXGRP)
4446 ip->i_d.di_mode &= ~S_ISGID;
4448 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4450 if (setprealloc)
4451 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4452 else if (clrprealloc)
4453 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4455 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4456 xfs_trans_set_sync(tp);
4458 error = xfs_trans_commit(tp, 0);
4460 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4462 return error;