blob: 43241e2898006ce5ee3ab2a83e2da9f7f4385305 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_dir2_sf.h"
36#include "xfs_attr_sf.h"
37#include "xfs_dinode.h"
38#include "xfs_inode.h"
39#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_itable.h"
41#include "xfs_btree.h"
42#include "xfs_ialloc.h"
43#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_bmap.h"
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020045#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_attr.h"
47#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_quota.h"
50#include "xfs_utils.h"
Nathan Scotta844f452005-11-02 14:38:42 +110051#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "xfs_log_priv.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100054#include "xfs_filestream.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100055#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000056#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Christoph Hellwig993386c2007-08-28 16:12:30 +100058int
Linus Torvalds1da177e2005-04-16 15:20:36 -070059xfs_setattr(
Christoph Hellwig0f285c82008-07-18 17:13:28 +100060 struct xfs_inode *ip,
61 struct iattr *iattr,
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +110062 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Christoph Hellwig993386c2007-08-28 16:12:30 +100064 xfs_mount_t *mp = ip->i_mount;
David Chinnere4f75292008-08-13 16:00:45 +100065 struct inode *inode = VFS_I(ip);
Christoph Hellwig0f285c82008-07-18 17:13:28 +100066 int mask = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 int code;
69 uint lock_flags;
70 uint commit_flags=0;
71 uid_t uid=0, iuid=0;
72 gid_t gid=0, igid=0;
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 Hellwig993386c2007-08-28 16:12:30 +1000551int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552xfs_readlink(
Christoph Hellwig993386c2007-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 Hellwig993386c2007-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 Hellwige8b217e2010-02-02 10:16:26 +1100600 int log_flushed = 0;
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (xfs_ipincount(ip)) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000632 error = _xfs_log_force(ip->i_mount, XFS_LOG_SYNC,
633 &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 } else {
636 /*
David Chinner978b7232008-05-19 16:29:46 +1000637 * Kick off a transaction to log the inode core to get the
638 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 */
640 xfs_iunlock(ip, XFS_ILOCK_SHARED);
641 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000642 error = xfs_trans_reserve(tp, 0,
643 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
644 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 xfs_trans_cancel(tp, 0);
646 return error;
647 }
648 xfs_ilock(ip, XFS_ILOCK_EXCL);
649
650 /*
David Chinner978b7232008-05-19 16:29:46 +1000651 * Note - it's possible that we might have pushed ourselves out
652 * of the way during trans_reserve which would flush the inode.
653 * But there's no guarantee that the inode buffer has actually
654 * gone out yet (it's delwri). Plus the buffer could be pinned
655 * anyway if it's part of an inode in another recent
656 * transaction. So we play it safe and fire off the
657 * transaction anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
659 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
660 xfs_trans_ihold(tp, ip);
661 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
David Chinner978b7232008-05-19 16:29:46 +1000662 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000663 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 xfs_iunlock(ip, XFS_ILOCK_EXCL);
666 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100667
Christoph Hellwige8b217e2010-02-02 10:16:26 +1100668 if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100669 /*
670 * If the log write didn't issue an ordered tag we need
671 * to flush the disk cache for the data device now.
672 */
673 if (!log_flushed)
674 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
675
676 /*
677 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000678 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100679 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100680 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100681 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
682 }
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return error;
685}
686
687/*
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000688 * Flags for xfs_free_eofblocks
689 */
690#define XFS_FREE_EOF_TRYLOCK (1<<0)
691
692/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000693 * This is called by xfs_inactive to free any blocks beyond eof
694 * when the link count isn't zero and by xfs_dm_punch_hole() when
695 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Eric Sandeend96f8f82009-07-02 00:09:33 -0500697STATIC int
David Chinner92dfe8d2007-05-24 15:22:19 +1000698xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000700 xfs_inode_t *ip,
701 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 xfs_trans_t *tp;
704 int error;
705 xfs_fileoff_t end_fsb;
706 xfs_fileoff_t last_fsb;
707 xfs_filblks_t map_len;
708 int nimaps;
709 xfs_bmbt_irec_t imap;
710
711 /*
712 * Figure out if there are any blocks beyond the end
713 * of the file. If not, then there is nothing to do.
714 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000715 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
717 map_len = last_fsb - end_fsb;
718 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 nimaps = 1;
722 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000723 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000724 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 xfs_iunlock(ip, XFS_ILOCK_SHARED);
726
727 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100728 (imap.br_startblock != HOLESTARTBLOCK ||
729 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
731 * Attach the dquots to the inode up front.
732 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200733 error = xfs_qm_dqattach(ip, 0);
734 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100735 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /*
738 * There are blocks after the end of file.
739 * Free them up now by truncating the file to
740 * its current size.
741 */
742 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
743
744 /*
745 * Do the xfs_itruncate_start() call before
746 * reserving any log space because
747 * itruncate_start will call into the buffer
748 * cache and we can't
749 * do that within a transaction.
750 */
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000751 if (flags & XFS_FREE_EOF_TRYLOCK) {
752 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
753 xfs_trans_cancel(tp, 0);
754 return 0;
755 }
756 } else {
David Chinner92dfe8d2007-05-24 15:22:19 +1000757 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000758 }
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000759 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000760 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000761 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000762 xfs_trans_cancel(tp, 0);
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000763 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000764 return error;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 error = xfs_trans_reserve(tp, 0,
768 XFS_ITRUNCATE_LOG_RES(mp),
769 0, XFS_TRANS_PERM_LOG_RES,
770 XFS_ITRUNCATE_LOG_COUNT);
771 if (error) {
772 ASSERT(XFS_FORCED_SHUTDOWN(mp));
773 xfs_trans_cancel(tp, 0);
774 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100775 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
778 xfs_ilock(ip, XFS_ILOCK_EXCL);
779 xfs_trans_ijoin(tp, ip,
780 XFS_IOLOCK_EXCL |
781 XFS_ILOCK_EXCL);
782 xfs_trans_ihold(tp, ip);
783
784 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000785 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 XFS_DATA_FORK,
787 0);
788 /*
789 * If we get an error at this point we
790 * simply don't bother truncating the file.
791 */
792 if (error) {
793 xfs_trans_cancel(tp,
794 (XFS_TRANS_RELEASE_LOG_RES |
795 XFS_TRANS_ABORT));
796 } else {
797 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000798 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Christoph Hellwigc56c9632009-10-19 04:03:46 +0000800 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100802 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805/*
806 * Free a symlink that has blocks associated with it.
807 */
808STATIC int
809xfs_inactive_symlink_rmt(
810 xfs_inode_t *ip,
811 xfs_trans_t **tpp)
812{
813 xfs_buf_t *bp;
814 int committed;
815 int done;
816 int error;
817 xfs_fsblock_t first_block;
818 xfs_bmap_free_t free_list;
819 int i;
820 xfs_mount_t *mp;
821 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
822 int nmaps;
823 xfs_trans_t *ntp;
824 int size;
825 xfs_trans_t *tp;
826
827 tp = *tpp;
828 mp = ip->i_mount;
829 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
830 /*
831 * We're freeing a symlink that has some
832 * blocks allocated to it. Free the
833 * blocks here. We know that we've got
834 * either 1 or 2 extents and that we can
835 * free them all in one bunmapi call.
836 */
837 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
838 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
839 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
840 ASSERT(XFS_FORCED_SHUTDOWN(mp));
841 xfs_trans_cancel(tp, 0);
842 *tpp = NULL;
843 return error;
844 }
845 /*
846 * Lock the inode, fix the size, and join it to the transaction.
847 * Hold it so in the normal path, we still have it locked for
848 * the second transaction. In the error paths we need it
849 * held so the cancel won't rele it, see below.
850 */
851 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
852 size = (int)ip->i_d.di_size;
853 ip->i_d.di_size = 0;
854 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
855 xfs_trans_ihold(tp, ip);
856 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
857 /*
858 * Find the block(s) so we can inval and unmap them.
859 */
860 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600861 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800862 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
864 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000865 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto error0;
867 /*
868 * Invalidate the block(s).
869 */
870 for (i = 0; i < nmaps; i++) {
871 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
872 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
873 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
874 xfs_trans_binval(tp, bp);
875 }
876 /*
877 * Unmap the dead block(s) to the free_list.
878 */
879 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000880 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 goto error1;
882 ASSERT(done);
883 /*
884 * Commit the first transaction. This logs the EFI and the inode.
885 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100886 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto error1;
888 /*
889 * The transaction must have been committed, since there were
890 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
891 * The new tp has the extent freeing and EFDs.
892 */
893 ASSERT(committed);
894 /*
895 * The first xact was committed, so add the inode to the new one.
896 * Mark it dirty so it will be logged and moved forward in the log as
897 * part of every commit.
898 */
899 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
900 xfs_trans_ihold(tp, ip);
901 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
902 /*
903 * Get a new, empty transaction to return to our caller.
904 */
905 ntp = xfs_trans_dup(tp);
906 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000907 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 * If we get an error on the commit here or on the reserve below,
909 * we need to unlock the inode since the new transaction doesn't
910 * have the inode attached.
911 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000912 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 tp = ntp;
914 if (error) {
915 ASSERT(XFS_FORCED_SHUTDOWN(mp));
916 goto error0;
917 }
918 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100919 * transaction commit worked ok so we can drop the extra ticket
920 * reference that we gained in xfs_trans_dup()
921 */
922 xfs_log_ticket_put(tp->t_ticket);
923
924 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 * Remove the memory for extent descriptions (just bookkeeping).
926 */
927 if (ip->i_df.if_bytes)
928 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
929 ASSERT(ip->i_df.if_bytes == 0);
930 /*
931 * Put an itruncate log reservation in the new transaction
932 * for our caller.
933 */
934 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
935 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
936 ASSERT(XFS_FORCED_SHUTDOWN(mp));
937 goto error0;
938 }
939 /*
940 * Return with the inode locked but not joined to the transaction.
941 */
942 *tpp = tp;
943 return 0;
944
945 error1:
946 xfs_bmap_cancel(&free_list);
947 error0:
948 /*
949 * Have to come here with the inode locked and either
950 * (held and in the transaction) or (not in the transaction).
951 * If the inode isn't held then cancel would iput it, but
952 * that's wrong since this is inactive and the vnode ref
953 * count is 0 already.
954 * Cancel won't do anything to the inode if held, but it still
955 * needs to be locked until the cancel is done, if it was
956 * joined to the transaction.
957 */
958 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
959 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
960 *tpp = NULL;
961 return error;
962
963}
964
965STATIC int
966xfs_inactive_symlink_local(
967 xfs_inode_t *ip,
968 xfs_trans_t **tpp)
969{
970 int error;
971
972 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
973 /*
974 * We're freeing a symlink which fit into
975 * the inode. Just free the memory used
976 * to hold the old symlink.
977 */
978 error = xfs_trans_reserve(*tpp, 0,
979 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
980 0, XFS_TRANS_PERM_LOG_RES,
981 XFS_ITRUNCATE_LOG_COUNT);
982
983 if (error) {
984 xfs_trans_cancel(*tpp, 0);
985 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100986 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
989
990 /*
991 * Zero length symlinks _can_ exist.
992 */
993 if (ip->i_df.if_bytes > 0) {
994 xfs_idata_realloc(ip,
995 -(ip->i_df.if_bytes),
996 XFS_DATA_FORK);
997 ASSERT(ip->i_df.if_bytes == 0);
998 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002STATIC int
1003xfs_inactive_attrs(
1004 xfs_inode_t *ip,
1005 xfs_trans_t **tpp)
1006{
1007 xfs_trans_t *tp;
1008 int error;
1009 xfs_mount_t *mp;
1010
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10001011 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 tp = *tpp;
1013 mp = ip->i_mount;
1014 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001015 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001017 if (error)
1018 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001021 if (error)
1022 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1025 error = xfs_trans_reserve(tp, 0,
1026 XFS_IFREE_LOG_RES(mp),
1027 0, XFS_TRANS_PERM_LOG_RES,
1028 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001029 if (error)
1030 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 xfs_ilock(ip, XFS_ILOCK_EXCL);
1033 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1034 xfs_trans_ihold(tp, ip);
1035 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1036
1037 ASSERT(ip->i_d.di_anextents == 0);
1038
1039 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001040 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001041
1042error_cancel:
1043 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1044 xfs_trans_cancel(tp, 0);
1045error_unlock:
1046 *tpp = NULL;
1047 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1048 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Christoph Hellwig993386c2007-08-28 16:12:30 +10001051int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001053 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001055 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 int error;
1057
Christoph Hellwig42173f62008-04-22 17:33:25 +10001058 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001062 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
1064
David Chinner2a82b8b2007-07-11 11:09:12 +10001065 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001066 int truncated;
1067
David Chinner2a82b8b2007-07-11 11:09:12 +10001068 /*
1069 * If we are using filestreams, and we have an unlinked
1070 * file that we are processing the last close on, then nothing
1071 * will be able to reopen and write to this file. Purge this
1072 * inode from the filestreams cache so that it doesn't delay
1073 * teardown of the inode.
1074 */
1075 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1076 xfs_filestream_deassociate(ip);
1077
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001078 /*
1079 * If we previously truncated this file and removed old data
1080 * in the process, we want to initiate "early" writeout on
1081 * the last close. This is an attempt to combat the notorious
1082 * NULL files problem which is particularly noticable from a
1083 * truncate down, buffered (re-)write (delalloc), followed by
1084 * a crash. What we are effectively doing here is
1085 * significantly reducing the time window where we'd otherwise
1086 * be exposed to that problem.
1087 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001088 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001089 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001090 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001091 }
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (ip->i_d.di_nlink != 0) {
1094 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001095 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001096 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001098 (!(ip->i_d.di_flags &
1099 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
Christoph Hellwigc56c9632009-10-19 04:03:46 +00001100
1101 /*
1102 * If we can't get the iolock just skip truncating
1103 * the blocks past EOF because we could deadlock
1104 * with the mmap_sem otherwise. We'll get another
1105 * chance to drop them once the last reference to
1106 * the inode is dropped, so we'll never leak blocks
1107 * permanently.
1108 */
1109 error = xfs_free_eofblocks(mp, ip,
1110 XFS_FREE_EOF_TRYLOCK);
David Chinner92dfe8d2007-05-24 15:22:19 +10001111 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001112 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114 }
1115
1116 return 0;
1117}
1118
1119/*
1120 * xfs_inactive
1121 *
1122 * This is called when the vnode reference count for the vnode
1123 * goes to zero. If the file has been unlinked, then it must
1124 * now be truncated. Also, we clear all of the read-ahead state
1125 * kept for the inode here since the file is now closed.
1126 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001127int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001129 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001131 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 xfs_fsblock_t first_block;
1133 int committed;
1134 xfs_trans_t *tp;
1135 xfs_mount_t *mp;
1136 int error;
1137 int truncate;
1138
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001139 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /*
1142 * If the inode is already free, then there can be nothing
1143 * to clean up here.
1144 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +01001145 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 ASSERT(ip->i_df.if_real_bytes == 0);
1147 ASSERT(ip->i_df.if_broot_bytes == 0);
1148 return VN_INACTIVE_CACHE;
1149 }
1150
1151 /*
1152 * Only do a truncate if it's a regular file with
1153 * some actual space in it. It's OK to look at the
1154 * inode's fields without the lock because we're the
1155 * only one with a reference to the inode.
1156 */
1157 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001158 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1159 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1161
1162 mp = ip->i_mount;
1163
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001164 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1165 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 error = 0;
1168
1169 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001170 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 goto out;
1172
1173 if (ip->i_d.di_nlink != 0) {
1174 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001175 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001176 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001177 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1178 (!(ip->i_d.di_flags &
1179 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1180 (ip->i_delayed_blks != 0)))) {
Christoph Hellwigc56c9632009-10-19 04:03:46 +00001181 error = xfs_free_eofblocks(mp, ip, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001182 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001183 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185 goto out;
1186 }
1187
1188 ASSERT(ip->i_d.di_nlink == 0);
1189
Christoph Hellwig7d095252009-06-08 15:33:32 +02001190 error = xfs_qm_dqattach(ip, 0);
1191 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001192 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1195 if (truncate) {
1196 /*
1197 * Do the xfs_itruncate_start() call before
1198 * reserving any log space because itruncate_start
1199 * will call into the buffer cache and we can't
1200 * do that within a transaction.
1201 */
1202 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1203
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001204 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1205 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001206 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001207 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1208 return VN_INACTIVE_CACHE;
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 error = xfs_trans_reserve(tp, 0,
1212 XFS_ITRUNCATE_LOG_RES(mp),
1213 0, XFS_TRANS_PERM_LOG_RES,
1214 XFS_ITRUNCATE_LOG_COUNT);
1215 if (error) {
1216 /* Don't call itruncate_cleanup */
1217 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1218 xfs_trans_cancel(tp, 0);
1219 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001220 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
1223 xfs_ilock(ip, XFS_ILOCK_EXCL);
1224 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1225 xfs_trans_ihold(tp, ip);
1226
1227 /*
1228 * normally, we have to run xfs_itruncate_finish sync.
1229 * But if filesystem is wsync and we're in the inactive
1230 * path, then we know that nlink == 0, and that the
1231 * xaction that made nlink == 0 is permanently committed
1232 * since xfs_remove runs as a synchronous transaction.
1233 */
1234 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1235 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1236
1237 if (error) {
1238 xfs_trans_cancel(tp,
1239 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1240 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001241 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1244
1245 /*
1246 * If we get an error while cleaning up a
1247 * symlink we bail out.
1248 */
1249 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1250 xfs_inactive_symlink_rmt(ip, &tp) :
1251 xfs_inactive_symlink_local(ip, &tp);
1252
1253 if (error) {
1254 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001255 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
1258 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1259 xfs_trans_ihold(tp, ip);
1260 } else {
1261 error = xfs_trans_reserve(tp, 0,
1262 XFS_IFREE_LOG_RES(mp),
1263 0, XFS_TRANS_PERM_LOG_RES,
1264 XFS_INACTIVE_LOG_COUNT);
1265 if (error) {
1266 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1267 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001268 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
1271 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1272 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1273 xfs_trans_ihold(tp, ip);
1274 }
1275
1276 /*
1277 * If there are attributes associated with the file
1278 * then blow them away now. The code calls a routine
1279 * that recursively deconstructs the attribute fork.
1280 * We need to just commit the current transaction
1281 * because we can't use it for xfs_attr_inactive().
1282 */
1283 if (ip->i_d.di_anextents > 0) {
1284 error = xfs_inactive_attrs(ip, &tp);
1285 /*
1286 * If we got an error, the transaction is already
1287 * cancelled, and the inode is unlocked. Just get out.
1288 */
1289 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001290 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 } else if (ip->i_afp) {
1292 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1293 }
1294
1295 /*
1296 * Free the inode.
1297 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001298 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 error = xfs_ifree(tp, ip, &free_list);
1300 if (error) {
1301 /*
1302 * If we fail to free the inode, shut down. The cancel
1303 * might do that, we need to make sure. Otherwise the
1304 * inode might be lost for a long time or forever.
1305 */
1306 if (!XFS_FORCED_SHUTDOWN(mp)) {
1307 cmn_err(CE_NOTE,
1308 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1309 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001310 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1313 } else {
1314 /*
1315 * Credit the quota account(s). The inode is gone.
1316 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001317 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 /*
David Chinner78e9da72008-04-10 12:24:17 +10001320 * Just ignore errors at this point. There is nothing we can
1321 * do except to try to keep going. Make sure it's not a silent
1322 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 */
David Chinner78e9da72008-04-10 12:24:17 +10001324 error = xfs_bmap_finish(&tp, &free_list, &committed);
1325 if (error)
1326 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1327 "xfs_bmap_finish() returned error %d", error);
1328 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1329 if (error)
1330 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1331 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Christoph Hellwig7d095252009-06-08 15:33:32 +02001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /*
1335 * Release the dquots held by inode, if any.
1336 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001337 xfs_qm_dqdetach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1339
1340 out:
1341 return VN_INACTIVE_CACHE;
1342}
1343
Barry Naujok384f3ce2008-05-21 16:58:22 +10001344/*
1345 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1346 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1347 * ci_name->name will point to a the actual name (caller must free) or
1348 * will be set to NULL if an exact match is found.
1349 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001350int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001352 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001353 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001354 xfs_inode_t **ipp,
1355 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001357 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 int error;
1359 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001361 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1364 return XFS_ERROR(EIO);
1365
1366 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001367 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001369
1370 if (error)
1371 goto out;
1372
1373 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1374 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001375 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001376
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001377 return 0;
1378
Barry Naujok384f3ce2008-05-21 16:58:22 +10001379out_free_name:
1380 if (ci_name)
1381 kmem_free(ci_name->name);
1382out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001383 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return error;
1385}
1386
Christoph Hellwig993386c2007-08-28 16:12:30 +10001387int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001389 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001390 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001391 mode_t mode,
1392 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001393 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 cred_t *credp)
1395{
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001396 int is_dir = S_ISDIR(mode);
1397 struct xfs_mount *mp = dp->i_mount;
1398 struct xfs_inode *ip = NULL;
1399 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +10001400 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 xfs_bmap_free_t free_list;
1402 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001403 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 uint cancel_flags;
1405 int committed;
1406 xfs_prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001407 struct xfs_dquot *udqp = NULL;
1408 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001410 uint log_res;
1411 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001413 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001415 if (XFS_FORCED_SHUTDOWN(mp))
1416 return XFS_ERROR(EIO);
1417
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001418 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001420 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001421 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001422 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 if (error)
1425 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
Nathan Scott365ca832005-06-21 15:39:12 +10001428 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1429 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 else
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001431 prid = dfltprid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /*
1434 * Make sure that we have allocated dquot(s) on disk.
1435 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001436 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001437 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (error)
1439 goto std_return;
1440
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001441 if (is_dir) {
1442 rdev = 0;
1443 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1444 log_res = XFS_MKDIR_LOG_RES(mp);
1445 log_count = XFS_MKDIR_LOG_COUNT;
1446 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1447 } else {
1448 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1449 log_res = XFS_CREATE_LOG_RES(mp);
1450 log_count = XFS_CREATE_LOG_COUNT;
1451 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /*
1457 * Initially assume that the file does not exist and
1458 * reserve the resources for that case. If that is not
1459 * the case we'll drop the one we have and get a more
1460 * appropriate transaction later.
1461 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001462 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1463 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +02001465 /* flush outstanding delalloc blocks and retry */
1466 xfs_flush_inodes(dp);
Christoph Hellwig4734d402009-09-09 18:19:02 -05001467 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1468 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +02001469 }
1470 if (error == ENOSPC) {
1471 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001473 error = xfs_trans_reserve(tp, 0, log_res, 0,
1474 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476 if (error) {
1477 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001478 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001481 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001482 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001484 /*
1485 * Check for directory link count overflow.
1486 */
1487 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1488 error = XFS_ERROR(EMLINK);
1489 goto out_trans_cancel;
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001492 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 /*
1495 * Reserve disk quota and the inode.
1496 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001497 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001499 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Barry Naujok556b8b12008-04-10 12:22:07 +10001501 error = xfs_dir_canenter(tp, dp, name, resblks);
1502 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001503 goto out_trans_cancel;
1504
1505 /*
1506 * A newly created regular or special file just has one directory
1507 * entry pointing to them, but a directory also the "." entry
1508 * pointing to itself.
1509 */
1510 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1511 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (error) {
1513 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001514 goto out_trans_cancel;
1515 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 /*
1519 * At this point, we've gotten a newly allocated inode.
1520 * It is locked (and joined to the transaction).
1521 */
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10001522 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001525 * Now we join the directory inode to the transaction. We do not do it
1526 * earlier because xfs_dir_ialloc might commit the previous transaction
1527 * (and release all the locks). An error from here on will result in
1528 * the transaction cancel unlocking dp so don't do it explicitly in the
1529 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001531 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001533 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Barry Naujok556b8b12008-04-10 12:22:07 +10001535 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001536 &first_block, &free_list, resblks ?
1537 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (error) {
1539 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001540 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1543 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1544
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001545 if (is_dir) {
1546 error = xfs_dir_init(tp, ip, dp);
1547 if (error)
1548 goto out_bmap_cancel;
1549
1550 error = xfs_bumplink(tp, dp);
1551 if (error)
1552 goto out_bmap_cancel;
1553 }
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /*
1556 * If this is a synchronous mount, make sure that the
1557 * create transaction goes to disk before returning to
1558 * the user.
1559 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001560 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /*
1564 * Attach the dquot(s) to the inodes and modify them incore.
1565 * These ids of the inode couldn't have changed since the new
1566 * inode has been locked ever since it was created.
1567 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001568 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 /*
1571 * xfs_trans_commit normally decrements the vnode ref count
1572 * when it unlocks the inode. Since we want to return the
1573 * vnode to the caller, we bump the vnode ref count now.
1574 */
1575 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001577 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001578 if (error)
1579 goto out_abort_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001581 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (error) {
1583 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001584 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Christoph Hellwig7d095252009-06-08 15:33:32 +02001587 xfs_qm_dqrele(udqp);
1588 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001590 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /* Fallthrough to std_return with error = 0 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001593 std_return:
1594 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1595 XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
1596 ip, DM_RIGHT_NULL, name->name, NULL, mode,
1597 error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return error;
1601
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001602 out_bmap_cancel:
1603 xfs_bmap_cancel(&free_list);
1604 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001606 out_trans_cancel:
1607 xfs_trans_cancel(tp, cancel_flags);
1608 out_dqrele:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001609 xfs_qm_dqrele(udqp);
1610 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Christoph Hellwig993386c2007-08-28 16:12:30 +10001612 if (unlock_dp_on_error)
1613 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 goto std_return;
1616
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001617 out_abort_rele:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /*
1619 * Wait until after the current transaction is aborted to
1620 * release the inode. This prevents recursive transactions
1621 * and deadlocks from xfs_inactive.
1622 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001623 xfs_bmap_cancel(&free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 cancel_flags |= XFS_TRANS_ABORT;
1625 xfs_trans_cancel(tp, cancel_flags);
1626 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001627 unlock_dp_on_error = B_FALSE;
1628 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
1631#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632int xfs_locked_n;
1633int xfs_small_retries;
1634int xfs_middle_retries;
1635int xfs_lots_retries;
1636int xfs_lock_delays;
1637#endif
1638
1639/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001640 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1641 * a different value
1642 */
1643static inline int
1644xfs_lock_inumorder(int lock_mode, int subclass)
1645{
1646 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001647 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001648 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001649 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001650
1651 return lock_mode;
1652}
1653
1654/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 * The following routine will lock n inodes in exclusive mode.
1656 * We assume the caller calls us with the inodes in i_ino order.
1657 *
1658 * We need to detect deadlock where an inode that we lock
1659 * is in the AIL and we start waiting for another inode that is locked
1660 * by a thread in a long running transaction (such as truncate). This can
1661 * result in deadlock since the long running trans might need to wait
1662 * for the inode we just locked in order to push the tail and free space
1663 * in the log.
1664 */
1665void
1666xfs_lock_inodes(
1667 xfs_inode_t **ips,
1668 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 uint lock_mode)
1670{
1671 int attempts = 0, i, j, try_lock;
1672 xfs_log_item_t *lp;
1673
1674 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1675
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001676 try_lock = 0;
1677 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679again:
1680 for (; i < inodes; i++) {
1681 ASSERT(ips[i]);
1682
1683 if (i && (ips[i] == ips[i-1])) /* Already locked */
1684 continue;
1685
1686 /*
1687 * If try_lock is not set yet, make sure all locked inodes
1688 * are not in the AIL.
1689 * If any are, set try_lock to be used later.
1690 */
1691
1692 if (!try_lock) {
1693 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1694 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1695 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1696 try_lock++;
1697 }
1698 }
1699 }
1700
1701 /*
1702 * If any of the previous locks we have locked is in the AIL,
1703 * we must TRY to get the second and subsequent locks. If
1704 * we can't get any, we must release all we have
1705 * and try again.
1706 */
1707
1708 if (try_lock) {
1709 /* try_lock must be 0 if i is 0. */
1710 /*
1711 * try_lock means we have an inode locked
1712 * that is in the AIL.
1713 */
1714 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001715 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 attempts++;
1717
1718 /*
1719 * Unlock all previous guys and try again.
1720 * xfs_iunlock will try to push the tail
1721 * if the inode is in the AIL.
1722 */
1723
1724 for(j = i - 1; j >= 0; j--) {
1725
1726 /*
1727 * Check to see if we've already
1728 * unlocked this one.
1729 * Not the first one going back,
1730 * and the inode ptr is the same.
1731 */
1732 if ((j != (i - 1)) && ips[j] ==
1733 ips[j+1])
1734 continue;
1735
1736 xfs_iunlock(ips[j], lock_mode);
1737 }
1738
1739 if ((attempts % 5) == 0) {
1740 delay(1); /* Don't just spin the CPU */
1741#ifdef DEBUG
1742 xfs_lock_delays++;
1743#endif
1744 }
1745 i = 0;
1746 try_lock = 0;
1747 goto again;
1748 }
1749 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001750 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752 }
1753
1754#ifdef DEBUG
1755 if (attempts) {
1756 if (attempts < 5) xfs_small_retries++;
1757 else if (attempts < 100) xfs_middle_retries++;
1758 else xfs_lots_retries++;
1759 } else {
1760 xfs_locked_n++;
1761 }
1762#endif
1763}
1764
David Chinnerf9114eb2008-09-17 16:51:21 +10001765/*
1766 * xfs_lock_two_inodes() can only be used to lock one type of lock
1767 * at a time - the iolock or the ilock, but not both at once. If
1768 * we lock both at once, lockdep will report false positives saying
1769 * we have violated locking orders.
1770 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001771void
1772xfs_lock_two_inodes(
1773 xfs_inode_t *ip0,
1774 xfs_inode_t *ip1,
1775 uint lock_mode)
1776{
1777 xfs_inode_t *temp;
1778 int attempts = 0;
1779 xfs_log_item_t *lp;
1780
David Chinnerf9114eb2008-09-17 16:51:21 +10001781 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1782 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001783 ASSERT(ip0->i_ino != ip1->i_ino);
1784
1785 if (ip0->i_ino > ip1->i_ino) {
1786 temp = ip0;
1787 ip0 = ip1;
1788 ip1 = temp;
1789 }
1790
1791 again:
1792 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1793
1794 /*
1795 * If the first lock we have locked is in the AIL, we must TRY to get
1796 * the second lock. If we can't get it, we must release the first one
1797 * and try again.
1798 */
1799 lp = (xfs_log_item_t *)ip0->i_itemp;
1800 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1801 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1802 xfs_iunlock(ip0, lock_mode);
1803 if ((++attempts % 5) == 0)
1804 delay(1); /* Don't just spin the CPU */
1805 goto again;
1806 }
1807 } else {
1808 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1809 }
1810}
1811
Christoph Hellwig993386c2007-08-28 16:12:30 +10001812int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001814 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001815 struct xfs_name *name,
1816 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001818 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001820 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int error = 0;
1822 xfs_bmap_free_t free_list;
1823 xfs_fsblock_t first_block;
1824 int cancel_flags;
1825 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int link_zero;
1827 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001828 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001830 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001831 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (XFS_FORCED_SHUTDOWN(mp))
1834 return XFS_ERROR(EIO);
1835
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001836 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001837 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1838 NULL, DM_RIGHT_NULL, name->name, NULL,
1839 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (error)
1841 return error;
1842 }
1843
Christoph Hellwig7d095252009-06-08 15:33:32 +02001844 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001845 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Christoph Hellwig7d095252009-06-08 15:33:32 +02001848 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001849 if (error)
1850 goto std_return;
1851
1852 if (is_dir) {
1853 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1854 log_count = XFS_DEFAULT_LOG_COUNT;
1855 } else {
1856 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1857 log_count = XFS_REMOVE_LOG_COUNT;
1858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 /*
1862 * We try to get the real space reservation first,
1863 * allowing for directory btree deletion(s) implying
1864 * possible bmap insert(s). If we can't get the space
1865 * reservation then we use 0 instead, and avoid the bmap
1866 * btree insert(s) in the directory code by, if the bmap
1867 * insert tries to happen, instead trimming the LAST
1868 * block from the directory.
1869 */
1870 resblks = XFS_REMOVE_SPACE_RES(mp);
1871 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001872 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (error == ENOSPC) {
1874 resblks = 0;
1875 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001876 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878 if (error) {
1879 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001880 cancel_flags = 0;
1881 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001884 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 /*
1887 * At this point, we've gotten both the directory and the entry
1888 * inodes locked.
1889 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001890 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10001892
1893 IHOLD(dp);
1894 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001897 * If we're removing a directory perform some additional validation.
1898 */
1899 if (is_dir) {
1900 ASSERT(ip->i_d.di_nlink >= 2);
1901 if (ip->i_d.di_nlink != 2) {
1902 error = XFS_ERROR(ENOTEMPTY);
1903 goto out_trans_cancel;
1904 }
1905 if (!xfs_dir_isempty(ip)) {
1906 error = XFS_ERROR(ENOTEMPTY);
1907 goto out_trans_cancel;
1908 }
1909 }
1910
Eric Sandeen9d87c312009-01-14 23:22:07 -06001911 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001912 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001913 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (error) {
1915 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001916 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1919
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001920 if (is_dir) {
1921 /*
1922 * Drop the link from ip's "..".
1923 */
1924 error = xfs_droplink(tp, dp);
1925 if (error)
1926 goto out_bmap_cancel;
1927
1928 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001929 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001930 */
1931 error = xfs_droplink(tp, ip);
1932 if (error)
1933 goto out_bmap_cancel;
1934 } else {
1935 /*
1936 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +11001937 * inode here. For a directory this is done implicitly
1938 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001939 */
1940 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001943 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001944 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001945 */
1946 error = xfs_droplink(tp, ip);
1947 if (error)
1948 goto out_bmap_cancel;
1949
1950 /*
1951 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 * we are in the transaction.
1953 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001954 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 * If this is a synchronous mount, make sure that the
1958 * remove transaction goes to disk before returning to
1959 * the user.
1960 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001961 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001964 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001965 if (error)
1966 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001968 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001969 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001973 * If we are using filestreams, kill the stream association.
1974 * If the file is still open it may get a new one but that
1975 * will get killed on last close in xfs_close() so we don't
1976 * have to worry about that.
1977 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001978 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10001979 xfs_filestream_deassociate(ip);
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001982 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001983 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
1984 NULL, DM_RIGHT_NULL, name->name, NULL,
1985 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return error;
1989
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001990 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 xfs_bmap_cancel(&free_list);
1992 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001993 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 xfs_trans_cancel(tp, cancel_flags);
1995 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Christoph Hellwig993386c2007-08-28 16:12:30 +10001998int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002000 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002001 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002002 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002004 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 int error;
2007 xfs_bmap_free_t free_list;
2008 xfs_fsblock_t first_block;
2009 int cancel_flags;
2010 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002013 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002014 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002016 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (XFS_FORCED_SHUTDOWN(mp))
2019 return XFS_ERROR(EIO);
2020
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002021 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002023 tdp, DM_RIGHT_NULL,
2024 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002025 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (error)
2027 return error;
2028 }
2029
2030 /* Return through std_return after this point. */
2031
Christoph Hellwig7d095252009-06-08 15:33:32 +02002032 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01002033 if (error)
2034 goto std_return;
2035
Christoph Hellwig7d095252009-06-08 15:33:32 +02002036 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (error)
2038 goto std_return;
2039
2040 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2041 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002042 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2044 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2045 if (error == ENOSPC) {
2046 resblks = 0;
2047 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2048 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2049 }
2050 if (error) {
2051 cancel_flags = 0;
2052 goto error_return;
2053 }
2054
Christoph Hellwige1cccd92008-08-13 16:18:07 +10002055 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 /*
2058 * Increment vnode ref counts since xfs_trans_commit &
2059 * xfs_trans_cancel will both unlock the inodes and
2060 * decrement the associated ref counts.
2061 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002062 IHOLD(sip);
2063 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2065 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2066
2067 /*
2068 * If the source has too many links, we can't make any more to it.
2069 */
2070 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2071 error = XFS_ERROR(EMLINK);
2072 goto error_return;
2073 }
2074
Nathan Scott365ca832005-06-21 15:39:12 +10002075 /*
2076 * If we are using project inheritance, we only allow hard link
2077 * creation in our tree when the project IDs are the same; else
2078 * the tree quota mechanism could be circumvented.
2079 */
2080 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2081 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002082 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002083 goto error_return;
2084 }
2085
Barry Naujok556b8b12008-04-10 12:22:07 +10002086 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2087 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 goto error_return;
2089
Eric Sandeen9d87c312009-01-14 23:22:07 -06002090 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Barry Naujok556b8b12008-04-10 12:22:07 +10002092 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2093 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (error)
2095 goto abort_return;
2096 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2098
2099 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002100 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /*
2104 * If this is a synchronous mount, make sure that the
2105 * link transaction goes to disk before returning to
2106 * the user.
2107 */
2108 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2109 xfs_trans_set_sync(tp);
2110 }
2111
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002112 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (error) {
2114 xfs_bmap_cancel(&free_list);
2115 goto abort_return;
2116 }
2117
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002118 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002119 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 /* Fall through to std_return with error = 0. */
2123std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002124 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002126 tdp, DM_RIGHT_NULL,
2127 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002128 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130 return error;
2131
2132 abort_return:
2133 cancel_flags |= XFS_TRANS_ABORT;
2134 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 error_return:
2137 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 goto std_return;
2139}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002140
Christoph Hellwig993386c2007-08-28 16:12:30 +10002141int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002143 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002144 struct xfs_name *link_name,
2145 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002146 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002147 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 cred_t *credp)
2149{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002150 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 xfs_inode_t *ip;
2153 int error;
2154 int pathlen;
2155 xfs_bmap_free_t free_list;
2156 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002157 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 uint cancel_flags;
2159 int committed;
2160 xfs_fileoff_t first_fsb;
2161 xfs_filblks_t fs_blocks;
2162 int nmaps;
2163 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2164 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002165 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 int byte_cnt;
2167 int n;
2168 xfs_buf_t *bp;
2169 xfs_prid_t prid;
2170 struct xfs_dquot *udqp, *gdqp;
2171 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Christoph Hellwig3937be52008-03-06 13:46:19 +11002173 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 error = 0;
2175 ip = NULL;
2176 tp = NULL;
2177
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002178 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 if (XFS_FORCED_SHUTDOWN(mp))
2181 return XFS_ERROR(EIO);
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 /*
2184 * Check component lengths of the target path name.
2185 */
2186 pathlen = strlen(target_path);
2187 if (pathlen >= MAXPATHLEN) /* total string too long */
2188 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002190 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002191 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Dave Chinner046ea752010-01-20 10:47:08 +11002193 link_name->name,
2194 (unsigned char *)target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (error)
2196 return error;
2197 }
2198
2199 /* Return through std_return after this point. */
2200
2201 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002202 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2203 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 else
2205 prid = (xfs_prid_t)dfltprid;
2206
2207 /*
2208 * Make sure that we have allocated dquot(s) on disk.
2209 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002210 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2212 if (error)
2213 goto std_return;
2214
2215 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2216 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2217 /*
2218 * The symlink will fit into the inode data fork?
2219 * There can't be any attributes so we get the whole variable part.
2220 */
2221 if (pathlen <= XFS_LITINO(mp))
2222 fs_blocks = 0;
2223 else
2224 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002225 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2227 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2228 if (error == ENOSPC && fs_blocks == 0) {
2229 resblks = 0;
2230 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2231 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2232 }
2233 if (error) {
2234 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 goto error_return;
2236 }
2237
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002238 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002239 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 /*
2242 * Check whether the directory allows new symlinks or not.
2243 */
2244 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2245 error = XFS_ERROR(EPERM);
2246 goto error_return;
2247 }
2248
2249 /*
2250 * Reserve disk quota : blocks and inode.
2251 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002252 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (error)
2254 goto error_return;
2255
2256 /*
2257 * Check for ability to enter directory entry, if no space reserved.
2258 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002259 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2260 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 goto error_return;
2262 /*
2263 * Initialize the bmap freelist prior to calling either
2264 * bmapi or the directory create code.
2265 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002266 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 /*
2269 * Allocate an inode for the symlink.
2270 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002271 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 1, 0, credp, prid, resblks > 0, &ip, NULL);
2273 if (error) {
2274 if (error == ENOSPC)
2275 goto error_return;
2276 goto error1;
2277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Christoph Hellwig993386c2007-08-28 16:12:30 +10002279 /*
2280 * An error after we've joined dp to the transaction will result in the
2281 * transaction cancel unlocking dp so don't do it explicitly in the
2282 * error path.
2283 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002284 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002286 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 /*
2289 * Also attach the dquot(s) to it, if applicable.
2290 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002291 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 if (resblks)
2294 resblks -= XFS_IALLOC_SPACE_RES(mp);
2295 /*
2296 * If the symlink will fit into the inode, write it inline.
2297 */
2298 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2299 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2300 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2301 ip->i_d.di_size = pathlen;
2302
2303 /*
2304 * The inode was initially created in extent format.
2305 */
2306 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2307 ip->i_df.if_flags |= XFS_IFINLINE;
2308
2309 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2310 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2311
2312 } else {
2313 first_fsb = 0;
2314 nmaps = SYMLINK_MAPS;
2315
2316 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2317 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2318 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002319 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (error) {
2321 goto error1;
2322 }
2323
2324 if (resblks)
2325 resblks -= fs_blocks;
2326 ip->i_d.di_size = pathlen;
2327 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2328
2329 cur_chunk = target_path;
2330 for (n = 0; n < nmaps; n++) {
2331 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2332 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2333 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2334 BTOBB(byte_cnt), 0);
2335 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2336 if (pathlen < byte_cnt) {
2337 byte_cnt = pathlen;
2338 }
2339 pathlen -= byte_cnt;
2340
2341 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2342 cur_chunk += byte_cnt;
2343
2344 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2345 }
2346 }
2347
2348 /*
2349 * Create the directory entry for the symlink.
2350 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002351 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2352 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002353 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2356 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2357
2358 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 * If this is a synchronous mount, make sure that the
2360 * symlink transaction goes to disk before returning to
2361 * the user.
2362 */
2363 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2364 xfs_trans_set_sync(tp);
2365 }
2366
2367 /*
2368 * xfs_trans_commit normally decrements the vnode ref count
2369 * when it unlocks the inode. Since we want to return the
2370 * vnode to the caller, we bump the vnode ref count now.
2371 */
2372 IHOLD(ip);
2373
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002374 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (error) {
2376 goto error2;
2377 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002378 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002379 xfs_qm_dqrele(udqp);
2380 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 /* Fall through to std_return with error = 0 or errno from
2383 * xfs_trans_commit */
2384std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10002385 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002387 dp, DM_RIGHT_NULL,
2388 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002389 DM_RIGHT_NULL, link_name->name,
Dave Chinner046ea752010-01-20 10:47:08 +11002390 (unsigned char *)target_path,
2391 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393
Christoph Hellwig3937be52008-03-06 13:46:19 +11002394 if (!error)
2395 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return error;
2397
2398 error2:
2399 IRELE(ip);
2400 error1:
2401 xfs_bmap_cancel(&free_list);
2402 cancel_flags |= XFS_TRANS_ABORT;
2403 error_return:
2404 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002405 xfs_qm_dqrele(udqp);
2406 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Christoph Hellwig993386c2007-08-28 16:12:30 +10002408 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 goto std_return;
2412}
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002415xfs_set_dmattrs(
2416 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002418 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002420 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 int error;
2423
2424 if (!capable(CAP_SYS_ADMIN))
2425 return XFS_ERROR(EPERM);
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (XFS_FORCED_SHUTDOWN(mp))
2428 return XFS_ERROR(EIO);
2429
2430 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2431 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2432 if (error) {
2433 xfs_trans_cancel(tp, 0);
2434 return error;
2435 }
2436 xfs_ilock(ip, XFS_ILOCK_EXCL);
2437 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2438
Christoph Hellwig613d7042007-10-11 17:44:08 +10002439 ip->i_d.di_dmevmask = evmask;
2440 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2443 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002444 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 return error;
2447}
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449/*
2450 * xfs_alloc_file_space()
2451 * This routine allocates disk space for the given file.
2452 *
2453 * If alloc_type == 0, this request is for an ALLOCSP type
2454 * request which will change the file size. In this case, no
2455 * DMAPI event will be generated by the call. A TRUNCATE event
2456 * will be generated later by xfs_setattr.
2457 *
2458 * If alloc_type != 0, this request is for a RESVSP type
2459 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2460 * lower block boundary byte address is less than the file's
2461 * length.
2462 *
2463 * RETURNS:
2464 * 0 on success
2465 * errno on error
2466 *
2467 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002468STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469xfs_alloc_file_space(
2470 xfs_inode_t *ip,
2471 xfs_off_t offset,
2472 xfs_off_t len,
2473 int alloc_type,
2474 int attr_flags)
2475{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002476 xfs_mount_t *mp = ip->i_mount;
2477 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 xfs_filblks_t allocated_fsb;
2479 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002480 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002482 xfs_fsblock_t firstfsb;
2483 int nimaps;
2484 int bmapi_flag;
2485 int quota_flag;
2486 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002488 xfs_bmbt_irec_t imaps[1], *imapp;
2489 xfs_bmap_free_t free_list;
2490 uint qblocks, resblks, resrtextents;
2491 int committed;
2492 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002494 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 if (XFS_FORCED_SHUTDOWN(mp))
2497 return XFS_ERROR(EIO);
2498
Christoph Hellwig7d095252009-06-08 15:33:32 +02002499 error = xfs_qm_dqattach(ip, 0);
2500 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return error;
2502
2503 if (len <= 0)
2504 return XFS_ERROR(EINVAL);
2505
David Chinner957d0eb2007-06-18 16:50:37 +10002506 rt = XFS_IS_REALTIME_INODE(ip);
2507 extsz = xfs_get_extsz_hint(ip);
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11002511 nimaps = 1;
2512 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2514 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2515
2516 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002517 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002518 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002519 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 xfs_off_t end_dmi_offset;
2521
2522 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002523 if (end_dmi_offset > ip->i_size)
2524 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002525 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2526 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002528 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530
2531 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002532 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 */
2534retry:
2535 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002536 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Nathan Scottdd9f4382006-01-11 15:28:28 +11002538 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11002539 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002540 */
2541 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002543 do_div(s, extsz);
2544 s *= extsz;
2545 e = startoffset_fsb + allocatesize_fsb;
2546 if ((temp = do_mod(startoffset_fsb, extsz)))
2547 e += temp;
2548 if ((temp = do_mod(e, extsz)))
2549 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002551 s = 0;
2552 e = allocatesize_fsb;
2553 }
2554
2555 if (unlikely(rt)) {
2556 resrtextents = qblocks = (uint)(e - s);
2557 resrtextents /= mp->m_sb.sb_rextsize;
2558 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2559 quota_flag = XFS_QMOPT_RES_RTBLKS;
2560 } else {
2561 resrtextents = 0;
2562 resblks = qblocks = \
2563 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2564 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
2566
2567 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002568 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 */
2570 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002571 error = xfs_trans_reserve(tp, resblks,
2572 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 XFS_TRANS_PERM_LOG_RES,
2574 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002576 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 */
2578 if (error) {
2579 /*
2580 * Free the transaction structure.
2581 */
2582 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2583 xfs_trans_cancel(tp, 0);
2584 break;
2585 }
2586 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002587 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2588 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (error)
2590 goto error1;
2591
2592 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2593 xfs_trans_ihold(tp, ip);
2594
2595 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002596 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002598 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002599 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002600 allocatesize_fsb, bmapi_flag,
2601 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002602 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if (error) {
2604 goto error0;
2605 }
2606
2607 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002608 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002610 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (error) {
2612 goto error0;
2613 }
2614
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002615 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2617 if (error) {
2618 break;
2619 }
2620
2621 allocated_fsb = imapp->br_blockcount;
2622
Nathan Scottdd9f4382006-01-11 15:28:28 +11002623 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 error = XFS_ERROR(ENOSPC);
2625 break;
2626 }
2627
2628 startoffset_fsb += allocated_fsb;
2629 allocatesize_fsb -= allocated_fsb;
2630 }
2631dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002632 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002633 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002635 ip, DM_RIGHT_NULL,
2636 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
2638 if (error == 0)
2639 goto retry; /* Maybe DMAPI app. has made space */
2640 /* else fall through with error from XFS_SEND_DATA */
2641 }
2642
2643 return error;
2644
Nathan Scottdd9f4382006-01-11 15:28:28 +11002645error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002647 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002648
2649error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2651 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2652 goto dmapi_enospc_check;
2653}
2654
2655/*
2656 * Zero file bytes between startoff and endoff inclusive.
2657 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002658 *
2659 * This function is used by xfs_free_file_space() to zero
2660 * partial blocks when the range to free is not block aligned.
2661 * When unreserving space with boundaries that are not block
2662 * aligned we round up the start and round down the end
2663 * boundaries and then use this function to zero the parts of
2664 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 */
2666STATIC int
2667xfs_zero_remaining_bytes(
2668 xfs_inode_t *ip,
2669 xfs_off_t startoff,
2670 xfs_off_t endoff)
2671{
2672 xfs_bmbt_irec_t imap;
2673 xfs_fileoff_t offset_fsb;
2674 xfs_off_t lastoffset;
2675 xfs_off_t offset;
2676 xfs_buf_t *bp;
2677 xfs_mount_t *mp = ip->i_mount;
2678 int nimap;
2679 int error = 0;
2680
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002681 /*
2682 * Avoid doing I/O beyond eof - it's not necessary
2683 * since nothing can read beyond eof. The space will
2684 * be zeroed when the file is extended anyway.
2685 */
2686 if (startoff >= ip->i_size)
2687 return 0;
2688
2689 if (endoff > ip->i_size)
2690 endoff = ip->i_size;
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002693 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 mp->m_rtdev_targp : mp->m_ddev_targp);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11002695 if (!bp)
2696 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2699 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2700 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002701 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002702 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (error || nimap < 1)
2704 break;
2705 ASSERT(imap.br_blockcount >= 1);
2706 ASSERT(imap.br_startoff == offset_fsb);
2707 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2708 if (lastoffset > endoff)
2709 lastoffset = endoff;
2710 if (imap.br_startblock == HOLESTARTBLOCK)
2711 continue;
2712 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2713 if (imap.br_state == XFS_EXT_UNWRITTEN)
2714 continue;
2715 XFS_BUF_UNDONE(bp);
2716 XFS_BUF_UNWRITE(bp);
2717 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002718 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002720 error = xfs_iowait(bp);
2721 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2723 mp, bp, XFS_BUF_ADDR(bp));
2724 break;
2725 }
2726 memset(XFS_BUF_PTR(bp) +
2727 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2728 0, lastoffset - offset + 1);
2729 XFS_BUF_UNDONE(bp);
2730 XFS_BUF_UNREAD(bp);
2731 XFS_BUF_WRITE(bp);
2732 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002733 error = xfs_iowait(bp);
2734 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2736 mp, bp, XFS_BUF_ADDR(bp));
2737 break;
2738 }
2739 }
2740 xfs_buf_free(bp);
2741 return error;
2742}
2743
2744/*
2745 * xfs_free_file_space()
2746 * This routine frees disk space for the given file.
2747 *
2748 * This routine is only called by xfs_change_file_space
2749 * for an UNRESVSP type call.
2750 *
2751 * RETURNS:
2752 * 0 on success
2753 * errno on error
2754 *
2755 */
2756STATIC int
2757xfs_free_file_space(
2758 xfs_inode_t *ip,
2759 xfs_off_t offset,
2760 xfs_off_t len,
2761 int attr_flags)
2762{
2763 int committed;
2764 int done;
2765 xfs_off_t end_dmi_offset;
2766 xfs_fileoff_t endoffset_fsb;
2767 int error;
2768 xfs_fsblock_t firstfsb;
2769 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 xfs_bmbt_irec_t imap;
2771 xfs_off_t ioffset;
2772 xfs_extlen_t mod=0;
2773 xfs_mount_t *mp;
2774 int nimap;
2775 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002776 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 int rt;
2778 xfs_fileoff_t startoffset_fsb;
2779 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002780 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 mp = ip->i_mount;
2783
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002784 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002785
Christoph Hellwig7d095252009-06-08 15:33:32 +02002786 error = xfs_qm_dqattach(ip, 0);
2787 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return error;
2789
2790 error = 0;
2791 if (len <= 0) /* if nothing being freed */
2792 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002793 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2795 end_dmi_offset = offset + len;
2796 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
2797
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002798 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002799 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002800 if (end_dmi_offset > ip->i_size)
2801 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002802 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 offset, end_dmi_offset - offset,
2804 AT_DELAY_FLAG(attr_flags), NULL);
2805 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002806 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 }
2808
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002809 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002810 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002811 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002813 /* wait for the completion of any pending DIOs */
2814 xfs_ioend_wait(ip);
Yingping Lu9fa80462006-03-22 12:44:35 +11002815 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002816
Tim Shimmine6a4b372007-11-23 16:30:42 +11002817 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002819
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002820 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002821 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10002822 if (error)
2823 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002824 }
2825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 /*
2827 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002828 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2830 * will take care of it for us.
2831 */
Eric Sandeen62118702008-03-06 13:44:28 +11002832 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002834 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002835 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 if (error)
2837 goto out_unlock_iolock;
2838 ASSERT(nimap == 0 || nimap == 1);
2839 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2840 xfs_daddr_t block;
2841
2842 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2843 block = imap.br_startblock;
2844 mod = do_div(block, mp->m_sb.sb_rextsize);
2845 if (mod)
2846 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2847 }
2848 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002849 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002850 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (error)
2852 goto out_unlock_iolock;
2853 ASSERT(nimap == 0 || nimap == 1);
2854 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2855 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2856 mod++;
2857 if (mod && (mod != mp->m_sb.sb_rextsize))
2858 endoffset_fsb -= mod;
2859 }
2860 }
2861 if ((done = (endoffset_fsb <= startoffset_fsb)))
2862 /*
2863 * One contiguous piece to clear
2864 */
2865 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2866 else {
2867 /*
2868 * Some full blocks, possibly two pieces to clear
2869 */
2870 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2871 error = xfs_zero_remaining_bytes(ip, offset,
2872 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2873 if (!error &&
2874 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2875 error = xfs_zero_remaining_bytes(ip,
2876 XFS_FSB_TO_B(mp, endoffset_fsb),
2877 offset + len - 1);
2878 }
2879
2880 /*
2881 * free file space until done or until there is an error
2882 */
2883 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2884 while (!error && !done) {
2885
2886 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002887 * allocate and setup the transaction. Allow this
2888 * transaction to dip into the reserve blocks to ensure
2889 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 */
2891 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002892 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 error = xfs_trans_reserve(tp,
2894 resblks,
2895 XFS_WRITE_LOG_RES(mp),
2896 0,
2897 XFS_TRANS_PERM_LOG_RES,
2898 XFS_WRITE_LOG_COUNT);
2899
2900 /*
2901 * check for running out of space
2902 */
2903 if (error) {
2904 /*
2905 * Free the transaction structure.
2906 */
2907 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2908 xfs_trans_cancel(tp, 0);
2909 break;
2910 }
2911 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002912 error = xfs_trans_reserve_quota(tp, mp,
2913 ip->i_udquot, ip->i_gdquot,
2914 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (error)
2916 goto error1;
2917
2918 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2919 xfs_trans_ihold(tp, ip);
2920
2921 /*
2922 * issue the bunmapi() call to free the blocks
2923 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002924 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002925 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002927 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if (error) {
2929 goto error0;
2930 }
2931
2932 /*
2933 * complete the transaction
2934 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002935 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (error) {
2937 goto error0;
2938 }
2939
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002940 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2942 }
2943
2944 out_unlock_iolock:
2945 if (need_iolock)
2946 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2947 return error;
2948
2949 error0:
2950 xfs_bmap_cancel(&free_list);
2951 error1:
2952 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2953 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2954 XFS_ILOCK_EXCL);
2955 return error;
2956}
2957
2958/*
2959 * xfs_change_file_space()
2960 * This routine allocates or frees disk space for the given file.
2961 * The user specified parameters are checked for alignment and size
2962 * limitations.
2963 *
2964 * RETURNS:
2965 * 0 on success
2966 * errno on error
2967 *
2968 */
2969int
2970xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002971 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 int cmd,
2973 xfs_flock64_t *bf,
2974 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 int attr_flags)
2976{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002977 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 int clrprealloc;
2979 int error;
2980 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 int setprealloc;
2982 xfs_off_t startoffset;
2983 xfs_off_t llen;
2984 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002985 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002987 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Christoph Hellwig993386c2007-08-28 16:12:30 +10002989 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return XFS_ERROR(EINVAL);
2991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 switch (bf->l_whence) {
2993 case 0: /*SEEK_SET*/
2994 break;
2995 case 1: /*SEEK_CUR*/
2996 bf->l_start += offset;
2997 break;
2998 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002999 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 break;
3001 default:
3002 return XFS_ERROR(EINVAL);
3003 }
3004
3005 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3006
3007 if ( (bf->l_start < 0)
3008 || (bf->l_start > XFS_MAXIOFFSET(mp))
3009 || (bf->l_start + llen < 0)
3010 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3011 return XFS_ERROR(EINVAL);
3012
3013 bf->l_whence = 0;
3014
3015 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003016 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 /*
3019 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3020 * file space.
3021 * These calls do NOT zero the data space allocated to the file,
3022 * nor do they change the file size.
3023 *
3024 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3025 * space.
3026 * These calls cause the new file data to be zeroed and the file
3027 * size to be changed.
3028 */
3029 setprealloc = clrprealloc = 0;
3030
3031 switch (cmd) {
3032 case XFS_IOC_RESVSP:
3033 case XFS_IOC_RESVSP64:
3034 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3035 1, attr_flags);
3036 if (error)
3037 return error;
3038 setprealloc = 1;
3039 break;
3040
3041 case XFS_IOC_UNRESVSP:
3042 case XFS_IOC_UNRESVSP64:
3043 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3044 attr_flags)))
3045 return error;
3046 break;
3047
3048 case XFS_IOC_ALLOCSP:
3049 case XFS_IOC_ALLOCSP64:
3050 case XFS_IOC_FREESP:
3051 case XFS_IOC_FREESP64:
3052 if (startoffset > fsize) {
3053 error = xfs_alloc_file_space(ip, fsize,
3054 startoffset - fsize, 0, attr_flags);
3055 if (error)
3056 break;
3057 }
3058
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003059 iattr.ia_valid = ATTR_SIZE;
3060 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +11003062 error = xfs_setattr(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 if (error)
3065 return error;
3066
3067 clrprealloc = 1;
3068 break;
3069
3070 default:
3071 ASSERT(0);
3072 return XFS_ERROR(EINVAL);
3073 }
3074
3075 /*
3076 * update the inode timestamp, mode, and prealloc flag bits
3077 */
3078 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3079
3080 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3081 0, 0, 0))) {
3082 /* ASSERT(0); */
3083 xfs_trans_cancel(tp, 0);
3084 return error;
3085 }
3086
3087 xfs_ilock(ip, XFS_ILOCK_EXCL);
3088
3089 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3090 xfs_trans_ihold(tp, ip);
3091
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003092 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 ip->i_d.di_mode &= ~S_ISUID;
3094
3095 /*
3096 * Note that we don't have to worry about mandatory
3097 * file locking being disabled here because we only
3098 * clear the S_ISGID bit if the Group execute bit is
3099 * on, but if it was on then mandatory locking wouldn't
3100 * have been enabled.
3101 */
3102 if (ip->i_d.di_mode & S_IXGRP)
3103 ip->i_d.di_mode &= ~S_ISGID;
3104
3105 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3106 }
3107 if (setprealloc)
3108 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3109 else if (clrprealloc)
3110 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3111
3112 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3113 xfs_trans_set_sync(tp);
3114
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003115 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3118
3119 return error;
3120}