blob: 76a1166af8227ac0b45715986bcad98daa469c87 [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;
Christoph Hellwigf13fae22008-07-21 16:16:15 +100086 struct inode *inode = XFS_ITOV(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. */
717 error = filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
718 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 bhv_vnode_t *vp = XFS_ITOV(ip);
1164 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int error;
1166
Christoph Hellwig42173f62008-04-22 17:33:25 +10001167 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001171 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return 0;
1173
David Chinner2a82b8b2007-07-11 11:09:12 +10001174 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001175 int truncated;
1176
David Chinner2a82b8b2007-07-11 11:09:12 +10001177 /*
1178 * If we are using filestreams, and we have an unlinked
1179 * file that we are processing the last close on, then nothing
1180 * will be able to reopen and write to this file. Purge this
1181 * inode from the filestreams cache so that it doesn't delay
1182 * teardown of the inode.
1183 */
1184 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1185 xfs_filestream_deassociate(ip);
1186
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001187 /*
1188 * If we previously truncated this file and removed old data
1189 * in the process, we want to initiate "early" writeout on
1190 * the last close. This is an attempt to combat the notorious
1191 * NULL files problem which is particularly noticable from a
1192 * truncate down, buffered (re-)write (delalloc), followed by
1193 * a crash. What we are effectively doing here is
1194 * significantly reducing the time window where we'd otherwise
1195 * be exposed to that problem.
1196 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001197 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001198 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001199 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001200 }
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (ip->i_d.di_nlink != 0) {
1203 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001204 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001205 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001207 (!(ip->i_d.di_flags &
1208 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001209 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1210 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001211 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213 }
1214
1215 return 0;
1216}
1217
1218/*
1219 * xfs_inactive
1220 *
1221 * This is called when the vnode reference count for the vnode
1222 * goes to zero. If the file has been unlinked, then it must
1223 * now be truncated. Also, we clear all of the read-ahead state
1224 * kept for the inode here since the file is now closed.
1225 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001226int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001228 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001230 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +10001231 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 xfs_fsblock_t first_block;
1233 int committed;
1234 xfs_trans_t *tp;
1235 xfs_mount_t *mp;
1236 int error;
1237 int truncate;
1238
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001239 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /*
1242 * If the inode is already free, then there can be nothing
1243 * to clean up here.
1244 */
1245 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1246 ASSERT(ip->i_df.if_real_bytes == 0);
1247 ASSERT(ip->i_df.if_broot_bytes == 0);
1248 return VN_INACTIVE_CACHE;
1249 }
1250
1251 /*
1252 * Only do a truncate if it's a regular file with
1253 * some actual space in it. It's OK to look at the
1254 * inode's fields without the lock because we're the
1255 * only one with a reference to the inode.
1256 */
1257 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001258 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1259 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1261
1262 mp = ip->i_mount;
1263
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001264 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1265 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 error = 0;
1268
1269 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001270 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 goto out;
1272
1273 if (ip->i_d.di_nlink != 0) {
1274 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001275 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001276 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001277 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1278 (!(ip->i_d.di_flags &
1279 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1280 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001281 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1282 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001283 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 goto out;
1286 }
1287
1288 ASSERT(ip->i_d.di_nlink == 0);
1289
1290 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001291 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1294 if (truncate) {
1295 /*
1296 * Do the xfs_itruncate_start() call before
1297 * reserving any log space because itruncate_start
1298 * will call into the buffer cache and we can't
1299 * do that within a transaction.
1300 */
1301 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1302
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001303 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1304 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001305 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001306 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1307 return VN_INACTIVE_CACHE;
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 error = xfs_trans_reserve(tp, 0,
1311 XFS_ITRUNCATE_LOG_RES(mp),
1312 0, XFS_TRANS_PERM_LOG_RES,
1313 XFS_ITRUNCATE_LOG_COUNT);
1314 if (error) {
1315 /* Don't call itruncate_cleanup */
1316 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1317 xfs_trans_cancel(tp, 0);
1318 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001319 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321
1322 xfs_ilock(ip, XFS_ILOCK_EXCL);
1323 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1324 xfs_trans_ihold(tp, ip);
1325
1326 /*
1327 * normally, we have to run xfs_itruncate_finish sync.
1328 * But if filesystem is wsync and we're in the inactive
1329 * path, then we know that nlink == 0, and that the
1330 * xaction that made nlink == 0 is permanently committed
1331 * since xfs_remove runs as a synchronous transaction.
1332 */
1333 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1334 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1335
1336 if (error) {
1337 xfs_trans_cancel(tp,
1338 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1339 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001340 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1343
1344 /*
1345 * If we get an error while cleaning up a
1346 * symlink we bail out.
1347 */
1348 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1349 xfs_inactive_symlink_rmt(ip, &tp) :
1350 xfs_inactive_symlink_local(ip, &tp);
1351
1352 if (error) {
1353 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001354 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356
1357 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1358 xfs_trans_ihold(tp, ip);
1359 } else {
1360 error = xfs_trans_reserve(tp, 0,
1361 XFS_IFREE_LOG_RES(mp),
1362 0, XFS_TRANS_PERM_LOG_RES,
1363 XFS_INACTIVE_LOG_COUNT);
1364 if (error) {
1365 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1366 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001367 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
1370 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1371 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1372 xfs_trans_ihold(tp, ip);
1373 }
1374
1375 /*
1376 * If there are attributes associated with the file
1377 * then blow them away now. The code calls a routine
1378 * that recursively deconstructs the attribute fork.
1379 * We need to just commit the current transaction
1380 * because we can't use it for xfs_attr_inactive().
1381 */
1382 if (ip->i_d.di_anextents > 0) {
1383 error = xfs_inactive_attrs(ip, &tp);
1384 /*
1385 * If we got an error, the transaction is already
1386 * cancelled, and the inode is unlocked. Just get out.
1387 */
1388 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001389 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 } else if (ip->i_afp) {
1391 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1392 }
1393
1394 /*
1395 * Free the inode.
1396 */
1397 XFS_BMAP_INIT(&free_list, &first_block);
1398 error = xfs_ifree(tp, ip, &free_list);
1399 if (error) {
1400 /*
1401 * If we fail to free the inode, shut down. The cancel
1402 * might do that, we need to make sure. Otherwise the
1403 * inode might be lost for a long time or forever.
1404 */
1405 if (!XFS_FORCED_SHUTDOWN(mp)) {
1406 cmn_err(CE_NOTE,
1407 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1408 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001409 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1412 } else {
1413 /*
1414 * Credit the quota account(s). The inode is gone.
1415 */
1416 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1417
1418 /*
David Chinner78e9da72008-04-10 12:24:17 +10001419 * Just ignore errors at this point. There is nothing we can
1420 * do except to try to keep going. Make sure it's not a silent
1421 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
David Chinner78e9da72008-04-10 12:24:17 +10001423 error = xfs_bmap_finish(&tp, &free_list, &committed);
1424 if (error)
1425 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1426 "xfs_bmap_finish() returned error %d", error);
1427 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1428 if (error)
1429 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1430 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432 /*
1433 * Release the dquots held by inode, if any.
1434 */
1435 XFS_QM_DQDETACH(mp, ip);
1436
1437 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1438
1439 out:
1440 return VN_INACTIVE_CACHE;
1441}
1442
Barry Naujok384f3ce2008-05-21 16:58:22 +10001443/*
1444 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1445 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1446 * ci_name->name will point to a the actual name (caller must free) or
1447 * will be set to NULL if an exact match is found.
1448 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001449int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001451 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001452 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001453 xfs_inode_t **ipp,
1454 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001456 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int error;
1458 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001460 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1463 return XFS_ERROR(EIO);
1464
1465 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001466 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001468
1469 if (error)
1470 goto out;
1471
1472 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1473 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001474 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001475
1476 xfs_itrace_ref(*ipp);
1477 return 0;
1478
Barry Naujok384f3ce2008-05-21 16:58:22 +10001479out_free_name:
1480 if (ci_name)
1481 kmem_free(ci_name->name);
1482out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001483 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return error;
1485}
1486
Christoph Hellwig993386c2007-08-28 16:12:30 +10001487int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001489 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001490 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001491 mode_t mode,
1492 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001493 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 cred_t *credp)
1495{
Barry Naujok556b8b12008-04-10 12:22:07 +10001496 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001497 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 xfs_trans_t *tp;
Barry Naujok556b8b12008-04-10 12:22:07 +10001499 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 xfs_bmap_free_t free_list;
1501 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001502 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int dm_event_sent = 0;
1504 uint cancel_flags;
1505 int committed;
1506 xfs_prid_t prid;
1507 struct xfs_dquot *udqp, *gdqp;
1508 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001510 ASSERT(!*ipp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001511 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001513 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001515 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001516 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001517 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (error)
1520 return error;
1521 dm_event_sent = 1;
1522 }
1523
1524 if (XFS_FORCED_SHUTDOWN(mp))
1525 return XFS_ERROR(EIO);
1526
1527 /* Return through std_return after this point. */
1528
1529 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001530 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1531 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 else
1533 prid = (xfs_prid_t)dfltprid;
1534
1535 /*
1536 * Make sure that we have allocated dquot(s) on disk.
1537 */
1538 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001539 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1541 if (error)
1542 goto std_return;
1543
1544 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1547 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001548 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 /*
1550 * Initially assume that the file does not exist and
1551 * reserve the resources for that case. If that is not
1552 * the case we'll drop the one we have and get a more
1553 * appropriate transaction later.
1554 */
1555 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1556 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1557 if (error == ENOSPC) {
1558 resblks = 0;
1559 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1560 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1561 }
1562 if (error) {
1563 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 goto error_return;
1565 }
1566
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001567 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001568 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 XFS_BMAP_INIT(&free_list, &first_block);
1571
1572 ASSERT(ip == NULL);
1573
1574 /*
1575 * Reserve disk quota and the inode.
1576 */
1577 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1578 if (error)
1579 goto error_return;
1580
Barry Naujok556b8b12008-04-10 12:22:07 +10001581 error = xfs_dir_canenter(tp, dp, name, resblks);
1582 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001584 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 rdev, credp, prid, resblks > 0,
1586 &ip, &committed);
1587 if (error) {
1588 if (error == ENOSPC)
1589 goto error_return;
1590 goto abort_return;
1591 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001592 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 /*
1595 * At this point, we've gotten a newly allocated inode.
1596 * It is locked (and joined to the transaction).
1597 */
1598
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001599 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001602 * Now we join the directory inode to the transaction. We do not do it
1603 * earlier because xfs_dir_ialloc might commit the previous transaction
1604 * (and release all the locks). An error from here on will result in
1605 * the transaction cancel unlocking dp so don't do it explicitly in the
1606 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001608 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001610 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Barry Naujok556b8b12008-04-10 12:22:07 +10001612 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001613 &first_block, &free_list, resblks ?
1614 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (error) {
1616 ASSERT(error != ENOSPC);
1617 goto abort_return;
1618 }
1619 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1620 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1621
1622 /*
1623 * If this is a synchronous mount, make sure that the
1624 * create transaction goes to disk before returning to
1625 * the user.
1626 */
1627 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1628 xfs_trans_set_sync(tp);
1629 }
1630
1631 dp->i_gen++;
1632
1633 /*
1634 * Attach the dquot(s) to the inodes and modify them incore.
1635 * These ids of the inode couldn't have changed since the new
1636 * inode has been locked ever since it was created.
1637 */
1638 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1639
1640 /*
1641 * xfs_trans_commit normally decrements the vnode ref count
1642 * when it unlocks the inode. Since we want to return the
1643 * vnode to the caller, we bump the vnode ref count now.
1644 */
1645 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001647 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (error) {
1649 xfs_bmap_cancel(&free_list);
1650 goto abort_rele;
1651 }
1652
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001653 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (error) {
1655 IRELE(ip);
1656 tp = NULL;
1657 goto error_return;
1658 }
1659
1660 XFS_QM_DQRELE(mp, udqp);
1661 XFS_QM_DQRELE(mp, gdqp);
1662
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001663 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 /* Fallthrough to std_return with error = 0 */
1666
1667std_return:
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001668 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10001669 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001671 dp, DM_RIGHT_NULL,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001672 *ipp ? ip : NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001673 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001674 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 return error;
1677
1678 abort_return:
1679 cancel_flags |= XFS_TRANS_ABORT;
1680 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Jesper Juhl014c2542006-01-15 02:37:08 +01001682 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (tp != NULL)
1684 xfs_trans_cancel(tp, cancel_flags);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 XFS_QM_DQRELE(mp, udqp);
1687 XFS_QM_DQRELE(mp, gdqp);
1688
Christoph Hellwig993386c2007-08-28 16:12:30 +10001689 if (unlock_dp_on_error)
1690 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto std_return;
1693
1694 abort_rele:
1695 /*
1696 * Wait until after the current transaction is aborted to
1697 * release the inode. This prevents recursive transactions
1698 * and deadlocks from xfs_inactive.
1699 */
1700 cancel_flags |= XFS_TRANS_ABORT;
1701 xfs_trans_cancel(tp, cancel_flags);
1702 IRELE(ip);
1703
1704 XFS_QM_DQRELE(mp, udqp);
1705 XFS_QM_DQRELE(mp, gdqp);
1706
1707 goto std_return;
1708}
1709
1710#ifdef DEBUG
1711/*
1712 * Some counters to see if (and how often) we are hitting some deadlock
1713 * prevention code paths.
1714 */
1715
1716int xfs_rm_locks;
1717int xfs_rm_lock_delays;
1718int xfs_rm_attempts;
1719#endif
1720
1721/*
1722 * The following routine will lock the inodes associated with the
1723 * directory and the named entry in the directory. The locks are
1724 * acquired in increasing inode number.
1725 *
1726 * If the entry is "..", then only the directory is locked. The
1727 * vnode ref count will still include that from the .. entry in
1728 * this case.
1729 *
1730 * There is a deadlock we need to worry about. If the locked directory is
1731 * in the AIL, it might be blocking up the log. The next inode we lock
1732 * could be already locked by another thread waiting for log space (e.g
1733 * a permanent log reservation with a long running transaction (see
1734 * xfs_itruncate_finish)). To solve this, we must check if the directory
1735 * is in the ail and use lock_nowait. If we can't lock, we need to
1736 * drop the inode lock on the directory and try again. xfs_iunlock will
1737 * potentially push the tail if we were holding up the log.
1738 */
1739STATIC int
1740xfs_lock_dir_and_entry(
1741 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 xfs_inode_t *ip) /* inode of entry 'name' */
1743{
1744 int attempts;
1745 xfs_ino_t e_inum;
1746 xfs_inode_t *ips[2];
1747 xfs_log_item_t *lp;
1748
1749#ifdef DEBUG
1750 xfs_rm_locks++;
1751#endif
1752 attempts = 0;
1753
1754again:
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001755 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 e_inum = ip->i_ino;
1758
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001759 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 /*
1762 * We want to lock in increasing inum. Since we've already
1763 * acquired the lock on the directory, we may need to release
1764 * if if the inum of the entry turns out to be less.
1765 */
1766 if (e_inum > dp->i_ino) {
1767 /*
1768 * We are already in the right order, so just
1769 * lock on the inode of the entry.
1770 * We need to use nowait if dp is in the AIL.
1771 */
1772
1773 lp = (xfs_log_item_t *)dp->i_itemp;
1774 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1775 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1776 attempts++;
1777#ifdef DEBUG
1778 xfs_rm_attempts++;
1779#endif
1780
1781 /*
1782 * Unlock dp and try again.
1783 * xfs_iunlock will try to push the tail
1784 * if the inode is in the AIL.
1785 */
1786
1787 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1788
1789 if ((attempts % 5) == 0) {
1790 delay(1); /* Don't just spin the CPU */
1791#ifdef DEBUG
1792 xfs_rm_lock_delays++;
1793#endif
1794 }
1795 goto again;
1796 }
1797 } else {
1798 xfs_ilock(ip, XFS_ILOCK_EXCL);
1799 }
1800 } else if (e_inum < dp->i_ino) {
1801 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1802
1803 ips[0] = ip;
1804 ips[1] = dp;
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001805 xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807 /* else e_inum == dp->i_ino */
1808 /* This can happen if we're asked to lock /x/..
1809 * the entry is "..", which is also the parent directory.
1810 */
1811
1812 return 0;
1813}
1814
1815#ifdef DEBUG
1816int xfs_locked_n;
1817int xfs_small_retries;
1818int xfs_middle_retries;
1819int xfs_lots_retries;
1820int xfs_lock_delays;
1821#endif
1822
1823/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001824 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1825 * a different value
1826 */
1827static inline int
1828xfs_lock_inumorder(int lock_mode, int subclass)
1829{
1830 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001831 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001832 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001833 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001834
1835 return lock_mode;
1836}
1837
1838/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 * The following routine will lock n inodes in exclusive mode.
1840 * We assume the caller calls us with the inodes in i_ino order.
1841 *
1842 * We need to detect deadlock where an inode that we lock
1843 * is in the AIL and we start waiting for another inode that is locked
1844 * by a thread in a long running transaction (such as truncate). This can
1845 * result in deadlock since the long running trans might need to wait
1846 * for the inode we just locked in order to push the tail and free space
1847 * in the log.
1848 */
1849void
1850xfs_lock_inodes(
1851 xfs_inode_t **ips,
1852 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 uint lock_mode)
1854{
1855 int attempts = 0, i, j, try_lock;
1856 xfs_log_item_t *lp;
1857
1858 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1859
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001860 try_lock = 0;
1861 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863again:
1864 for (; i < inodes; i++) {
1865 ASSERT(ips[i]);
1866
1867 if (i && (ips[i] == ips[i-1])) /* Already locked */
1868 continue;
1869
1870 /*
1871 * If try_lock is not set yet, make sure all locked inodes
1872 * are not in the AIL.
1873 * If any are, set try_lock to be used later.
1874 */
1875
1876 if (!try_lock) {
1877 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1878 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1879 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1880 try_lock++;
1881 }
1882 }
1883 }
1884
1885 /*
1886 * If any of the previous locks we have locked is in the AIL,
1887 * we must TRY to get the second and subsequent locks. If
1888 * we can't get any, we must release all we have
1889 * and try again.
1890 */
1891
1892 if (try_lock) {
1893 /* try_lock must be 0 if i is 0. */
1894 /*
1895 * try_lock means we have an inode locked
1896 * that is in the AIL.
1897 */
1898 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001899 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 attempts++;
1901
1902 /*
1903 * Unlock all previous guys and try again.
1904 * xfs_iunlock will try to push the tail
1905 * if the inode is in the AIL.
1906 */
1907
1908 for(j = i - 1; j >= 0; j--) {
1909
1910 /*
1911 * Check to see if we've already
1912 * unlocked this one.
1913 * Not the first one going back,
1914 * and the inode ptr is the same.
1915 */
1916 if ((j != (i - 1)) && ips[j] ==
1917 ips[j+1])
1918 continue;
1919
1920 xfs_iunlock(ips[j], lock_mode);
1921 }
1922
1923 if ((attempts % 5) == 0) {
1924 delay(1); /* Don't just spin the CPU */
1925#ifdef DEBUG
1926 xfs_lock_delays++;
1927#endif
1928 }
1929 i = 0;
1930 try_lock = 0;
1931 goto again;
1932 }
1933 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001934 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936 }
1937
1938#ifdef DEBUG
1939 if (attempts) {
1940 if (attempts < 5) xfs_small_retries++;
1941 else if (attempts < 100) xfs_middle_retries++;
1942 else xfs_lots_retries++;
1943 } else {
1944 xfs_locked_n++;
1945 }
1946#endif
1947}
1948
Christoph Hellwig993386c2007-08-28 16:12:30 +10001949int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001951 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001952 struct xfs_name *name,
1953 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001955 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001957 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 int error = 0;
1959 xfs_bmap_free_t free_list;
1960 xfs_fsblock_t first_block;
1961 int cancel_flags;
1962 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 int link_zero;
1964 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001965 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001967 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001968 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (XFS_FORCED_SHUTDOWN(mp))
1971 return XFS_ERROR(EIO);
1972
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001973 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001974 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1975 NULL, DM_RIGHT_NULL, name->name, NULL,
1976 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (error)
1978 return error;
1979 }
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 error = XFS_QM_DQATTACH(mp, dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001982 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001985 error = XFS_QM_DQATTACH(mp, ip, 0);
1986 if (error)
1987 goto std_return;
1988
1989 if (is_dir) {
1990 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1991 log_count = XFS_DEFAULT_LOG_COUNT;
1992 } else {
1993 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1994 log_count = XFS_REMOVE_LOG_COUNT;
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 /*
1999 * We try to get the real space reservation first,
2000 * allowing for directory btree deletion(s) implying
2001 * possible bmap insert(s). If we can't get the space
2002 * reservation then we use 0 instead, and avoid the bmap
2003 * btree insert(s) in the directory code by, if the bmap
2004 * insert tries to happen, instead trimming the LAST
2005 * block from the directory.
2006 */
2007 resblks = XFS_REMOVE_SPACE_RES(mp);
2008 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002009 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (error == ENOSPC) {
2011 resblks = 0;
2012 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002013 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 if (error) {
2016 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002017 cancel_flags = 0;
2018 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002021 error = xfs_lock_dir_and_entry(dp, ip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002022 if (error)
2023 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 /*
2026 * At this point, we've gotten both the directory and the entry
2027 * inodes locked.
2028 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002029 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10002031
2032 IHOLD(dp);
2033 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002036 * If we're removing a directory perform some additional validation.
2037 */
2038 if (is_dir) {
2039 ASSERT(ip->i_d.di_nlink >= 2);
2040 if (ip->i_d.di_nlink != 2) {
2041 error = XFS_ERROR(ENOTEMPTY);
2042 goto out_trans_cancel;
2043 }
2044 if (!xfs_dir_isempty(ip)) {
2045 error = XFS_ERROR(ENOTEMPTY);
2046 goto out_trans_cancel;
2047 }
2048 }
2049
2050 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2052 */
2053 XFS_BMAP_INIT(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10002054 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10002055 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (error) {
2057 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002058 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2061
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002062 /*
2063 * Bump the in memory generation count on the parent
2064 * directory so that other can know that it has changed.
2065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 dp->i_gen++;
2067 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2068
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002069 if (is_dir) {
2070 /*
2071 * Drop the link from ip's "..".
2072 */
2073 error = xfs_droplink(tp, dp);
2074 if (error)
2075 goto out_bmap_cancel;
2076
2077 /*
2078 * Drop the link from dp to ip.
2079 */
2080 error = xfs_droplink(tp, ip);
2081 if (error)
2082 goto out_bmap_cancel;
2083 } else {
2084 /*
2085 * When removing a non-directory we need to log the parent
2086 * inode here for the i_gen update. For a directory this is
2087 * done implicitly by the xfs_droplink call for the ".." entry.
2088 */
2089 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002092 /*
2093 * Drop the "." link from ip to self.
2094 */
2095 error = xfs_droplink(tp, ip);
2096 if (error)
2097 goto out_bmap_cancel;
2098
2099 /*
2100 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 * we are in the transaction.
2102 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002103 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * If this is a synchronous mount, make sure that the
2107 * remove transaction goes to disk before returning to
2108 * the user.
2109 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002110 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002113 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002114 if (error)
2115 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002117 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002118 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002122 * If we are using filestreams, kill the stream association.
2123 * If the file is still open it may get a new one but that
2124 * will get killed on last close in xfs_close() so we don't
2125 * have to worry about that.
2126 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002127 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10002128 xfs_filestream_deassociate(ip);
2129
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002130 xfs_itrace_exit(ip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002131 xfs_itrace_exit(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002134 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002135 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2136 NULL, DM_RIGHT_NULL, name->name, NULL,
2137 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return error;
2141
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002142 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 xfs_bmap_cancel(&free_list);
2144 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002145 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 xfs_trans_cancel(tp, cancel_flags);
2147 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
Christoph Hellwig993386c2007-08-28 16:12:30 +10002150int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002152 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002153 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002154 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002156 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 xfs_inode_t *ips[2];
2159 int error;
2160 xfs_bmap_free_t free_list;
2161 xfs_fsblock_t first_block;
2162 int cancel_flags;
2163 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002166 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002167 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002169 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (XFS_FORCED_SHUTDOWN(mp))
2172 return XFS_ERROR(EIO);
2173
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002174 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002176 tdp, DM_RIGHT_NULL,
2177 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002178 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (error)
2180 return error;
2181 }
2182
2183 /* Return through std_return after this point. */
2184
2185 error = XFS_QM_DQATTACH(mp, sip, 0);
2186 if (!error && sip != tdp)
2187 error = XFS_QM_DQATTACH(mp, tdp, 0);
2188 if (error)
2189 goto std_return;
2190
2191 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2192 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002193 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2195 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2196 if (error == ENOSPC) {
2197 resblks = 0;
2198 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2199 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2200 }
2201 if (error) {
2202 cancel_flags = 0;
2203 goto error_return;
2204 }
2205
2206 if (sip->i_ino < tdp->i_ino) {
2207 ips[0] = sip;
2208 ips[1] = tdp;
2209 } else {
2210 ips[0] = tdp;
2211 ips[1] = sip;
2212 }
2213
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10002214 xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 /*
2217 * Increment vnode ref counts since xfs_trans_commit &
2218 * xfs_trans_cancel will both unlock the inodes and
2219 * decrement the associated ref counts.
2220 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002221 IHOLD(sip);
2222 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2224 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2225
2226 /*
2227 * If the source has too many links, we can't make any more to it.
2228 */
2229 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2230 error = XFS_ERROR(EMLINK);
2231 goto error_return;
2232 }
2233
Nathan Scott365ca832005-06-21 15:39:12 +10002234 /*
2235 * If we are using project inheritance, we only allow hard link
2236 * creation in our tree when the project IDs are the same; else
2237 * the tree quota mechanism could be circumvented.
2238 */
2239 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2240 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002241 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002242 goto error_return;
2243 }
2244
Barry Naujok556b8b12008-04-10 12:22:07 +10002245 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2246 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 goto error_return;
2248
2249 XFS_BMAP_INIT(&free_list, &first_block);
2250
Barry Naujok556b8b12008-04-10 12:22:07 +10002251 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2252 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (error)
2254 goto abort_return;
2255 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2256 tdp->i_gen++;
2257 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2258
2259 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002260 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 /*
2264 * If this is a synchronous mount, make sure that the
2265 * link transaction goes to disk before returning to
2266 * the user.
2267 */
2268 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2269 xfs_trans_set_sync(tp);
2270 }
2271
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002272 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (error) {
2274 xfs_bmap_cancel(&free_list);
2275 goto abort_return;
2276 }
2277
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002278 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002279 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 /* Fall through to std_return with error = 0. */
2283std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002284 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002286 tdp, DM_RIGHT_NULL,
2287 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002288 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290 return error;
2291
2292 abort_return:
2293 cancel_flags |= XFS_TRANS_ABORT;
2294 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 error_return:
2297 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto std_return;
2299}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002300
2301
Christoph Hellwig993386c2007-08-28 16:12:30 +10002302int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002304 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002305 struct xfs_name *dir_name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002306 mode_t mode,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002307 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 cred_t *credp)
2309{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002310 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 xfs_inode_t *cdp; /* inode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 int cancel_flags;
2314 int error;
2315 int committed;
2316 xfs_bmap_free_t free_list;
2317 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002318 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 boolean_t created = B_FALSE;
2320 int dm_event_sent = 0;
2321 xfs_prid_t prid;
2322 struct xfs_dquot *udqp, *gdqp;
2323 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 if (XFS_FORCED_SHUTDOWN(mp))
2326 return XFS_ERROR(EIO);
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002330 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002332 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002333 DM_RIGHT_NULL, dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002334 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (error)
2336 return error;
2337 dm_event_sent = 1;
2338 }
2339
2340 /* Return through std_return after this point. */
2341
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002342 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 mp = dp->i_mount;
2345 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002346 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2347 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 else
2349 prid = (xfs_prid_t)dfltprid;
2350
2351 /*
2352 * Make sure that we have allocated dquot(s) on disk.
2353 */
2354 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002355 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2357 if (error)
2358 goto std_return;
2359
2360 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2361 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002362 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2364 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2365 if (error == ENOSPC) {
2366 resblks = 0;
2367 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2368 XFS_TRANS_PERM_LOG_RES,
2369 XFS_MKDIR_LOG_COUNT);
2370 }
2371 if (error) {
2372 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 goto error_return;
2374 }
2375
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002376 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002377 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 /*
2380 * Check for directory link count overflow.
2381 */
2382 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2383 error = XFS_ERROR(EMLINK);
2384 goto error_return;
2385 }
2386
2387 /*
2388 * Reserve disk quota and the inode.
2389 */
2390 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2391 if (error)
2392 goto error_return;
2393
Barry Naujok556b8b12008-04-10 12:22:07 +10002394 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2395 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 goto error_return;
2397 /*
2398 * create the directory inode.
2399 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002400 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 0, credp, prid, resblks > 0,
2402 &cdp, NULL);
2403 if (error) {
2404 if (error == ENOSPC)
2405 goto error_return;
2406 goto abort_return;
2407 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002408 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 /*
2411 * Now we add the directory inode to the transaction.
2412 * We waited until now since xfs_dir_ialloc might start
2413 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002414 * earlier, the locks might have gotten released. An error
2415 * from here on will result in the transaction cancel
2416 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002418 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002420 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 XFS_BMAP_INIT(&free_list, &first_block);
2423
Barry Naujok556b8b12008-04-10 12:22:07 +10002424 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2425 &first_block, &free_list, resblks ?
2426 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (error) {
2428 ASSERT(error != ENOSPC);
2429 goto error1;
2430 }
2431 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2432
2433 /*
2434 * Bump the in memory version number of the parent directory
2435 * so that other processes accessing it will recognize that
2436 * the directory has changed.
2437 */
2438 dp->i_gen++;
2439
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002440 error = xfs_dir_init(tp, cdp, dp);
2441 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 cdp->i_gen = 1;
2445 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002446 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 created = B_TRUE;
2450
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002451 *ipp = cdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 IHOLD(cdp);
2453
2454 /*
2455 * Attach the dquots to the new inode and modify the icount incore.
2456 */
2457 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2458
2459 /*
2460 * If this is a synchronous mount, make sure that the
2461 * mkdir transaction goes to disk before returning to
2462 * the user.
2463 */
2464 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2465 xfs_trans_set_sync(tp);
2466 }
2467
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002468 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (error) {
2470 IRELE(cdp);
2471 goto error2;
2472 }
2473
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002474 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 XFS_QM_DQRELE(mp, udqp);
2476 XFS_QM_DQRELE(mp, gdqp);
2477 if (error) {
2478 IRELE(cdp);
2479 }
2480
2481 /* Fall through to std_return with error = 0 or errno from
2482 * xfs_trans_commit. */
2483
2484std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002485 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002486 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002488 dp, DM_RIGHT_NULL,
2489 created ? cdp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002491 dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002492 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494 return error;
2495
2496 error2:
2497 error1:
2498 xfs_bmap_cancel(&free_list);
2499 abort_return:
2500 cancel_flags |= XFS_TRANS_ABORT;
2501 error_return:
2502 xfs_trans_cancel(tp, cancel_flags);
2503 XFS_QM_DQRELE(mp, udqp);
2504 XFS_QM_DQRELE(mp, gdqp);
2505
Christoph Hellwig993386c2007-08-28 16:12:30 +10002506 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 goto std_return;
2510}
2511
Christoph Hellwig993386c2007-08-28 16:12:30 +10002512int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002514 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002515 struct xfs_name *link_name,
2516 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002517 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002518 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 cred_t *credp)
2520{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002521 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 xfs_inode_t *ip;
2524 int error;
2525 int pathlen;
2526 xfs_bmap_free_t free_list;
2527 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002528 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 uint cancel_flags;
2530 int committed;
2531 xfs_fileoff_t first_fsb;
2532 xfs_filblks_t fs_blocks;
2533 int nmaps;
2534 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2535 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002536 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 int byte_cnt;
2538 int n;
2539 xfs_buf_t *bp;
2540 xfs_prid_t prid;
2541 struct xfs_dquot *udqp, *gdqp;
2542 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Christoph Hellwig3937be52008-03-06 13:46:19 +11002544 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 error = 0;
2546 ip = NULL;
2547 tp = NULL;
2548
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002549 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 if (XFS_FORCED_SHUTDOWN(mp))
2552 return XFS_ERROR(EIO);
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /*
2555 * Check component lengths of the target path name.
2556 */
2557 pathlen = strlen(target_path);
2558 if (pathlen >= MAXPATHLEN) /* total string too long */
2559 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002561 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002562 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002564 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (error)
2566 return error;
2567 }
2568
2569 /* Return through std_return after this point. */
2570
2571 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002572 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2573 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 else
2575 prid = (xfs_prid_t)dfltprid;
2576
2577 /*
2578 * Make sure that we have allocated dquot(s) on disk.
2579 */
2580 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002581 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2583 if (error)
2584 goto std_return;
2585
2586 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2587 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2588 /*
2589 * The symlink will fit into the inode data fork?
2590 * There can't be any attributes so we get the whole variable part.
2591 */
2592 if (pathlen <= XFS_LITINO(mp))
2593 fs_blocks = 0;
2594 else
2595 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002596 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2598 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2599 if (error == ENOSPC && fs_blocks == 0) {
2600 resblks = 0;
2601 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2602 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2603 }
2604 if (error) {
2605 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 goto error_return;
2607 }
2608
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002609 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002610 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 /*
2613 * Check whether the directory allows new symlinks or not.
2614 */
2615 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2616 error = XFS_ERROR(EPERM);
2617 goto error_return;
2618 }
2619
2620 /*
2621 * Reserve disk quota : blocks and inode.
2622 */
2623 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2624 if (error)
2625 goto error_return;
2626
2627 /*
2628 * Check for ability to enter directory entry, if no space reserved.
2629 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002630 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2631 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 goto error_return;
2633 /*
2634 * Initialize the bmap freelist prior to calling either
2635 * bmapi or the directory create code.
2636 */
2637 XFS_BMAP_INIT(&free_list, &first_block);
2638
2639 /*
2640 * Allocate an inode for the symlink.
2641 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002642 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 1, 0, credp, prid, resblks > 0, &ip, NULL);
2644 if (error) {
2645 if (error == ENOSPC)
2646 goto error_return;
2647 goto error1;
2648 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002649 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Christoph Hellwig993386c2007-08-28 16:12:30 +10002651 /*
2652 * An error after we've joined dp to the transaction will result in the
2653 * transaction cancel unlocking dp so don't do it explicitly in the
2654 * error path.
2655 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002656 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002658 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 /*
2661 * Also attach the dquot(s) to it, if applicable.
2662 */
2663 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2664
2665 if (resblks)
2666 resblks -= XFS_IALLOC_SPACE_RES(mp);
2667 /*
2668 * If the symlink will fit into the inode, write it inline.
2669 */
2670 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2671 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2672 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2673 ip->i_d.di_size = pathlen;
2674
2675 /*
2676 * The inode was initially created in extent format.
2677 */
2678 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2679 ip->i_df.if_flags |= XFS_IFINLINE;
2680
2681 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2682 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2683
2684 } else {
2685 first_fsb = 0;
2686 nmaps = SYMLINK_MAPS;
2687
2688 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2689 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2690 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002691 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (error) {
2693 goto error1;
2694 }
2695
2696 if (resblks)
2697 resblks -= fs_blocks;
2698 ip->i_d.di_size = pathlen;
2699 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2700
2701 cur_chunk = target_path;
2702 for (n = 0; n < nmaps; n++) {
2703 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2704 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2705 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2706 BTOBB(byte_cnt), 0);
2707 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2708 if (pathlen < byte_cnt) {
2709 byte_cnt = pathlen;
2710 }
2711 pathlen -= byte_cnt;
2712
2713 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2714 cur_chunk += byte_cnt;
2715
2716 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2717 }
2718 }
2719
2720 /*
2721 * Create the directory entry for the symlink.
2722 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002723 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2724 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002725 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2728 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2729
2730 /*
2731 * Bump the in memory version number of the parent directory
2732 * so that other processes accessing it will recognize that
2733 * the directory has changed.
2734 */
2735 dp->i_gen++;
2736
2737 /*
2738 * If this is a synchronous mount, make sure that the
2739 * symlink transaction goes to disk before returning to
2740 * the user.
2741 */
2742 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2743 xfs_trans_set_sync(tp);
2744 }
2745
2746 /*
2747 * xfs_trans_commit normally decrements the vnode ref count
2748 * when it unlocks the inode. Since we want to return the
2749 * vnode to the caller, we bump the vnode ref count now.
2750 */
2751 IHOLD(ip);
2752
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002753 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (error) {
2755 goto error2;
2756 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002757 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 XFS_QM_DQRELE(mp, udqp);
2759 XFS_QM_DQRELE(mp, gdqp);
2760
2761 /* Fall through to std_return with error = 0 or errno from
2762 * xfs_trans_commit */
2763std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10002764 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002766 dp, DM_RIGHT_NULL,
2767 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002768 DM_RIGHT_NULL, link_name->name,
2769 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
2771
Christoph Hellwig3937be52008-03-06 13:46:19 +11002772 if (!error)
2773 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return error;
2775
2776 error2:
2777 IRELE(ip);
2778 error1:
2779 xfs_bmap_cancel(&free_list);
2780 cancel_flags |= XFS_TRANS_ABORT;
2781 error_return:
2782 xfs_trans_cancel(tp, cancel_flags);
2783 XFS_QM_DQRELE(mp, udqp);
2784 XFS_QM_DQRELE(mp, gdqp);
2785
Christoph Hellwig993386c2007-08-28 16:12:30 +10002786 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 goto std_return;
2790}
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002794 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 int flags)
2796{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002797 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 int error = 0;
2799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 if (XFS_FORCED_SHUTDOWN(mp))
2801 return XFS_ERROR(EIO);
2802
2803 /*
2804 * Bypass inodes which have already been cleaned by
2805 * the inode flush clustering code inside xfs_iflush
2806 */
David Chinner33540402008-03-06 13:43:59 +11002807 if (xfs_inode_clean(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return 0;
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 /*
2811 * We make this non-blocking if the inode is contended,
2812 * return EAGAIN to indicate to the caller that they
2813 * did not succeed. This prevents the flush path from
2814 * blocking on inodes inside another operation right
2815 * now, they get caught later by xfs_sync.
2816 */
David Chinnera3f74ff2008-03-06 13:43:42 +11002817 if (flags & FLUSH_SYNC) {
2818 xfs_ilock(ip, XFS_ILOCK_SHARED);
2819 xfs_iflock(ip);
2820 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
2821 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
2822 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return EAGAIN;
2824 }
David Chinnera3f74ff2008-03-06 13:43:42 +11002825 } else {
2826 return EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
2828
David Chinnera3f74ff2008-03-06 13:43:42 +11002829 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
2830 : XFS_IFLUSH_ASYNC_NOBLOCK);
2831 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return error;
2834}
2835
Christoph Hellwig993386c2007-08-28 16:12:30 +10002836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002838xfs_set_dmattrs(
2839 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002841 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002843 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 int error;
2846
2847 if (!capable(CAP_SYS_ADMIN))
2848 return XFS_ERROR(EPERM);
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 if (XFS_FORCED_SHUTDOWN(mp))
2851 return XFS_ERROR(EIO);
2852
2853 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2854 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2855 if (error) {
2856 xfs_trans_cancel(tp, 0);
2857 return error;
2858 }
2859 xfs_ilock(ip, XFS_ILOCK_EXCL);
2860 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2861
Christoph Hellwig613d7042007-10-11 17:44:08 +10002862 ip->i_d.di_dmevmask = evmask;
2863 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
2865 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2866 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002867 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 return error;
2870}
2871
Christoph Hellwig993386c2007-08-28 16:12:30 +10002872int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002874 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002876 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002878 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 ASSERT(!VN_MAPPED(vp));
2881
2882 /* bad inode, get out here ASAP */
2883 if (VN_BAD(vp)) {
2884 xfs_ireclaim(ip);
2885 return 0;
2886 }
2887
Christoph Hellwigb677c212007-08-29 11:46:28 +10002888 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10002890 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11002892 /*
2893 * Make sure the atime in the XFS inode is correct before freeing the
2894 * Linux inode.
2895 */
2896 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
David Chinner4c606582006-11-11 18:05:00 +11002898 /*
2899 * If we have nothing to flush with this inode then complete the
2900 * teardown now, otherwise break the link between the xfs inode and the
2901 * linux inode and clean up the xfs inode later. This avoids flushing
2902 * the inode to disk during the delete operation itself.
2903 *
2904 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2905 * first to ensure that xfs_iunpin() will never see an xfs inode
2906 * that has a linux inode being reclaimed. Synchronisation is provided
2907 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 */
2909 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2910 xfs_ilock(ip, XFS_ILOCK_EXCL);
2911 xfs_iflock(ip);
2912 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
2913 } else {
2914 xfs_mount_t *mp = ip->i_mount;
2915
David Chinner4c606582006-11-11 18:05:00 +11002916 /* Protect sync and unpin from us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 XFS_MOUNT_ILOCK(mp);
David Chinner4c606582006-11-11 18:05:00 +11002918 spin_lock(&ip->i_flags_lock);
2919 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10002920 vn_to_inode(vp)->i_private = NULL;
2921 ip->i_vnode = NULL;
David Chinner4c606582006-11-11 18:05:00 +11002922 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 XFS_MOUNT_IUNLOCK(mp);
2925 }
2926 return 0;
2927}
2928
2929int
2930xfs_finish_reclaim(
2931 xfs_inode_t *ip,
2932 int locked,
2933 int sync_mode)
2934{
David Chinnerda353b02007-08-28 14:00:13 +10002935 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
Nathan Scott67fcaa72006-06-09 17:00:52 +10002936 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938 if (vp && VN_BAD(vp))
2939 goto reclaim;
2940
2941 /* The hash lock here protects a thread in xfs_iget_core from
2942 * racing with us on linking the inode back with a vnode.
2943 * Once we have the XFS_IRECLAIM flag set it will not touch
2944 * us.
2945 */
David Chinnerda353b02007-08-28 14:00:13 +10002946 write_lock(&pag->pag_ici_lock);
David Chinnerf273ab82006-09-28 11:06:03 +10002947 spin_lock(&ip->i_flags_lock);
David Chinner7a18c382006-11-11 18:04:54 +11002948 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
2949 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
David Chinnerf273ab82006-09-28 11:06:03 +10002950 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10002951 write_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 if (locked) {
2953 xfs_ifunlock(ip);
2954 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2955 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002956 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
David Chinner7a18c382006-11-11 18:04:54 +11002958 __xfs_iflags_set(ip, XFS_IRECLAIM);
David Chinnerf273ab82006-09-28 11:06:03 +10002959 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10002960 write_unlock(&pag->pag_ici_lock);
2961 xfs_put_perag(ip->i_mount, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 /*
2964 * If the inode is still dirty, then flush it out. If the inode
2965 * is not in the AIL, then it will be OK to flush it delwri as
2966 * long as xfs_iflush() does not keep any references to the inode.
2967 * We leave that decision up to xfs_iflush() since it has the
2968 * knowledge of whether it's OK to simply do a delwri flush of
2969 * the inode or whether we need to wait until the inode is
2970 * pulled from the AIL.
2971 * We get the flush lock regardless, though, just to make sure
2972 * we don't free it while it is being flushed.
2973 */
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11002974 if (!locked) {
2975 xfs_ilock(ip, XFS_ILOCK_EXCL);
2976 xfs_iflock(ip);
2977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Lachlan McIlroy7f871d52008-06-23 13:23:57 +10002979 /*
2980 * In the case of a forced shutdown we rely on xfs_iflush() to
2981 * wait for the inode to be unpinned before returning an error.
2982 */
2983 if (xfs_iflush(ip, sync_mode) == 0) {
2984 /* synchronize with xfs_iflush_done */
2985 xfs_iflock(ip);
2986 xfs_ifunlock(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
2988
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11002989 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 reclaim:
2992 xfs_ireclaim(ip);
2993 return 0;
2994}
2995
2996int
2997xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
2998{
2999 int purged;
3000 xfs_inode_t *ip, *n;
3001 int done = 0;
3002
3003 while (!done) {
3004 purged = 0;
3005 XFS_MOUNT_ILOCK(mp);
3006 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3007 if (noblock) {
3008 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3009 continue;
3010 if (xfs_ipincount(ip) ||
3011 !xfs_iflock_nowait(ip)) {
3012 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3013 continue;
3014 }
3015 }
3016 XFS_MOUNT_IUNLOCK(mp);
Felix Blyakher6b2cf612005-11-25 16:42:13 +11003017 if (xfs_finish_reclaim(ip, noblock,
3018 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3019 delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 purged = 1;
3021 break;
3022 }
3023
3024 done = !purged;
3025 }
3026
3027 XFS_MOUNT_IUNLOCK(mp);
3028 return 0;
3029}
3030
3031/*
3032 * xfs_alloc_file_space()
3033 * This routine allocates disk space for the given file.
3034 *
3035 * If alloc_type == 0, this request is for an ALLOCSP type
3036 * request which will change the file size. In this case, no
3037 * DMAPI event will be generated by the call. A TRUNCATE event
3038 * will be generated later by xfs_setattr.
3039 *
3040 * If alloc_type != 0, this request is for a RESVSP type
3041 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3042 * lower block boundary byte address is less than the file's
3043 * length.
3044 *
3045 * RETURNS:
3046 * 0 on success
3047 * errno on error
3048 *
3049 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003050STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051xfs_alloc_file_space(
3052 xfs_inode_t *ip,
3053 xfs_off_t offset,
3054 xfs_off_t len,
3055 int alloc_type,
3056 int attr_flags)
3057{
Nathan Scottdd9f4382006-01-11 15:28:28 +11003058 xfs_mount_t *mp = ip->i_mount;
3059 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 xfs_filblks_t allocated_fsb;
3061 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003062 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003064 xfs_fsblock_t firstfsb;
3065 int nimaps;
3066 int bmapi_flag;
3067 int quota_flag;
3068 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003070 xfs_bmbt_irec_t imaps[1], *imapp;
3071 xfs_bmap_free_t free_list;
3072 uint qblocks, resblks, resrtextents;
3073 int committed;
3074 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003076 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 if (XFS_FORCED_SHUTDOWN(mp))
3079 return XFS_ERROR(EIO);
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3082 return error;
3083
3084 if (len <= 0)
3085 return XFS_ERROR(EINVAL);
3086
David Chinner957d0eb2007-06-18 16:50:37 +10003087 rt = XFS_IS_REALTIME_INODE(ip);
3088 extsz = xfs_get_extsz_hint(ip);
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11003092 nimaps = 1;
3093 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3095 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3096
3097 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003098 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003099 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003100 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 xfs_off_t end_dmi_offset;
3102
3103 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003104 if (end_dmi_offset > ip->i_size)
3105 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003106 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
3107 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003109 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
3111
3112 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003113 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 */
3115retry:
3116 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003117 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Nathan Scottdd9f4382006-01-11 15:28:28 +11003119 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003120 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11003121 */
3122 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003124 do_div(s, extsz);
3125 s *= extsz;
3126 e = startoffset_fsb + allocatesize_fsb;
3127 if ((temp = do_mod(startoffset_fsb, extsz)))
3128 e += temp;
3129 if ((temp = do_mod(e, extsz)))
3130 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003132 s = 0;
3133 e = allocatesize_fsb;
3134 }
3135
3136 if (unlikely(rt)) {
3137 resrtextents = qblocks = (uint)(e - s);
3138 resrtextents /= mp->m_sb.sb_rextsize;
3139 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3140 quota_flag = XFS_QMOPT_RES_RTBLKS;
3141 } else {
3142 resrtextents = 0;
3143 resblks = qblocks = \
3144 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3145 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
3147
3148 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003149 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 */
3151 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003152 error = xfs_trans_reserve(tp, resblks,
3153 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 XFS_TRANS_PERM_LOG_RES,
3155 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003157 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 */
3159 if (error) {
3160 /*
3161 * Free the transaction structure.
3162 */
3163 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3164 xfs_trans_cancel(tp, 0);
3165 break;
3166 }
3167 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003168 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3169 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 if (error)
3171 goto error1;
3172
3173 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3174 xfs_trans_ihold(tp, ip);
3175
3176 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003177 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 */
3179 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003180 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003181 allocatesize_fsb, bmapi_flag,
3182 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003183 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 if (error) {
3185 goto error0;
3186 }
3187
3188 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003189 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003191 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 if (error) {
3193 goto error0;
3194 }
3195
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003196 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3198 if (error) {
3199 break;
3200 }
3201
3202 allocated_fsb = imapp->br_blockcount;
3203
Nathan Scottdd9f4382006-01-11 15:28:28 +11003204 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 error = XFS_ERROR(ENOSPC);
3206 break;
3207 }
3208
3209 startoffset_fsb += allocated_fsb;
3210 allocatesize_fsb -= allocated_fsb;
3211 }
3212dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003213 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003214 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003216 ip, DM_RIGHT_NULL,
3217 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3219 if (error == 0)
3220 goto retry; /* Maybe DMAPI app. has made space */
3221 /* else fall through with error from XFS_SEND_DATA */
3222 }
3223
3224 return error;
3225
Nathan Scottdd9f4382006-01-11 15:28:28 +11003226error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003228 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3229
3230error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3232 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3233 goto dmapi_enospc_check;
3234}
3235
3236/*
3237 * Zero file bytes between startoff and endoff inclusive.
3238 * The iolock is held exclusive and no blocks are buffered.
3239 */
3240STATIC int
3241xfs_zero_remaining_bytes(
3242 xfs_inode_t *ip,
3243 xfs_off_t startoff,
3244 xfs_off_t endoff)
3245{
3246 xfs_bmbt_irec_t imap;
3247 xfs_fileoff_t offset_fsb;
3248 xfs_off_t lastoffset;
3249 xfs_off_t offset;
3250 xfs_buf_t *bp;
3251 xfs_mount_t *mp = ip->i_mount;
3252 int nimap;
3253 int error = 0;
3254
3255 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003256 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 mp->m_rtdev_targp : mp->m_ddev_targp);
3258
3259 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3260 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3261 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003262 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003263 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (error || nimap < 1)
3265 break;
3266 ASSERT(imap.br_blockcount >= 1);
3267 ASSERT(imap.br_startoff == offset_fsb);
3268 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3269 if (lastoffset > endoff)
3270 lastoffset = endoff;
3271 if (imap.br_startblock == HOLESTARTBLOCK)
3272 continue;
3273 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3274 if (imap.br_state == XFS_EXT_UNWRITTEN)
3275 continue;
3276 XFS_BUF_UNDONE(bp);
3277 XFS_BUF_UNWRITE(bp);
3278 XFS_BUF_READ(bp);
3279 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3280 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003281 error = xfs_iowait(bp);
3282 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3284 mp, bp, XFS_BUF_ADDR(bp));
3285 break;
3286 }
3287 memset(XFS_BUF_PTR(bp) +
3288 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3289 0, lastoffset - offset + 1);
3290 XFS_BUF_UNDONE(bp);
3291 XFS_BUF_UNREAD(bp);
3292 XFS_BUF_WRITE(bp);
3293 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003294 error = xfs_iowait(bp);
3295 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3297 mp, bp, XFS_BUF_ADDR(bp));
3298 break;
3299 }
3300 }
3301 xfs_buf_free(bp);
3302 return error;
3303}
3304
3305/*
3306 * xfs_free_file_space()
3307 * This routine frees disk space for the given file.
3308 *
3309 * This routine is only called by xfs_change_file_space
3310 * for an UNRESVSP type call.
3311 *
3312 * RETURNS:
3313 * 0 on success
3314 * errno on error
3315 *
3316 */
3317STATIC int
3318xfs_free_file_space(
3319 xfs_inode_t *ip,
3320 xfs_off_t offset,
3321 xfs_off_t len,
3322 int attr_flags)
3323{
Nathan Scott67fcaa72006-06-09 17:00:52 +10003324 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 int committed;
3326 int done;
3327 xfs_off_t end_dmi_offset;
3328 xfs_fileoff_t endoffset_fsb;
3329 int error;
3330 xfs_fsblock_t firstfsb;
3331 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 xfs_bmbt_irec_t imap;
3333 xfs_off_t ioffset;
3334 xfs_extlen_t mod=0;
3335 xfs_mount_t *mp;
3336 int nimap;
3337 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10003338 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 int rt;
3340 xfs_fileoff_t startoffset_fsb;
3341 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003342 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003344 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 mp = ip->i_mount;
3346
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003347 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3350 return error;
3351
3352 error = 0;
3353 if (len <= 0) /* if nothing being freed */
3354 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003355 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3357 end_dmi_offset = offset + len;
3358 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3359
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003360 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003361 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003362 if (end_dmi_offset > ip->i_size)
3363 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003364 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 offset, end_dmi_offset - offset,
3366 AT_DELAY_FLAG(attr_flags), NULL);
3367 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003368 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
3370
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003371 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003372 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11003373 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10003375 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11003376 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003377
Tim Shimmine6a4b372007-11-23 16:30:42 +11003378 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003380
3381 if (VN_CACHED(vp) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003382 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3383 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10003384 if (error)
3385 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003386 }
3387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 /*
3389 * Need to zero the stuff we're not freeing, on disk.
3390 * If its a realtime file & can't use unwritten extents then we
3391 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3392 * will take care of it for us.
3393 */
Eric Sandeen62118702008-03-06 13:44:28 +11003394 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003396 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003397 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 if (error)
3399 goto out_unlock_iolock;
3400 ASSERT(nimap == 0 || nimap == 1);
3401 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3402 xfs_daddr_t block;
3403
3404 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3405 block = imap.br_startblock;
3406 mod = do_div(block, mp->m_sb.sb_rextsize);
3407 if (mod)
3408 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3409 }
3410 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003411 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003412 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 if (error)
3414 goto out_unlock_iolock;
3415 ASSERT(nimap == 0 || nimap == 1);
3416 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3417 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3418 mod++;
3419 if (mod && (mod != mp->m_sb.sb_rextsize))
3420 endoffset_fsb -= mod;
3421 }
3422 }
3423 if ((done = (endoffset_fsb <= startoffset_fsb)))
3424 /*
3425 * One contiguous piece to clear
3426 */
3427 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3428 else {
3429 /*
3430 * Some full blocks, possibly two pieces to clear
3431 */
3432 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3433 error = xfs_zero_remaining_bytes(ip, offset,
3434 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3435 if (!error &&
3436 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3437 error = xfs_zero_remaining_bytes(ip,
3438 XFS_FSB_TO_B(mp, endoffset_fsb),
3439 offset + len - 1);
3440 }
3441
3442 /*
3443 * free file space until done or until there is an error
3444 */
3445 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3446 while (!error && !done) {
3447
3448 /*
David Chinner91ebecc2007-07-19 16:28:30 +10003449 * allocate and setup the transaction. Allow this
3450 * transaction to dip into the reserve blocks to ensure
3451 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 */
3453 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10003454 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 error = xfs_trans_reserve(tp,
3456 resblks,
3457 XFS_WRITE_LOG_RES(mp),
3458 0,
3459 XFS_TRANS_PERM_LOG_RES,
3460 XFS_WRITE_LOG_COUNT);
3461
3462 /*
3463 * check for running out of space
3464 */
3465 if (error) {
3466 /*
3467 * Free the transaction structure.
3468 */
3469 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3470 xfs_trans_cancel(tp, 0);
3471 break;
3472 }
3473 xfs_ilock(ip, XFS_ILOCK_EXCL);
3474 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003475 ip->i_udquot, ip->i_gdquot, resblks, 0,
3476 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 if (error)
3478 goto error1;
3479
3480 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3481 xfs_trans_ihold(tp, ip);
3482
3483 /*
3484 * issue the bunmapi() call to free the blocks
3485 */
3486 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003487 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003489 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 if (error) {
3491 goto error0;
3492 }
3493
3494 /*
3495 * complete the transaction
3496 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003497 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 if (error) {
3499 goto error0;
3500 }
3501
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003502 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3504 }
3505
3506 out_unlock_iolock:
3507 if (need_iolock)
3508 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3509 return error;
3510
3511 error0:
3512 xfs_bmap_cancel(&free_list);
3513 error1:
3514 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3515 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3516 XFS_ILOCK_EXCL);
3517 return error;
3518}
3519
3520/*
3521 * xfs_change_file_space()
3522 * This routine allocates or frees disk space for the given file.
3523 * The user specified parameters are checked for alignment and size
3524 * limitations.
3525 *
3526 * RETURNS:
3527 * 0 on success
3528 * errno on error
3529 *
3530 */
3531int
3532xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003533 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 int cmd,
3535 xfs_flock64_t *bf,
3536 xfs_off_t offset,
3537 cred_t *credp,
3538 int attr_flags)
3539{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003540 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 int clrprealloc;
3542 int error;
3543 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 int setprealloc;
3545 xfs_off_t startoffset;
3546 xfs_off_t llen;
3547 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003548 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003550 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Christoph Hellwig993386c2007-08-28 16:12:30 +10003552 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return XFS_ERROR(EINVAL);
3554
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 switch (bf->l_whence) {
3556 case 0: /*SEEK_SET*/
3557 break;
3558 case 1: /*SEEK_CUR*/
3559 bf->l_start += offset;
3560 break;
3561 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003562 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 break;
3564 default:
3565 return XFS_ERROR(EINVAL);
3566 }
3567
3568 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3569
3570 if ( (bf->l_start < 0)
3571 || (bf->l_start > XFS_MAXIOFFSET(mp))
3572 || (bf->l_start + llen < 0)
3573 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3574 return XFS_ERROR(EINVAL);
3575
3576 bf->l_whence = 0;
3577
3578 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003579 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
3581 /*
3582 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3583 * file space.
3584 * These calls do NOT zero the data space allocated to the file,
3585 * nor do they change the file size.
3586 *
3587 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3588 * space.
3589 * These calls cause the new file data to be zeroed and the file
3590 * size to be changed.
3591 */
3592 setprealloc = clrprealloc = 0;
3593
3594 switch (cmd) {
3595 case XFS_IOC_RESVSP:
3596 case XFS_IOC_RESVSP64:
3597 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3598 1, attr_flags);
3599 if (error)
3600 return error;
3601 setprealloc = 1;
3602 break;
3603
3604 case XFS_IOC_UNRESVSP:
3605 case XFS_IOC_UNRESVSP64:
3606 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3607 attr_flags)))
3608 return error;
3609 break;
3610
3611 case XFS_IOC_ALLOCSP:
3612 case XFS_IOC_ALLOCSP64:
3613 case XFS_IOC_FREESP:
3614 case XFS_IOC_FREESP64:
3615 if (startoffset > fsize) {
3616 error = xfs_alloc_file_space(ip, fsize,
3617 startoffset - fsize, 0, attr_flags);
3618 if (error)
3619 break;
3620 }
3621
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003622 iattr.ia_valid = ATTR_SIZE;
3623 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003625 error = xfs_setattr(ip, &iattr, attr_flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
3627 if (error)
3628 return error;
3629
3630 clrprealloc = 1;
3631 break;
3632
3633 default:
3634 ASSERT(0);
3635 return XFS_ERROR(EINVAL);
3636 }
3637
3638 /*
3639 * update the inode timestamp, mode, and prealloc flag bits
3640 */
3641 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3642
3643 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3644 0, 0, 0))) {
3645 /* ASSERT(0); */
3646 xfs_trans_cancel(tp, 0);
3647 return error;
3648 }
3649
3650 xfs_ilock(ip, XFS_ILOCK_EXCL);
3651
3652 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3653 xfs_trans_ihold(tp, ip);
3654
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003655 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 ip->i_d.di_mode &= ~S_ISUID;
3657
3658 /*
3659 * Note that we don't have to worry about mandatory
3660 * file locking being disabled here because we only
3661 * clear the S_ISGID bit if the Group execute bit is
3662 * on, but if it was on then mandatory locking wouldn't
3663 * have been enabled.
3664 */
3665 if (ip->i_d.di_mode & S_IXGRP)
3666 ip->i_d.di_mode &= ~S_ISGID;
3667
3668 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3669 }
3670 if (setprealloc)
3671 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3672 else if (clrprealloc)
3673 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3674
3675 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3676 xfs_trans_set_sync(tp);
3677
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003678 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
3680 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3681
3682 return error;
3683}