blob: 34a1982ed6dc658337bd20a64e565185b5a88a01 [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.
235 * If the system was configured with the "restricted_chown"
236 * option, the owner is not permitted to give away the file,
237 * and can change the group id only to a group of which he
238 * or she is a member.
239 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000240 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /*
242 * These IDs could have changed since we last looked at them.
243 * But, we're assured that if the ownership did change
244 * while we didn't have the inode locked, inode's dquot(s)
245 * would have changed also.
246 */
247 iuid = ip->i_d.di_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 igid = ip->i_d.di_gid;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000249 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
250 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /*
253 * CAP_CHOWN overrides the following restrictions:
254 *
255 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
256 * shall override the restriction that a process cannot
257 * change the user ID of a file it owns and the restriction
258 * that the group ID supplied to the chown() function
259 * shall be equal to either the group ID or one of the
260 * supplementary group IDs of the calling process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
262 if (restricted_chown &&
263 (iuid != uid || (igid != gid &&
264 !in_group_p((gid_t)gid))) &&
265 !capable(CAP_CHOWN)) {
266 code = XFS_ERROR(EPERM);
267 goto error_return;
268 }
269 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000270 * Do a quota reservation only if uid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * going to change.
272 */
273 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
274 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
275 ASSERT(tp);
276 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
277 capable(CAP_FOWNER) ?
278 XFS_QMOPT_FORCE_RES : 0);
279 if (code) /* out of quota */
280 goto error_return;
281 }
282 }
283
284 /*
285 * Truncate file. Must have write permission and not be a directory.
286 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000287 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* Short circuit the truncate case for zero length files */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000289 if (iattr->ia_size == 0 &&
290 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 xfs_iunlock(ip, XFS_ILOCK_EXCL);
292 lock_flags &= ~XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000293 if (mask & ATTR_CTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
295 code = 0;
296 goto error_return;
297 }
298
Christoph Hellwig42173f62008-04-22 17:33:25 +1000299 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 code = XFS_ERROR(EISDIR);
301 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000302 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 code = XFS_ERROR(EINVAL);
304 goto error_return;
305 }
306 /*
307 * Make sure that the dquots are attached to the inode.
308 */
309 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
310 goto error_return;
311 }
312
313 /*
314 * Change file access or modified times.
315 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000316 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!file_owner) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000318 if ((mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 !capable(CAP_FOWNER)) {
320 code = XFS_ERROR(EPERM);
321 goto error_return;
322 }
323 }
324 }
325
326 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * Now we can make the changes. Before we join the inode
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000328 * to the transaction, if ATTR_SIZE is set then take care of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * the part of the truncation that must be done without the
330 * inode lock. This needs to be done before joining the inode
331 * to the transaction, because the inode cannot be unlocked
332 * once it is a part of the transaction.
333 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000334 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 code = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000336 if (iattr->ia_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000337 /*
338 * Do the first part of growing a file: zero any data
339 * in the last block that is beyond the old EOF. We
340 * need to do this before the inode is joined to the
341 * transaction to modify the i_size.
342 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000343 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000346
347 /*
348 * We are going to log the inode size change in this
349 * transaction so any previous writes that are beyond the on
350 * disk EOF and the new EOF that have not been written out need
351 * to be written here. If we do not write the data out, we
352 * expose ourselves to the null files problem.
353 *
354 * Only flush from the on disk size to the smaller of the in
355 * memory file size or the new size as that's the range we
356 * really care about here and prevents waiting for other data
357 * not within the range we care about here.
358 */
359 if (!code &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000360 ip->i_size != ip->i_d.di_size &&
361 iattr->ia_size > ip->i_d.di_size) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000362 code = xfs_flush_pages(ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000363 ip->i_d.di_size, iattr->ia_size,
David Chinnerc32676e2007-07-19 16:28:58 +1000364 XFS_B_ASYNC, FI_NONE);
365 }
366
367 /* wait for all I/O to complete */
Christoph Hellwigb677c212007-08-29 11:46:28 +1000368 vn_iowait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (!code)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000371 code = xfs_itruncate_data(ip, iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (code) {
373 ASSERT(tp == NULL);
374 lock_flags &= ~XFS_ILOCK_EXCL;
375 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
376 goto error_return;
377 }
378 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
379 if ((code = xfs_trans_reserve(tp, 0,
380 XFS_ITRUNCATE_LOG_RES(mp), 0,
381 XFS_TRANS_PERM_LOG_RES,
382 XFS_ITRUNCATE_LOG_COUNT))) {
383 xfs_trans_cancel(tp, 0);
384 if (need_iolock)
385 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
386 return code;
387 }
388 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
389 xfs_ilock(ip, XFS_ILOCK_EXCL);
390 }
391
392 if (tp) {
393 xfs_trans_ijoin(tp, ip, lock_flags);
394 xfs_trans_ihold(tp, ip);
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * Truncate file. Must have write permission and not be a directory.
399 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000400 if (mask & ATTR_SIZE) {
David Chinner44d814c2008-03-06 13:45:29 +1100401 /*
402 * Only change the c/mtime if we are changing the size
403 * or we are explicitly asked to change it. This handles
404 * the semantic difference between truncate() and ftruncate()
405 * as implemented in the VFS.
406 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000407 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
David Chinner44d814c2008-03-06 13:45:29 +1100408 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
409
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000410 if (iattr->ia_size > ip->i_size) {
411 ip->i_d.di_size = iattr->ia_size;
412 ip->i_size = iattr->ia_size;
413 if (!(flags & XFS_ATTR_DMI))
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000414 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
415 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000416 } else if (iattr->ia_size <= ip->i_size ||
417 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
419 * signal a sync transaction unless
420 * we're truncating an already unlinked
421 * file on a wsync filesystem
422 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000423 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 XFS_DATA_FORK,
425 ((ip->i_d.di_nlink != 0 ||
426 !(mp->m_flags & XFS_MOUNT_WSYNC))
427 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000428 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000430 /*
431 * Truncated "down", so we're removing references
432 * to old data here - if we now delay flushing for
433 * a long time, we expose ourselves unduly to the
434 * notorious NULL files problem. So, we mark this
435 * vnode and flush it when the file is closed, and
436 * do not wait the usual (long) time for writeout.
437 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000438 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 /*
443 * Change file access modes.
444 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000445 if (mask & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ip->i_d.di_mode &= S_IFMT;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000447 ip->i_d.di_mode |= iattr->ia_mode & ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000449 inode->i_mode &= S_IFMT;
450 inode->i_mode |= iattr->ia_mode & ~S_IFMT;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
453 timeflags |= XFS_ICHGTIME_CHG;
454 }
455
456 /*
457 * Change file ownership. Must be the owner or privileged.
458 * If the system was configured with the "restricted_chown"
459 * option, the owner is not permitted to give away the file,
460 * and can change the group id only to a group of which he
461 * or she is a member.
462 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000463 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 * CAP_FSETID overrides the following restrictions:
466 *
467 * The set-user-ID and set-group-ID bits of a file will be
468 * cleared upon successful return from chown()
469 */
470 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
471 !capable(CAP_FSETID)) {
472 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
473 }
474
475 /*
476 * Change the ownerships and register quota modifications
477 * in the transaction.
478 */
479 if (iuid != uid) {
480 if (XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000481 ASSERT(mask & ATTR_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ASSERT(udqp);
483 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
484 &ip->i_udquot, udqp);
485 }
486 ip->i_d.di_uid = uid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000487 inode->i_uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 if (igid != gid) {
490 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000491 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000492 ASSERT(mask & ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 ASSERT(gdqp);
494 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
495 &ip->i_gdquot, gdqp);
496 }
497 ip->i_d.di_gid = gid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000498 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
502 timeflags |= XFS_ICHGTIME_CHG;
503 }
504
505
506 /*
507 * Change file access or modified times.
508 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000509 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
510 if (mask & ATTR_ATIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000511 inode->i_atime = iattr->ia_atime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000512 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
513 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 ip->i_update_core = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000516 if (mask & ATTR_MTIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000517 inode->i_mtime = iattr->ia_mtime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000518 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
519 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 timeflags &= ~XFS_ICHGTIME_MOD;
521 timeflags |= XFS_ICHGTIME_CHG;
522 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000523 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
525 }
526
527 /*
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000528 * Change file inode change time only if ATTR_CTIME set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * AND we have been called by a DMI function.
530 */
531
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000532 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000533 inode->i_ctime = iattr->ia_ctime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000534 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
535 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ip->i_update_core = 1;
537 timeflags &= ~XFS_ICHGTIME_CHG;
538 }
539
540 /*
541 * Send out timestamp changes that need to be set to the
542 * current time. Not done when called by a DMI function.
543 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000544 if (timeflags && !(flags & XFS_ATTR_DMI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 xfs_ichgtime(ip, timeflags);
546
547 XFS_STATS_INC(xs_ig_attrchg);
548
549 /*
550 * If this is a synchronous mount, make sure that the
551 * transaction goes to disk before returning to the user.
552 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000553 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * One for the truncate and one for the timestamps since we
555 * don't want to change the timestamps unless we're sure the
556 * truncate worked. Truncates are less than 1% of the laddis
557 * mix so this probably isn't worth the trouble to optimize.
558 */
559 code = 0;
560 if (tp) {
561 if (mp->m_flags & XFS_MOUNT_WSYNC)
562 xfs_trans_set_sync(tp);
563
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000564 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 xfs_iunlock(ip, lock_flags);
568
569 /*
570 * Release any dquot(s) the inode had kept before chown.
571 */
572 XFS_QM_DQRELE(mp, olddquot1);
573 XFS_QM_DQRELE(mp, olddquot2);
574 XFS_QM_DQRELE(mp, udqp);
575 XFS_QM_DQRELE(mp, gdqp);
576
577 if (code) {
578 return code;
579 }
580
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000581 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000582 !(flags & XFS_ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100583 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 NULL, DM_RIGHT_NULL, NULL, NULL,
585 0, 0, AT_DELAY_FLAG(flags));
586 }
587 return 0;
588
589 abort_return:
590 commit_flags |= XFS_TRANS_ABORT;
591 /* FALLTHROUGH */
592 error_return:
593 XFS_QM_DQRELE(mp, udqp);
594 XFS_QM_DQRELE(mp, gdqp);
595 if (tp) {
596 xfs_trans_cancel(tp, commit_flags);
597 }
598 if (lock_flags != 0) {
599 xfs_iunlock(ip, lock_flags);
600 }
601 return code;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000605 * The maximum pathlen is 1024 bytes. Since the minimum file system
606 * blocksize is 512 bytes, we can get a max of 2 extents back from
607 * bmapi.
608 */
609#define SYMLINK_MAPS 2
610
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000611STATIC int
612xfs_readlink_bmap(
613 xfs_inode_t *ip,
614 char *link)
615{
616 xfs_mount_t *mp = ip->i_mount;
617 int pathlen = ip->i_d.di_size;
618 int nmaps = SYMLINK_MAPS;
619 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
620 xfs_daddr_t d;
621 int byte_cnt;
622 int n;
623 xfs_buf_t *bp;
624 int error = 0;
625
626 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
627 mval, &nmaps, NULL, NULL);
628 if (error)
629 goto out;
630
631 for (n = 0; n < nmaps; n++) {
632 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
633 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
634
635 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
636 error = XFS_BUF_GETERROR(bp);
637 if (error) {
638 xfs_ioerror_alert("xfs_readlink",
639 ip->i_mount, bp, XFS_BUF_ADDR(bp));
640 xfs_buf_relse(bp);
641 goto out;
642 }
643 if (pathlen < byte_cnt)
644 byte_cnt = pathlen;
645 pathlen -= byte_cnt;
646
647 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
648 xfs_buf_relse(bp);
649 }
650
651 link[ip->i_d.di_size] = '\0';
652 error = 0;
653
654 out:
655 return error;
656}
657
Christoph Hellwig993386c2007-08-28 16:12:30 +1000658int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000660 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000661 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000663 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100667 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (XFS_FORCED_SHUTDOWN(mp))
670 return XFS_ERROR(EIO);
671
672 xfs_ilock(ip, XFS_ILOCK_SHARED);
673
674 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000675 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000677 pathlen = ip->i_d.di_size;
678 if (!pathlen)
679 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000682 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
683 link[pathlen] = '\0';
684 } else {
685 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000688 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return error;
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/*
694 * xfs_fsync
695 *
David Chinner978b7232008-05-19 16:29:46 +1000696 * This is called to sync the inode and its data out to disk. We need to hold
697 * the I/O lock while flushing the data, and the inode lock while flushing the
698 * inode. The inode lock CANNOT be held while flushing the data, so acquire
699 * after we're done with that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000701int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702xfs_fsync(
David Chinner978b7232008-05-19 16:29:46 +1000703 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 xfs_trans_t *tp;
706 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100707 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100709 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
712 return XFS_ERROR(EIO);
713
David Chinner978b7232008-05-19 16:29:46 +1000714 /* capture size updates in I/O completion before writing the inode. */
Christoph Hellwig863890c2008-08-13 16:12:05 +1000715 error = filemap_fdatawait(VFS_I(ip)->i_mapping);
David Chinner978b7232008-05-19 16:29:46 +1000716 if (error)
717 return XFS_ERROR(error);
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +1000718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /*
David Chinner978b7232008-05-19 16:29:46 +1000720 * We always need to make sure that the required inode state is safe on
721 * disk. The vnode might be clean but we still might need to force the
722 * log because of committed transactions that haven't hit the disk yet.
723 * Likewise, there could be unflushed non-transactional changes to the
724 * inode core that have to go to disk and this requires us to issue
725 * a synchronous transaction to capture these changes correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 *
David Chinner978b7232008-05-19 16:29:46 +1000727 * This code relies on the assumption that if the update_* fields
728 * of the inode are clear and the inode is unpinned then it is clean
729 * and no action is required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
731 xfs_ilock(ip, XFS_ILOCK_SHARED);
732
David Chinner978b7232008-05-19 16:29:46 +1000733 if (!(ip->i_update_size || ip->i_update_core)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
David Chinner978b7232008-05-19 16:29:46 +1000735 * Timestamps/size haven't changed since last inode flush or
736 * inode transaction commit. That means either nothing got
737 * written or a transaction committed which caught the updates.
738 * If the latter happened and the transaction hasn't hit the
739 * disk yet, the inode will be still be pinned. If it is,
740 * force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
742
743 xfs_iunlock(ip, XFS_ILOCK_SHARED);
744
745 if (xfs_ipincount(ip)) {
David Chinner978b7232008-05-19 16:29:46 +1000746 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
747 XFS_LOG_FORCE | XFS_LOG_SYNC,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100748 &log_flushed);
749 } else {
750 /*
David Chinner978b7232008-05-19 16:29:46 +1000751 * If the inode is not pinned and nothing has changed
752 * we don't need to flush the cache.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100753 */
754 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 } else {
757 /*
David Chinner978b7232008-05-19 16:29:46 +1000758 * Kick off a transaction to log the inode core to get the
759 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
761 xfs_iunlock(ip, XFS_ILOCK_SHARED);
762 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000763 error = xfs_trans_reserve(tp, 0,
764 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
765 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 xfs_trans_cancel(tp, 0);
767 return error;
768 }
769 xfs_ilock(ip, XFS_ILOCK_EXCL);
770
771 /*
David Chinner978b7232008-05-19 16:29:46 +1000772 * Note - it's possible that we might have pushed ourselves out
773 * of the way during trans_reserve which would flush the inode.
774 * But there's no guarantee that the inode buffer has actually
775 * gone out yet (it's delwri). Plus the buffer could be pinned
776 * anyway if it's part of an inode in another recent
777 * transaction. So we play it safe and fire off the
778 * transaction anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
780 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
781 xfs_trans_ihold(tp, ip);
782 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
David Chinner978b7232008-05-19 16:29:46 +1000783 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000784 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 xfs_iunlock(ip, XFS_ILOCK_EXCL);
787 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100788
789 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
790 /*
791 * If the log write didn't issue an ordered tag we need
792 * to flush the disk cache for the data device now.
793 */
794 if (!log_flushed)
795 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
796
797 /*
798 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000799 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100800 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100801 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100802 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
803 }
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return error;
806}
807
808/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000809 * This is called by xfs_inactive to free any blocks beyond eof
810 * when the link count isn't zero and by xfs_dm_punch_hole() when
811 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000813int
814xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000816 xfs_inode_t *ip,
817 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 xfs_trans_t *tp;
820 int error;
821 xfs_fileoff_t end_fsb;
822 xfs_fileoff_t last_fsb;
823 xfs_filblks_t map_len;
824 int nimaps;
825 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +1000826 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 /*
829 * Figure out if there are any blocks beyond the end
830 * of the file. If not, then there is nothing to do.
831 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000832 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
834 map_len = last_fsb - end_fsb;
835 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 nimaps = 1;
839 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000840 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000841 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 xfs_iunlock(ip, XFS_ILOCK_SHARED);
843
844 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100845 (imap.br_startblock != HOLESTARTBLOCK ||
846 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /*
848 * Attach the dquots to the inode up front.
849 */
850 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +0100851 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 /*
854 * There are blocks after the end of file.
855 * Free them up now by truncating the file to
856 * its current size.
857 */
858 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
859
860 /*
861 * Do the xfs_itruncate_start() call before
862 * reserving any log space because
863 * itruncate_start will call into the buffer
864 * cache and we can't
865 * do that within a transaction.
866 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000867 if (use_iolock)
868 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000869 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000870 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000871 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000872 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +1000873 if (use_iolock)
874 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000875 return error;
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 error = xfs_trans_reserve(tp, 0,
879 XFS_ITRUNCATE_LOG_RES(mp),
880 0, XFS_TRANS_PERM_LOG_RES,
881 XFS_ITRUNCATE_LOG_COUNT);
882 if (error) {
883 ASSERT(XFS_FORCED_SHUTDOWN(mp));
884 xfs_trans_cancel(tp, 0);
885 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100886 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
889 xfs_ilock(ip, XFS_ILOCK_EXCL);
890 xfs_trans_ijoin(tp, ip,
891 XFS_IOLOCK_EXCL |
892 XFS_ILOCK_EXCL);
893 xfs_trans_ihold(tp, ip);
894
895 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000896 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 XFS_DATA_FORK,
898 0);
899 /*
900 * If we get an error at this point we
901 * simply don't bother truncating the file.
902 */
903 if (error) {
904 xfs_trans_cancel(tp,
905 (XFS_TRANS_RELEASE_LOG_RES |
906 XFS_TRANS_ABORT));
907 } else {
908 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000909 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
David Chinner92dfe8d2007-05-24 15:22:19 +1000911 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
912 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100914 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917/*
918 * Free a symlink that has blocks associated with it.
919 */
920STATIC int
921xfs_inactive_symlink_rmt(
922 xfs_inode_t *ip,
923 xfs_trans_t **tpp)
924{
925 xfs_buf_t *bp;
926 int committed;
927 int done;
928 int error;
929 xfs_fsblock_t first_block;
930 xfs_bmap_free_t free_list;
931 int i;
932 xfs_mount_t *mp;
933 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
934 int nmaps;
935 xfs_trans_t *ntp;
936 int size;
937 xfs_trans_t *tp;
938
939 tp = *tpp;
940 mp = ip->i_mount;
941 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
942 /*
943 * We're freeing a symlink that has some
944 * blocks allocated to it. Free the
945 * blocks here. We know that we've got
946 * either 1 or 2 extents and that we can
947 * free them all in one bunmapi call.
948 */
949 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
950 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
951 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
952 ASSERT(XFS_FORCED_SHUTDOWN(mp));
953 xfs_trans_cancel(tp, 0);
954 *tpp = NULL;
955 return error;
956 }
957 /*
958 * Lock the inode, fix the size, and join it to the transaction.
959 * Hold it so in the normal path, we still have it locked for
960 * the second transaction. In the error paths we need it
961 * held so the cancel won't rele it, see below.
962 */
963 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
964 size = (int)ip->i_d.di_size;
965 ip->i_d.di_size = 0;
966 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
967 xfs_trans_ihold(tp, ip);
968 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
969 /*
970 * Find the block(s) so we can inval and unmap them.
971 */
972 done = 0;
973 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800974 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
976 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000977 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 goto error0;
979 /*
980 * Invalidate the block(s).
981 */
982 for (i = 0; i < nmaps; i++) {
983 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
984 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
985 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
986 xfs_trans_binval(tp, bp);
987 }
988 /*
989 * Unmap the dead block(s) to the free_list.
990 */
991 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000992 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 goto error1;
994 ASSERT(done);
995 /*
996 * Commit the first transaction. This logs the EFI and the inode.
997 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100998 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 goto error1;
1000 /*
1001 * The transaction must have been committed, since there were
1002 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1003 * The new tp has the extent freeing and EFDs.
1004 */
1005 ASSERT(committed);
1006 /*
1007 * The first xact was committed, so add the inode to the new one.
1008 * Mark it dirty so it will be logged and moved forward in the log as
1009 * part of every commit.
1010 */
1011 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1012 xfs_trans_ihold(tp, ip);
1013 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1014 /*
1015 * Get a new, empty transaction to return to our caller.
1016 */
1017 ntp = xfs_trans_dup(tp);
1018 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001019 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * If we get an error on the commit here or on the reserve below,
1021 * we need to unlock the inode since the new transaction doesn't
1022 * have the inode attached.
1023 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001024 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 tp = ntp;
1026 if (error) {
1027 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1028 goto error0;
1029 }
1030 /*
1031 * Remove the memory for extent descriptions (just bookkeeping).
1032 */
1033 if (ip->i_df.if_bytes)
1034 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1035 ASSERT(ip->i_df.if_bytes == 0);
1036 /*
1037 * Put an itruncate log reservation in the new transaction
1038 * for our caller.
1039 */
1040 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1041 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1042 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1043 goto error0;
1044 }
1045 /*
1046 * Return with the inode locked but not joined to the transaction.
1047 */
1048 *tpp = tp;
1049 return 0;
1050
1051 error1:
1052 xfs_bmap_cancel(&free_list);
1053 error0:
1054 /*
1055 * Have to come here with the inode locked and either
1056 * (held and in the transaction) or (not in the transaction).
1057 * If the inode isn't held then cancel would iput it, but
1058 * that's wrong since this is inactive and the vnode ref
1059 * count is 0 already.
1060 * Cancel won't do anything to the inode if held, but it still
1061 * needs to be locked until the cancel is done, if it was
1062 * joined to the transaction.
1063 */
1064 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1065 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1066 *tpp = NULL;
1067 return error;
1068
1069}
1070
1071STATIC int
1072xfs_inactive_symlink_local(
1073 xfs_inode_t *ip,
1074 xfs_trans_t **tpp)
1075{
1076 int error;
1077
1078 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1079 /*
1080 * We're freeing a symlink which fit into
1081 * the inode. Just free the memory used
1082 * to hold the old symlink.
1083 */
1084 error = xfs_trans_reserve(*tpp, 0,
1085 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1086 0, XFS_TRANS_PERM_LOG_RES,
1087 XFS_ITRUNCATE_LOG_COUNT);
1088
1089 if (error) {
1090 xfs_trans_cancel(*tpp, 0);
1091 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001092 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1095
1096 /*
1097 * Zero length symlinks _can_ exist.
1098 */
1099 if (ip->i_df.if_bytes > 0) {
1100 xfs_idata_realloc(ip,
1101 -(ip->i_df.if_bytes),
1102 XFS_DATA_FORK);
1103 ASSERT(ip->i_df.if_bytes == 0);
1104 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108STATIC int
1109xfs_inactive_attrs(
1110 xfs_inode_t *ip,
1111 xfs_trans_t **tpp)
1112{
1113 xfs_trans_t *tp;
1114 int error;
1115 xfs_mount_t *mp;
1116
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001117 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 tp = *tpp;
1119 mp = ip->i_mount;
1120 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001121 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001123 if (error)
1124 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001127 if (error)
1128 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1131 error = xfs_trans_reserve(tp, 0,
1132 XFS_IFREE_LOG_RES(mp),
1133 0, XFS_TRANS_PERM_LOG_RES,
1134 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001135 if (error)
1136 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 xfs_ilock(ip, XFS_ILOCK_EXCL);
1139 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1140 xfs_trans_ihold(tp, ip);
1141 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1142
1143 ASSERT(ip->i_d.di_anextents == 0);
1144
1145 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001146 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001147
1148error_cancel:
1149 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1150 xfs_trans_cancel(tp, 0);
1151error_unlock:
1152 *tpp = NULL;
1153 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1154 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
Christoph Hellwig993386c2007-08-28 16:12:30 +10001157int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001159 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001161 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int error;
1163
Christoph Hellwig42173f62008-04-22 17:33:25 +10001164 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001168 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
1170
David Chinner2a82b8b2007-07-11 11:09:12 +10001171 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001172 int truncated;
1173
David Chinner2a82b8b2007-07-11 11:09:12 +10001174 /*
1175 * If we are using filestreams, and we have an unlinked
1176 * file that we are processing the last close on, then nothing
1177 * will be able to reopen and write to this file. Purge this
1178 * inode from the filestreams cache so that it doesn't delay
1179 * teardown of the inode.
1180 */
1181 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1182 xfs_filestream_deassociate(ip);
1183
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001184 /*
1185 * If we previously truncated this file and removed old data
1186 * in the process, we want to initiate "early" writeout on
1187 * the last close. This is an attempt to combat the notorious
1188 * NULL files problem which is particularly noticable from a
1189 * truncate down, buffered (re-)write (delalloc), followed by
1190 * a crash. What we are effectively doing here is
1191 * significantly reducing the time window where we'd otherwise
1192 * be exposed to that problem.
1193 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001194 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001195 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001196 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001197 }
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (ip->i_d.di_nlink != 0) {
1200 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001201 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001202 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001204 (!(ip->i_d.di_flags &
1205 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001206 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1207 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001208 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210 }
1211
1212 return 0;
1213}
1214
1215/*
1216 * xfs_inactive
1217 *
1218 * This is called when the vnode reference count for the vnode
1219 * goes to zero. If the file has been unlinked, then it must
1220 * now be truncated. Also, we clear all of the read-ahead state
1221 * kept for the inode here since the file is now closed.
1222 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001223int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001225 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001227 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 xfs_fsblock_t first_block;
1229 int committed;
1230 xfs_trans_t *tp;
1231 xfs_mount_t *mp;
1232 int error;
1233 int truncate;
1234
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001235 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /*
1238 * If the inode is already free, then there can be nothing
1239 * to clean up here.
1240 */
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001241 if (ip->i_d.di_mode == 0 || VN_BAD(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 ASSERT(ip->i_df.if_real_bytes == 0);
1243 ASSERT(ip->i_df.if_broot_bytes == 0);
1244 return VN_INACTIVE_CACHE;
1245 }
1246
1247 /*
1248 * Only do a truncate if it's a regular file with
1249 * some actual space in it. It's OK to look at the
1250 * inode's fields without the lock because we're the
1251 * only one with a reference to the inode.
1252 */
1253 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001254 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1255 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1257
1258 mp = ip->i_mount;
1259
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001260 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1261 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 error = 0;
1264
1265 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001266 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 goto out;
1268
1269 if (ip->i_d.di_nlink != 0) {
1270 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001271 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001272 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001273 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1274 (!(ip->i_d.di_flags &
1275 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1276 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001277 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1278 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001279 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 goto out;
1282 }
1283
1284 ASSERT(ip->i_d.di_nlink == 0);
1285
1286 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001287 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1290 if (truncate) {
1291 /*
1292 * Do the xfs_itruncate_start() call before
1293 * reserving any log space because itruncate_start
1294 * will call into the buffer cache and we can't
1295 * do that within a transaction.
1296 */
1297 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1298
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001299 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1300 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001301 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001302 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1303 return VN_INACTIVE_CACHE;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 error = xfs_trans_reserve(tp, 0,
1307 XFS_ITRUNCATE_LOG_RES(mp),
1308 0, XFS_TRANS_PERM_LOG_RES,
1309 XFS_ITRUNCATE_LOG_COUNT);
1310 if (error) {
1311 /* Don't call itruncate_cleanup */
1312 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1313 xfs_trans_cancel(tp, 0);
1314 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001315 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
1318 xfs_ilock(ip, XFS_ILOCK_EXCL);
1319 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1320 xfs_trans_ihold(tp, ip);
1321
1322 /*
1323 * normally, we have to run xfs_itruncate_finish sync.
1324 * But if filesystem is wsync and we're in the inactive
1325 * path, then we know that nlink == 0, and that the
1326 * xaction that made nlink == 0 is permanently committed
1327 * since xfs_remove runs as a synchronous transaction.
1328 */
1329 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1330 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1331
1332 if (error) {
1333 xfs_trans_cancel(tp,
1334 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1335 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001336 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1339
1340 /*
1341 * If we get an error while cleaning up a
1342 * symlink we bail out.
1343 */
1344 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1345 xfs_inactive_symlink_rmt(ip, &tp) :
1346 xfs_inactive_symlink_local(ip, &tp);
1347
1348 if (error) {
1349 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001350 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
1353 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1354 xfs_trans_ihold(tp, ip);
1355 } else {
1356 error = xfs_trans_reserve(tp, 0,
1357 XFS_IFREE_LOG_RES(mp),
1358 0, XFS_TRANS_PERM_LOG_RES,
1359 XFS_INACTIVE_LOG_COUNT);
1360 if (error) {
1361 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1362 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001363 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
1366 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1367 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1368 xfs_trans_ihold(tp, ip);
1369 }
1370
1371 /*
1372 * If there are attributes associated with the file
1373 * then blow them away now. The code calls a routine
1374 * that recursively deconstructs the attribute fork.
1375 * We need to just commit the current transaction
1376 * because we can't use it for xfs_attr_inactive().
1377 */
1378 if (ip->i_d.di_anextents > 0) {
1379 error = xfs_inactive_attrs(ip, &tp);
1380 /*
1381 * If we got an error, the transaction is already
1382 * cancelled, and the inode is unlocked. Just get out.
1383 */
1384 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001385 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 } else if (ip->i_afp) {
1387 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1388 }
1389
1390 /*
1391 * Free the inode.
1392 */
1393 XFS_BMAP_INIT(&free_list, &first_block);
1394 error = xfs_ifree(tp, ip, &free_list);
1395 if (error) {
1396 /*
1397 * If we fail to free the inode, shut down. The cancel
1398 * might do that, we need to make sure. Otherwise the
1399 * inode might be lost for a long time or forever.
1400 */
1401 if (!XFS_FORCED_SHUTDOWN(mp)) {
1402 cmn_err(CE_NOTE,
1403 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1404 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001405 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1408 } else {
1409 /*
1410 * Credit the quota account(s). The inode is gone.
1411 */
1412 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1413
1414 /*
David Chinner78e9da72008-04-10 12:24:17 +10001415 * Just ignore errors at this point. There is nothing we can
1416 * do except to try to keep going. Make sure it's not a silent
1417 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
David Chinner78e9da72008-04-10 12:24:17 +10001419 error = xfs_bmap_finish(&tp, &free_list, &committed);
1420 if (error)
1421 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1422 "xfs_bmap_finish() returned error %d", error);
1423 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1424 if (error)
1425 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1426 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 /*
1429 * Release the dquots held by inode, if any.
1430 */
1431 XFS_QM_DQDETACH(mp, ip);
1432
1433 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1434
1435 out:
1436 return VN_INACTIVE_CACHE;
1437}
1438
Barry Naujok384f3ce2008-05-21 16:58:22 +10001439/*
1440 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1441 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1442 * ci_name->name will point to a the actual name (caller must free) or
1443 * will be set to NULL if an exact match is found.
1444 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001445int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001447 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001448 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001449 xfs_inode_t **ipp,
1450 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001452 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int error;
1454 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001456 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1459 return XFS_ERROR(EIO);
1460
1461 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001462 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001464
1465 if (error)
1466 goto out;
1467
1468 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1469 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001470 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001471
1472 xfs_itrace_ref(*ipp);
1473 return 0;
1474
Barry Naujok384f3ce2008-05-21 16:58:22 +10001475out_free_name:
1476 if (ci_name)
1477 kmem_free(ci_name->name);
1478out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001479 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return error;
1481}
1482
Christoph Hellwig993386c2007-08-28 16:12:30 +10001483int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001485 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001486 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001487 mode_t mode,
1488 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001489 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 cred_t *credp)
1491{
Barry Naujok556b8b12008-04-10 12:22:07 +10001492 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001493 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 xfs_trans_t *tp;
Barry Naujok556b8b12008-04-10 12:22:07 +10001495 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 xfs_bmap_free_t free_list;
1497 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001498 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int dm_event_sent = 0;
1500 uint cancel_flags;
1501 int committed;
1502 xfs_prid_t prid;
1503 struct xfs_dquot *udqp, *gdqp;
1504 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001506 ASSERT(!*ipp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001507 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001509 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001511 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001512 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001513 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 if (error)
1516 return error;
1517 dm_event_sent = 1;
1518 }
1519
1520 if (XFS_FORCED_SHUTDOWN(mp))
1521 return XFS_ERROR(EIO);
1522
1523 /* Return through std_return after this point. */
1524
1525 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001526 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1527 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 else
1529 prid = (xfs_prid_t)dfltprid;
1530
1531 /*
1532 * Make sure that we have allocated dquot(s) on disk.
1533 */
1534 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01001535 current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1537 if (error)
1538 goto std_return;
1539
1540 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1543 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001544 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /*
1546 * Initially assume that the file does not exist and
1547 * reserve the resources for that case. If that is not
1548 * the case we'll drop the one we have and get a more
1549 * appropriate transaction later.
1550 */
1551 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1552 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1553 if (error == ENOSPC) {
1554 resblks = 0;
1555 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1556 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1557 }
1558 if (error) {
1559 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto error_return;
1561 }
1562
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001563 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001564 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 XFS_BMAP_INIT(&free_list, &first_block);
1567
1568 ASSERT(ip == NULL);
1569
1570 /*
1571 * Reserve disk quota and the inode.
1572 */
1573 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1574 if (error)
1575 goto error_return;
1576
Barry Naujok556b8b12008-04-10 12:22:07 +10001577 error = xfs_dir_canenter(tp, dp, name, resblks);
1578 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001580 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 rdev, credp, prid, resblks > 0,
1582 &ip, &committed);
1583 if (error) {
1584 if (error == ENOSPC)
1585 goto error_return;
1586 goto abort_return;
1587 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001588 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 /*
1591 * At this point, we've gotten a newly allocated inode.
1592 * It is locked (and joined to the transaction).
1593 */
1594
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001595 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001598 * Now we join the directory inode to the transaction. We do not do it
1599 * earlier because xfs_dir_ialloc might commit the previous transaction
1600 * (and release all the locks). An error from here on will result in
1601 * the transaction cancel unlocking dp so don't do it explicitly in the
1602 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001604 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001606 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Barry Naujok556b8b12008-04-10 12:22:07 +10001608 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001609 &first_block, &free_list, resblks ?
1610 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (error) {
1612 ASSERT(error != ENOSPC);
1613 goto abort_return;
1614 }
1615 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1616 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1617
1618 /*
1619 * If this is a synchronous mount, make sure that the
1620 * create transaction goes to disk before returning to
1621 * the user.
1622 */
1623 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1624 xfs_trans_set_sync(tp);
1625 }
1626
1627 dp->i_gen++;
1628
1629 /*
1630 * Attach the dquot(s) to the inodes and modify them incore.
1631 * These ids of the inode couldn't have changed since the new
1632 * inode has been locked ever since it was created.
1633 */
1634 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1635
1636 /*
1637 * xfs_trans_commit normally decrements the vnode ref count
1638 * when it unlocks the inode. Since we want to return the
1639 * vnode to the caller, we bump the vnode ref count now.
1640 */
1641 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001643 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (error) {
1645 xfs_bmap_cancel(&free_list);
1646 goto abort_rele;
1647 }
1648
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001649 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (error) {
1651 IRELE(ip);
1652 tp = NULL;
1653 goto error_return;
1654 }
1655
1656 XFS_QM_DQRELE(mp, udqp);
1657 XFS_QM_DQRELE(mp, gdqp);
1658
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001659 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* Fallthrough to std_return with error = 0 */
1662
1663std_return:
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001664 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10001665 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001667 dp, DM_RIGHT_NULL,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001668 *ipp ? ip : NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001669 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001670 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672 return error;
1673
1674 abort_return:
1675 cancel_flags |= XFS_TRANS_ABORT;
1676 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Jesper Juhl014c2542006-01-15 02:37:08 +01001678 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (tp != NULL)
1680 xfs_trans_cancel(tp, cancel_flags);
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 XFS_QM_DQRELE(mp, udqp);
1683 XFS_QM_DQRELE(mp, gdqp);
1684
Christoph Hellwig993386c2007-08-28 16:12:30 +10001685 if (unlock_dp_on_error)
1686 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 goto std_return;
1689
1690 abort_rele:
1691 /*
1692 * Wait until after the current transaction is aborted to
1693 * release the inode. This prevents recursive transactions
1694 * and deadlocks from xfs_inactive.
1695 */
1696 cancel_flags |= XFS_TRANS_ABORT;
1697 xfs_trans_cancel(tp, cancel_flags);
1698 IRELE(ip);
1699
1700 XFS_QM_DQRELE(mp, udqp);
1701 XFS_QM_DQRELE(mp, gdqp);
1702
1703 goto std_return;
1704}
1705
1706#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707int xfs_locked_n;
1708int xfs_small_retries;
1709int xfs_middle_retries;
1710int xfs_lots_retries;
1711int xfs_lock_delays;
1712#endif
1713
1714/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001715 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1716 * a different value
1717 */
1718static inline int
1719xfs_lock_inumorder(int lock_mode, int subclass)
1720{
1721 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001722 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001723 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001724 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001725
1726 return lock_mode;
1727}
1728
1729/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * The following routine will lock n inodes in exclusive mode.
1731 * We assume the caller calls us with the inodes in i_ino order.
1732 *
1733 * We need to detect deadlock where an inode that we lock
1734 * is in the AIL and we start waiting for another inode that is locked
1735 * by a thread in a long running transaction (such as truncate). This can
1736 * result in deadlock since the long running trans might need to wait
1737 * for the inode we just locked in order to push the tail and free space
1738 * in the log.
1739 */
1740void
1741xfs_lock_inodes(
1742 xfs_inode_t **ips,
1743 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 uint lock_mode)
1745{
1746 int attempts = 0, i, j, try_lock;
1747 xfs_log_item_t *lp;
1748
1749 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1750
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001751 try_lock = 0;
1752 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754again:
1755 for (; i < inodes; i++) {
1756 ASSERT(ips[i]);
1757
1758 if (i && (ips[i] == ips[i-1])) /* Already locked */
1759 continue;
1760
1761 /*
1762 * If try_lock is not set yet, make sure all locked inodes
1763 * are not in the AIL.
1764 * If any are, set try_lock to be used later.
1765 */
1766
1767 if (!try_lock) {
1768 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1769 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1770 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1771 try_lock++;
1772 }
1773 }
1774 }
1775
1776 /*
1777 * If any of the previous locks we have locked is in the AIL,
1778 * we must TRY to get the second and subsequent locks. If
1779 * we can't get any, we must release all we have
1780 * and try again.
1781 */
1782
1783 if (try_lock) {
1784 /* try_lock must be 0 if i is 0. */
1785 /*
1786 * try_lock means we have an inode locked
1787 * that is in the AIL.
1788 */
1789 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001790 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 attempts++;
1792
1793 /*
1794 * Unlock all previous guys and try again.
1795 * xfs_iunlock will try to push the tail
1796 * if the inode is in the AIL.
1797 */
1798
1799 for(j = i - 1; j >= 0; j--) {
1800
1801 /*
1802 * Check to see if we've already
1803 * unlocked this one.
1804 * Not the first one going back,
1805 * and the inode ptr is the same.
1806 */
1807 if ((j != (i - 1)) && ips[j] ==
1808 ips[j+1])
1809 continue;
1810
1811 xfs_iunlock(ips[j], lock_mode);
1812 }
1813
1814 if ((attempts % 5) == 0) {
1815 delay(1); /* Don't just spin the CPU */
1816#ifdef DEBUG
1817 xfs_lock_delays++;
1818#endif
1819 }
1820 i = 0;
1821 try_lock = 0;
1822 goto again;
1823 }
1824 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001825 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827 }
1828
1829#ifdef DEBUG
1830 if (attempts) {
1831 if (attempts < 5) xfs_small_retries++;
1832 else if (attempts < 100) xfs_middle_retries++;
1833 else xfs_lots_retries++;
1834 } else {
1835 xfs_locked_n++;
1836 }
1837#endif
1838}
1839
David Chinnerf9114eb2008-09-17 16:51:21 +10001840/*
1841 * xfs_lock_two_inodes() can only be used to lock one type of lock
1842 * at a time - the iolock or the ilock, but not both at once. If
1843 * we lock both at once, lockdep will report false positives saying
1844 * we have violated locking orders.
1845 */
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001846void
1847xfs_lock_two_inodes(
1848 xfs_inode_t *ip0,
1849 xfs_inode_t *ip1,
1850 uint lock_mode)
1851{
1852 xfs_inode_t *temp;
1853 int attempts = 0;
1854 xfs_log_item_t *lp;
1855
David Chinnerf9114eb2008-09-17 16:51:21 +10001856 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1857 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001858 ASSERT(ip0->i_ino != ip1->i_ino);
1859
1860 if (ip0->i_ino > ip1->i_ino) {
1861 temp = ip0;
1862 ip0 = ip1;
1863 ip1 = temp;
1864 }
1865
1866 again:
1867 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1868
1869 /*
1870 * If the first lock we have locked is in the AIL, we must TRY to get
1871 * the second lock. If we can't get it, we must release the first one
1872 * and try again.
1873 */
1874 lp = (xfs_log_item_t *)ip0->i_itemp;
1875 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1876 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1877 xfs_iunlock(ip0, lock_mode);
1878 if ((++attempts % 5) == 0)
1879 delay(1); /* Don't just spin the CPU */
1880 goto again;
1881 }
1882 } else {
1883 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1884 }
1885}
1886
Christoph Hellwig993386c2007-08-28 16:12:30 +10001887int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001889 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001890 struct xfs_name *name,
1891 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001893 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001895 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 int error = 0;
1897 xfs_bmap_free_t free_list;
1898 xfs_fsblock_t first_block;
1899 int cancel_flags;
1900 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int link_zero;
1902 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001903 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001905 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001906 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (XFS_FORCED_SHUTDOWN(mp))
1909 return XFS_ERROR(EIO);
1910
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001911 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001912 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1913 NULL, DM_RIGHT_NULL, name->name, NULL,
1914 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (error)
1916 return error;
1917 }
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 error = XFS_QM_DQATTACH(mp, dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001920 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001923 error = XFS_QM_DQATTACH(mp, ip, 0);
1924 if (error)
1925 goto std_return;
1926
1927 if (is_dir) {
1928 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1929 log_count = XFS_DEFAULT_LOG_COUNT;
1930 } else {
1931 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1932 log_count = XFS_REMOVE_LOG_COUNT;
1933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 /*
1937 * We try to get the real space reservation first,
1938 * allowing for directory btree deletion(s) implying
1939 * possible bmap insert(s). If we can't get the space
1940 * reservation then we use 0 instead, and avoid the bmap
1941 * btree insert(s) in the directory code by, if the bmap
1942 * insert tries to happen, instead trimming the LAST
1943 * block from the directory.
1944 */
1945 resblks = XFS_REMOVE_SPACE_RES(mp);
1946 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001947 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (error == ENOSPC) {
1949 resblks = 0;
1950 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001951 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953 if (error) {
1954 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001955 cancel_flags = 0;
1956 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001959 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 /*
1962 * At this point, we've gotten both the directory and the entry
1963 * inodes locked.
1964 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001965 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10001967
1968 IHOLD(dp);
1969 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001972 * If we're removing a directory perform some additional validation.
1973 */
1974 if (is_dir) {
1975 ASSERT(ip->i_d.di_nlink >= 2);
1976 if (ip->i_d.di_nlink != 2) {
1977 error = XFS_ERROR(ENOTEMPTY);
1978 goto out_trans_cancel;
1979 }
1980 if (!xfs_dir_isempty(ip)) {
1981 error = XFS_ERROR(ENOTEMPTY);
1982 goto out_trans_cancel;
1983 }
1984 }
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 XFS_BMAP_INIT(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001987 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001988 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (error) {
1990 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001991 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1994
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001995 /*
1996 * Bump the in memory generation count on the parent
1997 * directory so that other can know that it has changed.
1998 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 dp->i_gen++;
2000 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2001
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002002 if (is_dir) {
2003 /*
2004 * Drop the link from ip's "..".
2005 */
2006 error = xfs_droplink(tp, dp);
2007 if (error)
2008 goto out_bmap_cancel;
2009
2010 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11002011 * Drop the "." link from ip to self.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002012 */
2013 error = xfs_droplink(tp, ip);
2014 if (error)
2015 goto out_bmap_cancel;
2016 } else {
2017 /*
2018 * When removing a non-directory we need to log the parent
2019 * inode here for the i_gen update. For a directory this is
2020 * done implicitly by the xfs_droplink call for the ".." entry.
2021 */
2022 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002025 /*
Christoph Hellwig2b7035f2008-10-30 17:55:18 +11002026 * Drop the link from dp to ip.
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002027 */
2028 error = xfs_droplink(tp, ip);
2029 if (error)
2030 goto out_bmap_cancel;
2031
2032 /*
2033 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 * we are in the transaction.
2035 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002036 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 * If this is a synchronous mount, make sure that the
2040 * remove transaction goes to disk before returning to
2041 * the user.
2042 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002043 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002046 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002047 if (error)
2048 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002050 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002051 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002055 * If we are using filestreams, kill the stream association.
2056 * If the file is still open it may get a new one but that
2057 * will get killed on last close in xfs_close() so we don't
2058 * have to worry about that.
2059 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002060 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10002061 xfs_filestream_deassociate(ip);
2062
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002063 xfs_itrace_exit(ip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002064 xfs_itrace_exit(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002067 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002068 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2069 NULL, DM_RIGHT_NULL, name->name, NULL,
2070 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return error;
2074
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002075 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 xfs_bmap_cancel(&free_list);
2077 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002078 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 xfs_trans_cancel(tp, cancel_flags);
2080 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Christoph Hellwig993386c2007-08-28 16:12:30 +10002083int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002085 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002086 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002087 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002089 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int error;
2092 xfs_bmap_free_t free_list;
2093 xfs_fsblock_t first_block;
2094 int cancel_flags;
2095 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002098 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002099 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002101 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (XFS_FORCED_SHUTDOWN(mp))
2104 return XFS_ERROR(EIO);
2105
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002106 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002108 tdp, DM_RIGHT_NULL,
2109 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002110 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (error)
2112 return error;
2113 }
2114
2115 /* Return through std_return after this point. */
2116
2117 error = XFS_QM_DQATTACH(mp, sip, 0);
2118 if (!error && sip != tdp)
2119 error = XFS_QM_DQATTACH(mp, tdp, 0);
2120 if (error)
2121 goto std_return;
2122
2123 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2124 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002125 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2127 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2128 if (error == ENOSPC) {
2129 resblks = 0;
2130 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2131 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2132 }
2133 if (error) {
2134 cancel_flags = 0;
2135 goto error_return;
2136 }
2137
Christoph Hellwige1cccd92008-08-13 16:18:07 +10002138 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 /*
2141 * Increment vnode ref counts since xfs_trans_commit &
2142 * xfs_trans_cancel will both unlock the inodes and
2143 * decrement the associated ref counts.
2144 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002145 IHOLD(sip);
2146 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2148 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2149
2150 /*
2151 * If the source has too many links, we can't make any more to it.
2152 */
2153 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2154 error = XFS_ERROR(EMLINK);
2155 goto error_return;
2156 }
2157
Nathan Scott365ca832005-06-21 15:39:12 +10002158 /*
2159 * If we are using project inheritance, we only allow hard link
2160 * creation in our tree when the project IDs are the same; else
2161 * the tree quota mechanism could be circumvented.
2162 */
2163 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2164 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002165 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002166 goto error_return;
2167 }
2168
Barry Naujok556b8b12008-04-10 12:22:07 +10002169 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2170 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto error_return;
2172
2173 XFS_BMAP_INIT(&free_list, &first_block);
2174
Barry Naujok556b8b12008-04-10 12:22:07 +10002175 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2176 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (error)
2178 goto abort_return;
2179 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2180 tdp->i_gen++;
2181 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2182
2183 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002184 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 /*
2188 * If this is a synchronous mount, make sure that the
2189 * link transaction goes to disk before returning to
2190 * the user.
2191 */
2192 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2193 xfs_trans_set_sync(tp);
2194 }
2195
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002196 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (error) {
2198 xfs_bmap_cancel(&free_list);
2199 goto abort_return;
2200 }
2201
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002202 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002203 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 /* Fall through to std_return with error = 0. */
2207std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002208 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002210 tdp, DM_RIGHT_NULL,
2211 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002212 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
2214 return error;
2215
2216 abort_return:
2217 cancel_flags |= XFS_TRANS_ABORT;
2218 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 error_return:
2221 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 goto std_return;
2223}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002224
2225
Christoph Hellwig993386c2007-08-28 16:12:30 +10002226int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002228 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002229 struct xfs_name *dir_name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002230 mode_t mode,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002231 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 cred_t *credp)
2233{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002234 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 xfs_inode_t *cdp; /* inode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 int cancel_flags;
2238 int error;
2239 int committed;
2240 xfs_bmap_free_t free_list;
2241 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002242 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 boolean_t created = B_FALSE;
2244 int dm_event_sent = 0;
2245 xfs_prid_t prid;
2246 struct xfs_dquot *udqp, *gdqp;
2247 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 if (XFS_FORCED_SHUTDOWN(mp))
2250 return XFS_ERROR(EIO);
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002254 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002256 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002257 DM_RIGHT_NULL, dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002258 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (error)
2260 return error;
2261 dm_event_sent = 1;
2262 }
2263
2264 /* Return through std_return after this point. */
2265
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002266 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 mp = dp->i_mount;
2269 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002270 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2271 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 else
2273 prid = (xfs_prid_t)dfltprid;
2274
2275 /*
2276 * Make sure that we have allocated dquot(s) on disk.
2277 */
2278 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01002279 current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2281 if (error)
2282 goto std_return;
2283
2284 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2285 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002286 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2288 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2289 if (error == ENOSPC) {
2290 resblks = 0;
2291 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2292 XFS_TRANS_PERM_LOG_RES,
2293 XFS_MKDIR_LOG_COUNT);
2294 }
2295 if (error) {
2296 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 goto error_return;
2298 }
2299
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002300 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002301 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 /*
2304 * Check for directory link count overflow.
2305 */
2306 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2307 error = XFS_ERROR(EMLINK);
2308 goto error_return;
2309 }
2310
2311 /*
2312 * Reserve disk quota and the inode.
2313 */
2314 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2315 if (error)
2316 goto error_return;
2317
Barry Naujok556b8b12008-04-10 12:22:07 +10002318 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2319 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 goto error_return;
2321 /*
2322 * create the directory inode.
2323 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002324 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 0, credp, prid, resblks > 0,
2326 &cdp, NULL);
2327 if (error) {
2328 if (error == ENOSPC)
2329 goto error_return;
2330 goto abort_return;
2331 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002332 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 /*
2335 * Now we add the directory inode to the transaction.
2336 * We waited until now since xfs_dir_ialloc might start
2337 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002338 * earlier, the locks might have gotten released. An error
2339 * from here on will result in the transaction cancel
2340 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002342 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002344 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 XFS_BMAP_INIT(&free_list, &first_block);
2347
Barry Naujok556b8b12008-04-10 12:22:07 +10002348 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2349 &first_block, &free_list, resblks ?
2350 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (error) {
2352 ASSERT(error != ENOSPC);
2353 goto error1;
2354 }
2355 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2356
2357 /*
2358 * Bump the in memory version number of the parent directory
2359 * so that other processes accessing it will recognize that
2360 * the directory has changed.
2361 */
2362 dp->i_gen++;
2363
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002364 error = xfs_dir_init(tp, cdp, dp);
2365 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 cdp->i_gen = 1;
2369 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002370 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 created = B_TRUE;
2374
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002375 *ipp = cdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 IHOLD(cdp);
2377
2378 /*
2379 * Attach the dquots to the new inode and modify the icount incore.
2380 */
2381 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2382
2383 /*
2384 * If this is a synchronous mount, make sure that the
2385 * mkdir transaction goes to disk before returning to
2386 * the user.
2387 */
2388 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2389 xfs_trans_set_sync(tp);
2390 }
2391
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002392 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (error) {
2394 IRELE(cdp);
2395 goto error2;
2396 }
2397
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002398 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 XFS_QM_DQRELE(mp, udqp);
2400 XFS_QM_DQRELE(mp, gdqp);
2401 if (error) {
2402 IRELE(cdp);
2403 }
2404
2405 /* Fall through to std_return with error = 0 or errno from
2406 * xfs_trans_commit. */
2407
2408std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002409 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002410 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002412 dp, DM_RIGHT_NULL,
2413 created ? cdp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002415 dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002416 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
2418 return error;
2419
2420 error2:
2421 error1:
2422 xfs_bmap_cancel(&free_list);
2423 abort_return:
2424 cancel_flags |= XFS_TRANS_ABORT;
2425 error_return:
2426 xfs_trans_cancel(tp, cancel_flags);
2427 XFS_QM_DQRELE(mp, udqp);
2428 XFS_QM_DQRELE(mp, gdqp);
2429
Christoph Hellwig993386c2007-08-28 16:12:30 +10002430 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 goto std_return;
2434}
2435
Christoph Hellwig993386c2007-08-28 16:12:30 +10002436int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002438 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002439 struct xfs_name *link_name,
2440 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002441 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002442 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 cred_t *credp)
2444{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002445 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 xfs_inode_t *ip;
2448 int error;
2449 int pathlen;
2450 xfs_bmap_free_t free_list;
2451 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002452 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 uint cancel_flags;
2454 int committed;
2455 xfs_fileoff_t first_fsb;
2456 xfs_filblks_t fs_blocks;
2457 int nmaps;
2458 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2459 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002460 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 int byte_cnt;
2462 int n;
2463 xfs_buf_t *bp;
2464 xfs_prid_t prid;
2465 struct xfs_dquot *udqp, *gdqp;
2466 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Christoph Hellwig3937be52008-03-06 13:46:19 +11002468 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 error = 0;
2470 ip = NULL;
2471 tp = NULL;
2472
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002473 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 if (XFS_FORCED_SHUTDOWN(mp))
2476 return XFS_ERROR(EIO);
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /*
2479 * Check component lengths of the target path name.
2480 */
2481 pathlen = strlen(target_path);
2482 if (pathlen >= MAXPATHLEN) /* total string too long */
2483 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002485 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002486 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002488 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (error)
2490 return error;
2491 }
2492
2493 /* Return through std_return after this point. */
2494
2495 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002496 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2497 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 else
2499 prid = (xfs_prid_t)dfltprid;
2500
2501 /*
2502 * Make sure that we have allocated dquot(s) on disk.
2503 */
2504 error = XFS_QM_DQVOPALLOC(mp, dp,
David Howells9e2b2dc2008-08-13 16:20:04 +01002505 current_fsuid(), current_fsgid(), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2507 if (error)
2508 goto std_return;
2509
2510 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2511 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2512 /*
2513 * The symlink will fit into the inode data fork?
2514 * There can't be any attributes so we get the whole variable part.
2515 */
2516 if (pathlen <= XFS_LITINO(mp))
2517 fs_blocks = 0;
2518 else
2519 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002520 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2522 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2523 if (error == ENOSPC && fs_blocks == 0) {
2524 resblks = 0;
2525 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2526 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2527 }
2528 if (error) {
2529 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 goto error_return;
2531 }
2532
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002533 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002534 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 /*
2537 * Check whether the directory allows new symlinks or not.
2538 */
2539 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2540 error = XFS_ERROR(EPERM);
2541 goto error_return;
2542 }
2543
2544 /*
2545 * Reserve disk quota : blocks and inode.
2546 */
2547 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2548 if (error)
2549 goto error_return;
2550
2551 /*
2552 * Check for ability to enter directory entry, if no space reserved.
2553 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002554 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2555 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 goto error_return;
2557 /*
2558 * Initialize the bmap freelist prior to calling either
2559 * bmapi or the directory create code.
2560 */
2561 XFS_BMAP_INIT(&free_list, &first_block);
2562
2563 /*
2564 * Allocate an inode for the symlink.
2565 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002566 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 1, 0, credp, prid, resblks > 0, &ip, NULL);
2568 if (error) {
2569 if (error == ENOSPC)
2570 goto error_return;
2571 goto error1;
2572 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002573 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Christoph Hellwig993386c2007-08-28 16:12:30 +10002575 /*
2576 * An error after we've joined dp to the transaction will result in the
2577 * transaction cancel unlocking dp so don't do it explicitly in the
2578 * error path.
2579 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002580 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002582 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 /*
2585 * Also attach the dquot(s) to it, if applicable.
2586 */
2587 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2588
2589 if (resblks)
2590 resblks -= XFS_IALLOC_SPACE_RES(mp);
2591 /*
2592 * If the symlink will fit into the inode, write it inline.
2593 */
2594 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2595 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2596 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2597 ip->i_d.di_size = pathlen;
2598
2599 /*
2600 * The inode was initially created in extent format.
2601 */
2602 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2603 ip->i_df.if_flags |= XFS_IFINLINE;
2604
2605 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2606 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2607
2608 } else {
2609 first_fsb = 0;
2610 nmaps = SYMLINK_MAPS;
2611
2612 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2613 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2614 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002615 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 if (error) {
2617 goto error1;
2618 }
2619
2620 if (resblks)
2621 resblks -= fs_blocks;
2622 ip->i_d.di_size = pathlen;
2623 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2624
2625 cur_chunk = target_path;
2626 for (n = 0; n < nmaps; n++) {
2627 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2628 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2629 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2630 BTOBB(byte_cnt), 0);
2631 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2632 if (pathlen < byte_cnt) {
2633 byte_cnt = pathlen;
2634 }
2635 pathlen -= byte_cnt;
2636
2637 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2638 cur_chunk += byte_cnt;
2639
2640 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2641 }
2642 }
2643
2644 /*
2645 * Create the directory entry for the symlink.
2646 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002647 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2648 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002649 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2652 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2653
2654 /*
2655 * Bump the in memory version number of the parent directory
2656 * so that other processes accessing it will recognize that
2657 * the directory has changed.
2658 */
2659 dp->i_gen++;
2660
2661 /*
2662 * If this is a synchronous mount, make sure that the
2663 * symlink transaction goes to disk before returning to
2664 * the user.
2665 */
2666 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2667 xfs_trans_set_sync(tp);
2668 }
2669
2670 /*
2671 * xfs_trans_commit normally decrements the vnode ref count
2672 * when it unlocks the inode. Since we want to return the
2673 * vnode to the caller, we bump the vnode ref count now.
2674 */
2675 IHOLD(ip);
2676
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002677 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 if (error) {
2679 goto error2;
2680 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002681 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 XFS_QM_DQRELE(mp, udqp);
2683 XFS_QM_DQRELE(mp, gdqp);
2684
2685 /* Fall through to std_return with error = 0 or errno from
2686 * xfs_trans_commit */
2687std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10002688 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002690 dp, DM_RIGHT_NULL,
2691 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002692 DM_RIGHT_NULL, link_name->name,
2693 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
2695
Christoph Hellwig3937be52008-03-06 13:46:19 +11002696 if (!error)
2697 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return error;
2699
2700 error2:
2701 IRELE(ip);
2702 error1:
2703 xfs_bmap_cancel(&free_list);
2704 cancel_flags |= XFS_TRANS_ABORT;
2705 error_return:
2706 xfs_trans_cancel(tp, cancel_flags);
2707 XFS_QM_DQRELE(mp, udqp);
2708 XFS_QM_DQRELE(mp, gdqp);
2709
Christoph Hellwig993386c2007-08-28 16:12:30 +10002710 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 goto std_return;
2714}
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002718 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 int flags)
2720{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002721 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 int error = 0;
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (XFS_FORCED_SHUTDOWN(mp))
2725 return XFS_ERROR(EIO);
2726
2727 /*
2728 * Bypass inodes which have already been cleaned by
2729 * the inode flush clustering code inside xfs_iflush
2730 */
David Chinner33540402008-03-06 13:43:59 +11002731 if (xfs_inode_clean(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return 0;
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /*
2735 * We make this non-blocking if the inode is contended,
2736 * return EAGAIN to indicate to the caller that they
2737 * did not succeed. This prevents the flush path from
2738 * blocking on inodes inside another operation right
2739 * now, they get caught later by xfs_sync.
2740 */
David Chinnera3f74ff2008-03-06 13:43:42 +11002741 if (flags & FLUSH_SYNC) {
2742 xfs_ilock(ip, XFS_ILOCK_SHARED);
2743 xfs_iflock(ip);
2744 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
2745 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
2746 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return EAGAIN;
2748 }
David Chinnera3f74ff2008-03-06 13:43:42 +11002749 } else {
2750 return EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752
David Chinnera3f74ff2008-03-06 13:43:42 +11002753 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
2754 : XFS_IFLUSH_ASYNC_NOBLOCK);
2755 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return error;
2758}
2759
Christoph Hellwig993386c2007-08-28 16:12:30 +10002760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002762xfs_set_dmattrs(
2763 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002765 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002767 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 int error;
2770
2771 if (!capable(CAP_SYS_ADMIN))
2772 return XFS_ERROR(EPERM);
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (XFS_FORCED_SHUTDOWN(mp))
2775 return XFS_ERROR(EIO);
2776
2777 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2778 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2779 if (error) {
2780 xfs_trans_cancel(tp, 0);
2781 return error;
2782 }
2783 xfs_ilock(ip, XFS_ILOCK_EXCL);
2784 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2785
Christoph Hellwig613d7042007-10-11 17:44:08 +10002786 ip->i_d.di_dmevmask = evmask;
2787 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2790 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002791 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 return error;
2794}
2795
Christoph Hellwig993386c2007-08-28 16:12:30 +10002796int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002798 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002801 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002803 ASSERT(!VN_MAPPED(VFS_I(ip)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 /* bad inode, get out here ASAP */
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002806 if (VN_BAD(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 xfs_ireclaim(ip);
2808 return 0;
2809 }
2810
Christoph Hellwigb677c212007-08-29 11:46:28 +10002811 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10002813 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11002815 /*
2816 * Make sure the atime in the XFS inode is correct before freeing the
2817 * Linux inode.
2818 */
2819 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
David Chinner4c606582006-11-11 18:05:00 +11002821 /*
2822 * If we have nothing to flush with this inode then complete the
2823 * teardown now, otherwise break the link between the xfs inode and the
2824 * linux inode and clean up the xfs inode later. This avoids flushing
2825 * the inode to disk during the delete operation itself.
2826 *
2827 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2828 * first to ensure that xfs_iunpin() will never see an xfs inode
2829 * that has a linux inode being reclaimed. Synchronisation is provided
2830 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 */
2832 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2833 xfs_ilock(ip, XFS_ILOCK_EXCL);
2834 xfs_iflock(ip);
David Chinnerbf904242008-10-30 17:36:14 +11002835 xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner1dc33182008-10-30 17:37:15 +11002836 return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
David Chinner11654512008-10-30 17:37:49 +11002838 xfs_inode_set_reclaim_tag(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return 0;
2840}
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842/*
2843 * xfs_alloc_file_space()
2844 * This routine allocates disk space for the given file.
2845 *
2846 * If alloc_type == 0, this request is for an ALLOCSP type
2847 * request which will change the file size. In this case, no
2848 * DMAPI event will be generated by the call. A TRUNCATE event
2849 * will be generated later by xfs_setattr.
2850 *
2851 * If alloc_type != 0, this request is for a RESVSP type
2852 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2853 * lower block boundary byte address is less than the file's
2854 * length.
2855 *
2856 * RETURNS:
2857 * 0 on success
2858 * errno on error
2859 *
2860 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002861STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862xfs_alloc_file_space(
2863 xfs_inode_t *ip,
2864 xfs_off_t offset,
2865 xfs_off_t len,
2866 int alloc_type,
2867 int attr_flags)
2868{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002869 xfs_mount_t *mp = ip->i_mount;
2870 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 xfs_filblks_t allocated_fsb;
2872 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002873 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002875 xfs_fsblock_t firstfsb;
2876 int nimaps;
2877 int bmapi_flag;
2878 int quota_flag;
2879 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002881 xfs_bmbt_irec_t imaps[1], *imapp;
2882 xfs_bmap_free_t free_list;
2883 uint qblocks, resblks, resrtextents;
2884 int committed;
2885 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002887 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 if (XFS_FORCED_SHUTDOWN(mp))
2890 return XFS_ERROR(EIO);
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2893 return error;
2894
2895 if (len <= 0)
2896 return XFS_ERROR(EINVAL);
2897
David Chinner957d0eb2007-06-18 16:50:37 +10002898 rt = XFS_IS_REALTIME_INODE(ip);
2899 extsz = xfs_get_extsz_hint(ip);
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11002903 nimaps = 1;
2904 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2906 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2907
2908 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002909 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10002910 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002911 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 xfs_off_t end_dmi_offset;
2913
2914 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002915 if (end_dmi_offset > ip->i_size)
2916 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002917 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2918 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002920 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 }
2922
2923 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002924 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 */
2926retry:
2927 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002928 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Nathan Scottdd9f4382006-01-11 15:28:28 +11002930 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11002931 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002932 */
2933 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002935 do_div(s, extsz);
2936 s *= extsz;
2937 e = startoffset_fsb + allocatesize_fsb;
2938 if ((temp = do_mod(startoffset_fsb, extsz)))
2939 e += temp;
2940 if ((temp = do_mod(e, extsz)))
2941 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11002943 s = 0;
2944 e = allocatesize_fsb;
2945 }
2946
2947 if (unlikely(rt)) {
2948 resrtextents = qblocks = (uint)(e - s);
2949 resrtextents /= mp->m_sb.sb_rextsize;
2950 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2951 quota_flag = XFS_QMOPT_RES_RTBLKS;
2952 } else {
2953 resrtextents = 0;
2954 resblks = qblocks = \
2955 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2956 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958
2959 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002960 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 */
2962 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002963 error = xfs_trans_reserve(tp, resblks,
2964 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 XFS_TRANS_PERM_LOG_RES,
2966 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002968 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 */
2970 if (error) {
2971 /*
2972 * Free the transaction structure.
2973 */
2974 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2975 xfs_trans_cancel(tp, 0);
2976 break;
2977 }
2978 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002979 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
2980 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (error)
2982 goto error1;
2983
2984 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2985 xfs_trans_ihold(tp, ip);
2986
2987 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002988 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 */
2990 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10002991 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11002992 allocatesize_fsb, bmapi_flag,
2993 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002994 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 if (error) {
2996 goto error0;
2997 }
2998
2999 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003000 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003002 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 if (error) {
3004 goto error0;
3005 }
3006
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003007 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3009 if (error) {
3010 break;
3011 }
3012
3013 allocated_fsb = imapp->br_blockcount;
3014
Nathan Scottdd9f4382006-01-11 15:28:28 +11003015 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 error = XFS_ERROR(ENOSPC);
3017 break;
3018 }
3019
3020 startoffset_fsb += allocated_fsb;
3021 allocatesize_fsb -= allocated_fsb;
3022 }
3023dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003024 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003025 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003027 ip, DM_RIGHT_NULL,
3028 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3030 if (error == 0)
3031 goto retry; /* Maybe DMAPI app. has made space */
3032 /* else fall through with error from XFS_SEND_DATA */
3033 }
3034
3035 return error;
3036
Nathan Scottdd9f4382006-01-11 15:28:28 +11003037error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003039 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3040
3041error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3043 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3044 goto dmapi_enospc_check;
3045}
3046
3047/*
3048 * Zero file bytes between startoff and endoff inclusive.
3049 * The iolock is held exclusive and no blocks are buffered.
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10003050 *
3051 * This function is used by xfs_free_file_space() to zero
3052 * partial blocks when the range to free is not block aligned.
3053 * When unreserving space with boundaries that are not block
3054 * aligned we round up the start and round down the end
3055 * boundaries and then use this function to zero the parts of
3056 * the blocks that got dropped during the rounding.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 */
3058STATIC int
3059xfs_zero_remaining_bytes(
3060 xfs_inode_t *ip,
3061 xfs_off_t startoff,
3062 xfs_off_t endoff)
3063{
3064 xfs_bmbt_irec_t imap;
3065 xfs_fileoff_t offset_fsb;
3066 xfs_off_t lastoffset;
3067 xfs_off_t offset;
3068 xfs_buf_t *bp;
3069 xfs_mount_t *mp = ip->i_mount;
3070 int nimap;
3071 int error = 0;
3072
Lachlan McIlroy2fd6f6e2008-09-17 16:52:50 +10003073 /*
3074 * Avoid doing I/O beyond eof - it's not necessary
3075 * since nothing can read beyond eof. The space will
3076 * be zeroed when the file is extended anyway.
3077 */
3078 if (startoff >= ip->i_size)
3079 return 0;
3080
3081 if (endoff > ip->i_size)
3082 endoff = ip->i_size;
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003085 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 mp->m_rtdev_targp : mp->m_ddev_targp);
3087
3088 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3089 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3090 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003091 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003092 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (error || nimap < 1)
3094 break;
3095 ASSERT(imap.br_blockcount >= 1);
3096 ASSERT(imap.br_startoff == offset_fsb);
3097 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3098 if (lastoffset > endoff)
3099 lastoffset = endoff;
3100 if (imap.br_startblock == HOLESTARTBLOCK)
3101 continue;
3102 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3103 if (imap.br_state == XFS_EXT_UNWRITTEN)
3104 continue;
3105 XFS_BUF_UNDONE(bp);
3106 XFS_BUF_UNWRITE(bp);
3107 XFS_BUF_READ(bp);
3108 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3109 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003110 error = xfs_iowait(bp);
3111 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3113 mp, bp, XFS_BUF_ADDR(bp));
3114 break;
3115 }
3116 memset(XFS_BUF_PTR(bp) +
3117 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3118 0, lastoffset - offset + 1);
3119 XFS_BUF_UNDONE(bp);
3120 XFS_BUF_UNREAD(bp);
3121 XFS_BUF_WRITE(bp);
3122 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003123 error = xfs_iowait(bp);
3124 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3126 mp, bp, XFS_BUF_ADDR(bp));
3127 break;
3128 }
3129 }
3130 xfs_buf_free(bp);
3131 return error;
3132}
3133
3134/*
3135 * xfs_free_file_space()
3136 * This routine frees disk space for the given file.
3137 *
3138 * This routine is only called by xfs_change_file_space
3139 * for an UNRESVSP type call.
3140 *
3141 * RETURNS:
3142 * 0 on success
3143 * errno on error
3144 *
3145 */
3146STATIC int
3147xfs_free_file_space(
3148 xfs_inode_t *ip,
3149 xfs_off_t offset,
3150 xfs_off_t len,
3151 int attr_flags)
3152{
3153 int committed;
3154 int done;
3155 xfs_off_t end_dmi_offset;
3156 xfs_fileoff_t endoffset_fsb;
3157 int error;
3158 xfs_fsblock_t firstfsb;
3159 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 xfs_bmbt_irec_t imap;
3161 xfs_off_t ioffset;
3162 xfs_extlen_t mod=0;
3163 xfs_mount_t *mp;
3164 int nimap;
3165 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10003166 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 int rt;
3168 xfs_fileoff_t startoffset_fsb;
3169 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003170 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 mp = ip->i_mount;
3173
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003174 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3177 return error;
3178
3179 error = 0;
3180 if (len <= 0) /* if nothing being freed */
3181 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003182 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3184 end_dmi_offset = offset + len;
3185 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3186
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003187 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003188 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003189 if (end_dmi_offset > ip->i_size)
3190 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003191 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 offset, end_dmi_offset - offset,
3193 AT_DELAY_FLAG(attr_flags), NULL);
3194 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003195 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
3197
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003198 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003199 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11003200 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10003202 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11003203 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003204
Tim Shimmine6a4b372007-11-23 16:30:42 +11003205 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003207
Christoph Hellwigdf80c932008-08-13 16:22:09 +10003208 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003209 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3210 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10003211 if (error)
3212 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003213 }
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 /*
3216 * Need to zero the stuff we're not freeing, on disk.
3217 * If its a realtime file & can't use unwritten extents then we
3218 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3219 * will take care of it for us.
3220 */
Eric Sandeen62118702008-03-06 13:44:28 +11003221 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003223 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003224 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 if (error)
3226 goto out_unlock_iolock;
3227 ASSERT(nimap == 0 || nimap == 1);
3228 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3229 xfs_daddr_t block;
3230
3231 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3232 block = imap.br_startblock;
3233 mod = do_div(block, mp->m_sb.sb_rextsize);
3234 if (mod)
3235 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3236 }
3237 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003238 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003239 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (error)
3241 goto out_unlock_iolock;
3242 ASSERT(nimap == 0 || nimap == 1);
3243 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3244 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3245 mod++;
3246 if (mod && (mod != mp->m_sb.sb_rextsize))
3247 endoffset_fsb -= mod;
3248 }
3249 }
3250 if ((done = (endoffset_fsb <= startoffset_fsb)))
3251 /*
3252 * One contiguous piece to clear
3253 */
3254 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3255 else {
3256 /*
3257 * Some full blocks, possibly two pieces to clear
3258 */
3259 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3260 error = xfs_zero_remaining_bytes(ip, offset,
3261 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3262 if (!error &&
3263 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3264 error = xfs_zero_remaining_bytes(ip,
3265 XFS_FSB_TO_B(mp, endoffset_fsb),
3266 offset + len - 1);
3267 }
3268
3269 /*
3270 * free file space until done or until there is an error
3271 */
3272 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3273 while (!error && !done) {
3274
3275 /*
David Chinner91ebecc2007-07-19 16:28:30 +10003276 * allocate and setup the transaction. Allow this
3277 * transaction to dip into the reserve blocks to ensure
3278 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 */
3280 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10003281 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 error = xfs_trans_reserve(tp,
3283 resblks,
3284 XFS_WRITE_LOG_RES(mp),
3285 0,
3286 XFS_TRANS_PERM_LOG_RES,
3287 XFS_WRITE_LOG_COUNT);
3288
3289 /*
3290 * check for running out of space
3291 */
3292 if (error) {
3293 /*
3294 * Free the transaction structure.
3295 */
3296 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3297 xfs_trans_cancel(tp, 0);
3298 break;
3299 }
3300 xfs_ilock(ip, XFS_ILOCK_EXCL);
3301 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003302 ip->i_udquot, ip->i_gdquot, resblks, 0,
3303 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 if (error)
3305 goto error1;
3306
3307 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3308 xfs_trans_ihold(tp, ip);
3309
3310 /*
3311 * issue the bunmapi() call to free the blocks
3312 */
3313 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003314 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003316 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 if (error) {
3318 goto error0;
3319 }
3320
3321 /*
3322 * complete the transaction
3323 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003324 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 if (error) {
3326 goto error0;
3327 }
3328
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003329 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3331 }
3332
3333 out_unlock_iolock:
3334 if (need_iolock)
3335 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3336 return error;
3337
3338 error0:
3339 xfs_bmap_cancel(&free_list);
3340 error1:
3341 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3342 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3343 XFS_ILOCK_EXCL);
3344 return error;
3345}
3346
3347/*
3348 * xfs_change_file_space()
3349 * This routine allocates or frees disk space for the given file.
3350 * The user specified parameters are checked for alignment and size
3351 * limitations.
3352 *
3353 * RETURNS:
3354 * 0 on success
3355 * errno on error
3356 *
3357 */
3358int
3359xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003360 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 int cmd,
3362 xfs_flock64_t *bf,
3363 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 int attr_flags)
3365{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003366 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 int clrprealloc;
3368 int error;
3369 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 int setprealloc;
3371 xfs_off_t startoffset;
3372 xfs_off_t llen;
3373 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003374 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003376 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Christoph Hellwig993386c2007-08-28 16:12:30 +10003378 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return XFS_ERROR(EINVAL);
3380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 switch (bf->l_whence) {
3382 case 0: /*SEEK_SET*/
3383 break;
3384 case 1: /*SEEK_CUR*/
3385 bf->l_start += offset;
3386 break;
3387 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003388 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 break;
3390 default:
3391 return XFS_ERROR(EINVAL);
3392 }
3393
3394 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3395
3396 if ( (bf->l_start < 0)
3397 || (bf->l_start > XFS_MAXIOFFSET(mp))
3398 || (bf->l_start + llen < 0)
3399 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3400 return XFS_ERROR(EINVAL);
3401
3402 bf->l_whence = 0;
3403
3404 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003405 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
3407 /*
3408 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3409 * file space.
3410 * These calls do NOT zero the data space allocated to the file,
3411 * nor do they change the file size.
3412 *
3413 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3414 * space.
3415 * These calls cause the new file data to be zeroed and the file
3416 * size to be changed.
3417 */
3418 setprealloc = clrprealloc = 0;
3419
3420 switch (cmd) {
3421 case XFS_IOC_RESVSP:
3422 case XFS_IOC_RESVSP64:
3423 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3424 1, attr_flags);
3425 if (error)
3426 return error;
3427 setprealloc = 1;
3428 break;
3429
3430 case XFS_IOC_UNRESVSP:
3431 case XFS_IOC_UNRESVSP64:
3432 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3433 attr_flags)))
3434 return error;
3435 break;
3436
3437 case XFS_IOC_ALLOCSP:
3438 case XFS_IOC_ALLOCSP64:
3439 case XFS_IOC_FREESP:
3440 case XFS_IOC_FREESP64:
3441 if (startoffset > fsize) {
3442 error = xfs_alloc_file_space(ip, fsize,
3443 startoffset - fsize, 0, attr_flags);
3444 if (error)
3445 break;
3446 }
3447
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003448 iattr.ia_valid = ATTR_SIZE;
3449 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Christoph Hellwigea5a3dc82008-10-30 18:27:48 +11003451 error = xfs_setattr(ip, &iattr, attr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
3453 if (error)
3454 return error;
3455
3456 clrprealloc = 1;
3457 break;
3458
3459 default:
3460 ASSERT(0);
3461 return XFS_ERROR(EINVAL);
3462 }
3463
3464 /*
3465 * update the inode timestamp, mode, and prealloc flag bits
3466 */
3467 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3468
3469 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3470 0, 0, 0))) {
3471 /* ASSERT(0); */
3472 xfs_trans_cancel(tp, 0);
3473 return error;
3474 }
3475
3476 xfs_ilock(ip, XFS_ILOCK_EXCL);
3477
3478 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3479 xfs_trans_ihold(tp, ip);
3480
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003481 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 ip->i_d.di_mode &= ~S_ISUID;
3483
3484 /*
3485 * Note that we don't have to worry about mandatory
3486 * file locking being disabled here because we only
3487 * clear the S_ISGID bit if the Group execute bit is
3488 * on, but if it was on then mandatory locking wouldn't
3489 * have been enabled.
3490 */
3491 if (ip->i_d.di_mode & S_IXGRP)
3492 ip->i_d.di_mode &= ~S_ISGID;
3493
3494 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3495 }
3496 if (setprealloc)
3497 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3498 else if (clrprealloc)
3499 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3500
3501 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3502 xfs_trans_set_sync(tp);
3503
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003504 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
3506 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3507
3508 return error;
3509}