blob: 27e1bea8169f216b187fc3ab85da5e96efb69b77 [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
Lachlan McIlroycf441ee2008-02-07 16:42:19 +110091 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
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
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100139 if (!(XFS_IS_REALTIME_INODE(ip))) {
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
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100231 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000233 if (mp->m_flags & XFS_MOUNT_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) &&
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100511 (XFS_IS_REALTIME_INODE(ip)) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 (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
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100523 if (XFS_IS_REALTIME_INODE(ip) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 ((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 */
Christoph Hellwigb677c212007-08-29 11:46:28 +1000601 vn_iowait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000602
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) {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000807 if (vap->va_xflags & XFS_XFLAG_REALTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100809 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
810 di_flags |= XFS_DIFLAG_EXTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 ip->i_d.di_flags = di_flags;
813 }
814 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
815 timeflags |= XFS_ICHGTIME_CHG;
816 }
817
818 /*
819 * Change file inode change time only if XFS_AT_CTIME set
820 * AND we have been called by a DMI function.
821 */
822
823 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
824 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
825 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
826 ip->i_update_core = 1;
827 timeflags &= ~XFS_ICHGTIME_CHG;
828 }
829
830 /*
831 * Send out timestamp changes that need to be set to the
832 * current time. Not done when called by a DMI function.
833 */
834 if (timeflags && !(flags & ATTR_DMI))
835 xfs_ichgtime(ip, timeflags);
836
837 XFS_STATS_INC(xs_ig_attrchg);
838
839 /*
840 * If this is a synchronous mount, make sure that the
841 * transaction goes to disk before returning to the user.
842 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000843 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * One for the truncate and one for the timestamps since we
845 * don't want to change the timestamps unless we're sure the
846 * truncate worked. Truncates are less than 1% of the laddis
847 * mix so this probably isn't worth the trouble to optimize.
848 */
849 code = 0;
850 if (tp) {
851 if (mp->m_flags & XFS_MOUNT_WSYNC)
852 xfs_trans_set_sync(tp);
853
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000854 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 /*
858 * If the (regular) file's mandatory locking mode changed, then
859 * notify the vnode. We do this under the inode lock to prevent
860 * racing calls to vop_vnode_change.
861 */
862 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 xfs_iunlock(ip, lock_flags);
865
866 /*
867 * Release any dquot(s) the inode had kept before chown.
868 */
869 XFS_QM_DQRELE(mp, olddquot1);
870 XFS_QM_DQRELE(mp, olddquot2);
871 XFS_QM_DQRELE(mp, udqp);
872 XFS_QM_DQRELE(mp, gdqp);
873
874 if (code) {
875 return code;
876 }
877
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000878 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 !(flags & ATTR_DMI)) {
880 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
881 NULL, DM_RIGHT_NULL, NULL, NULL,
882 0, 0, AT_DELAY_FLAG(flags));
883 }
884 return 0;
885
886 abort_return:
887 commit_flags |= XFS_TRANS_ABORT;
888 /* FALLTHROUGH */
889 error_return:
890 XFS_QM_DQRELE(mp, udqp);
891 XFS_QM_DQRELE(mp, gdqp);
892 if (tp) {
893 xfs_trans_cancel(tp, commit_flags);
894 }
895 if (lock_flags != 0) {
896 xfs_iunlock(ip, lock_flags);
897 }
898 return code;
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000902 * The maximum pathlen is 1024 bytes. Since the minimum file system
903 * blocksize is 512 bytes, we can get a max of 2 extents back from
904 * bmapi.
905 */
906#define SYMLINK_MAPS 2
907
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000908STATIC int
909xfs_readlink_bmap(
910 xfs_inode_t *ip,
911 char *link)
912{
913 xfs_mount_t *mp = ip->i_mount;
914 int pathlen = ip->i_d.di_size;
915 int nmaps = SYMLINK_MAPS;
916 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
917 xfs_daddr_t d;
918 int byte_cnt;
919 int n;
920 xfs_buf_t *bp;
921 int error = 0;
922
923 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
924 mval, &nmaps, NULL, NULL);
925 if (error)
926 goto out;
927
928 for (n = 0; n < nmaps; n++) {
929 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
930 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
931
932 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
933 error = XFS_BUF_GETERROR(bp);
934 if (error) {
935 xfs_ioerror_alert("xfs_readlink",
936 ip->i_mount, bp, XFS_BUF_ADDR(bp));
937 xfs_buf_relse(bp);
938 goto out;
939 }
940 if (pathlen < byte_cnt)
941 byte_cnt = pathlen;
942 pathlen -= byte_cnt;
943
944 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
945 xfs_buf_relse(bp);
946 }
947
948 link[ip->i_d.di_size] = '\0';
949 error = 0;
950
951 out:
952 return error;
953}
954
Christoph Hellwig993386c2007-08-28 16:12:30 +1000955int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000957 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000958 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000960 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100964 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (XFS_FORCED_SHUTDOWN(mp))
967 return XFS_ERROR(EIO);
968
969 xfs_ilock(ip, XFS_ILOCK_SHARED);
970
971 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000972 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000974 pathlen = ip->i_d.di_size;
975 if (!pathlen)
976 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000979 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
980 link[pathlen] = '\0';
981 } else {
982 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000985 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return error;
988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990/*
991 * xfs_fsync
992 *
993 * This is called to sync the inode and its data out to disk.
994 * We need to hold the I/O lock while flushing the data, and
995 * the inode lock while flushing the inode. The inode lock CANNOT
996 * be held while flushing the data, so acquire after we're done
997 * with that.
998 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000999int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000xfs_fsync(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001001 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int flag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 xfs_off_t start,
1004 xfs_off_t stop)
1005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 xfs_trans_t *tp;
1007 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001008 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001010 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 ASSERT(start >= 0 && stop >= -1);
1013
1014 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1015 return XFS_ERROR(EIO);
1016
Lachlan McIlroy776a75f2007-09-14 15:22:50 +10001017 if (flag & FSYNC_DATA)
1018 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /*
1021 * We always need to make sure that the required inode state
1022 * is safe on disk. The vnode might be clean but because
1023 * of committed transactions that haven't hit the disk yet.
1024 * Likewise, there could be unflushed non-transactional
1025 * changes to the inode core that have to go to disk.
1026 *
1027 * The following code depends on one assumption: that
1028 * any transaction that changes an inode logs the core
1029 * because it has to change some field in the inode core
1030 * (typically nextents or nblocks). That assumption
1031 * implies that any transactions against an inode will
1032 * catch any non-transactional updates. If inode-altering
1033 * transactions exist that violate this assumption, the
1034 * code breaks. Right now, it figures that if the involved
1035 * update_* field is clear and the inode is unpinned, the
1036 * inode is clean. Either it's been flushed or it's been
1037 * committed and the commit has hit the disk unpinning the inode.
1038 * (Note that xfs_inode_item_format() called at commit clears
1039 * the update_* fields.)
1040 */
1041 xfs_ilock(ip, XFS_ILOCK_SHARED);
1042
1043 /* If we are flushing data then we care about update_size
1044 * being set, otherwise we care about update_core
1045 */
1046 if ((flag & FSYNC_DATA) ?
1047 (ip->i_update_size == 0) :
1048 (ip->i_update_core == 0)) {
1049 /*
1050 * Timestamps/size haven't changed since last inode
1051 * flush or inode transaction commit. That means
1052 * either nothing got written or a transaction
1053 * committed which caught the updates. If the
1054 * latter happened and the transaction hasn't
1055 * hit the disk yet, the inode will be still
1056 * be pinned. If it is, force the log.
1057 */
1058
1059 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1060
1061 if (xfs_ipincount(ip)) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001062 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 XFS_LOG_FORCE |
1064 ((flag & FSYNC_WAIT)
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001065 ? XFS_LOG_SYNC : 0),
1066 &log_flushed);
1067 } else {
1068 /*
1069 * If the inode is not pinned and nothing
1070 * has changed we don't need to flush the
1071 * cache.
1072 */
1073 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075 error = 0;
1076 } else {
1077 /*
1078 * Kick off a transaction to log the inode
1079 * core to get the updates. Make it
1080 * sync if FSYNC_WAIT is passed in (which
1081 * is done by everybody but specfs). The
1082 * sync transaction will also force the log.
1083 */
1084 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1085 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1086 if ((error = xfs_trans_reserve(tp, 0,
1087 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1088 0, 0, 0))) {
1089 xfs_trans_cancel(tp, 0);
1090 return error;
1091 }
1092 xfs_ilock(ip, XFS_ILOCK_EXCL);
1093
1094 /*
1095 * Note - it's possible that we might have pushed
1096 * ourselves out of the way during trans_reserve
1097 * which would flush the inode. But there's no
1098 * guarantee that the inode buffer has actually
1099 * gone out yet (it's delwri). Plus the buffer
1100 * could be pinned anyway if it's part of an
1101 * inode in another recent transaction. So we
1102 * play it safe and fire off the transaction anyway.
1103 */
1104 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1105 xfs_trans_ihold(tp, ip);
1106 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1107 if (flag & FSYNC_WAIT)
1108 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001109 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1112 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001113
1114 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1115 /*
1116 * If the log write didn't issue an ordered tag we need
1117 * to flush the disk cache for the data device now.
1118 */
1119 if (!log_flushed)
1120 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1121
1122 /*
1123 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +10001124 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001125 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11001126 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001127 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1128 }
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return error;
1131}
1132
1133/*
David Chinner92dfe8d2007-05-24 15:22:19 +10001134 * This is called by xfs_inactive to free any blocks beyond eof
1135 * when the link count isn't zero and by xfs_dm_punch_hole() when
1136 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001138int
1139xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +10001141 xfs_inode_t *ip,
1142 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 xfs_trans_t *tp;
1145 int error;
1146 xfs_fileoff_t end_fsb;
1147 xfs_fileoff_t last_fsb;
1148 xfs_filblks_t map_len;
1149 int nimaps;
1150 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +10001151 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 /*
1154 * Figure out if there are any blocks beyond the end
1155 * of the file. If not, then there is nothing to do.
1156 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001157 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1159 map_len = last_fsb - end_fsb;
1160 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 nimaps = 1;
1164 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001165 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001166 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1168
1169 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001170 (imap.br_startblock != HOLESTARTBLOCK ||
1171 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /*
1173 * Attach the dquots to the inode up front.
1174 */
1175 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001176 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /*
1179 * There are blocks after the end of file.
1180 * Free them up now by truncating the file to
1181 * its current size.
1182 */
1183 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1184
1185 /*
1186 * Do the xfs_itruncate_start() call before
1187 * reserving any log space because
1188 * itruncate_start will call into the buffer
1189 * cache and we can't
1190 * do that within a transaction.
1191 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001192 if (use_iolock)
1193 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001194 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001195 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001196 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001197 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001198 if (use_iolock)
1199 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001200 return error;
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 error = xfs_trans_reserve(tp, 0,
1204 XFS_ITRUNCATE_LOG_RES(mp),
1205 0, XFS_TRANS_PERM_LOG_RES,
1206 XFS_ITRUNCATE_LOG_COUNT);
1207 if (error) {
1208 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1209 xfs_trans_cancel(tp, 0);
1210 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001211 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
1214 xfs_ilock(ip, XFS_ILOCK_EXCL);
1215 xfs_trans_ijoin(tp, ip,
1216 XFS_IOLOCK_EXCL |
1217 XFS_ILOCK_EXCL);
1218 xfs_trans_ihold(tp, ip);
1219
1220 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001221 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 XFS_DATA_FORK,
1223 0);
1224 /*
1225 * If we get an error at this point we
1226 * simply don't bother truncating the file.
1227 */
1228 if (error) {
1229 xfs_trans_cancel(tp,
1230 (XFS_TRANS_RELEASE_LOG_RES |
1231 XFS_TRANS_ABORT));
1232 } else {
1233 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001234 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
David Chinner92dfe8d2007-05-24 15:22:19 +10001236 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1237 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001239 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242/*
1243 * Free a symlink that has blocks associated with it.
1244 */
1245STATIC int
1246xfs_inactive_symlink_rmt(
1247 xfs_inode_t *ip,
1248 xfs_trans_t **tpp)
1249{
1250 xfs_buf_t *bp;
1251 int committed;
1252 int done;
1253 int error;
1254 xfs_fsblock_t first_block;
1255 xfs_bmap_free_t free_list;
1256 int i;
1257 xfs_mount_t *mp;
1258 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1259 int nmaps;
1260 xfs_trans_t *ntp;
1261 int size;
1262 xfs_trans_t *tp;
1263
1264 tp = *tpp;
1265 mp = ip->i_mount;
1266 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1267 /*
1268 * We're freeing a symlink that has some
1269 * blocks allocated to it. Free the
1270 * blocks here. We know that we've got
1271 * either 1 or 2 extents and that we can
1272 * free them all in one bunmapi call.
1273 */
1274 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1275 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1276 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1277 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1278 xfs_trans_cancel(tp, 0);
1279 *tpp = NULL;
1280 return error;
1281 }
1282 /*
1283 * Lock the inode, fix the size, and join it to the transaction.
1284 * Hold it so in the normal path, we still have it locked for
1285 * the second transaction. In the error paths we need it
1286 * held so the cancel won't rele it, see below.
1287 */
1288 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1289 size = (int)ip->i_d.di_size;
1290 ip->i_d.di_size = 0;
1291 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1292 xfs_trans_ihold(tp, ip);
1293 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1294 /*
1295 * Find the block(s) so we can inval and unmap them.
1296 */
1297 done = 0;
1298 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -08001299 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1301 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001302 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 goto error0;
1304 /*
1305 * Invalidate the block(s).
1306 */
1307 for (i = 0; i < nmaps; i++) {
1308 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1309 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1310 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1311 xfs_trans_binval(tp, bp);
1312 }
1313 /*
1314 * Unmap the dead block(s) to the free_list.
1315 */
1316 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001317 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 goto error1;
1319 ASSERT(done);
1320 /*
1321 * Commit the first transaction. This logs the EFI and the inode.
1322 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001323 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 goto error1;
1325 /*
1326 * The transaction must have been committed, since there were
1327 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1328 * The new tp has the extent freeing and EFDs.
1329 */
1330 ASSERT(committed);
1331 /*
1332 * The first xact was committed, so add the inode to the new one.
1333 * Mark it dirty so it will be logged and moved forward in the log as
1334 * part of every commit.
1335 */
1336 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1337 xfs_trans_ihold(tp, ip);
1338 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1339 /*
1340 * Get a new, empty transaction to return to our caller.
1341 */
1342 ntp = xfs_trans_dup(tp);
1343 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001344 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 * If we get an error on the commit here or on the reserve below,
1346 * we need to unlock the inode since the new transaction doesn't
1347 * have the inode attached.
1348 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001349 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 tp = ntp;
1351 if (error) {
1352 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1353 goto error0;
1354 }
1355 /*
1356 * Remove the memory for extent descriptions (just bookkeeping).
1357 */
1358 if (ip->i_df.if_bytes)
1359 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1360 ASSERT(ip->i_df.if_bytes == 0);
1361 /*
1362 * Put an itruncate log reservation in the new transaction
1363 * for our caller.
1364 */
1365 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1366 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1367 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1368 goto error0;
1369 }
1370 /*
1371 * Return with the inode locked but not joined to the transaction.
1372 */
1373 *tpp = tp;
1374 return 0;
1375
1376 error1:
1377 xfs_bmap_cancel(&free_list);
1378 error0:
1379 /*
1380 * Have to come here with the inode locked and either
1381 * (held and in the transaction) or (not in the transaction).
1382 * If the inode isn't held then cancel would iput it, but
1383 * that's wrong since this is inactive and the vnode ref
1384 * count is 0 already.
1385 * Cancel won't do anything to the inode if held, but it still
1386 * needs to be locked until the cancel is done, if it was
1387 * joined to the transaction.
1388 */
1389 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1390 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1391 *tpp = NULL;
1392 return error;
1393
1394}
1395
1396STATIC int
1397xfs_inactive_symlink_local(
1398 xfs_inode_t *ip,
1399 xfs_trans_t **tpp)
1400{
1401 int error;
1402
1403 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1404 /*
1405 * We're freeing a symlink which fit into
1406 * the inode. Just free the memory used
1407 * to hold the old symlink.
1408 */
1409 error = xfs_trans_reserve(*tpp, 0,
1410 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1411 0, XFS_TRANS_PERM_LOG_RES,
1412 XFS_ITRUNCATE_LOG_COUNT);
1413
1414 if (error) {
1415 xfs_trans_cancel(*tpp, 0);
1416 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001417 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1420
1421 /*
1422 * Zero length symlinks _can_ exist.
1423 */
1424 if (ip->i_df.if_bytes > 0) {
1425 xfs_idata_realloc(ip,
1426 -(ip->i_df.if_bytes),
1427 XFS_DATA_FORK);
1428 ASSERT(ip->i_df.if_bytes == 0);
1429 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433STATIC int
1434xfs_inactive_attrs(
1435 xfs_inode_t *ip,
1436 xfs_trans_t **tpp)
1437{
1438 xfs_trans_t *tp;
1439 int error;
1440 xfs_mount_t *mp;
1441
1442 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1443 tp = *tpp;
1444 mp = ip->i_mount;
1445 ASSERT(ip->i_d.di_forkoff != 0);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001446 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1448
1449 error = xfs_attr_inactive(ip);
1450 if (error) {
1451 *tpp = NULL;
1452 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001453 return error; /* goto out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455
1456 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1457 error = xfs_trans_reserve(tp, 0,
1458 XFS_IFREE_LOG_RES(mp),
1459 0, XFS_TRANS_PERM_LOG_RES,
1460 XFS_INACTIVE_LOG_COUNT);
1461 if (error) {
1462 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1463 xfs_trans_cancel(tp, 0);
1464 *tpp = NULL;
1465 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001466 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468
1469 xfs_ilock(ip, XFS_ILOCK_EXCL);
1470 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1471 xfs_trans_ihold(tp, ip);
1472 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1473
1474 ASSERT(ip->i_d.di_anextents == 0);
1475
1476 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Christoph Hellwig993386c2007-08-28 16:12:30 +10001480int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001482 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001484 bhv_vnode_t *vp = XFS_ITOV(ip);
1485 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 int error;
1487
Nathan Scott7d4fb402006-06-09 15:27:16 +10001488 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001492 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return 0;
1494
David Chinner2a82b8b2007-07-11 11:09:12 +10001495 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001496 int truncated;
1497
David Chinner2a82b8b2007-07-11 11:09:12 +10001498 /*
1499 * If we are using filestreams, and we have an unlinked
1500 * file that we are processing the last close on, then nothing
1501 * will be able to reopen and write to this file. Purge this
1502 * inode from the filestreams cache so that it doesn't delay
1503 * teardown of the inode.
1504 */
1505 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1506 xfs_filestream_deassociate(ip);
1507
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001508 /*
1509 * If we previously truncated this file and removed old data
1510 * in the process, we want to initiate "early" writeout on
1511 * the last close. This is an attempt to combat the notorious
1512 * NULL files problem which is particularly noticable from a
1513 * truncate down, buffered (re-)write (delalloc), followed by
1514 * a crash. What we are effectively doing here is
1515 * significantly reducing the time window where we'd otherwise
1516 * be exposed to that problem.
1517 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001518 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001519 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001520 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001521 }
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523#ifdef HAVE_REFCACHE
1524 /* If we are in the NFS reference cache then don't do this now */
1525 if (ip->i_refcache)
1526 return 0;
1527#endif
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (ip->i_d.di_nlink != 0) {
1530 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001531 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001532 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001534 (!(ip->i_d.di_flags &
1535 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001536 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1537 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001538 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* Update linux inode block count after free above */
Nathan Scottec86dc02006-03-17 17:25:36 +11001540 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 ip->i_d.di_nblocks + ip->i_delayed_blks);
1542 }
1543 }
1544
1545 return 0;
1546}
1547
1548/*
1549 * xfs_inactive
1550 *
1551 * This is called when the vnode reference count for the vnode
1552 * goes to zero. If the file has been unlinked, then it must
1553 * now be truncated. Also, we clear all of the read-ahead state
1554 * kept for the inode here since the file is now closed.
1555 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001556int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001558 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001560 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +10001561 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 xfs_fsblock_t first_block;
1563 int committed;
1564 xfs_trans_t *tp;
1565 xfs_mount_t *mp;
1566 int error;
1567 int truncate;
1568
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001569 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /*
1572 * If the inode is already free, then there can be nothing
1573 * to clean up here.
1574 */
1575 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1576 ASSERT(ip->i_df.if_real_bytes == 0);
1577 ASSERT(ip->i_df.if_broot_bytes == 0);
1578 return VN_INACTIVE_CACHE;
1579 }
1580
1581 /*
1582 * Only do a truncate if it's a regular file with
1583 * some actual space in it. It's OK to look at the
1584 * inode's fields without the lock because we're the
1585 * only one with a reference to the inode.
1586 */
1587 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001588 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1589 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1591
1592 mp = ip->i_mount;
1593
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001594 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1596 }
1597
1598 error = 0;
1599
1600 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001601 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto out;
1603
1604 if (ip->i_d.di_nlink != 0) {
1605 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001606 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001607 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001608 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1609 (!(ip->i_d.di_flags &
1610 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1611 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001612 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1613 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001614 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /* Update linux inode block count after free above */
Nathan Scottec86dc02006-03-17 17:25:36 +11001616 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 ip->i_d.di_nblocks + ip->i_delayed_blks);
1618 }
1619 goto out;
1620 }
1621
1622 ASSERT(ip->i_d.di_nlink == 0);
1623
1624 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001625 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1628 if (truncate) {
1629 /*
1630 * Do the xfs_itruncate_start() call before
1631 * reserving any log space because itruncate_start
1632 * will call into the buffer cache and we can't
1633 * do that within a transaction.
1634 */
1635 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1636
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001637 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1638 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001639 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001640 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1641 return VN_INACTIVE_CACHE;
1642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 error = xfs_trans_reserve(tp, 0,
1645 XFS_ITRUNCATE_LOG_RES(mp),
1646 0, XFS_TRANS_PERM_LOG_RES,
1647 XFS_ITRUNCATE_LOG_COUNT);
1648 if (error) {
1649 /* Don't call itruncate_cleanup */
1650 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1651 xfs_trans_cancel(tp, 0);
1652 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001653 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655
1656 xfs_ilock(ip, XFS_ILOCK_EXCL);
1657 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1658 xfs_trans_ihold(tp, ip);
1659
1660 /*
1661 * normally, we have to run xfs_itruncate_finish sync.
1662 * But if filesystem is wsync and we're in the inactive
1663 * path, then we know that nlink == 0, and that the
1664 * xaction that made nlink == 0 is permanently committed
1665 * since xfs_remove runs as a synchronous transaction.
1666 */
1667 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1668 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1669
1670 if (error) {
1671 xfs_trans_cancel(tp,
1672 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1673 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001674 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1677
1678 /*
1679 * If we get an error while cleaning up a
1680 * symlink we bail out.
1681 */
1682 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1683 xfs_inactive_symlink_rmt(ip, &tp) :
1684 xfs_inactive_symlink_local(ip, &tp);
1685
1686 if (error) {
1687 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001688 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
1691 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1692 xfs_trans_ihold(tp, ip);
1693 } else {
1694 error = xfs_trans_reserve(tp, 0,
1695 XFS_IFREE_LOG_RES(mp),
1696 0, XFS_TRANS_PERM_LOG_RES,
1697 XFS_INACTIVE_LOG_COUNT);
1698 if (error) {
1699 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1700 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001701 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
1704 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1705 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1706 xfs_trans_ihold(tp, ip);
1707 }
1708
1709 /*
1710 * If there are attributes associated with the file
1711 * then blow them away now. The code calls a routine
1712 * that recursively deconstructs the attribute fork.
1713 * We need to just commit the current transaction
1714 * because we can't use it for xfs_attr_inactive().
1715 */
1716 if (ip->i_d.di_anextents > 0) {
1717 error = xfs_inactive_attrs(ip, &tp);
1718 /*
1719 * If we got an error, the transaction is already
1720 * cancelled, and the inode is unlocked. Just get out.
1721 */
1722 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001723 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 } else if (ip->i_afp) {
1725 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1726 }
1727
1728 /*
1729 * Free the inode.
1730 */
1731 XFS_BMAP_INIT(&free_list, &first_block);
1732 error = xfs_ifree(tp, ip, &free_list);
1733 if (error) {
1734 /*
1735 * If we fail to free the inode, shut down. The cancel
1736 * might do that, we need to make sure. Otherwise the
1737 * inode might be lost for a long time or forever.
1738 */
1739 if (!XFS_FORCED_SHUTDOWN(mp)) {
1740 cmn_err(CE_NOTE,
1741 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1742 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001743 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1746 } else {
1747 /*
1748 * Credit the quota account(s). The inode is gone.
1749 */
1750 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1751
1752 /*
1753 * Just ignore errors at this point. There is
1754 * nothing we can do except to try to keep going.
1755 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001756 (void) xfs_bmap_finish(&tp, &free_list, &committed);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001757 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759 /*
1760 * Release the dquots held by inode, if any.
1761 */
1762 XFS_QM_DQDETACH(mp, ip);
1763
1764 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1765
1766 out:
1767 return VN_INACTIVE_CACHE;
1768}
1769
1770
Christoph Hellwig993386c2007-08-28 16:12:30 +10001771int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001773 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10001774 bhv_vname_t *dentry,
Christoph Hellwig993386c2007-08-28 16:12:30 +10001775 bhv_vnode_t **vpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001777 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 xfs_ino_t e_inum;
1779 int error;
1780 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001782 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1785 return XFS_ERROR(EIO);
1786
1787 lock_mode = xfs_ilock_map_shared(dp);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001788 error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (!error) {
1790 *vpp = XFS_ITOV(ip);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001791 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793 xfs_iunlock_map_shared(dp, lock_mode);
1794 return error;
1795}
1796
Christoph Hellwig993386c2007-08-28 16:12:30 +10001797int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798xfs_create(
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 Hellwig3e5daf02007-10-11 18:09:12 +10001801 mode_t mode,
1802 xfs_dev_t rdev,
Nathan Scott67fcaa72006-06-09 17:00:52 +10001803 bhv_vnode_t **vpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 cred_t *credp)
1805{
1806 char *name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001807 xfs_mount_t *mp = dp->i_mount;
1808 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
1809 xfs_inode_t *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +10001810 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 int error;
1813 xfs_bmap_free_t free_list;
1814 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001815 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int dm_event_sent = 0;
1817 uint cancel_flags;
1818 int committed;
1819 xfs_prid_t prid;
1820 struct xfs_dquot *udqp, *gdqp;
1821 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 int namelen;
1823
1824 ASSERT(!*vpp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001825 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 namelen = VNAMELEN(dentry);
1828
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001829 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1831 dir_vp, DM_RIGHT_NULL, NULL,
1832 DM_RIGHT_NULL, name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001833 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 if (error)
1836 return error;
1837 dm_event_sent = 1;
1838 }
1839
1840 if (XFS_FORCED_SHUTDOWN(mp))
1841 return XFS_ERROR(EIO);
1842
1843 /* Return through std_return after this point. */
1844
1845 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001846 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1847 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 else
1849 prid = (xfs_prid_t)dfltprid;
1850
1851 /*
1852 * Make sure that we have allocated dquot(s) on disk.
1853 */
1854 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001855 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1857 if (error)
1858 goto std_return;
1859
1860 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1863 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1864 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1865 /*
1866 * Initially assume that the file does not exist and
1867 * reserve the resources for that case. If that is not
1868 * the case we'll drop the one we have and get a more
1869 * appropriate transaction later.
1870 */
1871 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1872 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1873 if (error == ENOSPC) {
1874 resblks = 0;
1875 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1876 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1877 }
1878 if (error) {
1879 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto error_return;
1881 }
1882
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001883 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001884 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 XFS_BMAP_INIT(&free_list, &first_block);
1887
1888 ASSERT(ip == NULL);
1889
1890 /*
1891 * Reserve disk quota and the inode.
1892 */
1893 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1894 if (error)
1895 goto error_return;
1896
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001897 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001899 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 rdev, credp, prid, resblks > 0,
1901 &ip, &committed);
1902 if (error) {
1903 if (error == ENOSPC)
1904 goto error_return;
1905 goto abort_return;
1906 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001907 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /*
1910 * At this point, we've gotten a newly allocated inode.
1911 * It is locked (and joined to the transaction).
1912 */
1913
1914 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1915
1916 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001917 * Now we join the directory inode to the transaction. We do not do it
1918 * earlier because xfs_dir_ialloc might commit the previous transaction
1919 * (and release all the locks). An error from here on will result in
1920 * the transaction cancel unlocking dp so don't do it explicitly in the
1921 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 VN_HOLD(dir_vp);
1924 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001925 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001927 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
1928 &first_block, &free_list, resblks ?
1929 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (error) {
1931 ASSERT(error != ENOSPC);
1932 goto abort_return;
1933 }
1934 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1935 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1936
1937 /*
1938 * If this is a synchronous mount, make sure that the
1939 * create transaction goes to disk before returning to
1940 * the user.
1941 */
1942 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1943 xfs_trans_set_sync(tp);
1944 }
1945
1946 dp->i_gen++;
1947
1948 /*
1949 * Attach the dquot(s) to the inodes and modify them incore.
1950 * These ids of the inode couldn't have changed since the new
1951 * inode has been locked ever since it was created.
1952 */
1953 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1954
1955 /*
1956 * xfs_trans_commit normally decrements the vnode ref count
1957 * when it unlocks the inode. Since we want to return the
1958 * vnode to the caller, we bump the vnode ref count now.
1959 */
1960 IHOLD(ip);
1961 vp = XFS_ITOV(ip);
1962
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001963 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (error) {
1965 xfs_bmap_cancel(&free_list);
1966 goto abort_rele;
1967 }
1968
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001969 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (error) {
1971 IRELE(ip);
1972 tp = NULL;
1973 goto error_return;
1974 }
1975
1976 XFS_QM_DQRELE(mp, udqp);
1977 XFS_QM_DQRELE(mp, gdqp);
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 *vpp = vp;
1980
1981 /* Fallthrough to std_return with error = 0 */
1982
1983std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001984 if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10001985 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
1987 dir_vp, DM_RIGHT_NULL,
1988 *vpp ? vp:NULL,
1989 DM_RIGHT_NULL, name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001990 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
1992 return error;
1993
1994 abort_return:
1995 cancel_flags |= XFS_TRANS_ABORT;
1996 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Jesper Juhl014c2542006-01-15 02:37:08 +01001998 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (tp != NULL)
2000 xfs_trans_cancel(tp, cancel_flags);
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 XFS_QM_DQRELE(mp, udqp);
2003 XFS_QM_DQRELE(mp, gdqp);
2004
Christoph Hellwig993386c2007-08-28 16:12:30 +10002005 if (unlock_dp_on_error)
2006 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 goto std_return;
2009
2010 abort_rele:
2011 /*
2012 * Wait until after the current transaction is aborted to
2013 * release the inode. This prevents recursive transactions
2014 * and deadlocks from xfs_inactive.
2015 */
2016 cancel_flags |= XFS_TRANS_ABORT;
2017 xfs_trans_cancel(tp, cancel_flags);
2018 IRELE(ip);
2019
2020 XFS_QM_DQRELE(mp, udqp);
2021 XFS_QM_DQRELE(mp, gdqp);
2022
2023 goto std_return;
2024}
2025
2026#ifdef DEBUG
2027/*
2028 * Some counters to see if (and how often) we are hitting some deadlock
2029 * prevention code paths.
2030 */
2031
2032int xfs_rm_locks;
2033int xfs_rm_lock_delays;
2034int xfs_rm_attempts;
2035#endif
2036
2037/*
2038 * The following routine will lock the inodes associated with the
2039 * directory and the named entry in the directory. The locks are
2040 * acquired in increasing inode number.
2041 *
2042 * If the entry is "..", then only the directory is locked. The
2043 * vnode ref count will still include that from the .. entry in
2044 * this case.
2045 *
2046 * There is a deadlock we need to worry about. If the locked directory is
2047 * in the AIL, it might be blocking up the log. The next inode we lock
2048 * could be already locked by another thread waiting for log space (e.g
2049 * a permanent log reservation with a long running transaction (see
2050 * xfs_itruncate_finish)). To solve this, we must check if the directory
2051 * is in the ail and use lock_nowait. If we can't lock, we need to
2052 * drop the inode lock on the directory and try again. xfs_iunlock will
2053 * potentially push the tail if we were holding up the log.
2054 */
2055STATIC int
2056xfs_lock_dir_and_entry(
2057 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 xfs_inode_t *ip) /* inode of entry 'name' */
2059{
2060 int attempts;
2061 xfs_ino_t e_inum;
2062 xfs_inode_t *ips[2];
2063 xfs_log_item_t *lp;
2064
2065#ifdef DEBUG
2066 xfs_rm_locks++;
2067#endif
2068 attempts = 0;
2069
2070again:
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002071 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 e_inum = ip->i_ino;
2074
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002075 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 /*
2078 * We want to lock in increasing inum. Since we've already
2079 * acquired the lock on the directory, we may need to release
2080 * if if the inum of the entry turns out to be less.
2081 */
2082 if (e_inum > dp->i_ino) {
2083 /*
2084 * We are already in the right order, so just
2085 * lock on the inode of the entry.
2086 * We need to use nowait if dp is in the AIL.
2087 */
2088
2089 lp = (xfs_log_item_t *)dp->i_itemp;
2090 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2091 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2092 attempts++;
2093#ifdef DEBUG
2094 xfs_rm_attempts++;
2095#endif
2096
2097 /*
2098 * Unlock dp and try again.
2099 * xfs_iunlock will try to push the tail
2100 * if the inode is in the AIL.
2101 */
2102
2103 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2104
2105 if ((attempts % 5) == 0) {
2106 delay(1); /* Don't just spin the CPU */
2107#ifdef DEBUG
2108 xfs_rm_lock_delays++;
2109#endif
2110 }
2111 goto again;
2112 }
2113 } else {
2114 xfs_ilock(ip, XFS_ILOCK_EXCL);
2115 }
2116 } else if (e_inum < dp->i_ino) {
2117 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2118
2119 ips[0] = ip;
2120 ips[1] = dp;
2121 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2122 }
2123 /* else e_inum == dp->i_ino */
2124 /* This can happen if we're asked to lock /x/..
2125 * the entry is "..", which is also the parent directory.
2126 */
2127
2128 return 0;
2129}
2130
2131#ifdef DEBUG
2132int xfs_locked_n;
2133int xfs_small_retries;
2134int xfs_middle_retries;
2135int xfs_lots_retries;
2136int xfs_lock_delays;
2137#endif
2138
2139/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002140 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2141 * a different value
2142 */
2143static inline int
2144xfs_lock_inumorder(int lock_mode, int subclass)
2145{
2146 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002147 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002148 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002149 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002150
2151 return lock_mode;
2152}
2153
2154/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 * The following routine will lock n inodes in exclusive mode.
2156 * We assume the caller calls us with the inodes in i_ino order.
2157 *
2158 * We need to detect deadlock where an inode that we lock
2159 * is in the AIL and we start waiting for another inode that is locked
2160 * by a thread in a long running transaction (such as truncate). This can
2161 * result in deadlock since the long running trans might need to wait
2162 * for the inode we just locked in order to push the tail and free space
2163 * in the log.
2164 */
2165void
2166xfs_lock_inodes(
2167 xfs_inode_t **ips,
2168 int inodes,
2169 int first_locked,
2170 uint lock_mode)
2171{
2172 int attempts = 0, i, j, try_lock;
2173 xfs_log_item_t *lp;
2174
2175 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2176
2177 if (first_locked) {
2178 try_lock = 1;
2179 i = 1;
2180 } else {
2181 try_lock = 0;
2182 i = 0;
2183 }
2184
2185again:
2186 for (; i < inodes; i++) {
2187 ASSERT(ips[i]);
2188
2189 if (i && (ips[i] == ips[i-1])) /* Already locked */
2190 continue;
2191
2192 /*
2193 * If try_lock is not set yet, make sure all locked inodes
2194 * are not in the AIL.
2195 * If any are, set try_lock to be used later.
2196 */
2197
2198 if (!try_lock) {
2199 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2200 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2201 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2202 try_lock++;
2203 }
2204 }
2205 }
2206
2207 /*
2208 * If any of the previous locks we have locked is in the AIL,
2209 * we must TRY to get the second and subsequent locks. If
2210 * we can't get any, we must release all we have
2211 * and try again.
2212 */
2213
2214 if (try_lock) {
2215 /* try_lock must be 0 if i is 0. */
2216 /*
2217 * try_lock means we have an inode locked
2218 * that is in the AIL.
2219 */
2220 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002221 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 attempts++;
2223
2224 /*
2225 * Unlock all previous guys and try again.
2226 * xfs_iunlock will try to push the tail
2227 * if the inode is in the AIL.
2228 */
2229
2230 for(j = i - 1; j >= 0; j--) {
2231
2232 /*
2233 * Check to see if we've already
2234 * unlocked this one.
2235 * Not the first one going back,
2236 * and the inode ptr is the same.
2237 */
2238 if ((j != (i - 1)) && ips[j] ==
2239 ips[j+1])
2240 continue;
2241
2242 xfs_iunlock(ips[j], lock_mode);
2243 }
2244
2245 if ((attempts % 5) == 0) {
2246 delay(1); /* Don't just spin the CPU */
2247#ifdef DEBUG
2248 xfs_lock_delays++;
2249#endif
2250 }
2251 i = 0;
2252 try_lock = 0;
2253 goto again;
2254 }
2255 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002256 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258 }
2259
2260#ifdef DEBUG
2261 if (attempts) {
2262 if (attempts < 5) xfs_small_retries++;
2263 else if (attempts < 100) xfs_middle_retries++;
2264 else xfs_lots_retries++;
2265 } else {
2266 xfs_locked_n++;
2267 }
2268#endif
2269}
2270
2271#ifdef DEBUG
2272#define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2273int remove_which_error_return = 0;
2274#else /* ! DEBUG */
2275#define REMOVE_DEBUG_TRACE(x)
2276#endif /* ! DEBUG */
2277
Christoph Hellwig993386c2007-08-28 16:12:30 +10002278int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002280 xfs_inode_t *dp,
2281 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002283 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 char *name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002285 xfs_mount_t *mp = dp->i_mount;
2286 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 xfs_trans_t *tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 int error = 0;
2289 xfs_bmap_free_t free_list;
2290 xfs_fsblock_t first_block;
2291 int cancel_flags;
2292 int committed;
2293 int dm_di_mode = 0;
2294 int link_zero;
2295 uint resblks;
2296 int namelen;
2297
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002298 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (XFS_FORCED_SHUTDOWN(mp))
2301 return XFS_ERROR(EIO);
2302
2303 namelen = VNAMELEN(dentry);
2304
Vlad Apostolov17370092006-09-28 11:02:30 +10002305 if (!xfs_get_dir_entry(dentry, &ip)) {
2306 dm_di_mode = ip->i_d.di_mode;
2307 IRELE(ip);
2308 }
2309
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002310 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2312 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Vlad Apostolov17370092006-09-28 11:02:30 +10002313 name, NULL, dm_di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (error)
2315 return error;
2316 }
2317
2318 /* From this point on, return through std_return */
2319 ip = NULL;
2320
2321 /*
2322 * We need to get a reference to ip before we get our log
2323 * reservation. The reason for this is that we cannot call
2324 * xfs_iget for an inode for which we do not have a reference
2325 * once we've acquired a log reservation. This is because the
2326 * inode we are trying to get might be in xfs_inactive going
2327 * for a log reservation. Since we'll have to wait for the
2328 * inactive code to complete before returning from xfs_iget,
2329 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002330 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 * to the inode before getting our log reservation.
2332 */
2333 error = xfs_get_dir_entry(dentry, &ip);
2334 if (error) {
2335 REMOVE_DEBUG_TRACE(__LINE__);
2336 goto std_return;
2337 }
2338
2339 dm_di_mode = ip->i_d.di_mode;
2340
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002341 xfs_itrace_entry(ip);
2342 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 error = XFS_QM_DQATTACH(mp, dp, 0);
2345 if (!error && dp != ip)
2346 error = XFS_QM_DQATTACH(mp, ip, 0);
2347 if (error) {
2348 REMOVE_DEBUG_TRACE(__LINE__);
2349 IRELE(ip);
2350 goto std_return;
2351 }
2352
2353 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2354 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2355 /*
2356 * We try to get the real space reservation first,
2357 * allowing for directory btree deletion(s) implying
2358 * possible bmap insert(s). If we can't get the space
2359 * reservation then we use 0 instead, and avoid the bmap
2360 * btree insert(s) in the directory code by, if the bmap
2361 * insert tries to happen, instead trimming the LAST
2362 * block from the directory.
2363 */
2364 resblks = XFS_REMOVE_SPACE_RES(mp);
2365 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2366 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2367 if (error == ENOSPC) {
2368 resblks = 0;
2369 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2370 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2371 }
2372 if (error) {
2373 ASSERT(error != ENOSPC);
2374 REMOVE_DEBUG_TRACE(__LINE__);
2375 xfs_trans_cancel(tp, 0);
2376 IRELE(ip);
2377 return error;
2378 }
2379
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002380 error = xfs_lock_dir_and_entry(dp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (error) {
2382 REMOVE_DEBUG_TRACE(__LINE__);
2383 xfs_trans_cancel(tp, cancel_flags);
2384 IRELE(ip);
2385 goto std_return;
2386 }
2387
2388 /*
2389 * At this point, we've gotten both the directory and the entry
2390 * inodes locked.
2391 */
2392 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2393 if (dp != ip) {
2394 /*
2395 * Increment vnode ref count only in this case since
2396 * there's an extra vnode reference in the case where
2397 * dp == ip.
2398 */
2399 IHOLD(dp);
2400 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2401 }
2402
2403 /*
2404 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2405 */
2406 XFS_BMAP_INIT(&free_list, &first_block);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002407 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2408 &first_block, &free_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (error) {
2410 ASSERT(error != ENOENT);
2411 REMOVE_DEBUG_TRACE(__LINE__);
2412 goto error1;
2413 }
2414 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2415
2416 dp->i_gen++;
2417 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2418
2419 error = xfs_droplink(tp, ip);
2420 if (error) {
2421 REMOVE_DEBUG_TRACE(__LINE__);
2422 goto error1;
2423 }
2424
2425 /* Determine if this is the last link while
2426 * we are in the transaction.
2427 */
2428 link_zero = (ip)->i_d.di_nlink==0;
2429
2430 /*
2431 * Take an extra ref on the inode so that it doesn't
2432 * go to xfs_inactive() from within the commit.
2433 */
2434 IHOLD(ip);
2435
2436 /*
2437 * If this is a synchronous mount, make sure that the
2438 * remove transaction goes to disk before returning to
2439 * the user.
2440 */
2441 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2442 xfs_trans_set_sync(tp);
2443 }
2444
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002445 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (error) {
2447 REMOVE_DEBUG_TRACE(__LINE__);
2448 goto error_rele;
2449 }
2450
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002451 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (error) {
2453 IRELE(ip);
2454 goto std_return;
2455 }
2456
2457 /*
2458 * Before we drop our extra reference to the inode, purge it
2459 * from the refcache if it is there. By waiting until afterwards
2460 * to do the IRELE, we ensure that we won't go inactive in the
2461 * xfs_refcache_purge_ip routine (although that would be OK).
2462 */
2463 xfs_refcache_purge_ip(ip);
2464
David Chinner2a82b8b2007-07-11 11:09:12 +10002465 /*
2466 * If we are using filestreams, kill the stream association.
2467 * If the file is still open it may get a new one but that
2468 * will get killed on last close in xfs_close() so we don't
2469 * have to worry about that.
2470 */
2471 if (link_zero && xfs_inode_is_filestream(ip))
2472 xfs_filestream_deassociate(ip);
2473
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002474 xfs_itrace_exit(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 IRELE(ip);
2476
2477/* Fall through to std_return with error = 0 */
2478 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002479 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2481 dir_vp, DM_RIGHT_NULL,
2482 NULL, DM_RIGHT_NULL,
2483 name, NULL, dm_di_mode, error, 0);
2484 }
2485 return error;
2486
2487 error1:
2488 xfs_bmap_cancel(&free_list);
2489 cancel_flags |= XFS_TRANS_ABORT;
2490 xfs_trans_cancel(tp, cancel_flags);
2491 goto std_return;
2492
2493 error_rele:
2494 /*
2495 * In this case make sure to not release the inode until after
2496 * the current transaction is aborted. Releasing it beforehand
2497 * can cause us to go to xfs_inactive and start a recursive
2498 * transaction which can easily deadlock with the current one.
2499 */
2500 xfs_bmap_cancel(&free_list);
2501 cancel_flags |= XFS_TRANS_ABORT;
2502 xfs_trans_cancel(tp, cancel_flags);
2503
2504 /*
2505 * Before we drop our extra reference to the inode, purge it
2506 * from the refcache if it is there. By waiting until afterwards
2507 * to do the IRELE, we ensure that we won't go inactive in the
2508 * xfs_refcache_purge_ip routine (although that would be OK).
2509 */
2510 xfs_refcache_purge_ip(ip);
2511
2512 IRELE(ip);
2513
2514 goto std_return;
2515}
2516
Christoph Hellwig993386c2007-08-28 16:12:30 +10002517int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002519 xfs_inode_t *tdp,
Nathan Scott67fcaa72006-06-09 17:00:52 +10002520 bhv_vnode_t *src_vp,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002521 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002523 bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
2524 xfs_mount_t *mp = tdp->i_mount;
2525 xfs_inode_t *sip = xfs_vtoi(src_vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 xfs_inode_t *ips[2];
2528 int error;
2529 xfs_bmap_free_t free_list;
2530 xfs_fsblock_t first_block;
2531 int cancel_flags;
2532 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 int resblks;
2534 char *target_name = VNAME(dentry);
2535 int target_namelen;
2536
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002537 xfs_itrace_entry(tdp);
2538 xfs_itrace_entry(xfs_vtoi(src_vp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 target_namelen = VNAMELEN(dentry);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002541 ASSERT(!VN_ISDIR(src_vp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (XFS_FORCED_SHUTDOWN(mp))
2544 return XFS_ERROR(EIO);
2545
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002546 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2548 target_dir_vp, DM_RIGHT_NULL,
2549 src_vp, DM_RIGHT_NULL,
2550 target_name, NULL, 0, 0, 0);
2551 if (error)
2552 return error;
2553 }
2554
2555 /* Return through std_return after this point. */
2556
2557 error = XFS_QM_DQATTACH(mp, sip, 0);
2558 if (!error && sip != tdp)
2559 error = XFS_QM_DQATTACH(mp, tdp, 0);
2560 if (error)
2561 goto std_return;
2562
2563 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2564 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2565 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2566 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2567 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2568 if (error == ENOSPC) {
2569 resblks = 0;
2570 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2571 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2572 }
2573 if (error) {
2574 cancel_flags = 0;
2575 goto error_return;
2576 }
2577
2578 if (sip->i_ino < tdp->i_ino) {
2579 ips[0] = sip;
2580 ips[1] = tdp;
2581 } else {
2582 ips[0] = tdp;
2583 ips[1] = sip;
2584 }
2585
2586 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2587
2588 /*
2589 * Increment vnode ref counts since xfs_trans_commit &
2590 * xfs_trans_cancel will both unlock the inodes and
2591 * decrement the associated ref counts.
2592 */
2593 VN_HOLD(src_vp);
2594 VN_HOLD(target_dir_vp);
2595 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2596 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2597
2598 /*
2599 * If the source has too many links, we can't make any more to it.
2600 */
2601 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2602 error = XFS_ERROR(EMLINK);
2603 goto error_return;
2604 }
2605
Nathan Scott365ca832005-06-21 15:39:12 +10002606 /*
2607 * If we are using project inheritance, we only allow hard link
2608 * creation in our tree when the project IDs are the same; else
2609 * the tree quota mechanism could be circumvented.
2610 */
2611 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2612 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002613 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002614 goto error_return;
2615 }
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002618 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 goto error_return;
2620
2621 XFS_BMAP_INIT(&free_list, &first_block);
2622
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002623 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 sip->i_ino, &first_block, &free_list,
2625 resblks);
2626 if (error)
2627 goto abort_return;
2628 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2629 tdp->i_gen++;
2630 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2631
2632 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002633 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 /*
2637 * If this is a synchronous mount, make sure that the
2638 * link transaction goes to disk before returning to
2639 * the user.
2640 */
2641 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2642 xfs_trans_set_sync(tp);
2643 }
2644
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002645 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (error) {
2647 xfs_bmap_cancel(&free_list);
2648 goto abort_return;
2649 }
2650
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002651 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002652 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 /* Fall through to std_return with error = 0. */
2656std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002657 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2659 target_dir_vp, DM_RIGHT_NULL,
2660 src_vp, DM_RIGHT_NULL,
2661 target_name, NULL, 0, error, 0);
2662 }
2663 return error;
2664
2665 abort_return:
2666 cancel_flags |= XFS_TRANS_ABORT;
2667 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 error_return:
2670 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 goto std_return;
2672}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002673
2674
Christoph Hellwig993386c2007-08-28 16:12:30 +10002675int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002677 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10002678 bhv_vname_t *dentry,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002679 mode_t mode,
Nathan Scott67fcaa72006-06-09 17:00:52 +10002680 bhv_vnode_t **vpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 cred_t *credp)
2682{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002683 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 char *dir_name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002685 int dir_namelen = VNAMELEN(dentry);
2686 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 xfs_inode_t *cdp; /* inode of created dir */
Nathan Scott67fcaa72006-06-09 17:00:52 +10002688 bhv_vnode_t *cvp; /* vnode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 int cancel_flags;
2691 int error;
2692 int committed;
2693 xfs_bmap_free_t free_list;
2694 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002695 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 boolean_t created = B_FALSE;
2697 int dm_event_sent = 0;
2698 xfs_prid_t prid;
2699 struct xfs_dquot *udqp, *gdqp;
2700 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 if (XFS_FORCED_SHUTDOWN(mp))
2703 return XFS_ERROR(EIO);
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002707 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2709 dir_vp, DM_RIGHT_NULL, NULL,
2710 DM_RIGHT_NULL, dir_name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002711 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 if (error)
2713 return error;
2714 dm_event_sent = 1;
2715 }
2716
2717 /* Return through std_return after this point. */
2718
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002719 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 mp = dp->i_mount;
2722 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002723 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2724 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 else
2726 prid = (xfs_prid_t)dfltprid;
2727
2728 /*
2729 * Make sure that we have allocated dquot(s) on disk.
2730 */
2731 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002732 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2734 if (error)
2735 goto std_return;
2736
2737 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2738 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2739 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2740 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2741 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2742 if (error == ENOSPC) {
2743 resblks = 0;
2744 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2745 XFS_TRANS_PERM_LOG_RES,
2746 XFS_MKDIR_LOG_COUNT);
2747 }
2748 if (error) {
2749 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 goto error_return;
2751 }
2752
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002753 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002754 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 /*
2757 * Check for directory link count overflow.
2758 */
2759 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2760 error = XFS_ERROR(EMLINK);
2761 goto error_return;
2762 }
2763
2764 /*
2765 * Reserve disk quota and the inode.
2766 */
2767 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2768 if (error)
2769 goto error_return;
2770
2771 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002772 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 goto error_return;
2774 /*
2775 * create the directory inode.
2776 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002777 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 0, credp, prid, resblks > 0,
2779 &cdp, NULL);
2780 if (error) {
2781 if (error == ENOSPC)
2782 goto error_return;
2783 goto abort_return;
2784 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002785 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787 /*
2788 * Now we add the directory inode to the transaction.
2789 * We waited until now since xfs_dir_ialloc might start
2790 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002791 * earlier, the locks might have gotten released. An error
2792 * from here on will result in the transaction cancel
2793 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 */
2795 VN_HOLD(dir_vp);
2796 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002797 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 XFS_BMAP_INIT(&free_list, &first_block);
2800
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002801 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2802 &first_block, &free_list, resblks ?
2803 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 if (error) {
2805 ASSERT(error != ENOSPC);
2806 goto error1;
2807 }
2808 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2809
2810 /*
2811 * Bump the in memory version number of the parent directory
2812 * so that other processes accessing it will recognize that
2813 * the directory has changed.
2814 */
2815 dp->i_gen++;
2816
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002817 error = xfs_dir_init(tp, cdp, dp);
2818 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 cdp->i_gen = 1;
2822 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002823 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 cvp = XFS_ITOV(cdp);
2827
2828 created = B_TRUE;
2829
2830 *vpp = cvp;
2831 IHOLD(cdp);
2832
2833 /*
2834 * Attach the dquots to the new inode and modify the icount incore.
2835 */
2836 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2837
2838 /*
2839 * If this is a synchronous mount, make sure that the
2840 * mkdir transaction goes to disk before returning to
2841 * the user.
2842 */
2843 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2844 xfs_trans_set_sync(tp);
2845 }
2846
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002847 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 if (error) {
2849 IRELE(cdp);
2850 goto error2;
2851 }
2852
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002853 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 XFS_QM_DQRELE(mp, udqp);
2855 XFS_QM_DQRELE(mp, gdqp);
2856 if (error) {
2857 IRELE(cdp);
2858 }
2859
2860 /* Fall through to std_return with error = 0 or errno from
2861 * xfs_trans_commit. */
2862
2863std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002864 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002865 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2867 dir_vp, DM_RIGHT_NULL,
2868 created ? XFS_ITOV(cdp):NULL,
2869 DM_RIGHT_NULL,
2870 dir_name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002871 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873 return error;
2874
2875 error2:
2876 error1:
2877 xfs_bmap_cancel(&free_list);
2878 abort_return:
2879 cancel_flags |= XFS_TRANS_ABORT;
2880 error_return:
2881 xfs_trans_cancel(tp, cancel_flags);
2882 XFS_QM_DQRELE(mp, udqp);
2883 XFS_QM_DQRELE(mp, gdqp);
2884
Christoph Hellwig993386c2007-08-28 16:12:30 +10002885 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
2888 goto std_return;
2889}
2890
Christoph Hellwig993386c2007-08-28 16:12:30 +10002891int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892xfs_rmdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002893 xfs_inode_t *dp,
2894 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002896 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 char *name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002898 int namelen = VNAMELEN(dentry);
2899 xfs_mount_t *mp = dp->i_mount;
2900 xfs_inode_t *cdp; /* child directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 int error;
2903 xfs_bmap_free_t free_list;
2904 xfs_fsblock_t first_block;
2905 int cancel_flags;
2906 int committed;
Vlad Apostolov17370092006-09-28 11:02:30 +10002907 int dm_di_mode = S_IFDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 int last_cdp_link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 uint resblks;
2910
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002911 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Christoph Hellwig993386c2007-08-28 16:12:30 +10002913 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Vlad Apostolov17370092006-09-28 11:02:30 +10002916 if (!xfs_get_dir_entry(dentry, &cdp)) {
2917 dm_di_mode = cdp->i_d.di_mode;
2918 IRELE(cdp);
2919 }
2920
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002921 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
2923 dir_vp, DM_RIGHT_NULL,
2924 NULL, DM_RIGHT_NULL,
Vlad Apostolov17370092006-09-28 11:02:30 +10002925 name, NULL, dm_di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (error)
2927 return XFS_ERROR(error);
2928 }
2929
2930 /* Return through std_return after this point. */
2931
2932 cdp = NULL;
2933
2934 /*
2935 * We need to get a reference to cdp before we get our log
2936 * reservation. The reason for this is that we cannot call
2937 * xfs_iget for an inode for which we do not have a reference
2938 * once we've acquired a log reservation. This is because the
2939 * inode we are trying to get might be in xfs_inactive going
2940 * for a log reservation. Since we'll have to wait for the
2941 * inactive code to complete before returning from xfs_iget,
2942 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002943 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 * to the inode before getting our log reservation.
2945 */
2946 error = xfs_get_dir_entry(dentry, &cdp);
2947 if (error) {
2948 REMOVE_DEBUG_TRACE(__LINE__);
2949 goto std_return;
2950 }
2951 mp = dp->i_mount;
2952 dm_di_mode = cdp->i_d.di_mode;
2953
2954 /*
2955 * Get the dquots for the inodes.
2956 */
2957 error = XFS_QM_DQATTACH(mp, dp, 0);
2958 if (!error && dp != cdp)
2959 error = XFS_QM_DQATTACH(mp, cdp, 0);
2960 if (error) {
2961 IRELE(cdp);
2962 REMOVE_DEBUG_TRACE(__LINE__);
2963 goto std_return;
2964 }
2965
2966 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2967 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2968 /*
2969 * We try to get the real space reservation first,
2970 * allowing for directory btree deletion(s) implying
2971 * possible bmap insert(s). If we can't get the space
2972 * reservation then we use 0 instead, and avoid the bmap
2973 * btree insert(s) in the directory code by, if the bmap
2974 * insert tries to happen, instead trimming the LAST
2975 * block from the directory.
2976 */
2977 resblks = XFS_REMOVE_SPACE_RES(mp);
2978 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2979 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2980 if (error == ENOSPC) {
2981 resblks = 0;
2982 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2983 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2984 }
2985 if (error) {
2986 ASSERT(error != ENOSPC);
2987 cancel_flags = 0;
2988 IRELE(cdp);
2989 goto error_return;
2990 }
2991 XFS_BMAP_INIT(&free_list, &first_block);
2992
2993 /*
2994 * Now lock the child directory inode and the parent directory
2995 * inode in the proper order. This will take care of validating
2996 * that the directory entry for the child directory inode has
2997 * not changed while we were obtaining a log reservation.
2998 */
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002999 error = xfs_lock_dir_and_entry(dp, cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (error) {
3001 xfs_trans_cancel(tp, cancel_flags);
3002 IRELE(cdp);
3003 goto std_return;
3004 }
3005
3006 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3007 if (dp != cdp) {
3008 /*
3009 * Only increment the parent directory vnode count if
3010 * we didn't bump it in looking up cdp. The only time
3011 * we don't bump it is when we're looking up ".".
3012 */
3013 VN_HOLD(dir_vp);
3014 }
3015
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003016 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3018
3019 ASSERT(cdp->i_d.di_nlink >= 2);
3020 if (cdp->i_d.di_nlink != 2) {
3021 error = XFS_ERROR(ENOTEMPTY);
3022 goto error_return;
3023 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003024 if (!xfs_dir_isempty(cdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 error = XFS_ERROR(ENOTEMPTY);
3026 goto error_return;
3027 }
3028
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003029 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3030 &first_block, &free_list, resblks);
3031 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3035
3036 /*
3037 * Bump the in memory generation count on the parent
3038 * directory so that other can know that it has changed.
3039 */
3040 dp->i_gen++;
3041
3042 /*
3043 * Drop the link from cdp's "..".
3044 */
3045 error = xfs_droplink(tp, dp);
3046 if (error) {
3047 goto error1;
3048 }
3049
3050 /*
3051 * Drop the link from dp to cdp.
3052 */
3053 error = xfs_droplink(tp, cdp);
3054 if (error) {
3055 goto error1;
3056 }
3057
3058 /*
3059 * Drop the "." link from cdp to self.
3060 */
3061 error = xfs_droplink(tp, cdp);
3062 if (error) {
3063 goto error1;
3064 }
3065
3066 /* Determine these before committing transaction */
3067 last_cdp_link = (cdp)->i_d.di_nlink==0;
3068
3069 /*
3070 * Take an extra ref on the child vnode so that it
3071 * does not go to xfs_inactive() from within the commit.
3072 */
3073 IHOLD(cdp);
3074
3075 /*
3076 * If this is a synchronous mount, make sure that the
3077 * rmdir transaction goes to disk before returning to
3078 * the user.
3079 */
3080 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3081 xfs_trans_set_sync(tp);
3082 }
3083
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003084 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (error) {
3086 xfs_bmap_cancel(&free_list);
3087 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3088 XFS_TRANS_ABORT));
3089 IRELE(cdp);
3090 goto std_return;
3091 }
3092
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003093 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (error) {
3095 IRELE(cdp);
3096 goto std_return;
3097 }
3098
3099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 IRELE(cdp);
3101
3102 /* Fall through to std_return with error = 0 or the errno
3103 * from xfs_trans_commit. */
Nathan Scottbb19fba2006-03-22 14:12:12 +11003104 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003105 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3107 dir_vp, DM_RIGHT_NULL,
3108 NULL, DM_RIGHT_NULL,
3109 name, NULL, dm_di_mode,
3110 error, 0);
3111 }
3112 return error;
3113
Nathan Scottbb19fba2006-03-22 14:12:12 +11003114 error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 xfs_bmap_cancel(&free_list);
3116 cancel_flags |= XFS_TRANS_ABORT;
Jesper Juhl014c2542006-01-15 02:37:08 +01003117 /* FALLTHROUGH */
3118
Nathan Scottbb19fba2006-03-22 14:12:12 +11003119 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 xfs_trans_cancel(tp, cancel_flags);
3121 goto std_return;
3122}
3123
Christoph Hellwig993386c2007-08-28 16:12:30 +10003124int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003126 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10003127 bhv_vname_t *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10003129 mode_t mode,
Nathan Scott67fcaa72006-06-09 17:00:52 +10003130 bhv_vnode_t **vpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 cred_t *credp)
3132{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003133 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
3134 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 xfs_inode_t *ip;
3137 int error;
3138 int pathlen;
3139 xfs_bmap_free_t free_list;
3140 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10003141 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 uint cancel_flags;
3143 int committed;
3144 xfs_fileoff_t first_fsb;
3145 xfs_filblks_t fs_blocks;
3146 int nmaps;
3147 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3148 xfs_daddr_t d;
3149 char *cur_chunk;
3150 int byte_cnt;
3151 int n;
3152 xfs_buf_t *bp;
3153 xfs_prid_t prid;
3154 struct xfs_dquot *udqp, *gdqp;
3155 uint resblks;
3156 char *link_name = VNAME(dentry);
3157 int link_namelen;
3158
3159 *vpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 error = 0;
3161 ip = NULL;
3162 tp = NULL;
3163
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003164 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 if (XFS_FORCED_SHUTDOWN(mp))
3167 return XFS_ERROR(EIO);
3168
3169 link_namelen = VNAMELEN(dentry);
3170
3171 /*
3172 * Check component lengths of the target path name.
3173 */
3174 pathlen = strlen(target_path);
3175 if (pathlen >= MAXPATHLEN) /* total string too long */
3176 return XFS_ERROR(ENAMETOOLONG);
3177 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3178 int len, total;
3179 char *path;
3180
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003181 for (total = 0, path = target_path; total < pathlen;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 /*
3183 * Skip any slashes.
3184 */
3185 while(*path == '/') {
3186 total++;
3187 path++;
3188 }
3189
3190 /*
3191 * Count up to the next slash or end of path.
3192 * Error out if the component is bigger than MAXNAMELEN.
3193 */
3194 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3195 if (++len >= MAXNAMELEN) {
3196 error = ENAMETOOLONG;
3197 return error;
3198 }
3199 }
3200 }
3201 }
3202
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003203 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3205 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3206 link_name, target_path, 0, 0, 0);
3207 if (error)
3208 return error;
3209 }
3210
3211 /* Return through std_return after this point. */
3212
3213 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10003214 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3215 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 else
3217 prid = (xfs_prid_t)dfltprid;
3218
3219 /*
3220 * Make sure that we have allocated dquot(s) on disk.
3221 */
3222 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10003223 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3225 if (error)
3226 goto std_return;
3227
3228 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3229 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3230 /*
3231 * The symlink will fit into the inode data fork?
3232 * There can't be any attributes so we get the whole variable part.
3233 */
3234 if (pathlen <= XFS_LITINO(mp))
3235 fs_blocks = 0;
3236 else
3237 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3238 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3239 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3240 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3241 if (error == ENOSPC && fs_blocks == 0) {
3242 resblks = 0;
3243 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3244 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3245 }
3246 if (error) {
3247 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 goto error_return;
3249 }
3250
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10003251 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10003252 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254 /*
3255 * Check whether the directory allows new symlinks or not.
3256 */
3257 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3258 error = XFS_ERROR(EPERM);
3259 goto error_return;
3260 }
3261
3262 /*
3263 * Reserve disk quota : blocks and inode.
3264 */
3265 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3266 if (error)
3267 goto error_return;
3268
3269 /*
3270 * Check for ability to enter directory entry, if no space reserved.
3271 */
3272 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003273 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 goto error_return;
3275 /*
3276 * Initialize the bmap freelist prior to calling either
3277 * bmapi or the directory create code.
3278 */
3279 XFS_BMAP_INIT(&free_list, &first_block);
3280
3281 /*
3282 * Allocate an inode for the symlink.
3283 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10003284 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 1, 0, credp, prid, resblks > 0, &ip, NULL);
3286 if (error) {
3287 if (error == ENOSPC)
3288 goto error_return;
3289 goto error1;
3290 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003291 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Christoph Hellwig993386c2007-08-28 16:12:30 +10003293 /*
3294 * An error after we've joined dp to the transaction will result in the
3295 * transaction cancel unlocking dp so don't do it explicitly in the
3296 * error path.
3297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 VN_HOLD(dir_vp);
3299 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10003300 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
3302 /*
3303 * Also attach the dquot(s) to it, if applicable.
3304 */
3305 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3306
3307 if (resblks)
3308 resblks -= XFS_IALLOC_SPACE_RES(mp);
3309 /*
3310 * If the symlink will fit into the inode, write it inline.
3311 */
3312 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3313 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3314 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3315 ip->i_d.di_size = pathlen;
3316
3317 /*
3318 * The inode was initially created in extent format.
3319 */
3320 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3321 ip->i_df.if_flags |= XFS_IFINLINE;
3322
3323 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3324 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3325
3326 } else {
3327 first_fsb = 0;
3328 nmaps = SYMLINK_MAPS;
3329
3330 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3331 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3332 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003333 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 if (error) {
3335 goto error1;
3336 }
3337
3338 if (resblks)
3339 resblks -= fs_blocks;
3340 ip->i_d.di_size = pathlen;
3341 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3342
3343 cur_chunk = target_path;
3344 for (n = 0; n < nmaps; n++) {
3345 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3346 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3347 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3348 BTOBB(byte_cnt), 0);
3349 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3350 if (pathlen < byte_cnt) {
3351 byte_cnt = pathlen;
3352 }
3353 pathlen -= byte_cnt;
3354
3355 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3356 cur_chunk += byte_cnt;
3357
3358 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3359 }
3360 }
3361
3362 /*
3363 * Create the directory entry for the symlink.
3364 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003365 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3366 &first_block, &free_list, resblks);
3367 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3370 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3371
3372 /*
3373 * Bump the in memory version number of the parent directory
3374 * so that other processes accessing it will recognize that
3375 * the directory has changed.
3376 */
3377 dp->i_gen++;
3378
3379 /*
3380 * If this is a synchronous mount, make sure that the
3381 * symlink transaction goes to disk before returning to
3382 * the user.
3383 */
3384 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3385 xfs_trans_set_sync(tp);
3386 }
3387
3388 /*
3389 * xfs_trans_commit normally decrements the vnode ref count
3390 * when it unlocks the inode. Since we want to return the
3391 * vnode to the caller, we bump the vnode ref count now.
3392 */
3393 IHOLD(ip);
3394
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003395 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 if (error) {
3397 goto error2;
3398 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003399 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 XFS_QM_DQRELE(mp, udqp);
3401 XFS_QM_DQRELE(mp, gdqp);
3402
3403 /* Fall through to std_return with error = 0 or errno from
3404 * xfs_trans_commit */
3405std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10003406 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3408 dir_vp, DM_RIGHT_NULL,
3409 error ? NULL : XFS_ITOV(ip),
3410 DM_RIGHT_NULL, link_name, target_path,
3411 0, error, 0);
3412 }
3413
3414 if (!error) {
Nathan Scott67fcaa72006-06-09 17:00:52 +10003415 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
3417 ASSERT(ip);
3418 vp = XFS_ITOV(ip);
3419 *vpp = vp;
3420 }
3421 return error;
3422
3423 error2:
3424 IRELE(ip);
3425 error1:
3426 xfs_bmap_cancel(&free_list);
3427 cancel_flags |= XFS_TRANS_ABORT;
3428 error_return:
3429 xfs_trans_cancel(tp, cancel_flags);
3430 XFS_QM_DQRELE(mp, udqp);
3431 XFS_QM_DQRELE(mp, gdqp);
3432
Christoph Hellwig993386c2007-08-28 16:12:30 +10003433 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
3436 goto std_return;
3437}
3438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439int
3440xfs_rwlock(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003441 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +10003442 bhv_vrwlock_t locktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003444 if (S_ISDIR(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 if (locktype == VRWLOCK_WRITE) {
3447 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3448 } else if (locktype == VRWLOCK_TRY_READ) {
Jesper Juhl014c2542006-01-15 02:37:08 +01003449 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 } else if (locktype == VRWLOCK_TRY_WRITE) {
Jesper Juhl014c2542006-01-15 02:37:08 +01003451 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 } else {
3453 ASSERT((locktype == VRWLOCK_READ) ||
3454 (locktype == VRWLOCK_WRITE_DIRECT));
3455 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3456 }
3457
3458 return 1;
3459}
3460
3461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462void
3463xfs_rwunlock(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003464 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +10003465 bhv_vrwlock_t locktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003467 if (S_ISDIR(ip->i_d.di_mode))
3468 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (locktype == VRWLOCK_WRITE) {
3470 /*
3471 * In the write case, we may have added a new entry to
3472 * the reference cache. This might store a pointer to
3473 * an inode to be released in this inode. If it is there,
3474 * clear the pointer and release the inode after unlocking
3475 * this one.
3476 */
3477 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3478 } else {
3479 ASSERT((locktype == VRWLOCK_READ) ||
3480 (locktype == VRWLOCK_WRITE_DIRECT));
3481 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3482 }
3483 return;
3484}
3485
Christoph Hellwig993386c2007-08-28 16:12:30 +10003486
3487int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003489 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 int flags)
3491{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003492 xfs_mount_t *mp = ip->i_mount;
3493 xfs_inode_log_item_t *iip = ip->i_itemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 int error = 0;
3495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 if (XFS_FORCED_SHUTDOWN(mp))
3497 return XFS_ERROR(EIO);
3498
3499 /*
3500 * Bypass inodes which have already been cleaned by
3501 * the inode flush clustering code inside xfs_iflush
3502 */
3503 if ((ip->i_update_core == 0) &&
3504 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3505 return 0;
3506
3507 if (flags & FLUSH_LOG) {
3508 if (iip && iip->ili_last_lsn) {
3509 xlog_t *log = mp->m_log;
3510 xfs_lsn_t sync_lsn;
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003511 int log_flags = XFS_LOG_FORCE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003513 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 sync_lsn = log->l_last_sync_lsn;
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003515 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Lachlan McIlroy776a75f2007-09-14 15:22:50 +10003517 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) > 0)) {
3518 if (flags & FLUSH_SYNC)
3519 log_flags |= XFS_LOG_SYNC;
3520 error = xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3521 if (error)
3522 return error;
3523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Lachlan McIlroy776a75f2007-09-14 15:22:50 +10003525 if (ip->i_update_core == 0)
3526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
3528 }
3529
3530 /*
3531 * We make this non-blocking if the inode is contended,
3532 * return EAGAIN to indicate to the caller that they
3533 * did not succeed. This prevents the flush path from
3534 * blocking on inodes inside another operation right
3535 * now, they get caught later by xfs_sync.
3536 */
3537 if (flags & FLUSH_INODE) {
3538 int flush_flags;
3539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 if (flags & FLUSH_SYNC) {
3541 xfs_ilock(ip, XFS_ILOCK_SHARED);
3542 xfs_iflock(ip);
3543 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3544 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3545 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3546 return EAGAIN;
3547 }
3548 } else {
3549 return EAGAIN;
3550 }
3551
3552 if (flags & FLUSH_SYNC)
3553 flush_flags = XFS_IFLUSH_SYNC;
3554 else
3555 flush_flags = XFS_IFLUSH_ASYNC;
3556
3557 error = xfs_iflush(ip, flush_flags);
3558 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3559 }
3560
3561 return error;
3562}
3563
Christoph Hellwig993386c2007-08-28 16:12:30 +10003564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565int
Christoph Hellwig993386c2007-08-28 16:12:30 +10003566xfs_set_dmattrs(
3567 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10003569 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003571 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 int error;
3574
3575 if (!capable(CAP_SYS_ADMIN))
3576 return XFS_ERROR(EPERM);
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 if (XFS_FORCED_SHUTDOWN(mp))
3579 return XFS_ERROR(EIO);
3580
3581 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3582 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3583 if (error) {
3584 xfs_trans_cancel(tp, 0);
3585 return error;
3586 }
3587 xfs_ilock(ip, XFS_ILOCK_EXCL);
3588 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3589
Christoph Hellwig613d7042007-10-11 17:44:08 +10003590 ip->i_d.di_dmevmask = evmask;
3591 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
3593 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3594 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003595 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
3597 return error;
3598}
3599
Christoph Hellwig993386c2007-08-28 16:12:30 +10003600int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003602 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003604 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003606 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
3608 ASSERT(!VN_MAPPED(vp));
3609
3610 /* bad inode, get out here ASAP */
3611 if (VN_BAD(vp)) {
3612 xfs_ireclaim(ip);
3613 return 0;
3614 }
3615
Christoph Hellwigb677c212007-08-29 11:46:28 +10003616 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10003618 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11003620 /*
3621 * Make sure the atime in the XFS inode is correct before freeing the
3622 * Linux inode.
3623 */
3624 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
David Chinner4c606582006-11-11 18:05:00 +11003626 /*
3627 * If we have nothing to flush with this inode then complete the
3628 * teardown now, otherwise break the link between the xfs inode and the
3629 * linux inode and clean up the xfs inode later. This avoids flushing
3630 * the inode to disk during the delete operation itself.
3631 *
3632 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3633 * first to ensure that xfs_iunpin() will never see an xfs inode
3634 * that has a linux inode being reclaimed. Synchronisation is provided
3635 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 */
3637 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3638 xfs_ilock(ip, XFS_ILOCK_EXCL);
3639 xfs_iflock(ip);
3640 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3641 } else {
3642 xfs_mount_t *mp = ip->i_mount;
3643
David Chinner4c606582006-11-11 18:05:00 +11003644 /* Protect sync and unpin from us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 XFS_MOUNT_ILOCK(mp);
David Chinner4c606582006-11-11 18:05:00 +11003646 spin_lock(&ip->i_flags_lock);
3647 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10003648 vn_to_inode(vp)->i_private = NULL;
3649 ip->i_vnode = NULL;
David Chinner4c606582006-11-11 18:05:00 +11003650 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 XFS_MOUNT_IUNLOCK(mp);
3653 }
3654 return 0;
3655}
3656
3657int
3658xfs_finish_reclaim(
3659 xfs_inode_t *ip,
3660 int locked,
3661 int sync_mode)
3662{
David Chinnerda353b02007-08-28 14:00:13 +10003663 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
Nathan Scott67fcaa72006-06-09 17:00:52 +10003664 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 int error;
3666
3667 if (vp && VN_BAD(vp))
3668 goto reclaim;
3669
3670 /* The hash lock here protects a thread in xfs_iget_core from
3671 * racing with us on linking the inode back with a vnode.
3672 * Once we have the XFS_IRECLAIM flag set it will not touch
3673 * us.
3674 */
David Chinnerda353b02007-08-28 14:00:13 +10003675 write_lock(&pag->pag_ici_lock);
David Chinnerf273ab82006-09-28 11:06:03 +10003676 spin_lock(&ip->i_flags_lock);
David Chinner7a18c382006-11-11 18:04:54 +11003677 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3678 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
David Chinnerf273ab82006-09-28 11:06:03 +10003679 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003680 write_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 if (locked) {
3682 xfs_ifunlock(ip);
3683 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3684 }
Jesper Juhl014c2542006-01-15 02:37:08 +01003685 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 }
David Chinner7a18c382006-11-11 18:04:54 +11003687 __xfs_iflags_set(ip, XFS_IRECLAIM);
David Chinnerf273ab82006-09-28 11:06:03 +10003688 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003689 write_unlock(&pag->pag_ici_lock);
3690 xfs_put_perag(ip->i_mount, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
3692 /*
3693 * If the inode is still dirty, then flush it out. If the inode
3694 * is not in the AIL, then it will be OK to flush it delwri as
3695 * long as xfs_iflush() does not keep any references to the inode.
3696 * We leave that decision up to xfs_iflush() since it has the
3697 * knowledge of whether it's OK to simply do a delwri flush of
3698 * the inode or whether we need to wait until the inode is
3699 * pulled from the AIL.
3700 * We get the flush lock regardless, though, just to make sure
3701 * we don't free it while it is being flushed.
3702 */
3703 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3704 if (!locked) {
3705 xfs_ilock(ip, XFS_ILOCK_EXCL);
3706 xfs_iflock(ip);
3707 }
3708
3709 if (ip->i_update_core ||
3710 ((ip->i_itemp != NULL) &&
3711 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3712 error = xfs_iflush(ip, sync_mode);
3713 /*
3714 * If we hit an error, typically because of filesystem
3715 * shutdown, we don't need to let vn_reclaim to know
3716 * because we're gonna reclaim the inode anyway.
3717 */
3718 if (error) {
3719 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3720 goto reclaim;
3721 }
3722 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3723 }
3724
3725 ASSERT(ip->i_update_core == 0);
3726 ASSERT(ip->i_itemp == NULL ||
3727 ip->i_itemp->ili_format.ilf_fields == 0);
3728 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3729 } else if (locked) {
3730 /*
3731 * We are not interested in doing an iflush if we're
3732 * in the process of shutting down the filesystem forcibly.
3733 * So, just reclaim the inode.
3734 */
3735 xfs_ifunlock(ip);
3736 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3737 }
3738
3739 reclaim:
3740 xfs_ireclaim(ip);
3741 return 0;
3742}
3743
3744int
3745xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3746{
3747 int purged;
3748 xfs_inode_t *ip, *n;
3749 int done = 0;
3750
3751 while (!done) {
3752 purged = 0;
3753 XFS_MOUNT_ILOCK(mp);
3754 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3755 if (noblock) {
3756 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3757 continue;
3758 if (xfs_ipincount(ip) ||
3759 !xfs_iflock_nowait(ip)) {
3760 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3761 continue;
3762 }
3763 }
3764 XFS_MOUNT_IUNLOCK(mp);
Felix Blyakher6b2cf612005-11-25 16:42:13 +11003765 if (xfs_finish_reclaim(ip, noblock,
3766 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3767 delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 purged = 1;
3769 break;
3770 }
3771
3772 done = !purged;
3773 }
3774
3775 XFS_MOUNT_IUNLOCK(mp);
3776 return 0;
3777}
3778
3779/*
3780 * xfs_alloc_file_space()
3781 * This routine allocates disk space for the given file.
3782 *
3783 * If alloc_type == 0, this request is for an ALLOCSP type
3784 * request which will change the file size. In this case, no
3785 * DMAPI event will be generated by the call. A TRUNCATE event
3786 * will be generated later by xfs_setattr.
3787 *
3788 * If alloc_type != 0, this request is for a RESVSP type
3789 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3790 * lower block boundary byte address is less than the file's
3791 * length.
3792 *
3793 * RETURNS:
3794 * 0 on success
3795 * errno on error
3796 *
3797 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003798STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799xfs_alloc_file_space(
3800 xfs_inode_t *ip,
3801 xfs_off_t offset,
3802 xfs_off_t len,
3803 int alloc_type,
3804 int attr_flags)
3805{
Nathan Scottdd9f4382006-01-11 15:28:28 +11003806 xfs_mount_t *mp = ip->i_mount;
3807 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 xfs_filblks_t allocated_fsb;
3809 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003810 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003812 xfs_fsblock_t firstfsb;
3813 int nimaps;
3814 int bmapi_flag;
3815 int quota_flag;
3816 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003818 xfs_bmbt_irec_t imaps[1], *imapp;
3819 xfs_bmap_free_t free_list;
3820 uint qblocks, resblks, resrtextents;
3821 int committed;
3822 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003824 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
3826 if (XFS_FORCED_SHUTDOWN(mp))
3827 return XFS_ERROR(EIO);
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3830 return error;
3831
3832 if (len <= 0)
3833 return XFS_ERROR(EINVAL);
3834
David Chinner957d0eb2007-06-18 16:50:37 +10003835 rt = XFS_IS_REALTIME_INODE(ip);
3836 extsz = xfs_get_extsz_hint(ip);
3837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11003840 nimaps = 1;
3841 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3843 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3844
3845 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003846 if (alloc_type != 0 && offset < ip->i_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 (attr_flags&ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003848 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 xfs_off_t end_dmi_offset;
3850
3851 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003852 if (end_dmi_offset > ip->i_size)
3853 end_dmi_offset = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
3855 offset, end_dmi_offset - offset,
3856 0, NULL);
3857 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003858 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 }
3860
3861 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003862 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 */
3864retry:
3865 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003866 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Nathan Scottdd9f4382006-01-11 15:28:28 +11003868 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003869 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11003870 */
3871 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003873 do_div(s, extsz);
3874 s *= extsz;
3875 e = startoffset_fsb + allocatesize_fsb;
3876 if ((temp = do_mod(startoffset_fsb, extsz)))
3877 e += temp;
3878 if ((temp = do_mod(e, extsz)))
3879 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003881 s = 0;
3882 e = allocatesize_fsb;
3883 }
3884
3885 if (unlikely(rt)) {
3886 resrtextents = qblocks = (uint)(e - s);
3887 resrtextents /= mp->m_sb.sb_rextsize;
3888 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3889 quota_flag = XFS_QMOPT_RES_RTBLKS;
3890 } else {
3891 resrtextents = 0;
3892 resblks = qblocks = \
3893 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3894 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 }
3896
3897 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003898 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 */
3900 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003901 error = xfs_trans_reserve(tp, resblks,
3902 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 XFS_TRANS_PERM_LOG_RES,
3904 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003906 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 */
3908 if (error) {
3909 /*
3910 * Free the transaction structure.
3911 */
3912 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3913 xfs_trans_cancel(tp, 0);
3914 break;
3915 }
3916 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003917 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3918 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 if (error)
3920 goto error1;
3921
3922 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3923 xfs_trans_ihold(tp, ip);
3924
3925 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003926 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 */
3928 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003929 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003930 allocatesize_fsb, bmapi_flag,
3931 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003932 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 if (error) {
3934 goto error0;
3935 }
3936
3937 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003938 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003940 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 if (error) {
3942 goto error0;
3943 }
3944
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003945 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3947 if (error) {
3948 break;
3949 }
3950
3951 allocated_fsb = imapp->br_blockcount;
3952
Nathan Scottdd9f4382006-01-11 15:28:28 +11003953 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 error = XFS_ERROR(ENOSPC);
3955 break;
3956 }
3957
3958 startoffset_fsb += allocated_fsb;
3959 allocatesize_fsb -= allocated_fsb;
3960 }
3961dmapi_enospc_check:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003962 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
3963 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
3965 XFS_ITOV(ip), DM_RIGHT_NULL,
3966 XFS_ITOV(ip), DM_RIGHT_NULL,
3967 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3968 if (error == 0)
3969 goto retry; /* Maybe DMAPI app. has made space */
3970 /* else fall through with error from XFS_SEND_DATA */
3971 }
3972
3973 return error;
3974
Nathan Scottdd9f4382006-01-11 15:28:28 +11003975error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003977 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3978
3979error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3981 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3982 goto dmapi_enospc_check;
3983}
3984
3985/*
3986 * Zero file bytes between startoff and endoff inclusive.
3987 * The iolock is held exclusive and no blocks are buffered.
3988 */
3989STATIC int
3990xfs_zero_remaining_bytes(
3991 xfs_inode_t *ip,
3992 xfs_off_t startoff,
3993 xfs_off_t endoff)
3994{
3995 xfs_bmbt_irec_t imap;
3996 xfs_fileoff_t offset_fsb;
3997 xfs_off_t lastoffset;
3998 xfs_off_t offset;
3999 xfs_buf_t *bp;
4000 xfs_mount_t *mp = ip->i_mount;
4001 int nimap;
4002 int error = 0;
4003
4004 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11004005 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 mp->m_rtdev_targp : mp->m_ddev_targp);
4007
4008 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4009 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4010 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004011 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004012 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 if (error || nimap < 1)
4014 break;
4015 ASSERT(imap.br_blockcount >= 1);
4016 ASSERT(imap.br_startoff == offset_fsb);
4017 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4018 if (lastoffset > endoff)
4019 lastoffset = endoff;
4020 if (imap.br_startblock == HOLESTARTBLOCK)
4021 continue;
4022 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4023 if (imap.br_state == XFS_EXT_UNWRITTEN)
4024 continue;
4025 XFS_BUF_UNDONE(bp);
4026 XFS_BUF_UNWRITE(bp);
4027 XFS_BUF_READ(bp);
4028 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4029 xfsbdstrat(mp, bp);
4030 if ((error = xfs_iowait(bp))) {
4031 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4032 mp, bp, XFS_BUF_ADDR(bp));
4033 break;
4034 }
4035 memset(XFS_BUF_PTR(bp) +
4036 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4037 0, lastoffset - offset + 1);
4038 XFS_BUF_UNDONE(bp);
4039 XFS_BUF_UNREAD(bp);
4040 XFS_BUF_WRITE(bp);
4041 xfsbdstrat(mp, bp);
4042 if ((error = xfs_iowait(bp))) {
4043 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4044 mp, bp, XFS_BUF_ADDR(bp));
4045 break;
4046 }
4047 }
4048 xfs_buf_free(bp);
4049 return error;
4050}
4051
4052/*
4053 * xfs_free_file_space()
4054 * This routine frees disk space for the given file.
4055 *
4056 * This routine is only called by xfs_change_file_space
4057 * for an UNRESVSP type call.
4058 *
4059 * RETURNS:
4060 * 0 on success
4061 * errno on error
4062 *
4063 */
4064STATIC int
4065xfs_free_file_space(
4066 xfs_inode_t *ip,
4067 xfs_off_t offset,
4068 xfs_off_t len,
4069 int attr_flags)
4070{
Nathan Scott67fcaa72006-06-09 17:00:52 +10004071 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 int committed;
4073 int done;
4074 xfs_off_t end_dmi_offset;
4075 xfs_fileoff_t endoffset_fsb;
4076 int error;
4077 xfs_fsblock_t firstfsb;
4078 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 xfs_bmbt_irec_t imap;
4080 xfs_off_t ioffset;
4081 xfs_extlen_t mod=0;
4082 xfs_mount_t *mp;
4083 int nimap;
4084 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10004085 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 int rt;
4087 xfs_fileoff_t startoffset_fsb;
4088 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07004089 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004091 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 mp = ip->i_mount;
4093
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11004094 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004095
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4097 return error;
4098
4099 error = 0;
4100 if (len <= 0) /* if nothing being freed */
4101 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11004102 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4104 end_dmi_offset = offset + len;
4105 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4106
Christoph Hellwigeb9df392007-08-16 18:42:07 +10004107 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
4108 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004109 if (end_dmi_offset > ip->i_size)
4110 end_dmi_offset = ip->i_size;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004111 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 offset, end_dmi_offset - offset,
4113 AT_DELAY_FLAG(attr_flags), NULL);
4114 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01004115 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
4117
Dean Roehrich5fcbab32005-05-05 13:27:19 -07004118 if (attr_flags & ATTR_NOLOCK)
4119 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11004120 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10004122 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11004123 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07004124
Tim Shimmine6a4b372007-11-23 16:30:42 +11004125 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004127
4128 if (VN_CACHED(vp) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11004129 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
4130 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10004131 if (error)
4132 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004133 }
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 /*
4136 * Need to zero the stuff we're not freeing, on disk.
4137 * If its a realtime file & can't use unwritten extents then we
4138 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4139 * will take care of it for us.
4140 */
4141 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4142 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004143 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004144 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 if (error)
4146 goto out_unlock_iolock;
4147 ASSERT(nimap == 0 || nimap == 1);
4148 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4149 xfs_daddr_t block;
4150
4151 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4152 block = imap.br_startblock;
4153 mod = do_div(block, mp->m_sb.sb_rextsize);
4154 if (mod)
4155 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4156 }
4157 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004158 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004159 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 if (error)
4161 goto out_unlock_iolock;
4162 ASSERT(nimap == 0 || nimap == 1);
4163 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4164 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4165 mod++;
4166 if (mod && (mod != mp->m_sb.sb_rextsize))
4167 endoffset_fsb -= mod;
4168 }
4169 }
4170 if ((done = (endoffset_fsb <= startoffset_fsb)))
4171 /*
4172 * One contiguous piece to clear
4173 */
4174 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4175 else {
4176 /*
4177 * Some full blocks, possibly two pieces to clear
4178 */
4179 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4180 error = xfs_zero_remaining_bytes(ip, offset,
4181 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4182 if (!error &&
4183 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4184 error = xfs_zero_remaining_bytes(ip,
4185 XFS_FSB_TO_B(mp, endoffset_fsb),
4186 offset + len - 1);
4187 }
4188
4189 /*
4190 * free file space until done or until there is an error
4191 */
4192 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4193 while (!error && !done) {
4194
4195 /*
David Chinner91ebecc2007-07-19 16:28:30 +10004196 * allocate and setup the transaction. Allow this
4197 * transaction to dip into the reserve blocks to ensure
4198 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 */
4200 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10004201 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 error = xfs_trans_reserve(tp,
4203 resblks,
4204 XFS_WRITE_LOG_RES(mp),
4205 0,
4206 XFS_TRANS_PERM_LOG_RES,
4207 XFS_WRITE_LOG_COUNT);
4208
4209 /*
4210 * check for running out of space
4211 */
4212 if (error) {
4213 /*
4214 * Free the transaction structure.
4215 */
4216 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4217 xfs_trans_cancel(tp, 0);
4218 break;
4219 }
4220 xfs_ilock(ip, XFS_ILOCK_EXCL);
4221 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004222 ip->i_udquot, ip->i_gdquot, resblks, 0,
4223 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 if (error)
4225 goto error1;
4226
4227 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4228 xfs_trans_ihold(tp, ip);
4229
4230 /*
4231 * issue the bunmapi() call to free the blocks
4232 */
4233 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004234 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004236 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 if (error) {
4238 goto error0;
4239 }
4240
4241 /*
4242 * complete the transaction
4243 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004244 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 if (error) {
4246 goto error0;
4247 }
4248
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004249 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4251 }
4252
4253 out_unlock_iolock:
4254 if (need_iolock)
4255 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4256 return error;
4257
4258 error0:
4259 xfs_bmap_cancel(&free_list);
4260 error1:
4261 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4262 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4263 XFS_ILOCK_EXCL);
4264 return error;
4265}
4266
4267/*
4268 * xfs_change_file_space()
4269 * This routine allocates or frees disk space for the given file.
4270 * The user specified parameters are checked for alignment and size
4271 * limitations.
4272 *
4273 * RETURNS:
4274 * 0 on success
4275 * errno on error
4276 *
4277 */
4278int
4279xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10004280 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 int cmd,
4282 xfs_flock64_t *bf,
4283 xfs_off_t offset,
4284 cred_t *credp,
4285 int attr_flags)
4286{
Christoph Hellwig993386c2007-08-28 16:12:30 +10004287 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 int clrprealloc;
4289 int error;
4290 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 int setprealloc;
4292 xfs_off_t startoffset;
4293 xfs_off_t llen;
4294 xfs_trans_t *tp;
Nathan Scott8285fb52006-06-09 17:07:12 +10004295 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11004297 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Christoph Hellwig993386c2007-08-28 16:12:30 +10004299 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 return XFS_ERROR(EINVAL);
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 switch (bf->l_whence) {
4303 case 0: /*SEEK_SET*/
4304 break;
4305 case 1: /*SEEK_CUR*/
4306 bf->l_start += offset;
4307 break;
4308 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004309 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 break;
4311 default:
4312 return XFS_ERROR(EINVAL);
4313 }
4314
4315 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4316
4317 if ( (bf->l_start < 0)
4318 || (bf->l_start > XFS_MAXIOFFSET(mp))
4319 || (bf->l_start + llen < 0)
4320 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4321 return XFS_ERROR(EINVAL);
4322
4323 bf->l_whence = 0;
4324
4325 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004326 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 /*
4329 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4330 * file space.
4331 * These calls do NOT zero the data space allocated to the file,
4332 * nor do they change the file size.
4333 *
4334 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4335 * space.
4336 * These calls cause the new file data to be zeroed and the file
4337 * size to be changed.
4338 */
4339 setprealloc = clrprealloc = 0;
4340
4341 switch (cmd) {
4342 case XFS_IOC_RESVSP:
4343 case XFS_IOC_RESVSP64:
4344 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4345 1, attr_flags);
4346 if (error)
4347 return error;
4348 setprealloc = 1;
4349 break;
4350
4351 case XFS_IOC_UNRESVSP:
4352 case XFS_IOC_UNRESVSP64:
4353 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4354 attr_flags)))
4355 return error;
4356 break;
4357
4358 case XFS_IOC_ALLOCSP:
4359 case XFS_IOC_ALLOCSP64:
4360 case XFS_IOC_FREESP:
4361 case XFS_IOC_FREESP64:
4362 if (startoffset > fsize) {
4363 error = xfs_alloc_file_space(ip, fsize,
4364 startoffset - fsize, 0, attr_flags);
4365 if (error)
4366 break;
4367 }
4368
4369 va.va_mask = XFS_AT_SIZE;
4370 va.va_size = startoffset;
4371
Christoph Hellwig993386c2007-08-28 16:12:30 +10004372 error = xfs_setattr(ip, &va, attr_flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374 if (error)
4375 return error;
4376
4377 clrprealloc = 1;
4378 break;
4379
4380 default:
4381 ASSERT(0);
4382 return XFS_ERROR(EINVAL);
4383 }
4384
4385 /*
4386 * update the inode timestamp, mode, and prealloc flag bits
4387 */
4388 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4389
4390 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4391 0, 0, 0))) {
4392 /* ASSERT(0); */
4393 xfs_trans_cancel(tp, 0);
4394 return error;
4395 }
4396
4397 xfs_ilock(ip, XFS_ILOCK_EXCL);
4398
4399 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4400 xfs_trans_ihold(tp, ip);
4401
4402 if ((attr_flags & ATTR_DMI) == 0) {
4403 ip->i_d.di_mode &= ~S_ISUID;
4404
4405 /*
4406 * Note that we don't have to worry about mandatory
4407 * file locking being disabled here because we only
4408 * clear the S_ISGID bit if the Group execute bit is
4409 * on, but if it was on then mandatory locking wouldn't
4410 * have been enabled.
4411 */
4412 if (ip->i_d.di_mode & S_IXGRP)
4413 ip->i_d.di_mode &= ~S_ISGID;
4414
4415 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4416 }
4417 if (setprealloc)
4418 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4419 else if (clrprealloc)
4420 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4421
4422 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4423 xfs_trans_set_sync(tp);
4424
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004425 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
4427 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4428
4429 return error;
4430}