blob: 578f3f59b789e7783c1c33343e0b8579eab46048 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Christoph Hellwig993386c2007-08-28 16:12:30 +100057int
Linus Torvalds1da177e2005-04-16 15:20:36 -070058xfs_setattr(
Christoph Hellwig0f285c82008-07-18 17:13:28 +100059 struct xfs_inode *ip,
60 struct iattr *iattr,
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +110061 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Christoph Hellwig993386c2007-08-28 16:12:30 +100063 xfs_mount_t *mp = ip->i_mount;
David Chinnere4f75292008-08-13 16:00:45 +100064 struct inode *inode = VFS_I(ip);
Christoph Hellwig0f285c82008-07-18 17:13:28 +100065 int mask = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int code;
68 uint lock_flags;
69 uint commit_flags=0;
70 uid_t uid=0, iuid=0;
71 gid_t gid=0, igid=0;
72 int timeflags = 0;
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)) {
137 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 (mp->m_flags & XFS_MOUNT_WSYNC)) {
139 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
140 commit_flags = 0;
141 if ((code = xfs_trans_reserve(tp, 0,
142 XFS_ICHANGE_LOG_RES(mp), 0,
143 0, 0))) {
144 lock_flags = 0;
145 goto error_return;
146 }
147 }
148 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000149 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000150 !(flags & XFS_ATTR_DMI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100152 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000153 iattr->ia_size, 0, dmflags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (code) {
155 lock_flags = 0;
156 goto error_return;
157 }
158 }
159 if (need_iolock)
160 lock_flags |= XFS_IOLOCK_EXCL;
161 }
162
163 xfs_ilock(ip, lock_flags);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
166 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000168 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /*
170 * These IDs could have changed since we last looked at them.
171 * But, we're assured that if the ownership did change
172 * while we didn't have the inode locked, inode's dquot(s)
173 * would have changed also.
174 */
175 iuid = ip->i_d.di_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 igid = ip->i_d.di_gid;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000177 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
178 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000181 * Do a quota reservation only if uid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * going to change.
183 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200184 if (XFS_IS_QUOTA_RUNNING(mp) &&
185 ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
186 (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ASSERT(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200188 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 capable(CAP_FOWNER) ?
190 XFS_QMOPT_FORCE_RES : 0);
191 if (code) /* out of quota */
192 goto error_return;
193 }
194 }
195
196 /*
197 * Truncate file. Must have write permission and not be a directory.
198 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000199 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* Short circuit the truncate case for zero length files */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000201 if (iattr->ia_size == 0 &&
202 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 xfs_iunlock(ip, XFS_ILOCK_EXCL);
204 lock_flags &= ~XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000205 if (mask & ATTR_CTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
207 code = 0;
208 goto error_return;
209 }
210
Christoph Hellwig42173f62008-04-22 17:33:25 +1000211 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 code = XFS_ERROR(EISDIR);
213 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000214 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 code = XFS_ERROR(EINVAL);
216 goto error_return;
217 }
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /*
220 * Make sure that the dquots are attached to the inode.
221 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200222 code = xfs_qm_dqattach_locked(ip, 0);
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500223 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500226 /*
227 * Now we can make the changes. Before we join the inode
228 * to the transaction, if ATTR_SIZE is set then take care of
229 * the part of the truncation that must be done without the
230 * inode lock. This needs to be done before joining the inode
231 * to the transaction, because the inode cannot be unlocked
232 * once it is a part of the transaction.
233 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000234 if (iattr->ia_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000235 /*
236 * Do the first part of growing a file: zero any data
237 * in the last block that is beyond the old EOF. We
238 * need to do this before the inode is joined to the
239 * transaction to modify the i_size.
240 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000241 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000244
245 /*
246 * We are going to log the inode size change in this
247 * transaction so any previous writes that are beyond the on
248 * disk EOF and the new EOF that have not been written out need
249 * to be written here. If we do not write the data out, we
250 * expose ourselves to the null files problem.
251 *
252 * Only flush from the on disk size to the smaller of the in
253 * memory file size or the new size as that's the range we
254 * really care about here and prevents waiting for other data
255 * not within the range we care about here.
256 */
257 if (!code &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000258 ip->i_size != ip->i_d.di_size &&
259 iattr->ia_size > ip->i_d.di_size) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000260 code = xfs_flush_pages(ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000261 ip->i_d.di_size, iattr->ia_size,
David Chinnerc32676e2007-07-19 16:28:58 +1000262 XFS_B_ASYNC, FI_NONE);
263 }
264
265 /* wait for all I/O to complete */
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100266 xfs_ioend_wait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!code)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000269 code = xfs_itruncate_data(ip, iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (code) {
271 ASSERT(tp == NULL);
272 lock_flags &= ~XFS_ILOCK_EXCL;
273 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
274 goto error_return;
275 }
276 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
277 if ((code = xfs_trans_reserve(tp, 0,
278 XFS_ITRUNCATE_LOG_RES(mp), 0,
279 XFS_TRANS_PERM_LOG_RES,
280 XFS_ITRUNCATE_LOG_COUNT))) {
281 xfs_trans_cancel(tp, 0);
282 if (need_iolock)
283 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
284 return code;
285 }
286 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
287 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 xfs_trans_ijoin(tp, ip, lock_flags);
290 xfs_trans_ihold(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
David Chinner44d814c2008-03-06 13:45:29 +1100292 /*
293 * Only change the c/mtime if we are changing the size
294 * or we are explicitly asked to change it. This handles
295 * the semantic difference between truncate() and ftruncate()
296 * as implemented in the VFS.
297 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000298 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
David Chinner44d814c2008-03-06 13:45:29 +1100299 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
300
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000301 if (iattr->ia_size > ip->i_size) {
302 ip->i_d.di_size = iattr->ia_size;
303 ip->i_size = iattr->ia_size;
304 if (!(flags & XFS_ATTR_DMI))
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000305 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
306 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000307 } else if (iattr->ia_size <= ip->i_size ||
308 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /*
310 * signal a sync transaction unless
311 * we're truncating an already unlinked
312 * file on a wsync filesystem
313 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000314 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 XFS_DATA_FORK,
316 ((ip->i_d.di_nlink != 0 ||
317 !(mp->m_flags & XFS_MOUNT_WSYNC))
318 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000319 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000321 /*
322 * Truncated "down", so we're removing references
323 * to old data here - if we now delay flushing for
324 * a long time, we expose ourselves unduly to the
325 * notorious NULL files problem. So, we mark this
326 * vnode and flush it when the file is closed, and
327 * do not wait the usual (long) time for writeout.
328 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000329 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500331 } else if (tp) {
332 xfs_trans_ijoin(tp, ip, lock_flags);
333 xfs_trans_ihold(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
336 /*
337 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000339 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /*
341 * CAP_FSETID overrides the following restrictions:
342 *
343 * The set-user-ID and set-group-ID bits of a file will be
344 * cleared upon successful return from chown()
345 */
346 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
347 !capable(CAP_FSETID)) {
348 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
349 }
350
351 /*
352 * Change the ownerships and register quota modifications
353 * in the transaction.
354 */
355 if (iuid != uid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200356 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000357 ASSERT(mask & ATTR_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 ASSERT(udqp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200359 olddquot1 = xfs_qm_vop_chown(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 &ip->i_udquot, udqp);
361 }
362 ip->i_d.di_uid = uid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000363 inode->i_uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 if (igid != gid) {
Christoph Hellwig7d095252009-06-08 15:33:32 +0200366 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000367 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000368 ASSERT(mask & ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 ASSERT(gdqp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200370 olddquot2 = xfs_qm_vop_chown(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 &ip->i_gdquot, gdqp);
372 }
373 ip->i_d.di_gid = gid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000374 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
378 timeflags |= XFS_ICHGTIME_CHG;
379 }
380
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500381 /*
382 * Change file access modes.
383 */
384 if (mask & ATTR_MODE) {
385 umode_t mode = iattr->ia_mode;
386
387 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
388 mode &= ~S_ISGID;
389
390 ip->i_d.di_mode &= S_IFMT;
391 ip->i_d.di_mode |= mode & ~S_IFMT;
392
393 inode->i_mode &= S_IFMT;
394 inode->i_mode |= mode & ~S_IFMT;
395
396 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
397 timeflags |= XFS_ICHGTIME_CHG;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
401 * Change file access or modified times.
402 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000403 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
404 if (mask & ATTR_ATIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000405 inode->i_atime = iattr->ia_atime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000406 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
407 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 ip->i_update_core = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000410 if (mask & ATTR_MTIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000411 inode->i_mtime = iattr->ia_mtime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000412 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
413 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 timeflags &= ~XFS_ICHGTIME_MOD;
415 timeflags |= XFS_ICHGTIME_CHG;
416 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000417 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
419 }
420
421 /*
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000422 * Change file inode change time only if ATTR_CTIME set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * AND we have been called by a DMI function.
424 */
425
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000426 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000427 inode->i_ctime = iattr->ia_ctime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000428 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
429 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ip->i_update_core = 1;
431 timeflags &= ~XFS_ICHGTIME_CHG;
432 }
433
434 /*
435 * Send out timestamp changes that need to be set to the
436 * current time. Not done when called by a DMI function.
437 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000438 if (timeflags && !(flags & XFS_ATTR_DMI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 xfs_ichgtime(ip, timeflags);
440
441 XFS_STATS_INC(xs_ig_attrchg);
442
443 /*
444 * If this is a synchronous mount, make sure that the
445 * transaction goes to disk before returning to the user.
446 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000447 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * One for the truncate and one for the timestamps since we
449 * don't want to change the timestamps unless we're sure the
450 * truncate worked. Truncates are less than 1% of the laddis
451 * mix so this probably isn't worth the trouble to optimize.
452 */
453 code = 0;
454 if (tp) {
455 if (mp->m_flags & XFS_MOUNT_WSYNC)
456 xfs_trans_set_sync(tp);
457
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000458 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 xfs_iunlock(ip, lock_flags);
462
463 /*
464 * Release any dquot(s) the inode had kept before chown.
465 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200466 xfs_qm_dqrele(olddquot1);
467 xfs_qm_dqrele(olddquot2);
468 xfs_qm_dqrele(udqp);
469 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200471 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return code;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200473
474 /*
475 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
476 * update. We could avoid this with linked transactions
477 * and passing down the transaction pointer all the way
478 * to attr_set. No previous user of the generic
479 * Posix ACL code seems to care about this issue either.
480 */
481 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
482 code = -xfs_acl_chmod(inode);
483 if (code)
484 return XFS_ERROR(code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000487 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000488 !(flags & XFS_ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100489 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 NULL, DM_RIGHT_NULL, NULL, NULL,
491 0, 0, AT_DELAY_FLAG(flags));
492 }
493 return 0;
494
495 abort_return:
496 commit_flags |= XFS_TRANS_ABORT;
497 /* FALLTHROUGH */
498 error_return:
Christoph Hellwig7d095252009-06-08 15:33:32 +0200499 xfs_qm_dqrele(udqp);
500 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (tp) {
502 xfs_trans_cancel(tp, commit_flags);
503 }
504 if (lock_flags != 0) {
505 xfs_iunlock(ip, lock_flags);
506 }
507 return code;
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000511 * The maximum pathlen is 1024 bytes. Since the minimum file system
512 * blocksize is 512 bytes, we can get a max of 2 extents back from
513 * bmapi.
514 */
515#define SYMLINK_MAPS 2
516
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000517STATIC int
518xfs_readlink_bmap(
519 xfs_inode_t *ip,
520 char *link)
521{
522 xfs_mount_t *mp = ip->i_mount;
523 int pathlen = ip->i_d.di_size;
524 int nmaps = SYMLINK_MAPS;
525 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
526 xfs_daddr_t d;
527 int byte_cnt;
528 int n;
529 xfs_buf_t *bp;
530 int error = 0;
531
532 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
533 mval, &nmaps, NULL, NULL);
534 if (error)
535 goto out;
536
537 for (n = 0; n < nmaps; n++) {
538 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
539 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
540
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000541 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
542 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000543 error = XFS_BUF_GETERROR(bp);
544 if (error) {
545 xfs_ioerror_alert("xfs_readlink",
546 ip->i_mount, bp, XFS_BUF_ADDR(bp));
547 xfs_buf_relse(bp);
548 goto out;
549 }
550 if (pathlen < byte_cnt)
551 byte_cnt = pathlen;
552 pathlen -= byte_cnt;
553
554 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
555 xfs_buf_relse(bp);
556 }
557
558 link[ip->i_d.di_size] = '\0';
559 error = 0;
560
561 out:
562 return error;
563}
564
Christoph Hellwig993386c2007-08-28 16:12:30 +1000565int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000567 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000568 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000570 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100574 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 if (XFS_FORCED_SHUTDOWN(mp))
577 return XFS_ERROR(EIO);
578
579 xfs_ilock(ip, XFS_ILOCK_SHARED);
580
581 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000582 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000584 pathlen = ip->i_d.di_size;
585 if (!pathlen)
586 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000589 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
590 link[pathlen] = '\0';
591 } else {
592 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000595 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return error;
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/*
601 * xfs_fsync
602 *
David Chinner978b7232008-05-19 16:29:46 +1000603 * This is called to sync the inode and its data out to disk. We need to hold
604 * the I/O lock while flushing the data, and the inode lock while flushing the
605 * inode. The inode lock CANNOT be held while flushing the data, so acquire
606 * after we're done with that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000608int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609xfs_fsync(
David Chinner978b7232008-05-19 16:29:46 +1000610 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 xfs_trans_t *tp;
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300613 int error = 0;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100614 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100616 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
619 return XFS_ERROR(EIO);
620
621 /*
David Chinner978b7232008-05-19 16:29:46 +1000622 * We always need to make sure that the required inode state is safe on
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300623 * disk. The inode might be clean but we still might need to force the
David Chinner978b7232008-05-19 16:29:46 +1000624 * log because of committed transactions that haven't hit the disk yet.
625 * Likewise, there could be unflushed non-transactional changes to the
626 * inode core that have to go to disk and this requires us to issue
627 * a synchronous transaction to capture these changes correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 *
David Chinner978b7232008-05-19 16:29:46 +1000629 * This code relies on the assumption that if the update_* fields
630 * of the inode are clear and the inode is unpinned then it is clean
631 * and no action is required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
633 xfs_ilock(ip, XFS_ILOCK_SHARED);
634
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300635 if (!ip->i_update_core) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /*
David Chinner978b7232008-05-19 16:29:46 +1000637 * Timestamps/size haven't changed since last inode flush or
638 * inode transaction commit. That means either nothing got
639 * written or a transaction committed which caught the updates.
640 * If the latter happened and the transaction hasn't hit the
641 * disk yet, the inode will be still be pinned. If it is,
642 * force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
644
645 xfs_iunlock(ip, XFS_ILOCK_SHARED);
646
647 if (xfs_ipincount(ip)) {
David Chinner978b7232008-05-19 16:29:46 +1000648 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
649 XFS_LOG_FORCE | XFS_LOG_SYNC,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100650 &log_flushed);
651 } else {
652 /*
David Chinner978b7232008-05-19 16:29:46 +1000653 * If the inode is not pinned and nothing has changed
654 * we don't need to flush the cache.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100655 */
656 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 } else {
659 /*
David Chinner978b7232008-05-19 16:29:46 +1000660 * Kick off a transaction to log the inode core to get the
661 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
663 xfs_iunlock(ip, XFS_ILOCK_SHARED);
664 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000665 error = xfs_trans_reserve(tp, 0,
666 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
667 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 xfs_trans_cancel(tp, 0);
669 return error;
670 }
671 xfs_ilock(ip, XFS_ILOCK_EXCL);
672
673 /*
David Chinner978b7232008-05-19 16:29:46 +1000674 * Note - it's possible that we might have pushed ourselves out
675 * of the way during trans_reserve which would flush the inode.
676 * But there's no guarantee that the inode buffer has actually
677 * gone out yet (it's delwri). Plus the buffer could be pinned
678 * anyway if it's part of an inode in another recent
679 * transaction. So we play it safe and fire off the
680 * transaction anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
682 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
683 xfs_trans_ihold(tp, ip);
684 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
David Chinner978b7232008-05-19 16:29:46 +1000685 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000686 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 xfs_iunlock(ip, XFS_ILOCK_EXCL);
689 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100690
691 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
692 /*
693 * If the log write didn't issue an ordered tag we need
694 * to flush the disk cache for the data device now.
695 */
696 if (!log_flushed)
697 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
698
699 /*
700 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000701 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100702 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100703 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100704 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
705 }
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return error;
708}
709
710/*
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000711 * Flags for xfs_free_eofblocks
712 */
713#define XFS_FREE_EOF_TRYLOCK (1<<0)
714
715/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000716 * This is called by xfs_inactive to free any blocks beyond eof
717 * when the link count isn't zero and by xfs_dm_punch_hole() when
718 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
Eric Sandeend96f8f82009-07-02 00:09:33 -0500720STATIC int
David Chinner92dfe8d2007-05-24 15:22:19 +1000721xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000723 xfs_inode_t *ip,
724 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 xfs_trans_t *tp;
727 int error;
728 xfs_fileoff_t end_fsb;
729 xfs_fileoff_t last_fsb;
730 xfs_filblks_t map_len;
731 int nimaps;
732 xfs_bmbt_irec_t imap;
733
734 /*
735 * Figure out if there are any blocks beyond the end
736 * of the file. If not, then there is nothing to do.
737 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000738 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
740 map_len = last_fsb - end_fsb;
741 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100742 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 nimaps = 1;
745 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000746 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000747 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 xfs_iunlock(ip, XFS_ILOCK_SHARED);
749
750 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100751 (imap.br_startblock != HOLESTARTBLOCK ||
752 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /*
754 * Attach the dquots to the inode up front.
755 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200756 error = xfs_qm_dqattach(ip, 0);
757 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100758 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /*
761 * There are blocks after the end of file.
762 * Free them up now by truncating the file to
763 * its current size.
764 */
765 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
766
767 /*
768 * Do the xfs_itruncate_start() call before
769 * reserving any log space because
770 * itruncate_start will call into the buffer
771 * cache and we can't
772 * do that within a transaction.
773 */
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000774 if (flags & XFS_FREE_EOF_TRYLOCK) {
775 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
776 xfs_trans_cancel(tp, 0);
777 return 0;
778 }
779 } else {
David Chinner92dfe8d2007-05-24 15:22:19 +1000780 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000781 }
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000782 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000783 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000784 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000785 xfs_trans_cancel(tp, 0);
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000786 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000787 return error;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 error = xfs_trans_reserve(tp, 0,
791 XFS_ITRUNCATE_LOG_RES(mp),
792 0, XFS_TRANS_PERM_LOG_RES,
793 XFS_ITRUNCATE_LOG_COUNT);
794 if (error) {
795 ASSERT(XFS_FORCED_SHUTDOWN(mp));
796 xfs_trans_cancel(tp, 0);
797 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100798 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
801 xfs_ilock(ip, XFS_ILOCK_EXCL);
802 xfs_trans_ijoin(tp, ip,
803 XFS_IOLOCK_EXCL |
804 XFS_ILOCK_EXCL);
805 xfs_trans_ihold(tp, ip);
806
807 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000808 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 XFS_DATA_FORK,
810 0);
811 /*
812 * If we get an error at this point we
813 * simply don't bother truncating the file.
814 */
815 if (error) {
816 xfs_trans_cancel(tp,
817 (XFS_TRANS_RELEASE_LOG_RES |
818 XFS_TRANS_ABORT));
819 } else {
820 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000821 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Christoph Hellwigc56c96312009-10-19 04:03:46 +0000823 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100825 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828/*
829 * Free a symlink that has blocks associated with it.
830 */
831STATIC int
832xfs_inactive_symlink_rmt(
833 xfs_inode_t *ip,
834 xfs_trans_t **tpp)
835{
836 xfs_buf_t *bp;
837 int committed;
838 int done;
839 int error;
840 xfs_fsblock_t first_block;
841 xfs_bmap_free_t free_list;
842 int i;
843 xfs_mount_t *mp;
844 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
845 int nmaps;
846 xfs_trans_t *ntp;
847 int size;
848 xfs_trans_t *tp;
849
850 tp = *tpp;
851 mp = ip->i_mount;
852 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
853 /*
854 * We're freeing a symlink that has some
855 * blocks allocated to it. Free the
856 * blocks here. We know that we've got
857 * either 1 or 2 extents and that we can
858 * free them all in one bunmapi call.
859 */
860 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
861 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
862 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
863 ASSERT(XFS_FORCED_SHUTDOWN(mp));
864 xfs_trans_cancel(tp, 0);
865 *tpp = NULL;
866 return error;
867 }
868 /*
869 * Lock the inode, fix the size, and join it to the transaction.
870 * Hold it so in the normal path, we still have it locked for
871 * the second transaction. In the error paths we need it
872 * held so the cancel won't rele it, see below.
873 */
874 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
875 size = (int)ip->i_d.di_size;
876 ip->i_d.di_size = 0;
877 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
878 xfs_trans_ihold(tp, ip);
879 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
880 /*
881 * Find the block(s) so we can inval and unmap them.
882 */
883 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600884 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800885 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
887 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000888 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 goto error0;
890 /*
891 * Invalidate the block(s).
892 */
893 for (i = 0; i < nmaps; i++) {
894 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
895 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
896 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
897 xfs_trans_binval(tp, bp);
898 }
899 /*
900 * Unmap the dead block(s) to the free_list.
901 */
902 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000903 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto error1;
905 ASSERT(done);
906 /*
907 * Commit the first transaction. This logs the EFI and the inode.
908 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100909 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto error1;
911 /*
912 * The transaction must have been committed, since there were
913 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
914 * The new tp has the extent freeing and EFDs.
915 */
916 ASSERT(committed);
917 /*
918 * The first xact was committed, so add the inode to the new one.
919 * Mark it dirty so it will be logged and moved forward in the log as
920 * part of every commit.
921 */
922 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
923 xfs_trans_ihold(tp, ip);
924 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
925 /*
926 * Get a new, empty transaction to return to our caller.
927 */
928 ntp = xfs_trans_dup(tp);
929 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000930 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * If we get an error on the commit here or on the reserve below,
932 * we need to unlock the inode since the new transaction doesn't
933 * have the inode attached.
934 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000935 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 tp = ntp;
937 if (error) {
938 ASSERT(XFS_FORCED_SHUTDOWN(mp));
939 goto error0;
940 }
941 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100942 * transaction commit worked ok so we can drop the extra ticket
943 * reference that we gained in xfs_trans_dup()
944 */
945 xfs_log_ticket_put(tp->t_ticket);
946
947 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * Remove the memory for extent descriptions (just bookkeeping).
949 */
950 if (ip->i_df.if_bytes)
951 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
952 ASSERT(ip->i_df.if_bytes == 0);
953 /*
954 * Put an itruncate log reservation in the new transaction
955 * for our caller.
956 */
957 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
958 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
959 ASSERT(XFS_FORCED_SHUTDOWN(mp));
960 goto error0;
961 }
962 /*
963 * Return with the inode locked but not joined to the transaction.
964 */
965 *tpp = tp;
966 return 0;
967
968 error1:
969 xfs_bmap_cancel(&free_list);
970 error0:
971 /*
972 * Have to come here with the inode locked and either
973 * (held and in the transaction) or (not in the transaction).
974 * If the inode isn't held then cancel would iput it, but
975 * that's wrong since this is inactive and the vnode ref
976 * count is 0 already.
977 * Cancel won't do anything to the inode if held, but it still
978 * needs to be locked until the cancel is done, if it was
979 * joined to the transaction.
980 */
981 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
982 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
983 *tpp = NULL;
984 return error;
985
986}
987
988STATIC int
989xfs_inactive_symlink_local(
990 xfs_inode_t *ip,
991 xfs_trans_t **tpp)
992{
993 int error;
994
995 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
996 /*
997 * We're freeing a symlink which fit into
998 * the inode. Just free the memory used
999 * to hold the old symlink.
1000 */
1001 error = xfs_trans_reserve(*tpp, 0,
1002 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1003 0, XFS_TRANS_PERM_LOG_RES,
1004 XFS_ITRUNCATE_LOG_COUNT);
1005
1006 if (error) {
1007 xfs_trans_cancel(*tpp, 0);
1008 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001009 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1012
1013 /*
1014 * Zero length symlinks _can_ exist.
1015 */
1016 if (ip->i_df.if_bytes > 0) {
1017 xfs_idata_realloc(ip,
1018 -(ip->i_df.if_bytes),
1019 XFS_DATA_FORK);
1020 ASSERT(ip->i_df.if_bytes == 0);
1021 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001022 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025STATIC int
1026xfs_inactive_attrs(
1027 xfs_inode_t *ip,
1028 xfs_trans_t **tpp)
1029{
1030 xfs_trans_t *tp;
1031 int error;
1032 xfs_mount_t *mp;
1033
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001034 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 tp = *tpp;
1036 mp = ip->i_mount;
1037 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001038 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001040 if (error)
1041 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001044 if (error)
1045 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1048 error = xfs_trans_reserve(tp, 0,
1049 XFS_IFREE_LOG_RES(mp),
1050 0, XFS_TRANS_PERM_LOG_RES,
1051 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001052 if (error)
1053 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 xfs_ilock(ip, XFS_ILOCK_EXCL);
1056 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1057 xfs_trans_ihold(tp, ip);
1058 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1059
1060 ASSERT(ip->i_d.di_anextents == 0);
1061
1062 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001063 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001064
1065error_cancel:
1066 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1067 xfs_trans_cancel(tp, 0);
1068error_unlock:
1069 *tpp = NULL;
1070 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1071 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Christoph Hellwig993386c2007-08-28 16:12:30 +10001074int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001076 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001078 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 int error;
1080
Christoph Hellwig42173f62008-04-22 17:33:25 +10001081 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001085 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return 0;
1087
David Chinner2a82b8b2007-07-11 11:09:12 +10001088 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001089 int truncated;
1090
David Chinner2a82b8b2007-07-11 11:09:12 +10001091 /*
1092 * If we are using filestreams, and we have an unlinked
1093 * file that we are processing the last close on, then nothing
1094 * will be able to reopen and write to this file. Purge this
1095 * inode from the filestreams cache so that it doesn't delay
1096 * teardown of the inode.
1097 */
1098 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1099 xfs_filestream_deassociate(ip);
1100
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001101 /*
1102 * If we previously truncated this file and removed old data
1103 * in the process, we want to initiate "early" writeout on
1104 * the last close. This is an attempt to combat the notorious
1105 * NULL files problem which is particularly noticable from a
1106 * truncate down, buffered (re-)write (delalloc), followed by
1107 * a crash. What we are effectively doing here is
1108 * significantly reducing the time window where we'd otherwise
1109 * be exposed to that problem.
1110 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001111 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001112 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001113 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001114 }
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (ip->i_d.di_nlink != 0) {
1117 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001118 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001119 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001121 (!(ip->i_d.di_flags &
1122 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
Christoph Hellwigc56c96312009-10-19 04:03:46 +00001123
1124 /*
1125 * If we can't get the iolock just skip truncating
1126 * the blocks past EOF because we could deadlock
1127 * with the mmap_sem otherwise. We'll get another
1128 * chance to drop them once the last reference to
1129 * the inode is dropped, so we'll never leak blocks
1130 * permanently.
1131 */
1132 error = xfs_free_eofblocks(mp, ip,
1133 XFS_FREE_EOF_TRYLOCK);
David Chinner92dfe8d2007-05-24 15:22:19 +10001134 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001135 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137 }
1138
1139 return 0;
1140}
1141
1142/*
1143 * xfs_inactive
1144 *
1145 * This is called when the vnode reference count for the vnode
1146 * goes to zero. If the file has been unlinked, then it must
1147 * now be truncated. Also, we clear all of the read-ahead state
1148 * kept for the inode here since the file is now closed.
1149 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001150int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001152 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001154 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 xfs_fsblock_t first_block;
1156 int committed;
1157 xfs_trans_t *tp;
1158 xfs_mount_t *mp;
1159 int error;
1160 int truncate;
1161
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001162 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /*
1165 * If the inode is already free, then there can be nothing
1166 * to clean up here.
1167 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +01001168 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 ASSERT(ip->i_df.if_real_bytes == 0);
1170 ASSERT(ip->i_df.if_broot_bytes == 0);
1171 return VN_INACTIVE_CACHE;
1172 }
1173
1174 /*
1175 * Only do a truncate if it's a regular file with
1176 * some actual space in it. It's OK to look at the
1177 * inode's fields without the lock because we're the
1178 * only one with a reference to the inode.
1179 */
1180 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001181 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1182 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1184
1185 mp = ip->i_mount;
1186
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001187 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1188 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 error = 0;
1191
1192 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001193 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto out;
1195
1196 if (ip->i_d.di_nlink != 0) {
1197 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001198 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001199 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001200 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1201 (!(ip->i_d.di_flags &
1202 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1203 (ip->i_delayed_blks != 0)))) {
Christoph Hellwigc56c96312009-10-19 04:03:46 +00001204 error = xfs_free_eofblocks(mp, ip, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001205 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001206 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208 goto out;
1209 }
1210
1211 ASSERT(ip->i_d.di_nlink == 0);
1212
Christoph Hellwig7d095252009-06-08 15:33:32 +02001213 error = xfs_qm_dqattach(ip, 0);
1214 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001215 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1218 if (truncate) {
1219 /*
1220 * Do the xfs_itruncate_start() call before
1221 * reserving any log space because itruncate_start
1222 * will call into the buffer cache and we can't
1223 * do that within a transaction.
1224 */
1225 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1226
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001227 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1228 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001229 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001230 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1231 return VN_INACTIVE_CACHE;
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 error = xfs_trans_reserve(tp, 0,
1235 XFS_ITRUNCATE_LOG_RES(mp),
1236 0, XFS_TRANS_PERM_LOG_RES,
1237 XFS_ITRUNCATE_LOG_COUNT);
1238 if (error) {
1239 /* Don't call itruncate_cleanup */
1240 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1241 xfs_trans_cancel(tp, 0);
1242 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001243 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245
1246 xfs_ilock(ip, XFS_ILOCK_EXCL);
1247 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1248 xfs_trans_ihold(tp, ip);
1249
1250 /*
1251 * normally, we have to run xfs_itruncate_finish sync.
1252 * But if filesystem is wsync and we're in the inactive
1253 * path, then we know that nlink == 0, and that the
1254 * xaction that made nlink == 0 is permanently committed
1255 * since xfs_remove runs as a synchronous transaction.
1256 */
1257 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1258 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1259
1260 if (error) {
1261 xfs_trans_cancel(tp,
1262 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1263 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001264 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1267
1268 /*
1269 * If we get an error while cleaning up a
1270 * symlink we bail out.
1271 */
1272 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1273 xfs_inactive_symlink_rmt(ip, &tp) :
1274 xfs_inactive_symlink_local(ip, &tp);
1275
1276 if (error) {
1277 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001278 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
1281 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1282 xfs_trans_ihold(tp, ip);
1283 } else {
1284 error = xfs_trans_reserve(tp, 0,
1285 XFS_IFREE_LOG_RES(mp),
1286 0, XFS_TRANS_PERM_LOG_RES,
1287 XFS_INACTIVE_LOG_COUNT);
1288 if (error) {
1289 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1290 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001291 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293
1294 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1295 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1296 xfs_trans_ihold(tp, ip);
1297 }
1298
1299 /*
1300 * If there are attributes associated with the file
1301 * then blow them away now. The code calls a routine
1302 * that recursively deconstructs the attribute fork.
1303 * We need to just commit the current transaction
1304 * because we can't use it for xfs_attr_inactive().
1305 */
1306 if (ip->i_d.di_anextents > 0) {
1307 error = xfs_inactive_attrs(ip, &tp);
1308 /*
1309 * If we got an error, the transaction is already
1310 * cancelled, and the inode is unlocked. Just get out.
1311 */
1312 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001313 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 } else if (ip->i_afp) {
1315 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1316 }
1317
1318 /*
1319 * Free the inode.
1320 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001321 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 error = xfs_ifree(tp, ip, &free_list);
1323 if (error) {
1324 /*
1325 * If we fail to free the inode, shut down. The cancel
1326 * might do that, we need to make sure. Otherwise the
1327 * inode might be lost for a long time or forever.
1328 */
1329 if (!XFS_FORCED_SHUTDOWN(mp)) {
1330 cmn_err(CE_NOTE,
1331 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1332 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001333 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1336 } else {
1337 /*
1338 * Credit the quota account(s). The inode is gone.
1339 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001340 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /*
David Chinner78e9da72008-04-10 12:24:17 +10001343 * Just ignore errors at this point. There is nothing we can
1344 * do except to try to keep going. Make sure it's not a silent
1345 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
David Chinner78e9da72008-04-10 12:24:17 +10001347 error = xfs_bmap_finish(&tp, &free_list, &committed);
1348 if (error)
1349 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1350 "xfs_bmap_finish() returned error %d", error);
1351 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1352 if (error)
1353 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1354 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
Christoph Hellwig7d095252009-06-08 15:33:32 +02001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /*
1358 * Release the dquots held by inode, if any.
1359 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001360 xfs_qm_dqdetach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1362
1363 out:
1364 return VN_INACTIVE_CACHE;
1365}
1366
Barry Naujok384f3ce2008-05-21 16:58:22 +10001367/*
1368 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1369 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1370 * ci_name->name will point to a the actual name (caller must free) or
1371 * will be set to NULL if an exact match is found.
1372 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001373int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001375 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001376 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001377 xfs_inode_t **ipp,
1378 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001380 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 int error;
1382 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001384 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1387 return XFS_ERROR(EIO);
1388
1389 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001390 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001392
1393 if (error)
1394 goto out;
1395
1396 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1397 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001398 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001399
1400 xfs_itrace_ref(*ipp);
1401 return 0;
1402
Barry Naujok384f3ce2008-05-21 16:58:22 +10001403out_free_name:
1404 if (ci_name)
1405 kmem_free(ci_name->name);
1406out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001407 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return error;
1409}
1410
Christoph Hellwig993386c2007-08-28 16:12:30 +10001411int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001413 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001414 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001415 mode_t mode,
1416 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001417 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 cred_t *credp)
1419{
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001420 int is_dir = S_ISDIR(mode);
1421 struct xfs_mount *mp = dp->i_mount;
1422 struct xfs_inode *ip = NULL;
1423 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +10001424 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 xfs_bmap_free_t free_list;
1426 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001427 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 uint cancel_flags;
1429 int committed;
1430 xfs_prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001431 struct xfs_dquot *udqp = NULL;
1432 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001434 uint log_res;
1435 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001437 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001439 if (XFS_FORCED_SHUTDOWN(mp))
1440 return XFS_ERROR(EIO);
1441
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001442 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001444 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001445 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001446 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 if (error)
1449 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
Nathan Scott365ca832005-06-21 15:39:12 +10001452 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1453 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 else
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001455 prid = dfltprid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /*
1458 * Make sure that we have allocated dquot(s) on disk.
1459 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001460 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001461 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (error)
1463 goto std_return;
1464
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001465 if (is_dir) {
1466 rdev = 0;
1467 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1468 log_res = XFS_MKDIR_LOG_RES(mp);
1469 log_count = XFS_MKDIR_LOG_COUNT;
1470 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1471 } else {
1472 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1473 log_res = XFS_CREATE_LOG_RES(mp);
1474 log_count = XFS_CREATE_LOG_COUNT;
1475 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /*
1481 * Initially assume that the file does not exist and
1482 * reserve the resources for that case. If that is not
1483 * the case we'll drop the one we have and get a more
1484 * appropriate transaction later.
1485 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001486 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1487 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +02001489 /* flush outstanding delalloc blocks and retry */
1490 xfs_flush_inodes(dp);
Christoph Hellwig4734d402009-09-09 18:19:02 -05001491 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1492 XFS_TRANS_PERM_LOG_RES, log_count);
Dave Chinner153fec42009-04-06 18:48:30 +02001493 }
1494 if (error == ENOSPC) {
1495 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001497 error = xfs_trans_reserve(tp, 0, log_res, 0,
1498 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500 if (error) {
1501 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001502 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001505 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001506 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001508 /*
1509 * Check for directory link count overflow.
1510 */
1511 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1512 error = XFS_ERROR(EMLINK);
1513 goto out_trans_cancel;
1514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001516 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 /*
1519 * Reserve disk quota and the inode.
1520 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001521 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001523 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Barry Naujok556b8b12008-04-10 12:22:07 +10001525 error = xfs_dir_canenter(tp, dp, name, resblks);
1526 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001527 goto out_trans_cancel;
1528
1529 /*
1530 * A newly created regular or special file just has one directory
1531 * entry pointing to them, but a directory also the "." entry
1532 * pointing to itself.
1533 */
1534 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1535 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (error) {
1537 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001538 goto out_trans_cancel;
1539 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /*
1543 * At this point, we've gotten a newly allocated inode.
1544 * It is locked (and joined to the transaction).
1545 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001546 xfs_itrace_ref(ip);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001547 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001550 * Now we join the directory inode to the transaction. We do not do it
1551 * earlier because xfs_dir_ialloc might commit the previous transaction
1552 * (and release all the locks). An error from here on will result in
1553 * the transaction cancel unlocking dp so don't do it explicitly in the
1554 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001556 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001558 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Barry Naujok556b8b12008-04-10 12:22:07 +10001560 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001561 &first_block, &free_list, resblks ?
1562 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (error) {
1564 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001565 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1568 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1569
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001570 if (is_dir) {
1571 error = xfs_dir_init(tp, ip, dp);
1572 if (error)
1573 goto out_bmap_cancel;
1574
1575 error = xfs_bumplink(tp, dp);
1576 if (error)
1577 goto out_bmap_cancel;
1578 }
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 /*
1581 * If this is a synchronous mount, make sure that the
1582 * create transaction goes to disk before returning to
1583 * the user.
1584 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001585 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /*
1589 * Attach the dquot(s) to the inodes and modify them incore.
1590 * These ids of the inode couldn't have changed since the new
1591 * inode has been locked ever since it was created.
1592 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001593 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 /*
1596 * xfs_trans_commit normally decrements the vnode ref count
1597 * when it unlocks the inode. Since we want to return the
1598 * vnode to the caller, we bump the vnode ref count now.
1599 */
1600 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001602 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001603 if (error)
1604 goto out_abort_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001606 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (error) {
1608 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001609 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611
Christoph Hellwig7d095252009-06-08 15:33:32 +02001612 xfs_qm_dqrele(udqp);
1613 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001615 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 /* Fallthrough to std_return with error = 0 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001618 std_return:
1619 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1620 XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
1621 ip, DM_RIGHT_NULL, name->name, NULL, mode,
1622 error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return error;
1626
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001627 out_bmap_cancel:
1628 xfs_bmap_cancel(&free_list);
1629 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001631 out_trans_cancel:
1632 xfs_trans_cancel(tp, cancel_flags);
1633 out_dqrele:
Christoph Hellwig7d095252009-06-08 15:33:32 +02001634 xfs_qm_dqrele(udqp);
1635 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Christoph Hellwig993386c2007-08-28 16:12:30 +10001637 if (unlock_dp_on_error)
1638 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 goto std_return;
1641
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001642 out_abort_rele:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /*
1644 * Wait until after the current transaction is aborted to
1645 * release the inode. This prevents recursive transactions
1646 * and deadlocks from xfs_inactive.
1647 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001648 xfs_bmap_cancel(&free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 cancel_flags |= XFS_TRANS_ABORT;
1650 xfs_trans_cancel(tp, cancel_flags);
1651 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001652 unlock_dp_on_error = B_FALSE;
1653 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
1656#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657int xfs_locked_n;
1658int xfs_small_retries;
1659int xfs_middle_retries;
1660int xfs_lots_retries;
1661int xfs_lock_delays;
1662#endif
1663
1664/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001665 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1666 * a different value
1667 */
1668static inline int
1669xfs_lock_inumorder(int lock_mode, int subclass)
1670{
1671 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001672 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001673 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001674 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001675
1676 return lock_mode;
1677}
1678
1679/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * The following routine will lock n inodes in exclusive mode.
1681 * We assume the caller calls us with the inodes in i_ino order.
1682 *
1683 * We need to detect deadlock where an inode that we lock
1684 * is in the AIL and we start waiting for another inode that is locked
1685 * by a thread in a long running transaction (such as truncate). This can
1686 * result in deadlock since the long running trans might need to wait
1687 * for the inode we just locked in order to push the tail and free space
1688 * in the log.
1689 */
1690void
1691xfs_lock_inodes(
1692 xfs_inode_t **ips,
1693 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 uint lock_mode)
1695{
1696 int attempts = 0, i, j, try_lock;
1697 xfs_log_item_t *lp;
1698
1699 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1700
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001701 try_lock = 0;
1702 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704again:
1705 for (; i < inodes; i++) {
1706 ASSERT(ips[i]);
1707
1708 if (i && (ips[i] == ips[i-1])) /* Already locked */
1709 continue;
1710
1711 /*
1712 * If try_lock is not set yet, make sure all locked inodes
1713 * are not in the AIL.
1714 * If any are, set try_lock to be used later.
1715 */
1716
1717 if (!try_lock) {
1718 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1719 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1720 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1721 try_lock++;
1722 }
1723 }
1724 }
1725
1726 /*
1727 * If any of the previous locks we have locked is in the AIL,
1728 * we must TRY to get the second and subsequent locks. If
1729 * we can't get any, we must release all we have
1730 * and try again.
1731 */
1732
1733 if (try_lock) {
1734 /* try_lock must be 0 if i is 0. */
1735 /*
1736 * try_lock means we have an inode locked
1737 * that is in the AIL.
1738 */
1739 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001740 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 attempts++;
1742
1743 /*
1744 * Unlock all previous guys and try again.
1745 * xfs_iunlock will try to push the tail
1746 * if the inode is in the AIL.
1747 */
1748
1749 for(j = i - 1; j >= 0; j--) {
1750
1751 /*
1752 * Check to see if we've already
1753 * unlocked this one.
1754 * Not the first one going back,
1755 * and the inode ptr is the same.
1756 */
1757 if ((j != (i - 1)) && ips[j] ==
1758 ips[j+1])
1759 continue;
1760
1761 xfs_iunlock(ips[j], lock_mode);
1762 }
1763
1764 if ((attempts % 5) == 0) {
1765 delay(1); /* Don't just spin the CPU */
1766#ifdef DEBUG
1767 xfs_lock_delays++;
1768#endif
1769 }
1770 i = 0;
1771 try_lock = 0;
1772 goto again;
1773 }
1774 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001775 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777 }
1778
1779#ifdef DEBUG
1780 if (attempts) {
1781 if (attempts < 5) xfs_small_retries++;
1782 else if (attempts < 100) xfs_middle_retries++;
1783 else xfs_lots_retries++;
1784 } else {
1785 xfs_locked_n++;
1786 }
1787#endif
1788}
1789
David Chinnerf9114eb2008-09-17 16:51:21 +10001790/*
1791 * xfs_lock_two_inodes() can only be used to lock one type of lock
1792 * at a time - the iolock or the ilock, but not both at once. If
1793 * we lock both at once, lockdep will report false positives saying
1794 * we have violated locking orders.
1795 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001796void
1797xfs_lock_two_inodes(
1798 xfs_inode_t *ip0,
1799 xfs_inode_t *ip1,
1800 uint lock_mode)
1801{
1802 xfs_inode_t *temp;
1803 int attempts = 0;
1804 xfs_log_item_t *lp;
1805
David Chinnerf9114eb2008-09-17 16:51:21 +10001806 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1807 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001808 ASSERT(ip0->i_ino != ip1->i_ino);
1809
1810 if (ip0->i_ino > ip1->i_ino) {
1811 temp = ip0;
1812 ip0 = ip1;
1813 ip1 = temp;
1814 }
1815
1816 again:
1817 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1818
1819 /*
1820 * If the first lock we have locked is in the AIL, we must TRY to get
1821 * the second lock. If we can't get it, we must release the first one
1822 * and try again.
1823 */
1824 lp = (xfs_log_item_t *)ip0->i_itemp;
1825 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1826 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1827 xfs_iunlock(ip0, lock_mode);
1828 if ((++attempts % 5) == 0)
1829 delay(1); /* Don't just spin the CPU */
1830 goto again;
1831 }
1832 } else {
1833 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1834 }
1835}
1836
Christoph Hellwig993386c2007-08-28 16:12:30 +10001837int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001839 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001840 struct xfs_name *name,
1841 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001843 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001845 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 int error = 0;
1847 xfs_bmap_free_t free_list;
1848 xfs_fsblock_t first_block;
1849 int cancel_flags;
1850 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 int link_zero;
1852 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001853 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001855 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001856 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (XFS_FORCED_SHUTDOWN(mp))
1859 return XFS_ERROR(EIO);
1860
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001861 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001862 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1863 NULL, DM_RIGHT_NULL, name->name, NULL,
1864 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (error)
1866 return error;
1867 }
1868
Christoph Hellwig7d095252009-06-08 15:33:32 +02001869 error = xfs_qm_dqattach(dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001870 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Christoph Hellwig7d095252009-06-08 15:33:32 +02001873 error = xfs_qm_dqattach(ip, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001874 if (error)
1875 goto std_return;
1876
1877 if (is_dir) {
1878 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1879 log_count = XFS_DEFAULT_LOG_COUNT;
1880 } else {
1881 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1882 log_count = XFS_REMOVE_LOG_COUNT;
1883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /*
1887 * We try to get the real space reservation first,
1888 * allowing for directory btree deletion(s) implying
1889 * possible bmap insert(s). If we can't get the space
1890 * reservation then we use 0 instead, and avoid the bmap
1891 * btree insert(s) in the directory code by, if the bmap
1892 * insert tries to happen, instead trimming the LAST
1893 * block from the directory.
1894 */
1895 resblks = XFS_REMOVE_SPACE_RES(mp);
1896 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001897 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (error == ENOSPC) {
1899 resblks = 0;
1900 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001901 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 if (error) {
1904 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001905 cancel_flags = 0;
1906 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001909 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /*
1912 * At this point, we've gotten both the directory and the entry
1913 * inodes locked.
1914 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001915 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10001917
1918 IHOLD(dp);
1919 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001922 * If we're removing a directory perform some additional validation.
1923 */
1924 if (is_dir) {
1925 ASSERT(ip->i_d.di_nlink >= 2);
1926 if (ip->i_d.di_nlink != 2) {
1927 error = XFS_ERROR(ENOTEMPTY);
1928 goto out_trans_cancel;
1929 }
1930 if (!xfs_dir_isempty(ip)) {
1931 error = XFS_ERROR(ENOTEMPTY);
1932 goto out_trans_cancel;
1933 }
1934 }
1935
Eric Sandeen9d87c312009-01-14 23:22:07 -06001936 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001937 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001938 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (error) {
1940 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001941 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1944
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001945 if (is_dir) {
1946 /*
1947 * Drop the link from ip's "..".
1948 */
1949 error = xfs_droplink(tp, dp);
1950 if (error)
1951 goto out_bmap_cancel;
1952
1953 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001954 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001955 */
1956 error = xfs_droplink(tp, ip);
1957 if (error)
1958 goto out_bmap_cancel;
1959 } else {
1960 /*
1961 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +11001962 * inode here. For a directory this is done implicitly
1963 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001964 */
1965 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001968 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001969 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001970 */
1971 error = xfs_droplink(tp, ip);
1972 if (error)
1973 goto out_bmap_cancel;
1974
1975 /*
1976 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 * we are in the transaction.
1978 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001979 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 * If this is a synchronous mount, make sure that the
1983 * remove transaction goes to disk before returning to
1984 * the user.
1985 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001986 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001989 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001990 if (error)
1991 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001993 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001994 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001998 * If we are using filestreams, kill the stream association.
1999 * If the file is still open it may get a new one but that
2000 * will get killed on last close in xfs_close() so we don't
2001 * have to worry about that.
2002 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002003 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10002004 xfs_filestream_deassociate(ip);
2005
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002006 xfs_itrace_exit(ip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002007 xfs_itrace_exit(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002010 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002011 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2012 NULL, DM_RIGHT_NULL, name->name, NULL,
2013 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return error;
2017
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002018 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 xfs_bmap_cancel(&free_list);
2020 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002021 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 xfs_trans_cancel(tp, cancel_flags);
2023 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Christoph Hellwig993386c2007-08-28 16:12:30 +10002026int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002028 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002029 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002030 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002032 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 int error;
2035 xfs_bmap_free_t free_list;
2036 xfs_fsblock_t first_block;
2037 int cancel_flags;
2038 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002041 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002042 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002044 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (XFS_FORCED_SHUTDOWN(mp))
2047 return XFS_ERROR(EIO);
2048
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002049 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002051 tdp, DM_RIGHT_NULL,
2052 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002053 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (error)
2055 return error;
2056 }
2057
2058 /* Return through std_return after this point. */
2059
Christoph Hellwig7d095252009-06-08 15:33:32 +02002060 error = xfs_qm_dqattach(sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01002061 if (error)
2062 goto std_return;
2063
Christoph Hellwig7d095252009-06-08 15:33:32 +02002064 error = xfs_qm_dqattach(tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (error)
2066 goto std_return;
2067
2068 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2069 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002070 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2072 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2073 if (error == ENOSPC) {
2074 resblks = 0;
2075 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2076 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2077 }
2078 if (error) {
2079 cancel_flags = 0;
2080 goto error_return;
2081 }
2082
Christoph Hellwige1cccd92008-08-13 16:18:07 +10002083 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 /*
2086 * Increment vnode ref counts since xfs_trans_commit &
2087 * xfs_trans_cancel will both unlock the inodes and
2088 * decrement the associated ref counts.
2089 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002090 IHOLD(sip);
2091 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2093 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2094
2095 /*
2096 * If the source has too many links, we can't make any more to it.
2097 */
2098 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2099 error = XFS_ERROR(EMLINK);
2100 goto error_return;
2101 }
2102
Nathan Scott365ca832005-06-21 15:39:12 +10002103 /*
2104 * If we are using project inheritance, we only allow hard link
2105 * creation in our tree when the project IDs are the same; else
2106 * the tree quota mechanism could be circumvented.
2107 */
2108 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2109 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002110 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002111 goto error_return;
2112 }
2113
Barry Naujok556b8b12008-04-10 12:22:07 +10002114 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2115 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 goto error_return;
2117
Eric Sandeen9d87c312009-01-14 23:22:07 -06002118 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Barry Naujok556b8b12008-04-10 12:22:07 +10002120 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2121 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (error)
2123 goto abort_return;
2124 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2126
2127 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002128 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 /*
2132 * If this is a synchronous mount, make sure that the
2133 * link transaction goes to disk before returning to
2134 * the user.
2135 */
2136 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2137 xfs_trans_set_sync(tp);
2138 }
2139
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002140 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (error) {
2142 xfs_bmap_cancel(&free_list);
2143 goto abort_return;
2144 }
2145
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002146 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002147 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /* Fall through to std_return with error = 0. */
2151std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002152 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002154 tdp, DM_RIGHT_NULL,
2155 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002156 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158 return error;
2159
2160 abort_return:
2161 cancel_flags |= XFS_TRANS_ABORT;
2162 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 error_return:
2165 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto std_return;
2167}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002168
Christoph Hellwig993386c2007-08-28 16:12:30 +10002169int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002171 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002172 struct xfs_name *link_name,
2173 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002174 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002175 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 cred_t *credp)
2177{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002178 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 xfs_inode_t *ip;
2181 int error;
2182 int pathlen;
2183 xfs_bmap_free_t free_list;
2184 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002185 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 uint cancel_flags;
2187 int committed;
2188 xfs_fileoff_t first_fsb;
2189 xfs_filblks_t fs_blocks;
2190 int nmaps;
2191 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2192 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002193 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 int byte_cnt;
2195 int n;
2196 xfs_buf_t *bp;
2197 xfs_prid_t prid;
2198 struct xfs_dquot *udqp, *gdqp;
2199 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Christoph Hellwig3937be52008-03-06 13:46:19 +11002201 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 error = 0;
2203 ip = NULL;
2204 tp = NULL;
2205
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002206 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 if (XFS_FORCED_SHUTDOWN(mp))
2209 return XFS_ERROR(EIO);
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /*
2212 * Check component lengths of the target path name.
2213 */
2214 pathlen = strlen(target_path);
2215 if (pathlen >= MAXPATHLEN) /* total string too long */
2216 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002218 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002219 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002221 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (error)
2223 return error;
2224 }
2225
2226 /* Return through std_return after this point. */
2227
2228 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002229 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2230 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 else
2232 prid = (xfs_prid_t)dfltprid;
2233
2234 /*
2235 * Make sure that we have allocated dquot(s) on disk.
2236 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002237 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2239 if (error)
2240 goto std_return;
2241
2242 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2243 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2244 /*
2245 * The symlink will fit into the inode data fork?
2246 * There can't be any attributes so we get the whole variable part.
2247 */
2248 if (pathlen <= XFS_LITINO(mp))
2249 fs_blocks = 0;
2250 else
2251 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002252 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2254 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2255 if (error == ENOSPC && fs_blocks == 0) {
2256 resblks = 0;
2257 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2258 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2259 }
2260 if (error) {
2261 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto error_return;
2263 }
2264
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002265 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002266 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 /*
2269 * Check whether the directory allows new symlinks or not.
2270 */
2271 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2272 error = XFS_ERROR(EPERM);
2273 goto error_return;
2274 }
2275
2276 /*
2277 * Reserve disk quota : blocks and inode.
2278 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002279 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (error)
2281 goto error_return;
2282
2283 /*
2284 * Check for ability to enter directory entry, if no space reserved.
2285 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002286 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2287 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 goto error_return;
2289 /*
2290 * Initialize the bmap freelist prior to calling either
2291 * bmapi or the directory create code.
2292 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002293 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 /*
2296 * Allocate an inode for the symlink.
2297 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002298 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 1, 0, credp, prid, resblks > 0, &ip, NULL);
2300 if (error) {
2301 if (error == ENOSPC)
2302 goto error_return;
2303 goto error1;
2304 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002305 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Christoph Hellwig993386c2007-08-28 16:12:30 +10002307 /*
2308 * An error after we've joined dp to the transaction will result in the
2309 * transaction cancel unlocking dp so don't do it explicitly in the
2310 * error path.
2311 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002312 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002314 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 /*
2317 * Also attach the dquot(s) to it, if applicable.
2318 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002319 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 if (resblks)
2322 resblks -= XFS_IALLOC_SPACE_RES(mp);
2323 /*
2324 * If the symlink will fit into the inode, write it inline.
2325 */
2326 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2327 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2328 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2329 ip->i_d.di_size = pathlen;
2330
2331 /*
2332 * The inode was initially created in extent format.
2333 */
2334 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2335 ip->i_df.if_flags |= XFS_IFINLINE;
2336
2337 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2338 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2339
2340 } else {
2341 first_fsb = 0;
2342 nmaps = SYMLINK_MAPS;
2343
2344 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2345 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2346 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002347 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (error) {
2349 goto error1;
2350 }
2351
2352 if (resblks)
2353 resblks -= fs_blocks;
2354 ip->i_d.di_size = pathlen;
2355 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2356
2357 cur_chunk = target_path;
2358 for (n = 0; n < nmaps; n++) {
2359 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2360 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2361 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2362 BTOBB(byte_cnt), 0);
2363 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2364 if (pathlen < byte_cnt) {
2365 byte_cnt = pathlen;
2366 }
2367 pathlen -= byte_cnt;
2368
2369 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2370 cur_chunk += byte_cnt;
2371
2372 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2373 }
2374 }
2375
2376 /*
2377 * Create the directory entry for the symlink.
2378 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002379 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2380 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002381 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2384 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2385
2386 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 * If this is a synchronous mount, make sure that the
2388 * symlink transaction goes to disk before returning to
2389 * the user.
2390 */
2391 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2392 xfs_trans_set_sync(tp);
2393 }
2394
2395 /*
2396 * xfs_trans_commit normally decrements the vnode ref count
2397 * when it unlocks the inode. Since we want to return the
2398 * vnode to the caller, we bump the vnode ref count now.
2399 */
2400 IHOLD(ip);
2401
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002402 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (error) {
2404 goto error2;
2405 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002406 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002407 xfs_qm_dqrele(udqp);
2408 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 /* Fall through to std_return with error = 0 or errno from
2411 * xfs_trans_commit */
2412std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10002413 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002415 dp, DM_RIGHT_NULL,
2416 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002417 DM_RIGHT_NULL, link_name->name,
2418 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420
Christoph Hellwig3937be52008-03-06 13:46:19 +11002421 if (!error)
2422 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return error;
2424
2425 error2:
2426 IRELE(ip);
2427 error1:
2428 xfs_bmap_cancel(&free_list);
2429 cancel_flags |= XFS_TRANS_ABORT;
2430 error_return:
2431 xfs_trans_cancel(tp, cancel_flags);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002432 xfs_qm_dqrele(udqp);
2433 xfs_qm_dqrele(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Christoph Hellwig993386c2007-08-28 16:12:30 +10002435 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 goto std_return;
2439}
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002442xfs_set_dmattrs(
2443 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002445 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002447 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 int error;
2450
2451 if (!capable(CAP_SYS_ADMIN))
2452 return XFS_ERROR(EPERM);
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (XFS_FORCED_SHUTDOWN(mp))
2455 return XFS_ERROR(EIO);
2456
2457 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2458 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2459 if (error) {
2460 xfs_trans_cancel(tp, 0);
2461 return error;
2462 }
2463 xfs_ilock(ip, XFS_ILOCK_EXCL);
2464 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2465
Christoph Hellwig613d7042007-10-11 17:44:08 +10002466 ip->i_d.di_dmevmask = evmask;
2467 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2470 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002471 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 return error;
2474}
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476/*
2477 * xfs_alloc_file_space()
2478 * This routine allocates disk space for the given file.
2479 *
2480 * If alloc_type == 0, this request is for an ALLOCSP type
2481 * request which will change the file size. In this case, no
2482 * DMAPI event will be generated by the call. A TRUNCATE event
2483 * will be generated later by xfs_setattr.
2484 *
2485 * If alloc_type != 0, this request is for a RESVSP type
2486 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2487 * lower block boundary byte address is less than the file's
2488 * length.
2489 *
2490 * RETURNS:
2491 * 0 on success
2492 * errno on error
2493 *
2494 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002495STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496xfs_alloc_file_space(
2497 xfs_inode_t *ip,
2498 xfs_off_t offset,
2499 xfs_off_t len,
2500 int alloc_type,
2501 int attr_flags)
2502{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002503 xfs_mount_t *mp = ip->i_mount;
2504 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 xfs_filblks_t allocated_fsb;
2506 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002507 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002509 xfs_fsblock_t firstfsb;
2510 int nimaps;
2511 int bmapi_flag;
2512 int quota_flag;
2513 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002515 xfs_bmbt_irec_t imaps[1], *imapp;
2516 xfs_bmap_free_t free_list;
2517 uint qblocks, resblks, resrtextents;
2518 int committed;
2519 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002521 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 if (XFS_FORCED_SHUTDOWN(mp))
2524 return XFS_ERROR(EIO);
2525
Christoph Hellwig7d095252009-06-08 15:33:32 +02002526 error = xfs_qm_dqattach(ip, 0);
2527 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return error;
2529
2530 if (len <= 0)
2531 return XFS_ERROR(EINVAL);
2532
David Chinner957d0eb2007-06-18 16:50:37 +10002533 rt = XFS_IS_REALTIME_INODE(ip);
2534 extsz = xfs_get_extsz_hint(ip);
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11002538 nimaps = 1;
2539 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2541 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2542
2543 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002544 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002545 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002546 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 xfs_off_t end_dmi_offset;
2548
2549 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002550 if (end_dmi_offset > ip->i_size)
2551 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002552 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2553 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002555 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557
2558 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002559 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 */
2561retry:
2562 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002563 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Nathan Scottdd9f4382006-01-11 15:28:28 +11002565 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11002566 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002567 */
2568 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002570 do_div(s, extsz);
2571 s *= extsz;
2572 e = startoffset_fsb + allocatesize_fsb;
2573 if ((temp = do_mod(startoffset_fsb, extsz)))
2574 e += temp;
2575 if ((temp = do_mod(e, extsz)))
2576 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002578 s = 0;
2579 e = allocatesize_fsb;
2580 }
2581
2582 if (unlikely(rt)) {
2583 resrtextents = qblocks = (uint)(e - s);
2584 resrtextents /= mp->m_sb.sb_rextsize;
2585 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2586 quota_flag = XFS_QMOPT_RES_RTBLKS;
2587 } else {
2588 resrtextents = 0;
2589 resblks = qblocks = \
2590 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2591 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
2593
2594 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002595 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 */
2597 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002598 error = xfs_trans_reserve(tp, resblks,
2599 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 XFS_TRANS_PERM_LOG_RES,
2601 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002603 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 */
2605 if (error) {
2606 /*
2607 * Free the transaction structure.
2608 */
2609 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2610 xfs_trans_cancel(tp, 0);
2611 break;
2612 }
2613 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002614 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2615 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 if (error)
2617 goto error1;
2618
2619 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2620 xfs_trans_ihold(tp, ip);
2621
2622 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002623 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002625 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002626 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002627 allocatesize_fsb, bmapi_flag,
2628 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002629 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (error) {
2631 goto error0;
2632 }
2633
2634 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002635 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002637 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (error) {
2639 goto error0;
2640 }
2641
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002642 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2644 if (error) {
2645 break;
2646 }
2647
2648 allocated_fsb = imapp->br_blockcount;
2649
Nathan Scottdd9f4382006-01-11 15:28:28 +11002650 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 error = XFS_ERROR(ENOSPC);
2652 break;
2653 }
2654
2655 startoffset_fsb += allocated_fsb;
2656 allocatesize_fsb -= allocated_fsb;
2657 }
2658dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002659 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002660 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002662 ip, DM_RIGHT_NULL,
2663 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
2665 if (error == 0)
2666 goto retry; /* Maybe DMAPI app. has made space */
2667 /* else fall through with error from XFS_SEND_DATA */
2668 }
2669
2670 return error;
2671
Nathan Scottdd9f4382006-01-11 15:28:28 +11002672error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002674 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002675
2676error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2678 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2679 goto dmapi_enospc_check;
2680}
2681
2682/*
2683 * Zero file bytes between startoff and endoff inclusive.
2684 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002685 *
2686 * This function is used by xfs_free_file_space() to zero
2687 * partial blocks when the range to free is not block aligned.
2688 * When unreserving space with boundaries that are not block
2689 * aligned we round up the start and round down the end
2690 * boundaries and then use this function to zero the parts of
2691 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
2693STATIC int
2694xfs_zero_remaining_bytes(
2695 xfs_inode_t *ip,
2696 xfs_off_t startoff,
2697 xfs_off_t endoff)
2698{
2699 xfs_bmbt_irec_t imap;
2700 xfs_fileoff_t offset_fsb;
2701 xfs_off_t lastoffset;
2702 xfs_off_t offset;
2703 xfs_buf_t *bp;
2704 xfs_mount_t *mp = ip->i_mount;
2705 int nimap;
2706 int error = 0;
2707
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002708 /*
2709 * Avoid doing I/O beyond eof - it's not necessary
2710 * since nothing can read beyond eof. The space will
2711 * be zeroed when the file is extended anyway.
2712 */
2713 if (startoff >= ip->i_size)
2714 return 0;
2715
2716 if (endoff > ip->i_size)
2717 endoff = ip->i_size;
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002720 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 mp->m_rtdev_targp : mp->m_ddev_targp);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11002722 if (!bp)
2723 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2726 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2727 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002728 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002729 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (error || nimap < 1)
2731 break;
2732 ASSERT(imap.br_blockcount >= 1);
2733 ASSERT(imap.br_startoff == offset_fsb);
2734 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2735 if (lastoffset > endoff)
2736 lastoffset = endoff;
2737 if (imap.br_startblock == HOLESTARTBLOCK)
2738 continue;
2739 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2740 if (imap.br_state == XFS_EXT_UNWRITTEN)
2741 continue;
2742 XFS_BUF_UNDONE(bp);
2743 XFS_BUF_UNWRITE(bp);
2744 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002745 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002747 error = xfs_iowait(bp);
2748 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2750 mp, bp, XFS_BUF_ADDR(bp));
2751 break;
2752 }
2753 memset(XFS_BUF_PTR(bp) +
2754 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2755 0, lastoffset - offset + 1);
2756 XFS_BUF_UNDONE(bp);
2757 XFS_BUF_UNREAD(bp);
2758 XFS_BUF_WRITE(bp);
2759 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002760 error = xfs_iowait(bp);
2761 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2763 mp, bp, XFS_BUF_ADDR(bp));
2764 break;
2765 }
2766 }
2767 xfs_buf_free(bp);
2768 return error;
2769}
2770
2771/*
2772 * xfs_free_file_space()
2773 * This routine frees disk space for the given file.
2774 *
2775 * This routine is only called by xfs_change_file_space
2776 * for an UNRESVSP type call.
2777 *
2778 * RETURNS:
2779 * 0 on success
2780 * errno on error
2781 *
2782 */
2783STATIC int
2784xfs_free_file_space(
2785 xfs_inode_t *ip,
2786 xfs_off_t offset,
2787 xfs_off_t len,
2788 int attr_flags)
2789{
2790 int committed;
2791 int done;
2792 xfs_off_t end_dmi_offset;
2793 xfs_fileoff_t endoffset_fsb;
2794 int error;
2795 xfs_fsblock_t firstfsb;
2796 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 xfs_bmbt_irec_t imap;
2798 xfs_off_t ioffset;
2799 xfs_extlen_t mod=0;
2800 xfs_mount_t *mp;
2801 int nimap;
2802 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002803 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 int rt;
2805 xfs_fileoff_t startoffset_fsb;
2806 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002807 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 mp = ip->i_mount;
2810
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002811 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002812
Christoph Hellwig7d095252009-06-08 15:33:32 +02002813 error = xfs_qm_dqattach(ip, 0);
2814 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return error;
2816
2817 error = 0;
2818 if (len <= 0) /* if nothing being freed */
2819 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002820 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2822 end_dmi_offset = offset + len;
2823 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
2824
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002825 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002826 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002827 if (end_dmi_offset > ip->i_size)
2828 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002829 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 offset, end_dmi_offset - offset,
2831 AT_DELAY_FLAG(attr_flags), NULL);
2832 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002833 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
2835
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002836 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002837 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002838 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002840 /* wait for the completion of any pending DIOs */
2841 xfs_ioend_wait(ip);
Yingping Lu9fa80462006-03-22 12:44:35 +11002842 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002843
Tim Shimmine6a4b372007-11-23 16:30:42 +11002844 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002846
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002847 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002848 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
2849 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10002850 if (error)
2851 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002852 }
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 /*
2855 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002856 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2858 * will take care of it for us.
2859 */
Eric Sandeen62118702008-03-06 13:44:28 +11002860 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002862 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002863 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (error)
2865 goto out_unlock_iolock;
2866 ASSERT(nimap == 0 || nimap == 1);
2867 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2868 xfs_daddr_t block;
2869
2870 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2871 block = imap.br_startblock;
2872 mod = do_div(block, mp->m_sb.sb_rextsize);
2873 if (mod)
2874 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2875 }
2876 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002877 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002878 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (error)
2880 goto out_unlock_iolock;
2881 ASSERT(nimap == 0 || nimap == 1);
2882 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2883 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2884 mod++;
2885 if (mod && (mod != mp->m_sb.sb_rextsize))
2886 endoffset_fsb -= mod;
2887 }
2888 }
2889 if ((done = (endoffset_fsb <= startoffset_fsb)))
2890 /*
2891 * One contiguous piece to clear
2892 */
2893 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2894 else {
2895 /*
2896 * Some full blocks, possibly two pieces to clear
2897 */
2898 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2899 error = xfs_zero_remaining_bytes(ip, offset,
2900 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2901 if (!error &&
2902 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2903 error = xfs_zero_remaining_bytes(ip,
2904 XFS_FSB_TO_B(mp, endoffset_fsb),
2905 offset + len - 1);
2906 }
2907
2908 /*
2909 * free file space until done or until there is an error
2910 */
2911 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2912 while (!error && !done) {
2913
2914 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002915 * allocate and setup the transaction. Allow this
2916 * transaction to dip into the reserve blocks to ensure
2917 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 */
2919 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002920 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 error = xfs_trans_reserve(tp,
2922 resblks,
2923 XFS_WRITE_LOG_RES(mp),
2924 0,
2925 XFS_TRANS_PERM_LOG_RES,
2926 XFS_WRITE_LOG_COUNT);
2927
2928 /*
2929 * check for running out of space
2930 */
2931 if (error) {
2932 /*
2933 * Free the transaction structure.
2934 */
2935 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2936 xfs_trans_cancel(tp, 0);
2937 break;
2938 }
2939 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02002940 error = xfs_trans_reserve_quota(tp, mp,
2941 ip->i_udquot, ip->i_gdquot,
2942 resblks, 0, XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if (error)
2944 goto error1;
2945
2946 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2947 xfs_trans_ihold(tp, ip);
2948
2949 /*
2950 * issue the bunmapi() call to free the blocks
2951 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002952 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002953 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002955 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (error) {
2957 goto error0;
2958 }
2959
2960 /*
2961 * complete the transaction
2962 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002963 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (error) {
2965 goto error0;
2966 }
2967
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002968 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2970 }
2971
2972 out_unlock_iolock:
2973 if (need_iolock)
2974 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2975 return error;
2976
2977 error0:
2978 xfs_bmap_cancel(&free_list);
2979 error1:
2980 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2981 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2982 XFS_ILOCK_EXCL);
2983 return error;
2984}
2985
2986/*
2987 * xfs_change_file_space()
2988 * This routine allocates or frees disk space for the given file.
2989 * The user specified parameters are checked for alignment and size
2990 * limitations.
2991 *
2992 * RETURNS:
2993 * 0 on success
2994 * errno on error
2995 *
2996 */
2997int
2998xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002999 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 int cmd,
3001 xfs_flock64_t *bf,
3002 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 int attr_flags)
3004{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003005 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 int clrprealloc;
3007 int error;
3008 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 int setprealloc;
3010 xfs_off_t startoffset;
3011 xfs_off_t llen;
3012 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003013 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003015 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Christoph Hellwig993386c2007-08-28 16:12:30 +10003017 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return XFS_ERROR(EINVAL);
3019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 switch (bf->l_whence) {
3021 case 0: /*SEEK_SET*/
3022 break;
3023 case 1: /*SEEK_CUR*/
3024 bf->l_start += offset;
3025 break;
3026 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003027 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 break;
3029 default:
3030 return XFS_ERROR(EINVAL);
3031 }
3032
3033 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3034
3035 if ( (bf->l_start < 0)
3036 || (bf->l_start > XFS_MAXIOFFSET(mp))
3037 || (bf->l_start + llen < 0)
3038 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3039 return XFS_ERROR(EINVAL);
3040
3041 bf->l_whence = 0;
3042
3043 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003044 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 /*
3047 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3048 * file space.
3049 * These calls do NOT zero the data space allocated to the file,
3050 * nor do they change the file size.
3051 *
3052 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3053 * space.
3054 * These calls cause the new file data to be zeroed and the file
3055 * size to be changed.
3056 */
3057 setprealloc = clrprealloc = 0;
3058
3059 switch (cmd) {
3060 case XFS_IOC_RESVSP:
3061 case XFS_IOC_RESVSP64:
3062 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3063 1, attr_flags);
3064 if (error)
3065 return error;
3066 setprealloc = 1;
3067 break;
3068
3069 case XFS_IOC_UNRESVSP:
3070 case XFS_IOC_UNRESVSP64:
3071 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3072 attr_flags)))
3073 return error;
3074 break;
3075
3076 case XFS_IOC_ALLOCSP:
3077 case XFS_IOC_ALLOCSP64:
3078 case XFS_IOC_FREESP:
3079 case XFS_IOC_FREESP64:
3080 if (startoffset > fsize) {
3081 error = xfs_alloc_file_space(ip, fsize,
3082 startoffset - fsize, 0, attr_flags);
3083 if (error)
3084 break;
3085 }
3086
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003087 iattr.ia_valid = ATTR_SIZE;
3088 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +11003090 error = xfs_setattr(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
3092 if (error)
3093 return error;
3094
3095 clrprealloc = 1;
3096 break;
3097
3098 default:
3099 ASSERT(0);
3100 return XFS_ERROR(EINVAL);
3101 }
3102
3103 /*
3104 * update the inode timestamp, mode, and prealloc flag bits
3105 */
3106 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3107
3108 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3109 0, 0, 0))) {
3110 /* ASSERT(0); */
3111 xfs_trans_cancel(tp, 0);
3112 return error;
3113 }
3114
3115 xfs_ilock(ip, XFS_ILOCK_EXCL);
3116
3117 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3118 xfs_trans_ihold(tp, ip);
3119
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003120 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 ip->i_d.di_mode &= ~S_ISUID;
3122
3123 /*
3124 * Note that we don't have to worry about mandatory
3125 * file locking being disabled here because we only
3126 * clear the S_ISGID bit if the Group execute bit is
3127 * on, but if it was on then mandatory locking wouldn't
3128 * have been enabled.
3129 */
3130 if (ip->i_d.di_mode & S_IXGRP)
3131 ip->i_d.di_mode &= ~S_ISGID;
3132
3133 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3134 }
3135 if (setprealloc)
3136 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3137 else if (clrprealloc)
3138 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3139
3140 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3141 xfs_trans_set_sync(tp);
3142
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003143 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3146
3147 return error;
3148}