blob: 21da312dd8b212ec9bcf86a87717679db40c5630 [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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 int flags,
83 cred_t *credp)
84{
Christoph Hellwig993386c2007-08-28 16:12:30 +100085 xfs_mount_t *mp = ip->i_mount;
David Chinnere4f75292008-08-13 16:00:45 +100086 struct inode *inode = VFS_I(ip);
Christoph Hellwig0f285c82008-07-18 17:13:28 +100087 int mask = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int code;
90 uint lock_flags;
91 uint commit_flags=0;
92 uid_t uid=0, iuid=0;
93 gid_t gid=0, igid=0;
94 int timeflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
96 int file_owner;
Dean Roehrich5fcbab32005-05-05 13:27:19 -070097 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Lachlan McIlroycf441ee2008-02-07 16:42:19 +110099 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000101 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return XFS_ERROR(EROFS);
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (XFS_FORCED_SHUTDOWN(mp))
105 return XFS_ERROR(EIO);
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 olddquot1 = olddquot2 = NULL;
108 udqp = gdqp = NULL;
109
110 /*
111 * If disk quotas is on, we make sure that the dquots do exist on disk,
112 * before we start any other transactions. Trying to do this later
113 * is messy. We don't care to take a readlock to look at the ids
114 * in inode here, because we can't hold it across the trans_reserve.
115 * If the IDs do change before we take the ilock, we're covered
116 * because the i_*dquot fields will get updated anyway.
117 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000118 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 uint qflags = 0;
120
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000121 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
122 uid = iattr->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 qflags |= XFS_QMOPT_UQUOTA;
124 } else {
125 uid = ip->i_d.di_uid;
126 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000127 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
128 gid = iattr->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 qflags |= XFS_QMOPT_GQUOTA;
130 } else {
131 gid = ip->i_d.di_gid;
132 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*
135 * We take a reference when we initialize udqp and gdqp,
136 * so it is important that we never blindly double trip on
137 * the same variable. See xfs_create() for an example.
138 */
139 ASSERT(udqp == NULL);
140 ASSERT(gdqp == NULL);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000141 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, ip->i_d.di_projid,
142 qflags, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100144 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
147 /*
148 * For the other attributes, we acquire the inode lock and
149 * first do an error checking pass.
150 */
151 tp = NULL;
152 lock_flags = XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000153 if (flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700154 need_iolock = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000155 if (!(mask & ATTR_SIZE)) {
156 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 (mp->m_flags & XFS_MOUNT_WSYNC)) {
158 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
159 commit_flags = 0;
160 if ((code = xfs_trans_reserve(tp, 0,
161 XFS_ICHANGE_LOG_RES(mp), 0,
162 0, 0))) {
163 lock_flags = 0;
164 goto error_return;
165 }
166 }
167 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000168 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000169 !(flags & XFS_ATTR_DMI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100171 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000172 iattr->ia_size, 0, dmflags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (code) {
174 lock_flags = 0;
175 goto error_return;
176 }
177 }
178 if (need_iolock)
179 lock_flags |= XFS_IOLOCK_EXCL;
180 }
181
182 xfs_ilock(ip, lock_flags);
183
184 /* boolean: are we the file owner? */
185 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
186
187 /*
188 * Change various properties of a file.
189 * Only the owner or users with CAP_FOWNER
190 * capability may do these things.
191 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000192 if (mask & (ATTR_MODE|ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
194 * CAP_FOWNER overrides the following restrictions:
195 *
196 * The user ID of the calling process must be equal
197 * to the file owner ID, except in cases where the
198 * CAP_FSETID capability is applicable.
199 */
200 if (!file_owner && !capable(CAP_FOWNER)) {
201 code = XFS_ERROR(EPERM);
202 goto error_return;
203 }
204
205 /*
206 * CAP_FSETID overrides the following restrictions:
207 *
208 * The effective user ID of the calling process shall match
209 * the file owner when setting the set-user-ID and
210 * set-group-ID bits on that file.
211 *
212 * The effective group ID or one of the supplementary group
213 * IDs of the calling process shall match the group owner of
214 * the file when setting the set-group-ID bit on that file
215 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000216 if (mask & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 mode_t m = 0;
218
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000219 if ((iattr->ia_mode & S_ISUID) && !file_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 m |= S_ISUID;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000221 if ((iattr->ia_mode & S_ISGID) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 !in_group_p((gid_t)ip->i_d.di_gid))
223 m |= S_ISGID;
224#if 0
225 /* Linux allows this, Irix doesn't. */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000226 if ((iattr->ia_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 m |= S_ISVTX;
228#endif
229 if (m && !capable(CAP_FSETID))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000230 iattr->ia_mode &= ~m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232 }
233
234 /*
235 * Change file ownership. Must be the owner or privileged.
236 * If the system was configured with the "restricted_chown"
237 * option, the owner is not permitted to give away the file,
238 * and can change the group id only to a group of which he
239 * or she is a member.
240 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000241 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /*
243 * These IDs could have changed since we last looked at them.
244 * But, we're assured that if the ownership did change
245 * while we didn't have the inode locked, inode's dquot(s)
246 * would have changed also.
247 */
248 iuid = ip->i_d.di_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 igid = ip->i_d.di_gid;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000250 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
251 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /*
254 * CAP_CHOWN overrides the following restrictions:
255 *
256 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
257 * shall override the restriction that a process cannot
258 * change the user ID of a file it owns and the restriction
259 * that the group ID supplied to the chown() function
260 * shall be equal to either the group ID or one of the
261 * supplementary group IDs of the calling process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263 if (restricted_chown &&
264 (iuid != uid || (igid != gid &&
265 !in_group_p((gid_t)gid))) &&
266 !capable(CAP_CHOWN)) {
267 code = XFS_ERROR(EPERM);
268 goto error_return;
269 }
270 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000271 * Do a quota reservation only if uid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * going to change.
273 */
274 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
275 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
276 ASSERT(tp);
277 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
278 capable(CAP_FOWNER) ?
279 XFS_QMOPT_FORCE_RES : 0);
280 if (code) /* out of quota */
281 goto error_return;
282 }
283 }
284
285 /*
286 * Truncate file. Must have write permission and not be a directory.
287 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000288 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* Short circuit the truncate case for zero length files */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000290 if (iattr->ia_size == 0 &&
291 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 xfs_iunlock(ip, XFS_ILOCK_EXCL);
293 lock_flags &= ~XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000294 if (mask & ATTR_CTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
296 code = 0;
297 goto error_return;
298 }
299
Christoph Hellwig42173f62008-04-22 17:33:25 +1000300 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 code = XFS_ERROR(EISDIR);
302 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000303 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 code = XFS_ERROR(EINVAL);
305 goto error_return;
306 }
307 /*
308 * Make sure that the dquots are attached to the inode.
309 */
310 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
311 goto error_return;
312 }
313
314 /*
315 * Change file access or modified times.
316 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000317 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!file_owner) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000319 if ((mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 !capable(CAP_FOWNER)) {
321 code = XFS_ERROR(EPERM);
322 goto error_return;
323 }
324 }
325 }
326
327 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * Now we can make the changes. Before we join the inode
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000329 * to the transaction, if ATTR_SIZE is set then take care of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * the part of the truncation that must be done without the
331 * inode lock. This needs to be done before joining the inode
332 * to the transaction, because the inode cannot be unlocked
333 * once it is a part of the transaction.
334 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000335 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 code = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000337 if (iattr->ia_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000338 /*
339 * Do the first part of growing a file: zero any data
340 * in the last block that is beyond the old EOF. We
341 * need to do this before the inode is joined to the
342 * transaction to modify the i_size.
343 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000344 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000347
348 /*
349 * We are going to log the inode size change in this
350 * transaction so any previous writes that are beyond the on
351 * disk EOF and the new EOF that have not been written out need
352 * to be written here. If we do not write the data out, we
353 * expose ourselves to the null files problem.
354 *
355 * Only flush from the on disk size to the smaller of the in
356 * memory file size or the new size as that's the range we
357 * really care about here and prevents waiting for other data
358 * not within the range we care about here.
359 */
360 if (!code &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000361 ip->i_size != ip->i_d.di_size &&
362 iattr->ia_size > ip->i_d.di_size) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000363 code = xfs_flush_pages(ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000364 ip->i_d.di_size, iattr->ia_size,
David Chinnerc32676e2007-07-19 16:28:58 +1000365 XFS_B_ASYNC, FI_NONE);
366 }
367
368 /* wait for all I/O to complete */
Christoph Hellwigb677c212007-08-29 11:46:28 +1000369 vn_iowait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (!code)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000372 code = xfs_itruncate_data(ip, iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (code) {
374 ASSERT(tp == NULL);
375 lock_flags &= ~XFS_ILOCK_EXCL;
376 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
377 goto error_return;
378 }
379 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
380 if ((code = xfs_trans_reserve(tp, 0,
381 XFS_ITRUNCATE_LOG_RES(mp), 0,
382 XFS_TRANS_PERM_LOG_RES,
383 XFS_ITRUNCATE_LOG_COUNT))) {
384 xfs_trans_cancel(tp, 0);
385 if (need_iolock)
386 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
387 return code;
388 }
389 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
390 xfs_ilock(ip, XFS_ILOCK_EXCL);
391 }
392
393 if (tp) {
394 xfs_trans_ijoin(tp, ip, lock_flags);
395 xfs_trans_ihold(tp, ip);
396 }
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /*
399 * Truncate file. Must have write permission and not be a directory.
400 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000401 if (mask & ATTR_SIZE) {
David Chinner44d814c2008-03-06 13:45:29 +1100402 /*
403 * Only change the c/mtime if we are changing the size
404 * or we are explicitly asked to change it. This handles
405 * the semantic difference between truncate() and ftruncate()
406 * as implemented in the VFS.
407 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000408 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
David Chinner44d814c2008-03-06 13:45:29 +1100409 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
410
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000411 if (iattr->ia_size > ip->i_size) {
412 ip->i_d.di_size = iattr->ia_size;
413 ip->i_size = iattr->ia_size;
414 if (!(flags & XFS_ATTR_DMI))
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000415 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
416 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000417 } else if (iattr->ia_size <= ip->i_size ||
418 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
420 * signal a sync transaction unless
421 * we're truncating an already unlinked
422 * file on a wsync filesystem
423 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000424 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 XFS_DATA_FORK,
426 ((ip->i_d.di_nlink != 0 ||
427 !(mp->m_flags & XFS_MOUNT_WSYNC))
428 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000429 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000431 /*
432 * Truncated "down", so we're removing references
433 * to old data here - if we now delay flushing for
434 * a long time, we expose ourselves unduly to the
435 * notorious NULL files problem. So, we mark this
436 * vnode and flush it when the file is closed, and
437 * do not wait the usual (long) time for writeout.
438 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000439 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
443 /*
444 * Change file access modes.
445 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000446 if (mask & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 ip->i_d.di_mode &= S_IFMT;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000448 ip->i_d.di_mode |= iattr->ia_mode & ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000450 inode->i_mode &= S_IFMT;
451 inode->i_mode |= iattr->ia_mode & ~S_IFMT;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
454 timeflags |= XFS_ICHGTIME_CHG;
455 }
456
457 /*
458 * Change file ownership. Must be the owner or privileged.
459 * If the system was configured with the "restricted_chown"
460 * option, the owner is not permitted to give away the file,
461 * and can change the group id only to a group of which he
462 * or she is a member.
463 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000464 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
466 * CAP_FSETID overrides the following restrictions:
467 *
468 * The set-user-ID and set-group-ID bits of a file will be
469 * cleared upon successful return from chown()
470 */
471 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
472 !capable(CAP_FSETID)) {
473 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
474 }
475
476 /*
477 * Change the ownerships and register quota modifications
478 * in the transaction.
479 */
480 if (iuid != uid) {
481 if (XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000482 ASSERT(mask & ATTR_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ASSERT(udqp);
484 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
485 &ip->i_udquot, udqp);
486 }
487 ip->i_d.di_uid = uid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000488 inode->i_uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 if (igid != gid) {
491 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000492 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000493 ASSERT(mask & ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 ASSERT(gdqp);
495 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
496 &ip->i_gdquot, gdqp);
497 }
498 ip->i_d.di_gid = gid;
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000499 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
503 timeflags |= XFS_ICHGTIME_CHG;
504 }
505
506
507 /*
508 * Change file access or modified times.
509 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000510 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
511 if (mask & ATTR_ATIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000512 inode->i_atime = iattr->ia_atime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000513 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
514 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 ip->i_update_core = 1;
516 timeflags &= ~XFS_ICHGTIME_ACC;
517 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000518 if (mask & ATTR_MTIME) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000519 inode->i_mtime = iattr->ia_mtime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000520 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
521 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 timeflags &= ~XFS_ICHGTIME_MOD;
523 timeflags |= XFS_ICHGTIME_CHG;
524 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000525 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
527 }
528
529 /*
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000530 * Change file inode change time only if ATTR_CTIME set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * AND we have been called by a DMI function.
532 */
533
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000534 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
Christoph Hellwigf13fae22008-07-21 16:16:15 +1000535 inode->i_ctime = iattr->ia_ctime;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000536 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
537 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 ip->i_update_core = 1;
539 timeflags &= ~XFS_ICHGTIME_CHG;
540 }
541
542 /*
543 * Send out timestamp changes that need to be set to the
544 * current time. Not done when called by a DMI function.
545 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000546 if (timeflags && !(flags & XFS_ATTR_DMI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 xfs_ichgtime(ip, timeflags);
548
549 XFS_STATS_INC(xs_ig_attrchg);
550
551 /*
552 * If this is a synchronous mount, make sure that the
553 * transaction goes to disk before returning to the user.
554 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000555 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * One for the truncate and one for the timestamps since we
557 * don't want to change the timestamps unless we're sure the
558 * truncate worked. Truncates are less than 1% of the laddis
559 * mix so this probably isn't worth the trouble to optimize.
560 */
561 code = 0;
562 if (tp) {
563 if (mp->m_flags & XFS_MOUNT_WSYNC)
564 xfs_trans_set_sync(tp);
565
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000566 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 xfs_iunlock(ip, lock_flags);
570
571 /*
572 * Release any dquot(s) the inode had kept before chown.
573 */
574 XFS_QM_DQRELE(mp, olddquot1);
575 XFS_QM_DQRELE(mp, olddquot2);
576 XFS_QM_DQRELE(mp, udqp);
577 XFS_QM_DQRELE(mp, gdqp);
578
579 if (code) {
580 return code;
581 }
582
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000583 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000584 !(flags & XFS_ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100585 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 NULL, DM_RIGHT_NULL, NULL, NULL,
587 0, 0, AT_DELAY_FLAG(flags));
588 }
589 return 0;
590
591 abort_return:
592 commit_flags |= XFS_TRANS_ABORT;
593 /* FALLTHROUGH */
594 error_return:
595 XFS_QM_DQRELE(mp, udqp);
596 XFS_QM_DQRELE(mp, gdqp);
597 if (tp) {
598 xfs_trans_cancel(tp, commit_flags);
599 }
600 if (lock_flags != 0) {
601 xfs_iunlock(ip, lock_flags);
602 }
603 return code;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000607 * The maximum pathlen is 1024 bytes. Since the minimum file system
608 * blocksize is 512 bytes, we can get a max of 2 extents back from
609 * bmapi.
610 */
611#define SYMLINK_MAPS 2
612
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000613STATIC int
614xfs_readlink_bmap(
615 xfs_inode_t *ip,
616 char *link)
617{
618 xfs_mount_t *mp = ip->i_mount;
619 int pathlen = ip->i_d.di_size;
620 int nmaps = SYMLINK_MAPS;
621 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
622 xfs_daddr_t d;
623 int byte_cnt;
624 int n;
625 xfs_buf_t *bp;
626 int error = 0;
627
628 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
629 mval, &nmaps, NULL, NULL);
630 if (error)
631 goto out;
632
633 for (n = 0; n < nmaps; n++) {
634 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
635 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
636
637 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
638 error = XFS_BUF_GETERROR(bp);
639 if (error) {
640 xfs_ioerror_alert("xfs_readlink",
641 ip->i_mount, bp, XFS_BUF_ADDR(bp));
642 xfs_buf_relse(bp);
643 goto out;
644 }
645 if (pathlen < byte_cnt)
646 byte_cnt = pathlen;
647 pathlen -= byte_cnt;
648
649 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
650 xfs_buf_relse(bp);
651 }
652
653 link[ip->i_d.di_size] = '\0';
654 error = 0;
655
656 out:
657 return error;
658}
659
Christoph Hellwig993386c2007-08-28 16:12:30 +1000660int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000662 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000663 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000665 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100669 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (XFS_FORCED_SHUTDOWN(mp))
672 return XFS_ERROR(EIO);
673
674 xfs_ilock(ip, XFS_ILOCK_SHARED);
675
676 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000677 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000679 pathlen = ip->i_d.di_size;
680 if (!pathlen)
681 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000684 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
685 link[pathlen] = '\0';
686 } else {
687 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000690 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return error;
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
696 * xfs_fsync
697 *
David Chinner978b7232008-05-19 16:29:46 +1000698 * This is called to sync the inode and its data out to disk. We need to hold
699 * the I/O lock while flushing the data, and the inode lock while flushing the
700 * inode. The inode lock CANNOT be held while flushing the data, so acquire
701 * after we're done with that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000703int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704xfs_fsync(
David Chinner978b7232008-05-19 16:29:46 +1000705 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 xfs_trans_t *tp;
708 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100709 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100711 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
714 return XFS_ERROR(EIO);
715
David Chinner978b7232008-05-19 16:29:46 +1000716 /* capture size updates in I/O completion before writing the inode. */
Christoph Hellwig863890c2008-08-13 16:12:05 +1000717 error = filemap_fdatawait(VFS_I(ip)->i_mapping);
David Chinner978b7232008-05-19 16:29:46 +1000718 if (error)
719 return XFS_ERROR(error);
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +1000720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /*
David Chinner978b7232008-05-19 16:29:46 +1000722 * We always need to make sure that the required inode state is safe on
723 * disk. The vnode might be clean but we still might need to force the
724 * log because of committed transactions that haven't hit the disk yet.
725 * Likewise, there could be unflushed non-transactional changes to the
726 * inode core that have to go to disk and this requires us to issue
727 * a synchronous transaction to capture these changes correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 *
David Chinner978b7232008-05-19 16:29:46 +1000729 * This code relies on the assumption that if the update_* fields
730 * of the inode are clear and the inode is unpinned then it is clean
731 * and no action is required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
733 xfs_ilock(ip, XFS_ILOCK_SHARED);
734
David Chinner978b7232008-05-19 16:29:46 +1000735 if (!(ip->i_update_size || ip->i_update_core)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /*
David Chinner978b7232008-05-19 16:29:46 +1000737 * Timestamps/size haven't changed since last inode flush or
738 * inode transaction commit. That means either nothing got
739 * written or a transaction committed which caught the updates.
740 * If the latter happened and the transaction hasn't hit the
741 * disk yet, the inode will be still be pinned. If it is,
742 * force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
744
745 xfs_iunlock(ip, XFS_ILOCK_SHARED);
746
747 if (xfs_ipincount(ip)) {
David Chinner978b7232008-05-19 16:29:46 +1000748 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
749 XFS_LOG_FORCE | XFS_LOG_SYNC,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100750 &log_flushed);
751 } else {
752 /*
David Chinner978b7232008-05-19 16:29:46 +1000753 * If the inode is not pinned and nothing has changed
754 * we don't need to flush the cache.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100755 */
756 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 } else {
759 /*
David Chinner978b7232008-05-19 16:29:46 +1000760 * Kick off a transaction to log the inode core to get the
761 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
763 xfs_iunlock(ip, XFS_ILOCK_SHARED);
764 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000765 error = xfs_trans_reserve(tp, 0,
766 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
767 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 xfs_trans_cancel(tp, 0);
769 return error;
770 }
771 xfs_ilock(ip, XFS_ILOCK_EXCL);
772
773 /*
David Chinner978b7232008-05-19 16:29:46 +1000774 * Note - it's possible that we might have pushed ourselves out
775 * of the way during trans_reserve which would flush the inode.
776 * But there's no guarantee that the inode buffer has actually
777 * gone out yet (it's delwri). Plus the buffer could be pinned
778 * anyway if it's part of an inode in another recent
779 * transaction. So we play it safe and fire off the
780 * transaction anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
782 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
783 xfs_trans_ihold(tp, ip);
784 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
David Chinner978b7232008-05-19 16:29:46 +1000785 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000786 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 xfs_iunlock(ip, XFS_ILOCK_EXCL);
789 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100790
791 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
792 /*
793 * If the log write didn't issue an ordered tag we need
794 * to flush the disk cache for the data device now.
795 */
796 if (!log_flushed)
797 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
798
799 /*
800 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000801 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100802 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100803 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100804 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
805 }
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return error;
808}
809
810/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000811 * This is called by xfs_inactive to free any blocks beyond eof
812 * when the link count isn't zero and by xfs_dm_punch_hole() when
813 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000815int
816xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000818 xfs_inode_t *ip,
819 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 xfs_trans_t *tp;
822 int error;
823 xfs_fileoff_t end_fsb;
824 xfs_fileoff_t last_fsb;
825 xfs_filblks_t map_len;
826 int nimaps;
827 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +1000828 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /*
831 * Figure out if there are any blocks beyond the end
832 * of the file. If not, then there is nothing to do.
833 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000834 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
836 map_len = last_fsb - end_fsb;
837 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 nimaps = 1;
841 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000842 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000843 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 xfs_iunlock(ip, XFS_ILOCK_SHARED);
845
846 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100847 (imap.br_startblock != HOLESTARTBLOCK ||
848 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
850 * Attach the dquots to the inode up front.
851 */
852 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +0100853 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /*
856 * There are blocks after the end of file.
857 * Free them up now by truncating the file to
858 * its current size.
859 */
860 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
861
862 /*
863 * Do the xfs_itruncate_start() call before
864 * reserving any log space because
865 * itruncate_start will call into the buffer
866 * cache and we can't
867 * do that within a transaction.
868 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000869 if (use_iolock)
870 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000871 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000872 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000873 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000874 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +1000875 if (use_iolock)
876 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000877 return error;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 error = xfs_trans_reserve(tp, 0,
881 XFS_ITRUNCATE_LOG_RES(mp),
882 0, XFS_TRANS_PERM_LOG_RES,
883 XFS_ITRUNCATE_LOG_COUNT);
884 if (error) {
885 ASSERT(XFS_FORCED_SHUTDOWN(mp));
886 xfs_trans_cancel(tp, 0);
887 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100888 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 xfs_ilock(ip, XFS_ILOCK_EXCL);
892 xfs_trans_ijoin(tp, ip,
893 XFS_IOLOCK_EXCL |
894 XFS_ILOCK_EXCL);
895 xfs_trans_ihold(tp, ip);
896
897 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000898 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 XFS_DATA_FORK,
900 0);
901 /*
902 * If we get an error at this point we
903 * simply don't bother truncating the file.
904 */
905 if (error) {
906 xfs_trans_cancel(tp,
907 (XFS_TRANS_RELEASE_LOG_RES |
908 XFS_TRANS_ABORT));
909 } else {
910 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000911 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
David Chinner92dfe8d2007-05-24 15:22:19 +1000913 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
914 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100916 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919/*
920 * Free a symlink that has blocks associated with it.
921 */
922STATIC int
923xfs_inactive_symlink_rmt(
924 xfs_inode_t *ip,
925 xfs_trans_t **tpp)
926{
927 xfs_buf_t *bp;
928 int committed;
929 int done;
930 int error;
931 xfs_fsblock_t first_block;
932 xfs_bmap_free_t free_list;
933 int i;
934 xfs_mount_t *mp;
935 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
936 int nmaps;
937 xfs_trans_t *ntp;
938 int size;
939 xfs_trans_t *tp;
940
941 tp = *tpp;
942 mp = ip->i_mount;
943 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
944 /*
945 * We're freeing a symlink that has some
946 * blocks allocated to it. Free the
947 * blocks here. We know that we've got
948 * either 1 or 2 extents and that we can
949 * free them all in one bunmapi call.
950 */
951 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
952 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
953 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
954 ASSERT(XFS_FORCED_SHUTDOWN(mp));
955 xfs_trans_cancel(tp, 0);
956 *tpp = NULL;
957 return error;
958 }
959 /*
960 * Lock the inode, fix the size, and join it to the transaction.
961 * Hold it so in the normal path, we still have it locked for
962 * the second transaction. In the error paths we need it
963 * held so the cancel won't rele it, see below.
964 */
965 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
966 size = (int)ip->i_d.di_size;
967 ip->i_d.di_size = 0;
968 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
969 xfs_trans_ihold(tp, ip);
970 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
971 /*
972 * Find the block(s) so we can inval and unmap them.
973 */
974 done = 0;
975 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800976 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
978 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000979 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 goto error0;
981 /*
982 * Invalidate the block(s).
983 */
984 for (i = 0; i < nmaps; i++) {
985 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
986 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
987 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
988 xfs_trans_binval(tp, bp);
989 }
990 /*
991 * Unmap the dead block(s) to the free_list.
992 */
993 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000994 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto error1;
996 ASSERT(done);
997 /*
998 * Commit the first transaction. This logs the EFI and the inode.
999 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001000 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto error1;
1002 /*
1003 * The transaction must have been committed, since there were
1004 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1005 * The new tp has the extent freeing and EFDs.
1006 */
1007 ASSERT(committed);
1008 /*
1009 * The first xact was committed, so add the inode to the new one.
1010 * Mark it dirty so it will be logged and moved forward in the log as
1011 * part of every commit.
1012 */
1013 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1014 xfs_trans_ihold(tp, ip);
1015 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1016 /*
1017 * Get a new, empty transaction to return to our caller.
1018 */
1019 ntp = xfs_trans_dup(tp);
1020 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001021 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * If we get an error on the commit here or on the reserve below,
1023 * we need to unlock the inode since the new transaction doesn't
1024 * have the inode attached.
1025 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001026 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 tp = ntp;
1028 if (error) {
1029 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1030 goto error0;
1031 }
1032 /*
1033 * Remove the memory for extent descriptions (just bookkeeping).
1034 */
1035 if (ip->i_df.if_bytes)
1036 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1037 ASSERT(ip->i_df.if_bytes == 0);
1038 /*
1039 * Put an itruncate log reservation in the new transaction
1040 * for our caller.
1041 */
1042 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1043 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1044 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1045 goto error0;
1046 }
1047 /*
1048 * Return with the inode locked but not joined to the transaction.
1049 */
1050 *tpp = tp;
1051 return 0;
1052
1053 error1:
1054 xfs_bmap_cancel(&free_list);
1055 error0:
1056 /*
1057 * Have to come here with the inode locked and either
1058 * (held and in the transaction) or (not in the transaction).
1059 * If the inode isn't held then cancel would iput it, but
1060 * that's wrong since this is inactive and the vnode ref
1061 * count is 0 already.
1062 * Cancel won't do anything to the inode if held, but it still
1063 * needs to be locked until the cancel is done, if it was
1064 * joined to the transaction.
1065 */
1066 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1067 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1068 *tpp = NULL;
1069 return error;
1070
1071}
1072
1073STATIC int
1074xfs_inactive_symlink_local(
1075 xfs_inode_t *ip,
1076 xfs_trans_t **tpp)
1077{
1078 int error;
1079
1080 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1081 /*
1082 * We're freeing a symlink which fit into
1083 * the inode. Just free the memory used
1084 * to hold the old symlink.
1085 */
1086 error = xfs_trans_reserve(*tpp, 0,
1087 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1088 0, XFS_TRANS_PERM_LOG_RES,
1089 XFS_ITRUNCATE_LOG_COUNT);
1090
1091 if (error) {
1092 xfs_trans_cancel(*tpp, 0);
1093 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001094 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1097
1098 /*
1099 * Zero length symlinks _can_ exist.
1100 */
1101 if (ip->i_df.if_bytes > 0) {
1102 xfs_idata_realloc(ip,
1103 -(ip->i_df.if_bytes),
1104 XFS_DATA_FORK);
1105 ASSERT(ip->i_df.if_bytes == 0);
1106 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110STATIC int
1111xfs_inactive_attrs(
1112 xfs_inode_t *ip,
1113 xfs_trans_t **tpp)
1114{
1115 xfs_trans_t *tp;
1116 int error;
1117 xfs_mount_t *mp;
1118
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001119 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 tp = *tpp;
1121 mp = ip->i_mount;
1122 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001123 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001125 if (error)
1126 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001129 if (error)
1130 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1133 error = xfs_trans_reserve(tp, 0,
1134 XFS_IFREE_LOG_RES(mp),
1135 0, XFS_TRANS_PERM_LOG_RES,
1136 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001137 if (error)
1138 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 xfs_ilock(ip, XFS_ILOCK_EXCL);
1141 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1142 xfs_trans_ihold(tp, ip);
1143 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1144
1145 ASSERT(ip->i_d.di_anextents == 0);
1146
1147 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001148 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001149
1150error_cancel:
1151 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1152 xfs_trans_cancel(tp, 0);
1153error_unlock:
1154 *tpp = NULL;
1155 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1156 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Christoph Hellwig993386c2007-08-28 16:12:30 +10001159int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001161 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001163 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 int error;
1165
Christoph Hellwig42173f62008-04-22 17:33:25 +10001166 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001170 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 0;
1172
David Chinner2a82b8b2007-07-11 11:09:12 +10001173 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001174 int truncated;
1175
David Chinner2a82b8b2007-07-11 11:09:12 +10001176 /*
1177 * If we are using filestreams, and we have an unlinked
1178 * file that we are processing the last close on, then nothing
1179 * will be able to reopen and write to this file. Purge this
1180 * inode from the filestreams cache so that it doesn't delay
1181 * teardown of the inode.
1182 */
1183 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1184 xfs_filestream_deassociate(ip);
1185
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001186 /*
1187 * If we previously truncated this file and removed old data
1188 * in the process, we want to initiate "early" writeout on
1189 * the last close. This is an attempt to combat the notorious
1190 * NULL files problem which is particularly noticable from a
1191 * truncate down, buffered (re-)write (delalloc), followed by
1192 * a crash. What we are effectively doing here is
1193 * significantly reducing the time window where we'd otherwise
1194 * be exposed to that problem.
1195 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001196 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001197 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001198 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001199 }
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (ip->i_d.di_nlink != 0) {
1202 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001203 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001204 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001206 (!(ip->i_d.di_flags &
1207 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001208 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1209 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001210 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 }
1213
1214 return 0;
1215}
1216
1217/*
1218 * xfs_inactive
1219 *
1220 * This is called when the vnode reference count for the vnode
1221 * goes to zero. If the file has been unlinked, then it must
1222 * now be truncated. Also, we clear all of the read-ahead state
1223 * kept for the inode here since the file is now closed.
1224 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001225int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001227 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001229 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 xfs_fsblock_t first_block;
1231 int committed;
1232 xfs_trans_t *tp;
1233 xfs_mount_t *mp;
1234 int error;
1235 int truncate;
1236
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001237 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /*
1240 * If the inode is already free, then there can be nothing
1241 * to clean up here.
1242 */
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001243 if (ip->i_d.di_mode == 0 || VN_BAD(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 ASSERT(ip->i_df.if_real_bytes == 0);
1245 ASSERT(ip->i_df.if_broot_bytes == 0);
1246 return VN_INACTIVE_CACHE;
1247 }
1248
1249 /*
1250 * Only do a truncate if it's a regular file with
1251 * some actual space in it. It's OK to look at the
1252 * inode's fields without the lock because we're the
1253 * only one with a reference to the inode.
1254 */
1255 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001256 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1257 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1259
1260 mp = ip->i_mount;
1261
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001262 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1263 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 error = 0;
1266
1267 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001268 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto out;
1270
1271 if (ip->i_d.di_nlink != 0) {
1272 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Christoph Hellwigdf80c932008-08-13 16:22:09 +10001273 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001274 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001275 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1276 (!(ip->i_d.di_flags &
1277 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1278 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001279 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1280 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001281 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 goto out;
1284 }
1285
1286 ASSERT(ip->i_d.di_nlink == 0);
1287
1288 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001289 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1292 if (truncate) {
1293 /*
1294 * Do the xfs_itruncate_start() call before
1295 * reserving any log space because itruncate_start
1296 * will call into the buffer cache and we can't
1297 * do that within a transaction.
1298 */
1299 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1300
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001301 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1302 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001303 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001304 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1305 return VN_INACTIVE_CACHE;
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 error = xfs_trans_reserve(tp, 0,
1309 XFS_ITRUNCATE_LOG_RES(mp),
1310 0, XFS_TRANS_PERM_LOG_RES,
1311 XFS_ITRUNCATE_LOG_COUNT);
1312 if (error) {
1313 /* Don't call itruncate_cleanup */
1314 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1315 xfs_trans_cancel(tp, 0);
1316 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001317 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
1320 xfs_ilock(ip, XFS_ILOCK_EXCL);
1321 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1322 xfs_trans_ihold(tp, ip);
1323
1324 /*
1325 * normally, we have to run xfs_itruncate_finish sync.
1326 * But if filesystem is wsync and we're in the inactive
1327 * path, then we know that nlink == 0, and that the
1328 * xaction that made nlink == 0 is permanently committed
1329 * since xfs_remove runs as a synchronous transaction.
1330 */
1331 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1332 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1333
1334 if (error) {
1335 xfs_trans_cancel(tp,
1336 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1337 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001338 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1341
1342 /*
1343 * If we get an error while cleaning up a
1344 * symlink we bail out.
1345 */
1346 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1347 xfs_inactive_symlink_rmt(ip, &tp) :
1348 xfs_inactive_symlink_local(ip, &tp);
1349
1350 if (error) {
1351 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001352 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354
1355 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1356 xfs_trans_ihold(tp, ip);
1357 } else {
1358 error = xfs_trans_reserve(tp, 0,
1359 XFS_IFREE_LOG_RES(mp),
1360 0, XFS_TRANS_PERM_LOG_RES,
1361 XFS_INACTIVE_LOG_COUNT);
1362 if (error) {
1363 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1364 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001365 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
1368 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1369 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1370 xfs_trans_ihold(tp, ip);
1371 }
1372
1373 /*
1374 * If there are attributes associated with the file
1375 * then blow them away now. The code calls a routine
1376 * that recursively deconstructs the attribute fork.
1377 * We need to just commit the current transaction
1378 * because we can't use it for xfs_attr_inactive().
1379 */
1380 if (ip->i_d.di_anextents > 0) {
1381 error = xfs_inactive_attrs(ip, &tp);
1382 /*
1383 * If we got an error, the transaction is already
1384 * cancelled, and the inode is unlocked. Just get out.
1385 */
1386 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001387 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 } else if (ip->i_afp) {
1389 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1390 }
1391
1392 /*
1393 * Free the inode.
1394 */
1395 XFS_BMAP_INIT(&free_list, &first_block);
1396 error = xfs_ifree(tp, ip, &free_list);
1397 if (error) {
1398 /*
1399 * If we fail to free the inode, shut down. The cancel
1400 * might do that, we need to make sure. Otherwise the
1401 * inode might be lost for a long time or forever.
1402 */
1403 if (!XFS_FORCED_SHUTDOWN(mp)) {
1404 cmn_err(CE_NOTE,
1405 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1406 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001407 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1410 } else {
1411 /*
1412 * Credit the quota account(s). The inode is gone.
1413 */
1414 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1415
1416 /*
David Chinner78e9da72008-04-10 12:24:17 +10001417 * Just ignore errors at this point. There is nothing we can
1418 * do except to try to keep going. Make sure it's not a silent
1419 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
David Chinner78e9da72008-04-10 12:24:17 +10001421 error = xfs_bmap_finish(&tp, &free_list, &committed);
1422 if (error)
1423 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1424 "xfs_bmap_finish() returned error %d", error);
1425 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1426 if (error)
1427 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1428 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430 /*
1431 * Release the dquots held by inode, if any.
1432 */
1433 XFS_QM_DQDETACH(mp, ip);
1434
1435 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1436
1437 out:
1438 return VN_INACTIVE_CACHE;
1439}
1440
Barry Naujok384f3ce2008-05-21 16:58:22 +10001441/*
1442 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1443 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1444 * ci_name->name will point to a the actual name (caller must free) or
1445 * will be set to NULL if an exact match is found.
1446 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001447int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001449 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001450 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001451 xfs_inode_t **ipp,
1452 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001454 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 int error;
1456 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001458 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1461 return XFS_ERROR(EIO);
1462
1463 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001464 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001466
1467 if (error)
1468 goto out;
1469
1470 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1471 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001472 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001473
1474 xfs_itrace_ref(*ipp);
1475 return 0;
1476
Barry Naujok384f3ce2008-05-21 16:58:22 +10001477out_free_name:
1478 if (ci_name)
1479 kmem_free(ci_name->name);
1480out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001481 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return error;
1483}
1484
Christoph Hellwig993386c2007-08-28 16:12:30 +10001485int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001487 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001488 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001489 mode_t mode,
1490 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001491 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 cred_t *credp)
1493{
Barry Naujok556b8b12008-04-10 12:22:07 +10001494 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001495 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 xfs_trans_t *tp;
Barry Naujok556b8b12008-04-10 12:22:07 +10001497 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 xfs_bmap_free_t free_list;
1499 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001500 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int dm_event_sent = 0;
1502 uint cancel_flags;
1503 int committed;
1504 xfs_prid_t prid;
1505 struct xfs_dquot *udqp, *gdqp;
1506 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001508 ASSERT(!*ipp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001509 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001511 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001513 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001514 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001515 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 if (error)
1518 return error;
1519 dm_event_sent = 1;
1520 }
1521
1522 if (XFS_FORCED_SHUTDOWN(mp))
1523 return XFS_ERROR(EIO);
1524
1525 /* Return through std_return after this point. */
1526
1527 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001528 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1529 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 else
1531 prid = (xfs_prid_t)dfltprid;
1532
1533 /*
1534 * Make sure that we have allocated dquot(s) on disk.
1535 */
1536 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001537 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1539 if (error)
1540 goto std_return;
1541
1542 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1545 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001546 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /*
1548 * Initially assume that the file does not exist and
1549 * reserve the resources for that case. If that is not
1550 * the case we'll drop the one we have and get a more
1551 * appropriate transaction later.
1552 */
1553 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1554 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1555 if (error == ENOSPC) {
1556 resblks = 0;
1557 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1558 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1559 }
1560 if (error) {
1561 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 goto error_return;
1563 }
1564
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001565 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001566 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 XFS_BMAP_INIT(&free_list, &first_block);
1569
1570 ASSERT(ip == NULL);
1571
1572 /*
1573 * Reserve disk quota and the inode.
1574 */
1575 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1576 if (error)
1577 goto error_return;
1578
Barry Naujok556b8b12008-04-10 12:22:07 +10001579 error = xfs_dir_canenter(tp, dp, name, resblks);
1580 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001582 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 rdev, credp, prid, resblks > 0,
1584 &ip, &committed);
1585 if (error) {
1586 if (error == ENOSPC)
1587 goto error_return;
1588 goto abort_return;
1589 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001590 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /*
1593 * At this point, we've gotten a newly allocated inode.
1594 * It is locked (and joined to the transaction).
1595 */
1596
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001597 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001600 * Now we join the directory inode to the transaction. We do not do it
1601 * earlier because xfs_dir_ialloc might commit the previous transaction
1602 * (and release all the locks). An error from here on will result in
1603 * the transaction cancel unlocking dp so don't do it explicitly in the
1604 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001606 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001608 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Barry Naujok556b8b12008-04-10 12:22:07 +10001610 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001611 &first_block, &free_list, resblks ?
1612 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (error) {
1614 ASSERT(error != ENOSPC);
1615 goto abort_return;
1616 }
1617 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1618 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1619
1620 /*
1621 * If this is a synchronous mount, make sure that the
1622 * create transaction goes to disk before returning to
1623 * the user.
1624 */
1625 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1626 xfs_trans_set_sync(tp);
1627 }
1628
1629 dp->i_gen++;
1630
1631 /*
1632 * Attach the dquot(s) to the inodes and modify them incore.
1633 * These ids of the inode couldn't have changed since the new
1634 * inode has been locked ever since it was created.
1635 */
1636 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1637
1638 /*
1639 * xfs_trans_commit normally decrements the vnode ref count
1640 * when it unlocks the inode. Since we want to return the
1641 * vnode to the caller, we bump the vnode ref count now.
1642 */
1643 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001645 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (error) {
1647 xfs_bmap_cancel(&free_list);
1648 goto abort_rele;
1649 }
1650
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001651 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (error) {
1653 IRELE(ip);
1654 tp = NULL;
1655 goto error_return;
1656 }
1657
1658 XFS_QM_DQRELE(mp, udqp);
1659 XFS_QM_DQRELE(mp, gdqp);
1660
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001661 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 /* Fallthrough to std_return with error = 0 */
1664
1665std_return:
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001666 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10001667 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001669 dp, DM_RIGHT_NULL,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001670 *ipp ? ip : NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001671 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001672 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674 return error;
1675
1676 abort_return:
1677 cancel_flags |= XFS_TRANS_ABORT;
1678 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Jesper Juhl014c2542006-01-15 02:37:08 +01001680 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (tp != NULL)
1682 xfs_trans_cancel(tp, cancel_flags);
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 XFS_QM_DQRELE(mp, udqp);
1685 XFS_QM_DQRELE(mp, gdqp);
1686
Christoph Hellwig993386c2007-08-28 16:12:30 +10001687 if (unlock_dp_on_error)
1688 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto std_return;
1691
1692 abort_rele:
1693 /*
1694 * Wait until after the current transaction is aborted to
1695 * release the inode. This prevents recursive transactions
1696 * and deadlocks from xfs_inactive.
1697 */
1698 cancel_flags |= XFS_TRANS_ABORT;
1699 xfs_trans_cancel(tp, cancel_flags);
1700 IRELE(ip);
1701
1702 XFS_QM_DQRELE(mp, udqp);
1703 XFS_QM_DQRELE(mp, gdqp);
1704
1705 goto std_return;
1706}
1707
1708#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709int xfs_locked_n;
1710int xfs_small_retries;
1711int xfs_middle_retries;
1712int xfs_lots_retries;
1713int xfs_lock_delays;
1714#endif
1715
1716/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001717 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1718 * a different value
1719 */
1720static inline int
1721xfs_lock_inumorder(int lock_mode, int subclass)
1722{
1723 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001724 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001725 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001726 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001727
1728 return lock_mode;
1729}
1730
1731/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 * The following routine will lock n inodes in exclusive mode.
1733 * We assume the caller calls us with the inodes in i_ino order.
1734 *
1735 * We need to detect deadlock where an inode that we lock
1736 * is in the AIL and we start waiting for another inode that is locked
1737 * by a thread in a long running transaction (such as truncate). This can
1738 * result in deadlock since the long running trans might need to wait
1739 * for the inode we just locked in order to push the tail and free space
1740 * in the log.
1741 */
1742void
1743xfs_lock_inodes(
1744 xfs_inode_t **ips,
1745 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 uint lock_mode)
1747{
1748 int attempts = 0, i, j, try_lock;
1749 xfs_log_item_t *lp;
1750
1751 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1752
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001753 try_lock = 0;
1754 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756again:
1757 for (; i < inodes; i++) {
1758 ASSERT(ips[i]);
1759
1760 if (i && (ips[i] == ips[i-1])) /* Already locked */
1761 continue;
1762
1763 /*
1764 * If try_lock is not set yet, make sure all locked inodes
1765 * are not in the AIL.
1766 * If any are, set try_lock to be used later.
1767 */
1768
1769 if (!try_lock) {
1770 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1771 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1772 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1773 try_lock++;
1774 }
1775 }
1776 }
1777
1778 /*
1779 * If any of the previous locks we have locked is in the AIL,
1780 * we must TRY to get the second and subsequent locks. If
1781 * we can't get any, we must release all we have
1782 * and try again.
1783 */
1784
1785 if (try_lock) {
1786 /* try_lock must be 0 if i is 0. */
1787 /*
1788 * try_lock means we have an inode locked
1789 * that is in the AIL.
1790 */
1791 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001792 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 attempts++;
1794
1795 /*
1796 * Unlock all previous guys and try again.
1797 * xfs_iunlock will try to push the tail
1798 * if the inode is in the AIL.
1799 */
1800
1801 for(j = i - 1; j >= 0; j--) {
1802
1803 /*
1804 * Check to see if we've already
1805 * unlocked this one.
1806 * Not the first one going back,
1807 * and the inode ptr is the same.
1808 */
1809 if ((j != (i - 1)) && ips[j] ==
1810 ips[j+1])
1811 continue;
1812
1813 xfs_iunlock(ips[j], lock_mode);
1814 }
1815
1816 if ((attempts % 5) == 0) {
1817 delay(1); /* Don't just spin the CPU */
1818#ifdef DEBUG
1819 xfs_lock_delays++;
1820#endif
1821 }
1822 i = 0;
1823 try_lock = 0;
1824 goto again;
1825 }
1826 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001827 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829 }
1830
1831#ifdef DEBUG
1832 if (attempts) {
1833 if (attempts < 5) xfs_small_retries++;
1834 else if (attempts < 100) xfs_middle_retries++;
1835 else xfs_lots_retries++;
1836 } else {
1837 xfs_locked_n++;
1838 }
1839#endif
1840}
1841
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001842void
1843xfs_lock_two_inodes(
1844 xfs_inode_t *ip0,
1845 xfs_inode_t *ip1,
1846 uint lock_mode)
1847{
1848 xfs_inode_t *temp;
1849 int attempts = 0;
1850 xfs_log_item_t *lp;
1851
1852 ASSERT(ip0->i_ino != ip1->i_ino);
1853
1854 if (ip0->i_ino > ip1->i_ino) {
1855 temp = ip0;
1856 ip0 = ip1;
1857 ip1 = temp;
1858 }
1859
1860 again:
1861 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1862
1863 /*
1864 * If the first lock we have locked is in the AIL, we must TRY to get
1865 * the second lock. If we can't get it, we must release the first one
1866 * and try again.
1867 */
1868 lp = (xfs_log_item_t *)ip0->i_itemp;
1869 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1870 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1871 xfs_iunlock(ip0, lock_mode);
1872 if ((++attempts % 5) == 0)
1873 delay(1); /* Don't just spin the CPU */
1874 goto again;
1875 }
1876 } else {
1877 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1878 }
1879}
1880
Christoph Hellwig993386c2007-08-28 16:12:30 +10001881int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001883 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001884 struct xfs_name *name,
1885 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001887 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001889 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int error = 0;
1891 xfs_bmap_free_t free_list;
1892 xfs_fsblock_t first_block;
1893 int cancel_flags;
1894 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 int link_zero;
1896 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001897 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001899 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001900 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (XFS_FORCED_SHUTDOWN(mp))
1903 return XFS_ERROR(EIO);
1904
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001905 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001906 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1907 NULL, DM_RIGHT_NULL, name->name, NULL,
1908 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (error)
1910 return error;
1911 }
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 error = XFS_QM_DQATTACH(mp, dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001914 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001917 error = XFS_QM_DQATTACH(mp, ip, 0);
1918 if (error)
1919 goto std_return;
1920
1921 if (is_dir) {
1922 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1923 log_count = XFS_DEFAULT_LOG_COUNT;
1924 } else {
1925 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1926 log_count = XFS_REMOVE_LOG_COUNT;
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /*
1931 * We try to get the real space reservation first,
1932 * allowing for directory btree deletion(s) implying
1933 * possible bmap insert(s). If we can't get the space
1934 * reservation then we use 0 instead, and avoid the bmap
1935 * btree insert(s) in the directory code by, if the bmap
1936 * insert tries to happen, instead trimming the LAST
1937 * block from the directory.
1938 */
1939 resblks = XFS_REMOVE_SPACE_RES(mp);
1940 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001941 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (error == ENOSPC) {
1943 resblks = 0;
1944 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001945 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947 if (error) {
1948 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001949 cancel_flags = 0;
1950 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952
Christoph Hellwige1cccd92008-08-13 16:18:07 +10001953 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 /*
1956 * At this point, we've gotten both the directory and the entry
1957 * inodes locked.
1958 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10001959 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10001961
1962 IHOLD(dp);
1963 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001966 * If we're removing a directory perform some additional validation.
1967 */
1968 if (is_dir) {
1969 ASSERT(ip->i_d.di_nlink >= 2);
1970 if (ip->i_d.di_nlink != 2) {
1971 error = XFS_ERROR(ENOTEMPTY);
1972 goto out_trans_cancel;
1973 }
1974 if (!xfs_dir_isempty(ip)) {
1975 error = XFS_ERROR(ENOTEMPTY);
1976 goto out_trans_cancel;
1977 }
1978 }
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 XFS_BMAP_INIT(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10001981 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10001982 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (error) {
1984 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001985 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1988
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001989 /*
1990 * Bump the in memory generation count on the parent
1991 * directory so that other can know that it has changed.
1992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 dp->i_gen++;
1994 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1995
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001996 if (is_dir) {
1997 /*
1998 * Drop the link from ip's "..".
1999 */
2000 error = xfs_droplink(tp, dp);
2001 if (error)
2002 goto out_bmap_cancel;
2003
2004 /*
2005 * Drop the link from dp to ip.
2006 */
2007 error = xfs_droplink(tp, ip);
2008 if (error)
2009 goto out_bmap_cancel;
2010 } else {
2011 /*
2012 * When removing a non-directory we need to log the parent
2013 * inode here for the i_gen update. For a directory this is
2014 * done implicitly by the xfs_droplink call for the ".." entry.
2015 */
2016 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002019 /*
2020 * Drop the "." link from ip to self.
2021 */
2022 error = xfs_droplink(tp, ip);
2023 if (error)
2024 goto out_bmap_cancel;
2025
2026 /*
2027 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 * we are in the transaction.
2029 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002030 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 * If this is a synchronous mount, make sure that the
2034 * remove transaction goes to disk before returning to
2035 * the user.
2036 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002037 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002040 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002041 if (error)
2042 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002044 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002045 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002049 * If we are using filestreams, kill the stream association.
2050 * If the file is still open it may get a new one but that
2051 * will get killed on last close in xfs_close() so we don't
2052 * have to worry about that.
2053 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002054 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10002055 xfs_filestream_deassociate(ip);
2056
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002057 xfs_itrace_exit(ip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002058 xfs_itrace_exit(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002061 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002062 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2063 NULL, DM_RIGHT_NULL, name->name, NULL,
2064 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return error;
2068
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002069 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 xfs_bmap_cancel(&free_list);
2071 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002072 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 xfs_trans_cancel(tp, cancel_flags);
2074 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
Christoph Hellwig993386c2007-08-28 16:12:30 +10002077int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002079 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002080 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002081 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002083 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 int error;
2086 xfs_bmap_free_t free_list;
2087 xfs_fsblock_t first_block;
2088 int cancel_flags;
2089 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002092 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002093 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002095 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (XFS_FORCED_SHUTDOWN(mp))
2098 return XFS_ERROR(EIO);
2099
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002100 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002102 tdp, DM_RIGHT_NULL,
2103 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002104 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (error)
2106 return error;
2107 }
2108
2109 /* Return through std_return after this point. */
2110
2111 error = XFS_QM_DQATTACH(mp, sip, 0);
2112 if (!error && sip != tdp)
2113 error = XFS_QM_DQATTACH(mp, tdp, 0);
2114 if (error)
2115 goto std_return;
2116
2117 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2118 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002119 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2121 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2122 if (error == ENOSPC) {
2123 resblks = 0;
2124 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2125 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2126 }
2127 if (error) {
2128 cancel_flags = 0;
2129 goto error_return;
2130 }
2131
Christoph Hellwige1cccd92008-08-13 16:18:07 +10002132 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /*
2135 * Increment vnode ref counts since xfs_trans_commit &
2136 * xfs_trans_cancel will both unlock the inodes and
2137 * decrement the associated ref counts.
2138 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002139 IHOLD(sip);
2140 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2142 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2143
2144 /*
2145 * If the source has too many links, we can't make any more to it.
2146 */
2147 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2148 error = XFS_ERROR(EMLINK);
2149 goto error_return;
2150 }
2151
Nathan Scott365ca832005-06-21 15:39:12 +10002152 /*
2153 * If we are using project inheritance, we only allow hard link
2154 * creation in our tree when the project IDs are the same; else
2155 * the tree quota mechanism could be circumvented.
2156 */
2157 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2158 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002159 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002160 goto error_return;
2161 }
2162
Barry Naujok556b8b12008-04-10 12:22:07 +10002163 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2164 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 goto error_return;
2166
2167 XFS_BMAP_INIT(&free_list, &first_block);
2168
Barry Naujok556b8b12008-04-10 12:22:07 +10002169 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2170 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (error)
2172 goto abort_return;
2173 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2174 tdp->i_gen++;
2175 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2176
2177 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002178 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 /*
2182 * If this is a synchronous mount, make sure that the
2183 * link transaction goes to disk before returning to
2184 * the user.
2185 */
2186 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2187 xfs_trans_set_sync(tp);
2188 }
2189
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002190 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (error) {
2192 xfs_bmap_cancel(&free_list);
2193 goto abort_return;
2194 }
2195
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002196 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002197 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 /* Fall through to std_return with error = 0. */
2201std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002202 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002204 tdp, DM_RIGHT_NULL,
2205 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002206 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208 return error;
2209
2210 abort_return:
2211 cancel_flags |= XFS_TRANS_ABORT;
2212 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 error_return:
2215 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 goto std_return;
2217}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002218
2219
Christoph Hellwig993386c2007-08-28 16:12:30 +10002220int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002222 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002223 struct xfs_name *dir_name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002224 mode_t mode,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002225 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 cred_t *credp)
2227{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002228 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 xfs_inode_t *cdp; /* inode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 int cancel_flags;
2232 int error;
2233 int committed;
2234 xfs_bmap_free_t free_list;
2235 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002236 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 boolean_t created = B_FALSE;
2238 int dm_event_sent = 0;
2239 xfs_prid_t prid;
2240 struct xfs_dquot *udqp, *gdqp;
2241 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 if (XFS_FORCED_SHUTDOWN(mp))
2244 return XFS_ERROR(EIO);
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002248 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002250 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002251 DM_RIGHT_NULL, dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002252 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (error)
2254 return error;
2255 dm_event_sent = 1;
2256 }
2257
2258 /* Return through std_return after this point. */
2259
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002260 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 mp = dp->i_mount;
2263 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002264 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2265 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 else
2267 prid = (xfs_prid_t)dfltprid;
2268
2269 /*
2270 * Make sure that we have allocated dquot(s) on disk.
2271 */
2272 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002273 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2275 if (error)
2276 goto std_return;
2277
2278 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2279 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002280 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2282 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2283 if (error == ENOSPC) {
2284 resblks = 0;
2285 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2286 XFS_TRANS_PERM_LOG_RES,
2287 XFS_MKDIR_LOG_COUNT);
2288 }
2289 if (error) {
2290 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto error_return;
2292 }
2293
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002294 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002295 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 /*
2298 * Check for directory link count overflow.
2299 */
2300 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2301 error = XFS_ERROR(EMLINK);
2302 goto error_return;
2303 }
2304
2305 /*
2306 * Reserve disk quota and the inode.
2307 */
2308 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2309 if (error)
2310 goto error_return;
2311
Barry Naujok556b8b12008-04-10 12:22:07 +10002312 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2313 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 goto error_return;
2315 /*
2316 * create the directory inode.
2317 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002318 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 0, credp, prid, resblks > 0,
2320 &cdp, NULL);
2321 if (error) {
2322 if (error == ENOSPC)
2323 goto error_return;
2324 goto abort_return;
2325 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002326 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 /*
2329 * Now we add the directory inode to the transaction.
2330 * We waited until now since xfs_dir_ialloc might start
2331 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002332 * earlier, the locks might have gotten released. An error
2333 * from here on will result in the transaction cancel
2334 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002336 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002338 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 XFS_BMAP_INIT(&free_list, &first_block);
2341
Barry Naujok556b8b12008-04-10 12:22:07 +10002342 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2343 &first_block, &free_list, resblks ?
2344 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (error) {
2346 ASSERT(error != ENOSPC);
2347 goto error1;
2348 }
2349 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2350
2351 /*
2352 * Bump the in memory version number of the parent directory
2353 * so that other processes accessing it will recognize that
2354 * the directory has changed.
2355 */
2356 dp->i_gen++;
2357
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002358 error = xfs_dir_init(tp, cdp, dp);
2359 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 cdp->i_gen = 1;
2363 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002364 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 created = B_TRUE;
2368
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002369 *ipp = cdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 IHOLD(cdp);
2371
2372 /*
2373 * Attach the dquots to the new inode and modify the icount incore.
2374 */
2375 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2376
2377 /*
2378 * If this is a synchronous mount, make sure that the
2379 * mkdir transaction goes to disk before returning to
2380 * the user.
2381 */
2382 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2383 xfs_trans_set_sync(tp);
2384 }
2385
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002386 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (error) {
2388 IRELE(cdp);
2389 goto error2;
2390 }
2391
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002392 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 XFS_QM_DQRELE(mp, udqp);
2394 XFS_QM_DQRELE(mp, gdqp);
2395 if (error) {
2396 IRELE(cdp);
2397 }
2398
2399 /* Fall through to std_return with error = 0 or errno from
2400 * xfs_trans_commit. */
2401
2402std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002403 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002404 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002406 dp, DM_RIGHT_NULL,
2407 created ? cdp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002409 dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002410 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
2412 return error;
2413
2414 error2:
2415 error1:
2416 xfs_bmap_cancel(&free_list);
2417 abort_return:
2418 cancel_flags |= XFS_TRANS_ABORT;
2419 error_return:
2420 xfs_trans_cancel(tp, cancel_flags);
2421 XFS_QM_DQRELE(mp, udqp);
2422 XFS_QM_DQRELE(mp, gdqp);
2423
Christoph Hellwig993386c2007-08-28 16:12:30 +10002424 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 goto std_return;
2428}
2429
Christoph Hellwig993386c2007-08-28 16:12:30 +10002430int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002432 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002433 struct xfs_name *link_name,
2434 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002435 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002436 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 cred_t *credp)
2438{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002439 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 xfs_inode_t *ip;
2442 int error;
2443 int pathlen;
2444 xfs_bmap_free_t free_list;
2445 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002446 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 uint cancel_flags;
2448 int committed;
2449 xfs_fileoff_t first_fsb;
2450 xfs_filblks_t fs_blocks;
2451 int nmaps;
2452 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2453 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002454 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 int byte_cnt;
2456 int n;
2457 xfs_buf_t *bp;
2458 xfs_prid_t prid;
2459 struct xfs_dquot *udqp, *gdqp;
2460 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Christoph Hellwig3937be52008-03-06 13:46:19 +11002462 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 error = 0;
2464 ip = NULL;
2465 tp = NULL;
2466
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002467 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 if (XFS_FORCED_SHUTDOWN(mp))
2470 return XFS_ERROR(EIO);
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /*
2473 * Check component lengths of the target path name.
2474 */
2475 pathlen = strlen(target_path);
2476 if (pathlen >= MAXPATHLEN) /* total string too long */
2477 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002479 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002480 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002482 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (error)
2484 return error;
2485 }
2486
2487 /* Return through std_return after this point. */
2488
2489 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002490 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2491 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 else
2493 prid = (xfs_prid_t)dfltprid;
2494
2495 /*
2496 * Make sure that we have allocated dquot(s) on disk.
2497 */
2498 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002499 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2501 if (error)
2502 goto std_return;
2503
2504 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2505 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2506 /*
2507 * The symlink will fit into the inode data fork?
2508 * There can't be any attributes so we get the whole variable part.
2509 */
2510 if (pathlen <= XFS_LITINO(mp))
2511 fs_blocks = 0;
2512 else
2513 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002514 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2516 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2517 if (error == ENOSPC && fs_blocks == 0) {
2518 resblks = 0;
2519 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2520 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2521 }
2522 if (error) {
2523 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 goto error_return;
2525 }
2526
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002527 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002528 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 /*
2531 * Check whether the directory allows new symlinks or not.
2532 */
2533 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2534 error = XFS_ERROR(EPERM);
2535 goto error_return;
2536 }
2537
2538 /*
2539 * Reserve disk quota : blocks and inode.
2540 */
2541 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2542 if (error)
2543 goto error_return;
2544
2545 /*
2546 * Check for ability to enter directory entry, if no space reserved.
2547 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002548 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2549 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 goto error_return;
2551 /*
2552 * Initialize the bmap freelist prior to calling either
2553 * bmapi or the directory create code.
2554 */
2555 XFS_BMAP_INIT(&free_list, &first_block);
2556
2557 /*
2558 * Allocate an inode for the symlink.
2559 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002560 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 1, 0, credp, prid, resblks > 0, &ip, NULL);
2562 if (error) {
2563 if (error == ENOSPC)
2564 goto error_return;
2565 goto error1;
2566 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002567 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Christoph Hellwig993386c2007-08-28 16:12:30 +10002569 /*
2570 * An error after we've joined dp to the transaction will result in the
2571 * transaction cancel unlocking dp so don't do it explicitly in the
2572 * error path.
2573 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002574 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002576 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 /*
2579 * Also attach the dquot(s) to it, if applicable.
2580 */
2581 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2582
2583 if (resblks)
2584 resblks -= XFS_IALLOC_SPACE_RES(mp);
2585 /*
2586 * If the symlink will fit into the inode, write it inline.
2587 */
2588 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2589 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2590 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2591 ip->i_d.di_size = pathlen;
2592
2593 /*
2594 * The inode was initially created in extent format.
2595 */
2596 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2597 ip->i_df.if_flags |= XFS_IFINLINE;
2598
2599 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2600 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2601
2602 } else {
2603 first_fsb = 0;
2604 nmaps = SYMLINK_MAPS;
2605
2606 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2607 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2608 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002609 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (error) {
2611 goto error1;
2612 }
2613
2614 if (resblks)
2615 resblks -= fs_blocks;
2616 ip->i_d.di_size = pathlen;
2617 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2618
2619 cur_chunk = target_path;
2620 for (n = 0; n < nmaps; n++) {
2621 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2622 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2623 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2624 BTOBB(byte_cnt), 0);
2625 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2626 if (pathlen < byte_cnt) {
2627 byte_cnt = pathlen;
2628 }
2629 pathlen -= byte_cnt;
2630
2631 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2632 cur_chunk += byte_cnt;
2633
2634 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2635 }
2636 }
2637
2638 /*
2639 * Create the directory entry for the symlink.
2640 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002641 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2642 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002643 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2646 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2647
2648 /*
2649 * Bump the in memory version number of the parent directory
2650 * so that other processes accessing it will recognize that
2651 * the directory has changed.
2652 */
2653 dp->i_gen++;
2654
2655 /*
2656 * If this is a synchronous mount, make sure that the
2657 * symlink transaction goes to disk before returning to
2658 * the user.
2659 */
2660 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2661 xfs_trans_set_sync(tp);
2662 }
2663
2664 /*
2665 * xfs_trans_commit normally decrements the vnode ref count
2666 * when it unlocks the inode. Since we want to return the
2667 * vnode to the caller, we bump the vnode ref count now.
2668 */
2669 IHOLD(ip);
2670
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002671 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (error) {
2673 goto error2;
2674 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002675 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 XFS_QM_DQRELE(mp, udqp);
2677 XFS_QM_DQRELE(mp, gdqp);
2678
2679 /* Fall through to std_return with error = 0 or errno from
2680 * xfs_trans_commit */
2681std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10002682 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002684 dp, DM_RIGHT_NULL,
2685 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002686 DM_RIGHT_NULL, link_name->name,
2687 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
2689
Christoph Hellwig3937be52008-03-06 13:46:19 +11002690 if (!error)
2691 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return error;
2693
2694 error2:
2695 IRELE(ip);
2696 error1:
2697 xfs_bmap_cancel(&free_list);
2698 cancel_flags |= XFS_TRANS_ABORT;
2699 error_return:
2700 xfs_trans_cancel(tp, cancel_flags);
2701 XFS_QM_DQRELE(mp, udqp);
2702 XFS_QM_DQRELE(mp, gdqp);
2703
Christoph Hellwig993386c2007-08-28 16:12:30 +10002704 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 goto std_return;
2708}
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002712 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 int flags)
2714{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002715 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 int error = 0;
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (XFS_FORCED_SHUTDOWN(mp))
2719 return XFS_ERROR(EIO);
2720
2721 /*
2722 * Bypass inodes which have already been cleaned by
2723 * the inode flush clustering code inside xfs_iflush
2724 */
David Chinner33540402008-03-06 13:43:59 +11002725 if (xfs_inode_clean(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 return 0;
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /*
2729 * We make this non-blocking if the inode is contended,
2730 * return EAGAIN to indicate to the caller that they
2731 * did not succeed. This prevents the flush path from
2732 * blocking on inodes inside another operation right
2733 * now, they get caught later by xfs_sync.
2734 */
David Chinnera3f74ff2008-03-06 13:43:42 +11002735 if (flags & FLUSH_SYNC) {
2736 xfs_ilock(ip, XFS_ILOCK_SHARED);
2737 xfs_iflock(ip);
2738 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
2739 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
2740 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return EAGAIN;
2742 }
David Chinnera3f74ff2008-03-06 13:43:42 +11002743 } else {
2744 return EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
2746
David Chinnera3f74ff2008-03-06 13:43:42 +11002747 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
2748 : XFS_IFLUSH_ASYNC_NOBLOCK);
2749 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return error;
2752}
2753
Christoph Hellwig993386c2007-08-28 16:12:30 +10002754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002756xfs_set_dmattrs(
2757 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002759 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002761 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 int error;
2764
2765 if (!capable(CAP_SYS_ADMIN))
2766 return XFS_ERROR(EPERM);
2767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (XFS_FORCED_SHUTDOWN(mp))
2769 return XFS_ERROR(EIO);
2770
2771 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2772 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2773 if (error) {
2774 xfs_trans_cancel(tp, 0);
2775 return error;
2776 }
2777 xfs_ilock(ip, XFS_ILOCK_EXCL);
2778 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2779
Christoph Hellwig613d7042007-10-11 17:44:08 +10002780 ip->i_d.di_dmevmask = evmask;
2781 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2784 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002785 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787 return error;
2788}
2789
Christoph Hellwig993386c2007-08-28 16:12:30 +10002790int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002792 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002795 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002797 ASSERT(!VN_MAPPED(VFS_I(ip)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 /* bad inode, get out here ASAP */
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002800 if (VN_BAD(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 xfs_ireclaim(ip);
2802 return 0;
2803 }
2804
Christoph Hellwigb677c212007-08-29 11:46:28 +10002805 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10002807 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11002809 /*
2810 * Make sure the atime in the XFS inode is correct before freeing the
2811 * Linux inode.
2812 */
2813 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
David Chinner4c606582006-11-11 18:05:00 +11002815 /*
2816 * If we have nothing to flush with this inode then complete the
2817 * teardown now, otherwise break the link between the xfs inode and the
2818 * linux inode and clean up the xfs inode later. This avoids flushing
2819 * the inode to disk during the delete operation itself.
2820 *
2821 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2822 * first to ensure that xfs_iunpin() will never see an xfs inode
2823 * that has a linux inode being reclaimed. Synchronisation is provided
2824 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 */
2826 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2827 xfs_ilock(ip, XFS_ILOCK_EXCL);
2828 xfs_iflock(ip);
2829 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
2830 } else {
2831 xfs_mount_t *mp = ip->i_mount;
2832
David Chinner4c606582006-11-11 18:05:00 +11002833 /* Protect sync and unpin from us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 XFS_MOUNT_ILOCK(mp);
David Chinner4c606582006-11-11 18:05:00 +11002835 spin_lock(&ip->i_flags_lock);
2836 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
Christoph Hellwigdf80c932008-08-13 16:22:09 +10002837 VFS_I(ip)->i_private = NULL;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10002838 ip->i_vnode = NULL;
David Chinner4c606582006-11-11 18:05:00 +11002839 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 XFS_MOUNT_IUNLOCK(mp);
2842 }
2843 return 0;
2844}
2845
2846int
2847xfs_finish_reclaim(
2848 xfs_inode_t *ip,
2849 int locked,
2850 int sync_mode)
2851{
David Chinnerda353b02007-08-28 14:00:13 +10002852 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
David Chinner705db4a2008-08-13 15:47:43 +10002853 bhv_vnode_t *vp = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855 if (vp && VN_BAD(vp))
2856 goto reclaim;
2857
2858 /* The hash lock here protects a thread in xfs_iget_core from
2859 * racing with us on linking the inode back with a vnode.
2860 * Once we have the XFS_IRECLAIM flag set it will not touch
2861 * us.
2862 */
David Chinnerda353b02007-08-28 14:00:13 +10002863 write_lock(&pag->pag_ici_lock);
David Chinnerf273ab82006-09-28 11:06:03 +10002864 spin_lock(&ip->i_flags_lock);
David Chinner7a18c382006-11-11 18:04:54 +11002865 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
2866 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
David Chinnerf273ab82006-09-28 11:06:03 +10002867 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10002868 write_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (locked) {
2870 xfs_ifunlock(ip);
2871 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2872 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002873 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
David Chinner7a18c382006-11-11 18:04:54 +11002875 __xfs_iflags_set(ip, XFS_IRECLAIM);
David Chinnerf273ab82006-09-28 11:06:03 +10002876 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10002877 write_unlock(&pag->pag_ici_lock);
2878 xfs_put_perag(ip->i_mount, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 /*
2881 * If the inode is still dirty, then flush it out. If the inode
2882 * is not in the AIL, then it will be OK to flush it delwri as
2883 * long as xfs_iflush() does not keep any references to the inode.
2884 * We leave that decision up to xfs_iflush() since it has the
2885 * knowledge of whether it's OK to simply do a delwri flush of
2886 * the inode or whether we need to wait until the inode is
2887 * pulled from the AIL.
2888 * We get the flush lock regardless, though, just to make sure
2889 * we don't free it while it is being flushed.
2890 */
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11002891 if (!locked) {
2892 xfs_ilock(ip, XFS_ILOCK_EXCL);
2893 xfs_iflock(ip);
2894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Lachlan McIlroy7f871d52008-06-23 13:23:57 +10002896 /*
2897 * In the case of a forced shutdown we rely on xfs_iflush() to
2898 * wait for the inode to be unpinned before returning an error.
2899 */
2900 if (xfs_iflush(ip, sync_mode) == 0) {
2901 /* synchronize with xfs_iflush_done */
2902 xfs_iflock(ip);
2903 xfs_ifunlock(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11002906 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 reclaim:
2909 xfs_ireclaim(ip);
2910 return 0;
2911}
2912
2913int
2914xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
2915{
2916 int purged;
2917 xfs_inode_t *ip, *n;
2918 int done = 0;
2919
2920 while (!done) {
2921 purged = 0;
2922 XFS_MOUNT_ILOCK(mp);
2923 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
2924 if (noblock) {
2925 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
2926 continue;
2927 if (xfs_ipincount(ip) ||
2928 !xfs_iflock_nowait(ip)) {
2929 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2930 continue;
2931 }
2932 }
2933 XFS_MOUNT_IUNLOCK(mp);
Felix Blyakher6b2cf612005-11-25 16:42:13 +11002934 if (xfs_finish_reclaim(ip, noblock,
2935 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
2936 delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 purged = 1;
2938 break;
2939 }
2940
2941 done = !purged;
2942 }
2943
2944 XFS_MOUNT_IUNLOCK(mp);
2945 return 0;
2946}
2947
2948/*
2949 * xfs_alloc_file_space()
2950 * This routine allocates disk space for the given file.
2951 *
2952 * If alloc_type == 0, this request is for an ALLOCSP type
2953 * request which will change the file size. In this case, no
2954 * DMAPI event will be generated by the call. A TRUNCATE event
2955 * will be generated later by xfs_setattr.
2956 *
2957 * If alloc_type != 0, this request is for a RESVSP type
2958 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2959 * lower block boundary byte address is less than the file's
2960 * length.
2961 *
2962 * RETURNS:
2963 * 0 on success
2964 * errno on error
2965 *
2966 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002967STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968xfs_alloc_file_space(
2969 xfs_inode_t *ip,
2970 xfs_off_t offset,
2971 xfs_off_t len,
2972 int alloc_type,
2973 int attr_flags)
2974{
Nathan Scottdd9f4382006-01-11 15:28:28 +11002975 xfs_mount_t *mp = ip->i_mount;
2976 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 xfs_filblks_t allocated_fsb;
2978 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002979 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002981 xfs_fsblock_t firstfsb;
2982 int nimaps;
2983 int bmapi_flag;
2984 int quota_flag;
2985 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002987 xfs_bmbt_irec_t imaps[1], *imapp;
2988 xfs_bmap_free_t free_list;
2989 uint qblocks, resblks, resrtextents;
2990 int committed;
2991 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002993 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 if (XFS_FORCED_SHUTDOWN(mp))
2996 return XFS_ERROR(EIO);
2997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2999 return error;
3000
3001 if (len <= 0)
3002 return XFS_ERROR(EINVAL);
3003
David Chinner957d0eb2007-06-18 16:50:37 +10003004 rt = XFS_IS_REALTIME_INODE(ip);
3005 extsz = xfs_get_extsz_hint(ip);
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11003009 nimaps = 1;
3010 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3012 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3013
3014 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003015 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003016 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003017 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 xfs_off_t end_dmi_offset;
3019
3020 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003021 if (end_dmi_offset > ip->i_size)
3022 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003023 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
3024 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003026 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028
3029 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003030 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 */
3032retry:
3033 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003034 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Nathan Scottdd9f4382006-01-11 15:28:28 +11003036 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003037 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11003038 */
3039 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003041 do_div(s, extsz);
3042 s *= extsz;
3043 e = startoffset_fsb + allocatesize_fsb;
3044 if ((temp = do_mod(startoffset_fsb, extsz)))
3045 e += temp;
3046 if ((temp = do_mod(e, extsz)))
3047 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003049 s = 0;
3050 e = allocatesize_fsb;
3051 }
3052
3053 if (unlikely(rt)) {
3054 resrtextents = qblocks = (uint)(e - s);
3055 resrtextents /= mp->m_sb.sb_rextsize;
3056 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3057 quota_flag = XFS_QMOPT_RES_RTBLKS;
3058 } else {
3059 resrtextents = 0;
3060 resblks = qblocks = \
3061 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3062 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
3064
3065 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003066 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 */
3068 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003069 error = xfs_trans_reserve(tp, resblks,
3070 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 XFS_TRANS_PERM_LOG_RES,
3072 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003074 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 */
3076 if (error) {
3077 /*
3078 * Free the transaction structure.
3079 */
3080 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3081 xfs_trans_cancel(tp, 0);
3082 break;
3083 }
3084 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003085 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3086 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (error)
3088 goto error1;
3089
3090 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3091 xfs_trans_ihold(tp, ip);
3092
3093 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003094 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 */
3096 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003097 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003098 allocatesize_fsb, bmapi_flag,
3099 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003100 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (error) {
3102 goto error0;
3103 }
3104
3105 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003106 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003108 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 if (error) {
3110 goto error0;
3111 }
3112
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003113 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3115 if (error) {
3116 break;
3117 }
3118
3119 allocated_fsb = imapp->br_blockcount;
3120
Nathan Scottdd9f4382006-01-11 15:28:28 +11003121 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 error = XFS_ERROR(ENOSPC);
3123 break;
3124 }
3125
3126 startoffset_fsb += allocated_fsb;
3127 allocatesize_fsb -= allocated_fsb;
3128 }
3129dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003130 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003131 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003133 ip, DM_RIGHT_NULL,
3134 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3136 if (error == 0)
3137 goto retry; /* Maybe DMAPI app. has made space */
3138 /* else fall through with error from XFS_SEND_DATA */
3139 }
3140
3141 return error;
3142
Nathan Scottdd9f4382006-01-11 15:28:28 +11003143error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003145 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3146
3147error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3149 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3150 goto dmapi_enospc_check;
3151}
3152
3153/*
3154 * Zero file bytes between startoff and endoff inclusive.
3155 * The iolock is held exclusive and no blocks are buffered.
3156 */
3157STATIC int
3158xfs_zero_remaining_bytes(
3159 xfs_inode_t *ip,
3160 xfs_off_t startoff,
3161 xfs_off_t endoff)
3162{
3163 xfs_bmbt_irec_t imap;
3164 xfs_fileoff_t offset_fsb;
3165 xfs_off_t lastoffset;
3166 xfs_off_t offset;
3167 xfs_buf_t *bp;
3168 xfs_mount_t *mp = ip->i_mount;
3169 int nimap;
3170 int error = 0;
3171
3172 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003173 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 mp->m_rtdev_targp : mp->m_ddev_targp);
3175
3176 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3177 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3178 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003179 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003180 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 if (error || nimap < 1)
3182 break;
3183 ASSERT(imap.br_blockcount >= 1);
3184 ASSERT(imap.br_startoff == offset_fsb);
3185 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3186 if (lastoffset > endoff)
3187 lastoffset = endoff;
3188 if (imap.br_startblock == HOLESTARTBLOCK)
3189 continue;
3190 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3191 if (imap.br_state == XFS_EXT_UNWRITTEN)
3192 continue;
3193 XFS_BUF_UNDONE(bp);
3194 XFS_BUF_UNWRITE(bp);
3195 XFS_BUF_READ(bp);
3196 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3197 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003198 error = xfs_iowait(bp);
3199 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3201 mp, bp, XFS_BUF_ADDR(bp));
3202 break;
3203 }
3204 memset(XFS_BUF_PTR(bp) +
3205 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3206 0, lastoffset - offset + 1);
3207 XFS_BUF_UNDONE(bp);
3208 XFS_BUF_UNREAD(bp);
3209 XFS_BUF_WRITE(bp);
3210 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003211 error = xfs_iowait(bp);
3212 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3214 mp, bp, XFS_BUF_ADDR(bp));
3215 break;
3216 }
3217 }
3218 xfs_buf_free(bp);
3219 return error;
3220}
3221
3222/*
3223 * xfs_free_file_space()
3224 * This routine frees disk space for the given file.
3225 *
3226 * This routine is only called by xfs_change_file_space
3227 * for an UNRESVSP type call.
3228 *
3229 * RETURNS:
3230 * 0 on success
3231 * errno on error
3232 *
3233 */
3234STATIC int
3235xfs_free_file_space(
3236 xfs_inode_t *ip,
3237 xfs_off_t offset,
3238 xfs_off_t len,
3239 int attr_flags)
3240{
3241 int committed;
3242 int done;
3243 xfs_off_t end_dmi_offset;
3244 xfs_fileoff_t endoffset_fsb;
3245 int error;
3246 xfs_fsblock_t firstfsb;
3247 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 xfs_bmbt_irec_t imap;
3249 xfs_off_t ioffset;
3250 xfs_extlen_t mod=0;
3251 xfs_mount_t *mp;
3252 int nimap;
3253 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10003254 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 int rt;
3256 xfs_fileoff_t startoffset_fsb;
3257 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003258 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 mp = ip->i_mount;
3261
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003262 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3265 return error;
3266
3267 error = 0;
3268 if (len <= 0) /* if nothing being freed */
3269 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003270 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3272 end_dmi_offset = offset + len;
3273 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3274
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003275 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003276 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003277 if (end_dmi_offset > ip->i_size)
3278 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003279 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 offset, end_dmi_offset - offset,
3281 AT_DELAY_FLAG(attr_flags), NULL);
3282 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003283 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 }
3285
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003286 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003287 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11003288 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10003290 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11003291 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003292
Tim Shimmine6a4b372007-11-23 16:30:42 +11003293 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003295
Christoph Hellwigdf80c932008-08-13 16:22:09 +10003296 if (VN_CACHED(VFS_I(ip)) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003297 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3298 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10003299 if (error)
3300 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003301 }
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 /*
3304 * Need to zero the stuff we're not freeing, on disk.
3305 * If its a realtime file & can't use unwritten extents then we
3306 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3307 * will take care of it for us.
3308 */
Eric Sandeen62118702008-03-06 13:44:28 +11003309 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003311 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003312 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (error)
3314 goto out_unlock_iolock;
3315 ASSERT(nimap == 0 || nimap == 1);
3316 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3317 xfs_daddr_t block;
3318
3319 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3320 block = imap.br_startblock;
3321 mod = do_div(block, mp->m_sb.sb_rextsize);
3322 if (mod)
3323 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3324 }
3325 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003326 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003327 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 if (error)
3329 goto out_unlock_iolock;
3330 ASSERT(nimap == 0 || nimap == 1);
3331 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3332 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3333 mod++;
3334 if (mod && (mod != mp->m_sb.sb_rextsize))
3335 endoffset_fsb -= mod;
3336 }
3337 }
3338 if ((done = (endoffset_fsb <= startoffset_fsb)))
3339 /*
3340 * One contiguous piece to clear
3341 */
3342 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3343 else {
3344 /*
3345 * Some full blocks, possibly two pieces to clear
3346 */
3347 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3348 error = xfs_zero_remaining_bytes(ip, offset,
3349 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3350 if (!error &&
3351 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3352 error = xfs_zero_remaining_bytes(ip,
3353 XFS_FSB_TO_B(mp, endoffset_fsb),
3354 offset + len - 1);
3355 }
3356
3357 /*
3358 * free file space until done or until there is an error
3359 */
3360 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3361 while (!error && !done) {
3362
3363 /*
David Chinner91ebecc2007-07-19 16:28:30 +10003364 * allocate and setup the transaction. Allow this
3365 * transaction to dip into the reserve blocks to ensure
3366 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 */
3368 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10003369 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 error = xfs_trans_reserve(tp,
3371 resblks,
3372 XFS_WRITE_LOG_RES(mp),
3373 0,
3374 XFS_TRANS_PERM_LOG_RES,
3375 XFS_WRITE_LOG_COUNT);
3376
3377 /*
3378 * check for running out of space
3379 */
3380 if (error) {
3381 /*
3382 * Free the transaction structure.
3383 */
3384 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3385 xfs_trans_cancel(tp, 0);
3386 break;
3387 }
3388 xfs_ilock(ip, XFS_ILOCK_EXCL);
3389 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003390 ip->i_udquot, ip->i_gdquot, resblks, 0,
3391 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (error)
3393 goto error1;
3394
3395 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3396 xfs_trans_ihold(tp, ip);
3397
3398 /*
3399 * issue the bunmapi() call to free the blocks
3400 */
3401 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003402 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003404 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 if (error) {
3406 goto error0;
3407 }
3408
3409 /*
3410 * complete the transaction
3411 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003412 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 if (error) {
3414 goto error0;
3415 }
3416
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003417 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3419 }
3420
3421 out_unlock_iolock:
3422 if (need_iolock)
3423 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3424 return error;
3425
3426 error0:
3427 xfs_bmap_cancel(&free_list);
3428 error1:
3429 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3430 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3431 XFS_ILOCK_EXCL);
3432 return error;
3433}
3434
3435/*
3436 * xfs_change_file_space()
3437 * This routine allocates or frees disk space for the given file.
3438 * The user specified parameters are checked for alignment and size
3439 * limitations.
3440 *
3441 * RETURNS:
3442 * 0 on success
3443 * errno on error
3444 *
3445 */
3446int
3447xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003448 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 int cmd,
3450 xfs_flock64_t *bf,
3451 xfs_off_t offset,
3452 cred_t *credp,
3453 int attr_flags)
3454{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003455 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 int clrprealloc;
3457 int error;
3458 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 int setprealloc;
3460 xfs_off_t startoffset;
3461 xfs_off_t llen;
3462 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003463 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003465 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Christoph Hellwig993386c2007-08-28 16:12:30 +10003467 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 return XFS_ERROR(EINVAL);
3469
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 switch (bf->l_whence) {
3471 case 0: /*SEEK_SET*/
3472 break;
3473 case 1: /*SEEK_CUR*/
3474 bf->l_start += offset;
3475 break;
3476 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003477 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 break;
3479 default:
3480 return XFS_ERROR(EINVAL);
3481 }
3482
3483 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3484
3485 if ( (bf->l_start < 0)
3486 || (bf->l_start > XFS_MAXIOFFSET(mp))
3487 || (bf->l_start + llen < 0)
3488 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3489 return XFS_ERROR(EINVAL);
3490
3491 bf->l_whence = 0;
3492
3493 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003494 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
3496 /*
3497 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3498 * file space.
3499 * These calls do NOT zero the data space allocated to the file,
3500 * nor do they change the file size.
3501 *
3502 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3503 * space.
3504 * These calls cause the new file data to be zeroed and the file
3505 * size to be changed.
3506 */
3507 setprealloc = clrprealloc = 0;
3508
3509 switch (cmd) {
3510 case XFS_IOC_RESVSP:
3511 case XFS_IOC_RESVSP64:
3512 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3513 1, attr_flags);
3514 if (error)
3515 return error;
3516 setprealloc = 1;
3517 break;
3518
3519 case XFS_IOC_UNRESVSP:
3520 case XFS_IOC_UNRESVSP64:
3521 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3522 attr_flags)))
3523 return error;
3524 break;
3525
3526 case XFS_IOC_ALLOCSP:
3527 case XFS_IOC_ALLOCSP64:
3528 case XFS_IOC_FREESP:
3529 case XFS_IOC_FREESP64:
3530 if (startoffset > fsize) {
3531 error = xfs_alloc_file_space(ip, fsize,
3532 startoffset - fsize, 0, attr_flags);
3533 if (error)
3534 break;
3535 }
3536
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003537 iattr.ia_valid = ATTR_SIZE;
3538 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003540 error = xfs_setattr(ip, &iattr, attr_flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 if (error)
3543 return error;
3544
3545 clrprealloc = 1;
3546 break;
3547
3548 default:
3549 ASSERT(0);
3550 return XFS_ERROR(EINVAL);
3551 }
3552
3553 /*
3554 * update the inode timestamp, mode, and prealloc flag bits
3555 */
3556 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3557
3558 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3559 0, 0, 0))) {
3560 /* ASSERT(0); */
3561 xfs_trans_cancel(tp, 0);
3562 return error;
3563 }
3564
3565 xfs_ilock(ip, XFS_ILOCK_EXCL);
3566
3567 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3568 xfs_trans_ihold(tp, ip);
3569
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003570 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 ip->i_d.di_mode &= ~S_ISUID;
3572
3573 /*
3574 * Note that we don't have to worry about mandatory
3575 * file locking being disabled here because we only
3576 * clear the S_ISGID bit if the Group execute bit is
3577 * on, but if it was on then mandatory locking wouldn't
3578 * have been enabled.
3579 */
3580 if (ip->i_d.di_mode & S_IXGRP)
3581 ip->i_d.di_mode &= ~S_ISGID;
3582
3583 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3584 }
3585 if (setprealloc)
3586 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3587 else if (clrprealloc)
3588 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3589
3590 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3591 xfs_trans_set_sync(tp);
3592
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003593 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
3595 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3596
3597 return error;
3598}