blob: f26b038004a74ab433b1cefb27e00d7b9a712601 [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;
73 int file_owner;
Dean Roehrich5fcbab32005-05-05 13:27:19 -070074 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Lachlan McIlroycf441ee2008-02-07 16:42:19 +110076 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Christoph Hellwigbd186aa2007-08-30 17:21:12 +100078 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return XFS_ERROR(EROFS);
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (XFS_FORCED_SHUTDOWN(mp))
82 return XFS_ERROR(EIO);
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 olddquot1 = olddquot2 = NULL;
85 udqp = gdqp = NULL;
86
87 /*
88 * If disk quotas is on, we make sure that the dquots do exist on disk,
89 * before we start any other transactions. Trying to do this later
90 * is messy. We don't care to take a readlock to look at the ids
91 * in inode here, because we can't hold it across the trans_reserve.
92 * If the IDs do change before we take the ilock, we're covered
93 * because the i_*dquot fields will get updated anyway.
94 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +100095 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 uint qflags = 0;
97
Christoph Hellwig0f285c82008-07-18 17:13:28 +100098 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
99 uid = iattr->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 qflags |= XFS_QMOPT_UQUOTA;
101 } else {
102 uid = ip->i_d.di_uid;
103 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000104 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
105 gid = iattr->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 qflags |= XFS_QMOPT_GQUOTA;
107 } else {
108 gid = ip->i_d.di_gid;
109 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /*
112 * We take a reference when we initialize udqp and gdqp,
113 * so it is important that we never blindly double trip on
114 * the same variable. See xfs_create() for an example.
115 */
116 ASSERT(udqp == NULL);
117 ASSERT(gdqp == NULL);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000118 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, ip->i_d.di_projid,
119 qflags, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100121 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
124 /*
125 * For the other attributes, we acquire the inode lock and
126 * first do an error checking pass.
127 */
128 tp = NULL;
129 lock_flags = XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000130 if (flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700131 need_iolock = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000132 if (!(mask & ATTR_SIZE)) {
133 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 (mp->m_flags & XFS_MOUNT_WSYNC)) {
135 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
136 commit_flags = 0;
137 if ((code = xfs_trans_reserve(tp, 0,
138 XFS_ICHANGE_LOG_RES(mp), 0,
139 0, 0))) {
140 lock_flags = 0;
141 goto error_return;
142 }
143 }
144 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000145 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000146 !(flags & XFS_ATTR_DMI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100148 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000149 iattr->ia_size, 0, dmflags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (code) {
151 lock_flags = 0;
152 goto error_return;
153 }
154 }
155 if (need_iolock)
156 lock_flags |= XFS_IOLOCK_EXCL;
157 }
158
159 xfs_ilock(ip, lock_flags);
160
161 /* boolean: are we the file owner? */
David Howells9e2b2dc2008-08-13 16:20:04 +0100162 file_owner = (current_fsuid() == ip->i_d.di_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /*
165 * Change various properties of a file.
166 * Only the owner or users with CAP_FOWNER
167 * capability may do these things.
168 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000169 if (mask & (ATTR_MODE|ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /*
171 * CAP_FOWNER overrides the following restrictions:
172 *
173 * The user ID of the calling process must be equal
174 * to the file owner ID, except in cases where the
175 * CAP_FSETID capability is applicable.
176 */
177 if (!file_owner && !capable(CAP_FOWNER)) {
178 code = XFS_ERROR(EPERM);
179 goto error_return;
180 }
181
182 /*
183 * CAP_FSETID overrides the following restrictions:
184 *
185 * The effective user ID of the calling process shall match
186 * the file owner when setting the set-user-ID and
187 * set-group-ID bits on that file.
188 *
189 * The effective group ID or one of the supplementary group
190 * IDs of the calling process shall match the group owner of
191 * the file when setting the set-group-ID bit on that file
192 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000193 if (mask & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 mode_t m = 0;
195
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000196 if ((iattr->ia_mode & S_ISUID) && !file_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 m |= S_ISUID;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000198 if ((iattr->ia_mode & S_ISGID) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 !in_group_p((gid_t)ip->i_d.di_gid))
200 m |= S_ISGID;
201#if 0
202 /* Linux allows this, Irix doesn't. */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000203 if ((iattr->ia_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 m |= S_ISVTX;
205#endif
206 if (m && !capable(CAP_FSETID))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000207 iattr->ia_mode &= ~m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 }
210
211 /*
212 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000214 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /*
216 * These IDs could have changed since we last looked at them.
217 * But, we're assured that if the ownership did change
218 * while we didn't have the inode locked, inode's dquot(s)
219 * would have changed also.
220 */
221 iuid = ip->i_d.di_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 igid = ip->i_d.di_gid;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000223 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
224 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /*
227 * CAP_CHOWN overrides the following restrictions:
228 *
229 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
230 * shall override the restriction that a process cannot
231 * change the user ID of a file it owns and the restriction
232 * that the group ID supplied to the chown() function
233 * shall be equal to either the group ID or one of the
234 * supplementary group IDs of the calling process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
Tim Shimmine0b8e8b2008-10-30 18:30:48 +1100236 if ((iuid != uid ||
237 (igid != gid && !in_group_p((gid_t)gid))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 !capable(CAP_CHOWN)) {
239 code = XFS_ERROR(EPERM);
240 goto error_return;
241 }
242 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000243 * Do a quota reservation only if uid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * going to change.
245 */
246 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
247 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
248 ASSERT(tp);
249 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
250 capable(CAP_FOWNER) ?
251 XFS_QMOPT_FORCE_RES : 0);
252 if (code) /* out of quota */
253 goto error_return;
254 }
255 }
256
257 /*
258 * Truncate file. Must have write permission and not be a directory.
259 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000260 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* Short circuit the truncate case for zero length files */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000262 if (iattr->ia_size == 0 &&
263 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 xfs_iunlock(ip, XFS_ILOCK_EXCL);
265 lock_flags &= ~XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000266 if (mask & ATTR_CTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
268 code = 0;
269 goto error_return;
270 }
271
Christoph Hellwig42173f62008-04-22 17:33:25 +1000272 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 code = XFS_ERROR(EISDIR);
274 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000275 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 code = XFS_ERROR(EINVAL);
277 goto error_return;
278 }
279 /*
280 * Make sure that the dquots are attached to the inode.
281 */
282 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
283 goto error_return;
284 }
285
286 /*
287 * Change file access or modified times.
288 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000289 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (!file_owner) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000291 if ((mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 !capable(CAP_FOWNER)) {
293 code = XFS_ERROR(EPERM);
294 goto error_return;
295 }
296 }
297 }
298
299 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * Now we can make the changes. Before we join the inode
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000301 * to the transaction, if ATTR_SIZE is set then take care of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * the part of the truncation that must be done without the
303 * inode lock. This needs to be done before joining the inode
304 * to the transaction, because the inode cannot be unlocked
305 * once it is a part of the transaction.
306 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000307 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 code = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000309 if (iattr->ia_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000310 /*
311 * Do the first part of growing a file: zero any data
312 * in the last block that is beyond the old EOF. We
313 * need to do this before the inode is joined to the
314 * transaction to modify the i_size.
315 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000316 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000319
320 /*
321 * We are going to log the inode size change in this
322 * transaction so any previous writes that are beyond the on
323 * disk EOF and the new EOF that have not been written out need
324 * to be written here. If we do not write the data out, we
325 * expose ourselves to the null files problem.
326 *
327 * Only flush from the on disk size to the smaller of the in
328 * memory file size or the new size as that's the range we
329 * really care about here and prevents waiting for other data
330 * not within the range we care about here.
331 */
332 if (!code &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000333 ip->i_size != ip->i_d.di_size &&
334 iattr->ia_size > ip->i_d.di_size) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000335 code = xfs_flush_pages(ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000336 ip->i_d.di_size, iattr->ia_size,
David Chinnerc32676e2007-07-19 16:28:58 +1000337 XFS_B_ASYNC, FI_NONE);
338 }
339
340 /* wait for all I/O to complete */
Christoph Hellwigb677c212007-08-29 11:46:28 +1000341 vn_iowait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (!code)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000344 code = xfs_itruncate_data(ip, iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (code) {
346 ASSERT(tp == NULL);
347 lock_flags &= ~XFS_ILOCK_EXCL;
348 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
349 goto error_return;
350 }
351 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
352 if ((code = xfs_trans_reserve(tp, 0,
353 XFS_ITRUNCATE_LOG_RES(mp), 0,
354 XFS_TRANS_PERM_LOG_RES,
355 XFS_ITRUNCATE_LOG_COUNT))) {
356 xfs_trans_cancel(tp, 0);
357 if (need_iolock)
358 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
359 return code;
360 }
361 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
362 xfs_ilock(ip, XFS_ILOCK_EXCL);
363 }
364
365 if (tp) {
366 xfs_trans_ijoin(tp, ip, lock_flags);
367 xfs_trans_ihold(tp, ip);
368 }
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
371 * Truncate file. Must have write permission and not be a directory.
372 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000373 if (mask & ATTR_SIZE) {
David Chinner44d814c2008-03-06 13:45:29 +1100374 /*
375 * Only change the c/mtime if we are changing the size
376 * or we are explicitly asked to change it. This handles
377 * the semantic difference between truncate() and ftruncate()
378 * as implemented in the VFS.
379 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000380 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
David Chinner44d814c2008-03-06 13:45:29 +1100381 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
382
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000383 if (iattr->ia_size > ip->i_size) {
384 ip->i_d.di_size = iattr->ia_size;
385 ip->i_size = iattr->ia_size;
386 if (!(flags & XFS_ATTR_DMI))
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000387 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
388 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000389 } else if (iattr->ia_size <= ip->i_size ||
390 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
392 * signal a sync transaction unless
393 * we're truncating an already unlinked
394 * file on a wsync filesystem
395 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000396 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 XFS_DATA_FORK,
398 ((ip->i_d.di_nlink != 0 ||
399 !(mp->m_flags & XFS_MOUNT_WSYNC))
400 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000401 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000403 /*
404 * Truncated "down", so we're removing references
405 * to old data here - if we now delay flushing for
406 * a long time, we expose ourselves unduly to the
407 * notorious NULL files problem. So, we mark this
408 * vnode and flush it when the file is closed, and
409 * do not wait the usual (long) time for writeout.
410 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000411 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 /*
416 * Change file access modes.
417 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000418 if (mask & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 ip->i_d.di_mode &= S_IFMT;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000420 ip->i_d.di_mode |= iattr->ia_mode & ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000422 inode->i_mode &= S_IFMT;
423 inode->i_mode |= iattr->ia_mode & ~S_IFMT;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
426 timeflags |= XFS_ICHGTIME_CHG;
427 }
428
429 /*
430 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000432 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /*
434 * CAP_FSETID overrides the following restrictions:
435 *
436 * The set-user-ID and set-group-ID bits of a file will be
437 * cleared upon successful return from chown()
438 */
439 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
440 !capable(CAP_FSETID)) {
441 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
442 }
443
444 /*
445 * Change the ownerships and register quota modifications
446 * in the transaction.
447 */
448 if (iuid != uid) {
449 if (XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000450 ASSERT(mask & ATTR_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 ASSERT(udqp);
452 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
453 &ip->i_udquot, udqp);
454 }
455 ip->i_d.di_uid = uid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000456 inode->i_uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 if (igid != gid) {
459 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000460 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000461 ASSERT(mask & ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 ASSERT(gdqp);
463 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
464 &ip->i_gdquot, gdqp);
465 }
466 ip->i_d.di_gid = gid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000467 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
471 timeflags |= XFS_ICHGTIME_CHG;
472 }
473
474
475 /*
476 * Change file access or modified times.
477 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000478 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
479 if (mask & ATTR_ATIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000480 inode->i_atime = iattr->ia_atime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000481 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
482 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ip->i_update_core = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000485 if (mask & ATTR_MTIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000486 inode->i_mtime = iattr->ia_mtime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000487 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
488 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 timeflags &= ~XFS_ICHGTIME_MOD;
490 timeflags |= XFS_ICHGTIME_CHG;
491 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000492 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
494 }
495
496 /*
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000497 * Change file inode change time only if ATTR_CTIME set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * AND we have been called by a DMI function.
499 */
500
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000501 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000502 inode->i_ctime = iattr->ia_ctime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000503 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
504 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ip->i_update_core = 1;
506 timeflags &= ~XFS_ICHGTIME_CHG;
507 }
508
509 /*
510 * Send out timestamp changes that need to be set to the
511 * current time. Not done when called by a DMI function.
512 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000513 if (timeflags && !(flags & XFS_ATTR_DMI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 xfs_ichgtime(ip, timeflags);
515
516 XFS_STATS_INC(xs_ig_attrchg);
517
518 /*
519 * If this is a synchronous mount, make sure that the
520 * transaction goes to disk before returning to the user.
521 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000522 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * One for the truncate and one for the timestamps since we
524 * don't want to change the timestamps unless we're sure the
525 * truncate worked. Truncates are less than 1% of the laddis
526 * mix so this probably isn't worth the trouble to optimize.
527 */
528 code = 0;
529 if (tp) {
530 if (mp->m_flags & XFS_MOUNT_WSYNC)
531 xfs_trans_set_sync(tp);
532
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000533 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 xfs_iunlock(ip, lock_flags);
537
538 /*
539 * Release any dquot(s) the inode had kept before chown.
540 */
541 XFS_QM_DQRELE(mp, olddquot1);
542 XFS_QM_DQRELE(mp, olddquot2);
543 XFS_QM_DQRELE(mp, udqp);
544 XFS_QM_DQRELE(mp, gdqp);
545
546 if (code) {
547 return code;
548 }
549
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000550 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000551 !(flags & XFS_ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100552 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 NULL, DM_RIGHT_NULL, NULL, NULL,
554 0, 0, AT_DELAY_FLAG(flags));
555 }
556 return 0;
557
558 abort_return:
559 commit_flags |= XFS_TRANS_ABORT;
560 /* FALLTHROUGH */
561 error_return:
562 XFS_QM_DQRELE(mp, udqp);
563 XFS_QM_DQRELE(mp, gdqp);
564 if (tp) {
565 xfs_trans_cancel(tp, commit_flags);
566 }
567 if (lock_flags != 0) {
568 xfs_iunlock(ip, lock_flags);
569 }
570 return code;
571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000574 * The maximum pathlen is 1024 bytes. Since the minimum file system
575 * blocksize is 512 bytes, we can get a max of 2 extents back from
576 * bmapi.
577 */
578#define SYMLINK_MAPS 2
579
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000580STATIC int
581xfs_readlink_bmap(
582 xfs_inode_t *ip,
583 char *link)
584{
585 xfs_mount_t *mp = ip->i_mount;
586 int pathlen = ip->i_d.di_size;
587 int nmaps = SYMLINK_MAPS;
588 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
589 xfs_daddr_t d;
590 int byte_cnt;
591 int n;
592 xfs_buf_t *bp;
593 int error = 0;
594
595 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
596 mval, &nmaps, NULL, NULL);
597 if (error)
598 goto out;
599
600 for (n = 0; n < nmaps; n++) {
601 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
602 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
603
604 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
605 error = XFS_BUF_GETERROR(bp);
606 if (error) {
607 xfs_ioerror_alert("xfs_readlink",
608 ip->i_mount, bp, XFS_BUF_ADDR(bp));
609 xfs_buf_relse(bp);
610 goto out;
611 }
612 if (pathlen < byte_cnt)
613 byte_cnt = pathlen;
614 pathlen -= byte_cnt;
615
616 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
617 xfs_buf_relse(bp);
618 }
619
620 link[ip->i_d.di_size] = '\0';
621 error = 0;
622
623 out:
624 return error;
625}
626
Christoph Hellwig993386c2007-08-28 16:12:30 +1000627int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000629 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000630 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000632 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100636 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (XFS_FORCED_SHUTDOWN(mp))
639 return XFS_ERROR(EIO);
640
641 xfs_ilock(ip, XFS_ILOCK_SHARED);
642
643 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000644 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000646 pathlen = ip->i_d.di_size;
647 if (!pathlen)
648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000651 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
652 link[pathlen] = '\0';
653 } else {
654 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000657 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return error;
660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/*
663 * xfs_fsync
664 *
David Chinner978b7232008-05-19 16:29:46 +1000665 * This is called to sync the inode and its data out to disk. We need to hold
666 * the I/O lock while flushing the data, and the inode lock while flushing the
667 * inode. The inode lock CANNOT be held while flushing the data, so acquire
668 * after we're done with that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000670int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671xfs_fsync(
David Chinner978b7232008-05-19 16:29:46 +1000672 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 xfs_trans_t *tp;
675 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100676 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100678 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
681 return XFS_ERROR(EIO);
682
David Chinner978b7232008-05-19 16:29:46 +1000683 /* capture size updates in I/O completion before writing the inode. */
Dave Chinner2e656092008-11-28 14:23:33 +1100684 error = xfs_wait_on_pages(ip, 0, -1);
David Chinner978b7232008-05-19 16:29:46 +1000685 if (error)
686 return XFS_ERROR(error);
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +1000687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
David Chinner978b7232008-05-19 16:29:46 +1000689 * We always need to make sure that the required inode state is safe on
690 * disk. The vnode might be clean but we still might need to force the
691 * log because of committed transactions that haven't hit the disk yet.
692 * Likewise, there could be unflushed non-transactional changes to the
693 * inode core that have to go to disk and this requires us to issue
694 * a synchronous transaction to capture these changes correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
David Chinner978b7232008-05-19 16:29:46 +1000696 * This code relies on the assumption that if the update_* fields
697 * of the inode are clear and the inode is unpinned then it is clean
698 * and no action is required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
700 xfs_ilock(ip, XFS_ILOCK_SHARED);
701
David Chinner978b7232008-05-19 16:29:46 +1000702 if (!(ip->i_update_size || ip->i_update_core)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /*
David Chinner978b7232008-05-19 16:29:46 +1000704 * Timestamps/size haven't changed since last inode flush or
705 * inode transaction commit. That means either nothing got
706 * written or a transaction committed which caught the updates.
707 * If the latter happened and the transaction hasn't hit the
708 * disk yet, the inode will be still be pinned. If it is,
709 * force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
711
712 xfs_iunlock(ip, XFS_ILOCK_SHARED);
713
714 if (xfs_ipincount(ip)) {
David Chinner978b7232008-05-19 16:29:46 +1000715 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
716 XFS_LOG_FORCE | XFS_LOG_SYNC,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100717 &log_flushed);
718 } else {
719 /*
David Chinner978b7232008-05-19 16:29:46 +1000720 * If the inode is not pinned and nothing has changed
721 * we don't need to flush the cache.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100722 */
723 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 } else {
726 /*
David Chinner978b7232008-05-19 16:29:46 +1000727 * Kick off a transaction to log the inode core to get the
728 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
730 xfs_iunlock(ip, XFS_ILOCK_SHARED);
731 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000732 error = xfs_trans_reserve(tp, 0,
733 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
734 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 xfs_trans_cancel(tp, 0);
736 return error;
737 }
738 xfs_ilock(ip, XFS_ILOCK_EXCL);
739
740 /*
David Chinner978b7232008-05-19 16:29:46 +1000741 * Note - it's possible that we might have pushed ourselves out
742 * of the way during trans_reserve which would flush the inode.
743 * But there's no guarantee that the inode buffer has actually
744 * gone out yet (it's delwri). Plus the buffer could be pinned
745 * anyway if it's part of an inode in another recent
746 * transaction. So we play it safe and fire off the
747 * transaction anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
749 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
750 xfs_trans_ihold(tp, ip);
751 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
David Chinner978b7232008-05-19 16:29:46 +1000752 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000753 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 xfs_iunlock(ip, XFS_ILOCK_EXCL);
756 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100757
758 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
759 /*
760 * If the log write didn't issue an ordered tag we need
761 * to flush the disk cache for the data device now.
762 */
763 if (!log_flushed)
764 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
765
766 /*
767 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000768 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100769 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100770 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100771 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
772 }
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return error;
775}
776
777/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000778 * This is called by xfs_inactive to free any blocks beyond eof
779 * when the link count isn't zero and by xfs_dm_punch_hole() when
780 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000782int
783xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000785 xfs_inode_t *ip,
786 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 xfs_trans_t *tp;
789 int error;
790 xfs_fileoff_t end_fsb;
791 xfs_fileoff_t last_fsb;
792 xfs_filblks_t map_len;
793 int nimaps;
794 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +1000795 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /*
798 * Figure out if there are any blocks beyond the end
799 * of the file. If not, then there is nothing to do.
800 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000801 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
803 map_len = last_fsb - end_fsb;
804 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 nimaps = 1;
808 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000809 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000810 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 xfs_iunlock(ip, XFS_ILOCK_SHARED);
812
813 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100814 (imap.br_startblock != HOLESTARTBLOCK ||
815 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * Attach the dquots to the inode up front.
818 */
819 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +0100820 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /*
823 * There are blocks after the end of file.
824 * Free them up now by truncating the file to
825 * its current size.
826 */
827 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
828
829 /*
830 * Do the xfs_itruncate_start() call before
831 * reserving any log space because
832 * itruncate_start will call into the buffer
833 * cache and we can't
834 * do that within a transaction.
835 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000836 if (use_iolock)
837 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000838 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000839 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000840 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000841 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +1000842 if (use_iolock)
843 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000844 return error;
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 error = xfs_trans_reserve(tp, 0,
848 XFS_ITRUNCATE_LOG_RES(mp),
849 0, XFS_TRANS_PERM_LOG_RES,
850 XFS_ITRUNCATE_LOG_COUNT);
851 if (error) {
852 ASSERT(XFS_FORCED_SHUTDOWN(mp));
853 xfs_trans_cancel(tp, 0);
854 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100855 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
858 xfs_ilock(ip, XFS_ILOCK_EXCL);
859 xfs_trans_ijoin(tp, ip,
860 XFS_IOLOCK_EXCL |
861 XFS_ILOCK_EXCL);
862 xfs_trans_ihold(tp, ip);
863
864 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000865 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 XFS_DATA_FORK,
867 0);
868 /*
869 * If we get an error at this point we
870 * simply don't bother truncating the file.
871 */
872 if (error) {
873 xfs_trans_cancel(tp,
874 (XFS_TRANS_RELEASE_LOG_RES |
875 XFS_TRANS_ABORT));
876 } else {
877 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000878 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
David Chinner92dfe8d2007-05-24 15:22:19 +1000880 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
881 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100883 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886/*
887 * Free a symlink that has blocks associated with it.
888 */
889STATIC int
890xfs_inactive_symlink_rmt(
891 xfs_inode_t *ip,
892 xfs_trans_t **tpp)
893{
894 xfs_buf_t *bp;
895 int committed;
896 int done;
897 int error;
898 xfs_fsblock_t first_block;
899 xfs_bmap_free_t free_list;
900 int i;
901 xfs_mount_t *mp;
902 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
903 int nmaps;
904 xfs_trans_t *ntp;
905 int size;
906 xfs_trans_t *tp;
907
908 tp = *tpp;
909 mp = ip->i_mount;
910 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
911 /*
912 * We're freeing a symlink that has some
913 * blocks allocated to it. Free the
914 * blocks here. We know that we've got
915 * either 1 or 2 extents and that we can
916 * free them all in one bunmapi call.
917 */
918 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
919 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
920 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
921 ASSERT(XFS_FORCED_SHUTDOWN(mp));
922 xfs_trans_cancel(tp, 0);
923 *tpp = NULL;
924 return error;
925 }
926 /*
927 * Lock the inode, fix the size, and join it to the transaction.
928 * Hold it so in the normal path, we still have it locked for
929 * the second transaction. In the error paths we need it
930 * held so the cancel won't rele it, see below.
931 */
932 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
933 size = (int)ip->i_d.di_size;
934 ip->i_d.di_size = 0;
935 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
936 xfs_trans_ihold(tp, ip);
937 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
938 /*
939 * Find the block(s) so we can inval and unmap them.
940 */
941 done = 0;
942 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800943 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
945 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000946 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto error0;
948 /*
949 * Invalidate the block(s).
950 */
951 for (i = 0; i < nmaps; i++) {
952 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
953 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
954 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
955 xfs_trans_binval(tp, bp);
956 }
957 /*
958 * Unmap the dead block(s) to the free_list.
959 */
960 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000961 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 goto error1;
963 ASSERT(done);
964 /*
965 * Commit the first transaction. This logs the EFI and the inode.
966 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100967 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 goto error1;
969 /*
970 * The transaction must have been committed, since there were
971 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
972 * The new tp has the extent freeing and EFDs.
973 */
974 ASSERT(committed);
975 /*
976 * The first xact was committed, so add the inode to the new one.
977 * Mark it dirty so it will be logged and moved forward in the log as
978 * part of every commit.
979 */
980 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
981 xfs_trans_ihold(tp, ip);
982 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
983 /*
984 * Get a new, empty transaction to return to our caller.
985 */
986 ntp = xfs_trans_dup(tp);
987 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000988 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * If we get an error on the commit here or on the reserve below,
990 * we need to unlock the inode since the new transaction doesn't
991 * have the inode attached.
992 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000993 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 tp = ntp;
995 if (error) {
996 ASSERT(XFS_FORCED_SHUTDOWN(mp));
997 goto error0;
998 }
999 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001000 * transaction commit worked ok so we can drop the extra ticket
1001 * reference that we gained in xfs_trans_dup()
1002 */
1003 xfs_log_ticket_put(tp->t_ticket);
1004
1005 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * Remove the memory for extent descriptions (just bookkeeping).
1007 */
1008 if (ip->i_df.if_bytes)
1009 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1010 ASSERT(ip->i_df.if_bytes == 0);
1011 /*
1012 * Put an itruncate log reservation in the new transaction
1013 * for our caller.
1014 */
1015 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1016 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1017 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1018 goto error0;
1019 }
1020 /*
1021 * Return with the inode locked but not joined to the transaction.
1022 */
1023 *tpp = tp;
1024 return 0;
1025
1026 error1:
1027 xfs_bmap_cancel(&free_list);
1028 error0:
1029 /*
1030 * Have to come here with the inode locked and either
1031 * (held and in the transaction) or (not in the transaction).
1032 * If the inode isn't held then cancel would iput it, but
1033 * that's wrong since this is inactive and the vnode ref
1034 * count is 0 already.
1035 * Cancel won't do anything to the inode if held, but it still
1036 * needs to be locked until the cancel is done, if it was
1037 * joined to the transaction.
1038 */
1039 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1040 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1041 *tpp = NULL;
1042 return error;
1043
1044}
1045
1046STATIC int
1047xfs_inactive_symlink_local(
1048 xfs_inode_t *ip,
1049 xfs_trans_t **tpp)
1050{
1051 int error;
1052
1053 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1054 /*
1055 * We're freeing a symlink which fit into
1056 * the inode. Just free the memory used
1057 * to hold the old symlink.
1058 */
1059 error = xfs_trans_reserve(*tpp, 0,
1060 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1061 0, XFS_TRANS_PERM_LOG_RES,
1062 XFS_ITRUNCATE_LOG_COUNT);
1063
1064 if (error) {
1065 xfs_trans_cancel(*tpp, 0);
1066 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001067 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1070
1071 /*
1072 * Zero length symlinks _can_ exist.
1073 */
1074 if (ip->i_df.if_bytes > 0) {
1075 xfs_idata_realloc(ip,
1076 -(ip->i_df.if_bytes),
1077 XFS_DATA_FORK);
1078 ASSERT(ip->i_df.if_bytes == 0);
1079 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083STATIC int
1084xfs_inactive_attrs(
1085 xfs_inode_t *ip,
1086 xfs_trans_t **tpp)
1087{
1088 xfs_trans_t *tp;
1089 int error;
1090 xfs_mount_t *mp;
1091
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001092 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 tp = *tpp;
1094 mp = ip->i_mount;
1095 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001096 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001098 if (error)
1099 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001102 if (error)
1103 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1106 error = xfs_trans_reserve(tp, 0,
1107 XFS_IFREE_LOG_RES(mp),
1108 0, XFS_TRANS_PERM_LOG_RES,
1109 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001110 if (error)
1111 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 xfs_ilock(ip, XFS_ILOCK_EXCL);
1114 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1115 xfs_trans_ihold(tp, ip);
1116 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1117
1118 ASSERT(ip->i_d.di_anextents == 0);
1119
1120 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001121 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001122
1123error_cancel:
1124 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1125 xfs_trans_cancel(tp, 0);
1126error_unlock:
1127 *tpp = NULL;
1128 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1129 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Christoph Hellwig993386c2007-08-28 16:12:30 +10001132int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001134 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001136 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int error;
1138
Christoph Hellwig42173f62008-04-22 17:33:25 +10001139 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001143 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return 0;
1145
David Chinner2a82b8b2007-07-11 11:09:12 +10001146 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001147 int truncated;
1148
David Chinner2a82b8b2007-07-11 11:09:12 +10001149 /*
1150 * If we are using filestreams, and we have an unlinked
1151 * file that we are processing the last close on, then nothing
1152 * will be able to reopen and write to this file. Purge this
1153 * inode from the filestreams cache so that it doesn't delay
1154 * teardown of the inode.
1155 */
1156 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1157 xfs_filestream_deassociate(ip);
1158
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001159 /*
1160 * If we previously truncated this file and removed old data
1161 * in the process, we want to initiate "early" writeout on
1162 * the last close. This is an attempt to combat the notorious
1163 * NULL files problem which is particularly noticable from a
1164 * truncate down, buffered (re-)write (delalloc), followed by
1165 * a crash. What we are effectively doing here is
1166 * significantly reducing the time window where we'd otherwise
1167 * be exposed to that problem.
1168 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001169 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001170 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001171 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001172 }
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (ip->i_d.di_nlink != 0) {
1175 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001176 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001177 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001179 (!(ip->i_d.di_flags &
1180 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001181 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1182 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001183 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185 }
1186
1187 return 0;
1188}
1189
1190/*
1191 * xfs_inactive
1192 *
1193 * This is called when the vnode reference count for the vnode
1194 * goes to zero. If the file has been unlinked, then it must
1195 * now be truncated. Also, we clear all of the read-ahead state
1196 * kept for the inode here since the file is now closed.
1197 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001198int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001200 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001202 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 xfs_fsblock_t first_block;
1204 int committed;
1205 xfs_trans_t *tp;
1206 xfs_mount_t *mp;
1207 int error;
1208 int truncate;
1209
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001210 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /*
1213 * If the inode is already free, then there can be nothing
1214 * to clean up here.
1215 */
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001216 if (ip->i_d.di_mode == 0 || VN_BAD(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 ASSERT(ip->i_df.if_real_bytes == 0);
1218 ASSERT(ip->i_df.if_broot_bytes == 0);
1219 return VN_INACTIVE_CACHE;
1220 }
1221
1222 /*
1223 * Only do a truncate if it's a regular file with
1224 * some actual space in it. It's OK to look at the
1225 * inode's fields without the lock because we're the
1226 * only one with a reference to the inode.
1227 */
1228 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001229 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1230 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1232
1233 mp = ip->i_mount;
1234
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001235 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1236 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 error = 0;
1239
1240 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001241 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 goto out;
1243
1244 if (ip->i_d.di_nlink != 0) {
1245 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001246 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001247 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001248 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1249 (!(ip->i_d.di_flags &
1250 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1251 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001252 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1253 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001254 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256 goto out;
1257 }
1258
1259 ASSERT(ip->i_d.di_nlink == 0);
1260
1261 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001262 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1265 if (truncate) {
1266 /*
1267 * Do the xfs_itruncate_start() call before
1268 * reserving any log space because itruncate_start
1269 * will call into the buffer cache and we can't
1270 * do that within a transaction.
1271 */
1272 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1273
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001274 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1275 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001276 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001277 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1278 return VN_INACTIVE_CACHE;
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 error = xfs_trans_reserve(tp, 0,
1282 XFS_ITRUNCATE_LOG_RES(mp),
1283 0, XFS_TRANS_PERM_LOG_RES,
1284 XFS_ITRUNCATE_LOG_COUNT);
1285 if (error) {
1286 /* Don't call itruncate_cleanup */
1287 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1288 xfs_trans_cancel(tp, 0);
1289 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001290 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292
1293 xfs_ilock(ip, XFS_ILOCK_EXCL);
1294 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1295 xfs_trans_ihold(tp, ip);
1296
1297 /*
1298 * normally, we have to run xfs_itruncate_finish sync.
1299 * But if filesystem is wsync and we're in the inactive
1300 * path, then we know that nlink == 0, and that the
1301 * xaction that made nlink == 0 is permanently committed
1302 * since xfs_remove runs as a synchronous transaction.
1303 */
1304 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1305 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1306
1307 if (error) {
1308 xfs_trans_cancel(tp,
1309 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1310 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001311 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1314
1315 /*
1316 * If we get an error while cleaning up a
1317 * symlink we bail out.
1318 */
1319 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1320 xfs_inactive_symlink_rmt(ip, &tp) :
1321 xfs_inactive_symlink_local(ip, &tp);
1322
1323 if (error) {
1324 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001325 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
1328 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1329 xfs_trans_ihold(tp, ip);
1330 } else {
1331 error = xfs_trans_reserve(tp, 0,
1332 XFS_IFREE_LOG_RES(mp),
1333 0, XFS_TRANS_PERM_LOG_RES,
1334 XFS_INACTIVE_LOG_COUNT);
1335 if (error) {
1336 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1337 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001338 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340
1341 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1342 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1343 xfs_trans_ihold(tp, ip);
1344 }
1345
1346 /*
1347 * If there are attributes associated with the file
1348 * then blow them away now. The code calls a routine
1349 * that recursively deconstructs the attribute fork.
1350 * We need to just commit the current transaction
1351 * because we can't use it for xfs_attr_inactive().
1352 */
1353 if (ip->i_d.di_anextents > 0) {
1354 error = xfs_inactive_attrs(ip, &tp);
1355 /*
1356 * If we got an error, the transaction is already
1357 * cancelled, and the inode is unlocked. Just get out.
1358 */
1359 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001360 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 } else if (ip->i_afp) {
1362 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1363 }
1364
1365 /*
1366 * Free the inode.
1367 */
1368 XFS_BMAP_INIT(&free_list, &first_block);
1369 error = xfs_ifree(tp, ip, &free_list);
1370 if (error) {
1371 /*
1372 * If we fail to free the inode, shut down. The cancel
1373 * might do that, we need to make sure. Otherwise the
1374 * inode might be lost for a long time or forever.
1375 */
1376 if (!XFS_FORCED_SHUTDOWN(mp)) {
1377 cmn_err(CE_NOTE,
1378 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1379 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001380 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1383 } else {
1384 /*
1385 * Credit the quota account(s). The inode is gone.
1386 */
1387 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1388
1389 /*
David Chinner78e9da72008-04-10 12:24:17 +10001390 * Just ignore errors at this point. There is nothing we can
1391 * do except to try to keep going. Make sure it's not a silent
1392 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 */
David Chinner78e9da72008-04-10 12:24:17 +10001394 error = xfs_bmap_finish(&tp, &free_list, &committed);
1395 if (error)
1396 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1397 "xfs_bmap_finish() returned error %d", error);
1398 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1399 if (error)
1400 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1401 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 /*
1404 * Release the dquots held by inode, if any.
1405 */
1406 XFS_QM_DQDETACH(mp, ip);
1407
1408 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1409
1410 out:
1411 return VN_INACTIVE_CACHE;
1412}
1413
Barry Naujok384f3ce2008-05-21 16:58:22 +10001414/*
1415 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1416 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1417 * ci_name->name will point to a the actual name (caller must free) or
1418 * will be set to NULL if an exact match is found.
1419 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001420int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001422 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001423 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001424 xfs_inode_t **ipp,
1425 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001427 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 int error;
1429 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001431 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1434 return XFS_ERROR(EIO);
1435
1436 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001437 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001439
1440 if (error)
1441 goto out;
1442
1443 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1444 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001445 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001446
1447 xfs_itrace_ref(*ipp);
1448 return 0;
1449
Barry Naujok384f3ce2008-05-21 16:58:22 +10001450out_free_name:
1451 if (ci_name)
1452 kmem_free(ci_name->name);
1453out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001454 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return error;
1456}
1457
Christoph Hellwig993386c2007-08-28 16:12:30 +10001458int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001460 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001461 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001462 mode_t mode,
1463 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001464 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 cred_t *credp)
1466{
Barry Naujok556b8b12008-04-10 12:22:07 +10001467 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001468 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 xfs_trans_t *tp;
Barry Naujok556b8b12008-04-10 12:22:07 +10001470 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 xfs_bmap_free_t free_list;
1472 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001473 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int dm_event_sent = 0;
1475 uint cancel_flags;
1476 int committed;
1477 xfs_prid_t prid;
1478 struct xfs_dquot *udqp, *gdqp;
1479 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001481 ASSERT(!*ipp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001482 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001484 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001486 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001487 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001488 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 if (error)
1491 return error;
1492 dm_event_sent = 1;
1493 }
1494
1495 if (XFS_FORCED_SHUTDOWN(mp))
1496 return XFS_ERROR(EIO);
1497
1498 /* Return through std_return after this point. */
1499
1500 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001501 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1502 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 else
1504 prid = (xfs_prid_t)dfltprid;
1505
1506 /*
1507 * Make sure that we have allocated dquot(s) on disk.
1508 */
1509 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01001510 current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1512 if (error)
1513 goto std_return;
1514
1515 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1518 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001519 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /*
1521 * Initially assume that the file does not exist and
1522 * reserve the resources for that case. If that is not
1523 * the case we'll drop the one we have and get a more
1524 * appropriate transaction later.
1525 */
1526 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1527 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1528 if (error == ENOSPC) {
1529 resblks = 0;
1530 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1531 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1532 }
1533 if (error) {
1534 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 goto error_return;
1536 }
1537
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001538 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001539 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 XFS_BMAP_INIT(&free_list, &first_block);
1542
1543 ASSERT(ip == NULL);
1544
1545 /*
1546 * Reserve disk quota and the inode.
1547 */
1548 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1549 if (error)
1550 goto error_return;
1551
Barry Naujok556b8b12008-04-10 12:22:07 +10001552 error = xfs_dir_canenter(tp, dp, name, resblks);
1553 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001555 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 rdev, credp, prid, resblks > 0,
1557 &ip, &committed);
1558 if (error) {
1559 if (error == ENOSPC)
1560 goto error_return;
1561 goto abort_return;
1562 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001563 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 /*
1566 * At this point, we've gotten a newly allocated inode.
1567 * It is locked (and joined to the transaction).
1568 */
1569
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001570 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001573 * Now we join the directory inode to the transaction. We do not do it
1574 * earlier because xfs_dir_ialloc might commit the previous transaction
1575 * (and release all the locks). An error from here on will result in
1576 * the transaction cancel unlocking dp so don't do it explicitly in the
1577 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001579 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001581 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Barry Naujok556b8b12008-04-10 12:22:07 +10001583 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001584 &first_block, &free_list, resblks ?
1585 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (error) {
1587 ASSERT(error != ENOSPC);
1588 goto abort_return;
1589 }
1590 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1591 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1592
1593 /*
1594 * If this is a synchronous mount, make sure that the
1595 * create transaction goes to disk before returning to
1596 * the user.
1597 */
1598 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1599 xfs_trans_set_sync(tp);
1600 }
1601
1602 dp->i_gen++;
1603
1604 /*
1605 * Attach the dquot(s) to the inodes and modify them incore.
1606 * These ids of the inode couldn't have changed since the new
1607 * inode has been locked ever since it was created.
1608 */
1609 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1610
1611 /*
1612 * xfs_trans_commit normally decrements the vnode ref count
1613 * when it unlocks the inode. Since we want to return the
1614 * vnode to the caller, we bump the vnode ref count now.
1615 */
1616 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001618 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (error) {
1620 xfs_bmap_cancel(&free_list);
1621 goto abort_rele;
1622 }
1623
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001624 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (error) {
1626 IRELE(ip);
1627 tp = NULL;
1628 goto error_return;
1629 }
1630
1631 XFS_QM_DQRELE(mp, udqp);
1632 XFS_QM_DQRELE(mp, gdqp);
1633
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001634 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 /* Fallthrough to std_return with error = 0 */
1637
1638std_return:
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001639 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10001640 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001642 dp, DM_RIGHT_NULL,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001643 *ipp ? ip : NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001644 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001645 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647 return error;
1648
1649 abort_return:
1650 cancel_flags |= XFS_TRANS_ABORT;
1651 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Jesper Juhl014c2542006-01-15 02:37:08 +01001653 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (tp != NULL)
1655 xfs_trans_cancel(tp, cancel_flags);
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 XFS_QM_DQRELE(mp, udqp);
1658 XFS_QM_DQRELE(mp, gdqp);
1659
Christoph Hellwig993386c2007-08-28 16:12:30 +10001660 if (unlock_dp_on_error)
1661 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 goto std_return;
1664
1665 abort_rele:
1666 /*
1667 * Wait until after the current transaction is aborted to
1668 * release the inode. This prevents recursive transactions
1669 * and deadlocks from xfs_inactive.
1670 */
1671 cancel_flags |= XFS_TRANS_ABORT;
1672 xfs_trans_cancel(tp, cancel_flags);
1673 IRELE(ip);
1674
1675 XFS_QM_DQRELE(mp, udqp);
1676 XFS_QM_DQRELE(mp, gdqp);
1677
1678 goto std_return;
1679}
1680
1681#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682int xfs_locked_n;
1683int xfs_small_retries;
1684int xfs_middle_retries;
1685int xfs_lots_retries;
1686int xfs_lock_delays;
1687#endif
1688
1689/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001690 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1691 * a different value
1692 */
1693static inline int
1694xfs_lock_inumorder(int lock_mode, int subclass)
1695{
1696 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001697 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001698 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001699 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001700
1701 return lock_mode;
1702}
1703
1704/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 * The following routine will lock n inodes in exclusive mode.
1706 * We assume the caller calls us with the inodes in i_ino order.
1707 *
1708 * We need to detect deadlock where an inode that we lock
1709 * is in the AIL and we start waiting for another inode that is locked
1710 * by a thread in a long running transaction (such as truncate). This can
1711 * result in deadlock since the long running trans might need to wait
1712 * for the inode we just locked in order to push the tail and free space
1713 * in the log.
1714 */
1715void
1716xfs_lock_inodes(
1717 xfs_inode_t **ips,
1718 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 uint lock_mode)
1720{
1721 int attempts = 0, i, j, try_lock;
1722 xfs_log_item_t *lp;
1723
1724 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1725
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001726 try_lock = 0;
1727 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729again:
1730 for (; i < inodes; i++) {
1731 ASSERT(ips[i]);
1732
1733 if (i && (ips[i] == ips[i-1])) /* Already locked */
1734 continue;
1735
1736 /*
1737 * If try_lock is not set yet, make sure all locked inodes
1738 * are not in the AIL.
1739 * If any are, set try_lock to be used later.
1740 */
1741
1742 if (!try_lock) {
1743 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1744 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1745 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1746 try_lock++;
1747 }
1748 }
1749 }
1750
1751 /*
1752 * If any of the previous locks we have locked is in the AIL,
1753 * we must TRY to get the second and subsequent locks. If
1754 * we can't get any, we must release all we have
1755 * and try again.
1756 */
1757
1758 if (try_lock) {
1759 /* try_lock must be 0 if i is 0. */
1760 /*
1761 * try_lock means we have an inode locked
1762 * that is in the AIL.
1763 */
1764 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001765 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 attempts++;
1767
1768 /*
1769 * Unlock all previous guys and try again.
1770 * xfs_iunlock will try to push the tail
1771 * if the inode is in the AIL.
1772 */
1773
1774 for(j = i - 1; j >= 0; j--) {
1775
1776 /*
1777 * Check to see if we've already
1778 * unlocked this one.
1779 * Not the first one going back,
1780 * and the inode ptr is the same.
1781 */
1782 if ((j != (i - 1)) && ips[j] ==
1783 ips[j+1])
1784 continue;
1785
1786 xfs_iunlock(ips[j], lock_mode);
1787 }
1788
1789 if ((attempts % 5) == 0) {
1790 delay(1); /* Don't just spin the CPU */
1791#ifdef DEBUG
1792 xfs_lock_delays++;
1793#endif
1794 }
1795 i = 0;
1796 try_lock = 0;
1797 goto again;
1798 }
1799 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001800 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802 }
1803
1804#ifdef DEBUG
1805 if (attempts) {
1806 if (attempts < 5) xfs_small_retries++;
1807 else if (attempts < 100) xfs_middle_retries++;
1808 else xfs_lots_retries++;
1809 } else {
1810 xfs_locked_n++;
1811 }
1812#endif
1813}
1814
David Chinnerf9114eb2008-09-17 16:51:21 +10001815/*
1816 * xfs_lock_two_inodes() can only be used to lock one type of lock
1817 * at a time - the iolock or the ilock, but not both at once. If
1818 * we lock both at once, lockdep will report false positives saying
1819 * we have violated locking orders.
1820 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001821void
1822xfs_lock_two_inodes(
1823 xfs_inode_t *ip0,
1824 xfs_inode_t *ip1,
1825 uint lock_mode)
1826{
1827 xfs_inode_t *temp;
1828 int attempts = 0;
1829 xfs_log_item_t *lp;
1830
David Chinnerf9114eb2008-09-17 16:51:21 +10001831 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1832 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001833 ASSERT(ip0->i_ino != ip1->i_ino);
1834
1835 if (ip0->i_ino > ip1->i_ino) {
1836 temp = ip0;
1837 ip0 = ip1;
1838 ip1 = temp;
1839 }
1840
1841 again:
1842 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1843
1844 /*
1845 * If the first lock we have locked is in the AIL, we must TRY to get
1846 * the second lock. If we can't get it, we must release the first one
1847 * and try again.
1848 */
1849 lp = (xfs_log_item_t *)ip0->i_itemp;
1850 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1851 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1852 xfs_iunlock(ip0, lock_mode);
1853 if ((++attempts % 5) == 0)
1854 delay(1); /* Don't just spin the CPU */
1855 goto again;
1856 }
1857 } else {
1858 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1859 }
1860}
1861
Christoph Hellwig993386c2007-08-28 16:12:30 +10001862int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001864 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001865 struct xfs_name *name,
1866 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001868 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001870 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 int error = 0;
1872 xfs_bmap_free_t free_list;
1873 xfs_fsblock_t first_block;
1874 int cancel_flags;
1875 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 int link_zero;
1877 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001878 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001880 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001881 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (XFS_FORCED_SHUTDOWN(mp))
1884 return XFS_ERROR(EIO);
1885
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001886 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001887 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1888 NULL, DM_RIGHT_NULL, name->name, NULL,
1889 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (error)
1891 return error;
1892 }
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 error = XFS_QM_DQATTACH(mp, dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001895 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001898 error = XFS_QM_DQATTACH(mp, ip, 0);
1899 if (error)
1900 goto std_return;
1901
1902 if (is_dir) {
1903 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1904 log_count = XFS_DEFAULT_LOG_COUNT;
1905 } else {
1906 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1907 log_count = XFS_REMOVE_LOG_COUNT;
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 /*
1912 * We try to get the real space reservation first,
1913 * allowing for directory btree deletion(s) implying
1914 * possible bmap insert(s). If we can't get the space
1915 * reservation then we use 0 instead, and avoid the bmap
1916 * btree insert(s) in the directory code by, if the bmap
1917 * insert tries to happen, instead trimming the LAST
1918 * block from the directory.
1919 */
1920 resblks = XFS_REMOVE_SPACE_RES(mp);
1921 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001922 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (error == ENOSPC) {
1924 resblks = 0;
1925 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001926 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928 if (error) {
1929 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001930 cancel_flags = 0;
1931 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001934 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 /*
1937 * At this point, we've gotten both the directory and the entry
1938 * inodes locked.
1939 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001940 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10001942
1943 IHOLD(dp);
1944 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001947 * If we're removing a directory perform some additional validation.
1948 */
1949 if (is_dir) {
1950 ASSERT(ip->i_d.di_nlink >= 2);
1951 if (ip->i_d.di_nlink != 2) {
1952 error = XFS_ERROR(ENOTEMPTY);
1953 goto out_trans_cancel;
1954 }
1955 if (!xfs_dir_isempty(ip)) {
1956 error = XFS_ERROR(ENOTEMPTY);
1957 goto out_trans_cancel;
1958 }
1959 }
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 XFS_BMAP_INIT(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001962 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001963 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (error) {
1965 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001966 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1969
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001970 /*
1971 * Bump the in memory generation count on the parent
1972 * directory so that other can know that it has changed.
1973 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 dp->i_gen++;
1975 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1976
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001977 if (is_dir) {
1978 /*
1979 * Drop the link from ip's "..".
1980 */
1981 error = xfs_droplink(tp, dp);
1982 if (error)
1983 goto out_bmap_cancel;
1984
1985 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11001986 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001987 */
1988 error = xfs_droplink(tp, ip);
1989 if (error)
1990 goto out_bmap_cancel;
1991 } else {
1992 /*
1993 * When removing a non-directory we need to log the parent
1994 * inode here for the i_gen update. For a directory this is
1995 * done implicitly by the xfs_droplink call for the ".." entry.
1996 */
1997 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002000 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11002001 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002002 */
2003 error = xfs_droplink(tp, ip);
2004 if (error)
2005 goto out_bmap_cancel;
2006
2007 /*
2008 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 * we are in the transaction.
2010 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002011 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 * If this is a synchronous mount, make sure that the
2015 * remove transaction goes to disk before returning to
2016 * the user.
2017 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002018 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002021 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002022 if (error)
2023 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002025 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002026 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002030 * If we are using filestreams, kill the stream association.
2031 * If the file is still open it may get a new one but that
2032 * will get killed on last close in xfs_close() so we don't
2033 * have to worry about that.
2034 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002035 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10002036 xfs_filestream_deassociate(ip);
2037
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002038 xfs_itrace_exit(ip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002039 xfs_itrace_exit(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002042 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002043 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2044 NULL, DM_RIGHT_NULL, name->name, NULL,
2045 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return error;
2049
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002050 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 xfs_bmap_cancel(&free_list);
2052 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002053 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 xfs_trans_cancel(tp, cancel_flags);
2055 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Christoph Hellwig993386c2007-08-28 16:12:30 +10002058int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002060 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002061 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002062 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002064 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 int error;
2067 xfs_bmap_free_t free_list;
2068 xfs_fsblock_t first_block;
2069 int cancel_flags;
2070 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002073 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002074 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002076 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (XFS_FORCED_SHUTDOWN(mp))
2079 return XFS_ERROR(EIO);
2080
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002081 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002083 tdp, DM_RIGHT_NULL,
2084 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002085 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (error)
2087 return error;
2088 }
2089
2090 /* Return through std_return after this point. */
2091
2092 error = XFS_QM_DQATTACH(mp, sip, 0);
2093 if (!error && sip != tdp)
2094 error = XFS_QM_DQATTACH(mp, tdp, 0);
2095 if (error)
2096 goto std_return;
2097
2098 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2099 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002100 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2102 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2103 if (error == ENOSPC) {
2104 resblks = 0;
2105 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2106 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2107 }
2108 if (error) {
2109 cancel_flags = 0;
2110 goto error_return;
2111 }
2112
Christoph Hellwige1cccd92008-08-13 16:18:07 +10002113 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 /*
2116 * Increment vnode ref counts since xfs_trans_commit &
2117 * xfs_trans_cancel will both unlock the inodes and
2118 * decrement the associated ref counts.
2119 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002120 IHOLD(sip);
2121 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2123 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2124
2125 /*
2126 * If the source has too many links, we can't make any more to it.
2127 */
2128 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2129 error = XFS_ERROR(EMLINK);
2130 goto error_return;
2131 }
2132
Nathan Scott365ca832005-06-21 15:39:12 +10002133 /*
2134 * If we are using project inheritance, we only allow hard link
2135 * creation in our tree when the project IDs are the same; else
2136 * the tree quota mechanism could be circumvented.
2137 */
2138 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2139 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002140 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002141 goto error_return;
2142 }
2143
Barry Naujok556b8b12008-04-10 12:22:07 +10002144 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2145 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 goto error_return;
2147
2148 XFS_BMAP_INIT(&free_list, &first_block);
2149
Barry Naujok556b8b12008-04-10 12:22:07 +10002150 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2151 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (error)
2153 goto abort_return;
2154 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2155 tdp->i_gen++;
2156 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2157
2158 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002159 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 /*
2163 * If this is a synchronous mount, make sure that the
2164 * link transaction goes to disk before returning to
2165 * the user.
2166 */
2167 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2168 xfs_trans_set_sync(tp);
2169 }
2170
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002171 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (error) {
2173 xfs_bmap_cancel(&free_list);
2174 goto abort_return;
2175 }
2176
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002177 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002178 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 /* Fall through to std_return with error = 0. */
2182std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002183 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002185 tdp, DM_RIGHT_NULL,
2186 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002187 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189 return error;
2190
2191 abort_return:
2192 cancel_flags |= XFS_TRANS_ABORT;
2193 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 error_return:
2196 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto std_return;
2198}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002199
2200
Christoph Hellwig993386c2007-08-28 16:12:30 +10002201int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002203 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002204 struct xfs_name *dir_name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002205 mode_t mode,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002206 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 cred_t *credp)
2208{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002209 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 xfs_inode_t *cdp; /* inode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 int cancel_flags;
2213 int error;
2214 int committed;
2215 xfs_bmap_free_t free_list;
2216 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002217 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 boolean_t created = B_FALSE;
2219 int dm_event_sent = 0;
2220 xfs_prid_t prid;
2221 struct xfs_dquot *udqp, *gdqp;
2222 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 if (XFS_FORCED_SHUTDOWN(mp))
2225 return XFS_ERROR(EIO);
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002229 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002231 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002232 DM_RIGHT_NULL, dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002233 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (error)
2235 return error;
2236 dm_event_sent = 1;
2237 }
2238
2239 /* Return through std_return after this point. */
2240
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002241 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 mp = dp->i_mount;
2244 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002245 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2246 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 else
2248 prid = (xfs_prid_t)dfltprid;
2249
2250 /*
2251 * Make sure that we have allocated dquot(s) on disk.
2252 */
2253 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01002254 current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2256 if (error)
2257 goto std_return;
2258
2259 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2260 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002261 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2263 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2264 if (error == ENOSPC) {
2265 resblks = 0;
2266 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2267 XFS_TRANS_PERM_LOG_RES,
2268 XFS_MKDIR_LOG_COUNT);
2269 }
2270 if (error) {
2271 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 goto error_return;
2273 }
2274
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002275 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002276 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /*
2279 * Check for directory link count overflow.
2280 */
2281 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2282 error = XFS_ERROR(EMLINK);
2283 goto error_return;
2284 }
2285
2286 /*
2287 * Reserve disk quota and the inode.
2288 */
2289 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2290 if (error)
2291 goto error_return;
2292
Barry Naujok556b8b12008-04-10 12:22:07 +10002293 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2294 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto error_return;
2296 /*
2297 * create the directory inode.
2298 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002299 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 0, credp, prid, resblks > 0,
2301 &cdp, NULL);
2302 if (error) {
2303 if (error == ENOSPC)
2304 goto error_return;
2305 goto abort_return;
2306 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002307 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 /*
2310 * Now we add the directory inode to the transaction.
2311 * We waited until now since xfs_dir_ialloc might start
2312 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002313 * earlier, the locks might have gotten released. An error
2314 * from here on will result in the transaction cancel
2315 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002317 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002319 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 XFS_BMAP_INIT(&free_list, &first_block);
2322
Barry Naujok556b8b12008-04-10 12:22:07 +10002323 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2324 &first_block, &free_list, resblks ?
2325 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (error) {
2327 ASSERT(error != ENOSPC);
2328 goto error1;
2329 }
2330 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2331
2332 /*
2333 * Bump the in memory version number of the parent directory
2334 * so that other processes accessing it will recognize that
2335 * the directory has changed.
2336 */
2337 dp->i_gen++;
2338
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002339 error = xfs_dir_init(tp, cdp, dp);
2340 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 cdp->i_gen = 1;
2344 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002345 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 created = B_TRUE;
2349
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002350 *ipp = cdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 IHOLD(cdp);
2352
2353 /*
2354 * Attach the dquots to the new inode and modify the icount incore.
2355 */
2356 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2357
2358 /*
2359 * If this is a synchronous mount, make sure that the
2360 * mkdir 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
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002367 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (error) {
2369 IRELE(cdp);
2370 goto error2;
2371 }
2372
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002373 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 XFS_QM_DQRELE(mp, udqp);
2375 XFS_QM_DQRELE(mp, gdqp);
2376 if (error) {
2377 IRELE(cdp);
2378 }
2379
2380 /* Fall through to std_return with error = 0 or errno from
2381 * xfs_trans_commit. */
2382
2383std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002384 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002385 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002387 dp, DM_RIGHT_NULL,
2388 created ? cdp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002390 dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002391 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 return error;
2394
2395 error2:
2396 error1:
2397 xfs_bmap_cancel(&free_list);
2398 abort_return:
2399 cancel_flags |= XFS_TRANS_ABORT;
2400 error_return:
2401 xfs_trans_cancel(tp, cancel_flags);
2402 XFS_QM_DQRELE(mp, udqp);
2403 XFS_QM_DQRELE(mp, gdqp);
2404
Christoph Hellwig993386c2007-08-28 16:12:30 +10002405 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 goto std_return;
2409}
2410
Christoph Hellwig993386c2007-08-28 16:12:30 +10002411int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002413 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002414 struct xfs_name *link_name,
2415 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002416 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002417 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 cred_t *credp)
2419{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002420 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 xfs_inode_t *ip;
2423 int error;
2424 int pathlen;
2425 xfs_bmap_free_t free_list;
2426 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002427 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 uint cancel_flags;
2429 int committed;
2430 xfs_fileoff_t first_fsb;
2431 xfs_filblks_t fs_blocks;
2432 int nmaps;
2433 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2434 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002435 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int byte_cnt;
2437 int n;
2438 xfs_buf_t *bp;
2439 xfs_prid_t prid;
2440 struct xfs_dquot *udqp, *gdqp;
2441 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Christoph Hellwig3937be52008-03-06 13:46:19 +11002443 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 error = 0;
2445 ip = NULL;
2446 tp = NULL;
2447
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002448 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 if (XFS_FORCED_SHUTDOWN(mp))
2451 return XFS_ERROR(EIO);
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /*
2454 * Check component lengths of the target path name.
2455 */
2456 pathlen = strlen(target_path);
2457 if (pathlen >= MAXPATHLEN) /* total string too long */
2458 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002460 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002461 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002463 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (error)
2465 return error;
2466 }
2467
2468 /* Return through std_return after this point. */
2469
2470 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002471 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2472 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 else
2474 prid = (xfs_prid_t)dfltprid;
2475
2476 /*
2477 * Make sure that we have allocated dquot(s) on disk.
2478 */
2479 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01002480 current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2482 if (error)
2483 goto std_return;
2484
2485 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2486 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2487 /*
2488 * The symlink will fit into the inode data fork?
2489 * There can't be any attributes so we get the whole variable part.
2490 */
2491 if (pathlen <= XFS_LITINO(mp))
2492 fs_blocks = 0;
2493 else
2494 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002495 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2497 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2498 if (error == ENOSPC && fs_blocks == 0) {
2499 resblks = 0;
2500 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2501 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2502 }
2503 if (error) {
2504 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 goto error_return;
2506 }
2507
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002508 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002509 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 /*
2512 * Check whether the directory allows new symlinks or not.
2513 */
2514 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2515 error = XFS_ERROR(EPERM);
2516 goto error_return;
2517 }
2518
2519 /*
2520 * Reserve disk quota : blocks and inode.
2521 */
2522 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2523 if (error)
2524 goto error_return;
2525
2526 /*
2527 * Check for ability to enter directory entry, if no space reserved.
2528 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002529 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2530 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 goto error_return;
2532 /*
2533 * Initialize the bmap freelist prior to calling either
2534 * bmapi or the directory create code.
2535 */
2536 XFS_BMAP_INIT(&free_list, &first_block);
2537
2538 /*
2539 * Allocate an inode for the symlink.
2540 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002541 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 1, 0, credp, prid, resblks > 0, &ip, NULL);
2543 if (error) {
2544 if (error == ENOSPC)
2545 goto error_return;
2546 goto error1;
2547 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002548 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Christoph Hellwig993386c2007-08-28 16:12:30 +10002550 /*
2551 * An error after we've joined dp to the transaction will result in the
2552 * transaction cancel unlocking dp so don't do it explicitly in the
2553 * error path.
2554 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002555 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002557 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 /*
2560 * Also attach the dquot(s) to it, if applicable.
2561 */
2562 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2563
2564 if (resblks)
2565 resblks -= XFS_IALLOC_SPACE_RES(mp);
2566 /*
2567 * If the symlink will fit into the inode, write it inline.
2568 */
2569 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2570 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2571 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2572 ip->i_d.di_size = pathlen;
2573
2574 /*
2575 * The inode was initially created in extent format.
2576 */
2577 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2578 ip->i_df.if_flags |= XFS_IFINLINE;
2579
2580 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2581 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2582
2583 } else {
2584 first_fsb = 0;
2585 nmaps = SYMLINK_MAPS;
2586
2587 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2588 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2589 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002590 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (error) {
2592 goto error1;
2593 }
2594
2595 if (resblks)
2596 resblks -= fs_blocks;
2597 ip->i_d.di_size = pathlen;
2598 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2599
2600 cur_chunk = target_path;
2601 for (n = 0; n < nmaps; n++) {
2602 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2603 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2604 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2605 BTOBB(byte_cnt), 0);
2606 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2607 if (pathlen < byte_cnt) {
2608 byte_cnt = pathlen;
2609 }
2610 pathlen -= byte_cnt;
2611
2612 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2613 cur_chunk += byte_cnt;
2614
2615 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2616 }
2617 }
2618
2619 /*
2620 * Create the directory entry for the symlink.
2621 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002622 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2623 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002624 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2627 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2628
2629 /*
2630 * Bump the in memory version number of the parent directory
2631 * so that other processes accessing it will recognize that
2632 * the directory has changed.
2633 */
2634 dp->i_gen++;
2635
2636 /*
2637 * If this is a synchronous mount, make sure that the
2638 * symlink transaction goes to disk before returning to
2639 * the user.
2640 */
2641 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2642 xfs_trans_set_sync(tp);
2643 }
2644
2645 /*
2646 * xfs_trans_commit normally decrements the vnode ref count
2647 * when it unlocks the inode. Since we want to return the
2648 * vnode to the caller, we bump the vnode ref count now.
2649 */
2650 IHOLD(ip);
2651
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002652 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (error) {
2654 goto error2;
2655 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002656 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 XFS_QM_DQRELE(mp, udqp);
2658 XFS_QM_DQRELE(mp, gdqp);
2659
2660 /* Fall through to std_return with error = 0 or errno from
2661 * xfs_trans_commit */
2662std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10002663 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002665 dp, DM_RIGHT_NULL,
2666 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002667 DM_RIGHT_NULL, link_name->name,
2668 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670
Christoph Hellwig3937be52008-03-06 13:46:19 +11002671 if (!error)
2672 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 return error;
2674
2675 error2:
2676 IRELE(ip);
2677 error1:
2678 xfs_bmap_cancel(&free_list);
2679 cancel_flags |= XFS_TRANS_ABORT;
2680 error_return:
2681 xfs_trans_cancel(tp, cancel_flags);
2682 XFS_QM_DQRELE(mp, udqp);
2683 XFS_QM_DQRELE(mp, gdqp);
2684
Christoph Hellwig993386c2007-08-28 16:12:30 +10002685 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 goto std_return;
2689}
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002693 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 int flags)
2695{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002696 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 int error = 0;
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (XFS_FORCED_SHUTDOWN(mp))
2700 return XFS_ERROR(EIO);
2701
2702 /*
2703 * Bypass inodes which have already been cleaned by
2704 * the inode flush clustering code inside xfs_iflush
2705 */
David Chinner33540402008-03-06 13:43:59 +11002706 if (xfs_inode_clean(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return 0;
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 /*
2710 * We make this non-blocking if the inode is contended,
2711 * return EAGAIN to indicate to the caller that they
2712 * did not succeed. This prevents the flush path from
2713 * blocking on inodes inside another operation right
2714 * now, they get caught later by xfs_sync.
2715 */
David Chinnera3f74ff2008-03-06 13:43:42 +11002716 if (flags & FLUSH_SYNC) {
2717 xfs_ilock(ip, XFS_ILOCK_SHARED);
2718 xfs_iflock(ip);
2719 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
2720 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
2721 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return EAGAIN;
2723 }
David Chinnera3f74ff2008-03-06 13:43:42 +11002724 } else {
2725 return EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 }
2727
David Chinnera3f74ff2008-03-06 13:43:42 +11002728 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
2729 : XFS_IFLUSH_ASYNC_NOBLOCK);
2730 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return error;
2733}
2734
Christoph Hellwig993386c2007-08-28 16:12:30 +10002735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002737xfs_set_dmattrs(
2738 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002740 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002742 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 int error;
2745
2746 if (!capable(CAP_SYS_ADMIN))
2747 return XFS_ERROR(EPERM);
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (XFS_FORCED_SHUTDOWN(mp))
2750 return XFS_ERROR(EIO);
2751
2752 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2753 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2754 if (error) {
2755 xfs_trans_cancel(tp, 0);
2756 return error;
2757 }
2758 xfs_ilock(ip, XFS_ILOCK_EXCL);
2759 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2760
Christoph Hellwig613d7042007-10-11 17:44:08 +10002761 ip->i_d.di_dmevmask = evmask;
2762 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2765 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002766 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 return error;
2769}
2770
Christoph Hellwig993386c2007-08-28 16:12:30 +10002771int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002773 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002776 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002778 ASSERT(!VN_MAPPED(VFS_I(ip)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 /* bad inode, get out here ASAP */
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002781 if (VN_BAD(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 xfs_ireclaim(ip);
2783 return 0;
2784 }
2785
Christoph Hellwigb677c212007-08-29 11:46:28 +10002786 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10002788 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11002790 /*
2791 * Make sure the atime in the XFS inode is correct before freeing the
2792 * Linux inode.
2793 */
2794 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
David Chinner4c606582006-11-11 18:05:00 +11002796 /*
2797 * If we have nothing to flush with this inode then complete the
2798 * teardown now, otherwise break the link between the xfs inode and the
2799 * linux inode and clean up the xfs inode later. This avoids flushing
2800 * the inode to disk during the delete operation itself.
2801 *
2802 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2803 * first to ensure that xfs_iunpin() will never see an xfs inode
2804 * that has a linux inode being reclaimed. Synchronisation is provided
2805 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 */
2807 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2808 xfs_ilock(ip, XFS_ILOCK_EXCL);
2809 xfs_iflock(ip);
David Chinnerbf904242008-10-30 17:36:14 +11002810 xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner1dc33182008-10-30 17:37:15 +11002811 return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
David Chinner11654512008-10-30 17:37:49 +11002813 xfs_inode_set_reclaim_tag(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return 0;
2815}
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817/*
2818 * xfs_alloc_file_space()
2819 * This routine allocates disk space for the given file.
2820 *
2821 * If alloc_type == 0, this request is for an ALLOCSP type
2822 * request which will change the file size. In this case, no
2823 * DMAPI event will be generated by the call. A TRUNCATE event
2824 * will be generated later by xfs_setattr.
2825 *
2826 * If alloc_type != 0, this request is for a RESVSP type
2827 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2828 * lower block boundary byte address is less than the file's
2829 * length.
2830 *
2831 * RETURNS:
2832 * 0 on success
2833 * errno on error
2834 *
2835 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002836STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837xfs_alloc_file_space(
2838 xfs_inode_t *ip,
2839 xfs_off_t offset,
2840 xfs_off_t len,
2841 int alloc_type,
2842 int attr_flags)
2843{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002844 xfs_mount_t *mp = ip->i_mount;
2845 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 xfs_filblks_t allocated_fsb;
2847 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002848 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002850 xfs_fsblock_t firstfsb;
2851 int nimaps;
2852 int bmapi_flag;
2853 int quota_flag;
2854 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002856 xfs_bmbt_irec_t imaps[1], *imapp;
2857 xfs_bmap_free_t free_list;
2858 uint qblocks, resblks, resrtextents;
2859 int committed;
2860 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002862 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 if (XFS_FORCED_SHUTDOWN(mp))
2865 return XFS_ERROR(EIO);
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2868 return error;
2869
2870 if (len <= 0)
2871 return XFS_ERROR(EINVAL);
2872
David Chinner957d0eb2007-06-18 16:50:37 +10002873 rt = XFS_IS_REALTIME_INODE(ip);
2874 extsz = xfs_get_extsz_hint(ip);
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11002878 nimaps = 1;
2879 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2881 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2882
2883 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002884 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002885 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002886 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 xfs_off_t end_dmi_offset;
2888
2889 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002890 if (end_dmi_offset > ip->i_size)
2891 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002892 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2893 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002895 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
2897
2898 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002899 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 */
2901retry:
2902 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002903 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Nathan Scottdd9f4382006-01-11 15:28:28 +11002905 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11002906 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002907 */
2908 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002910 do_div(s, extsz);
2911 s *= extsz;
2912 e = startoffset_fsb + allocatesize_fsb;
2913 if ((temp = do_mod(startoffset_fsb, extsz)))
2914 e += temp;
2915 if ((temp = do_mod(e, extsz)))
2916 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002918 s = 0;
2919 e = allocatesize_fsb;
2920 }
2921
2922 if (unlikely(rt)) {
2923 resrtextents = qblocks = (uint)(e - s);
2924 resrtextents /= mp->m_sb.sb_rextsize;
2925 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2926 quota_flag = XFS_QMOPT_RES_RTBLKS;
2927 } else {
2928 resrtextents = 0;
2929 resblks = qblocks = \
2930 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2931 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
2933
2934 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002935 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 */
2937 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002938 error = xfs_trans_reserve(tp, resblks,
2939 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 XFS_TRANS_PERM_LOG_RES,
2941 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002943 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 */
2945 if (error) {
2946 /*
2947 * Free the transaction structure.
2948 */
2949 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2950 xfs_trans_cancel(tp, 0);
2951 break;
2952 }
2953 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002954 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
2955 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (error)
2957 goto error1;
2958
2959 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2960 xfs_trans_ihold(tp, ip);
2961
2962 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002963 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 */
2965 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002966 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002967 allocatesize_fsb, bmapi_flag,
2968 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002969 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (error) {
2971 goto error0;
2972 }
2973
2974 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002975 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002977 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (error) {
2979 goto error0;
2980 }
2981
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002982 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2984 if (error) {
2985 break;
2986 }
2987
2988 allocated_fsb = imapp->br_blockcount;
2989
Nathan Scottdd9f4382006-01-11 15:28:28 +11002990 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 error = XFS_ERROR(ENOSPC);
2992 break;
2993 }
2994
2995 startoffset_fsb += allocated_fsb;
2996 allocatesize_fsb -= allocated_fsb;
2997 }
2998dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002999 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003000 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003002 ip, DM_RIGHT_NULL,
3003 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3005 if (error == 0)
3006 goto retry; /* Maybe DMAPI app. has made space */
3007 /* else fall through with error from XFS_SEND_DATA */
3008 }
3009
3010 return error;
3011
Nathan Scottdd9f4382006-01-11 15:28:28 +11003012error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003014 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3015
3016error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3018 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3019 goto dmapi_enospc_check;
3020}
3021
3022/*
3023 * Zero file bytes between startoff and endoff inclusive.
3024 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10003025 *
3026 * This function is used by xfs_free_file_space() to zero
3027 * partial blocks when the range to free is not block aligned.
3028 * When unreserving space with boundaries that are not block
3029 * aligned we round up the start and round down the end
3030 * boundaries and then use this function to zero the parts of
3031 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 */
3033STATIC int
3034xfs_zero_remaining_bytes(
3035 xfs_inode_t *ip,
3036 xfs_off_t startoff,
3037 xfs_off_t endoff)
3038{
3039 xfs_bmbt_irec_t imap;
3040 xfs_fileoff_t offset_fsb;
3041 xfs_off_t lastoffset;
3042 xfs_off_t offset;
3043 xfs_buf_t *bp;
3044 xfs_mount_t *mp = ip->i_mount;
3045 int nimap;
3046 int error = 0;
3047
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10003048 /*
3049 * Avoid doing I/O beyond eof - it's not necessary
3050 * since nothing can read beyond eof. The space will
3051 * be zeroed when the file is extended anyway.
3052 */
3053 if (startoff >= ip->i_size)
3054 return 0;
3055
3056 if (endoff > ip->i_size)
3057 endoff = ip->i_size;
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003060 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 mp->m_rtdev_targp : mp->m_ddev_targp);
3062
3063 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3064 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3065 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003066 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003067 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 if (error || nimap < 1)
3069 break;
3070 ASSERT(imap.br_blockcount >= 1);
3071 ASSERT(imap.br_startoff == offset_fsb);
3072 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3073 if (lastoffset > endoff)
3074 lastoffset = endoff;
3075 if (imap.br_startblock == HOLESTARTBLOCK)
3076 continue;
3077 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3078 if (imap.br_state == XFS_EXT_UNWRITTEN)
3079 continue;
3080 XFS_BUF_UNDONE(bp);
3081 XFS_BUF_UNWRITE(bp);
3082 XFS_BUF_READ(bp);
3083 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3084 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003085 error = xfs_iowait(bp);
3086 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3088 mp, bp, XFS_BUF_ADDR(bp));
3089 break;
3090 }
3091 memset(XFS_BUF_PTR(bp) +
3092 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3093 0, lastoffset - offset + 1);
3094 XFS_BUF_UNDONE(bp);
3095 XFS_BUF_UNREAD(bp);
3096 XFS_BUF_WRITE(bp);
3097 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003098 error = xfs_iowait(bp);
3099 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3101 mp, bp, XFS_BUF_ADDR(bp));
3102 break;
3103 }
3104 }
3105 xfs_buf_free(bp);
3106 return error;
3107}
3108
3109/*
3110 * xfs_free_file_space()
3111 * This routine frees disk space for the given file.
3112 *
3113 * This routine is only called by xfs_change_file_space
3114 * for an UNRESVSP type call.
3115 *
3116 * RETURNS:
3117 * 0 on success
3118 * errno on error
3119 *
3120 */
3121STATIC int
3122xfs_free_file_space(
3123 xfs_inode_t *ip,
3124 xfs_off_t offset,
3125 xfs_off_t len,
3126 int attr_flags)
3127{
3128 int committed;
3129 int done;
3130 xfs_off_t end_dmi_offset;
3131 xfs_fileoff_t endoffset_fsb;
3132 int error;
3133 xfs_fsblock_t firstfsb;
3134 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 xfs_bmbt_irec_t imap;
3136 xfs_off_t ioffset;
3137 xfs_extlen_t mod=0;
3138 xfs_mount_t *mp;
3139 int nimap;
3140 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10003141 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 int rt;
3143 xfs_fileoff_t startoffset_fsb;
3144 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003145 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 mp = ip->i_mount;
3148
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003149 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003150
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3152 return error;
3153
3154 error = 0;
3155 if (len <= 0) /* if nothing being freed */
3156 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003157 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3159 end_dmi_offset = offset + len;
3160 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3161
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003162 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003163 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003164 if (end_dmi_offset > ip->i_size)
3165 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003166 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 offset, end_dmi_offset - offset,
3168 AT_DELAY_FLAG(attr_flags), NULL);
3169 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003170 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
3172
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003173 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003174 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11003175 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10003177 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11003178 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003179
Tim Shimmine6a4b372007-11-23 16:30:42 +11003180 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003182
Christoph Hellwigdf80c932008-08-13 16:22:09 +10003183 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003184 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3185 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10003186 if (error)
3187 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003188 }
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 /*
3191 * Need to zero the stuff we're not freeing, on disk.
3192 * If its a realtime file & can't use unwritten extents then we
3193 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3194 * will take care of it for us.
3195 */
Eric Sandeen62118702008-03-06 13:44:28 +11003196 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003198 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003199 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 if (error)
3201 goto out_unlock_iolock;
3202 ASSERT(nimap == 0 || nimap == 1);
3203 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3204 xfs_daddr_t block;
3205
3206 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3207 block = imap.br_startblock;
3208 mod = do_div(block, mp->m_sb.sb_rextsize);
3209 if (mod)
3210 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3211 }
3212 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003213 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003214 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (error)
3216 goto out_unlock_iolock;
3217 ASSERT(nimap == 0 || nimap == 1);
3218 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3219 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3220 mod++;
3221 if (mod && (mod != mp->m_sb.sb_rextsize))
3222 endoffset_fsb -= mod;
3223 }
3224 }
3225 if ((done = (endoffset_fsb <= startoffset_fsb)))
3226 /*
3227 * One contiguous piece to clear
3228 */
3229 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3230 else {
3231 /*
3232 * Some full blocks, possibly two pieces to clear
3233 */
3234 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3235 error = xfs_zero_remaining_bytes(ip, offset,
3236 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3237 if (!error &&
3238 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3239 error = xfs_zero_remaining_bytes(ip,
3240 XFS_FSB_TO_B(mp, endoffset_fsb),
3241 offset + len - 1);
3242 }
3243
3244 /*
3245 * free file space until done or until there is an error
3246 */
3247 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3248 while (!error && !done) {
3249
3250 /*
David Chinner91ebecc2007-07-19 16:28:30 +10003251 * allocate and setup the transaction. Allow this
3252 * transaction to dip into the reserve blocks to ensure
3253 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 */
3255 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10003256 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 error = xfs_trans_reserve(tp,
3258 resblks,
3259 XFS_WRITE_LOG_RES(mp),
3260 0,
3261 XFS_TRANS_PERM_LOG_RES,
3262 XFS_WRITE_LOG_COUNT);
3263
3264 /*
3265 * check for running out of space
3266 */
3267 if (error) {
3268 /*
3269 * Free the transaction structure.
3270 */
3271 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3272 xfs_trans_cancel(tp, 0);
3273 break;
3274 }
3275 xfs_ilock(ip, XFS_ILOCK_EXCL);
3276 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003277 ip->i_udquot, ip->i_gdquot, resblks, 0,
3278 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 if (error)
3280 goto error1;
3281
3282 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3283 xfs_trans_ihold(tp, ip);
3284
3285 /*
3286 * issue the bunmapi() call to free the blocks
3287 */
3288 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003289 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003291 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (error) {
3293 goto error0;
3294 }
3295
3296 /*
3297 * complete the transaction
3298 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003299 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (error) {
3301 goto error0;
3302 }
3303
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003304 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3306 }
3307
3308 out_unlock_iolock:
3309 if (need_iolock)
3310 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3311 return error;
3312
3313 error0:
3314 xfs_bmap_cancel(&free_list);
3315 error1:
3316 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3317 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3318 XFS_ILOCK_EXCL);
3319 return error;
3320}
3321
3322/*
3323 * xfs_change_file_space()
3324 * This routine allocates or frees disk space for the given file.
3325 * The user specified parameters are checked for alignment and size
3326 * limitations.
3327 *
3328 * RETURNS:
3329 * 0 on success
3330 * errno on error
3331 *
3332 */
3333int
3334xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003335 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 int cmd,
3337 xfs_flock64_t *bf,
3338 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 int attr_flags)
3340{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003341 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 int clrprealloc;
3343 int error;
3344 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 int setprealloc;
3346 xfs_off_t startoffset;
3347 xfs_off_t llen;
3348 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003349 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003351 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Christoph Hellwig993386c2007-08-28 16:12:30 +10003353 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 return XFS_ERROR(EINVAL);
3355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 switch (bf->l_whence) {
3357 case 0: /*SEEK_SET*/
3358 break;
3359 case 1: /*SEEK_CUR*/
3360 bf->l_start += offset;
3361 break;
3362 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003363 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 break;
3365 default:
3366 return XFS_ERROR(EINVAL);
3367 }
3368
3369 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3370
3371 if ( (bf->l_start < 0)
3372 || (bf->l_start > XFS_MAXIOFFSET(mp))
3373 || (bf->l_start + llen < 0)
3374 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3375 return XFS_ERROR(EINVAL);
3376
3377 bf->l_whence = 0;
3378
3379 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003380 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382 /*
3383 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3384 * file space.
3385 * These calls do NOT zero the data space allocated to the file,
3386 * nor do they change the file size.
3387 *
3388 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3389 * space.
3390 * These calls cause the new file data to be zeroed and the file
3391 * size to be changed.
3392 */
3393 setprealloc = clrprealloc = 0;
3394
3395 switch (cmd) {
3396 case XFS_IOC_RESVSP:
3397 case XFS_IOC_RESVSP64:
3398 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3399 1, attr_flags);
3400 if (error)
3401 return error;
3402 setprealloc = 1;
3403 break;
3404
3405 case XFS_IOC_UNRESVSP:
3406 case XFS_IOC_UNRESVSP64:
3407 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3408 attr_flags)))
3409 return error;
3410 break;
3411
3412 case XFS_IOC_ALLOCSP:
3413 case XFS_IOC_ALLOCSP64:
3414 case XFS_IOC_FREESP:
3415 case XFS_IOC_FREESP64:
3416 if (startoffset > fsize) {
3417 error = xfs_alloc_file_space(ip, fsize,
3418 startoffset - fsize, 0, attr_flags);
3419 if (error)
3420 break;
3421 }
3422
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003423 iattr.ia_valid = ATTR_SIZE;
3424 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +11003426 error = xfs_setattr(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
3428 if (error)
3429 return error;
3430
3431 clrprealloc = 1;
3432 break;
3433
3434 default:
3435 ASSERT(0);
3436 return XFS_ERROR(EINVAL);
3437 }
3438
3439 /*
3440 * update the inode timestamp, mode, and prealloc flag bits
3441 */
3442 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3443
3444 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3445 0, 0, 0))) {
3446 /* ASSERT(0); */
3447 xfs_trans_cancel(tp, 0);
3448 return error;
3449 }
3450
3451 xfs_ilock(ip, XFS_ILOCK_EXCL);
3452
3453 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3454 xfs_trans_ihold(tp, ip);
3455
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003456 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 ip->i_d.di_mode &= ~S_ISUID;
3458
3459 /*
3460 * Note that we don't have to worry about mandatory
3461 * file locking being disabled here because we only
3462 * clear the S_ISGID bit if the Group execute bit is
3463 * on, but if it was on then mandatory locking wouldn't
3464 * have been enabled.
3465 */
3466 if (ip->i_d.di_mode & S_IXGRP)
3467 ip->i_d.di_mode &= ~S_ISGID;
3468
3469 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3470 }
3471 if (setprealloc)
3472 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3473 else if (clrprealloc)
3474 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3475
3476 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3477 xfs_trans_set_sync(tp);
3478
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003479 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
3481 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3482
3483 return error;
3484}