blob: 3fef54b11582cf91eaf65f5300a459c38a2060d7 [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 -070078/*
79 * xfs_getattr
80 */
Christoph Hellwig993386c2007-08-28 16:12:30 +100081int
Linus Torvalds1da177e2005-04-16 15:20:36 -070082xfs_getattr(
Christoph Hellwig993386c2007-08-28 16:12:30 +100083 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +100084 bhv_vattr_t *vap,
Christoph Hellwig993386c2007-08-28 16:12:30 +100085 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Christoph Hellwig993386c2007-08-28 16:12:30 +100087 bhv_vnode_t *vp = XFS_ITOV(ip);
88 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Lachlan McIlroycf441ee2008-02-07 16:42:19 +110090 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (XFS_FORCED_SHUTDOWN(mp))
93 return XFS_ERROR(EIO);
94
95 if (!(flags & ATTR_LAZY))
96 xfs_ilock(ip, XFS_ILOCK_SHARED);
97
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100098 vap->va_size = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (vap->va_mask == XFS_AT_SIZE)
100 goto all_done;
101
102 vap->va_nblocks =
103 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
104 vap->va_nodeid = ip->i_ino;
105#if XFS_BIG_INUMS
106 vap->va_nodeid += mp->m_inoadd;
107#endif
108 vap->va_nlink = ip->i_d.di_nlink;
109
110 /*
111 * Quick exit for non-stat callers
112 */
113 if ((vap->va_mask &
114 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
115 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
116 goto all_done;
117
118 /*
119 * Copy from in-core inode.
120 */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000121 vap->va_mode = ip->i_d.di_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 vap->va_uid = ip->i_d.di_uid;
123 vap->va_gid = ip->i_d.di_gid;
124 vap->va_projid = ip->i_d.di_projid;
125
126 /*
127 * Check vnode type block/char vs. everything else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000129 switch (ip->i_d.di_mode & S_IFMT) {
130 case S_IFBLK:
131 case S_IFCHR:
132 vap->va_rdev = ip->i_df.if_u2.if_rdev;
133 vap->va_blocksize = BLKDEV_IOSIZE;
134 break;
135 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 vap->va_rdev = 0;
137
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100138 if (!(XFS_IS_REALTIME_INODE(ip))) {
David Chinnere8c8b3a2005-11-02 10:33:05 +1100139 vap->va_blocksize = xfs_preferred_iosize(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 } else {
141
142 /*
143 * If the file blocks are being allocated from a
144 * realtime partition, then return the inode's
145 * realtime extent size or the realtime volume's
146 * extent size.
147 */
David Chinner957d0eb2007-06-18 16:50:37 +1000148 vap->va_blocksize =
149 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000151 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
Nathan Scottca5ccbf2006-01-11 21:03:04 +1100154 vn_atime_to_timespec(vp, &vap->va_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
156 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
157 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
158 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
159
160 /*
161 * Exit for stat callers. See if any of the rest of the fields
162 * to be filled in are needed.
163 */
164 if ((vap->va_mask &
165 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
166 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
167 goto all_done;
168
169 /*
170 * Convert di_flags to xflags.
171 */
172 vap->va_xflags = xfs_ip2xflags(ip);
173
174 /*
175 * Exit for inode revalidate. See if any of the rest of
176 * the fields to be filled in are needed.
177 */
178 if ((vap->va_mask &
179 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
180 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
181 goto all_done;
182
183 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
184 vap->va_nextents =
185 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
186 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
187 ip->i_d.di_nextents;
188 if (ip->i_afp)
189 vap->va_anextents =
190 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
191 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
192 ip->i_d.di_anextents;
193 else
194 vap->va_anextents = 0;
195 vap->va_gen = ip->i_d.di_gen;
196
197 all_done:
198 if (!(flags & ATTR_LAZY))
199 xfs_iunlock(ip, XFS_ILOCK_SHARED);
200 return 0;
201}
202
203
204/*
205 * xfs_setattr
206 */
207int
208xfs_setattr(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000209 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +1000210 bhv_vattr_t *vap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int flags,
212 cred_t *credp)
213{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000214 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int mask;
217 int code;
218 uint lock_flags;
219 uint commit_flags=0;
220 uid_t uid=0, iuid=0;
221 gid_t gid=0, igid=0;
222 int timeflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 xfs_prid_t projid=0, iprojid=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
225 int file_owner;
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700226 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100228 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000230 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return XFS_ERROR(EROFS);
232
233 /*
234 * Cannot set certain attributes.
235 */
236 mask = vap->va_mask;
237 if (mask & XFS_AT_NOSET) {
238 return XFS_ERROR(EINVAL);
239 }
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (XFS_FORCED_SHUTDOWN(mp))
242 return XFS_ERROR(EIO);
243
244 /*
245 * Timestamps do not need to be logged and hence do not
246 * need to be done within a transaction.
247 */
248 if (mask & XFS_AT_UPDTIMES) {
249 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
250 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
251 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
252 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
253 xfs_ichgtime(ip, timeflags);
254 return 0;
255 }
256
257 olddquot1 = olddquot2 = NULL;
258 udqp = gdqp = NULL;
259
260 /*
261 * If disk quotas is on, we make sure that the dquots do exist on disk,
262 * before we start any other transactions. Trying to do this later
263 * is messy. We don't care to take a readlock to look at the ids
264 * in inode here, because we can't hold it across the trans_reserve.
265 * If the IDs do change before we take the ilock, we're covered
266 * because the i_*dquot fields will get updated anyway.
267 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000268 if (XFS_IS_QUOTA_ON(mp) &&
269 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 uint qflags = 0;
271
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000272 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 uid = vap->va_uid;
274 qflags |= XFS_QMOPT_UQUOTA;
275 } else {
276 uid = ip->i_d.di_uid;
277 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000278 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 gid = vap->va_gid;
280 qflags |= XFS_QMOPT_GQUOTA;
281 } else {
282 gid = ip->i_d.di_gid;
283 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000284 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
285 projid = vap->va_projid;
286 qflags |= XFS_QMOPT_PQUOTA;
287 } else {
288 projid = ip->i_d.di_projid;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /*
291 * We take a reference when we initialize udqp and gdqp,
292 * so it is important that we never blindly double trip on
293 * the same variable. See xfs_create() for an example.
294 */
295 ASSERT(udqp == NULL);
296 ASSERT(gdqp == NULL);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000297 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
298 &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100300 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 /*
304 * For the other attributes, we acquire the inode lock and
305 * first do an error checking pass.
306 */
307 tp = NULL;
308 lock_flags = XFS_ILOCK_EXCL;
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700309 if (flags & ATTR_NOLOCK)
310 need_iolock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!(mask & XFS_AT_SIZE)) {
312 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
313 (mp->m_flags & XFS_MOUNT_WSYNC)) {
314 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
315 commit_flags = 0;
316 if ((code = xfs_trans_reserve(tp, 0,
317 XFS_ICHANGE_LOG_RES(mp), 0,
318 0, 0))) {
319 lock_flags = 0;
320 goto error_return;
321 }
322 }
323 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000324 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 !(flags & ATTR_DMI)) {
326 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100327 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 vap->va_size, 0, dmflags, NULL);
329 if (code) {
330 lock_flags = 0;
331 goto error_return;
332 }
333 }
334 if (need_iolock)
335 lock_flags |= XFS_IOLOCK_EXCL;
336 }
337
338 xfs_ilock(ip, lock_flags);
339
340 /* boolean: are we the file owner? */
341 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
342
343 /*
344 * Change various properties of a file.
345 * Only the owner or users with CAP_FOWNER
346 * capability may do these things.
347 */
348 if (mask &
349 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
350 XFS_AT_GID|XFS_AT_PROJID)) {
351 /*
352 * CAP_FOWNER overrides the following restrictions:
353 *
354 * The user ID of the calling process must be equal
355 * to the file owner ID, except in cases where the
356 * CAP_FSETID capability is applicable.
357 */
358 if (!file_owner && !capable(CAP_FOWNER)) {
359 code = XFS_ERROR(EPERM);
360 goto error_return;
361 }
362
363 /*
364 * CAP_FSETID overrides the following restrictions:
365 *
366 * The effective user ID of the calling process shall match
367 * the file owner when setting the set-user-ID and
368 * set-group-ID bits on that file.
369 *
370 * The effective group ID or one of the supplementary group
371 * IDs of the calling process shall match the group owner of
372 * the file when setting the set-group-ID bit on that file
373 */
374 if (mask & XFS_AT_MODE) {
375 mode_t m = 0;
376
377 if ((vap->va_mode & S_ISUID) && !file_owner)
378 m |= S_ISUID;
379 if ((vap->va_mode & S_ISGID) &&
380 !in_group_p((gid_t)ip->i_d.di_gid))
381 m |= S_ISGID;
382#if 0
383 /* Linux allows this, Irix doesn't. */
Christoph Hellwig42173f62008-04-22 17:33:25 +1000384 if ((vap->va_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 m |= S_ISVTX;
386#endif
387 if (m && !capable(CAP_FSETID))
388 vap->va_mode &= ~m;
389 }
390 }
391
392 /*
393 * Change file ownership. Must be the owner or privileged.
394 * If the system was configured with the "restricted_chown"
395 * option, the owner is not permitted to give away the file,
396 * and can change the group id only to a group of which he
397 * or she is a member.
398 */
399 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
400 /*
401 * These IDs could have changed since we last looked at them.
402 * But, we're assured that if the ownership did change
403 * while we didn't have the inode locked, inode's dquot(s)
404 * would have changed also.
405 */
406 iuid = ip->i_d.di_uid;
407 iprojid = ip->i_d.di_projid;
408 igid = ip->i_d.di_gid;
409 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
410 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
411 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
412 iprojid;
413
414 /*
415 * CAP_CHOWN overrides the following restrictions:
416 *
417 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
418 * shall override the restriction that a process cannot
419 * change the user ID of a file it owns and the restriction
420 * that the group ID supplied to the chown() function
421 * shall be equal to either the group ID or one of the
422 * supplementary group IDs of the calling process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
424 if (restricted_chown &&
425 (iuid != uid || (igid != gid &&
426 !in_group_p((gid_t)gid))) &&
427 !capable(CAP_CHOWN)) {
428 code = XFS_ERROR(EPERM);
429 goto error_return;
430 }
431 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000432 * Do a quota reservation only if uid/projid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * going to change.
434 */
435 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000436 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
438 ASSERT(tp);
439 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
440 capable(CAP_FOWNER) ?
441 XFS_QMOPT_FORCE_RES : 0);
442 if (code) /* out of quota */
443 goto error_return;
444 }
445 }
446
447 /*
448 * Truncate file. Must have write permission and not be a directory.
449 */
450 if (mask & XFS_AT_SIZE) {
451 /* Short circuit the truncate case for zero length files */
452 if ((vap->va_size == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000453 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 xfs_iunlock(ip, XFS_ILOCK_EXCL);
455 lock_flags &= ~XFS_ILOCK_EXCL;
456 if (mask & XFS_AT_CTIME)
457 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
458 code = 0;
459 goto error_return;
460 }
461
Christoph Hellwig42173f62008-04-22 17:33:25 +1000462 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 code = XFS_ERROR(EISDIR);
464 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000465 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 code = XFS_ERROR(EINVAL);
467 goto error_return;
468 }
469 /*
470 * Make sure that the dquots are attached to the inode.
471 */
472 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
473 goto error_return;
474 }
475
476 /*
477 * Change file access or modified times.
478 */
479 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
480 if (!file_owner) {
481 if ((flags & ATTR_UTIME) &&
482 !capable(CAP_FOWNER)) {
483 code = XFS_ERROR(EPERM);
484 goto error_return;
485 }
486 }
487 }
488
489 /*
490 * Change extent size or realtime flag.
491 */
492 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
493 /*
494 * Can't change extent size if any extents are allocated.
495 */
Eric Sandeene94af022005-11-02 15:10:41 +1100496 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
498 vap->va_extsize) ) {
499 code = XFS_ERROR(EINVAL); /* EFBIG? */
500 goto error_return;
501 }
502
503 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * Can't change realtime flag if any extents are allocated.
505 */
Eric Sandeene94af022005-11-02 15:10:41 +1100506 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
507 (mask & XFS_AT_XFLAGS) &&
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100508 (XFS_IS_REALTIME_INODE(ip)) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
510 code = XFS_ERROR(EINVAL); /* EFBIG? */
511 goto error_return;
512 }
513 /*
514 * Extent size must be a multiple of the appropriate block
515 * size, if set at all.
516 */
517 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
518 xfs_extlen_t size;
519
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100520 if (XFS_IS_REALTIME_INODE(ip) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 ((mask & XFS_AT_XFLAGS) &&
522 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
523 size = mp->m_sb.sb_rextsize <<
524 mp->m_sb.sb_blocklog;
525 } else {
526 size = mp->m_sb.sb_blocksize;
527 }
528 if (vap->va_extsize % size) {
529 code = XFS_ERROR(EINVAL);
530 goto error_return;
531 }
532 }
533 /*
534 * If realtime flag is set then must have realtime data.
535 */
536 if ((mask & XFS_AT_XFLAGS) &&
537 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
538 if ((mp->m_sb.sb_rblocks == 0) ||
539 (mp->m_sb.sb_rextsize == 0) ||
540 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
541 code = XFS_ERROR(EINVAL);
542 goto error_return;
543 }
544 }
545
546 /*
547 * Can't modify an immutable/append-only file unless
548 * we have appropriate permission.
549 */
550 if ((mask & XFS_AT_XFLAGS) &&
551 (ip->i_d.di_flags &
552 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
553 (vap->va_xflags &
554 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
555 !capable(CAP_LINUX_IMMUTABLE)) {
556 code = XFS_ERROR(EPERM);
557 goto error_return;
558 }
559 }
560
561 /*
562 * Now we can make the changes. Before we join the inode
563 * to the transaction, if XFS_AT_SIZE is set then take care of
564 * the part of the truncation that must be done without the
565 * inode lock. This needs to be done before joining the inode
566 * to the transaction, because the inode cannot be unlocked
567 * once it is a part of the transaction.
568 */
569 if (mask & XFS_AT_SIZE) {
570 code = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000571 if ((vap->va_size > ip->i_size) &&
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100572 (flags & ATTR_NOSIZETOK) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 code = xfs_igrow_start(ip, vap->va_size, credp);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000576
577 /*
578 * We are going to log the inode size change in this
579 * transaction so any previous writes that are beyond the on
580 * disk EOF and the new EOF that have not been written out need
581 * to be written here. If we do not write the data out, we
582 * expose ourselves to the null files problem.
583 *
584 * Only flush from the on disk size to the smaller of the in
585 * memory file size or the new size as that's the range we
586 * really care about here and prevents waiting for other data
587 * not within the range we care about here.
588 */
589 if (!code &&
590 (ip->i_size != ip->i_d.di_size) &&
591 (vap->va_size > ip->i_d.di_size)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000592 code = xfs_flush_pages(ip,
David Chinnerc32676e2007-07-19 16:28:58 +1000593 ip->i_d.di_size, vap->va_size,
594 XFS_B_ASYNC, FI_NONE);
595 }
596
597 /* wait for all I/O to complete */
Christoph Hellwigb677c212007-08-29 11:46:28 +1000598 vn_iowait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!code)
601 code = xfs_itruncate_data(ip, vap->va_size);
602 if (code) {
603 ASSERT(tp == NULL);
604 lock_flags &= ~XFS_ILOCK_EXCL;
605 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
606 goto error_return;
607 }
608 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
609 if ((code = xfs_trans_reserve(tp, 0,
610 XFS_ITRUNCATE_LOG_RES(mp), 0,
611 XFS_TRANS_PERM_LOG_RES,
612 XFS_ITRUNCATE_LOG_COUNT))) {
613 xfs_trans_cancel(tp, 0);
614 if (need_iolock)
615 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
616 return code;
617 }
618 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
619 xfs_ilock(ip, XFS_ILOCK_EXCL);
620 }
621
622 if (tp) {
623 xfs_trans_ijoin(tp, ip, lock_flags);
624 xfs_trans_ihold(tp, ip);
625 }
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /*
628 * Truncate file. Must have write permission and not be a directory.
629 */
630 if (mask & XFS_AT_SIZE) {
David Chinner44d814c2008-03-06 13:45:29 +1100631 /*
632 * Only change the c/mtime if we are changing the size
633 * or we are explicitly asked to change it. This handles
634 * the semantic difference between truncate() and ftruncate()
635 * as implemented in the VFS.
636 */
637 if (vap->va_size != ip->i_size || (mask & XFS_AT_CTIME))
638 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
639
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000640 if (vap->va_size > ip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 xfs_igrow_finish(tp, ip, vap->va_size,
642 !(flags & ATTR_DMI));
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000643 } else if ((vap->va_size <= ip->i_size) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
645 /*
646 * signal a sync transaction unless
647 * we're truncating an already unlinked
648 * file on a wsync filesystem
649 */
650 code = xfs_itruncate_finish(&tp, ip,
651 (xfs_fsize_t)vap->va_size,
652 XFS_DATA_FORK,
653 ((ip->i_d.di_nlink != 0 ||
654 !(mp->m_flags & XFS_MOUNT_WSYNC))
655 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000656 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000658 /*
659 * Truncated "down", so we're removing references
660 * to old data here - if we now delay flushing for
661 * a long time, we expose ourselves unduly to the
662 * notorious NULL files problem. So, we mark this
663 * vnode and flush it when the file is closed, and
664 * do not wait the usual (long) time for writeout.
665 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000666 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
670 /*
671 * Change file access modes.
672 */
673 if (mask & XFS_AT_MODE) {
674 ip->i_d.di_mode &= S_IFMT;
675 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
676
677 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
678 timeflags |= XFS_ICHGTIME_CHG;
679 }
680
681 /*
682 * Change file ownership. Must be the owner or privileged.
683 * If the system was configured with the "restricted_chown"
684 * option, the owner is not permitted to give away the file,
685 * and can change the group id only to a group of which he
686 * or she is a member.
687 */
688 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
689 /*
690 * CAP_FSETID overrides the following restrictions:
691 *
692 * The set-user-ID and set-group-ID bits of a file will be
693 * cleared upon successful return from chown()
694 */
695 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
696 !capable(CAP_FSETID)) {
697 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
698 }
699
700 /*
701 * Change the ownerships and register quota modifications
702 * in the transaction.
703 */
704 if (iuid != uid) {
705 if (XFS_IS_UQUOTA_ON(mp)) {
706 ASSERT(mask & XFS_AT_UID);
707 ASSERT(udqp);
708 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
709 &ip->i_udquot, udqp);
710 }
711 ip->i_d.di_uid = uid;
712 }
713 if (igid != gid) {
714 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000715 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 ASSERT(mask & XFS_AT_GID);
717 ASSERT(gdqp);
718 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
719 &ip->i_gdquot, gdqp);
720 }
721 ip->i_d.di_gid = gid;
722 }
723 if (iprojid != projid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000724 if (XFS_IS_PQUOTA_ON(mp)) {
725 ASSERT(!XFS_IS_GQUOTA_ON(mp));
726 ASSERT(mask & XFS_AT_PROJID);
727 ASSERT(gdqp);
728 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
729 &ip->i_gdquot, gdqp);
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ip->i_d.di_projid = projid;
732 /*
733 * We may have to rev the inode as well as
734 * the superblock version number since projids didn't
735 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
736 */
737 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
738 xfs_bump_ino_vers2(tp, ip);
739 }
740
741 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
742 timeflags |= XFS_ICHGTIME_CHG;
743 }
744
745
746 /*
747 * Change file access or modified times.
748 */
749 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
750 if (mask & XFS_AT_ATIME) {
751 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
752 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
753 ip->i_update_core = 1;
754 timeflags &= ~XFS_ICHGTIME_ACC;
755 }
756 if (mask & XFS_AT_MTIME) {
757 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
758 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
759 timeflags &= ~XFS_ICHGTIME_MOD;
760 timeflags |= XFS_ICHGTIME_CHG;
761 }
762 if (tp && (flags & ATTR_UTIME))
763 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
764 }
765
766 /*
767 * Change XFS-added attributes.
768 */
769 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
770 if (mask & XFS_AT_EXTSIZE) {
771 /*
772 * Converting bytes to fs blocks.
773 */
774 ip->i_d.di_extsize = vap->va_extsize >>
775 mp->m_sb.sb_blocklog;
776 }
777 if (mask & XFS_AT_XFLAGS) {
778 uint di_flags;
779
780 /* can't set PREALLOC this way, just preserve it */
781 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
782 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
783 di_flags |= XFS_DIFLAG_IMMUTABLE;
784 if (vap->va_xflags & XFS_XFLAG_APPEND)
785 di_flags |= XFS_DIFLAG_APPEND;
786 if (vap->va_xflags & XFS_XFLAG_SYNC)
787 di_flags |= XFS_DIFLAG_SYNC;
788 if (vap->va_xflags & XFS_XFLAG_NOATIME)
789 di_flags |= XFS_DIFLAG_NOATIME;
790 if (vap->va_xflags & XFS_XFLAG_NODUMP)
791 di_flags |= XFS_DIFLAG_NODUMP;
Nathan Scott365ca832005-06-21 15:39:12 +1000792 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
793 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000794 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
795 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000796 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
797 di_flags |= XFS_DIFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
799 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
800 di_flags |= XFS_DIFLAG_RTINHERIT;
801 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
802 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100803 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
804 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
805 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000806 if (vap->va_xflags & XFS_XFLAG_REALTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100808 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
809 di_flags |= XFS_DIFLAG_EXTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811 ip->i_d.di_flags = di_flags;
812 }
813 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
814 timeflags |= XFS_ICHGTIME_CHG;
815 }
816
817 /*
818 * Change file inode change time only if XFS_AT_CTIME set
819 * AND we have been called by a DMI function.
820 */
821
822 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
823 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
824 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
825 ip->i_update_core = 1;
826 timeflags &= ~XFS_ICHGTIME_CHG;
827 }
828
829 /*
830 * Send out timestamp changes that need to be set to the
831 * current time. Not done when called by a DMI function.
832 */
833 if (timeflags && !(flags & ATTR_DMI))
834 xfs_ichgtime(ip, timeflags);
835
836 XFS_STATS_INC(xs_ig_attrchg);
837
838 /*
839 * If this is a synchronous mount, make sure that the
840 * transaction goes to disk before returning to the user.
841 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000842 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * One for the truncate and one for the timestamps since we
844 * don't want to change the timestamps unless we're sure the
845 * truncate worked. Truncates are less than 1% of the laddis
846 * mix so this probably isn't worth the trouble to optimize.
847 */
848 code = 0;
849 if (tp) {
850 if (mp->m_flags & XFS_MOUNT_WSYNC)
851 xfs_trans_set_sync(tp);
852
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000853 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 xfs_iunlock(ip, lock_flags);
857
858 /*
859 * Release any dquot(s) the inode had kept before chown.
860 */
861 XFS_QM_DQRELE(mp, olddquot1);
862 XFS_QM_DQRELE(mp, olddquot2);
863 XFS_QM_DQRELE(mp, udqp);
864 XFS_QM_DQRELE(mp, gdqp);
865
866 if (code) {
867 return code;
868 }
869
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000870 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 !(flags & ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100872 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 NULL, DM_RIGHT_NULL, NULL, NULL,
874 0, 0, AT_DELAY_FLAG(flags));
875 }
876 return 0;
877
878 abort_return:
879 commit_flags |= XFS_TRANS_ABORT;
880 /* FALLTHROUGH */
881 error_return:
882 XFS_QM_DQRELE(mp, udqp);
883 XFS_QM_DQRELE(mp, gdqp);
884 if (tp) {
885 xfs_trans_cancel(tp, commit_flags);
886 }
887 if (lock_flags != 0) {
888 xfs_iunlock(ip, lock_flags);
889 }
890 return code;
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000894 * The maximum pathlen is 1024 bytes. Since the minimum file system
895 * blocksize is 512 bytes, we can get a max of 2 extents back from
896 * bmapi.
897 */
898#define SYMLINK_MAPS 2
899
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000900STATIC int
901xfs_readlink_bmap(
902 xfs_inode_t *ip,
903 char *link)
904{
905 xfs_mount_t *mp = ip->i_mount;
906 int pathlen = ip->i_d.di_size;
907 int nmaps = SYMLINK_MAPS;
908 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
909 xfs_daddr_t d;
910 int byte_cnt;
911 int n;
912 xfs_buf_t *bp;
913 int error = 0;
914
915 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
916 mval, &nmaps, NULL, NULL);
917 if (error)
918 goto out;
919
920 for (n = 0; n < nmaps; n++) {
921 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
922 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
923
924 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
925 error = XFS_BUF_GETERROR(bp);
926 if (error) {
927 xfs_ioerror_alert("xfs_readlink",
928 ip->i_mount, bp, XFS_BUF_ADDR(bp));
929 xfs_buf_relse(bp);
930 goto out;
931 }
932 if (pathlen < byte_cnt)
933 byte_cnt = pathlen;
934 pathlen -= byte_cnt;
935
936 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
937 xfs_buf_relse(bp);
938 }
939
940 link[ip->i_d.di_size] = '\0';
941 error = 0;
942
943 out:
944 return error;
945}
946
Christoph Hellwig993386c2007-08-28 16:12:30 +1000947int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000949 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000950 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000952 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100956 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (XFS_FORCED_SHUTDOWN(mp))
959 return XFS_ERROR(EIO);
960
961 xfs_ilock(ip, XFS_ILOCK_SHARED);
962
963 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000964 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000966 pathlen = ip->i_d.di_size;
967 if (!pathlen)
968 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000971 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
972 link[pathlen] = '\0';
973 } else {
974 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000977 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return error;
980}
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/*
983 * xfs_fsync
984 *
985 * This is called to sync the inode and its data out to disk.
986 * We need to hold the I/O lock while flushing the data, and
987 * the inode lock while flushing the inode. The inode lock CANNOT
988 * be held while flushing the data, so acquire after we're done
989 * with that.
990 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000991int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992xfs_fsync(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000993 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 int flag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 xfs_off_t start,
996 xfs_off_t stop)
997{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 xfs_trans_t *tp;
999 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001000 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001002 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 ASSERT(start >= 0 && stop >= -1);
1005
1006 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1007 return XFS_ERROR(EIO);
1008
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +10001009 if (flag & FSYNC_DATA)
1010 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * We always need to make sure that the required inode state
1014 * is safe on disk. The vnode might be clean but because
1015 * of committed transactions that haven't hit the disk yet.
1016 * Likewise, there could be unflushed non-transactional
1017 * changes to the inode core that have to go to disk.
1018 *
1019 * The following code depends on one assumption: that
1020 * any transaction that changes an inode logs the core
1021 * because it has to change some field in the inode core
1022 * (typically nextents or nblocks). That assumption
1023 * implies that any transactions against an inode will
1024 * catch any non-transactional updates. If inode-altering
1025 * transactions exist that violate this assumption, the
1026 * code breaks. Right now, it figures that if the involved
1027 * update_* field is clear and the inode is unpinned, the
1028 * inode is clean. Either it's been flushed or it's been
1029 * committed and the commit has hit the disk unpinning the inode.
1030 * (Note that xfs_inode_item_format() called at commit clears
1031 * the update_* fields.)
1032 */
1033 xfs_ilock(ip, XFS_ILOCK_SHARED);
1034
1035 /* If we are flushing data then we care about update_size
1036 * being set, otherwise we care about update_core
1037 */
1038 if ((flag & FSYNC_DATA) ?
1039 (ip->i_update_size == 0) :
1040 (ip->i_update_core == 0)) {
1041 /*
1042 * Timestamps/size haven't changed since last inode
1043 * flush or inode transaction commit. That means
1044 * either nothing got written or a transaction
1045 * committed which caught the updates. If the
1046 * latter happened and the transaction hasn't
1047 * hit the disk yet, the inode will be still
1048 * be pinned. If it is, force the log.
1049 */
1050
1051 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1052
1053 if (xfs_ipincount(ip)) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001054 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 XFS_LOG_FORCE |
1056 ((flag & FSYNC_WAIT)
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001057 ? XFS_LOG_SYNC : 0),
1058 &log_flushed);
1059 } else {
1060 /*
1061 * If the inode is not pinned and nothing
1062 * has changed we don't need to flush the
1063 * cache.
1064 */
1065 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 error = 0;
1068 } else {
1069 /*
1070 * Kick off a transaction to log the inode
1071 * core to get the updates. Make it
1072 * sync if FSYNC_WAIT is passed in (which
1073 * is done by everybody but specfs). The
1074 * sync transaction will also force the log.
1075 */
1076 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1077 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1078 if ((error = xfs_trans_reserve(tp, 0,
1079 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1080 0, 0, 0))) {
1081 xfs_trans_cancel(tp, 0);
1082 return error;
1083 }
1084 xfs_ilock(ip, XFS_ILOCK_EXCL);
1085
1086 /*
1087 * Note - it's possible that we might have pushed
1088 * ourselves out of the way during trans_reserve
1089 * which would flush the inode. But there's no
1090 * guarantee that the inode buffer has actually
1091 * gone out yet (it's delwri). Plus the buffer
1092 * could be pinned anyway if it's part of an
1093 * inode in another recent transaction. So we
1094 * play it safe and fire off the transaction anyway.
1095 */
1096 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1097 xfs_trans_ihold(tp, ip);
1098 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1099 if (flag & FSYNC_WAIT)
1100 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001101 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1104 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001105
1106 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1107 /*
1108 * If the log write didn't issue an ordered tag we need
1109 * to flush the disk cache for the data device now.
1110 */
1111 if (!log_flushed)
1112 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1113
1114 /*
1115 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +10001116 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001117 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11001118 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001119 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1120 }
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return error;
1123}
1124
1125/*
David Chinner92dfe8d2007-05-24 15:22:19 +10001126 * This is called by xfs_inactive to free any blocks beyond eof
1127 * when the link count isn't zero and by xfs_dm_punch_hole() when
1128 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001130int
1131xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +10001133 xfs_inode_t *ip,
1134 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 xfs_trans_t *tp;
1137 int error;
1138 xfs_fileoff_t end_fsb;
1139 xfs_fileoff_t last_fsb;
1140 xfs_filblks_t map_len;
1141 int nimaps;
1142 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +10001143 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 /*
1146 * Figure out if there are any blocks beyond the end
1147 * of the file. If not, then there is nothing to do.
1148 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001149 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1151 map_len = last_fsb - end_fsb;
1152 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 nimaps = 1;
1156 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001157 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001158 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1160
1161 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001162 (imap.br_startblock != HOLESTARTBLOCK ||
1163 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /*
1165 * Attach the dquots to the inode up front.
1166 */
1167 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001168 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /*
1171 * There are blocks after the end of file.
1172 * Free them up now by truncating the file to
1173 * its current size.
1174 */
1175 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1176
1177 /*
1178 * Do the xfs_itruncate_start() call before
1179 * reserving any log space because
1180 * itruncate_start will call into the buffer
1181 * cache and we can't
1182 * do that within a transaction.
1183 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001184 if (use_iolock)
1185 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001186 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001187 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001188 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001189 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001190 if (use_iolock)
1191 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001192 return error;
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 error = xfs_trans_reserve(tp, 0,
1196 XFS_ITRUNCATE_LOG_RES(mp),
1197 0, XFS_TRANS_PERM_LOG_RES,
1198 XFS_ITRUNCATE_LOG_COUNT);
1199 if (error) {
1200 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1201 xfs_trans_cancel(tp, 0);
1202 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001203 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205
1206 xfs_ilock(ip, XFS_ILOCK_EXCL);
1207 xfs_trans_ijoin(tp, ip,
1208 XFS_IOLOCK_EXCL |
1209 XFS_ILOCK_EXCL);
1210 xfs_trans_ihold(tp, ip);
1211
1212 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001213 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 XFS_DATA_FORK,
1215 0);
1216 /*
1217 * If we get an error at this point we
1218 * simply don't bother truncating the file.
1219 */
1220 if (error) {
1221 xfs_trans_cancel(tp,
1222 (XFS_TRANS_RELEASE_LOG_RES |
1223 XFS_TRANS_ABORT));
1224 } else {
1225 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001226 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
David Chinner92dfe8d2007-05-24 15:22:19 +10001228 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1229 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001231 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
1234/*
1235 * Free a symlink that has blocks associated with it.
1236 */
1237STATIC int
1238xfs_inactive_symlink_rmt(
1239 xfs_inode_t *ip,
1240 xfs_trans_t **tpp)
1241{
1242 xfs_buf_t *bp;
1243 int committed;
1244 int done;
1245 int error;
1246 xfs_fsblock_t first_block;
1247 xfs_bmap_free_t free_list;
1248 int i;
1249 xfs_mount_t *mp;
1250 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1251 int nmaps;
1252 xfs_trans_t *ntp;
1253 int size;
1254 xfs_trans_t *tp;
1255
1256 tp = *tpp;
1257 mp = ip->i_mount;
1258 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1259 /*
1260 * We're freeing a symlink that has some
1261 * blocks allocated to it. Free the
1262 * blocks here. We know that we've got
1263 * either 1 or 2 extents and that we can
1264 * free them all in one bunmapi call.
1265 */
1266 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1267 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1268 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1269 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1270 xfs_trans_cancel(tp, 0);
1271 *tpp = NULL;
1272 return error;
1273 }
1274 /*
1275 * Lock the inode, fix the size, and join it to the transaction.
1276 * Hold it so in the normal path, we still have it locked for
1277 * the second transaction. In the error paths we need it
1278 * held so the cancel won't rele it, see below.
1279 */
1280 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1281 size = (int)ip->i_d.di_size;
1282 ip->i_d.di_size = 0;
1283 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1284 xfs_trans_ihold(tp, ip);
1285 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1286 /*
1287 * Find the block(s) so we can inval and unmap them.
1288 */
1289 done = 0;
1290 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -08001291 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1293 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001294 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 goto error0;
1296 /*
1297 * Invalidate the block(s).
1298 */
1299 for (i = 0; i < nmaps; i++) {
1300 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1301 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1302 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1303 xfs_trans_binval(tp, bp);
1304 }
1305 /*
1306 * Unmap the dead block(s) to the free_list.
1307 */
1308 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001309 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 goto error1;
1311 ASSERT(done);
1312 /*
1313 * Commit the first transaction. This logs the EFI and the inode.
1314 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001315 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 goto error1;
1317 /*
1318 * The transaction must have been committed, since there were
1319 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1320 * The new tp has the extent freeing and EFDs.
1321 */
1322 ASSERT(committed);
1323 /*
1324 * The first xact was committed, so add the inode to the new one.
1325 * Mark it dirty so it will be logged and moved forward in the log as
1326 * part of every commit.
1327 */
1328 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1329 xfs_trans_ihold(tp, ip);
1330 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1331 /*
1332 * Get a new, empty transaction to return to our caller.
1333 */
1334 ntp = xfs_trans_dup(tp);
1335 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001336 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * If we get an error on the commit here or on the reserve below,
1338 * we need to unlock the inode since the new transaction doesn't
1339 * have the inode attached.
1340 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001341 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 tp = ntp;
1343 if (error) {
1344 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1345 goto error0;
1346 }
1347 /*
1348 * Remove the memory for extent descriptions (just bookkeeping).
1349 */
1350 if (ip->i_df.if_bytes)
1351 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1352 ASSERT(ip->i_df.if_bytes == 0);
1353 /*
1354 * Put an itruncate log reservation in the new transaction
1355 * for our caller.
1356 */
1357 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1358 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1359 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1360 goto error0;
1361 }
1362 /*
1363 * Return with the inode locked but not joined to the transaction.
1364 */
1365 *tpp = tp;
1366 return 0;
1367
1368 error1:
1369 xfs_bmap_cancel(&free_list);
1370 error0:
1371 /*
1372 * Have to come here with the inode locked and either
1373 * (held and in the transaction) or (not in the transaction).
1374 * If the inode isn't held then cancel would iput it, but
1375 * that's wrong since this is inactive and the vnode ref
1376 * count is 0 already.
1377 * Cancel won't do anything to the inode if held, but it still
1378 * needs to be locked until the cancel is done, if it was
1379 * joined to the transaction.
1380 */
1381 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1382 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1383 *tpp = NULL;
1384 return error;
1385
1386}
1387
1388STATIC int
1389xfs_inactive_symlink_local(
1390 xfs_inode_t *ip,
1391 xfs_trans_t **tpp)
1392{
1393 int error;
1394
1395 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1396 /*
1397 * We're freeing a symlink which fit into
1398 * the inode. Just free the memory used
1399 * to hold the old symlink.
1400 */
1401 error = xfs_trans_reserve(*tpp, 0,
1402 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1403 0, XFS_TRANS_PERM_LOG_RES,
1404 XFS_ITRUNCATE_LOG_COUNT);
1405
1406 if (error) {
1407 xfs_trans_cancel(*tpp, 0);
1408 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001409 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1412
1413 /*
1414 * Zero length symlinks _can_ exist.
1415 */
1416 if (ip->i_df.if_bytes > 0) {
1417 xfs_idata_realloc(ip,
1418 -(ip->i_df.if_bytes),
1419 XFS_DATA_FORK);
1420 ASSERT(ip->i_df.if_bytes == 0);
1421 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425STATIC int
1426xfs_inactive_attrs(
1427 xfs_inode_t *ip,
1428 xfs_trans_t **tpp)
1429{
1430 xfs_trans_t *tp;
1431 int error;
1432 xfs_mount_t *mp;
1433
1434 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1435 tp = *tpp;
1436 mp = ip->i_mount;
1437 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001438 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001440 if (error)
1441 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001444 if (error)
1445 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1448 error = xfs_trans_reserve(tp, 0,
1449 XFS_IFREE_LOG_RES(mp),
1450 0, XFS_TRANS_PERM_LOG_RES,
1451 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001452 if (error)
1453 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 xfs_ilock(ip, XFS_ILOCK_EXCL);
1456 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1457 xfs_trans_ihold(tp, ip);
1458 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1459
1460 ASSERT(ip->i_d.di_anextents == 0);
1461
1462 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001463 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001464
1465error_cancel:
1466 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1467 xfs_trans_cancel(tp, 0);
1468error_unlock:
1469 *tpp = NULL;
1470 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1471 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Christoph Hellwig993386c2007-08-28 16:12:30 +10001474int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001476 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001478 bhv_vnode_t *vp = XFS_ITOV(ip);
1479 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 int error;
1481
Christoph Hellwig42173f62008-04-22 17:33:25 +10001482 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001486 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return 0;
1488
David Chinner2a82b8b2007-07-11 11:09:12 +10001489 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001490 int truncated;
1491
David Chinner2a82b8b2007-07-11 11:09:12 +10001492 /*
1493 * If we are using filestreams, and we have an unlinked
1494 * file that we are processing the last close on, then nothing
1495 * will be able to reopen and write to this file. Purge this
1496 * inode from the filestreams cache so that it doesn't delay
1497 * teardown of the inode.
1498 */
1499 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1500 xfs_filestream_deassociate(ip);
1501
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001502 /*
1503 * If we previously truncated this file and removed old data
1504 * in the process, we want to initiate "early" writeout on
1505 * the last close. This is an attempt to combat the notorious
1506 * NULL files problem which is particularly noticable from a
1507 * truncate down, buffered (re-)write (delalloc), followed by
1508 * a crash. What we are effectively doing here is
1509 * significantly reducing the time window where we'd otherwise
1510 * be exposed to that problem.
1511 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001512 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001513 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001514 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001515 }
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (ip->i_d.di_nlink != 0) {
1518 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001519 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001520 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001522 (!(ip->i_d.di_flags &
1523 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001524 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1525 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001526 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 }
1529
1530 return 0;
1531}
1532
1533/*
1534 * xfs_inactive
1535 *
1536 * This is called when the vnode reference count for the vnode
1537 * goes to zero. If the file has been unlinked, then it must
1538 * now be truncated. Also, we clear all of the read-ahead state
1539 * kept for the inode here since the file is now closed.
1540 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001541int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001543 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001545 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +10001546 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 xfs_fsblock_t first_block;
1548 int committed;
1549 xfs_trans_t *tp;
1550 xfs_mount_t *mp;
1551 int error;
1552 int truncate;
1553
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001554 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /*
1557 * If the inode is already free, then there can be nothing
1558 * to clean up here.
1559 */
1560 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1561 ASSERT(ip->i_df.if_real_bytes == 0);
1562 ASSERT(ip->i_df.if_broot_bytes == 0);
1563 return VN_INACTIVE_CACHE;
1564 }
1565
1566 /*
1567 * Only do a truncate if it's a regular file with
1568 * some actual space in it. It's OK to look at the
1569 * inode's fields without the lock because we're the
1570 * only one with a reference to the inode.
1571 */
1572 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001573 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1574 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1576
1577 mp = ip->i_mount;
1578
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001579 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1580 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 error = 0;
1583
1584 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001585 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 goto out;
1587
1588 if (ip->i_d.di_nlink != 0) {
1589 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001590 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001591 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001592 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1593 (!(ip->i_d.di_flags &
1594 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1595 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001596 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1597 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001598 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
1600 goto out;
1601 }
1602
1603 ASSERT(ip->i_d.di_nlink == 0);
1604
1605 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001606 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1609 if (truncate) {
1610 /*
1611 * Do the xfs_itruncate_start() call before
1612 * reserving any log space because itruncate_start
1613 * will call into the buffer cache and we can't
1614 * do that within a transaction.
1615 */
1616 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1617
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001618 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1619 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001620 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001621 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1622 return VN_INACTIVE_CACHE;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 error = xfs_trans_reserve(tp, 0,
1626 XFS_ITRUNCATE_LOG_RES(mp),
1627 0, XFS_TRANS_PERM_LOG_RES,
1628 XFS_ITRUNCATE_LOG_COUNT);
1629 if (error) {
1630 /* Don't call itruncate_cleanup */
1631 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1632 xfs_trans_cancel(tp, 0);
1633 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001634 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636
1637 xfs_ilock(ip, XFS_ILOCK_EXCL);
1638 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1639 xfs_trans_ihold(tp, ip);
1640
1641 /*
1642 * normally, we have to run xfs_itruncate_finish sync.
1643 * But if filesystem is wsync and we're in the inactive
1644 * path, then we know that nlink == 0, and that the
1645 * xaction that made nlink == 0 is permanently committed
1646 * since xfs_remove runs as a synchronous transaction.
1647 */
1648 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1649 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1650
1651 if (error) {
1652 xfs_trans_cancel(tp,
1653 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1654 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001655 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1658
1659 /*
1660 * If we get an error while cleaning up a
1661 * symlink we bail out.
1662 */
1663 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1664 xfs_inactive_symlink_rmt(ip, &tp) :
1665 xfs_inactive_symlink_local(ip, &tp);
1666
1667 if (error) {
1668 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001669 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671
1672 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1673 xfs_trans_ihold(tp, ip);
1674 } else {
1675 error = xfs_trans_reserve(tp, 0,
1676 XFS_IFREE_LOG_RES(mp),
1677 0, XFS_TRANS_PERM_LOG_RES,
1678 XFS_INACTIVE_LOG_COUNT);
1679 if (error) {
1680 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1681 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001682 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
1684
1685 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1686 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1687 xfs_trans_ihold(tp, ip);
1688 }
1689
1690 /*
1691 * If there are attributes associated with the file
1692 * then blow them away now. The code calls a routine
1693 * that recursively deconstructs the attribute fork.
1694 * We need to just commit the current transaction
1695 * because we can't use it for xfs_attr_inactive().
1696 */
1697 if (ip->i_d.di_anextents > 0) {
1698 error = xfs_inactive_attrs(ip, &tp);
1699 /*
1700 * If we got an error, the transaction is already
1701 * cancelled, and the inode is unlocked. Just get out.
1702 */
1703 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001704 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 } else if (ip->i_afp) {
1706 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1707 }
1708
1709 /*
1710 * Free the inode.
1711 */
1712 XFS_BMAP_INIT(&free_list, &first_block);
1713 error = xfs_ifree(tp, ip, &free_list);
1714 if (error) {
1715 /*
1716 * If we fail to free the inode, shut down. The cancel
1717 * might do that, we need to make sure. Otherwise the
1718 * inode might be lost for a long time or forever.
1719 */
1720 if (!XFS_FORCED_SHUTDOWN(mp)) {
1721 cmn_err(CE_NOTE,
1722 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1723 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001724 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1727 } else {
1728 /*
1729 * Credit the quota account(s). The inode is gone.
1730 */
1731 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1732
1733 /*
David Chinner78e9da72008-04-10 12:24:17 +10001734 * Just ignore errors at this point. There is nothing we can
1735 * do except to try to keep going. Make sure it's not a silent
1736 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 */
David Chinner78e9da72008-04-10 12:24:17 +10001738 error = xfs_bmap_finish(&tp, &free_list, &committed);
1739 if (error)
1740 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1741 "xfs_bmap_finish() returned error %d", error);
1742 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1743 if (error)
1744 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1745 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747 /*
1748 * Release the dquots held by inode, if any.
1749 */
1750 XFS_QM_DQDETACH(mp, ip);
1751
1752 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1753
1754 out:
1755 return VN_INACTIVE_CACHE;
1756}
1757
1758
Christoph Hellwig993386c2007-08-28 16:12:30 +10001759int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001761 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001762 struct xfs_name *name,
Christoph Hellwigef1f5e72008-03-06 13:46:25 +11001763 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001765 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 xfs_ino_t e_inum;
1767 int error;
1768 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001770 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1773 return XFS_ERROR(EIO);
1774
1775 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok556b8b12008-04-10 12:22:07 +10001776 error = xfs_dir_lookup_int(dp, lock_mode, name, &e_inum, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (!error) {
Christoph Hellwigef1f5e72008-03-06 13:46:25 +11001778 *ipp = ip;
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001779 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 xfs_iunlock_map_shared(dp, lock_mode);
1782 return error;
1783}
1784
Christoph Hellwig993386c2007-08-28 16:12:30 +10001785int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001787 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001788 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001789 mode_t mode,
1790 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001791 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 cred_t *credp)
1793{
Barry Naujok556b8b12008-04-10 12:22:07 +10001794 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001795 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 xfs_trans_t *tp;
Barry Naujok556b8b12008-04-10 12:22:07 +10001797 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 xfs_bmap_free_t free_list;
1799 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001800 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 int dm_event_sent = 0;
1802 uint cancel_flags;
1803 int committed;
1804 xfs_prid_t prid;
1805 struct xfs_dquot *udqp, *gdqp;
1806 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001808 ASSERT(!*ipp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001809 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001811 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001813 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001814 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001815 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 if (error)
1818 return error;
1819 dm_event_sent = 1;
1820 }
1821
1822 if (XFS_FORCED_SHUTDOWN(mp))
1823 return XFS_ERROR(EIO);
1824
1825 /* Return through std_return after this point. */
1826
1827 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001828 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1829 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 else
1831 prid = (xfs_prid_t)dfltprid;
1832
1833 /*
1834 * Make sure that we have allocated dquot(s) on disk.
1835 */
1836 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001837 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1839 if (error)
1840 goto std_return;
1841
1842 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1845 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001846 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /*
1848 * Initially assume that the file does not exist and
1849 * reserve the resources for that case. If that is not
1850 * the case we'll drop the one we have and get a more
1851 * appropriate transaction later.
1852 */
1853 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1854 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1855 if (error == ENOSPC) {
1856 resblks = 0;
1857 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1858 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1859 }
1860 if (error) {
1861 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 goto error_return;
1863 }
1864
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001865 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001866 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 XFS_BMAP_INIT(&free_list, &first_block);
1869
1870 ASSERT(ip == NULL);
1871
1872 /*
1873 * Reserve disk quota and the inode.
1874 */
1875 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1876 if (error)
1877 goto error_return;
1878
Barry Naujok556b8b12008-04-10 12:22:07 +10001879 error = xfs_dir_canenter(tp, dp, name, resblks);
1880 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001882 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 rdev, credp, prid, resblks > 0,
1884 &ip, &committed);
1885 if (error) {
1886 if (error == ENOSPC)
1887 goto error_return;
1888 goto abort_return;
1889 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001890 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /*
1893 * At this point, we've gotten a newly allocated inode.
1894 * It is locked (and joined to the transaction).
1895 */
1896
1897 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1898
1899 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001900 * Now we join the directory inode to the transaction. We do not do it
1901 * earlier because xfs_dir_ialloc might commit the previous transaction
1902 * (and release all the locks). An error from here on will result in
1903 * the transaction cancel unlocking dp so don't do it explicitly in the
1904 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001906 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001908 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Barry Naujok556b8b12008-04-10 12:22:07 +10001910 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001911 &first_block, &free_list, resblks ?
1912 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (error) {
1914 ASSERT(error != ENOSPC);
1915 goto abort_return;
1916 }
1917 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1918 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1919
1920 /*
1921 * If this is a synchronous mount, make sure that the
1922 * create transaction goes to disk before returning to
1923 * the user.
1924 */
1925 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1926 xfs_trans_set_sync(tp);
1927 }
1928
1929 dp->i_gen++;
1930
1931 /*
1932 * Attach the dquot(s) to the inodes and modify them incore.
1933 * These ids of the inode couldn't have changed since the new
1934 * inode has been locked ever since it was created.
1935 */
1936 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1937
1938 /*
1939 * xfs_trans_commit normally decrements the vnode ref count
1940 * when it unlocks the inode. Since we want to return the
1941 * vnode to the caller, we bump the vnode ref count now.
1942 */
1943 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001945 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (error) {
1947 xfs_bmap_cancel(&free_list);
1948 goto abort_rele;
1949 }
1950
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001951 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (error) {
1953 IRELE(ip);
1954 tp = NULL;
1955 goto error_return;
1956 }
1957
1958 XFS_QM_DQRELE(mp, udqp);
1959 XFS_QM_DQRELE(mp, gdqp);
1960
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001961 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /* Fallthrough to std_return with error = 0 */
1964
1965std_return:
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001966 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10001967 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001969 dp, DM_RIGHT_NULL,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001970 *ipp ? ip : NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001971 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001972 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 return error;
1975
1976 abort_return:
1977 cancel_flags |= XFS_TRANS_ABORT;
1978 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Jesper Juhl014c2542006-01-15 02:37:08 +01001980 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (tp != NULL)
1982 xfs_trans_cancel(tp, cancel_flags);
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 XFS_QM_DQRELE(mp, udqp);
1985 XFS_QM_DQRELE(mp, gdqp);
1986
Christoph Hellwig993386c2007-08-28 16:12:30 +10001987 if (unlock_dp_on_error)
1988 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 goto std_return;
1991
1992 abort_rele:
1993 /*
1994 * Wait until after the current transaction is aborted to
1995 * release the inode. This prevents recursive transactions
1996 * and deadlocks from xfs_inactive.
1997 */
1998 cancel_flags |= XFS_TRANS_ABORT;
1999 xfs_trans_cancel(tp, cancel_flags);
2000 IRELE(ip);
2001
2002 XFS_QM_DQRELE(mp, udqp);
2003 XFS_QM_DQRELE(mp, gdqp);
2004
2005 goto std_return;
2006}
2007
2008#ifdef DEBUG
2009/*
2010 * Some counters to see if (and how often) we are hitting some deadlock
2011 * prevention code paths.
2012 */
2013
2014int xfs_rm_locks;
2015int xfs_rm_lock_delays;
2016int xfs_rm_attempts;
2017#endif
2018
2019/*
2020 * The following routine will lock the inodes associated with the
2021 * directory and the named entry in the directory. The locks are
2022 * acquired in increasing inode number.
2023 *
2024 * If the entry is "..", then only the directory is locked. The
2025 * vnode ref count will still include that from the .. entry in
2026 * this case.
2027 *
2028 * There is a deadlock we need to worry about. If the locked directory is
2029 * in the AIL, it might be blocking up the log. The next inode we lock
2030 * could be already locked by another thread waiting for log space (e.g
2031 * a permanent log reservation with a long running transaction (see
2032 * xfs_itruncate_finish)). To solve this, we must check if the directory
2033 * is in the ail and use lock_nowait. If we can't lock, we need to
2034 * drop the inode lock on the directory and try again. xfs_iunlock will
2035 * potentially push the tail if we were holding up the log.
2036 */
2037STATIC int
2038xfs_lock_dir_and_entry(
2039 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 xfs_inode_t *ip) /* inode of entry 'name' */
2041{
2042 int attempts;
2043 xfs_ino_t e_inum;
2044 xfs_inode_t *ips[2];
2045 xfs_log_item_t *lp;
2046
2047#ifdef DEBUG
2048 xfs_rm_locks++;
2049#endif
2050 attempts = 0;
2051
2052again:
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002053 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 e_inum = ip->i_ino;
2056
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002057 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 /*
2060 * We want to lock in increasing inum. Since we've already
2061 * acquired the lock on the directory, we may need to release
2062 * if if the inum of the entry turns out to be less.
2063 */
2064 if (e_inum > dp->i_ino) {
2065 /*
2066 * We are already in the right order, so just
2067 * lock on the inode of the entry.
2068 * We need to use nowait if dp is in the AIL.
2069 */
2070
2071 lp = (xfs_log_item_t *)dp->i_itemp;
2072 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2073 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2074 attempts++;
2075#ifdef DEBUG
2076 xfs_rm_attempts++;
2077#endif
2078
2079 /*
2080 * Unlock dp and try again.
2081 * xfs_iunlock will try to push the tail
2082 * if the inode is in the AIL.
2083 */
2084
2085 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2086
2087 if ((attempts % 5) == 0) {
2088 delay(1); /* Don't just spin the CPU */
2089#ifdef DEBUG
2090 xfs_rm_lock_delays++;
2091#endif
2092 }
2093 goto again;
2094 }
2095 } else {
2096 xfs_ilock(ip, XFS_ILOCK_EXCL);
2097 }
2098 } else if (e_inum < dp->i_ino) {
2099 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2100
2101 ips[0] = ip;
2102 ips[1] = dp;
2103 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2104 }
2105 /* else e_inum == dp->i_ino */
2106 /* This can happen if we're asked to lock /x/..
2107 * the entry is "..", which is also the parent directory.
2108 */
2109
2110 return 0;
2111}
2112
2113#ifdef DEBUG
2114int xfs_locked_n;
2115int xfs_small_retries;
2116int xfs_middle_retries;
2117int xfs_lots_retries;
2118int xfs_lock_delays;
2119#endif
2120
2121/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002122 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2123 * a different value
2124 */
2125static inline int
2126xfs_lock_inumorder(int lock_mode, int subclass)
2127{
2128 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002129 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002130 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002131 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002132
2133 return lock_mode;
2134}
2135
2136/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 * The following routine will lock n inodes in exclusive mode.
2138 * We assume the caller calls us with the inodes in i_ino order.
2139 *
2140 * We need to detect deadlock where an inode that we lock
2141 * is in the AIL and we start waiting for another inode that is locked
2142 * by a thread in a long running transaction (such as truncate). This can
2143 * result in deadlock since the long running trans might need to wait
2144 * for the inode we just locked in order to push the tail and free space
2145 * in the log.
2146 */
2147void
2148xfs_lock_inodes(
2149 xfs_inode_t **ips,
2150 int inodes,
2151 int first_locked,
2152 uint lock_mode)
2153{
2154 int attempts = 0, i, j, try_lock;
2155 xfs_log_item_t *lp;
2156
2157 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2158
2159 if (first_locked) {
2160 try_lock = 1;
2161 i = 1;
2162 } else {
2163 try_lock = 0;
2164 i = 0;
2165 }
2166
2167again:
2168 for (; i < inodes; i++) {
2169 ASSERT(ips[i]);
2170
2171 if (i && (ips[i] == ips[i-1])) /* Already locked */
2172 continue;
2173
2174 /*
2175 * If try_lock is not set yet, make sure all locked inodes
2176 * are not in the AIL.
2177 * If any are, set try_lock to be used later.
2178 */
2179
2180 if (!try_lock) {
2181 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2182 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2183 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2184 try_lock++;
2185 }
2186 }
2187 }
2188
2189 /*
2190 * If any of the previous locks we have locked is in the AIL,
2191 * we must TRY to get the second and subsequent locks. If
2192 * we can't get any, we must release all we have
2193 * and try again.
2194 */
2195
2196 if (try_lock) {
2197 /* try_lock must be 0 if i is 0. */
2198 /*
2199 * try_lock means we have an inode locked
2200 * that is in the AIL.
2201 */
2202 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002203 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 attempts++;
2205
2206 /*
2207 * Unlock all previous guys and try again.
2208 * xfs_iunlock will try to push the tail
2209 * if the inode is in the AIL.
2210 */
2211
2212 for(j = i - 1; j >= 0; j--) {
2213
2214 /*
2215 * Check to see if we've already
2216 * unlocked this one.
2217 * Not the first one going back,
2218 * and the inode ptr is the same.
2219 */
2220 if ((j != (i - 1)) && ips[j] ==
2221 ips[j+1])
2222 continue;
2223
2224 xfs_iunlock(ips[j], lock_mode);
2225 }
2226
2227 if ((attempts % 5) == 0) {
2228 delay(1); /* Don't just spin the CPU */
2229#ifdef DEBUG
2230 xfs_lock_delays++;
2231#endif
2232 }
2233 i = 0;
2234 try_lock = 0;
2235 goto again;
2236 }
2237 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002238 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
2240 }
2241
2242#ifdef DEBUG
2243 if (attempts) {
2244 if (attempts < 5) xfs_small_retries++;
2245 else if (attempts < 100) xfs_middle_retries++;
2246 else xfs_lots_retries++;
2247 } else {
2248 xfs_locked_n++;
2249 }
2250#endif
2251}
2252
2253#ifdef DEBUG
2254#define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2255int remove_which_error_return = 0;
2256#else /* ! DEBUG */
2257#define REMOVE_DEBUG_TRACE(x)
2258#endif /* ! DEBUG */
2259
Christoph Hellwig993386c2007-08-28 16:12:30 +10002260int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002262 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002263 struct xfs_name *name,
2264 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002266 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 xfs_trans_t *tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 int error = 0;
2269 xfs_bmap_free_t free_list;
2270 xfs_fsblock_t first_block;
2271 int cancel_flags;
2272 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 int link_zero;
2274 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002276 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (XFS_FORCED_SHUTDOWN(mp))
2279 return XFS_ERROR(EIO);
2280
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002281 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10002282 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
2283 NULL, DM_RIGHT_NULL, name->name, NULL,
2284 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (error)
2286 return error;
2287 }
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 /*
2290 * We need to get a reference to ip before we get our log
2291 * reservation. The reason for this is that we cannot call
2292 * xfs_iget for an inode for which we do not have a reference
2293 * once we've acquired a log reservation. This is because the
2294 * inode we are trying to get might be in xfs_inactive going
2295 * for a log reservation. Since we'll have to wait for the
2296 * inactive code to complete before returning from xfs_iget,
2297 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002298 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 * to the inode before getting our log reservation.
2300 */
Christoph Hellwig43973962008-03-06 13:44:50 +11002301 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002303 xfs_itrace_entry(ip);
2304 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 error = XFS_QM_DQATTACH(mp, dp, 0);
2307 if (!error && dp != ip)
2308 error = XFS_QM_DQATTACH(mp, ip, 0);
2309 if (error) {
2310 REMOVE_DEBUG_TRACE(__LINE__);
2311 IRELE(ip);
2312 goto std_return;
2313 }
2314
2315 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2316 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2317 /*
2318 * We try to get the real space reservation first,
2319 * allowing for directory btree deletion(s) implying
2320 * possible bmap insert(s). If we can't get the space
2321 * reservation then we use 0 instead, and avoid the bmap
2322 * btree insert(s) in the directory code by, if the bmap
2323 * insert tries to happen, instead trimming the LAST
2324 * block from the directory.
2325 */
2326 resblks = XFS_REMOVE_SPACE_RES(mp);
2327 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2328 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2329 if (error == ENOSPC) {
2330 resblks = 0;
2331 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2332 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2333 }
2334 if (error) {
2335 ASSERT(error != ENOSPC);
2336 REMOVE_DEBUG_TRACE(__LINE__);
2337 xfs_trans_cancel(tp, 0);
2338 IRELE(ip);
2339 return error;
2340 }
2341
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002342 error = xfs_lock_dir_and_entry(dp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (error) {
2344 REMOVE_DEBUG_TRACE(__LINE__);
2345 xfs_trans_cancel(tp, cancel_flags);
2346 IRELE(ip);
2347 goto std_return;
2348 }
2349
2350 /*
2351 * At this point, we've gotten both the directory and the entry
2352 * inodes locked.
2353 */
2354 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2355 if (dp != ip) {
2356 /*
2357 * Increment vnode ref count only in this case since
2358 * there's an extra vnode reference in the case where
2359 * dp == ip.
2360 */
2361 IHOLD(dp);
2362 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2363 }
2364
2365 /*
2366 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2367 */
2368 XFS_BMAP_INIT(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10002369 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002370 &first_block, &free_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (error) {
2372 ASSERT(error != ENOENT);
2373 REMOVE_DEBUG_TRACE(__LINE__);
2374 goto error1;
2375 }
2376 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2377
2378 dp->i_gen++;
2379 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2380
2381 error = xfs_droplink(tp, ip);
2382 if (error) {
2383 REMOVE_DEBUG_TRACE(__LINE__);
2384 goto error1;
2385 }
2386
2387 /* Determine if this is the last link while
2388 * we are in the transaction.
2389 */
2390 link_zero = (ip)->i_d.di_nlink==0;
2391
2392 /*
2393 * Take an extra ref on the inode so that it doesn't
2394 * go to xfs_inactive() from within the commit.
2395 */
2396 IHOLD(ip);
2397
2398 /*
2399 * If this is a synchronous mount, make sure that the
2400 * remove transaction goes to disk before returning to
2401 * the user.
2402 */
2403 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2404 xfs_trans_set_sync(tp);
2405 }
2406
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002407 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (error) {
2409 REMOVE_DEBUG_TRACE(__LINE__);
2410 goto error_rele;
2411 }
2412
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002413 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (error) {
2415 IRELE(ip);
2416 goto std_return;
2417 }
2418
2419 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002420 * If we are using filestreams, kill the stream association.
2421 * If the file is still open it may get a new one but that
2422 * will get killed on last close in xfs_close() so we don't
2423 * have to worry about that.
2424 */
2425 if (link_zero && xfs_inode_is_filestream(ip))
2426 xfs_filestream_deassociate(ip);
2427
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002428 xfs_itrace_exit(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 IRELE(ip);
2430
2431/* Fall through to std_return with error = 0 */
2432 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002433 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002435 dp, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002437 name->name, NULL, ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439 return error;
2440
2441 error1:
2442 xfs_bmap_cancel(&free_list);
2443 cancel_flags |= XFS_TRANS_ABORT;
2444 xfs_trans_cancel(tp, cancel_flags);
2445 goto std_return;
2446
2447 error_rele:
2448 /*
2449 * In this case make sure to not release the inode until after
2450 * the current transaction is aborted. Releasing it beforehand
2451 * can cause us to go to xfs_inactive and start a recursive
2452 * transaction which can easily deadlock with the current one.
2453 */
2454 xfs_bmap_cancel(&free_list);
2455 cancel_flags |= XFS_TRANS_ABORT;
2456 xfs_trans_cancel(tp, cancel_flags);
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 IRELE(ip);
2459
2460 goto std_return;
2461}
2462
Christoph Hellwig993386c2007-08-28 16:12:30 +10002463int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002465 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002466 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002467 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002469 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 xfs_inode_t *ips[2];
2472 int error;
2473 xfs_bmap_free_t free_list;
2474 xfs_fsblock_t first_block;
2475 int cancel_flags;
2476 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002479 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002480 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002482 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (XFS_FORCED_SHUTDOWN(mp))
2485 return XFS_ERROR(EIO);
2486
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002487 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002489 tdp, DM_RIGHT_NULL,
2490 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002491 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (error)
2493 return error;
2494 }
2495
2496 /* Return through std_return after this point. */
2497
2498 error = XFS_QM_DQATTACH(mp, sip, 0);
2499 if (!error && sip != tdp)
2500 error = XFS_QM_DQATTACH(mp, tdp, 0);
2501 if (error)
2502 goto std_return;
2503
2504 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2505 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002506 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2508 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2509 if (error == ENOSPC) {
2510 resblks = 0;
2511 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2512 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2513 }
2514 if (error) {
2515 cancel_flags = 0;
2516 goto error_return;
2517 }
2518
2519 if (sip->i_ino < tdp->i_ino) {
2520 ips[0] = sip;
2521 ips[1] = tdp;
2522 } else {
2523 ips[0] = tdp;
2524 ips[1] = sip;
2525 }
2526
2527 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2528
2529 /*
2530 * Increment vnode ref counts since xfs_trans_commit &
2531 * xfs_trans_cancel will both unlock the inodes and
2532 * decrement the associated ref counts.
2533 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002534 IHOLD(sip);
2535 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2537 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2538
2539 /*
2540 * If the source has too many links, we can't make any more to it.
2541 */
2542 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2543 error = XFS_ERROR(EMLINK);
2544 goto error_return;
2545 }
2546
Nathan Scott365ca832005-06-21 15:39:12 +10002547 /*
2548 * If we are using project inheritance, we only allow hard link
2549 * creation in our tree when the project IDs are the same; else
2550 * the tree quota mechanism could be circumvented.
2551 */
2552 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2553 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002554 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002555 goto error_return;
2556 }
2557
Barry Naujok556b8b12008-04-10 12:22:07 +10002558 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2559 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 goto error_return;
2561
2562 XFS_BMAP_INIT(&free_list, &first_block);
2563
Barry Naujok556b8b12008-04-10 12:22:07 +10002564 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2565 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (error)
2567 goto abort_return;
2568 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2569 tdp->i_gen++;
2570 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2571
2572 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002573 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 /*
2577 * If this is a synchronous mount, make sure that the
2578 * link transaction goes to disk before returning to
2579 * the user.
2580 */
2581 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2582 xfs_trans_set_sync(tp);
2583 }
2584
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002585 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (error) {
2587 xfs_bmap_cancel(&free_list);
2588 goto abort_return;
2589 }
2590
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002591 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002592 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 /* Fall through to std_return with error = 0. */
2596std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002597 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002599 tdp, DM_RIGHT_NULL,
2600 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002601 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603 return error;
2604
2605 abort_return:
2606 cancel_flags |= XFS_TRANS_ABORT;
2607 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 error_return:
2610 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 goto std_return;
2612}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002613
2614
Christoph Hellwig993386c2007-08-28 16:12:30 +10002615int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002617 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002618 struct xfs_name *dir_name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002619 mode_t mode,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002620 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 cred_t *credp)
2622{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002623 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 xfs_inode_t *cdp; /* inode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 int cancel_flags;
2627 int error;
2628 int committed;
2629 xfs_bmap_free_t free_list;
2630 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002631 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 boolean_t created = B_FALSE;
2633 int dm_event_sent = 0;
2634 xfs_prid_t prid;
2635 struct xfs_dquot *udqp, *gdqp;
2636 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 if (XFS_FORCED_SHUTDOWN(mp))
2639 return XFS_ERROR(EIO);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002643 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002645 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002646 DM_RIGHT_NULL, dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002647 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (error)
2649 return error;
2650 dm_event_sent = 1;
2651 }
2652
2653 /* Return through std_return after this point. */
2654
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002655 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 mp = dp->i_mount;
2658 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002659 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2660 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 else
2662 prid = (xfs_prid_t)dfltprid;
2663
2664 /*
2665 * Make sure that we have allocated dquot(s) on disk.
2666 */
2667 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002668 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2670 if (error)
2671 goto std_return;
2672
2673 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2674 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002675 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2677 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2678 if (error == ENOSPC) {
2679 resblks = 0;
2680 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2681 XFS_TRANS_PERM_LOG_RES,
2682 XFS_MKDIR_LOG_COUNT);
2683 }
2684 if (error) {
2685 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 goto error_return;
2687 }
2688
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002689 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002690 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
2692 /*
2693 * Check for directory link count overflow.
2694 */
2695 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2696 error = XFS_ERROR(EMLINK);
2697 goto error_return;
2698 }
2699
2700 /*
2701 * Reserve disk quota and the inode.
2702 */
2703 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2704 if (error)
2705 goto error_return;
2706
Barry Naujok556b8b12008-04-10 12:22:07 +10002707 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2708 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 goto error_return;
2710 /*
2711 * create the directory inode.
2712 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002713 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 0, credp, prid, resblks > 0,
2715 &cdp, NULL);
2716 if (error) {
2717 if (error == ENOSPC)
2718 goto error_return;
2719 goto abort_return;
2720 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002721 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 /*
2724 * Now we add the directory inode to the transaction.
2725 * We waited until now since xfs_dir_ialloc might start
2726 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002727 * earlier, the locks might have gotten released. An error
2728 * from here on will result in the transaction cancel
2729 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002731 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002733 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735 XFS_BMAP_INIT(&free_list, &first_block);
2736
Barry Naujok556b8b12008-04-10 12:22:07 +10002737 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2738 &first_block, &free_list, resblks ?
2739 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (error) {
2741 ASSERT(error != ENOSPC);
2742 goto error1;
2743 }
2744 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2745
2746 /*
2747 * Bump the in memory version number of the parent directory
2748 * so that other processes accessing it will recognize that
2749 * the directory has changed.
2750 */
2751 dp->i_gen++;
2752
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002753 error = xfs_dir_init(tp, cdp, dp);
2754 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 cdp->i_gen = 1;
2758 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002759 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 created = B_TRUE;
2763
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002764 *ipp = cdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 IHOLD(cdp);
2766
2767 /*
2768 * Attach the dquots to the new inode and modify the icount incore.
2769 */
2770 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2771
2772 /*
2773 * If this is a synchronous mount, make sure that the
2774 * mkdir transaction goes to disk before returning to
2775 * the user.
2776 */
2777 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2778 xfs_trans_set_sync(tp);
2779 }
2780
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002781 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (error) {
2783 IRELE(cdp);
2784 goto error2;
2785 }
2786
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002787 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 XFS_QM_DQRELE(mp, udqp);
2789 XFS_QM_DQRELE(mp, gdqp);
2790 if (error) {
2791 IRELE(cdp);
2792 }
2793
2794 /* Fall through to std_return with error = 0 or errno from
2795 * xfs_trans_commit. */
2796
2797std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002798 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002799 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002801 dp, DM_RIGHT_NULL,
2802 created ? cdp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002804 dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002805 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
2807 return error;
2808
2809 error2:
2810 error1:
2811 xfs_bmap_cancel(&free_list);
2812 abort_return:
2813 cancel_flags |= XFS_TRANS_ABORT;
2814 error_return:
2815 xfs_trans_cancel(tp, cancel_flags);
2816 XFS_QM_DQRELE(mp, udqp);
2817 XFS_QM_DQRELE(mp, gdqp);
2818
Christoph Hellwig993386c2007-08-28 16:12:30 +10002819 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 goto std_return;
2823}
2824
Christoph Hellwig993386c2007-08-28 16:12:30 +10002825int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826xfs_rmdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002827 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002828 struct xfs_name *name,
2829 xfs_inode_t *cdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002831 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002832 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 int error;
2835 xfs_bmap_free_t free_list;
2836 xfs_fsblock_t first_block;
2837 int cancel_flags;
2838 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 int last_cdp_link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 uint resblks;
2841
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002842 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Christoph Hellwig993386c2007-08-28 16:12:30 +10002844 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002847 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002849 dp, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002850 NULL, DM_RIGHT_NULL, name->name,
2851 NULL, cdp->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (error)
2853 return XFS_ERROR(error);
2854 }
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 /*
2857 * We need to get a reference to cdp before we get our log
2858 * reservation. The reason for this is that we cannot call
2859 * xfs_iget for an inode for which we do not have a reference
2860 * once we've acquired a log reservation. This is because the
2861 * inode we are trying to get might be in xfs_inactive going
2862 * for a log reservation. Since we'll have to wait for the
2863 * inactive code to complete before returning from xfs_iget,
2864 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002865 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 * to the inode before getting our log reservation.
2867 */
Christoph Hellwig43973962008-03-06 13:44:50 +11002868 IHOLD(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 /*
2871 * Get the dquots for the inodes.
2872 */
2873 error = XFS_QM_DQATTACH(mp, dp, 0);
2874 if (!error && dp != cdp)
2875 error = XFS_QM_DQATTACH(mp, cdp, 0);
2876 if (error) {
2877 IRELE(cdp);
2878 REMOVE_DEBUG_TRACE(__LINE__);
2879 goto std_return;
2880 }
2881
2882 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2883 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2884 /*
2885 * We try to get the real space reservation first,
2886 * allowing for directory btree deletion(s) implying
2887 * possible bmap insert(s). If we can't get the space
2888 * reservation then we use 0 instead, and avoid the bmap
2889 * btree insert(s) in the directory code by, if the bmap
2890 * insert tries to happen, instead trimming the LAST
2891 * block from the directory.
2892 */
2893 resblks = XFS_REMOVE_SPACE_RES(mp);
2894 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2895 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2896 if (error == ENOSPC) {
2897 resblks = 0;
2898 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2899 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2900 }
2901 if (error) {
2902 ASSERT(error != ENOSPC);
2903 cancel_flags = 0;
2904 IRELE(cdp);
2905 goto error_return;
2906 }
2907 XFS_BMAP_INIT(&free_list, &first_block);
2908
2909 /*
2910 * Now lock the child directory inode and the parent directory
2911 * inode in the proper order. This will take care of validating
2912 * that the directory entry for the child directory inode has
2913 * not changed while we were obtaining a log reservation.
2914 */
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002915 error = xfs_lock_dir_and_entry(dp, cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (error) {
2917 xfs_trans_cancel(tp, cancel_flags);
2918 IRELE(cdp);
2919 goto std_return;
2920 }
2921
2922 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2923 if (dp != cdp) {
2924 /*
2925 * Only increment the parent directory vnode count if
2926 * we didn't bump it in looking up cdp. The only time
2927 * we don't bump it is when we're looking up ".".
2928 */
2929 VN_HOLD(dir_vp);
2930 }
2931
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002932 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
2934
2935 ASSERT(cdp->i_d.di_nlink >= 2);
2936 if (cdp->i_d.di_nlink != 2) {
2937 error = XFS_ERROR(ENOTEMPTY);
2938 goto error_return;
2939 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002940 if (!xfs_dir_isempty(cdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 error = XFS_ERROR(ENOTEMPTY);
2942 goto error_return;
2943 }
2944
Barry Naujok556b8b12008-04-10 12:22:07 +10002945 error = xfs_dir_removename(tp, dp, name, cdp->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002946 &first_block, &free_list, resblks);
2947 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2951
2952 /*
2953 * Bump the in memory generation count on the parent
2954 * directory so that other can know that it has changed.
2955 */
2956 dp->i_gen++;
2957
2958 /*
2959 * Drop the link from cdp's "..".
2960 */
2961 error = xfs_droplink(tp, dp);
2962 if (error) {
2963 goto error1;
2964 }
2965
2966 /*
2967 * Drop the link from dp to cdp.
2968 */
2969 error = xfs_droplink(tp, cdp);
2970 if (error) {
2971 goto error1;
2972 }
2973
2974 /*
2975 * Drop the "." link from cdp to self.
2976 */
2977 error = xfs_droplink(tp, cdp);
2978 if (error) {
2979 goto error1;
2980 }
2981
2982 /* Determine these before committing transaction */
2983 last_cdp_link = (cdp)->i_d.di_nlink==0;
2984
2985 /*
2986 * Take an extra ref on the child vnode so that it
2987 * does not go to xfs_inactive() from within the commit.
2988 */
2989 IHOLD(cdp);
2990
2991 /*
2992 * If this is a synchronous mount, make sure that the
2993 * rmdir transaction goes to disk before returning to
2994 * the user.
2995 */
2996 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2997 xfs_trans_set_sync(tp);
2998 }
2999
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003000 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 if (error) {
3002 xfs_bmap_cancel(&free_list);
3003 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3004 XFS_TRANS_ABORT));
3005 IRELE(cdp);
3006 goto std_return;
3007 }
3008
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003009 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (error) {
3011 IRELE(cdp);
3012 goto std_return;
3013 }
3014
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 IRELE(cdp);
3017
3018 /* Fall through to std_return with error = 0 or the errno
3019 * from xfs_trans_commit. */
Nathan Scottbb19fba2006-03-22 14:12:12 +11003020 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003021 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003023 dp, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10003025 name->name, NULL, cdp->i_d.di_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 error, 0);
3027 }
3028 return error;
3029
Nathan Scottbb19fba2006-03-22 14:12:12 +11003030 error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 xfs_bmap_cancel(&free_list);
3032 cancel_flags |= XFS_TRANS_ABORT;
Jesper Juhl014c2542006-01-15 02:37:08 +01003033 /* FALLTHROUGH */
3034
Nathan Scottbb19fba2006-03-22 14:12:12 +11003035 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 xfs_trans_cancel(tp, cancel_flags);
3037 goto std_return;
3038}
3039
Christoph Hellwig993386c2007-08-28 16:12:30 +10003040int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003042 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10003043 struct xfs_name *link_name,
3044 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10003045 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11003046 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 cred_t *credp)
3048{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003049 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 xfs_inode_t *ip;
3052 int error;
3053 int pathlen;
3054 xfs_bmap_free_t free_list;
3055 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10003056 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 uint cancel_flags;
3058 int committed;
3059 xfs_fileoff_t first_fsb;
3060 xfs_filblks_t fs_blocks;
3061 int nmaps;
3062 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3063 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10003064 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 int byte_cnt;
3066 int n;
3067 xfs_buf_t *bp;
3068 xfs_prid_t prid;
3069 struct xfs_dquot *udqp, *gdqp;
3070 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Christoph Hellwig3937be52008-03-06 13:46:19 +11003072 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 error = 0;
3074 ip = NULL;
3075 tp = NULL;
3076
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003077 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 if (XFS_FORCED_SHUTDOWN(mp))
3080 return XFS_ERROR(EIO);
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 /*
3083 * Check component lengths of the target path name.
3084 */
3085 pathlen = strlen(target_path);
3086 if (pathlen >= MAXPATHLEN) /* total string too long */
3087 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003089 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003090 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10003092 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (error)
3094 return error;
3095 }
3096
3097 /* Return through std_return after this point. */
3098
3099 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10003100 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3101 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 else
3103 prid = (xfs_prid_t)dfltprid;
3104
3105 /*
3106 * Make sure that we have allocated dquot(s) on disk.
3107 */
3108 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10003109 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3111 if (error)
3112 goto std_return;
3113
3114 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3115 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3116 /*
3117 * The symlink will fit into the inode data fork?
3118 * There can't be any attributes so we get the whole variable part.
3119 */
3120 if (pathlen <= XFS_LITINO(mp))
3121 fs_blocks = 0;
3122 else
3123 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10003124 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3126 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3127 if (error == ENOSPC && fs_blocks == 0) {
3128 resblks = 0;
3129 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3130 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3131 }
3132 if (error) {
3133 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 goto error_return;
3135 }
3136
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10003137 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10003138 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140 /*
3141 * Check whether the directory allows new symlinks or not.
3142 */
3143 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3144 error = XFS_ERROR(EPERM);
3145 goto error_return;
3146 }
3147
3148 /*
3149 * Reserve disk quota : blocks and inode.
3150 */
3151 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3152 if (error)
3153 goto error_return;
3154
3155 /*
3156 * Check for ability to enter directory entry, if no space reserved.
3157 */
Barry Naujok556b8b12008-04-10 12:22:07 +10003158 error = xfs_dir_canenter(tp, dp, link_name, resblks);
3159 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 goto error_return;
3161 /*
3162 * Initialize the bmap freelist prior to calling either
3163 * bmapi or the directory create code.
3164 */
3165 XFS_BMAP_INIT(&free_list, &first_block);
3166
3167 /*
3168 * Allocate an inode for the symlink.
3169 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10003170 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 1, 0, credp, prid, resblks > 0, &ip, NULL);
3172 if (error) {
3173 if (error == ENOSPC)
3174 goto error_return;
3175 goto error1;
3176 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003177 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Christoph Hellwig993386c2007-08-28 16:12:30 +10003179 /*
3180 * An error after we've joined dp to the transaction will result in the
3181 * transaction cancel unlocking dp so don't do it explicitly in the
3182 * error path.
3183 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11003184 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10003186 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
3188 /*
3189 * Also attach the dquot(s) to it, if applicable.
3190 */
3191 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3192
3193 if (resblks)
3194 resblks -= XFS_IALLOC_SPACE_RES(mp);
3195 /*
3196 * If the symlink will fit into the inode, write it inline.
3197 */
3198 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3199 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3200 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3201 ip->i_d.di_size = pathlen;
3202
3203 /*
3204 * The inode was initially created in extent format.
3205 */
3206 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3207 ip->i_df.if_flags |= XFS_IFINLINE;
3208
3209 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3210 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3211
3212 } else {
3213 first_fsb = 0;
3214 nmaps = SYMLINK_MAPS;
3215
3216 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3217 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3218 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003219 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (error) {
3221 goto error1;
3222 }
3223
3224 if (resblks)
3225 resblks -= fs_blocks;
3226 ip->i_d.di_size = pathlen;
3227 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3228
3229 cur_chunk = target_path;
3230 for (n = 0; n < nmaps; n++) {
3231 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3232 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3233 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3234 BTOBB(byte_cnt), 0);
3235 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3236 if (pathlen < byte_cnt) {
3237 byte_cnt = pathlen;
3238 }
3239 pathlen -= byte_cnt;
3240
3241 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3242 cur_chunk += byte_cnt;
3243
3244 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3245 }
3246 }
3247
3248 /*
3249 * Create the directory entry for the symlink.
3250 */
Barry Naujok556b8b12008-04-10 12:22:07 +10003251 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
3252 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003253 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3256 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3257
3258 /*
3259 * Bump the in memory version number of the parent directory
3260 * so that other processes accessing it will recognize that
3261 * the directory has changed.
3262 */
3263 dp->i_gen++;
3264
3265 /*
3266 * If this is a synchronous mount, make sure that the
3267 * symlink transaction goes to disk before returning to
3268 * the user.
3269 */
3270 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3271 xfs_trans_set_sync(tp);
3272 }
3273
3274 /*
3275 * xfs_trans_commit normally decrements the vnode ref count
3276 * when it unlocks the inode. Since we want to return the
3277 * vnode to the caller, we bump the vnode ref count now.
3278 */
3279 IHOLD(ip);
3280
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003281 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 if (error) {
3283 goto error2;
3284 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003285 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 XFS_QM_DQRELE(mp, udqp);
3287 XFS_QM_DQRELE(mp, gdqp);
3288
3289 /* Fall through to std_return with error = 0 or errno from
3290 * xfs_trans_commit */
3291std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10003292 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003294 dp, DM_RIGHT_NULL,
3295 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10003296 DM_RIGHT_NULL, link_name->name,
3297 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
3299
Christoph Hellwig3937be52008-03-06 13:46:19 +11003300 if (!error)
3301 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return error;
3303
3304 error2:
3305 IRELE(ip);
3306 error1:
3307 xfs_bmap_cancel(&free_list);
3308 cancel_flags |= XFS_TRANS_ABORT;
3309 error_return:
3310 xfs_trans_cancel(tp, cancel_flags);
3311 XFS_QM_DQRELE(mp, udqp);
3312 XFS_QM_DQRELE(mp, gdqp);
3313
Christoph Hellwig993386c2007-08-28 16:12:30 +10003314 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 goto std_return;
3318}
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003322 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 int flags)
3324{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003325 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 int error = 0;
3327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 if (XFS_FORCED_SHUTDOWN(mp))
3329 return XFS_ERROR(EIO);
3330
3331 /*
3332 * Bypass inodes which have already been cleaned by
3333 * the inode flush clustering code inside xfs_iflush
3334 */
David Chinner33540402008-03-06 13:43:59 +11003335 if (xfs_inode_clean(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return 0;
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 /*
3339 * We make this non-blocking if the inode is contended,
3340 * return EAGAIN to indicate to the caller that they
3341 * did not succeed. This prevents the flush path from
3342 * blocking on inodes inside another operation right
3343 * now, they get caught later by xfs_sync.
3344 */
David Chinnera3f74ff2008-03-06 13:43:42 +11003345 if (flags & FLUSH_SYNC) {
3346 xfs_ilock(ip, XFS_ILOCK_SHARED);
3347 xfs_iflock(ip);
3348 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3349 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3350 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return EAGAIN;
3352 }
David Chinnera3f74ff2008-03-06 13:43:42 +11003353 } else {
3354 return EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
3356
David Chinnera3f74ff2008-03-06 13:43:42 +11003357 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
3358 : XFS_IFLUSH_ASYNC_NOBLOCK);
3359 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return error;
3362}
3363
Christoph Hellwig993386c2007-08-28 16:12:30 +10003364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365int
Christoph Hellwig993386c2007-08-28 16:12:30 +10003366xfs_set_dmattrs(
3367 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10003369 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003371 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 int error;
3374
3375 if (!capable(CAP_SYS_ADMIN))
3376 return XFS_ERROR(EPERM);
3377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 if (XFS_FORCED_SHUTDOWN(mp))
3379 return XFS_ERROR(EIO);
3380
3381 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3382 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3383 if (error) {
3384 xfs_trans_cancel(tp, 0);
3385 return error;
3386 }
3387 xfs_ilock(ip, XFS_ILOCK_EXCL);
3388 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3389
Christoph Hellwig613d7042007-10-11 17:44:08 +10003390 ip->i_d.di_dmevmask = evmask;
3391 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3394 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003395 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 return error;
3398}
3399
Christoph Hellwig993386c2007-08-28 16:12:30 +10003400int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003402 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003404 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003406 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408 ASSERT(!VN_MAPPED(vp));
3409
3410 /* bad inode, get out here ASAP */
3411 if (VN_BAD(vp)) {
3412 xfs_ireclaim(ip);
3413 return 0;
3414 }
3415
Christoph Hellwigb677c212007-08-29 11:46:28 +10003416 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10003418 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11003420 /*
3421 * Make sure the atime in the XFS inode is correct before freeing the
3422 * Linux inode.
3423 */
3424 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
David Chinner4c606582006-11-11 18:05:00 +11003426 /*
3427 * If we have nothing to flush with this inode then complete the
3428 * teardown now, otherwise break the link between the xfs inode and the
3429 * linux inode and clean up the xfs inode later. This avoids flushing
3430 * the inode to disk during the delete operation itself.
3431 *
3432 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3433 * first to ensure that xfs_iunpin() will never see an xfs inode
3434 * that has a linux inode being reclaimed. Synchronisation is provided
3435 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 */
3437 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3438 xfs_ilock(ip, XFS_ILOCK_EXCL);
3439 xfs_iflock(ip);
3440 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3441 } else {
3442 xfs_mount_t *mp = ip->i_mount;
3443
David Chinner4c606582006-11-11 18:05:00 +11003444 /* Protect sync and unpin from us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 XFS_MOUNT_ILOCK(mp);
David Chinner4c606582006-11-11 18:05:00 +11003446 spin_lock(&ip->i_flags_lock);
3447 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10003448 vn_to_inode(vp)->i_private = NULL;
3449 ip->i_vnode = NULL;
David Chinner4c606582006-11-11 18:05:00 +11003450 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 XFS_MOUNT_IUNLOCK(mp);
3453 }
3454 return 0;
3455}
3456
3457int
3458xfs_finish_reclaim(
3459 xfs_inode_t *ip,
3460 int locked,
3461 int sync_mode)
3462{
David Chinnerda353b02007-08-28 14:00:13 +10003463 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
Nathan Scott67fcaa72006-06-09 17:00:52 +10003464 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 int error;
3466
3467 if (vp && VN_BAD(vp))
3468 goto reclaim;
3469
3470 /* The hash lock here protects a thread in xfs_iget_core from
3471 * racing with us on linking the inode back with a vnode.
3472 * Once we have the XFS_IRECLAIM flag set it will not touch
3473 * us.
3474 */
David Chinnerda353b02007-08-28 14:00:13 +10003475 write_lock(&pag->pag_ici_lock);
David Chinnerf273ab82006-09-28 11:06:03 +10003476 spin_lock(&ip->i_flags_lock);
David Chinner7a18c382006-11-11 18:04:54 +11003477 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3478 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
David Chinnerf273ab82006-09-28 11:06:03 +10003479 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003480 write_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 if (locked) {
3482 xfs_ifunlock(ip);
3483 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3484 }
Jesper Juhl014c2542006-01-15 02:37:08 +01003485 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 }
David Chinner7a18c382006-11-11 18:04:54 +11003487 __xfs_iflags_set(ip, XFS_IRECLAIM);
David Chinnerf273ab82006-09-28 11:06:03 +10003488 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003489 write_unlock(&pag->pag_ici_lock);
3490 xfs_put_perag(ip->i_mount, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 /*
3493 * If the inode is still dirty, then flush it out. If the inode
3494 * is not in the AIL, then it will be OK to flush it delwri as
3495 * long as xfs_iflush() does not keep any references to the inode.
3496 * We leave that decision up to xfs_iflush() since it has the
3497 * knowledge of whether it's OK to simply do a delwri flush of
3498 * the inode or whether we need to wait until the inode is
3499 * pulled from the AIL.
3500 * We get the flush lock regardless, though, just to make sure
3501 * we don't free it while it is being flushed.
3502 */
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003503 if (!locked) {
3504 xfs_ilock(ip, XFS_ILOCK_EXCL);
3505 xfs_iflock(ip);
3506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003508 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 if (ip->i_update_core ||
3510 ((ip->i_itemp != NULL) &&
3511 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3512 error = xfs_iflush(ip, sync_mode);
3513 /*
3514 * If we hit an error, typically because of filesystem
3515 * shutdown, we don't need to let vn_reclaim to know
3516 * because we're gonna reclaim the inode anyway.
3517 */
3518 if (error) {
3519 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3520 goto reclaim;
3521 }
3522 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3523 }
3524
3525 ASSERT(ip->i_update_core == 0);
3526 ASSERT(ip->i_itemp == NULL ||
3527 ip->i_itemp->ili_format.ilf_fields == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
3529
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003530 xfs_ifunlock(ip);
3531 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 reclaim:
3534 xfs_ireclaim(ip);
3535 return 0;
3536}
3537
3538int
3539xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3540{
3541 int purged;
3542 xfs_inode_t *ip, *n;
3543 int done = 0;
3544
3545 while (!done) {
3546 purged = 0;
3547 XFS_MOUNT_ILOCK(mp);
3548 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3549 if (noblock) {
3550 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3551 continue;
3552 if (xfs_ipincount(ip) ||
3553 !xfs_iflock_nowait(ip)) {
3554 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3555 continue;
3556 }
3557 }
3558 XFS_MOUNT_IUNLOCK(mp);
Felix Blyakher6b2cf612005-11-25 16:42:13 +11003559 if (xfs_finish_reclaim(ip, noblock,
3560 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3561 delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 purged = 1;
3563 break;
3564 }
3565
3566 done = !purged;
3567 }
3568
3569 XFS_MOUNT_IUNLOCK(mp);
3570 return 0;
3571}
3572
3573/*
3574 * xfs_alloc_file_space()
3575 * This routine allocates disk space for the given file.
3576 *
3577 * If alloc_type == 0, this request is for an ALLOCSP type
3578 * request which will change the file size. In this case, no
3579 * DMAPI event will be generated by the call. A TRUNCATE event
3580 * will be generated later by xfs_setattr.
3581 *
3582 * If alloc_type != 0, this request is for a RESVSP type
3583 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3584 * lower block boundary byte address is less than the file's
3585 * length.
3586 *
3587 * RETURNS:
3588 * 0 on success
3589 * errno on error
3590 *
3591 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003592STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593xfs_alloc_file_space(
3594 xfs_inode_t *ip,
3595 xfs_off_t offset,
3596 xfs_off_t len,
3597 int alloc_type,
3598 int attr_flags)
3599{
Nathan Scottdd9f4382006-01-11 15:28:28 +11003600 xfs_mount_t *mp = ip->i_mount;
3601 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 xfs_filblks_t allocated_fsb;
3603 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003604 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003606 xfs_fsblock_t firstfsb;
3607 int nimaps;
3608 int bmapi_flag;
3609 int quota_flag;
3610 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003612 xfs_bmbt_irec_t imaps[1], *imapp;
3613 xfs_bmap_free_t free_list;
3614 uint qblocks, resblks, resrtextents;
3615 int committed;
3616 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003618 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 if (XFS_FORCED_SHUTDOWN(mp))
3621 return XFS_ERROR(EIO);
3622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3624 return error;
3625
3626 if (len <= 0)
3627 return XFS_ERROR(EINVAL);
3628
David Chinner957d0eb2007-06-18 16:50:37 +10003629 rt = XFS_IS_REALTIME_INODE(ip);
3630 extsz = xfs_get_extsz_hint(ip);
3631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11003634 nimaps = 1;
3635 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3637 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3638
3639 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003640 if (alloc_type != 0 && offset < ip->i_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 (attr_flags&ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003642 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 xfs_off_t end_dmi_offset;
3644
3645 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003646 if (end_dmi_offset > ip->i_size)
3647 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003648 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
3649 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003651 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 }
3653
3654 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003655 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 */
3657retry:
3658 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003659 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Nathan Scottdd9f4382006-01-11 15:28:28 +11003661 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003662 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11003663 */
3664 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003666 do_div(s, extsz);
3667 s *= extsz;
3668 e = startoffset_fsb + allocatesize_fsb;
3669 if ((temp = do_mod(startoffset_fsb, extsz)))
3670 e += temp;
3671 if ((temp = do_mod(e, extsz)))
3672 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003674 s = 0;
3675 e = allocatesize_fsb;
3676 }
3677
3678 if (unlikely(rt)) {
3679 resrtextents = qblocks = (uint)(e - s);
3680 resrtextents /= mp->m_sb.sb_rextsize;
3681 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3682 quota_flag = XFS_QMOPT_RES_RTBLKS;
3683 } else {
3684 resrtextents = 0;
3685 resblks = qblocks = \
3686 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3687 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 }
3689
3690 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003691 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 */
3693 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003694 error = xfs_trans_reserve(tp, resblks,
3695 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 XFS_TRANS_PERM_LOG_RES,
3697 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003699 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 */
3701 if (error) {
3702 /*
3703 * Free the transaction structure.
3704 */
3705 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3706 xfs_trans_cancel(tp, 0);
3707 break;
3708 }
3709 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003710 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3711 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 if (error)
3713 goto error1;
3714
3715 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3716 xfs_trans_ihold(tp, ip);
3717
3718 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003719 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 */
3721 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003722 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003723 allocatesize_fsb, bmapi_flag,
3724 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003725 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 if (error) {
3727 goto error0;
3728 }
3729
3730 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003731 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003733 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 if (error) {
3735 goto error0;
3736 }
3737
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003738 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3740 if (error) {
3741 break;
3742 }
3743
3744 allocated_fsb = imapp->br_blockcount;
3745
Nathan Scottdd9f4382006-01-11 15:28:28 +11003746 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 error = XFS_ERROR(ENOSPC);
3748 break;
3749 }
3750
3751 startoffset_fsb += allocated_fsb;
3752 allocatesize_fsb -= allocated_fsb;
3753 }
3754dmapi_enospc_check:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003755 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
3756 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003758 ip, DM_RIGHT_NULL,
3759 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3761 if (error == 0)
3762 goto retry; /* Maybe DMAPI app. has made space */
3763 /* else fall through with error from XFS_SEND_DATA */
3764 }
3765
3766 return error;
3767
Nathan Scottdd9f4382006-01-11 15:28:28 +11003768error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003770 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3771
3772error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3774 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3775 goto dmapi_enospc_check;
3776}
3777
3778/*
3779 * Zero file bytes between startoff and endoff inclusive.
3780 * The iolock is held exclusive and no blocks are buffered.
3781 */
3782STATIC int
3783xfs_zero_remaining_bytes(
3784 xfs_inode_t *ip,
3785 xfs_off_t startoff,
3786 xfs_off_t endoff)
3787{
3788 xfs_bmbt_irec_t imap;
3789 xfs_fileoff_t offset_fsb;
3790 xfs_off_t lastoffset;
3791 xfs_off_t offset;
3792 xfs_buf_t *bp;
3793 xfs_mount_t *mp = ip->i_mount;
3794 int nimap;
3795 int error = 0;
3796
3797 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003798 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 mp->m_rtdev_targp : mp->m_ddev_targp);
3800
3801 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3802 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3803 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003804 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003805 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 if (error || nimap < 1)
3807 break;
3808 ASSERT(imap.br_blockcount >= 1);
3809 ASSERT(imap.br_startoff == offset_fsb);
3810 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3811 if (lastoffset > endoff)
3812 lastoffset = endoff;
3813 if (imap.br_startblock == HOLESTARTBLOCK)
3814 continue;
3815 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3816 if (imap.br_state == XFS_EXT_UNWRITTEN)
3817 continue;
3818 XFS_BUF_UNDONE(bp);
3819 XFS_BUF_UNWRITE(bp);
3820 XFS_BUF_READ(bp);
3821 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3822 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003823 error = xfs_iowait(bp);
3824 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3826 mp, bp, XFS_BUF_ADDR(bp));
3827 break;
3828 }
3829 memset(XFS_BUF_PTR(bp) +
3830 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3831 0, lastoffset - offset + 1);
3832 XFS_BUF_UNDONE(bp);
3833 XFS_BUF_UNREAD(bp);
3834 XFS_BUF_WRITE(bp);
3835 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003836 error = xfs_iowait(bp);
3837 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3839 mp, bp, XFS_BUF_ADDR(bp));
3840 break;
3841 }
3842 }
3843 xfs_buf_free(bp);
3844 return error;
3845}
3846
3847/*
3848 * xfs_free_file_space()
3849 * This routine frees disk space for the given file.
3850 *
3851 * This routine is only called by xfs_change_file_space
3852 * for an UNRESVSP type call.
3853 *
3854 * RETURNS:
3855 * 0 on success
3856 * errno on error
3857 *
3858 */
3859STATIC int
3860xfs_free_file_space(
3861 xfs_inode_t *ip,
3862 xfs_off_t offset,
3863 xfs_off_t len,
3864 int attr_flags)
3865{
Nathan Scott67fcaa72006-06-09 17:00:52 +10003866 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 int committed;
3868 int done;
3869 xfs_off_t end_dmi_offset;
3870 xfs_fileoff_t endoffset_fsb;
3871 int error;
3872 xfs_fsblock_t firstfsb;
3873 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 xfs_bmbt_irec_t imap;
3875 xfs_off_t ioffset;
3876 xfs_extlen_t mod=0;
3877 xfs_mount_t *mp;
3878 int nimap;
3879 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10003880 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 int rt;
3882 xfs_fileoff_t startoffset_fsb;
3883 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003884 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003886 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 mp = ip->i_mount;
3888
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003889 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3892 return error;
3893
3894 error = 0;
3895 if (len <= 0) /* if nothing being freed */
3896 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003897 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3899 end_dmi_offset = offset + len;
3900 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3901
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003902 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
3903 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003904 if (end_dmi_offset > ip->i_size)
3905 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003906 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 offset, end_dmi_offset - offset,
3908 AT_DELAY_FLAG(attr_flags), NULL);
3909 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003910 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 }
3912
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003913 if (attr_flags & ATTR_NOLOCK)
3914 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11003915 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10003917 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11003918 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003919
Tim Shimmine6a4b372007-11-23 16:30:42 +11003920 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003922
3923 if (VN_CACHED(vp) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003924 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3925 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10003926 if (error)
3927 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003928 }
3929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 /*
3931 * Need to zero the stuff we're not freeing, on disk.
3932 * If its a realtime file & can't use unwritten extents then we
3933 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3934 * will take care of it for us.
3935 */
Eric Sandeen62118702008-03-06 13:44:28 +11003936 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003938 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003939 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 if (error)
3941 goto out_unlock_iolock;
3942 ASSERT(nimap == 0 || nimap == 1);
3943 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3944 xfs_daddr_t block;
3945
3946 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3947 block = imap.br_startblock;
3948 mod = do_div(block, mp->m_sb.sb_rextsize);
3949 if (mod)
3950 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3951 }
3952 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003953 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003954 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (error)
3956 goto out_unlock_iolock;
3957 ASSERT(nimap == 0 || nimap == 1);
3958 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3959 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3960 mod++;
3961 if (mod && (mod != mp->m_sb.sb_rextsize))
3962 endoffset_fsb -= mod;
3963 }
3964 }
3965 if ((done = (endoffset_fsb <= startoffset_fsb)))
3966 /*
3967 * One contiguous piece to clear
3968 */
3969 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3970 else {
3971 /*
3972 * Some full blocks, possibly two pieces to clear
3973 */
3974 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3975 error = xfs_zero_remaining_bytes(ip, offset,
3976 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3977 if (!error &&
3978 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3979 error = xfs_zero_remaining_bytes(ip,
3980 XFS_FSB_TO_B(mp, endoffset_fsb),
3981 offset + len - 1);
3982 }
3983
3984 /*
3985 * free file space until done or until there is an error
3986 */
3987 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3988 while (!error && !done) {
3989
3990 /*
David Chinner91ebecc2007-07-19 16:28:30 +10003991 * allocate and setup the transaction. Allow this
3992 * transaction to dip into the reserve blocks to ensure
3993 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 */
3995 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10003996 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 error = xfs_trans_reserve(tp,
3998 resblks,
3999 XFS_WRITE_LOG_RES(mp),
4000 0,
4001 XFS_TRANS_PERM_LOG_RES,
4002 XFS_WRITE_LOG_COUNT);
4003
4004 /*
4005 * check for running out of space
4006 */
4007 if (error) {
4008 /*
4009 * Free the transaction structure.
4010 */
4011 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4012 xfs_trans_cancel(tp, 0);
4013 break;
4014 }
4015 xfs_ilock(ip, XFS_ILOCK_EXCL);
4016 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004017 ip->i_udquot, ip->i_gdquot, resblks, 0,
4018 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 if (error)
4020 goto error1;
4021
4022 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4023 xfs_trans_ihold(tp, ip);
4024
4025 /*
4026 * issue the bunmapi() call to free the blocks
4027 */
4028 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004029 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004031 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 if (error) {
4033 goto error0;
4034 }
4035
4036 /*
4037 * complete the transaction
4038 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004039 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 if (error) {
4041 goto error0;
4042 }
4043
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004044 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4046 }
4047
4048 out_unlock_iolock:
4049 if (need_iolock)
4050 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4051 return error;
4052
4053 error0:
4054 xfs_bmap_cancel(&free_list);
4055 error1:
4056 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4057 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4058 XFS_ILOCK_EXCL);
4059 return error;
4060}
4061
4062/*
4063 * xfs_change_file_space()
4064 * This routine allocates or frees disk space for the given file.
4065 * The user specified parameters are checked for alignment and size
4066 * limitations.
4067 *
4068 * RETURNS:
4069 * 0 on success
4070 * errno on error
4071 *
4072 */
4073int
4074xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10004075 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 int cmd,
4077 xfs_flock64_t *bf,
4078 xfs_off_t offset,
4079 cred_t *credp,
4080 int attr_flags)
4081{
Christoph Hellwig993386c2007-08-28 16:12:30 +10004082 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 int clrprealloc;
4084 int error;
4085 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 int setprealloc;
4087 xfs_off_t startoffset;
4088 xfs_off_t llen;
4089 xfs_trans_t *tp;
Nathan Scott8285fb52006-06-09 17:07:12 +10004090 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11004092 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Christoph Hellwig993386c2007-08-28 16:12:30 +10004094 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 return XFS_ERROR(EINVAL);
4096
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 switch (bf->l_whence) {
4098 case 0: /*SEEK_SET*/
4099 break;
4100 case 1: /*SEEK_CUR*/
4101 bf->l_start += offset;
4102 break;
4103 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004104 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 break;
4106 default:
4107 return XFS_ERROR(EINVAL);
4108 }
4109
4110 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4111
4112 if ( (bf->l_start < 0)
4113 || (bf->l_start > XFS_MAXIOFFSET(mp))
4114 || (bf->l_start + llen < 0)
4115 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4116 return XFS_ERROR(EINVAL);
4117
4118 bf->l_whence = 0;
4119
4120 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004121 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
4123 /*
4124 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4125 * file space.
4126 * These calls do NOT zero the data space allocated to the file,
4127 * nor do they change the file size.
4128 *
4129 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4130 * space.
4131 * These calls cause the new file data to be zeroed and the file
4132 * size to be changed.
4133 */
4134 setprealloc = clrprealloc = 0;
4135
4136 switch (cmd) {
4137 case XFS_IOC_RESVSP:
4138 case XFS_IOC_RESVSP64:
4139 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4140 1, attr_flags);
4141 if (error)
4142 return error;
4143 setprealloc = 1;
4144 break;
4145
4146 case XFS_IOC_UNRESVSP:
4147 case XFS_IOC_UNRESVSP64:
4148 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4149 attr_flags)))
4150 return error;
4151 break;
4152
4153 case XFS_IOC_ALLOCSP:
4154 case XFS_IOC_ALLOCSP64:
4155 case XFS_IOC_FREESP:
4156 case XFS_IOC_FREESP64:
4157 if (startoffset > fsize) {
4158 error = xfs_alloc_file_space(ip, fsize,
4159 startoffset - fsize, 0, attr_flags);
4160 if (error)
4161 break;
4162 }
4163
4164 va.va_mask = XFS_AT_SIZE;
4165 va.va_size = startoffset;
4166
Christoph Hellwig993386c2007-08-28 16:12:30 +10004167 error = xfs_setattr(ip, &va, attr_flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169 if (error)
4170 return error;
4171
4172 clrprealloc = 1;
4173 break;
4174
4175 default:
4176 ASSERT(0);
4177 return XFS_ERROR(EINVAL);
4178 }
4179
4180 /*
4181 * update the inode timestamp, mode, and prealloc flag bits
4182 */
4183 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4184
4185 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4186 0, 0, 0))) {
4187 /* ASSERT(0); */
4188 xfs_trans_cancel(tp, 0);
4189 return error;
4190 }
4191
4192 xfs_ilock(ip, XFS_ILOCK_EXCL);
4193
4194 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4195 xfs_trans_ihold(tp, ip);
4196
4197 if ((attr_flags & ATTR_DMI) == 0) {
4198 ip->i_d.di_mode &= ~S_ISUID;
4199
4200 /*
4201 * Note that we don't have to worry about mandatory
4202 * file locking being disabled here because we only
4203 * clear the S_ISGID bit if the Group execute bit is
4204 * on, but if it was on then mandatory locking wouldn't
4205 * have been enabled.
4206 */
4207 if (ip->i_d.di_mode & S_IXGRP)
4208 ip->i_d.di_mode &= ~S_ISGID;
4209
4210 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4211 }
4212 if (setprealloc)
4213 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4214 else if (clrprealloc)
4215 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4216
4217 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4218 xfs_trans_set_sync(tp);
4219
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004220 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
4222 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4223
4224 return error;
4225}