blob: b792a121b1a72c3b512c0bfb622e6f610802e3e2 [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 Hellwig0f285c82008-07-18 17:13:28 +100086 int mask = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int code;
89 uint lock_flags;
90 uint commit_flags=0;
91 uid_t uid=0, iuid=0;
92 gid_t gid=0, igid=0;
93 int timeflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
95 int file_owner;
Dean Roehrich5fcbab32005-05-05 13:27:19 -070096 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Lachlan McIlroycf441ee2008-02-07 16:42:19 +110098 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000100 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return XFS_ERROR(EROFS);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (XFS_FORCED_SHUTDOWN(mp))
104 return XFS_ERROR(EIO);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 olddquot1 = olddquot2 = NULL;
107 udqp = gdqp = NULL;
108
109 /*
110 * If disk quotas is on, we make sure that the dquots do exist on disk,
111 * before we start any other transactions. Trying to do this later
112 * is messy. We don't care to take a readlock to look at the ids
113 * in inode here, because we can't hold it across the trans_reserve.
114 * If the IDs do change before we take the ilock, we're covered
115 * because the i_*dquot fields will get updated anyway.
116 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000117 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 uint qflags = 0;
119
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000120 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
121 uid = iattr->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 qflags |= XFS_QMOPT_UQUOTA;
123 } else {
124 uid = ip->i_d.di_uid;
125 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000126 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
127 gid = iattr->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 qflags |= XFS_QMOPT_GQUOTA;
129 } else {
130 gid = ip->i_d.di_gid;
131 }
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /*
134 * We take a reference when we initialize udqp and gdqp,
135 * so it is important that we never blindly double trip on
136 * the same variable. See xfs_create() for an example.
137 */
138 ASSERT(udqp == NULL);
139 ASSERT(gdqp == NULL);
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000140 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, ip->i_d.di_projid,
141 qflags, &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100143 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
146 /*
147 * For the other attributes, we acquire the inode lock and
148 * first do an error checking pass.
149 */
150 tp = NULL;
151 lock_flags = XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000152 if (flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700153 need_iolock = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000154 if (!(mask & ATTR_SIZE)) {
155 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 (mp->m_flags & XFS_MOUNT_WSYNC)) {
157 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
158 commit_flags = 0;
159 if ((code = xfs_trans_reserve(tp, 0,
160 XFS_ICHANGE_LOG_RES(mp), 0,
161 0, 0))) {
162 lock_flags = 0;
163 goto error_return;
164 }
165 }
166 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000167 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000168 !(flags & XFS_ATTR_DMI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100170 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000171 iattr->ia_size, 0, dmflags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (code) {
173 lock_flags = 0;
174 goto error_return;
175 }
176 }
177 if (need_iolock)
178 lock_flags |= XFS_IOLOCK_EXCL;
179 }
180
181 xfs_ilock(ip, lock_flags);
182
183 /* boolean: are we the file owner? */
184 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
185
186 /*
187 * Change various properties of a file.
188 * Only the owner or users with CAP_FOWNER
189 * capability may do these things.
190 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000191 if (mask & (ATTR_MODE|ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * CAP_FOWNER overrides the following restrictions:
194 *
195 * The user ID of the calling process must be equal
196 * to the file owner ID, except in cases where the
197 * CAP_FSETID capability is applicable.
198 */
199 if (!file_owner && !capable(CAP_FOWNER)) {
200 code = XFS_ERROR(EPERM);
201 goto error_return;
202 }
203
204 /*
205 * CAP_FSETID overrides the following restrictions:
206 *
207 * The effective user ID of the calling process shall match
208 * the file owner when setting the set-user-ID and
209 * set-group-ID bits on that file.
210 *
211 * The effective group ID or one of the supplementary group
212 * IDs of the calling process shall match the group owner of
213 * the file when setting the set-group-ID bit on that file
214 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000215 if (mask & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 mode_t m = 0;
217
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000218 if ((iattr->ia_mode & S_ISUID) && !file_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 m |= S_ISUID;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000220 if ((iattr->ia_mode & S_ISGID) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 !in_group_p((gid_t)ip->i_d.di_gid))
222 m |= S_ISGID;
223#if 0
224 /* Linux allows this, Irix doesn't. */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000225 if ((iattr->ia_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 m |= S_ISVTX;
227#endif
228 if (m && !capable(CAP_FSETID))
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000229 iattr->ia_mode &= ~m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 }
232
233 /*
234 * Change file ownership. Must be the owner or privileged.
235 * If the system was configured with the "restricted_chown"
236 * option, the owner is not permitted to give away the file,
237 * and can change the group id only to a group of which he
238 * or she is a member.
239 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000240 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /*
242 * These IDs could have changed since we last looked at them.
243 * But, we're assured that if the ownership did change
244 * while we didn't have the inode locked, inode's dquot(s)
245 * would have changed also.
246 */
247 iuid = ip->i_d.di_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 igid = ip->i_d.di_gid;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000249 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
250 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /*
253 * CAP_CHOWN overrides the following restrictions:
254 *
255 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
256 * shall override the restriction that a process cannot
257 * change the user ID of a file it owns and the restriction
258 * that the group ID supplied to the chown() function
259 * shall be equal to either the group ID or one of the
260 * supplementary group IDs of the calling process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
262 if (restricted_chown &&
263 (iuid != uid || (igid != gid &&
264 !in_group_p((gid_t)gid))) &&
265 !capable(CAP_CHOWN)) {
266 code = XFS_ERROR(EPERM);
267 goto error_return;
268 }
269 /*
Christoph Hellwig25fe55e2008-07-18 17:13:20 +1000270 * Do a quota reservation only if uid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * going to change.
272 */
273 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
274 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
275 ASSERT(tp);
276 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
277 capable(CAP_FOWNER) ?
278 XFS_QMOPT_FORCE_RES : 0);
279 if (code) /* out of quota */
280 goto error_return;
281 }
282 }
283
284 /*
285 * Truncate file. Must have write permission and not be a directory.
286 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000287 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* Short circuit the truncate case for zero length files */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000289 if (iattr->ia_size == 0 &&
290 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 xfs_iunlock(ip, XFS_ILOCK_EXCL);
292 lock_flags &= ~XFS_ILOCK_EXCL;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000293 if (mask & ATTR_CTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
295 code = 0;
296 goto error_return;
297 }
298
Christoph Hellwig42173f62008-04-22 17:33:25 +1000299 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 code = XFS_ERROR(EISDIR);
301 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000302 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 code = XFS_ERROR(EINVAL);
304 goto error_return;
305 }
306 /*
307 * Make sure that the dquots are attached to the inode.
308 */
309 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
310 goto error_return;
311 }
312
313 /*
314 * Change file access or modified times.
315 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000316 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!file_owner) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000318 if ((mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 !capable(CAP_FOWNER)) {
320 code = XFS_ERROR(EPERM);
321 goto error_return;
322 }
323 }
324 }
325
326 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * Now we can make the changes. Before we join the inode
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000328 * to the transaction, if ATTR_SIZE is set then take care of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * the part of the truncation that must be done without the
330 * inode lock. This needs to be done before joining the inode
331 * to the transaction, because the inode cannot be unlocked
332 * once it is a part of the transaction.
333 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000334 if (mask & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 code = 0;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000336 if (iattr->ia_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000337 /*
338 * Do the first part of growing a file: zero any data
339 * in the last block that is beyond the old EOF. We
340 * need to do this before the inode is joined to the
341 * transaction to modify the i_size.
342 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000343 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000346
347 /*
348 * We are going to log the inode size change in this
349 * transaction so any previous writes that are beyond the on
350 * disk EOF and the new EOF that have not been written out need
351 * to be written here. If we do not write the data out, we
352 * expose ourselves to the null files problem.
353 *
354 * Only flush from the on disk size to the smaller of the in
355 * memory file size or the new size as that's the range we
356 * really care about here and prevents waiting for other data
357 * not within the range we care about here.
358 */
359 if (!code &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000360 ip->i_size != ip->i_d.di_size &&
361 iattr->ia_size > ip->i_d.di_size) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000362 code = xfs_flush_pages(ip,
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000363 ip->i_d.di_size, iattr->ia_size,
David Chinnerc32676e2007-07-19 16:28:58 +1000364 XFS_B_ASYNC, FI_NONE);
365 }
366
367 /* wait for all I/O to complete */
Christoph Hellwigb677c212007-08-29 11:46:28 +1000368 vn_iowait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (!code)
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000371 code = xfs_itruncate_data(ip, iattr->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (code) {
373 ASSERT(tp == NULL);
374 lock_flags &= ~XFS_ILOCK_EXCL;
375 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
376 goto error_return;
377 }
378 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
379 if ((code = xfs_trans_reserve(tp, 0,
380 XFS_ITRUNCATE_LOG_RES(mp), 0,
381 XFS_TRANS_PERM_LOG_RES,
382 XFS_ITRUNCATE_LOG_COUNT))) {
383 xfs_trans_cancel(tp, 0);
384 if (need_iolock)
385 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
386 return code;
387 }
388 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
389 xfs_ilock(ip, XFS_ILOCK_EXCL);
390 }
391
392 if (tp) {
393 xfs_trans_ijoin(tp, ip, lock_flags);
394 xfs_trans_ihold(tp, ip);
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * Truncate file. Must have write permission and not be a directory.
399 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000400 if (mask & ATTR_SIZE) {
David Chinner44d814c2008-03-06 13:45:29 +1100401 /*
402 * Only change the c/mtime if we are changing the size
403 * or we are explicitly asked to change it. This handles
404 * the semantic difference between truncate() and ftruncate()
405 * as implemented in the VFS.
406 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000407 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
David Chinner44d814c2008-03-06 13:45:29 +1100408 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
409
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000410 if (iattr->ia_size > ip->i_size) {
411 ip->i_d.di_size = iattr->ia_size;
412 ip->i_size = iattr->ia_size;
413 if (!(flags & XFS_ATTR_DMI))
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000414 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
415 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000416 } else if (iattr->ia_size <= ip->i_size ||
417 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
419 * signal a sync transaction unless
420 * we're truncating an already unlinked
421 * file on a wsync filesystem
422 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000423 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 XFS_DATA_FORK,
425 ((ip->i_d.di_nlink != 0 ||
426 !(mp->m_flags & XFS_MOUNT_WSYNC))
427 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000428 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000430 /*
431 * Truncated "down", so we're removing references
432 * to old data here - if we now delay flushing for
433 * a long time, we expose ourselves unduly to the
434 * notorious NULL files problem. So, we mark this
435 * vnode and flush it when the file is closed, and
436 * do not wait the usual (long) time for writeout.
437 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000438 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 /*
443 * Change file access modes.
444 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000445 if (mask & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ip->i_d.di_mode &= S_IFMT;
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000447 ip->i_d.di_mode |= iattr->ia_mode & ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
450 timeflags |= XFS_ICHGTIME_CHG;
451 }
452
453 /*
454 * Change file ownership. Must be the owner or privileged.
455 * If the system was configured with the "restricted_chown"
456 * option, the owner is not permitted to give away the file,
457 * and can change the group id only to a group of which he
458 * or she is a member.
459 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000460 if (mask & (ATTR_UID|ATTR_GID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /*
462 * CAP_FSETID overrides the following restrictions:
463 *
464 * The set-user-ID and set-group-ID bits of a file will be
465 * cleared upon successful return from chown()
466 */
467 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
468 !capable(CAP_FSETID)) {
469 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
470 }
471
472 /*
473 * Change the ownerships and register quota modifications
474 * in the transaction.
475 */
476 if (iuid != uid) {
477 if (XFS_IS_UQUOTA_ON(mp)) {
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000478 ASSERT(mask & ATTR_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 ASSERT(udqp);
480 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
481 &ip->i_udquot, udqp);
482 }
483 ip->i_d.di_uid = uid;
484 }
485 if (igid != gid) {
486 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000487 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000488 ASSERT(mask & ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 ASSERT(gdqp);
490 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
491 &ip->i_gdquot, gdqp);
492 }
493 ip->i_d.di_gid = gid;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
497 timeflags |= XFS_ICHGTIME_CHG;
498 }
499
500
501 /*
502 * Change file access or modified times.
503 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000504 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
505 if (mask & ATTR_ATIME) {
506 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
507 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 ip->i_update_core = 1;
509 timeflags &= ~XFS_ICHGTIME_ACC;
510 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000511 if (mask & ATTR_MTIME) {
512 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
513 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 timeflags &= ~XFS_ICHGTIME_MOD;
515 timeflags |= XFS_ICHGTIME_CHG;
516 }
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000517 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
519 }
520
521 /*
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000522 * Change file inode change time only if ATTR_CTIME set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * AND we have been called by a DMI function.
524 */
525
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000526 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
527 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
528 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ip->i_update_core = 1;
530 timeflags &= ~XFS_ICHGTIME_CHG;
531 }
532
533 /*
534 * Send out timestamp changes that need to be set to the
535 * current time. Not done when called by a DMI function.
536 */
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000537 if (timeflags && !(flags & XFS_ATTR_DMI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 xfs_ichgtime(ip, timeflags);
539
540 XFS_STATS_INC(xs_ig_attrchg);
541
542 /*
543 * If this is a synchronous mount, make sure that the
544 * transaction goes to disk before returning to the user.
545 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000546 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * One for the truncate and one for the timestamps since we
548 * don't want to change the timestamps unless we're sure the
549 * truncate worked. Truncates are less than 1% of the laddis
550 * mix so this probably isn't worth the trouble to optimize.
551 */
552 code = 0;
553 if (tp) {
554 if (mp->m_flags & XFS_MOUNT_WSYNC)
555 xfs_trans_set_sync(tp);
556
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000557 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 xfs_iunlock(ip, lock_flags);
561
562 /*
563 * Release any dquot(s) the inode had kept before chown.
564 */
565 XFS_QM_DQRELE(mp, olddquot1);
566 XFS_QM_DQRELE(mp, olddquot2);
567 XFS_QM_DQRELE(mp, udqp);
568 XFS_QM_DQRELE(mp, gdqp);
569
570 if (code) {
571 return code;
572 }
573
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000574 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +1000575 !(flags & XFS_ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100576 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 NULL, DM_RIGHT_NULL, NULL, NULL,
578 0, 0, AT_DELAY_FLAG(flags));
579 }
580 return 0;
581
582 abort_return:
583 commit_flags |= XFS_TRANS_ABORT;
584 /* FALLTHROUGH */
585 error_return:
586 XFS_QM_DQRELE(mp, udqp);
587 XFS_QM_DQRELE(mp, gdqp);
588 if (tp) {
589 xfs_trans_cancel(tp, commit_flags);
590 }
591 if (lock_flags != 0) {
592 xfs_iunlock(ip, lock_flags);
593 }
594 return code;
595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000598 * The maximum pathlen is 1024 bytes. Since the minimum file system
599 * blocksize is 512 bytes, we can get a max of 2 extents back from
600 * bmapi.
601 */
602#define SYMLINK_MAPS 2
603
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000604STATIC int
605xfs_readlink_bmap(
606 xfs_inode_t *ip,
607 char *link)
608{
609 xfs_mount_t *mp = ip->i_mount;
610 int pathlen = ip->i_d.di_size;
611 int nmaps = SYMLINK_MAPS;
612 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
613 xfs_daddr_t d;
614 int byte_cnt;
615 int n;
616 xfs_buf_t *bp;
617 int error = 0;
618
619 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
620 mval, &nmaps, NULL, NULL);
621 if (error)
622 goto out;
623
624 for (n = 0; n < nmaps; n++) {
625 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
626 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
627
628 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
629 error = XFS_BUF_GETERROR(bp);
630 if (error) {
631 xfs_ioerror_alert("xfs_readlink",
632 ip->i_mount, bp, XFS_BUF_ADDR(bp));
633 xfs_buf_relse(bp);
634 goto out;
635 }
636 if (pathlen < byte_cnt)
637 byte_cnt = pathlen;
638 pathlen -= byte_cnt;
639
640 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
641 xfs_buf_relse(bp);
642 }
643
644 link[ip->i_d.di_size] = '\0';
645 error = 0;
646
647 out:
648 return error;
649}
650
Christoph Hellwig993386c2007-08-28 16:12:30 +1000651int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000653 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000654 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000656 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100660 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (XFS_FORCED_SHUTDOWN(mp))
663 return XFS_ERROR(EIO);
664
665 xfs_ilock(ip, XFS_ILOCK_SHARED);
666
667 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000668 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000670 pathlen = ip->i_d.di_size;
671 if (!pathlen)
672 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000675 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
676 link[pathlen] = '\0';
677 } else {
678 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000681 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return error;
684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/*
687 * xfs_fsync
688 *
David Chinner978b7232008-05-19 16:29:46 +1000689 * This is called to sync the inode and its data out to disk. We need to hold
690 * the I/O lock while flushing the data, and the inode lock while flushing the
691 * inode. The inode lock CANNOT be held while flushing the data, so acquire
692 * after we're done with that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000694int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695xfs_fsync(
David Chinner978b7232008-05-19 16:29:46 +1000696 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 xfs_trans_t *tp;
699 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100700 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100702 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
705 return XFS_ERROR(EIO);
706
David Chinner978b7232008-05-19 16:29:46 +1000707 /* capture size updates in I/O completion before writing the inode. */
708 error = filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
709 if (error)
710 return XFS_ERROR(error);
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +1000711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
David Chinner978b7232008-05-19 16:29:46 +1000713 * We always need to make sure that the required inode state is safe on
714 * disk. The vnode might be clean but we still might need to force the
715 * log because of committed transactions that haven't hit the disk yet.
716 * Likewise, there could be unflushed non-transactional changes to the
717 * inode core that have to go to disk and this requires us to issue
718 * a synchronous transaction to capture these changes correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 *
David Chinner978b7232008-05-19 16:29:46 +1000720 * This code relies on the assumption that if the update_* fields
721 * of the inode are clear and the inode is unpinned then it is clean
722 * and no action is required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
724 xfs_ilock(ip, XFS_ILOCK_SHARED);
725
David Chinner978b7232008-05-19 16:29:46 +1000726 if (!(ip->i_update_size || ip->i_update_core)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /*
David Chinner978b7232008-05-19 16:29:46 +1000728 * Timestamps/size haven't changed since last inode flush or
729 * inode transaction commit. That means either nothing got
730 * written or a transaction committed which caught the updates.
731 * If the latter happened and the transaction hasn't hit the
732 * disk yet, the inode will be still be pinned. If it is,
733 * force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
735
736 xfs_iunlock(ip, XFS_ILOCK_SHARED);
737
738 if (xfs_ipincount(ip)) {
David Chinner978b7232008-05-19 16:29:46 +1000739 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
740 XFS_LOG_FORCE | XFS_LOG_SYNC,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100741 &log_flushed);
742 } else {
743 /*
David Chinner978b7232008-05-19 16:29:46 +1000744 * If the inode is not pinned and nothing has changed
745 * we don't need to flush the cache.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100746 */
747 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 } else {
750 /*
David Chinner978b7232008-05-19 16:29:46 +1000751 * Kick off a transaction to log the inode core to get the
752 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
754 xfs_iunlock(ip, XFS_ILOCK_SHARED);
755 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000756 error = xfs_trans_reserve(tp, 0,
757 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
758 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 xfs_trans_cancel(tp, 0);
760 return error;
761 }
762 xfs_ilock(ip, XFS_ILOCK_EXCL);
763
764 /*
David Chinner978b7232008-05-19 16:29:46 +1000765 * Note - it's possible that we might have pushed ourselves out
766 * of the way during trans_reserve which would flush the inode.
767 * But there's no guarantee that the inode buffer has actually
768 * gone out yet (it's delwri). Plus the buffer could be pinned
769 * anyway if it's part of an inode in another recent
770 * transaction. So we play it safe and fire off the
771 * transaction anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
773 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
774 xfs_trans_ihold(tp, ip);
775 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
David Chinner978b7232008-05-19 16:29:46 +1000776 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000777 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 xfs_iunlock(ip, XFS_ILOCK_EXCL);
780 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100781
782 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
783 /*
784 * If the log write didn't issue an ordered tag we need
785 * to flush the disk cache for the data device now.
786 */
787 if (!log_flushed)
788 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
789
790 /*
791 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000792 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100793 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100794 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100795 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
796 }
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return error;
799}
800
801/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000802 * This is called by xfs_inactive to free any blocks beyond eof
803 * when the link count isn't zero and by xfs_dm_punch_hole() when
804 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000806int
807xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000809 xfs_inode_t *ip,
810 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 xfs_trans_t *tp;
813 int error;
814 xfs_fileoff_t end_fsb;
815 xfs_fileoff_t last_fsb;
816 xfs_filblks_t map_len;
817 int nimaps;
818 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +1000819 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /*
822 * Figure out if there are any blocks beyond the end
823 * of the file. If not, then there is nothing to do.
824 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000825 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
827 map_len = last_fsb - end_fsb;
828 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 nimaps = 1;
832 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000833 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000834 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 xfs_iunlock(ip, XFS_ILOCK_SHARED);
836
837 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100838 (imap.br_startblock != HOLESTARTBLOCK ||
839 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /*
841 * Attach the dquots to the inode up front.
842 */
843 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +0100844 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /*
847 * There are blocks after the end of file.
848 * Free them up now by truncating the file to
849 * its current size.
850 */
851 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
852
853 /*
854 * Do the xfs_itruncate_start() call before
855 * reserving any log space because
856 * itruncate_start will call into the buffer
857 * cache and we can't
858 * do that within a transaction.
859 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000860 if (use_iolock)
861 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000862 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000863 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000864 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +1000865 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +1000866 if (use_iolock)
867 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000868 return error;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 error = xfs_trans_reserve(tp, 0,
872 XFS_ITRUNCATE_LOG_RES(mp),
873 0, XFS_TRANS_PERM_LOG_RES,
874 XFS_ITRUNCATE_LOG_COUNT);
875 if (error) {
876 ASSERT(XFS_FORCED_SHUTDOWN(mp));
877 xfs_trans_cancel(tp, 0);
878 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +0100879 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
882 xfs_ilock(ip, XFS_ILOCK_EXCL);
883 xfs_trans_ijoin(tp, ip,
884 XFS_IOLOCK_EXCL |
885 XFS_ILOCK_EXCL);
886 xfs_trans_ihold(tp, ip);
887
888 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000889 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 XFS_DATA_FORK,
891 0);
892 /*
893 * If we get an error at this point we
894 * simply don't bother truncating the file.
895 */
896 if (error) {
897 xfs_trans_cancel(tp,
898 (XFS_TRANS_RELEASE_LOG_RES |
899 XFS_TRANS_ABORT));
900 } else {
901 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000902 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
David Chinner92dfe8d2007-05-24 15:22:19 +1000904 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
905 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100907 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910/*
911 * Free a symlink that has blocks associated with it.
912 */
913STATIC int
914xfs_inactive_symlink_rmt(
915 xfs_inode_t *ip,
916 xfs_trans_t **tpp)
917{
918 xfs_buf_t *bp;
919 int committed;
920 int done;
921 int error;
922 xfs_fsblock_t first_block;
923 xfs_bmap_free_t free_list;
924 int i;
925 xfs_mount_t *mp;
926 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
927 int nmaps;
928 xfs_trans_t *ntp;
929 int size;
930 xfs_trans_t *tp;
931
932 tp = *tpp;
933 mp = ip->i_mount;
934 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
935 /*
936 * We're freeing a symlink that has some
937 * blocks allocated to it. Free the
938 * blocks here. We know that we've got
939 * either 1 or 2 extents and that we can
940 * free them all in one bunmapi call.
941 */
942 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
943 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
944 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
945 ASSERT(XFS_FORCED_SHUTDOWN(mp));
946 xfs_trans_cancel(tp, 0);
947 *tpp = NULL;
948 return error;
949 }
950 /*
951 * Lock the inode, fix the size, and join it to the transaction.
952 * Hold it so in the normal path, we still have it locked for
953 * the second transaction. In the error paths we need it
954 * held so the cancel won't rele it, see below.
955 */
956 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
957 size = (int)ip->i_d.di_size;
958 ip->i_d.di_size = 0;
959 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
960 xfs_trans_ihold(tp, ip);
961 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
962 /*
963 * Find the block(s) so we can inval and unmap them.
964 */
965 done = 0;
966 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -0800967 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
969 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000970 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto error0;
972 /*
973 * Invalidate the block(s).
974 */
975 for (i = 0; i < nmaps; i++) {
976 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
977 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
978 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
979 xfs_trans_binval(tp, bp);
980 }
981 /*
982 * Unmap the dead block(s) to the free_list.
983 */
984 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000985 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 goto error1;
987 ASSERT(done);
988 /*
989 * Commit the first transaction. This logs the EFI and the inode.
990 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100991 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 goto error1;
993 /*
994 * The transaction must have been committed, since there were
995 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
996 * The new tp has the extent freeing and EFDs.
997 */
998 ASSERT(committed);
999 /*
1000 * The first xact was committed, so add the inode to the new one.
1001 * Mark it dirty so it will be logged and moved forward in the log as
1002 * part of every commit.
1003 */
1004 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1005 xfs_trans_ihold(tp, ip);
1006 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1007 /*
1008 * Get a new, empty transaction to return to our caller.
1009 */
1010 ntp = xfs_trans_dup(tp);
1011 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001012 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * If we get an error on the commit here or on the reserve below,
1014 * we need to unlock the inode since the new transaction doesn't
1015 * have the inode attached.
1016 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001017 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 tp = ntp;
1019 if (error) {
1020 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1021 goto error0;
1022 }
1023 /*
1024 * Remove the memory for extent descriptions (just bookkeeping).
1025 */
1026 if (ip->i_df.if_bytes)
1027 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1028 ASSERT(ip->i_df.if_bytes == 0);
1029 /*
1030 * Put an itruncate log reservation in the new transaction
1031 * for our caller.
1032 */
1033 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1034 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1035 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1036 goto error0;
1037 }
1038 /*
1039 * Return with the inode locked but not joined to the transaction.
1040 */
1041 *tpp = tp;
1042 return 0;
1043
1044 error1:
1045 xfs_bmap_cancel(&free_list);
1046 error0:
1047 /*
1048 * Have to come here with the inode locked and either
1049 * (held and in the transaction) or (not in the transaction).
1050 * If the inode isn't held then cancel would iput it, but
1051 * that's wrong since this is inactive and the vnode ref
1052 * count is 0 already.
1053 * Cancel won't do anything to the inode if held, but it still
1054 * needs to be locked until the cancel is done, if it was
1055 * joined to the transaction.
1056 */
1057 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1058 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1059 *tpp = NULL;
1060 return error;
1061
1062}
1063
1064STATIC int
1065xfs_inactive_symlink_local(
1066 xfs_inode_t *ip,
1067 xfs_trans_t **tpp)
1068{
1069 int error;
1070
1071 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1072 /*
1073 * We're freeing a symlink which fit into
1074 * the inode. Just free the memory used
1075 * to hold the old symlink.
1076 */
1077 error = xfs_trans_reserve(*tpp, 0,
1078 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1079 0, XFS_TRANS_PERM_LOG_RES,
1080 XFS_ITRUNCATE_LOG_COUNT);
1081
1082 if (error) {
1083 xfs_trans_cancel(*tpp, 0);
1084 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001085 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1088
1089 /*
1090 * Zero length symlinks _can_ exist.
1091 */
1092 if (ip->i_df.if_bytes > 0) {
1093 xfs_idata_realloc(ip,
1094 -(ip->i_df.if_bytes),
1095 XFS_DATA_FORK);
1096 ASSERT(ip->i_df.if_bytes == 0);
1097 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101STATIC int
1102xfs_inactive_attrs(
1103 xfs_inode_t *ip,
1104 xfs_trans_t **tpp)
1105{
1106 xfs_trans_t *tp;
1107 int error;
1108 xfs_mount_t *mp;
1109
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001110 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 tp = *tpp;
1112 mp = ip->i_mount;
1113 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001114 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001116 if (error)
1117 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001120 if (error)
1121 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1124 error = xfs_trans_reserve(tp, 0,
1125 XFS_IFREE_LOG_RES(mp),
1126 0, XFS_TRANS_PERM_LOG_RES,
1127 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001128 if (error)
1129 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 xfs_ilock(ip, XFS_ILOCK_EXCL);
1132 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1133 xfs_trans_ihold(tp, ip);
1134 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1135
1136 ASSERT(ip->i_d.di_anextents == 0);
1137
1138 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001139 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001140
1141error_cancel:
1142 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1143 xfs_trans_cancel(tp, 0);
1144error_unlock:
1145 *tpp = NULL;
1146 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1147 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Christoph Hellwig993386c2007-08-28 16:12:30 +10001150int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001152 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001154 bhv_vnode_t *vp = XFS_ITOV(ip);
1155 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int error;
1157
Christoph Hellwig42173f62008-04-22 17:33:25 +10001158 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001162 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return 0;
1164
David Chinner2a82b8b2007-07-11 11:09:12 +10001165 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001166 int truncated;
1167
David Chinner2a82b8b2007-07-11 11:09:12 +10001168 /*
1169 * If we are using filestreams, and we have an unlinked
1170 * file that we are processing the last close on, then nothing
1171 * will be able to reopen and write to this file. Purge this
1172 * inode from the filestreams cache so that it doesn't delay
1173 * teardown of the inode.
1174 */
1175 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1176 xfs_filestream_deassociate(ip);
1177
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001178 /*
1179 * If we previously truncated this file and removed old data
1180 * in the process, we want to initiate "early" writeout on
1181 * the last close. This is an attempt to combat the notorious
1182 * NULL files problem which is particularly noticable from a
1183 * truncate down, buffered (re-)write (delalloc), followed by
1184 * a crash. What we are effectively doing here is
1185 * significantly reducing the time window where we'd otherwise
1186 * be exposed to that problem.
1187 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001188 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001189 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001190 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001191 }
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (ip->i_d.di_nlink != 0) {
1194 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001195 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001196 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001198 (!(ip->i_d.di_flags &
1199 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001200 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1201 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001202 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204 }
1205
1206 return 0;
1207}
1208
1209/*
1210 * xfs_inactive
1211 *
1212 * This is called when the vnode reference count for the vnode
1213 * goes to zero. If the file has been unlinked, then it must
1214 * now be truncated. Also, we clear all of the read-ahead state
1215 * kept for the inode here since the file is now closed.
1216 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001217int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001219 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001221 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +10001222 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 xfs_fsblock_t first_block;
1224 int committed;
1225 xfs_trans_t *tp;
1226 xfs_mount_t *mp;
1227 int error;
1228 int truncate;
1229
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001230 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /*
1233 * If the inode is already free, then there can be nothing
1234 * to clean up here.
1235 */
1236 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1237 ASSERT(ip->i_df.if_real_bytes == 0);
1238 ASSERT(ip->i_df.if_broot_bytes == 0);
1239 return VN_INACTIVE_CACHE;
1240 }
1241
1242 /*
1243 * Only do a truncate if it's a regular file with
1244 * some actual space in it. It's OK to look at the
1245 * inode's fields without the lock because we're the
1246 * only one with a reference to the inode.
1247 */
1248 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001249 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1250 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1252
1253 mp = ip->i_mount;
1254
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001255 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1256 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 error = 0;
1259
1260 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001261 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 goto out;
1263
1264 if (ip->i_d.di_nlink != 0) {
1265 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001266 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001267 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001268 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1269 (!(ip->i_d.di_flags &
1270 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1271 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001272 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1273 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001274 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276 goto out;
1277 }
1278
1279 ASSERT(ip->i_d.di_nlink == 0);
1280
1281 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001282 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1285 if (truncate) {
1286 /*
1287 * Do the xfs_itruncate_start() call before
1288 * reserving any log space because itruncate_start
1289 * will call into the buffer cache and we can't
1290 * do that within a transaction.
1291 */
1292 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1293
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001294 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1295 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001296 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001297 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1298 return VN_INACTIVE_CACHE;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 error = xfs_trans_reserve(tp, 0,
1302 XFS_ITRUNCATE_LOG_RES(mp),
1303 0, XFS_TRANS_PERM_LOG_RES,
1304 XFS_ITRUNCATE_LOG_COUNT);
1305 if (error) {
1306 /* Don't call itruncate_cleanup */
1307 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1308 xfs_trans_cancel(tp, 0);
1309 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001310 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
1313 xfs_ilock(ip, XFS_ILOCK_EXCL);
1314 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1315 xfs_trans_ihold(tp, ip);
1316
1317 /*
1318 * normally, we have to run xfs_itruncate_finish sync.
1319 * But if filesystem is wsync and we're in the inactive
1320 * path, then we know that nlink == 0, and that the
1321 * xaction that made nlink == 0 is permanently committed
1322 * since xfs_remove runs as a synchronous transaction.
1323 */
1324 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1325 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1326
1327 if (error) {
1328 xfs_trans_cancel(tp,
1329 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1330 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001331 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1334
1335 /*
1336 * If we get an error while cleaning up a
1337 * symlink we bail out.
1338 */
1339 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1340 xfs_inactive_symlink_rmt(ip, &tp) :
1341 xfs_inactive_symlink_local(ip, &tp);
1342
1343 if (error) {
1344 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001345 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347
1348 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1349 xfs_trans_ihold(tp, ip);
1350 } else {
1351 error = xfs_trans_reserve(tp, 0,
1352 XFS_IFREE_LOG_RES(mp),
1353 0, XFS_TRANS_PERM_LOG_RES,
1354 XFS_INACTIVE_LOG_COUNT);
1355 if (error) {
1356 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1357 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001358 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360
1361 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1362 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1363 xfs_trans_ihold(tp, ip);
1364 }
1365
1366 /*
1367 * If there are attributes associated with the file
1368 * then blow them away now. The code calls a routine
1369 * that recursively deconstructs the attribute fork.
1370 * We need to just commit the current transaction
1371 * because we can't use it for xfs_attr_inactive().
1372 */
1373 if (ip->i_d.di_anextents > 0) {
1374 error = xfs_inactive_attrs(ip, &tp);
1375 /*
1376 * If we got an error, the transaction is already
1377 * cancelled, and the inode is unlocked. Just get out.
1378 */
1379 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001380 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 } else if (ip->i_afp) {
1382 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1383 }
1384
1385 /*
1386 * Free the inode.
1387 */
1388 XFS_BMAP_INIT(&free_list, &first_block);
1389 error = xfs_ifree(tp, ip, &free_list);
1390 if (error) {
1391 /*
1392 * If we fail to free the inode, shut down. The cancel
1393 * might do that, we need to make sure. Otherwise the
1394 * inode might be lost for a long time or forever.
1395 */
1396 if (!XFS_FORCED_SHUTDOWN(mp)) {
1397 cmn_err(CE_NOTE,
1398 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1399 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001400 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1403 } else {
1404 /*
1405 * Credit the quota account(s). The inode is gone.
1406 */
1407 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1408
1409 /*
David Chinner78e9da72008-04-10 12:24:17 +10001410 * Just ignore errors at this point. There is nothing we can
1411 * do except to try to keep going. Make sure it's not a silent
1412 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
David Chinner78e9da72008-04-10 12:24:17 +10001414 error = xfs_bmap_finish(&tp, &free_list, &committed);
1415 if (error)
1416 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1417 "xfs_bmap_finish() returned error %d", error);
1418 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1419 if (error)
1420 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1421 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423 /*
1424 * Release the dquots held by inode, if any.
1425 */
1426 XFS_QM_DQDETACH(mp, ip);
1427
1428 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1429
1430 out:
1431 return VN_INACTIVE_CACHE;
1432}
1433
Barry Naujok384f3ce2008-05-21 16:58:22 +10001434/*
1435 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1436 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1437 * ci_name->name will point to a the actual name (caller must free) or
1438 * will be set to NULL if an exact match is found.
1439 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001440int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001442 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001443 struct xfs_name *name,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001444 xfs_inode_t **ipp,
1445 struct xfs_name *ci_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001447 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int error;
1449 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001451 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1454 return XFS_ERROR(EIO);
1455
1456 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001457 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001459
1460 if (error)
1461 goto out;
1462
1463 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1464 if (error)
Barry Naujok384f3ce2008-05-21 16:58:22 +10001465 goto out_free_name;
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001466
1467 xfs_itrace_ref(*ipp);
1468 return 0;
1469
Barry Naujok384f3ce2008-05-21 16:58:22 +10001470out_free_name:
1471 if (ci_name)
1472 kmem_free(ci_name->name);
1473out:
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001474 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return error;
1476}
1477
Christoph Hellwig993386c2007-08-28 16:12:30 +10001478int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001480 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001481 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001482 mode_t mode,
1483 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001484 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 cred_t *credp)
1486{
Barry Naujok556b8b12008-04-10 12:22:07 +10001487 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001488 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 xfs_trans_t *tp;
Barry Naujok556b8b12008-04-10 12:22:07 +10001490 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 xfs_bmap_free_t free_list;
1492 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001493 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 int dm_event_sent = 0;
1495 uint cancel_flags;
1496 int committed;
1497 xfs_prid_t prid;
1498 struct xfs_dquot *udqp, *gdqp;
1499 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001501 ASSERT(!*ipp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001502 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001504 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001506 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001507 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001508 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 if (error)
1511 return error;
1512 dm_event_sent = 1;
1513 }
1514
1515 if (XFS_FORCED_SHUTDOWN(mp))
1516 return XFS_ERROR(EIO);
1517
1518 /* Return through std_return after this point. */
1519
1520 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001521 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1522 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 else
1524 prid = (xfs_prid_t)dfltprid;
1525
1526 /*
1527 * Make sure that we have allocated dquot(s) on disk.
1528 */
1529 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001530 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1532 if (error)
1533 goto std_return;
1534
1535 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1538 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001539 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /*
1541 * Initially assume that the file does not exist and
1542 * reserve the resources for that case. If that is not
1543 * the case we'll drop the one we have and get a more
1544 * appropriate transaction later.
1545 */
1546 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1547 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1548 if (error == ENOSPC) {
1549 resblks = 0;
1550 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1551 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1552 }
1553 if (error) {
1554 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 goto error_return;
1556 }
1557
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001558 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001559 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 XFS_BMAP_INIT(&free_list, &first_block);
1562
1563 ASSERT(ip == NULL);
1564
1565 /*
1566 * Reserve disk quota and the inode.
1567 */
1568 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1569 if (error)
1570 goto error_return;
1571
Barry Naujok556b8b12008-04-10 12:22:07 +10001572 error = xfs_dir_canenter(tp, dp, name, resblks);
1573 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001575 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 rdev, credp, prid, resblks > 0,
1577 &ip, &committed);
1578 if (error) {
1579 if (error == ENOSPC)
1580 goto error_return;
1581 goto abort_return;
1582 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001583 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 /*
1586 * At this point, we've gotten a newly allocated inode.
1587 * It is locked (and joined to the transaction).
1588 */
1589
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001590 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001593 * Now we join the directory inode to the transaction. We do not do it
1594 * earlier because xfs_dir_ialloc might commit the previous transaction
1595 * (and release all the locks). An error from here on will result in
1596 * the transaction cancel unlocking dp so don't do it explicitly in the
1597 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001599 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001601 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Barry Naujok556b8b12008-04-10 12:22:07 +10001603 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001604 &first_block, &free_list, resblks ?
1605 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (error) {
1607 ASSERT(error != ENOSPC);
1608 goto abort_return;
1609 }
1610 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1611 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1612
1613 /*
1614 * If this is a synchronous mount, make sure that the
1615 * create transaction goes to disk before returning to
1616 * the user.
1617 */
1618 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1619 xfs_trans_set_sync(tp);
1620 }
1621
1622 dp->i_gen++;
1623
1624 /*
1625 * Attach the dquot(s) to the inodes and modify them incore.
1626 * These ids of the inode couldn't have changed since the new
1627 * inode has been locked ever since it was created.
1628 */
1629 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1630
1631 /*
1632 * xfs_trans_commit normally decrements the vnode ref count
1633 * when it unlocks the inode. Since we want to return the
1634 * vnode to the caller, we bump the vnode ref count now.
1635 */
1636 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001638 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (error) {
1640 xfs_bmap_cancel(&free_list);
1641 goto abort_rele;
1642 }
1643
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001644 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (error) {
1646 IRELE(ip);
1647 tp = NULL;
1648 goto error_return;
1649 }
1650
1651 XFS_QM_DQRELE(mp, udqp);
1652 XFS_QM_DQRELE(mp, gdqp);
1653
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001654 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 /* Fallthrough to std_return with error = 0 */
1657
1658std_return:
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001659 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10001660 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001662 dp, DM_RIGHT_NULL,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001663 *ipp ? ip : NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001664 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001665 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 return error;
1668
1669 abort_return:
1670 cancel_flags |= XFS_TRANS_ABORT;
1671 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Jesper Juhl014c2542006-01-15 02:37:08 +01001673 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (tp != NULL)
1675 xfs_trans_cancel(tp, cancel_flags);
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 XFS_QM_DQRELE(mp, udqp);
1678 XFS_QM_DQRELE(mp, gdqp);
1679
Christoph Hellwig993386c2007-08-28 16:12:30 +10001680 if (unlock_dp_on_error)
1681 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 goto std_return;
1684
1685 abort_rele:
1686 /*
1687 * Wait until after the current transaction is aborted to
1688 * release the inode. This prevents recursive transactions
1689 * and deadlocks from xfs_inactive.
1690 */
1691 cancel_flags |= XFS_TRANS_ABORT;
1692 xfs_trans_cancel(tp, cancel_flags);
1693 IRELE(ip);
1694
1695 XFS_QM_DQRELE(mp, udqp);
1696 XFS_QM_DQRELE(mp, gdqp);
1697
1698 goto std_return;
1699}
1700
1701#ifdef DEBUG
1702/*
1703 * Some counters to see if (and how often) we are hitting some deadlock
1704 * prevention code paths.
1705 */
1706
1707int xfs_rm_locks;
1708int xfs_rm_lock_delays;
1709int xfs_rm_attempts;
1710#endif
1711
1712/*
1713 * The following routine will lock the inodes associated with the
1714 * directory and the named entry in the directory. The locks are
1715 * acquired in increasing inode number.
1716 *
1717 * If the entry is "..", then only the directory is locked. The
1718 * vnode ref count will still include that from the .. entry in
1719 * this case.
1720 *
1721 * There is a deadlock we need to worry about. If the locked directory is
1722 * in the AIL, it might be blocking up the log. The next inode we lock
1723 * could be already locked by another thread waiting for log space (e.g
1724 * a permanent log reservation with a long running transaction (see
1725 * xfs_itruncate_finish)). To solve this, we must check if the directory
1726 * is in the ail and use lock_nowait. If we can't lock, we need to
1727 * drop the inode lock on the directory and try again. xfs_iunlock will
1728 * potentially push the tail if we were holding up the log.
1729 */
1730STATIC int
1731xfs_lock_dir_and_entry(
1732 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 xfs_inode_t *ip) /* inode of entry 'name' */
1734{
1735 int attempts;
1736 xfs_ino_t e_inum;
1737 xfs_inode_t *ips[2];
1738 xfs_log_item_t *lp;
1739
1740#ifdef DEBUG
1741 xfs_rm_locks++;
1742#endif
1743 attempts = 0;
1744
1745again:
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001746 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 e_inum = ip->i_ino;
1749
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001750 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 /*
1753 * We want to lock in increasing inum. Since we've already
1754 * acquired the lock on the directory, we may need to release
1755 * if if the inum of the entry turns out to be less.
1756 */
1757 if (e_inum > dp->i_ino) {
1758 /*
1759 * We are already in the right order, so just
1760 * lock on the inode of the entry.
1761 * We need to use nowait if dp is in the AIL.
1762 */
1763
1764 lp = (xfs_log_item_t *)dp->i_itemp;
1765 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1766 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1767 attempts++;
1768#ifdef DEBUG
1769 xfs_rm_attempts++;
1770#endif
1771
1772 /*
1773 * Unlock dp and try again.
1774 * xfs_iunlock will try to push the tail
1775 * if the inode is in the AIL.
1776 */
1777
1778 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1779
1780 if ((attempts % 5) == 0) {
1781 delay(1); /* Don't just spin the CPU */
1782#ifdef DEBUG
1783 xfs_rm_lock_delays++;
1784#endif
1785 }
1786 goto again;
1787 }
1788 } else {
1789 xfs_ilock(ip, XFS_ILOCK_EXCL);
1790 }
1791 } else if (e_inum < dp->i_ino) {
1792 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1793
1794 ips[0] = ip;
1795 ips[1] = dp;
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001796 xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798 /* else e_inum == dp->i_ino */
1799 /* This can happen if we're asked to lock /x/..
1800 * the entry is "..", which is also the parent directory.
1801 */
1802
1803 return 0;
1804}
1805
1806#ifdef DEBUG
1807int xfs_locked_n;
1808int xfs_small_retries;
1809int xfs_middle_retries;
1810int xfs_lots_retries;
1811int xfs_lock_delays;
1812#endif
1813
1814/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001815 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1816 * a different value
1817 */
1818static inline int
1819xfs_lock_inumorder(int lock_mode, int subclass)
1820{
1821 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001822 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001823 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001824 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001825
1826 return lock_mode;
1827}
1828
1829/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 * The following routine will lock n inodes in exclusive mode.
1831 * We assume the caller calls us with the inodes in i_ino order.
1832 *
1833 * We need to detect deadlock where an inode that we lock
1834 * is in the AIL and we start waiting for another inode that is locked
1835 * by a thread in a long running transaction (such as truncate). This can
1836 * result in deadlock since the long running trans might need to wait
1837 * for the inode we just locked in order to push the tail and free space
1838 * in the log.
1839 */
1840void
1841xfs_lock_inodes(
1842 xfs_inode_t **ips,
1843 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 uint lock_mode)
1845{
1846 int attempts = 0, i, j, try_lock;
1847 xfs_log_item_t *lp;
1848
1849 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1850
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001851 try_lock = 0;
1852 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854again:
1855 for (; i < inodes; i++) {
1856 ASSERT(ips[i]);
1857
1858 if (i && (ips[i] == ips[i-1])) /* Already locked */
1859 continue;
1860
1861 /*
1862 * If try_lock is not set yet, make sure all locked inodes
1863 * are not in the AIL.
1864 * If any are, set try_lock to be used later.
1865 */
1866
1867 if (!try_lock) {
1868 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1869 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1870 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1871 try_lock++;
1872 }
1873 }
1874 }
1875
1876 /*
1877 * If any of the previous locks we have locked is in the AIL,
1878 * we must TRY to get the second and subsequent locks. If
1879 * we can't get any, we must release all we have
1880 * and try again.
1881 */
1882
1883 if (try_lock) {
1884 /* try_lock must be 0 if i is 0. */
1885 /*
1886 * try_lock means we have an inode locked
1887 * that is in the AIL.
1888 */
1889 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001890 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 attempts++;
1892
1893 /*
1894 * Unlock all previous guys and try again.
1895 * xfs_iunlock will try to push the tail
1896 * if the inode is in the AIL.
1897 */
1898
1899 for(j = i - 1; j >= 0; j--) {
1900
1901 /*
1902 * Check to see if we've already
1903 * unlocked this one.
1904 * Not the first one going back,
1905 * and the inode ptr is the same.
1906 */
1907 if ((j != (i - 1)) && ips[j] ==
1908 ips[j+1])
1909 continue;
1910
1911 xfs_iunlock(ips[j], lock_mode);
1912 }
1913
1914 if ((attempts % 5) == 0) {
1915 delay(1); /* Don't just spin the CPU */
1916#ifdef DEBUG
1917 xfs_lock_delays++;
1918#endif
1919 }
1920 i = 0;
1921 try_lock = 0;
1922 goto again;
1923 }
1924 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001925 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927 }
1928
1929#ifdef DEBUG
1930 if (attempts) {
1931 if (attempts < 5) xfs_small_retries++;
1932 else if (attempts < 100) xfs_middle_retries++;
1933 else xfs_lots_retries++;
1934 } else {
1935 xfs_locked_n++;
1936 }
1937#endif
1938}
1939
Christoph Hellwig993386c2007-08-28 16:12:30 +10001940int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001942 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001943 struct xfs_name *name,
1944 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001946 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 xfs_trans_t *tp = NULL;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001948 int is_dir = S_ISDIR(ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 int error = 0;
1950 xfs_bmap_free_t free_list;
1951 xfs_fsblock_t first_block;
1952 int cancel_flags;
1953 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 int link_zero;
1955 uint resblks;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001956 uint log_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001958 xfs_itrace_entry(dp);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001959 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (XFS_FORCED_SHUTDOWN(mp))
1962 return XFS_ERROR(EIO);
1963
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001964 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10001965 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1966 NULL, DM_RIGHT_NULL, name->name, NULL,
1967 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (error)
1969 return error;
1970 }
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 error = XFS_QM_DQATTACH(mp, dp, 0);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001973 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001976 error = XFS_QM_DQATTACH(mp, ip, 0);
1977 if (error)
1978 goto std_return;
1979
1980 if (is_dir) {
1981 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1982 log_count = XFS_DEFAULT_LOG_COUNT;
1983 } else {
1984 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1985 log_count = XFS_REMOVE_LOG_COUNT;
1986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 /*
1990 * We try to get the real space reservation first,
1991 * allowing for directory btree deletion(s) implying
1992 * possible bmap insert(s). If we can't get the space
1993 * reservation then we use 0 instead, and avoid the bmap
1994 * btree insert(s) in the directory code by, if the bmap
1995 * insert tries to happen, instead trimming the LAST
1996 * block from the directory.
1997 */
1998 resblks = XFS_REMOVE_SPACE_RES(mp);
1999 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002000 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (error == ENOSPC) {
2002 resblks = 0;
2003 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002004 XFS_TRANS_PERM_LOG_RES, log_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006 if (error) {
2007 ASSERT(error != ENOSPC);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002008 cancel_flags = 0;
2009 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002012 error = xfs_lock_dir_and_entry(dp, ip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002013 if (error)
2014 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 /*
2017 * At this point, we've gotten both the directory and the entry
2018 * inodes locked.
2019 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002020 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10002022
2023 IHOLD(dp);
2024 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 /*
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002027 * If we're removing a directory perform some additional validation.
2028 */
2029 if (is_dir) {
2030 ASSERT(ip->i_d.di_nlink >= 2);
2031 if (ip->i_d.di_nlink != 2) {
2032 error = XFS_ERROR(ENOTEMPTY);
2033 goto out_trans_cancel;
2034 }
2035 if (!xfs_dir_isempty(ip)) {
2036 error = XFS_ERROR(ENOTEMPTY);
2037 goto out_trans_cancel;
2038 }
2039 }
2040
2041 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2043 */
2044 XFS_BMAP_INIT(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10002045 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10002046 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (error) {
2048 ASSERT(error != ENOENT);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002049 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2052
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002053 /*
2054 * Bump the in memory generation count on the parent
2055 * directory so that other can know that it has changed.
2056 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 dp->i_gen++;
2058 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2059
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002060 if (is_dir) {
2061 /*
2062 * Drop the link from ip's "..".
2063 */
2064 error = xfs_droplink(tp, dp);
2065 if (error)
2066 goto out_bmap_cancel;
2067
2068 /*
2069 * Drop the link from dp to ip.
2070 */
2071 error = xfs_droplink(tp, ip);
2072 if (error)
2073 goto out_bmap_cancel;
2074 } else {
2075 /*
2076 * When removing a non-directory we need to log the parent
2077 * inode here for the i_gen update. For a directory this is
2078 * done implicitly by the xfs_droplink call for the ".." entry.
2079 */
2080 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
2082
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002083 /*
2084 * Drop the "." link from ip to self.
2085 */
2086 error = xfs_droplink(tp, ip);
2087 if (error)
2088 goto out_bmap_cancel;
2089
2090 /*
2091 * Determine if this is the last link while
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 * we are in the transaction.
2093 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002094 link_zero = (ip->i_d.di_nlink == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 * If this is a synchronous mount, make sure that the
2098 * remove transaction goes to disk before returning to
2099 * the user.
2100 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002101 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002104 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002105 if (error)
2106 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002108 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002109 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002113 * If we are using filestreams, kill the stream association.
2114 * If the file is still open it may get a new one but that
2115 * will get killed on last close in xfs_close() so we don't
2116 * have to worry about that.
2117 */
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002118 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
David Chinner2a82b8b2007-07-11 11:09:12 +10002119 xfs_filestream_deassociate(ip);
2120
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002121 xfs_itrace_exit(ip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002122 xfs_itrace_exit(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002125 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002126 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2127 NULL, DM_RIGHT_NULL, name->name, NULL,
2128 ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return error;
2132
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002133 out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 xfs_bmap_cancel(&free_list);
2135 cancel_flags |= XFS_TRANS_ABORT;
Christoph Hellwig8f112e32008-06-23 13:25:17 +10002136 out_trans_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 xfs_trans_cancel(tp, cancel_flags);
2138 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Christoph Hellwig993386c2007-08-28 16:12:30 +10002141int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002143 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002144 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002145 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002147 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 xfs_inode_t *ips[2];
2150 int error;
2151 xfs_bmap_free_t free_list;
2152 xfs_fsblock_t first_block;
2153 int cancel_flags;
2154 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002157 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002158 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002160 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (XFS_FORCED_SHUTDOWN(mp))
2163 return XFS_ERROR(EIO);
2164
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002165 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002167 tdp, DM_RIGHT_NULL,
2168 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002169 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (error)
2171 return error;
2172 }
2173
2174 /* Return through std_return after this point. */
2175
2176 error = XFS_QM_DQATTACH(mp, sip, 0);
2177 if (!error && sip != tdp)
2178 error = XFS_QM_DQATTACH(mp, tdp, 0);
2179 if (error)
2180 goto std_return;
2181
2182 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2183 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002184 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2186 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2187 if (error == ENOSPC) {
2188 resblks = 0;
2189 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2190 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2191 }
2192 if (error) {
2193 cancel_flags = 0;
2194 goto error_return;
2195 }
2196
2197 if (sip->i_ino < tdp->i_ino) {
2198 ips[0] = sip;
2199 ips[1] = tdp;
2200 } else {
2201 ips[0] = tdp;
2202 ips[1] = sip;
2203 }
2204
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10002205 xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 /*
2208 * Increment vnode ref counts since xfs_trans_commit &
2209 * xfs_trans_cancel will both unlock the inodes and
2210 * decrement the associated ref counts.
2211 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002212 IHOLD(sip);
2213 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2215 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2216
2217 /*
2218 * If the source has too many links, we can't make any more to it.
2219 */
2220 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2221 error = XFS_ERROR(EMLINK);
2222 goto error_return;
2223 }
2224
Nathan Scott365ca832005-06-21 15:39:12 +10002225 /*
2226 * If we are using project inheritance, we only allow hard link
2227 * creation in our tree when the project IDs are the same; else
2228 * the tree quota mechanism could be circumvented.
2229 */
2230 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2231 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002232 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002233 goto error_return;
2234 }
2235
Barry Naujok556b8b12008-04-10 12:22:07 +10002236 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2237 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 goto error_return;
2239
2240 XFS_BMAP_INIT(&free_list, &first_block);
2241
Barry Naujok556b8b12008-04-10 12:22:07 +10002242 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2243 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (error)
2245 goto abort_return;
2246 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2247 tdp->i_gen++;
2248 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2249
2250 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002251 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 /*
2255 * If this is a synchronous mount, make sure that the
2256 * link transaction goes to disk before returning to
2257 * the user.
2258 */
2259 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2260 xfs_trans_set_sync(tp);
2261 }
2262
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002263 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (error) {
2265 xfs_bmap_cancel(&free_list);
2266 goto abort_return;
2267 }
2268
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002269 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002270 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /* Fall through to std_return with error = 0. */
2274std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002275 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002277 tdp, DM_RIGHT_NULL,
2278 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002279 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281 return error;
2282
2283 abort_return:
2284 cancel_flags |= XFS_TRANS_ABORT;
2285 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 error_return:
2288 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 goto std_return;
2290}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002291
2292
Christoph Hellwig993386c2007-08-28 16:12:30 +10002293int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002295 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002296 struct xfs_name *dir_name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002297 mode_t mode,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002298 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 cred_t *credp)
2300{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002301 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 xfs_inode_t *cdp; /* inode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 int cancel_flags;
2305 int error;
2306 int committed;
2307 xfs_bmap_free_t free_list;
2308 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002309 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 boolean_t created = B_FALSE;
2311 int dm_event_sent = 0;
2312 xfs_prid_t prid;
2313 struct xfs_dquot *udqp, *gdqp;
2314 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 if (XFS_FORCED_SHUTDOWN(mp))
2317 return XFS_ERROR(EIO);
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002321 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002323 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002324 DM_RIGHT_NULL, dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002325 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (error)
2327 return error;
2328 dm_event_sent = 1;
2329 }
2330
2331 /* Return through std_return after this point. */
2332
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002333 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 mp = dp->i_mount;
2336 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002337 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2338 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 else
2340 prid = (xfs_prid_t)dfltprid;
2341
2342 /*
2343 * Make sure that we have allocated dquot(s) on disk.
2344 */
2345 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002346 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2348 if (error)
2349 goto std_return;
2350
2351 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2352 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002353 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2355 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2356 if (error == ENOSPC) {
2357 resblks = 0;
2358 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2359 XFS_TRANS_PERM_LOG_RES,
2360 XFS_MKDIR_LOG_COUNT);
2361 }
2362 if (error) {
2363 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 goto error_return;
2365 }
2366
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002367 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002368 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 /*
2371 * Check for directory link count overflow.
2372 */
2373 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2374 error = XFS_ERROR(EMLINK);
2375 goto error_return;
2376 }
2377
2378 /*
2379 * Reserve disk quota and the inode.
2380 */
2381 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2382 if (error)
2383 goto error_return;
2384
Barry Naujok556b8b12008-04-10 12:22:07 +10002385 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2386 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 goto error_return;
2388 /*
2389 * create the directory inode.
2390 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002391 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 0, credp, prid, resblks > 0,
2393 &cdp, NULL);
2394 if (error) {
2395 if (error == ENOSPC)
2396 goto error_return;
2397 goto abort_return;
2398 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002399 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 /*
2402 * Now we add the directory inode to the transaction.
2403 * We waited until now since xfs_dir_ialloc might start
2404 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002405 * earlier, the locks might have gotten released. An error
2406 * from here on will result in the transaction cancel
2407 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002409 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002411 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 XFS_BMAP_INIT(&free_list, &first_block);
2414
Barry Naujok556b8b12008-04-10 12:22:07 +10002415 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2416 &first_block, &free_list, resblks ?
2417 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if (error) {
2419 ASSERT(error != ENOSPC);
2420 goto error1;
2421 }
2422 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2423
2424 /*
2425 * Bump the in memory version number of the parent directory
2426 * so that other processes accessing it will recognize that
2427 * the directory has changed.
2428 */
2429 dp->i_gen++;
2430
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002431 error = xfs_dir_init(tp, cdp, dp);
2432 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 cdp->i_gen = 1;
2436 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002437 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 created = B_TRUE;
2441
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002442 *ipp = cdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 IHOLD(cdp);
2444
2445 /*
2446 * Attach the dquots to the new inode and modify the icount incore.
2447 */
2448 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2449
2450 /*
2451 * If this is a synchronous mount, make sure that the
2452 * mkdir transaction goes to disk before returning to
2453 * the user.
2454 */
2455 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2456 xfs_trans_set_sync(tp);
2457 }
2458
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002459 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if (error) {
2461 IRELE(cdp);
2462 goto error2;
2463 }
2464
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002465 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 XFS_QM_DQRELE(mp, udqp);
2467 XFS_QM_DQRELE(mp, gdqp);
2468 if (error) {
2469 IRELE(cdp);
2470 }
2471
2472 /* Fall through to std_return with error = 0 or errno from
2473 * xfs_trans_commit. */
2474
2475std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002476 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002477 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002479 dp, DM_RIGHT_NULL,
2480 created ? cdp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002482 dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002483 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
2485 return error;
2486
2487 error2:
2488 error1:
2489 xfs_bmap_cancel(&free_list);
2490 abort_return:
2491 cancel_flags |= XFS_TRANS_ABORT;
2492 error_return:
2493 xfs_trans_cancel(tp, cancel_flags);
2494 XFS_QM_DQRELE(mp, udqp);
2495 XFS_QM_DQRELE(mp, gdqp);
2496
Christoph Hellwig993386c2007-08-28 16:12:30 +10002497 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 goto std_return;
2501}
2502
Christoph Hellwig993386c2007-08-28 16:12:30 +10002503int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002505 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002506 struct xfs_name *link_name,
2507 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002508 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002509 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 cred_t *credp)
2511{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002512 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 xfs_inode_t *ip;
2515 int error;
2516 int pathlen;
2517 xfs_bmap_free_t free_list;
2518 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002519 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 uint cancel_flags;
2521 int committed;
2522 xfs_fileoff_t first_fsb;
2523 xfs_filblks_t fs_blocks;
2524 int nmaps;
2525 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2526 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002527 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 int byte_cnt;
2529 int n;
2530 xfs_buf_t *bp;
2531 xfs_prid_t prid;
2532 struct xfs_dquot *udqp, *gdqp;
2533 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Christoph Hellwig3937be52008-03-06 13:46:19 +11002535 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 error = 0;
2537 ip = NULL;
2538 tp = NULL;
2539
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002540 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 if (XFS_FORCED_SHUTDOWN(mp))
2543 return XFS_ERROR(EIO);
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 /*
2546 * Check component lengths of the target path name.
2547 */
2548 pathlen = strlen(target_path);
2549 if (pathlen >= MAXPATHLEN) /* total string too long */
2550 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002552 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002553 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002555 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (error)
2557 return error;
2558 }
2559
2560 /* Return through std_return after this point. */
2561
2562 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002563 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2564 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 else
2566 prid = (xfs_prid_t)dfltprid;
2567
2568 /*
2569 * Make sure that we have allocated dquot(s) on disk.
2570 */
2571 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002572 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2574 if (error)
2575 goto std_return;
2576
2577 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2578 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2579 /*
2580 * The symlink will fit into the inode data fork?
2581 * There can't be any attributes so we get the whole variable part.
2582 */
2583 if (pathlen <= XFS_LITINO(mp))
2584 fs_blocks = 0;
2585 else
2586 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002587 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2589 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2590 if (error == ENOSPC && fs_blocks == 0) {
2591 resblks = 0;
2592 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2593 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2594 }
2595 if (error) {
2596 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 goto error_return;
2598 }
2599
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002600 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002601 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 /*
2604 * Check whether the directory allows new symlinks or not.
2605 */
2606 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2607 error = XFS_ERROR(EPERM);
2608 goto error_return;
2609 }
2610
2611 /*
2612 * Reserve disk quota : blocks and inode.
2613 */
2614 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2615 if (error)
2616 goto error_return;
2617
2618 /*
2619 * Check for ability to enter directory entry, if no space reserved.
2620 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002621 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2622 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 goto error_return;
2624 /*
2625 * Initialize the bmap freelist prior to calling either
2626 * bmapi or the directory create code.
2627 */
2628 XFS_BMAP_INIT(&free_list, &first_block);
2629
2630 /*
2631 * Allocate an inode for the symlink.
2632 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002633 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 1, 0, credp, prid, resblks > 0, &ip, NULL);
2635 if (error) {
2636 if (error == ENOSPC)
2637 goto error_return;
2638 goto error1;
2639 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002640 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Christoph Hellwig993386c2007-08-28 16:12:30 +10002642 /*
2643 * An error after we've joined dp to the transaction will result in the
2644 * transaction cancel unlocking dp so don't do it explicitly in the
2645 * error path.
2646 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002647 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002649 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 /*
2652 * Also attach the dquot(s) to it, if applicable.
2653 */
2654 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2655
2656 if (resblks)
2657 resblks -= XFS_IALLOC_SPACE_RES(mp);
2658 /*
2659 * If the symlink will fit into the inode, write it inline.
2660 */
2661 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2662 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2663 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2664 ip->i_d.di_size = pathlen;
2665
2666 /*
2667 * The inode was initially created in extent format.
2668 */
2669 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2670 ip->i_df.if_flags |= XFS_IFINLINE;
2671
2672 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2673 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2674
2675 } else {
2676 first_fsb = 0;
2677 nmaps = SYMLINK_MAPS;
2678
2679 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2680 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2681 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002682 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (error) {
2684 goto error1;
2685 }
2686
2687 if (resblks)
2688 resblks -= fs_blocks;
2689 ip->i_d.di_size = pathlen;
2690 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2691
2692 cur_chunk = target_path;
2693 for (n = 0; n < nmaps; n++) {
2694 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2695 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2696 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2697 BTOBB(byte_cnt), 0);
2698 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2699 if (pathlen < byte_cnt) {
2700 byte_cnt = pathlen;
2701 }
2702 pathlen -= byte_cnt;
2703
2704 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2705 cur_chunk += byte_cnt;
2706
2707 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2708 }
2709 }
2710
2711 /*
2712 * Create the directory entry for the symlink.
2713 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002714 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2715 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002716 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2719 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2720
2721 /*
2722 * Bump the in memory version number of the parent directory
2723 * so that other processes accessing it will recognize that
2724 * the directory has changed.
2725 */
2726 dp->i_gen++;
2727
2728 /*
2729 * If this is a synchronous mount, make sure that the
2730 * symlink transaction goes to disk before returning to
2731 * the user.
2732 */
2733 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2734 xfs_trans_set_sync(tp);
2735 }
2736
2737 /*
2738 * xfs_trans_commit normally decrements the vnode ref count
2739 * when it unlocks the inode. Since we want to return the
2740 * vnode to the caller, we bump the vnode ref count now.
2741 */
2742 IHOLD(ip);
2743
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002744 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (error) {
2746 goto error2;
2747 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002748 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 XFS_QM_DQRELE(mp, udqp);
2750 XFS_QM_DQRELE(mp, gdqp);
2751
2752 /* Fall through to std_return with error = 0 or errno from
2753 * xfs_trans_commit */
2754std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10002755 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002757 dp, DM_RIGHT_NULL,
2758 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002759 DM_RIGHT_NULL, link_name->name,
2760 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
2762
Christoph Hellwig3937be52008-03-06 13:46:19 +11002763 if (!error)
2764 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return error;
2766
2767 error2:
2768 IRELE(ip);
2769 error1:
2770 xfs_bmap_cancel(&free_list);
2771 cancel_flags |= XFS_TRANS_ABORT;
2772 error_return:
2773 xfs_trans_cancel(tp, cancel_flags);
2774 XFS_QM_DQRELE(mp, udqp);
2775 XFS_QM_DQRELE(mp, gdqp);
2776
Christoph Hellwig993386c2007-08-28 16:12:30 +10002777 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 goto std_return;
2781}
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002785 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 int flags)
2787{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002788 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 int error = 0;
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (XFS_FORCED_SHUTDOWN(mp))
2792 return XFS_ERROR(EIO);
2793
2794 /*
2795 * Bypass inodes which have already been cleaned by
2796 * the inode flush clustering code inside xfs_iflush
2797 */
David Chinner33540402008-03-06 13:43:59 +11002798 if (xfs_inode_clean(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return 0;
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 /*
2802 * We make this non-blocking if the inode is contended,
2803 * return EAGAIN to indicate to the caller that they
2804 * did not succeed. This prevents the flush path from
2805 * blocking on inodes inside another operation right
2806 * now, they get caught later by xfs_sync.
2807 */
David Chinnera3f74ff2008-03-06 13:43:42 +11002808 if (flags & FLUSH_SYNC) {
2809 xfs_ilock(ip, XFS_ILOCK_SHARED);
2810 xfs_iflock(ip);
2811 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
2812 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
2813 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return EAGAIN;
2815 }
David Chinnera3f74ff2008-03-06 13:43:42 +11002816 } else {
2817 return EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
2819
David Chinnera3f74ff2008-03-06 13:43:42 +11002820 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
2821 : XFS_IFLUSH_ASYNC_NOBLOCK);
2822 xfs_iunlock(ip, XFS_ILOCK_SHARED);
2823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return error;
2825}
2826
Christoph Hellwig993386c2007-08-28 16:12:30 +10002827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828int
Christoph Hellwig993386c2007-08-28 16:12:30 +10002829xfs_set_dmattrs(
2830 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002832 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002834 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 int error;
2837
2838 if (!capable(CAP_SYS_ADMIN))
2839 return XFS_ERROR(EPERM);
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (XFS_FORCED_SHUTDOWN(mp))
2842 return XFS_ERROR(EIO);
2843
2844 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2845 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2846 if (error) {
2847 xfs_trans_cancel(tp, 0);
2848 return error;
2849 }
2850 xfs_ilock(ip, XFS_ILOCK_EXCL);
2851 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2852
Christoph Hellwig613d7042007-10-11 17:44:08 +10002853 ip->i_d.di_dmevmask = evmask;
2854 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2857 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002858 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 return error;
2861}
2862
Christoph Hellwig993386c2007-08-28 16:12:30 +10002863int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002865 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002867 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002869 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871 ASSERT(!VN_MAPPED(vp));
2872
2873 /* bad inode, get out here ASAP */
2874 if (VN_BAD(vp)) {
2875 xfs_ireclaim(ip);
2876 return 0;
2877 }
2878
Christoph Hellwigb677c212007-08-29 11:46:28 +10002879 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10002881 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11002883 /*
2884 * Make sure the atime in the XFS inode is correct before freeing the
2885 * Linux inode.
2886 */
2887 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
David Chinner4c606582006-11-11 18:05:00 +11002889 /*
2890 * If we have nothing to flush with this inode then complete the
2891 * teardown now, otherwise break the link between the xfs inode and the
2892 * linux inode and clean up the xfs inode later. This avoids flushing
2893 * the inode to disk during the delete operation itself.
2894 *
2895 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2896 * first to ensure that xfs_iunpin() will never see an xfs inode
2897 * that has a linux inode being reclaimed. Synchronisation is provided
2898 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 */
2900 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2901 xfs_ilock(ip, XFS_ILOCK_EXCL);
2902 xfs_iflock(ip);
2903 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
2904 } else {
2905 xfs_mount_t *mp = ip->i_mount;
2906
David Chinner4c606582006-11-11 18:05:00 +11002907 /* Protect sync and unpin from us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 XFS_MOUNT_ILOCK(mp);
David Chinner4c606582006-11-11 18:05:00 +11002909 spin_lock(&ip->i_flags_lock);
2910 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10002911 vn_to_inode(vp)->i_private = NULL;
2912 ip->i_vnode = NULL;
David Chinner4c606582006-11-11 18:05:00 +11002913 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 XFS_MOUNT_IUNLOCK(mp);
2916 }
2917 return 0;
2918}
2919
2920int
2921xfs_finish_reclaim(
2922 xfs_inode_t *ip,
2923 int locked,
2924 int sync_mode)
2925{
David Chinnerda353b02007-08-28 14:00:13 +10002926 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
Nathan Scott67fcaa72006-06-09 17:00:52 +10002927 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 if (vp && VN_BAD(vp))
2930 goto reclaim;
2931
2932 /* The hash lock here protects a thread in xfs_iget_core from
2933 * racing with us on linking the inode back with a vnode.
2934 * Once we have the XFS_IRECLAIM flag set it will not touch
2935 * us.
2936 */
David Chinnerda353b02007-08-28 14:00:13 +10002937 write_lock(&pag->pag_ici_lock);
David Chinnerf273ab82006-09-28 11:06:03 +10002938 spin_lock(&ip->i_flags_lock);
David Chinner7a18c382006-11-11 18:04:54 +11002939 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
2940 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
David Chinnerf273ab82006-09-28 11:06:03 +10002941 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10002942 write_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if (locked) {
2944 xfs_ifunlock(ip);
2945 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2946 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002947 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 }
David Chinner7a18c382006-11-11 18:04:54 +11002949 __xfs_iflags_set(ip, XFS_IRECLAIM);
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);
2952 xfs_put_perag(ip->i_mount, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954 /*
2955 * If the inode is still dirty, then flush it out. If the inode
2956 * is not in the AIL, then it will be OK to flush it delwri as
2957 * long as xfs_iflush() does not keep any references to the inode.
2958 * We leave that decision up to xfs_iflush() since it has the
2959 * knowledge of whether it's OK to simply do a delwri flush of
2960 * the inode or whether we need to wait until the inode is
2961 * pulled from the AIL.
2962 * We get the flush lock regardless, though, just to make sure
2963 * we don't free it while it is being flushed.
2964 */
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11002965 if (!locked) {
2966 xfs_ilock(ip, XFS_ILOCK_EXCL);
2967 xfs_iflock(ip);
2968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Lachlan McIlroy7f871d52008-06-23 13:23:57 +10002970 /*
2971 * In the case of a forced shutdown we rely on xfs_iflush() to
2972 * wait for the inode to be unpinned before returning an error.
2973 */
2974 if (xfs_iflush(ip, sync_mode) == 0) {
2975 /* synchronize with xfs_iflush_done */
2976 xfs_iflock(ip);
2977 xfs_ifunlock(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
2979
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11002980 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 reclaim:
2983 xfs_ireclaim(ip);
2984 return 0;
2985}
2986
2987int
2988xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
2989{
2990 int purged;
2991 xfs_inode_t *ip, *n;
2992 int done = 0;
2993
2994 while (!done) {
2995 purged = 0;
2996 XFS_MOUNT_ILOCK(mp);
2997 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
2998 if (noblock) {
2999 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3000 continue;
3001 if (xfs_ipincount(ip) ||
3002 !xfs_iflock_nowait(ip)) {
3003 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3004 continue;
3005 }
3006 }
3007 XFS_MOUNT_IUNLOCK(mp);
Felix Blyakher6b2cf612005-11-25 16:42:13 +11003008 if (xfs_finish_reclaim(ip, noblock,
3009 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3010 delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 purged = 1;
3012 break;
3013 }
3014
3015 done = !purged;
3016 }
3017
3018 XFS_MOUNT_IUNLOCK(mp);
3019 return 0;
3020}
3021
3022/*
3023 * xfs_alloc_file_space()
3024 * This routine allocates disk space for the given file.
3025 *
3026 * If alloc_type == 0, this request is for an ALLOCSP type
3027 * request which will change the file size. In this case, no
3028 * DMAPI event will be generated by the call. A TRUNCATE event
3029 * will be generated later by xfs_setattr.
3030 *
3031 * If alloc_type != 0, this request is for a RESVSP type
3032 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3033 * lower block boundary byte address is less than the file's
3034 * length.
3035 *
3036 * RETURNS:
3037 * 0 on success
3038 * errno on error
3039 *
3040 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003041STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042xfs_alloc_file_space(
3043 xfs_inode_t *ip,
3044 xfs_off_t offset,
3045 xfs_off_t len,
3046 int alloc_type,
3047 int attr_flags)
3048{
Nathan Scottdd9f4382006-01-11 15:28:28 +11003049 xfs_mount_t *mp = ip->i_mount;
3050 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 xfs_filblks_t allocated_fsb;
3052 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003053 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003055 xfs_fsblock_t firstfsb;
3056 int nimaps;
3057 int bmapi_flag;
3058 int quota_flag;
3059 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003061 xfs_bmbt_irec_t imaps[1], *imapp;
3062 xfs_bmap_free_t free_list;
3063 uint qblocks, resblks, resrtextents;
3064 int committed;
3065 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003067 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 if (XFS_FORCED_SHUTDOWN(mp))
3070 return XFS_ERROR(EIO);
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3073 return error;
3074
3075 if (len <= 0)
3076 return XFS_ERROR(EINVAL);
3077
David Chinner957d0eb2007-06-18 16:50:37 +10003078 rt = XFS_IS_REALTIME_INODE(ip);
3079 extsz = xfs_get_extsz_hint(ip);
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11003083 nimaps = 1;
3084 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3086 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3087
3088 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003089 if (alloc_type != 0 && offset < ip->i_size &&
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003090 (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003091 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 xfs_off_t end_dmi_offset;
3093
3094 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003095 if (end_dmi_offset > ip->i_size)
3096 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003097 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
3098 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003100 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 }
3102
3103 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003104 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 */
3106retry:
3107 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003108 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Nathan Scottdd9f4382006-01-11 15:28:28 +11003110 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003111 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11003112 */
3113 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003115 do_div(s, extsz);
3116 s *= extsz;
3117 e = startoffset_fsb + allocatesize_fsb;
3118 if ((temp = do_mod(startoffset_fsb, extsz)))
3119 e += temp;
3120 if ((temp = do_mod(e, extsz)))
3121 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003123 s = 0;
3124 e = allocatesize_fsb;
3125 }
3126
3127 if (unlikely(rt)) {
3128 resrtextents = qblocks = (uint)(e - s);
3129 resrtextents /= mp->m_sb.sb_rextsize;
3130 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3131 quota_flag = XFS_QMOPT_RES_RTBLKS;
3132 } else {
3133 resrtextents = 0;
3134 resblks = qblocks = \
3135 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3136 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
3138
3139 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003140 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 */
3142 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003143 error = xfs_trans_reserve(tp, resblks,
3144 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 XFS_TRANS_PERM_LOG_RES,
3146 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003148 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 */
3150 if (error) {
3151 /*
3152 * Free the transaction structure.
3153 */
3154 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3155 xfs_trans_cancel(tp, 0);
3156 break;
3157 }
3158 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003159 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3160 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (error)
3162 goto error1;
3163
3164 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3165 xfs_trans_ihold(tp, ip);
3166
3167 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003168 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 */
3170 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003171 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003172 allocatesize_fsb, bmapi_flag,
3173 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003174 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 if (error) {
3176 goto error0;
3177 }
3178
3179 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003180 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003182 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (error) {
3184 goto error0;
3185 }
3186
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003187 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3189 if (error) {
3190 break;
3191 }
3192
3193 allocated_fsb = imapp->br_blockcount;
3194
Nathan Scottdd9f4382006-01-11 15:28:28 +11003195 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 error = XFS_ERROR(ENOSPC);
3197 break;
3198 }
3199
3200 startoffset_fsb += allocated_fsb;
3201 allocatesize_fsb -= allocated_fsb;
3202 }
3203dmapi_enospc_check:
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003204 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003205 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003207 ip, DM_RIGHT_NULL,
3208 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3210 if (error == 0)
3211 goto retry; /* Maybe DMAPI app. has made space */
3212 /* else fall through with error from XFS_SEND_DATA */
3213 }
3214
3215 return error;
3216
Nathan Scottdd9f4382006-01-11 15:28:28 +11003217error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003219 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3220
3221error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3223 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3224 goto dmapi_enospc_check;
3225}
3226
3227/*
3228 * Zero file bytes between startoff and endoff inclusive.
3229 * The iolock is held exclusive and no blocks are buffered.
3230 */
3231STATIC int
3232xfs_zero_remaining_bytes(
3233 xfs_inode_t *ip,
3234 xfs_off_t startoff,
3235 xfs_off_t endoff)
3236{
3237 xfs_bmbt_irec_t imap;
3238 xfs_fileoff_t offset_fsb;
3239 xfs_off_t lastoffset;
3240 xfs_off_t offset;
3241 xfs_buf_t *bp;
3242 xfs_mount_t *mp = ip->i_mount;
3243 int nimap;
3244 int error = 0;
3245
3246 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003247 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 mp->m_rtdev_targp : mp->m_ddev_targp);
3249
3250 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3251 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3252 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003253 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003254 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if (error || nimap < 1)
3256 break;
3257 ASSERT(imap.br_blockcount >= 1);
3258 ASSERT(imap.br_startoff == offset_fsb);
3259 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3260 if (lastoffset > endoff)
3261 lastoffset = endoff;
3262 if (imap.br_startblock == HOLESTARTBLOCK)
3263 continue;
3264 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3265 if (imap.br_state == XFS_EXT_UNWRITTEN)
3266 continue;
3267 XFS_BUF_UNDONE(bp);
3268 XFS_BUF_UNWRITE(bp);
3269 XFS_BUF_READ(bp);
3270 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3271 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003272 error = xfs_iowait(bp);
3273 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3275 mp, bp, XFS_BUF_ADDR(bp));
3276 break;
3277 }
3278 memset(XFS_BUF_PTR(bp) +
3279 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3280 0, lastoffset - offset + 1);
3281 XFS_BUF_UNDONE(bp);
3282 XFS_BUF_UNREAD(bp);
3283 XFS_BUF_WRITE(bp);
3284 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003285 error = xfs_iowait(bp);
3286 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3288 mp, bp, XFS_BUF_ADDR(bp));
3289 break;
3290 }
3291 }
3292 xfs_buf_free(bp);
3293 return error;
3294}
3295
3296/*
3297 * xfs_free_file_space()
3298 * This routine frees disk space for the given file.
3299 *
3300 * This routine is only called by xfs_change_file_space
3301 * for an UNRESVSP type call.
3302 *
3303 * RETURNS:
3304 * 0 on success
3305 * errno on error
3306 *
3307 */
3308STATIC int
3309xfs_free_file_space(
3310 xfs_inode_t *ip,
3311 xfs_off_t offset,
3312 xfs_off_t len,
3313 int attr_flags)
3314{
Nathan Scott67fcaa72006-06-09 17:00:52 +10003315 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 int committed;
3317 int done;
3318 xfs_off_t end_dmi_offset;
3319 xfs_fileoff_t endoffset_fsb;
3320 int error;
3321 xfs_fsblock_t firstfsb;
3322 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 xfs_bmbt_irec_t imap;
3324 xfs_off_t ioffset;
3325 xfs_extlen_t mod=0;
3326 xfs_mount_t *mp;
3327 int nimap;
3328 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10003329 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 int rt;
3331 xfs_fileoff_t startoffset_fsb;
3332 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003333 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003335 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 mp = ip->i_mount;
3337
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003338 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3341 return error;
3342
3343 error = 0;
3344 if (len <= 0) /* if nothing being freed */
3345 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003346 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3348 end_dmi_offset = offset + len;
3349 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3350
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003351 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003352 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003353 if (end_dmi_offset > ip->i_size)
3354 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003355 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 offset, end_dmi_offset - offset,
3357 AT_DELAY_FLAG(attr_flags), NULL);
3358 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003359 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
3361
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003362 if (attr_flags & XFS_ATTR_NOLOCK)
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003363 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11003364 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10003366 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11003367 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003368
Tim Shimmine6a4b372007-11-23 16:30:42 +11003369 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003371
3372 if (VN_CACHED(vp) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003373 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3374 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10003375 if (error)
3376 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003377 }
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /*
3380 * Need to zero the stuff we're not freeing, on disk.
3381 * If its a realtime file & can't use unwritten extents then we
3382 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3383 * will take care of it for us.
3384 */
Eric Sandeen62118702008-03-06 13:44:28 +11003385 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003387 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003388 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (error)
3390 goto out_unlock_iolock;
3391 ASSERT(nimap == 0 || nimap == 1);
3392 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3393 xfs_daddr_t block;
3394
3395 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3396 block = imap.br_startblock;
3397 mod = do_div(block, mp->m_sb.sb_rextsize);
3398 if (mod)
3399 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3400 }
3401 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003402 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003403 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 if (error)
3405 goto out_unlock_iolock;
3406 ASSERT(nimap == 0 || nimap == 1);
3407 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3408 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3409 mod++;
3410 if (mod && (mod != mp->m_sb.sb_rextsize))
3411 endoffset_fsb -= mod;
3412 }
3413 }
3414 if ((done = (endoffset_fsb <= startoffset_fsb)))
3415 /*
3416 * One contiguous piece to clear
3417 */
3418 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3419 else {
3420 /*
3421 * Some full blocks, possibly two pieces to clear
3422 */
3423 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3424 error = xfs_zero_remaining_bytes(ip, offset,
3425 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3426 if (!error &&
3427 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3428 error = xfs_zero_remaining_bytes(ip,
3429 XFS_FSB_TO_B(mp, endoffset_fsb),
3430 offset + len - 1);
3431 }
3432
3433 /*
3434 * free file space until done or until there is an error
3435 */
3436 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3437 while (!error && !done) {
3438
3439 /*
David Chinner91ebecc2007-07-19 16:28:30 +10003440 * allocate and setup the transaction. Allow this
3441 * transaction to dip into the reserve blocks to ensure
3442 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 */
3444 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10003445 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 error = xfs_trans_reserve(tp,
3447 resblks,
3448 XFS_WRITE_LOG_RES(mp),
3449 0,
3450 XFS_TRANS_PERM_LOG_RES,
3451 XFS_WRITE_LOG_COUNT);
3452
3453 /*
3454 * check for running out of space
3455 */
3456 if (error) {
3457 /*
3458 * Free the transaction structure.
3459 */
3460 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3461 xfs_trans_cancel(tp, 0);
3462 break;
3463 }
3464 xfs_ilock(ip, XFS_ILOCK_EXCL);
3465 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003466 ip->i_udquot, ip->i_gdquot, resblks, 0,
3467 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (error)
3469 goto error1;
3470
3471 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3472 xfs_trans_ihold(tp, ip);
3473
3474 /*
3475 * issue the bunmapi() call to free the blocks
3476 */
3477 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003478 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003480 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 if (error) {
3482 goto error0;
3483 }
3484
3485 /*
3486 * complete the transaction
3487 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003488 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 if (error) {
3490 goto error0;
3491 }
3492
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003493 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3495 }
3496
3497 out_unlock_iolock:
3498 if (need_iolock)
3499 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3500 return error;
3501
3502 error0:
3503 xfs_bmap_cancel(&free_list);
3504 error1:
3505 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3506 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3507 XFS_ILOCK_EXCL);
3508 return error;
3509}
3510
3511/*
3512 * xfs_change_file_space()
3513 * This routine allocates or frees disk space for the given file.
3514 * The user specified parameters are checked for alignment and size
3515 * limitations.
3516 *
3517 * RETURNS:
3518 * 0 on success
3519 * errno on error
3520 *
3521 */
3522int
3523xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003524 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 int cmd,
3526 xfs_flock64_t *bf,
3527 xfs_off_t offset,
3528 cred_t *credp,
3529 int attr_flags)
3530{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003531 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 int clrprealloc;
3533 int error;
3534 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 int setprealloc;
3536 xfs_off_t startoffset;
3537 xfs_off_t llen;
3538 xfs_trans_t *tp;
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003539 struct iattr iattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003541 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Christoph Hellwig993386c2007-08-28 16:12:30 +10003543 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 return XFS_ERROR(EINVAL);
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 switch (bf->l_whence) {
3547 case 0: /*SEEK_SET*/
3548 break;
3549 case 1: /*SEEK_CUR*/
3550 bf->l_start += offset;
3551 break;
3552 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003553 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 break;
3555 default:
3556 return XFS_ERROR(EINVAL);
3557 }
3558
3559 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3560
3561 if ( (bf->l_start < 0)
3562 || (bf->l_start > XFS_MAXIOFFSET(mp))
3563 || (bf->l_start + llen < 0)
3564 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3565 return XFS_ERROR(EINVAL);
3566
3567 bf->l_whence = 0;
3568
3569 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003570 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572 /*
3573 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3574 * file space.
3575 * These calls do NOT zero the data space allocated to the file,
3576 * nor do they change the file size.
3577 *
3578 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3579 * space.
3580 * These calls cause the new file data to be zeroed and the file
3581 * size to be changed.
3582 */
3583 setprealloc = clrprealloc = 0;
3584
3585 switch (cmd) {
3586 case XFS_IOC_RESVSP:
3587 case XFS_IOC_RESVSP64:
3588 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3589 1, attr_flags);
3590 if (error)
3591 return error;
3592 setprealloc = 1;
3593 break;
3594
3595 case XFS_IOC_UNRESVSP:
3596 case XFS_IOC_UNRESVSP64:
3597 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3598 attr_flags)))
3599 return error;
3600 break;
3601
3602 case XFS_IOC_ALLOCSP:
3603 case XFS_IOC_ALLOCSP64:
3604 case XFS_IOC_FREESP:
3605 case XFS_IOC_FREESP64:
3606 if (startoffset > fsize) {
3607 error = xfs_alloc_file_space(ip, fsize,
3608 startoffset - fsize, 0, attr_flags);
3609 if (error)
3610 break;
3611 }
3612
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003613 iattr.ia_valid = ATTR_SIZE;
3614 iattr.ia_size = startoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003616 error = xfs_setattr(ip, &iattr, attr_flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
3618 if (error)
3619 return error;
3620
3621 clrprealloc = 1;
3622 break;
3623
3624 default:
3625 ASSERT(0);
3626 return XFS_ERROR(EINVAL);
3627 }
3628
3629 /*
3630 * update the inode timestamp, mode, and prealloc flag bits
3631 */
3632 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3633
3634 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3635 0, 0, 0))) {
3636 /* ASSERT(0); */
3637 xfs_trans_cancel(tp, 0);
3638 return error;
3639 }
3640
3641 xfs_ilock(ip, XFS_ILOCK_EXCL);
3642
3643 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3644 xfs_trans_ihold(tp, ip);
3645
Christoph Hellwig0f285c82008-07-18 17:13:28 +10003646 if ((attr_flags & XFS_ATTR_DMI) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 ip->i_d.di_mode &= ~S_ISUID;
3648
3649 /*
3650 * Note that we don't have to worry about mandatory
3651 * file locking being disabled here because we only
3652 * clear the S_ISGID bit if the Group execute bit is
3653 * on, but if it was on then mandatory locking wouldn't
3654 * have been enabled.
3655 */
3656 if (ip->i_d.di_mode & S_IXGRP)
3657 ip->i_d.di_mode &= ~S_ISGID;
3658
3659 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3660 }
3661 if (setprealloc)
3662 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3663 else if (clrprealloc)
3664 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3665
3666 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3667 xfs_trans_set_sync(tp);
3668
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003669 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
3671 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3672
3673 return error;
3674}