blob: 19cf90a9c762841dc8b189676cb684ee4304946f [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_attr.h"
46#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_quota.h"
49#include "xfs_utils.h"
Nathan Scotta844f452005-11-02 14:38:42 +110050#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "xfs_log_priv.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100053#include "xfs_filestream.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100054#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Christoph Hellwig993386c2007-08-28 16:12:30 +100056int
Linus Torvalds1da177e2005-04-16 15:20:36 -070057xfs_setattr(
Christoph Hellwig0f285c82008-07-18 17:13:28 +100058 struct xfs_inode *ip,
59 struct iattr *iattr,
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +110060 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Christoph Hellwig993386c2007-08-28 16:12:30 +100062 xfs_mount_t *mp = ip->i_mount;
David Chinnere4f75292008-08-13 16:00:45 +100063 struct inode *inode = VFS_I(ip);
Christoph Hellwig0f285c82008-07-18 17:13:28 +100064 int mask = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 int code;
67 uint lock_flags;
68 uint commit_flags=0;
69 uid_t uid=0, iuid=0;
70 gid_t gid=0, igid=0;
71 int timeflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
Dean Roehrich5fcbab32005-05-05 13:27:19 -070073 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Lachlan McIlroycf441ee2008-02-07 16:42:19 +110075 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Christoph Hellwigbd186aa2007-08-30 17:21:12 +100077 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return XFS_ERROR(EROFS);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (XFS_FORCED_SHUTDOWN(mp))
81 return XFS_ERROR(EIO);
82
Christoph Hellwigc4cd7472008-12-09 04:47:34 -050083 code = -inode_change_ok(inode, iattr);
84 if (code)
85 return code;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 olddquot1 = olddquot2 = NULL;
88 udqp = gdqp = NULL;
89
90 /*
91 * If disk quotas is on, we make sure that the dquots do exist on disk,
92 * before we start any other transactions. Trying to do this later
93 * is messy. We don't care to take a readlock to look at the ids
94 * in inode here, because we can't hold it across the trans_reserve.
95 * If the IDs do change before we take the ilock, we're covered
96 * because the i_*dquot fields will get updated anyway.
97 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +100098 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 uint qflags = 0;
100
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000101 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
102 uid = iattr->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 qflags |= XFS_QMOPT_UQUOTA;
104 } else {
105 uid = ip->i_d.di_uid;
106 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000107 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
108 gid = iattr->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 qflags |= XFS_QMOPT_GQUOTA;
110 } else {
111 gid = ip->i_d.di_gid;
112 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /*
115 * We take a reference when we initialize udqp and gdqp,
116 * so it is important that we never blindly double trip on
117 * the same variable. See xfs_create() for an example.
118 */
119 ASSERT(udqp == NULL);
120 ASSERT(gdqp == NULL);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000121 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, ip->i_d.di_projid,
122 qflags, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100124 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126
127 /*
128 * For the other attributes, we acquire the inode lock and
129 * first do an error checking pass.
130 */
131 tp = NULL;
132 lock_flags = XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000133 if (flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700134 need_iolock = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000135 if (!(mask & ATTR_SIZE)) {
136 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 (mp->m_flags & XFS_MOUNT_WSYNC)) {
138 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
139 commit_flags = 0;
140 if ((code = xfs_trans_reserve(tp, 0,
141 XFS_ICHANGE_LOG_RES(mp), 0,
142 0, 0))) {
143 lock_flags = 0;
144 goto error_return;
145 }
146 }
147 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000148 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000149 !(flags & XFS_ATTR_DMI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100151 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000152 iattr->ia_size, 0, dmflags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (code) {
154 lock_flags = 0;
155 goto error_return;
156 }
157 }
158 if (need_iolock)
159 lock_flags |= XFS_IOLOCK_EXCL;
160 }
161
162 xfs_ilock(ip, lock_flags);
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
165 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000167 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /*
169 * These IDs could have changed since we last looked at them.
170 * But, we're assured that if the ownership did change
171 * while we didn't have the inode locked, inode's dquot(s)
172 * would have changed also.
173 */
174 iuid = ip->i_d.di_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 igid = ip->i_d.di_gid;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000176 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
177 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000180 * Do a quota reservation only if uid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * going to change.
182 */
183 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
184 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
185 ASSERT(tp);
186 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
187 capable(CAP_FOWNER) ?
188 XFS_QMOPT_FORCE_RES : 0);
189 if (code) /* out of quota */
190 goto error_return;
191 }
192 }
193
194 /*
195 * Truncate file. Must have write permission and not be a directory.
196 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000197 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Short circuit the truncate case for zero length files */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000199 if (iattr->ia_size == 0 &&
200 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 xfs_iunlock(ip, XFS_ILOCK_EXCL);
202 lock_flags &= ~XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000203 if (mask & ATTR_CTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
205 code = 0;
206 goto error_return;
207 }
208
Christoph Hellwig42173f62008-04-22 17:33:25 +1000209 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 code = XFS_ERROR(EISDIR);
211 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000212 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 code = XFS_ERROR(EINVAL);
214 goto error_return;
215 }
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /*
218 * Make sure that the dquots are attached to the inode.
219 */
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500220 code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
221 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500224 /*
225 * Now we can make the changes. Before we join the inode
226 * to the transaction, if ATTR_SIZE is set then take care of
227 * the part of the truncation that must be done without the
228 * inode lock. This needs to be done before joining the inode
229 * to the transaction, because the inode cannot be unlocked
230 * once it is a part of the transaction.
231 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000232 if (iattr->ia_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000233 /*
234 * Do the first part of growing a file: zero any data
235 * in the last block that is beyond the old EOF. We
236 * need to do this before the inode is joined to the
237 * transaction to modify the i_size.
238 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000239 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000242
243 /*
244 * We are going to log the inode size change in this
245 * transaction so any previous writes that are beyond the on
246 * disk EOF and the new EOF that have not been written out need
247 * to be written here. If we do not write the data out, we
248 * expose ourselves to the null files problem.
249 *
250 * Only flush from the on disk size to the smaller of the in
251 * memory file size or the new size as that's the range we
252 * really care about here and prevents waiting for other data
253 * not within the range we care about here.
254 */
255 if (!code &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000256 ip->i_size != ip->i_d.di_size &&
257 iattr->ia_size > ip->i_d.di_size) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000258 code = xfs_flush_pages(ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000259 ip->i_d.di_size, iattr->ia_size,
David Chinnerc32676e2007-07-19 16:28:58 +1000260 XFS_B_ASYNC, FI_NONE);
261 }
262
263 /* wait for all I/O to complete */
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100264 xfs_ioend_wait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!code)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000267 code = xfs_itruncate_data(ip, iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (code) {
269 ASSERT(tp == NULL);
270 lock_flags &= ~XFS_ILOCK_EXCL;
271 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
272 goto error_return;
273 }
274 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
275 if ((code = xfs_trans_reserve(tp, 0,
276 XFS_ITRUNCATE_LOG_RES(mp), 0,
277 XFS_TRANS_PERM_LOG_RES,
278 XFS_ITRUNCATE_LOG_COUNT))) {
279 xfs_trans_cancel(tp, 0);
280 if (need_iolock)
281 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
282 return code;
283 }
284 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
285 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 xfs_trans_ijoin(tp, ip, lock_flags);
288 xfs_trans_ihold(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
David Chinner44d814c2008-03-06 13:45:29 +1100290 /*
291 * Only change the c/mtime if we are changing the size
292 * or we are explicitly asked to change it. This handles
293 * the semantic difference between truncate() and ftruncate()
294 * as implemented in the VFS.
295 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000296 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
David Chinner44d814c2008-03-06 13:45:29 +1100297 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
298
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000299 if (iattr->ia_size > ip->i_size) {
300 ip->i_d.di_size = iattr->ia_size;
301 ip->i_size = iattr->ia_size;
302 if (!(flags & XFS_ATTR_DMI))
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000303 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
304 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000305 } else if (iattr->ia_size <= ip->i_size ||
306 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /*
308 * signal a sync transaction unless
309 * we're truncating an already unlinked
310 * file on a wsync filesystem
311 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000312 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 XFS_DATA_FORK,
314 ((ip->i_d.di_nlink != 0 ||
315 !(mp->m_flags & XFS_MOUNT_WSYNC))
316 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000317 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000319 /*
320 * Truncated "down", so we're removing references
321 * to old data here - if we now delay flushing for
322 * a long time, we expose ourselves unduly to the
323 * notorious NULL files problem. So, we mark this
324 * vnode and flush it when the file is closed, and
325 * do not wait the usual (long) time for writeout.
326 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000327 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500329 } else if (tp) {
330 xfs_trans_ijoin(tp, ip, lock_flags);
331 xfs_trans_ihold(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
334 /*
335 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000337 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /*
339 * CAP_FSETID overrides the following restrictions:
340 *
341 * The set-user-ID and set-group-ID bits of a file will be
342 * cleared upon successful return from chown()
343 */
344 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
345 !capable(CAP_FSETID)) {
346 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
347 }
348
349 /*
350 * Change the ownerships and register quota modifications
351 * in the transaction.
352 */
353 if (iuid != uid) {
354 if (XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000355 ASSERT(mask & ATTR_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 ASSERT(udqp);
357 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
358 &ip->i_udquot, udqp);
359 }
360 ip->i_d.di_uid = uid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000361 inode->i_uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363 if (igid != gid) {
364 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000365 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000366 ASSERT(mask & ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ASSERT(gdqp);
368 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
369 &ip->i_gdquot, gdqp);
370 }
371 ip->i_d.di_gid = gid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000372 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
376 timeflags |= XFS_ICHGTIME_CHG;
377 }
378
Christoph Hellwigc4cd7472008-12-09 04:47:34 -0500379 /*
380 * Change file access modes.
381 */
382 if (mask & ATTR_MODE) {
383 umode_t mode = iattr->ia_mode;
384
385 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
386 mode &= ~S_ISGID;
387
388 ip->i_d.di_mode &= S_IFMT;
389 ip->i_d.di_mode |= mode & ~S_IFMT;
390
391 inode->i_mode &= S_IFMT;
392 inode->i_mode |= mode & ~S_IFMT;
393
394 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
395 timeflags |= XFS_ICHGTIME_CHG;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /*
399 * Change file access or modified times.
400 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000401 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
402 if (mask & ATTR_ATIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000403 inode->i_atime = iattr->ia_atime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000404 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
405 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 ip->i_update_core = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000408 if (mask & ATTR_MTIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000409 inode->i_mtime = iattr->ia_mtime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000410 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
411 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 timeflags &= ~XFS_ICHGTIME_MOD;
413 timeflags |= XFS_ICHGTIME_CHG;
414 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000415 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
417 }
418
419 /*
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000420 * Change file inode change time only if ATTR_CTIME set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * AND we have been called by a DMI function.
422 */
423
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000424 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000425 inode->i_ctime = iattr->ia_ctime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000426 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
427 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 ip->i_update_core = 1;
429 timeflags &= ~XFS_ICHGTIME_CHG;
430 }
431
432 /*
433 * Send out timestamp changes that need to be set to the
434 * current time. Not done when called by a DMI function.
435 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000436 if (timeflags && !(flags & XFS_ATTR_DMI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 xfs_ichgtime(ip, timeflags);
438
439 XFS_STATS_INC(xs_ig_attrchg);
440
441 /*
442 * If this is a synchronous mount, make sure that the
443 * transaction goes to disk before returning to the user.
444 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000445 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * One for the truncate and one for the timestamps since we
447 * don't want to change the timestamps unless we're sure the
448 * truncate worked. Truncates are less than 1% of the laddis
449 * mix so this probably isn't worth the trouble to optimize.
450 */
451 code = 0;
452 if (tp) {
453 if (mp->m_flags & XFS_MOUNT_WSYNC)
454 xfs_trans_set_sync(tp);
455
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000456 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 xfs_iunlock(ip, lock_flags);
460
461 /*
462 * Release any dquot(s) the inode had kept before chown.
463 */
464 XFS_QM_DQRELE(mp, olddquot1);
465 XFS_QM_DQRELE(mp, olddquot2);
466 XFS_QM_DQRELE(mp, udqp);
467 XFS_QM_DQRELE(mp, gdqp);
468
469 if (code) {
470 return code;
471 }
472
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000473 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000474 !(flags & XFS_ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100475 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 NULL, DM_RIGHT_NULL, NULL, NULL,
477 0, 0, AT_DELAY_FLAG(flags));
478 }
479 return 0;
480
481 abort_return:
482 commit_flags |= XFS_TRANS_ABORT;
483 /* FALLTHROUGH */
484 error_return:
485 XFS_QM_DQRELE(mp, udqp);
486 XFS_QM_DQRELE(mp, gdqp);
487 if (tp) {
488 xfs_trans_cancel(tp, commit_flags);
489 }
490 if (lock_flags != 0) {
491 xfs_iunlock(ip, lock_flags);
492 }
493 return code;
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000497 * The maximum pathlen is 1024 bytes. Since the minimum file system
498 * blocksize is 512 bytes, we can get a max of 2 extents back from
499 * bmapi.
500 */
501#define SYMLINK_MAPS 2
502
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000503STATIC int
504xfs_readlink_bmap(
505 xfs_inode_t *ip,
506 char *link)
507{
508 xfs_mount_t *mp = ip->i_mount;
509 int pathlen = ip->i_d.di_size;
510 int nmaps = SYMLINK_MAPS;
511 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
512 xfs_daddr_t d;
513 int byte_cnt;
514 int n;
515 xfs_buf_t *bp;
516 int error = 0;
517
518 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
519 mval, &nmaps, NULL, NULL);
520 if (error)
521 goto out;
522
523 for (n = 0; n < nmaps; n++) {
524 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
525 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
526
527 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
528 error = XFS_BUF_GETERROR(bp);
529 if (error) {
530 xfs_ioerror_alert("xfs_readlink",
531 ip->i_mount, bp, XFS_BUF_ADDR(bp));
532 xfs_buf_relse(bp);
533 goto out;
534 }
535 if (pathlen < byte_cnt)
536 byte_cnt = pathlen;
537 pathlen -= byte_cnt;
538
539 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
540 xfs_buf_relse(bp);
541 }
542
543 link[ip->i_d.di_size] = '\0';
544 error = 0;
545
546 out:
547 return error;
548}
549
Christoph Hellwig993386c2007-08-28 16:12:30 +1000550int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000552 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000553 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000555 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100559 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (XFS_FORCED_SHUTDOWN(mp))
562 return XFS_ERROR(EIO);
563
564 xfs_ilock(ip, XFS_ILOCK_SHARED);
565
566 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000567 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000569 pathlen = ip->i_d.di_size;
570 if (!pathlen)
571 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000574 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
575 link[pathlen] = '\0';
576 } else {
577 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000580 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return error;
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
586 * xfs_fsync
587 *
David Chinner978b7232008-05-19 16:29:46 +1000588 * This is called to sync the inode and its data out to disk. We need to hold
589 * the I/O lock while flushing the data, and the inode lock while flushing the
590 * inode. The inode lock CANNOT be held while flushing the data, so acquire
591 * after we're done with that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000593int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594xfs_fsync(
David Chinner978b7232008-05-19 16:29:46 +1000595 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 xfs_trans_t *tp;
598 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100599 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100601 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
604 return XFS_ERROR(EIO);
605
David Chinner978b7232008-05-19 16:29:46 +1000606 /* capture size updates in I/O completion before writing the inode. */
Dave Chinner2e656092008-11-28 14:23:33 +1100607 error = xfs_wait_on_pages(ip, 0, -1);
David Chinner978b7232008-05-19 16:29:46 +1000608 if (error)
609 return XFS_ERROR(error);
Lachlan McIlroy776a75f2007-09-14 15:22:50 +1000610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
David Chinner978b7232008-05-19 16:29:46 +1000612 * We always need to make sure that the required inode state is safe on
613 * disk. The vnode might be clean but we still might need to force the
614 * log because of committed transactions that haven't hit the disk yet.
615 * Likewise, there could be unflushed non-transactional changes to the
616 * inode core that have to go to disk and this requires us to issue
617 * a synchronous transaction to capture these changes correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 *
David Chinner978b7232008-05-19 16:29:46 +1000619 * This code relies on the assumption that if the update_* fields
620 * of the inode are clear and the inode is unpinned then it is clean
621 * and no action is required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
623 xfs_ilock(ip, XFS_ILOCK_SHARED);
624
David Chinner978b7232008-05-19 16:29:46 +1000625 if (!(ip->i_update_size || ip->i_update_core)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /*
David Chinner978b7232008-05-19 16:29:46 +1000627 * Timestamps/size haven't changed since last inode flush or
628 * inode transaction commit. That means either nothing got
629 * written or a transaction committed which caught the updates.
630 * If the latter happened and the transaction hasn't hit the
631 * disk yet, the inode will be still be pinned. If it is,
632 * force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
634
635 xfs_iunlock(ip, XFS_ILOCK_SHARED);
636
637 if (xfs_ipincount(ip)) {
David Chinner978b7232008-05-19 16:29:46 +1000638 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
639 XFS_LOG_FORCE | XFS_LOG_SYNC,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100640 &log_flushed);
641 } else {
642 /*
David Chinner978b7232008-05-19 16:29:46 +1000643 * If the inode is not pinned and nothing has changed
644 * we don't need to flush the cache.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100645 */
646 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 } else {
649 /*
David Chinner978b7232008-05-19 16:29:46 +1000650 * Kick off a transaction to log the inode core to get the
651 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
653 xfs_iunlock(ip, XFS_ILOCK_SHARED);
654 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000655 error = xfs_trans_reserve(tp, 0,
656 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
657 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfs_trans_cancel(tp, 0);
659 return error;
660 }
661 xfs_ilock(ip, XFS_ILOCK_EXCL);
662
663 /*
David Chinner978b7232008-05-19 16:29:46 +1000664 * Note - it's possible that we might have pushed ourselves out
665 * of the way during trans_reserve which would flush the inode.
666 * But there's no guarantee that the inode buffer has actually
667 * gone out yet (it's delwri). Plus the buffer could be pinned
668 * anyway if it's part of an inode in another recent
669 * transaction. So we play it safe and fire off the
670 * transaction anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
672 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
673 xfs_trans_ihold(tp, ip);
674 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
David Chinner978b7232008-05-19 16:29:46 +1000675 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000676 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 xfs_iunlock(ip, XFS_ILOCK_EXCL);
679 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100680
681 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
682 /*
683 * If the log write didn't issue an ordered tag we need
684 * to flush the disk cache for the data device now.
685 */
686 if (!log_flushed)
687 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
688
689 /*
690 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000691 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100692 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100693 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100694 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
695 }
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return error;
698}
699
700/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000701 * This is called by xfs_inactive to free any blocks beyond eof
702 * when the link count isn't zero and by xfs_dm_punch_hole() when
703 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000705int
706xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000708 xfs_inode_t *ip,
709 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 xfs_trans_t *tp;
712 int error;
713 xfs_fileoff_t end_fsb;
714 xfs_fileoff_t last_fsb;
715 xfs_filblks_t map_len;
716 int nimaps;
717 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +1000718 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /*
721 * Figure out if there are any blocks beyond the end
722 * of the file. If not, then there is nothing to do.
723 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000724 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
726 map_len = last_fsb - end_fsb;
727 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 nimaps = 1;
731 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000732 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000733 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 xfs_iunlock(ip, XFS_ILOCK_SHARED);
735
736 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100737 (imap.br_startblock != HOLESTARTBLOCK ||
738 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /*
740 * Attach the dquots to the inode up front.
741 */
742 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +0100743 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /*
746 * There are blocks after the end of file.
747 * Free them up now by truncating the file to
748 * its current size.
749 */
750 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
751
752 /*
753 * Do the xfs_itruncate_start() call before
754 * reserving any log space because
755 * itruncate_start will call into the buffer
756 * cache and we can't
757 * do that within a transaction.
758 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000759 if (use_iolock)
760 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000761 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000762 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000763 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000764 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +1000765 if (use_iolock)
766 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000767 return error;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 error = xfs_trans_reserve(tp, 0,
771 XFS_ITRUNCATE_LOG_RES(mp),
772 0, XFS_TRANS_PERM_LOG_RES,
773 XFS_ITRUNCATE_LOG_COUNT);
774 if (error) {
775 ASSERT(XFS_FORCED_SHUTDOWN(mp));
776 xfs_trans_cancel(tp, 0);
777 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100778 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
781 xfs_ilock(ip, XFS_ILOCK_EXCL);
782 xfs_trans_ijoin(tp, ip,
783 XFS_IOLOCK_EXCL |
784 XFS_ILOCK_EXCL);
785 xfs_trans_ihold(tp, ip);
786
787 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000788 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 XFS_DATA_FORK,
790 0);
791 /*
792 * If we get an error at this point we
793 * simply don't bother truncating the file.
794 */
795 if (error) {
796 xfs_trans_cancel(tp,
797 (XFS_TRANS_RELEASE_LOG_RES |
798 XFS_TRANS_ABORT));
799 } else {
800 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000801 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
David Chinner92dfe8d2007-05-24 15:22:19 +1000803 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
804 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100806 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809/*
810 * Free a symlink that has blocks associated with it.
811 */
812STATIC int
813xfs_inactive_symlink_rmt(
814 xfs_inode_t *ip,
815 xfs_trans_t **tpp)
816{
817 xfs_buf_t *bp;
818 int committed;
819 int done;
820 int error;
821 xfs_fsblock_t first_block;
822 xfs_bmap_free_t free_list;
823 int i;
824 xfs_mount_t *mp;
825 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
826 int nmaps;
827 xfs_trans_t *ntp;
828 int size;
829 xfs_trans_t *tp;
830
831 tp = *tpp;
832 mp = ip->i_mount;
833 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
834 /*
835 * We're freeing a symlink that has some
836 * blocks allocated to it. Free the
837 * blocks here. We know that we've got
838 * either 1 or 2 extents and that we can
839 * free them all in one bunmapi call.
840 */
841 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
842 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
843 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
844 ASSERT(XFS_FORCED_SHUTDOWN(mp));
845 xfs_trans_cancel(tp, 0);
846 *tpp = NULL;
847 return error;
848 }
849 /*
850 * Lock the inode, fix the size, and join it to the transaction.
851 * Hold it so in the normal path, we still have it locked for
852 * the second transaction. In the error paths we need it
853 * held so the cancel won't rele it, see below.
854 */
855 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
856 size = (int)ip->i_d.di_size;
857 ip->i_d.di_size = 0;
858 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
859 xfs_trans_ihold(tp, ip);
860 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
861 /*
862 * Find the block(s) so we can inval and unmap them.
863 */
864 done = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600865 xfs_bmap_init(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800866 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
868 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000869 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto error0;
871 /*
872 * Invalidate the block(s).
873 */
874 for (i = 0; i < nmaps; i++) {
875 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
876 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
877 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
878 xfs_trans_binval(tp, bp);
879 }
880 /*
881 * Unmap the dead block(s) to the free_list.
882 */
883 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000884 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto error1;
886 ASSERT(done);
887 /*
888 * Commit the first transaction. This logs the EFI and the inode.
889 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100890 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 goto error1;
892 /*
893 * The transaction must have been committed, since there were
894 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
895 * The new tp has the extent freeing and EFDs.
896 */
897 ASSERT(committed);
898 /*
899 * The first xact was committed, so add the inode to the new one.
900 * Mark it dirty so it will be logged and moved forward in the log as
901 * part of every commit.
902 */
903 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
904 xfs_trans_ihold(tp, ip);
905 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
906 /*
907 * Get a new, empty transaction to return to our caller.
908 */
909 ntp = xfs_trans_dup(tp);
910 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000911 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * If we get an error on the commit here or on the reserve below,
913 * we need to unlock the inode since the new transaction doesn't
914 * have the inode attached.
915 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000916 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 tp = ntp;
918 if (error) {
919 ASSERT(XFS_FORCED_SHUTDOWN(mp));
920 goto error0;
921 }
922 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100923 * transaction commit worked ok so we can drop the extra ticket
924 * reference that we gained in xfs_trans_dup()
925 */
926 xfs_log_ticket_put(tp->t_ticket);
927
928 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 * Remove the memory for extent descriptions (just bookkeeping).
930 */
931 if (ip->i_df.if_bytes)
932 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
933 ASSERT(ip->i_df.if_bytes == 0);
934 /*
935 * Put an itruncate log reservation in the new transaction
936 * for our caller.
937 */
938 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
939 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
940 ASSERT(XFS_FORCED_SHUTDOWN(mp));
941 goto error0;
942 }
943 /*
944 * Return with the inode locked but not joined to the transaction.
945 */
946 *tpp = tp;
947 return 0;
948
949 error1:
950 xfs_bmap_cancel(&free_list);
951 error0:
952 /*
953 * Have to come here with the inode locked and either
954 * (held and in the transaction) or (not in the transaction).
955 * If the inode isn't held then cancel would iput it, but
956 * that's wrong since this is inactive and the vnode ref
957 * count is 0 already.
958 * Cancel won't do anything to the inode if held, but it still
959 * needs to be locked until the cancel is done, if it was
960 * joined to the transaction.
961 */
962 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
963 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
964 *tpp = NULL;
965 return error;
966
967}
968
969STATIC int
970xfs_inactive_symlink_local(
971 xfs_inode_t *ip,
972 xfs_trans_t **tpp)
973{
974 int error;
975
976 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
977 /*
978 * We're freeing a symlink which fit into
979 * the inode. Just free the memory used
980 * to hold the old symlink.
981 */
982 error = xfs_trans_reserve(*tpp, 0,
983 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
984 0, XFS_TRANS_PERM_LOG_RES,
985 XFS_ITRUNCATE_LOG_COUNT);
986
987 if (error) {
988 xfs_trans_cancel(*tpp, 0);
989 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +0100990 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
993
994 /*
995 * Zero length symlinks _can_ exist.
996 */
997 if (ip->i_df.if_bytes > 0) {
998 xfs_idata_realloc(ip,
999 -(ip->i_df.if_bytes),
1000 XFS_DATA_FORK);
1001 ASSERT(ip->i_df.if_bytes == 0);
1002 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001003 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006STATIC int
1007xfs_inactive_attrs(
1008 xfs_inode_t *ip,
1009 xfs_trans_t **tpp)
1010{
1011 xfs_trans_t *tp;
1012 int error;
1013 xfs_mount_t *mp;
1014
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001015 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 tp = *tpp;
1017 mp = ip->i_mount;
1018 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001019 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001021 if (error)
1022 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001025 if (error)
1026 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1029 error = xfs_trans_reserve(tp, 0,
1030 XFS_IFREE_LOG_RES(mp),
1031 0, XFS_TRANS_PERM_LOG_RES,
1032 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001033 if (error)
1034 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 xfs_ilock(ip, XFS_ILOCK_EXCL);
1037 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1038 xfs_trans_ihold(tp, ip);
1039 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1040
1041 ASSERT(ip->i_d.di_anextents == 0);
1042
1043 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001044 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001045
1046error_cancel:
1047 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1048 xfs_trans_cancel(tp, 0);
1049error_unlock:
1050 *tpp = NULL;
1051 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1052 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Christoph Hellwig993386c2007-08-28 16:12:30 +10001055int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001057 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001059 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 int error;
1061
Christoph Hellwig42173f62008-04-22 17:33:25 +10001062 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001066 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return 0;
1068
David Chinner2a82b8b2007-07-11 11:09:12 +10001069 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001070 int truncated;
1071
David Chinner2a82b8b2007-07-11 11:09:12 +10001072 /*
1073 * If we are using filestreams, and we have an unlinked
1074 * file that we are processing the last close on, then nothing
1075 * will be able to reopen and write to this file. Purge this
1076 * inode from the filestreams cache so that it doesn't delay
1077 * teardown of the inode.
1078 */
1079 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1080 xfs_filestream_deassociate(ip);
1081
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001082 /*
1083 * If we previously truncated this file and removed old data
1084 * in the process, we want to initiate "early" writeout on
1085 * the last close. This is an attempt to combat the notorious
1086 * NULL files problem which is particularly noticable from a
1087 * truncate down, buffered (re-)write (delalloc), followed by
1088 * a crash. What we are effectively doing here is
1089 * significantly reducing the time window where we'd otherwise
1090 * be exposed to that problem.
1091 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001092 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001093 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001094 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001095 }
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (ip->i_d.di_nlink != 0) {
1098 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001099 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001100 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001102 (!(ip->i_d.di_flags &
1103 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001104 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1105 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001106 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108 }
1109
1110 return 0;
1111}
1112
1113/*
1114 * xfs_inactive
1115 *
1116 * This is called when the vnode reference count for the vnode
1117 * goes to zero. If the file has been unlinked, then it must
1118 * now be truncated. Also, we clear all of the read-ahead state
1119 * kept for the inode here since the file is now closed.
1120 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001121int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001123 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001125 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 xfs_fsblock_t first_block;
1127 int committed;
1128 xfs_trans_t *tp;
1129 xfs_mount_t *mp;
1130 int error;
1131 int truncate;
1132
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001133 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /*
1136 * If the inode is already free, then there can be nothing
1137 * to clean up here.
1138 */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +01001139 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 ASSERT(ip->i_df.if_real_bytes == 0);
1141 ASSERT(ip->i_df.if_broot_bytes == 0);
1142 return VN_INACTIVE_CACHE;
1143 }
1144
1145 /*
1146 * Only do a truncate if it's a regular file with
1147 * some actual space in it. It's OK to look at the
1148 * inode's fields without the lock because we're the
1149 * only one with a reference to the inode.
1150 */
1151 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001152 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1153 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1155
1156 mp = ip->i_mount;
1157
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001158 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1159 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 error = 0;
1162
1163 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001164 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 goto out;
1166
1167 if (ip->i_d.di_nlink != 0) {
1168 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001169 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001170 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001171 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1172 (!(ip->i_d.di_flags &
1173 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1174 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001175 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1176 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001177 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179 goto out;
1180 }
1181
1182 ASSERT(ip->i_d.di_nlink == 0);
1183
1184 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001185 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1188 if (truncate) {
1189 /*
1190 * Do the xfs_itruncate_start() call before
1191 * reserving any log space because itruncate_start
1192 * will call into the buffer cache and we can't
1193 * do that within a transaction.
1194 */
1195 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1196
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001197 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1198 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001199 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001200 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1201 return VN_INACTIVE_CACHE;
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 error = xfs_trans_reserve(tp, 0,
1205 XFS_ITRUNCATE_LOG_RES(mp),
1206 0, XFS_TRANS_PERM_LOG_RES,
1207 XFS_ITRUNCATE_LOG_COUNT);
1208 if (error) {
1209 /* Don't call itruncate_cleanup */
1210 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1211 xfs_trans_cancel(tp, 0);
1212 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001213 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
1216 xfs_ilock(ip, XFS_ILOCK_EXCL);
1217 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1218 xfs_trans_ihold(tp, ip);
1219
1220 /*
1221 * normally, we have to run xfs_itruncate_finish sync.
1222 * But if filesystem is wsync and we're in the inactive
1223 * path, then we know that nlink == 0, and that the
1224 * xaction that made nlink == 0 is permanently committed
1225 * since xfs_remove runs as a synchronous transaction.
1226 */
1227 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1228 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1229
1230 if (error) {
1231 xfs_trans_cancel(tp,
1232 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1233 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001234 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1237
1238 /*
1239 * If we get an error while cleaning up a
1240 * symlink we bail out.
1241 */
1242 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1243 xfs_inactive_symlink_rmt(ip, &tp) :
1244 xfs_inactive_symlink_local(ip, &tp);
1245
1246 if (error) {
1247 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001248 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
1251 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1252 xfs_trans_ihold(tp, ip);
1253 } else {
1254 error = xfs_trans_reserve(tp, 0,
1255 XFS_IFREE_LOG_RES(mp),
1256 0, XFS_TRANS_PERM_LOG_RES,
1257 XFS_INACTIVE_LOG_COUNT);
1258 if (error) {
1259 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1260 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001261 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
1264 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1265 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1266 xfs_trans_ihold(tp, ip);
1267 }
1268
1269 /*
1270 * If there are attributes associated with the file
1271 * then blow them away now. The code calls a routine
1272 * that recursively deconstructs the attribute fork.
1273 * We need to just commit the current transaction
1274 * because we can't use it for xfs_attr_inactive().
1275 */
1276 if (ip->i_d.di_anextents > 0) {
1277 error = xfs_inactive_attrs(ip, &tp);
1278 /*
1279 * If we got an error, the transaction is already
1280 * cancelled, and the inode is unlocked. Just get out.
1281 */
1282 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001283 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 } else if (ip->i_afp) {
1285 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1286 }
1287
1288 /*
1289 * Free the inode.
1290 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06001291 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 error = xfs_ifree(tp, ip, &free_list);
1293 if (error) {
1294 /*
1295 * If we fail to free the inode, shut down. The cancel
1296 * might do that, we need to make sure. Otherwise the
1297 * inode might be lost for a long time or forever.
1298 */
1299 if (!XFS_FORCED_SHUTDOWN(mp)) {
1300 cmn_err(CE_NOTE,
1301 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1302 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001303 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1306 } else {
1307 /*
1308 * Credit the quota account(s). The inode is gone.
1309 */
1310 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1311
1312 /*
David Chinner78e9da72008-04-10 12:24:17 +10001313 * Just ignore errors at this point. There is nothing we can
1314 * do except to try to keep going. Make sure it's not a silent
1315 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
David Chinner78e9da72008-04-10 12:24:17 +10001317 error = xfs_bmap_finish(&tp, &free_list, &committed);
1318 if (error)
1319 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1320 "xfs_bmap_finish() returned error %d", error);
1321 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1322 if (error)
1323 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1324 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326 /*
1327 * Release the dquots held by inode, if any.
1328 */
1329 XFS_QM_DQDETACH(mp, ip);
1330
1331 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1332
1333 out:
1334 return VN_INACTIVE_CACHE;
1335}
1336
Barry Naujok384f3ce2008-05-21 16:58:22 +10001337/*
1338 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1339 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1340 * ci_name->name will point to a the actual name (caller must free) or
1341 * will be set to NULL if an exact match is found.
1342 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001343int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001345 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001346 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001347 xfs_inode_t **ipp,
1348 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001350 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int error;
1352 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001354 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1357 return XFS_ERROR(EIO);
1358
1359 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001360 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001362
1363 if (error)
1364 goto out;
1365
1366 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1367 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001368 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001369
1370 xfs_itrace_ref(*ipp);
1371 return 0;
1372
Barry Naujok384f3ce2008-05-21 16:58:22 +10001373out_free_name:
1374 if (ci_name)
1375 kmem_free(ci_name->name);
1376out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001377 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return error;
1379}
1380
Christoph Hellwig993386c2007-08-28 16:12:30 +10001381int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001383 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001384 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001385 mode_t mode,
1386 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001387 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 cred_t *credp)
1389{
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001390 int is_dir = S_ISDIR(mode);
1391 struct xfs_mount *mp = dp->i_mount;
1392 struct xfs_inode *ip = NULL;
1393 struct xfs_trans *tp = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +10001394 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 xfs_bmap_free_t free_list;
1396 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001397 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 uint cancel_flags;
1399 int committed;
1400 xfs_prid_t prid;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001401 struct xfs_dquot *udqp = NULL;
1402 struct xfs_dquot *gdqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 uint resblks;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001404 uint log_res;
1405 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001407 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001409 if (XFS_FORCED_SHUTDOWN(mp))
1410 return XFS_ERROR(EIO);
1411
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001412 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001414 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001415 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001416 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 if (error)
1419 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421
Nathan Scott365ca832005-06-21 15:39:12 +10001422 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1423 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 else
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001425 prid = dfltprid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 /*
1428 * Make sure that we have allocated dquot(s) on disk.
1429 */
1430 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01001431 current_fsuid(), current_fsgid(), prid,
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001432 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (error)
1434 goto std_return;
1435
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001436 if (is_dir) {
1437 rdev = 0;
1438 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1439 log_res = XFS_MKDIR_LOG_RES(mp);
1440 log_count = XFS_MKDIR_LOG_COUNT;
1441 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1442 } else {
1443 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1444 log_res = XFS_CREATE_LOG_RES(mp);
1445 log_count = XFS_CREATE_LOG_COUNT;
1446 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /*
1452 * Initially assume that the file does not exist and
1453 * reserve the resources for that case. If that is not
1454 * the case we'll drop the one we have and get a more
1455 * appropriate transaction later.
1456 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001457 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1458 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (error == ENOSPC) {
Dave Chinner153fec42009-04-06 18:48:30 +02001460 /* flush outstanding delalloc blocks and retry */
1461 xfs_flush_inodes(dp);
1462 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1463 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1464 }
1465 if (error == ENOSPC) {
1466 /* No space at all so try a "no-allocation" reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 resblks = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001468 error = xfs_trans_reserve(tp, 0, log_res, 0,
1469 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471 if (error) {
1472 cancel_flags = 0;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001473 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001476 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001477 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001479 /*
1480 * Check for directory link count overflow.
1481 */
1482 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1483 error = XFS_ERROR(EMLINK);
1484 goto out_trans_cancel;
1485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001487 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 /*
1490 * Reserve disk quota and the inode.
1491 */
1492 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1493 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001494 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Barry Naujok556b8b12008-04-10 12:22:07 +10001496 error = xfs_dir_canenter(tp, dp, name, resblks);
1497 if (error)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001498 goto out_trans_cancel;
1499
1500 /*
1501 * A newly created regular or special file just has one directory
1502 * entry pointing to them, but a directory also the "." entry
1503 * pointing to itself.
1504 */
1505 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1506 prid, resblks > 0, &ip, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (error) {
1508 if (error == ENOSPC)
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001509 goto out_trans_cancel;
1510 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /*
1514 * At this point, we've gotten a newly allocated inode.
1515 * It is locked (and joined to the transaction).
1516 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001517 xfs_itrace_ref(ip);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001518 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001521 * Now we join the directory inode to the transaction. We do not do it
1522 * earlier because xfs_dir_ialloc might commit the previous transaction
1523 * (and release all the locks). An error from here on will result in
1524 * the transaction cancel unlocking dp so don't do it explicitly in the
1525 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001527 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001529 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Barry Naujok556b8b12008-04-10 12:22:07 +10001531 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001532 &first_block, &free_list, resblks ?
1533 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (error) {
1535 ASSERT(error != ENOSPC);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001536 goto out_trans_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1539 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1540
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001541 if (is_dir) {
1542 error = xfs_dir_init(tp, ip, dp);
1543 if (error)
1544 goto out_bmap_cancel;
1545
1546 error = xfs_bumplink(tp, dp);
1547 if (error)
1548 goto out_bmap_cancel;
1549 }
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
1552 * If this is a synchronous mount, make sure that the
1553 * create transaction goes to disk before returning to
1554 * the user.
1555 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001556 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /*
1560 * Attach the dquot(s) to the inodes and modify them incore.
1561 * These ids of the inode couldn't have changed since the new
1562 * inode has been locked ever since it was created.
1563 */
1564 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1565
1566 /*
1567 * xfs_trans_commit normally decrements the vnode ref count
1568 * when it unlocks the inode. Since we want to return the
1569 * vnode to the caller, we bump the vnode ref count now.
1570 */
1571 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001573 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001574 if (error)
1575 goto out_abort_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001577 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (error) {
1579 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001580 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582
1583 XFS_QM_DQRELE(mp, udqp);
1584 XFS_QM_DQRELE(mp, gdqp);
1585
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001586 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /* Fallthrough to std_return with error = 0 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001589 std_return:
1590 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1591 XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
1592 ip, DM_RIGHT_NULL, name->name, NULL, mode,
1593 error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return error;
1597
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001598 out_bmap_cancel:
1599 xfs_bmap_cancel(&free_list);
1600 out_trans_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001602 out_trans_cancel:
1603 xfs_trans_cancel(tp, cancel_flags);
1604 out_dqrele:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 XFS_QM_DQRELE(mp, udqp);
1606 XFS_QM_DQRELE(mp, gdqp);
1607
Christoph Hellwig993386c2007-08-28 16:12:30 +10001608 if (unlock_dp_on_error)
1609 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 goto std_return;
1612
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001613 out_abort_rele:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /*
1615 * Wait until after the current transaction is aborted to
1616 * release the inode. This prevents recursive transactions
1617 * and deadlocks from xfs_inactive.
1618 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001619 xfs_bmap_cancel(&free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 cancel_flags |= XFS_TRANS_ABORT;
1621 xfs_trans_cancel(tp, cancel_flags);
1622 IRELE(ip);
Christoph Hellwig517b5e82009-02-09 08:38:02 +01001623 unlock_dp_on_error = B_FALSE;
1624 goto out_dqrele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628int xfs_locked_n;
1629int xfs_small_retries;
1630int xfs_middle_retries;
1631int xfs_lots_retries;
1632int xfs_lock_delays;
1633#endif
1634
1635/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001636 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1637 * a different value
1638 */
1639static inline int
1640xfs_lock_inumorder(int lock_mode, int subclass)
1641{
1642 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001643 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001644 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001645 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001646
1647 return lock_mode;
1648}
1649
1650/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * The following routine will lock n inodes in exclusive mode.
1652 * We assume the caller calls us with the inodes in i_ino order.
1653 *
1654 * We need to detect deadlock where an inode that we lock
1655 * is in the AIL and we start waiting for another inode that is locked
1656 * by a thread in a long running transaction (such as truncate). This can
1657 * result in deadlock since the long running trans might need to wait
1658 * for the inode we just locked in order to push the tail and free space
1659 * in the log.
1660 */
1661void
1662xfs_lock_inodes(
1663 xfs_inode_t **ips,
1664 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 uint lock_mode)
1666{
1667 int attempts = 0, i, j, try_lock;
1668 xfs_log_item_t *lp;
1669
1670 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1671
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001672 try_lock = 0;
1673 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675again:
1676 for (; i < inodes; i++) {
1677 ASSERT(ips[i]);
1678
1679 if (i && (ips[i] == ips[i-1])) /* Already locked */
1680 continue;
1681
1682 /*
1683 * If try_lock is not set yet, make sure all locked inodes
1684 * are not in the AIL.
1685 * If any are, set try_lock to be used later.
1686 */
1687
1688 if (!try_lock) {
1689 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1690 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1691 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1692 try_lock++;
1693 }
1694 }
1695 }
1696
1697 /*
1698 * If any of the previous locks we have locked is in the AIL,
1699 * we must TRY to get the second and subsequent locks. If
1700 * we can't get any, we must release all we have
1701 * and try again.
1702 */
1703
1704 if (try_lock) {
1705 /* try_lock must be 0 if i is 0. */
1706 /*
1707 * try_lock means we have an inode locked
1708 * that is in the AIL.
1709 */
1710 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001711 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 attempts++;
1713
1714 /*
1715 * Unlock all previous guys and try again.
1716 * xfs_iunlock will try to push the tail
1717 * if the inode is in the AIL.
1718 */
1719
1720 for(j = i - 1; j >= 0; j--) {
1721
1722 /*
1723 * Check to see if we've already
1724 * unlocked this one.
1725 * Not the first one going back,
1726 * and the inode ptr is the same.
1727 */
1728 if ((j != (i - 1)) && ips[j] ==
1729 ips[j+1])
1730 continue;
1731
1732 xfs_iunlock(ips[j], lock_mode);
1733 }
1734
1735 if ((attempts % 5) == 0) {
1736 delay(1); /* Don't just spin the CPU */
1737#ifdef DEBUG
1738 xfs_lock_delays++;
1739#endif
1740 }
1741 i = 0;
1742 try_lock = 0;
1743 goto again;
1744 }
1745 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001746 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748 }
1749
1750#ifdef DEBUG
1751 if (attempts) {
1752 if (attempts < 5) xfs_small_retries++;
1753 else if (attempts < 100) xfs_middle_retries++;
1754 else xfs_lots_retries++;
1755 } else {
1756 xfs_locked_n++;
1757 }
1758#endif
1759}
1760
David Chinnerf9114eb2008-09-17 16:51:21 +10001761/*
1762 * xfs_lock_two_inodes() can only be used to lock one type of lock
1763 * at a time - the iolock or the ilock, but not both at once. If
1764 * we lock both at once, lockdep will report false positives saying
1765 * we have violated locking orders.
1766 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001767void
1768xfs_lock_two_inodes(
1769 xfs_inode_t *ip0,
1770 xfs_inode_t *ip1,
1771 uint lock_mode)
1772{
1773 xfs_inode_t *temp;
1774 int attempts = 0;
1775 xfs_log_item_t *lp;
1776
David Chinnerf9114eb2008-09-17 16:51:21 +10001777 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1778 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001779 ASSERT(ip0->i_ino != ip1->i_ino);
1780
1781 if (ip0->i_ino > ip1->i_ino) {
1782 temp = ip0;
1783 ip0 = ip1;
1784 ip1 = temp;
1785 }
1786
1787 again:
1788 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1789
1790 /*
1791 * If the first lock we have locked is in the AIL, we must TRY to get
1792 * the second lock. If we can't get it, we must release the first one
1793 * and try again.
1794 */
1795 lp = (xfs_log_item_t *)ip0->i_itemp;
1796 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1797 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1798 xfs_iunlock(ip0, lock_mode);
1799 if ((++attempts % 5) == 0)
1800 delay(1); /* Don't just spin the CPU */
1801 goto again;
1802 }
1803 } else {
1804 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1805 }
1806}
1807
Christoph Hellwig993386c2007-08-28 16:12:30 +10001808int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001810 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001811 struct xfs_name *name,
1812 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001814 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001816 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 int error = 0;
1818 xfs_bmap_free_t free_list;
1819 xfs_fsblock_t first_block;
1820 int cancel_flags;
1821 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 int link_zero;
1823 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001824 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001826 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001827 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (XFS_FORCED_SHUTDOWN(mp))
1830 return XFS_ERROR(EIO);
1831
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001832 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001833 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1834 NULL, DM_RIGHT_NULL, name->name, NULL,
1835 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (error)
1837 return error;
1838 }
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 error = XFS_QM_DQATTACH(mp, dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001841 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001844 error = XFS_QM_DQATTACH(mp, ip, 0);
1845 if (error)
1846 goto std_return;
1847
1848 if (is_dir) {
1849 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1850 log_count = XFS_DEFAULT_LOG_COUNT;
1851 } else {
1852 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1853 log_count = XFS_REMOVE_LOG_COUNT;
1854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
1858 * We try to get the real space reservation first,
1859 * allowing for directory btree deletion(s) implying
1860 * possible bmap insert(s). If we can't get the space
1861 * reservation then we use 0 instead, and avoid the bmap
1862 * btree insert(s) in the directory code by, if the bmap
1863 * insert tries to happen, instead trimming the LAST
1864 * block from the directory.
1865 */
1866 resblks = XFS_REMOVE_SPACE_RES(mp);
1867 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001868 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (error == ENOSPC) {
1870 resblks = 0;
1871 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001872 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874 if (error) {
1875 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001876 cancel_flags = 0;
1877 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001880 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 /*
1883 * At this point, we've gotten both the directory and the entry
1884 * inodes locked.
1885 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001886 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10001888
1889 IHOLD(dp);
1890 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001893 * If we're removing a directory perform some additional validation.
1894 */
1895 if (is_dir) {
1896 ASSERT(ip->i_d.di_nlink >= 2);
1897 if (ip->i_d.di_nlink != 2) {
1898 error = XFS_ERROR(ENOTEMPTY);
1899 goto out_trans_cancel;
1900 }
1901 if (!xfs_dir_isempty(ip)) {
1902 error = XFS_ERROR(ENOTEMPTY);
1903 goto out_trans_cancel;
1904 }
1905 }
1906
Eric Sandeen9d87c312009-01-14 23:22:07 -06001907 xfs_bmap_init(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001908 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001909 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (error) {
1911 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001912 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1915
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001916 if (is_dir) {
1917 /*
1918 * Drop the link from ip's "..".
1919 */
1920 error = xfs_droplink(tp, dp);
1921 if (error)
1922 goto out_bmap_cancel;
1923
1924 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001925 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001926 */
1927 error = xfs_droplink(tp, ip);
1928 if (error)
1929 goto out_bmap_cancel;
1930 } else {
1931 /*
1932 * When removing a non-directory we need to log the parent
Dave Chinner26c52952008-11-28 14:23:37 +11001933 * inode here. For a directory this is done implicitly
1934 * by the xfs_droplink call for the ".." entry.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001935 */
1936 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001939 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001940 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001941 */
1942 error = xfs_droplink(tp, ip);
1943 if (error)
1944 goto out_bmap_cancel;
1945
1946 /*
1947 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 * we are in the transaction.
1949 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001950 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 * If this is a synchronous mount, make sure that the
1954 * remove transaction goes to disk before returning to
1955 * the user.
1956 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001957 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001960 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001961 if (error)
1962 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001964 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001965 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10001969 * If we are using filestreams, kill the stream association.
1970 * If the file is still open it may get a new one but that
1971 * will get killed on last close in xfs_close() so we don't
1972 * have to worry about that.
1973 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001974 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10001975 xfs_filestream_deassociate(ip);
1976
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001977 xfs_itrace_exit(ip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001978 xfs_itrace_exit(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001981 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001982 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
1983 NULL, DM_RIGHT_NULL, name->name, NULL,
1984 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return error;
1988
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001989 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 xfs_bmap_cancel(&free_list);
1991 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001992 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 xfs_trans_cancel(tp, cancel_flags);
1994 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
Christoph Hellwig993386c2007-08-28 16:12:30 +10001997int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001999 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002000 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002001 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002003 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 int error;
2006 xfs_bmap_free_t free_list;
2007 xfs_fsblock_t first_block;
2008 int cancel_flags;
2009 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002012 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002013 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002015 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (XFS_FORCED_SHUTDOWN(mp))
2018 return XFS_ERROR(EIO);
2019
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002020 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002022 tdp, DM_RIGHT_NULL,
2023 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002024 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (error)
2026 return error;
2027 }
2028
2029 /* Return through std_return after this point. */
2030
2031 error = XFS_QM_DQATTACH(mp, sip, 0);
Christoph Hellwigcb3f35b2009-02-04 09:34:20 +01002032 if (error)
2033 goto std_return;
2034
2035 error = XFS_QM_DQATTACH(mp, tdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (error)
2037 goto std_return;
2038
2039 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2040 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002041 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2043 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2044 if (error == ENOSPC) {
2045 resblks = 0;
2046 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2047 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2048 }
2049 if (error) {
2050 cancel_flags = 0;
2051 goto error_return;
2052 }
2053
Christoph Hellwige1cccd92008-08-13 16:18:07 +10002054 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 /*
2057 * Increment vnode ref counts since xfs_trans_commit &
2058 * xfs_trans_cancel will both unlock the inodes and
2059 * decrement the associated ref counts.
2060 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002061 IHOLD(sip);
2062 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2064 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2065
2066 /*
2067 * If the source has too many links, we can't make any more to it.
2068 */
2069 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2070 error = XFS_ERROR(EMLINK);
2071 goto error_return;
2072 }
2073
Nathan Scott365ca832005-06-21 15:39:12 +10002074 /*
2075 * If we are using project inheritance, we only allow hard link
2076 * creation in our tree when the project IDs are the same; else
2077 * the tree quota mechanism could be circumvented.
2078 */
2079 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2080 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002081 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002082 goto error_return;
2083 }
2084
Barry Naujok556b8b12008-04-10 12:22:07 +10002085 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2086 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 goto error_return;
2088
Eric Sandeen9d87c312009-01-14 23:22:07 -06002089 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Barry Naujok556b8b12008-04-10 12:22:07 +10002091 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2092 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (error)
2094 goto abort_return;
2095 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2097
2098 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002099 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 /*
2103 * If this is a synchronous mount, make sure that the
2104 * link transaction goes to disk before returning to
2105 * the user.
2106 */
2107 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2108 xfs_trans_set_sync(tp);
2109 }
2110
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002111 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (error) {
2113 xfs_bmap_cancel(&free_list);
2114 goto abort_return;
2115 }
2116
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002117 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002118 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /* Fall through to std_return with error = 0. */
2122std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002123 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002125 tdp, DM_RIGHT_NULL,
2126 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002127 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129 return error;
2130
2131 abort_return:
2132 cancel_flags |= XFS_TRANS_ABORT;
2133 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 error_return:
2136 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 goto std_return;
2138}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002139
Christoph Hellwig993386c2007-08-28 16:12:30 +10002140int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002142 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002143 struct xfs_name *link_name,
2144 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002145 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002146 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 cred_t *credp)
2148{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002149 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 xfs_inode_t *ip;
2152 int error;
2153 int pathlen;
2154 xfs_bmap_free_t free_list;
2155 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002156 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 uint cancel_flags;
2158 int committed;
2159 xfs_fileoff_t first_fsb;
2160 xfs_filblks_t fs_blocks;
2161 int nmaps;
2162 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2163 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002164 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 int byte_cnt;
2166 int n;
2167 xfs_buf_t *bp;
2168 xfs_prid_t prid;
2169 struct xfs_dquot *udqp, *gdqp;
2170 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Christoph Hellwig3937be52008-03-06 13:46:19 +11002172 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 error = 0;
2174 ip = NULL;
2175 tp = NULL;
2176
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002177 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 if (XFS_FORCED_SHUTDOWN(mp))
2180 return XFS_ERROR(EIO);
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 /*
2183 * Check component lengths of the target path name.
2184 */
2185 pathlen = strlen(target_path);
2186 if (pathlen >= MAXPATHLEN) /* total string too long */
2187 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002189 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002190 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002192 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (error)
2194 return error;
2195 }
2196
2197 /* Return through std_return after this point. */
2198
2199 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002200 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2201 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 else
2203 prid = (xfs_prid_t)dfltprid;
2204
2205 /*
2206 * Make sure that we have allocated dquot(s) on disk.
2207 */
2208 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01002209 current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2211 if (error)
2212 goto std_return;
2213
2214 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2215 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2216 /*
2217 * The symlink will fit into the inode data fork?
2218 * There can't be any attributes so we get the whole variable part.
2219 */
2220 if (pathlen <= XFS_LITINO(mp))
2221 fs_blocks = 0;
2222 else
2223 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002224 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2226 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2227 if (error == ENOSPC && fs_blocks == 0) {
2228 resblks = 0;
2229 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2230 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2231 }
2232 if (error) {
2233 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 goto error_return;
2235 }
2236
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002237 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002238 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 /*
2241 * Check whether the directory allows new symlinks or not.
2242 */
2243 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2244 error = XFS_ERROR(EPERM);
2245 goto error_return;
2246 }
2247
2248 /*
2249 * Reserve disk quota : blocks and inode.
2250 */
2251 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2252 if (error)
2253 goto error_return;
2254
2255 /*
2256 * Check for ability to enter directory entry, if no space reserved.
2257 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002258 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2259 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 goto error_return;
2261 /*
2262 * Initialize the bmap freelist prior to calling either
2263 * bmapi or the directory create code.
2264 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002265 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 /*
2268 * Allocate an inode for the symlink.
2269 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002270 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 1, 0, credp, prid, resblks > 0, &ip, NULL);
2272 if (error) {
2273 if (error == ENOSPC)
2274 goto error_return;
2275 goto error1;
2276 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002277 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Christoph Hellwig993386c2007-08-28 16:12:30 +10002279 /*
2280 * An error after we've joined dp to the transaction will result in the
2281 * transaction cancel unlocking dp so don't do it explicitly in the
2282 * error path.
2283 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002284 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002286 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 /*
2289 * Also attach the dquot(s) to it, if applicable.
2290 */
2291 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2292
2293 if (resblks)
2294 resblks -= XFS_IALLOC_SPACE_RES(mp);
2295 /*
2296 * If the symlink will fit into the inode, write it inline.
2297 */
2298 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2299 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2300 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2301 ip->i_d.di_size = pathlen;
2302
2303 /*
2304 * The inode was initially created in extent format.
2305 */
2306 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2307 ip->i_df.if_flags |= XFS_IFINLINE;
2308
2309 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2310 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2311
2312 } else {
2313 first_fsb = 0;
2314 nmaps = SYMLINK_MAPS;
2315
2316 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2317 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2318 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002319 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (error) {
2321 goto error1;
2322 }
2323
2324 if (resblks)
2325 resblks -= fs_blocks;
2326 ip->i_d.di_size = pathlen;
2327 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2328
2329 cur_chunk = target_path;
2330 for (n = 0; n < nmaps; n++) {
2331 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2332 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2333 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2334 BTOBB(byte_cnt), 0);
2335 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2336 if (pathlen < byte_cnt) {
2337 byte_cnt = pathlen;
2338 }
2339 pathlen -= byte_cnt;
2340
2341 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2342 cur_chunk += byte_cnt;
2343
2344 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2345 }
2346 }
2347
2348 /*
2349 * Create the directory entry for the symlink.
2350 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002351 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2352 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002353 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2356 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2357
2358 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 * If this is a synchronous mount, make sure that the
2360 * symlink transaction goes to disk before returning to
2361 * the user.
2362 */
2363 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2364 xfs_trans_set_sync(tp);
2365 }
2366
2367 /*
2368 * xfs_trans_commit normally decrements the vnode ref count
2369 * when it unlocks the inode. Since we want to return the
2370 * vnode to the caller, we bump the vnode ref count now.
2371 */
2372 IHOLD(ip);
2373
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002374 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (error) {
2376 goto error2;
2377 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002378 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 XFS_QM_DQRELE(mp, udqp);
2380 XFS_QM_DQRELE(mp, gdqp);
2381
2382 /* Fall through to std_return with error = 0 or errno from
2383 * xfs_trans_commit */
2384std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10002385 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002387 dp, DM_RIGHT_NULL,
2388 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002389 DM_RIGHT_NULL, link_name->name,
2390 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392
Christoph Hellwig3937be52008-03-06 13:46:19 +11002393 if (!error)
2394 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return error;
2396
2397 error2:
2398 IRELE(ip);
2399 error1:
2400 xfs_bmap_cancel(&free_list);
2401 cancel_flags |= XFS_TRANS_ABORT;
2402 error_return:
2403 xfs_trans_cancel(tp, cancel_flags);
2404 XFS_QM_DQRELE(mp, udqp);
2405 XFS_QM_DQRELE(mp, gdqp);
2406
Christoph Hellwig993386c2007-08-28 16:12:30 +10002407 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 goto std_return;
2411}
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002414xfs_set_dmattrs(
2415 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002417 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002419 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 int error;
2422
2423 if (!capable(CAP_SYS_ADMIN))
2424 return XFS_ERROR(EPERM);
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (XFS_FORCED_SHUTDOWN(mp))
2427 return XFS_ERROR(EIO);
2428
2429 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2430 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2431 if (error) {
2432 xfs_trans_cancel(tp, 0);
2433 return error;
2434 }
2435 xfs_ilock(ip, XFS_ILOCK_EXCL);
2436 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2437
Christoph Hellwig613d7042007-10-11 17:44:08 +10002438 ip->i_d.di_dmevmask = evmask;
2439 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2442 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002443 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 return error;
2446}
2447
Christoph Hellwig993386c2007-08-28 16:12:30 +10002448int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002450 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002453 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002455 ASSERT(!VN_MAPPED(VFS_I(ip)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 /* bad inode, get out here ASAP */
Christoph Hellwigcb4c8cc2009-03-16 08:25:25 +01002458 if (is_bad_inode(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 xfs_ireclaim(ip);
2460 return 0;
2461 }
2462
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002463 xfs_ioend_wait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10002465 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11002467 /*
2468 * Make sure the atime in the XFS inode is correct before freeing the
2469 * Linux inode.
2470 */
2471 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
David Chinner4c606582006-11-11 18:05:00 +11002473 /*
2474 * If we have nothing to flush with this inode then complete the
2475 * teardown now, otherwise break the link between the xfs inode and the
2476 * linux inode and clean up the xfs inode later. This avoids flushing
2477 * the inode to disk during the delete operation itself.
2478 *
2479 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2480 * first to ensure that xfs_iunpin() will never see an xfs inode
2481 * that has a linux inode being reclaimed. Synchronisation is provided
2482 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 */
2484 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2485 xfs_ilock(ip, XFS_ILOCK_EXCL);
2486 xfs_iflock(ip);
David Chinnerbf904242008-10-30 17:36:14 +11002487 xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner1dc33182008-10-30 17:37:15 +11002488 return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
David Chinner11654512008-10-30 17:37:49 +11002490 xfs_inode_set_reclaim_tag(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return 0;
2492}
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494/*
2495 * xfs_alloc_file_space()
2496 * This routine allocates disk space for the given file.
2497 *
2498 * If alloc_type == 0, this request is for an ALLOCSP type
2499 * request which will change the file size. In this case, no
2500 * DMAPI event will be generated by the call. A TRUNCATE event
2501 * will be generated later by xfs_setattr.
2502 *
2503 * If alloc_type != 0, this request is for a RESVSP type
2504 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2505 * lower block boundary byte address is less than the file's
2506 * length.
2507 *
2508 * RETURNS:
2509 * 0 on success
2510 * errno on error
2511 *
2512 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002513STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514xfs_alloc_file_space(
2515 xfs_inode_t *ip,
2516 xfs_off_t offset,
2517 xfs_off_t len,
2518 int alloc_type,
2519 int attr_flags)
2520{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002521 xfs_mount_t *mp = ip->i_mount;
2522 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 xfs_filblks_t allocated_fsb;
2524 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002525 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002527 xfs_fsblock_t firstfsb;
2528 int nimaps;
2529 int bmapi_flag;
2530 int quota_flag;
2531 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002533 xfs_bmbt_irec_t imaps[1], *imapp;
2534 xfs_bmap_free_t free_list;
2535 uint qblocks, resblks, resrtextents;
2536 int committed;
2537 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002539 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 if (XFS_FORCED_SHUTDOWN(mp))
2542 return XFS_ERROR(EIO);
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2545 return error;
2546
2547 if (len <= 0)
2548 return XFS_ERROR(EINVAL);
2549
David Chinner957d0eb2007-06-18 16:50:37 +10002550 rt = XFS_IS_REALTIME_INODE(ip);
2551 extsz = xfs_get_extsz_hint(ip);
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11002555 nimaps = 1;
2556 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2558 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2559
2560 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002561 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002562 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002563 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 xfs_off_t end_dmi_offset;
2565
2566 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002567 if (end_dmi_offset > ip->i_size)
2568 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002569 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2570 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002572 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
2574
2575 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002576 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 */
2578retry:
2579 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002580 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Nathan Scottdd9f4382006-01-11 15:28:28 +11002582 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11002583 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002584 */
2585 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002587 do_div(s, extsz);
2588 s *= extsz;
2589 e = startoffset_fsb + allocatesize_fsb;
2590 if ((temp = do_mod(startoffset_fsb, extsz)))
2591 e += temp;
2592 if ((temp = do_mod(e, extsz)))
2593 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002595 s = 0;
2596 e = allocatesize_fsb;
2597 }
2598
2599 if (unlikely(rt)) {
2600 resrtextents = qblocks = (uint)(e - s);
2601 resrtextents /= mp->m_sb.sb_rextsize;
2602 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2603 quota_flag = XFS_QMOPT_RES_RTBLKS;
2604 } else {
2605 resrtextents = 0;
2606 resblks = qblocks = \
2607 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2608 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610
2611 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002612 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 */
2614 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002615 error = xfs_trans_reserve(tp, resblks,
2616 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 XFS_TRANS_PERM_LOG_RES,
2618 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002620 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 */
2622 if (error) {
2623 /*
2624 * Free the transaction structure.
2625 */
2626 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2627 xfs_trans_cancel(tp, 0);
2628 break;
2629 }
2630 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002631 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
2632 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (error)
2634 goto error1;
2635
2636 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2637 xfs_trans_ihold(tp, ip);
2638
2639 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002640 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002642 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002643 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002644 allocatesize_fsb, bmapi_flag,
2645 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002646 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (error) {
2648 goto error0;
2649 }
2650
2651 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002652 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002654 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (error) {
2656 goto error0;
2657 }
2658
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002659 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2661 if (error) {
2662 break;
2663 }
2664
2665 allocated_fsb = imapp->br_blockcount;
2666
Nathan Scottdd9f4382006-01-11 15:28:28 +11002667 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 error = XFS_ERROR(ENOSPC);
2669 break;
2670 }
2671
2672 startoffset_fsb += allocated_fsb;
2673 allocatesize_fsb -= allocated_fsb;
2674 }
2675dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002676 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002677 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002679 ip, DM_RIGHT_NULL,
2680 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
2682 if (error == 0)
2683 goto retry; /* Maybe DMAPI app. has made space */
2684 /* else fall through with error from XFS_SEND_DATA */
2685 }
2686
2687 return error;
2688
Nathan Scottdd9f4382006-01-11 15:28:28 +11002689error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002691 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
2692
2693error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2695 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2696 goto dmapi_enospc_check;
2697}
2698
2699/*
2700 * Zero file bytes between startoff and endoff inclusive.
2701 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002702 *
2703 * This function is used by xfs_free_file_space() to zero
2704 * partial blocks when the range to free is not block aligned.
2705 * When unreserving space with boundaries that are not block
2706 * aligned we round up the start and round down the end
2707 * boundaries and then use this function to zero the parts of
2708 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 */
2710STATIC int
2711xfs_zero_remaining_bytes(
2712 xfs_inode_t *ip,
2713 xfs_off_t startoff,
2714 xfs_off_t endoff)
2715{
2716 xfs_bmbt_irec_t imap;
2717 xfs_fileoff_t offset_fsb;
2718 xfs_off_t lastoffset;
2719 xfs_off_t offset;
2720 xfs_buf_t *bp;
2721 xfs_mount_t *mp = ip->i_mount;
2722 int nimap;
2723 int error = 0;
2724
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10002725 /*
2726 * Avoid doing I/O beyond eof - it's not necessary
2727 * since nothing can read beyond eof. The space will
2728 * be zeroed when the file is extended anyway.
2729 */
2730 if (startoff >= ip->i_size)
2731 return 0;
2732
2733 if (endoff > ip->i_size)
2734 endoff = ip->i_size;
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002737 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 mp->m_rtdev_targp : mp->m_ddev_targp);
Lachlan McIlroyc6422612008-12-05 13:16:15 +11002739 if (!bp)
2740 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2743 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2744 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002745 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002746 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 if (error || nimap < 1)
2748 break;
2749 ASSERT(imap.br_blockcount >= 1);
2750 ASSERT(imap.br_startoff == offset_fsb);
2751 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2752 if (lastoffset > endoff)
2753 lastoffset = endoff;
2754 if (imap.br_startblock == HOLESTARTBLOCK)
2755 continue;
2756 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2757 if (imap.br_state == XFS_EXT_UNWRITTEN)
2758 continue;
2759 XFS_BUF_UNDONE(bp);
2760 XFS_BUF_UNWRITE(bp);
2761 XFS_BUF_READ(bp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002762 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002764 error = xfs_iowait(bp);
2765 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2767 mp, bp, XFS_BUF_ADDR(bp));
2768 break;
2769 }
2770 memset(XFS_BUF_PTR(bp) +
2771 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2772 0, lastoffset - offset + 1);
2773 XFS_BUF_UNDONE(bp);
2774 XFS_BUF_UNREAD(bp);
2775 XFS_BUF_WRITE(bp);
2776 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10002777 error = xfs_iowait(bp);
2778 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2780 mp, bp, XFS_BUF_ADDR(bp));
2781 break;
2782 }
2783 }
2784 xfs_buf_free(bp);
2785 return error;
2786}
2787
2788/*
2789 * xfs_free_file_space()
2790 * This routine frees disk space for the given file.
2791 *
2792 * This routine is only called by xfs_change_file_space
2793 * for an UNRESVSP type call.
2794 *
2795 * RETURNS:
2796 * 0 on success
2797 * errno on error
2798 *
2799 */
2800STATIC int
2801xfs_free_file_space(
2802 xfs_inode_t *ip,
2803 xfs_off_t offset,
2804 xfs_off_t len,
2805 int attr_flags)
2806{
2807 int committed;
2808 int done;
2809 xfs_off_t end_dmi_offset;
2810 xfs_fileoff_t endoffset_fsb;
2811 int error;
2812 xfs_fsblock_t firstfsb;
2813 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 xfs_bmbt_irec_t imap;
2815 xfs_off_t ioffset;
2816 xfs_extlen_t mod=0;
2817 xfs_mount_t *mp;
2818 int nimap;
2819 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10002820 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 int rt;
2822 xfs_fileoff_t startoffset_fsb;
2823 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002824 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 mp = ip->i_mount;
2827
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002828 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2831 return error;
2832
2833 error = 0;
2834 if (len <= 0) /* if nothing being freed */
2835 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002836 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2838 end_dmi_offset = offset + len;
2839 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
2840
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002841 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002842 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002843 if (end_dmi_offset > ip->i_size)
2844 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002845 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 offset, end_dmi_offset - offset,
2847 AT_DELAY_FLAG(attr_flags), NULL);
2848 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002849 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
2851
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002852 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002853 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11002854 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig25e41b32008-12-03 12:20:39 +01002856 /* wait for the completion of any pending DIOs */
2857 xfs_ioend_wait(ip);
Yingping Lu9fa80462006-03-22 12:44:35 +11002858 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07002859
Tim Shimmine6a4b372007-11-23 16:30:42 +11002860 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002862
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002863 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002864 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
2865 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10002866 if (error)
2867 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10002868 }
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 /*
2871 * Need to zero the stuff we're not freeing, on disk.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002872 * If it's a realtime file & can't use unwritten extents then we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2874 * will take care of it for us.
2875 */
Eric Sandeen62118702008-03-06 13:44:28 +11002876 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002878 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002879 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (error)
2881 goto out_unlock_iolock;
2882 ASSERT(nimap == 0 || nimap == 1);
2883 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2884 xfs_daddr_t block;
2885
2886 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2887 block = imap.br_startblock;
2888 mod = do_div(block, mp->m_sb.sb_rextsize);
2889 if (mod)
2890 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2891 }
2892 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002893 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002894 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (error)
2896 goto out_unlock_iolock;
2897 ASSERT(nimap == 0 || nimap == 1);
2898 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2899 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2900 mod++;
2901 if (mod && (mod != mp->m_sb.sb_rextsize))
2902 endoffset_fsb -= mod;
2903 }
2904 }
2905 if ((done = (endoffset_fsb <= startoffset_fsb)))
2906 /*
2907 * One contiguous piece to clear
2908 */
2909 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2910 else {
2911 /*
2912 * Some full blocks, possibly two pieces to clear
2913 */
2914 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2915 error = xfs_zero_remaining_bytes(ip, offset,
2916 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2917 if (!error &&
2918 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2919 error = xfs_zero_remaining_bytes(ip,
2920 XFS_FSB_TO_B(mp, endoffset_fsb),
2921 offset + len - 1);
2922 }
2923
2924 /*
2925 * free file space until done or until there is an error
2926 */
2927 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2928 while (!error && !done) {
2929
2930 /*
David Chinner91ebecc2007-07-19 16:28:30 +10002931 * allocate and setup the transaction. Allow this
2932 * transaction to dip into the reserve blocks to ensure
2933 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 */
2935 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10002936 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 error = xfs_trans_reserve(tp,
2938 resblks,
2939 XFS_WRITE_LOG_RES(mp),
2940 0,
2941 XFS_TRANS_PERM_LOG_RES,
2942 XFS_WRITE_LOG_COUNT);
2943
2944 /*
2945 * check for running out of space
2946 */
2947 if (error) {
2948 /*
2949 * Free the transaction structure.
2950 */
2951 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2952 xfs_trans_cancel(tp, 0);
2953 break;
2954 }
2955 xfs_ilock(ip, XFS_ILOCK_EXCL);
2956 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002957 ip->i_udquot, ip->i_gdquot, resblks, 0,
2958 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (error)
2960 goto error1;
2961
2962 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2963 xfs_trans_ihold(tp, ip);
2964
2965 /*
2966 * issue the bunmapi() call to free the blocks
2967 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002968 xfs_bmap_init(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002969 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002971 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (error) {
2973 goto error0;
2974 }
2975
2976 /*
2977 * complete the transaction
2978 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002979 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (error) {
2981 goto error0;
2982 }
2983
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002984 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2986 }
2987
2988 out_unlock_iolock:
2989 if (need_iolock)
2990 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2991 return error;
2992
2993 error0:
2994 xfs_bmap_cancel(&free_list);
2995 error1:
2996 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2997 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2998 XFS_ILOCK_EXCL);
2999 return error;
3000}
3001
3002/*
3003 * xfs_change_file_space()
3004 * This routine allocates or frees disk space for the given file.
3005 * The user specified parameters are checked for alignment and size
3006 * limitations.
3007 *
3008 * RETURNS:
3009 * 0 on success
3010 * errno on error
3011 *
3012 */
3013int
3014xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003015 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 int cmd,
3017 xfs_flock64_t *bf,
3018 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 int attr_flags)
3020{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003021 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 int clrprealloc;
3023 int error;
3024 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 int setprealloc;
3026 xfs_off_t startoffset;
3027 xfs_off_t llen;
3028 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003029 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003031 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Christoph Hellwig993386c2007-08-28 16:12:30 +10003033 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return XFS_ERROR(EINVAL);
3035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 switch (bf->l_whence) {
3037 case 0: /*SEEK_SET*/
3038 break;
3039 case 1: /*SEEK_CUR*/
3040 bf->l_start += offset;
3041 break;
3042 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003043 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 break;
3045 default:
3046 return XFS_ERROR(EINVAL);
3047 }
3048
3049 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3050
3051 if ( (bf->l_start < 0)
3052 || (bf->l_start > XFS_MAXIOFFSET(mp))
3053 || (bf->l_start + llen < 0)
3054 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3055 return XFS_ERROR(EINVAL);
3056
3057 bf->l_whence = 0;
3058
3059 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003060 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 /*
3063 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3064 * file space.
3065 * These calls do NOT zero the data space allocated to the file,
3066 * nor do they change the file size.
3067 *
3068 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3069 * space.
3070 * These calls cause the new file data to be zeroed and the file
3071 * size to be changed.
3072 */
3073 setprealloc = clrprealloc = 0;
3074
3075 switch (cmd) {
3076 case XFS_IOC_RESVSP:
3077 case XFS_IOC_RESVSP64:
3078 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3079 1, attr_flags);
3080 if (error)
3081 return error;
3082 setprealloc = 1;
3083 break;
3084
3085 case XFS_IOC_UNRESVSP:
3086 case XFS_IOC_UNRESVSP64:
3087 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3088 attr_flags)))
3089 return error;
3090 break;
3091
3092 case XFS_IOC_ALLOCSP:
3093 case XFS_IOC_ALLOCSP64:
3094 case XFS_IOC_FREESP:
3095 case XFS_IOC_FREESP64:
3096 if (startoffset > fsize) {
3097 error = xfs_alloc_file_space(ip, fsize,
3098 startoffset - fsize, 0, attr_flags);
3099 if (error)
3100 break;
3101 }
3102
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003103 iattr.ia_valid = ATTR_SIZE;
3104 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +11003106 error = xfs_setattr(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108 if (error)
3109 return error;
3110
3111 clrprealloc = 1;
3112 break;
3113
3114 default:
3115 ASSERT(0);
3116 return XFS_ERROR(EINVAL);
3117 }
3118
3119 /*
3120 * update the inode timestamp, mode, and prealloc flag bits
3121 */
3122 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3123
3124 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3125 0, 0, 0))) {
3126 /* ASSERT(0); */
3127 xfs_trans_cancel(tp, 0);
3128 return error;
3129 }
3130
3131 xfs_ilock(ip, XFS_ILOCK_EXCL);
3132
3133 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3134 xfs_trans_ihold(tp, ip);
3135
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003136 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 ip->i_d.di_mode &= ~S_ISUID;
3138
3139 /*
3140 * Note that we don't have to worry about mandatory
3141 * file locking being disabled here because we only
3142 * clear the S_ISGID bit if the Group execute bit is
3143 * on, but if it was on then mandatory locking wouldn't
3144 * have been enabled.
3145 */
3146 if (ip->i_d.di_mode & S_IXGRP)
3147 ip->i_d.di_mode &= ~S_ISGID;
3148
3149 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3150 }
3151 if (setprealloc)
3152 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3153 else if (clrprealloc)
3154 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3155
3156 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3157 xfs_trans_set_sync(tp);
3158
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003159 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3162
3163 return error;
3164}