xfs: fix mmap_sem/iolock inversion in xfs_free_eofblocks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_vnodeops.c
blobd98401470cf0195da1cb503a7ba68c16f0ba1148
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_types.h"
22 #include "xfs_bit.h"
23 #include "xfs_log.h"
24 #include "xfs_inum.h"
25 #include "xfs_trans.h"
26 #include "xfs_sb.h"
27 #include "xfs_ag.h"
28 #include "xfs_dir2.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_itable.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_alloc.h"
44 #include "xfs_bmap.h"
45 #include "xfs_acl.h"
46 #include "xfs_attr.h"
47 #include "xfs_rw.h"
48 #include "xfs_error.h"
49 #include "xfs_quota.h"
50 #include "xfs_utils.h"
51 #include "xfs_rtalloc.h"
52 #include "xfs_trans_space.h"
53 #include "xfs_log_priv.h"
54 #include "xfs_filestream.h"
55 #include "xfs_vnodeops.h"
57 int
58 xfs_setattr(
59 struct xfs_inode *ip,
60 struct iattr *iattr,
61 int flags)
63 xfs_mount_t *mp = ip->i_mount;
64 struct inode *inode = VFS_I(ip);
65 int mask = iattr->ia_valid;
66 xfs_trans_t *tp;
67 int code;
68 uint lock_flags;
69 uint commit_flags=0;
70 uid_t uid=0, iuid=0;
71 gid_t gid=0, igid=0;
72 int timeflags = 0;
73 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
74 int need_iolock = 1;
76 xfs_itrace_entry(ip);
78 if (mp->m_flags & XFS_MOUNT_RDONLY)
79 return XFS_ERROR(EROFS);
81 if (XFS_FORCED_SHUTDOWN(mp))
82 return XFS_ERROR(EIO);
84 code = -inode_change_ok(inode, iattr);
85 if (code)
86 return code;
88 olddquot1 = olddquot2 = NULL;
89 udqp = gdqp = NULL;
92 * If disk quotas is on, we make sure that the dquots do exist on disk,
93 * before we start any other transactions. Trying to do this later
94 * is messy. We don't care to take a readlock to look at the ids
95 * in inode here, because we can't hold it across the trans_reserve.
96 * If the IDs do change before we take the ilock, we're covered
97 * because the i_*dquot fields will get updated anyway.
99 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
100 uint qflags = 0;
102 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
103 uid = iattr->ia_uid;
104 qflags |= XFS_QMOPT_UQUOTA;
105 } else {
106 uid = ip->i_d.di_uid;
108 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
109 gid = iattr->ia_gid;
110 qflags |= XFS_QMOPT_GQUOTA;
111 } else {
112 gid = ip->i_d.di_gid;
116 * We take a reference when we initialize udqp and gdqp,
117 * so it is important that we never blindly double trip on
118 * the same variable. See xfs_create() for an example.
120 ASSERT(udqp == NULL);
121 ASSERT(gdqp == NULL);
122 code = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid,
123 qflags, &udqp, &gdqp);
124 if (code)
125 return code;
129 * For the other attributes, we acquire the inode lock and
130 * first do an error checking pass.
132 tp = NULL;
133 lock_flags = XFS_ILOCK_EXCL;
134 if (flags & XFS_ATTR_NOLOCK)
135 need_iolock = 0;
136 if (!(mask & ATTR_SIZE)) {
137 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
138 (mp->m_flags & XFS_MOUNT_WSYNC)) {
139 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
140 commit_flags = 0;
141 if ((code = xfs_trans_reserve(tp, 0,
142 XFS_ICHANGE_LOG_RES(mp), 0,
143 0, 0))) {
144 lock_flags = 0;
145 goto error_return;
148 } else {
149 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
150 !(flags & XFS_ATTR_DMI)) {
151 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
152 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
153 iattr->ia_size, 0, dmflags, NULL);
154 if (code) {
155 lock_flags = 0;
156 goto error_return;
159 if (need_iolock)
160 lock_flags |= XFS_IOLOCK_EXCL;
163 xfs_ilock(ip, lock_flags);
166 * Change file ownership. Must be the owner or privileged.
168 if (mask & (ATTR_UID|ATTR_GID)) {
170 * These IDs could have changed since we last looked at them.
171 * But, we're assured that if the ownership did change
172 * while we didn't have the inode locked, inode's dquot(s)
173 * would have changed also.
175 iuid = ip->i_d.di_uid;
176 igid = ip->i_d.di_gid;
177 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
178 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
181 * Do a quota reservation only if uid/gid is actually
182 * going to change.
184 if (XFS_IS_QUOTA_RUNNING(mp) &&
185 ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
186 (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
187 ASSERT(tp);
188 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
189 capable(CAP_FOWNER) ?
190 XFS_QMOPT_FORCE_RES : 0);
191 if (code) /* out of quota */
192 goto error_return;
197 * Truncate file. Must have write permission and not be a directory.
199 if (mask & ATTR_SIZE) {
200 /* Short circuit the truncate case for zero length files */
201 if (iattr->ia_size == 0 &&
202 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
203 xfs_iunlock(ip, XFS_ILOCK_EXCL);
204 lock_flags &= ~XFS_ILOCK_EXCL;
205 if (mask & ATTR_CTIME)
206 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
207 code = 0;
208 goto error_return;
211 if (S_ISDIR(ip->i_d.di_mode)) {
212 code = XFS_ERROR(EISDIR);
213 goto error_return;
214 } else if (!S_ISREG(ip->i_d.di_mode)) {
215 code = XFS_ERROR(EINVAL);
216 goto error_return;
220 * Make sure that the dquots are attached to the inode.
222 code = xfs_qm_dqattach_locked(ip, 0);
223 if (code)
224 goto error_return;
227 * Now we can make the changes. Before we join the inode
228 * to the transaction, if ATTR_SIZE is set then take care of
229 * the part of the truncation that must be done without the
230 * inode lock. This needs to be done before joining the inode
231 * to the transaction, because the inode cannot be unlocked
232 * once it is a part of the transaction.
234 if (iattr->ia_size > ip->i_size) {
236 * Do the first part of growing a file: zero any data
237 * in the last block that is beyond the old EOF. We
238 * need to do this before the inode is joined to the
239 * transaction to modify the i_size.
241 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
243 xfs_iunlock(ip, XFS_ILOCK_EXCL);
246 * We are going to log the inode size change in this
247 * transaction so any previous writes that are beyond the on
248 * disk EOF and the new EOF that have not been written out need
249 * to be written here. If we do not write the data out, we
250 * expose ourselves to the null files problem.
252 * Only flush from the on disk size to the smaller of the in
253 * memory file size or the new size as that's the range we
254 * really care about here and prevents waiting for other data
255 * not within the range we care about here.
257 if (!code &&
258 ip->i_size != ip->i_d.di_size &&
259 iattr->ia_size > ip->i_d.di_size) {
260 code = xfs_flush_pages(ip,
261 ip->i_d.di_size, iattr->ia_size,
262 XFS_B_ASYNC, FI_NONE);
265 /* wait for all I/O to complete */
266 xfs_ioend_wait(ip);
268 if (!code)
269 code = xfs_itruncate_data(ip, iattr->ia_size);
270 if (code) {
271 ASSERT(tp == NULL);
272 lock_flags &= ~XFS_ILOCK_EXCL;
273 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
274 goto error_return;
276 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
277 if ((code = xfs_trans_reserve(tp, 0,
278 XFS_ITRUNCATE_LOG_RES(mp), 0,
279 XFS_TRANS_PERM_LOG_RES,
280 XFS_ITRUNCATE_LOG_COUNT))) {
281 xfs_trans_cancel(tp, 0);
282 if (need_iolock)
283 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
284 return code;
286 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
287 xfs_ilock(ip, XFS_ILOCK_EXCL);
289 xfs_trans_ijoin(tp, ip, lock_flags);
290 xfs_trans_ihold(tp, ip);
293 * Only change the c/mtime if we are changing the size
294 * or we are explicitly asked to change it. This handles
295 * the semantic difference between truncate() and ftruncate()
296 * as implemented in the VFS.
298 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
299 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
301 if (iattr->ia_size > ip->i_size) {
302 ip->i_d.di_size = iattr->ia_size;
303 ip->i_size = iattr->ia_size;
304 if (!(flags & XFS_ATTR_DMI))
305 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
306 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
307 } else if (iattr->ia_size <= ip->i_size ||
308 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
310 * signal a sync transaction unless
311 * we're truncating an already unlinked
312 * file on a wsync filesystem
314 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
315 XFS_DATA_FORK,
316 ((ip->i_d.di_nlink != 0 ||
317 !(mp->m_flags & XFS_MOUNT_WSYNC))
318 ? 1 : 0));
319 if (code)
320 goto abort_return;
322 * Truncated "down", so we're removing references
323 * to old data here - if we now delay flushing for
324 * a long time, we expose ourselves unduly to the
325 * notorious NULL files problem. So, we mark this
326 * vnode and flush it when the file is closed, and
327 * do not wait the usual (long) time for writeout.
329 xfs_iflags_set(ip, XFS_ITRUNCATED);
331 } else if (tp) {
332 xfs_trans_ijoin(tp, ip, lock_flags);
333 xfs_trans_ihold(tp, ip);
337 * Change file ownership. Must be the owner or privileged.
339 if (mask & (ATTR_UID|ATTR_GID)) {
341 * CAP_FSETID overrides the following restrictions:
343 * The set-user-ID and set-group-ID bits of a file will be
344 * cleared upon successful return from chown()
346 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
347 !capable(CAP_FSETID)) {
348 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
352 * Change the ownerships and register quota modifications
353 * in the transaction.
355 if (iuid != uid) {
356 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
357 ASSERT(mask & ATTR_UID);
358 ASSERT(udqp);
359 olddquot1 = xfs_qm_vop_chown(tp, ip,
360 &ip->i_udquot, udqp);
362 ip->i_d.di_uid = uid;
363 inode->i_uid = uid;
365 if (igid != gid) {
366 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
367 ASSERT(!XFS_IS_PQUOTA_ON(mp));
368 ASSERT(mask & ATTR_GID);
369 ASSERT(gdqp);
370 olddquot2 = xfs_qm_vop_chown(tp, ip,
371 &ip->i_gdquot, gdqp);
373 ip->i_d.di_gid = gid;
374 inode->i_gid = gid;
377 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
378 timeflags |= XFS_ICHGTIME_CHG;
382 * Change file access modes.
384 if (mask & ATTR_MODE) {
385 umode_t mode = iattr->ia_mode;
387 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
388 mode &= ~S_ISGID;
390 ip->i_d.di_mode &= S_IFMT;
391 ip->i_d.di_mode |= mode & ~S_IFMT;
393 inode->i_mode &= S_IFMT;
394 inode->i_mode |= mode & ~S_IFMT;
396 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
397 timeflags |= XFS_ICHGTIME_CHG;
401 * Change file access or modified times.
403 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
404 if (mask & ATTR_ATIME) {
405 inode->i_atime = iattr->ia_atime;
406 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
407 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
408 ip->i_update_core = 1;
410 if (mask & ATTR_MTIME) {
411 inode->i_mtime = iattr->ia_mtime;
412 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
413 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
414 timeflags &= ~XFS_ICHGTIME_MOD;
415 timeflags |= XFS_ICHGTIME_CHG;
417 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
418 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
422 * Change file inode change time only if ATTR_CTIME set
423 * AND we have been called by a DMI function.
426 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
427 inode->i_ctime = iattr->ia_ctime;
428 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
429 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
430 ip->i_update_core = 1;
431 timeflags &= ~XFS_ICHGTIME_CHG;
435 * Send out timestamp changes that need to be set to the
436 * current time. Not done when called by a DMI function.
438 if (timeflags && !(flags & XFS_ATTR_DMI))
439 xfs_ichgtime(ip, timeflags);
441 XFS_STATS_INC(xs_ig_attrchg);
444 * If this is a synchronous mount, make sure that the
445 * transaction goes to disk before returning to the user.
446 * This is slightly sub-optimal in that truncates require
447 * two sync transactions instead of one for wsync filesystems.
448 * One for the truncate and one for the timestamps since we
449 * don't want to change the timestamps unless we're sure the
450 * truncate worked. Truncates are less than 1% of the laddis
451 * mix so this probably isn't worth the trouble to optimize.
453 code = 0;
454 if (tp) {
455 if (mp->m_flags & XFS_MOUNT_WSYNC)
456 xfs_trans_set_sync(tp);
458 code = xfs_trans_commit(tp, commit_flags);
461 xfs_iunlock(ip, lock_flags);
464 * Release any dquot(s) the inode had kept before chown.
466 xfs_qm_dqrele(olddquot1);
467 xfs_qm_dqrele(olddquot2);
468 xfs_qm_dqrele(udqp);
469 xfs_qm_dqrele(gdqp);
471 if (code)
472 return code;
475 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
476 * update. We could avoid this with linked transactions
477 * and passing down the transaction pointer all the way
478 * to attr_set. No previous user of the generic
479 * Posix ACL code seems to care about this issue either.
481 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
482 code = -xfs_acl_chmod(inode);
483 if (code)
484 return XFS_ERROR(code);
487 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
488 !(flags & XFS_ATTR_DMI)) {
489 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
490 NULL, DM_RIGHT_NULL, NULL, NULL,
491 0, 0, AT_DELAY_FLAG(flags));
493 return 0;
495 abort_return:
496 commit_flags |= XFS_TRANS_ABORT;
497 /* FALLTHROUGH */
498 error_return:
499 xfs_qm_dqrele(udqp);
500 xfs_qm_dqrele(gdqp);
501 if (tp) {
502 xfs_trans_cancel(tp, commit_flags);
504 if (lock_flags != 0) {
505 xfs_iunlock(ip, lock_flags);
507 return code;
511 * The maximum pathlen is 1024 bytes. Since the minimum file system
512 * blocksize is 512 bytes, we can get a max of 2 extents back from
513 * bmapi.
515 #define SYMLINK_MAPS 2
517 STATIC int
518 xfs_readlink_bmap(
519 xfs_inode_t *ip,
520 char *link)
522 xfs_mount_t *mp = ip->i_mount;
523 int pathlen = ip->i_d.di_size;
524 int nmaps = SYMLINK_MAPS;
525 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
526 xfs_daddr_t d;
527 int byte_cnt;
528 int n;
529 xfs_buf_t *bp;
530 int error = 0;
532 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
533 mval, &nmaps, NULL, NULL);
534 if (error)
535 goto out;
537 for (n = 0; n < nmaps; n++) {
538 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
539 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
541 bp = xfs_buf_read_flags(mp->m_ddev_targp, d, BTOBB(byte_cnt),
542 XBF_LOCK | XBF_MAPPED |
543 XBF_DONT_BLOCK);
544 error = XFS_BUF_GETERROR(bp);
545 if (error) {
546 xfs_ioerror_alert("xfs_readlink",
547 ip->i_mount, bp, XFS_BUF_ADDR(bp));
548 xfs_buf_relse(bp);
549 goto out;
551 if (pathlen < byte_cnt)
552 byte_cnt = pathlen;
553 pathlen -= byte_cnt;
555 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
556 xfs_buf_relse(bp);
559 link[ip->i_d.di_size] = '\0';
560 error = 0;
562 out:
563 return error;
567 xfs_readlink(
568 xfs_inode_t *ip,
569 char *link)
571 xfs_mount_t *mp = ip->i_mount;
572 int pathlen;
573 int error = 0;
575 xfs_itrace_entry(ip);
577 if (XFS_FORCED_SHUTDOWN(mp))
578 return XFS_ERROR(EIO);
580 xfs_ilock(ip, XFS_ILOCK_SHARED);
582 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
583 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
585 pathlen = ip->i_d.di_size;
586 if (!pathlen)
587 goto out;
589 if (ip->i_df.if_flags & XFS_IFINLINE) {
590 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
591 link[pathlen] = '\0';
592 } else {
593 error = xfs_readlink_bmap(ip, link);
596 out:
597 xfs_iunlock(ip, XFS_ILOCK_SHARED);
598 return error;
602 * xfs_fsync
604 * This is called to sync the inode and its data out to disk. We need to hold
605 * the I/O lock while flushing the data, and the inode lock while flushing the
606 * inode. The inode lock CANNOT be held while flushing the data, so acquire
607 * after we're done with that.
610 xfs_fsync(
611 xfs_inode_t *ip)
613 xfs_trans_t *tp;
614 int error = 0;
615 int log_flushed = 0, changed = 1;
617 xfs_itrace_entry(ip);
619 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
620 return XFS_ERROR(EIO);
623 * We always need to make sure that the required inode state is safe on
624 * disk. The inode might be clean but we still might need to force the
625 * log because of committed transactions that haven't hit the disk yet.
626 * Likewise, there could be unflushed non-transactional changes to the
627 * inode core that have to go to disk and this requires us to issue
628 * a synchronous transaction to capture these changes correctly.
630 * This code relies on the assumption that if the update_* fields
631 * of the inode are clear and the inode is unpinned then it is clean
632 * and no action is required.
634 xfs_ilock(ip, XFS_ILOCK_SHARED);
636 if (!ip->i_update_core) {
638 * Timestamps/size haven't changed since last inode flush or
639 * inode transaction commit. That means either nothing got
640 * written or a transaction committed which caught the updates.
641 * If the latter happened and the transaction hasn't hit the
642 * disk yet, the inode will be still be pinned. If it is,
643 * force the log.
646 xfs_iunlock(ip, XFS_ILOCK_SHARED);
648 if (xfs_ipincount(ip)) {
649 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
650 XFS_LOG_FORCE | XFS_LOG_SYNC,
651 &log_flushed);
652 } else {
654 * If the inode is not pinned and nothing has changed
655 * we don't need to flush the cache.
657 changed = 0;
659 } else {
661 * Kick off a transaction to log the inode core to get the
662 * updates. The sync transaction will also force the log.
664 xfs_iunlock(ip, XFS_ILOCK_SHARED);
665 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
666 error = xfs_trans_reserve(tp, 0,
667 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
668 if (error) {
669 xfs_trans_cancel(tp, 0);
670 return error;
672 xfs_ilock(ip, XFS_ILOCK_EXCL);
675 * Note - it's possible that we might have pushed ourselves out
676 * of the way during trans_reserve which would flush the inode.
677 * But there's no guarantee that the inode buffer has actually
678 * gone out yet (it's delwri). Plus the buffer could be pinned
679 * anyway if it's part of an inode in another recent
680 * transaction. So we play it safe and fire off the
681 * transaction anyway.
683 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
684 xfs_trans_ihold(tp, ip);
685 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
686 xfs_trans_set_sync(tp);
687 error = _xfs_trans_commit(tp, 0, &log_flushed);
689 xfs_iunlock(ip, XFS_ILOCK_EXCL);
692 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
694 * If the log write didn't issue an ordered tag we need
695 * to flush the disk cache for the data device now.
697 if (!log_flushed)
698 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
701 * If this inode is on the RT dev we need to flush that
702 * cache as well.
704 if (XFS_IS_REALTIME_INODE(ip))
705 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
708 return error;
712 * Flags for xfs_free_eofblocks
714 #define XFS_FREE_EOF_TRYLOCK (1<<0)
717 * This is called by xfs_inactive to free any blocks beyond eof
718 * when the link count isn't zero and by xfs_dm_punch_hole() when
719 * punching a hole to EOF.
721 STATIC int
722 xfs_free_eofblocks(
723 xfs_mount_t *mp,
724 xfs_inode_t *ip,
725 int flags)
727 xfs_trans_t *tp;
728 int error;
729 xfs_fileoff_t end_fsb;
730 xfs_fileoff_t last_fsb;
731 xfs_filblks_t map_len;
732 int nimaps;
733 xfs_bmbt_irec_t imap;
736 * Figure out if there are any blocks beyond the end
737 * of the file. If not, then there is nothing to do.
739 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
740 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
741 map_len = last_fsb - end_fsb;
742 if (map_len <= 0)
743 return 0;
745 nimaps = 1;
746 xfs_ilock(ip, XFS_ILOCK_SHARED);
747 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
748 NULL, 0, &imap, &nimaps, NULL, NULL);
749 xfs_iunlock(ip, XFS_ILOCK_SHARED);
751 if (!error && (nimaps != 0) &&
752 (imap.br_startblock != HOLESTARTBLOCK ||
753 ip->i_delayed_blks)) {
755 * Attach the dquots to the inode up front.
757 error = xfs_qm_dqattach(ip, 0);
758 if (error)
759 return error;
762 * There are blocks after the end of file.
763 * Free them up now by truncating the file to
764 * its current size.
766 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
769 * Do the xfs_itruncate_start() call before
770 * reserving any log space because
771 * itruncate_start will call into the buffer
772 * cache and we can't
773 * do that within a transaction.
775 if (flags & XFS_FREE_EOF_TRYLOCK) {
776 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
777 xfs_trans_cancel(tp, 0);
778 return 0;
780 } else {
781 xfs_ilock(ip, XFS_IOLOCK_EXCL);
783 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
784 ip->i_size);
785 if (error) {
786 xfs_trans_cancel(tp, 0);
787 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
788 return error;
791 error = xfs_trans_reserve(tp, 0,
792 XFS_ITRUNCATE_LOG_RES(mp),
793 0, XFS_TRANS_PERM_LOG_RES,
794 XFS_ITRUNCATE_LOG_COUNT);
795 if (error) {
796 ASSERT(XFS_FORCED_SHUTDOWN(mp));
797 xfs_trans_cancel(tp, 0);
798 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
799 return error;
802 xfs_ilock(ip, XFS_ILOCK_EXCL);
803 xfs_trans_ijoin(tp, ip,
804 XFS_IOLOCK_EXCL |
805 XFS_ILOCK_EXCL);
806 xfs_trans_ihold(tp, ip);
808 error = xfs_itruncate_finish(&tp, ip,
809 ip->i_size,
810 XFS_DATA_FORK,
813 * If we get an error at this point we
814 * simply don't bother truncating the file.
816 if (error) {
817 xfs_trans_cancel(tp,
818 (XFS_TRANS_RELEASE_LOG_RES |
819 XFS_TRANS_ABORT));
820 } else {
821 error = xfs_trans_commit(tp,
822 XFS_TRANS_RELEASE_LOG_RES);
824 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
826 return error;
830 * Free a symlink that has blocks associated with it.
832 STATIC int
833 xfs_inactive_symlink_rmt(
834 xfs_inode_t *ip,
835 xfs_trans_t **tpp)
837 xfs_buf_t *bp;
838 int committed;
839 int done;
840 int error;
841 xfs_fsblock_t first_block;
842 xfs_bmap_free_t free_list;
843 int i;
844 xfs_mount_t *mp;
845 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
846 int nmaps;
847 xfs_trans_t *ntp;
848 int size;
849 xfs_trans_t *tp;
851 tp = *tpp;
852 mp = ip->i_mount;
853 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
855 * We're freeing a symlink that has some
856 * blocks allocated to it. Free the
857 * blocks here. We know that we've got
858 * either 1 or 2 extents and that we can
859 * free them all in one bunmapi call.
861 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
862 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
863 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
864 ASSERT(XFS_FORCED_SHUTDOWN(mp));
865 xfs_trans_cancel(tp, 0);
866 *tpp = NULL;
867 return error;
870 * Lock the inode, fix the size, and join it to the transaction.
871 * Hold it so in the normal path, we still have it locked for
872 * the second transaction. In the error paths we need it
873 * held so the cancel won't rele it, see below.
875 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
876 size = (int)ip->i_d.di_size;
877 ip->i_d.di_size = 0;
878 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
879 xfs_trans_ihold(tp, ip);
880 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
882 * Find the block(s) so we can inval and unmap them.
884 done = 0;
885 xfs_bmap_init(&free_list, &first_block);
886 nmaps = ARRAY_SIZE(mval);
887 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
888 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
889 &free_list, NULL)))
890 goto error0;
892 * Invalidate the block(s).
894 for (i = 0; i < nmaps; i++) {
895 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
896 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
897 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
898 xfs_trans_binval(tp, bp);
901 * Unmap the dead block(s) to the free_list.
903 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
904 &first_block, &free_list, NULL, &done)))
905 goto error1;
906 ASSERT(done);
908 * Commit the first transaction. This logs the EFI and the inode.
910 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
911 goto error1;
913 * The transaction must have been committed, since there were
914 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
915 * The new tp has the extent freeing and EFDs.
917 ASSERT(committed);
919 * The first xact was committed, so add the inode to the new one.
920 * Mark it dirty so it will be logged and moved forward in the log as
921 * part of every commit.
923 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
924 xfs_trans_ihold(tp, ip);
925 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
927 * Get a new, empty transaction to return to our caller.
929 ntp = xfs_trans_dup(tp);
931 * Commit the transaction containing extent freeing and EFDs.
932 * If we get an error on the commit here or on the reserve below,
933 * we need to unlock the inode since the new transaction doesn't
934 * have the inode attached.
936 error = xfs_trans_commit(tp, 0);
937 tp = ntp;
938 if (error) {
939 ASSERT(XFS_FORCED_SHUTDOWN(mp));
940 goto error0;
943 * transaction commit worked ok so we can drop the extra ticket
944 * reference that we gained in xfs_trans_dup()
946 xfs_log_ticket_put(tp->t_ticket);
949 * Remove the memory for extent descriptions (just bookkeeping).
951 if (ip->i_df.if_bytes)
952 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
953 ASSERT(ip->i_df.if_bytes == 0);
955 * Put an itruncate log reservation in the new transaction
956 * for our caller.
958 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
959 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
960 ASSERT(XFS_FORCED_SHUTDOWN(mp));
961 goto error0;
964 * Return with the inode locked but not joined to the transaction.
966 *tpp = tp;
967 return 0;
969 error1:
970 xfs_bmap_cancel(&free_list);
971 error0:
973 * Have to come here with the inode locked and either
974 * (held and in the transaction) or (not in the transaction).
975 * If the inode isn't held then cancel would iput it, but
976 * that's wrong since this is inactive and the vnode ref
977 * count is 0 already.
978 * Cancel won't do anything to the inode if held, but it still
979 * needs to be locked until the cancel is done, if it was
980 * joined to the transaction.
982 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
983 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
984 *tpp = NULL;
985 return error;
989 STATIC int
990 xfs_inactive_symlink_local(
991 xfs_inode_t *ip,
992 xfs_trans_t **tpp)
994 int error;
996 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
998 * We're freeing a symlink which fit into
999 * the inode. Just free the memory used
1000 * to hold the old symlink.
1002 error = xfs_trans_reserve(*tpp, 0,
1003 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1004 0, XFS_TRANS_PERM_LOG_RES,
1005 XFS_ITRUNCATE_LOG_COUNT);
1007 if (error) {
1008 xfs_trans_cancel(*tpp, 0);
1009 *tpp = NULL;
1010 return error;
1012 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1015 * Zero length symlinks _can_ exist.
1017 if (ip->i_df.if_bytes > 0) {
1018 xfs_idata_realloc(ip,
1019 -(ip->i_df.if_bytes),
1020 XFS_DATA_FORK);
1021 ASSERT(ip->i_df.if_bytes == 0);
1023 return 0;
1026 STATIC int
1027 xfs_inactive_attrs(
1028 xfs_inode_t *ip,
1029 xfs_trans_t **tpp)
1031 xfs_trans_t *tp;
1032 int error;
1033 xfs_mount_t *mp;
1035 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1036 tp = *tpp;
1037 mp = ip->i_mount;
1038 ASSERT(ip->i_d.di_forkoff != 0);
1039 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1040 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1041 if (error)
1042 goto error_unlock;
1044 error = xfs_attr_inactive(ip);
1045 if (error)
1046 goto error_unlock;
1048 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1049 error = xfs_trans_reserve(tp, 0,
1050 XFS_IFREE_LOG_RES(mp),
1051 0, XFS_TRANS_PERM_LOG_RES,
1052 XFS_INACTIVE_LOG_COUNT);
1053 if (error)
1054 goto error_cancel;
1056 xfs_ilock(ip, XFS_ILOCK_EXCL);
1057 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1058 xfs_trans_ihold(tp, ip);
1059 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1061 ASSERT(ip->i_d.di_anextents == 0);
1063 *tpp = tp;
1064 return 0;
1066 error_cancel:
1067 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1068 xfs_trans_cancel(tp, 0);
1069 error_unlock:
1070 *tpp = NULL;
1071 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1072 return error;
1076 xfs_release(
1077 xfs_inode_t *ip)
1079 xfs_mount_t *mp = ip->i_mount;
1080 int error;
1082 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1083 return 0;
1085 /* If this is a read-only mount, don't do this (would generate I/O) */
1086 if (mp->m_flags & XFS_MOUNT_RDONLY)
1087 return 0;
1089 if (!XFS_FORCED_SHUTDOWN(mp)) {
1090 int truncated;
1093 * If we are using filestreams, and we have an unlinked
1094 * file that we are processing the last close on, then nothing
1095 * will be able to reopen and write to this file. Purge this
1096 * inode from the filestreams cache so that it doesn't delay
1097 * teardown of the inode.
1099 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1100 xfs_filestream_deassociate(ip);
1103 * If we previously truncated this file and removed old data
1104 * in the process, we want to initiate "early" writeout on
1105 * the last close. This is an attempt to combat the notorious
1106 * NULL files problem which is particularly noticable from a
1107 * truncate down, buffered (re-)write (delalloc), followed by
1108 * a crash. What we are effectively doing here is
1109 * significantly reducing the time window where we'd otherwise
1110 * be exposed to that problem.
1112 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1113 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
1114 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1117 if (ip->i_d.di_nlink != 0) {
1118 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1119 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1120 ip->i_delayed_blks > 0)) &&
1121 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1122 (!(ip->i_d.di_flags &
1123 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1126 * If we can't get the iolock just skip truncating
1127 * the blocks past EOF because we could deadlock
1128 * with the mmap_sem otherwise. We'll get another
1129 * chance to drop them once the last reference to
1130 * the inode is dropped, so we'll never leak blocks
1131 * permanently.
1133 error = xfs_free_eofblocks(mp, ip,
1134 XFS_FREE_EOF_TRYLOCK);
1135 if (error)
1136 return error;
1140 return 0;
1144 * xfs_inactive
1146 * This is called when the vnode reference count for the vnode
1147 * goes to zero. If the file has been unlinked, then it must
1148 * now be truncated. Also, we clear all of the read-ahead state
1149 * kept for the inode here since the file is now closed.
1152 xfs_inactive(
1153 xfs_inode_t *ip)
1155 xfs_bmap_free_t free_list;
1156 xfs_fsblock_t first_block;
1157 int committed;
1158 xfs_trans_t *tp;
1159 xfs_mount_t *mp;
1160 int error;
1161 int truncate;
1163 xfs_itrace_entry(ip);
1166 * If the inode is already free, then there can be nothing
1167 * to clean up here.
1169 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
1170 ASSERT(ip->i_df.if_real_bytes == 0);
1171 ASSERT(ip->i_df.if_broot_bytes == 0);
1172 return VN_INACTIVE_CACHE;
1176 * Only do a truncate if it's a regular file with
1177 * some actual space in it. It's OK to look at the
1178 * inode's fields without the lock because we're the
1179 * only one with a reference to the inode.
1181 truncate = ((ip->i_d.di_nlink == 0) &&
1182 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1183 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1184 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1186 mp = ip->i_mount;
1188 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1189 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1191 error = 0;
1193 /* If this is a read-only mount, don't do this (would generate I/O) */
1194 if (mp->m_flags & XFS_MOUNT_RDONLY)
1195 goto out;
1197 if (ip->i_d.di_nlink != 0) {
1198 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1199 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1200 ip->i_delayed_blks > 0)) &&
1201 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1202 (!(ip->i_d.di_flags &
1203 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1204 (ip->i_delayed_blks != 0)))) {
1205 error = xfs_free_eofblocks(mp, ip, 0);
1206 if (error)
1207 return VN_INACTIVE_CACHE;
1209 goto out;
1212 ASSERT(ip->i_d.di_nlink == 0);
1214 error = xfs_qm_dqattach(ip, 0);
1215 if (error)
1216 return VN_INACTIVE_CACHE;
1218 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1219 if (truncate) {
1221 * Do the xfs_itruncate_start() call before
1222 * reserving any log space because itruncate_start
1223 * will call into the buffer cache and we can't
1224 * do that within a transaction.
1226 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1228 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1229 if (error) {
1230 xfs_trans_cancel(tp, 0);
1231 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1232 return VN_INACTIVE_CACHE;
1235 error = xfs_trans_reserve(tp, 0,
1236 XFS_ITRUNCATE_LOG_RES(mp),
1237 0, XFS_TRANS_PERM_LOG_RES,
1238 XFS_ITRUNCATE_LOG_COUNT);
1239 if (error) {
1240 /* Don't call itruncate_cleanup */
1241 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1242 xfs_trans_cancel(tp, 0);
1243 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1244 return VN_INACTIVE_CACHE;
1247 xfs_ilock(ip, XFS_ILOCK_EXCL);
1248 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1249 xfs_trans_ihold(tp, ip);
1252 * normally, we have to run xfs_itruncate_finish sync.
1253 * But if filesystem is wsync and we're in the inactive
1254 * path, then we know that nlink == 0, and that the
1255 * xaction that made nlink == 0 is permanently committed
1256 * since xfs_remove runs as a synchronous transaction.
1258 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1259 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1261 if (error) {
1262 xfs_trans_cancel(tp,
1263 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1264 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1265 return VN_INACTIVE_CACHE;
1267 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1270 * If we get an error while cleaning up a
1271 * symlink we bail out.
1273 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1274 xfs_inactive_symlink_rmt(ip, &tp) :
1275 xfs_inactive_symlink_local(ip, &tp);
1277 if (error) {
1278 ASSERT(tp == NULL);
1279 return VN_INACTIVE_CACHE;
1282 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1283 xfs_trans_ihold(tp, ip);
1284 } else {
1285 error = xfs_trans_reserve(tp, 0,
1286 XFS_IFREE_LOG_RES(mp),
1287 0, XFS_TRANS_PERM_LOG_RES,
1288 XFS_INACTIVE_LOG_COUNT);
1289 if (error) {
1290 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1291 xfs_trans_cancel(tp, 0);
1292 return VN_INACTIVE_CACHE;
1295 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1296 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1297 xfs_trans_ihold(tp, ip);
1301 * If there are attributes associated with the file
1302 * then blow them away now. The code calls a routine
1303 * that recursively deconstructs the attribute fork.
1304 * We need to just commit the current transaction
1305 * because we can't use it for xfs_attr_inactive().
1307 if (ip->i_d.di_anextents > 0) {
1308 error = xfs_inactive_attrs(ip, &tp);
1310 * If we got an error, the transaction is already
1311 * cancelled, and the inode is unlocked. Just get out.
1313 if (error)
1314 return VN_INACTIVE_CACHE;
1315 } else if (ip->i_afp) {
1316 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1320 * Free the inode.
1322 xfs_bmap_init(&free_list, &first_block);
1323 error = xfs_ifree(tp, ip, &free_list);
1324 if (error) {
1326 * If we fail to free the inode, shut down. The cancel
1327 * might do that, we need to make sure. Otherwise the
1328 * inode might be lost for a long time or forever.
1330 if (!XFS_FORCED_SHUTDOWN(mp)) {
1331 cmn_err(CE_NOTE,
1332 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1333 error, mp->m_fsname);
1334 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1336 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1337 } else {
1339 * Credit the quota account(s). The inode is gone.
1341 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1344 * Just ignore errors at this point. There is nothing we can
1345 * do except to try to keep going. Make sure it's not a silent
1346 * error.
1348 error = xfs_bmap_finish(&tp, &free_list, &committed);
1349 if (error)
1350 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1351 "xfs_bmap_finish() returned error %d", error);
1352 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1353 if (error)
1354 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1355 "xfs_trans_commit() returned error %d", error);
1359 * Release the dquots held by inode, if any.
1361 xfs_qm_dqdetach(ip);
1362 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1364 out:
1365 return VN_INACTIVE_CACHE;
1369 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1370 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1371 * ci_name->name will point to a the actual name (caller must free) or
1372 * will be set to NULL if an exact match is found.
1375 xfs_lookup(
1376 xfs_inode_t *dp,
1377 struct xfs_name *name,
1378 xfs_inode_t **ipp,
1379 struct xfs_name *ci_name)
1381 xfs_ino_t inum;
1382 int error;
1383 uint lock_mode;
1385 xfs_itrace_entry(dp);
1387 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1388 return XFS_ERROR(EIO);
1390 lock_mode = xfs_ilock_map_shared(dp);
1391 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1392 xfs_iunlock_map_shared(dp, lock_mode);
1394 if (error)
1395 goto out;
1397 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1398 if (error)
1399 goto out_free_name;
1401 xfs_itrace_ref(*ipp);
1402 return 0;
1404 out_free_name:
1405 if (ci_name)
1406 kmem_free(ci_name->name);
1407 out:
1408 *ipp = NULL;
1409 return error;
1413 xfs_create(
1414 xfs_inode_t *dp,
1415 struct xfs_name *name,
1416 mode_t mode,
1417 xfs_dev_t rdev,
1418 xfs_inode_t **ipp,
1419 cred_t *credp)
1421 int is_dir = S_ISDIR(mode);
1422 struct xfs_mount *mp = dp->i_mount;
1423 struct xfs_inode *ip = NULL;
1424 struct xfs_trans *tp = NULL;
1425 int error;
1426 xfs_bmap_free_t free_list;
1427 xfs_fsblock_t first_block;
1428 boolean_t unlock_dp_on_error = B_FALSE;
1429 uint cancel_flags;
1430 int committed;
1431 xfs_prid_t prid;
1432 struct xfs_dquot *udqp = NULL;
1433 struct xfs_dquot *gdqp = NULL;
1434 uint resblks;
1435 uint log_res;
1436 uint log_count;
1438 xfs_itrace_entry(dp);
1440 if (XFS_FORCED_SHUTDOWN(mp))
1441 return XFS_ERROR(EIO);
1443 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1444 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1445 dp, DM_RIGHT_NULL, NULL,
1446 DM_RIGHT_NULL, name->name, NULL,
1447 mode, 0, 0);
1449 if (error)
1450 return error;
1453 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1454 prid = dp->i_d.di_projid;
1455 else
1456 prid = dfltprid;
1459 * Make sure that we have allocated dquot(s) on disk.
1461 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1462 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1463 if (error)
1464 goto std_return;
1466 if (is_dir) {
1467 rdev = 0;
1468 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1469 log_res = XFS_MKDIR_LOG_RES(mp);
1470 log_count = XFS_MKDIR_LOG_COUNT;
1471 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1472 } else {
1473 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1474 log_res = XFS_CREATE_LOG_RES(mp);
1475 log_count = XFS_CREATE_LOG_COUNT;
1476 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1479 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1482 * Initially assume that the file does not exist and
1483 * reserve the resources for that case. If that is not
1484 * the case we'll drop the one we have and get a more
1485 * appropriate transaction later.
1487 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1488 XFS_TRANS_PERM_LOG_RES, log_count);
1489 if (error == ENOSPC) {
1490 /* flush outstanding delalloc blocks and retry */
1491 xfs_flush_inodes(dp);
1492 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1493 XFS_TRANS_PERM_LOG_RES, log_count);
1495 if (error == ENOSPC) {
1496 /* No space at all so try a "no-allocation" reservation */
1497 resblks = 0;
1498 error = xfs_trans_reserve(tp, 0, log_res, 0,
1499 XFS_TRANS_PERM_LOG_RES, log_count);
1501 if (error) {
1502 cancel_flags = 0;
1503 goto out_trans_cancel;
1506 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1507 unlock_dp_on_error = B_TRUE;
1510 * Check for directory link count overflow.
1512 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1513 error = XFS_ERROR(EMLINK);
1514 goto out_trans_cancel;
1517 xfs_bmap_init(&free_list, &first_block);
1520 * Reserve disk quota and the inode.
1522 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
1523 if (error)
1524 goto out_trans_cancel;
1526 error = xfs_dir_canenter(tp, dp, name, resblks);
1527 if (error)
1528 goto out_trans_cancel;
1531 * A newly created regular or special file just has one directory
1532 * entry pointing to them, but a directory also the "." entry
1533 * pointing to itself.
1535 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1536 prid, resblks > 0, &ip, &committed);
1537 if (error) {
1538 if (error == ENOSPC)
1539 goto out_trans_cancel;
1540 goto out_trans_abort;
1544 * At this point, we've gotten a newly allocated inode.
1545 * It is locked (and joined to the transaction).
1547 xfs_itrace_ref(ip);
1548 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1551 * Now we join the directory inode to the transaction. We do not do it
1552 * earlier because xfs_dir_ialloc might commit the previous transaction
1553 * (and release all the locks). An error from here on will result in
1554 * the transaction cancel unlocking dp so don't do it explicitly in the
1555 * error path.
1557 IHOLD(dp);
1558 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1559 unlock_dp_on_error = B_FALSE;
1561 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1562 &first_block, &free_list, resblks ?
1563 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1564 if (error) {
1565 ASSERT(error != ENOSPC);
1566 goto out_trans_abort;
1568 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1569 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1571 if (is_dir) {
1572 error = xfs_dir_init(tp, ip, dp);
1573 if (error)
1574 goto out_bmap_cancel;
1576 error = xfs_bumplink(tp, dp);
1577 if (error)
1578 goto out_bmap_cancel;
1582 * If this is a synchronous mount, make sure that the
1583 * create transaction goes to disk before returning to
1584 * the user.
1586 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1587 xfs_trans_set_sync(tp);
1590 * Attach the dquot(s) to the inodes and modify them incore.
1591 * These ids of the inode couldn't have changed since the new
1592 * inode has been locked ever since it was created.
1594 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
1597 * xfs_trans_commit normally decrements the vnode ref count
1598 * when it unlocks the inode. Since we want to return the
1599 * vnode to the caller, we bump the vnode ref count now.
1601 IHOLD(ip);
1603 error = xfs_bmap_finish(&tp, &free_list, &committed);
1604 if (error)
1605 goto out_abort_rele;
1607 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1608 if (error) {
1609 IRELE(ip);
1610 goto out_dqrele;
1613 xfs_qm_dqrele(udqp);
1614 xfs_qm_dqrele(gdqp);
1616 *ipp = ip;
1618 /* Fallthrough to std_return with error = 0 */
1619 std_return:
1620 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1621 XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
1622 ip, DM_RIGHT_NULL, name->name, NULL, mode,
1623 error, 0);
1626 return error;
1628 out_bmap_cancel:
1629 xfs_bmap_cancel(&free_list);
1630 out_trans_abort:
1631 cancel_flags |= XFS_TRANS_ABORT;
1632 out_trans_cancel:
1633 xfs_trans_cancel(tp, cancel_flags);
1634 out_dqrele:
1635 xfs_qm_dqrele(udqp);
1636 xfs_qm_dqrele(gdqp);
1638 if (unlock_dp_on_error)
1639 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1641 goto std_return;
1643 out_abort_rele:
1645 * Wait until after the current transaction is aborted to
1646 * release the inode. This prevents recursive transactions
1647 * and deadlocks from xfs_inactive.
1649 xfs_bmap_cancel(&free_list);
1650 cancel_flags |= XFS_TRANS_ABORT;
1651 xfs_trans_cancel(tp, cancel_flags);
1652 IRELE(ip);
1653 unlock_dp_on_error = B_FALSE;
1654 goto out_dqrele;
1657 #ifdef DEBUG
1658 int xfs_locked_n;
1659 int xfs_small_retries;
1660 int xfs_middle_retries;
1661 int xfs_lots_retries;
1662 int xfs_lock_delays;
1663 #endif
1666 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1667 * a different value
1669 static inline int
1670 xfs_lock_inumorder(int lock_mode, int subclass)
1672 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1673 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1674 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1675 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1677 return lock_mode;
1681 * The following routine will lock n inodes in exclusive mode.
1682 * We assume the caller calls us with the inodes in i_ino order.
1684 * We need to detect deadlock where an inode that we lock
1685 * is in the AIL and we start waiting for another inode that is locked
1686 * by a thread in a long running transaction (such as truncate). This can
1687 * result in deadlock since the long running trans might need to wait
1688 * for the inode we just locked in order to push the tail and free space
1689 * in the log.
1691 void
1692 xfs_lock_inodes(
1693 xfs_inode_t **ips,
1694 int inodes,
1695 uint lock_mode)
1697 int attempts = 0, i, j, try_lock;
1698 xfs_log_item_t *lp;
1700 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1702 try_lock = 0;
1703 i = 0;
1705 again:
1706 for (; i < inodes; i++) {
1707 ASSERT(ips[i]);
1709 if (i && (ips[i] == ips[i-1])) /* Already locked */
1710 continue;
1713 * If try_lock is not set yet, make sure all locked inodes
1714 * are not in the AIL.
1715 * If any are, set try_lock to be used later.
1718 if (!try_lock) {
1719 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1720 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1721 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1722 try_lock++;
1728 * If any of the previous locks we have locked is in the AIL,
1729 * we must TRY to get the second and subsequent locks. If
1730 * we can't get any, we must release all we have
1731 * and try again.
1734 if (try_lock) {
1735 /* try_lock must be 0 if i is 0. */
1737 * try_lock means we have an inode locked
1738 * that is in the AIL.
1740 ASSERT(i != 0);
1741 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1742 attempts++;
1745 * Unlock all previous guys and try again.
1746 * xfs_iunlock will try to push the tail
1747 * if the inode is in the AIL.
1750 for(j = i - 1; j >= 0; j--) {
1753 * Check to see if we've already
1754 * unlocked this one.
1755 * Not the first one going back,
1756 * and the inode ptr is the same.
1758 if ((j != (i - 1)) && ips[j] ==
1759 ips[j+1])
1760 continue;
1762 xfs_iunlock(ips[j], lock_mode);
1765 if ((attempts % 5) == 0) {
1766 delay(1); /* Don't just spin the CPU */
1767 #ifdef DEBUG
1768 xfs_lock_delays++;
1769 #endif
1771 i = 0;
1772 try_lock = 0;
1773 goto again;
1775 } else {
1776 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1780 #ifdef DEBUG
1781 if (attempts) {
1782 if (attempts < 5) xfs_small_retries++;
1783 else if (attempts < 100) xfs_middle_retries++;
1784 else xfs_lots_retries++;
1785 } else {
1786 xfs_locked_n++;
1788 #endif
1792 * xfs_lock_two_inodes() can only be used to lock one type of lock
1793 * at a time - the iolock or the ilock, but not both at once. If
1794 * we lock both at once, lockdep will report false positives saying
1795 * we have violated locking orders.
1797 void
1798 xfs_lock_two_inodes(
1799 xfs_inode_t *ip0,
1800 xfs_inode_t *ip1,
1801 uint lock_mode)
1803 xfs_inode_t *temp;
1804 int attempts = 0;
1805 xfs_log_item_t *lp;
1807 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1808 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1809 ASSERT(ip0->i_ino != ip1->i_ino);
1811 if (ip0->i_ino > ip1->i_ino) {
1812 temp = ip0;
1813 ip0 = ip1;
1814 ip1 = temp;
1817 again:
1818 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1821 * If the first lock we have locked is in the AIL, we must TRY to get
1822 * the second lock. If we can't get it, we must release the first one
1823 * and try again.
1825 lp = (xfs_log_item_t *)ip0->i_itemp;
1826 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1827 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1828 xfs_iunlock(ip0, lock_mode);
1829 if ((++attempts % 5) == 0)
1830 delay(1); /* Don't just spin the CPU */
1831 goto again;
1833 } else {
1834 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1839 xfs_remove(
1840 xfs_inode_t *dp,
1841 struct xfs_name *name,
1842 xfs_inode_t *ip)
1844 xfs_mount_t *mp = dp->i_mount;
1845 xfs_trans_t *tp = NULL;
1846 int is_dir = S_ISDIR(ip->i_d.di_mode);
1847 int error = 0;
1848 xfs_bmap_free_t free_list;
1849 xfs_fsblock_t first_block;
1850 int cancel_flags;
1851 int committed;
1852 int link_zero;
1853 uint resblks;
1854 uint log_count;
1856 xfs_itrace_entry(dp);
1857 xfs_itrace_entry(ip);
1859 if (XFS_FORCED_SHUTDOWN(mp))
1860 return XFS_ERROR(EIO);
1862 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
1863 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1864 NULL, DM_RIGHT_NULL, name->name, NULL,
1865 ip->i_d.di_mode, 0, 0);
1866 if (error)
1867 return error;
1870 error = xfs_qm_dqattach(dp, 0);
1871 if (error)
1872 goto std_return;
1874 error = xfs_qm_dqattach(ip, 0);
1875 if (error)
1876 goto std_return;
1878 if (is_dir) {
1879 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1880 log_count = XFS_DEFAULT_LOG_COUNT;
1881 } else {
1882 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1883 log_count = XFS_REMOVE_LOG_COUNT;
1885 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1888 * We try to get the real space reservation first,
1889 * allowing for directory btree deletion(s) implying
1890 * possible bmap insert(s). If we can't get the space
1891 * reservation then we use 0 instead, and avoid the bmap
1892 * btree insert(s) in the directory code by, if the bmap
1893 * insert tries to happen, instead trimming the LAST
1894 * block from the directory.
1896 resblks = XFS_REMOVE_SPACE_RES(mp);
1897 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1898 XFS_TRANS_PERM_LOG_RES, log_count);
1899 if (error == ENOSPC) {
1900 resblks = 0;
1901 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1902 XFS_TRANS_PERM_LOG_RES, log_count);
1904 if (error) {
1905 ASSERT(error != ENOSPC);
1906 cancel_flags = 0;
1907 goto out_trans_cancel;
1910 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1913 * At this point, we've gotten both the directory and the entry
1914 * inodes locked.
1916 IHOLD(ip);
1917 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1919 IHOLD(dp);
1920 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1923 * If we're removing a directory perform some additional validation.
1925 if (is_dir) {
1926 ASSERT(ip->i_d.di_nlink >= 2);
1927 if (ip->i_d.di_nlink != 2) {
1928 error = XFS_ERROR(ENOTEMPTY);
1929 goto out_trans_cancel;
1931 if (!xfs_dir_isempty(ip)) {
1932 error = XFS_ERROR(ENOTEMPTY);
1933 goto out_trans_cancel;
1937 xfs_bmap_init(&free_list, &first_block);
1938 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1939 &first_block, &free_list, resblks);
1940 if (error) {
1941 ASSERT(error != ENOENT);
1942 goto out_bmap_cancel;
1944 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1946 if (is_dir) {
1948 * Drop the link from ip's "..".
1950 error = xfs_droplink(tp, dp);
1951 if (error)
1952 goto out_bmap_cancel;
1955 * Drop the "." link from ip to self.
1957 error = xfs_droplink(tp, ip);
1958 if (error)
1959 goto out_bmap_cancel;
1960 } else {
1962 * When removing a non-directory we need to log the parent
1963 * inode here. For a directory this is done implicitly
1964 * by the xfs_droplink call for the ".." entry.
1966 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1970 * Drop the link from dp to ip.
1972 error = xfs_droplink(tp, ip);
1973 if (error)
1974 goto out_bmap_cancel;
1977 * Determine if this is the last link while
1978 * we are in the transaction.
1980 link_zero = (ip->i_d.di_nlink == 0);
1983 * If this is a synchronous mount, make sure that the
1984 * remove transaction goes to disk before returning to
1985 * the user.
1987 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1988 xfs_trans_set_sync(tp);
1990 error = xfs_bmap_finish(&tp, &free_list, &committed);
1991 if (error)
1992 goto out_bmap_cancel;
1994 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1995 if (error)
1996 goto std_return;
1999 * If we are using filestreams, kill the stream association.
2000 * If the file is still open it may get a new one but that
2001 * will get killed on last close in xfs_close() so we don't
2002 * have to worry about that.
2004 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2005 xfs_filestream_deassociate(ip);
2007 xfs_itrace_exit(ip);
2008 xfs_itrace_exit(dp);
2010 std_return:
2011 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2012 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2013 NULL, DM_RIGHT_NULL, name->name, NULL,
2014 ip->i_d.di_mode, error, 0);
2017 return error;
2019 out_bmap_cancel:
2020 xfs_bmap_cancel(&free_list);
2021 cancel_flags |= XFS_TRANS_ABORT;
2022 out_trans_cancel:
2023 xfs_trans_cancel(tp, cancel_flags);
2024 goto std_return;
2028 xfs_link(
2029 xfs_inode_t *tdp,
2030 xfs_inode_t *sip,
2031 struct xfs_name *target_name)
2033 xfs_mount_t *mp = tdp->i_mount;
2034 xfs_trans_t *tp;
2035 int error;
2036 xfs_bmap_free_t free_list;
2037 xfs_fsblock_t first_block;
2038 int cancel_flags;
2039 int committed;
2040 int resblks;
2042 xfs_itrace_entry(tdp);
2043 xfs_itrace_entry(sip);
2045 ASSERT(!S_ISDIR(sip->i_d.di_mode));
2047 if (XFS_FORCED_SHUTDOWN(mp))
2048 return XFS_ERROR(EIO);
2050 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2051 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2052 tdp, DM_RIGHT_NULL,
2053 sip, DM_RIGHT_NULL,
2054 target_name->name, NULL, 0, 0, 0);
2055 if (error)
2056 return error;
2059 /* Return through std_return after this point. */
2061 error = xfs_qm_dqattach(sip, 0);
2062 if (error)
2063 goto std_return;
2065 error = xfs_qm_dqattach(tdp, 0);
2066 if (error)
2067 goto std_return;
2069 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2070 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2071 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
2072 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2073 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2074 if (error == ENOSPC) {
2075 resblks = 0;
2076 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2077 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2079 if (error) {
2080 cancel_flags = 0;
2081 goto error_return;
2084 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
2087 * Increment vnode ref counts since xfs_trans_commit &
2088 * xfs_trans_cancel will both unlock the inodes and
2089 * decrement the associated ref counts.
2091 IHOLD(sip);
2092 IHOLD(tdp);
2093 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2094 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2097 * If the source has too many links, we can't make any more to it.
2099 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2100 error = XFS_ERROR(EMLINK);
2101 goto error_return;
2105 * If we are using project inheritance, we only allow hard link
2106 * creation in our tree when the project IDs are the same; else
2107 * the tree quota mechanism could be circumvented.
2109 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2110 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2111 error = XFS_ERROR(EXDEV);
2112 goto error_return;
2115 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2116 if (error)
2117 goto error_return;
2119 xfs_bmap_init(&free_list, &first_block);
2121 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2122 &first_block, &free_list, resblks);
2123 if (error)
2124 goto abort_return;
2125 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2126 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2128 error = xfs_bumplink(tp, sip);
2129 if (error)
2130 goto abort_return;
2133 * If this is a synchronous mount, make sure that the
2134 * link transaction goes to disk before returning to
2135 * the user.
2137 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2138 xfs_trans_set_sync(tp);
2141 error = xfs_bmap_finish (&tp, &free_list, &committed);
2142 if (error) {
2143 xfs_bmap_cancel(&free_list);
2144 goto abort_return;
2147 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2148 if (error)
2149 goto std_return;
2151 /* Fall through to std_return with error = 0. */
2152 std_return:
2153 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2154 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2155 tdp, DM_RIGHT_NULL,
2156 sip, DM_RIGHT_NULL,
2157 target_name->name, NULL, 0, error, 0);
2159 return error;
2161 abort_return:
2162 cancel_flags |= XFS_TRANS_ABORT;
2163 /* FALLTHROUGH */
2165 error_return:
2166 xfs_trans_cancel(tp, cancel_flags);
2167 goto std_return;
2171 xfs_symlink(
2172 xfs_inode_t *dp,
2173 struct xfs_name *link_name,
2174 const char *target_path,
2175 mode_t mode,
2176 xfs_inode_t **ipp,
2177 cred_t *credp)
2179 xfs_mount_t *mp = dp->i_mount;
2180 xfs_trans_t *tp;
2181 xfs_inode_t *ip;
2182 int error;
2183 int pathlen;
2184 xfs_bmap_free_t free_list;
2185 xfs_fsblock_t first_block;
2186 boolean_t unlock_dp_on_error = B_FALSE;
2187 uint cancel_flags;
2188 int committed;
2189 xfs_fileoff_t first_fsb;
2190 xfs_filblks_t fs_blocks;
2191 int nmaps;
2192 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2193 xfs_daddr_t d;
2194 const char *cur_chunk;
2195 int byte_cnt;
2196 int n;
2197 xfs_buf_t *bp;
2198 xfs_prid_t prid;
2199 struct xfs_dquot *udqp, *gdqp;
2200 uint resblks;
2202 *ipp = NULL;
2203 error = 0;
2204 ip = NULL;
2205 tp = NULL;
2207 xfs_itrace_entry(dp);
2209 if (XFS_FORCED_SHUTDOWN(mp))
2210 return XFS_ERROR(EIO);
2213 * Check component lengths of the target path name.
2215 pathlen = strlen(target_path);
2216 if (pathlen >= MAXPATHLEN) /* total string too long */
2217 return XFS_ERROR(ENAMETOOLONG);
2219 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
2220 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
2221 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2222 link_name->name, target_path, 0, 0, 0);
2223 if (error)
2224 return error;
2227 /* Return through std_return after this point. */
2229 udqp = gdqp = NULL;
2230 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2231 prid = dp->i_d.di_projid;
2232 else
2233 prid = (xfs_prid_t)dfltprid;
2236 * Make sure that we have allocated dquot(s) on disk.
2238 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
2239 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2240 if (error)
2241 goto std_return;
2243 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2244 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2246 * The symlink will fit into the inode data fork?
2247 * There can't be any attributes so we get the whole variable part.
2249 if (pathlen <= XFS_LITINO(mp))
2250 fs_blocks = 0;
2251 else
2252 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2253 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2254 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2255 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2256 if (error == ENOSPC && fs_blocks == 0) {
2257 resblks = 0;
2258 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2259 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2261 if (error) {
2262 cancel_flags = 0;
2263 goto error_return;
2266 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2267 unlock_dp_on_error = B_TRUE;
2270 * Check whether the directory allows new symlinks or not.
2272 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2273 error = XFS_ERROR(EPERM);
2274 goto error_return;
2278 * Reserve disk quota : blocks and inode.
2280 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
2281 if (error)
2282 goto error_return;
2285 * Check for ability to enter directory entry, if no space reserved.
2287 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2288 if (error)
2289 goto error_return;
2291 * Initialize the bmap freelist prior to calling either
2292 * bmapi or the directory create code.
2294 xfs_bmap_init(&free_list, &first_block);
2297 * Allocate an inode for the symlink.
2299 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
2300 1, 0, credp, prid, resblks > 0, &ip, NULL);
2301 if (error) {
2302 if (error == ENOSPC)
2303 goto error_return;
2304 goto error1;
2306 xfs_itrace_ref(ip);
2309 * An error after we've joined dp to the transaction will result in the
2310 * transaction cancel unlocking dp so don't do it explicitly in the
2311 * error path.
2313 IHOLD(dp);
2314 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2315 unlock_dp_on_error = B_FALSE;
2318 * Also attach the dquot(s) to it, if applicable.
2320 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
2322 if (resblks)
2323 resblks -= XFS_IALLOC_SPACE_RES(mp);
2325 * If the symlink will fit into the inode, write it inline.
2327 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2328 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2329 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2330 ip->i_d.di_size = pathlen;
2333 * The inode was initially created in extent format.
2335 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2336 ip->i_df.if_flags |= XFS_IFINLINE;
2338 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2339 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2341 } else {
2342 first_fsb = 0;
2343 nmaps = SYMLINK_MAPS;
2345 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2346 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2347 &first_block, resblks, mval, &nmaps,
2348 &free_list, NULL);
2349 if (error) {
2350 goto error1;
2353 if (resblks)
2354 resblks -= fs_blocks;
2355 ip->i_d.di_size = pathlen;
2356 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2358 cur_chunk = target_path;
2359 for (n = 0; n < nmaps; n++) {
2360 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2361 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2362 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2363 BTOBB(byte_cnt), 0);
2364 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2365 if (pathlen < byte_cnt) {
2366 byte_cnt = pathlen;
2368 pathlen -= byte_cnt;
2370 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2371 cur_chunk += byte_cnt;
2373 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2378 * Create the directory entry for the symlink.
2380 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2381 &first_block, &free_list, resblks);
2382 if (error)
2383 goto error1;
2384 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2385 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2388 * If this is a synchronous mount, make sure that the
2389 * symlink transaction goes to disk before returning to
2390 * the user.
2392 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2393 xfs_trans_set_sync(tp);
2397 * xfs_trans_commit normally decrements the vnode ref count
2398 * when it unlocks the inode. Since we want to return the
2399 * vnode to the caller, we bump the vnode ref count now.
2401 IHOLD(ip);
2403 error = xfs_bmap_finish(&tp, &free_list, &committed);
2404 if (error) {
2405 goto error2;
2407 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2408 xfs_qm_dqrele(udqp);
2409 xfs_qm_dqrele(gdqp);
2411 /* Fall through to std_return with error = 0 or errno from
2412 * xfs_trans_commit */
2413 std_return:
2414 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
2415 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
2416 dp, DM_RIGHT_NULL,
2417 error ? NULL : ip,
2418 DM_RIGHT_NULL, link_name->name,
2419 target_path, 0, error, 0);
2422 if (!error)
2423 *ipp = ip;
2424 return error;
2426 error2:
2427 IRELE(ip);
2428 error1:
2429 xfs_bmap_cancel(&free_list);
2430 cancel_flags |= XFS_TRANS_ABORT;
2431 error_return:
2432 xfs_trans_cancel(tp, cancel_flags);
2433 xfs_qm_dqrele(udqp);
2434 xfs_qm_dqrele(gdqp);
2436 if (unlock_dp_on_error)
2437 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2439 goto std_return;
2443 xfs_set_dmattrs(
2444 xfs_inode_t *ip,
2445 u_int evmask,
2446 u_int16_t state)
2448 xfs_mount_t *mp = ip->i_mount;
2449 xfs_trans_t *tp;
2450 int error;
2452 if (!capable(CAP_SYS_ADMIN))
2453 return XFS_ERROR(EPERM);
2455 if (XFS_FORCED_SHUTDOWN(mp))
2456 return XFS_ERROR(EIO);
2458 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2459 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2460 if (error) {
2461 xfs_trans_cancel(tp, 0);
2462 return error;
2464 xfs_ilock(ip, XFS_ILOCK_EXCL);
2465 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2467 ip->i_d.di_dmevmask = evmask;
2468 ip->i_d.di_dmstate = state;
2470 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2471 IHOLD(ip);
2472 error = xfs_trans_commit(tp, 0);
2474 return error;
2478 * xfs_alloc_file_space()
2479 * This routine allocates disk space for the given file.
2481 * If alloc_type == 0, this request is for an ALLOCSP type
2482 * request which will change the file size. In this case, no
2483 * DMAPI event will be generated by the call. A TRUNCATE event
2484 * will be generated later by xfs_setattr.
2486 * If alloc_type != 0, this request is for a RESVSP type
2487 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2488 * lower block boundary byte address is less than the file's
2489 * length.
2491 * RETURNS:
2492 * 0 on success
2493 * errno on error
2496 STATIC int
2497 xfs_alloc_file_space(
2498 xfs_inode_t *ip,
2499 xfs_off_t offset,
2500 xfs_off_t len,
2501 int alloc_type,
2502 int attr_flags)
2504 xfs_mount_t *mp = ip->i_mount;
2505 xfs_off_t count;
2506 xfs_filblks_t allocated_fsb;
2507 xfs_filblks_t allocatesize_fsb;
2508 xfs_extlen_t extsz, temp;
2509 xfs_fileoff_t startoffset_fsb;
2510 xfs_fsblock_t firstfsb;
2511 int nimaps;
2512 int bmapi_flag;
2513 int quota_flag;
2514 int rt;
2515 xfs_trans_t *tp;
2516 xfs_bmbt_irec_t imaps[1], *imapp;
2517 xfs_bmap_free_t free_list;
2518 uint qblocks, resblks, resrtextents;
2519 int committed;
2520 int error;
2522 xfs_itrace_entry(ip);
2524 if (XFS_FORCED_SHUTDOWN(mp))
2525 return XFS_ERROR(EIO);
2527 error = xfs_qm_dqattach(ip, 0);
2528 if (error)
2529 return error;
2531 if (len <= 0)
2532 return XFS_ERROR(EINVAL);
2534 rt = XFS_IS_REALTIME_INODE(ip);
2535 extsz = xfs_get_extsz_hint(ip);
2537 count = len;
2538 imapp = &imaps[0];
2539 nimaps = 1;
2540 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
2541 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2542 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2544 /* Generate a DMAPI event if needed. */
2545 if (alloc_type != 0 && offset < ip->i_size &&
2546 (attr_flags & XFS_ATTR_DMI) == 0 &&
2547 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2548 xfs_off_t end_dmi_offset;
2550 end_dmi_offset = offset+len;
2551 if (end_dmi_offset > ip->i_size)
2552 end_dmi_offset = ip->i_size;
2553 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2554 end_dmi_offset - offset, 0, NULL);
2555 if (error)
2556 return error;
2560 * Allocate file space until done or until there is an error
2562 retry:
2563 while (allocatesize_fsb && !error) {
2564 xfs_fileoff_t s, e;
2567 * Determine space reservations for data/realtime.
2569 if (unlikely(extsz)) {
2570 s = startoffset_fsb;
2571 do_div(s, extsz);
2572 s *= extsz;
2573 e = startoffset_fsb + allocatesize_fsb;
2574 if ((temp = do_mod(startoffset_fsb, extsz)))
2575 e += temp;
2576 if ((temp = do_mod(e, extsz)))
2577 e += extsz - temp;
2578 } else {
2579 s = 0;
2580 e = allocatesize_fsb;
2583 if (unlikely(rt)) {
2584 resrtextents = qblocks = (uint)(e - s);
2585 resrtextents /= mp->m_sb.sb_rextsize;
2586 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2587 quota_flag = XFS_QMOPT_RES_RTBLKS;
2588 } else {
2589 resrtextents = 0;
2590 resblks = qblocks = \
2591 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2592 quota_flag = XFS_QMOPT_RES_REGBLKS;
2596 * Allocate and setup the transaction.
2598 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2599 error = xfs_trans_reserve(tp, resblks,
2600 XFS_WRITE_LOG_RES(mp), resrtextents,
2601 XFS_TRANS_PERM_LOG_RES,
2602 XFS_WRITE_LOG_COUNT);
2604 * Check for running out of space
2606 if (error) {
2608 * Free the transaction structure.
2610 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2611 xfs_trans_cancel(tp, 0);
2612 break;
2614 xfs_ilock(ip, XFS_ILOCK_EXCL);
2615 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2616 0, quota_flag);
2617 if (error)
2618 goto error1;
2620 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2621 xfs_trans_ihold(tp, ip);
2624 * Issue the xfs_bmapi() call to allocate the blocks
2626 xfs_bmap_init(&free_list, &firstfsb);
2627 error = xfs_bmapi(tp, ip, startoffset_fsb,
2628 allocatesize_fsb, bmapi_flag,
2629 &firstfsb, 0, imapp, &nimaps,
2630 &free_list, NULL);
2631 if (error) {
2632 goto error0;
2636 * Complete the transaction
2638 error = xfs_bmap_finish(&tp, &free_list, &committed);
2639 if (error) {
2640 goto error0;
2643 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2644 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2645 if (error) {
2646 break;
2649 allocated_fsb = imapp->br_blockcount;
2651 if (nimaps == 0) {
2652 error = XFS_ERROR(ENOSPC);
2653 break;
2656 startoffset_fsb += allocated_fsb;
2657 allocatesize_fsb -= allocated_fsb;
2659 dmapi_enospc_check:
2660 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
2661 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
2662 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
2663 ip, DM_RIGHT_NULL,
2664 ip, DM_RIGHT_NULL,
2665 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
2666 if (error == 0)
2667 goto retry; /* Maybe DMAPI app. has made space */
2668 /* else fall through with error from XFS_SEND_DATA */
2671 return error;
2673 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
2674 xfs_bmap_cancel(&free_list);
2675 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
2677 error1: /* Just cancel transaction */
2678 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2679 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2680 goto dmapi_enospc_check;
2684 * Zero file bytes between startoff and endoff inclusive.
2685 * The iolock is held exclusive and no blocks are buffered.
2687 * This function is used by xfs_free_file_space() to zero
2688 * partial blocks when the range to free is not block aligned.
2689 * When unreserving space with boundaries that are not block
2690 * aligned we round up the start and round down the end
2691 * boundaries and then use this function to zero the parts of
2692 * the blocks that got dropped during the rounding.
2694 STATIC int
2695 xfs_zero_remaining_bytes(
2696 xfs_inode_t *ip,
2697 xfs_off_t startoff,
2698 xfs_off_t endoff)
2700 xfs_bmbt_irec_t imap;
2701 xfs_fileoff_t offset_fsb;
2702 xfs_off_t lastoffset;
2703 xfs_off_t offset;
2704 xfs_buf_t *bp;
2705 xfs_mount_t *mp = ip->i_mount;
2706 int nimap;
2707 int error = 0;
2710 * Avoid doing I/O beyond eof - it's not necessary
2711 * since nothing can read beyond eof. The space will
2712 * be zeroed when the file is extended anyway.
2714 if (startoff >= ip->i_size)
2715 return 0;
2717 if (endoff > ip->i_size)
2718 endoff = ip->i_size;
2720 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
2721 XFS_IS_REALTIME_INODE(ip) ?
2722 mp->m_rtdev_targp : mp->m_ddev_targp);
2723 if (!bp)
2724 return XFS_ERROR(ENOMEM);
2726 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2727 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2728 nimap = 1;
2729 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
2730 NULL, 0, &imap, &nimap, NULL, NULL);
2731 if (error || nimap < 1)
2732 break;
2733 ASSERT(imap.br_blockcount >= 1);
2734 ASSERT(imap.br_startoff == offset_fsb);
2735 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2736 if (lastoffset > endoff)
2737 lastoffset = endoff;
2738 if (imap.br_startblock == HOLESTARTBLOCK)
2739 continue;
2740 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2741 if (imap.br_state == XFS_EXT_UNWRITTEN)
2742 continue;
2743 XFS_BUF_UNDONE(bp);
2744 XFS_BUF_UNWRITE(bp);
2745 XFS_BUF_READ(bp);
2746 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
2747 xfsbdstrat(mp, bp);
2748 error = xfs_iowait(bp);
2749 if (error) {
2750 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2751 mp, bp, XFS_BUF_ADDR(bp));
2752 break;
2754 memset(XFS_BUF_PTR(bp) +
2755 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2756 0, lastoffset - offset + 1);
2757 XFS_BUF_UNDONE(bp);
2758 XFS_BUF_UNREAD(bp);
2759 XFS_BUF_WRITE(bp);
2760 xfsbdstrat(mp, bp);
2761 error = xfs_iowait(bp);
2762 if (error) {
2763 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2764 mp, bp, XFS_BUF_ADDR(bp));
2765 break;
2768 xfs_buf_free(bp);
2769 return error;
2773 * xfs_free_file_space()
2774 * This routine frees disk space for the given file.
2776 * This routine is only called by xfs_change_file_space
2777 * for an UNRESVSP type call.
2779 * RETURNS:
2780 * 0 on success
2781 * errno on error
2784 STATIC int
2785 xfs_free_file_space(
2786 xfs_inode_t *ip,
2787 xfs_off_t offset,
2788 xfs_off_t len,
2789 int attr_flags)
2791 int committed;
2792 int done;
2793 xfs_off_t end_dmi_offset;
2794 xfs_fileoff_t endoffset_fsb;
2795 int error;
2796 xfs_fsblock_t firstfsb;
2797 xfs_bmap_free_t free_list;
2798 xfs_bmbt_irec_t imap;
2799 xfs_off_t ioffset;
2800 xfs_extlen_t mod=0;
2801 xfs_mount_t *mp;
2802 int nimap;
2803 uint resblks;
2804 uint rounding;
2805 int rt;
2806 xfs_fileoff_t startoffset_fsb;
2807 xfs_trans_t *tp;
2808 int need_iolock = 1;
2810 mp = ip->i_mount;
2812 xfs_itrace_entry(ip);
2814 error = xfs_qm_dqattach(ip, 0);
2815 if (error)
2816 return error;
2818 error = 0;
2819 if (len <= 0) /* if nothing being freed */
2820 return error;
2821 rt = XFS_IS_REALTIME_INODE(ip);
2822 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2823 end_dmi_offset = offset + len;
2824 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
2826 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
2827 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2828 if (end_dmi_offset > ip->i_size)
2829 end_dmi_offset = ip->i_size;
2830 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
2831 offset, end_dmi_offset - offset,
2832 AT_DELAY_FLAG(attr_flags), NULL);
2833 if (error)
2834 return error;
2837 if (attr_flags & XFS_ATTR_NOLOCK)
2838 need_iolock = 0;
2839 if (need_iolock) {
2840 xfs_ilock(ip, XFS_IOLOCK_EXCL);
2841 /* wait for the completion of any pending DIOs */
2842 xfs_ioend_wait(ip);
2845 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
2846 ioffset = offset & ~(rounding - 1);
2848 if (VN_CACHED(VFS_I(ip)) != 0) {
2849 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
2850 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
2851 if (error)
2852 goto out_unlock_iolock;
2856 * Need to zero the stuff we're not freeing, on disk.
2857 * If it's a realtime file & can't use unwritten extents then we
2858 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2859 * will take care of it for us.
2861 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
2862 nimap = 1;
2863 error = xfs_bmapi(NULL, ip, startoffset_fsb,
2864 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
2865 if (error)
2866 goto out_unlock_iolock;
2867 ASSERT(nimap == 0 || nimap == 1);
2868 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2869 xfs_daddr_t block;
2871 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2872 block = imap.br_startblock;
2873 mod = do_div(block, mp->m_sb.sb_rextsize);
2874 if (mod)
2875 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2877 nimap = 1;
2878 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
2879 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
2880 if (error)
2881 goto out_unlock_iolock;
2882 ASSERT(nimap == 0 || nimap == 1);
2883 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2884 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2885 mod++;
2886 if (mod && (mod != mp->m_sb.sb_rextsize))
2887 endoffset_fsb -= mod;
2890 if ((done = (endoffset_fsb <= startoffset_fsb)))
2892 * One contiguous piece to clear
2894 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2895 else {
2897 * Some full blocks, possibly two pieces to clear
2899 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2900 error = xfs_zero_remaining_bytes(ip, offset,
2901 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2902 if (!error &&
2903 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2904 error = xfs_zero_remaining_bytes(ip,
2905 XFS_FSB_TO_B(mp, endoffset_fsb),
2906 offset + len - 1);
2910 * free file space until done or until there is an error
2912 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2913 while (!error && !done) {
2916 * allocate and setup the transaction. Allow this
2917 * transaction to dip into the reserve blocks to ensure
2918 * the freeing of the space succeeds at ENOSPC.
2920 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2921 tp->t_flags |= XFS_TRANS_RESERVE;
2922 error = xfs_trans_reserve(tp,
2923 resblks,
2924 XFS_WRITE_LOG_RES(mp),
2926 XFS_TRANS_PERM_LOG_RES,
2927 XFS_WRITE_LOG_COUNT);
2930 * check for running out of space
2932 if (error) {
2934 * Free the transaction structure.
2936 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2937 xfs_trans_cancel(tp, 0);
2938 break;
2940 xfs_ilock(ip, XFS_ILOCK_EXCL);
2941 error = xfs_trans_reserve_quota(tp, mp,
2942 ip->i_udquot, ip->i_gdquot,
2943 resblks, 0, XFS_QMOPT_RES_REGBLKS);
2944 if (error)
2945 goto error1;
2947 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2948 xfs_trans_ihold(tp, ip);
2951 * issue the bunmapi() call to free the blocks
2953 xfs_bmap_init(&free_list, &firstfsb);
2954 error = xfs_bunmapi(tp, ip, startoffset_fsb,
2955 endoffset_fsb - startoffset_fsb,
2956 0, 2, &firstfsb, &free_list, NULL, &done);
2957 if (error) {
2958 goto error0;
2962 * complete the transaction
2964 error = xfs_bmap_finish(&tp, &free_list, &committed);
2965 if (error) {
2966 goto error0;
2969 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2970 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2973 out_unlock_iolock:
2974 if (need_iolock)
2975 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2976 return error;
2978 error0:
2979 xfs_bmap_cancel(&free_list);
2980 error1:
2981 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2982 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2983 XFS_ILOCK_EXCL);
2984 return error;
2988 * xfs_change_file_space()
2989 * This routine allocates or frees disk space for the given file.
2990 * The user specified parameters are checked for alignment and size
2991 * limitations.
2993 * RETURNS:
2994 * 0 on success
2995 * errno on error
2999 xfs_change_file_space(
3000 xfs_inode_t *ip,
3001 int cmd,
3002 xfs_flock64_t *bf,
3003 xfs_off_t offset,
3004 int attr_flags)
3006 xfs_mount_t *mp = ip->i_mount;
3007 int clrprealloc;
3008 int error;
3009 xfs_fsize_t fsize;
3010 int setprealloc;
3011 xfs_off_t startoffset;
3012 xfs_off_t llen;
3013 xfs_trans_t *tp;
3014 struct iattr iattr;
3016 xfs_itrace_entry(ip);
3018 if (!S_ISREG(ip->i_d.di_mode))
3019 return XFS_ERROR(EINVAL);
3021 switch (bf->l_whence) {
3022 case 0: /*SEEK_SET*/
3023 break;
3024 case 1: /*SEEK_CUR*/
3025 bf->l_start += offset;
3026 break;
3027 case 2: /*SEEK_END*/
3028 bf->l_start += ip->i_size;
3029 break;
3030 default:
3031 return XFS_ERROR(EINVAL);
3034 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3036 if ( (bf->l_start < 0)
3037 || (bf->l_start > XFS_MAXIOFFSET(mp))
3038 || (bf->l_start + llen < 0)
3039 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3040 return XFS_ERROR(EINVAL);
3042 bf->l_whence = 0;
3044 startoffset = bf->l_start;
3045 fsize = ip->i_size;
3048 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3049 * file space.
3050 * These calls do NOT zero the data space allocated to the file,
3051 * nor do they change the file size.
3053 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3054 * space.
3055 * These calls cause the new file data to be zeroed and the file
3056 * size to be changed.
3058 setprealloc = clrprealloc = 0;
3060 switch (cmd) {
3061 case XFS_IOC_RESVSP:
3062 case XFS_IOC_RESVSP64:
3063 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3064 1, attr_flags);
3065 if (error)
3066 return error;
3067 setprealloc = 1;
3068 break;
3070 case XFS_IOC_UNRESVSP:
3071 case XFS_IOC_UNRESVSP64:
3072 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3073 attr_flags)))
3074 return error;
3075 break;
3077 case XFS_IOC_ALLOCSP:
3078 case XFS_IOC_ALLOCSP64:
3079 case XFS_IOC_FREESP:
3080 case XFS_IOC_FREESP64:
3081 if (startoffset > fsize) {
3082 error = xfs_alloc_file_space(ip, fsize,
3083 startoffset - fsize, 0, attr_flags);
3084 if (error)
3085 break;
3088 iattr.ia_valid = ATTR_SIZE;
3089 iattr.ia_size = startoffset;
3091 error = xfs_setattr(ip, &iattr, attr_flags);
3093 if (error)
3094 return error;
3096 clrprealloc = 1;
3097 break;
3099 default:
3100 ASSERT(0);
3101 return XFS_ERROR(EINVAL);
3105 * update the inode timestamp, mode, and prealloc flag bits
3107 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3109 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3110 0, 0, 0))) {
3111 /* ASSERT(0); */
3112 xfs_trans_cancel(tp, 0);
3113 return error;
3116 xfs_ilock(ip, XFS_ILOCK_EXCL);
3118 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3119 xfs_trans_ihold(tp, ip);
3121 if ((attr_flags & XFS_ATTR_DMI) == 0) {
3122 ip->i_d.di_mode &= ~S_ISUID;
3125 * Note that we don't have to worry about mandatory
3126 * file locking being disabled here because we only
3127 * clear the S_ISGID bit if the Group execute bit is
3128 * on, but if it was on then mandatory locking wouldn't
3129 * have been enabled.
3131 if (ip->i_d.di_mode & S_IXGRP)
3132 ip->i_d.di_mode &= ~S_ISGID;
3134 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3136 if (setprealloc)
3137 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3138 else if (clrprealloc)
3139 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3141 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3142 xfs_trans_set_sync(tp);
3144 error = xfs_trans_commit(tp, 0);
3146 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3148 return error;