blob: 6558ffd8d140683bdb1eaebb0e201dcdba3b04e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_itable.h"
41#include "xfs_btree.h"
42#include "xfs_ialloc.h"
43#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_bmap.h"
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020045#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_attr.h"
47#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_quota.h"
50#include "xfs_utils.h"
Nathan Scotta844f452005-11-02 14:38:42 +110051#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "xfs_log_priv.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100054#include "xfs_filestream.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100055#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000056#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Christoph Hellwig993386c2007-08-28 16:12:30 +100058int
Linus Torvalds1da177e2005-04-16 15:20:36 -070059xfs_setattr(
Christoph Hellwig0f285c82008-07-18 17:13:28 +100060 struct xfs_inode *ip,
61 struct iattr *iattr,
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +110062 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Christoph Hellwig993386c2007-08-28 16:12:30 +100064 xfs_mount_t *mp = ip->i_mount;
David Chinnere4f75292008-08-13 16:00:45 +100065 struct inode *inode = VFS_I(ip);
Christoph Hellwig0f285c82008-07-18 17:13:28 +100066 int mask = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 int code;
69 uint lock_flags;
70 uint commit_flags=0;
71 uid_t uid=0, iuid=0;
72 gid_t gid=0, igid=0;
73 int timeflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
Dean Roehrich5fcbab32005-05-05 13:27:19 -070075 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Lachlan McIlroycf441ee2008-02-07 16:42:19 +110077 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Christoph Hellwigbd186aa2007-08-30 17:21:12 +100079 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return XFS_ERROR(EROFS);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (XFS_FORCED_SHUTDOWN(mp))
83 return XFS_ERROR(EIO);
84
Christoph Hellwigc4cd7472008-12-09 04:47:34 -050085 code = -inode_change_ok(inode, iattr);
86 if (code)
87 return code;
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 olddquot1 = olddquot2 = NULL;
90 udqp = gdqp = NULL;
91
92 /*
93 * If disk quotas is on, we make sure that the dquots do exist on disk,
94 * before we start any other transactions. Trying to do this later
95 * is messy. We don't care to take a readlock to look at the ids
96 * in inode here, because we can't hold it across the trans_reserve.
97 * If the IDs do change before we take the ilock, we're covered
98 * because the i_*dquot fields will get updated anyway.
99 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000100 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 uint qflags = 0;
102
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000103 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
104 uid = iattr->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 qflags |= XFS_QMOPT_UQUOTA;
106 } else {
107 uid = ip->i_d.di_uid;
108 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000109 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
110 gid = iattr->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 qflags |= XFS_QMOPT_GQUOTA;
112 } else {
113 gid = ip->i_d.di_gid;
114 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /*
117 * We take a reference when we initialize udqp and gdqp,
118 * so it is important that we never blindly double trip on
119 * the same variable. See xfs_create() for an example.
120 */
121 ASSERT(udqp == NULL);
122 ASSERT(gdqp == NULL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200123 code = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000124 qflags, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100126 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
129 /*
130 * For the other attributes, we acquire the inode lock and
131 * first do an error checking pass.
132 */
133 tp = NULL;
134 lock_flags = XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000135 if (flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700136 need_iolock = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000137 if (!(mask & ATTR_SIZE)) {
138 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 (mp->m_flags & XFS_MOUNT_WSYNC)) {
140 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
141 commit_flags = 0;
142 if ((code = xfs_trans_reserve(tp, 0,
143 XFS_ICHANGE_LOG_RES(mp), 0,
144 0, 0))) {
145 lock_flags = 0;
146 goto error_return;
147 }
148 }
149 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000150 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000151 !(flags & XFS_ATTR_DMI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100153 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000154 iattr->ia_size, 0, dmflags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (code) {
156 lock_flags = 0;
157 goto error_return;
158 }
159 }
160 if (need_iolock)
161 lock_flags |= XFS_IOLOCK_EXCL;
162 }
163
164 xfs_ilock(ip, lock_flags);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /*
167 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000169 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /*
171 * These IDs could have changed since we last looked at them.
172 * But, we're assured that if the ownership did change
173 * while we didn't have the inode locked, inode's dquot(s)
174 * would have changed also.
175 */
176 iuid = ip->i_d.di_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 igid = ip->i_d.di_gid;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000178 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
179 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000182 * Do a quota reservation only if uid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * going to change.
184 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200185 if (XFS_IS_QUOTA_RUNNING(mp) &&
186 ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
187 (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 ASSERT(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200189 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 capable(CAP_FOWNER) ?
191 XFS_QMOPT_FORCE_RES : 0);
192 if (code) /* out of quota */
193 goto error_return;
194 }
195 }
196
197 /*
198 * Truncate file. Must have write permission and not be a directory.
199 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000200 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* Short circuit the truncate case for zero length files */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000202 if (iattr->ia_size == 0 &&
203 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 xfs_iunlock(ip, XFS_ILOCK_EXCL);
205 lock_flags &= ~XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000206 if (mask & ATTR_CTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
208 code = 0;
209 goto error_return;
210 }
211
Christoph Hellwig42173f62008-04-22 17:33:25 +1000212 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 code = XFS_ERROR(EISDIR);
214 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000215 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 code = XFS_ERROR(EINVAL);
217 goto error_return;
218 }
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /*
221 * Make sure that the dquots are attached to the inode.
222 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200223 code = xfs_qm_dqattach_locked(ip, 0);
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500224 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500227 /*
228 * Now we can make the changes. Before we join the inode
229 * to the transaction, if ATTR_SIZE is set then take care of
230 * the part of the truncation that must be done without the
231 * inode lock. This needs to be done before joining the inode
232 * to the transaction, because the inode cannot be unlocked
233 * once it is a part of the transaction.
234 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000235 if (iattr->ia_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000236 /*
237 * Do the first part of growing a file: zero any data
238 * in the last block that is beyond the old EOF. We
239 * need to do this before the inode is joined to the
240 * transaction to modify the i_size.
241 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000242 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000245
246 /*
247 * We are going to log the inode size change in this
248 * transaction so any previous writes that are beyond the on
249 * disk EOF and the new EOF that have not been written out need
250 * to be written here. If we do not write the data out, we
251 * expose ourselves to the null files problem.
252 *
253 * Only flush from the on disk size to the smaller of the in
254 * memory file size or the new size as that's the range we
255 * really care about here and prevents waiting for other data
256 * not within the range we care about here.
257 */
258 if (!code &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000259 ip->i_size != ip->i_d.di_size &&
260 iattr->ia_size > ip->i_d.di_size) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000261 code = xfs_flush_pages(ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000262 ip->i_d.di_size, iattr->ia_size,
David Chinnerc32676e2007-07-19 16:28:58 +1000263 XFS_B_ASYNC, FI_NONE);
264 }
265
266 /* wait for all I/O to complete */
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100267 xfs_ioend_wait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!code)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000270 code = xfs_itruncate_data(ip, iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (code) {
272 ASSERT(tp == NULL);
273 lock_flags &= ~XFS_ILOCK_EXCL;
274 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
275 goto error_return;
276 }
277 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
278 if ((code = xfs_trans_reserve(tp, 0,
279 XFS_ITRUNCATE_LOG_RES(mp), 0,
280 XFS_TRANS_PERM_LOG_RES,
281 XFS_ITRUNCATE_LOG_COUNT))) {
282 xfs_trans_cancel(tp, 0);
283 if (need_iolock)
284 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
285 return code;
286 }
287 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
288 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 xfs_trans_ijoin(tp, ip, lock_flags);
291 xfs_trans_ihold(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
David Chinner44d814c2008-03-06 13:45:29 +1100293 /*
294 * Only change the c/mtime if we are changing the size
295 * or we are explicitly asked to change it. This handles
296 * the semantic difference between truncate() and ftruncate()
297 * as implemented in the VFS.
298 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000299 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
David Chinner44d814c2008-03-06 13:45:29 +1100300 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
301
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000302 if (iattr->ia_size > ip->i_size) {
303 ip->i_d.di_size = iattr->ia_size;
304 ip->i_size = iattr->ia_size;
305 if (!(flags & XFS_ATTR_DMI))
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000306 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
307 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000308 } else if (iattr->ia_size <= ip->i_size ||
309 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /*
311 * signal a sync transaction unless
312 * we're truncating an already unlinked
313 * file on a wsync filesystem
314 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000315 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 XFS_DATA_FORK,
317 ((ip->i_d.di_nlink != 0 ||
318 !(mp->m_flags & XFS_MOUNT_WSYNC))
319 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000320 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000322 /*
323 * Truncated "down", so we're removing references
324 * to old data here - if we now delay flushing for
325 * a long time, we expose ourselves unduly to the
326 * notorious NULL files problem. So, we mark this
327 * vnode and flush it when the file is closed, and
328 * do not wait the usual (long) time for writeout.
329 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000330 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500332 } else if (tp) {
333 xfs_trans_ijoin(tp, ip, lock_flags);
334 xfs_trans_ihold(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 /*
338 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000340 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /*
342 * CAP_FSETID overrides the following restrictions:
343 *
344 * The set-user-ID and set-group-ID bits of a file will be
345 * cleared upon successful return from chown()
346 */
347 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
348 !capable(CAP_FSETID)) {
349 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
350 }
351
352 /*
353 * Change the ownerships and register quota modifications
354 * in the transaction.
355 */
356 if (iuid != uid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200357 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000358 ASSERT(mask & ATTR_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 ASSERT(udqp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200360 olddquot1 = xfs_qm_vop_chown(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 &ip->i_udquot, udqp);
362 }
363 ip->i_d.di_uid = uid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000364 inode->i_uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 if (igid != gid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200367 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000368 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000369 ASSERT(mask & ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ASSERT(gdqp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200371 olddquot2 = xfs_qm_vop_chown(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 &ip->i_gdquot, gdqp);
373 }
374 ip->i_d.di_gid = gid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000375 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
379 timeflags |= XFS_ICHGTIME_CHG;
380 }
381
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500382 /*
383 * Change file access modes.
384 */
385 if (mask & ATTR_MODE) {
386 umode_t mode = iattr->ia_mode;
387
388 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
389 mode &= ~S_ISGID;
390
391 ip->i_d.di_mode &= S_IFMT;
392 ip->i_d.di_mode |= mode & ~S_IFMT;
393
394 inode->i_mode &= S_IFMT;
395 inode->i_mode |= mode & ~S_IFMT;
396
397 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
398 timeflags |= XFS_ICHGTIME_CHG;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /*
402 * Change file access or modified times.
403 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000404 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
405 if (mask & ATTR_ATIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000406 inode->i_atime = iattr->ia_atime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000407 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
408 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 ip->i_update_core = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000411 if (mask & ATTR_MTIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000412 inode->i_mtime = iattr->ia_mtime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000413 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
414 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 timeflags &= ~XFS_ICHGTIME_MOD;
416 timeflags |= XFS_ICHGTIME_CHG;
417 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000418 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
420 }
421
422 /*
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000423 * Change file inode change time only if ATTR_CTIME set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * AND we have been called by a DMI function.
425 */
426
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000427 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000428 inode->i_ctime = iattr->ia_ctime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000429 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
430 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 ip->i_update_core = 1;
432 timeflags &= ~XFS_ICHGTIME_CHG;
433 }
434
435 /*
436 * Send out timestamp changes that need to be set to the
437 * current time. Not done when called by a DMI function.
438 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000439 if (timeflags && !(flags & XFS_ATTR_DMI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 xfs_ichgtime(ip, timeflags);
441
442 XFS_STATS_INC(xs_ig_attrchg);
443
444 /*
445 * If this is a synchronous mount, make sure that the
446 * transaction goes to disk before returning to the user.
447 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000448 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * One for the truncate and one for the timestamps since we
450 * don't want to change the timestamps unless we're sure the
451 * truncate worked. Truncates are less than 1% of the laddis
452 * mix so this probably isn't worth the trouble to optimize.
453 */
454 code = 0;
455 if (tp) {
456 if (mp->m_flags & XFS_MOUNT_WSYNC)
457 xfs_trans_set_sync(tp);
458
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000459 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 xfs_iunlock(ip, lock_flags);
463
464 /*
465 * Release any dquot(s) the inode had kept before chown.
466 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200467 xfs_qm_dqrele(olddquot1);
468 xfs_qm_dqrele(olddquot2);
469 xfs_qm_dqrele(udqp);
470 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200472 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return code;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200474
475 /*
476 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
477 * update. We could avoid this with linked transactions
478 * and passing down the transaction pointer all the way
479 * to attr_set. No previous user of the generic
480 * Posix ACL code seems to care about this issue either.
481 */
482 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
483 code = -xfs_acl_chmod(inode);
484 if (code)
485 return XFS_ERROR(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000488 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000489 !(flags & XFS_ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100490 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 NULL, DM_RIGHT_NULL, NULL, NULL,
492 0, 0, AT_DELAY_FLAG(flags));
493 }
494 return 0;
495
496 abort_return:
497 commit_flags |= XFS_TRANS_ABORT;
498 /* FALLTHROUGH */
499 error_return:
Christoph Hellwig7d095252009-06-08 15:33:32 +0200500 xfs_qm_dqrele(udqp);
501 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (tp) {
503 xfs_trans_cancel(tp, commit_flags);
504 }
505 if (lock_flags != 0) {
506 xfs_iunlock(ip, lock_flags);
507 }
508 return code;
509}
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000512 * The maximum pathlen is 1024 bytes. Since the minimum file system
513 * blocksize is 512 bytes, we can get a max of 2 extents back from
514 * bmapi.
515 */
516#define SYMLINK_MAPS 2
517
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000518STATIC int
519xfs_readlink_bmap(
520 xfs_inode_t *ip,
521 char *link)
522{
523 xfs_mount_t *mp = ip->i_mount;
524 int pathlen = ip->i_d.di_size;
525 int nmaps = SYMLINK_MAPS;
526 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
527 xfs_daddr_t d;
528 int byte_cnt;
529 int n;
530 xfs_buf_t *bp;
531 int error = 0;
532
533 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
534 mval, &nmaps, NULL, NULL);
535 if (error)
536 goto out;
537
538 for (n = 0; n < nmaps; n++) {
539 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
540 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
541
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000542 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
543 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000544 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;
550 }
551 if (pathlen < byte_cnt)
552 byte_cnt = pathlen;
553 pathlen -= byte_cnt;
554
555 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
556 xfs_buf_relse(bp);
557 }
558
559 link[ip->i_d.di_size] = '\0';
560 error = 0;
561
562 out:
563 return error;
564}
565
Christoph Hellwig993386c2007-08-28 16:12:30 +1000566int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000568 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000569 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000571 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100575 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (XFS_FORCED_SHUTDOWN(mp))
578 return XFS_ERROR(EIO);
579
580 xfs_ilock(ip, XFS_ILOCK_SHARED);
581
582 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000583 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000585 pathlen = ip->i_d.di_size;
586 if (!pathlen)
587 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000590 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
591 link[pathlen] = '\0';
592 } else {
593 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000596 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return error;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/*
602 * xfs_fsync
603 *
David Chinner978b7232008-05-19 16:29:46 +1000604 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000609int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610xfs_fsync(
David Chinner978b7232008-05-19 16:29:46 +1000611 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 xfs_trans_t *tp;
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300614 int error = 0;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100615 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100617 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
620 return XFS_ERROR(EIO);
621
622 /*
David Chinner978b7232008-05-19 16:29:46 +1000623 * We always need to make sure that the required inode state is safe on
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300624 * disk. The inode might be clean but we still might need to force the
David Chinner978b7232008-05-19 16:29:46 +1000625 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *
David Chinner978b7232008-05-19 16:29:46 +1000630 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
634 xfs_ilock(ip, XFS_ILOCK_SHARED);
635
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300636 if (!ip->i_update_core) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*
David Chinner978b7232008-05-19 16:29:46 +1000638 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 */
645
646 xfs_iunlock(ip, XFS_ILOCK_SHARED);
647
648 if (xfs_ipincount(ip)) {
David Chinner978b7232008-05-19 16:29:46 +1000649 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
650 XFS_LOG_FORCE | XFS_LOG_SYNC,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100651 &log_flushed);
652 } else {
653 /*
David Chinner978b7232008-05-19 16:29:46 +1000654 * If the inode is not pinned and nothing has changed
655 * we don't need to flush the cache.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100656 */
657 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 } else {
660 /*
David Chinner978b7232008-05-19 16:29:46 +1000661 * Kick off a transaction to log the inode core to get the
662 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
664 xfs_iunlock(ip, XFS_ILOCK_SHARED);
665 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000666 error = xfs_trans_reserve(tp, 0,
667 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
668 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 xfs_trans_cancel(tp, 0);
670 return error;
671 }
672 xfs_ilock(ip, XFS_ILOCK_EXCL);
673
674 /*
David Chinner978b7232008-05-19 16:29:46 +1000675 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
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);
David Chinner978b7232008-05-19 16:29:46 +1000686 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000687 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 xfs_iunlock(ip, XFS_ILOCK_EXCL);
690 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100691
692 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
693 /*
694 * If the log write didn't issue an ordered tag we need
695 * to flush the disk cache for the data device now.
696 */
697 if (!log_flushed)
698 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
699
700 /*
701 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000702 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100703 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100704 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100705 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
706 }
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return error;
709}
710
711/*
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000712 * Flags for xfs_free_eofblocks
713 */
714#define XFS_FREE_EOF_TRYLOCK (1<<0)
715
716/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000717 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Eric Sandeend96f8f82009-07-02 00:09:33 -0500721STATIC int
David Chinner92dfe8d2007-05-24 15:22:19 +1000722xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000724 xfs_inode_t *ip,
725 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
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;
734
735 /*
736 * Figure out if there are any blocks beyond the end
737 * of the file. If not, then there is nothing to do.
738 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000739 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 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)
Jesper Juhl014c2542006-01-15 02:37:08 +0100743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 nimaps = 1;
746 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000747 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000748 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 xfs_iunlock(ip, XFS_ILOCK_SHARED);
750
751 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100752 (imap.br_startblock != HOLESTARTBLOCK ||
753 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /*
755 * Attach the dquots to the inode up front.
756 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200757 error = xfs_qm_dqattach(ip, 0);
758 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100759 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /*
762 * There are blocks after the end of file.
763 * Free them up now by truncating the file to
764 * its current size.
765 */
766 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
767
768 /*
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.
774 */
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000775 if (flags & XFS_FREE_EOF_TRYLOCK) {
776 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
777 xfs_trans_cancel(tp, 0);
778 return 0;
779 }
780 } else {
David Chinner92dfe8d2007-05-24 15:22:19 +1000781 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000782 }
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000783 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000784 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000785 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000786 xfs_trans_cancel(tp, 0);
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000787 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000788 return error;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
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);
Jesper Juhl014c2542006-01-15 02:37:08 +0100799 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
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);
807
808 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000809 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 XFS_DATA_FORK,
811 0);
812 /*
813 * If we get an error at this point we
814 * simply don't bother truncating the file.
815 */
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,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000822 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000824 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100826 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
829/*
830 * Free a symlink that has blocks associated with it.
831 */
832STATIC int
833xfs_inactive_symlink_rmt(
834 xfs_inode_t *ip,
835 xfs_trans_t **tpp)
836{
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;
850
851 tp = *tpp;
852 mp = ip->i_mount;
853 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
854 /*
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.
860 */
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;
868 }
869 /*
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.
874 */
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);
881 /*
882 * Find the block(s) so we can inval and unmap them.
883 */
884 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600885 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800886 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
888 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000889 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 goto error0;
891 /*
892 * Invalidate the block(s).
893 */
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);
899 }
900 /*
901 * Unmap the dead block(s) to the free_list.
902 */
903 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000904 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto error1;
906 ASSERT(done);
907 /*
908 * Commit the first transaction. This logs the EFI and the inode.
909 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100910 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto error1;
912 /*
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.
916 */
917 ASSERT(committed);
918 /*
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.
922 */
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);
926 /*
927 * Get a new, empty transaction to return to our caller.
928 */
929 ntp = xfs_trans_dup(tp);
930 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000931 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 * 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.
935 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000936 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 tp = ntp;
938 if (error) {
939 ASSERT(XFS_FORCED_SHUTDOWN(mp));
940 goto error0;
941 }
942 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100943 * transaction commit worked ok so we can drop the extra ticket
944 * reference that we gained in xfs_trans_dup()
945 */
946 xfs_log_ticket_put(tp->t_ticket);
947
948 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * Remove the memory for extent descriptions (just bookkeeping).
950 */
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);
954 /*
955 * Put an itruncate log reservation in the new transaction
956 * for our caller.
957 */
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;
962 }
963 /*
964 * Return with the inode locked but not joined to the transaction.
965 */
966 *tpp = tp;
967 return 0;
968
969 error1:
970 xfs_bmap_cancel(&free_list);
971 error0:
972 /*
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.
981 */
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;
986
987}
988
989STATIC int
990xfs_inactive_symlink_local(
991 xfs_inode_t *ip,
992 xfs_trans_t **tpp)
993{
994 int error;
995
996 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
997 /*
998 * We're freeing a symlink which fit into
999 * the inode. Just free the memory used
1000 * to hold the old symlink.
1001 */
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);
1006
1007 if (error) {
1008 xfs_trans_cancel(*tpp, 0);
1009 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001010 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1013
1014 /*
1015 * Zero length symlinks _can_ exist.
1016 */
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);
1022 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026STATIC int
1027xfs_inactive_attrs(
1028 xfs_inode_t *ip,
1029 xfs_trans_t **tpp)
1030{
1031 xfs_trans_t *tp;
1032 int error;
1033 xfs_mount_t *mp;
1034
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001035 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 tp = *tpp;
1037 mp = ip->i_mount;
1038 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001039 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001041 if (error)
1042 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001045 if (error)
1046 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
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);
David Chinnere5720ee2008-04-10 12:21:18 +10001053 if (error)
1054 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
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);
1060
1061 ASSERT(ip->i_d.di_anextents == 0);
1062
1063 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001064 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001065
1066error_cancel:
1067 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1068 xfs_trans_cancel(tp, 0);
1069error_unlock:
1070 *tpp = NULL;
1071 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1072 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Christoph Hellwig993386c2007-08-28 16:12:30 +10001075int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001077 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001079 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int error;
1081
Christoph Hellwig42173f62008-04-22 17:33:25 +10001082 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001086 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return 0;
1088
David Chinner2a82b8b2007-07-11 11:09:12 +10001089 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001090 int truncated;
1091
David Chinner2a82b8b2007-07-11 11:09:12 +10001092 /*
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.
1098 */
1099 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1100 xfs_filestream_deassociate(ip);
1101
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001102 /*
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.
1111 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001112 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001113 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001114 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001115 }
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (ip->i_d.di_nlink != 0) {
1118 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001119 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001120 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001122 (!(ip->i_d.di_flags &
1123 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
Christoph Hellwigc56c96312009-10-19 04:03:46 +00001124
1125 /*
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.
1132 */
1133 error = xfs_free_eofblocks(mp, ip,
1134 XFS_FREE_EOF_TRYLOCK);
David Chinner92dfe8d2007-05-24 15:22:19 +10001135 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001136 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 }
1139
1140 return 0;
1141}
1142
1143/*
1144 * xfs_inactive
1145 *
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.
1150 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001151int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001153 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001155 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 xfs_fsblock_t first_block;
1157 int committed;
1158 xfs_trans_t *tp;
1159 xfs_mount_t *mp;
1160 int error;
1161 int truncate;
1162
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001163 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /*
1166 * If the inode is already free, then there can be nothing
1167 * to clean up here.
1168 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +01001169 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 ASSERT(ip->i_df.if_real_bytes == 0);
1171 ASSERT(ip->i_df.if_broot_bytes == 0);
1172 return VN_INACTIVE_CACHE;
1173 }
1174
1175 /*
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.
1180 */
1181 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001182 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1183 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1185
1186 mp = ip->i_mount;
1187
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001188 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1189 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 error = 0;
1192
1193 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001194 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto out;
1196
1197 if (ip->i_d.di_nlink != 0) {
1198 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001199 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001200 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001201 (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)))) {
Christoph Hellwigc56c96312009-10-19 04:03:46 +00001205 error = xfs_free_eofblocks(mp, ip, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001206 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001207 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 goto out;
1210 }
1211
1212 ASSERT(ip->i_d.di_nlink == 0);
1213
Christoph Hellwig7d095252009-06-08 15:33:32 +02001214 error = xfs_qm_dqattach(ip, 0);
1215 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001216 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1219 if (truncate) {
1220 /*
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.
1225 */
1226 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1227
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001228 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1229 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001230 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001231 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1232 return VN_INACTIVE_CACHE;
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
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);
Jesper Juhl014c2542006-01-15 02:37:08 +01001244 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246
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);
1250
1251 /*
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.
1257 */
1258 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1259 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1260
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);
Jesper Juhl014c2542006-01-15 02:37:08 +01001265 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1268
1269 /*
1270 * If we get an error while cleaning up a
1271 * symlink we bail out.
1272 */
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);
1276
1277 if (error) {
1278 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001279 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
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);
Jesper Juhl014c2542006-01-15 02:37:08 +01001292 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
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);
1298 }
1299
1300 /*
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().
1306 */
1307 if (ip->i_d.di_anextents > 0) {
1308 error = xfs_inactive_attrs(ip, &tp);
1309 /*
1310 * If we got an error, the transaction is already
1311 * cancelled, and the inode is unlocked. Just get out.
1312 */
1313 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001314 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 } else if (ip->i_afp) {
1316 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1317 }
1318
1319 /*
1320 * Free the inode.
1321 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001322 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 error = xfs_ifree(tp, ip, &free_list);
1324 if (error) {
1325 /*
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.
1329 */
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);
Nathan Scott7d04a332006-06-09 14:58:38 +10001334 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1337 } else {
1338 /*
1339 * Credit the quota account(s). The inode is gone.
1340 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001341 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /*
David Chinner78e9da72008-04-10 12:24:17 +10001344 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
David Chinner78e9da72008-04-10 12:24:17 +10001348 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Christoph Hellwig7d095252009-06-08 15:33:32 +02001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /*
1359 * Release the dquots held by inode, if any.
1360 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001361 xfs_qm_dqdetach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1363
1364 out:
1365 return VN_INACTIVE_CACHE;
1366}
1367
Barry Naujok384f3ce2008-05-21 16:58:22 +10001368/*
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.
1373 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001374int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001376 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001377 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001378 xfs_inode_t **ipp,
1379 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001381 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 int error;
1383 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001385 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1388 return XFS_ERROR(EIO);
1389
1390 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001391 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001393
1394 if (error)
1395 goto out;
1396
1397 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1398 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001399 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001400
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001401 return 0;
1402
Barry Naujok384f3ce2008-05-21 16:58:22 +10001403out_free_name:
1404 if (ci_name)
1405 kmem_free(ci_name->name);
1406out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001407 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return error;
1409}
1410
Christoph Hellwig993386c2007-08-28 16:12:30 +10001411int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001413 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001414 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001415 mode_t mode,
1416 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001417 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 cred_t *credp)
1419{
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001420 int is_dir = S_ISDIR(mode);
1421 struct xfs_mount *mp = dp->i_mount;
1422 struct xfs_inode *ip = NULL;
1423 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +10001424 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 xfs_bmap_free_t free_list;
1426 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001427 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 uint cancel_flags;
1429 int committed;
1430 xfs_prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001431 struct xfs_dquot *udqp = NULL;
1432 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001434 uint log_res;
1435 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001437 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001439 if (XFS_FORCED_SHUTDOWN(mp))
1440 return XFS_ERROR(EIO);
1441
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001442 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001444 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001445 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001446 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 if (error)
1449 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
Nathan Scott365ca832005-06-21 15:39:12 +10001452 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1453 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 else
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001455 prid = dfltprid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /*
1458 * Make sure that we have allocated dquot(s) on disk.
1459 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001460 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001461 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (error)
1463 goto std_return;
1464
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001465 if (is_dir) {
1466 rdev = 0;
1467 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1468 log_res = XFS_MKDIR_LOG_RES(mp);
1469 log_count = XFS_MKDIR_LOG_COUNT;
1470 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1471 } else {
1472 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1473 log_res = XFS_CREATE_LOG_RES(mp);
1474 log_count = XFS_CREATE_LOG_COUNT;
1475 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /*
1481 * Initially assume that the file does not exist and
1482 * reserve the resources for that case. If that is not
1483 * the case we'll drop the one we have and get a more
1484 * appropriate transaction later.
1485 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001486 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1487 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +02001489 /* flush outstanding delalloc blocks and retry */
1490 xfs_flush_inodes(dp);
Christoph Hellwig4734d402009-09-09 18:19:02 -05001491 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1492 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +02001493 }
1494 if (error == ENOSPC) {
1495 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001497 error = xfs_trans_reserve(tp, 0, log_res, 0,
1498 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500 if (error) {
1501 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001502 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001505 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001506 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001508 /*
1509 * Check for directory link count overflow.
1510 */
1511 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1512 error = XFS_ERROR(EMLINK);
1513 goto out_trans_cancel;
1514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001516 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 /*
1519 * Reserve disk quota and the inode.
1520 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001521 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001523 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Barry Naujok556b8b12008-04-10 12:22:07 +10001525 error = xfs_dir_canenter(tp, dp, name, resblks);
1526 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001527 goto out_trans_cancel;
1528
1529 /*
1530 * A newly created regular or special file just has one directory
1531 * entry pointing to them, but a directory also the "." entry
1532 * pointing to itself.
1533 */
1534 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1535 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (error) {
1537 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001538 goto out_trans_cancel;
1539 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /*
1543 * At this point, we've gotten a newly allocated inode.
1544 * It is locked (and joined to the transaction).
1545 */
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001546 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001549 * Now we join the directory inode to the transaction. We do not do it
1550 * earlier because xfs_dir_ialloc might commit the previous transaction
1551 * (and release all the locks). An error from here on will result in
1552 * the transaction cancel unlocking dp so don't do it explicitly in the
1553 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001555 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001557 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Barry Naujok556b8b12008-04-10 12:22:07 +10001559 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001560 &first_block, &free_list, resblks ?
1561 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (error) {
1563 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001564 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1567 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1568
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001569 if (is_dir) {
1570 error = xfs_dir_init(tp, ip, dp);
1571 if (error)
1572 goto out_bmap_cancel;
1573
1574 error = xfs_bumplink(tp, dp);
1575 if (error)
1576 goto out_bmap_cancel;
1577 }
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /*
1580 * If this is a synchronous mount, make sure that the
1581 * create transaction goes to disk before returning to
1582 * the user.
1583 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001584 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /*
1588 * Attach the dquot(s) to the inodes and modify them incore.
1589 * These ids of the inode couldn't have changed since the new
1590 * inode has been locked ever since it was created.
1591 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001592 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 /*
1595 * xfs_trans_commit normally decrements the vnode ref count
1596 * when it unlocks the inode. Since we want to return the
1597 * vnode to the caller, we bump the vnode ref count now.
1598 */
1599 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001601 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001602 if (error)
1603 goto out_abort_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001605 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (error) {
1607 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001608 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610
Christoph Hellwig7d095252009-06-08 15:33:32 +02001611 xfs_qm_dqrele(udqp);
1612 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001614 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 /* Fallthrough to std_return with error = 0 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001617 std_return:
1618 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1619 XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
1620 ip, DM_RIGHT_NULL, name->name, NULL, mode,
1621 error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return error;
1625
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001626 out_bmap_cancel:
1627 xfs_bmap_cancel(&free_list);
1628 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001630 out_trans_cancel:
1631 xfs_trans_cancel(tp, cancel_flags);
1632 out_dqrele:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001633 xfs_qm_dqrele(udqp);
1634 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Christoph Hellwig993386c2007-08-28 16:12:30 +10001636 if (unlock_dp_on_error)
1637 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 goto std_return;
1640
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001641 out_abort_rele:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 /*
1643 * Wait until after the current transaction is aborted to
1644 * release the inode. This prevents recursive transactions
1645 * and deadlocks from xfs_inactive.
1646 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001647 xfs_bmap_cancel(&free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 cancel_flags |= XFS_TRANS_ABORT;
1649 xfs_trans_cancel(tp, cancel_flags);
1650 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001651 unlock_dp_on_error = B_FALSE;
1652 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
1655#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656int xfs_locked_n;
1657int xfs_small_retries;
1658int xfs_middle_retries;
1659int xfs_lots_retries;
1660int xfs_lock_delays;
1661#endif
1662
1663/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001664 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1665 * a different value
1666 */
1667static inline int
1668xfs_lock_inumorder(int lock_mode, int subclass)
1669{
1670 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001671 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001672 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001673 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001674
1675 return lock_mode;
1676}
1677
1678/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 * The following routine will lock n inodes in exclusive mode.
1680 * We assume the caller calls us with the inodes in i_ino order.
1681 *
1682 * We need to detect deadlock where an inode that we lock
1683 * is in the AIL and we start waiting for another inode that is locked
1684 * by a thread in a long running transaction (such as truncate). This can
1685 * result in deadlock since the long running trans might need to wait
1686 * for the inode we just locked in order to push the tail and free space
1687 * in the log.
1688 */
1689void
1690xfs_lock_inodes(
1691 xfs_inode_t **ips,
1692 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 uint lock_mode)
1694{
1695 int attempts = 0, i, j, try_lock;
1696 xfs_log_item_t *lp;
1697
1698 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1699
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001700 try_lock = 0;
1701 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703again:
1704 for (; i < inodes; i++) {
1705 ASSERT(ips[i]);
1706
1707 if (i && (ips[i] == ips[i-1])) /* Already locked */
1708 continue;
1709
1710 /*
1711 * If try_lock is not set yet, make sure all locked inodes
1712 * are not in the AIL.
1713 * If any are, set try_lock to be used later.
1714 */
1715
1716 if (!try_lock) {
1717 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1718 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1719 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1720 try_lock++;
1721 }
1722 }
1723 }
1724
1725 /*
1726 * If any of the previous locks we have locked is in the AIL,
1727 * we must TRY to get the second and subsequent locks. If
1728 * we can't get any, we must release all we have
1729 * and try again.
1730 */
1731
1732 if (try_lock) {
1733 /* try_lock must be 0 if i is 0. */
1734 /*
1735 * try_lock means we have an inode locked
1736 * that is in the AIL.
1737 */
1738 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001739 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 attempts++;
1741
1742 /*
1743 * Unlock all previous guys and try again.
1744 * xfs_iunlock will try to push the tail
1745 * if the inode is in the AIL.
1746 */
1747
1748 for(j = i - 1; j >= 0; j--) {
1749
1750 /*
1751 * Check to see if we've already
1752 * unlocked this one.
1753 * Not the first one going back,
1754 * and the inode ptr is the same.
1755 */
1756 if ((j != (i - 1)) && ips[j] ==
1757 ips[j+1])
1758 continue;
1759
1760 xfs_iunlock(ips[j], lock_mode);
1761 }
1762
1763 if ((attempts % 5) == 0) {
1764 delay(1); /* Don't just spin the CPU */
1765#ifdef DEBUG
1766 xfs_lock_delays++;
1767#endif
1768 }
1769 i = 0;
1770 try_lock = 0;
1771 goto again;
1772 }
1773 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001774 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776 }
1777
1778#ifdef DEBUG
1779 if (attempts) {
1780 if (attempts < 5) xfs_small_retries++;
1781 else if (attempts < 100) xfs_middle_retries++;
1782 else xfs_lots_retries++;
1783 } else {
1784 xfs_locked_n++;
1785 }
1786#endif
1787}
1788
David Chinnerf9114eb2008-09-17 16:51:21 +10001789/*
1790 * xfs_lock_two_inodes() can only be used to lock one type of lock
1791 * at a time - the iolock or the ilock, but not both at once. If
1792 * we lock both at once, lockdep will report false positives saying
1793 * we have violated locking orders.
1794 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001795void
1796xfs_lock_two_inodes(
1797 xfs_inode_t *ip0,
1798 xfs_inode_t *ip1,
1799 uint lock_mode)
1800{
1801 xfs_inode_t *temp;
1802 int attempts = 0;
1803 xfs_log_item_t *lp;
1804
David Chinnerf9114eb2008-09-17 16:51:21 +10001805 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1806 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001807 ASSERT(ip0->i_ino != ip1->i_ino);
1808
1809 if (ip0->i_ino > ip1->i_ino) {
1810 temp = ip0;
1811 ip0 = ip1;
1812 ip1 = temp;
1813 }
1814
1815 again:
1816 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1817
1818 /*
1819 * If the first lock we have locked is in the AIL, we must TRY to get
1820 * the second lock. If we can't get it, we must release the first one
1821 * and try again.
1822 */
1823 lp = (xfs_log_item_t *)ip0->i_itemp;
1824 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1825 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1826 xfs_iunlock(ip0, lock_mode);
1827 if ((++attempts % 5) == 0)
1828 delay(1); /* Don't just spin the CPU */
1829 goto again;
1830 }
1831 } else {
1832 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1833 }
1834}
1835
Christoph Hellwig993386c2007-08-28 16:12:30 +10001836int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001838 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001839 struct xfs_name *name,
1840 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001842 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001844 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 int error = 0;
1846 xfs_bmap_free_t free_list;
1847 xfs_fsblock_t first_block;
1848 int cancel_flags;
1849 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 int link_zero;
1851 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001852 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001854 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001855 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (XFS_FORCED_SHUTDOWN(mp))
1858 return XFS_ERROR(EIO);
1859
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001860 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001861 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1862 NULL, DM_RIGHT_NULL, name->name, NULL,
1863 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (error)
1865 return error;
1866 }
1867
Christoph Hellwig7d095252009-06-08 15:33:32 +02001868 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001869 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Christoph Hellwig7d095252009-06-08 15:33:32 +02001872 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001873 if (error)
1874 goto std_return;
1875
1876 if (is_dir) {
1877 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1878 log_count = XFS_DEFAULT_LOG_COUNT;
1879 } else {
1880 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1881 log_count = XFS_REMOVE_LOG_COUNT;
1882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /*
1886 * We try to get the real space reservation first,
1887 * allowing for directory btree deletion(s) implying
1888 * possible bmap insert(s). If we can't get the space
1889 * reservation then we use 0 instead, and avoid the bmap
1890 * btree insert(s) in the directory code by, if the bmap
1891 * insert tries to happen, instead trimming the LAST
1892 * block from the directory.
1893 */
1894 resblks = XFS_REMOVE_SPACE_RES(mp);
1895 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001896 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (error == ENOSPC) {
1898 resblks = 0;
1899 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001900 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902 if (error) {
1903 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001904 cancel_flags = 0;
1905 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001908 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 /*
1911 * At this point, we've gotten both the directory and the entry
1912 * inodes locked.
1913 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001914 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10001916
1917 IHOLD(dp);
1918 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001921 * If we're removing a directory perform some additional validation.
1922 */
1923 if (is_dir) {
1924 ASSERT(ip->i_d.di_nlink >= 2);
1925 if (ip->i_d.di_nlink != 2) {
1926 error = XFS_ERROR(ENOTEMPTY);
1927 goto out_trans_cancel;
1928 }
1929 if (!xfs_dir_isempty(ip)) {
1930 error = XFS_ERROR(ENOTEMPTY);
1931 goto out_trans_cancel;
1932 }
1933 }
1934
Eric Sandeen9d87c312009-01-14 23:22:07 -06001935 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001936 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001937 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (error) {
1939 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001940 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1943
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001944 if (is_dir) {
1945 /*
1946 * Drop the link from ip's "..".
1947 */
1948 error = xfs_droplink(tp, dp);
1949 if (error)
1950 goto out_bmap_cancel;
1951
1952 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001953 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001954 */
1955 error = xfs_droplink(tp, ip);
1956 if (error)
1957 goto out_bmap_cancel;
1958 } else {
1959 /*
1960 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +11001961 * inode here. For a directory this is done implicitly
1962 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001963 */
1964 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001967 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001968 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001969 */
1970 error = xfs_droplink(tp, ip);
1971 if (error)
1972 goto out_bmap_cancel;
1973
1974 /*
1975 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 * we are in the transaction.
1977 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001978 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 * If this is a synchronous mount, make sure that the
1982 * remove transaction goes to disk before returning to
1983 * the user.
1984 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001985 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001988 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001989 if (error)
1990 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001992 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001993 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001997 * If we are using filestreams, kill the stream association.
1998 * If the file is still open it may get a new one but that
1999 * will get killed on last close in xfs_close() so we don't
2000 * have to worry about that.
2001 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002002 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10002003 xfs_filestream_deassociate(ip);
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002006 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002007 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2008 NULL, DM_RIGHT_NULL, name->name, NULL,
2009 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return error;
2013
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002014 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 xfs_bmap_cancel(&free_list);
2016 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002017 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 xfs_trans_cancel(tp, cancel_flags);
2019 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Christoph Hellwig993386c2007-08-28 16:12:30 +10002022int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002024 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002025 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002026 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002028 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 int error;
2031 xfs_bmap_free_t free_list;
2032 xfs_fsblock_t first_block;
2033 int cancel_flags;
2034 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002037 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002038 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002040 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (XFS_FORCED_SHUTDOWN(mp))
2043 return XFS_ERROR(EIO);
2044
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002045 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002047 tdp, DM_RIGHT_NULL,
2048 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002049 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (error)
2051 return error;
2052 }
2053
2054 /* Return through std_return after this point. */
2055
Christoph Hellwig7d095252009-06-08 15:33:32 +02002056 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01002057 if (error)
2058 goto std_return;
2059
Christoph Hellwig7d095252009-06-08 15:33:32 +02002060 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (error)
2062 goto std_return;
2063
2064 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2065 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002066 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2068 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2069 if (error == ENOSPC) {
2070 resblks = 0;
2071 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2072 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2073 }
2074 if (error) {
2075 cancel_flags = 0;
2076 goto error_return;
2077 }
2078
Christoph Hellwige1cccd92008-08-13 16:18:07 +10002079 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 /*
2082 * Increment vnode ref counts since xfs_trans_commit &
2083 * xfs_trans_cancel will both unlock the inodes and
2084 * decrement the associated ref counts.
2085 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002086 IHOLD(sip);
2087 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2089 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2090
2091 /*
2092 * If the source has too many links, we can't make any more to it.
2093 */
2094 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2095 error = XFS_ERROR(EMLINK);
2096 goto error_return;
2097 }
2098
Nathan Scott365ca832005-06-21 15:39:12 +10002099 /*
2100 * If we are using project inheritance, we only allow hard link
2101 * creation in our tree when the project IDs are the same; else
2102 * the tree quota mechanism could be circumvented.
2103 */
2104 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2105 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002106 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002107 goto error_return;
2108 }
2109
Barry Naujok556b8b12008-04-10 12:22:07 +10002110 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2111 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 goto error_return;
2113
Eric Sandeen9d87c312009-01-14 23:22:07 -06002114 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Barry Naujok556b8b12008-04-10 12:22:07 +10002116 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2117 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (error)
2119 goto abort_return;
2120 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2122
2123 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002124 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 /*
2128 * If this is a synchronous mount, make sure that the
2129 * link transaction goes to disk before returning to
2130 * the user.
2131 */
2132 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2133 xfs_trans_set_sync(tp);
2134 }
2135
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002136 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (error) {
2138 xfs_bmap_cancel(&free_list);
2139 goto abort_return;
2140 }
2141
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002142 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002143 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 /* Fall through to std_return with error = 0. */
2147std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002148 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002150 tdp, DM_RIGHT_NULL,
2151 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002152 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154 return error;
2155
2156 abort_return:
2157 cancel_flags |= XFS_TRANS_ABORT;
2158 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 error_return:
2161 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 goto std_return;
2163}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002164
Christoph Hellwig993386c2007-08-28 16:12:30 +10002165int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002167 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002168 struct xfs_name *link_name,
2169 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002170 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002171 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 cred_t *credp)
2173{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002174 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 xfs_inode_t *ip;
2177 int error;
2178 int pathlen;
2179 xfs_bmap_free_t free_list;
2180 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002181 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 uint cancel_flags;
2183 int committed;
2184 xfs_fileoff_t first_fsb;
2185 xfs_filblks_t fs_blocks;
2186 int nmaps;
2187 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2188 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002189 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 int byte_cnt;
2191 int n;
2192 xfs_buf_t *bp;
2193 xfs_prid_t prid;
2194 struct xfs_dquot *udqp, *gdqp;
2195 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Christoph Hellwig3937be52008-03-06 13:46:19 +11002197 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 error = 0;
2199 ip = NULL;
2200 tp = NULL;
2201
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002202 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 if (XFS_FORCED_SHUTDOWN(mp))
2205 return XFS_ERROR(EIO);
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /*
2208 * Check component lengths of the target path name.
2209 */
2210 pathlen = strlen(target_path);
2211 if (pathlen >= MAXPATHLEN) /* total string too long */
2212 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002214 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002215 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002217 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (error)
2219 return error;
2220 }
2221
2222 /* Return through std_return after this point. */
2223
2224 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002225 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2226 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 else
2228 prid = (xfs_prid_t)dfltprid;
2229
2230 /*
2231 * Make sure that we have allocated dquot(s) on disk.
2232 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002233 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2235 if (error)
2236 goto std_return;
2237
2238 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2239 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2240 /*
2241 * The symlink will fit into the inode data fork?
2242 * There can't be any attributes so we get the whole variable part.
2243 */
2244 if (pathlen <= XFS_LITINO(mp))
2245 fs_blocks = 0;
2246 else
2247 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002248 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2250 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2251 if (error == ENOSPC && fs_blocks == 0) {
2252 resblks = 0;
2253 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2254 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2255 }
2256 if (error) {
2257 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 goto error_return;
2259 }
2260
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002261 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002262 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 /*
2265 * Check whether the directory allows new symlinks or not.
2266 */
2267 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2268 error = XFS_ERROR(EPERM);
2269 goto error_return;
2270 }
2271
2272 /*
2273 * Reserve disk quota : blocks and inode.
2274 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002275 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (error)
2277 goto error_return;
2278
2279 /*
2280 * Check for ability to enter directory entry, if no space reserved.
2281 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002282 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2283 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 goto error_return;
2285 /*
2286 * Initialize the bmap freelist prior to calling either
2287 * bmapi or the directory create code.
2288 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002289 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 /*
2292 * Allocate an inode for the symlink.
2293 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002294 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 1, 0, credp, prid, resblks > 0, &ip, NULL);
2296 if (error) {
2297 if (error == ENOSPC)
2298 goto error_return;
2299 goto error1;
2300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Christoph Hellwig993386c2007-08-28 16:12:30 +10002302 /*
2303 * An error after we've joined dp to the transaction will result in the
2304 * transaction cancel unlocking dp so don't do it explicitly in the
2305 * error path.
2306 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002307 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002309 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 /*
2312 * Also attach the dquot(s) to it, if applicable.
2313 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002314 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 if (resblks)
2317 resblks -= XFS_IALLOC_SPACE_RES(mp);
2318 /*
2319 * If the symlink will fit into the inode, write it inline.
2320 */
2321 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2322 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2323 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2324 ip->i_d.di_size = pathlen;
2325
2326 /*
2327 * The inode was initially created in extent format.
2328 */
2329 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2330 ip->i_df.if_flags |= XFS_IFINLINE;
2331
2332 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2333 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2334
2335 } else {
2336 first_fsb = 0;
2337 nmaps = SYMLINK_MAPS;
2338
2339 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2340 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2341 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002342 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (error) {
2344 goto error1;
2345 }
2346
2347 if (resblks)
2348 resblks -= fs_blocks;
2349 ip->i_d.di_size = pathlen;
2350 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2351
2352 cur_chunk = target_path;
2353 for (n = 0; n < nmaps; n++) {
2354 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2355 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2356 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2357 BTOBB(byte_cnt), 0);
2358 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2359 if (pathlen < byte_cnt) {
2360 byte_cnt = pathlen;
2361 }
2362 pathlen -= byte_cnt;
2363
2364 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2365 cur_chunk += byte_cnt;
2366
2367 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2368 }
2369 }
2370
2371 /*
2372 * Create the directory entry for the symlink.
2373 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002374 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2375 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002376 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2379 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2380
2381 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 * If this is a synchronous mount, make sure that the
2383 * symlink transaction goes to disk before returning to
2384 * the user.
2385 */
2386 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2387 xfs_trans_set_sync(tp);
2388 }
2389
2390 /*
2391 * xfs_trans_commit normally decrements the vnode ref count
2392 * when it unlocks the inode. Since we want to return the
2393 * vnode to the caller, we bump the vnode ref count now.
2394 */
2395 IHOLD(ip);
2396
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002397 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (error) {
2399 goto error2;
2400 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002401 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002402 xfs_qm_dqrele(udqp);
2403 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 /* Fall through to std_return with error = 0 or errno from
2406 * xfs_trans_commit */
2407std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10002408 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002410 dp, DM_RIGHT_NULL,
2411 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002412 DM_RIGHT_NULL, link_name->name,
2413 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415
Christoph Hellwig3937be52008-03-06 13:46:19 +11002416 if (!error)
2417 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return error;
2419
2420 error2:
2421 IRELE(ip);
2422 error1:
2423 xfs_bmap_cancel(&free_list);
2424 cancel_flags |= XFS_TRANS_ABORT;
2425 error_return:
2426 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002427 xfs_qm_dqrele(udqp);
2428 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Christoph Hellwig993386c2007-08-28 16:12:30 +10002430 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 goto std_return;
2434}
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002437xfs_set_dmattrs(
2438 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002440 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002442 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 int error;
2445
2446 if (!capable(CAP_SYS_ADMIN))
2447 return XFS_ERROR(EPERM);
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if (XFS_FORCED_SHUTDOWN(mp))
2450 return XFS_ERROR(EIO);
2451
2452 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2453 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2454 if (error) {
2455 xfs_trans_cancel(tp, 0);
2456 return error;
2457 }
2458 xfs_ilock(ip, XFS_ILOCK_EXCL);
2459 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2460
Christoph Hellwig613d7042007-10-11 17:44:08 +10002461 ip->i_d.di_dmevmask = evmask;
2462 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2465 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002466 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 return error;
2469}
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471/*
2472 * xfs_alloc_file_space()
2473 * This routine allocates disk space for the given file.
2474 *
2475 * If alloc_type == 0, this request is for an ALLOCSP type
2476 * request which will change the file size. In this case, no
2477 * DMAPI event will be generated by the call. A TRUNCATE event
2478 * will be generated later by xfs_setattr.
2479 *
2480 * If alloc_type != 0, this request is for a RESVSP type
2481 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2482 * lower block boundary byte address is less than the file's
2483 * length.
2484 *
2485 * RETURNS:
2486 * 0 on success
2487 * errno on error
2488 *
2489 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002490STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491xfs_alloc_file_space(
2492 xfs_inode_t *ip,
2493 xfs_off_t offset,
2494 xfs_off_t len,
2495 int alloc_type,
2496 int attr_flags)
2497{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002498 xfs_mount_t *mp = ip->i_mount;
2499 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 xfs_filblks_t allocated_fsb;
2501 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002502 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002504 xfs_fsblock_t firstfsb;
2505 int nimaps;
2506 int bmapi_flag;
2507 int quota_flag;
2508 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002510 xfs_bmbt_irec_t imaps[1], *imapp;
2511 xfs_bmap_free_t free_list;
2512 uint qblocks, resblks, resrtextents;
2513 int committed;
2514 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002516 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 if (XFS_FORCED_SHUTDOWN(mp))
2519 return XFS_ERROR(EIO);
2520
Christoph Hellwig7d095252009-06-08 15:33:32 +02002521 error = xfs_qm_dqattach(ip, 0);
2522 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return error;
2524
2525 if (len <= 0)
2526 return XFS_ERROR(EINVAL);
2527
David Chinner957d0eb2007-06-18 16:50:37 +10002528 rt = XFS_IS_REALTIME_INODE(ip);
2529 extsz = xfs_get_extsz_hint(ip);
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11002533 nimaps = 1;
2534 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2536 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2537
2538 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002539 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002540 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002541 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 xfs_off_t end_dmi_offset;
2543
2544 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002545 if (end_dmi_offset > ip->i_size)
2546 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002547 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2548 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002550 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552
2553 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002554 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 */
2556retry:
2557 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002558 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Nathan Scottdd9f4382006-01-11 15:28:28 +11002560 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11002561 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002562 */
2563 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002565 do_div(s, extsz);
2566 s *= extsz;
2567 e = startoffset_fsb + allocatesize_fsb;
2568 if ((temp = do_mod(startoffset_fsb, extsz)))
2569 e += temp;
2570 if ((temp = do_mod(e, extsz)))
2571 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002573 s = 0;
2574 e = allocatesize_fsb;
2575 }
2576
2577 if (unlikely(rt)) {
2578 resrtextents = qblocks = (uint)(e - s);
2579 resrtextents /= mp->m_sb.sb_rextsize;
2580 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2581 quota_flag = XFS_QMOPT_RES_RTBLKS;
2582 } else {
2583 resrtextents = 0;
2584 resblks = qblocks = \
2585 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2586 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588
2589 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002590 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 */
2592 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002593 error = xfs_trans_reserve(tp, resblks,
2594 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 XFS_TRANS_PERM_LOG_RES,
2596 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002598 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 */
2600 if (error) {
2601 /*
2602 * Free the transaction structure.
2603 */
2604 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2605 xfs_trans_cancel(tp, 0);
2606 break;
2607 }
2608 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002609 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2610 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (error)
2612 goto error1;
2613
2614 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2615 xfs_trans_ihold(tp, ip);
2616
2617 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002618 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002620 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002621 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002622 allocatesize_fsb, bmapi_flag,
2623 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002624 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (error) {
2626 goto error0;
2627 }
2628
2629 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002630 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002632 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (error) {
2634 goto error0;
2635 }
2636
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002637 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2639 if (error) {
2640 break;
2641 }
2642
2643 allocated_fsb = imapp->br_blockcount;
2644
Nathan Scottdd9f4382006-01-11 15:28:28 +11002645 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 error = XFS_ERROR(ENOSPC);
2647 break;
2648 }
2649
2650 startoffset_fsb += allocated_fsb;
2651 allocatesize_fsb -= allocated_fsb;
2652 }
2653dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002654 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002655 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002657 ip, DM_RIGHT_NULL,
2658 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
2660 if (error == 0)
2661 goto retry; /* Maybe DMAPI app. has made space */
2662 /* else fall through with error from XFS_SEND_DATA */
2663 }
2664
2665 return error;
2666
Nathan Scottdd9f4382006-01-11 15:28:28 +11002667error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002669 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002670
2671error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2673 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2674 goto dmapi_enospc_check;
2675}
2676
2677/*
2678 * Zero file bytes between startoff and endoff inclusive.
2679 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002680 *
2681 * This function is used by xfs_free_file_space() to zero
2682 * partial blocks when the range to free is not block aligned.
2683 * When unreserving space with boundaries that are not block
2684 * aligned we round up the start and round down the end
2685 * boundaries and then use this function to zero the parts of
2686 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 */
2688STATIC int
2689xfs_zero_remaining_bytes(
2690 xfs_inode_t *ip,
2691 xfs_off_t startoff,
2692 xfs_off_t endoff)
2693{
2694 xfs_bmbt_irec_t imap;
2695 xfs_fileoff_t offset_fsb;
2696 xfs_off_t lastoffset;
2697 xfs_off_t offset;
2698 xfs_buf_t *bp;
2699 xfs_mount_t *mp = ip->i_mount;
2700 int nimap;
2701 int error = 0;
2702
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002703 /*
2704 * Avoid doing I/O beyond eof - it's not necessary
2705 * since nothing can read beyond eof. The space will
2706 * be zeroed when the file is extended anyway.
2707 */
2708 if (startoff >= ip->i_size)
2709 return 0;
2710
2711 if (endoff > ip->i_size)
2712 endoff = ip->i_size;
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002715 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 mp->m_rtdev_targp : mp->m_ddev_targp);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11002717 if (!bp)
2718 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2721 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2722 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002723 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002724 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (error || nimap < 1)
2726 break;
2727 ASSERT(imap.br_blockcount >= 1);
2728 ASSERT(imap.br_startoff == offset_fsb);
2729 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2730 if (lastoffset > endoff)
2731 lastoffset = endoff;
2732 if (imap.br_startblock == HOLESTARTBLOCK)
2733 continue;
2734 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2735 if (imap.br_state == XFS_EXT_UNWRITTEN)
2736 continue;
2737 XFS_BUF_UNDONE(bp);
2738 XFS_BUF_UNWRITE(bp);
2739 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002740 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002742 error = xfs_iowait(bp);
2743 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2745 mp, bp, XFS_BUF_ADDR(bp));
2746 break;
2747 }
2748 memset(XFS_BUF_PTR(bp) +
2749 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2750 0, lastoffset - offset + 1);
2751 XFS_BUF_UNDONE(bp);
2752 XFS_BUF_UNREAD(bp);
2753 XFS_BUF_WRITE(bp);
2754 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002755 error = xfs_iowait(bp);
2756 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2758 mp, bp, XFS_BUF_ADDR(bp));
2759 break;
2760 }
2761 }
2762 xfs_buf_free(bp);
2763 return error;
2764}
2765
2766/*
2767 * xfs_free_file_space()
2768 * This routine frees disk space for the given file.
2769 *
2770 * This routine is only called by xfs_change_file_space
2771 * for an UNRESVSP type call.
2772 *
2773 * RETURNS:
2774 * 0 on success
2775 * errno on error
2776 *
2777 */
2778STATIC int
2779xfs_free_file_space(
2780 xfs_inode_t *ip,
2781 xfs_off_t offset,
2782 xfs_off_t len,
2783 int attr_flags)
2784{
2785 int committed;
2786 int done;
2787 xfs_off_t end_dmi_offset;
2788 xfs_fileoff_t endoffset_fsb;
2789 int error;
2790 xfs_fsblock_t firstfsb;
2791 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 xfs_bmbt_irec_t imap;
2793 xfs_off_t ioffset;
2794 xfs_extlen_t mod=0;
2795 xfs_mount_t *mp;
2796 int nimap;
2797 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002798 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 int rt;
2800 xfs_fileoff_t startoffset_fsb;
2801 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002802 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 mp = ip->i_mount;
2805
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002806 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002807
Christoph Hellwig7d095252009-06-08 15:33:32 +02002808 error = xfs_qm_dqattach(ip, 0);
2809 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return error;
2811
2812 error = 0;
2813 if (len <= 0) /* if nothing being freed */
2814 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002815 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2817 end_dmi_offset = offset + len;
2818 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
2819
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002820 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002821 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002822 if (end_dmi_offset > ip->i_size)
2823 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002824 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 offset, end_dmi_offset - offset,
2826 AT_DELAY_FLAG(attr_flags), NULL);
2827 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002828 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002831 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002832 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002833 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002835 /* wait for the completion of any pending DIOs */
2836 xfs_ioend_wait(ip);
Yingping Lu9fa80462006-03-22 12:44:35 +11002837 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002838
Tim Shimmine6a4b372007-11-23 16:30:42 +11002839 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002841
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002842 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002843 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10002844 if (error)
2845 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002846 }
2847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 /*
2849 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002850 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2852 * will take care of it for us.
2853 */
Eric Sandeen62118702008-03-06 13:44:28 +11002854 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002856 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002857 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (error)
2859 goto out_unlock_iolock;
2860 ASSERT(nimap == 0 || nimap == 1);
2861 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2862 xfs_daddr_t block;
2863
2864 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2865 block = imap.br_startblock;
2866 mod = do_div(block, mp->m_sb.sb_rextsize);
2867 if (mod)
2868 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2869 }
2870 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002871 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002872 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (error)
2874 goto out_unlock_iolock;
2875 ASSERT(nimap == 0 || nimap == 1);
2876 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2877 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2878 mod++;
2879 if (mod && (mod != mp->m_sb.sb_rextsize))
2880 endoffset_fsb -= mod;
2881 }
2882 }
2883 if ((done = (endoffset_fsb <= startoffset_fsb)))
2884 /*
2885 * One contiguous piece to clear
2886 */
2887 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2888 else {
2889 /*
2890 * Some full blocks, possibly two pieces to clear
2891 */
2892 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2893 error = xfs_zero_remaining_bytes(ip, offset,
2894 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2895 if (!error &&
2896 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2897 error = xfs_zero_remaining_bytes(ip,
2898 XFS_FSB_TO_B(mp, endoffset_fsb),
2899 offset + len - 1);
2900 }
2901
2902 /*
2903 * free file space until done or until there is an error
2904 */
2905 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2906 while (!error && !done) {
2907
2908 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002909 * allocate and setup the transaction. Allow this
2910 * transaction to dip into the reserve blocks to ensure
2911 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 */
2913 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002914 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 error = xfs_trans_reserve(tp,
2916 resblks,
2917 XFS_WRITE_LOG_RES(mp),
2918 0,
2919 XFS_TRANS_PERM_LOG_RES,
2920 XFS_WRITE_LOG_COUNT);
2921
2922 /*
2923 * check for running out of space
2924 */
2925 if (error) {
2926 /*
2927 * Free the transaction structure.
2928 */
2929 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2930 xfs_trans_cancel(tp, 0);
2931 break;
2932 }
2933 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002934 error = xfs_trans_reserve_quota(tp, mp,
2935 ip->i_udquot, ip->i_gdquot,
2936 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (error)
2938 goto error1;
2939
2940 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2941 xfs_trans_ihold(tp, ip);
2942
2943 /*
2944 * issue the bunmapi() call to free the blocks
2945 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002946 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002947 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002949 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (error) {
2951 goto error0;
2952 }
2953
2954 /*
2955 * complete the transaction
2956 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002957 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (error) {
2959 goto error0;
2960 }
2961
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002962 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2964 }
2965
2966 out_unlock_iolock:
2967 if (need_iolock)
2968 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2969 return error;
2970
2971 error0:
2972 xfs_bmap_cancel(&free_list);
2973 error1:
2974 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2975 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2976 XFS_ILOCK_EXCL);
2977 return error;
2978}
2979
2980/*
2981 * xfs_change_file_space()
2982 * This routine allocates or frees disk space for the given file.
2983 * The user specified parameters are checked for alignment and size
2984 * limitations.
2985 *
2986 * RETURNS:
2987 * 0 on success
2988 * errno on error
2989 *
2990 */
2991int
2992xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002993 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 int cmd,
2995 xfs_flock64_t *bf,
2996 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 int attr_flags)
2998{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002999 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 int clrprealloc;
3001 int error;
3002 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 int setprealloc;
3004 xfs_off_t startoffset;
3005 xfs_off_t llen;
3006 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003007 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003009 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Christoph Hellwig993386c2007-08-28 16:12:30 +10003011 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return XFS_ERROR(EINVAL);
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 switch (bf->l_whence) {
3015 case 0: /*SEEK_SET*/
3016 break;
3017 case 1: /*SEEK_CUR*/
3018 bf->l_start += offset;
3019 break;
3020 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003021 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 break;
3023 default:
3024 return XFS_ERROR(EINVAL);
3025 }
3026
3027 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3028
3029 if ( (bf->l_start < 0)
3030 || (bf->l_start > XFS_MAXIOFFSET(mp))
3031 || (bf->l_start + llen < 0)
3032 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3033 return XFS_ERROR(EINVAL);
3034
3035 bf->l_whence = 0;
3036
3037 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003038 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 /*
3041 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3042 * file space.
3043 * These calls do NOT zero the data space allocated to the file,
3044 * nor do they change the file size.
3045 *
3046 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3047 * space.
3048 * These calls cause the new file data to be zeroed and the file
3049 * size to be changed.
3050 */
3051 setprealloc = clrprealloc = 0;
3052
3053 switch (cmd) {
3054 case XFS_IOC_RESVSP:
3055 case XFS_IOC_RESVSP64:
3056 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3057 1, attr_flags);
3058 if (error)
3059 return error;
3060 setprealloc = 1;
3061 break;
3062
3063 case XFS_IOC_UNRESVSP:
3064 case XFS_IOC_UNRESVSP64:
3065 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3066 attr_flags)))
3067 return error;
3068 break;
3069
3070 case XFS_IOC_ALLOCSP:
3071 case XFS_IOC_ALLOCSP64:
3072 case XFS_IOC_FREESP:
3073 case XFS_IOC_FREESP64:
3074 if (startoffset > fsize) {
3075 error = xfs_alloc_file_space(ip, fsize,
3076 startoffset - fsize, 0, attr_flags);
3077 if (error)
3078 break;
3079 }
3080
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003081 iattr.ia_valid = ATTR_SIZE;
3082 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +11003084 error = xfs_setattr(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 if (error)
3087 return error;
3088
3089 clrprealloc = 1;
3090 break;
3091
3092 default:
3093 ASSERT(0);
3094 return XFS_ERROR(EINVAL);
3095 }
3096
3097 /*
3098 * update the inode timestamp, mode, and prealloc flag bits
3099 */
3100 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3101
3102 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3103 0, 0, 0))) {
3104 /* ASSERT(0); */
3105 xfs_trans_cancel(tp, 0);
3106 return error;
3107 }
3108
3109 xfs_ilock(ip, XFS_ILOCK_EXCL);
3110
3111 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3112 xfs_trans_ihold(tp, ip);
3113
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003114 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 ip->i_d.di_mode &= ~S_ISUID;
3116
3117 /*
3118 * Note that we don't have to worry about mandatory
3119 * file locking being disabled here because we only
3120 * clear the S_ISGID bit if the Group execute bit is
3121 * on, but if it was on then mandatory locking wouldn't
3122 * have been enabled.
3123 */
3124 if (ip->i_d.di_mode & S_IXGRP)
3125 ip->i_d.di_mode &= ~S_ISGID;
3126
3127 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3128 }
3129 if (setprealloc)
3130 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3131 else if (clrprealloc)
3132 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3133
3134 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3135 xfs_trans_set_sync(tp);
3136
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003137 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3140
3141 return error;
3142}