blob: 8ed7af3c5d63d3bdab304509641e2f32ecd10a65 [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"
51#include "xfs_refcache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "xfs_log_priv.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100054#include "xfs_filestream.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100055#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Christoph Hellwig993386c2007-08-28 16:12:30 +100057int
Linus Torvalds1da177e2005-04-16 15:20:36 -070058xfs_open(
Christoph Hellwig993386c2007-08-28 16:12:30 +100059 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
64 return XFS_ERROR(EIO);
65
66 /*
67 * If it's a directory with any blocks, read-ahead block 0
68 * as we're almost certain to have the next operation be a read there.
69 */
Christoph Hellwig993386c2007-08-28 16:12:30 +100070 if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 mode = xfs_ilock_map_shared(ip);
72 if (ip->i_d.di_nextents > 0)
73 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
74 xfs_iunlock(ip, mode);
75 }
76 return 0;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * xfs_getattr
81 */
Christoph Hellwig993386c2007-08-28 16:12:30 +100082int
Linus Torvalds1da177e2005-04-16 15:20:36 -070083xfs_getattr(
Christoph Hellwig993386c2007-08-28 16:12:30 +100084 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +100085 bhv_vattr_t *vap,
Christoph Hellwig993386c2007-08-28 16:12:30 +100086 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Christoph Hellwig993386c2007-08-28 16:12:30 +100088 bhv_vnode_t *vp = XFS_ITOV(ip);
89 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (XFS_FORCED_SHUTDOWN(mp))
94 return XFS_ERROR(EIO);
95
96 if (!(flags & ATTR_LAZY))
97 xfs_ilock(ip, XFS_ILOCK_SHARED);
98
Lachlan McIlroyba87ea62007-05-08 13:49:46 +100099 vap->va_size = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (vap->va_mask == XFS_AT_SIZE)
101 goto all_done;
102
103 vap->va_nblocks =
104 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
105 vap->va_nodeid = ip->i_ino;
106#if XFS_BIG_INUMS
107 vap->va_nodeid += mp->m_inoadd;
108#endif
109 vap->va_nlink = ip->i_d.di_nlink;
110
111 /*
112 * Quick exit for non-stat callers
113 */
114 if ((vap->va_mask &
115 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
116 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
117 goto all_done;
118
119 /*
120 * Copy from in-core inode.
121 */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000122 vap->va_mode = ip->i_d.di_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 vap->va_uid = ip->i_d.di_uid;
124 vap->va_gid = ip->i_d.di_gid;
125 vap->va_projid = ip->i_d.di_projid;
126
127 /*
128 * Check vnode type block/char vs. everything else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000130 switch (ip->i_d.di_mode & S_IFMT) {
131 case S_IFBLK:
132 case S_IFCHR:
133 vap->va_rdev = ip->i_df.if_u2.if_rdev;
134 vap->va_blocksize = BLKDEV_IOSIZE;
135 break;
136 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 vap->va_rdev = 0;
138
139 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
David Chinnere8c8b3a2005-11-02 10:33:05 +1100140 vap->va_blocksize = xfs_preferred_iosize(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 } else {
142
143 /*
144 * If the file blocks are being allocated from a
145 * realtime partition, then return the inode's
146 * realtime extent size or the realtime volume's
147 * extent size.
148 */
David Chinner957d0eb2007-06-18 16:50:37 +1000149 vap->va_blocksize =
150 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
Nathan Scottca5ccbf2006-01-11 21:03:04 +1100155 vn_atime_to_timespec(vp, &vap->va_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
157 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
158 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
159 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
160
161 /*
162 * Exit for stat callers. See if any of the rest of the fields
163 * to be filled in are needed.
164 */
165 if ((vap->va_mask &
166 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
167 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
168 goto all_done;
169
170 /*
171 * Convert di_flags to xflags.
172 */
173 vap->va_xflags = xfs_ip2xflags(ip);
174
175 /*
176 * Exit for inode revalidate. See if any of the rest of
177 * the fields to be filled in are needed.
178 */
179 if ((vap->va_mask &
180 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
181 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
182 goto all_done;
183
184 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
185 vap->va_nextents =
186 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
187 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
188 ip->i_d.di_nextents;
189 if (ip->i_afp)
190 vap->va_anextents =
191 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
192 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
193 ip->i_d.di_anextents;
194 else
195 vap->va_anextents = 0;
196 vap->va_gen = ip->i_d.di_gen;
197
198 all_done:
199 if (!(flags & ATTR_LAZY))
200 xfs_iunlock(ip, XFS_ILOCK_SHARED);
201 return 0;
202}
203
204
205/*
206 * xfs_setattr
207 */
208int
209xfs_setattr(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000210 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +1000211 bhv_vattr_t *vap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int flags,
213 cred_t *credp)
214{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000215 bhv_vnode_t *vp = XFS_ITOV(ip);
216 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int mask;
219 int code;
220 uint lock_flags;
221 uint commit_flags=0;
222 uid_t uid=0, iuid=0;
223 gid_t gid=0, igid=0;
224 int timeflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 xfs_prid_t projid=0, iprojid=0;
226 int mandlock_before, mandlock_after;
227 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
228 int file_owner;
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700229 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
232
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +1000233 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return XFS_ERROR(EROFS);
235
236 /*
237 * Cannot set certain attributes.
238 */
239 mask = vap->va_mask;
240 if (mask & XFS_AT_NOSET) {
241 return XFS_ERROR(EINVAL);
242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (XFS_FORCED_SHUTDOWN(mp))
245 return XFS_ERROR(EIO);
246
247 /*
248 * Timestamps do not need to be logged and hence do not
249 * need to be done within a transaction.
250 */
251 if (mask & XFS_AT_UPDTIMES) {
252 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
253 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
254 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
255 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
256 xfs_ichgtime(ip, timeflags);
257 return 0;
258 }
259
260 olddquot1 = olddquot2 = NULL;
261 udqp = gdqp = NULL;
262
263 /*
264 * If disk quotas is on, we make sure that the dquots do exist on disk,
265 * before we start any other transactions. Trying to do this later
266 * is messy. We don't care to take a readlock to look at the ids
267 * in inode here, because we can't hold it across the trans_reserve.
268 * If the IDs do change before we take the ilock, we're covered
269 * because the i_*dquot fields will get updated anyway.
270 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000271 if (XFS_IS_QUOTA_ON(mp) &&
272 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 uint qflags = 0;
274
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000275 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 uid = vap->va_uid;
277 qflags |= XFS_QMOPT_UQUOTA;
278 } else {
279 uid = ip->i_d.di_uid;
280 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000281 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 gid = vap->va_gid;
283 qflags |= XFS_QMOPT_GQUOTA;
284 } else {
285 gid = ip->i_d.di_gid;
286 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000287 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
288 projid = vap->va_projid;
289 qflags |= XFS_QMOPT_PQUOTA;
290 } else {
291 projid = ip->i_d.di_projid;
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /*
294 * We take a reference when we initialize udqp and gdqp,
295 * so it is important that we never blindly double trip on
296 * the same variable. See xfs_create() for an example.
297 */
298 ASSERT(udqp == NULL);
299 ASSERT(gdqp == NULL);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000300 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
301 &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100303 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
306 /*
307 * For the other attributes, we acquire the inode lock and
308 * first do an error checking pass.
309 */
310 tp = NULL;
311 lock_flags = XFS_ILOCK_EXCL;
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700312 if (flags & ATTR_NOLOCK)
313 need_iolock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!(mask & XFS_AT_SIZE)) {
315 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
316 (mp->m_flags & XFS_MOUNT_WSYNC)) {
317 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
318 commit_flags = 0;
319 if ((code = xfs_trans_reserve(tp, 0,
320 XFS_ICHANGE_LOG_RES(mp), 0,
321 0, 0))) {
322 lock_flags = 0;
323 goto error_return;
324 }
325 }
326 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000327 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 !(flags & ATTR_DMI)) {
329 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
330 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
331 vap->va_size, 0, dmflags, NULL);
332 if (code) {
333 lock_flags = 0;
334 goto error_return;
335 }
336 }
337 if (need_iolock)
338 lock_flags |= XFS_IOLOCK_EXCL;
339 }
340
341 xfs_ilock(ip, lock_flags);
342
343 /* boolean: are we the file owner? */
344 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
345
346 /*
347 * Change various properties of a file.
348 * Only the owner or users with CAP_FOWNER
349 * capability may do these things.
350 */
351 if (mask &
352 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
353 XFS_AT_GID|XFS_AT_PROJID)) {
354 /*
355 * CAP_FOWNER overrides the following restrictions:
356 *
357 * The user ID of the calling process must be equal
358 * to the file owner ID, except in cases where the
359 * CAP_FSETID capability is applicable.
360 */
361 if (!file_owner && !capable(CAP_FOWNER)) {
362 code = XFS_ERROR(EPERM);
363 goto error_return;
364 }
365
366 /*
367 * CAP_FSETID overrides the following restrictions:
368 *
369 * The effective user ID of the calling process shall match
370 * the file owner when setting the set-user-ID and
371 * set-group-ID bits on that file.
372 *
373 * The effective group ID or one of the supplementary group
374 * IDs of the calling process shall match the group owner of
375 * the file when setting the set-group-ID bit on that file
376 */
377 if (mask & XFS_AT_MODE) {
378 mode_t m = 0;
379
380 if ((vap->va_mode & S_ISUID) && !file_owner)
381 m |= S_ISUID;
382 if ((vap->va_mode & S_ISGID) &&
383 !in_group_p((gid_t)ip->i_d.di_gid))
384 m |= S_ISGID;
385#if 0
386 /* Linux allows this, Irix doesn't. */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000387 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 m |= S_ISVTX;
389#endif
390 if (m && !capable(CAP_FSETID))
391 vap->va_mode &= ~m;
392 }
393 }
394
395 /*
396 * Change file ownership. Must be the owner or privileged.
397 * If the system was configured with the "restricted_chown"
398 * option, the owner is not permitted to give away the file,
399 * and can change the group id only to a group of which he
400 * or she is a member.
401 */
402 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
403 /*
404 * These IDs could have changed since we last looked at them.
405 * But, we're assured that if the ownership did change
406 * while we didn't have the inode locked, inode's dquot(s)
407 * would have changed also.
408 */
409 iuid = ip->i_d.di_uid;
410 iprojid = ip->i_d.di_projid;
411 igid = ip->i_d.di_gid;
412 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
413 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
414 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
415 iprojid;
416
417 /*
418 * CAP_CHOWN overrides the following restrictions:
419 *
420 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
421 * shall override the restriction that a process cannot
422 * change the user ID of a file it owns and the restriction
423 * that the group ID supplied to the chown() function
424 * shall be equal to either the group ID or one of the
425 * supplementary group IDs of the calling process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
427 if (restricted_chown &&
428 (iuid != uid || (igid != gid &&
429 !in_group_p((gid_t)gid))) &&
430 !capable(CAP_CHOWN)) {
431 code = XFS_ERROR(EPERM);
432 goto error_return;
433 }
434 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000435 * Do a quota reservation only if uid/projid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * going to change.
437 */
438 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000439 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
441 ASSERT(tp);
442 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
443 capable(CAP_FOWNER) ?
444 XFS_QMOPT_FORCE_RES : 0);
445 if (code) /* out of quota */
446 goto error_return;
447 }
448 }
449
450 /*
451 * Truncate file. Must have write permission and not be a directory.
452 */
453 if (mask & XFS_AT_SIZE) {
454 /* Short circuit the truncate case for zero length files */
455 if ((vap->va_size == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000456 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 xfs_iunlock(ip, XFS_ILOCK_EXCL);
458 lock_flags &= ~XFS_ILOCK_EXCL;
459 if (mask & XFS_AT_CTIME)
460 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
461 code = 0;
462 goto error_return;
463 }
464
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000465 if (VN_ISDIR(vp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 code = XFS_ERROR(EISDIR);
467 goto error_return;
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000468 } else if (!VN_ISREG(vp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 code = XFS_ERROR(EINVAL);
470 goto error_return;
471 }
472 /*
473 * Make sure that the dquots are attached to the inode.
474 */
475 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
476 goto error_return;
477 }
478
479 /*
480 * Change file access or modified times.
481 */
482 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
483 if (!file_owner) {
484 if ((flags & ATTR_UTIME) &&
485 !capable(CAP_FOWNER)) {
486 code = XFS_ERROR(EPERM);
487 goto error_return;
488 }
489 }
490 }
491
492 /*
493 * Change extent size or realtime flag.
494 */
495 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
496 /*
497 * Can't change extent size if any extents are allocated.
498 */
Eric Sandeene94af022005-11-02 15:10:41 +1100499 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
501 vap->va_extsize) ) {
502 code = XFS_ERROR(EINVAL); /* EFBIG? */
503 goto error_return;
504 }
505
506 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * Can't change realtime flag if any extents are allocated.
508 */
Eric Sandeene94af022005-11-02 15:10:41 +1100509 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
510 (mask & XFS_AT_XFLAGS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
512 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
513 code = XFS_ERROR(EINVAL); /* EFBIG? */
514 goto error_return;
515 }
516 /*
517 * Extent size must be a multiple of the appropriate block
518 * size, if set at all.
519 */
520 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
521 xfs_extlen_t size;
522
523 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
524 ((mask & XFS_AT_XFLAGS) &&
525 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
526 size = mp->m_sb.sb_rextsize <<
527 mp->m_sb.sb_blocklog;
528 } else {
529 size = mp->m_sb.sb_blocksize;
530 }
531 if (vap->va_extsize % size) {
532 code = XFS_ERROR(EINVAL);
533 goto error_return;
534 }
535 }
536 /*
537 * If realtime flag is set then must have realtime data.
538 */
539 if ((mask & XFS_AT_XFLAGS) &&
540 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
541 if ((mp->m_sb.sb_rblocks == 0) ||
542 (mp->m_sb.sb_rextsize == 0) ||
543 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
544 code = XFS_ERROR(EINVAL);
545 goto error_return;
546 }
547 }
548
549 /*
550 * Can't modify an immutable/append-only file unless
551 * we have appropriate permission.
552 */
553 if ((mask & XFS_AT_XFLAGS) &&
554 (ip->i_d.di_flags &
555 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
556 (vap->va_xflags &
557 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
558 !capable(CAP_LINUX_IMMUTABLE)) {
559 code = XFS_ERROR(EPERM);
560 goto error_return;
561 }
562 }
563
564 /*
565 * Now we can make the changes. Before we join the inode
566 * to the transaction, if XFS_AT_SIZE is set then take care of
567 * the part of the truncation that must be done without the
568 * inode lock. This needs to be done before joining the inode
569 * to the transaction, because the inode cannot be unlocked
570 * once it is a part of the transaction.
571 */
572 if (mask & XFS_AT_SIZE) {
573 code = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000574 if ((vap->va_size > ip->i_size) &&
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100575 (flags & ATTR_NOSIZETOK) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 code = xfs_igrow_start(ip, vap->va_size, credp);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000579
580 /*
581 * We are going to log the inode size change in this
582 * transaction so any previous writes that are beyond the on
583 * disk EOF and the new EOF that have not been written out need
584 * to be written here. If we do not write the data out, we
585 * expose ourselves to the null files problem.
586 *
587 * Only flush from the on disk size to the smaller of the in
588 * memory file size or the new size as that's the range we
589 * really care about here and prevents waiting for other data
590 * not within the range we care about here.
591 */
592 if (!code &&
593 (ip->i_size != ip->i_d.di_size) &&
594 (vap->va_size > ip->i_d.di_size)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000595 code = xfs_flush_pages(ip,
David Chinnerc32676e2007-07-19 16:28:58 +1000596 ip->i_d.di_size, vap->va_size,
597 XFS_B_ASYNC, FI_NONE);
598 }
599
600 /* wait for all I/O to complete */
601 vn_iowait(vp);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!code)
604 code = xfs_itruncate_data(ip, vap->va_size);
605 if (code) {
606 ASSERT(tp == NULL);
607 lock_flags &= ~XFS_ILOCK_EXCL;
608 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
609 goto error_return;
610 }
611 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
612 if ((code = xfs_trans_reserve(tp, 0,
613 XFS_ITRUNCATE_LOG_RES(mp), 0,
614 XFS_TRANS_PERM_LOG_RES,
615 XFS_ITRUNCATE_LOG_COUNT))) {
616 xfs_trans_cancel(tp, 0);
617 if (need_iolock)
618 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
619 return code;
620 }
621 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
622 xfs_ilock(ip, XFS_ILOCK_EXCL);
623 }
624
625 if (tp) {
626 xfs_trans_ijoin(tp, ip, lock_flags);
627 xfs_trans_ihold(tp, ip);
628 }
629
630 /* determine whether mandatory locking mode changes */
631 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
632
633 /*
634 * Truncate file. Must have write permission and not be a directory.
635 */
636 if (mask & XFS_AT_SIZE) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000637 if (vap->va_size > ip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 xfs_igrow_finish(tp, ip, vap->va_size,
639 !(flags & ATTR_DMI));
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000640 } else if ((vap->va_size <= ip->i_size) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
642 /*
643 * signal a sync transaction unless
644 * we're truncating an already unlinked
645 * file on a wsync filesystem
646 */
647 code = xfs_itruncate_finish(&tp, ip,
648 (xfs_fsize_t)vap->va_size,
649 XFS_DATA_FORK,
650 ((ip->i_d.di_nlink != 0 ||
651 !(mp->m_flags & XFS_MOUNT_WSYNC))
652 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000653 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000655 /*
656 * Truncated "down", so we're removing references
657 * to old data here - if we now delay flushing for
658 * a long time, we expose ourselves unduly to the
659 * notorious NULL files problem. So, we mark this
660 * vnode and flush it when the file is closed, and
661 * do not wait the usual (long) time for writeout.
662 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000663 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665 /*
666 * Have to do this even if the file's size doesn't change.
667 */
668 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
669 }
670
671 /*
672 * Change file access modes.
673 */
674 if (mask & XFS_AT_MODE) {
675 ip->i_d.di_mode &= S_IFMT;
676 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
677
678 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
679 timeflags |= XFS_ICHGTIME_CHG;
680 }
681
682 /*
683 * Change file ownership. Must be the owner or privileged.
684 * If the system was configured with the "restricted_chown"
685 * option, the owner is not permitted to give away the file,
686 * and can change the group id only to a group of which he
687 * or she is a member.
688 */
689 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
690 /*
691 * CAP_FSETID overrides the following restrictions:
692 *
693 * The set-user-ID and set-group-ID bits of a file will be
694 * cleared upon successful return from chown()
695 */
696 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
697 !capable(CAP_FSETID)) {
698 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
699 }
700
701 /*
702 * Change the ownerships and register quota modifications
703 * in the transaction.
704 */
705 if (iuid != uid) {
706 if (XFS_IS_UQUOTA_ON(mp)) {
707 ASSERT(mask & XFS_AT_UID);
708 ASSERT(udqp);
709 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
710 &ip->i_udquot, udqp);
711 }
712 ip->i_d.di_uid = uid;
713 }
714 if (igid != gid) {
715 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000716 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ASSERT(mask & XFS_AT_GID);
718 ASSERT(gdqp);
719 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
720 &ip->i_gdquot, gdqp);
721 }
722 ip->i_d.di_gid = gid;
723 }
724 if (iprojid != projid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000725 if (XFS_IS_PQUOTA_ON(mp)) {
726 ASSERT(!XFS_IS_GQUOTA_ON(mp));
727 ASSERT(mask & XFS_AT_PROJID);
728 ASSERT(gdqp);
729 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
730 &ip->i_gdquot, gdqp);
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 ip->i_d.di_projid = projid;
733 /*
734 * We may have to rev the inode as well as
735 * the superblock version number since projids didn't
736 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
737 */
738 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
739 xfs_bump_ino_vers2(tp, ip);
740 }
741
742 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
743 timeflags |= XFS_ICHGTIME_CHG;
744 }
745
746
747 /*
748 * Change file access or modified times.
749 */
750 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
751 if (mask & XFS_AT_ATIME) {
752 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
753 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
754 ip->i_update_core = 1;
755 timeflags &= ~XFS_ICHGTIME_ACC;
756 }
757 if (mask & XFS_AT_MTIME) {
758 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
759 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
760 timeflags &= ~XFS_ICHGTIME_MOD;
761 timeflags |= XFS_ICHGTIME_CHG;
762 }
763 if (tp && (flags & ATTR_UTIME))
764 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
765 }
766
767 /*
768 * Change XFS-added attributes.
769 */
770 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
771 if (mask & XFS_AT_EXTSIZE) {
772 /*
773 * Converting bytes to fs blocks.
774 */
775 ip->i_d.di_extsize = vap->va_extsize >>
776 mp->m_sb.sb_blocklog;
777 }
778 if (mask & XFS_AT_XFLAGS) {
779 uint di_flags;
780
781 /* can't set PREALLOC this way, just preserve it */
782 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
783 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
784 di_flags |= XFS_DIFLAG_IMMUTABLE;
785 if (vap->va_xflags & XFS_XFLAG_APPEND)
786 di_flags |= XFS_DIFLAG_APPEND;
787 if (vap->va_xflags & XFS_XFLAG_SYNC)
788 di_flags |= XFS_DIFLAG_SYNC;
789 if (vap->va_xflags & XFS_XFLAG_NOATIME)
790 di_flags |= XFS_DIFLAG_NOATIME;
791 if (vap->va_xflags & XFS_XFLAG_NODUMP)
792 di_flags |= XFS_DIFLAG_NODUMP;
Nathan Scott365ca832005-06-21 15:39:12 +1000793 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
794 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000795 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
796 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000797 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
798 di_flags |= XFS_DIFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
800 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
801 di_flags |= XFS_DIFLAG_RTINHERIT;
802 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
803 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100804 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
805 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
806 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
808 di_flags |= XFS_DIFLAG_REALTIME;
809 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
810 } else {
811 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
812 }
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)) {
884 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
885 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
905
906/*
907 * xfs_access
908 * Null conversion from vnode mode bits to inode mode bits, as in efs.
909 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000910int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911xfs_access(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000912 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 int mode,
914 cred_t *credp)
915{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int error;
917
Christoph Hellwig993386c2007-08-28 16:12:30 +1000918 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 xfs_ilock(ip, XFS_ILOCK_SHARED);
921 error = xfs_iaccess(ip, mode, credp);
922 xfs_iunlock(ip, XFS_ILOCK_SHARED);
923 return error;
924}
925
926
927/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000928 * The maximum pathlen is 1024 bytes. Since the minimum file system
929 * blocksize is 512 bytes, we can get a max of 2 extents back from
930 * bmapi.
931 */
932#define SYMLINK_MAPS 2
933
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000934STATIC int
935xfs_readlink_bmap(
936 xfs_inode_t *ip,
937 char *link)
938{
939 xfs_mount_t *mp = ip->i_mount;
940 int pathlen = ip->i_d.di_size;
941 int nmaps = SYMLINK_MAPS;
942 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
943 xfs_daddr_t d;
944 int byte_cnt;
945 int n;
946 xfs_buf_t *bp;
947 int error = 0;
948
949 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
950 mval, &nmaps, NULL, NULL);
951 if (error)
952 goto out;
953
954 for (n = 0; n < nmaps; n++) {
955 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
956 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
957
958 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
959 error = XFS_BUF_GETERROR(bp);
960 if (error) {
961 xfs_ioerror_alert("xfs_readlink",
962 ip->i_mount, bp, XFS_BUF_ADDR(bp));
963 xfs_buf_relse(bp);
964 goto out;
965 }
966 if (pathlen < byte_cnt)
967 byte_cnt = pathlen;
968 pathlen -= byte_cnt;
969
970 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
971 xfs_buf_relse(bp);
972 }
973
974 link[ip->i_d.di_size] = '\0';
975 error = 0;
976
977 out:
978 return error;
979}
980
Christoph Hellwig993386c2007-08-28 16:12:30 +1000981int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000983 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000984 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000986 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000990 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (XFS_FORCED_SHUTDOWN(mp))
993 return XFS_ERROR(EIO);
994
995 xfs_ilock(ip, XFS_ILOCK_SHARED);
996
997 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000998 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Christoph Hellwig804c83c2007-08-28 13:59:03 +10001000 pathlen = ip->i_d.di_size;
1001 if (!pathlen)
1002 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +10001005 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
1006 link[pathlen] = '\0';
1007 } else {
1008 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
Christoph Hellwig804c83c2007-08-28 13:59:03 +10001011 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return error;
1014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/*
1017 * xfs_fsync
1018 *
1019 * This is called to sync the inode and its data out to disk.
1020 * We need to hold the I/O lock while flushing the data, and
1021 * the inode lock while flushing the inode. The inode lock CANNOT
1022 * be held while flushing the data, so acquire after we're done
1023 * with that.
1024 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001025int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026xfs_fsync(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001027 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 int flag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 xfs_off_t start,
1030 xfs_off_t stop)
1031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 xfs_trans_t *tp;
1033 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001034 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Christoph Hellwig993386c2007-08-28 16:12:30 +10001036 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 ASSERT(start >= 0 && stop >= -1);
1039
1040 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1041 return XFS_ERROR(EIO);
1042
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +10001043 if (flag & FSYNC_DATA)
1044 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /*
1047 * We always need to make sure that the required inode state
1048 * is safe on disk. The vnode might be clean but because
1049 * of committed transactions that haven't hit the disk yet.
1050 * Likewise, there could be unflushed non-transactional
1051 * changes to the inode core that have to go to disk.
1052 *
1053 * The following code depends on one assumption: that
1054 * any transaction that changes an inode logs the core
1055 * because it has to change some field in the inode core
1056 * (typically nextents or nblocks). That assumption
1057 * implies that any transactions against an inode will
1058 * catch any non-transactional updates. If inode-altering
1059 * transactions exist that violate this assumption, the
1060 * code breaks. Right now, it figures that if the involved
1061 * update_* field is clear and the inode is unpinned, the
1062 * inode is clean. Either it's been flushed or it's been
1063 * committed and the commit has hit the disk unpinning the inode.
1064 * (Note that xfs_inode_item_format() called at commit clears
1065 * the update_* fields.)
1066 */
1067 xfs_ilock(ip, XFS_ILOCK_SHARED);
1068
1069 /* If we are flushing data then we care about update_size
1070 * being set, otherwise we care about update_core
1071 */
1072 if ((flag & FSYNC_DATA) ?
1073 (ip->i_update_size == 0) :
1074 (ip->i_update_core == 0)) {
1075 /*
1076 * Timestamps/size haven't changed since last inode
1077 * flush or inode transaction commit. That means
1078 * either nothing got written or a transaction
1079 * committed which caught the updates. If the
1080 * latter happened and the transaction hasn't
1081 * hit the disk yet, the inode will be still
1082 * be pinned. If it is, force the log.
1083 */
1084
1085 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1086
1087 if (xfs_ipincount(ip)) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001088 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 XFS_LOG_FORCE |
1090 ((flag & FSYNC_WAIT)
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001091 ? XFS_LOG_SYNC : 0),
1092 &log_flushed);
1093 } else {
1094 /*
1095 * If the inode is not pinned and nothing
1096 * has changed we don't need to flush the
1097 * cache.
1098 */
1099 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101 error = 0;
1102 } else {
1103 /*
1104 * Kick off a transaction to log the inode
1105 * core to get the updates. Make it
1106 * sync if FSYNC_WAIT is passed in (which
1107 * is done by everybody but specfs). The
1108 * sync transaction will also force the log.
1109 */
1110 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1111 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1112 if ((error = xfs_trans_reserve(tp, 0,
1113 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1114 0, 0, 0))) {
1115 xfs_trans_cancel(tp, 0);
1116 return error;
1117 }
1118 xfs_ilock(ip, XFS_ILOCK_EXCL);
1119
1120 /*
1121 * Note - it's possible that we might have pushed
1122 * ourselves out of the way during trans_reserve
1123 * which would flush the inode. But there's no
1124 * guarantee that the inode buffer has actually
1125 * gone out yet (it's delwri). Plus the buffer
1126 * could be pinned anyway if it's part of an
1127 * inode in another recent transaction. So we
1128 * play it safe and fire off the transaction anyway.
1129 */
1130 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1131 xfs_trans_ihold(tp, ip);
1132 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1133 if (flag & FSYNC_WAIT)
1134 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001135 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1138 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001139
1140 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1141 /*
1142 * If the log write didn't issue an ordered tag we need
1143 * to flush the disk cache for the data device now.
1144 */
1145 if (!log_flushed)
1146 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1147
1148 /*
1149 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +10001150 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001151 */
1152 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1153 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1154 }
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return error;
1157}
1158
1159/*
David Chinner92dfe8d2007-05-24 15:22:19 +10001160 * This is called by xfs_inactive to free any blocks beyond eof
1161 * when the link count isn't zero and by xfs_dm_punch_hole() when
1162 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001164int
1165xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +10001167 xfs_inode_t *ip,
1168 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 xfs_trans_t *tp;
1171 int error;
1172 xfs_fileoff_t end_fsb;
1173 xfs_fileoff_t last_fsb;
1174 xfs_filblks_t map_len;
1175 int nimaps;
1176 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +10001177 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /*
1180 * Figure out if there are any blocks beyond the end
1181 * of the file. If not, then there is nothing to do.
1182 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001183 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1185 map_len = last_fsb - end_fsb;
1186 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 nimaps = 1;
1190 xfs_ilock(ip, XFS_ILOCK_SHARED);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001191 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, end_fsb, map_len, 0,
1192 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1194
1195 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001196 (imap.br_startblock != HOLESTARTBLOCK ||
1197 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /*
1199 * Attach the dquots to the inode up front.
1200 */
1201 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001202 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /*
1205 * There are blocks after the end of file.
1206 * Free them up now by truncating the file to
1207 * its current size.
1208 */
1209 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1210
1211 /*
1212 * Do the xfs_itruncate_start() call before
1213 * reserving any log space because
1214 * itruncate_start will call into the buffer
1215 * cache and we can't
1216 * do that within a transaction.
1217 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001218 if (use_iolock)
1219 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001220 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001221 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001222 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001223 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001224 if (use_iolock)
1225 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001226 return error;
1227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 error = xfs_trans_reserve(tp, 0,
1230 XFS_ITRUNCATE_LOG_RES(mp),
1231 0, XFS_TRANS_PERM_LOG_RES,
1232 XFS_ITRUNCATE_LOG_COUNT);
1233 if (error) {
1234 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1235 xfs_trans_cancel(tp, 0);
1236 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001237 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
1240 xfs_ilock(ip, XFS_ILOCK_EXCL);
1241 xfs_trans_ijoin(tp, ip,
1242 XFS_IOLOCK_EXCL |
1243 XFS_ILOCK_EXCL);
1244 xfs_trans_ihold(tp, ip);
1245
1246 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001247 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 XFS_DATA_FORK,
1249 0);
1250 /*
1251 * If we get an error at this point we
1252 * simply don't bother truncating the file.
1253 */
1254 if (error) {
1255 xfs_trans_cancel(tp,
1256 (XFS_TRANS_RELEASE_LOG_RES |
1257 XFS_TRANS_ABORT));
1258 } else {
1259 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001260 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
David Chinner92dfe8d2007-05-24 15:22:19 +10001262 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1263 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001265 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268/*
1269 * Free a symlink that has blocks associated with it.
1270 */
1271STATIC int
1272xfs_inactive_symlink_rmt(
1273 xfs_inode_t *ip,
1274 xfs_trans_t **tpp)
1275{
1276 xfs_buf_t *bp;
1277 int committed;
1278 int done;
1279 int error;
1280 xfs_fsblock_t first_block;
1281 xfs_bmap_free_t free_list;
1282 int i;
1283 xfs_mount_t *mp;
1284 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1285 int nmaps;
1286 xfs_trans_t *ntp;
1287 int size;
1288 xfs_trans_t *tp;
1289
1290 tp = *tpp;
1291 mp = ip->i_mount;
1292 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1293 /*
1294 * We're freeing a symlink that has some
1295 * blocks allocated to it. Free the
1296 * blocks here. We know that we've got
1297 * either 1 or 2 extents and that we can
1298 * free them all in one bunmapi call.
1299 */
1300 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1301 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1302 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1303 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1304 xfs_trans_cancel(tp, 0);
1305 *tpp = NULL;
1306 return error;
1307 }
1308 /*
1309 * Lock the inode, fix the size, and join it to the transaction.
1310 * Hold it so in the normal path, we still have it locked for
1311 * the second transaction. In the error paths we need it
1312 * held so the cancel won't rele it, see below.
1313 */
1314 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1315 size = (int)ip->i_d.di_size;
1316 ip->i_d.di_size = 0;
1317 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1318 xfs_trans_ihold(tp, ip);
1319 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1320 /*
1321 * Find the block(s) so we can inval and unmap them.
1322 */
1323 done = 0;
1324 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -08001325 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1327 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001328 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto error0;
1330 /*
1331 * Invalidate the block(s).
1332 */
1333 for (i = 0; i < nmaps; i++) {
1334 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1335 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1336 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1337 xfs_trans_binval(tp, bp);
1338 }
1339 /*
1340 * Unmap the dead block(s) to the free_list.
1341 */
1342 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001343 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 goto error1;
1345 ASSERT(done);
1346 /*
1347 * Commit the first transaction. This logs the EFI and the inode.
1348 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001349 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 goto error1;
1351 /*
1352 * The transaction must have been committed, since there were
1353 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1354 * The new tp has the extent freeing and EFDs.
1355 */
1356 ASSERT(committed);
1357 /*
1358 * The first xact was committed, so add the inode to the new one.
1359 * Mark it dirty so it will be logged and moved forward in the log as
1360 * part of every commit.
1361 */
1362 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1363 xfs_trans_ihold(tp, ip);
1364 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1365 /*
1366 * Get a new, empty transaction to return to our caller.
1367 */
1368 ntp = xfs_trans_dup(tp);
1369 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001370 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * If we get an error on the commit here or on the reserve below,
1372 * we need to unlock the inode since the new transaction doesn't
1373 * have the inode attached.
1374 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001375 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 tp = ntp;
1377 if (error) {
1378 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1379 goto error0;
1380 }
1381 /*
1382 * Remove the memory for extent descriptions (just bookkeeping).
1383 */
1384 if (ip->i_df.if_bytes)
1385 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1386 ASSERT(ip->i_df.if_bytes == 0);
1387 /*
1388 * Put an itruncate log reservation in the new transaction
1389 * for our caller.
1390 */
1391 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1392 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1393 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1394 goto error0;
1395 }
1396 /*
1397 * Return with the inode locked but not joined to the transaction.
1398 */
1399 *tpp = tp;
1400 return 0;
1401
1402 error1:
1403 xfs_bmap_cancel(&free_list);
1404 error0:
1405 /*
1406 * Have to come here with the inode locked and either
1407 * (held and in the transaction) or (not in the transaction).
1408 * If the inode isn't held then cancel would iput it, but
1409 * that's wrong since this is inactive and the vnode ref
1410 * count is 0 already.
1411 * Cancel won't do anything to the inode if held, but it still
1412 * needs to be locked until the cancel is done, if it was
1413 * joined to the transaction.
1414 */
1415 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1416 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1417 *tpp = NULL;
1418 return error;
1419
1420}
1421
1422STATIC int
1423xfs_inactive_symlink_local(
1424 xfs_inode_t *ip,
1425 xfs_trans_t **tpp)
1426{
1427 int error;
1428
1429 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1430 /*
1431 * We're freeing a symlink which fit into
1432 * the inode. Just free the memory used
1433 * to hold the old symlink.
1434 */
1435 error = xfs_trans_reserve(*tpp, 0,
1436 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1437 0, XFS_TRANS_PERM_LOG_RES,
1438 XFS_ITRUNCATE_LOG_COUNT);
1439
1440 if (error) {
1441 xfs_trans_cancel(*tpp, 0);
1442 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001443 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1446
1447 /*
1448 * Zero length symlinks _can_ exist.
1449 */
1450 if (ip->i_df.if_bytes > 0) {
1451 xfs_idata_realloc(ip,
1452 -(ip->i_df.if_bytes),
1453 XFS_DATA_FORK);
1454 ASSERT(ip->i_df.if_bytes == 0);
1455 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459STATIC int
1460xfs_inactive_attrs(
1461 xfs_inode_t *ip,
1462 xfs_trans_t **tpp)
1463{
1464 xfs_trans_t *tp;
1465 int error;
1466 xfs_mount_t *mp;
1467
1468 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1469 tp = *tpp;
1470 mp = ip->i_mount;
1471 ASSERT(ip->i_d.di_forkoff != 0);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001472 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1474
1475 error = xfs_attr_inactive(ip);
1476 if (error) {
1477 *tpp = NULL;
1478 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001479 return error; /* goto out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481
1482 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1483 error = xfs_trans_reserve(tp, 0,
1484 XFS_IFREE_LOG_RES(mp),
1485 0, XFS_TRANS_PERM_LOG_RES,
1486 XFS_INACTIVE_LOG_COUNT);
1487 if (error) {
1488 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1489 xfs_trans_cancel(tp, 0);
1490 *tpp = NULL;
1491 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001492 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
1495 xfs_ilock(ip, XFS_ILOCK_EXCL);
1496 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1497 xfs_trans_ihold(tp, ip);
1498 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1499
1500 ASSERT(ip->i_d.di_anextents == 0);
1501
1502 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Christoph Hellwig993386c2007-08-28 16:12:30 +10001506int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001508 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001510 bhv_vnode_t *vp = XFS_ITOV(ip);
1511 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 int error;
1513
Nathan Scott7d4fb402006-06-09 15:27:16 +10001514 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +10001518 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
1520
David Chinner2a82b8b2007-07-11 11:09:12 +10001521 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001522 int truncated;
1523
David Chinner2a82b8b2007-07-11 11:09:12 +10001524 /*
1525 * If we are using filestreams, and we have an unlinked
1526 * file that we are processing the last close on, then nothing
1527 * will be able to reopen and write to this file. Purge this
1528 * inode from the filestreams cache so that it doesn't delay
1529 * teardown of the inode.
1530 */
1531 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1532 xfs_filestream_deassociate(ip);
1533
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001534 /*
1535 * If we previously truncated this file and removed old data
1536 * in the process, we want to initiate "early" writeout on
1537 * the last close. This is an attempt to combat the notorious
1538 * NULL files problem which is particularly noticable from a
1539 * truncate down, buffered (re-)write (delalloc), followed by
1540 * a crash. What we are effectively doing here is
1541 * significantly reducing the time window where we'd otherwise
1542 * be exposed to that problem.
1543 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001544 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001545 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001546 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001547 }
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549#ifdef HAVE_REFCACHE
1550 /* If we are in the NFS reference cache then don't do this now */
1551 if (ip->i_refcache)
1552 return 0;
1553#endif
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (ip->i_d.di_nlink != 0) {
1556 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001557 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001558 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001560 (!(ip->i_d.di_flags &
1561 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001562 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1563 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001564 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* Update linux inode block count after free above */
Nathan Scottec86dc02006-03-17 17:25:36 +11001566 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 ip->i_d.di_nblocks + ip->i_delayed_blks);
1568 }
1569 }
1570
1571 return 0;
1572}
1573
1574/*
1575 * xfs_inactive
1576 *
1577 * This is called when the vnode reference count for the vnode
1578 * goes to zero. If the file has been unlinked, then it must
1579 * now be truncated. Also, we clear all of the read-ahead state
1580 * kept for the inode here since the file is now closed.
1581 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001582int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001584 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001586 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +10001587 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 xfs_fsblock_t first_block;
1589 int committed;
1590 xfs_trans_t *tp;
1591 xfs_mount_t *mp;
1592 int error;
1593 int truncate;
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /*
1598 * If the inode is already free, then there can be nothing
1599 * to clean up here.
1600 */
1601 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1602 ASSERT(ip->i_df.if_real_bytes == 0);
1603 ASSERT(ip->i_df.if_broot_bytes == 0);
1604 return VN_INACTIVE_CACHE;
1605 }
1606
1607 /*
1608 * Only do a truncate if it's a regular file with
1609 * some actual space in it. It's OK to look at the
1610 * inode's fields without the lock because we're the
1611 * only one with a reference to the inode.
1612 */
1613 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001614 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1615 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1617
1618 mp = ip->i_mount;
1619
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001620 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1622 }
1623
1624 error = 0;
1625
1626 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwig2f6f7b32007-08-29 11:44:18 +10001627 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto out;
1629
1630 if (ip->i_d.di_nlink != 0) {
1631 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001632 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001633 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001634 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1635 (!(ip->i_d.di_flags &
1636 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1637 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001638 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1639 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001640 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 /* Update linux inode block count after free above */
Nathan Scottec86dc02006-03-17 17:25:36 +11001642 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 ip->i_d.di_nblocks + ip->i_delayed_blks);
1644 }
1645 goto out;
1646 }
1647
1648 ASSERT(ip->i_d.di_nlink == 0);
1649
1650 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001651 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1654 if (truncate) {
1655 /*
1656 * Do the xfs_itruncate_start() call before
1657 * reserving any log space because itruncate_start
1658 * will call into the buffer cache and we can't
1659 * do that within a transaction.
1660 */
1661 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1662
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001663 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1664 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001665 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001666 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1667 return VN_INACTIVE_CACHE;
1668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 error = xfs_trans_reserve(tp, 0,
1671 XFS_ITRUNCATE_LOG_RES(mp),
1672 0, XFS_TRANS_PERM_LOG_RES,
1673 XFS_ITRUNCATE_LOG_COUNT);
1674 if (error) {
1675 /* Don't call itruncate_cleanup */
1676 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1677 xfs_trans_cancel(tp, 0);
1678 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001679 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681
1682 xfs_ilock(ip, XFS_ILOCK_EXCL);
1683 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1684 xfs_trans_ihold(tp, ip);
1685
1686 /*
1687 * normally, we have to run xfs_itruncate_finish sync.
1688 * But if filesystem is wsync and we're in the inactive
1689 * path, then we know that nlink == 0, and that the
1690 * xaction that made nlink == 0 is permanently committed
1691 * since xfs_remove runs as a synchronous transaction.
1692 */
1693 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1694 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1695
1696 if (error) {
1697 xfs_trans_cancel(tp,
1698 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1699 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001700 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1703
1704 /*
1705 * If we get an error while cleaning up a
1706 * symlink we bail out.
1707 */
1708 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1709 xfs_inactive_symlink_rmt(ip, &tp) :
1710 xfs_inactive_symlink_local(ip, &tp);
1711
1712 if (error) {
1713 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001714 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
1717 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1718 xfs_trans_ihold(tp, ip);
1719 } else {
1720 error = xfs_trans_reserve(tp, 0,
1721 XFS_IFREE_LOG_RES(mp),
1722 0, XFS_TRANS_PERM_LOG_RES,
1723 XFS_INACTIVE_LOG_COUNT);
1724 if (error) {
1725 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1726 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001727 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
1730 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1731 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1732 xfs_trans_ihold(tp, ip);
1733 }
1734
1735 /*
1736 * If there are attributes associated with the file
1737 * then blow them away now. The code calls a routine
1738 * that recursively deconstructs the attribute fork.
1739 * We need to just commit the current transaction
1740 * because we can't use it for xfs_attr_inactive().
1741 */
1742 if (ip->i_d.di_anextents > 0) {
1743 error = xfs_inactive_attrs(ip, &tp);
1744 /*
1745 * If we got an error, the transaction is already
1746 * cancelled, and the inode is unlocked. Just get out.
1747 */
1748 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001749 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 } else if (ip->i_afp) {
1751 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1752 }
1753
1754 /*
1755 * Free the inode.
1756 */
1757 XFS_BMAP_INIT(&free_list, &first_block);
1758 error = xfs_ifree(tp, ip, &free_list);
1759 if (error) {
1760 /*
1761 * If we fail to free the inode, shut down. The cancel
1762 * might do that, we need to make sure. Otherwise the
1763 * inode might be lost for a long time or forever.
1764 */
1765 if (!XFS_FORCED_SHUTDOWN(mp)) {
1766 cmn_err(CE_NOTE,
1767 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1768 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001769 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1772 } else {
1773 /*
1774 * Credit the quota account(s). The inode is gone.
1775 */
1776 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1777
1778 /*
1779 * Just ignore errors at this point. There is
1780 * nothing we can do except to try to keep going.
1781 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001782 (void) xfs_bmap_finish(&tp, &free_list, &committed);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001783 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 /*
1786 * Release the dquots held by inode, if any.
1787 */
1788 XFS_QM_DQDETACH(mp, ip);
1789
1790 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1791
1792 out:
1793 return VN_INACTIVE_CACHE;
1794}
1795
1796
Christoph Hellwig993386c2007-08-28 16:12:30 +10001797int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001799 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10001800 bhv_vname_t *dentry,
Christoph Hellwig993386c2007-08-28 16:12:30 +10001801 bhv_vnode_t **vpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001803 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 xfs_ino_t e_inum;
1805 int error;
1806 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Christoph Hellwig993386c2007-08-28 16:12:30 +10001808 vn_trace_entry(XFS_ITOV(dp), __FUNCTION__, (inst_t *)__return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1811 return XFS_ERROR(EIO);
1812
1813 lock_mode = xfs_ilock_map_shared(dp);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001814 error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (!error) {
1816 *vpp = XFS_ITOV(ip);
1817 ITRACE(ip);
1818 }
1819 xfs_iunlock_map_shared(dp, lock_mode);
1820 return error;
1821}
1822
Christoph Hellwig993386c2007-08-28 16:12:30 +10001823int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001825 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10001826 bhv_vname_t *dentry,
1827 bhv_vattr_t *vap,
Nathan Scott67fcaa72006-06-09 17:00:52 +10001828 bhv_vnode_t **vpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 cred_t *credp)
1830{
1831 char *name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001832 xfs_mount_t *mp = dp->i_mount;
1833 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
1834 xfs_inode_t *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +10001835 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 xfs_dev_t rdev;
1838 int error;
1839 xfs_bmap_free_t free_list;
1840 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001841 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 int dm_event_sent = 0;
1843 uint cancel_flags;
1844 int committed;
1845 xfs_prid_t prid;
1846 struct xfs_dquot *udqp, *gdqp;
1847 uint resblks;
1848 int dm_di_mode;
1849 int namelen;
1850
1851 ASSERT(!*vpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1853
Christoph Hellwig0432dab2005-09-02 16:46:51 +10001854 dm_di_mode = vap->va_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 namelen = VNAMELEN(dentry);
1856
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001857 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1859 dir_vp, DM_RIGHT_NULL, NULL,
1860 DM_RIGHT_NULL, name, NULL,
1861 dm_di_mode, 0, 0);
1862
1863 if (error)
1864 return error;
1865 dm_event_sent = 1;
1866 }
1867
1868 if (XFS_FORCED_SHUTDOWN(mp))
1869 return XFS_ERROR(EIO);
1870
1871 /* Return through std_return after this point. */
1872
1873 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001874 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1875 prid = dp->i_d.di_projid;
1876 else if (vap->va_mask & XFS_AT_PROJID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 prid = (xfs_prid_t)vap->va_projid;
1878 else
1879 prid = (xfs_prid_t)dfltprid;
1880
1881 /*
1882 * Make sure that we have allocated dquot(s) on disk.
1883 */
1884 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001885 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1887 if (error)
1888 goto std_return;
1889
1890 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1893 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1894 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1895 /*
1896 * Initially assume that the file does not exist and
1897 * reserve the resources for that case. If that is not
1898 * the case we'll drop the one we have and get a more
1899 * appropriate transaction later.
1900 */
1901 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1902 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1903 if (error == ENOSPC) {
1904 resblks = 0;
1905 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1906 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1907 }
1908 if (error) {
1909 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 goto error_return;
1911 }
1912
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001913 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001914 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 XFS_BMAP_INIT(&free_list, &first_block);
1917
1918 ASSERT(ip == NULL);
1919
1920 /*
1921 * Reserve disk quota and the inode.
1922 */
1923 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1924 if (error)
1925 goto error_return;
1926
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001927 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 goto error_return;
1929 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
Christoph Hellwig0432dab2005-09-02 16:46:51 +10001930 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 rdev, credp, prid, resblks > 0,
1932 &ip, &committed);
1933 if (error) {
1934 if (error == ENOSPC)
1935 goto error_return;
1936 goto abort_return;
1937 }
1938 ITRACE(ip);
1939
1940 /*
1941 * At this point, we've gotten a newly allocated inode.
1942 * It is locked (and joined to the transaction).
1943 */
1944
1945 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1946
1947 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001948 * Now we join the directory inode to the transaction. We do not do it
1949 * earlier because xfs_dir_ialloc might commit the previous transaction
1950 * (and release all the locks). An error from here on will result in
1951 * the transaction cancel unlocking dp so don't do it explicitly in the
1952 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 VN_HOLD(dir_vp);
1955 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001956 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001958 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
1959 &first_block, &free_list, resblks ?
1960 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (error) {
1962 ASSERT(error != ENOSPC);
1963 goto abort_return;
1964 }
1965 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1966 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1967
1968 /*
1969 * If this is a synchronous mount, make sure that the
1970 * create transaction goes to disk before returning to
1971 * the user.
1972 */
1973 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1974 xfs_trans_set_sync(tp);
1975 }
1976
1977 dp->i_gen++;
1978
1979 /*
1980 * Attach the dquot(s) to the inodes and modify them incore.
1981 * These ids of the inode couldn't have changed since the new
1982 * inode has been locked ever since it was created.
1983 */
1984 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1985
1986 /*
1987 * xfs_trans_commit normally decrements the vnode ref count
1988 * when it unlocks the inode. Since we want to return the
1989 * vnode to the caller, we bump the vnode ref count now.
1990 */
1991 IHOLD(ip);
1992 vp = XFS_ITOV(ip);
1993
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001994 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (error) {
1996 xfs_bmap_cancel(&free_list);
1997 goto abort_rele;
1998 }
1999
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002000 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (error) {
2002 IRELE(ip);
2003 tp = NULL;
2004 goto error_return;
2005 }
2006
2007 XFS_QM_DQRELE(mp, udqp);
2008 XFS_QM_DQRELE(mp, gdqp);
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 *vpp = vp;
2011
2012 /* Fallthrough to std_return with error = 0 */
2013
2014std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002015 if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002016 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2018 dir_vp, DM_RIGHT_NULL,
2019 *vpp ? vp:NULL,
2020 DM_RIGHT_NULL, name, NULL,
2021 dm_di_mode, error, 0);
2022 }
2023 return error;
2024
2025 abort_return:
2026 cancel_flags |= XFS_TRANS_ABORT;
2027 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Jesper Juhl014c2542006-01-15 02:37:08 +01002029 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (tp != NULL)
2031 xfs_trans_cancel(tp, cancel_flags);
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 XFS_QM_DQRELE(mp, udqp);
2034 XFS_QM_DQRELE(mp, gdqp);
2035
Christoph Hellwig993386c2007-08-28 16:12:30 +10002036 if (unlock_dp_on_error)
2037 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 goto std_return;
2040
2041 abort_rele:
2042 /*
2043 * Wait until after the current transaction is aborted to
2044 * release the inode. This prevents recursive transactions
2045 * and deadlocks from xfs_inactive.
2046 */
2047 cancel_flags |= XFS_TRANS_ABORT;
2048 xfs_trans_cancel(tp, cancel_flags);
2049 IRELE(ip);
2050
2051 XFS_QM_DQRELE(mp, udqp);
2052 XFS_QM_DQRELE(mp, gdqp);
2053
2054 goto std_return;
2055}
2056
2057#ifdef DEBUG
2058/*
2059 * Some counters to see if (and how often) we are hitting some deadlock
2060 * prevention code paths.
2061 */
2062
2063int xfs_rm_locks;
2064int xfs_rm_lock_delays;
2065int xfs_rm_attempts;
2066#endif
2067
2068/*
2069 * The following routine will lock the inodes associated with the
2070 * directory and the named entry in the directory. The locks are
2071 * acquired in increasing inode number.
2072 *
2073 * If the entry is "..", then only the directory is locked. The
2074 * vnode ref count will still include that from the .. entry in
2075 * this case.
2076 *
2077 * There is a deadlock we need to worry about. If the locked directory is
2078 * in the AIL, it might be blocking up the log. The next inode we lock
2079 * could be already locked by another thread waiting for log space (e.g
2080 * a permanent log reservation with a long running transaction (see
2081 * xfs_itruncate_finish)). To solve this, we must check if the directory
2082 * is in the ail and use lock_nowait. If we can't lock, we need to
2083 * drop the inode lock on the directory and try again. xfs_iunlock will
2084 * potentially push the tail if we were holding up the log.
2085 */
2086STATIC int
2087xfs_lock_dir_and_entry(
2088 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 xfs_inode_t *ip) /* inode of entry 'name' */
2090{
2091 int attempts;
2092 xfs_ino_t e_inum;
2093 xfs_inode_t *ips[2];
2094 xfs_log_item_t *lp;
2095
2096#ifdef DEBUG
2097 xfs_rm_locks++;
2098#endif
2099 attempts = 0;
2100
2101again:
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002102 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 e_inum = ip->i_ino;
2105
2106 ITRACE(ip);
2107
2108 /*
2109 * We want to lock in increasing inum. Since we've already
2110 * acquired the lock on the directory, we may need to release
2111 * if if the inum of the entry turns out to be less.
2112 */
2113 if (e_inum > dp->i_ino) {
2114 /*
2115 * We are already in the right order, so just
2116 * lock on the inode of the entry.
2117 * We need to use nowait if dp is in the AIL.
2118 */
2119
2120 lp = (xfs_log_item_t *)dp->i_itemp;
2121 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2122 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2123 attempts++;
2124#ifdef DEBUG
2125 xfs_rm_attempts++;
2126#endif
2127
2128 /*
2129 * Unlock dp and try again.
2130 * xfs_iunlock will try to push the tail
2131 * if the inode is in the AIL.
2132 */
2133
2134 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2135
2136 if ((attempts % 5) == 0) {
2137 delay(1); /* Don't just spin the CPU */
2138#ifdef DEBUG
2139 xfs_rm_lock_delays++;
2140#endif
2141 }
2142 goto again;
2143 }
2144 } else {
2145 xfs_ilock(ip, XFS_ILOCK_EXCL);
2146 }
2147 } else if (e_inum < dp->i_ino) {
2148 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2149
2150 ips[0] = ip;
2151 ips[1] = dp;
2152 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2153 }
2154 /* else e_inum == dp->i_ino */
2155 /* This can happen if we're asked to lock /x/..
2156 * the entry is "..", which is also the parent directory.
2157 */
2158
2159 return 0;
2160}
2161
2162#ifdef DEBUG
2163int xfs_locked_n;
2164int xfs_small_retries;
2165int xfs_middle_retries;
2166int xfs_lots_retries;
2167int xfs_lock_delays;
2168#endif
2169
2170/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002171 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2172 * a different value
2173 */
2174static inline int
2175xfs_lock_inumorder(int lock_mode, int subclass)
2176{
2177 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002178 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002179 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002180 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002181
2182 return lock_mode;
2183}
2184
2185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 * The following routine will lock n inodes in exclusive mode.
2187 * We assume the caller calls us with the inodes in i_ino order.
2188 *
2189 * We need to detect deadlock where an inode that we lock
2190 * is in the AIL and we start waiting for another inode that is locked
2191 * by a thread in a long running transaction (such as truncate). This can
2192 * result in deadlock since the long running trans might need to wait
2193 * for the inode we just locked in order to push the tail and free space
2194 * in the log.
2195 */
2196void
2197xfs_lock_inodes(
2198 xfs_inode_t **ips,
2199 int inodes,
2200 int first_locked,
2201 uint lock_mode)
2202{
2203 int attempts = 0, i, j, try_lock;
2204 xfs_log_item_t *lp;
2205
2206 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2207
2208 if (first_locked) {
2209 try_lock = 1;
2210 i = 1;
2211 } else {
2212 try_lock = 0;
2213 i = 0;
2214 }
2215
2216again:
2217 for (; i < inodes; i++) {
2218 ASSERT(ips[i]);
2219
2220 if (i && (ips[i] == ips[i-1])) /* Already locked */
2221 continue;
2222
2223 /*
2224 * If try_lock is not set yet, make sure all locked inodes
2225 * are not in the AIL.
2226 * If any are, set try_lock to be used later.
2227 */
2228
2229 if (!try_lock) {
2230 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2231 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2232 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2233 try_lock++;
2234 }
2235 }
2236 }
2237
2238 /*
2239 * If any of the previous locks we have locked is in the AIL,
2240 * we must TRY to get the second and subsequent locks. If
2241 * we can't get any, we must release all we have
2242 * and try again.
2243 */
2244
2245 if (try_lock) {
2246 /* try_lock must be 0 if i is 0. */
2247 /*
2248 * try_lock means we have an inode locked
2249 * that is in the AIL.
2250 */
2251 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002252 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 attempts++;
2254
2255 /*
2256 * Unlock all previous guys and try again.
2257 * xfs_iunlock will try to push the tail
2258 * if the inode is in the AIL.
2259 */
2260
2261 for(j = i - 1; j >= 0; j--) {
2262
2263 /*
2264 * Check to see if we've already
2265 * unlocked this one.
2266 * Not the first one going back,
2267 * and the inode ptr is the same.
2268 */
2269 if ((j != (i - 1)) && ips[j] ==
2270 ips[j+1])
2271 continue;
2272
2273 xfs_iunlock(ips[j], lock_mode);
2274 }
2275
2276 if ((attempts % 5) == 0) {
2277 delay(1); /* Don't just spin the CPU */
2278#ifdef DEBUG
2279 xfs_lock_delays++;
2280#endif
2281 }
2282 i = 0;
2283 try_lock = 0;
2284 goto again;
2285 }
2286 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002287 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289 }
2290
2291#ifdef DEBUG
2292 if (attempts) {
2293 if (attempts < 5) xfs_small_retries++;
2294 else if (attempts < 100) xfs_middle_retries++;
2295 else xfs_lots_retries++;
2296 } else {
2297 xfs_locked_n++;
2298 }
2299#endif
2300}
2301
2302#ifdef DEBUG
2303#define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2304int remove_which_error_return = 0;
2305#else /* ! DEBUG */
2306#define REMOVE_DEBUG_TRACE(x)
2307#endif /* ! DEBUG */
2308
Christoph Hellwig993386c2007-08-28 16:12:30 +10002309int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002311 xfs_inode_t *dp,
2312 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002314 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 char *name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002316 xfs_mount_t *mp = dp->i_mount;
2317 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 xfs_trans_t *tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 int error = 0;
2320 xfs_bmap_free_t free_list;
2321 xfs_fsblock_t first_block;
2322 int cancel_flags;
2323 int committed;
2324 int dm_di_mode = 0;
2325 int link_zero;
2326 uint resblks;
2327 int namelen;
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (XFS_FORCED_SHUTDOWN(mp))
2332 return XFS_ERROR(EIO);
2333
2334 namelen = VNAMELEN(dentry);
2335
Vlad Apostolov17370092006-09-28 11:02:30 +10002336 if (!xfs_get_dir_entry(dentry, &ip)) {
2337 dm_di_mode = ip->i_d.di_mode;
2338 IRELE(ip);
2339 }
2340
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002341 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2343 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Vlad Apostolov17370092006-09-28 11:02:30 +10002344 name, NULL, dm_di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (error)
2346 return error;
2347 }
2348
2349 /* From this point on, return through std_return */
2350 ip = NULL;
2351
2352 /*
2353 * We need to get a reference to ip before we get our log
2354 * reservation. The reason for this is that we cannot call
2355 * xfs_iget for an inode for which we do not have a reference
2356 * once we've acquired a log reservation. This is because the
2357 * inode we are trying to get might be in xfs_inactive going
2358 * for a log reservation. Since we'll have to wait for the
2359 * inactive code to complete before returning from xfs_iget,
2360 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002361 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 * to the inode before getting our log reservation.
2363 */
2364 error = xfs_get_dir_entry(dentry, &ip);
2365 if (error) {
2366 REMOVE_DEBUG_TRACE(__LINE__);
2367 goto std_return;
2368 }
2369
2370 dm_di_mode = ip->i_d.di_mode;
2371
2372 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2373
2374 ITRACE(ip);
2375
2376 error = XFS_QM_DQATTACH(mp, dp, 0);
2377 if (!error && dp != ip)
2378 error = XFS_QM_DQATTACH(mp, ip, 0);
2379 if (error) {
2380 REMOVE_DEBUG_TRACE(__LINE__);
2381 IRELE(ip);
2382 goto std_return;
2383 }
2384
2385 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2386 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2387 /*
2388 * We try to get the real space reservation first,
2389 * allowing for directory btree deletion(s) implying
2390 * possible bmap insert(s). If we can't get the space
2391 * reservation then we use 0 instead, and avoid the bmap
2392 * btree insert(s) in the directory code by, if the bmap
2393 * insert tries to happen, instead trimming the LAST
2394 * block from the directory.
2395 */
2396 resblks = XFS_REMOVE_SPACE_RES(mp);
2397 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2398 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2399 if (error == ENOSPC) {
2400 resblks = 0;
2401 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2402 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2403 }
2404 if (error) {
2405 ASSERT(error != ENOSPC);
2406 REMOVE_DEBUG_TRACE(__LINE__);
2407 xfs_trans_cancel(tp, 0);
2408 IRELE(ip);
2409 return error;
2410 }
2411
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002412 error = xfs_lock_dir_and_entry(dp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (error) {
2414 REMOVE_DEBUG_TRACE(__LINE__);
2415 xfs_trans_cancel(tp, cancel_flags);
2416 IRELE(ip);
2417 goto std_return;
2418 }
2419
2420 /*
2421 * At this point, we've gotten both the directory and the entry
2422 * inodes locked.
2423 */
2424 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2425 if (dp != ip) {
2426 /*
2427 * Increment vnode ref count only in this case since
2428 * there's an extra vnode reference in the case where
2429 * dp == ip.
2430 */
2431 IHOLD(dp);
2432 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2433 }
2434
2435 /*
2436 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2437 */
2438 XFS_BMAP_INIT(&free_list, &first_block);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002439 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2440 &first_block, &free_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (error) {
2442 ASSERT(error != ENOENT);
2443 REMOVE_DEBUG_TRACE(__LINE__);
2444 goto error1;
2445 }
2446 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2447
2448 dp->i_gen++;
2449 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2450
2451 error = xfs_droplink(tp, ip);
2452 if (error) {
2453 REMOVE_DEBUG_TRACE(__LINE__);
2454 goto error1;
2455 }
2456
2457 /* Determine if this is the last link while
2458 * we are in the transaction.
2459 */
2460 link_zero = (ip)->i_d.di_nlink==0;
2461
2462 /*
2463 * Take an extra ref on the inode so that it doesn't
2464 * go to xfs_inactive() from within the commit.
2465 */
2466 IHOLD(ip);
2467
2468 /*
2469 * If this is a synchronous mount, make sure that the
2470 * remove transaction goes to disk before returning to
2471 * the user.
2472 */
2473 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2474 xfs_trans_set_sync(tp);
2475 }
2476
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002477 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (error) {
2479 REMOVE_DEBUG_TRACE(__LINE__);
2480 goto error_rele;
2481 }
2482
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002483 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (error) {
2485 IRELE(ip);
2486 goto std_return;
2487 }
2488
2489 /*
2490 * Before we drop our extra reference to the inode, purge it
2491 * from the refcache if it is there. By waiting until afterwards
2492 * to do the IRELE, we ensure that we won't go inactive in the
2493 * xfs_refcache_purge_ip routine (although that would be OK).
2494 */
2495 xfs_refcache_purge_ip(ip);
2496
David Chinner2a82b8b2007-07-11 11:09:12 +10002497 /*
2498 * If we are using filestreams, kill the stream association.
2499 * If the file is still open it may get a new one but that
2500 * will get killed on last close in xfs_close() so we don't
2501 * have to worry about that.
2502 */
2503 if (link_zero && xfs_inode_is_filestream(ip))
2504 xfs_filestream_deassociate(ip);
2505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 IRELE(ip);
2509
2510/* Fall through to std_return with error = 0 */
2511 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002512 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2514 dir_vp, DM_RIGHT_NULL,
2515 NULL, DM_RIGHT_NULL,
2516 name, NULL, dm_di_mode, error, 0);
2517 }
2518 return error;
2519
2520 error1:
2521 xfs_bmap_cancel(&free_list);
2522 cancel_flags |= XFS_TRANS_ABORT;
2523 xfs_trans_cancel(tp, cancel_flags);
2524 goto std_return;
2525
2526 error_rele:
2527 /*
2528 * In this case make sure to not release the inode until after
2529 * the current transaction is aborted. Releasing it beforehand
2530 * can cause us to go to xfs_inactive and start a recursive
2531 * transaction which can easily deadlock with the current one.
2532 */
2533 xfs_bmap_cancel(&free_list);
2534 cancel_flags |= XFS_TRANS_ABORT;
2535 xfs_trans_cancel(tp, cancel_flags);
2536
2537 /*
2538 * Before we drop our extra reference to the inode, purge it
2539 * from the refcache if it is there. By waiting until afterwards
2540 * to do the IRELE, we ensure that we won't go inactive in the
2541 * xfs_refcache_purge_ip routine (although that would be OK).
2542 */
2543 xfs_refcache_purge_ip(ip);
2544
2545 IRELE(ip);
2546
2547 goto std_return;
2548}
2549
Christoph Hellwig993386c2007-08-28 16:12:30 +10002550int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002552 xfs_inode_t *tdp,
Nathan Scott67fcaa72006-06-09 17:00:52 +10002553 bhv_vnode_t *src_vp,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002554 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002556 bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
2557 xfs_mount_t *mp = tdp->i_mount;
2558 xfs_inode_t *sip = xfs_vtoi(src_vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 xfs_inode_t *ips[2];
2561 int error;
2562 xfs_bmap_free_t free_list;
2563 xfs_fsblock_t first_block;
2564 int cancel_flags;
2565 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 int resblks;
2567 char *target_name = VNAME(dentry);
2568 int target_namelen;
2569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2571 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2572
2573 target_namelen = VNAMELEN(dentry);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002574 ASSERT(!VN_ISDIR(src_vp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if (XFS_FORCED_SHUTDOWN(mp))
2577 return XFS_ERROR(EIO);
2578
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002579 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2581 target_dir_vp, DM_RIGHT_NULL,
2582 src_vp, DM_RIGHT_NULL,
2583 target_name, NULL, 0, 0, 0);
2584 if (error)
2585 return error;
2586 }
2587
2588 /* Return through std_return after this point. */
2589
2590 error = XFS_QM_DQATTACH(mp, sip, 0);
2591 if (!error && sip != tdp)
2592 error = XFS_QM_DQATTACH(mp, tdp, 0);
2593 if (error)
2594 goto std_return;
2595
2596 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2597 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2598 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2599 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2600 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2601 if (error == ENOSPC) {
2602 resblks = 0;
2603 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2604 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2605 }
2606 if (error) {
2607 cancel_flags = 0;
2608 goto error_return;
2609 }
2610
2611 if (sip->i_ino < tdp->i_ino) {
2612 ips[0] = sip;
2613 ips[1] = tdp;
2614 } else {
2615 ips[0] = tdp;
2616 ips[1] = sip;
2617 }
2618
2619 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2620
2621 /*
2622 * Increment vnode ref counts since xfs_trans_commit &
2623 * xfs_trans_cancel will both unlock the inodes and
2624 * decrement the associated ref counts.
2625 */
2626 VN_HOLD(src_vp);
2627 VN_HOLD(target_dir_vp);
2628 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2629 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2630
2631 /*
2632 * If the source has too many links, we can't make any more to it.
2633 */
2634 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2635 error = XFS_ERROR(EMLINK);
2636 goto error_return;
2637 }
2638
Nathan Scott365ca832005-06-21 15:39:12 +10002639 /*
2640 * If we are using project inheritance, we only allow hard link
2641 * creation in our tree when the project IDs are the same; else
2642 * the tree quota mechanism could be circumvented.
2643 */
2644 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2645 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002646 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002647 goto error_return;
2648 }
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002651 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 goto error_return;
2653
2654 XFS_BMAP_INIT(&free_list, &first_block);
2655
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002656 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 sip->i_ino, &first_block, &free_list,
2658 resblks);
2659 if (error)
2660 goto abort_return;
2661 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2662 tdp->i_gen++;
2663 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2664
2665 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002666 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 /*
2670 * If this is a synchronous mount, make sure that the
2671 * link transaction goes to disk before returning to
2672 * the user.
2673 */
2674 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2675 xfs_trans_set_sync(tp);
2676 }
2677
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002678 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (error) {
2680 xfs_bmap_cancel(&free_list);
2681 goto abort_return;
2682 }
2683
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002684 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002685 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 /* Fall through to std_return with error = 0. */
2689std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002690 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2692 target_dir_vp, DM_RIGHT_NULL,
2693 src_vp, DM_RIGHT_NULL,
2694 target_name, NULL, 0, error, 0);
2695 }
2696 return error;
2697
2698 abort_return:
2699 cancel_flags |= XFS_TRANS_ABORT;
2700 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 error_return:
2703 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 goto std_return;
2705}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002706
2707
Christoph Hellwig993386c2007-08-28 16:12:30 +10002708int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002710 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10002711 bhv_vname_t *dentry,
2712 bhv_vattr_t *vap,
Nathan Scott67fcaa72006-06-09 17:00:52 +10002713 bhv_vnode_t **vpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 cred_t *credp)
2715{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002716 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 char *dir_name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002718 int dir_namelen = VNAMELEN(dentry);
2719 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 xfs_inode_t *cdp; /* inode of created dir */
Nathan Scott67fcaa72006-06-09 17:00:52 +10002721 bhv_vnode_t *cvp; /* vnode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 int cancel_flags;
2724 int error;
2725 int committed;
2726 xfs_bmap_free_t free_list;
2727 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002728 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 boolean_t created = B_FALSE;
2730 int dm_event_sent = 0;
2731 xfs_prid_t prid;
2732 struct xfs_dquot *udqp, *gdqp;
2733 uint resblks;
2734 int dm_di_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736 if (XFS_FORCED_SHUTDOWN(mp))
2737 return XFS_ERROR(EIO);
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 tp = NULL;
Christoph Hellwig0432dab2005-09-02 16:46:51 +10002740 dm_di_mode = vap->va_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002742 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2744 dir_vp, DM_RIGHT_NULL, NULL,
2745 DM_RIGHT_NULL, dir_name, NULL,
2746 dm_di_mode, 0, 0);
2747 if (error)
2748 return error;
2749 dm_event_sent = 1;
2750 }
2751
2752 /* Return through std_return after this point. */
2753
2754 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2755
2756 mp = dp->i_mount;
2757 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002758 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2759 prid = dp->i_d.di_projid;
2760 else if (vap->va_mask & XFS_AT_PROJID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 prid = (xfs_prid_t)vap->va_projid;
2762 else
2763 prid = (xfs_prid_t)dfltprid;
2764
2765 /*
2766 * Make sure that we have allocated dquot(s) on disk.
2767 */
2768 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002769 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2771 if (error)
2772 goto std_return;
2773
2774 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2775 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2776 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2777 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2778 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2779 if (error == ENOSPC) {
2780 resblks = 0;
2781 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2782 XFS_TRANS_PERM_LOG_RES,
2783 XFS_MKDIR_LOG_COUNT);
2784 }
2785 if (error) {
2786 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 goto error_return;
2788 }
2789
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002790 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002791 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 /*
2794 * Check for directory link count overflow.
2795 */
2796 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2797 error = XFS_ERROR(EMLINK);
2798 goto error_return;
2799 }
2800
2801 /*
2802 * Reserve disk quota and the inode.
2803 */
2804 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2805 if (error)
2806 goto error_return;
2807
2808 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002809 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 goto error_return;
2811 /*
2812 * create the directory inode.
2813 */
Christoph Hellwig0432dab2005-09-02 16:46:51 +10002814 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 0, credp, prid, resblks > 0,
2816 &cdp, NULL);
2817 if (error) {
2818 if (error == ENOSPC)
2819 goto error_return;
2820 goto abort_return;
2821 }
2822 ITRACE(cdp);
2823
2824 /*
2825 * Now we add the directory inode to the transaction.
2826 * We waited until now since xfs_dir_ialloc might start
2827 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002828 * earlier, the locks might have gotten released. An error
2829 * from here on will result in the transaction cancel
2830 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 */
2832 VN_HOLD(dir_vp);
2833 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002834 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 XFS_BMAP_INIT(&free_list, &first_block);
2837
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002838 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2839 &first_block, &free_list, resblks ?
2840 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (error) {
2842 ASSERT(error != ENOSPC);
2843 goto error1;
2844 }
2845 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2846
2847 /*
2848 * Bump the in memory version number of the parent directory
2849 * so that other processes accessing it will recognize that
2850 * the directory has changed.
2851 */
2852 dp->i_gen++;
2853
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002854 error = xfs_dir_init(tp, cdp, dp);
2855 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
2858 cdp->i_gen = 1;
2859 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002860 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 cvp = XFS_ITOV(cdp);
2864
2865 created = B_TRUE;
2866
2867 *vpp = cvp;
2868 IHOLD(cdp);
2869
2870 /*
2871 * Attach the dquots to the new inode and modify the icount incore.
2872 */
2873 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2874
2875 /*
2876 * If this is a synchronous mount, make sure that the
2877 * mkdir transaction goes to disk before returning to
2878 * the user.
2879 */
2880 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2881 xfs_trans_set_sync(tp);
2882 }
2883
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002884 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (error) {
2886 IRELE(cdp);
2887 goto error2;
2888 }
2889
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002890 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 XFS_QM_DQRELE(mp, udqp);
2892 XFS_QM_DQRELE(mp, gdqp);
2893 if (error) {
2894 IRELE(cdp);
2895 }
2896
2897 /* Fall through to std_return with error = 0 or errno from
2898 * xfs_trans_commit. */
2899
2900std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002901 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002902 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2904 dir_vp, DM_RIGHT_NULL,
2905 created ? XFS_ITOV(cdp):NULL,
2906 DM_RIGHT_NULL,
2907 dir_name, NULL,
2908 dm_di_mode, error, 0);
2909 }
2910 return error;
2911
2912 error2:
2913 error1:
2914 xfs_bmap_cancel(&free_list);
2915 abort_return:
2916 cancel_flags |= XFS_TRANS_ABORT;
2917 error_return:
2918 xfs_trans_cancel(tp, cancel_flags);
2919 XFS_QM_DQRELE(mp, udqp);
2920 XFS_QM_DQRELE(mp, gdqp);
2921
Christoph Hellwig993386c2007-08-28 16:12:30 +10002922 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 goto std_return;
2926}
2927
Christoph Hellwig993386c2007-08-28 16:12:30 +10002928int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929xfs_rmdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002930 xfs_inode_t *dp,
2931 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002933 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 char *name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002935 int namelen = VNAMELEN(dentry);
2936 xfs_mount_t *mp = dp->i_mount;
2937 xfs_inode_t *cdp; /* child directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 int error;
2940 xfs_bmap_free_t free_list;
2941 xfs_fsblock_t first_block;
2942 int cancel_flags;
2943 int committed;
Vlad Apostolov17370092006-09-28 11:02:30 +10002944 int dm_di_mode = S_IFDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 int last_cdp_link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 uint resblks;
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2949
Christoph Hellwig993386c2007-08-28 16:12:30 +10002950 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Vlad Apostolov17370092006-09-28 11:02:30 +10002953 if (!xfs_get_dir_entry(dentry, &cdp)) {
2954 dm_di_mode = cdp->i_d.di_mode;
2955 IRELE(cdp);
2956 }
2957
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002958 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
2960 dir_vp, DM_RIGHT_NULL,
2961 NULL, DM_RIGHT_NULL,
Vlad Apostolov17370092006-09-28 11:02:30 +10002962 name, NULL, dm_di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (error)
2964 return XFS_ERROR(error);
2965 }
2966
2967 /* Return through std_return after this point. */
2968
2969 cdp = NULL;
2970
2971 /*
2972 * We need to get a reference to cdp before we get our log
2973 * reservation. The reason for this is that we cannot call
2974 * xfs_iget for an inode for which we do not have a reference
2975 * once we've acquired a log reservation. This is because the
2976 * inode we are trying to get might be in xfs_inactive going
2977 * for a log reservation. Since we'll have to wait for the
2978 * inactive code to complete before returning from xfs_iget,
2979 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002980 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 * to the inode before getting our log reservation.
2982 */
2983 error = xfs_get_dir_entry(dentry, &cdp);
2984 if (error) {
2985 REMOVE_DEBUG_TRACE(__LINE__);
2986 goto std_return;
2987 }
2988 mp = dp->i_mount;
2989 dm_di_mode = cdp->i_d.di_mode;
2990
2991 /*
2992 * Get the dquots for the inodes.
2993 */
2994 error = XFS_QM_DQATTACH(mp, dp, 0);
2995 if (!error && dp != cdp)
2996 error = XFS_QM_DQATTACH(mp, cdp, 0);
2997 if (error) {
2998 IRELE(cdp);
2999 REMOVE_DEBUG_TRACE(__LINE__);
3000 goto std_return;
3001 }
3002
3003 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3004 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3005 /*
3006 * We try to get the real space reservation first,
3007 * allowing for directory btree deletion(s) implying
3008 * possible bmap insert(s). If we can't get the space
3009 * reservation then we use 0 instead, and avoid the bmap
3010 * btree insert(s) in the directory code by, if the bmap
3011 * insert tries to happen, instead trimming the LAST
3012 * block from the directory.
3013 */
3014 resblks = XFS_REMOVE_SPACE_RES(mp);
3015 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3016 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3017 if (error == ENOSPC) {
3018 resblks = 0;
3019 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3020 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3021 }
3022 if (error) {
3023 ASSERT(error != ENOSPC);
3024 cancel_flags = 0;
3025 IRELE(cdp);
3026 goto error_return;
3027 }
3028 XFS_BMAP_INIT(&free_list, &first_block);
3029
3030 /*
3031 * Now lock the child directory inode and the parent directory
3032 * inode in the proper order. This will take care of validating
3033 * that the directory entry for the child directory inode has
3034 * not changed while we were obtaining a log reservation.
3035 */
Eric Sandeene9ed9d22007-05-08 13:48:56 +10003036 error = xfs_lock_dir_and_entry(dp, cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 if (error) {
3038 xfs_trans_cancel(tp, cancel_flags);
3039 IRELE(cdp);
3040 goto std_return;
3041 }
3042
3043 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3044 if (dp != cdp) {
3045 /*
3046 * Only increment the parent directory vnode count if
3047 * we didn't bump it in looking up cdp. The only time
3048 * we don't bump it is when we're looking up ".".
3049 */
3050 VN_HOLD(dir_vp);
3051 }
3052
3053 ITRACE(cdp);
3054 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3055
3056 ASSERT(cdp->i_d.di_nlink >= 2);
3057 if (cdp->i_d.di_nlink != 2) {
3058 error = XFS_ERROR(ENOTEMPTY);
3059 goto error_return;
3060 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003061 if (!xfs_dir_isempty(cdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 error = XFS_ERROR(ENOTEMPTY);
3063 goto error_return;
3064 }
3065
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003066 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3067 &first_block, &free_list, resblks);
3068 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3072
3073 /*
3074 * Bump the in memory generation count on the parent
3075 * directory so that other can know that it has changed.
3076 */
3077 dp->i_gen++;
3078
3079 /*
3080 * Drop the link from cdp's "..".
3081 */
3082 error = xfs_droplink(tp, dp);
3083 if (error) {
3084 goto error1;
3085 }
3086
3087 /*
3088 * Drop the link from dp to cdp.
3089 */
3090 error = xfs_droplink(tp, cdp);
3091 if (error) {
3092 goto error1;
3093 }
3094
3095 /*
3096 * Drop the "." link from cdp to self.
3097 */
3098 error = xfs_droplink(tp, cdp);
3099 if (error) {
3100 goto error1;
3101 }
3102
3103 /* Determine these before committing transaction */
3104 last_cdp_link = (cdp)->i_d.di_nlink==0;
3105
3106 /*
3107 * Take an extra ref on the child vnode so that it
3108 * does not go to xfs_inactive() from within the commit.
3109 */
3110 IHOLD(cdp);
3111
3112 /*
3113 * If this is a synchronous mount, make sure that the
3114 * rmdir transaction goes to disk before returning to
3115 * the user.
3116 */
3117 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3118 xfs_trans_set_sync(tp);
3119 }
3120
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003121 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 if (error) {
3123 xfs_bmap_cancel(&free_list);
3124 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3125 XFS_TRANS_ABORT));
3126 IRELE(cdp);
3127 goto std_return;
3128 }
3129
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003130 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (error) {
3132 IRELE(cdp);
3133 goto std_return;
3134 }
3135
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 IRELE(cdp);
3138
3139 /* Fall through to std_return with error = 0 or the errno
3140 * from xfs_trans_commit. */
Nathan Scottbb19fba2006-03-22 14:12:12 +11003141 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003142 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3144 dir_vp, DM_RIGHT_NULL,
3145 NULL, DM_RIGHT_NULL,
3146 name, NULL, dm_di_mode,
3147 error, 0);
3148 }
3149 return error;
3150
Nathan Scottbb19fba2006-03-22 14:12:12 +11003151 error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 xfs_bmap_cancel(&free_list);
3153 cancel_flags |= XFS_TRANS_ABORT;
Jesper Juhl014c2542006-01-15 02:37:08 +01003154 /* FALLTHROUGH */
3155
Nathan Scottbb19fba2006-03-22 14:12:12 +11003156 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 xfs_trans_cancel(tp, cancel_flags);
3158 goto std_return;
3159}
3160
Christoph Hellwig993386c2007-08-28 16:12:30 +10003161int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003163 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10003164 bhv_vname_t *dentry,
3165 bhv_vattr_t *vap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 char *target_path,
Nathan Scott67fcaa72006-06-09 17:00:52 +10003167 bhv_vnode_t **vpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 cred_t *credp)
3169{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003170 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
3171 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 xfs_inode_t *ip;
3174 int error;
3175 int pathlen;
3176 xfs_bmap_free_t free_list;
3177 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10003178 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 uint cancel_flags;
3180 int committed;
3181 xfs_fileoff_t first_fsb;
3182 xfs_filblks_t fs_blocks;
3183 int nmaps;
3184 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3185 xfs_daddr_t d;
3186 char *cur_chunk;
3187 int byte_cnt;
3188 int n;
3189 xfs_buf_t *bp;
3190 xfs_prid_t prid;
3191 struct xfs_dquot *udqp, *gdqp;
3192 uint resblks;
3193 char *link_name = VNAME(dentry);
3194 int link_namelen;
3195
3196 *vpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 error = 0;
3198 ip = NULL;
3199 tp = NULL;
3200
3201 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
3204 if (XFS_FORCED_SHUTDOWN(mp))
3205 return XFS_ERROR(EIO);
3206
3207 link_namelen = VNAMELEN(dentry);
3208
3209 /*
3210 * Check component lengths of the target path name.
3211 */
3212 pathlen = strlen(target_path);
3213 if (pathlen >= MAXPATHLEN) /* total string too long */
3214 return XFS_ERROR(ENAMETOOLONG);
3215 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3216 int len, total;
3217 char *path;
3218
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003219 for (total = 0, path = target_path; total < pathlen;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 /*
3221 * Skip any slashes.
3222 */
3223 while(*path == '/') {
3224 total++;
3225 path++;
3226 }
3227
3228 /*
3229 * Count up to the next slash or end of path.
3230 * Error out if the component is bigger than MAXNAMELEN.
3231 */
3232 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3233 if (++len >= MAXNAMELEN) {
3234 error = ENAMETOOLONG;
3235 return error;
3236 }
3237 }
3238 }
3239 }
3240
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003241 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3243 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3244 link_name, target_path, 0, 0, 0);
3245 if (error)
3246 return error;
3247 }
3248
3249 /* Return through std_return after this point. */
3250
3251 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10003252 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3253 prid = dp->i_d.di_projid;
3254 else if (vap->va_mask & XFS_AT_PROJID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 prid = (xfs_prid_t)vap->va_projid;
3256 else
3257 prid = (xfs_prid_t)dfltprid;
3258
3259 /*
3260 * Make sure that we have allocated dquot(s) on disk.
3261 */
3262 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10003263 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3265 if (error)
3266 goto std_return;
3267
3268 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3269 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3270 /*
3271 * The symlink will fit into the inode data fork?
3272 * There can't be any attributes so we get the whole variable part.
3273 */
3274 if (pathlen <= XFS_LITINO(mp))
3275 fs_blocks = 0;
3276 else
3277 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3278 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3279 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3280 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3281 if (error == ENOSPC && fs_blocks == 0) {
3282 resblks = 0;
3283 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3284 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3285 }
3286 if (error) {
3287 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 goto error_return;
3289 }
3290
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10003291 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10003292 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 /*
3295 * Check whether the directory allows new symlinks or not.
3296 */
3297 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3298 error = XFS_ERROR(EPERM);
3299 goto error_return;
3300 }
3301
3302 /*
3303 * Reserve disk quota : blocks and inode.
3304 */
3305 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3306 if (error)
3307 goto error_return;
3308
3309 /*
3310 * Check for ability to enter directory entry, if no space reserved.
3311 */
3312 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003313 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 goto error_return;
3315 /*
3316 * Initialize the bmap freelist prior to calling either
3317 * bmapi or the directory create code.
3318 */
3319 XFS_BMAP_INIT(&free_list, &first_block);
3320
3321 /*
3322 * Allocate an inode for the symlink.
3323 */
3324 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3325 1, 0, credp, prid, resblks > 0, &ip, NULL);
3326 if (error) {
3327 if (error == ENOSPC)
3328 goto error_return;
3329 goto error1;
3330 }
3331 ITRACE(ip);
3332
Christoph Hellwig993386c2007-08-28 16:12:30 +10003333 /*
3334 * An error after we've joined dp to the transaction will result in the
3335 * transaction cancel unlocking dp so don't do it explicitly in the
3336 * error path.
3337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 VN_HOLD(dir_vp);
3339 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10003340 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 /*
3343 * Also attach the dquot(s) to it, if applicable.
3344 */
3345 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3346
3347 if (resblks)
3348 resblks -= XFS_IALLOC_SPACE_RES(mp);
3349 /*
3350 * If the symlink will fit into the inode, write it inline.
3351 */
3352 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3353 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3354 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3355 ip->i_d.di_size = pathlen;
3356
3357 /*
3358 * The inode was initially created in extent format.
3359 */
3360 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3361 ip->i_df.if_flags |= XFS_IFINLINE;
3362
3363 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3364 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3365
3366 } else {
3367 first_fsb = 0;
3368 nmaps = SYMLINK_MAPS;
3369
3370 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3371 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3372 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003373 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 if (error) {
3375 goto error1;
3376 }
3377
3378 if (resblks)
3379 resblks -= fs_blocks;
3380 ip->i_d.di_size = pathlen;
3381 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3382
3383 cur_chunk = target_path;
3384 for (n = 0; n < nmaps; n++) {
3385 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3386 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3387 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3388 BTOBB(byte_cnt), 0);
3389 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3390 if (pathlen < byte_cnt) {
3391 byte_cnt = pathlen;
3392 }
3393 pathlen -= byte_cnt;
3394
3395 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3396 cur_chunk += byte_cnt;
3397
3398 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3399 }
3400 }
3401
3402 /*
3403 * Create the directory entry for the symlink.
3404 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003405 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3406 &first_block, &free_list, resblks);
3407 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3410 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3411
3412 /*
3413 * Bump the in memory version number of the parent directory
3414 * so that other processes accessing it will recognize that
3415 * the directory has changed.
3416 */
3417 dp->i_gen++;
3418
3419 /*
3420 * If this is a synchronous mount, make sure that the
3421 * symlink transaction goes to disk before returning to
3422 * the user.
3423 */
3424 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3425 xfs_trans_set_sync(tp);
3426 }
3427
3428 /*
3429 * xfs_trans_commit normally decrements the vnode ref count
3430 * when it unlocks the inode. Since we want to return the
3431 * vnode to the caller, we bump the vnode ref count now.
3432 */
3433 IHOLD(ip);
3434
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003435 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 if (error) {
3437 goto error2;
3438 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003439 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 XFS_QM_DQRELE(mp, udqp);
3441 XFS_QM_DQRELE(mp, gdqp);
3442
3443 /* Fall through to std_return with error = 0 or errno from
3444 * xfs_trans_commit */
3445std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10003446 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3448 dir_vp, DM_RIGHT_NULL,
3449 error ? NULL : XFS_ITOV(ip),
3450 DM_RIGHT_NULL, link_name, target_path,
3451 0, error, 0);
3452 }
3453
3454 if (!error) {
Nathan Scott67fcaa72006-06-09 17:00:52 +10003455 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 ASSERT(ip);
3458 vp = XFS_ITOV(ip);
3459 *vpp = vp;
3460 }
3461 return error;
3462
3463 error2:
3464 IRELE(ip);
3465 error1:
3466 xfs_bmap_cancel(&free_list);
3467 cancel_flags |= XFS_TRANS_ABORT;
3468 error_return:
3469 xfs_trans_cancel(tp, cancel_flags);
3470 XFS_QM_DQRELE(mp, udqp);
3471 XFS_QM_DQRELE(mp, gdqp);
3472
Christoph Hellwig993386c2007-08-28 16:12:30 +10003473 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 goto std_return;
3477}
3478
3479
3480/*
3481 * xfs_fid2
3482 *
3483 * A fid routine that takes a pointer to a previously allocated
3484 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3485 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10003486int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487xfs_fid2(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003488 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 fid_t *fidp)
3490{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003491 xfs_fid2_t *xfid = (xfs_fid2_t *)fidp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Christoph Hellwig993386c2007-08-28 16:12:30 +10003493 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3497 xfid->fid_pad = 0;
3498 /*
3499 * use memcpy because the inode is a long long and there's no
3500 * assurance that xfid->fid_ino is properly aligned.
3501 */
3502 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3503 xfid->fid_gen = ip->i_d.di_gen;
3504
3505 return 0;
3506}
3507
3508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509int
3510xfs_rwlock(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003511 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +10003512 bhv_vrwlock_t locktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003514 if (S_ISDIR(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 if (locktype == VRWLOCK_WRITE) {
3517 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3518 } else if (locktype == VRWLOCK_TRY_READ) {
Jesper Juhl014c2542006-01-15 02:37:08 +01003519 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 } else if (locktype == VRWLOCK_TRY_WRITE) {
Jesper Juhl014c2542006-01-15 02:37:08 +01003521 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 } else {
3523 ASSERT((locktype == VRWLOCK_READ) ||
3524 (locktype == VRWLOCK_WRITE_DIRECT));
3525 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3526 }
3527
3528 return 1;
3529}
3530
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532void
3533xfs_rwunlock(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003534 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +10003535 bhv_vrwlock_t locktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003537 if (S_ISDIR(ip->i_d.di_mode))
3538 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 if (locktype == VRWLOCK_WRITE) {
3540 /*
3541 * In the write case, we may have added a new entry to
3542 * the reference cache. This might store a pointer to
3543 * an inode to be released in this inode. If it is there,
3544 * clear the pointer and release the inode after unlocking
3545 * this one.
3546 */
3547 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3548 } else {
3549 ASSERT((locktype == VRWLOCK_READ) ||
3550 (locktype == VRWLOCK_WRITE_DIRECT));
3551 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3552 }
3553 return;
3554}
3555
Christoph Hellwig993386c2007-08-28 16:12:30 +10003556
3557int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003559 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 int flags)
3561{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003562 xfs_mount_t *mp = ip->i_mount;
3563 xfs_inode_log_item_t *iip = ip->i_itemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 int error = 0;
3565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 if (XFS_FORCED_SHUTDOWN(mp))
3567 return XFS_ERROR(EIO);
3568
3569 /*
3570 * Bypass inodes which have already been cleaned by
3571 * the inode flush clustering code inside xfs_iflush
3572 */
3573 if ((ip->i_update_core == 0) &&
3574 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3575 return 0;
3576
3577 if (flags & FLUSH_LOG) {
3578 if (iip && iip->ili_last_lsn) {
3579 xlog_t *log = mp->m_log;
3580 xfs_lsn_t sync_lsn;
3581 int s, log_flags = XFS_LOG_FORCE;
3582
3583 s = GRANT_LOCK(log);
3584 sync_lsn = log->l_last_sync_lsn;
3585 GRANT_UNLOCK(log, s);
3586
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +10003587 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) > 0)) {
3588 if (flags & FLUSH_SYNC)
3589 log_flags |= XFS_LOG_SYNC;
3590 error = xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3591 if (error)
3592 return error;
3593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +10003595 if (ip->i_update_core == 0)
3596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 }
3598 }
3599
3600 /*
3601 * We make this non-blocking if the inode is contended,
3602 * return EAGAIN to indicate to the caller that they
3603 * did not succeed. This prevents the flush path from
3604 * blocking on inodes inside another operation right
3605 * now, they get caught later by xfs_sync.
3606 */
3607 if (flags & FLUSH_INODE) {
3608 int flush_flags;
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 if (flags & FLUSH_SYNC) {
3611 xfs_ilock(ip, XFS_ILOCK_SHARED);
3612 xfs_iflock(ip);
3613 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3614 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3615 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3616 return EAGAIN;
3617 }
3618 } else {
3619 return EAGAIN;
3620 }
3621
3622 if (flags & FLUSH_SYNC)
3623 flush_flags = XFS_IFLUSH_SYNC;
3624 else
3625 flush_flags = XFS_IFLUSH_ASYNC;
3626
3627 error = xfs_iflush(ip, flush_flags);
3628 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3629 }
3630
3631 return error;
3632}
3633
Christoph Hellwig993386c2007-08-28 16:12:30 +10003634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635int
Christoph Hellwig993386c2007-08-28 16:12:30 +10003636xfs_set_dmattrs(
3637 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10003639 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003641 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 int error;
3644
3645 if (!capable(CAP_SYS_ADMIN))
3646 return XFS_ERROR(EPERM);
3647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 if (XFS_FORCED_SHUTDOWN(mp))
3649 return XFS_ERROR(EIO);
3650
3651 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3652 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3653 if (error) {
3654 xfs_trans_cancel(tp, 0);
3655 return error;
3656 }
3657 xfs_ilock(ip, XFS_ILOCK_EXCL);
3658 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3659
3660 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3661 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3662
3663 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3664 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003665 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
3667 return error;
3668}
3669
Christoph Hellwig993386c2007-08-28 16:12:30 +10003670int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003672 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003674 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
3676 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3677
3678 ASSERT(!VN_MAPPED(vp));
3679
3680 /* bad inode, get out here ASAP */
3681 if (VN_BAD(vp)) {
3682 xfs_ireclaim(ip);
3683 return 0;
3684 }
3685
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10003686 vn_iowait(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10003688 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11003690 /*
3691 * Make sure the atime in the XFS inode is correct before freeing the
3692 * Linux inode.
3693 */
3694 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
David Chinner4c606582006-11-11 18:05:00 +11003696 /*
3697 * If we have nothing to flush with this inode then complete the
3698 * teardown now, otherwise break the link between the xfs inode and the
3699 * linux inode and clean up the xfs inode later. This avoids flushing
3700 * the inode to disk during the delete operation itself.
3701 *
3702 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3703 * first to ensure that xfs_iunpin() will never see an xfs inode
3704 * that has a linux inode being reclaimed. Synchronisation is provided
3705 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 */
3707 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3708 xfs_ilock(ip, XFS_ILOCK_EXCL);
3709 xfs_iflock(ip);
3710 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3711 } else {
3712 xfs_mount_t *mp = ip->i_mount;
3713
David Chinner4c606582006-11-11 18:05:00 +11003714 /* Protect sync and unpin from us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 XFS_MOUNT_ILOCK(mp);
David Chinner4c606582006-11-11 18:05:00 +11003716 spin_lock(&ip->i_flags_lock);
3717 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10003718 vn_to_inode(vp)->i_private = NULL;
3719 ip->i_vnode = NULL;
David Chinner4c606582006-11-11 18:05:00 +11003720 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 XFS_MOUNT_IUNLOCK(mp);
3723 }
3724 return 0;
3725}
3726
3727int
3728xfs_finish_reclaim(
3729 xfs_inode_t *ip,
3730 int locked,
3731 int sync_mode)
3732{
David Chinnerda353b02007-08-28 14:00:13 +10003733 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
Nathan Scott67fcaa72006-06-09 17:00:52 +10003734 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 int error;
3736
3737 if (vp && VN_BAD(vp))
3738 goto reclaim;
3739
3740 /* The hash lock here protects a thread in xfs_iget_core from
3741 * racing with us on linking the inode back with a vnode.
3742 * Once we have the XFS_IRECLAIM flag set it will not touch
3743 * us.
3744 */
David Chinnerda353b02007-08-28 14:00:13 +10003745 write_lock(&pag->pag_ici_lock);
David Chinnerf273ab82006-09-28 11:06:03 +10003746 spin_lock(&ip->i_flags_lock);
David Chinner7a18c382006-11-11 18:04:54 +11003747 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3748 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
David Chinnerf273ab82006-09-28 11:06:03 +10003749 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003750 write_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 if (locked) {
3752 xfs_ifunlock(ip);
3753 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3754 }
Jesper Juhl014c2542006-01-15 02:37:08 +01003755 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 }
David Chinner7a18c382006-11-11 18:04:54 +11003757 __xfs_iflags_set(ip, XFS_IRECLAIM);
David Chinnerf273ab82006-09-28 11:06:03 +10003758 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003759 write_unlock(&pag->pag_ici_lock);
3760 xfs_put_perag(ip->i_mount, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
3762 /*
3763 * If the inode is still dirty, then flush it out. If the inode
3764 * is not in the AIL, then it will be OK to flush it delwri as
3765 * long as xfs_iflush() does not keep any references to the inode.
3766 * We leave that decision up to xfs_iflush() since it has the
3767 * knowledge of whether it's OK to simply do a delwri flush of
3768 * the inode or whether we need to wait until the inode is
3769 * pulled from the AIL.
3770 * We get the flush lock regardless, though, just to make sure
3771 * we don't free it while it is being flushed.
3772 */
3773 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3774 if (!locked) {
3775 xfs_ilock(ip, XFS_ILOCK_EXCL);
3776 xfs_iflock(ip);
3777 }
3778
3779 if (ip->i_update_core ||
3780 ((ip->i_itemp != NULL) &&
3781 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3782 error = xfs_iflush(ip, sync_mode);
3783 /*
3784 * If we hit an error, typically because of filesystem
3785 * shutdown, we don't need to let vn_reclaim to know
3786 * because we're gonna reclaim the inode anyway.
3787 */
3788 if (error) {
3789 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3790 goto reclaim;
3791 }
3792 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3793 }
3794
3795 ASSERT(ip->i_update_core == 0);
3796 ASSERT(ip->i_itemp == NULL ||
3797 ip->i_itemp->ili_format.ilf_fields == 0);
3798 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3799 } else if (locked) {
3800 /*
3801 * We are not interested in doing an iflush if we're
3802 * in the process of shutting down the filesystem forcibly.
3803 * So, just reclaim the inode.
3804 */
3805 xfs_ifunlock(ip);
3806 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3807 }
3808
3809 reclaim:
3810 xfs_ireclaim(ip);
3811 return 0;
3812}
3813
3814int
3815xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3816{
3817 int purged;
3818 xfs_inode_t *ip, *n;
3819 int done = 0;
3820
3821 while (!done) {
3822 purged = 0;
3823 XFS_MOUNT_ILOCK(mp);
3824 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3825 if (noblock) {
3826 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3827 continue;
3828 if (xfs_ipincount(ip) ||
3829 !xfs_iflock_nowait(ip)) {
3830 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3831 continue;
3832 }
3833 }
3834 XFS_MOUNT_IUNLOCK(mp);
Felix Blyakher6b2cf612005-11-25 16:42:13 +11003835 if (xfs_finish_reclaim(ip, noblock,
3836 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3837 delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 purged = 1;
3839 break;
3840 }
3841
3842 done = !purged;
3843 }
3844
3845 XFS_MOUNT_IUNLOCK(mp);
3846 return 0;
3847}
3848
3849/*
3850 * xfs_alloc_file_space()
3851 * This routine allocates disk space for the given file.
3852 *
3853 * If alloc_type == 0, this request is for an ALLOCSP type
3854 * request which will change the file size. In this case, no
3855 * DMAPI event will be generated by the call. A TRUNCATE event
3856 * will be generated later by xfs_setattr.
3857 *
3858 * If alloc_type != 0, this request is for a RESVSP type
3859 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3860 * lower block boundary byte address is less than the file's
3861 * length.
3862 *
3863 * RETURNS:
3864 * 0 on success
3865 * errno on error
3866 *
3867 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003868STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869xfs_alloc_file_space(
3870 xfs_inode_t *ip,
3871 xfs_off_t offset,
3872 xfs_off_t len,
3873 int alloc_type,
3874 int attr_flags)
3875{
Nathan Scottdd9f4382006-01-11 15:28:28 +11003876 xfs_mount_t *mp = ip->i_mount;
3877 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 xfs_filblks_t allocated_fsb;
3879 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003880 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003882 xfs_fsblock_t firstfsb;
3883 int nimaps;
3884 int bmapi_flag;
3885 int quota_flag;
3886 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003888 xfs_bmbt_irec_t imaps[1], *imapp;
3889 xfs_bmap_free_t free_list;
3890 uint qblocks, resblks, resrtextents;
3891 int committed;
3892 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
3894 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
3896 if (XFS_FORCED_SHUTDOWN(mp))
3897 return XFS_ERROR(EIO);
3898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3900 return error;
3901
3902 if (len <= 0)
3903 return XFS_ERROR(EINVAL);
3904
David Chinner957d0eb2007-06-18 16:50:37 +10003905 rt = XFS_IS_REALTIME_INODE(ip);
3906 extsz = xfs_get_extsz_hint(ip);
3907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11003910 nimaps = 1;
3911 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3913 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3914
3915 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003916 if (alloc_type != 0 && offset < ip->i_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 (attr_flags&ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003918 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 xfs_off_t end_dmi_offset;
3920
3921 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003922 if (end_dmi_offset > ip->i_size)
3923 end_dmi_offset = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
3925 offset, end_dmi_offset - offset,
3926 0, NULL);
3927 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003928 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 }
3930
3931 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003932 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 */
3934retry:
3935 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003936 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Nathan Scottdd9f4382006-01-11 15:28:28 +11003938 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003939 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11003940 */
3941 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003943 do_div(s, extsz);
3944 s *= extsz;
3945 e = startoffset_fsb + allocatesize_fsb;
3946 if ((temp = do_mod(startoffset_fsb, extsz)))
3947 e += temp;
3948 if ((temp = do_mod(e, extsz)))
3949 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003951 s = 0;
3952 e = allocatesize_fsb;
3953 }
3954
3955 if (unlikely(rt)) {
3956 resrtextents = qblocks = (uint)(e - s);
3957 resrtextents /= mp->m_sb.sb_rextsize;
3958 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3959 quota_flag = XFS_QMOPT_RES_RTBLKS;
3960 } else {
3961 resrtextents = 0;
3962 resblks = qblocks = \
3963 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3964 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
3966
3967 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003968 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 */
3970 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003971 error = xfs_trans_reserve(tp, resblks,
3972 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 XFS_TRANS_PERM_LOG_RES,
3974 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003976 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 */
3978 if (error) {
3979 /*
3980 * Free the transaction structure.
3981 */
3982 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3983 xfs_trans_cancel(tp, 0);
3984 break;
3985 }
3986 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003987 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3988 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (error)
3990 goto error1;
3991
3992 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3993 xfs_trans_ihold(tp, ip);
3994
3995 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003996 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 */
3998 XFS_BMAP_INIT(&free_list, &firstfsb);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003999 error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004000 allocatesize_fsb, bmapi_flag,
4001 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004002 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 if (error) {
4004 goto error0;
4005 }
4006
4007 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11004008 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004010 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 if (error) {
4012 goto error0;
4013 }
4014
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004015 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4017 if (error) {
4018 break;
4019 }
4020
4021 allocated_fsb = imapp->br_blockcount;
4022
Nathan Scottdd9f4382006-01-11 15:28:28 +11004023 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 error = XFS_ERROR(ENOSPC);
4025 break;
4026 }
4027
4028 startoffset_fsb += allocated_fsb;
4029 allocatesize_fsb -= allocated_fsb;
4030 }
4031dmapi_enospc_check:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10004032 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
4033 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4035 XFS_ITOV(ip), DM_RIGHT_NULL,
4036 XFS_ITOV(ip), DM_RIGHT_NULL,
4037 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4038 if (error == 0)
4039 goto retry; /* Maybe DMAPI app. has made space */
4040 /* else fall through with error from XFS_SEND_DATA */
4041 }
4042
4043 return error;
4044
Nathan Scottdd9f4382006-01-11 15:28:28 +11004045error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004047 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4048
4049error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4051 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4052 goto dmapi_enospc_check;
4053}
4054
4055/*
4056 * Zero file bytes between startoff and endoff inclusive.
4057 * The iolock is held exclusive and no blocks are buffered.
4058 */
4059STATIC int
4060xfs_zero_remaining_bytes(
4061 xfs_inode_t *ip,
4062 xfs_off_t startoff,
4063 xfs_off_t endoff)
4064{
4065 xfs_bmbt_irec_t imap;
4066 xfs_fileoff_t offset_fsb;
4067 xfs_off_t lastoffset;
4068 xfs_off_t offset;
4069 xfs_buf_t *bp;
4070 xfs_mount_t *mp = ip->i_mount;
4071 int nimap;
4072 int error = 0;
4073
4074 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4075 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4076 mp->m_rtdev_targp : mp->m_ddev_targp);
4077
4078 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4079 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4080 nimap = 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004081 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
4082 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 if (error || nimap < 1)
4084 break;
4085 ASSERT(imap.br_blockcount >= 1);
4086 ASSERT(imap.br_startoff == offset_fsb);
4087 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4088 if (lastoffset > endoff)
4089 lastoffset = endoff;
4090 if (imap.br_startblock == HOLESTARTBLOCK)
4091 continue;
4092 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4093 if (imap.br_state == XFS_EXT_UNWRITTEN)
4094 continue;
4095 XFS_BUF_UNDONE(bp);
4096 XFS_BUF_UNWRITE(bp);
4097 XFS_BUF_READ(bp);
4098 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4099 xfsbdstrat(mp, bp);
4100 if ((error = xfs_iowait(bp))) {
4101 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4102 mp, bp, XFS_BUF_ADDR(bp));
4103 break;
4104 }
4105 memset(XFS_BUF_PTR(bp) +
4106 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4107 0, lastoffset - offset + 1);
4108 XFS_BUF_UNDONE(bp);
4109 XFS_BUF_UNREAD(bp);
4110 XFS_BUF_WRITE(bp);
4111 xfsbdstrat(mp, bp);
4112 if ((error = xfs_iowait(bp))) {
4113 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4114 mp, bp, XFS_BUF_ADDR(bp));
4115 break;
4116 }
4117 }
4118 xfs_buf_free(bp);
4119 return error;
4120}
4121
4122/*
4123 * xfs_free_file_space()
4124 * This routine frees disk space for the given file.
4125 *
4126 * This routine is only called by xfs_change_file_space
4127 * for an UNRESVSP type call.
4128 *
4129 * RETURNS:
4130 * 0 on success
4131 * errno on error
4132 *
4133 */
4134STATIC int
4135xfs_free_file_space(
4136 xfs_inode_t *ip,
4137 xfs_off_t offset,
4138 xfs_off_t len,
4139 int attr_flags)
4140{
Nathan Scott67fcaa72006-06-09 17:00:52 +10004141 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 int committed;
4143 int done;
4144 xfs_off_t end_dmi_offset;
4145 xfs_fileoff_t endoffset_fsb;
4146 int error;
4147 xfs_fsblock_t firstfsb;
4148 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 xfs_bmbt_irec_t imap;
4150 xfs_off_t ioffset;
4151 xfs_extlen_t mod=0;
4152 xfs_mount_t *mp;
4153 int nimap;
4154 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10004155 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 int rt;
4157 xfs_fileoff_t startoffset_fsb;
4158 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07004159 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004161 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 mp = ip->i_mount;
4163
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004164 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4167 return error;
4168
4169 error = 0;
4170 if (len <= 0) /* if nothing being freed */
4171 return error;
4172 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4173 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4174 end_dmi_offset = offset + len;
4175 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4176
Christoph Hellwigeb9df392007-08-16 18:42:07 +10004177 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
4178 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004179 if (end_dmi_offset > ip->i_size)
4180 end_dmi_offset = ip->i_size;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004181 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 offset, end_dmi_offset - offset,
4183 AT_DELAY_FLAG(attr_flags), NULL);
4184 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01004185 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 }
4187
Dean Roehrich5fcbab32005-05-05 13:27:19 -07004188 if (attr_flags & ATTR_NOLOCK)
4189 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11004190 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Yingping Lu9fa80462006-03-22 12:44:35 +11004192 vn_iowait(vp); /* wait for the completion of any pending DIOs */
4193 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07004194
Nathan Scott673cdf52006-09-28 10:56:26 +10004195 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, NBPP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004197
4198 if (VN_CACHED(vp) != 0) {
4199 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4200 ctooff(offtoct(ioffset)), -1);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10004201 error = xfs_flushinval_pages(ip,
4202 ctooff(offtoct(ioffset)),
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004203 -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10004204 if (error)
4205 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004206 }
4207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 /*
4209 * Need to zero the stuff we're not freeing, on disk.
4210 * If its a realtime file & can't use unwritten extents then we
4211 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4212 * will take care of it for us.
4213 */
4214 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4215 nimap = 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004216 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
4217 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 if (error)
4219 goto out_unlock_iolock;
4220 ASSERT(nimap == 0 || nimap == 1);
4221 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4222 xfs_daddr_t block;
4223
4224 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4225 block = imap.br_startblock;
4226 mod = do_div(block, mp->m_sb.sb_rextsize);
4227 if (mod)
4228 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4229 }
4230 nimap = 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004231 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
4232 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 if (error)
4234 goto out_unlock_iolock;
4235 ASSERT(nimap == 0 || nimap == 1);
4236 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4237 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4238 mod++;
4239 if (mod && (mod != mp->m_sb.sb_rextsize))
4240 endoffset_fsb -= mod;
4241 }
4242 }
4243 if ((done = (endoffset_fsb <= startoffset_fsb)))
4244 /*
4245 * One contiguous piece to clear
4246 */
4247 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4248 else {
4249 /*
4250 * Some full blocks, possibly two pieces to clear
4251 */
4252 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4253 error = xfs_zero_remaining_bytes(ip, offset,
4254 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4255 if (!error &&
4256 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4257 error = xfs_zero_remaining_bytes(ip,
4258 XFS_FSB_TO_B(mp, endoffset_fsb),
4259 offset + len - 1);
4260 }
4261
4262 /*
4263 * free file space until done or until there is an error
4264 */
4265 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4266 while (!error && !done) {
4267
4268 /*
David Chinner91ebecc2007-07-19 16:28:30 +10004269 * allocate and setup the transaction. Allow this
4270 * transaction to dip into the reserve blocks to ensure
4271 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 */
4273 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10004274 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 error = xfs_trans_reserve(tp,
4276 resblks,
4277 XFS_WRITE_LOG_RES(mp),
4278 0,
4279 XFS_TRANS_PERM_LOG_RES,
4280 XFS_WRITE_LOG_COUNT);
4281
4282 /*
4283 * check for running out of space
4284 */
4285 if (error) {
4286 /*
4287 * Free the transaction structure.
4288 */
4289 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4290 xfs_trans_cancel(tp, 0);
4291 break;
4292 }
4293 xfs_ilock(ip, XFS_ILOCK_EXCL);
4294 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004295 ip->i_udquot, ip->i_gdquot, resblks, 0,
4296 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 if (error)
4298 goto error1;
4299
4300 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4301 xfs_trans_ihold(tp, ip);
4302
4303 /*
4304 * issue the bunmapi() call to free the blocks
4305 */
4306 XFS_BMAP_INIT(&free_list, &firstfsb);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004307 error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004309 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 if (error) {
4311 goto error0;
4312 }
4313
4314 /*
4315 * complete the transaction
4316 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004317 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 if (error) {
4319 goto error0;
4320 }
4321
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004322 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4324 }
4325
4326 out_unlock_iolock:
4327 if (need_iolock)
4328 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4329 return error;
4330
4331 error0:
4332 xfs_bmap_cancel(&free_list);
4333 error1:
4334 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4335 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4336 XFS_ILOCK_EXCL);
4337 return error;
4338}
4339
4340/*
4341 * xfs_change_file_space()
4342 * This routine allocates or frees disk space for the given file.
4343 * The user specified parameters are checked for alignment and size
4344 * limitations.
4345 *
4346 * RETURNS:
4347 * 0 on success
4348 * errno on error
4349 *
4350 */
4351int
4352xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10004353 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 int cmd,
4355 xfs_flock64_t *bf,
4356 xfs_off_t offset,
4357 cred_t *credp,
4358 int attr_flags)
4359{
Christoph Hellwig993386c2007-08-28 16:12:30 +10004360 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 int clrprealloc;
4362 int error;
4363 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 int setprealloc;
4365 xfs_off_t startoffset;
4366 xfs_off_t llen;
4367 xfs_trans_t *tp;
Nathan Scott8285fb52006-06-09 17:07:12 +10004368 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
Christoph Hellwig993386c2007-08-28 16:12:30 +10004370 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
4372 /*
4373 * must be a regular file and have write permission
4374 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10004375 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 return XFS_ERROR(EINVAL);
4377
4378 xfs_ilock(ip, XFS_ILOCK_SHARED);
4379
4380 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4381 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4382 return error;
4383 }
4384
4385 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4386
4387 switch (bf->l_whence) {
4388 case 0: /*SEEK_SET*/
4389 break;
4390 case 1: /*SEEK_CUR*/
4391 bf->l_start += offset;
4392 break;
4393 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004394 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 break;
4396 default:
4397 return XFS_ERROR(EINVAL);
4398 }
4399
4400 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4401
4402 if ( (bf->l_start < 0)
4403 || (bf->l_start > XFS_MAXIOFFSET(mp))
4404 || (bf->l_start + llen < 0)
4405 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4406 return XFS_ERROR(EINVAL);
4407
4408 bf->l_whence = 0;
4409
4410 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004411 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
4413 /*
4414 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4415 * file space.
4416 * These calls do NOT zero the data space allocated to the file,
4417 * nor do they change the file size.
4418 *
4419 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4420 * space.
4421 * These calls cause the new file data to be zeroed and the file
4422 * size to be changed.
4423 */
4424 setprealloc = clrprealloc = 0;
4425
4426 switch (cmd) {
4427 case XFS_IOC_RESVSP:
4428 case XFS_IOC_RESVSP64:
4429 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4430 1, attr_flags);
4431 if (error)
4432 return error;
4433 setprealloc = 1;
4434 break;
4435
4436 case XFS_IOC_UNRESVSP:
4437 case XFS_IOC_UNRESVSP64:
4438 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4439 attr_flags)))
4440 return error;
4441 break;
4442
4443 case XFS_IOC_ALLOCSP:
4444 case XFS_IOC_ALLOCSP64:
4445 case XFS_IOC_FREESP:
4446 case XFS_IOC_FREESP64:
4447 if (startoffset > fsize) {
4448 error = xfs_alloc_file_space(ip, fsize,
4449 startoffset - fsize, 0, attr_flags);
4450 if (error)
4451 break;
4452 }
4453
4454 va.va_mask = XFS_AT_SIZE;
4455 va.va_size = startoffset;
4456
Christoph Hellwig993386c2007-08-28 16:12:30 +10004457 error = xfs_setattr(ip, &va, attr_flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
4459 if (error)
4460 return error;
4461
4462 clrprealloc = 1;
4463 break;
4464
4465 default:
4466 ASSERT(0);
4467 return XFS_ERROR(EINVAL);
4468 }
4469
4470 /*
4471 * update the inode timestamp, mode, and prealloc flag bits
4472 */
4473 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4474
4475 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4476 0, 0, 0))) {
4477 /* ASSERT(0); */
4478 xfs_trans_cancel(tp, 0);
4479 return error;
4480 }
4481
4482 xfs_ilock(ip, XFS_ILOCK_EXCL);
4483
4484 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4485 xfs_trans_ihold(tp, ip);
4486
4487 if ((attr_flags & ATTR_DMI) == 0) {
4488 ip->i_d.di_mode &= ~S_ISUID;
4489
4490 /*
4491 * Note that we don't have to worry about mandatory
4492 * file locking being disabled here because we only
4493 * clear the S_ISGID bit if the Group execute bit is
4494 * on, but if it was on then mandatory locking wouldn't
4495 * have been enabled.
4496 */
4497 if (ip->i_d.di_mode & S_IXGRP)
4498 ip->i_d.di_mode &= ~S_ISGID;
4499
4500 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4501 }
4502 if (setprealloc)
4503 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4504 else if (clrprealloc)
4505 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4506
4507 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4508 xfs_trans_set_sync(tp);
4509
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004510 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
4512 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4513
4514 return error;
4515}