blob: a42d7fe6a5e89715b452ddeeceb7305aba29ab1a [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 Hellwig993386c12007-08-28 16:12:30 +100054#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Christoph Hellwig993386c12007-08-28 16:12:30 +100056int
Linus Torvalds1da177e2005-04-16 15:20:36 -070057xfs_open(
Christoph Hellwig993386c12007-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 Hellwig993386c12007-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 Hellwig993386c12007-08-28 16:12:30 +100081int
Linus Torvalds1da177e2005-04-16 15:20:36 -070082xfs_getattr(
Christoph Hellwig993386c12007-08-28 16:12:30 +100083 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +100084 bhv_vattr_t *vap,
Christoph Hellwig993386c12007-08-28 16:12:30 +100085 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Christoph Hellwig993386c12007-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 Hellwig993386c12007-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 Hellwig993386c12007-08-28 16:12:30 +1000214 bhv_vnode_t *vp = XFS_ITOV(ip);
215 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int mask;
218 int code;
219 uint lock_flags;
220 uint commit_flags=0;
221 uid_t uid=0, iuid=0;
222 gid_t gid=0, igid=0;
223 int timeflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 xfs_prid_t projid=0, iprojid=0;
225 int mandlock_before, mandlock_after;
226 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
227 int file_owner;
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700228 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100230 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000232 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return XFS_ERROR(EROFS);
234
235 /*
236 * Cannot set certain attributes.
237 */
238 mask = vap->va_mask;
239 if (mask & XFS_AT_NOSET) {
240 return XFS_ERROR(EINVAL);
241 }
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (XFS_FORCED_SHUTDOWN(mp))
244 return XFS_ERROR(EIO);
245
246 /*
247 * Timestamps do not need to be logged and hence do not
248 * need to be done within a transaction.
249 */
250 if (mask & XFS_AT_UPDTIMES) {
251 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
252 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
253 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
254 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
255 xfs_ichgtime(ip, timeflags);
256 return 0;
257 }
258
259 olddquot1 = olddquot2 = NULL;
260 udqp = gdqp = NULL;
261
262 /*
263 * If disk quotas is on, we make sure that the dquots do exist on disk,
264 * before we start any other transactions. Trying to do this later
265 * is messy. We don't care to take a readlock to look at the ids
266 * in inode here, because we can't hold it across the trans_reserve.
267 * If the IDs do change before we take the ilock, we're covered
268 * because the i_*dquot fields will get updated anyway.
269 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000270 if (XFS_IS_QUOTA_ON(mp) &&
271 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 uint qflags = 0;
273
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000274 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 uid = vap->va_uid;
276 qflags |= XFS_QMOPT_UQUOTA;
277 } else {
278 uid = ip->i_d.di_uid;
279 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000280 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 gid = vap->va_gid;
282 qflags |= XFS_QMOPT_GQUOTA;
283 } else {
284 gid = ip->i_d.di_gid;
285 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000286 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
287 projid = vap->va_projid;
288 qflags |= XFS_QMOPT_PQUOTA;
289 } else {
290 projid = ip->i_d.di_projid;
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /*
293 * We take a reference when we initialize udqp and gdqp,
294 * so it is important that we never blindly double trip on
295 * the same variable. See xfs_create() for an example.
296 */
297 ASSERT(udqp == NULL);
298 ASSERT(gdqp == NULL);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000299 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
300 &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100302 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
305 /*
306 * For the other attributes, we acquire the inode lock and
307 * first do an error checking pass.
308 */
309 tp = NULL;
310 lock_flags = XFS_ILOCK_EXCL;
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700311 if (flags & ATTR_NOLOCK)
312 need_iolock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!(mask & XFS_AT_SIZE)) {
314 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
315 (mp->m_flags & XFS_MOUNT_WSYNC)) {
316 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
317 commit_flags = 0;
318 if ((code = xfs_trans_reserve(tp, 0,
319 XFS_ICHANGE_LOG_RES(mp), 0,
320 0, 0))) {
321 lock_flags = 0;
322 goto error_return;
323 }
324 }
325 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000326 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 !(flags & ATTR_DMI)) {
328 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100329 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 vap->va_size, 0, dmflags, NULL);
331 if (code) {
332 lock_flags = 0;
333 goto error_return;
334 }
335 }
336 if (need_iolock)
337 lock_flags |= XFS_IOLOCK_EXCL;
338 }
339
340 xfs_ilock(ip, lock_flags);
341
342 /* boolean: are we the file owner? */
343 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
344
345 /*
346 * Change various properties of a file.
347 * Only the owner or users with CAP_FOWNER
348 * capability may do these things.
349 */
350 if (mask &
351 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
352 XFS_AT_GID|XFS_AT_PROJID)) {
353 /*
354 * CAP_FOWNER overrides the following restrictions:
355 *
356 * The user ID of the calling process must be equal
357 * to the file owner ID, except in cases where the
358 * CAP_FSETID capability is applicable.
359 */
360 if (!file_owner && !capable(CAP_FOWNER)) {
361 code = XFS_ERROR(EPERM);
362 goto error_return;
363 }
364
365 /*
366 * CAP_FSETID overrides the following restrictions:
367 *
368 * The effective user ID of the calling process shall match
369 * the file owner when setting the set-user-ID and
370 * set-group-ID bits on that file.
371 *
372 * The effective group ID or one of the supplementary group
373 * IDs of the calling process shall match the group owner of
374 * the file when setting the set-group-ID bit on that file
375 */
376 if (mask & XFS_AT_MODE) {
377 mode_t m = 0;
378
379 if ((vap->va_mode & S_ISUID) && !file_owner)
380 m |= S_ISUID;
381 if ((vap->va_mode & S_ISGID) &&
382 !in_group_p((gid_t)ip->i_d.di_gid))
383 m |= S_ISGID;
384#if 0
385 /* Linux allows this, Irix doesn't. */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000386 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 m |= S_ISVTX;
388#endif
389 if (m && !capable(CAP_FSETID))
390 vap->va_mode &= ~m;
391 }
392 }
393
394 /*
395 * Change file ownership. Must be the owner or privileged.
396 * If the system was configured with the "restricted_chown"
397 * option, the owner is not permitted to give away the file,
398 * and can change the group id only to a group of which he
399 * or she is a member.
400 */
401 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
402 /*
403 * These IDs could have changed since we last looked at them.
404 * But, we're assured that if the ownership did change
405 * while we didn't have the inode locked, inode's dquot(s)
406 * would have changed also.
407 */
408 iuid = ip->i_d.di_uid;
409 iprojid = ip->i_d.di_projid;
410 igid = ip->i_d.di_gid;
411 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
412 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
413 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
414 iprojid;
415
416 /*
417 * CAP_CHOWN overrides the following restrictions:
418 *
419 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
420 * shall override the restriction that a process cannot
421 * change the user ID of a file it owns and the restriction
422 * that the group ID supplied to the chown() function
423 * shall be equal to either the group ID or one of the
424 * supplementary group IDs of the calling process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
426 if (restricted_chown &&
427 (iuid != uid || (igid != gid &&
428 !in_group_p((gid_t)gid))) &&
429 !capable(CAP_CHOWN)) {
430 code = XFS_ERROR(EPERM);
431 goto error_return;
432 }
433 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000434 * Do a quota reservation only if uid/projid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * going to change.
436 */
437 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000438 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
440 ASSERT(tp);
441 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
442 capable(CAP_FOWNER) ?
443 XFS_QMOPT_FORCE_RES : 0);
444 if (code) /* out of quota */
445 goto error_return;
446 }
447 }
448
449 /*
450 * Truncate file. Must have write permission and not be a directory.
451 */
452 if (mask & XFS_AT_SIZE) {
453 /* Short circuit the truncate case for zero length files */
454 if ((vap->va_size == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000455 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 xfs_iunlock(ip, XFS_ILOCK_EXCL);
457 lock_flags &= ~XFS_ILOCK_EXCL;
458 if (mask & XFS_AT_CTIME)
459 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
460 code = 0;
461 goto error_return;
462 }
463
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000464 if (VN_ISDIR(vp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 code = XFS_ERROR(EISDIR);
466 goto error_return;
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000467 } else if (!VN_ISREG(vp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 code = XFS_ERROR(EINVAL);
469 goto error_return;
470 }
471 /*
472 * Make sure that the dquots are attached to the inode.
473 */
474 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
475 goto error_return;
476 }
477
478 /*
479 * Change file access or modified times.
480 */
481 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
482 if (!file_owner) {
483 if ((flags & ATTR_UTIME) &&
484 !capable(CAP_FOWNER)) {
485 code = XFS_ERROR(EPERM);
486 goto error_return;
487 }
488 }
489 }
490
491 /*
492 * Change extent size or realtime flag.
493 */
494 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
495 /*
496 * Can't change extent size if any extents are allocated.
497 */
Eric Sandeene94af022005-11-02 15:10:41 +1100498 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
500 vap->va_extsize) ) {
501 code = XFS_ERROR(EINVAL); /* EFBIG? */
502 goto error_return;
503 }
504
505 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * Can't change realtime flag if any extents are allocated.
507 */
Eric Sandeene94af022005-11-02 15:10:41 +1100508 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
509 (mask & XFS_AT_XFLAGS) &&
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100510 (XFS_IS_REALTIME_INODE(ip)) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
512 code = XFS_ERROR(EINVAL); /* EFBIG? */
513 goto error_return;
514 }
515 /*
516 * Extent size must be a multiple of the appropriate block
517 * size, if set at all.
518 */
519 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
520 xfs_extlen_t size;
521
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100522 if (XFS_IS_REALTIME_INODE(ip) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 ((mask & XFS_AT_XFLAGS) &&
524 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
525 size = mp->m_sb.sb_rextsize <<
526 mp->m_sb.sb_blocklog;
527 } else {
528 size = mp->m_sb.sb_blocksize;
529 }
530 if (vap->va_extsize % size) {
531 code = XFS_ERROR(EINVAL);
532 goto error_return;
533 }
534 }
535 /*
536 * If realtime flag is set then must have realtime data.
537 */
538 if ((mask & XFS_AT_XFLAGS) &&
539 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
540 if ((mp->m_sb.sb_rblocks == 0) ||
541 (mp->m_sb.sb_rextsize == 0) ||
542 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
543 code = XFS_ERROR(EINVAL);
544 goto error_return;
545 }
546 }
547
548 /*
549 * Can't modify an immutable/append-only file unless
550 * we have appropriate permission.
551 */
552 if ((mask & XFS_AT_XFLAGS) &&
553 (ip->i_d.di_flags &
554 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
555 (vap->va_xflags &
556 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
557 !capable(CAP_LINUX_IMMUTABLE)) {
558 code = XFS_ERROR(EPERM);
559 goto error_return;
560 }
561 }
562
563 /*
564 * Now we can make the changes. Before we join the inode
565 * to the transaction, if XFS_AT_SIZE is set then take care of
566 * the part of the truncation that must be done without the
567 * inode lock. This needs to be done before joining the inode
568 * to the transaction, because the inode cannot be unlocked
569 * once it is a part of the transaction.
570 */
571 if (mask & XFS_AT_SIZE) {
572 code = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000573 if ((vap->va_size > ip->i_size) &&
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100574 (flags & ATTR_NOSIZETOK) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 code = xfs_igrow_start(ip, vap->va_size, credp);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000578
579 /*
580 * We are going to log the inode size change in this
581 * transaction so any previous writes that are beyond the on
582 * disk EOF and the new EOF that have not been written out need
583 * to be written here. If we do not write the data out, we
584 * expose ourselves to the null files problem.
585 *
586 * Only flush from the on disk size to the smaller of the in
587 * memory file size or the new size as that's the range we
588 * really care about here and prevents waiting for other data
589 * not within the range we care about here.
590 */
591 if (!code &&
592 (ip->i_size != ip->i_d.di_size) &&
593 (vap->va_size > ip->i_d.di_size)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000594 code = xfs_flush_pages(ip,
David Chinnerc32676e2007-07-19 16:28:58 +1000595 ip->i_d.di_size, vap->va_size,
596 XFS_B_ASYNC, FI_NONE);
597 }
598
599 /* wait for all I/O to complete */
Christoph Hellwigb677c212007-08-29 11:46:28 +1000600 vn_iowait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!code)
603 code = xfs_itruncate_data(ip, vap->va_size);
604 if (code) {
605 ASSERT(tp == NULL);
606 lock_flags &= ~XFS_ILOCK_EXCL;
607 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
608 goto error_return;
609 }
610 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
611 if ((code = xfs_trans_reserve(tp, 0,
612 XFS_ITRUNCATE_LOG_RES(mp), 0,
613 XFS_TRANS_PERM_LOG_RES,
614 XFS_ITRUNCATE_LOG_COUNT))) {
615 xfs_trans_cancel(tp, 0);
616 if (need_iolock)
617 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
618 return code;
619 }
620 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
621 xfs_ilock(ip, XFS_ILOCK_EXCL);
622 }
623
624 if (tp) {
625 xfs_trans_ijoin(tp, ip, lock_flags);
626 xfs_trans_ihold(tp, ip);
627 }
628
629 /* determine whether mandatory locking mode changes */
630 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
631
632 /*
633 * Truncate file. Must have write permission and not be a directory.
634 */
635 if (mask & XFS_AT_SIZE) {
David Chinner44d814c2008-03-06 13:45:29 +1100636 /*
637 * Only change the c/mtime if we are changing the size
638 * or we are explicitly asked to change it. This handles
639 * the semantic difference between truncate() and ftruncate()
640 * as implemented in the VFS.
641 */
642 if (vap->va_size != ip->i_size || (mask & XFS_AT_CTIME))
643 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
644
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000645 if (vap->va_size > ip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 xfs_igrow_finish(tp, ip, vap->va_size,
647 !(flags & ATTR_DMI));
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000648 } else if ((vap->va_size <= ip->i_size) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
650 /*
651 * signal a sync transaction unless
652 * we're truncating an already unlinked
653 * file on a wsync filesystem
654 */
655 code = xfs_itruncate_finish(&tp, ip,
656 (xfs_fsize_t)vap->va_size,
657 XFS_DATA_FORK,
658 ((ip->i_d.di_nlink != 0 ||
659 !(mp->m_flags & XFS_MOUNT_WSYNC))
660 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000661 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000663 /*
664 * Truncated "down", so we're removing references
665 * to old data here - if we now delay flushing for
666 * a long time, we expose ourselves unduly to the
667 * notorious NULL files problem. So, we mark this
668 * vnode and flush it when the file is closed, and
669 * do not wait the usual (long) time for writeout.
670 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000671 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
675 /*
676 * Change file access modes.
677 */
678 if (mask & XFS_AT_MODE) {
679 ip->i_d.di_mode &= S_IFMT;
680 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
681
682 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
683 timeflags |= XFS_ICHGTIME_CHG;
684 }
685
686 /*
687 * Change file ownership. Must be the owner or privileged.
688 * If the system was configured with the "restricted_chown"
689 * option, the owner is not permitted to give away the file,
690 * and can change the group id only to a group of which he
691 * or she is a member.
692 */
693 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
694 /*
695 * CAP_FSETID overrides the following restrictions:
696 *
697 * The set-user-ID and set-group-ID bits of a file will be
698 * cleared upon successful return from chown()
699 */
700 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
701 !capable(CAP_FSETID)) {
702 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
703 }
704
705 /*
706 * Change the ownerships and register quota modifications
707 * in the transaction.
708 */
709 if (iuid != uid) {
710 if (XFS_IS_UQUOTA_ON(mp)) {
711 ASSERT(mask & XFS_AT_UID);
712 ASSERT(udqp);
713 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
714 &ip->i_udquot, udqp);
715 }
716 ip->i_d.di_uid = uid;
717 }
718 if (igid != gid) {
719 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000720 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 ASSERT(mask & XFS_AT_GID);
722 ASSERT(gdqp);
723 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
724 &ip->i_gdquot, gdqp);
725 }
726 ip->i_d.di_gid = gid;
727 }
728 if (iprojid != projid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000729 if (XFS_IS_PQUOTA_ON(mp)) {
730 ASSERT(!XFS_IS_GQUOTA_ON(mp));
731 ASSERT(mask & XFS_AT_PROJID);
732 ASSERT(gdqp);
733 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
734 &ip->i_gdquot, gdqp);
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ip->i_d.di_projid = projid;
737 /*
738 * We may have to rev the inode as well as
739 * the superblock version number since projids didn't
740 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
741 */
742 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
743 xfs_bump_ino_vers2(tp, ip);
744 }
745
746 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
747 timeflags |= XFS_ICHGTIME_CHG;
748 }
749
750
751 /*
752 * Change file access or modified times.
753 */
754 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
755 if (mask & XFS_AT_ATIME) {
756 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
757 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
758 ip->i_update_core = 1;
759 timeflags &= ~XFS_ICHGTIME_ACC;
760 }
761 if (mask & XFS_AT_MTIME) {
762 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
763 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
764 timeflags &= ~XFS_ICHGTIME_MOD;
765 timeflags |= XFS_ICHGTIME_CHG;
766 }
767 if (tp && (flags & ATTR_UTIME))
768 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
769 }
770
771 /*
772 * Change XFS-added attributes.
773 */
774 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
775 if (mask & XFS_AT_EXTSIZE) {
776 /*
777 * Converting bytes to fs blocks.
778 */
779 ip->i_d.di_extsize = vap->va_extsize >>
780 mp->m_sb.sb_blocklog;
781 }
782 if (mask & XFS_AT_XFLAGS) {
783 uint di_flags;
784
785 /* can't set PREALLOC this way, just preserve it */
786 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
787 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
788 di_flags |= XFS_DIFLAG_IMMUTABLE;
789 if (vap->va_xflags & XFS_XFLAG_APPEND)
790 di_flags |= XFS_DIFLAG_APPEND;
791 if (vap->va_xflags & XFS_XFLAG_SYNC)
792 di_flags |= XFS_DIFLAG_SYNC;
793 if (vap->va_xflags & XFS_XFLAG_NOATIME)
794 di_flags |= XFS_DIFLAG_NOATIME;
795 if (vap->va_xflags & XFS_XFLAG_NODUMP)
796 di_flags |= XFS_DIFLAG_NODUMP;
Nathan Scott365ca832005-06-21 15:39:12 +1000797 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
798 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000799 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
800 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000801 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
802 di_flags |= XFS_DIFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
804 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
805 di_flags |= XFS_DIFLAG_RTINHERIT;
806 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
807 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100808 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
809 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
810 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000811 if (vap->va_xflags & XFS_XFLAG_REALTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100813 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
814 di_flags |= XFS_DIFLAG_EXTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 ip->i_d.di_flags = di_flags;
817 }
818 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
819 timeflags |= XFS_ICHGTIME_CHG;
820 }
821
822 /*
823 * Change file inode change time only if XFS_AT_CTIME set
824 * AND we have been called by a DMI function.
825 */
826
827 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
828 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
829 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
830 ip->i_update_core = 1;
831 timeflags &= ~XFS_ICHGTIME_CHG;
832 }
833
834 /*
835 * Send out timestamp changes that need to be set to the
836 * current time. Not done when called by a DMI function.
837 */
838 if (timeflags && !(flags & ATTR_DMI))
839 xfs_ichgtime(ip, timeflags);
840
841 XFS_STATS_INC(xs_ig_attrchg);
842
843 /*
844 * If this is a synchronous mount, make sure that the
845 * transaction goes to disk before returning to the user.
846 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000847 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * One for the truncate and one for the timestamps since we
849 * don't want to change the timestamps unless we're sure the
850 * truncate worked. Truncates are less than 1% of the laddis
851 * mix so this probably isn't worth the trouble to optimize.
852 */
853 code = 0;
854 if (tp) {
855 if (mp->m_flags & XFS_MOUNT_WSYNC)
856 xfs_trans_set_sync(tp);
857
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000858 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 /*
862 * If the (regular) file's mandatory locking mode changed, then
863 * notify the vnode. We do this under the inode lock to prevent
864 * racing calls to vop_vnode_change.
865 */
866 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 xfs_iunlock(ip, lock_flags);
869
870 /*
871 * Release any dquot(s) the inode had kept before chown.
872 */
873 XFS_QM_DQRELE(mp, olddquot1);
874 XFS_QM_DQRELE(mp, olddquot2);
875 XFS_QM_DQRELE(mp, udqp);
876 XFS_QM_DQRELE(mp, gdqp);
877
878 if (code) {
879 return code;
880 }
881
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000882 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 !(flags & ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100884 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 NULL, DM_RIGHT_NULL, NULL, NULL,
886 0, 0, AT_DELAY_FLAG(flags));
887 }
888 return 0;
889
890 abort_return:
891 commit_flags |= XFS_TRANS_ABORT;
892 /* FALLTHROUGH */
893 error_return:
894 XFS_QM_DQRELE(mp, udqp);
895 XFS_QM_DQRELE(mp, gdqp);
896 if (tp) {
897 xfs_trans_cancel(tp, commit_flags);
898 }
899 if (lock_flags != 0) {
900 xfs_iunlock(ip, lock_flags);
901 }
902 return code;
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000906 * The maximum pathlen is 1024 bytes. Since the minimum file system
907 * blocksize is 512 bytes, we can get a max of 2 extents back from
908 * bmapi.
909 */
910#define SYMLINK_MAPS 2
911
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000912STATIC int
913xfs_readlink_bmap(
914 xfs_inode_t *ip,
915 char *link)
916{
917 xfs_mount_t *mp = ip->i_mount;
918 int pathlen = ip->i_d.di_size;
919 int nmaps = SYMLINK_MAPS;
920 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
921 xfs_daddr_t d;
922 int byte_cnt;
923 int n;
924 xfs_buf_t *bp;
925 int error = 0;
926
927 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
928 mval, &nmaps, NULL, NULL);
929 if (error)
930 goto out;
931
932 for (n = 0; n < nmaps; n++) {
933 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
934 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
935
936 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
937 error = XFS_BUF_GETERROR(bp);
938 if (error) {
939 xfs_ioerror_alert("xfs_readlink",
940 ip->i_mount, bp, XFS_BUF_ADDR(bp));
941 xfs_buf_relse(bp);
942 goto out;
943 }
944 if (pathlen < byte_cnt)
945 byte_cnt = pathlen;
946 pathlen -= byte_cnt;
947
948 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
949 xfs_buf_relse(bp);
950 }
951
952 link[ip->i_d.di_size] = '\0';
953 error = 0;
954
955 out:
956 return error;
957}
958
Christoph Hellwig993386c12007-08-28 16:12:30 +1000959int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960xfs_readlink(
Christoph Hellwig993386c12007-08-28 16:12:30 +1000961 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000962 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000964 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100968 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 if (XFS_FORCED_SHUTDOWN(mp))
971 return XFS_ERROR(EIO);
972
973 xfs_ilock(ip, XFS_ILOCK_SHARED);
974
975 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000976 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000978 pathlen = ip->i_d.di_size;
979 if (!pathlen)
980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000983 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
984 link[pathlen] = '\0';
985 } else {
986 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000989 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return error;
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/*
995 * xfs_fsync
996 *
997 * This is called to sync the inode and its data out to disk.
998 * We need to hold the I/O lock while flushing the data, and
999 * the inode lock while flushing the inode. The inode lock CANNOT
1000 * be held while flushing the data, so acquire after we're done
1001 * with that.
1002 */
Christoph Hellwig993386c12007-08-28 16:12:30 +10001003int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004xfs_fsync(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001005 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int flag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 xfs_off_t start,
1008 xfs_off_t stop)
1009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 xfs_trans_t *tp;
1011 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001012 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001014 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 ASSERT(start >= 0 && stop >= -1);
1017
1018 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1019 return XFS_ERROR(EIO);
1020
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +10001021 if (flag & FSYNC_DATA)
1022 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /*
1025 * We always need to make sure that the required inode state
1026 * is safe on disk. The vnode might be clean but because
1027 * of committed transactions that haven't hit the disk yet.
1028 * Likewise, there could be unflushed non-transactional
1029 * changes to the inode core that have to go to disk.
1030 *
1031 * The following code depends on one assumption: that
1032 * any transaction that changes an inode logs the core
1033 * because it has to change some field in the inode core
1034 * (typically nextents or nblocks). That assumption
1035 * implies that any transactions against an inode will
1036 * catch any non-transactional updates. If inode-altering
1037 * transactions exist that violate this assumption, the
1038 * code breaks. Right now, it figures that if the involved
1039 * update_* field is clear and the inode is unpinned, the
1040 * inode is clean. Either it's been flushed or it's been
1041 * committed and the commit has hit the disk unpinning the inode.
1042 * (Note that xfs_inode_item_format() called at commit clears
1043 * the update_* fields.)
1044 */
1045 xfs_ilock(ip, XFS_ILOCK_SHARED);
1046
1047 /* If we are flushing data then we care about update_size
1048 * being set, otherwise we care about update_core
1049 */
1050 if ((flag & FSYNC_DATA) ?
1051 (ip->i_update_size == 0) :
1052 (ip->i_update_core == 0)) {
1053 /*
1054 * Timestamps/size haven't changed since last inode
1055 * flush or inode transaction commit. That means
1056 * either nothing got written or a transaction
1057 * committed which caught the updates. If the
1058 * latter happened and the transaction hasn't
1059 * hit the disk yet, the inode will be still
1060 * be pinned. If it is, force the log.
1061 */
1062
1063 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1064
1065 if (xfs_ipincount(ip)) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001066 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 XFS_LOG_FORCE |
1068 ((flag & FSYNC_WAIT)
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001069 ? XFS_LOG_SYNC : 0),
1070 &log_flushed);
1071 } else {
1072 /*
1073 * If the inode is not pinned and nothing
1074 * has changed we don't need to flush the
1075 * cache.
1076 */
1077 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079 error = 0;
1080 } else {
1081 /*
1082 * Kick off a transaction to log the inode
1083 * core to get the updates. Make it
1084 * sync if FSYNC_WAIT is passed in (which
1085 * is done by everybody but specfs). The
1086 * sync transaction will also force the log.
1087 */
1088 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1089 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1090 if ((error = xfs_trans_reserve(tp, 0,
1091 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1092 0, 0, 0))) {
1093 xfs_trans_cancel(tp, 0);
1094 return error;
1095 }
1096 xfs_ilock(ip, XFS_ILOCK_EXCL);
1097
1098 /*
1099 * Note - it's possible that we might have pushed
1100 * ourselves out of the way during trans_reserve
1101 * which would flush the inode. But there's no
1102 * guarantee that the inode buffer has actually
1103 * gone out yet (it's delwri). Plus the buffer
1104 * could be pinned anyway if it's part of an
1105 * inode in another recent transaction. So we
1106 * play it safe and fire off the transaction anyway.
1107 */
1108 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1109 xfs_trans_ihold(tp, ip);
1110 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1111 if (flag & FSYNC_WAIT)
1112 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001113 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1116 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001117
1118 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1119 /*
1120 * If the log write didn't issue an ordered tag we need
1121 * to flush the disk cache for the data device now.
1122 */
1123 if (!log_flushed)
1124 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1125
1126 /*
1127 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +10001128 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001129 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11001130 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001131 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1132 }
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return error;
1135}
1136
1137/*
David Chinner92dfe8d2007-05-24 15:22:19 +10001138 * This is called by xfs_inactive to free any blocks beyond eof
1139 * when the link count isn't zero and by xfs_dm_punch_hole() when
1140 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001142int
1143xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +10001145 xfs_inode_t *ip,
1146 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 xfs_trans_t *tp;
1149 int error;
1150 xfs_fileoff_t end_fsb;
1151 xfs_fileoff_t last_fsb;
1152 xfs_filblks_t map_len;
1153 int nimaps;
1154 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +10001155 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /*
1158 * Figure out if there are any blocks beyond the end
1159 * of the file. If not, then there is nothing to do.
1160 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001161 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1163 map_len = last_fsb - end_fsb;
1164 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 nimaps = 1;
1168 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001169 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001170 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1172
1173 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001174 (imap.br_startblock != HOLESTARTBLOCK ||
1175 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /*
1177 * Attach the dquots to the inode up front.
1178 */
1179 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001180 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 /*
1183 * There are blocks after the end of file.
1184 * Free them up now by truncating the file to
1185 * its current size.
1186 */
1187 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1188
1189 /*
1190 * Do the xfs_itruncate_start() call before
1191 * reserving any log space because
1192 * itruncate_start will call into the buffer
1193 * cache and we can't
1194 * do that within a transaction.
1195 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001196 if (use_iolock)
1197 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001198 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001199 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001200 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001201 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001202 if (use_iolock)
1203 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001204 return error;
1205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 error = xfs_trans_reserve(tp, 0,
1208 XFS_ITRUNCATE_LOG_RES(mp),
1209 0, XFS_TRANS_PERM_LOG_RES,
1210 XFS_ITRUNCATE_LOG_COUNT);
1211 if (error) {
1212 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1213 xfs_trans_cancel(tp, 0);
1214 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001215 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
1218 xfs_ilock(ip, XFS_ILOCK_EXCL);
1219 xfs_trans_ijoin(tp, ip,
1220 XFS_IOLOCK_EXCL |
1221 XFS_ILOCK_EXCL);
1222 xfs_trans_ihold(tp, ip);
1223
1224 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001225 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 XFS_DATA_FORK,
1227 0);
1228 /*
1229 * If we get an error at this point we
1230 * simply don't bother truncating the file.
1231 */
1232 if (error) {
1233 xfs_trans_cancel(tp,
1234 (XFS_TRANS_RELEASE_LOG_RES |
1235 XFS_TRANS_ABORT));
1236 } else {
1237 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001238 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
David Chinner92dfe8d2007-05-24 15:22:19 +10001240 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1241 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001243 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246/*
1247 * Free a symlink that has blocks associated with it.
1248 */
1249STATIC int
1250xfs_inactive_symlink_rmt(
1251 xfs_inode_t *ip,
1252 xfs_trans_t **tpp)
1253{
1254 xfs_buf_t *bp;
1255 int committed;
1256 int done;
1257 int error;
1258 xfs_fsblock_t first_block;
1259 xfs_bmap_free_t free_list;
1260 int i;
1261 xfs_mount_t *mp;
1262 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1263 int nmaps;
1264 xfs_trans_t *ntp;
1265 int size;
1266 xfs_trans_t *tp;
1267
1268 tp = *tpp;
1269 mp = ip->i_mount;
1270 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1271 /*
1272 * We're freeing a symlink that has some
1273 * blocks allocated to it. Free the
1274 * blocks here. We know that we've got
1275 * either 1 or 2 extents and that we can
1276 * free them all in one bunmapi call.
1277 */
1278 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1279 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1280 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1281 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1282 xfs_trans_cancel(tp, 0);
1283 *tpp = NULL;
1284 return error;
1285 }
1286 /*
1287 * Lock the inode, fix the size, and join it to the transaction.
1288 * Hold it so in the normal path, we still have it locked for
1289 * the second transaction. In the error paths we need it
1290 * held so the cancel won't rele it, see below.
1291 */
1292 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1293 size = (int)ip->i_d.di_size;
1294 ip->i_d.di_size = 0;
1295 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1296 xfs_trans_ihold(tp, ip);
1297 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1298 /*
1299 * Find the block(s) so we can inval and unmap them.
1300 */
1301 done = 0;
1302 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -08001303 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1305 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001306 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 goto error0;
1308 /*
1309 * Invalidate the block(s).
1310 */
1311 for (i = 0; i < nmaps; i++) {
1312 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1313 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1314 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1315 xfs_trans_binval(tp, bp);
1316 }
1317 /*
1318 * Unmap the dead block(s) to the free_list.
1319 */
1320 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001321 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 goto error1;
1323 ASSERT(done);
1324 /*
1325 * Commit the first transaction. This logs the EFI and the inode.
1326 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001327 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 goto error1;
1329 /*
1330 * The transaction must have been committed, since there were
1331 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1332 * The new tp has the extent freeing and EFDs.
1333 */
1334 ASSERT(committed);
1335 /*
1336 * The first xact was committed, so add the inode to the new one.
1337 * Mark it dirty so it will be logged and moved forward in the log as
1338 * part of every commit.
1339 */
1340 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1341 xfs_trans_ihold(tp, ip);
1342 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1343 /*
1344 * Get a new, empty transaction to return to our caller.
1345 */
1346 ntp = xfs_trans_dup(tp);
1347 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001348 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * If we get an error on the commit here or on the reserve below,
1350 * we need to unlock the inode since the new transaction doesn't
1351 * have the inode attached.
1352 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001353 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 tp = ntp;
1355 if (error) {
1356 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1357 goto error0;
1358 }
1359 /*
1360 * Remove the memory for extent descriptions (just bookkeeping).
1361 */
1362 if (ip->i_df.if_bytes)
1363 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1364 ASSERT(ip->i_df.if_bytes == 0);
1365 /*
1366 * Put an itruncate log reservation in the new transaction
1367 * for our caller.
1368 */
1369 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1370 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1371 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1372 goto error0;
1373 }
1374 /*
1375 * Return with the inode locked but not joined to the transaction.
1376 */
1377 *tpp = tp;
1378 return 0;
1379
1380 error1:
1381 xfs_bmap_cancel(&free_list);
1382 error0:
1383 /*
1384 * Have to come here with the inode locked and either
1385 * (held and in the transaction) or (not in the transaction).
1386 * If the inode isn't held then cancel would iput it, but
1387 * that's wrong since this is inactive and the vnode ref
1388 * count is 0 already.
1389 * Cancel won't do anything to the inode if held, but it still
1390 * needs to be locked until the cancel is done, if it was
1391 * joined to the transaction.
1392 */
1393 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1394 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1395 *tpp = NULL;
1396 return error;
1397
1398}
1399
1400STATIC int
1401xfs_inactive_symlink_local(
1402 xfs_inode_t *ip,
1403 xfs_trans_t **tpp)
1404{
1405 int error;
1406
1407 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1408 /*
1409 * We're freeing a symlink which fit into
1410 * the inode. Just free the memory used
1411 * to hold the old symlink.
1412 */
1413 error = xfs_trans_reserve(*tpp, 0,
1414 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1415 0, XFS_TRANS_PERM_LOG_RES,
1416 XFS_ITRUNCATE_LOG_COUNT);
1417
1418 if (error) {
1419 xfs_trans_cancel(*tpp, 0);
1420 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001421 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1424
1425 /*
1426 * Zero length symlinks _can_ exist.
1427 */
1428 if (ip->i_df.if_bytes > 0) {
1429 xfs_idata_realloc(ip,
1430 -(ip->i_df.if_bytes),
1431 XFS_DATA_FORK);
1432 ASSERT(ip->i_df.if_bytes == 0);
1433 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437STATIC int
1438xfs_inactive_attrs(
1439 xfs_inode_t *ip,
1440 xfs_trans_t **tpp)
1441{
1442 xfs_trans_t *tp;
1443 int error;
1444 xfs_mount_t *mp;
1445
1446 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1447 tp = *tpp;
1448 mp = ip->i_mount;
1449 ASSERT(ip->i_d.di_forkoff != 0);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001450 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1452
1453 error = xfs_attr_inactive(ip);
1454 if (error) {
1455 *tpp = NULL;
1456 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001457 return error; /* goto out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
1460 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1461 error = xfs_trans_reserve(tp, 0,
1462 XFS_IFREE_LOG_RES(mp),
1463 0, XFS_TRANS_PERM_LOG_RES,
1464 XFS_INACTIVE_LOG_COUNT);
1465 if (error) {
1466 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1467 xfs_trans_cancel(tp, 0);
1468 *tpp = NULL;
1469 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001470 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472
1473 xfs_ilock(ip, XFS_ILOCK_EXCL);
1474 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1475 xfs_trans_ihold(tp, ip);
1476 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1477
1478 ASSERT(ip->i_d.di_anextents == 0);
1479
1480 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Christoph Hellwig993386c12007-08-28 16:12:30 +10001484int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485xfs_release(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001486 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001488 bhv_vnode_t *vp = XFS_ITOV(ip);
1489 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 int error;
1491
Nathan Scott7d4fb402006-06-09 15:27:16 +10001492 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001496 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498
David Chinner2a82b8b2007-07-11 11:09:12 +10001499 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001500 int truncated;
1501
David Chinner2a82b8b2007-07-11 11:09:12 +10001502 /*
1503 * If we are using filestreams, and we have an unlinked
1504 * file that we are processing the last close on, then nothing
1505 * will be able to reopen and write to this file. Purge this
1506 * inode from the filestreams cache so that it doesn't delay
1507 * teardown of the inode.
1508 */
1509 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1510 xfs_filestream_deassociate(ip);
1511
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001512 /*
1513 * If we previously truncated this file and removed old data
1514 * in the process, we want to initiate "early" writeout on
1515 * the last close. This is an attempt to combat the notorious
1516 * NULL files problem which is particularly noticable from a
1517 * truncate down, buffered (re-)write (delalloc), followed by
1518 * a crash. What we are effectively doing here is
1519 * significantly reducing the time window where we'd otherwise
1520 * be exposed to that problem.
1521 */
Christoph Hellwig09262b432007-08-29 11:44:50 +10001522 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001523 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001524 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001525 }
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (ip->i_d.di_nlink != 0) {
1528 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001529 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001530 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001532 (!(ip->i_d.di_flags &
1533 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001534 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1535 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001536 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538 }
1539
1540 return 0;
1541}
1542
1543/*
1544 * xfs_inactive
1545 *
1546 * This is called when the vnode reference count for the vnode
1547 * goes to zero. If the file has been unlinked, then it must
1548 * now be truncated. Also, we clear all of the read-ahead state
1549 * kept for the inode here since the file is now closed.
1550 */
Christoph Hellwig993386c12007-08-28 16:12:30 +10001551int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552xfs_inactive(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001553 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001555 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +10001556 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 xfs_fsblock_t first_block;
1558 int committed;
1559 xfs_trans_t *tp;
1560 xfs_mount_t *mp;
1561 int error;
1562 int truncate;
1563
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001564 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /*
1567 * If the inode is already free, then there can be nothing
1568 * to clean up here.
1569 */
1570 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1571 ASSERT(ip->i_df.if_real_bytes == 0);
1572 ASSERT(ip->i_df.if_broot_bytes == 0);
1573 return VN_INACTIVE_CACHE;
1574 }
1575
1576 /*
1577 * Only do a truncate if it's a regular file with
1578 * some actual space in it. It's OK to look at the
1579 * inode's fields without the lock because we're the
1580 * only one with a reference to the inode.
1581 */
1582 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001583 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1584 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1586
1587 mp = ip->i_mount;
1588
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001589 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1590 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 error = 0;
1593
1594 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001595 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 goto out;
1597
1598 if (ip->i_d.di_nlink != 0) {
1599 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001600 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001601 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001602 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1603 (!(ip->i_d.di_flags &
1604 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1605 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001606 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1607 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001608 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610 goto out;
1611 }
1612
1613 ASSERT(ip->i_d.di_nlink == 0);
1614
1615 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001616 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1619 if (truncate) {
1620 /*
1621 * Do the xfs_itruncate_start() call before
1622 * reserving any log space because itruncate_start
1623 * will call into the buffer cache and we can't
1624 * do that within a transaction.
1625 */
1626 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1627
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001628 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1629 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001630 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001631 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1632 return VN_INACTIVE_CACHE;
1633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 error = xfs_trans_reserve(tp, 0,
1636 XFS_ITRUNCATE_LOG_RES(mp),
1637 0, XFS_TRANS_PERM_LOG_RES,
1638 XFS_ITRUNCATE_LOG_COUNT);
1639 if (error) {
1640 /* Don't call itruncate_cleanup */
1641 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1642 xfs_trans_cancel(tp, 0);
1643 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001644 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
1646
1647 xfs_ilock(ip, XFS_ILOCK_EXCL);
1648 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1649 xfs_trans_ihold(tp, ip);
1650
1651 /*
1652 * normally, we have to run xfs_itruncate_finish sync.
1653 * But if filesystem is wsync and we're in the inactive
1654 * path, then we know that nlink == 0, and that the
1655 * xaction that made nlink == 0 is permanently committed
1656 * since xfs_remove runs as a synchronous transaction.
1657 */
1658 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1659 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1660
1661 if (error) {
1662 xfs_trans_cancel(tp,
1663 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1664 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001665 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1668
1669 /*
1670 * If we get an error while cleaning up a
1671 * symlink we bail out.
1672 */
1673 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1674 xfs_inactive_symlink_rmt(ip, &tp) :
1675 xfs_inactive_symlink_local(ip, &tp);
1676
1677 if (error) {
1678 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001679 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681
1682 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1683 xfs_trans_ihold(tp, ip);
1684 } else {
1685 error = xfs_trans_reserve(tp, 0,
1686 XFS_IFREE_LOG_RES(mp),
1687 0, XFS_TRANS_PERM_LOG_RES,
1688 XFS_INACTIVE_LOG_COUNT);
1689 if (error) {
1690 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1691 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001692 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694
1695 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1696 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1697 xfs_trans_ihold(tp, ip);
1698 }
1699
1700 /*
1701 * If there are attributes associated with the file
1702 * then blow them away now. The code calls a routine
1703 * that recursively deconstructs the attribute fork.
1704 * We need to just commit the current transaction
1705 * because we can't use it for xfs_attr_inactive().
1706 */
1707 if (ip->i_d.di_anextents > 0) {
1708 error = xfs_inactive_attrs(ip, &tp);
1709 /*
1710 * If we got an error, the transaction is already
1711 * cancelled, and the inode is unlocked. Just get out.
1712 */
1713 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001714 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 } else if (ip->i_afp) {
1716 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1717 }
1718
1719 /*
1720 * Free the inode.
1721 */
1722 XFS_BMAP_INIT(&free_list, &first_block);
1723 error = xfs_ifree(tp, ip, &free_list);
1724 if (error) {
1725 /*
1726 * If we fail to free the inode, shut down. The cancel
1727 * might do that, we need to make sure. Otherwise the
1728 * inode might be lost for a long time or forever.
1729 */
1730 if (!XFS_FORCED_SHUTDOWN(mp)) {
1731 cmn_err(CE_NOTE,
1732 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1733 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001734 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1737 } else {
1738 /*
1739 * Credit the quota account(s). The inode is gone.
1740 */
1741 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1742
1743 /*
1744 * Just ignore errors at this point. There is
1745 * nothing we can do except to try to keep going.
1746 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001747 (void) xfs_bmap_finish(&tp, &free_list, &committed);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001748 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750 /*
1751 * Release the dquots held by inode, if any.
1752 */
1753 XFS_QM_DQDETACH(mp, ip);
1754
1755 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1756
1757 out:
1758 return VN_INACTIVE_CACHE;
1759}
1760
1761
Christoph Hellwig993386c12007-08-28 16:12:30 +10001762int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763xfs_lookup(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001764 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10001765 bhv_vname_t *dentry,
Christoph Hellwig993386c12007-08-28 16:12:30 +10001766 bhv_vnode_t **vpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Christoph Hellwig993386c12007-08-28 16:12:30 +10001768 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 xfs_ino_t e_inum;
1770 int error;
1771 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001773 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1776 return XFS_ERROR(EIO);
1777
1778 lock_mode = xfs_ilock_map_shared(dp);
Christoph Hellwig993386c12007-08-28 16:12:30 +10001779 error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (!error) {
1781 *vpp = XFS_ITOV(ip);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001782 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 xfs_iunlock_map_shared(dp, lock_mode);
1785 return error;
1786}
1787
Christoph Hellwig993386c12007-08-28 16:12:30 +10001788int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789xfs_create(
Christoph Hellwig993386c12007-08-28 16:12:30 +10001790 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10001791 bhv_vname_t *dentry,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001792 mode_t mode,
1793 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001794 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 cred_t *credp)
1796{
1797 char *name = VNAME(dentry);
Christoph Hellwig993386c12007-08-28 16:12:30 +10001798 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig993386c12007-08-28 16:12:30 +10001799 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 int error;
1802 xfs_bmap_free_t free_list;
1803 xfs_fsblock_t first_block;
Christoph Hellwig993386c12007-08-28 16:12:30 +10001804 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 int dm_event_sent = 0;
1806 uint cancel_flags;
1807 int committed;
1808 xfs_prid_t prid;
1809 struct xfs_dquot *udqp, *gdqp;
1810 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 int namelen;
1812
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001813 ASSERT(!*ipp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001814 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 namelen = VNAMELEN(dentry);
1817
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001818 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001820 dp, DM_RIGHT_NULL, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 DM_RIGHT_NULL, name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001822 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 if (error)
1825 return error;
1826 dm_event_sent = 1;
1827 }
1828
1829 if (XFS_FORCED_SHUTDOWN(mp))
1830 return XFS_ERROR(EIO);
1831
1832 /* Return through std_return after this point. */
1833
1834 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001835 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1836 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 else
1838 prid = (xfs_prid_t)dfltprid;
1839
1840 /*
1841 * Make sure that we have allocated dquot(s) on disk.
1842 */
1843 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001844 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1846 if (error)
1847 goto std_return;
1848
1849 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1852 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1853 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1854 /*
1855 * Initially assume that the file does not exist and
1856 * reserve the resources for that case. If that is not
1857 * the case we'll drop the one we have and get a more
1858 * appropriate transaction later.
1859 */
1860 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1861 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1862 if (error == ENOSPC) {
1863 resblks = 0;
1864 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1865 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1866 }
1867 if (error) {
1868 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 goto error_return;
1870 }
1871
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001872 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c12007-08-28 16:12:30 +10001873 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 XFS_BMAP_INIT(&free_list, &first_block);
1876
1877 ASSERT(ip == NULL);
1878
1879 /*
1880 * Reserve disk quota and the inode.
1881 */
1882 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1883 if (error)
1884 goto error_return;
1885
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001886 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001888 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 rdev, credp, prid, resblks > 0,
1890 &ip, &committed);
1891 if (error) {
1892 if (error == ENOSPC)
1893 goto error_return;
1894 goto abort_return;
1895 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001896 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /*
1899 * At this point, we've gotten a newly allocated inode.
1900 * It is locked (and joined to the transaction).
1901 */
1902
1903 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1904
1905 /*
Christoph Hellwig993386c12007-08-28 16:12:30 +10001906 * Now we join the directory inode to the transaction. We do not do it
1907 * earlier because xfs_dir_ialloc might commit the previous transaction
1908 * (and release all the locks). An error from here on will result in
1909 * the transaction cancel unlocking dp so don't do it explicitly in the
1910 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001912 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c12007-08-28 16:12:30 +10001914 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001916 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
1917 &first_block, &free_list, resblks ?
1918 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (error) {
1920 ASSERT(error != ENOSPC);
1921 goto abort_return;
1922 }
1923 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1924 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1925
1926 /*
1927 * If this is a synchronous mount, make sure that the
1928 * create transaction goes to disk before returning to
1929 * the user.
1930 */
1931 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1932 xfs_trans_set_sync(tp);
1933 }
1934
1935 dp->i_gen++;
1936
1937 /*
1938 * Attach the dquot(s) to the inodes and modify them incore.
1939 * These ids of the inode couldn't have changed since the new
1940 * inode has been locked ever since it was created.
1941 */
1942 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1943
1944 /*
1945 * xfs_trans_commit normally decrements the vnode ref count
1946 * when it unlocks the inode. Since we want to return the
1947 * vnode to the caller, we bump the vnode ref count now.
1948 */
1949 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001951 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (error) {
1953 xfs_bmap_cancel(&free_list);
1954 goto abort_rele;
1955 }
1956
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001957 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (error) {
1959 IRELE(ip);
1960 tp = NULL;
1961 goto error_return;
1962 }
1963
1964 XFS_QM_DQRELE(mp, udqp);
1965 XFS_QM_DQRELE(mp, gdqp);
1966
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001967 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 /* Fallthrough to std_return with error = 0 */
1970
1971std_return:
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001972 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c12007-08-28 16:12:30 +10001973 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001975 dp, DM_RIGHT_NULL,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001976 *ipp ? ip : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 DM_RIGHT_NULL, name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001978 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980 return error;
1981
1982 abort_return:
1983 cancel_flags |= XFS_TRANS_ABORT;
1984 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Jesper Juhl014c2542006-01-15 02:37:08 +01001986 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (tp != NULL)
1988 xfs_trans_cancel(tp, cancel_flags);
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 XFS_QM_DQRELE(mp, udqp);
1991 XFS_QM_DQRELE(mp, gdqp);
1992
Christoph Hellwig993386c12007-08-28 16:12:30 +10001993 if (unlock_dp_on_error)
1994 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 goto std_return;
1997
1998 abort_rele:
1999 /*
2000 * Wait until after the current transaction is aborted to
2001 * release the inode. This prevents recursive transactions
2002 * and deadlocks from xfs_inactive.
2003 */
2004 cancel_flags |= XFS_TRANS_ABORT;
2005 xfs_trans_cancel(tp, cancel_flags);
2006 IRELE(ip);
2007
2008 XFS_QM_DQRELE(mp, udqp);
2009 XFS_QM_DQRELE(mp, gdqp);
2010
2011 goto std_return;
2012}
2013
2014#ifdef DEBUG
2015/*
2016 * Some counters to see if (and how often) we are hitting some deadlock
2017 * prevention code paths.
2018 */
2019
2020int xfs_rm_locks;
2021int xfs_rm_lock_delays;
2022int xfs_rm_attempts;
2023#endif
2024
2025/*
2026 * The following routine will lock the inodes associated with the
2027 * directory and the named entry in the directory. The locks are
2028 * acquired in increasing inode number.
2029 *
2030 * If the entry is "..", then only the directory is locked. The
2031 * vnode ref count will still include that from the .. entry in
2032 * this case.
2033 *
2034 * There is a deadlock we need to worry about. If the locked directory is
2035 * in the AIL, it might be blocking up the log. The next inode we lock
2036 * could be already locked by another thread waiting for log space (e.g
2037 * a permanent log reservation with a long running transaction (see
2038 * xfs_itruncate_finish)). To solve this, we must check if the directory
2039 * is in the ail and use lock_nowait. If we can't lock, we need to
2040 * drop the inode lock on the directory and try again. xfs_iunlock will
2041 * potentially push the tail if we were holding up the log.
2042 */
2043STATIC int
2044xfs_lock_dir_and_entry(
2045 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 xfs_inode_t *ip) /* inode of entry 'name' */
2047{
2048 int attempts;
2049 xfs_ino_t e_inum;
2050 xfs_inode_t *ips[2];
2051 xfs_log_item_t *lp;
2052
2053#ifdef DEBUG
2054 xfs_rm_locks++;
2055#endif
2056 attempts = 0;
2057
2058again:
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002059 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 e_inum = ip->i_ino;
2062
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002063 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 /*
2066 * We want to lock in increasing inum. Since we've already
2067 * acquired the lock on the directory, we may need to release
2068 * if if the inum of the entry turns out to be less.
2069 */
2070 if (e_inum > dp->i_ino) {
2071 /*
2072 * We are already in the right order, so just
2073 * lock on the inode of the entry.
2074 * We need to use nowait if dp is in the AIL.
2075 */
2076
2077 lp = (xfs_log_item_t *)dp->i_itemp;
2078 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2079 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2080 attempts++;
2081#ifdef DEBUG
2082 xfs_rm_attempts++;
2083#endif
2084
2085 /*
2086 * Unlock dp and try again.
2087 * xfs_iunlock will try to push the tail
2088 * if the inode is in the AIL.
2089 */
2090
2091 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2092
2093 if ((attempts % 5) == 0) {
2094 delay(1); /* Don't just spin the CPU */
2095#ifdef DEBUG
2096 xfs_rm_lock_delays++;
2097#endif
2098 }
2099 goto again;
2100 }
2101 } else {
2102 xfs_ilock(ip, XFS_ILOCK_EXCL);
2103 }
2104 } else if (e_inum < dp->i_ino) {
2105 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2106
2107 ips[0] = ip;
2108 ips[1] = dp;
2109 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2110 }
2111 /* else e_inum == dp->i_ino */
2112 /* This can happen if we're asked to lock /x/..
2113 * the entry is "..", which is also the parent directory.
2114 */
2115
2116 return 0;
2117}
2118
2119#ifdef DEBUG
2120int xfs_locked_n;
2121int xfs_small_retries;
2122int xfs_middle_retries;
2123int xfs_lots_retries;
2124int xfs_lock_delays;
2125#endif
2126
2127/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002128 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2129 * a different value
2130 */
2131static inline int
2132xfs_lock_inumorder(int lock_mode, int subclass)
2133{
2134 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002135 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002136 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002137 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002138
2139 return lock_mode;
2140}
2141
2142/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 * The following routine will lock n inodes in exclusive mode.
2144 * We assume the caller calls us with the inodes in i_ino order.
2145 *
2146 * We need to detect deadlock where an inode that we lock
2147 * is in the AIL and we start waiting for another inode that is locked
2148 * by a thread in a long running transaction (such as truncate). This can
2149 * result in deadlock since the long running trans might need to wait
2150 * for the inode we just locked in order to push the tail and free space
2151 * in the log.
2152 */
2153void
2154xfs_lock_inodes(
2155 xfs_inode_t **ips,
2156 int inodes,
2157 int first_locked,
2158 uint lock_mode)
2159{
2160 int attempts = 0, i, j, try_lock;
2161 xfs_log_item_t *lp;
2162
2163 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2164
2165 if (first_locked) {
2166 try_lock = 1;
2167 i = 1;
2168 } else {
2169 try_lock = 0;
2170 i = 0;
2171 }
2172
2173again:
2174 for (; i < inodes; i++) {
2175 ASSERT(ips[i]);
2176
2177 if (i && (ips[i] == ips[i-1])) /* Already locked */
2178 continue;
2179
2180 /*
2181 * If try_lock is not set yet, make sure all locked inodes
2182 * are not in the AIL.
2183 * If any are, set try_lock to be used later.
2184 */
2185
2186 if (!try_lock) {
2187 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2188 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2189 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2190 try_lock++;
2191 }
2192 }
2193 }
2194
2195 /*
2196 * If any of the previous locks we have locked is in the AIL,
2197 * we must TRY to get the second and subsequent locks. If
2198 * we can't get any, we must release all we have
2199 * and try again.
2200 */
2201
2202 if (try_lock) {
2203 /* try_lock must be 0 if i is 0. */
2204 /*
2205 * try_lock means we have an inode locked
2206 * that is in the AIL.
2207 */
2208 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002209 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 attempts++;
2211
2212 /*
2213 * Unlock all previous guys and try again.
2214 * xfs_iunlock will try to push the tail
2215 * if the inode is in the AIL.
2216 */
2217
2218 for(j = i - 1; j >= 0; j--) {
2219
2220 /*
2221 * Check to see if we've already
2222 * unlocked this one.
2223 * Not the first one going back,
2224 * and the inode ptr is the same.
2225 */
2226 if ((j != (i - 1)) && ips[j] ==
2227 ips[j+1])
2228 continue;
2229
2230 xfs_iunlock(ips[j], lock_mode);
2231 }
2232
2233 if ((attempts % 5) == 0) {
2234 delay(1); /* Don't just spin the CPU */
2235#ifdef DEBUG
2236 xfs_lock_delays++;
2237#endif
2238 }
2239 i = 0;
2240 try_lock = 0;
2241 goto again;
2242 }
2243 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002244 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246 }
2247
2248#ifdef DEBUG
2249 if (attempts) {
2250 if (attempts < 5) xfs_small_retries++;
2251 else if (attempts < 100) xfs_middle_retries++;
2252 else xfs_lots_retries++;
2253 } else {
2254 xfs_locked_n++;
2255 }
2256#endif
2257}
2258
2259#ifdef DEBUG
2260#define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2261int remove_which_error_return = 0;
2262#else /* ! DEBUG */
2263#define REMOVE_DEBUG_TRACE(x)
2264#endif /* ! DEBUG */
2265
Christoph Hellwig993386c12007-08-28 16:12:30 +10002266int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267xfs_remove(
Christoph Hellwig993386c12007-08-28 16:12:30 +10002268 xfs_inode_t *dp,
2269 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 char *name = VNAME(dentry);
Christoph Hellwig993386c12007-08-28 16:12:30 +10002272 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig43973962008-03-06 13:44:50 +11002273 xfs_inode_t *ip = VNAME_TO_INODE(dentry);
2274 int namelen = VNAMELEN(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 xfs_trans_t *tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 int error = 0;
2277 xfs_bmap_free_t free_list;
2278 xfs_fsblock_t first_block;
2279 int cancel_flags;
2280 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 int link_zero;
2282 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002284 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (XFS_FORCED_SHUTDOWN(mp))
2287 return XFS_ERROR(EIO);
2288
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002289 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002290 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Christoph Hellwig43973962008-03-06 13:44:50 +11002292 name, NULL, ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (error)
2294 return error;
2295 }
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 /*
2298 * We need to get a reference to ip before we get our log
2299 * reservation. The reason for this is that we cannot call
2300 * xfs_iget for an inode for which we do not have a reference
2301 * once we've acquired a log reservation. This is because the
2302 * inode we are trying to get might be in xfs_inactive going
2303 * for a log reservation. Since we'll have to wait for the
2304 * inactive code to complete before returning from xfs_iget,
2305 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002306 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 * to the inode before getting our log reservation.
2308 */
Christoph Hellwig43973962008-03-06 13:44:50 +11002309 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002311 xfs_itrace_entry(ip);
2312 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 error = XFS_QM_DQATTACH(mp, dp, 0);
2315 if (!error && dp != ip)
2316 error = XFS_QM_DQATTACH(mp, ip, 0);
2317 if (error) {
2318 REMOVE_DEBUG_TRACE(__LINE__);
2319 IRELE(ip);
2320 goto std_return;
2321 }
2322
2323 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2324 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2325 /*
2326 * We try to get the real space reservation first,
2327 * allowing for directory btree deletion(s) implying
2328 * possible bmap insert(s). If we can't get the space
2329 * reservation then we use 0 instead, and avoid the bmap
2330 * btree insert(s) in the directory code by, if the bmap
2331 * insert tries to happen, instead trimming the LAST
2332 * block from the directory.
2333 */
2334 resblks = XFS_REMOVE_SPACE_RES(mp);
2335 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2336 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2337 if (error == ENOSPC) {
2338 resblks = 0;
2339 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2340 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2341 }
2342 if (error) {
2343 ASSERT(error != ENOSPC);
2344 REMOVE_DEBUG_TRACE(__LINE__);
2345 xfs_trans_cancel(tp, 0);
2346 IRELE(ip);
2347 return error;
2348 }
2349
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002350 error = xfs_lock_dir_and_entry(dp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (error) {
2352 REMOVE_DEBUG_TRACE(__LINE__);
2353 xfs_trans_cancel(tp, cancel_flags);
2354 IRELE(ip);
2355 goto std_return;
2356 }
2357
2358 /*
2359 * At this point, we've gotten both the directory and the entry
2360 * inodes locked.
2361 */
2362 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2363 if (dp != ip) {
2364 /*
2365 * Increment vnode ref count only in this case since
2366 * there's an extra vnode reference in the case where
2367 * dp == ip.
2368 */
2369 IHOLD(dp);
2370 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2371 }
2372
2373 /*
2374 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2375 */
2376 XFS_BMAP_INIT(&free_list, &first_block);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002377 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2378 &first_block, &free_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (error) {
2380 ASSERT(error != ENOENT);
2381 REMOVE_DEBUG_TRACE(__LINE__);
2382 goto error1;
2383 }
2384 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2385
2386 dp->i_gen++;
2387 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2388
2389 error = xfs_droplink(tp, ip);
2390 if (error) {
2391 REMOVE_DEBUG_TRACE(__LINE__);
2392 goto error1;
2393 }
2394
2395 /* Determine if this is the last link while
2396 * we are in the transaction.
2397 */
2398 link_zero = (ip)->i_d.di_nlink==0;
2399
2400 /*
2401 * Take an extra ref on the inode so that it doesn't
2402 * go to xfs_inactive() from within the commit.
2403 */
2404 IHOLD(ip);
2405
2406 /*
2407 * If this is a synchronous mount, make sure that the
2408 * remove transaction goes to disk before returning to
2409 * the user.
2410 */
2411 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2412 xfs_trans_set_sync(tp);
2413 }
2414
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002415 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (error) {
2417 REMOVE_DEBUG_TRACE(__LINE__);
2418 goto error_rele;
2419 }
2420
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002421 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (error) {
2423 IRELE(ip);
2424 goto std_return;
2425 }
2426
2427 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002428 * If we are using filestreams, kill the stream association.
2429 * If the file is still open it may get a new one but that
2430 * will get killed on last close in xfs_close() so we don't
2431 * have to worry about that.
2432 */
2433 if (link_zero && xfs_inode_is_filestream(ip))
2434 xfs_filestream_deassociate(ip);
2435
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002436 xfs_itrace_exit(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 IRELE(ip);
2438
2439/* Fall through to std_return with error = 0 */
2440 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002441 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002443 dp, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 NULL, DM_RIGHT_NULL,
Christoph Hellwig43973962008-03-06 13:44:50 +11002445 name, NULL, ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447 return error;
2448
2449 error1:
2450 xfs_bmap_cancel(&free_list);
2451 cancel_flags |= XFS_TRANS_ABORT;
2452 xfs_trans_cancel(tp, cancel_flags);
2453 goto std_return;
2454
2455 error_rele:
2456 /*
2457 * In this case make sure to not release the inode until after
2458 * the current transaction is aborted. Releasing it beforehand
2459 * can cause us to go to xfs_inactive and start a recursive
2460 * transaction which can easily deadlock with the current one.
2461 */
2462 xfs_bmap_cancel(&free_list);
2463 cancel_flags |= XFS_TRANS_ABORT;
2464 xfs_trans_cancel(tp, cancel_flags);
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 IRELE(ip);
2467
2468 goto std_return;
2469}
2470
Christoph Hellwig993386c12007-08-28 16:12:30 +10002471int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472xfs_link(
Christoph Hellwig993386c12007-08-28 16:12:30 +10002473 xfs_inode_t *tdp,
Nathan Scott67fcaa72006-06-09 17:00:52 +10002474 bhv_vnode_t *src_vp,
Christoph Hellwig993386c12007-08-28 16:12:30 +10002475 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Christoph Hellwig993386c12007-08-28 16:12:30 +10002477 bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
2478 xfs_mount_t *mp = tdp->i_mount;
2479 xfs_inode_t *sip = xfs_vtoi(src_vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 xfs_inode_t *ips[2];
2482 int error;
2483 xfs_bmap_free_t free_list;
2484 xfs_fsblock_t first_block;
2485 int cancel_flags;
2486 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 int resblks;
2488 char *target_name = VNAME(dentry);
2489 int target_namelen;
2490
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002491 xfs_itrace_entry(tdp);
2492 xfs_itrace_entry(xfs_vtoi(src_vp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 target_namelen = VNAMELEN(dentry);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002495 ASSERT(!VN_ISDIR(src_vp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (XFS_FORCED_SHUTDOWN(mp))
2498 return XFS_ERROR(EIO);
2499
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002500 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002502 tdp, DM_RIGHT_NULL,
2503 sip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 target_name, NULL, 0, 0, 0);
2505 if (error)
2506 return error;
2507 }
2508
2509 /* Return through std_return after this point. */
2510
2511 error = XFS_QM_DQATTACH(mp, sip, 0);
2512 if (!error && sip != tdp)
2513 error = XFS_QM_DQATTACH(mp, tdp, 0);
2514 if (error)
2515 goto std_return;
2516
2517 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2518 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2519 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2520 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2521 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2522 if (error == ENOSPC) {
2523 resblks = 0;
2524 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2525 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2526 }
2527 if (error) {
2528 cancel_flags = 0;
2529 goto error_return;
2530 }
2531
2532 if (sip->i_ino < tdp->i_ino) {
2533 ips[0] = sip;
2534 ips[1] = tdp;
2535 } else {
2536 ips[0] = tdp;
2537 ips[1] = sip;
2538 }
2539
2540 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2541
2542 /*
2543 * Increment vnode ref counts since xfs_trans_commit &
2544 * xfs_trans_cancel will both unlock the inodes and
2545 * decrement the associated ref counts.
2546 */
2547 VN_HOLD(src_vp);
2548 VN_HOLD(target_dir_vp);
2549 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2550 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2551
2552 /*
2553 * If the source has too many links, we can't make any more to it.
2554 */
2555 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2556 error = XFS_ERROR(EMLINK);
2557 goto error_return;
2558 }
2559
Nathan Scott365ca832005-06-21 15:39:12 +10002560 /*
2561 * If we are using project inheritance, we only allow hard link
2562 * creation in our tree when the project IDs are the same; else
2563 * the tree quota mechanism could be circumvented.
2564 */
2565 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2566 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002567 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002568 goto error_return;
2569 }
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002572 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 goto error_return;
2574
2575 XFS_BMAP_INIT(&free_list, &first_block);
2576
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002577 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 sip->i_ino, &first_block, &free_list,
2579 resblks);
2580 if (error)
2581 goto abort_return;
2582 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2583 tdp->i_gen++;
2584 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2585
2586 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002587 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 /*
2591 * If this is a synchronous mount, make sure that the
2592 * link transaction goes to disk before returning to
2593 * the user.
2594 */
2595 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2596 xfs_trans_set_sync(tp);
2597 }
2598
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002599 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if (error) {
2601 xfs_bmap_cancel(&free_list);
2602 goto abort_return;
2603 }
2604
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002605 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002606 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 /* Fall through to std_return with error = 0. */
2610std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002611 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002613 tdp, DM_RIGHT_NULL,
2614 sip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 target_name, NULL, 0, error, 0);
2616 }
2617 return error;
2618
2619 abort_return:
2620 cancel_flags |= XFS_TRANS_ABORT;
2621 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 error_return:
2624 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 goto std_return;
2626}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002627
2628
Christoph Hellwig993386c12007-08-28 16:12:30 +10002629int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630xfs_mkdir(
Christoph Hellwig993386c12007-08-28 16:12:30 +10002631 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10002632 bhv_vname_t *dentry,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002633 mode_t mode,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002634 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 cred_t *credp)
2636{
2637 char *dir_name = VNAME(dentry);
Christoph Hellwig993386c12007-08-28 16:12:30 +10002638 int dir_namelen = VNAMELEN(dentry);
2639 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 xfs_inode_t *cdp; /* inode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 int cancel_flags;
2643 int error;
2644 int committed;
2645 xfs_bmap_free_t free_list;
2646 xfs_fsblock_t first_block;
Christoph Hellwig993386c12007-08-28 16:12:30 +10002647 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 boolean_t created = B_FALSE;
2649 int dm_event_sent = 0;
2650 xfs_prid_t prid;
2651 struct xfs_dquot *udqp, *gdqp;
2652 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 if (XFS_FORCED_SHUTDOWN(mp))
2655 return XFS_ERROR(EIO);
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002659 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002661 dp, DM_RIGHT_NULL, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 DM_RIGHT_NULL, dir_name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002663 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (error)
2665 return error;
2666 dm_event_sent = 1;
2667 }
2668
2669 /* Return through std_return after this point. */
2670
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002671 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 mp = dp->i_mount;
2674 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002675 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2676 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 else
2678 prid = (xfs_prid_t)dfltprid;
2679
2680 /*
2681 * Make sure that we have allocated dquot(s) on disk.
2682 */
2683 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002684 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2686 if (error)
2687 goto std_return;
2688
2689 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2690 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2691 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2692 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2693 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2694 if (error == ENOSPC) {
2695 resblks = 0;
2696 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2697 XFS_TRANS_PERM_LOG_RES,
2698 XFS_MKDIR_LOG_COUNT);
2699 }
2700 if (error) {
2701 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 goto error_return;
2703 }
2704
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002705 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c12007-08-28 16:12:30 +10002706 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 /*
2709 * Check for directory link count overflow.
2710 */
2711 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2712 error = XFS_ERROR(EMLINK);
2713 goto error_return;
2714 }
2715
2716 /*
2717 * Reserve disk quota and the inode.
2718 */
2719 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2720 if (error)
2721 goto error_return;
2722
2723 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002724 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 goto error_return;
2726 /*
2727 * create the directory inode.
2728 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002729 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 0, credp, prid, resblks > 0,
2731 &cdp, NULL);
2732 if (error) {
2733 if (error == ENOSPC)
2734 goto error_return;
2735 goto abort_return;
2736 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002737 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 /*
2740 * Now we add the directory inode to the transaction.
2741 * We waited until now since xfs_dir_ialloc might start
2742 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c12007-08-28 16:12:30 +10002743 * earlier, the locks might have gotten released. An error
2744 * from here on will result in the transaction cancel
2745 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002747 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c12007-08-28 16:12:30 +10002749 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 XFS_BMAP_INIT(&free_list, &first_block);
2752
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002753 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2754 &first_block, &free_list, resblks ?
2755 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (error) {
2757 ASSERT(error != ENOSPC);
2758 goto error1;
2759 }
2760 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2761
2762 /*
2763 * Bump the in memory version number of the parent directory
2764 * so that other processes accessing it will recognize that
2765 * the directory has changed.
2766 */
2767 dp->i_gen++;
2768
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002769 error = xfs_dir_init(tp, cdp, dp);
2770 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 cdp->i_gen = 1;
2774 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 created = B_TRUE;
2779
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002780 *ipp = cdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 IHOLD(cdp);
2782
2783 /*
2784 * Attach the dquots to the new inode and modify the icount incore.
2785 */
2786 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2787
2788 /*
2789 * If this is a synchronous mount, make sure that the
2790 * mkdir transaction goes to disk before returning to
2791 * the user.
2792 */
2793 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2794 xfs_trans_set_sync(tp);
2795 }
2796
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002797 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (error) {
2799 IRELE(cdp);
2800 goto error2;
2801 }
2802
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002803 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 XFS_QM_DQRELE(mp, udqp);
2805 XFS_QM_DQRELE(mp, gdqp);
2806 if (error) {
2807 IRELE(cdp);
2808 }
2809
2810 /* Fall through to std_return with error = 0 or errno from
2811 * xfs_trans_commit. */
2812
2813std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002814 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c12007-08-28 16:12:30 +10002815 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002817 dp, DM_RIGHT_NULL,
2818 created ? cdp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 DM_RIGHT_NULL,
2820 dir_name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002821 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 }
2823 return error;
2824
2825 error2:
2826 error1:
2827 xfs_bmap_cancel(&free_list);
2828 abort_return:
2829 cancel_flags |= XFS_TRANS_ABORT;
2830 error_return:
2831 xfs_trans_cancel(tp, cancel_flags);
2832 XFS_QM_DQRELE(mp, udqp);
2833 XFS_QM_DQRELE(mp, gdqp);
2834
Christoph Hellwig993386c12007-08-28 16:12:30 +10002835 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 goto std_return;
2839}
2840
Christoph Hellwig993386c12007-08-28 16:12:30 +10002841int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842xfs_rmdir(
Christoph Hellwig993386c12007-08-28 16:12:30 +10002843 xfs_inode_t *dp,
2844 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Christoph Hellwig993386c12007-08-28 16:12:30 +10002846 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 char *name = VNAME(dentry);
Christoph Hellwig993386c12007-08-28 16:12:30 +10002848 int namelen = VNAMELEN(dentry);
2849 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig43973962008-03-06 13:44:50 +11002850 xfs_inode_t *cdp = VNAME_TO_INODE(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 int error;
2853 xfs_bmap_free_t free_list;
2854 xfs_fsblock_t first_block;
2855 int cancel_flags;
2856 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 int last_cdp_link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 uint resblks;
2859
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002860 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Christoph Hellwig993386c12007-08-28 16:12:30 +10002862 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002865 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002867 dp, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 NULL, DM_RIGHT_NULL,
Christoph Hellwig43973962008-03-06 13:44:50 +11002869 name, NULL, cdp->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (error)
2871 return XFS_ERROR(error);
2872 }
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /*
2875 * We need to get a reference to cdp before we get our log
2876 * reservation. The reason for this is that we cannot call
2877 * xfs_iget for an inode for which we do not have a reference
2878 * once we've acquired a log reservation. This is because the
2879 * inode we are trying to get might be in xfs_inactive going
2880 * for a log reservation. Since we'll have to wait for the
2881 * inactive code to complete before returning from xfs_iget,
2882 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002883 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 * to the inode before getting our log reservation.
2885 */
Christoph Hellwig43973962008-03-06 13:44:50 +11002886 IHOLD(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
2888 /*
2889 * Get the dquots for the inodes.
2890 */
2891 error = XFS_QM_DQATTACH(mp, dp, 0);
2892 if (!error && dp != cdp)
2893 error = XFS_QM_DQATTACH(mp, cdp, 0);
2894 if (error) {
2895 IRELE(cdp);
2896 REMOVE_DEBUG_TRACE(__LINE__);
2897 goto std_return;
2898 }
2899
2900 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2901 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2902 /*
2903 * We try to get the real space reservation first,
2904 * allowing for directory btree deletion(s) implying
2905 * possible bmap insert(s). If we can't get the space
2906 * reservation then we use 0 instead, and avoid the bmap
2907 * btree insert(s) in the directory code by, if the bmap
2908 * insert tries to happen, instead trimming the LAST
2909 * block from the directory.
2910 */
2911 resblks = XFS_REMOVE_SPACE_RES(mp);
2912 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2913 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2914 if (error == ENOSPC) {
2915 resblks = 0;
2916 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2917 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2918 }
2919 if (error) {
2920 ASSERT(error != ENOSPC);
2921 cancel_flags = 0;
2922 IRELE(cdp);
2923 goto error_return;
2924 }
2925 XFS_BMAP_INIT(&free_list, &first_block);
2926
2927 /*
2928 * Now lock the child directory inode and the parent directory
2929 * inode in the proper order. This will take care of validating
2930 * that the directory entry for the child directory inode has
2931 * not changed while we were obtaining a log reservation.
2932 */
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002933 error = xfs_lock_dir_and_entry(dp, cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (error) {
2935 xfs_trans_cancel(tp, cancel_flags);
2936 IRELE(cdp);
2937 goto std_return;
2938 }
2939
2940 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2941 if (dp != cdp) {
2942 /*
2943 * Only increment the parent directory vnode count if
2944 * we didn't bump it in looking up cdp. The only time
2945 * we don't bump it is when we're looking up ".".
2946 */
2947 VN_HOLD(dir_vp);
2948 }
2949
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002950 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
2952
2953 ASSERT(cdp->i_d.di_nlink >= 2);
2954 if (cdp->i_d.di_nlink != 2) {
2955 error = XFS_ERROR(ENOTEMPTY);
2956 goto error_return;
2957 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002958 if (!xfs_dir_isempty(cdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 error = XFS_ERROR(ENOTEMPTY);
2960 goto error_return;
2961 }
2962
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002963 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
2964 &first_block, &free_list, resblks);
2965 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2969
2970 /*
2971 * Bump the in memory generation count on the parent
2972 * directory so that other can know that it has changed.
2973 */
2974 dp->i_gen++;
2975
2976 /*
2977 * Drop the link from cdp's "..".
2978 */
2979 error = xfs_droplink(tp, dp);
2980 if (error) {
2981 goto error1;
2982 }
2983
2984 /*
2985 * Drop the link from dp to cdp.
2986 */
2987 error = xfs_droplink(tp, cdp);
2988 if (error) {
2989 goto error1;
2990 }
2991
2992 /*
2993 * Drop the "." link from cdp to self.
2994 */
2995 error = xfs_droplink(tp, cdp);
2996 if (error) {
2997 goto error1;
2998 }
2999
3000 /* Determine these before committing transaction */
3001 last_cdp_link = (cdp)->i_d.di_nlink==0;
3002
3003 /*
3004 * Take an extra ref on the child vnode so that it
3005 * does not go to xfs_inactive() from within the commit.
3006 */
3007 IHOLD(cdp);
3008
3009 /*
3010 * If this is a synchronous mount, make sure that the
3011 * rmdir transaction goes to disk before returning to
3012 * the user.
3013 */
3014 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3015 xfs_trans_set_sync(tp);
3016 }
3017
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003018 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 if (error) {
3020 xfs_bmap_cancel(&free_list);
3021 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3022 XFS_TRANS_ABORT));
3023 IRELE(cdp);
3024 goto std_return;
3025 }
3026
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003027 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 if (error) {
3029 IRELE(cdp);
3030 goto std_return;
3031 }
3032
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 IRELE(cdp);
3035
3036 /* Fall through to std_return with error = 0 or the errno
3037 * from xfs_trans_commit. */
Nathan Scottbb19fba2006-03-22 14:12:12 +11003038 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003039 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003041 dp, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 NULL, DM_RIGHT_NULL,
Christoph Hellwig43973962008-03-06 13:44:50 +11003043 name, NULL, cdp->i_d.di_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 error, 0);
3045 }
3046 return error;
3047
Nathan Scottbb19fba2006-03-22 14:12:12 +11003048 error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 xfs_bmap_cancel(&free_list);
3050 cancel_flags |= XFS_TRANS_ABORT;
Jesper Juhl014c2542006-01-15 02:37:08 +01003051 /* FALLTHROUGH */
3052
Nathan Scottbb19fba2006-03-22 14:12:12 +11003053 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 xfs_trans_cancel(tp, cancel_flags);
3055 goto std_return;
3056}
3057
Christoph Hellwig993386c12007-08-28 16:12:30 +10003058int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059xfs_symlink(
Christoph Hellwig993386c12007-08-28 16:12:30 +10003060 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10003061 bhv_vname_t *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10003063 mode_t mode,
Nathan Scott67fcaa72006-06-09 17:00:52 +10003064 bhv_vnode_t **vpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 cred_t *credp)
3066{
Christoph Hellwig993386c12007-08-28 16:12:30 +10003067 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
3068 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 xfs_inode_t *ip;
3071 int error;
3072 int pathlen;
3073 xfs_bmap_free_t free_list;
3074 xfs_fsblock_t first_block;
Christoph Hellwig993386c12007-08-28 16:12:30 +10003075 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 uint cancel_flags;
3077 int committed;
3078 xfs_fileoff_t first_fsb;
3079 xfs_filblks_t fs_blocks;
3080 int nmaps;
3081 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3082 xfs_daddr_t d;
3083 char *cur_chunk;
3084 int byte_cnt;
3085 int n;
3086 xfs_buf_t *bp;
3087 xfs_prid_t prid;
3088 struct xfs_dquot *udqp, *gdqp;
3089 uint resblks;
3090 char *link_name = VNAME(dentry);
3091 int link_namelen;
3092
3093 *vpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 error = 0;
3095 ip = NULL;
3096 tp = NULL;
3097
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003098 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100 if (XFS_FORCED_SHUTDOWN(mp))
3101 return XFS_ERROR(EIO);
3102
3103 link_namelen = VNAMELEN(dentry);
3104
3105 /*
3106 * Check component lengths of the target path name.
3107 */
3108 pathlen = strlen(target_path);
3109 if (pathlen >= MAXPATHLEN) /* total string too long */
3110 return XFS_ERROR(ENAMETOOLONG);
3111 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3112 int len, total;
3113 char *path;
3114
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003115 for (total = 0, path = target_path; total < pathlen;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 /*
3117 * Skip any slashes.
3118 */
3119 while(*path == '/') {
3120 total++;
3121 path++;
3122 }
3123
3124 /*
3125 * Count up to the next slash or end of path.
3126 * Error out if the component is bigger than MAXNAMELEN.
3127 */
3128 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3129 if (++len >= MAXNAMELEN) {
3130 error = ENAMETOOLONG;
3131 return error;
3132 }
3133 }
3134 }
3135 }
3136
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003137 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003138 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3140 link_name, target_path, 0, 0, 0);
3141 if (error)
3142 return error;
3143 }
3144
3145 /* Return through std_return after this point. */
3146
3147 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10003148 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3149 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 else
3151 prid = (xfs_prid_t)dfltprid;
3152
3153 /*
3154 * Make sure that we have allocated dquot(s) on disk.
3155 */
3156 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10003157 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3159 if (error)
3160 goto std_return;
3161
3162 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3163 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3164 /*
3165 * The symlink will fit into the inode data fork?
3166 * There can't be any attributes so we get the whole variable part.
3167 */
3168 if (pathlen <= XFS_LITINO(mp))
3169 fs_blocks = 0;
3170 else
3171 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3172 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3173 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3174 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3175 if (error == ENOSPC && fs_blocks == 0) {
3176 resblks = 0;
3177 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3178 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3179 }
3180 if (error) {
3181 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 goto error_return;
3183 }
3184
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10003185 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c12007-08-28 16:12:30 +10003186 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
3188 /*
3189 * Check whether the directory allows new symlinks or not.
3190 */
3191 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3192 error = XFS_ERROR(EPERM);
3193 goto error_return;
3194 }
3195
3196 /*
3197 * Reserve disk quota : blocks and inode.
3198 */
3199 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3200 if (error)
3201 goto error_return;
3202
3203 /*
3204 * Check for ability to enter directory entry, if no space reserved.
3205 */
3206 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003207 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 goto error_return;
3209 /*
3210 * Initialize the bmap freelist prior to calling either
3211 * bmapi or the directory create code.
3212 */
3213 XFS_BMAP_INIT(&free_list, &first_block);
3214
3215 /*
3216 * Allocate an inode for the symlink.
3217 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10003218 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 1, 0, credp, prid, resblks > 0, &ip, NULL);
3220 if (error) {
3221 if (error == ENOSPC)
3222 goto error_return;
3223 goto error1;
3224 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003225 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Christoph Hellwig993386c12007-08-28 16:12:30 +10003227 /*
3228 * An error after we've joined dp to the transaction will result in the
3229 * transaction cancel unlocking dp so don't do it explicitly in the
3230 * error path.
3231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 VN_HOLD(dir_vp);
3233 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c12007-08-28 16:12:30 +10003234 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 /*
3237 * Also attach the dquot(s) to it, if applicable.
3238 */
3239 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3240
3241 if (resblks)
3242 resblks -= XFS_IALLOC_SPACE_RES(mp);
3243 /*
3244 * If the symlink will fit into the inode, write it inline.
3245 */
3246 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3247 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3248 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3249 ip->i_d.di_size = pathlen;
3250
3251 /*
3252 * The inode was initially created in extent format.
3253 */
3254 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3255 ip->i_df.if_flags |= XFS_IFINLINE;
3256
3257 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3258 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3259
3260 } else {
3261 first_fsb = 0;
3262 nmaps = SYMLINK_MAPS;
3263
3264 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3265 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3266 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003267 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 if (error) {
3269 goto error1;
3270 }
3271
3272 if (resblks)
3273 resblks -= fs_blocks;
3274 ip->i_d.di_size = pathlen;
3275 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3276
3277 cur_chunk = target_path;
3278 for (n = 0; n < nmaps; n++) {
3279 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3280 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3281 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3282 BTOBB(byte_cnt), 0);
3283 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3284 if (pathlen < byte_cnt) {
3285 byte_cnt = pathlen;
3286 }
3287 pathlen -= byte_cnt;
3288
3289 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3290 cur_chunk += byte_cnt;
3291
3292 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3293 }
3294 }
3295
3296 /*
3297 * Create the directory entry for the symlink.
3298 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003299 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3300 &first_block, &free_list, resblks);
3301 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3304 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3305
3306 /*
3307 * Bump the in memory version number of the parent directory
3308 * so that other processes accessing it will recognize that
3309 * the directory has changed.
3310 */
3311 dp->i_gen++;
3312
3313 /*
3314 * If this is a synchronous mount, make sure that the
3315 * symlink transaction goes to disk before returning to
3316 * the user.
3317 */
3318 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3319 xfs_trans_set_sync(tp);
3320 }
3321
3322 /*
3323 * xfs_trans_commit normally decrements the vnode ref count
3324 * when it unlocks the inode. Since we want to return the
3325 * vnode to the caller, we bump the vnode ref count now.
3326 */
3327 IHOLD(ip);
3328
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003329 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (error) {
3331 goto error2;
3332 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003333 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 XFS_QM_DQRELE(mp, udqp);
3335 XFS_QM_DQRELE(mp, gdqp);
3336
3337 /* Fall through to std_return with error = 0 or errno from
3338 * xfs_trans_commit */
3339std_return:
Christoph Hellwig993386c12007-08-28 16:12:30 +10003340 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003342 dp, DM_RIGHT_NULL,
3343 error ? NULL : ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 DM_RIGHT_NULL, link_name, target_path,
3345 0, error, 0);
3346 }
3347
3348 if (!error) {
Nathan Scott67fcaa72006-06-09 17:00:52 +10003349 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
3351 ASSERT(ip);
3352 vp = XFS_ITOV(ip);
3353 *vpp = vp;
3354 }
3355 return error;
3356
3357 error2:
3358 IRELE(ip);
3359 error1:
3360 xfs_bmap_cancel(&free_list);
3361 cancel_flags |= XFS_TRANS_ABORT;
3362 error_return:
3363 xfs_trans_cancel(tp, cancel_flags);
3364 XFS_QM_DQRELE(mp, udqp);
3365 XFS_QM_DQRELE(mp, gdqp);
3366
Christoph Hellwig993386c12007-08-28 16:12:30 +10003367 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
3370 goto std_return;
3371}
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374xfs_inode_flush(
Christoph Hellwig993386c12007-08-28 16:12:30 +10003375 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 int flags)
3377{
Christoph Hellwig993386c12007-08-28 16:12:30 +10003378 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 int error = 0;
3380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (XFS_FORCED_SHUTDOWN(mp))
3382 return XFS_ERROR(EIO);
3383
3384 /*
3385 * Bypass inodes which have already been cleaned by
3386 * the inode flush clustering code inside xfs_iflush
3387 */
David Chinner33540402008-03-06 13:43:59 +11003388 if (xfs_inode_clean(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 return 0;
3390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 /*
3392 * We make this non-blocking if the inode is contended,
3393 * return EAGAIN to indicate to the caller that they
3394 * did not succeed. This prevents the flush path from
3395 * blocking on inodes inside another operation right
3396 * now, they get caught later by xfs_sync.
3397 */
David Chinnera3f74ff2008-03-06 13:43:42 +11003398 if (flags & FLUSH_SYNC) {
3399 xfs_ilock(ip, XFS_ILOCK_SHARED);
3400 xfs_iflock(ip);
3401 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3402 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3403 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return EAGAIN;
3405 }
David Chinnera3f74ff2008-03-06 13:43:42 +11003406 } else {
3407 return EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409
David Chinnera3f74ff2008-03-06 13:43:42 +11003410 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
3411 : XFS_IFLUSH_ASYNC_NOBLOCK);
3412 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return error;
3415}
3416
Christoph Hellwig993386c12007-08-28 16:12:30 +10003417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418int
Christoph Hellwig993386c12007-08-28 16:12:30 +10003419xfs_set_dmattrs(
3420 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 u_int evmask,
Christoph Hellwig993386c12007-08-28 16:12:30 +10003422 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
Christoph Hellwig993386c12007-08-28 16:12:30 +10003424 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 int error;
3427
3428 if (!capable(CAP_SYS_ADMIN))
3429 return XFS_ERROR(EPERM);
3430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 if (XFS_FORCED_SHUTDOWN(mp))
3432 return XFS_ERROR(EIO);
3433
3434 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3435 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3436 if (error) {
3437 xfs_trans_cancel(tp, 0);
3438 return error;
3439 }
3440 xfs_ilock(ip, XFS_ILOCK_EXCL);
3441 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3442
Christoph Hellwig613d7042007-10-11 17:44:08 +10003443 ip->i_d.di_dmevmask = evmask;
3444 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
3446 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3447 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003448 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 return error;
3451}
3452
Christoph Hellwig993386c12007-08-28 16:12:30 +10003453int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454xfs_reclaim(
Christoph Hellwig993386c12007-08-28 16:12:30 +10003455 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
Christoph Hellwig993386c12007-08-28 16:12:30 +10003457 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003459 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 ASSERT(!VN_MAPPED(vp));
3462
3463 /* bad inode, get out here ASAP */
3464 if (VN_BAD(vp)) {
3465 xfs_ireclaim(ip);
3466 return 0;
3467 }
3468
Christoph Hellwigb677c212007-08-29 11:46:28 +10003469 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10003471 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11003473 /*
3474 * Make sure the atime in the XFS inode is correct before freeing the
3475 * Linux inode.
3476 */
3477 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
David Chinner4c606582006-11-11 18:05:00 +11003479 /*
3480 * If we have nothing to flush with this inode then complete the
3481 * teardown now, otherwise break the link between the xfs inode and the
3482 * linux inode and clean up the xfs inode later. This avoids flushing
3483 * the inode to disk during the delete operation itself.
3484 *
3485 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3486 * first to ensure that xfs_iunpin() will never see an xfs inode
3487 * that has a linux inode being reclaimed. Synchronisation is provided
3488 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 */
3490 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3491 xfs_ilock(ip, XFS_ILOCK_EXCL);
3492 xfs_iflock(ip);
3493 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3494 } else {
3495 xfs_mount_t *mp = ip->i_mount;
3496
David Chinner4c606582006-11-11 18:05:00 +11003497 /* Protect sync and unpin from us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 XFS_MOUNT_ILOCK(mp);
David Chinner4c606582006-11-11 18:05:00 +11003499 spin_lock(&ip->i_flags_lock);
3500 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10003501 vn_to_inode(vp)->i_private = NULL;
3502 ip->i_vnode = NULL;
David Chinner4c606582006-11-11 18:05:00 +11003503 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 XFS_MOUNT_IUNLOCK(mp);
3506 }
3507 return 0;
3508}
3509
3510int
3511xfs_finish_reclaim(
3512 xfs_inode_t *ip,
3513 int locked,
3514 int sync_mode)
3515{
David Chinnerda353b02007-08-28 14:00:13 +10003516 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
Nathan Scott67fcaa72006-06-09 17:00:52 +10003517 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 int error;
3519
3520 if (vp && VN_BAD(vp))
3521 goto reclaim;
3522
3523 /* The hash lock here protects a thread in xfs_iget_core from
3524 * racing with us on linking the inode back with a vnode.
3525 * Once we have the XFS_IRECLAIM flag set it will not touch
3526 * us.
3527 */
David Chinnerda353b02007-08-28 14:00:13 +10003528 write_lock(&pag->pag_ici_lock);
David Chinnerf273ab82006-09-28 11:06:03 +10003529 spin_lock(&ip->i_flags_lock);
David Chinner7a18c382006-11-11 18:04:54 +11003530 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3531 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
David Chinnerf273ab82006-09-28 11:06:03 +10003532 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003533 write_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 if (locked) {
3535 xfs_ifunlock(ip);
3536 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3537 }
Jesper Juhl014c2542006-01-15 02:37:08 +01003538 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
David Chinner7a18c382006-11-11 18:04:54 +11003540 __xfs_iflags_set(ip, XFS_IRECLAIM);
David Chinnerf273ab82006-09-28 11:06:03 +10003541 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003542 write_unlock(&pag->pag_ici_lock);
3543 xfs_put_perag(ip->i_mount, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 /*
3546 * If the inode is still dirty, then flush it out. If the inode
3547 * is not in the AIL, then it will be OK to flush it delwri as
3548 * long as xfs_iflush() does not keep any references to the inode.
3549 * We leave that decision up to xfs_iflush() since it has the
3550 * knowledge of whether it's OK to simply do a delwri flush of
3551 * the inode or whether we need to wait until the inode is
3552 * pulled from the AIL.
3553 * We get the flush lock regardless, though, just to make sure
3554 * we don't free it while it is being flushed.
3555 */
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003556 if (!locked) {
3557 xfs_ilock(ip, XFS_ILOCK_EXCL);
3558 xfs_iflock(ip);
3559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003561 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 if (ip->i_update_core ||
3563 ((ip->i_itemp != NULL) &&
3564 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3565 error = xfs_iflush(ip, sync_mode);
3566 /*
3567 * If we hit an error, typically because of filesystem
3568 * shutdown, we don't need to let vn_reclaim to know
3569 * because we're gonna reclaim the inode anyway.
3570 */
3571 if (error) {
3572 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3573 goto reclaim;
3574 }
3575 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3576 }
3577
3578 ASSERT(ip->i_update_core == 0);
3579 ASSERT(ip->i_itemp == NULL ||
3580 ip->i_itemp->ili_format.ilf_fields == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 }
3582
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003583 xfs_ifunlock(ip);
3584 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 reclaim:
3587 xfs_ireclaim(ip);
3588 return 0;
3589}
3590
3591int
3592xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3593{
3594 int purged;
3595 xfs_inode_t *ip, *n;
3596 int done = 0;
3597
3598 while (!done) {
3599 purged = 0;
3600 XFS_MOUNT_ILOCK(mp);
3601 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3602 if (noblock) {
3603 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3604 continue;
3605 if (xfs_ipincount(ip) ||
3606 !xfs_iflock_nowait(ip)) {
3607 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3608 continue;
3609 }
3610 }
3611 XFS_MOUNT_IUNLOCK(mp);
Felix Blyakher6b2cf612005-11-25 16:42:13 +11003612 if (xfs_finish_reclaim(ip, noblock,
3613 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3614 delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 purged = 1;
3616 break;
3617 }
3618
3619 done = !purged;
3620 }
3621
3622 XFS_MOUNT_IUNLOCK(mp);
3623 return 0;
3624}
3625
3626/*
3627 * xfs_alloc_file_space()
3628 * This routine allocates disk space for the given file.
3629 *
3630 * If alloc_type == 0, this request is for an ALLOCSP type
3631 * request which will change the file size. In this case, no
3632 * DMAPI event will be generated by the call. A TRUNCATE event
3633 * will be generated later by xfs_setattr.
3634 *
3635 * If alloc_type != 0, this request is for a RESVSP type
3636 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3637 * lower block boundary byte address is less than the file's
3638 * length.
3639 *
3640 * RETURNS:
3641 * 0 on success
3642 * errno on error
3643 *
3644 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003645STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646xfs_alloc_file_space(
3647 xfs_inode_t *ip,
3648 xfs_off_t offset,
3649 xfs_off_t len,
3650 int alloc_type,
3651 int attr_flags)
3652{
Nathan Scottdd9f4382006-01-11 15:28:28 +11003653 xfs_mount_t *mp = ip->i_mount;
3654 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 xfs_filblks_t allocated_fsb;
3656 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003657 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003659 xfs_fsblock_t firstfsb;
3660 int nimaps;
3661 int bmapi_flag;
3662 int quota_flag;
3663 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003665 xfs_bmbt_irec_t imaps[1], *imapp;
3666 xfs_bmap_free_t free_list;
3667 uint qblocks, resblks, resrtextents;
3668 int committed;
3669 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003671 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
3673 if (XFS_FORCED_SHUTDOWN(mp))
3674 return XFS_ERROR(EIO);
3675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3677 return error;
3678
3679 if (len <= 0)
3680 return XFS_ERROR(EINVAL);
3681
David Chinner957d0eb2007-06-18 16:50:37 +10003682 rt = XFS_IS_REALTIME_INODE(ip);
3683 extsz = xfs_get_extsz_hint(ip);
3684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11003687 nimaps = 1;
3688 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3690 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3691
3692 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003693 if (alloc_type != 0 && offset < ip->i_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 (attr_flags&ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003695 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 xfs_off_t end_dmi_offset;
3697
3698 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003699 if (end_dmi_offset > ip->i_size)
3700 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003701 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
3702 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003704 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
3706
3707 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003708 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 */
3710retry:
3711 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003712 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Nathan Scottdd9f4382006-01-11 15:28:28 +11003714 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003715 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11003716 */
3717 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003719 do_div(s, extsz);
3720 s *= extsz;
3721 e = startoffset_fsb + allocatesize_fsb;
3722 if ((temp = do_mod(startoffset_fsb, extsz)))
3723 e += temp;
3724 if ((temp = do_mod(e, extsz)))
3725 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003727 s = 0;
3728 e = allocatesize_fsb;
3729 }
3730
3731 if (unlikely(rt)) {
3732 resrtextents = qblocks = (uint)(e - s);
3733 resrtextents /= mp->m_sb.sb_rextsize;
3734 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3735 quota_flag = XFS_QMOPT_RES_RTBLKS;
3736 } else {
3737 resrtextents = 0;
3738 resblks = qblocks = \
3739 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3740 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 }
3742
3743 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003744 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 */
3746 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003747 error = xfs_trans_reserve(tp, resblks,
3748 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 XFS_TRANS_PERM_LOG_RES,
3750 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003752 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 */
3754 if (error) {
3755 /*
3756 * Free the transaction structure.
3757 */
3758 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3759 xfs_trans_cancel(tp, 0);
3760 break;
3761 }
3762 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003763 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3764 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if (error)
3766 goto error1;
3767
3768 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3769 xfs_trans_ihold(tp, ip);
3770
3771 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003772 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 */
3774 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003775 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003776 allocatesize_fsb, bmapi_flag,
3777 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003778 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 if (error) {
3780 goto error0;
3781 }
3782
3783 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003784 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003786 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 if (error) {
3788 goto error0;
3789 }
3790
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003791 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3793 if (error) {
3794 break;
3795 }
3796
3797 allocated_fsb = imapp->br_blockcount;
3798
Nathan Scottdd9f4382006-01-11 15:28:28 +11003799 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 error = XFS_ERROR(ENOSPC);
3801 break;
3802 }
3803
3804 startoffset_fsb += allocated_fsb;
3805 allocatesize_fsb -= allocated_fsb;
3806 }
3807dmapi_enospc_check:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003808 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
3809 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003811 ip, DM_RIGHT_NULL,
3812 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3814 if (error == 0)
3815 goto retry; /* Maybe DMAPI app. has made space */
3816 /* else fall through with error from XFS_SEND_DATA */
3817 }
3818
3819 return error;
3820
Nathan Scottdd9f4382006-01-11 15:28:28 +11003821error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003823 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3824
3825error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3827 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3828 goto dmapi_enospc_check;
3829}
3830
3831/*
3832 * Zero file bytes between startoff and endoff inclusive.
3833 * The iolock is held exclusive and no blocks are buffered.
3834 */
3835STATIC int
3836xfs_zero_remaining_bytes(
3837 xfs_inode_t *ip,
3838 xfs_off_t startoff,
3839 xfs_off_t endoff)
3840{
3841 xfs_bmbt_irec_t imap;
3842 xfs_fileoff_t offset_fsb;
3843 xfs_off_t lastoffset;
3844 xfs_off_t offset;
3845 xfs_buf_t *bp;
3846 xfs_mount_t *mp = ip->i_mount;
3847 int nimap;
3848 int error = 0;
3849
3850 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003851 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 mp->m_rtdev_targp : mp->m_ddev_targp);
3853
3854 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3855 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3856 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003857 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003858 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 if (error || nimap < 1)
3860 break;
3861 ASSERT(imap.br_blockcount >= 1);
3862 ASSERT(imap.br_startoff == offset_fsb);
3863 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3864 if (lastoffset > endoff)
3865 lastoffset = endoff;
3866 if (imap.br_startblock == HOLESTARTBLOCK)
3867 continue;
3868 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3869 if (imap.br_state == XFS_EXT_UNWRITTEN)
3870 continue;
3871 XFS_BUF_UNDONE(bp);
3872 XFS_BUF_UNWRITE(bp);
3873 XFS_BUF_READ(bp);
3874 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3875 xfsbdstrat(mp, bp);
3876 if ((error = xfs_iowait(bp))) {
3877 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3878 mp, bp, XFS_BUF_ADDR(bp));
3879 break;
3880 }
3881 memset(XFS_BUF_PTR(bp) +
3882 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3883 0, lastoffset - offset + 1);
3884 XFS_BUF_UNDONE(bp);
3885 XFS_BUF_UNREAD(bp);
3886 XFS_BUF_WRITE(bp);
3887 xfsbdstrat(mp, bp);
3888 if ((error = xfs_iowait(bp))) {
3889 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3890 mp, bp, XFS_BUF_ADDR(bp));
3891 break;
3892 }
3893 }
3894 xfs_buf_free(bp);
3895 return error;
3896}
3897
3898/*
3899 * xfs_free_file_space()
3900 * This routine frees disk space for the given file.
3901 *
3902 * This routine is only called by xfs_change_file_space
3903 * for an UNRESVSP type call.
3904 *
3905 * RETURNS:
3906 * 0 on success
3907 * errno on error
3908 *
3909 */
3910STATIC int
3911xfs_free_file_space(
3912 xfs_inode_t *ip,
3913 xfs_off_t offset,
3914 xfs_off_t len,
3915 int attr_flags)
3916{
Nathan Scott67fcaa72006-06-09 17:00:52 +10003917 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 int committed;
3919 int done;
3920 xfs_off_t end_dmi_offset;
3921 xfs_fileoff_t endoffset_fsb;
3922 int error;
3923 xfs_fsblock_t firstfsb;
3924 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 xfs_bmbt_irec_t imap;
3926 xfs_off_t ioffset;
3927 xfs_extlen_t mod=0;
3928 xfs_mount_t *mp;
3929 int nimap;
3930 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10003931 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 int rt;
3933 xfs_fileoff_t startoffset_fsb;
3934 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003935 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003937 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 mp = ip->i_mount;
3939
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003940 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3943 return error;
3944
3945 error = 0;
3946 if (len <= 0) /* if nothing being freed */
3947 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003948 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3950 end_dmi_offset = offset + len;
3951 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3952
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003953 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
3954 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003955 if (end_dmi_offset > ip->i_size)
3956 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003957 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 offset, end_dmi_offset - offset,
3959 AT_DELAY_FLAG(attr_flags), NULL);
3960 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003961 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 }
3963
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003964 if (attr_flags & ATTR_NOLOCK)
3965 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11003966 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10003968 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11003969 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003970
Tim Shimmine6a4b372007-11-23 16:30:42 +11003971 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003973
3974 if (VN_CACHED(vp) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003975 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3976 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10003977 if (error)
3978 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003979 }
3980
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 /*
3982 * Need to zero the stuff we're not freeing, on disk.
3983 * If its a realtime file & can't use unwritten extents then we
3984 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3985 * will take care of it for us.
3986 */
Eric Sandeen62118702008-03-06 13:44:28 +11003987 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003989 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003990 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 if (error)
3992 goto out_unlock_iolock;
3993 ASSERT(nimap == 0 || nimap == 1);
3994 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3995 xfs_daddr_t block;
3996
3997 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3998 block = imap.br_startblock;
3999 mod = do_div(block, mp->m_sb.sb_rextsize);
4000 if (mod)
4001 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4002 }
4003 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004004 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004005 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 if (error)
4007 goto out_unlock_iolock;
4008 ASSERT(nimap == 0 || nimap == 1);
4009 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4010 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4011 mod++;
4012 if (mod && (mod != mp->m_sb.sb_rextsize))
4013 endoffset_fsb -= mod;
4014 }
4015 }
4016 if ((done = (endoffset_fsb <= startoffset_fsb)))
4017 /*
4018 * One contiguous piece to clear
4019 */
4020 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4021 else {
4022 /*
4023 * Some full blocks, possibly two pieces to clear
4024 */
4025 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4026 error = xfs_zero_remaining_bytes(ip, offset,
4027 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4028 if (!error &&
4029 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4030 error = xfs_zero_remaining_bytes(ip,
4031 XFS_FSB_TO_B(mp, endoffset_fsb),
4032 offset + len - 1);
4033 }
4034
4035 /*
4036 * free file space until done or until there is an error
4037 */
4038 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4039 while (!error && !done) {
4040
4041 /*
David Chinner91ebecc2007-07-19 16:28:30 +10004042 * allocate and setup the transaction. Allow this
4043 * transaction to dip into the reserve blocks to ensure
4044 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 */
4046 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10004047 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 error = xfs_trans_reserve(tp,
4049 resblks,
4050 XFS_WRITE_LOG_RES(mp),
4051 0,
4052 XFS_TRANS_PERM_LOG_RES,
4053 XFS_WRITE_LOG_COUNT);
4054
4055 /*
4056 * check for running out of space
4057 */
4058 if (error) {
4059 /*
4060 * Free the transaction structure.
4061 */
4062 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4063 xfs_trans_cancel(tp, 0);
4064 break;
4065 }
4066 xfs_ilock(ip, XFS_ILOCK_EXCL);
4067 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004068 ip->i_udquot, ip->i_gdquot, resblks, 0,
4069 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 if (error)
4071 goto error1;
4072
4073 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4074 xfs_trans_ihold(tp, ip);
4075
4076 /*
4077 * issue the bunmapi() call to free the blocks
4078 */
4079 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004080 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004082 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 if (error) {
4084 goto error0;
4085 }
4086
4087 /*
4088 * complete the transaction
4089 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004090 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 if (error) {
4092 goto error0;
4093 }
4094
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004095 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4097 }
4098
4099 out_unlock_iolock:
4100 if (need_iolock)
4101 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4102 return error;
4103
4104 error0:
4105 xfs_bmap_cancel(&free_list);
4106 error1:
4107 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4108 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4109 XFS_ILOCK_EXCL);
4110 return error;
4111}
4112
4113/*
4114 * xfs_change_file_space()
4115 * This routine allocates or frees disk space for the given file.
4116 * The user specified parameters are checked for alignment and size
4117 * limitations.
4118 *
4119 * RETURNS:
4120 * 0 on success
4121 * errno on error
4122 *
4123 */
4124int
4125xfs_change_file_space(
Christoph Hellwig993386c12007-08-28 16:12:30 +10004126 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 int cmd,
4128 xfs_flock64_t *bf,
4129 xfs_off_t offset,
4130 cred_t *credp,
4131 int attr_flags)
4132{
Christoph Hellwig993386c12007-08-28 16:12:30 +10004133 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 int clrprealloc;
4135 int error;
4136 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 int setprealloc;
4138 xfs_off_t startoffset;
4139 xfs_off_t llen;
4140 xfs_trans_t *tp;
Nathan Scott8285fb52006-06-09 17:07:12 +10004141 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11004143 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
Christoph Hellwig993386c12007-08-28 16:12:30 +10004145 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 return XFS_ERROR(EINVAL);
4147
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 switch (bf->l_whence) {
4149 case 0: /*SEEK_SET*/
4150 break;
4151 case 1: /*SEEK_CUR*/
4152 bf->l_start += offset;
4153 break;
4154 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004155 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 break;
4157 default:
4158 return XFS_ERROR(EINVAL);
4159 }
4160
4161 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4162
4163 if ( (bf->l_start < 0)
4164 || (bf->l_start > XFS_MAXIOFFSET(mp))
4165 || (bf->l_start + llen < 0)
4166 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4167 return XFS_ERROR(EINVAL);
4168
4169 bf->l_whence = 0;
4170
4171 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004172 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
4174 /*
4175 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4176 * file space.
4177 * These calls do NOT zero the data space allocated to the file,
4178 * nor do they change the file size.
4179 *
4180 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4181 * space.
4182 * These calls cause the new file data to be zeroed and the file
4183 * size to be changed.
4184 */
4185 setprealloc = clrprealloc = 0;
4186
4187 switch (cmd) {
4188 case XFS_IOC_RESVSP:
4189 case XFS_IOC_RESVSP64:
4190 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4191 1, attr_flags);
4192 if (error)
4193 return error;
4194 setprealloc = 1;
4195 break;
4196
4197 case XFS_IOC_UNRESVSP:
4198 case XFS_IOC_UNRESVSP64:
4199 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4200 attr_flags)))
4201 return error;
4202 break;
4203
4204 case XFS_IOC_ALLOCSP:
4205 case XFS_IOC_ALLOCSP64:
4206 case XFS_IOC_FREESP:
4207 case XFS_IOC_FREESP64:
4208 if (startoffset > fsize) {
4209 error = xfs_alloc_file_space(ip, fsize,
4210 startoffset - fsize, 0, attr_flags);
4211 if (error)
4212 break;
4213 }
4214
4215 va.va_mask = XFS_AT_SIZE;
4216 va.va_size = startoffset;
4217
Christoph Hellwig993386c12007-08-28 16:12:30 +10004218 error = xfs_setattr(ip, &va, attr_flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
4220 if (error)
4221 return error;
4222
4223 clrprealloc = 1;
4224 break;
4225
4226 default:
4227 ASSERT(0);
4228 return XFS_ERROR(EINVAL);
4229 }
4230
4231 /*
4232 * update the inode timestamp, mode, and prealloc flag bits
4233 */
4234 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4235
4236 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4237 0, 0, 0))) {
4238 /* ASSERT(0); */
4239 xfs_trans_cancel(tp, 0);
4240 return error;
4241 }
4242
4243 xfs_ilock(ip, XFS_ILOCK_EXCL);
4244
4245 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4246 xfs_trans_ihold(tp, ip);
4247
4248 if ((attr_flags & ATTR_DMI) == 0) {
4249 ip->i_d.di_mode &= ~S_ISUID;
4250
4251 /*
4252 * Note that we don't have to worry about mandatory
4253 * file locking being disabled here because we only
4254 * clear the S_ISGID bit if the Group execute bit is
4255 * on, but if it was on then mandatory locking wouldn't
4256 * have been enabled.
4257 */
4258 if (ip->i_d.di_mode & S_IXGRP)
4259 ip->i_d.di_mode &= ~S_ISGID;
4260
4261 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4262 }
4263 if (setprealloc)
4264 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4265 else if (clrprealloc)
4266 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4267
4268 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4269 xfs_trans_set_sync(tp);
4270
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004271 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4274
4275 return error;
4276}