blob: fd108b7385594cac9310518caf74b6e592ea7152 [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 Hellwig993386c12007-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 Hellwig993386c12007-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 Hellwig993386c12007-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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
Dean Roehrich5fcbab32005-05-05 13:27:19 -070074 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Lachlan McIlroycf441ee2008-02-07 16:42:19 +110076 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Christoph Hellwigbd186aa2007-08-30 17:21:12 +100078 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return XFS_ERROR(EROFS);
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (XFS_FORCED_SHUTDOWN(mp))
82 return XFS_ERROR(EIO);
83
Christoph Hellwigc4cd7472008-12-09 04:47:34 -050084 code = -inode_change_ok(inode, iattr);
85 if (code)
86 return code;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 olddquot1 = olddquot2 = NULL;
89 udqp = gdqp = NULL;
90
91 /*
92 * If disk quotas is on, we make sure that the dquots do exist on disk,
93 * before we start any other transactions. Trying to do this later
94 * is messy. We don't care to take a readlock to look at the ids
95 * in inode here, because we can't hold it across the trans_reserve.
96 * If the IDs do change before we take the ilock, we're covered
97 * because the i_*dquot fields will get updated anyway.
98 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +100099 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 uint qflags = 0;
101
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000102 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
103 uid = iattr->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 qflags |= XFS_QMOPT_UQUOTA;
105 } else {
106 uid = ip->i_d.di_uid;
107 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000108 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
109 gid = iattr->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 qflags |= XFS_QMOPT_GQUOTA;
111 } else {
112 gid = ip->i_d.di_gid;
113 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /*
116 * We take a reference when we initialize udqp and gdqp,
117 * so it is important that we never blindly double trip on
118 * the same variable. See xfs_create() for an example.
119 */
120 ASSERT(udqp == NULL);
121 ASSERT(gdqp == NULL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200122 code = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid,
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000123 qflags, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100125 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
128 /*
129 * For the other attributes, we acquire the inode lock and
130 * first do an error checking pass.
131 */
132 tp = NULL;
133 lock_flags = XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000134 if (flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700135 need_iolock = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000136 if (!(mask & ATTR_SIZE)) {
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000137 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
138 commit_flags = 0;
139 code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp),
140 0, 0, 0);
141 if (code) {
142 lock_flags = 0;
143 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000146 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000147 !(flags & XFS_ATTR_DMI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100149 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000150 iattr->ia_size, 0, dmflags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (code) {
152 lock_flags = 0;
153 goto error_return;
154 }
155 }
156 if (need_iolock)
157 lock_flags |= XFS_IOLOCK_EXCL;
158 }
159
160 xfs_ilock(ip, lock_flags);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /*
163 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000165 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /*
167 * These IDs could have changed since we last looked at them.
168 * But, we're assured that if the ownership did change
169 * while we didn't have the inode locked, inode's dquot(s)
170 * would have changed also.
171 */
172 iuid = ip->i_d.di_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 igid = ip->i_d.di_gid;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000174 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
175 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000178 * Do a quota reservation only if uid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * going to change.
180 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200181 if (XFS_IS_QUOTA_RUNNING(mp) &&
182 ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
183 (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 ASSERT(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200185 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 capable(CAP_FOWNER) ?
187 XFS_QMOPT_FORCE_RES : 0);
188 if (code) /* out of quota */
189 goto error_return;
190 }
191 }
192
193 /*
194 * Truncate file. Must have write permission and not be a directory.
195 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000196 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* Short circuit the truncate case for zero length files */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000198 if (iattr->ia_size == 0 &&
199 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 xfs_iunlock(ip, XFS_ILOCK_EXCL);
201 lock_flags &= ~XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000202 if (mask & ATTR_CTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
204 code = 0;
205 goto error_return;
206 }
207
Christoph Hellwig42173f62008-04-22 17:33:25 +1000208 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 code = XFS_ERROR(EISDIR);
210 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000211 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 code = XFS_ERROR(EINVAL);
213 goto error_return;
214 }
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /*
217 * Make sure that the dquots are attached to the inode.
218 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200219 code = xfs_qm_dqattach_locked(ip, 0);
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500220 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500223 /*
224 * Now we can make the changes. Before we join the inode
225 * to the transaction, if ATTR_SIZE is set then take care of
226 * the part of the truncation that must be done without the
227 * inode lock. This needs to be done before joining the inode
228 * to the transaction, because the inode cannot be unlocked
229 * once it is a part of the transaction.
230 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000231 if (iattr->ia_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000232 /*
233 * Do the first part of growing a file: zero any data
234 * in the last block that is beyond the old EOF. We
235 * need to do this before the inode is joined to the
236 * transaction to modify the i_size.
237 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000238 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000241
242 /*
243 * We are going to log the inode size change in this
244 * transaction so any previous writes that are beyond the on
245 * disk EOF and the new EOF that have not been written out need
246 * to be written here. If we do not write the data out, we
247 * expose ourselves to the null files problem.
248 *
249 * Only flush from the on disk size to the smaller of the in
250 * memory file size or the new size as that's the range we
251 * really care about here and prevents waiting for other data
252 * not within the range we care about here.
253 */
254 if (!code &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000255 ip->i_size != ip->i_d.di_size &&
256 iattr->ia_size > ip->i_d.di_size) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000257 code = xfs_flush_pages(ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000258 ip->i_d.di_size, iattr->ia_size,
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000259 XBF_ASYNC, FI_NONE);
David Chinnerc32676e2007-07-19 16:28:58 +1000260 }
261
262 /* wait for all I/O to complete */
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100263 xfs_ioend_wait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (!code)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000266 code = xfs_itruncate_data(ip, iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (code) {
268 ASSERT(tp == NULL);
269 lock_flags &= ~XFS_ILOCK_EXCL;
270 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
271 goto error_return;
272 }
273 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
274 if ((code = xfs_trans_reserve(tp, 0,
275 XFS_ITRUNCATE_LOG_RES(mp), 0,
276 XFS_TRANS_PERM_LOG_RES,
277 XFS_ITRUNCATE_LOG_COUNT))) {
278 xfs_trans_cancel(tp, 0);
279 if (need_iolock)
280 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
281 return code;
282 }
283 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
284 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 xfs_trans_ijoin(tp, ip, lock_flags);
287 xfs_trans_ihold(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
David Chinner44d814c2008-03-06 13:45:29 +1100289 /*
290 * Only change the c/mtime if we are changing the size
291 * or we are explicitly asked to change it. This handles
292 * the semantic difference between truncate() and ftruncate()
293 * as implemented in the VFS.
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000294 *
295 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME
296 * is a special case where we need to update the times despite
297 * not having these flags set. For all other operations the
298 * VFS set these flags explicitly if it wants a timestamp
299 * update.
David Chinner44d814c2008-03-06 13:45:29 +1100300 */
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000301 if (iattr->ia_size != ip->i_size &&
302 (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
303 iattr->ia_ctime = iattr->ia_mtime =
304 current_fs_time(inode->i_sb);
305 mask |= ATTR_CTIME | ATTR_MTIME;
306 }
David Chinner44d814c2008-03-06 13:45:29 +1100307
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000308 if (iattr->ia_size > ip->i_size) {
309 ip->i_d.di_size = iattr->ia_size;
310 ip->i_size = iattr->ia_size;
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000311 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000312 } else if (iattr->ia_size <= ip->i_size ||
313 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /*
315 * signal a sync transaction unless
316 * we're truncating an already unlinked
317 * file on a wsync filesystem
318 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000319 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 XFS_DATA_FORK,
321 ((ip->i_d.di_nlink != 0 ||
322 !(mp->m_flags & XFS_MOUNT_WSYNC))
323 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000324 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000326 /*
327 * Truncated "down", so we're removing references
328 * to old data here - if we now delay flushing for
329 * a long time, we expose ourselves unduly to the
330 * notorious NULL files problem. So, we mark this
331 * vnode and flush it when the file is closed, and
332 * do not wait the usual (long) time for writeout.
333 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000334 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500336 } else if (tp) {
337 xfs_trans_ijoin(tp, ip, lock_flags);
338 xfs_trans_ihold(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341 /*
342 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000344 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /*
346 * CAP_FSETID overrides the following restrictions:
347 *
348 * The set-user-ID and set-group-ID bits of a file will be
349 * cleared upon successful return from chown()
350 */
351 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
352 !capable(CAP_FSETID)) {
353 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
354 }
355
356 /*
357 * Change the ownerships and register quota modifications
358 * in the transaction.
359 */
360 if (iuid != uid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200361 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000362 ASSERT(mask & ATTR_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ASSERT(udqp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200364 olddquot1 = xfs_qm_vop_chown(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 &ip->i_udquot, udqp);
366 }
367 ip->i_d.di_uid = uid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000368 inode->i_uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370 if (igid != gid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200371 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000372 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000373 ASSERT(mask & ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ASSERT(gdqp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200375 olddquot2 = xfs_qm_vop_chown(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 &ip->i_gdquot, gdqp);
377 }
378 ip->i_d.di_gid = gid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000379 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500383 /*
384 * Change file access modes.
385 */
386 if (mask & ATTR_MODE) {
387 umode_t mode = iattr->ia_mode;
388
389 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
390 mode &= ~S_ISGID;
391
392 ip->i_d.di_mode &= S_IFMT;
393 ip->i_d.di_mode |= mode & ~S_IFMT;
394
395 inode->i_mode &= S_IFMT;
396 inode->i_mode |= mode & ~S_IFMT;
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /*
400 * Change file access or modified times.
401 */
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000402 if (mask & ATTR_ATIME) {
403 inode->i_atime = iattr->ia_atime;
404 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
405 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
406 ip->i_update_core = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000408 if (mask & ATTR_CTIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000409 inode->i_ctime = iattr->ia_ctime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000410 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
411 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 ip->i_update_core = 1;
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000413 }
414 if (mask & ATTR_MTIME) {
415 inode->i_mtime = iattr->ia_mtime;
416 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
417 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
418 ip->i_update_core = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
421 /*
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000422 * And finally, log the inode core if any attribute in it
423 * has been changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000425 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE|
426 ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
427 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 XFS_STATS_INC(xs_ig_attrchg);
430
431 /*
432 * If this is a synchronous mount, make sure that the
433 * transaction goes to disk before returning to the user.
434 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000435 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * One for the truncate and one for the timestamps since we
437 * don't want to change the timestamps unless we're sure the
438 * truncate worked. Truncates are less than 1% of the laddis
439 * mix so this probably isn't worth the trouble to optimize.
440 */
441 code = 0;
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000442 if (mp->m_flags & XFS_MOUNT_WSYNC)
443 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000445 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 xfs_iunlock(ip, lock_flags);
448
449 /*
450 * Release any dquot(s) the inode had kept before chown.
451 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200452 xfs_qm_dqrele(olddquot1);
453 xfs_qm_dqrele(olddquot2);
454 xfs_qm_dqrele(udqp);
455 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200457 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return code;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200459
460 /*
461 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
462 * update. We could avoid this with linked transactions
463 * and passing down the transaction pointer all the way
464 * to attr_set. No previous user of the generic
465 * Posix ACL code seems to care about this issue either.
466 */
467 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
468 code = -xfs_acl_chmod(inode);
469 if (code)
470 return XFS_ERROR(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000473 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000474 !(flags & XFS_ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100475 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 NULL, DM_RIGHT_NULL, NULL, NULL,
477 0, 0, AT_DELAY_FLAG(flags));
478 }
479 return 0;
480
481 abort_return:
482 commit_flags |= XFS_TRANS_ABORT;
483 /* FALLTHROUGH */
484 error_return:
Christoph Hellwig7d095252009-06-08 15:33:32 +0200485 xfs_qm_dqrele(udqp);
486 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (tp) {
488 xfs_trans_cancel(tp, commit_flags);
489 }
490 if (lock_flags != 0) {
491 xfs_iunlock(ip, lock_flags);
492 }
493 return code;
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000497 * The maximum pathlen is 1024 bytes. Since the minimum file system
498 * blocksize is 512 bytes, we can get a max of 2 extents back from
499 * bmapi.
500 */
501#define SYMLINK_MAPS 2
502
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000503STATIC int
504xfs_readlink_bmap(
505 xfs_inode_t *ip,
506 char *link)
507{
508 xfs_mount_t *mp = ip->i_mount;
509 int pathlen = ip->i_d.di_size;
510 int nmaps = SYMLINK_MAPS;
511 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
512 xfs_daddr_t d;
513 int byte_cnt;
514 int n;
515 xfs_buf_t *bp;
516 int error = 0;
517
518 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
519 mval, &nmaps, NULL, NULL);
520 if (error)
521 goto out;
522
523 for (n = 0; n < nmaps; n++) {
524 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
525 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
526
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000527 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
528 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000529 error = XFS_BUF_GETERROR(bp);
530 if (error) {
531 xfs_ioerror_alert("xfs_readlink",
532 ip->i_mount, bp, XFS_BUF_ADDR(bp));
533 xfs_buf_relse(bp);
534 goto out;
535 }
536 if (pathlen < byte_cnt)
537 byte_cnt = pathlen;
538 pathlen -= byte_cnt;
539
540 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
541 xfs_buf_relse(bp);
542 }
543
544 link[ip->i_d.di_size] = '\0';
545 error = 0;
546
547 out:
548 return error;
549}
550
Christoph Hellwig993386c12007-08-28 16:12:30 +1000551int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552xfs_readlink(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000553 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000554 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000556 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100560 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (XFS_FORCED_SHUTDOWN(mp))
563 return XFS_ERROR(EIO);
564
565 xfs_ilock(ip, XFS_ILOCK_SHARED);
566
567 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000568 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000570 pathlen = ip->i_d.di_size;
571 if (!pathlen)
572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000575 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
576 link[pathlen] = '\0';
577 } else {
578 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000581 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return error;
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * xfs_fsync
588 *
David Chinner978b7232008-05-19 16:29:46 +1000589 * This is called to sync the inode and its data out to disk. We need to hold
590 * the I/O lock while flushing the data, and the inode lock while flushing the
591 * inode. The inode lock CANNOT be held while flushing the data, so acquire
592 * after we're done with that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Christoph Hellwig993386c12007-08-28 16:12:30 +1000594int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595xfs_fsync(
David Chinner978b7232008-05-19 16:29:46 +1000596 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 xfs_trans_t *tp;
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300599 int error = 0;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100600 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100602 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
605 return XFS_ERROR(EIO);
606
607 /*
David Chinner978b7232008-05-19 16:29:46 +1000608 * We always need to make sure that the required inode state is safe on
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300609 * disk. The inode might be clean but we still might need to force the
David Chinner978b7232008-05-19 16:29:46 +1000610 * log because of committed transactions that haven't hit the disk yet.
611 * Likewise, there could be unflushed non-transactional changes to the
612 * inode core that have to go to disk and this requires us to issue
613 * a synchronous transaction to capture these changes correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *
David Chinner978b7232008-05-19 16:29:46 +1000615 * This code relies on the assumption that if the update_* fields
616 * of the inode are clear and the inode is unpinned then it is clean
617 * and no action is required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
619 xfs_ilock(ip, XFS_ILOCK_SHARED);
620
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300621 if (!ip->i_update_core) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
David Chinner978b7232008-05-19 16:29:46 +1000623 * Timestamps/size haven't changed since last inode flush or
624 * inode transaction commit. That means either nothing got
625 * written or a transaction committed which caught the updates.
626 * If the latter happened and the transaction hasn't hit the
627 * disk yet, the inode will be still be pinned. If it is,
628 * force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
630
631 xfs_iunlock(ip, XFS_ILOCK_SHARED);
632
633 if (xfs_ipincount(ip)) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000634 error = _xfs_log_force(ip->i_mount, XFS_LOG_SYNC,
635 &log_flushed);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100636 } else {
637 /*
David Chinner978b7232008-05-19 16:29:46 +1000638 * If the inode is not pinned and nothing has changed
639 * we don't need to flush the cache.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100640 */
641 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 } else {
644 /*
David Chinner978b7232008-05-19 16:29:46 +1000645 * Kick off a transaction to log the inode core to get the
646 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
648 xfs_iunlock(ip, XFS_ILOCK_SHARED);
649 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000650 error = xfs_trans_reserve(tp, 0,
651 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
652 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 xfs_trans_cancel(tp, 0);
654 return error;
655 }
656 xfs_ilock(ip, XFS_ILOCK_EXCL);
657
658 /*
David Chinner978b7232008-05-19 16:29:46 +1000659 * Note - it's possible that we might have pushed ourselves out
660 * of the way during trans_reserve which would flush the inode.
661 * But there's no guarantee that the inode buffer has actually
662 * gone out yet (it's delwri). Plus the buffer could be pinned
663 * anyway if it's part of an inode in another recent
664 * transaction. So we play it safe and fire off the
665 * transaction anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
667 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
668 xfs_trans_ihold(tp, ip);
669 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
David Chinner978b7232008-05-19 16:29:46 +1000670 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000671 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 xfs_iunlock(ip, XFS_ILOCK_EXCL);
674 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100675
676 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
677 /*
678 * If the log write didn't issue an ordered tag we need
679 * to flush the disk cache for the data device now.
680 */
681 if (!log_flushed)
682 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
683
684 /*
685 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000686 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100687 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100688 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100689 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
690 }
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return error;
693}
694
695/*
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000696 * Flags for xfs_free_eofblocks
697 */
698#define XFS_FREE_EOF_TRYLOCK (1<<0)
699
700/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000701 * This is called by xfs_inactive to free any blocks beyond eof
702 * when the link count isn't zero and by xfs_dm_punch_hole() when
703 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
Eric Sandeend96f8f82009-07-02 00:09:33 -0500705STATIC int
David Chinner92dfe8d2007-05-24 15:22:19 +1000706xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000708 xfs_inode_t *ip,
709 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 xfs_trans_t *tp;
712 int error;
713 xfs_fileoff_t end_fsb;
714 xfs_fileoff_t last_fsb;
715 xfs_filblks_t map_len;
716 int nimaps;
717 xfs_bmbt_irec_t imap;
718
719 /*
720 * Figure out if there are any blocks beyond the end
721 * of the file. If not, then there is nothing to do.
722 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000723 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
725 map_len = last_fsb - end_fsb;
726 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 nimaps = 1;
730 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000731 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000732 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 xfs_iunlock(ip, XFS_ILOCK_SHARED);
734
735 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100736 (imap.br_startblock != HOLESTARTBLOCK ||
737 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /*
739 * Attach the dquots to the inode up front.
740 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200741 error = xfs_qm_dqattach(ip, 0);
742 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100743 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /*
746 * There are blocks after the end of file.
747 * Free them up now by truncating the file to
748 * its current size.
749 */
750 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
751
752 /*
753 * Do the xfs_itruncate_start() call before
754 * reserving any log space because
755 * itruncate_start will call into the buffer
756 * cache and we can't
757 * do that within a transaction.
758 */
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000759 if (flags & XFS_FREE_EOF_TRYLOCK) {
760 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
761 xfs_trans_cancel(tp, 0);
762 return 0;
763 }
764 } else {
David Chinner92dfe8d2007-05-24 15:22:19 +1000765 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000766 }
Lachlan McIlroyd3cf20942007-05-08 13:49:27 +1000767 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000768 ip->i_size);
Lachlan McIlroyd3cf20942007-05-08 13:49:27 +1000769 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000770 xfs_trans_cancel(tp, 0);
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000771 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf20942007-05-08 13:49:27 +1000772 return error;
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 error = xfs_trans_reserve(tp, 0,
776 XFS_ITRUNCATE_LOG_RES(mp),
777 0, XFS_TRANS_PERM_LOG_RES,
778 XFS_ITRUNCATE_LOG_COUNT);
779 if (error) {
780 ASSERT(XFS_FORCED_SHUTDOWN(mp));
781 xfs_trans_cancel(tp, 0);
782 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100783 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
786 xfs_ilock(ip, XFS_ILOCK_EXCL);
787 xfs_trans_ijoin(tp, ip,
788 XFS_IOLOCK_EXCL |
789 XFS_ILOCK_EXCL);
790 xfs_trans_ihold(tp, ip);
791
792 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000793 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 XFS_DATA_FORK,
795 0);
796 /*
797 * If we get an error at this point we
798 * simply don't bother truncating the file.
799 */
800 if (error) {
801 xfs_trans_cancel(tp,
802 (XFS_TRANS_RELEASE_LOG_RES |
803 XFS_TRANS_ABORT));
804 } else {
805 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000806 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000808 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100810 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/*
814 * Free a symlink that has blocks associated with it.
815 */
816STATIC int
817xfs_inactive_symlink_rmt(
818 xfs_inode_t *ip,
819 xfs_trans_t **tpp)
820{
821 xfs_buf_t *bp;
822 int committed;
823 int done;
824 int error;
825 xfs_fsblock_t first_block;
826 xfs_bmap_free_t free_list;
827 int i;
828 xfs_mount_t *mp;
829 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
830 int nmaps;
831 xfs_trans_t *ntp;
832 int size;
833 xfs_trans_t *tp;
834
835 tp = *tpp;
836 mp = ip->i_mount;
837 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
838 /*
839 * We're freeing a symlink that has some
840 * blocks allocated to it. Free the
841 * blocks here. We know that we've got
842 * either 1 or 2 extents and that we can
843 * free them all in one bunmapi call.
844 */
845 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
846 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
847 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
848 ASSERT(XFS_FORCED_SHUTDOWN(mp));
849 xfs_trans_cancel(tp, 0);
850 *tpp = NULL;
851 return error;
852 }
853 /*
854 * Lock the inode, fix the size, and join it to the transaction.
855 * Hold it so in the normal path, we still have it locked for
856 * the second transaction. In the error paths we need it
857 * held so the cancel won't rele it, see below.
858 */
859 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
860 size = (int)ip->i_d.di_size;
861 ip->i_d.di_size = 0;
862 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
863 xfs_trans_ihold(tp, ip);
864 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
865 /*
866 * Find the block(s) so we can inval and unmap them.
867 */
868 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600869 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800870 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
872 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000873 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 goto error0;
875 /*
876 * Invalidate the block(s).
877 */
878 for (i = 0; i < nmaps; i++) {
879 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
880 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
881 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
882 xfs_trans_binval(tp, bp);
883 }
884 /*
885 * Unmap the dead block(s) to the free_list.
886 */
887 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000888 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 goto error1;
890 ASSERT(done);
891 /*
892 * Commit the first transaction. This logs the EFI and the inode.
893 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100894 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 goto error1;
896 /*
897 * The transaction must have been committed, since there were
898 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
899 * The new tp has the extent freeing and EFDs.
900 */
901 ASSERT(committed);
902 /*
903 * The first xact was committed, so add the inode to the new one.
904 * Mark it dirty so it will be logged and moved forward in the log as
905 * part of every commit.
906 */
907 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
908 xfs_trans_ihold(tp, ip);
909 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
910 /*
911 * Get a new, empty transaction to return to our caller.
912 */
913 ntp = xfs_trans_dup(tp);
914 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000915 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * If we get an error on the commit here or on the reserve below,
917 * we need to unlock the inode since the new transaction doesn't
918 * have the inode attached.
919 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000920 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 tp = ntp;
922 if (error) {
923 ASSERT(XFS_FORCED_SHUTDOWN(mp));
924 goto error0;
925 }
926 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100927 * transaction commit worked ok so we can drop the extra ticket
928 * reference that we gained in xfs_trans_dup()
929 */
930 xfs_log_ticket_put(tp->t_ticket);
931
932 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * Remove the memory for extent descriptions (just bookkeeping).
934 */
935 if (ip->i_df.if_bytes)
936 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
937 ASSERT(ip->i_df.if_bytes == 0);
938 /*
939 * Put an itruncate log reservation in the new transaction
940 * for our caller.
941 */
942 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
943 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
944 ASSERT(XFS_FORCED_SHUTDOWN(mp));
945 goto error0;
946 }
947 /*
948 * Return with the inode locked but not joined to the transaction.
949 */
950 *tpp = tp;
951 return 0;
952
953 error1:
954 xfs_bmap_cancel(&free_list);
955 error0:
956 /*
957 * Have to come here with the inode locked and either
958 * (held and in the transaction) or (not in the transaction).
959 * If the inode isn't held then cancel would iput it, but
960 * that's wrong since this is inactive and the vnode ref
961 * count is 0 already.
962 * Cancel won't do anything to the inode if held, but it still
963 * needs to be locked until the cancel is done, if it was
964 * joined to the transaction.
965 */
966 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
967 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
968 *tpp = NULL;
969 return error;
970
971}
972
973STATIC int
974xfs_inactive_symlink_local(
975 xfs_inode_t *ip,
976 xfs_trans_t **tpp)
977{
978 int error;
979
980 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
981 /*
982 * We're freeing a symlink which fit into
983 * the inode. Just free the memory used
984 * to hold the old symlink.
985 */
986 error = xfs_trans_reserve(*tpp, 0,
987 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
988 0, XFS_TRANS_PERM_LOG_RES,
989 XFS_ITRUNCATE_LOG_COUNT);
990
991 if (error) {
992 xfs_trans_cancel(*tpp, 0);
993 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100994 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
997
998 /*
999 * Zero length symlinks _can_ exist.
1000 */
1001 if (ip->i_df.if_bytes > 0) {
1002 xfs_idata_realloc(ip,
1003 -(ip->i_df.if_bytes),
1004 XFS_DATA_FORK);
1005 ASSERT(ip->i_df.if_bytes == 0);
1006 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010STATIC int
1011xfs_inactive_attrs(
1012 xfs_inode_t *ip,
1013 xfs_trans_t **tpp)
1014{
1015 xfs_trans_t *tp;
1016 int error;
1017 xfs_mount_t *mp;
1018
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001019 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 tp = *tpp;
1021 mp = ip->i_mount;
1022 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001023 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001025 if (error)
1026 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001029 if (error)
1030 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1033 error = xfs_trans_reserve(tp, 0,
1034 XFS_IFREE_LOG_RES(mp),
1035 0, XFS_TRANS_PERM_LOG_RES,
1036 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001037 if (error)
1038 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 xfs_ilock(ip, XFS_ILOCK_EXCL);
1041 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1042 xfs_trans_ihold(tp, ip);
1043 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1044
1045 ASSERT(ip->i_d.di_anextents == 0);
1046
1047 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001048 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001049
1050error_cancel:
1051 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1052 xfs_trans_cancel(tp, 0);
1053error_unlock:
1054 *tpp = NULL;
1055 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1056 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Christoph Hellwig993386c12007-08-28 16:12:30 +10001059int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060xfs_release(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001061 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001063 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 int error;
1065
Christoph Hellwig42173f62008-04-22 17:33:25 +10001066 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001070 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return 0;
1072
David Chinner2a82b8b2007-07-11 11:09:12 +10001073 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001074 int truncated;
1075
David Chinner2a82b8b2007-07-11 11:09:12 +10001076 /*
1077 * If we are using filestreams, and we have an unlinked
1078 * file that we are processing the last close on, then nothing
1079 * will be able to reopen and write to this file. Purge this
1080 * inode from the filestreams cache so that it doesn't delay
1081 * teardown of the inode.
1082 */
1083 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1084 xfs_filestream_deassociate(ip);
1085
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001086 /*
1087 * If we previously truncated this file and removed old data
1088 * in the process, we want to initiate "early" writeout on
1089 * the last close. This is an attempt to combat the notorious
1090 * NULL files problem which is particularly noticable from a
1091 * truncate down, buffered (re-)write (delalloc), followed by
1092 * a crash. What we are effectively doing here is
1093 * significantly reducing the time window where we'd otherwise
1094 * be exposed to that problem.
1095 */
Christoph Hellwig09262b432007-08-29 11:44:50 +10001096 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001097 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001098 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001099 }
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (ip->i_d.di_nlink != 0) {
1102 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001103 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001104 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001106 (!(ip->i_d.di_flags &
1107 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
Christoph Hellwigc56c96312009-10-19 04:03:46 +00001108
1109 /*
1110 * If we can't get the iolock just skip truncating
1111 * the blocks past EOF because we could deadlock
1112 * with the mmap_sem otherwise. We'll get another
1113 * chance to drop them once the last reference to
1114 * the inode is dropped, so we'll never leak blocks
1115 * permanently.
1116 */
1117 error = xfs_free_eofblocks(mp, ip,
1118 XFS_FREE_EOF_TRYLOCK);
David Chinner92dfe8d2007-05-24 15:22:19 +10001119 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001120 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122 }
1123
1124 return 0;
1125}
1126
1127/*
1128 * xfs_inactive
1129 *
1130 * This is called when the vnode reference count for the vnode
1131 * goes to zero. If the file has been unlinked, then it must
1132 * now be truncated. Also, we clear all of the read-ahead state
1133 * kept for the inode here since the file is now closed.
1134 */
Christoph Hellwig993386c12007-08-28 16:12:30 +10001135int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136xfs_inactive(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001137 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001139 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 xfs_fsblock_t first_block;
1141 int committed;
1142 xfs_trans_t *tp;
1143 xfs_mount_t *mp;
1144 int error;
1145 int truncate;
1146
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001147 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /*
1150 * If the inode is already free, then there can be nothing
1151 * to clean up here.
1152 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +01001153 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ASSERT(ip->i_df.if_real_bytes == 0);
1155 ASSERT(ip->i_df.if_broot_bytes == 0);
1156 return VN_INACTIVE_CACHE;
1157 }
1158
1159 /*
1160 * Only do a truncate if it's a regular file with
1161 * some actual space in it. It's OK to look at the
1162 * inode's fields without the lock because we're the
1163 * only one with a reference to the inode.
1164 */
1165 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001166 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1167 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1169
1170 mp = ip->i_mount;
1171
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001172 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1173 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 error = 0;
1176
1177 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001178 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto out;
1180
1181 if (ip->i_d.di_nlink != 0) {
1182 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001183 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001184 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001185 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1186 (!(ip->i_d.di_flags &
1187 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1188 (ip->i_delayed_blks != 0)))) {
Christoph Hellwigc56c96312009-10-19 04:03:46 +00001189 error = xfs_free_eofblocks(mp, ip, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001190 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001191 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193 goto out;
1194 }
1195
1196 ASSERT(ip->i_d.di_nlink == 0);
1197
Christoph Hellwig7d095252009-06-08 15:33:32 +02001198 error = xfs_qm_dqattach(ip, 0);
1199 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001200 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1203 if (truncate) {
1204 /*
1205 * Do the xfs_itruncate_start() call before
1206 * reserving any log space because itruncate_start
1207 * will call into the buffer cache and we can't
1208 * do that within a transaction.
1209 */
1210 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1211
Lachlan McIlroyd3cf20942007-05-08 13:49:27 +10001212 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1213 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001214 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf20942007-05-08 13:49:27 +10001215 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1216 return VN_INACTIVE_CACHE;
1217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 error = xfs_trans_reserve(tp, 0,
1220 XFS_ITRUNCATE_LOG_RES(mp),
1221 0, XFS_TRANS_PERM_LOG_RES,
1222 XFS_ITRUNCATE_LOG_COUNT);
1223 if (error) {
1224 /* Don't call itruncate_cleanup */
1225 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1226 xfs_trans_cancel(tp, 0);
1227 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001228 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230
1231 xfs_ilock(ip, XFS_ILOCK_EXCL);
1232 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1233 xfs_trans_ihold(tp, ip);
1234
1235 /*
1236 * normally, we have to run xfs_itruncate_finish sync.
1237 * But if filesystem is wsync and we're in the inactive
1238 * path, then we know that nlink == 0, and that the
1239 * xaction that made nlink == 0 is permanently committed
1240 * since xfs_remove runs as a synchronous transaction.
1241 */
1242 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1243 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1244
1245 if (error) {
1246 xfs_trans_cancel(tp,
1247 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1248 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001249 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1252
1253 /*
1254 * If we get an error while cleaning up a
1255 * symlink we bail out.
1256 */
1257 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1258 xfs_inactive_symlink_rmt(ip, &tp) :
1259 xfs_inactive_symlink_local(ip, &tp);
1260
1261 if (error) {
1262 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001263 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
1266 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1267 xfs_trans_ihold(tp, ip);
1268 } else {
1269 error = xfs_trans_reserve(tp, 0,
1270 XFS_IFREE_LOG_RES(mp),
1271 0, XFS_TRANS_PERM_LOG_RES,
1272 XFS_INACTIVE_LOG_COUNT);
1273 if (error) {
1274 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1275 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001276 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
1279 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1280 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1281 xfs_trans_ihold(tp, ip);
1282 }
1283
1284 /*
1285 * If there are attributes associated with the file
1286 * then blow them away now. The code calls a routine
1287 * that recursively deconstructs the attribute fork.
1288 * We need to just commit the current transaction
1289 * because we can't use it for xfs_attr_inactive().
1290 */
1291 if (ip->i_d.di_anextents > 0) {
1292 error = xfs_inactive_attrs(ip, &tp);
1293 /*
1294 * If we got an error, the transaction is already
1295 * cancelled, and the inode is unlocked. Just get out.
1296 */
1297 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001298 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 } else if (ip->i_afp) {
1300 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1301 }
1302
1303 /*
1304 * Free the inode.
1305 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001306 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 error = xfs_ifree(tp, ip, &free_list);
1308 if (error) {
1309 /*
1310 * If we fail to free the inode, shut down. The cancel
1311 * might do that, we need to make sure. Otherwise the
1312 * inode might be lost for a long time or forever.
1313 */
1314 if (!XFS_FORCED_SHUTDOWN(mp)) {
1315 cmn_err(CE_NOTE,
1316 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1317 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001318 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1321 } else {
1322 /*
1323 * Credit the quota account(s). The inode is gone.
1324 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001325 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /*
David Chinner78e9da72008-04-10 12:24:17 +10001328 * Just ignore errors at this point. There is nothing we can
1329 * do except to try to keep going. Make sure it's not a silent
1330 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
David Chinner78e9da72008-04-10 12:24:17 +10001332 error = xfs_bmap_finish(&tp, &free_list, &committed);
1333 if (error)
1334 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1335 "xfs_bmap_finish() returned error %d", error);
1336 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1337 if (error)
1338 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1339 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Christoph Hellwig7d095252009-06-08 15:33:32 +02001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 /*
1343 * Release the dquots held by inode, if any.
1344 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001345 xfs_qm_dqdetach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1347
1348 out:
1349 return VN_INACTIVE_CACHE;
1350}
1351
Barry Naujok384f3ce2008-05-21 16:58:22 +10001352/*
1353 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1354 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1355 * ci_name->name will point to a the actual name (caller must free) or
1356 * will be set to NULL if an exact match is found.
1357 */
Christoph Hellwig993386c12007-08-28 16:12:30 +10001358int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359xfs_lookup(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001360 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001361 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001362 xfs_inode_t **ipp,
1363 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001365 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int error;
1367 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001369 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1372 return XFS_ERROR(EIO);
1373
1374 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001375 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001377
1378 if (error)
1379 goto out;
1380
1381 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1382 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001383 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001384
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001385 return 0;
1386
Barry Naujok384f3ce2008-05-21 16:58:22 +10001387out_free_name:
1388 if (ci_name)
1389 kmem_free(ci_name->name);
1390out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001391 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return error;
1393}
1394
Christoph Hellwig993386c12007-08-28 16:12:30 +10001395int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396xfs_create(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001397 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001398 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001399 mode_t mode,
1400 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001401 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 cred_t *credp)
1403{
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001404 int is_dir = S_ISDIR(mode);
1405 struct xfs_mount *mp = dp->i_mount;
1406 struct xfs_inode *ip = NULL;
1407 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +10001408 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 xfs_bmap_free_t free_list;
1410 xfs_fsblock_t first_block;
Christoph Hellwig993386c12007-08-28 16:12:30 +10001411 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 uint cancel_flags;
1413 int committed;
1414 xfs_prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001415 struct xfs_dquot *udqp = NULL;
1416 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001418 uint log_res;
1419 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001421 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001423 if (XFS_FORCED_SHUTDOWN(mp))
1424 return XFS_ERROR(EIO);
1425
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001426 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001428 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001429 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001430 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 if (error)
1433 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
Nathan Scott365ca832005-06-21 15:39:12 +10001436 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1437 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 else
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001439 prid = dfltprid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /*
1442 * Make sure that we have allocated dquot(s) on disk.
1443 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001444 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001445 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (error)
1447 goto std_return;
1448
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001449 if (is_dir) {
1450 rdev = 0;
1451 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1452 log_res = XFS_MKDIR_LOG_RES(mp);
1453 log_count = XFS_MKDIR_LOG_COUNT;
1454 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1455 } else {
1456 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1457 log_res = XFS_CREATE_LOG_RES(mp);
1458 log_count = XFS_CREATE_LOG_COUNT;
1459 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /*
1465 * Initially assume that the file does not exist and
1466 * reserve the resources for that case. If that is not
1467 * the case we'll drop the one we have and get a more
1468 * appropriate transaction later.
1469 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001470 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1471 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +02001473 /* flush outstanding delalloc blocks and retry */
1474 xfs_flush_inodes(dp);
Christoph Hellwig4734d402009-09-09 18:19:02 -05001475 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1476 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +02001477 }
1478 if (error == ENOSPC) {
1479 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001481 error = xfs_trans_reserve(tp, 0, log_res, 0,
1482 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 if (error) {
1485 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001486 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001489 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c12007-08-28 16:12:30 +10001490 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001492 /*
1493 * Check for directory link count overflow.
1494 */
1495 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1496 error = XFS_ERROR(EMLINK);
1497 goto out_trans_cancel;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001500 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 /*
1503 * Reserve disk quota and the inode.
1504 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001505 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001507 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Barry Naujok556b8b12008-04-10 12:22:07 +10001509 error = xfs_dir_canenter(tp, dp, name, resblks);
1510 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001511 goto out_trans_cancel;
1512
1513 /*
1514 * A newly created regular or special file just has one directory
1515 * entry pointing to them, but a directory also the "." entry
1516 * pointing to itself.
1517 */
1518 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1519 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (error) {
1521 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001522 goto out_trans_cancel;
1523 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 /*
1527 * At this point, we've gotten a newly allocated inode.
1528 * It is locked (and joined to the transaction).
1529 */
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001530 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 /*
Christoph Hellwig993386c12007-08-28 16:12:30 +10001533 * Now we join the directory inode to the transaction. We do not do it
1534 * earlier because xfs_dir_ialloc might commit the previous transaction
1535 * (and release all the locks). An error from here on will result in
1536 * the transaction cancel unlocking dp so don't do it explicitly in the
1537 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001539 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c12007-08-28 16:12:30 +10001541 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Barry Naujok556b8b12008-04-10 12:22:07 +10001543 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001544 &first_block, &free_list, resblks ?
1545 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (error) {
1547 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001548 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1551 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1552
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001553 if (is_dir) {
1554 error = xfs_dir_init(tp, ip, dp);
1555 if (error)
1556 goto out_bmap_cancel;
1557
1558 error = xfs_bumplink(tp, dp);
1559 if (error)
1560 goto out_bmap_cancel;
1561 }
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /*
1564 * If this is a synchronous mount, make sure that the
1565 * create transaction goes to disk before returning to
1566 * the user.
1567 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001568 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /*
1572 * Attach the dquot(s) to the inodes and modify them incore.
1573 * These ids of the inode couldn't have changed since the new
1574 * inode has been locked ever since it was created.
1575 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001576 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 /*
1579 * xfs_trans_commit normally decrements the vnode ref count
1580 * when it unlocks the inode. Since we want to return the
1581 * vnode to the caller, we bump the vnode ref count now.
1582 */
1583 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001585 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001586 if (error)
1587 goto out_abort_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001589 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (error) {
1591 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001592 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594
Christoph Hellwig7d095252009-06-08 15:33:32 +02001595 xfs_qm_dqrele(udqp);
1596 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001598 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 /* Fallthrough to std_return with error = 0 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001601 std_return:
1602 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1603 XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
1604 ip, DM_RIGHT_NULL, name->name, NULL, mode,
1605 error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return error;
1609
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001610 out_bmap_cancel:
1611 xfs_bmap_cancel(&free_list);
1612 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001614 out_trans_cancel:
1615 xfs_trans_cancel(tp, cancel_flags);
1616 out_dqrele:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001617 xfs_qm_dqrele(udqp);
1618 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Christoph Hellwig993386c12007-08-28 16:12:30 +10001620 if (unlock_dp_on_error)
1621 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto std_return;
1624
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001625 out_abort_rele:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /*
1627 * Wait until after the current transaction is aborted to
1628 * release the inode. This prevents recursive transactions
1629 * and deadlocks from xfs_inactive.
1630 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001631 xfs_bmap_cancel(&free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 cancel_flags |= XFS_TRANS_ABORT;
1633 xfs_trans_cancel(tp, cancel_flags);
1634 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001635 unlock_dp_on_error = B_FALSE;
1636 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640int xfs_locked_n;
1641int xfs_small_retries;
1642int xfs_middle_retries;
1643int xfs_lots_retries;
1644int xfs_lock_delays;
1645#endif
1646
1647/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001648 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1649 * a different value
1650 */
1651static inline int
1652xfs_lock_inumorder(int lock_mode, int subclass)
1653{
1654 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001655 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001656 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001657 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001658
1659 return lock_mode;
1660}
1661
1662/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 * The following routine will lock n inodes in exclusive mode.
1664 * We assume the caller calls us with the inodes in i_ino order.
1665 *
1666 * We need to detect deadlock where an inode that we lock
1667 * is in the AIL and we start waiting for another inode that is locked
1668 * by a thread in a long running transaction (such as truncate). This can
1669 * result in deadlock since the long running trans might need to wait
1670 * for the inode we just locked in order to push the tail and free space
1671 * in the log.
1672 */
1673void
1674xfs_lock_inodes(
1675 xfs_inode_t **ips,
1676 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 uint lock_mode)
1678{
1679 int attempts = 0, i, j, try_lock;
1680 xfs_log_item_t *lp;
1681
1682 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1683
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001684 try_lock = 0;
1685 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687again:
1688 for (; i < inodes; i++) {
1689 ASSERT(ips[i]);
1690
1691 if (i && (ips[i] == ips[i-1])) /* Already locked */
1692 continue;
1693
1694 /*
1695 * If try_lock is not set yet, make sure all locked inodes
1696 * are not in the AIL.
1697 * If any are, set try_lock to be used later.
1698 */
1699
1700 if (!try_lock) {
1701 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1702 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1703 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1704 try_lock++;
1705 }
1706 }
1707 }
1708
1709 /*
1710 * If any of the previous locks we have locked is in the AIL,
1711 * we must TRY to get the second and subsequent locks. If
1712 * we can't get any, we must release all we have
1713 * and try again.
1714 */
1715
1716 if (try_lock) {
1717 /* try_lock must be 0 if i is 0. */
1718 /*
1719 * try_lock means we have an inode locked
1720 * that is in the AIL.
1721 */
1722 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001723 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 attempts++;
1725
1726 /*
1727 * Unlock all previous guys and try again.
1728 * xfs_iunlock will try to push the tail
1729 * if the inode is in the AIL.
1730 */
1731
1732 for(j = i - 1; j >= 0; j--) {
1733
1734 /*
1735 * Check to see if we've already
1736 * unlocked this one.
1737 * Not the first one going back,
1738 * and the inode ptr is the same.
1739 */
1740 if ((j != (i - 1)) && ips[j] ==
1741 ips[j+1])
1742 continue;
1743
1744 xfs_iunlock(ips[j], lock_mode);
1745 }
1746
1747 if ((attempts % 5) == 0) {
1748 delay(1); /* Don't just spin the CPU */
1749#ifdef DEBUG
1750 xfs_lock_delays++;
1751#endif
1752 }
1753 i = 0;
1754 try_lock = 0;
1755 goto again;
1756 }
1757 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001758 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760 }
1761
1762#ifdef DEBUG
1763 if (attempts) {
1764 if (attempts < 5) xfs_small_retries++;
1765 else if (attempts < 100) xfs_middle_retries++;
1766 else xfs_lots_retries++;
1767 } else {
1768 xfs_locked_n++;
1769 }
1770#endif
1771}
1772
David Chinnerf9114eb2008-09-17 16:51:21 +10001773/*
1774 * xfs_lock_two_inodes() can only be used to lock one type of lock
1775 * at a time - the iolock or the ilock, but not both at once. If
1776 * we lock both at once, lockdep will report false positives saying
1777 * we have violated locking orders.
1778 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001779void
1780xfs_lock_two_inodes(
1781 xfs_inode_t *ip0,
1782 xfs_inode_t *ip1,
1783 uint lock_mode)
1784{
1785 xfs_inode_t *temp;
1786 int attempts = 0;
1787 xfs_log_item_t *lp;
1788
David Chinnerf9114eb2008-09-17 16:51:21 +10001789 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1790 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001791 ASSERT(ip0->i_ino != ip1->i_ino);
1792
1793 if (ip0->i_ino > ip1->i_ino) {
1794 temp = ip0;
1795 ip0 = ip1;
1796 ip1 = temp;
1797 }
1798
1799 again:
1800 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1801
1802 /*
1803 * If the first lock we have locked is in the AIL, we must TRY to get
1804 * the second lock. If we can't get it, we must release the first one
1805 * and try again.
1806 */
1807 lp = (xfs_log_item_t *)ip0->i_itemp;
1808 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1809 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1810 xfs_iunlock(ip0, lock_mode);
1811 if ((++attempts % 5) == 0)
1812 delay(1); /* Don't just spin the CPU */
1813 goto again;
1814 }
1815 } else {
1816 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1817 }
1818}
1819
Christoph Hellwig993386c12007-08-28 16:12:30 +10001820int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821xfs_remove(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001822 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001823 struct xfs_name *name,
1824 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001826 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001828 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 int error = 0;
1830 xfs_bmap_free_t free_list;
1831 xfs_fsblock_t first_block;
1832 int cancel_flags;
1833 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 int link_zero;
1835 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001836 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001838 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001839 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (XFS_FORCED_SHUTDOWN(mp))
1842 return XFS_ERROR(EIO);
1843
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001844 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001845 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1846 NULL, DM_RIGHT_NULL, name->name, NULL,
1847 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (error)
1849 return error;
1850 }
1851
Christoph Hellwig7d095252009-06-08 15:33:32 +02001852 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001853 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Christoph Hellwig7d095252009-06-08 15:33:32 +02001856 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001857 if (error)
1858 goto std_return;
1859
1860 if (is_dir) {
1861 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1862 log_count = XFS_DEFAULT_LOG_COUNT;
1863 } else {
1864 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1865 log_count = XFS_REMOVE_LOG_COUNT;
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /*
1870 * We try to get the real space reservation first,
1871 * allowing for directory btree deletion(s) implying
1872 * possible bmap insert(s). If we can't get the space
1873 * reservation then we use 0 instead, and avoid the bmap
1874 * btree insert(s) in the directory code by, if the bmap
1875 * insert tries to happen, instead trimming the LAST
1876 * block from the directory.
1877 */
1878 resblks = XFS_REMOVE_SPACE_RES(mp);
1879 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001880 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (error == ENOSPC) {
1882 resblks = 0;
1883 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001884 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886 if (error) {
1887 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001888 cancel_flags = 0;
1889 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001892 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 /*
1895 * At this point, we've gotten both the directory and the entry
1896 * inodes locked.
1897 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001898 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10001900
1901 IHOLD(dp);
1902 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001905 * If we're removing a directory perform some additional validation.
1906 */
1907 if (is_dir) {
1908 ASSERT(ip->i_d.di_nlink >= 2);
1909 if (ip->i_d.di_nlink != 2) {
1910 error = XFS_ERROR(ENOTEMPTY);
1911 goto out_trans_cancel;
1912 }
1913 if (!xfs_dir_isempty(ip)) {
1914 error = XFS_ERROR(ENOTEMPTY);
1915 goto out_trans_cancel;
1916 }
1917 }
1918
Eric Sandeen9d87c312009-01-14 23:22:07 -06001919 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001920 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001921 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (error) {
1923 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001924 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1927
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001928 if (is_dir) {
1929 /*
1930 * Drop the link from ip's "..".
1931 */
1932 error = xfs_droplink(tp, dp);
1933 if (error)
1934 goto out_bmap_cancel;
1935
1936 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001937 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001938 */
1939 error = xfs_droplink(tp, ip);
1940 if (error)
1941 goto out_bmap_cancel;
1942 } else {
1943 /*
1944 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +11001945 * inode here. For a directory this is done implicitly
1946 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001947 */
1948 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001951 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001952 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001953 */
1954 error = xfs_droplink(tp, ip);
1955 if (error)
1956 goto out_bmap_cancel;
1957
1958 /*
1959 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 * we are in the transaction.
1961 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001962 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 * If this is a synchronous mount, make sure that the
1966 * remove transaction goes to disk before returning to
1967 * the user.
1968 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001969 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001972 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001973 if (error)
1974 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001976 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001977 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001981 * If we are using filestreams, kill the stream association.
1982 * If the file is still open it may get a new one but that
1983 * will get killed on last close in xfs_close() so we don't
1984 * have to worry about that.
1985 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001986 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10001987 xfs_filestream_deassociate(ip);
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001990 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001991 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
1992 NULL, DM_RIGHT_NULL, name->name, NULL,
1993 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return error;
1997
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001998 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 xfs_bmap_cancel(&free_list);
2000 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002001 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 xfs_trans_cancel(tp, cancel_flags);
2003 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Christoph Hellwig993386c12007-08-28 16:12:30 +10002006int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007xfs_link(
Christoph Hellwig993386c12007-08-28 16:12:30 +10002008 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002009 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002010 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Christoph Hellwig993386c12007-08-28 16:12:30 +10002012 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 int error;
2015 xfs_bmap_free_t free_list;
2016 xfs_fsblock_t first_block;
2017 int cancel_flags;
2018 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002021 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002022 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002024 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (XFS_FORCED_SHUTDOWN(mp))
2027 return XFS_ERROR(EIO);
2028
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002029 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002031 tdp, DM_RIGHT_NULL,
2032 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002033 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (error)
2035 return error;
2036 }
2037
2038 /* Return through std_return after this point. */
2039
Christoph Hellwig7d095252009-06-08 15:33:32 +02002040 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01002041 if (error)
2042 goto std_return;
2043
Christoph Hellwig7d095252009-06-08 15:33:32 +02002044 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (error)
2046 goto std_return;
2047
2048 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2049 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002050 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2052 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2053 if (error == ENOSPC) {
2054 resblks = 0;
2055 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2056 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2057 }
2058 if (error) {
2059 cancel_flags = 0;
2060 goto error_return;
2061 }
2062
Christoph Hellwige1cccd92008-08-13 16:18:07 +10002063 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 /*
2066 * Increment vnode ref counts since xfs_trans_commit &
2067 * xfs_trans_cancel will both unlock the inodes and
2068 * decrement the associated ref counts.
2069 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002070 IHOLD(sip);
2071 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2073 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2074
2075 /*
2076 * If the source has too many links, we can't make any more to it.
2077 */
2078 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2079 error = XFS_ERROR(EMLINK);
2080 goto error_return;
2081 }
2082
Nathan Scott365ca832005-06-21 15:39:12 +10002083 /*
2084 * If we are using project inheritance, we only allow hard link
2085 * creation in our tree when the project IDs are the same; else
2086 * the tree quota mechanism could be circumvented.
2087 */
2088 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2089 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002090 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002091 goto error_return;
2092 }
2093
Barry Naujok556b8b12008-04-10 12:22:07 +10002094 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2095 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 goto error_return;
2097
Eric Sandeen9d87c312009-01-14 23:22:07 -06002098 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Barry Naujok556b8b12008-04-10 12:22:07 +10002100 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2101 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (error)
2103 goto abort_return;
2104 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2106
2107 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002108 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 /*
2112 * If this is a synchronous mount, make sure that the
2113 * link transaction goes to disk before returning to
2114 * the user.
2115 */
2116 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2117 xfs_trans_set_sync(tp);
2118 }
2119
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002120 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (error) {
2122 xfs_bmap_cancel(&free_list);
2123 goto abort_return;
2124 }
2125
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002126 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002127 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /* Fall through to std_return with error = 0. */
2131std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002132 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002134 tdp, DM_RIGHT_NULL,
2135 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002136 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 return error;
2139
2140 abort_return:
2141 cancel_flags |= XFS_TRANS_ABORT;
2142 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 error_return:
2145 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 goto std_return;
2147}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002148
Christoph Hellwig993386c12007-08-28 16:12:30 +10002149int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150xfs_symlink(
Christoph Hellwig993386c12007-08-28 16:12:30 +10002151 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002152 struct xfs_name *link_name,
2153 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002154 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002155 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 cred_t *credp)
2157{
Christoph Hellwig993386c12007-08-28 16:12:30 +10002158 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 xfs_inode_t *ip;
2161 int error;
2162 int pathlen;
2163 xfs_bmap_free_t free_list;
2164 xfs_fsblock_t first_block;
Christoph Hellwig993386c12007-08-28 16:12:30 +10002165 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 uint cancel_flags;
2167 int committed;
2168 xfs_fileoff_t first_fsb;
2169 xfs_filblks_t fs_blocks;
2170 int nmaps;
2171 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2172 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002173 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 int byte_cnt;
2175 int n;
2176 xfs_buf_t *bp;
2177 xfs_prid_t prid;
2178 struct xfs_dquot *udqp, *gdqp;
2179 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Christoph Hellwig3937be52008-03-06 13:46:19 +11002181 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 error = 0;
2183 ip = NULL;
2184 tp = NULL;
2185
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002186 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 if (XFS_FORCED_SHUTDOWN(mp))
2189 return XFS_ERROR(EIO);
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /*
2192 * Check component lengths of the target path name.
2193 */
2194 pathlen = strlen(target_path);
2195 if (pathlen >= MAXPATHLEN) /* total string too long */
2196 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002198 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002199 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Dave Chinner046ea752010-01-20 10:47:08 +11002201 link_name->name,
2202 (unsigned char *)target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (error)
2204 return error;
2205 }
2206
2207 /* Return through std_return after this point. */
2208
2209 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002210 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2211 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 else
2213 prid = (xfs_prid_t)dfltprid;
2214
2215 /*
2216 * Make sure that we have allocated dquot(s) on disk.
2217 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002218 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2220 if (error)
2221 goto std_return;
2222
2223 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2224 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2225 /*
2226 * The symlink will fit into the inode data fork?
2227 * There can't be any attributes so we get the whole variable part.
2228 */
2229 if (pathlen <= XFS_LITINO(mp))
2230 fs_blocks = 0;
2231 else
2232 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002233 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2235 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2236 if (error == ENOSPC && fs_blocks == 0) {
2237 resblks = 0;
2238 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2239 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2240 }
2241 if (error) {
2242 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 goto error_return;
2244 }
2245
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002246 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c12007-08-28 16:12:30 +10002247 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 /*
2250 * Check whether the directory allows new symlinks or not.
2251 */
2252 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2253 error = XFS_ERROR(EPERM);
2254 goto error_return;
2255 }
2256
2257 /*
2258 * Reserve disk quota : blocks and inode.
2259 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002260 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (error)
2262 goto error_return;
2263
2264 /*
2265 * Check for ability to enter directory entry, if no space reserved.
2266 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002267 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2268 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto error_return;
2270 /*
2271 * Initialize the bmap freelist prior to calling either
2272 * bmapi or the directory create code.
2273 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002274 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 /*
2277 * Allocate an inode for the symlink.
2278 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002279 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 1, 0, credp, prid, resblks > 0, &ip, NULL);
2281 if (error) {
2282 if (error == ENOSPC)
2283 goto error_return;
2284 goto error1;
2285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Christoph Hellwig993386c12007-08-28 16:12:30 +10002287 /*
2288 * An error after we've joined dp to the transaction will result in the
2289 * transaction cancel unlocking dp so don't do it explicitly in the
2290 * error path.
2291 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002292 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c12007-08-28 16:12:30 +10002294 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 /*
2297 * Also attach the dquot(s) to it, if applicable.
2298 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002299 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 if (resblks)
2302 resblks -= XFS_IALLOC_SPACE_RES(mp);
2303 /*
2304 * If the symlink will fit into the inode, write it inline.
2305 */
2306 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2307 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2308 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2309 ip->i_d.di_size = pathlen;
2310
2311 /*
2312 * The inode was initially created in extent format.
2313 */
2314 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2315 ip->i_df.if_flags |= XFS_IFINLINE;
2316
2317 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2318 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2319
2320 } else {
2321 first_fsb = 0;
2322 nmaps = SYMLINK_MAPS;
2323
2324 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2325 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2326 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002327 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (error) {
2329 goto error1;
2330 }
2331
2332 if (resblks)
2333 resblks -= fs_blocks;
2334 ip->i_d.di_size = pathlen;
2335 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2336
2337 cur_chunk = target_path;
2338 for (n = 0; n < nmaps; n++) {
2339 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2340 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2341 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2342 BTOBB(byte_cnt), 0);
2343 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2344 if (pathlen < byte_cnt) {
2345 byte_cnt = pathlen;
2346 }
2347 pathlen -= byte_cnt;
2348
2349 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2350 cur_chunk += byte_cnt;
2351
2352 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2353 }
2354 }
2355
2356 /*
2357 * Create the directory entry for the symlink.
2358 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002359 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2360 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002361 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2364 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2365
2366 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 * If this is a synchronous mount, make sure that the
2368 * symlink transaction goes to disk before returning to
2369 * the user.
2370 */
2371 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2372 xfs_trans_set_sync(tp);
2373 }
2374
2375 /*
2376 * xfs_trans_commit normally decrements the vnode ref count
2377 * when it unlocks the inode. Since we want to return the
2378 * vnode to the caller, we bump the vnode ref count now.
2379 */
2380 IHOLD(ip);
2381
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002382 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (error) {
2384 goto error2;
2385 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002386 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002387 xfs_qm_dqrele(udqp);
2388 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 /* Fall through to std_return with error = 0 or errno from
2391 * xfs_trans_commit */
2392std_return:
Christoph Hellwig993386c12007-08-28 16:12:30 +10002393 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002395 dp, DM_RIGHT_NULL,
2396 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002397 DM_RIGHT_NULL, link_name->name,
Dave Chinner046ea752010-01-20 10:47:08 +11002398 (unsigned char *)target_path,
2399 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401
Christoph Hellwig3937be52008-03-06 13:46:19 +11002402 if (!error)
2403 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return error;
2405
2406 error2:
2407 IRELE(ip);
2408 error1:
2409 xfs_bmap_cancel(&free_list);
2410 cancel_flags |= XFS_TRANS_ABORT;
2411 error_return:
2412 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002413 xfs_qm_dqrele(udqp);
2414 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Christoph Hellwig993386c12007-08-28 16:12:30 +10002416 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 goto std_return;
2420}
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422int
Christoph Hellwig993386c12007-08-28 16:12:30 +10002423xfs_set_dmattrs(
2424 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 u_int evmask,
Christoph Hellwig993386c12007-08-28 16:12:30 +10002426 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Christoph Hellwig993386c12007-08-28 16:12:30 +10002428 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 int error;
2431
2432 if (!capable(CAP_SYS_ADMIN))
2433 return XFS_ERROR(EPERM);
2434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if (XFS_FORCED_SHUTDOWN(mp))
2436 return XFS_ERROR(EIO);
2437
2438 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2439 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2440 if (error) {
2441 xfs_trans_cancel(tp, 0);
2442 return error;
2443 }
2444 xfs_ilock(ip, XFS_ILOCK_EXCL);
2445 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2446
Christoph Hellwig613d7042007-10-11 17:44:08 +10002447 ip->i_d.di_dmevmask = evmask;
2448 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2451 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002452 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 return error;
2455}
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457/*
2458 * xfs_alloc_file_space()
2459 * This routine allocates disk space for the given file.
2460 *
2461 * If alloc_type == 0, this request is for an ALLOCSP type
2462 * request which will change the file size. In this case, no
2463 * DMAPI event will be generated by the call. A TRUNCATE event
2464 * will be generated later by xfs_setattr.
2465 *
2466 * If alloc_type != 0, this request is for a RESVSP type
2467 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2468 * lower block boundary byte address is less than the file's
2469 * length.
2470 *
2471 * RETURNS:
2472 * 0 on success
2473 * errno on error
2474 *
2475 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002476STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477xfs_alloc_file_space(
2478 xfs_inode_t *ip,
2479 xfs_off_t offset,
2480 xfs_off_t len,
2481 int alloc_type,
2482 int attr_flags)
2483{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002484 xfs_mount_t *mp = ip->i_mount;
2485 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 xfs_filblks_t allocated_fsb;
2487 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002488 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002490 xfs_fsblock_t firstfsb;
2491 int nimaps;
2492 int bmapi_flag;
2493 int quota_flag;
2494 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002496 xfs_bmbt_irec_t imaps[1], *imapp;
2497 xfs_bmap_free_t free_list;
2498 uint qblocks, resblks, resrtextents;
2499 int committed;
2500 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002502 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 if (XFS_FORCED_SHUTDOWN(mp))
2505 return XFS_ERROR(EIO);
2506
Christoph Hellwig7d095252009-06-08 15:33:32 +02002507 error = xfs_qm_dqattach(ip, 0);
2508 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return error;
2510
2511 if (len <= 0)
2512 return XFS_ERROR(EINVAL);
2513
David Chinner957d0eb2007-06-18 16:50:37 +10002514 rt = XFS_IS_REALTIME_INODE(ip);
2515 extsz = xfs_get_extsz_hint(ip);
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11002519 nimaps = 1;
2520 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2522 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2523
2524 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002525 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002526 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002527 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 xfs_off_t end_dmi_offset;
2529
2530 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002531 if (end_dmi_offset > ip->i_size)
2532 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002533 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2534 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002536 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
2538
2539 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002540 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 */
2542retry:
2543 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002544 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Nathan Scottdd9f4382006-01-11 15:28:28 +11002546 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11002547 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002548 */
2549 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002551 do_div(s, extsz);
2552 s *= extsz;
2553 e = startoffset_fsb + allocatesize_fsb;
2554 if ((temp = do_mod(startoffset_fsb, extsz)))
2555 e += temp;
2556 if ((temp = do_mod(e, extsz)))
2557 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002559 s = 0;
2560 e = allocatesize_fsb;
2561 }
2562
2563 if (unlikely(rt)) {
2564 resrtextents = qblocks = (uint)(e - s);
2565 resrtextents /= mp->m_sb.sb_rextsize;
2566 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2567 quota_flag = XFS_QMOPT_RES_RTBLKS;
2568 } else {
2569 resrtextents = 0;
2570 resblks = qblocks = \
2571 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2572 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
2574
2575 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002576 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 */
2578 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002579 error = xfs_trans_reserve(tp, resblks,
2580 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 XFS_TRANS_PERM_LOG_RES,
2582 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002584 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 */
2586 if (error) {
2587 /*
2588 * Free the transaction structure.
2589 */
2590 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2591 xfs_trans_cancel(tp, 0);
2592 break;
2593 }
2594 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002595 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2596 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if (error)
2598 goto error1;
2599
2600 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2601 xfs_trans_ihold(tp, ip);
2602
2603 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002604 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002606 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002607 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002608 allocatesize_fsb, bmapi_flag,
2609 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002610 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (error) {
2612 goto error0;
2613 }
2614
2615 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002616 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002618 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 if (error) {
2620 goto error0;
2621 }
2622
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002623 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2625 if (error) {
2626 break;
2627 }
2628
2629 allocated_fsb = imapp->br_blockcount;
2630
Nathan Scottdd9f4382006-01-11 15:28:28 +11002631 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 error = XFS_ERROR(ENOSPC);
2633 break;
2634 }
2635
2636 startoffset_fsb += allocated_fsb;
2637 allocatesize_fsb -= allocated_fsb;
2638 }
2639dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002640 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002641 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002643 ip, DM_RIGHT_NULL,
2644 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
2646 if (error == 0)
2647 goto retry; /* Maybe DMAPI app. has made space */
2648 /* else fall through with error from XFS_SEND_DATA */
2649 }
2650
2651 return error;
2652
Nathan Scottdd9f4382006-01-11 15:28:28 +11002653error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002655 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002656
2657error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2659 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2660 goto dmapi_enospc_check;
2661}
2662
2663/*
2664 * Zero file bytes between startoff and endoff inclusive.
2665 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002666 *
2667 * This function is used by xfs_free_file_space() to zero
2668 * partial blocks when the range to free is not block aligned.
2669 * When unreserving space with boundaries that are not block
2670 * aligned we round up the start and round down the end
2671 * boundaries and then use this function to zero the parts of
2672 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 */
2674STATIC int
2675xfs_zero_remaining_bytes(
2676 xfs_inode_t *ip,
2677 xfs_off_t startoff,
2678 xfs_off_t endoff)
2679{
2680 xfs_bmbt_irec_t imap;
2681 xfs_fileoff_t offset_fsb;
2682 xfs_off_t lastoffset;
2683 xfs_off_t offset;
2684 xfs_buf_t *bp;
2685 xfs_mount_t *mp = ip->i_mount;
2686 int nimap;
2687 int error = 0;
2688
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002689 /*
2690 * Avoid doing I/O beyond eof - it's not necessary
2691 * since nothing can read beyond eof. The space will
2692 * be zeroed when the file is extended anyway.
2693 */
2694 if (startoff >= ip->i_size)
2695 return 0;
2696
2697 if (endoff > ip->i_size)
2698 endoff = ip->i_size;
2699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002701 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 mp->m_rtdev_targp : mp->m_ddev_targp);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11002703 if (!bp)
2704 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2707 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2708 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002709 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002710 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (error || nimap < 1)
2712 break;
2713 ASSERT(imap.br_blockcount >= 1);
2714 ASSERT(imap.br_startoff == offset_fsb);
2715 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2716 if (lastoffset > endoff)
2717 lastoffset = endoff;
2718 if (imap.br_startblock == HOLESTARTBLOCK)
2719 continue;
2720 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2721 if (imap.br_state == XFS_EXT_UNWRITTEN)
2722 continue;
2723 XFS_BUF_UNDONE(bp);
2724 XFS_BUF_UNWRITE(bp);
2725 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002726 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002728 error = xfs_iowait(bp);
2729 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2731 mp, bp, XFS_BUF_ADDR(bp));
2732 break;
2733 }
2734 memset(XFS_BUF_PTR(bp) +
2735 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2736 0, lastoffset - offset + 1);
2737 XFS_BUF_UNDONE(bp);
2738 XFS_BUF_UNREAD(bp);
2739 XFS_BUF_WRITE(bp);
2740 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002741 error = xfs_iowait(bp);
2742 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2744 mp, bp, XFS_BUF_ADDR(bp));
2745 break;
2746 }
2747 }
2748 xfs_buf_free(bp);
2749 return error;
2750}
2751
2752/*
2753 * xfs_free_file_space()
2754 * This routine frees disk space for the given file.
2755 *
2756 * This routine is only called by xfs_change_file_space
2757 * for an UNRESVSP type call.
2758 *
2759 * RETURNS:
2760 * 0 on success
2761 * errno on error
2762 *
2763 */
2764STATIC int
2765xfs_free_file_space(
2766 xfs_inode_t *ip,
2767 xfs_off_t offset,
2768 xfs_off_t len,
2769 int attr_flags)
2770{
2771 int committed;
2772 int done;
2773 xfs_off_t end_dmi_offset;
2774 xfs_fileoff_t endoffset_fsb;
2775 int error;
2776 xfs_fsblock_t firstfsb;
2777 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 xfs_bmbt_irec_t imap;
2779 xfs_off_t ioffset;
2780 xfs_extlen_t mod=0;
2781 xfs_mount_t *mp;
2782 int nimap;
2783 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002784 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 int rt;
2786 xfs_fileoff_t startoffset_fsb;
2787 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002788 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 mp = ip->i_mount;
2791
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002792 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002793
Christoph Hellwig7d095252009-06-08 15:33:32 +02002794 error = xfs_qm_dqattach(ip, 0);
2795 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return error;
2797
2798 error = 0;
2799 if (len <= 0) /* if nothing being freed */
2800 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002801 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2803 end_dmi_offset = offset + len;
2804 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
2805
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002806 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002807 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002808 if (end_dmi_offset > ip->i_size)
2809 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002810 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 offset, end_dmi_offset - offset,
2812 AT_DELAY_FLAG(attr_flags), NULL);
2813 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002814 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
2816
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002817 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002818 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002819 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002821 /* wait for the completion of any pending DIOs */
2822 xfs_ioend_wait(ip);
Yingping Lu9fa80462006-03-22 12:44:35 +11002823 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002824
Tim Shimmine6a4b372007-11-23 16:30:42 +11002825 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002827
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002828 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002829 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf20942007-05-08 13:49:27 +10002830 if (error)
2831 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002832 }
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 /*
2835 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002836 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2838 * will take care of it for us.
2839 */
Eric Sandeen62118702008-03-06 13:44:28 +11002840 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002842 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002843 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (error)
2845 goto out_unlock_iolock;
2846 ASSERT(nimap == 0 || nimap == 1);
2847 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2848 xfs_daddr_t block;
2849
2850 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2851 block = imap.br_startblock;
2852 mod = do_div(block, mp->m_sb.sb_rextsize);
2853 if (mod)
2854 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2855 }
2856 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002857 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002858 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (error)
2860 goto out_unlock_iolock;
2861 ASSERT(nimap == 0 || nimap == 1);
2862 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2863 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2864 mod++;
2865 if (mod && (mod != mp->m_sb.sb_rextsize))
2866 endoffset_fsb -= mod;
2867 }
2868 }
2869 if ((done = (endoffset_fsb <= startoffset_fsb)))
2870 /*
2871 * One contiguous piece to clear
2872 */
2873 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2874 else {
2875 /*
2876 * Some full blocks, possibly two pieces to clear
2877 */
2878 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2879 error = xfs_zero_remaining_bytes(ip, offset,
2880 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2881 if (!error &&
2882 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2883 error = xfs_zero_remaining_bytes(ip,
2884 XFS_FSB_TO_B(mp, endoffset_fsb),
2885 offset + len - 1);
2886 }
2887
2888 /*
2889 * free file space until done or until there is an error
2890 */
2891 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2892 while (!error && !done) {
2893
2894 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002895 * allocate and setup the transaction. Allow this
2896 * transaction to dip into the reserve blocks to ensure
2897 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 */
2899 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002900 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 error = xfs_trans_reserve(tp,
2902 resblks,
2903 XFS_WRITE_LOG_RES(mp),
2904 0,
2905 XFS_TRANS_PERM_LOG_RES,
2906 XFS_WRITE_LOG_COUNT);
2907
2908 /*
2909 * check for running out of space
2910 */
2911 if (error) {
2912 /*
2913 * Free the transaction structure.
2914 */
2915 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2916 xfs_trans_cancel(tp, 0);
2917 break;
2918 }
2919 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002920 error = xfs_trans_reserve_quota(tp, mp,
2921 ip->i_udquot, ip->i_gdquot,
2922 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (error)
2924 goto error1;
2925
2926 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2927 xfs_trans_ihold(tp, ip);
2928
2929 /*
2930 * issue the bunmapi() call to free the blocks
2931 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002932 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002933 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002935 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (error) {
2937 goto error0;
2938 }
2939
2940 /*
2941 * complete the transaction
2942 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002943 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 if (error) {
2945 goto error0;
2946 }
2947
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002948 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2950 }
2951
2952 out_unlock_iolock:
2953 if (need_iolock)
2954 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2955 return error;
2956
2957 error0:
2958 xfs_bmap_cancel(&free_list);
2959 error1:
2960 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2961 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2962 XFS_ILOCK_EXCL);
2963 return error;
2964}
2965
2966/*
2967 * xfs_change_file_space()
2968 * This routine allocates or frees disk space for the given file.
2969 * The user specified parameters are checked for alignment and size
2970 * limitations.
2971 *
2972 * RETURNS:
2973 * 0 on success
2974 * errno on error
2975 *
2976 */
2977int
2978xfs_change_file_space(
Christoph Hellwig993386c12007-08-28 16:12:30 +10002979 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 int cmd,
2981 xfs_flock64_t *bf,
2982 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 int attr_flags)
2984{
Christoph Hellwig993386c12007-08-28 16:12:30 +10002985 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 int clrprealloc;
2987 int error;
2988 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 int setprealloc;
2990 xfs_off_t startoffset;
2991 xfs_off_t llen;
2992 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002993 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002995 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Christoph Hellwig993386c12007-08-28 16:12:30 +10002997 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return XFS_ERROR(EINVAL);
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 switch (bf->l_whence) {
3001 case 0: /*SEEK_SET*/
3002 break;
3003 case 1: /*SEEK_CUR*/
3004 bf->l_start += offset;
3005 break;
3006 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003007 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 break;
3009 default:
3010 return XFS_ERROR(EINVAL);
3011 }
3012
3013 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3014
3015 if ( (bf->l_start < 0)
3016 || (bf->l_start > XFS_MAXIOFFSET(mp))
3017 || (bf->l_start + llen < 0)
3018 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3019 return XFS_ERROR(EINVAL);
3020
3021 bf->l_whence = 0;
3022
3023 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003024 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
3026 /*
3027 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3028 * file space.
3029 * These calls do NOT zero the data space allocated to the file,
3030 * nor do they change the file size.
3031 *
3032 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3033 * space.
3034 * These calls cause the new file data to be zeroed and the file
3035 * size to be changed.
3036 */
3037 setprealloc = clrprealloc = 0;
3038
3039 switch (cmd) {
3040 case XFS_IOC_RESVSP:
3041 case XFS_IOC_RESVSP64:
3042 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3043 1, attr_flags);
3044 if (error)
3045 return error;
3046 setprealloc = 1;
3047 break;
3048
3049 case XFS_IOC_UNRESVSP:
3050 case XFS_IOC_UNRESVSP64:
3051 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3052 attr_flags)))
3053 return error;
3054 break;
3055
3056 case XFS_IOC_ALLOCSP:
3057 case XFS_IOC_ALLOCSP64:
3058 case XFS_IOC_FREESP:
3059 case XFS_IOC_FREESP64:
3060 if (startoffset > fsize) {
3061 error = xfs_alloc_file_space(ip, fsize,
3062 startoffset - fsize, 0, attr_flags);
3063 if (error)
3064 break;
3065 }
3066
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003067 iattr.ia_valid = ATTR_SIZE;
3068 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +11003070 error = xfs_setattr(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 if (error)
3073 return error;
3074
3075 clrprealloc = 1;
3076 break;
3077
3078 default:
3079 ASSERT(0);
3080 return XFS_ERROR(EINVAL);
3081 }
3082
3083 /*
3084 * update the inode timestamp, mode, and prealloc flag bits
3085 */
3086 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3087
3088 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3089 0, 0, 0))) {
3090 /* ASSERT(0); */
3091 xfs_trans_cancel(tp, 0);
3092 return error;
3093 }
3094
3095 xfs_ilock(ip, XFS_ILOCK_EXCL);
3096
3097 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3098 xfs_trans_ihold(tp, ip);
3099
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003100 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 ip->i_d.di_mode &= ~S_ISUID;
3102
3103 /*
3104 * Note that we don't have to worry about mandatory
3105 * file locking being disabled here because we only
3106 * clear the S_ISGID bit if the Group execute bit is
3107 * on, but if it was on then mandatory locking wouldn't
3108 * have been enabled.
3109 */
3110 if (ip->i_d.di_mode & S_IXGRP)
3111 ip->i_d.di_mode &= ~S_ISGID;
3112
3113 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3114 }
3115 if (setprealloc)
3116 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3117 else if (clrprealloc)
3118 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3119
3120 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3121 xfs_trans_set_sync(tp);
3122
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003123 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3126
3127 return error;
3128}