[CPUFREQ] ondemand governor automatic downscaling
[linux-2.6/mini2440.git] / fs / xfs / xfs_vnodeops.c
blob25a526629b125163c772815025fec6be0d283657
1 /*
2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include "xfs.h"
34 #include "xfs_macros.h"
35 #include "xfs_types.h"
36 #include "xfs_inum.h"
37 #include "xfs_log.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_ag.h"
41 #include "xfs_dir.h"
42 #include "xfs_dir2.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_alloc_btree.h"
46 #include "xfs_bmap_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_itable.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_alloc.h"
52 #include "xfs_attr_sf.h"
53 #include "xfs_dir_sf.h"
54 #include "xfs_dir2_sf.h"
55 #include "xfs_dinode.h"
56 #include "xfs_inode_item.h"
57 #include "xfs_inode.h"
58 #include "xfs_bmap.h"
59 #include "xfs_da_btree.h"
60 #include "xfs_attr.h"
61 #include "xfs_rw.h"
62 #include "xfs_refcache.h"
63 #include "xfs_error.h"
64 #include "xfs_bit.h"
65 #include "xfs_rtalloc.h"
66 #include "xfs_quota.h"
67 #include "xfs_utils.h"
68 #include "xfs_trans_space.h"
69 #include "xfs_dir_leaf.h"
70 #include "xfs_mac.h"
71 #include "xfs_log_priv.h"
75 * The maximum pathlen is 1024 bytes. Since the minimum file system
76 * blocksize is 512 bytes, we can get a max of 2 extents back from
77 * bmapi.
79 #define SYMLINK_MAPS 2
82 * For xfs, we check that the file isn't too big to be opened by this kernel.
83 * No other open action is required for regular files. Devices are handled
84 * through the specfs file system, pipes through fifofs. Device and
85 * fifo vnodes are "wrapped" by specfs and fifofs vnodes, respectively,
86 * when a new vnode is first looked up or created.
88 STATIC int
89 xfs_open(
90 bhv_desc_t *bdp,
91 cred_t *credp)
93 int mode;
94 vnode_t *vp;
95 xfs_inode_t *ip;
97 vp = BHV_TO_VNODE(bdp);
98 ip = XFS_BHVTOI(bdp);
100 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
101 return XFS_ERROR(EIO);
104 * If it's a directory with any blocks, read-ahead block 0
105 * as we're almost certain to have the next operation be a read there.
107 if (vp->v_type == VDIR && ip->i_d.di_nextents > 0) {
108 mode = xfs_ilock_map_shared(ip);
109 if (ip->i_d.di_nextents > 0)
110 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
111 xfs_iunlock(ip, mode);
113 return 0;
118 * xfs_getattr
120 STATIC int
121 xfs_getattr(
122 bhv_desc_t *bdp,
123 vattr_t *vap,
124 int flags,
125 cred_t *credp)
127 xfs_inode_t *ip;
128 xfs_mount_t *mp;
129 vnode_t *vp;
131 vp = BHV_TO_VNODE(bdp);
132 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
134 ip = XFS_BHVTOI(bdp);
135 mp = ip->i_mount;
137 if (XFS_FORCED_SHUTDOWN(mp))
138 return XFS_ERROR(EIO);
140 if (!(flags & ATTR_LAZY))
141 xfs_ilock(ip, XFS_ILOCK_SHARED);
143 vap->va_size = ip->i_d.di_size;
144 if (vap->va_mask == XFS_AT_SIZE)
145 goto all_done;
147 vap->va_nblocks =
148 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
149 vap->va_nodeid = ip->i_ino;
150 #if XFS_BIG_INUMS
151 vap->va_nodeid += mp->m_inoadd;
152 #endif
153 vap->va_nlink = ip->i_d.di_nlink;
156 * Quick exit for non-stat callers
158 if ((vap->va_mask &
159 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
160 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
161 goto all_done;
164 * Copy from in-core inode.
166 vap->va_type = vp->v_type;
167 vap->va_mode = ip->i_d.di_mode & MODEMASK;
168 vap->va_uid = ip->i_d.di_uid;
169 vap->va_gid = ip->i_d.di_gid;
170 vap->va_projid = ip->i_d.di_projid;
173 * Check vnode type block/char vs. everything else.
174 * Do it with bitmask because that's faster than looking
175 * for multiple values individually.
177 if (((1 << vp->v_type) & ((1<<VBLK) | (1<<VCHR))) == 0) {
178 vap->va_rdev = 0;
180 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
182 #if 0
183 /* Large block sizes confuse various
184 * user space programs, so letting the
185 * stripe size through is not a good
186 * idea for now.
188 vap->va_blocksize = mp->m_swidth ?
190 * If the underlying volume is a stripe, then
191 * return the stripe width in bytes as the
192 * recommended I/O size.
194 (mp->m_swidth << mp->m_sb.sb_blocklog) :
196 * Return the largest of the preferred buffer
197 * sizes since doing small I/Os into larger
198 * buffers causes buffers to be decommissioned.
199 * The value returned is in bytes.
201 (1 << (int)MAX(mp->m_readio_log,
202 mp->m_writeio_log));
204 #else
205 vap->va_blocksize =
207 * Return the largest of the preferred buffer
208 * sizes since doing small I/Os into larger
209 * buffers causes buffers to be decommissioned.
210 * The value returned is in bytes.
212 1 << (int)MAX(mp->m_readio_log,
213 mp->m_writeio_log);
214 #endif
215 } else {
218 * If the file blocks are being allocated from a
219 * realtime partition, then return the inode's
220 * realtime extent size or the realtime volume's
221 * extent size.
223 vap->va_blocksize = ip->i_d.di_extsize ?
224 (ip->i_d.di_extsize << mp->m_sb.sb_blocklog) :
225 (mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog);
227 } else {
228 vap->va_rdev = ip->i_df.if_u2.if_rdev;
229 vap->va_blocksize = BLKDEV_IOSIZE;
232 vap->va_atime.tv_sec = ip->i_d.di_atime.t_sec;
233 vap->va_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
234 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
235 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
236 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
237 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
240 * Exit for stat callers. See if any of the rest of the fields
241 * to be filled in are needed.
243 if ((vap->va_mask &
244 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
245 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
246 goto all_done;
249 * Convert di_flags to xflags.
251 vap->va_xflags = xfs_ip2xflags(ip);
254 * Exit for inode revalidate. See if any of the rest of
255 * the fields to be filled in are needed.
257 if ((vap->va_mask &
258 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
259 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
260 goto all_done;
262 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
263 vap->va_nextents =
264 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
265 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
266 ip->i_d.di_nextents;
267 if (ip->i_afp)
268 vap->va_anextents =
269 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
270 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
271 ip->i_d.di_anextents;
272 else
273 vap->va_anextents = 0;
274 vap->va_gen = ip->i_d.di_gen;
276 all_done:
277 if (!(flags & ATTR_LAZY))
278 xfs_iunlock(ip, XFS_ILOCK_SHARED);
279 return 0;
284 * xfs_setattr
287 xfs_setattr(
288 bhv_desc_t *bdp,
289 vattr_t *vap,
290 int flags,
291 cred_t *credp)
293 xfs_inode_t *ip;
294 xfs_trans_t *tp;
295 xfs_mount_t *mp;
296 int mask;
297 int code;
298 uint lock_flags;
299 uint commit_flags=0;
300 uid_t uid=0, iuid=0;
301 gid_t gid=0, igid=0;
302 int timeflags = 0;
303 vnode_t *vp;
304 xfs_prid_t projid=0, iprojid=0;
305 int mandlock_before, mandlock_after;
306 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
307 int file_owner;
308 int need_iolock = 1;
310 vp = BHV_TO_VNODE(bdp);
311 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
313 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
314 return XFS_ERROR(EROFS);
317 * Cannot set certain attributes.
319 mask = vap->va_mask;
320 if (mask & XFS_AT_NOSET) {
321 return XFS_ERROR(EINVAL);
324 ip = XFS_BHVTOI(bdp);
325 mp = ip->i_mount;
327 if (XFS_FORCED_SHUTDOWN(mp))
328 return XFS_ERROR(EIO);
331 * Timestamps do not need to be logged and hence do not
332 * need to be done within a transaction.
334 if (mask & XFS_AT_UPDTIMES) {
335 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
336 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
337 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
338 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
339 xfs_ichgtime(ip, timeflags);
340 return 0;
343 olddquot1 = olddquot2 = NULL;
344 udqp = gdqp = NULL;
347 * If disk quotas is on, we make sure that the dquots do exist on disk,
348 * before we start any other transactions. Trying to do this later
349 * is messy. We don't care to take a readlock to look at the ids
350 * in inode here, because we can't hold it across the trans_reserve.
351 * If the IDs do change before we take the ilock, we're covered
352 * because the i_*dquot fields will get updated anyway.
354 if (XFS_IS_QUOTA_ON(mp) && (mask & (XFS_AT_UID|XFS_AT_GID))) {
355 uint qflags = 0;
357 if (mask & XFS_AT_UID) {
358 uid = vap->va_uid;
359 qflags |= XFS_QMOPT_UQUOTA;
360 } else {
361 uid = ip->i_d.di_uid;
363 if (mask & XFS_AT_GID) {
364 gid = vap->va_gid;
365 qflags |= XFS_QMOPT_GQUOTA;
366 } else {
367 gid = ip->i_d.di_gid;
370 * We take a reference when we initialize udqp and gdqp,
371 * so it is important that we never blindly double trip on
372 * the same variable. See xfs_create() for an example.
374 ASSERT(udqp == NULL);
375 ASSERT(gdqp == NULL);
376 code = XFS_QM_DQVOPALLOC(mp, ip, uid,gid, qflags, &udqp, &gdqp);
377 if (code)
378 return (code);
382 * For the other attributes, we acquire the inode lock and
383 * first do an error checking pass.
385 tp = NULL;
386 lock_flags = XFS_ILOCK_EXCL;
387 ASSERT(flags & ATTR_NOLOCK ? flags & ATTR_DMI : 1);
388 if (flags & ATTR_NOLOCK)
389 need_iolock = 0;
390 if (!(mask & XFS_AT_SIZE)) {
391 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
392 (mp->m_flags & XFS_MOUNT_WSYNC)) {
393 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
394 commit_flags = 0;
395 if ((code = xfs_trans_reserve(tp, 0,
396 XFS_ICHANGE_LOG_RES(mp), 0,
397 0, 0))) {
398 lock_flags = 0;
399 goto error_return;
402 } else {
403 if (DM_EVENT_ENABLED (vp->v_vfsp, ip, DM_EVENT_TRUNCATE) &&
404 !(flags & ATTR_DMI)) {
405 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
406 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
407 vap->va_size, 0, dmflags, NULL);
408 if (code) {
409 lock_flags = 0;
410 goto error_return;
413 if (need_iolock)
414 lock_flags |= XFS_IOLOCK_EXCL;
417 xfs_ilock(ip, lock_flags);
419 /* boolean: are we the file owner? */
420 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
423 * Change various properties of a file.
424 * Only the owner or users with CAP_FOWNER
425 * capability may do these things.
427 if (mask &
428 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
429 XFS_AT_GID|XFS_AT_PROJID)) {
431 * CAP_FOWNER overrides the following restrictions:
433 * The user ID of the calling process must be equal
434 * to the file owner ID, except in cases where the
435 * CAP_FSETID capability is applicable.
437 if (!file_owner && !capable(CAP_FOWNER)) {
438 code = XFS_ERROR(EPERM);
439 goto error_return;
443 * CAP_FSETID overrides the following restrictions:
445 * The effective user ID of the calling process shall match
446 * the file owner when setting the set-user-ID and
447 * set-group-ID bits on that file.
449 * The effective group ID or one of the supplementary group
450 * IDs of the calling process shall match the group owner of
451 * the file when setting the set-group-ID bit on that file
453 if (mask & XFS_AT_MODE) {
454 mode_t m = 0;
456 if ((vap->va_mode & S_ISUID) && !file_owner)
457 m |= S_ISUID;
458 if ((vap->va_mode & S_ISGID) &&
459 !in_group_p((gid_t)ip->i_d.di_gid))
460 m |= S_ISGID;
461 #if 0
462 /* Linux allows this, Irix doesn't. */
463 if ((vap->va_mode & S_ISVTX) && vp->v_type != VDIR)
464 m |= S_ISVTX;
465 #endif
466 if (m && !capable(CAP_FSETID))
467 vap->va_mode &= ~m;
472 * Change file ownership. Must be the owner or privileged.
473 * If the system was configured with the "restricted_chown"
474 * option, the owner is not permitted to give away the file,
475 * and can change the group id only to a group of which he
476 * or she is a member.
478 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
480 * These IDs could have changed since we last looked at them.
481 * But, we're assured that if the ownership did change
482 * while we didn't have the inode locked, inode's dquot(s)
483 * would have changed also.
485 iuid = ip->i_d.di_uid;
486 iprojid = ip->i_d.di_projid;
487 igid = ip->i_d.di_gid;
488 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
489 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
490 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
491 iprojid;
494 * CAP_CHOWN overrides the following restrictions:
496 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
497 * shall override the restriction that a process cannot
498 * change the user ID of a file it owns and the restriction
499 * that the group ID supplied to the chown() function
500 * shall be equal to either the group ID or one of the
501 * supplementary group IDs of the calling process.
503 * XXX: How does restricted_chown affect projid?
505 if (restricted_chown &&
506 (iuid != uid || (igid != gid &&
507 !in_group_p((gid_t)gid))) &&
508 !capable(CAP_CHOWN)) {
509 code = XFS_ERROR(EPERM);
510 goto error_return;
513 * Do a quota reservation only if uid or gid is actually
514 * going to change.
516 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
517 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
518 ASSERT(tp);
519 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
520 capable(CAP_FOWNER) ?
521 XFS_QMOPT_FORCE_RES : 0);
522 if (code) /* out of quota */
523 goto error_return;
528 * Truncate file. Must have write permission and not be a directory.
530 if (mask & XFS_AT_SIZE) {
531 /* Short circuit the truncate case for zero length files */
532 if ((vap->va_size == 0) &&
533 (ip->i_d.di_size == 0) && (ip->i_d.di_nextents == 0)) {
534 xfs_iunlock(ip, XFS_ILOCK_EXCL);
535 lock_flags &= ~XFS_ILOCK_EXCL;
536 if (mask & XFS_AT_CTIME)
537 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
538 code = 0;
539 goto error_return;
542 if (vp->v_type == VDIR) {
543 code = XFS_ERROR(EISDIR);
544 goto error_return;
545 } else if (vp->v_type != VREG) {
546 code = XFS_ERROR(EINVAL);
547 goto error_return;
550 * Make sure that the dquots are attached to the inode.
552 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
553 goto error_return;
557 * Change file access or modified times.
559 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
560 if (!file_owner) {
561 if ((flags & ATTR_UTIME) &&
562 !capable(CAP_FOWNER)) {
563 code = XFS_ERROR(EPERM);
564 goto error_return;
570 * Change extent size or realtime flag.
572 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
574 * Can't change extent size if any extents are allocated.
576 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
577 (mask & XFS_AT_EXTSIZE) &&
578 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
579 vap->va_extsize) ) {
580 code = XFS_ERROR(EINVAL); /* EFBIG? */
581 goto error_return;
585 * Can't set extent size unless the file is marked, or
586 * about to be marked as a realtime file.
588 * This check will be removed when fixed size extents
589 * with buffered data writes is implemented.
592 if ((mask & XFS_AT_EXTSIZE) &&
593 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
594 vap->va_extsize) &&
595 (!((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
596 ((mask & XFS_AT_XFLAGS) &&
597 (vap->va_xflags & XFS_XFLAG_REALTIME))))) {
598 code = XFS_ERROR(EINVAL);
599 goto error_return;
603 * Can't change realtime flag if any extents are allocated.
605 if (ip->i_d.di_nextents && (mask & XFS_AT_XFLAGS) &&
606 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
607 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
608 code = XFS_ERROR(EINVAL); /* EFBIG? */
609 goto error_return;
612 * Extent size must be a multiple of the appropriate block
613 * size, if set at all.
615 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
616 xfs_extlen_t size;
618 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
619 ((mask & XFS_AT_XFLAGS) &&
620 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
621 size = mp->m_sb.sb_rextsize <<
622 mp->m_sb.sb_blocklog;
623 } else {
624 size = mp->m_sb.sb_blocksize;
626 if (vap->va_extsize % size) {
627 code = XFS_ERROR(EINVAL);
628 goto error_return;
632 * If realtime flag is set then must have realtime data.
634 if ((mask & XFS_AT_XFLAGS) &&
635 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
636 if ((mp->m_sb.sb_rblocks == 0) ||
637 (mp->m_sb.sb_rextsize == 0) ||
638 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
639 code = XFS_ERROR(EINVAL);
640 goto error_return;
645 * Can't modify an immutable/append-only file unless
646 * we have appropriate permission.
648 if ((mask & XFS_AT_XFLAGS) &&
649 (ip->i_d.di_flags &
650 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
651 (vap->va_xflags &
652 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
653 !capable(CAP_LINUX_IMMUTABLE)) {
654 code = XFS_ERROR(EPERM);
655 goto error_return;
660 * Now we can make the changes. Before we join the inode
661 * to the transaction, if XFS_AT_SIZE is set then take care of
662 * the part of the truncation that must be done without the
663 * inode lock. This needs to be done before joining the inode
664 * to the transaction, because the inode cannot be unlocked
665 * once it is a part of the transaction.
667 if (mask & XFS_AT_SIZE) {
668 code = 0;
669 if (vap->va_size > ip->i_d.di_size)
670 code = xfs_igrow_start(ip, vap->va_size, credp);
671 xfs_iunlock(ip, XFS_ILOCK_EXCL);
672 if (!code)
673 code = xfs_itruncate_data(ip, vap->va_size);
674 if (code) {
675 ASSERT(tp == NULL);
676 lock_flags &= ~XFS_ILOCK_EXCL;
677 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
678 goto error_return;
680 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
681 if ((code = xfs_trans_reserve(tp, 0,
682 XFS_ITRUNCATE_LOG_RES(mp), 0,
683 XFS_TRANS_PERM_LOG_RES,
684 XFS_ITRUNCATE_LOG_COUNT))) {
685 xfs_trans_cancel(tp, 0);
686 if (need_iolock)
687 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
688 return code;
690 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
691 xfs_ilock(ip, XFS_ILOCK_EXCL);
694 if (tp) {
695 xfs_trans_ijoin(tp, ip, lock_flags);
696 xfs_trans_ihold(tp, ip);
699 /* determine whether mandatory locking mode changes */
700 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
703 * Truncate file. Must have write permission and not be a directory.
705 if (mask & XFS_AT_SIZE) {
706 if (vap->va_size > ip->i_d.di_size) {
707 xfs_igrow_finish(tp, ip, vap->va_size,
708 !(flags & ATTR_DMI));
709 } else if ((vap->va_size <= ip->i_d.di_size) ||
710 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
712 * signal a sync transaction unless
713 * we're truncating an already unlinked
714 * file on a wsync filesystem
716 code = xfs_itruncate_finish(&tp, ip,
717 (xfs_fsize_t)vap->va_size,
718 XFS_DATA_FORK,
719 ((ip->i_d.di_nlink != 0 ||
720 !(mp->m_flags & XFS_MOUNT_WSYNC))
721 ? 1 : 0));
722 if (code) {
723 goto abort_return;
727 * Have to do this even if the file's size doesn't change.
729 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
733 * Change file access modes.
735 if (mask & XFS_AT_MODE) {
736 ip->i_d.di_mode &= S_IFMT;
737 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
739 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
740 timeflags |= XFS_ICHGTIME_CHG;
744 * Change file ownership. Must be the owner or privileged.
745 * If the system was configured with the "restricted_chown"
746 * option, the owner is not permitted to give away the file,
747 * and can change the group id only to a group of which he
748 * or she is a member.
750 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
752 * CAP_FSETID overrides the following restrictions:
754 * The set-user-ID and set-group-ID bits of a file will be
755 * cleared upon successful return from chown()
757 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
758 !capable(CAP_FSETID)) {
759 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
763 * Change the ownerships and register quota modifications
764 * in the transaction.
766 if (iuid != uid) {
767 if (XFS_IS_UQUOTA_ON(mp)) {
768 ASSERT(mask & XFS_AT_UID);
769 ASSERT(udqp);
770 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
771 &ip->i_udquot, udqp);
773 ip->i_d.di_uid = uid;
775 if (igid != gid) {
776 if (XFS_IS_GQUOTA_ON(mp)) {
777 ASSERT(mask & XFS_AT_GID);
778 ASSERT(gdqp);
779 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
780 &ip->i_gdquot, gdqp);
782 ip->i_d.di_gid = gid;
784 if (iprojid != projid) {
785 ip->i_d.di_projid = projid;
787 * We may have to rev the inode as well as
788 * the superblock version number since projids didn't
789 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
791 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
792 xfs_bump_ino_vers2(tp, ip);
795 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
796 timeflags |= XFS_ICHGTIME_CHG;
801 * Change file access or modified times.
803 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
804 if (mask & XFS_AT_ATIME) {
805 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
806 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
807 ip->i_update_core = 1;
808 timeflags &= ~XFS_ICHGTIME_ACC;
810 if (mask & XFS_AT_MTIME) {
811 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
812 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
813 timeflags &= ~XFS_ICHGTIME_MOD;
814 timeflags |= XFS_ICHGTIME_CHG;
816 if (tp && (flags & ATTR_UTIME))
817 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
821 * Change XFS-added attributes.
823 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
824 if (mask & XFS_AT_EXTSIZE) {
826 * Converting bytes to fs blocks.
828 ip->i_d.di_extsize = vap->va_extsize >>
829 mp->m_sb.sb_blocklog;
831 if (mask & XFS_AT_XFLAGS) {
832 uint di_flags;
834 /* can't set PREALLOC this way, just preserve it */
835 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
836 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
837 di_flags |= XFS_DIFLAG_IMMUTABLE;
838 if (vap->va_xflags & XFS_XFLAG_APPEND)
839 di_flags |= XFS_DIFLAG_APPEND;
840 if (vap->va_xflags & XFS_XFLAG_SYNC)
841 di_flags |= XFS_DIFLAG_SYNC;
842 if (vap->va_xflags & XFS_XFLAG_NOATIME)
843 di_flags |= XFS_DIFLAG_NOATIME;
844 if (vap->va_xflags & XFS_XFLAG_NODUMP)
845 di_flags |= XFS_DIFLAG_NODUMP;
846 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
847 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
848 di_flags |= XFS_DIFLAG_RTINHERIT;
849 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
850 di_flags |= XFS_DIFLAG_NOSYMLINKS;
851 } else {
852 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
853 di_flags |= XFS_DIFLAG_REALTIME;
854 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
855 } else {
856 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
859 ip->i_d.di_flags = di_flags;
861 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
862 timeflags |= XFS_ICHGTIME_CHG;
866 * Change file inode change time only if XFS_AT_CTIME set
867 * AND we have been called by a DMI function.
870 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
871 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
872 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
873 ip->i_update_core = 1;
874 timeflags &= ~XFS_ICHGTIME_CHG;
878 * Send out timestamp changes that need to be set to the
879 * current time. Not done when called by a DMI function.
881 if (timeflags && !(flags & ATTR_DMI))
882 xfs_ichgtime(ip, timeflags);
884 XFS_STATS_INC(xs_ig_attrchg);
887 * If this is a synchronous mount, make sure that the
888 * transaction goes to disk before returning to the user.
889 * This is slightly sub-optimal in that truncates require
890 * two sync transactions instead of one for wsync filesytems.
891 * One for the truncate and one for the timestamps since we
892 * don't want to change the timestamps unless we're sure the
893 * truncate worked. Truncates are less than 1% of the laddis
894 * mix so this probably isn't worth the trouble to optimize.
896 code = 0;
897 if (tp) {
898 if (mp->m_flags & XFS_MOUNT_WSYNC)
899 xfs_trans_set_sync(tp);
901 code = xfs_trans_commit(tp, commit_flags, NULL);
905 * If the (regular) file's mandatory locking mode changed, then
906 * notify the vnode. We do this under the inode lock to prevent
907 * racing calls to vop_vnode_change.
909 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
910 if (mandlock_before != mandlock_after) {
911 VOP_VNODE_CHANGE(vp, VCHANGE_FLAGS_ENF_LOCKING,
912 mandlock_after);
915 xfs_iunlock(ip, lock_flags);
918 * Release any dquot(s) the inode had kept before chown.
920 XFS_QM_DQRELE(mp, olddquot1);
921 XFS_QM_DQRELE(mp, olddquot2);
922 XFS_QM_DQRELE(mp, udqp);
923 XFS_QM_DQRELE(mp, gdqp);
925 if (code) {
926 return code;
929 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
930 !(flags & ATTR_DMI)) {
931 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
932 NULL, DM_RIGHT_NULL, NULL, NULL,
933 0, 0, AT_DELAY_FLAG(flags));
935 return 0;
937 abort_return:
938 commit_flags |= XFS_TRANS_ABORT;
939 /* FALLTHROUGH */
940 error_return:
941 XFS_QM_DQRELE(mp, udqp);
942 XFS_QM_DQRELE(mp, gdqp);
943 if (tp) {
944 xfs_trans_cancel(tp, commit_flags);
946 if (lock_flags != 0) {
947 xfs_iunlock(ip, lock_flags);
949 return code;
954 * xfs_access
955 * Null conversion from vnode mode bits to inode mode bits, as in efs.
957 STATIC int
958 xfs_access(
959 bhv_desc_t *bdp,
960 int mode,
961 cred_t *credp)
963 xfs_inode_t *ip;
964 int error;
966 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
967 (inst_t *)__return_address);
969 ip = XFS_BHVTOI(bdp);
970 xfs_ilock(ip, XFS_ILOCK_SHARED);
971 error = xfs_iaccess(ip, mode, credp);
972 xfs_iunlock(ip, XFS_ILOCK_SHARED);
973 return error;
978 * xfs_readlink
981 STATIC int
982 xfs_readlink(
983 bhv_desc_t *bdp,
984 uio_t *uiop,
985 int ioflags,
986 cred_t *credp)
988 xfs_inode_t *ip;
989 int count;
990 xfs_off_t offset;
991 int pathlen;
992 vnode_t *vp;
993 int error = 0;
994 xfs_mount_t *mp;
995 int nmaps;
996 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
997 xfs_daddr_t d;
998 int byte_cnt;
999 int n;
1000 xfs_buf_t *bp;
1002 vp = BHV_TO_VNODE(bdp);
1003 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1005 ip = XFS_BHVTOI(bdp);
1006 mp = ip->i_mount;
1008 if (XFS_FORCED_SHUTDOWN(mp))
1009 return XFS_ERROR(EIO);
1011 xfs_ilock(ip, XFS_ILOCK_SHARED);
1013 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
1015 offset = uiop->uio_offset;
1016 count = uiop->uio_resid;
1018 if (offset < 0) {
1019 error = XFS_ERROR(EINVAL);
1020 goto error_return;
1022 if (count <= 0) {
1023 error = 0;
1024 goto error_return;
1027 if (!(ioflags & IO_INVIS)) {
1028 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
1032 * See if the symlink is stored inline.
1034 pathlen = (int)ip->i_d.di_size;
1036 if (ip->i_df.if_flags & XFS_IFINLINE) {
1037 error = uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
1039 else {
1041 * Symlink not inline. Call bmap to get it in.
1043 nmaps = SYMLINK_MAPS;
1045 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
1046 0, NULL, 0, mval, &nmaps, NULL);
1048 if (error) {
1049 goto error_return;
1052 for (n = 0; n < nmaps; n++) {
1053 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1054 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1055 bp = xfs_buf_read(mp->m_ddev_targp, d,
1056 BTOBB(byte_cnt), 0);
1057 error = XFS_BUF_GETERROR(bp);
1058 if (error) {
1059 xfs_ioerror_alert("xfs_readlink",
1060 ip->i_mount, bp, XFS_BUF_ADDR(bp));
1061 xfs_buf_relse(bp);
1062 goto error_return;
1064 if (pathlen < byte_cnt)
1065 byte_cnt = pathlen;
1066 pathlen -= byte_cnt;
1068 error = uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
1069 xfs_buf_relse (bp);
1075 error_return:
1077 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1079 return error;
1084 * xfs_fsync
1086 * This is called to sync the inode and its data out to disk.
1087 * We need to hold the I/O lock while flushing the data, and
1088 * the inode lock while flushing the inode. The inode lock CANNOT
1089 * be held while flushing the data, so acquire after we're done
1090 * with that.
1092 STATIC int
1093 xfs_fsync(
1094 bhv_desc_t *bdp,
1095 int flag,
1096 cred_t *credp,
1097 xfs_off_t start,
1098 xfs_off_t stop)
1100 xfs_inode_t *ip;
1101 xfs_trans_t *tp;
1102 int error;
1104 vn_trace_entry(BHV_TO_VNODE(bdp),
1105 __FUNCTION__, (inst_t *)__return_address);
1107 ip = XFS_BHVTOI(bdp);
1109 ASSERT(start >= 0 && stop >= -1);
1111 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1112 return XFS_ERROR(EIO);
1115 * We always need to make sure that the required inode state
1116 * is safe on disk. The vnode might be clean but because
1117 * of committed transactions that haven't hit the disk yet.
1118 * Likewise, there could be unflushed non-transactional
1119 * changes to the inode core that have to go to disk.
1121 * The following code depends on one assumption: that
1122 * any transaction that changes an inode logs the core
1123 * because it has to change some field in the inode core
1124 * (typically nextents or nblocks). That assumption
1125 * implies that any transactions against an inode will
1126 * catch any non-transactional updates. If inode-altering
1127 * transactions exist that violate this assumption, the
1128 * code breaks. Right now, it figures that if the involved
1129 * update_* field is clear and the inode is unpinned, the
1130 * inode is clean. Either it's been flushed or it's been
1131 * committed and the commit has hit the disk unpinning the inode.
1132 * (Note that xfs_inode_item_format() called at commit clears
1133 * the update_* fields.)
1135 xfs_ilock(ip, XFS_ILOCK_SHARED);
1137 /* If we are flushing data then we care about update_size
1138 * being set, otherwise we care about update_core
1140 if ((flag & FSYNC_DATA) ?
1141 (ip->i_update_size == 0) :
1142 (ip->i_update_core == 0)) {
1144 * Timestamps/size haven't changed since last inode
1145 * flush or inode transaction commit. That means
1146 * either nothing got written or a transaction
1147 * committed which caught the updates. If the
1148 * latter happened and the transaction hasn't
1149 * hit the disk yet, the inode will be still
1150 * be pinned. If it is, force the log.
1153 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1155 if (xfs_ipincount(ip)) {
1156 xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1157 XFS_LOG_FORCE |
1158 ((flag & FSYNC_WAIT)
1159 ? XFS_LOG_SYNC : 0));
1161 error = 0;
1162 } else {
1164 * Kick off a transaction to log the inode
1165 * core to get the updates. Make it
1166 * sync if FSYNC_WAIT is passed in (which
1167 * is done by everybody but specfs). The
1168 * sync transaction will also force the log.
1170 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1171 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1172 if ((error = xfs_trans_reserve(tp, 0,
1173 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1174 0, 0, 0))) {
1175 xfs_trans_cancel(tp, 0);
1176 return error;
1178 xfs_ilock(ip, XFS_ILOCK_EXCL);
1181 * Note - it's possible that we might have pushed
1182 * ourselves out of the way during trans_reserve
1183 * which would flush the inode. But there's no
1184 * guarantee that the inode buffer has actually
1185 * gone out yet (it's delwri). Plus the buffer
1186 * could be pinned anyway if it's part of an
1187 * inode in another recent transaction. So we
1188 * play it safe and fire off the transaction anyway.
1190 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1191 xfs_trans_ihold(tp, ip);
1192 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1193 if (flag & FSYNC_WAIT)
1194 xfs_trans_set_sync(tp);
1195 error = xfs_trans_commit(tp, 0, NULL);
1197 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1199 return error;
1203 * This is called by xfs_inactive to free any blocks beyond eof,
1204 * when the link count isn't zero.
1206 STATIC int
1207 xfs_inactive_free_eofblocks(
1208 xfs_mount_t *mp,
1209 xfs_inode_t *ip)
1211 xfs_trans_t *tp;
1212 int error;
1213 xfs_fileoff_t end_fsb;
1214 xfs_fileoff_t last_fsb;
1215 xfs_filblks_t map_len;
1216 int nimaps;
1217 xfs_bmbt_irec_t imap;
1220 * Figure out if there are any blocks beyond the end
1221 * of the file. If not, then there is nothing to do.
1223 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_d.di_size));
1224 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1225 map_len = last_fsb - end_fsb;
1226 if (map_len <= 0)
1227 return (0);
1229 nimaps = 1;
1230 xfs_ilock(ip, XFS_ILOCK_SHARED);
1231 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
1232 NULL, 0, &imap, &nimaps, NULL);
1233 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1235 if (!error && (nimaps != 0) &&
1236 (imap.br_startblock != HOLESTARTBLOCK)) {
1238 * Attach the dquots to the inode up front.
1240 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1241 return (error);
1244 * There are blocks after the end of file.
1245 * Free them up now by truncating the file to
1246 * its current size.
1248 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1251 * Do the xfs_itruncate_start() call before
1252 * reserving any log space because
1253 * itruncate_start will call into the buffer
1254 * cache and we can't
1255 * do that within a transaction.
1257 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1258 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1259 ip->i_d.di_size);
1261 error = xfs_trans_reserve(tp, 0,
1262 XFS_ITRUNCATE_LOG_RES(mp),
1263 0, XFS_TRANS_PERM_LOG_RES,
1264 XFS_ITRUNCATE_LOG_COUNT);
1265 if (error) {
1266 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1267 xfs_trans_cancel(tp, 0);
1268 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1269 return (error);
1272 xfs_ilock(ip, XFS_ILOCK_EXCL);
1273 xfs_trans_ijoin(tp, ip,
1274 XFS_IOLOCK_EXCL |
1275 XFS_ILOCK_EXCL);
1276 xfs_trans_ihold(tp, ip);
1278 error = xfs_itruncate_finish(&tp, ip,
1279 ip->i_d.di_size,
1280 XFS_DATA_FORK,
1283 * If we get an error at this point we
1284 * simply don't bother truncating the file.
1286 if (error) {
1287 xfs_trans_cancel(tp,
1288 (XFS_TRANS_RELEASE_LOG_RES |
1289 XFS_TRANS_ABORT));
1290 } else {
1291 error = xfs_trans_commit(tp,
1292 XFS_TRANS_RELEASE_LOG_RES,
1293 NULL);
1295 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1297 return (error);
1301 * Free a symlink that has blocks associated with it.
1303 STATIC int
1304 xfs_inactive_symlink_rmt(
1305 xfs_inode_t *ip,
1306 xfs_trans_t **tpp)
1308 xfs_buf_t *bp;
1309 int committed;
1310 int done;
1311 int error;
1312 xfs_fsblock_t first_block;
1313 xfs_bmap_free_t free_list;
1314 int i;
1315 xfs_mount_t *mp;
1316 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1317 int nmaps;
1318 xfs_trans_t *ntp;
1319 int size;
1320 xfs_trans_t *tp;
1322 tp = *tpp;
1323 mp = ip->i_mount;
1324 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1326 * We're freeing a symlink that has some
1327 * blocks allocated to it. Free the
1328 * blocks here. We know that we've got
1329 * either 1 or 2 extents and that we can
1330 * free them all in one bunmapi call.
1332 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1333 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1334 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1335 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1336 xfs_trans_cancel(tp, 0);
1337 *tpp = NULL;
1338 return error;
1341 * Lock the inode, fix the size, and join it to the transaction.
1342 * Hold it so in the normal path, we still have it locked for
1343 * the second transaction. In the error paths we need it
1344 * held so the cancel won't rele it, see below.
1346 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1347 size = (int)ip->i_d.di_size;
1348 ip->i_d.di_size = 0;
1349 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1350 xfs_trans_ihold(tp, ip);
1351 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1353 * Find the block(s) so we can inval and unmap them.
1355 done = 0;
1356 XFS_BMAP_INIT(&free_list, &first_block);
1357 nmaps = sizeof(mval) / sizeof(mval[0]);
1358 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1359 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1360 &free_list)))
1361 goto error0;
1363 * Invalidate the block(s).
1365 for (i = 0; i < nmaps; i++) {
1366 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1367 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1368 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1369 xfs_trans_binval(tp, bp);
1372 * Unmap the dead block(s) to the free_list.
1374 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1375 &first_block, &free_list, &done)))
1376 goto error1;
1377 ASSERT(done);
1379 * Commit the first transaction. This logs the EFI and the inode.
1381 if ((error = xfs_bmap_finish(&tp, &free_list, first_block, &committed)))
1382 goto error1;
1384 * The transaction must have been committed, since there were
1385 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1386 * The new tp has the extent freeing and EFDs.
1388 ASSERT(committed);
1390 * The first xact was committed, so add the inode to the new one.
1391 * Mark it dirty so it will be logged and moved forward in the log as
1392 * part of every commit.
1394 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1395 xfs_trans_ihold(tp, ip);
1396 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1398 * Get a new, empty transaction to return to our caller.
1400 ntp = xfs_trans_dup(tp);
1402 * Commit the transaction containing extent freeing and EFD's.
1403 * If we get an error on the commit here or on the reserve below,
1404 * we need to unlock the inode since the new transaction doesn't
1405 * have the inode attached.
1407 error = xfs_trans_commit(tp, 0, NULL);
1408 tp = ntp;
1409 if (error) {
1410 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1411 goto error0;
1414 * Remove the memory for extent descriptions (just bookkeeping).
1416 if (ip->i_df.if_bytes)
1417 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1418 ASSERT(ip->i_df.if_bytes == 0);
1420 * Put an itruncate log reservation in the new transaction
1421 * for our caller.
1423 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1424 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1425 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1426 goto error0;
1429 * Return with the inode locked but not joined to the transaction.
1431 *tpp = tp;
1432 return 0;
1434 error1:
1435 xfs_bmap_cancel(&free_list);
1436 error0:
1438 * Have to come here with the inode locked and either
1439 * (held and in the transaction) or (not in the transaction).
1440 * If the inode isn't held then cancel would iput it, but
1441 * that's wrong since this is inactive and the vnode ref
1442 * count is 0 already.
1443 * Cancel won't do anything to the inode if held, but it still
1444 * needs to be locked until the cancel is done, if it was
1445 * joined to the transaction.
1447 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1448 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1449 *tpp = NULL;
1450 return error;
1454 STATIC int
1455 xfs_inactive_symlink_local(
1456 xfs_inode_t *ip,
1457 xfs_trans_t **tpp)
1459 int error;
1461 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1463 * We're freeing a symlink which fit into
1464 * the inode. Just free the memory used
1465 * to hold the old symlink.
1467 error = xfs_trans_reserve(*tpp, 0,
1468 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1469 0, XFS_TRANS_PERM_LOG_RES,
1470 XFS_ITRUNCATE_LOG_COUNT);
1472 if (error) {
1473 xfs_trans_cancel(*tpp, 0);
1474 *tpp = NULL;
1475 return (error);
1477 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1480 * Zero length symlinks _can_ exist.
1482 if (ip->i_df.if_bytes > 0) {
1483 xfs_idata_realloc(ip,
1484 -(ip->i_df.if_bytes),
1485 XFS_DATA_FORK);
1486 ASSERT(ip->i_df.if_bytes == 0);
1488 return (0);
1494 STATIC int
1495 xfs_inactive_attrs(
1496 xfs_inode_t *ip,
1497 xfs_trans_t **tpp)
1499 xfs_trans_t *tp;
1500 int error;
1501 xfs_mount_t *mp;
1503 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1504 tp = *tpp;
1505 mp = ip->i_mount;
1506 ASSERT(ip->i_d.di_forkoff != 0);
1507 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1508 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1510 error = xfs_attr_inactive(ip);
1511 if (error) {
1512 *tpp = NULL;
1513 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1514 return (error); /* goto out*/
1517 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1518 error = xfs_trans_reserve(tp, 0,
1519 XFS_IFREE_LOG_RES(mp),
1520 0, XFS_TRANS_PERM_LOG_RES,
1521 XFS_INACTIVE_LOG_COUNT);
1522 if (error) {
1523 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1524 xfs_trans_cancel(tp, 0);
1525 *tpp = NULL;
1526 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1527 return (error);
1530 xfs_ilock(ip, XFS_ILOCK_EXCL);
1531 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1532 xfs_trans_ihold(tp, ip);
1533 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1535 ASSERT(ip->i_d.di_anextents == 0);
1537 *tpp = tp;
1538 return (0);
1541 STATIC int
1542 xfs_release(
1543 bhv_desc_t *bdp)
1545 xfs_inode_t *ip;
1546 vnode_t *vp;
1547 xfs_mount_t *mp;
1548 int error;
1550 vp = BHV_TO_VNODE(bdp);
1551 ip = XFS_BHVTOI(bdp);
1553 if ((vp->v_type != VREG) || (ip->i_d.di_mode == 0)) {
1554 return 0;
1557 /* If this is a read-only mount, don't do this (would generate I/O) */
1558 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1559 return 0;
1561 #ifdef HAVE_REFCACHE
1562 /* If we are in the NFS reference cache then don't do this now */
1563 if (ip->i_refcache)
1564 return 0;
1565 #endif
1567 mp = ip->i_mount;
1569 if (ip->i_d.di_nlink != 0) {
1570 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1571 ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0)) &&
1572 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1573 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)))) {
1574 if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1575 return (error);
1576 /* Update linux inode block count after free above */
1577 LINVFS_GET_IP(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1578 ip->i_d.di_nblocks + ip->i_delayed_blks);
1582 return 0;
1586 * xfs_inactive
1588 * This is called when the vnode reference count for the vnode
1589 * goes to zero. If the file has been unlinked, then it must
1590 * now be truncated. Also, we clear all of the read-ahead state
1591 * kept for the inode here since the file is now closed.
1593 STATIC int
1594 xfs_inactive(
1595 bhv_desc_t *bdp,
1596 cred_t *credp)
1598 xfs_inode_t *ip;
1599 vnode_t *vp;
1600 xfs_bmap_free_t free_list;
1601 xfs_fsblock_t first_block;
1602 int committed;
1603 xfs_trans_t *tp;
1604 xfs_mount_t *mp;
1605 int error;
1606 int truncate;
1608 vp = BHV_TO_VNODE(bdp);
1609 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1611 ip = XFS_BHVTOI(bdp);
1614 * If the inode is already free, then there can be nothing
1615 * to clean up here.
1617 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1618 ASSERT(ip->i_df.if_real_bytes == 0);
1619 ASSERT(ip->i_df.if_broot_bytes == 0);
1620 return VN_INACTIVE_CACHE;
1624 * Only do a truncate if it's a regular file with
1625 * some actual space in it. It's OK to look at the
1626 * inode's fields without the lock because we're the
1627 * only one with a reference to the inode.
1629 truncate = ((ip->i_d.di_nlink == 0) &&
1630 ((ip->i_d.di_size != 0) || (ip->i_d.di_nextents > 0)) &&
1631 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1633 mp = ip->i_mount;
1635 if (ip->i_d.di_nlink == 0 &&
1636 DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
1637 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1640 error = 0;
1642 /* If this is a read-only mount, don't do this (would generate I/O) */
1643 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1644 goto out;
1646 if (ip->i_d.di_nlink != 0) {
1647 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1648 ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0)) &&
1649 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1650 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)) ||
1651 (ip->i_delayed_blks != 0))) {
1652 if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1653 return (VN_INACTIVE_CACHE);
1654 /* Update linux inode block count after free above */
1655 LINVFS_GET_IP(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1656 ip->i_d.di_nblocks + ip->i_delayed_blks);
1658 goto out;
1661 ASSERT(ip->i_d.di_nlink == 0);
1663 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1664 return (VN_INACTIVE_CACHE);
1666 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1667 if (truncate) {
1669 * Do the xfs_itruncate_start() call before
1670 * reserving any log space because itruncate_start
1671 * will call into the buffer cache and we can't
1672 * do that within a transaction.
1674 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1676 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1678 error = xfs_trans_reserve(tp, 0,
1679 XFS_ITRUNCATE_LOG_RES(mp),
1680 0, XFS_TRANS_PERM_LOG_RES,
1681 XFS_ITRUNCATE_LOG_COUNT);
1682 if (error) {
1683 /* Don't call itruncate_cleanup */
1684 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1685 xfs_trans_cancel(tp, 0);
1686 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1687 return (VN_INACTIVE_CACHE);
1690 xfs_ilock(ip, XFS_ILOCK_EXCL);
1691 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1692 xfs_trans_ihold(tp, ip);
1695 * normally, we have to run xfs_itruncate_finish sync.
1696 * But if filesystem is wsync and we're in the inactive
1697 * path, then we know that nlink == 0, and that the
1698 * xaction that made nlink == 0 is permanently committed
1699 * since xfs_remove runs as a synchronous transaction.
1701 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1702 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1704 if (error) {
1705 xfs_trans_cancel(tp,
1706 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1707 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1708 return (VN_INACTIVE_CACHE);
1710 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1713 * If we get an error while cleaning up a
1714 * symlink we bail out.
1716 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1717 xfs_inactive_symlink_rmt(ip, &tp) :
1718 xfs_inactive_symlink_local(ip, &tp);
1720 if (error) {
1721 ASSERT(tp == NULL);
1722 return (VN_INACTIVE_CACHE);
1725 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1726 xfs_trans_ihold(tp, ip);
1727 } else {
1728 error = xfs_trans_reserve(tp, 0,
1729 XFS_IFREE_LOG_RES(mp),
1730 0, XFS_TRANS_PERM_LOG_RES,
1731 XFS_INACTIVE_LOG_COUNT);
1732 if (error) {
1733 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1734 xfs_trans_cancel(tp, 0);
1735 return (VN_INACTIVE_CACHE);
1738 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1739 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1740 xfs_trans_ihold(tp, ip);
1744 * If there are attributes associated with the file
1745 * then blow them away now. The code calls a routine
1746 * that recursively deconstructs the attribute fork.
1747 * We need to just commit the current transaction
1748 * because we can't use it for xfs_attr_inactive().
1750 if (ip->i_d.di_anextents > 0) {
1751 error = xfs_inactive_attrs(ip, &tp);
1753 * If we got an error, the transaction is already
1754 * cancelled, and the inode is unlocked. Just get out.
1756 if (error)
1757 return (VN_INACTIVE_CACHE);
1758 } else if (ip->i_afp) {
1759 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1763 * Free the inode.
1765 XFS_BMAP_INIT(&free_list, &first_block);
1766 error = xfs_ifree(tp, ip, &free_list);
1767 if (error) {
1769 * If we fail to free the inode, shut down. The cancel
1770 * might do that, we need to make sure. Otherwise the
1771 * inode might be lost for a long time or forever.
1773 if (!XFS_FORCED_SHUTDOWN(mp)) {
1774 cmn_err(CE_NOTE,
1775 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1776 error, mp->m_fsname);
1777 xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
1779 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1780 } else {
1782 * Credit the quota account(s). The inode is gone.
1784 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1787 * Just ignore errors at this point. There is
1788 * nothing we can do except to try to keep going.
1790 (void) xfs_bmap_finish(&tp, &free_list, first_block,
1791 &committed);
1792 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1795 * Release the dquots held by inode, if any.
1797 XFS_QM_DQDETACH(mp, ip);
1799 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1801 out:
1802 return VN_INACTIVE_CACHE;
1807 * xfs_lookup
1809 STATIC int
1810 xfs_lookup(
1811 bhv_desc_t *dir_bdp,
1812 vname_t *dentry,
1813 vnode_t **vpp,
1814 int flags,
1815 vnode_t *rdir,
1816 cred_t *credp)
1818 xfs_inode_t *dp, *ip;
1819 xfs_ino_t e_inum;
1820 int error;
1821 uint lock_mode;
1822 vnode_t *dir_vp;
1824 dir_vp = BHV_TO_VNODE(dir_bdp);
1825 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1827 dp = XFS_BHVTOI(dir_bdp);
1829 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1830 return XFS_ERROR(EIO);
1832 lock_mode = xfs_ilock_map_shared(dp);
1833 error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1834 if (!error) {
1835 *vpp = XFS_ITOV(ip);
1836 ITRACE(ip);
1838 xfs_iunlock_map_shared(dp, lock_mode);
1839 return error;
1844 * xfs_create (create a new file).
1846 STATIC int
1847 xfs_create(
1848 bhv_desc_t *dir_bdp,
1849 vname_t *dentry,
1850 vattr_t *vap,
1851 vnode_t **vpp,
1852 cred_t *credp)
1854 char *name = VNAME(dentry);
1855 vnode_t *dir_vp;
1856 xfs_inode_t *dp, *ip;
1857 vnode_t *vp=NULL;
1858 xfs_trans_t *tp;
1859 xfs_mount_t *mp;
1860 xfs_dev_t rdev;
1861 int error;
1862 xfs_bmap_free_t free_list;
1863 xfs_fsblock_t first_block;
1864 boolean_t dp_joined_to_trans;
1865 int dm_event_sent = 0;
1866 uint cancel_flags;
1867 int committed;
1868 xfs_prid_t prid;
1869 struct xfs_dquot *udqp, *gdqp;
1870 uint resblks;
1871 int dm_di_mode;
1872 int namelen;
1874 ASSERT(!*vpp);
1875 dir_vp = BHV_TO_VNODE(dir_bdp);
1876 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1878 dp = XFS_BHVTOI(dir_bdp);
1879 mp = dp->i_mount;
1881 dm_di_mode = vap->va_mode|VTTOIF(vap->va_type);
1882 namelen = VNAMELEN(dentry);
1884 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
1885 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1886 dir_vp, DM_RIGHT_NULL, NULL,
1887 DM_RIGHT_NULL, name, NULL,
1888 dm_di_mode, 0, 0);
1890 if (error)
1891 return error;
1892 dm_event_sent = 1;
1895 if (XFS_FORCED_SHUTDOWN(mp))
1896 return XFS_ERROR(EIO);
1898 /* Return through std_return after this point. */
1900 udqp = gdqp = NULL;
1901 if (vap->va_mask & XFS_AT_PROJID)
1902 prid = (xfs_prid_t)vap->va_projid;
1903 else
1904 prid = (xfs_prid_t)dfltprid;
1907 * Make sure that we have allocated dquot(s) on disk.
1909 error = XFS_QM_DQVOPALLOC(mp, dp,
1910 current_fsuid(credp), current_fsgid(credp),
1911 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1912 if (error)
1913 goto std_return;
1915 ip = NULL;
1916 dp_joined_to_trans = B_FALSE;
1918 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1919 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1920 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1922 * Initially assume that the file does not exist and
1923 * reserve the resources for that case. If that is not
1924 * the case we'll drop the one we have and get a more
1925 * appropriate transaction later.
1927 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1928 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1929 if (error == ENOSPC) {
1930 resblks = 0;
1931 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1932 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1934 if (error) {
1935 cancel_flags = 0;
1936 dp = NULL;
1937 goto error_return;
1940 xfs_ilock(dp, XFS_ILOCK_EXCL);
1942 XFS_BMAP_INIT(&free_list, &first_block);
1944 ASSERT(ip == NULL);
1947 * Reserve disk quota and the inode.
1949 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1950 if (error)
1951 goto error_return;
1953 if (resblks == 0 &&
1954 (error = XFS_DIR_CANENTER(mp, tp, dp, name, namelen)))
1955 goto error_return;
1956 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1957 error = xfs_dir_ialloc(&tp, dp,
1958 MAKEIMODE(vap->va_type,vap->va_mode), 1,
1959 rdev, credp, prid, resblks > 0,
1960 &ip, &committed);
1961 if (error) {
1962 if (error == ENOSPC)
1963 goto error_return;
1964 goto abort_return;
1966 ITRACE(ip);
1969 * At this point, we've gotten a newly allocated inode.
1970 * It is locked (and joined to the transaction).
1973 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1976 * Now we join the directory inode to the transaction.
1977 * We do not do it earlier because xfs_dir_ialloc
1978 * might commit the previous transaction (and release
1979 * all the locks).
1982 VN_HOLD(dir_vp);
1983 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1984 dp_joined_to_trans = B_TRUE;
1986 error = XFS_DIR_CREATENAME(mp, tp, dp, name, namelen, ip->i_ino,
1987 &first_block, &free_list,
1988 resblks ? resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1989 if (error) {
1990 ASSERT(error != ENOSPC);
1991 goto abort_return;
1993 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1994 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1997 * If this is a synchronous mount, make sure that the
1998 * create transaction goes to disk before returning to
1999 * the user.
2001 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2002 xfs_trans_set_sync(tp);
2005 dp->i_gen++;
2008 * Attach the dquot(s) to the inodes and modify them incore.
2009 * These ids of the inode couldn't have changed since the new
2010 * inode has been locked ever since it was created.
2012 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2015 * xfs_trans_commit normally decrements the vnode ref count
2016 * when it unlocks the inode. Since we want to return the
2017 * vnode to the caller, we bump the vnode ref count now.
2019 IHOLD(ip);
2020 vp = XFS_ITOV(ip);
2022 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2023 if (error) {
2024 xfs_bmap_cancel(&free_list);
2025 goto abort_rele;
2028 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2029 if (error) {
2030 IRELE(ip);
2031 tp = NULL;
2032 goto error_return;
2035 XFS_QM_DQRELE(mp, udqp);
2036 XFS_QM_DQRELE(mp, gdqp);
2039 * Propogate the fact that the vnode changed after the
2040 * xfs_inode locks have been released.
2042 VOP_VNODE_CHANGE(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2044 *vpp = vp;
2046 /* Fallthrough to std_return with error = 0 */
2048 std_return:
2049 if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
2050 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2051 DM_EVENT_POSTCREATE)) {
2052 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2053 dir_vp, DM_RIGHT_NULL,
2054 *vpp ? vp:NULL,
2055 DM_RIGHT_NULL, name, NULL,
2056 dm_di_mode, error, 0);
2058 return error;
2060 abort_return:
2061 cancel_flags |= XFS_TRANS_ABORT;
2062 /* FALLTHROUGH */
2063 error_return:
2065 if (tp != NULL)
2066 xfs_trans_cancel(tp, cancel_flags);
2068 if (!dp_joined_to_trans && (dp != NULL))
2069 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2070 XFS_QM_DQRELE(mp, udqp);
2071 XFS_QM_DQRELE(mp, gdqp);
2073 goto std_return;
2075 abort_rele:
2077 * Wait until after the current transaction is aborted to
2078 * release the inode. This prevents recursive transactions
2079 * and deadlocks from xfs_inactive.
2081 cancel_flags |= XFS_TRANS_ABORT;
2082 xfs_trans_cancel(tp, cancel_flags);
2083 IRELE(ip);
2085 XFS_QM_DQRELE(mp, udqp);
2086 XFS_QM_DQRELE(mp, gdqp);
2088 goto std_return;
2091 #ifdef DEBUG
2093 * Some counters to see if (and how often) we are hitting some deadlock
2094 * prevention code paths.
2097 int xfs_rm_locks;
2098 int xfs_rm_lock_delays;
2099 int xfs_rm_attempts;
2100 #endif
2103 * The following routine will lock the inodes associated with the
2104 * directory and the named entry in the directory. The locks are
2105 * acquired in increasing inode number.
2107 * If the entry is "..", then only the directory is locked. The
2108 * vnode ref count will still include that from the .. entry in
2109 * this case.
2111 * There is a deadlock we need to worry about. If the locked directory is
2112 * in the AIL, it might be blocking up the log. The next inode we lock
2113 * could be already locked by another thread waiting for log space (e.g
2114 * a permanent log reservation with a long running transaction (see
2115 * xfs_itruncate_finish)). To solve this, we must check if the directory
2116 * is in the ail and use lock_nowait. If we can't lock, we need to
2117 * drop the inode lock on the directory and try again. xfs_iunlock will
2118 * potentially push the tail if we were holding up the log.
2120 STATIC int
2121 xfs_lock_dir_and_entry(
2122 xfs_inode_t *dp,
2123 vname_t *dentry,
2124 xfs_inode_t *ip) /* inode of entry 'name' */
2126 int attempts;
2127 xfs_ino_t e_inum;
2128 xfs_inode_t *ips[2];
2129 xfs_log_item_t *lp;
2131 #ifdef DEBUG
2132 xfs_rm_locks++;
2133 #endif
2134 attempts = 0;
2136 again:
2137 xfs_ilock(dp, XFS_ILOCK_EXCL);
2139 e_inum = ip->i_ino;
2141 ITRACE(ip);
2144 * We want to lock in increasing inum. Since we've already
2145 * acquired the lock on the directory, we may need to release
2146 * if if the inum of the entry turns out to be less.
2148 if (e_inum > dp->i_ino) {
2150 * We are already in the right order, so just
2151 * lock on the inode of the entry.
2152 * We need to use nowait if dp is in the AIL.
2155 lp = (xfs_log_item_t *)dp->i_itemp;
2156 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2157 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2158 attempts++;
2159 #ifdef DEBUG
2160 xfs_rm_attempts++;
2161 #endif
2164 * Unlock dp and try again.
2165 * xfs_iunlock will try to push the tail
2166 * if the inode is in the AIL.
2169 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2171 if ((attempts % 5) == 0) {
2172 delay(1); /* Don't just spin the CPU */
2173 #ifdef DEBUG
2174 xfs_rm_lock_delays++;
2175 #endif
2177 goto again;
2179 } else {
2180 xfs_ilock(ip, XFS_ILOCK_EXCL);
2182 } else if (e_inum < dp->i_ino) {
2183 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2185 ips[0] = ip;
2186 ips[1] = dp;
2187 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2189 /* else e_inum == dp->i_ino */
2190 /* This can happen if we're asked to lock /x/..
2191 * the entry is "..", which is also the parent directory.
2194 return 0;
2197 #ifdef DEBUG
2198 int xfs_locked_n;
2199 int xfs_small_retries;
2200 int xfs_middle_retries;
2201 int xfs_lots_retries;
2202 int xfs_lock_delays;
2203 #endif
2206 * The following routine will lock n inodes in exclusive mode.
2207 * We assume the caller calls us with the inodes in i_ino order.
2209 * We need to detect deadlock where an inode that we lock
2210 * is in the AIL and we start waiting for another inode that is locked
2211 * by a thread in a long running transaction (such as truncate). This can
2212 * result in deadlock since the long running trans might need to wait
2213 * for the inode we just locked in order to push the tail and free space
2214 * in the log.
2216 void
2217 xfs_lock_inodes(
2218 xfs_inode_t **ips,
2219 int inodes,
2220 int first_locked,
2221 uint lock_mode)
2223 int attempts = 0, i, j, try_lock;
2224 xfs_log_item_t *lp;
2226 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2228 if (first_locked) {
2229 try_lock = 1;
2230 i = 1;
2231 } else {
2232 try_lock = 0;
2233 i = 0;
2236 again:
2237 for (; i < inodes; i++) {
2238 ASSERT(ips[i]);
2240 if (i && (ips[i] == ips[i-1])) /* Already locked */
2241 continue;
2244 * If try_lock is not set yet, make sure all locked inodes
2245 * are not in the AIL.
2246 * If any are, set try_lock to be used later.
2249 if (!try_lock) {
2250 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2251 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2252 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2253 try_lock++;
2259 * If any of the previous locks we have locked is in the AIL,
2260 * we must TRY to get the second and subsequent locks. If
2261 * we can't get any, we must release all we have
2262 * and try again.
2265 if (try_lock) {
2266 /* try_lock must be 0 if i is 0. */
2268 * try_lock means we have an inode locked
2269 * that is in the AIL.
2271 ASSERT(i != 0);
2272 if (!xfs_ilock_nowait(ips[i], lock_mode)) {
2273 attempts++;
2276 * Unlock all previous guys and try again.
2277 * xfs_iunlock will try to push the tail
2278 * if the inode is in the AIL.
2281 for(j = i - 1; j >= 0; j--) {
2284 * Check to see if we've already
2285 * unlocked this one.
2286 * Not the first one going back,
2287 * and the inode ptr is the same.
2289 if ((j != (i - 1)) && ips[j] ==
2290 ips[j+1])
2291 continue;
2293 xfs_iunlock(ips[j], lock_mode);
2296 if ((attempts % 5) == 0) {
2297 delay(1); /* Don't just spin the CPU */
2298 #ifdef DEBUG
2299 xfs_lock_delays++;
2300 #endif
2302 i = 0;
2303 try_lock = 0;
2304 goto again;
2306 } else {
2307 xfs_ilock(ips[i], lock_mode);
2311 #ifdef DEBUG
2312 if (attempts) {
2313 if (attempts < 5) xfs_small_retries++;
2314 else if (attempts < 100) xfs_middle_retries++;
2315 else xfs_lots_retries++;
2316 } else {
2317 xfs_locked_n++;
2319 #endif
2322 #ifdef DEBUG
2323 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2324 int remove_which_error_return = 0;
2325 #else /* ! DEBUG */
2326 #define REMOVE_DEBUG_TRACE(x)
2327 #endif /* ! DEBUG */
2331 * xfs_remove
2334 STATIC int
2335 xfs_remove(
2336 bhv_desc_t *dir_bdp,
2337 vname_t *dentry,
2338 cred_t *credp)
2340 vnode_t *dir_vp;
2341 char *name = VNAME(dentry);
2342 xfs_inode_t *dp, *ip;
2343 xfs_trans_t *tp = NULL;
2344 xfs_mount_t *mp;
2345 int error = 0;
2346 xfs_bmap_free_t free_list;
2347 xfs_fsblock_t first_block;
2348 int cancel_flags;
2349 int committed;
2350 int dm_di_mode = 0;
2351 int link_zero;
2352 uint resblks;
2353 int namelen;
2355 dir_vp = BHV_TO_VNODE(dir_bdp);
2356 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2358 dp = XFS_BHVTOI(dir_bdp);
2359 mp = dp->i_mount;
2361 if (XFS_FORCED_SHUTDOWN(mp))
2362 return XFS_ERROR(EIO);
2364 namelen = VNAMELEN(dentry);
2366 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
2367 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2368 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2369 name, NULL, 0, 0, 0);
2370 if (error)
2371 return error;
2374 /* From this point on, return through std_return */
2375 ip = NULL;
2378 * We need to get a reference to ip before we get our log
2379 * reservation. The reason for this is that we cannot call
2380 * xfs_iget for an inode for which we do not have a reference
2381 * once we've acquired a log reservation. This is because the
2382 * inode we are trying to get might be in xfs_inactive going
2383 * for a log reservation. Since we'll have to wait for the
2384 * inactive code to complete before returning from xfs_iget,
2385 * we need to make sure that we don't have log space reserved
2386 * when we call xfs_iget. Instead we get an unlocked referece
2387 * to the inode before getting our log reservation.
2389 error = xfs_get_dir_entry(dentry, &ip);
2390 if (error) {
2391 REMOVE_DEBUG_TRACE(__LINE__);
2392 goto std_return;
2395 dm_di_mode = ip->i_d.di_mode;
2397 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2399 ITRACE(ip);
2401 error = XFS_QM_DQATTACH(mp, dp, 0);
2402 if (!error && dp != ip)
2403 error = XFS_QM_DQATTACH(mp, ip, 0);
2404 if (error) {
2405 REMOVE_DEBUG_TRACE(__LINE__);
2406 IRELE(ip);
2407 goto std_return;
2410 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2411 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2413 * We try to get the real space reservation first,
2414 * allowing for directory btree deletion(s) implying
2415 * possible bmap insert(s). If we can't get the space
2416 * reservation then we use 0 instead, and avoid the bmap
2417 * btree insert(s) in the directory code by, if the bmap
2418 * insert tries to happen, instead trimming the LAST
2419 * block from the directory.
2421 resblks = XFS_REMOVE_SPACE_RES(mp);
2422 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2423 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2424 if (error == ENOSPC) {
2425 resblks = 0;
2426 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2427 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2429 if (error) {
2430 ASSERT(error != ENOSPC);
2431 REMOVE_DEBUG_TRACE(__LINE__);
2432 xfs_trans_cancel(tp, 0);
2433 IRELE(ip);
2434 return error;
2437 error = xfs_lock_dir_and_entry(dp, dentry, ip);
2438 if (error) {
2439 REMOVE_DEBUG_TRACE(__LINE__);
2440 xfs_trans_cancel(tp, cancel_flags);
2441 IRELE(ip);
2442 goto std_return;
2446 * At this point, we've gotten both the directory and the entry
2447 * inodes locked.
2449 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2450 if (dp != ip) {
2452 * Increment vnode ref count only in this case since
2453 * there's an extra vnode reference in the case where
2454 * dp == ip.
2456 IHOLD(dp);
2457 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2461 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2463 XFS_BMAP_INIT(&free_list, &first_block);
2464 error = XFS_DIR_REMOVENAME(mp, tp, dp, name, namelen, ip->i_ino,
2465 &first_block, &free_list, 0);
2466 if (error) {
2467 ASSERT(error != ENOENT);
2468 REMOVE_DEBUG_TRACE(__LINE__);
2469 goto error1;
2471 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2473 dp->i_gen++;
2474 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2476 error = xfs_droplink(tp, ip);
2477 if (error) {
2478 REMOVE_DEBUG_TRACE(__LINE__);
2479 goto error1;
2482 /* Determine if this is the last link while
2483 * we are in the transaction.
2485 link_zero = (ip)->i_d.di_nlink==0;
2488 * Take an extra ref on the inode so that it doesn't
2489 * go to xfs_inactive() from within the commit.
2491 IHOLD(ip);
2494 * If this is a synchronous mount, make sure that the
2495 * remove transaction goes to disk before returning to
2496 * the user.
2498 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2499 xfs_trans_set_sync(tp);
2502 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2503 if (error) {
2504 REMOVE_DEBUG_TRACE(__LINE__);
2505 goto error_rele;
2508 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2509 if (error) {
2510 IRELE(ip);
2511 goto std_return;
2515 * Before we drop our extra reference to the inode, purge it
2516 * from the refcache if it is there. By waiting until afterwards
2517 * to do the IRELE, we ensure that we won't go inactive in the
2518 * xfs_refcache_purge_ip routine (although that would be OK).
2520 xfs_refcache_purge_ip(ip);
2522 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2525 * Let interposed file systems know about removed links.
2527 VOP_LINK_REMOVED(XFS_ITOV(ip), dir_vp, link_zero);
2529 IRELE(ip);
2531 /* Fall through to std_return with error = 0 */
2532 std_return:
2533 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
2534 DM_EVENT_POSTREMOVE)) {
2535 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2536 dir_vp, DM_RIGHT_NULL,
2537 NULL, DM_RIGHT_NULL,
2538 name, NULL, dm_di_mode, error, 0);
2540 return error;
2542 error1:
2543 xfs_bmap_cancel(&free_list);
2544 cancel_flags |= XFS_TRANS_ABORT;
2545 xfs_trans_cancel(tp, cancel_flags);
2546 goto std_return;
2548 error_rele:
2550 * In this case make sure to not release the inode until after
2551 * the current transaction is aborted. Releasing it beforehand
2552 * can cause us to go to xfs_inactive and start a recursive
2553 * transaction which can easily deadlock with the current one.
2555 xfs_bmap_cancel(&free_list);
2556 cancel_flags |= XFS_TRANS_ABORT;
2557 xfs_trans_cancel(tp, cancel_flags);
2560 * Before we drop our extra reference to the inode, purge it
2561 * from the refcache if it is there. By waiting until afterwards
2562 * to do the IRELE, we ensure that we won't go inactive in the
2563 * xfs_refcache_purge_ip routine (although that would be OK).
2565 xfs_refcache_purge_ip(ip);
2567 IRELE(ip);
2569 goto std_return;
2574 * xfs_link
2577 STATIC int
2578 xfs_link(
2579 bhv_desc_t *target_dir_bdp,
2580 vnode_t *src_vp,
2581 vname_t *dentry,
2582 cred_t *credp)
2584 xfs_inode_t *tdp, *sip;
2585 xfs_trans_t *tp;
2586 xfs_mount_t *mp;
2587 xfs_inode_t *ips[2];
2588 int error;
2589 xfs_bmap_free_t free_list;
2590 xfs_fsblock_t first_block;
2591 int cancel_flags;
2592 int committed;
2593 vnode_t *target_dir_vp;
2594 bhv_desc_t *src_bdp;
2595 int resblks;
2596 char *target_name = VNAME(dentry);
2597 int target_namelen;
2599 target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2600 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2601 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2603 target_namelen = VNAMELEN(dentry);
2604 if (src_vp->v_type == VDIR)
2605 return XFS_ERROR(EPERM);
2608 * For now, manually find the XFS behavior descriptor for
2609 * the source vnode. If it doesn't exist then something
2610 * is wrong and we should just return an error.
2611 * Eventually we need to figure out how link is going to
2612 * work in the face of stacked vnodes.
2614 src_bdp = vn_bhv_lookup_unlocked(VN_BHV_HEAD(src_vp), &xfs_vnodeops);
2615 if (src_bdp == NULL) {
2616 return XFS_ERROR(EXDEV);
2618 sip = XFS_BHVTOI(src_bdp);
2619 tdp = XFS_BHVTOI(target_dir_bdp);
2620 mp = tdp->i_mount;
2621 if (XFS_FORCED_SHUTDOWN(mp))
2622 return XFS_ERROR(EIO);
2624 if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
2625 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2626 target_dir_vp, DM_RIGHT_NULL,
2627 src_vp, DM_RIGHT_NULL,
2628 target_name, NULL, 0, 0, 0);
2629 if (error)
2630 return error;
2633 /* Return through std_return after this point. */
2635 error = XFS_QM_DQATTACH(mp, sip, 0);
2636 if (!error && sip != tdp)
2637 error = XFS_QM_DQATTACH(mp, tdp, 0);
2638 if (error)
2639 goto std_return;
2641 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2642 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2643 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2644 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2645 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2646 if (error == ENOSPC) {
2647 resblks = 0;
2648 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2649 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2651 if (error) {
2652 cancel_flags = 0;
2653 goto error_return;
2656 if (sip->i_ino < tdp->i_ino) {
2657 ips[0] = sip;
2658 ips[1] = tdp;
2659 } else {
2660 ips[0] = tdp;
2661 ips[1] = sip;
2664 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2667 * Increment vnode ref counts since xfs_trans_commit &
2668 * xfs_trans_cancel will both unlock the inodes and
2669 * decrement the associated ref counts.
2671 VN_HOLD(src_vp);
2672 VN_HOLD(target_dir_vp);
2673 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2674 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2677 * If the source has too many links, we can't make any more to it.
2679 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2680 error = XFS_ERROR(EMLINK);
2681 goto error_return;
2684 if (resblks == 0 &&
2685 (error = XFS_DIR_CANENTER(mp, tp, tdp, target_name,
2686 target_namelen)))
2687 goto error_return;
2689 XFS_BMAP_INIT(&free_list, &first_block);
2691 error = XFS_DIR_CREATENAME(mp, tp, tdp, target_name, target_namelen,
2692 sip->i_ino, &first_block, &free_list,
2693 resblks);
2694 if (error)
2695 goto abort_return;
2696 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2697 tdp->i_gen++;
2698 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2700 error = xfs_bumplink(tp, sip);
2701 if (error) {
2702 goto abort_return;
2706 * If this is a synchronous mount, make sure that the
2707 * link transaction goes to disk before returning to
2708 * the user.
2710 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2711 xfs_trans_set_sync(tp);
2714 error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
2715 if (error) {
2716 xfs_bmap_cancel(&free_list);
2717 goto abort_return;
2720 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2721 if (error) {
2722 goto std_return;
2725 /* Fall through to std_return with error = 0. */
2726 std_return:
2727 if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
2728 DM_EVENT_POSTLINK)) {
2729 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2730 target_dir_vp, DM_RIGHT_NULL,
2731 src_vp, DM_RIGHT_NULL,
2732 target_name, NULL, 0, error, 0);
2734 return error;
2736 abort_return:
2737 cancel_flags |= XFS_TRANS_ABORT;
2738 /* FALLTHROUGH */
2739 error_return:
2740 xfs_trans_cancel(tp, cancel_flags);
2742 goto std_return;
2745 * xfs_mkdir
2748 STATIC int
2749 xfs_mkdir(
2750 bhv_desc_t *dir_bdp,
2751 vname_t *dentry,
2752 vattr_t *vap,
2753 vnode_t **vpp,
2754 cred_t *credp)
2756 char *dir_name = VNAME(dentry);
2757 xfs_inode_t *dp;
2758 xfs_inode_t *cdp; /* inode of created dir */
2759 vnode_t *cvp; /* vnode of created dir */
2760 xfs_trans_t *tp;
2761 xfs_mount_t *mp;
2762 int cancel_flags;
2763 int error;
2764 int committed;
2765 xfs_bmap_free_t free_list;
2766 xfs_fsblock_t first_block;
2767 vnode_t *dir_vp;
2768 boolean_t dp_joined_to_trans;
2769 boolean_t created = B_FALSE;
2770 int dm_event_sent = 0;
2771 xfs_prid_t prid;
2772 struct xfs_dquot *udqp, *gdqp;
2773 uint resblks;
2774 int dm_di_mode;
2775 int dir_namelen;
2777 dir_vp = BHV_TO_VNODE(dir_bdp);
2778 dp = XFS_BHVTOI(dir_bdp);
2779 mp = dp->i_mount;
2781 if (XFS_FORCED_SHUTDOWN(mp))
2782 return XFS_ERROR(EIO);
2784 dir_namelen = VNAMELEN(dentry);
2786 tp = NULL;
2787 dp_joined_to_trans = B_FALSE;
2788 dm_di_mode = vap->va_mode|VTTOIF(vap->va_type);
2790 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
2791 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2792 dir_vp, DM_RIGHT_NULL, NULL,
2793 DM_RIGHT_NULL, dir_name, NULL,
2794 dm_di_mode, 0, 0);
2795 if (error)
2796 return error;
2797 dm_event_sent = 1;
2800 /* Return through std_return after this point. */
2802 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2804 mp = dp->i_mount;
2805 udqp = gdqp = NULL;
2806 if (vap->va_mask & XFS_AT_PROJID)
2807 prid = (xfs_prid_t)vap->va_projid;
2808 else
2809 prid = (xfs_prid_t)dfltprid;
2812 * Make sure that we have allocated dquot(s) on disk.
2814 error = XFS_QM_DQVOPALLOC(mp, dp,
2815 current_fsuid(credp), current_fsgid(credp),
2816 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2817 if (error)
2818 goto std_return;
2820 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2821 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2822 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2823 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2824 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2825 if (error == ENOSPC) {
2826 resblks = 0;
2827 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2828 XFS_TRANS_PERM_LOG_RES,
2829 XFS_MKDIR_LOG_COUNT);
2831 if (error) {
2832 cancel_flags = 0;
2833 dp = NULL;
2834 goto error_return;
2837 xfs_ilock(dp, XFS_ILOCK_EXCL);
2840 * Check for directory link count overflow.
2842 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2843 error = XFS_ERROR(EMLINK);
2844 goto error_return;
2848 * Reserve disk quota and the inode.
2850 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2851 if (error)
2852 goto error_return;
2854 if (resblks == 0 &&
2855 (error = XFS_DIR_CANENTER(mp, tp, dp, dir_name, dir_namelen)))
2856 goto error_return;
2858 * create the directory inode.
2860 error = xfs_dir_ialloc(&tp, dp,
2861 MAKEIMODE(vap->va_type,vap->va_mode), 2,
2862 0, credp, prid, resblks > 0,
2863 &cdp, NULL);
2864 if (error) {
2865 if (error == ENOSPC)
2866 goto error_return;
2867 goto abort_return;
2869 ITRACE(cdp);
2872 * Now we add the directory inode to the transaction.
2873 * We waited until now since xfs_dir_ialloc might start
2874 * a new transaction. Had we joined the transaction
2875 * earlier, the locks might have gotten released.
2877 VN_HOLD(dir_vp);
2878 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2879 dp_joined_to_trans = B_TRUE;
2881 XFS_BMAP_INIT(&free_list, &first_block);
2883 error = XFS_DIR_CREATENAME(mp, tp, dp, dir_name, dir_namelen,
2884 cdp->i_ino, &first_block, &free_list,
2885 resblks ? resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2886 if (error) {
2887 ASSERT(error != ENOSPC);
2888 goto error1;
2890 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2893 * Bump the in memory version number of the parent directory
2894 * so that other processes accessing it will recognize that
2895 * the directory has changed.
2897 dp->i_gen++;
2899 error = XFS_DIR_INIT(mp, tp, cdp, dp);
2900 if (error) {
2901 goto error2;
2904 cdp->i_gen = 1;
2905 error = xfs_bumplink(tp, dp);
2906 if (error) {
2907 goto error2;
2910 cvp = XFS_ITOV(cdp);
2912 created = B_TRUE;
2914 *vpp = cvp;
2915 IHOLD(cdp);
2918 * Attach the dquots to the new inode and modify the icount incore.
2920 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2923 * If this is a synchronous mount, make sure that the
2924 * mkdir transaction goes to disk before returning to
2925 * the user.
2927 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2928 xfs_trans_set_sync(tp);
2931 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2932 if (error) {
2933 IRELE(cdp);
2934 goto error2;
2937 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2938 XFS_QM_DQRELE(mp, udqp);
2939 XFS_QM_DQRELE(mp, gdqp);
2940 if (error) {
2941 IRELE(cdp);
2944 /* Fall through to std_return with error = 0 or errno from
2945 * xfs_trans_commit. */
2947 std_return:
2948 if ( (created || (error != 0 && dm_event_sent != 0)) &&
2949 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2950 DM_EVENT_POSTCREATE)) {
2951 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2952 dir_vp, DM_RIGHT_NULL,
2953 created ? XFS_ITOV(cdp):NULL,
2954 DM_RIGHT_NULL,
2955 dir_name, NULL,
2956 dm_di_mode, error, 0);
2958 return error;
2960 error2:
2961 error1:
2962 xfs_bmap_cancel(&free_list);
2963 abort_return:
2964 cancel_flags |= XFS_TRANS_ABORT;
2965 error_return:
2966 xfs_trans_cancel(tp, cancel_flags);
2967 XFS_QM_DQRELE(mp, udqp);
2968 XFS_QM_DQRELE(mp, gdqp);
2970 if (!dp_joined_to_trans && (dp != NULL)) {
2971 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2974 goto std_return;
2979 * xfs_rmdir
2982 STATIC int
2983 xfs_rmdir(
2984 bhv_desc_t *dir_bdp,
2985 vname_t *dentry,
2986 cred_t *credp)
2988 char *name = VNAME(dentry);
2989 xfs_inode_t *dp;
2990 xfs_inode_t *cdp; /* child directory */
2991 xfs_trans_t *tp;
2992 xfs_mount_t *mp;
2993 int error;
2994 xfs_bmap_free_t free_list;
2995 xfs_fsblock_t first_block;
2996 int cancel_flags;
2997 int committed;
2998 vnode_t *dir_vp;
2999 int dm_di_mode = 0;
3000 int last_cdp_link;
3001 int namelen;
3002 uint resblks;
3004 dir_vp = BHV_TO_VNODE(dir_bdp);
3005 dp = XFS_BHVTOI(dir_bdp);
3006 mp = dp->i_mount;
3008 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3010 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3011 return XFS_ERROR(EIO);
3012 namelen = VNAMELEN(dentry);
3014 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
3015 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3016 dir_vp, DM_RIGHT_NULL,
3017 NULL, DM_RIGHT_NULL,
3018 name, NULL, 0, 0, 0);
3019 if (error)
3020 return XFS_ERROR(error);
3023 /* Return through std_return after this point. */
3025 cdp = NULL;
3028 * We need to get a reference to cdp before we get our log
3029 * reservation. The reason for this is that we cannot call
3030 * xfs_iget for an inode for which we do not have a reference
3031 * once we've acquired a log reservation. This is because the
3032 * inode we are trying to get might be in xfs_inactive going
3033 * for a log reservation. Since we'll have to wait for the
3034 * inactive code to complete before returning from xfs_iget,
3035 * we need to make sure that we don't have log space reserved
3036 * when we call xfs_iget. Instead we get an unlocked referece
3037 * to the inode before getting our log reservation.
3039 error = xfs_get_dir_entry(dentry, &cdp);
3040 if (error) {
3041 REMOVE_DEBUG_TRACE(__LINE__);
3042 goto std_return;
3044 mp = dp->i_mount;
3045 dm_di_mode = cdp->i_d.di_mode;
3048 * Get the dquots for the inodes.
3050 error = XFS_QM_DQATTACH(mp, dp, 0);
3051 if (!error && dp != cdp)
3052 error = XFS_QM_DQATTACH(mp, cdp, 0);
3053 if (error) {
3054 IRELE(cdp);
3055 REMOVE_DEBUG_TRACE(__LINE__);
3056 goto std_return;
3059 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3060 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3062 * We try to get the real space reservation first,
3063 * allowing for directory btree deletion(s) implying
3064 * possible bmap insert(s). If we can't get the space
3065 * reservation then we use 0 instead, and avoid the bmap
3066 * btree insert(s) in the directory code by, if the bmap
3067 * insert tries to happen, instead trimming the LAST
3068 * block from the directory.
3070 resblks = XFS_REMOVE_SPACE_RES(mp);
3071 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3072 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3073 if (error == ENOSPC) {
3074 resblks = 0;
3075 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3076 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3078 if (error) {
3079 ASSERT(error != ENOSPC);
3080 cancel_flags = 0;
3081 IRELE(cdp);
3082 goto error_return;
3084 XFS_BMAP_INIT(&free_list, &first_block);
3087 * Now lock the child directory inode and the parent directory
3088 * inode in the proper order. This will take care of validating
3089 * that the directory entry for the child directory inode has
3090 * not changed while we were obtaining a log reservation.
3092 error = xfs_lock_dir_and_entry(dp, dentry, cdp);
3093 if (error) {
3094 xfs_trans_cancel(tp, cancel_flags);
3095 IRELE(cdp);
3096 goto std_return;
3099 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3100 if (dp != cdp) {
3102 * Only increment the parent directory vnode count if
3103 * we didn't bump it in looking up cdp. The only time
3104 * we don't bump it is when we're looking up ".".
3106 VN_HOLD(dir_vp);
3109 ITRACE(cdp);
3110 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3112 ASSERT(cdp->i_d.di_nlink >= 2);
3113 if (cdp->i_d.di_nlink != 2) {
3114 error = XFS_ERROR(ENOTEMPTY);
3115 goto error_return;
3117 if (!XFS_DIR_ISEMPTY(mp, cdp)) {
3118 error = XFS_ERROR(ENOTEMPTY);
3119 goto error_return;
3122 error = XFS_DIR_REMOVENAME(mp, tp, dp, name, namelen, cdp->i_ino,
3123 &first_block, &free_list, resblks);
3124 if (error) {
3125 goto error1;
3128 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3131 * Bump the in memory generation count on the parent
3132 * directory so that other can know that it has changed.
3134 dp->i_gen++;
3137 * Drop the link from cdp's "..".
3139 error = xfs_droplink(tp, dp);
3140 if (error) {
3141 goto error1;
3145 * Drop the link from dp to cdp.
3147 error = xfs_droplink(tp, cdp);
3148 if (error) {
3149 goto error1;
3153 * Drop the "." link from cdp to self.
3155 error = xfs_droplink(tp, cdp);
3156 if (error) {
3157 goto error1;
3160 /* Determine these before committing transaction */
3161 last_cdp_link = (cdp)->i_d.di_nlink==0;
3164 * Take an extra ref on the child vnode so that it
3165 * does not go to xfs_inactive() from within the commit.
3167 IHOLD(cdp);
3170 * If this is a synchronous mount, make sure that the
3171 * rmdir transaction goes to disk before returning to
3172 * the user.
3174 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3175 xfs_trans_set_sync(tp);
3178 error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
3179 if (error) {
3180 xfs_bmap_cancel(&free_list);
3181 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3182 XFS_TRANS_ABORT));
3183 IRELE(cdp);
3184 goto std_return;
3187 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3188 if (error) {
3189 IRELE(cdp);
3190 goto std_return;
3195 * Let interposed file systems know about removed links.
3197 VOP_LINK_REMOVED(XFS_ITOV(cdp), dir_vp, last_cdp_link);
3199 IRELE(cdp);
3201 /* Fall through to std_return with error = 0 or the errno
3202 * from xfs_trans_commit. */
3203 std_return:
3204 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
3205 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3206 dir_vp, DM_RIGHT_NULL,
3207 NULL, DM_RIGHT_NULL,
3208 name, NULL, dm_di_mode,
3209 error, 0);
3211 return error;
3213 error1:
3214 xfs_bmap_cancel(&free_list);
3215 cancel_flags |= XFS_TRANS_ABORT;
3216 error_return:
3217 xfs_trans_cancel(tp, cancel_flags);
3218 goto std_return;
3223 * xfs_readdir
3225 * Read dp's entries starting at uiop->uio_offset and translate them into
3226 * bufsize bytes worth of struct dirents starting at bufbase.
3228 STATIC int
3229 xfs_readdir(
3230 bhv_desc_t *dir_bdp,
3231 uio_t *uiop,
3232 cred_t *credp,
3233 int *eofp)
3235 xfs_inode_t *dp;
3236 xfs_trans_t *tp = NULL;
3237 int error = 0;
3238 uint lock_mode;
3239 xfs_off_t start_offset;
3241 vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
3242 (inst_t *)__return_address);
3243 dp = XFS_BHVTOI(dir_bdp);
3245 if (XFS_FORCED_SHUTDOWN(dp->i_mount)) {
3246 return XFS_ERROR(EIO);
3249 lock_mode = xfs_ilock_map_shared(dp);
3250 start_offset = uiop->uio_offset;
3251 error = XFS_DIR_GETDENTS(dp->i_mount, tp, dp, uiop, eofp);
3252 if (start_offset != uiop->uio_offset) {
3253 xfs_ichgtime(dp, XFS_ICHGTIME_ACC);
3255 xfs_iunlock_map_shared(dp, lock_mode);
3256 return error;
3261 * xfs_symlink
3264 STATIC int
3265 xfs_symlink(
3266 bhv_desc_t *dir_bdp,
3267 vname_t *dentry,
3268 vattr_t *vap,
3269 char *target_path,
3270 vnode_t **vpp,
3271 cred_t *credp)
3273 xfs_trans_t *tp;
3274 xfs_mount_t *mp;
3275 xfs_inode_t *dp;
3276 xfs_inode_t *ip;
3277 int error;
3278 int pathlen;
3279 xfs_bmap_free_t free_list;
3280 xfs_fsblock_t first_block;
3281 boolean_t dp_joined_to_trans;
3282 vnode_t *dir_vp;
3283 uint cancel_flags;
3284 int committed;
3285 xfs_fileoff_t first_fsb;
3286 xfs_filblks_t fs_blocks;
3287 int nmaps;
3288 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3289 xfs_daddr_t d;
3290 char *cur_chunk;
3291 int byte_cnt;
3292 int n;
3293 xfs_buf_t *bp;
3294 xfs_prid_t prid;
3295 struct xfs_dquot *udqp, *gdqp;
3296 uint resblks;
3297 char *link_name = VNAME(dentry);
3298 int link_namelen;
3300 *vpp = NULL;
3301 dir_vp = BHV_TO_VNODE(dir_bdp);
3302 dp = XFS_BHVTOI(dir_bdp);
3303 dp_joined_to_trans = B_FALSE;
3304 error = 0;
3305 ip = NULL;
3306 tp = NULL;
3308 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3310 mp = dp->i_mount;
3312 if (XFS_FORCED_SHUTDOWN(mp))
3313 return XFS_ERROR(EIO);
3315 link_namelen = VNAMELEN(dentry);
3318 * Check component lengths of the target path name.
3320 pathlen = strlen(target_path);
3321 if (pathlen >= MAXPATHLEN) /* total string too long */
3322 return XFS_ERROR(ENAMETOOLONG);
3323 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3324 int len, total;
3325 char *path;
3327 for(total = 0, path = target_path; total < pathlen;) {
3329 * Skip any slashes.
3331 while(*path == '/') {
3332 total++;
3333 path++;
3337 * Count up to the next slash or end of path.
3338 * Error out if the component is bigger than MAXNAMELEN.
3340 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3341 if (++len >= MAXNAMELEN) {
3342 error = ENAMETOOLONG;
3343 return error;
3349 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
3350 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3351 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3352 link_name, target_path, 0, 0, 0);
3353 if (error)
3354 return error;
3357 /* Return through std_return after this point. */
3359 udqp = gdqp = NULL;
3360 if (vap->va_mask & XFS_AT_PROJID)
3361 prid = (xfs_prid_t)vap->va_projid;
3362 else
3363 prid = (xfs_prid_t)dfltprid;
3366 * Make sure that we have allocated dquot(s) on disk.
3368 error = XFS_QM_DQVOPALLOC(mp, dp,
3369 current_fsuid(credp), current_fsgid(credp),
3370 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3371 if (error)
3372 goto std_return;
3374 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3375 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3377 * The symlink will fit into the inode data fork?
3378 * There can't be any attributes so we get the whole variable part.
3380 if (pathlen <= XFS_LITINO(mp))
3381 fs_blocks = 0;
3382 else
3383 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3384 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3385 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3386 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3387 if (error == ENOSPC && fs_blocks == 0) {
3388 resblks = 0;
3389 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3390 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3392 if (error) {
3393 cancel_flags = 0;
3394 dp = NULL;
3395 goto error_return;
3398 xfs_ilock(dp, XFS_ILOCK_EXCL);
3401 * Check whether the directory allows new symlinks or not.
3403 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3404 error = XFS_ERROR(EPERM);
3405 goto error_return;
3409 * Reserve disk quota : blocks and inode.
3411 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3412 if (error)
3413 goto error_return;
3416 * Check for ability to enter directory entry, if no space reserved.
3418 if (resblks == 0 &&
3419 (error = XFS_DIR_CANENTER(mp, tp, dp, link_name, link_namelen)))
3420 goto error_return;
3422 * Initialize the bmap freelist prior to calling either
3423 * bmapi or the directory create code.
3425 XFS_BMAP_INIT(&free_list, &first_block);
3428 * Allocate an inode for the symlink.
3430 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3431 1, 0, credp, prid, resblks > 0, &ip, NULL);
3432 if (error) {
3433 if (error == ENOSPC)
3434 goto error_return;
3435 goto error1;
3437 ITRACE(ip);
3439 VN_HOLD(dir_vp);
3440 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3441 dp_joined_to_trans = B_TRUE;
3444 * Also attach the dquot(s) to it, if applicable.
3446 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3448 if (resblks)
3449 resblks -= XFS_IALLOC_SPACE_RES(mp);
3451 * If the symlink will fit into the inode, write it inline.
3453 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3454 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3455 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3456 ip->i_d.di_size = pathlen;
3459 * The inode was initially created in extent format.
3461 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3462 ip->i_df.if_flags |= XFS_IFINLINE;
3464 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3465 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3467 } else {
3468 first_fsb = 0;
3469 nmaps = SYMLINK_MAPS;
3471 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3472 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3473 &first_block, resblks, mval, &nmaps,
3474 &free_list);
3475 if (error) {
3476 goto error1;
3479 if (resblks)
3480 resblks -= fs_blocks;
3481 ip->i_d.di_size = pathlen;
3482 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3484 cur_chunk = target_path;
3485 for (n = 0; n < nmaps; n++) {
3486 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3487 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3488 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3489 BTOBB(byte_cnt), 0);
3490 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3491 if (pathlen < byte_cnt) {
3492 byte_cnt = pathlen;
3494 pathlen -= byte_cnt;
3496 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3497 cur_chunk += byte_cnt;
3499 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3504 * Create the directory entry for the symlink.
3506 error = XFS_DIR_CREATENAME(mp, tp, dp, link_name, link_namelen,
3507 ip->i_ino, &first_block, &free_list, resblks);
3508 if (error) {
3509 goto error1;
3511 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3512 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3515 * Bump the in memory version number of the parent directory
3516 * so that other processes accessing it will recognize that
3517 * the directory has changed.
3519 dp->i_gen++;
3522 * If this is a synchronous mount, make sure that the
3523 * symlink transaction goes to disk before returning to
3524 * the user.
3526 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3527 xfs_trans_set_sync(tp);
3531 * xfs_trans_commit normally decrements the vnode ref count
3532 * when it unlocks the inode. Since we want to return the
3533 * vnode to the caller, we bump the vnode ref count now.
3535 IHOLD(ip);
3537 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
3538 if (error) {
3539 goto error2;
3541 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3542 XFS_QM_DQRELE(mp, udqp);
3543 XFS_QM_DQRELE(mp, gdqp);
3545 /* Fall through to std_return with error = 0 or errno from
3546 * xfs_trans_commit */
3547 std_return:
3548 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
3549 DM_EVENT_POSTSYMLINK)) {
3550 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3551 dir_vp, DM_RIGHT_NULL,
3552 error ? NULL : XFS_ITOV(ip),
3553 DM_RIGHT_NULL, link_name, target_path,
3554 0, error, 0);
3557 if (!error) {
3558 vnode_t *vp;
3560 ASSERT(ip);
3561 vp = XFS_ITOV(ip);
3562 *vpp = vp;
3564 return error;
3566 error2:
3567 IRELE(ip);
3568 error1:
3569 xfs_bmap_cancel(&free_list);
3570 cancel_flags |= XFS_TRANS_ABORT;
3571 error_return:
3572 xfs_trans_cancel(tp, cancel_flags);
3573 XFS_QM_DQRELE(mp, udqp);
3574 XFS_QM_DQRELE(mp, gdqp);
3576 if (!dp_joined_to_trans && (dp != NULL)) {
3577 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3580 goto std_return;
3585 * xfs_fid2
3587 * A fid routine that takes a pointer to a previously allocated
3588 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3590 STATIC int
3591 xfs_fid2(
3592 bhv_desc_t *bdp,
3593 fid_t *fidp)
3595 xfs_inode_t *ip;
3596 xfs_fid2_t *xfid;
3598 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
3599 (inst_t *)__return_address);
3600 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3602 xfid = (xfs_fid2_t *)fidp;
3603 ip = XFS_BHVTOI(bdp);
3604 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3605 xfid->fid_pad = 0;
3607 * use memcpy because the inode is a long long and there's no
3608 * assurance that xfid->fid_ino is properly aligned.
3610 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3611 xfid->fid_gen = ip->i_d.di_gen;
3613 return 0;
3618 * xfs_rwlock
3621 xfs_rwlock(
3622 bhv_desc_t *bdp,
3623 vrwlock_t locktype)
3625 xfs_inode_t *ip;
3626 vnode_t *vp;
3628 vp = BHV_TO_VNODE(bdp);
3629 if (vp->v_type == VDIR)
3630 return 1;
3631 ip = XFS_BHVTOI(bdp);
3632 if (locktype == VRWLOCK_WRITE) {
3633 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3634 } else if (locktype == VRWLOCK_TRY_READ) {
3635 return (xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED));
3636 } else if (locktype == VRWLOCK_TRY_WRITE) {
3637 return (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL));
3638 } else {
3639 ASSERT((locktype == VRWLOCK_READ) ||
3640 (locktype == VRWLOCK_WRITE_DIRECT));
3641 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3644 return 1;
3649 * xfs_rwunlock
3651 void
3652 xfs_rwunlock(
3653 bhv_desc_t *bdp,
3654 vrwlock_t locktype)
3656 xfs_inode_t *ip;
3657 vnode_t *vp;
3659 vp = BHV_TO_VNODE(bdp);
3660 if (vp->v_type == VDIR)
3661 return;
3662 ip = XFS_BHVTOI(bdp);
3663 if (locktype == VRWLOCK_WRITE) {
3665 * In the write case, we may have added a new entry to
3666 * the reference cache. This might store a pointer to
3667 * an inode to be released in this inode. If it is there,
3668 * clear the pointer and release the inode after unlocking
3669 * this one.
3671 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3672 } else {
3673 ASSERT((locktype == VRWLOCK_READ) ||
3674 (locktype == VRWLOCK_WRITE_DIRECT));
3675 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3677 return;
3680 STATIC int
3681 xfs_inode_flush(
3682 bhv_desc_t *bdp,
3683 int flags)
3685 xfs_inode_t *ip;
3686 xfs_mount_t *mp;
3687 xfs_inode_log_item_t *iip;
3688 int error = 0;
3690 ip = XFS_BHVTOI(bdp);
3691 mp = ip->i_mount;
3692 iip = ip->i_itemp;
3694 if (XFS_FORCED_SHUTDOWN(mp))
3695 return XFS_ERROR(EIO);
3698 * Bypass inodes which have already been cleaned by
3699 * the inode flush clustering code inside xfs_iflush
3701 if ((ip->i_update_core == 0) &&
3702 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3703 return 0;
3705 if (flags & FLUSH_LOG) {
3706 if (iip && iip->ili_last_lsn) {
3707 xlog_t *log = mp->m_log;
3708 xfs_lsn_t sync_lsn;
3709 int s, log_flags = XFS_LOG_FORCE;
3711 s = GRANT_LOCK(log);
3712 sync_lsn = log->l_last_sync_lsn;
3713 GRANT_UNLOCK(log, s);
3715 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
3716 return 0;
3718 if (flags & FLUSH_SYNC)
3719 log_flags |= XFS_LOG_SYNC;
3720 return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3725 * We make this non-blocking if the inode is contended,
3726 * return EAGAIN to indicate to the caller that they
3727 * did not succeed. This prevents the flush path from
3728 * blocking on inodes inside another operation right
3729 * now, they get caught later by xfs_sync.
3731 if (flags & FLUSH_INODE) {
3732 int flush_flags;
3734 if (xfs_ipincount(ip))
3735 return EAGAIN;
3737 if (flags & FLUSH_SYNC) {
3738 xfs_ilock(ip, XFS_ILOCK_SHARED);
3739 xfs_iflock(ip);
3740 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3741 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3742 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3743 return EAGAIN;
3745 } else {
3746 return EAGAIN;
3749 if (flags & FLUSH_SYNC)
3750 flush_flags = XFS_IFLUSH_SYNC;
3751 else
3752 flush_flags = XFS_IFLUSH_ASYNC;
3754 error = xfs_iflush(ip, flush_flags);
3755 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3758 return error;
3763 xfs_set_dmattrs (
3764 bhv_desc_t *bdp,
3765 u_int evmask,
3766 u_int16_t state,
3767 cred_t *credp)
3769 xfs_inode_t *ip;
3770 xfs_trans_t *tp;
3771 xfs_mount_t *mp;
3772 int error;
3774 if (!capable(CAP_SYS_ADMIN))
3775 return XFS_ERROR(EPERM);
3777 ip = XFS_BHVTOI(bdp);
3778 mp = ip->i_mount;
3780 if (XFS_FORCED_SHUTDOWN(mp))
3781 return XFS_ERROR(EIO);
3783 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3784 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3785 if (error) {
3786 xfs_trans_cancel(tp, 0);
3787 return error;
3789 xfs_ilock(ip, XFS_ILOCK_EXCL);
3790 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3792 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3793 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3795 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3796 IHOLD(ip);
3797 error = xfs_trans_commit(tp, 0, NULL);
3799 return error;
3804 * xfs_reclaim
3806 STATIC int
3807 xfs_reclaim(
3808 bhv_desc_t *bdp)
3810 xfs_inode_t *ip;
3811 vnode_t *vp;
3813 vp = BHV_TO_VNODE(bdp);
3814 ip = XFS_BHVTOI(bdp);
3816 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3818 ASSERT(!VN_MAPPED(vp));
3820 /* bad inode, get out here ASAP */
3821 if (VN_BAD(vp)) {
3822 xfs_ireclaim(ip);
3823 return 0;
3826 if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
3827 if (ip->i_d.di_size > 0) {
3829 * Flush and invalidate any data left around that is
3830 * a part of this file.
3832 * Get the inode's i/o lock so that buffers are pushed
3833 * out while holding the proper lock. We can't hold
3834 * the inode lock here since flushing out buffers may
3835 * cause us to try to get the lock in xfs_strategy().
3837 * We don't have to call remapf() here, because there
3838 * cannot be any mapped file references to this vnode
3839 * since it is being reclaimed.
3841 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3844 * If we hit an IO error, we need to make sure that the
3845 * buffer and page caches of file data for
3846 * the file are tossed away. We don't want to use
3847 * VOP_FLUSHINVAL_PAGES here because we don't want dirty
3848 * pages to stay attached to the vnode, but be
3849 * marked P_BAD. pdflush/vnode_pagebad
3850 * hates that.
3852 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3853 VOP_FLUSHINVAL_PAGES(vp, 0, -1, FI_NONE);
3854 } else {
3855 VOP_TOSS_PAGES(vp, 0, -1, FI_NONE);
3858 ASSERT(VN_CACHED(vp) == 0);
3859 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
3860 ip->i_delayed_blks == 0);
3861 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3862 } else if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3864 * di_size field may not be quite accurate if we're
3865 * shutting down.
3867 VOP_TOSS_PAGES(vp, 0, -1, FI_NONE);
3868 ASSERT(VN_CACHED(vp) == 0);
3872 /* If we have nothing to flush with this inode then complete the
3873 * teardown now, otherwise break the link between the xfs inode
3874 * and the linux inode and clean up the xfs inode later. This
3875 * avoids flushing the inode to disk during the delete operation
3876 * itself.
3878 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3879 xfs_ilock(ip, XFS_ILOCK_EXCL);
3880 xfs_iflock(ip);
3881 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3882 } else {
3883 xfs_mount_t *mp = ip->i_mount;
3885 /* Protect sync from us */
3886 XFS_MOUNT_ILOCK(mp);
3887 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
3888 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3889 ip->i_flags |= XFS_IRECLAIMABLE;
3890 XFS_MOUNT_IUNLOCK(mp);
3892 return 0;
3896 xfs_finish_reclaim(
3897 xfs_inode_t *ip,
3898 int locked,
3899 int sync_mode)
3901 xfs_ihash_t *ih = ip->i_hash;
3902 vnode_t *vp = XFS_ITOV_NULL(ip);
3903 int error;
3905 if (vp && VN_BAD(vp))
3906 goto reclaim;
3908 /* The hash lock here protects a thread in xfs_iget_core from
3909 * racing with us on linking the inode back with a vnode.
3910 * Once we have the XFS_IRECLAIM flag set it will not touch
3911 * us.
3913 write_lock(&ih->ih_lock);
3914 if ((ip->i_flags & XFS_IRECLAIM) ||
3915 (!(ip->i_flags & XFS_IRECLAIMABLE) && vp == NULL)) {
3916 write_unlock(&ih->ih_lock);
3917 if (locked) {
3918 xfs_ifunlock(ip);
3919 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3921 return(1);
3923 ip->i_flags |= XFS_IRECLAIM;
3924 write_unlock(&ih->ih_lock);
3927 * If the inode is still dirty, then flush it out. If the inode
3928 * is not in the AIL, then it will be OK to flush it delwri as
3929 * long as xfs_iflush() does not keep any references to the inode.
3930 * We leave that decision up to xfs_iflush() since it has the
3931 * knowledge of whether it's OK to simply do a delwri flush of
3932 * the inode or whether we need to wait until the inode is
3933 * pulled from the AIL.
3934 * We get the flush lock regardless, though, just to make sure
3935 * we don't free it while it is being flushed.
3937 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3938 if (!locked) {
3939 xfs_ilock(ip, XFS_ILOCK_EXCL);
3940 xfs_iflock(ip);
3943 if (ip->i_update_core ||
3944 ((ip->i_itemp != NULL) &&
3945 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3946 error = xfs_iflush(ip, sync_mode);
3948 * If we hit an error, typically because of filesystem
3949 * shutdown, we don't need to let vn_reclaim to know
3950 * because we're gonna reclaim the inode anyway.
3952 if (error) {
3953 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3954 goto reclaim;
3956 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3959 ASSERT(ip->i_update_core == 0);
3960 ASSERT(ip->i_itemp == NULL ||
3961 ip->i_itemp->ili_format.ilf_fields == 0);
3962 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3963 } else if (locked) {
3965 * We are not interested in doing an iflush if we're
3966 * in the process of shutting down the filesystem forcibly.
3967 * So, just reclaim the inode.
3969 xfs_ifunlock(ip);
3970 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3973 reclaim:
3974 xfs_ireclaim(ip);
3975 return 0;
3979 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3981 int purged;
3982 xfs_inode_t *ip, *n;
3983 int done = 0;
3985 while (!done) {
3986 purged = 0;
3987 XFS_MOUNT_ILOCK(mp);
3988 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3989 if (noblock) {
3990 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3991 continue;
3992 if (xfs_ipincount(ip) ||
3993 !xfs_iflock_nowait(ip)) {
3994 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3995 continue;
3998 XFS_MOUNT_IUNLOCK(mp);
3999 xfs_finish_reclaim(ip, noblock,
4000 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
4001 purged = 1;
4002 break;
4005 done = !purged;
4008 XFS_MOUNT_IUNLOCK(mp);
4009 return 0;
4013 * xfs_alloc_file_space()
4014 * This routine allocates disk space for the given file.
4016 * If alloc_type == 0, this request is for an ALLOCSP type
4017 * request which will change the file size. In this case, no
4018 * DMAPI event will be generated by the call. A TRUNCATE event
4019 * will be generated later by xfs_setattr.
4021 * If alloc_type != 0, this request is for a RESVSP type
4022 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
4023 * lower block boundary byte address is less than the file's
4024 * length.
4026 * RETURNS:
4027 * 0 on success
4028 * errno on error
4032 xfs_alloc_file_space(
4033 xfs_inode_t *ip,
4034 xfs_off_t offset,
4035 xfs_off_t len,
4036 int alloc_type,
4037 int attr_flags)
4039 xfs_filblks_t allocated_fsb;
4040 xfs_filblks_t allocatesize_fsb;
4041 int committed;
4042 xfs_off_t count;
4043 xfs_filblks_t datablocks;
4044 int error;
4045 xfs_fsblock_t firstfsb;
4046 xfs_bmap_free_t free_list;
4047 xfs_bmbt_irec_t *imapp;
4048 xfs_bmbt_irec_t imaps[1];
4049 xfs_mount_t *mp;
4050 int numrtextents;
4051 int reccount;
4052 uint resblks;
4053 int rt;
4054 int rtextsize;
4055 xfs_fileoff_t startoffset_fsb;
4056 xfs_trans_t *tp;
4057 int xfs_bmapi_flags;
4059 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4060 mp = ip->i_mount;
4062 if (XFS_FORCED_SHUTDOWN(mp))
4063 return XFS_ERROR(EIO);
4066 * determine if this is a realtime file
4068 if ((rt = XFS_IS_REALTIME_INODE(ip)) != 0) {
4069 if (ip->i_d.di_extsize)
4070 rtextsize = ip->i_d.di_extsize;
4071 else
4072 rtextsize = mp->m_sb.sb_rextsize;
4073 } else
4074 rtextsize = 0;
4076 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4077 return error;
4079 if (len <= 0)
4080 return XFS_ERROR(EINVAL);
4082 count = len;
4083 error = 0;
4084 imapp = &imaps[0];
4085 reccount = 1;
4086 xfs_bmapi_flags = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
4087 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
4088 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
4090 /* Generate a DMAPI event if needed. */
4091 if (alloc_type != 0 && offset < ip->i_d.di_size &&
4092 (attr_flags&ATTR_DMI) == 0 &&
4093 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4094 xfs_off_t end_dmi_offset;
4096 end_dmi_offset = offset+len;
4097 if (end_dmi_offset > ip->i_d.di_size)
4098 end_dmi_offset = ip->i_d.di_size;
4099 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4100 offset, end_dmi_offset - offset,
4101 0, NULL);
4102 if (error)
4103 return(error);
4107 * allocate file space until done or until there is an error
4109 retry:
4110 while (allocatesize_fsb && !error) {
4112 * determine if reserving space on
4113 * the data or realtime partition.
4115 if (rt) {
4116 xfs_fileoff_t s, e;
4118 s = startoffset_fsb;
4119 do_div(s, rtextsize);
4120 s *= rtextsize;
4121 e = roundup_64(startoffset_fsb + allocatesize_fsb,
4122 rtextsize);
4123 numrtextents = (int)(e - s) / mp->m_sb.sb_rextsize;
4124 datablocks = 0;
4125 } else {
4126 datablocks = allocatesize_fsb;
4127 numrtextents = 0;
4131 * allocate and setup the transaction
4133 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4134 resblks = XFS_DIOSTRAT_SPACE_RES(mp, datablocks);
4135 error = xfs_trans_reserve(tp,
4136 resblks,
4137 XFS_WRITE_LOG_RES(mp),
4138 numrtextents,
4139 XFS_TRANS_PERM_LOG_RES,
4140 XFS_WRITE_LOG_COUNT);
4143 * check for running out of space
4145 if (error) {
4147 * Free the transaction structure.
4149 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4150 xfs_trans_cancel(tp, 0);
4151 break;
4153 xfs_ilock(ip, XFS_ILOCK_EXCL);
4154 error = XFS_TRANS_RESERVE_QUOTA_BYDQUOTS(mp, tp,
4155 ip->i_udquot, ip->i_gdquot, resblks, 0, rt ?
4156 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4157 if (error)
4158 goto error1;
4160 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4161 xfs_trans_ihold(tp, ip);
4164 * issue the bmapi() call to allocate the blocks
4166 XFS_BMAP_INIT(&free_list, &firstfsb);
4167 error = xfs_bmapi(tp, ip, startoffset_fsb,
4168 allocatesize_fsb, xfs_bmapi_flags,
4169 &firstfsb, 0, imapp, &reccount,
4170 &free_list);
4171 if (error) {
4172 goto error0;
4176 * complete the transaction
4178 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4179 if (error) {
4180 goto error0;
4183 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4184 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4185 if (error) {
4186 break;
4189 allocated_fsb = imapp->br_blockcount;
4191 if (reccount == 0) {
4192 error = XFS_ERROR(ENOSPC);
4193 break;
4196 startoffset_fsb += allocated_fsb;
4197 allocatesize_fsb -= allocated_fsb;
4199 dmapi_enospc_check:
4200 if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
4201 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
4203 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4204 XFS_ITOV(ip), DM_RIGHT_NULL,
4205 XFS_ITOV(ip), DM_RIGHT_NULL,
4206 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4207 if (error == 0)
4208 goto retry; /* Maybe DMAPI app. has made space */
4209 /* else fall through with error from XFS_SEND_DATA */
4212 return error;
4214 error0:
4215 xfs_bmap_cancel(&free_list);
4216 error1:
4217 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4218 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4219 goto dmapi_enospc_check;
4223 * Zero file bytes between startoff and endoff inclusive.
4224 * The iolock is held exclusive and no blocks are buffered.
4226 STATIC int
4227 xfs_zero_remaining_bytes(
4228 xfs_inode_t *ip,
4229 xfs_off_t startoff,
4230 xfs_off_t endoff)
4232 xfs_bmbt_irec_t imap;
4233 xfs_fileoff_t offset_fsb;
4234 xfs_off_t lastoffset;
4235 xfs_off_t offset;
4236 xfs_buf_t *bp;
4237 xfs_mount_t *mp = ip->i_mount;
4238 int nimap;
4239 int error = 0;
4241 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4242 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4243 mp->m_rtdev_targp : mp->m_ddev_targp);
4245 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4246 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4247 nimap = 1;
4248 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0, NULL, 0, &imap,
4249 &nimap, NULL);
4250 if (error || nimap < 1)
4251 break;
4252 ASSERT(imap.br_blockcount >= 1);
4253 ASSERT(imap.br_startoff == offset_fsb);
4254 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4255 if (lastoffset > endoff)
4256 lastoffset = endoff;
4257 if (imap.br_startblock == HOLESTARTBLOCK)
4258 continue;
4259 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4260 if (imap.br_state == XFS_EXT_UNWRITTEN)
4261 continue;
4262 XFS_BUF_UNDONE(bp);
4263 XFS_BUF_UNWRITE(bp);
4264 XFS_BUF_READ(bp);
4265 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4266 xfsbdstrat(mp, bp);
4267 if ((error = xfs_iowait(bp))) {
4268 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4269 mp, bp, XFS_BUF_ADDR(bp));
4270 break;
4272 memset(XFS_BUF_PTR(bp) +
4273 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4274 0, lastoffset - offset + 1);
4275 XFS_BUF_UNDONE(bp);
4276 XFS_BUF_UNREAD(bp);
4277 XFS_BUF_WRITE(bp);
4278 xfsbdstrat(mp, bp);
4279 if ((error = xfs_iowait(bp))) {
4280 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4281 mp, bp, XFS_BUF_ADDR(bp));
4282 break;
4285 xfs_buf_free(bp);
4286 return error;
4290 * xfs_free_file_space()
4291 * This routine frees disk space for the given file.
4293 * This routine is only called by xfs_change_file_space
4294 * for an UNRESVSP type call.
4296 * RETURNS:
4297 * 0 on success
4298 * errno on error
4301 STATIC int
4302 xfs_free_file_space(
4303 xfs_inode_t *ip,
4304 xfs_off_t offset,
4305 xfs_off_t len,
4306 int attr_flags)
4308 int committed;
4309 int done;
4310 xfs_off_t end_dmi_offset;
4311 xfs_fileoff_t endoffset_fsb;
4312 int error;
4313 xfs_fsblock_t firstfsb;
4314 xfs_bmap_free_t free_list;
4315 xfs_off_t ilen;
4316 xfs_bmbt_irec_t imap;
4317 xfs_off_t ioffset;
4318 xfs_extlen_t mod=0;
4319 xfs_mount_t *mp;
4320 int nimap;
4321 uint resblks;
4322 int rounding;
4323 int rt;
4324 xfs_fileoff_t startoffset_fsb;
4325 xfs_trans_t *tp;
4326 int need_iolock = 1;
4328 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4329 mp = ip->i_mount;
4331 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4332 return error;
4334 error = 0;
4335 if (len <= 0) /* if nothing being freed */
4336 return error;
4337 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4338 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4339 end_dmi_offset = offset + len;
4340 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4342 if (offset < ip->i_d.di_size &&
4343 (attr_flags & ATTR_DMI) == 0 &&
4344 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4345 if (end_dmi_offset > ip->i_d.di_size)
4346 end_dmi_offset = ip->i_d.di_size;
4347 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4348 offset, end_dmi_offset - offset,
4349 AT_DELAY_FLAG(attr_flags), NULL);
4350 if (error)
4351 return(error);
4354 ASSERT(attr_flags & ATTR_NOLOCK ? attr_flags & ATTR_DMI : 1);
4355 if (attr_flags & ATTR_NOLOCK)
4356 need_iolock = 0;
4357 if (need_iolock)
4358 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4360 rounding = MAX((__uint8_t)(1 << mp->m_sb.sb_blocklog),
4361 (__uint8_t)NBPP);
4362 ilen = len + (offset & (rounding - 1));
4363 ioffset = offset & ~(rounding - 1);
4364 if (ilen & (rounding - 1))
4365 ilen = (ilen + rounding) & ~(rounding - 1);
4366 xfs_inval_cached_pages(XFS_ITOV(ip), &(ip->i_iocore), ioffset, 0, 0);
4368 * Need to zero the stuff we're not freeing, on disk.
4369 * If its a realtime file & can't use unwritten extents then we
4370 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4371 * will take care of it for us.
4373 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4374 nimap = 1;
4375 error = xfs_bmapi(NULL, ip, startoffset_fsb, 1, 0, NULL, 0,
4376 &imap, &nimap, NULL);
4377 if (error)
4378 goto out_unlock_iolock;
4379 ASSERT(nimap == 0 || nimap == 1);
4380 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4381 xfs_daddr_t block;
4383 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4384 block = imap.br_startblock;
4385 mod = do_div(block, mp->m_sb.sb_rextsize);
4386 if (mod)
4387 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4389 nimap = 1;
4390 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1, 1, 0, NULL, 0,
4391 &imap, &nimap, NULL);
4392 if (error)
4393 goto out_unlock_iolock;
4394 ASSERT(nimap == 0 || nimap == 1);
4395 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4396 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4397 mod++;
4398 if (mod && (mod != mp->m_sb.sb_rextsize))
4399 endoffset_fsb -= mod;
4402 if ((done = (endoffset_fsb <= startoffset_fsb)))
4404 * One contiguous piece to clear
4406 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4407 else {
4409 * Some full blocks, possibly two pieces to clear
4411 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4412 error = xfs_zero_remaining_bytes(ip, offset,
4413 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4414 if (!error &&
4415 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4416 error = xfs_zero_remaining_bytes(ip,
4417 XFS_FSB_TO_B(mp, endoffset_fsb),
4418 offset + len - 1);
4422 * free file space until done or until there is an error
4424 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4425 while (!error && !done) {
4428 * allocate and setup the transaction
4430 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4431 error = xfs_trans_reserve(tp,
4432 resblks,
4433 XFS_WRITE_LOG_RES(mp),
4435 XFS_TRANS_PERM_LOG_RES,
4436 XFS_WRITE_LOG_COUNT);
4439 * check for running out of space
4441 if (error) {
4443 * Free the transaction structure.
4445 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4446 xfs_trans_cancel(tp, 0);
4447 break;
4449 xfs_ilock(ip, XFS_ILOCK_EXCL);
4450 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4451 ip->i_udquot, ip->i_gdquot, resblks, 0, rt ?
4452 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4453 if (error)
4454 goto error1;
4456 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4457 xfs_trans_ihold(tp, ip);
4460 * issue the bunmapi() call to free the blocks
4462 XFS_BMAP_INIT(&free_list, &firstfsb);
4463 error = xfs_bunmapi(tp, ip, startoffset_fsb,
4464 endoffset_fsb - startoffset_fsb,
4465 0, 2, &firstfsb, &free_list, &done);
4466 if (error) {
4467 goto error0;
4471 * complete the transaction
4473 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4474 if (error) {
4475 goto error0;
4478 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4479 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4482 out_unlock_iolock:
4483 if (need_iolock)
4484 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4485 return error;
4487 error0:
4488 xfs_bmap_cancel(&free_list);
4489 error1:
4490 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4491 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4492 XFS_ILOCK_EXCL);
4493 return error;
4497 * xfs_change_file_space()
4498 * This routine allocates or frees disk space for the given file.
4499 * The user specified parameters are checked for alignment and size
4500 * limitations.
4502 * RETURNS:
4503 * 0 on success
4504 * errno on error
4508 xfs_change_file_space(
4509 bhv_desc_t *bdp,
4510 int cmd,
4511 xfs_flock64_t *bf,
4512 xfs_off_t offset,
4513 cred_t *credp,
4514 int attr_flags)
4516 int clrprealloc;
4517 int error;
4518 xfs_fsize_t fsize;
4519 xfs_inode_t *ip;
4520 xfs_mount_t *mp;
4521 int setprealloc;
4522 xfs_off_t startoffset;
4523 xfs_off_t llen;
4524 xfs_trans_t *tp;
4525 vattr_t va;
4526 vnode_t *vp;
4528 vp = BHV_TO_VNODE(bdp);
4529 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4531 ip = XFS_BHVTOI(bdp);
4532 mp = ip->i_mount;
4535 * must be a regular file and have write permission
4537 if (vp->v_type != VREG)
4538 return XFS_ERROR(EINVAL);
4540 xfs_ilock(ip, XFS_ILOCK_SHARED);
4542 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4543 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4544 return error;
4547 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4549 switch (bf->l_whence) {
4550 case 0: /*SEEK_SET*/
4551 break;
4552 case 1: /*SEEK_CUR*/
4553 bf->l_start += offset;
4554 break;
4555 case 2: /*SEEK_END*/
4556 bf->l_start += ip->i_d.di_size;
4557 break;
4558 default:
4559 return XFS_ERROR(EINVAL);
4562 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4564 if ( (bf->l_start < 0)
4565 || (bf->l_start > XFS_MAXIOFFSET(mp))
4566 || (bf->l_start + llen < 0)
4567 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4568 return XFS_ERROR(EINVAL);
4570 bf->l_whence = 0;
4572 startoffset = bf->l_start;
4573 fsize = ip->i_d.di_size;
4576 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4577 * file space.
4578 * These calls do NOT zero the data space allocated to the file,
4579 * nor do they change the file size.
4581 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4582 * space.
4583 * These calls cause the new file data to be zeroed and the file
4584 * size to be changed.
4586 setprealloc = clrprealloc = 0;
4588 switch (cmd) {
4589 case XFS_IOC_RESVSP:
4590 case XFS_IOC_RESVSP64:
4591 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4592 1, attr_flags);
4593 if (error)
4594 return error;
4595 setprealloc = 1;
4596 break;
4598 case XFS_IOC_UNRESVSP:
4599 case XFS_IOC_UNRESVSP64:
4600 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4601 attr_flags)))
4602 return error;
4603 break;
4605 case XFS_IOC_ALLOCSP:
4606 case XFS_IOC_ALLOCSP64:
4607 case XFS_IOC_FREESP:
4608 case XFS_IOC_FREESP64:
4609 if (startoffset > fsize) {
4610 error = xfs_alloc_file_space(ip, fsize,
4611 startoffset - fsize, 0, attr_flags);
4612 if (error)
4613 break;
4616 va.va_mask = XFS_AT_SIZE;
4617 va.va_size = startoffset;
4619 error = xfs_setattr(bdp, &va, attr_flags, credp);
4621 if (error)
4622 return error;
4624 clrprealloc = 1;
4625 break;
4627 default:
4628 ASSERT(0);
4629 return XFS_ERROR(EINVAL);
4633 * update the inode timestamp, mode, and prealloc flag bits
4635 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4637 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4638 0, 0, 0))) {
4639 /* ASSERT(0); */
4640 xfs_trans_cancel(tp, 0);
4641 return error;
4644 xfs_ilock(ip, XFS_ILOCK_EXCL);
4646 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4647 xfs_trans_ihold(tp, ip);
4649 if ((attr_flags & ATTR_DMI) == 0) {
4650 ip->i_d.di_mode &= ~S_ISUID;
4653 * Note that we don't have to worry about mandatory
4654 * file locking being disabled here because we only
4655 * clear the S_ISGID bit if the Group execute bit is
4656 * on, but if it was on then mandatory locking wouldn't
4657 * have been enabled.
4659 if (ip->i_d.di_mode & S_IXGRP)
4660 ip->i_d.di_mode &= ~S_ISGID;
4662 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4664 if (setprealloc)
4665 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4666 else if (clrprealloc)
4667 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4669 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4670 xfs_trans_set_sync(tp);
4672 error = xfs_trans_commit(tp, 0, NULL);
4674 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4676 return error;
4679 vnodeops_t xfs_vnodeops = {
4680 BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
4681 .vop_open = xfs_open,
4682 .vop_read = xfs_read,
4683 #ifdef HAVE_SENDFILE
4684 .vop_sendfile = xfs_sendfile,
4685 #endif
4686 .vop_write = xfs_write,
4687 .vop_ioctl = xfs_ioctl,
4688 .vop_getattr = xfs_getattr,
4689 .vop_setattr = xfs_setattr,
4690 .vop_access = xfs_access,
4691 .vop_lookup = xfs_lookup,
4692 .vop_create = xfs_create,
4693 .vop_remove = xfs_remove,
4694 .vop_link = xfs_link,
4695 .vop_rename = xfs_rename,
4696 .vop_mkdir = xfs_mkdir,
4697 .vop_rmdir = xfs_rmdir,
4698 .vop_readdir = xfs_readdir,
4699 .vop_symlink = xfs_symlink,
4700 .vop_readlink = xfs_readlink,
4701 .vop_fsync = xfs_fsync,
4702 .vop_inactive = xfs_inactive,
4703 .vop_fid2 = xfs_fid2,
4704 .vop_rwlock = xfs_rwlock,
4705 .vop_rwunlock = xfs_rwunlock,
4706 .vop_bmap = xfs_bmap,
4707 .vop_reclaim = xfs_reclaim,
4708 .vop_attr_get = xfs_attr_get,
4709 .vop_attr_set = xfs_attr_set,
4710 .vop_attr_remove = xfs_attr_remove,
4711 .vop_attr_list = xfs_attr_list,
4712 .vop_link_removed = (vop_link_removed_t)fs_noval,
4713 .vop_vnode_change = (vop_vnode_change_t)fs_noval,
4714 .vop_tosspages = fs_tosspages,
4715 .vop_flushinval_pages = fs_flushinval_pages,
4716 .vop_flush_pages = fs_flush_pages,
4717 .vop_release = xfs_release,
4718 .vop_iflush = xfs_inode_flush,