blob: 0574aadc4d3cfe287d8a88e23e82b11814225831 [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_open(
Christoph Hellwig993386c2007-08-28 16:12:30 +100058 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
63 return XFS_ERROR(EIO);
64
65 /*
66 * If it's a directory with any blocks, read-ahead block 0
67 * as we're almost certain to have the next operation be a read there.
68 */
Christoph Hellwig993386c2007-08-28 16:12:30 +100069 if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 mode = xfs_ilock_map_shared(ip);
71 if (ip->i_d.di_nextents > 0)
72 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
73 xfs_iunlock(ip, mode);
74 }
75 return 0;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078int
79xfs_setattr(
Christoph Hellwig0f285c82008-07-18 17:13:28 +100080 struct xfs_inode *ip,
81 struct iattr *iattr,
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +110082 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Christoph Hellwig993386c2007-08-28 16:12:30 +100084 xfs_mount_t *mp = ip->i_mount;
David Chinnere4f75292008-08-13 16:00:45 +100085 struct inode *inode = VFS_I(ip);
Christoph Hellwig0f285c82008-07-18 17:13:28 +100086 int mask = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int code;
89 uint lock_flags;
90 uint commit_flags=0;
91 uid_t uid=0, iuid=0;
92 gid_t gid=0, igid=0;
93 int timeflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
95 int file_owner;
Dean Roehrich5fcbab32005-05-05 13:27:19 -070096 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Lachlan McIlroycf441ee2008-02-07 16:42:19 +110098 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000100 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return XFS_ERROR(EROFS);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (XFS_FORCED_SHUTDOWN(mp))
104 return XFS_ERROR(EIO);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 olddquot1 = olddquot2 = NULL;
107 udqp = gdqp = NULL;
108
109 /*
110 * If disk quotas is on, we make sure that the dquots do exist on disk,
111 * before we start any other transactions. Trying to do this later
112 * is messy. We don't care to take a readlock to look at the ids
113 * in inode here, because we can't hold it across the trans_reserve.
114 * If the IDs do change before we take the ilock, we're covered
115 * because the i_*dquot fields will get updated anyway.
116 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000117 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 uint qflags = 0;
119
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000120 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
121 uid = iattr->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 qflags |= XFS_QMOPT_UQUOTA;
123 } else {
124 uid = ip->i_d.di_uid;
125 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000126 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
127 gid = iattr->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 qflags |= XFS_QMOPT_GQUOTA;
129 } else {
130 gid = ip->i_d.di_gid;
131 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /*
134 * We take a reference when we initialize udqp and gdqp,
135 * so it is important that we never blindly double trip on
136 * the same variable. See xfs_create() for an example.
137 */
138 ASSERT(udqp == NULL);
139 ASSERT(gdqp == NULL);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000140 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, ip->i_d.di_projid,
141 qflags, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100143 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
146 /*
147 * For the other attributes, we acquire the inode lock and
148 * first do an error checking pass.
149 */
150 tp = NULL;
151 lock_flags = XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000152 if (flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700153 need_iolock = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000154 if (!(mask & ATTR_SIZE)) {
155 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 (mp->m_flags & XFS_MOUNT_WSYNC)) {
157 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
158 commit_flags = 0;
159 if ((code = xfs_trans_reserve(tp, 0,
160 XFS_ICHANGE_LOG_RES(mp), 0,
161 0, 0))) {
162 lock_flags = 0;
163 goto error_return;
164 }
165 }
166 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000167 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000168 !(flags & XFS_ATTR_DMI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100170 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000171 iattr->ia_size, 0, dmflags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (code) {
173 lock_flags = 0;
174 goto error_return;
175 }
176 }
177 if (need_iolock)
178 lock_flags |= XFS_IOLOCK_EXCL;
179 }
180
181 xfs_ilock(ip, lock_flags);
182
183 /* boolean: are we the file owner? */
David Howells9e2b2dc2008-08-13 16:20:04 +0100184 file_owner = (current_fsuid() == ip->i_d.di_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /*
187 * Change various properties of a file.
188 * Only the owner or users with CAP_FOWNER
189 * capability may do these things.
190 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000191 if (mask & (ATTR_MODE|ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * CAP_FOWNER overrides the following restrictions:
194 *
195 * The user ID of the calling process must be equal
196 * to the file owner ID, except in cases where the
197 * CAP_FSETID capability is applicable.
198 */
199 if (!file_owner && !capable(CAP_FOWNER)) {
200 code = XFS_ERROR(EPERM);
201 goto error_return;
202 }
203
204 /*
205 * CAP_FSETID overrides the following restrictions:
206 *
207 * The effective user ID of the calling process shall match
208 * the file owner when setting the set-user-ID and
209 * set-group-ID bits on that file.
210 *
211 * The effective group ID or one of the supplementary group
212 * IDs of the calling process shall match the group owner of
213 * the file when setting the set-group-ID bit on that file
214 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000215 if (mask & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 mode_t m = 0;
217
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000218 if ((iattr->ia_mode & S_ISUID) && !file_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 m |= S_ISUID;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000220 if ((iattr->ia_mode & S_ISGID) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 !in_group_p((gid_t)ip->i_d.di_gid))
222 m |= S_ISGID;
223#if 0
224 /* Linux allows this, Irix doesn't. */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000225 if ((iattr->ia_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 m |= S_ISVTX;
227#endif
228 if (m && !capable(CAP_FSETID))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000229 iattr->ia_mode &= ~m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 }
232
233 /*
234 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000236 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /*
238 * These IDs could have changed since we last looked at them.
239 * But, we're assured that if the ownership did change
240 * while we didn't have the inode locked, inode's dquot(s)
241 * would have changed also.
242 */
243 iuid = ip->i_d.di_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 igid = ip->i_d.di_gid;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000245 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
246 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /*
249 * CAP_CHOWN overrides the following restrictions:
250 *
251 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
252 * shall override the restriction that a process cannot
253 * change the user ID of a file it owns and the restriction
254 * that the group ID supplied to the chown() function
255 * shall be equal to either the group ID or one of the
256 * supplementary group IDs of the calling process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Tim Shimmine0b8e8b2008-10-30 18:30:48 +1100258 if ((iuid != uid ||
259 (igid != gid && !in_group_p((gid_t)gid))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 !capable(CAP_CHOWN)) {
261 code = XFS_ERROR(EPERM);
262 goto error_return;
263 }
264 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000265 * Do a quota reservation only if uid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * going to change.
267 */
268 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
269 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
270 ASSERT(tp);
271 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
272 capable(CAP_FOWNER) ?
273 XFS_QMOPT_FORCE_RES : 0);
274 if (code) /* out of quota */
275 goto error_return;
276 }
277 }
278
279 /*
280 * Truncate file. Must have write permission and not be a directory.
281 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000282 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* Short circuit the truncate case for zero length files */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000284 if (iattr->ia_size == 0 &&
285 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 xfs_iunlock(ip, XFS_ILOCK_EXCL);
287 lock_flags &= ~XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000288 if (mask & ATTR_CTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
290 code = 0;
291 goto error_return;
292 }
293
Christoph Hellwig42173f62008-04-22 17:33:25 +1000294 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 code = XFS_ERROR(EISDIR);
296 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000297 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 code = XFS_ERROR(EINVAL);
299 goto error_return;
300 }
301 /*
302 * Make sure that the dquots are attached to the inode.
303 */
304 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
305 goto error_return;
306 }
307
308 /*
309 * Change file access or modified times.
310 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000311 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!file_owner) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000313 if ((mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 !capable(CAP_FOWNER)) {
315 code = XFS_ERROR(EPERM);
316 goto error_return;
317 }
318 }
319 }
320
321 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * Now we can make the changes. Before we join the inode
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000323 * to the transaction, if ATTR_SIZE is set then take care of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * the part of the truncation that must be done without the
325 * inode lock. This needs to be done before joining the inode
326 * to the transaction, because the inode cannot be unlocked
327 * once it is a part of the transaction.
328 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000329 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 code = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000331 if (iattr->ia_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000332 /*
333 * Do the first part of growing a file: zero any data
334 * in the last block that is beyond the old EOF. We
335 * need to do this before the inode is joined to the
336 * transaction to modify the i_size.
337 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000338 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000341
342 /*
343 * We are going to log the inode size change in this
344 * transaction so any previous writes that are beyond the on
345 * disk EOF and the new EOF that have not been written out need
346 * to be written here. If we do not write the data out, we
347 * expose ourselves to the null files problem.
348 *
349 * Only flush from the on disk size to the smaller of the in
350 * memory file size or the new size as that's the range we
351 * really care about here and prevents waiting for other data
352 * not within the range we care about here.
353 */
354 if (!code &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000355 ip->i_size != ip->i_d.di_size &&
356 iattr->ia_size > ip->i_d.di_size) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000357 code = xfs_flush_pages(ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000358 ip->i_d.di_size, iattr->ia_size,
David Chinnerc32676e2007-07-19 16:28:58 +1000359 XFS_B_ASYNC, FI_NONE);
360 }
361
362 /* wait for all I/O to complete */
Christoph Hellwigb677c212007-08-29 11:46:28 +1000363 vn_iowait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!code)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000366 code = xfs_itruncate_data(ip, iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (code) {
368 ASSERT(tp == NULL);
369 lock_flags &= ~XFS_ILOCK_EXCL;
370 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
371 goto error_return;
372 }
373 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
374 if ((code = xfs_trans_reserve(tp, 0,
375 XFS_ITRUNCATE_LOG_RES(mp), 0,
376 XFS_TRANS_PERM_LOG_RES,
377 XFS_ITRUNCATE_LOG_COUNT))) {
378 xfs_trans_cancel(tp, 0);
379 if (need_iolock)
380 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
381 return code;
382 }
383 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
384 xfs_ilock(ip, XFS_ILOCK_EXCL);
385 }
386
387 if (tp) {
388 xfs_trans_ijoin(tp, ip, lock_flags);
389 xfs_trans_ihold(tp, ip);
390 }
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /*
393 * Truncate file. Must have write permission and not be a directory.
394 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000395 if (mask & ATTR_SIZE) {
David Chinner44d814c2008-03-06 13:45:29 +1100396 /*
397 * Only change the c/mtime if we are changing the size
398 * or we are explicitly asked to change it. This handles
399 * the semantic difference between truncate() and ftruncate()
400 * as implemented in the VFS.
401 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000402 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
David Chinner44d814c2008-03-06 13:45:29 +1100403 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
404
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000405 if (iattr->ia_size > ip->i_size) {
406 ip->i_d.di_size = iattr->ia_size;
407 ip->i_size = iattr->ia_size;
408 if (!(flags & XFS_ATTR_DMI))
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000409 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
410 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000411 } else if (iattr->ia_size <= ip->i_size ||
412 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /*
414 * signal a sync transaction unless
415 * we're truncating an already unlinked
416 * file on a wsync filesystem
417 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000418 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 XFS_DATA_FORK,
420 ((ip->i_d.di_nlink != 0 ||
421 !(mp->m_flags & XFS_MOUNT_WSYNC))
422 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000423 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000425 /*
426 * Truncated "down", so we're removing references
427 * to old data here - if we now delay flushing for
428 * a long time, we expose ourselves unduly to the
429 * notorious NULL files problem. So, we mark this
430 * vnode and flush it when the file is closed, and
431 * do not wait the usual (long) time for writeout.
432 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000433 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
437 /*
438 * Change file access modes.
439 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000440 if (mask & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 ip->i_d.di_mode &= S_IFMT;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000442 ip->i_d.di_mode |= iattr->ia_mode & ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000444 inode->i_mode &= S_IFMT;
445 inode->i_mode |= iattr->ia_mode & ~S_IFMT;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
448 timeflags |= XFS_ICHGTIME_CHG;
449 }
450
451 /*
452 * Change file ownership. Must be the owner or privileged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000454 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /*
456 * CAP_FSETID overrides the following restrictions:
457 *
458 * The set-user-ID and set-group-ID bits of a file will be
459 * cleared upon successful return from chown()
460 */
461 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
462 !capable(CAP_FSETID)) {
463 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
464 }
465
466 /*
467 * Change the ownerships and register quota modifications
468 * in the transaction.
469 */
470 if (iuid != uid) {
471 if (XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000472 ASSERT(mask & ATTR_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 ASSERT(udqp);
474 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
475 &ip->i_udquot, udqp);
476 }
477 ip->i_d.di_uid = uid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000478 inode->i_uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480 if (igid != gid) {
481 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000482 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000483 ASSERT(mask & ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 ASSERT(gdqp);
485 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
486 &ip->i_gdquot, gdqp);
487 }
488 ip->i_d.di_gid = gid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000489 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
493 timeflags |= XFS_ICHGTIME_CHG;
494 }
495
496
497 /*
498 * Change file access or modified times.
499 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000500 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
501 if (mask & ATTR_ATIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000502 inode->i_atime = iattr->ia_atime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000503 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
504 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 ip->i_update_core = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000507 if (mask & ATTR_MTIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000508 inode->i_mtime = iattr->ia_mtime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000509 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
510 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 timeflags &= ~XFS_ICHGTIME_MOD;
512 timeflags |= XFS_ICHGTIME_CHG;
513 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000514 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
516 }
517
518 /*
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000519 * Change file inode change time only if ATTR_CTIME set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * AND we have been called by a DMI function.
521 */
522
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000523 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000524 inode->i_ctime = iattr->ia_ctime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000525 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
526 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ip->i_update_core = 1;
528 timeflags &= ~XFS_ICHGTIME_CHG;
529 }
530
531 /*
532 * Send out timestamp changes that need to be set to the
533 * current time. Not done when called by a DMI function.
534 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000535 if (timeflags && !(flags & XFS_ATTR_DMI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 xfs_ichgtime(ip, timeflags);
537
538 XFS_STATS_INC(xs_ig_attrchg);
539
540 /*
541 * If this is a synchronous mount, make sure that the
542 * transaction goes to disk before returning to the user.
543 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000544 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 * One for the truncate and one for the timestamps since we
546 * don't want to change the timestamps unless we're sure the
547 * truncate worked. Truncates are less than 1% of the laddis
548 * mix so this probably isn't worth the trouble to optimize.
549 */
550 code = 0;
551 if (tp) {
552 if (mp->m_flags & XFS_MOUNT_WSYNC)
553 xfs_trans_set_sync(tp);
554
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000555 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 xfs_iunlock(ip, lock_flags);
559
560 /*
561 * Release any dquot(s) the inode had kept before chown.
562 */
563 XFS_QM_DQRELE(mp, olddquot1);
564 XFS_QM_DQRELE(mp, olddquot2);
565 XFS_QM_DQRELE(mp, udqp);
566 XFS_QM_DQRELE(mp, gdqp);
567
568 if (code) {
569 return code;
570 }
571
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000572 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000573 !(flags & XFS_ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100574 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 NULL, DM_RIGHT_NULL, NULL, NULL,
576 0, 0, AT_DELAY_FLAG(flags));
577 }
578 return 0;
579
580 abort_return:
581 commit_flags |= XFS_TRANS_ABORT;
582 /* FALLTHROUGH */
583 error_return:
584 XFS_QM_DQRELE(mp, udqp);
585 XFS_QM_DQRELE(mp, gdqp);
586 if (tp) {
587 xfs_trans_cancel(tp, commit_flags);
588 }
589 if (lock_flags != 0) {
590 xfs_iunlock(ip, lock_flags);
591 }
592 return code;
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000596 * The maximum pathlen is 1024 bytes. Since the minimum file system
597 * blocksize is 512 bytes, we can get a max of 2 extents back from
598 * bmapi.
599 */
600#define SYMLINK_MAPS 2
601
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000602STATIC int
603xfs_readlink_bmap(
604 xfs_inode_t *ip,
605 char *link)
606{
607 xfs_mount_t *mp = ip->i_mount;
608 int pathlen = ip->i_d.di_size;
609 int nmaps = SYMLINK_MAPS;
610 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
611 xfs_daddr_t d;
612 int byte_cnt;
613 int n;
614 xfs_buf_t *bp;
615 int error = 0;
616
617 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
618 mval, &nmaps, NULL, NULL);
619 if (error)
620 goto out;
621
622 for (n = 0; n < nmaps; n++) {
623 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
624 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
625
626 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
627 error = XFS_BUF_GETERROR(bp);
628 if (error) {
629 xfs_ioerror_alert("xfs_readlink",
630 ip->i_mount, bp, XFS_BUF_ADDR(bp));
631 xfs_buf_relse(bp);
632 goto out;
633 }
634 if (pathlen < byte_cnt)
635 byte_cnt = pathlen;
636 pathlen -= byte_cnt;
637
638 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
639 xfs_buf_relse(bp);
640 }
641
642 link[ip->i_d.di_size] = '\0';
643 error = 0;
644
645 out:
646 return error;
647}
648
Christoph Hellwig993386c2007-08-28 16:12:30 +1000649int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000651 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000652 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000654 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100658 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (XFS_FORCED_SHUTDOWN(mp))
661 return XFS_ERROR(EIO);
662
663 xfs_ilock(ip, XFS_ILOCK_SHARED);
664
665 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000666 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000668 pathlen = ip->i_d.di_size;
669 if (!pathlen)
670 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000673 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
674 link[pathlen] = '\0';
675 } else {
676 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000679 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return error;
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/*
685 * xfs_fsync
686 *
David Chinner978b7232008-05-19 16:29:46 +1000687 * This is called to sync the inode and its data out to disk. We need to hold
688 * the I/O lock while flushing the data, and the inode lock while flushing the
689 * inode. The inode lock CANNOT be held while flushing the data, so acquire
690 * after we're done with that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000692int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693xfs_fsync(
David Chinner978b7232008-05-19 16:29:46 +1000694 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 xfs_trans_t *tp;
697 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100698 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100700 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
703 return XFS_ERROR(EIO);
704
David Chinner978b7232008-05-19 16:29:46 +1000705 /* capture size updates in I/O completion before writing the inode. */
Christoph Hellwig863890c2008-08-13 16:12:05 +1000706 error = filemap_fdatawait(VFS_I(ip)->i_mapping);
David Chinner978b7232008-05-19 16:29:46 +1000707 if (error)
708 return XFS_ERROR(error);
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +1000709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
David Chinner978b7232008-05-19 16:29:46 +1000711 * We always need to make sure that the required inode state is safe on
712 * disk. The vnode might be clean but we still might need to force the
713 * log because of committed transactions that haven't hit the disk yet.
714 * Likewise, there could be unflushed non-transactional changes to the
715 * inode core that have to go to disk and this requires us to issue
716 * a synchronous transaction to capture these changes correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 *
David Chinner978b7232008-05-19 16:29:46 +1000718 * This code relies on the assumption that if the update_* fields
719 * of the inode are clear and the inode is unpinned then it is clean
720 * and no action is required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 */
722 xfs_ilock(ip, XFS_ILOCK_SHARED);
723
David Chinner978b7232008-05-19 16:29:46 +1000724 if (!(ip->i_update_size || ip->i_update_core)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
David Chinner978b7232008-05-19 16:29:46 +1000726 * Timestamps/size haven't changed since last inode flush or
727 * inode transaction commit. That means either nothing got
728 * written or a transaction committed which caught the updates.
729 * If the latter happened and the transaction hasn't hit the
730 * disk yet, the inode will be still be pinned. If it is,
731 * force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
733
734 xfs_iunlock(ip, XFS_ILOCK_SHARED);
735
736 if (xfs_ipincount(ip)) {
David Chinner978b7232008-05-19 16:29:46 +1000737 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
738 XFS_LOG_FORCE | XFS_LOG_SYNC,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100739 &log_flushed);
740 } else {
741 /*
David Chinner978b7232008-05-19 16:29:46 +1000742 * If the inode is not pinned and nothing has changed
743 * we don't need to flush the cache.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100744 */
745 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 } else {
748 /*
David Chinner978b7232008-05-19 16:29:46 +1000749 * Kick off a transaction to log the inode core to get the
750 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
752 xfs_iunlock(ip, XFS_ILOCK_SHARED);
753 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000754 error = xfs_trans_reserve(tp, 0,
755 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
756 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 xfs_trans_cancel(tp, 0);
758 return error;
759 }
760 xfs_ilock(ip, XFS_ILOCK_EXCL);
761
762 /*
David Chinner978b7232008-05-19 16:29:46 +1000763 * Note - it's possible that we might have pushed ourselves out
764 * of the way during trans_reserve which would flush the inode.
765 * But there's no guarantee that the inode buffer has actually
766 * gone out yet (it's delwri). Plus the buffer could be pinned
767 * anyway if it's part of an inode in another recent
768 * transaction. So we play it safe and fire off the
769 * transaction anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
771 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
772 xfs_trans_ihold(tp, ip);
773 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
David Chinner978b7232008-05-19 16:29:46 +1000774 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000775 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 xfs_iunlock(ip, XFS_ILOCK_EXCL);
778 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100779
780 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
781 /*
782 * If the log write didn't issue an ordered tag we need
783 * to flush the disk cache for the data device now.
784 */
785 if (!log_flushed)
786 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
787
788 /*
789 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000790 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100791 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100792 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100793 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
794 }
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return error;
797}
798
799/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000800 * This is called by xfs_inactive to free any blocks beyond eof
801 * when the link count isn't zero and by xfs_dm_punch_hole() when
802 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000804int
805xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000807 xfs_inode_t *ip,
808 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 xfs_trans_t *tp;
811 int error;
812 xfs_fileoff_t end_fsb;
813 xfs_fileoff_t last_fsb;
814 xfs_filblks_t map_len;
815 int nimaps;
816 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +1000817 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /*
820 * Figure out if there are any blocks beyond the end
821 * of the file. If not, then there is nothing to do.
822 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000823 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
825 map_len = last_fsb - end_fsb;
826 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 nimaps = 1;
830 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000831 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000832 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 xfs_iunlock(ip, XFS_ILOCK_SHARED);
834
835 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100836 (imap.br_startblock != HOLESTARTBLOCK ||
837 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
839 * Attach the dquots to the inode up front.
840 */
841 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +0100842 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /*
845 * There are blocks after the end of file.
846 * Free them up now by truncating the file to
847 * its current size.
848 */
849 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
850
851 /*
852 * Do the xfs_itruncate_start() call before
853 * reserving any log space because
854 * itruncate_start will call into the buffer
855 * cache and we can't
856 * do that within a transaction.
857 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000858 if (use_iolock)
859 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000860 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000861 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000862 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000863 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +1000864 if (use_iolock)
865 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000866 return error;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 error = xfs_trans_reserve(tp, 0,
870 XFS_ITRUNCATE_LOG_RES(mp),
871 0, XFS_TRANS_PERM_LOG_RES,
872 XFS_ITRUNCATE_LOG_COUNT);
873 if (error) {
874 ASSERT(XFS_FORCED_SHUTDOWN(mp));
875 xfs_trans_cancel(tp, 0);
876 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100877 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
880 xfs_ilock(ip, XFS_ILOCK_EXCL);
881 xfs_trans_ijoin(tp, ip,
882 XFS_IOLOCK_EXCL |
883 XFS_ILOCK_EXCL);
884 xfs_trans_ihold(tp, ip);
885
886 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000887 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 XFS_DATA_FORK,
889 0);
890 /*
891 * If we get an error at this point we
892 * simply don't bother truncating the file.
893 */
894 if (error) {
895 xfs_trans_cancel(tp,
896 (XFS_TRANS_RELEASE_LOG_RES |
897 XFS_TRANS_ABORT));
898 } else {
899 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000900 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
David Chinner92dfe8d2007-05-24 15:22:19 +1000902 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
903 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100905 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908/*
909 * Free a symlink that has blocks associated with it.
910 */
911STATIC int
912xfs_inactive_symlink_rmt(
913 xfs_inode_t *ip,
914 xfs_trans_t **tpp)
915{
916 xfs_buf_t *bp;
917 int committed;
918 int done;
919 int error;
920 xfs_fsblock_t first_block;
921 xfs_bmap_free_t free_list;
922 int i;
923 xfs_mount_t *mp;
924 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
925 int nmaps;
926 xfs_trans_t *ntp;
927 int size;
928 xfs_trans_t *tp;
929
930 tp = *tpp;
931 mp = ip->i_mount;
932 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
933 /*
934 * We're freeing a symlink that has some
935 * blocks allocated to it. Free the
936 * blocks here. We know that we've got
937 * either 1 or 2 extents and that we can
938 * free them all in one bunmapi call.
939 */
940 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
941 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
942 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
943 ASSERT(XFS_FORCED_SHUTDOWN(mp));
944 xfs_trans_cancel(tp, 0);
945 *tpp = NULL;
946 return error;
947 }
948 /*
949 * Lock the inode, fix the size, and join it to the transaction.
950 * Hold it so in the normal path, we still have it locked for
951 * the second transaction. In the error paths we need it
952 * held so the cancel won't rele it, see below.
953 */
954 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
955 size = (int)ip->i_d.di_size;
956 ip->i_d.di_size = 0;
957 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
958 xfs_trans_ihold(tp, ip);
959 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
960 /*
961 * Find the block(s) so we can inval and unmap them.
962 */
963 done = 0;
964 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800965 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
967 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000968 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 goto error0;
970 /*
971 * Invalidate the block(s).
972 */
973 for (i = 0; i < nmaps; i++) {
974 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
975 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
976 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
977 xfs_trans_binval(tp, bp);
978 }
979 /*
980 * Unmap the dead block(s) to the free_list.
981 */
982 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000983 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto error1;
985 ASSERT(done);
986 /*
987 * Commit the first transaction. This logs the EFI and the inode.
988 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100989 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 goto error1;
991 /*
992 * The transaction must have been committed, since there were
993 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
994 * The new tp has the extent freeing and EFDs.
995 */
996 ASSERT(committed);
997 /*
998 * The first xact was committed, so add the inode to the new one.
999 * Mark it dirty so it will be logged and moved forward in the log as
1000 * part of every commit.
1001 */
1002 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1003 xfs_trans_ihold(tp, ip);
1004 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1005 /*
1006 * Get a new, empty transaction to return to our caller.
1007 */
1008 ntp = xfs_trans_dup(tp);
1009 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001010 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * If we get an error on the commit here or on the reserve below,
1012 * we need to unlock the inode since the new transaction doesn't
1013 * have the inode attached.
1014 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001015 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 tp = ntp;
1017 if (error) {
1018 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1019 goto error0;
1020 }
1021 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001022 * transaction commit worked ok so we can drop the extra ticket
1023 * reference that we gained in xfs_trans_dup()
1024 */
1025 xfs_log_ticket_put(tp->t_ticket);
1026
1027 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 * Remove the memory for extent descriptions (just bookkeeping).
1029 */
1030 if (ip->i_df.if_bytes)
1031 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1032 ASSERT(ip->i_df.if_bytes == 0);
1033 /*
1034 * Put an itruncate log reservation in the new transaction
1035 * for our caller.
1036 */
1037 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1038 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1039 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1040 goto error0;
1041 }
1042 /*
1043 * Return with the inode locked but not joined to the transaction.
1044 */
1045 *tpp = tp;
1046 return 0;
1047
1048 error1:
1049 xfs_bmap_cancel(&free_list);
1050 error0:
1051 /*
1052 * Have to come here with the inode locked and either
1053 * (held and in the transaction) or (not in the transaction).
1054 * If the inode isn't held then cancel would iput it, but
1055 * that's wrong since this is inactive and the vnode ref
1056 * count is 0 already.
1057 * Cancel won't do anything to the inode if held, but it still
1058 * needs to be locked until the cancel is done, if it was
1059 * joined to the transaction.
1060 */
1061 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1062 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1063 *tpp = NULL;
1064 return error;
1065
1066}
1067
1068STATIC int
1069xfs_inactive_symlink_local(
1070 xfs_inode_t *ip,
1071 xfs_trans_t **tpp)
1072{
1073 int error;
1074
1075 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1076 /*
1077 * We're freeing a symlink which fit into
1078 * the inode. Just free the memory used
1079 * to hold the old symlink.
1080 */
1081 error = xfs_trans_reserve(*tpp, 0,
1082 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1083 0, XFS_TRANS_PERM_LOG_RES,
1084 XFS_ITRUNCATE_LOG_COUNT);
1085
1086 if (error) {
1087 xfs_trans_cancel(*tpp, 0);
1088 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001089 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1092
1093 /*
1094 * Zero length symlinks _can_ exist.
1095 */
1096 if (ip->i_df.if_bytes > 0) {
1097 xfs_idata_realloc(ip,
1098 -(ip->i_df.if_bytes),
1099 XFS_DATA_FORK);
1100 ASSERT(ip->i_df.if_bytes == 0);
1101 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105STATIC int
1106xfs_inactive_attrs(
1107 xfs_inode_t *ip,
1108 xfs_trans_t **tpp)
1109{
1110 xfs_trans_t *tp;
1111 int error;
1112 xfs_mount_t *mp;
1113
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001114 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 tp = *tpp;
1116 mp = ip->i_mount;
1117 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001118 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001120 if (error)
1121 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001124 if (error)
1125 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1128 error = xfs_trans_reserve(tp, 0,
1129 XFS_IFREE_LOG_RES(mp),
1130 0, XFS_TRANS_PERM_LOG_RES,
1131 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001132 if (error)
1133 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 xfs_ilock(ip, XFS_ILOCK_EXCL);
1136 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1137 xfs_trans_ihold(tp, ip);
1138 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1139
1140 ASSERT(ip->i_d.di_anextents == 0);
1141
1142 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001143 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001144
1145error_cancel:
1146 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1147 xfs_trans_cancel(tp, 0);
1148error_unlock:
1149 *tpp = NULL;
1150 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1151 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Christoph Hellwig993386c2007-08-28 16:12:30 +10001154int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001156 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001158 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int error;
1160
Christoph Hellwig42173f62008-04-22 17:33:25 +10001161 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001165 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return 0;
1167
David Chinner2a82b8b2007-07-11 11:09:12 +10001168 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001169 int truncated;
1170
David Chinner2a82b8b2007-07-11 11:09:12 +10001171 /*
1172 * If we are using filestreams, and we have an unlinked
1173 * file that we are processing the last close on, then nothing
1174 * will be able to reopen and write to this file. Purge this
1175 * inode from the filestreams cache so that it doesn't delay
1176 * teardown of the inode.
1177 */
1178 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1179 xfs_filestream_deassociate(ip);
1180
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001181 /*
1182 * If we previously truncated this file and removed old data
1183 * in the process, we want to initiate "early" writeout on
1184 * the last close. This is an attempt to combat the notorious
1185 * NULL files problem which is particularly noticable from a
1186 * truncate down, buffered (re-)write (delalloc), followed by
1187 * a crash. What we are effectively doing here is
1188 * significantly reducing the time window where we'd otherwise
1189 * be exposed to that problem.
1190 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001191 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001192 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001193 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001194 }
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (ip->i_d.di_nlink != 0) {
1197 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001198 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001199 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001201 (!(ip->i_d.di_flags &
1202 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001203 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1204 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001205 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 }
1208
1209 return 0;
1210}
1211
1212/*
1213 * xfs_inactive
1214 *
1215 * This is called when the vnode reference count for the vnode
1216 * goes to zero. If the file has been unlinked, then it must
1217 * now be truncated. Also, we clear all of the read-ahead state
1218 * kept for the inode here since the file is now closed.
1219 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001220int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001222 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001224 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 xfs_fsblock_t first_block;
1226 int committed;
1227 xfs_trans_t *tp;
1228 xfs_mount_t *mp;
1229 int error;
1230 int truncate;
1231
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001232 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * If the inode is already free, then there can be nothing
1236 * to clean up here.
1237 */
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001238 if (ip->i_d.di_mode == 0 || VN_BAD(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 ASSERT(ip->i_df.if_real_bytes == 0);
1240 ASSERT(ip->i_df.if_broot_bytes == 0);
1241 return VN_INACTIVE_CACHE;
1242 }
1243
1244 /*
1245 * Only do a truncate if it's a regular file with
1246 * some actual space in it. It's OK to look at the
1247 * inode's fields without the lock because we're the
1248 * only one with a reference to the inode.
1249 */
1250 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001251 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1252 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1254
1255 mp = ip->i_mount;
1256
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001257 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1258 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 error = 0;
1261
1262 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001263 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto out;
1265
1266 if (ip->i_d.di_nlink != 0) {
1267 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001268 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001269 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001270 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1271 (!(ip->i_d.di_flags &
1272 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1273 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001274 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1275 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001276 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 goto out;
1279 }
1280
1281 ASSERT(ip->i_d.di_nlink == 0);
1282
1283 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001284 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1287 if (truncate) {
1288 /*
1289 * Do the xfs_itruncate_start() call before
1290 * reserving any log space because itruncate_start
1291 * will call into the buffer cache and we can't
1292 * do that within a transaction.
1293 */
1294 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1295
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001296 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1297 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001298 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001299 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1300 return VN_INACTIVE_CACHE;
1301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 error = xfs_trans_reserve(tp, 0,
1304 XFS_ITRUNCATE_LOG_RES(mp),
1305 0, XFS_TRANS_PERM_LOG_RES,
1306 XFS_ITRUNCATE_LOG_COUNT);
1307 if (error) {
1308 /* Don't call itruncate_cleanup */
1309 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1310 xfs_trans_cancel(tp, 0);
1311 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001312 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
1315 xfs_ilock(ip, XFS_ILOCK_EXCL);
1316 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1317 xfs_trans_ihold(tp, ip);
1318
1319 /*
1320 * normally, we have to run xfs_itruncate_finish sync.
1321 * But if filesystem is wsync and we're in the inactive
1322 * path, then we know that nlink == 0, and that the
1323 * xaction that made nlink == 0 is permanently committed
1324 * since xfs_remove runs as a synchronous transaction.
1325 */
1326 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1327 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1328
1329 if (error) {
1330 xfs_trans_cancel(tp,
1331 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1332 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001333 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1336
1337 /*
1338 * If we get an error while cleaning up a
1339 * symlink we bail out.
1340 */
1341 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1342 xfs_inactive_symlink_rmt(ip, &tp) :
1343 xfs_inactive_symlink_local(ip, &tp);
1344
1345 if (error) {
1346 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001347 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
1350 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1351 xfs_trans_ihold(tp, ip);
1352 } else {
1353 error = xfs_trans_reserve(tp, 0,
1354 XFS_IFREE_LOG_RES(mp),
1355 0, XFS_TRANS_PERM_LOG_RES,
1356 XFS_INACTIVE_LOG_COUNT);
1357 if (error) {
1358 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1359 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001360 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362
1363 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1364 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1365 xfs_trans_ihold(tp, ip);
1366 }
1367
1368 /*
1369 * If there are attributes associated with the file
1370 * then blow them away now. The code calls a routine
1371 * that recursively deconstructs the attribute fork.
1372 * We need to just commit the current transaction
1373 * because we can't use it for xfs_attr_inactive().
1374 */
1375 if (ip->i_d.di_anextents > 0) {
1376 error = xfs_inactive_attrs(ip, &tp);
1377 /*
1378 * If we got an error, the transaction is already
1379 * cancelled, and the inode is unlocked. Just get out.
1380 */
1381 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001382 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 } else if (ip->i_afp) {
1384 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1385 }
1386
1387 /*
1388 * Free the inode.
1389 */
1390 XFS_BMAP_INIT(&free_list, &first_block);
1391 error = xfs_ifree(tp, ip, &free_list);
1392 if (error) {
1393 /*
1394 * If we fail to free the inode, shut down. The cancel
1395 * might do that, we need to make sure. Otherwise the
1396 * inode might be lost for a long time or forever.
1397 */
1398 if (!XFS_FORCED_SHUTDOWN(mp)) {
1399 cmn_err(CE_NOTE,
1400 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1401 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001402 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1405 } else {
1406 /*
1407 * Credit the quota account(s). The inode is gone.
1408 */
1409 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1410
1411 /*
David Chinner78e9da72008-04-10 12:24:17 +10001412 * Just ignore errors at this point. There is nothing we can
1413 * do except to try to keep going. Make sure it's not a silent
1414 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 */
David Chinner78e9da72008-04-10 12:24:17 +10001416 error = xfs_bmap_finish(&tp, &free_list, &committed);
1417 if (error)
1418 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1419 "xfs_bmap_finish() returned error %d", error);
1420 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1421 if (error)
1422 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1423 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425 /*
1426 * Release the dquots held by inode, if any.
1427 */
1428 XFS_QM_DQDETACH(mp, ip);
1429
1430 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1431
1432 out:
1433 return VN_INACTIVE_CACHE;
1434}
1435
Barry Naujok384f3ce2008-05-21 16:58:22 +10001436/*
1437 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1438 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1439 * ci_name->name will point to a the actual name (caller must free) or
1440 * will be set to NULL if an exact match is found.
1441 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001442int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001444 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001445 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001446 xfs_inode_t **ipp,
1447 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001449 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int error;
1451 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001453 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1456 return XFS_ERROR(EIO);
1457
1458 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001459 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001461
1462 if (error)
1463 goto out;
1464
1465 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1466 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001467 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001468
1469 xfs_itrace_ref(*ipp);
1470 return 0;
1471
Barry Naujok384f3ce2008-05-21 16:58:22 +10001472out_free_name:
1473 if (ci_name)
1474 kmem_free(ci_name->name);
1475out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001476 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return error;
1478}
1479
Christoph Hellwig993386c2007-08-28 16:12:30 +10001480int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001482 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001483 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001484 mode_t mode,
1485 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001486 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 cred_t *credp)
1488{
Barry Naujok556b8b12008-04-10 12:22:07 +10001489 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001490 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 xfs_trans_t *tp;
Barry Naujok556b8b12008-04-10 12:22:07 +10001492 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 xfs_bmap_free_t free_list;
1494 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001495 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 int dm_event_sent = 0;
1497 uint cancel_flags;
1498 int committed;
1499 xfs_prid_t prid;
1500 struct xfs_dquot *udqp, *gdqp;
1501 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001503 ASSERT(!*ipp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001504 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001506 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001508 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001509 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001510 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 if (error)
1513 return error;
1514 dm_event_sent = 1;
1515 }
1516
1517 if (XFS_FORCED_SHUTDOWN(mp))
1518 return XFS_ERROR(EIO);
1519
1520 /* Return through std_return after this point. */
1521
1522 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001523 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1524 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 else
1526 prid = (xfs_prid_t)dfltprid;
1527
1528 /*
1529 * Make sure that we have allocated dquot(s) on disk.
1530 */
1531 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01001532 current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1534 if (error)
1535 goto std_return;
1536
1537 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1540 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001541 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * Initially assume that the file does not exist and
1544 * reserve the resources for that case. If that is not
1545 * the case we'll drop the one we have and get a more
1546 * appropriate transaction later.
1547 */
1548 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1549 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1550 if (error == ENOSPC) {
1551 resblks = 0;
1552 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1553 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1554 }
1555 if (error) {
1556 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto error_return;
1558 }
1559
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001560 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001561 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 XFS_BMAP_INIT(&free_list, &first_block);
1564
1565 ASSERT(ip == NULL);
1566
1567 /*
1568 * Reserve disk quota and the inode.
1569 */
1570 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1571 if (error)
1572 goto error_return;
1573
Barry Naujok556b8b12008-04-10 12:22:07 +10001574 error = xfs_dir_canenter(tp, dp, name, resblks);
1575 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001577 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 rdev, credp, prid, resblks > 0,
1579 &ip, &committed);
1580 if (error) {
1581 if (error == ENOSPC)
1582 goto error_return;
1583 goto abort_return;
1584 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001585 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /*
1588 * At this point, we've gotten a newly allocated inode.
1589 * It is locked (and joined to the transaction).
1590 */
1591
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001592 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001595 * Now we join the directory inode to the transaction. We do not do it
1596 * earlier because xfs_dir_ialloc might commit the previous transaction
1597 * (and release all the locks). An error from here on will result in
1598 * the transaction cancel unlocking dp so don't do it explicitly in the
1599 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001601 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001603 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Barry Naujok556b8b12008-04-10 12:22:07 +10001605 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001606 &first_block, &free_list, resblks ?
1607 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (error) {
1609 ASSERT(error != ENOSPC);
1610 goto abort_return;
1611 }
1612 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1613 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1614
1615 /*
1616 * If this is a synchronous mount, make sure that the
1617 * create transaction goes to disk before returning to
1618 * the user.
1619 */
1620 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1621 xfs_trans_set_sync(tp);
1622 }
1623
1624 dp->i_gen++;
1625
1626 /*
1627 * Attach the dquot(s) to the inodes and modify them incore.
1628 * These ids of the inode couldn't have changed since the new
1629 * inode has been locked ever since it was created.
1630 */
1631 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1632
1633 /*
1634 * xfs_trans_commit normally decrements the vnode ref count
1635 * when it unlocks the inode. Since we want to return the
1636 * vnode to the caller, we bump the vnode ref count now.
1637 */
1638 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001640 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (error) {
1642 xfs_bmap_cancel(&free_list);
1643 goto abort_rele;
1644 }
1645
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001646 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (error) {
1648 IRELE(ip);
1649 tp = NULL;
1650 goto error_return;
1651 }
1652
1653 XFS_QM_DQRELE(mp, udqp);
1654 XFS_QM_DQRELE(mp, gdqp);
1655
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001656 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 /* Fallthrough to std_return with error = 0 */
1659
1660std_return:
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001661 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10001662 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001664 dp, DM_RIGHT_NULL,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001665 *ipp ? ip : NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001666 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001667 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669 return error;
1670
1671 abort_return:
1672 cancel_flags |= XFS_TRANS_ABORT;
1673 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Jesper Juhl014c2542006-01-15 02:37:08 +01001675 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (tp != NULL)
1677 xfs_trans_cancel(tp, cancel_flags);
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 XFS_QM_DQRELE(mp, udqp);
1680 XFS_QM_DQRELE(mp, gdqp);
1681
Christoph Hellwig993386c2007-08-28 16:12:30 +10001682 if (unlock_dp_on_error)
1683 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 goto std_return;
1686
1687 abort_rele:
1688 /*
1689 * Wait until after the current transaction is aborted to
1690 * release the inode. This prevents recursive transactions
1691 * and deadlocks from xfs_inactive.
1692 */
1693 cancel_flags |= XFS_TRANS_ABORT;
1694 xfs_trans_cancel(tp, cancel_flags);
1695 IRELE(ip);
1696
1697 XFS_QM_DQRELE(mp, udqp);
1698 XFS_QM_DQRELE(mp, gdqp);
1699
1700 goto std_return;
1701}
1702
1703#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704int xfs_locked_n;
1705int xfs_small_retries;
1706int xfs_middle_retries;
1707int xfs_lots_retries;
1708int xfs_lock_delays;
1709#endif
1710
1711/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001712 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1713 * a different value
1714 */
1715static inline int
1716xfs_lock_inumorder(int lock_mode, int subclass)
1717{
1718 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001719 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001720 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001721 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001722
1723 return lock_mode;
1724}
1725
1726/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 * The following routine will lock n inodes in exclusive mode.
1728 * We assume the caller calls us with the inodes in i_ino order.
1729 *
1730 * We need to detect deadlock where an inode that we lock
1731 * is in the AIL and we start waiting for another inode that is locked
1732 * by a thread in a long running transaction (such as truncate). This can
1733 * result in deadlock since the long running trans might need to wait
1734 * for the inode we just locked in order to push the tail and free space
1735 * in the log.
1736 */
1737void
1738xfs_lock_inodes(
1739 xfs_inode_t **ips,
1740 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 uint lock_mode)
1742{
1743 int attempts = 0, i, j, try_lock;
1744 xfs_log_item_t *lp;
1745
1746 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1747
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001748 try_lock = 0;
1749 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751again:
1752 for (; i < inodes; i++) {
1753 ASSERT(ips[i]);
1754
1755 if (i && (ips[i] == ips[i-1])) /* Already locked */
1756 continue;
1757
1758 /*
1759 * If try_lock is not set yet, make sure all locked inodes
1760 * are not in the AIL.
1761 * If any are, set try_lock to be used later.
1762 */
1763
1764 if (!try_lock) {
1765 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1766 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1767 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1768 try_lock++;
1769 }
1770 }
1771 }
1772
1773 /*
1774 * If any of the previous locks we have locked is in the AIL,
1775 * we must TRY to get the second and subsequent locks. If
1776 * we can't get any, we must release all we have
1777 * and try again.
1778 */
1779
1780 if (try_lock) {
1781 /* try_lock must be 0 if i is 0. */
1782 /*
1783 * try_lock means we have an inode locked
1784 * that is in the AIL.
1785 */
1786 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001787 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 attempts++;
1789
1790 /*
1791 * Unlock all previous guys and try again.
1792 * xfs_iunlock will try to push the tail
1793 * if the inode is in the AIL.
1794 */
1795
1796 for(j = i - 1; j >= 0; j--) {
1797
1798 /*
1799 * Check to see if we've already
1800 * unlocked this one.
1801 * Not the first one going back,
1802 * and the inode ptr is the same.
1803 */
1804 if ((j != (i - 1)) && ips[j] ==
1805 ips[j+1])
1806 continue;
1807
1808 xfs_iunlock(ips[j], lock_mode);
1809 }
1810
1811 if ((attempts % 5) == 0) {
1812 delay(1); /* Don't just spin the CPU */
1813#ifdef DEBUG
1814 xfs_lock_delays++;
1815#endif
1816 }
1817 i = 0;
1818 try_lock = 0;
1819 goto again;
1820 }
1821 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001822 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 }
1825
1826#ifdef DEBUG
1827 if (attempts) {
1828 if (attempts < 5) xfs_small_retries++;
1829 else if (attempts < 100) xfs_middle_retries++;
1830 else xfs_lots_retries++;
1831 } else {
1832 xfs_locked_n++;
1833 }
1834#endif
1835}
1836
David Chinnerf9114eb2008-09-17 16:51:21 +10001837/*
1838 * xfs_lock_two_inodes() can only be used to lock one type of lock
1839 * at a time - the iolock or the ilock, but not both at once. If
1840 * we lock both at once, lockdep will report false positives saying
1841 * we have violated locking orders.
1842 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001843void
1844xfs_lock_two_inodes(
1845 xfs_inode_t *ip0,
1846 xfs_inode_t *ip1,
1847 uint lock_mode)
1848{
1849 xfs_inode_t *temp;
1850 int attempts = 0;
1851 xfs_log_item_t *lp;
1852
David Chinnerf9114eb2008-09-17 16:51:21 +10001853 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1854 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001855 ASSERT(ip0->i_ino != ip1->i_ino);
1856
1857 if (ip0->i_ino > ip1->i_ino) {
1858 temp = ip0;
1859 ip0 = ip1;
1860 ip1 = temp;
1861 }
1862
1863 again:
1864 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1865
1866 /*
1867 * If the first lock we have locked is in the AIL, we must TRY to get
1868 * the second lock. If we can't get it, we must release the first one
1869 * and try again.
1870 */
1871 lp = (xfs_log_item_t *)ip0->i_itemp;
1872 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1873 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1874 xfs_iunlock(ip0, lock_mode);
1875 if ((++attempts % 5) == 0)
1876 delay(1); /* Don't just spin the CPU */
1877 goto again;
1878 }
1879 } else {
1880 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1881 }
1882}
1883
Christoph Hellwig993386c2007-08-28 16:12:30 +10001884int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001886 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001887 struct xfs_name *name,
1888 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001890 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001892 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 int error = 0;
1894 xfs_bmap_free_t free_list;
1895 xfs_fsblock_t first_block;
1896 int cancel_flags;
1897 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 int link_zero;
1899 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001900 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001902 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001903 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (XFS_FORCED_SHUTDOWN(mp))
1906 return XFS_ERROR(EIO);
1907
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001908 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001909 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1910 NULL, DM_RIGHT_NULL, name->name, NULL,
1911 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (error)
1913 return error;
1914 }
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 error = XFS_QM_DQATTACH(mp, dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001917 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001920 error = XFS_QM_DQATTACH(mp, ip, 0);
1921 if (error)
1922 goto std_return;
1923
1924 if (is_dir) {
1925 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1926 log_count = XFS_DEFAULT_LOG_COUNT;
1927 } else {
1928 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1929 log_count = XFS_REMOVE_LOG_COUNT;
1930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /*
1934 * We try to get the real space reservation first,
1935 * allowing for directory btree deletion(s) implying
1936 * possible bmap insert(s). If we can't get the space
1937 * reservation then we use 0 instead, and avoid the bmap
1938 * btree insert(s) in the directory code by, if the bmap
1939 * insert tries to happen, instead trimming the LAST
1940 * block from the directory.
1941 */
1942 resblks = XFS_REMOVE_SPACE_RES(mp);
1943 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001944 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (error == ENOSPC) {
1946 resblks = 0;
1947 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001948 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950 if (error) {
1951 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001952 cancel_flags = 0;
1953 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001956 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /*
1959 * At this point, we've gotten both the directory and the entry
1960 * inodes locked.
1961 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001962 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10001964
1965 IHOLD(dp);
1966 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001969 * If we're removing a directory perform some additional validation.
1970 */
1971 if (is_dir) {
1972 ASSERT(ip->i_d.di_nlink >= 2);
1973 if (ip->i_d.di_nlink != 2) {
1974 error = XFS_ERROR(ENOTEMPTY);
1975 goto out_trans_cancel;
1976 }
1977 if (!xfs_dir_isempty(ip)) {
1978 error = XFS_ERROR(ENOTEMPTY);
1979 goto out_trans_cancel;
1980 }
1981 }
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 XFS_BMAP_INIT(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001984 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001985 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (error) {
1987 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001988 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
1990 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1991
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001992 /*
1993 * Bump the in memory generation count on the parent
1994 * directory so that other can know that it has changed.
1995 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 dp->i_gen++;
1997 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1998
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001999 if (is_dir) {
2000 /*
2001 * Drop the link from ip's "..".
2002 */
2003 error = xfs_droplink(tp, dp);
2004 if (error)
2005 goto out_bmap_cancel;
2006
2007 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11002008 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002009 */
2010 error = xfs_droplink(tp, ip);
2011 if (error)
2012 goto out_bmap_cancel;
2013 } else {
2014 /*
2015 * When removing a non-directory we need to log the parent
2016 * inode here for the i_gen update. For a directory this is
2017 * done implicitly by the xfs_droplink call for the ".." entry.
2018 */
2019 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002022 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11002023 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002024 */
2025 error = xfs_droplink(tp, ip);
2026 if (error)
2027 goto out_bmap_cancel;
2028
2029 /*
2030 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 * we are in the transaction.
2032 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002033 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 * If this is a synchronous mount, make sure that the
2037 * remove transaction goes to disk before returning to
2038 * the user.
2039 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002040 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002043 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002044 if (error)
2045 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002047 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002048 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002052 * If we are using filestreams, kill the stream association.
2053 * If the file is still open it may get a new one but that
2054 * will get killed on last close in xfs_close() so we don't
2055 * have to worry about that.
2056 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002057 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10002058 xfs_filestream_deassociate(ip);
2059
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002060 xfs_itrace_exit(ip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002061 xfs_itrace_exit(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002064 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002065 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2066 NULL, DM_RIGHT_NULL, name->name, NULL,
2067 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return error;
2071
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002072 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 xfs_bmap_cancel(&free_list);
2074 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002075 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 xfs_trans_cancel(tp, cancel_flags);
2077 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Christoph Hellwig993386c2007-08-28 16:12:30 +10002080int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002082 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002083 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002084 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002086 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 int error;
2089 xfs_bmap_free_t free_list;
2090 xfs_fsblock_t first_block;
2091 int cancel_flags;
2092 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002095 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002096 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002098 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (XFS_FORCED_SHUTDOWN(mp))
2101 return XFS_ERROR(EIO);
2102
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002103 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002105 tdp, DM_RIGHT_NULL,
2106 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002107 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (error)
2109 return error;
2110 }
2111
2112 /* Return through std_return after this point. */
2113
2114 error = XFS_QM_DQATTACH(mp, sip, 0);
2115 if (!error && sip != tdp)
2116 error = XFS_QM_DQATTACH(mp, tdp, 0);
2117 if (error)
2118 goto std_return;
2119
2120 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2121 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002122 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2124 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2125 if (error == ENOSPC) {
2126 resblks = 0;
2127 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2128 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2129 }
2130 if (error) {
2131 cancel_flags = 0;
2132 goto error_return;
2133 }
2134
Christoph Hellwige1cccd92008-08-13 16:18:07 +10002135 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 /*
2138 * Increment vnode ref counts since xfs_trans_commit &
2139 * xfs_trans_cancel will both unlock the inodes and
2140 * decrement the associated ref counts.
2141 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002142 IHOLD(sip);
2143 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2145 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2146
2147 /*
2148 * If the source has too many links, we can't make any more to it.
2149 */
2150 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2151 error = XFS_ERROR(EMLINK);
2152 goto error_return;
2153 }
2154
Nathan Scott365ca832005-06-21 15:39:12 +10002155 /*
2156 * If we are using project inheritance, we only allow hard link
2157 * creation in our tree when the project IDs are the same; else
2158 * the tree quota mechanism could be circumvented.
2159 */
2160 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2161 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002162 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002163 goto error_return;
2164 }
2165
Barry Naujok556b8b12008-04-10 12:22:07 +10002166 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2167 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 goto error_return;
2169
2170 XFS_BMAP_INIT(&free_list, &first_block);
2171
Barry Naujok556b8b12008-04-10 12:22:07 +10002172 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2173 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (error)
2175 goto abort_return;
2176 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2177 tdp->i_gen++;
2178 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2179
2180 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002181 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 /*
2185 * If this is a synchronous mount, make sure that the
2186 * link transaction goes to disk before returning to
2187 * the user.
2188 */
2189 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2190 xfs_trans_set_sync(tp);
2191 }
2192
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002193 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (error) {
2195 xfs_bmap_cancel(&free_list);
2196 goto abort_return;
2197 }
2198
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002199 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002200 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* Fall through to std_return with error = 0. */
2204std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002205 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002207 tdp, DM_RIGHT_NULL,
2208 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002209 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211 return error;
2212
2213 abort_return:
2214 cancel_flags |= XFS_TRANS_ABORT;
2215 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 error_return:
2218 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 goto std_return;
2220}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002221
2222
Christoph Hellwig993386c2007-08-28 16:12:30 +10002223int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002225 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002226 struct xfs_name *dir_name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002227 mode_t mode,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002228 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 cred_t *credp)
2230{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002231 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 xfs_inode_t *cdp; /* inode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 int cancel_flags;
2235 int error;
2236 int committed;
2237 xfs_bmap_free_t free_list;
2238 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002239 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 boolean_t created = B_FALSE;
2241 int dm_event_sent = 0;
2242 xfs_prid_t prid;
2243 struct xfs_dquot *udqp, *gdqp;
2244 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 if (XFS_FORCED_SHUTDOWN(mp))
2247 return XFS_ERROR(EIO);
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002251 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002253 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002254 DM_RIGHT_NULL, dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002255 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (error)
2257 return error;
2258 dm_event_sent = 1;
2259 }
2260
2261 /* Return through std_return after this point. */
2262
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002263 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 mp = dp->i_mount;
2266 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002267 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2268 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 else
2270 prid = (xfs_prid_t)dfltprid;
2271
2272 /*
2273 * Make sure that we have allocated dquot(s) on disk.
2274 */
2275 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01002276 current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2278 if (error)
2279 goto std_return;
2280
2281 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2282 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002283 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2285 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2286 if (error == ENOSPC) {
2287 resblks = 0;
2288 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2289 XFS_TRANS_PERM_LOG_RES,
2290 XFS_MKDIR_LOG_COUNT);
2291 }
2292 if (error) {
2293 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 goto error_return;
2295 }
2296
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002297 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002298 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 /*
2301 * Check for directory link count overflow.
2302 */
2303 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2304 error = XFS_ERROR(EMLINK);
2305 goto error_return;
2306 }
2307
2308 /*
2309 * Reserve disk quota and the inode.
2310 */
2311 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2312 if (error)
2313 goto error_return;
2314
Barry Naujok556b8b12008-04-10 12:22:07 +10002315 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2316 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 goto error_return;
2318 /*
2319 * create the directory inode.
2320 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002321 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 0, credp, prid, resblks > 0,
2323 &cdp, NULL);
2324 if (error) {
2325 if (error == ENOSPC)
2326 goto error_return;
2327 goto abort_return;
2328 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002329 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 /*
2332 * Now we add the directory inode to the transaction.
2333 * We waited until now since xfs_dir_ialloc might start
2334 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002335 * earlier, the locks might have gotten released. An error
2336 * from here on will result in the transaction cancel
2337 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002339 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002341 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 XFS_BMAP_INIT(&free_list, &first_block);
2344
Barry Naujok556b8b12008-04-10 12:22:07 +10002345 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2346 &first_block, &free_list, resblks ?
2347 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (error) {
2349 ASSERT(error != ENOSPC);
2350 goto error1;
2351 }
2352 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2353
2354 /*
2355 * Bump the in memory version number of the parent directory
2356 * so that other processes accessing it will recognize that
2357 * the directory has changed.
2358 */
2359 dp->i_gen++;
2360
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002361 error = xfs_dir_init(tp, cdp, dp);
2362 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 cdp->i_gen = 1;
2366 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002367 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 created = B_TRUE;
2371
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002372 *ipp = cdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 IHOLD(cdp);
2374
2375 /*
2376 * Attach the dquots to the new inode and modify the icount incore.
2377 */
2378 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2379
2380 /*
2381 * If this is a synchronous mount, make sure that the
2382 * mkdir transaction goes to disk before returning to
2383 * the user.
2384 */
2385 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2386 xfs_trans_set_sync(tp);
2387 }
2388
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002389 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (error) {
2391 IRELE(cdp);
2392 goto error2;
2393 }
2394
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002395 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 XFS_QM_DQRELE(mp, udqp);
2397 XFS_QM_DQRELE(mp, gdqp);
2398 if (error) {
2399 IRELE(cdp);
2400 }
2401
2402 /* Fall through to std_return with error = 0 or errno from
2403 * xfs_trans_commit. */
2404
2405std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002406 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002407 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002409 dp, DM_RIGHT_NULL,
2410 created ? cdp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002412 dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002413 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415 return error;
2416
2417 error2:
2418 error1:
2419 xfs_bmap_cancel(&free_list);
2420 abort_return:
2421 cancel_flags |= XFS_TRANS_ABORT;
2422 error_return:
2423 xfs_trans_cancel(tp, cancel_flags);
2424 XFS_QM_DQRELE(mp, udqp);
2425 XFS_QM_DQRELE(mp, gdqp);
2426
Christoph Hellwig993386c2007-08-28 16:12:30 +10002427 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 goto std_return;
2431}
2432
Christoph Hellwig993386c2007-08-28 16:12:30 +10002433int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002435 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002436 struct xfs_name *link_name,
2437 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002438 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002439 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 cred_t *credp)
2441{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002442 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 xfs_inode_t *ip;
2445 int error;
2446 int pathlen;
2447 xfs_bmap_free_t free_list;
2448 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002449 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 uint cancel_flags;
2451 int committed;
2452 xfs_fileoff_t first_fsb;
2453 xfs_filblks_t fs_blocks;
2454 int nmaps;
2455 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2456 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002457 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 int byte_cnt;
2459 int n;
2460 xfs_buf_t *bp;
2461 xfs_prid_t prid;
2462 struct xfs_dquot *udqp, *gdqp;
2463 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Christoph Hellwig3937be52008-03-06 13:46:19 +11002465 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 error = 0;
2467 ip = NULL;
2468 tp = NULL;
2469
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002470 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 if (XFS_FORCED_SHUTDOWN(mp))
2473 return XFS_ERROR(EIO);
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 /*
2476 * Check component lengths of the target path name.
2477 */
2478 pathlen = strlen(target_path);
2479 if (pathlen >= MAXPATHLEN) /* total string too long */
2480 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002482 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002483 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002485 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (error)
2487 return error;
2488 }
2489
2490 /* Return through std_return after this point. */
2491
2492 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002493 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2494 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 else
2496 prid = (xfs_prid_t)dfltprid;
2497
2498 /*
2499 * Make sure that we have allocated dquot(s) on disk.
2500 */
2501 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01002502 current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2504 if (error)
2505 goto std_return;
2506
2507 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2508 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2509 /*
2510 * The symlink will fit into the inode data fork?
2511 * There can't be any attributes so we get the whole variable part.
2512 */
2513 if (pathlen <= XFS_LITINO(mp))
2514 fs_blocks = 0;
2515 else
2516 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002517 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2519 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2520 if (error == ENOSPC && fs_blocks == 0) {
2521 resblks = 0;
2522 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2523 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2524 }
2525 if (error) {
2526 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 goto error_return;
2528 }
2529
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002530 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002531 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 /*
2534 * Check whether the directory allows new symlinks or not.
2535 */
2536 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2537 error = XFS_ERROR(EPERM);
2538 goto error_return;
2539 }
2540
2541 /*
2542 * Reserve disk quota : blocks and inode.
2543 */
2544 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2545 if (error)
2546 goto error_return;
2547
2548 /*
2549 * Check for ability to enter directory entry, if no space reserved.
2550 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002551 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2552 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 goto error_return;
2554 /*
2555 * Initialize the bmap freelist prior to calling either
2556 * bmapi or the directory create code.
2557 */
2558 XFS_BMAP_INIT(&free_list, &first_block);
2559
2560 /*
2561 * Allocate an inode for the symlink.
2562 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002563 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 1, 0, credp, prid, resblks > 0, &ip, NULL);
2565 if (error) {
2566 if (error == ENOSPC)
2567 goto error_return;
2568 goto error1;
2569 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002570 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Christoph Hellwig993386c2007-08-28 16:12:30 +10002572 /*
2573 * An error after we've joined dp to the transaction will result in the
2574 * transaction cancel unlocking dp so don't do it explicitly in the
2575 * error path.
2576 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002577 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002579 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 /*
2582 * Also attach the dquot(s) to it, if applicable.
2583 */
2584 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2585
2586 if (resblks)
2587 resblks -= XFS_IALLOC_SPACE_RES(mp);
2588 /*
2589 * If the symlink will fit into the inode, write it inline.
2590 */
2591 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2592 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2593 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2594 ip->i_d.di_size = pathlen;
2595
2596 /*
2597 * The inode was initially created in extent format.
2598 */
2599 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2600 ip->i_df.if_flags |= XFS_IFINLINE;
2601
2602 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2603 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2604
2605 } else {
2606 first_fsb = 0;
2607 nmaps = SYMLINK_MAPS;
2608
2609 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2610 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2611 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002612 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (error) {
2614 goto error1;
2615 }
2616
2617 if (resblks)
2618 resblks -= fs_blocks;
2619 ip->i_d.di_size = pathlen;
2620 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2621
2622 cur_chunk = target_path;
2623 for (n = 0; n < nmaps; n++) {
2624 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2625 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2626 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2627 BTOBB(byte_cnt), 0);
2628 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2629 if (pathlen < byte_cnt) {
2630 byte_cnt = pathlen;
2631 }
2632 pathlen -= byte_cnt;
2633
2634 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2635 cur_chunk += byte_cnt;
2636
2637 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2638 }
2639 }
2640
2641 /*
2642 * Create the directory entry for the symlink.
2643 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002644 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2645 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002646 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2649 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2650
2651 /*
2652 * Bump the in memory version number of the parent directory
2653 * so that other processes accessing it will recognize that
2654 * the directory has changed.
2655 */
2656 dp->i_gen++;
2657
2658 /*
2659 * If this is a synchronous mount, make sure that the
2660 * symlink transaction goes to disk before returning to
2661 * the user.
2662 */
2663 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2664 xfs_trans_set_sync(tp);
2665 }
2666
2667 /*
2668 * xfs_trans_commit normally decrements the vnode ref count
2669 * when it unlocks the inode. Since we want to return the
2670 * vnode to the caller, we bump the vnode ref count now.
2671 */
2672 IHOLD(ip);
2673
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002674 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (error) {
2676 goto error2;
2677 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002678 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 XFS_QM_DQRELE(mp, udqp);
2680 XFS_QM_DQRELE(mp, gdqp);
2681
2682 /* Fall through to std_return with error = 0 or errno from
2683 * xfs_trans_commit */
2684std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10002685 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002687 dp, DM_RIGHT_NULL,
2688 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002689 DM_RIGHT_NULL, link_name->name,
2690 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
2692
Christoph Hellwig3937be52008-03-06 13:46:19 +11002693 if (!error)
2694 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return error;
2696
2697 error2:
2698 IRELE(ip);
2699 error1:
2700 xfs_bmap_cancel(&free_list);
2701 cancel_flags |= XFS_TRANS_ABORT;
2702 error_return:
2703 xfs_trans_cancel(tp, cancel_flags);
2704 XFS_QM_DQRELE(mp, udqp);
2705 XFS_QM_DQRELE(mp, gdqp);
2706
Christoph Hellwig993386c2007-08-28 16:12:30 +10002707 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 goto std_return;
2711}
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002715 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 int flags)
2717{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002718 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 int error = 0;
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (XFS_FORCED_SHUTDOWN(mp))
2722 return XFS_ERROR(EIO);
2723
2724 /*
2725 * Bypass inodes which have already been cleaned by
2726 * the inode flush clustering code inside xfs_iflush
2727 */
David Chinner33540402008-03-06 13:43:59 +11002728 if (xfs_inode_clean(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return 0;
2730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 /*
2732 * We make this non-blocking if the inode is contended,
2733 * return EAGAIN to indicate to the caller that they
2734 * did not succeed. This prevents the flush path from
2735 * blocking on inodes inside another operation right
2736 * now, they get caught later by xfs_sync.
2737 */
David Chinnera3f74ff2008-03-06 13:43:42 +11002738 if (flags & FLUSH_SYNC) {
2739 xfs_ilock(ip, XFS_ILOCK_SHARED);
2740 xfs_iflock(ip);
2741 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
2742 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
2743 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return EAGAIN;
2745 }
David Chinnera3f74ff2008-03-06 13:43:42 +11002746 } else {
2747 return EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
2749
David Chinnera3f74ff2008-03-06 13:43:42 +11002750 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
2751 : XFS_IFLUSH_ASYNC_NOBLOCK);
2752 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return error;
2755}
2756
Christoph Hellwig993386c2007-08-28 16:12:30 +10002757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002759xfs_set_dmattrs(
2760 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002762 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002764 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 int error;
2767
2768 if (!capable(CAP_SYS_ADMIN))
2769 return XFS_ERROR(EPERM);
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (XFS_FORCED_SHUTDOWN(mp))
2772 return XFS_ERROR(EIO);
2773
2774 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2775 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2776 if (error) {
2777 xfs_trans_cancel(tp, 0);
2778 return error;
2779 }
2780 xfs_ilock(ip, XFS_ILOCK_EXCL);
2781 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2782
Christoph Hellwig613d7042007-10-11 17:44:08 +10002783 ip->i_d.di_dmevmask = evmask;
2784 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2787 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002788 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790 return error;
2791}
2792
Christoph Hellwig993386c2007-08-28 16:12:30 +10002793int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002795 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002798 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002800 ASSERT(!VN_MAPPED(VFS_I(ip)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 /* bad inode, get out here ASAP */
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002803 if (VN_BAD(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 xfs_ireclaim(ip);
2805 return 0;
2806 }
2807
Christoph Hellwigb677c212007-08-29 11:46:28 +10002808 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10002810 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11002812 /*
2813 * Make sure the atime in the XFS inode is correct before freeing the
2814 * Linux inode.
2815 */
2816 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
David Chinner4c606582006-11-11 18:05:00 +11002818 /*
2819 * If we have nothing to flush with this inode then complete the
2820 * teardown now, otherwise break the link between the xfs inode and the
2821 * linux inode and clean up the xfs inode later. This avoids flushing
2822 * the inode to disk during the delete operation itself.
2823 *
2824 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2825 * first to ensure that xfs_iunpin() will never see an xfs inode
2826 * that has a linux inode being reclaimed. Synchronisation is provided
2827 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 */
2829 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2830 xfs_ilock(ip, XFS_ILOCK_EXCL);
2831 xfs_iflock(ip);
David Chinnerbf904242008-10-30 17:36:14 +11002832 xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner1dc33182008-10-30 17:37:15 +11002833 return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
David Chinner11654512008-10-30 17:37:49 +11002835 xfs_inode_set_reclaim_tag(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return 0;
2837}
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839/*
2840 * xfs_alloc_file_space()
2841 * This routine allocates disk space for the given file.
2842 *
2843 * If alloc_type == 0, this request is for an ALLOCSP type
2844 * request which will change the file size. In this case, no
2845 * DMAPI event will be generated by the call. A TRUNCATE event
2846 * will be generated later by xfs_setattr.
2847 *
2848 * If alloc_type != 0, this request is for a RESVSP type
2849 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2850 * lower block boundary byte address is less than the file's
2851 * length.
2852 *
2853 * RETURNS:
2854 * 0 on success
2855 * errno on error
2856 *
2857 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002858STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859xfs_alloc_file_space(
2860 xfs_inode_t *ip,
2861 xfs_off_t offset,
2862 xfs_off_t len,
2863 int alloc_type,
2864 int attr_flags)
2865{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002866 xfs_mount_t *mp = ip->i_mount;
2867 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 xfs_filblks_t allocated_fsb;
2869 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002870 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002872 xfs_fsblock_t firstfsb;
2873 int nimaps;
2874 int bmapi_flag;
2875 int quota_flag;
2876 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002878 xfs_bmbt_irec_t imaps[1], *imapp;
2879 xfs_bmap_free_t free_list;
2880 uint qblocks, resblks, resrtextents;
2881 int committed;
2882 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002884 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
2886 if (XFS_FORCED_SHUTDOWN(mp))
2887 return XFS_ERROR(EIO);
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2890 return error;
2891
2892 if (len <= 0)
2893 return XFS_ERROR(EINVAL);
2894
David Chinner957d0eb2007-06-18 16:50:37 +10002895 rt = XFS_IS_REALTIME_INODE(ip);
2896 extsz = xfs_get_extsz_hint(ip);
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11002900 nimaps = 1;
2901 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2903 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2904
2905 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002906 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002907 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002908 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 xfs_off_t end_dmi_offset;
2910
2911 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002912 if (end_dmi_offset > ip->i_size)
2913 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002914 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2915 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002917 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
2919
2920 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002921 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 */
2923retry:
2924 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002925 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Nathan Scottdd9f4382006-01-11 15:28:28 +11002927 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11002928 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002929 */
2930 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002932 do_div(s, extsz);
2933 s *= extsz;
2934 e = startoffset_fsb + allocatesize_fsb;
2935 if ((temp = do_mod(startoffset_fsb, extsz)))
2936 e += temp;
2937 if ((temp = do_mod(e, extsz)))
2938 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002940 s = 0;
2941 e = allocatesize_fsb;
2942 }
2943
2944 if (unlikely(rt)) {
2945 resrtextents = qblocks = (uint)(e - s);
2946 resrtextents /= mp->m_sb.sb_rextsize;
2947 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2948 quota_flag = XFS_QMOPT_RES_RTBLKS;
2949 } else {
2950 resrtextents = 0;
2951 resblks = qblocks = \
2952 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2953 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
2955
2956 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002957 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 */
2959 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002960 error = xfs_trans_reserve(tp, resblks,
2961 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 XFS_TRANS_PERM_LOG_RES,
2963 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002965 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 */
2967 if (error) {
2968 /*
2969 * Free the transaction structure.
2970 */
2971 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2972 xfs_trans_cancel(tp, 0);
2973 break;
2974 }
2975 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002976 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
2977 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (error)
2979 goto error1;
2980
2981 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2982 xfs_trans_ihold(tp, ip);
2983
2984 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002985 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 */
2987 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002988 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002989 allocatesize_fsb, bmapi_flag,
2990 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002991 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 if (error) {
2993 goto error0;
2994 }
2995
2996 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002997 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002999 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (error) {
3001 goto error0;
3002 }
3003
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003004 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3006 if (error) {
3007 break;
3008 }
3009
3010 allocated_fsb = imapp->br_blockcount;
3011
Nathan Scottdd9f4382006-01-11 15:28:28 +11003012 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 error = XFS_ERROR(ENOSPC);
3014 break;
3015 }
3016
3017 startoffset_fsb += allocated_fsb;
3018 allocatesize_fsb -= allocated_fsb;
3019 }
3020dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003021 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003022 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003024 ip, DM_RIGHT_NULL,
3025 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3027 if (error == 0)
3028 goto retry; /* Maybe DMAPI app. has made space */
3029 /* else fall through with error from XFS_SEND_DATA */
3030 }
3031
3032 return error;
3033
Nathan Scottdd9f4382006-01-11 15:28:28 +11003034error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003036 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3037
3038error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3040 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3041 goto dmapi_enospc_check;
3042}
3043
3044/*
3045 * Zero file bytes between startoff and endoff inclusive.
3046 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10003047 *
3048 * This function is used by xfs_free_file_space() to zero
3049 * partial blocks when the range to free is not block aligned.
3050 * When unreserving space with boundaries that are not block
3051 * aligned we round up the start and round down the end
3052 * boundaries and then use this function to zero the parts of
3053 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 */
3055STATIC int
3056xfs_zero_remaining_bytes(
3057 xfs_inode_t *ip,
3058 xfs_off_t startoff,
3059 xfs_off_t endoff)
3060{
3061 xfs_bmbt_irec_t imap;
3062 xfs_fileoff_t offset_fsb;
3063 xfs_off_t lastoffset;
3064 xfs_off_t offset;
3065 xfs_buf_t *bp;
3066 xfs_mount_t *mp = ip->i_mount;
3067 int nimap;
3068 int error = 0;
3069
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10003070 /*
3071 * Avoid doing I/O beyond eof - it's not necessary
3072 * since nothing can read beyond eof. The space will
3073 * be zeroed when the file is extended anyway.
3074 */
3075 if (startoff >= ip->i_size)
3076 return 0;
3077
3078 if (endoff > ip->i_size)
3079 endoff = ip->i_size;
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003082 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 mp->m_rtdev_targp : mp->m_ddev_targp);
3084
3085 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3086 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3087 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003088 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003089 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (error || nimap < 1)
3091 break;
3092 ASSERT(imap.br_blockcount >= 1);
3093 ASSERT(imap.br_startoff == offset_fsb);
3094 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3095 if (lastoffset > endoff)
3096 lastoffset = endoff;
3097 if (imap.br_startblock == HOLESTARTBLOCK)
3098 continue;
3099 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3100 if (imap.br_state == XFS_EXT_UNWRITTEN)
3101 continue;
3102 XFS_BUF_UNDONE(bp);
3103 XFS_BUF_UNWRITE(bp);
3104 XFS_BUF_READ(bp);
3105 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3106 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003107 error = xfs_iowait(bp);
3108 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3110 mp, bp, XFS_BUF_ADDR(bp));
3111 break;
3112 }
3113 memset(XFS_BUF_PTR(bp) +
3114 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3115 0, lastoffset - offset + 1);
3116 XFS_BUF_UNDONE(bp);
3117 XFS_BUF_UNREAD(bp);
3118 XFS_BUF_WRITE(bp);
3119 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003120 error = xfs_iowait(bp);
3121 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3123 mp, bp, XFS_BUF_ADDR(bp));
3124 break;
3125 }
3126 }
3127 xfs_buf_free(bp);
3128 return error;
3129}
3130
3131/*
3132 * xfs_free_file_space()
3133 * This routine frees disk space for the given file.
3134 *
3135 * This routine is only called by xfs_change_file_space
3136 * for an UNRESVSP type call.
3137 *
3138 * RETURNS:
3139 * 0 on success
3140 * errno on error
3141 *
3142 */
3143STATIC int
3144xfs_free_file_space(
3145 xfs_inode_t *ip,
3146 xfs_off_t offset,
3147 xfs_off_t len,
3148 int attr_flags)
3149{
3150 int committed;
3151 int done;
3152 xfs_off_t end_dmi_offset;
3153 xfs_fileoff_t endoffset_fsb;
3154 int error;
3155 xfs_fsblock_t firstfsb;
3156 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 xfs_bmbt_irec_t imap;
3158 xfs_off_t ioffset;
3159 xfs_extlen_t mod=0;
3160 xfs_mount_t *mp;
3161 int nimap;
3162 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10003163 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 int rt;
3165 xfs_fileoff_t startoffset_fsb;
3166 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003167 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 mp = ip->i_mount;
3170
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003171 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3174 return error;
3175
3176 error = 0;
3177 if (len <= 0) /* if nothing being freed */
3178 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003179 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3181 end_dmi_offset = offset + len;
3182 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3183
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003184 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003185 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003186 if (end_dmi_offset > ip->i_size)
3187 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003188 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 offset, end_dmi_offset - offset,
3190 AT_DELAY_FLAG(attr_flags), NULL);
3191 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003192 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
3194
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003195 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003196 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11003197 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10003199 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11003200 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003201
Tim Shimmine6a4b372007-11-23 16:30:42 +11003202 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003204
Christoph Hellwigdf80c932008-08-13 16:22:09 +10003205 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003206 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3207 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10003208 if (error)
3209 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003210 }
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 /*
3213 * Need to zero the stuff we're not freeing, on disk.
3214 * If its a realtime file & can't use unwritten extents then we
3215 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3216 * will take care of it for us.
3217 */
Eric Sandeen62118702008-03-06 13:44:28 +11003218 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003220 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003221 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 if (error)
3223 goto out_unlock_iolock;
3224 ASSERT(nimap == 0 || nimap == 1);
3225 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3226 xfs_daddr_t block;
3227
3228 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3229 block = imap.br_startblock;
3230 mod = do_div(block, mp->m_sb.sb_rextsize);
3231 if (mod)
3232 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3233 }
3234 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003235 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003236 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 if (error)
3238 goto out_unlock_iolock;
3239 ASSERT(nimap == 0 || nimap == 1);
3240 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3241 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3242 mod++;
3243 if (mod && (mod != mp->m_sb.sb_rextsize))
3244 endoffset_fsb -= mod;
3245 }
3246 }
3247 if ((done = (endoffset_fsb <= startoffset_fsb)))
3248 /*
3249 * One contiguous piece to clear
3250 */
3251 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3252 else {
3253 /*
3254 * Some full blocks, possibly two pieces to clear
3255 */
3256 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3257 error = xfs_zero_remaining_bytes(ip, offset,
3258 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3259 if (!error &&
3260 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3261 error = xfs_zero_remaining_bytes(ip,
3262 XFS_FSB_TO_B(mp, endoffset_fsb),
3263 offset + len - 1);
3264 }
3265
3266 /*
3267 * free file space until done or until there is an error
3268 */
3269 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3270 while (!error && !done) {
3271
3272 /*
David Chinner91ebecc2007-07-19 16:28:30 +10003273 * allocate and setup the transaction. Allow this
3274 * transaction to dip into the reserve blocks to ensure
3275 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 */
3277 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10003278 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 error = xfs_trans_reserve(tp,
3280 resblks,
3281 XFS_WRITE_LOG_RES(mp),
3282 0,
3283 XFS_TRANS_PERM_LOG_RES,
3284 XFS_WRITE_LOG_COUNT);
3285
3286 /*
3287 * check for running out of space
3288 */
3289 if (error) {
3290 /*
3291 * Free the transaction structure.
3292 */
3293 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3294 xfs_trans_cancel(tp, 0);
3295 break;
3296 }
3297 xfs_ilock(ip, XFS_ILOCK_EXCL);
3298 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003299 ip->i_udquot, ip->i_gdquot, resblks, 0,
3300 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (error)
3302 goto error1;
3303
3304 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3305 xfs_trans_ihold(tp, ip);
3306
3307 /*
3308 * issue the bunmapi() call to free the blocks
3309 */
3310 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003311 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003313 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 if (error) {
3315 goto error0;
3316 }
3317
3318 /*
3319 * complete the transaction
3320 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003321 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 if (error) {
3323 goto error0;
3324 }
3325
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003326 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3328 }
3329
3330 out_unlock_iolock:
3331 if (need_iolock)
3332 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3333 return error;
3334
3335 error0:
3336 xfs_bmap_cancel(&free_list);
3337 error1:
3338 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3339 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3340 XFS_ILOCK_EXCL);
3341 return error;
3342}
3343
3344/*
3345 * xfs_change_file_space()
3346 * This routine allocates or frees disk space for the given file.
3347 * The user specified parameters are checked for alignment and size
3348 * limitations.
3349 *
3350 * RETURNS:
3351 * 0 on success
3352 * errno on error
3353 *
3354 */
3355int
3356xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003357 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 int cmd,
3359 xfs_flock64_t *bf,
3360 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 int attr_flags)
3362{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003363 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 int clrprealloc;
3365 int error;
3366 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 int setprealloc;
3368 xfs_off_t startoffset;
3369 xfs_off_t llen;
3370 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003371 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003373 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Christoph Hellwig993386c2007-08-28 16:12:30 +10003375 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 return XFS_ERROR(EINVAL);
3377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 switch (bf->l_whence) {
3379 case 0: /*SEEK_SET*/
3380 break;
3381 case 1: /*SEEK_CUR*/
3382 bf->l_start += offset;
3383 break;
3384 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003385 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 break;
3387 default:
3388 return XFS_ERROR(EINVAL);
3389 }
3390
3391 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3392
3393 if ( (bf->l_start < 0)
3394 || (bf->l_start > XFS_MAXIOFFSET(mp))
3395 || (bf->l_start + llen < 0)
3396 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3397 return XFS_ERROR(EINVAL);
3398
3399 bf->l_whence = 0;
3400
3401 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003402 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
3404 /*
3405 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3406 * file space.
3407 * These calls do NOT zero the data space allocated to the file,
3408 * nor do they change the file size.
3409 *
3410 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3411 * space.
3412 * These calls cause the new file data to be zeroed and the file
3413 * size to be changed.
3414 */
3415 setprealloc = clrprealloc = 0;
3416
3417 switch (cmd) {
3418 case XFS_IOC_RESVSP:
3419 case XFS_IOC_RESVSP64:
3420 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3421 1, attr_flags);
3422 if (error)
3423 return error;
3424 setprealloc = 1;
3425 break;
3426
3427 case XFS_IOC_UNRESVSP:
3428 case XFS_IOC_UNRESVSP64:
3429 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3430 attr_flags)))
3431 return error;
3432 break;
3433
3434 case XFS_IOC_ALLOCSP:
3435 case XFS_IOC_ALLOCSP64:
3436 case XFS_IOC_FREESP:
3437 case XFS_IOC_FREESP64:
3438 if (startoffset > fsize) {
3439 error = xfs_alloc_file_space(ip, fsize,
3440 startoffset - fsize, 0, attr_flags);
3441 if (error)
3442 break;
3443 }
3444
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003445 iattr.ia_valid = ATTR_SIZE;
3446 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +11003448 error = xfs_setattr(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 if (error)
3451 return error;
3452
3453 clrprealloc = 1;
3454 break;
3455
3456 default:
3457 ASSERT(0);
3458 return XFS_ERROR(EINVAL);
3459 }
3460
3461 /*
3462 * update the inode timestamp, mode, and prealloc flag bits
3463 */
3464 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3465
3466 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3467 0, 0, 0))) {
3468 /* ASSERT(0); */
3469 xfs_trans_cancel(tp, 0);
3470 return error;
3471 }
3472
3473 xfs_ilock(ip, XFS_ILOCK_EXCL);
3474
3475 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3476 xfs_trans_ihold(tp, ip);
3477
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003478 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 ip->i_d.di_mode &= ~S_ISUID;
3480
3481 /*
3482 * Note that we don't have to worry about mandatory
3483 * file locking being disabled here because we only
3484 * clear the S_ISGID bit if the Group execute bit is
3485 * on, but if it was on then mandatory locking wouldn't
3486 * have been enabled.
3487 */
3488 if (ip->i_d.di_mode & S_IXGRP)
3489 ip->i_d.di_mode &= ~S_ISGID;
3490
3491 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3492 }
3493 if (setprealloc)
3494 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3495 else if (clrprealloc)
3496 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3497
3498 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3499 xfs_trans_set_sync(tp);
3500
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003501 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3504
3505 return error;
3506}