blob: d964e21521ab764a8f520af8db155825345a930e [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
139 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
David Chinnere8c8b3a2005-11-02 10:33:05 +1100140 vap->va_blocksize = xfs_preferred_iosize(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 } else {
142
143 /*
144 * If the file blocks are being allocated from a
145 * realtime partition, then return the inode's
146 * realtime extent size or the realtime volume's
147 * extent size.
148 */
David Chinner957d0eb2007-06-18 16:50:37 +1000149 vap->va_blocksize =
150 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
Nathan Scottca5ccbf2006-01-11 21:03:04 +1100155 vn_atime_to_timespec(vp, &vap->va_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
157 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
158 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
159 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
160
161 /*
162 * Exit for stat callers. See if any of the rest of the fields
163 * to be filled in are needed.
164 */
165 if ((vap->va_mask &
166 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
167 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
168 goto all_done;
169
170 /*
171 * Convert di_flags to xflags.
172 */
173 vap->va_xflags = xfs_ip2xflags(ip);
174
175 /*
176 * Exit for inode revalidate. See if any of the rest of
177 * the fields to be filled in are needed.
178 */
179 if ((vap->va_mask &
180 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
181 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
182 goto all_done;
183
184 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
185 vap->va_nextents =
186 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
187 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
188 ip->i_d.di_nextents;
189 if (ip->i_afp)
190 vap->va_anextents =
191 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
192 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
193 ip->i_d.di_anextents;
194 else
195 vap->va_anextents = 0;
196 vap->va_gen = ip->i_d.di_gen;
197
198 all_done:
199 if (!(flags & ATTR_LAZY))
200 xfs_iunlock(ip, XFS_ILOCK_SHARED);
201 return 0;
202}
203
204
205/*
206 * xfs_setattr
207 */
208int
209xfs_setattr(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000210 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +1000211 bhv_vattr_t *vap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int flags,
213 cred_t *credp)
214{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000215 bhv_vnode_t *vp = XFS_ITOV(ip);
216 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int mask;
219 int code;
220 uint lock_flags;
221 uint commit_flags=0;
222 uid_t uid=0, iuid=0;
223 gid_t gid=0, igid=0;
224 int timeflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 xfs_prid_t projid=0, iprojid=0;
226 int mandlock_before, mandlock_after;
227 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
228 int file_owner;
Dean Roehrich5fcbab32005-05-05 13:27:19 -0700229 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
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) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
512 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
513 code = XFS_ERROR(EINVAL); /* EFBIG? */
514 goto error_return;
515 }
516 /*
517 * Extent size must be a multiple of the appropriate block
518 * size, if set at all.
519 */
520 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
521 xfs_extlen_t size;
522
523 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
524 ((mask & XFS_AT_XFLAGS) &&
525 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
526 size = mp->m_sb.sb_rextsize <<
527 mp->m_sb.sb_blocklog;
528 } else {
529 size = mp->m_sb.sb_blocksize;
530 }
531 if (vap->va_extsize % size) {
532 code = XFS_ERROR(EINVAL);
533 goto error_return;
534 }
535 }
536 /*
537 * If realtime flag is set then must have realtime data.
538 */
539 if ((mask & XFS_AT_XFLAGS) &&
540 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
541 if ((mp->m_sb.sb_rblocks == 0) ||
542 (mp->m_sb.sb_rextsize == 0) ||
543 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
544 code = XFS_ERROR(EINVAL);
545 goto error_return;
546 }
547 }
548
549 /*
550 * Can't modify an immutable/append-only file unless
551 * we have appropriate permission.
552 */
553 if ((mask & XFS_AT_XFLAGS) &&
554 (ip->i_d.di_flags &
555 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
556 (vap->va_xflags &
557 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
558 !capable(CAP_LINUX_IMMUTABLE)) {
559 code = XFS_ERROR(EPERM);
560 goto error_return;
561 }
562 }
563
564 /*
565 * Now we can make the changes. Before we join the inode
566 * to the transaction, if XFS_AT_SIZE is set then take care of
567 * the part of the truncation that must be done without the
568 * inode lock. This needs to be done before joining the inode
569 * to the transaction, because the inode cannot be unlocked
570 * once it is a part of the transaction.
571 */
572 if (mask & XFS_AT_SIZE) {
573 code = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000574 if ((vap->va_size > ip->i_size) &&
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100575 (flags & ATTR_NOSIZETOK) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 code = xfs_igrow_start(ip, vap->va_size, credp);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000579
580 /*
581 * We are going to log the inode size change in this
582 * transaction so any previous writes that are beyond the on
583 * disk EOF and the new EOF that have not been written out need
584 * to be written here. If we do not write the data out, we
585 * expose ourselves to the null files problem.
586 *
587 * Only flush from the on disk size to the smaller of the in
588 * memory file size or the new size as that's the range we
589 * really care about here and prevents waiting for other data
590 * not within the range we care about here.
591 */
592 if (!code &&
593 (ip->i_size != ip->i_d.di_size) &&
594 (vap->va_size > ip->i_d.di_size)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000595 code = xfs_flush_pages(ip,
David Chinnerc32676e2007-07-19 16:28:58 +1000596 ip->i_d.di_size, vap->va_size,
597 XFS_B_ASYNC, FI_NONE);
598 }
599
600 /* wait for all I/O to complete */
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
901
902/*
903 * xfs_access
904 * Null conversion from vnode mode bits to inode mode bits, as in efs.
905 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000906int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907xfs_access(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000908 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int mode,
910 cred_t *credp)
911{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int error;
913
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100914 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 xfs_ilock(ip, XFS_ILOCK_SHARED);
916 error = xfs_iaccess(ip, mode, credp);
917 xfs_iunlock(ip, XFS_ILOCK_SHARED);
918 return error;
919}
920
921
922/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000923 * The maximum pathlen is 1024 bytes. Since the minimum file system
924 * blocksize is 512 bytes, we can get a max of 2 extents back from
925 * bmapi.
926 */
927#define SYMLINK_MAPS 2
928
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000929STATIC int
930xfs_readlink_bmap(
931 xfs_inode_t *ip,
932 char *link)
933{
934 xfs_mount_t *mp = ip->i_mount;
935 int pathlen = ip->i_d.di_size;
936 int nmaps = SYMLINK_MAPS;
937 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
938 xfs_daddr_t d;
939 int byte_cnt;
940 int n;
941 xfs_buf_t *bp;
942 int error = 0;
943
944 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
945 mval, &nmaps, NULL, NULL);
946 if (error)
947 goto out;
948
949 for (n = 0; n < nmaps; n++) {
950 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
951 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
952
953 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
954 error = XFS_BUF_GETERROR(bp);
955 if (error) {
956 xfs_ioerror_alert("xfs_readlink",
957 ip->i_mount, bp, XFS_BUF_ADDR(bp));
958 xfs_buf_relse(bp);
959 goto out;
960 }
961 if (pathlen < byte_cnt)
962 byte_cnt = pathlen;
963 pathlen -= byte_cnt;
964
965 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
966 xfs_buf_relse(bp);
967 }
968
969 link[ip->i_d.di_size] = '\0';
970 error = 0;
971
972 out:
973 return error;
974}
975
Christoph Hellwig993386c2007-08-28 16:12:30 +1000976int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000978 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000979 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000981 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100985 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 if (XFS_FORCED_SHUTDOWN(mp))
988 return XFS_ERROR(EIO);
989
990 xfs_ilock(ip, XFS_ILOCK_SHARED);
991
992 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000993 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000995 pathlen = ip->i_d.di_size;
996 if (!pathlen)
997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +10001000 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
1001 link[pathlen] = '\0';
1002 } else {
1003 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
Christoph Hellwig804c83c2007-08-28 13:59:03 +10001006 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return error;
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/*
1012 * xfs_fsync
1013 *
1014 * This is called to sync the inode and its data out to disk.
1015 * We need to hold the I/O lock while flushing the data, and
1016 * the inode lock while flushing the inode. The inode lock CANNOT
1017 * be held while flushing the data, so acquire after we're done
1018 * with that.
1019 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001020int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021xfs_fsync(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001022 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 int flag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 xfs_off_t start,
1025 xfs_off_t stop)
1026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 xfs_trans_t *tp;
1028 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001029 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001031 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 ASSERT(start >= 0 && stop >= -1);
1034
1035 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1036 return XFS_ERROR(EIO);
1037
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +10001038 if (flag & FSYNC_DATA)
1039 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * We always need to make sure that the required inode state
1043 * is safe on disk. The vnode might be clean but because
1044 * of committed transactions that haven't hit the disk yet.
1045 * Likewise, there could be unflushed non-transactional
1046 * changes to the inode core that have to go to disk.
1047 *
1048 * The following code depends on one assumption: that
1049 * any transaction that changes an inode logs the core
1050 * because it has to change some field in the inode core
1051 * (typically nextents or nblocks). That assumption
1052 * implies that any transactions against an inode will
1053 * catch any non-transactional updates. If inode-altering
1054 * transactions exist that violate this assumption, the
1055 * code breaks. Right now, it figures that if the involved
1056 * update_* field is clear and the inode is unpinned, the
1057 * inode is clean. Either it's been flushed or it's been
1058 * committed and the commit has hit the disk unpinning the inode.
1059 * (Note that xfs_inode_item_format() called at commit clears
1060 * the update_* fields.)
1061 */
1062 xfs_ilock(ip, XFS_ILOCK_SHARED);
1063
1064 /* If we are flushing data then we care about update_size
1065 * being set, otherwise we care about update_core
1066 */
1067 if ((flag & FSYNC_DATA) ?
1068 (ip->i_update_size == 0) :
1069 (ip->i_update_core == 0)) {
1070 /*
1071 * Timestamps/size haven't changed since last inode
1072 * flush or inode transaction commit. That means
1073 * either nothing got written or a transaction
1074 * committed which caught the updates. If the
1075 * latter happened and the transaction hasn't
1076 * hit the disk yet, the inode will be still
1077 * be pinned. If it is, force the log.
1078 */
1079
1080 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1081
1082 if (xfs_ipincount(ip)) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001083 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 XFS_LOG_FORCE |
1085 ((flag & FSYNC_WAIT)
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001086 ? XFS_LOG_SYNC : 0),
1087 &log_flushed);
1088 } else {
1089 /*
1090 * If the inode is not pinned and nothing
1091 * has changed we don't need to flush the
1092 * cache.
1093 */
1094 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 error = 0;
1097 } else {
1098 /*
1099 * Kick off a transaction to log the inode
1100 * core to get the updates. Make it
1101 * sync if FSYNC_WAIT is passed in (which
1102 * is done by everybody but specfs). The
1103 * sync transaction will also force the log.
1104 */
1105 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1106 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1107 if ((error = xfs_trans_reserve(tp, 0,
1108 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1109 0, 0, 0))) {
1110 xfs_trans_cancel(tp, 0);
1111 return error;
1112 }
1113 xfs_ilock(ip, XFS_ILOCK_EXCL);
1114
1115 /*
1116 * Note - it's possible that we might have pushed
1117 * ourselves out of the way during trans_reserve
1118 * which would flush the inode. But there's no
1119 * guarantee that the inode buffer has actually
1120 * gone out yet (it's delwri). Plus the buffer
1121 * could be pinned anyway if it's part of an
1122 * inode in another recent transaction. So we
1123 * play it safe and fire off the transaction anyway.
1124 */
1125 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1126 xfs_trans_ihold(tp, ip);
1127 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1128 if (flag & FSYNC_WAIT)
1129 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001130 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1133 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001134
1135 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1136 /*
1137 * If the log write didn't issue an ordered tag we need
1138 * to flush the disk cache for the data device now.
1139 */
1140 if (!log_flushed)
1141 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1142
1143 /*
1144 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +10001145 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001146 */
1147 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1148 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1149 }
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return error;
1152}
1153
1154/*
David Chinner92dfe8d2007-05-24 15:22:19 +10001155 * This is called by xfs_inactive to free any blocks beyond eof
1156 * when the link count isn't zero and by xfs_dm_punch_hole() when
1157 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001159int
1160xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +10001162 xfs_inode_t *ip,
1163 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 xfs_trans_t *tp;
1166 int error;
1167 xfs_fileoff_t end_fsb;
1168 xfs_fileoff_t last_fsb;
1169 xfs_filblks_t map_len;
1170 int nimaps;
1171 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +10001172 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /*
1175 * Figure out if there are any blocks beyond the end
1176 * of the file. If not, then there is nothing to do.
1177 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001178 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1180 map_len = last_fsb - end_fsb;
1181 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 nimaps = 1;
1185 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001186 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001187 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1189
1190 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001191 (imap.br_startblock != HOLESTARTBLOCK ||
1192 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /*
1194 * Attach the dquots to the inode up front.
1195 */
1196 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001197 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /*
1200 * There are blocks after the end of file.
1201 * Free them up now by truncating the file to
1202 * its current size.
1203 */
1204 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1205
1206 /*
1207 * Do the xfs_itruncate_start() call before
1208 * reserving any log space because
1209 * itruncate_start will call into the buffer
1210 * cache and we can't
1211 * do that within a transaction.
1212 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001213 if (use_iolock)
1214 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001215 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001216 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001217 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001218 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001219 if (use_iolock)
1220 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001221 return error;
1222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 error = xfs_trans_reserve(tp, 0,
1225 XFS_ITRUNCATE_LOG_RES(mp),
1226 0, XFS_TRANS_PERM_LOG_RES,
1227 XFS_ITRUNCATE_LOG_COUNT);
1228 if (error) {
1229 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1230 xfs_trans_cancel(tp, 0);
1231 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001232 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 xfs_ilock(ip, XFS_ILOCK_EXCL);
1236 xfs_trans_ijoin(tp, ip,
1237 XFS_IOLOCK_EXCL |
1238 XFS_ILOCK_EXCL);
1239 xfs_trans_ihold(tp, ip);
1240
1241 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001242 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 XFS_DATA_FORK,
1244 0);
1245 /*
1246 * If we get an error at this point we
1247 * simply don't bother truncating the file.
1248 */
1249 if (error) {
1250 xfs_trans_cancel(tp,
1251 (XFS_TRANS_RELEASE_LOG_RES |
1252 XFS_TRANS_ABORT));
1253 } else {
1254 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001255 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
David Chinner92dfe8d2007-05-24 15:22:19 +10001257 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1258 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001260 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263/*
1264 * Free a symlink that has blocks associated with it.
1265 */
1266STATIC int
1267xfs_inactive_symlink_rmt(
1268 xfs_inode_t *ip,
1269 xfs_trans_t **tpp)
1270{
1271 xfs_buf_t *bp;
1272 int committed;
1273 int done;
1274 int error;
1275 xfs_fsblock_t first_block;
1276 xfs_bmap_free_t free_list;
1277 int i;
1278 xfs_mount_t *mp;
1279 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1280 int nmaps;
1281 xfs_trans_t *ntp;
1282 int size;
1283 xfs_trans_t *tp;
1284
1285 tp = *tpp;
1286 mp = ip->i_mount;
1287 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1288 /*
1289 * We're freeing a symlink that has some
1290 * blocks allocated to it. Free the
1291 * blocks here. We know that we've got
1292 * either 1 or 2 extents and that we can
1293 * free them all in one bunmapi call.
1294 */
1295 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1296 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1297 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1298 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1299 xfs_trans_cancel(tp, 0);
1300 *tpp = NULL;
1301 return error;
1302 }
1303 /*
1304 * Lock the inode, fix the size, and join it to the transaction.
1305 * Hold it so in the normal path, we still have it locked for
1306 * the second transaction. In the error paths we need it
1307 * held so the cancel won't rele it, see below.
1308 */
1309 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1310 size = (int)ip->i_d.di_size;
1311 ip->i_d.di_size = 0;
1312 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1313 xfs_trans_ihold(tp, ip);
1314 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1315 /*
1316 * Find the block(s) so we can inval and unmap them.
1317 */
1318 done = 0;
1319 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -08001320 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1322 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001323 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 goto error0;
1325 /*
1326 * Invalidate the block(s).
1327 */
1328 for (i = 0; i < nmaps; i++) {
1329 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1330 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1331 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1332 xfs_trans_binval(tp, bp);
1333 }
1334 /*
1335 * Unmap the dead block(s) to the free_list.
1336 */
1337 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001338 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto error1;
1340 ASSERT(done);
1341 /*
1342 * Commit the first transaction. This logs the EFI and the inode.
1343 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001344 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 goto error1;
1346 /*
1347 * The transaction must have been committed, since there were
1348 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1349 * The new tp has the extent freeing and EFDs.
1350 */
1351 ASSERT(committed);
1352 /*
1353 * The first xact was committed, so add the inode to the new one.
1354 * Mark it dirty so it will be logged and moved forward in the log as
1355 * part of every commit.
1356 */
1357 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1358 xfs_trans_ihold(tp, ip);
1359 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1360 /*
1361 * Get a new, empty transaction to return to our caller.
1362 */
1363 ntp = xfs_trans_dup(tp);
1364 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001365 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 * If we get an error on the commit here or on the reserve below,
1367 * we need to unlock the inode since the new transaction doesn't
1368 * have the inode attached.
1369 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001370 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 tp = ntp;
1372 if (error) {
1373 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1374 goto error0;
1375 }
1376 /*
1377 * Remove the memory for extent descriptions (just bookkeeping).
1378 */
1379 if (ip->i_df.if_bytes)
1380 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1381 ASSERT(ip->i_df.if_bytes == 0);
1382 /*
1383 * Put an itruncate log reservation in the new transaction
1384 * for our caller.
1385 */
1386 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1387 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1388 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1389 goto error0;
1390 }
1391 /*
1392 * Return with the inode locked but not joined to the transaction.
1393 */
1394 *tpp = tp;
1395 return 0;
1396
1397 error1:
1398 xfs_bmap_cancel(&free_list);
1399 error0:
1400 /*
1401 * Have to come here with the inode locked and either
1402 * (held and in the transaction) or (not in the transaction).
1403 * If the inode isn't held then cancel would iput it, but
1404 * that's wrong since this is inactive and the vnode ref
1405 * count is 0 already.
1406 * Cancel won't do anything to the inode if held, but it still
1407 * needs to be locked until the cancel is done, if it was
1408 * joined to the transaction.
1409 */
1410 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1411 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1412 *tpp = NULL;
1413 return error;
1414
1415}
1416
1417STATIC int
1418xfs_inactive_symlink_local(
1419 xfs_inode_t *ip,
1420 xfs_trans_t **tpp)
1421{
1422 int error;
1423
1424 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1425 /*
1426 * We're freeing a symlink which fit into
1427 * the inode. Just free the memory used
1428 * to hold the old symlink.
1429 */
1430 error = xfs_trans_reserve(*tpp, 0,
1431 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1432 0, XFS_TRANS_PERM_LOG_RES,
1433 XFS_ITRUNCATE_LOG_COUNT);
1434
1435 if (error) {
1436 xfs_trans_cancel(*tpp, 0);
1437 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001438 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1441
1442 /*
1443 * Zero length symlinks _can_ exist.
1444 */
1445 if (ip->i_df.if_bytes > 0) {
1446 xfs_idata_realloc(ip,
1447 -(ip->i_df.if_bytes),
1448 XFS_DATA_FORK);
1449 ASSERT(ip->i_df.if_bytes == 0);
1450 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454STATIC int
1455xfs_inactive_attrs(
1456 xfs_inode_t *ip,
1457 xfs_trans_t **tpp)
1458{
1459 xfs_trans_t *tp;
1460 int error;
1461 xfs_mount_t *mp;
1462
1463 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1464 tp = *tpp;
1465 mp = ip->i_mount;
1466 ASSERT(ip->i_d.di_forkoff != 0);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001467 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1469
1470 error = xfs_attr_inactive(ip);
1471 if (error) {
1472 *tpp = NULL;
1473 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001474 return error; /* goto out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476
1477 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1478 error = xfs_trans_reserve(tp, 0,
1479 XFS_IFREE_LOG_RES(mp),
1480 0, XFS_TRANS_PERM_LOG_RES,
1481 XFS_INACTIVE_LOG_COUNT);
1482 if (error) {
1483 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1484 xfs_trans_cancel(tp, 0);
1485 *tpp = NULL;
1486 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001487 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489
1490 xfs_ilock(ip, XFS_ILOCK_EXCL);
1491 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1492 xfs_trans_ihold(tp, ip);
1493 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1494
1495 ASSERT(ip->i_d.di_anextents == 0);
1496
1497 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
Christoph Hellwig993386c2007-08-28 16:12:30 +10001501int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001503 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001505 bhv_vnode_t *vp = XFS_ITOV(ip);
1506 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int error;
1508
Nathan Scott7d4fb402006-06-09 15:27:16 +10001509 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001513 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return 0;
1515
David Chinner2a82b8b2007-07-11 11:09:12 +10001516 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001517 int truncated;
1518
David Chinner2a82b8b2007-07-11 11:09:12 +10001519 /*
1520 * If we are using filestreams, and we have an unlinked
1521 * file that we are processing the last close on, then nothing
1522 * will be able to reopen and write to this file. Purge this
1523 * inode from the filestreams cache so that it doesn't delay
1524 * teardown of the inode.
1525 */
1526 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1527 xfs_filestream_deassociate(ip);
1528
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001529 /*
1530 * If we previously truncated this file and removed old data
1531 * in the process, we want to initiate "early" writeout on
1532 * the last close. This is an attempt to combat the notorious
1533 * NULL files problem which is particularly noticable from a
1534 * truncate down, buffered (re-)write (delalloc), followed by
1535 * a crash. What we are effectively doing here is
1536 * significantly reducing the time window where we'd otherwise
1537 * be exposed to that problem.
1538 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001539 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001540 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001541 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001542 }
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544#ifdef HAVE_REFCACHE
1545 /* If we are in the NFS reference cache then don't do this now */
1546 if (ip->i_refcache)
1547 return 0;
1548#endif
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (ip->i_d.di_nlink != 0) {
1551 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001552 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001553 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001555 (!(ip->i_d.di_flags &
1556 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001557 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1558 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001559 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* Update linux inode block count after free above */
Nathan Scottec86dc02006-03-17 17:25:36 +11001561 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 ip->i_d.di_nblocks + ip->i_delayed_blks);
1563 }
1564 }
1565
1566 return 0;
1567}
1568
1569/*
1570 * xfs_inactive
1571 *
1572 * This is called when the vnode reference count for the vnode
1573 * goes to zero. If the file has been unlinked, then it must
1574 * now be truncated. Also, we clear all of the read-ahead state
1575 * kept for the inode here since the file is now closed.
1576 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001577int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001579 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001581 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +10001582 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 xfs_fsblock_t first_block;
1584 int committed;
1585 xfs_trans_t *tp;
1586 xfs_mount_t *mp;
1587 int error;
1588 int truncate;
1589
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001590 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /*
1593 * If the inode is already free, then there can be nothing
1594 * to clean up here.
1595 */
1596 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1597 ASSERT(ip->i_df.if_real_bytes == 0);
1598 ASSERT(ip->i_df.if_broot_bytes == 0);
1599 return VN_INACTIVE_CACHE;
1600 }
1601
1602 /*
1603 * Only do a truncate if it's a regular file with
1604 * some actual space in it. It's OK to look at the
1605 * inode's fields without the lock because we're the
1606 * only one with a reference to the inode.
1607 */
1608 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001609 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1610 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1612
1613 mp = ip->i_mount;
1614
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001615 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1617 }
1618
1619 error = 0;
1620
1621 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001622 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto out;
1624
1625 if (ip->i_d.di_nlink != 0) {
1626 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001627 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001628 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001629 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1630 (!(ip->i_d.di_flags &
1631 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1632 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001633 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1634 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001635 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* Update linux inode block count after free above */
Nathan Scottec86dc02006-03-17 17:25:36 +11001637 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 ip->i_d.di_nblocks + ip->i_delayed_blks);
1639 }
1640 goto out;
1641 }
1642
1643 ASSERT(ip->i_d.di_nlink == 0);
1644
1645 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001646 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1649 if (truncate) {
1650 /*
1651 * Do the xfs_itruncate_start() call before
1652 * reserving any log space because itruncate_start
1653 * will call into the buffer cache and we can't
1654 * do that within a transaction.
1655 */
1656 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1657
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001658 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1659 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001660 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001661 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1662 return VN_INACTIVE_CACHE;
1663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 error = xfs_trans_reserve(tp, 0,
1666 XFS_ITRUNCATE_LOG_RES(mp),
1667 0, XFS_TRANS_PERM_LOG_RES,
1668 XFS_ITRUNCATE_LOG_COUNT);
1669 if (error) {
1670 /* Don't call itruncate_cleanup */
1671 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1672 xfs_trans_cancel(tp, 0);
1673 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001674 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
1677 xfs_ilock(ip, XFS_ILOCK_EXCL);
1678 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1679 xfs_trans_ihold(tp, ip);
1680
1681 /*
1682 * normally, we have to run xfs_itruncate_finish sync.
1683 * But if filesystem is wsync and we're in the inactive
1684 * path, then we know that nlink == 0, and that the
1685 * xaction that made nlink == 0 is permanently committed
1686 * since xfs_remove runs as a synchronous transaction.
1687 */
1688 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1689 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1690
1691 if (error) {
1692 xfs_trans_cancel(tp,
1693 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1694 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001695 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1698
1699 /*
1700 * If we get an error while cleaning up a
1701 * symlink we bail out.
1702 */
1703 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1704 xfs_inactive_symlink_rmt(ip, &tp) :
1705 xfs_inactive_symlink_local(ip, &tp);
1706
1707 if (error) {
1708 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001709 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
1712 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1713 xfs_trans_ihold(tp, ip);
1714 } else {
1715 error = xfs_trans_reserve(tp, 0,
1716 XFS_IFREE_LOG_RES(mp),
1717 0, XFS_TRANS_PERM_LOG_RES,
1718 XFS_INACTIVE_LOG_COUNT);
1719 if (error) {
1720 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1721 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001722 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724
1725 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1726 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1727 xfs_trans_ihold(tp, ip);
1728 }
1729
1730 /*
1731 * If there are attributes associated with the file
1732 * then blow them away now. The code calls a routine
1733 * that recursively deconstructs the attribute fork.
1734 * We need to just commit the current transaction
1735 * because we can't use it for xfs_attr_inactive().
1736 */
1737 if (ip->i_d.di_anextents > 0) {
1738 error = xfs_inactive_attrs(ip, &tp);
1739 /*
1740 * If we got an error, the transaction is already
1741 * cancelled, and the inode is unlocked. Just get out.
1742 */
1743 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001744 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 } else if (ip->i_afp) {
1746 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1747 }
1748
1749 /*
1750 * Free the inode.
1751 */
1752 XFS_BMAP_INIT(&free_list, &first_block);
1753 error = xfs_ifree(tp, ip, &free_list);
1754 if (error) {
1755 /*
1756 * If we fail to free the inode, shut down. The cancel
1757 * might do that, we need to make sure. Otherwise the
1758 * inode might be lost for a long time or forever.
1759 */
1760 if (!XFS_FORCED_SHUTDOWN(mp)) {
1761 cmn_err(CE_NOTE,
1762 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1763 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001764 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1767 } else {
1768 /*
1769 * Credit the quota account(s). The inode is gone.
1770 */
1771 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1772
1773 /*
1774 * Just ignore errors at this point. There is
1775 * nothing we can do except to try to keep going.
1776 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001777 (void) xfs_bmap_finish(&tp, &free_list, &committed);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001778 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780 /*
1781 * Release the dquots held by inode, if any.
1782 */
1783 XFS_QM_DQDETACH(mp, ip);
1784
1785 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1786
1787 out:
1788 return VN_INACTIVE_CACHE;
1789}
1790
1791
Christoph Hellwig993386c2007-08-28 16:12:30 +10001792int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001794 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10001795 bhv_vname_t *dentry,
Christoph Hellwig993386c2007-08-28 16:12:30 +10001796 bhv_vnode_t **vpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001798 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 xfs_ino_t e_inum;
1800 int error;
1801 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001803 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1806 return XFS_ERROR(EIO);
1807
1808 lock_mode = xfs_ilock_map_shared(dp);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001809 error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (!error) {
1811 *vpp = XFS_ITOV(ip);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001812 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814 xfs_iunlock_map_shared(dp, lock_mode);
1815 return error;
1816}
1817
Christoph Hellwig993386c2007-08-28 16:12:30 +10001818int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001820 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10001821 bhv_vname_t *dentry,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001822 mode_t mode,
1823 xfs_dev_t rdev,
Nathan Scott67fcaa72006-06-09 17:00:52 +10001824 bhv_vnode_t **vpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 cred_t *credp)
1826{
1827 char *name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001828 xfs_mount_t *mp = dp->i_mount;
1829 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
1830 xfs_inode_t *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +10001831 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 int error;
1834 xfs_bmap_free_t free_list;
1835 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001836 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 int dm_event_sent = 0;
1838 uint cancel_flags;
1839 int committed;
1840 xfs_prid_t prid;
1841 struct xfs_dquot *udqp, *gdqp;
1842 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 int namelen;
1844
1845 ASSERT(!*vpp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001846 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 namelen = VNAMELEN(dentry);
1849
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001850 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1852 dir_vp, DM_RIGHT_NULL, NULL,
1853 DM_RIGHT_NULL, name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001854 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 if (error)
1857 return error;
1858 dm_event_sent = 1;
1859 }
1860
1861 if (XFS_FORCED_SHUTDOWN(mp))
1862 return XFS_ERROR(EIO);
1863
1864 /* Return through std_return after this point. */
1865
1866 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001867 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1868 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 else
1870 prid = (xfs_prid_t)dfltprid;
1871
1872 /*
1873 * Make sure that we have allocated dquot(s) on disk.
1874 */
1875 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001876 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1878 if (error)
1879 goto std_return;
1880
1881 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1884 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1885 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1886 /*
1887 * Initially assume that the file does not exist and
1888 * reserve the resources for that case. If that is not
1889 * the case we'll drop the one we have and get a more
1890 * appropriate transaction later.
1891 */
1892 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1893 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1894 if (error == ENOSPC) {
1895 resblks = 0;
1896 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1897 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1898 }
1899 if (error) {
1900 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 goto error_return;
1902 }
1903
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001904 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001905 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 XFS_BMAP_INIT(&free_list, &first_block);
1908
1909 ASSERT(ip == NULL);
1910
1911 /*
1912 * Reserve disk quota and the inode.
1913 */
1914 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1915 if (error)
1916 goto error_return;
1917
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001918 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001920 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 rdev, credp, prid, resblks > 0,
1922 &ip, &committed);
1923 if (error) {
1924 if (error == ENOSPC)
1925 goto error_return;
1926 goto abort_return;
1927 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001928 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /*
1931 * At this point, we've gotten a newly allocated inode.
1932 * It is locked (and joined to the transaction).
1933 */
1934
1935 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1936
1937 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001938 * Now we join the directory inode to the transaction. We do not do it
1939 * earlier because xfs_dir_ialloc might commit the previous transaction
1940 * (and release all the locks). An error from here on will result in
1941 * the transaction cancel unlocking dp so don't do it explicitly in the
1942 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 VN_HOLD(dir_vp);
1945 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001946 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001948 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
1949 &first_block, &free_list, resblks ?
1950 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (error) {
1952 ASSERT(error != ENOSPC);
1953 goto abort_return;
1954 }
1955 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1956 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1957
1958 /*
1959 * If this is a synchronous mount, make sure that the
1960 * create transaction goes to disk before returning to
1961 * the user.
1962 */
1963 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1964 xfs_trans_set_sync(tp);
1965 }
1966
1967 dp->i_gen++;
1968
1969 /*
1970 * Attach the dquot(s) to the inodes and modify them incore.
1971 * These ids of the inode couldn't have changed since the new
1972 * inode has been locked ever since it was created.
1973 */
1974 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1975
1976 /*
1977 * xfs_trans_commit normally decrements the vnode ref count
1978 * when it unlocks the inode. Since we want to return the
1979 * vnode to the caller, we bump the vnode ref count now.
1980 */
1981 IHOLD(ip);
1982 vp = XFS_ITOV(ip);
1983
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001984 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (error) {
1986 xfs_bmap_cancel(&free_list);
1987 goto abort_rele;
1988 }
1989
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001990 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (error) {
1992 IRELE(ip);
1993 tp = NULL;
1994 goto error_return;
1995 }
1996
1997 XFS_QM_DQRELE(mp, udqp);
1998 XFS_QM_DQRELE(mp, gdqp);
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 *vpp = vp;
2001
2002 /* Fallthrough to std_return with error = 0 */
2003
2004std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002005 if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002006 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2008 dir_vp, DM_RIGHT_NULL,
2009 *vpp ? vp:NULL,
2010 DM_RIGHT_NULL, name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002011 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013 return error;
2014
2015 abort_return:
2016 cancel_flags |= XFS_TRANS_ABORT;
2017 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Jesper Juhl014c2542006-01-15 02:37:08 +01002019 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (tp != NULL)
2021 xfs_trans_cancel(tp, cancel_flags);
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 XFS_QM_DQRELE(mp, udqp);
2024 XFS_QM_DQRELE(mp, gdqp);
2025
Christoph Hellwig993386c2007-08-28 16:12:30 +10002026 if (unlock_dp_on_error)
2027 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 goto std_return;
2030
2031 abort_rele:
2032 /*
2033 * Wait until after the current transaction is aborted to
2034 * release the inode. This prevents recursive transactions
2035 * and deadlocks from xfs_inactive.
2036 */
2037 cancel_flags |= XFS_TRANS_ABORT;
2038 xfs_trans_cancel(tp, cancel_flags);
2039 IRELE(ip);
2040
2041 XFS_QM_DQRELE(mp, udqp);
2042 XFS_QM_DQRELE(mp, gdqp);
2043
2044 goto std_return;
2045}
2046
2047#ifdef DEBUG
2048/*
2049 * Some counters to see if (and how often) we are hitting some deadlock
2050 * prevention code paths.
2051 */
2052
2053int xfs_rm_locks;
2054int xfs_rm_lock_delays;
2055int xfs_rm_attempts;
2056#endif
2057
2058/*
2059 * The following routine will lock the inodes associated with the
2060 * directory and the named entry in the directory. The locks are
2061 * acquired in increasing inode number.
2062 *
2063 * If the entry is "..", then only the directory is locked. The
2064 * vnode ref count will still include that from the .. entry in
2065 * this case.
2066 *
2067 * There is a deadlock we need to worry about. If the locked directory is
2068 * in the AIL, it might be blocking up the log. The next inode we lock
2069 * could be already locked by another thread waiting for log space (e.g
2070 * a permanent log reservation with a long running transaction (see
2071 * xfs_itruncate_finish)). To solve this, we must check if the directory
2072 * is in the ail and use lock_nowait. If we can't lock, we need to
2073 * drop the inode lock on the directory and try again. xfs_iunlock will
2074 * potentially push the tail if we were holding up the log.
2075 */
2076STATIC int
2077xfs_lock_dir_and_entry(
2078 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 xfs_inode_t *ip) /* inode of entry 'name' */
2080{
2081 int attempts;
2082 xfs_ino_t e_inum;
2083 xfs_inode_t *ips[2];
2084 xfs_log_item_t *lp;
2085
2086#ifdef DEBUG
2087 xfs_rm_locks++;
2088#endif
2089 attempts = 0;
2090
2091again:
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002092 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 e_inum = ip->i_ino;
2095
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002096 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 /*
2099 * We want to lock in increasing inum. Since we've already
2100 * acquired the lock on the directory, we may need to release
2101 * if if the inum of the entry turns out to be less.
2102 */
2103 if (e_inum > dp->i_ino) {
2104 /*
2105 * We are already in the right order, so just
2106 * lock on the inode of the entry.
2107 * We need to use nowait if dp is in the AIL.
2108 */
2109
2110 lp = (xfs_log_item_t *)dp->i_itemp;
2111 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2112 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2113 attempts++;
2114#ifdef DEBUG
2115 xfs_rm_attempts++;
2116#endif
2117
2118 /*
2119 * Unlock dp and try again.
2120 * xfs_iunlock will try to push the tail
2121 * if the inode is in the AIL.
2122 */
2123
2124 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2125
2126 if ((attempts % 5) == 0) {
2127 delay(1); /* Don't just spin the CPU */
2128#ifdef DEBUG
2129 xfs_rm_lock_delays++;
2130#endif
2131 }
2132 goto again;
2133 }
2134 } else {
2135 xfs_ilock(ip, XFS_ILOCK_EXCL);
2136 }
2137 } else if (e_inum < dp->i_ino) {
2138 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2139
2140 ips[0] = ip;
2141 ips[1] = dp;
2142 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2143 }
2144 /* else e_inum == dp->i_ino */
2145 /* This can happen if we're asked to lock /x/..
2146 * the entry is "..", which is also the parent directory.
2147 */
2148
2149 return 0;
2150}
2151
2152#ifdef DEBUG
2153int xfs_locked_n;
2154int xfs_small_retries;
2155int xfs_middle_retries;
2156int xfs_lots_retries;
2157int xfs_lock_delays;
2158#endif
2159
2160/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002161 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2162 * a different value
2163 */
2164static inline int
2165xfs_lock_inumorder(int lock_mode, int subclass)
2166{
2167 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002168 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002169 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002170 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002171
2172 return lock_mode;
2173}
2174
2175/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 * The following routine will lock n inodes in exclusive mode.
2177 * We assume the caller calls us with the inodes in i_ino order.
2178 *
2179 * We need to detect deadlock where an inode that we lock
2180 * is in the AIL and we start waiting for another inode that is locked
2181 * by a thread in a long running transaction (such as truncate). This can
2182 * result in deadlock since the long running trans might need to wait
2183 * for the inode we just locked in order to push the tail and free space
2184 * in the log.
2185 */
2186void
2187xfs_lock_inodes(
2188 xfs_inode_t **ips,
2189 int inodes,
2190 int first_locked,
2191 uint lock_mode)
2192{
2193 int attempts = 0, i, j, try_lock;
2194 xfs_log_item_t *lp;
2195
2196 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2197
2198 if (first_locked) {
2199 try_lock = 1;
2200 i = 1;
2201 } else {
2202 try_lock = 0;
2203 i = 0;
2204 }
2205
2206again:
2207 for (; i < inodes; i++) {
2208 ASSERT(ips[i]);
2209
2210 if (i && (ips[i] == ips[i-1])) /* Already locked */
2211 continue;
2212
2213 /*
2214 * If try_lock is not set yet, make sure all locked inodes
2215 * are not in the AIL.
2216 * If any are, set try_lock to be used later.
2217 */
2218
2219 if (!try_lock) {
2220 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2221 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2222 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2223 try_lock++;
2224 }
2225 }
2226 }
2227
2228 /*
2229 * If any of the previous locks we have locked is in the AIL,
2230 * we must TRY to get the second and subsequent locks. If
2231 * we can't get any, we must release all we have
2232 * and try again.
2233 */
2234
2235 if (try_lock) {
2236 /* try_lock must be 0 if i is 0. */
2237 /*
2238 * try_lock means we have an inode locked
2239 * that is in the AIL.
2240 */
2241 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002242 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 attempts++;
2244
2245 /*
2246 * Unlock all previous guys and try again.
2247 * xfs_iunlock will try to push the tail
2248 * if the inode is in the AIL.
2249 */
2250
2251 for(j = i - 1; j >= 0; j--) {
2252
2253 /*
2254 * Check to see if we've already
2255 * unlocked this one.
2256 * Not the first one going back,
2257 * and the inode ptr is the same.
2258 */
2259 if ((j != (i - 1)) && ips[j] ==
2260 ips[j+1])
2261 continue;
2262
2263 xfs_iunlock(ips[j], lock_mode);
2264 }
2265
2266 if ((attempts % 5) == 0) {
2267 delay(1); /* Don't just spin the CPU */
2268#ifdef DEBUG
2269 xfs_lock_delays++;
2270#endif
2271 }
2272 i = 0;
2273 try_lock = 0;
2274 goto again;
2275 }
2276 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002277 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
2279 }
2280
2281#ifdef DEBUG
2282 if (attempts) {
2283 if (attempts < 5) xfs_small_retries++;
2284 else if (attempts < 100) xfs_middle_retries++;
2285 else xfs_lots_retries++;
2286 } else {
2287 xfs_locked_n++;
2288 }
2289#endif
2290}
2291
2292#ifdef DEBUG
2293#define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2294int remove_which_error_return = 0;
2295#else /* ! DEBUG */
2296#define REMOVE_DEBUG_TRACE(x)
2297#endif /* ! DEBUG */
2298
Christoph Hellwig993386c2007-08-28 16:12:30 +10002299int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002301 xfs_inode_t *dp,
2302 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002304 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 char *name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002306 xfs_mount_t *mp = dp->i_mount;
2307 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 xfs_trans_t *tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 int error = 0;
2310 xfs_bmap_free_t free_list;
2311 xfs_fsblock_t first_block;
2312 int cancel_flags;
2313 int committed;
2314 int dm_di_mode = 0;
2315 int link_zero;
2316 uint resblks;
2317 int namelen;
2318
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002319 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (XFS_FORCED_SHUTDOWN(mp))
2322 return XFS_ERROR(EIO);
2323
2324 namelen = VNAMELEN(dentry);
2325
Vlad Apostolov17370092006-09-28 11:02:30 +10002326 if (!xfs_get_dir_entry(dentry, &ip)) {
2327 dm_di_mode = ip->i_d.di_mode;
2328 IRELE(ip);
2329 }
2330
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002331 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2333 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Vlad Apostolov17370092006-09-28 11:02:30 +10002334 name, NULL, dm_di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (error)
2336 return error;
2337 }
2338
2339 /* From this point on, return through std_return */
2340 ip = NULL;
2341
2342 /*
2343 * We need to get a reference to ip before we get our log
2344 * reservation. The reason for this is that we cannot call
2345 * xfs_iget for an inode for which we do not have a reference
2346 * once we've acquired a log reservation. This is because the
2347 * inode we are trying to get might be in xfs_inactive going
2348 * for a log reservation. Since we'll have to wait for the
2349 * inactive code to complete before returning from xfs_iget,
2350 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002351 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 * to the inode before getting our log reservation.
2353 */
2354 error = xfs_get_dir_entry(dentry, &ip);
2355 if (error) {
2356 REMOVE_DEBUG_TRACE(__LINE__);
2357 goto std_return;
2358 }
2359
2360 dm_di_mode = ip->i_d.di_mode;
2361
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002362 xfs_itrace_entry(ip);
2363 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 error = XFS_QM_DQATTACH(mp, dp, 0);
2366 if (!error && dp != ip)
2367 error = XFS_QM_DQATTACH(mp, ip, 0);
2368 if (error) {
2369 REMOVE_DEBUG_TRACE(__LINE__);
2370 IRELE(ip);
2371 goto std_return;
2372 }
2373
2374 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2375 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2376 /*
2377 * We try to get the real space reservation first,
2378 * allowing for directory btree deletion(s) implying
2379 * possible bmap insert(s). If we can't get the space
2380 * reservation then we use 0 instead, and avoid the bmap
2381 * btree insert(s) in the directory code by, if the bmap
2382 * insert tries to happen, instead trimming the LAST
2383 * block from the directory.
2384 */
2385 resblks = XFS_REMOVE_SPACE_RES(mp);
2386 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2387 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2388 if (error == ENOSPC) {
2389 resblks = 0;
2390 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2391 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2392 }
2393 if (error) {
2394 ASSERT(error != ENOSPC);
2395 REMOVE_DEBUG_TRACE(__LINE__);
2396 xfs_trans_cancel(tp, 0);
2397 IRELE(ip);
2398 return error;
2399 }
2400
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002401 error = xfs_lock_dir_and_entry(dp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (error) {
2403 REMOVE_DEBUG_TRACE(__LINE__);
2404 xfs_trans_cancel(tp, cancel_flags);
2405 IRELE(ip);
2406 goto std_return;
2407 }
2408
2409 /*
2410 * At this point, we've gotten both the directory and the entry
2411 * inodes locked.
2412 */
2413 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2414 if (dp != ip) {
2415 /*
2416 * Increment vnode ref count only in this case since
2417 * there's an extra vnode reference in the case where
2418 * dp == ip.
2419 */
2420 IHOLD(dp);
2421 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2422 }
2423
2424 /*
2425 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2426 */
2427 XFS_BMAP_INIT(&free_list, &first_block);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002428 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2429 &first_block, &free_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (error) {
2431 ASSERT(error != ENOENT);
2432 REMOVE_DEBUG_TRACE(__LINE__);
2433 goto error1;
2434 }
2435 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2436
2437 dp->i_gen++;
2438 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2439
2440 error = xfs_droplink(tp, ip);
2441 if (error) {
2442 REMOVE_DEBUG_TRACE(__LINE__);
2443 goto error1;
2444 }
2445
2446 /* Determine if this is the last link while
2447 * we are in the transaction.
2448 */
2449 link_zero = (ip)->i_d.di_nlink==0;
2450
2451 /*
2452 * Take an extra ref on the inode so that it doesn't
2453 * go to xfs_inactive() from within the commit.
2454 */
2455 IHOLD(ip);
2456
2457 /*
2458 * If this is a synchronous mount, make sure that the
2459 * remove transaction goes to disk before returning to
2460 * the user.
2461 */
2462 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2463 xfs_trans_set_sync(tp);
2464 }
2465
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002466 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (error) {
2468 REMOVE_DEBUG_TRACE(__LINE__);
2469 goto error_rele;
2470 }
2471
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002472 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (error) {
2474 IRELE(ip);
2475 goto std_return;
2476 }
2477
2478 /*
2479 * Before we drop our extra reference to the inode, purge it
2480 * from the refcache if it is there. By waiting until afterwards
2481 * to do the IRELE, we ensure that we won't go inactive in the
2482 * xfs_refcache_purge_ip routine (although that would be OK).
2483 */
2484 xfs_refcache_purge_ip(ip);
2485
David Chinner2a82b8b2007-07-11 11:09:12 +10002486 /*
2487 * If we are using filestreams, kill the stream association.
2488 * If the file is still open it may get a new one but that
2489 * will get killed on last close in xfs_close() so we don't
2490 * have to worry about that.
2491 */
2492 if (link_zero && xfs_inode_is_filestream(ip))
2493 xfs_filestream_deassociate(ip);
2494
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002495 xfs_itrace_exit(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 IRELE(ip);
2497
2498/* Fall through to std_return with error = 0 */
2499 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002500 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2502 dir_vp, DM_RIGHT_NULL,
2503 NULL, DM_RIGHT_NULL,
2504 name, NULL, dm_di_mode, error, 0);
2505 }
2506 return error;
2507
2508 error1:
2509 xfs_bmap_cancel(&free_list);
2510 cancel_flags |= XFS_TRANS_ABORT;
2511 xfs_trans_cancel(tp, cancel_flags);
2512 goto std_return;
2513
2514 error_rele:
2515 /*
2516 * In this case make sure to not release the inode until after
2517 * the current transaction is aborted. Releasing it beforehand
2518 * can cause us to go to xfs_inactive and start a recursive
2519 * transaction which can easily deadlock with the current one.
2520 */
2521 xfs_bmap_cancel(&free_list);
2522 cancel_flags |= XFS_TRANS_ABORT;
2523 xfs_trans_cancel(tp, cancel_flags);
2524
2525 /*
2526 * Before we drop our extra reference to the inode, purge it
2527 * from the refcache if it is there. By waiting until afterwards
2528 * to do the IRELE, we ensure that we won't go inactive in the
2529 * xfs_refcache_purge_ip routine (although that would be OK).
2530 */
2531 xfs_refcache_purge_ip(ip);
2532
2533 IRELE(ip);
2534
2535 goto std_return;
2536}
2537
Christoph Hellwig993386c2007-08-28 16:12:30 +10002538int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002540 xfs_inode_t *tdp,
Nathan Scott67fcaa72006-06-09 17:00:52 +10002541 bhv_vnode_t *src_vp,
Christoph Hellwig993386c2007-08-28 16:12:30 +10002542 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002544 bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
2545 xfs_mount_t *mp = tdp->i_mount;
2546 xfs_inode_t *sip = xfs_vtoi(src_vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 xfs_inode_t *ips[2];
2549 int error;
2550 xfs_bmap_free_t free_list;
2551 xfs_fsblock_t first_block;
2552 int cancel_flags;
2553 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 int resblks;
2555 char *target_name = VNAME(dentry);
2556 int target_namelen;
2557
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002558 xfs_itrace_entry(tdp);
2559 xfs_itrace_entry(xfs_vtoi(src_vp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 target_namelen = VNAMELEN(dentry);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002562 ASSERT(!VN_ISDIR(src_vp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (XFS_FORCED_SHUTDOWN(mp))
2565 return XFS_ERROR(EIO);
2566
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002567 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2569 target_dir_vp, DM_RIGHT_NULL,
2570 src_vp, DM_RIGHT_NULL,
2571 target_name, NULL, 0, 0, 0);
2572 if (error)
2573 return error;
2574 }
2575
2576 /* Return through std_return after this point. */
2577
2578 error = XFS_QM_DQATTACH(mp, sip, 0);
2579 if (!error && sip != tdp)
2580 error = XFS_QM_DQATTACH(mp, tdp, 0);
2581 if (error)
2582 goto std_return;
2583
2584 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2585 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2586 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2587 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2588 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2589 if (error == ENOSPC) {
2590 resblks = 0;
2591 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2592 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2593 }
2594 if (error) {
2595 cancel_flags = 0;
2596 goto error_return;
2597 }
2598
2599 if (sip->i_ino < tdp->i_ino) {
2600 ips[0] = sip;
2601 ips[1] = tdp;
2602 } else {
2603 ips[0] = tdp;
2604 ips[1] = sip;
2605 }
2606
2607 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2608
2609 /*
2610 * Increment vnode ref counts since xfs_trans_commit &
2611 * xfs_trans_cancel will both unlock the inodes and
2612 * decrement the associated ref counts.
2613 */
2614 VN_HOLD(src_vp);
2615 VN_HOLD(target_dir_vp);
2616 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2617 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2618
2619 /*
2620 * If the source has too many links, we can't make any more to it.
2621 */
2622 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2623 error = XFS_ERROR(EMLINK);
2624 goto error_return;
2625 }
2626
Nathan Scott365ca832005-06-21 15:39:12 +10002627 /*
2628 * If we are using project inheritance, we only allow hard link
2629 * creation in our tree when the project IDs are the same; else
2630 * the tree quota mechanism could be circumvented.
2631 */
2632 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2633 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002634 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002635 goto error_return;
2636 }
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002639 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 goto error_return;
2641
2642 XFS_BMAP_INIT(&free_list, &first_block);
2643
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002644 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 sip->i_ino, &first_block, &free_list,
2646 resblks);
2647 if (error)
2648 goto abort_return;
2649 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2650 tdp->i_gen++;
2651 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2652
2653 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002654 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 /*
2658 * If this is a synchronous mount, make sure that the
2659 * link transaction goes to disk before returning to
2660 * the user.
2661 */
2662 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2663 xfs_trans_set_sync(tp);
2664 }
2665
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002666 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 if (error) {
2668 xfs_bmap_cancel(&free_list);
2669 goto abort_return;
2670 }
2671
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002672 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002673 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676 /* Fall through to std_return with error = 0. */
2677std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002678 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2680 target_dir_vp, DM_RIGHT_NULL,
2681 src_vp, DM_RIGHT_NULL,
2682 target_name, NULL, 0, error, 0);
2683 }
2684 return error;
2685
2686 abort_return:
2687 cancel_flags |= XFS_TRANS_ABORT;
2688 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 error_return:
2691 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 goto std_return;
2693}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002694
2695
Christoph Hellwig993386c2007-08-28 16:12:30 +10002696int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002698 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10002699 bhv_vname_t *dentry,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002700 mode_t mode,
Nathan Scott67fcaa72006-06-09 17:00:52 +10002701 bhv_vnode_t **vpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 cred_t *credp)
2703{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002704 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 char *dir_name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002706 int dir_namelen = VNAMELEN(dentry);
2707 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 xfs_inode_t *cdp; /* inode of created dir */
Nathan Scott67fcaa72006-06-09 17:00:52 +10002709 bhv_vnode_t *cvp; /* vnode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 int cancel_flags;
2712 int error;
2713 int committed;
2714 xfs_bmap_free_t free_list;
2715 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002716 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 boolean_t created = B_FALSE;
2718 int dm_event_sent = 0;
2719 xfs_prid_t prid;
2720 struct xfs_dquot *udqp, *gdqp;
2721 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 if (XFS_FORCED_SHUTDOWN(mp))
2724 return XFS_ERROR(EIO);
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002728 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2730 dir_vp, DM_RIGHT_NULL, NULL,
2731 DM_RIGHT_NULL, dir_name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002732 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 if (error)
2734 return error;
2735 dm_event_sent = 1;
2736 }
2737
2738 /* Return through std_return after this point. */
2739
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002740 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 mp = dp->i_mount;
2743 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002744 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2745 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 else
2747 prid = (xfs_prid_t)dfltprid;
2748
2749 /*
2750 * Make sure that we have allocated dquot(s) on disk.
2751 */
2752 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002753 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2755 if (error)
2756 goto std_return;
2757
2758 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2759 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2760 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2761 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2762 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2763 if (error == ENOSPC) {
2764 resblks = 0;
2765 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2766 XFS_TRANS_PERM_LOG_RES,
2767 XFS_MKDIR_LOG_COUNT);
2768 }
2769 if (error) {
2770 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 goto error_return;
2772 }
2773
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002774 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002775 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 /*
2778 * Check for directory link count overflow.
2779 */
2780 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2781 error = XFS_ERROR(EMLINK);
2782 goto error_return;
2783 }
2784
2785 /*
2786 * Reserve disk quota and the inode.
2787 */
2788 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2789 if (error)
2790 goto error_return;
2791
2792 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002793 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 goto error_return;
2795 /*
2796 * create the directory inode.
2797 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002798 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 0, credp, prid, resblks > 0,
2800 &cdp, NULL);
2801 if (error) {
2802 if (error == ENOSPC)
2803 goto error_return;
2804 goto abort_return;
2805 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002806 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 /*
2809 * Now we add the directory inode to the transaction.
2810 * We waited until now since xfs_dir_ialloc might start
2811 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002812 * earlier, the locks might have gotten released. An error
2813 * from here on will result in the transaction cancel
2814 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 */
2816 VN_HOLD(dir_vp);
2817 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002818 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
2820 XFS_BMAP_INIT(&free_list, &first_block);
2821
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002822 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2823 &first_block, &free_list, resblks ?
2824 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (error) {
2826 ASSERT(error != ENOSPC);
2827 goto error1;
2828 }
2829 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2830
2831 /*
2832 * Bump the in memory version number of the parent directory
2833 * so that other processes accessing it will recognize that
2834 * the directory has changed.
2835 */
2836 dp->i_gen++;
2837
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002838 error = xfs_dir_init(tp, cdp, dp);
2839 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 cdp->i_gen = 1;
2843 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002844 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 cvp = XFS_ITOV(cdp);
2848
2849 created = B_TRUE;
2850
2851 *vpp = cvp;
2852 IHOLD(cdp);
2853
2854 /*
2855 * Attach the dquots to the new inode and modify the icount incore.
2856 */
2857 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2858
2859 /*
2860 * If this is a synchronous mount, make sure that the
2861 * mkdir transaction goes to disk before returning to
2862 * the user.
2863 */
2864 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2865 xfs_trans_set_sync(tp);
2866 }
2867
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002868 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (error) {
2870 IRELE(cdp);
2871 goto error2;
2872 }
2873
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002874 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 XFS_QM_DQRELE(mp, udqp);
2876 XFS_QM_DQRELE(mp, gdqp);
2877 if (error) {
2878 IRELE(cdp);
2879 }
2880
2881 /* Fall through to std_return with error = 0 or errno from
2882 * xfs_trans_commit. */
2883
2884std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002885 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002886 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2888 dir_vp, DM_RIGHT_NULL,
2889 created ? XFS_ITOV(cdp):NULL,
2890 DM_RIGHT_NULL,
2891 dir_name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002892 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
2894 return error;
2895
2896 error2:
2897 error1:
2898 xfs_bmap_cancel(&free_list);
2899 abort_return:
2900 cancel_flags |= XFS_TRANS_ABORT;
2901 error_return:
2902 xfs_trans_cancel(tp, cancel_flags);
2903 XFS_QM_DQRELE(mp, udqp);
2904 XFS_QM_DQRELE(mp, gdqp);
2905
Christoph Hellwig993386c2007-08-28 16:12:30 +10002906 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
2909 goto std_return;
2910}
2911
Christoph Hellwig993386c2007-08-28 16:12:30 +10002912int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913xfs_rmdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002914 xfs_inode_t *dp,
2915 bhv_vname_t *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002917 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 char *name = VNAME(dentry);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002919 int namelen = VNAMELEN(dentry);
2920 xfs_mount_t *mp = dp->i_mount;
2921 xfs_inode_t *cdp; /* child directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 int error;
2924 xfs_bmap_free_t free_list;
2925 xfs_fsblock_t first_block;
2926 int cancel_flags;
2927 int committed;
Vlad Apostolov17370092006-09-28 11:02:30 +10002928 int dm_di_mode = S_IFDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 int last_cdp_link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 uint resblks;
2931
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002932 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Christoph Hellwig993386c2007-08-28 16:12:30 +10002934 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Vlad Apostolov17370092006-09-28 11:02:30 +10002937 if (!xfs_get_dir_entry(dentry, &cdp)) {
2938 dm_di_mode = cdp->i_d.di_mode;
2939 IRELE(cdp);
2940 }
2941
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002942 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
2944 dir_vp, DM_RIGHT_NULL,
2945 NULL, DM_RIGHT_NULL,
Vlad Apostolov17370092006-09-28 11:02:30 +10002946 name, NULL, dm_di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (error)
2948 return XFS_ERROR(error);
2949 }
2950
2951 /* Return through std_return after this point. */
2952
2953 cdp = NULL;
2954
2955 /*
2956 * We need to get a reference to cdp before we get our log
2957 * reservation. The reason for this is that we cannot call
2958 * xfs_iget for an inode for which we do not have a reference
2959 * once we've acquired a log reservation. This is because the
2960 * inode we are trying to get might be in xfs_inactive going
2961 * for a log reservation. Since we'll have to wait for the
2962 * inactive code to complete before returning from xfs_iget,
2963 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002964 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 * to the inode before getting our log reservation.
2966 */
2967 error = xfs_get_dir_entry(dentry, &cdp);
2968 if (error) {
2969 REMOVE_DEBUG_TRACE(__LINE__);
2970 goto std_return;
2971 }
2972 mp = dp->i_mount;
2973 dm_di_mode = cdp->i_d.di_mode;
2974
2975 /*
2976 * Get the dquots for the inodes.
2977 */
2978 error = XFS_QM_DQATTACH(mp, dp, 0);
2979 if (!error && dp != cdp)
2980 error = XFS_QM_DQATTACH(mp, cdp, 0);
2981 if (error) {
2982 IRELE(cdp);
2983 REMOVE_DEBUG_TRACE(__LINE__);
2984 goto std_return;
2985 }
2986
2987 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2988 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2989 /*
2990 * We try to get the real space reservation first,
2991 * allowing for directory btree deletion(s) implying
2992 * possible bmap insert(s). If we can't get the space
2993 * reservation then we use 0 instead, and avoid the bmap
2994 * btree insert(s) in the directory code by, if the bmap
2995 * insert tries to happen, instead trimming the LAST
2996 * block from the directory.
2997 */
2998 resblks = XFS_REMOVE_SPACE_RES(mp);
2999 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3000 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3001 if (error == ENOSPC) {
3002 resblks = 0;
3003 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3004 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3005 }
3006 if (error) {
3007 ASSERT(error != ENOSPC);
3008 cancel_flags = 0;
3009 IRELE(cdp);
3010 goto error_return;
3011 }
3012 XFS_BMAP_INIT(&free_list, &first_block);
3013
3014 /*
3015 * Now lock the child directory inode and the parent directory
3016 * inode in the proper order. This will take care of validating
3017 * that the directory entry for the child directory inode has
3018 * not changed while we were obtaining a log reservation.
3019 */
Eric Sandeene9ed9d22007-05-08 13:48:56 +10003020 error = xfs_lock_dir_and_entry(dp, cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if (error) {
3022 xfs_trans_cancel(tp, cancel_flags);
3023 IRELE(cdp);
3024 goto std_return;
3025 }
3026
3027 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3028 if (dp != cdp) {
3029 /*
3030 * Only increment the parent directory vnode count if
3031 * we didn't bump it in looking up cdp. The only time
3032 * we don't bump it is when we're looking up ".".
3033 */
3034 VN_HOLD(dir_vp);
3035 }
3036
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003037 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3039
3040 ASSERT(cdp->i_d.di_nlink >= 2);
3041 if (cdp->i_d.di_nlink != 2) {
3042 error = XFS_ERROR(ENOTEMPTY);
3043 goto error_return;
3044 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003045 if (!xfs_dir_isempty(cdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 error = XFS_ERROR(ENOTEMPTY);
3047 goto error_return;
3048 }
3049
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003050 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3051 &first_block, &free_list, resblks);
3052 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
3055 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3056
3057 /*
3058 * Bump the in memory generation count on the parent
3059 * directory so that other can know that it has changed.
3060 */
3061 dp->i_gen++;
3062
3063 /*
3064 * Drop the link from cdp's "..".
3065 */
3066 error = xfs_droplink(tp, dp);
3067 if (error) {
3068 goto error1;
3069 }
3070
3071 /*
3072 * Drop the link from dp to cdp.
3073 */
3074 error = xfs_droplink(tp, cdp);
3075 if (error) {
3076 goto error1;
3077 }
3078
3079 /*
3080 * Drop the "." link from cdp to self.
3081 */
3082 error = xfs_droplink(tp, cdp);
3083 if (error) {
3084 goto error1;
3085 }
3086
3087 /* Determine these before committing transaction */
3088 last_cdp_link = (cdp)->i_d.di_nlink==0;
3089
3090 /*
3091 * Take an extra ref on the child vnode so that it
3092 * does not go to xfs_inactive() from within the commit.
3093 */
3094 IHOLD(cdp);
3095
3096 /*
3097 * If this is a synchronous mount, make sure that the
3098 * rmdir transaction goes to disk before returning to
3099 * the user.
3100 */
3101 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3102 xfs_trans_set_sync(tp);
3103 }
3104
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003105 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if (error) {
3107 xfs_bmap_cancel(&free_list);
3108 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3109 XFS_TRANS_ABORT));
3110 IRELE(cdp);
3111 goto std_return;
3112 }
3113
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003114 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 if (error) {
3116 IRELE(cdp);
3117 goto std_return;
3118 }
3119
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 IRELE(cdp);
3122
3123 /* Fall through to std_return with error = 0 or the errno
3124 * from xfs_trans_commit. */
Nathan Scottbb19fba2006-03-22 14:12:12 +11003125 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003126 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3128 dir_vp, DM_RIGHT_NULL,
3129 NULL, DM_RIGHT_NULL,
3130 name, NULL, dm_di_mode,
3131 error, 0);
3132 }
3133 return error;
3134
Nathan Scottbb19fba2006-03-22 14:12:12 +11003135 error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 xfs_bmap_cancel(&free_list);
3137 cancel_flags |= XFS_TRANS_ABORT;
Jesper Juhl014c2542006-01-15 02:37:08 +01003138 /* FALLTHROUGH */
3139
Nathan Scottbb19fba2006-03-22 14:12:12 +11003140 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 xfs_trans_cancel(tp, cancel_flags);
3142 goto std_return;
3143}
3144
Christoph Hellwig993386c2007-08-28 16:12:30 +10003145int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003147 xfs_inode_t *dp,
Nathan Scott8285fb52006-06-09 17:07:12 +10003148 bhv_vname_t *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10003150 mode_t mode,
Nathan Scott67fcaa72006-06-09 17:00:52 +10003151 bhv_vnode_t **vpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 cred_t *credp)
3153{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003154 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
3155 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 xfs_inode_t *ip;
3158 int error;
3159 int pathlen;
3160 xfs_bmap_free_t free_list;
3161 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10003162 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 uint cancel_flags;
3164 int committed;
3165 xfs_fileoff_t first_fsb;
3166 xfs_filblks_t fs_blocks;
3167 int nmaps;
3168 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3169 xfs_daddr_t d;
3170 char *cur_chunk;
3171 int byte_cnt;
3172 int n;
3173 xfs_buf_t *bp;
3174 xfs_prid_t prid;
3175 struct xfs_dquot *udqp, *gdqp;
3176 uint resblks;
3177 char *link_name = VNAME(dentry);
3178 int link_namelen;
3179
3180 *vpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 error = 0;
3182 ip = NULL;
3183 tp = NULL;
3184
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003185 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 if (XFS_FORCED_SHUTDOWN(mp))
3188 return XFS_ERROR(EIO);
3189
3190 link_namelen = VNAMELEN(dentry);
3191
3192 /*
3193 * Check component lengths of the target path name.
3194 */
3195 pathlen = strlen(target_path);
3196 if (pathlen >= MAXPATHLEN) /* total string too long */
3197 return XFS_ERROR(ENAMETOOLONG);
3198 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3199 int len, total;
3200 char *path;
3201
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003202 for (total = 0, path = target_path; total < pathlen;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 /*
3204 * Skip any slashes.
3205 */
3206 while(*path == '/') {
3207 total++;
3208 path++;
3209 }
3210
3211 /*
3212 * Count up to the next slash or end of path.
3213 * Error out if the component is bigger than MAXNAMELEN.
3214 */
3215 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3216 if (++len >= MAXNAMELEN) {
3217 error = ENAMETOOLONG;
3218 return error;
3219 }
3220 }
3221 }
3222 }
3223
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003224 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3226 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3227 link_name, target_path, 0, 0, 0);
3228 if (error)
3229 return error;
3230 }
3231
3232 /* Return through std_return after this point. */
3233
3234 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10003235 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3236 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 else
3238 prid = (xfs_prid_t)dfltprid;
3239
3240 /*
3241 * Make sure that we have allocated dquot(s) on disk.
3242 */
3243 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10003244 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3246 if (error)
3247 goto std_return;
3248
3249 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3250 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3251 /*
3252 * The symlink will fit into the inode data fork?
3253 * There can't be any attributes so we get the whole variable part.
3254 */
3255 if (pathlen <= XFS_LITINO(mp))
3256 fs_blocks = 0;
3257 else
3258 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3259 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3260 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3261 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3262 if (error == ENOSPC && fs_blocks == 0) {
3263 resblks = 0;
3264 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3265 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3266 }
3267 if (error) {
3268 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 goto error_return;
3270 }
3271
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10003272 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10003273 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 /*
3276 * Check whether the directory allows new symlinks or not.
3277 */
3278 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3279 error = XFS_ERROR(EPERM);
3280 goto error_return;
3281 }
3282
3283 /*
3284 * Reserve disk quota : blocks and inode.
3285 */
3286 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3287 if (error)
3288 goto error_return;
3289
3290 /*
3291 * Check for ability to enter directory entry, if no space reserved.
3292 */
3293 if (resblks == 0 &&
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003294 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 goto error_return;
3296 /*
3297 * Initialize the bmap freelist prior to calling either
3298 * bmapi or the directory create code.
3299 */
3300 XFS_BMAP_INIT(&free_list, &first_block);
3301
3302 /*
3303 * Allocate an inode for the symlink.
3304 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10003305 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 1, 0, credp, prid, resblks > 0, &ip, NULL);
3307 if (error) {
3308 if (error == ENOSPC)
3309 goto error_return;
3310 goto error1;
3311 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003312 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Christoph Hellwig993386c2007-08-28 16:12:30 +10003314 /*
3315 * An error after we've joined dp to the transaction will result in the
3316 * transaction cancel unlocking dp so don't do it explicitly in the
3317 * error path.
3318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 VN_HOLD(dir_vp);
3320 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10003321 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
3323 /*
3324 * Also attach the dquot(s) to it, if applicable.
3325 */
3326 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3327
3328 if (resblks)
3329 resblks -= XFS_IALLOC_SPACE_RES(mp);
3330 /*
3331 * If the symlink will fit into the inode, write it inline.
3332 */
3333 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3334 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3335 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3336 ip->i_d.di_size = pathlen;
3337
3338 /*
3339 * The inode was initially created in extent format.
3340 */
3341 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3342 ip->i_df.if_flags |= XFS_IFINLINE;
3343
3344 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3345 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3346
3347 } else {
3348 first_fsb = 0;
3349 nmaps = SYMLINK_MAPS;
3350
3351 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3352 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3353 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003354 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 if (error) {
3356 goto error1;
3357 }
3358
3359 if (resblks)
3360 resblks -= fs_blocks;
3361 ip->i_d.di_size = pathlen;
3362 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3363
3364 cur_chunk = target_path;
3365 for (n = 0; n < nmaps; n++) {
3366 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3367 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3368 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3369 BTOBB(byte_cnt), 0);
3370 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3371 if (pathlen < byte_cnt) {
3372 byte_cnt = pathlen;
3373 }
3374 pathlen -= byte_cnt;
3375
3376 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3377 cur_chunk += byte_cnt;
3378
3379 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3380 }
3381 }
3382
3383 /*
3384 * Create the directory entry for the symlink.
3385 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003386 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3387 &first_block, &free_list, resblks);
3388 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3391 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3392
3393 /*
3394 * Bump the in memory version number of the parent directory
3395 * so that other processes accessing it will recognize that
3396 * the directory has changed.
3397 */
3398 dp->i_gen++;
3399
3400 /*
3401 * If this is a synchronous mount, make sure that the
3402 * symlink transaction goes to disk before returning to
3403 * the user.
3404 */
3405 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3406 xfs_trans_set_sync(tp);
3407 }
3408
3409 /*
3410 * xfs_trans_commit normally decrements the vnode ref count
3411 * when it unlocks the inode. Since we want to return the
3412 * vnode to the caller, we bump the vnode ref count now.
3413 */
3414 IHOLD(ip);
3415
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003416 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 if (error) {
3418 goto error2;
3419 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003420 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 XFS_QM_DQRELE(mp, udqp);
3422 XFS_QM_DQRELE(mp, gdqp);
3423
3424 /* Fall through to std_return with error = 0 or errno from
3425 * xfs_trans_commit */
3426std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10003427 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3429 dir_vp, DM_RIGHT_NULL,
3430 error ? NULL : XFS_ITOV(ip),
3431 DM_RIGHT_NULL, link_name, target_path,
3432 0, error, 0);
3433 }
3434
3435 if (!error) {
Nathan Scott67fcaa72006-06-09 17:00:52 +10003436 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
3438 ASSERT(ip);
3439 vp = XFS_ITOV(ip);
3440 *vpp = vp;
3441 }
3442 return error;
3443
3444 error2:
3445 IRELE(ip);
3446 error1:
3447 xfs_bmap_cancel(&free_list);
3448 cancel_flags |= XFS_TRANS_ABORT;
3449 error_return:
3450 xfs_trans_cancel(tp, cancel_flags);
3451 XFS_QM_DQRELE(mp, udqp);
3452 XFS_QM_DQRELE(mp, gdqp);
3453
Christoph Hellwig993386c2007-08-28 16:12:30 +10003454 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 goto std_return;
3458}
3459
3460
Christoph Hellwig993386c2007-08-28 16:12:30 +10003461int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462xfs_fid2(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003463 xfs_inode_t *ip,
Christoph Hellwigc6143912007-09-14 15:22:37 +10003464 xfs_fid_t *xfid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003466 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Christoph Hellwigc6143912007-09-14 15:22:37 +10003468 xfid->fid_len = sizeof(xfs_fid_t) - sizeof(xfid->fid_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 xfid->fid_pad = 0;
3470 /*
3471 * use memcpy because the inode is a long long and there's no
3472 * assurance that xfid->fid_ino is properly aligned.
3473 */
3474 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3475 xfid->fid_gen = ip->i_d.di_gen;
3476
3477 return 0;
3478}
3479
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481int
3482xfs_rwlock(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003483 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +10003484 bhv_vrwlock_t locktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003486 if (S_ISDIR(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 if (locktype == VRWLOCK_WRITE) {
3489 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3490 } else if (locktype == VRWLOCK_TRY_READ) {
Jesper Juhl014c2542006-01-15 02:37:08 +01003491 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 } else if (locktype == VRWLOCK_TRY_WRITE) {
Jesper Juhl014c2542006-01-15 02:37:08 +01003493 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 } else {
3495 ASSERT((locktype == VRWLOCK_READ) ||
3496 (locktype == VRWLOCK_WRITE_DIRECT));
3497 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3498 }
3499
3500 return 1;
3501}
3502
3503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504void
3505xfs_rwunlock(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003506 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +10003507 bhv_vrwlock_t locktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003509 if (S_ISDIR(ip->i_d.di_mode))
3510 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (locktype == VRWLOCK_WRITE) {
3512 /*
3513 * In the write case, we may have added a new entry to
3514 * the reference cache. This might store a pointer to
3515 * an inode to be released in this inode. If it is there,
3516 * clear the pointer and release the inode after unlocking
3517 * this one.
3518 */
3519 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3520 } else {
3521 ASSERT((locktype == VRWLOCK_READ) ||
3522 (locktype == VRWLOCK_WRITE_DIRECT));
3523 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3524 }
3525 return;
3526}
3527
Christoph Hellwig993386c2007-08-28 16:12:30 +10003528
3529int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003531 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 int flags)
3533{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003534 xfs_mount_t *mp = ip->i_mount;
3535 xfs_inode_log_item_t *iip = ip->i_itemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 int error = 0;
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (XFS_FORCED_SHUTDOWN(mp))
3539 return XFS_ERROR(EIO);
3540
3541 /*
3542 * Bypass inodes which have already been cleaned by
3543 * the inode flush clustering code inside xfs_iflush
3544 */
3545 if ((ip->i_update_core == 0) &&
3546 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3547 return 0;
3548
3549 if (flags & FLUSH_LOG) {
3550 if (iip && iip->ili_last_lsn) {
3551 xlog_t *log = mp->m_log;
3552 xfs_lsn_t sync_lsn;
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003553 int log_flags = XFS_LOG_FORCE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003555 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 sync_lsn = log->l_last_sync_lsn;
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003557 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +10003559 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) > 0)) {
3560 if (flags & FLUSH_SYNC)
3561 log_flags |= XFS_LOG_SYNC;
3562 error = xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3563 if (error)
3564 return error;
3565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +10003567 if (ip->i_update_core == 0)
3568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
3570 }
3571
3572 /*
3573 * We make this non-blocking if the inode is contended,
3574 * return EAGAIN to indicate to the caller that they
3575 * did not succeed. This prevents the flush path from
3576 * blocking on inodes inside another operation right
3577 * now, they get caught later by xfs_sync.
3578 */
3579 if (flags & FLUSH_INODE) {
3580 int flush_flags;
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 if (flags & FLUSH_SYNC) {
3583 xfs_ilock(ip, XFS_ILOCK_SHARED);
3584 xfs_iflock(ip);
3585 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3586 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3587 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3588 return EAGAIN;
3589 }
3590 } else {
3591 return EAGAIN;
3592 }
3593
3594 if (flags & FLUSH_SYNC)
3595 flush_flags = XFS_IFLUSH_SYNC;
3596 else
3597 flush_flags = XFS_IFLUSH_ASYNC;
3598
3599 error = xfs_iflush(ip, flush_flags);
3600 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3601 }
3602
3603 return error;
3604}
3605
Christoph Hellwig993386c2007-08-28 16:12:30 +10003606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607int
Christoph Hellwig993386c2007-08-28 16:12:30 +10003608xfs_set_dmattrs(
3609 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10003611 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003613 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 int error;
3616
3617 if (!capable(CAP_SYS_ADMIN))
3618 return XFS_ERROR(EPERM);
3619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 if (XFS_FORCED_SHUTDOWN(mp))
3621 return XFS_ERROR(EIO);
3622
3623 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3624 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3625 if (error) {
3626 xfs_trans_cancel(tp, 0);
3627 return error;
3628 }
3629 xfs_ilock(ip, XFS_ILOCK_EXCL);
3630 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3631
Christoph Hellwig613d7042007-10-11 17:44:08 +10003632 ip->i_d.di_dmevmask = evmask;
3633 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
3635 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3636 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003637 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
3639 return error;
3640}
3641
Christoph Hellwig993386c2007-08-28 16:12:30 +10003642int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003644 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003646 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003648 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
3650 ASSERT(!VN_MAPPED(vp));
3651
3652 /* bad inode, get out here ASAP */
3653 if (VN_BAD(vp)) {
3654 xfs_ireclaim(ip);
3655 return 0;
3656 }
3657
Christoph Hellwigb677c212007-08-29 11:46:28 +10003658 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10003660 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11003662 /*
3663 * Make sure the atime in the XFS inode is correct before freeing the
3664 * Linux inode.
3665 */
3666 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
David Chinner4c606582006-11-11 18:05:00 +11003668 /*
3669 * If we have nothing to flush with this inode then complete the
3670 * teardown now, otherwise break the link between the xfs inode and the
3671 * linux inode and clean up the xfs inode later. This avoids flushing
3672 * the inode to disk during the delete operation itself.
3673 *
3674 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3675 * first to ensure that xfs_iunpin() will never see an xfs inode
3676 * that has a linux inode being reclaimed. Synchronisation is provided
3677 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 */
3679 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3680 xfs_ilock(ip, XFS_ILOCK_EXCL);
3681 xfs_iflock(ip);
3682 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3683 } else {
3684 xfs_mount_t *mp = ip->i_mount;
3685
David Chinner4c606582006-11-11 18:05:00 +11003686 /* Protect sync and unpin from us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 XFS_MOUNT_ILOCK(mp);
David Chinner4c606582006-11-11 18:05:00 +11003688 spin_lock(&ip->i_flags_lock);
3689 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10003690 vn_to_inode(vp)->i_private = NULL;
3691 ip->i_vnode = NULL;
David Chinner4c606582006-11-11 18:05:00 +11003692 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 XFS_MOUNT_IUNLOCK(mp);
3695 }
3696 return 0;
3697}
3698
3699int
3700xfs_finish_reclaim(
3701 xfs_inode_t *ip,
3702 int locked,
3703 int sync_mode)
3704{
David Chinnerda353b02007-08-28 14:00:13 +10003705 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
Nathan Scott67fcaa72006-06-09 17:00:52 +10003706 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 int error;
3708
3709 if (vp && VN_BAD(vp))
3710 goto reclaim;
3711
3712 /* The hash lock here protects a thread in xfs_iget_core from
3713 * racing with us on linking the inode back with a vnode.
3714 * Once we have the XFS_IRECLAIM flag set it will not touch
3715 * us.
3716 */
David Chinnerda353b02007-08-28 14:00:13 +10003717 write_lock(&pag->pag_ici_lock);
David Chinnerf273ab82006-09-28 11:06:03 +10003718 spin_lock(&ip->i_flags_lock);
David Chinner7a18c382006-11-11 18:04:54 +11003719 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3720 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
David Chinnerf273ab82006-09-28 11:06:03 +10003721 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003722 write_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 if (locked) {
3724 xfs_ifunlock(ip);
3725 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3726 }
Jesper Juhl014c2542006-01-15 02:37:08 +01003727 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 }
David Chinner7a18c382006-11-11 18:04:54 +11003729 __xfs_iflags_set(ip, XFS_IRECLAIM);
David Chinnerf273ab82006-09-28 11:06:03 +10003730 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003731 write_unlock(&pag->pag_ici_lock);
3732 xfs_put_perag(ip->i_mount, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
3734 /*
3735 * If the inode is still dirty, then flush it out. If the inode
3736 * is not in the AIL, then it will be OK to flush it delwri as
3737 * long as xfs_iflush() does not keep any references to the inode.
3738 * We leave that decision up to xfs_iflush() since it has the
3739 * knowledge of whether it's OK to simply do a delwri flush of
3740 * the inode or whether we need to wait until the inode is
3741 * pulled from the AIL.
3742 * We get the flush lock regardless, though, just to make sure
3743 * we don't free it while it is being flushed.
3744 */
3745 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3746 if (!locked) {
3747 xfs_ilock(ip, XFS_ILOCK_EXCL);
3748 xfs_iflock(ip);
3749 }
3750
3751 if (ip->i_update_core ||
3752 ((ip->i_itemp != NULL) &&
3753 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3754 error = xfs_iflush(ip, sync_mode);
3755 /*
3756 * If we hit an error, typically because of filesystem
3757 * shutdown, we don't need to let vn_reclaim to know
3758 * because we're gonna reclaim the inode anyway.
3759 */
3760 if (error) {
3761 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3762 goto reclaim;
3763 }
3764 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3765 }
3766
3767 ASSERT(ip->i_update_core == 0);
3768 ASSERT(ip->i_itemp == NULL ||
3769 ip->i_itemp->ili_format.ilf_fields == 0);
3770 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3771 } else if (locked) {
3772 /*
3773 * We are not interested in doing an iflush if we're
3774 * in the process of shutting down the filesystem forcibly.
3775 * So, just reclaim the inode.
3776 */
3777 xfs_ifunlock(ip);
3778 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3779 }
3780
3781 reclaim:
3782 xfs_ireclaim(ip);
3783 return 0;
3784}
3785
3786int
3787xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3788{
3789 int purged;
3790 xfs_inode_t *ip, *n;
3791 int done = 0;
3792
3793 while (!done) {
3794 purged = 0;
3795 XFS_MOUNT_ILOCK(mp);
3796 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3797 if (noblock) {
3798 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3799 continue;
3800 if (xfs_ipincount(ip) ||
3801 !xfs_iflock_nowait(ip)) {
3802 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3803 continue;
3804 }
3805 }
3806 XFS_MOUNT_IUNLOCK(mp);
Felix Blyakher6b2cf612005-11-25 16:42:13 +11003807 if (xfs_finish_reclaim(ip, noblock,
3808 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3809 delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 purged = 1;
3811 break;
3812 }
3813
3814 done = !purged;
3815 }
3816
3817 XFS_MOUNT_IUNLOCK(mp);
3818 return 0;
3819}
3820
3821/*
3822 * xfs_alloc_file_space()
3823 * This routine allocates disk space for the given file.
3824 *
3825 * If alloc_type == 0, this request is for an ALLOCSP type
3826 * request which will change the file size. In this case, no
3827 * DMAPI event will be generated by the call. A TRUNCATE event
3828 * will be generated later by xfs_setattr.
3829 *
3830 * If alloc_type != 0, this request is for a RESVSP type
3831 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3832 * lower block boundary byte address is less than the file's
3833 * length.
3834 *
3835 * RETURNS:
3836 * 0 on success
3837 * errno on error
3838 *
3839 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003840STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841xfs_alloc_file_space(
3842 xfs_inode_t *ip,
3843 xfs_off_t offset,
3844 xfs_off_t len,
3845 int alloc_type,
3846 int attr_flags)
3847{
Nathan Scottdd9f4382006-01-11 15:28:28 +11003848 xfs_mount_t *mp = ip->i_mount;
3849 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 xfs_filblks_t allocated_fsb;
3851 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003852 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003854 xfs_fsblock_t firstfsb;
3855 int nimaps;
3856 int bmapi_flag;
3857 int quota_flag;
3858 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003860 xfs_bmbt_irec_t imaps[1], *imapp;
3861 xfs_bmap_free_t free_list;
3862 uint qblocks, resblks, resrtextents;
3863 int committed;
3864 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003866 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 if (XFS_FORCED_SHUTDOWN(mp))
3869 return XFS_ERROR(EIO);
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3872 return error;
3873
3874 if (len <= 0)
3875 return XFS_ERROR(EINVAL);
3876
David Chinner957d0eb2007-06-18 16:50:37 +10003877 rt = XFS_IS_REALTIME_INODE(ip);
3878 extsz = xfs_get_extsz_hint(ip);
3879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11003882 nimaps = 1;
3883 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3885 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3886
3887 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003888 if (alloc_type != 0 && offset < ip->i_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 (attr_flags&ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003890 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 xfs_off_t end_dmi_offset;
3892
3893 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003894 if (end_dmi_offset > ip->i_size)
3895 end_dmi_offset = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
3897 offset, end_dmi_offset - offset,
3898 0, NULL);
3899 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003900 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 }
3902
3903 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003904 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 */
3906retry:
3907 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003908 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Nathan Scottdd9f4382006-01-11 15:28:28 +11003910 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003911 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11003912 */
3913 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003915 do_div(s, extsz);
3916 s *= extsz;
3917 e = startoffset_fsb + allocatesize_fsb;
3918 if ((temp = do_mod(startoffset_fsb, extsz)))
3919 e += temp;
3920 if ((temp = do_mod(e, extsz)))
3921 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003923 s = 0;
3924 e = allocatesize_fsb;
3925 }
3926
3927 if (unlikely(rt)) {
3928 resrtextents = qblocks = (uint)(e - s);
3929 resrtextents /= mp->m_sb.sb_rextsize;
3930 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3931 quota_flag = XFS_QMOPT_RES_RTBLKS;
3932 } else {
3933 resrtextents = 0;
3934 resblks = qblocks = \
3935 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3936 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 }
3938
3939 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003940 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 */
3942 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003943 error = xfs_trans_reserve(tp, resblks,
3944 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 XFS_TRANS_PERM_LOG_RES,
3946 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003948 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 */
3950 if (error) {
3951 /*
3952 * Free the transaction structure.
3953 */
3954 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3955 xfs_trans_cancel(tp, 0);
3956 break;
3957 }
3958 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003959 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3960 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 if (error)
3962 goto error1;
3963
3964 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3965 xfs_trans_ihold(tp, ip);
3966
3967 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003968 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 */
3970 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003971 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003972 allocatesize_fsb, bmapi_flag,
3973 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003974 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 if (error) {
3976 goto error0;
3977 }
3978
3979 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003980 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003982 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 if (error) {
3984 goto error0;
3985 }
3986
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003987 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3989 if (error) {
3990 break;
3991 }
3992
3993 allocated_fsb = imapp->br_blockcount;
3994
Nathan Scottdd9f4382006-01-11 15:28:28 +11003995 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 error = XFS_ERROR(ENOSPC);
3997 break;
3998 }
3999
4000 startoffset_fsb += allocated_fsb;
4001 allocatesize_fsb -= allocated_fsb;
4002 }
4003dmapi_enospc_check:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10004004 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
4005 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4007 XFS_ITOV(ip), DM_RIGHT_NULL,
4008 XFS_ITOV(ip), DM_RIGHT_NULL,
4009 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4010 if (error == 0)
4011 goto retry; /* Maybe DMAPI app. has made space */
4012 /* else fall through with error from XFS_SEND_DATA */
4013 }
4014
4015 return error;
4016
Nathan Scottdd9f4382006-01-11 15:28:28 +11004017error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004019 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4020
4021error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4023 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4024 goto dmapi_enospc_check;
4025}
4026
4027/*
4028 * Zero file bytes between startoff and endoff inclusive.
4029 * The iolock is held exclusive and no blocks are buffered.
4030 */
4031STATIC int
4032xfs_zero_remaining_bytes(
4033 xfs_inode_t *ip,
4034 xfs_off_t startoff,
4035 xfs_off_t endoff)
4036{
4037 xfs_bmbt_irec_t imap;
4038 xfs_fileoff_t offset_fsb;
4039 xfs_off_t lastoffset;
4040 xfs_off_t offset;
4041 xfs_buf_t *bp;
4042 xfs_mount_t *mp = ip->i_mount;
4043 int nimap;
4044 int error = 0;
4045
4046 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4047 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4048 mp->m_rtdev_targp : mp->m_ddev_targp);
4049
4050 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4051 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4052 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004053 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004054 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 if (error || nimap < 1)
4056 break;
4057 ASSERT(imap.br_blockcount >= 1);
4058 ASSERT(imap.br_startoff == offset_fsb);
4059 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4060 if (lastoffset > endoff)
4061 lastoffset = endoff;
4062 if (imap.br_startblock == HOLESTARTBLOCK)
4063 continue;
4064 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4065 if (imap.br_state == XFS_EXT_UNWRITTEN)
4066 continue;
4067 XFS_BUF_UNDONE(bp);
4068 XFS_BUF_UNWRITE(bp);
4069 XFS_BUF_READ(bp);
4070 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4071 xfsbdstrat(mp, bp);
4072 if ((error = xfs_iowait(bp))) {
4073 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4074 mp, bp, XFS_BUF_ADDR(bp));
4075 break;
4076 }
4077 memset(XFS_BUF_PTR(bp) +
4078 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4079 0, lastoffset - offset + 1);
4080 XFS_BUF_UNDONE(bp);
4081 XFS_BUF_UNREAD(bp);
4082 XFS_BUF_WRITE(bp);
4083 xfsbdstrat(mp, bp);
4084 if ((error = xfs_iowait(bp))) {
4085 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4086 mp, bp, XFS_BUF_ADDR(bp));
4087 break;
4088 }
4089 }
4090 xfs_buf_free(bp);
4091 return error;
4092}
4093
4094/*
4095 * xfs_free_file_space()
4096 * This routine frees disk space for the given file.
4097 *
4098 * This routine is only called by xfs_change_file_space
4099 * for an UNRESVSP type call.
4100 *
4101 * RETURNS:
4102 * 0 on success
4103 * errno on error
4104 *
4105 */
4106STATIC int
4107xfs_free_file_space(
4108 xfs_inode_t *ip,
4109 xfs_off_t offset,
4110 xfs_off_t len,
4111 int attr_flags)
4112{
Nathan Scott67fcaa72006-06-09 17:00:52 +10004113 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 int committed;
4115 int done;
4116 xfs_off_t end_dmi_offset;
4117 xfs_fileoff_t endoffset_fsb;
4118 int error;
4119 xfs_fsblock_t firstfsb;
4120 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 xfs_bmbt_irec_t imap;
4122 xfs_off_t ioffset;
4123 xfs_extlen_t mod=0;
4124 xfs_mount_t *mp;
4125 int nimap;
4126 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10004127 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 int rt;
4129 xfs_fileoff_t startoffset_fsb;
4130 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07004131 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004133 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 mp = ip->i_mount;
4135
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11004136 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4139 return error;
4140
4141 error = 0;
4142 if (len <= 0) /* if nothing being freed */
4143 return error;
4144 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4145 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4146 end_dmi_offset = offset + len;
4147 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4148
Christoph Hellwigeb9df392007-08-16 18:42:07 +10004149 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
4150 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004151 if (end_dmi_offset > ip->i_size)
4152 end_dmi_offset = ip->i_size;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004153 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 offset, end_dmi_offset - offset,
4155 AT_DELAY_FLAG(attr_flags), NULL);
4156 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01004157 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 }
4159
Dean Roehrich5fcbab32005-05-05 13:27:19 -07004160 if (attr_flags & ATTR_NOLOCK)
4161 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11004162 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10004164 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11004165 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07004166
Nathan Scott673cdf52006-09-28 10:56:26 +10004167 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, NBPP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004169
4170 if (VN_CACHED(vp) != 0) {
Christoph Hellwig613d7042007-10-11 17:44:08 +10004171 xfs_inval_cached_trace(ip, ioffset, -1,
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004172 ctooff(offtoct(ioffset)), -1);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10004173 error = xfs_flushinval_pages(ip,
4174 ctooff(offtoct(ioffset)),
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004175 -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10004176 if (error)
4177 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10004178 }
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 /*
4181 * Need to zero the stuff we're not freeing, on disk.
4182 * If its a realtime file & can't use unwritten extents then we
4183 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4184 * will take care of it for us.
4185 */
4186 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4187 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004188 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004189 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 if (error)
4191 goto out_unlock_iolock;
4192 ASSERT(nimap == 0 || nimap == 1);
4193 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4194 xfs_daddr_t block;
4195
4196 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4197 block = imap.br_startblock;
4198 mod = do_div(block, mp->m_sb.sb_rextsize);
4199 if (mod)
4200 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4201 }
4202 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004203 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004204 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 if (error)
4206 goto out_unlock_iolock;
4207 ASSERT(nimap == 0 || nimap == 1);
4208 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4209 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4210 mod++;
4211 if (mod && (mod != mp->m_sb.sb_rextsize))
4212 endoffset_fsb -= mod;
4213 }
4214 }
4215 if ((done = (endoffset_fsb <= startoffset_fsb)))
4216 /*
4217 * One contiguous piece to clear
4218 */
4219 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4220 else {
4221 /*
4222 * Some full blocks, possibly two pieces to clear
4223 */
4224 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4225 error = xfs_zero_remaining_bytes(ip, offset,
4226 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4227 if (!error &&
4228 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4229 error = xfs_zero_remaining_bytes(ip,
4230 XFS_FSB_TO_B(mp, endoffset_fsb),
4231 offset + len - 1);
4232 }
4233
4234 /*
4235 * free file space until done or until there is an error
4236 */
4237 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4238 while (!error && !done) {
4239
4240 /*
David Chinner91ebecc2007-07-19 16:28:30 +10004241 * allocate and setup the transaction. Allow this
4242 * transaction to dip into the reserve blocks to ensure
4243 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 */
4245 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10004246 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 error = xfs_trans_reserve(tp,
4248 resblks,
4249 XFS_WRITE_LOG_RES(mp),
4250 0,
4251 XFS_TRANS_PERM_LOG_RES,
4252 XFS_WRITE_LOG_COUNT);
4253
4254 /*
4255 * check for running out of space
4256 */
4257 if (error) {
4258 /*
4259 * Free the transaction structure.
4260 */
4261 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4262 xfs_trans_cancel(tp, 0);
4263 break;
4264 }
4265 xfs_ilock(ip, XFS_ILOCK_EXCL);
4266 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004267 ip->i_udquot, ip->i_gdquot, resblks, 0,
4268 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 if (error)
4270 goto error1;
4271
4272 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4273 xfs_trans_ihold(tp, ip);
4274
4275 /*
4276 * issue the bunmapi() call to free the blocks
4277 */
4278 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10004279 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004281 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 if (error) {
4283 goto error0;
4284 }
4285
4286 /*
4287 * complete the transaction
4288 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004289 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 if (error) {
4291 goto error0;
4292 }
4293
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004294 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4296 }
4297
4298 out_unlock_iolock:
4299 if (need_iolock)
4300 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4301 return error;
4302
4303 error0:
4304 xfs_bmap_cancel(&free_list);
4305 error1:
4306 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4307 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4308 XFS_ILOCK_EXCL);
4309 return error;
4310}
4311
4312/*
4313 * xfs_change_file_space()
4314 * This routine allocates or frees disk space for the given file.
4315 * The user specified parameters are checked for alignment and size
4316 * limitations.
4317 *
4318 * RETURNS:
4319 * 0 on success
4320 * errno on error
4321 *
4322 */
4323int
4324xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10004325 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 int cmd,
4327 xfs_flock64_t *bf,
4328 xfs_off_t offset,
4329 cred_t *credp,
4330 int attr_flags)
4331{
Christoph Hellwig993386c2007-08-28 16:12:30 +10004332 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 int clrprealloc;
4334 int error;
4335 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 int setprealloc;
4337 xfs_off_t startoffset;
4338 xfs_off_t llen;
4339 xfs_trans_t *tp;
Nathan Scott8285fb52006-06-09 17:07:12 +10004340 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11004342 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
4344 /*
4345 * must be a regular file and have write permission
4346 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10004347 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return XFS_ERROR(EINVAL);
4349
4350 xfs_ilock(ip, XFS_ILOCK_SHARED);
4351
4352 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4353 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4354 return error;
4355 }
4356
4357 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4358
4359 switch (bf->l_whence) {
4360 case 0: /*SEEK_SET*/
4361 break;
4362 case 1: /*SEEK_CUR*/
4363 bf->l_start += offset;
4364 break;
4365 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004366 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 break;
4368 default:
4369 return XFS_ERROR(EINVAL);
4370 }
4371
4372 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4373
4374 if ( (bf->l_start < 0)
4375 || (bf->l_start > XFS_MAXIOFFSET(mp))
4376 || (bf->l_start + llen < 0)
4377 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4378 return XFS_ERROR(EINVAL);
4379
4380 bf->l_whence = 0;
4381
4382 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004383 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385 /*
4386 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4387 * file space.
4388 * These calls do NOT zero the data space allocated to the file,
4389 * nor do they change the file size.
4390 *
4391 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4392 * space.
4393 * These calls cause the new file data to be zeroed and the file
4394 * size to be changed.
4395 */
4396 setprealloc = clrprealloc = 0;
4397
4398 switch (cmd) {
4399 case XFS_IOC_RESVSP:
4400 case XFS_IOC_RESVSP64:
4401 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4402 1, attr_flags);
4403 if (error)
4404 return error;
4405 setprealloc = 1;
4406 break;
4407
4408 case XFS_IOC_UNRESVSP:
4409 case XFS_IOC_UNRESVSP64:
4410 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4411 attr_flags)))
4412 return error;
4413 break;
4414
4415 case XFS_IOC_ALLOCSP:
4416 case XFS_IOC_ALLOCSP64:
4417 case XFS_IOC_FREESP:
4418 case XFS_IOC_FREESP64:
4419 if (startoffset > fsize) {
4420 error = xfs_alloc_file_space(ip, fsize,
4421 startoffset - fsize, 0, attr_flags);
4422 if (error)
4423 break;
4424 }
4425
4426 va.va_mask = XFS_AT_SIZE;
4427 va.va_size = startoffset;
4428
Christoph Hellwig993386c2007-08-28 16:12:30 +10004429 error = xfs_setattr(ip, &va, attr_flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
4431 if (error)
4432 return error;
4433
4434 clrprealloc = 1;
4435 break;
4436
4437 default:
4438 ASSERT(0);
4439 return XFS_ERROR(EINVAL);
4440 }
4441
4442 /*
4443 * update the inode timestamp, mode, and prealloc flag bits
4444 */
4445 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4446
4447 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4448 0, 0, 0))) {
4449 /* ASSERT(0); */
4450 xfs_trans_cancel(tp, 0);
4451 return error;
4452 }
4453
4454 xfs_ilock(ip, XFS_ILOCK_EXCL);
4455
4456 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4457 xfs_trans_ihold(tp, ip);
4458
4459 if ((attr_flags & ATTR_DMI) == 0) {
4460 ip->i_d.di_mode &= ~S_ISUID;
4461
4462 /*
4463 * Note that we don't have to worry about mandatory
4464 * file locking being disabled here because we only
4465 * clear the S_ISGID bit if the Group execute bit is
4466 * on, but if it was on then mandatory locking wouldn't
4467 * have been enabled.
4468 */
4469 if (ip->i_d.di_mode & S_IXGRP)
4470 ip->i_d.di_mode &= ~S_ISGID;
4471
4472 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4473 }
4474 if (setprealloc)
4475 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4476 else if (clrprealloc)
4477 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4478
4479 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4480 xfs_trans_set_sync(tp);
4481
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004482 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
4484 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4485
4486 return error;
4487}